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