test_libbpf_open.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* SPDX-License-Identifier: GPL-2.0
  2. * Copyright (c) 2018 Jesper Dangaard Brouer, Red Hat Inc.
  3. */
  4. static const char *__doc__ =
  5. "Libbpf test program for loading BPF ELF object files";
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include <bpf/libbpf.h>
  11. #include <getopt.h>
  12. #include "bpf_rlimit.h"
  13. static const struct option long_options[] = {
  14. {"help", no_argument, NULL, 'h' },
  15. {"debug", no_argument, NULL, 'D' },
  16. {"quiet", no_argument, NULL, 'q' },
  17. {0, 0, NULL, 0 }
  18. };
  19. static void usage(char *argv[])
  20. {
  21. int i;
  22. printf("\nDOCUMENTATION:\n%s\n\n", __doc__);
  23. printf(" Usage: %s (options-see-below) BPF_FILE\n", argv[0]);
  24. printf(" Listing options:\n");
  25. for (i = 0; long_options[i].name != 0; i++) {
  26. printf(" --%-12s", long_options[i].name);
  27. printf(" short-option: -%c",
  28. long_options[i].val);
  29. printf("\n");
  30. }
  31. printf("\n");
  32. }
  33. #define DEFINE_PRINT_FN(name, enabled) \
  34. static int libbpf_##name(const char *fmt, ...) \
  35. { \
  36. va_list args; \
  37. int ret; \
  38. \
  39. va_start(args, fmt); \
  40. if (enabled) { \
  41. fprintf(stderr, "[" #name "] "); \
  42. ret = vfprintf(stderr, fmt, args); \
  43. } \
  44. va_end(args); \
  45. return ret; \
  46. }
  47. DEFINE_PRINT_FN(warning, 1)
  48. DEFINE_PRINT_FN(info, 1)
  49. DEFINE_PRINT_FN(debug, 1)
  50. #define EXIT_FAIL_LIBBPF EXIT_FAILURE
  51. #define EXIT_FAIL_OPTION 2
  52. int test_walk_progs(struct bpf_object *obj, bool verbose)
  53. {
  54. struct bpf_program *prog;
  55. int cnt = 0;
  56. bpf_object__for_each_program(prog, obj) {
  57. cnt++;
  58. if (verbose)
  59. printf("Prog (count:%d) section_name: %s\n", cnt,
  60. bpf_program__title(prog, false));
  61. }
  62. return 0;
  63. }
  64. int test_walk_maps(struct bpf_object *obj, bool verbose)
  65. {
  66. struct bpf_map *map;
  67. int cnt = 0;
  68. bpf_map__for_each(map, obj) {
  69. cnt++;
  70. if (verbose)
  71. printf("Map (count:%d) name: %s\n", cnt,
  72. bpf_map__name(map));
  73. }
  74. return 0;
  75. }
  76. int test_open_file(char *filename, bool verbose)
  77. {
  78. struct bpf_object *bpfobj = NULL;
  79. long err;
  80. if (verbose)
  81. printf("Open BPF ELF-file with libbpf: %s\n", filename);
  82. /* Load BPF ELF object file and check for errors */
  83. bpfobj = bpf_object__open(filename);
  84. err = libbpf_get_error(bpfobj);
  85. if (err) {
  86. char err_buf[128];
  87. libbpf_strerror(err, err_buf, sizeof(err_buf));
  88. if (verbose)
  89. printf("Unable to load eBPF objects in file '%s': %s\n",
  90. filename, err_buf);
  91. return EXIT_FAIL_LIBBPF;
  92. }
  93. test_walk_progs(bpfobj, verbose);
  94. test_walk_maps(bpfobj, verbose);
  95. if (verbose)
  96. printf("Close BPF ELF-file with libbpf: %s\n",
  97. bpf_object__name(bpfobj));
  98. bpf_object__close(bpfobj);
  99. return 0;
  100. }
  101. int main(int argc, char **argv)
  102. {
  103. char filename[1024] = { 0 };
  104. bool verbose = 1;
  105. int longindex = 0;
  106. int opt;
  107. libbpf_set_print(libbpf_warning, libbpf_info, NULL);
  108. /* Parse commands line args */
  109. while ((opt = getopt_long(argc, argv, "hDq",
  110. long_options, &longindex)) != -1) {
  111. switch (opt) {
  112. case 'D':
  113. libbpf_set_print(libbpf_warning, libbpf_info,
  114. libbpf_debug);
  115. break;
  116. case 'q': /* Use in scripting mode */
  117. verbose = 0;
  118. break;
  119. case 'h':
  120. default:
  121. usage(argv);
  122. return EXIT_FAIL_OPTION;
  123. }
  124. }
  125. if (optind >= argc) {
  126. usage(argv);
  127. printf("ERROR: Expected BPF_FILE argument after options\n");
  128. return EXIT_FAIL_OPTION;
  129. }
  130. snprintf(filename, sizeof(filename), "%s", argv[optind]);
  131. return test_open_file(filename, verbose);
  132. }