Linux Perf
openat-syscall-tp-fields.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/err.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include "perf.h"
7 #include "evlist.h"
8 #include "evsel.h"
9 #include "thread_map.h"
10 #include "tests.h"
11 #include "debug.h"
12 #include <errno.h>
13 
14 #ifndef O_DIRECTORY
15 #define O_DIRECTORY 00200000
16 #endif
17 #ifndef AT_FDCWD
18 #define AT_FDCWD -100
19 #endif
20 
21 int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
22 {
23  struct record_opts opts = {
24  .target = {
25  .uid = UINT_MAX,
26  .uses_mmap = true,
27  },
28  .no_buffering = true,
29  .freq = 1,
30  .mmap_pages = 256,
31  .raw_samples = true,
32  };
33  const char *filename = "/etc/passwd";
34  int flags = O_RDONLY | O_DIRECTORY;
35  struct perf_evlist *evlist = perf_evlist__new();
36  struct perf_evsel *evsel;
37  int err = -1, i, nr_events = 0, nr_polls = 0;
38  char sbuf[STRERR_BUFSIZE];
39 
40  if (evlist == NULL) {
41  pr_debug("%s: perf_evlist__new\n", __func__);
42  goto out;
43  }
44 
45  evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
46  if (IS_ERR(evsel)) {
47  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 
59  perf_evsel__config(evsel, &opts, NULL);
60 
61  thread_map__set_pid(evlist->threads, 0, getpid());
62 
63  err = perf_evlist__open(evlist);
64  if (err < 0) {
65  pr_debug("perf_evlist__open: %s\n",
66  str_error_r(errno, sbuf, sizeof(sbuf)));
67  goto out_delete_evlist;
68  }
69 
70  err = perf_evlist__mmap(evlist, UINT_MAX);
71  if (err < 0) {
72  pr_debug("perf_evlist__mmap: %s\n",
73  str_error_r(errno, sbuf, sizeof(sbuf)));
74  goto out_delete_evlist;
75  }
76 
77  perf_evlist__enable(evlist);
78 
79  /*
80  * Generate the event:
81  */
82  openat(AT_FDCWD, filename, flags);
83 
84  while (1) {
85  int before = nr_events;
86 
87  for (i = 0; i < evlist->nr_mmaps; i++) {
88  union perf_event *event;
89  struct perf_mmap *md;
90 
91  md = &evlist->mmap[i];
92  if (perf_mmap__read_init(md) < 0)
93  continue;
94 
95  while ((event = perf_mmap__read_event(md)) != NULL) {
96  const u32 type = event->header.type;
97  int tp_flags;
98  struct perf_sample sample;
99 
100  ++nr_events;
101 
102  if (type != PERF_RECORD_SAMPLE) {
103  perf_mmap__consume(md);
104  continue;
105  }
106 
107  err = perf_evsel__parse_sample(evsel, event, &sample);
108  if (err) {
109  pr_debug("Can't parse sample, err = %d\n", err);
110  goto out_delete_evlist;
111  }
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);
118  goto out_delete_evlist;
119  }
120 
121  goto out_ok;
122  }
124  }
125 
126  if (nr_events == before)
127  perf_evlist__poll(evlist, 10);
128 
129  if (++nr_polls > 5) {
130  pr_debug("%s: no events!\n", __func__);
131  goto out_delete_evlist;
132  }
133  }
134 out_ok:
135  err = 0;
136 out_delete_evlist:
137  perf_evlist__delete(evlist);
138 out:
139  return err;
140 }
void perf_mmap__consume(struct perf_mmap *map)
Definition: mmap.c:118
#define O_DIRECTORY
int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Definition: evlist.c:1066
struct perf_mmap * mmap
Definition: evlist.h:45
const char * filename
Definition: hists_common.c:26
void perf_evlist__enable(struct perf_evlist *evlist)
Definition: evlist.c:369
int int err
Definition: 5sec.c:44
static void thread_map__set_pid(struct thread_map *map, int thread, pid_t pid)
Definition: thread_map.h:52
void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts, struct callchain_param *callchain)
Definition: evsel.c:879
void perf_evlist__delete(struct perf_evlist *evlist)
Definition: evlist.c:133
struct thread_map * threads
Definition: evlist.h:47
Definition: mmap.h:17
struct target target
Definition: perf.h:38
int perf_mmap__read_init(struct perf_mmap *map)
Definition: mmap.c:271
int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
Definition: evlist.c:491
#define pr_debug(fmt,...)
Definition: json.h:27
int perf_evlist__open(struct perf_evlist *evlist)
Definition: evlist.c:1398
void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
Definition: evlist.c:176
#define event
u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, const char *name)
Definition: evsel.c:2722
int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages)
Definition: evlist.c:1061
int nr_mmaps
Definition: evlist.h:32
#define AT_FDCWD
void perf_mmap__read_done(struct perf_mmap *map)
Definition: mmap.c:329
uid_t uid
Definition: target.h:13
union perf_event * perf_mmap__read_event(struct perf_mmap *map)
Definition: mmap.c:78
Definition: tests.h:30
u32 flags
#define STRERR_BUFSIZE
Definition: debug.h:43
int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, struct perf_sample *data)
Definition: evsel.c:2059
static struct perf_evsel * perf_evsel__newtp(const char *sys, const char *name)
Definition: evsel.h:207
int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused)
struct perf_evlist * perf_evlist__new(void)
Definition: evlist.c:54