A dynamic tracer for Linux

sym.c 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "sym.h"
  7. sym_t *sym_get(symtab_t *st, const char *name)
  8. {
  9. size_t i;
  10. for (i = 0; i < st->len; i++) {
  11. if (!strcmp(st->sym[i].name, name))
  12. return &st->sym[i];
  13. }
  14. return NULL;
  15. }
  16. int sym_add(symtab_t *st, const char *name, type_t *type, sym_t **new)
  17. {
  18. sym_t *sym;
  19. sym = sym_get(st, name);
  20. if (sym) {
  21. /* if there is no type info, accept anything */
  22. if (!sym->type)
  23. sym->type = type;
  24. /* otherwise, if specified, it must match */
  25. else if (type && !type_equal(sym->type, type))
  26. return -EINVAL;
  27. goto found;
  28. }
  29. st->sym = realloc(st->sym, ++st->len * sizeof(*st->sym));
  30. assert(st->sym);
  31. sym = &st->sym[st->len-1];
  32. sym->st = st;
  33. sym->name = name;
  34. sym->type = type;
  35. found:
  36. if (new)
  37. *new = sym;
  38. return 0;
  39. }
  40. void sym_dump(sym_t *sym, FILE *fp)
  41. {
  42. type_dump(sym->type, fp);
  43. fputc(' ', fp);
  44. fputs(sym->name, fp);
  45. }
  46. void symtab_dump(symtab_t *st, FILE *fp)
  47. {
  48. size_t i;
  49. for (i = 0; i < st->len; i++) {
  50. sym_dump(&st->sym[i], fp);
  51. fputc('\n', fp);
  52. }
  53. }