开发者

Can my app arrange a gdb breakpoint or watch?

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 locati开发者_开发知识库on 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).


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)


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.


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.


The commonly used approach is to use a dummy function with non-obvious name. Then, you can augment your .gdbinit or use whatever other technique to always break on that symbol name.

Trivial dummy function:

void my_dummy_breakpoint_loc(void) {}

Code under test (can be an assert-like macro):

if (rare_condition)
    my_dummy_breakpoint_loc();

gdb session (obvious, eh?):

b my_dummy_breakpoint_loc

It is important to make sure that "my_dummy_breakpoint_loc" is not optimized away by compiler for this technique to work.

In the fanciest of cases, the actual assembler instruction that calls my_dummy_breakpoint_loc can be replaced by "nops" and enabled on site by site basis by a bit of code self-modification in run-time. This technique is used by Linux kernel development instrumentation, to name a one example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜