In general, GDB is mainly debugging the C + + program. To debug C + + programs, first at compile time, we have to
Add debugging information to the executable file. This can be done using the-g parameter of the compiler (cc/gcc/g++). Such as:
> cc-g hello.c-o Hello
> g++-G hello.cpp-o Hello
Without-G, you will not see the program's function name, variable name, instead of the memory address of the runtime. When you use
After the debug message is added and the target code is compiled successfully, let's look at how to debug it with GDB.
There are several ways to start gdb:
1. GDB <program>
Program is your execution file, usually under the catalogue of course.
2. GDB <program> Core
Using GDB to debug a running program and core file at the same time, core is the program after the illegal execution of core dump generated
The file.
3. GDB <program> <PID>
If your program is a service program, you can specify the process ID at which the service program runs. Gd
B will automatically attach up and debug him. The program should be searched in the PATH environment variable.
GDB start, you can add some gdb start switch, the detailed switch can be viewed with gdb-help. I'm just down here.
Examples of some of the more commonly used parameters:
-symbols <file>
-S <file>
Reads the symbol table from the specified file.
-se file
Reads the symbol table information from the specified file and uses it in the executable file.
-core <file>
-C <file>
Core file of core dump when debugging.
-directory <directory>
-D <directory>
Add a search path to a source file. The default search path is the path defined by path in the environment variable.
When GDB is started with gdb <program>, GDB searches the path path and the current directory for <program> source files
。 To confirm that GDB is reading the source file, you can use the L or List command to see if GDB can list the source code.
In GDB, run the program using the R or Run command. program runs, you may need to set up the following four things.
1, program operation parameters.
Set args can specify runtime parameters. (Example: Set args 10 20 30 40 50)
The show args command can view the set run parameters.
2, the operating environment.
Path <dir> can set the running paths of the program.
Show paths view the running path of the program.
Set environment VarName [=value] Sets the environment variable. such as: Set env User=hchen
Show environment [varname] View environment variables.
3, working directory.
CD <dir> equivalent to the Shell's CD command.
The PWD displays the current directory.
4, the input and output of the program.
Info terminal shows you the terminal mode used by your program.
Use redirection control program output. such as: Run > outfile
A TTY command can refer to an end device that writes input and output. such as: Tty/dev/ttyb
Specifically, it is
For example, a program named prog parameter is-l A-C ABC
Then, run gcc/g++-G Prog.c/cpp-o Prog
You can debug the program with GDB prog
#gdb Prog
go to gdb Debug interface
The Input Parameters command set Args is followed by the parameters to be used by the program, note that no longer with the program name, directly add parameters, such as:
Set args-l a-c ABC
Enter after carriage return
R
can start
GDB Debug with Parametric program