blob: 97c9407d02a010689c08e1e7c73903c93687fea5 [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>
Jiri Olsadc447ee2012-11-10 01:46:49 +01003#include "perf.h"
4#include "evlist.h"
5#include "evsel.h"
6#include "thread_map.h"
7#include "tests.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +02008#include "debug.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03009#include <errno.h>
Jiri Olsadc447ee2012-11-10 01:46:49 +010010
Arnaldo Carvalho de Meloab6526b2016-07-05 14:43:27 -030011#ifndef O_DIRECTORY
12#define O_DIRECTORY 00200000
13#endif
14#ifndef AT_FDCWD
15#define AT_FDCWD -100
16#endif
17
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030018int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsadc447ee2012-11-10 01:46:49 +010019{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030020 struct record_opts opts = {
Jiri Olsadc447ee2012-11-10 01:46:49 +010021 .target = {
22 .uid = UINT_MAX,
23 .uses_mmap = true,
24 },
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -030025 .no_buffering = true,
26 .freq = 1,
27 .mmap_pages = 256,
28 .raw_samples = true,
Jiri Olsadc447ee2012-11-10 01:46:49 +010029 };
30 const char *filename = "/etc/passwd";
31 int flags = O_RDONLY | O_DIRECTORY;
Namhyung Kim334fe7a2013-03-11 16:43:12 +090032 struct perf_evlist *evlist = perf_evlist__new();
Jiri Olsadc447ee2012-11-10 01:46:49 +010033 struct perf_evsel *evsel;
34 int err = -1, i, nr_events = 0, nr_polls = 0;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000035 char sbuf[STRERR_BUFSIZE];
Jiri Olsadc447ee2012-11-10 01:46:49 +010036
37 if (evlist == NULL) {
38 pr_debug("%s: perf_evlist__new\n", __func__);
39 goto out;
40 }
41
Riku Voipio43f322b2015-04-16 16:52:53 +030042 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020043 if (IS_ERR(evsel)) {
Jiri Olsadc447ee2012-11-10 01:46:49 +010044 pr_debug("%s: perf_evsel__newtp\n", __func__);
45 goto out_delete_evlist;
46 }
47
48 perf_evlist__add(evlist, evsel);
49
50 err = perf_evlist__create_maps(evlist, &opts.target);
51 if (err < 0) {
52 pr_debug("%s: perf_evlist__create_maps\n", __func__);
53 goto out_delete_evlist;
54 }
55
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030056 perf_evsel__config(evsel, &opts, NULL);
Jiri Olsadc447ee2012-11-10 01:46:49 +010057
Jiri Olsae13798c2015-06-23 00:36:02 +020058 thread_map__set_pid(evlist->threads, 0, getpid());
Jiri Olsadc447ee2012-11-10 01:46:49 +010059
60 err = perf_evlist__open(evlist);
61 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000062 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030063 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030064 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010065 }
66
Wang Nanf74b9d3a2017-12-03 02:00:37 +000067 err = perf_evlist__mmap(evlist, UINT_MAX);
Jiri Olsadc447ee2012-11-10 01:46:49 +010068 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000069 pr_debug("perf_evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030070 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030071 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010072 }
73
74 perf_evlist__enable(evlist);
75
76 /*
77 * Generate the event:
78 */
Riku Voipio43f322b2015-04-16 16:52:53 +030079 openat(AT_FDCWD, filename, flags);
Jiri Olsadc447ee2012-11-10 01:46:49 +010080
81 while (1) {
82 int before = nr_events;
83
84 for (i = 0; i < evlist->nr_mmaps; i++) {
85 union perf_event *event;
86
87 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
88 const u32 type = event->header.type;
89 int tp_flags;
90 struct perf_sample sample;
91
92 ++nr_events;
93
Zhouyi Zhou8e50d382013-10-24 15:43:33 +080094 if (type != PERF_RECORD_SAMPLE) {
95 perf_evlist__mmap_consume(evlist, i);
Jiri Olsadc447ee2012-11-10 01:46:49 +010096 continue;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +080097 }
Jiri Olsadc447ee2012-11-10 01:46:49 +010098
99 err = perf_evsel__parse_sample(evsel, event, &sample);
100 if (err) {
Namhyung Kim87191382015-10-20 00:23:48 +0900101 pr_debug("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300102 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100103 }
104
105 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
106
107 if (flags != tp_flags) {
108 pr_debug("%s: Expected flags=%#x, got %#x\n",
109 __func__, flags, tp_flags);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300110 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100111 }
112
113 goto out_ok;
114 }
115 }
116
117 if (nr_events == before)
Arnaldo Carvalho de Melof66a889d2014-08-18 17:25:59 -0300118 perf_evlist__poll(evlist, 10);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100119
120 if (++nr_polls > 5) {
121 pr_debug("%s: no events!\n", __func__);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300122 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100123 }
124 }
125out_ok:
126 err = 0;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100127out_delete_evlist:
128 perf_evlist__delete(evlist);
129out:
130 return err;
131}