【问题标题】:View/Print function code from within GDB从 GDB 中查看/打印函数代码
【发布时间】:2013-05-08 16:21:08
【问题描述】:

我正在尝试开发一个简单的基于文本的用户界面,它可以运行一些 gdb 命令。 我希望用户能够在代码的某个区域设置和中断/跟踪点并运行一些调试命令。

我想让用户输入需要调试的功能。然后我取这个函数名并打印出函数的源代码,然后让用户选择在哪一行代码设置断点/跟踪点。目前,使用反汇编命令我可以为用户打印出内存地址,但我想打印实际的源代码。

这可以在 gdb 中完成吗?

目前:

Dump of assembler code for function test_function:
   0x03800f70 <test_function+0>:  push   %ebp
   0x03800f71 <test_function+1>:  mov    %esp,%ebp
   0x03800f73 <test_function+3>:  sub    $0x48,%esp

我想要什么:

void main()
{
  printf("Hello World\n");
}

谢谢!

编辑: 我得到了这个:

(gdb) list myFunction
941     directory/directory_etc/sourcefile.c: No such file or directory.
        in directory/directory_etc/sourcefile.c

然后我尝试指定 linenum:

(gdb) list directory/directory_etc/sourcefile.c:941
936     in directory/directory_etc/sourcefile.c

所以行为类似于您所描述的,但“列表文件名:linenum”仍然不起作用

谢谢!

【问题讨论】:

标签: view printing gdb


【解决方案1】:

用途:

(gdb) list FUNCTION

详见list命令的在线帮助:

(gdb) help list
List specified function or line.
With no argument, lists ten more lines after or around previous listing.
"list -" lists the ten lines before a previous ten-line listing.
One argument specifies a line, and ten lines are listed around that line.
Two arguments with comma between specify starting and ending lines to list.
Lines can be specified in these ways:
  LINENUM, to list around that line in current file,
  FILE:LINENUM, to list around that line in that file,
  FUNCTION, to list around beginning of that function,
  FILE:FUNCTION, to distinguish among like-named static functions.
  *ADDRESS, to list around the line containing that address.
With two args if one is empty it stands for ten lines away from the other arg.

对于任何非玩具项目,您可能会遇到这样的情况:

$ gdb /bin/true
<...>
(gdb) start
<...>
(gdb) list printf
file: "/usr/include/bits/stdio2.h", line number: 104
file: "printf.c", line number: 29

其中列出了代码库中函数的多个定义。在上面的printf() 案例中,非重载的纯C 函数有两个定义。一个在stdio2.h 中定义。然后,您可以使用list FILE:LINENUM 表单指定要列出的内容:

(gdb) list printf.c:29
24  
25  /* Write formatted output to stdout from the format string FORMAT.  */
26  /* VARARGS1 */
27  int
28  __printf (const char *format, ...)
29  {
30    va_list arg;
31    int done;
32  
33    va_start (arg, format);

对于您看到的“sourcefile.c: No such file or directory”错误,您需要告诉 GDB 在哪里查找源代码。见GDB Manual: Source Path。显然,您需要实际拥有要在您的机器上列出的函数的源代码。

【讨论】:

  • 谢谢,当我在一个简单的文件 (HelloWorld.exe) 上运行这个命令时,它没有问题。但是,我正在尝试使用 gdb 来调试大型实时系统。我要查看的函数位于许多 c 文件之一中,这些文件被编译成一个 .elf 文件,当我在那里运行它时,我得到: (gdb) list FUNCTION 941 in directory/directory_etc/code.c
  • @user2342775,当 gdb 的 list 命令显示函数名称的多个定义时,我已经修改了我的答案。这是你看到的吗?
  • 我已经编辑了我的原始问题以使其更具可读性,谢谢!
  • 有没有办法问gdb函数定义在哪里?
  • @x-yuri,是的!尝试“(gdb)信息行MY_FUNCTION”。我在这里写了一些关于使用 GDB 帮助导航 Linux 内核代码的注释docs.google.com/document/d/… 要真正使用这些功能,您需要阅读 GDB 手册的“第 9 章检查源文件”。
【解决方案2】:

对于 gcc

在编译源代码时添加调试选项标志 -g:

gcc -g test.c

要测试,请使用:

gdb ./a.out
(gdb) list

要了解更多功能,请查看手册页:

man gcc
man gdb

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2014-02-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多