Can my application place a gdb breakpoint or watch?

Is there a way my code can be used to insert a breakpoint or view in a memory location that gdb will execute? (And presumably has no effect when gdb is not connected.)

I know how to do things like gdb commands in a gdb session, but for certain types of debugging it would be very convenient to do this “programmatically” if you know what I mean, for example, only an error occurs with any particular circumstance, and not with the first 11,024 times when the failure procedure is called, or the first 43 0228 503 times when this memory location changes, so setting a simple breakpoint in a routine or observation point on a variable does not help - all are false positives.

I'm mostly interested in Linux, but I'm wondering if similar solutions exist for OS X (or Windows, although obviously not with gdb).

+4
source share
4 answers

For x86 breakpoints you can break anywhere with

asm("int3"); 

Unfortunately, I don’t know how to determine if it works inside gdb (doing this outside the debugger will cause your program to be bitten with a SIGTRAP signal)

+3
source

GDB supports a scripting language that can help in such situations. For example, you can run a custom script bit at a breakpoint, which (for example) may decide to continue because some condition has not been met.

+2
source

Not directly related to your question, but may be helpful. You looked at the callbacks and callbacks_symbol in execinfo.h

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

This can help you register backtracking whenever your condition is met. It is not gdb, so you cannot break and execute your program, but it can be useful as a quick diagnostic.

+2
source

A commonly used approach is to use a dummy function with a non-obvious name. Then you can increase your .gdbinit or use any other technique to always break that name.

Trivial dummy function:

 void my_dummy_breakpoint_loc(void) {} 

Checked code (may be an assert type macro):

 if (rare_condition) my_dummy_breakpoint_loc(); 

gdb session (obviously eh?):

 b my_dummy_breakpoint_loc 

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

In the most severe cases, the actual assembler instruction that calls my_dummy_breakpoint_loc can be replaced with "nops" and included on the site by site using a slight modification of the code at runtime. This method is used by Linux kernel development tools to name one example.

+2
source

Source: https://habr.com/ru/post/1307294/


All Articles