xref: /linux/tools/perf/builtin-check.c (revision ec714e371f22f716a04e6ecb2a24988c92b26911)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "builtin.h"
3 #include "color.h"
4 #include "util/bpf-utils.h"
5 #include "util/debug.h"
6 #include "util/header.h"
7 #include <tools/config.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <subcmd/parse-options.h>
12 
13 static const char * const check_subcommands[] = { "feature", NULL };
14 static struct option check_options[] = {
15 	OPT_BOOLEAN('q', "quiet", &quiet, "do not show any warnings or messages"),
16 	OPT_END()
17 };
18 static struct option check_feature_options[] = { OPT_PARENT(check_options) };
19 
20 static const char *check_usage[] = { NULL, NULL };
21 static const char *check_feature_usage[] = {
22 	"perf check feature <feature_list>",
23 	NULL
24 };
25 
26 #define FEATURE_STATUS(name_, macro_) {    \
27 	.name = name_,                     \
28 	.macro = #macro_,                  \
29 	.is_builtin = IS_BUILTIN(macro_) }
30 
31 #define FEATURE_STATUS_TIP(name_, macro_, tip_) { \
32 	.name = name_,				  \
33 	.macro = #macro_,			  \
34 	.tip = tip_,				  \
35 	.is_builtin = IS_BUILTIN(macro_) }
36 
37 struct feature_status supported_features[] = {
38 	FEATURE_STATUS("aio", HAVE_AIO_SUPPORT),
39 	FEATURE_STATUS("bpf", HAVE_LIBBPF_SUPPORT),
40 	FEATURE_STATUS("bpf_skeletons", HAVE_BPF_SKEL),
41 	FEATURE_STATUS("debuginfod", HAVE_DEBUGINFOD_SUPPORT),
42 	FEATURE_STATUS("dwarf", HAVE_LIBDW_SUPPORT),
43 	FEATURE_STATUS("dwarf_getlocations", HAVE_LIBDW_SUPPORT),
44 	FEATURE_STATUS("dwarf-unwind", HAVE_DWARF_UNWIND_SUPPORT),
45 	FEATURE_STATUS("auxtrace", HAVE_AUXTRACE_SUPPORT),
46 	FEATURE_STATUS_TIP("libbfd", HAVE_LIBBFD_SUPPORT, "Deprecated, license incompatibility, use BUILD_NONDISTRO=1 and install binutils-dev[el]"),
47 	FEATURE_STATUS("libbpf-strings", HAVE_LIBBPF_STRINGS_SUPPORT),
48 	FEATURE_STATUS("libcapstone", HAVE_LIBCAPSTONE_SUPPORT),
49 	FEATURE_STATUS("libdw-dwarf-unwind", HAVE_LIBDW_SUPPORT),
50 	FEATURE_STATUS("libelf", HAVE_LIBELF_SUPPORT),
51 	FEATURE_STATUS("libLLVM", HAVE_LIBLLVM_SUPPORT),
52 	FEATURE_STATUS("libnuma", HAVE_LIBNUMA_SUPPORT),
53 	FEATURE_STATUS("libopencsd", HAVE_CSTRACE_SUPPORT),
54 	FEATURE_STATUS_TIP("libperl", HAVE_LIBPERL_SUPPORT, "Deprecated, use LIBPERL=1 and install perl-ExtUtils-Embed/libperl-dev to build with it"),
55 	FEATURE_STATUS("libpfm4", HAVE_LIBPFM),
56 	FEATURE_STATUS("libpython", HAVE_LIBPYTHON_SUPPORT),
57 	FEATURE_STATUS("libslang", HAVE_SLANG_SUPPORT),
58 	FEATURE_STATUS("libtraceevent", HAVE_LIBTRACEEVENT),
59 	FEATURE_STATUS_TIP("libunwind", HAVE_LIBUNWIND_SUPPORT, "Deprecated, use LIBUNWIND=1 and install libunwind-dev[el] to build with it"),
60 	FEATURE_STATUS("lzma", HAVE_LZMA_SUPPORT),
61 	FEATURE_STATUS("numa_num_possible_cpus", HAVE_LIBNUMA_SUPPORT),
62 	FEATURE_STATUS("zlib", HAVE_ZLIB_SUPPORT),
63 	FEATURE_STATUS("zstd", HAVE_ZSTD_SUPPORT),
64 
65 	/* this should remain at end, to know the array end */
66 	FEATURE_STATUS(NULL, _)
67 };
68 
on_off_print(const char * status)69 static void on_off_print(const char *status)
70 {
71 	printf("[ ");
72 
73 	if (!strcmp(status, "OFF"))
74 		color_fprintf(stdout, PERF_COLOR_RED, "%-3s", status);
75 	else
76 		color_fprintf(stdout, PERF_COLOR_GREEN, "%-3s", status);
77 
78 	printf(" ]");
79 }
80 
81 /* Helper function to print status of a feature along with name/macro */
feature_status__printf(const struct feature_status * feature)82 void feature_status__printf(const struct feature_status *feature)
83 {
84 	const char *name = feature->name, *macro = feature->macro,
85 		   *status = feature->is_builtin ? "on" : "OFF";
86 
87 	printf("%22s: ", name);
88 	on_off_print(status);
89 	printf("  # %s", macro);
90 
91 	if (!feature->is_builtin && feature->tip)
92 		printf(" ( tip: %s )", feature->tip);
93 
94 	putchar('\n');
95 }
96 
97 /**
98  * check whether "feature" is built-in with perf
99  *
100  * returns:
101  *    0: NOT built-in or Feature not known
102  *    1: Built-in
103  */
has_support(const char * feature)104 static int has_support(const char *feature)
105 {
106 	for (int i = 0; supported_features[i].name; ++i) {
107 		if ((strcasecmp(feature, supported_features[i].name) == 0) ||
108 		    (strcasecmp(feature, supported_features[i].macro) == 0)) {
109 			if (!quiet)
110 				feature_status__printf(&supported_features[i]);
111 			return supported_features[i].is_builtin;
112 		}
113 	}
114 
115 	if (!quiet)
116 		pr_err("Unknown feature '%s', please use 'perf version --build-options' to see which ones are available.\n", feature);
117 
118 	return 0;
119 }
120 
121 
122 /**
123  * Usage: 'perf check feature <feature_list>'
124  *
125  * <feature_list> can be a single feature name/macro, or a comma-separated list
126  * of feature names/macros
127  * eg. argument can be "libtraceevent" or "libtraceevent,bpf" etc
128  *
129  * In case of a comma-separated list, feature_enabled will be 1, only if
130  * all features passed in the string are supported
131  *
132  * Note that argv will get modified
133  */
subcommand_feature(int argc,const char ** argv)134 static int subcommand_feature(int argc, const char **argv)
135 {
136 	char *feature_list;
137 	char *feature_name;
138 	int feature_enabled;
139 
140 	argc = parse_options(argc, argv, check_feature_options,
141 			check_feature_usage, 0);
142 
143 	if (!argc)
144 		usage_with_options(check_feature_usage, check_feature_options);
145 
146 	if (argc > 1) {
147 		pr_err("Too many arguments passed to 'perf check feature'\n");
148 		return -1;
149 	}
150 
151 	feature_enabled = 1;
152 	/* feature_list is a non-const copy of 'argv[0]' */
153 	feature_list = strdup(argv[0]);
154 	if (!feature_list) {
155 		pr_err("ERROR: failed to allocate memory for feature list\n");
156 		return -1;
157 	}
158 
159 	feature_name = strtok(feature_list, ",");
160 
161 	while (feature_name) {
162 		feature_enabled &= has_support(feature_name);
163 		feature_name = strtok(NULL, ",");
164 	}
165 
166 	free(feature_list);
167 
168 	return !feature_enabled;
169 }
170 
cmd_check(int argc,const char ** argv)171 int cmd_check(int argc, const char **argv)
172 {
173 	argc = parse_options_subcommand(argc, argv, check_options,
174 			check_subcommands, check_usage, 0);
175 
176 	if (!argc)
177 		usage_with_options(check_usage, check_options);
178 
179 	if (strcmp(argv[0], "feature") == 0)
180 		return subcommand_feature(argc, argv);
181 
182 	/* If no subcommand matched above, print usage help */
183 	pr_err("Unknown subcommand: %s\n", argv[0]);
184 	usage_with_options(check_usage, check_options);
185 
186 	/* free usage string allocated by parse_options_subcommand */
187 	free((void *)check_usage[0]);
188 
189 	return 0;
190 }
191