如何使用gdb进行调试?

问题描述:

我试图使用如何使用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

这里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以获取所有有效命令的列表和描述。

启动gdb与可执行文件作为参数,以便它知道要调试哪个程序:

gdb ./myprogram 

那么你应该能够设置断点。例如:

b myfile.cpp:25 
b some_function 
+4

,不要忘记用调试信息编译(gcc有“-g”参数)。 – wilhelmtell 2010-01-15 03:53:52

你需要告诉gdb进行可执行文件的名称,或者当您运行GDB或使用该文件的命令:

$ gdb a.out 

(gdb) file a.out 

确保在编译时使用了-g选项。

您需要在程序编译时使用-g或-ggdb选项。

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