spintest_user.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <bpf/libbpf.h>
  7. #include <bpf/bpf.h>
  8. #include "trace_helpers.h"
  9. int main(int ac, char **argv)
  10. {
  11. struct bpf_object *obj = NULL;
  12. struct bpf_link *links[20];
  13. long key, next_key, value;
  14. struct bpf_program *prog;
  15. int map_fd, i, j = 0;
  16. char filename[256];
  17. struct ksym *sym;
  18. if (load_kallsyms()) {
  19. printf("failed to process /proc/kallsyms\n");
  20. return 2;
  21. }
  22. snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
  23. obj = bpf_object__open_file(filename, NULL);
  24. if (libbpf_get_error(obj)) {
  25. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  26. obj = NULL;
  27. goto cleanup;
  28. }
  29. /* load BPF program */
  30. if (bpf_object__load(obj)) {
  31. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  32. goto cleanup;
  33. }
  34. map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
  35. if (map_fd < 0) {
  36. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  37. goto cleanup;
  38. }
  39. bpf_object__for_each_program(prog, obj) {
  40. links[j] = bpf_program__attach(prog);
  41. if (libbpf_get_error(links[j])) {
  42. fprintf(stderr, "bpf_program__attach failed\n");
  43. links[j] = NULL;
  44. goto cleanup;
  45. }
  46. j++;
  47. }
  48. for (i = 0; i < 5; i++) {
  49. key = 0;
  50. printf("kprobing funcs:");
  51. while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
  52. bpf_map_lookup_elem(map_fd, &next_key, &value);
  53. assert(next_key == value);
  54. sym = ksym_search(value);
  55. key = next_key;
  56. if (!sym) {
  57. printf("ksym not found. Is kallsyms loaded?\n");
  58. continue;
  59. }
  60. printf(" %s", sym->name);
  61. }
  62. if (key)
  63. printf("\n");
  64. key = 0;
  65. while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0)
  66. bpf_map_delete_elem(map_fd, &next_key);
  67. sleep(1);
  68. }
  69. cleanup:
  70. for (j--; j >= 0; j--)
  71. bpf_link__destroy(links[j]);
  72. bpf_object__close(obj);
  73. return 0;
  74. }