Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1289707ybt; Sat, 11 Jul 2020 05:44:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQasUlO7HL7wEw7G7eWFyn71En0laL5IfOoMCkDSWzuzwR4mE0UlK2mMXqFwZxByN/i4Uc X-Received: by 2002:a17:906:5fc4:: with SMTP id k4mr65199978ejv.94.1594471449140; Sat, 11 Jul 2020 05:44:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594471449; cv=none; d=google.com; s=arc-20160816; b=xX7HkFQClewEG0GBJNFw5htyCfshgL0GtlY5Q6HSRg9g1zb7m2xsUevJVJKTr2sTif dp8xHms+DgOHYpzCqdgx6x1pIO/Sz4Ar8sCFX1IVI+fNybpI2CUF+wtgLBTmNKmRre+J ZkpWdXAoEDfTu72qBjMpPJYRxmlowHHyo7LUax7ONjSeqRZ/pqRusHNHXy85FtL/qcr/ E+bLrFNLzMLOAXe28w6bBMCYs1ij+m/+/V14cT5iGeJhCU40kfXkvNTyZ9AqCFmlP0D1 pSWJ/FY++ybiiwPV19GU6J12HMKLCiToPpLRRPnzgBgslbSRCcmEhTyCWkDvxjMEU8wZ lvwg== 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=ZDkgYwE02T7zwHYgSe2pvm9tnnPOI+GyaM0Wn65V9qqVf1DUyJlzk3ZCZInQDd2He5 Bwo5x/Lruafb5M1yJw9UQEDtT2dtwYBSzWQCGAqKYusPqCeO1z9OvGw0T7UpxxoC+FOX LqAvYyea6rM4+VK+Y5QdZSTaeKNsONsm2H//MRiKpp1a95EASgbMvKEv/7NQ/4gNme6h xX28bEF6nBaI1Yt6FGH02NEchayxz9PNWkKaDByNRUM+GVb4SWIFCgQt/iyR6DrKTG7x gYAP9IVrJ5W178BFIcmj6/66fhZjB97Kc2sTW4CE1SkBt9vda4SYWE0zkHTkiJvAlZMy QmCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gYJ7ODul; 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 dn9si5741270edb.344.2020.07.11.05.43.46; Sat, 11 Jul 2020 05:44:09 -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=gYJ7ODul; 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 S1728401AbgGKMnE (ORCPT + 99 others); Sat, 11 Jul 2020 08:43:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726502AbgGKMnD (ORCPT ); Sat, 11 Jul 2020 08:43:03 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B48BC08C5DD for ; Sat, 11 Jul 2020 05:43:03 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id g67so3752269pgc.8 for ; Sat, 11 Jul 2020 05:43:03 -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=gYJ7ODulJaXK7RshciUPcnDPVtoo8P6Ghqw/WGnTtBeGKIivt5KeLB4j9FeARPmF3F /AhoPMo2RC9kVoHnH/7exGG6USsha2+S33q8Xhc0KtZRKCWJOpfz+fu26dSG/pM5y4BQ Q2BkFg4QIE1UZPTZ04+mwKHJddaP2v0g+sD8jFy4gAHAvVKlgGfyFAF+kUgzPZWVrZos dhP3UQihLEYqo+S1xZxT5mz6pauGZQjDcCQ3P3hbtxcNOuErQVswisqH4+phnoBLlqP7 aGRMqHx3ml3gHE33a4t5ctlV67R1aTkxUF8j1nLL+6YZJDeZ75BZj1CaUheElfC55wkN kBnA== 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=ewzAOX/U+lr1U8u16kpzAdnYXnH/+GhOENrxRzJbjD2je5tvXFwrwQ6B98dbo3IEfs h90wC+qCdWoDlx0OdB9XO/DoxD4FFjnqWbMxrrxSYhxoqYILKumy03SKb1y4cfYBr/nX hs2X3tyzumhwgGdSLU+h6w9mIfFJeHpof8ce+JOo3MpsP2TPDDtK27GfLHR9QYLdxpI9 1sFzII2imb6Rb4RpxZl1qVMYbLdgg56j+gZxI6IY9GUqewLPzl4rTuZZ/fVkfYb+TLCQ 0SpRFrT41JEKatoHXEGKUOz8BlYPTGV2C907Y0deuN8HP+XxxyoQJQ18uaSKON0fi1td frFw== X-Gm-Message-State: AOAM530mhM/jHKR3i0OEhC3UECQzS7zYiRnHCwPnIWT1EUnQ1CCum/L5 ndM0wIEygy8NTF6txiC1i9M= X-Received: by 2002:a63:8f58:: with SMTP id r24mr60275566pgn.379.1594471383130; Sat, 11 Jul 2020 05:43:03 -0700 (PDT) Received: from vultr.guest ([149.248.10.52]) by smtp.gmail.com with ESMTPSA id c19sm8352899pjs.11.2020.07.11.05.42.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2020 05:43:02 -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 v5 08/17] perf: util: add general function to parse sublevel options Date: Sat, 11 Jul 2020 20:40:26 +0800 Message-Id: <20200711124035.6513-9-changbin.du@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200711124035.6513-1-changbin.du@gmail.com> References: <20200711124035.6513-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