Debugging c++ gdb under Linux

GDB overview


GDB is a powerful command line debugging tool. As we all know, the power of the command line is that it can form an execution sequence and form a script. All software under UNIX is command line, which provides great convenience for program development. The advantage of command line software is that they can be integrated very easily, using a few simple commands of existing tools. You can make a very powerful function.
Therefore, the software under UNIX can be more organically combined than the software under Windows. Each can play its own strengths and combine to form more powerful functions. The graphics software under Windows is basically independent and cannot call each other, which is not conducive to the mutual integration of various software. This is not to make any comparison with Windows. As the saying goes, "inches are longer than feet," graphical tools are still inferior to command lines.
Debugging programs with GDB


GDB is a powerful program debugging tool under UNIX released by the GNU open source organization. Perhaps, you prefer the graphical interface method, such as IDE debugging such as VC, BCB, etc., but if you are making software under the UNIX platform, you
will find that the debugging tool GDB is more powerful. The graphical debuggers of VC and BCB have more powerful functions. This is the saying "an inch is long and a foot is short".
Generally speaking, GDB mainly helps you complete the following four functions:
1. Start your program and do whatever you want according to your customized requirements. running program.
2. The debugged program can be stopped at the breakpoint you specify. (The breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happened in your program at this time.
4. Dynamically change the execution environment of your program.
From the above, GDB is no different from general debugging tools. It basically completes these functions. However, in the details, you will find that GDB is a powerful debugging tool, and you may be more accustomed to it. There are graphical debugging tools, but sometimes, command line debugging tools have functions that graphical tools cannot complete.


gdb basic command list:   

                                                    

 


1 Create a new source file vi swap.cc

The content of the source file is as follows:

#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
        int tmp;
        tmp=a;
        a=b;
        b=tmp;
}

int main()
{
        int i,j;
        cout<<endl<<"Input two int number:"<<endl;
        cin>>i>>j;
        cout<<"Before swap(),i="<<i<<" j="<<j<<endl;
        swap(i,j);
        cout<<"After swap(),i="<<i<<" j="<<j<<endl<<endl;
        return 0;
}

Directly copy and paste the generated source file

 2. Generate the executable file g++ -g -o swap swap.cc, Note that you must use the -g parameter< a i=3>, debugging information will be added to the compilation, otherwise the executable file cannot be debugged

3. Start debugging gdb swap

       3.1 View the source file list 1, press Enter to repeat the last command

             3.2 Set debugging breakpoint break 16, set a breakpoint on line 16, info break View breakpoint information (you can also use the abbreviation i b )

         3.3 调试 运行 输入run Some person

         3.3 Single-step debugging, step  or  s to enter Inside the function

                3.4 查 查 查 变 变 变 变 变 变 <<变

                3.5 View the function stackbt, exit the functionfinish

                   3.6 Continue running until the next breakpoint or the end of the main functioncontinue or c

                  3.7 Exit debugging Enterq

 



Debug log:


Debug core file:

To generate a core file, first check whether it is set to allow the generation of core files. If the result of ulimit -c is 0, it means that it is not allowed. You need to set ulimit -c unlimited,



After executing the program, a core file will be generated. Use gdb to debug the core file.


Use the bt command to view the program that generated the error


Gdb debugging program method with parameters:

1, gdb --args ./A V1 V2 V3
2, gdb ./A, after entering gdb r V1 V2 V3
3 , gdb ./A, after entering gdb, set the parameters set args V1 V2 V3 and then directly r



GDB prints all information

set print element 0





Guess you like

Origin blog.csdn.net/u013995172/article/details/45646683