有 Mac GUI 应用程序为更令人讨厌的命令行工具(通常包含在应用程序包中)提供前端。我想看看在这种 GUI 的幕后发生了什么。

如何“附加”到应用程序,监视它对命令行实用程序的调用并记录这些调用的文件名和命令行参数?

解决方案也可以是记录 Mac OS X 上所有应用程序执行情况的应用程序(过滤掉最常见的系统调用)。

GUI 前端示例:http://xact.sourceforge.net/(因为它是开源的,所以可以调试它,但 xACT 只是一个示例。假设我们只有一个现成的 *.app 来监控)。

更新: dtrace 可以监视 exec 调用并打印所调用命令的名称。这是解决方案的一半,另一半是获取其命令行参数。这还没有解决(直到有人确认他们有 dtrace 来做到这一点)。

最佳答案

DTrace可以胜任。根据我在该问题其他地方的评论中与 Joey Hagedorn 的讨论,可以改进 10.6 附带的脚本以使用合理数量的参数 (50+)。因为脚本有很多重复,我将在此处包含一个脚本,该脚本输出运行良好的 DTrace 脚本。这个最多可容纳50个论点;您可能希望通过更改 for 循环来扩展参数的数量。

#!/bin/bash

cat <<HEADER
#!/usr/sbin/dtrace -s
/*
 * newproc.d - snoop new processes as they are executed. DTrace OneLiner.
 *
 * This is a DTrace OneLiner from the DTraceToolkit.
 *
 * 15-May-2005  Brendan Gregg   Created this.
 */

/*
 * Updated to capture arguments in OS X. Unfortunately this isn't straight forward...
 */

#pragma D option quiet

this unsigned long long argv_ptr; /* Wide enough for 64 bit user procs */

proc:::exec-success
{
    print_pid[pid] = 1; /* This pid emerged from an exec, make a note of that. */
}

/*
 * The "this" variables are local to (all) of the following syscall::mmap:return probes,
 * and only those probes. They must be initialized before use in each new firing.
 */
syscall::mmap:return
{
    this->argc = 0; /* Disable argument collection until we notice an exec-success */
}

syscall::mmap:return
/ print_pid[pid] /
{
    print_pid[pid] = 0;

    this->is64Bit = curpsinfo->pr_dmodel == PR_MODEL_ILP32 ? 0 : 1;
    this->wordsize = this->is64Bit ? 8 : 4;

    this->argc = curpsinfo->pr_argc;
    this->argc = (this->argc < 0) ? 0 : this->argc; /* Safety */

    this->argv_ptr = curpsinfo->pr_argv;

    printf("%d %s ", pid, this->is64Bit ? "64b" : "32b");
}

HEADER

for ((i=0;i<50;++i)); do

cat <<REPEAT
syscall::mmap:return
/ this->argc /
{
    this->here_argv = copyin(this->argv_ptr, this->wordsize);
    this->arg = this->is64Bit ? *(unsigned long long*)(this->here_argv) : *(unsigned long*)(this->here_argv);
    printf("%s ", copyinstr(this->arg));
    this->argv_ptr += this->wordsize;
    this->argc--;
}

REPEAT
done

cat <<FOOTER
syscall::mmap:return
/ this->argv_ptr /
{
    printf("%s\n", this->argc > 0 ? "(...)" : "");
    this->argc = 0;
    this->argv_ptr = 0;
}
FOOTER

关于cocoa - 监视 Cocoa 应用程序以在 Mac OS X 上执行外部实用程序(例如 ffmpeg)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/459493/

10-14 13:21