Skip to contentSkip to conversation

perf tools: Fix mis-matching of perf sub-commands

PATCH RESEND Superseded Wei Li 2 participants 5 messages Mar 18, 2022 – Mar 25, 2022 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Li

author
#0 lore raw

cover perf tools: Fix mis-matching of perf sub-commands

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. So fix the mis-matching.

Wei Li (2):
perf string: Add strcmp_prefix()
perf tools: Enhance the matching of sub-commands

 tools/perf/builtin-c2c.c       |  5 +++--
 tools/perf/builtin-kmem.c      |  2 +-
 tools/perf/builtin-kvm.c       |  9 +++++----
 tools/perf/builtin-lock.c      |  5 +++--
 tools/perf/builtin-mem.c       |  5 +++--
 tools/perf/builtin-sched.c     |  4 ++--
 tools/perf/builtin-script.c    |  6 ++++--
 tools/perf/builtin-stat.c      |  4 ++--
 tools/perf/builtin-timechart.c |  3 ++-
 tools/perf/util/string.c       | 17 +++++++++++++++++
 tools/perf/util/string2.h      |  1 +
 11 files changed, 43 insertions(+), 18 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH RESEND 1/2 perf string: Add strcmp_prefix()

Add a helper to check whether a string has the given prefix. This function is stolen from fs/xattr.c

---
 tools/perf/util/string.c  | 17 +++++++++++++++++
 tools/perf/util/string2.h |  1 +
 2 files changed, 18 insertions(+)
tools/perf/util/string.c +17 -0
index f6d90cdd9225..0ed3e2d0b70f 100644
@@ -209,6 +209,23 @@ int strtailcmp(const char *s1, const char *s2)
 	return 0;
 }
 
+/**
+ * strcmp_prefix - check string for given prefix
+ * @str: the target string to check
+ * @prefix: the given prefix to match
+ *
+ * Return the rest string in @str if @str has the given @prefix, return NULL
+ * otherwise.
+ */
+const char *strcmp_prefix(const char *str, const char *prefix)
+{
+	while (*prefix && *str == *prefix) {
+		str++;
+		prefix++;
+	}
+	return *prefix ? NULL : str;
+}
+
 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
 {
 	/*
tools/perf/util/string2.h +1 -0
index 56c30fef9682..58929ad928f7 100644
@@ -20,6 +20,7 @@ static inline bool strisglob(const char *str)
 	return strpbrk(str, "*?[") != NULL;
 }
 int strtailcmp(const char *s1, const char *s2);
+const char *strcmp_prefix(const char *str, const char *prefix);
 
 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH RESEND 1/2] perf string: Add strcmp_prefix()

Em Fri, Mar 18, 2022 at 05:22:44PM +0800, Wei Li escreveu:

Quoted 2 lines

Add a helper to check whether a string has the given prefix. This function is stolen from fs/xattr.c

Can't you use strstarts()? See tools/include/linux/string.h.

- Arnaldo

Quoted 48 lines
---
 tools/perf/util/string.c  | 17 +++++++++++++++++
 tools/perf/util/string2.h |  1 +
 2 files changed, 18 insertions(+)
tools/perf/util/string.c +17 -0
index f6d90cdd9225..0ed3e2d0b70f 100644
@@ -209,6 +209,23 @@ int strtailcmp(const char *s1, const char *s2)
 	return 0;
 }
 
+/**
+ * strcmp_prefix - check string for given prefix
+ * @str: the target string to check
+ * @prefix: the given prefix to match
+ *
+ * Return the rest string in @str if @str has the given @prefix, return NULL
+ * otherwise.
+ */
+const char *strcmp_prefix(const char *str, const char *prefix)
+{
+	while (*prefix && *str == *prefix) {
+		str++;
+		prefix++;
+	}
+	return *prefix ? NULL : str;
+}
+
 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
 {
 	/*
tools/perf/util/string2.h +1 -0
index 56c30fef9682..58929ad928f7 100644
@@ -20,6 +20,7 @@ static inline bool strisglob(const char *str)
 	return strpbrk(str, "*?[") != NULL;
 }
 int strtailcmp(const char *s1, const char *s2);
+const char *strcmp_prefix(const char *str, const char *prefix);
 
 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
 
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
author
#3 lore raw

Re: [PATCH RESEND 1/2] perf string: Add strcmp_prefix()

Quoted 6 lines · Arnaldo Carvalho de Melo

Em Fri, Mar 18, 2022 at 05:22:44PM +0800, Wei Li escreveu:

Quoted 2 lines

Add a helper to check whether a string has the given prefix. This function is stolen from fs/xattr.c

Can't you use strstarts()? See tools/include/linux/string.h.

Sorry, i haven't known that before, will use strstarts() in v2.

Thanks,
Wei

Quoted 50 lines
Quoted 49 lines
---
 tools/perf/util/string.c  | 17 +++++++++++++++++
 tools/perf/util/string2.h |  1 +
 2 files changed, 18 insertions(+)
tools/perf/util/string.c +17 -0
index f6d90cdd9225..0ed3e2d0b70f 100644
@@ -209,6 +209,23 @@ int strtailcmp(const char *s1, const char *s2)
 	return 0;
 }
 
+/**
+ * strcmp_prefix - check string for given prefix
+ * @str: the target string to check
+ * @prefix: the given prefix to match
+ *
+ * Return the rest string in @str if @str has the given @prefix, return NULL
+ * otherwise.
+ */
+const char *strcmp_prefix(const char *str, const char *prefix)
+{
+	while (*prefix && *str == *prefix) {
+		str++;
+		prefix++;
+	}
+	return *prefix ? NULL : str;
+}
+
 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
 {
 	/*
tools/perf/util/string2.h +1 -0
index 56c30fef9682..58929ad928f7 100644
@@ -20,6 +20,7 @@ static inline bool strisglob(const char *str)
 	return strpbrk(str, "*?[") != NULL;
 }
 int strtailcmp(const char *s1, const char *s2);
+const char *strcmp_prefix(const char *str, const char *prefix);
 
 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
 
Signature · 1 line
2.25.1
author
#4 lore raw

PATCH RESEND 2/2 perf tools: Enhance the matching of sub-commands

In reply to Wei Li

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.

---
 tools/perf/builtin-c2c.c       | 5 +++--
 tools/perf/builtin-kmem.c      | 2 +-
 tools/perf/builtin-kvm.c       | 9 +++++----
 tools/perf/builtin-lock.c      | 5 +++--
 tools/perf/builtin-mem.c       | 5 +++--
 tools/perf/builtin-sched.c     | 4 ++--
 tools/perf/builtin-script.c    | 6 ++++--
 tools/perf/builtin-stat.c      | 4 ++--
 tools/perf/builtin-timechart.c | 3 ++-
 9 files changed, 25 insertions(+), 18 deletions(-)
tools/perf/builtin-c2c.c +3 -2
index 77dd4afacca4..8c5f106af46f 100644
@@ -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 (!strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0])) {
 		return perf_c2c__record(argc, argv);
-	} else if (!strncmp(argv[0], "rep", 3)) {
+	} else if (!strncmp(argv[0], "rep", 3) && strcmp_prefix("report", argv[0])) {
 		return perf_c2c__report(argc, argv);
 	} else {
 		usage_with_options(c2c_usage, c2c_options);
tools/perf/builtin-kmem.c +1 -1
index 99d7ff9a8eff..3358c73db43b 100644
@@ -1946,7 +1946,7 @@ int cmd_kmem(int argc, const char **argv)
 			kmem_page = 1;
 	}
 
-	if (!strncmp(argv[0], "rec", 3)) {
+	if (!strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0])) {
 		symbol__init(NULL);
 		return __cmd_record(argc, argv);
 	}
tools/perf/builtin-kvm.c +5 -4
index c6f352ee57e6..afdfc5931df6 100644
@@ -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 (!strncmp(argv[1], "rec", 3) && strcmp_prefix("record", argv[1]))
 		return kvm_events_record(&kvm, argc - 1, argv + 1);
 
-	if (!strncmp(argv[1], "rep", 3))
+	if (!strncmp(argv[1], "rep", 3) && strcmp_prefix("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 (!strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0]))
 		return __cmd_record(file_name, argc, argv);
-	else if (!strncmp(argv[0], "rep", 3))
+	else if (!strncmp(argv[0], "rep", 3) && strcmp_prefix("report", argv[0]))
 		return __cmd_report(file_name, argc, argv);
 	else if (!strncmp(argv[0], "diff", 4))
 		return cmd_diff(argc, argv);
tools/perf/builtin-lock.c +3 -2
index d70131b7b1b1..7bf7662c6089 100644
@@ -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>
@@ -997,9 +998,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 (!strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0])) {
 		return __cmd_record(argc, argv);
-	} else if (!strncmp(argv[0], "report", 6)) {
+	} else if (!strncmp(argv[0], "rep", 3) && strcmp_prefix("report", argv[0])) {
 		trace_handler = &report_lock_ops;
 		if (argc) {
 			argc = parse_options(argc, argv,
tools/perf/builtin-mem.c +3 -2
index fcf65a59bea2..ed0083d43e27 100644
@@ -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 (!strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0]))
 		return __cmd_record(argc, argv, &mem);
-	else if (!strncmp(argv[0], "rep", 3))
+	else if (!strncmp(argv[0], "rep", 3) && strcmp_prefix("report", argv[0]))
 		return report_events(argc, argv, &mem);
 	else
 		usage_with_options(mem_usage, mem_options);
tools/perf/builtin-sched.c +2 -2
index 72d446de9c60..c4615ae07b5d 100644
@@ -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 (!strncmp(argv[0], "rec", 3) && strcmp_prefix("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 (!strncmp(argv[0], "rep", 3) && strcmp_prefix("replay", argv[0])) {
 		sched.tp_handler = &replay_ops;
 		if (argc) {
 			argc = parse_options(argc, argv, replay_options, replay_usage, 0);
tools/perf/builtin-script.c +4 -2
index 23bc0fb5a381..b800b4f5964b 100644
@@ -3842,13 +3842,15 @@ 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 && !strncmp(argv[0], "rec", strlen("rec")) &&
+		strcmp_prefix("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 && !strncmp(argv[0], "rep", strlen("rep")) &&
+		strcmp_prefix("report", argv[0])) {
 		rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
 		if (!rep_script_path) {
 			fprintf(stderr,
tools/perf/builtin-stat.c +2 -2
index 3f98689dd687..f0e19c93f3f8 100644
@@ -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 && !strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0])) {
 		argc = __cmd_record(argc, argv);
 		if (argc < 0)
 			return -1;
-	} else if (argc && !strncmp(argv[0], "rep", 3))
+	} else if (argc && !strncmp(argv[0], "rep", 3) && strcmp_prefix("report", argv[0]))
 		return __cmd_report(argc, argv);
 
 	interval = stat_config.interval;
tools/perf/builtin-timechart.c +2 -1
index 43bf4d67edb0..1b180cfcda74 100644
@@ -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 && !strncmp(argv[0], "rec", 3) && strcmp_prefix("record", argv[0])) {
 		argc = parse_options(argc, argv, timechart_record_options,
 				     timechart_record_usage,
 				     PARSE_OPT_STOP_AT_NON_OPTION);
Signature · 1 line
2.25.1