openat-syscall-tp-fields.c 2.5 KB
Newer Older
1
#include <linux/err.h>
2 3 4 5 6
#include "perf.h"
#include "evlist.h"
#include "evsel.h"
#include "thread_map.h"
#include "tests.h"
7
#include "debug.h"
8

9
int test__syscall_openat_tp_fields(void)
10
{
11
	struct record_opts opts = {
12 13 14 15
		.target = {
			.uid = UINT_MAX,
			.uses_mmap = true,
		},
16 17 18 19
		.no_buffering = true,
		.freq	      = 1,
		.mmap_pages   = 256,
		.raw_samples  = true,
20 21 22
	};
	const char *filename = "/etc/passwd";
	int flags = O_RDONLY | O_DIRECTORY;
23
	struct perf_evlist *evlist = perf_evlist__new();
24 25
	struct perf_evsel *evsel;
	int err = -1, i, nr_events = 0, nr_polls = 0;
26
	char sbuf[STRERR_BUFSIZE];
27 28 29 30 31 32

	if (evlist == NULL) {
		pr_debug("%s: perf_evlist__new\n", __func__);
		goto out;
	}

33
	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
34
	if (IS_ERR(evsel)) {
35 36 37 38 39 40 41 42 43 44 45 46
		pr_debug("%s: perf_evsel__newtp\n", __func__);
		goto out_delete_evlist;
	}

	perf_evlist__add(evlist, evsel);

	err = perf_evlist__create_maps(evlist, &opts.target);
	if (err < 0) {
		pr_debug("%s: perf_evlist__create_maps\n", __func__);
		goto out_delete_evlist;
	}

47
	perf_evsel__config(evsel, &opts);
48

49
	thread_map__set_pid(evlist->threads, 0, getpid());
50 51 52

	err = perf_evlist__open(evlist);
	if (err < 0) {
53 54
		pr_debug("perf_evlist__open: %s\n",
			 strerror_r(errno, sbuf, sizeof(sbuf)));
55
		goto out_delete_evlist;
56 57 58 59
	}

	err = perf_evlist__mmap(evlist, UINT_MAX, false);
	if (err < 0) {
60 61
		pr_debug("perf_evlist__mmap: %s\n",
			 strerror_r(errno, sbuf, sizeof(sbuf)));
62
		goto out_delete_evlist;
63 64 65 66 67 68 69
	}

	perf_evlist__enable(evlist);

	/*
	 * Generate the event:
	 */
70
	openat(AT_FDCWD, filename, flags);
71 72 73 74 75 76 77 78 79 80 81 82 83 84

	while (1) {
		int before = nr_events;

		for (i = 0; i < evlist->nr_mmaps; i++) {
			union perf_event *event;

			while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
				const u32 type = event->header.type;
				int tp_flags;
				struct perf_sample sample;

				++nr_events;

85 86
				if (type != PERF_RECORD_SAMPLE) {
					perf_evlist__mmap_consume(evlist, i);
87
					continue;
88
				}
89 90 91 92

				err = perf_evsel__parse_sample(evsel, event, &sample);
				if (err) {
					pr_err("Can't parse sample, err = %d\n", err);
93
					goto out_delete_evlist;
94 95 96 97 98 99 100
				}

				tp_flags = perf_evsel__intval(evsel, &sample, "flags");

				if (flags != tp_flags) {
					pr_debug("%s: Expected flags=%#x, got %#x\n",
						 __func__, flags, tp_flags);
101
					goto out_delete_evlist;
102 103 104 105 106 107 108
				}

				goto out_ok;
			}
		}

		if (nr_events == before)
109
			perf_evlist__poll(evlist, 10);
110 111 112

		if (++nr_polls > 5) {
			pr_debug("%s: no events!\n", __func__);
113
			goto out_delete_evlist;
114 115 116 117 118 119 120 121 122
		}
	}
out_ok:
	err = 0;
out_delete_evlist:
	perf_evlist__delete(evlist);
out:
	return err;
}