Commit f664d515 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf tools: Suggest inbuilt commands for unknown command

The existing unknown command code looks for perf scripts like
perf-archive.sh and perf-iostat.sh, however, inbuilt commands aren't
suggested. Add the inbuilt commands so they may be suggested too.

Before:

  $ perf reccord
  perf: 'reccord' is not a perf-command. See 'perf --help'.
  $

After:

  $ perf reccord
  perf: 'reccord' is not a perf-command. See 'perf --help'.

  Did you mean this?
          record
  $
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Tested-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240301201306.2680986-1-irogers@google.com
[ Added some fixes from Ian to problems I noticed while testing ]
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 5f2f051a
......@@ -2,8 +2,10 @@
#ifndef BUILTIN_H
#define BUILTIN_H
struct cmdnames;
void list_common_cmds_help(void);
const char *help_unknown_cmd(const char *cmd);
const char *help_unknown_cmd(const char *cmd, struct cmdnames *main_cmds);
int cmd_annotate(int argc, const char **argv);
int cmd_bench(int argc, const char **argv);
......
......@@ -18,6 +18,7 @@
#include <subcmd/run-command.h>
#include "util/parse-events.h"
#include <subcmd/parse-options.h>
#include <subcmd/help.h>
#include "util/debug.h"
#include "util/event.h"
#include "util/util.h" // usage()
......@@ -458,7 +459,7 @@ static int libperf_print(enum libperf_print_level level,
int main(int argc, const char **argv)
{
int err;
int err, done_help = 0;
const char *cmd;
char sbuf[STRERR_BUFSIZE];
......@@ -557,22 +558,32 @@ int main(int argc, const char **argv)
pthread__block_sigwinch();
while (1) {
static int done_help;
run_argv(&argc, &argv);
if (errno != ENOENT)
break;
if (!done_help) {
cmd = argv[0] = help_unknown_cmd(cmd);
struct cmdnames main_cmds = {};
for (unsigned int i = 0; i < ARRAY_SIZE(commands); i++) {
add_cmdname(&main_cmds,
commands[i].cmd,
strlen(commands[i].cmd));
}
cmd = argv[0] = help_unknown_cmd(cmd, &main_cmds);
clean_cmdnames(&main_cmds);
done_help = 1;
if (!cmd)
break;
} else
break;
}
if (cmd) {
fprintf(stderr, "Failed to run command '%s': %s\n",
cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
}
out:
if (debug_fp)
fclose(debug_fp);
......
......@@ -52,46 +52,44 @@ static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
return 0;
}
const char *help_unknown_cmd(const char *cmd)
const char *help_unknown_cmd(const char *cmd, struct cmdnames *main_cmds)
{
unsigned int i, n = 0, best_similarity = 0;
struct cmdnames main_cmds, other_cmds;
struct cmdnames other_cmds;
memset(&main_cmds, 0, sizeof(main_cmds));
memset(&other_cmds, 0, sizeof(main_cmds));
memset(&other_cmds, 0, sizeof(other_cmds));
perf_config(perf_unknown_cmd_config, NULL);
load_command_list("perf-", &main_cmds, &other_cmds);
load_command_list("perf-", main_cmds, &other_cmds);
if (add_cmd_list(&main_cmds, &other_cmds) < 0) {
if (add_cmd_list(main_cmds, &other_cmds) < 0) {
fprintf(stderr, "ERROR: Failed to allocate command list for unknown command.\n");
goto end;
}
qsort(main_cmds.names, main_cmds.cnt,
sizeof(main_cmds.names), cmdname_compare);
uniq(&main_cmds);
qsort(main_cmds->names, main_cmds->cnt,
sizeof(main_cmds->names), cmdname_compare);
uniq(main_cmds);
if (main_cmds.cnt) {
if (main_cmds->cnt) {
/* This reuses cmdname->len for similarity index */
for (i = 0; i < main_cmds.cnt; ++i)
main_cmds.names[i]->len =
levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
for (i = 0; i < main_cmds->cnt; ++i)
main_cmds->names[i]->len =
levenshtein(cmd, main_cmds->names[i]->name, 0, 2, 1, 4);
qsort(main_cmds.names, main_cmds.cnt,
sizeof(*main_cmds.names), levenshtein_compare);
qsort(main_cmds->names, main_cmds->cnt,
sizeof(*main_cmds->names), levenshtein_compare);
best_similarity = main_cmds.names[0]->len;
best_similarity = main_cmds->names[0]->len;
n = 1;
while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
while (n < main_cmds->cnt && best_similarity == main_cmds->names[n]->len)
++n;
}
if (autocorrect && n == 1) {
const char *assumed = main_cmds.names[0]->name;
const char *assumed = main_cmds->names[0]->name;
main_cmds.names[0] = NULL;
clean_cmdnames(&main_cmds);
main_cmds->names[0] = NULL;
clean_cmdnames(&other_cmds);
fprintf(stderr, "WARNING: You called a perf program named '%s', "
"which does not exist.\n"
......@@ -107,15 +105,14 @@ const char *help_unknown_cmd(const char *cmd)
fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
if (main_cmds.cnt && best_similarity < 6) {
if (main_cmds->cnt && best_similarity < 6) {
fprintf(stderr, "\nDid you mean %s?\n",
n < 2 ? "this": "one of these");
for (i = 0; i < n; i++)
fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
fprintf(stderr, "\t%s\n", main_cmds->names[i]->name);
}
end:
clean_cmdnames(&main_cmds);
clean_cmdnames(&other_cmds);
exit(1);
return NULL;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment