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

前端 未结 6 1927
广开言路
广开言路 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: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.

提交回复
热议问题