2010-01-15 133 views
10

我試圖使用如何使用gdb進行調試?

b {line number} 

我的程序添加一個斷點,但我總是得到一個錯誤,指出:

No symbol table is loaded. Use the "file" command. 

我該怎麼辦?

+1

http://www.yolinux.com/TUTORIALS/GDB-Commands.html這裏是一個很好的gdb命令表。你會發現你需要知道的關於gdb的一切。 – Phong 2010-01-15 03:59:21

回答

22

這裏gdb快速入門教程:

/* test.c */ 
/* Sample program to debug. */ 
#include <stdio.h> 
#include <stdlib.h> 

int 
main (int argc, char **argv) 
{ 
    if (argc != 3) 
    return 1; 
    int a = atoi (argv[1]); 
    int b = atoi (argv[2]); 
    int c = a + b; 
    printf ("%d\n", c); 
    return 0; 
} 

Co與-g選項mpile:

gcc -g -o test test.c 

負載的可執行文件,它現在包含調試符號,到GDB:

gdb --annotate=3 test.exe 

現在,你應該在gdb提示發現自己。在那裏你可以發出命令給gdb。 說你要打一個斷點線11,並通過執行步驟,打印局部變量的值 - 下列命令序列將幫助你做到這一點:

(gdb) break test.c:11 
Breakpoint 1 at 0x401329: file test.c, line 11. 
(gdb) set args 10 20 
(gdb) run 
Starting program: c:\Documents and Settings\VMathew\Desktop/test.exe 10 20 
[New thread 3824.0x8e8] 

Breakpoint 1, main (argc=3, argv=0x3d5a90) at test.c:11 
(gdb) n 
(gdb) print a 
$1 = 10 
(gdb) n 
(gdb) print b 
$2 = 20 
(gdb) n 
(gdb) print c 
$3 = 30 
(gdb) c 
Continuing. 
30 

Program exited normally. 
(gdb) 

總之,下面的命令都您需要開始使用gdb:

break file:lineno - sets a breakpoint in the file at lineno. 
set args - sets the command line arguments. 
run - executes the debugged program with the given command line arguments. 
next (n) and step (s) - step program and step program until it 
         reaches a different source line, respectively. 
print - prints a local variable 
bt - print backtrace of all stack frames 
c - continue execution. 

在(gdb)提示符處輸入help以獲取所有有效命令的列表和描述。

4

啓動gdb與可執行文件作爲參數,以便它知道要調試哪個程序:

gdb ./myprogram 

那麼你應該能夠設置斷點。例如:

b myfile.cpp:25 
b some_function 
+4

,不要忘記用調試信息編譯(gcc有「-g」參數)。 – wilhelmtell 2010-01-15 03:53:52

2

你需要告訴gdb進行可執行文件的名稱,或者當您運行GDB或使用該文件的命令:

$ gdb a.out 

(gdb) file a.out 
2

確保在編譯時使用了-g選項。

-1

您需要在程序編譯時使用-g或-ggdb選項。

例如,gcc -ggdb file_name.c ; gdb ./a.out