如何从一开始就跟踪程序而不以root身份运行它

Pet*_*sey 11 macos dtrace

我正在编写一个调用DTrace来跟踪用户指定的程序的工具.

如果我的工具使用dtrace -c将程序作为DTrace的子进程运行,我不仅不能将任何参数传递给程序,而且程序运行时具有DTrace的所有权限 - 就像root(我正在使用) Mac OS X).这使得某些事情应该有效,并且显然会使很多事情变得不可行.

我知道的另一个解决方案是自己启动程序,通过发送它来暂停它,将其SIGSTOP传递给它dtrace -p,然后通过发送它继续它SIGCONT.问题是,当DTrace收集符号信息时,程序运行几秒钟而没有被跟踪,或者如果我在继续进程之前睡了几秒钟,DTrace会抱怨objc<pid>:<class>:<method>:entry没有探测器匹配.

有没有办法可以在用户的​​帐户下运行程序,而不是root用户,但仍然可以让DTrace从头开始跟踪它?

ale*_*nge 6

类似的东西sudo dtruss -f sudo -u <original username> <command>对我有用,但之后我感觉很糟糕.

我提交了一个关于它的雷达漏洞并将其作为#5108629的副本关闭.


geo*_*war 5

此脚本将您想要监控的可执行文件的名称(对于应用程序来说是 info.plist 的 CFBundleExecutable)作为参数传递给 DTrace(然后您可以在此脚本运行后启动目标应用程序):

string gTarget;     /* the name of the target executable */

dtrace:::BEGIN
{
    gTarget = $$1;  /* get the target execname from 1st DTrace parameter */

    /*
    * Note: DTrace's execname is limited to 15 characters so if $$1 has more
    * than 15 characters the simple string comparison "($$1 == execname)"
    * will fail. We work around this by copying the parameter passed in $$1
    * to gTarget and truncating that to 15 characters.
    */

    gTarget[15] = 0;        /* truncate to 15 bytes */
    gTargetPID = -1;        /* invalidate target pid */
}

/*
* capture target launch (success)
*/
proc:::exec-success
/
    gTarget == execname
/
{
    gTargetPID = pid;
}

/*
*   detect when our target exits
*/
syscall::*exit:entry
/
    pid == gTargetPID
/
{
    gTargetPID = -1;        /* invalidate target pid */
}

/*
* capture open arguments
*/
syscall::open*:entry
/
    ((pid == gTargetPID) || progenyof(gTargetPID))
/
{
    self->arg0 = arg0;
    self->arg1 = arg1;
}

/*
* track opens
*/
syscall::open*:return
/
    ((pid == gTargetPID) || progenyof(gTargetPID))
/
{
    this->op_kind = ((self->arg1 & O_ACCMODE) == O_RDONLY) ? "READ" : "WRITE";
    this->path0 = self->arg0 ? copyinstr(self->arg0) : "<nil>";

    printf("open for %s: <%s> #%d",
        this->op_kind,
        this->path0,
        arg0);
}
Run Code Online (Sandbox Code Playgroud)


bdo*_*lan 2

创建一个启动程序,该程序将等待某种信号(不一定是文字信号,只是表明它已准备好),然后 exec() 您的目标。现在 dtrace -p 启动程序,一旦 dtrace 启动,就让启动程序运行。