1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_synth - synthetic trace events
5 * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
21 #include "trace_synth.h"
25 C(BAD_NAME, "Illegal name"), \
26 C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\
27 C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\
28 C(EVENT_EXISTS, "Event already exists"), \
29 C(TOO_MANY_FIELDS, "Too many fields"), \
30 C(INCOMPLETE_TYPE, "Incomplete type"), \
31 C(INVALID_TYPE, "Invalid type"), \
32 C(INVALID_FIELD, "Invalid field"), \
33 C(INVALID_ARRAY_SPEC, "Invalid array specification"),
36 #define C(a, b) SYNTH_ERR_##a
43 static const char *err_text
[] = { ERRORS
};
45 static char last_cmd
[MAX_FILTER_STR_VAL
];
47 static int errpos(const char *str
)
49 return err_pos(last_cmd
, str
);
52 static void last_cmd_set(const char *str
)
57 strncpy(last_cmd
, str
, MAX_FILTER_STR_VAL
- 1);
60 static void synth_err(u8 err_type
, u8 err_pos
)
62 tracing_log_err(NULL
, "synthetic_events", last_cmd
, err_text
,
66 static int create_synth_event(const char *raw_command
);
67 static int synth_event_show(struct seq_file
*m
, struct dyn_event
*ev
);
68 static int synth_event_release(struct dyn_event
*ev
);
69 static bool synth_event_is_busy(struct dyn_event
*ev
);
70 static bool synth_event_match(const char *system
, const char *event
,
71 int argc
, const char **argv
, struct dyn_event
*ev
);
73 static struct dyn_event_operations synth_event_ops
= {
74 .create
= create_synth_event
,
75 .show
= synth_event_show
,
76 .is_busy
= synth_event_is_busy
,
77 .free
= synth_event_release
,
78 .match
= synth_event_match
,
81 static bool is_synth_event(struct dyn_event
*ev
)
83 return ev
->ops
== &synth_event_ops
;
86 static struct synth_event
*to_synth_event(struct dyn_event
*ev
)
88 return container_of(ev
, struct synth_event
, devent
);
91 static bool synth_event_is_busy(struct dyn_event
*ev
)
93 struct synth_event
*event
= to_synth_event(ev
);
95 return event
->ref
!= 0;
98 static bool synth_event_match(const char *system
, const char *event
,
99 int argc
, const char **argv
, struct dyn_event
*ev
)
101 struct synth_event
*sev
= to_synth_event(ev
);
103 return strcmp(sev
->name
, event
) == 0 &&
104 (!system
|| strcmp(system
, SYNTH_SYSTEM
) == 0);
107 struct synth_trace_event
{
108 struct trace_entry ent
;
112 static int synth_event_define_fields(struct trace_event_call
*call
)
114 struct synth_trace_event trace
;
115 int offset
= offsetof(typeof(trace
), fields
);
116 struct synth_event
*event
= call
->data
;
117 unsigned int i
, size
, n_u64
;
122 for (i
= 0, n_u64
= 0; i
< event
->n_fields
; i
++) {
123 size
= event
->fields
[i
]->size
;
124 is_signed
= event
->fields
[i
]->is_signed
;
125 type
= event
->fields
[i
]->type
;
126 name
= event
->fields
[i
]->name
;
127 ret
= trace_define_field(call
, type
, name
, offset
, size
,
128 is_signed
, FILTER_OTHER
);
132 event
->fields
[i
]->offset
= n_u64
;
134 if (event
->fields
[i
]->is_string
&& !event
->fields
[i
]->is_dynamic
) {
135 offset
+= STR_VAR_LEN_MAX
;
136 n_u64
+= STR_VAR_LEN_MAX
/ sizeof(u64
);
138 offset
+= sizeof(u64
);
143 event
->n_u64
= n_u64
;
148 static bool synth_field_signed(char *type
)
150 if (str_has_prefix(type
, "u"))
152 if (strcmp(type
, "gfp_t") == 0)
158 static int synth_field_is_string(char *type
)
160 if (strstr(type
, "char[") != NULL
)
166 static int synth_field_string_size(char *type
)
168 char buf
[4], *end
, *start
;
172 start
= strstr(type
, "char[");
175 start
+= sizeof("char[") - 1;
177 end
= strchr(type
, ']');
178 if (!end
|| end
< start
|| type
+ strlen(type
) > end
+ 1)
186 return 0; /* variable-length string */
188 strncpy(buf
, start
, len
);
191 err
= kstrtouint(buf
, 0, &size
);
195 if (size
> STR_VAR_LEN_MAX
)
201 static int synth_field_size(char *type
)
205 if (strcmp(type
, "s64") == 0)
207 else if (strcmp(type
, "u64") == 0)
209 else if (strcmp(type
, "s32") == 0)
211 else if (strcmp(type
, "u32") == 0)
213 else if (strcmp(type
, "s16") == 0)
215 else if (strcmp(type
, "u16") == 0)
217 else if (strcmp(type
, "s8") == 0)
219 else if (strcmp(type
, "u8") == 0)
221 else if (strcmp(type
, "char") == 0)
223 else if (strcmp(type
, "unsigned char") == 0)
224 size
= sizeof(unsigned char);
225 else if (strcmp(type
, "int") == 0)
227 else if (strcmp(type
, "unsigned int") == 0)
228 size
= sizeof(unsigned int);
229 else if (strcmp(type
, "long") == 0)
231 else if (strcmp(type
, "unsigned long") == 0)
232 size
= sizeof(unsigned long);
233 else if (strcmp(type
, "bool") == 0)
235 else if (strcmp(type
, "pid_t") == 0)
236 size
= sizeof(pid_t
);
237 else if (strcmp(type
, "gfp_t") == 0)
238 size
= sizeof(gfp_t
);
239 else if (synth_field_is_string(type
))
240 size
= synth_field_string_size(type
);
245 static const char *synth_field_fmt(char *type
)
247 const char *fmt
= "%llu";
249 if (strcmp(type
, "s64") == 0)
251 else if (strcmp(type
, "u64") == 0)
253 else if (strcmp(type
, "s32") == 0)
255 else if (strcmp(type
, "u32") == 0)
257 else if (strcmp(type
, "s16") == 0)
259 else if (strcmp(type
, "u16") == 0)
261 else if (strcmp(type
, "s8") == 0)
263 else if (strcmp(type
, "u8") == 0)
265 else if (strcmp(type
, "char") == 0)
267 else if (strcmp(type
, "unsigned char") == 0)
269 else if (strcmp(type
, "int") == 0)
271 else if (strcmp(type
, "unsigned int") == 0)
273 else if (strcmp(type
, "long") == 0)
275 else if (strcmp(type
, "unsigned long") == 0)
277 else if (strcmp(type
, "bool") == 0)
279 else if (strcmp(type
, "pid_t") == 0)
281 else if (strcmp(type
, "gfp_t") == 0)
283 else if (synth_field_is_string(type
))
289 static void print_synth_event_num_val(struct trace_seq
*s
,
290 char *print_fmt
, char *name
,
291 int size
, u64 val
, char *space
)
295 trace_seq_printf(s
, print_fmt
, name
, (u8
)val
, space
);
299 trace_seq_printf(s
, print_fmt
, name
, (u16
)val
, space
);
303 trace_seq_printf(s
, print_fmt
, name
, (u32
)val
, space
);
307 trace_seq_printf(s
, print_fmt
, name
, val
, space
);
312 static enum print_line_t
print_synth_event(struct trace_iterator
*iter
,
314 struct trace_event
*event
)
316 struct trace_array
*tr
= iter
->tr
;
317 struct trace_seq
*s
= &iter
->seq
;
318 struct synth_trace_event
*entry
;
319 struct synth_event
*se
;
320 unsigned int i
, n_u64
;
324 entry
= (struct synth_trace_event
*)iter
->ent
;
325 se
= container_of(event
, struct synth_event
, call
.event
);
327 trace_seq_printf(s
, "%s: ", se
->name
);
329 for (i
= 0, n_u64
= 0; i
< se
->n_fields
; i
++) {
330 if (trace_seq_has_overflowed(s
))
333 fmt
= synth_field_fmt(se
->fields
[i
]->type
);
335 /* parameter types */
336 if (tr
&& tr
->trace_flags
& TRACE_ITER_VERBOSE
)
337 trace_seq_printf(s
, "%s ", fmt
);
339 snprintf(print_fmt
, sizeof(print_fmt
), "%%s=%s%%s", fmt
);
341 /* parameter values */
342 if (se
->fields
[i
]->is_string
) {
343 if (se
->fields
[i
]->is_dynamic
) {
344 u32 offset
, data_offset
;
347 offset
= (u32
)entry
->fields
[n_u64
];
348 data_offset
= offset
& 0xffff;
350 str_field
= (char *)entry
+ data_offset
;
352 trace_seq_printf(s
, print_fmt
, se
->fields
[i
]->name
,
355 i
== se
->n_fields
- 1 ? "" : " ");
358 trace_seq_printf(s
, print_fmt
, se
->fields
[i
]->name
,
360 (char *)&entry
->fields
[n_u64
],
361 i
== se
->n_fields
- 1 ? "" : " ");
362 n_u64
+= STR_VAR_LEN_MAX
/ sizeof(u64
);
365 struct trace_print_flags __flags
[] = {
366 __def_gfpflag_names
, {-1, NULL
} };
367 char *space
= (i
== se
->n_fields
- 1 ? "" : " ");
369 print_synth_event_num_val(s
, print_fmt
,
372 entry
->fields
[n_u64
],
375 if (strcmp(se
->fields
[i
]->type
, "gfp_t") == 0) {
376 trace_seq_puts(s
, " (");
377 trace_print_flags_seq(s
, "|",
378 entry
->fields
[n_u64
],
380 trace_seq_putc(s
, ')');
386 trace_seq_putc(s
, '\n');
388 return trace_handle_return(s
);
391 static struct trace_event_functions synth_event_funcs
= {
392 .trace
= print_synth_event
395 static unsigned int trace_string(struct synth_trace_event
*entry
,
396 struct synth_event
*event
,
399 unsigned int data_size
,
402 unsigned int len
= 0;
408 data_offset
= offsetof(typeof(*entry
), fields
);
409 data_offset
+= event
->n_u64
* sizeof(u64
);
410 data_offset
+= data_size
;
412 str_field
= (char *)entry
+ data_offset
;
414 len
= strlen(str_val
) + 1;
415 strscpy(str_field
, str_val
, len
);
417 data_offset
|= len
<< 16;
418 *(u32
*)&entry
->fields
[*n_u64
] = data_offset
;
422 str_field
= (char *)&entry
->fields
[*n_u64
];
424 strscpy(str_field
, str_val
, STR_VAR_LEN_MAX
);
425 (*n_u64
) += STR_VAR_LEN_MAX
/ sizeof(u64
);
431 static notrace
void trace_event_raw_event_synth(void *__data
,
433 unsigned int *var_ref_idx
)
435 unsigned int i
, n_u64
, val_idx
, len
, data_size
= 0;
436 struct trace_event_file
*trace_file
= __data
;
437 struct synth_trace_event
*entry
;
438 struct trace_event_buffer fbuffer
;
439 struct trace_buffer
*buffer
;
440 struct synth_event
*event
;
443 event
= trace_file
->event_call
->data
;
445 if (trace_trigger_soft_disabled(trace_file
))
448 fields_size
= event
->n_u64
* sizeof(u64
);
450 for (i
= 0; i
< event
->n_dynamic_fields
; i
++) {
451 unsigned int field_pos
= event
->dynamic_fields
[i
]->field_pos
;
454 val_idx
= var_ref_idx
[field_pos
];
455 str_val
= (char *)(long)var_ref_vals
[val_idx
];
457 len
= strlen(str_val
) + 1;
463 * Avoid ring buffer recursion detection, as this event
464 * is being performed within another event.
466 buffer
= trace_file
->tr
->array_buffer
.buffer
;
467 ring_buffer_nest_start(buffer
);
469 entry
= trace_event_buffer_reserve(&fbuffer
, trace_file
,
470 sizeof(*entry
) + fields_size
);
474 for (i
= 0, n_u64
= 0; i
< event
->n_fields
; i
++) {
475 val_idx
= var_ref_idx
[i
];
476 if (event
->fields
[i
]->is_string
) {
477 char *str_val
= (char *)(long)var_ref_vals
[val_idx
];
479 len
= trace_string(entry
, event
, str_val
,
480 event
->fields
[i
]->is_dynamic
,
482 data_size
+= len
; /* only dynamic string increments */
484 struct synth_field
*field
= event
->fields
[i
];
485 u64 val
= var_ref_vals
[val_idx
];
487 switch (field
->size
) {
489 *(u8
*)&entry
->fields
[n_u64
] = (u8
)val
;
493 *(u16
*)&entry
->fields
[n_u64
] = (u16
)val
;
497 *(u32
*)&entry
->fields
[n_u64
] = (u32
)val
;
501 entry
->fields
[n_u64
] = val
;
508 trace_event_buffer_commit(&fbuffer
);
510 ring_buffer_nest_end(buffer
);
513 static void free_synth_event_print_fmt(struct trace_event_call
*call
)
516 kfree(call
->print_fmt
);
517 call
->print_fmt
= NULL
;
521 static int __set_synth_event_print_fmt(struct synth_event
*event
,
528 /* When len=0, we just calculate the needed length */
529 #define LEN_OR_ZERO (len ? len - pos : 0)
531 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"");
532 for (i
= 0; i
< event
->n_fields
; i
++) {
533 fmt
= synth_field_fmt(event
->fields
[i
]->type
);
534 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "%s=%s%s",
535 event
->fields
[i
]->name
, fmt
,
536 i
== event
->n_fields
- 1 ? "" : ", ");
538 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"");
540 for (i
= 0; i
< event
->n_fields
; i
++) {
541 if (event
->fields
[i
]->is_string
&&
542 event
->fields
[i
]->is_dynamic
)
543 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
544 ", __get_str(%s)", event
->fields
[i
]->name
);
546 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
547 ", REC->%s", event
->fields
[i
]->name
);
552 /* return the length of print_fmt */
556 static int set_synth_event_print_fmt(struct trace_event_call
*call
)
558 struct synth_event
*event
= call
->data
;
562 /* First: called with 0 length to calculate the needed length */
563 len
= __set_synth_event_print_fmt(event
, NULL
, 0);
565 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
569 /* Second: actually write the @print_fmt */
570 __set_synth_event_print_fmt(event
, print_fmt
, len
+ 1);
571 call
->print_fmt
= print_fmt
;
576 static void free_synth_field(struct synth_field
*field
)
583 static int check_field_version(const char *prefix
, const char *field_type
,
584 const char *field_name
)
587 * For backward compatibility, the old synthetic event command
588 * format did not require semicolons, and in order to not
589 * break user space, that old format must still work. If a new
590 * feature is added, then the format that uses the new feature
591 * will be required to have semicolons, as nothing that uses
592 * the old format would be using the new, yet to be created,
593 * feature. When a new feature is added, this will detect it,
594 * and return a number greater than 1, and require the format
600 static struct synth_field
*parse_synth_field(int argc
, char **argv
,
601 int *consumed
, int *field_version
)
603 const char *prefix
= NULL
, *field_type
= argv
[0], *field_name
, *array
;
604 struct synth_field
*field
;
605 int len
, ret
= -ENOMEM
;
609 if (!strcmp(field_type
, "unsigned")) {
611 synth_err(SYNTH_ERR_INCOMPLETE_TYPE
, errpos(field_type
));
612 return ERR_PTR(-EINVAL
);
614 prefix
= "unsigned ";
615 field_type
= argv
[1];
616 field_name
= argv
[2];
619 field_name
= argv
[1];
624 synth_err(SYNTH_ERR_INVALID_FIELD
, errpos(field_type
));
625 return ERR_PTR(-EINVAL
);
628 *field_version
= check_field_version(prefix
, field_type
, field_name
);
630 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
632 return ERR_PTR(-ENOMEM
);
634 len
= strlen(field_name
);
635 array
= strchr(field_name
, '[');
637 len
-= strlen(array
);
639 field
->name
= kmemdup_nul(field_name
, len
, GFP_KERNEL
);
643 if (!is_good_name(field
->name
)) {
644 synth_err(SYNTH_ERR_BAD_NAME
, errpos(field_name
));
649 len
= strlen(field_type
) + 1;
652 len
+= strlen(array
);
655 len
+= strlen(prefix
);
657 field
->type
= kzalloc(len
, GFP_KERNEL
);
661 seq_buf_init(&s
, field
->type
, len
);
663 seq_buf_puts(&s
, prefix
);
664 seq_buf_puts(&s
, field_type
);
666 seq_buf_puts(&s
, array
);
667 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s
)))
670 s
.buffer
[s
.len
] = '\0';
672 size
= synth_field_size(field
->type
);
675 synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC
, errpos(field_name
));
677 synth_err(SYNTH_ERR_INVALID_TYPE
, errpos(field_type
));
680 } else if (size
== 0) {
681 if (synth_field_is_string(field
->type
)) {
684 len
= sizeof("__data_loc ") + strlen(field
->type
) + 1;
685 type
= kzalloc(len
, GFP_KERNEL
);
689 seq_buf_init(&s
, type
, len
);
690 seq_buf_puts(&s
, "__data_loc ");
691 seq_buf_puts(&s
, field
->type
);
693 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s
)))
695 s
.buffer
[s
.len
] = '\0';
700 field
->is_dynamic
= true;
703 synth_err(SYNTH_ERR_INVALID_TYPE
, errpos(field_type
));
710 if (synth_field_is_string(field
->type
))
711 field
->is_string
= true;
713 field
->is_signed
= synth_field_signed(field
->type
);
717 free_synth_field(field
);
718 field
= ERR_PTR(ret
);
722 static void free_synth_tracepoint(struct tracepoint
*tp
)
731 static struct tracepoint
*alloc_synth_tracepoint(char *name
)
733 struct tracepoint
*tp
;
735 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
737 return ERR_PTR(-ENOMEM
);
739 tp
->name
= kstrdup(name
, GFP_KERNEL
);
742 return ERR_PTR(-ENOMEM
);
748 struct synth_event
*find_synth_event(const char *name
)
750 struct dyn_event
*pos
;
751 struct synth_event
*event
;
753 for_each_dyn_event(pos
) {
754 if (!is_synth_event(pos
))
756 event
= to_synth_event(pos
);
757 if (strcmp(event
->name
, name
) == 0)
764 static struct trace_event_fields synth_event_fields_array
[] = {
765 { .type
= TRACE_FUNCTION_TYPE
,
766 .define_fields
= synth_event_define_fields
},
770 static int register_synth_event(struct synth_event
*event
)
772 struct trace_event_call
*call
= &event
->call
;
775 event
->call
.class = &event
->class;
776 event
->class.system
= kstrdup(SYNTH_SYSTEM
, GFP_KERNEL
);
777 if (!event
->class.system
) {
782 event
->tp
= alloc_synth_tracepoint(event
->name
);
783 if (IS_ERR(event
->tp
)) {
784 ret
= PTR_ERR(event
->tp
);
789 INIT_LIST_HEAD(&call
->class->fields
);
790 call
->event
.funcs
= &synth_event_funcs
;
791 call
->class->fields_array
= synth_event_fields_array
;
793 ret
= register_trace_event(&call
->event
);
798 call
->flags
= TRACE_EVENT_FL_TRACEPOINT
;
799 call
->class->reg
= trace_event_reg
;
800 call
->class->probe
= trace_event_raw_event_synth
;
802 call
->tp
= event
->tp
;
804 ret
= trace_add_event_call(call
);
806 pr_warn("Failed to register synthetic event: %s\n",
807 trace_event_name(call
));
811 ret
= set_synth_event_print_fmt(call
);
813 trace_remove_event_call(call
);
819 unregister_trace_event(&call
->event
);
823 static int unregister_synth_event(struct synth_event
*event
)
825 struct trace_event_call
*call
= &event
->call
;
828 ret
= trace_remove_event_call(call
);
833 static void free_synth_event(struct synth_event
*event
)
840 for (i
= 0; i
< event
->n_fields
; i
++)
841 free_synth_field(event
->fields
[i
]);
843 kfree(event
->fields
);
844 kfree(event
->dynamic_fields
);
846 kfree(event
->class.system
);
847 free_synth_tracepoint(event
->tp
);
848 free_synth_event_print_fmt(&event
->call
);
852 static struct synth_event
*alloc_synth_event(const char *name
, int n_fields
,
853 struct synth_field
**fields
)
855 unsigned int i
, j
, n_dynamic_fields
= 0;
856 struct synth_event
*event
;
858 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
860 event
= ERR_PTR(-ENOMEM
);
864 event
->name
= kstrdup(name
, GFP_KERNEL
);
867 event
= ERR_PTR(-ENOMEM
);
871 event
->fields
= kcalloc(n_fields
, sizeof(*event
->fields
), GFP_KERNEL
);
872 if (!event
->fields
) {
873 free_synth_event(event
);
874 event
= ERR_PTR(-ENOMEM
);
878 for (i
= 0; i
< n_fields
; i
++)
879 if (fields
[i
]->is_dynamic
)
882 if (n_dynamic_fields
) {
883 event
->dynamic_fields
= kcalloc(n_dynamic_fields
,
884 sizeof(*event
->dynamic_fields
),
886 if (!event
->dynamic_fields
) {
887 free_synth_event(event
);
888 event
= ERR_PTR(-ENOMEM
);
893 dyn_event_init(&event
->devent
, &synth_event_ops
);
895 for (i
= 0, j
= 0; i
< n_fields
; i
++) {
896 fields
[i
]->field_pos
= i
;
897 event
->fields
[i
] = fields
[i
];
899 if (fields
[i
]->is_dynamic
)
900 event
->dynamic_fields
[j
++] = fields
[i
];
902 event
->n_dynamic_fields
= j
;
903 event
->n_fields
= n_fields
;
908 static int synth_event_check_arg_fn(void *data
)
910 struct dynevent_arg_pair
*arg_pair
= data
;
913 size
= synth_field_size((char *)arg_pair
->lhs
);
915 if (strstr((char *)arg_pair
->lhs
, "["))
919 return size
? 0 : -EINVAL
;
923 * synth_event_add_field - Add a new field to a synthetic event cmd
924 * @cmd: A pointer to the dynevent_cmd struct representing the new event
925 * @type: The type of the new field to add
926 * @name: The name of the new field to add
928 * Add a new field to a synthetic event cmd object. Field ordering is in
929 * the same order the fields are added.
931 * See synth_field_size() for available types. If field_name contains
932 * [n] the field is considered to be an array.
934 * Return: 0 if successful, error otherwise.
936 int synth_event_add_field(struct dynevent_cmd
*cmd
, const char *type
,
939 struct dynevent_arg_pair arg_pair
;
942 if (cmd
->type
!= DYNEVENT_TYPE_SYNTH
)
948 dynevent_arg_pair_init(&arg_pair
, 0, ';');
953 ret
= dynevent_arg_pair_add(cmd
, &arg_pair
, synth_event_check_arg_fn
);
957 if (++cmd
->n_fields
> SYNTH_FIELDS_MAX
)
962 EXPORT_SYMBOL_GPL(synth_event_add_field
);
965 * synth_event_add_field_str - Add a new field to a synthetic event cmd
966 * @cmd: A pointer to the dynevent_cmd struct representing the new event
967 * @type_name: The type and name of the new field to add, as a single string
969 * Add a new field to a synthetic event cmd object, as a single
970 * string. The @type_name string is expected to be of the form 'type
971 * name', which will be appended by ';'. No sanity checking is done -
972 * what's passed in is assumed to already be well-formed. Field
973 * ordering is in the same order the fields are added.
975 * See synth_field_size() for available types. If field_name contains
976 * [n] the field is considered to be an array.
978 * Return: 0 if successful, error otherwise.
980 int synth_event_add_field_str(struct dynevent_cmd
*cmd
, const char *type_name
)
982 struct dynevent_arg arg
;
985 if (cmd
->type
!= DYNEVENT_TYPE_SYNTH
)
991 dynevent_arg_init(&arg
, ';');
995 ret
= dynevent_arg_add(cmd
, &arg
, NULL
);
999 if (++cmd
->n_fields
> SYNTH_FIELDS_MAX
)
1004 EXPORT_SYMBOL_GPL(synth_event_add_field_str
);
1007 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1008 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1009 * @fields: An array of type/name field descriptions
1010 * @n_fields: The number of field descriptions contained in the fields array
1012 * Add a new set of fields to a synthetic event cmd object. The event
1013 * fields that will be defined for the event should be passed in as an
1014 * array of struct synth_field_desc, and the number of elements in the
1015 * array passed in as n_fields. Field ordering will retain the
1016 * ordering given in the fields array.
1018 * See synth_field_size() for available types. If field_name contains
1019 * [n] the field is considered to be an array.
1021 * Return: 0 if successful, error otherwise.
1023 int synth_event_add_fields(struct dynevent_cmd
*cmd
,
1024 struct synth_field_desc
*fields
,
1025 unsigned int n_fields
)
1030 for (i
= 0; i
< n_fields
; i
++) {
1031 if (fields
[i
].type
== NULL
|| fields
[i
].name
== NULL
) {
1036 ret
= synth_event_add_field(cmd
, fields
[i
].type
, fields
[i
].name
);
1043 EXPORT_SYMBOL_GPL(synth_event_add_fields
);
1046 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1047 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1048 * @name: The name of the synthetic event
1049 * @mod: The module creating the event, NULL if not created from a module
1050 * @args: Variable number of arg (pairs), one pair for each field
1052 * NOTE: Users normally won't want to call this function directly, but
1053 * rather use the synth_event_gen_cmd_start() wrapper, which
1054 * automatically adds a NULL to the end of the arg list. If this
1055 * function is used directly, make sure the last arg in the variable
1058 * Generate a synthetic event command to be executed by
1059 * synth_event_gen_cmd_end(). This function can be used to generate
1060 * the complete command or only the first part of it; in the latter
1061 * case, synth_event_add_field(), synth_event_add_field_str(), or
1062 * synth_event_add_fields() can be used to add more fields following
1065 * There should be an even number variable args, each pair consisting
1066 * of a type followed by a field name.
1068 * See synth_field_size() for available types. If field_name contains
1069 * [n] the field is considered to be an array.
1071 * Return: 0 if successful, error otherwise.
1073 int __synth_event_gen_cmd_start(struct dynevent_cmd
*cmd
, const char *name
,
1074 struct module
*mod
, ...)
1076 struct dynevent_arg arg
;
1080 cmd
->event_name
= name
;
1081 cmd
->private_data
= mod
;
1083 if (cmd
->type
!= DYNEVENT_TYPE_SYNTH
)
1086 dynevent_arg_init(&arg
, 0);
1088 ret
= dynevent_arg_add(cmd
, &arg
, NULL
);
1092 va_start(args
, mod
);
1094 const char *type
, *name
;
1096 type
= va_arg(args
, const char *);
1099 name
= va_arg(args
, const char *);
1103 if (++cmd
->n_fields
> SYNTH_FIELDS_MAX
) {
1108 ret
= synth_event_add_field(cmd
, type
, name
);
1116 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start
);
1119 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1120 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1121 * @name: The name of the synthetic event
1122 * @fields: An array of type/name field descriptions
1123 * @n_fields: The number of field descriptions contained in the fields array
1125 * Generate a synthetic event command to be executed by
1126 * synth_event_gen_cmd_end(). This function can be used to generate
1127 * the complete command or only the first part of it; in the latter
1128 * case, synth_event_add_field(), synth_event_add_field_str(), or
1129 * synth_event_add_fields() can be used to add more fields following
1132 * The event fields that will be defined for the event should be
1133 * passed in as an array of struct synth_field_desc, and the number of
1134 * elements in the array passed in as n_fields. Field ordering will
1135 * retain the ordering given in the fields array.
1137 * See synth_field_size() for available types. If field_name contains
1138 * [n] the field is considered to be an array.
1140 * Return: 0 if successful, error otherwise.
1142 int synth_event_gen_cmd_array_start(struct dynevent_cmd
*cmd
, const char *name
,
1144 struct synth_field_desc
*fields
,
1145 unsigned int n_fields
)
1147 struct dynevent_arg arg
;
1151 cmd
->event_name
= name
;
1152 cmd
->private_data
= mod
;
1154 if (cmd
->type
!= DYNEVENT_TYPE_SYNTH
)
1157 if (n_fields
> SYNTH_FIELDS_MAX
)
1160 dynevent_arg_init(&arg
, 0);
1162 ret
= dynevent_arg_add(cmd
, &arg
, NULL
);
1166 for (i
= 0; i
< n_fields
; i
++) {
1167 if (fields
[i
].type
== NULL
|| fields
[i
].name
== NULL
)
1170 ret
= synth_event_add_field(cmd
, fields
[i
].type
, fields
[i
].name
);
1177 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start
);
1179 static int __create_synth_event(const char *name
, const char *raw_fields
)
1181 char **argv
, *field_str
, *tmp_fields
, *saved_fields
= NULL
;
1182 struct synth_field
*field
, *fields
[SYNTH_FIELDS_MAX
];
1183 int consumed
, cmd_version
= 1, n_fields_this_loop
;
1184 int i
, argc
, n_fields
= 0, ret
= 0;
1185 struct synth_event
*event
= NULL
;
1189 * - Add synthetic event: <event_name> field[;field] ...
1190 * - Remove synthetic event: !<event_name> field[;field] ...
1191 * where 'field' = type field_name
1194 if (name
[0] == '\0') {
1195 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
1199 if (!is_good_name(name
)) {
1200 synth_err(SYNTH_ERR_BAD_NAME
, errpos(name
));
1204 mutex_lock(&event_mutex
);
1206 event
= find_synth_event(name
);
1208 synth_err(SYNTH_ERR_EVENT_EXISTS
, errpos(name
));
1213 tmp_fields
= saved_fields
= kstrdup(raw_fields
, GFP_KERNEL
);
1219 while ((field_str
= strsep(&tmp_fields
, ";")) != NULL
) {
1220 argv
= argv_split(GFP_KERNEL
, field_str
, &argc
);
1231 n_fields_this_loop
= 0;
1233 while (argc
> consumed
) {
1236 field
= parse_synth_field(argc
- consumed
,
1237 argv
+ consumed
, &consumed
,
1239 if (IS_ERR(field
)) {
1241 ret
= PTR_ERR(field
);
1246 * Track the highest version of any field we
1247 * found in the command.
1249 if (field_version
> cmd_version
)
1250 cmd_version
= field_version
;
1253 * Now sort out what is and isn't valid for
1254 * each supported version.
1256 * If we see more than 1 field per loop, it
1257 * means we have multiple fields between
1258 * semicolons, and that's something we no
1259 * longer support in a version 2 or greater
1262 if (cmd_version
> 1 && n_fields_this_loop
>= 1) {
1263 synth_err(SYNTH_ERR_INVALID_CMD
, errpos(field_str
));
1268 fields
[n_fields
++] = field
;
1269 if (n_fields
== SYNTH_FIELDS_MAX
) {
1270 synth_err(SYNTH_ERR_TOO_MANY_FIELDS
, 0);
1275 n_fields_this_loop
++;
1278 if (consumed
< argc
) {
1279 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
1287 if (n_fields
== 0) {
1288 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
1293 event
= alloc_synth_event(name
, n_fields
, fields
);
1294 if (IS_ERR(event
)) {
1295 ret
= PTR_ERR(event
);
1299 ret
= register_synth_event(event
);
1301 dyn_event_add(&event
->devent
);
1303 free_synth_event(event
);
1305 mutex_unlock(&event_mutex
);
1307 kfree(saved_fields
);
1311 for (i
= 0; i
< n_fields
; i
++)
1312 free_synth_field(fields
[i
]);
1318 * synth_event_create - Create a new synthetic event
1319 * @name: The name of the new synthetic event
1320 * @fields: An array of type/name field descriptions
1321 * @n_fields: The number of field descriptions contained in the fields array
1322 * @mod: The module creating the event, NULL if not created from a module
1324 * Create a new synthetic event with the given name under the
1325 * trace/events/synthetic/ directory. The event fields that will be
1326 * defined for the event should be passed in as an array of struct
1327 * synth_field_desc, and the number elements in the array passed in as
1328 * n_fields. Field ordering will retain the ordering given in the
1331 * If the new synthetic event is being created from a module, the mod
1332 * param must be non-NULL. This will ensure that the trace buffer
1333 * won't contain unreadable events.
1335 * The new synth event should be deleted using synth_event_delete()
1336 * function. The new synthetic event can be generated from modules or
1337 * other kernel code using trace_synth_event() and related functions.
1339 * Return: 0 if successful, error otherwise.
1341 int synth_event_create(const char *name
, struct synth_field_desc
*fields
,
1342 unsigned int n_fields
, struct module
*mod
)
1344 struct dynevent_cmd cmd
;
1348 buf
= kzalloc(MAX_DYNEVENT_CMD_LEN
, GFP_KERNEL
);
1352 synth_event_cmd_init(&cmd
, buf
, MAX_DYNEVENT_CMD_LEN
);
1354 ret
= synth_event_gen_cmd_array_start(&cmd
, name
, mod
,
1359 ret
= synth_event_gen_cmd_end(&cmd
);
1365 EXPORT_SYMBOL_GPL(synth_event_create
);
1367 static int destroy_synth_event(struct synth_event
*se
)
1374 ret
= unregister_synth_event(se
);
1376 dyn_event_remove(&se
->devent
);
1377 free_synth_event(se
);
1385 * synth_event_delete - Delete a synthetic event
1386 * @event_name: The name of the new synthetic event
1388 * Delete a synthetic event that was created with synth_event_create().
1390 * Return: 0 if successful, error otherwise.
1392 int synth_event_delete(const char *event_name
)
1394 struct synth_event
*se
= NULL
;
1395 struct module
*mod
= NULL
;
1398 mutex_lock(&event_mutex
);
1399 se
= find_synth_event(event_name
);
1402 ret
= destroy_synth_event(se
);
1404 mutex_unlock(&event_mutex
);
1407 mutex_lock(&trace_types_lock
);
1409 * It is safest to reset the ring buffer if the module
1410 * being unloaded registered any events that were
1411 * used. The only worry is if a new module gets
1412 * loaded, and takes on the same id as the events of
1413 * this module. When printing out the buffer, traced
1414 * events left over from this module may be passed to
1415 * the new module events and unexpected results may
1418 tracing_reset_all_online_cpus();
1419 mutex_unlock(&trace_types_lock
);
1424 EXPORT_SYMBOL_GPL(synth_event_delete
);
1426 static int check_command(const char *raw_command
)
1428 char **argv
= NULL
, *cmd
, *saved_cmd
, *name_and_field
;
1431 cmd
= saved_cmd
= kstrdup(raw_command
, GFP_KERNEL
);
1435 name_and_field
= strsep(&cmd
, ";");
1436 if (!name_and_field
) {
1441 if (name_and_field
[0] == '!')
1444 argv
= argv_split(GFP_KERNEL
, name_and_field
, &argc
);
1459 static int create_or_delete_synth_event(const char *raw_command
)
1461 char *name
= NULL
, *fields
, *p
;
1464 raw_command
= skip_spaces(raw_command
);
1465 if (raw_command
[0] == '\0')
1468 last_cmd_set(raw_command
);
1470 ret
= check_command(raw_command
);
1472 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
1476 p
= strpbrk(raw_command
, " \t");
1477 if (!p
&& raw_command
[0] != '!') {
1478 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
1483 name
= kmemdup_nul(raw_command
, p
? p
- raw_command
: strlen(raw_command
), GFP_KERNEL
);
1487 if (name
[0] == '!') {
1488 ret
= synth_event_delete(name
+ 1);
1492 fields
= skip_spaces(p
);
1494 ret
= __create_synth_event(name
, fields
);
1501 static int synth_event_run_command(struct dynevent_cmd
*cmd
)
1503 struct synth_event
*se
;
1506 ret
= create_or_delete_synth_event(cmd
->seq
.buffer
);
1510 se
= find_synth_event(cmd
->event_name
);
1514 se
->mod
= cmd
->private_data
;
1520 * synth_event_cmd_init - Initialize a synthetic event command object
1521 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1522 * @buf: A pointer to the buffer used to build the command
1523 * @maxlen: The length of the buffer passed in @buf
1525 * Initialize a synthetic event command object. Use this before
1526 * calling any of the other dyenvent_cmd functions.
1528 void synth_event_cmd_init(struct dynevent_cmd
*cmd
, char *buf
, int maxlen
)
1530 dynevent_cmd_init(cmd
, buf
, maxlen
, DYNEVENT_TYPE_SYNTH
,
1531 synth_event_run_command
);
1533 EXPORT_SYMBOL_GPL(synth_event_cmd_init
);
1536 __synth_event_trace_init(struct trace_event_file
*file
,
1537 struct synth_event_trace_state
*trace_state
)
1541 memset(trace_state
, '\0', sizeof(*trace_state
));
1544 * Normal event tracing doesn't get called at all unless the
1545 * ENABLED bit is set (which attaches the probe thus allowing
1546 * this code to be called, etc). Because this is called
1547 * directly by the user, we don't have that but we still need
1548 * to honor not logging when disabled. For the iterated
1549 * trace case, we save the enabled state upon start and just
1550 * ignore the following data calls.
1552 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
) ||
1553 trace_trigger_soft_disabled(file
)) {
1554 trace_state
->disabled
= true;
1559 trace_state
->event
= file
->event_call
->data
;
1565 __synth_event_trace_start(struct trace_event_file
*file
,
1566 struct synth_event_trace_state
*trace_state
,
1567 int dynamic_fields_size
)
1569 int entry_size
, fields_size
= 0;
1572 fields_size
= trace_state
->event
->n_u64
* sizeof(u64
);
1573 fields_size
+= dynamic_fields_size
;
1576 * Avoid ring buffer recursion detection, as this event
1577 * is being performed within another event.
1579 trace_state
->buffer
= file
->tr
->array_buffer
.buffer
;
1580 ring_buffer_nest_start(trace_state
->buffer
);
1582 entry_size
= sizeof(*trace_state
->entry
) + fields_size
;
1583 trace_state
->entry
= trace_event_buffer_reserve(&trace_state
->fbuffer
,
1586 if (!trace_state
->entry
) {
1587 ring_buffer_nest_end(trace_state
->buffer
);
1595 __synth_event_trace_end(struct synth_event_trace_state
*trace_state
)
1597 trace_event_buffer_commit(&trace_state
->fbuffer
);
1599 ring_buffer_nest_end(trace_state
->buffer
);
1603 * synth_event_trace - Trace a synthetic event
1604 * @file: The trace_event_file representing the synthetic event
1605 * @n_vals: The number of values in vals
1606 * @args: Variable number of args containing the event values
1608 * Trace a synthetic event using the values passed in the variable
1611 * The argument list should be a list 'n_vals' u64 values. The number
1612 * of vals must match the number of field in the synthetic event, and
1613 * must be in the same order as the synthetic event fields.
1615 * All vals should be cast to u64, and string vals are just pointers
1616 * to strings, cast to u64. Strings will be copied into space
1617 * reserved in the event for the string, using these pointers.
1619 * Return: 0 on success, err otherwise.
1621 int synth_event_trace(struct trace_event_file
*file
, unsigned int n_vals
, ...)
1623 unsigned int i
, n_u64
, len
, data_size
= 0;
1624 struct synth_event_trace_state state
;
1628 ret
= __synth_event_trace_init(file
, &state
);
1631 ret
= 0; /* just disabled, not really an error */
1635 if (state
.event
->n_dynamic_fields
) {
1636 va_start(args
, n_vals
);
1638 for (i
= 0; i
< state
.event
->n_fields
; i
++) {
1639 u64 val
= va_arg(args
, u64
);
1641 if (state
.event
->fields
[i
]->is_string
&&
1642 state
.event
->fields
[i
]->is_dynamic
) {
1643 char *str_val
= (char *)(long)val
;
1645 data_size
+= strlen(str_val
) + 1;
1652 ret
= __synth_event_trace_start(file
, &state
, data_size
);
1656 if (n_vals
!= state
.event
->n_fields
) {
1663 va_start(args
, n_vals
);
1664 for (i
= 0, n_u64
= 0; i
< state
.event
->n_fields
; i
++) {
1667 val
= va_arg(args
, u64
);
1669 if (state
.event
->fields
[i
]->is_string
) {
1670 char *str_val
= (char *)(long)val
;
1672 len
= trace_string(state
.entry
, state
.event
, str_val
,
1673 state
.event
->fields
[i
]->is_dynamic
,
1675 data_size
+= len
; /* only dynamic string increments */
1677 struct synth_field
*field
= state
.event
->fields
[i
];
1679 switch (field
->size
) {
1681 *(u8
*)&state
.entry
->fields
[n_u64
] = (u8
)val
;
1685 *(u16
*)&state
.entry
->fields
[n_u64
] = (u16
)val
;
1689 *(u32
*)&state
.entry
->fields
[n_u64
] = (u32
)val
;
1693 state
.entry
->fields
[n_u64
] = val
;
1701 __synth_event_trace_end(&state
);
1705 EXPORT_SYMBOL_GPL(synth_event_trace
);
1708 * synth_event_trace_array - Trace a synthetic event from an array
1709 * @file: The trace_event_file representing the synthetic event
1710 * @vals: Array of values
1711 * @n_vals: The number of values in vals
1713 * Trace a synthetic event using the values passed in as 'vals'.
1715 * The 'vals' array is just an array of 'n_vals' u64. The number of
1716 * vals must match the number of field in the synthetic event, and
1717 * must be in the same order as the synthetic event fields.
1719 * All vals should be cast to u64, and string vals are just pointers
1720 * to strings, cast to u64. Strings will be copied into space
1721 * reserved in the event for the string, using these pointers.
1723 * Return: 0 on success, err otherwise.
1725 int synth_event_trace_array(struct trace_event_file
*file
, u64
*vals
,
1726 unsigned int n_vals
)
1728 unsigned int i
, n_u64
, field_pos
, len
, data_size
= 0;
1729 struct synth_event_trace_state state
;
1733 ret
= __synth_event_trace_init(file
, &state
);
1736 ret
= 0; /* just disabled, not really an error */
1740 if (state
.event
->n_dynamic_fields
) {
1741 for (i
= 0; i
< state
.event
->n_dynamic_fields
; i
++) {
1742 field_pos
= state
.event
->dynamic_fields
[i
]->field_pos
;
1743 str_val
= (char *)(long)vals
[field_pos
];
1744 len
= strlen(str_val
) + 1;
1749 ret
= __synth_event_trace_start(file
, &state
, data_size
);
1753 if (n_vals
!= state
.event
->n_fields
) {
1760 for (i
= 0, n_u64
= 0; i
< state
.event
->n_fields
; i
++) {
1761 if (state
.event
->fields
[i
]->is_string
) {
1762 char *str_val
= (char *)(long)vals
[i
];
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
];
1772 switch (field
->size
) {
1774 *(u8
*)&state
.entry
->fields
[n_u64
] = (u8
)val
;
1778 *(u16
*)&state
.entry
->fields
[n_u64
] = (u16
)val
;
1782 *(u32
*)&state
.entry
->fields
[n_u64
] = (u32
)val
;
1786 state
.entry
->fields
[n_u64
] = val
;
1793 __synth_event_trace_end(&state
);
1797 EXPORT_SYMBOL_GPL(synth_event_trace_array
);
1800 * synth_event_trace_start - Start piecewise synthetic event trace
1801 * @file: The trace_event_file representing the synthetic event
1802 * @trace_state: A pointer to object tracking the piecewise trace state
1804 * Start the trace of a synthetic event field-by-field rather than all
1807 * This function 'opens' an event trace, which means space is reserved
1808 * for the event in the trace buffer, after which the event's
1809 * individual field values can be set through either
1810 * synth_event_add_next_val() or synth_event_add_val().
1812 * A pointer to a trace_state object is passed in, which will keep
1813 * track of the current event trace state until the event trace is
1814 * closed (and the event finally traced) using
1815 * synth_event_trace_end().
1817 * Note that synth_event_trace_end() must be called after all values
1818 * have been added for each event trace, regardless of whether adding
1819 * all field values succeeded or not.
1821 * Note also that for a given event trace, all fields must be added
1822 * using either synth_event_add_next_val() or synth_event_add_val()
1823 * but not both together or interleaved.
1825 * Return: 0 on success, err otherwise.
1827 int synth_event_trace_start(struct trace_event_file
*file
,
1828 struct synth_event_trace_state
*trace_state
)
1835 ret
= __synth_event_trace_init(file
, trace_state
);
1838 ret
= 0; /* just disabled, not really an error */
1842 if (trace_state
->event
->n_dynamic_fields
)
1845 ret
= __synth_event_trace_start(file
, trace_state
, 0);
1849 EXPORT_SYMBOL_GPL(synth_event_trace_start
);
1851 static int __synth_event_add_val(const char *field_name
, u64 val
,
1852 struct synth_event_trace_state
*trace_state
)
1854 struct synth_field
*field
= NULL
;
1855 struct synth_trace_event
*entry
;
1856 struct synth_event
*event
;
1864 /* can't mix add_next_synth_val() with add_synth_val() */
1866 if (trace_state
->add_next
) {
1870 trace_state
->add_name
= true;
1872 if (trace_state
->add_name
) {
1876 trace_state
->add_next
= true;
1879 if (trace_state
->disabled
)
1882 event
= trace_state
->event
;
1883 if (trace_state
->add_name
) {
1884 for (i
= 0; i
< event
->n_fields
; i
++) {
1885 field
= event
->fields
[i
];
1886 if (strcmp(field
->name
, field_name
) == 0)
1894 if (trace_state
->cur_field
>= event
->n_fields
) {
1898 field
= event
->fields
[trace_state
->cur_field
++];
1901 entry
= trace_state
->entry
;
1902 if (field
->is_string
) {
1903 char *str_val
= (char *)(long)val
;
1906 if (field
->is_dynamic
) { /* add_val can't do dynamic strings */
1916 str_field
= (char *)&entry
->fields
[field
->offset
];
1917 strscpy(str_field
, str_val
, STR_VAR_LEN_MAX
);
1919 switch (field
->size
) {
1921 *(u8
*)&trace_state
->entry
->fields
[field
->offset
] = (u8
)val
;
1925 *(u16
*)&trace_state
->entry
->fields
[field
->offset
] = (u16
)val
;
1929 *(u32
*)&trace_state
->entry
->fields
[field
->offset
] = (u32
)val
;
1933 trace_state
->entry
->fields
[field
->offset
] = val
;
1942 * synth_event_add_next_val - Add the next field's value to an open synth trace
1943 * @val: The value to set the next field to
1944 * @trace_state: A pointer to object tracking the piecewise trace state
1946 * Set the value of the next field in an event that's been opened by
1947 * synth_event_trace_start().
1949 * The val param should be the value cast to u64. If the value points
1950 * to a string, the val param should be a char * cast to u64.
1952 * This function assumes all the fields in an event are to be set one
1953 * after another - successive calls to this function are made, one for
1954 * each field, in the order of the fields in the event, until all
1955 * fields have been set. If you'd rather set each field individually
1956 * without regard to ordering, synth_event_add_val() can be used
1959 * Note however that synth_event_add_next_val() and
1960 * synth_event_add_val() can't be intermixed for a given event trace -
1961 * one or the other but not both can be used at the same time.
1963 * Note also that synth_event_trace_end() must be called after all
1964 * values have been added for each event trace, regardless of whether
1965 * adding all field values succeeded or not.
1967 * Return: 0 on success, err otherwise.
1969 int synth_event_add_next_val(u64 val
,
1970 struct synth_event_trace_state
*trace_state
)
1972 return __synth_event_add_val(NULL
, val
, trace_state
);
1974 EXPORT_SYMBOL_GPL(synth_event_add_next_val
);
1977 * synth_event_add_val - Add a named field's value to an open synth trace
1978 * @field_name: The name of the synthetic event field value to set
1979 * @val: The value to set the next field to
1980 * @trace_state: A pointer to object tracking the piecewise trace state
1982 * Set the value of the named field in an event that's been opened by
1983 * synth_event_trace_start().
1985 * The val param should be the value cast to u64. If the value points
1986 * to a string, the val param should be a char * cast to u64.
1988 * This function looks up the field name, and if found, sets the field
1989 * to the specified value. This lookup makes this function more
1990 * expensive than synth_event_add_next_val(), so use that or the
1991 * none-piecewise synth_event_trace() instead if efficiency is more
1994 * Note however that synth_event_add_next_val() and
1995 * synth_event_add_val() can't be intermixed for a given event trace -
1996 * one or the other but not both can be used at the same time.
1998 * Note also that synth_event_trace_end() must be called after all
1999 * values have been added for each event trace, regardless of whether
2000 * adding all field values succeeded or not.
2002 * Return: 0 on success, err otherwise.
2004 int synth_event_add_val(const char *field_name
, u64 val
,
2005 struct synth_event_trace_state
*trace_state
)
2007 return __synth_event_add_val(field_name
, val
, trace_state
);
2009 EXPORT_SYMBOL_GPL(synth_event_add_val
);
2012 * synth_event_trace_end - End piecewise synthetic event trace
2013 * @trace_state: A pointer to object tracking the piecewise trace state
2015 * End the trace of a synthetic event opened by
2016 * synth_event_trace__start().
2018 * This function 'closes' an event trace, which basically means that
2019 * it commits the reserved event and cleans up other loose ends.
2021 * A pointer to a trace_state object is passed in, which will keep
2022 * track of the current event trace state opened with
2023 * synth_event_trace_start().
2025 * Note that this function must be called after all values have been
2026 * added for each event trace, regardless of whether adding all field
2027 * values succeeded or not.
2029 * Return: 0 on success, err otherwise.
2031 int synth_event_trace_end(struct synth_event_trace_state
*trace_state
)
2036 __synth_event_trace_end(trace_state
);
2040 EXPORT_SYMBOL_GPL(synth_event_trace_end
);
2042 static int create_synth_event(const char *raw_command
)
2048 raw_command
= skip_spaces(raw_command
);
2049 if (raw_command
[0] == '\0')
2052 last_cmd_set(raw_command
);
2054 p
= strpbrk(raw_command
, " \t");
2056 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
2060 fields
= skip_spaces(p
);
2064 if (name
[0] != 's' || name
[1] != ':')
2068 /* This interface accepts group name prefix */
2069 if (strchr(name
, '/')) {
2070 len
= str_has_prefix(name
, SYNTH_SYSTEM
"/");
2072 synth_err(SYNTH_ERR_INVALID_DYN_CMD
, 0);
2078 len
= name
- raw_command
;
2080 ret
= check_command(raw_command
+ len
);
2082 synth_err(SYNTH_ERR_INVALID_CMD
, 0);
2086 name
= kmemdup_nul(raw_command
+ len
, p
- raw_command
- len
, GFP_KERNEL
);
2090 ret
= __create_synth_event(name
, fields
);
2097 static int synth_event_release(struct dyn_event
*ev
)
2099 struct synth_event
*event
= to_synth_event(ev
);
2105 ret
= unregister_synth_event(event
);
2109 dyn_event_remove(ev
);
2110 free_synth_event(event
);
2114 static int __synth_event_show(struct seq_file
*m
, struct synth_event
*event
)
2116 struct synth_field
*field
;
2120 seq_printf(m
, "%s\t", event
->name
);
2122 for (i
= 0; i
< event
->n_fields
; i
++) {
2123 field
= event
->fields
[i
];
2126 t
= strstr(type
, "__data_loc");
2127 if (t
) { /* __data_loc belongs in format but not event desc */
2128 t
+= sizeof("__data_loc");
2132 /* parameter values */
2133 seq_printf(m
, "%s %s%s", type
, field
->name
,
2134 i
== event
->n_fields
- 1 ? "" : "; ");
2142 static int synth_event_show(struct seq_file
*m
, struct dyn_event
*ev
)
2144 struct synth_event
*event
= to_synth_event(ev
);
2146 seq_printf(m
, "s:%s/", event
->class.system
);
2148 return __synth_event_show(m
, event
);
2151 static int synth_events_seq_show(struct seq_file
*m
, void *v
)
2153 struct dyn_event
*ev
= v
;
2155 if (!is_synth_event(ev
))
2158 return __synth_event_show(m
, to_synth_event(ev
));
2161 static const struct seq_operations synth_events_seq_op
= {
2162 .start
= dyn_event_seq_start
,
2163 .next
= dyn_event_seq_next
,
2164 .stop
= dyn_event_seq_stop
,
2165 .show
= synth_events_seq_show
,
2168 static int synth_events_open(struct inode
*inode
, struct file
*file
)
2172 ret
= security_locked_down(LOCKDOWN_TRACEFS
);
2176 if ((file
->f_mode
& FMODE_WRITE
) && (file
->f_flags
& O_TRUNC
)) {
2177 ret
= dyn_events_release_all(&synth_event_ops
);
2182 return seq_open(file
, &synth_events_seq_op
);
2185 static ssize_t
synth_events_write(struct file
*file
,
2186 const char __user
*buffer
,
2187 size_t count
, loff_t
*ppos
)
2189 return trace_parse_run_command(file
, buffer
, count
, ppos
,
2190 create_or_delete_synth_event
);
2193 static const struct file_operations synth_events_fops
= {
2194 .open
= synth_events_open
,
2195 .write
= synth_events_write
,
2197 .llseek
= seq_lseek
,
2198 .release
= seq_release
,
2202 * Register dynevent at core_initcall. This allows kernel to setup kprobe
2203 * events in postcore_initcall without tracefs.
2205 static __init
int trace_events_synth_init_early(void)
2209 err
= dyn_event_register(&synth_event_ops
);
2211 pr_warn("Could not register synth_event_ops\n");
2215 core_initcall(trace_events_synth_init_early
);
2217 static __init
int trace_events_synth_init(void)
2219 struct dentry
*entry
= NULL
;
2221 err
= tracing_init_dentry();
2225 entry
= tracefs_create_file("synthetic_events", 0644, NULL
,
2226 NULL
, &synth_events_fops
);
2234 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2239 fs_initcall(trace_events_synth_init
);