How to check what shared libraries are loaded at run time for a given process?

59,409

Solution 1

Other people are on the right track. Here are a couple ways.

cat /proc/NNNN/maps | awk '{print $6}' | grep '\.so' | sort | uniq

Or, with strace:

strace CMD.... 2>&1 | grep -E '^open(at)?\(.*\.so'

Both of these assume that shared libraries have ".so" somewhere in their paths, but you can modify that. The first one gives fairly pretty output as just a list of libraries, one per line. The second one will keep on listing libraries as they are opened, so that's nice.

And of course lsof...

lsof -p NNNN | awk '{print $9}' | grep '\.so'

Solution 2

May be lsof - the swiss army knife of linux will help?

edit: to run, lsof -p <pid>, lists all sorts of useful information, for example, if the process is java, lists all the open jars - very cool...

Solution 3

Actually, you can do this in your code in the following way:

#include <link.h>

using UnknownStruct = struct unknown_struct {
   void*  pointers[3];
   struct unknown_struct* ptr;
};
using LinkMap = struct link_map;

auto* handle = dlopen(NULL, RTLD_NOW);
auto* p = reinterpret_cast<UnknownStruct*>(handle)->ptr;
auto* map = reinterpret_cast<LinkMap*>(p->ptr);

while (map) {
  std::cout << map->l_name << std::endl;
  // do something with |map| like with handle, returned by |dlopen()|.
  map = map->l_next;
}

The link_map structure contains at least the base address and the absolute file name. It's the structure that is actually returned by dlopen() with non-NULL first argument. For more details see here.

Solution 4

ltrace seems to be your friend.

From ltrace manual:

ltrace is a program that simply runs the specified command until it exits. It intercepts and records the dynamic library calls which are called by the executed process and the signals which are received by that process. It can also intercept and print the system calls exe‐ cuted by the program.

       Its use is very similar to strace(1).

Solution 5

On Linux, /proc/<processid>/maps contains a list of all the files mapped into memory, which I believe should include any loaded by dlopen().

Share:
59,409
BЈовић
Author by

BЈовић

Drink milk, it is healthier then oil! If you want to see how I see the Presenter first's implementation in Qt, take a look into my pet project.

Updated on November 28, 2020

Comments

  • BЈовић
    BЈовић over 3 years

    Is there a way to check which libraries is a running process using?

    To be more specific, if a program loads some shared libraries using dlopen, then readelf or ldd is not going to show it. Is it possible at all to get that information from a running process? If yes, how?

  • BЈовић
    BЈовић about 13 years
    lsof seams to be the solution. Can you add an example how to call lsof on a process that is already running?
  • hertzsprung
    hertzsprung about 9 years
    Also, strace -f is best when child process might be spawned
  • Iharob Al Asimi
    Iharob Al Asimi over 8 years
    You can use the /proc/self/maps path where self is a symlink to the current process.
  • Alexis Wilke
    Alexis Wilke over 8 years
    Ha! So ugly, but it works. Would there be any documentation about the so called "unknown_struct"?
  • joaerl
    joaerl almost 8 years
    Exists on Linux too. Really seems a lot easier than the other proposed commands.
  • pi3
    pi3 almost 8 years
    The same should work with dlinfo() called with RTLD_DI_LINKMAP (see "man dlinfo")
  • Daniele E. Domenichelli
    Daniele E. Domenichelli over 7 years
    This works for me without using the "unknown_struct" #include <link.h> #include <iostream> int main(int argc, char argv[]) { struct link_map map = reinterpret_cast<struct link_map*>(dlopen(NULL, RTLD_NOW)); map = map->l_next->l_next; while (map) { std::cout << map->l_name << std::endl; map = map->l_next; } }
  • Luke Yeager
    Luke Yeager over 4 years
    Some improvements for your strace snippet ... (1) Some systems use the openat() syscall instead of open(), (2) people probably want to see versioned DSOs in addition to the unversioned ones, and (3) bash4 syntax is pretty safe to recommend at this point. strace CMD ... |& grep '^open.*\.so'
  • Dietrich Epp
    Dietrich Epp over 4 years
    @LukeYeager: Not everyone uses Bash, so I like to keep bashisms out of shell snippets.
  • Mike H-R
    Mike H-R over 3 years
    nice answer, fyi, you need to escape the second open paren ( in your strace grep.
  • PersianGulf
    PersianGulf over 2 years
    strace can't show when you open a library via dlopen
  • Dietrich Epp
    Dietrich Epp over 2 years
    @PersianGulf: How does dlopen work, if not by opening the library?