blob: 7837ae93667922ff5d51a7776c56510f4e021439 [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;
Namhyung Kim334fe7a2013-03-11 16:43:12 +090035 struct perf_evlist *evlist = perf_evlist__new();
Jiri Olsadc447ee2012-11-10 01:46:49 +010036 struct perf_evsel *evsel;
37 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
51 perf_evlist__add(evlist, evsel);
52
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 Olsae13798c2015-06-23 00:36:02 +020061 thread_map__set_pid(evlist->threads, 0, getpid());
Jiri Olsadc447ee2012-11-10 01:46:49 +010062
63 err = perf_evlist__open(evlist);
64 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
77 perf_evlist__enable(evlist);
78
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;
90 u64 end, start;
Jiri Olsadc447ee2012-11-10 01:46:49 +010091
Kan Liang1d1b56322018-03-01 18:09:05 -050092 md = &evlist->mmap[i];
93 if (perf_mmap__read_init(md, false, &start, &end) < 0)
94 continue;
95
96 while ((event = perf_mmap__read_event(md, false, &start, end)) != NULL) {
Jiri Olsadc447ee2012-11-10 01:46:49 +010097 const u32 type = event->header.type;
98 int tp_flags;
99 struct perf_sample sample;
100
101 ++nr_events;
102
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800103 if (type != PERF_RECORD_SAMPLE) {
Kan Liangd6ace3d2018-03-06 10:36:05 -0500104 perf_mmap__consume(md);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100105 continue;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800106 }
Jiri Olsadc447ee2012-11-10 01:46:49 +0100107
108 err = perf_evsel__parse_sample(evsel, event, &sample);
109 if (err) {
Namhyung Kim87191382015-10-20 00:23:48 +0900110 pr_debug("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300111 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100112 }
113
114 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
115
116 if (flags != tp_flags) {
117 pr_debug("%s: Expected flags=%#x, got %#x\n",
118 __func__, flags, tp_flags);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300119 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100120 }
121
122 goto out_ok;
123 }
Kan Liang1d1b56322018-03-01 18:09:05 -0500124 perf_mmap__read_done(md);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100125 }
126
127 if (nr_events == before)
Arnaldo Carvalho de Melof66a889d2014-08-18 17:25:59 -0300128 perf_evlist__poll(evlist, 10);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100129
130 if (++nr_polls > 5) {
131 pr_debug("%s: no events!\n", __func__);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300132 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100133 }
134 }
135out_ok:
136 err = 0;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100137out_delete_evlist:
138 perf_evlist__delete(evlist);
139out:
140 return err;
141}