1 // SPDX-License-Identifier: GPL-2.0
3 * Generic dynamic event control interface
5 * Copyright (C) 2018 Masami Hiramatsu <mhiramat@kernel.org>
8 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/tracefs.h>
16 #include "trace_dynevent.h"
18 static DEFINE_MUTEX(dyn_event_ops_mutex);
19 static LIST_HEAD(dyn_event_ops_list);
21 int dyn_event_register(struct dyn_event_operations *ops)
23 if (!ops || !ops->create || !ops->show || !ops->is_busy ||
24 !ops->free || !ops->match)
27 INIT_LIST_HEAD(&ops->list);
28 mutex_lock(&dyn_event_ops_mutex);
29 list_add_tail(&ops->list, &dyn_event_ops_list);
30 mutex_unlock(&dyn_event_ops_mutex);
34 int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type)
36 struct dyn_event *pos, *n;
37 char *system = NULL, *event, *p;
40 if (argv[0][0] == '-') {
41 if (argv[0][1] != ':')
45 event = strchr(argv[0], ':');
52 p = strchr(event, '/');
61 mutex_lock(&event_mutex);
62 for_each_dyn_event_safe(pos, n) {
63 if (type && type != pos->ops)
65 if (!pos->ops->match(system, event,
66 argc, (const char **)argv, pos))
69 ret = pos->ops->free(pos);
73 mutex_unlock(&event_mutex);
78 static int create_dyn_event(int argc, char **argv)
80 struct dyn_event_operations *ops;
83 if (argv[0][0] == '-' || argv[0][0] == '!')
84 return dyn_event_release(argc, argv, NULL);
86 mutex_lock(&dyn_event_ops_mutex);
87 list_for_each_entry(ops, &dyn_event_ops_list, list) {
88 ret = ops->create(argc, (const char **)argv);
89 if (!ret || ret != -ECANCELED)
92 mutex_unlock(&dyn_event_ops_mutex);
93 if (ret == -ECANCELED)
99 /* Protected by event_mutex */
100 LIST_HEAD(dyn_event_list);
102 void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
104 mutex_lock(&event_mutex);
105 return seq_list_start(&dyn_event_list, *pos);
108 void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
110 return seq_list_next(v, &dyn_event_list, pos);
113 void dyn_event_seq_stop(struct seq_file *m, void *v)
115 mutex_unlock(&event_mutex);
118 static int dyn_event_seq_show(struct seq_file *m, void *v)
120 struct dyn_event *ev = v;
123 return ev->ops->show(m, ev);
128 static const struct seq_operations dyn_event_seq_op = {
129 .start = dyn_event_seq_start,
130 .next = dyn_event_seq_next,
131 .stop = dyn_event_seq_stop,
132 .show = dyn_event_seq_show
136 * dyn_events_release_all - Release all specific events
137 * @type: the dyn_event_operations * which filters releasing events
139 * This releases all events which ->ops matches @type. If @type is NULL,
140 * all events are released.
141 * Return -EBUSY if any of them are in use, and return other errors when
142 * it failed to free the given event. Except for -EBUSY, event releasing
143 * process will be aborted at that point and there may be some other
144 * releasable events on the list.
146 int dyn_events_release_all(struct dyn_event_operations *type)
148 struct dyn_event *ev, *tmp;
151 mutex_lock(&event_mutex);
152 for_each_dyn_event(ev) {
153 if (type && ev->ops != type)
155 if (ev->ops->is_busy(ev)) {
160 for_each_dyn_event_safe(ev, tmp) {
161 if (type && ev->ops != type)
163 ret = ev->ops->free(ev);
168 mutex_unlock(&event_mutex);
173 static int dyn_event_open(struct inode *inode, struct file *file)
177 ret = tracing_check_open_get_tr(NULL);
181 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
182 ret = dyn_events_release_all(NULL);
187 return seq_open(file, &dyn_event_seq_op);
190 static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
191 size_t count, loff_t *ppos)
193 return trace_parse_run_command(file, buffer, count, ppos,
197 static const struct file_operations dynamic_events_ops = {
198 .owner = THIS_MODULE,
199 .open = dyn_event_open,
202 .release = seq_release,
203 .write = dyn_event_write,
206 /* Make a tracefs interface for controlling dynamic events */
207 static __init int init_dynamic_event(void)
209 struct dentry *d_tracer;
210 struct dentry *entry;
212 d_tracer = tracing_init_dentry();
213 if (IS_ERR(d_tracer))
216 entry = tracefs_create_file("dynamic_events", 0644, d_tracer,
217 NULL, &dynamic_events_ops);
219 /* Event list interface */
221 pr_warn("Could not create tracefs 'dynamic_events' entry\n");
225 fs_initcall(init_dynamic_event);
228 * dynevent_arg_add - Add an arg to a dynevent_cmd
229 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
230 * @arg: The argument to append to the current cmd
232 * Append an argument to a dynevent_cmd. The argument string will be
233 * appended to the current cmd string, followed by a separator, if
234 * applicable. Before the argument is added, the check_arg()
235 * function, if defined, is called.
237 * The cmd string, separator, and check_arg() function should be set
238 * using the dynevent_arg_init() before any arguments are added using
241 * Return: 0 if successful, error otherwise.
243 int dynevent_arg_add(struct dynevent_cmd *cmd,
244 struct dynevent_arg *arg)
250 if (arg->check_arg) {
251 ret = arg->check_arg(arg);
256 q = cmd->buf + (cmd->maxlen - cmd->remaining);
258 delta = snprintf(q, cmd->remaining, " %s%c", arg->str, arg->separator);
259 if (delta >= cmd->remaining) {
260 pr_err("String is too long: %s\n", arg->str);
263 cmd->remaining -= delta;
269 * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
270 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
271 * @arg_pair: The argument pair to append to the current cmd
273 * Append an argument pair to a dynevent_cmd. An argument pair
274 * consists of a left-hand-side argument and a right-hand-side
275 * argument separated by an operator, which can be whitespace, all
276 * followed by a separator, if applicable. This can be used to add
277 * arguments of the form 'type variable_name;' or 'x+y'.
279 * The lhs argument string will be appended to the current cmd string,
280 * followed by an operator, if applicable, followd by the rhs string,
281 * followed finally by a separator, if applicable. Before anything is
282 * added, the check_arg() function, if defined, is called.
284 * The cmd strings, operator, separator, and check_arg() function
285 * should be set using the dynevent_arg_pair_init() before any arguments
286 * are added using this function.
288 * Return: 0 if successful, error otherwise.
290 int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
291 struct dynevent_arg_pair *arg_pair)
297 if (arg_pair->check_arg) {
298 ret = arg_pair->check_arg(arg_pair);
303 q = cmd->buf + (cmd->maxlen - cmd->remaining);
305 delta = snprintf(q, cmd->remaining, " %s%c", arg_pair->lhs,
307 if (delta >= cmd->remaining) {
308 pr_err("field string is too long: %s\n", arg_pair->lhs);
311 cmd->remaining -= delta; q += delta;
313 delta = snprintf(q, cmd->remaining, "%s%c", arg_pair->rhs,
314 arg_pair->separator);
315 if (delta >= cmd->remaining) {
316 pr_err("field string is too long: %s\n", arg_pair->rhs);
319 cmd->remaining -= delta; q += delta;
325 * dynevent_str_add - Add a string to a dynevent_cmd
326 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
327 * @str: The string to append to the current cmd
329 * Append a string to a dynevent_cmd. The string will be appended to
330 * the current cmd string as-is, with nothing prepended or appended.
332 * Return: 0 if successful, error otherwise.
334 int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
340 q = cmd->buf + (cmd->maxlen - cmd->remaining);
342 delta = snprintf(q, cmd->remaining, "%s", str);
343 if (delta >= cmd->remaining) {
344 pr_err("String is too long: %s\n", str);
347 cmd->remaining -= delta;
353 * dynevent_cmd_init - Initialize a dynevent_cmd object
354 * @cmd: A pointer to the dynevent_cmd struct representing the cmd
355 * @buf: A pointer to the buffer to generate the command into
356 * @maxlen: The length of the buffer the command will be generated into
357 * @type: The type of the cmd, checked against further operations
358 * @run_command: The type-specific function that will actually run the command
360 * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and
361 * run dynamic event creation commands, such as commands for creating
362 * synthetic and kprobe events. Before calling any of the functions
363 * used to build the command, a dynevent_cmd object should be
364 * instantiated and initialized using this function.
366 * The initialization sets things up by saving a pointer to the
367 * user-supplied buffer and its length via the @buf and @maxlen
368 * params, and by saving the cmd-specific @type and @run_command
369 * params which are used to check subsequent dynevent_cmd operations
370 * and actually run the command when complete.
372 void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
373 enum dynevent_type type,
374 dynevent_create_fn_t run_command)
376 memset(cmd, '\0', sizeof(*cmd));
379 cmd->maxlen = maxlen;
380 cmd->remaining = cmd->maxlen;
382 cmd->run_command = run_command;
386 * dynevent_arg_init - Initialize a dynevent_arg object
387 * @arg: A pointer to the dynevent_arg struct representing the arg
388 * @check_arg: An (optional) pointer to a function checking arg sanity
389 * @separator: An (optional) separator, appended after adding the arg
391 * Initialize a dynevent_arg object. A dynevent_arg represents an
392 * object used to append single arguments to the current command
393 * string. The @check_arg function, if present, will be used to check
394 * the sanity of the current arg string (which is directly set by the
395 * caller). After the arg string is successfully appended to the
396 * command string, the optional @separator is appended. If no
397 * separator was specified when initializing the arg, a space will be
400 void dynevent_arg_init(struct dynevent_arg *arg,
401 dynevent_check_arg_fn_t check_arg,
404 memset(arg, '\0', sizeof(*arg));
408 arg->separator = separator;
410 arg->check_arg = check_arg;
414 * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
415 * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
416 * @check_arg: An (optional) pointer to a function checking arg sanity
417 * @operator: An (optional) operator, appended after adding the first arg
418 * @separator: An (optional) separator, appended after adding the second arg
420 * Initialize a dynevent_arg_pair object. A dynevent_arg_pair
421 * represents an object used to append argument pairs such as 'type
422 * variable_name;' or 'x+y' to the current command string. An
423 * argument pair consists of a left-hand-side argument and a
424 * right-hand-side argument separated by an operator, which can be
425 * whitespace, all followed by a separator, if applicable. The
426 * @check_arg function, if present, will be used to check the sanity
427 * of the current arg strings (which is directly set by the caller).
428 * After the first arg string is successfully appended to the command
429 * string, the optional @operator is appended, followed by the second
430 * arg and and optional @separator. If no separator was specified
431 * when initializing the arg, a space will be appended.
433 void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
434 dynevent_check_arg_fn_t check_arg,
435 char operator, char separator)
437 memset(arg_pair, '\0', sizeof(*arg_pair));
441 arg_pair->operator = operator;
445 arg_pair->separator = separator;
447 arg_pair->check_arg = check_arg;
451 * dynevent_create - Create the dynamic event contained in dynevent_cmd
452 * @cmd: The dynevent_cmd object containing the dynamic event creation command
454 * Once a dynevent_cmd object has been successfully built up via the
455 * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
456 * functions, this function runs the final command to actually create
459 * Return: 0 if the event was successfully created, error otherwise.
461 int dynevent_create(struct dynevent_cmd *cmd)
463 return cmd->run_command(cmd);
465 EXPORT_SYMBOL_GPL(dynevent_create);