Http://blog.chinaunix.net/uid-26922071-id-3756018.html
one, multi-threaded debugging
1. Multi-threaded debugging, the most important of several commands:
Info Threads View the threads of the current process.
GDB assigns an ID to each thread, which is used when the thread is being manipulated.
Previous * is the thread that is currently being debugged.
Thread <ID> Switches Debug threads to the specified ID of the thread.
break file.c:100 thread AlL sets a breakpoint on line 100th of the file.c file for all threads passing through here.
set scheduler-locking off|on|step
when debugging a currently debugged thread using the step or Continue command, the other threads are executed concurrently,
How do you just let the debugger execute it?
This requirement can be achieved with this command.
off does not lock any threads, that is, all threads are executed, which is the default value.
On only the currently debugged program will execute.
Step in the case of a single step, except for the next function
(people familiar with the situation may know that this is actually a setting breakpoint and then continue the behavior),
only when the front-line will execute.
thread apply ID1 ID2 command to have one or more threads execute GDB commands
The thread apply all command lets all the debugged threads execute GDB command commands.
2. Examples of use:
thread Generation Notification: GDB gives a hint when a new thread is generated
(GDB) R
Starting program:/root/thread
[New Thread 1073951360 (LWP 12900)]
[New Thread 1082342592 (LWP 12907)]---The following three new threads are created
[New Thread 1090731072 (LWP 12908)]
[New Thread 1099119552 (LWP 12909)]
View Threads: Use info threads to view the threads that are running.
(GDB) info Threads
4 Thread 1099119552 (LWP 12940) 0xffffe002 in?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in?? ()
* 1 Thread 1073951360 (LWP 12931) Main (argc=1, argv=0xbfffda04) at thread.c:21
(GDB)
Note that the line header is the thread ID number assigned by GDB, and the ID number is used when the thread is switched.
Additionally, an asterisk at the beginning of the line identifies the currently active thread
To switch threads:
use thread threadnumber to switch threadnumber to the thread ID number mentioned above.
The following example shows switching the active thread from 1 to 4.
(GDB) info Threads
4 Thread 1099119552 (LWP 12940) 0xffffe002 in?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in?? ()
* 1 Thread 1073951360 (LWP 12931) Main (argc=1, argv=0xbfffda04) at thread.c:21
(GDB) thread 4
[Switching to Thread 4 (thread 1099119552 (LWP 12940)]) #0 0xffffe002 in?? ()
(GDB) info Threads
* 4 Thread 1099119552 (LWP 12940) 0xffffe002 in?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in?? ()
1 Thread 1073951360 (LWP 12931) Main (argc=1, argv=0xbfffda04) at thread.c:21
(GDB)
These are some of the basic commands for debugging multithreading with GDB.
In addition, GDB provides breakpoint settings on threads and commands for issuing commands to specified or all threads
Second, debug macros
under GDB, we cannot print the macro definition because the macro is precompiled.
but we still have a way to debug the macro, which requires GCC mates.
when you compile the program in GCC, add
The -ggdb3 parameter so that you can debug the macro.
Alternatively, you can use the following GDB macro debugging commands to view the related macros.
Info Macro to see which files the macro is referenced in and what the macro definition is.
Macro to see how the macros unfold.
Third, the source file
GDB, you are prompted not to find the source file.
The following checks are required:
compiles whether the programmer added the-G parameter to contain the debug information.
the path is set correctly.
Use the GDB directory command to set the directory for the source files.
Below is an example of a debug/bin/ls (under Ubuntu)
$ apt-get Source coreutils
$ sudo apt-get install coreutils-dbgsym
$ gdb/bin/ls
GNU gdb (gdb) 7.1-ubuntu
(GDB) List main
1192 Ls.c:no such file or directory.
In ls.c
(gdb) directory ~/src/coreutils-7.4/src/
Source Directories searched:/home/hchen/src/coreutils-7.4: $cdir: $CWD
(GDB) List main
1192}
1193}
1194
1195 int
1196 Main (int argc, char **argv)
1197 {
1198 int i;
1199 struct pending *thispend;
N_files int;
1201
Iv. Conditional Breakpoints
The conditional breakpoint is the syntax:
Break [where] if [condition]
This breakpoint really works.
especially in a loop or recursion, or to monitor a variable.
Note that this setting is in gdb, except that GDB will help you check if the condition is satisfied every time you pass that breakpoint.
Five, command-line parameters
Sometimes we need to debug a program that requires command-line arguments, and there are three ways to do it:
gdb command-line-args parameter
the Set args command in the GDB environment.
the run parameter in the GDB environment
vi. variables for gdb
Sometimes, when you debug a program, we don't just look at the variables at run time ,
we can also directly set the variables in the program, to simulate some difficult to appear in the test, compare some errors,
or switch's branch statement. Use the SET command to modify variables in a program.
Also , do you know that there can be variables in GDB?
just like the shell, the variables in GDB start with $, like you want to print an array of elements, you can do this:
(gdb) Set $i = 0
(GDB) p a[$i + +]
... #然后就一路回车下去了
of course, here is just an example, which indicates that the variables of the program and the GDB variables can be interacted with.
Seven, x command
Perhaps, you like to use the P command.
Therefore, when you do not know the variable name, you may be unprepared, because the P command always requires a variable name.
The x command is used to view the memory, and in GdB, "Help X" You can view its assistance.
x/x with hexadecimal output
x/d output in decimal
x/c output with single character
x/i disassembly – Typically, we use x/10i $ip-20来 to view the current assembly ($IP is the instruction register)
x/s output as a string
viii. command Commands
how to automate debugging.
here we introduce command command, simple understanding, it is a set of GDB command package, a bit like word processing software "macro."
Here is an example:
(gdb) break func
Breakpoint 1 at 0x3475678:file test.c.
(gdb) command 1
Type commands for when Breakpoint 1 are hit, one per line.
End with a line saying just "End".
>print arg1
>print arg2
>print Arg3
>end
(GDB)
when our breakpoint arrives, the three commands in the command are automatically executed, and the three parameter values of Func are called out.