c - 如何使用gdb调试?

标签 c gdb

我正在尝试在我的程序中添加一个断点使用

b {line number}

但我总是收到一条错误消息:

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

我该怎么办?

最佳答案

这是 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;
}

-g3 option 编译. g3 包含额外信息,例如程序中存在的所有宏定义。

gcc -g3 -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 以获得所有有效命令的列表和描述。

关于c - 如何使用gdb调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2069367/

相关文章:

ios - 为什么我无法在调试器中打印该变量的值?

linux - 如何更改 debuginfo-install 存储库

c++ - C++ strtok跳过第二个标记或连续的定界符

c - 为什么我在 gdb 回溯中看不到行号?

c - 在内核中工作时包含错误

c - 嵌套在for循环中的fprintf不写入第一个元素

c - 为什么在 c 中使用 rand() 时每个子进程都生成相同的 "random"数字?

python - 使用 Numba 进行调试

c++ - 如何在 gdb 中将 void 指针转换为 unique_ptr<T>?

c - gdb 堆栈溢出