[Example] Create two .c files main.c and add.c through vi:
Compile main.c and add.c files in one step, and add debugging information:
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:
(2) Display code
The input l
shows the code of the current main.c:
Enter l 文件名:行号
code that shows another file:
Enter l add:1
the code that shows add.c
(3) Set breakpoints
① Add a breakpoint in the currently displayed file
Because l add:1
the code that displays add.c has just been entered, so adding a breakpoint at this time is to add a breakpoint in add.c:
②Display other files, add breakpoints in other files
Input l main:1
shows the code in main.c:
At this time, you can add a breakpoint in main.c:
③ 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
(4) Start the program
Enter r
the startup program:
Stop at the first breakpoint.
Enter to n
step through:
View the value of a:
View the value of b:
Since b has not been assigned a value at this time, the value of b is 0
View the value of a+b:
View the addresses of a and b:
You can then enter c
to continue execution until you hit the second breakpoint:
The function add is automatically entered here because a breakpoint is added in add.
Continue stepping through:
At this point, look at the value of d:
Continue to execute, the value of the execution result d is also 5:
(5) Exit gdb
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
(2) Display code
Directly display the code in the main.c file:
(3) Set breakpoints
(4) Run the program
Continuous single-step execution:
At this point, if you continue to execute step by step, it will execute add by default:
Looking at the value of d, it is still correct:
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
3. How to enter the called function without adding a breakpoint to the called function
(1) Enter gdb, track the main program
(2) Display code
Directly display the code in the main.c file:
(3) Set breakpoints
(4) Run the program
At this point, if you want to enter the add function, you need to enter s
:
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
:
At this time, we exited the add function and returned to the main function.
(5) Exit gdb