【问题标题】:Tracing calls to a shared library跟踪对共享库的调用
【发布时间】:2014-09-28 01:38:04
【问题描述】:

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

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

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

【问题讨论】:

  • 你到底是什么意思?事实上,strace 正在跟踪程序中的每个系统调用。包括由执行时动态加载的外部函数(共享库、插件)发出的函数。唯一可能的问题可能来自多进程程序。
  • 我不需要跟踪系统调用。我需要跟踪对共享库的所有调用。它与 strace 类似:strace 跟踪系统调用,但我想跟踪对库的调用(而不是系统调用)。
  • 好的,知道了。我给你写一个答案。不过,等着看ltrace

标签: shared-libraries trace strace


【解决方案1】:

您要查找的工具名为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()

【讨论】:

  • run_all_tests-&gt;raptor_new_uri_from_counted_string(0x87f8ad0, 0xf76b0958, 22, 0 &lt;unfinished ...&gt; - 如何摆脱“未完成”并打印所有函数参数?
  • 事实上,我不知道如何摆脱这个限制(似乎显示的参数数量总是4)......我首先虽然它是由于80列约束,然后我尝试了 -A 选项但没有成功。所以,我真的不知道。我的建议是在找到函数调用后使用调试器(例如 gdb)。
  • @porton 如果 ltrace 不知道函数它只是假设它有 4 个参数。您可以通过为库编写原型文件来教授 ltrace 新功能。 This article 有更多信息。
猜你喜欢
  • 2010-11-23
  • 2018-08-04
  • 2019-05-01
  • 2013-03-12
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多