3
votes

I am trying to debug a C++ program using GDB. I have set 15 breakpoints. Most of the breakpoints are in different files. After the first 5 breakpoints, it became difficult to remember what line of code any given breakpoint refers to.

I struggle quite a bit simply trying to recall what a given breakpoint refers to. I find this quite distracting. I was wondering if there is a way to tell gdb to display code around a certain breakpoint.

Something like this - $(gdb) code 3 shows 30 lines of code around breakpoint 3. Is this possible today. Could you please show me how?

I run gdb in tui mode, and I also keep emacs open to edit my source files.

2

2 Answers

1
votes

I don't think you can do it exactly like this in gdb as such, but it can be scripted in gdb python.

This crude script should help:

import gdb

class Listbreak (gdb.Command):
        """ listbreak n Lists code around breakpoint """

        def __init__ (self):
                super(Listbreak, self).__init__ ("listbreak", gdb.COMMAND_DATA)

        def invoke (self, arg, from_tty):
                printed = 0
                for bp in gdb.breakpoints():
                        if bp.number == int(arg[0]):
                                printed = 1
                                print ("Code around breakpoint " + arg[0] + " (" + bp.location + "):")
                                gdb.execute("list " + bp.location)
                if printed == 0:
                        print ("No such breakpoint")
Listbreak()

Copy this to listbreak.py, source it in gdb (source listbreak.py), then use it like this:

listbreak 2

4
votes

You can use gdb within emacs.

In emacs, type M-x gdb, after entering the name of the executable, type M-x gdb-many-windows. It brings up an IDE-like interface, with access to debugger, locals, source, input/output, stack frame and breakpoints.

You can find a reference and snapshot here.