A dynamic tracer for Linux

node.c 3.2KB

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