Underappreciated builtin: Grand Unified Debugger
Underappreciated builtin: Grand Unified Debugger
被低估的内置功能:Grand Unified Debugger (GUD)
Or as I would like to call it, GLORIOUS Unified Debugger. This is my submission for June’s Emacs Carnival, Underappreciated Emacs built-ins. You can find detailed information in the excellent documentation at (info “(emacs) Debuggers”). 或者正如我喜欢称呼它的那样,GLORIOUS(光荣的)Unified Debugger。这是我为六月 Emacs 嘉年华提交的文章,主题是“被低估的 Emacs 内置功能”。你可以在 (info “(emacs) Debuggers”) 的优秀文档中找到详细信息。
Multi debugger support
多调试器支持
I mostly use its GDB Graphical Interface. But it supports multiple debuggers such as, lldb (LLVM debugger), perldb (Perl debugger), jdb (Java debugger), pdb (Python debugger), guiler (Guile!), dbx (Debugger with support for C, C++, and so.), xdb (Debugger for MS Windows (?)), sdb (System Debugger). 我主要使用它的 GDB 图形界面,但它支持多种调试器,例如:lldb (LLVM 调试器)、perldb (Perl 调试器)、jdb (Java 调试器)、pdb (Python 调试器)、guiler (Guile!)、dbx (支持 C、C++ 等的调试器)、xdb (MS Windows 调试器 (?)) 以及 sdb (系统调试器)。
The GDB interface
GDB 界面
The manual has a entire section of the special GDB GUI interface. Which I will illustrate now. This is our demo source file: fibo.c.
手册中有一整节专门介绍 GDB GUI 界面,我将在此演示。这是我们的演示源文件:fibo.c。
#include <stdio.h>
int fibo (int n) {
if (n < 2) return 1;
else return fibo (n - 1) + fibo (n - 2);
}
int main (void) {
for (int i = 0; i < 5; i++)
printf ("%d: %d\n", i, fibo(i));
}
Firstly, we compile via, gcc -g fibo.c -o fibo (The -g flag is necessary for debugging with GDB.) Now that we have a compiled program, we can try executing it.
首先,我们通过 gcc -g fibo.c -o fibo 进行编译(-g 标志对于使用 GDB 调试是必需的)。现在我们有了编译好的程序,可以尝试执行它。
./fibo
0: 1
1: 1
2: 2
3: 3
4: 5
Now we can proceed with starting gdb gud in Emacs. To do so, we type M-x gdb RET fibo RET, and this creates a buffer called *gud-fibo*.
现在我们可以在 Emacs 中启动 gdb gud。为此,我们输入 M-x gdb RET fibo RET,这将创建一个名为 *gud-fibo* 的缓冲区。
Though we won’t be directly using this CLI interface, you can use it if you want to. Let’s set some breakpoints. Yup, you can just click on the fringe (the place left to the text), and automatically set up the breakpoints, which show up like so in *gud-fibo*.
虽然我们不会直接使用这个命令行界面,但如果你愿意,完全可以使用它。让我们设置一些断点。没错,你只需点击边缘(文本左侧的区域),即可自动设置断点,它们会像这样显示在 *gud-fibo* 中:
Breakpoint 1 at 0x401138: file fibo.c, line 7.
Breakpoint 2 at 0x40113f: file fibo.c, line 9.
Breakpoint 3 at 0x401174: file fibo.c, line 16.
Now, we will start debugging by running the program. These buttons: Run, Next Line, Step Line, Up Stack, and Down Stack are part of the tool bar. And immediately it stops at the first breakpoint it encounters, (breakpoint 3). The white arrow represents the current line that is being executed. 现在,我们将通过运行程序开始调试。工具栏中包含“运行”、“下一行”、“单步执行”、“向上堆栈”和“向下堆栈”等按钮。程序会立即停在遇到的第一个断点(断点 3)处。白色箭头表示当前正在执行的行。
Now we can step into fibo function by using Step Line. Notice that we also have a new button for Continue now, which continues until the next break point is hit. So, let us turn off the breakpoint in the for loop. For this we will use the dedicated breakpoints management window.
现在我们可以使用“单步执行”进入 fibo 函数。注意,我们现在还有一个“继续”按钮,它会运行直到遇到下一个断点。因此,让我们关闭 for 循环中的断点。为此,我们将使用专门的断点管理窗口。
Now let’s just spam the continue button a bunch of times. And since we are calling printf in the for loop, there is a pop out dedicated window for I/O. (Notice that it is not just for output).
现在让我们多点几次“继续”按钮。由于我们在 for 循环中调用了 printf,因此会弹出一个专门的 I/O 窗口。(注意,它不仅仅用于输出)。
Well what if I want to see what the value of n is currently? There are actually a bunch of ways to look at that. Here you just kinda put your cursor over the thing you want to get the value of … and you just get it? :) The other way to use to the locals window, But then who wants to keep track of this manually? Let’s just watch the variable, so that we get update whenever it changes.
那么,如果我想查看当前 n 的值是多少呢?实际上有很多方法可以查看。你只需将光标悬停在你想要查看值的对象上……然后就能直接看到它?:) 另一种方法是使用局部变量窗口,但谁想手动跟踪这些呢?让我们直接监视该变量,这样每当它发生变化时,我们都能得到更新。
Closing
结语
I would like to cover many other features, but I unfortunately don’t have the time to get through all of them, maybe I will write a sort-of sequel covering more features and other debuggers supported by GUD. 我本想介绍更多功能,但很遗憾没有时间一一详述。也许我会写一篇续集,涵盖更多功能以及 GUD 支持的其他调试器。