Linux uses gdb to debug C programs

1. Some basic commands of gdb

l: Display code
l n: jump to the code of the nth line of the current code page
l filename.c :n: jump to the nth line of the filename.c file code
b 行号: add a breakpoint
info break: view the breakpoint information
delete 断点编号: delete the breakpoint
r, run the program
n, single-step execution
c, Continue to execute, stop execution when a breakpoint is encountered
p, print
s, enter a function
finish, exit a function
q, and exit gdb

2. Debugging example

First write a main.c program, which is used to output the string input from the keyboard in a loop until it encounters "end" to end the loop:

insert image description here
insert image description here

Compile and run main.c:

insert image description here

It is found that it is not the same as our expectation. After entering "end", the loop does not end, and the process is terminated by Ctrl+c.

  1. Enter gdb, gdb to debug the Release version:

insert image description here

It can be seen from the above figure that debugging is currently not possible. The reason why some information can be printed out is because the information is printed by gdb, and the main program has not been executed. The following sentence tells us that we did not find it in the main program
insert image description here
. Appropriate symbols, that is, do not contain debugging information, so it cannot be debugged at this time. When entering lcode for display, the following sentence
insert image description here
tells us that no debug symbols were imported. It shows that there is a problem with the main program at this time, and no debugging information is included. Type q to exit gdb.

  1. gdb to debug the Debug version:

(1) Compile and track

Follow gcc -o main main.c -gto compile main.c to generate the executable program main, and use gdb to track the main program:

insert image description here
At this time, we can insert image description here
see through this sentence that gdb has read the symbol table from main.

(2) Display the written code

lDisplay code through gdb command

insert image description here

Only 10 lines of code can be displayed at a time.

(3) Set breakpoints

Grammatical form: break 行号, break can be abbreviated as b

Add a breakpoint to line 13:

insert image description here

You can continue to set a breakpoint:

insert image description here

Check which breakpoints are added info break:

insert image description here

Remove breakpoint delete Num:

Remove the 2nd breakpoint:

insert image description here

(4) Run the program for debugging

Input runcan be abbreviated as r, press Enter:

insert image description here

The current program execution stops at line 13.

Enter n, and let the program step through:

insert image description here

It can be seen from the above figure that the string "hello" is entered, and then the program runs to the if statement on line 17.

At this time, we enter p buffto view the contents of some buffs:

insert image description here

It can be seen that the string "hello\n" is stored in the current buff.

Continue typing nto step through:

insert image description here

The current program has been executed to 13 lines.

Let's continue stepping, this time enter the string "end", and the program jumps to the if statement on line 17:

insert image description here

At this point, let's enter again p buffto view the contents of some buffs:

insert image description here

It can be seen that the string "end\n" is stored in the current buff, which is different from the string "end" we input.

Therefore, when continuing to execute, the program will still enter a loop:

insert image description here

At this point, we found the problem in the code through debugging.

  1. exit gdb

Enter qto exit gdb. At this time, it will prompt that if you want to exit, it will kill the main program just now. If you want to exit, input ymeans exit, and press Enter to exit gdb.
insert image description here

After exiting gdb, the main program just ended:

insert image description here

  1. Correct code by debugging results

Change strcmp() to strncmp():

insert image description here

At this point, we will compile main.c and execute the main program. The result is as follows:

insert image description here

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/132248874