Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <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>

authored by

Wei Li and committed by
Arnaldo Carvalho de Melo
ae0f4eb3 c2eeac98

+23 -18
+3 -2
tools/perf/builtin-c2c.c
··· 44 44 #include "../perf.h" 45 45 #include "pmu.h" 46 46 #include "pmu-hybrid.h" 47 + #include "string2.h" 47 48 48 49 struct c2c_hists { 49 50 struct hists hists; ··· 3026 3025 if (!argc) 3027 3026 usage_with_options(c2c_usage, c2c_options); 3028 3027 3029 - if (!strncmp(argv[0], "rec", 3)) { 3028 + if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 3030 3029 return perf_c2c__record(argc, argv); 3031 - } else if (!strncmp(argv[0], "rep", 3)) { 3030 + } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) { 3032 3031 return perf_c2c__report(argc, argv); 3033 3032 } else { 3034 3033 usage_with_options(c2c_usage, c2c_options);
+1 -1
tools/perf/builtin-kmem.c
··· 1946 1946 kmem_page = 1; 1947 1947 } 1948 1948 1949 - if (!strncmp(argv[0], "rec", 3)) { 1949 + if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 1950 1950 symbol__init(NULL); 1951 1951 return __cmd_record(argc, argv); 1952 1952 }
+5 -4
tools/perf/builtin-kvm.c
··· 24 24 #include "util/ordered-events.h" 25 25 #include "util/kvm-stat.h" 26 26 #include "ui/ui.h" 27 + #include "util/string2.h" 27 28 28 29 #include <sys/prctl.h> 29 30 #ifdef HAVE_TIMERFD_SUPPORT ··· 1501 1500 goto perf_stat; 1502 1501 } 1503 1502 1504 - if (!strncmp(argv[1], "rec", 3)) 1503 + if (strlen(argv[1]) > 2 && strstarts("record", argv[1])) 1505 1504 return kvm_events_record(&kvm, argc - 1, argv + 1); 1506 1505 1507 - if (!strncmp(argv[1], "rep", 3)) 1506 + if (strlen(argv[1]) > 2 && strstarts("report", argv[1])) 1508 1507 return kvm_events_report(&kvm, argc - 1 , argv + 1); 1509 1508 1510 1509 #ifdef HAVE_TIMERFD_SUPPORT ··· 1632 1631 } 1633 1632 } 1634 1633 1635 - if (!strncmp(argv[0], "rec", 3)) 1634 + if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) 1636 1635 return __cmd_record(file_name, argc, argv); 1637 - else if (!strncmp(argv[0], "rep", 3)) 1636 + else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) 1638 1637 return __cmd_report(file_name, argc, argv); 1639 1638 else if (!strncmp(argv[0], "diff", 4)) 1640 1639 return cmd_diff(argc, argv);
+3 -2
tools/perf/builtin-lock.c
··· 18 18 #include "util/session.h" 19 19 #include "util/tool.h" 20 20 #include "util/data.h" 21 + #include "util/string2.h" 21 22 22 23 #include <sys/types.h> 23 24 #include <sys/prctl.h> ··· 1167 1166 if (!argc) 1168 1167 usage_with_options(lock_usage, lock_options); 1169 1168 1170 - if (!strncmp(argv[0], "rec", 3)) { 1169 + if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 1171 1170 return __cmd_record(argc, argv); 1172 - } else if (!strncmp(argv[0], "report", 6)) { 1171 + } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) { 1173 1172 trace_handler = &report_lock_ops; 1174 1173 if (argc) { 1175 1174 argc = parse_options(argc, argv,
+3 -2
tools/perf/builtin-mem.c
··· 20 20 #include "util/symbol.h" 21 21 #include "util/pmu.h" 22 22 #include "util/pmu-hybrid.h" 23 + #include "util/string2.h" 23 24 #include <linux/err.h> 24 25 25 26 #define MEM_OPERATION_LOAD 0x1 ··· 497 496 mem.input_name = "perf.data"; 498 497 } 499 498 500 - if (!strncmp(argv[0], "rec", 3)) 499 + if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) 501 500 return __cmd_record(argc, argv, &mem); 502 - else if (!strncmp(argv[0], "rep", 3)) 501 + else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) 503 502 return report_events(argc, argv, &mem); 504 503 else 505 504 usage_with_options(mem_usage, mem_options);
+2 -2
tools/perf/builtin-sched.c
··· 3561 3561 if (!strcmp(argv[0], "script")) 3562 3562 return cmd_script(argc, argv); 3563 3563 3564 - if (!strncmp(argv[0], "rec", 3)) { 3564 + if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 3565 3565 return __cmd_record(argc, argv); 3566 3566 } else if (!strncmp(argv[0], "lat", 3)) { 3567 3567 sched.tp_handler = &lat_ops; ··· 3581 3581 sched.tp_handler = &map_ops; 3582 3582 setup_sorting(&sched, latency_options, latency_usage); 3583 3583 return perf_sched__map(&sched); 3584 - } else if (!strncmp(argv[0], "rep", 3)) { 3584 + } else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) { 3585 3585 sched.tp_handler = &replay_ops; 3586 3586 if (argc) { 3587 3587 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
+2 -2
tools/perf/builtin-script.c
··· 3922 3922 if (symbol__validate_sym_arguments()) 3923 3923 return -1; 3924 3924 3925 - if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { 3925 + if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 3926 3926 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); 3927 3927 if (!rec_script_path) 3928 3928 return cmd_record(argc, argv); 3929 3929 } 3930 3930 3931 - if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { 3931 + if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) { 3932 3932 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); 3933 3933 if (!rep_script_path) { 3934 3934 fprintf(stderr,
+2 -2
tools/perf/builtin-stat.c
··· 2271 2271 } else 2272 2272 stat_config.csv_sep = DEFAULT_SEPARATOR; 2273 2273 2274 - if (argc && !strncmp(argv[0], "rec", 3)) { 2274 + if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 2275 2275 argc = __cmd_record(argc, argv); 2276 2276 if (argc < 0) 2277 2277 return -1; 2278 - } else if (argc && !strncmp(argv[0], "rep", 3)) 2278 + } else if (argc && strlen(argv[0]) > 2 && strstarts("report", argv[0])) 2279 2279 return __cmd_report(argc, argv); 2280 2280 2281 2281 interval = stat_config.interval;
+2 -1
tools/perf/builtin-timechart.c
··· 35 35 #include "util/tool.h" 36 36 #include "util/data.h" 37 37 #include "util/debug.h" 38 + #include "util/string2.h" 38 39 #include <linux/err.h> 39 40 40 41 #ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE ··· 1984 1983 return -1; 1985 1984 } 1986 1985 1987 - if (argc && !strncmp(argv[0], "rec", 3)) { 1986 + if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 1988 1987 argc = parse_options(argc, argv, timechart_record_options, 1989 1988 timechart_record_usage, 1990 1989 PARSE_OPT_STOP_AT_NON_OPTION);