In programming debugging, there is often a segment error, at which time GDB can be debugged. The specific method is to register the segment error signal handler function and start gdb in the handler function.
The specific code is as follows:
void Segv_handler (int no)
{
Char buf[512];
Char cmd[512];
FILE *file;
snprintf (buf, sizeof (BUF), "/proc/%d/cmdline", Getpid ());
if (! ( File = fopen (buf, "R")))
{
Exit (Exit_failure);
}
if (!fgets (buf, sizeof (BUF), file))
{
Eixt (exit_failure);
}
if (Buf[strlen (BUF)-1] = = ' \ n ')
{
Buf[strlen (BUF)-1] = ' + ';
}
snprintf (cmd, sizeof (CMD), "GdB%s%d", buf, Getpid ());
System (CMD);
}
Registration function:
Signal (SIGSEGV, Segv_handler);
The following is a summary from some:
As a program ape, it is unavoidable to solve various bugs in daily development. For simple bugs through log analysis, or to increase the printing information can quickly locate the cause and solve. But for some of the more complex situations, it is often difficult to locate a bug. Looking at a lot of information, after a constant attempt, I found that gdb debugging can be a great help. Below I will summarize some common techniques and examples of using GDB.
The following is a summary of several key uses:
First, start GDB debugging
Using GDB debugging first when compiling the program with the-G parameter: $ gcc–g–o foo FOO.C
There are several ways to start GDB debugging, depending on the scene to choose the right way, which is also a good place for gdb.
1. When the program is not running, GDB +<program> directly with GDB to run the program;
2. There are two ways to debug GDB in a program run:
A.ps View program Pid,gdb + <program> + PID, automatically hook up to the running program;
B.ps pid,gdb + <program> run GDB, use the Attach + PID instruction to hook up to the program and use detach to cancel the hook process.
3. After the program has died, the GDB +<program> + core file is debugged, and the core file is the "kernel dump" file resulting from the illegal execution of the program. In some cases the core cannot be generated, and the system environment needs to be set up with the ulimit-c unlimited instruction first.
Two, for the second type of GDB start, you can have the following implementation methods.
The program has a segment error, but the exception occurs with some randomness, in order to catch the exception and GDB debugging, in the program to capture the SIGSEGV signal and the following processing, so that when the program runs in the event of a segment error, go directly to the GDB debugging environment.
Third, GDB debugging common directives
The common instructions and introduction of GDB debugging can be consulted here, http://blog.csdn.net/liwf616/article/details/46833107
In the GDB environment, press ENTER directly to execute the previous command
1. Break to set breakpoints,
Break10 set breakpoints in the 10th line of the source program
Breakfunc set breakpoints at the entrance of the Func function
Infobreak Viewing breakpoint information
2. Run program, can be abbreviated as R
3. Next single step tracking, function call as a simple statement execution, can be abbreviated to n
Step single-step tracking, function transfer into the called function body, can be abbreviated as S
Stepi or SI single-step tracking of a machine instruction
Nexti or NI single-step tracking of a machine instruction
4. Continue continue to run the program, can be abbreviated to C
5. Print prints the values of variables, strings, expressions, and so on, which can be shortened to P
P count Prints the value of Count
P COU1+COU2+COU3 Print Expression values
6. BT View function stack
7. Finish Exit Function
8. Quit the GDB
9. The shell does not exit GDB using the shell command
Make <make-args> recompile the program without exiting GDB
Each. Set args specifies run-time parameters. (Example: Set ARGS10 20 30 40 50)
Showargs View the set run parameters.
Path <dir> set the running paths of the program.
Showpaths view the running path of the program.
Set environment VarName [=value] Sets the environment variable. such as: Setenv User=hchen
showenvironment [varname] View environment variables.
14. Other
Cd<dir> equivalent to the Shell's CD command.
The PWD displays the current directory.
Infoterminal Displays the terminal mode used by the program.
TTY refers to an end device that writes input and output. such as: Tty/dev/ttyb
Until a single-step trace in a loop, the command runs the program to exit the loop body. Shorthand U
Four, multi-threaded debugging
The advantage of debugging with GDB is that you can view information such as the stack when the program is running, and you can find the problem intuitively.
Info threads//Displays all threads currently available for debugging, each with a GDB assigned ID in front of each thread;
Thread ID//switch to this ID corresponding to the threads;
BT//Show stack information for this thread
Thread apply all BT//Show all thread stack information
Break fun.c:123 thread ID//Set breakpoints for specified threads
The set scheduler-locking off|on|step//off means that no thread is locked, all threads are executed; on only when the front-thread executes; step takes one step at a time except for the next function, only when the front-thread executes
GDB uses _ Go