blob: 127dcae0b76033cf457a4a4ff572da6771594e6f [file] [log] [blame]
Jiri Olsadc447ee2012-11-10 01:46:49 +01001#include "perf.h"
2#include "evlist.h"
3#include "evsel.h"
4#include "thread_map.h"
5#include "tests.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +02006#include "debug.h"
Jiri Olsadc447ee2012-11-10 01:46:49 +01007
8int test__syscall_open_tp_fields(void)
9{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030010 struct record_opts opts = {
Jiri Olsadc447ee2012-11-10 01:46:49 +010011 .target = {
12 .uid = UINT_MAX,
13 .uses_mmap = true,
14 },
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -030015 .no_buffering = true,
16 .freq = 1,
17 .mmap_pages = 256,
18 .raw_samples = true,
Jiri Olsadc447ee2012-11-10 01:46:49 +010019 };
20 const char *filename = "/etc/passwd";
21 int flags = O_RDONLY | O_DIRECTORY;
Namhyung Kim334fe7a2013-03-11 16:43:12 +090022 struct perf_evlist *evlist = perf_evlist__new();
Jiri Olsadc447ee2012-11-10 01:46:49 +010023 struct perf_evsel *evsel;
24 int err = -1, i, nr_events = 0, nr_polls = 0;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000025 char sbuf[STRERR_BUFSIZE];
Jiri Olsadc447ee2012-11-10 01:46:49 +010026
27 if (evlist == NULL) {
28 pr_debug("%s: perf_evlist__new\n", __func__);
29 goto out;
30 }
31
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -030032 evsel = perf_evsel__newtp("syscalls", "sys_enter_open");
Jiri Olsadc447ee2012-11-10 01:46:49 +010033 if (evsel == NULL) {
34 pr_debug("%s: perf_evsel__newtp\n", __func__);
35 goto out_delete_evlist;
36 }
37
38 perf_evlist__add(evlist, evsel);
39
40 err = perf_evlist__create_maps(evlist, &opts.target);
41 if (err < 0) {
42 pr_debug("%s: perf_evlist__create_maps\n", __func__);
43 goto out_delete_evlist;
44 }
45
Jiri Olsacac21422012-11-12 18:34:00 +010046 perf_evsel__config(evsel, &opts);
Jiri Olsadc447ee2012-11-10 01:46:49 +010047
48 evlist->threads->map[0] = getpid();
49
50 err = perf_evlist__open(evlist);
51 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000052 pr_debug("perf_evlist__open: %s\n",
53 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030054 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010055 }
56
57 err = perf_evlist__mmap(evlist, UINT_MAX, false);
58 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000059 pr_debug("perf_evlist__mmap: %s\n",
60 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030061 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010062 }
63
64 perf_evlist__enable(evlist);
65
66 /*
67 * Generate the event:
68 */
69 open(filename, flags);
70
71 while (1) {
72 int before = nr_events;
73
74 for (i = 0; i < evlist->nr_mmaps; i++) {
75 union perf_event *event;
76
77 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
78 const u32 type = event->header.type;
79 int tp_flags;
80 struct perf_sample sample;
81
82 ++nr_events;
83
Zhouyi Zhou8e50d382013-10-24 15:43:33 +080084 if (type != PERF_RECORD_SAMPLE) {
85 perf_evlist__mmap_consume(evlist, i);
Jiri Olsadc447ee2012-11-10 01:46:49 +010086 continue;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +080087 }
Jiri Olsadc447ee2012-11-10 01:46:49 +010088
89 err = perf_evsel__parse_sample(evsel, event, &sample);
90 if (err) {
91 pr_err("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -030092 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +010093 }
94
95 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
96
97 if (flags != tp_flags) {
98 pr_debug("%s: Expected flags=%#x, got %#x\n",
99 __func__, flags, tp_flags);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300100 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100101 }
102
103 goto out_ok;
104 }
105 }
106
107 if (nr_events == before)
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300108 perf_evlist__poll(evlist, 10);
Jiri Olsadc447ee2012-11-10 01:46:49 +0100109
110 if (++nr_polls > 5) {
111 pr_debug("%s: no events!\n", __func__);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300112 goto out_delete_evlist;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100113 }
114 }
115out_ok:
116 err = 0;
Jiri Olsadc447ee2012-11-10 01:46:49 +0100117out_delete_evlist:
118 perf_evlist__delete(evlist);
119out:
120 return err;
121}