User:Zippy1981/Projects/Debugging Scratchpad

From Open Watcom

< User:Zippy1981 | ProjectsRevision as of 13:42, 31 October 2006; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

Debugging technique and methodology is a topic of great interest to me. Its also a poorly understood and documented area of programming. I am trying to rectify the documentation part. I will keep notes here that will eventualy evolve into artcles in the main section of the wiki.


Detaching from a running process

It is possible for a debugger to detach a from a running process in Windows XP and later (XP, 2k3, Vista). The API call for doing this is DebugActiveProcessStop() [1]. wdw does not currently implement this.

Interacting with the Debugger (Win32)

Sometimes you want your program to interact with the debugger. Example, if you are debugging an application, you might want your application to tell the debugger to break. There are two ways of doing this. The debugging hooks for the Windows API provide methods for this. In addition there is a Watcom specific way of dealing with the debugger.

Windows API

There is an MSDN article[2] on the subject. Basic technique is illustrated in the code below. Please note that the code must be compiled as a Win32 Console application in order to work being it uses printf().


#include <stdio.h>
#include <windows.h>

int main (int argc, char *argv[]) {
	printf ("DebugTest.exe\n\tSubroutine to demonstrate Win32 Debugging techniques\n");
	if (IsDebuggerPresent()) {
		OutputDebugString("This message was sent to the debugger by DebugTest.exe");
		printf ("\tThe debugger is present.\n");

	} else {
		printf ("\tThe debugger is not present.\n");
	return 0x0000;

OutputDebugString() messages are meant to be displayed by the debugger. The option for seeing them in wdw is hidden in the most unintutive of places on the menu under Data | Log. You can also set the debuger to breat on these messages with the menu option Break | On Debug message.

Watcom Specific way

This is an undocumented method specific to OpenWatcom. There are a few implementations of this header. The most polished one I've found is in the Opwn Watcom Source in the file bld/watcom/h/enterdb.h. Below is a copy of the files contents.

 * FILE: wdwipc.h (Watcom Debugger Interactions)
 *                          Open Watcom Project
 *    This file is automatically generated. Do not edit directly.
 * =========================================================================
 * Description: This header exposes hooks into the watcom debugger.

#if defined(__WATCOMC__)

/* Variable used to determine if the debugger is present */

extern char volatile __WD_Present;

extern void Int3( void );
#pragma aux Int3 = "int 3"

/* Macro to enter the debugger and pass a message */

void Int3WithSignature( const char __far * );
#pragma aux Int3WithSignature parm caller [] = \
    "int 3" \
    "jmp short L1" \
    'W' 'V' 'I' 'D' 'E' 'O' \

#define EnterDebugger() \
{ \
    if( __WD_Present ) Init3(); \

#define EnterDebuggerWithMessage(msg) \
{ \
    if( __WD_Present ) Int3WithSignature(msg); \

#endif /* defined(__WATCOMC__) */

#endif /* _WDWIPC_H_INCLUDED */

Examples Where I derived this code from

  • snap code from[3]
  • Sybase documentation[4]

Third party tools

Some powerful task manager alternatives :


  1. DebugActiveProcessStop()
  2. MSDN article on debugging
Personal tools