5

I'd like to programmatically get a list of the shared libraries linked by my binary on Linux and Solaris. Right now I shell out to pmap (I can't use ldd on the binary because it won't include dlopen'd libraries, and I can't use pldd because it's Solaris only):

std::ostringstream cmd;
cmd << "/usr/bin/pmap " << getpid() << " | awk '{ print $NF }' | grep '\\.so' | sort -u";
FILE* p = popen(cmd.str().c_str(), "r");

This is a bit hackish but it works on both Solaris and Linux (the pmap output is slightly different but the desired info is always in the last column). Is there a way to get the same information without shelling out? That works on both platforms? I assume the /proc/$PID files are formatted differently between them but I don't know where the headers for helping parse those are usually located (if there's a common location at all?).

Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
  • Strictly speaking `dlopen`ed libraries aren't "linked", they're loaded at runtime. I assume you actually want a list of all the loaded shared libraries? – Mark B Oct 25 '11 at 15:50

1 Answers1

2

You can use the pmap 1234 command with 1234 being a process id.

From inside your program, the simpler way (Linux-specific) is to read and parse the /proc/self/maps file.

Try running

cat /proc/self/maps

under Linux: it will show you the memory mapping of the process running the cat command above.

And if you've got some precise pointer, you can use dladdr (a GNU/Linux or Glibc specific function) to get information about which dynamic library contains it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547