1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdbool.h>
3 #include <linux/err.h>
4 #include <linux/string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include "evlist.h"
9 #include "evsel.h"
10 #include "thread_map.h"
11 #include "record.h"
12 #include "tests.h"
13 #include "debug.h"
14 #include "util/mmap.h"
15 #include <errno.h>
16 #include <perf/mmap.h>
17 
18 #ifndef O_DIRECTORY
19 #define O_DIRECTORY    00200000
20 #endif
21 #ifndef AT_FDCWD
22 #define AT_FDCWD       -100
23 #endif
24 
25 static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused,
26 					  int subtest __maybe_unused)
27 {
28 	struct record_opts opts = {
29 		.target = {
30 			.uid = UINT_MAX,
31 			.uses_mmap = true,
32 		},
33 		.no_buffering = true,
34 		.freq	      = 1,
35 		.mmap_pages   = 256,
36 		.raw_samples  = true,
37 	};
38 	const char *filename = "/etc/passwd";
39 	int flags = O_RDONLY | O_DIRECTORY;
40 	struct evlist *evlist = evlist__new();
41 	struct evsel *evsel;
42 	int err = -1, i, nr_events = 0, nr_polls = 0;
43 	char sbuf[STRERR_BUFSIZE];
44 
45 	if (evlist == NULL) {
46 		pr_debug("%s: evlist__new\n", __func__);
47 		goto out;
48 	}
49 
50 	evsel = evsel__newtp("syscalls", "sys_enter_openat");
51 	if (IS_ERR(evsel)) {
52 		pr_debug("%s: evsel__newtp\n", __func__);
53 		goto out_delete_evlist;
54 	}
55 
56 	evlist__add(evlist, evsel);
57 
58 	err = evlist__create_maps(evlist, &opts.target);
59 	if (err < 0) {
60 		pr_debug("%s: evlist__create_maps\n", __func__);
61 		goto out_delete_evlist;
62 	}
63 
64 	evsel__config(evsel, &opts, NULL);
65 
66 	perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
67 
68 	err = evlist__open(evlist);
69 	if (err < 0) {
70 		pr_debug("perf_evlist__open: %s\n",
71 			 str_error_r(errno, sbuf, sizeof(sbuf)));
72 		goto out_delete_evlist;
73 	}
74 
75 	err = evlist__mmap(evlist, UINT_MAX);
76 	if (err < 0) {
77 		pr_debug("evlist__mmap: %s\n",
78 			 str_error_r(errno, sbuf, sizeof(sbuf)));
79 		goto out_delete_evlist;
80 	}
81 
82 	evlist__enable(evlist);
83 
84 	/*
85 	 * Generate the event:
86 	 */
87 	openat(AT_FDCWD, filename, flags);
88 
89 	while (1) {
90 		int before = nr_events;
91 
92 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
93 			union perf_event *event;
94 			struct mmap *md;
95 
96 			md = &evlist->mmap[i];
97 			if (perf_mmap__read_init(&md->core) < 0)
98 				continue;
99 
100 			while ((event = perf_mmap__read_event(&md->core)) != NULL) {
101 				const u32 type = event->header.type;
102 				int tp_flags;
103 				struct perf_sample sample;
104 
105 				++nr_events;
106 
107 				if (type != PERF_RECORD_SAMPLE) {
108 					perf_mmap__consume(&md->core);
109 					continue;
110 				}
111 
112 				err = evsel__parse_sample(evsel, event, &sample);
113 				if (err) {
114 					pr_debug("Can't parse sample, err = %d\n", err);
115 					goto out_delete_evlist;
116 				}
117 
118 				tp_flags = evsel__intval(evsel, &sample, "flags");
119 
120 				if (flags != tp_flags) {
121 					pr_debug("%s: Expected flags=%#x, got %#x\n",
122 						 __func__, flags, tp_flags);
123 					goto out_delete_evlist;
124 				}
125 
126 				goto out_ok;
127 			}
128 			perf_mmap__read_done(&md->core);
129 		}
130 
131 		if (nr_events == before)
132 			evlist__poll(evlist, 10);
133 
134 		if (++nr_polls > 5) {
135 			pr_debug("%s: no events!\n", __func__);
136 			goto out_delete_evlist;
137 		}
138 	}
139 out_ok:
140 	err = 0;
141 out_delete_evlist:
142 	evlist__delete(evlist);
143 out:
144 	return err;
145 }
146 
147 DEFINE_SUITE("syscalls:sys_enter_openat event fields", syscall_openat_tp_fields);
148