Common Methods of Debugging C/C++ Programs

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 -gto 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.
insert image description here

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.
Please add a picture description

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.
insert image description here

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

Please add a picture description

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

Please add a picture description

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.

Please add a picture description

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:
Please add a picture description
But in gdb mode, we can't pass in parameters like this.

insert image description here
So we need to pass parameters to the main program in a new way:

(gdb) set args 参数1 参数2 ... ... 

insert image description here

print variable

Use the print command to view variable values. Renamed and abbreviated as p

(gdb) print a

Please add a picture description

quit

Use the quit command to exit gdb debugging. This command can be abbreviated as q

(gdb) quit

Please add a picture description

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.

Please add a picture description

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文件

Please add a picture description

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.

References

  1. C Language Technology Network

  2. Bilibili_C Language Technology Network_The essence of gdb debugging in C language

Guess you like

Origin blog.csdn.net/hesorchen/article/details/124436643