GDB debugging
gdb basic command summary
command | |
---|---|
File < file name > | Load the debugged executable file |
run | Single step execution: run the program and stop at the first execution statement. If the executable program needs to pass parameters, it can be written after run |
list | View the source code, abbreviated as l |
set | Set the value of the variable |
next | Single step debugging (single step execution without entering, direct function execution), abbreviated as n |
step | Single step debugging (jump into the custom function for execution, and use finish to exit the calling function of the function), abbreviated as s |
backtrace | Check the stack and hierarchy of function calls, abbreviated as bt |
frame | Stack frame of switching function, abbreviated as f |
info | View the value of the local variable inside the function, abbreviated as i |
finish | End the current function and return to the function call point |
continue | Continue running, abbreviated c |
Print value and address, abbreviated as p | |
quit | Exit gdb, abbreviated as q |
delete | Remove Breakpoint |
Debug executable
-
Whether the file can be debugged by gdb, and judge whether the binary file is compiled with the - g parameter
- Method 1: to debug the C/C + + program, first add the - g parameter when compiling the source code (if there is no - g parameter, the compiler will report No debugging symbols found in)
- Method 2: read the header information of the executable program (see whether it contains debug debugging information) readelf -S test_out | grep debug
[28] .debug_aranges PROGBITS 0000000000000000 00003039
[29] .debug_info PROGBITS 0000000000000000 00003069
[30] .debug_abbrev PROGBITS 0000000000000000 00003378
[31] .debug_line PROGBITS 0000000000000000 00003444
[32] .debug_str PROGBITS 0000000000000000 00003556
Debug core files
core dump: core means memory, and dump means throw out (segment error). When developing and using Unix programs, there will be program down and sometimes prompt (core dumped). Through gdb analysis of core files, it is convenient for us to quickly locate problems
-
How to generate core files
First, input ulimit -c in the current terminal. If it is 0, the core file will not be generated; If it is not 0, the maximum number of core files will be generated (the value may be small); We need to generate the core file and set the core size to unlimited ulimit -c unlimited
-
How to specify the storage core file
%t: Time of core dump generation
%e: Name of the program that generated the core dump
%p: Process ID of generating core dump
-
Method 1: temporary modification of terminal settings
Modify / proc / sys / kernel / core in root mode_ Pattern file, / home / CJJ / GDB test / coredump / is the directory where the core file is stored
cjj@ubuntu:~/gdb-test$ su Password:
root@ubuntu:/home/cjj/gdb-test# echo /home/cjj/gdb-test/coredump/coredump.%t.%e.%p> /proc/sys/kernel/core_pattern -
Method 2: system configuration file setting
Modify the linux kernel configuration file = = / etc / sysctl Conf = =, add the following:
kernel.core_pattern = /home/cjj/gdb-test/coredump/coredump.%t.%e.%p
kernel.core_uses_pid = 1
Enable configuration file: sysctl - P / etc / sysctl conf
-
-
How to debug core files and locate problems
- Method 1: GDB < executable file > < core file >
cjj@ubuntu:~/gdb-test/coredump$ gdb ../test_out coredump.test_out.37006 GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1 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 ../test_out... [New LWP 37006] Core was generated by `./test_out'. Program terminated with signal SIGSEGV, Segmentation fault. #0 __memset_avx2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S:190 190 ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S: No such file or directory. (gdb) (gdb) bt #0 __memset_avx2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S:190 #1 0x00005614d22c419f in main () at test.c:10 (gdb)
-
Method 2: GDB < executable >
cjj@ubuntu:~/gdb-test/coredump$ gdb ../test_out GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1 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 ../test_out... (gdb) core-file coredump.test_out.37006 [New LWP 37006] Core was generated by `./test_out'. Program terminated with signal SIGSEGV, Segmentation fault. #0 __memset_avx2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S:190 190 ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S: No such file or directory. (gdb) bt #0 __memset_avx2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S:190 #1 0x00005614d22c419f in main () at test.c:10 (gdb)
Note: after gdb executes BT (back trace to check the stack frame and hierarchical relationship of function call), the cause of print segment error memset and the position test c:10
gdb breakpoint debugging
Before starting breakpoint debugging, I have three questions. Let's start the following study with these three questions:
1. What is set breakpoint debugging?
2. What is the role of setting breakpoint debugging in actual development?
3. How to set breakpoint debugging?
-
1. What is set breakpoint debugging?
Pause by setting the code line, function name, conditional statement, specifying the entry position of the routine, and then analyze the variable value and stack information.
-
Single file breakpoint setting
command break line-number Make the program stop before a given line break function-name Causes the program to stop just before entering the given function break line or function if condition If the condition condition is true, the program will stop before reaching the specified line or function break routine Set a breakpoint at the entry of the specified routine -
Multi file breakpoint setting
command break file-name:line-number Make the program stop before a given line
-
-
2. What is the role of setting breakpoint debugging in actual development?
16 //================User incoming method====================== 17 void active_func() 18 { 19 printf("%s\n", __func__); 20 printf("go to home\n"); (gdb) 21 sleep(100); 22 } 23 24 void pthread_handler(void *value) 25 { 26 char *name = (char *)value; 27 28 if (0 == strcmp("cat", name)) 29 animal_active_handle(name, active_func); 30 (gdb) b 21 Breakpoint 1 at 0x12aa: file test.c, line 21. (gdb) run Starting program: /home/cjj/gdb-test/test_out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [New Thread 0x7ffff7d96700 (LWP 40777)] #########name=cat########## active_func go to home [New Thread 0x7ffff7595700 (LWP 40778)] [140737343215360]dog [New Thread 0x7ffff6d94700 (LWP 40779)] [Switching to Thread 0x7ffff7d96700 (LWP 40777)] Thread 2 "test_out" hit Breakpoint 1, active_func () at test.c:21 21 sleep(100); (gdb) bt #0 active_func () at test.c:21 #1 0x000055555555527b in animal_active_handle (name=0x555555556041 "cat", func=0x55555555528a <active_func>) at test.c:12 #2 0x00005555555552f9 in pthread_handler (value=0x555555556041) at test.c:29 #3 0x00007ffff7f94609 in start_thread (arg=<optimized out>) at pthread_create.c:477 #4 0x00007ffff7eb9163 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95 (gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x00005555555552aa in active_func at test.c:21 breakpoint already hit 1 time (gdb) info frame Stack level 0, frame at 0x7ffff7d95eb0: rip = 0x5555555552aa in active_func (test.c:21); saved rip = 0x55555555527b called by frame at 0x7ffff7d95ed0 source language c. Arglist at 0x7ffff7d95e98, args: Locals at 0x7ffff7d95e98, Previous frame's sp is 0x7ffff7d95eb0 Saved registers: rbp at 0x7ffff7d95ea0, rip at 0x7ffff7d95ea8 (gdb) info threads Id Target Id Frame 1 Thread 0x7ffff7d97740 (LWP 40773) "test_out" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:78 * 2 Thread 0x7ffff7d96700 (LWP 40777) "test_out" active_func () at test.c:21 3 Thread 0x7ffff7595700 (LWP 40778) "test_out" 0x00007ffff7e7726f in __GI___clock_nanosleep ( clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x7ffff7594e90, rem=rem@entry=0x7ffff7594e90) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:78 4 Thread 0x7ffff6d94700 (LWP 40779) "test_out" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:78 (gdb)
Note: when the contemporary code quantity is relatively large and the code call is relatively complex, you can add a breakpoint in a function, and then print the stack information of the function call according to the bt instruction. You can clearly know who called the function and analyze the call relationship of the function
-
3. How to set breakpoint debugging?
Common instructions for breakpoint debugging: r(run execution program), B (break trace set breakpoint), n(next single step debugging, run to the next breakpoint), p(print print variable value), l(list view source code), s(step single step debugging, enter into sub function), d(delete to delete breakpoint)
After setting the breakpoint, you can view the stack information and variable values at the breakpoint, and you can see the changes of variables and stack information step by step,