Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get executable shared library list from C++?

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

like image 571
Joseph Garvin Avatar asked Nov 04 '22 12:11

Joseph Garvin


1 Answers

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.

like image 95
Basile Starynkevitch Avatar answered Nov 14 '22 11:11

Basile Starynkevitch