Commit b1019d5e authored by Masami Hiramatsu's avatar Masami Hiramatsu Committed by Arnaldo Carvalho de Melo

perf probe: Cleanup and consolidate command parsers

To simplify the 'perf probe' command code, consolidate some similar
functions and use command short-name for command classification, instead
of separate booleans.
Signed-off-by: default avatarMasami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20150505152257.18790.41548.stgit@localhost.localdomainSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 3da166b7
...@@ -48,14 +48,10 @@ ...@@ -48,14 +48,10 @@
/* Session management structure */ /* Session management structure */
static struct { static struct {
int command; /* Command short_name */
bool list_events; bool list_events;
bool force_add; bool force_add;
bool show_lines;
bool show_vars;
bool show_ext_vars; bool show_ext_vars;
bool show_funcs;
bool mod_events;
bool del_events;
bool uprobes; bool uprobes;
bool quiet; bool quiet;
bool target_used; bool target_used;
...@@ -175,44 +171,11 @@ static int parse_probe_event_argv(int argc, const char **argv) ...@@ -175,44 +171,11 @@ static int parse_probe_event_argv(int argc, const char **argv)
len += sprintf(&buf[len], "%s ", argv[i]); len += sprintf(&buf[len], "%s ", argv[i]);
} }
params.mod_events = true;
ret = parse_probe_event(buf); ret = parse_probe_event(buf);
free(buf); free(buf);
return ret; return ret;
} }
static int opt_add_probe_event(const struct option *opt __maybe_unused,
const char *str, int unset __maybe_unused)
{
if (str) {
params.mod_events = true;
return parse_probe_event(str);
} else
return 0;
}
static int opt_del_probe_event(const struct option *opt __maybe_unused,
const char *str, int unset __maybe_unused)
{
if (str) {
params.del_events = true;
return params_add_filter(str);
}
return 0;
}
static int opt_list_probe_event(const struct option *opt __maybe_unused,
const char *str, int unset)
{
if (!unset)
params.list_events = true;
if (str)
return params_add_filter(str);
return 0;
}
static int opt_set_target(const struct option *opt, const char *str, static int opt_set_target(const struct option *opt, const char *str,
int unset __maybe_unused) int unset __maybe_unused)
{ {
...@@ -250,8 +213,10 @@ static int opt_set_target(const struct option *opt, const char *str, ...@@ -250,8 +213,10 @@ static int opt_set_target(const struct option *opt, const char *str,
return ret; return ret;
} }
/* Command option callbacks */
#ifdef HAVE_DWARF_SUPPORT #ifdef HAVE_DWARF_SUPPORT
static int opt_show_lines(const struct option *opt __maybe_unused, static int opt_show_lines(const struct option *opt,
const char *str, int unset __maybe_unused) const char *str, int unset __maybe_unused)
{ {
int ret = 0; int ret = 0;
...@@ -259,19 +224,19 @@ static int opt_show_lines(const struct option *opt __maybe_unused, ...@@ -259,19 +224,19 @@ static int opt_show_lines(const struct option *opt __maybe_unused,
if (!str) if (!str)
return 0; return 0;
if (params.show_lines) { if (params.command == 'L') {
pr_warning("Warning: more than one --line options are" pr_warning("Warning: more than one --line options are"
" detected. Only the first one is valid.\n"); " detected. Only the first one is valid.\n");
return 0; return 0;
} }
params.show_lines = true; params.command = opt->short_name;
ret = parse_line_range_desc(str, &params.line_range); ret = parse_line_range_desc(str, &params.line_range);
return ret; return ret;
} }
static int opt_show_vars(const struct option *opt __maybe_unused, static int opt_show_vars(const struct option *opt,
const char *str, int unset __maybe_unused) const char *str, int unset __maybe_unused)
{ {
struct perf_probe_event *pev = &params.events[params.nevents]; struct perf_probe_event *pev = &params.events[params.nevents];
...@@ -285,16 +250,27 @@ static int opt_show_vars(const struct option *opt __maybe_unused, ...@@ -285,16 +250,27 @@ static int opt_show_vars(const struct option *opt __maybe_unused,
pr_err(" Error: '--vars' doesn't accept arguments.\n"); pr_err(" Error: '--vars' doesn't accept arguments.\n");
return -EINVAL; return -EINVAL;
} }
params.show_vars = true; params.command = opt->short_name;
return ret; return ret;
} }
#endif #endif
static int opt_show_funcs(const struct option *opt __maybe_unused, static int opt_add_probe_event(const struct option *opt,
const char *str, int unset) const char *str, int unset __maybe_unused)
{
if (str) {
params.command = opt->short_name;
return parse_probe_event(str);
}
return 0;
}
static int opt_set_filter_with_command(const struct option *opt,
const char *str, int unset)
{ {
if (!unset) if (!unset)
params.show_funcs = true; params.command = opt->short_name;
if (str) if (str)
return params_add_filter(str); return params_add_filter(str);
...@@ -360,10 +336,10 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -360,10 +336,10 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
OPT_BOOLEAN('q', "quiet", &params.quiet, OPT_BOOLEAN('q', "quiet", &params.quiet,
"be quiet (do not show any mesages)"), "be quiet (do not show any mesages)"),
OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT", OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT",
"list up probe events", opt_list_probe_event, "list up probe events",
DEFAULT_LIST_FILTER), opt_set_filter_with_command, DEFAULT_LIST_FILTER),
OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.", OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
opt_del_probe_event), opt_set_filter_with_command),
OPT_CALLBACK('a', "add", NULL, OPT_CALLBACK('a', "add", NULL,
#ifdef HAVE_DWARF_SUPPORT #ifdef HAVE_DWARF_SUPPORT
"[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT" "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
...@@ -412,7 +388,7 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -412,7 +388,7 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
"Set how many probe points can be found for a probe."), "Set how many probe points can be found for a probe."),
OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]", OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
"Show potential probe-able functions.", "Show potential probe-able functions.",
opt_show_funcs, DEFAULT_FUNC_FILTER), opt_set_filter_with_command, DEFAULT_FUNC_FILTER),
OPT_CALLBACK('\0', "filter", NULL, OPT_CALLBACK('\0', "filter", NULL,
"[!]FILTER", "Set a filter (with --vars/funcs only)\n" "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
"\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n" "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
...@@ -444,11 +420,16 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -444,11 +420,16 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
pr_warning(" Error: '-' is not supported.\n"); pr_warning(" Error: '-' is not supported.\n");
usage_with_options(probe_usage, options); usage_with_options(probe_usage, options);
} }
if (params.command && params.command != 'a') {
pr_warning(" Error: another command except --add is set.\n");
usage_with_options(probe_usage, options);
}
ret = parse_probe_event_argv(argc, argv); ret = parse_probe_event_argv(argc, argv);
if (ret < 0) { if (ret < 0) {
pr_err_with_code(" Error: Command Parse Error.", ret); pr_err_with_code(" Error: Command Parse Error.", ret);
return ret; return ret;
} }
params.command = 'a';
} }
if (params.quiet) { if (params.quiet) {
...@@ -462,16 +443,13 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -462,16 +443,13 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
if (params.max_probe_points == 0) if (params.max_probe_points == 0)
params.max_probe_points = MAX_PROBES; params.max_probe_points = MAX_PROBES;
if ((!params.nevents && !params.del_events && !params.list_events &&
!params.show_lines && !params.show_funcs))
usage_with_options(probe_usage, options);
/* /*
* Only consider the user's kernel image path if given. * Only consider the user's kernel image path if given.
*/ */
symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
if (params.list_events) { switch (params.command) {
case 'l':
if (params.uprobes) { if (params.uprobes) {
pr_warning(" Error: Don't use --list with --exec.\n"); pr_warning(" Error: Don't use --list with --exec.\n");
usage_with_options(probe_usage, options); usage_with_options(probe_usage, options);
...@@ -480,24 +458,20 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -480,24 +458,20 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
if (ret < 0) if (ret < 0)
pr_err_with_code(" Error: Failed to show event list.", ret); pr_err_with_code(" Error: Failed to show event list.", ret);
return ret; return ret;
} case 'F':
if (params.show_funcs) {
ret = show_available_funcs(params.target, params.filter, ret = show_available_funcs(params.target, params.filter,
params.uprobes); params.uprobes);
if (ret < 0) if (ret < 0)
pr_err_with_code(" Error: Failed to show functions.", ret); pr_err_with_code(" Error: Failed to show functions.", ret);
return ret; return ret;
}
#ifdef HAVE_DWARF_SUPPORT #ifdef HAVE_DWARF_SUPPORT
if (params.show_lines) { case 'L':
ret = show_line_range(&params.line_range, params.target, ret = show_line_range(&params.line_range, params.target,
params.uprobes); params.uprobes);
if (ret < 0) if (ret < 0)
pr_err_with_code(" Error: Failed to show lines.", ret); pr_err_with_code(" Error: Failed to show lines.", ret);
return ret; return ret;
} case 'V':
if (params.show_vars) {
if (!params.filter) if (!params.filter)
params.filter = strfilter__new(DEFAULT_VAR_FILTER, params.filter = strfilter__new(DEFAULT_VAR_FILTER,
NULL); NULL);
...@@ -510,18 +484,15 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -510,18 +484,15 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
if (ret < 0) if (ret < 0)
pr_err_with_code(" Error: Failed to show vars.", ret); pr_err_with_code(" Error: Failed to show vars.", ret);
return ret; return ret;
}
#endif #endif
case 'd':
if (params.del_events) {
ret = del_perf_probe_events(params.filter); ret = del_perf_probe_events(params.filter);
if (ret < 0) { if (ret < 0) {
pr_err_with_code(" Error: Failed to delete events.", ret); pr_err_with_code(" Error: Failed to delete events.", ret);
return ret; return ret;
} }
} break;
case 'a':
if (params.nevents) {
/* Ensure the last given target is used */ /* Ensure the last given target is used */
if (params.target && !params.target_used) { if (params.target && !params.target_used) {
pr_warning(" Error: -x/-m must follow the probe definitions.\n"); pr_warning(" Error: -x/-m must follow the probe definitions.\n");
...@@ -535,6 +506,9 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -535,6 +506,9 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
pr_err_with_code(" Error: Failed to add events.", ret); pr_err_with_code(" Error: Failed to add events.", ret);
return ret; return ret;
} }
break;
default:
usage_with_options(probe_usage, options);
} }
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment