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_TIP("libbfd", HAVE_LIBBFD_SUPPORT, "Deprecated, license incompatibility, use BUILD_NONDISTRO=1 and install binutils-dev[el]"),
46 FEATURE_STATUS("libbabeltrace", HAVE_LIBBABELTRACE_SUPPORT),
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 FEATURE_STATUS("rust", HAVE_RUST_SUPPORT),
65
66 /* this should remain at end, to know the array end */
67 FEATURE_STATUS(NULL, _)
68 };
69
on_off_print(const char * status)70 static void on_off_print(const char *status)
71 {
72 printf("[ ");
73
74 if (!strcmp(status, "OFF"))
75 color_fprintf(stdout, PERF_COLOR_RED, "%-3s", status);
76 else
77 color_fprintf(stdout, PERF_COLOR_GREEN, "%-3s", status);
78
79 printf(" ]");
80 }
81
82 /* Helper function to print status of a feature along with name/macro */
feature_status__printf(const struct feature_status * feature)83 void feature_status__printf(const struct feature_status *feature)
84 {
85 const char *name = feature->name, *macro = feature->macro,
86 *status = feature->is_builtin ? "on" : "OFF";
87
88 printf("%22s: ", name);
89 on_off_print(status);
90 printf(" # %s", macro);
91
92 if (!feature->is_builtin && feature->tip)
93 printf(" ( tip: %s )", feature->tip);
94
95 putchar('\n');
96 }
97
98 /**
99 * check whether "feature" is built-in with perf
100 *
101 * returns:
102 * 0: NOT built-in or Feature not known
103 * 1: Built-in
104 */
has_support(const char * feature)105 static int has_support(const char *feature)
106 {
107 for (int i = 0; supported_features[i].name; ++i) {
108 if ((strcasecmp(feature, supported_features[i].name) == 0) ||
109 (strcasecmp(feature, supported_features[i].macro) == 0)) {
110 if (!quiet)
111 feature_status__printf(&supported_features[i]);
112 return supported_features[i].is_builtin;
113 }
114 }
115
116 if (!quiet)
117 pr_err("Unknown feature '%s', please use 'perf version --build-options' to see which ones are available.\n", feature);
118
119 return 0;
120 }
121
122
123 /**
124 * Usage: 'perf check feature <feature_list>'
125 *
126 * <feature_list> can be a single feature name/macro, or a comma-separated list
127 * of feature names/macros
128 * eg. argument can be "libtraceevent" or "libtraceevent,bpf" etc
129 *
130 * In case of a comma-separated list, feature_enabled will be 1, only if
131 * all features passed in the string are supported
132 *
133 * Note that argv will get modified
134 */
subcommand_feature(int argc,const char ** argv)135 static int subcommand_feature(int argc, const char **argv)
136 {
137 char *feature_list;
138 char *feature_name;
139 int feature_enabled;
140
141 argc = parse_options(argc, argv, check_feature_options,
142 check_feature_usage, 0);
143
144 if (!argc)
145 usage_with_options(check_feature_usage, check_feature_options);
146
147 if (argc > 1) {
148 pr_err("Too many arguments passed to 'perf check feature'\n");
149 return -1;
150 }
151
152 feature_enabled = 1;
153 /* feature_list is a non-const copy of 'argv[0]' */
154 feature_list = strdup(argv[0]);
155 if (!feature_list) {
156 pr_err("ERROR: failed to allocate memory for feature list\n");
157 return -1;
158 }
159
160 feature_name = strtok(feature_list, ",");
161
162 while (feature_name) {
163 feature_enabled &= has_support(feature_name);
164 feature_name = strtok(NULL, ",");
165 }
166
167 free(feature_list);
168
169 return !feature_enabled;
170 }
171
cmd_check(int argc,const char ** argv)172 int cmd_check(int argc, const char **argv)
173 {
174 argc = parse_options_subcommand(argc, argv, check_options,
175 check_subcommands, check_usage, 0);
176
177 if (!argc)
178 usage_with_options(check_usage, check_options);
179
180 if (strcmp(argv[0], "feature") == 0)
181 return subcommand_feature(argc, argv);
182
183 /* If no subcommand matched above, print usage help */
184 pr_err("Unknown subcommand: %s\n", argv[0]);
185 usage_with_options(check_usage, check_options);
186
187 /* free usage string allocated by parse_options_subcommand */
188 free((void *)check_usage[0]);
189
190 return 0;
191 }
192