Getting a list of used libraries by a running process (unix)

后端 未结 7 464
梦如初夏
梦如初夏 2020-12-23 20:30

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

相关标签:
7条回答
  • 2020-12-23 20:56

    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.

    0 讨论(0)
  • 2020-12-23 21:00

    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.

    0 讨论(0)
  • 2020-12-23 21:05

    On OS X, just need to set DYLD_PRINT_LIBRARIES

    export DYLD_PRINT_LIBRARIES=1
    ./your_process
    
    0 讨论(0)
  • 2020-12-23 21:06

    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

    0 讨论(0)
  • 2020-12-23 21:08

    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.

    0 讨论(0)
  • 2020-12-23 21:11

    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

    0 讨论(0)
提交回复
热议问题