A dynamic tracer for Linux

node.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(struct node *n, FILE *fp)
  18. {
  19. switch (n->ntype) {
  20. case N_EXPR:
  21. fprintf(fp, "\e[31m%s\e[0m", n->expr.func);
  22. break;
  23. case N_IDENT:
  24. fputs(n->ident.name, fp);
  25. break;
  26. case N_NUM:
  27. fprintf(fp, "%"PRId64, n->num.num);
  28. break;
  29. case N_STRING:
  30. fprintf(fp, "\"%s\"", n->string.data);
  31. break;
  32. default:
  33. fputs("<INVALID>", fp);
  34. }
  35. __type_dump(n->type, fp);
  36. }
  37. struct node_dump_info {
  38. FILE *fp;
  39. int indent, width;
  40. };
  41. int __node_dump_pre(struct node *n, void *_info)
  42. {
  43. struct node_dump_info *info = _info;
  44. struct node *arg;
  45. if (info->indent)
  46. fprintf(info->fp, "\n%*s", info->width, "");
  47. else
  48. fputc(' ', fp);
  49. if (n->ntype == N_EXPR) {
  50. fputc('(', fp);
  51. info->indent = 0;
  52. node_expr_foreach(n, arg) {
  53. struct node *subarg;
  54. if (arg->ntype != N_EXPR)
  55. continue;
  56. node_expr_foreach(arg, subarg) {
  57. if (arg->ntype == N_EXPR) {
  58. info->indent = 1;
  59. info->width += 2;
  60. break;
  61. }
  62. }
  63. if (info->indent)
  64. break;
  65. }
  66. }
  67. node_print(n, info->fp);
  68. return 0;
  69. }
  70. int __node_dump_post(struct node *n, void *_info)
  71. {
  72. struct node_dump_info *info = _info;
  73. struct node *c;
  74. if (n->ntype != N_EXPR)
  75. return 0;
  76. /* if (info->ident) { */
  77. /* info->width -= 2; */
  78. /* fprintf(info->fp, "\n%*s", info->width, ""); */
  79. /* } */
  80. fputc(')', info->fp);
  81. return 0;
  82. }
  83. void node_dump(struct node *n, FILE *fp)
  84. {
  85. struct node_dump_info info = {
  86. .fp = fp,
  87. };
  88. node_walk(n, __node_dump_pre, __node_dump_post, &info);
  89. }
  90. int node_walk(struct node *n,
  91. int (*pre)(struct node *, void *),
  92. int (*post)(struct node *, void *),
  93. void *ctx)
  94. {
  95. int err = 0;
  96. if (pre && (err = pre(n, ctx)))
  97. return err;
  98. if (n->ntype == N_EXPR) {
  99. struct node *arg;
  100. node_expr_foreach(n, arg) {
  101. err = node_walk(arg, pre, post, ctx);
  102. if (err)
  103. return err;
  104. }
  105. }
  106. if (post && (err = post(n, ctx)))
  107. return err;
  108. return 0;
  109. }
  110. int node_replace(struct node *n, struct node *new)
  111. {
  112. new->up = n->up;
  113. if (n->prev) {
  114. new->prev = n->prev;
  115. n->prev->next = new;
  116. }
  117. if (n->next) {
  118. new->next = n->next;
  119. n->next->prev = new;
  120. }
  121. /* TODO: don't leak memory */
  122. return 0;
  123. }
  124. /* constructors */
  125. static struct node *node_new(enum ntype ntype)
  126. {
  127. struct node *n;
  128. n = calloc(1, sizeof(*n));
  129. assert(n);
  130. n->ntype = N_IDENT;
  131. n->ident.name = name;
  132. return n;
  133. }
  134. struct node *node_string(char *data)
  135. {
  136. struct node *n = node_new(N_STRING);
  137. n->string.data = data;
  138. return n;
  139. }
  140. struct node *node_num(int64_t num)
  141. {
  142. struct node *n = node_new(N_NUM);
  143. n->num.num = num;
  144. return n;
  145. }
  146. struct node *node_ident(char *name)
  147. {
  148. struct node *n = node_new(N_IDENT);
  149. n->ident.name = name;
  150. return n;
  151. }
  152. struct node *node_append(struct node *n, struct node *arg)
  153. {
  154. struct node *last;
  155. assert(n->ntype == N_EXPR);
  156. arg->up = n;
  157. if (!n->expr.args) {
  158. n->expr.args = arg;
  159. return n;
  160. }
  161. for (last = n->expr.args; last->next; last = last->next);
  162. last->next = arg;
  163. arg->prev = last;
  164. return n;
  165. }
  166. struct node *node_expr(char *func, ...)
  167. {
  168. va_list ap;
  169. struct node *n, *arg;
  170. n = node_new(N_EXPR);
  171. n->expr.func = func;
  172. va_start(ap, func);
  173. while ((arg = va_arg(ap, struct node *)))
  174. append(n, arg);
  175. va_end(ap);
  176. return n;
  177. }