跟踪对共享库的调用 [英] Tracing calls to a shared library

查看:28
本文介绍了跟踪对共享库的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

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

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

$>跟踪 ls/__libc_start_main(0x4028c0, 2, 0x7fff1f4e72d8, 0x411e60 <未完成...>strrchr("ls", '/') = nilsetlocale(LC_ALL, "") = "en_US.UTF-8"bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"textdomain("coreutils") = "coreutils"__cxa_atexit(0x40a200, 0, 0, 0x736c6974756572) = 0isatty(1) = 0getenv("QUOTING_STYLE") = nilgetenv("COLUMNS") = nilioctl(1, 21523, 0x7fff1f4e6e80) = -1getenv("TABSIZE") = nilgetopt_long(2, 0x7fff1, "abcdfghiklmnopqrstuvw:xABCDFGHI:"..., 0x413080, -1) = -1...+++ 退出(状态 0)+++

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

<块引用>

-l, --library library_pattern仅显示对匹配的库实现的函数的调用图书馆模式.可以使用多个指定多个库模式此选项的实例.library_pattern 的语法在部分过滤器表达式.请注意,虽然此选项选择可能被定向到选定的库,并不能实际保证调用不会由于例如被定向到别处LD_PRELOAD 或简单的依赖顺序.如果你想确保给定库中的符号被实际调用,改用 -x @library_pattern .

例如,获取对 libselinux.so.1 的调用列表是这样完成的:

$ ltrace -l libselinux.so.1 ls/ls->freecon(0, 0xffffffff, 0x7f78c4eee628, 0) = 0bin dev media root sbin sys usr boot etc home lib lost+found proc run tmp+++ 退出(状态 0)+++

本次运行中只调用了一次freecon()函数.

I am developing a program under Linux.

For debugging purposes I want to trace all calls from my program to a certain (preferably shared) library. (I do not want to trace calls happening inside the library.)

For syscalls there is strace. Is there any instrument to trace calls to a shared library?

解决方案

The tool you are looking for is called ltrace. It allows to trace any call from the program to all (or a set of given) libraries.

For example, the following call will list any call to an external function loaded by a shared library:

$> 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) +++

If you want to focus on a particular library, then you should use the --library=pattern option:

-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.

So, for example, getting the list of calls to libselinux.so.1 is done like this:

$ 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) +++

Only one call to the function freecon() is taken out this run.

这篇关于跟踪对共享库的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆