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
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With