trace_helpers.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /* kallsyms not loaded. return NULL */
  48. if (sym_cnt <= 0)
  49. return NULL;
  50. while (start < end) {
  51. size_t mid = start + (end - start) / 2;
  52. result = key - syms[mid].addr;
  53. if (result < 0)
  54. end = mid;
  55. else if (result > 0)
  56. start = mid + 1;
  57. else
  58. return &syms[mid];
  59. }
  60. if (start >= 1 && syms[start - 1].addr < key &&
  61. key < syms[start].addr)
  62. /* valid ksym */
  63. return &syms[start - 1];
  64. /* out of range. return _stext */
  65. return &syms[0];
  66. }
  67. long ksym_get_addr(const char *name)
  68. {
  69. int i;
  70. for (i = 0; i < sym_cnt; i++) {
  71. if (strcmp(syms[i].name, name) == 0)
  72. return syms[i].addr;
  73. }
  74. return 0;
  75. }
  76. static int page_size;
  77. static int page_cnt = 8;
  78. static struct perf_event_mmap_page *header;
  79. int perf_event_mmap_header(int fd, struct perf_event_mmap_page **header)
  80. {
  81. void *base;
  82. int mmap_size;
  83. page_size = getpagesize();
  84. mmap_size = page_size * (page_cnt + 1);
  85. base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  86. if (base == MAP_FAILED) {
  87. printf("mmap err\n");
  88. return -1;
  89. }
  90. *header = base;
  91. return 0;
  92. }
  93. int perf_event_mmap(int fd)
  94. {
  95. return perf_event_mmap_header(fd, &header);
  96. }
  97. static int perf_event_poll(int fd)
  98. {
  99. struct pollfd pfd = { .fd = fd, .events = POLLIN };
  100. return poll(&pfd, 1, 1000);
  101. }
  102. struct perf_event_sample {
  103. struct perf_event_header header;
  104. __u32 size;
  105. char data[];
  106. };
  107. static enum bpf_perf_event_ret bpf_perf_event_print(void *event, void *priv)
  108. {
  109. struct perf_event_sample *e = event;
  110. perf_event_print_fn fn = priv;
  111. int ret;
  112. if (e->header.type == PERF_RECORD_SAMPLE) {
  113. ret = fn(e->data, e->size);
  114. if (ret != LIBBPF_PERF_EVENT_CONT)
  115. return ret;
  116. } else if (e->header.type == PERF_RECORD_LOST) {
  117. struct {
  118. struct perf_event_header header;
  119. __u64 id;
  120. __u64 lost;
  121. } *lost = (void *) e;
  122. printf("lost %lld events\n", lost->lost);
  123. } else {
  124. printf("unknown event type=%d size=%d\n",
  125. e->header.type, e->header.size);
  126. }
  127. return LIBBPF_PERF_EVENT_CONT;
  128. }
  129. int perf_event_poller(int fd, perf_event_print_fn output_fn)
  130. {
  131. enum bpf_perf_event_ret ret;
  132. void *buf = NULL;
  133. size_t len = 0;
  134. for (;;) {
  135. perf_event_poll(fd);
  136. ret = bpf_perf_event_read_simple(header, page_cnt * page_size,
  137. page_size, &buf, &len,
  138. bpf_perf_event_print,
  139. output_fn);
  140. if (ret != LIBBPF_PERF_EVENT_CONT)
  141. break;
  142. }
  143. free(buf);
  144. return ret;
  145. }
  146. int perf_event_poller_multi(int *fds, struct perf_event_mmap_page **headers,
  147. int num_fds, perf_event_print_fn output_fn)
  148. {
  149. enum bpf_perf_event_ret ret;
  150. struct pollfd *pfds;
  151. void *buf = NULL;
  152. size_t len = 0;
  153. int i;
  154. pfds = calloc(num_fds, sizeof(*pfds));
  155. if (!pfds)
  156. return LIBBPF_PERF_EVENT_ERROR;
  157. for (i = 0; i < num_fds; i++) {
  158. pfds[i].fd = fds[i];
  159. pfds[i].events = POLLIN;
  160. }
  161. for (;;) {
  162. poll(pfds, num_fds, 1000);
  163. for (i = 0; i < num_fds; i++) {
  164. if (!pfds[i].revents)
  165. continue;
  166. ret = bpf_perf_event_read_simple(headers[i],
  167. page_cnt * page_size,
  168. page_size, &buf, &len,
  169. bpf_perf_event_print,
  170. output_fn);
  171. if (ret != LIBBPF_PERF_EVENT_CONT)
  172. break;
  173. }
  174. }
  175. free(buf);
  176. free(pfds);
  177. return ret;
  178. }