1

I have a large source tree with a directory that has several files in it. I'd like gdb to break every time any of those functions are called, but don't want to have to specify every file. I've tried setting break /path/to/dir/:*, break /path/to/dir/*:*, rbreak /path/to/dir/.*:* but none of them catch any of the functions in that directory. How can I get gdb to do what I want?

William Everett
  • 751
  • 1
  • 8
  • 18
  • Related: [Using gdb stop the program when it is using any function from file X](http://stackoverflow.com/questions/475283/using-gdb-stop-the-program-when-it-is-using-any-function-from-file-x). – Mark Plotnick Apr 12 '15 at 16:22

2 Answers2

2

There seems to be no direct way to do it:

  • rbreak file:. does not seem to accept directories, only files. Also note that you would want a dot ., not asterisk *
  • there seems to be no way to loop over symbols in the Python API, see https://stackoverflow.com/a/30032690/895245

The best workaround I've found is to loop over the files with the Python API, and then call rbreak with those files:

import os

class RbreakDir(gdb.Command):
    def __init__(self):
        super().__init__(
            'rbreak-dir',
            gdb.COMMAND_BREAKPOINTS,
            gdb.COMPLETE_NONE,
            False
        )
    def invoke(self, arg, from_tty):
        for root, dirs, files in os.walk(arg):
            for basename in files:
                path = os.path.abspath(os.path.join(root, basename))
                gdb.execute('rbreak {}:.'.format(path), to_string=True)
RbreakDir()

Sample usage:

source a.py
rbreak-dir directory

This is ugly because of the gdb.execute call, but seems to work.

It is however too slow if you have a lot of files under the directory.

My test code is in my GitHub repo.

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

You could probably do this using the Python scripting that comes with modern gdb's. Two options: one is to list all the symbols and then if they contain the required directory create an instance of the Breakpoint class at the appropriate place to set the breakpoint. (Sorry, I can't recall off hand how to get a list of all the symbols, but I think you can do this.)

You haven't said why exactly you need to do this, but depending on your use-case an alternative may be to use reversible debugging - i.e. let it crash, and then step backwards. You can use gdb's inbuilt reversible debugging, or for radically improved performance, see UndoDB (http://undo-software.com/)

Greg Law
  • 79
  • 1
  • I could not find how to list all symbols in GDB's Python API, and Tromey suggests it is not possible: http://stackoverflow.com/a/30032690/895245 I would mention on the answer / profile that you co-founded Undo. I don't doubt it's more efficient. :-) – Ciro Santilli OurBigBook.com Jul 17 '15 at 20:12