ratrace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. typedef struct Str Str;
  23. struct Str {
  24. char *buf;
  25. int len;
  26. };
  27. void
  28. die(char *s)
  29. {
  30. fprint(2, "%s\n", s);
  31. exits(s);
  32. }
  33. void
  34. cwrite(int fd, char *path, char *cmd, int len)
  35. {
  36. werrstr("");
  37. if (write(fd, cmd, len) < len) {
  38. fprint(2, "cwrite: %s: failed writing %d bytes: %r\n",
  39. path, len);
  40. sendp(quit, nil);
  41. threadexits(nil);
  42. }
  43. }
  44. Str *
  45. newstr(void)
  46. {
  47. Str *s;
  48. s = mallocz(sizeof(Str) + Bufsize, 1);
  49. if (s == nil)
  50. sysfatal("malloc");
  51. s->buf = (char *)&s[1];
  52. return s;
  53. }
  54. void
  55. reader(void *v)
  56. {
  57. int cfd, tfd, forking = 0, exiting, pid;
  58. uintptr_t newpid;
  59. char *ctl, *truss;
  60. Str *s;
  61. static char start[] = "start";
  62. static char waitstop[] = "waitstop";
  63. pid = (int)(uintptr)v;
  64. if (debug)
  65. fprint(2, "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(2, "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(2, "DEBUG: back for %d\n", pid);
  78. if (debug)
  79. fprint(2, "DEBUG: Send %s to pid %d\n", "startsyscall", pid);
  80. cwrite(cfd, ctl, "startsyscall", 12);
  81. if (debug)
  82. fprint(2, "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(2, "DEBUG: Send %s to pid %d\n", "startsyscall", pid);
  106. cwrite(cfd, ctl, "startsyscall", 12);
  107. if (debug)
  108. fprint(2, "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(2, "%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 [-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. uintptr_t 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. if (argc < 2)
  181. usage();
  182. while (argv[1][0] == '-') {
  183. switch(argv[1][1]) {
  184. case 'c':
  185. if (argc < 3)
  186. usage();
  187. cmd = strdup(argv[2]);
  188. args = &argv[2];
  189. break;
  190. case 'd':
  191. debug = 1;
  192. break;
  193. default:
  194. usage();
  195. }
  196. ++argv;
  197. --argc;
  198. }
  199. /* run a command? */
  200. if(cmd) {
  201. pid = fork();
  202. if (pid < 0)
  203. sysfatal("fork failed: %r");
  204. if(pid == 0) {
  205. hang();
  206. exec(cmd, args);
  207. if(cmd[0] != '/')
  208. exec(smprint("/bin/%s", cmd), args);
  209. sysfatal("exec %s failed: %r", cmd);
  210. }
  211. } else {
  212. if(argc != 2)
  213. usage();
  214. pid = atoi(argv[1]);
  215. }
  216. out = chancreate(sizeof(char*), 0);
  217. quit = chancreate(sizeof(char*), 0);
  218. forkc = chancreate(sizeof(uint32_t *), 0);
  219. nread++;
  220. procrfork(writer, nil, Stacksize, 0);
  221. reader((void*)pid);
  222. }