tags:

views:

565

answers:

5

Hello I am using GDB, 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

+1  A: 

gdb breakpoints have a couple of syntax... See here.

It won't break on any function in the file though....

Edit: You could do something stupid like making all function call a dummy function void foo(void), and breakpoint inside. At least you would break inside the file, and should be trivial to find which function in side of file X is being called.

Calyth
It is not a bad idea the foo function, the thing is that I have many methods and files, the only option that maybe I can use is rbreak regex. Thanks
Eduardo
+10  A: 

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.

Employed Russian
I was going to say to do this, but +1 for actually doing it. =)
A. Rex
+2  A: 

Employed Russian's answer looks very good, but since you say:

I do not know exactly what functions are been called and what functions are not.

Would a report of which functions are hit, generated by a code coverage tool such as gcov or something involving Valgrind be a good solution to your problem?

therefromhere
A: 

you an use command

break foo.cpp:function-name //this should be the name of your function

kal
+2  A: 

rbreak regex

Set breakpoints on all functions matching the regular expression regex. This command sets an unconditional breakpoint on all matches, printing a list of all breakpoints it set. Once these breakpoints are set, they are treated just like the breakpoints set with the break command. You can delete them, disable them, or make them conditional the same way as any other breakpoint.

The syntax of the regular expression is the standard one used with tools like `grep'. Note that this is different from the syntax used by shells, so for instance foo* matches all functions that include an fo followed by zero or more os. There is an implicit .* leading and trailing the regular expression you supply, so to match only functions that begin with foo, use ^foo.

When debugging C++ programs, rbreak is useful for setting breakpoints on overloaded functions that are not members of any special classes.

This seems to be better than the accepted answer to me :)
kizzx2
This helped me enormously!Although I guess it can't be the accepted answer since it matches function names and not the filename of the file it resides in.
kigurai