Re: [PATCH v2 1/4] tracing/dynevent: Delegate parsing to create function

From: Masami Hiramatsu
Date: Sat Oct 24 2020 - 04:50:47 EST


Hi Tom,

Thanks for the update!

On Fri, 23 Oct 2020 15:33:52 -0500
Tom Zanussi <zanussi@xxxxxxxxxx> wrote:

> From: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
>
> Delegate command parsing to each create function so that the
> command syntax can be customized.
>
> Signed-off-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
> [ zanussi@xxxxxxxxxx: added synthetic event modifications ]

Since you've customized the synth_event parser, could you update
the patch description? (Or split this into 2 patches)

> Signed-off-by: Tom Zanussi <zanussi@xxxxxxxxxx>
> ---
> kernel/trace/trace.c | 23 +---
> kernel/trace/trace.h | 3 +-
> kernel/trace/trace_dynevent.c | 35 +++---
> kernel/trace/trace_dynevent.h | 4 +-
> kernel/trace/trace_events_synth.c | 186 ++++++++++++++++--------------
> kernel/trace/trace_kprobe.c | 33 +++---
> kernel/trace/trace_probe.c | 17 +++
> kernel/trace/trace_probe.h | 1 +
> kernel/trace/trace_uprobe.c | 17 ++-
> 9 files changed, 174 insertions(+), 145 deletions(-)

[..]
> @@ -1223,26 +1176,43 @@ static int __create_synth_event(int argc, const char *name, const char **argv)
> goto out;
> }
>
> - for (i = 0; i < argc - 1; i++) {
> - if (strcmp(argv[i], ";") == 0)
> - continue;
> + tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
> + if (!tmp_fields) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
> if (n_fields == SYNTH_FIELDS_MAX) {
> synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
> ret = -EINVAL;
> goto err;
> }
>
> - field = parse_synth_field(argc - i, &argv[i], &consumed);
> + argv = argv_split(GFP_KERNEL, field_str, &argc);
> + if (!argv) {
> + ret = -ENOMEM;
> + goto err;
> + }
> +
> + if (!argc)
> + continue;
> +
> + field = parse_synth_field(argc, argv);
> if (IS_ERR(field)) {
> + argv_free(argv);
> ret = PTR_ERR(field);
> goto err;
> }
> +
> + argv_free(argv);
> +
> fields[n_fields++] = field;
> i += consumed - 1;

You may not need this line (and "consumed") anymore?

> }
>
> - if (i < argc && strcmp(argv[i], ";") != 0) {
> - synth_err(SYNTH_ERR_INVALID_FIELD, errpos(argv[i]));
> + if (n_fields == 0) {
> + synth_err(SYNTH_ERR_CMD_INCOMPLETE, 0);
> ret = -EINVAL;
> goto err;
> }
> @@ -1261,6 +1231,8 @@ static int __create_synth_event(int argc, const char *name, const char **argv)
> out:
> mutex_unlock(&event_mutex);
>
> + kfree(saved_fields);
> +
> return ret;
> err:
> for (i = 0; i < n_fields; i++)
> @@ -1378,18 +1350,35 @@ int synth_event_delete(const char *event_name)
> }
> 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];
> - int ret;
> + char **argv, *name = NULL, *fields;
> + int argc = 0, ret = 0;
> +
> + last_cmd_set(raw_command);
> +
> + argv = argv_split(GFP_KERNEL, raw_command, &argc);
> + if (!argv)
> + return -ENOMEM;

If you are sure the first argument is the name, you don't need
to use argv_split, but just strpbrk(raw_command, " \t"), something
like this.

raw_command = skip_spaces(raw_command);
p = strpbrk(raw_command, " \t");
if (!p)
return -EINVAL;

name = kmemdup_nul(raw_command, p - raw_command, GFP_KERNEL);
field = skip_spaces(p);

...

ret = __create_synth_event(name, fields);
free:
kfree(name);

(BTW, we should have find_spaces() instead of slow strpbrk().)


Thank you,


--
Masami Hiramatsu <mhiramat@xxxxxxxxxx>