Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i want to set hardware breakpoint to address in the memory without using CreateProcess function

What I have tried:

ps : this code work fine
C#
bool dwResult;
	STARTUPINFO sinfo;
	DEBUG_EVENT DebugEvent;
	


	memset(&sinfo, 0, sizeof(sinfo));
	dwResult = CreateProcess("MainWindow.exe", 0, 0, 0, false, DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS, 0, 0, &sinfo, &pinfo);
	if (dwResult)
	{
		while (dwResult)
		{
			WaitForDebugEvent(&DebugEvent, INFINITE);
			DWORD EventCode = DebugEvent.dwDebugEventCode;
			DWORD ExceptionCode = DebugEvent.u.Exception.ExceptionRecord.ExceptionCode;
			switch (EventCode)
			{
			case EXIT_PROCESS_DEBUG_EVENT:
				{
					dwResult = false;
					continue;
				};
			case CREATE_PROCESS_DEBUG_EVENT:
				{
				HANDLE Thread = OpenThread(THREAD_ALL_ACCESS, false, DebugEvent.dwThreadId);
				SetHWBP(Thread, 0x00E5254C, HWBP_EXECUTE, HWBP_BYTE, 0);	// to sniff serial
						
					
					CloseHandle(Thread);
				};
				
			case EXCEPTION_DEBUG_EVENT:
				{
					if (EXCEPTION_SINGLE_STEP == ExceptionCode)
					{
						HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, false, DebugEvent.dwThreadId);
						if (hThread)
						{
							CONTEXT context = {CONTEXT_ALL|CONTEXT_DEBUG_REGISTERS|CONTEXT_CONTROL};
							if (GetThreadContext(hThread, &context))
							{
								if ((context.Dr6 & (1 << 0)))	
								{
									DoSomething(&context); //execute after doing the HWBP
									RemoveHWBP(hThread, 0);
								}
								
							};
							CloseHandle(hThread);
						};
					};
				};
			};
			dwResult = ContinueDebugEvent(DebugEvent.dwProcessId, DebugEvent.dwThreadId, DBG_CONTINUE);
		};
		CloseHandle(pinfo.hProcess);
		CloseHandle(pinfo.hThread);
	};
	//system("pause");
	return false;
Posted
Updated 12-Dec-16 9:05am
Comments
Richard MacCutchan 12-Dec-16 11:03am    
So what is the question?
Member 10852608 12-Dec-16 11:49am    
i want to set hardware breakpoint to address in process without using CreateProcess function cuz i don't want to run the process from my debugger , i just want to get handle to it and set HWBP to address
Richard MacCutchan 12-Dec-16 12:08pm    
You have a comment above that states "this code work fine". However, if the address you reference is outside your application's address space it will fail with an illegal address exception.

Also, I cannot find any references to the SetHWBP function; where is it defined?
Member 10852608 12-Dec-16 12:13pm    
don't worry about the address it's static
sorry here's the whole source

http://rgho.st/74Pp4SdMd

1 solution

Quote:
how to write simple debugger
Writing a debugger is anything but simple.
Quote:
i want to set hardware breakpoint to address in the memory
Modern OS are pretty complicated and the handling of memory is pretty complex.
I recommend to use existing debuggers, the one of your IDE is a good start.

Note than debugging a serial communication may imply a special tooling to ensure that the debugging itself do not alter the communication.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900