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 DEFINE_MUTEX(lastcmd_mutex);
48 static char *last_cmd;
50 static int errpos(const char *str)
54 mutex_lock(&lastcmd_mutex);
55 if (!str || !last_cmd)
58 ret = err_pos(last_cmd, str);
60 mutex_unlock(&lastcmd_mutex);
64 static void last_cmd_set(const char *str)
69 mutex_lock(&lastcmd_mutex);
71 last_cmd = kstrdup(str, GFP_KERNEL);
72 mutex_unlock(&lastcmd_mutex);
75 static void synth_err(u8 err_type, u16 err_pos)
77 mutex_lock(&lastcmd_mutex);
81 tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
84 mutex_unlock(&lastcmd_mutex);
87 static int create_synth_event(const char *raw_command);
88 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
89 static int synth_event_release(struct dyn_event *ev);
90 static bool synth_event_is_busy(struct dyn_event *ev);
91 static bool synth_event_match(const char *system, const char *event,
92 int argc, const char **argv, struct dyn_event *ev);
94 static struct dyn_event_operations synth_event_ops = {
95 .create = create_synth_event,
96 .show = synth_event_show,
97 .is_busy = synth_event_is_busy,
98 .free = synth_event_release,
99 .match = synth_event_match,
102 static bool is_synth_event(struct dyn_event *ev)
104 return ev->ops == &synth_event_ops;
107 static struct synth_event *to_synth_event(struct dyn_event *ev)
109 return container_of(ev, struct synth_event, devent);
112 static bool synth_event_is_busy(struct dyn_event *ev)
114 struct synth_event *event = to_synth_event(ev);
116 return event->ref != 0;
119 static bool synth_event_match(const char *system, const char *event,
120 int argc, const char **argv, struct dyn_event *ev)
122 struct synth_event *sev = to_synth_event(ev);
124 return strcmp(sev->name, event) == 0 &&
125 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
128 struct synth_trace_event {
129 struct trace_entry ent;
130 union trace_synth_field fields[];
133 static int synth_event_define_fields(struct trace_event_call *call)
135 struct synth_trace_event trace;
136 int offset = offsetof(typeof(trace), fields);
137 struct synth_event *event = call->data;
138 unsigned int i, size, n_u64;
143 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
144 size = event->fields[i]->size;
145 is_signed = event->fields[i]->is_signed;
146 type = event->fields[i]->type;
147 name = event->fields[i]->name;
148 ret = trace_define_field(call, type, name, offset, size,
149 is_signed, FILTER_OTHER);
153 event->fields[i]->offset = n_u64;
155 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
156 offset += STR_VAR_LEN_MAX;
157 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
159 offset += sizeof(u64);
164 event->n_u64 = n_u64;
169 static bool synth_field_signed(char *type)
171 if (str_has_prefix(type, "u"))
173 if (strcmp(type, "gfp_t") == 0)
179 static int synth_field_is_string(char *type)
181 if (strstr(type, "char[") != NULL)
187 static int synth_field_is_stack(char *type)
189 if (strstr(type, "long[") != NULL)
195 static int synth_field_string_size(char *type)
197 char buf[4], *end, *start;
201 start = strstr(type, "char[");
204 start += sizeof("char[") - 1;
206 end = strchr(type, ']');
207 if (!end || end < start || type + strlen(type) > end + 1)
215 return 0; /* variable-length string */
217 strncpy(buf, start, len);
220 err = kstrtouint(buf, 0, &size);
224 if (size > STR_VAR_LEN_MAX)
230 static int synth_field_size(char *type)
234 if (strcmp(type, "s64") == 0)
236 else if (strcmp(type, "u64") == 0)
238 else if (strcmp(type, "s32") == 0)
240 else if (strcmp(type, "u32") == 0)
242 else if (strcmp(type, "s16") == 0)
244 else if (strcmp(type, "u16") == 0)
246 else if (strcmp(type, "s8") == 0)
248 else if (strcmp(type, "u8") == 0)
250 else if (strcmp(type, "char") == 0)
252 else if (strcmp(type, "unsigned char") == 0)
253 size = sizeof(unsigned char);
254 else if (strcmp(type, "int") == 0)
256 else if (strcmp(type, "unsigned int") == 0)
257 size = sizeof(unsigned int);
258 else if (strcmp(type, "long") == 0)
260 else if (strcmp(type, "unsigned long") == 0)
261 size = sizeof(unsigned long);
262 else if (strcmp(type, "bool") == 0)
264 else if (strcmp(type, "pid_t") == 0)
265 size = sizeof(pid_t);
266 else if (strcmp(type, "gfp_t") == 0)
267 size = sizeof(gfp_t);
268 else if (synth_field_is_string(type))
269 size = synth_field_string_size(type);
270 else if (synth_field_is_stack(type))
276 static const char *synth_field_fmt(char *type)
278 const char *fmt = "%llu";
280 if (strcmp(type, "s64") == 0)
282 else if (strcmp(type, "u64") == 0)
284 else if (strcmp(type, "s32") == 0)
286 else if (strcmp(type, "u32") == 0)
288 else if (strcmp(type, "s16") == 0)
290 else if (strcmp(type, "u16") == 0)
292 else if (strcmp(type, "s8") == 0)
294 else if (strcmp(type, "u8") == 0)
296 else if (strcmp(type, "char") == 0)
298 else if (strcmp(type, "unsigned char") == 0)
300 else if (strcmp(type, "int") == 0)
302 else if (strcmp(type, "unsigned int") == 0)
304 else if (strcmp(type, "long") == 0)
306 else if (strcmp(type, "unsigned long") == 0)
308 else if (strcmp(type, "bool") == 0)
310 else if (strcmp(type, "pid_t") == 0)
312 else if (strcmp(type, "gfp_t") == 0)
314 else if (synth_field_is_string(type))
316 else if (synth_field_is_stack(type))
322 static void print_synth_event_num_val(struct trace_seq *s,
323 char *print_fmt, char *name,
324 int size, union trace_synth_field *val, char *space)
328 trace_seq_printf(s, print_fmt, name, val->as_u8, space);
332 trace_seq_printf(s, print_fmt, name, val->as_u16, space);
336 trace_seq_printf(s, print_fmt, name, val->as_u32, space);
340 trace_seq_printf(s, print_fmt, name, val->as_u64, space);
345 static enum print_line_t print_synth_event(struct trace_iterator *iter,
347 struct trace_event *event)
349 struct trace_array *tr = iter->tr;
350 struct trace_seq *s = &iter->seq;
351 struct synth_trace_event *entry;
352 struct synth_event *se;
353 unsigned int i, j, n_u64;
357 entry = (struct synth_trace_event *)iter->ent;
358 se = container_of(event, struct synth_event, call.event);
360 trace_seq_printf(s, "%s: ", se->name);
362 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
363 if (trace_seq_has_overflowed(s))
366 fmt = synth_field_fmt(se->fields[i]->type);
368 /* parameter types */
369 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
370 trace_seq_printf(s, "%s ", fmt);
372 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
374 /* parameter values */
375 if (se->fields[i]->is_string) {
376 if (se->fields[i]->is_dynamic) {
377 union trace_synth_field *data = &entry->fields[n_u64];
379 trace_seq_printf(s, print_fmt, se->fields[i]->name,
381 (char *)entry + data->as_dynamic.offset,
382 i == se->n_fields - 1 ? "" : " ");
385 trace_seq_printf(s, print_fmt, se->fields[i]->name,
387 (char *)&entry->fields[n_u64].as_u64,
388 i == se->n_fields - 1 ? "" : " ");
389 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
391 } else if (se->fields[i]->is_stack) {
392 union trace_synth_field *data = &entry->fields[n_u64];
393 unsigned long *p = (void *)entry + data->as_dynamic.offset;
395 trace_seq_printf(s, "%s=STACK:\n", se->fields[i]->name);
396 for (j = 1; j < data->as_dynamic.len / sizeof(long); j++)
397 trace_seq_printf(s, "=> %pS\n", (void *)p[j]);
400 struct trace_print_flags __flags[] = {
401 __def_gfpflag_names, {-1, NULL} };
402 char *space = (i == se->n_fields - 1 ? "" : " ");
404 print_synth_event_num_val(s, print_fmt,
407 &entry->fields[n_u64],
410 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
411 trace_seq_puts(s, " (");
412 trace_print_flags_seq(s, "|",
413 entry->fields[n_u64].as_u64,
415 trace_seq_putc(s, ')');
421 trace_seq_putc(s, '\n');
423 return trace_handle_return(s);
426 static struct trace_event_functions synth_event_funcs = {
427 .trace = print_synth_event
430 static unsigned int trace_string(struct synth_trace_event *entry,
431 struct synth_event *event,
434 unsigned int data_size,
437 unsigned int len = 0;
442 union trace_synth_field *data = &entry->fields[*n_u64];
444 data->as_dynamic.offset = struct_size(entry, fields, event->n_u64) + data_size;
445 data->as_dynamic.len = fetch_store_strlen((unsigned long)str_val);
447 ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
451 str_field = (char *)&entry->fields[*n_u64].as_u64;
453 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
454 if ((unsigned long)str_val < TASK_SIZE)
455 ret = strncpy_from_user_nofault(str_field, str_val, STR_VAR_LEN_MAX);
458 ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX);
461 strcpy(str_field, FAULT_STRING);
463 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
469 static unsigned int trace_stack(struct synth_trace_event *entry,
470 struct synth_event *event,
472 unsigned int data_size,
475 union trace_synth_field *data = &entry->fields[*n_u64];
480 data_offset = struct_size(entry, fields, event->n_u64);
481 data_offset += data_size;
483 for (len = 0; len < HIST_STACKTRACE_DEPTH; len++) {
490 /* Find the dynamic section to copy the stack into. */
491 data_loc = (void *)entry + data_offset;
492 memcpy(data_loc, stack, len);
494 /* Fill in the field that holds the offset/len combo */
496 data->as_dynamic.offset = data_offset;
497 data->as_dynamic.len = len;
504 static notrace void trace_event_raw_event_synth(void *__data,
506 unsigned int *var_ref_idx)
508 unsigned int i, n_u64, val_idx, len, data_size = 0;
509 struct trace_event_file *trace_file = __data;
510 struct synth_trace_event *entry;
511 struct trace_event_buffer fbuffer;
512 struct trace_buffer *buffer;
513 struct synth_event *event;
516 event = trace_file->event_call->data;
518 if (trace_trigger_soft_disabled(trace_file))
521 fields_size = event->n_u64 * sizeof(u64);
523 for (i = 0; i < event->n_dynamic_fields; i++) {
524 unsigned int field_pos = event->dynamic_fields[i]->field_pos;
527 val_idx = var_ref_idx[field_pos];
528 str_val = (char *)(long)var_ref_vals[val_idx];
530 if (event->dynamic_fields[i]->is_stack) {
531 /* reserve one extra element for size */
532 len = *((unsigned long *)str_val) + 1;
533 len *= sizeof(unsigned long);
535 len = fetch_store_strlen((unsigned long)str_val);
542 * Avoid ring buffer recursion detection, as this event
543 * is being performed within another event.
545 buffer = trace_file->tr->array_buffer.buffer;
546 ring_buffer_nest_start(buffer);
548 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
549 sizeof(*entry) + fields_size);
553 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
554 val_idx = var_ref_idx[i];
555 if (event->fields[i]->is_string) {
556 char *str_val = (char *)(long)var_ref_vals[val_idx];
558 len = trace_string(entry, event, str_val,
559 event->fields[i]->is_dynamic,
561 data_size += len; /* only dynamic string increments */
562 } else if (event->fields[i]->is_stack) {
563 long *stack = (long *)(long)var_ref_vals[val_idx];
565 len = trace_stack(entry, event, stack,
569 struct synth_field *field = event->fields[i];
570 u64 val = var_ref_vals[val_idx];
572 switch (field->size) {
574 entry->fields[n_u64].as_u8 = (u8)val;
578 entry->fields[n_u64].as_u16 = (u16)val;
582 entry->fields[n_u64].as_u32 = (u32)val;
586 entry->fields[n_u64].as_u64 = val;
593 trace_event_buffer_commit(&fbuffer);
595 ring_buffer_nest_end(buffer);
598 static void free_synth_event_print_fmt(struct trace_event_call *call)
601 kfree(call->print_fmt);
602 call->print_fmt = NULL;
606 static int __set_synth_event_print_fmt(struct synth_event *event,
613 /* When len=0, we just calculate the needed length */
614 #define LEN_OR_ZERO (len ? len - pos : 0)
616 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
617 for (i = 0; i < event->n_fields; i++) {
618 fmt = synth_field_fmt(event->fields[i]->type);
619 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
620 event->fields[i]->name, fmt,
621 i == event->n_fields - 1 ? "" : ", ");
623 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
625 for (i = 0; i < event->n_fields; i++) {
626 if (event->fields[i]->is_string &&
627 event->fields[i]->is_dynamic)
628 pos += snprintf(buf + pos, LEN_OR_ZERO,
629 ", __get_str(%s)", event->fields[i]->name);
630 else if (event->fields[i]->is_stack)
631 pos += snprintf(buf + pos, LEN_OR_ZERO,
632 ", __get_stacktrace(%s)", event->fields[i]->name);
634 pos += snprintf(buf + pos, LEN_OR_ZERO,
635 ", REC->%s", event->fields[i]->name);
640 /* return the length of print_fmt */
644 static int set_synth_event_print_fmt(struct trace_event_call *call)
646 struct synth_event *event = call->data;
650 /* First: called with 0 length to calculate the needed length */
651 len = __set_synth_event_print_fmt(event, NULL, 0);
653 print_fmt = kmalloc(len + 1, GFP_KERNEL);
657 /* Second: actually write the @print_fmt */
658 __set_synth_event_print_fmt(event, print_fmt, len + 1);
659 call->print_fmt = print_fmt;
664 static void free_synth_field(struct synth_field *field)
671 static int check_field_version(const char *prefix, const char *field_type,
672 const char *field_name)
675 * For backward compatibility, the old synthetic event command
676 * format did not require semicolons, and in order to not
677 * break user space, that old format must still work. If a new
678 * feature is added, then the format that uses the new feature
679 * will be required to have semicolons, as nothing that uses
680 * the old format would be using the new, yet to be created,
681 * feature. When a new feature is added, this will detect it,
682 * and return a number greater than 1, and require the format
688 static struct synth_field *parse_synth_field(int argc, char **argv,
689 int *consumed, int *field_version)
691 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
692 struct synth_field *field;
693 int len, ret = -ENOMEM;
697 if (!strcmp(field_type, "unsigned")) {
699 synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
700 return ERR_PTR(-EINVAL);
702 prefix = "unsigned ";
703 field_type = argv[1];
704 field_name = argv[2];
707 field_name = argv[1];
712 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type));
713 return ERR_PTR(-EINVAL);
716 *field_version = check_field_version(prefix, field_type, field_name);
718 field = kzalloc(sizeof(*field), GFP_KERNEL);
720 return ERR_PTR(-ENOMEM);
722 len = strlen(field_name);
723 array = strchr(field_name, '[');
725 len -= strlen(array);
727 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
731 if (!is_good_name(field->name)) {
732 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
737 len = strlen(field_type) + 1;
740 len += strlen(array);
743 len += strlen(prefix);
745 field->type = kzalloc(len, GFP_KERNEL);
749 seq_buf_init(&s, field->type, len);
751 seq_buf_puts(&s, prefix);
752 seq_buf_puts(&s, field_type);
754 seq_buf_puts(&s, array);
755 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
758 s.buffer[s.len] = '\0';
760 size = synth_field_size(field->type);
763 synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name));
765 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
768 } else if (size == 0) {
769 if (synth_field_is_string(field->type) ||
770 synth_field_is_stack(field->type)) {
773 len = sizeof("__data_loc ") + strlen(field->type) + 1;
774 type = kzalloc(len, GFP_KERNEL);
778 seq_buf_init(&s, type, len);
779 seq_buf_puts(&s, "__data_loc ");
780 seq_buf_puts(&s, field->type);
782 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
784 s.buffer[s.len] = '\0';
789 field->is_dynamic = true;
792 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
799 if (synth_field_is_string(field->type))
800 field->is_string = true;
801 else if (synth_field_is_stack(field->type))
802 field->is_stack = true;
804 field->is_signed = synth_field_signed(field->type);
808 free_synth_field(field);
809 field = ERR_PTR(ret);
813 static void free_synth_tracepoint(struct tracepoint *tp)
822 static struct tracepoint *alloc_synth_tracepoint(char *name)
824 struct tracepoint *tp;
826 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
828 return ERR_PTR(-ENOMEM);
830 tp->name = kstrdup(name, GFP_KERNEL);
833 return ERR_PTR(-ENOMEM);
839 struct synth_event *find_synth_event(const char *name)
841 struct dyn_event *pos;
842 struct synth_event *event;
844 for_each_dyn_event(pos) {
845 if (!is_synth_event(pos))
847 event = to_synth_event(pos);
848 if (strcmp(event->name, name) == 0)
855 static struct trace_event_fields synth_event_fields_array[] = {
856 { .type = TRACE_FUNCTION_TYPE,
857 .define_fields = synth_event_define_fields },
861 static int register_synth_event(struct synth_event *event)
863 struct trace_event_call *call = &event->call;
866 event->call.class = &event->class;
867 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
868 if (!event->class.system) {
873 event->tp = alloc_synth_tracepoint(event->name);
874 if (IS_ERR(event->tp)) {
875 ret = PTR_ERR(event->tp);
880 INIT_LIST_HEAD(&call->class->fields);
881 call->event.funcs = &synth_event_funcs;
882 call->class->fields_array = synth_event_fields_array;
884 ret = register_trace_event(&call->event);
889 call->flags = TRACE_EVENT_FL_TRACEPOINT;
890 call->class->reg = trace_event_reg;
891 call->class->probe = trace_event_raw_event_synth;
893 call->tp = event->tp;
895 ret = trace_add_event_call(call);
897 pr_warn("Failed to register synthetic event: %s\n",
898 trace_event_name(call));
902 ret = set_synth_event_print_fmt(call);
903 /* unregister_trace_event() will be called inside */
905 trace_remove_event_call(call);
909 unregister_trace_event(&call->event);
913 static int unregister_synth_event(struct synth_event *event)
915 struct trace_event_call *call = &event->call;
918 ret = trace_remove_event_call(call);
923 static void free_synth_event(struct synth_event *event)
930 for (i = 0; i < event->n_fields; i++)
931 free_synth_field(event->fields[i]);
933 kfree(event->fields);
934 kfree(event->dynamic_fields);
936 kfree(event->class.system);
937 free_synth_tracepoint(event->tp);
938 free_synth_event_print_fmt(&event->call);
942 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
943 struct synth_field **fields)
945 unsigned int i, j, n_dynamic_fields = 0;
946 struct synth_event *event;
948 event = kzalloc(sizeof(*event), GFP_KERNEL);
950 event = ERR_PTR(-ENOMEM);
954 event->name = kstrdup(name, GFP_KERNEL);
957 event = ERR_PTR(-ENOMEM);
961 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
962 if (!event->fields) {
963 free_synth_event(event);
964 event = ERR_PTR(-ENOMEM);
968 for (i = 0; i < n_fields; i++)
969 if (fields[i]->is_dynamic)
972 if (n_dynamic_fields) {
973 event->dynamic_fields = kcalloc(n_dynamic_fields,
974 sizeof(*event->dynamic_fields),
976 if (!event->dynamic_fields) {
977 free_synth_event(event);
978 event = ERR_PTR(-ENOMEM);
983 dyn_event_init(&event->devent, &synth_event_ops);
985 for (i = 0, j = 0; i < n_fields; i++) {
986 fields[i]->field_pos = i;
987 event->fields[i] = fields[i];
989 if (fields[i]->is_dynamic)
990 event->dynamic_fields[j++] = fields[i];
992 event->n_dynamic_fields = j;
993 event->n_fields = n_fields;
998 static int synth_event_check_arg_fn(void *data)
1000 struct dynevent_arg_pair *arg_pair = data;
1003 size = synth_field_size((char *)arg_pair->lhs);
1005 if (strstr((char *)arg_pair->lhs, "["))
1009 return size ? 0 : -EINVAL;
1013 * synth_event_add_field - Add a new field to a synthetic event cmd
1014 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1015 * @type: The type of the new field to add
1016 * @name: The name of the new field to add
1018 * Add a new field to a synthetic event cmd object. Field ordering is in
1019 * the same order the fields are added.
1021 * See synth_field_size() for available types. If field_name contains
1022 * [n] the field is considered to be an array.
1024 * Return: 0 if successful, error otherwise.
1026 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
1029 struct dynevent_arg_pair arg_pair;
1032 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1038 dynevent_arg_pair_init(&arg_pair, 0, ';');
1040 arg_pair.lhs = type;
1041 arg_pair.rhs = name;
1043 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
1047 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1052 EXPORT_SYMBOL_GPL(synth_event_add_field);
1055 * synth_event_add_field_str - Add a new field to a synthetic event cmd
1056 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1057 * @type_name: The type and name of the new field to add, as a single string
1059 * Add a new field to a synthetic event cmd object, as a single
1060 * string. The @type_name string is expected to be of the form 'type
1061 * name', which will be appended by ';'. No sanity checking is done -
1062 * what's passed in is assumed to already be well-formed. Field
1063 * ordering is in the same order the fields are added.
1065 * See synth_field_size() for available types. If field_name contains
1066 * [n] the field is considered to be an array.
1068 * Return: 0 if successful, error otherwise.
1070 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1072 struct dynevent_arg arg;
1075 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1081 dynevent_arg_init(&arg, ';');
1083 arg.str = type_name;
1085 ret = dynevent_arg_add(cmd, &arg, NULL);
1089 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1094 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1097 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1098 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1099 * @fields: An array of type/name field descriptions
1100 * @n_fields: The number of field descriptions contained in the fields array
1102 * Add a new set of fields to a synthetic event cmd object. The event
1103 * fields that will be defined for the event should be passed in as an
1104 * array of struct synth_field_desc, and the number of elements in the
1105 * array passed in as n_fields. Field ordering will retain the
1106 * ordering given in the fields array.
1108 * See synth_field_size() for available types. If field_name contains
1109 * [n] the field is considered to be an array.
1111 * Return: 0 if successful, error otherwise.
1113 int synth_event_add_fields(struct dynevent_cmd *cmd,
1114 struct synth_field_desc *fields,
1115 unsigned int n_fields)
1120 for (i = 0; i < n_fields; i++) {
1121 if (fields[i].type == NULL || fields[i].name == NULL) {
1126 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1133 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1136 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1137 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1138 * @name: The name of the synthetic event
1139 * @mod: The module creating the event, NULL if not created from a module
1140 * @args: Variable number of arg (pairs), one pair for each field
1142 * NOTE: Users normally won't want to call this function directly, but
1143 * rather use the synth_event_gen_cmd_start() wrapper, which
1144 * automatically adds a NULL to the end of the arg list. If this
1145 * function is used directly, make sure the last arg in the variable
1148 * Generate a synthetic event command to be executed by
1149 * synth_event_gen_cmd_end(). This function can be used to generate
1150 * the complete command or only the first part of it; in the latter
1151 * case, synth_event_add_field(), synth_event_add_field_str(), or
1152 * synth_event_add_fields() can be used to add more fields following
1155 * There should be an even number variable args, each pair consisting
1156 * of a type followed by a field name.
1158 * See synth_field_size() for available types. If field_name contains
1159 * [n] the field is considered to be an array.
1161 * Return: 0 if successful, error otherwise.
1163 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1164 struct module *mod, ...)
1166 struct dynevent_arg arg;
1170 cmd->event_name = name;
1171 cmd->private_data = mod;
1173 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1176 dynevent_arg_init(&arg, 0);
1178 ret = dynevent_arg_add(cmd, &arg, NULL);
1182 va_start(args, mod);
1184 const char *type, *name;
1186 type = va_arg(args, const char *);
1189 name = va_arg(args, const char *);
1193 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1198 ret = synth_event_add_field(cmd, type, name);
1206 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1209 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1210 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1211 * @name: The name of the synthetic event
1212 * @mod: The module creating the event, NULL if not created from a module
1213 * @fields: An array of type/name field descriptions
1214 * @n_fields: The number of field descriptions contained in the fields array
1216 * Generate a synthetic event command to be executed by
1217 * synth_event_gen_cmd_end(). This function can be used to generate
1218 * the complete command or only the first part of it; in the latter
1219 * case, synth_event_add_field(), synth_event_add_field_str(), or
1220 * synth_event_add_fields() can be used to add more fields following
1223 * The event fields that will be defined for the event should be
1224 * passed in as an array of struct synth_field_desc, and the number of
1225 * elements in the array passed in as n_fields. Field ordering will
1226 * retain the ordering given in the fields array.
1228 * See synth_field_size() for available types. If field_name contains
1229 * [n] the field is considered to be an array.
1231 * Return: 0 if successful, error otherwise.
1233 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1235 struct synth_field_desc *fields,
1236 unsigned int n_fields)
1238 struct dynevent_arg arg;
1242 cmd->event_name = name;
1243 cmd->private_data = mod;
1245 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1248 if (n_fields > SYNTH_FIELDS_MAX)
1251 dynevent_arg_init(&arg, 0);
1253 ret = dynevent_arg_add(cmd, &arg, NULL);
1257 for (i = 0; i < n_fields; i++) {
1258 if (fields[i].type == NULL || fields[i].name == NULL)
1261 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1268 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1270 static int __create_synth_event(const char *name, const char *raw_fields)
1272 char **argv, *field_str, *tmp_fields, *saved_fields = NULL;
1273 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1274 int consumed, cmd_version = 1, n_fields_this_loop;
1275 int i, argc, n_fields = 0, ret = 0;
1276 struct synth_event *event = NULL;
1280 * - Add synthetic event: <event_name> field[;field] ...
1281 * - Remove synthetic event: !<event_name> field[;field] ...
1282 * where 'field' = type field_name
1285 if (name[0] == '\0') {
1286 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1290 if (!is_good_name(name)) {
1291 synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1295 mutex_lock(&event_mutex);
1297 event = find_synth_event(name);
1299 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1304 tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
1310 while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
1311 argv = argv_split(GFP_KERNEL, field_str, &argc);
1322 n_fields_this_loop = 0;
1324 while (argc > consumed) {
1327 field = parse_synth_field(argc - consumed,
1328 argv + consumed, &consumed,
1330 if (IS_ERR(field)) {
1331 ret = PTR_ERR(field);
1336 * Track the highest version of any field we
1337 * found in the command.
1339 if (field_version > cmd_version)
1340 cmd_version = field_version;
1343 * Now sort out what is and isn't valid for
1344 * each supported version.
1346 * If we see more than 1 field per loop, it
1347 * means we have multiple fields between
1348 * semicolons, and that's something we no
1349 * longer support in a version 2 or greater
1352 if (cmd_version > 1 && n_fields_this_loop >= 1) {
1353 synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str));
1358 if (n_fields == SYNTH_FIELDS_MAX) {
1359 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1363 fields[n_fields++] = field;
1365 n_fields_this_loop++;
1369 if (consumed < argc) {
1370 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1377 if (n_fields == 0) {
1378 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1383 event = alloc_synth_event(name, n_fields, fields);
1384 if (IS_ERR(event)) {
1385 ret = PTR_ERR(event);
1389 ret = register_synth_event(event);
1391 dyn_event_add(&event->devent, &event->call);
1393 free_synth_event(event);
1395 mutex_unlock(&event_mutex);
1397 kfree(saved_fields);
1403 for (i = 0; i < n_fields; i++)
1404 free_synth_field(fields[i]);
1410 * synth_event_create - Create a new synthetic event
1411 * @name: The name of the new synthetic event
1412 * @fields: An array of type/name field descriptions
1413 * @n_fields: The number of field descriptions contained in the fields array
1414 * @mod: The module creating the event, NULL if not created from a module
1416 * Create a new synthetic event with the given name under the
1417 * trace/events/synthetic/ directory. The event fields that will be
1418 * defined for the event should be passed in as an array of struct
1419 * synth_field_desc, and the number elements in the array passed in as
1420 * n_fields. Field ordering will retain the ordering given in the
1423 * If the new synthetic event is being created from a module, the mod
1424 * param must be non-NULL. This will ensure that the trace buffer
1425 * won't contain unreadable events.
1427 * The new synth event should be deleted using synth_event_delete()
1428 * function. The new synthetic event can be generated from modules or
1429 * other kernel code using trace_synth_event() and related functions.
1431 * Return: 0 if successful, error otherwise.
1433 int synth_event_create(const char *name, struct synth_field_desc *fields,
1434 unsigned int n_fields, struct module *mod)
1436 struct dynevent_cmd cmd;
1440 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1444 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1446 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1451 ret = synth_event_gen_cmd_end(&cmd);
1457 EXPORT_SYMBOL_GPL(synth_event_create);
1459 static int destroy_synth_event(struct synth_event *se)
1466 if (trace_event_dyn_busy(&se->call))
1469 ret = unregister_synth_event(se);
1471 dyn_event_remove(&se->devent);
1472 free_synth_event(se);
1479 * synth_event_delete - Delete a synthetic event
1480 * @event_name: The name of the new synthetic event
1482 * Delete a synthetic event that was created with synth_event_create().
1484 * Return: 0 if successful, error otherwise.
1486 int synth_event_delete(const char *event_name)
1488 struct synth_event *se = NULL;
1489 struct module *mod = NULL;
1492 mutex_lock(&event_mutex);
1493 se = find_synth_event(event_name);
1496 ret = destroy_synth_event(se);
1498 mutex_unlock(&event_mutex);
1502 * It is safest to reset the ring buffer if the module
1503 * being unloaded registered any events that were
1504 * used. The only worry is if a new module gets
1505 * loaded, and takes on the same id as the events of
1506 * this module. When printing out the buffer, traced
1507 * events left over from this module may be passed to
1508 * the new module events and unexpected results may
1511 tracing_reset_all_online_cpus();
1516 EXPORT_SYMBOL_GPL(synth_event_delete);
1518 static int check_command(const char *raw_command)
1520 char **argv = NULL, *cmd, *saved_cmd, *name_and_field;
1523 cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL);
1527 name_and_field = strsep(&cmd, ";");
1528 if (!name_and_field) {
1533 if (name_and_field[0] == '!')
1536 argv = argv_split(GFP_KERNEL, name_and_field, &argc);
1551 static int create_or_delete_synth_event(const char *raw_command)
1553 char *name = NULL, *fields, *p;
1556 raw_command = skip_spaces(raw_command);
1557 if (raw_command[0] == '\0')
1560 last_cmd_set(raw_command);
1562 ret = check_command(raw_command);
1564 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1568 p = strpbrk(raw_command, " \t");
1569 if (!p && raw_command[0] != '!') {
1570 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1575 name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL);
1579 if (name[0] == '!') {
1580 ret = synth_event_delete(name + 1);
1584 fields = skip_spaces(p);
1586 ret = __create_synth_event(name, fields);
1593 static int synth_event_run_command(struct dynevent_cmd *cmd)
1595 struct synth_event *se;
1598 ret = create_or_delete_synth_event(cmd->seq.buffer);
1602 se = find_synth_event(cmd->event_name);
1606 se->mod = cmd->private_data;
1612 * synth_event_cmd_init - Initialize a synthetic event command object
1613 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1614 * @buf: A pointer to the buffer used to build the command
1615 * @maxlen: The length of the buffer passed in @buf
1617 * Initialize a synthetic event command object. Use this before
1618 * calling any of the other dyenvent_cmd functions.
1620 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1622 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1623 synth_event_run_command);
1625 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1628 __synth_event_trace_init(struct trace_event_file *file,
1629 struct synth_event_trace_state *trace_state)
1633 memset(trace_state, '\0', sizeof(*trace_state));
1636 * Normal event tracing doesn't get called at all unless the
1637 * ENABLED bit is set (which attaches the probe thus allowing
1638 * this code to be called, etc). Because this is called
1639 * directly by the user, we don't have that but we still need
1640 * to honor not logging when disabled. For the iterated
1641 * trace case, we save the enabled state upon start and just
1642 * ignore the following data calls.
1644 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1645 trace_trigger_soft_disabled(file)) {
1646 trace_state->disabled = true;
1651 trace_state->event = file->event_call->data;
1657 __synth_event_trace_start(struct trace_event_file *file,
1658 struct synth_event_trace_state *trace_state,
1659 int dynamic_fields_size)
1661 int entry_size, fields_size = 0;
1664 fields_size = trace_state->event->n_u64 * sizeof(u64);
1665 fields_size += dynamic_fields_size;
1668 * Avoid ring buffer recursion detection, as this event
1669 * is being performed within another event.
1671 trace_state->buffer = file->tr->array_buffer.buffer;
1672 ring_buffer_nest_start(trace_state->buffer);
1674 entry_size = sizeof(*trace_state->entry) + fields_size;
1675 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1678 if (!trace_state->entry) {
1679 ring_buffer_nest_end(trace_state->buffer);
1687 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1689 trace_event_buffer_commit(&trace_state->fbuffer);
1691 ring_buffer_nest_end(trace_state->buffer);
1695 * synth_event_trace - Trace a synthetic event
1696 * @file: The trace_event_file representing the synthetic event
1697 * @n_vals: The number of values in vals
1698 * @args: Variable number of args containing the event values
1700 * Trace a synthetic event using the values passed in the variable
1703 * The argument list should be a list 'n_vals' u64 values. The number
1704 * of vals must match the number of field in the synthetic event, and
1705 * must be in the same order as the synthetic event fields.
1707 * All vals should be cast to u64, and string vals are just pointers
1708 * to strings, cast to u64. Strings will be copied into space
1709 * reserved in the event for the string, using these pointers.
1711 * Return: 0 on success, err otherwise.
1713 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1715 unsigned int i, n_u64, len, data_size = 0;
1716 struct synth_event_trace_state state;
1720 ret = __synth_event_trace_init(file, &state);
1723 ret = 0; /* just disabled, not really an error */
1727 if (state.event->n_dynamic_fields) {
1728 va_start(args, n_vals);
1730 for (i = 0; i < state.event->n_fields; i++) {
1731 u64 val = va_arg(args, u64);
1733 if (state.event->fields[i]->is_string &&
1734 state.event->fields[i]->is_dynamic) {
1735 char *str_val = (char *)(long)val;
1737 data_size += strlen(str_val) + 1;
1744 ret = __synth_event_trace_start(file, &state, data_size);
1748 if (n_vals != state.event->n_fields) {
1755 va_start(args, n_vals);
1756 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1759 val = va_arg(args, u64);
1761 if (state.event->fields[i]->is_string) {
1762 char *str_val = (char *)(long)val;
1764 len = trace_string(state.entry, state.event, str_val,
1765 state.event->fields[i]->is_dynamic,
1767 data_size += len; /* only dynamic string increments */
1769 struct synth_field *field = state.event->fields[i];
1771 switch (field->size) {
1773 state.entry->fields[n_u64].as_u8 = (u8)val;
1777 state.entry->fields[n_u64].as_u16 = (u16)val;
1781 state.entry->fields[n_u64].as_u32 = (u32)val;
1785 state.entry->fields[n_u64].as_u64 = val;
1793 __synth_event_trace_end(&state);
1797 EXPORT_SYMBOL_GPL(synth_event_trace);
1800 * synth_event_trace_array - Trace a synthetic event from an array
1801 * @file: The trace_event_file representing the synthetic event
1802 * @vals: Array of values
1803 * @n_vals: The number of values in vals
1805 * Trace a synthetic event using the values passed in as 'vals'.
1807 * The 'vals' array is just an array of 'n_vals' u64. The number of
1808 * vals must match the number of field in the synthetic event, and
1809 * must be in the same order as the synthetic event fields.
1811 * All vals should be cast to u64, and string vals are just pointers
1812 * to strings, cast to u64. Strings will be copied into space
1813 * reserved in the event for the string, using these pointers.
1815 * Return: 0 on success, err otherwise.
1817 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1818 unsigned int n_vals)
1820 unsigned int i, n_u64, field_pos, len, data_size = 0;
1821 struct synth_event_trace_state state;
1825 ret = __synth_event_trace_init(file, &state);
1828 ret = 0; /* just disabled, not really an error */
1832 if (state.event->n_dynamic_fields) {
1833 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1834 field_pos = state.event->dynamic_fields[i]->field_pos;
1835 str_val = (char *)(long)vals[field_pos];
1836 len = strlen(str_val) + 1;
1841 ret = __synth_event_trace_start(file, &state, data_size);
1845 if (n_vals != state.event->n_fields) {
1852 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1853 if (state.event->fields[i]->is_string) {
1854 char *str_val = (char *)(long)vals[i];
1856 len = trace_string(state.entry, state.event, str_val,
1857 state.event->fields[i]->is_dynamic,
1859 data_size += len; /* only dynamic string increments */
1861 struct synth_field *field = state.event->fields[i];
1864 switch (field->size) {
1866 state.entry->fields[n_u64].as_u8 = (u8)val;
1870 state.entry->fields[n_u64].as_u16 = (u16)val;
1874 state.entry->fields[n_u64].as_u32 = (u32)val;
1878 state.entry->fields[n_u64].as_u64 = val;
1885 __synth_event_trace_end(&state);
1889 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1892 * synth_event_trace_start - Start piecewise synthetic event trace
1893 * @file: The trace_event_file representing the synthetic event
1894 * @trace_state: A pointer to object tracking the piecewise trace state
1896 * Start the trace of a synthetic event field-by-field rather than all
1899 * This function 'opens' an event trace, which means space is reserved
1900 * for the event in the trace buffer, after which the event's
1901 * individual field values can be set through either
1902 * synth_event_add_next_val() or synth_event_add_val().
1904 * A pointer to a trace_state object is passed in, which will keep
1905 * track of the current event trace state until the event trace is
1906 * closed (and the event finally traced) using
1907 * synth_event_trace_end().
1909 * Note that synth_event_trace_end() must be called after all values
1910 * have been added for each event trace, regardless of whether adding
1911 * all field values succeeded or not.
1913 * Note also that for a given event trace, all fields must be added
1914 * using either synth_event_add_next_val() or synth_event_add_val()
1915 * but not both together or interleaved.
1917 * Return: 0 on success, err otherwise.
1919 int synth_event_trace_start(struct trace_event_file *file,
1920 struct synth_event_trace_state *trace_state)
1927 ret = __synth_event_trace_init(file, trace_state);
1930 ret = 0; /* just disabled, not really an error */
1934 if (trace_state->event->n_dynamic_fields)
1937 ret = __synth_event_trace_start(file, trace_state, 0);
1941 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1943 static int __synth_event_add_val(const char *field_name, u64 val,
1944 struct synth_event_trace_state *trace_state)
1946 struct synth_field *field = NULL;
1947 struct synth_trace_event *entry;
1948 struct synth_event *event;
1956 /* can't mix add_next_synth_val() with add_synth_val() */
1958 if (trace_state->add_next) {
1962 trace_state->add_name = true;
1964 if (trace_state->add_name) {
1968 trace_state->add_next = true;
1971 if (trace_state->disabled)
1974 event = trace_state->event;
1975 if (trace_state->add_name) {
1976 for (i = 0; i < event->n_fields; i++) {
1977 field = event->fields[i];
1978 if (strcmp(field->name, field_name) == 0)
1986 if (trace_state->cur_field >= event->n_fields) {
1990 field = event->fields[trace_state->cur_field++];
1993 entry = trace_state->entry;
1994 if (field->is_string) {
1995 char *str_val = (char *)(long)val;
1998 if (field->is_dynamic) { /* add_val can't do dynamic strings */
2008 str_field = (char *)&entry->fields[field->offset];
2009 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2011 switch (field->size) {
2013 trace_state->entry->fields[field->offset].as_u8 = (u8)val;
2017 trace_state->entry->fields[field->offset].as_u16 = (u16)val;
2021 trace_state->entry->fields[field->offset].as_u32 = (u32)val;
2025 trace_state->entry->fields[field->offset].as_u64 = val;
2034 * synth_event_add_next_val - Add the next field's value to an open synth trace
2035 * @val: The value to set the next field to
2036 * @trace_state: A pointer to object tracking the piecewise trace state
2038 * Set the value of the next field in an event that's been opened by
2039 * synth_event_trace_start().
2041 * The val param should be the value cast to u64. If the value points
2042 * to a string, the val param should be a char * cast to u64.
2044 * This function assumes all the fields in an event are to be set one
2045 * after another - successive calls to this function are made, one for
2046 * each field, in the order of the fields in the event, until all
2047 * fields have been set. If you'd rather set each field individually
2048 * without regard to ordering, synth_event_add_val() can be used
2051 * Note however that synth_event_add_next_val() and
2052 * synth_event_add_val() can't be intermixed for a given event trace -
2053 * one or the other but not both can be used at the same time.
2055 * Note also that synth_event_trace_end() must be called after all
2056 * values have been added for each event trace, regardless of whether
2057 * adding all field values succeeded or not.
2059 * Return: 0 on success, err otherwise.
2061 int synth_event_add_next_val(u64 val,
2062 struct synth_event_trace_state *trace_state)
2064 return __synth_event_add_val(NULL, val, trace_state);
2066 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2069 * synth_event_add_val - Add a named field's value to an open synth trace
2070 * @field_name: The name of the synthetic event field value to set
2071 * @val: The value to set the named field to
2072 * @trace_state: A pointer to object tracking the piecewise trace state
2074 * Set the value of the named field in an event that's been opened by
2075 * synth_event_trace_start().
2077 * The val param should be the value cast to u64. If the value points
2078 * to a string, the val param should be a char * cast to u64.
2080 * This function looks up the field name, and if found, sets the field
2081 * to the specified value. This lookup makes this function more
2082 * expensive than synth_event_add_next_val(), so use that or the
2083 * none-piecewise synth_event_trace() instead if efficiency is more
2086 * Note however that synth_event_add_next_val() and
2087 * synth_event_add_val() can't be intermixed for a given event trace -
2088 * one or the other but not both can be used at the same time.
2090 * Note also that synth_event_trace_end() must be called after all
2091 * values have been added for each event trace, regardless of whether
2092 * adding all field values succeeded or not.
2094 * Return: 0 on success, err otherwise.
2096 int synth_event_add_val(const char *field_name, u64 val,
2097 struct synth_event_trace_state *trace_state)
2099 return __synth_event_add_val(field_name, val, trace_state);
2101 EXPORT_SYMBOL_GPL(synth_event_add_val);
2104 * synth_event_trace_end - End piecewise synthetic event trace
2105 * @trace_state: A pointer to object tracking the piecewise trace state
2107 * End the trace of a synthetic event opened by
2108 * synth_event_trace__start().
2110 * This function 'closes' an event trace, which basically means that
2111 * it commits the reserved event and cleans up other loose ends.
2113 * A pointer to a trace_state object is passed in, which will keep
2114 * track of the current event trace state opened with
2115 * synth_event_trace_start().
2117 * Note that this function must be called after all values have been
2118 * added for each event trace, regardless of whether adding all field
2119 * values succeeded or not.
2121 * Return: 0 on success, err otherwise.
2123 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2128 __synth_event_trace_end(trace_state);
2132 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2134 static int create_synth_event(const char *raw_command)
2140 raw_command = skip_spaces(raw_command);
2141 if (raw_command[0] == '\0')
2144 last_cmd_set(raw_command);
2148 /* Don't try to process if not our system */
2149 if (name[0] != 's' || name[1] != ':')
2153 p = strpbrk(raw_command, " \t");
2155 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2159 fields = skip_spaces(p);
2161 /* This interface accepts group name prefix */
2162 if (strchr(name, '/')) {
2163 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2165 synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0);
2171 len = name - raw_command;
2173 ret = check_command(raw_command + len);
2175 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2179 name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL);
2183 ret = __create_synth_event(name, fields);
2190 static int synth_event_release(struct dyn_event *ev)
2192 struct synth_event *event = to_synth_event(ev);
2198 if (trace_event_dyn_busy(&event->call))
2201 ret = unregister_synth_event(event);
2205 dyn_event_remove(ev);
2206 free_synth_event(event);
2210 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2212 struct synth_field *field;
2216 seq_printf(m, "%s\t", event->name);
2218 for (i = 0; i < event->n_fields; i++) {
2219 field = event->fields[i];
2222 t = strstr(type, "__data_loc");
2223 if (t) { /* __data_loc belongs in format but not event desc */
2224 t += sizeof("__data_loc");
2228 /* parameter values */
2229 seq_printf(m, "%s %s%s", type, field->name,
2230 i == event->n_fields - 1 ? "" : "; ");
2238 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2240 struct synth_event *event = to_synth_event(ev);
2242 seq_printf(m, "s:%s/", event->class.system);
2244 return __synth_event_show(m, event);
2247 static int synth_events_seq_show(struct seq_file *m, void *v)
2249 struct dyn_event *ev = v;
2251 if (!is_synth_event(ev))
2254 return __synth_event_show(m, to_synth_event(ev));
2257 static const struct seq_operations synth_events_seq_op = {
2258 .start = dyn_event_seq_start,
2259 .next = dyn_event_seq_next,
2260 .stop = dyn_event_seq_stop,
2261 .show = synth_events_seq_show,
2264 static int synth_events_open(struct inode *inode, struct file *file)
2268 ret = security_locked_down(LOCKDOWN_TRACEFS);
2272 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2273 ret = dyn_events_release_all(&synth_event_ops);
2278 return seq_open(file, &synth_events_seq_op);
2281 static ssize_t synth_events_write(struct file *file,
2282 const char __user *buffer,
2283 size_t count, loff_t *ppos)
2285 return trace_parse_run_command(file, buffer, count, ppos,
2286 create_or_delete_synth_event);
2289 static const struct file_operations synth_events_fops = {
2290 .open = synth_events_open,
2291 .write = synth_events_write,
2293 .llseek = seq_lseek,
2294 .release = seq_release,
2298 * Register dynevent at core_initcall. This allows kernel to setup kprobe
2299 * events in postcore_initcall without tracefs.
2301 static __init int trace_events_synth_init_early(void)
2305 err = dyn_event_register(&synth_event_ops);
2307 pr_warn("Could not register synth_event_ops\n");
2311 core_initcall(trace_events_synth_init_early);
2313 static __init int trace_events_synth_init(void)
2315 struct dentry *entry = NULL;
2317 err = tracing_init_dentry();
2321 entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE,
2322 NULL, NULL, &synth_events_fops);
2330 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2335 fs_initcall(trace_events_synth_init);