ratrace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <thread.h>
  13. enum {
  14. Stacksize = 8*1024,
  15. Bufsize = 8*1024,
  16. };
  17. Channel *out;
  18. Channel *quit;
  19. Channel *forkc;
  20. int nread = 0;
  21. int debug = 0;
  22. int outf = 2;
  23. typedef struct Str Str;
  24. struct Str {
  25. char *buf;
  26. int len;
  27. };
  28. void
  29. die(char *s)
  30. {
  31. fprint(outf, "%s\n", s);
  32. exits(s);
  33. }
  34. void
  35. cwrite(int fd, char *path, char *cmd, int len)
  36. {
  37. werrstr("");
  38. if (write(fd, cmd, len) < len) {
  39. fprint(outf, "cwrite: %s: failed writing %d bytes: %r\n",
  40. path, len);
  41. sendp(quit, nil);
  42. threadexits(nil);
  43. }
  44. }
  45. Str *
  46. newstr(void)
  47. {
  48. Str *s;
  49. s = mallocz(sizeof(Str) + Bufsize, 1);
  50. if (s == nil)
  51. sysfatal("malloc");
  52. s->buf = (char *)&s[1];
  53. return s;
  54. }
  55. void
  56. reader(void *v)
  57. {
  58. int cfd, tfd, exiting, pid;
  59. uintptr_t newpid;
  60. char *ctl, *truss;
  61. Str *s;
  62. static char waitstop[] = "waitstop";
  63. pid = (int)(uintptr)v;
  64. if (debug)
  65. fprint(outf, "DEBUG: -------------> reader starts with pid %d\n", pid);
  66. ctl = smprint("/proc/%d/ctl", pid);
  67. if ((cfd = open(ctl, OWRITE)) < 0)
  68. die(smprint("%s: %r", ctl));
  69. truss = smprint("/proc/%d/syscall", pid);
  70. if ((tfd = open(truss, OREAD)) < 0)
  71. die(smprint("%s: %r", truss));
  72. if (debug)
  73. fprint(outf, "DEBUG: Send %s to pid %d ...", waitstop, pid);
  74. /* child was stopped by hang msg earlier */
  75. cwrite(cfd, ctl, waitstop, sizeof waitstop - 1);
  76. if (debug)
  77. fprint(outf, "DEBUG: back for %d\n", pid);
  78. if (debug)
  79. fprint(outf, "DEBUG: Send %s to pid %d\n", "startsyscall", pid);
  80. cwrite(cfd, ctl, "startsyscall", 12);
  81. if (debug)
  82. fprint(outf, "DEBUG: back for %d\n", pid);
  83. s = newstr();
  84. exiting = 0;
  85. while((s->len = pread(tfd, s->buf, Bufsize - 1, 0)) >= 0){
  86. if (s->buf[0] == 'F') {
  87. char *val = strstr(s->buf, "= ");
  88. if (val) {
  89. newpid = strtol(&val[2], 0, 0);
  90. sendp(forkc, (void*)newpid);
  91. procrfork(reader, (void*)newpid, Stacksize, 0);
  92. }
  93. }
  94. if (strstr(s->buf, " Exits") != nil)
  95. exiting = 1;
  96. sendp(out, s); /* print line from /proc/$child/syscall */
  97. if (exiting) {
  98. s = newstr();
  99. strcpy(s->buf, "\n");
  100. sendp(out, s);
  101. break;
  102. }
  103. /* flush syscall trace buffer */
  104. if (debug)
  105. fprint(outf, "DEBUG: Send %s to pid %d\n", "startsyscall", pid);
  106. cwrite(cfd, ctl, "startsyscall", 12);
  107. if (debug)
  108. fprint(outf, "DEBUG: back for %d\n", pid);
  109. s = newstr();
  110. }
  111. sendp(quit, nil);
  112. threadexitsall(nil);
  113. }
  114. void
  115. writer(void *v)
  116. {
  117. int newpid;
  118. Alt a[4];
  119. Str *s;
  120. a[0].op = CHANRCV;
  121. a[0].c = quit;
  122. a[0].v = nil;
  123. a[1].op = CHANRCV;
  124. a[1].c = out;
  125. a[1].v = &s;
  126. a[2].op = CHANRCV;
  127. a[2].c = forkc;
  128. a[2].v = &newpid;
  129. a[3].op = CHANEND;
  130. for(;;)
  131. switch(alt(a)){
  132. case 0: /* quit */
  133. nread--;
  134. if(nread <= 0)
  135. goto done;
  136. break;
  137. case 1: /* out */
  138. fprint(outf, "%s", s->buf);
  139. free(s);
  140. break;
  141. case 2: /* forkc */
  142. // procrfork(reader, (void*)newpid, Stacksize, 0);
  143. nread++;
  144. break;
  145. }
  146. done:
  147. exits(nil);
  148. }
  149. void
  150. usage(void)
  151. {
  152. fprint(2, "Usage: ratrace [-o file] [-d] [-c cmd [arg...]] | [pid]\n");
  153. exits("usage");
  154. }
  155. void
  156. hang(void)
  157. {
  158. int me;
  159. char *myctl;
  160. static char hang[] = "hang";
  161. myctl = smprint("/proc/%d/ctl", getpid());
  162. me = open(myctl, OWRITE);
  163. if (me < 0)
  164. sysfatal("can't open %s: %r", myctl);
  165. cwrite(me, myctl, hang, sizeof hang - 1);
  166. close(me);
  167. free(myctl);
  168. }
  169. void
  170. threadmain(int argc, char **argv)
  171. {
  172. int pid;
  173. char *cmd = nil;
  174. char **args = nil;
  175. /*
  176. * don't bother with fancy arg processing, because it picks up options
  177. * for the command you are starting. Just check for -c as argv[1]
  178. * and then take it from there.
  179. */
  180. ++argv;
  181. --argc;
  182. while (argv[0][0] == '-') {
  183. switch(argv[0][1]) {
  184. case 'c':
  185. if (argc < 2)
  186. usage();
  187. cmd = strdup(argv[1]);
  188. args = &argv[1];
  189. break;
  190. case 'd':
  191. debug = 1;
  192. break;
  193. case 'o':
  194. if (argc < 2)
  195. usage();
  196. outf = create(argv[1], OWRITE, 0666);
  197. if (outf < 0)
  198. sysfatal(smprint("%s: %r", argv[1]));
  199. ++argv;
  200. --argc;
  201. break;
  202. default:
  203. usage();
  204. }
  205. ++argv;
  206. --argc;
  207. }
  208. /* run a command? */
  209. if(cmd) {
  210. pid = fork();
  211. if (pid < 0)
  212. sysfatal("fork failed: %r");
  213. if(pid == 0) {
  214. hang();
  215. exec(cmd, args);
  216. if(cmd[0] != '/')
  217. exec(smprint("/bin/%s", cmd), args);
  218. sysfatal("exec %s failed: %r", cmd);
  219. }
  220. } else {
  221. if(argc != 1)
  222. usage();
  223. pid = atoi(argv[0]);
  224. }
  225. out = chancreate(sizeof(char*), 0);
  226. quit = chancreate(sizeof(char*), 0);
  227. forkc = chancreate(sizeof(uint32_t *), 0);
  228. nread++;
  229. procrfork(writer, nil, Stacksize, 0);
  230. reader((void*)(uintptr_t)pid);
  231. }