加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使用gdb进行调试?

发布时间:2020-12-16 06:03:24 所属栏目:百科 来源:网络整理
导读:我试图在我的程序中添加一个断点 b {line number} 但是我总是发现错误: No symbol table is loaded. Use the "file" command. 我该怎么办? 解决方法 这是gdb的快速入门教程: /* test.c *//* Sample program to debug. */#include stdio.h#include stdlib.
我试图在我的程序中添加一个断点
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 ("%dn",c);
  return 0;
}

编译-g选项:

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 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:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读