22

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

Svante
  • 50,694
  • 11
  • 78
  • 122
Eduardo
  • 19,928
  • 23
  • 65
  • 73

6 Answers6

22

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.

Community
  • 1
  • 1
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
12

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.

Mayank Jain
  • 405
  • 9
  • 19
  • 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. – Hannes Ovrén Aug 12 '10 at 08:02
8

rbreak foo.cpp:.

Here . matches anything, and so breaks on all functions of file foo.cpp.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
2

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?

John Carter
  • 53,924
  • 26
  • 111
  • 144
1

You can use command:

break foo.cpp:function-name
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
kal
  • 28,545
  • 49
  • 129
  • 149
0

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
  • 1,673
  • 3
  • 16
  • 26
  • 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 Jan 24 '09 at 01:37