GDB debugging Guide

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 foundrepresents 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 -goptions:

gcc -g -o xxx xxx.c

1.3 gdb xxx into the debugger

  • b 行号或函数 Add a breakpoint

  • r Go to the next breakpoint

  • s Single-step tracking

  • n Single-step execution

  • pSee currently running program data such as: p athe value of a variable output format of the output can be provided: for example p/d athe decimal value of the output of a variable

  • p array@idx You can view the value of the array array idx in

  • Setting display, such as display athis will be after each output value of a variable debugging

  • x Syntax view the memory addresses: x /

  • lSee the original program code, l 9listed in annex source line 9 ( l 2,8listed data between rows 2-8), l funcare listed in the source code of the specified function attachments

  • p x=8 Change the value of the variable x in the debugging process, following the entry into force

  • jump 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 code

  • signal Generating an amount signal

  • return Forced return

  • call Forced to call

  • until(简写u) When a body of the loop, the loop is exited run

  • until +行号 Run a row to stop, not just out of the loop

  • finish After performing this function, and printing stack return address and return values ​​of function parameters and other information

  • skipSkip some code function or do not want to focus on a file in the step, as skip function addexpressed over the function to add, skip file step.cskip the file step.c, info skipview information skipped.

  • c Continue to jump to the next breakpoint

  • bt Stack View

  • whereSee what went wrong when an error, and btsimilar

  • info b View breakpoint situation

  • q drop out

  • ptype Output structure type

  • info registersDisplay register values, info all-registersshow all registers

  • info breakpoints You can view all endpoints that have been set

1.5 Advanced Command

1.5.1 set breakpoints

  1. info breakpoints View all breakpoints
  2. b 9Or b test.c:9line numbers to set breakpoints
  3. b func Set breakpoints function name
  4. b 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)
  5. rbreak print*For the beginning of all print functions are set breakpoints rbreak test.c:.on all functions set breakpoints test.c
  6. tbreak test.c:9Set temporary breakpoints that only take effect once the breakpoint
  7. ignore 1 30 Ignore front of a breakpoint, 30 times, 31 times from the start to take effect, save time
  8. watch a Observation of a value or expression changes when
  9. disable/enable num Disable / enable all / a breakpoint
  10. clearClear all breakpoints, used to clear a function, break a line, such as clear func,clear linenum
  11. delete Delete all breakpoints, including watchpoints, catchpoints, used to delete breakpoint whose number, such as delete bnum

1.5.2 View variables

  1. p 'test.c'::aVariable print a file, p 'main'::bprint a function defined variable
  2. p *p@10 Print content pointer, the length of the print after the @
  3. p *$.next Print the contents of the next node in the list linkNode
  4. p/x c Hexadecimal print content (x: hexadecimal, d: decimal, o: octal, t: binary, c: character format, f: floating point format)
  5. x addr View memory address value
  6. display 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

  1. First find the child process to be debugged: ps -ef | grep xxxor pidofprocess name
  2. Enter gdbmode, enterattach pid
  3. 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.confthe

kernel.yama.ptrace_scope = 1

Read:

kernel.yama.ptrace_scope = 0

2.2 follow-fork-mode mode 方法

  1. Enter gdbmode, input set follow-fork-mode mode(mode optional parent, child, parent or child represents the debugging process)
  2. 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 thread
  • set 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, onrepresent only the current thread debugging will continue, offsaid they did not shield any thread, all threads can be executed, stepsaid 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.conffile, 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_patternset 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_pidmake 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.cview 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:

  1. Modify the file /etc/yum.repos.d/CentOS-Debuginfo.repoin the enabledparameter, modify the value 1
  2. yum install nss-softokn-debuginfo --nogpgcheck
  3. debuginfo-install glibcIf the following questions arise: -bash: debuginfo-install: command not foundis installed yum-utils, use the command:yum install yum-utils
  4. 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

2 Linux Core Dump

3 GDB debugging tool


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.

Guess you like

Origin www.cnblogs.com/bakari/p/11571480.html