ratrace.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 (forking && s->buf[1] == '=' && s->buf[3] != '-') {
  87. forking = 0;
  88. newpid = strtol(&s->buf[3], 0, 0);
  89. sendp(forkc, (void*)newpid);
  90. procrfork(reader, (void*)newpid, Stacksize, 0);
  91. }
  92. /*
  93. * There are three tests here and they (I hope) guarantee
  94. * no false positives.
  95. */
  96. /* BUG: when we update all the system call generation stuff, we broke rfork tracing.
  97. * The convention is the first letter of the system call in syscallfmt should be upper case.
  98. * This subtlety got lost. FIX ME but it will be tricky.
  99. * The reason to upcase is we need to be careful not to be fooled by random text.
  100. if (strstr(s->buf, " Rfork") != nil) {
  101. */
  102. if (strstr(s->buf, " rfork") != nil) {
  103. char *a[8];
  104. char *rf;
  105. rf = strdup(s->buf);
  106. if (tokenize(rf, a, 8) == 4 &&
  107. strtoul(a[3], 0, 16) & RFPROC)
  108. forking = 1;
  109. if (debug)
  110. fprint(2, "DEBUG:really forking? %d\n", forking);
  111. free(rf);
  112. } else if (strstr(s->buf, " Exits") != nil)
  113. exiting = 1;
  114. sendp(out, s); /* print line from /proc/$child/syscall */
  115. if (exiting) {
  116. s = newstr();
  117. strcpy(s->buf, "\n");
  118. sendp(out, s);
  119. break;
  120. }
  121. /* flush syscall trace buffer */
  122. if (debug)
  123. fprint(2, "DEBUG: Send %s to pid %d\n", "startsyscall", pid);
  124. cwrite(cfd, ctl, "startsyscall", 12);
  125. if (debug)
  126. fprint(2, "DEBUG: back for %d\n", pid);
  127. s = newstr();
  128. }
  129. sendp(quit, nil);
  130. threadexitsall(nil);
  131. }
  132. void
  133. writer(void *v)
  134. {
  135. int newpid;
  136. Alt a[4];
  137. Str *s;
  138. a[0].op = CHANRCV;
  139. a[0].c = quit;
  140. a[0].v = nil;
  141. a[1].op = CHANRCV;
  142. a[1].c = out;
  143. a[1].v = &s;
  144. a[2].op = CHANRCV;
  145. a[2].c = forkc;
  146. a[2].v = &newpid;
  147. a[3].op = CHANEND;
  148. for(;;)
  149. switch(alt(a)){
  150. case 0: /* quit */
  151. nread--;
  152. if(nread <= 0)
  153. goto done;
  154. break;
  155. case 1: /* out */
  156. /* it's a nice null terminated thing */
  157. fprint(2, "%s", s->buf);
  158. free(s);
  159. break;
  160. case 2: /* forkc */
  161. // procrfork(reader, (void*)newpid, Stacksize, 0);
  162. nread++;
  163. break;
  164. }
  165. done:
  166. exits(nil);
  167. }
  168. void
  169. usage(void)
  170. {
  171. fprint(2, "Usage: ratrace [-c cmd [arg...]] | [pid]\n");
  172. exits("usage");
  173. }
  174. void
  175. hang(void)
  176. {
  177. int me;
  178. char *myctl;
  179. static char hang[] = "hang";
  180. myctl = smprint("/proc/%d/ctl", getpid());
  181. me = open(myctl, OWRITE);
  182. if (me < 0)
  183. sysfatal("can't open %s: %r", myctl);
  184. cwrite(me, myctl, hang, sizeof hang - 1);
  185. close(me);
  186. free(myctl);
  187. }
  188. void
  189. threadmain(int argc, char **argv)
  190. {
  191. uintptr_t pid;
  192. char *cmd = nil;
  193. char **args = nil;
  194. /*
  195. * don't bother with fancy arg processing, because it picks up options
  196. * for the command you are starting. Just check for -c as argv[1]
  197. * and then take it from there.
  198. */
  199. if (argc < 2)
  200. usage();
  201. while (argv[1][0] == '-') {
  202. switch(argv[1][1]) {
  203. case 'c':
  204. if (argc < 3)
  205. usage();
  206. cmd = strdup(argv[2]);
  207. args = &argv[2];
  208. break;
  209. case 'd':
  210. debug = 1;
  211. break;
  212. default:
  213. usage();
  214. }
  215. ++argv;
  216. --argc;
  217. }
  218. /* run a command? */
  219. if(cmd) {
  220. pid = fork();
  221. if (pid < 0)
  222. sysfatal("fork failed: %r");
  223. if(pid == 0) {
  224. hang();
  225. exec(cmd, args);
  226. if(cmd[0] != '/')
  227. exec(smprint("/bin/%s", cmd), args);
  228. sysfatal("exec %s failed: %r", cmd);
  229. }
  230. } else {
  231. if(argc != 2)
  232. usage();
  233. pid = atoi(argv[1]);
  234. }
  235. out = chancreate(sizeof(char*), 0);
  236. quit = chancreate(sizeof(char*), 0);
  237. forkc = chancreate(sizeof(uint32_t *), 0);
  238. nread++;
  239. procrfork(writer, nil, Stacksize, 0);
  240. reader((void*)pid);
  241. }