Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp553411ybt; Fri, 10 Jul 2020 06:46:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwssoeHJITn2PzpmVzcs6oSCJot7mVXPY7+KpdCQblYdWsGFSCdtE1xo+5wG7wPJKIxlegq X-Received: by 2002:aa7:d44f:: with SMTP id q15mr73864823edr.340.1594388787759; Fri, 10 Jul 2020 06:46:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594388787; cv=none; d=google.com; s=arc-20160816; b=uSBk6IYwWFeNGMHXzQWKSwzNw/9dIkwM3GrSTz042Df9WYk+6VSCRUWF1UI+oBjcTR EgfJ+k0f0Yn/Iam6fL/0mhVxhndeVaYfAOigjwu3CgQrBYgnz4TzvuasoTceEMZH89+b r/izhS2XWWy5nirmCuljxGEipGgJr+zuYO8vZAAAK++IsM+xG9ZAxgpqujDXCdGyqgiT kv/GMJRkvJC5qvhKUrTc6P36v9ahbCj1i7SOGyhpi8+FAFR7n9yPRWzyXF/ZgOWDLrOv OJE2ol7+MeNKpOLbkp7mdeHBpZwgLwGnPdTrj4Gj8uHPFc4DGgzaVE5xfk3/S8kPTelL dTAQ== 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=uv3rCj+Vqb7FqQwqXteSi9GlGTQnVRu2j5c/6wlPEzA=; b=P72TzdRv8x420KEgL3jCQT6Ep6S7yXSFKrWn/5G6qR6Pf59XoGvSsQsN58LTpN85wz 56SY4MwAV6vts6+Xp/aza98mE094MRvmW+cQoauePt4gkyh1SO5EBGS0igU8oEGytcST GPeUlVPYBbFm+CLs1BvY5dv9AgFej+vBpSXrH9wqzmh6h16Ms9gLa4yPxLQ/iCaDLNQr lNXVA5Sq4g2G/9DXV5VaPre+xaUuNuCt5YG+/I7RMvrFcknCmH0Xzg5cX11Q7i7cAwXI XMTQsg1/1F6d58IyeCYSTQROOTfIl6JX+Uj4l34Yr2iNhicwFCbaUddWEMbLfuRBQfUf HS0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WnKOMvJ1; 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 dp4si4939164ejc.258.2020.07.10.06.46.04; Fri, 10 Jul 2020 06:46:27 -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=WnKOMvJ1; 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 S1728259AbgGJNpE (ORCPT + 99 others); Fri, 10 Jul 2020 09:45:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728177AbgGJNop (ORCPT ); Fri, 10 Jul 2020 09:44:45 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46B70C08C5CE for ; Fri, 10 Jul 2020 06:44:45 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id ch3so2634462pjb.5 for ; Fri, 10 Jul 2020 06:44:45 -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=uv3rCj+Vqb7FqQwqXteSi9GlGTQnVRu2j5c/6wlPEzA=; b=WnKOMvJ1Zon5uI4z9fjuXdS78Em0iWVSQCmgUwpINR4FRd1D4aLuqZI+2d5Av5z8fo Rb58TFCR/Z9K7klwEmgzSaUPmgMTAuaAs2AwEEnMt1tpsA164m3xQMej1DCFSa/Esjbc uh5WXD+RmrR6CQwZBrIBlxAPmkNH0k6YNlilX8axWHy9xufr5qVkEgF/5p8cSM6WxRgA eZwqdf+Gixl/KcwFBjt6s41X+jNYSO1hJpTUXeCXM0ALA8fDLigqN4DmnyC5WoxYY3Da guEEv5puKQHTo+qNbwWW6gfkVvi0Mg+zTEjRj4aNTKhOgylUAH4SqGimM8FCLVl+cN+u WXig== 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=uv3rCj+Vqb7FqQwqXteSi9GlGTQnVRu2j5c/6wlPEzA=; b=LFyGk7ryCY2IGdJDe2o32VlZkcIQPUPjYN2dXeYVt6MId3pTkDDzwvNOhQfM5vVQbc o3S3KCHC0ITQfAVX05pdmooUlYiEJ7o5j1X7cHyDzLPM49BXqeuGyTBIc9d9EIEwuQjw MuGW/0uQQjVsQO0TR/nbR5o6CrY+pnuXRzjOE2AqRqCD8HXRBHyk+SvW8JIiuL3jOm8d LAoA08QOxhBDfms5nnPgBwPHF2Sgx5Tc+4vtFhoYz+9sSJzRNqZ5gGl6/IO9QLl8XoxI rJc3U8YpBm9A6hlQ1IyWFdNuixDNj+yhUc0Rn5Dm0srRX6PI32Xp8lSvAKeJcUGE32X5 b8aQ== X-Gm-Message-State: AOAM530hXhWsP1ZaRqcf6JMd5/9G0OUN31QjyKKJdF9Si/AX1AFeyVHN xn2sk3/quKWYOw9kUUAbHDes3R55 X-Received: by 2002:a17:902:bb8a:: with SMTP id m10mr3088198pls.137.1594388684836; Fri, 10 Jul 2020 06:44:44 -0700 (PDT) Received: from vultr.guest ([149.248.10.52]) by smtp.gmail.com with ESMTPSA id 198sm6297363pfb.27.2020.07.10.06.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2020 06:44:44 -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 v4 08/17] perf: util: add general function to parse sublevel options Date: Fri, 10 Jul 2020 21:43:13 +0800 Message-Id: <20200710134322.15400-9-changbin.du@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200710134322.15400-1-changbin.du@gmail.com> References: <20200710134322.15400-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 | 63 ++++++++++++++++++++++++ tools/perf/util/parse-sublevel-options.h | 11 +++++ 4 files changed, 92 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..39798568547c --- /dev/null +++ b/tools/perf/util/parse-sublevel-options.c @@ -0,0 +1,63 @@ +#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[0]; + char *vstr, *s = strdup(str); + int v = 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) + 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