perf tools: Enhance the matching of sub-commands abbreviations
authorWei Li <liwei391@huawei.com>
Fri, 25 Mar 2022 09:20:32 +0000 (17:20 +0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 26 Mar 2022 13:55:57 +0000 (10:55 -0300)
We support short command 'rec*' for 'record' and 'rep*' for 'report' in
lots of sub-commands, but the matching is not quite strict currnetly.

It may be puzzling sometime, like we mis-type a 'recport' to report but
it will perform 'record' in fact without any message.

To fix this, add a check to ensure that the short cmd is valid prefix
of the real command.

Committer testing:

  [root@quaco ~]# perf c2c re sleep 1

   Usage: perf c2c {record|report}

      -v, --verbose         be more verbose (show counter open errors, etc)

  # perf c2c rec sleep 1
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.038 MB perf.data (16 samples) ]
  # perf c2c recport sleep 1

   Usage: perf c2c {record|report}

      -v, --verbose         be more verbose (show counter open errors, etc)

  # perf c2c record sleep 1
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.038 MB perf.data (15 samples) ]
  # perf c2c records sleep 1

   Usage: perf c2c {record|report}

      -v, --verbose         be more verbose (show counter open errors, etc)

  #

Signed-off-by: Wei Li <liwei391@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Hanjun Guo <guohanjun@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Rui Xiang <rui.xiang@huawei.com>
Link: http://lore.kernel.org/lkml/20220325092032.2956161-1-liwei391@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-c2c.c
tools/perf/builtin-kmem.c
tools/perf/builtin-kvm.c
tools/perf/builtin-lock.c
tools/perf/builtin-mem.c
tools/perf/builtin-sched.c
tools/perf/builtin-script.c
tools/perf/builtin-stat.c
tools/perf/builtin-timechart.c

index 14f787c..fbbed43 100644 (file)
@@ -44,6 +44,7 @@
 #include "../perf.h"
 #include "pmu.h"
 #include "pmu-hybrid.h"
+#include "string2.h"
 
 struct c2c_hists {
        struct hists            hists;
@@ -3025,9 +3026,9 @@ int cmd_c2c(int argc, const char **argv)
        if (!argc)
                usage_with_options(c2c_usage, c2c_options);
 
-       if (!strncmp(argv[0], "rec", 3)) {
+       if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                return perf_c2c__record(argc, argv);
-       } else if (!strncmp(argv[0], "rep", 3)) {
+       } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
                return perf_c2c__report(argc, argv);
        } else {
                usage_with_options(c2c_usage, c2c_options);
index 99d7ff9..ebfab2c 100644 (file)
@@ -1946,7 +1946,7 @@ int cmd_kmem(int argc, const char **argv)
                        kmem_page = 1;
        }
 
-       if (!strncmp(argv[0], "rec", 3)) {
+       if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                symbol__init(NULL);
                return __cmd_record(argc, argv);
        }
index b23a1f3..2fa687f 100644 (file)
@@ -24,6 +24,7 @@
 #include "util/ordered-events.h"
 #include "util/kvm-stat.h"
 #include "ui/ui.h"
+#include "util/string2.h"
 
 #include <sys/prctl.h>
 #ifdef HAVE_TIMERFD_SUPPORT
@@ -1500,10 +1501,10 @@ static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
                goto perf_stat;
        }
 
-       if (!strncmp(argv[1], "rec", 3))
+       if (strlen(argv[1]) > 2 && strstarts("record", argv[1]))
                return kvm_events_record(&kvm, argc - 1, argv + 1);
 
-       if (!strncmp(argv[1], "rep", 3))
+       if (strlen(argv[1]) > 2 && strstarts("report", argv[1]))
                return kvm_events_report(&kvm, argc - 1 , argv + 1);
 
 #ifdef HAVE_TIMERFD_SUPPORT
@@ -1631,9 +1632,9 @@ int cmd_kvm(int argc, const char **argv)
                }
        }
 
-       if (!strncmp(argv[0], "rec", 3))
+       if (strlen(argv[0]) > 2 && strstarts("record", argv[0]))
                return __cmd_record(file_name, argc, argv);
-       else if (!strncmp(argv[0], "rep", 3))
+       else if (strlen(argv[0]) > 2 && strstarts("report", argv[0]))
                return __cmd_report(file_name, argc, argv);
        else if (!strncmp(argv[0], "diff", 4))
                return cmd_diff(argc, argv);
index a708976..cdfe1d4 100644 (file)
@@ -18,6 +18,7 @@
 #include "util/session.h"
 #include "util/tool.h"
 #include "util/data.h"
+#include "util/string2.h"
 
 #include <sys/types.h>
 #include <sys/prctl.h>
@@ -1166,9 +1167,9 @@ int cmd_lock(int argc, const char **argv)
        if (!argc)
                usage_with_options(lock_usage, lock_options);
 
-       if (!strncmp(argv[0], "rec", 3)) {
+       if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                return __cmd_record(argc, argv);
-       } else if (!strncmp(argv[0], "report", 6)) {
+       } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
                trace_handler = &report_lock_ops;
                if (argc) {
                        argc = parse_options(argc, argv,
index fcf65a5..9e435fd 100644 (file)
@@ -20,6 +20,7 @@
 #include "util/symbol.h"
 #include "util/pmu.h"
 #include "util/pmu-hybrid.h"
+#include "util/string2.h"
 #include <linux/err.h>
 
 #define MEM_OPERATION_LOAD     0x1
@@ -496,9 +497,9 @@ int cmd_mem(int argc, const char **argv)
                        mem.input_name = "perf.data";
        }
 
-       if (!strncmp(argv[0], "rec", 3))
+       if (strlen(argv[0]) > 2 && strstarts("record", argv[0]))
                return __cmd_record(argc, argv, &mem);
-       else if (!strncmp(argv[0], "rep", 3))
+       else if (strlen(argv[0]) > 2 && strstarts("report", argv[0]))
                return report_events(argc, argv, &mem);
        else
                usage_with_options(mem_usage, mem_options);
index 72d446d..646bd93 100644 (file)
@@ -3561,7 +3561,7 @@ int cmd_sched(int argc, const char **argv)
        if (!strcmp(argv[0], "script"))
                return cmd_script(argc, argv);
 
-       if (!strncmp(argv[0], "rec", 3)) {
+       if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                return __cmd_record(argc, argv);
        } else if (!strncmp(argv[0], "lat", 3)) {
                sched.tp_handler = &lat_ops;
@@ -3581,7 +3581,7 @@ int cmd_sched(int argc, const char **argv)
                sched.tp_handler = &map_ops;
                setup_sorting(&sched, latency_options, latency_usage);
                return perf_sched__map(&sched);
-       } else if (!strncmp(argv[0], "rep", 3)) {
+       } else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
                sched.tp_handler = &replay_ops;
                if (argc) {
                        argc = parse_options(argc, argv, replay_options, replay_usage, 0);
index f12dc5e..a2f1179 100644 (file)
@@ -3922,13 +3922,13 @@ int cmd_script(int argc, const char **argv)
        if (symbol__validate_sym_arguments())
                return -1;
 
-       if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
+       if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
                if (!rec_script_path)
                        return cmd_record(argc, argv);
        }
 
-       if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
+       if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
                rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
                if (!rep_script_path) {
                        fprintf(stderr,
index 60baa3d..4ee40de 100644 (file)
@@ -2271,11 +2271,11 @@ int cmd_stat(int argc, const char **argv)
        } else
                stat_config.csv_sep = DEFAULT_SEPARATOR;
 
-       if (argc && !strncmp(argv[0], "rec", 3)) {
+       if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                argc = __cmd_record(argc, argv);
                if (argc < 0)
                        return -1;
-       } else if (argc && !strncmp(argv[0], "rep", 3))
+       } else if (argc && strlen(argv[0]) > 2 && strstarts("report", argv[0]))
                return __cmd_report(argc, argv);
 
        interval = stat_config.interval;
index 43bf4d6..afce731 100644 (file)
@@ -35,6 +35,7 @@
 #include "util/tool.h"
 #include "util/data.h"
 #include "util/debug.h"
+#include "util/string2.h"
 #include <linux/err.h>
 
 #ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE
@@ -1983,7 +1984,7 @@ int cmd_timechart(int argc, const char **argv)
                return -1;
        }
 
-       if (argc && !strncmp(argv[0], "rec", 3)) {
+       if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
                argc = parse_options(argc, argv, timechart_record_options,
                                     timechart_record_usage,
                                     PARSE_OPT_STOP_AT_NON_OPTION);