gdb调试命令【转】(GDB debugging command 【 turn 】)

转自:https://www.cnblogs.com/wuyuegb2312/archive/2013/03/29/2987025.html

本文主要参考自:http://www.cnblogs.com/zzx1045917067/archive/2012/12/26/2834310.html,进行了一点补充和编排;Core dump部分参考了:http://blog.ddup.us/?p=176。

  gdb是一个在UNIX环境下的命令行调试工具。

  如果需要使用gdb调试程序,请在gcc时加上-g选项。

  下面的命令部分是简化版,比如使用l代替list等等。

1.基本命令

1)进入GDB  #gdb test

  test是要调试的程序,由gcc test.c -g -o test生成。进入后提示符变为(gdb) 。

2)查看源码  (gdb) l

  源码会进行行号提示。

  如果需要查看在其他文件中定义的函数,在l后加上函数名即可定位到这个函数的定义及查看附近的其他源码。或者:使用断点或单步运行,到某个函数处使用s进入这个函数。

3)设置断点  (gdb) b 6

  这样会在运行到源码第6行时停止,可以查看变量的值、堆栈情况等;这个行号是gdb的行号。

 4)查看断点处情况  (gdb) info b

  可以键入”info b”来查看断点处情况,可以设置多个断点;

5)运行代码  (gdb) r

6)显示变量值  (gdb) p n

  在程序暂停时,键入”p 变量名”(print)即可;

  GDB在显示变量值时都会在对应值之前加上”$N”标记,它是当前变量值的引用标记,以后若想再次引用此变量,就可以直接写作”$N”,而无需写冗长的变量名;

7)观察变量  (gdb) watch n

 在某一循环处,往往希望能够观察一个变量的变化情况,这时就可以键入命令”watch”来观察变量的变化情况,GDB在”n”设置了观察点;

8)单步运行  (gdb) n

9)程序继续运行  (gdb) c

  使程序继续往下运行,直到再次遇到断点或程序结束;

10)退出GDB  (gdb) q

2.断点调试

命令格式                        例子                   作用

break + 设置断点的行号  break n      在n行处设置断点

tbreak + 行号或函数名  tbreak n/func    设置临时断点,到达后被自动删除

break + filename + 行号  break main.c:10  用于在指定文件对应行设置断点

break + <0x…>  break 0x3400a      用于在内存某一位置处暂停 

break + 行号 + if + 条件  break 10 if i==3   用于设置条件断点,在循环中使用非常方便 

info breakpoints/watchpoints [n]  info break  n表示断点号,查看断点/观察点的情况 

clear + 要清除的断点行号  clear 10    用于清除对应行的断点,要给出断点的行号,清除时GDB会给出提示

delete + 要清除的断点编号  delete 3    用于清除断点和自动显示的表达式的命令,要给出断点的编号,清除时GDB不会给出任何提示

disable/enable + 断点编号  disable 3    让所设断点暂时失效/使能,如果要让多个编号处的断点失效/使能,可将编号之间用空格隔开

awatch/watch + 变量  awatch/watch i    设置一个观察点,当变量被读出或写入时程序被暂停 

rwatch + 变量      rwatch i        设置一个观察点,当变量被读出时,程序被暂停 

catch                  设置捕捉点来补捉程序运行时的一些事件。如:载入共享库(动态链接库)或是C++的异常 

tcatch                  只设置一次捕捉点,当程序停住以后,应点被自动删除

3.数据命令

display +表达式  display a  用于显示表达式的值,每当程序运行到断点处都会显示表达式的值 

info display      用于显示当前所有要显示值的表达式的情况 

delete + display 编号  delete 3  用于删除一个要显示值的表达式,被删除的表达式将不被显示

disable/enable + display 编号  disable/enable 3  使一个要显示值的表达式暂时失效/使能 

undisplay + display 编号  undisplay 3  用于结束某个表达式值的显示

whatis + 变量  whatis i  显示某个表达式的数据类型

print(p) + 变量/表达式  p n  用于打印变量或表达式的值

set + 变量 = 变量值  set i = 3  改变程序中某个变量的值

  在使用print命令时,可以对变量按指定格式进行输出,其命令格式为print /变量名 + 格式

  其中常用的变量格式:x:十六进制;d:十进制;u:无符号数;o:八进制;c:字符格式;f:浮点数。

4.调试运行环境相关命令

set args  set args arg1 arg2  设置运行参数

show args  show args  参看运行参数

set width + 数目  set width 70  设置GDB的行宽

cd + 工作目录  cd ../  切换工作目录

run  r/run  程序开始执行

step(s)  s  进入式(会进入到所调用的子函数中)单步执行,进入函数的前提是,此函数被编译有debug信息

next(n)  n  非进入式(不会进入到所调用的子函数中)单步执行

finish  finish  一直运行到函数返回并打印函数返回时的堆栈地址和返回值及参数值等信息

until + 行数  u 3  运行到函数某一行 

continue(c)  c  执行到下一个断点或程序结束 

return <返回值>  return 5  改变程序流程,直接结束当前函数,并将指定值返回

call + 函数  call func  在当前位置执行所要运行的函数

5.堆栈相关命令

backtrace/bt  bt  用来打印栈帧指针,也可以在该命令后加上要打印的栈帧指针的个数,查看程序执行到此时,是经过哪些函数呼叫的程序,程序“调用堆栈”是当前函数之前的所有已调用函数的列表(包括当前函数)。每个函数及其变量都被分配了一个“帧”,最近调用的函数在 0 号帧中(“底部”帧)

frame  frame 1  用于打印指定栈帧

info reg  info reg  查看寄存器使用情况

info stack  info stack  查看堆栈使用情况

up/down  up/down  跳到上一层/下一层函数

6.跳转执行

jump  指定下一条语句的运行点。可以是文件的行号,可以是file:line格式,可以是+num这种偏移量格式。表式着下一条运行语句从哪里开始。相当于改变了PC寄存器内容,堆栈内容并没有改变,跨函数跳转容易发生错误。

7.信号命令

signal   signal SIGXXX   产生XXX信号,如SIGINT。一种速查Linux查询信号的方法:# kill -l

handle   在GDB中定义一个信号处理。信号可以以SIG开头或不以SIG开头,可以用定义一个要处理信号的范围(如:SIGIO-SIGKILL,表示处理从SIGIO信号到SIGKILL的信号,其中包括SIGIO,SIGIOT,SIGKILL三个信号),也可以使用关键字all来标明要处理所有的信号。一旦被调试的程序接收到信号,运行程序马上会被GDB停住,以供调试。其可以是以下几种关键字的一个或多个:  nostop/stop    当被调试的程序收到信号时,GDB不会停住程序的运行,但会打出消息告诉你收到这种信号/GDB会停住你的程序    print/noprint    当被调试的程序收到信号时,GDB会显示出一条信息/GDB不会告诉你收到信号的信息   pass   noignore     当被调试的程序收到信号时,GDB不处理信号。这表示,GDB会把这个信号交给被调试程序会处理。   nopass   ignore     当被调试的程序收到信号时,GDB不会让被调试程序来处理这个信号。   info signals   info handle     可以查看哪些信号被GDB处理,并且可以看到缺省的处理方式

  single命令和shell的kill命令不同,系统的kill命令发信号给被调试程序时,是由GDB截获的,而single命令所发出一信号则是直接发给被调试程序的。

8.运行Shell命令

  如(gdb)shell ls来运行ls。  

9.更多程序运行选项和调试

1、程序运行参数。   set args 可指定运行时参数。(如:set args 10 20 30 40 50)   show args 命令可以查看设置好的运行参数。 2、运行环境。   path 可设定程序的运行路径。   show paths 查看程序的运行路径。

  set environment varname [=value] 设置环境变量。如:set env USER=hchen 

  show environment [varname] 查看环境变量。 

3、工作目录。

  cd    相当于shell的cd命令。 

  pwd  显示当前的所在目录。 4、程序的输入输出。   info terminal 显示你程序用到的终端的模式。   使用重定向控制程序输出。如:run > outfile   tty命令可以指写输入输出的终端设备。如:tty /dev/ttyb

5、调试已运行的程序

两种方法:   (1)在UNIX下用ps查看正在运行的程序的PID(进程ID),然后用gdb PID格式挂接正在运行的程序。   (2)先用gdb 关联上源代码,并进行gdb,在gdb中用attach命令来挂接进程的PID。并用detach来取消挂接的进程。

6、暂停 / 恢复程序运行  当进程被gdb停住时,你可以使用info program 来查看程序的是否在运行,进程号,被暂停的原因。 在gdb中,我们可以有以下几种暂停方式:断点(BreakPoint)、观察点(WatchPoint)、捕捉点(CatchPoint)、信号(Signals)、线程停止(Thread Stops),如果要恢复程序运行,可以使用c或是continue命令。

7、线程(Thread Stops)

如果程序是多线程,可以定义断点是否在所有的线程上,或是在某个特定的线程。   break thread  break thread if …   linespec指定了断点设置在的源程序的行号。threadno指定了线程的ID,注意,这个ID是GDB分配的,可以通过“info threads”命令来查看正在运行程序中的线程信息。如果不指定thread 则表示断点设在所有线程上面。还可以为某线程指定断点条件。如:   (gdb) break frik.c:13 thread 28 if bartab > lim 当你的程序被GDB停住时,所有的运行线程都会被停住。这方便查看运行程序的总体情况。而在你恢复程序运行时,所有的线程也会被恢复运行。

10.调试core文件

Core Dump:Core的意思是内存,Dump的意思是扔出来,堆出来。开发和使用Unix程序时,有时程序莫名其妙的down了,却没有任何的提示(有时候会提示core dumped),这时候可以查看一下有没有形如core.进程号的文件生成,这个文件便是操作系统把程序down掉时的内存内容扔出来生成的, 它可以做为调试程序的参考

(1)生成Core文件

  #设置core大小为无限  ulimit -c unlimited  #设置文件大小为无限  ulimit unlimited  这些需要有root权限, 在ubuntu下每次重新打开中断都需要重新输入上面的第一条命令, 来设置core大小为无限

core文件生成路径:输入可执行文件运行命令的同一路径下。若系统生成的core文件不带其他任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。

1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。可通过以下命令修改此文件:echo “1” > /proc/sys/kernel/core_uses_pid

2)proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。可通过以下命令修改此文件:echo “/corefile/core-%e-%p-%t” > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳以下是参数列表:    %p – insert pid into filename 添加pid    %u – insert current uid into filename 添加当前uid    %g – insert current gid into filename 添加当前gid    %s – insert signal that caused the coredump into the filename 添加导致产生core的信号    %t – insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间    %h – insert hostname where the coredump happened into filename 添加主机名    %e – insert coredumping executable name into filename 添加命令名

(2)用gdb查看core文件

  发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行.  gdb [exec file] [core file]  如:  gdb ./test core

  或gdb ./a.out   core-file core.xxxx  gdb后, 用bt命令backtrace或where查看程序运行到哪里, 来定位core dump的文件->行.

  待调试的可执行文件,在编译的时候需要加-g,core文件才能正常显示出错信息

  1)gdb -core=core.xxxx  file ./a.out  bt  2)gdb -c core.xxxx  file ./a.out  bt

(3)用gdb实时观察某进程crash信息

  启动进程  gdb -p PID  c  运行进程至crash  gdb会显示crash信息  bt

作者:五岳出处:http://www.cnblogs.com/wuyuegb2312对于标题未标注为“转载”的文章均为原创,其版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

————————

转自:https://www.cnblogs.com/wuyuegb2312/archive/2013/03/29/2987025.html

This paper mainly refers to: http://www.cnblogs.com/zzx1045917067/archive/2012/12/26/2834310.html , a little supplement and arrangement are made; The core dump section refers to: http://blog.ddup.us/?p=176 。

GDB is a command line debugging tool in UNIX environment.

If you need to use the GDB debugger, add the – G option to GCC.

The following command part is a simplified version, such as using L instead of list, etc.

1. Basic commands

1) Enter GDB #gdb test

Test is the program to be debugged by GCC test C – G – O test generation. After entering, the prompt changes to (GDB).

2) View source code (GDB) l

The source code will prompt the line number.

If you need to view the functions defined in other files, add the function name after L to locate the definition of this function and view other source codes nearby. Or: use breakpoints or step-by-step run to a function and use s to enter the function.

3) Set breakpoint (GDB) B 6

In this way, it will stop when running to line 6 of the source code. You can view the value of variables, stack, etc; This line number is the line number of GDB.

4) check the breakpoint (GDB) info B

You can type “info B” to view the breakpoints, and you can set multiple breakpoints;

5) Run code (GDB) r

6) Display variable value (GDB) P n

When the program pauses, type “P variable name” (print);

When GDB displays the variable value, it will add “$n” mark before the corresponding value, which is the reference mark of the current variable value. If you want to refer to this variable again in the future, you can write “$n” directly without writing a lengthy variable name;

7) Observation variable (GDB) watch n

In a loop, you often want to observe the change of a variable. At this time, you can type the command “watch” to observe the change of the variable. GDB sets the observation point in “n”;

8) Single step operation (GDB) n

9) Program continue (GDB) C

Make the program continue to run until the breakpoint is encountered again or the program ends;

10) Exit GDB (GDB) Q

2. Breakpoint debugging

Command format} function

Break + set the line number of the breakpoint break n set the breakpoint at line n

Tbreak + line number or function name tbreak n / func sets a temporary breakpoint and is automatically deleted when it arrives

Break + filename + line number break main c: 10 is used to set a breakpoint on the corresponding line of the specified file

break + < 0x…& gt; Break 0x3400a is used to pause at a location in memory

Break + line number + if + conditional break 10 if I = = 3 is used to set conditional breakpoints, which is very convenient for use in loops

Info breakpoints / watchpoints [n] info break n indicates the breakpoint number. Check the breakpoint / observation point

Clear + breakpoint line number to be cleared clear 10 is used to clear the breakpoint of the corresponding line. To give the line number of the breakpoint, GDB will give a prompt when clearing

Delete + breakpoint number to be cleared delete 3 commands for clearing breakpoints and automatically displayed expressions. To give the breakpoint number, GDB will not give any prompt when clearing

Disable / enable + breakpoint number disable 3 temporarily invalidates / enables the set breakpoint. If you want to invalidate / enable breakpoints at multiple numbers, you can separate the numbers with spaces

Watch / watch + variable watch / watch I sets an observation point. When the variable is read or written, the program is suspended

Rwatch + variable rwatch I sets an observation point. When the variable is read out, the program is suspended

Catch sets capture points to catch some events when the program is running. For example, loading shared libraries (dynamic link libraries) or C + + exceptions

Tcatch only sets the capture point once. When the program stops, the point should be deleted automatically

3. Data command

Display + expression display a is used to display the value of the expression. The value of the expression will be displayed whenever the program runs to a breakpoint

Info display is used to display all current expressions to display values

Delete + display number delete 3 is used to delete an expression to display a value. The deleted expression will not be displayed

Disable / enable + display number disable / enable 3 temporarily invalidates / enables an expression to display a value

Undisplay + display number undisplay 3 is used to end the display of an expression value

Whatis + variable whatis I displays the data type of an expression

Print (P) + variable / expression P n is used to print the value of a variable or expression

Set + variable = variable value set I = 3 change the value of a variable in the program

When using the print command, you can output variables in the specified format. The command format is print / variable name + format

The commonly used variable format: X: hexadecimal; d: Decimal system; u: Unsigned number; o: Octal; c: Character format; f: Floating point number.

4. Commands related to commissioning operation environment

Set args set args arg1 arg2 set operating parameters

Show args show args see operating parameters

Set width + number set width 70 sets the row width of GDB

CD + working directory CD/ Switch working directory

The run R / run program starts executing

Step (s) s entry type (will enter into the called sub function) is executed in one step. The premise of entering the function is that the function is compiled with debug information

Next (n) n non intrusive (will not enter the called sub function) single step execution

Finish finish runs until the function returns and prints the stack address, return value, parameter value and other information when the function returns

Until + number of lines U 3 run to a line of the function

Continue (c) C executes to the next breakpoint or the end of the program

return < Return value & gt; Return 5 change the program flow, end the current function directly, and return the specified value

Call + functioncall func executes the function to be run at the current location

5. Stack related commands

Backtrace / BT BT is used to print stack frame pointers. You can also add the number of stack frame pointers to be printed after this command to see which functions call the program up to this time. The program “call stack” is a list of all called functions before the current function (including the current function). Each function and its variables are assigned a “frame”, and the most recently called function is in frame 0 (the “bottom” frame)

Frame frame 1 is used to print the specified stack frame

Info reg info reg view register usage

Info stack info stack view stack usage

Up / down up / down skip to the previous / next function

6. Jump execution

Jump specifies the run point of the next statement. It can be the line number of the file, file: line format or + num offset format. The table indicates where the next run statement starts. It is equivalent to changing the content of PC register, the content of stack does not change, and cross function jump is prone to errors.

7. Signal command

Signal signal sigxxx generates XXX signal, such as SIGINT. A method to quickly check Linux query signal: # kill – L

Handle defines a signal processing in GDB. Signals can start with sig or not. You can define a range of signals to be processed (for example, sigio-sigkill, which means processing signals from sigio signal to sigkill, including sigio, sigiot and sigkill). You can also use the keyword all to indicate all signals to be processed. Once the debugged program receives the signal, the running program will be stopped by GDB for debugging. It can be one or more of the following keywords: Nostop / stop when the debugged program receives a signal, GDB will not stop the operation of the program, but will send a message to tell you that GDB will stop your program after receiving this signal / print / noprintwhen the debugged program receives a signal, GDB will display a message / GDB will not tell you the information of the received signal. GDB will not process the signal when the debugged program receives the signal. This means that GDB will give this signal to the debugged program for processing. Nopass ignore when the debugged program receives a signal, GDB will not let the debugged program process the signal. Info signals , info handle , you can view which signals are processed by GDB and see the default processing method

The single command is different from the shell Kill Command. When the system Kill Command sends a signal to the debugged program, it is intercepted by GDB, while the single command sends a signal directly to the debugged program.

8. Run the shell command

Such as (GDB) shell ls to run ls.

9. More program running options and debugging

1. Program running parameters. Set args to specify runtime parameters. (for example, set args 10 20 30 40 50) the show args command can view the set operating parameters. 2. Operating environment. Path to set the running path of the program. Show paths view the running path of the program.

  set environment varname [=value] 设置环境变量。如:set env USER=hchen 

Show environment [Varname] view environment variables.

3. Working directory.

CD is equivalent to the CD command of the shell.

PWD displays the current directory. 4. Program input and output. Info terminal displays the mode of the terminal used by your program. Use redirection to control program output. For example: run & gt; The outfile TTY command can refer to a terminal device that writes input and output. For example: TTY / dev / ttyb

5. Debugging a running program

Two methods: (1) under UNIX, use PS to view the PID (process ID) of the running program, and then use GDB PID format to connect the running program. (2) first associate the source code with GDB and perform GDB. In GDB, attach the PID of the process with the attach command. And use detach to cancel the attached process.

6. Pause / resume the program. When the process is stopped by GDB, you can use info program to check whether the program is running, process number and the reason for the pause. In GDB, we can pause in the following ways: breakpoint, watchpoint, catch point, signals, thread stops. If you want to resume the program, you can use C or continue.

7. Thread stops

If the program is multithreaded, you can define whether the breakpoint is on all threads or on a specific thread.    break thread  break thread if … Linespec specifies the line number of the source program where the breakpoint is set. Threadno specifies the ID of the thread. Note that this ID is allocated by GDB. You can view the thread information in the running program through the “info threads” command. If thread is not specified, it means that the breakpoint is set on all threads. You can also specify breakpoint conditions for a thread. For example: (GDB) break frik c:13 thread 28 if bartab > Lim# when your program is stopped by GDB, all running threads will be stopped. This makes it easy to see the overall situation of the running program. When you resume the program, all threads will be resumed.

10. Debug core files

Core dump: core means memory, and dump means throw it out and pile it up. When developing and using UNIX programs, sometimes the program inexplicably goes down without any prompt (sometimes core dumped). At this time, you can check whether there is a core Process number file generation. This file is generated by throwing out the memory content when the operating system drops the program down. It can be used as a reference for debugging programs

(1) Generate core file

# set the core size to unlimited ulimit – C unlimited # set the file size to unlimited ulimit unlimited. These require root permission. You need to re-enter the first command above every time you reopen the interrupt under Ubuntu to set the core size to unlimited

Core file generation path: enter the executable file to run the command in the same path. If the core files generated by the system do not have any other extended names, they are all named core. The new core file generation will overwrite the original core file.

1)/proc/sys/kernel/core_ uses_ PID can control whether to add PID as an extension to the file name of the core file. The file content is 1, which means adding PID as the extension, and the generated core file format is core xxxx; If it is 0, the generated core file is named core. This file can be modified with the following command: echo “1” & gt/ proc/sys/kernel/core_ uses_ pid

2)proc/sys/kernel/core_ Pattern can control the storage location and file name format of core files. This file can be modified with the following command: echo “/ core file / core -% e -% p -% T” & gt; core_ Pattern, you can uniformly generate core files into the / corefile directory, The generated file name is core command name PID timestamp. The following is the parameter list:% p – insert PID into filename add PID% U – insert current uid into filename add current uid% G – insert current GID into filename add current GID% s – insert signal that caused the coredump into the filename add the signal that caused the core to generate% t- Insert UNIX time that the coredump occurred into filename UNIX time when adding the core file generation% H – insert hostname where the coredump occurred into filename add hostname% e – insert coredump executable name into filename add command name

(2) Viewing core files with GDB

After the core dump occurs, use GDB to view the contents of the core file to locate the line in the file that causes the core dump GDB [exec file] [core file] such as GDB/ test core

Or GDB/ a.out   core-file core. After XXXX GDB, use the BT command backtrace or where to check where the program runs to locate the core dump file – & gt; that ‘s ok.

When compiling the executable file to be debugged, you need to add – G to the core file to display the error message normally

1)gdb -core=core. xxxx  file ./ a.out  bt  2)gdb -c core. xxxx  file ./ a.out  bt

(3) Using GDB to observe the crash information of a process in real time

Start the process GDB – P PID c run the process to crash GDB will display crash information BT

Author: Wuyue source: http://www.cnblogs.com/wuyuegb2312 The articles whose titles are not marked as “reprint” are original, and their copyright belongs to the author. Reprint is welcome, but this statement must be retained without the consent of the author, and the original text connection must be given in an obvious position on the article page, otherwise the right to investigate legal responsibility is reserved.