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