blob: 2b5c468130537417dceaae8d703d358194897182 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
David Brazdil0f672f62019-12-10 10:32:29 +00002#include <stdbool.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003#include <linux/err.h>
David Brazdil0f672f62019-12-10 10:32:29 +00004#include <linux/string.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#include "evlist.h"
9#include "evsel.h"
10#include "thread_map.h"
David Brazdil0f672f62019-12-10 10:32:29 +000011#include "record.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include "tests.h"
13#include "debug.h"
David Brazdil0f672f62019-12-10 10:32:29 +000014#include "util/mmap.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include <errno.h>
16
17#ifndef O_DIRECTORY
18#define O_DIRECTORY 00200000
19#endif
20#ifndef AT_FDCWD
21#define AT_FDCWD -100
22#endif
23
24int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
25{
26 struct record_opts opts = {
27 .target = {
28 .uid = UINT_MAX,
29 .uses_mmap = true,
30 },
31 .no_buffering = true,
32 .freq = 1,
33 .mmap_pages = 256,
34 .raw_samples = true,
35 };
36 const char *filename = "/etc/passwd";
37 int flags = O_RDONLY | O_DIRECTORY;
David Brazdil0f672f62019-12-10 10:32:29 +000038 struct evlist *evlist = evlist__new();
39 struct evsel *evsel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 int err = -1, i, nr_events = 0, nr_polls = 0;
41 char sbuf[STRERR_BUFSIZE];
42
43 if (evlist == NULL) {
44 pr_debug("%s: perf_evlist__new\n", __func__);
45 goto out;
46 }
47
48 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
49 if (IS_ERR(evsel)) {
50 pr_debug("%s: perf_evsel__newtp\n", __func__);
51 goto out_delete_evlist;
52 }
53
David Brazdil0f672f62019-12-10 10:32:29 +000054 evlist__add(evlist, evsel);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
56 err = perf_evlist__create_maps(evlist, &opts.target);
57 if (err < 0) {
58 pr_debug("%s: perf_evlist__create_maps\n", __func__);
59 goto out_delete_evlist;
60 }
61
62 perf_evsel__config(evsel, &opts, NULL);
63
David Brazdil0f672f62019-12-10 10:32:29 +000064 perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065
David Brazdil0f672f62019-12-10 10:32:29 +000066 err = evlist__open(evlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 if (err < 0) {
68 pr_debug("perf_evlist__open: %s\n",
69 str_error_r(errno, sbuf, sizeof(sbuf)));
70 goto out_delete_evlist;
71 }
72
David Brazdil0f672f62019-12-10 10:32:29 +000073 err = evlist__mmap(evlist, UINT_MAX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 if (err < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +000075 pr_debug("evlist__mmap: %s\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 str_error_r(errno, sbuf, sizeof(sbuf)));
77 goto out_delete_evlist;
78 }
79
David Brazdil0f672f62019-12-10 10:32:29 +000080 evlist__enable(evlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081
82 /*
83 * Generate the event:
84 */
85 openat(AT_FDCWD, filename, flags);
86
87 while (1) {
88 int before = nr_events;
89
David Brazdil0f672f62019-12-10 10:32:29 +000090 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091 union perf_event *event;
David Brazdil0f672f62019-12-10 10:32:29 +000092 struct mmap *md;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
94 md = &evlist->mmap[i];
95 if (perf_mmap__read_init(md) < 0)
96 continue;
97
98 while ((event = perf_mmap__read_event(md)) != NULL) {
99 const u32 type = event->header.type;
100 int tp_flags;
101 struct perf_sample sample;
102
103 ++nr_events;
104
105 if (type != PERF_RECORD_SAMPLE) {
106 perf_mmap__consume(md);
107 continue;
108 }
109
110 err = perf_evsel__parse_sample(evsel, event, &sample);
111 if (err) {
112 pr_debug("Can't parse sample, err = %d\n", err);
113 goto out_delete_evlist;
114 }
115
116 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
117
118 if (flags != tp_flags) {
119 pr_debug("%s: Expected flags=%#x, got %#x\n",
120 __func__, flags, tp_flags);
121 goto out_delete_evlist;
122 }
123
124 goto out_ok;
125 }
126 perf_mmap__read_done(md);
127 }
128
129 if (nr_events == before)
David Brazdil0f672f62019-12-10 10:32:29 +0000130 evlist__poll(evlist, 10);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131
132 if (++nr_polls > 5) {
133 pr_debug("%s: no events!\n", __func__);
134 goto out_delete_evlist;
135 }
136 }
137out_ok:
138 err = 0;
139out_delete_evlist:
David Brazdil0f672f62019-12-10 10:32:29 +0000140 evlist__delete(evlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141out:
142 return err;
143}