Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp588716ybt; Fri, 10 Jul 2020 07:32:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXlsZF4VwtB5eIAOWS9DM4FZXRcuSK14gu2igZc52bnnPUlc6cO/18wTm7FQdkbnqY+L/X X-Received: by 2002:a05:6402:796:: with SMTP id d22mr81688120edy.78.1594391574160; Fri, 10 Jul 2020 07:32:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594391574; cv=none; d=google.com; s=arc-20160816; b=XIPit6wxysoC8G50RsgdTPZyMSFfDyeABYC4/jHOHz4gumMGphEk9wgxh1j71Y2pBt REWV416h+5TqSm2rBKu+Qv7wcxBOsCLOa3YqmdqDYK7mSD+z1LY4AbMmGUe05ckRoX67 VZJddwxTsFWU1ZBGRl0aeOd9SY3S5r3JGD3S58GdIlSsSLpt5taXFTtbPjtiJ7UsJ9QB MAEdXX2cxGQvJIOMXAmXTGSwO8byPsvD/Yd+GA8nbdh0UHoe7dIXgVlga03zgzE3sHvj GFRkBEoBGRleSik8F4fanFRhsi63dyKsTA16tzu40czJWmuswD3c0Sk73ROELOsVKBUg 0grA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=pdnUnHtfirlekhrwzQjF1FZ328y4VuucsToB4ssq670=; b=t0dSSphVDuHnKDKvLeMZLxOdoGSMYpdD9sjp8uvsDJMiQHq36i1B8jeClmf+xK8SqX gSz9I1bpxrkFzzSwvvX83yWDMowHp+Wp+BQZ1UGEeTP2AhoHVfhHwY5bakndhs/Nq6CB 4gCtxqzzuxoEXwK6fun5qXRBCyiMsnq5RqpaUBphONyVRIHsk8rMe8Lm+I8zEamjLQkV CAq2KCcyAkKl8upNJq1XRgz+JpCTyWE+XSzQFRsBaWOdEQd0JulQdhlzOYz0VZN5Y1AV 7cWGRWqS5Zci6B3rD8Pa/MzxEgHcZq6F3Uys9mX8g/3fGpSWU7XRvK9o4CbsvrFWkPCa gp+w== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce16si4012835ejb.436.2020.07.10.07.32.30; Fri, 10 Jul 2020 07:32:54 -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; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728059AbgGJOaD (ORCPT + 99 others); Fri, 10 Jul 2020 10:30:03 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:34028 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728040AbgGJOaD (ORCPT ); Fri, 10 Jul 2020 10:30:03 -0400 Received: by mail-wm1-f67.google.com with SMTP id g10so8592549wmc.1 for ; Fri, 10 Jul 2020 07:30:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pdnUnHtfirlekhrwzQjF1FZ328y4VuucsToB4ssq670=; b=kemD2Anqae6DdE1oBkwiALzvnLyufMoRvtwCRFju5x9GmzSh9vFyDbZ6iFwGkuvJMS GYH2pfa4wLBxtOe2pUtX/+DqYidxBPKu6KPuCi0THFACZPEMjw+Ty14DaVR18jFlYo8y Ae9McW8m8Nlu3+o6M2m8uL6PPI7B0CIYaYlXYjJyTNj7Mq7GeGeainn6U2VTgPS0Q36x Fg0XVdZB/J3m4KWt8N1IZu7vuGrMq/JlSbD/jrmfEUoBovGkhWYLV+GeDGvVEfNlEmxU SqOECn1wZW0fjgaGRvo/MZy6bNbkIM6hm3x/FRciergwNzWLunKoh+z+UXUr+eLH1up8 yEhg== X-Gm-Message-State: AOAM530W1Zrxe1wF6wbG2TI8Ri2FaxNohtf9HO+buTCUZkKRn/aeg4Kx ENO6jUjjkXv+ImYPZ4vxz4sdBiHUKpXtnEBJ4/Y= X-Received: by 2002:a1c:6308:: with SMTP id x8mr5708081wmb.92.1594391400604; Fri, 10 Jul 2020 07:30:00 -0700 (PDT) MIME-Version: 1.0 References: <20200710134322.15400-1-changbin.du@gmail.com> <20200710134322.15400-9-changbin.du@gmail.com> In-Reply-To: <20200710134322.15400-9-changbin.du@gmail.com> From: Namhyung Kim Date: Fri, 10 Jul 2020 23:29:49 +0900 Message-ID: Subject: Re: [PATCH v4 08/17] perf: util: add general function to parse sublevel options To: Changbin Du Cc: Jiri Olsa , Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Steven Rostedt , linux-kernel Content-Type: text/plain; charset="UTF-8" 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 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(). Also I think you can just use the opts pointer.. > + > + 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 >