在 gdb 中使用函数调用堆栈进行导航

Navigate using function call stack in gdb

在 Visual Studio 中,如果您单击调用堆栈中的条目,将打开编辑器并显示该函数的源代码。 gdb 中可能有类似的东西吗?我在 gdb 中使用 tui(文本用户界面)。是否可以让 tui 在回溯中显示给定条目的源代码?

如果没有,那么你如何利用回溯中的信息?您是否手动打开文件并导航到正确的行?

当您在断点处以 gdb(在任何模式下)停止并且可以使用 backtracewhere 命令查看回溯时,请使用 updown 命令专注于回溯的不同功能(框架)。

您可以使用 up 2 向上移动两帧。 list 命令将显示当前帧周围的源代码行。

我认为,tui 会在 up/down 命令后更改当前显示的 function/registers; tui 中不能有点击回溯(tui 中是否支持鼠标?)。仅记录了 tui 的 windows 是 https://sourceware.org/gdb/onlinedocs/gdb/TUI-Commands.html

source, assembly, and command windows.

在TUI中有改变当前帧的键,但在普通TUI模式下没有(https://sourceware.org/gdb/onlinedocs/gdb/TUI-Keys.html),所以你可以使用gdb的文本命令。

还有"TUI Single Key Mode",由Ctrl-x s激活,此模式下有up/down个命令:u/dw 获取回溯。该模式记录在 https://sourceware.org/gdb/onlinedocs/gdb/TUI-Single-Key-Mode.html#TUI-Single-Key-Mode:

25.3 TUI Single Key Mode

 w    where
 u    up
 d    down
 r    run
 s    step
 n    next
 c    continue
 f    finish
 q    exit the SingleKey mode.
 v    info locals

Other keys temporarily switch to the gdb command prompt. The key that was pressed is inserted in the editing buffer so that it is possible to type most gdb commands without interaction with the TUI SingleKey mode. Once the command is entered the TUI SingleKey mode is restored. The only way to permanently leave this mode is by typing q or C-x s.

您也可以尝试一些带有 GUI 的调试器(gnu ddd or KDbg), or any other gdb wrapper builtin in most Linux IDEs (list, wiki list:Eclipse、Netbeans、CLion、KDevelop、Code::Blocks、CodeLite 等)。它们都更现代,更便于调试。

要添加到 osgx 的答案中,您还可以使用例如frame 7 转到 backtrace 中标记为 #7 的帧,而不是仅使用 up/down.