1 // SPDX-License-Identifier: GPL-2.0
3 * dlfilter.c: Interface to perf script --dlfilter shared object
4 * Copyright (c) 2021, Intel Corporation.
10 #include <subcmd/exec-cmd.h>
11 #include <linux/zalloc.h>
12 #include <linux/build_bug.h>
20 #include "trace-event.h"
24 #include "perf_dlfilter.h"
26 static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
28 struct symbol *sym = al->sym;
30 d_al->size = sizeof(*d_al);
32 struct dso *dso = al->map->dso;
34 if (symbol_conf.show_kernel_path && dso->long_name)
35 d_al->dso = dso->long_name;
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;
44 d_al->buildid_size = 0;
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;
54 d_al->symoff = al->addr - al->map->start - sym->start;
55 d_al->sym_binding = sym->binding;
61 d_al->sym_binding = 0;
63 d_al->addr = al->addr;
68 static struct addr_location *get_al(struct dlfilter *d)
70 struct addr_location *al = d->al;
72 if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0)
77 static struct thread *get_thread(struct dlfilter *d)
79 struct addr_location *al = get_al(d);
81 return al ? al->thread : NULL;
84 static const struct perf_dlfilter_al *dlfilter__resolve_ip(void *ctx)
86 struct dlfilter *d = (struct dlfilter *)ctx;
87 struct perf_dlfilter_al *d_al = d->d_ip_al;
88 struct addr_location *al;
93 /* 'size' is also used to indicate already initialized */
101 al_to_d_al(al, d_al);
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;
110 static const struct perf_dlfilter_al *dlfilter__resolve_addr(void *ctx)
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;
116 if (!d->ctx_valid || !d->d_sample->addr_correlates_sym)
119 /* 'size' is also used to indicate already initialized */
123 if (!addr_al->thread) {
124 struct thread *thread = get_thread(d);
128 thread__resolve(thread, addr_al, d->sample);
131 al_to_d_al(addr_al, d_addr_al);
133 d_addr_al->is_kernel_ip = machine__kernel_ip(d->machine, d->sample->addr);
138 static char **dlfilter__args(void *ctx, int *dlargc)
140 struct dlfilter *d = (struct dlfilter *)ctx;
147 if (!d->ctx_valid && !d->in_start && !d->in_stop)
154 static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfilter_al *d_al_p)
156 struct dlfilter *d = (struct dlfilter *)ctx;
157 struct perf_dlfilter_al d_al;
158 struct addr_location al;
159 struct thread *thread;
162 if (!d->ctx_valid || !d_al_p)
165 thread = get_thread(d);
169 thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
171 al_to_d_al(&al, &d_al);
173 d_al.is_kernel_ip = machine__kernel_ip(d->machine, address);
176 memcpy(d_al_p, &d_al, min((size_t)sz, sizeof(d_al)));
182 static const __u8 *dlfilter__insn(void *ctx, __u32 *len)
184 struct dlfilter *d = (struct dlfilter *)ctx;
194 if (d->sample->ip && !d->sample->insn_len) {
195 struct addr_location *al = d->al;
197 if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0)
200 if (al->thread->maps && al->thread->maps->machine)
201 script_fetch_insn(d->sample, al->thread, al->thread->maps->machine);
204 if (!d->sample->insn_len)
207 *len = d->sample->insn_len;
209 return (__u8 *)d->sample->insn;
212 static const char *dlfilter__srcline(void *ctx, __u32 *line_no)
214 struct dlfilter *d = (struct dlfilter *)ctx;
215 struct addr_location *al;
216 unsigned int line = 0;
217 char *srcfile = NULL;
221 if (!d->ctx_valid || !line_no)
232 srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line);
238 static struct perf_event_attr *dlfilter__attr(void *ctx)
240 struct dlfilter *d = (struct dlfilter *)ctx;
245 return &d->evsel->core.attr;
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,
258 static char *find_dlfilter(const char *file)
263 if (strchr(file, '/'))
266 if (!access(file, R_OK)) {
268 * Prepend "./" so that dlopen will find the file in the
271 snprintf(path, sizeof(path), "./%s", file);
276 exec_path = get_argv_exec_path();
279 snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, file);
281 if (!access(path, R_OK))
287 #define CHECK_FLAG(x) BUILD_BUG_ON((u64)PERF_DLFILTER_FLAG_ ## x != (u64)PERF_IP_FLAG_ ## x)
289 static int dlfilter__init(struct dlfilter *d, const char *file, int dlargc, char **dlargv)
294 CHECK_FLAG(CONDITIONAL);
295 CHECK_FLAG(SYSCALLRET);
297 CHECK_FLAG(INTERRUPT);
298 CHECK_FLAG(TX_ABORT);
299 CHECK_FLAG(TRACE_BEGIN);
300 CHECK_FLAG(TRACE_END);
305 memset(d, 0, sizeof(*d));
306 d->file = find_dlfilter(file);
314 static void dlfilter__exit(struct dlfilter *d)
319 static int dlfilter__open(struct dlfilter *d)
321 d->handle = dlopen(d->file, RTLD_NOW);
323 pr_err("dlopen failed for: '%s'\n", d->file);
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");
332 memcpy(d->fns, &perf_dlfilter_fns, sizeof(struct perf_dlfilter_fns));
336 static int dlfilter__close(struct dlfilter *d)
338 return dlclose(d->handle);
341 struct dlfilter *dlfilter__new(const char *file, int dlargc, char **dlargv)
343 struct dlfilter *d = malloc(sizeof(*d));
348 if (dlfilter__init(d, file, dlargc, dlargv))
351 if (dlfilter__open(d))
363 static void dlfilter__free(struct dlfilter *d)
371 int dlfilter__start(struct dlfilter *d, struct perf_session *session)
374 d->session = session;
379 ret = d->start(&d->data, d);
387 static int dlfilter__stop(struct dlfilter *d)
393 ret = d->stop(d->data, d);
400 void dlfilter__cleanup(struct dlfilter *d)
409 #define ASSIGN(x) d_sample.x = sample->x
411 int dlfilter__do_filter_event(struct dlfilter *d,
412 union perf_event *event,
413 struct perf_sample *sample,
415 struct machine *machine,
416 struct addr_location *al,
417 struct addr_location *addr_al,
420 struct perf_dlfilter_sample d_sample;
421 struct perf_dlfilter_al d_ip_al;
422 struct perf_dlfilter_al d_addr_al;
428 d->machine = machine;
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;
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 */
457 ASSIGN(data_page_size);
458 ASSIGN(code_page_size);
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);
469 d_sample.brstack_nr = 0;
470 d_sample.brstack = NULL;
473 if (sample->callchain) {
474 d_sample.raw_callchain_nr = sample->callchain->nr;
475 d_sample.raw_callchain = (__u64 *)sample->callchain->ips;
477 d_sample.raw_callchain_nr = 0;
478 d_sample.raw_callchain = NULL;
481 d_sample.addr_correlates_sym =
482 (evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
483 sample_addr_correlates_sym(&evsel->core.attr);
485 d_sample.event = evsel__name(evsel);
490 ret = d->filter_event_early(d->data, &d_sample, d);
492 ret = d->filter_event(d->data, &d_sample, d);
494 d->ctx_valid = false;
499 static bool get_filter_desc(const char *dirname, const char *name,
500 char **desc, char **long_desc)
504 const char *(*desc_fn)(const char **long_description);
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")))
510 desc_fn = dlsym(handle, "filter_description");
513 const char *long_dsc;
515 dsc = desc_fn(&long_dsc);
519 *long_desc = strdup(long_dsc);
525 static void list_filters(const char *dirname)
527 struct dirent *entry;
530 dir = opendir(dirname);
534 while ((entry = readdir(dir)) != NULL)
536 size_t n = strlen(entry->d_name);
537 char *long_desc = NULL;
540 if (entry->d_type == DT_DIR || n < 4 ||
541 strcmp(".so", entry->d_name + n - 3))
543 if (!get_filter_desc(dirname, entry->d_name, &desc, &long_desc))
545 printf(" %-36s %s\n", entry->d_name, desc ? desc : "");
550 while ((line = strsep(&p, "\n")) != NULL)
551 printf("%39s%s\n", "", line);
560 int list_available_dlfilters(const struct option *opt __maybe_unused,
561 const char *s __maybe_unused,
562 int unset __maybe_unused)
567 printf("List of available dlfilters:\n");
571 exec_path = get_argv_exec_path();
574 snprintf(path, sizeof(path), "%s/dlfilters", exec_path);