| 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("libbpf-strings" , HAVE_LIBBPF_STRINGS_SUPPORT), |
| 47 | FEATURE_STATUS("libcapstone" , HAVE_LIBCAPSTONE_SUPPORT), |
| 48 | FEATURE_STATUS("libdw-dwarf-unwind" , HAVE_LIBDW_SUPPORT), |
| 49 | FEATURE_STATUS("libelf" , HAVE_LIBELF_SUPPORT), |
| 50 | FEATURE_STATUS("libLLVM" , HAVE_LIBLLVM_SUPPORT), |
| 51 | FEATURE_STATUS("libnuma" , HAVE_LIBNUMA_SUPPORT), |
| 52 | FEATURE_STATUS("libopencsd" , HAVE_CSTRACE_SUPPORT), |
| 53 | FEATURE_STATUS_TIP("libperl" , HAVE_LIBPERL_SUPPORT, "Deprecated, use LIBPERL=1 and install perl-ExtUtils-Embed/libperl-dev to build with it" ), |
| 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(format: "[ " ); |
| 71 | |
| 72 | if (!strcmp(s1: status, s2: "OFF" )) |
| 73 | color_fprintf(stdout, PERF_COLOR_RED, "%-3s" , status); |
| 74 | else |
| 75 | color_fprintf(stdout, PERF_COLOR_GREEN, "%-3s" , status); |
| 76 | |
| 77 | printf(format: " ]" ); |
| 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(format: "%22s: " , name); |
| 87 | on_off_print(status); |
| 88 | printf(format: " # %s" , macro); |
| 89 | |
| 90 | if (!feature->is_builtin && feature->tip) |
| 91 | printf(format: " ( tip: %s )" , feature->tip); |
| 92 | |
| 93 | putchar(c: '\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(s1: feature, s2: supported_features[i].name) == 0) || |
| 107 | (strcasecmp(s1: feature, s2: supported_features[i].macro) == 0)) { |
| 108 | if (!quiet) |
| 109 | feature_status__printf(feature: &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, options: check_feature_options, |
| 140 | usagestr: check_feature_usage, flags: 0); |
| 141 | |
| 142 | if (!argc) |
| 143 | usage_with_options(usagestr: check_feature_usage, options: 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(s: 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(s: feature_list, delim: "," ); |
| 159 | |
| 160 | while (feature_name) { |
| 161 | feature_enabled &= has_support(feature: feature_name); |
| 162 | feature_name = strtok(NULL, delim: "," ); |
| 163 | } |
| 164 | |
| 165 | free(ptr: 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, options: check_options, |
| 173 | subcommands: check_subcommands, usagestr: check_usage, flags: 0); |
| 174 | |
| 175 | if (!argc) |
| 176 | usage_with_options(usagestr: check_usage, options: check_options); |
| 177 | |
| 178 | if (strcmp(s1: argv[0], s2: "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(usagestr: check_usage, options: check_options); |
| 184 | |
| 185 | /* free usage string allocated by parse_options_subcommand */ |
| 186 | free(ptr: (void *)check_usage[0]); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |