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