1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/perf_event.h>
11 #include "trace_helpers.h"
13 #define MAX_SYMS 300000
14 static struct ksym syms
[MAX_SYMS
];
17 static int ksym_cmp(const void *p1
, const void *p2
)
19 return ((struct ksym
*)p1
)->addr
- ((struct ksym
*)p2
)->addr
;
22 int load_kallsyms(void)
24 FILE *f
= fopen("/proc/kallsyms", "r");
25 char func
[256], buf
[256];
33 while (fgets(buf
, sizeof(buf
), f
)) {
34 if (sscanf(buf
, "%p %c %s", &addr
, &symbol
, func
) != 3)
38 syms
[i
].addr
= (long) addr
;
39 syms
[i
].name
= strdup(func
);
44 qsort(syms
, sym_cnt
, sizeof(struct ksym
), ksym_cmp
);
48 struct ksym
*ksym_search(long key
)
50 int start
= 0, end
= sym_cnt
;
53 /* kallsyms not loaded. return NULL */
58 size_t mid
= start
+ (end
- start
) / 2;
60 result
= key
- syms
[mid
].addr
;
69 if (start
>= 1 && syms
[start
- 1].addr
< key
&&
70 key
< syms
[start
].addr
)
72 return &syms
[start
- 1];
74 /* out of range. return _stext */
78 long ksym_get_addr(const char *name
)
82 for (i
= 0; i
< sym_cnt
; i
++) {
83 if (strcmp(syms
[i
].name
, name
) == 0)