tools/bpf/bpftool/main.c

Source file repositories/reference/linux-study-clean/tools/bpf/bpftool/main.c

File Facts

System
Linux kernel
Corpus path
tools/bpf/bpftool/main.c
Extension
.c
Size
12785 bytes
Lines
580
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (!strcmp(commands[i].cmd, "prog")) {
			/* Assume we run a bootstrap version if "bpftool prog"
			 * is not available.
			 */
			bootstrap = !commands[i].func;
			break;
		}
	}

	if (json_output) {
		jsonw_start_object(json_wtr);	/* root object */

		jsonw_name(json_wtr, "version");
#ifdef BPFTOOL_VERSION
		jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
#else
		jsonw_printf(json_wtr, "\"%d.%d.%d\"", BPFTOOL_MAJOR_VERSION,
			     BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
#endif
		jsonw_name(json_wtr, "libbpf_version");
		jsonw_printf(json_wtr, "\"%u.%u\"",
			     libbpf_major_version(), libbpf_minor_version());

		jsonw_name(json_wtr, "features");
		jsonw_start_object(json_wtr);	/* features */
		jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
		jsonw_bool_field(json_wtr, "llvm", has_llvm);
		jsonw_bool_field(json_wtr, "crypto", has_crypto);
		jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
		jsonw_bool_field(json_wtr, "bootstrap", bootstrap);
		jsonw_end_object(json_wtr);	/* features */

		jsonw_end_object(json_wtr);	/* root object */
	} else {
		unsigned int nb_features = 0;

#ifdef BPFTOOL_VERSION
		printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
#else
		printf("%s v%d.%d.%d\n", bin_name, BPFTOOL_MAJOR_VERSION,
		       BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
#endif
		printf("using libbpf %s\n", libbpf_version_string());
		printf("features:");
		print_feature("libbfd", has_libbfd, &nb_features);
		print_feature("llvm", has_llvm, &nb_features);
		print_feature("crypto", has_crypto, &nb_features);
		print_feature("skeletons", has_skeletons, &nb_features);
		print_feature("bootstrap", bootstrap, &nb_features);
		printf("\n");
	}
	return 0;
}

int cmd_select(const struct cmd *cmds, int argc, char **argv,
	       int (*help)(int argc, char **argv))
{
	unsigned int i;

	last_argc = argc;
	last_argv = argv;
	last_do_help = help;

	if (argc < 1 && cmds[0].func)
		return cmds[0].func(argc, argv);

	for (i = 0; cmds[i].cmd; i++) {
		if (is_prefix(*argv, cmds[i].cmd)) {
			if (!cmds[i].func) {
				p_err("command '%s' is not supported in bootstrap mode",
				      cmds[i].cmd);
				return -1;
			}
			return cmds[i].func(argc - 1, argv + 1);
		}
	}

	help(argc - 1, argv + 1);

	return -1;
}

bool is_prefix(const char *pfx, const char *str)
{
	if (!pfx)
		return false;
	if (strlen(str) < strlen(pfx))
		return false;

	return !memcmp(str, pfx, strlen(pfx));

Annotation

Implementation Notes