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

前端 未结 6 1926
广开言路
广开言路 2020-12-03 04:57

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:



        
相关标签:
6条回答
  • 2020-12-03 05:37

    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.

    0 讨论(0)
  • 2020-12-03 05:40

    rbreak foo.cpp:.

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

    0 讨论(0)
  • 2020-12-03 05:44

    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?

    0 讨论(0)
  • 2020-12-03 05:47

    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.

    0 讨论(0)
  • 2020-12-03 05:49

    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.

    0 讨论(0)
  • 2020-12-03 05:50

    You can use command:

    break foo.cpp:function-name
    
    0 讨论(0)
提交回复
热议问题