xref: /openbmc/linux/tools/perf/tests/openat-syscall-tp-fields.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
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>
17*9823147dSArnaldo Carvalho de Melo #include "util/sample.h"
18fbb2df23SRiku Voipio 
19ab6526b2SArnaldo Carvalho de Melo #ifndef O_DIRECTORY
20ab6526b2SArnaldo Carvalho de Melo #define O_DIRECTORY    00200000
21ab6526b2SArnaldo Carvalho de Melo #endif
22ab6526b2SArnaldo Carvalho de Melo #ifndef AT_FDCWD
23ab6526b2SArnaldo Carvalho de Melo #define AT_FDCWD       -100
24ab6526b2SArnaldo Carvalho de Melo #endif
25ab6526b2SArnaldo Carvalho de Melo 
test__syscall_openat_tp_fields(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2633f44bfdSIan Rogers static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused,
27d68f0365SIan Rogers 					  int subtest __maybe_unused)
28fbb2df23SRiku Voipio {
29fbb2df23SRiku Voipio 	struct record_opts opts = {
30fbb2df23SRiku Voipio 		.target = {
31fbb2df23SRiku Voipio 			.uid = UINT_MAX,
32fbb2df23SRiku Voipio 			.uses_mmap = true,
33fbb2df23SRiku Voipio 		},
34fbb2df23SRiku Voipio 		.no_buffering = true,
35fbb2df23SRiku Voipio 		.freq	      = 1,
36fbb2df23SRiku Voipio 		.mmap_pages   = 256,
37fbb2df23SRiku Voipio 		.raw_samples  = true,
38fbb2df23SRiku Voipio 	};
39fbb2df23SRiku Voipio 	const char *filename = "/etc/passwd";
40fbb2df23SRiku Voipio 	int flags = O_RDONLY | O_DIRECTORY;
410f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
4232dcd021SJiri Olsa 	struct evsel *evsel;
43fbb2df23SRiku Voipio 	int err = -1, i, nr_events = 0, nr_polls = 0;
44fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
45fbb2df23SRiku Voipio 
46fbb2df23SRiku Voipio 	if (evlist == NULL) {
47606e2c29SArnaldo Carvalho de Melo 		pr_debug("%s: evlist__new\n", __func__);
48fbb2df23SRiku Voipio 		goto out;
49fbb2df23SRiku Voipio 	}
50fbb2df23SRiku Voipio 
518f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__newtp("syscalls", "sys_enter_openat");
528dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
538f6725a2SArnaldo Carvalho de Melo 		pr_debug("%s: evsel__newtp\n", __func__);
54fbb2df23SRiku Voipio 		goto out_delete_evlist;
55fbb2df23SRiku Voipio 	}
56fbb2df23SRiku Voipio 
57a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
58fbb2df23SRiku Voipio 
597748bb71SArnaldo Carvalho de Melo 	err = evlist__create_maps(evlist, &opts.target);
60fbb2df23SRiku Voipio 	if (err < 0) {
617748bb71SArnaldo Carvalho de Melo 		pr_debug("%s: evlist__create_maps\n", __func__);
62fbb2df23SRiku Voipio 		goto out_delete_evlist;
63fbb2df23SRiku Voipio 	}
64fbb2df23SRiku Voipio 
656ec17b4eSArnaldo Carvalho de Melo 	evsel__config(evsel, &opts, NULL);
66fbb2df23SRiku Voipio 
6703617c22SJiri Olsa 	perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
68fbb2df23SRiku Voipio 
69474ddc4cSJiri Olsa 	err = evlist__open(evlist);
70fbb2df23SRiku Voipio 	if (err < 0) {
71fbb2df23SRiku Voipio 		pr_debug("perf_evlist__open: %s\n",
72c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
73fbb2df23SRiku Voipio 		goto out_delete_evlist;
74fbb2df23SRiku Voipio 	}
75fbb2df23SRiku Voipio 
769521b5f2SJiri Olsa 	err = evlist__mmap(evlist, UINT_MAX);
77fbb2df23SRiku Voipio 	if (err < 0) {
789521b5f2SJiri Olsa 		pr_debug("evlist__mmap: %s\n",
79c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
80fbb2df23SRiku Voipio 		goto out_delete_evlist;
81fbb2df23SRiku Voipio 	}
82fbb2df23SRiku Voipio 
831c87f165SJiri Olsa 	evlist__enable(evlist);
84fbb2df23SRiku Voipio 
85fbb2df23SRiku Voipio 	/*
86fbb2df23SRiku Voipio 	 * Generate the event:
87fbb2df23SRiku Voipio 	 */
88fbb2df23SRiku Voipio 	openat(AT_FDCWD, filename, flags);
89fbb2df23SRiku Voipio 
90fbb2df23SRiku Voipio 	while (1) {
91fbb2df23SRiku Voipio 		int before = nr_events;
92fbb2df23SRiku Voipio 
93c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
94fbb2df23SRiku Voipio 			union perf_event *event;
95a5830532SJiri Olsa 			struct mmap *md;
96fbb2df23SRiku Voipio 
971d1b5632SKan Liang 			md = &evlist->mmap[i];
987c4d4182SJiri Olsa 			if (perf_mmap__read_init(&md->core) < 0)
991d1b5632SKan Liang 				continue;
1001d1b5632SKan Liang 
101151ed5d7SJiri Olsa 			while ((event = perf_mmap__read_event(&md->core)) != NULL) {
102fbb2df23SRiku Voipio 				const u32 type = event->header.type;
103fbb2df23SRiku Voipio 				int tp_flags;
104fbb2df23SRiku Voipio 				struct perf_sample sample;
105fbb2df23SRiku Voipio 
106fbb2df23SRiku Voipio 				++nr_events;
107fbb2df23SRiku Voipio 
108fbb2df23SRiku Voipio 				if (type != PERF_RECORD_SAMPLE) {
1097728fa0cSJiri Olsa 					perf_mmap__consume(&md->core);
110fbb2df23SRiku Voipio 					continue;
111fbb2df23SRiku Voipio 				}
112fbb2df23SRiku Voipio 
1136b6017a2SArnaldo Carvalho de Melo 				err = evsel__parse_sample(evsel, event, &sample);
114fbb2df23SRiku Voipio 				if (err) {
11587191383SNamhyung Kim 					pr_debug("Can't parse sample, err = %d\n", err);
116fbb2df23SRiku Voipio 					goto out_delete_evlist;
117fbb2df23SRiku Voipio 				}
118fbb2df23SRiku Voipio 
119efc0cdc9SArnaldo Carvalho de Melo 				tp_flags = evsel__intval(evsel, &sample, "flags");
120fbb2df23SRiku Voipio 
121fbb2df23SRiku Voipio 				if (flags != tp_flags) {
122fbb2df23SRiku Voipio 					pr_debug("%s: Expected flags=%#x, got %#x\n",
123fbb2df23SRiku Voipio 						 __func__, flags, tp_flags);
124fbb2df23SRiku Voipio 					goto out_delete_evlist;
125fbb2df23SRiku Voipio 				}
126fbb2df23SRiku Voipio 
127fbb2df23SRiku Voipio 				goto out_ok;
128fbb2df23SRiku Voipio 			}
12932fdc2caSJiri Olsa 			perf_mmap__read_done(&md->core);
130fbb2df23SRiku Voipio 		}
131fbb2df23SRiku Voipio 
132fbb2df23SRiku Voipio 		if (nr_events == before)
13380ab2987SJiri Olsa 			evlist__poll(evlist, 10);
134fbb2df23SRiku Voipio 
135fbb2df23SRiku Voipio 		if (++nr_polls > 5) {
136fbb2df23SRiku Voipio 			pr_debug("%s: no events!\n", __func__);
137fbb2df23SRiku Voipio 			goto out_delete_evlist;
138fbb2df23SRiku Voipio 		}
139fbb2df23SRiku Voipio 	}
140fbb2df23SRiku Voipio out_ok:
141fbb2df23SRiku Voipio 	err = 0;
142fbb2df23SRiku Voipio out_delete_evlist:
143c12995a5SJiri Olsa 	evlist__delete(evlist);
144fbb2df23SRiku Voipio out:
145fbb2df23SRiku Voipio 	return err;
146fbb2df23SRiku Voipio }
147d68f0365SIan Rogers 
148d68f0365SIan Rogers DEFINE_SUITE("syscalls:sys_enter_openat event fields", syscall_openat_tp_fields);
149