tags:

views:

113

answers:

3

Is there a way for my code to be instrumented to insert a break point or watch on a memory location that will be honored by gdb? (And presumably have no effect when gdb is not attached.)

I know how to do such things as gdb commands within the gdb session, but for certain types of debugging it would be really handy to do it "programmatically", if you know what I mean -- for example, the bug only happens with a particular circumstance, not any of the first 11,024 times the crashing routine is called, or the first 43,028,503 times that memory location is modified, so setting a simple break point on the routine or watch point on the variable is not helpful -- it's all false positives.

I'm concerned mostly about Linux, but curious about if similar solutions exist for OS X (or Windows, though obviously not with gdb).

+1  A: 

For breakpoints, on x86 you can break at any location with

asm("int3");

Unfortunately, I don't know how to detect if you're running inside gdb (doing that outside a debugger will kill your program with a SIGTRAP signal)

Michael Mrozek
I don't mind doing it custom just for my debugging task, then ripping it out later. But what about establishing a watch point?
Larry Gritz
+3  A: 

GDB supports a scripting language that can help in situations like this. For example, you can trigger a bit of custom script on a breakpoint that (for example) may decided to "continue" because some condition hasn't been met.

dicroce
A: 

Not directly related to your question, but may be helpful. Have you looked at backtrace and backtrace_symbol calls in execinfo.h

http://linux.die.net/man/3/backtrace

This can help you log a backtrace whenever your condition is met. It isn't gdb, so you can't break and step through your program, but may be useful as a quick diagnostic.

Jasmeet