trace_helpers.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <poll.h>
  8. #include <unistd.h>
  9. #include <linux/perf_event.h>
  10. #include <sys/mman.h>
  11. #include "trace_helpers.h"
  12. #define MAX_SYMS 300000
  13. static struct ksym syms[MAX_SYMS];
  14. static int sym_cnt;
  15. static int ksym_cmp(const void *p1, const void *p2)
  16. {
  17. return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
  18. }
  19. int load_kallsyms(void)
  20. {
  21. FILE *f = fopen("/proc/kallsyms", "r");
  22. char func[256], buf[256];
  23. char symbol;
  24. void *addr;
  25. int i = 0;
  26. if (!f)
  27. return -ENOENT;
  28. while (!feof(f)) {
  29. if (!fgets(buf, sizeof(buf), f))
  30. break;
  31. if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
  32. break;
  33. if (!addr)
  34. continue;
  35. syms[i].addr = (long) addr;
  36. syms[i].name = strdup(func);
  37. i++;
  38. }
  39. sym_cnt = i;
  40. qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
  41. return 0;
  42. }
  43. struct ksym *ksym_search(long key)
  44. {
  45. int start = 0, end = sym_cnt;
  46. int result;
  47. while (start < end) {
  48. size_t mid = start + (end - start) / 2;
  49. result = key - syms[mid].addr;
  50. if (result < 0)
  51. end = mid;
  52. else if (result > 0)
  53. start = mid + 1;
  54. else
  55. return &syms[mid];
  56. }
  57. if (start >= 1 && syms[start - 1].addr < key &&
  58. key < syms[start].addr)
  59. /* valid ksym */
  60. return &syms[start - 1];
  61. /* out of range. return _stext */
  62. return &syms[0];
  63. }
  64. static int page_size;
  65. static int page_cnt = 8;
  66. static struct perf_event_mmap_page *header;
  67. int perf_event_mmap(int fd)
  68. {
  69. void *base;
  70. int mmap_size;
  71. page_size = getpagesize();
  72. mmap_size = page_size * (page_cnt + 1);
  73. base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  74. if (base == MAP_FAILED) {
  75. printf("mmap err\n");
  76. return -1;
  77. }
  78. header = base;
  79. return 0;
  80. }
  81. static int perf_event_poll(int fd)
  82. {
  83. struct pollfd pfd = { .fd = fd, .events = POLLIN };
  84. return poll(&pfd, 1, 1000);
  85. }
  86. struct perf_event_sample {
  87. struct perf_event_header header;
  88. __u32 size;
  89. char data[];
  90. };
  91. static enum bpf_perf_event_ret bpf_perf_event_print(void *event, void *priv)
  92. {
  93. struct perf_event_sample *e = event;
  94. perf_event_print_fn fn = priv;
  95. int ret;
  96. if (e->header.type == PERF_RECORD_SAMPLE) {
  97. ret = fn(e->data, e->size);
  98. if (ret != LIBBPF_PERF_EVENT_CONT)
  99. return ret;
  100. } else if (e->header.type == PERF_RECORD_LOST) {
  101. struct {
  102. struct perf_event_header header;
  103. __u64 id;
  104. __u64 lost;
  105. } *lost = (void *) e;
  106. printf("lost %lld events\n", lost->lost);
  107. } else {
  108. printf("unknown event type=%d size=%d\n",
  109. e->header.type, e->header.size);
  110. }
  111. return LIBBPF_PERF_EVENT_CONT;
  112. }
  113. int perf_event_poller(int fd, perf_event_print_fn output_fn)
  114. {
  115. enum bpf_perf_event_ret ret;
  116. void *buf = NULL;
  117. size_t len = 0;
  118. for (;;) {
  119. perf_event_poll(fd);
  120. ret = bpf_perf_event_read_simple(header, page_cnt * page_size,
  121. page_size, &buf, &len,
  122. bpf_perf_event_print,
  123. output_fn);
  124. if (ret != LIBBPF_PERF_EVENT_CONT)
  125. break;
  126. }
  127. free(buf);
  128. return ret;
  129. }