'Retrieving the memory map of its own process in OS X 10.5/10.6

In Linux, the easiest way to look at a process' memory map is looking at /proc/PID/maps, giving something like this:

08048000-08056000 r-xp 00000000 03:0c 64593      /usr/sbin/gpm
08056000-08058000 rw-p 0000d000 03:0c 64593      /usr/sbin/gpm
08058000-0805b000 rwxp 00000000 00:00 0
40000000-40013000 r-xp 00000000 03:0c 4165       /lib/ld-2.2.4.so
40013000-40015000 rw-p 00012000 03:0c 4165       /lib/ld-2.2.4.so
4001f000-40135000 r-xp 00000000 03:0c 45494      /lib/libc-2.2.4.so
40135000-4013e000 rw-p 00115000 03:0c 45494      /lib/libc-2.2.4.so
4013e000-40142000 rw-p 00000000 00:00 0
bffff000-c0000000 rwxp 00000000 00:00 0

How can a process get the equivalent information (address ranges, protection, mapped filename, etc...) about a process' own memory map under OSX 10.5 or 10.6?



Solution 1:[1]

Take a look at this thread from 2007 on the Darwin-kernel mailing list. In a nutshell, your choices are to popen vmmap (which is setgid appropriately) or use the Mach VM region APIs in /usr/include/mach/mach_vm.h. I found a decent example of using the Mach API in the Sage Mathematics System sources.

Solution 2:[2]

A couple more links for those looking for vmmap source (it's not published):

Getting the the mapped file name and the names of libraries from dyld_shared_cache: https://stackoverflow.com/a/17180619/1026

Solution 3:[3]

GNUlib (http://www.gnu.org/software/gnulib/) contains a function for iterating over all the virtual memory segments in most OSes including MAC OS X. It's in vma-iter.c

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben Stiglitz
Solution 2 Community
Solution 3 Dan Fego