xref: /linux/tools/perf/tests/openat-syscall-tp-fields.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 #include "perf.h"
2 #include "evlist.h"
3 #include "evsel.h"
4 #include "thread_map.h"
5 #include "tests.h"
6 #include "debug.h"
7 
8 int test__syscall_openat_tp_fields(void)
9 {
10 	struct record_opts opts = {
11 		.target = {
12 			.uid = UINT_MAX,
13 			.uses_mmap = true,
14 		},
15 		.no_buffering = true,
16 		.freq	      = 1,
17 		.mmap_pages   = 256,
18 		.raw_samples  = true,
19 	};
20 	const char *filename = "/etc/passwd";
21 	int flags = O_RDONLY | O_DIRECTORY;
22 	struct perf_evlist *evlist = perf_evlist__new();
23 	struct perf_evsel *evsel;
24 	int err = -1, i, nr_events = 0, nr_polls = 0;
25 	char sbuf[STRERR_BUFSIZE];
26 
27 	if (evlist == NULL) {
28 		pr_debug("%s: perf_evlist__new\n", __func__);
29 		goto out;
30 	}
31 
32 	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
33 	if (evsel == NULL) {
34 		pr_debug("%s: perf_evsel__newtp\n", __func__);
35 		goto out_delete_evlist;
36 	}
37 
38 	perf_evlist__add(evlist, evsel);
39 
40 	err = perf_evlist__create_maps(evlist, &opts.target);
41 	if (err < 0) {
42 		pr_debug("%s: perf_evlist__create_maps\n", __func__);
43 		goto out_delete_evlist;
44 	}
45 
46 	perf_evsel__config(evsel, &opts);
47 
48 	thread_map__set_pid(evlist->threads, 0, getpid());
49 
50 	err = perf_evlist__open(evlist);
51 	if (err < 0) {
52 		pr_debug("perf_evlist__open: %s\n",
53 			 strerror_r(errno, sbuf, sizeof(sbuf)));
54 		goto out_delete_evlist;
55 	}
56 
57 	err = perf_evlist__mmap(evlist, UINT_MAX, false);
58 	if (err < 0) {
59 		pr_debug("perf_evlist__mmap: %s\n",
60 			 strerror_r(errno, sbuf, sizeof(sbuf)));
61 		goto out_delete_evlist;
62 	}
63 
64 	perf_evlist__enable(evlist);
65 
66 	/*
67 	 * Generate the event:
68 	 */
69 	openat(AT_FDCWD, filename, flags);
70 
71 	while (1) {
72 		int before = nr_events;
73 
74 		for (i = 0; i < evlist->nr_mmaps; i++) {
75 			union perf_event *event;
76 
77 			while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
78 				const u32 type = event->header.type;
79 				int tp_flags;
80 				struct perf_sample sample;
81 
82 				++nr_events;
83 
84 				if (type != PERF_RECORD_SAMPLE) {
85 					perf_evlist__mmap_consume(evlist, i);
86 					continue;
87 				}
88 
89 				err = perf_evsel__parse_sample(evsel, event, &sample);
90 				if (err) {
91 					pr_err("Can't parse sample, err = %d\n", err);
92 					goto out_delete_evlist;
93 				}
94 
95 				tp_flags = perf_evsel__intval(evsel, &sample, "flags");
96 
97 				if (flags != tp_flags) {
98 					pr_debug("%s: Expected flags=%#x, got %#x\n",
99 						 __func__, flags, tp_flags);
100 					goto out_delete_evlist;
101 				}
102 
103 				goto out_ok;
104 			}
105 		}
106 
107 		if (nr_events == before)
108 			perf_evlist__poll(evlist, 10);
109 
110 		if (++nr_polls > 5) {
111 			pr_debug("%s: no events!\n", __func__);
112 			goto out_delete_evlist;
113 		}
114 	}
115 out_ok:
116 	err = 0;
117 out_delete_evlist:
118 	perf_evlist__delete(evlist);
119 out:
120 	return err;
121 }
122