ratrace.c 4.4 KB

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