ratrace.c 3.7 KB

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