A dynamic tracer for Linux

node.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "printxf.h"
  11. void node_print(struct node *n, FILE *fp)
  12. {
  13. switch (n->ntype) {
  14. case N_EXPR:
  15. fprintf(fp, "\e[31m%s\e[0m", n->expr.func);
  16. break;
  17. case N_IDENT:
  18. fputs(n->ident.name, fp);
  19. break;
  20. case N_NUM:
  21. fprintf(fp, "%"PRId64, n->num.num);
  22. break;
  23. case N_STRING:
  24. fprintf(fp, "\"%s\"", n->string.data);
  25. break;
  26. default:
  27. fputs("<INVALID>", fp);
  28. }
  29. }
  30. int node_vfprintxf(struct printxf *pxf, FILE *fp, const char *spec, va_list ap)
  31. {
  32. struct node *n;
  33. n = va_arg(ap, struct node *);
  34. /* TODO: "%#N" should print source file location */
  35. node_print(n, fp);
  36. return 0;
  37. }
  38. int node_walk(struct node *n,
  39. int (*pre)(struct node *, void *),
  40. int (*post)(struct node *, void *),
  41. void *ctx)
  42. {
  43. int err = 0;
  44. if (pre && (err = pre(n, ctx)))
  45. return err;
  46. if (n->ntype == N_EXPR) {
  47. struct node *arg;
  48. node_expr_foreach(n, arg) {
  49. err = node_walk(arg, pre, post, ctx);
  50. if (err)
  51. return err;
  52. }
  53. }
  54. if (post && (err = post(n, ctx)))
  55. return err;
  56. return 0;
  57. }
  58. int node_replace(struct node *n, struct node *new)
  59. {
  60. new->up = n->up;
  61. if (n->prev) {
  62. new->prev = n->prev;
  63. n->prev->next = new;
  64. }
  65. if (n->next) {
  66. new->next = n->next;
  67. n->next->prev = new;
  68. }
  69. /* TODO: don't leak memory */
  70. return 0;
  71. }
  72. /* helpers */
  73. int node_is_block(struct node *n)
  74. {
  75. if (!n || (n->ntype != N_EXPR))
  76. return 0;
  77. return !strcmp(n->expr.func, ":block");
  78. }
  79. int node_is_map(struct node *n)
  80. {
  81. if (!n || (n->ntype != N_EXPR))
  82. return 0;
  83. return !strcmp(n->expr.func, ":map");
  84. }
  85. /* constructors */
  86. static struct node *node_new(enum ntype ntype)
  87. {
  88. struct node *n;
  89. n = calloc(1, sizeof(*n));
  90. assert(n);
  91. n->ntype = ntype;
  92. return n;
  93. }
  94. struct node *node_string(char *data)
  95. {
  96. struct node *n = node_new(N_STRING);
  97. n->string.data = data;
  98. return n;
  99. }
  100. struct node *node_num(int64_t num)
  101. {
  102. struct node *n = node_new(N_NUM);
  103. n->num.num = num;
  104. return n;
  105. }
  106. struct node *node_ident(char *name)
  107. {
  108. struct node *n = node_new(N_IDENT);
  109. n->ident.name = name;
  110. return n;
  111. }
  112. struct node *node_append(struct node *n, struct node *arg)
  113. {
  114. struct node *last;
  115. assert(n->ntype == N_EXPR);
  116. arg->up = n;
  117. if (!n->expr.args) {
  118. n->expr.args = arg;
  119. return n;
  120. }
  121. for (last = n->expr.args; last->next; last = last->next);
  122. last->next = arg;
  123. arg->prev = last;
  124. return n;
  125. }
  126. struct node *node_expr(char *func, ...)
  127. {
  128. va_list ap;
  129. struct node *n, *arg;
  130. n = node_new(N_EXPR);
  131. n->expr.func = func;
  132. va_start(ap, func);
  133. while ((arg = va_arg(ap, struct node *)))
  134. node_append(n, arg);
  135. va_end(ap);
  136. return n;
  137. }
  138. __attribute__((constructor))
  139. static void node_init(void)
  140. {
  141. printxf_default.vfprintxf['N'] = node_vfprintxf;
  142. }