Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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).

like image 621
Chad Avatar asked Dec 07 '15 17:12

Chad


People also ask

How do I show breakpoints in gdb?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.

How do you use the breakpoint command?

You can use breakpoint commands to start your program up again. Simply use the continue command, or step , or any other command that resumes execution. Any other commands in the command list, after a command that resumes execution, are ignored.

What does the LIST command do in gdb?

To print lines from a source file, use the list command (abbreviated l ). By default, ten lines are printed. There are several ways to specify what part of the file you want to print; see Specify Location, for the full list.

What command is used in gdb to set a breakpoint?

The rbreak command can be used to set breakpoints in all the functions in a program, like this: (gdb) rbreak . Print a table of all breakpoints, watchpoints, and catchpoints set and not deleted, with the following columns for each breakpoint: Breakpoint Numbers.

What is the gdb command that will show the address of the next instruction to execute when the current function reaches its one return statement?

It is often useful to do ' display/i $pc ' when stepping by machine instructions. This makes GDB automatically display the next instruction to be executed, each time your program stops.


1 Answers

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).

like image 105
Tom Tromey Avatar answered Sep 26 '22 00:09

Tom Tromey