gpt4 book ai didi

shared-libraries - 跟踪对共享库的调用

转载 作者:行者123 更新时间:2023-12-04 03:04:05 25 4
gpt4 key购买 nike

我正在Linux下开发一个程序。

出于调试目的,我想跟踪从我的程序到某个(最好是共享)库的所有调用。 (我不想跟踪库内发生的调用。)

对于系统调用,有 strace。是否有任何工具可以跟踪对共享库的调用?

最佳答案

您正在寻找的工具名为 ltrace .它允许跟踪从程序到所有(或一组给定)库的任何调用。

例如,以下调用将列出对共享库加载的外部函数的任何调用:

$> ltrace ls /
__libc_start_main(0x4028c0, 2, 0x7fff1f4e72d8, 0x411e60 <unfinished ...>
strrchr("ls", '/') = nil
setlocale(LC_ALL, "") = "en_US.UTF-8"
bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"
textdomain("coreutils") = "coreutils"
__cxa_atexit(0x40a200, 0, 0, 0x736c6974756572) = 0
isatty(1) = 0
getenv("QUOTING_STYLE") = nil
getenv("COLUMNS") = nil
ioctl(1, 21523, 0x7fff1f4e6e80) = -1
getenv("TABSIZE") = nil
getopt_long(2, 0x7fff1, "abcdfghiklmnopqrstuvw:xABCDFGHI:"..., 0x413080, -1) = -1
...
+++ exited (status 0) +++

如果你想专注于一个特定的库,那么你应该使用 --library=pattern选项:

-l, --library library_pattern
Display only calls to functions implemented by libraries that match
library_pattern. Multiple library patterns can be specified with several
instances of this option. Syntax of library_pattern is described in
section FILTER EXPRESSIONS.

Note that while this option selects calls that might be directed to the
selected libraries, there's no actual guarantee that the call won't be
directed elsewhere due to e.g. LD_PRELOAD or simply dependency ordering.
If you want to make sure that symbols in given library are actually called,
use -x @library_pattern instead.


因此,例如,获取对 libselinux.so.1 的调用列表是这样完成的:
$ ltrace -l libselinux.so.1 ls /
ls->freecon(0, 0xffffffff, 0x7f78c4eee628, 0) = 0
bin dev media root sbin sys usr boot etc home lib lost+found proc run tmp
+++ exited (status 0) +++

只有一次调用函数 freecon()被取出这个运行。

关于shared-libraries - 跟踪对共享库的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com