dt-bindings: pinctrl: add bindings for MediaTek MT6779 SoC
[linux/fpc-iii.git] / kernel / trace / trace_events_synth.c
blobc6cca0d1d5840ac2754b1f8280038dd754bba53d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace_events_synth - synthetic trace events
5 * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
6 */
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
21 #include "trace_synth.h"
23 static int create_synth_event(int argc, const char **argv);
24 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
25 static int synth_event_release(struct dyn_event *ev);
26 static bool synth_event_is_busy(struct dyn_event *ev);
27 static bool synth_event_match(const char *system, const char *event,
28 int argc, const char **argv, struct dyn_event *ev);
30 static struct dyn_event_operations synth_event_ops = {
31 .create = create_synth_event,
32 .show = synth_event_show,
33 .is_busy = synth_event_is_busy,
34 .free = synth_event_release,
35 .match = synth_event_match,
38 static bool is_synth_event(struct dyn_event *ev)
40 return ev->ops == &synth_event_ops;
43 static struct synth_event *to_synth_event(struct dyn_event *ev)
45 return container_of(ev, struct synth_event, devent);
48 static bool synth_event_is_busy(struct dyn_event *ev)
50 struct synth_event *event = to_synth_event(ev);
52 return event->ref != 0;
55 static bool synth_event_match(const char *system, const char *event,
56 int argc, const char **argv, struct dyn_event *ev)
58 struct synth_event *sev = to_synth_event(ev);
60 return strcmp(sev->name, event) == 0 &&
61 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
64 struct synth_trace_event {
65 struct trace_entry ent;
66 u64 fields[];
69 static int synth_event_define_fields(struct trace_event_call *call)
71 struct synth_trace_event trace;
72 int offset = offsetof(typeof(trace), fields);
73 struct synth_event *event = call->data;
74 unsigned int i, size, n_u64;
75 char *name, *type;
76 bool is_signed;
77 int ret = 0;
79 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
80 size = event->fields[i]->size;
81 is_signed = event->fields[i]->is_signed;
82 type = event->fields[i]->type;
83 name = event->fields[i]->name;
84 ret = trace_define_field(call, type, name, offset, size,
85 is_signed, FILTER_OTHER);
86 if (ret)
87 break;
89 event->fields[i]->offset = n_u64;
91 if (event->fields[i]->is_string) {
92 offset += STR_VAR_LEN_MAX;
93 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
94 } else {
95 offset += sizeof(u64);
96 n_u64++;
100 event->n_u64 = n_u64;
102 return ret;
105 static bool synth_field_signed(char *type)
107 if (str_has_prefix(type, "u"))
108 return false;
109 if (strcmp(type, "gfp_t") == 0)
110 return false;
112 return true;
115 static int synth_field_is_string(char *type)
117 if (strstr(type, "char[") != NULL)
118 return true;
120 return false;
123 static int synth_field_string_size(char *type)
125 char buf[4], *end, *start;
126 unsigned int len;
127 int size, err;
129 start = strstr(type, "char[");
130 if (start == NULL)
131 return -EINVAL;
132 start += sizeof("char[") - 1;
134 end = strchr(type, ']');
135 if (!end || end < start)
136 return -EINVAL;
138 len = end - start;
139 if (len > 3)
140 return -EINVAL;
142 strncpy(buf, start, len);
143 buf[len] = '\0';
145 err = kstrtouint(buf, 0, &size);
146 if (err)
147 return err;
149 if (size > STR_VAR_LEN_MAX)
150 return -EINVAL;
152 return size;
155 static int synth_field_size(char *type)
157 int size = 0;
159 if (strcmp(type, "s64") == 0)
160 size = sizeof(s64);
161 else if (strcmp(type, "u64") == 0)
162 size = sizeof(u64);
163 else if (strcmp(type, "s32") == 0)
164 size = sizeof(s32);
165 else if (strcmp(type, "u32") == 0)
166 size = sizeof(u32);
167 else if (strcmp(type, "s16") == 0)
168 size = sizeof(s16);
169 else if (strcmp(type, "u16") == 0)
170 size = sizeof(u16);
171 else if (strcmp(type, "s8") == 0)
172 size = sizeof(s8);
173 else if (strcmp(type, "u8") == 0)
174 size = sizeof(u8);
175 else if (strcmp(type, "char") == 0)
176 size = sizeof(char);
177 else if (strcmp(type, "unsigned char") == 0)
178 size = sizeof(unsigned char);
179 else if (strcmp(type, "int") == 0)
180 size = sizeof(int);
181 else if (strcmp(type, "unsigned int") == 0)
182 size = sizeof(unsigned int);
183 else if (strcmp(type, "long") == 0)
184 size = sizeof(long);
185 else if (strcmp(type, "unsigned long") == 0)
186 size = sizeof(unsigned long);
187 else if (strcmp(type, "pid_t") == 0)
188 size = sizeof(pid_t);
189 else if (strcmp(type, "gfp_t") == 0)
190 size = sizeof(gfp_t);
191 else if (synth_field_is_string(type))
192 size = synth_field_string_size(type);
194 return size;
197 static const char *synth_field_fmt(char *type)
199 const char *fmt = "%llu";
201 if (strcmp(type, "s64") == 0)
202 fmt = "%lld";
203 else if (strcmp(type, "u64") == 0)
204 fmt = "%llu";
205 else if (strcmp(type, "s32") == 0)
206 fmt = "%d";
207 else if (strcmp(type, "u32") == 0)
208 fmt = "%u";
209 else if (strcmp(type, "s16") == 0)
210 fmt = "%d";
211 else if (strcmp(type, "u16") == 0)
212 fmt = "%u";
213 else if (strcmp(type, "s8") == 0)
214 fmt = "%d";
215 else if (strcmp(type, "u8") == 0)
216 fmt = "%u";
217 else if (strcmp(type, "char") == 0)
218 fmt = "%d";
219 else if (strcmp(type, "unsigned char") == 0)
220 fmt = "%u";
221 else if (strcmp(type, "int") == 0)
222 fmt = "%d";
223 else if (strcmp(type, "unsigned int") == 0)
224 fmt = "%u";
225 else if (strcmp(type, "long") == 0)
226 fmt = "%ld";
227 else if (strcmp(type, "unsigned long") == 0)
228 fmt = "%lu";
229 else if (strcmp(type, "pid_t") == 0)
230 fmt = "%d";
231 else if (strcmp(type, "gfp_t") == 0)
232 fmt = "%x";
233 else if (synth_field_is_string(type))
234 fmt = "%s";
236 return fmt;
239 static void print_synth_event_num_val(struct trace_seq *s,
240 char *print_fmt, char *name,
241 int size, u64 val, char *space)
243 switch (size) {
244 case 1:
245 trace_seq_printf(s, print_fmt, name, (u8)val, space);
246 break;
248 case 2:
249 trace_seq_printf(s, print_fmt, name, (u16)val, space);
250 break;
252 case 4:
253 trace_seq_printf(s, print_fmt, name, (u32)val, space);
254 break;
256 default:
257 trace_seq_printf(s, print_fmt, name, val, space);
258 break;
262 static enum print_line_t print_synth_event(struct trace_iterator *iter,
263 int flags,
264 struct trace_event *event)
266 struct trace_array *tr = iter->tr;
267 struct trace_seq *s = &iter->seq;
268 struct synth_trace_event *entry;
269 struct synth_event *se;
270 unsigned int i, n_u64;
271 char print_fmt[32];
272 const char *fmt;
274 entry = (struct synth_trace_event *)iter->ent;
275 se = container_of(event, struct synth_event, call.event);
277 trace_seq_printf(s, "%s: ", se->name);
279 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
280 if (trace_seq_has_overflowed(s))
281 goto end;
283 fmt = synth_field_fmt(se->fields[i]->type);
285 /* parameter types */
286 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
287 trace_seq_printf(s, "%s ", fmt);
289 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
291 /* parameter values */
292 if (se->fields[i]->is_string) {
293 trace_seq_printf(s, print_fmt, se->fields[i]->name,
294 (char *)&entry->fields[n_u64],
295 i == se->n_fields - 1 ? "" : " ");
296 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
297 } else {
298 struct trace_print_flags __flags[] = {
299 __def_gfpflag_names, {-1, NULL} };
300 char *space = (i == se->n_fields - 1 ? "" : " ");
302 print_synth_event_num_val(s, print_fmt,
303 se->fields[i]->name,
304 se->fields[i]->size,
305 entry->fields[n_u64],
306 space);
308 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
309 trace_seq_puts(s, " (");
310 trace_print_flags_seq(s, "|",
311 entry->fields[n_u64],
312 __flags);
313 trace_seq_putc(s, ')');
315 n_u64++;
318 end:
319 trace_seq_putc(s, '\n');
321 return trace_handle_return(s);
324 static struct trace_event_functions synth_event_funcs = {
325 .trace = print_synth_event
328 static notrace void trace_event_raw_event_synth(void *__data,
329 u64 *var_ref_vals,
330 unsigned int *var_ref_idx)
332 struct trace_event_file *trace_file = __data;
333 struct synth_trace_event *entry;
334 struct trace_event_buffer fbuffer;
335 struct trace_buffer *buffer;
336 struct synth_event *event;
337 unsigned int i, n_u64, val_idx;
338 int fields_size = 0;
340 event = trace_file->event_call->data;
342 if (trace_trigger_soft_disabled(trace_file))
343 return;
345 fields_size = event->n_u64 * sizeof(u64);
348 * Avoid ring buffer recursion detection, as this event
349 * is being performed within another event.
351 buffer = trace_file->tr->array_buffer.buffer;
352 ring_buffer_nest_start(buffer);
354 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
355 sizeof(*entry) + fields_size);
356 if (!entry)
357 goto out;
359 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
360 val_idx = var_ref_idx[i];
361 if (event->fields[i]->is_string) {
362 char *str_val = (char *)(long)var_ref_vals[val_idx];
363 char *str_field = (char *)&entry->fields[n_u64];
365 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
366 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
367 } else {
368 struct synth_field *field = event->fields[i];
369 u64 val = var_ref_vals[val_idx];
371 switch (field->size) {
372 case 1:
373 *(u8 *)&entry->fields[n_u64] = (u8)val;
374 break;
376 case 2:
377 *(u16 *)&entry->fields[n_u64] = (u16)val;
378 break;
380 case 4:
381 *(u32 *)&entry->fields[n_u64] = (u32)val;
382 break;
384 default:
385 entry->fields[n_u64] = val;
386 break;
388 n_u64++;
392 trace_event_buffer_commit(&fbuffer);
393 out:
394 ring_buffer_nest_end(buffer);
397 static void free_synth_event_print_fmt(struct trace_event_call *call)
399 if (call) {
400 kfree(call->print_fmt);
401 call->print_fmt = NULL;
405 static int __set_synth_event_print_fmt(struct synth_event *event,
406 char *buf, int len)
408 const char *fmt;
409 int pos = 0;
410 int i;
412 /* When len=0, we just calculate the needed length */
413 #define LEN_OR_ZERO (len ? len - pos : 0)
415 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
416 for (i = 0; i < event->n_fields; i++) {
417 fmt = synth_field_fmt(event->fields[i]->type);
418 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
419 event->fields[i]->name, fmt,
420 i == event->n_fields - 1 ? "" : ", ");
422 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
424 for (i = 0; i < event->n_fields; i++) {
425 pos += snprintf(buf + pos, LEN_OR_ZERO,
426 ", REC->%s", event->fields[i]->name);
429 #undef LEN_OR_ZERO
431 /* return the length of print_fmt */
432 return pos;
435 static int set_synth_event_print_fmt(struct trace_event_call *call)
437 struct synth_event *event = call->data;
438 char *print_fmt;
439 int len;
441 /* First: called with 0 length to calculate the needed length */
442 len = __set_synth_event_print_fmt(event, NULL, 0);
444 print_fmt = kmalloc(len + 1, GFP_KERNEL);
445 if (!print_fmt)
446 return -ENOMEM;
448 /* Second: actually write the @print_fmt */
449 __set_synth_event_print_fmt(event, print_fmt, len + 1);
450 call->print_fmt = print_fmt;
452 return 0;
455 static void free_synth_field(struct synth_field *field)
457 kfree(field->type);
458 kfree(field->name);
459 kfree(field);
462 static struct synth_field *parse_synth_field(int argc, const char **argv,
463 int *consumed)
465 struct synth_field *field;
466 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
467 int len, ret = 0;
469 if (field_type[0] == ';')
470 field_type++;
472 if (!strcmp(field_type, "unsigned")) {
473 if (argc < 3)
474 return ERR_PTR(-EINVAL);
475 prefix = "unsigned ";
476 field_type = argv[1];
477 field_name = argv[2];
478 *consumed = 3;
479 } else {
480 field_name = argv[1];
481 *consumed = 2;
484 field = kzalloc(sizeof(*field), GFP_KERNEL);
485 if (!field)
486 return ERR_PTR(-ENOMEM);
488 len = strlen(field_name);
489 array = strchr(field_name, '[');
490 if (array)
491 len -= strlen(array);
492 else if (field_name[len - 1] == ';')
493 len--;
495 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
496 if (!field->name) {
497 ret = -ENOMEM;
498 goto free;
501 if (field_type[0] == ';')
502 field_type++;
503 len = strlen(field_type) + 1;
504 if (array)
505 len += strlen(array);
506 if (prefix)
507 len += strlen(prefix);
509 field->type = kzalloc(len, GFP_KERNEL);
510 if (!field->type) {
511 ret = -ENOMEM;
512 goto free;
514 if (prefix)
515 strcat(field->type, prefix);
516 strcat(field->type, field_type);
517 if (array) {
518 strcat(field->type, array);
519 if (field->type[len - 1] == ';')
520 field->type[len - 1] = '\0';
523 field->size = synth_field_size(field->type);
524 if (!field->size) {
525 ret = -EINVAL;
526 goto free;
529 if (synth_field_is_string(field->type))
530 field->is_string = true;
532 field->is_signed = synth_field_signed(field->type);
534 out:
535 return field;
536 free:
537 free_synth_field(field);
538 field = ERR_PTR(ret);
539 goto out;
542 static void free_synth_tracepoint(struct tracepoint *tp)
544 if (!tp)
545 return;
547 kfree(tp->name);
548 kfree(tp);
551 static struct tracepoint *alloc_synth_tracepoint(char *name)
553 struct tracepoint *tp;
555 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
556 if (!tp)
557 return ERR_PTR(-ENOMEM);
559 tp->name = kstrdup(name, GFP_KERNEL);
560 if (!tp->name) {
561 kfree(tp);
562 return ERR_PTR(-ENOMEM);
565 return tp;
568 struct synth_event *find_synth_event(const char *name)
570 struct dyn_event *pos;
571 struct synth_event *event;
573 for_each_dyn_event(pos) {
574 if (!is_synth_event(pos))
575 continue;
576 event = to_synth_event(pos);
577 if (strcmp(event->name, name) == 0)
578 return event;
581 return NULL;
584 static struct trace_event_fields synth_event_fields_array[] = {
585 { .type = TRACE_FUNCTION_TYPE,
586 .define_fields = synth_event_define_fields },
590 static int register_synth_event(struct synth_event *event)
592 struct trace_event_call *call = &event->call;
593 int ret = 0;
595 event->call.class = &event->class;
596 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
597 if (!event->class.system) {
598 ret = -ENOMEM;
599 goto out;
602 event->tp = alloc_synth_tracepoint(event->name);
603 if (IS_ERR(event->tp)) {
604 ret = PTR_ERR(event->tp);
605 event->tp = NULL;
606 goto out;
609 INIT_LIST_HEAD(&call->class->fields);
610 call->event.funcs = &synth_event_funcs;
611 call->class->fields_array = synth_event_fields_array;
613 ret = register_trace_event(&call->event);
614 if (!ret) {
615 ret = -ENODEV;
616 goto out;
618 call->flags = TRACE_EVENT_FL_TRACEPOINT;
619 call->class->reg = trace_event_reg;
620 call->class->probe = trace_event_raw_event_synth;
621 call->data = event;
622 call->tp = event->tp;
624 ret = trace_add_event_call(call);
625 if (ret) {
626 pr_warn("Failed to register synthetic event: %s\n",
627 trace_event_name(call));
628 goto err;
631 ret = set_synth_event_print_fmt(call);
632 if (ret < 0) {
633 trace_remove_event_call(call);
634 goto err;
636 out:
637 return ret;
638 err:
639 unregister_trace_event(&call->event);
640 goto out;
643 static int unregister_synth_event(struct synth_event *event)
645 struct trace_event_call *call = &event->call;
646 int ret;
648 ret = trace_remove_event_call(call);
650 return ret;
653 static void free_synth_event(struct synth_event *event)
655 unsigned int i;
657 if (!event)
658 return;
660 for (i = 0; i < event->n_fields; i++)
661 free_synth_field(event->fields[i]);
663 kfree(event->fields);
664 kfree(event->name);
665 kfree(event->class.system);
666 free_synth_tracepoint(event->tp);
667 free_synth_event_print_fmt(&event->call);
668 kfree(event);
671 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
672 struct synth_field **fields)
674 struct synth_event *event;
675 unsigned int i;
677 event = kzalloc(sizeof(*event), GFP_KERNEL);
678 if (!event) {
679 event = ERR_PTR(-ENOMEM);
680 goto out;
683 event->name = kstrdup(name, GFP_KERNEL);
684 if (!event->name) {
685 kfree(event);
686 event = ERR_PTR(-ENOMEM);
687 goto out;
690 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
691 if (!event->fields) {
692 free_synth_event(event);
693 event = ERR_PTR(-ENOMEM);
694 goto out;
697 dyn_event_init(&event->devent, &synth_event_ops);
699 for (i = 0; i < n_fields; i++)
700 event->fields[i] = fields[i];
702 event->n_fields = n_fields;
703 out:
704 return event;
707 static int synth_event_check_arg_fn(void *data)
709 struct dynevent_arg_pair *arg_pair = data;
710 int size;
712 size = synth_field_size((char *)arg_pair->lhs);
714 return size ? 0 : -EINVAL;
718 * synth_event_add_field - Add a new field to a synthetic event cmd
719 * @cmd: A pointer to the dynevent_cmd struct representing the new event
720 * @type: The type of the new field to add
721 * @name: The name of the new field to add
723 * Add a new field to a synthetic event cmd object. Field ordering is in
724 * the same order the fields are added.
726 * See synth_field_size() for available types. If field_name contains
727 * [n] the field is considered to be an array.
729 * Return: 0 if successful, error otherwise.
731 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
732 const char *name)
734 struct dynevent_arg_pair arg_pair;
735 int ret;
737 if (cmd->type != DYNEVENT_TYPE_SYNTH)
738 return -EINVAL;
740 if (!type || !name)
741 return -EINVAL;
743 dynevent_arg_pair_init(&arg_pair, 0, ';');
745 arg_pair.lhs = type;
746 arg_pair.rhs = name;
748 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
749 if (ret)
750 return ret;
752 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
753 ret = -EINVAL;
755 return ret;
757 EXPORT_SYMBOL_GPL(synth_event_add_field);
760 * synth_event_add_field_str - Add a new field to a synthetic event cmd
761 * @cmd: A pointer to the dynevent_cmd struct representing the new event
762 * @type_name: The type and name of the new field to add, as a single string
764 * Add a new field to a synthetic event cmd object, as a single
765 * string. The @type_name string is expected to be of the form 'type
766 * name', which will be appended by ';'. No sanity checking is done -
767 * what's passed in is assumed to already be well-formed. Field
768 * ordering is in the same order the fields are added.
770 * See synth_field_size() for available types. If field_name contains
771 * [n] the field is considered to be an array.
773 * Return: 0 if successful, error otherwise.
775 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
777 struct dynevent_arg arg;
778 int ret;
780 if (cmd->type != DYNEVENT_TYPE_SYNTH)
781 return -EINVAL;
783 if (!type_name)
784 return -EINVAL;
786 dynevent_arg_init(&arg, ';');
788 arg.str = type_name;
790 ret = dynevent_arg_add(cmd, &arg, NULL);
791 if (ret)
792 return ret;
794 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
795 ret = -EINVAL;
797 return ret;
799 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
802 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
803 * @cmd: A pointer to the dynevent_cmd struct representing the new event
804 * @fields: An array of type/name field descriptions
805 * @n_fields: The number of field descriptions contained in the fields array
807 * Add a new set of fields to a synthetic event cmd object. The event
808 * fields that will be defined for the event should be passed in as an
809 * array of struct synth_field_desc, and the number of elements in the
810 * array passed in as n_fields. Field ordering will retain the
811 * ordering given in the fields array.
813 * See synth_field_size() for available types. If field_name contains
814 * [n] the field is considered to be an array.
816 * Return: 0 if successful, error otherwise.
818 int synth_event_add_fields(struct dynevent_cmd *cmd,
819 struct synth_field_desc *fields,
820 unsigned int n_fields)
822 unsigned int i;
823 int ret = 0;
825 for (i = 0; i < n_fields; i++) {
826 if (fields[i].type == NULL || fields[i].name == NULL) {
827 ret = -EINVAL;
828 break;
831 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
832 if (ret)
833 break;
836 return ret;
838 EXPORT_SYMBOL_GPL(synth_event_add_fields);
841 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
842 * @cmd: A pointer to the dynevent_cmd struct representing the new event
843 * @name: The name of the synthetic event
844 * @mod: The module creating the event, NULL if not created from a module
845 * @args: Variable number of arg (pairs), one pair for each field
847 * NOTE: Users normally won't want to call this function directly, but
848 * rather use the synth_event_gen_cmd_start() wrapper, which
849 * automatically adds a NULL to the end of the arg list. If this
850 * function is used directly, make sure the last arg in the variable
851 * arg list is NULL.
853 * Generate a synthetic event command to be executed by
854 * synth_event_gen_cmd_end(). This function can be used to generate
855 * the complete command or only the first part of it; in the latter
856 * case, synth_event_add_field(), synth_event_add_field_str(), or
857 * synth_event_add_fields() can be used to add more fields following
858 * this.
860 * There should be an even number variable args, each pair consisting
861 * of a type followed by a field name.
863 * See synth_field_size() for available types. If field_name contains
864 * [n] the field is considered to be an array.
866 * Return: 0 if successful, error otherwise.
868 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
869 struct module *mod, ...)
871 struct dynevent_arg arg;
872 va_list args;
873 int ret;
875 cmd->event_name = name;
876 cmd->private_data = mod;
878 if (cmd->type != DYNEVENT_TYPE_SYNTH)
879 return -EINVAL;
881 dynevent_arg_init(&arg, 0);
882 arg.str = name;
883 ret = dynevent_arg_add(cmd, &arg, NULL);
884 if (ret)
885 return ret;
887 va_start(args, mod);
888 for (;;) {
889 const char *type, *name;
891 type = va_arg(args, const char *);
892 if (!type)
893 break;
894 name = va_arg(args, const char *);
895 if (!name)
896 break;
898 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
899 ret = -EINVAL;
900 break;
903 ret = synth_event_add_field(cmd, type, name);
904 if (ret)
905 break;
907 va_end(args);
909 return ret;
911 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
914 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
915 * @cmd: A pointer to the dynevent_cmd struct representing the new event
916 * @name: The name of the synthetic event
917 * @fields: An array of type/name field descriptions
918 * @n_fields: The number of field descriptions contained in the fields array
920 * Generate a synthetic event command to be executed by
921 * synth_event_gen_cmd_end(). This function can be used to generate
922 * the complete command or only the first part of it; in the latter
923 * case, synth_event_add_field(), synth_event_add_field_str(), or
924 * synth_event_add_fields() can be used to add more fields following
925 * this.
927 * The event fields that will be defined for the event should be
928 * passed in as an array of struct synth_field_desc, and the number of
929 * elements in the array passed in as n_fields. Field ordering will
930 * retain the ordering given in the fields array.
932 * See synth_field_size() for available types. If field_name contains
933 * [n] the field is considered to be an array.
935 * Return: 0 if successful, error otherwise.
937 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
938 struct module *mod,
939 struct synth_field_desc *fields,
940 unsigned int n_fields)
942 struct dynevent_arg arg;
943 unsigned int i;
944 int ret = 0;
946 cmd->event_name = name;
947 cmd->private_data = mod;
949 if (cmd->type != DYNEVENT_TYPE_SYNTH)
950 return -EINVAL;
952 if (n_fields > SYNTH_FIELDS_MAX)
953 return -EINVAL;
955 dynevent_arg_init(&arg, 0);
956 arg.str = name;
957 ret = dynevent_arg_add(cmd, &arg, NULL);
958 if (ret)
959 return ret;
961 for (i = 0; i < n_fields; i++) {
962 if (fields[i].type == NULL || fields[i].name == NULL)
963 return -EINVAL;
965 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
966 if (ret)
967 break;
970 return ret;
972 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
974 static int __create_synth_event(int argc, const char *name, const char **argv)
976 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
977 struct synth_event *event = NULL;
978 int i, consumed = 0, n_fields = 0, ret = 0;
981 * Argument syntax:
982 * - Add synthetic event: <event_name> field[;field] ...
983 * - Remove synthetic event: !<event_name> field[;field] ...
984 * where 'field' = type field_name
987 if (name[0] == '\0' || argc < 1)
988 return -EINVAL;
990 mutex_lock(&event_mutex);
992 event = find_synth_event(name);
993 if (event) {
994 ret = -EEXIST;
995 goto out;
998 for (i = 0; i < argc - 1; i++) {
999 if (strcmp(argv[i], ";") == 0)
1000 continue;
1001 if (n_fields == SYNTH_FIELDS_MAX) {
1002 ret = -EINVAL;
1003 goto err;
1006 field = parse_synth_field(argc - i, &argv[i], &consumed);
1007 if (IS_ERR(field)) {
1008 ret = PTR_ERR(field);
1009 goto err;
1011 fields[n_fields++] = field;
1012 i += consumed - 1;
1015 if (i < argc && strcmp(argv[i], ";") != 0) {
1016 ret = -EINVAL;
1017 goto err;
1020 event = alloc_synth_event(name, n_fields, fields);
1021 if (IS_ERR(event)) {
1022 ret = PTR_ERR(event);
1023 event = NULL;
1024 goto err;
1026 ret = register_synth_event(event);
1027 if (!ret)
1028 dyn_event_add(&event->devent);
1029 else
1030 free_synth_event(event);
1031 out:
1032 mutex_unlock(&event_mutex);
1034 return ret;
1035 err:
1036 for (i = 0; i < n_fields; i++)
1037 free_synth_field(fields[i]);
1039 goto out;
1043 * synth_event_create - Create a new synthetic event
1044 * @name: The name of the new sythetic event
1045 * @fields: An array of type/name field descriptions
1046 * @n_fields: The number of field descriptions contained in the fields array
1047 * @mod: The module creating the event, NULL if not created from a module
1049 * Create a new synthetic event with the given name under the
1050 * trace/events/synthetic/ directory. The event fields that will be
1051 * defined for the event should be passed in as an array of struct
1052 * synth_field_desc, and the number elements in the array passed in as
1053 * n_fields. Field ordering will retain the ordering given in the
1054 * fields array.
1056 * If the new synthetic event is being created from a module, the mod
1057 * param must be non-NULL. This will ensure that the trace buffer
1058 * won't contain unreadable events.
1060 * The new synth event should be deleted using synth_event_delete()
1061 * function. The new synthetic event can be generated from modules or
1062 * other kernel code using trace_synth_event() and related functions.
1064 * Return: 0 if successful, error otherwise.
1066 int synth_event_create(const char *name, struct synth_field_desc *fields,
1067 unsigned int n_fields, struct module *mod)
1069 struct dynevent_cmd cmd;
1070 char *buf;
1071 int ret;
1073 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1074 if (!buf)
1075 return -ENOMEM;
1077 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1079 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1080 fields, n_fields);
1081 if (ret)
1082 goto out;
1084 ret = synth_event_gen_cmd_end(&cmd);
1085 out:
1086 kfree(buf);
1088 return ret;
1090 EXPORT_SYMBOL_GPL(synth_event_create);
1092 static int destroy_synth_event(struct synth_event *se)
1094 int ret;
1096 if (se->ref)
1097 ret = -EBUSY;
1098 else {
1099 ret = unregister_synth_event(se);
1100 if (!ret) {
1101 dyn_event_remove(&se->devent);
1102 free_synth_event(se);
1106 return ret;
1110 * synth_event_delete - Delete a synthetic event
1111 * @event_name: The name of the new sythetic event
1113 * Delete a synthetic event that was created with synth_event_create().
1115 * Return: 0 if successful, error otherwise.
1117 int synth_event_delete(const char *event_name)
1119 struct synth_event *se = NULL;
1120 struct module *mod = NULL;
1121 int ret = -ENOENT;
1123 mutex_lock(&event_mutex);
1124 se = find_synth_event(event_name);
1125 if (se) {
1126 mod = se->mod;
1127 ret = destroy_synth_event(se);
1129 mutex_unlock(&event_mutex);
1131 if (mod) {
1132 mutex_lock(&trace_types_lock);
1134 * It is safest to reset the ring buffer if the module
1135 * being unloaded registered any events that were
1136 * used. The only worry is if a new module gets
1137 * loaded, and takes on the same id as the events of
1138 * this module. When printing out the buffer, traced
1139 * events left over from this module may be passed to
1140 * the new module events and unexpected results may
1141 * occur.
1143 tracing_reset_all_online_cpus();
1144 mutex_unlock(&trace_types_lock);
1147 return ret;
1149 EXPORT_SYMBOL_GPL(synth_event_delete);
1151 static int create_or_delete_synth_event(int argc, char **argv)
1153 const char *name = argv[0];
1154 int ret;
1156 /* trace_run_command() ensures argc != 0 */
1157 if (name[0] == '!') {
1158 ret = synth_event_delete(name + 1);
1159 return ret;
1162 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1163 return ret == -ECANCELED ? -EINVAL : ret;
1166 static int synth_event_run_command(struct dynevent_cmd *cmd)
1168 struct synth_event *se;
1169 int ret;
1171 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1172 if (ret)
1173 return ret;
1175 se = find_synth_event(cmd->event_name);
1176 if (WARN_ON(!se))
1177 return -ENOENT;
1179 se->mod = cmd->private_data;
1181 return ret;
1185 * synth_event_cmd_init - Initialize a synthetic event command object
1186 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1187 * @buf: A pointer to the buffer used to build the command
1188 * @maxlen: The length of the buffer passed in @buf
1190 * Initialize a synthetic event command object. Use this before
1191 * calling any of the other dyenvent_cmd functions.
1193 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1195 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1196 synth_event_run_command);
1198 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1200 static inline int
1201 __synth_event_trace_start(struct trace_event_file *file,
1202 struct synth_event_trace_state *trace_state)
1204 int entry_size, fields_size = 0;
1205 int ret = 0;
1207 memset(trace_state, '\0', sizeof(*trace_state));
1210 * Normal event tracing doesn't get called at all unless the
1211 * ENABLED bit is set (which attaches the probe thus allowing
1212 * this code to be called, etc). Because this is called
1213 * directly by the user, we don't have that but we still need
1214 * to honor not logging when disabled. For the the iterated
1215 * trace case, we save the enabed state upon start and just
1216 * ignore the following data calls.
1218 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1219 trace_trigger_soft_disabled(file)) {
1220 trace_state->disabled = true;
1221 ret = -ENOENT;
1222 goto out;
1225 trace_state->event = file->event_call->data;
1227 fields_size = trace_state->event->n_u64 * sizeof(u64);
1230 * Avoid ring buffer recursion detection, as this event
1231 * is being performed within another event.
1233 trace_state->buffer = file->tr->array_buffer.buffer;
1234 ring_buffer_nest_start(trace_state->buffer);
1236 entry_size = sizeof(*trace_state->entry) + fields_size;
1237 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1238 file,
1239 entry_size);
1240 if (!trace_state->entry) {
1241 ring_buffer_nest_end(trace_state->buffer);
1242 ret = -EINVAL;
1244 out:
1245 return ret;
1248 static inline void
1249 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1251 trace_event_buffer_commit(&trace_state->fbuffer);
1253 ring_buffer_nest_end(trace_state->buffer);
1257 * synth_event_trace - Trace a synthetic event
1258 * @file: The trace_event_file representing the synthetic event
1259 * @n_vals: The number of values in vals
1260 * @args: Variable number of args containing the event values
1262 * Trace a synthetic event using the values passed in the variable
1263 * argument list.
1265 * The argument list should be a list 'n_vals' u64 values. The number
1266 * of vals must match the number of field in the synthetic event, and
1267 * must be in the same order as the synthetic event fields.
1269 * All vals should be cast to u64, and string vals are just pointers
1270 * to strings, cast to u64. Strings will be copied into space
1271 * reserved in the event for the string, using these pointers.
1273 * Return: 0 on success, err otherwise.
1275 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1277 struct synth_event_trace_state state;
1278 unsigned int i, n_u64;
1279 va_list args;
1280 int ret;
1282 ret = __synth_event_trace_start(file, &state);
1283 if (ret) {
1284 if (ret == -ENOENT)
1285 ret = 0; /* just disabled, not really an error */
1286 return ret;
1289 if (n_vals != state.event->n_fields) {
1290 ret = -EINVAL;
1291 goto out;
1294 va_start(args, n_vals);
1295 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1296 u64 val;
1298 val = va_arg(args, u64);
1300 if (state.event->fields[i]->is_string) {
1301 char *str_val = (char *)(long)val;
1302 char *str_field = (char *)&state.entry->fields[n_u64];
1304 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1305 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1306 } else {
1307 struct synth_field *field = state.event->fields[i];
1309 switch (field->size) {
1310 case 1:
1311 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1312 break;
1314 case 2:
1315 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1316 break;
1318 case 4:
1319 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1320 break;
1322 default:
1323 state.entry->fields[n_u64] = val;
1324 break;
1326 n_u64++;
1329 va_end(args);
1330 out:
1331 __synth_event_trace_end(&state);
1333 return ret;
1335 EXPORT_SYMBOL_GPL(synth_event_trace);
1338 * synth_event_trace_array - Trace a synthetic event from an array
1339 * @file: The trace_event_file representing the synthetic event
1340 * @vals: Array of values
1341 * @n_vals: The number of values in vals
1343 * Trace a synthetic event using the values passed in as 'vals'.
1345 * The 'vals' array is just an array of 'n_vals' u64. The number of
1346 * vals must match the number of field in the synthetic event, and
1347 * must be in the same order as the synthetic event fields.
1349 * All vals should be cast to u64, and string vals are just pointers
1350 * to strings, cast to u64. Strings will be copied into space
1351 * reserved in the event for the string, using these pointers.
1353 * Return: 0 on success, err otherwise.
1355 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1356 unsigned int n_vals)
1358 struct synth_event_trace_state state;
1359 unsigned int i, n_u64;
1360 int ret;
1362 ret = __synth_event_trace_start(file, &state);
1363 if (ret) {
1364 if (ret == -ENOENT)
1365 ret = 0; /* just disabled, not really an error */
1366 return ret;
1369 if (n_vals != state.event->n_fields) {
1370 ret = -EINVAL;
1371 goto out;
1374 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1375 if (state.event->fields[i]->is_string) {
1376 char *str_val = (char *)(long)vals[i];
1377 char *str_field = (char *)&state.entry->fields[n_u64];
1379 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1380 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1381 } else {
1382 struct synth_field *field = state.event->fields[i];
1383 u64 val = vals[i];
1385 switch (field->size) {
1386 case 1:
1387 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1388 break;
1390 case 2:
1391 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1392 break;
1394 case 4:
1395 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1396 break;
1398 default:
1399 state.entry->fields[n_u64] = val;
1400 break;
1402 n_u64++;
1405 out:
1406 __synth_event_trace_end(&state);
1408 return ret;
1410 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1413 * synth_event_trace_start - Start piecewise synthetic event trace
1414 * @file: The trace_event_file representing the synthetic event
1415 * @trace_state: A pointer to object tracking the piecewise trace state
1417 * Start the trace of a synthetic event field-by-field rather than all
1418 * at once.
1420 * This function 'opens' an event trace, which means space is reserved
1421 * for the event in the trace buffer, after which the event's
1422 * individual field values can be set through either
1423 * synth_event_add_next_val() or synth_event_add_val().
1425 * A pointer to a trace_state object is passed in, which will keep
1426 * track of the current event trace state until the event trace is
1427 * closed (and the event finally traced) using
1428 * synth_event_trace_end().
1430 * Note that synth_event_trace_end() must be called after all values
1431 * have been added for each event trace, regardless of whether adding
1432 * all field values succeeded or not.
1434 * Note also that for a given event trace, all fields must be added
1435 * using either synth_event_add_next_val() or synth_event_add_val()
1436 * but not both together or interleaved.
1438 * Return: 0 on success, err otherwise.
1440 int synth_event_trace_start(struct trace_event_file *file,
1441 struct synth_event_trace_state *trace_state)
1443 int ret;
1445 if (!trace_state)
1446 return -EINVAL;
1448 ret = __synth_event_trace_start(file, trace_state);
1449 if (ret == -ENOENT)
1450 ret = 0; /* just disabled, not really an error */
1452 return ret;
1454 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1456 static int __synth_event_add_val(const char *field_name, u64 val,
1457 struct synth_event_trace_state *trace_state)
1459 struct synth_field *field = NULL;
1460 struct synth_trace_event *entry;
1461 struct synth_event *event;
1462 int i, ret = 0;
1464 if (!trace_state) {
1465 ret = -EINVAL;
1466 goto out;
1469 /* can't mix add_next_synth_val() with add_synth_val() */
1470 if (field_name) {
1471 if (trace_state->add_next) {
1472 ret = -EINVAL;
1473 goto out;
1475 trace_state->add_name = true;
1476 } else {
1477 if (trace_state->add_name) {
1478 ret = -EINVAL;
1479 goto out;
1481 trace_state->add_next = true;
1484 if (trace_state->disabled)
1485 goto out;
1487 event = trace_state->event;
1488 if (trace_state->add_name) {
1489 for (i = 0; i < event->n_fields; i++) {
1490 field = event->fields[i];
1491 if (strcmp(field->name, field_name) == 0)
1492 break;
1494 if (!field) {
1495 ret = -EINVAL;
1496 goto out;
1498 } else {
1499 if (trace_state->cur_field >= event->n_fields) {
1500 ret = -EINVAL;
1501 goto out;
1503 field = event->fields[trace_state->cur_field++];
1506 entry = trace_state->entry;
1507 if (field->is_string) {
1508 char *str_val = (char *)(long)val;
1509 char *str_field;
1511 if (!str_val) {
1512 ret = -EINVAL;
1513 goto out;
1516 str_field = (char *)&entry->fields[field->offset];
1517 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1518 } else {
1519 switch (field->size) {
1520 case 1:
1521 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1522 break;
1524 case 2:
1525 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1526 break;
1528 case 4:
1529 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1530 break;
1532 default:
1533 trace_state->entry->fields[field->offset] = val;
1534 break;
1537 out:
1538 return ret;
1542 * synth_event_add_next_val - Add the next field's value to an open synth trace
1543 * @val: The value to set the next field to
1544 * @trace_state: A pointer to object tracking the piecewise trace state
1546 * Set the value of the next field in an event that's been opened by
1547 * synth_event_trace_start().
1549 * The val param should be the value cast to u64. If the value points
1550 * to a string, the val param should be a char * cast to u64.
1552 * This function assumes all the fields in an event are to be set one
1553 * after another - successive calls to this function are made, one for
1554 * each field, in the order of the fields in the event, until all
1555 * fields have been set. If you'd rather set each field individually
1556 * without regard to ordering, synth_event_add_val() can be used
1557 * instead.
1559 * Note however that synth_event_add_next_val() and
1560 * synth_event_add_val() can't be intermixed for a given event trace -
1561 * one or the other but not both can be used at the same time.
1563 * Note also that synth_event_trace_end() must be called after all
1564 * values have been added for each event trace, regardless of whether
1565 * adding all field values succeeded or not.
1567 * Return: 0 on success, err otherwise.
1569 int synth_event_add_next_val(u64 val,
1570 struct synth_event_trace_state *trace_state)
1572 return __synth_event_add_val(NULL, val, trace_state);
1574 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
1577 * synth_event_add_val - Add a named field's value to an open synth trace
1578 * @field_name: The name of the synthetic event field value to set
1579 * @val: The value to set the next field to
1580 * @trace_state: A pointer to object tracking the piecewise trace state
1582 * Set the value of the named field in an event that's been opened by
1583 * synth_event_trace_start().
1585 * The val param should be the value cast to u64. If the value points
1586 * to a string, the val param should be a char * cast to u64.
1588 * This function looks up the field name, and if found, sets the field
1589 * to the specified value. This lookup makes this function more
1590 * expensive than synth_event_add_next_val(), so use that or the
1591 * none-piecewise synth_event_trace() instead if efficiency is more
1592 * important.
1594 * Note however that synth_event_add_next_val() and
1595 * synth_event_add_val() can't be intermixed for a given event trace -
1596 * one or the other but not both can be used at the same time.
1598 * Note also that synth_event_trace_end() must be called after all
1599 * values have been added for each event trace, regardless of whether
1600 * adding all field values succeeded or not.
1602 * Return: 0 on success, err otherwise.
1604 int synth_event_add_val(const char *field_name, u64 val,
1605 struct synth_event_trace_state *trace_state)
1607 return __synth_event_add_val(field_name, val, trace_state);
1609 EXPORT_SYMBOL_GPL(synth_event_add_val);
1612 * synth_event_trace_end - End piecewise synthetic event trace
1613 * @trace_state: A pointer to object tracking the piecewise trace state
1615 * End the trace of a synthetic event opened by
1616 * synth_event_trace__start().
1618 * This function 'closes' an event trace, which basically means that
1619 * it commits the reserved event and cleans up other loose ends.
1621 * A pointer to a trace_state object is passed in, which will keep
1622 * track of the current event trace state opened with
1623 * synth_event_trace_start().
1625 * Note that this function must be called after all values have been
1626 * added for each event trace, regardless of whether adding all field
1627 * values succeeded or not.
1629 * Return: 0 on success, err otherwise.
1631 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
1633 if (!trace_state)
1634 return -EINVAL;
1636 __synth_event_trace_end(trace_state);
1638 return 0;
1640 EXPORT_SYMBOL_GPL(synth_event_trace_end);
1642 static int create_synth_event(int argc, const char **argv)
1644 const char *name = argv[0];
1645 int len;
1647 if (name[0] != 's' || name[1] != ':')
1648 return -ECANCELED;
1649 name += 2;
1651 /* This interface accepts group name prefix */
1652 if (strchr(name, '/')) {
1653 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1654 if (len == 0)
1655 return -EINVAL;
1656 name += len;
1658 return __create_synth_event(argc - 1, name, argv + 1);
1661 static int synth_event_release(struct dyn_event *ev)
1663 struct synth_event *event = to_synth_event(ev);
1664 int ret;
1666 if (event->ref)
1667 return -EBUSY;
1669 ret = unregister_synth_event(event);
1670 if (ret)
1671 return ret;
1673 dyn_event_remove(ev);
1674 free_synth_event(event);
1675 return 0;
1678 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1680 struct synth_field *field;
1681 unsigned int i;
1683 seq_printf(m, "%s\t", event->name);
1685 for (i = 0; i < event->n_fields; i++) {
1686 field = event->fields[i];
1688 /* parameter values */
1689 seq_printf(m, "%s %s%s", field->type, field->name,
1690 i == event->n_fields - 1 ? "" : "; ");
1693 seq_putc(m, '\n');
1695 return 0;
1698 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1700 struct synth_event *event = to_synth_event(ev);
1702 seq_printf(m, "s:%s/", event->class.system);
1704 return __synth_event_show(m, event);
1707 static int synth_events_seq_show(struct seq_file *m, void *v)
1709 struct dyn_event *ev = v;
1711 if (!is_synth_event(ev))
1712 return 0;
1714 return __synth_event_show(m, to_synth_event(ev));
1717 static const struct seq_operations synth_events_seq_op = {
1718 .start = dyn_event_seq_start,
1719 .next = dyn_event_seq_next,
1720 .stop = dyn_event_seq_stop,
1721 .show = synth_events_seq_show,
1724 static int synth_events_open(struct inode *inode, struct file *file)
1726 int ret;
1728 ret = security_locked_down(LOCKDOWN_TRACEFS);
1729 if (ret)
1730 return ret;
1732 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1733 ret = dyn_events_release_all(&synth_event_ops);
1734 if (ret < 0)
1735 return ret;
1738 return seq_open(file, &synth_events_seq_op);
1741 static ssize_t synth_events_write(struct file *file,
1742 const char __user *buffer,
1743 size_t count, loff_t *ppos)
1745 return trace_parse_run_command(file, buffer, count, ppos,
1746 create_or_delete_synth_event);
1749 static const struct file_operations synth_events_fops = {
1750 .open = synth_events_open,
1751 .write = synth_events_write,
1752 .read = seq_read,
1753 .llseek = seq_lseek,
1754 .release = seq_release,
1757 static __init int trace_events_synth_init(void)
1759 struct dentry *entry = NULL;
1760 struct dentry *d_tracer;
1761 int err = 0;
1763 err = dyn_event_register(&synth_event_ops);
1764 if (err) {
1765 pr_warn("Could not register synth_event_ops\n");
1766 return err;
1769 d_tracer = tracing_init_dentry();
1770 if (IS_ERR(d_tracer)) {
1771 err = PTR_ERR(d_tracer);
1772 goto err;
1775 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
1776 NULL, &synth_events_fops);
1777 if (!entry) {
1778 err = -ENODEV;
1779 goto err;
1782 return err;
1783 err:
1784 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
1786 return err;
1789 fs_initcall(trace_events_synth_init);