openat-syscall-tp-fields.c 2.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <linux/err.h>
3 4 5
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
6 7 8 9 10
#include "perf.h"
#include "evlist.h"
#include "evsel.h"
#include "thread_map.h"
#include "tests.h"
11
#include "debug.h"
12
#include <errno.h>
13

14 15 16 17 18 19 20
#ifndef O_DIRECTORY
#define O_DIRECTORY    00200000
#endif
#ifndef AT_FDCWD
#define AT_FDCWD       -100
#endif

21
int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
22
{
23
	struct record_opts opts = {
24 25 26 27
		.target = {
			.uid = UINT_MAX,
			.uses_mmap = true,
		},
28 29 30 31
		.no_buffering = true,
		.freq	      = 1,
		.mmap_pages   = 256,
		.raw_samples  = true,
32 33 34
	};
	const char *filename = "/etc/passwd";
	int flags = O_RDONLY | O_DIRECTORY;
35
	struct evlist *evlist = evlist__new();
36
	struct evsel *evsel;
37
	int err = -1, i, nr_events = 0, nr_polls = 0;
38
	char sbuf[STRERR_BUFSIZE];
39 40 41 42 43 44

	if (evlist == NULL) {
		pr_debug("%s: perf_evlist__new\n", __func__);
		goto out;
	}

45
	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
46
	if (IS_ERR(evsel)) {
47 48 49 50 51 52 53 54 55 56 57 58
		pr_debug("%s: perf_evsel__newtp\n", __func__);
		goto out_delete_evlist;
	}

	perf_evlist__add(evlist, evsel);

	err = perf_evlist__create_maps(evlist, &opts.target);
	if (err < 0) {
		pr_debug("%s: perf_evlist__create_maps\n", __func__);
		goto out_delete_evlist;
	}

59
	perf_evsel__config(evsel, &opts, NULL);
60

61
	thread_map__set_pid(evlist->threads, 0, getpid());
62 63 64

	err = perf_evlist__open(evlist);
	if (err < 0) {
65
		pr_debug("perf_evlist__open: %s\n",
66
			 str_error_r(errno, sbuf, sizeof(sbuf)));
67
		goto out_delete_evlist;
68 69
	}

70
	err = perf_evlist__mmap(evlist, UINT_MAX);
71
	if (err < 0) {
72
		pr_debug("perf_evlist__mmap: %s\n",
73
			 str_error_r(errno, sbuf, sizeof(sbuf)));
74
		goto out_delete_evlist;
75 76 77 78 79 80 81
	}

	perf_evlist__enable(evlist);

	/*
	 * Generate the event:
	 */
82
	openat(AT_FDCWD, filename, flags);
83 84 85 86 87 88

	while (1) {
		int before = nr_events;

		for (i = 0; i < evlist->nr_mmaps; i++) {
			union perf_event *event;
89
			struct perf_mmap *md;
90

91
			md = &evlist->mmap[i];
92
			if (perf_mmap__read_init(md) < 0)
93 94
				continue;

95
			while ((event = perf_mmap__read_event(md)) != NULL) {
96 97 98 99 100 101
				const u32 type = event->header.type;
				int tp_flags;
				struct perf_sample sample;

				++nr_events;

102
				if (type != PERF_RECORD_SAMPLE) {
103
					perf_mmap__consume(md);
104
					continue;
105
				}
106 107 108

				err = perf_evsel__parse_sample(evsel, event, &sample);
				if (err) {
109
					pr_debug("Can't parse sample, err = %d\n", err);
110
					goto out_delete_evlist;
111 112 113 114 115 116 117
				}

				tp_flags = perf_evsel__intval(evsel, &sample, "flags");

				if (flags != tp_flags) {
					pr_debug("%s: Expected flags=%#x, got %#x\n",
						 __func__, flags, tp_flags);
118
					goto out_delete_evlist;
119 120 121 122
				}

				goto out_ok;
			}
123
			perf_mmap__read_done(md);
124 125 126
		}

		if (nr_events == before)
127
			perf_evlist__poll(evlist, 10);
128 129 130

		if (++nr_polls > 5) {
			pr_debug("%s: no events!\n", __func__);
131
			goto out_delete_evlist;
132 133 134 135 136 137 138 139 140
		}
	}
out_ok:
	err = 0;
out_delete_evlist:
	perf_evlist__delete(evlist);
out:
	return err;
}