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