0

根据手册页 /proc/pid/exe 是一个符号链接,其中包含已执行命令的实际路径。

当我在我的程序上运行 valgrind 时,我看到/proc/pid/exe指向/usr/lib64/valgrind/amd64-linux/memcheck

lnx-host> which valgrind
/usr/bin/valgrind

知道为什么 /proc/pid/exe 在我将其作为 valgrind 调用时指向 usr/lib64/valgrind/amd64-linux/memcheck 吗?

在我的代码中,我试图从 pid 中获取可执行文件名,在这种情况下希望看到 valgrind。

4

1 回答 1

2

memcheck是 Valgrind 使用的默认工具,除非您告诉它使用其他工具,例如callgrind.

用于--tool=<name>指定要调用的工具。

旁注:你/usr/bin/valgrind也是一个脚本,就像默认情况下一样?为什么不玩它来做你想要实现的目标?在我的系统上,首先调用/usr/bin/valgrind.bin相应的(后端)工具(/usr/lib/valgrind/memcheck-amd64-linux)。


相关输出strace

execve("/usr/bin/valgrind", ["valgrind", "./myprog"], [/* 35 vars */]) = 0
stat("/home/user/HEAD/myprog", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
execve("/usr/bin/valgrind.bin", ["/usr/bin/valgrind.bin", "./myprog"], [/* 39 vars */]) = 0
open("./myprog", O_RDONLY)              = 3
execve("/usr/lib/valgrind/memcheck-amd64-linux", ["/usr/bin/valgrind.bin", "./myprog"], [/* 40 vars */]) = 0
getcwd("/home/user/HEAD/myprog", 4095) = 25
open("./myprog", O_RDONLY)              = 3
stat("./myprog", {st_mode=S_IFREG|0755, st_size=1886240, ...}) = 0
readlink("/proc/self/fd/3", "/home/user/HEAD/myprog/myprog", 4096) = 31
readlink("/proc/self/fd/3", "/home/user/HEAD/myprog/myprog", 4096) = 31
open("./myprog", O_RDONLY)              = 3
write(1015, "./myprog", 8)              = 8
write(1016, "==23547== Command: ./myprog\n", 28==23547== Command: ./myprog
stat("/home/user/HEAD/myprog/myprog", {st_mode=S_IFREG|0755, st_size=1886240, ...}) = 0
open("/home/user/HEAD/myprog/myprog", O_RDONLY) = 3
stat("/home/user/HEAD/myprog/myprog", {st_mode=S_IFREG|0755, st_size=1886240, ...}) = 0
open("/home/user/HEAD/myprog/myprog", O_RDONLY) = 3
open("/home/user/HEAD/myprog/myprog", O_RDONLY) = 3
readlink("/proc/self/fd/3", "/home/user/HEAD/myprog/myprog", 4096) = 31
getcwd("/home/user/HEAD/myprog", 4096) = 25
lstat("/home/user/HEAD/myprog/myprog", {st_mode=S_IFREG|0755, st_size=1886240, ...}) = 0
open("/home/user/HEAD/myprog/datafile", O_RDONLY) = 3
access("/home/user/HEAD/myprog/datafile", F_OK) = 0
open("/home/user/HEAD/myprog/datafile", O_RDONLY) = 3
open("/home/user/HEAD/myprog/datafile", O_RDONLY) = 4

您会注意到,所有execve调用都不是指./myprogValgrind 包装脚本、二进制文件,然后是后端工具:

execve("/usr/bin/valgrind", ["valgrind", "./myprog"], [/* 35 vars */]) = 0
execve("/usr/bin/valgrind.bin", ["/usr/bin/valgrind.bin", "./myprog"], [/* 39 vars */]) = 0
execve("/usr/lib/valgrind/memcheck-amd64-linux", ["/usr/bin/valgrind.bin", "./myprog"], [/* 40 vars */]) = 0
于 2012-05-12T02:08:01.537 回答