Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2711528pxa; Fri, 7 Aug 2020 19:41:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwx21OtV9SEwuDOborg62zXWp40JrOh3TdrdRWRQLurb4mqTKhtW0TSHT7KKJW9jUXjTLC X-Received: by 2002:a05:6402:84e:: with SMTP id b14mr11340797edz.115.1596854510981; Fri, 07 Aug 2020 19:41:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596854510; cv=none; d=google.com; s=arc-20160816; b=iWe/gaJ4abXzrsf7m+QfXPafNyo6KN3/iCC5j84iJd5rbCiq3plg6VKj6nmIXSsoq0 RhMhNnRvHeHPMOShZqgjK5t+yeprOXIbe1niAd5IVFxdvYSVz3XB8DOzz3UVEFWl2cS+ hZmS+gkUQi6LVjio4OFHjcuDxIZshxe1jJgoWJmzJ0UX13IK2Z3B2BzNoNCQPwsyD7ph l1USBVF73CT/xh6BLG+4JWGG/ICgDx0XGy9BPJRKMdIY+JGKCaIeNTEjmT3HXf0LdGTi 7fsdHkmHMGFmK9q/kMacLn3xjEmqSGdbRZKJxZt8nAlfKIcoVI/vJotbj4EG2SYs4b5/ pwpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=djkMUkZcrMKQIRHk66rYevNXyQYjoTPBJqAUtrmCrFU=; b=XXDv1o5ggIX5db9hjhobMX0OLWBrd4089QZP99NbPrJsaF0Y61xp4EMlL6MyQUA9Xe qFNtYMqVWN1V7HzOg2zEVJXiO9mh8Goo646oLuHjyicCfhjLjBRU6q2DwWcoAajFRqur qK8PIfkmHxl59FUh8TitEBD/74favI2IDQwGZM8VKou5XLDhLLN2V4r5V3aref5L9/G4 HatGdUw0+LjT4Vt20zbz0uNBhGO4GLuB9c1R+xAmM9mcQqmbfaeSP1My7dMCeZQmAg48 QOu60aRwFgMdbv3uaX3eTwF+YetB2YOizC2EaTB2J1fFe70VwVV40X2u4pz9EqYv0IOx sjbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MUwMipCi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i2si7185289ejp.500.2020.08.07.19.41.28; Fri, 07 Aug 2020 19:41:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MUwMipCi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726293AbgHHCjK (ORCPT + 99 others); Fri, 7 Aug 2020 22:39:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgHHCjI (ORCPT ); Fri, 7 Aug 2020 22:39:08 -0400 Received: from mail-qv1-xf44.google.com (mail-qv1-xf44.google.com [IPv6:2607:f8b0:4864:20::f44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F71CC061756 for ; Fri, 7 Aug 2020 19:39:08 -0700 (PDT) Received: by mail-qv1-xf44.google.com with SMTP id o2so1670434qvk.6 for ; Fri, 07 Aug 2020 19:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=djkMUkZcrMKQIRHk66rYevNXyQYjoTPBJqAUtrmCrFU=; b=MUwMipCiS3U+QJPXtfsbgCtqpFrdTwS7pey7j+iXKnaHtVF0SdQzOvfX3VstiI+9Lp YGJ5PSZawNpvNT7Ym8GmtO+Va4ChofXWWIlZ1YQd/ywqM39TBHw5JB1v8Z6xf3SQR8rC ruAXdaXrHljG5xVxGea/PR5cMlGpfs1jVrwn0DsFE312dzaWWRD7v10ysiZXFhESf4y7 ofE9G+xFR/l3WkpgE+yw8neg3AAUXl1pmWkZo8tl3Yup18dRzioUlzl0xcy62r1MuVRp g5jtdS0y9Mmi8+ARrVixLmmcfFatHnF7I5U9EqMaIrgIqmtJ0FqO6Msm8fE3eR/9pS+o er9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=djkMUkZcrMKQIRHk66rYevNXyQYjoTPBJqAUtrmCrFU=; b=Zl2Vi2qsOYMphHfUw2YTSfvz/Q7Wu2PFcD9Ugj9BkqgePdThr0FdyzsMPjUdZxGd+q HihY+Z6d7F8yb6fxjZLfZDfRIy5d1R72lzJvGMvkNyCp9CZCGtdHRTwxAc81C2l0vVWp efr19gG2UW1DqxpPv3TXb7liPo/TBEWezAzDgPQ6WSAs3c2pB16xgaGLyIobNgooB/Ez kgQgMlxelxN+1BrYrp5bCbYX0Dq4WA3wKHSGe3GySmqCyChYB+I1vWM4NEU/h5CLm5xH EuAAweBEfIejzebXjRADbOWUOCI2GTnELVrNRl8KY9ft66k7alKANbIXvCDhJilLOkTe CKOw== X-Gm-Message-State: AOAM531R44uoReWI+uFUd5PEZaV7IcfRCwavRhEht9YD6E+HOE1PvpGa DbcT0xbm4KVgLpaF88gsnDI= X-Received: by 2002:a0c:fb11:: with SMTP id c17mr17008021qvp.113.1596854347051; Fri, 07 Aug 2020 19:39:07 -0700 (PDT) Received: from vultr.guest ([66.42.85.36]) by smtp.gmail.com with ESMTPSA id n33sm7818702qtd.43.2020.08.07.19.36.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Aug 2020 19:39:06 -0700 (PDT) From: Changbin Du To: Jiri Olsa , Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , Namhyung Kim , Steven Rostedt , linux-kernel@vger.kernel.org, Changbin Du Subject: [PATCH v8 07/18] perf: util: add general function to parse sublevel options Date: Sat, 8 Aug 2020 10:31:30 +0800 Message-Id: <20200808023141.14227-8-changbin.du@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200808023141.14227-1-changbin.du@gmail.com> References: <20200808023141.14227-1-changbin.du@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This factors out a general function perf_parse_sublevel_options() to parse sublevel options. The 'sublevel' options is something like the '--debug' options which allow more sublevel options. Signed-off-by: Changbin Du --- v2: add util/parse-sublevel-options.c --- tools/perf/util/Build | 1 + tools/perf/util/debug.c | 61 ++++++--------------- tools/perf/util/parse-sublevel-options.c | 70 ++++++++++++++++++++++++ tools/perf/util/parse-sublevel-options.h | 11 ++++ 4 files changed, 99 insertions(+), 44 deletions(-) create mode 100644 tools/perf/util/parse-sublevel-options.c create mode 100644 tools/perf/util/parse-sublevel-options.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 8d18380ecd10..e86607ada0b5 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -117,6 +117,7 @@ endif perf-y += parse-branch-options.o perf-y += dump-insn.o perf-y += parse-regs-options.o +perf-y += parse-sublevel-options.o perf-y += term.o perf-y += help-unknown-cmd.o perf-y += mem-events.o diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index adb656745ecc..5cda5565777a 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -20,6 +20,7 @@ #include "target.h" #include "ui/helpline.h" #include "ui/ui.h" +#include "util/parse-sublevel-options.h" #include @@ -173,65 +174,37 @@ void trace_event(union perf_event *event) trace_event_printer, event); } -static struct debug_variable { - const char *name; - int *ptr; -} debug_variables[] = { - { .name = "verbose", .ptr = &verbose }, - { .name = "ordered-events", .ptr = &debug_ordered_events}, - { .name = "stderr", .ptr = &redirect_to_stderr}, - { .name = "data-convert", .ptr = &debug_data_convert }, - { .name = "perf-event-open", .ptr = &debug_peo_args }, +static struct sublevel_option debug_opts[] = { + { .name = "verbose", .value_ptr = &verbose }, + { .name = "ordered-events", .value_ptr = &debug_ordered_events}, + { .name = "stderr", .value_ptr = &redirect_to_stderr}, + { .name = "data-convert", .value_ptr = &debug_data_convert }, + { .name = "perf-event-open", .value_ptr = &debug_peo_args }, { .name = NULL, } }; int perf_debug_option(const char *str) { - struct debug_variable *var = &debug_variables[0]; - char *vstr, *s = strdup(str); - int v = 1; - - vstr = strchr(s, '='); - if (vstr) - *vstr++ = 0; - - while (var->name) { - if (!strcmp(s, var->name)) - break; - var++; - } - - if (!var->name) { - pr_err("Unknown debug variable name '%s'\n", s); - free(s); - return -1; - } + int ret; - if (vstr) { - v = atoi(vstr); - /* - * Allow only values in range (0, 10), - * otherwise set 0. - */ - v = (v < 0) || (v > 10) ? 0 : v; - } + ret = perf_parse_sublevel_options(str, debug_opts); + if (ret) + return ret; - if (quiet) - v = -1; + /* Allow only verbose value in range (0, 10), otherwise set 0. */ + verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose; - *var->ptr = v; - free(s); return 0; } int perf_quiet_option(void) { - struct debug_variable *var = &debug_variables[0]; + struct sublevel_option *opt = &debug_opts[0]; /* disable all debug messages */ - while (var->name) { - *var->ptr = -1; - var++; + while (opt->name) { + *opt->value_ptr = -1; + opt++; } return 0; diff --git a/tools/perf/util/parse-sublevel-options.c b/tools/perf/util/parse-sublevel-options.c new file mode 100644 index 000000000000..a841d17ffd57 --- /dev/null +++ b/tools/perf/util/parse-sublevel-options.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#include "util/debug.h" +#include "util/parse-sublevel-options.h" + +static int parse_one_sublevel_option(const char *str, + struct sublevel_option *opts) +{ + struct sublevel_option *opt = opts; + char *vstr, *s = strdup(str); + int v = 1; + + if (!s) { + pr_err("no memory\n"); + return -1; + } + + vstr = strchr(s, '='); + if (vstr) + *vstr++ = 0; + + while (opt->name) { + if (!strcmp(s, opt->name)) + break; + opt++; + } + + if (!opt->name) { + pr_err("Unknown option name '%s'\n", s); + free(s); + return -1; + } + + if (vstr) + v = atoi(vstr); + + *opt->value_ptr = v; + free(s); + return 0; +} + +/* parse options like --foo a=,b,c... */ +int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts) +{ + char *s = strdup(str); + char *p = NULL; + int ret; + + if (!s) { + pr_err("no memory\n"); + return -1; + } + + p = strtok(s, ","); + while (p) { + ret = parse_one_sublevel_option(p, opts); + if (ret) { + free(s); + return ret; + } + + p = strtok(NULL, ","); + } + + free(s); + return 0; +} diff --git a/tools/perf/util/parse-sublevel-options.h b/tools/perf/util/parse-sublevel-options.h new file mode 100644 index 000000000000..9b9efcc2aaad --- /dev/null +++ b/tools/perf/util/parse-sublevel-options.h @@ -0,0 +1,11 @@ +#ifndef _PERF_PARSE_SUBLEVEL_OPTIONS_H +#define _PERF_PARSE_SUBLEVEL_OPTIONS_H + +struct sublevel_option { + const char *name; + int *value_ptr; +}; + +int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts); + +#endif \ No newline at end of file -- 2.25.1