1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/perf_event.h>
12 #include "trace_helpers.h"
14 #define DEBUGFS "/sys/kernel/debug/tracing/"
16 #define MAX_SYMS 300000
17 static struct ksym syms[MAX_SYMS];
20 static int ksym_cmp(const void *p1, const void *p2)
22 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
25 int load_kallsyms(void)
27 FILE *f = fopen("/proc/kallsyms", "r");
28 char func[256], buf[256];
36 while (fgets(buf, sizeof(buf), f)) {
37 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
41 syms[i].addr = (long) addr;
42 syms[i].name = strdup(func);
47 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
51 struct ksym *ksym_search(long key)
53 int start = 0, end = sym_cnt;
56 /* kallsyms not loaded. return NULL */
61 size_t mid = start + (end - start) / 2;
63 result = key - syms[mid].addr;
72 if (start >= 1 && syms[start - 1].addr < key &&
73 key < syms[start].addr)
75 return &syms[start - 1];
77 /* out of range. return _stext */
81 long ksym_get_addr(const char *name)
85 for (i = 0; i < sym_cnt; i++) {
86 if (strcmp(syms[i].name, name) == 0)
93 void read_trace_pipe(void)
97 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
102 static char buf[4096];
105 sz = read(trace_fd, buf, sizeof(buf) - 1);