A dynamic tracer for Linux

node.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <assert.h>
  2. #include <ctype.h>
  3. #include <inttypes.h>
  4. #include <search.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "ply.h"
  9. void __type_dump(type_t *t, FILE *fp)
  10. {
  11. if (!t)
  12. return;
  13. fputs(" \e[2m<", fp);
  14. type_dump(t, fp);
  15. fputs(">\e[0m", fp);
  16. }
  17. void node_print(node_t *n, FILE *fp)
  18. {
  19. switch (n->ntype) {
  20. case N_LIST:
  21. fputs("()", fp);
  22. break;
  23. case N_KEYWORD:
  24. fputs("\e[31m", fp);
  25. fputc(n->keyword.class, fp);
  26. if (n->keyword.op)
  27. fputc(n->keyword.op, fp);
  28. fputs("\e[0m", fp);
  29. break;
  30. case N_IDENT:
  31. fputs(n->ident, fp);
  32. break;
  33. case N_NUM:
  34. fprintf(fp, "%#"PRIx64, n->num);
  35. break;
  36. case N_STRING:
  37. fprintf(fp, "\"%s\"", n->string);
  38. break;
  39. default:
  40. fputs("<INVALID>", fp);
  41. }
  42. __type_dump(n->type, fp);
  43. }
  44. struct node_dump_info {
  45. FILE *fp;
  46. int indent;
  47. };
  48. int __node_dump_pre(node_t *n, void *_info)
  49. {
  50. struct node_dump_info *info = _info;
  51. switch (n->ntype) {
  52. case N_LIST:
  53. fprintf(info->fp, "%s%*s(",
  54. info->indent? "\n" : "",
  55. info->indent, "");
  56. info->indent += 2;
  57. break;
  58. default:
  59. if (n->prev) {
  60. node_t *c;
  61. for (c = n->next; c; c = c->next) {
  62. if (c->ntype == N_LIST) {
  63. fprintf(info->fp, "\n%*s",
  64. info->indent, "");
  65. break;
  66. }
  67. }
  68. if (!c)
  69. fputc(' ', info->fp);
  70. }
  71. node_print(n, info->fp);
  72. }
  73. return 0;
  74. }
  75. int __node_dump_post(node_t *n, void *_info)
  76. {
  77. struct node_dump_info *info = _info;
  78. node_t *c;
  79. if (n->ntype != N_LIST)
  80. return 0;
  81. info->indent -= 2;
  82. for (c = n->list; c; c = c->next) {
  83. if (c->ntype == N_LIST) {
  84. fprintf(info->fp, "\n%*s)", info->indent, "");
  85. __type_dump(n->type, info->fp);
  86. return 0;
  87. }
  88. }
  89. fputc(')', info->fp);
  90. __type_dump(n->type, info->fp);
  91. return 0;
  92. }
  93. void node_dump(node_t *n, FILE *fp)
  94. {
  95. struct node_dump_info info = {
  96. .fp = fp,
  97. };
  98. node_walk(n, __node_dump_pre, __node_dump_post, &info);
  99. }
  100. int node_walk(node_t *n,
  101. int (*pre)(node_t *, void *),
  102. int (*post)(node_t *, void *),
  103. void *ctx)
  104. {
  105. int err = 0;
  106. if (pre && (err = pre(n, ctx)))
  107. return err;
  108. if (n->ntype == N_LIST) {
  109. node_t *c;
  110. for (c = n->list; c; c = c->next) {
  111. err = node_walk(c, pre, post, ctx);
  112. if (err)
  113. return err;
  114. }
  115. }
  116. if (post && (err = post(n, ctx)))
  117. return err;
  118. return 0;
  119. }
  120. int node_replace(node_t *n, node_t *new)
  121. {
  122. new->up = n->up;
  123. if (n->prev) {
  124. new->prev = n->prev;
  125. n->prev->next = new;
  126. }
  127. if (n->next) {
  128. new->next = n->next;
  129. n->next->prev = new;
  130. }
  131. /* TODO: don't leak memory */
  132. return 0;
  133. }
  134. /* constructors */
  135. static node_t *__node(ntype_t ntype)
  136. {
  137. node_t *n = calloc(1, sizeof(*n));
  138. assert(n);
  139. n->ntype = ntype;
  140. return n;
  141. }
  142. node_t *node_list(node_t *head)
  143. {
  144. node_t *n = __node(N_LIST);
  145. n->list = head;
  146. head->up = n;
  147. return n;
  148. }
  149. node_t *node_vlist(node_t *head, ...)
  150. {
  151. va_list ap;
  152. node_t *n, *next;
  153. n = node_list(head);
  154. va_start(ap, head);
  155. while ((next = va_arg(ap, node_t *))) {
  156. insque(next, head);
  157. next->up = n;
  158. head = next;
  159. }
  160. va_end(ap);
  161. return n;
  162. }
  163. node_t *node_keyword(kwclass_t class, int op)
  164. {
  165. node_t *n = __node(N_KEYWORD);
  166. n->keyword.class = class;
  167. n->keyword.op = op;
  168. return n;
  169. }
  170. node_t *node_ident(const char *name)
  171. {
  172. node_t *n = __node(N_IDENT);
  173. n->ident = name;
  174. return n;
  175. }
  176. node_t *node_num(int64_t num)
  177. {
  178. node_t *n = __node(N_NUM);
  179. n->num = num;
  180. /* TODO: handle L UL ULL and such nonsense */
  181. n->type = &t_s64;
  182. return n;
  183. }
  184. node_t *node_string(const char *string)
  185. {
  186. node_t *n = __node(N_STRING);
  187. n->string = string;
  188. /* TODO: string type like dtrace? */
  189. n->type = type_ptr_of(&t_char);
  190. return n;
  191. }