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