Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Generic dynamic event control interface
  4 *
  5 * Copyright (C) 2018 Masami Hiramatsu <mhiramat@kernel.org>
  6 */
  7
  8#include <linux/debugfs.h>
  9#include <linux/kernel.h>
 10#include <linux/list.h>
 11#include <linux/mm.h>
 12#include <linux/mutex.h>
 13#include <linux/tracefs.h>
 14
 15#include "trace.h"
 16#include "trace_output.h"	/* for trace_event_sem */
 17#include "trace_dynevent.h"
 18
 19static DEFINE_MUTEX(dyn_event_ops_mutex);
 20static LIST_HEAD(dyn_event_ops_list);
 21
 22bool trace_event_dyn_try_get_ref(struct trace_event_call *dyn_call)
 23{
 24	struct trace_event_call *call;
 25	bool ret = false;
 26
 27	if (WARN_ON_ONCE(!(dyn_call->flags & TRACE_EVENT_FL_DYNAMIC)))
 28		return false;
 29
 30	down_read(&trace_event_sem);
 31	list_for_each_entry(call, &ftrace_events, list) {
 32		if (call == dyn_call) {
 33			atomic_inc(&dyn_call->refcnt);
 34			ret = true;
 35		}
 36	}
 37	up_read(&trace_event_sem);
 38	return ret;
 39}
 40
 41void trace_event_dyn_put_ref(struct trace_event_call *call)
 42{
 43	if (WARN_ON_ONCE(!(call->flags & TRACE_EVENT_FL_DYNAMIC)))
 44		return;
 45
 46	if (WARN_ON_ONCE(atomic_read(&call->refcnt) <= 0)) {
 47		atomic_set(&call->refcnt, 0);
 48		return;
 49	}
 50
 51	atomic_dec(&call->refcnt);
 52}
 53
 54bool trace_event_dyn_busy(struct trace_event_call *call)
 55{
 56	return atomic_read(&call->refcnt) != 0;
 57}
 58
 59int dyn_event_register(struct dyn_event_operations *ops)
 60{
 61	if (!ops || !ops->create || !ops->show || !ops->is_busy ||
 62	    !ops->free || !ops->match)
 63		return -EINVAL;
 64
 65	INIT_LIST_HEAD(&ops->list);
 66	mutex_lock(&dyn_event_ops_mutex);
 67	list_add_tail(&ops->list, &dyn_event_ops_list);
 68	mutex_unlock(&dyn_event_ops_mutex);
 69	return 0;
 70}
 71
 72int dyn_event_release(const char *raw_command, struct dyn_event_operations *type)
 73{
 74	struct dyn_event *pos, *n;
 75	char *system = NULL, *event, *p;
 76	int argc, ret = -ENOENT;
 77	char **argv;
 78
 79	argv = argv_split(GFP_KERNEL, raw_command, &argc);
 80	if (!argv)
 81		return -ENOMEM;
 82
 83	if (argv[0][0] == '-') {
 84		if (argv[0][1] != ':') {
 85			ret = -EINVAL;
 86			goto out;
 87		}
 88		event = &argv[0][2];
 89	} else {
 90		event = strchr(argv[0], ':');
 91		if (!event) {
 92			ret = -EINVAL;
 93			goto out;
 94		}
 95		event++;
 96	}
 97
 98	p = strchr(event, '/');
 99	if (p) {
100		system = event;
101		event = p + 1;
102		*p = '\0';
103	}
104	if (!system && event[0] == '\0') {
105		ret = -EINVAL;
106		goto out;
107	}
108
109	mutex_lock(&event_mutex);
110	for_each_dyn_event_safe(pos, n) {
111		if (type && type != pos->ops)
112			continue;
113		if (!pos->ops->match(system, event,
114				argc - 1, (const char **)argv + 1, pos))
115			continue;
116
117		ret = pos->ops->free(pos);
118		if (ret)
119			break;
120	}
121	tracing_reset_all_online_cpus();
122	mutex_unlock(&event_mutex);
123out:
124	argv_free(argv);
125	return ret;
126}
127
128static int create_dyn_event(const char *raw_command)
129{
130	struct dyn_event_operations *ops;
131	int ret = -ENODEV;
132
133	if (raw_command[0] == '-' || raw_command[0] == '!')
134		return dyn_event_release(raw_command, NULL);
135
136	mutex_lock(&dyn_event_ops_mutex);
137	list_for_each_entry(ops, &dyn_event_ops_list, list) {
138		ret = ops->create(raw_command);
139		if (!ret || ret != -ECANCELED)
140			break;
141	}
142	mutex_unlock(&dyn_event_ops_mutex);
143	if (ret == -ECANCELED)
144		ret = -EINVAL;
145
146	return ret;
147}
148
149/* Protected by event_mutex */
150LIST_HEAD(dyn_event_list);
151
152void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
153{
154	mutex_lock(&event_mutex);
155	return seq_list_start(&dyn_event_list, *pos);
156}
157
158void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
159{
160	return seq_list_next(v, &dyn_event_list, pos);
161}
162
163void dyn_event_seq_stop(struct seq_file *m, void *v)
164{
165	mutex_unlock(&event_mutex);
166}
167
168static int dyn_event_seq_show(struct seq_file *m, void *v)
169{
170	struct dyn_event *ev = v;
171
172	if (ev && ev->ops)
173		return ev->ops->show(m, ev);
174
175	return 0;
176}
177
178static const struct seq_operations dyn_event_seq_op = {
179	.start	= dyn_event_seq_start,
180	.next	= dyn_event_seq_next,
181	.stop	= dyn_event_seq_stop,
182	.show	= dyn_event_seq_show
183};
184
185/*
186 * dyn_events_release_all - Release all specific events
187 * @type:	the dyn_event_operations * which filters releasing events
188 *
189 * This releases all events which ->ops matches @type. If @type is NULL,
190 * all events are released.
191 * Return -EBUSY if any of them are in use, and return other errors when
192 * it failed to free the given event. Except for -EBUSY, event releasing
193 * process will be aborted at that point and there may be some other
194 * releasable events on the list.
195 */
196int dyn_events_release_all(struct dyn_event_operations *type)
197{
198	struct dyn_event *ev, *tmp;
199	int ret = 0;
200
201	mutex_lock(&event_mutex);
202	for_each_dyn_event(ev) {
203		if (type && ev->ops != type)
204			continue;
205		if (ev->ops->is_busy(ev)) {
206			ret = -EBUSY;
207			goto out;
208		}
209	}
210	for_each_dyn_event_safe(ev, tmp) {
211		if (type && ev->ops != type)
212			continue;
213		ret = ev->ops->free(ev);
214		if (ret)
215			break;
216	}
217out:
218	tracing_reset_all_online_cpus();
219	mutex_unlock(&event_mutex);
220
221	return ret;
222}
223
224static int dyn_event_open(struct inode *inode, struct file *file)
225{
226	int ret;
227
228	ret = tracing_check_open_get_tr(NULL);
229	if (ret)
230		return ret;
231
232	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
233		ret = dyn_events_release_all(NULL);
234		if (ret < 0)
235			return ret;
236	}
237
238	return seq_open(file, &dyn_event_seq_op);
239}
240
241static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
242				size_t count, loff_t *ppos)
243{
244	return trace_parse_run_command(file, buffer, count, ppos,
245				       create_dyn_event);
246}
247
248static const struct file_operations dynamic_events_ops = {
249	.owner          = THIS_MODULE,
250	.open           = dyn_event_open,
251	.read           = seq_read,
252	.llseek         = seq_lseek,
253	.release        = seq_release,
254	.write		= dyn_event_write,
255};
256
257/* Make a tracefs interface for controlling dynamic events */
258static __init int init_dynamic_event(void)
259{
 
260	int ret;
261
262	ret = tracing_init_dentry();
263	if (ret)
264		return 0;
265
266	trace_create_file("dynamic_events", TRACE_MODE_WRITE, NULL,
267			  NULL, &dynamic_events_ops);
 
 
 
 
268
269	return 0;
270}
271fs_initcall(init_dynamic_event);
272
273/**
274 * dynevent_arg_add - Add an arg to a dynevent_cmd
275 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
276 * @arg: The argument to append to the current cmd
277 * @check_arg: An (optional) pointer to a function checking arg sanity
278 *
279 * Append an argument to a dynevent_cmd.  The argument string will be
280 * appended to the current cmd string, followed by a separator, if
281 * applicable.  Before the argument is added, the @check_arg function,
282 * if present, will be used to check the sanity of the current arg
283 * string.
284 *
285 * The cmd string and separator should be set using the
286 * dynevent_arg_init() before any arguments are added using this
287 * function.
288 *
289 * Return: 0 if successful, error otherwise.
290 */
291int dynevent_arg_add(struct dynevent_cmd *cmd,
292		     struct dynevent_arg *arg,
293		     dynevent_check_arg_fn_t check_arg)
294{
295	int ret = 0;
296
297	if (check_arg) {
298		ret = check_arg(arg);
299		if (ret)
300			return ret;
301	}
302
303	ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
304	if (ret) {
305		pr_err("String is too long: %s%c\n", arg->str, arg->separator);
306		return -E2BIG;
307	}
308
309	return ret;
310}
311
312/**
313 * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
314 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
315 * @arg_pair: The argument pair to append to the current cmd
316 * @check_arg: An (optional) pointer to a function checking arg sanity
317 *
318 * Append an argument pair to a dynevent_cmd.  An argument pair
319 * consists of a left-hand-side argument and a right-hand-side
320 * argument separated by an operator, which can be whitespace, all
321 * followed by a separator, if applicable.  This can be used to add
322 * arguments of the form 'type variable_name;' or 'x+y'.
323 *
324 * The lhs argument string will be appended to the current cmd string,
325 * followed by an operator, if applicable, followed by the rhs string,
326 * followed finally by a separator, if applicable.  Before the
327 * argument is added, the @check_arg function, if present, will be
328 * used to check the sanity of the current arg strings.
329 *
330 * The cmd strings, operator, and separator should be set using the
331 * dynevent_arg_pair_init() before any arguments are added using this
332 * function.
333 *
334 * Return: 0 if successful, error otherwise.
335 */
336int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
337			  struct dynevent_arg_pair *arg_pair,
338			  dynevent_check_arg_fn_t check_arg)
339{
340	int ret = 0;
341
342	if (check_arg) {
343		ret = check_arg(arg_pair);
344		if (ret)
345			return ret;
346	}
347
348	ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
349			     arg_pair->operator, arg_pair->rhs,
350			     arg_pair->separator);
351	if (ret) {
352		pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
353		       arg_pair->operator, arg_pair->rhs,
354		       arg_pair->separator);
355		return -E2BIG;
356	}
357
358	return ret;
359}
360
361/**
362 * dynevent_str_add - Add a string to a dynevent_cmd
363 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
364 * @str: The string to append to the current cmd
365 *
366 * Append a string to a dynevent_cmd.  The string will be appended to
367 * the current cmd string as-is, with nothing prepended or appended.
368 *
369 * Return: 0 if successful, error otherwise.
370 */
371int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
372{
373	int ret = 0;
374
375	ret = seq_buf_puts(&cmd->seq, str);
376	if (ret) {
377		pr_err("String is too long: %s\n", str);
378		return -E2BIG;
379	}
380
381	return ret;
382}
383
384/**
385 * dynevent_cmd_init - Initialize a dynevent_cmd object
386 * @cmd: A pointer to the dynevent_cmd struct representing the cmd
387 * @buf: A pointer to the buffer to generate the command into
388 * @maxlen: The length of the buffer the command will be generated into
389 * @type: The type of the cmd, checked against further operations
390 * @run_command: The type-specific function that will actually run the command
391 *
392 * Initialize a dynevent_cmd.  A dynevent_cmd is used to build up and
393 * run dynamic event creation commands, such as commands for creating
394 * synthetic and kprobe events.  Before calling any of the functions
395 * used to build the command, a dynevent_cmd object should be
396 * instantiated and initialized using this function.
397 *
398 * The initialization sets things up by saving a pointer to the
399 * user-supplied buffer and its length via the @buf and @maxlen
400 * params, and by saving the cmd-specific @type and @run_command
401 * params which are used to check subsequent dynevent_cmd operations
402 * and actually run the command when complete.
403 */
404void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
405		       enum dynevent_type type,
406		       dynevent_create_fn_t run_command)
407{
408	memset(cmd, '\0', sizeof(*cmd));
409
410	seq_buf_init(&cmd->seq, buf, maxlen);
411	cmd->type = type;
412	cmd->run_command = run_command;
413}
414
415/**
416 * dynevent_arg_init - Initialize a dynevent_arg object
417 * @arg: A pointer to the dynevent_arg struct representing the arg
418 * @separator: An (optional) separator, appended after adding the arg
419 *
420 * Initialize a dynevent_arg object.  A dynevent_arg represents an
421 * object used to append single arguments to the current command
422 * string.  After the arg string is successfully appended to the
423 * command string, the optional @separator is appended.  If no
424 * separator was specified when initializing the arg, a space will be
425 * appended.
426 */
427void dynevent_arg_init(struct dynevent_arg *arg,
428		       char separator)
429{
430	memset(arg, '\0', sizeof(*arg));
431
432	if (!separator)
433		separator = ' ';
434	arg->separator = separator;
435}
436
437/**
438 * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
439 * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
440 * @operator: An (optional) operator, appended after adding the first arg
441 * @separator: An (optional) separator, appended after adding the second arg
442 *
443 * Initialize a dynevent_arg_pair object.  A dynevent_arg_pair
444 * represents an object used to append argument pairs such as 'type
445 * variable_name;' or 'x+y' to the current command string.  An
446 * argument pair consists of a left-hand-side argument and a
447 * right-hand-side argument separated by an operator, which can be
448 * whitespace, all followed by a separator, if applicable.  After the
449 * first arg string is successfully appended to the command string,
450 * the optional @operator is appended, followed by the second arg and
451 * optional @separator.  If no separator was specified when
452 * initializing the arg, a space will be appended.
453 */
454void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
455			    char operator, char separator)
456{
457	memset(arg_pair, '\0', sizeof(*arg_pair));
458
459	if (!operator)
460		operator = ' ';
461	arg_pair->operator = operator;
462
463	if (!separator)
464		separator = ' ';
465	arg_pair->separator = separator;
466}
467
468/**
469 * dynevent_create - Create the dynamic event contained in dynevent_cmd
470 * @cmd: The dynevent_cmd object containing the dynamic event creation command
471 *
472 * Once a dynevent_cmd object has been successfully built up via the
473 * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
474 * functions, this function runs the final command to actually create
475 * the event.
476 *
477 * Return: 0 if the event was successfully created, error otherwise.
478 */
479int dynevent_create(struct dynevent_cmd *cmd)
480{
481	return cmd->run_command(cmd);
482}
483EXPORT_SYMBOL_GPL(dynevent_create);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Generic dynamic event control interface
  4 *
  5 * Copyright (C) 2018 Masami Hiramatsu <mhiramat@kernel.org>
  6 */
  7
  8#include <linux/debugfs.h>
  9#include <linux/kernel.h>
 10#include <linux/list.h>
 11#include <linux/mm.h>
 12#include <linux/mutex.h>
 13#include <linux/tracefs.h>
 14
 15#include "trace.h"
 
 16#include "trace_dynevent.h"
 17
 18static DEFINE_MUTEX(dyn_event_ops_mutex);
 19static LIST_HEAD(dyn_event_ops_list);
 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 21int dyn_event_register(struct dyn_event_operations *ops)
 22{
 23	if (!ops || !ops->create || !ops->show || !ops->is_busy ||
 24	    !ops->free || !ops->match)
 25		return -EINVAL;
 26
 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);
 31	return 0;
 32}
 33
 34int dyn_event_release(const char *raw_command, struct dyn_event_operations *type)
 35{
 36	struct dyn_event *pos, *n;
 37	char *system = NULL, *event, *p;
 38	int argc, ret = -ENOENT;
 39	char **argv;
 40
 41	argv = argv_split(GFP_KERNEL, raw_command, &argc);
 42	if (!argv)
 43		return -ENOMEM;
 44
 45	if (argv[0][0] == '-') {
 46		if (argv[0][1] != ':') {
 47			ret = -EINVAL;
 48			goto out;
 49		}
 50		event = &argv[0][2];
 51	} else {
 52		event = strchr(argv[0], ':');
 53		if (!event) {
 54			ret = -EINVAL;
 55			goto out;
 56		}
 57		event++;
 58	}
 59
 60	p = strchr(event, '/');
 61	if (p) {
 62		system = event;
 63		event = p + 1;
 64		*p = '\0';
 65	}
 66	if (event[0] == '\0') {
 67		ret = -EINVAL;
 68		goto out;
 69	}
 70
 71	mutex_lock(&event_mutex);
 72	for_each_dyn_event_safe(pos, n) {
 73		if (type && type != pos->ops)
 74			continue;
 75		if (!pos->ops->match(system, event,
 76				argc - 1, (const char **)argv + 1, pos))
 77			continue;
 78
 79		ret = pos->ops->free(pos);
 80		if (ret)
 81			break;
 82	}
 
 83	mutex_unlock(&event_mutex);
 84out:
 85	argv_free(argv);
 86	return ret;
 87}
 88
 89static int create_dyn_event(const char *raw_command)
 90{
 91	struct dyn_event_operations *ops;
 92	int ret = -ENODEV;
 93
 94	if (raw_command[0] == '-' || raw_command[0] == '!')
 95		return dyn_event_release(raw_command, NULL);
 96
 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)
101			break;
102	}
103	mutex_unlock(&dyn_event_ops_mutex);
104	if (ret == -ECANCELED)
105		ret = -EINVAL;
106
107	return ret;
108}
109
110/* Protected by event_mutex */
111LIST_HEAD(dyn_event_list);
112
113void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
114{
115	mutex_lock(&event_mutex);
116	return seq_list_start(&dyn_event_list, *pos);
117}
118
119void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
120{
121	return seq_list_next(v, &dyn_event_list, pos);
122}
123
124void dyn_event_seq_stop(struct seq_file *m, void *v)
125{
126	mutex_unlock(&event_mutex);
127}
128
129static int dyn_event_seq_show(struct seq_file *m, void *v)
130{
131	struct dyn_event *ev = v;
132
133	if (ev && ev->ops)
134		return ev->ops->show(m, ev);
135
136	return 0;
137}
138
139static 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
144};
145
146/*
147 * dyn_events_release_all - Release all specific events
148 * @type:	the dyn_event_operations * which filters releasing events
149 *
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.
156 */
157int dyn_events_release_all(struct dyn_event_operations *type)
158{
159	struct dyn_event *ev, *tmp;
160	int ret = 0;
161
162	mutex_lock(&event_mutex);
163	for_each_dyn_event(ev) {
164		if (type && ev->ops != type)
165			continue;
166		if (ev->ops->is_busy(ev)) {
167			ret = -EBUSY;
168			goto out;
169		}
170	}
171	for_each_dyn_event_safe(ev, tmp) {
172		if (type && ev->ops != type)
173			continue;
174		ret = ev->ops->free(ev);
175		if (ret)
176			break;
177	}
178out:
 
179	mutex_unlock(&event_mutex);
180
181	return ret;
182}
183
184static int dyn_event_open(struct inode *inode, struct file *file)
185{
186	int ret;
187
188	ret = tracing_check_open_get_tr(NULL);
189	if (ret)
190		return ret;
191
192	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
193		ret = dyn_events_release_all(NULL);
194		if (ret < 0)
195			return ret;
196	}
197
198	return seq_open(file, &dyn_event_seq_op);
199}
200
201static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
202				size_t count, loff_t *ppos)
203{
204	return trace_parse_run_command(file, buffer, count, ppos,
205				       create_dyn_event);
206}
207
208static const struct file_operations dynamic_events_ops = {
209	.owner          = THIS_MODULE,
210	.open           = dyn_event_open,
211	.read           = seq_read,
212	.llseek         = seq_lseek,
213	.release        = seq_release,
214	.write		= dyn_event_write,
215};
216
217/* Make a tracefs interface for controlling dynamic events */
218static __init int init_dynamic_event(void)
219{
220	struct dentry *entry;
221	int ret;
222
223	ret = tracing_init_dentry();
224	if (ret)
225		return 0;
226
227	entry = tracefs_create_file("dynamic_events", 0644, NULL,
228				    NULL, &dynamic_events_ops);
229
230	/* Event list interface */
231	if (!entry)
232		pr_warn("Could not create tracefs 'dynamic_events' entry\n");
233
234	return 0;
235}
236fs_initcall(init_dynamic_event);
237
238/**
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
243 *
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
248 * string.
249 *
250 * The cmd string and separator should be set using the
251 * dynevent_arg_init() before any arguments are added using this
252 * function.
253 *
254 * Return: 0 if successful, error otherwise.
255 */
256int dynevent_arg_add(struct dynevent_cmd *cmd,
257		     struct dynevent_arg *arg,
258		     dynevent_check_arg_fn_t check_arg)
259{
260	int ret = 0;
261
262	if (check_arg) {
263		ret = check_arg(arg);
264		if (ret)
265			return ret;
266	}
267
268	ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
269	if (ret) {
270		pr_err("String is too long: %s%c\n", arg->str, arg->separator);
271		return -E2BIG;
272	}
273
274	return ret;
275}
276
277/**
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
282 *
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'.
288 *
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.
294 *
295 * The cmd strings, operator, and separator should be set using the
296 * dynevent_arg_pair_init() before any arguments are added using this
297 * function.
298 *
299 * Return: 0 if successful, error otherwise.
300 */
301int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
302			  struct dynevent_arg_pair *arg_pair,
303			  dynevent_check_arg_fn_t check_arg)
304{
305	int ret = 0;
306
307	if (check_arg) {
308		ret = check_arg(arg_pair);
309		if (ret)
310			return ret;
311	}
312
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);
316	if (ret) {
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);
320		return -E2BIG;
321	}
322
323	return ret;
324}
325
326/**
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
330 *
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.
333 *
334 * Return: 0 if successful, error otherwise.
335 */
336int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
337{
338	int ret = 0;
339
340	ret = seq_buf_puts(&cmd->seq, str);
341	if (ret) {
342		pr_err("String is too long: %s\n", str);
343		return -E2BIG;
344	}
345
346	return ret;
347}
348
349/**
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
356 *
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.
362 *
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.
368 */
369void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
370		       enum dynevent_type type,
371		       dynevent_create_fn_t run_command)
372{
373	memset(cmd, '\0', sizeof(*cmd));
374
375	seq_buf_init(&cmd->seq, buf, maxlen);
376	cmd->type = type;
377	cmd->run_command = run_command;
378}
379
380/**
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
384 *
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
390 * appended.
391 */
392void dynevent_arg_init(struct dynevent_arg *arg,
393		       char separator)
394{
395	memset(arg, '\0', sizeof(*arg));
396
397	if (!separator)
398		separator = ' ';
399	arg->separator = separator;
400}
401
402/**
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
407 *
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.
418 */
419void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
420			    char operator, char separator)
421{
422	memset(arg_pair, '\0', sizeof(*arg_pair));
423
424	if (!operator)
425		operator = ' ';
426	arg_pair->operator = operator;
427
428	if (!separator)
429		separator = ' ';
430	arg_pair->separator = separator;
431}
432
433/**
434 * dynevent_create - Create the dynamic event contained in dynevent_cmd
435 * @cmd: The dynevent_cmd object containing the dynamic event creation command
436 *
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
440 * the event.
441 *
442 * Return: 0 if the event was successfully created, error otherwise.
443 */
444int dynevent_create(struct dynevent_cmd *cmd)
445{
446	return cmd->run_command(cmd);
447}
448EXPORT_SYMBOL_GPL(dynevent_create);