简体   繁体   English

从C / C ++程序在Mac OS X上对进程进行采样

[英]Sample a process on Mac OS X from a C/C++ program

The Sample Process feature in Activity Monitor is quite a useful thing. Activity Monitor中的Sample Process功能非常有用。 However, I need to do the same thing (take samples) of a certain process from another running process (C/C++) or a command line. 但是,我需要从另一个运行进程(C / C ++)或命令行对某个进程执行相同的操作(取样)。

Is there any way to do this? 有没有办法做到这一点? I have been googling for this since a few days without any luck. 几天以来我一直在谷歌搜索没有任何运气。

There is a command-line utility sample . 有一个命令行实用程序sample

Example: 例:

sample Safari -file /dev/stdout

It will get exactly the same output with Activity Monitor. 它将与Activity Monitor获得完全相同的输出。

There are some few commandsline application that come in handy: sample and top . 有一些命令行应用程序派上用场: sampletop

If you want to write your own program, you can use the sysctl system call to get such information. 如果要编写自己的程序,可以使用sysctl系统调用来获取此类信息。 However, it's quite tedious. 但是,这很乏味。

I would recommend installing procfs file system (built with MacFUSE). 我建议安装procfs文件系统 (使用MacFUSE构建)。 This would create a new "directory" at /proc that contains a lot of useful information for each application (eg memory usage, cpu usage, locks, opened files, sockets, threads, etc). 这将在/proc创建一个新的“目录”,其中包含每个应用程序的大量有用信息(例如内存使用,CPU使用,锁,打开的文件,套接字,线程等)。 The site gives a sample of how it can be accessed. 该网站提供了如何访问它的示例。 Then you can simply script your access to those files. 然后,您可以简单地编写对这些文件的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM