简体   繁体   English

在Emacs中使用gdb:始终显示源代码

[英]Use gdb within Emacs: always show the source code

I'm a Vim user and don't know much about Emacs. 我是Vim用户,对Emacs不太了解。 I'm interested with Emacs because I find that debugging within Emacs is more pleasant. 我对Emacs感兴趣,因为我发现在Emacs中进行调试会更加愉快。 For example, it provides syntax highlighting and I can set breakpoints with the mouse. 例如,它提供语法高亮显示,并且我可以使用鼠标设置断点。

Everything works well except when printf is encountered. 一切正常,除非遇到printf

Simple code for illustration: 用于说明的简单代码:

1 #include <stdio.h>
2 
3 int main()
4 {
5     int a = 1;
6     printf("%d\n", a);
7     int b = 2;
8     return 0;
9 }

emacs main.c emacs main.c
left click on the bottom half 左键单击下半部分
Mx gdb[return][return] MX gdb [return] [return]
(gdb) b 6 (gdb)b 6
(gdb) r (gdb)r

By now, the source codes are showed in the upper half, and gdb prompt in the bottom half. 到目前为止, 源代码显示在上半部分,而gdb提示显示在下半部分。 This is exactly what I want. 这正是我想要的。

(gdb) n (gdb)n

Now the source codes disappear, and the upper half is used to show stdout instead. 现在源代码消失了,上半部分用来显示stdout。 This is really inconvenient. 这真的很不方便。 I'd like the stdout to show in the gdb buffer, and the sources stay in the upper buffer, just as the gdb -tui mode. 我希望将stdout显示在gdb缓冲区中,并将源保留在较高的缓冲区中,就像gdb -tui模式一样。

Instead of manually setting up your splits each time, try telling GDB which windows you want available . 与其每次都手动设置拆分,不如告诉GDB您想要哪些窗口

For example: 例如:

;; Show main source buffer when using GDB
(setq gdb-show-main t)

Now you can simply use Mx gdb to start GDB, and it should keep your source code buffer displayed in a split window. 现在,您可以简单地使用Mx gdb启动GDB,它应该使您的源代码缓冲区显示在拆分窗口中。

Incidentally, Emacs' GDB interface supports a number of other windows that you may want to enable: 顺便说一句,Emacs的GDB界面支持您可能要启用的许多其他窗口:

If gdb-many-windows is non- nil , then Mx gdb displays the following frame layout: 如果gdb-many-windows为非nil ,则Mx gdb显示以下框架布局:

 +--------------------------------+--------------------------------+ | GUD interaction buffer | Locals/Registers buffer | |--------------------------------+--------------------------------+ | Primary Source buffer | I/O buffer for debugged pgm | |--------------------------------+--------------------------------+ | Stack buffer | Breakpoints/Threads buffer | +--------------------------------+--------------------------------+ 

If you ever change the window layout, you can restore the "many windows" layout by typing Mx gdb-restore-windows . 如果您更改了窗口布局,则可以通过键入Mx gdb-restore-windows来还原“许多窗口”布局。 To toggle between the many windows layout and a simple layout with just the GUD interaction buffer and a source file, type Mx gdb-many-windows . 要在仅使用GUD交互缓冲区和源文件的多个窗口布局和简单布局之间切换,请键入Mx gdb-many-windows

You may also specify additional GDB-related buffers to display, either in the same frame or a different one. 您还可以指定要显示的其他GDB相关缓冲区,它们可以在同一帧中显示,也可以在另一帧中显示。 Select the buffers you want by typing Mx gdb-display-BUFFERTYPE-buffer or Mx gdb-frame-BUFFERTYPE-buffer , where BUFFERTYPE is the relevant buffer type, such as breakpoints . 通过键入Mx gdb-display-BUFFERTYPE-bufferMx gdb-frame-BUFFERTYPE-buffer ,其中BUFFERTYPE是相关的缓冲区类型,例如breakpoints You can do the same with the menu bar, with the GDB-Windows and GDB-Frames sub-menus of the GUD menu. 您可以使用菜单栏以及GUD菜单的GDB-WindowsGDB-Frames子菜单执行相同的操作。

When you finish debugging, kill the GUD interaction buffer with Cx k , which will also kill all the buffers associated with the session. 完成调试后,请使用Cx k杀死GUD交互缓冲区,这还将终止与该会话关联的所有缓冲区。 However you need not do this if, after editing and re-compiling your source code within Emacs, you wish to continue debugging. 但是,如果在Emacs中编辑并重新编译源代码后希望继续调试,则无需执行此操作。 When you restart execution, GDB automatically finds the new executable. 当您重新开始执行时,GDB会自动找到新的可执行文件。 Keeping the GUD interaction buffer has the advantage of keeping the shell history as well as GDB's breakpoints. 保留GUD交互缓冲区的优点是可以保留Shell历史记录以及GDB的断点。 You do need to check that the breakpoints in recently edited source files are still in the right places. 您确实需要检查最近编辑的源文件中的断点是否仍在正确的位置。

You might also like to try Mx gud-gdb . 您可能还想尝试Mx gud-gdb It's a much more bare-bones UI, but I personally prefer it. 它是一个更加简单的用户界面,但我个人更喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM