xref: /openbmc/linux/tools/perf/tests/openat-syscall-tp-fields.c (revision 32fdc2ca7e2ae8ae5d0ff660ca7783acd8ee6396)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2aeb00b1aSArnaldo Carvalho de Melo #include <stdbool.h>
38dd2a131SJiri Olsa #include <linux/err.h>
48520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
5bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
6bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
7bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
8fbb2df23SRiku Voipio #include "evlist.h"
9fbb2df23SRiku Voipio #include "evsel.h"
10fbb2df23SRiku Voipio #include "thread_map.h"
11aeb00b1aSArnaldo Carvalho de Melo #include "record.h"
12fbb2df23SRiku Voipio #include "tests.h"
13fbb2df23SRiku Voipio #include "debug.h"
14e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
15a43783aeSArnaldo Carvalho de Melo #include <errno.h>
167728fa0cSJiri Olsa #include <perf/mmap.h>
17fbb2df23SRiku Voipio 
18ab6526b2SArnaldo Carvalho de Melo #ifndef O_DIRECTORY
19ab6526b2SArnaldo Carvalho de Melo #define O_DIRECTORY    00200000
20ab6526b2SArnaldo Carvalho de Melo #endif
21ab6526b2SArnaldo Carvalho de Melo #ifndef AT_FDCWD
22ab6526b2SArnaldo Carvalho de Melo #define AT_FDCWD       -100
23ab6526b2SArnaldo Carvalho de Melo #endif
24ab6526b2SArnaldo Carvalho de Melo 
2581f17c90SArnaldo Carvalho de Melo int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
26fbb2df23SRiku Voipio {
27fbb2df23SRiku Voipio 	struct record_opts opts = {
28fbb2df23SRiku Voipio 		.target = {
29fbb2df23SRiku Voipio 			.uid = UINT_MAX,
30fbb2df23SRiku Voipio 			.uses_mmap = true,
31fbb2df23SRiku Voipio 		},
32fbb2df23SRiku Voipio 		.no_buffering = true,
33fbb2df23SRiku Voipio 		.freq	      = 1,
34fbb2df23SRiku Voipio 		.mmap_pages   = 256,
35fbb2df23SRiku Voipio 		.raw_samples  = true,
36fbb2df23SRiku Voipio 	};
37fbb2df23SRiku Voipio 	const char *filename = "/etc/passwd";
38fbb2df23SRiku Voipio 	int flags = O_RDONLY | O_DIRECTORY;
390f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
4032dcd021SJiri Olsa 	struct evsel *evsel;
41fbb2df23SRiku Voipio 	int err = -1, i, nr_events = 0, nr_polls = 0;
42fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
43fbb2df23SRiku Voipio 
44fbb2df23SRiku Voipio 	if (evlist == NULL) {
45fbb2df23SRiku Voipio 		pr_debug("%s: perf_evlist__new\n", __func__);
46fbb2df23SRiku Voipio 		goto out;
47fbb2df23SRiku Voipio 	}
48fbb2df23SRiku Voipio 
49fbb2df23SRiku Voipio 	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
508dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
51fbb2df23SRiku Voipio 		pr_debug("%s: perf_evsel__newtp\n", __func__);
52fbb2df23SRiku Voipio 		goto out_delete_evlist;
53fbb2df23SRiku Voipio 	}
54fbb2df23SRiku Voipio 
55a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
56fbb2df23SRiku Voipio 
57fbb2df23SRiku Voipio 	err = perf_evlist__create_maps(evlist, &opts.target);
58fbb2df23SRiku Voipio 	if (err < 0) {
59fbb2df23SRiku Voipio 		pr_debug("%s: perf_evlist__create_maps\n", __func__);
60fbb2df23SRiku Voipio 		goto out_delete_evlist;
61fbb2df23SRiku Voipio 	}
62fbb2df23SRiku Voipio 
63e68ae9cfSArnaldo Carvalho de Melo 	perf_evsel__config(evsel, &opts, NULL);
64fbb2df23SRiku Voipio 
6503617c22SJiri Olsa 	perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
66fbb2df23SRiku Voipio 
67474ddc4cSJiri Olsa 	err = evlist__open(evlist);
68fbb2df23SRiku Voipio 	if (err < 0) {
69fbb2df23SRiku Voipio 		pr_debug("perf_evlist__open: %s\n",
70c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
71fbb2df23SRiku Voipio 		goto out_delete_evlist;
72fbb2df23SRiku Voipio 	}
73fbb2df23SRiku Voipio 
749521b5f2SJiri Olsa 	err = evlist__mmap(evlist, UINT_MAX);
75fbb2df23SRiku Voipio 	if (err < 0) {
769521b5f2SJiri Olsa 		pr_debug("evlist__mmap: %s\n",
77c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
78fbb2df23SRiku Voipio 		goto out_delete_evlist;
79fbb2df23SRiku Voipio 	}
80fbb2df23SRiku Voipio 
811c87f165SJiri Olsa 	evlist__enable(evlist);
82fbb2df23SRiku Voipio 
83fbb2df23SRiku Voipio 	/*
84fbb2df23SRiku Voipio 	 * Generate the event:
85fbb2df23SRiku Voipio 	 */
86fbb2df23SRiku Voipio 	openat(AT_FDCWD, filename, flags);
87fbb2df23SRiku Voipio 
88fbb2df23SRiku Voipio 	while (1) {
89fbb2df23SRiku Voipio 		int before = nr_events;
90fbb2df23SRiku Voipio 
91c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
92fbb2df23SRiku Voipio 			union perf_event *event;
93a5830532SJiri Olsa 			struct mmap *md;
94fbb2df23SRiku Voipio 
951d1b5632SKan Liang 			md = &evlist->mmap[i];
967c4d4182SJiri Olsa 			if (perf_mmap__read_init(&md->core) < 0)
971d1b5632SKan Liang 				continue;
981d1b5632SKan Liang 
990019dc87SKan Liang 			while ((event = perf_mmap__read_event(md)) != NULL) {
100fbb2df23SRiku Voipio 				const u32 type = event->header.type;
101fbb2df23SRiku Voipio 				int tp_flags;
102fbb2df23SRiku Voipio 				struct perf_sample sample;
103fbb2df23SRiku Voipio 
104fbb2df23SRiku Voipio 				++nr_events;
105fbb2df23SRiku Voipio 
106fbb2df23SRiku Voipio 				if (type != PERF_RECORD_SAMPLE) {
1077728fa0cSJiri Olsa 					perf_mmap__consume(&md->core);
108fbb2df23SRiku Voipio 					continue;
109fbb2df23SRiku Voipio 				}
110fbb2df23SRiku Voipio 
111fbb2df23SRiku Voipio 				err = perf_evsel__parse_sample(evsel, event, &sample);
112fbb2df23SRiku Voipio 				if (err) {
11387191383SNamhyung Kim 					pr_debug("Can't parse sample, err = %d\n", err);
114fbb2df23SRiku Voipio 					goto out_delete_evlist;
115fbb2df23SRiku Voipio 				}
116fbb2df23SRiku Voipio 
117fbb2df23SRiku Voipio 				tp_flags = perf_evsel__intval(evsel, &sample, "flags");
118fbb2df23SRiku Voipio 
119fbb2df23SRiku Voipio 				if (flags != tp_flags) {
120fbb2df23SRiku Voipio 					pr_debug("%s: Expected flags=%#x, got %#x\n",
121fbb2df23SRiku Voipio 						 __func__, flags, tp_flags);
122fbb2df23SRiku Voipio 					goto out_delete_evlist;
123fbb2df23SRiku Voipio 				}
124fbb2df23SRiku Voipio 
125fbb2df23SRiku Voipio 				goto out_ok;
126fbb2df23SRiku Voipio 			}
127*32fdc2caSJiri Olsa 			perf_mmap__read_done(&md->core);
128fbb2df23SRiku Voipio 		}
129fbb2df23SRiku Voipio 
130fbb2df23SRiku Voipio 		if (nr_events == before)
13180ab2987SJiri Olsa 			evlist__poll(evlist, 10);
132fbb2df23SRiku Voipio 
133fbb2df23SRiku Voipio 		if (++nr_polls > 5) {
134fbb2df23SRiku Voipio 			pr_debug("%s: no events!\n", __func__);
135fbb2df23SRiku Voipio 			goto out_delete_evlist;
136fbb2df23SRiku Voipio 		}
137fbb2df23SRiku Voipio 	}
138fbb2df23SRiku Voipio out_ok:
139fbb2df23SRiku Voipio 	err = 0;
140fbb2df23SRiku Voipio out_delete_evlist:
141c12995a5SJiri Olsa 	evlist__delete(evlist);
142fbb2df23SRiku Voipio out:
143fbb2df23SRiku Voipio 	return err;
144fbb2df23SRiku Voipio }
145