例子:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    time_t time_stamp;
    struct tm info;

    info.tm_year = atoi(argv[1]) - 1900;
    info.tm_mon = 1 - 1;
    info.tm_mday = 22;
    info.tm_hour = 19;
    info.tm_min = 10;
    info.tm_sec = 1;

    printf("tm_isdst: %d\n", info.tm_isdst);
    time_stamp = mktime(&info);
    printf("tm_isdst: %d\n", info.tm_isdst);
    printf("uninitialized: %ld\n", time_stamp);
    
    info.tm_isdst = -1;
    time_stamp = mktime(&info);
    printf("negative: %ld\n", time_stamp);
    
    info.tm_isdst = 0;
    time_stamp = mktime(&info);
    printf("zero: %ld\n", time_stamp);
    
    info.tm_isdst = 1;
    time_stamp = mktime(&info);
    printf("positive: %ld\n", time_stamp);

    return 0;
}



编译程序:-g 选项

gcc -Wall -g test.c -o test

注意:-g 选项


基本命令

start: 开始运行

continue: 继续运行,用在被断点打断之后继续运行

next: 逐行执行

print: 打印变量

ptype: 打印变量的类型


调试时如何传入参数

启动 gdb 时传入

gdb --args test 1991

(gdb) show args
Argument list to give program being debugged when it is started is "1991".



启动 gdb 之后传入

(gdb) set args 1991
(gdb) show args
Argument list to give program being debugged when it is started is "1991".



查看源代码

list [filename:]<function>|<line>
list [test.c:]20 - test.c [16,25]
list [test.c:]10,20 - test.c [10,20]
list [test.c:]main - test.c mian() [1,10]



(gdb) list test.c:1,30
1       #include <stdio.h>
2       #include <time.h>
3       #include <stdlib.h>
4
5       int main (int argc, char *argv[])
6       {
7           time_t time_stamp;
8           struct tm info;
9
10          info.tm_year = atoi(argv[1]) - 1900;
11          info.tm_mon = 1 - 1;
12          info.tm_mday = 22;
13          info.tm_hour = 19;
14          info.tm_min = 10;
15          info.tm_sec = 1;
16
17          printf("tm_isdst: %d\n", info.tm_isdst);
18          time_stamp = mktime(&info);
19          printf("tm_isdst: %d\n", info.tm_isdst);
20          printf("uninitialized: %ld\n", time_stamp);
21          
22          info.tm_isdst = -1;
23          time_stamp = mktime(&info);
24          printf("negative: %ld\n", time_stamp);
25          
26          info.tm_isdst = 0;
27          time_stamp = mktime(&info);
28          printf("zero: %ld\n", time_stamp);
29          
30          info.tm_isdst = 1;



设置一次显示多少行

(gdb) show listsize
Number of source lines gdb will list by default is 10.
(gdb) set listsize 30
(gdb) show listsize
Number of source lines gdb will list by default is 30.


查看编译路径

需要注意的是,用 list 显示的是编译路径下的代码,并不是可执行文件中包含了源码。加 -g 之后可执行文件中会记录编译路径这些信息,list 时 gdb 去读编译路径下的源码,如果编译路径下的源码删除或者没有源码,那么 list 是显示不了代码的。

(gdb) info source
Current source file is main.c
Compilation directory is /data/xxx/xml-input
Located in /data/xxx/xml-input/main.c
Contains 75 lines.
Source language is c.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.



设置断点

break [filename:]<function>|<line>



查看断点:

info break



清除断点:

clear [filename:]<function>|<line>



(gdb) info break
No breakpoints or watchpoints.
(gdb) break 10
Breakpoint 1 at 0x4005a7: file test.c, line 10.
(gdb) info break
Num Type           Disp Enb Address            What
1   breakpoint     keep y   0x00000000004005a7 in main at test.c:10
(gdb) clear 1
No breakpoint at 1.
(gdb) clear 10
Deleted breakpoint 1 
(gdb) info break
No breakpoints or watchpoints.
(gdb) start
Breakpoint 2 at 0x4005a7: file test.c, line 10.
Starting program: /data/francishe/tmp/test 1991
main (argc=2, argv=0x7fff3909efe8) at test.c:10
10          info.tm_year = atoi(argv[1]) - 1900;