kallsyms.c 948 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "symbol/kallsyms.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int kallsyms__parse(const char *filename, void *arg,
  5. int (*process_symbol)(void *arg, const char *name,
  6. char type, u64 start))
  7. {
  8. char *line = NULL;
  9. size_t n;
  10. int err = -1;
  11. FILE *file = fopen(filename, "r");
  12. if (file == NULL)
  13. goto out_failure;
  14. err = 0;
  15. while (!feof(file)) {
  16. u64 start;
  17. int line_len, len;
  18. char symbol_type;
  19. char *symbol_name;
  20. line_len = getline(&line, &n, file);
  21. if (line_len < 0 || !line)
  22. break;
  23. line[--line_len] = '\0'; /* \n */
  24. len = hex2u64(line, &start);
  25. len++;
  26. if (len + 2 >= line_len)
  27. continue;
  28. symbol_type = line[len];
  29. len += 2;
  30. symbol_name = line + len;
  31. len = line_len - len;
  32. if (len >= KSYM_NAME_LEN) {
  33. err = -1;
  34. break;
  35. }
  36. err = process_symbol(arg, symbol_name, symbol_type, start);
  37. if (err)
  38. break;
  39. }
  40. free(line);
  41. fclose(file);
  42. return err;
  43. out_failure:
  44. return -1;
  45. }