gdb debugging mechanism
In the process of debugging, you can clearly understand the context in order to analyze the program principle and the causes of errors.
- code
- Stack information
- Process and thread status
debug information
- When using gcc or makefile, adding the - g option during compilation means that the debugging symbol information is retained during compilation. The program compiled by this method is significantly larger than the program usually compiled
$ g++ -g -o core_dump2 core_dump2.cpp $ g++ -o core_dump22 core_dump2.cpp -rwxrwxr-x 1 fl fl 56K 1 May 15-17:59 core_dump2* -rwxrwxr-x 1 fl fl 19K 1 June 15-18:18 core_dump22*
- Integrating gdb in cmake
## gdb set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb") set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
Process ID
$ ps -ef | grep core_dump2 fl 71382 71367 0 17:59 pts/1 00:00:00 ./core_dump2 fl 71393 69623 0 18:00 pts/0 00:00:00 grep --color=auto core_dump2
core file
-
Use the command ulimit -a to check whether the core file will be saved after the program crashes. If the core file is retained, it can be used to locate the problem.
-
Use the command ulimit -option value to edit the specified parameters. For example, ulimit -c unlimited edits the value of the core file size option to unlimited.
-
After editing, use source /etc/profile to refresh the environment configuration information contained in the specified file.
$ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31113 max locked memory (kbytes, -l) 65536 max memory size (kbytes, -m) unlimited open files (-n) 8192 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 31113 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited $ ulimit -c unlimited $ source /etc/profile $ ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31113 max locked memory (kbytes, -l) 65536 max memory size (kbytes, -m) unlimited open files (-n) 8192 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 31113 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
-
Core dump file directory and command rules of core dump
-
The default is to be applied to the directory after the chdir is executed under the execution directory of the application or after the program starts.
-
In / etc / sysctl Write the path of core dump file in conf
kernel.core_pattern=/home/aaa/core_dump/core_%e-%p-%u-%s-%t
%e:Application name %p:Application process id %u:Application thread id %s:cause dump Signal id %t:produce dump Event stamp for
-
The configuration takes effect when the command is executed
sudo sysctl -p /etc/sysctl.conf
-
Create core_dump directory
mkdir /home/aaa/core_dump
-
Start and stop of gdb debugging
Target program
Confirm that the symbol information is loaded successfully
- Display of loading failure
fl@fl:~/tmp/test$ gdb ./core_dump GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... ./core_dump: No such file or directory. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (gdb)
- Display of loading success
fl@fl:~/tmp/test$ gdb ./core_dump GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./core_dump... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (gdb)
Specify process
- Run the program/ core_dump22
$ ./core_dump22 1 2 3 ...
- Query the process id of the program
$ ps -ef | grep core_dump22 fl 75837 69623 0 18:48 pts/0 00:00:00 ./core_dump22 fl 76089 71367 0 18:50 pts/1 00:00:00 grep --color=auto core_dump22
- Attach process, this operation requires root permission, sudo gdb attach 75837
$ sudo gdb attach 75837 GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... attach: No such file or directory. Attaching to process 75837 Reading symbols from /home/fl/tmp/test/core_dump22... (No debugging symbols found in /home/fl/tmp/test/core_dump22) Reading symbols from /lib/x86_64-linux-gnu/libstdc++.so.6... (No debugging symbols found in /lib/x86_64-linux-gnu/libstdc++.so.6) Reading symbols from /lib/x86_64-linux-gnu/libc.so.6... Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.31.so... Reading symbols from /lib/x86_64-linux-gnu/libm.so.6... Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libm-2.31.so... Reading symbols from /lib64/ld-linux-x86-64.so.2... (No debugging symbols found in /lib64/ld-linux-x86-64.so.2) Reading symbols from /lib/x86_64-linux-gnu/libgcc_s.so.1... (No debugging symbols found in /lib/x86_64-linux-gnu/libgcc_s.so.1) --Type <RET> for more, q to quit, c to continue without paging--
- At this point, the program enters the suspended state
- To perform specific debugging operations, you need to continue to execute the program and use the continue command
- After debugging, use the command detach to specify the process, and use the command quit to exit gdb
(gdb) detach Detaching from program: /home/fl/tmp/test/core_dump22, process 75837 [Inferior 1 (process 75837) detached] (gdb) q $
core dump file
- Get core files and Applications
- Use the command gdb to debug the application core file to get the crash reason
$ gdb ./core_dump ~/core_dump/core_core_dump-77936-1000-8-1642244418 GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./core_dump... [New LWP 77936] Core was generated by `./core_dump'. Program terminated with signal SIGFPE, Arithmetic exception. #0 0x0000560c0c46719f in main (argc=1, argv=0x7fff72528708) at core_dump.c:9 9 c = a / b; (gdb)
- Debug... Find the reason for the cash.
conclusion
Whether gdb or windbg, the difference is that the command syntax is inconsistent, and the debugging principle is similar.