blob: 1dc2897d2df926aaec51da03e0e43490f5a00a23 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -03002#include <stdbool.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02003#include <linux/err.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -03004#include <linux/string.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03005#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
Jiri Olsadc447ee2012-11-10 01:46:49 +01008#include "evlist.h"
9#include "evsel.h"
10#include "thread_map.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030011#include "record.h"
Jiri Olsadc447ee2012-11-10 01:46:49 +010012#include "tests.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020013#include "debug.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030014#include "util/mmap.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030015#include <errno.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +020016#include <perf/mmap.h>
Jiri Olsadc447ee2012-11-10 01:46:49 +010017
Arnaldo Carvalho de Meloab6526b2016-07-05 14:43:27 -030018#ifndef O_DIRECTORY
19#define O_DIRECTORY 00200000
20#endif
21#ifndef AT_FDCWD
22#define AT_FDCWD -100
23#endif
24
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030025int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsadc447ee2012-11-10 01:46:49 +010026{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030027 struct record_opts opts = {
Jiri Olsadc447ee2012-11-10 01:46:49 +010028 .target = {
29 .uid = UINT_MAX,
30 .uses_mmap = true,
31 },
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -030032 .no_buffering = true,
33 .freq = 1,
34 .mmap_pages = 256,
35 .raw_samples = true,
Jiri Olsadc447ee2012-11-10 01:46:49 +010036 };
37 const char *filename = "/etc/passwd";
38 int flags = O_RDONLY | O_DIRECTORY;
Jiri Olsa0f98b112019-07-21 13:23:55 +020039 struct evlist *evlist = evlist__new();
Jiri Olsa32dcd022019-07-21 13:23:51 +020040 struct evsel *evsel;
Jiri Olsadc447ee2012-11-10 01:46:49 +010041 int err = -1, i, nr_events = 0, nr_polls = 0;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000042 char sbuf[STRERR_BUFSIZE];
Jiri Olsadc447ee2012-11-10 01:46:49 +010043
44 if (evlist == NULL) {
45 pr_debug("%s: perf_evlist__new\n", __func__);
46 goto out;
47 }
48
Riku Voipio43f322b2015-04-16 16:52:53 +030049 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020050 if (IS_ERR(evsel)) {
Jiri Olsadc447ee2012-11-10 01:46:49 +010051 pr_debug("%s: perf_evsel__newtp\n", __func__);
52 goto out_delete_evlist;
53 }
54
Jiri Olsaa1cf3a72019-07-21 13:23:59 +020055 evlist__add(evlist, evsel);
Jiri Olsadc447ee2012-11-10 01:46:49 +010056
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
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -030063 evsel__config(evsel, &opts, NULL);
Jiri Olsadc447ee2012-11-10 01:46:49 +010064
Jiri Olsa03617c22019-07-21 13:24:42 +020065 perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
Jiri Olsadc447ee2012-11-10 01:46:49 +010066
Jiri Olsa474ddc42019-07-21 13:24:06 +020067 err = evlist__open(evlist);
Jiri Olsadc447ee2012-11-10 01:46:49 +010068 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000069 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030070 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030071 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010072 }
73
Jiri Olsa9521b5f2019-07-28 12:45:35 +020074 err = evlist__mmap(evlist, UINT_MAX);
Jiri Olsadc447ee2012-11-10 01:46:49 +010075 if (err < 0) {
Jiri Olsa9521b5f2019-07-28 12:45:35 +020076 pr_debug("evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030077 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030078 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010079 }
80
Jiri Olsa1c87f162019-07-21 13:24:08 +020081 evlist__enable(evlist);
Jiri Olsadc447ee2012-11-10 01:46:49 +010082
83 /*
84 * Generate the event:
85 */
Riku Voipio43f322b2015-04-16 16:52:53 +030086 openat(AT_FDCWD, filename, flags);
Jiri Olsadc447ee2012-11-10 01:46:49 +010087
88 while (1) {
89 int before = nr_events;
90
Jiri Olsac976ee12019-07-30 13:04:59 +020091 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Jiri Olsadc447ee2012-11-10 01:46:49 +010092 union perf_event *event;
Jiri Olsaa5830532019-07-27 20:30:53 +020093 struct mmap *md;
Jiri Olsadc447ee2012-11-10 01:46:49 +010094
Kan Liang1d1b56322018-03-01 18:09:05 -050095 md = &evlist->mmap[i];
Jiri Olsa7c4d4182019-10-07 14:53:18 +020096 if (perf_mmap__read_init(&md->core) < 0)
Kan Liang1d1b56322018-03-01 18:09:05 -050097 continue;
98
Jiri Olsa151ed5d2019-10-07 14:53:20 +020099 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Jiri Olsadc447ee2012-11-10 01:46:49 +0100100 const u32 type = event->header.type;
101 int tp_flags;
102 struct perf_sample sample;
103
104 ++nr_events;
105
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800106 if (type != PERF_RECORD_SAMPLE) {
Jiri Olsa7728fa02019-10-07 14:53:17 +0200107 perf_mmap__consume(&md->core);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100108 continue;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800109 }
Jiri Olsadc447ee2012-11-10 01:46:49 +0100110
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -0300111 err = evsel__parse_sample(evsel, event, &sample);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100112 if (err) {
Namhyung Kim87191382015-10-20 00:23:48 +0900113 pr_debug("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300114 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100115 }
116
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300117 tp_flags = evsel__intval(evsel, &sample, "flags");
Jiri Olsadc447ee2012-11-10 01:46:49 +0100118
119 if (flags != tp_flags) {
120 pr_debug("%s: Expected flags=%#x, got %#x\n",
121 __func__, flags, tp_flags);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300122 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100123 }
124
125 goto out_ok;
126 }
Jiri Olsa32fdc2c2019-10-07 14:53:19 +0200127 perf_mmap__read_done(&md->core);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100128 }
129
130 if (nr_events == before)
Jiri Olsa80ab2982019-08-31 22:48:33 +0200131 evlist__poll(evlist, 10);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100132
133 if (++nr_polls > 5) {
134 pr_debug("%s: no events!\n", __func__);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300135 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100136 }
137 }
138out_ok:
139 err = 0;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100140out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +0200141 evlist__delete(evlist);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100142out:
143 return err;
144}