This is the GNU debugger. Usage:
gdb [options] [executable-file [core-file or process-id]]
gdb [options] --args executable-file [inferior-arguments …]
Debug core file
Configuration Environment
Segmentation fault (core dumped)
Set ulimit -c unlimited to take effect permanently
vim /etc/security/limits.conf
修改后如下面两行
$ ulimit -c unlimited #Indicates no limit on core file size
$ ulimit -c 10 #Set the maximum size in blocks. The default block is 512 bytes.
Set core file generation address
vim /etc/sysctl.conf
Add a line: kernel.core_pattern=core-%e-%p-%t
sysctl -p
Let the configuration take effect immediately
Summarize
If a segmentation fault occurs, the core file will appear in the current execution directory.
$ gdb program file name core file name
Debugging a running program
What if the program is already running?
First use the ps command to find the process id:
$ ps -ef|grep process name
or:
$ pidof process name
attach method
Assume that the process ID obtained is 20829, you can debug the process in the following way:
$ gdb
(gdb) attach 20829
Locating crash issues—addr2line
Sometimes the program crashes but unfortunately no core file is generated. Is there nothing that can be done? Still an example of cmdTest. After running cmdTest, we can obtain the following content through the dmesg command
1 | [27153070.538380] traps: cmdTest[2836] trap divide error ip:40053b sp:7ffc230d9280 error:0 in cmdTest[400000+1000] |
This information records the basic reason for the cmdTest operation error (divide error) and the error location (40053b). We use the addr2line command to obtain the specific line number of the error:
1 2 | addr2line -e cmdTest 40053b /home/hyb/practice/cmdTest.c:4 |
You can see that the addr2line command translates the address (40053b) into the file name (cmdTest.c) and line number (4), and determines the error location.
summary
This section mainly introduces two types of GDB startup debugging methods, namely debugging unrunning programs and already running programs. There is also a brief explanation of what kind of programs can be debugged.