blob: 1f5f5e79ae253e0981575aa3ded1dfbf7b7bbb06 [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>
Olivier Deprez157378f2022-04-04 15:47:50 +020016#include <perf/mmap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017
18#ifndef O_DIRECTORY
19#define O_DIRECTORY 00200000
20#endif
21#ifndef AT_FDCWD
22#define AT_FDCWD -100
23#endif
24
25int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
26{
27 struct record_opts opts = {
28 .target = {
29 .uid = UINT_MAX,
30 .uses_mmap = true,
31 },
32 .no_buffering = true,
33 .freq = 1,
34 .mmap_pages = 256,
35 .raw_samples = true,
36 };
37 const char *filename = "/etc/passwd";
38 int flags = O_RDONLY | O_DIRECTORY;
David Brazdil0f672f62019-12-10 10:32:29 +000039 struct evlist *evlist = evlist__new();
40 struct evsel *evsel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041 int err = -1, i, nr_events = 0, nr_polls = 0;
42 char sbuf[STRERR_BUFSIZE];
43
44 if (evlist == NULL) {
45 pr_debug("%s: perf_evlist__new\n", __func__);
46 goto out;
47 }
48
Olivier Deprez157378f2022-04-04 15:47:50 +020049 evsel = evsel__newtp("syscalls", "sys_enter_openat");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 if (IS_ERR(evsel)) {
Olivier Deprez157378f2022-04-04 15:47:50 +020051 pr_debug("%s: evsel__newtp\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052 goto out_delete_evlist;
53 }
54
David Brazdil0f672f62019-12-10 10:32:29 +000055 evlist__add(evlist, evsel);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056
57 err = perf_evlist__create_maps(evlist, &opts.target);
58 if (err < 0) {
59 pr_debug("%s: perf_evlist__create_maps\n", __func__);
60 goto out_delete_evlist;
61 }
62
Olivier Deprez157378f2022-04-04 15:47:50 +020063 evsel__config(evsel, &opts, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064
David Brazdil0f672f62019-12-10 10:32:29 +000065 perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
David Brazdil0f672f62019-12-10 10:32:29 +000067 err = evlist__open(evlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 if (err < 0) {
69 pr_debug("perf_evlist__open: %s\n",
70 str_error_r(errno, sbuf, sizeof(sbuf)));
71 goto out_delete_evlist;
72 }
73
David Brazdil0f672f62019-12-10 10:32:29 +000074 err = evlist__mmap(evlist, UINT_MAX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 if (err < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +000076 pr_debug("evlist__mmap: %s\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 str_error_r(errno, sbuf, sizeof(sbuf)));
78 goto out_delete_evlist;
79 }
80
David Brazdil0f672f62019-12-10 10:32:29 +000081 evlist__enable(evlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082
83 /*
84 * Generate the event:
85 */
86 openat(AT_FDCWD, filename, flags);
87
88 while (1) {
89 int before = nr_events;
90
David Brazdil0f672f62019-12-10 10:32:29 +000091 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092 union perf_event *event;
David Brazdil0f672f62019-12-10 10:32:29 +000093 struct mmap *md;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094
95 md = &evlist->mmap[i];
Olivier Deprez157378f2022-04-04 15:47:50 +020096 if (perf_mmap__read_init(&md->core) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097 continue;
98
Olivier Deprez157378f2022-04-04 15:47:50 +020099 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100 const u32 type = event->header.type;
101 int tp_flags;
102 struct perf_sample sample;
103
104 ++nr_events;
105
106 if (type != PERF_RECORD_SAMPLE) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200107 perf_mmap__consume(&md->core);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108 continue;
109 }
110
Olivier Deprez157378f2022-04-04 15:47:50 +0200111 err = evsel__parse_sample(evsel, event, &sample);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112 if (err) {
113 pr_debug("Can't parse sample, err = %d\n", err);
114 goto out_delete_evlist;
115 }
116
Olivier Deprez157378f2022-04-04 15:47:50 +0200117 tp_flags = evsel__intval(evsel, &sample, "flags");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118
119 if (flags != tp_flags) {
120 pr_debug("%s: Expected flags=%#x, got %#x\n",
121 __func__, flags, tp_flags);
122 goto out_delete_evlist;
123 }
124
125 goto out_ok;
126 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200127 perf_mmap__read_done(&md->core);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 }
129
130 if (nr_events == before)
David Brazdil0f672f62019-12-10 10:32:29 +0000131 evlist__poll(evlist, 10);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132
133 if (++nr_polls > 5) {
134 pr_debug("%s: no events!\n", __func__);
135 goto out_delete_evlist;
136 }
137 }
138out_ok:
139 err = 0;
140out_delete_evlist:
David Brazdil0f672f62019-12-10 10:32:29 +0000141 evlist__delete(evlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142out:
143 return err;
144}