Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Write Your Own Debugger to Handle Breakpoints

, 17 May 2011 CPOL
Basic debugger, Breakpoints, OutputDebugString
Debugger.zip
Debugger
Debugger.vcxproj.filters
Debugger.vcxproj.user
Debuggee
Debuggee.vcxproj.filters
Debuggee.vcxproj.user
// Debugger.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
PROCESS_INFORMATION  pi={};
void EnterDebugLoop()
{
	DEBUG_EVENT de;
	while(1)
	{
		WaitForDebugEvent(&de, INFINITE); 

		switch (de.dwDebugEventCode)
		{
		case EXCEPTION_DEBUG_EVENT: 
			switch(de.u.Exception.ExceptionRecord.ExceptionCode)
			{
			case EXCEPTION_BREAKPOINT: 
				MessageBoxA(0,"Found break point","",0);
				break;
			}
			break;
		case OUTPUT_DEBUG_STRING_EVENT:
			{
				char a[100];
				ReadProcessMemory(pi.hProcess,de.u.DebugString.lpDebugStringData,a,de.u.DebugString.nDebugStringLength,NULL);
				printf("output from debug string is: %s",a);
			}
			break;
		}

		ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	STARTUPINFOA si={}; 
	::CreateProcessA("..\\Debug\\Debuggee.exe","",0,0,0,CREATE_NEW_CONSOLE, 0,0,&si,&pi);

	/*BYTE p[]={0xcc}; //0xcc=int 3
	DWORD d=0;
	::WriteProcessMemory(pi.hProcess,(void*)0x00F21460,p,sizeof(p),&d);*/

	BOOL bo=DebugActiveProcess(pi.dwProcessId);
	if(bo==0)
		printf("Could not attach debugger, GetLastError: %u \n",GetLastError());

	EnterDebugLoop();
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Asif Bahrainwala
Instructor / Trainer
India India
Hi,
I have been working with computers since my eight grade, programming the ZX Spectrum. I have always had an interest in assembly language and computer theory (and is still the reason for taking tons of online courses), actively code using C/C++ on Windows (using VS) and Linux (using QT).
 
I also provide training on data structures, algorithms, parallel patterns library , Graphics (DX11), GPGPUs (DX11-CS,AMP) and programming for performance on x86.
Feel free to call me at 0091-9823018914 (UTC +5:30)
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 May 2011
Article Copyright 2011 by Asif Bahrainwala
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid