macos 通过正在运行的进程(unix)获取使用的库列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2184775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:43:49  来源:igfitidea点击:

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

macosunixprocess

提问by Alexander Cohen

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 need to find out what processes are using it, is this possible.

我需要找出 unix 进程已加载并可能在其整个生命周期中使用的库。这是否可能以及如何。或者更好的是,我有一个库名,我需要找出正在使用它的进程,这可能吗?

On the same note, is it possible to get notified some how when a unix process is launched and when it is quit. They would not be child processes of my process, i just need to know globally.

同样,是否可以在 unix 进程启动和退出时得到一些通知。它们不会是我的进程的子进程,我只需要全局了解。

Update:

更新:

I think I didn't give enough information. The unix i was talking about was MacOS X ( even though some say its not really completely unix ), and I was looking for a way to find the loaded libraries a process has and i need to do it in C/C++.

我想我没有提供足够的信息。我正在谈论的 unix 是 MacOS X(尽管有人说它并不完全是 unix ),我正在寻找一种方法来查找进程所具有的加载库,我需要在 C/C++ 中进行。

回答by Nikolai Fetissov

Solaris has pldd. For Linux you can call lddon the executable or pmapon a running process or look into /proc/PID/mapsfor mapped libraries.

Solaris 有pldd. 对于 Linux,您可以调用ldd可执行文件或pmap正在运行的进程或查看/proc/PID/maps映射库。

回答by ggiroux

if lsof is not installed, you can simply cat /proc/$pid/maps

如果 lsof 没有安装,你可以简单地 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()).

您还可以使用 ldd 检查磁盘可执行文件以查看它们将打开哪些库(但这不会显示使用 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.

至于监视新进程,您可以在 /proc 上添加一个 inotify 监视来监视新的仅数字目录的创建/销毁。

Update: inotify on /proc doesn't work, but there are apparently alternatives, see this thread

更新:/proc 上的 inotify 不起作用,但显然有替代方案,请参阅此线程

回答by Cong Ma

On OS X, just need to set DYLD_PRINT_LIBRARIES

在 OS X 上,只需要设置 DYLD_PRINT_LIBRARIES

export DYLD_PRINT_LIBRARIES=1
./your_process

回答by Ross Bencina

On Mac OS X you can use vmmap $pidto 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).

在 Mac OS X 上,您可以使用它vmmap $pid来获取进程的映射内存区域列表。这确实显示了所有加载的库(至少它在 10.7.5 上对我有用)。

ps -Awill give you a list of all processes, so ps -A | grep $APPNAMEwill get you your process id $pid for use with vmmap $pid. lsof -p $pidalso works.

ps -A将为您提供所有进程的列表,因此ps -A | grep $APPNAME将为您提供进程 ID $pid 以用于vmmap $pid. lsof -p $pid也有效。

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.

问题似乎是在要求 C++ 中的动态方法。您可以使用这些命令进行轮询并分析结果,尽管您可能会错过快速加载/卸载事件。

lsofis 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

lsof是 BSD 许可下的开源软件。它的源代码无疑为如何从 C/C++ 做到这一点提供了一些见解。请参阅:http: //en.wikipedia.org/wiki/Lsof

回答by ghostdog74

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 -efpiped to grep, or tools like pgrepas well. check for the return value to know if its quit or not.

你可以使用lsof。有关更多信息,请参阅手册页。另一个工具是strace. 要查看进程是否已启动,您可以使用ps -ef管道到grep或类似的工具pgrep。检查返回值以了解它是否退出。

回答by alaska.alex

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.

我也在尝试(但失败)这样做。查看 mach_vm_read 和 vm_region_recurse_64。vmmap 和 Apple 的 Crash Reporter 等闭源应用程序也使用这些方法以及开源 GDB 来执行此操作。您可能会尝试在那里寻找答案,但来源很难阅读。

回答by Christopher Dale Frazee

I do not have the specific answer that you are looking for, but I have something close, that will perhaps get you close to what you want. You can display the linked library of a specific binary (not process) by:

我没有你正在寻找的具体答案,但我有一些接近的东西,这可能会让你接近你想要的。您可以通过以下方式显示特定二进制文件(非进程)的链接库:

  1. install xcode https://developer.apple.com/xcode/
  2. execute: otool -L PATH_TO_BINARY
  1. 安装 xcode https://developer.apple.com/xcode/
  2. 执行:otool -L PATH_TO_BINARY

EXAMPLE:

例子:

chris$ otool -L /usr/local/bin/mtr
mtr:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 46.1.0)