aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/subcmd/parse-options.c
diff options
context:
space:
mode:
authorNamhyung Kim <[email protected]>2024-04-29 23:37:07 +0000
committerArnaldo Carvalho de Melo <[email protected]>2024-05-13 00:09:52 +0000
commitea558c86248b4955e5c5f3c0c921df450880605e (patch)
tree156c03a9f1da0c3b7ff5952fca1ebf0eb5d04ca2 /tools/lib/subcmd/parse-options.c
parentperf pmu: Count sys and cpuid JSON events separately (diff)
downloadkernel-ea558c86248b4955e5c5f3c0c921df450880605e.tar.gz
kernel-ea558c86248b4955e5c5f3c0c921df450880605e.zip
tools lib subcmd: Show parent options in help
I've just realized that help message in a subcommand didn't show one in the parent command. Since the option parser understands the parent, display code should do the same. For example, `perf ftrace latency -h` should show options in the `perf ftrace` command too. Before: $ perf ftrace latency -h Usage: perf ftrace [<options>] [<command>] or: perf ftrace [<options>] -- [<command>] [<options>] or: perf ftrace {trace|latency} [<options>] [<command>] or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] -b, --use-bpf Use BPF to measure function latency -n, --use-nsec Use nano-second histogram -T, --trace-funcs <func> Show latency of given function After: $ perf ftrace latency -h Usage: perf ftrace [<options>] [<command>] or: perf ftrace [<options>] -- [<command>] [<options>] or: perf ftrace {trace|latency} [<options>] [<command>] or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] -a, --all-cpus System-wide collection from all CPUs -b, --use-bpf Use BPF to measure function latency -C, --cpu <cpu> List of cpus to monitor -n, --use-nsec Use nano-second histogram -p, --pid <pid> Trace on existing process id -T, --trace-funcs <func> Show latency of given function -v, --verbose Be more verbose --tid <tid> Trace on existing thread id (exclusive to --pid) Reviewed-by: Ian Rogers <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
Diffstat (limited to 'tools/lib/subcmd/parse-options.c')
-rw-r--r--tools/lib/subcmd/parse-options.c32
1 files changed, 22 insertions, 10 deletions
diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
index d943d78b787e..4b60ec03b0bb 100644
--- a/tools/lib/subcmd/parse-options.c
+++ b/tools/lib/subcmd/parse-options.c
@@ -808,18 +808,30 @@ static int option__cmp(const void *va, const void *vb)
static struct option *options__order(const struct option *opts)
{
- int nr_opts = 0, nr_group = 0, len;
- const struct option *o = opts;
- struct option *opt, *ordered, *group;
+ int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
+ const struct option *o, *p = opts;
+ struct option *opt, *ordered = NULL, *group;
- for (o = opts; o->type != OPTION_END; o++)
- ++nr_opts;
+ /* flatten the options that have parents */
+ for (p = opts; p != NULL; p = o->parent) {
+ for (o = p; o->type != OPTION_END; o++)
+ ++nr_opts;
- len = sizeof(*o) * (nr_opts + 1);
- ordered = malloc(len);
- if (!ordered)
- goto out;
- memcpy(ordered, opts, len);
+ /*
+ * the length is given by the number of options plus a null
+ * terminator for the last loop iteration.
+ */
+ len = sizeof(*o) * (nr_opts + !o->parent);
+ group = realloc(ordered, len);
+ if (!group)
+ goto out;
+ ordered = group;
+ memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent));
+
+ nr_parent = nr_opts;
+ }
+ /* copy the last OPTION_END */
+ memcpy(&ordered[nr_opts], o, sizeof(*o));
/* sort each option group individually */
for (opt = group = ordered; opt->type != OPTION_END; opt++) {