A dynamic tracer for Linux

sym.c 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "node.h"
  7. #include "sym.h"
  8. #include "type.h"
  9. static struct sym *__sym_alloc(struct symtab *st, const char *name,
  10. const struct func *func)
  11. {
  12. struct sym *sym;
  13. st->syms = realloc(st->syms, ++st->len * sizeof(*st->syms));
  14. assert(st->syms);
  15. st->syms[st->len - 1] = calloc(1, sizeof(struct sym));
  16. sym = st->syms[st->len - 1];
  17. sym->st = st;
  18. sym->name = name;
  19. sym->func = func;
  20. return sym;
  21. }
  22. static struct sym *sym_alloc_ident(struct symtab *st, struct node *n,
  23. const struct func *func)
  24. {
  25. struct sym **sym;
  26. symtab_foreach(st, sym) {
  27. if ((*sym)->name && !strcmp((*sym)->name, n->ident.name))
  28. return *sym;
  29. }
  30. return __sym_alloc(st, n->ident.name, func);
  31. }
  32. struct sym *sym_alloc(struct symtab *st, struct node *n,
  33. const struct func *func)
  34. {
  35. switch (n->ntype) {
  36. case N_EXPR:
  37. case N_NUM:
  38. case N_STRING:
  39. return __sym_alloc(st, NULL, func);
  40. case N_IDENT:
  41. return sym_alloc_ident(st, n, func);
  42. }
  43. assert(0);
  44. return NULL;
  45. }
  46. void sym_dump(struct sym *sym, FILE *fp)
  47. {
  48. type_dump(sym->type, sym->name, fp);
  49. }
  50. void symtab_dump(struct symtab *st, FILE *fp)
  51. {
  52. struct sym **sym;
  53. symtab_foreach(st, sym) {
  54. if (!(*sym)->name)
  55. continue;
  56. sym_dump(*sym, fp);
  57. fputc('\n', fp);
  58. }
  59. }