linux c of the gdb debugger

gdb Overview

gdb debugging tool is a program under a powerful UNIX GNU open source release. Perhaps you prefer the kind of graphical interface, like debugging VC, BCB IDE, etc., but if you are doing software in the UNIX platform, you will find the gdb debugger tool more than VC, BCB graphical debugger powerful functions. The so-called "inch a director, has a short foot" is the truth.

In general, gdb to help you complete the main function of the following four aspects:

1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
3、当程序被停住时,可以检查此时你的程序中所发生的事。
4、动态的改变你程序的执行环境。

From the above it seems, GDB debugging tool and general no different, basically accomplish these functions, but in the details, you will find this powerful GDB debugging tool, we may be more accustomed to the graphical debugging tool, but there are when debugging command line tool can not be completed but it has the function of graphical tools. Let us have it seems.

gdb debugging examples

Source gdb_test1.c

  1 #include<stdio.h>                                                                                      
  2 
  3 int func(int n)
  4 {
  5     int sum=0,i;
  6     for(i=1;i<=n;i++)
  7     {
  8         sum+=i;
  9     }
 10     return sum;
 11 }
 12 
 13 
 14 main()
 15 {
 16     int i;
 17     long result=0;
 18     for(i=1;i<=100;i++)
 19     {
 20         result+=i;
 21     }
 22 
 23     printf("result[1-100]=%d \n",result);
 24     printf("result[1-250]=%d \n",func(250));
 25 }

In general GDB debugger is mainly C / C ++ program. To debug C C ++ program /, first at compile time, we have to put debugging information to the executable file. Compiler (cc / gcc / g ++) is -g parameter can do it. Such as:

>g++ -g gdb_test1.c -o gdb_test1
>cc -g gdb_test1.c -o gdb_test1

Compiled executable file. Here we use

> g++ -g gdb_test1.c -o gdb_test1

Using gdb debugging:

root@f49:/home/fhj/mycode# gdb gdb_test1  <---------- 启动GDB
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 gdb_test1...done.
(gdb) l   <-------------------- l命令相当于list,从第一行开始例出原码。
3	int func(int n)
4	{
5		int sum=0,i;
6		for(i=1;i<=n;i++)
7		{
8			sum+=i;
9		}
10		return sum;
11	}
12	
(gdb)    <-------------------- 直接回车表示,重复上一次命令
13	
14	main()
15	{
16		int i;
17		long result=0;
18		for(i=1;i<=100;i++)
19		{
20			result+=i;
21		}
22	

(gdb) break 16   <-------------------- 设置断点,在源程序第16行处。
Breakpoint 1 at 0x40055c: file gdb_test1.c, line 16.
(gdb) break func  <-------------------- 设置断点,在函数func()入口处。
Breakpoint 2 at 0x40052d: file gdb_test1.c, line 5.
(gdb) info break   <-------------------- 查看断点信息。
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040055c in main() at gdb_test1.c:16
2       breakpoint     keep y   0x000000000040052d in func(int) at gdb_test1.c:5
(gdb) r  <--------------------- 运行程序,run命令简写
Starting program: /home/fhj/mycode/gdb_test1 

Breakpoint 1, main () at gdb_test1.c:17  <---------- 在断点处停住
17		long result=0;
(gdb) n  <--------------------- 单条语句执行,next命令简写。
18		for(i=1;i<=100;i++)
(gdb) n
20			result+=i;
(gdb) n
18		for(i=1;i<=100;i++)
(gdb) n
20			result+=i;
(gdb) c   <--------------------- 继续运行程序,continue命令简写(结束本次循环)
Continuing.
result[1-100]=5050  <----------程序输出。

Breakpoint 2, func (n=250) at gdb_test1.c:5
5		int sum=0,i;
(gdb) n
6		for(i=1;i<=n;i++)
(gdb) p i  <--------------------- 打印变量i的值,print命令简写。
$1 = 32767
(gdb) n
8			sum+=i;
(gdb) n
6		for(i=1;i<=n;i++)
(gdb) p sum
$2 = 1
(gdb) n
8			sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6		for(i=1;i<=n;i++)
(gdb) p sum
$4 = 3
(gdb) bt  <--------------------- 查看函数堆栈。
#0  func (n=250) at gdb_test1.c:6
#1  0x00000000004005a0 in main () at gdb_test1.c:24
(gdb) finish   <--------------------- 退出函数
Run till exit from #0  func (n=250) at gdb_test1.c:6
0x00000000004005a0 in main () at gdb_test1.c:24
24		printf("result[1-250]=%d \n",func(250));
Value returned is $5 = 31375

(gdb) c  <--------------------- 继续运行。
Continuing.
result[1-250]=31375   <----------程序输出
[Inferior 1 (process 2907) exited normally]  <--------程序退出,调试结束

(gdb) quit   <--------------------- 退出gdb。

Using gdb

  • Compile Debugging

In general GDB debugger is mainly C / C ++ program. To debug C C ++ program /, first at compile time, we have to put debugging information to the executable file. Compiler (cc / gcc / g ++) is -g parameter can do it. Such as:

> cc -g hello.c -o hello
> g++ -g hello.cpp -o hello

If no -g, you will not see the name of the function procedures, variable names, all of which are replaced by run-time memory address. When you use the -g debugging information is added, and after successfully compiled object code, let's look at how to use gdb debugging him.

  • start up
对C/C++程序的调试,需要在编译前就加上-g选项:
$g++ -g hello.cpp -o hello
       program也就是你的执行文件,一般在当前目录下。
$gdb <program> <core dump file>
$gdb program core.11127
       用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。
 $gdb <program> <PID>
 $gdb hello 11127
	如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去,并调试他。program应该在PATH环境变量中搜索得到。

gdb interactive command

After starting GDB, to enter the interactive mode, the program is completed by the following debugging command; Note that the high order usually used abbreviations, these abbreviations familiar with the debug command to improve efficiency;

run

  • run: abbreviated as r, whose role is to run the program, when a breakpoint is encountered, the program stops running at a breakpoint, waiting for the next user input commands.
  • continue (abbreviated c): proceed to the next breakpoint (or end of the run)
  • next :( n-abbreviated), single-step tracking program, when a function call is encountered, this function does not enter the body; main difference of this command is the same step, step encountered user-defined function, stepping into the function to run, and next directly call the function, it does not enter into the body of the function.
    STEP (abbreviated s): single step if the function call, the function proceeds; n different command, n is a function call does not enter
  • until: When you get tired during a single-step tracking loop body, this command can run the program until you exit the loop.
    -until + line number: Run to a line, not out of the loop for
  • finish: Run the program until completion of the current function returns, and printing stack return address and returns the function information value and argument values.
  • call function (parameter): caller visible functions, and passes the "Parameter", such as: call gdb_test (55)
  • quit: abbreviated as q, quit gdb

Set a breakpoint

  • BREAK n (abbreviated bn): setting a breakpoint at the n-th row
    (code path and can bring Code Name: b OAGUPDATE.cpp: 578)
  • b fn1 if a> b: setting a breakpoint condition b 13 if i = 3, line 13 if i = 3 is provided at a breakpoint
  • break func (break abbreviated b): setting a breakpoint in a function FUNC () at the inlet, such as: break cb_button
  • delete breakpoint number n: n-th delete breakpoint
  • disable breakpoint number n: n-th breakpoints pause
  • enable breakpoint number n: n-th open break
  • clear line number n: n-th row of clear breakpoints
  • info b (info breakpoints): case where breakpoint is set to display the current program
  • delete breakpoints: Clear all breakpoints:

View source

  • list: abbreviated as L, its role is listed in the program source code, the default display every 10 rows.
  • line number list: shows the current file "line number" as the center longitudinal line 10, such as: list 12 is
  • function name list: where the function will display "function" source code, such as: main list
  • list: no parameters, then the last of the list command, the content output below.

Print Expressions

  • print expression: abbreviated as p, where "expression" can be any valid expression currently being tested programs, such as C language program currently being debugged, then the "expression" can be any valid expression of the C language including digital, variable or even a function call.
  • print a: an integer of a value will be displayed
  • print ++ a: 1 a will increase in value, and displayed
  • print name: String name values ​​will be displayed
  • print gdb_test (22): integer 22 will call gdb_test () as a parameter
  • print gdb_test (a): a variable as an argument will invoke gdb_test () function
  • display expression: is useful in a single step operation, after a command to set the display expression, it will after each single step instruction, followed by the expression and the output value is set. Such as: display a
  • watch expression: setting a watchpoint, once monitored "expression" change value, GDB forcibly terminate the program being debugged. Such as: watch a
  • whatis: query variable or function
  • info function: Query function
  • Extended info locals: displays all variables of current stack page

Information query runs

  • where / bt: Stack list of currently running;
  • bt backtrace Displays the current call stack
  • up / down to change the depth of the stack display
  • set args parameter: specifies the parameters at runtime
  • show args: Check-set parameters
  • info program: to see whether the program is running, a process ID, the reason being suspended.

Split window

  • layout: a split window, you can view the code side, the side of the test:
  • layout src: source code display window
  • layout asm: Displays the disassemble window
  • layout regs: display the source / disassembly and CPU register window
  • layout split: display the source code and disassembly window
  • Ctrl + L: Refresh window

notes

Directly enter the interactive mode of action is a repeat instruction for stepping very convenient;

Reprinted from: https://blog.csdn.net/haoel/article/details/2879
https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/gdb.html

Guess you like

Origin blog.csdn.net/qq_35429629/article/details/90348792
Recommended