A dynamic tracer for Linux

sym.c 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, tid_t tid, 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->tid)
  23. sym->tid = tid;
  24. /* otherwise, if specified, it must match */
  25. else if (tid && (sym->tid != tid))
  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->name = name;
  33. sym->tid = tid;
  34. found:
  35. if (new)
  36. *new = sym;
  37. return 0;
  38. }
  39. void sym_dump(sym_t *sym, FILE *fp)
  40. {
  41. type_dump_cdecl(type_info(sym->tid), fp);
  42. fputc(' ', fp);
  43. fputs(sym->name, fp);
  44. }
  45. void symtab_dump(symtab_t *st, FILE *fp)
  46. {
  47. size_t i;
  48. for (i = 0; i < st->len; i++) {
  49. sym_dump(&st->sym[i], fp);
  50. fputc('\n', fp);
  51. }
  52. }