从 GDB 中查看/打印功能代码

标签 view printing gdb

我正在尝试开发一个简单的基于文本的用户界面,它运行一些 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”仍然不起作用

谢谢!

最佳答案

使用:

(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 .显然,您实际上需要拥有要在计算机上列出的函数的源代码。

关于从 GDB 中查看/打印功能代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445505/

相关文章:

android - Android 应用程序的 Activity 和 View 设置 - 我应该为每个页面创建新 Activity 吗?

java - 尝试打印包含来自 JavaFx 的图像的节点时出现像素化结果

css - 从@import 加载的打印样式表不起作用?

c - 全局 SDL_Renderer* 在函数调用时更改

c# - View 和 Entity Framework 数据不对?

sqlite - sqlite View 的性能损失

java - 打印整个java项目的源代码

c - gdb:__kernel_vsyscall () 中始终停在 0xffffe410 处

emacs - 如何设置 Emacs gdb 以便在箭头上显示最近的命令?

asp.net-mvc-3 - 在 ASP .NET MVC 3 中过滤索引 View ?