Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1286582ybt; Sat, 11 Jul 2020 05:38:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2CQ7stmrA5IKWgv51JAzdurJZgxhDqBpST1NpgVT84K4fMAiWNeVkoW8qrkF7CJz8SkAm X-Received: by 2002:a17:906:1a59:: with SMTP id j25mr61974336ejf.398.1594471110863; Sat, 11 Jul 2020 05:38:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594471110; cv=none; d=google.com; s=arc-20160816; b=VhGX2Z+VManihSIaLDNRyOZRYcfKLTkF3ShHflbrYkN8/vcRXKdNWAeOtApzr59Wsm g18cae4BkVm+tN1jQKuVCxAPe1fjpb0kp84ckh6ko8tbL6IRfWQlLKcHWi/H2mv6HS6+ TWamfKS55WJazcqTHECnJhuaQHmFWOX5IxxhjnvDQaEeKtCgoXZmuFBwge9Awv53eowV gtFKZNXvouTBgUgyXNiGG2foeWVZOeumTCc3d5MCj4+vfkyyU+1lzdqFLeXVcm+dFe6w aOfXZpF6wu9ksM//CqDc2l7QeO7ulWPJHQuOS2qLJ0sBILn7j4aFFPykoqLqRewH4YaG wQAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=Ja2u/aTH0QYUI+aOka8Hx8/x8jsy05eo2BYAUZcjGF0=; b=ZGa9QbGA5QB0wtzRHAsIm8NwE7s9UVktGBIm9Gph8GI23E78bjw+wvOhd9ySD/Xru8 sCTtKS572c2G+EZQXofmKaQ6SOdwhEEFksdKp7u43VnURlQlkeiMMQmPS9xRVv+a4aft QEQr6T28vwZS0OXr3RXSDef4IA7tCfKgLyXXCcCFZ999XVXlGyN7AedzD8+sV2PBS3Ep Iun8oFWvE9LPvH2WuZ4VIn8Ph1vdeDWb3/DZmnyLC2mSpOXRgi1F5eYCeN0kaeLryiC+ TiUMLii0yXkotX3jDogjTkELNRWBwYCqqAOdpQf0Lwc3oR0w4nRnHT/jlW0QCGQFTraG A7Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=cEaKL+pO; 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 cq21si5902928edb.353.2020.07.11.05.38.08; Sat, 11 Jul 2020 05:38:30 -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=cEaKL+pO; 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 S1728174AbgGKMhf (ORCPT + 99 others); Sat, 11 Jul 2020 08:37:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726502AbgGKMhe (ORCPT ); Sat, 11 Jul 2020 08:37:34 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F807C08C5DD for ; Sat, 11 Jul 2020 05:37:34 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id m9so3768378pfh.0 for ; Sat, 11 Jul 2020 05:37:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Ja2u/aTH0QYUI+aOka8Hx8/x8jsy05eo2BYAUZcjGF0=; b=cEaKL+pOBerfmPoBsOViPTFvVm0Zxt7177Srgy7ciPMn5Dy2LZOidFFyHRJVOZWlJS jGtfA02Pu+Bkmdlw5MyKqATYFlS8dlZhU9VyHCMzn5GdjQX8X6ZuF6P7k+DA73rqvLcH 6JgT02fgZtrn4DFIma69ccMb6FOZXv1bvL3TEiwDhpriwrMhjfJkvnJjBXXFnMXLK3P9 ebKv7wvg4L226CoD4+Q8BQ/Wt5TV/hUrw1/YQpzicBNJRBZOKoDJEeJUB1ytRB6TUpda yQG5g3GPLhozlSdRy6OWQ5p8UypJRsNsY8cy7R8UAIKgi2awjpz7+4lIxcAx/J2nRxif ilFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Ja2u/aTH0QYUI+aOka8Hx8/x8jsy05eo2BYAUZcjGF0=; b=k60ghXJF+Q7aK+5knW4lLbS3H6963RgV06farauh1fmgplKTXyvYbWtBWFo6KOykUz OUooX0uafhwnNeMHMRm3YHIru8GLW3maCRv5gIGNcYxNd2usCI0L91Wudm/0ujW3BNqB De5x7+6hrTIoYhEQgXpOwEl66e/PJRI7EipRyxUA1aV1k2yWCciBqRUyuRX/a1zjfx3k Y2mZH6fLI3U7+FsR+kg04H06SYekbEiljN5GfGQoyZFVk/J7g+QL553acgWiPcP7hYIN DxxN0xmJU5WWluNOLJyltM6n4rY7MHcAodo6yvOhMeESZiJgztUBmwagOlZ+9RvEHZ0n yWsg== X-Gm-Message-State: AOAM533ZG2aKqAAyR8fknD2epzKUl+Q748efrUJ1htTpBGmL0KT8wz38 pj1lEuCDVrMFm/oCGYSsljI= X-Received: by 2002:a65:6703:: with SMTP id u3mr58633040pgf.254.1594471053983; Sat, 11 Jul 2020 05:37:33 -0700 (PDT) Received: from mail.google.com ([149.248.10.52]) by smtp.gmail.com with ESMTPSA id c2sm8558777pgk.77.2020.07.11.05.37.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2020 05:37:32 -0700 (PDT) Date: Sat, 11 Jul 2020 20:37:25 +0800 From: Changbin Du To: Namhyung Kim Cc: Changbin Du , Jiri Olsa , Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Steven Rostedt , linux-kernel Subject: Re: [PATCH v4 08/17] perf: util: add general function to parse sublevel options Message-ID: <20200711123725.sojcctyvzyxzlfzs@mail.google.com> References: <20200710134322.15400-1-changbin.du@gmail.com> <20200710134322.15400-9-changbin.du@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 10, 2020 at 11:29:49PM +0900, Namhyung Kim wrote: > On Fri, Jul 10, 2020 at 10:44 PM Changbin Du wrote: > > > > 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 > > --- > [snip] > > 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; > > I know you just copied the code, but let's add a check for > the return value of strdup(). > yes, the 's' should be checked here. > Also I think you can just use the opts pointer.. > For this, personally I prefer to opt as it's singular form. Because we are dealing with single element. > > > + > > + 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 > > Please add a newline at the end. > > Thanks > Namhyung > > > > -- > > 2.25.1 > > -- Cheers, Changbin Du