如何使用 gdb 进行调试? [英] How to debug using gdb?
问题描述
我正在尝试在我的程序中添加断点
I am trying to add a breakpoint in my program using
b {line number}
但我总是收到一条错误消息:
but I am always getting an error that says:
No symbol table is loaded. Use the "file" command.
我该怎么办?
推荐答案
这里有一个gdb的快速入门教程:
Here is a quick start tutorial for 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
", c);
return 0;
}
使用 -g3
选项进行编译一个>.g3
包括额外的信息,例如程序中存在的所有宏定义.
Compile with the -g3
option. g3
includes extra information, such as all the macro definitions present in the program.
gcc -g3 -o test test.c
将现在包含调试符号的可执行文件加载到 gdb 中:
Load the executable, which now contain the debugging symbols, into gdb:
gdb --annotate=3 test.exe
现在您应该会在 gdb 提示符下找到自己.在那里你可以向 gdb 发出命令.假设您想在第 11 行放置一个断点并逐步执行,打印局部变量的值 - 以下命令序列将帮助您执行此操作:
Now you should find yourself at the gdb prompt. There you can issue commands to gdb. Say you like to place a breakpoint at line 11 and step through the execution, printing the values of the local variables - the following commands sequences will help you do this:
(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 SettingsVMathewDesktop/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 所需的全部内容:
In short, the following commands are all you need to get started using 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) 提示符下键入帮助以获取所有有效命令的列表和说明.
Type help at the (gdb) prompt to get a list and description of all valid commands.
这篇关于如何使用 gdb 进行调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!