test_libbpf_open.c 3.3 KB

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