ratrace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <thread.h>
  5. enum {
  6. Stacksize = 8*1024,
  7. Bufsize = 8*1024,
  8. };
  9. Channel *out;
  10. Channel *quit;
  11. Channel *forkc;
  12. int nread = 0;
  13. typedef struct Str Str;
  14. struct Str {
  15. char *buf;
  16. int len;
  17. };
  18. void
  19. die(char *s)
  20. {
  21. fprint(2, "%s\n", s);
  22. exits(s);
  23. }
  24. void
  25. cwrite(int fd, char *path, char *cmd, int len)
  26. {
  27. werrstr("");
  28. if (write(fd, cmd, len) < len) {
  29. fprint(2, "cwrite: %s: failed writing %d bytes: %r\n",
  30. path, len);
  31. sendp(quit, nil);
  32. threadexits(nil);
  33. }
  34. }
  35. Str *
  36. newstr(void)
  37. {
  38. Str *s;
  39. s = mallocz(sizeof(Str) + Bufsize, 1);
  40. if (s == nil)
  41. sysfatal("malloc");
  42. s->buf = (char *)&s[1];
  43. return s;
  44. }
  45. void
  46. reader(void *v)
  47. {
  48. int cfd, tfd, forking = 0, exiting, pid, newpid;
  49. char *ctl, *truss;
  50. Str *s;
  51. static char start[] = "start";
  52. static char waitstop[] = "waitstop";
  53. pid = (int)(uintptr)v;
  54. ctl = smprint("/proc/%d/ctl", pid);
  55. if ((cfd = open(ctl, OWRITE)) < 0)
  56. die(smprint("%s: %r", ctl));
  57. truss = smprint("/proc/%d/syscall", pid);
  58. if ((tfd = open(truss, OREAD)) < 0)
  59. die(smprint("%s: %r", truss));
  60. /* child was stopped by hang msg earlier */
  61. cwrite(cfd, ctl, waitstop, sizeof waitstop - 1);
  62. cwrite(cfd, ctl, "startsyscall", 12);
  63. s = newstr();
  64. exiting = 0;
  65. while((s->len = pread(tfd, s->buf, Bufsize - 1, 0)) >= 0){
  66. if (forking && s->buf[1] == '=' && s->buf[3] != '-') {
  67. forking = 0;
  68. newpid = strtol(&s->buf[3], 0, 0);
  69. sendp(forkc, (void*)newpid);
  70. procrfork(reader, (void*)newpid, Stacksize, 0);
  71. }
  72. /*
  73. * There are three tests here and they (I hope) guarantee
  74. * no false positives.
  75. */
  76. if (strstr(s->buf, " Rfork") != nil) {
  77. char *a[8];
  78. char *rf;
  79. rf = strdup(s->buf);
  80. if (tokenize(rf, a, 8) == 5 &&
  81. strtoul(a[4], 0, 16) & RFPROC)
  82. forking = 1;
  83. free(rf);
  84. } else if (strstr(s->buf, " Exits") != nil)
  85. exiting = 1;
  86. sendp(out, s); /* print line from /proc/$child/syscall */
  87. if (exiting) {
  88. s = newstr();
  89. strcpy(s->buf, "\n");
  90. sendp(out, s);
  91. break;
  92. }
  93. /* flush syscall trace buffer */
  94. cwrite(cfd, ctl, "startsyscall", 12);
  95. s = newstr();
  96. }
  97. sendp(quit, nil);
  98. threadexitsall(nil);
  99. }
  100. void
  101. writer(void *)
  102. {
  103. int newpid;
  104. Alt a[4];
  105. Str *s;
  106. a[0].op = CHANRCV;
  107. a[0].c = quit;
  108. a[0].v = nil;
  109. a[1].op = CHANRCV;
  110. a[1].c = out;
  111. a[1].v = &s;
  112. a[2].op = CHANRCV;
  113. a[2].c = forkc;
  114. a[2].v = &newpid;
  115. a[3].op = CHANEND;
  116. for(;;)
  117. switch(alt(a)){
  118. case 0: /* quit */
  119. nread--;
  120. if(nread <= 0)
  121. goto done;
  122. break;
  123. case 1: /* out */
  124. /* it's a nice null terminated thing */
  125. fprint(2, "%s", s->buf);
  126. free(s);
  127. break;
  128. case 2: /* forkc */
  129. // procrfork(reader, (void*)newpid, Stacksize, 0);
  130. nread++;
  131. break;
  132. }
  133. done:
  134. exits(nil);
  135. }
  136. void
  137. usage(void)
  138. {
  139. fprint(2, "Usage: ratrace [-c cmd [arg...]] | [pid]\n");
  140. exits("usage");
  141. }
  142. void
  143. hang(void)
  144. {
  145. int me;
  146. char *myctl;
  147. static char hang[] = "hang";
  148. myctl = smprint("/proc/%d/ctl", getpid());
  149. me = open(myctl, OWRITE);
  150. if (me < 0)
  151. sysfatal("can't open %s: %r", myctl);
  152. cwrite(me, myctl, hang, sizeof hang - 1);
  153. close(me);
  154. free(myctl);
  155. }
  156. void
  157. threadmain(int argc, char **argv)
  158. {
  159. int pid;
  160. char *cmd = nil;
  161. char **args = nil;
  162. /*
  163. * don't bother with fancy arg processing, because it picks up options
  164. * for the command you are starting. Just check for -c as argv[1]
  165. * and then take it from there.
  166. */
  167. if (argc < 2)
  168. usage();
  169. while (argv[1][0] == '-') {
  170. switch(argv[1][1]) {
  171. case 'c':
  172. if (argc < 3)
  173. usage();
  174. cmd = strdup(argv[2]);
  175. args = &argv[2];
  176. break;
  177. default:
  178. usage();
  179. }
  180. ++argv;
  181. --argc;
  182. }
  183. /* run a command? */
  184. if(cmd) {
  185. pid = fork();
  186. if (pid < 0)
  187. sysfatal("fork failed: %r");
  188. if(pid == 0) {
  189. hang();
  190. exec(cmd, args);
  191. if(cmd[0] != '/')
  192. exec(smprint("/bin/%s", cmd), args);
  193. sysfatal("exec %s failed: %r", cmd);
  194. }
  195. } else {
  196. if(argc != 2)
  197. usage();
  198. pid = atoi(argv[1]);
  199. }
  200. out = chancreate(sizeof(char*), 0);
  201. quit = chancreate(sizeof(char*), 0);
  202. forkc = chancreate(sizeof(ulong *), 0);
  203. nread++;
  204. procrfork(writer, nil, Stacksize, 0);
  205. reader((void*)pid);
  206. }