xref: /openbmc/linux/tools/perf/tests/openat-syscall-tp-fields.c (revision a1cf3a75d3317ed893d453c222d220ca4d5f4c4e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28dd2a131SJiri Olsa #include <linux/err.h>
3bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
4bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
5bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
6fbb2df23SRiku Voipio #include "perf.h"
7fbb2df23SRiku Voipio #include "evlist.h"
8fbb2df23SRiku Voipio #include "evsel.h"
9fbb2df23SRiku Voipio #include "thread_map.h"
10fbb2df23SRiku Voipio #include "tests.h"
11fbb2df23SRiku Voipio #include "debug.h"
12a43783aeSArnaldo Carvalho de Melo #include <errno.h>
13fbb2df23SRiku Voipio 
14ab6526b2SArnaldo Carvalho de Melo #ifndef O_DIRECTORY
15ab6526b2SArnaldo Carvalho de Melo #define O_DIRECTORY    00200000
16ab6526b2SArnaldo Carvalho de Melo #endif
17ab6526b2SArnaldo Carvalho de Melo #ifndef AT_FDCWD
18ab6526b2SArnaldo Carvalho de Melo #define AT_FDCWD       -100
19ab6526b2SArnaldo Carvalho de Melo #endif
20ab6526b2SArnaldo Carvalho de Melo 
2181f17c90SArnaldo Carvalho de Melo int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
22fbb2df23SRiku Voipio {
23fbb2df23SRiku Voipio 	struct record_opts opts = {
24fbb2df23SRiku Voipio 		.target = {
25fbb2df23SRiku Voipio 			.uid = UINT_MAX,
26fbb2df23SRiku Voipio 			.uses_mmap = true,
27fbb2df23SRiku Voipio 		},
28fbb2df23SRiku Voipio 		.no_buffering = true,
29fbb2df23SRiku Voipio 		.freq	      = 1,
30fbb2df23SRiku Voipio 		.mmap_pages   = 256,
31fbb2df23SRiku Voipio 		.raw_samples  = true,
32fbb2df23SRiku Voipio 	};
33fbb2df23SRiku Voipio 	const char *filename = "/etc/passwd";
34fbb2df23SRiku Voipio 	int flags = O_RDONLY | O_DIRECTORY;
350f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
3632dcd021SJiri Olsa 	struct evsel *evsel;
37fbb2df23SRiku Voipio 	int err = -1, i, nr_events = 0, nr_polls = 0;
38fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
39fbb2df23SRiku Voipio 
40fbb2df23SRiku Voipio 	if (evlist == NULL) {
41fbb2df23SRiku Voipio 		pr_debug("%s: perf_evlist__new\n", __func__);
42fbb2df23SRiku Voipio 		goto out;
43fbb2df23SRiku Voipio 	}
44fbb2df23SRiku Voipio 
45fbb2df23SRiku Voipio 	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
468dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
47fbb2df23SRiku Voipio 		pr_debug("%s: perf_evsel__newtp\n", __func__);
48fbb2df23SRiku Voipio 		goto out_delete_evlist;
49fbb2df23SRiku Voipio 	}
50fbb2df23SRiku Voipio 
51*a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
52fbb2df23SRiku Voipio 
53fbb2df23SRiku Voipio 	err = perf_evlist__create_maps(evlist, &opts.target);
54fbb2df23SRiku Voipio 	if (err < 0) {
55fbb2df23SRiku Voipio 		pr_debug("%s: perf_evlist__create_maps\n", __func__);
56fbb2df23SRiku Voipio 		goto out_delete_evlist;
57fbb2df23SRiku Voipio 	}
58fbb2df23SRiku Voipio 
59e68ae9cfSArnaldo Carvalho de Melo 	perf_evsel__config(evsel, &opts, NULL);
60fbb2df23SRiku Voipio 
61e13798c7SJiri Olsa 	thread_map__set_pid(evlist->threads, 0, getpid());
62fbb2df23SRiku Voipio 
63fbb2df23SRiku Voipio 	err = perf_evlist__open(evlist);
64fbb2df23SRiku Voipio 	if (err < 0) {
65fbb2df23SRiku Voipio 		pr_debug("perf_evlist__open: %s\n",
66c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
67fbb2df23SRiku Voipio 		goto out_delete_evlist;
68fbb2df23SRiku Voipio 	}
69fbb2df23SRiku Voipio 
70f74b9d3aSWang Nan 	err = perf_evlist__mmap(evlist, UINT_MAX);
71fbb2df23SRiku Voipio 	if (err < 0) {
72fbb2df23SRiku Voipio 		pr_debug("perf_evlist__mmap: %s\n",
73c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
74fbb2df23SRiku Voipio 		goto out_delete_evlist;
75fbb2df23SRiku Voipio 	}
76fbb2df23SRiku Voipio 
77fbb2df23SRiku Voipio 	perf_evlist__enable(evlist);
78fbb2df23SRiku Voipio 
79fbb2df23SRiku Voipio 	/*
80fbb2df23SRiku Voipio 	 * Generate the event:
81fbb2df23SRiku Voipio 	 */
82fbb2df23SRiku Voipio 	openat(AT_FDCWD, filename, flags);
83fbb2df23SRiku Voipio 
84fbb2df23SRiku Voipio 	while (1) {
85fbb2df23SRiku Voipio 		int before = nr_events;
86fbb2df23SRiku Voipio 
87fbb2df23SRiku Voipio 		for (i = 0; i < evlist->nr_mmaps; i++) {
88fbb2df23SRiku Voipio 			union perf_event *event;
891d1b5632SKan Liang 			struct perf_mmap *md;
90fbb2df23SRiku Voipio 
911d1b5632SKan Liang 			md = &evlist->mmap[i];
92b9bae2c8SKan Liang 			if (perf_mmap__read_init(md) < 0)
931d1b5632SKan Liang 				continue;
941d1b5632SKan Liang 
950019dc87SKan Liang 			while ((event = perf_mmap__read_event(md)) != NULL) {
96fbb2df23SRiku Voipio 				const u32 type = event->header.type;
97fbb2df23SRiku Voipio 				int tp_flags;
98fbb2df23SRiku Voipio 				struct perf_sample sample;
99fbb2df23SRiku Voipio 
100fbb2df23SRiku Voipio 				++nr_events;
101fbb2df23SRiku Voipio 
102fbb2df23SRiku Voipio 				if (type != PERF_RECORD_SAMPLE) {
103d6ace3dfSKan Liang 					perf_mmap__consume(md);
104fbb2df23SRiku Voipio 					continue;
105fbb2df23SRiku Voipio 				}
106fbb2df23SRiku Voipio 
107fbb2df23SRiku Voipio 				err = perf_evsel__parse_sample(evsel, event, &sample);
108fbb2df23SRiku Voipio 				if (err) {
10987191383SNamhyung Kim 					pr_debug("Can't parse sample, err = %d\n", err);
110fbb2df23SRiku Voipio 					goto out_delete_evlist;
111fbb2df23SRiku Voipio 				}
112fbb2df23SRiku Voipio 
113fbb2df23SRiku Voipio 				tp_flags = perf_evsel__intval(evsel, &sample, "flags");
114fbb2df23SRiku Voipio 
115fbb2df23SRiku Voipio 				if (flags != tp_flags) {
116fbb2df23SRiku Voipio 					pr_debug("%s: Expected flags=%#x, got %#x\n",
117fbb2df23SRiku Voipio 						 __func__, flags, tp_flags);
118fbb2df23SRiku Voipio 					goto out_delete_evlist;
119fbb2df23SRiku Voipio 				}
120fbb2df23SRiku Voipio 
121fbb2df23SRiku Voipio 				goto out_ok;
122fbb2df23SRiku Voipio 			}
1231d1b5632SKan Liang 			perf_mmap__read_done(md);
124fbb2df23SRiku Voipio 		}
125fbb2df23SRiku Voipio 
126fbb2df23SRiku Voipio 		if (nr_events == before)
127fbb2df23SRiku Voipio 			perf_evlist__poll(evlist, 10);
128fbb2df23SRiku Voipio 
129fbb2df23SRiku Voipio 		if (++nr_polls > 5) {
130fbb2df23SRiku Voipio 			pr_debug("%s: no events!\n", __func__);
131fbb2df23SRiku Voipio 			goto out_delete_evlist;
132fbb2df23SRiku Voipio 		}
133fbb2df23SRiku Voipio 	}
134fbb2df23SRiku Voipio out_ok:
135fbb2df23SRiku Voipio 	err = 0;
136fbb2df23SRiku Voipio out_delete_evlist:
137c12995a5SJiri Olsa 	evlist__delete(evlist);
138fbb2df23SRiku Voipio out:
139fbb2df23SRiku Voipio 	return err;
140fbb2df23SRiku Voipio }
141