A dynamic tracer for Linux

node.c 3.3KB

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