Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect whether there is a debugger attached in c++?

I have created a macro,

#define DEBUG_BREAK(a) if (a) __asm int 3;

But the problem is if there is no debugger attached, the program would run incorrectly.

So I need to know whether there is a debugger attached. If there's a debugger, the app should call int 3. Otherwise, it should not.

How could i do this?

like image 690
Jichao Avatar asked May 22 '13 08:05

Jichao


People also ask

How do I know if debugger is attached?

Kernel-mode code can determine the status of kernel debugging by using the following variables and routines: The KD_DEBUGGER_ENABLED global kernel variable indicates whether kernel debugging is enabled. The KD_DEBUGGER_NOT_PRESENT global kernel variable indicates whether a kernel debugger is currently attached.

How do I find debugger?

Navigate code in the debugger using step commands To start your app with the debugger attached, press F11 (Debug > Step Into). F11 is the Step Into command and advances the app execution one statement at a time. When you start the app with F11, the debugger breaks on the first statement that gets executed.

How do I find GDB?

If you just want to know whether the application is running under GDB for debugging purposes, the simplest solution on Linux is to readlink("/proc/<ppid>/exe") , and search the result for "gdb" .

How do debuggers attach?

The user tells the debugger which process to attach to, either by name or by process ID. If it is a name then the debugger will look up the process ID, and initiate the debug session via a system call; under Windows this would be DebugActiveProcess.


1 Answers

For what you want to do, it would be better if you'd use the proper exposed kernel32.dll function DebugBreak. Basically along the lines of

#define DEBUG_BREAK(a) if(a) if (IsDebuggerPresent()) DebugBreak()

or instead of doing the __asm int 3 routine, use the VC-supplied intrinsic __debugbreak, i.e.:

#define DEBUG_BREAK(a) if(a) if (IsDebuggerPresent()) __debugbreak()

The latter makes a difference (compared to int 3) when compiling with /clr as pointed out in the documentation. Of course the intrinsic hasn't always been there, so depends on your VS/VC version (which you don't state).

In both cases you need at least windows.h included for IsDebuggerPresent().


However, that's the exact reason you would have a debug and release build and build those conditionally. Keep in mind that the optimizer can garble the results in the debugger despite your efforts to carefully place the breakpoints in code. The reason is simply that some lines in your source code won't be represented anymore or will have changed in a deterministic manner. So using one configuration for both doesn't make a lot of sense. So what I'm saying is to use something along the lines of:

#ifdef _DEBUG
#   define DEBUG_BREAK(a) if(a) __debugbreak()
#else
#   define DEBUG_BREAK(a) do {} while(0)
#endif
like image 122
0xC0000022L Avatar answered Sep 24 '22 19:09

0xC0000022L