Skip to content

Commit a80abe2

Browse files
changbinduacmel
authored andcommitted
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 <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Steven Rostedt (VMware) <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 5b34747 commit a80abe2

File tree

4 files changed

+99
-44
lines changed

4 files changed

+99
-44
lines changed

tools/perf/util/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ endif
117117
perf-y += parse-branch-options.o
118118
perf-y += dump-insn.o
119119
perf-y += parse-regs-options.o
120+
perf-y += parse-sublevel-options.o
120121
perf-y += term.o
121122
perf-y += help-unknown-cmd.o
122123
perf-y += mem-events.o

tools/perf/util/debug.c

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "target.h"
2121
#include "ui/helpline.h"
2222
#include "ui/ui.h"
23+
#include "util/parse-sublevel-options.h"
2324

2425
#include <linux/ctype.h>
2526

@@ -173,65 +174,37 @@ void trace_event(union perf_event *event)
173174
trace_event_printer, event);
174175
}
175176

176-
static struct debug_variable {
177-
const char *name;
178-
int *ptr;
179-
} debug_variables[] = {
180-
{ .name = "verbose", .ptr = &verbose },
181-
{ .name = "ordered-events", .ptr = &debug_ordered_events},
182-
{ .name = "stderr", .ptr = &redirect_to_stderr},
183-
{ .name = "data-convert", .ptr = &debug_data_convert },
184-
{ .name = "perf-event-open", .ptr = &debug_peo_args },
177+
static struct sublevel_option debug_opts[] = {
178+
{ .name = "verbose", .value_ptr = &verbose },
179+
{ .name = "ordered-events", .value_ptr = &debug_ordered_events},
180+
{ .name = "stderr", .value_ptr = &redirect_to_stderr},
181+
{ .name = "data-convert", .value_ptr = &debug_data_convert },
182+
{ .name = "perf-event-open", .value_ptr = &debug_peo_args },
185183
{ .name = NULL, }
186184
};
187185

188186
int perf_debug_option(const char *str)
189187
{
190-
struct debug_variable *var = &debug_variables[0];
191-
char *vstr, *s = strdup(str);
192-
int v = 1;
193-
194-
vstr = strchr(s, '=');
195-
if (vstr)
196-
*vstr++ = 0;
197-
198-
while (var->name) {
199-
if (!strcmp(s, var->name))
200-
break;
201-
var++;
202-
}
203-
204-
if (!var->name) {
205-
pr_err("Unknown debug variable name '%s'\n", s);
206-
free(s);
207-
return -1;
208-
}
188+
int ret;
209189

210-
if (vstr) {
211-
v = atoi(vstr);
212-
/*
213-
* Allow only values in range (0, 10),
214-
* otherwise set 0.
215-
*/
216-
v = (v < 0) || (v > 10) ? 0 : v;
217-
}
190+
ret = perf_parse_sublevel_options(str, debug_opts);
191+
if (ret)
192+
return ret;
218193

219-
if (quiet)
220-
v = -1;
194+
/* Allow only verbose value in range (0, 10), otherwise set 0. */
195+
verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
221196

222-
*var->ptr = v;
223-
free(s);
224197
return 0;
225198
}
226199

227200
int perf_quiet_option(void)
228201
{
229-
struct debug_variable *var = &debug_variables[0];
202+
struct sublevel_option *opt = &debug_opts[0];
230203

231204
/* disable all debug messages */
232-
while (var->name) {
233-
*var->ptr = -1;
234-
var++;
205+
while (opt->name) {
206+
*opt->value_ptr = -1;
207+
opt++;
235208
}
236209

237210
return 0;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#include "util/debug.h"
7+
#include "util/parse-sublevel-options.h"
8+
9+
static int parse_one_sublevel_option(const char *str,
10+
struct sublevel_option *opts)
11+
{
12+
struct sublevel_option *opt = opts;
13+
char *vstr, *s = strdup(str);
14+
int v = 1;
15+
16+
if (!s) {
17+
pr_err("no memory\n");
18+
return -1;
19+
}
20+
21+
vstr = strchr(s, '=');
22+
if (vstr)
23+
*vstr++ = 0;
24+
25+
while (opt->name) {
26+
if (!strcmp(s, opt->name))
27+
break;
28+
opt++;
29+
}
30+
31+
if (!opt->name) {
32+
pr_err("Unknown option name '%s'\n", s);
33+
free(s);
34+
return -1;
35+
}
36+
37+
if (vstr)
38+
v = atoi(vstr);
39+
40+
*opt->value_ptr = v;
41+
free(s);
42+
return 0;
43+
}
44+
45+
/* parse options like --foo a=<n>,b,c... */
46+
int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts)
47+
{
48+
char *s = strdup(str);
49+
char *p = NULL;
50+
int ret;
51+
52+
if (!s) {
53+
pr_err("no memory\n");
54+
return -1;
55+
}
56+
57+
p = strtok(s, ",");
58+
while (p) {
59+
ret = parse_one_sublevel_option(p, opts);
60+
if (ret) {
61+
free(s);
62+
return ret;
63+
}
64+
65+
p = strtok(NULL, ",");
66+
}
67+
68+
free(s);
69+
return 0;
70+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef _PERF_PARSE_SUBLEVEL_OPTIONS_H
2+
#define _PERF_PARSE_SUBLEVEL_OPTIONS_H
3+
4+
struct sublevel_option {
5+
const char *name;
6+
int *value_ptr;
7+
};
8+
9+
int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts);
10+
11+
#endif

0 commit comments

Comments
 (0)