Linux uses gdb to debug multi-file C programs

[Example] Create two .c files main.c and add.c through vi:

insert image description here

Compile main.c and add.c files in one step, and add debugging information:

insert image description here

1. When a breakpoint is added in the called function, when the main function is executed, it will automatically enter the called function

(1) Enter gdb and track the main program:

insert image description here

(2) Display code

The input lshows the code of the current main.c:

insert image description here
Enter l 文件名:行号code that shows another file:

Enter l add:1the code that shows add.c

insert image description here

(3) Set breakpoints

① Add a breakpoint in the currently displayed file

Because l add:1the code that displays add.c has just been entered, so adding a breakpoint at this time is to add a breakpoint in add.c:

insert image description here
insert image description here

②Display other files, add breakpoints in other files

Input l main:1shows the code in main.c:

insert image description here

At this time, you can add a breakpoint in main.c:

insert image description here

③ Add a breakpoint to the specified function

Adding a breakpoint to the specified function is to add a breakpoint at the beginning of the function

insert image description here

(4) Start the program

Enter rthe startup program:

insert image description here
Stop at the first breakpoint.

Enter to nstep through:

insert image description here

View the value of a:

insert image description here

View the value of b:

Since b has not been assigned a value at this time, the value of b is 0

insert image description here

View the value of a+b:

insert image description here

View the addresses of a and b:

insert image description here

You can then enter cto continue execution until you hit the second breakpoint:

insert image description here

The function add is automatically entered here because a breakpoint is added in add.

Continue stepping through:

insert image description here

At this point, look at the value of d:

insert image description here

Continue to execute, the value of the execution result d is also 5:

insert image description here
(5) Exit gdb

insert image description here

2. When no breakpoint is added in the called function, it will not enter the inside of the called function

(1) Enter gdb, track the main program

insert image description here

(2) Display code

Directly display the code in the main.c file:

insert image description here

(3) Set breakpoints

insert image description here

(4) Run the program

insert image description here

Continuous single-step execution:

insert image description here

At this point, if you continue to execute step by step, it will execute add by default:

insert image description here

Looking at the value of d, it is still correct:

insert image description here

It can be found that although gdb does not track the inside of add, it skips the execution process inside the add function. Through single-step execution, add execution can pass directly.

(5) Exit gdb

insert image description here

3. How to enter the called function without adding a breakpoint to the called function

(1) Enter gdb, track the main program

insert image description here

(2) Display code

Directly display the code in the main.c file:

insert image description here

(3) Set breakpoints

insert image description here

(4) Run the program

insert image description here

At this point, if you want to enter the add function, you need to enter s:

insert image description here

It can be seen that at this time, it has entered the inside of the add function.

When it is determined that there is no error inside the add function, exit the add function and enter finish:

insert image description here

At this time, we exited the add function and returned to the main function.

(5) Exit gdb

insert image description here

Guess you like

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