Sample a process on Mac OS X from a C/C++ program
Asked Answered
T

2

9

The Sample Process feature in Activity Monitor is quite a useful thing. However, I need to do the same thing (take samples) of a certain process from another running process (C/C++) or a command line.

Is there any way to do this? I have been googling for this since a few days without any luck.

Thrall answered 31/12, 2009 at 20:16 Comment(0)
Y
15

There is a command-line utility sample.

Example:

sample Safari -file /dev/stdout

It will get exactly the same output with Activity Monitor.

Yukoyukon answered 31/12, 2009 at 20:24 Comment(0)
N
2

There are some few commandsline application that come in handy: sample and top.

If you want to write your own program, you can use the sysctl system call to get such information. However, it's quite tedious.

I would recommend installing procfs file system (built with MacFUSE). This would create a new "directory" at /proc that contains a lot of useful information for each application (e.g. memory usage, cpu usage, locks, opened files, sockets, threads, etc). The site gives a sample of how it can be accessed. Then you can simply script your access to those files.

Northwest answered 31/12, 2009 at 20:27 Comment(1)
Thanks for your reply. I will take a look at the other options that you gave.Thrall

© 2022 - 2024 — McMap. All rights reserved.