gdb - list source of current function without typing its name

31,042

Solution 1

(gdb) help list
List specified function or line.
With no argument, lists ten more lines after or around previous listing.
"list -" lists the ten lines before a previous ten-line listing.
One argument specifies a line, and ten lines are listed around that line.
Two arguments with comma between specify starting and ending lines to list.
Lines can be specified in these ways:
  LINENUM, to list around that line in current file,
  FILE:LINENUM, to list around that line in that file,
  FUNCTION, to list around beginning of that function,
  FILE:FUNCTION, to distinguish among like-named static functions.
  *ADDRESS, to list around the line containing that address.
With two args if one is empty it stands for ten lines away from the other arg.

The *ADDRESS is what is interesting.

On x86/x64 current pointer is in rip register so:

(gdb) list *$pc
0x7ffff7b018a0 is at ../sysdeps/unix/syscall-template.S:82.
77  in ../sysdeps/unix/syscall-template.S

The example is from cat command as I don't have anything with debug info at hand.

Solution 2

When you are stopped in a function type bt for backtrace. Backtrace will list the current stack. The element at the top, #0, is usually the function you are interested in and the source file and line number is listed also.

For example:

(gdb) bt
#0  myClass::EntityTypeStruct::readAttributes (this=0x7fffd00066e0, buf=0x7fffd0006020 "", len=48)
    at /team/project/src/EntityTypeStruct.cc:55
#1  0x000000000044ca86 in workerThread (ts=0x7fffea71dcc0)
    at /team/project/src/threads/workerThread.cc:219
#2  0x00007ffff775e9d1 in start_thread () from /lib64/libpthread.so.0
#3  0x00007ffff6c07b5d in clone () from /lib64/libc.so.6

See http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_7.html#SEC42 for more info.

Also, when you set a breakpoint you can specify commands that will run everytime you hit that breakpoint. See http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_6.html#SEC35

So, if you know how many lines are in your function you could set a command to list all source lines of the function:

(gdb) break myClass::EntityTypeStruct::readAttributes
Breakpoint 1 at 0x61ec3b: file /team/project/src/EntityTypeStruct.cc, line 38.
(gdb) commands 1
list 38,104
end

Solution 3

gdbtui can be useful to view the source during debugging.

Solution 4

The 'frame' command shows the function name and the current line location and sets the current line for list to the current executable line.

set listsize 17
frame
list

lists the 8 lines surrounding the current line.

Share:
31,042
s5s
Author by

s5s

Updated on December 01, 2020

Comments

  • s5s
    s5s over 3 years

    In GDB, the command:

    list function
    

    will list all the source for the function.

    Is there a command that will list all of the source of the function you are currently in, without requiring you to manually type the function name?

    • s5s
      s5s over 11 years
      it prints a number of lines but not the whole function.
    • Ben
      Ben over 11 years
      You can keep hitting enter after list function to repeat the previous command and continue printing out the function 10 lines at a time.
    • Basile Starynkevitch
      Basile Starynkevitch over 11 years
      And you can also run gdb inside Emacs with M-x gdb; I find this very useful.
    • Ciro Santilli OurBigBook.com
      Ciro Santilli OurBigBook.com almost 9 years
      By "implicitly" do you mean without typing the function name directly?
    • s5s
      s5s almost 9 years
      @CiroSantilli六四事件法轮功纳米比亚胡海峰 Yes - since a C program is a collection of functions you are always in a function, that's guaranteed. It would be useful to have a shortcut which will list the current function implicitly (i.e. without specifying the name of the curent function).
    • Ciro Santilli OurBigBook.com
      Ciro Santilli OurBigBook.com almost 9 years
      Thanks, so this is the question I wanted. Edited to make it clearer.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com almost 9 years
    This id not work for me on GDB 7.7: it printed 10 lines around the current line, just like list, not the entire function.
  • Geof Sawaya
    Geof Sawaya over 8 years
    A little late to the party, but clearly the best answer.
  • Ruslan
    Ruslan almost 6 years
    Why does rip being the program counter make any difference if you use $pc, the GDB's generic name for program counter? And, actually, on x86 (32-bit) it's not rip – there's even no such register. It's instead eip.
  • Timmmm
    Timmmm over 2 years
    How? It doesn't even answer the question.
  • Timmmm
    Timmmm over 2 years
    Yeah this does not behave any differently to just list.