gdb 一步一步調試程序

例子:

#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;



參考

http://www.cnblogs.com/kzloser/archive/2012/09/21/2697185.html





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章