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

前端 未结 7 1634
我在风中等你
我在风中等你 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:09

    Well, this is a bit old, but why not :-)..

    I don't think there is a way to do this simply from command line, but as suggested, a simple launcher application, such as the following, would do it. The manual attaching could of course also be replaced with a few calls to libdtrace.

    int main(int argc, char *argv[]) {
        pid_t pid = fork();
        if(pid == 0) {
            setuid(123);
            seteuid(123);
            ptrace(PT_TRACE_ME, 0, NULL, 0);
            execl("/bin/ls", "/bin/ls", NULL);
        } else if(pid > 0) {
            int status;
            wait(&status);
    
            printf("Process %d started. Attach now, and click enter.\n", pid);
            getchar();
    
            ptrace(PT_CONTINUE, pid, (caddr_t) 1, 0);
        }
    
        return 0;
    }
    

提交回复
热议问题