I need to find out what libraries a unix process has loaded and might use throughout it\'s lifetime. Is this possible and how. Or better yet, i have a library name and i nee
you can use lsof. See the man page for more info. Another tool is strace
. To see if a process is launched, you can use ps -ef
piped to grep
, or tools like pgrep
as well. check for the return value to know if its quit or not.
Solaris has pldd. For Linux you can call ldd on the executable or pmap on a running process or look into /proc/PID/maps
for mapped libraries.
On OS X, just need to set DYLD_PRINT_LIBRARIES
export DYLD_PRINT_LIBRARIES=1
./your_process
if lsof is not installed, you can simply cat /proc/$pid/maps
you can also check on disk executables with ldd to see what libs they will open (but that doesn't show libraries opened dynamically using dlopen()).
As for monitoring new processes, you can possibly add an inotify watch on /proc to monitor the creation/destruction of new numeric only directories.
Update: inotify on /proc doesn't work, but there are apparently alternatives, see this thread
I'm trying (and failing) to do this also. Look at mach_vm_read and vm_region_recurse_64. Closed-source applications like vmmap and Apple's Crash Reporter do this also using those methods, as well as open-source GDB. You might try looking there for an answer, but the source is challenging to read.
On Mac OS X you can use vmmap $pid
to get a list of mapped memory regions for a process. This does show all loaded libraries (at least it works for me here on 10.7.5).
ps -A
will give you a list of all processes, so ps -A | grep $APPNAME
will get you your process id $pid for use with vmmap $pid
. lsof -p $pid
also works.
The question seems to be asking for a dynamic method from C++. You could poll with these commands and analyse the results, although you may miss fast load/unload events.
lsof
is open source software under a BSD licence. Its source code no doubt provides some insight for how to do this from C/C++. See: http://en.wikipedia.org/wiki/Lsof