views:

120

answers:

2
+1  Q: 

Under a debugger

How do I detect in my program that it's being run under a debugger? I am aware that this seems to indicate that I try to do something I shouldn't, and that is too messy. I think it is an interesting question tho. Specifically, is there a way to do it in a POSIX environment? For example using sigaction (2) to detect that some handler is installed? Even a worse thought; is there some inline assembly code I could use on an x86 architecture?

As we discuss it -- would it eventually be possible to launch a debugger, such as gdb (1), and break at the place where you perform this possible hack. Thanks for any dirty one-liners or unlikely references to standards related to this.

+3  A: 

Does this article help?

It suggests, amongst other things:

  • file descriptors leaking from the parent process
  • environment variables ($_)
  • process state (getsid(), etc).

Note that most (if not all) of these rely on the debugger spawning the process that's being debugged. They're not so effective if the debugger is attached to a process that's already running.

Alnitak
seems that the heuristic (getsid()!=getppid() there's no stable way to do this. The best I found till now is to check $_ with an ending in "/gdb". Thanks!
hept
+1  A: 

There is no reliable way to detect that you are running under a debugger. That's because a debugger may use any number of methods to actually debug your code, some of which will almost certainly not be caught by your method.

My question is, why would you care? Unless you're trying to hide something :-)

paxdiablo
I'm not hiding anything. A few times I've thought it would be nice to set breakpoints automatically in my program in the case that theres a debugger on me. But what I've much more wanted at difficult times is to spawn a gdb (1) from my daemon attaching it to my daemon and to some local TCP port as its stdio. I would await on that port to start my session. Say there appears to be a broken invariant in the process that occurs only in a production network.
hept
I have a vague recollection that xdb (another debugger I used a while ago) was scriptable (load program, set breakpoint, run, etc). That may be one approach to automatically do it albeit by the debugger rather than your program.
paxdiablo