A dynamic tracer for Linux

utils.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include "node.h"
  4. #include "printxf.h"
  5. #include "sym.h"
  6. #include "type.h"
  7. struct ast_fprint_info {
  8. FILE *fp;
  9. int indent;
  10. };
  11. static int __ast_fprint_pre(struct node *n, void *_info)
  12. {
  13. struct ast_fprint_info *info = _info;
  14. struct node *arg;
  15. if (n->prev)
  16. fputc(' ', info->fp);
  17. if (node_is_block(n->up)) {
  18. info->indent += 4;
  19. fprintf(info->fp, "\n%*s", info->indent, "");
  20. }
  21. if (n->ntype == N_EXPR)
  22. fputc('(', info->fp);
  23. fprintxf(NULL, info->fp,
  24. "%N%T", n, n->sym ? n->sym->type : NULL);
  25. if ((n->ntype == N_EXPR) && n->expr.args)
  26. fputc(' ', info->fp);
  27. return 0;
  28. }
  29. static int __ast_fprint_post(struct node *n, void *_info)
  30. {
  31. struct ast_fprint_info *info = _info;
  32. struct node *c;
  33. if (node_is_block(n))
  34. fprintf(info->fp, "\n%*s", info->indent, "");
  35. if (n->ntype == N_EXPR)
  36. fputc(')', info->fp);
  37. if (node_is_block(n->up))
  38. info->indent -= 4;
  39. return 0;
  40. }
  41. void ast_fprint(FILE *fp, struct node *root)
  42. {
  43. struct ast_fprint_info info = {
  44. .fp = fp,
  45. };
  46. node_walk(root, __ast_fprint_pre, __ast_fprint_post, &info);
  47. fputc('\n', fp);
  48. }
  49. int order_vfprintxf(struct printxf *pxf, FILE *fp, const char *fmt, va_list ap)
  50. {
  51. int arg = va_arg(ap, int);
  52. switch (arg) {
  53. case 1:
  54. fputs("1st", fp);
  55. return 3;
  56. case 2:
  57. fputs("2nd", fp);
  58. return 3;
  59. case 3:
  60. fputs("3rd", fp);
  61. return 3;
  62. }
  63. return fprintf(fp, "%dth", arg);
  64. }
  65. __attribute__((constructor))
  66. static void utils_init(void)
  67. {
  68. printxf_default.vfprintxf['O'] = order_vfprintxf;
  69. }