Chad
Chad

Reputation: 1980

Is there a quick way to display the source code at a breakpoint in gdb?

I've set a breakpoint in gdb, and I'd like to see the exact line of source the breakpoint is on, just to confirm it's correct -- is there a quick way to do this?

The "info b" command gives me information about the breakpoints, but it doesn't display source:

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000006c3ba4 in MyClass::foo(bar*)
                                                   at /home/user1/src/MyClass.cpp:1021

I can type "list MyClass.cpp:1021" to see the lines around this breakpoint, but I'm wondering if there's a shorter way. Googling and reading the gdb manual didn't turn up anything.

I know that if I'm executing the program and have hit the breakpoint, I can just type "list", but I'm asking specifically about the case where I am not at the breakpoint (the program may not even be running).

Upvotes: 12

Views: 18945

Answers (3)

Spacen Jasset
Spacen Jasset

Reputation: 976

* The correct answer is below *

This is a 10 year old Q&A so it's not likely this will become the correct answer, but it should. The other responses suggest work-arounds, not a direct solution.

There is a way as follows, (at least in GDB 13.1+) from reading the following carefully:

help info break 

First, display the breakpoint in question:

info break <breakpoint-number>

List the address of expression $_ (last displayed breakpoint):

list *$_

In short:

i b <bp-number>
l *$_

Combining this into a User Command, put the following in your ~/.gdbinit, restart gdb, and then you can run the command listbr 2 to see the listing of breakpoint #2.

define listbr
   info br $arg0
   list *$_
end
document listbr
    listbr <n> list code at breakpoint <n>
end

Upvotes: 2

Andreas Spindler
Andreas Spindler

Reputation: 8150

I think the closest one can get to this is by turning the history on (set history save on) and then press CTRL-R to do a reverse search for the former list command.

More specifically, change your workflow when setting a breakpoint. After each command like b main GDB shows the source file like path/to/main.cpp, line 12. Immediately use this information in a quick list main.cpp:12. To show this location later press CTRL-R and type "main".

https://sourceware.org/gdb/onlinedocs/gdb/Command-History.html

Upvotes: 0

Tom Tromey
Tom Tromey

Reputation: 22549

You can use the list command to show sources. list takes a "linespec", which is gdb terminology for the kinds of arguments accepted by break. So, you can either pass it whatever argument you used to make the breakpoint in the first place (e.g., list function) or you can pass it the file and line shown by info b (e.g., list mysource.c:75).

Upvotes: 14

Related Questions