如何知道我的程序在运行时实际使用哪个共享库?

时间:2019-04-20 00:01:48

标签: c linux shared-libraries ld dynamic-linking

如何确定运行时程序正在使用的共享库的路径?

我将glibc 2.12作为运行在CentOS 6.10系统上的主要glibc,并且在 tibble::tribble( ~age, ~area, ~cluster, ~norm.to.area, ~sum.norm.to.area, "gw_25", "cingulate", "cluster_1", 0.03, 0.11, "gw_20", "cingulate", "cluster_1", 0.03, 0.11, "gw_18", "hippocampus", "cluster_1", 0.02, 0.11, "gw_25", "insula", "cluster_1", 0.01, 0.11, "gw_20", "motor", "cluster_1", 0.01, 0.11, "gw_22", "motor", "cluster_1", 0.01, 0.11, "gw_25", "motor", "cluster_1", 0.01, 0.11, "gw_14", "motor", "cluster_1", 0.01, 0.11, "gw_18", "motor", "cluster_1", 0.01, 0.11, "gw_19", "motor", "cluster_1", 0.01, 0.11, "gw_17", "motor", "cluster_1", 0.01, 0.11, "gw_20", "occipital", "cluster_1", 0.01, 0.11, "gw_17", "occipital", "cluster_1", 0.01, 0.11, "gw_18", "occipital", "cluster_1", 0.01, 0.11, "gw_19", "occipital", "cluster_1", 0.01, 0.11, "gw_22", "occipital", "cluster_1", 0.01, 0.11, "gw_14", "occipital", "cluster_1", 0.01, 0.11, "gw_22", "parietal", "cluster_1", 0, 0.11, "gw_25", "parietal", "cluster_1", 0, 0.11, "gw_17", "parietal", "cluster_1", 0, 0.11, "gw_19", "parietal", "cluster_1", 0, 0.11, "gw_20", "parietal", "cluster_1", 0, 0.11, "gw_20", "PFC", "cluster_1", 0.01, 0.11, "gw_22", "PFC", "cluster_1", 0.01, 0.11, "gw_25", "PFC", "cluster_1", 0.01, 0.11, "gw_18", "PFC", "cluster_1", 0.01, 0.11, "gw_19", "PFC", "cluster_1", 0.01, 0.11, "gw_17", "PFC", "cluster_1", 0.01, 0.11, "gw_22", "somatosensory", "cluster_1", 0.01, 0.11, "gw_20", "somatosensory", "cluster_1", 0.01, 0.11, "gw_25", "somatosensory", "cluster_1", 0.01, 0.11, "gw_18", "somatosensory", "cluster_1", 0.01, 0.11, "gw_19", "somatosensory", "cluster_1", 0.01, 0.11, "gw_25", "temporal", "cluster_1", 0.01, 0.11, "gw_19", "temporal", "cluster_1", 0.01, 0.11, "gw_20", "temporal", "cluster_1", 0.01, 0.11 ) 中也有installed glibc 2.14。

当我使用来检查我的可执行文件时

/opt/glibc-2.14

它提供了此信息

$ objdump -p ./myProgram

我的Dynamic Section: NEEDED libpthread.so.0 NEEDED libcurl.so.4 NEEDED libc.so.6 的值为LD_LIBRARY_PATH

是否可以查看我的程序在运行时实际上正在使用哪个/opt/glibc-2.14/lib库(也许带有库文件的路径)?

1 个答案:

答案 0 :(得分:4)

在Linux上:一种可能的方法是查看/proc/文件系统中的相应条目。例如,对于PID为X的程序,您可以在/proc/X/maps中找到类似于以下内容的信息:

...
7f34a73d6000-7f34a73f8000 r--p 00000000 08:03 18371015                   /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc-2.27.so
7f34a73f8000-7f34a7535000 r-xp 00022000 08:03 18371015                   /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc-2.27.so
...

它清楚地显示了我的libc(该程序使用的libc)在哪里。


Example(缺少一些错误处理!)以显示fopen的来源:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>

#define BSIZE 200

int main(void) {
    char buffer[BSIZE];
    int const pid = getpid();
    snprintf(buffer, BSIZE, "/proc/%d/maps", pid);
    FILE * const maps = fopen(buffer, "r");
    while (fgets(buffer, BSIZE, maps) != NULL) {
        unsigned long from, to;
        int const r = sscanf(buffer, "%lx-%lx", &from, &to);
        if (r != 2) {
            puts("!");
            continue;
        }
        if ((from <= (uintptr_t)&fopen) && ((uintptr_t)&fopen < to)) {
            char const * name = strchr(buffer, '/');
            if (name) {
                printf("%s", name);
            } else {
                puts("?");
            }
        }
    }
    fclose(maps);
}