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];
34 if (!fgets(buf
, sizeof(buf
), f
))
36 if (sscanf(buf
, "%p %c %s", &addr
, &symbol
, func
) != 3)
40 syms
[i
].addr
= (long) addr
;
41 syms
[i
].name
= strdup(func
);
46 qsort(syms
, sym_cnt
, sizeof(struct ksym
), ksym_cmp
);
50 struct ksym
*ksym_search(long key
)
52 int start
= 0, end
= sym_cnt
;
55 /* kallsyms not loaded. return NULL */
60 size_t mid
= start
+ (end
- start
) / 2;
62 result
= key
- syms
[mid
].addr
;
71 if (start
>= 1 && syms
[start
- 1].addr
< key
&&
72 key
< syms
[start
].addr
)
74 return &syms
[start
- 1];
76 /* out of range. return _stext */
80 long ksym_get_addr(const char *name
)
84 for (i
= 0; i
< sym_cnt
; i
++) {
85 if (strcmp(syms
[i
].name
, name
) == 0)
93 static int page_cnt
= 8;
94 static struct perf_event_mmap_page
*header
;
96 int perf_event_mmap_header(int fd
, struct perf_event_mmap_page
**header
)
101 page_size
= getpagesize();
102 mmap_size
= page_size
* (page_cnt
+ 1);
104 base
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
105 if (base
== MAP_FAILED
) {
106 printf("mmap err\n");
114 int perf_event_mmap(int fd
)
116 return perf_event_mmap_header(fd
, &header
);
119 static int perf_event_poll(int fd
)
121 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
123 return poll(&pfd
, 1, 1000);
126 struct perf_event_sample
{
127 struct perf_event_header header
;
132 static enum bpf_perf_event_ret
bpf_perf_event_print(void *event
, void *priv
)
134 struct perf_event_sample
*e
= event
;
135 perf_event_print_fn fn
= priv
;
138 if (e
->header
.type
== PERF_RECORD_SAMPLE
) {
139 ret
= fn(e
->data
, e
->size
);
140 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
142 } else if (e
->header
.type
== PERF_RECORD_LOST
) {
144 struct perf_event_header header
;
147 } *lost
= (void *) e
;
148 printf("lost %lld events\n", lost
->lost
);
150 printf("unknown event type=%d size=%d\n",
151 e
->header
.type
, e
->header
.size
);
154 return LIBBPF_PERF_EVENT_CONT
;
157 int perf_event_poller(int fd
, perf_event_print_fn output_fn
)
159 enum bpf_perf_event_ret ret
;
165 ret
= bpf_perf_event_read_simple(header
, page_cnt
* page_size
,
166 page_size
, &buf
, &len
,
167 bpf_perf_event_print
,
169 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
177 int perf_event_poller_multi(int *fds
, struct perf_event_mmap_page
**headers
,
178 int num_fds
, perf_event_print_fn output_fn
)
180 enum bpf_perf_event_ret ret
;
186 pfds
= calloc(num_fds
, sizeof(*pfds
));
188 return LIBBPF_PERF_EVENT_ERROR
;
190 for (i
= 0; i
< num_fds
; i
++) {
192 pfds
[i
].events
= POLLIN
;
196 poll(pfds
, num_fds
, 1000);
197 for (i
= 0; i
< num_fds
; i
++) {
198 if (!pfds
[i
].revents
)
201 ret
= bpf_perf_event_read_simple(headers
[i
],
202 page_cnt
* page_size
,
203 page_size
, &buf
, &len
,
204 bpf_perf_event_print
,
206 if (ret
!= LIBBPF_PERF_EVENT_CONT
)