问题
I wanted to debug printf function, so when I step inside the printf function (gdb debugger) it showed me this:
__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.
Please help me to understand this.
回答1:
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. 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. 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.
回答2:
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. For that, the usual "debug symbol" packages of your operating systems are optimal.
回答3:
As others have answered, GDB was unable to find the source file.
For the C runtime libraries, Linux distributions may provide a debuginfo RPM that you can install, which may allow GDB to view the files. 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. There is no explicit dependency, but glibc-debuginfo
package won't work unless it is matched with the same version of 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.
The directory
command tells GDB to prepend a prefix ahead of any source file path it is attempting to find. For example, if the source tree is actually located under the /tmp
, you could use:
(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. 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:
(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
.
来源:https://stackoverflow.com/questions/45662035/how-to-debug-standard-c-library-functions-like-printf