Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gdb stop the program when it is using any function from file X

Tags:

and I would like to know if there is any way to stop a program when is using a function from a certain file. Ideally what I am looking for is something like:

GDB Stop when use a function from file foo.cpp 

The reason to do this is because I am debugging a code that is not mine and I do not know exactly what functions are been called and what functions are not. Is there a function in GDB to do what I am looking for, or any other recommended way to do something similar?.

Thanks

like image 588
Eduardo Avatar asked Jan 24 '09 01:01

Eduardo


People also ask

How do I stop a program in GDB?

To stop your program while it is running, type "(ctrl) + c" (hold down the ctrl key and press c). gdb will stop your program at whatever line it has just executed. From here you can examine variables and move through your program. To specify other places where gdb should stop, see the section on breakpoints below.

How do you exit a function in GDB?

You can cancel execution of a function call with the return command. If you give an expression argument, its value is used as the function's return value. When you use return , GDB discards the selected stack frame (and all frames within it).

How do you stop a breakpoint in GDB?

With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers.


1 Answers

Step 1: construct a list of all functions defined in foo.cpp
The simplest way I can think of (assuming you have binutils and GNU grep):

nm a.out | grep ' T ' | addr2line  -fe a.out |   grep -B1 'foo\.cpp' | grep -v 'foo\.cpp' > funclist 

Step 2: construct a GDB script which will set a break point on each of the above functions:

sed 's/^/break /' funclist > stop-in-foo.gdb 

[Obviously, steps 1 and 2 could be combined ;-]

Step 3: actually set the breakpoints:

gdb a.out (gdb) source stop-in-foo.gdb 

Looking at this answer, an even simpler (if you are on Fedora Linux) way to find out which foo.cpp functions are called:

ftrace -sym='foo.cpp#*' ./a.out 

Too bad ftrace man page says this isn't implemented yet.

like image 91
Employed Russian Avatar answered Oct 08 '22 00:10

Employed Russian