syscall_tp_user.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (c) 2017 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <stdlib.h>
  11. #include <signal.h>
  12. #include <linux/bpf.h>
  13. #include <string.h>
  14. #include <linux/perf_event.h>
  15. #include <errno.h>
  16. #include <assert.h>
  17. #include <stdbool.h>
  18. #include <sys/resource.h>
  19. #include <bpf/bpf.h>
  20. #include "bpf_load.h"
  21. /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
  22. * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
  23. */
  24. static void usage(const char *cmd)
  25. {
  26. printf("USAGE: %s [-i num_progs] [-h]\n", cmd);
  27. printf(" -i num_progs # number of progs of the test\n");
  28. printf(" -h # help\n");
  29. }
  30. static void verify_map(int map_id)
  31. {
  32. __u32 key = 0;
  33. __u32 val;
  34. if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
  35. fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
  36. return;
  37. }
  38. if (val == 0) {
  39. fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
  40. return;
  41. }
  42. val = 0;
  43. if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) {
  44. fprintf(stderr, "map_update failed: %s\n", strerror(errno));
  45. return;
  46. }
  47. }
  48. static int test(char *filename, int num_progs)
  49. {
  50. int i, fd, map0_fds[num_progs], map1_fds[num_progs];
  51. for (i = 0; i < num_progs; i++) {
  52. if (load_bpf_file(filename)) {
  53. fprintf(stderr, "%s", bpf_log_buf);
  54. return 1;
  55. }
  56. printf("prog #%d: map ids %d %d\n", i, map_fd[0], map_fd[1]);
  57. map0_fds[i] = map_fd[0];
  58. map1_fds[i] = map_fd[1];
  59. }
  60. /* current load_bpf_file has perf_event_open default pid = -1
  61. * and cpu = 0, which permits attached bpf execution on
  62. * all cpus for all pid's. bpf program execution ignores
  63. * cpu affinity.
  64. */
  65. /* trigger some "open" operations */
  66. fd = open(filename, O_RDONLY);
  67. if (fd < 0) {
  68. fprintf(stderr, "open failed: %s\n", strerror(errno));
  69. return 1;
  70. }
  71. close(fd);
  72. /* verify the map */
  73. for (i = 0; i < num_progs; i++) {
  74. verify_map(map0_fds[i]);
  75. verify_map(map1_fds[i]);
  76. }
  77. return 0;
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  82. int opt, num_progs = 1;
  83. char filename[256];
  84. while ((opt = getopt(argc, argv, "i:h")) != -1) {
  85. switch (opt) {
  86. case 'i':
  87. num_progs = atoi(optarg);
  88. break;
  89. case 'h':
  90. default:
  91. usage(argv[0]);
  92. return 0;
  93. }
  94. }
  95. setrlimit(RLIMIT_MEMLOCK, &r);
  96. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  97. return test(filename, num_progs);
  98. }