gdb program debugging in Linux environment

  • This article will introduce gdb and some commonly used gdb debugging commands;

gdb introduction

Gdb is a debugger in linux . Since linux is a command-line operation, debugging must also be carried out in the command line. Compared with the debugging of VS and other IDEs under win, the operation is more cumbersome to use, but program debugging The ideas are general.

However, in some development scenarios , there is no special IDE designed. In this case, there is no way to use the IDE to debug. At this time, you can only use gdb to debug .

As the saying goes, a qualified programmer spends most of his time in debugging, so it is very important to be familiar with gdb debugging!


Enter the gdb debugging environment

  1. Create the source file of mytest.c and write the following code for debugging;

insert image description here

Compile and run results:

insert image description here

Enter the gdb environment for debugging:

  • Command: gdb + program name

insert image description here

But under the next version, we found that when we input the gdb command: l 0, an error was reported:

insert image description here

The reason is that by default, the programs we generate by using gcc/g++ under linux are all release versions . This kind of released version does not have debugging information and cannot be debugged. It is for users;

We need to debug, so we have to compile with the option -g to tell the compiler to compile the debug adjustable version of the program!
insert image description here

After further observation, we can find that the release version is smaller than the debug version, because of optimization, debugging information, etc. are erased:


command learning

After successfully entering the gdb debugging environment and being able to debug, we introduce the learning of common instructions

l (list) command

l(list) : Display the code in the executable program for easy observation

insert image description here

When we use the l command to match a number: l + num means to display the code from the num line;

At this time, only 10 lines of code can be displayed:, then continue to press Enter, and the subsequent 10 lines of code can be displayed;

insert image description here

When we use the l command to match two numbers (separated by commas): l num1, num2 means to display the code from line num1 to line num2;
insert image description here

When we use l+function name: l addToVal means to display the code of the addToVal function, and also display 10 lines each time;

insert image description here

b (break) command

b + number of lines: means to add a breakpoint at the specified line (breakpoint)

break+function name: enter the specified function , and set a breakpoint on the first line of valid code within its scope

Add breakpoints on lines 5 and 10:

insert image description here

Set a breakpoint at the first line of code in the addToVal function:

insert image description here

info b command

info b: display information about all breakpoints

insert image description here

d command

d + breakpoint number : delete the breakpoint with this number

insert image description here
d : delete all breakpoints

insert image description here

(Note that the breakpoint number is not the line number, you need to use info to check the breakpoint number Num before d)

r (run) command

r(run) : The program runs the command, runs to the first breakpoint with a breakpoint, and runs the entire program without a breakpoint;

Run the entire program without breakpoints:

insert image description here

Run to the first breakpoint:
insert image description here

The number of the breakpoint , the function name , the current file name, and the line number of the breakpoint will be displayed ;

n (next) command

n(next): On the basis that the program has already been run, it will be executed step by step . When a function call is encountered, it will not enter the execution and complete it directly. It is similar to F10 in Debug debugging in VS

insert image description here

s (step) command

s(step): Execute statement by statement on the basis that the program has been run. When you encounter a function, you can enter the function to observe the details, similar to F11 in Debug debugging in VS

insert image description here

c (continue) command

c(continue): Jump from the current breakpoint to the next breakpoint on the basis that the program has been run , similar to F5 in Debug debugging in VS

insert image description here

Stop at the breakpoint at the entrance of the addToVal() function on line 24, and use the c command to jump directly to the breakpoint on line 26, during which the addToVal() and Print() functions have been executed!

bt (breaktrace) command

bt : View the stack frame information (stack frame number and function) and parameters of function calls at all levels

insert image description here
Since the interview was asked about this command, make the following expansion:

  • bt(backtrace) break step next continue These instructions must be clear; they are the most basic and commonly used;

bt + number of stack frames --> This is very useful for calling stack frames, and you can ignore those stack frames that you don't care about. For example, if bt 2 is executed, only two stack frames will be displayed.

f(frame) + stack frame number --> switch stack frames, the running context of the program corresponding to each stack frame is different, for example, the local variables of stack frame 1 and the local variables of stack frame 2 are different, only switching to Only after a specific stack frame can you view the local variable information corresponding to the stack frame
f(frame) + stack frame address --> switch stack frame; the frame address is the address corresponding to the stack frame. If the program crashes, the stack backtrace information may be destroyed, then you can use this command to switch the stack frame through the stack frame address

i(info) + locals --> view the values ​​of all local variables of the current frame
i(info) + args --> view all function parameters and values ​​of the current frame

i(info) + f(frame) + stack frame number/address --> View the details of the stack frame

In addition, how does gdb debug multi-threading (each thread has its own handler function, which is essentially a stack frame)?

info+thread displays all currently debuggable threads, get the thread ID
thread+thread ID to switch to a certain thread, then use bt to view the stack frame of the current thread, break to set a breakpoint, etc.

finish command

finish: When inside a function , if you don’t want to execute the function anymore, you can use finish to end the function directly, thus jumping out of the function , 1. Then return to the calling place of this function, and wait for further commands; 2. Get the return of this function value;
insert image description here

p (print) command

p(print)+variable name: print the value of the variable in the current stack frame

insert image description here
p(print)+(expression) : print the value of the expression in the brackets of the current battle formation

insert image description here

display command

display temporary variable: track a variable, the variable is resident on the screen , and its value is displayed every time it stops; it is a bit like adding a monitored variable in the monitoring window of VS;

insert image description here

undisplay command

undisplay+temporary variable number: make it no longer resident on the screen (use with display and have the opposite effect)

insert image description here

until command

until+line number: jump directly to the specified valid code line , generally used to directly jump out of a loop or recursion

insert image description here

disable command

diable+breakpoint number: disable the specified breakpoint

insert image description here
disable b: disable all breakpoints

insert image description here

enable command

Enable breakpoints, the same as the above disable usage, no longer described;

Guess you like

Origin blog.csdn.net/wtl666_6/article/details/128524245