Table of contents
gdb debugging
install gdb
You need to install gdb before use:
yum install -y gdb
Preparation before use
When compiling a file, parameters need to be added -g
to ensure that the compiled file can be debugged.
g++ -g demo.cpp -o demo
use gdb
demo code 1
#include <unistd.h>
#include <bits/stdc++.h>
using namespace std;
void print()
{
cout << "hello world 111" << endl;
cout << "hello world 222" << endl;
cout << "hello world 333" << endl;
}
int main(int argc, char **argv)
{
for (int i = 0; i < argc; i++)
cout << i << ' ' << argv[i] << endl;
int a = 1;
cout << a << endl;
a++;
cout << a << endl;
a++;
cout << a << endl;
print();
a++;
cout << a << endl;
a++;
cout << a << endl;
return 0;
}
enter debug
gdb 可执行文件名
After successfully entering gdb debugging, you can find that the command line header descriptor has changed to (gdb) format.
run the program
The run command starts to run the program, and stops when encountering a breakpoint, and ends if there is no breakpoint. This command can be abbreviated as r
(gdb) r
Since no breakpoint is set, it can be found that the program has ended.
set breakpoint
break Line number, set a breakpoint on this line, and the program will stop when running to this line. This command can be abbreviated as b
(gdb) break row_id
Since a breakpoint is set at line 12, the program stops at line 12 after the run command is executed.
Next step
There are two commands that are both next steps.
The next command executes the current statement. If a function call is encountered, it will not enter the function (the function is executed, but gdb does not enter the function). This command can be abbreviated as n
(gdb) next
The step command executes the current statement. If it encounters a function call (it cannot be a library function, a function declared by itself if necessary), it will enter the interior of the function. This command can be abbreviated as s
(gdb) step
continue to execute
After encountering a breakpoint and the program stops, we can use the continue command to continue executing the program until the next breakpoint is encountered. This command can be abbreviated as c
(gdb) continue
After encountering the breakpoint, the program stops at line 12, enter the continue command, and the program continues to execute until the end.
Set main function parameters
In the demo code, parameters can be passed in the main function as follows when running the program.
./demo arg1 arg2 arg1000
The following is the running result:
But in gdb mode, we can't pass in parameters like this.
So we need to pass parameters to the main program in a new way:
(gdb) set args 参数1 参数2 ... ...
print variable
Use the print command to view variable values. Renamed and abbreviated as p
(gdb) print a
quit
Use the quit command to exit gdb debugging. This command can be abbreviated as q
(gdb) quit
coredump
In Linux, the ulimit command is used to control the resources of the shell execution program. Before using coredump, we need to set the maximum value of the core file to unlimited
ulimit -c unlimited
After the program encounters abnormal termination, it will generate a core file, which can be used to debug the program.
demo code 2
#include <unistd.h>
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv)
{
cout << 1 / 0 << endl;
return 0;
}
Compile and run the above code, the program will cause an error and terminate, use the ls command to view the file, you can find a core file.
Use the following command to debug the core file
gdb 可执行文件名 对应的core文件
It is more intuitive to see the wrong statement, and of course other information is also very useful.
program log
Use the form of program log to debug. It is essentially a debugging method that outputs intermediate results. Compared with gdb debugging, the advantage is that the program will not be suspended during debugging, and it is closer to the actual environment.