Let's talk about GDB together (sixth time: GDB modifies the running environment), talkgdb
Hello, everyone. In the last review, we talked about the information display function of GDB and how to use it to view information when running the program.
Information. In this session, we will continue to introduce the debugging function of GDB: modifying the running environment. Of course, we will also introduce how to use GDB
Modify the running environment. When you leave the rest of your time, your words will go right. Let's talk GDB together!
Everyone knows that a runtime environment is required for running the program, such as allocating memory from the system and allowing the program to read some data. We connect
Let's talk about how to modify the running environment of a program so that the program can run according to our requirements. Modifying the running environment
The most common method is to modify the value of a variable.
As an excellent debugger, GBD provides the ability to modify variables. This function is implemented by using special commands: set variable iVal = 0.
In this example, the value of the variable iVal is changed to 0. The following is an example.
#include<stdio.h>int main(){ int a,b; a = 3; b = 5; if(a < b) printf("a is less then b \n"); else printf("a is larger then b \n"); return 0;}
1. Write a program.Open VIM, input the above program, and save it to the m. c file.
2. Compile the program.Enter gcc-g m. c-o s in the terminal
3. Run the program.Enter./s in the terminal to obtain the following running result:
A is less then B
We can see that the value of a is 3, and the value of B is 5, so a is obviously less than B. What if we want to see the running result of a greater than B? Modify
To make it larger than B, and then re-compile and run. This viewer is right, but this method is relatively time-consuming. We can
To modify the value of a while running the program, and then you can see the result of a greater than B. The procedure is as follows:
Enter gdb s // in the terminal to debug the program using GDB.
(Gdb) start // start debugging
Temporary breakpoint 1 at 0x8048426: file m. c, line 6.
Starting program: xxx/s
Temporary breakpoint 1, main () at m. c: 6
6 a = 3;
(Gdb) n // single-step debugging
7 B = 5;
(Gdb) // enter the carriage return to continue single-step debugging.
9 if (a <B)
(Gdb) set variable a = 8 // change the value of a to 8, which is greater than the value of B
(Gdb) n // single-step debugging
12 printf ("a is larger then B \ n"); // display the result of a greater than B
(Gdb)
You can see the result of "a is greater than B" from the debugging result. Through this example, we can understand that during the process of running the program, such
If you want to verify a result, You can manually modify the value of the variable so that the program can run as required, and then verify that the program is running
The result is not the expected result.
When the program is running, modifying the variable value in the program can be called a very powerful function because it modifies the variable value in the running process.
Program value. If we find the cause of the error in the program, we usually modify the error and compile the verification. With this function, you can
The verification is directly modified during the debugging process, saving the compilation effort and saving time.
Let's talk about GDB today. I want to know what to do later, and listen to the next decomposition!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.