kallsyms.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "symbol/kallsyms.h"
  3. #include "api/io.h"
  4. #include <stdio.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. u8 kallsyms2elf_type(char type)
  8. {
  9. type = tolower(type);
  10. return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT;
  11. }
  12. bool kallsyms__is_function(char symbol_type)
  13. {
  14. symbol_type = toupper(symbol_type);
  15. return symbol_type == 'T' || symbol_type == 'W';
  16. }
  17. static void read_to_eol(struct io *io)
  18. {
  19. int ch;
  20. for (;;) {
  21. ch = io__get_char(io);
  22. if (ch < 0 || ch == '\n')
  23. return;
  24. }
  25. }
  26. int kallsyms__parse(const char *filename, void *arg,
  27. int (*process_symbol)(void *arg, const char *name,
  28. char type, u64 start))
  29. {
  30. struct io io;
  31. char bf[BUFSIZ];
  32. int err;
  33. io.fd = open(filename, O_RDONLY, 0);
  34. if (io.fd < 0)
  35. return -1;
  36. io__init(&io, io.fd, bf, sizeof(bf));
  37. err = 0;
  38. while (!io.eof) {
  39. __u64 start;
  40. int ch;
  41. size_t i;
  42. char symbol_type;
  43. char symbol_name[KSYM_NAME_LEN + 1];
  44. if (io__get_hex(&io, &start) != ' ') {
  45. read_to_eol(&io);
  46. continue;
  47. }
  48. symbol_type = io__get_char(&io);
  49. if (io__get_char(&io) != ' ') {
  50. read_to_eol(&io);
  51. continue;
  52. }
  53. for (i = 0; i < sizeof(symbol_name); i++) {
  54. ch = io__get_char(&io);
  55. if (ch < 0 || ch == '\n')
  56. break;
  57. symbol_name[i] = ch;
  58. }
  59. symbol_name[i] = '\0';
  60. err = process_symbol(arg, symbol_name, symbol_type, start);
  61. if (err)
  62. break;
  63. }
  64. close(io.fd);
  65. return err;
  66. }