gdb - 列出当前函数的源代码而不键入其名称

s5s*_*s5s 17 c c++ linux gdb

在GDB中,命令:

list function
Run Code Online (Sandbox Code Playgroud)

将列出该函数的所有源代码.

是否有一个命令会列出您当前所在功能的所有来源,而无需手动输入功能名称?

dpc*_*.pw 13

(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.
Run Code Online (Sandbox Code Playgroud)

*ADDRESS很有趣.

在x86/x64上,当前指针位于rip寄存器中,因此:

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

该示例来自cat命令,因为我手头没有调试信息.

  • 这个ID在GDB 7.7上对我不起作用:它在当前行周围打印了"10"行,就像`list`,而不是整个函数. (2认同)

Dav*_*ite 5

当您停止在函数中时,请输入bt作为回溯。Backtrace将列出当前堆栈。顶部的元素#0通常是您感兴趣的功能,并且还会列出源文件和行号。

例如:

(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
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_7.html#SEC42

另外,设置断点时,您可以指定将在每次击中该断点时运行的命令。参见http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_6.html#SEC35

因此,如果您知道函数中有多少行,则可以设置命令以列出函数的所有源代码行:

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