简体   繁体   English

如何调试像 printf 这样的标准 c 库函数?

[英]How to debug standard c library functions like printf?

I wanted to debug printf function, so when I step inside the printf function (gdb debugger) it showed me this:我想调试 printf 函数,所以当我进入 printf 函数(gdb 调试器)时,它向我展示了这个:

__printf (format=0x80484d0 " my name is Adam") at printf.c:28
28  printf.c: No such file or directory.

What is the meaning of this?这是什么意思?

And when I again started step then there are a lot more statements like this.当我再次开始 step 时,会有更多这样的陈述。

Please help me to understand this.请帮助我理解这一点。

I think it's pretty clear.我认为这很清楚。 There is a place where the gdb expects the source code to be, so download glibc 's source code and put it there.有一个gdb期望源代码的地方,所以下载glibc的源代码并将其放在那里。 I think the error message contains the full path.我认为错误消息包含完整路径。

If it's a linux distro it's fairly simple in fact because usually source packages are shipped too.如果它是一个 linux 发行版,它实际上相当简单,因为通常也会提供源包。 Otherwise you need to find the source code yourself, note that it MUST be exactly the same that was used to compile the c library components, not just the same version because distributors often make changes to the sources.否则你需要自己找到源代码,注意它必须与用于编译 c 库组件的完全相同,而不仅仅是相同的版本,因为分发者经常对源代码进行更改。

Well, for the debugger to show you the code that was compiled into the binaries you're using, you need the original code somewhere.好吧,为了让调试器向您展示编译成您正在使用的二进制文件的代码,您需要在某处使用原始代码。

You don't seem to have that, so your debugger can't find it.你似乎没有那个,所以你的调试器找不到它。

Notice that you usually do not want to debug the source code of your std library functions, but only the way they are being called.请注意,您通常不想调试 std 库函数的源代码,只想调试它们的调用方式。 For that, the usual "debug symbol" packages of your operating systems are optimal.为此,操作系统的常用“调试符号”包是最佳的。

As others have answered, GDB was unable to find the source file.正如其他人所回答的那样,GDB 无法找到源文件。

For the C runtime libraries, Linux distributions may provide a debuginfo RPM that you can install, which may allow GDB to view the files.对于 C 运行时库,Linux 发行版可能会提供您可以安装的debuginfo RPM,它可能允许 GDB 查看文件。 For example:例如:

 $ yum search glibc-debuginfo

... ...

 glibc-debuginfo.x86_64 : Debug information for package glibc glibc-debuginfo-common.x86_64 : Debug information for package glibc

... ...

The glibc package and the glibc-debuginfo are a matched pair. glibc包和glibc-debuginfo是一对匹配的。 There is no explicit dependency, but glibc-debuginfo package won't work unless it is matched with the same version of glibc .没有明确的依赖关系,但是glibc-debuginfo包将无法工作,除非它与相同版本的glibc匹配。

If you have the sources unpacked somewhere, but not where GDB is expecting them to be, you can attempt to use either the directory or the set substitute-path command to let GDB know where the sources are.如果您在某处解压了源代码,但不是 GDB 期望它们所在的位置,您可以尝试使用directoryset substitute-path命令让 GDB 知道源代码的位置。

The directory command tells GDB to prepend a prefix ahead of any source file path it is attempting to find. directory命令告诉 GDB 在它试图查找的任何源文件路径之前添加一个前缀。 For example, if the source tree is actually located under the /tmp , you could use:例如,如果源树实际上位于/tmp ,则可以使用:

(gdb) directory /tmp

The set substitute-path command is used to tell GDB to replace a matching prefix in a source file path with a different path prefix. set substitute-path命令用于告诉 GDB 将源文件路径中匹配的前缀替换为不同的路径前缀。 For example, if the compiled source file was in /build/path/source.c , but in debugging the source file is actually in /usr/home/alice/release-1.1/source.c , then you could use:例如,如果编译的源文件在/build/path/source.c ,但在调试时源文件实际上在/usr/home/alice/release-1.1/source.c ,那么您可以使用:

(gdb) set substitute-path /build/path /usr/home/alice/release-1.1

The command assumes that you are only specifying a complete path names, so it won't perform the substitution on /build/pathological/source.c .该命令假定您只指定了完整的路径名,因此它不会在/build/pathological/source.c上执行替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM