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 __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
250 struct dlfilter *d = (struct dlfilter *)ctx;
251 struct addr_location *al;
252 struct addr_location a;
265 if (map && ip >= map->start && ip < map->end &&
266 machine__kernel_ip(d->machine, ip) == machine__kernel_ip(d->machine, d->sample->ip))
269 thread__find_map_fb(al->thread, d->sample->cpumode, ip, &a);
275 offset = map->map_ip(map, ip);
276 if (ip + len >= map->end)
278 return dso__data_read_offset(map->dso, d->machine, offset, buf, len);
281 static const struct perf_dlfilter_fns perf_dlfilter_fns = {
282 .resolve_ip = dlfilter__resolve_ip,
283 .resolve_addr = dlfilter__resolve_addr,
284 .args = dlfilter__args,
285 .resolve_address = dlfilter__resolve_address,
286 .insn = dlfilter__insn,
287 .srcline = dlfilter__srcline,
288 .attr = dlfilter__attr,
289 .object_code = dlfilter__object_code,
292 static char *find_dlfilter(const char *file)
297 if (strchr(file, '/'))
300 if (!access(file, R_OK)) {
302 * Prepend "./" so that dlopen will find the file in the
305 snprintf(path, sizeof(path), "./%s", file);
310 exec_path = get_argv_exec_path();
313 snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, file);
315 if (!access(path, R_OK))
321 #define CHECK_FLAG(x) BUILD_BUG_ON((u64)PERF_DLFILTER_FLAG_ ## x != (u64)PERF_IP_FLAG_ ## x)
323 static int dlfilter__init(struct dlfilter *d, const char *file, int dlargc, char **dlargv)
328 CHECK_FLAG(CONDITIONAL);
329 CHECK_FLAG(SYSCALLRET);
331 CHECK_FLAG(INTERRUPT);
332 CHECK_FLAG(TX_ABORT);
333 CHECK_FLAG(TRACE_BEGIN);
334 CHECK_FLAG(TRACE_END);
339 memset(d, 0, sizeof(*d));
340 d->file = find_dlfilter(file);
348 static void dlfilter__exit(struct dlfilter *d)
353 static int dlfilter__open(struct dlfilter *d)
355 d->handle = dlopen(d->file, RTLD_NOW);
357 pr_err("dlopen failed for: '%s'\n", d->file);
360 d->start = dlsym(d->handle, "start");
361 d->filter_event = dlsym(d->handle, "filter_event");
362 d->filter_event_early = dlsym(d->handle, "filter_event_early");
363 d->stop = dlsym(d->handle, "stop");
364 d->fns = dlsym(d->handle, "perf_dlfilter_fns");
366 memcpy(d->fns, &perf_dlfilter_fns, sizeof(struct perf_dlfilter_fns));
370 static int dlfilter__close(struct dlfilter *d)
372 return dlclose(d->handle);
375 struct dlfilter *dlfilter__new(const char *file, int dlargc, char **dlargv)
377 struct dlfilter *d = malloc(sizeof(*d));
382 if (dlfilter__init(d, file, dlargc, dlargv))
385 if (dlfilter__open(d))
397 static void dlfilter__free(struct dlfilter *d)
405 int dlfilter__start(struct dlfilter *d, struct perf_session *session)
408 d->session = session;
413 ret = d->start(&d->data, d);
421 static int dlfilter__stop(struct dlfilter *d)
427 ret = d->stop(d->data, d);
434 void dlfilter__cleanup(struct dlfilter *d)
443 #define ASSIGN(x) d_sample.x = sample->x
445 int dlfilter__do_filter_event(struct dlfilter *d,
446 union perf_event *event,
447 struct perf_sample *sample,
449 struct machine *machine,
450 struct addr_location *al,
451 struct addr_location *addr_al,
454 struct perf_dlfilter_sample d_sample;
455 struct perf_dlfilter_al d_ip_al;
456 struct perf_dlfilter_al d_addr_al;
462 d->machine = machine;
464 d->addr_al = addr_al;
465 d->d_sample = &d_sample;
466 d->d_ip_al = &d_ip_al;
467 d->d_addr_al = &d_addr_al;
469 d_sample.size = sizeof(d_sample);
470 d_ip_al.size = 0; /* To indicate d_ip_al is not initialized */
471 d_addr_al.size = 0; /* To indicate d_addr_al is not initialized */
491 ASSIGN(data_page_size);
492 ASSIGN(code_page_size);
499 if (sample->branch_stack) {
500 d_sample.brstack_nr = sample->branch_stack->nr;
501 d_sample.brstack = (struct perf_branch_entry *)perf_sample__branch_entries(sample);
503 d_sample.brstack_nr = 0;
504 d_sample.brstack = NULL;
507 if (sample->callchain) {
508 d_sample.raw_callchain_nr = sample->callchain->nr;
509 d_sample.raw_callchain = (__u64 *)sample->callchain->ips;
511 d_sample.raw_callchain_nr = 0;
512 d_sample.raw_callchain = NULL;
515 d_sample.addr_correlates_sym =
516 (evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
517 sample_addr_correlates_sym(&evsel->core.attr);
519 d_sample.event = evsel__name(evsel);
524 ret = d->filter_event_early(d->data, &d_sample, d);
526 ret = d->filter_event(d->data, &d_sample, d);
528 d->ctx_valid = false;
533 static bool get_filter_desc(const char *dirname, const char *name,
534 char **desc, char **long_desc)
538 const char *(*desc_fn)(const char **long_description);
540 snprintf(path, sizeof(path), "%s/%s", dirname, name);
541 handle = dlopen(path, RTLD_NOW);
542 if (!handle || !(dlsym(handle, "filter_event") || dlsym(handle, "filter_event_early")))
544 desc_fn = dlsym(handle, "filter_description");
547 const char *long_dsc;
549 dsc = desc_fn(&long_dsc);
553 *long_desc = strdup(long_dsc);
559 static void list_filters(const char *dirname)
561 struct dirent *entry;
564 dir = opendir(dirname);
568 while ((entry = readdir(dir)) != NULL)
570 size_t n = strlen(entry->d_name);
571 char *long_desc = NULL;
574 if (entry->d_type == DT_DIR || n < 4 ||
575 strcmp(".so", entry->d_name + n - 3))
577 if (!get_filter_desc(dirname, entry->d_name, &desc, &long_desc))
579 printf(" %-36s %s\n", entry->d_name, desc ? desc : "");
584 while ((line = strsep(&p, "\n")) != NULL)
585 printf("%39s%s\n", "", line);
594 int list_available_dlfilters(const struct option *opt __maybe_unused,
595 const char *s __maybe_unused,
596 int unset __maybe_unused)
601 printf("List of available dlfilters:\n");
605 exec_path = get_argv_exec_path();
608 snprintf(path, sizeof(path), "%s/dlfilters", exec_path);