How to trace a program from its very beginning without running it as root

前端 未结 7 1559
我在风中等你
我在风中等你 2021-02-04 09:44

I\'m writing a tool that calls through to DTrace to trace the program that the user specifies.

If my tool uses dtrace -c to run the program as a subprocess of DTrace, no

相关标签:
7条回答
  • 2021-02-04 10:19

    This script takes the name of the executable (for an app this is the info.plist's CFBundleExecutable) you want to monitor to DTrace as a parameter (you can then launch the target app after this script is running):

    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);
    }
    
    0 讨论(0)
提交回复
热议问题