syscall_tp_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <sys/resource.h>
  12. #include <bpf/libbpf.h>
  13. #include <bpf/bpf.h>
  14. /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
  15. * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
  16. */
  17. static void usage(const char *cmd)
  18. {
  19. printf("USAGE: %s [-i num_progs] [-h]\n", cmd);
  20. printf(" -i num_progs # number of progs of the test\n");
  21. printf(" -h # help\n");
  22. }
  23. static void verify_map(int map_id)
  24. {
  25. __u32 key = 0;
  26. __u32 val;
  27. if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
  28. fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
  29. return;
  30. }
  31. if (val == 0) {
  32. fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
  33. return;
  34. }
  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 num_progs)
  42. {
  43. int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0;
  44. struct bpf_link *links[num_progs * 4];
  45. struct bpf_object *objs[num_progs];
  46. struct bpf_program *prog;
  47. for (i = 0; i < num_progs; 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. /* load BPF program */
  55. if (bpf_object__load(objs[i])) {
  56. fprintf(stderr, "loading BPF object file failed\n");
  57. goto cleanup;
  58. }
  59. map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
  60. "enter_open_map");
  61. map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
  62. "exit_open_map");
  63. if (map0_fds[i] < 0 || map1_fds[i] < 0) {
  64. fprintf(stderr, "finding a map in obj file failed\n");
  65. goto cleanup;
  66. }
  67. bpf_object__for_each_program(prog, objs[i]) {
  68. links[j] = bpf_program__attach(prog);
  69. if (libbpf_get_error(links[j])) {
  70. fprintf(stderr, "bpf_program__attach failed\n");
  71. links[j] = NULL;
  72. goto cleanup;
  73. }
  74. j++;
  75. }
  76. printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]);
  77. }
  78. /* current load_bpf_file has perf_event_open default pid = -1
  79. * and cpu = 0, which permits attached bpf execution on
  80. * all cpus for all pid's. bpf program execution ignores
  81. * cpu affinity.
  82. */
  83. /* trigger some "open" operations */
  84. fd = open(filename, O_RDONLY);
  85. if (fd < 0) {
  86. fprintf(stderr, "open failed: %s\n", strerror(errno));
  87. return 1;
  88. }
  89. close(fd);
  90. /* verify the map */
  91. for (i = 0; i < num_progs; i++) {
  92. verify_map(map0_fds[i]);
  93. verify_map(map1_fds[i]);
  94. }
  95. cleanup:
  96. for (j--; j >= 0; j--)
  97. bpf_link__destroy(links[j]);
  98. for (i--; i >= 0; i--)
  99. bpf_object__close(objs[i]);
  100. return 0;
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  105. int opt, num_progs = 1;
  106. char filename[256];
  107. while ((opt = getopt(argc, argv, "i:h")) != -1) {
  108. switch (opt) {
  109. case 'i':
  110. num_progs = atoi(optarg);
  111. break;
  112. case 'h':
  113. default:
  114. usage(argv[0]);
  115. return 0;
  116. }
  117. }
  118. setrlimit(RLIMIT_MEMLOCK, &r);
  119. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  120. return test(filename, num_progs);
  121. }