trace_helpers.c 4.1 KB

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