1 /* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2018 Jesper Dangaard Brouer, Red Hat Inc.
4 static const char *__doc__ =
5 "Libbpf test program for loading BPF ELF object files";
11 #include <bpf/libbpf.h>
14 #include "bpf_rlimit.h"
16 static const struct option long_options[] = {
17 {"help", no_argument, NULL, 'h' },
18 {"debug", no_argument, NULL, 'D' },
19 {"quiet", no_argument, NULL, 'q' },
23 static void usage(char *argv[])
27 printf("\nDOCUMENTATION:\n%s\n\n", __doc__);
28 printf(" Usage: %s (options-see-below) BPF_FILE\n", argv[0]);
29 printf(" Listing options:\n");
30 for (i = 0; long_options[i].name != 0; i++) {
31 printf(" --%-12s", long_options[i].name);
32 printf(" short-option: -%c",
39 #define DEFINE_PRINT_FN(name, enabled) \
40 static int libbpf_##name(const char *fmt, ...) \
45 va_start(args, fmt); \
47 fprintf(stderr, "[" #name "] "); \
48 ret = vfprintf(stderr, fmt, args); \
53 DEFINE_PRINT_FN(warning, 1)
54 DEFINE_PRINT_FN(info, 1)
55 DEFINE_PRINT_FN(debug, 1)
57 #define EXIT_FAIL_LIBBPF EXIT_FAILURE
58 #define EXIT_FAIL_OPTION 2
60 int test_walk_progs(struct bpf_object *obj, bool verbose)
62 struct bpf_program *prog;
65 bpf_object__for_each_program(prog, obj) {
68 printf("Prog (count:%d) section_name: %s\n", cnt,
69 bpf_program__title(prog, false));
74 int test_walk_maps(struct bpf_object *obj, bool verbose)
79 bpf_map__for_each(map, obj) {
82 printf("Map (count:%d) name: %s\n", cnt,
88 int test_open_file(char *filename, bool verbose)
90 struct bpf_object *bpfobj = NULL;
94 printf("Open BPF ELF-file with libbpf: %s\n", filename);
96 /* Load BPF ELF object file and check for errors */
97 bpfobj = bpf_object__open(filename);
98 err = libbpf_get_error(bpfobj);
101 libbpf_strerror(err, err_buf, sizeof(err_buf));
103 printf("Unable to load eBPF objects in file '%s': %s\n",
105 return EXIT_FAIL_LIBBPF;
107 test_walk_progs(bpfobj, verbose);
108 test_walk_maps(bpfobj, verbose);
111 printf("Close BPF ELF-file with libbpf: %s\n",
112 bpf_object__name(bpfobj));
113 bpf_object__close(bpfobj);
118 int main(int argc, char **argv)
120 char filename[1024] = { 0 };
125 libbpf_set_print(libbpf_warning, libbpf_info, NULL);
127 /* Parse commands line args */
128 while ((opt = getopt_long(argc, argv, "hDq",
129 long_options, &longindex)) != -1) {
132 libbpf_set_print(libbpf_warning, libbpf_info,
135 case 'q': /* Use in scripting mode */
141 return EXIT_FAIL_OPTION;
144 if (optind >= argc) {
146 printf("ERROR: Expected BPF_FILE argument after options\n");
147 return EXIT_FAIL_OPTION;
149 snprintf(filename, sizeof(filename), "%s", argv[optind]);
151 return test_open_file(filename, verbose);