aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/perf/util/dlfilter.c
blob: 908e16813722d76f275d50e81aa0a596dec554fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// SPDX-License-Identifier: GPL-2.0
/*
 * dlfilter.c: Interface to perf script --dlfilter shared object
 * Copyright (c) 2021, Intel Corporation.
 */
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <subcmd/exec-cmd.h>
#include <linux/zalloc.h>
#include <linux/build_bug.h>
#include <linux/kernel.h>
#include <linux/string.h>

#include "debug.h"
#include "event.h"
#include "evsel.h"
#include "dso.h"
#include "map.h"
#include "thread.h"
#include "trace-event.h"
#include "symbol.h"
#include "srcline.h"
#include "dlfilter.h"
#include "../include/perf/perf_dlfilter.h"

static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
{
	struct symbol *sym = al->sym;

	d_al->size = sizeof(*d_al);
	if (al->map) {
		struct dso *dso = map__dso(al->map);

		if (symbol_conf.show_kernel_path && dso->long_name)
			d_al->dso = dso->long_name;
		else
			d_al->dso = dso->name;
		d_al->is_64_bit = dso->is_64_bit;
		d_al->buildid_size = dso->bid.size;
		d_al->buildid = dso->bid.data;
	} else {
		d_al->dso = NULL;
		d_al->is_64_bit = 0;
		d_al->buildid_size = 0;
		d_al->buildid = NULL;
	}
	if (sym) {
		d_al->sym = sym->name;
		d_al->sym_start = sym->start;
		d_al->sym_end = sym->end;
		if (al->addr < sym->end)
			d_al->symoff = al->addr - sym->start;
		else if (al->map)
			d_al->symoff = al->addr - map__start(al->map) - sym->start;
		else
			d_al->symoff = 0;
		d_al->sym_binding = sym->binding;
	} else {
		d_al->sym = NULL;
		d_al->sym_start = 0;
		d_al->sym_end = 0;
		d_al->symoff = 0;
		d_al->sym_binding = 0;
	}
	d_al->addr = al->addr;
	d_al->comm = NULL;
	d_al->filtered = 0;
	d_al->priv = NULL;
}

static struct addr_location *get_al(struct dlfilter *d)
{
	struct addr_location *al = d->al;

	if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0)
		return NULL;
	return al;
}

static struct thread *get_thread(struct dlfilter *d)
{
	struct addr_location *al = get_al(d);

	return al ? al->thread : NULL;
}

static const struct perf_dlfilter_al *dlfilter__resolve_ip(void *ctx)
{
	struct dlfilter *d = (struct dlfilter *)ctx;
	struct perf_dlfilter_al *d_al = d->d_ip_al;
	struct addr_location *al;

	if (!d->ctx_valid)
		return NULL;

	/* 'size' is also used to indicate already initialized */
	if (d_al->size)
		return d_al;

	al = get_al(d);
	if (!al)
		return NULL;

	al_to_d_al(al, d_al);

	d_al->is_kernel_ip = machine__kernel_ip(d->machine, d->sample->ip);
	d_al->comm = al->thread ? thread__comm_str(al->thread) : ":-1";
	d_al->filtered = al->filtered;

	return d_al;
}

static const struct perf_dlfilter_al *dlfilter__resolve_addr(void *ctx)
{
	struct dlfilter *d = (struct dlfilter *)ctx;
	struct perf_dlfilter_al *d_addr_al = d->d_addr_al;
	struct addr_location *addr_al = d->addr_al;

	if (!d->ctx_valid || !d->d_sample->addr_correlates_sym)
		return NULL;

	/* 'size' is also used to indicate already initialized */
	if (d_addr_al->size)
		return d_addr_al;

	if (!addr_al->thread) {
		struct thread *thread = get_thread(d);

		if (!thread)
			return NULL;
		thread__resolve(thread, addr_al, d->sample);
	}

	al_to_d_al(addr_al, d_addr_al);

	d_addr_al->is_kernel_ip = machine__kernel_ip(d->machine, d->sample->addr);

	return d_addr_al;
}

static char **dlfilter__args(void *ctx, int *dlargc)
{
	struct dlfilter *d = (struct dlfilter *)ctx;

	if (dlargc)
		*dlargc = 0;
	else
		return NULL;

	if (!d->ctx_valid && !d->in_start && !d->in_stop)
		return NULL;

	*dlargc = d->dlargc;
	return d->dlargv;
}

static bool has_priv(struct perf_dlfilter_al *d_al_p)
{
	return d_al_p->size >= offsetof(struct perf_dlfilter_al, priv) + sizeof(d_al_p->priv);
}

static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfilter_al *d_al_p)
{
	struct dlfilter *d = (struct dlfilter *)ctx;
	struct perf_dlfilter_al d_al;
	struct addr_location al;
	struct thread *thread;
	__u32 sz;

	if (!d->ctx_valid || !d_al_p)
		return -1;

	thread = get_thread(d);
	if (!thread)
		return -1;

	addr_location__init(&al);
	thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);

	al_to_d_al(&al, &d_al);

	d_al.is_kernel_ip = machine__kernel_ip(d->machine, address);

	sz = d_al_p->size;
	memcpy(d_al_p, &d_al, min((size_t)sz, sizeof(d_al)));
	d_al_p->size = sz;

	if (has_priv(d_al_p))
		d_al_p->priv = memdup(&al, sizeof(al));
	else /* Avoid leak for v0 API */
		addr_location__exit(&al);

	return 0;
}

static void dlfilter__al_cleanup(void *ctx __maybe_unused, struct perf_dlfilter_al *d_al_p)
{
	struct addr_location *al;

	/* Ensure backward compatibility */
	if (!has_priv(d_al_p) || !d_al_p->priv)
		return;

	al = d_al_p->priv;

	d_al_p->priv = NULL;

	addr_location__exit(al);

	free(al);
}

static const __u8 *dlfilter__insn(void *ctx, __u32 *len)
{
	struct dlfilter *d = (struct dlfilter *)ctx;

	if (!len)
		return NULL;

	*len = 0;

	if (!d->ctx_valid)
		return NULL;

	if (d->sample->ip && !d->sample->insn_len) {
		struct addr_location *al = d->al;

		if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0)
			return NULL;

		if (thread__maps(al->thread)) {
			struct machine *machine = maps__machine(thread__maps(al->thread));

			if (machine)
				script_fetch_insn(d->sample, al->thread, machine);
		}
	}

	if (!d->sample->insn_len)
		return NULL;

	*len = d->sample->insn_len;

	return (__u8 *)d->sample->insn;
}

static const char *dlfilter__srcline(void *ctx, __u32 *line_no)
{
	struct dlfilter *d = (struct dlfilter *)ctx;
	struct addr_location *al;
	unsigned int line = 0;
	char *srcfile = NULL;
	struct map *map;
	struct dso *dso;
	u64 addr;

	if (!d->ctx_valid || !line_no)
		return NULL;

	al = get_al(d);
	if (!al)
		return NULL;

	map = al->map;
	addr = al->addr;
	dso = map ? map__dso(map) : NULL;

	if (dso)
		srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);

	*line_no = line;
	return srcfile;
}

static struct perf_event_attr *dlfilter__attr(void *ctx)
{
	struct dlfilter *d = (struct dlfilter *)ctx;

	if (!d->ctx_valid)
		return NULL;

	return &d->evsel->core.attr;
}

static __s32 code_read(__u64 ip, struct map *map, struct machine *machine, void *buf, __u32 len)
{
	u64 offset = map__map_ip(map, ip);

	if (ip + len >= map__end(map))
		len = map__end(map) - ip;

	return dso__data_read_offset(map__dso(map), machine, offset, buf, len);
}

static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
{
	struct dlfilter *d = (struct dlfilter *)ctx;
	struct addr_location *al;
	struct addr_location a;
	__s32 ret;

	if (!d->ctx_valid)
		return -1;

	al = get_al(d);
	if (!al)
		return -1;

	if (al->map && ip >= map__start(al->map) && ip < map__end(al->map) &&
	    machine__kernel_ip(d->machine, ip) == machine__kernel_ip(d->machine, d->sample->ip))
		return code_read(ip, al->map, d->machine, buf, len);

	addr_location__init(&a);

	thread__find_map_fb(al->thread, d->sample->cpumode, ip, &a);
	ret = a.map ? code_read(ip, a.map, d->machine, buf, len) : -1;

	addr_location__exit(&a);

	return ret;
}

static const struct perf_dlfilter_fns perf_dlfilter_fns = {
	.resolve_ip      = dlfilter__resolve_ip,
	.resolve_addr    = dlfilter__resolve_addr,
	.args            = dlfilter__args,
	.resolve_address = dlfilter__resolve_address,
	.al_cleanup      = dlfilter__al_cleanup,
	.insn            = dlfilter__insn,
	.srcline         = dlfilter__srcline,
	.attr            = dlfilter__attr,
	.object_code     = dlfilter__object_code,
};

static char *find_dlfilter(const char *file)
{
	char path[PATH_MAX];
	char *exec_path;

	if (strchr(file, '/'))
		goto out;

	if (!access(file, R_OK)) {
		/*
		 * Prepend "./" so that dlopen will find the file in the
		 * current directory.
		 */
		snprintf(path, sizeof(path), "./%s", file);
		file = path;
		goto out;
	}

	exec_path = get_argv_exec_path();
	if (!exec_path)
		goto out;
	snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, file);
	free(exec_path);
	if (!access(path, R_OK))
		file = path;
out:
	return strdup(file);
}

#define CHECK_FLAG(x) BUILD_BUG_ON((u64)PERF_DLFILTER_FLAG_ ## x != (u64)PERF_IP_FLAG_ ## x)

static int dlfilter__init(struct dlfilter *d, const char *file, int dlargc, char **dlargv)
{
	CHECK_FLAG(BRANCH);
	CHECK_FLAG(CALL);
	CHECK_FLAG(RETURN);
	CHECK_FLAG(CONDITIONAL);
	CHECK_FLAG(SYSCALLRET);
	CHECK_FLAG(ASYNC);
	CHECK_FLAG(INTERRUPT);
	CHECK_FLAG(TX_ABORT);
	CHECK_FLAG(TRACE_BEGIN);
	CHECK_FLAG(TRACE_END);
	CHECK_FLAG(IN_TX);
	CHECK_FLAG(VMENTRY);
	CHECK_FLAG(VMEXIT);

	memset(d, 0, sizeof(*d));
	d->file = find_dlfilter(file);
	if (!d->file)
		return -1;
	d->dlargc = dlargc;
	d->dlargv = dlargv;
	return 0;
}

static void dlfilter__exit(struct dlfilter *d)
{
	zfree(&d->file);
}

static int dlfilter__open(struct dlfilter *d)
{
	d->handle = dlopen(d->file, RTLD_NOW);
	if (!d->handle) {
		pr_err("dlopen failed for: '%s'\n", d->file);
		return -1;
	}
	d->start = dlsym(d->handle, "start");
	d->filter_event = dlsym(d->handle, "filter_event");
	d->filter_event_early = dlsym(d->handle, "filter_event_early");
	d->stop = dlsym(d->handle, "stop");
	d->fns = dlsym(d->handle, "perf_dlfilter_fns");
	if (d->fns)
		memcpy(d->fns, &perf_dlfilter_fns, sizeof(struct perf_dlfilter_fns));
	return 0;
}

static int dlfilter__close(struct dlfilter *d)
{
	return dlclose(d->handle);
}

struct dlfilter *dlfilter__new(const char *file, int dlargc, char **dlargv)
{
	struct dlfilter *d = malloc(sizeof(*d));

	if (!d)
		return NULL;

	if (dlfilter__init(d, file, dlargc, dlargv))
		goto err_free;

	if (dlfilter__open(d))
		goto err_exit;

	return d;

err_exit:
	dlfilter__exit(d);
err_free:
	free(d);
	return NULL;
}

static void dlfilter__free(struct dlfilter *d)
{
	if (d) {
		dlfilter__exit(d);
		free(d);
	}
}

int dlfilter__start(struct dlfilter *d, struct perf_session *session)
{
	if (d) {
		d->session = session;
		if (d->start) {
			int ret;

			d->in_start = true;
			ret = d->start(&d->data, d);
			d->in_start = false;
			return ret;
		}
	}
	return 0;
}

static int dlfilter__stop(struct dlfilter *d)
{
	if (d && d->stop) {
		int ret;

		d->in_stop = true;
		ret = d->stop(d->data, d);
		d->in_stop = false;
		return ret;
	}
	return 0;
}

void dlfilter__cleanup(struct dlfilter *d)
{
	if (d) {
		dlfilter__stop(d);
		dlfilter__close(d);
		dlfilter__free(d);
	}
}

#define ASSIGN(x) d_sample.x = sample->x

int dlfilter__do_filter_event(struct dlfilter *d,
			      union perf_event *event,
			      struct perf_sample *sample,
			      struct evsel *evsel,
			      struct machine *machine,
			      struct addr_location *al,
			      struct addr_location *addr_al,
			      bool early)
{
	struct perf_dlfilter_sample d_sample;
	struct perf_dlfilter_al d_ip_al;
	struct perf_dlfilter_al d_addr_al;
	int ret;

	d->event       = event;
	d->sample      = sample;
	d->evsel       = evsel;
	d->machine     = machine;
	d->al          = al;
	d->addr_al     = addr_al;
	d->d_sample    = &d_sample;
	d->d_ip_al     = &d_ip_al;
	d->d_addr_al   = &d_addr_al;

	d_sample.size  = sizeof(d_sample);
	d_ip_al.size   = 0; /* To indicate d_ip_al is not initialized */
	d_addr_al.size = 0; /* To indicate d_addr_al is not initialized */

	ASSIGN(ip);
	ASSIGN(pid);
	ASSIGN(tid);
	ASSIGN(time);
	ASSIGN(addr);
	ASSIGN(id);
	ASSIGN(stream_id);
	ASSIGN(period);
	ASSIGN(weight);
	ASSIGN(ins_lat);
	ASSIGN(p_stage_cyc);
	ASSIGN(transaction);
	ASSIGN(insn_cnt);
	ASSIGN(cyc_cnt);
	ASSIGN(cpu);
	ASSIGN(flags);
	ASSIGN(data_src);
	ASSIGN(phys_addr);
	ASSIGN(data_page_size);
	ASSIGN(code_page_size);
	ASSIGN(cgroup);
	ASSIGN(cpumode);
	ASSIGN(misc);
	ASSIGN(raw_size);
	ASSIGN(raw_data);
	ASSIGN(machine_pid);
	ASSIGN(vcpu);

	if (sample->branch_stack) {
		d_sample.brstack_nr = sample->branch_stack->nr;
		d_sample.brstack = (struct perf_branch_entry *)perf_sample__branch_entries(sample);
	} else {
		d_sample.brstack_nr = 0;
		d_sample.brstack = NULL;
	}

	if (sample->callchain) {
		d_sample.raw_callchain_nr = sample->callchain->nr;
		d_sample.raw_callchain = (__u64 *)sample->callchain->ips;
	} else {
		d_sample.raw_callchain_nr = 0;
		d_sample.raw_callchain = NULL;
	}

	d_sample.addr_correlates_sym =
		(evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
		sample_addr_correlates_sym(&evsel->core.attr);

	d_sample.event = evsel__name(evsel);

	d->ctx_valid = true;

	if (early)
		ret = d->filter_event_early(d->data, &d_sample, d);
	else
		ret = d->filter_event(d->data, &d_sample, d);

	d->ctx_valid = false;

	return ret;
}

bool get_filter_desc(const char *dirname, const char *name, char **desc,
		     char **long_desc)
{
	char path[PATH_MAX];
	void *handle;
	const char *(*desc_fn)(const char **long_description);

	snprintf(path, sizeof(path), "%s/%s", dirname, name);
	handle = dlopen(path, RTLD_NOW);
	if (!handle || !(dlsym(handle, "filter_event") || dlsym(handle, "filter_event_early")))
		return false;
	desc_fn = dlsym(handle, "filter_description");
	if (desc_fn) {
		const char *dsc;
		const char *long_dsc;

		dsc = desc_fn(&long_dsc);
		if (dsc)
			*desc = strdup(dsc);
		if (long_dsc)
			*long_desc = strdup(long_dsc);
	}
	dlclose(handle);
	return true;
}

static void list_filters(const char *dirname)
{
	struct dirent *entry;
	DIR *dir;

	dir = opendir(dirname);
	if (!dir)
		return;

	while ((entry = readdir(dir)) != NULL)
	{
		size_t n = strlen(entry->d_name);
		char *long_desc = NULL;
		char *desc = NULL;

		if (entry->d_type == DT_DIR || n < 4 ||
		    strcmp(".so", entry->d_name + n - 3))
			continue;
		if (!get_filter_desc(dirname, entry->d_name, &desc, &long_desc))
			continue;
		printf("  %-36s %s\n", entry->d_name, desc ? desc : "");
		if (verbose > 0) {
			char *p = long_desc;
			char *line;

			while ((line = strsep(&p, "\n")) != NULL)
				printf("%39s%s\n", "", line);
		}
		free(long_desc);
		free(desc);
	}

	closedir(dir);
}

int list_available_dlfilters(const struct option *opt __maybe_unused,
			     const char *s __maybe_unused,
			     int unset __maybe_unused)
{
	char path[PATH_MAX];
	char *exec_path;

	printf("List of available dlfilters:\n");

	list_filters(".");

	exec_path = get_argv_exec_path();
	if (!exec_path)
		goto out;
	snprintf(path, sizeof(path), "%s/dlfilters", exec_path);

	list_filters(path);

	free(exec_path);
out:
	exit(0);
}