This article first appeared in my public No. Linux cloud computing network (the above mentioned id: cloud_dev) , focused on dry goods share, an inner number 10T books and video resources, backstage reply "1024" to receive, welcome everyone's attention, the end of the two-dimensional code text can sweep .
00 Introduction
GDB (GNU Debugger) is a powerful debugging tools under UNIX and UNIX-like, can debug ada, c, c ++, asm, minimal, d, fortran, objective-c, go, java, pascal and other languages, this is a guide us c primarily by way of example.
01 Basic
1.1 determine whether the program can be debugged
# gdb helloworld
Reading symbols from helloWorld...(no debugging symbols found)...done.
# gdb helloworld
Reading symbols from helloWorld...done.
A kind of the above no debugging symbols found
represents a non-commissioning, the following is adjustable.
Or readelf view pieces of information:
1# readelf -S helloworld|grep debug
2 [28] .debug_aranges PROGBITS 0000000000000000 0000106d
3 [29] .debug_info PROGBITS 0000000000000000 0000109d
4 [30] .debug_abbrev PROGBITS 0000000000000000 0000115b
5 [31] .debug_line PROGBITS 0000000000000000 000011b9
6 [32] .debug_str PROGBITS 0000000000000000 000011fc
If any debug information is not output, it can not be debugged.
1.2 open gdb compiled
Plus -g
options:
gcc -g -o xxx xxx.c
1.3 gdb xxx into the debugger
b 行号或函数
Add a breakpointr
Go to the next breakpoints
Single-step trackingn
Single-step executionp
See currently running program data such as:p a
the value of a variable output format of the output can be provided: for examplep/d a
the decimal value of the output of a variablep array@idx
You can view the value of the array array idx inSetting display, such as
display a
this will be after each output value of a variable debuggingx
Syntax view the memory addresses: x /l
See the original program code,l 9
listed in annex source line 9 (l 2,8
listed data between rows 2-8),l func
are listed in the source code of the specified function attachmentsp x=8
Change the value of the variable x in the debugging process, following the entry into forcejump
Jump to realize, may be the line number of the file, it can also be a file: line, may also be in this format + num jump address is the memory address lines of codesignal
Generating an amount signalreturn
Forced returncall
Forced to calluntil(简写u)
When a body of the loop, the loop is exited rununtil +行号
Run a row to stop, not just out of the loopfinish
After performing this function, and printing stack return address and return values of function parameters and other informationskip
Skip some code function or do not want to focus on a file in the step, asskip function add
expressed over the function to add,skip file step.c
skip the file step.c,info skip
view information skipped.c
Continue to jump to the next breakpointbt
Stack Viewwhere
See what went wrong when an error, andbt
similarinfo b
View breakpoint situationq
drop outptype
Output structure typeinfo registers
Display register values,info all-registers
show all registersinfo breakpoints
You can view all endpoints that have been set
1.5 Advanced Command
1.5.1 set breakpoints
info breakpoints
View all breakpointsb 9
Orb test.c:9
line numbers to set breakpointsb func
Set breakpoints function nameb test.c:9 if b==0
The procedure will be a problem condition, the condition setting breakpoint (so that when the problem occurs, the master card, is used to determine whether the problem)rbreak print*
For the beginning of all print functions are set breakpointsrbreak test.c:.
on all functions set breakpoints test.ctbreak test.c:9
Set temporary breakpoints that only take effect once the breakpointignore 1 30
Ignore front of a breakpoint, 30 times, 31 times from the start to take effect, save timewatch a
Observation of a value or expression changes whendisable/enable num
Disable / enable all / a breakpointclear
Clear all breakpoints, used to clear a function, break a line, such asclear func
,clear linenum
delete
Delete all breakpoints, including watchpoints, catchpoints, used to delete breakpoint whose number, such asdelete bnum
1.5.2 View variables
p 'test.c'::a
Variable print a file,p 'main'::b
print a function defined variablep *p@10
Print content pointer, the length of the print after the @p *$.next
Print the contents of the next node in the list linkNodep/x c
Hexadecimal print content (x: hexadecimal, d: decimal, o: octal, t: binary, c: character format, f: floating point format)x addr
View memory address valuedisplay e
Live off the program displays the value of a variable
1.5.3 Edit Source
After starting the debugger, source code editor and do not want to exit the program, how to do it?
gdb mode the default editor is used /bin/ex
, or if no wants replaced by other editors, such as the VIM, can be:
export EDITOR=/usr/bin/vim
Under gdb mode to edit source code:
(gdb)edit 3 # 编辑第三行
(gdb)edit func # 编辑func函数
(gdb)edit test.c:5 #编辑test.c第五行
After finished, recompile the program ( note be sure to bring the shell command, indicating a shell command ):
(gdb)shell gcc -g -o main main.c test.c
Or this:
Start is to bring tui (Text User Interface), you can debug multiple windows:
gdb main -tui
1.6 debugging parameters
1. Start time of the end of the parameter
gdb --args xxx 参数
After the belt run 2. Start parameters
# gdb xxx
(gdb)run 参数
3. After setting the parameters set args start
# gdb xxx
(gdb) set args 参数
02 multi-process debugging
2.1 attach method
- First find the child process to be debugged:
ps -ef | grep xxx
orpidof
process name - Enter
gdb
mode, enterattach pid
- Break point, run into the debugger
Or simply this: gdb <program> pid
(or gdb <program> --pid pid
), gdb automatically attach.
If there are mistakes:
Could not attach to process. If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.
Switch to root, to /etc/sysctl.d/10-ptrace.conf
the
kernel.yama.ptrace_scope = 1
Read:
kernel.yama.ptrace_scope = 0
2.2 follow-fork-mode mode 方法
- Enter
gdb
mode, inputset follow-fork-mode mode
(mode optional parent, child, parent or child represents the debugging process) - Break point
2.3 Debugging to run the program
It has been running the program usually no debugging information, but if you can not stop the current program restart debugging, you can:
The same code, and then compile a version with debug information, then:
# gdb
(gdb) file hello
Reading symbols from hello...done.
(gdb)attach 20829
03 Debugging Multithreaded
gdb
There is a set of commands that can assist multithreaded debugging:
info threads
: Displays all of the current thread adjustable front thread ID of "*" indicates that the current thread being debugged.thread id
: Debug target id specified threadset scheduler-locking [on|off|step]
: Multi-threaded environment, there will be multiple threads to run, this will affect the outcome of a thread debugging, run this command to set the situation when debugging multiple threads,on
represent only the current thread debugging will continue,off
said they did not shield any thread, all threads can be executed,step
said in a single step, only the current thread will execute.
04 coredump debugging
coredump debugging depends on the core file, core file is generated after the core dump file after the program illegal execution. This is a protective mechanism Linux system, even when some of the costs to develop and test a tremendous effort failed to find the problem occurs, Linux system also provides the last barrier, the core file you can let these the problem visible.
4.1 open core dump
To generate core files when you want the program to crash, enable input ulimit -c
, if the output is 0, indicating off by default core dump.
There are two ways to open, one is through the ulimit command, is to write the code in a program open, talk about the end of the first, the second reference to the text here referenced 1 .
ulimit -c unlimied # 表示不限制core文件大小
ulimit -c 10 # 设置最大大小,单位为块,一块默认为512字节
The above is temporarily open, permanently open to modify the /etc/security/limits.conf
file, add the line:
# /etc/security/limits.conf
# <domain> <type> <item> <value>
* soft core unlimited
This will generate a core file, the file name is the core, and is generated by default in the current program directory, if you want to specify a directory, you can echo "/tmp/corefile-%e-%p-%t" > /proc/sys/kernel/core_pattern
set the core file is saved in the directory "/ tmp / corefile", the file name format is "core- command name -pid- timestamp "
Can also echo 1 > /proc/sys/kernel/core_uses_pid
make the resulting file becomes the core core.pid
, pid is the pid of the process.
4.2 debugging core dump
use
gdb <program> core文件名
After starting gdb or use
-core <file>
-c <file>
To debug the core file
Below is an example:
#include <stdio.h>
int func(int *p)
{
int y = *p;
return y;
}
int main()
{
int *p = NULL;
return func(p);
}
Compile: gdb -g -o core_dump core_dump.c
view the core file with gdb
root@root:~$ gcc core_demo.c -o core_demo -g
root@root:~$ ./core_demo
Segmentation fault (core dumped)
root@root:~$ gdb core_demo core_demo.core.24816
...
Core was generated by './core_demo'.
Program terminated with signal 11, Segmentation fault.
#0 0x080483cd in func (p=0x0) at core_demo.c:5
5 int y = *p;
(gdb) where
#0 0x080483cd in func (p=0x0) at core_demo.c:5
#1 0x080483ef in main () at core_demo.c:12
(gdb) info frame
Stack level 0, frame at 0xffd590a4:
eip = 0x80483cd in func (core_demo.c:5); saved eip 0x80483ef
called by frame at 0xffd590c0
source language c.
Arglist at 0xffd5909c, args: p=0x0
Locals at 0xffd5909c, Previous frame's sp is 0xffd590a4
Saved registers:
ebp at 0xffd5909c, eip at 0xffd590a0
(gdb)
You can see that we can restore the scene when the core_demo execution, and use the program calls the function where to view the current stack frame, you can also use gdb commands to view information register variables.
common problem
Question 1
When open the GDB debugging:
Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-28.el7_5.1.x86_64 numactl-libs-2.0.9-7.el7.x86_64
solve:
- Modify the file
/etc/yum.repos.d/CentOS-Debuginfo.repo
in theenabled
parameter, modify the value 1 yum install nss-softokn-debuginfo --nogpgcheck
debuginfo-install glibc
If the following questions arise:-bash: debuginfo-install: command not found
is installedyum-utils
, use the command:yum install yum-utils
- Libraries were installed two questions prompt:
use: debuginfo-install libgcc-4.8.5-28.el7_5.1.x86_64 numactl-libs-2.0.9-7.el7.x86_64
reference
1 Linux core dump under summary
No public reply back " plus group " takes you into a clearing exchange group
My public number "Linux cloud computing network" (the above mentioned id: cloud_dev) , an inner number 10T books and video resources, backstage reply "1024" to receive and share content including but not limited to Linux, networking, cloud computing virtualization, the contents of the container Docker, OpenStack, Kubernetes, tools, SDN, OVS, DPDK, Go , Python, C / C ++ programming technology, we welcome the attention.