A dynamic tracer for Linux

node.c 3.4KB

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