blob: 9c06130d37be7eef1cb835f489cf5bedf7f06e22 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002#include <linux/err.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03003#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
Jiri Olsadc447ee2012-11-10 01:46:49 +01006#include "perf.h"
7#include "evlist.h"
8#include "evsel.h"
9#include "thread_map.h"
10#include "tests.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020011#include "debug.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030012#include <errno.h>
Jiri Olsadc447ee2012-11-10 01:46:49 +010013
Arnaldo Carvalho de Meloab6526b2016-07-05 14:43:27 -030014#ifndef O_DIRECTORY
15#define O_DIRECTORY 00200000
16#endif
17#ifndef AT_FDCWD
18#define AT_FDCWD -100
19#endif
20
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030021int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsadc447ee2012-11-10 01:46:49 +010022{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030023 struct record_opts opts = {
Jiri Olsadc447ee2012-11-10 01:46:49 +010024 .target = {
25 .uid = UINT_MAX,
26 .uses_mmap = true,
27 },
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -030028 .no_buffering = true,
29 .freq = 1,
30 .mmap_pages = 256,
31 .raw_samples = true,
Jiri Olsadc447ee2012-11-10 01:46:49 +010032 };
33 const char *filename = "/etc/passwd";
34 int flags = O_RDONLY | O_DIRECTORY;
Jiri Olsa0f98b112019-07-21 13:23:55 +020035 struct evlist *evlist = evlist__new();
Jiri Olsa32dcd022019-07-21 13:23:51 +020036 struct evsel *evsel;
Jiri Olsadc447ee2012-11-10 01:46:49 +010037 int err = -1, i, nr_events = 0, nr_polls = 0;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000038 char sbuf[STRERR_BUFSIZE];
Jiri Olsadc447ee2012-11-10 01:46:49 +010039
40 if (evlist == NULL) {
41 pr_debug("%s: perf_evlist__new\n", __func__);
42 goto out;
43 }
44
Riku Voipio43f322b2015-04-16 16:52:53 +030045 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020046 if (IS_ERR(evsel)) {
Jiri Olsadc447ee2012-11-10 01:46:49 +010047 pr_debug("%s: perf_evsel__newtp\n", __func__);
48 goto out_delete_evlist;
49 }
50
Jiri Olsaa1cf3a72019-07-21 13:23:59 +020051 evlist__add(evlist, evsel);
Jiri Olsadc447ee2012-11-10 01:46:49 +010052
53 err = perf_evlist__create_maps(evlist, &opts.target);
54 if (err < 0) {
55 pr_debug("%s: perf_evlist__create_maps\n", __func__);
56 goto out_delete_evlist;
57 }
58
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030059 perf_evsel__config(evsel, &opts, NULL);
Jiri Olsadc447ee2012-11-10 01:46:49 +010060
Jiri Olsa03617c22019-07-21 13:24:42 +020061 perf_thread_map__set_pid(evlist->core.threads, 0, getpid());
Jiri Olsadc447ee2012-11-10 01:46:49 +010062
Jiri Olsa474ddc42019-07-21 13:24:06 +020063 err = evlist__open(evlist);
Jiri Olsadc447ee2012-11-10 01:46:49 +010064 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000065 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030066 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030067 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010068 }
69
Wang Nanf74b9d3a2017-12-03 02:00:37 +000070 err = perf_evlist__mmap(evlist, UINT_MAX);
Jiri Olsadc447ee2012-11-10 01:46:49 +010071 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000072 pr_debug("perf_evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030073 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030074 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010075 }
76
Jiri Olsa1c87f162019-07-21 13:24:08 +020077 evlist__enable(evlist);
Jiri Olsadc447ee2012-11-10 01:46:49 +010078
79 /*
80 * Generate the event:
81 */
Riku Voipio43f322b2015-04-16 16:52:53 +030082 openat(AT_FDCWD, filename, flags);
Jiri Olsadc447ee2012-11-10 01:46:49 +010083
84 while (1) {
85 int before = nr_events;
86
87 for (i = 0; i < evlist->nr_mmaps; i++) {
88 union perf_event *event;
Kan Liang1d1b56322018-03-01 18:09:05 -050089 struct perf_mmap *md;
Jiri Olsadc447ee2012-11-10 01:46:49 +010090
Kan Liang1d1b56322018-03-01 18:09:05 -050091 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -050092 if (perf_mmap__read_init(md) < 0)
Kan Liang1d1b56322018-03-01 18:09:05 -050093 continue;
94
Kan Liang0019dc872018-03-06 10:36:06 -050095 while ((event = perf_mmap__read_event(md)) != NULL) {
Jiri Olsadc447ee2012-11-10 01:46:49 +010096 const u32 type = event->header.type;
97 int tp_flags;
98 struct perf_sample sample;
99
100 ++nr_events;
101
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800102 if (type != PERF_RECORD_SAMPLE) {
Kan Liangd6ace3d2018-03-06 10:36:05 -0500103 perf_mmap__consume(md);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100104 continue;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800105 }
Jiri Olsadc447ee2012-11-10 01:46:49 +0100106
107 err = perf_evsel__parse_sample(evsel, event, &sample);
108 if (err) {
Namhyung Kim87191382015-10-20 00:23:48 +0900109 pr_debug("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300110 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100111 }
112
113 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
114
115 if (flags != tp_flags) {
116 pr_debug("%s: Expected flags=%#x, got %#x\n",
117 __func__, flags, tp_flags);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300118 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100119 }
120
121 goto out_ok;
122 }
Kan Liang1d1b56322018-03-01 18:09:05 -0500123 perf_mmap__read_done(md);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100124 }
125
126 if (nr_events == before)
Arnaldo Carvalho de Melof66a889d2014-08-18 17:25:59 -0300127 perf_evlist__poll(evlist, 10);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100128
129 if (++nr_polls > 5) {
130 pr_debug("%s: no events!\n", __func__);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300131 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100132 }
133 }
134out_ok:
135 err = 0;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100136out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +0200137 evlist__delete(evlist);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100138out:
139 return err;
140}