strace 查看加载动态库的情况

strace 命令可以查看程序加载动态库的情况,打印程序尝试从哪些路径去加载动态库了。

例如一个叫 thread 的程序尝试加载动态库 libboost_thread.so.1.52.0,我们用

strace ./thread

可以看到下面内容:

execve("../bin/thread", ["../bin/thread"], [/* 27 vars */]) = 0
brk(0)                                  = 0x99d4000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=97443, ...}) = 0
mmap2(NULL, 97443, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f6e000
close(3)                                = 0
open("/lib/tls/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/i686/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/lib/tls/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/i686", 0xbfca499c)     = -1 ENOENT (No such file or directory)
open("/lib/tls/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/sse2", 0xbfca499c)     = -1 ENOENT (No such file or directory)
open("/lib/tls/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/lib/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/i686/sse2", 0xbfca499c)    = -1 ENOENT (No such file or directory)
open("/lib/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/i686", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/lib/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/sse2", 0xbfca499c)         = -1 ENOENT (No such file or directory)
open("/lib/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/tls/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls/i686/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls/i686", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/i686/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/i686", 0xbfca499c)     = -1 ENOENT (No such file or directory)
open("/usr/lib/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/sse2", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib", {st_mode=S_IFDIR|0755, st_size=98304, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f6d000
writev(2, [{"../bin/thread", 13}, {": ", 2}, {"error while loading shared libra"..., 36}, {": ", 2}, {"libboost_thread.so.1.52.0", 25}, {": ", 2}, {"cannot open shared object file", 30}, {": ", 2}, {"No such file or directory", 25}, {"\n", 1}], 10../bin/thread: error while loading shared libraries: libboost_thread.so.1.52.0: cannot open shared object file: No such file or directory
) = 138
exit_group(127)                         = ?


这个例子中加载动态库失败了,因为程序 thread 没有从存放
brk(0)                                  = 0x99d4000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=97443, ...}) = 0
mmap2(NULL, 97443, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f6e000
close(3)                                = 0
open("/lib/tls/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/i686/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/lib/tls/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/i686", 0xbfca499c)     = -1 ENOENT (No such file or directory)
open("/lib/tls/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/sse2", 0xbfca499c)     = -1 ENOENT (No such file or directory)
open("/lib/tls/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/tls", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/lib/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/i686/sse2", 0xbfca499c)    = -1 ENOENT (No such file or directory)
open("/lib/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/i686", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/lib/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib/sse2", 0xbfca499c)         = -1 ENOENT (No such file or directory)
open("/lib/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/tls/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls/i686/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls/i686", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/tls", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/i686/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/i686/sse2", 0xbfca499c) = -1 ENOENT (No such file or directory)
open("/usr/lib/i686/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/i686", 0xbfca499c)     = -1 ENOENT (No such file or directory)
open("/usr/lib/sse2/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/sse2", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/libboost_thread.so.1.52.0", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/usr/lib", {st_mode=S_IFDIR|0755, st_size=98304, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f6d000
writev(2, [{"../bin/thread", 13}, {": ", 2}, {"error while loading shared libra"..., 36}, {": ", 2}, {"libboost_thread.so.1.52.0", 25}, {": ", 2}, {"cannot open shared object file", 30}, {": ", 2}, {"No such file or directory", 25}, {"\n", 1}], 10../bin/thread: error while loading shared libraries: libboost_thread.so.1.52.0: cannot open shared object file: No such file or directory
) = 138
exit_group(127)                         = ?


这个例子中,程序加载动态库失败了,因为没有从存放 libboost_thread.so.1.52.0 的路径(这里我们存放的路径是 /usr/bin/boost/lib)。通过 strace 命令就可以看的很清楚。


你可能感兴趣的:(strace 查看加载动态库的情况)