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>
21 #include "trace_synth.h"
25 C(BAD_NAME, "Illegal name"), \
26 C(CMD_INCOMPLETE, "Incomplete command"), \
27 C(EVENT_EXISTS, "Event already exists"), \
28 C(TOO_MANY_FIELDS, "Too many fields"), \
29 C(INCOMPLETE_TYPE, "Incomplete type"), \
30 C(INVALID_TYPE, "Invalid type"), \
31 C(INVALID_FIELD, "Invalid field"), \
32 C(CMD_TOO_LONG, "Command too long"),
35 #define C(a, b) SYNTH_ERR_##a
42 static const char *err_text[] = { ERRORS };
44 static char last_cmd[MAX_FILTER_STR_VAL];
46 static int errpos(const char *str)
48 return err_pos(last_cmd, str);
51 static void last_cmd_set(char *str)
56 strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1);
59 static void synth_err(u8 err_type, u8 err_pos)
61 tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
65 static int create_synth_event(int argc, const char **argv);
66 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
67 static int synth_event_release(struct dyn_event *ev);
68 static bool synth_event_is_busy(struct dyn_event *ev);
69 static bool synth_event_match(const char *system, const char *event,
70 int argc, const char **argv, struct dyn_event *ev);
72 static struct dyn_event_operations synth_event_ops = {
73 .create = create_synth_event,
74 .show = synth_event_show,
75 .is_busy = synth_event_is_busy,
76 .free = synth_event_release,
77 .match = synth_event_match,
80 static bool is_synth_event(struct dyn_event *ev)
82 return ev->ops == &synth_event_ops;
85 static struct synth_event *to_synth_event(struct dyn_event *ev)
87 return container_of(ev, struct synth_event, devent);
90 static bool synth_event_is_busy(struct dyn_event *ev)
92 struct synth_event *event = to_synth_event(ev);
94 return event->ref != 0;
97 static bool synth_event_match(const char *system, const char *event,
98 int argc, const char **argv, struct dyn_event *ev)
100 struct synth_event *sev = to_synth_event(ev);
102 return strcmp(sev->name, event) == 0 &&
103 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
106 struct synth_trace_event {
107 struct trace_entry ent;
111 static int synth_event_define_fields(struct trace_event_call *call)
113 struct synth_trace_event trace;
114 int offset = offsetof(typeof(trace), fields);
115 struct synth_event *event = call->data;
116 unsigned int i, size, n_u64;
121 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
122 size = event->fields[i]->size;
123 is_signed = event->fields[i]->is_signed;
124 type = event->fields[i]->type;
125 name = event->fields[i]->name;
126 ret = trace_define_field(call, type, name, offset, size,
127 is_signed, FILTER_OTHER);
131 event->fields[i]->offset = n_u64;
133 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
134 offset += STR_VAR_LEN_MAX;
135 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
137 offset += sizeof(u64);
142 event->n_u64 = n_u64;
147 static bool synth_field_signed(char *type)
149 if (str_has_prefix(type, "u"))
151 if (strcmp(type, "gfp_t") == 0)
157 static int synth_field_is_string(char *type)
159 if (strstr(type, "char[") != NULL)
165 static int synth_field_string_size(char *type)
167 char buf[4], *end, *start;
171 start = strstr(type, "char[");
174 start += sizeof("char[") - 1;
176 end = strchr(type, ']');
177 if (!end || end < start || type + strlen(type) > end + 1)
185 return 0; /* variable-length string */
187 strncpy(buf, start, len);
190 err = kstrtouint(buf, 0, &size);
194 if (size > STR_VAR_LEN_MAX)
200 static int synth_field_size(char *type)
204 if (strcmp(type, "s64") == 0)
206 else if (strcmp(type, "u64") == 0)
208 else if (strcmp(type, "s32") == 0)
210 else if (strcmp(type, "u32") == 0)
212 else if (strcmp(type, "s16") == 0)
214 else if (strcmp(type, "u16") == 0)
216 else if (strcmp(type, "s8") == 0)
218 else if (strcmp(type, "u8") == 0)
220 else if (strcmp(type, "char") == 0)
222 else if (strcmp(type, "unsigned char") == 0)
223 size = sizeof(unsigned char);
224 else if (strcmp(type, "int") == 0)
226 else if (strcmp(type, "unsigned int") == 0)
227 size = sizeof(unsigned int);
228 else if (strcmp(type, "long") == 0)
230 else if (strcmp(type, "unsigned long") == 0)
231 size = sizeof(unsigned long);
232 else if (strcmp(type, "bool") == 0)
234 else if (strcmp(type, "pid_t") == 0)
235 size = sizeof(pid_t);
236 else if (strcmp(type, "gfp_t") == 0)
237 size = sizeof(gfp_t);
238 else if (synth_field_is_string(type))
239 size = synth_field_string_size(type);
244 static const char *synth_field_fmt(char *type)
246 const char *fmt = "%llu";
248 if (strcmp(type, "s64") == 0)
250 else if (strcmp(type, "u64") == 0)
252 else if (strcmp(type, "s32") == 0)
254 else if (strcmp(type, "u32") == 0)
256 else if (strcmp(type, "s16") == 0)
258 else if (strcmp(type, "u16") == 0)
260 else if (strcmp(type, "s8") == 0)
262 else if (strcmp(type, "u8") == 0)
264 else if (strcmp(type, "char") == 0)
266 else if (strcmp(type, "unsigned char") == 0)
268 else if (strcmp(type, "int") == 0)
270 else if (strcmp(type, "unsigned int") == 0)
272 else if (strcmp(type, "long") == 0)
274 else if (strcmp(type, "unsigned long") == 0)
276 else if (strcmp(type, "bool") == 0)
278 else if (strcmp(type, "pid_t") == 0)
280 else if (strcmp(type, "gfp_t") == 0)
282 else if (synth_field_is_string(type))
288 static void print_synth_event_num_val(struct trace_seq *s,
289 char *print_fmt, char *name,
290 int size, u64 val, char *space)
294 trace_seq_printf(s, print_fmt, name, (u8)val, space);
298 trace_seq_printf(s, print_fmt, name, (u16)val, space);
302 trace_seq_printf(s, print_fmt, name, (u32)val, space);
306 trace_seq_printf(s, print_fmt, name, val, space);
311 static enum print_line_t print_synth_event(struct trace_iterator *iter,
313 struct trace_event *event)
315 struct trace_array *tr = iter->tr;
316 struct trace_seq *s = &iter->seq;
317 struct synth_trace_event *entry;
318 struct synth_event *se;
319 unsigned int i, n_u64;
323 entry = (struct synth_trace_event *)iter->ent;
324 se = container_of(event, struct synth_event, call.event);
326 trace_seq_printf(s, "%s: ", se->name);
328 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
329 if (trace_seq_has_overflowed(s))
332 fmt = synth_field_fmt(se->fields[i]->type);
334 /* parameter types */
335 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
336 trace_seq_printf(s, "%s ", fmt);
338 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
340 /* parameter values */
341 if (se->fields[i]->is_string) {
342 if (se->fields[i]->is_dynamic) {
343 u32 offset, data_offset;
346 offset = (u32)entry->fields[n_u64];
347 data_offset = offset & 0xffff;
349 str_field = (char *)entry + data_offset;
351 trace_seq_printf(s, print_fmt, se->fields[i]->name,
354 i == se->n_fields - 1 ? "" : " ");
357 trace_seq_printf(s, print_fmt, se->fields[i]->name,
359 (char *)&entry->fields[n_u64],
360 i == se->n_fields - 1 ? "" : " ");
361 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
364 struct trace_print_flags __flags[] = {
365 __def_gfpflag_names, {-1, NULL} };
366 char *space = (i == se->n_fields - 1 ? "" : " ");
368 print_synth_event_num_val(s, print_fmt,
371 entry->fields[n_u64],
374 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
375 trace_seq_puts(s, " (");
376 trace_print_flags_seq(s, "|",
377 entry->fields[n_u64],
379 trace_seq_putc(s, ')');
385 trace_seq_putc(s, '\n');
387 return trace_handle_return(s);
390 static struct trace_event_functions synth_event_funcs = {
391 .trace = print_synth_event
394 static unsigned int trace_string(struct synth_trace_event *entry,
395 struct synth_event *event,
398 unsigned int data_size,
401 unsigned int len = 0;
407 data_offset = offsetof(typeof(*entry), fields);
408 data_offset += event->n_u64 * sizeof(u64);
409 data_offset += data_size;
411 str_field = (char *)entry + data_offset;
413 len = strlen(str_val) + 1;
414 strscpy(str_field, str_val, len);
416 data_offset |= len << 16;
417 *(u32 *)&entry->fields[*n_u64] = data_offset;
421 str_field = (char *)&entry->fields[*n_u64];
423 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
424 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
430 static notrace void trace_event_raw_event_synth(void *__data,
432 unsigned int *var_ref_idx)
434 unsigned int i, n_u64, val_idx, len, data_size = 0;
435 struct trace_event_file *trace_file = __data;
436 struct synth_trace_event *entry;
437 struct trace_event_buffer fbuffer;
438 struct trace_buffer *buffer;
439 struct synth_event *event;
442 event = trace_file->event_call->data;
444 if (trace_trigger_soft_disabled(trace_file))
447 fields_size = event->n_u64 * sizeof(u64);
449 for (i = 0; i < event->n_dynamic_fields; i++) {
450 unsigned int field_pos = event->dynamic_fields[i]->field_pos;
453 val_idx = var_ref_idx[field_pos];
454 str_val = (char *)(long)var_ref_vals[val_idx];
456 len = strlen(str_val) + 1;
462 * Avoid ring buffer recursion detection, as this event
463 * is being performed within another event.
465 buffer = trace_file->tr->array_buffer.buffer;
466 ring_buffer_nest_start(buffer);
468 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
469 sizeof(*entry) + fields_size);
473 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
474 val_idx = var_ref_idx[i];
475 if (event->fields[i]->is_string) {
476 char *str_val = (char *)(long)var_ref_vals[val_idx];
478 len = trace_string(entry, event, str_val,
479 event->fields[i]->is_dynamic,
481 data_size += len; /* only dynamic string increments */
483 struct synth_field *field = event->fields[i];
484 u64 val = var_ref_vals[val_idx];
486 switch (field->size) {
488 *(u8 *)&entry->fields[n_u64] = (u8)val;
492 *(u16 *)&entry->fields[n_u64] = (u16)val;
496 *(u32 *)&entry->fields[n_u64] = (u32)val;
500 entry->fields[n_u64] = val;
507 trace_event_buffer_commit(&fbuffer);
509 ring_buffer_nest_end(buffer);
512 static void free_synth_event_print_fmt(struct trace_event_call *call)
515 kfree(call->print_fmt);
516 call->print_fmt = NULL;
520 static int __set_synth_event_print_fmt(struct synth_event *event,
527 /* When len=0, we just calculate the needed length */
528 #define LEN_OR_ZERO (len ? len - pos : 0)
530 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
531 for (i = 0; i < event->n_fields; i++) {
532 fmt = synth_field_fmt(event->fields[i]->type);
533 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
534 event->fields[i]->name, fmt,
535 i == event->n_fields - 1 ? "" : ", ");
537 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
539 for (i = 0; i < event->n_fields; i++) {
540 if (event->fields[i]->is_string &&
541 event->fields[i]->is_dynamic)
542 pos += snprintf(buf + pos, LEN_OR_ZERO,
543 ", __get_str(%s)", event->fields[i]->name);
545 pos += snprintf(buf + pos, LEN_OR_ZERO,
546 ", REC->%s", event->fields[i]->name);
551 /* return the length of print_fmt */
555 static int set_synth_event_print_fmt(struct trace_event_call *call)
557 struct synth_event *event = call->data;
561 /* First: called with 0 length to calculate the needed length */
562 len = __set_synth_event_print_fmt(event, NULL, 0);
564 print_fmt = kmalloc(len + 1, GFP_KERNEL);
568 /* Second: actually write the @print_fmt */
569 __set_synth_event_print_fmt(event, print_fmt, len + 1);
570 call->print_fmt = print_fmt;
575 static void free_synth_field(struct synth_field *field)
582 static struct synth_field *parse_synth_field(int argc, const char **argv,
585 struct synth_field *field;
586 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
590 if (field_type[0] == ';')
593 if (!strcmp(field_type, "unsigned")) {
595 synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
596 return ERR_PTR(-EINVAL);
598 prefix = "unsigned ";
599 field_type = argv[1];
600 field_name = argv[2];
603 field_name = argv[1];
607 field = kzalloc(sizeof(*field), GFP_KERNEL);
609 return ERR_PTR(-ENOMEM);
611 len = strlen(field_name);
612 array = strchr(field_name, '[');
614 len -= strlen(array);
615 else if (field_name[len - 1] == ';')
618 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
623 if (!is_good_name(field->name)) {
624 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
629 if (field_type[0] == ';')
631 len = strlen(field_type) + 1;
634 int l = strlen(array);
636 if (l && array[l - 1] == ';')
641 len += strlen(prefix);
643 field->type = kzalloc(len, GFP_KERNEL);
649 strcat(field->type, prefix);
650 strcat(field->type, field_type);
652 strcat(field->type, array);
653 if (field->type[len - 1] == ';')
654 field->type[len - 1] = '\0';
657 size = synth_field_size(field->type);
659 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
662 } else if (size == 0) {
663 if (synth_field_is_string(field->type)) {
666 type = kzalloc(sizeof("__data_loc ") + strlen(field->type) + 1, GFP_KERNEL);
672 strcat(type, "__data_loc ");
673 strcat(type, field->type);
677 field->is_dynamic = true;
680 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
687 if (synth_field_is_string(field->type))
688 field->is_string = true;
690 field->is_signed = synth_field_signed(field->type);
694 free_synth_field(field);
695 field = ERR_PTR(ret);
699 static void free_synth_tracepoint(struct tracepoint *tp)
708 static struct tracepoint *alloc_synth_tracepoint(char *name)
710 struct tracepoint *tp;
712 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
714 return ERR_PTR(-ENOMEM);
716 tp->name = kstrdup(name, GFP_KERNEL);
719 return ERR_PTR(-ENOMEM);
725 struct synth_event *find_synth_event(const char *name)
727 struct dyn_event *pos;
728 struct synth_event *event;
730 for_each_dyn_event(pos) {
731 if (!is_synth_event(pos))
733 event = to_synth_event(pos);
734 if (strcmp(event->name, name) == 0)
741 static struct trace_event_fields synth_event_fields_array[] = {
742 { .type = TRACE_FUNCTION_TYPE,
743 .define_fields = synth_event_define_fields },
747 static int register_synth_event(struct synth_event *event)
749 struct trace_event_call *call = &event->call;
752 event->call.class = &event->class;
753 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
754 if (!event->class.system) {
759 event->tp = alloc_synth_tracepoint(event->name);
760 if (IS_ERR(event->tp)) {
761 ret = PTR_ERR(event->tp);
766 INIT_LIST_HEAD(&call->class->fields);
767 call->event.funcs = &synth_event_funcs;
768 call->class->fields_array = synth_event_fields_array;
770 ret = register_trace_event(&call->event);
775 call->flags = TRACE_EVENT_FL_TRACEPOINT;
776 call->class->reg = trace_event_reg;
777 call->class->probe = trace_event_raw_event_synth;
779 call->tp = event->tp;
781 ret = trace_add_event_call(call);
783 pr_warn("Failed to register synthetic event: %s\n",
784 trace_event_name(call));
788 ret = set_synth_event_print_fmt(call);
790 trace_remove_event_call(call);
796 unregister_trace_event(&call->event);
800 static int unregister_synth_event(struct synth_event *event)
802 struct trace_event_call *call = &event->call;
805 ret = trace_remove_event_call(call);
810 static void free_synth_event(struct synth_event *event)
817 for (i = 0; i < event->n_fields; i++)
818 free_synth_field(event->fields[i]);
820 kfree(event->fields);
821 kfree(event->dynamic_fields);
823 kfree(event->class.system);
824 free_synth_tracepoint(event->tp);
825 free_synth_event_print_fmt(&event->call);
829 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
830 struct synth_field **fields)
832 unsigned int i, j, n_dynamic_fields = 0;
833 struct synth_event *event;
835 event = kzalloc(sizeof(*event), GFP_KERNEL);
837 event = ERR_PTR(-ENOMEM);
841 event->name = kstrdup(name, GFP_KERNEL);
844 event = ERR_PTR(-ENOMEM);
848 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
849 if (!event->fields) {
850 free_synth_event(event);
851 event = ERR_PTR(-ENOMEM);
855 for (i = 0; i < n_fields; i++)
856 if (fields[i]->is_dynamic)
859 if (n_dynamic_fields) {
860 event->dynamic_fields = kcalloc(n_dynamic_fields,
861 sizeof(*event->dynamic_fields),
863 if (!event->dynamic_fields) {
864 free_synth_event(event);
865 event = ERR_PTR(-ENOMEM);
870 dyn_event_init(&event->devent, &synth_event_ops);
872 for (i = 0, j = 0; i < n_fields; i++) {
873 event->fields[i] = fields[i];
875 if (fields[i]->is_dynamic) {
876 event->dynamic_fields[j] = fields[i];
877 event->dynamic_fields[j]->field_pos = i;
878 event->dynamic_fields[j++] = fields[i];
879 event->n_dynamic_fields++;
882 event->n_fields = n_fields;
887 static int synth_event_check_arg_fn(void *data)
889 struct dynevent_arg_pair *arg_pair = data;
892 size = synth_field_size((char *)arg_pair->lhs);
894 if (strstr((char *)arg_pair->lhs, "["))
898 return size ? 0 : -EINVAL;
902 * synth_event_add_field - Add a new field to a synthetic event cmd
903 * @cmd: A pointer to the dynevent_cmd struct representing the new event
904 * @type: The type of the new field to add
905 * @name: The name of the new field to add
907 * Add a new field to a synthetic event cmd object. Field ordering is in
908 * the same order the fields are added.
910 * See synth_field_size() for available types. If field_name contains
911 * [n] the field is considered to be an array.
913 * Return: 0 if successful, error otherwise.
915 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
918 struct dynevent_arg_pair arg_pair;
921 if (cmd->type != DYNEVENT_TYPE_SYNTH)
927 dynevent_arg_pair_init(&arg_pair, 0, ';');
932 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
936 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
941 EXPORT_SYMBOL_GPL(synth_event_add_field);
944 * synth_event_add_field_str - Add a new field to a synthetic event cmd
945 * @cmd: A pointer to the dynevent_cmd struct representing the new event
946 * @type_name: The type and name of the new field to add, as a single string
948 * Add a new field to a synthetic event cmd object, as a single
949 * string. The @type_name string is expected to be of the form 'type
950 * name', which will be appended by ';'. No sanity checking is done -
951 * what's passed in is assumed to already be well-formed. Field
952 * ordering is in the same order the fields are added.
954 * See synth_field_size() for available types. If field_name contains
955 * [n] the field is considered to be an array.
957 * Return: 0 if successful, error otherwise.
959 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
961 struct dynevent_arg arg;
964 if (cmd->type != DYNEVENT_TYPE_SYNTH)
970 dynevent_arg_init(&arg, ';');
974 ret = dynevent_arg_add(cmd, &arg, NULL);
978 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
983 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
986 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
987 * @cmd: A pointer to the dynevent_cmd struct representing the new event
988 * @fields: An array of type/name field descriptions
989 * @n_fields: The number of field descriptions contained in the fields array
991 * Add a new set of fields to a synthetic event cmd object. The event
992 * fields that will be defined for the event should be passed in as an
993 * array of struct synth_field_desc, and the number of elements in the
994 * array passed in as n_fields. Field ordering will retain the
995 * ordering given in the fields array.
997 * See synth_field_size() for available types. If field_name contains
998 * [n] the field is considered to be an array.
1000 * Return: 0 if successful, error otherwise.
1002 int synth_event_add_fields(struct dynevent_cmd *cmd,
1003 struct synth_field_desc *fields,
1004 unsigned int n_fields)
1009 for (i = 0; i < n_fields; i++) {
1010 if (fields[i].type == NULL || fields[i].name == NULL) {
1015 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1022 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1025 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1026 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1027 * @name: The name of the synthetic event
1028 * @mod: The module creating the event, NULL if not created from a module
1029 * @args: Variable number of arg (pairs), one pair for each field
1031 * NOTE: Users normally won't want to call this function directly, but
1032 * rather use the synth_event_gen_cmd_start() wrapper, which
1033 * automatically adds a NULL to the end of the arg list. If this
1034 * function is used directly, make sure the last arg in the variable
1037 * Generate a synthetic event command to be executed by
1038 * synth_event_gen_cmd_end(). This function can be used to generate
1039 * the complete command or only the first part of it; in the latter
1040 * case, synth_event_add_field(), synth_event_add_field_str(), or
1041 * synth_event_add_fields() can be used to add more fields following
1044 * There should be an even number variable args, each pair consisting
1045 * of a type followed by a field name.
1047 * See synth_field_size() for available types. If field_name contains
1048 * [n] the field is considered to be an array.
1050 * Return: 0 if successful, error otherwise.
1052 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1053 struct module *mod, ...)
1055 struct dynevent_arg arg;
1059 cmd->event_name = name;
1060 cmd->private_data = mod;
1062 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1065 dynevent_arg_init(&arg, 0);
1067 ret = dynevent_arg_add(cmd, &arg, NULL);
1071 va_start(args, mod);
1073 const char *type, *name;
1075 type = va_arg(args, const char *);
1078 name = va_arg(args, const char *);
1082 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1087 ret = synth_event_add_field(cmd, type, name);
1095 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1098 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1099 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1100 * @name: The name of the synthetic event
1101 * @fields: An array of type/name field descriptions
1102 * @n_fields: The number of field descriptions contained in the fields array
1104 * Generate a synthetic event command to be executed by
1105 * synth_event_gen_cmd_end(). This function can be used to generate
1106 * the complete command or only the first part of it; in the latter
1107 * case, synth_event_add_field(), synth_event_add_field_str(), or
1108 * synth_event_add_fields() can be used to add more fields following
1111 * The event fields that will be defined for the event should be
1112 * passed in as an array of struct synth_field_desc, and the number of
1113 * elements in the array passed in as n_fields. Field ordering will
1114 * retain the ordering given in the fields array.
1116 * See synth_field_size() for available types. If field_name contains
1117 * [n] the field is considered to be an array.
1119 * Return: 0 if successful, error otherwise.
1121 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1123 struct synth_field_desc *fields,
1124 unsigned int n_fields)
1126 struct dynevent_arg arg;
1130 cmd->event_name = name;
1131 cmd->private_data = mod;
1133 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1136 if (n_fields > SYNTH_FIELDS_MAX)
1139 dynevent_arg_init(&arg, 0);
1141 ret = dynevent_arg_add(cmd, &arg, NULL);
1145 for (i = 0; i < n_fields; i++) {
1146 if (fields[i].type == NULL || fields[i].name == NULL)
1149 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1156 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1158 static int save_cmdstr(int argc, const char *name, const char **argv)
1164 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1168 seq_buf_init(&s, buf, MAX_DYNEVENT_CMD_LEN);
1170 seq_buf_puts(&s, name);
1172 for (i = 0; i < argc; i++) {
1173 seq_buf_putc(&s, ' ');
1174 seq_buf_puts(&s, argv[i]);
1177 if (!seq_buf_buffer_left(&s)) {
1178 synth_err(SYNTH_ERR_CMD_TOO_LONG, 0);
1189 static int __create_synth_event(int argc, const char *name, const char **argv)
1191 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1192 struct synth_event *event = NULL;
1193 int i, consumed = 0, n_fields = 0, ret = 0;
1195 ret = save_cmdstr(argc, name, argv);
1201 * - Add synthetic event: <event_name> field[;field] ...
1202 * - Remove synthetic event: !<event_name> field[;field] ...
1203 * where 'field' = type field_name
1206 if (name[0] == '\0' || argc < 1) {
1207 synth_err(SYNTH_ERR_CMD_INCOMPLETE, 0);
1211 mutex_lock(&event_mutex);
1213 if (!is_good_name(name)) {
1214 synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1219 event = find_synth_event(name);
1221 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1226 for (i = 0; i < argc - 1; i++) {
1227 if (strcmp(argv[i], ";") == 0)
1229 if (n_fields == SYNTH_FIELDS_MAX) {
1230 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1235 field = parse_synth_field(argc - i, &argv[i], &consumed);
1236 if (IS_ERR(field)) {
1237 ret = PTR_ERR(field);
1240 fields[n_fields++] = field;
1244 if (i < argc && strcmp(argv[i], ";") != 0) {
1245 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(argv[i]));
1250 event = alloc_synth_event(name, n_fields, fields);
1251 if (IS_ERR(event)) {
1252 ret = PTR_ERR(event);
1256 ret = register_synth_event(event);
1258 dyn_event_add(&event->devent);
1260 free_synth_event(event);
1262 mutex_unlock(&event_mutex);
1266 for (i = 0; i < n_fields; i++)
1267 free_synth_field(fields[i]);
1273 * synth_event_create - Create a new synthetic event
1274 * @name: The name of the new sythetic event
1275 * @fields: An array of type/name field descriptions
1276 * @n_fields: The number of field descriptions contained in the fields array
1277 * @mod: The module creating the event, NULL if not created from a module
1279 * Create a new synthetic event with the given name under the
1280 * trace/events/synthetic/ directory. The event fields that will be
1281 * defined for the event should be passed in as an array of struct
1282 * synth_field_desc, and the number elements in the array passed in as
1283 * n_fields. Field ordering will retain the ordering given in the
1286 * If the new synthetic event is being created from a module, the mod
1287 * param must be non-NULL. This will ensure that the trace buffer
1288 * won't contain unreadable events.
1290 * The new synth event should be deleted using synth_event_delete()
1291 * function. The new synthetic event can be generated from modules or
1292 * other kernel code using trace_synth_event() and related functions.
1294 * Return: 0 if successful, error otherwise.
1296 int synth_event_create(const char *name, struct synth_field_desc *fields,
1297 unsigned int n_fields, struct module *mod)
1299 struct dynevent_cmd cmd;
1303 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1307 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1309 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1314 ret = synth_event_gen_cmd_end(&cmd);
1320 EXPORT_SYMBOL_GPL(synth_event_create);
1322 static int destroy_synth_event(struct synth_event *se)
1329 ret = unregister_synth_event(se);
1331 dyn_event_remove(&se->devent);
1332 free_synth_event(se);
1340 * synth_event_delete - Delete a synthetic event
1341 * @event_name: The name of the new sythetic event
1343 * Delete a synthetic event that was created with synth_event_create().
1345 * Return: 0 if successful, error otherwise.
1347 int synth_event_delete(const char *event_name)
1349 struct synth_event *se = NULL;
1350 struct module *mod = NULL;
1353 mutex_lock(&event_mutex);
1354 se = find_synth_event(event_name);
1357 ret = destroy_synth_event(se);
1359 mutex_unlock(&event_mutex);
1362 mutex_lock(&trace_types_lock);
1364 * It is safest to reset the ring buffer if the module
1365 * being unloaded registered any events that were
1366 * used. The only worry is if a new module gets
1367 * loaded, and takes on the same id as the events of
1368 * this module. When printing out the buffer, traced
1369 * events left over from this module may be passed to
1370 * the new module events and unexpected results may
1373 tracing_reset_all_online_cpus();
1374 mutex_unlock(&trace_types_lock);
1379 EXPORT_SYMBOL_GPL(synth_event_delete);
1381 static int create_or_delete_synth_event(int argc, char **argv)
1383 const char *name = argv[0];
1386 /* trace_run_command() ensures argc != 0 */
1387 if (name[0] == '!') {
1388 ret = synth_event_delete(name + 1);
1392 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1393 return ret == -ECANCELED ? -EINVAL : ret;
1396 static int synth_event_run_command(struct dynevent_cmd *cmd)
1398 struct synth_event *se;
1401 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1405 se = find_synth_event(cmd->event_name);
1409 se->mod = cmd->private_data;
1415 * synth_event_cmd_init - Initialize a synthetic event command object
1416 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1417 * @buf: A pointer to the buffer used to build the command
1418 * @maxlen: The length of the buffer passed in @buf
1420 * Initialize a synthetic event command object. Use this before
1421 * calling any of the other dyenvent_cmd functions.
1423 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1425 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1426 synth_event_run_command);
1428 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1431 __synth_event_trace_init(struct trace_event_file *file,
1432 struct synth_event_trace_state *trace_state)
1436 memset(trace_state, '\0', sizeof(*trace_state));
1439 * Normal event tracing doesn't get called at all unless the
1440 * ENABLED bit is set (which attaches the probe thus allowing
1441 * this code to be called, etc). Because this is called
1442 * directly by the user, we don't have that but we still need
1443 * to honor not logging when disabled. For the iterated
1444 * trace case, we save the enabed state upon start and just
1445 * ignore the following data calls.
1447 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1448 trace_trigger_soft_disabled(file)) {
1449 trace_state->disabled = true;
1454 trace_state->event = file->event_call->data;
1460 __synth_event_trace_start(struct trace_event_file *file,
1461 struct synth_event_trace_state *trace_state,
1462 int dynamic_fields_size)
1464 int entry_size, fields_size = 0;
1467 fields_size = trace_state->event->n_u64 * sizeof(u64);
1468 fields_size += dynamic_fields_size;
1471 * Avoid ring buffer recursion detection, as this event
1472 * is being performed within another event.
1474 trace_state->buffer = file->tr->array_buffer.buffer;
1475 ring_buffer_nest_start(trace_state->buffer);
1477 entry_size = sizeof(*trace_state->entry) + fields_size;
1478 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1481 if (!trace_state->entry) {
1482 ring_buffer_nest_end(trace_state->buffer);
1490 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1492 trace_event_buffer_commit(&trace_state->fbuffer);
1494 ring_buffer_nest_end(trace_state->buffer);
1498 * synth_event_trace - Trace a synthetic event
1499 * @file: The trace_event_file representing the synthetic event
1500 * @n_vals: The number of values in vals
1501 * @args: Variable number of args containing the event values
1503 * Trace a synthetic event using the values passed in the variable
1506 * The argument list should be a list 'n_vals' u64 values. The number
1507 * of vals must match the number of field in the synthetic event, and
1508 * must be in the same order as the synthetic event fields.
1510 * All vals should be cast to u64, and string vals are just pointers
1511 * to strings, cast to u64. Strings will be copied into space
1512 * reserved in the event for the string, using these pointers.
1514 * Return: 0 on success, err otherwise.
1516 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1518 unsigned int i, n_u64, len, data_size = 0;
1519 struct synth_event_trace_state state;
1523 ret = __synth_event_trace_init(file, &state);
1526 ret = 0; /* just disabled, not really an error */
1530 if (state.event->n_dynamic_fields) {
1531 va_start(args, n_vals);
1533 for (i = 0; i < state.event->n_fields; i++) {
1534 u64 val = va_arg(args, u64);
1536 if (state.event->fields[i]->is_string &&
1537 state.event->fields[i]->is_dynamic) {
1538 char *str_val = (char *)(long)val;
1540 data_size += strlen(str_val) + 1;
1547 ret = __synth_event_trace_start(file, &state, data_size);
1551 if (n_vals != state.event->n_fields) {
1558 va_start(args, n_vals);
1559 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1562 val = va_arg(args, u64);
1564 if (state.event->fields[i]->is_string) {
1565 char *str_val = (char *)(long)val;
1567 len = trace_string(state.entry, state.event, str_val,
1568 state.event->fields[i]->is_dynamic,
1570 data_size += len; /* only dynamic string increments */
1572 struct synth_field *field = state.event->fields[i];
1574 switch (field->size) {
1576 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1580 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1584 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1588 state.entry->fields[n_u64] = val;
1596 __synth_event_trace_end(&state);
1600 EXPORT_SYMBOL_GPL(synth_event_trace);
1603 * synth_event_trace_array - Trace a synthetic event from an array
1604 * @file: The trace_event_file representing the synthetic event
1605 * @vals: Array of values
1606 * @n_vals: The number of values in vals
1608 * Trace a synthetic event using the values passed in as 'vals'.
1610 * The 'vals' array is just an array of 'n_vals' u64. The number of
1611 * vals must match the number of field in the synthetic event, and
1612 * must be in the same order as the synthetic event fields.
1614 * All vals should be cast to u64, and string vals are just pointers
1615 * to strings, cast to u64. Strings will be copied into space
1616 * reserved in the event for the string, using these pointers.
1618 * Return: 0 on success, err otherwise.
1620 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1621 unsigned int n_vals)
1623 unsigned int i, n_u64, field_pos, len, data_size = 0;
1624 struct synth_event_trace_state state;
1628 ret = __synth_event_trace_init(file, &state);
1631 ret = 0; /* just disabled, not really an error */
1635 if (state.event->n_dynamic_fields) {
1636 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1637 field_pos = state.event->dynamic_fields[i]->field_pos;
1638 str_val = (char *)(long)vals[field_pos];
1639 len = strlen(str_val) + 1;
1644 ret = __synth_event_trace_start(file, &state, data_size);
1648 if (n_vals != state.event->n_fields) {
1655 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1656 if (state.event->fields[i]->is_string) {
1657 char *str_val = (char *)(long)vals[i];
1659 len = trace_string(state.entry, state.event, str_val,
1660 state.event->fields[i]->is_dynamic,
1662 data_size += len; /* only dynamic string increments */
1664 struct synth_field *field = state.event->fields[i];
1667 switch (field->size) {
1669 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1673 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1677 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1681 state.entry->fields[n_u64] = val;
1688 __synth_event_trace_end(&state);
1692 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1695 * synth_event_trace_start - Start piecewise synthetic event trace
1696 * @file: The trace_event_file representing the synthetic event
1697 * @trace_state: A pointer to object tracking the piecewise trace state
1699 * Start the trace of a synthetic event field-by-field rather than all
1702 * This function 'opens' an event trace, which means space is reserved
1703 * for the event in the trace buffer, after which the event's
1704 * individual field values can be set through either
1705 * synth_event_add_next_val() or synth_event_add_val().
1707 * A pointer to a trace_state object is passed in, which will keep
1708 * track of the current event trace state until the event trace is
1709 * closed (and the event finally traced) using
1710 * synth_event_trace_end().
1712 * Note that synth_event_trace_end() must be called after all values
1713 * have been added for each event trace, regardless of whether adding
1714 * all field values succeeded or not.
1716 * Note also that for a given event trace, all fields must be added
1717 * using either synth_event_add_next_val() or synth_event_add_val()
1718 * but not both together or interleaved.
1720 * Return: 0 on success, err otherwise.
1722 int synth_event_trace_start(struct trace_event_file *file,
1723 struct synth_event_trace_state *trace_state)
1730 ret = __synth_event_trace_init(file, trace_state);
1733 ret = 0; /* just disabled, not really an error */
1737 if (trace_state->event->n_dynamic_fields)
1740 ret = __synth_event_trace_start(file, trace_state, 0);
1744 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1746 static int __synth_event_add_val(const char *field_name, u64 val,
1747 struct synth_event_trace_state *trace_state)
1749 struct synth_field *field = NULL;
1750 struct synth_trace_event *entry;
1751 struct synth_event *event;
1759 /* can't mix add_next_synth_val() with add_synth_val() */
1761 if (trace_state->add_next) {
1765 trace_state->add_name = true;
1767 if (trace_state->add_name) {
1771 trace_state->add_next = true;
1774 if (trace_state->disabled)
1777 event = trace_state->event;
1778 if (trace_state->add_name) {
1779 for (i = 0; i < event->n_fields; i++) {
1780 field = event->fields[i];
1781 if (strcmp(field->name, field_name) == 0)
1789 if (trace_state->cur_field >= event->n_fields) {
1793 field = event->fields[trace_state->cur_field++];
1796 entry = trace_state->entry;
1797 if (field->is_string) {
1798 char *str_val = (char *)(long)val;
1801 if (field->is_dynamic) { /* add_val can't do dynamic strings */
1811 str_field = (char *)&entry->fields[field->offset];
1812 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1814 switch (field->size) {
1816 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1820 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1824 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1828 trace_state->entry->fields[field->offset] = val;
1837 * synth_event_add_next_val - Add the next field's value to an open synth trace
1838 * @val: The value to set the next field to
1839 * @trace_state: A pointer to object tracking the piecewise trace state
1841 * Set the value of the next field in an event that's been opened by
1842 * synth_event_trace_start().
1844 * The val param should be the value cast to u64. If the value points
1845 * to a string, the val param should be a char * cast to u64.
1847 * This function assumes all the fields in an event are to be set one
1848 * after another - successive calls to this function are made, one for
1849 * each field, in the order of the fields in the event, until all
1850 * fields have been set. If you'd rather set each field individually
1851 * without regard to ordering, synth_event_add_val() can be used
1854 * Note however that synth_event_add_next_val() and
1855 * synth_event_add_val() can't be intermixed for a given event trace -
1856 * one or the other but not both can be used at the same time.
1858 * Note also that synth_event_trace_end() must be called after all
1859 * values have been added for each event trace, regardless of whether
1860 * adding all field values succeeded or not.
1862 * Return: 0 on success, err otherwise.
1864 int synth_event_add_next_val(u64 val,
1865 struct synth_event_trace_state *trace_state)
1867 return __synth_event_add_val(NULL, val, trace_state);
1869 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
1872 * synth_event_add_val - Add a named field's value to an open synth trace
1873 * @field_name: The name of the synthetic event field value to set
1874 * @val: The value to set the next field to
1875 * @trace_state: A pointer to object tracking the piecewise trace state
1877 * Set the value of the named field in an event that's been opened by
1878 * synth_event_trace_start().
1880 * The val param should be the value cast to u64. If the value points
1881 * to a string, the val param should be a char * cast to u64.
1883 * This function looks up the field name, and if found, sets the field
1884 * to the specified value. This lookup makes this function more
1885 * expensive than synth_event_add_next_val(), so use that or the
1886 * none-piecewise synth_event_trace() instead if efficiency is more
1889 * Note however that synth_event_add_next_val() and
1890 * synth_event_add_val() can't be intermixed for a given event trace -
1891 * one or the other but not both can be used at the same time.
1893 * Note also that synth_event_trace_end() must be called after all
1894 * values have been added for each event trace, regardless of whether
1895 * adding all field values succeeded or not.
1897 * Return: 0 on success, err otherwise.
1899 int synth_event_add_val(const char *field_name, u64 val,
1900 struct synth_event_trace_state *trace_state)
1902 return __synth_event_add_val(field_name, val, trace_state);
1904 EXPORT_SYMBOL_GPL(synth_event_add_val);
1907 * synth_event_trace_end - End piecewise synthetic event trace
1908 * @trace_state: A pointer to object tracking the piecewise trace state
1910 * End the trace of a synthetic event opened by
1911 * synth_event_trace__start().
1913 * This function 'closes' an event trace, which basically means that
1914 * it commits the reserved event and cleans up other loose ends.
1916 * A pointer to a trace_state object is passed in, which will keep
1917 * track of the current event trace state opened with
1918 * synth_event_trace_start().
1920 * Note that this function must be called after all values have been
1921 * added for each event trace, regardless of whether adding all field
1922 * values succeeded or not.
1924 * Return: 0 on success, err otherwise.
1926 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
1931 __synth_event_trace_end(trace_state);
1935 EXPORT_SYMBOL_GPL(synth_event_trace_end);
1937 static int create_synth_event(int argc, const char **argv)
1939 const char *name = argv[0];
1942 if (name[0] != 's' || name[1] != ':')
1946 /* This interface accepts group name prefix */
1947 if (strchr(name, '/')) {
1948 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1953 return __create_synth_event(argc - 1, name, argv + 1);
1956 static int synth_event_release(struct dyn_event *ev)
1958 struct synth_event *event = to_synth_event(ev);
1964 ret = unregister_synth_event(event);
1968 dyn_event_remove(ev);
1969 free_synth_event(event);
1973 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1975 struct synth_field *field;
1979 seq_printf(m, "%s\t", event->name);
1981 for (i = 0; i < event->n_fields; i++) {
1982 field = event->fields[i];
1985 t = strstr(type, "__data_loc");
1986 if (t) { /* __data_loc belongs in format but not event desc */
1987 t += sizeof("__data_loc");
1991 /* parameter values */
1992 seq_printf(m, "%s %s%s", type, field->name,
1993 i == event->n_fields - 1 ? "" : "; ");
2001 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2003 struct synth_event *event = to_synth_event(ev);
2005 seq_printf(m, "s:%s/", event->class.system);
2007 return __synth_event_show(m, event);
2010 static int synth_events_seq_show(struct seq_file *m, void *v)
2012 struct dyn_event *ev = v;
2014 if (!is_synth_event(ev))
2017 return __synth_event_show(m, to_synth_event(ev));
2020 static const struct seq_operations synth_events_seq_op = {
2021 .start = dyn_event_seq_start,
2022 .next = dyn_event_seq_next,
2023 .stop = dyn_event_seq_stop,
2024 .show = synth_events_seq_show,
2027 static int synth_events_open(struct inode *inode, struct file *file)
2031 ret = security_locked_down(LOCKDOWN_TRACEFS);
2035 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2036 ret = dyn_events_release_all(&synth_event_ops);
2041 return seq_open(file, &synth_events_seq_op);
2044 static ssize_t synth_events_write(struct file *file,
2045 const char __user *buffer,
2046 size_t count, loff_t *ppos)
2048 return trace_parse_run_command(file, buffer, count, ppos,
2049 create_or_delete_synth_event);
2052 static const struct file_operations synth_events_fops = {
2053 .open = synth_events_open,
2054 .write = synth_events_write,
2056 .llseek = seq_lseek,
2057 .release = seq_release,
2061 * Register dynevent at core_initcall. This allows kernel to setup kprobe
2062 * events in postcore_initcall without tracefs.
2064 static __init int trace_events_synth_init_early(void)
2068 err = dyn_event_register(&synth_event_ops);
2070 pr_warn("Could not register synth_event_ops\n");
2074 core_initcall(trace_events_synth_init_early);
2076 static __init int trace_events_synth_init(void)
2078 struct dentry *entry = NULL;
2080 err = tracing_init_dentry();
2084 entry = tracefs_create_file("synthetic_events", 0644, NULL,
2085 NULL, &synth_events_fops);
2093 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2098 fs_initcall(trace_events_synth_init);