perf tools: Add general function to parse sublevel options
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 <changbin.du@gmail.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt (VMware) <rostedt@goodmis.org> Link: http://lore.kernel.org/lkml/20200808023141.14227-8-changbin.du@gmail.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
committed by
Arnaldo Carvalho de Melo
parent
5b34747238
commit
a80abe2a9a
@@ -117,6 +117,7 @@ endif
|
|||||||
perf-y += parse-branch-options.o
|
perf-y += parse-branch-options.o
|
||||||
perf-y += dump-insn.o
|
perf-y += dump-insn.o
|
||||||
perf-y += parse-regs-options.o
|
perf-y += parse-regs-options.o
|
||||||
|
perf-y += parse-sublevel-options.o
|
||||||
perf-y += term.o
|
perf-y += term.o
|
||||||
perf-y += help-unknown-cmd.o
|
perf-y += help-unknown-cmd.o
|
||||||
perf-y += mem-events.o
|
perf-y += mem-events.o
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include "target.h"
|
#include "target.h"
|
||||||
#include "ui/helpline.h"
|
#include "ui/helpline.h"
|
||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
|
#include "util/parse-sublevel-options.h"
|
||||||
|
|
||||||
#include <linux/ctype.h>
|
#include <linux/ctype.h>
|
||||||
|
|
||||||
@@ -173,65 +174,37 @@ void trace_event(union perf_event *event)
|
|||||||
trace_event_printer, event);
|
trace_event_printer, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct debug_variable {
|
static struct sublevel_option debug_opts[] = {
|
||||||
const char *name;
|
{ .name = "verbose", .value_ptr = &verbose },
|
||||||
int *ptr;
|
{ .name = "ordered-events", .value_ptr = &debug_ordered_events},
|
||||||
} debug_variables[] = {
|
{ .name = "stderr", .value_ptr = &redirect_to_stderr},
|
||||||
{ .name = "verbose", .ptr = &verbose },
|
{ .name = "data-convert", .value_ptr = &debug_data_convert },
|
||||||
{ .name = "ordered-events", .ptr = &debug_ordered_events},
|
{ .name = "perf-event-open", .value_ptr = &debug_peo_args },
|
||||||
{ .name = "stderr", .ptr = &redirect_to_stderr},
|
|
||||||
{ .name = "data-convert", .ptr = &debug_data_convert },
|
|
||||||
{ .name = "perf-event-open", .ptr = &debug_peo_args },
|
|
||||||
{ .name = NULL, }
|
{ .name = NULL, }
|
||||||
};
|
};
|
||||||
|
|
||||||
int perf_debug_option(const char *str)
|
int perf_debug_option(const char *str)
|
||||||
{
|
{
|
||||||
struct debug_variable *var = &debug_variables[0];
|
int ret;
|
||||||
char *vstr, *s = strdup(str);
|
|
||||||
int v = 1;
|
|
||||||
|
|
||||||
vstr = strchr(s, '=');
|
ret = perf_parse_sublevel_options(str, debug_opts);
|
||||||
if (vstr)
|
if (ret)
|
||||||
*vstr++ = 0;
|
return ret;
|
||||||
|
|
||||||
while (var->name) {
|
/* Allow only verbose value in range (0, 10), otherwise set 0. */
|
||||||
if (!strcmp(s, var->name))
|
verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
|
||||||
break;
|
|
||||||
var++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!var->name) {
|
|
||||||
pr_err("Unknown debug variable name '%s'\n", s);
|
|
||||||
free(s);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vstr) {
|
|
||||||
v = atoi(vstr);
|
|
||||||
/*
|
|
||||||
* Allow only values in range (0, 10),
|
|
||||||
* otherwise set 0.
|
|
||||||
*/
|
|
||||||
v = (v < 0) || (v > 10) ? 0 : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (quiet)
|
|
||||||
v = -1;
|
|
||||||
|
|
||||||
*var->ptr = v;
|
|
||||||
free(s);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int perf_quiet_option(void)
|
int perf_quiet_option(void)
|
||||||
{
|
{
|
||||||
struct debug_variable *var = &debug_variables[0];
|
struct sublevel_option *opt = &debug_opts[0];
|
||||||
|
|
||||||
/* disable all debug messages */
|
/* disable all debug messages */
|
||||||
while (var->name) {
|
while (opt->name) {
|
||||||
*var->ptr = -1;
|
*opt->value_ptr = -1;
|
||||||
var++;
|
opt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
70
tools/perf/util/parse-sublevel-options.c
Normal file
70
tools/perf/util/parse-sublevel-options.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#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=<n>,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;
|
||||||
|
}
|
||||||
11
tools/perf/util/parse-sublevel-options.h
Normal file
11
tools/perf/util/parse-sublevel-options.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user