tracing: Add and use event_command parsing func helpers
v4 cover tracing: Add and use event_command parsing func helpers
With more event commands being implemented, it's been pointed out that it would make sense to clean up the existing ones and make it easier to implement new ones without copying a lot of boilerplate. The main culprit here is the event_command.func() callback - the rest of the event_command infrastructure has default implementations that work for most implementations. The func() callback is a little different in that every new command needs to customize parsing to some extent.
This patchset attempts to help clean that up and make it easier for new users to deal with.
v4: Added two patches changing the names of event_command.func() and
event_trigger_ops.func() to make them reflect their functions.
Added back missing kfree(trigger_data) in event_trigger_callback().
Changed char *param to const char *param in event_trigger_check_remove() and event_trigger_empty_param().
Changed event_trigger_separate_filter() to use separate param and filter outparams, and changed the name of the param inparam to param_and_filter to better reflect its contents and avoid the clash with new param outparam. Changed all parse() implementations to use this new scheme.
Fixed some typos and added more extensive comments with examples explaining various things that were mentioned as causing confusion and just in general tried to clarify things with respect to the callbacks and parameters.
v3: broke up event_trigger_check() into smaller functions instead of
parameterizing it, and added function documentation.
v2: removed unused event_trigger_remove(). No change in functionality.
The following changes since commit a6ed2aee54644cfa2d04ca86308767f5c3a087e8:
tracing: Switch to kvfree_rcu() API (2021-12-06 17:53:50 -0500)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/zanussi/linux-trace.git ftrace/cleanup-hist-func-v4
Tom Zanussi (4):
tracing: Change event_command func() to parse()
tracing: Change event_trigger_ops func() to trigger()
tracing: Add helper functions to simplify event_command.parse()
callback handling
tracing: Have existing event_command.parse() implementations use
helpers
kernel/trace/trace.h | 63 +++- kernel/trace/trace_eprobe.c | 11 +- kernel/trace/trace_events_hist.c | 109 +++--- kernel/trace/trace_events_trigger.c | 558 ++++++++++++++++++++-------- 4 files changed, 515 insertions(+), 226 deletions(-)
Signature · 1 line
2.17.1
PATCH v4 1/4 tracing: Change event_command func() to parse()
The name of the func() callback on event_command is too generic and is easily confused with other callbacks with that name, so change it to something that reflects its actual purpose.
In this case, the main purpose of the callback is to parse an event command, so call it parse() instead.
--- kernel/trace/trace.h | 19 +++++++++++------- kernel/trace/trace_eprobe.c | 8 ++++---- kernel/trace/trace_events_hist.c | 26 ++++++++++++------------- kernel/trace/trace_events_trigger.c | 30 ++++++++++++++--------------- 4 files changed, 44 insertions(+), 39 deletions(-)
kernel/trace/trace.h +12 -7
@@ -1578,9 +1578,9 @@ extern int event_enable_trigger_print(struct seq_file *m, struct event_trigger_data *data);extern void event_enable_trigger_free (struct event_trigger_ops *ops,struct event_trigger_data *data); -extern int event_enable_trigger_func (struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *param); +extern int event_enable_trigger_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd,char *param); extern int event_enable_register_trigger(char *glob, struct event_trigger_ops *ops, struct event_trigger_data *data,
@@ -1702,7 +1702,7 @@ struct event_trigger_ops {* All the methods below, except for @set_filter() and @unreg_all(), * must be implemented. * -* @func: The callback function responsible for parsing and +* @parse: The callback function responsible for parsing and * registering the trigger written to the 'trigger' file by the * user. It allocates the trigger instance and registers it with * the appropriate trace event. It makes use of the other
@@ -1737,15 +1737,20 @@ struct event_trigger_ops {* * @get_trigger_ops: The callback function invoked to retrieve the * event_trigger_ops implementation associated with the command. +* This callback function allows a single event_command to +* support multiple trigger implementations via different sets of +* event_trigger_ops, depending on the value of the @param +* string. */ struct event_command {struct list_head list;char *name;enum event_trigger_type trigger_type;int flags; -int (*func)(struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *params); +int (*parse)(struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd, +char *param_and_filter); int (*reg)(char *glob, struct event_trigger_ops *ops, struct event_trigger_data *data,
kernel/trace/trace_eprobe.c +4 -4
@@ -549,9 +549,9 @@ static struct event_trigger_ops eprobe_trigger_ops = { .free = eprobe_trigger_free, }; -static int eprobe_trigger_cmd_func (struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *param) +static int eprobe_trigger_cmd_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd,char *param) {return -1 ; }
@@ -580,7 +580,7 @@ static struct event_command event_trigger_cmd = { .name = "eprobe", .trigger_type = ETT_EVENT_EPROBE, .flags = EVENT_CMD_FL_NEEDS_REC, - .func = eprobe_trigger_cmd_func, + .parse = eprobe_trigger_cmd_parse, .reg = eprobe_trigger_reg_func, .unreg = eprobe_trigger_unreg_func, .unreg_all = NULL,
kernel/trace/trace_events_hist.c +13 -13
@@ -2761,9 +2761,9 @@ static char *find_trigger_filter(struct hist_trigger_data *hist_data, }static struct event_command trigger_hist_cmd; -static int event_hist_trigger_func (struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *param); +static int event_hist_trigger_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd,char *param); static bool compatible_keys(struct hist_trigger_data *target_hist_data, struct hist_trigger_data *hist_data,
@@ -2966,8 +2966,8 @@ create_field_var_hist(struct hist_trigger_data *target_hist_data, var_hist->hist_data = hist_data;/* Create the new histogram with our variable */ - ret =event_hist_trigger_func (&trigger_hist_cmd, file, -"" ,"hist" , cmd); + ret =event_hist_trigger_parse (&trigger_hist_cmd, file, +"" ,"hist" , cmd);if (ret) {kfree (cmd);kfree (var_hist->cmd);
@@ -5729,8 +5729,8 @@ static void unregister_field_var_hists(struct hist_trigger_data *hist_data)for (i =0 ; i < hist_data->n_field_var_hists; i++) { file = hist_data->field_var_hists[i]->hist_data->event_file; cmd = hist_data->field_var_hists[i]->cmd; - ret =event_hist_trigger_func (&trigger_hist_cmd, file, -"!hist" ,"hist" , cmd); + ret =event_hist_trigger_parse (&trigger_hist_cmd, file, +"!hist" ,"hist" , cmd);WARN_ON_ONCE (ret <0 ); } }
@@ -6146,9 +6146,9 @@ static void hist_unreg_all(struct trace_event_file *file) } } -static int event_hist_trigger_func (struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *param) +static int event_hist_trigger_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd,char *param) {unsigned int hist_trigger_bits =TRACING_MAP_BITS_DEFAULT ;struct event_trigger_data *trigger_data;
@@ -6331,7 +6331,7 @@ static struct event_command trigger_hist_cmd = { .name = "hist", .trigger_type = ETT_EVENT_HIST, .flags = EVENT_CMD_FL_NEEDS_REC, - .func = event_hist_trigger_func, + .parse = event_hist_trigger_parse, .reg = hist_register_trigger, .unreg = hist_unregister_trigger, .unreg_all = hist_unreg_all,
@@ -6446,7 +6446,7 @@ static void hist_enable_unreg_all(struct trace_event_file *file)static struct event_command trigger_hist_enable_cmd = { .name =ENABLE_HIST_STR , .trigger_type =ETT_HIST_ENABLE , - .func = event_enable_trigger_func, + .parse = event_enable_trigger_parse, .reg = event_enable_register_trigger, .unreg = event_enable_unregister_trigger, .unreg_all = hist_enable_unreg_all,
@@ -6457,7 +6457,7 @@ static struct event_command trigger_hist_enable_cmd = {static struct event_command trigger_hist_disable_cmd = { .name =DISABLE_HIST_STR , .trigger_type =ETT_HIST_ENABLE , - .func = event_enable_trigger_func, + .parse = event_enable_trigger_parse, .reg = event_enable_register_trigger, .unreg = event_enable_unregister_trigger, .unreg_all = hist_enable_unreg_all,
kernel/trace/trace_events_trigger.c +15 -15
@@ -245,7 +245,7 @@ int trigger_process_regex(struct trace_event_file *file, char *buff)mutex_lock (&trigger_cmd_mutex);list_for_each_entry (p, &trigger_commands, list) {if (strcmp (p->name, command) ==0 ) { - ret = p->func (p, file, buff, command, next); + ret = p->parse (p, file, buff, command, next); goto out_unlock; } }
@@ -622,7 +622,7 @@ static void unregister_trigger(char *glob, struct event_trigger_ops *ops, }/** -* event_trigger_callback - Generic event_command @func implementation +* event_trigger_parse - Generic event_command @parse implementation * @cmd_ops: The command ops, used for trigger registration * @file: The trace_event_file associated with the event * @glob: The raw string used to register the trigger
@@ -632,15 +632,15 @@ static void unregister_trigger(char *glob, struct event_trigger_ops *ops,* Common implementation for event command parsing and trigger * instantiation. * -* Usually used directly as the @func method in event command +* Usually used directly as the @parse method in event command * implementations. * * Return: 0 on success, errno otherwise */ static int -event_trigger_callback (struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *param) +event_trigger_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd,char *param) {struct event_trigger_data *trigger_data;struct event_trigger_ops *trigger_ops;
@@ -1069,7 +1069,7 @@ onoff_get_trigger_ops(char *cmd, char *param)static struct event_command trigger_traceon_cmd = { .name ="traceon" , .trigger_type =ETT_TRACE_ONOFF , - .func = event_trigger_callback, + .parse = event_trigger_parse, .reg = register_trigger, .unreg = unregister_trigger, .get_trigger_ops = onoff_get_trigger_ops,
@@ -1080,7 +1080,7 @@ static struct event_command trigger_traceoff_cmd = { .name = "traceoff", .trigger_type = ETT_TRACE_ONOFF, .flags = EVENT_CMD_FL_POST_TRIGGER, - .func = event_trigger_callback, + .parse = event_trigger_parse, .reg = register_trigger, .unreg = unregister_trigger, .get_trigger_ops = onoff_get_trigger_ops,
@@ -1157,7 +1157,7 @@ snapshot_get_trigger_ops(char *cmd, char *param)static struct event_command trigger_snapshot_cmd = { .name ="snapshot" , .trigger_type =ETT_SNAPSHOT , - .func = event_trigger_callback, + .parse = event_trigger_parse, .reg = register_snapshot_trigger, .unreg = unregister_trigger, .get_trigger_ops = snapshot_get_trigger_ops,
@@ -1249,7 +1249,7 @@ static struct event_command trigger_stacktrace_cmd = { .name = "stacktrace", .trigger_type = ETT_STACKTRACE, .flags = EVENT_CMD_FL_POST_TRIGGER, - .func = event_trigger_callback, + .parse = event_trigger_parse, .reg = register_trigger, .unreg = unregister_trigger, .get_trigger_ops = stacktrace_get_trigger_ops,
@@ -1380,9 +1380,9 @@ static struct event_trigger_ops event_disable_count_trigger_ops = { .free = event_enable_trigger_free, }; -int event_enable_trigger_func (struct event_command *cmd_ops, -struct trace_event_file *file, -char *glob,char *cmd,char *param) +int event_enable_trigger_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd,char *param) {struct trace_event_file *event_enable_file;struct enable_trigger_data *enable_data;
@@ -1628,7 +1628,7 @@ event_enable_get_trigger_ops(char *cmd, char *param)static struct event_command trigger_enable_cmd = { .name =ENABLE_EVENT_STR , .trigger_type =ETT_EVENT_ENABLE , - .func = event_enable_trigger_func, + .parse = event_enable_trigger_parse, .reg = event_enable_register_trigger, .unreg = event_enable_unregister_trigger, .get_trigger_ops = event_enable_get_trigger_ops,
@@ -1638,7 +1638,7 @@ static struct event_command trigger_enable_cmd = {static struct event_command trigger_disable_cmd = { .name =DISABLE_EVENT_STR , .trigger_type =ETT_EVENT_ENABLE , - .func = event_enable_trigger_func, + .parse = event_enable_trigger_parse, .reg = event_enable_register_trigger, .unreg = event_enable_unregister_trigger, .get_trigger_ops = event_enable_get_trigger_ops,
Signature · 1 line
2.17.1
PATCH v4 3/4 tracing: Add helper functions to simplify event_command.parse() callback handling
In reply to Tom ZanussiThe event_command.parse() callback is responsible for parsing and registering triggers. The existing command implementions for this callback duplicate a lot of the same code, so to clean up and consolidate those implementations, introduce a handful of helper functions for implementors to use.
This also makes it easier for new commands to be implemented and allows them to focus more on the customizations they provide rather than obscuring and complicating it with boilerplate code.
--- kernel/trace/trace.h | 24 ++ kernel/trace/trace_events_trigger.c | 344 ++++++++++++++++++++++++++++ 2 files changed, 368 insertions(+)
kernel/trace/trace.h +24 -0
@@ -1612,6 +1612,30 @@ get_named_trigger_data(struct event_trigger_data *data);extern int register_event_command (struct event_command *cmd);extern int unregister_event_command (struct event_command *cmd);extern int register_trigger_hist_enable_disable_cmds (void ); +extern bool event_trigger_check_remove (const char *glob); +extern bool event_trigger_empty_param (const char *param); +extern int event_trigger_separate_filter (char *param_and_filter,char **param, +char **filter,bool param_required); +extern struct event_trigger_data * +event_trigger_alloc (struct event_command *cmd_ops, +char *cmd, +char *param, +void *private_data); +extern int event_trigger_parse_num (char *trigger, +struct event_trigger_data *trigger_data); +extern int event_trigger_set_filter (struct event_command *cmd_ops, +struct trace_event_file *file, +char *param, +struct event_trigger_data *trigger_data); +extern void event_trigger_reset_filter (struct event_command *cmd_ops, +struct event_trigger_data *trigger_data); +extern int event_trigger_register (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob, +char *cmd, +char *trigger, +struct event_trigger_data *trigger_data, +int *n_registered);/** * struct event_trigger_ops - callbacks for trace event triggers
kernel/trace/trace_events_trigger.c +344 -0
350 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 2/4 tracing: Change event_trigger_ops func() to trigger()
In reply to Tom ZanussiThe name of the func() callback on event_trigger_ops is too generic and is easily confused with other callbacks with that name, so change it to something that reflects its actual purpose.
In this case, the main purpose of the callback is to implement an event trigger, so call it trigger() instead.
Also add some more documentation to event_trigger_ops describing the callbacks a bit better.
--- kernel/trace/trace.h | 19 ++++++++++++++---- kernel/trace/trace_eprobe.c | 2 +- kernel/trace/trace_events_hist.c | 12 ++++++------ kernel/trace/trace_events_trigger.c | 30 ++++++++++++++--------------- 4 files changed, 37 insertions(+), 26 deletions(-)
kernel/trace/trace.h +15 -4
@@ -1619,10 +1619,20 @@ extern int register_trigger_hist_enable_disable_cmds(void);* The methods in this structure provide per-event trigger hooks for * various trigger operations. * +* The @init and @free methods are used during trigger setup and +* teardown, typically called from an event_command's @parse() +* function implementation. +* +* The @print method is used to print the trigger spec. +* +* The @trigger method is the function that actually implements the +* trigger and is called in the context of the triggering event +* whenever that event occurs. +* * All the methods below, except for @init() and @free(), must be * implemented. * -* @func: The trigger 'probe' function called when the triggering +* @trigger: The trigger 'probe' function called when the triggering * event occurs. The data passed into this callback is the data * that was supplied to the event_command @reg() function that * registered the trigger (see struct event_command) along with
@@ -1651,9 +1661,10 @@ extern int register_trigger_hist_enable_disable_cmds(void);* (see trace_event_triggers.c). */ struct event_trigger_ops { -void (*func)(struct event_trigger_data *data, -struct trace_buffer *buffer,void *rec, -struct ring_buffer_event *rbe); +void (*trigger)(struct event_trigger_data *data, +struct trace_buffer *buffer, +void *rec, +struct ring_buffer_event *rbe);int (*init)(struct event_trigger_ops *ops,struct event_trigger_data *data); void (*free)(struct event_trigger_ops *ops,
kernel/trace/trace_eprobe.c +1 -1
@@ -543,7 +543,7 @@ static void eprobe_trigger_func(struct event_trigger_data *data, }static struct event_trigger_ops eprobe_trigger_ops = { - .func = eprobe_trigger_func, + .trigger = eprobe_trigger_func, .print = eprobe_trigger_print, .init = eprobe_trigger_init, .free = eprobe_trigger_free,
kernel/trace/trace_events_hist.c +6 -6
@@ -5759,7 +5759,7 @@ static void event_hist_trigger_free(struct event_trigger_ops *ops, }static struct event_trigger_ops event_hist_trigger_ops = { - .func = event_hist_trigger, + .trigger = event_hist_trigger, .print = event_hist_trigger_print, .init = event_hist_trigger_init, .free = event_hist_trigger_free,
@@ -5793,7 +5793,7 @@ static void event_hist_trigger_named_free(struct event_trigger_ops *ops, }static struct event_trigger_ops event_hist_trigger_named_ops = { - .func = event_hist_trigger, + .trigger = event_hist_trigger, .print = event_hist_trigger_print, .init = event_hist_trigger_named_init, .free = event_hist_trigger_named_free,
@@ -6383,28 +6383,28 @@ hist_enable_count_trigger(struct event_trigger_data *data, }static struct event_trigger_ops hist_enable_trigger_ops = { - .func = hist_enable_trigger, + .trigger = hist_enable_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free, };static struct event_trigger_ops hist_enable_count_trigger_ops = { - .func = hist_enable_count_trigger, + .trigger = hist_enable_count_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free, };static struct event_trigger_ops hist_disable_trigger_ops = { - .func = hist_enable_trigger, + .trigger = hist_enable_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free, };static struct event_trigger_ops hist_disable_count_trigger_ops = { - .func = hist_enable_count_trigger, + .trigger = hist_enable_count_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free,
kernel/trace/trace_events_trigger.c +15 -15
@@ -68,7 +68,7 @@ event_triggers_call(struct trace_event_file *file,if (data->paused)continue ;if (!rec) { - data->ops->func (data, buffer, rec, event); + data->ops->trigger (data, buffer, rec, event);continue ; } filter =rcu_dereference_sched (data->filter);
@@ -78,7 +78,7 @@ event_triggers_call(struct trace_event_file *file, tt |= data->cmd_ops->trigger_type;continue ; } - data->ops->func (data, buffer, rec, event); + data->ops->trigger (data, buffer, rec, event); }return tt; }
@@ -106,7 +106,7 @@ event_triggers_post_call(struct trace_event_file *file,if (data->paused)continue ;if (data->cmd_ops->trigger_type & tt) - data->ops->func (data,NULL ,NULL ,NULL ); + data->ops->trigger (data,NULL ,NULL ,NULL ); } }EXPORT_SYMBOL_GPL (event_triggers_post_call);
@@ -1023,28 +1023,28 @@ traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, }static struct event_trigger_ops traceon_trigger_ops = { - .func = traceon_trigger, + .trigger = traceon_trigger, .print = traceon_trigger_print, .init = event_trigger_init, .free = event_trigger_free, };static struct event_trigger_ops traceon_count_trigger_ops = { - .func = traceon_count_trigger, + .trigger = traceon_count_trigger, .print = traceon_trigger_print, .init = event_trigger_init, .free = event_trigger_free, };static struct event_trigger_ops traceoff_trigger_ops = { - .func = traceoff_trigger, + .trigger = traceoff_trigger, .print = traceoff_trigger_print, .init = event_trigger_init, .free = event_trigger_free, };static struct event_trigger_ops traceoff_count_trigger_ops = { - .func = traceoff_count_trigger, + .trigger = traceoff_count_trigger, .print = traceoff_trigger_print, .init = event_trigger_init, .free = event_trigger_free,
@@ -1135,14 +1135,14 @@ snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, }static struct event_trigger_ops snapshot_trigger_ops = { - .func = snapshot_trigger, + .trigger = snapshot_trigger, .print = snapshot_trigger_print, .init = event_trigger_init, .free = event_trigger_free, };static struct event_trigger_ops snapshot_count_trigger_ops = { - .func = snapshot_count_trigger, + .trigger = snapshot_count_trigger, .print = snapshot_trigger_print, .init = event_trigger_init, .free = event_trigger_free,
@@ -1226,14 +1226,14 @@ stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, }static struct event_trigger_ops stacktrace_trigger_ops = { - .func = stacktrace_trigger, + .trigger = stacktrace_trigger, .print = stacktrace_trigger_print, .init = event_trigger_init, .free = event_trigger_free, };static struct event_trigger_ops stacktrace_count_trigger_ops = { - .func = stacktrace_count_trigger, + .trigger = stacktrace_count_trigger, .print = stacktrace_trigger_print, .init = event_trigger_init, .free = event_trigger_free,
@@ -1353,28 +1353,28 @@ void event_enable_trigger_free(struct event_trigger_ops *ops, }static struct event_trigger_ops event_enable_trigger_ops = { - .func = event_enable_trigger, + .trigger = event_enable_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free, };static struct event_trigger_ops event_enable_count_trigger_ops = { - .func = event_enable_count_trigger, + .trigger = event_enable_count_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free, };static struct event_trigger_ops event_disable_trigger_ops = { - .func = event_enable_trigger, + .trigger = event_enable_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free, };static struct event_trigger_ops event_disable_count_trigger_ops = { - .func = event_enable_count_trigger, + .trigger = event_enable_count_trigger, .print = event_enable_trigger_print, .init = event_trigger_init, .free = event_enable_trigger_free,
Signature · 1 line
2.17.1
PATCH v4 4/4 tracing: Have existing event_command.parse() implementations use helpers
In reply to Tom ZanussiSimplify the existing event_command.parse() implementations by having them make use of the helper functions previously introduced.
--- kernel/trace/trace.h | 3 +- kernel/trace/trace_eprobe.c | 3 +- kernel/trace/trace_events_hist.c | 75 +++++------- kernel/trace/trace_events_trigger.c | 176 ++++++++-------------------- 4 files changed, 81 insertions(+), 176 deletions(-)
kernel/trace/trace.h +2 -1
@@ -1580,7 +1580,8 @@ extern void event_enable_trigger_free(struct event_trigger_ops *ops, struct event_trigger_data *data);extern int event_enable_trigger_parse (struct event_command *cmd_ops,struct trace_event_file *file, -char *glob,char *cmd,char *param); +char *glob,char *cmd, +char *param_and_filter); extern int event_enable_register_trigger(char *glob, struct event_trigger_ops *ops, struct event_trigger_data *data,
kernel/trace/trace_eprobe.c +2 -1
@@ -551,7 +551,8 @@ static struct event_trigger_ops eprobe_trigger_ops = {static int eprobe_trigger_cmd_parse (struct event_command *cmd_ops,struct trace_event_file *file, -char *glob,char *cmd,char *param) +char *glob,char *cmd, +char *param_and_filter) {return -1 ; }
kernel/trace/trace_events_hist.c +29 -46
163 lines not on this page · raw message
kernel/trace/trace_events_trigger.c +48 -128
282 lines not on this page · raw message
Signature · 1 line
2.17.1
Masami Hiramatsu
maintainerRe: [PATCH v4 3/4] tracing: Add helper functions to simplify event_command.parse() callback handling
In reply to Tom ZanussiHi Tom,
Quoted 20 lines · On Fri, 10 Dec 2021 15:16:24 -0600 Tom Zanussi <[email protected]> wrote:
+/*
+ * Event trigger parsing helper functions.
+ *
+ * These functions help make it easier to write an event trigger
+ * parsing function i.e. the structevent_command.parse()callback
+ * function responsible for parsing and registering a trigger command
+ * written to the 'trigger' file.
+ *
+ * A trigger command (or just 'trigger' for short) takes the form:
+ * [trigger] [if filter]
+ *
+ * The structevent_command.parse()callback (and other struct
+ *event_commandfunctions) refer to several components of a trigger
+ * command. Those same components are referenced by the event trigger
+ * parsing helper functions defined below. These components are:
+ *+ * cmd - the trigger command name + * glob - the trigger command name optionally prefaced with '!' + * param - text remaining after the command is stripped of cmd and ':' + * filter - the optional filter text following (and including) 'if'
Quoted 8 lines
+ *
+ * To illustrate the use of these componenents, here are some concrete
+ * examples. For the following triggers:
+ *
+ * echo 'traceon:5 if pid == 0' > trigger+ * - 'traceon' is both cmd and glob + * - '5 if pid == 0' is the param + * - 'if pid == 0' is the filter
I would like to know why both 'param' and 'filter' has "if pid == 0". I thought that part is (generic) filter and not a parameter.
If I missed any discussion on this point, sorry about that.
Quoted 123 lines
+ *
+ * echo 'enable_event:sys:event:n' > trigger+ * - 'enable_event' is both cmd and glob + * - 'sys:event:n' is the param + * - there is no filter + * + * echo 'hist:keys=pid if prio > 50' > trigger + * - 'hist' is both cmd and glob + * - 'keys=pid if prio > 50' is the param + * - 'if prio > 50' is the filter + * + * echo '!enable_event:sys:event:n' > trigger + * - 'enable_event' the cmd + * - '!enable_event' is the glob + * - 'sys:event:n' is the param + * - there is no filter + * + * echo 'traceoff' > trigger + * - 'traceoff' is both cmd and glob + * - there is no param + * - there is no filter+ *
+ * There are a few different categories of event trigger covered by
+ * these helpers:
+ *+ * - triggers that don't require a parameter e.g. traceon + * - triggers that do require a parameter e.g. enable_event and hist + * - triggers that though they may not require a param may support an+ * optional 'n' param (n = number of times the trigger should fire)
+ * e.g.: traceon:5 or enable_event:sys:event:n
+ * - triggers that do not support an 'n' param e.g. hist
+ *
+ * These functions can be used or ignored as necessary - it all
+ * depends on the complexity of the trigger, and the granularity of
+ * the functions supported reflects the fact that some implementations
+ * may need to customize certain aspects of their implementations and
+ * won't need certain functions. For instance, the hist trigger
+ * implementation doesn't useevent_trigger_separate_filter()because
+ * it has special requirements for handling the filter.
+ */kernel/trace/trace_events_trigger.c +83
-0+ +/** +* event_trigger_check_remove - check whether an event trigger specifies remove +* @glob: The trigger command string, with optional remove(!) operator +* +* The event trigger callback implementations pass in 'glob' as a +* parameter. This is the command name either with or without a +* remove(!) operator. This function simply parses the glob and +* determines whether the command corresponds to a trigger removal or +* a trigger addition. +* +* Return: true if this is a remove command, false otherwise +*/ +bool event_trigger_check_remove (const char *glob) +{ +return (glob && glob[0 ] =='!' ) ? true : false; +} + +/** +* event_trigger_empty_param - check whether the param is empty +* @param: The trigger param string +* +* The event trigger callback implementations pass in 'param' as a +* parameter. This corresponds to the string following the command +* name minus the command name. This function can be called by a +* callback implementation for any command that requires a param; a +* callback that doesn't require a param can ignore it. +* +* Return: true if this is an empty param, false otherwise +*/ +bool event_trigger_empty_param (const char *param) +{ +return !param; +} + +/** +* event_trigger_separate_filter - separate an event trigger from a filter +* @param: The param string containing trigger and possibly filter +* @trigger: outparam, will be filled with a pointer to the trigger +* @filter: outparam, will be filled with a pointer to the filter +* @param_required: Specifies whether or not the param string is required +* +* Given a param string of the form '[trigger] [if filter]', this +* function separates the filter from the trigger and returns the +* trigger in *trigger and the filter in *filter. Either the *trigger +* or the *filter may be set to NULL by this function - if not set to +* NULL, they will contain strings corresponding to the trigger and +* filter. +* +* There are two cases that need to be handled with respect to the +* passed-in param: either the param is required, or it is not +* required. If @param_required is set, and there's no param, it will +* return -EINVAL. If @param_required is not set and there's a param +* that starts with a number, that corresponds to the case of a +* trigger with :n (n = number of times the trigger should fire) and +* the parsing continues normally; otherwise the function just returns +* and assumes param just contains a filter and there's nothing else +* to do. +* +* Return: 0 on success, errno otherwise +*/ +int event_trigger_separate_filter (char *param_and_filter,char **param, +char **filter,bool param_required) +{ +int ret =0 ; + + *param = *filter =NULL ; + +if (!param_and_filter) { +if (param_required) + ret = -EINVAL ; + goto out; + } + +/* +* Here we check for an optional param. The only legal +* optional param is :n, and if that's the case, continue +* below. Otherwise we assume what's left is a filter and +* return it as the filter string for the caller to deal with. +*/ +if (!param_required && param_and_filter && !isdigit (param_and_filter[0 ])) { + *filter = param_and_filter; + goto out;
kernel/trace/trace_events_trigger.c static void unregister_trigger(char *glob, struct event_trigger_ops *ops, · line 784
Quoted 10 lines
kernel/trace/trace_events_trigger.c +10
-0+ } + +/* +* Separate the param from the filter (param [if filter]). +* Here we have either an optional :n param or a required +* param and an optional filter. +*/ + *param =strsep (¶m_and_filter," \t" ); +if (!*param) { + ret = -EINVAL ;
This seems not happen because this means "!param_and_filter" and that is already checked at the entry of this function.
Quoted 50 lines
kernel/trace/trace_events_trigger.c +50
-0+ goto out; + } + +/* +* Here we have a filter, though it may be empty. +*/ +if (param_and_filter) { + *filter =skip_spaces (param_and_filter); +if (!**filter) + *filter =NULL ; + } +out: +return ret; +} + +/** +* event_trigger_alloc - allocate and init event_trigger_data for a trigger +* @cmd_ops: The event_command operations for the trigger +* @cmd: The cmd string +* @param: The param string +* @private_data: User data to associate with the event trigger +* +* Allocate an event_trigger_data instance and initialize it. The +* @cmd_ops are used along with the @cmd and @param to get the +* trigger_ops to assign to the event_trigger_data. @private_data can +* also be passed in and associated with the event_trigger_data. +* +* Use event_trigger_free() to free an event_trigger_data object. +* +* Return: The trigger_data object success, NULL otherwise +*/ +struct event_trigger_data *event_trigger_alloc (struct event_command *cmd_ops, +char *cmd, +char *param, +void *private_data) +{ +struct event_trigger_data *trigger_data; +struct event_trigger_ops *trigger_ops; + + trigger_ops = cmd_ops->get_trigger_ops (cmd, param); + + trigger_data =kzalloc (sizeof (*trigger_data),GFP_KERNEL ); +if (!trigger_data) +return NULL ; + + trigger_data->count =-1 ; + trigger_data->ops = trigger_ops; + trigger_data->cmd_ops = cmd_ops; + trigger_data->private_data = private_data; +
kernel/trace/trace_events_trigger.c static void unregister_trigger(char *glob, struct event_trigger_ops *ops, · line 844
Quoted 10 lines
kernel/trace/trace_events_trigger.c +10
-0+INIT_LIST_HEAD (&trigger_data->list); +INIT_LIST_HEAD (&trigger_data->named_list); +RCU_INIT_POINTER (trigger_data->filter,NULL ); + +return trigger_data; +} + +/** +* event_trigger_parse_num - parse and return the number param for a trigger +* @trigger: The trigger string
Does this mean trigger parameter?
Quoted 18 lines
+ * @trigger_data: The
trigger_datafor the trigger
+ *
+ * Parse the :n (n = number of times the trigger should fire) param
+ * and set the count variable in thetrigger_datato the parsed count.
+ *
+ * Return: 0 on success, errno otherwise
+ */kernel/trace/trace_events_trigger.c +11
-0+int event_trigger_parse_num (char *trigger, +struct event_trigger_data *trigger_data) +{ +char *number; +int ret =0 ; + +if (trigger) { + number =strsep (&trigger,":" ); + +if (!strlen (number)) +return -EINVAL ;
kernel/trace/trace_events_trigger.c static void unregister_trigger(char *glob, struct event_trigger_ops *ops, · line 872
Quoted 10 lines
kernel/trace/trace_events_trigger.c +10
-0+ +/* +* We use the callback data field (which is a pointer) +* as our counter. +*/ + ret =kstrtoul (number,0 , &trigger_data->count); + } + +return ret; +}
Does this work with '!enable_event:sys:event:n' case? (we need to find the last parameter.)
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Re: [PATCH v4 3/4] tracing: Add helper functions to simplify event_command.parse() callback handling
Hi Masami,
Quoted 41 lines · Masami Hiramatsu
Hi Tom,
Quoted 32 lines · On Fri, 10 Dec 2021 15:16:24 -0600 Tom Zanussi <[email protected]> wrote:
+/*
+ * Event trigger parsing helper functions.
+ *
+ * These functions help make it easier to write an event trigger
+ * parsing function i.e. the structevent_command.parse()callback
+ * function responsible for parsing and registering a trigger command
+ * written to the 'trigger' file.
+ *
+ * A trigger command (or just 'trigger' for short) takes the form:
+ * [trigger] [if filter]
+ *
+ * The structevent_command.parse()callback (and other struct
+ *event_commandfunctions) refer to several components of a trigger
+ * command. Those same components are referenced by the event trigger
+ * parsing helper functions defined below. These components are:
+ *+ * cmd - the trigger command name + * glob - the trigger command name optionally prefaced with'!'
+ * param - text remaining after the command is stripped of cmd
and ':'
+ * filter - the optional filter text following (and including)
'if'
+ *
+ * To illustrate the use of these componenents, here are some concrete
+ * examples. For the following triggers:
+ *
+ * echo 'traceon:5 if pid == 0' > triggerQuoted 3 lines
+ * - 'traceon' is both cmd and glob + * - '5 if pid == 0' is the param + * - 'if pid == 0' is the filter
Quoted 4 lines
I would like to know why both 'param' and 'filter' has "if pid == 0". I thought that part is (generic) filter and not a parameter.
If I missed any discussion on this point, sorry about that.
No discussion about this in particular, but from Steve's review I did change 'param' to 'param_and_filter' in the code. So I guess I should change these to strip the filter from the param when describing them, and then it's obvious that param_and_filter refers to the combination. I'll change that in the next version.
Quoted 155 lines
Quoted 155 lines
+ *
+ * echo 'enable_event:sys:event:n' > trigger+ * - 'enable_event' is both cmd and glob + * - 'sys:event:n' is the param + * - there is no filter + * + * echo 'hist:keys=pid if prio > 50' > trigger + * - 'hist' is both cmd and glob + * - 'keys=pid if prio > 50' is the param + * - 'if prio > 50' is the filter + * + * echo '!enable_event:sys:event:n' > trigger + * - 'enable_event' the cmd + * - '!enable_event' is the glob + * - 'sys:event:n' is the param + * - there is no filter + * + * echo 'traceoff' > trigger + * - 'traceoff' is both cmd and glob + * - there is no param + * - there is no filter+ *
+ * There are a few different categories of event trigger covered by
+ * these helpers:
+ *
+ * - triggers that don't require a parameter e.g. traceon
+ * - triggers that do require a parameter e.g.enable_eventand hist
+ * - triggers that though they may not require a param may support an
+ * optional 'n' param (n = number of times the trigger should
fire)
+ * e.g.: traceon:5 or enable_event:sys:event:n
+ * - triggers that do not support an 'n' param e.g. hist
+ *
+ * These functions can be used or ignored as necessary - it all
+ * depends on the complexity of the trigger, and the granularity of
+ * the functions supported reflects the fact that some implementations
+ * may need to customize certain aspects of their implementations and
+ * won't need certain functions. For instance, the hist trigger
+ * implementation doesn't useevent_trigger_separate_filter()because
+ * it has special requirements for handling the filter.
+ */kernel/trace/trace_events_trigger.c +3
-0+ +/** +* event_trigger_check_remove - check whether an event trigger specifies remove
+ * @glob: The trigger command string, with optional remove(!) operator
+ *
+ * The event trigger callback implementations pass in 'glob' as a
+ * parameter. This is the command name either with or without a
+ * remove(!) operator. This function simply parses the glob and
+ * determines whether the command corresponds to a trigger removal or
+ * a trigger addition.
+ *
+ * Return: true if this is a remove command, false otherwise
+ */kernel/trace/trace_events_trigger.c +11
-0+bool event_trigger_check_remove (const char *glob) +{ +return (glob && glob[0 ] =='!' ) ? true : false; +} + +/** +* event_trigger_empty_param - check whether the param is empty +* @param: The trigger param string +* +* The event trigger callback implementations pass in 'param' as a +* parameter. This corresponds to the string following the command
+ * name minus the command name. This function can be called by a
+ * callback implementation for any command that requires a param; a
+ * callback that doesn't require a param can ignore it.
+ *
+ * Return: true if this is an empty param, false otherwise
+ */kernel/trace/trace_events_trigger.c +7
-0+bool event_trigger_empty_param (const char *param) +{ +return !param; +} + +/** +* event_trigger_separate_filter - separate an event trigger from a filter
+ * @param: The param string containing trigger and possibly filter
+ * @trigger: outparam, will be filled with a pointer to the trigger
+ * @filter: outparam, will be filled with a pointer to the filter
+ * @param_required: Specifies whether or not the param string is required
+ *
+ * Given a param string of the form '[trigger] [if filter]', this
+ * function separates the filter from the trigger and returns the
+ * trigger in *trigger and the filter in *filter. Either the *trigger
+ * or the *filter may be set to NULL by this function - if not set to
+ * NULL, they will contain strings corresponding to the trigger and
+ * filter.
+ *
+ * There are two cases that need to be handled with respect to the
+ * passed-in param: either the param is required, or it is not
+ * required. If @param_required is set, and there's no param, it will
+ * return-EINVAL. If @param_required is not set and there's a param
+ * that starts with a number, that corresponds to the case of a
+ * trigger with :n (n = number of times the trigger should fire) and
+ * the parsing continues normally; otherwise the function just returns
+ * and assumes param just contains a filter and there's nothing else
+ * to do.
+ *
+ * Return: 0 on success, errno otherwise
+ */
+int event_trigger_separate_filter(char*param_and_filter, char **param,kernel/trace/trace_events_trigger.c +19
-0+ char **filter, bool param_required) +{ + int ret = 0; + + *param = *filter =NULL ; + +if (!param_and_filter) { +if (param_required) + ret = -EINVAL ; + goto out; + } + +/* +* Here we check for an optional param. The only legal +* optional param is :n, and if that's the case, continue +* below. Otherwise we assume what's left is a filter and +* return it as the filter string for the caller to deal with. +*/ + if (!param_required && param_and_filter &&!isdigit(param_and_filter[0])) {
kernel/trace/trace_events_trigger.c +7
-0+ *filter = param_and_filter; + goto out; + } + +/* +* Separate the param from the filter (param [if filter]). +* Here we have either an optional :n param or a required
Quoted 8 lines
kernel/trace/trace_events_trigger.c static void unregister_trigger(char *glob, struct event_trigger_ops *ops, · line 784
Quoted 5 lines
kernel/trace/trace_events_trigger.c +5
-0+* param and an optional filter. +*/ + *param =strsep (¶m_and_filter," \t" ); +if (!*param) { + ret = -EINVAL ;This seems not happen because this means "!param_and_filter" and that is already checked at the entry of this function.
Yes, I think you're correct. I can just remove that check.
Quoted 59 lines
Quoted 59 lines
kernel/trace/trace_events_trigger.c +17
-0+ goto out; + } + +/* +* Here we have a filter, though it may be empty. +*/ +if (param_and_filter) { + *filter =skip_spaces (param_and_filter); +if (!**filter) + *filter =NULL ; + } +out: +return ret; +} + +/** +* event_trigger_alloc - allocate and init event_trigger_data for a trigger
+ * @cmd_ops: Theevent_commandoperations for the trigger
+ * @cmd: The cmd string
+ * @param: The param string
+ * @private_data: User data to associate with the event trigger
+ *
+ * Allocate anevent_trigger_datainstance and initialize it. The
+ * @cmd_ops are used along with the @cmd and @param to get the
+ *trigger_opsto assign to theevent_trigger_data. @private_data can
+ * also be passed in and associated with theevent_trigger_data.
+ *
+ * Useevent_trigger_free()to free anevent_trigger_dataobject.
+ *
+ * Return: Thetrigger_dataobject success, NULL otherwise
+ */
+structevent_trigger_data*event_trigger_alloc(structevent_command*cmd_ops,kernel/trace/trace_events_trigger.c +23
-0+char *cmd, + char *param, + void *private_data) +{ + struct event_trigger_data *trigger_data; +struct event_trigger_ops *trigger_ops; + + trigger_ops = cmd_ops->get_trigger_ops (cmd, param); + + trigger_data =kzalloc (sizeof (*trigger_data),GFP_KERNEL ); +if (!trigger_data) +return NULL ; + + trigger_data->count =-1 ; + trigger_data->ops = trigger_ops; + trigger_data->cmd_ops = cmd_ops; + trigger_data->private_data = private_data; + +INIT_LIST_HEAD (&trigger_data->list); +INIT_LIST_HEAD (&trigger_data->named_list); +RCU_INIT_POINTER (trigger_data->filter,NULL ); + +return trigger_data;
Quoted 8 lines
Quoted 6 lines
kernel/trace/trace_events_trigger.c +4
-0+} + +/** +* event_trigger_parse_num - parse and return the number param for a trigger
+ * @trigger: The trigger stringDoes this mean trigger parameter?
Yes, I'll change this to 'param'.
Quoted 26 lines
Quoted 26 lines
+ * @trigger_data: The
trigger_datafor the trigger
+ *
+ * Parse the :n (n = number of times the trigger should fire) param
+ * and set the count variable in thetrigger_datato the parsed count.
+ *
+ * Return: 0 on success, errno otherwise
+ */kernel/trace/trace_events_trigger.c +16
-0+int event_trigger_parse_num (char *trigger, +struct event_trigger_data *trigger_data) +{ +char *number; +int ret =0 ; + +if (trigger) { + number =strsep (&trigger,":" ); + +if (!strlen (number)) +return -EINVAL ; + +/* +* We use the callback data field (which is a pointer) +* as our counter. +*/
Quoted 8 lines
kernel/trace/trace_events_trigger.c static void unregister_trigger(char *glob, struct event_trigger_ops *ops, · line 872
Quoted 5 lines
kernel/trace/trace_events_trigger.c +5
-0+ ret =kstrtoul (number,0 , &trigger_data->count); + } + +return ret; +}Does this work with '!enable_event:sys:event:n' case? (we need to find the last parameter.)
Yes, because :n isn't used in that case. Since you can't have multiple 'enable_event:sys:event:n' where sys:event are the same, whether using :n or not, it suffices for delete to use only sys:event and ignore :n.
Thanks,
Tom
Quoted 4 lines
Thank you,