Which company is best for embedded development terminal debugging?

Follow the + star public account and never miss exciting content

4ac0767c247f3a52da017444e55260fa.gif

Source | Zhihu Wei Yixiao

Embedded development is inseparable from debugging tools, and there are many debugging tools on the market, but in terms of compatibility and versatility, GDB definitely takes the lead.

Today let’s talk about this powerful GDB tool.

Streaking status: original GDB command line

Before putting on various clothes, you must at least learn to run naked first. Find a simple GDB cheat sheet and compare it:

c5c2348a36e11cf36eba55f63a4e47c7.png

When a crash occurs in the production environment, because the online server generally does not have a development environment and no supporting source code, after the program crashes, if you are too lazy to drag the core file back to the development machine for inspection, you can first take a simple gdb look at the online server. .

GDB commands are densely packed, and the most commonly used ones are the ones on the table. For example, the first step after entering is to use bt to check the call stack, info local to check the local variables, and then use up/down to go up and down between different levels of the entire call stack. Move around, check the values ​​of local variables everywhere, and print an expression. Even if there is no code, you can generally debug by looking at the symbols and disassembly.

If you encounter a complicated BUG, ​​you must cooperate with the source code. Then you have to pull the core file into the development environment, then use gdb to debug against the source code, and use the list [line number] command to view the currently running source code, and then cooperate with other method for debugging.

Then at this time, if the debugging complexity continues to increase, you need to keep breaking points. After each next/step step, you need to list the source code before and after, or use disassemble [function name/address] to check the instructions. Many people will feel crazy. At this time, we need to put on some underwear for the naked GDB.

Put on your underwear: TUI

This is the text interface that comes with gdb. Use the gdb -tui command to start it, such as:

gdb -tui hello

This will open our text interface:

b1f2d8414a5809afbfe232bd3eba323f.png

The top is the source code window, and the bottom is the gdb terminal. The window management shortcut keys imitate emacs, and use cx o to switch windows. If you still want to view the command window, you can enter:layout split

f4007db9ad87d8c7160878aa232fbfc0.png

Then the source code window above will scroll when you single step, which is much more convenient than the non-stop list before. If you want to see the source code before and after, you can continue cx o. Switch the window and scroll up and down.

Sometimes after you switch the stack frame up/down, the above code may not have time to update. Then you use the update command to position the above code window to your latest execution position. Sometimes you also need CTRL-L to redraw the entire interface.

In the tui mode that comes with gdb, you can not only view the code/instructions at any time, but also view the registers. Well, in fact, no matter how simple the gdb is, it still comes with a pair of underwear, but many people forget to take them out and wear them.

Put on your underwear: gdbinit

If the above text TUI information is not rich enough, you may be interested in .gdbinit. ~/.gdbinit is a gdb configuration script that can set some plug-ins written in python, such as peda:

9cf72516d36adb58369794346271d4ea.png

After ~/.gdbinitconfiguring peda above, you can see that the command prompt changes from (gdb) to gdb-peda$. Every time you type a single-step command, peda will display extremely rich information. You can also configure to add more. With highlighting, you can get an enhanced version of the command line.

But I don't use this much, because it will affect the TUI mode interface, and I don't like to make too many messy things in my gdb. At the same time, when our debugging needs continue to become more complex, you have to keep breaking points and frequent single steps. The constant input of various instructions in these traditional command lines has obviously made us exhausted. Can we directly View the source code on the interface and set shortcut key breakpoints directly on the code. What about shortcut key single step?

Yes, try cgdb.

Jacket on: cgdb

CGDB is similar to gdb tui and is divided into terminal window and code window:

b21a090038b72e9af0ac0ad8d47a625b.png

The above code window is called cgdb mode, and the lower gdb window is called gdb mode. Debugging is to constantly switch between the two modes. The keys imitate vim, press ESC to switch back to cgdb mode, and press i to switch to gdb mode.

This looks similar to TUI gdb? In addition to syntax highlighting, how is it more efficient than gdb tui? The answer is: most operations can be performed using shortcut keys in cgdb mode (that is, the source code window).

Use the arrow keys or hjklto move the cursor, page-down/upor c-f / c-bto turn pages forward and backward. Pressing the o key will list the source code list of the current executable file. You can switch to view other related code files. Press /or to search for documents.

The above shortcut keys basically correspond to vim, allowing you to browse the source code conveniently. At the same time, you can use the space bar to switch breakpoints in the code window. There are other shortcut keys:

  • F5 - Send a run command to GDB.

  • F6 - Send a continue command to GDB.

  • F7 - Send a finish command to GDB.

  • F8 - Send a next command to GDB.

  • F10 - Send a step command to GDB.

See [here] for more shortcut keys. Now single-stepping and breakpoints are much easier than before. You can basically stay in the source code window without exiting. Most of the time, you don't need to enter one by one in the gdb command line next/step.

Edit it to make ~/.cgdb/cgdbrcit easier to use with simple adjustments:

set ignorecase
set ts=4
set wso=vertical
set eld=shortarrow
set hls
map <F9> :until<cr>

Here we roughly set up the search case insensitivity, tab size, split screen mode (default is changed to left and right split screen), search highlighting, and added an F9 shortcut key for jumping out of the loop (there is no such shortcut key by default):

9894ca5bda5c45b183cc6616ed925e69.png

Now that everyone has a widescreen monitor, the default is to change to left and right split screen (supported by cgdb 7.0), which is much more comfortable.

cgdb has more functions, such as viewing the assembly when there is no source file, or cross-checking the source code and assembly, setting marks, etc. For details, see its official documentation.

This small tool can free you from a large number of gdb command lines. The configuration file also allows you to bind various commands to shortcut keys according to your preferences.

Dressed up: Emacs GDB

Although most operations of CGDB can be performed using shortcut keys, there are still many times when you need to switch back to the gdb mode on the right to type commands. For example, when you need to use info local to check local variables or print a certain variable every time you step. When changing the value of a global variable, frequent left and right switching will make you exhausted.

When debugging some relatively complex problems, the above tools can't hold on. For those who usually write code in Vim, I never refuse to use emacs's gdb mode to debug some complex problems. There is no doubt that emacs is currently the most popular terminal. The most powerful GDB front end.

It doesn’t matter if you have never used emacs before. As long as you install emacs and remember the following commands, you can debug it:

  • Switch files: cx cf (press ctrl_x first and then ctrl_f). You can use tab completion when entering the file name.

  • Move the cursor: arrow keys, or cf, cb, cp, cn; page turning pgup/down or cv/mv

  • Switch cache: cx b switches the cache in the current window, cx cb opens a new window and switches the cache.

  • Window operation: cx o window switching, cx 2 / cx 3 up and down / left and right split screen, cx 0 / cx 1 close / exclusive

  • Input command: mx (press ALT_x) You can use tab to complete the input command, and cg to exit the command input.

Of course, don’t forget that the exit command is cx cc. This is enough. First we start emacs:

d1300f9717cd849e32cb06ab446721d6.png

Press mx (alt+x) and prompt to enter the command (if the alt key of the terminal software does not work, you can set the terminal software, or press ESC first within one second, and then press the x key, which is equivalent under the xterm terminal) , type "gdb" and press Enter:

869aad695e0fc438b277fbabb828e8ab.png

The following will ask you how to run gdb. Enter the gdb shell to start the command line and press Enter to start gdb mode:

4904ebd4f20031e2fec871137afc77ea.png

When you see this, you may ask, is this different from the direct command line gdb? Don’t worry, continue mx and enter the command:

gdb-many-windows

Satisfy you immediately:

330b94ba2838719e82a4ff820be11638.png

These commands starting with gud- are the operating commands of the general debugger under emacs gdb-mode. They correspond to gdb commands one-to-one. You can use mx to input these commands, and you can also directly use shortcut keys to operate them.

If you want to replace a certain window with disassembly or register monitoring, you can use mx input:

gdb-display-disassembly-buffer

Then one of the windows will be switched to the disassembly window:

6b4fe8a6bbfbf4d7805d6a97811f3c43.png

The gdb terminal in the upper left corner has been switched to the disassembly window. After reading, remember to cx b to switch back to the gdb console. If you want to avoid a certain window being switched away, you can search for the dedicated window of emacs. Furthermore, we can use the window management shortcut keys to split the layout according to our preferences:

f268127856130477deae0b4d45f53aaf.png

For example, re-split it, move the gdb terminal to the right, and display the place where the gdb terminal was originally displayed as disassembly under the output window. Basically, all the debugging methods you can see on the IDE can be operated in emacs, and emacs can also use gdb to debug mingw programs under Windows.

Then maybe you will complain that operating under emacs is too troublesome. It doesn't matter. Just tweak it a little.

Tune emacs gdb-mode

Open the ~/.emacs (~/_emacs under Windows) file and enter the following content:

(global-set-key [M-left] 'windmove-left)
(global-set-key [M-right] 'windmove-right)
(global-set-key [M-up] 'windmove-up)
(global-set-key [M-down] 'windmove-down)

(global-set-key [f5] 'gud-run)
(global-set-key [S-f5] 'gud-cont)
(global-set-key [f6] 'gud-jump)
(global-set-key [S-f6] 'gud-print)
(global-set-key [f7] 'gud-step)
(global-set-key [f8] 'gud-next)
(global-set-key [S-f7] 'gud-stepi)
(global-set-key [S-f8] 'gud-nexti)
(global-set-key [f9] 'gud-break)
(global-set-key [S-f9] 'gud-remove)
(global-set-key [f10] 'gud-until)
(global-set-key [S-f10] 'gud-finish)

(global-set-key [f4] 'gud-up)
(global-set-key [S-f4] 'gud-down)

(setq gdb-many-windows t)

To explain, if you set ALT + arrow keys at the top, you can jump in the window. If your terminal alt+ direction does not work, you can change it to C-left, C-right, etc. Use ctrl + arrow keys to move the window, and set it later. Shortcuts for some commonly used commands.

Finally, it is set to open gdb-many-windows by default. It is best to add the following paragraph to allow mouse operations:

(require 'xt-mouse)
(xterm-mouse-mode)
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))

(setq mouse-wheel-follow-mouse 't)

(defvar alternating-scroll-down-next t)
(defvar alternating-scroll-up-next t)

(defun alternating-scroll-down-line ()
  (interactive "@")
    (when alternating-scroll-down-next
      (scroll-down-line))
    (setq alternating-scroll-down-next (not alternating-scroll-down-next)))

(defun alternating-scroll-up-line ()
  (interactive "@")
    (when alternating-scroll-up-next
      (scroll-up-line))
    (setq alternating-scroll-up-next (not alternating-scroll-up-next)))

(global-set-key (kbd "<mouse-4>") 'alternating-scroll-down-line)
(global-set-key (kbd "<mouse-5>") 'alternating-scroll-up-line)

Now under xterm, you can use the mouse to switch windows, click the button, and use the scroll wheel to view the source code up and down:

8e9f183dd538b8b4adaa64bb5c883b9c.png

Now you have more freedom. ALT+direction or mouse click can jump directly to the window, and use the following command to operate:

  • F5 - Run, Shift + F5 - Continue

  • F7/F8 code-level single-stepping, and Shift-F7/F8 instruction-level single-stepping

  • F9 - set breakpoint, Shift-F9 delete breakpoint

  • F10 - break out of loop, Shift-F10 out of function

  • F4 - Move to the previous call stack frame, Shift-F4 to move to the next

There are two buttons on the local variable window in the upper right corner. You can click the mouse to switch between displaying local variables and registers. There are also two buttons in the lower right corner to switch between displaying breakpoints or threads. The button in the green part at the top can also be clicked directly with the mouse.

It is finally much easier to use than our original version. If you like to mess around, you can also customize the initial layout of gdb-many-windows, such as a more complex one:

b5ed70ee09bc10e10d92659443cbc7d8.png

It completely meets your various complex debugging needs. I won’t go into details here. If you are interested, please see [this article].

Finally, is it troublesome to open emacs every time when debugging, mx input gdb and then type the file name? Let’s adjust .bashrc:

gdbtool () { emacs --eval "(gdb \"gdb --annotate=3 -i=mi $*\")";}

Okay, under the terminal we only need to enter the program to be debugged:

$ gdbtool hello

You can automatically open emacs and switch to gdb-mode, expand our multi-window, and start debugging. Isn't it very pleasant?

When I don't have an environment for debugging, I usually use gdb-tui to check it out first. If there is an environment or the problem is complicated, I will open emacs to debug. later

After introducing cgdb to me, gdb-tui was retired. If there is no environment, I would first use cgdb to get an overview. When I encountered complex problems, I opened emacs. After some training, it became very smooth.

Make GDB sexier

In fact, emacs is almost there, but if you really can’t remember these limited shortcut keys or gdb commands, you can also try gdbgui:

3f96caa38c12432e8556996ab50e2f9d.pngThe browser-based GDB front-end developed in python can be installed with pip. When using it:

gdbgui --host 0.0.0.0 hello

It will listen to the local port 5000. At this time, we can use the browser to open the address on the server and debug it completely with the mouse.

The middle is the program code, and the bottom is the GDB terminal. The source code can be switched on the top, and you can control run/continue/single-step, etc. On the right, you can view in real time: local variables, call stack frames, memory addresses, breakpoints, threads, etc.

774ea5debbe4e20a45c1d7c9f275c230.png

For example, the call stack and thread view above, and the custom structure query below:

de15f44d40ccce5cd4f490ec30c5aead.png

You can also visually query the data structure:

You can set/delete breakpoints by clicking the mouse on the left side of the code, and switch the display of assembly code by clicking the button above. Basically, the main functions of GDB are visualized, and the modern web interface is displayed in front of you. You don't need to remember any shortcut keys, just click with the mouse.

At the same time, this gdbgui also supports gdb debugging under Windows. Currently, gdbgui is still under active development. As the most beautiful gdb front end, I believe it will become more and more powerful.

Summarize

Well, we have gone through the streaking state of gdb's command line at the beginning, to the most complete debugging environment under the terminal, emacs gdb-mode, and then to the sexy gdbgui. The advantage of GDB is that it can introduce various plug-ins internally and provide various interfaces externally. You can do the debugging yourself. The front-end interface can be completely led by other programs, whether local or remote, so there are various functions. A variety of rich GDB Front End, as well as various remote GDB tools.

It will not limit you to say that you can only debug in this way, or that you only have one kind of UI that can be operated, so there are thousands of combinations of GDB. Without talking about GDB, it seems that there is only one usage of primitive typing commands. Bar? There are so many usages above, just choose two and it will speed up your terminal debugging work in less than half an hour.

Source address: https://zhuanlan.zhihu.com/p/32843449

Statement: The material of this article comes from the Internet, and the copyright belongs to the original author. If there is any copyright issue with the work, please contact me to delete it.

------------ END ------------

68c54d44dcf8cae2c354b11bbb856659.gif

●Column "Embedded Tools "

●Column "Embedded Development"

●Column "Keil Tutorial"

●Embedded column selected tutorials

Follow the official account and reply " Add Group " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.

5ee629ebdda4c68cad95b7ee322e47ca.jpeg

18cb0d5582985018c0148295727ed42f.png

Click " Read the original text " to view more sharing.

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/132680221