[英]Using Valgrind with LD_PRELOAD
To run a program, I add an LD_PRELOAD
variable before the command to load a specific library for instrumentation.为了运行程序,我在命令之前添加了一个
LD_PRELOAD
变量来加载特定的库以进行检测。 So the command looks like所以命令看起来像
LD_PRELOAD=foo.so ./run_program
In order to use valgrind, If I run valgrind LD_PRELOAD=foo.so./run_program
, I get:为了使用 valgrind,如果我运行
valgrind LD_PRELOAD=foo.so./run_program
,我会得到:
valgrind: LD_PRELOAD=foo.so: No such file or directory
And if I run LD_PRELOAD=foo.so valgrind./run_program
, the loaded library doesn't come into effect.如果我运行
LD_PRELOAD=foo.so valgrind./run_program
,加载的库不会生效。 So, I would like to know how to attach valgrind to the program with the loaded library.所以,我想知道如何使用加载的库将 valgrind 附加到程序中。 Any thoughts on that?
对此有什么想法吗?
You need to set LD_PRELOAD before both Valgrind and the guest executable.您需要在 Valgrind 和来宾可执行文件之前设置 LD_PRELOAD。
Valgrind expects the first non-valgrind argument to be the application to be run (either a script or an ELF executable binary). Valgrind 期望第一个非 valgrind 参数是要运行的应用程序(脚本或 ELF 可执行二进制文件)。
For example, running the following script例如,运行以下脚本
#!/usr/bin/env ksh
print $LD_PRELOAD
$ LD_PRELOAD=/lib64/libc.so.6 valgrind -q./sh $ LD_PRELOAD=/lib64/libc.so.6 valgrind -q./sh
/lib64/libc.so.6 /lib64/libc.so.6
There is no way to "attach" Valgrind to a running executable.无法将 Valgrind “附加”到正在运行的可执行文件。 It does not use ptrace like debuggers (gdb, lldb and udb for instance).
它不像调试器那样使用 ptrace(例如 gdb、lldb 和 udb)。
More details on what is going on are here and the full thread有关正在发生的事情的更多详细信息和完整的线程
Finally, if you run valgrind with -v you should see something like this (there is a lot of output so I've trimmed a lot).最后,如果您使用 -v 运行 valgrind,您应该会看到类似这样的内容(有很多 output 所以我已经修剪了很多)。 The example below was run with
下面的例子是用
valgrind -d pwd valgrind -d 密码
export LD_PRELOAD=./myprint.so导出 LD_PRELOAD=./myprint.so
--18766:1: aspacem (0,4,5) /usr/lib64/valgrind/memcheck-amd64-linux
--18766:1: aspacem (1,49,3) /usr/bin/pwd
--18766:1: aspacem (2,66,3) /usr/lib64/ld-2.17.so
[memory map]
memcheck was exec'd by valgrind, and memcheck then mmap'd the guest (pwd) and ld.so, then it started executing the guest, which will start loading dependencies. memcheck 由 valgrind 执行,然后 memcheck 映射来宾(pwd)和 ld.so,然后它开始执行来宾,这将开始加载依赖项。
--18766:1: aspacem (0,4,5) /usr/lib64/valgrind/memcheck-amd64-linux
--18766:1: aspacem (1,49,4) /usr/bin/pwd
--18766:1: aspacem (2,66,4) /usr/lib64/ld-2.17.so
--18766:1: aspacem (4,162,5) /usr/lib64/valgrind/vgpreload_core-amd64-linux.so
--18766:1: aspacem (5,216,5) /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
--18766:1: aspacem (6,274,8) /home/paulf/scratch/valgrind/cachegrind/tests/myprint.so
--18766:1: aspacem (7,337,0) [free slot: size=17 next=0]
--18766:1: aspacem (8,358,5) /usr/lib64/libc-2.17.so
[more detailed memory map]
Here the guest has loaded the libs listed in LD_PRELOAD (myprint.so and also core and memcheck, which valgrind added to LD_PRELOAD before execing memcheck).在这里,来宾已经加载了 LD_PRELOAD 中列出的库(myprint.so 以及 core 和 memcheck,valgrind 在执行 memcheck 之前将其添加到 LD_PRELOAD)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.