 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Tue Nov 02, 2010 2:19 pm Post subject: Help removing hardware breakpoint |
|
|
Hi everyone. Here's my problem:
I have been working in a function for setting hardware breakpoints and a VEH for handling them. Now, when the exception EXCEPTION_SINGLE_STEP occurs (I'm setting a breakpoint on execute) I know it must disable the breakpoint to avoid getting into an infinite loop when I try to continue execution. I do call my function of remove breakpoint, inside this function, both Get/SetThreadContext do their work alright, but for some mystical reason, i do enter into this infinite loop.
I have printed Dr0 and Dr7 at the beginning and end on my remove breakpoint function and seems to do it work good, but when i print Dr0 and Dr7 before and after (at VEH) the remove breakpoint function, i get the same results (meaning my remove breakpoint fail).
I will leave my code, and i hope you can help me fix this
| Code: |
#include "stdafx.h"
#pragma auto_inline(off)
PVOID pExceptionHandler;
HANDLE hThread;
void function()
{
printf_s("function executes\n");
_getch();
}
BOOL SetBreakpoint(HANDLE hThread, PVOID lpAddr, DWORD dwDRX, DWORD dwLevel, DWORD dwCondition, DWORD dwLength)
{
CONTEXT lpContext;
if (GetThreadContext(hThread, &lpContext))
{
lpContext.ContextFlags = CONTEXT_DEBUG_REGISTERS;
switch (dwDRX)
{
case DEBUG_REGISTER_0:
lpContext.Dr0 = (DWORD)lpAddr;
lpContext.Dr7 |= BREAKPOINT_LOCAL_EXACT | dwLevel | dwCondition | dwLength;
break;
case DEBUG_REGISTER_1:
lpContext.Dr1 = (DWORD)lpAddr;
lpContext.Dr7 |= BREAKPOINT_LOCAL_EXACT | dwLevel | dwCondition | dwLength;
break;
case DEBUG_REGISTER_2:
lpContext.Dr2 = (DWORD)lpAddr;
lpContext.Dr7 |= BREAKPOINT_LOCAL_EXACT | dwLevel | dwCondition | dwLength;
break;
case DEBUG_REGISTER_3:
lpContext.Dr3 = (DWORD)lpAddr;
lpContext.Dr7 |= BREAKPOINT_LOCAL_EXACT | dwLevel | dwCondition | dwLength;
break;
}
return SetThreadContext(hThread, &lpContext);
}
return 0;
}
BOOL RemoveBreakpoint(HANDLE hThread, PVOID lpAddr, DWORD dwDRX, DWORD dwLevel, DWORD dwCondition, DWORD dwLength)
{
CONTEXT lpContext;
if (GetThreadContext(hThread, &lpContext))
{
lpContext.ContextFlags = CONTEXT_DEBUG_REGISTERS;
switch (dwDRX)
{
case DEBUG_REGISTER_0:
lpContext.Dr0 = (DWORD)0;
lpContext.Dr7 ^= dwLevel | dwCondition | dwLength;
break;
case DEBUG_REGISTER_1:
lpContext.Dr1 = (DWORD)0;
lpContext.Dr7 ^= dwLevel | dwCondition | dwLength;
break;
case DEBUG_REGISTER_2:
lpContext.Dr2 = (DWORD)0;
lpContext.Dr7 ^= dwLevel | dwCondition | dwLength;
break;
case DEBUG_REGISTER_3:
lpContext.Dr3 = (DWORD)0;
lpContext.Dr7 ^= dwLevel | dwCondition | dwLength;
break;
}
return SetThreadContext(hThread, &lpContext);
}
return 0;
}
DWORD WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
if (pExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP)
{
printf_s("[VEH] Breakpoint hit\n");
RemoveBreakpoint(hThread, (PVOID)function, DEBUG_REGISTER_0, DR0_BREAKPOINT_LOCAL, DR0_EXECUTE, DR0_ONE_BYTE);
return EXCEPTION_CONTINUE_EXECUTION;
}
return EXCEPTION_CONTINUE_SEARCH;
}
int main(int argc, char* argv[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
hThread = GetCurrentThread();
pExceptionHandler = AddVectoredExceptionHandler(1,(PVECTORED_EXCEPTION_HANDLER)VectoredExceptionHandler);
SetBreakpoint(hThread, (PVOID)function, DEBUG_REGISTER_0, DR0_BREAKPOINT_LOCAL, DR0_EXECUTE, DR0_ONE_BYTE);
function();
RemoveVectoredExceptionHandler(pExceptionHandler);
return EXIT_SUCCESS;
}
|
| Code: |
#pragma once
#include "targetver.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
// TODO: reference additional headers your program requires here
// Macros
#define DEBUG_REGISTER_0 0x00000001
#define DEBUG_REGISTER_1 0x00000002
#define DEBUG_REGISTER_2 0x00000004
#define DEBUG_REGISTER_3 0x00000008
// Macros for DR0
#define DR0_BREAKPOINT_LOCAL 0x00000001
#define DR0_BREAKPOINT_GLOBAL 0x00000002
#define DR0_WRITE 0x00010000
#define DR0_ACCESS 0x00030000
#define DR0_EXECUTE 0x00000000
#define DR0_ONE_BYTE 0x00000000
#define DR0_TWO_BYTE 0x00040000
#define DR0_FOUR_BYTE 0x000C0000
//Macros for DR1
#define DR1_BREAKPOINT_LOCAL 0x00000004
#define DR1_BREAKPOINT_GLOBAL 0x00000008
#define DR1_WRITE 0x00100000
#define DR1_ACCESS 0x00300000
#define DR1_EXECUTE 0x00000000
#define DR1_ONE_BYTE 0x00000000
#define DR1_TWO_BYTE 0x00400000
#define DR1_FOUR_BYTE 0x00C00000
// Macros for DR2
#define DR2_BREAKPOINT_LOCAL 0x00000010
#define DR2_BREAKPOINT_GLOBAL 0x00000020
#define DR2_WRITE 0x01000000
#define DR2_ACCESS 0x03000000
#define DR2_EXECUTE 0x00000000
#define DR2_ONE_BYTE 0x00000000
#define DR2_TWO_BYTE 0x04000000
#define DR2_FOUR_BYTE 0x0C000000
// Macros for DR3
#define DR3_BREAKPOINT_LOCAL 0x00000040
#define DR3_BREAKPOINT_GLOBAL 0x00000080
#define DR3_WRITE 0x10000000
#define DR3_ACCESS 0x30000000
#define DR3_EXECUTE 0x00000000
#define DR3_ONE_BYTE 0x00000000
#define DR3_TWO_BYTE 0x40000000
#define DR3_FOUR_BYTE 0xC0000000
//Macros for general DR
#define BREAKPOINT_LOCAL_EXACT 0x00000100
#define BREAKPOINT_GLOBAL_EXACT 0x00000200
#define RESERVED_BIT_10 0x00000400
#define GENERAL_DETECT 0x00002000
|
Thanks for you time.
_________________
+~
Last edited by igoticecream on Tue Jul 21, 2015 12:51 pm; edited 1 time in total |
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Tue Nov 02, 2010 2:35 pm Post subject: |
|
|
| Code: | | lpContext.Dr7 ^= dwLevel | dwCondition | dwLength; |
You xor the Dr7 with those values, that means that you FLIP the flags Level, Condition and Length. You might wanna try '&=' to turn them off instead of flipping them. So:
| Code: | | lpContext.Dr7 &= dwLevel | dwCondition | dwLength; |
|
|
| Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Tue Nov 02, 2010 5:33 pm Post subject: |
|
|
I don't know if its the solution to your problem, but you have an error with GetThreadContext: MSDN
| MSDN wrote: | | The value of the ContextFlags member of this structure specifies which portions of a thread's context are retrieved. |
The following line should also be placed BEFORE GetThreadContext. It will tell GetThreadContext what info it should retrieve.
| Code: | | lpContext.ContextFlags = CONTEXT_DEBUG_REGISTERS; |
EDIT: Oh you solved it.
|
|
| Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25848 Location: The netherlands
|
Posted: Fri Nov 05, 2010 4:46 pm Post subject: |
|
|
RF flag will only work in windows vista and windows 7
the RF flag is 'sanitized' by XP before returning
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|