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)
91 static int page_cnt
= 8;
92 static struct perf_event_mmap_page
*header
;
94 int perf_event_mmap_header(int fd
, struct perf_event_mmap_page
**header
)
99 page_size
= getpagesize();
100 mmap_size
= page_size
* (page_cnt
+ 1);
102 base
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
103 if (base
== MAP_FAILED
) {
104 printf("mmap err\n");
112 int perf_event_mmap(int fd
)
114 return perf_event_mmap_header(fd
, &header
);
117 static int perf_event_poll(int fd
)
119 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
121 return poll(&pfd
, 1, 1000);
124 struct perf_event_sample
{
125 struct perf_event_header header
;
130 static enum bpf_perf_event_ret
131 bpf_perf_event_print(struct perf_event_header
*hdr
, void *private_data
)
133 struct perf_event_sample
*e
= (struct perf_event_sample
*)hdr
;
134 perf_event_print_fn fn
= private_data
;
137 if (e
->header
.type
== PERF_RECORD_SAMPLE
) {
138 ret
= fn(e
->data
, e
->size
);
139 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
141 } else if (e
->header
.type
== PERF_RECORD_LOST
) {
143 struct perf_event_header header
;
146 } *lost
= (void *) e
;
147 printf("lost %lld events\n", lost
->lost
);
149 printf("unknown event type=%d size=%d\n",
150 e
->header
.type
, e
->header
.size
);
153 return LIBBPF_PERF_EVENT_CONT
;
156 int perf_event_poller(int fd
, perf_event_print_fn output_fn
)
158 enum bpf_perf_event_ret ret
;
164 ret
= bpf_perf_event_read_simple(header
, page_cnt
* page_size
,
165 page_size
, &buf
, &len
,
166 bpf_perf_event_print
,
168 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
176 int perf_event_poller_multi(int *fds
, struct perf_event_mmap_page
**headers
,
177 int num_fds
, perf_event_print_fn output_fn
)
179 enum bpf_perf_event_ret ret
;
185 pfds
= calloc(num_fds
, sizeof(*pfds
));
187 return LIBBPF_PERF_EVENT_ERROR
;
189 for (i
= 0; i
< num_fds
; i
++) {
191 pfds
[i
].events
= POLLIN
;
195 poll(pfds
, num_fds
, 1000);
196 for (i
= 0; i
< num_fds
; i
++) {
197 if (!pfds
[i
].revents
)
200 ret
= bpf_perf_event_read_simple(headers
[i
],
201 page_cnt
* page_size
,
202 page_size
, &buf
, &len
,
203 bpf_perf_event_print
,
205 if (ret
!= LIBBPF_PERF_EVENT_CONT
)