Find Loaded Libraries for a Process
As part of packaging [aMSN](http://amsn-project.net/), it is useful to know which libraries we have loaded so that we can package them with the product when we ship it. For Mac OS X, we use MacPorts to manage external libraries that we ship. For example libpng and libjpeg. For 0.98 we will also be shipping parts of farsight2, gstreamer, and some of the gst-plugins-* collections. This is to enable voice chat within aMSN. It's quite hard to track dependencies from within gstreamer's code, so I decided to look into how to find which libraries were loaded into a process. The first solution I came up with was using Activity Monitor in OS X. If you inspect your process in activity monitor and choose the "open files + ports" option, it will show the list of files in there. This is quite a neat solution, but I wanted to be able to do this from the command line, so I could write a script to copy all the automatically when we ship. While taking a read of [Mac OS X Internals](http://www.osxbook.com/) by Amit Singh, I came across a short description of vmmap (p130). It sounded interesting, and so I dug a bit further in the [manpage](http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/vmmap.1.html). After a bit of tweaking (and awk revision), I came up with this bash function to list the loaded libs: loadedlibs() { if [ -z “$1” ]; then echo “usage: loadedlibs ()” return fi pid=“$1” prefix=“$2” if [ -n “$prefix” ]; then vmmap -w “$pid” | grep __IMPORT | grep “$prefix” | awk ‘{print substr($0,index($0,$7))}’ | grep -v __IMPORT else vmmap -w “$pid” | grep __IMPORT | awk ‘{print substr($0,index($0,$7))}’ | grep -v __IMPORT fi }













