tracing/dynevent: Delegate parsing to create function

Delegate command parsing to each create function so that the
command syntax can be customized.

This requires changes to the kprobe/uprobe/synthetic event handling,
which are also included here.

Link: https://lkml.kernel.org/r/e488726f49cbdbc01568618f8680584306c4c79f.1612208610.git.zanussi@kernel.org

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
[ zanussi@kernel.org: added synthetic event modifications ]
Signed-off-by: Tom Zanussi <zanussi@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
This commit is contained in:
Masami Hiramatsu 2021-02-01 13:48:11 -06:00 committed by Steven Rostedt (VMware)
parent 33b1d14668
commit d262271d04
9 changed files with 120 additions and 73 deletions

View File

@ -9412,30 +9412,11 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
} }
EXPORT_SYMBOL_GPL(ftrace_dump); EXPORT_SYMBOL_GPL(ftrace_dump);
int trace_run_command(const char *buf, int (*createfn)(int, char **))
{
char **argv;
int argc, ret;
argc = 0;
ret = 0;
argv = argv_split(GFP_KERNEL, buf, &argc);
if (!argv)
return -ENOMEM;
if (argc)
ret = createfn(argc, argv);
argv_free(argv);
return ret;
}
#define WRITE_BUFSIZE 4096 #define WRITE_BUFSIZE 4096
ssize_t trace_parse_run_command(struct file *file, const char __user *buffer, ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos, size_t count, loff_t *ppos,
int (*createfn)(int, char **)) int (*createfn)(const char *))
{ {
char *kbuf, *buf, *tmp; char *kbuf, *buf, *tmp;
int ret = 0; int ret = 0;
@ -9483,7 +9464,7 @@ ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
if (tmp) if (tmp)
*tmp = '\0'; *tmp = '\0';
ret = trace_run_command(buf, createfn); ret = createfn(buf);
if (ret) if (ret)
goto out; goto out;
buf += size; buf += size;

View File

@ -1807,10 +1807,9 @@ extern int tracing_set_cpumask(struct trace_array *tr,
#define MAX_EVENT_NAME_LEN 64 #define MAX_EVENT_NAME_LEN 64
extern int trace_run_command(const char *buf, int (*createfn)(int, char**));
extern ssize_t trace_parse_run_command(struct file *file, extern ssize_t trace_parse_run_command(struct file *file,
const char __user *buffer, size_t count, loff_t *ppos, const char __user *buffer, size_t count, loff_t *ppos,
int (*createfn)(int, char**)); int (*createfn)(const char *));
extern unsigned int err_pos(char *cmd, const char *str); extern unsigned int err_pos(char *cmd, const char *str);
extern void tracing_log_err(struct trace_array *tr, extern void tracing_log_err(struct trace_array *tr,

View File

@ -31,23 +31,31 @@ int dyn_event_register(struct dyn_event_operations *ops)
return 0; return 0;
} }
int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type) int dyn_event_release(const char *raw_command, struct dyn_event_operations *type)
{ {
struct dyn_event *pos, *n; struct dyn_event *pos, *n;
char *system = NULL, *event, *p; char *system = NULL, *event, *p;
int ret = -ENOENT; int argc, ret = -ENOENT;
char **argv;
argv = argv_split(GFP_KERNEL, raw_command, &argc);
if (!argv)
return -ENOMEM;
if (argv[0][0] == '-') { if (argv[0][0] == '-') {
if (argv[0][1] != ':') if (argv[0][1] != ':') {
return -EINVAL; ret = -EINVAL;
goto out;
}
event = &argv[0][2]; event = &argv[0][2];
} else { } else {
event = strchr(argv[0], ':'); event = strchr(argv[0], ':');
if (!event) if (!event) {
return -EINVAL; ret = -EINVAL;
goto out;
}
event++; event++;
} }
argc--; argv++;
p = strchr(event, '/'); p = strchr(event, '/');
if (p) { if (p) {
@ -63,7 +71,7 @@ int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type)
if (type && type != pos->ops) if (type && type != pos->ops)
continue; continue;
if (!pos->ops->match(system, event, if (!pos->ops->match(system, event,
argc, (const char **)argv, pos)) argc - 1, (const char **)argv + 1, pos))
continue; continue;
ret = pos->ops->free(pos); ret = pos->ops->free(pos);
@ -71,21 +79,22 @@ int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type)
break; break;
} }
mutex_unlock(&event_mutex); mutex_unlock(&event_mutex);
out:
argv_free(argv);
return ret; return ret;
} }
static int create_dyn_event(int argc, char **argv) static int create_dyn_event(const char *raw_command)
{ {
struct dyn_event_operations *ops; struct dyn_event_operations *ops;
int ret = -ENODEV; int ret = -ENODEV;
if (argv[0][0] == '-' || argv[0][0] == '!') if (raw_command[0] == '-' || raw_command[0] == '!')
return dyn_event_release(argc, argv, NULL); return dyn_event_release(raw_command, NULL);
mutex_lock(&dyn_event_ops_mutex); mutex_lock(&dyn_event_ops_mutex);
list_for_each_entry(ops, &dyn_event_ops_list, list) { list_for_each_entry(ops, &dyn_event_ops_list, list) {
ret = ops->create(argc, (const char **)argv); ret = ops->create(raw_command);
if (!ret || ret != -ECANCELED) if (!ret || ret != -ECANCELED)
break; break;
} }

View File

@ -39,7 +39,7 @@ struct dyn_event;
*/ */
struct dyn_event_operations { struct dyn_event_operations {
struct list_head list; struct list_head list;
int (*create)(int argc, const char *argv[]); int (*create)(const char *raw_command);
int (*show)(struct seq_file *m, struct dyn_event *ev); int (*show)(struct seq_file *m, struct dyn_event *ev);
bool (*is_busy)(struct dyn_event *ev); bool (*is_busy)(struct dyn_event *ev);
int (*free)(struct dyn_event *ev); int (*free)(struct dyn_event *ev);
@ -97,7 +97,7 @@ void *dyn_event_seq_start(struct seq_file *m, loff_t *pos);
void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos); void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos);
void dyn_event_seq_stop(struct seq_file *m, void *v); void dyn_event_seq_stop(struct seq_file *m, void *v);
int dyn_events_release_all(struct dyn_event_operations *type); int dyn_events_release_all(struct dyn_event_operations *type);
int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type); int dyn_event_release(const char *raw_command, struct dyn_event_operations *type);
/* /*
* for_each_dyn_event - iterate over the dyn_event list * for_each_dyn_event - iterate over the dyn_event list

View File

@ -62,7 +62,7 @@ static void synth_err(u8 err_type, u8 err_pos)
err_type, err_pos); err_type, err_pos);
} }
static int create_synth_event(int argc, const char **argv); static int create_synth_event(const char *raw_command);
static int synth_event_show(struct seq_file *m, struct dyn_event *ev); static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
static int synth_event_release(struct dyn_event *ev); static int synth_event_release(struct dyn_event *ev);
static bool synth_event_is_busy(struct dyn_event *ev); static bool synth_event_is_busy(struct dyn_event *ev);
@ -1383,18 +1383,30 @@ int synth_event_delete(const char *event_name)
} }
EXPORT_SYMBOL_GPL(synth_event_delete); EXPORT_SYMBOL_GPL(synth_event_delete);
static int create_or_delete_synth_event(int argc, char **argv) static int create_or_delete_synth_event(const char *raw_command)
{ {
const char *name = argv[0]; char **argv, *name = NULL;
int ret; int argc = 0, ret = 0;
argv = argv_split(GFP_KERNEL, raw_command, &argc);
if (!argv)
return -ENOMEM;
if (!argc)
goto free;
name = argv[0];
/* trace_run_command() ensures argc != 0 */ /* trace_run_command() ensures argc != 0 */
if (name[0] == '!') { if (name[0] == '!') {
ret = synth_event_delete(name + 1); ret = synth_event_delete(name + 1);
return ret; goto free;
} }
ret = __create_synth_event(argc - 1, name, (const char **)argv + 1); ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
free:
argv_free(argv);
return ret == -ECANCELED ? -EINVAL : ret; return ret == -ECANCELED ? -EINVAL : ret;
} }
@ -1403,7 +1415,7 @@ static int synth_event_run_command(struct dynevent_cmd *cmd)
struct synth_event *se; struct synth_event *se;
int ret; int ret;
ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event); ret = create_or_delete_synth_event(cmd->seq.buffer);
if (ret) if (ret)
return ret; return ret;
@ -1939,23 +1951,43 @@ int synth_event_trace_end(struct synth_event_trace_state *trace_state)
} }
EXPORT_SYMBOL_GPL(synth_event_trace_end); EXPORT_SYMBOL_GPL(synth_event_trace_end);
static int create_synth_event(int argc, const char **argv) static int create_synth_event(const char *raw_command)
{ {
const char *name = argv[0]; char **argv, *name;
int len; int len, argc = 0, ret = 0;
if (name[0] != 's' || name[1] != ':') argv = argv_split(GFP_KERNEL, raw_command, &argc);
return -ECANCELED; if (!argv) {
ret = -ENOMEM;
return ret;
}
if (!argc)
goto free;
name = argv[0];
if (name[0] != 's' || name[1] != ':') {
ret = -ECANCELED;
goto free;
}
name += 2; name += 2;
/* This interface accepts group name prefix */ /* This interface accepts group name prefix */
if (strchr(name, '/')) { if (strchr(name, '/')) {
len = str_has_prefix(name, SYNTH_SYSTEM "/"); len = str_has_prefix(name, SYNTH_SYSTEM "/");
if (len == 0) if (len == 0) {
return -EINVAL; ret = -EINVAL;
goto free;
}
name += len; name += len;
} }
return __create_synth_event(argc - 1, name, argv + 1);
ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
free:
argv_free(argv);
return ret;
} }
static int synth_event_release(struct dyn_event *ev) static int synth_event_release(struct dyn_event *ev)

View File

@ -35,7 +35,7 @@ static int __init set_kprobe_boot_events(char *str)
} }
__setup("kprobe_event=", set_kprobe_boot_events); __setup("kprobe_event=", set_kprobe_boot_events);
static int trace_kprobe_create(int argc, const char **argv); static int trace_kprobe_create(const char *raw_command);
static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev); static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
static int trace_kprobe_release(struct dyn_event *ev); static int trace_kprobe_release(struct dyn_event *ev);
static bool trace_kprobe_is_busy(struct dyn_event *ev); static bool trace_kprobe_is_busy(struct dyn_event *ev);
@ -711,7 +711,7 @@ static inline void sanitize_event_name(char *name)
*name = '_'; *name = '_';
} }
static int trace_kprobe_create(int argc, const char *argv[]) static int __trace_kprobe_create(int argc, const char *argv[])
{ {
/* /*
* Argument syntax: * Argument syntax:
@ -910,20 +910,25 @@ error:
goto out; goto out;
} }
static int create_or_delete_trace_kprobe(int argc, char **argv) static int trace_kprobe_create(const char *raw_command)
{
return trace_probe_create(raw_command, __trace_kprobe_create);
}
static int create_or_delete_trace_kprobe(const char *raw_command)
{ {
int ret; int ret;
if (argv[0][0] == '-') if (raw_command[0] == '-')
return dyn_event_release(argc, argv, &trace_kprobe_ops); return dyn_event_release(raw_command, &trace_kprobe_ops);
ret = trace_kprobe_create(argc, (const char **)argv); ret = trace_kprobe_create(raw_command);
return ret == -ECANCELED ? -EINVAL : ret; return ret == -ECANCELED ? -EINVAL : ret;
} }
static int trace_kprobe_run_command(struct dynevent_cmd *cmd) static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
{ {
return trace_run_command(cmd->seq.buffer, create_or_delete_trace_kprobe); return create_or_delete_trace_kprobe(cmd->seq.buffer);
} }
/** /**
@ -1084,7 +1089,7 @@ int kprobe_event_delete(const char *name)
snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name); snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
return trace_run_command(buf, create_or_delete_trace_kprobe); return create_or_delete_trace_kprobe(buf);
} }
EXPORT_SYMBOL_GPL(kprobe_event_delete); EXPORT_SYMBOL_GPL(kprobe_event_delete);
@ -1886,7 +1891,7 @@ static __init void setup_boot_kprobe_events(void)
if (p) if (p)
*p++ = '\0'; *p++ = '\0';
ret = trace_run_command(cmd, create_or_delete_trace_kprobe); ret = create_or_delete_trace_kprobe(cmd);
if (ret) if (ret)
pr_warn("Failed to add event(%d): %s\n", ret, cmd); pr_warn("Failed to add event(%d): %s\n", ret, cmd);
@ -1980,8 +1985,7 @@ static __init int kprobe_trace_self_tests_init(void)
pr_info("Testing kprobe tracing: "); pr_info("Testing kprobe tracing: ");
ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)", ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)");
create_or_delete_trace_kprobe);
if (WARN_ON_ONCE(ret)) { if (WARN_ON_ONCE(ret)) {
pr_warn("error on probing function entry.\n"); pr_warn("error on probing function entry.\n");
warn++; warn++;
@ -2002,8 +2006,7 @@ static __init int kprobe_trace_self_tests_init(void)
} }
} }
ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval", ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval");
create_or_delete_trace_kprobe);
if (WARN_ON_ONCE(ret)) { if (WARN_ON_ONCE(ret)) {
pr_warn("error on probing function return.\n"); pr_warn("error on probing function return.\n");
warn++; warn++;
@ -2076,13 +2079,13 @@ static __init int kprobe_trace_self_tests_init(void)
trace_probe_event_call(&tk->tp), file); trace_probe_event_call(&tk->tp), file);
} }
ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe); ret = create_or_delete_trace_kprobe("-:testprobe");
if (WARN_ON_ONCE(ret)) { if (WARN_ON_ONCE(ret)) {
pr_warn("error on deleting a probe.\n"); pr_warn("error on deleting a probe.\n");
warn++; warn++;
} }
ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe); ret = create_or_delete_trace_kprobe("-:testprobe2");
if (WARN_ON_ONCE(ret)) { if (WARN_ON_ONCE(ret)) {
pr_warn("error on deleting a probe.\n"); pr_warn("error on deleting a probe.\n");
warn++; warn++;

View File

@ -1134,3 +1134,20 @@ bool trace_probe_match_command_args(struct trace_probe *tp,
} }
return true; return true;
} }
int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
{
int argc = 0, ret = 0;
char **argv;
argv = argv_split(GFP_KERNEL, raw_command, &argc);
if (!argv)
return -ENOMEM;
if (argc)
ret = createfn(argc, (const char **)argv);
argv_free(argv);
return ret;
}

View File

@ -341,6 +341,7 @@ struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b); int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b);
bool trace_probe_match_command_args(struct trace_probe *tp, bool trace_probe_match_command_args(struct trace_probe *tp,
int argc, const char **argv); int argc, const char **argv);
int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **));
#define trace_probe_for_each_link(pos, tp) \ #define trace_probe_for_each_link(pos, tp) \
list_for_each_entry(pos, &(tp)->event->files, list) list_for_each_entry(pos, &(tp)->event->files, list)

View File

@ -34,7 +34,7 @@ struct uprobe_trace_entry_head {
#define DATAOF_TRACE_ENTRY(entry, is_return) \ #define DATAOF_TRACE_ENTRY(entry, is_return) \
((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
static int trace_uprobe_create(int argc, const char **argv); static int trace_uprobe_create(const char *raw_command);
static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev); static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
static int trace_uprobe_release(struct dyn_event *ev); static int trace_uprobe_release(struct dyn_event *ev);
static bool trace_uprobe_is_busy(struct dyn_event *ev); static bool trace_uprobe_is_busy(struct dyn_event *ev);
@ -530,7 +530,7 @@ end:
* Argument syntax: * Argument syntax:
* - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET[%return][(REF)] [FETCHARGS] * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET[%return][(REF)] [FETCHARGS]
*/ */
static int trace_uprobe_create(int argc, const char **argv) static int __trace_uprobe_create(int argc, const char **argv)
{ {
struct trace_uprobe *tu; struct trace_uprobe *tu;
const char *event = NULL, *group = UPROBE_EVENT_SYSTEM; const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
@ -716,14 +716,19 @@ fail_address_parse:
return ret; return ret;
} }
static int create_or_delete_trace_uprobe(int argc, char **argv) int trace_uprobe_create(const char *raw_command)
{
return trace_probe_create(raw_command, __trace_uprobe_create);
}
static int create_or_delete_trace_uprobe(const char *raw_command)
{ {
int ret; int ret;
if (argv[0][0] == '-') if (raw_command[0] == '-')
return dyn_event_release(argc, argv, &trace_uprobe_ops); return dyn_event_release(raw_command, &trace_uprobe_ops);
ret = trace_uprobe_create(argc, (const char **)argv); ret = trace_uprobe_create(raw_command);
return ret == -ECANCELED ? -EINVAL : ret; return ret == -ECANCELED ? -EINVAL : ret;
} }