Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1686687lqa; Mon, 29 Apr 2024 16:37:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVxziTEj55GtbcMUv/Mlwchvwlr5W51ZO5JiNn6PzgRc2O9MPAAlccCiSlcSNTLz9U5B740Dw8dAFA1DgsfHPl7lWdCV2UaoHdhjEoe5g== X-Google-Smtp-Source: AGHT+IEDMJPKfbfMyl71TpnkYIu2fnUA1NVH1rbqgrYx0RUXudLb2/ei2u8p+q1uOPWRqriiYd9b X-Received: by 2002:a05:6512:2f9:b0:518:bd04:4faf with SMTP id m25-20020a05651202f900b00518bd044fafmr5215357lfq.44.1714433837417; Mon, 29 Apr 2024 16:37:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714433837; cv=pass; d=google.com; s=arc-20160816; b=OkVZ6cdNqeq62R1qqXoBIfRtCSAN4+T5JUtp9GguYQmRirnKVP4yFD5FbFNEjvu8PZ eupOwR/qzPpnUiCawrWGL+qhZhnOXmaCC+khMSB4h3mphnYedF8O049N6OTIYHvKFKPP UEACanKt4lNxSD7viSC02lJxA9SPI09fRx7JnSTbCvc7HhM2oyN0moNNnyl0QWkyMhAq 2cH5/hMbYWPG6UJO+ciF5VMu/plQh4u6yTeNogBwbKac6MDGhIxLkcu8RZdKHCsSrH70 xr/YstS9xIWqLW1CuzvIv3Q+KCT2kXVTai5qHLLNIgr86/M8HK59zdiN0LeuUeqALE1K nRbg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=X5T8plwWCeFOfIFtPGjiLAC8fBCoIIy8kMJJUxrIhik=; fh=fHhbvXgCev/wpffLVpfaAi8pPyv7hrTyhVPgsZ7HNrs=; b=kwss9EHSL4HO/SIP1AyZLbDQWAGIbYnvuJK8qO7h3+IBC/Hta5BoH+huFOJJUCPjkz PlZmQhsZIRDiDhzsTKHcuLYG05mlHhTAN5mlNrRT6DY6/+8RNPgTTrqgfzRT/GlKzKH8 F+Yfew1gNlPmoIjIzHegjDZdQngJaiInMCp686GKEVQHVo2rOFrgYelbY42HtN43AcFY f3PtmhPPt3YxZgxswm45RTEDHhKNH8DZ5LaV7qJtY4bZL+K0KknVvVKTe9jxX/d/daiy Q3RvarYrSQlmvSETS/qGfYRflBr01GbTY99Y3u0H5N3WAfVEYJu5KPNC/Y0UA219+BDq aQ1Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BSEkZM6h; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163154-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163154-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i10-20020a05640200ca00b0056e0e0ce7fasi14361522edu.342.2024.04.29.16.37.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 16:37:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163154-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BSEkZM6h; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163154-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163154-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 276C11F232BB for ; Mon, 29 Apr 2024 23:37:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74B0B194C7D; Mon, 29 Apr 2024 23:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BSEkZM6h" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9594085945; Mon, 29 Apr 2024 23:37:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714433828; cv=none; b=Oy66IbQppxczs4q/D/V2WzmQsMM8S4QXhxfFLJeitSuwVgi5XKUwn4J3ZcpbWz9Tb07J+Mi1KrhE3+qqUz+Pr1w+hux5gfLFEaRSZWWLjAMrxoslQz/2HkccLDNPKvfZHj5ZqsvN5wUTSdNBH9oy4wrpQrrCFkIYbuvhqhE6oJ8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714433828; c=relaxed/simple; bh=8Izqy6wmN7d6G+VKdDnydx8YVjPo7ETQXmEV8pw8ae0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=SE7itYD4BUEAIbcszcXugFRoKMN+dyyKEkr2QR2kiQOthpN76ySv90AIMjpttVDxkEqzos/+QajUlx98z3Ze6mXhR3ISQcvsba7x5dCzaXYAgvwEFzDNnkiNgiYKsEY7FDMNvHX/0u/jD9kAWAMiS6T1RdA7FX12EScyQFvYwtA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BSEkZM6h; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D189C113CD; Mon, 29 Apr 2024 23:37:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714433828; bh=8Izqy6wmN7d6G+VKdDnydx8YVjPo7ETQXmEV8pw8ae0=; h=From:To:Cc:Subject:Date:From; b=BSEkZM6hPsYKjtpV1/E7dgKyuSPi4LQmjP2FoDwk5M+OyvdSY/poOi5ooOKoML1Q0 5fMlTsYcdlaUVZne6EJqfD26BAEdxlyhK3CEb/q/UN9EdGSoBndNeCnBiVIrIpnmq6 E1lE9ORzUIspHIRLvE6PvkeAr10xPXHimM+zVgtuFav49JH8B9TMVyaITTcYU2F63y YrRIFEW1gw3VyQA8xkBhjxY00VHHJXwrE08coHuy1+3LMqWuKzaeO8vBdfnpNiovmd rLPTcjORtoVkeqt442RD8UUSbySOCgPgvdGWZHOEwM1QmeCFy2dIDZuMirBHeFkm5s QlGGbA34UESlg== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH v2] tools lib subcmd: Show parent options in help Date: Mon, 29 Apr 2024 16:37:07 -0700 Message-ID: <20240429233707.1511175-1-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 [] [] or: perf ftrace [] -- [] [] or: perf ftrace {trace|latency} [] [] or: perf ftrace {trace|latency} [] -- [] [] -b, --use-bpf Use BPF to measure function latency -n, --use-nsec Use nano-second histogram -T, --trace-funcs Show latency of given function After: $ perf ftrace latency -h Usage: perf ftrace [] [] or: perf ftrace [] -- [] [] or: perf ftrace {trace|latency} [] [] or: perf ftrace {trace|latency} [] -- [] [] -a, --all-cpus System-wide collection from all CPUs -b, --use-bpf Use BPF to measure function latency -C, --cpu List of cpus to monitor -n, --use-nsec Use nano-second histogram -p, --pid Trace on existing process id -T, --trace-funcs Show latency of given function -v, --verbose Be more verbose --tid Trace on existing thread id (exclusive to --pid) Signed-off-by: Namhyung Kim --- *v2: remove goto and add comments (Ian) tools/lib/subcmd/parse-options.c | 36 +++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c index 9fa75943f2ed..eb896d30545b 100644 --- a/tools/lib/subcmd/parse-options.c +++ b/tools/lib/subcmd/parse-options.c @@ -806,18 +806,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; - - for (o = opts; o->type != OPTION_END; o++) - ++nr_opts; - - len = sizeof(*o) * (nr_opts + 1); - ordered = malloc(len); - if (!ordered) - goto out; - memcpy(ordered, opts, len); + int nr_opts = 0, nr_group = 0, nr_parent = 0, len; + const struct option *o, *p = opts; + struct option *opt, *ordered = NULL, *group; + + /* flatten the options that have parents */ + for (p = opts; p != NULL; p = o->parent) { + for (o = p; o->type != OPTION_END; o++) + ++nr_opts; + + /* + * 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++) { -- 2.44.0.769.g3c40516874-goog