Perform certain actions when certain breakpoints in gdb

I am looking for a way to do some kind of action when a particular breakpoint hits gdb.

Basically I have memleak in my program. When malloc and free function get there, I need to go into the function (step) and collect some basic information like addr and size (basically print the values ​​there). After completing my program.

Do we have a good way to do this?

+37
c gdb
Jun 29 2018-11-11T00:
source share
3 answers

For example, here you can use break commands to print the value of x when entering foo whenever x is positive.

break foo if x>0 commands silent printf "x is %d\n",x cont end 

If the first command specified in the command list is silent , the usual stop message at the breakpoint is not printed. This may be desirable for control points that should print a specific message and then continue. If none of the remaining commands prints anything, you see no signs that the breakpoint has been reached. silence only makes sense at the beginning of the breakpoint command list.

One application for breakpoint commands is to compensate for one error, so you can check it for another. Put a breakpoint immediately after the erroneous line of code, give it a condition to detect a case in which something is erroneous, and give it commands to assign the correct values ​​to any variables that need them. Finish with the continue command so that your program does not stop, and start with a silent command so that no output is produced. Here is an example:

 break 403 commands silent set x = y + 4 cont end 
+44
Jun 29 '11 at 8:01
source share

To clarify Fredrick’s answer, commands (or just command , it seems) automatically knows that you just set a breakpoint. That is, what Fredrik shows is not a multi-line break command, these are two separate commands: break and commands . It looks like this:

 (gdb) break 989 Breakpoint 23 at 0x7fffe2761dac: file foo.cpp, line 989. (gdb) command Type commands for breakpoint(s) 23, one per line. End with a line saying just "end". >silent >print result >end (gdb) c Continuing. $79 = {elems = {0, 0}} (gdb) 
+37
Nov 01 '13 at 15:29
source share

dprintf (dynamic printf )

https://sourceware.org/gdb/onlinedocs/gdb/Dynamic-Printf.html

This is the most convenient solution for a specific printing case:

 dprintf <line>, "%u\n", variable 

It can also be faster than commands , because it can compile and inject code instead of giving GDB control to interpret arbitrary command lines. TODO I don't know if this is really done. dprintf vs commands : What is the difference between dprintf and break + commands + continue?

Detailed example:

main.c

 #include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> int main(void) { uint32_t i; uint32_t r = 0; for (i = 0; i < 10; ++i) { r += i*i + 13*r*i + 17; /* LINE 10. */ } printf("%" PRIu32 "\n", r); return EXIT_SUCCESS; } 

Then:

 gcc -ggdb3 -O0 -std=c99 -o main main.c gdb -batch -nh -q -ex 'dprintf 10, "%u %u\n", i, r' -ex 'run' ./main 

Output:

 Dprintf 1 at 0x400545: file main.c, line 10. 0 0 1 17 2 256 3 6933 4 277346 5 14699371 6 970158528 7 3628079733 8 3070853710 9 317092431 3057168588 [Inferior 1 (process 14305) exited normally] 

Tested on Ubuntu 14.04.

+2
Mar 08 '17 at 14:12
source share



All Articles