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