1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_synth - synthetic trace events
5 * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
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>
20 #include "trace_probe.h"
21 #include "trace_probe_kernel.h"
23 #include "trace_synth.h"
27 C(BAD_NAME, "Illegal name"), \
28 C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\
29 C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\
30 C(EVENT_EXISTS, "Event already exists"), \
31 C(TOO_MANY_FIELDS, "Too many fields"), \
32 C(INCOMPLETE_TYPE, "Incomplete type"), \
33 C(INVALID_TYPE, "Invalid type"), \
34 C(INVALID_FIELD, "Invalid field"), \
35 C(INVALID_ARRAY_SPEC, "Invalid array specification"),
38 #define C(a, b) SYNTH_ERR_##a
45 static const char *err_text[] = { ERRORS };
47 static char *last_cmd;
49 static int errpos(const char *str)
51 if (!str || !last_cmd)
54 return err_pos(last_cmd, str);
57 static void last_cmd_set(const char *str)
64 last_cmd = kstrdup(str, GFP_KERNEL);
67 static void synth_err(u8 err_type, u16 err_pos)
72 tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
76 static int create_synth_event(const char *raw_command);
77 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
78 static int synth_event_release(struct dyn_event *ev);
79 static bool synth_event_is_busy(struct dyn_event *ev);
80 static bool synth_event_match(const char *system, const char *event,
81 int argc, const char **argv, struct dyn_event *ev);
83 static struct dyn_event_operations synth_event_ops = {
84 .create = create_synth_event,
85 .show = synth_event_show,
86 .is_busy = synth_event_is_busy,
87 .free = synth_event_release,
88 .match = synth_event_match,
91 static bool is_synth_event(struct dyn_event *ev)
93 return ev->ops == &synth_event_ops;
96 static struct synth_event *to_synth_event(struct dyn_event *ev)
98 return container_of(ev, struct synth_event, devent);
101 static bool synth_event_is_busy(struct dyn_event *ev)
103 struct synth_event *event = to_synth_event(ev);
105 return event->ref != 0;
108 static bool synth_event_match(const char *system, const char *event,
109 int argc, const char **argv, struct dyn_event *ev)
111 struct synth_event *sev = to_synth_event(ev);
113 return strcmp(sev->name, event) == 0 &&
114 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
117 struct synth_trace_event {
118 struct trace_entry ent;
122 static int synth_event_define_fields(struct trace_event_call *call)
124 struct synth_trace_event trace;
125 int offset = offsetof(typeof(trace), fields);
126 struct synth_event *event = call->data;
127 unsigned int i, size, n_u64;
132 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
133 size = event->fields[i]->size;
134 is_signed = event->fields[i]->is_signed;
135 type = event->fields[i]->type;
136 name = event->fields[i]->name;
137 ret = trace_define_field(call, type, name, offset, size,
138 is_signed, FILTER_OTHER);
142 event->fields[i]->offset = n_u64;
144 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
145 offset += STR_VAR_LEN_MAX;
146 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
148 offset += sizeof(u64);
153 event->n_u64 = n_u64;
158 static bool synth_field_signed(char *type)
160 if (str_has_prefix(type, "u"))
162 if (strcmp(type, "gfp_t") == 0)
168 static int synth_field_is_string(char *type)
170 if (strstr(type, "char[") != NULL)
176 static int synth_field_string_size(char *type)
178 char buf[4], *end, *start;
182 start = strstr(type, "char[");
185 start += sizeof("char[") - 1;
187 end = strchr(type, ']');
188 if (!end || end < start || type + strlen(type) > end + 1)
196 return 0; /* variable-length string */
198 strncpy(buf, start, len);
201 err = kstrtouint(buf, 0, &size);
205 if (size > STR_VAR_LEN_MAX)
211 static int synth_field_size(char *type)
215 if (strcmp(type, "s64") == 0)
217 else if (strcmp(type, "u64") == 0)
219 else if (strcmp(type, "s32") == 0)
221 else if (strcmp(type, "u32") == 0)
223 else if (strcmp(type, "s16") == 0)
225 else if (strcmp(type, "u16") == 0)
227 else if (strcmp(type, "s8") == 0)
229 else if (strcmp(type, "u8") == 0)
231 else if (strcmp(type, "char") == 0)
233 else if (strcmp(type, "unsigned char") == 0)
234 size = sizeof(unsigned char);
235 else if (strcmp(type, "int") == 0)
237 else if (strcmp(type, "unsigned int") == 0)
238 size = sizeof(unsigned int);
239 else if (strcmp(type, "long") == 0)
241 else if (strcmp(type, "unsigned long") == 0)
242 size = sizeof(unsigned long);
243 else if (strcmp(type, "bool") == 0)
245 else if (strcmp(type, "pid_t") == 0)
246 size = sizeof(pid_t);
247 else if (strcmp(type, "gfp_t") == 0)
248 size = sizeof(gfp_t);
249 else if (synth_field_is_string(type))
250 size = synth_field_string_size(type);
255 static const char *synth_field_fmt(char *type)
257 const char *fmt = "%llu";
259 if (strcmp(type, "s64") == 0)
261 else if (strcmp(type, "u64") == 0)
263 else if (strcmp(type, "s32") == 0)
265 else if (strcmp(type, "u32") == 0)
267 else if (strcmp(type, "s16") == 0)
269 else if (strcmp(type, "u16") == 0)
271 else if (strcmp(type, "s8") == 0)
273 else if (strcmp(type, "u8") == 0)
275 else if (strcmp(type, "char") == 0)
277 else if (strcmp(type, "unsigned char") == 0)
279 else if (strcmp(type, "int") == 0)
281 else if (strcmp(type, "unsigned int") == 0)
283 else if (strcmp(type, "long") == 0)
285 else if (strcmp(type, "unsigned long") == 0)
287 else if (strcmp(type, "bool") == 0)
289 else if (strcmp(type, "pid_t") == 0)
291 else if (strcmp(type, "gfp_t") == 0)
293 else if (synth_field_is_string(type))
299 static void print_synth_event_num_val(struct trace_seq *s,
300 char *print_fmt, char *name,
301 int size, u64 val, char *space)
305 trace_seq_printf(s, print_fmt, name, (u8)val, space);
309 trace_seq_printf(s, print_fmt, name, (u16)val, space);
313 trace_seq_printf(s, print_fmt, name, (u32)val, space);
317 trace_seq_printf(s, print_fmt, name, val, space);
322 static enum print_line_t print_synth_event(struct trace_iterator *iter,
324 struct trace_event *event)
326 struct trace_array *tr = iter->tr;
327 struct trace_seq *s = &iter->seq;
328 struct synth_trace_event *entry;
329 struct synth_event *se;
330 unsigned int i, n_u64;
334 entry = (struct synth_trace_event *)iter->ent;
335 se = container_of(event, struct synth_event, call.event);
337 trace_seq_printf(s, "%s: ", se->name);
339 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
340 if (trace_seq_has_overflowed(s))
343 fmt = synth_field_fmt(se->fields[i]->type);
345 /* parameter types */
346 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
347 trace_seq_printf(s, "%s ", fmt);
349 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
351 /* parameter values */
352 if (se->fields[i]->is_string) {
353 if (se->fields[i]->is_dynamic) {
354 u32 offset, data_offset;
357 offset = (u32)entry->fields[n_u64];
358 data_offset = offset & 0xffff;
360 str_field = (char *)entry + data_offset;
362 trace_seq_printf(s, print_fmt, se->fields[i]->name,
365 i == se->n_fields - 1 ? "" : " ");
368 trace_seq_printf(s, print_fmt, se->fields[i]->name,
370 (char *)&entry->fields[n_u64],
371 i == se->n_fields - 1 ? "" : " ");
372 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
375 struct trace_print_flags __flags[] = {
376 __def_gfpflag_names, {-1, NULL} };
377 char *space = (i == se->n_fields - 1 ? "" : " ");
379 print_synth_event_num_val(s, print_fmt,
382 entry->fields[n_u64],
385 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
386 trace_seq_puts(s, " (");
387 trace_print_flags_seq(s, "|",
388 entry->fields[n_u64],
390 trace_seq_putc(s, ')');
396 trace_seq_putc(s, '\n');
398 return trace_handle_return(s);
401 static struct trace_event_functions synth_event_funcs = {
402 .trace = print_synth_event
405 static unsigned int trace_string(struct synth_trace_event *entry,
406 struct synth_event *event,
409 unsigned int data_size,
412 unsigned int len = 0;
419 data_offset = offsetof(typeof(*entry), fields);
420 data_offset += event->n_u64 * sizeof(u64);
421 data_offset += data_size;
423 len = kern_fetch_store_strlen((unsigned long)str_val);
425 data_offset |= len << 16;
426 *(u32 *)&entry->fields[*n_u64] = data_offset;
428 ret = kern_fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
432 str_field = (char *)&entry->fields[*n_u64];
434 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
435 if ((unsigned long)str_val < TASK_SIZE)
436 ret = strncpy_from_user_nofault(str_field, str_val, STR_VAR_LEN_MAX);
439 ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX);
442 strcpy(str_field, FAULT_STRING);
444 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
450 static notrace void trace_event_raw_event_synth(void *__data,
452 unsigned int *var_ref_idx)
454 unsigned int i, n_u64, val_idx, len, data_size = 0;
455 struct trace_event_file *trace_file = __data;
456 struct synth_trace_event *entry;
457 struct trace_event_buffer fbuffer;
458 struct trace_buffer *buffer;
459 struct synth_event *event;
462 event = trace_file->event_call->data;
464 if (trace_trigger_soft_disabled(trace_file))
467 fields_size = event->n_u64 * sizeof(u64);
469 for (i = 0; i < event->n_dynamic_fields; i++) {
470 unsigned int field_pos = event->dynamic_fields[i]->field_pos;
473 val_idx = var_ref_idx[field_pos];
474 str_val = (char *)(long)var_ref_vals[val_idx];
476 len = kern_fetch_store_strlen((unsigned long)str_val);
482 * Avoid ring buffer recursion detection, as this event
483 * is being performed within another event.
485 buffer = trace_file->tr->array_buffer.buffer;
486 ring_buffer_nest_start(buffer);
488 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
489 sizeof(*entry) + fields_size);
493 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
494 val_idx = var_ref_idx[i];
495 if (event->fields[i]->is_string) {
496 char *str_val = (char *)(long)var_ref_vals[val_idx];
498 len = trace_string(entry, event, str_val,
499 event->fields[i]->is_dynamic,
501 data_size += len; /* only dynamic string increments */
503 struct synth_field *field = event->fields[i];
504 u64 val = var_ref_vals[val_idx];
506 switch (field->size) {
508 *(u8 *)&entry->fields[n_u64] = (u8)val;
512 *(u16 *)&entry->fields[n_u64] = (u16)val;
516 *(u32 *)&entry->fields[n_u64] = (u32)val;
520 entry->fields[n_u64] = val;
527 trace_event_buffer_commit(&fbuffer);
529 ring_buffer_nest_end(buffer);
532 static void free_synth_event_print_fmt(struct trace_event_call *call)
535 kfree(call->print_fmt);
536 call->print_fmt = NULL;
540 static int __set_synth_event_print_fmt(struct synth_event *event,
547 /* When len=0, we just calculate the needed length */
548 #define LEN_OR_ZERO (len ? len - pos : 0)
550 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
551 for (i = 0; i < event->n_fields; i++) {
552 fmt = synth_field_fmt(event->fields[i]->type);
553 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
554 event->fields[i]->name, fmt,
555 i == event->n_fields - 1 ? "" : ", ");
557 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
559 for (i = 0; i < event->n_fields; i++) {
560 if (event->fields[i]->is_string &&
561 event->fields[i]->is_dynamic)
562 pos += snprintf(buf + pos, LEN_OR_ZERO,
563 ", __get_str(%s)", event->fields[i]->name);
565 pos += snprintf(buf + pos, LEN_OR_ZERO,
566 ", REC->%s", event->fields[i]->name);
571 /* return the length of print_fmt */
575 static int set_synth_event_print_fmt(struct trace_event_call *call)
577 struct synth_event *event = call->data;
581 /* First: called with 0 length to calculate the needed length */
582 len = __set_synth_event_print_fmt(event, NULL, 0);
584 print_fmt = kmalloc(len + 1, GFP_KERNEL);
588 /* Second: actually write the @print_fmt */
589 __set_synth_event_print_fmt(event, print_fmt, len + 1);
590 call->print_fmt = print_fmt;
595 static void free_synth_field(struct synth_field *field)
602 static int check_field_version(const char *prefix, const char *field_type,
603 const char *field_name)
606 * For backward compatibility, the old synthetic event command
607 * format did not require semicolons, and in order to not
608 * break user space, that old format must still work. If a new
609 * feature is added, then the format that uses the new feature
610 * will be required to have semicolons, as nothing that uses
611 * the old format would be using the new, yet to be created,
612 * feature. When a new feature is added, this will detect it,
613 * and return a number greater than 1, and require the format
619 static struct synth_field *parse_synth_field(int argc, char **argv,
620 int *consumed, int *field_version)
622 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
623 struct synth_field *field;
624 int len, ret = -ENOMEM;
628 if (!strcmp(field_type, "unsigned")) {
630 synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
631 return ERR_PTR(-EINVAL);
633 prefix = "unsigned ";
634 field_type = argv[1];
635 field_name = argv[2];
638 field_name = argv[1];
643 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type));
644 return ERR_PTR(-EINVAL);
647 *field_version = check_field_version(prefix, field_type, field_name);
649 field = kzalloc(sizeof(*field), GFP_KERNEL);
651 return ERR_PTR(-ENOMEM);
653 len = strlen(field_name);
654 array = strchr(field_name, '[');
656 len -= strlen(array);
658 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
662 if (!is_good_name(field->name)) {
663 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
668 len = strlen(field_type) + 1;
671 len += strlen(array);
674 len += strlen(prefix);
676 field->type = kzalloc(len, GFP_KERNEL);
680 seq_buf_init(&s, field->type, len);
682 seq_buf_puts(&s, prefix);
683 seq_buf_puts(&s, field_type);
685 seq_buf_puts(&s, array);
686 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
689 s.buffer[s.len] = '\0';
691 size = synth_field_size(field->type);
694 synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name));
696 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
699 } else if (size == 0) {
700 if (synth_field_is_string(field->type)) {
703 len = sizeof("__data_loc ") + strlen(field->type) + 1;
704 type = kzalloc(len, GFP_KERNEL);
708 seq_buf_init(&s, type, len);
709 seq_buf_puts(&s, "__data_loc ");
710 seq_buf_puts(&s, field->type);
712 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
714 s.buffer[s.len] = '\0';
719 field->is_dynamic = true;
722 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
729 if (synth_field_is_string(field->type))
730 field->is_string = true;
732 field->is_signed = synth_field_signed(field->type);
736 free_synth_field(field);
737 field = ERR_PTR(ret);
741 static void free_synth_tracepoint(struct tracepoint *tp)
750 static struct tracepoint *alloc_synth_tracepoint(char *name)
752 struct tracepoint *tp;
754 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
756 return ERR_PTR(-ENOMEM);
758 tp->name = kstrdup(name, GFP_KERNEL);
761 return ERR_PTR(-ENOMEM);
767 struct synth_event *find_synth_event(const char *name)
769 struct dyn_event *pos;
770 struct synth_event *event;
772 for_each_dyn_event(pos) {
773 if (!is_synth_event(pos))
775 event = to_synth_event(pos);
776 if (strcmp(event->name, name) == 0)
783 static struct trace_event_fields synth_event_fields_array[] = {
784 { .type = TRACE_FUNCTION_TYPE,
785 .define_fields = synth_event_define_fields },
789 static int register_synth_event(struct synth_event *event)
791 struct trace_event_call *call = &event->call;
794 event->call.class = &event->class;
795 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
796 if (!event->class.system) {
801 event->tp = alloc_synth_tracepoint(event->name);
802 if (IS_ERR(event->tp)) {
803 ret = PTR_ERR(event->tp);
808 INIT_LIST_HEAD(&call->class->fields);
809 call->event.funcs = &synth_event_funcs;
810 call->class->fields_array = synth_event_fields_array;
812 ret = register_trace_event(&call->event);
817 call->flags = TRACE_EVENT_FL_TRACEPOINT;
818 call->class->reg = trace_event_reg;
819 call->class->probe = trace_event_raw_event_synth;
821 call->tp = event->tp;
823 ret = trace_add_event_call(call);
825 pr_warn("Failed to register synthetic event: %s\n",
826 trace_event_name(call));
830 ret = set_synth_event_print_fmt(call);
831 /* unregister_trace_event() will be called inside */
833 trace_remove_event_call(call);
837 unregister_trace_event(&call->event);
841 static int unregister_synth_event(struct synth_event *event)
843 struct trace_event_call *call = &event->call;
846 ret = trace_remove_event_call(call);
851 static void free_synth_event(struct synth_event *event)
858 for (i = 0; i < event->n_fields; i++)
859 free_synth_field(event->fields[i]);
861 kfree(event->fields);
862 kfree(event->dynamic_fields);
864 kfree(event->class.system);
865 free_synth_tracepoint(event->tp);
866 free_synth_event_print_fmt(&event->call);
870 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
871 struct synth_field **fields)
873 unsigned int i, j, n_dynamic_fields = 0;
874 struct synth_event *event;
876 event = kzalloc(sizeof(*event), GFP_KERNEL);
878 event = ERR_PTR(-ENOMEM);
882 event->name = kstrdup(name, GFP_KERNEL);
885 event = ERR_PTR(-ENOMEM);
889 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
890 if (!event->fields) {
891 free_synth_event(event);
892 event = ERR_PTR(-ENOMEM);
896 for (i = 0; i < n_fields; i++)
897 if (fields[i]->is_dynamic)
900 if (n_dynamic_fields) {
901 event->dynamic_fields = kcalloc(n_dynamic_fields,
902 sizeof(*event->dynamic_fields),
904 if (!event->dynamic_fields) {
905 free_synth_event(event);
906 event = ERR_PTR(-ENOMEM);
911 dyn_event_init(&event->devent, &synth_event_ops);
913 for (i = 0, j = 0; i < n_fields; i++) {
914 fields[i]->field_pos = i;
915 event->fields[i] = fields[i];
917 if (fields[i]->is_dynamic)
918 event->dynamic_fields[j++] = fields[i];
920 event->n_dynamic_fields = j;
921 event->n_fields = n_fields;
926 static int synth_event_check_arg_fn(void *data)
928 struct dynevent_arg_pair *arg_pair = data;
931 size = synth_field_size((char *)arg_pair->lhs);
933 if (strstr((char *)arg_pair->lhs, "["))
937 return size ? 0 : -EINVAL;
941 * synth_event_add_field - Add a new field to a synthetic event cmd
942 * @cmd: A pointer to the dynevent_cmd struct representing the new event
943 * @type: The type of the new field to add
944 * @name: The name of the new field to add
946 * Add a new field to a synthetic event cmd object. Field ordering is in
947 * the same order the fields are added.
949 * See synth_field_size() for available types. If field_name contains
950 * [n] the field is considered to be an array.
952 * Return: 0 if successful, error otherwise.
954 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
957 struct dynevent_arg_pair arg_pair;
960 if (cmd->type != DYNEVENT_TYPE_SYNTH)
966 dynevent_arg_pair_init(&arg_pair, 0, ';');
971 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
975 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
980 EXPORT_SYMBOL_GPL(synth_event_add_field);
983 * synth_event_add_field_str - Add a new field to a synthetic event cmd
984 * @cmd: A pointer to the dynevent_cmd struct representing the new event
985 * @type_name: The type and name of the new field to add, as a single string
987 * Add a new field to a synthetic event cmd object, as a single
988 * string. The @type_name string is expected to be of the form 'type
989 * name', which will be appended by ';'. No sanity checking is done -
990 * what's passed in is assumed to already be well-formed. Field
991 * ordering is in the same order the fields are added.
993 * See synth_field_size() for available types. If field_name contains
994 * [n] the field is considered to be an array.
996 * Return: 0 if successful, error otherwise.
998 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1000 struct dynevent_arg arg;
1003 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1009 dynevent_arg_init(&arg, ';');
1011 arg.str = type_name;
1013 ret = dynevent_arg_add(cmd, &arg, NULL);
1017 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1022 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1025 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1026 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1027 * @fields: An array of type/name field descriptions
1028 * @n_fields: The number of field descriptions contained in the fields array
1030 * Add a new set of fields to a synthetic event cmd object. The event
1031 * fields that will be defined for the event should be passed in as an
1032 * array of struct synth_field_desc, and the number of elements in the
1033 * array passed in as n_fields. Field ordering will retain the
1034 * ordering given in the fields array.
1036 * See synth_field_size() for available types. If field_name contains
1037 * [n] the field is considered to be an array.
1039 * Return: 0 if successful, error otherwise.
1041 int synth_event_add_fields(struct dynevent_cmd *cmd,
1042 struct synth_field_desc *fields,
1043 unsigned int n_fields)
1048 for (i = 0; i < n_fields; i++) {
1049 if (fields[i].type == NULL || fields[i].name == NULL) {
1054 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1061 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1064 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1065 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1066 * @name: The name of the synthetic event
1067 * @mod: The module creating the event, NULL if not created from a module
1068 * @args: Variable number of arg (pairs), one pair for each field
1070 * NOTE: Users normally won't want to call this function directly, but
1071 * rather use the synth_event_gen_cmd_start() wrapper, which
1072 * automatically adds a NULL to the end of the arg list. If this
1073 * function is used directly, make sure the last arg in the variable
1076 * Generate a synthetic event command to be executed by
1077 * synth_event_gen_cmd_end(). This function can be used to generate
1078 * the complete command or only the first part of it; in the latter
1079 * case, synth_event_add_field(), synth_event_add_field_str(), or
1080 * synth_event_add_fields() can be used to add more fields following
1083 * There should be an even number variable args, each pair consisting
1084 * of a type followed by a field name.
1086 * See synth_field_size() for available types. If field_name contains
1087 * [n] the field is considered to be an array.
1089 * Return: 0 if successful, error otherwise.
1091 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1092 struct module *mod, ...)
1094 struct dynevent_arg arg;
1098 cmd->event_name = name;
1099 cmd->private_data = mod;
1101 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1104 dynevent_arg_init(&arg, 0);
1106 ret = dynevent_arg_add(cmd, &arg, NULL);
1110 va_start(args, mod);
1112 const char *type, *name;
1114 type = va_arg(args, const char *);
1117 name = va_arg(args, const char *);
1121 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1126 ret = synth_event_add_field(cmd, type, name);
1134 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1137 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1138 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1139 * @name: The name of the synthetic event
1140 * @fields: An array of type/name field descriptions
1141 * @n_fields: The number of field descriptions contained in the fields array
1143 * Generate a synthetic event command to be executed by
1144 * synth_event_gen_cmd_end(). This function can be used to generate
1145 * the complete command or only the first part of it; in the latter
1146 * case, synth_event_add_field(), synth_event_add_field_str(), or
1147 * synth_event_add_fields() can be used to add more fields following
1150 * The event fields that will be defined for the event should be
1151 * passed in as an array of struct synth_field_desc, and the number of
1152 * elements in the array passed in as n_fields. Field ordering will
1153 * retain the ordering given in the fields array.
1155 * See synth_field_size() for available types. If field_name contains
1156 * [n] the field is considered to be an array.
1158 * Return: 0 if successful, error otherwise.
1160 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1162 struct synth_field_desc *fields,
1163 unsigned int n_fields)
1165 struct dynevent_arg arg;
1169 cmd->event_name = name;
1170 cmd->private_data = mod;
1172 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1175 if (n_fields > SYNTH_FIELDS_MAX)
1178 dynevent_arg_init(&arg, 0);
1180 ret = dynevent_arg_add(cmd, &arg, NULL);
1184 for (i = 0; i < n_fields; i++) {
1185 if (fields[i].type == NULL || fields[i].name == NULL)
1188 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1195 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1197 static int __create_synth_event(const char *name, const char *raw_fields)
1199 char **argv, *field_str, *tmp_fields, *saved_fields = NULL;
1200 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1201 int consumed, cmd_version = 1, n_fields_this_loop;
1202 int i, argc, n_fields = 0, ret = 0;
1203 struct synth_event *event = NULL;
1207 * - Add synthetic event: <event_name> field[;field] ...
1208 * - Remove synthetic event: !<event_name> field[;field] ...
1209 * where 'field' = type field_name
1212 if (name[0] == '\0') {
1213 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1217 if (!is_good_name(name)) {
1218 synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1222 mutex_lock(&event_mutex);
1224 event = find_synth_event(name);
1226 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1231 tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
1237 while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
1238 argv = argv_split(GFP_KERNEL, field_str, &argc);
1249 n_fields_this_loop = 0;
1251 while (argc > consumed) {
1254 field = parse_synth_field(argc - consumed,
1255 argv + consumed, &consumed,
1257 if (IS_ERR(field)) {
1258 ret = PTR_ERR(field);
1263 * Track the highest version of any field we
1264 * found in the command.
1266 if (field_version > cmd_version)
1267 cmd_version = field_version;
1270 * Now sort out what is and isn't valid for
1271 * each supported version.
1273 * If we see more than 1 field per loop, it
1274 * means we have multiple fields between
1275 * semicolons, and that's something we no
1276 * longer support in a version 2 or greater
1279 if (cmd_version > 1 && n_fields_this_loop >= 1) {
1280 synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str));
1285 fields[n_fields++] = field;
1286 if (n_fields == SYNTH_FIELDS_MAX) {
1287 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1292 n_fields_this_loop++;
1296 if (consumed < argc) {
1297 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1304 if (n_fields == 0) {
1305 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1310 event = alloc_synth_event(name, n_fields, fields);
1311 if (IS_ERR(event)) {
1312 ret = PTR_ERR(event);
1316 ret = register_synth_event(event);
1318 dyn_event_add(&event->devent, &event->call);
1320 free_synth_event(event);
1322 mutex_unlock(&event_mutex);
1324 kfree(saved_fields);
1330 for (i = 0; i < n_fields; i++)
1331 free_synth_field(fields[i]);
1337 * synth_event_create - Create a new synthetic event
1338 * @name: The name of the new synthetic event
1339 * @fields: An array of type/name field descriptions
1340 * @n_fields: The number of field descriptions contained in the fields array
1341 * @mod: The module creating the event, NULL if not created from a module
1343 * Create a new synthetic event with the given name under the
1344 * trace/events/synthetic/ directory. The event fields that will be
1345 * defined for the event should be passed in as an array of struct
1346 * synth_field_desc, and the number elements in the array passed in as
1347 * n_fields. Field ordering will retain the ordering given in the
1350 * If the new synthetic event is being created from a module, the mod
1351 * param must be non-NULL. This will ensure that the trace buffer
1352 * won't contain unreadable events.
1354 * The new synth event should be deleted using synth_event_delete()
1355 * function. The new synthetic event can be generated from modules or
1356 * other kernel code using trace_synth_event() and related functions.
1358 * Return: 0 if successful, error otherwise.
1360 int synth_event_create(const char *name, struct synth_field_desc *fields,
1361 unsigned int n_fields, struct module *mod)
1363 struct dynevent_cmd cmd;
1367 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1371 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1373 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1378 ret = synth_event_gen_cmd_end(&cmd);
1384 EXPORT_SYMBOL_GPL(synth_event_create);
1386 static int destroy_synth_event(struct synth_event *se)
1393 if (trace_event_dyn_busy(&se->call))
1396 ret = unregister_synth_event(se);
1398 dyn_event_remove(&se->devent);
1399 free_synth_event(se);
1406 * synth_event_delete - Delete a synthetic event
1407 * @event_name: The name of the new synthetic event
1409 * Delete a synthetic event that was created with synth_event_create().
1411 * Return: 0 if successful, error otherwise.
1413 int synth_event_delete(const char *event_name)
1415 struct synth_event *se = NULL;
1416 struct module *mod = NULL;
1419 mutex_lock(&event_mutex);
1420 se = find_synth_event(event_name);
1423 ret = destroy_synth_event(se);
1425 mutex_unlock(&event_mutex);
1428 mutex_lock(&trace_types_lock);
1430 * It is safest to reset the ring buffer if the module
1431 * being unloaded registered any events that were
1432 * used. The only worry is if a new module gets
1433 * loaded, and takes on the same id as the events of
1434 * this module. When printing out the buffer, traced
1435 * events left over from this module may be passed to
1436 * the new module events and unexpected results may
1439 tracing_reset_all_online_cpus();
1440 mutex_unlock(&trace_types_lock);
1445 EXPORT_SYMBOL_GPL(synth_event_delete);
1447 static int check_command(const char *raw_command)
1449 char **argv = NULL, *cmd, *saved_cmd, *name_and_field;
1452 cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL);
1456 name_and_field = strsep(&cmd, ";");
1457 if (!name_and_field) {
1462 if (name_and_field[0] == '!')
1465 argv = argv_split(GFP_KERNEL, name_and_field, &argc);
1480 static int create_or_delete_synth_event(const char *raw_command)
1482 char *name = NULL, *fields, *p;
1485 raw_command = skip_spaces(raw_command);
1486 if (raw_command[0] == '\0')
1489 last_cmd_set(raw_command);
1491 ret = check_command(raw_command);
1493 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1497 p = strpbrk(raw_command, " \t");
1498 if (!p && raw_command[0] != '!') {
1499 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1504 name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL);
1508 if (name[0] == '!') {
1509 ret = synth_event_delete(name + 1);
1513 fields = skip_spaces(p);
1515 ret = __create_synth_event(name, fields);
1522 static int synth_event_run_command(struct dynevent_cmd *cmd)
1524 struct synth_event *se;
1527 ret = create_or_delete_synth_event(cmd->seq.buffer);
1531 se = find_synth_event(cmd->event_name);
1535 se->mod = cmd->private_data;
1541 * synth_event_cmd_init - Initialize a synthetic event command object
1542 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1543 * @buf: A pointer to the buffer used to build the command
1544 * @maxlen: The length of the buffer passed in @buf
1546 * Initialize a synthetic event command object. Use this before
1547 * calling any of the other dyenvent_cmd functions.
1549 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1551 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1552 synth_event_run_command);
1554 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1557 __synth_event_trace_init(struct trace_event_file *file,
1558 struct synth_event_trace_state *trace_state)
1562 memset(trace_state, '\0', sizeof(*trace_state));
1565 * Normal event tracing doesn't get called at all unless the
1566 * ENABLED bit is set (which attaches the probe thus allowing
1567 * this code to be called, etc). Because this is called
1568 * directly by the user, we don't have that but we still need
1569 * to honor not logging when disabled. For the iterated
1570 * trace case, we save the enabled state upon start and just
1571 * ignore the following data calls.
1573 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1574 trace_trigger_soft_disabled(file)) {
1575 trace_state->disabled = true;
1580 trace_state->event = file->event_call->data;
1586 __synth_event_trace_start(struct trace_event_file *file,
1587 struct synth_event_trace_state *trace_state,
1588 int dynamic_fields_size)
1590 int entry_size, fields_size = 0;
1593 fields_size = trace_state->event->n_u64 * sizeof(u64);
1594 fields_size += dynamic_fields_size;
1597 * Avoid ring buffer recursion detection, as this event
1598 * is being performed within another event.
1600 trace_state->buffer = file->tr->array_buffer.buffer;
1601 ring_buffer_nest_start(trace_state->buffer);
1603 entry_size = sizeof(*trace_state->entry) + fields_size;
1604 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1607 if (!trace_state->entry) {
1608 ring_buffer_nest_end(trace_state->buffer);
1616 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1618 trace_event_buffer_commit(&trace_state->fbuffer);
1620 ring_buffer_nest_end(trace_state->buffer);
1624 * synth_event_trace - Trace a synthetic event
1625 * @file: The trace_event_file representing the synthetic event
1626 * @n_vals: The number of values in vals
1627 * @args: Variable number of args containing the event values
1629 * Trace a synthetic event using the values passed in the variable
1632 * The argument list should be a list 'n_vals' u64 values. The number
1633 * of vals must match the number of field in the synthetic event, and
1634 * must be in the same order as the synthetic event fields.
1636 * All vals should be cast to u64, and string vals are just pointers
1637 * to strings, cast to u64. Strings will be copied into space
1638 * reserved in the event for the string, using these pointers.
1640 * Return: 0 on success, err otherwise.
1642 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1644 unsigned int i, n_u64, len, data_size = 0;
1645 struct synth_event_trace_state state;
1649 ret = __synth_event_trace_init(file, &state);
1652 ret = 0; /* just disabled, not really an error */
1656 if (state.event->n_dynamic_fields) {
1657 va_start(args, n_vals);
1659 for (i = 0; i < state.event->n_fields; i++) {
1660 u64 val = va_arg(args, u64);
1662 if (state.event->fields[i]->is_string &&
1663 state.event->fields[i]->is_dynamic) {
1664 char *str_val = (char *)(long)val;
1666 data_size += strlen(str_val) + 1;
1673 ret = __synth_event_trace_start(file, &state, data_size);
1677 if (n_vals != state.event->n_fields) {
1684 va_start(args, n_vals);
1685 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1688 val = va_arg(args, u64);
1690 if (state.event->fields[i]->is_string) {
1691 char *str_val = (char *)(long)val;
1693 len = trace_string(state.entry, state.event, str_val,
1694 state.event->fields[i]->is_dynamic,
1696 data_size += len; /* only dynamic string increments */
1698 struct synth_field *field = state.event->fields[i];
1700 switch (field->size) {
1702 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1706 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1710 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1714 state.entry->fields[n_u64] = val;
1722 __synth_event_trace_end(&state);
1726 EXPORT_SYMBOL_GPL(synth_event_trace);
1729 * synth_event_trace_array - Trace a synthetic event from an array
1730 * @file: The trace_event_file representing the synthetic event
1731 * @vals: Array of values
1732 * @n_vals: The number of values in vals
1734 * Trace a synthetic event using the values passed in as 'vals'.
1736 * The 'vals' array is just an array of 'n_vals' u64. The number of
1737 * vals must match the number of field in the synthetic event, and
1738 * must be in the same order as the synthetic event fields.
1740 * All vals should be cast to u64, and string vals are just pointers
1741 * to strings, cast to u64. Strings will be copied into space
1742 * reserved in the event for the string, using these pointers.
1744 * Return: 0 on success, err otherwise.
1746 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1747 unsigned int n_vals)
1749 unsigned int i, n_u64, field_pos, len, data_size = 0;
1750 struct synth_event_trace_state state;
1754 ret = __synth_event_trace_init(file, &state);
1757 ret = 0; /* just disabled, not really an error */
1761 if (state.event->n_dynamic_fields) {
1762 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1763 field_pos = state.event->dynamic_fields[i]->field_pos;
1764 str_val = (char *)(long)vals[field_pos];
1765 len = strlen(str_val) + 1;
1770 ret = __synth_event_trace_start(file, &state, data_size);
1774 if (n_vals != state.event->n_fields) {
1781 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1782 if (state.event->fields[i]->is_string) {
1783 char *str_val = (char *)(long)vals[i];
1785 len = trace_string(state.entry, state.event, str_val,
1786 state.event->fields[i]->is_dynamic,
1788 data_size += len; /* only dynamic string increments */
1790 struct synth_field *field = state.event->fields[i];
1793 switch (field->size) {
1795 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1799 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1803 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1807 state.entry->fields[n_u64] = val;
1814 __synth_event_trace_end(&state);
1818 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1821 * synth_event_trace_start - Start piecewise synthetic event trace
1822 * @file: The trace_event_file representing the synthetic event
1823 * @trace_state: A pointer to object tracking the piecewise trace state
1825 * Start the trace of a synthetic event field-by-field rather than all
1828 * This function 'opens' an event trace, which means space is reserved
1829 * for the event in the trace buffer, after which the event's
1830 * individual field values can be set through either
1831 * synth_event_add_next_val() or synth_event_add_val().
1833 * A pointer to a trace_state object is passed in, which will keep
1834 * track of the current event trace state until the event trace is
1835 * closed (and the event finally traced) using
1836 * synth_event_trace_end().
1838 * Note that synth_event_trace_end() must be called after all values
1839 * have been added for each event trace, regardless of whether adding
1840 * all field values succeeded or not.
1842 * Note also that for a given event trace, all fields must be added
1843 * using either synth_event_add_next_val() or synth_event_add_val()
1844 * but not both together or interleaved.
1846 * Return: 0 on success, err otherwise.
1848 int synth_event_trace_start(struct trace_event_file *file,
1849 struct synth_event_trace_state *trace_state)
1856 ret = __synth_event_trace_init(file, trace_state);
1859 ret = 0; /* just disabled, not really an error */
1863 if (trace_state->event->n_dynamic_fields)
1866 ret = __synth_event_trace_start(file, trace_state, 0);
1870 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1872 static int __synth_event_add_val(const char *field_name, u64 val,
1873 struct synth_event_trace_state *trace_state)
1875 struct synth_field *field = NULL;
1876 struct synth_trace_event *entry;
1877 struct synth_event *event;
1885 /* can't mix add_next_synth_val() with add_synth_val() */
1887 if (trace_state->add_next) {
1891 trace_state->add_name = true;
1893 if (trace_state->add_name) {
1897 trace_state->add_next = true;
1900 if (trace_state->disabled)
1903 event = trace_state->event;
1904 if (trace_state->add_name) {
1905 for (i = 0; i < event->n_fields; i++) {
1906 field = event->fields[i];
1907 if (strcmp(field->name, field_name) == 0)
1915 if (trace_state->cur_field >= event->n_fields) {
1919 field = event->fields[trace_state->cur_field++];
1922 entry = trace_state->entry;
1923 if (field->is_string) {
1924 char *str_val = (char *)(long)val;
1927 if (field->is_dynamic) { /* add_val can't do dynamic strings */
1937 str_field = (char *)&entry->fields[field->offset];
1938 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1940 switch (field->size) {
1942 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1946 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1950 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1954 trace_state->entry->fields[field->offset] = val;
1963 * synth_event_add_next_val - Add the next field's value to an open synth trace
1964 * @val: The value to set the next field to
1965 * @trace_state: A pointer to object tracking the piecewise trace state
1967 * Set the value of the next field in an event that's been opened by
1968 * synth_event_trace_start().
1970 * The val param should be the value cast to u64. If the value points
1971 * to a string, the val param should be a char * cast to u64.
1973 * This function assumes all the fields in an event are to be set one
1974 * after another - successive calls to this function are made, one for
1975 * each field, in the order of the fields in the event, until all
1976 * fields have been set. If you'd rather set each field individually
1977 * without regard to ordering, synth_event_add_val() can be used
1980 * Note however that synth_event_add_next_val() and
1981 * synth_event_add_val() can't be intermixed for a given event trace -
1982 * one or the other but not both can be used at the same time.
1984 * Note also that synth_event_trace_end() must be called after all
1985 * values have been added for each event trace, regardless of whether
1986 * adding all field values succeeded or not.
1988 * Return: 0 on success, err otherwise.
1990 int synth_event_add_next_val(u64 val,
1991 struct synth_event_trace_state *trace_state)
1993 return __synth_event_add_val(NULL, val, trace_state);
1995 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
1998 * synth_event_add_val - Add a named field's value to an open synth trace
1999 * @field_name: The name of the synthetic event field value to set
2000 * @val: The value to set the named field to
2001 * @trace_state: A pointer to object tracking the piecewise trace state
2003 * Set the value of the named field in an event that's been opened by
2004 * synth_event_trace_start().
2006 * The val param should be the value cast to u64. If the value points
2007 * to a string, the val param should be a char * cast to u64.
2009 * This function looks up the field name, and if found, sets the field
2010 * to the specified value. This lookup makes this function more
2011 * expensive than synth_event_add_next_val(), so use that or the
2012 * none-piecewise synth_event_trace() instead if efficiency is more
2015 * Note however that synth_event_add_next_val() and
2016 * synth_event_add_val() can't be intermixed for a given event trace -
2017 * one or the other but not both can be used at the same time.
2019 * Note also that synth_event_trace_end() must be called after all
2020 * values have been added for each event trace, regardless of whether
2021 * adding all field values succeeded or not.
2023 * Return: 0 on success, err otherwise.
2025 int synth_event_add_val(const char *field_name, u64 val,
2026 struct synth_event_trace_state *trace_state)
2028 return __synth_event_add_val(field_name, val, trace_state);
2030 EXPORT_SYMBOL_GPL(synth_event_add_val);
2033 * synth_event_trace_end - End piecewise synthetic event trace
2034 * @trace_state: A pointer to object tracking the piecewise trace state
2036 * End the trace of a synthetic event opened by
2037 * synth_event_trace__start().
2039 * This function 'closes' an event trace, which basically means that
2040 * it commits the reserved event and cleans up other loose ends.
2042 * A pointer to a trace_state object is passed in, which will keep
2043 * track of the current event trace state opened with
2044 * synth_event_trace_start().
2046 * Note that this function must be called after all values have been
2047 * added for each event trace, regardless of whether adding all field
2048 * values succeeded or not.
2050 * Return: 0 on success, err otherwise.
2052 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2057 __synth_event_trace_end(trace_state);
2061 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2063 static int create_synth_event(const char *raw_command)
2069 raw_command = skip_spaces(raw_command);
2070 if (raw_command[0] == '\0')
2073 last_cmd_set(raw_command);
2077 /* Don't try to process if not our system */
2078 if (name[0] != 's' || name[1] != ':')
2082 p = strpbrk(raw_command, " \t");
2084 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2088 fields = skip_spaces(p);
2090 /* This interface accepts group name prefix */
2091 if (strchr(name, '/')) {
2092 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2094 synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0);
2100 len = name - raw_command;
2102 ret = check_command(raw_command + len);
2104 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2108 name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL);
2112 ret = __create_synth_event(name, fields);
2119 static int synth_event_release(struct dyn_event *ev)
2121 struct synth_event *event = to_synth_event(ev);
2127 if (trace_event_dyn_busy(&event->call))
2130 ret = unregister_synth_event(event);
2134 dyn_event_remove(ev);
2135 free_synth_event(event);
2139 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2141 struct synth_field *field;
2145 seq_printf(m, "%s\t", event->name);
2147 for (i = 0; i < event->n_fields; i++) {
2148 field = event->fields[i];
2151 t = strstr(type, "__data_loc");
2152 if (t) { /* __data_loc belongs in format but not event desc */
2153 t += sizeof("__data_loc");
2157 /* parameter values */
2158 seq_printf(m, "%s %s%s", type, field->name,
2159 i == event->n_fields - 1 ? "" : "; ");
2167 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2169 struct synth_event *event = to_synth_event(ev);
2171 seq_printf(m, "s:%s/", event->class.system);
2173 return __synth_event_show(m, event);
2176 static int synth_events_seq_show(struct seq_file *m, void *v)
2178 struct dyn_event *ev = v;
2180 if (!is_synth_event(ev))
2183 return __synth_event_show(m, to_synth_event(ev));
2186 static const struct seq_operations synth_events_seq_op = {
2187 .start = dyn_event_seq_start,
2188 .next = dyn_event_seq_next,
2189 .stop = dyn_event_seq_stop,
2190 .show = synth_events_seq_show,
2193 static int synth_events_open(struct inode *inode, struct file *file)
2197 ret = security_locked_down(LOCKDOWN_TRACEFS);
2201 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2202 ret = dyn_events_release_all(&synth_event_ops);
2207 return seq_open(file, &synth_events_seq_op);
2210 static ssize_t synth_events_write(struct file *file,
2211 const char __user *buffer,
2212 size_t count, loff_t *ppos)
2214 return trace_parse_run_command(file, buffer, count, ppos,
2215 create_or_delete_synth_event);
2218 static const struct file_operations synth_events_fops = {
2219 .open = synth_events_open,
2220 .write = synth_events_write,
2222 .llseek = seq_lseek,
2223 .release = seq_release,
2227 * Register dynevent at core_initcall. This allows kernel to setup kprobe
2228 * events in postcore_initcall without tracefs.
2230 static __init int trace_events_synth_init_early(void)
2234 err = dyn_event_register(&synth_event_ops);
2236 pr_warn("Could not register synth_event_ops\n");
2240 core_initcall(trace_events_synth_init_early);
2242 static __init int trace_events_synth_init(void)
2244 struct dentry *entry = NULL;
2246 err = tracing_init_dentry();
2250 entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE,
2251 NULL, NULL, &synth_events_fops);
2259 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2264 fs_initcall(trace_events_synth_init);