A dynamic tracer for Linux

node.c 3.1KB

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