Address: http://emacser.com/emacs-gdb.htm
1
Introduction
In addition to powerful editing functions, emacs can also be used as the front-end of the debugging tool GDB to debug the program. Emacs can be used for debugging. You can write and debug the program to Emacs and use the powerful functions of Emacs to assist debugging. emacs is an essential function for IDE.
This article assumes that the reader has basic program debugging knowledge and wants to know the corresponding operations for Basic Debugging in Emacs. The level is limited. Welcome to shoot bricks.
2
Preparation: Compile the debugging information in the program
To enable GDB to debug the program, you must add the required information during compilation. If you use gcc/g ++ for compilation, you need to use the-G parameter, for example:
?
View code
Bash
If you use the./confiugre & make compilation process, you can introduce the-G parameter as follows:
?
View code
Bash
1 2
|
CFLAGS="-g" ./configure make |
Note: Do not add any optimization parameters (such as-O and-O2). Otherwise, the debugging may be quite different ......
3
Start: Enable the Emacs debug mode Gud 3.1.
Run GDB
After compiling the program, you can start debugging. Run the gdb command M-x gdb ret directly.
The GDB command to be executed will appear in minibuffer. Example: GDB-annotate = 3 prog
If there is an executable file in the current directory (usually the file to be debugged), GDB will automatically complete the executable file later. Otherwise, you must add the program file to be debugged in minibuffer.
Name.
Press enter again. The Gud (Grand uniied debugger) of Emacs will be associated with GDB and the program to be debugged will be loaded.
3.2
GDB Interface
After GDB is started, the Emacs interface will become one of the following two types:
Single-pane GDB Mode |
GDB multi-pane Mode |
You can use GDB-starter-windows to switch between the two interfaces.
If the interface is messy (for example, use completion in minibuffer to view help and re-compile the Program), you can use GDB-restore-windows to restore the interface layout.
3.3
Summary
Command |
Function |
GDB |
Start GDB for debugging |
GDB-embedded-Windows |
Switch Single-pane/Multi-pane Mode |
GDB-restore-Windows |
Restore pane Layout |
Next we will start to debug the program.
4
Debugging: set breakpoints and control program flow 4.1
Set and delete breakpoints
First, set the breakpoint where you want to debug it. There are two methods:
First, click fringe on the left side of the line to which you want to set the breakpoint (that is, the blank part between the left side and the scroll bar of the text ). A friend who hides fringe can display it in M-x fringe-mode.
Second, use the default shortcut C-x C-a c-B, or C-x <SPC>. They are all associated with the command gud-break.
Regardless of the method used, fringe displays a red dot on the line with the breakpoint set, indicating that the line has a breakpoint:
Breakpoint marker on fringe
In addition, the existing breakpoint information is displayed in the breakpoint Buffer:
Breakpoint Buffer
To delete a breakpoint, you can also click the breakpoint on fringe or use the shortcut key C-x C-a c-d (corresponding to the command gud-Remove ).
You can click a breakpoint on the breakpoint buffer to switch to the breakpoint location. Moving the cursor to the breakpoint and pressing the carriage return will also have the same effect.
You can press the Space key on the breakpoint buffer to switch the activation and disabling statuses of the breakpoint.
4.2
Run the program
After the breakpoint is set, you can run the program. Click
And then start running. You can also run the command gud-go. It is strange that no default shortcut key is bound.
When the program runs to the breakpoint, the program stops at the breakpoint and automatically opens the code file where the stopped statement is located. At the same time, there is a triangle indicator at the Stop Statement on fringe.
Current statement indicator
Now let's run the program step by step.
4.3
One-step execution, run to the cursor
One-step execution is the most common function in debugging. There are two types of single-step execution: using a function call as a statement to execute (next) and entering the function for debugging (STEP) when a function is encountered ).
To use the first method, the default shortcut key is C-x C-a c-N, and the corresponding command is gud-next. You can also click
.
The default shortcut for the second method is C-x C-a c-S, and the corresponding command is gud-step. You can also click
.
To jump out of the current function, run the command gud-finish. The default shortcut is C-x C-a c-f.
Available.
In Emacs, you can also run to the row where the cursor is located. Run the command gud-until. The default shortcut is C-x C-a c-u. 1
You can also drag the current statement indicator to any row, and the program will run to that row and stop.
4.4
Continue running the program
After the program is interrupted, continue to run the program, still use the gud-Go command or
You can also use the command gud-cont. The shortcut key is C-x C-a c-r.
4.5
Summary
Function |
Command |
Default shortcut |
Add breakpoint |
Gud-break |
C-x C-a c-B or C-x <SPC> |
Delete breakpoint |
Gud-Remove |
C-x C-a c-d |
Run/continue the program |
Gud-go |
None |
One-step execution, ignoring Functions |
Gud-next |
C-x C-a c-n |
Step by step to enter the Function |
Gud-step |
C-x C-a c-S |
Jump out of the current function |
Gud-finish |
C-x C-a c-F |
Statement where the cursor is located |
Gud- |
C-x C-a c-u |
Continue running the program |
Gud-cont |
C-x C-a c-r |
5
View variable values
You can view the change value during debugging. Emacs provides a convenient function for us to view the value of a variable.
5.1
Local Variable Buffer
If GDB-starter-Windows is enabled, locals is displayed in the upper right corner.
Buffer, showing the value of the current local variable. If the display is a register buffer, click locals on the left to switch to locals.
Buffer. You can conveniently observe the values of each variable.
If you have not enabled GDB-external-Windows, you can use GDB-display-locals-buffer to display the buffer.
5.2
View variable values
When it comes to variables not displayed in locals or complicated structures, you need to use the function of observing variables.
Place the cursor on the variable to be observed and execute the command gud-watch to add the variable to the observation list. The default shortcut key is C-x C-a c-W. You can also use
.
The observed variables will be displayed in speedbar. For a complex structure, click the plus (+) on the speedbar to expand or contract it. Pressing the Space key on the "+" symbol also has the same effect. 2
Sometimes the variables observed by Emacs are not what you want, generally a-> B. In this case, you can select the part to be observed and then use the above method.
Observe variables in speedbar
5.3
DISPLAY variable values with a tooltip
You can use gud-tooltip-mode to enable or disable the tooltip. When the function is enabled, the value of the variable is displayed in the tooltip when you move the cursor over the variable name.
Display the variable value in the tooltip
5.4
Summary
Function |
Command |
Default shortcut |
Observe Variables |
Gud-watch |
C-x C-a c-W |
Expand/Contract variable |
|
<SPC> |
Tooltip for enabling/disabling |
Gud-tooltip-Mode |
|
6
Input and Output
If the program needs to interact with standard input/output, you may need to use the functions described below.
6.1
Separate Io Buffer
By default, the input and output of the program are displayed in GDB buffer. In this way, the output information is mixed with the gdb information, which is inconvenient to read. At this time, you need to display the input and output separately in a buffer for easy viewing.
Using GDB-use-separate-io-buffer, you can create an IO buffer on the right side of the program code buffer, and the program will redirect operations on the standard input and output to this. If you execute this command again, it will be hidden.
Io Buffer
6.2
Input data
When you need to input data, you only need to input data in the IO buffer and press Enter. The input data is bold and distinguished from the output information.
6.3
Redirect to file
Sometimes we have prepared the data for input in the file to avoid tedious input during debugging. At this time, you need to redirect the input and output during debugging.
To redirect, you can only use the built-in functions of GDB. Enter run <data. In> data. Out in GDB buffer to redirect the standard input to data. In and redirect the standard output to data. Out.
7
Key Binding
To be honest, it is really troublesome to bind the built-in buttons of Gud. Three key combinations are required to use a function. Even if you press Ctrl, the mini-finger presses the acid. Therefore, you can bind commonly used buttons to a convenient location to enjoy the same pleasure as other ides.
The following code binds F5, F7, and F8 to gud-go, gud-step, and gud-Next respectively:
?
View code
LISP
1 2 3 4
|
(add-hook 'gdb-mode-hook '(lambda () (define-key c-mode-base-map [(f5)] 'gud-go) (define-key c-mode-base-map [(f7)] 'gud-step) (define-key c-mode-base-map [(f8)] 'gud-next))) |
The reason why I am bound to C-mode-base-map is that I basically debug it in the Code buffer. To use it in GDB-buffer, use gud-mode-map. If you want to use it on all buffers, You can bind it to the global button:
?
View code
LISP
(global-set-key [(f5)] 'gud-go) |
8
End
With the debugging function, Emacs is complete as an IDE. This article introduces the basic methods for GDB debugging in Emacs. The debugging functions of Emacs are far more than that. For more information, see Emacs manual.
Debuggers
.
Because I am also learning and writing, there must be many shortcomings or errors. Please give me more advice.
Footnotes:
1
Note: during use, only the row where the cursor is located after the current row and in the same function; otherwise, it will jump to a strange place.
2
I often don't respond when I use it, or I only expand it after adding new elements. I want to explain it to someone else.