61

I have two, related questions:

  • How can I see if a shared library is currently loaded? (i.e. system-wide, process agnostic)
  • How can I see all shared libraries loaded by a process?
Max
  • 611
  • 1
  • 5
  • 3

4 Answers4

70

You can do both with lsof. To see what processes have a library open or mapped do:

lsof /path/to/lib.so

and to see what files (including shared libraries) a process has open and/or mapped, do:

lsof -p <pid>
TomH
  • 3,094
  • 17
  • 16
  • That only helps if you know WHICH instance of a `.so` file is loaded. Is it possible to list all th e`.so` files actually used by an app to find the disk path to the one I need? – will Mar 26 '20 at 07:38
  • It's also better to run these commands with root rights (e.g. sudo) to aovid missing some processes – leokom Oct 31 '22 at 08:32
33

Another way to see what's loaded in a process is by looking at the /proc/PID/maps file. This shows everything mapped into your address space, including shared objects mapped in.

Rich Homolka
  • 31,057
  • 6
  • 55
  • 80
  • Worked fine on my embedded ARM platform. While the BusyBox implementation of `lsof` did not have the needed functionality. – Alex Che Sep 26 '18 at 07:53
10
sudo grep libcairo.so /proc/*/maps

is a nice way to explore all /proc/PID/maps mentioned by Rich at once. Sample output:

/proc/8390/maps:7f0a9afae000-7f0a9b0bc000 r-xp 00000000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8390/maps:7f0a9b0bc000-7f0a9b2bc000 ---p 0010e000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8390/maps:7f0a9b2bc000-7f0a9b2bf000 r--p 0010e000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8390/maps:7f0a9b2bf000-7f0a9b2c0000 rw-p 00111000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8466/maps:7f0a9afae000-7f0a9b0bc000 r-xp 00000000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8466/maps:7f0a9b0bc000-7f0a9b2bc000 ---p 0010e000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8466/maps:7f0a9b2bc000-7f0a9b2bf000 r--p 0010e000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
/proc/8466/maps:7f0a9b2bf000-7f0a9b2c0000 rw-p 00111000 fc:00 274690                     /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6

Further awk and bash-fu can refine the output further.

This method also shows libraries opened with dlopen, tested with this minimal setup hacked up with a sleep(1000) on Ubuntu 18.04.

8

You can run the next command by root and see a full list,

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

This is for users who don't have lsof.

Nobutarou
  • 91
  • 1
  • 2
  • 1
    Why do you want the user to try that? Please [edit](https://superuser.com/posts/1319475/edit) your answer to better understanding. – CaldeiraG May 03 '18 at 15:04