openat-syscall.c 1.7 KB
Newer Older
1
#include <errno.h>
2
#include <inttypes.h>
3
#include <api/fs/tracing_path.h>
4
#include <linux/err.h>
5 6 7
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
8 9 10 11 12
#include "thread_map.h"
#include "evsel.h"
#include "debug.h"
#include "tests.h"

13
int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused)
14 15 16
{
	int err = -1, fd;
	struct perf_evsel *evsel;
17
	unsigned int nr_openat_calls = 111, i;
18
	struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
19
	char sbuf[STRERR_BUFSIZE];
20
	char errbuf[BUFSIZ];
21 22 23 24 25 26

	if (threads == NULL) {
		pr_debug("thread_map__new\n");
		return -1;
	}

27
	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
28
	if (IS_ERR(evsel)) {
29
		tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
30
		pr_debug("%s\n", errbuf);
31 32 33 34 35 36
		goto out_thread_map_delete;
	}

	if (perf_evsel__open_per_thread(evsel, threads) < 0) {
		pr_debug("failed to open counter: %s, "
			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
37
			 str_error_r(errno, sbuf, sizeof(sbuf)));
38 39 40
		goto out_evsel_delete;
	}

41 42
	for (i = 0; i < nr_openat_calls; ++i) {
		fd = openat(0, "/etc/passwd", O_RDONLY);
43 44 45 46 47 48 49 50
		close(fd);
	}

	if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
		pr_debug("perf_evsel__read_on_cpu\n");
		goto out_close_fd;
	}

51
	if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
52
		pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
53
			 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
54 55 56 57 58
		goto out_close_fd;
	}

	err = 0;
out_close_fd:
59
	perf_evsel__close_fd(evsel);
60 61 62
out_evsel_delete:
	perf_evsel__delete(evsel);
out_thread_map_delete:
63
	thread_map__put(threads);
64 65
	return err;
}