Linux gdb debugger

gdb tool is one of the necessary tools to develop under Linux

Generally speaking, gdb mainly helps you complete the following four functions:

1) Start your program, and you can run the program as you like according to your custom requirements.

2) Allows the debugged program to stop at the breakpoint you set. (breakpoints can be conditional expressions)

3) When the program is stopped, you can check what happens in your program at this time.

4) You can change your program to correct the impact of a BUG

1. GDB common commands

test.c

#include <stdio.h>
int sum(int m);
int main()
{
        int i,n=0;
        sum(50);
        for(i=1;i<=50;i++)
        {
                n+=i;
        }
        printf("The sum of 1-50 is %d\n",n);
}

int sum(int m)
{
        int i,n=0;
        for(i=0;i<m;i++)
        {
                n+=i;
                printf("The sum of 1-%d is %d\n",i,n);
        }

}
~          

1. Start gdb

Compile a test program, - g means it can be debugged. The commands are as follows:

gcc  -g  test.c  -o  test

The results are as follows:

  2. Start gdb with the following command:

gdb  test

gdb   - q   test    // Indicates that GDB version information is not printed, and the interface is clean;

The results are as follows:

  gdb startup completed!

2. View the file.

Type "l" (list) in gdb to view the loaded file, as shown in the following figure

List (abbreviated l): check the source code. 10 lines are displayed by default. Press enter to continue to see the rest.

  It can be seen that the corresponding line number is clearly given in the source code listed in gdb, which can greatly facilitate the positioning of the code.

3. Operation procedure

Run (abbreviated r): run the program until it meets the end or breakpoint, and wait for the next command.

(programs without breakpoints) the results are as follows:

(gdb) r
Starting program: /home/lzf/gdb/test 
The sum of 1-0 is 0
The sum of 1-1 is 1
The sum of 1-2 is 3
The sum of 1-3 is 6
The sum of 1-4 is 10
The sum of 1-5 is 15
The sum of 1-6 is 21
The sum of 1-7 is 28
The sum of 1-8 is 36
The sum of 1-9 is 45
The sum of 1-10 is 55
The sum of 1-11 is 66
The sum of 1-12 is 78
The sum of 1-13 is 91
The sum of 1-14 is 105
The sum of 1-15 is 120
The sum of 1-16 is 136
The sum of 1-17 is 153
The sum of 1-18 is 171
The sum of 1-19 is 190
The sum of 1-20 is 210
The sum of 1-21 is 231

4. Set breakpoints

Setting breakpoint is a very important means in debugging program. It can make the program pause when it runs to a certain position

Therefore, programmers can easily view the value of variables and stack at this location, so as to find out the crux of the code.

Setting breakpoints in gdb is very simple. Just add the corresponding line number after "b"

break (abbreviated as b): B line number. Set a breakpoint on a line:

info breakpoints: displays breakpoint information

After viewing the breakpoint, the user can type "info"   Click "b" to view the breakpoint setting,

Breakpoint information is as follows:

(gdb) b 5
Breakpoint 1 at 0x555555554652: file test.c, line 5.
(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555554652 in main at test.c:5

The user can find the calling function (stack) by typing "backrace" (just enter "bt" at the breakpoint). This function is widely used in program debugging and is often used to eliminate errors or monitor the calling stack.

(gdb) b 19
Breakpoint 2 at 0x5555555546b4: file test.c, line 19.
(gdb) c
Continuing.

Breakpoint 2, sum (m=50) at test.c:19
19	                n+=i;
(gdb) bt
#0  sum (m=50) at test.c:19
#1  0x0000555555554663 in main () at test.c:6

5. Single step execution

Run the code and type 'r' and the program stops at the breakpoint.

View variable values

After the program stops, all the programmer has to do is look at the relevant information at the breakpoint

"p" + variable value

Single step operation

The command "n" (next) or "s" (step) can be used for single step operation. The difference between them is that if there is a function call

, s will enter the function and n will not.

Use command   continue,step,next

Continue: abbreviated as c, enables GDB to resume the execution of the program until the breakpoint is triggered or the program ends. The continue command can accept an optional integer parameter n, which requires GDB to ignore the next n breakpoints.

step: abbreviated as s,     next:   Abbreviated as n

After using n, the program displays the operation of the function sum() and executes downward, while after using "s", it enters the sum() function and runs one step

        As like as two peas, if the next sentence to execute is a function call statement, then the behavior of step and next is exactly the same.

The results are as follows:  

[Inferior 1 (process 2659) exited normally]
(gdb) b 7
Breakpoint 1 at 0x555555554663: file test.c, line 7.
(gdb) b 17
Breakpoint 2 at 0x5555555546ab: file test.c, line 17.
(gdb) r
Starting program: /home/lzf/gdb/test 

Breakpoint 2, sum (m=50) at test.c:17
17	        for(i=0;i<m;i++)
(gdb) s
19	                n+=i;
(gdb) n
20	                printf("The sum of 1-%d is %d\n",i,n);
(gdb) n
The sum of 1-0 is 0
17	        for(i=0;i<m;i++)
(gdb) n
19	                n+=i;
(gdb) n
20	                printf("The sum of 1-%d is %d\n",i,n);
(gdb) n
The sum of 1-1 is 1
17	        for(i=0;i<m;i++)
(gdb) n
19	                n+=i;
(gdb) n
20	                printf("The sum of 1-%d is %d\n",i,n);
(gdb) c
Continuing.
The sum of 1-2 is 3
The sum of 1-3 is 6
The sum of 1-4 is 10
The sum of 1-5 is 15
The sum of 1-6 is 21
The sum of 1-7 is 28
The sum of 1-8 is 36
The sum of 1-9 is 45
The sum of 1-10 is 55

6. View variables

Use the print and whatis commands

The results are as follows:

Breakpoint 1, main () at test.c:7
7	        for(i=1;i<=50;i++)
(gdb) print i
$1 = 0
(gdb) whatis i
type = int

7. Exit gdb

Exit gdb with the quit command:

(gdb) q
A debugging session is active.

	Inferior 1 [process 2698] will be killed.

Quit anyway? (y or n) y

Keywords: Linux

Added by matrixd on Wed, 22 Sep 2021 02:48:33 +0300