1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_hist - trace event hist triggers
5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/stacktrace.h>
13 #include <linux/rculist.h>
14 #include <linux/tracefs.h>
16 #include "tracing_map.h"
18 #include "trace_dynevent.h"
20 #define SYNTH_SYSTEM "synthetic"
21 #define SYNTH_FIELDS_MAX 16
23 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
27 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
28 struct tracing_map_elt *elt,
29 struct ring_buffer_event *rbe,
32 #define HIST_FIELD_OPERANDS_MAX 2
33 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
34 #define HIST_ACTIONS_MAX 8
44 * A hist_var (histogram variable) contains variable information for
45 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
46 * flag set. A hist_var has a variable name e.g. ts0, and is
47 * associated with a given histogram trigger, as specified by
48 * hist_data. The hist_var idx is the unique index assigned to the
49 * variable by the hist trigger's tracing_map. The idx is what is
50 * used to set a variable's value and, by a variable reference, to
55 struct hist_trigger_data *hist_data;
60 struct ftrace_event_field *field;
65 unsigned int is_signed;
67 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
68 struct hist_trigger_data *hist_data;
71 * Variable fields contain variable-specific info in var.
74 enum field_op_id operator;
79 * The name field is used for EXPR and VAR_REF fields. VAR
80 * fields contain the variable name in var.name.
85 * When a histogram trigger is hit, if it has any references
86 * to variables, the values of those variables are collected
87 * into a var_ref_vals array by resolve_var_refs(). The
88 * current value of each variable is read from the tracing_map
89 * using the hist field's hist_var.idx and entered into the
90 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
92 unsigned int var_ref_idx;
96 static u64 hist_field_none(struct hist_field *field,
97 struct tracing_map_elt *elt,
98 struct ring_buffer_event *rbe,
104 static u64 hist_field_counter(struct hist_field *field,
105 struct tracing_map_elt *elt,
106 struct ring_buffer_event *rbe,
112 static u64 hist_field_string(struct hist_field *hist_field,
113 struct tracing_map_elt *elt,
114 struct ring_buffer_event *rbe,
117 char *addr = (char *)(event + hist_field->field->offset);
119 return (u64)(unsigned long)addr;
122 static u64 hist_field_dynstring(struct hist_field *hist_field,
123 struct tracing_map_elt *elt,
124 struct ring_buffer_event *rbe,
127 u32 str_item = *(u32 *)(event + hist_field->field->offset);
128 int str_loc = str_item & 0xffff;
129 char *addr = (char *)(event + str_loc);
131 return (u64)(unsigned long)addr;
134 static u64 hist_field_pstring(struct hist_field *hist_field,
135 struct tracing_map_elt *elt,
136 struct ring_buffer_event *rbe,
139 char **addr = (char **)(event + hist_field->field->offset);
141 return (u64)(unsigned long)*addr;
144 static u64 hist_field_log2(struct hist_field *hist_field,
145 struct tracing_map_elt *elt,
146 struct ring_buffer_event *rbe,
149 struct hist_field *operand = hist_field->operands[0];
151 u64 val = operand->fn(operand, elt, rbe, event);
153 return (u64) ilog2(roundup_pow_of_two(val));
156 static u64 hist_field_plus(struct hist_field *hist_field,
157 struct tracing_map_elt *elt,
158 struct ring_buffer_event *rbe,
161 struct hist_field *operand1 = hist_field->operands[0];
162 struct hist_field *operand2 = hist_field->operands[1];
164 u64 val1 = operand1->fn(operand1, elt, rbe, event);
165 u64 val2 = operand2->fn(operand2, elt, rbe, event);
170 static u64 hist_field_minus(struct hist_field *hist_field,
171 struct tracing_map_elt *elt,
172 struct ring_buffer_event *rbe,
175 struct hist_field *operand1 = hist_field->operands[0];
176 struct hist_field *operand2 = hist_field->operands[1];
178 u64 val1 = operand1->fn(operand1, elt, rbe, event);
179 u64 val2 = operand2->fn(operand2, elt, rbe, event);
184 static u64 hist_field_unary_minus(struct hist_field *hist_field,
185 struct tracing_map_elt *elt,
186 struct ring_buffer_event *rbe,
189 struct hist_field *operand = hist_field->operands[0];
191 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
192 u64 val = (u64)-sval;
197 #define DEFINE_HIST_FIELD_FN(type) \
198 static u64 hist_field_##type(struct hist_field *hist_field, \
199 struct tracing_map_elt *elt, \
200 struct ring_buffer_event *rbe, \
203 type *addr = (type *)(event + hist_field->field->offset); \
205 return (u64)(unsigned long)*addr; \
208 DEFINE_HIST_FIELD_FN(s64);
209 DEFINE_HIST_FIELD_FN(u64);
210 DEFINE_HIST_FIELD_FN(s32);
211 DEFINE_HIST_FIELD_FN(u32);
212 DEFINE_HIST_FIELD_FN(s16);
213 DEFINE_HIST_FIELD_FN(u16);
214 DEFINE_HIST_FIELD_FN(s8);
215 DEFINE_HIST_FIELD_FN(u8);
217 #define for_each_hist_field(i, hist_data) \
218 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
220 #define for_each_hist_val_field(i, hist_data) \
221 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
223 #define for_each_hist_key_field(i, hist_data) \
224 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
226 #define HIST_STACKTRACE_DEPTH 16
227 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
228 #define HIST_STACKTRACE_SKIP 5
230 #define HITCOUNT_IDX 0
231 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
233 enum hist_field_flags {
234 HIST_FIELD_FL_HITCOUNT = 1 << 0,
235 HIST_FIELD_FL_KEY = 1 << 1,
236 HIST_FIELD_FL_STRING = 1 << 2,
237 HIST_FIELD_FL_HEX = 1 << 3,
238 HIST_FIELD_FL_SYM = 1 << 4,
239 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
240 HIST_FIELD_FL_EXECNAME = 1 << 6,
241 HIST_FIELD_FL_SYSCALL = 1 << 7,
242 HIST_FIELD_FL_STACKTRACE = 1 << 8,
243 HIST_FIELD_FL_LOG2 = 1 << 9,
244 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
245 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
246 HIST_FIELD_FL_VAR = 1 << 12,
247 HIST_FIELD_FL_EXPR = 1 << 13,
248 HIST_FIELD_FL_VAR_REF = 1 << 14,
249 HIST_FIELD_FL_CPU = 1 << 15,
250 HIST_FIELD_FL_ALIAS = 1 << 16,
255 char *name[TRACING_MAP_VARS_MAX];
256 char *expr[TRACING_MAP_VARS_MAX];
259 struct hist_trigger_attrs {
269 unsigned int map_bits;
271 char *assignment_str[TRACING_MAP_VARS_MAX];
272 unsigned int n_assignments;
274 char *action_str[HIST_ACTIONS_MAX];
275 unsigned int n_actions;
277 struct var_defs var_defs;
281 struct hist_field *var;
282 struct hist_field *val;
285 struct field_var_hist {
286 struct hist_trigger_data *hist_data;
290 struct hist_trigger_data {
291 struct hist_field *fields[HIST_FIELDS_MAX];
294 unsigned int n_fields;
296 unsigned int key_size;
297 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
298 unsigned int n_sort_keys;
299 struct trace_event_file *event_file;
300 struct hist_trigger_attrs *attrs;
301 struct tracing_map *map;
302 bool enable_timestamps;
304 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
305 unsigned int n_var_refs;
307 struct action_data *actions[HIST_ACTIONS_MAX];
308 unsigned int n_actions;
310 struct field_var *field_vars[SYNTH_FIELDS_MAX];
311 unsigned int n_field_vars;
312 unsigned int n_field_var_str;
313 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
314 unsigned int n_field_var_hists;
316 struct field_var *max_vars[SYNTH_FIELDS_MAX];
317 unsigned int n_max_vars;
318 unsigned int n_max_var_str;
321 static int synth_event_create(int argc, const char **argv);
322 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
323 static int synth_event_release(struct dyn_event *ev);
324 static bool synth_event_is_busy(struct dyn_event *ev);
325 static bool synth_event_match(const char *system, const char *event,
326 struct dyn_event *ev);
328 static struct dyn_event_operations synth_event_ops = {
329 .create = synth_event_create,
330 .show = synth_event_show,
331 .is_busy = synth_event_is_busy,
332 .free = synth_event_release,
333 .match = synth_event_match,
345 struct dyn_event devent;
348 struct synth_field **fields;
349 unsigned int n_fields;
351 struct trace_event_class class;
352 struct trace_event_call call;
353 struct tracepoint *tp;
356 static bool is_synth_event(struct dyn_event *ev)
358 return ev->ops == &synth_event_ops;
361 static struct synth_event *to_synth_event(struct dyn_event *ev)
363 return container_of(ev, struct synth_event, devent);
366 static bool synth_event_is_busy(struct dyn_event *ev)
368 struct synth_event *event = to_synth_event(ev);
370 return event->ref != 0;
373 static bool synth_event_match(const char *system, const char *event,
374 struct dyn_event *ev)
376 struct synth_event *sev = to_synth_event(ev);
378 return strcmp(sev->name, event) == 0 &&
379 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
384 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
385 struct tracing_map_elt *elt, void *rec,
386 struct ring_buffer_event *rbe,
387 struct action_data *data, u64 *var_ref_vals);
391 unsigned int n_params;
392 char *params[SYNTH_FIELDS_MAX];
397 * When a histogram trigger is hit, the values of any
398 * references to variables, including variables being passed
399 * as parameters to synthetic events, are collected into a
400 * var_ref_vals array. This var_ref_idx is the index of the
401 * first param in the array to be passed to the synthetic
404 unsigned int var_ref_idx;
406 char *match_event_system;
407 char *synth_event_name;
408 struct synth_event *synth_event;
414 unsigned int max_var_ref_idx;
415 struct hist_field *max_var;
416 struct hist_field *var;
422 static char last_hist_cmd[MAX_FILTER_STR_VAL];
423 static char hist_err_str[MAX_FILTER_STR_VAL];
425 static void last_cmd_set(char *str)
430 strncpy(last_hist_cmd, str, MAX_FILTER_STR_VAL - 1);
433 static void hist_err(char *str, char *var)
435 int maxlen = MAX_FILTER_STR_VAL - 1;
440 if (strlen(hist_err_str))
446 if (strlen(hist_err_str) + strlen(str) + strlen(var) > maxlen)
449 strcat(hist_err_str, str);
450 strcat(hist_err_str, var);
453 static void hist_err_event(char *str, char *system, char *event, char *var)
455 char err[MAX_FILTER_STR_VAL];
458 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s.%s", system, event, var);
460 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
462 strscpy(err, var, MAX_FILTER_STR_VAL);
467 static void hist_err_clear(void)
469 hist_err_str[0] = '\0';
472 static bool have_hist_err(void)
474 if (strlen(hist_err_str))
480 struct synth_trace_event {
481 struct trace_entry ent;
485 static int synth_event_define_fields(struct trace_event_call *call)
487 struct synth_trace_event trace;
488 int offset = offsetof(typeof(trace), fields);
489 struct synth_event *event = call->data;
490 unsigned int i, size, n_u64;
495 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
496 size = event->fields[i]->size;
497 is_signed = event->fields[i]->is_signed;
498 type = event->fields[i]->type;
499 name = event->fields[i]->name;
500 ret = trace_define_field(call, type, name, offset, size,
501 is_signed, FILTER_OTHER);
505 if (event->fields[i]->is_string) {
506 offset += STR_VAR_LEN_MAX;
507 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
509 offset += sizeof(u64);
514 event->n_u64 = n_u64;
519 static bool synth_field_signed(char *type)
521 if (str_has_prefix(type, "u"))
527 static int synth_field_is_string(char *type)
529 if (strstr(type, "char[") != NULL)
535 static int synth_field_string_size(char *type)
537 char buf[4], *end, *start;
541 start = strstr(type, "char[");
544 start += sizeof("char[") - 1;
546 end = strchr(type, ']');
547 if (!end || end < start)
554 strncpy(buf, start, len);
557 err = kstrtouint(buf, 0, &size);
561 if (size > STR_VAR_LEN_MAX)
567 static int synth_field_size(char *type)
571 if (strcmp(type, "s64") == 0)
573 else if (strcmp(type, "u64") == 0)
575 else if (strcmp(type, "s32") == 0)
577 else if (strcmp(type, "u32") == 0)
579 else if (strcmp(type, "s16") == 0)
581 else if (strcmp(type, "u16") == 0)
583 else if (strcmp(type, "s8") == 0)
585 else if (strcmp(type, "u8") == 0)
587 else if (strcmp(type, "char") == 0)
589 else if (strcmp(type, "unsigned char") == 0)
590 size = sizeof(unsigned char);
591 else if (strcmp(type, "int") == 0)
593 else if (strcmp(type, "unsigned int") == 0)
594 size = sizeof(unsigned int);
595 else if (strcmp(type, "long") == 0)
597 else if (strcmp(type, "unsigned long") == 0)
598 size = sizeof(unsigned long);
599 else if (strcmp(type, "pid_t") == 0)
600 size = sizeof(pid_t);
601 else if (synth_field_is_string(type))
602 size = synth_field_string_size(type);
607 static const char *synth_field_fmt(char *type)
609 const char *fmt = "%llu";
611 if (strcmp(type, "s64") == 0)
613 else if (strcmp(type, "u64") == 0)
615 else if (strcmp(type, "s32") == 0)
617 else if (strcmp(type, "u32") == 0)
619 else if (strcmp(type, "s16") == 0)
621 else if (strcmp(type, "u16") == 0)
623 else if (strcmp(type, "s8") == 0)
625 else if (strcmp(type, "u8") == 0)
627 else if (strcmp(type, "char") == 0)
629 else if (strcmp(type, "unsigned char") == 0)
631 else if (strcmp(type, "int") == 0)
633 else if (strcmp(type, "unsigned int") == 0)
635 else if (strcmp(type, "long") == 0)
637 else if (strcmp(type, "unsigned long") == 0)
639 else if (strcmp(type, "pid_t") == 0)
641 else if (synth_field_is_string(type))
647 static enum print_line_t print_synth_event(struct trace_iterator *iter,
649 struct trace_event *event)
651 struct trace_array *tr = iter->tr;
652 struct trace_seq *s = &iter->seq;
653 struct synth_trace_event *entry;
654 struct synth_event *se;
655 unsigned int i, n_u64;
659 entry = (struct synth_trace_event *)iter->ent;
660 se = container_of(event, struct synth_event, call.event);
662 trace_seq_printf(s, "%s: ", se->name);
664 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
665 if (trace_seq_has_overflowed(s))
668 fmt = synth_field_fmt(se->fields[i]->type);
670 /* parameter types */
671 if (tr->trace_flags & TRACE_ITER_VERBOSE)
672 trace_seq_printf(s, "%s ", fmt);
674 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
676 /* parameter values */
677 if (se->fields[i]->is_string) {
678 trace_seq_printf(s, print_fmt, se->fields[i]->name,
679 (char *)&entry->fields[n_u64],
680 i == se->n_fields - 1 ? "" : " ");
681 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
683 trace_seq_printf(s, print_fmt, se->fields[i]->name,
684 entry->fields[n_u64],
685 i == se->n_fields - 1 ? "" : " ");
690 trace_seq_putc(s, '\n');
692 return trace_handle_return(s);
695 static struct trace_event_functions synth_event_funcs = {
696 .trace = print_synth_event
699 static notrace void trace_event_raw_event_synth(void *__data,
701 unsigned int var_ref_idx)
703 struct trace_event_file *trace_file = __data;
704 struct synth_trace_event *entry;
705 struct trace_event_buffer fbuffer;
706 struct ring_buffer *buffer;
707 struct synth_event *event;
708 unsigned int i, n_u64;
711 event = trace_file->event_call->data;
713 if (trace_trigger_soft_disabled(trace_file))
716 fields_size = event->n_u64 * sizeof(u64);
719 * Avoid ring buffer recursion detection, as this event
720 * is being performed within another event.
722 buffer = trace_file->tr->trace_buffer.buffer;
723 ring_buffer_nest_start(buffer);
725 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
726 sizeof(*entry) + fields_size);
730 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
731 if (event->fields[i]->is_string) {
732 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
733 char *str_field = (char *)&entry->fields[n_u64];
735 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
736 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
738 entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
743 trace_event_buffer_commit(&fbuffer);
745 ring_buffer_nest_end(buffer);
748 static void free_synth_event_print_fmt(struct trace_event_call *call)
751 kfree(call->print_fmt);
752 call->print_fmt = NULL;
756 static int __set_synth_event_print_fmt(struct synth_event *event,
763 /* When len=0, we just calculate the needed length */
764 #define LEN_OR_ZERO (len ? len - pos : 0)
766 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
767 for (i = 0; i < event->n_fields; i++) {
768 fmt = synth_field_fmt(event->fields[i]->type);
769 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
770 event->fields[i]->name, fmt,
771 i == event->n_fields - 1 ? "" : ", ");
773 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
775 for (i = 0; i < event->n_fields; i++) {
776 pos += snprintf(buf + pos, LEN_OR_ZERO,
777 ", REC->%s", event->fields[i]->name);
782 /* return the length of print_fmt */
786 static int set_synth_event_print_fmt(struct trace_event_call *call)
788 struct synth_event *event = call->data;
792 /* First: called with 0 length to calculate the needed length */
793 len = __set_synth_event_print_fmt(event, NULL, 0);
795 print_fmt = kmalloc(len + 1, GFP_KERNEL);
799 /* Second: actually write the @print_fmt */
800 __set_synth_event_print_fmt(event, print_fmt, len + 1);
801 call->print_fmt = print_fmt;
806 static void free_synth_field(struct synth_field *field)
813 static struct synth_field *parse_synth_field(int argc, const char **argv,
816 struct synth_field *field;
817 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
820 if (field_type[0] == ';')
823 if (!strcmp(field_type, "unsigned")) {
825 return ERR_PTR(-EINVAL);
826 prefix = "unsigned ";
827 field_type = argv[1];
828 field_name = argv[2];
831 field_name = argv[1];
835 field = kzalloc(sizeof(*field), GFP_KERNEL);
837 return ERR_PTR(-ENOMEM);
839 len = strlen(field_name);
840 array = strchr(field_name, '[');
842 len -= strlen(array);
843 else if (field_name[len - 1] == ';')
846 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
852 if (field_type[0] == ';')
854 len = strlen(field_type) + 1;
856 len += strlen(array);
858 len += strlen(prefix);
860 field->type = kzalloc(len, GFP_KERNEL);
866 strcat(field->type, prefix);
867 strcat(field->type, field_type);
869 strcat(field->type, array);
870 if (field->type[len - 1] == ';')
871 field->type[len - 1] = '\0';
874 field->size = synth_field_size(field->type);
880 if (synth_field_is_string(field->type))
881 field->is_string = true;
883 field->is_signed = synth_field_signed(field->type);
888 free_synth_field(field);
889 field = ERR_PTR(ret);
893 static void free_synth_tracepoint(struct tracepoint *tp)
902 static struct tracepoint *alloc_synth_tracepoint(char *name)
904 struct tracepoint *tp;
906 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
908 return ERR_PTR(-ENOMEM);
910 tp->name = kstrdup(name, GFP_KERNEL);
913 return ERR_PTR(-ENOMEM);
919 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
920 unsigned int var_ref_idx);
922 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
923 unsigned int var_ref_idx)
925 struct tracepoint *tp = event->tp;
927 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
928 struct tracepoint_func *probe_func_ptr;
929 synth_probe_func_t probe_func;
932 if (!(cpu_online(raw_smp_processor_id())))
935 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
936 if (probe_func_ptr) {
938 probe_func = probe_func_ptr->func;
939 __data = probe_func_ptr->data;
940 probe_func(__data, var_ref_vals, var_ref_idx);
941 } while ((++probe_func_ptr)->func);
946 static struct synth_event *find_synth_event(const char *name)
948 struct dyn_event *pos;
949 struct synth_event *event;
951 for_each_dyn_event(pos) {
952 if (!is_synth_event(pos))
954 event = to_synth_event(pos);
955 if (strcmp(event->name, name) == 0)
962 static int register_synth_event(struct synth_event *event)
964 struct trace_event_call *call = &event->call;
967 event->call.class = &event->class;
968 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
969 if (!event->class.system) {
974 event->tp = alloc_synth_tracepoint(event->name);
975 if (IS_ERR(event->tp)) {
976 ret = PTR_ERR(event->tp);
981 INIT_LIST_HEAD(&call->class->fields);
982 call->event.funcs = &synth_event_funcs;
983 call->class->define_fields = synth_event_define_fields;
985 ret = register_trace_event(&call->event);
990 call->flags = TRACE_EVENT_FL_TRACEPOINT;
991 call->class->reg = trace_event_reg;
992 call->class->probe = trace_event_raw_event_synth;
994 call->tp = event->tp;
996 ret = trace_add_event_call(call);
998 pr_warn("Failed to register synthetic event: %s\n",
999 trace_event_name(call));
1003 ret = set_synth_event_print_fmt(call);
1005 trace_remove_event_call(call);
1011 unregister_trace_event(&call->event);
1015 static int unregister_synth_event(struct synth_event *event)
1017 struct trace_event_call *call = &event->call;
1020 ret = trace_remove_event_call(call);
1025 static void free_synth_event(struct synth_event *event)
1032 for (i = 0; i < event->n_fields; i++)
1033 free_synth_field(event->fields[i]);
1035 kfree(event->fields);
1037 kfree(event->class.system);
1038 free_synth_tracepoint(event->tp);
1039 free_synth_event_print_fmt(&event->call);
1043 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1044 struct synth_field **fields)
1046 struct synth_event *event;
1049 event = kzalloc(sizeof(*event), GFP_KERNEL);
1051 event = ERR_PTR(-ENOMEM);
1055 event->name = kstrdup(name, GFP_KERNEL);
1058 event = ERR_PTR(-ENOMEM);
1062 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1063 if (!event->fields) {
1064 free_synth_event(event);
1065 event = ERR_PTR(-ENOMEM);
1069 dyn_event_init(&event->devent, &synth_event_ops);
1071 for (i = 0; i < n_fields; i++)
1072 event->fields[i] = fields[i];
1074 event->n_fields = n_fields;
1079 static void action_trace(struct hist_trigger_data *hist_data,
1080 struct tracing_map_elt *elt, void *rec,
1081 struct ring_buffer_event *rbe,
1082 struct action_data *data, u64 *var_ref_vals)
1084 struct synth_event *event = data->onmatch.synth_event;
1086 trace_synth(event, var_ref_vals, data->onmatch.var_ref_idx);
1089 struct hist_var_data {
1090 struct list_head list;
1091 struct hist_trigger_data *hist_data;
1094 static int __create_synth_event(int argc, const char *name, const char **argv)
1096 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1097 struct synth_event *event = NULL;
1098 int i, consumed = 0, n_fields = 0, ret = 0;
1102 * - Add synthetic event: <event_name> field[;field] ...
1103 * - Remove synthetic event: !<event_name> field[;field] ...
1104 * where 'field' = type field_name
1107 if (name[0] == '\0' || argc < 1)
1110 mutex_lock(&event_mutex);
1112 event = find_synth_event(name);
1118 for (i = 0; i < argc - 1; i++) {
1119 if (strcmp(argv[i], ";") == 0)
1121 if (n_fields == SYNTH_FIELDS_MAX) {
1126 field = parse_synth_field(argc - i, &argv[i], &consumed);
1127 if (IS_ERR(field)) {
1128 ret = PTR_ERR(field);
1131 fields[n_fields++] = field;
1135 if (i < argc && strcmp(argv[i], ";") != 0) {
1140 event = alloc_synth_event(name, n_fields, fields);
1141 if (IS_ERR(event)) {
1142 ret = PTR_ERR(event);
1146 ret = register_synth_event(event);
1148 dyn_event_add(&event->devent);
1150 free_synth_event(event);
1152 mutex_unlock(&event_mutex);
1156 for (i = 0; i < n_fields; i++)
1157 free_synth_field(fields[i]);
1162 static int create_or_delete_synth_event(int argc, char **argv)
1164 const char *name = argv[0];
1165 struct synth_event *event = NULL;
1168 /* trace_run_command() ensures argc != 0 */
1169 if (name[0] == '!') {
1170 mutex_lock(&event_mutex);
1171 event = find_synth_event(name + 1);
1176 ret = unregister_synth_event(event);
1178 dyn_event_remove(&event->devent);
1179 free_synth_event(event);
1184 mutex_unlock(&event_mutex);
1188 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1189 return ret == -ECANCELED ? -EINVAL : ret;
1192 static int synth_event_create(int argc, const char **argv)
1194 const char *name = argv[0];
1197 if (name[0] != 's' || name[1] != ':')
1201 /* This interface accepts group name prefix */
1202 if (strchr(name, '/')) {
1203 len = sizeof(SYNTH_SYSTEM "/") - 1;
1204 if (strncmp(name, SYNTH_SYSTEM "/", len))
1208 return __create_synth_event(argc - 1, name, argv + 1);
1211 static int synth_event_release(struct dyn_event *ev)
1213 struct synth_event *event = to_synth_event(ev);
1219 ret = unregister_synth_event(event);
1223 dyn_event_remove(ev);
1224 free_synth_event(event);
1228 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1230 struct synth_field *field;
1233 seq_printf(m, "%s\t", event->name);
1235 for (i = 0; i < event->n_fields; i++) {
1236 field = event->fields[i];
1238 /* parameter values */
1239 seq_printf(m, "%s %s%s", field->type, field->name,
1240 i == event->n_fields - 1 ? "" : "; ");
1248 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1250 struct synth_event *event = to_synth_event(ev);
1252 seq_printf(m, "s:%s/", event->class.system);
1254 return __synth_event_show(m, event);
1257 static int synth_events_seq_show(struct seq_file *m, void *v)
1259 struct dyn_event *ev = v;
1261 if (!is_synth_event(ev))
1264 return __synth_event_show(m, to_synth_event(ev));
1267 static const struct seq_operations synth_events_seq_op = {
1268 .start = dyn_event_seq_start,
1269 .next = dyn_event_seq_next,
1270 .stop = dyn_event_seq_stop,
1271 .show = synth_events_seq_show,
1274 static int synth_events_open(struct inode *inode, struct file *file)
1278 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1279 ret = dyn_events_release_all(&synth_event_ops);
1284 return seq_open(file, &synth_events_seq_op);
1287 static ssize_t synth_events_write(struct file *file,
1288 const char __user *buffer,
1289 size_t count, loff_t *ppos)
1291 return trace_parse_run_command(file, buffer, count, ppos,
1292 create_or_delete_synth_event);
1295 static const struct file_operations synth_events_fops = {
1296 .open = synth_events_open,
1297 .write = synth_events_write,
1299 .llseek = seq_lseek,
1300 .release = seq_release,
1303 static u64 hist_field_timestamp(struct hist_field *hist_field,
1304 struct tracing_map_elt *elt,
1305 struct ring_buffer_event *rbe,
1308 struct hist_trigger_data *hist_data = hist_field->hist_data;
1309 struct trace_array *tr = hist_data->event_file->tr;
1311 u64 ts = ring_buffer_event_time_stamp(rbe);
1313 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1319 static u64 hist_field_cpu(struct hist_field *hist_field,
1320 struct tracing_map_elt *elt,
1321 struct ring_buffer_event *rbe,
1324 int cpu = smp_processor_id();
1330 * check_field_for_var_ref - Check if a VAR_REF field references a variable
1331 * @hist_field: The VAR_REF field to check
1332 * @var_data: The hist trigger that owns the variable
1333 * @var_idx: The trigger variable identifier
1335 * Check the given VAR_REF field to see whether or not it references
1336 * the given variable associated with the given trigger.
1338 * Return: The VAR_REF field if it does reference the variable, NULL if not
1340 static struct hist_field *
1341 check_field_for_var_ref(struct hist_field *hist_field,
1342 struct hist_trigger_data *var_data,
1343 unsigned int var_idx)
1345 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
1347 if (hist_field && hist_field->var.idx == var_idx &&
1348 hist_field->var.hist_data == var_data)
1355 * find_var_ref - Check if a trigger has a reference to a trigger variable
1356 * @hist_data: The hist trigger that might have a reference to the variable
1357 * @var_data: The hist trigger that owns the variable
1358 * @var_idx: The trigger variable identifier
1360 * Check the list of var_refs[] on the first hist trigger to see
1361 * whether any of them are references to the variable on the second
1364 * Return: The VAR_REF field referencing the variable if so, NULL if not
1366 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1367 struct hist_trigger_data *var_data,
1368 unsigned int var_idx)
1370 struct hist_field *hist_field;
1373 for (i = 0; i < hist_data->n_var_refs; i++) {
1374 hist_field = hist_data->var_refs[i];
1375 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1383 * find_any_var_ref - Check if there is a reference to a given trigger variable
1384 * @hist_data: The hist trigger
1385 * @var_idx: The trigger variable identifier
1387 * Check to see whether the given variable is currently referenced by
1388 * any other trigger.
1390 * The trigger the variable is defined on is explicitly excluded - the
1391 * assumption being that a self-reference doesn't prevent a trigger
1392 * from being removed.
1394 * Return: The VAR_REF field referencing the variable if so, NULL if not
1396 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1397 unsigned int var_idx)
1399 struct trace_array *tr = hist_data->event_file->tr;
1400 struct hist_field *found = NULL;
1401 struct hist_var_data *var_data;
1403 list_for_each_entry(var_data, &tr->hist_vars, list) {
1404 if (var_data->hist_data == hist_data)
1406 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1415 * check_var_refs - Check if there is a reference to any of trigger's variables
1416 * @hist_data: The hist trigger
1418 * A trigger can define one or more variables. If any one of them is
1419 * currently referenced by any other trigger, this function will
1422 * Typically used to determine whether or not a trigger can be removed
1423 * - if there are any references to a trigger's variables, it cannot.
1425 * Return: True if there is a reference to any of trigger's variables
1427 static bool check_var_refs(struct hist_trigger_data *hist_data)
1429 struct hist_field *field;
1433 for_each_hist_field(i, hist_data) {
1434 field = hist_data->fields[i];
1435 if (field && field->flags & HIST_FIELD_FL_VAR) {
1436 if (find_any_var_ref(hist_data, field->var.idx)) {
1446 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1448 struct trace_array *tr = hist_data->event_file->tr;
1449 struct hist_var_data *var_data, *found = NULL;
1451 list_for_each_entry(var_data, &tr->hist_vars, list) {
1452 if (var_data->hist_data == hist_data) {
1461 static bool field_has_hist_vars(struct hist_field *hist_field,
1472 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1473 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1476 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1477 struct hist_field *operand;
1479 operand = hist_field->operands[i];
1480 if (field_has_hist_vars(operand, level + 1))
1487 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1489 struct hist_field *hist_field;
1492 for_each_hist_field(i, hist_data) {
1493 hist_field = hist_data->fields[i];
1494 if (field_has_hist_vars(hist_field, 0))
1501 static int save_hist_vars(struct hist_trigger_data *hist_data)
1503 struct trace_array *tr = hist_data->event_file->tr;
1504 struct hist_var_data *var_data;
1506 var_data = find_hist_vars(hist_data);
1510 if (trace_array_get(tr) < 0)
1513 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1515 trace_array_put(tr);
1519 var_data->hist_data = hist_data;
1520 list_add(&var_data->list, &tr->hist_vars);
1525 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1527 struct trace_array *tr = hist_data->event_file->tr;
1528 struct hist_var_data *var_data;
1530 var_data = find_hist_vars(hist_data);
1534 if (WARN_ON(check_var_refs(hist_data)))
1537 list_del(&var_data->list);
1541 trace_array_put(tr);
1544 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1545 const char *var_name)
1547 struct hist_field *hist_field, *found = NULL;
1550 for_each_hist_field(i, hist_data) {
1551 hist_field = hist_data->fields[i];
1552 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1553 strcmp(hist_field->var.name, var_name) == 0) {
1562 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1563 struct trace_event_file *file,
1564 const char *var_name)
1566 struct hist_trigger_data *test_data;
1567 struct event_trigger_data *test;
1568 struct hist_field *hist_field;
1570 hist_field = find_var_field(hist_data, var_name);
1574 list_for_each_entry_rcu(test, &file->triggers, list) {
1575 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1576 test_data = test->private_data;
1577 hist_field = find_var_field(test_data, var_name);
1586 static struct trace_event_file *find_var_file(struct trace_array *tr,
1591 struct hist_trigger_data *var_hist_data;
1592 struct hist_var_data *var_data;
1593 struct trace_event_file *file, *found = NULL;
1596 return find_event_file(tr, system, event_name);
1598 list_for_each_entry(var_data, &tr->hist_vars, list) {
1599 var_hist_data = var_data->hist_data;
1600 file = var_hist_data->event_file;
1604 if (find_var_field(var_hist_data, var_name)) {
1606 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1617 static struct hist_field *find_file_var(struct trace_event_file *file,
1618 const char *var_name)
1620 struct hist_trigger_data *test_data;
1621 struct event_trigger_data *test;
1622 struct hist_field *hist_field;
1624 list_for_each_entry_rcu(test, &file->triggers, list) {
1625 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1626 test_data = test->private_data;
1627 hist_field = find_var_field(test_data, var_name);
1636 static struct hist_field *
1637 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1639 struct trace_array *tr = hist_data->event_file->tr;
1640 struct hist_field *hist_field, *found = NULL;
1641 struct trace_event_file *file;
1644 for (i = 0; i < hist_data->n_actions; i++) {
1645 struct action_data *data = hist_data->actions[i];
1647 if (data->fn == action_trace) {
1648 char *system = data->onmatch.match_event_system;
1649 char *event_name = data->onmatch.match_event;
1651 file = find_var_file(tr, system, event_name, var_name);
1654 hist_field = find_file_var(file, var_name);
1657 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1658 return ERR_PTR(-EINVAL);
1668 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1673 struct trace_array *tr = hist_data->event_file->tr;
1674 struct hist_field *hist_field = NULL;
1675 struct trace_event_file *file;
1677 if (!system || !event_name) {
1678 hist_field = find_match_var(hist_data, var_name);
1679 if (IS_ERR(hist_field))
1685 file = find_var_file(tr, system, event_name, var_name);
1689 hist_field = find_file_var(file, var_name);
1694 struct hist_elt_data {
1697 char *field_var_str[SYNTH_FIELDS_MAX];
1700 static u64 hist_field_var_ref(struct hist_field *hist_field,
1701 struct tracing_map_elt *elt,
1702 struct ring_buffer_event *rbe,
1705 struct hist_elt_data *elt_data;
1708 elt_data = elt->private_data;
1709 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1714 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1715 u64 *var_ref_vals, bool self)
1717 struct hist_trigger_data *var_data;
1718 struct tracing_map_elt *var_elt;
1719 struct hist_field *hist_field;
1720 unsigned int i, var_idx;
1721 bool resolved = true;
1724 for (i = 0; i < hist_data->n_var_refs; i++) {
1725 hist_field = hist_data->var_refs[i];
1726 var_idx = hist_field->var.idx;
1727 var_data = hist_field->var.hist_data;
1729 if (var_data == NULL) {
1734 if ((self && var_data != hist_data) ||
1735 (!self && var_data == hist_data))
1738 var_elt = tracing_map_lookup(var_data->map, key);
1744 if (!tracing_map_var_set(var_elt, var_idx)) {
1749 if (self || !hist_field->read_once)
1750 var_val = tracing_map_read_var(var_elt, var_idx);
1752 var_val = tracing_map_read_var_once(var_elt, var_idx);
1754 var_ref_vals[i] = var_val;
1760 static const char *hist_field_name(struct hist_field *field,
1763 const char *field_name = "";
1769 field_name = field->field->name;
1770 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1771 field->flags & HIST_FIELD_FL_ALIAS)
1772 field_name = hist_field_name(field->operands[0], ++level);
1773 else if (field->flags & HIST_FIELD_FL_CPU)
1775 else if (field->flags & HIST_FIELD_FL_EXPR ||
1776 field->flags & HIST_FIELD_FL_VAR_REF) {
1777 if (field->system) {
1778 static char full_name[MAX_FILTER_STR_VAL];
1780 strcat(full_name, field->system);
1781 strcat(full_name, ".");
1782 strcat(full_name, field->event_name);
1783 strcat(full_name, ".");
1784 strcat(full_name, field->name);
1785 field_name = full_name;
1787 field_name = field->name;
1788 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1789 field_name = "common_timestamp";
1791 if (field_name == NULL)
1797 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1799 hist_field_fn_t fn = NULL;
1801 switch (field_size) {
1803 if (field_is_signed)
1804 fn = hist_field_s64;
1806 fn = hist_field_u64;
1809 if (field_is_signed)
1810 fn = hist_field_s32;
1812 fn = hist_field_u32;
1815 if (field_is_signed)
1816 fn = hist_field_s16;
1818 fn = hist_field_u16;
1821 if (field_is_signed)
1831 static int parse_map_size(char *str)
1833 unsigned long size, map_bits;
1842 ret = kstrtoul(str, 0, &size);
1846 map_bits = ilog2(roundup_pow_of_two(size));
1847 if (map_bits < TRACING_MAP_BITS_MIN ||
1848 map_bits > TRACING_MAP_BITS_MAX)
1856 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1863 for (i = 0; i < attrs->n_assignments; i++)
1864 kfree(attrs->assignment_str[i]);
1866 for (i = 0; i < attrs->n_actions; i++)
1867 kfree(attrs->action_str[i]);
1870 kfree(attrs->sort_key_str);
1871 kfree(attrs->keys_str);
1872 kfree(attrs->vals_str);
1873 kfree(attrs->clock);
1877 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1881 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1884 if ((str_has_prefix(str, "onmatch(")) ||
1885 (str_has_prefix(str, "onmax("))) {
1886 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1887 if (!attrs->action_str[attrs->n_actions]) {
1898 static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
1902 if ((str_has_prefix(str, "key=")) ||
1903 (str_has_prefix(str, "keys="))) {
1904 attrs->keys_str = kstrdup(str, GFP_KERNEL);
1905 if (!attrs->keys_str) {
1909 } else if ((str_has_prefix(str, "val=")) ||
1910 (str_has_prefix(str, "vals=")) ||
1911 (str_has_prefix(str, "values="))) {
1912 attrs->vals_str = kstrdup(str, GFP_KERNEL);
1913 if (!attrs->vals_str) {
1917 } else if (str_has_prefix(str, "sort=")) {
1918 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
1919 if (!attrs->sort_key_str) {
1923 } else if (str_has_prefix(str, "name=")) {
1924 attrs->name = kstrdup(str, GFP_KERNEL);
1929 } else if (str_has_prefix(str, "clock=")) {
1936 str = strstrip(str);
1937 attrs->clock = kstrdup(str, GFP_KERNEL);
1938 if (!attrs->clock) {
1942 } else if (str_has_prefix(str, "size=")) {
1943 int map_bits = parse_map_size(str);
1949 attrs->map_bits = map_bits;
1953 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1954 hist_err("Too many variables defined: ", str);
1959 assignment = kstrdup(str, GFP_KERNEL);
1965 attrs->assignment_str[attrs->n_assignments++] = assignment;
1971 static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
1973 struct hist_trigger_attrs *attrs;
1976 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1978 return ERR_PTR(-ENOMEM);
1980 while (trigger_str) {
1981 char *str = strsep(&trigger_str, ":");
1983 if (strchr(str, '=')) {
1984 ret = parse_assignment(str, attrs);
1987 } else if (strcmp(str, "pause") == 0)
1988 attrs->pause = true;
1989 else if ((strcmp(str, "cont") == 0) ||
1990 (strcmp(str, "continue") == 0))
1992 else if (strcmp(str, "clear") == 0)
1993 attrs->clear = true;
1995 ret = parse_action(str, attrs);
2001 if (!attrs->keys_str) {
2006 if (!attrs->clock) {
2007 attrs->clock = kstrdup("global", GFP_KERNEL);
2008 if (!attrs->clock) {
2016 destroy_hist_trigger_attrs(attrs);
2018 return ERR_PTR(ret);
2021 static inline void save_comm(char *comm, struct task_struct *task)
2024 strcpy(comm, "<idle>");
2028 if (WARN_ON_ONCE(task->pid < 0)) {
2029 strcpy(comm, "<XXX>");
2033 memcpy(comm, task->comm, TASK_COMM_LEN);
2036 static void hist_elt_data_free(struct hist_elt_data *elt_data)
2040 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2041 kfree(elt_data->field_var_str[i]);
2043 kfree(elt_data->comm);
2047 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2049 struct hist_elt_data *elt_data = elt->private_data;
2051 hist_elt_data_free(elt_data);
2054 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
2056 struct hist_trigger_data *hist_data = elt->map->private_data;
2057 unsigned int size = TASK_COMM_LEN;
2058 struct hist_elt_data *elt_data;
2059 struct hist_field *key_field;
2060 unsigned int i, n_str;
2062 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2066 for_each_hist_key_field(i, hist_data) {
2067 key_field = hist_data->fields[i];
2069 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
2070 elt_data->comm = kzalloc(size, GFP_KERNEL);
2071 if (!elt_data->comm) {
2079 n_str = hist_data->n_field_var_str + hist_data->n_max_var_str;
2081 size = STR_VAR_LEN_MAX;
2083 for (i = 0; i < n_str; i++) {
2084 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2085 if (!elt_data->field_var_str[i]) {
2086 hist_elt_data_free(elt_data);
2091 elt->private_data = elt_data;
2096 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2098 struct hist_elt_data *elt_data = elt->private_data;
2101 save_comm(elt_data->comm, current);
2104 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2105 .elt_alloc = hist_trigger_elt_data_alloc,
2106 .elt_free = hist_trigger_elt_data_free,
2107 .elt_init = hist_trigger_elt_data_init,
2110 static const char *get_hist_field_flags(struct hist_field *hist_field)
2112 const char *flags_str = NULL;
2114 if (hist_field->flags & HIST_FIELD_FL_HEX)
2116 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2118 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2119 flags_str = "sym-offset";
2120 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2121 flags_str = "execname";
2122 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2123 flags_str = "syscall";
2124 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2126 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2127 flags_str = "usecs";
2132 static void expr_field_str(struct hist_field *field, char *expr)
2134 if (field->flags & HIST_FIELD_FL_VAR_REF)
2137 strcat(expr, hist_field_name(field, 0));
2139 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2140 const char *flags_str = get_hist_field_flags(field);
2144 strcat(expr, flags_str);
2149 static char *expr_str(struct hist_field *field, unsigned int level)
2156 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2160 if (!field->operands[0]) {
2161 expr_field_str(field, expr);
2165 if (field->operator == FIELD_OP_UNARY_MINUS) {
2169 subexpr = expr_str(field->operands[0], ++level);
2174 strcat(expr, subexpr);
2182 expr_field_str(field->operands[0], expr);
2184 switch (field->operator) {
2185 case FIELD_OP_MINUS:
2196 expr_field_str(field->operands[1], expr);
2201 static int contains_operator(char *str)
2203 enum field_op_id field_op = FIELD_OP_NONE;
2206 op = strpbrk(str, "+-");
2208 return FIELD_OP_NONE;
2213 field_op = FIELD_OP_UNARY_MINUS;
2215 field_op = FIELD_OP_MINUS;
2218 field_op = FIELD_OP_PLUS;
2227 static void __destroy_hist_field(struct hist_field *hist_field)
2229 kfree(hist_field->var.name);
2230 kfree(hist_field->name);
2231 kfree(hist_field->type);
2236 static void destroy_hist_field(struct hist_field *hist_field,
2247 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2248 return; /* var refs will be destroyed separately */
2250 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2251 destroy_hist_field(hist_field->operands[i], level + 1);
2253 __destroy_hist_field(hist_field);
2256 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2257 struct ftrace_event_field *field,
2258 unsigned long flags,
2261 struct hist_field *hist_field;
2263 if (field && is_function_field(field))
2266 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2270 hist_field->hist_data = hist_data;
2272 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2273 goto out; /* caller will populate */
2275 if (flags & HIST_FIELD_FL_VAR_REF) {
2276 hist_field->fn = hist_field_var_ref;
2280 if (flags & HIST_FIELD_FL_HITCOUNT) {
2281 hist_field->fn = hist_field_counter;
2282 hist_field->size = sizeof(u64);
2283 hist_field->type = kstrdup("u64", GFP_KERNEL);
2284 if (!hist_field->type)
2289 if (flags & HIST_FIELD_FL_STACKTRACE) {
2290 hist_field->fn = hist_field_none;
2294 if (flags & HIST_FIELD_FL_LOG2) {
2295 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2296 hist_field->fn = hist_field_log2;
2297 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2298 hist_field->size = hist_field->operands[0]->size;
2299 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2300 if (!hist_field->type)
2305 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2306 hist_field->fn = hist_field_timestamp;
2307 hist_field->size = sizeof(u64);
2308 hist_field->type = kstrdup("u64", GFP_KERNEL);
2309 if (!hist_field->type)
2314 if (flags & HIST_FIELD_FL_CPU) {
2315 hist_field->fn = hist_field_cpu;
2316 hist_field->size = sizeof(int);
2317 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2318 if (!hist_field->type)
2323 if (WARN_ON_ONCE(!field))
2326 if (is_string_field(field)) {
2327 flags |= HIST_FIELD_FL_STRING;
2329 hist_field->size = MAX_FILTER_STR_VAL;
2330 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2331 if (!hist_field->type)
2334 if (field->filter_type == FILTER_STATIC_STRING)
2335 hist_field->fn = hist_field_string;
2336 else if (field->filter_type == FILTER_DYN_STRING)
2337 hist_field->fn = hist_field_dynstring;
2339 hist_field->fn = hist_field_pstring;
2341 hist_field->size = field->size;
2342 hist_field->is_signed = field->is_signed;
2343 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2344 if (!hist_field->type)
2347 hist_field->fn = select_value_fn(field->size,
2349 if (!hist_field->fn) {
2350 destroy_hist_field(hist_field, 0);
2355 hist_field->field = field;
2356 hist_field->flags = flags;
2359 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2360 if (!hist_field->var.name)
2366 destroy_hist_field(hist_field, 0);
2370 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2374 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2375 if (hist_data->fields[i]) {
2376 destroy_hist_field(hist_data->fields[i], 0);
2377 hist_data->fields[i] = NULL;
2381 for (i = 0; i < hist_data->n_var_refs; i++) {
2382 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2383 __destroy_hist_field(hist_data->var_refs[i]);
2384 hist_data->var_refs[i] = NULL;
2388 static int init_var_ref(struct hist_field *ref_field,
2389 struct hist_field *var_field,
2390 char *system, char *event_name)
2394 ref_field->var.idx = var_field->var.idx;
2395 ref_field->var.hist_data = var_field->hist_data;
2396 ref_field->size = var_field->size;
2397 ref_field->is_signed = var_field->is_signed;
2398 ref_field->flags |= var_field->flags &
2399 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2402 ref_field->system = kstrdup(system, GFP_KERNEL);
2403 if (!ref_field->system)
2408 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2409 if (!ref_field->event_name) {
2415 if (var_field->var.name) {
2416 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2417 if (!ref_field->name) {
2421 } else if (var_field->name) {
2422 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2423 if (!ref_field->name) {
2429 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2430 if (!ref_field->type) {
2437 kfree(ref_field->system);
2438 kfree(ref_field->event_name);
2439 kfree(ref_field->name);
2445 * create_var_ref - Create a variable reference and attach it to trigger
2446 * @hist_data: The trigger that will be referencing the variable
2447 * @var_field: The VAR field to create a reference to
2448 * @system: The optional system string
2449 * @event_name: The optional event_name string
2451 * Given a variable hist_field, create a VAR_REF hist_field that
2452 * represents a reference to it.
2454 * This function also adds the reference to the trigger that
2455 * now references the variable.
2457 * Return: The VAR_REF field if successful, NULL if not
2459 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2460 struct hist_field *var_field,
2461 char *system, char *event_name)
2463 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2464 struct hist_field *ref_field;
2466 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2468 if (init_var_ref(ref_field, var_field, system, event_name)) {
2469 destroy_hist_field(ref_field, 0);
2473 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2474 ref_field->var_ref_idx = hist_data->n_var_refs++;
2480 static bool is_var_ref(char *var_name)
2482 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2488 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2494 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2495 name = hist_data->attrs->var_defs.name[i];
2497 if (strcmp(var_name, name) == 0) {
2498 field = hist_data->attrs->var_defs.expr[i];
2499 if (contains_operator(field) || is_var_ref(field))
2508 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2509 char *system, char *event_name,
2512 struct trace_event_call *call;
2514 if (system && event_name) {
2515 call = hist_data->event_file->event_call;
2517 if (strcmp(system, call->class->system) != 0)
2520 if (strcmp(event_name, trace_event_name(call)) != 0)
2524 if (!!system != !!event_name)
2527 if (!is_var_ref(var_name))
2532 return field_name_from_var(hist_data, var_name);
2535 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2536 char *system, char *event_name,
2539 struct hist_field *var_field = NULL, *ref_field = NULL;
2541 if (!is_var_ref(var_name))
2546 var_field = find_event_var(hist_data, system, event_name, var_name);
2548 ref_field = create_var_ref(hist_data, var_field,
2549 system, event_name);
2552 hist_err_event("Couldn't find variable: $",
2553 system, event_name, var_name);
2558 static struct ftrace_event_field *
2559 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2560 char *field_str, unsigned long *flags)
2562 struct ftrace_event_field *field = NULL;
2563 char *field_name, *modifier, *str;
2565 modifier = str = kstrdup(field_str, GFP_KERNEL);
2567 return ERR_PTR(-ENOMEM);
2569 field_name = strsep(&modifier, ".");
2571 if (strcmp(modifier, "hex") == 0)
2572 *flags |= HIST_FIELD_FL_HEX;
2573 else if (strcmp(modifier, "sym") == 0)
2574 *flags |= HIST_FIELD_FL_SYM;
2575 else if (strcmp(modifier, "sym-offset") == 0)
2576 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2577 else if ((strcmp(modifier, "execname") == 0) &&
2578 (strcmp(field_name, "common_pid") == 0))
2579 *flags |= HIST_FIELD_FL_EXECNAME;
2580 else if (strcmp(modifier, "syscall") == 0)
2581 *flags |= HIST_FIELD_FL_SYSCALL;
2582 else if (strcmp(modifier, "log2") == 0)
2583 *flags |= HIST_FIELD_FL_LOG2;
2584 else if (strcmp(modifier, "usecs") == 0)
2585 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2587 hist_err("Invalid field modifier: ", modifier);
2588 field = ERR_PTR(-EINVAL);
2593 if (strcmp(field_name, "common_timestamp") == 0) {
2594 *flags |= HIST_FIELD_FL_TIMESTAMP;
2595 hist_data->enable_timestamps = true;
2596 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2597 hist_data->attrs->ts_in_usecs = true;
2598 } else if (strcmp(field_name, "cpu") == 0)
2599 *flags |= HIST_FIELD_FL_CPU;
2601 field = trace_find_event_field(file->event_call, field_name);
2602 if (!field || !field->size) {
2603 hist_err("Couldn't find field: ", field_name);
2604 field = ERR_PTR(-EINVAL);
2614 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2615 struct hist_field *var_ref,
2618 struct hist_field *alias = NULL;
2619 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2621 alias = create_hist_field(hist_data, NULL, flags, var_name);
2625 alias->fn = var_ref->fn;
2626 alias->operands[0] = var_ref;
2628 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2629 destroy_hist_field(alias, 0);
2636 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2637 struct trace_event_file *file, char *str,
2638 unsigned long *flags, char *var_name)
2640 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2641 struct ftrace_event_field *field = NULL;
2642 struct hist_field *hist_field = NULL;
2645 s = strchr(str, '.');
2647 s = strchr(++s, '.');
2649 ref_system = strsep(&str, ".");
2654 ref_event = strsep(&str, ".");
2663 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2665 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2668 hist_field = create_alias(hist_data, hist_field, var_name);
2679 field = parse_field(hist_data, file, str, flags);
2680 if (IS_ERR(field)) {
2681 ret = PTR_ERR(field);
2685 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2693 return ERR_PTR(ret);
2696 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2697 struct trace_event_file *file,
2698 char *str, unsigned long flags,
2699 char *var_name, unsigned int level);
2701 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2702 struct trace_event_file *file,
2703 char *str, unsigned long flags,
2704 char *var_name, unsigned int level)
2706 struct hist_field *operand1, *expr = NULL;
2707 unsigned long operand_flags;
2711 /* we support only -(xxx) i.e. explicit parens required */
2714 hist_err("Too many subexpressions (3 max): ", str);
2719 str++; /* skip leading '-' */
2721 s = strchr(str, '(');
2729 s = strrchr(str, ')');
2733 ret = -EINVAL; /* no closing ')' */
2737 flags |= HIST_FIELD_FL_EXPR;
2738 expr = create_hist_field(hist_data, NULL, flags, var_name);
2745 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2746 if (IS_ERR(operand1)) {
2747 ret = PTR_ERR(operand1);
2751 expr->flags |= operand1->flags &
2752 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2753 expr->fn = hist_field_unary_minus;
2754 expr->operands[0] = operand1;
2755 expr->operator = FIELD_OP_UNARY_MINUS;
2756 expr->name = expr_str(expr, 0);
2757 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2765 destroy_hist_field(expr, 0);
2766 return ERR_PTR(ret);
2769 static int check_expr_operands(struct hist_field *operand1,
2770 struct hist_field *operand2)
2772 unsigned long operand1_flags = operand1->flags;
2773 unsigned long operand2_flags = operand2->flags;
2775 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2776 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2777 struct hist_field *var;
2779 var = find_var_field(operand1->var.hist_data, operand1->name);
2782 operand1_flags = var->flags;
2785 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2786 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2787 struct hist_field *var;
2789 var = find_var_field(operand2->var.hist_data, operand2->name);
2792 operand2_flags = var->flags;
2795 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2796 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2797 hist_err("Timestamp units in expression don't match", NULL);
2804 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2805 struct trace_event_file *file,
2806 char *str, unsigned long flags,
2807 char *var_name, unsigned int level)
2809 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2810 unsigned long operand_flags;
2811 int field_op, ret = -EINVAL;
2812 char *sep, *operand1_str;
2815 hist_err("Too many subexpressions (3 max): ", str);
2816 return ERR_PTR(-EINVAL);
2819 field_op = contains_operator(str);
2821 if (field_op == FIELD_OP_NONE)
2822 return parse_atom(hist_data, file, str, &flags, var_name);
2824 if (field_op == FIELD_OP_UNARY_MINUS)
2825 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2828 case FIELD_OP_MINUS:
2838 operand1_str = strsep(&str, sep);
2839 if (!operand1_str || !str)
2843 operand1 = parse_atom(hist_data, file, operand1_str,
2844 &operand_flags, NULL);
2845 if (IS_ERR(operand1)) {
2846 ret = PTR_ERR(operand1);
2851 /* rest of string could be another expression e.g. b+c in a+b+c */
2853 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2854 if (IS_ERR(operand2)) {
2855 ret = PTR_ERR(operand2);
2860 ret = check_expr_operands(operand1, operand2);
2864 flags |= HIST_FIELD_FL_EXPR;
2866 flags |= operand1->flags &
2867 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2869 expr = create_hist_field(hist_data, NULL, flags, var_name);
2875 operand1->read_once = true;
2876 operand2->read_once = true;
2878 expr->operands[0] = operand1;
2879 expr->operands[1] = operand2;
2880 expr->operator = field_op;
2881 expr->name = expr_str(expr, 0);
2882 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2889 case FIELD_OP_MINUS:
2890 expr->fn = hist_field_minus;
2893 expr->fn = hist_field_plus;
2902 destroy_hist_field(operand1, 0);
2903 destroy_hist_field(operand2, 0);
2904 destroy_hist_field(expr, 0);
2906 return ERR_PTR(ret);
2909 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2910 struct trace_event_file *file)
2912 struct event_trigger_data *test;
2914 list_for_each_entry_rcu(test, &file->triggers, list) {
2915 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2916 if (test->private_data == hist_data)
2917 return test->filter_str;
2924 static struct event_command trigger_hist_cmd;
2925 static int event_hist_trigger_func(struct event_command *cmd_ops,
2926 struct trace_event_file *file,
2927 char *glob, char *cmd, char *param);
2929 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2930 struct hist_trigger_data *hist_data,
2931 unsigned int n_keys)
2933 struct hist_field *target_hist_field, *hist_field;
2934 unsigned int n, i, j;
2936 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2939 i = hist_data->n_vals;
2940 j = target_hist_data->n_vals;
2942 for (n = 0; n < n_keys; n++) {
2943 hist_field = hist_data->fields[i + n];
2944 target_hist_field = target_hist_data->fields[j + n];
2946 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2948 if (hist_field->size != target_hist_field->size)
2950 if (hist_field->is_signed != target_hist_field->is_signed)
2957 static struct hist_trigger_data *
2958 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2959 struct trace_event_file *file)
2961 struct hist_trigger_data *hist_data;
2962 struct event_trigger_data *test;
2963 unsigned int n_keys;
2965 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2967 list_for_each_entry_rcu(test, &file->triggers, list) {
2968 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2969 hist_data = test->private_data;
2971 if (compatible_keys(target_hist_data, hist_data, n_keys))
2979 static struct trace_event_file *event_file(struct trace_array *tr,
2980 char *system, char *event_name)
2982 struct trace_event_file *file;
2984 file = __find_event_file(tr, system, event_name);
2986 return ERR_PTR(-EINVAL);
2991 static struct hist_field *
2992 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2993 char *system, char *event_name, char *field_name)
2995 struct hist_field *event_var;
2996 char *synthetic_name;
2998 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2999 if (!synthetic_name)
3000 return ERR_PTR(-ENOMEM);
3002 strcpy(synthetic_name, "synthetic_");
3003 strcat(synthetic_name, field_name);
3005 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3007 kfree(synthetic_name);
3013 * create_field_var_hist - Automatically create a histogram and var for a field
3014 * @target_hist_data: The target hist trigger
3015 * @subsys_name: Optional subsystem name
3016 * @event_name: Optional event name
3017 * @field_name: The name of the field (and the resulting variable)
3019 * Hist trigger actions fetch data from variables, not directly from
3020 * events. However, for convenience, users are allowed to directly
3021 * specify an event field in an action, which will be automatically
3022 * converted into a variable on their behalf.
3024 * If a user specifies a field on an event that isn't the event the
3025 * histogram currently being defined (the target event histogram), the
3026 * only way that can be accomplished is if a new hist trigger is
3027 * created and the field variable defined on that.
3029 * This function creates a new histogram compatible with the target
3030 * event (meaning a histogram with the same key as the target
3031 * histogram), and creates a variable for the specified field, but
3032 * with 'synthetic_' prepended to the variable name in order to avoid
3033 * collision with normal field variables.
3035 * Return: The variable created for the field.
3037 static struct hist_field *
3038 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3039 char *subsys_name, char *event_name, char *field_name)
3041 struct trace_array *tr = target_hist_data->event_file->tr;
3042 struct hist_field *event_var = ERR_PTR(-EINVAL);
3043 struct hist_trigger_data *hist_data;
3044 unsigned int i, n, first = true;
3045 struct field_var_hist *var_hist;
3046 struct trace_event_file *file;
3047 struct hist_field *key_field;
3052 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3053 hist_err_event("onmatch: Too many field variables defined: ",
3054 subsys_name, event_name, field_name);
3055 return ERR_PTR(-EINVAL);
3058 file = event_file(tr, subsys_name, event_name);
3061 hist_err_event("onmatch: Event file not found: ",
3062 subsys_name, event_name, field_name);
3063 ret = PTR_ERR(file);
3064 return ERR_PTR(ret);
3068 * Look for a histogram compatible with target. We'll use the
3069 * found histogram specification to create a new matching
3070 * histogram with our variable on it. target_hist_data is not
3071 * yet a registered histogram so we can't use that.
3073 hist_data = find_compatible_hist(target_hist_data, file);
3075 hist_err_event("onmatch: Matching event histogram not found: ",
3076 subsys_name, event_name, field_name);
3077 return ERR_PTR(-EINVAL);
3080 /* See if a synthetic field variable has already been created */
3081 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3082 event_name, field_name);
3083 if (!IS_ERR_OR_NULL(event_var))
3086 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3088 return ERR_PTR(-ENOMEM);
3090 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3093 return ERR_PTR(-ENOMEM);
3096 /* Use the same keys as the compatible histogram */
3097 strcat(cmd, "keys=");
3099 for_each_hist_key_field(i, hist_data) {
3100 key_field = hist_data->fields[i];
3103 strcat(cmd, key_field->field->name);
3107 /* Create the synthetic field variable specification */
3108 strcat(cmd, ":synthetic_");
3109 strcat(cmd, field_name);
3111 strcat(cmd, field_name);
3113 /* Use the same filter as the compatible histogram */
3114 saved_filter = find_trigger_filter(hist_data, file);
3116 strcat(cmd, " if ");
3117 strcat(cmd, saved_filter);
3120 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3121 if (!var_hist->cmd) {
3124 return ERR_PTR(-ENOMEM);
3127 /* Save the compatible histogram information */
3128 var_hist->hist_data = hist_data;
3130 /* Create the new histogram with our variable */
3131 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3135 kfree(var_hist->cmd);
3137 hist_err_event("onmatch: Couldn't create histogram for field: ",
3138 subsys_name, event_name, field_name);
3139 return ERR_PTR(ret);
3144 /* If we can't find the variable, something went wrong */
3145 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3146 event_name, field_name);
3147 if (IS_ERR_OR_NULL(event_var)) {
3148 kfree(var_hist->cmd);
3150 hist_err_event("onmatch: Couldn't find synthetic variable: ",
3151 subsys_name, event_name, field_name);
3152 return ERR_PTR(-EINVAL);
3155 n = target_hist_data->n_field_var_hists;
3156 target_hist_data->field_var_hists[n] = var_hist;
3157 target_hist_data->n_field_var_hists++;
3162 static struct hist_field *
3163 find_target_event_var(struct hist_trigger_data *hist_data,
3164 char *subsys_name, char *event_name, char *var_name)
3166 struct trace_event_file *file = hist_data->event_file;
3167 struct hist_field *hist_field = NULL;
3170 struct trace_event_call *call;
3175 call = file->event_call;
3177 if (strcmp(subsys_name, call->class->system) != 0)
3180 if (strcmp(event_name, trace_event_name(call)) != 0)
3184 hist_field = find_var_field(hist_data, var_name);
3189 static inline void __update_field_vars(struct tracing_map_elt *elt,
3190 struct ring_buffer_event *rbe,
3192 struct field_var **field_vars,
3193 unsigned int n_field_vars,
3194 unsigned int field_var_str_start)
3196 struct hist_elt_data *elt_data = elt->private_data;
3197 unsigned int i, j, var_idx;
3200 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3201 struct field_var *field_var = field_vars[i];
3202 struct hist_field *var = field_var->var;
3203 struct hist_field *val = field_var->val;
3205 var_val = val->fn(val, elt, rbe, rec);
3206 var_idx = var->var.idx;
3208 if (val->flags & HIST_FIELD_FL_STRING) {
3209 char *str = elt_data->field_var_str[j++];
3210 char *val_str = (char *)(uintptr_t)var_val;
3212 strscpy(str, val_str, STR_VAR_LEN_MAX);
3213 var_val = (u64)(uintptr_t)str;
3215 tracing_map_set_var(elt, var_idx, var_val);
3219 static void update_field_vars(struct hist_trigger_data *hist_data,
3220 struct tracing_map_elt *elt,
3221 struct ring_buffer_event *rbe,
3224 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3225 hist_data->n_field_vars, 0);
3228 static void update_max_vars(struct hist_trigger_data *hist_data,
3229 struct tracing_map_elt *elt,
3230 struct ring_buffer_event *rbe,
3233 __update_field_vars(elt, rbe, rec, hist_data->max_vars,
3234 hist_data->n_max_vars, hist_data->n_field_var_str);
3237 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3238 struct trace_event_file *file,
3239 char *name, int size, const char *type)
3241 struct hist_field *var;
3244 if (find_var(hist_data, file, name) && !hist_data->remove) {
3245 var = ERR_PTR(-EINVAL);
3249 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3251 var = ERR_PTR(-ENOMEM);
3255 idx = tracing_map_add_var(hist_data->map);
3258 var = ERR_PTR(-EINVAL);
3262 var->flags = HIST_FIELD_FL_VAR;
3264 var->var.hist_data = var->hist_data = hist_data;
3266 var->var.name = kstrdup(name, GFP_KERNEL);
3267 var->type = kstrdup(type, GFP_KERNEL);
3268 if (!var->var.name || !var->type) {
3269 kfree(var->var.name);
3272 var = ERR_PTR(-ENOMEM);
3278 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3279 struct trace_event_file *file,
3282 struct hist_field *val = NULL, *var = NULL;
3283 unsigned long flags = HIST_FIELD_FL_VAR;
3284 struct field_var *field_var;
3287 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3288 hist_err("Too many field variables defined: ", field_name);
3293 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3295 hist_err("Couldn't parse field variable: ", field_name);
3300 var = create_var(hist_data, file, field_name, val->size, val->type);
3302 hist_err("Couldn't create or find variable: ", field_name);
3308 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3316 field_var->var = var;
3317 field_var->val = val;
3321 field_var = ERR_PTR(ret);
3326 * create_target_field_var - Automatically create a variable for a field
3327 * @target_hist_data: The target hist trigger
3328 * @subsys_name: Optional subsystem name
3329 * @event_name: Optional event name
3330 * @var_name: The name of the field (and the resulting variable)
3332 * Hist trigger actions fetch data from variables, not directly from
3333 * events. However, for convenience, users are allowed to directly
3334 * specify an event field in an action, which will be automatically
3335 * converted into a variable on their behalf.
3337 * This function creates a field variable with the name var_name on
3338 * the hist trigger currently being defined on the target event. If
3339 * subsys_name and event_name are specified, this function simply
3340 * verifies that they do in fact match the target event subsystem and
3343 * Return: The variable created for the field.
3345 static struct field_var *
3346 create_target_field_var(struct hist_trigger_data *target_hist_data,
3347 char *subsys_name, char *event_name, char *var_name)
3349 struct trace_event_file *file = target_hist_data->event_file;
3352 struct trace_event_call *call;
3357 call = file->event_call;
3359 if (strcmp(subsys_name, call->class->system) != 0)
3362 if (strcmp(event_name, trace_event_name(call)) != 0)
3366 return create_field_var(target_hist_data, file, var_name);
3369 static void onmax_print(struct seq_file *m,
3370 struct hist_trigger_data *hist_data,
3371 struct tracing_map_elt *elt,
3372 struct action_data *data)
3374 unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
3376 seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
3378 for (i = 0; i < hist_data->n_max_vars; i++) {
3379 struct hist_field *save_val = hist_data->max_vars[i]->val;
3380 struct hist_field *save_var = hist_data->max_vars[i]->var;
3383 save_var_idx = save_var->var.idx;
3385 val = tracing_map_read_var(elt, save_var_idx);
3387 if (save_val->flags & HIST_FIELD_FL_STRING) {
3388 seq_printf(m, " %s: %-32s", save_var->var.name,
3389 (char *)(uintptr_t)(val));
3391 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3395 static void onmax_save(struct hist_trigger_data *hist_data,
3396 struct tracing_map_elt *elt, void *rec,
3397 struct ring_buffer_event *rbe,
3398 struct action_data *data, u64 *var_ref_vals)
3400 unsigned int max_idx = data->onmax.max_var->var.idx;
3401 unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
3403 u64 var_val, max_val;
3405 var_val = var_ref_vals[max_var_ref_idx];
3406 max_val = tracing_map_read_var(elt, max_idx);
3408 if (var_val <= max_val)
3411 tracing_map_set_var(elt, max_idx, var_val);
3413 update_max_vars(hist_data, elt, rbe, rec);
3416 static void onmax_destroy(struct action_data *data)
3420 destroy_hist_field(data->onmax.max_var, 0);
3421 destroy_hist_field(data->onmax.var, 0);
3423 kfree(data->onmax.var_str);
3424 kfree(data->onmax.fn_name);
3426 for (i = 0; i < data->n_params; i++)
3427 kfree(data->params[i]);
3432 static int onmax_create(struct hist_trigger_data *hist_data,
3433 struct action_data *data)
3435 struct trace_event_file *file = hist_data->event_file;
3436 struct hist_field *var_field, *ref_field, *max_var;
3437 unsigned int var_ref_idx = hist_data->n_var_refs;
3438 struct field_var *field_var;
3439 char *onmax_var_str, *param;
3443 onmax_var_str = data->onmax.var_str;
3444 if (onmax_var_str[0] != '$') {
3445 hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
3450 var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
3452 hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
3456 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3460 data->onmax.var = ref_field;
3462 data->fn = onmax_save;
3463 data->onmax.max_var_ref_idx = var_ref_idx;
3464 max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
3465 if (IS_ERR(max_var)) {
3466 hist_err("onmax: Couldn't create onmax variable: ", "max");
3467 ret = PTR_ERR(max_var);
3470 data->onmax.max_var = max_var;
3472 for (i = 0; i < data->n_params; i++) {
3473 param = kstrdup(data->params[i], GFP_KERNEL);
3479 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3480 if (IS_ERR(field_var)) {
3481 hist_err("onmax: Couldn't create field variable: ", param);
3482 ret = PTR_ERR(field_var);
3487 hist_data->max_vars[hist_data->n_max_vars++] = field_var;
3488 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3489 hist_data->n_max_var_str++;
3497 static int parse_action_params(char *params, struct action_data *data)
3499 char *param, *saved_param;
3503 if (data->n_params >= SYNTH_FIELDS_MAX)
3506 param = strsep(¶ms, ",");
3512 param = strstrip(param);
3513 if (strlen(param) < 2) {
3514 hist_err("Invalid action param: ", param);
3519 saved_param = kstrdup(param, GFP_KERNEL);
3525 data->params[data->n_params++] = saved_param;
3531 static struct action_data *onmax_parse(char *str)
3533 char *onmax_fn_name, *onmax_var_str;
3534 struct action_data *data;
3537 data = kzalloc(sizeof(*data), GFP_KERNEL);
3539 return ERR_PTR(-ENOMEM);
3541 onmax_var_str = strsep(&str, ")");
3542 if (!onmax_var_str || !str) {
3547 data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
3548 if (!data->onmax.var_str) {
3557 onmax_fn_name = strsep(&str, "(");
3558 if (!onmax_fn_name || !str)
3561 if (str_has_prefix(onmax_fn_name, "save")) {
3562 char *params = strsep(&str, ")");
3569 ret = parse_action_params(params, data);
3575 data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
3576 if (!data->onmax.fn_name) {
3583 onmax_destroy(data);
3584 data = ERR_PTR(ret);
3588 static void onmatch_destroy(struct action_data *data)
3592 lockdep_assert_held(&event_mutex);
3594 kfree(data->onmatch.match_event);
3595 kfree(data->onmatch.match_event_system);
3596 kfree(data->onmatch.synth_event_name);
3598 for (i = 0; i < data->n_params; i++)
3599 kfree(data->params[i]);
3601 if (data->onmatch.synth_event)
3602 data->onmatch.synth_event->ref--;
3607 static void destroy_field_var(struct field_var *field_var)
3612 destroy_hist_field(field_var->var, 0);
3613 destroy_hist_field(field_var->val, 0);
3618 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3622 for (i = 0; i < hist_data->n_field_vars; i++)
3623 destroy_field_var(hist_data->field_vars[i]);
3626 static void save_field_var(struct hist_trigger_data *hist_data,
3627 struct field_var *field_var)
3629 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3631 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3632 hist_data->n_field_var_str++;
3636 static int check_synth_field(struct synth_event *event,
3637 struct hist_field *hist_field,
3638 unsigned int field_pos)
3640 struct synth_field *field;
3642 if (field_pos >= event->n_fields)
3645 field = event->fields[field_pos];
3647 if (strcmp(field->type, hist_field->type) != 0)
3653 static struct hist_field *
3654 onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
3655 char *system, char *event, char *var)
3657 struct hist_field *hist_field;
3659 var++; /* skip '$' */
3661 hist_field = find_target_event_var(hist_data, system, event, var);
3664 system = data->onmatch.match_event_system;
3665 event = data->onmatch.match_event;
3668 hist_field = find_event_var(hist_data, system, event, var);
3672 hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
3677 static struct hist_field *
3678 onmatch_create_field_var(struct hist_trigger_data *hist_data,
3679 struct action_data *data, char *system,
3680 char *event, char *var)
3682 struct hist_field *hist_field = NULL;
3683 struct field_var *field_var;
3686 * First try to create a field var on the target event (the
3687 * currently being defined). This will create a variable for
3688 * unqualified fields on the target event, or if qualified,
3689 * target fields that have qualified names matching the target.
3691 field_var = create_target_field_var(hist_data, system, event, var);
3693 if (field_var && !IS_ERR(field_var)) {
3694 save_field_var(hist_data, field_var);
3695 hist_field = field_var->var;
3699 * If no explicit system.event is specfied, default to
3700 * looking for fields on the onmatch(system.event.xxx)
3704 system = data->onmatch.match_event_system;
3705 event = data->onmatch.match_event;
3709 * At this point, we're looking at a field on another
3710 * event. Because we can't modify a hist trigger on
3711 * another event to add a variable for a field, we need
3712 * to create a new trigger on that event and create the
3713 * variable at the same time.
3715 hist_field = create_field_var_hist(hist_data, system, event, var);
3716 if (IS_ERR(hist_field))
3722 destroy_field_var(field_var);
3727 static int onmatch_create(struct hist_trigger_data *hist_data,
3728 struct trace_event_file *file,
3729 struct action_data *data)
3731 char *event_name, *param, *system = NULL;
3732 struct hist_field *hist_field, *var_ref;
3733 unsigned int i, var_ref_idx;
3734 unsigned int field_pos = 0;
3735 struct synth_event *event;
3738 lockdep_assert_held(&event_mutex);
3740 event = find_synth_event(data->onmatch.synth_event_name);
3742 hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
3747 var_ref_idx = hist_data->n_var_refs;
3749 for (i = 0; i < data->n_params; i++) {
3752 p = param = kstrdup(data->params[i], GFP_KERNEL);
3758 system = strsep(¶m, ".");
3760 param = (char *)system;
3761 system = event_name = NULL;
3763 event_name = strsep(¶m, ".");
3771 if (param[0] == '$')
3772 hist_field = onmatch_find_var(hist_data, data, system,
3775 hist_field = onmatch_create_field_var(hist_data, data,
3786 if (check_synth_field(event, hist_field, field_pos) == 0) {
3787 var_ref = create_var_ref(hist_data, hist_field,
3788 system, event_name);
3800 hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
3801 system, event_name, param);
3807 if (field_pos != event->n_fields) {
3808 hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
3813 data->fn = action_trace;
3814 data->onmatch.synth_event = event;
3815 data->onmatch.var_ref_idx = var_ref_idx;
3824 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3826 char *match_event, *match_event_system;
3827 char *synth_event_name, *params;
3828 struct action_data *data;
3831 data = kzalloc(sizeof(*data), GFP_KERNEL);
3833 return ERR_PTR(-ENOMEM);
3835 match_event = strsep(&str, ")");
3836 if (!match_event || !str) {
3837 hist_err("onmatch: Missing closing paren: ", match_event);
3841 match_event_system = strsep(&match_event, ".");
3843 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
3847 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3848 hist_err_event("onmatch: Invalid subsystem or event name: ",
3849 match_event_system, match_event, NULL);
3853 data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
3854 if (!data->onmatch.match_event) {
3859 data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
3860 if (!data->onmatch.match_event_system) {
3867 hist_err("onmatch: Missing . after onmatch(): ", str);
3871 synth_event_name = strsep(&str, "(");
3872 if (!synth_event_name || !str) {
3873 hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
3877 data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
3878 if (!data->onmatch.synth_event_name) {
3883 params = strsep(&str, ")");
3884 if (!params || !str || (str && strlen(str))) {
3885 hist_err("onmatch: Missing closing paramlist paren: ", params);
3889 ret = parse_action_params(params, data);
3895 onmatch_destroy(data);
3896 data = ERR_PTR(ret);
3900 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3902 hist_data->fields[HITCOUNT_IDX] =
3903 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3904 if (!hist_data->fields[HITCOUNT_IDX])
3907 hist_data->n_vals++;
3908 hist_data->n_fields++;
3910 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3916 static int __create_val_field(struct hist_trigger_data *hist_data,
3917 unsigned int val_idx,
3918 struct trace_event_file *file,
3919 char *var_name, char *field_str,
3920 unsigned long flags)
3922 struct hist_field *hist_field;
3925 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3926 if (IS_ERR(hist_field)) {
3927 ret = PTR_ERR(hist_field);
3931 hist_data->fields[val_idx] = hist_field;
3933 ++hist_data->n_vals;
3934 ++hist_data->n_fields;
3936 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3942 static int create_val_field(struct hist_trigger_data *hist_data,
3943 unsigned int val_idx,
3944 struct trace_event_file *file,
3947 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3950 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3953 static int create_var_field(struct hist_trigger_data *hist_data,
3954 unsigned int val_idx,
3955 struct trace_event_file *file,
3956 char *var_name, char *expr_str)
3958 unsigned long flags = 0;
3960 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3963 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3964 hist_err("Variable already defined: ", var_name);
3968 flags |= HIST_FIELD_FL_VAR;
3969 hist_data->n_vars++;
3970 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3973 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3976 static int create_val_fields(struct hist_trigger_data *hist_data,
3977 struct trace_event_file *file)
3979 char *fields_str, *field_str;
3980 unsigned int i, j = 1;
3983 ret = create_hitcount_val(hist_data);
3987 fields_str = hist_data->attrs->vals_str;
3991 strsep(&fields_str, "=");
3995 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3996 j < TRACING_MAP_VALS_MAX; i++) {
3997 field_str = strsep(&fields_str, ",");
4001 if (strcmp(field_str, "hitcount") == 0)
4004 ret = create_val_field(hist_data, j++, file, field_str);
4009 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4015 static int create_key_field(struct hist_trigger_data *hist_data,
4016 unsigned int key_idx,
4017 unsigned int key_offset,
4018 struct trace_event_file *file,
4021 struct hist_field *hist_field = NULL;
4023 unsigned long flags = 0;
4024 unsigned int key_size;
4027 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4030 flags |= HIST_FIELD_FL_KEY;
4032 if (strcmp(field_str, "stacktrace") == 0) {
4033 flags |= HIST_FIELD_FL_STACKTRACE;
4034 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4035 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4037 hist_field = parse_expr(hist_data, file, field_str, flags,
4039 if (IS_ERR(hist_field)) {
4040 ret = PTR_ERR(hist_field);
4044 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
4045 hist_err("Using variable references as keys not supported: ", field_str);
4046 destroy_hist_field(hist_field, 0);
4051 key_size = hist_field->size;
4054 hist_data->fields[key_idx] = hist_field;
4056 key_size = ALIGN(key_size, sizeof(u64));
4057 hist_data->fields[key_idx]->size = key_size;
4058 hist_data->fields[key_idx]->offset = key_offset;
4060 hist_data->key_size += key_size;
4062 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4067 hist_data->n_keys++;
4068 hist_data->n_fields++;
4070 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4078 static int create_key_fields(struct hist_trigger_data *hist_data,
4079 struct trace_event_file *file)
4081 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4082 char *fields_str, *field_str;
4085 fields_str = hist_data->attrs->keys_str;
4089 strsep(&fields_str, "=");
4093 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4094 field_str = strsep(&fields_str, ",");
4097 ret = create_key_field(hist_data, i, key_offset,
4112 static int create_var_fields(struct hist_trigger_data *hist_data,
4113 struct trace_event_file *file)
4115 unsigned int i, j = hist_data->n_vals;
4118 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4120 for (i = 0; i < n_vars; i++) {
4121 char *var_name = hist_data->attrs->var_defs.name[i];
4122 char *expr = hist_data->attrs->var_defs.expr[i];
4124 ret = create_var_field(hist_data, j++, file, var_name, expr);
4132 static void free_var_defs(struct hist_trigger_data *hist_data)
4136 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4137 kfree(hist_data->attrs->var_defs.name[i]);
4138 kfree(hist_data->attrs->var_defs.expr[i]);
4141 hist_data->attrs->var_defs.n_vars = 0;
4144 static int parse_var_defs(struct hist_trigger_data *hist_data)
4146 char *s, *str, *var_name, *field_str;
4147 unsigned int i, j, n_vars = 0;
4150 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4151 str = hist_data->attrs->assignment_str[i];
4152 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4153 field_str = strsep(&str, ",");
4157 var_name = strsep(&field_str, "=");
4158 if (!var_name || !field_str) {
4159 hist_err("Malformed assignment: ", var_name);
4164 if (n_vars == TRACING_MAP_VARS_MAX) {
4165 hist_err("Too many variables defined: ", var_name);
4170 s = kstrdup(var_name, GFP_KERNEL);
4175 hist_data->attrs->var_defs.name[n_vars] = s;
4177 s = kstrdup(field_str, GFP_KERNEL);
4179 kfree(hist_data->attrs->var_defs.name[n_vars]);
4183 hist_data->attrs->var_defs.expr[n_vars++] = s;
4185 hist_data->attrs->var_defs.n_vars = n_vars;
4191 free_var_defs(hist_data);
4196 static int create_hist_fields(struct hist_trigger_data *hist_data,
4197 struct trace_event_file *file)
4201 ret = parse_var_defs(hist_data);
4205 ret = create_val_fields(hist_data, file);
4209 ret = create_var_fields(hist_data, file);
4213 ret = create_key_fields(hist_data, file);
4217 free_var_defs(hist_data);
4222 static int is_descending(const char *str)
4227 if (strcmp(str, "descending") == 0)
4230 if (strcmp(str, "ascending") == 0)
4236 static int create_sort_keys(struct hist_trigger_data *hist_data)
4238 char *fields_str = hist_data->attrs->sort_key_str;
4239 struct tracing_map_sort_key *sort_key;
4240 int descending, ret = 0;
4241 unsigned int i, j, k;
4243 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4248 strsep(&fields_str, "=");
4254 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4255 struct hist_field *hist_field;
4256 char *field_str, *field_name;
4257 const char *test_name;
4259 sort_key = &hist_data->sort_keys[i];
4261 field_str = strsep(&fields_str, ",");
4268 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4273 field_name = strsep(&field_str, ".");
4279 if (strcmp(field_name, "hitcount") == 0) {
4280 descending = is_descending(field_str);
4281 if (descending < 0) {
4285 sort_key->descending = descending;
4289 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4292 hist_field = hist_data->fields[j];
4293 if (hist_field->flags & HIST_FIELD_FL_VAR)
4298 test_name = hist_field_name(hist_field, 0);
4300 if (strcmp(field_name, test_name) == 0) {
4301 sort_key->field_idx = idx;
4302 descending = is_descending(field_str);
4303 if (descending < 0) {
4307 sort_key->descending = descending;
4311 if (j == hist_data->n_fields) {
4317 hist_data->n_sort_keys = i;
4322 static void destroy_actions(struct hist_trigger_data *hist_data)
4326 for (i = 0; i < hist_data->n_actions; i++) {
4327 struct action_data *data = hist_data->actions[i];
4329 if (data->fn == action_trace)
4330 onmatch_destroy(data);
4331 else if (data->fn == onmax_save)
4332 onmax_destroy(data);
4338 static int parse_actions(struct hist_trigger_data *hist_data)
4340 struct trace_array *tr = hist_data->event_file->tr;
4341 struct action_data *data;
4347 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4348 str = hist_data->attrs->action_str[i];
4350 if ((len = str_has_prefix(str, "onmatch("))) {
4351 char *action_str = str + len;
4353 data = onmatch_parse(tr, action_str);
4355 ret = PTR_ERR(data);
4358 data->fn = action_trace;
4359 } else if ((len = str_has_prefix(str, "onmax("))) {
4360 char *action_str = str + len;
4362 data = onmax_parse(action_str);
4364 ret = PTR_ERR(data);
4367 data->fn = onmax_save;
4373 hist_data->actions[hist_data->n_actions++] = data;
4379 static int create_actions(struct hist_trigger_data *hist_data,
4380 struct trace_event_file *file)
4382 struct action_data *data;
4386 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4387 data = hist_data->actions[i];
4389 if (data->fn == action_trace) {
4390 ret = onmatch_create(hist_data, file, data);
4393 } else if (data->fn == onmax_save) {
4394 ret = onmax_create(hist_data, data);
4403 static void print_actions(struct seq_file *m,
4404 struct hist_trigger_data *hist_data,
4405 struct tracing_map_elt *elt)
4409 for (i = 0; i < hist_data->n_actions; i++) {
4410 struct action_data *data = hist_data->actions[i];
4412 if (data->fn == onmax_save)
4413 onmax_print(m, hist_data, elt, data);
4417 static void print_onmax_spec(struct seq_file *m,
4418 struct hist_trigger_data *hist_data,
4419 struct action_data *data)
4423 seq_puts(m, ":onmax(");
4424 seq_printf(m, "%s", data->onmax.var_str);
4425 seq_printf(m, ").%s(", data->onmax.fn_name);
4427 for (i = 0; i < hist_data->n_max_vars; i++) {
4428 seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
4429 if (i < hist_data->n_max_vars - 1)
4435 static void print_onmatch_spec(struct seq_file *m,
4436 struct hist_trigger_data *hist_data,
4437 struct action_data *data)
4441 seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
4442 data->onmatch.match_event);
4444 seq_printf(m, "%s(", data->onmatch.synth_event->name);
4446 for (i = 0; i < data->n_params; i++) {
4449 seq_printf(m, "%s", data->params[i]);
4455 static bool actions_match(struct hist_trigger_data *hist_data,
4456 struct hist_trigger_data *hist_data_test)
4460 if (hist_data->n_actions != hist_data_test->n_actions)
4463 for (i = 0; i < hist_data->n_actions; i++) {
4464 struct action_data *data = hist_data->actions[i];
4465 struct action_data *data_test = hist_data_test->actions[i];
4467 if (data->fn != data_test->fn)
4470 if (data->n_params != data_test->n_params)
4473 for (j = 0; j < data->n_params; j++) {
4474 if (strcmp(data->params[j], data_test->params[j]) != 0)
4478 if (data->fn == action_trace) {
4479 if (strcmp(data->onmatch.synth_event_name,
4480 data_test->onmatch.synth_event_name) != 0)
4482 if (strcmp(data->onmatch.match_event_system,
4483 data_test->onmatch.match_event_system) != 0)
4485 if (strcmp(data->onmatch.match_event,
4486 data_test->onmatch.match_event) != 0)
4488 } else if (data->fn == onmax_save) {
4489 if (strcmp(data->onmax.var_str,
4490 data_test->onmax.var_str) != 0)
4492 if (strcmp(data->onmax.fn_name,
4493 data_test->onmax.fn_name) != 0)
4502 static void print_actions_spec(struct seq_file *m,
4503 struct hist_trigger_data *hist_data)
4507 for (i = 0; i < hist_data->n_actions; i++) {
4508 struct action_data *data = hist_data->actions[i];
4510 if (data->fn == action_trace)
4511 print_onmatch_spec(m, hist_data, data);
4512 else if (data->fn == onmax_save)
4513 print_onmax_spec(m, hist_data, data);
4517 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4521 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4522 kfree(hist_data->field_var_hists[i]->cmd);
4523 kfree(hist_data->field_var_hists[i]);
4527 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4532 destroy_hist_trigger_attrs(hist_data->attrs);
4533 destroy_hist_fields(hist_data);
4534 tracing_map_destroy(hist_data->map);
4536 destroy_actions(hist_data);
4537 destroy_field_vars(hist_data);
4538 destroy_field_var_hists(hist_data);
4543 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4545 struct tracing_map *map = hist_data->map;
4546 struct ftrace_event_field *field;
4547 struct hist_field *hist_field;
4550 for_each_hist_field(i, hist_data) {
4551 hist_field = hist_data->fields[i];
4552 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4553 tracing_map_cmp_fn_t cmp_fn;
4555 field = hist_field->field;
4557 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4558 cmp_fn = tracing_map_cmp_none;
4560 cmp_fn = tracing_map_cmp_num(hist_field->size,
4561 hist_field->is_signed);
4562 else if (is_string_field(field))
4563 cmp_fn = tracing_map_cmp_string;
4565 cmp_fn = tracing_map_cmp_num(field->size,
4567 idx = tracing_map_add_key_field(map,
4570 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4571 idx = tracing_map_add_sum_field(map);
4576 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4577 idx = tracing_map_add_var(map);
4580 hist_field->var.idx = idx;
4581 hist_field->var.hist_data = hist_data;
4588 static struct hist_trigger_data *
4589 create_hist_data(unsigned int map_bits,
4590 struct hist_trigger_attrs *attrs,
4591 struct trace_event_file *file,
4594 const struct tracing_map_ops *map_ops = NULL;
4595 struct hist_trigger_data *hist_data;
4598 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4600 return ERR_PTR(-ENOMEM);
4602 hist_data->attrs = attrs;
4603 hist_data->remove = remove;
4604 hist_data->event_file = file;
4606 ret = parse_actions(hist_data);
4610 ret = create_hist_fields(hist_data, file);
4614 ret = create_sort_keys(hist_data);
4618 map_ops = &hist_trigger_elt_data_ops;
4620 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4621 map_ops, hist_data);
4622 if (IS_ERR(hist_data->map)) {
4623 ret = PTR_ERR(hist_data->map);
4624 hist_data->map = NULL;
4628 ret = create_tracing_map_fields(hist_data);
4634 hist_data->attrs = NULL;
4636 destroy_hist_data(hist_data);
4638 hist_data = ERR_PTR(ret);
4643 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4644 struct tracing_map_elt *elt, void *rec,
4645 struct ring_buffer_event *rbe,
4648 struct hist_elt_data *elt_data;
4649 struct hist_field *hist_field;
4650 unsigned int i, var_idx;
4653 elt_data = elt->private_data;
4654 elt_data->var_ref_vals = var_ref_vals;
4656 for_each_hist_val_field(i, hist_data) {
4657 hist_field = hist_data->fields[i];
4658 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4659 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4660 var_idx = hist_field->var.idx;
4661 tracing_map_set_var(elt, var_idx, hist_val);
4664 tracing_map_update_sum(elt, i, hist_val);
4667 for_each_hist_key_field(i, hist_data) {
4668 hist_field = hist_data->fields[i];
4669 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4670 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4671 var_idx = hist_field->var.idx;
4672 tracing_map_set_var(elt, var_idx, hist_val);
4676 update_field_vars(hist_data, elt, rbe, rec);
4679 static inline void add_to_key(char *compound_key, void *key,
4680 struct hist_field *key_field, void *rec)
4682 size_t size = key_field->size;
4684 if (key_field->flags & HIST_FIELD_FL_STRING) {
4685 struct ftrace_event_field *field;
4687 field = key_field->field;
4688 if (field->filter_type == FILTER_DYN_STRING)
4689 size = *(u32 *)(rec + field->offset) >> 16;
4690 else if (field->filter_type == FILTER_PTR_STRING)
4692 else if (field->filter_type == FILTER_STATIC_STRING)
4695 /* ensure NULL-termination */
4696 if (size > key_field->size - 1)
4697 size = key_field->size - 1;
4700 memcpy(compound_key + key_field->offset, key, size);
4704 hist_trigger_actions(struct hist_trigger_data *hist_data,
4705 struct tracing_map_elt *elt, void *rec,
4706 struct ring_buffer_event *rbe, u64 *var_ref_vals)
4708 struct action_data *data;
4711 for (i = 0; i < hist_data->n_actions; i++) {
4712 data = hist_data->actions[i];
4713 data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
4717 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4718 struct ring_buffer_event *rbe)
4720 struct hist_trigger_data *hist_data = data->private_data;
4721 bool use_compound_key = (hist_data->n_keys > 1);
4722 unsigned long entries[HIST_STACKTRACE_DEPTH];
4723 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4724 char compound_key[HIST_KEY_SIZE_MAX];
4725 struct tracing_map_elt *elt = NULL;
4726 struct stack_trace stacktrace;
4727 struct hist_field *key_field;
4732 memset(compound_key, 0, hist_data->key_size);
4734 for_each_hist_key_field(i, hist_data) {
4735 key_field = hist_data->fields[i];
4737 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4738 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
4739 stacktrace.entries = entries;
4740 stacktrace.nr_entries = 0;
4741 stacktrace.skip = HIST_STACKTRACE_SKIP;
4743 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
4744 save_stack_trace(&stacktrace);
4748 field_contents = key_field->fn(key_field, elt, rbe, rec);
4749 if (key_field->flags & HIST_FIELD_FL_STRING) {
4750 key = (void *)(unsigned long)field_contents;
4751 use_compound_key = true;
4753 key = (void *)&field_contents;
4756 if (use_compound_key)
4757 add_to_key(compound_key, key, key_field, rec);
4760 if (use_compound_key)
4763 if (hist_data->n_var_refs &&
4764 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4767 elt = tracing_map_insert(hist_data->map, key);
4771 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4773 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4774 hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
4777 static void hist_trigger_stacktrace_print(struct seq_file *m,
4778 unsigned long *stacktrace_entries,
4779 unsigned int max_entries)
4781 char str[KSYM_SYMBOL_LEN];
4782 unsigned int spaces = 8;
4785 for (i = 0; i < max_entries; i++) {
4786 if (stacktrace_entries[i] == ULONG_MAX)
4789 seq_printf(m, "%*c", 1 + spaces, ' ');
4790 sprint_symbol(str, stacktrace_entries[i]);
4791 seq_printf(m, "%s\n", str);
4796 hist_trigger_entry_print(struct seq_file *m,
4797 struct hist_trigger_data *hist_data, void *key,
4798 struct tracing_map_elt *elt)
4800 struct hist_field *key_field;
4801 char str[KSYM_SYMBOL_LEN];
4802 bool multiline = false;
4803 const char *field_name;
4809 for_each_hist_key_field(i, hist_data) {
4810 key_field = hist_data->fields[i];
4812 if (i > hist_data->n_vals)
4815 field_name = hist_field_name(key_field, 0);
4817 if (key_field->flags & HIST_FIELD_FL_HEX) {
4818 uval = *(u64 *)(key + key_field->offset);
4819 seq_printf(m, "%s: %llx", field_name, uval);
4820 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4821 uval = *(u64 *)(key + key_field->offset);
4822 sprint_symbol_no_offset(str, uval);
4823 seq_printf(m, "%s: [%llx] %-45s", field_name,
4825 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4826 uval = *(u64 *)(key + key_field->offset);
4827 sprint_symbol(str, uval);
4828 seq_printf(m, "%s: [%llx] %-55s", field_name,
4830 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4831 struct hist_elt_data *elt_data = elt->private_data;
4834 if (WARN_ON_ONCE(!elt_data))
4837 comm = elt_data->comm;
4839 uval = *(u64 *)(key + key_field->offset);
4840 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4842 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4843 const char *syscall_name;
4845 uval = *(u64 *)(key + key_field->offset);
4846 syscall_name = get_syscall_name(uval);
4848 syscall_name = "unknown_syscall";
4850 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4851 syscall_name, uval);
4852 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4853 seq_puts(m, "stacktrace:\n");
4854 hist_trigger_stacktrace_print(m,
4855 key + key_field->offset,
4856 HIST_STACKTRACE_DEPTH);
4858 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4859 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4860 *(u64 *)(key + key_field->offset));
4861 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4862 seq_printf(m, "%s: %-50s", field_name,
4863 (char *)(key + key_field->offset));
4865 uval = *(u64 *)(key + key_field->offset);
4866 seq_printf(m, "%s: %10llu", field_name, uval);
4875 seq_printf(m, " hitcount: %10llu",
4876 tracing_map_read_sum(elt, HITCOUNT_IDX));
4878 for (i = 1; i < hist_data->n_vals; i++) {
4879 field_name = hist_field_name(hist_data->fields[i], 0);
4881 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4882 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4885 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4886 seq_printf(m, " %s: %10llx", field_name,
4887 tracing_map_read_sum(elt, i));
4889 seq_printf(m, " %s: %10llu", field_name,
4890 tracing_map_read_sum(elt, i));
4894 print_actions(m, hist_data, elt);
4899 static int print_entries(struct seq_file *m,
4900 struct hist_trigger_data *hist_data)
4902 struct tracing_map_sort_entry **sort_entries = NULL;
4903 struct tracing_map *map = hist_data->map;
4906 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4907 hist_data->n_sort_keys,
4912 for (i = 0; i < n_entries; i++)
4913 hist_trigger_entry_print(m, hist_data,
4914 sort_entries[i]->key,
4915 sort_entries[i]->elt);
4917 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4922 static void hist_trigger_show(struct seq_file *m,
4923 struct event_trigger_data *data, int n)
4925 struct hist_trigger_data *hist_data;
4929 seq_puts(m, "\n\n");
4931 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4932 data->ops->print(m, data->ops, data);
4933 seq_puts(m, "#\n\n");
4935 hist_data = data->private_data;
4936 n_entries = print_entries(m, hist_data);
4940 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4941 (u64)atomic64_read(&hist_data->map->hits),
4942 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4945 static int hist_show(struct seq_file *m, void *v)
4947 struct event_trigger_data *data;
4948 struct trace_event_file *event_file;
4951 mutex_lock(&event_mutex);
4953 event_file = event_file_data(m->private);
4954 if (unlikely(!event_file)) {
4959 list_for_each_entry_rcu(data, &event_file->triggers, list) {
4960 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4961 hist_trigger_show(m, data, n++);
4964 if (have_hist_err()) {
4965 seq_printf(m, "\nERROR: %s\n", hist_err_str);
4966 seq_printf(m, " Last command: %s\n", last_hist_cmd);
4970 mutex_unlock(&event_mutex);
4975 static int event_hist_open(struct inode *inode, struct file *file)
4977 return single_open(file, hist_show, file);
4980 const struct file_operations event_hist_fops = {
4981 .open = event_hist_open,
4983 .llseek = seq_lseek,
4984 .release = single_release,
4987 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
4989 const char *field_name = hist_field_name(hist_field, 0);
4991 if (hist_field->var.name)
4992 seq_printf(m, "%s=", hist_field->var.name);
4994 if (hist_field->flags & HIST_FIELD_FL_CPU)
4996 else if (field_name) {
4997 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
4998 hist_field->flags & HIST_FIELD_FL_ALIAS)
5000 seq_printf(m, "%s", field_name);
5001 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5002 seq_puts(m, "common_timestamp");
5004 if (hist_field->flags) {
5005 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5006 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5007 const char *flags = get_hist_field_flags(hist_field);
5010 seq_printf(m, ".%s", flags);
5015 static int event_hist_trigger_print(struct seq_file *m,
5016 struct event_trigger_ops *ops,
5017 struct event_trigger_data *data)
5019 struct hist_trigger_data *hist_data = data->private_data;
5020 struct hist_field *field;
5021 bool have_var = false;
5024 seq_puts(m, "hist:");
5027 seq_printf(m, "%s:", data->name);
5029 seq_puts(m, "keys=");
5031 for_each_hist_key_field(i, hist_data) {
5032 field = hist_data->fields[i];
5034 if (i > hist_data->n_vals)
5037 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5038 seq_puts(m, "stacktrace");
5040 hist_field_print(m, field);
5043 seq_puts(m, ":vals=");
5045 for_each_hist_val_field(i, hist_data) {
5046 field = hist_data->fields[i];
5047 if (field->flags & HIST_FIELD_FL_VAR) {
5052 if (i == HITCOUNT_IDX)
5053 seq_puts(m, "hitcount");
5056 hist_field_print(m, field);
5065 for_each_hist_val_field(i, hist_data) {
5066 field = hist_data->fields[i];
5068 if (field->flags & HIST_FIELD_FL_VAR) {
5071 hist_field_print(m, field);
5076 seq_puts(m, ":sort=");
5078 for (i = 0; i < hist_data->n_sort_keys; i++) {
5079 struct tracing_map_sort_key *sort_key;
5080 unsigned int idx, first_key_idx;
5083 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5085 sort_key = &hist_data->sort_keys[i];
5086 idx = sort_key->field_idx;
5088 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5094 if (idx == HITCOUNT_IDX)
5095 seq_puts(m, "hitcount");
5097 if (idx >= first_key_idx)
5098 idx += hist_data->n_vars;
5099 hist_field_print(m, hist_data->fields[idx]);
5102 if (sort_key->descending)
5103 seq_puts(m, ".descending");
5105 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5106 if (hist_data->enable_timestamps)
5107 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5109 print_actions_spec(m, hist_data);
5111 if (data->filter_str)
5112 seq_printf(m, " if %s", data->filter_str);
5115 seq_puts(m, " [paused]");
5117 seq_puts(m, " [active]");
5124 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5125 struct event_trigger_data *data)
5127 struct hist_trigger_data *hist_data = data->private_data;
5129 if (!data->ref && hist_data->attrs->name)
5130 save_named_trigger(hist_data->attrs->name, data);
5137 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5139 struct trace_event_file *file;
5144 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5145 file = hist_data->field_var_hists[i]->hist_data->event_file;
5146 cmd = hist_data->field_var_hists[i]->cmd;
5147 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5148 "!hist", "hist", cmd);
5152 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5153 struct event_trigger_data *data)
5155 struct hist_trigger_data *hist_data = data->private_data;
5157 if (WARN_ON_ONCE(data->ref <= 0))
5163 del_named_trigger(data);
5165 trigger_data_free(data);
5167 remove_hist_vars(hist_data);
5169 unregister_field_var_hists(hist_data);
5171 destroy_hist_data(hist_data);
5175 static struct event_trigger_ops event_hist_trigger_ops = {
5176 .func = event_hist_trigger,
5177 .print = event_hist_trigger_print,
5178 .init = event_hist_trigger_init,
5179 .free = event_hist_trigger_free,
5182 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5183 struct event_trigger_data *data)
5187 save_named_trigger(data->named_data->name, data);
5189 event_hist_trigger_init(ops, data->named_data);
5194 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5195 struct event_trigger_data *data)
5197 if (WARN_ON_ONCE(data->ref <= 0))
5200 event_hist_trigger_free(ops, data->named_data);
5204 del_named_trigger(data);
5205 trigger_data_free(data);
5209 static struct event_trigger_ops event_hist_trigger_named_ops = {
5210 .func = event_hist_trigger,
5211 .print = event_hist_trigger_print,
5212 .init = event_hist_trigger_named_init,
5213 .free = event_hist_trigger_named_free,
5216 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5219 return &event_hist_trigger_ops;
5222 static void hist_clear(struct event_trigger_data *data)
5224 struct hist_trigger_data *hist_data = data->private_data;
5227 pause_named_trigger(data);
5229 tracepoint_synchronize_unregister();
5231 tracing_map_clear(hist_data->map);
5234 unpause_named_trigger(data);
5237 static bool compatible_field(struct ftrace_event_field *field,
5238 struct ftrace_event_field *test_field)
5240 if (field == test_field)
5242 if (field == NULL || test_field == NULL)
5244 if (strcmp(field->name, test_field->name) != 0)
5246 if (strcmp(field->type, test_field->type) != 0)
5248 if (field->size != test_field->size)
5250 if (field->is_signed != test_field->is_signed)
5256 static bool hist_trigger_match(struct event_trigger_data *data,
5257 struct event_trigger_data *data_test,
5258 struct event_trigger_data *named_data,
5261 struct tracing_map_sort_key *sort_key, *sort_key_test;
5262 struct hist_trigger_data *hist_data, *hist_data_test;
5263 struct hist_field *key_field, *key_field_test;
5266 if (named_data && (named_data != data_test) &&
5267 (named_data != data_test->named_data))
5270 if (!named_data && is_named_trigger(data_test))
5273 hist_data = data->private_data;
5274 hist_data_test = data_test->private_data;
5276 if (hist_data->n_vals != hist_data_test->n_vals ||
5277 hist_data->n_fields != hist_data_test->n_fields ||
5278 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5281 if (!ignore_filter) {
5282 if ((data->filter_str && !data_test->filter_str) ||
5283 (!data->filter_str && data_test->filter_str))
5287 for_each_hist_field(i, hist_data) {
5288 key_field = hist_data->fields[i];
5289 key_field_test = hist_data_test->fields[i];
5291 if (key_field->flags != key_field_test->flags)
5293 if (!compatible_field(key_field->field, key_field_test->field))
5295 if (key_field->offset != key_field_test->offset)
5297 if (key_field->size != key_field_test->size)
5299 if (key_field->is_signed != key_field_test->is_signed)
5301 if (!!key_field->var.name != !!key_field_test->var.name)
5303 if (key_field->var.name &&
5304 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5308 for (i = 0; i < hist_data->n_sort_keys; i++) {
5309 sort_key = &hist_data->sort_keys[i];
5310 sort_key_test = &hist_data_test->sort_keys[i];
5312 if (sort_key->field_idx != sort_key_test->field_idx ||
5313 sort_key->descending != sort_key_test->descending)
5317 if (!ignore_filter && data->filter_str &&
5318 (strcmp(data->filter_str, data_test->filter_str) != 0))
5321 if (!actions_match(hist_data, hist_data_test))
5327 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5328 struct event_trigger_data *data,
5329 struct trace_event_file *file)
5331 struct hist_trigger_data *hist_data = data->private_data;
5332 struct event_trigger_data *test, *named_data = NULL;
5335 if (hist_data->attrs->name) {
5336 named_data = find_named_trigger(hist_data->attrs->name);
5338 if (!hist_trigger_match(data, named_data, named_data,
5340 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
5347 if (hist_data->attrs->name && !named_data)
5350 list_for_each_entry_rcu(test, &file->triggers, list) {
5351 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5352 if (!hist_trigger_match(data, test, named_data, false))
5354 if (hist_data->attrs->pause)
5355 test->paused = true;
5356 else if (hist_data->attrs->cont)
5357 test->paused = false;
5358 else if (hist_data->attrs->clear)
5361 hist_err("Hist trigger already exists", NULL);
5368 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5369 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
5374 if (hist_data->attrs->pause)
5375 data->paused = true;
5378 data->private_data = named_data->private_data;
5379 set_named_trigger_data(data, named_data);
5380 data->ops = &event_hist_trigger_named_ops;
5383 if (data->ops->init) {
5384 ret = data->ops->init(data->ops, data);
5389 if (hist_data->enable_timestamps) {
5390 char *clock = hist_data->attrs->clock;
5392 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5394 hist_err("Couldn't set trace_clock: ", clock);
5398 tracing_set_time_stamp_abs(file->tr, true);
5402 destroy_hist_data(hist_data);
5409 static int hist_trigger_enable(struct event_trigger_data *data,
5410 struct trace_event_file *file)
5414 list_add_tail_rcu(&data->list, &file->triggers);
5416 update_cond_flag(file);
5418 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5419 list_del_rcu(&data->list);
5420 update_cond_flag(file);
5427 static bool have_hist_trigger_match(struct event_trigger_data *data,
5428 struct trace_event_file *file)
5430 struct hist_trigger_data *hist_data = data->private_data;
5431 struct event_trigger_data *test, *named_data = NULL;
5434 if (hist_data->attrs->name)
5435 named_data = find_named_trigger(hist_data->attrs->name);
5437 list_for_each_entry_rcu(test, &file->triggers, list) {
5438 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5439 if (hist_trigger_match(data, test, named_data, false)) {
5449 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5450 struct trace_event_file *file)
5452 struct hist_trigger_data *hist_data = data->private_data;
5453 struct event_trigger_data *test, *named_data = NULL;
5455 if (hist_data->attrs->name)
5456 named_data = find_named_trigger(hist_data->attrs->name);
5458 list_for_each_entry_rcu(test, &file->triggers, list) {
5459 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5460 if (!hist_trigger_match(data, test, named_data, false))
5462 hist_data = test->private_data;
5463 if (check_var_refs(hist_data))
5472 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5473 struct event_trigger_data *data,
5474 struct trace_event_file *file)
5476 struct hist_trigger_data *hist_data = data->private_data;
5477 struct event_trigger_data *test, *named_data = NULL;
5478 bool unregistered = false;
5480 if (hist_data->attrs->name)
5481 named_data = find_named_trigger(hist_data->attrs->name);
5483 list_for_each_entry_rcu(test, &file->triggers, list) {
5484 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5485 if (!hist_trigger_match(data, test, named_data, false))
5487 unregistered = true;
5488 list_del_rcu(&test->list);
5489 trace_event_trigger_enable_disable(file, 0);
5490 update_cond_flag(file);
5495 if (unregistered && test->ops->free)
5496 test->ops->free(test->ops, test);
5498 if (hist_data->enable_timestamps) {
5499 if (!hist_data->remove || unregistered)
5500 tracing_set_time_stamp_abs(file->tr, false);
5504 static bool hist_file_check_refs(struct trace_event_file *file)
5506 struct hist_trigger_data *hist_data;
5507 struct event_trigger_data *test;
5509 list_for_each_entry_rcu(test, &file->triggers, list) {
5510 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5511 hist_data = test->private_data;
5512 if (check_var_refs(hist_data))
5520 static void hist_unreg_all(struct trace_event_file *file)
5522 struct event_trigger_data *test, *n;
5523 struct hist_trigger_data *hist_data;
5524 struct synth_event *se;
5525 const char *se_name;
5527 lockdep_assert_held(&event_mutex);
5529 if (hist_file_check_refs(file))
5532 list_for_each_entry_safe(test, n, &file->triggers, list) {
5533 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5534 hist_data = test->private_data;
5535 list_del_rcu(&test->list);
5536 trace_event_trigger_enable_disable(file, 0);
5538 se_name = trace_event_name(file->event_call);
5539 se = find_synth_event(se_name);
5543 update_cond_flag(file);
5544 if (hist_data->enable_timestamps)
5545 tracing_set_time_stamp_abs(file->tr, false);
5546 if (test->ops->free)
5547 test->ops->free(test->ops, test);
5552 static int event_hist_trigger_func(struct event_command *cmd_ops,
5553 struct trace_event_file *file,
5554 char *glob, char *cmd, char *param)
5556 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5557 struct event_trigger_data *trigger_data;
5558 struct hist_trigger_attrs *attrs;
5559 struct event_trigger_ops *trigger_ops;
5560 struct hist_trigger_data *hist_data;
5561 struct synth_event *se;
5562 const char *se_name;
5563 bool remove = false;
5567 lockdep_assert_held(&event_mutex);
5569 if (glob && strlen(glob)) {
5570 last_cmd_set(param);
5581 * separate the trigger from the filter (k:v [if filter])
5582 * allowing for whitespace in the trigger
5584 p = trigger = param;
5586 p = strstr(p, "if");
5591 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5595 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
5597 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
5608 param = strstrip(p);
5609 trigger = strstrip(trigger);
5612 attrs = parse_hist_trigger_attrs(trigger);
5614 return PTR_ERR(attrs);
5616 if (attrs->map_bits)
5617 hist_trigger_bits = attrs->map_bits;
5619 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5620 if (IS_ERR(hist_data)) {
5621 destroy_hist_trigger_attrs(attrs);
5622 return PTR_ERR(hist_data);
5625 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5627 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5628 if (!trigger_data) {
5633 trigger_data->count = -1;
5634 trigger_data->ops = trigger_ops;
5635 trigger_data->cmd_ops = cmd_ops;
5637 INIT_LIST_HEAD(&trigger_data->list);
5638 RCU_INIT_POINTER(trigger_data->filter, NULL);
5640 trigger_data->private_data = hist_data;
5642 /* if param is non-empty, it's supposed to be a filter */
5643 if (param && cmd_ops->set_filter) {
5644 ret = cmd_ops->set_filter(param, trigger_data, file);
5650 if (!have_hist_trigger_match(trigger_data, file))
5653 if (hist_trigger_check_refs(trigger_data, file)) {
5658 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5659 se_name = trace_event_name(file->event_call);
5660 se = find_synth_event(se_name);
5667 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5669 * The above returns on success the # of triggers registered,
5670 * but if it didn't register any it returns zero. Consider no
5671 * triggers registered a failure too.
5674 if (!(attrs->pause || attrs->cont || attrs->clear))
5680 if (get_named_trigger_data(trigger_data))
5683 if (has_hist_vars(hist_data))
5684 save_hist_vars(hist_data);
5686 ret = create_actions(hist_data, file);
5690 ret = tracing_map_init(hist_data->map);
5694 ret = hist_trigger_enable(trigger_data, file);
5698 se_name = trace_event_name(file->event_call);
5699 se = find_synth_event(se_name);
5702 /* Just return zero, not the number of registered triggers */
5710 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5712 if (cmd_ops->set_filter)
5713 cmd_ops->set_filter(NULL, trigger_data, NULL);
5715 remove_hist_vars(hist_data);
5717 kfree(trigger_data);
5719 destroy_hist_data(hist_data);
5723 static struct event_command trigger_hist_cmd = {
5725 .trigger_type = ETT_EVENT_HIST,
5726 .flags = EVENT_CMD_FL_NEEDS_REC,
5727 .func = event_hist_trigger_func,
5728 .reg = hist_register_trigger,
5729 .unreg = hist_unregister_trigger,
5730 .unreg_all = hist_unreg_all,
5731 .get_trigger_ops = event_hist_get_trigger_ops,
5732 .set_filter = set_trigger_filter,
5735 __init int register_trigger_hist_cmd(void)
5739 ret = register_event_command(&trigger_hist_cmd);
5746 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5747 struct ring_buffer_event *event)
5749 struct enable_trigger_data *enable_data = data->private_data;
5750 struct event_trigger_data *test;
5752 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
5753 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5754 if (enable_data->enable)
5755 test->paused = false;
5757 test->paused = true;
5763 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5764 struct ring_buffer_event *event)
5769 if (data->count != -1)
5772 hist_enable_trigger(data, rec, event);
5775 static struct event_trigger_ops hist_enable_trigger_ops = {
5776 .func = hist_enable_trigger,
5777 .print = event_enable_trigger_print,
5778 .init = event_trigger_init,
5779 .free = event_enable_trigger_free,
5782 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5783 .func = hist_enable_count_trigger,
5784 .print = event_enable_trigger_print,
5785 .init = event_trigger_init,
5786 .free = event_enable_trigger_free,
5789 static struct event_trigger_ops hist_disable_trigger_ops = {
5790 .func = hist_enable_trigger,
5791 .print = event_enable_trigger_print,
5792 .init = event_trigger_init,
5793 .free = event_enable_trigger_free,
5796 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5797 .func = hist_enable_count_trigger,
5798 .print = event_enable_trigger_print,
5799 .init = event_trigger_init,
5800 .free = event_enable_trigger_free,
5803 static struct event_trigger_ops *
5804 hist_enable_get_trigger_ops(char *cmd, char *param)
5806 struct event_trigger_ops *ops;
5809 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5812 ops = param ? &hist_enable_count_trigger_ops :
5813 &hist_enable_trigger_ops;
5815 ops = param ? &hist_disable_count_trigger_ops :
5816 &hist_disable_trigger_ops;
5821 static void hist_enable_unreg_all(struct trace_event_file *file)
5823 struct event_trigger_data *test, *n;
5825 list_for_each_entry_safe(test, n, &file->triggers, list) {
5826 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5827 list_del_rcu(&test->list);
5828 update_cond_flag(file);
5829 trace_event_trigger_enable_disable(file, 0);
5830 if (test->ops->free)
5831 test->ops->free(test->ops, test);
5836 static struct event_command trigger_hist_enable_cmd = {
5837 .name = ENABLE_HIST_STR,
5838 .trigger_type = ETT_HIST_ENABLE,
5839 .func = event_enable_trigger_func,
5840 .reg = event_enable_register_trigger,
5841 .unreg = event_enable_unregister_trigger,
5842 .unreg_all = hist_enable_unreg_all,
5843 .get_trigger_ops = hist_enable_get_trigger_ops,
5844 .set_filter = set_trigger_filter,
5847 static struct event_command trigger_hist_disable_cmd = {
5848 .name = DISABLE_HIST_STR,
5849 .trigger_type = ETT_HIST_ENABLE,
5850 .func = event_enable_trigger_func,
5851 .reg = event_enable_register_trigger,
5852 .unreg = event_enable_unregister_trigger,
5853 .unreg_all = hist_enable_unreg_all,
5854 .get_trigger_ops = hist_enable_get_trigger_ops,
5855 .set_filter = set_trigger_filter,
5858 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5860 unregister_event_command(&trigger_hist_enable_cmd);
5861 unregister_event_command(&trigger_hist_disable_cmd);
5864 __init int register_trigger_hist_enable_disable_cmds(void)
5868 ret = register_event_command(&trigger_hist_enable_cmd);
5869 if (WARN_ON(ret < 0))
5871 ret = register_event_command(&trigger_hist_disable_cmd);
5872 if (WARN_ON(ret < 0))
5873 unregister_trigger_hist_enable_disable_cmds();
5878 static __init int trace_events_hist_init(void)
5880 struct dentry *entry = NULL;
5881 struct dentry *d_tracer;
5884 err = dyn_event_register(&synth_event_ops);
5886 pr_warn("Could not register synth_event_ops\n");
5890 d_tracer = tracing_init_dentry();
5891 if (IS_ERR(d_tracer)) {
5892 err = PTR_ERR(d_tracer);
5896 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
5897 NULL, &synth_events_fops);
5905 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
5910 fs_initcall(trace_events_hist_init);