openat-syscall.c 1.6 KB

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