1 /* Copyright (c) 2016 Google Inc., All Rights Reserved.
2 * Ron Minnich <rminnich@google.com>
3 * See LICENSE for details. */
7 #include <parlib/parlib.h>
10 #include <iplib/iplib.h>
11 #include <iplib/icmp.h>
14 #include <parlib/spinlock.h>
15 #include <parlib/timing.h>
16 #include <parlib/tsc-compat.h>
17 #include <parlib/printf-ext.h>
18 #include <benchutil/alarm.h>
19 #include <ndblib/ndb.h>
22 #include <sys/types.h>
27 fprintf(stderr, "usage: strace command [args...]\n");
31 void main(int argc, char **argv, char **envp)
36 static char p[2 * MAX_PATH_LEN];
37 static char buf[16384];
39 char *prog_name = argv[1];
44 if ((*argv[1] != '/') && (*argv[1] != '.')) {
45 snprintf(p, sizeof(p), "/bin/%s", argv[1]);
49 pid = sys_proc_create(prog_name, strlen(prog_name), argv + 1, envp,
52 perror("proc_create");
55 /* We need to wait on the child asynchronously. If we hold a ref (as the
56 * parent), the child won't proc_free and that won't hangup/wake us from a
58 syscall_async(&sysc, SYS_waitpid, pid, NULL, 0, 0, 0, 0);
60 snprintf(p, sizeof(p), "/proc/%d/ctl", pid);
61 fd = open(p, O_WRITE);
63 fprintf(stderr, "open %s: %r\n", p);
67 snprintf(p, sizeof(p), "straceall");
68 if (write(fd, p, strlen(p)) < strlen(p)) {
69 fprintf(stderr, "write to ctl %s %d: %r\n", p, fd);
74 snprintf(p, sizeof(p), "/proc/%d/strace", pid);
77 fprintf(stderr, "open %s: %r\n", p);
81 /* now that we've set up the tracing, we can run the process. isn't it
82 * great that the process doesn't immediately start when you make it? */
85 while ((amt = read(fd, buf, sizeof(buf))) > 0) {
86 if (write(fileno(stderr), buf, amt) < amt) {
87 fprintf(stderr, "Write to stdout: %r\n");
91 fprintf(stderr, "strace of PID %d: %r\n", pid);