Mac OS X:使用其 PID 获取任意运行应用程序的详细进程信息(特别是其启动参数) [英] Mac OS X: Getting detailed process information (specifically its launch arguments) for arbitrary running applications using its PID

查看:67
本文介绍了Mac OS X:使用其 PID 获取任意运行应用程序的详细进程信息(特别是其启动参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测特定应用程序的启动时间.

I am trying to detect when particular applications are launched.

目前我正在使用 NSWorkspace,注册did launch application"通知.我还使用 runningApplications 方法来获取我的应用程序启动时当前正在运行的应用程序.

Currently I am using NSWorkspace, registering for the "did launch application" notification. I also use the runningApplications method to get apps that are currently running when my app starts.

对于大多数应用程序,应用程序包的名称就足够了.我有一个已知应用程序"列表,我将其与通知中传递的应用程序名称进行交叉核对.

For most apps, the name of the app bundle is enough. I have a plist of "known apps" that I cross check with the name of that passed in the notification.

这很好用,直到您遇到一个应用程序,该应用程序充当使用命令行参数启动另一个应用程序的代理.

This works fine until you come across an app that acts as a proxy for launching another application using command line arguments.

示例:Mac 上新发布的 Portal 没有专用的应用程序包.Steam 可以创建一个快捷方式,它只不过是使用 -game 参数和 portal 作为参数启动 hl2_osx 应用程序.

Example: The newly released Portal on the Mac doesn't have a dedicated app bundle. Steam can create a shortcut, which serves as nothing more than to launch the hl2_osx app with the -game argument and portal as it's parameter.

由于更多基于 Source 的游戏将登陆 Mac,我想它们将使用相同的方法来启动,有效地运行带有 -game 参数的 hl2_osx 应用程序.

Since more Source based games are heading to the Mac, I imagine they'll use the same method to launch, effectively running the hl2_osx app with the -game argument.

是否有使用 Cocoa API 获取参数(及其参数)列表的好方法?

Is there a nice way to get a list of the arguments (and their parameters) using a Cocoa API?

NSProcessInfo 接近,提供了一个 `-arguments' 方法,但只提供它自己的进程的信息......

NSProcessInfo comes close, offering an `-arguments' method, but only provides information for its own process...

NSRunningApplication 提供使用 PID 获取任意应用程序信息的能力,但没有命令行参数...

NSRunningApplication offers the ability to get information about arbitrary apps using a PID, but no command line args...

有什么东西可以填补两者之间的空白吗?

Is there anything that fills the gap between the two?

我试图不走生成 NSTask 的路线来运行 ps -p [pid] 并解析输出......我更喜欢更高层次的东西.

I'm trying not to go down the route of spawning an NSTask to run ps -p [pid] and parsing the output... I'd prefer something more high level.

推荐答案

你可以使用 ps 使用的任何东西,尽管它不是基于可可的.根据 Singhps 基于 kvm 和 sysctl调用.倾注 source,相关电话似乎成为 kvm_openfiles, kvm_getprocskvm_getargv.要获取命令行参数,首先调用kvm_openfiles 访问内核内存空间,然后使用kvm_getprocs 获取内核进程信息,然后使用kvm_getargv>.

You could use whatever ps uses, though it isn't cocoa based. According to Singh, ps is based on kvm and sysctl calls. Pouring over the source, the pertinant calls seem to be kvm_openfiles, kvm_getprocs and kvm_getargv. To get the command line arguments, first call kvm_openfiles to get access to the kernel memory space, then use kvm_getprocs to get kernel process info, then kvm_getargv.

ps 中使用 sysctl 似乎与您的目标不太相关;它用于获取其他信息,例如组 ID 和父进程 ID.使用的特定 sysctl 名称是 {CTL_KERN, KERN_PROC, KERN_PROC_which, flags},其中 which 指定一个进程过滤器(例如 ALLPID)和 flags 是过滤器的参数(详细信息在 sysctl 手册页).

The use of sysctl in ps seems less relevant to your goal; it's used to get other information, such as the group ID and parent proces ID. The particular sysctl name used is {CTL_KERN, KERN_PROC, KERN_PROC_which, flags}, where which specifies a process filter (e.g. ALL, PID) and flags are arguments for the filter (the details are in the sysctl man page).

OS X 不支持 procfs,但 Singh 开发了一个基于 FUSE 的版本,在 GPLv2 下发布.如果您将它与您的应用程序捆绑在一起,您还必须在 GPLv2 下发布它.大多数 MacFUSE 是在 BSD 风格许可下发布的,因此它可以与您的应用一起分发,而无需将其开源(fusefs/fuse_nodehash.c 是在 Apple 的开源许可下发布的,但它也允许链接到闭源应用).

OS X doesn't have support procfs, but Singh developed a FUSE based version, released under GPLv2. If you bundle it with your application, you'll have to release it under GPLv2 as well. Most of MacFUSE is released under a BSD-style license, so it can be distributed with your app without making it open source (fusefs/fuse_nodehash.c is released under Apple's open source license, but it also allows linking to closed source apps).

问题在 OS X 中获取其他进程的 argvusing C" 应该有用,因为它有使用 kvm 和 sysctl 的示例代码.TN 2050无需轮询观察进程生命周期" 也可能对您有用.

The question "Get other process' argv in OS X using C" should be of use, as it has sample code using kvm and sysctl. TN 2050 "Observing Process Lifetimes Without Polling" may also be of use to you.

这篇关于Mac OS X:使用其 PID 获取任意运行应用程序的详细进程信息(特别是其启动参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆