澳门威尼斯人网址Linux之GDB调节和测量检验介绍与应用20170601

率先用一个简便的打印字符的次序来做下示范

一、GDB调节和测量试验命令

 

描述

backtrace(或bt)

翻开各级函数调用及参数

finish

三番两次运营到日前函数再次来到截至,然后停下来等待命令

frame(或f) 帧编号

慎选栈帧

info(或i) locals

查看当前栈帧局地变量的值

list(或l)

列出源代码,接着上次的职位往下列,每便列10行

list 行号

列出从第几行初步的源代码

list 函数名

列出某些函数的源代码

next(或n)

实践下一行语句

print(或p)

打字与印刷表达式的值,通过表明式能够修改换量的值大概调用函数

quit(或q)

退出gdb调解意况

set var

修改造量的值

start

伊始施行顺序,停在main函数第一行语句后面等待命令

step(或s)

实施下一行语句,尽管有函数调用则进入到函数中

举例还会有多少个指令如下

wath     观望八个变量

current 跳转到下个断点,或则跳转到观看点

quit  退出gdb调试

专程地,对于段错误的解决:

1.编写翻译的时候增加-g选项
2.在ARM开辟板上:
2.1.ulimit -c unlimited
2.2.实践应用程序:程序出错开上下班时间会在当前目录下生成名字为core的文件
3.在PC上
3.1. arm-linux-gdb App core文件所在目录/core
3.2.在gdb中输入:backtrace 就足以清楚在何地出错

注意:

1.比如输入bt未有结果,大概提示不能够运维,经常是平素不权力导致的,chmod 777
core就可以 

2.出现0x40000780 in ?? ()

warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
and all your stack frames look like this:
0x40000780 in ?? ()

貌似是找不到动态库的由来:
消除办法
首先输入info sharedlibrary 查询紧缺的动态库
接下来把动态库拷贝到PC端的当前目录
末段设置路线为当前目录 set solib-search-path .
要么应用别的措施设置

http://blog.chinaunix.net/uid-24922718-id-3489839.html
http://www.fayewilliams.com/2013/01/31/gdb-unable-to-find-dynamic-linker-breakpoint-function/
http://bbs.chinaunix.net/thread-3611739-1-1.html
//也许动态库要增添-g才行

 

 

安装GDB:

  1. sudo apt-get install gdb  

[email protected]:~/ctest/05gdb$
gdb simple //开始gdb调试
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html&gt;
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 “i686-linux-gnu”.
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/&gt;…
Reading symbols from /home/mickole/ctest/05gdb/simple…done.
(gdb) list //呈现源文件也可用l简写
1    #include <stdio.h>
2    #include <stdlib.h>
3   
4    long func(int n);
5   
6    int main(int argc, char *argv[])
7    {
8        printf(“Entering main …\n”);
9        int i;
10        for (i=0; i<argc; ++i)
(gdb) //回车键,继续上一条命令
11        {
12            printf(“%s “, argv[i]);
13        }
14        printf(“\n”);
15        long result = 0;
16        for (i=1; i<=100; ++i)
17        {
18            result += i;
19        }
20        printf(“result[1-100] = %ld\n”, result);
(gdb)
21        printf(“result[1-10] = %ld\n”, func(10));
22   
23        printf(“Exiting main …\n”);
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
(gdb)
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;
34        }
35   
36        return sum;
37    }
(gdb)
Line number 38 out of range; simple.c has 37 lines.

#include<stdio.h>
#include<string.h>
void main()
{
    int i=0;
    char a[4]=”abc”;
    for(i=0;i<strlen(a);i++)
    {
        printf(“%c”,a[i]);
    }
}

二、深入分析例子

代码如下:

  1. #include <stdio.h>  
  2.   
  3.   
  4. int add_range(int low, int high);  
  5.   
  6. int add (int a, int b);  
  7.   
  8. int add (int a, int b) {  
  9.     int result = a + b;  
  10.     return result;  
  11. }  
  12.   
  13. int add_range(int low, int high)  
  14. {  
  15.     int i = 0, sum = 0;  
  16.     for (i = low; i <= high; i++)  
  17.         sum = sum + i;  
  18.     return sum;  
  19. }  
  20.   
  21. int main(void)  
  22. {  
  23.     int result[100];  
  24.     result[0] = add_range(1, 10);  
  25.     result[1] = add_range(1, 100);  
  26.     printf(“result[0]=%d\nresult[1]=%d\n”, result[0], result[1]);  
  27.     int a = 9, b = 10;  
  28.         int result1 = a + b;  
  29.         int result2 = a * b;  
  30.         printf(“result1 = %d, result2 = %d”, result1, result2);  
  31.         int result3 = add(result1, result2) ;  
  32.         printf(“result3=%d”, result3);  
  33.         if (result3 == 10) {  
  34.            printf(“result3 = %d”, result3);  
  35.         } else {  
  36.            printf(“result != 10”);   
  37.     }  
  38.         return 0;  
  39. }  

1.在编写翻译时要增加-g选项,生成的可施行文件才具用gdb举行源码级调节和测量试验:

 命令如下:

  1. gcc -g main.c -o hello1  
  2. gdb hello1  

如下图

 

2.然后大家怎么查看源代码呢?命令如下

  1. list   

 

接下来大家还要看下边包车型客车代码能够怎么操作,你能够  回车 或则 输入l 
之后回车(因为list 简写 l)注意相当多gdb命令都足以简写成首字母,或则输入
list 2 回车也可以

 

大家退出gdb命令如下

  1. quit  

咱俩那边先不脱离

 

3.大家先运营程序用下边发号施令

  1. start  

如下图

 

4.大家用next命令奉行下一条语句,next命令简写为n,输入下民命令

  1. n  

 

5.我们也可是使用step命令步入函数内入追踪,简写命令s,再输入命令n推行下一步,输入上面发号施令

  1. s  

6.在函数中有两种查看情形的方式,backtrace命令(简写为bt)能够查阅函数调用的栈帧:

 

看得出当前的add_range函数是被main函数调用的,main传进来的参数是low=1, high=100main函数的栈帧编号为1,add_range的栈帧编号为0。

7.现行反革命得以用info命令(简写为i)查看函数局地变量的值:

i locals

add_range函数局地变量的值:

v:shapes=”_x0000_i1032″>

假如想查看main函数当前有的变量的值也足以达成,先用frame命令(简写为f)选用1号栈帧然后再查看局地变量:

(gdb) f 1

 

输入下边发号施令

  1. i locals  

8.大家还是能用p打印出当前值

 

9.大家也足以修改sum的值

  1. set var sum = 0  

v:shapes=”_x0000_i1036″>

10.举例大家毫不运营这么些函数了,要运营那么些函数下边包车型大巴顺序,我们得以用finish 

  1. finish   

 

11.quit     退出命令

v:shapes=”_x0000_i1038″>

(gdb) break 10 //设置断点也可用b
Breakpoint 1 at 0x8048459: file simple.c, line 10.
(gdb) info b //查看全数断点音信也可用i b替代
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10

要想用gdb调节和测量试验文件,有二种方式 1
gcc生成可进行的文件, gdb
间接调节和测量试验文件2
直接进去gdb
然后输入file
可施行文件

三、断点调节和测量试验

比喻剖判:

hello2.c源文件如下

  1. #include<stdio.h>  
  2.   
  3. int main() {   
  4.    int a = 1, b = 2, c = 3;  
  5.    int i;  
  6.    printf(“hello chenyu”);    
  7.    for (i = 0 ; i < 100; i++) {  
  8.         ++a;  
  9.         if (i > 90) {  
  10.             ++b;  
  11.         } else {  
  12.             ++c;  
  13.         }  
  14.    }   
  15.    printf(“a = %d, b = %d, c = %d”, a, b, c);  
  16.    int cc = 0;  
  17.    for (cc; cc < 9; cc++) {  
  18.         printf(“c %d”, i);  
  19.    }  
  20.    return 0;  
  21. }  

举例说大家在 ++b (第10行)那行和printf(“c %d”,
i)(第18行)这两行打断点示例:

1、编译

在编写翻译时要抬高-g选项,生成的可试行文件才干用gdb进行源码级调试

gcc -g hello2.c 

2、gdb调试

  1. gdb hello2  

3、展现代码行数命令如下

  1. list  

list(或l)

列出源代码,接着上次的位置往下列,每次列10行

list 行号

列出从第几行开始的源代码

list 函数名

列出某个函数的源代码

回车 往下查看

4 、然后输入打断点命令

1) 、源文件的某一行设置断点

  1. break 行号  

2)、二个一定的函数设置断点

  1. break 函数名  

3)、设置规范断点

  1. break 行号 if 条件 

示例:

  1. break 10  
  2.   
  3. break 18  

图片如下:

 

5 、 运维并且跳转到断点

1)、运行

  1. start  

2) 、跳转到断点命令(countine 简写c)

  1. c  

次第就到++b 那行来,上边2个步骤也得以用这几个run (简写 r)命令代码

  1. r  

 

我们那边代码会跳到那一个断点里面来,笔者么就幸免了手动循环这么多次,因为这些断点会打很频仍,所以大家可以用c命令,依然会在那行,所以打断点平日不要打在循环之中,当那一个代码实践完事后,大家再用c命令,就能打字与印刷18行的代码

   

6、看函数有的变量的值或单个变量的值能够用上面发号施令

  1. i locals  
  2.   
  3. p c  

例如想查看其余函数当前有的变量的值也足以完毕,先用frame命令(简写为f)选拔别的函数的栈帧号(backtrace命令(简写为bt)获取)然后再查看局地变量

f 1

i locals  

7、步向函数内部用上边发号施令

  1. s  

8、单步调节和测验往下实行用上边发号施令

  1. n  

9、删除断点

  1. delete 行号  

10、大家也能够用watch命令来察看变量,如若变量的值发生变化,程序就能够终止

  1. watch b

11、退出gdb命令

  1. quit  

(gdb) b func //也可在某些函数的入口地点设置断点
Breakpoint 2 at 0x804850b: file simple.c, line 29.
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10
2       breakpoint     keep y   0x0804850b in func at simple.c:29

方法一:

四、如何保存断点

有的时候运营时,上次打地铁断点没了,怎么保存呢?

1、查看断点消息并保留

  1. info b  

2 、保存断点

  1. save breakpoint fig.dp  

断点保存在fig.dp文件之中

3、调试时候读取保存断点文件

  1. gdb hello2 -x fig.dp  

要记住加上参数 -x 

下一场再去查看是或不是有断点

  1. info b  

 

可以看来是有2个断点的

 

原文:http://blog.csdn.net/u011068702/article/details/53925415

   http://blog.csdn.net/u011068702/article/details/53931648

(gdb) l 10 //查看钦命行源代码
5   
6    int main(int argc, char *argv[])
7    {
8        printf(“Entering main …\n”);
9        int i;
10        for (i=0; i<argc; ++i)
11        {
12            printf(“%s “, argv[i]);
13        }
14        printf(“\n”);
(gdb) l 29
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;

root@zhf-linux:/home/zhf/zhf/c_prj# gcc -g -o test1
test1.c

五、实例:

示例1:

book@book-desktop:/work/projects/test$ gcc -o LinkListClearOprNoHeadNode
-g LinkListClearOprNoHeadNode.c

book@book-desktop:/work/projects/test$ gcc -g -o
LinkListClearOprNoHeadNode LinkListClearOprNoHeadNode.c

book@book-desktop:/work/projects/test$ gdb LinkListClearOprNoHeadNode

GNU gdb (GDB) 7.0-ubuntu

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html&gt;

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 “i486-linux-gnu”.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/&gt;…

Reading symbols from
/work/projects/test/LinkListClearOprNoHeadNode…done.

(gdb) list

35      -Input                  :

36      -Output                 :

37      -Return                 :

38      * Modify Date     Version               
Author                   Modification

39      * ———————————————–

40      * 2017/03/29      V1.0.0                 Yu Weifeng      
Created

41     
******************************************************************************/

42      int main(int argc,char **argv)

43      {

44              PT_LinkList ptLinkListHead=NULL;

(gdb)

45              T_LinkListElement tLinkListElement;

46              tLinkListElement.cData=1;

47             
InsertNodeToLinkList(&ptLinkListHead,1,&tLinkListElement);

48              tLinkListElement.cData=23;

49             
InsertNodeToLinkList(&ptLinkListHead,2,&tLinkListElement);

50              tLinkListElement.cData=24;

51             
InsertNodeToLinkList(&ptLinkListHead,3,&tLinkListElement);

52              tLinkListElement.cData=26;

53             
InsertNodeToLinkList(&ptLinkListHead,4,&tLinkListElement);

54              tLinkListElement.cData=66;

(gdb)

55             
InsertNodeToLinkList(&ptLinkListHead,5,&tLinkListElement);

56              tLinkListElement.cData=99;

57     
        InsertNodeToLinkList(&ptLinkListHead,6,&tLinkListElement);

58

59              printf(“插入结果:”);

60              TraverseLinkList(ptLinkListHead);

61             
DeleteNodeFromLinkList(ptLinkListHead,6,&tLinkListElement);

62             
DeleteNodeFromLinkList(ptLinkListHead,4,&tLinkListElement);

63

64              printf(“删除结果:”);

(gdb)

65              TraverseLinkList(ptLinkListHead);

66

67              printf(“插入结果:”);

68              tLinkListElement.cData=5;

69             
InsertNodeToLinkList(&ptLinkListHead,3,&tLinkListElement);

70              TraverseLinkList(ptLinkListHead);

71              return 0;

72      }

73     
/*****************************************************************************

74      -Fuction                : GetLinkListLength

(gdb) info b

No breakpoints or watchpoints.

(gdb) break 47

Breakpoint 1 at 0x80484ca: file LinkListClearOprNoHeadNode.c, line 47.

(gdb) start

Temporary breakpoint 2 at 0x80484bd: file LinkListClearOprNoHeadNode.c,
line 44.

Starting program: /work/projects/test/LinkListClearOprNoHeadNode

 

Temporary breakpoint 2, main (argc=1, argv=0xbffff784) at
LinkListClearOprNoHeadNode.c:44

44              PT_LinkList ptLinkListHead=NULL;

(gdb) c

Continuing.

 

Breakpoint 1, main (argc=1, argv=0xbffff784) at
LinkListClearOprNoHeadNode.c:47

47             
InsertNodeToLinkList(&ptLinkListHead,1,&tLinkListElement);

(gdb) s

InsertNodeToLinkList (i_pptLinkListHead=0xbffff6c8, i_dwPosition=1,
i_ptElement=0xbffff6cf) at LinkListClearOprNoHeadNode.c:114

114             int ret=-1;

(gdb) i locals

ret = 134513528

ptLinkListNode = 0xbffff6a8

ptInsertListNode = 0x8049ff4

iLen = 2424820

(gdb) p iLen

$1 = 2424820

(gdb) n

115             T_LinkList *ptLinkListNode=*i_pptLinkListHead;

(gdb)

116             T_LinkList *ptInsertListNode=NULL;

(gdb)

117             int iLen=0;

(gdb)

118             iLen=GetLinkListLength(ptLinkListNode);

 

示例2:

book@book-desktop:/work/projects/test$ gdb LinkListClearOprNoHeadNode

GNU gdb (GDB) 7.0-ubuntu

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html&gt;

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 “i486-linux-gnu”.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/&gt;…

Reading symbols from
/work/projects/test/LinkListClearOprNoHeadNode…done.

(gdb) list InsertNodeToLinkList

108     * Modify Date     Version               
Author                   Modification

109     * ———————————————–

110     * 2017/03/29      V1.0.0                 Yu Weifeng      
Created

111    
******************************************************************************/

112     static int InsertNodeToLinkList(T_LinkList
**i_pptLinkListHead,int i_dwPosition,T_LinkListElement
*i_ptElement)

113     {

114             int ret=-1;

115             T_LinkList *ptLinkListNode=*i_pptLinkListHead;

116             T_LinkList *ptInsertListNode=NULL;

117             int iLen=0;

(gdb)

118             iLen=GetLinkListLength(ptLinkListNode);

119             if(i_dwPosition<1||i_dwPosition-1>iLen)

120             {

121                     ret=-1;

122                    
printf(“InsertPositionErr,Len:%d,Pos:%d\r\n”,iLen,i_dwPosition);

123             }

124             else

125             {

126                     ptInsertListNode=(T_LinkList
*)malloc(sizeof(T_LinkList));

127                     if(NULL==ptInsertListNode)

(gdb)

128                     {

129                            
printf(“pInsertListNodeMallocErr\r\n”);

130                             ret=-1;

131                     }

132                     else

133                     {

134                             ptInsertListNode->ptNext=NULL;

135                            
memcpy(&ptInsertListNode->tData,i_ptElement,sizeof(T_LinkListElement));

136                             if(NULL==*i_pptLinkListHead)

137                             {

(gdb)

138                                    
*i_pptLinkListHead=ptInsertListNode;

139                             }

140                             else

141                             {

142                                     while(–i_dwPosition)

143                                     {

144                                            
ptLinkListNode=ptLinkListNode->ptNext;

145                                     }

146                                    
ptInsertListNode->ptNext=ptLinkListNode;

147                                     ptLinkListNode=ptInsertListNode;

(gdb) info b

No breakpoints or watchpoints.

(gdb) break 144

Breakpoint 1 at 0x804873f: file LinkListClearOprNoHeadNode.c, line 144.

(gdb) start

Temporary breakpoint 2 at 0x80484bd: file LinkListClearOprNoHeadNode.c,
line 44.

Starting program: /work/projects/test/LinkListClearOprNoHeadNode

 

Temporary breakpoint 2, main (argc=1, argv=0xbffff784) at
LinkListClearOprNoHeadNode.c:44

44              PT_LinkList ptLinkListHead=NULL;

(gdb) c

Continuing.

ListNull

 

Breakpoint 1, InsertNodeToLinkList (i_pptLinkListHead=0xbffff6c8,
i_dwPosition=1, i_ptElement=0xbffff6cf) at
LinkListClearOprNoHeadNode.c:144

144                                            
ptLinkListNode=ptLinkListNode->ptNext;

(gdb) i locals

ret = -1

ptLinkListNode = 0x804b008

ptInsertListNode = 0x804b018

iLen = 1

 

(gdb) r //起首运维调节和测量试验run ,停在首先个断点处
Starting program: /home/mickole/ctest/05gdb/simple
Entering main …

root@zhf-linux:/home/zhf/zhf/c_prj# gdb test1

Breakpoint 1, main (argc=1, argv=0xbffff3f4) at simple.c:10
10        for (i=0; i<argc; ++i)
(gdb) step //单步调节和测量试验
12            printf(“%s “, argv[i]);
(gdb) s
10        for (i=0; i<argc; ++i)
(gdb)
14        printf(“\n”);
(gdb)
/home/mickole/ctest/05gdb/simple
15        long result = 0;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb)
18            result += i;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb) p i //打印i变量值print i
$1 = 1
(gdb) s
18            result += i;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb)
18            result += i;
(gdb) p i
$2 = 3

GNU gdb (Ubuntu
7.11.1-0ubuntu1~16.5) 7.11.1

(gdb) until //如若不想再持续循环可用until跳出循环,此处跳出for循环
20        printf(“result[1-100] = %ld\n”, result);
(gdb) l 20
15        long result = 0;
16        for (i=1; i<=100; ++i)
17        {
18            result += i;
19        }
20        printf(“result[1-100] = %ld\n”, result);
21        printf(“result[1-10] = %ld\n”, func(10));
22   
23        printf(“Exiting main …\n”);
24        return 0;

Copyright (C) 2016 Free
Software Foundation, Inc.

(gdb) p result //跳出循环后打字与印刷result值
$4 = 5050
(gdb) s //继续单步追踪
result[1-100] = 5050
21        printf(“result[1-10] = %ld\n”, func(10));
(gdb) c//continue继续追踪,间接跳到下贰个断点处
Continuing.

License GPLv3+: GNU GPL
version 3 or later
<http://gnu.org/licenses/gpl.html&gt;

Breakpoint 2, func (n=10) at simple.c:29
29        long sum = 0;
(gdb) l 29
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;
(gdb) s //单步追踪
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb) p sum
$5 = 6
(gdb) s
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb) finish //假诺想跳出函数,可用finish
Run till exit from #0  func (n=10) at simple.c:31
0x080484e1 in main (argc=1, argv=0xbffff3f4) at simple.c:21
21        printf(“result[1-10] = %ld\n”, func(10));
Value returned is $6 = 55
(gdb) s
result[1-10] = 55
23        printf(“Exiting main …\n”);
(gdb) s
Exiting main …
24        return 0;
(gdb) c //继续追踪,直至平常停止
Continuing.
[Inferior 1 (process 4450) exited normally]

This is free software: you
are free to change and redistribute it.

(gdb) quit //退出调节和测量检验
[email protected]:~/ctest/05gdb$

There is NO WARRANTY, to
the extent permitted by law. Type “show copying”

以下是单个命令深入分析:

and “show warranty” for
details.

run: 最初调和,后边可接程序运维所需参数,如:

This GDB was configured as
“i686-linux-gnu”.

[email protected]:~/ctest/05gdb$
gdb simple
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html&gt;
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 “i686-linux-gnu”.
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/&gt;…
Reading symbols from /home/mickole/ctest/05gdb/simple…done.
(gdb) r a b c
Starting program: /home/mickole/ctest/05gdb/simple a b c
Entering main …
/home/mickole/ctest/05gdb/simple a b c
result[1-100] = 5050
result[1-10] = 55
Exiting main …
[Inferior 1 (process 4476) exited normally]
(gdb)

Type “show configuration”
for configuration details.

 

For bug reporting
instructions, please see:

list: 列出源代码:

<http://www.gnu.org/software/gdb/bugs/&gt;.

list 默许列出前十行

Find the GDB manual and
other documentation resources online at:

list 10 列出钦赐行

<http://www.gnu.org/software/gdb/documentation/&gt;.

list 2,10 列出2到10行

For help, type
“help”.

list ,15 列出第15行包蕴第15行前边十行,如:

Type “apropos word” to
search for commands related to “word”…

(gdb) l ,15
6    int main(int argc, char *argv[])
7    {
8        printf(“Entering main …\n”);
9        int i;
10        for (i=0; i<argc; ++i)
11        {
12            printf(“%s “, argv[i]);
13        }
14        printf(“\n”);
15        long result = 0;

Reading symbols from
test1…(no debugging symbols found)…done.

 

(gdb)
run

list main :列出钦命函数的前十行,如

Starting program:
/home/zhf/zhf/c_prj/test1

(gdb) l main
2    #include <stdlib.h>
3   
4    long func(int n);
5   
6    int main(int argc, char *argv[])
7    {
8        printf(“Entering main …\n”);
9        int i;
10        for (i=0; i<argc; ++i)
11        {

abc[Inferior 1 (process
3802) exited normally]

list hello.c:func  :查看钦命文件中的钦点函数源码

方法二:

 

root@zhf-linux:/home/zhf/zhf/c_prj#
gdb

设置断点和查看断点:

GNU gdb (Ubuntu
7.11.1-0ubuntu1~16.5) 7.11.1

break 行号

Copyright (C) 2016 Free
Software Foundation, Inc.

break fun

License GPLv3+: GNU GPL
version 3 or later
<http://gnu.org/licenses/gpl.html&gt;

break file:行号

This is free software: you
are free to change and redistribute it.

break file:fun

There is NO WARRANTY, to
the extent permitted by law. Type “show copying”

break if <condition> – 条件建立即先后停住。

and “show warranty” for
details.

info break(i b) – 查看断点

This GDB was configured as
“i686-linux-gnu”.

watch expr – 一量expr值发生更换,程序停住。

Type “show configuration”
for configuration details.

delete n – 删除断点,如:

For bug reporting
instructions, please see:

(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10
2       breakpoint     keep y   0x0804850b in func at simple.c:29
(gdb) d 1
(gdb) d 2

<http://www.gnu.org/software/gdb/bugs/&gt;.

 

Find the GDB manual and
other documentation resources online at:

单步调节和测验:

<http://www.gnu.org/software/gdb/documentation/&gt;.

continue(c) – 运营至下多个断点

For help, type
“help”.

step(s) – 单步追踪,踏向函数,类似于VC中的step in

Type “apropos word” to
search for commands related to “word”.

next(n) – 单步追踪,不进来函数,类似于VC中的step out

(gdb) file
test1

finish –
运维程序,直到当前函数达成重回。并打字与印刷函数再次来到时的旅社地址和再次回到值及参数值等音讯。

Reading symbols from
test1…(no debugging symbols found)…done.

until –
当厌烦了在一个循环体内单步追踪时,那一个命令能够运作程序直到退出循环体。

(gdb)
run

 

Starting program:
/home/zhf/zhf/c_prj/test1

gdb常用命令计算:

abc[Inferior 1 (process
3818) exited normally]

澳门威尼斯人网址 1

跻身gdb后,大家来看下有如何职能可供调节和测验。在程序调试中,断点是必须的。我们先来看下怎样设置断点。break命令就是用来安装断点用的。

 

Break
行号 运维到某行截至

现行采取gdb查看运转时数据:

break
函数名称 运转到某些函数甘休运作

print – 查看变量值

break
行号/函数名称 if
条件

ptype – 查看类型

地方的次第中咱们在第7行约等于for循环的地点做几个断点。disable
断点号能够告一段落断点。比方disable
1就是结束掉第三个断点

print array – 查看数组

(gdb) break
7

print arr1  -查看全部数组成分

Breakpoint 1 at 0x80484b7:
file test1.c, line 7.

print arr[2] -查看钦定数组成分

下一场实践run,程序试行到第7行的时候结束

print &arr1[2] -查看钦命下标成分地址

(gdb)
run

print &arr1 – 查看数组首地址

Starting program:
/home/zhf/zhf/c_prj/test1

print
*[email protected]

Breakpoint 1, main () at
test1.c:7

  • 翻看动态内部存款和储蓄器

7 char
a[4]=”abc”;

print x=5 – 改造运作时数据

步入断点后大家得以经过print命令来打字与印刷具体的变量,来查阅变量的值。当中$1代表的是第一次采用print命令。

 

(gdb) print
i

gdb调节和测量检验段错误:

$1 = 0

段错误是由于访谈违规地址而发生的荒谬。

(gdb) print
a[0]

  • 拜谒系统数据区,尤其是往系统爱戴的内部存款和储蓄器地址写多少。最常见正是给多个指南针以0地址
  • 内部存款和储蓄器越界(数组越界,变量类型不均等等) 访谈到不属于你的内部存款和储蓄器区域

$2 = 97
‘a’

示例:

要在断点处继续实施要求推行next命令,与此同期大家还是能安装display
变量来查阅每趟运维后变量的

[email protected]:~/ctest/06gdb$
gdb bugging
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html&gt;
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 “i686-linux-gnu”.
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/&gt;…
Reading symbols from /home/mickole/ctest/06gdb/bugging…done.
(gdb) r
//借使程序出现段错误,最佳的定点格局正是一贯运转,它会自动停在段错误处
Starting program: /home/mickole/ctest/06gdb/bugging

(gdb) display
i

Program received signal SIGSEGV, Segmentation fault.
0x080483c4 in segfault () at bugging.c:7
7        *p = 100;
(gdb) bt  //利用栈回溯
#0  0x080483c4 in segfault () at bugging.c:7
#1  0x080483d4 in main () at bugging.c:12
(gdb) l 7 //定位到第7行
2    #include <stdlib.h>
3   
4    void segfault()
5    {
6        int *p = NULL;
7        *p = 100;
8    }
9   
10    int main(void)
11    {
(gdb)

1: i =
0

 

(gdb)
next

应用core文件进行调度:

9
printf(“%c”,a[i]);

1、core文件

1: i =
0

在程序崩溃时,日常会生成三个文本叫core文件。core文件记录的是程序崩溃时的内部存款和储蓄器印象,并投入调节和测验新闻。

(gdb)
next

core文件生成的长河叫做core dump

7
for(i=0;i<strlen(a);i++)

2、设置生成core文件

1: i =
0

ulimit -c 查看core-dump状态便是不是会时有发生core文件,如:

(gdb)
next

[email protected]:~/ctest/06gdb$
ulimit -a
core file size          (blocks, -c) 0
//块大小为0,表示不会时有产生core文件
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7895
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
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) 7895
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[email protected]:~/ctest/06gdb$
ulimit -c
0
[email protected]:~/ctest/06gdb$
ulimit -c 1024 //设置core文件块大小
[email protected]:~/ctest/06gdb$
ulimit -c
1024

9
printf(“%c”,a[i]);

ulimit -c 数字(如:ulimit -c 1024)

1: i =
1

ulimit -c unlimited

(gdb)
next

3、gdb利用core文件调节和测验

7
for(i=0;i<strlen(a);i++)

gdb 文件名 core文件

1: i =
1

bt

(gdb)
next

 

9
printf(“%c”,a[i]);

示例:

1: i =
2

[email protected]:~/ctest/06gdb$
./bugging
Segmentation fault (core dumped)
[email protected]:~/ctest/06gdb$
ls
bugging  bugging.c  core  Makefile  reverse  reverse.c  simple  simple.c

即使不想单步的实行,能够运作continue命令运行到第四个断点处停止,若无第4个断点,则平昔运营到程序结尾

如上出现段错误,发生core文件,以往选择core文件进行调护医疗:

透过whatis
变量名能够查阅变量只怕函数的连串:

[email protected]:~/ctest/06gdb$
gdb bugging core
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html&gt;
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 “i686-linux-gnu”.
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/&gt;…
Reading symbols from /home/mickole/ctest/06gdb/bugging…done.
[New LWP 4582]

(gdb) whatis
i

warning: Can’t read pathname for load map: Input/output error.
Core was generated by `./bugging’.
Program terminated with signal 11, Segmentation fault.
#0  0x080483c4 in segfault () at bugging.c:7
7        *p = 100;
(gdb) bt
#0  0x080483c4 in segfault () at bugging.c:7
#1  0x080483d4 in main () at bugging.c:12
(gdb) l 7
2    #include <stdlib.h>
3   
4    void segfault()
5    {
6        int *p = NULL;
7        *p = 100;
8    }
9   
10    int main(void)
11    {
(gdb)

type =
int

http://www.bkjia.com/Linuxjc/443551.htmlwww.bkjia.comtruehttp://www.bkjia.com/Linuxjc/443551.htmlTechArticle[\[email protected]](http://www.bkjia.com/cdn-cgi/l/email-protection):~/ctest/05gdb$
gdb simple //开始gdb调试 GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1)
7.4-2012.04 Copyright (C) 2012 Free Software Foundation, Inc. License
GPLv3+…

在运作的时候还是能够实时的改换变量的值来决定程序照旧循环

(gdb) set var
i=0

(gdb) print
i

$7 = 0

另外还能安装观望窗口,当变量产生变化的时候,程序及时停下,并展现变化的值。命令是watch

这里设置当i>0的时候甘休,并呈现出old
value和new
value

(gdb) watch
i>0

Hardware watchpoint 5:
i>0

(gdb)
continue

Continuing.

Hardware watchpoint 3:
i

Old value =
0

New value =
1

Hardware watchpoint 5:
i>0

Old value =
0

New value =
1

0x080484ec in main () at
test1.c:7

7
for(i=0;i<strlen(a);i++)

其余还会有rwatch:当表达式值被读取的时候,就停止程序。awatch:用于当表达式的值被读或被写的时候,停止程序

在运行进程中还足以实时的查看源代码。采纳list命令。

(gdb)
list

4 {

5 int
i=0;

6 char
a[4]=”abc”;

7
for(i=0;i<strlen(a);i++)

8 {

9
printf(“%c”,a[i]);

10 }

11 }

(gdb) list
4,6

4 {

5 int
i=0;

6 char
a[4]=”abc”;

list <function>
列出有个别函数的代码

list
<first,last>列出first到last行的代码

list
<,last>列出当前行到last行的代码

list <filename,n>
列出文件的第n行代码

list
<filename,function>列出文件名函数名叫function的代码

相关文章