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"
23 static int create_synth_event(int argc, const char **argv);
24 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
25 static int synth_event_release(struct dyn_event *ev);
26 static bool synth_event_is_busy(struct dyn_event *ev);
27 static bool synth_event_match(const char *system, const char *event,
28 int argc, const char **argv, struct dyn_event *ev);
30 static struct dyn_event_operations synth_event_ops = {
31 .create = create_synth_event,
32 .show = synth_event_show,
33 .is_busy = synth_event_is_busy,
34 .free = synth_event_release,
35 .match = synth_event_match,
38 static bool is_synth_event(struct dyn_event *ev)
40 return ev->ops == &synth_event_ops;
43 static struct synth_event *to_synth_event(struct dyn_event *ev)
45 return container_of(ev, struct synth_event, devent);
48 static bool synth_event_is_busy(struct dyn_event *ev)
50 struct synth_event *event = to_synth_event(ev);
52 return event->ref != 0;
55 static bool synth_event_match(const char *system, const char *event,
56 int argc, const char **argv, struct dyn_event *ev)
58 struct synth_event *sev = to_synth_event(ev);
60 return strcmp(sev->name, event) == 0 &&
61 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
64 struct synth_trace_event {
65 struct trace_entry ent;
69 static int synth_event_define_fields(struct trace_event_call *call)
71 struct synth_trace_event trace;
72 int offset = offsetof(typeof(trace), fields);
73 struct synth_event *event = call->data;
74 unsigned int i, size, n_u64;
79 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
80 size = event->fields[i]->size;
81 is_signed = event->fields[i]->is_signed;
82 type = event->fields[i]->type;
83 name = event->fields[i]->name;
84 ret = trace_define_field(call, type, name, offset, size,
85 is_signed, FILTER_OTHER);
89 event->fields[i]->offset = n_u64;
91 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
92 offset += STR_VAR_LEN_MAX;
93 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
95 offset += sizeof(u64);
100 event->n_u64 = n_u64;
105 static bool synth_field_signed(char *type)
107 if (str_has_prefix(type, "u"))
109 if (strcmp(type, "gfp_t") == 0)
115 static int synth_field_is_string(char *type)
117 if (strstr(type, "char[") != NULL)
123 static int synth_field_string_size(char *type)
125 char buf[4], *end, *start;
129 start = strstr(type, "char[");
132 start += sizeof("char[") - 1;
134 end = strchr(type, ']');
135 if (!end || end < start)
143 return 0; /* variable-length string */
145 strncpy(buf, start, len);
148 err = kstrtouint(buf, 0, &size);
152 if (size > STR_VAR_LEN_MAX)
158 static int synth_field_size(char *type)
162 if (strcmp(type, "s64") == 0)
164 else if (strcmp(type, "u64") == 0)
166 else if (strcmp(type, "s32") == 0)
168 else if (strcmp(type, "u32") == 0)
170 else if (strcmp(type, "s16") == 0)
172 else if (strcmp(type, "u16") == 0)
174 else if (strcmp(type, "s8") == 0)
176 else if (strcmp(type, "u8") == 0)
178 else if (strcmp(type, "char") == 0)
180 else if (strcmp(type, "unsigned char") == 0)
181 size = sizeof(unsigned char);
182 else if (strcmp(type, "int") == 0)
184 else if (strcmp(type, "unsigned int") == 0)
185 size = sizeof(unsigned int);
186 else if (strcmp(type, "long") == 0)
188 else if (strcmp(type, "unsigned long") == 0)
189 size = sizeof(unsigned long);
190 else if (strcmp(type, "pid_t") == 0)
191 size = sizeof(pid_t);
192 else if (strcmp(type, "gfp_t") == 0)
193 size = sizeof(gfp_t);
194 else if (synth_field_is_string(type))
195 size = synth_field_string_size(type);
200 static const char *synth_field_fmt(char *type)
202 const char *fmt = "%llu";
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)
224 else if (strcmp(type, "int") == 0)
226 else if (strcmp(type, "unsigned int") == 0)
228 else if (strcmp(type, "long") == 0)
230 else if (strcmp(type, "unsigned long") == 0)
232 else if (strcmp(type, "pid_t") == 0)
234 else if (strcmp(type, "gfp_t") == 0)
236 else if (synth_field_is_string(type))
242 static void print_synth_event_num_val(struct trace_seq *s,
243 char *print_fmt, char *name,
244 int size, u64 val, char *space)
248 trace_seq_printf(s, print_fmt, name, (u8)val, space);
252 trace_seq_printf(s, print_fmt, name, (u16)val, space);
256 trace_seq_printf(s, print_fmt, name, (u32)val, space);
260 trace_seq_printf(s, print_fmt, name, val, space);
265 static enum print_line_t print_synth_event(struct trace_iterator *iter,
267 struct trace_event *event)
269 struct trace_array *tr = iter->tr;
270 struct trace_seq *s = &iter->seq;
271 struct synth_trace_event *entry;
272 struct synth_event *se;
273 unsigned int i, n_u64;
277 entry = (struct synth_trace_event *)iter->ent;
278 se = container_of(event, struct synth_event, call.event);
280 trace_seq_printf(s, "%s: ", se->name);
282 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
283 if (trace_seq_has_overflowed(s))
286 fmt = synth_field_fmt(se->fields[i]->type);
288 /* parameter types */
289 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
290 trace_seq_printf(s, "%s ", fmt);
292 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
294 /* parameter values */
295 if (se->fields[i]->is_string) {
296 if (se->fields[i]->is_dynamic) {
297 u32 offset, data_offset;
300 offset = (u32)entry->fields[n_u64];
301 data_offset = offset & 0xffff;
303 str_field = (char *)entry + data_offset;
305 trace_seq_printf(s, print_fmt, se->fields[i]->name,
307 i == se->n_fields - 1 ? "" : " ");
310 trace_seq_printf(s, print_fmt, se->fields[i]->name,
311 (char *)&entry->fields[n_u64],
312 i == se->n_fields - 1 ? "" : " ");
313 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
316 struct trace_print_flags __flags[] = {
317 __def_gfpflag_names, {-1, NULL} };
318 char *space = (i == se->n_fields - 1 ? "" : " ");
320 print_synth_event_num_val(s, print_fmt,
323 entry->fields[n_u64],
326 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
327 trace_seq_puts(s, " (");
328 trace_print_flags_seq(s, "|",
329 entry->fields[n_u64],
331 trace_seq_putc(s, ')');
337 trace_seq_putc(s, '\n');
339 return trace_handle_return(s);
342 static struct trace_event_functions synth_event_funcs = {
343 .trace = print_synth_event
346 static unsigned int trace_string(struct synth_trace_event *entry,
347 struct synth_event *event,
350 unsigned int data_size,
353 unsigned int len = 0;
359 data_offset = offsetof(typeof(*entry), fields);
360 data_offset += event->n_u64 * sizeof(u64);
361 data_offset += data_size;
363 str_field = (char *)entry + data_offset;
365 len = strlen(str_val) + 1;
366 strscpy(str_field, str_val, len);
368 data_offset |= len << 16;
369 *(u32 *)&entry->fields[*n_u64] = data_offset;
373 str_field = (char *)&entry->fields[*n_u64];
375 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
376 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
382 static notrace void trace_event_raw_event_synth(void *__data,
384 unsigned int *var_ref_idx)
386 unsigned int i, n_u64, val_idx, len, data_size = 0;
387 struct trace_event_file *trace_file = __data;
388 struct synth_trace_event *entry;
389 struct trace_event_buffer fbuffer;
390 struct trace_buffer *buffer;
391 struct synth_event *event;
394 event = trace_file->event_call->data;
396 if (trace_trigger_soft_disabled(trace_file))
399 fields_size = event->n_u64 * sizeof(u64);
401 for (i = 0; i < event->n_dynamic_fields; i++) {
402 unsigned int field_pos = event->dynamic_fields[i]->field_pos;
405 val_idx = var_ref_idx[field_pos];
406 str_val = (char *)(long)var_ref_vals[val_idx];
408 len = strlen(str_val) + 1;
414 * Avoid ring buffer recursion detection, as this event
415 * is being performed within another event.
417 buffer = trace_file->tr->array_buffer.buffer;
418 ring_buffer_nest_start(buffer);
420 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
421 sizeof(*entry) + fields_size);
425 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
426 val_idx = var_ref_idx[i];
427 if (event->fields[i]->is_string) {
428 char *str_val = (char *)(long)var_ref_vals[val_idx];
430 len = trace_string(entry, event, str_val,
431 event->fields[i]->is_dynamic,
433 data_size += len; /* only dynamic string increments */
435 struct synth_field *field = event->fields[i];
436 u64 val = var_ref_vals[val_idx];
438 switch (field->size) {
440 *(u8 *)&entry->fields[n_u64] = (u8)val;
444 *(u16 *)&entry->fields[n_u64] = (u16)val;
448 *(u32 *)&entry->fields[n_u64] = (u32)val;
452 entry->fields[n_u64] = val;
459 trace_event_buffer_commit(&fbuffer);
461 ring_buffer_nest_end(buffer);
464 static void free_synth_event_print_fmt(struct trace_event_call *call)
467 kfree(call->print_fmt);
468 call->print_fmt = NULL;
472 static int __set_synth_event_print_fmt(struct synth_event *event,
479 /* When len=0, we just calculate the needed length */
480 #define LEN_OR_ZERO (len ? len - pos : 0)
482 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
483 for (i = 0; i < event->n_fields; i++) {
484 fmt = synth_field_fmt(event->fields[i]->type);
485 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
486 event->fields[i]->name, fmt,
487 i == event->n_fields - 1 ? "" : ", ");
489 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
491 for (i = 0; i < event->n_fields; i++) {
492 if (event->fields[i]->is_dynamic &&
493 event->fields[i]->is_dynamic)
494 pos += snprintf(buf + pos, LEN_OR_ZERO,
495 ", __get_str(%s)", event->fields[i]->name);
497 pos += snprintf(buf + pos, LEN_OR_ZERO,
498 ", REC->%s", event->fields[i]->name);
503 /* return the length of print_fmt */
507 static int set_synth_event_print_fmt(struct trace_event_call *call)
509 struct synth_event *event = call->data;
513 /* First: called with 0 length to calculate the needed length */
514 len = __set_synth_event_print_fmt(event, NULL, 0);
516 print_fmt = kmalloc(len + 1, GFP_KERNEL);
520 /* Second: actually write the @print_fmt */
521 __set_synth_event_print_fmt(event, print_fmt, len + 1);
522 call->print_fmt = print_fmt;
527 static void free_synth_field(struct synth_field *field)
534 static struct synth_field *parse_synth_field(int argc, const char **argv,
537 struct synth_field *field;
538 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
542 if (field_type[0] == ';')
545 if (!strcmp(field_type, "unsigned")) {
547 return ERR_PTR(-EINVAL);
548 prefix = "unsigned ";
549 field_type = argv[1];
550 field_name = argv[2];
553 field_name = argv[1];
557 field = kzalloc(sizeof(*field), GFP_KERNEL);
559 return ERR_PTR(-ENOMEM);
561 len = strlen(field_name);
562 array = strchr(field_name, '[');
564 len -= strlen(array);
565 else if (field_name[len - 1] == ';')
568 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
574 if (field_type[0] == ';')
576 len = strlen(field_type) + 1;
578 len += strlen(array);
580 len += strlen(prefix);
582 field->type = kzalloc(len, GFP_KERNEL);
588 strcat(field->type, prefix);
589 strcat(field->type, field_type);
591 strcat(field->type, array);
592 if (field->type[len - 1] == ';')
593 field->type[len - 1] = '\0';
596 size = synth_field_size(field->type);
600 } else if (size == 0) {
601 if (synth_field_is_string(field->type)) {
604 type = kzalloc(sizeof("__data_loc ") + strlen(field->type) + 1, GFP_KERNEL);
610 strcat(type, "__data_loc ");
611 strcat(type, field->type);
615 field->is_dynamic = true;
624 if (synth_field_is_string(field->type))
625 field->is_string = true;
627 field->is_signed = synth_field_signed(field->type);
631 free_synth_field(field);
632 field = ERR_PTR(ret);
636 static void free_synth_tracepoint(struct tracepoint *tp)
645 static struct tracepoint *alloc_synth_tracepoint(char *name)
647 struct tracepoint *tp;
649 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
651 return ERR_PTR(-ENOMEM);
653 tp->name = kstrdup(name, GFP_KERNEL);
656 return ERR_PTR(-ENOMEM);
662 struct synth_event *find_synth_event(const char *name)
664 struct dyn_event *pos;
665 struct synth_event *event;
667 for_each_dyn_event(pos) {
668 if (!is_synth_event(pos))
670 event = to_synth_event(pos);
671 if (strcmp(event->name, name) == 0)
678 static struct trace_event_fields synth_event_fields_array[] = {
679 { .type = TRACE_FUNCTION_TYPE,
680 .define_fields = synth_event_define_fields },
684 static int register_synth_event(struct synth_event *event)
686 struct trace_event_call *call = &event->call;
689 event->call.class = &event->class;
690 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
691 if (!event->class.system) {
696 event->tp = alloc_synth_tracepoint(event->name);
697 if (IS_ERR(event->tp)) {
698 ret = PTR_ERR(event->tp);
703 INIT_LIST_HEAD(&call->class->fields);
704 call->event.funcs = &synth_event_funcs;
705 call->class->fields_array = synth_event_fields_array;
707 ret = register_trace_event(&call->event);
712 call->flags = TRACE_EVENT_FL_TRACEPOINT;
713 call->class->reg = trace_event_reg;
714 call->class->probe = trace_event_raw_event_synth;
716 call->tp = event->tp;
718 ret = trace_add_event_call(call);
720 pr_warn("Failed to register synthetic event: %s\n",
721 trace_event_name(call));
725 ret = set_synth_event_print_fmt(call);
727 trace_remove_event_call(call);
733 unregister_trace_event(&call->event);
737 static int unregister_synth_event(struct synth_event *event)
739 struct trace_event_call *call = &event->call;
742 ret = trace_remove_event_call(call);
747 static void free_synth_event(struct synth_event *event)
754 for (i = 0; i < event->n_fields; i++)
755 free_synth_field(event->fields[i]);
757 kfree(event->fields);
758 kfree(event->dynamic_fields);
760 kfree(event->class.system);
761 free_synth_tracepoint(event->tp);
762 free_synth_event_print_fmt(&event->call);
766 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
767 struct synth_field **fields)
769 unsigned int i, j, n_dynamic_fields = 0;
770 struct synth_event *event;
772 event = kzalloc(sizeof(*event), GFP_KERNEL);
774 event = ERR_PTR(-ENOMEM);
778 event->name = kstrdup(name, GFP_KERNEL);
781 event = ERR_PTR(-ENOMEM);
785 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
786 if (!event->fields) {
787 free_synth_event(event);
788 event = ERR_PTR(-ENOMEM);
792 for (i = 0; i < n_fields; i++)
793 if (fields[i]->is_dynamic)
796 if (n_dynamic_fields) {
797 event->dynamic_fields = kcalloc(n_dynamic_fields,
798 sizeof(*event->dynamic_fields),
800 if (!event->dynamic_fields) {
801 free_synth_event(event);
802 event = ERR_PTR(-ENOMEM);
807 dyn_event_init(&event->devent, &synth_event_ops);
809 for (i = 0, j = 0; i < n_fields; i++) {
810 event->fields[i] = fields[i];
812 if (fields[i]->is_dynamic) {
813 event->dynamic_fields[j] = fields[i];
814 event->dynamic_fields[j]->field_pos = i;
815 event->dynamic_fields[j++] = fields[i];
816 event->n_dynamic_fields++;
819 event->n_fields = n_fields;
824 static int synth_event_check_arg_fn(void *data)
826 struct dynevent_arg_pair *arg_pair = data;
829 size = synth_field_size((char *)arg_pair->lhs);
831 if (strstr((char *)arg_pair->lhs, "["))
835 return size ? 0 : -EINVAL;
839 * synth_event_add_field - Add a new field to a synthetic event cmd
840 * @cmd: A pointer to the dynevent_cmd struct representing the new event
841 * @type: The type of the new field to add
842 * @name: The name of the new field to add
844 * Add a new field to a synthetic event cmd object. Field ordering is in
845 * the same order the fields are added.
847 * See synth_field_size() for available types. If field_name contains
848 * [n] the field is considered to be an array.
850 * Return: 0 if successful, error otherwise.
852 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
855 struct dynevent_arg_pair arg_pair;
858 if (cmd->type != DYNEVENT_TYPE_SYNTH)
864 dynevent_arg_pair_init(&arg_pair, 0, ';');
869 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
873 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
878 EXPORT_SYMBOL_GPL(synth_event_add_field);
881 * synth_event_add_field_str - Add a new field to a synthetic event cmd
882 * @cmd: A pointer to the dynevent_cmd struct representing the new event
883 * @type_name: The type and name of the new field to add, as a single string
885 * Add a new field to a synthetic event cmd object, as a single
886 * string. The @type_name string is expected to be of the form 'type
887 * name', which will be appended by ';'. No sanity checking is done -
888 * what's passed in is assumed to already be well-formed. Field
889 * ordering is in the same order the fields are added.
891 * See synth_field_size() for available types. If field_name contains
892 * [n] the field is considered to be an array.
894 * Return: 0 if successful, error otherwise.
896 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
898 struct dynevent_arg arg;
901 if (cmd->type != DYNEVENT_TYPE_SYNTH)
907 dynevent_arg_init(&arg, ';');
911 ret = dynevent_arg_add(cmd, &arg, NULL);
915 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
920 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
923 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
924 * @cmd: A pointer to the dynevent_cmd struct representing the new event
925 * @fields: An array of type/name field descriptions
926 * @n_fields: The number of field descriptions contained in the fields array
928 * Add a new set of fields to a synthetic event cmd object. The event
929 * fields that will be defined for the event should be passed in as an
930 * array of struct synth_field_desc, and the number of elements in the
931 * array passed in as n_fields. Field ordering will retain the
932 * ordering given in the fields array.
934 * See synth_field_size() for available types. If field_name contains
935 * [n] the field is considered to be an array.
937 * Return: 0 if successful, error otherwise.
939 int synth_event_add_fields(struct dynevent_cmd *cmd,
940 struct synth_field_desc *fields,
941 unsigned int n_fields)
946 for (i = 0; i < n_fields; i++) {
947 if (fields[i].type == NULL || fields[i].name == NULL) {
952 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
959 EXPORT_SYMBOL_GPL(synth_event_add_fields);
962 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
963 * @cmd: A pointer to the dynevent_cmd struct representing the new event
964 * @name: The name of the synthetic event
965 * @mod: The module creating the event, NULL if not created from a module
966 * @args: Variable number of arg (pairs), one pair for each field
968 * NOTE: Users normally won't want to call this function directly, but
969 * rather use the synth_event_gen_cmd_start() wrapper, which
970 * automatically adds a NULL to the end of the arg list. If this
971 * function is used directly, make sure the last arg in the variable
974 * Generate a synthetic event command to be executed by
975 * synth_event_gen_cmd_end(). This function can be used to generate
976 * the complete command or only the first part of it; in the latter
977 * case, synth_event_add_field(), synth_event_add_field_str(), or
978 * synth_event_add_fields() can be used to add more fields following
981 * There should be an even number variable args, each pair consisting
982 * of a type followed by a field name.
984 * See synth_field_size() for available types. If field_name contains
985 * [n] the field is considered to be an array.
987 * Return: 0 if successful, error otherwise.
989 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
990 struct module *mod, ...)
992 struct dynevent_arg arg;
996 cmd->event_name = name;
997 cmd->private_data = mod;
999 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1002 dynevent_arg_init(&arg, 0);
1004 ret = dynevent_arg_add(cmd, &arg, NULL);
1008 va_start(args, mod);
1010 const char *type, *name;
1012 type = va_arg(args, const char *);
1015 name = va_arg(args, const char *);
1019 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1024 ret = synth_event_add_field(cmd, type, name);
1032 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1035 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1036 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1037 * @name: The name of the synthetic event
1038 * @fields: An array of type/name field descriptions
1039 * @n_fields: The number of field descriptions contained in the fields array
1041 * Generate a synthetic event command to be executed by
1042 * synth_event_gen_cmd_end(). This function can be used to generate
1043 * the complete command or only the first part of it; in the latter
1044 * case, synth_event_add_field(), synth_event_add_field_str(), or
1045 * synth_event_add_fields() can be used to add more fields following
1048 * The event fields that will be defined for the event should be
1049 * passed in as an array of struct synth_field_desc, and the number of
1050 * elements in the array passed in as n_fields. Field ordering will
1051 * retain the ordering given in the fields array.
1053 * See synth_field_size() for available types. If field_name contains
1054 * [n] the field is considered to be an array.
1056 * Return: 0 if successful, error otherwise.
1058 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1060 struct synth_field_desc *fields,
1061 unsigned int n_fields)
1063 struct dynevent_arg arg;
1067 cmd->event_name = name;
1068 cmd->private_data = mod;
1070 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1073 if (n_fields > SYNTH_FIELDS_MAX)
1076 dynevent_arg_init(&arg, 0);
1078 ret = dynevent_arg_add(cmd, &arg, NULL);
1082 for (i = 0; i < n_fields; i++) {
1083 if (fields[i].type == NULL || fields[i].name == NULL)
1086 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1093 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1095 static int __create_synth_event(int argc, const char *name, const char **argv)
1097 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1098 struct synth_event *event = NULL;
1099 int i, consumed = 0, n_fields = 0, ret = 0;
1103 * - Add synthetic event: <event_name> field[;field] ...
1104 * - Remove synthetic event: !<event_name> field[;field] ...
1105 * where 'field' = type field_name
1108 if (name[0] == '\0' || argc < 1)
1111 mutex_lock(&event_mutex);
1113 event = find_synth_event(name);
1119 for (i = 0; i < argc - 1; i++) {
1120 if (strcmp(argv[i], ";") == 0)
1122 if (n_fields == SYNTH_FIELDS_MAX) {
1127 field = parse_synth_field(argc - i, &argv[i], &consumed);
1128 if (IS_ERR(field)) {
1129 ret = PTR_ERR(field);
1132 fields[n_fields++] = field;
1136 if (i < argc && strcmp(argv[i], ";") != 0) {
1141 event = alloc_synth_event(name, n_fields, fields);
1142 if (IS_ERR(event)) {
1143 ret = PTR_ERR(event);
1147 ret = register_synth_event(event);
1149 dyn_event_add(&event->devent);
1151 free_synth_event(event);
1153 mutex_unlock(&event_mutex);
1157 for (i = 0; i < n_fields; i++)
1158 free_synth_field(fields[i]);
1164 * synth_event_create - Create a new synthetic event
1165 * @name: The name of the new sythetic event
1166 * @fields: An array of type/name field descriptions
1167 * @n_fields: The number of field descriptions contained in the fields array
1168 * @mod: The module creating the event, NULL if not created from a module
1170 * Create a new synthetic event with the given name under the
1171 * trace/events/synthetic/ directory. The event fields that will be
1172 * defined for the event should be passed in as an array of struct
1173 * synth_field_desc, and the number elements in the array passed in as
1174 * n_fields. Field ordering will retain the ordering given in the
1177 * If the new synthetic event is being created from a module, the mod
1178 * param must be non-NULL. This will ensure that the trace buffer
1179 * won't contain unreadable events.
1181 * The new synth event should be deleted using synth_event_delete()
1182 * function. The new synthetic event can be generated from modules or
1183 * other kernel code using trace_synth_event() and related functions.
1185 * Return: 0 if successful, error otherwise.
1187 int synth_event_create(const char *name, struct synth_field_desc *fields,
1188 unsigned int n_fields, struct module *mod)
1190 struct dynevent_cmd cmd;
1194 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1198 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1200 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1205 ret = synth_event_gen_cmd_end(&cmd);
1211 EXPORT_SYMBOL_GPL(synth_event_create);
1213 static int destroy_synth_event(struct synth_event *se)
1220 ret = unregister_synth_event(se);
1222 dyn_event_remove(&se->devent);
1223 free_synth_event(se);
1231 * synth_event_delete - Delete a synthetic event
1232 * @event_name: The name of the new sythetic event
1234 * Delete a synthetic event that was created with synth_event_create().
1236 * Return: 0 if successful, error otherwise.
1238 int synth_event_delete(const char *event_name)
1240 struct synth_event *se = NULL;
1241 struct module *mod = NULL;
1244 mutex_lock(&event_mutex);
1245 se = find_synth_event(event_name);
1248 ret = destroy_synth_event(se);
1250 mutex_unlock(&event_mutex);
1253 mutex_lock(&trace_types_lock);
1255 * It is safest to reset the ring buffer if the module
1256 * being unloaded registered any events that were
1257 * used. The only worry is if a new module gets
1258 * loaded, and takes on the same id as the events of
1259 * this module. When printing out the buffer, traced
1260 * events left over from this module may be passed to
1261 * the new module events and unexpected results may
1264 tracing_reset_all_online_cpus();
1265 mutex_unlock(&trace_types_lock);
1270 EXPORT_SYMBOL_GPL(synth_event_delete);
1272 static int create_or_delete_synth_event(int argc, char **argv)
1274 const char *name = argv[0];
1277 /* trace_run_command() ensures argc != 0 */
1278 if (name[0] == '!') {
1279 ret = synth_event_delete(name + 1);
1283 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1284 return ret == -ECANCELED ? -EINVAL : ret;
1287 static int synth_event_run_command(struct dynevent_cmd *cmd)
1289 struct synth_event *se;
1292 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1296 se = find_synth_event(cmd->event_name);
1300 se->mod = cmd->private_data;
1306 * synth_event_cmd_init - Initialize a synthetic event command object
1307 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1308 * @buf: A pointer to the buffer used to build the command
1309 * @maxlen: The length of the buffer passed in @buf
1311 * Initialize a synthetic event command object. Use this before
1312 * calling any of the other dyenvent_cmd functions.
1314 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1316 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1317 synth_event_run_command);
1319 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1322 __synth_event_trace_init(struct trace_event_file *file,
1323 struct synth_event_trace_state *trace_state)
1327 memset(trace_state, '\0', sizeof(*trace_state));
1330 * Normal event tracing doesn't get called at all unless the
1331 * ENABLED bit is set (which attaches the probe thus allowing
1332 * this code to be called, etc). Because this is called
1333 * directly by the user, we don't have that but we still need
1334 * to honor not logging when disabled. For the iterated
1335 * trace case, we save the enabed state upon start and just
1336 * ignore the following data calls.
1338 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1339 trace_trigger_soft_disabled(file)) {
1340 trace_state->disabled = true;
1345 trace_state->event = file->event_call->data;
1351 __synth_event_trace_start(struct trace_event_file *file,
1352 struct synth_event_trace_state *trace_state,
1353 int dynamic_fields_size)
1355 int entry_size, fields_size = 0;
1358 fields_size = trace_state->event->n_u64 * sizeof(u64);
1359 fields_size += dynamic_fields_size;
1362 * Avoid ring buffer recursion detection, as this event
1363 * is being performed within another event.
1365 trace_state->buffer = file->tr->array_buffer.buffer;
1366 ring_buffer_nest_start(trace_state->buffer);
1368 entry_size = sizeof(*trace_state->entry) + fields_size;
1369 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1372 if (!trace_state->entry) {
1373 ring_buffer_nest_end(trace_state->buffer);
1381 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1383 trace_event_buffer_commit(&trace_state->fbuffer);
1385 ring_buffer_nest_end(trace_state->buffer);
1389 * synth_event_trace - Trace a synthetic event
1390 * @file: The trace_event_file representing the synthetic event
1391 * @n_vals: The number of values in vals
1392 * @args: Variable number of args containing the event values
1394 * Trace a synthetic event using the values passed in the variable
1397 * The argument list should be a list 'n_vals' u64 values. The number
1398 * of vals must match the number of field in the synthetic event, and
1399 * must be in the same order as the synthetic event fields.
1401 * All vals should be cast to u64, and string vals are just pointers
1402 * to strings, cast to u64. Strings will be copied into space
1403 * reserved in the event for the string, using these pointers.
1405 * Return: 0 on success, err otherwise.
1407 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1409 unsigned int i, n_u64, len, data_size = 0;
1410 struct synth_event_trace_state state;
1414 ret = __synth_event_trace_init(file, &state);
1417 ret = 0; /* just disabled, not really an error */
1421 if (state.event->n_dynamic_fields) {
1422 va_start(args, n_vals);
1424 for (i = 0; i < state.event->n_fields; i++) {
1425 u64 val = va_arg(args, u64);
1427 if (state.event->fields[i]->is_string &&
1428 state.event->fields[i]->is_dynamic) {
1429 char *str_val = (char *)(long)val;
1431 data_size += strlen(str_val) + 1;
1438 ret = __synth_event_trace_start(file, &state, data_size);
1442 if (n_vals != state.event->n_fields) {
1449 va_start(args, n_vals);
1450 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1453 val = va_arg(args, u64);
1455 if (state.event->fields[i]->is_string) {
1456 char *str_val = (char *)(long)val;
1458 len = trace_string(state.entry, state.event, str_val,
1459 state.event->fields[i]->is_dynamic,
1461 data_size += len; /* only dynamic string increments */
1463 struct synth_field *field = state.event->fields[i];
1465 switch (field->size) {
1467 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1471 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1475 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1479 state.entry->fields[n_u64] = val;
1487 __synth_event_trace_end(&state);
1491 EXPORT_SYMBOL_GPL(synth_event_trace);
1494 * synth_event_trace_array - Trace a synthetic event from an array
1495 * @file: The trace_event_file representing the synthetic event
1496 * @vals: Array of values
1497 * @n_vals: The number of values in vals
1499 * Trace a synthetic event using the values passed in as 'vals'.
1501 * The 'vals' array is just an array of 'n_vals' u64. The number of
1502 * vals must match the number of field in the synthetic event, and
1503 * must be in the same order as the synthetic event fields.
1505 * All vals should be cast to u64, and string vals are just pointers
1506 * to strings, cast to u64. Strings will be copied into space
1507 * reserved in the event for the string, using these pointers.
1509 * Return: 0 on success, err otherwise.
1511 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1512 unsigned int n_vals)
1514 unsigned int i, n_u64, field_pos, len, data_size = 0;
1515 struct synth_event_trace_state state;
1519 ret = __synth_event_trace_init(file, &state);
1522 ret = 0; /* just disabled, not really an error */
1526 if (state.event->n_dynamic_fields) {
1527 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1528 field_pos = state.event->dynamic_fields[i]->field_pos;
1529 str_val = (char *)(long)vals[field_pos];
1530 len = strlen(str_val) + 1;
1535 ret = __synth_event_trace_start(file, &state, data_size);
1539 if (n_vals != state.event->n_fields) {
1546 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1547 if (state.event->fields[i]->is_string) {
1548 char *str_val = (char *)(long)vals[i];
1550 len = trace_string(state.entry, state.event, str_val,
1551 state.event->fields[i]->is_dynamic,
1553 data_size += len; /* only dynamic string increments */
1555 struct synth_field *field = state.event->fields[i];
1558 switch (field->size) {
1560 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1564 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1568 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1572 state.entry->fields[n_u64] = val;
1579 __synth_event_trace_end(&state);
1583 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1586 * synth_event_trace_start - Start piecewise synthetic event trace
1587 * @file: The trace_event_file representing the synthetic event
1588 * @trace_state: A pointer to object tracking the piecewise trace state
1590 * Start the trace of a synthetic event field-by-field rather than all
1593 * This function 'opens' an event trace, which means space is reserved
1594 * for the event in the trace buffer, after which the event's
1595 * individual field values can be set through either
1596 * synth_event_add_next_val() or synth_event_add_val().
1598 * A pointer to a trace_state object is passed in, which will keep
1599 * track of the current event trace state until the event trace is
1600 * closed (and the event finally traced) using
1601 * synth_event_trace_end().
1603 * Note that synth_event_trace_end() must be called after all values
1604 * have been added for each event trace, regardless of whether adding
1605 * all field values succeeded or not.
1607 * Note also that for a given event trace, all fields must be added
1608 * using either synth_event_add_next_val() or synth_event_add_val()
1609 * but not both together or interleaved.
1611 * Return: 0 on success, err otherwise.
1613 int synth_event_trace_start(struct trace_event_file *file,
1614 struct synth_event_trace_state *trace_state)
1621 ret = __synth_event_trace_init(file, trace_state);
1624 ret = 0; /* just disabled, not really an error */
1628 if (trace_state->event->n_dynamic_fields)
1631 ret = __synth_event_trace_start(file, trace_state, 0);
1635 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1637 static int __synth_event_add_val(const char *field_name, u64 val,
1638 struct synth_event_trace_state *trace_state)
1640 struct synth_field *field = NULL;
1641 struct synth_trace_event *entry;
1642 struct synth_event *event;
1650 /* can't mix add_next_synth_val() with add_synth_val() */
1652 if (trace_state->add_next) {
1656 trace_state->add_name = true;
1658 if (trace_state->add_name) {
1662 trace_state->add_next = true;
1665 if (trace_state->disabled)
1668 event = trace_state->event;
1669 if (trace_state->add_name) {
1670 for (i = 0; i < event->n_fields; i++) {
1671 field = event->fields[i];
1672 if (strcmp(field->name, field_name) == 0)
1680 if (trace_state->cur_field >= event->n_fields) {
1684 field = event->fields[trace_state->cur_field++];
1687 entry = trace_state->entry;
1688 if (field->is_string) {
1689 char *str_val = (char *)(long)val;
1692 if (field->is_dynamic) { /* add_val can't do dynamic strings */
1702 str_field = (char *)&entry->fields[field->offset];
1703 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1705 switch (field->size) {
1707 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1711 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1715 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1719 trace_state->entry->fields[field->offset] = val;
1728 * synth_event_add_next_val - Add the next field's value to an open synth trace
1729 * @val: The value to set the next field to
1730 * @trace_state: A pointer to object tracking the piecewise trace state
1732 * Set the value of the next field in an event that's been opened by
1733 * synth_event_trace_start().
1735 * The val param should be the value cast to u64. If the value points
1736 * to a string, the val param should be a char * cast to u64.
1738 * This function assumes all the fields in an event are to be set one
1739 * after another - successive calls to this function are made, one for
1740 * each field, in the order of the fields in the event, until all
1741 * fields have been set. If you'd rather set each field individually
1742 * without regard to ordering, synth_event_add_val() can be used
1745 * Note however that synth_event_add_next_val() and
1746 * synth_event_add_val() can't be intermixed for a given event trace -
1747 * one or the other but not both can be used at the same time.
1749 * Note also that synth_event_trace_end() must be called after all
1750 * values have been added for each event trace, regardless of whether
1751 * adding all field values succeeded or not.
1753 * Return: 0 on success, err otherwise.
1755 int synth_event_add_next_val(u64 val,
1756 struct synth_event_trace_state *trace_state)
1758 return __synth_event_add_val(NULL, val, trace_state);
1760 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
1763 * synth_event_add_val - Add a named field's value to an open synth trace
1764 * @field_name: The name of the synthetic event field value to set
1765 * @val: The value to set the next field to
1766 * @trace_state: A pointer to object tracking the piecewise trace state
1768 * Set the value of the named field in an event that's been opened by
1769 * synth_event_trace_start().
1771 * The val param should be the value cast to u64. If the value points
1772 * to a string, the val param should be a char * cast to u64.
1774 * This function looks up the field name, and if found, sets the field
1775 * to the specified value. This lookup makes this function more
1776 * expensive than synth_event_add_next_val(), so use that or the
1777 * none-piecewise synth_event_trace() instead if efficiency is more
1780 * Note however that synth_event_add_next_val() and
1781 * synth_event_add_val() can't be intermixed for a given event trace -
1782 * one or the other but not both can be used at the same time.
1784 * Note also that synth_event_trace_end() must be called after all
1785 * values have been added for each event trace, regardless of whether
1786 * adding all field values succeeded or not.
1788 * Return: 0 on success, err otherwise.
1790 int synth_event_add_val(const char *field_name, u64 val,
1791 struct synth_event_trace_state *trace_state)
1793 return __synth_event_add_val(field_name, val, trace_state);
1795 EXPORT_SYMBOL_GPL(synth_event_add_val);
1798 * synth_event_trace_end - End piecewise synthetic event trace
1799 * @trace_state: A pointer to object tracking the piecewise trace state
1801 * End the trace of a synthetic event opened by
1802 * synth_event_trace__start().
1804 * This function 'closes' an event trace, which basically means that
1805 * it commits the reserved event and cleans up other loose ends.
1807 * A pointer to a trace_state object is passed in, which will keep
1808 * track of the current event trace state opened with
1809 * synth_event_trace_start().
1811 * Note that this function must be called after all values have been
1812 * added for each event trace, regardless of whether adding all field
1813 * values succeeded or not.
1815 * Return: 0 on success, err otherwise.
1817 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
1822 __synth_event_trace_end(trace_state);
1826 EXPORT_SYMBOL_GPL(synth_event_trace_end);
1828 static int create_synth_event(int argc, const char **argv)
1830 const char *name = argv[0];
1833 if (name[0] != 's' || name[1] != ':')
1837 /* This interface accepts group name prefix */
1838 if (strchr(name, '/')) {
1839 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1844 return __create_synth_event(argc - 1, name, argv + 1);
1847 static int synth_event_release(struct dyn_event *ev)
1849 struct synth_event *event = to_synth_event(ev);
1855 ret = unregister_synth_event(event);
1859 dyn_event_remove(ev);
1860 free_synth_event(event);
1864 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1866 struct synth_field *field;
1869 seq_printf(m, "%s\t", event->name);
1871 for (i = 0; i < event->n_fields; i++) {
1872 field = event->fields[i];
1874 /* parameter values */
1875 seq_printf(m, "%s %s%s", field->type, field->name,
1876 i == event->n_fields - 1 ? "" : "; ");
1884 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1886 struct synth_event *event = to_synth_event(ev);
1888 seq_printf(m, "s:%s/", event->class.system);
1890 return __synth_event_show(m, event);
1893 static int synth_events_seq_show(struct seq_file *m, void *v)
1895 struct dyn_event *ev = v;
1897 if (!is_synth_event(ev))
1900 return __synth_event_show(m, to_synth_event(ev));
1903 static const struct seq_operations synth_events_seq_op = {
1904 .start = dyn_event_seq_start,
1905 .next = dyn_event_seq_next,
1906 .stop = dyn_event_seq_stop,
1907 .show = synth_events_seq_show,
1910 static int synth_events_open(struct inode *inode, struct file *file)
1914 ret = security_locked_down(LOCKDOWN_TRACEFS);
1918 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1919 ret = dyn_events_release_all(&synth_event_ops);
1924 return seq_open(file, &synth_events_seq_op);
1927 static ssize_t synth_events_write(struct file *file,
1928 const char __user *buffer,
1929 size_t count, loff_t *ppos)
1931 return trace_parse_run_command(file, buffer, count, ppos,
1932 create_or_delete_synth_event);
1935 static const struct file_operations synth_events_fops = {
1936 .open = synth_events_open,
1937 .write = synth_events_write,
1939 .llseek = seq_lseek,
1940 .release = seq_release,
1944 * Register dynevent at core_initcall. This allows kernel to setup kprobe
1945 * events in postcore_initcall without tracefs.
1947 static __init int trace_events_synth_init_early(void)
1951 err = dyn_event_register(&synth_event_ops);
1953 pr_warn("Could not register synth_event_ops\n");
1957 core_initcall(trace_events_synth_init_early);
1959 static __init int trace_events_synth_init(void)
1961 struct dentry *entry = NULL;
1963 err = tracing_init_dentry();
1967 entry = tracefs_create_file("synthetic_events", 0644, NULL,
1968 NULL, &synth_events_fops);
1976 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
1981 fs_initcall(trace_events_synth_init);