Skip to content

Commit ae0f4eb

Browse files
stkidacmel
authored andcommitted
perf tools: Enhance the matching of sub-commands abbreviations
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 <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Hanjun Guo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Rui Xiang <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c2eeac9 commit ae0f4eb

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

tools/perf/builtin-c2c.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "../perf.h"
4545
#include "pmu.h"
4646
#include "pmu-hybrid.h"
47+
#include "string2.h"
4748

4849
struct c2c_hists {
4950
struct hists hists;
@@ -3025,9 +3026,9 @@ int cmd_c2c(int argc, const char **argv)
30253026
if (!argc)
30263027
usage_with_options(c2c_usage, c2c_options);
30273028

3028-
if (!strncmp(argv[0], "rec", 3)) {
3029+
if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
30293030
return perf_c2c__record(argc, argv);
3030-
} else if (!strncmp(argv[0], "rep", 3)) {
3031+
} else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
30313032
return perf_c2c__report(argc, argv);
30323033
} else {
30333034
usage_with_options(c2c_usage, c2c_options);

tools/perf/builtin-kmem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ int cmd_kmem(int argc, const char **argv)
19461946
kmem_page = 1;
19471947
}
19481948

1949-
if (!strncmp(argv[0], "rec", 3)) {
1949+
if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
19501950
symbol__init(NULL);
19511951
return __cmd_record(argc, argv);
19521952
}

tools/perf/builtin-kvm.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "util/ordered-events.h"
2525
#include "util/kvm-stat.h"
2626
#include "ui/ui.h"
27+
#include "util/string2.h"
2728

2829
#include <sys/prctl.h>
2930
#ifdef HAVE_TIMERFD_SUPPORT
@@ -1500,10 +1501,10 @@ static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
15001501
goto perf_stat;
15011502
}
15021503

1503-
if (!strncmp(argv[1], "rec", 3))
1504+
if (strlen(argv[1]) > 2 && strstarts("record", argv[1]))
15041505
return kvm_events_record(&kvm, argc - 1, argv + 1);
15051506

1506-
if (!strncmp(argv[1], "rep", 3))
1507+
if (strlen(argv[1]) > 2 && strstarts("report", argv[1]))
15071508
return kvm_events_report(&kvm, argc - 1 , argv + 1);
15081509

15091510
#ifdef HAVE_TIMERFD_SUPPORT
@@ -1631,9 +1632,9 @@ int cmd_kvm(int argc, const char **argv)
16311632
}
16321633
}
16331634

1634-
if (!strncmp(argv[0], "rec", 3))
1635+
if (strlen(argv[0]) > 2 && strstarts("record", argv[0]))
16351636
return __cmd_record(file_name, argc, argv);
1636-
else if (!strncmp(argv[0], "rep", 3))
1637+
else if (strlen(argv[0]) > 2 && strstarts("report", argv[0]))
16371638
return __cmd_report(file_name, argc, argv);
16381639
else if (!strncmp(argv[0], "diff", 4))
16391640
return cmd_diff(argc, argv);

tools/perf/builtin-lock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "util/session.h"
1919
#include "util/tool.h"
2020
#include "util/data.h"
21+
#include "util/string2.h"
2122

2223
#include <sys/types.h>
2324
#include <sys/prctl.h>
@@ -1166,9 +1167,9 @@ int cmd_lock(int argc, const char **argv)
11661167
if (!argc)
11671168
usage_with_options(lock_usage, lock_options);
11681169

1169-
if (!strncmp(argv[0], "rec", 3)) {
1170+
if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
11701171
return __cmd_record(argc, argv);
1171-
} else if (!strncmp(argv[0], "report", 6)) {
1172+
} else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
11721173
trace_handler = &report_lock_ops;
11731174
if (argc) {
11741175
argc = parse_options(argc, argv,

tools/perf/builtin-mem.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "util/symbol.h"
2121
#include "util/pmu.h"
2222
#include "util/pmu-hybrid.h"
23+
#include "util/string2.h"
2324
#include <linux/err.h>
2425

2526
#define MEM_OPERATION_LOAD 0x1
@@ -496,9 +497,9 @@ int cmd_mem(int argc, const char **argv)
496497
mem.input_name = "perf.data";
497498
}
498499

499-
if (!strncmp(argv[0], "rec", 3))
500+
if (strlen(argv[0]) > 2 && strstarts("record", argv[0]))
500501
return __cmd_record(argc, argv, &mem);
501-
else if (!strncmp(argv[0], "rep", 3))
502+
else if (strlen(argv[0]) > 2 && strstarts("report", argv[0]))
502503
return report_events(argc, argv, &mem);
503504
else
504505
usage_with_options(mem_usage, mem_options);

tools/perf/builtin-sched.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,7 +3561,7 @@ int cmd_sched(int argc, const char **argv)
35613561
if (!strcmp(argv[0], "script"))
35623562
return cmd_script(argc, argv);
35633563

3564-
if (!strncmp(argv[0], "rec", 3)) {
3564+
if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
35653565
return __cmd_record(argc, argv);
35663566
} else if (!strncmp(argv[0], "lat", 3)) {
35673567
sched.tp_handler = &lat_ops;
@@ -3581,7 +3581,7 @@ int cmd_sched(int argc, const char **argv)
35813581
sched.tp_handler = &map_ops;
35823582
setup_sorting(&sched, latency_options, latency_usage);
35833583
return perf_sched__map(&sched);
3584-
} else if (!strncmp(argv[0], "rep", 3)) {
3584+
} else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
35853585
sched.tp_handler = &replay_ops;
35863586
if (argc) {
35873587
argc = parse_options(argc, argv, replay_options, replay_usage, 0);

tools/perf/builtin-script.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,13 +3922,13 @@ int cmd_script(int argc, const char **argv)
39223922
if (symbol__validate_sym_arguments())
39233923
return -1;
39243924

3925-
if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3925+
if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
39263926
rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
39273927
if (!rec_script_path)
39283928
return cmd_record(argc, argv);
39293929
}
39303930

3931-
if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3931+
if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
39323932
rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
39333933
if (!rep_script_path) {
39343934
fprintf(stderr,

tools/perf/builtin-stat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,11 +2271,11 @@ int cmd_stat(int argc, const char **argv)
22712271
} else
22722272
stat_config.csv_sep = DEFAULT_SEPARATOR;
22732273

2274-
if (argc && !strncmp(argv[0], "rec", 3)) {
2274+
if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
22752275
argc = __cmd_record(argc, argv);
22762276
if (argc < 0)
22772277
return -1;
2278-
} else if (argc && !strncmp(argv[0], "rep", 3))
2278+
} else if (argc && strlen(argv[0]) > 2 && strstarts("report", argv[0]))
22792279
return __cmd_report(argc, argv);
22802280

22812281
interval = stat_config.interval;

tools/perf/builtin-timechart.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "util/tool.h"
3636
#include "util/data.h"
3737
#include "util/debug.h"
38+
#include "util/string2.h"
3839
#include <linux/err.h>
3940

4041
#ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE
@@ -1983,7 +1984,7 @@ int cmd_timechart(int argc, const char **argv)
19831984
return -1;
19841985
}
19851986

1986-
if (argc && !strncmp(argv[0], "rec", 3)) {
1987+
if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
19871988
argc = parse_options(argc, argv, timechart_record_options,
19881989
timechart_record_usage,
19891990
PARSE_OPT_STOP_AT_NON_OPTION);

0 commit comments

Comments
 (0)