Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can my app arrange a gdb breakpoint or watch?

Tags:

c++

c

debugging

gdb

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).

like image 694
Larry Gritz Avatar asked Jan 22 '23 05:01

Larry Gritz


2 Answers

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)

like image 130
Michael Mrozek Avatar answered Jan 30 '23 06:01

Michael Mrozek


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.

like image 31
dicroce Avatar answered Jan 30 '23 07:01

dicroce