[PATCH v5 2/5] perf: util: support string type option for perf_parse_sublevel_options

From: Changbin Du
Date: Tue Sep 26 2023 - 00:30:26 EST


Add string type option to get non-int value.

Signed-off-by: Changbin Du <changbin.du@xxxxxxxxxx>
---
tools/perf/util/parse-sublevel-options.c | 12 +++++++++---
tools/perf/util/parse-sublevel-options.h | 7 +++++++
2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/parse-sublevel-options.c b/tools/perf/util/parse-sublevel-options.c
index a841d17ffd57..d08a1ccc9616 100644
--- a/tools/perf/util/parse-sublevel-options.c
+++ b/tools/perf/util/parse-sublevel-options.c
@@ -34,10 +34,16 @@ static int parse_one_sublevel_option(const char *str,
return -1;
}

- if (vstr)
- v = atoi(vstr);
+ if (vstr) {
+ /* The value of option either is a integer or string. */
+ if (opt->value_ptr) {
+ v = atoi(vstr);
+ *opt->value_ptr = v;
+ } else {
+ *opt->str_ptr = strdup(vstr);
+ }
+ }

- *opt->value_ptr = v;
free(s);
return 0;
}
diff --git a/tools/perf/util/parse-sublevel-options.h b/tools/perf/util/parse-sublevel-options.h
index 578b18ef03bb..d536ebe43b58 100644
--- a/tools/perf/util/parse-sublevel-options.h
+++ b/tools/perf/util/parse-sublevel-options.h
@@ -3,7 +3,14 @@

struct sublevel_option {
const char *name;
+
+ /*
+ * Only one of below can be non-null. So we simply support
+ * two types: integer and string. For string, the caller is
+ * responsible for freeing allocated memory after use.
+ */
int *value_ptr;
+ char **str_ptr;
};

int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts);
--
2.25.1