Write Debugger Friendly Applications

One of the challenges for debugging when developing distributed system is that, you may need to debug some processes that are created by some other processes(for example Deamon on Data/Compute Node).

How to get debugger hooked in at the very first place when the process is spawned by those Deamon processes?
- The solution is to write "Debug Friendly" applications.

In order to get the process hooked in by Debugger at the starting up point, you should enable:
1. Stop and Wait the debugger to start and kick in, at the process entry point.
2. Some flag/switch mechanism to contorl whether your process should do step 1

Here is one way to realize these visions:
1. Use environment variable to control whether the debuggee should stop/wait debugger
2. Use IsDebuggerPresent() to determine whether your process is attached by a debugger
3. Use DebugBreak() to get Debugger kicked into the Debuggee.

    // if MY_DEBUG_BREAK is defined, break into the debugger
    SomeStrUtil strDebugBreak;
    if (strDebugBreak.GetEnvVar("MY_DEBUG_BREAK ") == S_OK && strDebugBreak.GetLength() != 0) {
        printf("Process-[%d] is waiting for debugger ...\n", GetCurrentProcessId()</a>);
        while (!IsDebuggerPresent()) {

If the upper code is put at the entry point (main/wmain) of your code, and you set the proper environment variable(here its MY_DEBUG_BREAK), it will wait the Debugger to attach to it. If it's attached, it will kick the Debugger into its code logic.

You can now break the debuggee, set break point and start your debugging journey.

No comments: