openat-syscall-tp-fields.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <linux/err.h>
  2. #include "perf.h"
  3. #include "evlist.h"
  4. #include "evsel.h"
  5. #include "thread_map.h"
  6. #include "tests.h"
  7. #include "debug.h"
  8. int test__syscall_openat_tp_fields(void)
  9. {
  10. struct record_opts opts = {
  11. .target = {
  12. .uid = UINT_MAX,
  13. .uses_mmap = true,
  14. },
  15. .no_buffering = true,
  16. .freq = 1,
  17. .mmap_pages = 256,
  18. .raw_samples = true,
  19. };
  20. const char *filename = "/etc/passwd";
  21. int flags = O_RDONLY | O_DIRECTORY;
  22. struct perf_evlist *evlist = perf_evlist__new();
  23. struct perf_evsel *evsel;
  24. int err = -1, i, nr_events = 0, nr_polls = 0;
  25. char sbuf[STRERR_BUFSIZE];
  26. if (evlist == NULL) {
  27. pr_debug("%s: perf_evlist__new\n", __func__);
  28. goto out;
  29. }
  30. evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
  31. if (IS_ERR(evsel)) {
  32. pr_debug("%s: perf_evsel__newtp\n", __func__);
  33. goto out_delete_evlist;
  34. }
  35. perf_evlist__add(evlist, evsel);
  36. err = perf_evlist__create_maps(evlist, &opts.target);
  37. if (err < 0) {
  38. pr_debug("%s: perf_evlist__create_maps\n", __func__);
  39. goto out_delete_evlist;
  40. }
  41. perf_evsel__config(evsel, &opts);
  42. thread_map__set_pid(evlist->threads, 0, getpid());
  43. err = perf_evlist__open(evlist);
  44. if (err < 0) {
  45. pr_debug("perf_evlist__open: %s\n",
  46. strerror_r(errno, sbuf, sizeof(sbuf)));
  47. goto out_delete_evlist;
  48. }
  49. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  50. if (err < 0) {
  51. pr_debug("perf_evlist__mmap: %s\n",
  52. strerror_r(errno, sbuf, sizeof(sbuf)));
  53. goto out_delete_evlist;
  54. }
  55. perf_evlist__enable(evlist);
  56. /*
  57. * Generate the event:
  58. */
  59. openat(AT_FDCWD, filename, flags);
  60. while (1) {
  61. int before = nr_events;
  62. for (i = 0; i < evlist->nr_mmaps; i++) {
  63. union perf_event *event;
  64. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  65. const u32 type = event->header.type;
  66. int tp_flags;
  67. struct perf_sample sample;
  68. ++nr_events;
  69. if (type != PERF_RECORD_SAMPLE) {
  70. perf_evlist__mmap_consume(evlist, i);
  71. continue;
  72. }
  73. err = perf_evsel__parse_sample(evsel, event, &sample);
  74. if (err) {
  75. pr_debug("Can't parse sample, err = %d\n", err);
  76. goto out_delete_evlist;
  77. }
  78. tp_flags = perf_evsel__intval(evsel, &sample, "flags");
  79. if (flags != tp_flags) {
  80. pr_debug("%s: Expected flags=%#x, got %#x\n",
  81. __func__, flags, tp_flags);
  82. goto out_delete_evlist;
  83. }
  84. goto out_ok;
  85. }
  86. }
  87. if (nr_events == before)
  88. perf_evlist__poll(evlist, 10);
  89. if (++nr_polls > 5) {
  90. pr_debug("%s: no events!\n", __func__);
  91. goto out_delete_evlist;
  92. }
  93. }
  94. out_ok:
  95. err = 0;
  96. out_delete_evlist:
  97. perf_evlist__delete(evlist);
  98. out:
  99. return err;
  100. }