xref: /openbmc/linux/tools/perf/tests/openat-syscall-tp-fields.c (revision 8520a98dbab61e9e340cdfb72dd17ccc8a98961e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2aeb00b1aSArnaldo Carvalho de Melo #include <stdbool.h>
38dd2a131SJiri Olsa #include <linux/err.h>
4*8520a98dSArnaldo 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"
14a43783aeSArnaldo Carvalho de Melo #include <errno.h>
15fbb2df23SRiku Voipio 
16ab6526b2SArnaldo Carvalho de Melo #ifndef O_DIRECTORY
17ab6526b2SArnaldo Carvalho de Melo #define O_DIRECTORY    00200000
18ab6526b2SArnaldo Carvalho de Melo #endif
19ab6526b2SArnaldo Carvalho de Melo #ifndef AT_FDCWD
20ab6526b2SArnaldo Carvalho de Melo #define AT_FDCWD       -100
21ab6526b2SArnaldo Carvalho de Melo #endif
22ab6526b2SArnaldo Carvalho de Melo 
2381f17c90SArnaldo Carvalho de Melo int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
24fbb2df23SRiku Voipio {
25fbb2df23SRiku Voipio 	struct record_opts opts = {
26fbb2df23SRiku Voipio 		.target = {
27fbb2df23SRiku Voipio 			.uid = UINT_MAX,
28fbb2df23SRiku Voipio 			.uses_mmap = true,
29fbb2df23SRiku Voipio 		},
30fbb2df23SRiku Voipio 		.no_buffering = true,
31fbb2df23SRiku Voipio 		.freq	      = 1,
32fbb2df23SRiku Voipio 		.mmap_pages   = 256,
33fbb2df23SRiku Voipio 		.raw_samples  = true,
34fbb2df23SRiku Voipio 	};
35fbb2df23SRiku Voipio 	const char *filename = "/etc/passwd";
36fbb2df23SRiku Voipio 	int flags = O_RDONLY | O_DIRECTORY;
370f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
3832dcd021SJiri Olsa 	struct evsel *evsel;
39fbb2df23SRiku Voipio 	int err = -1, i, nr_events = 0, nr_polls = 0;
40fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
41fbb2df23SRiku Voipio 
42fbb2df23SRiku Voipio 	if (evlist == NULL) {
43fbb2df23SRiku Voipio 		pr_debug("%s: perf_evlist__new\n", __func__);
44fbb2df23SRiku Voipio 		goto out;
45fbb2df23SRiku Voipio 	}
46fbb2df23SRiku Voipio 
47fbb2df23SRiku Voipio 	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
488dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
49fbb2df23SRiku Voipio 		pr_debug("%s: perf_evsel__newtp\n", __func__);
50fbb2df23SRiku Voipio 		goto out_delete_evlist;
51fbb2df23SRiku Voipio 	}
52fbb2df23SRiku Voipio 
53a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
54fbb2df23SRiku Voipio 
55fbb2df23SRiku Voipio 	err = perf_evlist__create_maps(evlist, &opts.target);
56fbb2df23SRiku Voipio 	if (err < 0) {
57fbb2df23SRiku Voipio 		pr_debug("%s: perf_evlist__create_maps\n", __func__);
58fbb2df23SRiku Voipio 		goto out_delete_evlist;
59fbb2df23SRiku Voipio 	}
60fbb2df23SRiku Voipio 
61e68ae9cfSArnaldo Carvalho de Melo 	perf_evsel__config(evsel, &opts, NULL);
62fbb2df23SRiku Voipio 
6303617c22SJiri Olsa 	perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
64fbb2df23SRiku Voipio 
65474ddc4cSJiri Olsa 	err = evlist__open(evlist);
66fbb2df23SRiku Voipio 	if (err < 0) {
67fbb2df23SRiku Voipio 		pr_debug("perf_evlist__open: %s\n",
68c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
69fbb2df23SRiku Voipio 		goto out_delete_evlist;
70fbb2df23SRiku Voipio 	}
71fbb2df23SRiku Voipio 
72f74b9d3aSWang Nan 	err = perf_evlist__mmap(evlist, UINT_MAX);
73fbb2df23SRiku Voipio 	if (err < 0) {
74fbb2df23SRiku Voipio 		pr_debug("perf_evlist__mmap: %s\n",
75c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
76fbb2df23SRiku Voipio 		goto out_delete_evlist;
77fbb2df23SRiku Voipio 	}
78fbb2df23SRiku Voipio 
791c87f165SJiri Olsa 	evlist__enable(evlist);
80fbb2df23SRiku Voipio 
81fbb2df23SRiku Voipio 	/*
82fbb2df23SRiku Voipio 	 * Generate the event:
83fbb2df23SRiku Voipio 	 */
84fbb2df23SRiku Voipio 	openat(AT_FDCWD, filename, flags);
85fbb2df23SRiku Voipio 
86fbb2df23SRiku Voipio 	while (1) {
87fbb2df23SRiku Voipio 		int before = nr_events;
88fbb2df23SRiku Voipio 
89fbb2df23SRiku Voipio 		for (i = 0; i < evlist->nr_mmaps; i++) {
90fbb2df23SRiku Voipio 			union perf_event *event;
911d1b5632SKan Liang 			struct perf_mmap *md;
92fbb2df23SRiku Voipio 
931d1b5632SKan Liang 			md = &evlist->mmap[i];
94b9bae2c8SKan Liang 			if (perf_mmap__read_init(md) < 0)
951d1b5632SKan Liang 				continue;
961d1b5632SKan Liang 
970019dc87SKan Liang 			while ((event = perf_mmap__read_event(md)) != NULL) {
98fbb2df23SRiku Voipio 				const u32 type = event->header.type;
99fbb2df23SRiku Voipio 				int tp_flags;
100fbb2df23SRiku Voipio 				struct perf_sample sample;
101fbb2df23SRiku Voipio 
102fbb2df23SRiku Voipio 				++nr_events;
103fbb2df23SRiku Voipio 
104fbb2df23SRiku Voipio 				if (type != PERF_RECORD_SAMPLE) {
105d6ace3dfSKan Liang 					perf_mmap__consume(md);
106fbb2df23SRiku Voipio 					continue;
107fbb2df23SRiku Voipio 				}
108fbb2df23SRiku Voipio 
109fbb2df23SRiku Voipio 				err = perf_evsel__parse_sample(evsel, event, &sample);
110fbb2df23SRiku Voipio 				if (err) {
11187191383SNamhyung Kim 					pr_debug("Can't parse sample, err = %d\n", err);
112fbb2df23SRiku Voipio 					goto out_delete_evlist;
113fbb2df23SRiku Voipio 				}
114fbb2df23SRiku Voipio 
115fbb2df23SRiku Voipio 				tp_flags = perf_evsel__intval(evsel, &sample, "flags");
116fbb2df23SRiku Voipio 
117fbb2df23SRiku Voipio 				if (flags != tp_flags) {
118fbb2df23SRiku Voipio 					pr_debug("%s: Expected flags=%#x, got %#x\n",
119fbb2df23SRiku Voipio 						 __func__, flags, tp_flags);
120fbb2df23SRiku Voipio 					goto out_delete_evlist;
121fbb2df23SRiku Voipio 				}
122fbb2df23SRiku Voipio 
123fbb2df23SRiku Voipio 				goto out_ok;
124fbb2df23SRiku Voipio 			}
1251d1b5632SKan Liang 			perf_mmap__read_done(md);
126fbb2df23SRiku Voipio 		}
127fbb2df23SRiku Voipio 
128fbb2df23SRiku Voipio 		if (nr_events == before)
129fbb2df23SRiku Voipio 			perf_evlist__poll(evlist, 10);
130fbb2df23SRiku Voipio 
131fbb2df23SRiku Voipio 		if (++nr_polls > 5) {
132fbb2df23SRiku Voipio 			pr_debug("%s: no events!\n", __func__);
133fbb2df23SRiku Voipio 			goto out_delete_evlist;
134fbb2df23SRiku Voipio 		}
135fbb2df23SRiku Voipio 	}
136fbb2df23SRiku Voipio out_ok:
137fbb2df23SRiku Voipio 	err = 0;
138fbb2df23SRiku Voipio out_delete_evlist:
139c12995a5SJiri Olsa 	evlist__delete(evlist);
140fbb2df23SRiku Voipio out:
141fbb2df23SRiku Voipio 	return err;
142fbb2df23SRiku Voipio }
143