perf dlfilter: Add attr() to perf_dlfilter_fns
[linux-2.6-microblaze.git] / tools / perf / util / dlfilter.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dlfilter.c: Interface to perf script --dlfilter shared object
4  * Copyright (c) 2021, Intel Corporation.
5  */
6 #include <dlfcn.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <dirent.h>
10 #include <subcmd/exec-cmd.h>
11 #include <linux/zalloc.h>
12 #include <linux/build_bug.h>
13
14 #include "debug.h"
15 #include "event.h"
16 #include "evsel.h"
17 #include "dso.h"
18 #include "map.h"
19 #include "thread.h"
20 #include "trace-event.h"
21 #include "symbol.h"
22 #include "srcline.h"
23 #include "dlfilter.h"
24 #include "perf_dlfilter.h"
25
26 static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
27 {
28         struct symbol *sym = al->sym;
29
30         d_al->size = sizeof(*d_al);
31         if (al->map) {
32                 struct dso *dso = al->map->dso;
33
34                 if (symbol_conf.show_kernel_path && dso->long_name)
35                         d_al->dso = dso->long_name;
36                 else
37                         d_al->dso = dso->name;
38                 d_al->is_64_bit = dso->is_64_bit;
39                 d_al->buildid_size = dso->bid.size;
40                 d_al->buildid = dso->bid.data;
41         } else {
42                 d_al->dso = NULL;
43                 d_al->is_64_bit = 0;
44                 d_al->buildid_size = 0;
45                 d_al->buildid = NULL;
46         }
47         if (sym) {
48                 d_al->sym = sym->name;
49                 d_al->sym_start = sym->start;
50                 d_al->sym_end = sym->end;
51                 if (al->addr < sym->end)
52                         d_al->symoff = al->addr - sym->start;
53                 else
54                         d_al->symoff = al->addr - al->map->start - sym->start;
55                 d_al->sym_binding = sym->binding;
56         } else {
57                 d_al->sym = NULL;
58                 d_al->sym_start = 0;
59                 d_al->sym_end = 0;
60                 d_al->symoff = 0;
61                 d_al->sym_binding = 0;
62         }
63         d_al->addr = al->addr;
64         d_al->comm = NULL;
65         d_al->filtered = 0;
66 }
67
68 static struct addr_location *get_al(struct dlfilter *d)
69 {
70         struct addr_location *al = d->al;
71
72         if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0)
73                 return NULL;
74         return al;
75 }
76
77 static struct thread *get_thread(struct dlfilter *d)
78 {
79         struct addr_location *al = get_al(d);
80
81         return al ? al->thread : NULL;
82 }
83
84 static const struct perf_dlfilter_al *dlfilter__resolve_ip(void *ctx)
85 {
86         struct dlfilter *d = (struct dlfilter *)ctx;
87         struct perf_dlfilter_al *d_al = d->d_ip_al;
88         struct addr_location *al;
89
90         if (!d->ctx_valid)
91                 return NULL;
92
93         /* 'size' is also used to indicate already initialized */
94         if (d_al->size)
95                 return d_al;
96
97         al = get_al(d);
98         if (!al)
99                 return NULL;
100
101         al_to_d_al(al, d_al);
102
103         d_al->is_kernel_ip = machine__kernel_ip(d->machine, d->sample->ip);
104         d_al->comm = al->thread ? thread__comm_str(al->thread) : ":-1";
105         d_al->filtered = al->filtered;
106
107         return d_al;
108 }
109
110 static const struct perf_dlfilter_al *dlfilter__resolve_addr(void *ctx)
111 {
112         struct dlfilter *d = (struct dlfilter *)ctx;
113         struct perf_dlfilter_al *d_addr_al = d->d_addr_al;
114         struct addr_location *addr_al = d->addr_al;
115
116         if (!d->ctx_valid || !d->d_sample->addr_correlates_sym)
117                 return NULL;
118
119         /* 'size' is also used to indicate already initialized */
120         if (d_addr_al->size)
121                 return d_addr_al;
122
123         if (!addr_al->thread) {
124                 struct thread *thread = get_thread(d);
125
126                 if (!thread)
127                         return NULL;
128                 thread__resolve(thread, addr_al, d->sample);
129         }
130
131         al_to_d_al(addr_al, d_addr_al);
132
133         d_addr_al->is_kernel_ip = machine__kernel_ip(d->machine, d->sample->addr);
134
135         return d_addr_al;
136 }
137
138 static char **dlfilter__args(void *ctx, int *dlargc)
139 {
140         struct dlfilter *d = (struct dlfilter *)ctx;
141
142         if (dlargc)
143                 *dlargc = 0;
144         else
145                 return NULL;
146
147         if (!d->ctx_valid && !d->in_start && !d->in_stop)
148                 return NULL;
149
150         *dlargc = d->dlargc;
151         return d->dlargv;
152 }
153
154 static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfilter_al *d_al_p)
155 {
156         struct dlfilter *d = (struct dlfilter *)ctx;
157         struct perf_dlfilter_al d_al;
158         struct addr_location al;
159         struct thread *thread;
160         __u32 sz;
161
162         if (!d->ctx_valid || !d_al_p)
163                 return -1;
164
165         thread = get_thread(d);
166         if (!thread)
167                 return -1;
168
169         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
170
171         al_to_d_al(&al, &d_al);
172
173         d_al.is_kernel_ip = machine__kernel_ip(d->machine, address);
174
175         sz = d_al_p->size;
176         memcpy(d_al_p, &d_al, min((size_t)sz, sizeof(d_al)));
177         d_al_p->size = sz;
178
179         return 0;
180 }
181
182 static const __u8 *dlfilter__insn(void *ctx, __u32 *len)
183 {
184         struct dlfilter *d = (struct dlfilter *)ctx;
185
186         if (!len)
187                 return NULL;
188
189         *len = 0;
190
191         if (!d->ctx_valid)
192                 return NULL;
193
194         if (d->sample->ip && !d->sample->insn_len) {
195                 struct addr_location *al = d->al;
196
197                 if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0)
198                         return NULL;
199
200                 if (al->thread->maps && al->thread->maps->machine)
201                         script_fetch_insn(d->sample, al->thread, al->thread->maps->machine);
202         }
203
204         if (!d->sample->insn_len)
205                 return NULL;
206
207         *len = d->sample->insn_len;
208
209         return (__u8 *)d->sample->insn;
210 }
211
212 static const char *dlfilter__srcline(void *ctx, __u32 *line_no)
213 {
214         struct dlfilter *d = (struct dlfilter *)ctx;
215         struct addr_location *al;
216         unsigned int line = 0;
217         char *srcfile = NULL;
218         struct map *map;
219         u64 addr;
220
221         if (!d->ctx_valid || !line_no)
222                 return NULL;
223
224         al = get_al(d);
225         if (!al)
226                 return NULL;
227
228         map = al->map;
229         addr = al->addr;
230
231         if (map && map->dso)
232                 srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line);
233
234         *line_no = line;
235         return srcfile;
236 }
237
238 static struct perf_event_attr *dlfilter__attr(void *ctx)
239 {
240         struct dlfilter *d = (struct dlfilter *)ctx;
241
242         if (!d->ctx_valid)
243                 return NULL;
244
245         return &d->evsel->core.attr;
246 }
247
248 static const struct perf_dlfilter_fns perf_dlfilter_fns = {
249         .resolve_ip      = dlfilter__resolve_ip,
250         .resolve_addr    = dlfilter__resolve_addr,
251         .args            = dlfilter__args,
252         .resolve_address = dlfilter__resolve_address,
253         .insn            = dlfilter__insn,
254         .srcline         = dlfilter__srcline,
255         .attr            = dlfilter__attr,
256 };
257
258 static char *find_dlfilter(const char *file)
259 {
260         char path[PATH_MAX];
261         char *exec_path;
262
263         if (strchr(file, '/'))
264                 goto out;
265
266         if (!access(file, R_OK)) {
267                 /*
268                  * Prepend "./" so that dlopen will find the file in the
269                  * current directory.
270                  */
271                 snprintf(path, sizeof(path), "./%s", file);
272                 file = path;
273                 goto out;
274         }
275
276         exec_path = get_argv_exec_path();
277         if (!exec_path)
278                 goto out;
279         snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, file);
280         free(exec_path);
281         if (!access(path, R_OK))
282                 file = path;
283 out:
284         return strdup(file);
285 }
286
287 #define CHECK_FLAG(x) BUILD_BUG_ON((u64)PERF_DLFILTER_FLAG_ ## x != (u64)PERF_IP_FLAG_ ## x)
288
289 static int dlfilter__init(struct dlfilter *d, const char *file, int dlargc, char **dlargv)
290 {
291         CHECK_FLAG(BRANCH);
292         CHECK_FLAG(CALL);
293         CHECK_FLAG(RETURN);
294         CHECK_FLAG(CONDITIONAL);
295         CHECK_FLAG(SYSCALLRET);
296         CHECK_FLAG(ASYNC);
297         CHECK_FLAG(INTERRUPT);
298         CHECK_FLAG(TX_ABORT);
299         CHECK_FLAG(TRACE_BEGIN);
300         CHECK_FLAG(TRACE_END);
301         CHECK_FLAG(IN_TX);
302         CHECK_FLAG(VMENTRY);
303         CHECK_FLAG(VMEXIT);
304
305         memset(d, 0, sizeof(*d));
306         d->file = find_dlfilter(file);
307         if (!d->file)
308                 return -1;
309         d->dlargc = dlargc;
310         d->dlargv = dlargv;
311         return 0;
312 }
313
314 static void dlfilter__exit(struct dlfilter *d)
315 {
316         zfree(&d->file);
317 }
318
319 static int dlfilter__open(struct dlfilter *d)
320 {
321         d->handle = dlopen(d->file, RTLD_NOW);
322         if (!d->handle) {
323                 pr_err("dlopen failed for: '%s'\n", d->file);
324                 return -1;
325         }
326         d->start = dlsym(d->handle, "start");
327         d->filter_event = dlsym(d->handle, "filter_event");
328         d->filter_event_early = dlsym(d->handle, "filter_event_early");
329         d->stop = dlsym(d->handle, "stop");
330         d->fns = dlsym(d->handle, "perf_dlfilter_fns");
331         if (d->fns)
332                 memcpy(d->fns, &perf_dlfilter_fns, sizeof(struct perf_dlfilter_fns));
333         return 0;
334 }
335
336 static int dlfilter__close(struct dlfilter *d)
337 {
338         return dlclose(d->handle);
339 }
340
341 struct dlfilter *dlfilter__new(const char *file, int dlargc, char **dlargv)
342 {
343         struct dlfilter *d = malloc(sizeof(*d));
344
345         if (!d)
346                 return NULL;
347
348         if (dlfilter__init(d, file, dlargc, dlargv))
349                 goto err_free;
350
351         if (dlfilter__open(d))
352                 goto err_exit;
353
354         return d;
355
356 err_exit:
357         dlfilter__exit(d);
358 err_free:
359         free(d);
360         return NULL;
361 }
362
363 static void dlfilter__free(struct dlfilter *d)
364 {
365         if (d) {
366                 dlfilter__exit(d);
367                 free(d);
368         }
369 }
370
371 int dlfilter__start(struct dlfilter *d, struct perf_session *session)
372 {
373         if (d) {
374                 d->session = session;
375                 if (d->start) {
376                         int ret;
377
378                         d->in_start = true;
379                         ret = d->start(&d->data, d);
380                         d->in_start = false;
381                         return ret;
382                 }
383         }
384         return 0;
385 }
386
387 static int dlfilter__stop(struct dlfilter *d)
388 {
389         if (d && d->stop) {
390                 int ret;
391
392                 d->in_stop = true;
393                 ret = d->stop(d->data, d);
394                 d->in_stop = false;
395                 return ret;
396         }
397         return 0;
398 }
399
400 void dlfilter__cleanup(struct dlfilter *d)
401 {
402         if (d) {
403                 dlfilter__stop(d);
404                 dlfilter__close(d);
405                 dlfilter__free(d);
406         }
407 }
408
409 #define ASSIGN(x) d_sample.x = sample->x
410
411 int dlfilter__do_filter_event(struct dlfilter *d,
412                               union perf_event *event,
413                               struct perf_sample *sample,
414                               struct evsel *evsel,
415                               struct machine *machine,
416                               struct addr_location *al,
417                               struct addr_location *addr_al,
418                               bool early)
419 {
420         struct perf_dlfilter_sample d_sample;
421         struct perf_dlfilter_al d_ip_al;
422         struct perf_dlfilter_al d_addr_al;
423         int ret;
424
425         d->event       = event;
426         d->sample      = sample;
427         d->evsel       = evsel;
428         d->machine     = machine;
429         d->al          = al;
430         d->addr_al     = addr_al;
431         d->d_sample    = &d_sample;
432         d->d_ip_al     = &d_ip_al;
433         d->d_addr_al   = &d_addr_al;
434
435         d_sample.size  = sizeof(d_sample);
436         d_ip_al.size   = 0; /* To indicate d_ip_al is not initialized */
437         d_addr_al.size = 0; /* To indicate d_addr_al is not initialized */
438
439         ASSIGN(ip);
440         ASSIGN(pid);
441         ASSIGN(tid);
442         ASSIGN(time);
443         ASSIGN(addr);
444         ASSIGN(id);
445         ASSIGN(stream_id);
446         ASSIGN(period);
447         ASSIGN(weight);
448         ASSIGN(ins_lat);
449         ASSIGN(p_stage_cyc);
450         ASSIGN(transaction);
451         ASSIGN(insn_cnt);
452         ASSIGN(cyc_cnt);
453         ASSIGN(cpu);
454         ASSIGN(flags);
455         ASSIGN(data_src);
456         ASSIGN(phys_addr);
457         ASSIGN(data_page_size);
458         ASSIGN(code_page_size);
459         ASSIGN(cgroup);
460         ASSIGN(cpumode);
461         ASSIGN(misc);
462         ASSIGN(raw_size);
463         ASSIGN(raw_data);
464
465         if (sample->branch_stack) {
466                 d_sample.brstack_nr = sample->branch_stack->nr;
467                 d_sample.brstack = (struct perf_branch_entry *)perf_sample__branch_entries(sample);
468         } else {
469                 d_sample.brstack_nr = 0;
470                 d_sample.brstack = NULL;
471         }
472
473         if (sample->callchain) {
474                 d_sample.raw_callchain_nr = sample->callchain->nr;
475                 d_sample.raw_callchain = (__u64 *)sample->callchain->ips;
476         } else {
477                 d_sample.raw_callchain_nr = 0;
478                 d_sample.raw_callchain = NULL;
479         }
480
481         d_sample.addr_correlates_sym =
482                 (evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
483                 sample_addr_correlates_sym(&evsel->core.attr);
484
485         d_sample.event = evsel__name(evsel);
486
487         d->ctx_valid = true;
488
489         if (early)
490                 ret = d->filter_event_early(d->data, &d_sample, d);
491         else
492                 ret = d->filter_event(d->data, &d_sample, d);
493
494         d->ctx_valid = false;
495
496         return ret;
497 }
498
499 static bool get_filter_desc(const char *dirname, const char *name,
500                             char **desc, char **long_desc)
501 {
502         char path[PATH_MAX];
503         void *handle;
504         const char *(*desc_fn)(const char **long_description);
505
506         snprintf(path, sizeof(path), "%s/%s", dirname, name);
507         handle = dlopen(path, RTLD_NOW);
508         if (!handle || !(dlsym(handle, "filter_event") || dlsym(handle, "filter_event_early")))
509                 return false;
510         desc_fn = dlsym(handle, "filter_description");
511         if (desc_fn) {
512                 const char *dsc;
513                 const char *long_dsc;
514
515                 dsc = desc_fn(&long_dsc);
516                 if (dsc)
517                         *desc = strdup(dsc);
518                 if (long_dsc)
519                         *long_desc = strdup(long_dsc);
520         }
521         dlclose(handle);
522         return true;
523 }
524
525 static void list_filters(const char *dirname)
526 {
527         struct dirent *entry;
528         DIR *dir;
529
530         dir = opendir(dirname);
531         if (!dir)
532                 return;
533
534         while ((entry = readdir(dir)) != NULL)
535         {
536                 size_t n = strlen(entry->d_name);
537                 char *long_desc = NULL;
538                 char *desc = NULL;
539
540                 if (entry->d_type == DT_DIR || n < 4 ||
541                     strcmp(".so", entry->d_name + n - 3))
542                         continue;
543                 if (!get_filter_desc(dirname, entry->d_name, &desc, &long_desc))
544                         continue;
545                 printf("  %-36s %s\n", entry->d_name, desc ? desc : "");
546                 if (verbose) {
547                         char *p = long_desc;
548                         char *line;
549
550                         while ((line = strsep(&p, "\n")) != NULL)
551                                 printf("%39s%s\n", "", line);
552                 }
553                 free(long_desc);
554                 free(desc);
555         }
556
557         closedir(dir);
558 }
559
560 int list_available_dlfilters(const struct option *opt __maybe_unused,
561                              const char *s __maybe_unused,
562                              int unset __maybe_unused)
563 {
564         char path[PATH_MAX];
565         char *exec_path;
566
567         printf("List of available dlfilters:\n");
568
569         list_filters(".");
570
571         exec_path = get_argv_exec_path();
572         if (!exec_path)
573                 goto out;
574         snprintf(path, sizeof(path), "%s/dlfilters", exec_path);
575
576         list_filters(path);
577
578         free(exec_path);
579 out:
580         exit(0);
581 }