openat-syscall.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <api/fs/tracing_path.h>
  5. #include <linux/err.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include "thread_map.h"
  10. #include "evsel.h"
  11. #include "debug.h"
  12. #include "tests.h"
  13. int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused)
  14. {
  15. int err = -1, fd;
  16. 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. if (threads == NULL) {
  22. pr_debug("thread_map__new\n");
  23. return -1;
  24. }
  25. evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
  26. if (IS_ERR(evsel)) {
  27. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
  28. pr_debug("%s\n", errbuf);
  29. goto out_thread_map_delete;
  30. }
  31. if (perf_evsel__open_per_thread(evsel, threads) < 0) {
  32. pr_debug("failed to open counter: %s, "
  33. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  34. str_error_r(errno, sbuf, sizeof(sbuf)));
  35. goto out_evsel_delete;
  36. }
  37. for (i = 0; i < nr_openat_calls; ++i) {
  38. fd = openat(0, "/etc/passwd", O_RDONLY);
  39. close(fd);
  40. }
  41. if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
  42. pr_debug("perf_evsel__read_on_cpu\n");
  43. goto out_close_fd;
  44. }
  45. if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
  46. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
  47. nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
  48. goto out_close_fd;
  49. }
  50. err = 0;
  51. out_close_fd:
  52. perf_evsel__close_fd(evsel);
  53. out_evsel_delete:
  54. perf_evsel__delete(evsel);
  55. out_thread_map_delete:
  56. thread_map__put(threads);
  57. return err;
  58. }