A dynamic tracer for Linux

node.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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;
  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 (n->prev)
  46. fputc(' ', info->fp);
  47. if (node_is_block(n->up)) {
  48. info->indent += 4;
  49. fprintf(info->fp, "\n%*s", info->indent, "");
  50. }
  51. if (n->ntype == N_EXPR)
  52. fputc('(', info->fp);
  53. node_print(n, info->fp);
  54. if ((n->ntype == N_EXPR) && n->expr.args)
  55. fputc(' ', info->fp);
  56. return 0;
  57. }
  58. int __node_dump_post(struct node *n, void *_info)
  59. {
  60. struct node_dump_info *info = _info;
  61. struct node *c;
  62. if (node_is_block(n))
  63. fprintf(info->fp, "\n%*s", info->indent, "");
  64. if (n->ntype == N_EXPR)
  65. fputc(')', info->fp);
  66. if (node_is_block(n->up))
  67. info->indent -= 4;
  68. return 0;
  69. }
  70. void node_dump(struct node *n, FILE *fp)
  71. {
  72. struct node_dump_info info = {
  73. .fp = fp,
  74. };
  75. node_walk(n, __node_dump_pre, __node_dump_post, &info);
  76. fputc('\n', fp);
  77. }
  78. int node_walk(struct node *n,
  79. int (*pre)(struct node *, void *),
  80. int (*post)(struct node *, void *),
  81. void *ctx)
  82. {
  83. int err = 0;
  84. if (pre && (err = pre(n, ctx)))
  85. return err;
  86. if (n->ntype == N_EXPR) {
  87. struct node *arg;
  88. node_expr_foreach(n, arg) {
  89. err = node_walk(arg, pre, post, ctx);
  90. if (err)
  91. return err;
  92. }
  93. }
  94. if (post && (err = post(n, ctx)))
  95. return err;
  96. return 0;
  97. }
  98. int node_replace(struct node *n, struct node *new)
  99. {
  100. new->up = n->up;
  101. if (n->prev) {
  102. new->prev = n->prev;
  103. n->prev->next = new;
  104. }
  105. if (n->next) {
  106. new->next = n->next;
  107. n->next->prev = new;
  108. }
  109. /* TODO: don't leak memory */
  110. return 0;
  111. }
  112. /* constructors */
  113. static struct node *node_new(enum ntype ntype)
  114. {
  115. struct node *n;
  116. n = calloc(1, sizeof(*n));
  117. assert(n);
  118. n->ntype = ntype;
  119. return n;
  120. }
  121. struct node *node_string(char *data)
  122. {
  123. struct node *n = node_new(N_STRING);
  124. n->string.data = data;
  125. return n;
  126. }
  127. struct node *node_num(int64_t num)
  128. {
  129. struct node *n = node_new(N_NUM);
  130. n->num.num = num;
  131. return n;
  132. }
  133. struct node *node_ident(char *name)
  134. {
  135. struct node *n = node_new(N_IDENT);
  136. n->ident.name = name;
  137. return n;
  138. }
  139. struct node *node_append(struct node *n, struct node *arg)
  140. {
  141. struct node *last;
  142. assert(n->ntype == N_EXPR);
  143. arg->up = n;
  144. if (!n->expr.args) {
  145. n->expr.args = arg;
  146. return n;
  147. }
  148. for (last = n->expr.args; last->next; last = last->next);
  149. last->next = arg;
  150. arg->prev = last;
  151. return n;
  152. }
  153. struct node *node_expr(char *func, ...)
  154. {
  155. va_list ap;
  156. struct node *n, *arg;
  157. n = node_new(N_EXPR);
  158. n->expr.func = func;
  159. va_start(ap, func);
  160. while ((arg = va_arg(ap, struct node *)))
  161. node_append(n, arg);
  162. va_end(ap);
  163. return n;
  164. }