1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_synth - synthetic trace events
5 * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
20 #include "trace_probe.h"
21 #include "trace_probe_kernel.h"
23 #include "trace_synth.h"
27 C(BAD_NAME, "Illegal name"), \
28 C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\
29 C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\
30 C(EVENT_EXISTS, "Event already exists"), \
31 C(TOO_MANY_FIELDS, "Too many fields"), \
32 C(INCOMPLETE_TYPE, "Incomplete type"), \
33 C(INVALID_TYPE, "Invalid type"), \
34 C(INVALID_FIELD, "Invalid field"), \
35 C(INVALID_ARRAY_SPEC, "Invalid array specification"),
38 #define C(a, b) SYNTH_ERR_##a
45 static const char *err_text[] = { ERRORS };
47 static char last_cmd[MAX_FILTER_STR_VAL];
49 static int errpos(const char *str)
51 return err_pos(last_cmd, str);
54 static void last_cmd_set(const char *str)
59 strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1);
62 static void synth_err(u8 err_type, u8 err_pos)
64 tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
68 static int create_synth_event(const char *raw_command);
69 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
70 static int synth_event_release(struct dyn_event *ev);
71 static bool synth_event_is_busy(struct dyn_event *ev);
72 static bool synth_event_match(const char *system, const char *event,
73 int argc, const char **argv, struct dyn_event *ev);
75 static struct dyn_event_operations synth_event_ops = {
76 .create = create_synth_event,
77 .show = synth_event_show,
78 .is_busy = synth_event_is_busy,
79 .free = synth_event_release,
80 .match = synth_event_match,
83 static bool is_synth_event(struct dyn_event *ev)
85 return ev->ops == &synth_event_ops;
88 static struct synth_event *to_synth_event(struct dyn_event *ev)
90 return container_of(ev, struct synth_event, devent);
93 static bool synth_event_is_busy(struct dyn_event *ev)
95 struct synth_event *event = to_synth_event(ev);
97 return event->ref != 0;
100 static bool synth_event_match(const char *system, const char *event,
101 int argc, const char **argv, struct dyn_event *ev)
103 struct synth_event *sev = to_synth_event(ev);
105 return strcmp(sev->name, event) == 0 &&
106 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
109 struct synth_trace_event {
110 struct trace_entry ent;
114 static int synth_event_define_fields(struct trace_event_call *call)
116 struct synth_trace_event trace;
117 int offset = offsetof(typeof(trace), fields);
118 struct synth_event *event = call->data;
119 unsigned int i, size, n_u64;
124 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
125 size = event->fields[i]->size;
126 is_signed = event->fields[i]->is_signed;
127 type = event->fields[i]->type;
128 name = event->fields[i]->name;
129 ret = trace_define_field(call, type, name, offset, size,
130 is_signed, FILTER_OTHER);
134 event->fields[i]->offset = n_u64;
136 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
137 offset += STR_VAR_LEN_MAX;
138 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
140 offset += sizeof(u64);
145 event->n_u64 = n_u64;
150 static bool synth_field_signed(char *type)
152 if (str_has_prefix(type, "u"))
154 if (strcmp(type, "gfp_t") == 0)
160 static int synth_field_is_string(char *type)
162 if (strstr(type, "char[") != NULL)
168 static int synth_field_is_stack(char *type)
170 if (strstr(type, "long[") != NULL)
176 static int synth_field_string_size(char *type)
178 char buf[4], *end, *start;
182 start = strstr(type, "char[");
185 start += sizeof("char[") - 1;
187 end = strchr(type, ']');
188 if (!end || end < start || type + strlen(type) > end + 1)
196 return 0; /* variable-length string */
198 strncpy(buf, start, len);
201 err = kstrtouint(buf, 0, &size);
205 if (size > STR_VAR_LEN_MAX)
211 static int synth_field_size(char *type)
215 if (strcmp(type, "s64") == 0)
217 else if (strcmp(type, "u64") == 0)
219 else if (strcmp(type, "s32") == 0)
221 else if (strcmp(type, "u32") == 0)
223 else if (strcmp(type, "s16") == 0)
225 else if (strcmp(type, "u16") == 0)
227 else if (strcmp(type, "s8") == 0)
229 else if (strcmp(type, "u8") == 0)
231 else if (strcmp(type, "char") == 0)
233 else if (strcmp(type, "unsigned char") == 0)
234 size = sizeof(unsigned char);
235 else if (strcmp(type, "int") == 0)
237 else if (strcmp(type, "unsigned int") == 0)
238 size = sizeof(unsigned int);
239 else if (strcmp(type, "long") == 0)
241 else if (strcmp(type, "unsigned long") == 0)
242 size = sizeof(unsigned long);
243 else if (strcmp(type, "bool") == 0)
245 else if (strcmp(type, "pid_t") == 0)
246 size = sizeof(pid_t);
247 else if (strcmp(type, "gfp_t") == 0)
248 size = sizeof(gfp_t);
249 else if (synth_field_is_string(type))
250 size = synth_field_string_size(type);
251 else if (synth_field_is_stack(type))
257 static const char *synth_field_fmt(char *type)
259 const char *fmt = "%llu";
261 if (strcmp(type, "s64") == 0)
263 else if (strcmp(type, "u64") == 0)
265 else if (strcmp(type, "s32") == 0)
267 else if (strcmp(type, "u32") == 0)
269 else if (strcmp(type, "s16") == 0)
271 else if (strcmp(type, "u16") == 0)
273 else if (strcmp(type, "s8") == 0)
275 else if (strcmp(type, "u8") == 0)
277 else if (strcmp(type, "char") == 0)
279 else if (strcmp(type, "unsigned char") == 0)
281 else if (strcmp(type, "int") == 0)
283 else if (strcmp(type, "unsigned int") == 0)
285 else if (strcmp(type, "long") == 0)
287 else if (strcmp(type, "unsigned long") == 0)
289 else if (strcmp(type, "bool") == 0)
291 else if (strcmp(type, "pid_t") == 0)
293 else if (strcmp(type, "gfp_t") == 0)
295 else if (synth_field_is_string(type))
297 else if (synth_field_is_stack(type))
303 static void print_synth_event_num_val(struct trace_seq *s,
304 char *print_fmt, char *name,
305 int size, u64 val, char *space)
309 trace_seq_printf(s, print_fmt, name, (u8)val, space);
313 trace_seq_printf(s, print_fmt, name, (u16)val, space);
317 trace_seq_printf(s, print_fmt, name, (u32)val, space);
321 trace_seq_printf(s, print_fmt, name, val, space);
326 static enum print_line_t print_synth_event(struct trace_iterator *iter,
328 struct trace_event *event)
330 struct trace_array *tr = iter->tr;
331 struct trace_seq *s = &iter->seq;
332 struct synth_trace_event *entry;
333 struct synth_event *se;
334 unsigned int i, n_u64;
338 entry = (struct synth_trace_event *)iter->ent;
339 se = container_of(event, struct synth_event, call.event);
341 trace_seq_printf(s, "%s: ", se->name);
343 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
344 if (trace_seq_has_overflowed(s))
347 fmt = synth_field_fmt(se->fields[i]->type);
349 /* parameter types */
350 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
351 trace_seq_printf(s, "%s ", fmt);
353 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
355 /* parameter values */
356 if (se->fields[i]->is_string) {
357 if (se->fields[i]->is_dynamic) {
358 u32 offset, data_offset;
361 offset = (u32)entry->fields[n_u64];
362 data_offset = offset & 0xffff;
364 str_field = (char *)entry + data_offset;
366 trace_seq_printf(s, print_fmt, se->fields[i]->name,
369 i == se->n_fields - 1 ? "" : " ");
372 trace_seq_printf(s, print_fmt, se->fields[i]->name,
374 (char *)&entry->fields[n_u64],
375 i == se->n_fields - 1 ? "" : " ");
376 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
378 } else if (se->fields[i]->is_stack) {
379 u32 offset, data_offset, len;
380 unsigned long *p, *end;
382 offset = (u32)entry->fields[n_u64];
383 data_offset = offset & 0xffff;
386 p = (void *)entry + data_offset;
387 end = (void *)p + len - (sizeof(long) - 1);
389 trace_seq_printf(s, "%s=STACK:\n", se->fields[i]->name);
391 for (; *p && p < end; p++)
392 trace_seq_printf(s, "=> %pS\n", (void *)*p);
396 struct trace_print_flags __flags[] = {
397 __def_gfpflag_names, {-1, NULL} };
398 char *space = (i == se->n_fields - 1 ? "" : " ");
400 print_synth_event_num_val(s, print_fmt,
403 entry->fields[n_u64],
406 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
407 trace_seq_puts(s, " (");
408 trace_print_flags_seq(s, "|",
409 entry->fields[n_u64],
411 trace_seq_putc(s, ')');
417 trace_seq_putc(s, '\n');
419 return trace_handle_return(s);
422 static struct trace_event_functions synth_event_funcs = {
423 .trace = print_synth_event
426 static unsigned int trace_string(struct synth_trace_event *entry,
427 struct synth_event *event,
430 unsigned int data_size,
433 unsigned int len = 0;
440 data_offset = offsetof(typeof(*entry), fields);
441 data_offset += event->n_u64 * sizeof(u64);
442 data_offset += data_size;
444 len = kern_fetch_store_strlen((unsigned long)str_val);
446 data_offset |= len << 16;
447 *(u32 *)&entry->fields[*n_u64] = data_offset;
449 ret = kern_fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
453 str_field = (char *)&entry->fields[*n_u64];
455 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
456 if ((unsigned long)str_val < TASK_SIZE)
457 ret = strncpy_from_user_nofault(str_field, str_val, STR_VAR_LEN_MAX);
460 ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX);
463 strcpy(str_field, FAULT_STRING);
465 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
471 static unsigned int trace_stack(struct synth_trace_event *entry,
472 struct synth_event *event,
474 unsigned int data_size,
481 data_offset = struct_size(entry, fields, event->n_u64);
482 data_offset += data_size;
484 for (len = 0; len < HIST_STACKTRACE_DEPTH; len++) {
489 /* Include the zero'd element if it fits */
490 if (len < HIST_STACKTRACE_DEPTH)
495 /* Find the dynamic section to copy the stack into. */
496 data_loc = (void *)entry + data_offset;
497 memcpy(data_loc, stack, len);
499 /* Fill in the field that holds the offset/len combo */
500 data_offset |= len << 16;
501 *(u32 *)&entry->fields[*n_u64] = data_offset;
508 static notrace void trace_event_raw_event_synth(void *__data,
510 unsigned int *var_ref_idx)
512 unsigned int i, n_u64, val_idx, len, data_size = 0;
513 struct trace_event_file *trace_file = __data;
514 struct synth_trace_event *entry;
515 struct trace_event_buffer fbuffer;
516 struct trace_buffer *buffer;
517 struct synth_event *event;
520 event = trace_file->event_call->data;
522 if (trace_trigger_soft_disabled(trace_file))
525 fields_size = event->n_u64 * sizeof(u64);
527 for (i = 0; i < event->n_dynamic_fields; i++) {
528 unsigned int field_pos = event->dynamic_fields[i]->field_pos;
531 val_idx = var_ref_idx[field_pos];
532 str_val = (char *)(long)var_ref_vals[val_idx];
534 len = kern_fetch_store_strlen((unsigned long)str_val);
540 * Avoid ring buffer recursion detection, as this event
541 * is being performed within another event.
543 buffer = trace_file->tr->array_buffer.buffer;
544 ring_buffer_nest_start(buffer);
546 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
547 sizeof(*entry) + fields_size);
551 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
552 val_idx = var_ref_idx[i];
553 if (event->fields[i]->is_string) {
554 char *str_val = (char *)(long)var_ref_vals[val_idx];
556 len = trace_string(entry, event, str_val,
557 event->fields[i]->is_dynamic,
559 data_size += len; /* only dynamic string increments */
560 } else if (event->fields[i]->is_stack) {
561 long *stack = (long *)(long)var_ref_vals[val_idx];
563 len = trace_stack(entry, event, stack,
567 struct synth_field *field = event->fields[i];
568 u64 val = var_ref_vals[val_idx];
570 switch (field->size) {
572 *(u8 *)&entry->fields[n_u64] = (u8)val;
576 *(u16 *)&entry->fields[n_u64] = (u16)val;
580 *(u32 *)&entry->fields[n_u64] = (u32)val;
584 entry->fields[n_u64] = val;
591 trace_event_buffer_commit(&fbuffer);
593 ring_buffer_nest_end(buffer);
596 static void free_synth_event_print_fmt(struct trace_event_call *call)
599 kfree(call->print_fmt);
600 call->print_fmt = NULL;
604 static int __set_synth_event_print_fmt(struct synth_event *event,
611 /* When len=0, we just calculate the needed length */
612 #define LEN_OR_ZERO (len ? len - pos : 0)
614 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
615 for (i = 0; i < event->n_fields; i++) {
616 fmt = synth_field_fmt(event->fields[i]->type);
617 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
618 event->fields[i]->name, fmt,
619 i == event->n_fields - 1 ? "" : ", ");
621 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
623 for (i = 0; i < event->n_fields; i++) {
624 if (event->fields[i]->is_string &&
625 event->fields[i]->is_dynamic)
626 pos += snprintf(buf + pos, LEN_OR_ZERO,
627 ", __get_str(%s)", event->fields[i]->name);
628 else if (event->fields[i]->is_stack)
629 pos += snprintf(buf + pos, LEN_OR_ZERO,
630 ", __get_stacktrace(%s)", event->fields[i]->name);
632 pos += snprintf(buf + pos, LEN_OR_ZERO,
633 ", REC->%s", event->fields[i]->name);
638 /* return the length of print_fmt */
642 static int set_synth_event_print_fmt(struct trace_event_call *call)
644 struct synth_event *event = call->data;
648 /* First: called with 0 length to calculate the needed length */
649 len = __set_synth_event_print_fmt(event, NULL, 0);
651 print_fmt = kmalloc(len + 1, GFP_KERNEL);
655 /* Second: actually write the @print_fmt */
656 __set_synth_event_print_fmt(event, print_fmt, len + 1);
657 call->print_fmt = print_fmt;
662 static void free_synth_field(struct synth_field *field)
669 static int check_field_version(const char *prefix, const char *field_type,
670 const char *field_name)
673 * For backward compatibility, the old synthetic event command
674 * format did not require semicolons, and in order to not
675 * break user space, that old format must still work. If a new
676 * feature is added, then the format that uses the new feature
677 * will be required to have semicolons, as nothing that uses
678 * the old format would be using the new, yet to be created,
679 * feature. When a new feature is added, this will detect it,
680 * and return a number greater than 1, and require the format
686 static struct synth_field *parse_synth_field(int argc, char **argv,
687 int *consumed, int *field_version)
689 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
690 struct synth_field *field;
691 int len, ret = -ENOMEM;
695 if (!strcmp(field_type, "unsigned")) {
697 synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
698 return ERR_PTR(-EINVAL);
700 prefix = "unsigned ";
701 field_type = argv[1];
702 field_name = argv[2];
705 field_name = argv[1];
710 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type));
711 return ERR_PTR(-EINVAL);
714 *field_version = check_field_version(prefix, field_type, field_name);
716 field = kzalloc(sizeof(*field), GFP_KERNEL);
718 return ERR_PTR(-ENOMEM);
720 len = strlen(field_name);
721 array = strchr(field_name, '[');
723 len -= strlen(array);
725 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
729 if (!is_good_name(field->name)) {
730 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
735 len = strlen(field_type) + 1;
738 len += strlen(array);
741 len += strlen(prefix);
743 field->type = kzalloc(len, GFP_KERNEL);
747 seq_buf_init(&s, field->type, len);
749 seq_buf_puts(&s, prefix);
750 seq_buf_puts(&s, field_type);
752 seq_buf_puts(&s, array);
753 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
756 s.buffer[s.len] = '\0';
758 size = synth_field_size(field->type);
761 synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name));
763 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
766 } else if (size == 0) {
767 if (synth_field_is_string(field->type) ||
768 synth_field_is_stack(field->type)) {
771 len = sizeof("__data_loc ") + strlen(field->type) + 1;
772 type = kzalloc(len, GFP_KERNEL);
776 seq_buf_init(&s, type, len);
777 seq_buf_puts(&s, "__data_loc ");
778 seq_buf_puts(&s, field->type);
780 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
782 s.buffer[s.len] = '\0';
787 field->is_dynamic = true;
790 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
797 if (synth_field_is_string(field->type))
798 field->is_string = true;
799 else if (synth_field_is_stack(field->type))
800 field->is_stack = true;
802 field->is_signed = synth_field_signed(field->type);
806 free_synth_field(field);
807 field = ERR_PTR(ret);
811 static void free_synth_tracepoint(struct tracepoint *tp)
820 static struct tracepoint *alloc_synth_tracepoint(char *name)
822 struct tracepoint *tp;
824 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
826 return ERR_PTR(-ENOMEM);
828 tp->name = kstrdup(name, GFP_KERNEL);
831 return ERR_PTR(-ENOMEM);
837 struct synth_event *find_synth_event(const char *name)
839 struct dyn_event *pos;
840 struct synth_event *event;
842 for_each_dyn_event(pos) {
843 if (!is_synth_event(pos))
845 event = to_synth_event(pos);
846 if (strcmp(event->name, name) == 0)
853 static struct trace_event_fields synth_event_fields_array[] = {
854 { .type = TRACE_FUNCTION_TYPE,
855 .define_fields = synth_event_define_fields },
859 static int register_synth_event(struct synth_event *event)
861 struct trace_event_call *call = &event->call;
864 event->call.class = &event->class;
865 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
866 if (!event->class.system) {
871 event->tp = alloc_synth_tracepoint(event->name);
872 if (IS_ERR(event->tp)) {
873 ret = PTR_ERR(event->tp);
878 INIT_LIST_HEAD(&call->class->fields);
879 call->event.funcs = &synth_event_funcs;
880 call->class->fields_array = synth_event_fields_array;
882 ret = register_trace_event(&call->event);
887 call->flags = TRACE_EVENT_FL_TRACEPOINT;
888 call->class->reg = trace_event_reg;
889 call->class->probe = trace_event_raw_event_synth;
891 call->tp = event->tp;
893 ret = trace_add_event_call(call);
895 pr_warn("Failed to register synthetic event: %s\n",
896 trace_event_name(call));
900 ret = set_synth_event_print_fmt(call);
901 /* unregister_trace_event() will be called inside */
903 trace_remove_event_call(call);
907 unregister_trace_event(&call->event);
911 static int unregister_synth_event(struct synth_event *event)
913 struct trace_event_call *call = &event->call;
916 ret = trace_remove_event_call(call);
921 static void free_synth_event(struct synth_event *event)
928 for (i = 0; i < event->n_fields; i++)
929 free_synth_field(event->fields[i]);
931 kfree(event->fields);
932 kfree(event->dynamic_fields);
934 kfree(event->class.system);
935 free_synth_tracepoint(event->tp);
936 free_synth_event_print_fmt(&event->call);
940 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
941 struct synth_field **fields)
943 unsigned int i, j, n_dynamic_fields = 0;
944 struct synth_event *event;
946 event = kzalloc(sizeof(*event), GFP_KERNEL);
948 event = ERR_PTR(-ENOMEM);
952 event->name = kstrdup(name, GFP_KERNEL);
955 event = ERR_PTR(-ENOMEM);
959 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
960 if (!event->fields) {
961 free_synth_event(event);
962 event = ERR_PTR(-ENOMEM);
966 for (i = 0; i < n_fields; i++)
967 if (fields[i]->is_dynamic)
970 if (n_dynamic_fields) {
971 event->dynamic_fields = kcalloc(n_dynamic_fields,
972 sizeof(*event->dynamic_fields),
974 if (!event->dynamic_fields) {
975 free_synth_event(event);
976 event = ERR_PTR(-ENOMEM);
981 dyn_event_init(&event->devent, &synth_event_ops);
983 for (i = 0, j = 0; i < n_fields; i++) {
984 fields[i]->field_pos = i;
985 event->fields[i] = fields[i];
987 if (fields[i]->is_dynamic)
988 event->dynamic_fields[j++] = fields[i];
990 event->n_dynamic_fields = j;
991 event->n_fields = n_fields;
996 static int synth_event_check_arg_fn(void *data)
998 struct dynevent_arg_pair *arg_pair = data;
1001 size = synth_field_size((char *)arg_pair->lhs);
1003 if (strstr((char *)arg_pair->lhs, "["))
1007 return size ? 0 : -EINVAL;
1011 * synth_event_add_field - Add a new field to a synthetic event cmd
1012 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1013 * @type: The type of the new field to add
1014 * @name: The name of the new field to add
1016 * Add a new field to a synthetic event cmd object. Field ordering is in
1017 * the same order the fields are added.
1019 * See synth_field_size() for available types. If field_name contains
1020 * [n] the field is considered to be an array.
1022 * Return: 0 if successful, error otherwise.
1024 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
1027 struct dynevent_arg_pair arg_pair;
1030 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1036 dynevent_arg_pair_init(&arg_pair, 0, ';');
1038 arg_pair.lhs = type;
1039 arg_pair.rhs = name;
1041 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
1045 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1050 EXPORT_SYMBOL_GPL(synth_event_add_field);
1053 * synth_event_add_field_str - Add a new field to a synthetic event cmd
1054 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1055 * @type_name: The type and name of the new field to add, as a single string
1057 * Add a new field to a synthetic event cmd object, as a single
1058 * string. The @type_name string is expected to be of the form 'type
1059 * name', which will be appended by ';'. No sanity checking is done -
1060 * what's passed in is assumed to already be well-formed. Field
1061 * ordering is in the same order the fields are added.
1063 * See synth_field_size() for available types. If field_name contains
1064 * [n] the field is considered to be an array.
1066 * Return: 0 if successful, error otherwise.
1068 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1070 struct dynevent_arg arg;
1073 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1079 dynevent_arg_init(&arg, ';');
1081 arg.str = type_name;
1083 ret = dynevent_arg_add(cmd, &arg, NULL);
1087 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1092 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1095 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1096 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1097 * @fields: An array of type/name field descriptions
1098 * @n_fields: The number of field descriptions contained in the fields array
1100 * Add a new set of fields to a synthetic event cmd object. The event
1101 * fields that will be defined for the event should be passed in as an
1102 * array of struct synth_field_desc, and the number of elements in the
1103 * array passed in as n_fields. Field ordering will retain the
1104 * ordering given in the fields array.
1106 * See synth_field_size() for available types. If field_name contains
1107 * [n] the field is considered to be an array.
1109 * Return: 0 if successful, error otherwise.
1111 int synth_event_add_fields(struct dynevent_cmd *cmd,
1112 struct synth_field_desc *fields,
1113 unsigned int n_fields)
1118 for (i = 0; i < n_fields; i++) {
1119 if (fields[i].type == NULL || fields[i].name == NULL) {
1124 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1131 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1134 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1135 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1136 * @name: The name of the synthetic event
1137 * @mod: The module creating the event, NULL if not created from a module
1138 * @args: Variable number of arg (pairs), one pair for each field
1140 * NOTE: Users normally won't want to call this function directly, but
1141 * rather use the synth_event_gen_cmd_start() wrapper, which
1142 * automatically adds a NULL to the end of the arg list. If this
1143 * function is used directly, make sure the last arg in the variable
1146 * Generate a synthetic event command to be executed by
1147 * synth_event_gen_cmd_end(). This function can be used to generate
1148 * the complete command or only the first part of it; in the latter
1149 * case, synth_event_add_field(), synth_event_add_field_str(), or
1150 * synth_event_add_fields() can be used to add more fields following
1153 * There should be an even number variable args, each pair consisting
1154 * of a type followed by a field name.
1156 * See synth_field_size() for available types. If field_name contains
1157 * [n] the field is considered to be an array.
1159 * Return: 0 if successful, error otherwise.
1161 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1162 struct module *mod, ...)
1164 struct dynevent_arg arg;
1168 cmd->event_name = name;
1169 cmd->private_data = mod;
1171 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1174 dynevent_arg_init(&arg, 0);
1176 ret = dynevent_arg_add(cmd, &arg, NULL);
1180 va_start(args, mod);
1182 const char *type, *name;
1184 type = va_arg(args, const char *);
1187 name = va_arg(args, const char *);
1191 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1196 ret = synth_event_add_field(cmd, type, name);
1204 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1207 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1208 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1209 * @name: The name of the synthetic event
1210 * @fields: An array of type/name field descriptions
1211 * @n_fields: The number of field descriptions contained in the fields array
1213 * Generate a synthetic event command to be executed by
1214 * synth_event_gen_cmd_end(). This function can be used to generate
1215 * the complete command or only the first part of it; in the latter
1216 * case, synth_event_add_field(), synth_event_add_field_str(), or
1217 * synth_event_add_fields() can be used to add more fields following
1220 * The event fields that will be defined for the event should be
1221 * passed in as an array of struct synth_field_desc, and the number of
1222 * elements in the array passed in as n_fields. Field ordering will
1223 * retain the ordering given in the fields array.
1225 * See synth_field_size() for available types. If field_name contains
1226 * [n] the field is considered to be an array.
1228 * Return: 0 if successful, error otherwise.
1230 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1232 struct synth_field_desc *fields,
1233 unsigned int n_fields)
1235 struct dynevent_arg arg;
1239 cmd->event_name = name;
1240 cmd->private_data = mod;
1242 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1245 if (n_fields > SYNTH_FIELDS_MAX)
1248 dynevent_arg_init(&arg, 0);
1250 ret = dynevent_arg_add(cmd, &arg, NULL);
1254 for (i = 0; i < n_fields; i++) {
1255 if (fields[i].type == NULL || fields[i].name == NULL)
1258 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1265 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1267 static int __create_synth_event(const char *name, const char *raw_fields)
1269 char **argv, *field_str, *tmp_fields, *saved_fields = NULL;
1270 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1271 int consumed, cmd_version = 1, n_fields_this_loop;
1272 int i, argc, n_fields = 0, ret = 0;
1273 struct synth_event *event = NULL;
1277 * - Add synthetic event: <event_name> field[;field] ...
1278 * - Remove synthetic event: !<event_name> field[;field] ...
1279 * where 'field' = type field_name
1282 if (name[0] == '\0') {
1283 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1287 if (!is_good_name(name)) {
1288 synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1292 mutex_lock(&event_mutex);
1294 event = find_synth_event(name);
1296 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1301 tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
1307 while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
1308 argv = argv_split(GFP_KERNEL, field_str, &argc);
1319 n_fields_this_loop = 0;
1321 while (argc > consumed) {
1324 field = parse_synth_field(argc - consumed,
1325 argv + consumed, &consumed,
1327 if (IS_ERR(field)) {
1329 ret = PTR_ERR(field);
1334 * Track the highest version of any field we
1335 * found in the command.
1337 if (field_version > cmd_version)
1338 cmd_version = field_version;
1341 * Now sort out what is and isn't valid for
1342 * each supported version.
1344 * If we see more than 1 field per loop, it
1345 * means we have multiple fields between
1346 * semicolons, and that's something we no
1347 * longer support in a version 2 or greater
1350 if (cmd_version > 1 && n_fields_this_loop >= 1) {
1351 synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str));
1356 if (n_fields == SYNTH_FIELDS_MAX) {
1357 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1361 fields[n_fields++] = field;
1363 n_fields_this_loop++;
1366 if (consumed < argc) {
1367 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1375 if (n_fields == 0) {
1376 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1381 event = alloc_synth_event(name, n_fields, fields);
1382 if (IS_ERR(event)) {
1383 ret = PTR_ERR(event);
1387 ret = register_synth_event(event);
1389 dyn_event_add(&event->devent, &event->call);
1391 free_synth_event(event);
1393 mutex_unlock(&event_mutex);
1395 kfree(saved_fields);
1399 for (i = 0; i < n_fields; i++)
1400 free_synth_field(fields[i]);
1406 * synth_event_create - Create a new synthetic event
1407 * @name: The name of the new synthetic event
1408 * @fields: An array of type/name field descriptions
1409 * @n_fields: The number of field descriptions contained in the fields array
1410 * @mod: The module creating the event, NULL if not created from a module
1412 * Create a new synthetic event with the given name under the
1413 * trace/events/synthetic/ directory. The event fields that will be
1414 * defined for the event should be passed in as an array of struct
1415 * synth_field_desc, and the number elements in the array passed in as
1416 * n_fields. Field ordering will retain the ordering given in the
1419 * If the new synthetic event is being created from a module, the mod
1420 * param must be non-NULL. This will ensure that the trace buffer
1421 * won't contain unreadable events.
1423 * The new synth event should be deleted using synth_event_delete()
1424 * function. The new synthetic event can be generated from modules or
1425 * other kernel code using trace_synth_event() and related functions.
1427 * Return: 0 if successful, error otherwise.
1429 int synth_event_create(const char *name, struct synth_field_desc *fields,
1430 unsigned int n_fields, struct module *mod)
1432 struct dynevent_cmd cmd;
1436 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1440 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1442 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1447 ret = synth_event_gen_cmd_end(&cmd);
1453 EXPORT_SYMBOL_GPL(synth_event_create);
1455 static int destroy_synth_event(struct synth_event *se)
1462 if (trace_event_dyn_busy(&se->call))
1465 ret = unregister_synth_event(se);
1467 dyn_event_remove(&se->devent);
1468 free_synth_event(se);
1475 * synth_event_delete - Delete a synthetic event
1476 * @event_name: The name of the new synthetic event
1478 * Delete a synthetic event that was created with synth_event_create().
1480 * Return: 0 if successful, error otherwise.
1482 int synth_event_delete(const char *event_name)
1484 struct synth_event *se = NULL;
1485 struct module *mod = NULL;
1488 mutex_lock(&event_mutex);
1489 se = find_synth_event(event_name);
1492 ret = destroy_synth_event(se);
1494 mutex_unlock(&event_mutex);
1498 * It is safest to reset the ring buffer if the module
1499 * being unloaded registered any events that were
1500 * used. The only worry is if a new module gets
1501 * loaded, and takes on the same id as the events of
1502 * this module. When printing out the buffer, traced
1503 * events left over from this module may be passed to
1504 * the new module events and unexpected results may
1507 tracing_reset_all_online_cpus();
1512 EXPORT_SYMBOL_GPL(synth_event_delete);
1514 static int check_command(const char *raw_command)
1516 char **argv = NULL, *cmd, *saved_cmd, *name_and_field;
1519 cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL);
1523 name_and_field = strsep(&cmd, ";");
1524 if (!name_and_field) {
1529 if (name_and_field[0] == '!')
1532 argv = argv_split(GFP_KERNEL, name_and_field, &argc);
1547 static int create_or_delete_synth_event(const char *raw_command)
1549 char *name = NULL, *fields, *p;
1552 raw_command = skip_spaces(raw_command);
1553 if (raw_command[0] == '\0')
1556 last_cmd_set(raw_command);
1558 ret = check_command(raw_command);
1560 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1564 p = strpbrk(raw_command, " \t");
1565 if (!p && raw_command[0] != '!') {
1566 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1571 name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL);
1575 if (name[0] == '!') {
1576 ret = synth_event_delete(name + 1);
1580 fields = skip_spaces(p);
1582 ret = __create_synth_event(name, fields);
1589 static int synth_event_run_command(struct dynevent_cmd *cmd)
1591 struct synth_event *se;
1594 ret = create_or_delete_synth_event(cmd->seq.buffer);
1598 se = find_synth_event(cmd->event_name);
1602 se->mod = cmd->private_data;
1608 * synth_event_cmd_init - Initialize a synthetic event command object
1609 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1610 * @buf: A pointer to the buffer used to build the command
1611 * @maxlen: The length of the buffer passed in @buf
1613 * Initialize a synthetic event command object. Use this before
1614 * calling any of the other dyenvent_cmd functions.
1616 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1618 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1619 synth_event_run_command);
1621 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1624 __synth_event_trace_init(struct trace_event_file *file,
1625 struct synth_event_trace_state *trace_state)
1629 memset(trace_state, '\0', sizeof(*trace_state));
1632 * Normal event tracing doesn't get called at all unless the
1633 * ENABLED bit is set (which attaches the probe thus allowing
1634 * this code to be called, etc). Because this is called
1635 * directly by the user, we don't have that but we still need
1636 * to honor not logging when disabled. For the iterated
1637 * trace case, we save the enabled state upon start and just
1638 * ignore the following data calls.
1640 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1641 trace_trigger_soft_disabled(file)) {
1642 trace_state->disabled = true;
1647 trace_state->event = file->event_call->data;
1653 __synth_event_trace_start(struct trace_event_file *file,
1654 struct synth_event_trace_state *trace_state,
1655 int dynamic_fields_size)
1657 int entry_size, fields_size = 0;
1660 fields_size = trace_state->event->n_u64 * sizeof(u64);
1661 fields_size += dynamic_fields_size;
1664 * Avoid ring buffer recursion detection, as this event
1665 * is being performed within another event.
1667 trace_state->buffer = file->tr->array_buffer.buffer;
1668 ring_buffer_nest_start(trace_state->buffer);
1670 entry_size = sizeof(*trace_state->entry) + fields_size;
1671 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1674 if (!trace_state->entry) {
1675 ring_buffer_nest_end(trace_state->buffer);
1683 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1685 trace_event_buffer_commit(&trace_state->fbuffer);
1687 ring_buffer_nest_end(trace_state->buffer);
1691 * synth_event_trace - Trace a synthetic event
1692 * @file: The trace_event_file representing the synthetic event
1693 * @n_vals: The number of values in vals
1694 * @args: Variable number of args containing the event values
1696 * Trace a synthetic event using the values passed in the variable
1699 * The argument list should be a list 'n_vals' u64 values. The number
1700 * of vals must match the number of field in the synthetic event, and
1701 * must be in the same order as the synthetic event fields.
1703 * All vals should be cast to u64, and string vals are just pointers
1704 * to strings, cast to u64. Strings will be copied into space
1705 * reserved in the event for the string, using these pointers.
1707 * Return: 0 on success, err otherwise.
1709 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1711 unsigned int i, n_u64, len, data_size = 0;
1712 struct synth_event_trace_state state;
1716 ret = __synth_event_trace_init(file, &state);
1719 ret = 0; /* just disabled, not really an error */
1723 if (state.event->n_dynamic_fields) {
1724 va_start(args, n_vals);
1726 for (i = 0; i < state.event->n_fields; i++) {
1727 u64 val = va_arg(args, u64);
1729 if (state.event->fields[i]->is_string &&
1730 state.event->fields[i]->is_dynamic) {
1731 char *str_val = (char *)(long)val;
1733 data_size += strlen(str_val) + 1;
1740 ret = __synth_event_trace_start(file, &state, data_size);
1744 if (n_vals != state.event->n_fields) {
1751 va_start(args, n_vals);
1752 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1755 val = va_arg(args, u64);
1757 if (state.event->fields[i]->is_string) {
1758 char *str_val = (char *)(long)val;
1760 len = trace_string(state.entry, state.event, str_val,
1761 state.event->fields[i]->is_dynamic,
1763 data_size += len; /* only dynamic string increments */
1765 struct synth_field *field = state.event->fields[i];
1767 switch (field->size) {
1769 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1773 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1777 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1781 state.entry->fields[n_u64] = val;
1789 __synth_event_trace_end(&state);
1793 EXPORT_SYMBOL_GPL(synth_event_trace);
1796 * synth_event_trace_array - Trace a synthetic event from an array
1797 * @file: The trace_event_file representing the synthetic event
1798 * @vals: Array of values
1799 * @n_vals: The number of values in vals
1801 * Trace a synthetic event using the values passed in as 'vals'.
1803 * The 'vals' array is just an array of 'n_vals' u64. The number of
1804 * vals must match the number of field in the synthetic event, and
1805 * must be in the same order as the synthetic event fields.
1807 * All vals should be cast to u64, and string vals are just pointers
1808 * to strings, cast to u64. Strings will be copied into space
1809 * reserved in the event for the string, using these pointers.
1811 * Return: 0 on success, err otherwise.
1813 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1814 unsigned int n_vals)
1816 unsigned int i, n_u64, field_pos, len, data_size = 0;
1817 struct synth_event_trace_state state;
1821 ret = __synth_event_trace_init(file, &state);
1824 ret = 0; /* just disabled, not really an error */
1828 if (state.event->n_dynamic_fields) {
1829 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1830 field_pos = state.event->dynamic_fields[i]->field_pos;
1831 str_val = (char *)(long)vals[field_pos];
1832 len = strlen(str_val) + 1;
1837 ret = __synth_event_trace_start(file, &state, data_size);
1841 if (n_vals != state.event->n_fields) {
1848 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1849 if (state.event->fields[i]->is_string) {
1850 char *str_val = (char *)(long)vals[i];
1852 len = trace_string(state.entry, state.event, str_val,
1853 state.event->fields[i]->is_dynamic,
1855 data_size += len; /* only dynamic string increments */
1857 struct synth_field *field = state.event->fields[i];
1860 switch (field->size) {
1862 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1866 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1870 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1874 state.entry->fields[n_u64] = val;
1881 __synth_event_trace_end(&state);
1885 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1888 * synth_event_trace_start - Start piecewise synthetic event trace
1889 * @file: The trace_event_file representing the synthetic event
1890 * @trace_state: A pointer to object tracking the piecewise trace state
1892 * Start the trace of a synthetic event field-by-field rather than all
1895 * This function 'opens' an event trace, which means space is reserved
1896 * for the event in the trace buffer, after which the event's
1897 * individual field values can be set through either
1898 * synth_event_add_next_val() or synth_event_add_val().
1900 * A pointer to a trace_state object is passed in, which will keep
1901 * track of the current event trace state until the event trace is
1902 * closed (and the event finally traced) using
1903 * synth_event_trace_end().
1905 * Note that synth_event_trace_end() must be called after all values
1906 * have been added for each event trace, regardless of whether adding
1907 * all field values succeeded or not.
1909 * Note also that for a given event trace, all fields must be added
1910 * using either synth_event_add_next_val() or synth_event_add_val()
1911 * but not both together or interleaved.
1913 * Return: 0 on success, err otherwise.
1915 int synth_event_trace_start(struct trace_event_file *file,
1916 struct synth_event_trace_state *trace_state)
1923 ret = __synth_event_trace_init(file, trace_state);
1926 ret = 0; /* just disabled, not really an error */
1930 if (trace_state->event->n_dynamic_fields)
1933 ret = __synth_event_trace_start(file, trace_state, 0);
1937 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1939 static int __synth_event_add_val(const char *field_name, u64 val,
1940 struct synth_event_trace_state *trace_state)
1942 struct synth_field *field = NULL;
1943 struct synth_trace_event *entry;
1944 struct synth_event *event;
1952 /* can't mix add_next_synth_val() with add_synth_val() */
1954 if (trace_state->add_next) {
1958 trace_state->add_name = true;
1960 if (trace_state->add_name) {
1964 trace_state->add_next = true;
1967 if (trace_state->disabled)
1970 event = trace_state->event;
1971 if (trace_state->add_name) {
1972 for (i = 0; i < event->n_fields; i++) {
1973 field = event->fields[i];
1974 if (strcmp(field->name, field_name) == 0)
1982 if (trace_state->cur_field >= event->n_fields) {
1986 field = event->fields[trace_state->cur_field++];
1989 entry = trace_state->entry;
1990 if (field->is_string) {
1991 char *str_val = (char *)(long)val;
1994 if (field->is_dynamic) { /* add_val can't do dynamic strings */
2004 str_field = (char *)&entry->fields[field->offset];
2005 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2007 switch (field->size) {
2009 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
2013 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
2017 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
2021 trace_state->entry->fields[field->offset] = val;
2030 * synth_event_add_next_val - Add the next field's value to an open synth trace
2031 * @val: The value to set the next field to
2032 * @trace_state: A pointer to object tracking the piecewise trace state
2034 * Set the value of the next field in an event that's been opened by
2035 * synth_event_trace_start().
2037 * The val param should be the value cast to u64. If the value points
2038 * to a string, the val param should be a char * cast to u64.
2040 * This function assumes all the fields in an event are to be set one
2041 * after another - successive calls to this function are made, one for
2042 * each field, in the order of the fields in the event, until all
2043 * fields have been set. If you'd rather set each field individually
2044 * without regard to ordering, synth_event_add_val() can be used
2047 * Note however that synth_event_add_next_val() and
2048 * synth_event_add_val() can't be intermixed for a given event trace -
2049 * one or the other but not both can be used at the same time.
2051 * Note also that synth_event_trace_end() must be called after all
2052 * values have been added for each event trace, regardless of whether
2053 * adding all field values succeeded or not.
2055 * Return: 0 on success, err otherwise.
2057 int synth_event_add_next_val(u64 val,
2058 struct synth_event_trace_state *trace_state)
2060 return __synth_event_add_val(NULL, val, trace_state);
2062 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2065 * synth_event_add_val - Add a named field's value to an open synth trace
2066 * @field_name: The name of the synthetic event field value to set
2067 * @val: The value to set the next field to
2068 * @trace_state: A pointer to object tracking the piecewise trace state
2070 * Set the value of the named field in an event that's been opened by
2071 * synth_event_trace_start().
2073 * The val param should be the value cast to u64. If the value points
2074 * to a string, the val param should be a char * cast to u64.
2076 * This function looks up the field name, and if found, sets the field
2077 * to the specified value. This lookup makes this function more
2078 * expensive than synth_event_add_next_val(), so use that or the
2079 * none-piecewise synth_event_trace() instead if efficiency is more
2082 * Note however that synth_event_add_next_val() and
2083 * synth_event_add_val() can't be intermixed for a given event trace -
2084 * one or the other but not both can be used at the same time.
2086 * Note also that synth_event_trace_end() must be called after all
2087 * values have been added for each event trace, regardless of whether
2088 * adding all field values succeeded or not.
2090 * Return: 0 on success, err otherwise.
2092 int synth_event_add_val(const char *field_name, u64 val,
2093 struct synth_event_trace_state *trace_state)
2095 return __synth_event_add_val(field_name, val, trace_state);
2097 EXPORT_SYMBOL_GPL(synth_event_add_val);
2100 * synth_event_trace_end - End piecewise synthetic event trace
2101 * @trace_state: A pointer to object tracking the piecewise trace state
2103 * End the trace of a synthetic event opened by
2104 * synth_event_trace__start().
2106 * This function 'closes' an event trace, which basically means that
2107 * it commits the reserved event and cleans up other loose ends.
2109 * A pointer to a trace_state object is passed in, which will keep
2110 * track of the current event trace state opened with
2111 * synth_event_trace_start().
2113 * Note that this function must be called after all values have been
2114 * added for each event trace, regardless of whether adding all field
2115 * values succeeded or not.
2117 * Return: 0 on success, err otherwise.
2119 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2124 __synth_event_trace_end(trace_state);
2128 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2130 static int create_synth_event(const char *raw_command)
2136 raw_command = skip_spaces(raw_command);
2137 if (raw_command[0] == '\0')
2140 last_cmd_set(raw_command);
2144 /* Don't try to process if not our system */
2145 if (name[0] != 's' || name[1] != ':')
2149 p = strpbrk(raw_command, " \t");
2151 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2155 fields = skip_spaces(p);
2157 /* This interface accepts group name prefix */
2158 if (strchr(name, '/')) {
2159 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2161 synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0);
2167 len = name - raw_command;
2169 ret = check_command(raw_command + len);
2171 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2175 name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL);
2179 ret = __create_synth_event(name, fields);
2186 static int synth_event_release(struct dyn_event *ev)
2188 struct synth_event *event = to_synth_event(ev);
2194 if (trace_event_dyn_busy(&event->call))
2197 ret = unregister_synth_event(event);
2201 dyn_event_remove(ev);
2202 free_synth_event(event);
2206 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2208 struct synth_field *field;
2212 seq_printf(m, "%s\t", event->name);
2214 for (i = 0; i < event->n_fields; i++) {
2215 field = event->fields[i];
2218 t = strstr(type, "__data_loc");
2219 if (t) { /* __data_loc belongs in format but not event desc */
2220 t += sizeof("__data_loc");
2224 /* parameter values */
2225 seq_printf(m, "%s %s%s", type, field->name,
2226 i == event->n_fields - 1 ? "" : "; ");
2234 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2236 struct synth_event *event = to_synth_event(ev);
2238 seq_printf(m, "s:%s/", event->class.system);
2240 return __synth_event_show(m, event);
2243 static int synth_events_seq_show(struct seq_file *m, void *v)
2245 struct dyn_event *ev = v;
2247 if (!is_synth_event(ev))
2250 return __synth_event_show(m, to_synth_event(ev));
2253 static const struct seq_operations synth_events_seq_op = {
2254 .start = dyn_event_seq_start,
2255 .next = dyn_event_seq_next,
2256 .stop = dyn_event_seq_stop,
2257 .show = synth_events_seq_show,
2260 static int synth_events_open(struct inode *inode, struct file *file)
2264 ret = security_locked_down(LOCKDOWN_TRACEFS);
2268 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2269 ret = dyn_events_release_all(&synth_event_ops);
2274 return seq_open(file, &synth_events_seq_op);
2277 static ssize_t synth_events_write(struct file *file,
2278 const char __user *buffer,
2279 size_t count, loff_t *ppos)
2281 return trace_parse_run_command(file, buffer, count, ppos,
2282 create_or_delete_synth_event);
2285 static const struct file_operations synth_events_fops = {
2286 .open = synth_events_open,
2287 .write = synth_events_write,
2289 .llseek = seq_lseek,
2290 .release = seq_release,
2294 * Register dynevent at core_initcall. This allows kernel to setup kprobe
2295 * events in postcore_initcall without tracefs.
2297 static __init int trace_events_synth_init_early(void)
2301 err = dyn_event_register(&synth_event_ops);
2303 pr_warn("Could not register synth_event_ops\n");
2307 core_initcall(trace_events_synth_init_early);
2309 static __init int trace_events_synth_init(void)
2311 struct dentry *entry = NULL;
2313 err = tracing_init_dentry();
2317 entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE,
2318 NULL, NULL, &synth_events_fops);
2326 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2331 fs_initcall(trace_events_synth_init);