GDB从零学习

从例子入手。

#include <stdio.h>
void my_itoa(int n,char s[])  
{  
    int i,j,sign;  
  
    if((sign=n)<0)    //记录符号  
        n=-n;         //使n成为正数  
    i=0;  
    do{  
        s[i++]=n%10+'0';    //取下一个数字  
    }while((n/=10)>0);      //循环相除  
  
    if(sign<0)  
        s[i++]='-';  
    s[i]='\0';  
    for(j=i-1;j>=0;j--)        //生成的数字是逆序的,所以要逆序输出  
        printf("%c",s[j]);  
}  
  
  
void main()  
{  
    int n;  
    char str[100];  
    my_itoa(-123,str);  
    printf("\n");  
    printf("%d\n",my_atoi("123"));  
    system("pause");  
}  

将上述文件保存到test.cpp文件,然后编译运行。

Peek 2018-04-16 17-25.gif

然后我们来调试一下,看看具体应该怎么调试。按照下面的方式来调试代码。

linan@linan-PC:~$ gdb test
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
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 test...done.
(gdb) l
2	
3	int func(int n)
4	{
5	    int sum=0,i;
6	    for(i=0; i<n; i++)
7	    {
8	        sum+=i;
9	    }
10	    return sum;
11	}
(gdb) 
12	
13	main()
14	{
15	    int i;
16	    long result = 0;
17	    for(i=1; i<=100; i++)
18	    {
19	        result += i;
20	    }
21	
(gdb) 
22	    printf("result[1-100] = %d \n", result );
23	    printf("result[1-250] = %d \n", func(250) );
24	}
(gdb) 
Line number 25 out of range; test.cpp has 24 lines.
(gdb) break 15
Breakpoint 1 at 0x706: file test.cpp, line 15.
(gdb) break func
Breakpoint 2 at 0x6d7: file test.cpp, line 5.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000000706 in main() at test.cpp:15
2       breakpoint     keep y   0x00000000000006d7 in func(int) at test.cpp:5
(gdb) run
Starting program: /home/linan/test 

Breakpoint 1, main () at test.cpp:16
16	    long result = 0;
(gdb) next
17	    for(i=1; i<=100; i++)
(gdb) next
19	        result += i;
(gdb) next
17	    for(i=1; i<=100; i++)
(gdb) continue
Continuing.
result[1-100] = 5050 

Breakpoint 2, func (n=250) at test.cpp:5
5	    int sum=0,i;
(gdb) p sum
$1 = 32767
(gdb) n
6	    for(i=0; i<n; i++)
(gdb) p sum
$2 = 0
(gdb) p i
$3 = -134225552
(gdb) print i
$4 = -134225552
(gdb) n
8	        sum+=i;
(gdb) print i
$5 = 0
(gdb) bt
#0  func (n=250) at test.cpp:8
#1  0x000055555555474c in main () at test.cpp:23
(gdb) help bt
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
Use of the 'no-filters' qualifier prohibits frame filters from executing
on this backtrace.

(gdb) bt
#0  func (n=250) at test.cpp:8
#1  0x000055555555474c in main () at test.cpp:23
(gdb) finish
Run till exit from #0  func (n=250) at test.cpp:8
0x000055555555474c in main () at test.cpp:23
23	    printf("result[1-250] = %d \n", func(250) );
Value returned is $6 = 31125
(gdb) c
Continuing.
result[1-250] = 31125 
[Inferior 1 (process 11480) exited normally]
(gdb) 

Peek 2018-04-16 17-39.gif

使用GDB之前

首先确保你的程序在编译时使用了-g参数。这样才能将相关信息插入到可执行程序中。这些相关信息对程序是没有帮助的,但是对调试有帮助。

使用GDB启动调试

启动GDB的方法有以下几种:

  • gdb program也就是你的执行文件,一般在当然目录下。

  • gdb core 用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。

  • gdb 如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动 attach 上去,并调试他。program应该在PATH环境变量中搜索得到。

使用GDB的新手注意事项

GDB调试的时候需要知道源码的文件在哪里,在上面的例子中,源码和运行程序在一起。但是实际中不可能这样。在GDB命令加入参数-d或者在运行时使用direcotry 命令来添加源码目录。

其他注意点:

    -symbols <file> 
    -s <file> 
    从指定文件中读取符号表。

    -se file 
    从指定文件中读取符号表信息,并把他用在可执行文件中。

    -core <file>
    -c <file> 
    调试时core dump的core文件。
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计