openat-syscall.c 1.5 KB

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