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(const char *raw_command, struct dyn_event_operations *type)
36 struct dyn_event *pos, *n;
37 char *system = NULL, *event, *p;
38 int argc, ret = -ENOENT;
41 argv = argv_split(GFP_KERNEL, raw_command, &argc);
45 if (argv[0][0] == '-') {
46 if (argv[0][1] != ':') {
52 event = strchr(argv[0], ':');
60 p = strchr(event, '/');
66 if (event[0] == '\0') {
71 mutex_lock(&event_mutex);
72 for_each_dyn_event_safe(pos, n) {
73 if (type && type != pos->ops)
75 if (!pos->ops->match(system, event,
76 argc - 1, (const char **)argv + 1, pos))
79 ret = pos->ops->free(pos);
83 mutex_unlock(&event_mutex);
89 static int create_dyn_event(const char *raw_command)
91 struct dyn_event_operations *ops;
94 if (raw_command[0] == '-' || raw_command[0] == '!')
95 return dyn_event_release(raw_command, NULL);
97 mutex_lock(&dyn_event_ops_mutex);
98 list_for_each_entry(ops, &dyn_event_ops_list, list) {
99 ret = ops->create(raw_command);
100 if (!ret || ret != -ECANCELED)
103 mutex_unlock(&dyn_event_ops_mutex);
104 if (ret == -ECANCELED)
110 /* Protected by event_mutex */
111 LIST_HEAD(dyn_event_list);
113 void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
115 mutex_lock(&event_mutex);
116 return seq_list_start(&dyn_event_list, *pos);
119 void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
121 return seq_list_next(v, &dyn_event_list, pos);
124 void dyn_event_seq_stop(struct seq_file *m, void *v)
126 mutex_unlock(&event_mutex);
129 static int dyn_event_seq_show(struct seq_file *m, void *v)
131 struct dyn_event *ev = v;
134 return ev->ops->show(m, ev);
139 static const struct seq_operations dyn_event_seq_op = {
140 .start = dyn_event_seq_start,
141 .next = dyn_event_seq_next,
142 .stop = dyn_event_seq_stop,
143 .show = dyn_event_seq_show
147 * dyn_events_release_all - Release all specific events
148 * @type: the dyn_event_operations * which filters releasing events
150 * This releases all events which ->ops matches @type. If @type is NULL,
151 * all events are released.
152 * Return -EBUSY if any of them are in use, and return other errors when
153 * it failed to free the given event. Except for -EBUSY, event releasing
154 * process will be aborted at that point and there may be some other
155 * releasable events on the list.
157 int dyn_events_release_all(struct dyn_event_operations *type)
159 struct dyn_event *ev, *tmp;
162 mutex_lock(&event_mutex);
163 for_each_dyn_event(ev) {
164 if (type && ev->ops != type)
166 if (ev->ops->is_busy(ev)) {
171 for_each_dyn_event_safe(ev, tmp) {
172 if (type && ev->ops != type)
174 ret = ev->ops->free(ev);
179 mutex_unlock(&event_mutex);
184 static int dyn_event_open(struct inode *inode, struct file *file)
188 ret = tracing_check_open_get_tr(NULL);
192 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
193 ret = dyn_events_release_all(NULL);
198 return seq_open(file, &dyn_event_seq_op);
201 static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
202 size_t count, loff_t *ppos)
204 return trace_parse_run_command(file, buffer, count, ppos,
208 static const struct file_operations dynamic_events_ops = {
209 .owner = THIS_MODULE,
210 .open = dyn_event_open,
213 .release = seq_release,
214 .write = dyn_event_write,
217 /* Make a tracefs interface for controlling dynamic events */
218 static __init int init_dynamic_event(void)
220 struct dentry *entry;
223 ret = tracing_init_dentry();
227 entry = tracefs_create_file("dynamic_events", 0644, NULL,
228 NULL, &dynamic_events_ops);
230 /* Event list interface */
232 pr_warn("Could not create tracefs 'dynamic_events' entry\n");
236 fs_initcall(init_dynamic_event);
239 * dynevent_arg_add - Add an arg to a dynevent_cmd
240 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
241 * @arg: The argument to append to the current cmd
242 * @check_arg: An (optional) pointer to a function checking arg sanity
244 * Append an argument to a dynevent_cmd. The argument string will be
245 * appended to the current cmd string, followed by a separator, if
246 * applicable. Before the argument is added, the @check_arg function,
247 * if present, will be used to check the sanity of the current arg
250 * The cmd string and separator should be set using the
251 * dynevent_arg_init() before any arguments are added using this
254 * Return: 0 if successful, error otherwise.
256 int dynevent_arg_add(struct dynevent_cmd *cmd,
257 struct dynevent_arg *arg,
258 dynevent_check_arg_fn_t check_arg)
263 ret = check_arg(arg);
268 ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
270 pr_err("String is too long: %s%c\n", arg->str, arg->separator);
278 * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
279 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
280 * @arg_pair: The argument pair to append to the current cmd
281 * @check_arg: An (optional) pointer to a function checking arg sanity
283 * Append an argument pair to a dynevent_cmd. An argument pair
284 * consists of a left-hand-side argument and a right-hand-side
285 * argument separated by an operator, which can be whitespace, all
286 * followed by a separator, if applicable. This can be used to add
287 * arguments of the form 'type variable_name;' or 'x+y'.
289 * The lhs argument string will be appended to the current cmd string,
290 * followed by an operator, if applicable, followed by the rhs string,
291 * followed finally by a separator, if applicable. Before the
292 * argument is added, the @check_arg function, if present, will be
293 * used to check the sanity of the current arg strings.
295 * The cmd strings, operator, and separator should be set using the
296 * dynevent_arg_pair_init() before any arguments are added using this
299 * Return: 0 if successful, error otherwise.
301 int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
302 struct dynevent_arg_pair *arg_pair,
303 dynevent_check_arg_fn_t check_arg)
308 ret = check_arg(arg_pair);
313 ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
314 arg_pair->operator, arg_pair->rhs,
315 arg_pair->separator);
317 pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
318 arg_pair->operator, arg_pair->rhs,
319 arg_pair->separator);
327 * dynevent_str_add - Add a string to a dynevent_cmd
328 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
329 * @str: The string to append to the current cmd
331 * Append a string to a dynevent_cmd. The string will be appended to
332 * the current cmd string as-is, with nothing prepended or appended.
334 * Return: 0 if successful, error otherwise.
336 int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
340 ret = seq_buf_puts(&cmd->seq, str);
342 pr_err("String is too long: %s\n", str);
350 * dynevent_cmd_init - Initialize a dynevent_cmd object
351 * @cmd: A pointer to the dynevent_cmd struct representing the cmd
352 * @buf: A pointer to the buffer to generate the command into
353 * @maxlen: The length of the buffer the command will be generated into
354 * @type: The type of the cmd, checked against further operations
355 * @run_command: The type-specific function that will actually run the command
357 * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and
358 * run dynamic event creation commands, such as commands for creating
359 * synthetic and kprobe events. Before calling any of the functions
360 * used to build the command, a dynevent_cmd object should be
361 * instantiated and initialized using this function.
363 * The initialization sets things up by saving a pointer to the
364 * user-supplied buffer and its length via the @buf and @maxlen
365 * params, and by saving the cmd-specific @type and @run_command
366 * params which are used to check subsequent dynevent_cmd operations
367 * and actually run the command when complete.
369 void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
370 enum dynevent_type type,
371 dynevent_create_fn_t run_command)
373 memset(cmd, '\0', sizeof(*cmd));
375 seq_buf_init(&cmd->seq, buf, maxlen);
377 cmd->run_command = run_command;
381 * dynevent_arg_init - Initialize a dynevent_arg object
382 * @arg: A pointer to the dynevent_arg struct representing the arg
383 * @separator: An (optional) separator, appended after adding the arg
385 * Initialize a dynevent_arg object. A dynevent_arg represents an
386 * object used to append single arguments to the current command
387 * string. After the arg string is successfully appended to the
388 * command string, the optional @separator is appended. If no
389 * separator was specified when initializing the arg, a space will be
392 void dynevent_arg_init(struct dynevent_arg *arg,
395 memset(arg, '\0', sizeof(*arg));
399 arg->separator = separator;
403 * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
404 * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
405 * @operator: An (optional) operator, appended after adding the first arg
406 * @separator: An (optional) separator, appended after adding the second arg
408 * Initialize a dynevent_arg_pair object. A dynevent_arg_pair
409 * represents an object used to append argument pairs such as 'type
410 * variable_name;' or 'x+y' to the current command string. An
411 * argument pair consists of a left-hand-side argument and a
412 * right-hand-side argument separated by an operator, which can be
413 * whitespace, all followed by a separator, if applicable. After the
414 * first arg string is successfully appended to the command string,
415 * the optional @operator is appended, followed by the second arg and
416 * optional @separator. If no separator was specified when
417 * initializing the arg, a space will be appended.
419 void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
420 char operator, char separator)
422 memset(arg_pair, '\0', sizeof(*arg_pair));
426 arg_pair->operator = operator;
430 arg_pair->separator = separator;
434 * dynevent_create - Create the dynamic event contained in dynevent_cmd
435 * @cmd: The dynevent_cmd object containing the dynamic event creation command
437 * Once a dynevent_cmd object has been successfully built up via the
438 * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
439 * functions, this function runs the final command to actually create
442 * Return: 0 if the event was successfully created, error otherwise.
444 int dynevent_create(struct dynevent_cmd *cmd)
446 return cmd->run_command(cmd);
448 EXPORT_SYMBOL_GPL(dynevent_create);