syscall_tp_user.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017 Facebook
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <linux/perf_event.h>
  10. #include <errno.h>
  11. #include <bpf/libbpf.h>
  12. #include <bpf/bpf.h>
  13. /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
  14. * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
  15. */
  16. static void usage(const char *cmd)
  17. {
  18. printf("USAGE: %s [-i nr_tests] [-h]\n", cmd);
  19. printf(" -i nr_tests # rounds of test to run\n");
  20. printf(" -h # help\n");
  21. }
  22. static void verify_map(int map_id)
  23. {
  24. __u32 key = 0;
  25. __u32 val;
  26. if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
  27. fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
  28. return;
  29. }
  30. if (val == 0) {
  31. fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
  32. return;
  33. }
  34. printf("verify map:%d val: %d\n", map_id, val);
  35. val = 0;
  36. if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) {
  37. fprintf(stderr, "map_update failed: %s\n", strerror(errno));
  38. return;
  39. }
  40. }
  41. static int test(char *filename, int nr_tests)
  42. {
  43. int map0_fds[nr_tests], map1_fds[nr_tests], fd, i, j = 0;
  44. struct bpf_link **links = NULL;
  45. struct bpf_object *objs[nr_tests];
  46. struct bpf_program *prog;
  47. for (i = 0; i < nr_tests; i++) {
  48. objs[i] = bpf_object__open_file(filename, NULL);
  49. if (libbpf_get_error(objs[i])) {
  50. fprintf(stderr, "opening BPF object file failed\n");
  51. objs[i] = NULL;
  52. goto cleanup;
  53. }
  54. /* One-time initialization */
  55. if (!links) {
  56. int nr_progs = 0;
  57. bpf_object__for_each_program(prog, objs[i])
  58. nr_progs += 1;
  59. links = calloc(nr_progs * nr_tests, sizeof(struct bpf_link *));
  60. if (!links)
  61. goto cleanup;
  62. }
  63. /* load BPF program */
  64. if (bpf_object__load(objs[i])) {
  65. fprintf(stderr, "loading BPF object file failed\n");
  66. goto cleanup;
  67. }
  68. map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
  69. "enter_open_map");
  70. map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
  71. "exit_open_map");
  72. if (map0_fds[i] < 0 || map1_fds[i] < 0) {
  73. fprintf(stderr, "finding a map in obj file failed\n");
  74. goto cleanup;
  75. }
  76. bpf_object__for_each_program(prog, objs[i]) {
  77. links[j] = bpf_program__attach(prog);
  78. if (libbpf_get_error(links[j])) {
  79. fprintf(stderr, "bpf_program__attach failed\n");
  80. links[j] = NULL;
  81. goto cleanup;
  82. }
  83. j++;
  84. }
  85. printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]);
  86. }
  87. /* current load_bpf_file has perf_event_open default pid = -1
  88. * and cpu = 0, which permits attached bpf execution on
  89. * all cpus for all pid's. bpf program execution ignores
  90. * cpu affinity.
  91. */
  92. /* trigger some "open" operations */
  93. fd = open(filename, O_RDONLY);
  94. if (fd < 0) {
  95. fprintf(stderr, "open failed: %s\n", strerror(errno));
  96. return 1;
  97. }
  98. close(fd);
  99. /* verify the map */
  100. for (i = 0; i < nr_tests; i++) {
  101. verify_map(map0_fds[i]);
  102. verify_map(map1_fds[i]);
  103. }
  104. cleanup:
  105. if (links) {
  106. for (j--; j >= 0; j--)
  107. bpf_link__destroy(links[j]);
  108. free(links);
  109. }
  110. for (i--; i >= 0; i--)
  111. bpf_object__close(objs[i]);
  112. return 0;
  113. }
  114. int main(int argc, char **argv)
  115. {
  116. int opt, nr_tests = 1;
  117. char filename[256];
  118. while ((opt = getopt(argc, argv, "i:h")) != -1) {
  119. switch (opt) {
  120. case 'i':
  121. nr_tests = atoi(optarg);
  122. break;
  123. case 'h':
  124. default:
  125. usage(argv[0]);
  126. return 0;
  127. }
  128. }
  129. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  130. return test(filename, nr_tests);
  131. }