Linux gdb - 列出当前函数的来源而不输入其名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12824251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:34:07  来源:igfitidea点击:

gdb - list source of current function without typing its name

c++clinuxgdb

提问by s5s

In GDB, the command:

在 GDB 中,命令:

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?

是否有一个命令可以列出您当前所在函数的所有源代码,而无需您手动键入函数名称?

采纳答案by dpc.pw

(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 *ADDRESSis what is interesting.

*ADDRESS是什么是有趣的。

On x86/x64 current pointer is in ripregister so:

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

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

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

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

回答by SparKot

gdbtui can be useful to view the source during debugging.

gdbtui 可用于在调试期间查看源代码。

回答by Dave Hite

When you are stopped in a function type btfor 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.

当您在函数中停止时,键入bt进行回溯。回溯将列出当前堆栈。顶部的元素#0 通常是您感兴趣的函数,并且还列出了源文件和行号。

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#SEC42for more info.

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

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

此外,当您设置断点时,您可以指定每次点击该断点时将运行的命令。见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

回答by Gregg

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

'frame' 命令显示函数名称和当前行位置,并将列表的当前行设置为当前可执行行。

set listsize 17
frame
list

lists the 8 lines surrounding the current line.

列出当前行周围的 8 行。