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"
19 #define SYNTH_SYSTEM "synthetic"
20 #define SYNTH_FIELDS_MAX 16
22 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
26 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
27 struct tracing_map_elt *elt,
28 struct ring_buffer_event *rbe,
31 #define HIST_FIELD_OPERANDS_MAX 2
32 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
33 #define HIST_ACTIONS_MAX 8
44 struct hist_trigger_data *hist_data;
49 struct ftrace_event_field *field;
54 unsigned int is_signed;
56 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
57 struct hist_trigger_data *hist_data;
59 enum field_op_id operator;
64 unsigned int var_ref_idx;
68 static u64 hist_field_none(struct hist_field *field,
69 struct tracing_map_elt *elt,
70 struct ring_buffer_event *rbe,
76 static u64 hist_field_counter(struct hist_field *field,
77 struct tracing_map_elt *elt,
78 struct ring_buffer_event *rbe,
84 static u64 hist_field_string(struct hist_field *hist_field,
85 struct tracing_map_elt *elt,
86 struct ring_buffer_event *rbe,
89 char *addr = (char *)(event + hist_field->field->offset);
91 return (u64)(unsigned long)addr;
94 static u64 hist_field_dynstring(struct hist_field *hist_field,
95 struct tracing_map_elt *elt,
96 struct ring_buffer_event *rbe,
99 u32 str_item = *(u32 *)(event + hist_field->field->offset);
100 int str_loc = str_item & 0xffff;
101 char *addr = (char *)(event + str_loc);
103 return (u64)(unsigned long)addr;
106 static u64 hist_field_pstring(struct hist_field *hist_field,
107 struct tracing_map_elt *elt,
108 struct ring_buffer_event *rbe,
111 char **addr = (char **)(event + hist_field->field->offset);
113 return (u64)(unsigned long)*addr;
116 static u64 hist_field_log2(struct hist_field *hist_field,
117 struct tracing_map_elt *elt,
118 struct ring_buffer_event *rbe,
121 struct hist_field *operand = hist_field->operands[0];
123 u64 val = operand->fn(operand, elt, rbe, event);
125 return (u64) ilog2(roundup_pow_of_two(val));
128 static u64 hist_field_plus(struct hist_field *hist_field,
129 struct tracing_map_elt *elt,
130 struct ring_buffer_event *rbe,
133 struct hist_field *operand1 = hist_field->operands[0];
134 struct hist_field *operand2 = hist_field->operands[1];
136 u64 val1 = operand1->fn(operand1, elt, rbe, event);
137 u64 val2 = operand2->fn(operand2, elt, rbe, event);
142 static u64 hist_field_minus(struct hist_field *hist_field,
143 struct tracing_map_elt *elt,
144 struct ring_buffer_event *rbe,
147 struct hist_field *operand1 = hist_field->operands[0];
148 struct hist_field *operand2 = hist_field->operands[1];
150 u64 val1 = operand1->fn(operand1, elt, rbe, event);
151 u64 val2 = operand2->fn(operand2, elt, rbe, event);
156 static u64 hist_field_unary_minus(struct hist_field *hist_field,
157 struct tracing_map_elt *elt,
158 struct ring_buffer_event *rbe,
161 struct hist_field *operand = hist_field->operands[0];
163 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
164 u64 val = (u64)-sval;
169 #define DEFINE_HIST_FIELD_FN(type) \
170 static u64 hist_field_##type(struct hist_field *hist_field, \
171 struct tracing_map_elt *elt, \
172 struct ring_buffer_event *rbe, \
175 type *addr = (type *)(event + hist_field->field->offset); \
177 return (u64)(unsigned long)*addr; \
180 DEFINE_HIST_FIELD_FN(s64);
181 DEFINE_HIST_FIELD_FN(u64);
182 DEFINE_HIST_FIELD_FN(s32);
183 DEFINE_HIST_FIELD_FN(u32);
184 DEFINE_HIST_FIELD_FN(s16);
185 DEFINE_HIST_FIELD_FN(u16);
186 DEFINE_HIST_FIELD_FN(s8);
187 DEFINE_HIST_FIELD_FN(u8);
189 #define for_each_hist_field(i, hist_data) \
190 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
192 #define for_each_hist_val_field(i, hist_data) \
193 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
195 #define for_each_hist_key_field(i, hist_data) \
196 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
198 #define HIST_STACKTRACE_DEPTH 16
199 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
200 #define HIST_STACKTRACE_SKIP 5
202 #define HITCOUNT_IDX 0
203 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
205 enum hist_field_flags {
206 HIST_FIELD_FL_HITCOUNT = 1 << 0,
207 HIST_FIELD_FL_KEY = 1 << 1,
208 HIST_FIELD_FL_STRING = 1 << 2,
209 HIST_FIELD_FL_HEX = 1 << 3,
210 HIST_FIELD_FL_SYM = 1 << 4,
211 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
212 HIST_FIELD_FL_EXECNAME = 1 << 6,
213 HIST_FIELD_FL_SYSCALL = 1 << 7,
214 HIST_FIELD_FL_STACKTRACE = 1 << 8,
215 HIST_FIELD_FL_LOG2 = 1 << 9,
216 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
217 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
218 HIST_FIELD_FL_VAR = 1 << 12,
219 HIST_FIELD_FL_EXPR = 1 << 13,
220 HIST_FIELD_FL_VAR_REF = 1 << 14,
221 HIST_FIELD_FL_CPU = 1 << 15,
222 HIST_FIELD_FL_ALIAS = 1 << 16,
227 char *name[TRACING_MAP_VARS_MAX];
228 char *expr[TRACING_MAP_VARS_MAX];
231 struct hist_trigger_attrs {
241 unsigned int map_bits;
243 char *assignment_str[TRACING_MAP_VARS_MAX];
244 unsigned int n_assignments;
246 char *action_str[HIST_ACTIONS_MAX];
247 unsigned int n_actions;
249 struct var_defs var_defs;
253 struct hist_field *var;
254 struct hist_field *val;
257 struct field_var_hist {
258 struct hist_trigger_data *hist_data;
262 struct hist_trigger_data {
263 struct hist_field *fields[HIST_FIELDS_MAX];
266 unsigned int n_fields;
268 unsigned int key_size;
269 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
270 unsigned int n_sort_keys;
271 struct trace_event_file *event_file;
272 struct hist_trigger_attrs *attrs;
273 struct tracing_map *map;
274 bool enable_timestamps;
276 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
277 unsigned int n_var_refs;
279 struct action_data *actions[HIST_ACTIONS_MAX];
280 unsigned int n_actions;
282 struct hist_field *synth_var_refs[SYNTH_FIELDS_MAX];
283 unsigned int n_synth_var_refs;
284 struct field_var *field_vars[SYNTH_FIELDS_MAX];
285 unsigned int n_field_vars;
286 unsigned int n_field_var_str;
287 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
288 unsigned int n_field_var_hists;
290 struct field_var *max_vars[SYNTH_FIELDS_MAX];
291 unsigned int n_max_vars;
292 unsigned int n_max_var_str;
304 struct list_head list;
307 struct synth_field **fields;
308 unsigned int n_fields;
310 struct trace_event_class class;
311 struct trace_event_call call;
312 struct tracepoint *tp;
317 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
318 struct tracing_map_elt *elt, void *rec,
319 struct ring_buffer_event *rbe,
320 struct action_data *data, u64 *var_ref_vals);
324 unsigned int n_params;
325 char *params[SYNTH_FIELDS_MAX];
329 unsigned int var_ref_idx;
331 char *match_event_system;
332 char *synth_event_name;
333 struct synth_event *synth_event;
339 unsigned int max_var_ref_idx;
340 struct hist_field *max_var;
341 struct hist_field *var;
347 static char last_hist_cmd[MAX_FILTER_STR_VAL];
348 static char hist_err_str[MAX_FILTER_STR_VAL];
350 static void last_cmd_set(char *str)
355 strncpy(last_hist_cmd, str, MAX_FILTER_STR_VAL - 1);
358 static void hist_err(char *str, char *var)
360 int maxlen = MAX_FILTER_STR_VAL - 1;
365 if (strlen(hist_err_str))
371 if (strlen(hist_err_str) + strlen(str) + strlen(var) > maxlen)
374 strcat(hist_err_str, str);
375 strcat(hist_err_str, var);
378 static void hist_err_event(char *str, char *system, char *event, char *var)
380 char err[MAX_FILTER_STR_VAL];
383 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s.%s", system, event, var);
385 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
387 strscpy(err, var, MAX_FILTER_STR_VAL);
392 static void hist_err_clear(void)
394 hist_err_str[0] = '\0';
397 static bool have_hist_err(void)
399 if (strlen(hist_err_str))
405 static LIST_HEAD(synth_event_list);
406 static DEFINE_MUTEX(synth_event_mutex);
408 struct synth_trace_event {
409 struct trace_entry ent;
413 static int synth_event_define_fields(struct trace_event_call *call)
415 struct synth_trace_event trace;
416 int offset = offsetof(typeof(trace), fields);
417 struct synth_event *event = call->data;
418 unsigned int i, size, n_u64;
423 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
424 size = event->fields[i]->size;
425 is_signed = event->fields[i]->is_signed;
426 type = event->fields[i]->type;
427 name = event->fields[i]->name;
428 ret = trace_define_field(call, type, name, offset, size,
429 is_signed, FILTER_OTHER);
433 if (event->fields[i]->is_string) {
434 offset += STR_VAR_LEN_MAX;
435 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
437 offset += sizeof(u64);
442 event->n_u64 = n_u64;
447 static bool synth_field_signed(char *type)
449 if (strncmp(type, "u", 1) == 0)
455 static int synth_field_is_string(char *type)
457 if (strstr(type, "char[") != NULL)
463 static int synth_field_string_size(char *type)
465 char buf[4], *end, *start;
469 start = strstr(type, "char[");
472 start += strlen("char[");
474 end = strchr(type, ']');
475 if (!end || end < start)
482 strncpy(buf, start, len);
485 err = kstrtouint(buf, 0, &size);
489 if (size > STR_VAR_LEN_MAX)
495 static int synth_field_size(char *type)
499 if (strcmp(type, "s64") == 0)
501 else if (strcmp(type, "u64") == 0)
503 else if (strcmp(type, "s32") == 0)
505 else if (strcmp(type, "u32") == 0)
507 else if (strcmp(type, "s16") == 0)
509 else if (strcmp(type, "u16") == 0)
511 else if (strcmp(type, "s8") == 0)
513 else if (strcmp(type, "u8") == 0)
515 else if (strcmp(type, "char") == 0)
517 else if (strcmp(type, "unsigned char") == 0)
518 size = sizeof(unsigned char);
519 else if (strcmp(type, "int") == 0)
521 else if (strcmp(type, "unsigned int") == 0)
522 size = sizeof(unsigned int);
523 else if (strcmp(type, "long") == 0)
525 else if (strcmp(type, "unsigned long") == 0)
526 size = sizeof(unsigned long);
527 else if (strcmp(type, "pid_t") == 0)
528 size = sizeof(pid_t);
529 else if (synth_field_is_string(type))
530 size = synth_field_string_size(type);
535 static const char *synth_field_fmt(char *type)
537 const char *fmt = "%llu";
539 if (strcmp(type, "s64") == 0)
541 else if (strcmp(type, "u64") == 0)
543 else if (strcmp(type, "s32") == 0)
545 else if (strcmp(type, "u32") == 0)
547 else if (strcmp(type, "s16") == 0)
549 else if (strcmp(type, "u16") == 0)
551 else if (strcmp(type, "s8") == 0)
553 else if (strcmp(type, "u8") == 0)
555 else if (strcmp(type, "char") == 0)
557 else if (strcmp(type, "unsigned char") == 0)
559 else if (strcmp(type, "int") == 0)
561 else if (strcmp(type, "unsigned int") == 0)
563 else if (strcmp(type, "long") == 0)
565 else if (strcmp(type, "unsigned long") == 0)
567 else if (strcmp(type, "pid_t") == 0)
569 else if (synth_field_is_string(type))
575 static enum print_line_t print_synth_event(struct trace_iterator *iter,
577 struct trace_event *event)
579 struct trace_array *tr = iter->tr;
580 struct trace_seq *s = &iter->seq;
581 struct synth_trace_event *entry;
582 struct synth_event *se;
583 unsigned int i, n_u64;
587 entry = (struct synth_trace_event *)iter->ent;
588 se = container_of(event, struct synth_event, call.event);
590 trace_seq_printf(s, "%s: ", se->name);
592 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
593 if (trace_seq_has_overflowed(s))
596 fmt = synth_field_fmt(se->fields[i]->type);
598 /* parameter types */
599 if (tr->trace_flags & TRACE_ITER_VERBOSE)
600 trace_seq_printf(s, "%s ", fmt);
602 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
604 /* parameter values */
605 if (se->fields[i]->is_string) {
606 trace_seq_printf(s, print_fmt, se->fields[i]->name,
607 (char *)&entry->fields[n_u64],
608 i == se->n_fields - 1 ? "" : " ");
609 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
611 trace_seq_printf(s, print_fmt, se->fields[i]->name,
612 entry->fields[n_u64],
613 i == se->n_fields - 1 ? "" : " ");
618 trace_seq_putc(s, '\n');
620 return trace_handle_return(s);
623 static struct trace_event_functions synth_event_funcs = {
624 .trace = print_synth_event
627 static notrace void trace_event_raw_event_synth(void *__data,
629 unsigned int var_ref_idx)
631 struct trace_event_file *trace_file = __data;
632 struct synth_trace_event *entry;
633 struct trace_event_buffer fbuffer;
634 struct ring_buffer *buffer;
635 struct synth_event *event;
636 unsigned int i, n_u64;
639 event = trace_file->event_call->data;
641 if (trace_trigger_soft_disabled(trace_file))
644 fields_size = event->n_u64 * sizeof(u64);
647 * Avoid ring buffer recursion detection, as this event
648 * is being performed within another event.
650 buffer = trace_file->tr->trace_buffer.buffer;
651 ring_buffer_nest_start(buffer);
653 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
654 sizeof(*entry) + fields_size);
658 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
659 if (event->fields[i]->is_string) {
660 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
661 char *str_field = (char *)&entry->fields[n_u64];
663 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
664 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
666 entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
671 trace_event_buffer_commit(&fbuffer);
673 ring_buffer_nest_end(buffer);
676 static void free_synth_event_print_fmt(struct trace_event_call *call)
679 kfree(call->print_fmt);
680 call->print_fmt = NULL;
684 static int __set_synth_event_print_fmt(struct synth_event *event,
691 /* When len=0, we just calculate the needed length */
692 #define LEN_OR_ZERO (len ? len - pos : 0)
694 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
695 for (i = 0; i < event->n_fields; i++) {
696 fmt = synth_field_fmt(event->fields[i]->type);
697 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
698 event->fields[i]->name, fmt,
699 i == event->n_fields - 1 ? "" : ", ");
701 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
703 for (i = 0; i < event->n_fields; i++) {
704 pos += snprintf(buf + pos, LEN_OR_ZERO,
705 ", REC->%s", event->fields[i]->name);
710 /* return the length of print_fmt */
714 static int set_synth_event_print_fmt(struct trace_event_call *call)
716 struct synth_event *event = call->data;
720 /* First: called with 0 length to calculate the needed length */
721 len = __set_synth_event_print_fmt(event, NULL, 0);
723 print_fmt = kmalloc(len + 1, GFP_KERNEL);
727 /* Second: actually write the @print_fmt */
728 __set_synth_event_print_fmt(event, print_fmt, len + 1);
729 call->print_fmt = print_fmt;
734 static void free_synth_field(struct synth_field *field)
741 static struct synth_field *parse_synth_field(int argc, char **argv,
744 struct synth_field *field;
745 const char *prefix = NULL;
746 char *field_type = argv[0], *field_name;
750 if (field_type[0] == ';')
753 if (!strcmp(field_type, "unsigned")) {
755 return ERR_PTR(-EINVAL);
756 prefix = "unsigned ";
757 field_type = argv[1];
758 field_name = argv[2];
761 field_name = argv[1];
765 len = strlen(field_name);
766 if (field_name[len - 1] == ';')
767 field_name[len - 1] = '\0';
769 field = kzalloc(sizeof(*field), GFP_KERNEL);
771 return ERR_PTR(-ENOMEM);
773 len = strlen(field_type) + 1;
774 array = strchr(field_name, '[');
776 len += strlen(array);
778 len += strlen(prefix);
779 field->type = kzalloc(len, GFP_KERNEL);
785 strcat(field->type, prefix);
786 strcat(field->type, field_type);
788 strcat(field->type, array);
792 field->size = synth_field_size(field->type);
798 if (synth_field_is_string(field->type))
799 field->is_string = true;
801 field->is_signed = synth_field_signed(field->type);
803 field->name = kstrdup(field_name, GFP_KERNEL);
811 free_synth_field(field);
812 field = ERR_PTR(ret);
816 static void free_synth_tracepoint(struct tracepoint *tp)
825 static struct tracepoint *alloc_synth_tracepoint(char *name)
827 struct tracepoint *tp;
829 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
831 return ERR_PTR(-ENOMEM);
833 tp->name = kstrdup(name, GFP_KERNEL);
836 return ERR_PTR(-ENOMEM);
842 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
843 unsigned int var_ref_idx);
845 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
846 unsigned int var_ref_idx)
848 struct tracepoint *tp = event->tp;
850 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
851 struct tracepoint_func *probe_func_ptr;
852 synth_probe_func_t probe_func;
855 if (!(cpu_online(raw_smp_processor_id())))
858 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
859 if (probe_func_ptr) {
861 probe_func = probe_func_ptr->func;
862 __data = probe_func_ptr->data;
863 probe_func(__data, var_ref_vals, var_ref_idx);
864 } while ((++probe_func_ptr)->func);
869 static struct synth_event *find_synth_event(const char *name)
871 struct synth_event *event;
873 list_for_each_entry(event, &synth_event_list, list) {
874 if (strcmp(event->name, name) == 0)
881 static int register_synth_event(struct synth_event *event)
883 struct trace_event_call *call = &event->call;
886 event->call.class = &event->class;
887 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
888 if (!event->class.system) {
893 event->tp = alloc_synth_tracepoint(event->name);
894 if (IS_ERR(event->tp)) {
895 ret = PTR_ERR(event->tp);
900 INIT_LIST_HEAD(&call->class->fields);
901 call->event.funcs = &synth_event_funcs;
902 call->class->define_fields = synth_event_define_fields;
904 ret = register_trace_event(&call->event);
909 call->flags = TRACE_EVENT_FL_TRACEPOINT;
910 call->class->reg = trace_event_reg;
911 call->class->probe = trace_event_raw_event_synth;
913 call->tp = event->tp;
915 ret = trace_add_event_call(call);
917 pr_warn("Failed to register synthetic event: %s\n",
918 trace_event_name(call));
922 ret = set_synth_event_print_fmt(call);
924 trace_remove_event_call(call);
930 unregister_trace_event(&call->event);
934 static int unregister_synth_event(struct synth_event *event)
936 struct trace_event_call *call = &event->call;
939 ret = trace_remove_event_call(call);
944 static void free_synth_event(struct synth_event *event)
951 for (i = 0; i < event->n_fields; i++)
952 free_synth_field(event->fields[i]);
954 kfree(event->fields);
956 kfree(event->class.system);
957 free_synth_tracepoint(event->tp);
958 free_synth_event_print_fmt(&event->call);
962 static struct synth_event *alloc_synth_event(char *event_name, int n_fields,
963 struct synth_field **fields)
965 struct synth_event *event;
968 event = kzalloc(sizeof(*event), GFP_KERNEL);
970 event = ERR_PTR(-ENOMEM);
974 event->name = kstrdup(event_name, GFP_KERNEL);
977 event = ERR_PTR(-ENOMEM);
981 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
982 if (!event->fields) {
983 free_synth_event(event);
984 event = ERR_PTR(-ENOMEM);
988 for (i = 0; i < n_fields; i++)
989 event->fields[i] = fields[i];
991 event->n_fields = n_fields;
996 static void action_trace(struct hist_trigger_data *hist_data,
997 struct tracing_map_elt *elt, void *rec,
998 struct ring_buffer_event *rbe,
999 struct action_data *data, u64 *var_ref_vals)
1001 struct synth_event *event = data->onmatch.synth_event;
1003 trace_synth(event, var_ref_vals, data->onmatch.var_ref_idx);
1006 struct hist_var_data {
1007 struct list_head list;
1008 struct hist_trigger_data *hist_data;
1011 static void add_or_delete_synth_event(struct synth_event *event, int delete)
1014 free_synth_event(event);
1016 mutex_lock(&synth_event_mutex);
1017 if (!find_synth_event(event->name))
1018 list_add(&event->list, &synth_event_list);
1020 free_synth_event(event);
1021 mutex_unlock(&synth_event_mutex);
1025 static int create_synth_event(int argc, char **argv)
1027 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1028 struct synth_event *event = NULL;
1029 bool delete_event = false;
1030 int i, consumed = 0, n_fields = 0, ret = 0;
1033 mutex_lock(&synth_event_mutex);
1037 * - Add synthetic event: <event_name> field[;field] ...
1038 * - Remove synthetic event: !<event_name> field[;field] ...
1039 * where 'field' = type field_name
1047 if (name[0] == '!') {
1048 delete_event = true;
1052 event = find_synth_event(name);
1060 list_del(&event->list);
1066 } else if (delete_event)
1074 for (i = 1; i < argc - 1; i++) {
1075 if (strcmp(argv[i], ";") == 0)
1077 if (n_fields == SYNTH_FIELDS_MAX) {
1082 field = parse_synth_field(argc - i, &argv[i], &consumed);
1083 if (IS_ERR(field)) {
1084 ret = PTR_ERR(field);
1087 fields[n_fields++] = field;
1091 if (i < argc && strcmp(argv[i], ";") != 0) {
1096 event = alloc_synth_event(name, n_fields, fields);
1097 if (IS_ERR(event)) {
1098 ret = PTR_ERR(event);
1103 mutex_unlock(&synth_event_mutex);
1107 ret = unregister_synth_event(event);
1108 add_or_delete_synth_event(event, !ret);
1110 ret = register_synth_event(event);
1111 add_or_delete_synth_event(event, ret);
1117 mutex_unlock(&synth_event_mutex);
1119 for (i = 0; i < n_fields; i++)
1120 free_synth_field(fields[i]);
1121 free_synth_event(event);
1126 static int release_all_synth_events(void)
1128 struct list_head release_events;
1129 struct synth_event *event, *e;
1132 INIT_LIST_HEAD(&release_events);
1134 mutex_lock(&synth_event_mutex);
1136 list_for_each_entry(event, &synth_event_list, list) {
1138 mutex_unlock(&synth_event_mutex);
1143 list_splice_init(&event->list, &release_events);
1145 mutex_unlock(&synth_event_mutex);
1147 list_for_each_entry_safe(event, e, &release_events, list) {
1148 list_del(&event->list);
1150 ret = unregister_synth_event(event);
1151 add_or_delete_synth_event(event, !ret);
1158 static void *synth_events_seq_start(struct seq_file *m, loff_t *pos)
1160 mutex_lock(&synth_event_mutex);
1162 return seq_list_start(&synth_event_list, *pos);
1165 static void *synth_events_seq_next(struct seq_file *m, void *v, loff_t *pos)
1167 return seq_list_next(v, &synth_event_list, pos);
1170 static void synth_events_seq_stop(struct seq_file *m, void *v)
1172 mutex_unlock(&synth_event_mutex);
1175 static int synth_events_seq_show(struct seq_file *m, void *v)
1177 struct synth_field *field;
1178 struct synth_event *event = v;
1181 seq_printf(m, "%s\t", event->name);
1183 for (i = 0; i < event->n_fields; i++) {
1184 field = event->fields[i];
1186 /* parameter values */
1187 seq_printf(m, "%s %s%s", field->type, field->name,
1188 i == event->n_fields - 1 ? "" : "; ");
1196 static const struct seq_operations synth_events_seq_op = {
1197 .start = synth_events_seq_start,
1198 .next = synth_events_seq_next,
1199 .stop = synth_events_seq_stop,
1200 .show = synth_events_seq_show
1203 static int synth_events_open(struct inode *inode, struct file *file)
1207 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1208 ret = release_all_synth_events();
1213 return seq_open(file, &synth_events_seq_op);
1216 static ssize_t synth_events_write(struct file *file,
1217 const char __user *buffer,
1218 size_t count, loff_t *ppos)
1220 return trace_parse_run_command(file, buffer, count, ppos,
1221 create_synth_event);
1224 static const struct file_operations synth_events_fops = {
1225 .open = synth_events_open,
1226 .write = synth_events_write,
1228 .llseek = seq_lseek,
1229 .release = seq_release,
1232 static u64 hist_field_timestamp(struct hist_field *hist_field,
1233 struct tracing_map_elt *elt,
1234 struct ring_buffer_event *rbe,
1237 struct hist_trigger_data *hist_data = hist_field->hist_data;
1238 struct trace_array *tr = hist_data->event_file->tr;
1240 u64 ts = ring_buffer_event_time_stamp(rbe);
1242 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1248 static u64 hist_field_cpu(struct hist_field *hist_field,
1249 struct tracing_map_elt *elt,
1250 struct ring_buffer_event *rbe,
1253 int cpu = smp_processor_id();
1258 static struct hist_field *
1259 check_field_for_var_ref(struct hist_field *hist_field,
1260 struct hist_trigger_data *var_data,
1261 unsigned int var_idx)
1263 struct hist_field *found = NULL;
1265 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF) {
1266 if (hist_field->var.idx == var_idx &&
1267 hist_field->var.hist_data == var_data) {
1275 static struct hist_field *
1276 check_field_for_var_refs(struct hist_trigger_data *hist_data,
1277 struct hist_field *hist_field,
1278 struct hist_trigger_data *var_data,
1279 unsigned int var_idx,
1282 struct hist_field *found = NULL;
1291 found = check_field_for_var_ref(hist_field, var_data, var_idx);
1295 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1296 struct hist_field *operand;
1298 operand = hist_field->operands[i];
1299 found = check_field_for_var_refs(hist_data, operand, var_data,
1300 var_idx, level + 1);
1308 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1309 struct hist_trigger_data *var_data,
1310 unsigned int var_idx)
1312 struct hist_field *hist_field, *found = NULL;
1315 for_each_hist_field(i, hist_data) {
1316 hist_field = hist_data->fields[i];
1317 found = check_field_for_var_refs(hist_data, hist_field,
1318 var_data, var_idx, 0);
1323 for (i = 0; i < hist_data->n_synth_var_refs; i++) {
1324 hist_field = hist_data->synth_var_refs[i];
1325 found = check_field_for_var_refs(hist_data, hist_field,
1326 var_data, var_idx, 0);
1334 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1335 unsigned int var_idx)
1337 struct trace_array *tr = hist_data->event_file->tr;
1338 struct hist_field *found = NULL;
1339 struct hist_var_data *var_data;
1341 list_for_each_entry(var_data, &tr->hist_vars, list) {
1342 if (var_data->hist_data == hist_data)
1344 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1352 static bool check_var_refs(struct hist_trigger_data *hist_data)
1354 struct hist_field *field;
1358 for_each_hist_field(i, hist_data) {
1359 field = hist_data->fields[i];
1360 if (field && field->flags & HIST_FIELD_FL_VAR) {
1361 if (find_any_var_ref(hist_data, field->var.idx)) {
1371 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1373 struct trace_array *tr = hist_data->event_file->tr;
1374 struct hist_var_data *var_data, *found = NULL;
1376 list_for_each_entry(var_data, &tr->hist_vars, list) {
1377 if (var_data->hist_data == hist_data) {
1386 static bool field_has_hist_vars(struct hist_field *hist_field,
1397 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1398 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1401 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1402 struct hist_field *operand;
1404 operand = hist_field->operands[i];
1405 if (field_has_hist_vars(operand, level + 1))
1412 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1414 struct hist_field *hist_field;
1417 for_each_hist_field(i, hist_data) {
1418 hist_field = hist_data->fields[i];
1419 if (field_has_hist_vars(hist_field, 0))
1426 static int save_hist_vars(struct hist_trigger_data *hist_data)
1428 struct trace_array *tr = hist_data->event_file->tr;
1429 struct hist_var_data *var_data;
1431 var_data = find_hist_vars(hist_data);
1435 if (trace_array_get(tr) < 0)
1438 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1440 trace_array_put(tr);
1444 var_data->hist_data = hist_data;
1445 list_add(&var_data->list, &tr->hist_vars);
1450 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1452 struct trace_array *tr = hist_data->event_file->tr;
1453 struct hist_var_data *var_data;
1455 var_data = find_hist_vars(hist_data);
1459 if (WARN_ON(check_var_refs(hist_data)))
1462 list_del(&var_data->list);
1466 trace_array_put(tr);
1469 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1470 const char *var_name)
1472 struct hist_field *hist_field, *found = NULL;
1475 for_each_hist_field(i, hist_data) {
1476 hist_field = hist_data->fields[i];
1477 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1478 strcmp(hist_field->var.name, var_name) == 0) {
1487 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1488 struct trace_event_file *file,
1489 const char *var_name)
1491 struct hist_trigger_data *test_data;
1492 struct event_trigger_data *test;
1493 struct hist_field *hist_field;
1495 hist_field = find_var_field(hist_data, var_name);
1499 list_for_each_entry_rcu(test, &file->triggers, list) {
1500 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1501 test_data = test->private_data;
1502 hist_field = find_var_field(test_data, var_name);
1511 static struct trace_event_file *find_var_file(struct trace_array *tr,
1516 struct hist_trigger_data *var_hist_data;
1517 struct hist_var_data *var_data;
1518 struct trace_event_file *file, *found = NULL;
1521 return find_event_file(tr, system, event_name);
1523 list_for_each_entry(var_data, &tr->hist_vars, list) {
1524 var_hist_data = var_data->hist_data;
1525 file = var_hist_data->event_file;
1529 if (find_var_field(var_hist_data, var_name)) {
1531 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1542 static struct hist_field *find_file_var(struct trace_event_file *file,
1543 const char *var_name)
1545 struct hist_trigger_data *test_data;
1546 struct event_trigger_data *test;
1547 struct hist_field *hist_field;
1549 list_for_each_entry_rcu(test, &file->triggers, list) {
1550 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1551 test_data = test->private_data;
1552 hist_field = find_var_field(test_data, var_name);
1561 static struct hist_field *
1562 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1564 struct trace_array *tr = hist_data->event_file->tr;
1565 struct hist_field *hist_field, *found = NULL;
1566 struct trace_event_file *file;
1569 for (i = 0; i < hist_data->n_actions; i++) {
1570 struct action_data *data = hist_data->actions[i];
1572 if (data->fn == action_trace) {
1573 char *system = data->onmatch.match_event_system;
1574 char *event_name = data->onmatch.match_event;
1576 file = find_var_file(tr, system, event_name, var_name);
1579 hist_field = find_file_var(file, var_name);
1582 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1583 return ERR_PTR(-EINVAL);
1593 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1598 struct trace_array *tr = hist_data->event_file->tr;
1599 struct hist_field *hist_field = NULL;
1600 struct trace_event_file *file;
1602 if (!system || !event_name) {
1603 hist_field = find_match_var(hist_data, var_name);
1604 if (IS_ERR(hist_field))
1610 file = find_var_file(tr, system, event_name, var_name);
1614 hist_field = find_file_var(file, var_name);
1619 struct hist_elt_data {
1622 char *field_var_str[SYNTH_FIELDS_MAX];
1625 static u64 hist_field_var_ref(struct hist_field *hist_field,
1626 struct tracing_map_elt *elt,
1627 struct ring_buffer_event *rbe,
1630 struct hist_elt_data *elt_data;
1633 elt_data = elt->private_data;
1634 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1639 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1640 u64 *var_ref_vals, bool self)
1642 struct hist_trigger_data *var_data;
1643 struct tracing_map_elt *var_elt;
1644 struct hist_field *hist_field;
1645 unsigned int i, var_idx;
1646 bool resolved = true;
1649 for (i = 0; i < hist_data->n_var_refs; i++) {
1650 hist_field = hist_data->var_refs[i];
1651 var_idx = hist_field->var.idx;
1652 var_data = hist_field->var.hist_data;
1654 if (var_data == NULL) {
1659 if ((self && var_data != hist_data) ||
1660 (!self && var_data == hist_data))
1663 var_elt = tracing_map_lookup(var_data->map, key);
1669 if (!tracing_map_var_set(var_elt, var_idx)) {
1674 if (self || !hist_field->read_once)
1675 var_val = tracing_map_read_var(var_elt, var_idx);
1677 var_val = tracing_map_read_var_once(var_elt, var_idx);
1679 var_ref_vals[i] = var_val;
1685 static const char *hist_field_name(struct hist_field *field,
1688 const char *field_name = "";
1694 field_name = field->field->name;
1695 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1696 field->flags & HIST_FIELD_FL_ALIAS)
1697 field_name = hist_field_name(field->operands[0], ++level);
1698 else if (field->flags & HIST_FIELD_FL_CPU)
1700 else if (field->flags & HIST_FIELD_FL_EXPR ||
1701 field->flags & HIST_FIELD_FL_VAR_REF) {
1702 if (field->system) {
1703 static char full_name[MAX_FILTER_STR_VAL];
1705 strcat(full_name, field->system);
1706 strcat(full_name, ".");
1707 strcat(full_name, field->event_name);
1708 strcat(full_name, ".");
1709 strcat(full_name, field->name);
1710 field_name = full_name;
1712 field_name = field->name;
1713 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1714 field_name = "common_timestamp";
1716 if (field_name == NULL)
1722 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1724 hist_field_fn_t fn = NULL;
1726 switch (field_size) {
1728 if (field_is_signed)
1729 fn = hist_field_s64;
1731 fn = hist_field_u64;
1734 if (field_is_signed)
1735 fn = hist_field_s32;
1737 fn = hist_field_u32;
1740 if (field_is_signed)
1741 fn = hist_field_s16;
1743 fn = hist_field_u16;
1746 if (field_is_signed)
1756 static int parse_map_size(char *str)
1758 unsigned long size, map_bits;
1767 ret = kstrtoul(str, 0, &size);
1771 map_bits = ilog2(roundup_pow_of_two(size));
1772 if (map_bits < TRACING_MAP_BITS_MIN ||
1773 map_bits > TRACING_MAP_BITS_MAX)
1781 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1788 for (i = 0; i < attrs->n_assignments; i++)
1789 kfree(attrs->assignment_str[i]);
1791 for (i = 0; i < attrs->n_actions; i++)
1792 kfree(attrs->action_str[i]);
1795 kfree(attrs->sort_key_str);
1796 kfree(attrs->keys_str);
1797 kfree(attrs->vals_str);
1798 kfree(attrs->clock);
1802 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1806 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1809 if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) ||
1810 (strncmp(str, "onmax(", strlen("onmax(")) == 0)) {
1811 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1812 if (!attrs->action_str[attrs->n_actions]) {
1823 static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
1827 if ((strncmp(str, "key=", strlen("key=")) == 0) ||
1828 (strncmp(str, "keys=", strlen("keys=")) == 0)) {
1829 attrs->keys_str = kstrdup(str, GFP_KERNEL);
1830 if (!attrs->keys_str) {
1834 } else if ((strncmp(str, "val=", strlen("val=")) == 0) ||
1835 (strncmp(str, "vals=", strlen("vals=")) == 0) ||
1836 (strncmp(str, "values=", strlen("values=")) == 0)) {
1837 attrs->vals_str = kstrdup(str, GFP_KERNEL);
1838 if (!attrs->vals_str) {
1842 } else if (strncmp(str, "sort=", strlen("sort=")) == 0) {
1843 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
1844 if (!attrs->sort_key_str) {
1848 } else if (strncmp(str, "name=", strlen("name=")) == 0) {
1849 attrs->name = kstrdup(str, GFP_KERNEL);
1854 } else if (strncmp(str, "clock=", strlen("clock=")) == 0) {
1861 str = strstrip(str);
1862 attrs->clock = kstrdup(str, GFP_KERNEL);
1863 if (!attrs->clock) {
1867 } else if (strncmp(str, "size=", strlen("size=")) == 0) {
1868 int map_bits = parse_map_size(str);
1874 attrs->map_bits = map_bits;
1878 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1879 hist_err("Too many variables defined: ", str);
1884 assignment = kstrdup(str, GFP_KERNEL);
1890 attrs->assignment_str[attrs->n_assignments++] = assignment;
1896 static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
1898 struct hist_trigger_attrs *attrs;
1901 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1903 return ERR_PTR(-ENOMEM);
1905 while (trigger_str) {
1906 char *str = strsep(&trigger_str, ":");
1908 if (strchr(str, '=')) {
1909 ret = parse_assignment(str, attrs);
1912 } else if (strcmp(str, "pause") == 0)
1913 attrs->pause = true;
1914 else if ((strcmp(str, "cont") == 0) ||
1915 (strcmp(str, "continue") == 0))
1917 else if (strcmp(str, "clear") == 0)
1918 attrs->clear = true;
1920 ret = parse_action(str, attrs);
1926 if (!attrs->keys_str) {
1931 if (!attrs->clock) {
1932 attrs->clock = kstrdup("global", GFP_KERNEL);
1933 if (!attrs->clock) {
1941 destroy_hist_trigger_attrs(attrs);
1943 return ERR_PTR(ret);
1946 static inline void save_comm(char *comm, struct task_struct *task)
1949 strcpy(comm, "<idle>");
1953 if (WARN_ON_ONCE(task->pid < 0)) {
1954 strcpy(comm, "<XXX>");
1958 memcpy(comm, task->comm, TASK_COMM_LEN);
1961 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1965 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1966 kfree(elt_data->field_var_str[i]);
1968 kfree(elt_data->comm);
1972 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1974 struct hist_elt_data *elt_data = elt->private_data;
1976 hist_elt_data_free(elt_data);
1979 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1981 struct hist_trigger_data *hist_data = elt->map->private_data;
1982 unsigned int size = TASK_COMM_LEN;
1983 struct hist_elt_data *elt_data;
1984 struct hist_field *key_field;
1985 unsigned int i, n_str;
1987 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1991 for_each_hist_key_field(i, hist_data) {
1992 key_field = hist_data->fields[i];
1994 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
1995 elt_data->comm = kzalloc(size, GFP_KERNEL);
1996 if (!elt_data->comm) {
2004 n_str = hist_data->n_field_var_str + hist_data->n_max_var_str;
2006 size = STR_VAR_LEN_MAX;
2008 for (i = 0; i < n_str; i++) {
2009 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2010 if (!elt_data->field_var_str[i]) {
2011 hist_elt_data_free(elt_data);
2016 elt->private_data = elt_data;
2021 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2023 struct hist_elt_data *elt_data = elt->private_data;
2026 save_comm(elt_data->comm, current);
2029 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2030 .elt_alloc = hist_trigger_elt_data_alloc,
2031 .elt_free = hist_trigger_elt_data_free,
2032 .elt_init = hist_trigger_elt_data_init,
2035 static const char *get_hist_field_flags(struct hist_field *hist_field)
2037 const char *flags_str = NULL;
2039 if (hist_field->flags & HIST_FIELD_FL_HEX)
2041 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2043 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2044 flags_str = "sym-offset";
2045 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2046 flags_str = "execname";
2047 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2048 flags_str = "syscall";
2049 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2051 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2052 flags_str = "usecs";
2057 static void expr_field_str(struct hist_field *field, char *expr)
2059 if (field->flags & HIST_FIELD_FL_VAR_REF)
2062 strcat(expr, hist_field_name(field, 0));
2064 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2065 const char *flags_str = get_hist_field_flags(field);
2069 strcat(expr, flags_str);
2074 static char *expr_str(struct hist_field *field, unsigned int level)
2081 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2085 if (!field->operands[0]) {
2086 expr_field_str(field, expr);
2090 if (field->operator == FIELD_OP_UNARY_MINUS) {
2094 subexpr = expr_str(field->operands[0], ++level);
2099 strcat(expr, subexpr);
2107 expr_field_str(field->operands[0], expr);
2109 switch (field->operator) {
2110 case FIELD_OP_MINUS:
2121 expr_field_str(field->operands[1], expr);
2126 static int contains_operator(char *str)
2128 enum field_op_id field_op = FIELD_OP_NONE;
2131 op = strpbrk(str, "+-");
2133 return FIELD_OP_NONE;
2138 field_op = FIELD_OP_UNARY_MINUS;
2140 field_op = FIELD_OP_MINUS;
2143 field_op = FIELD_OP_PLUS;
2152 static void destroy_hist_field(struct hist_field *hist_field,
2163 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2164 destroy_hist_field(hist_field->operands[i], level + 1);
2166 kfree(hist_field->var.name);
2167 kfree(hist_field->name);
2168 kfree(hist_field->type);
2173 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2174 struct ftrace_event_field *field,
2175 unsigned long flags,
2178 struct hist_field *hist_field;
2180 if (field && is_function_field(field))
2183 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2187 hist_field->hist_data = hist_data;
2189 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2190 goto out; /* caller will populate */
2192 if (flags & HIST_FIELD_FL_VAR_REF) {
2193 hist_field->fn = hist_field_var_ref;
2197 if (flags & HIST_FIELD_FL_HITCOUNT) {
2198 hist_field->fn = hist_field_counter;
2199 hist_field->size = sizeof(u64);
2200 hist_field->type = kstrdup("u64", GFP_KERNEL);
2201 if (!hist_field->type)
2206 if (flags & HIST_FIELD_FL_STACKTRACE) {
2207 hist_field->fn = hist_field_none;
2211 if (flags & HIST_FIELD_FL_LOG2) {
2212 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2213 hist_field->fn = hist_field_log2;
2214 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2215 hist_field->size = hist_field->operands[0]->size;
2216 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2217 if (!hist_field->type)
2222 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2223 hist_field->fn = hist_field_timestamp;
2224 hist_field->size = sizeof(u64);
2225 hist_field->type = kstrdup("u64", GFP_KERNEL);
2226 if (!hist_field->type)
2231 if (flags & HIST_FIELD_FL_CPU) {
2232 hist_field->fn = hist_field_cpu;
2233 hist_field->size = sizeof(int);
2234 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2235 if (!hist_field->type)
2240 if (WARN_ON_ONCE(!field))
2243 if (is_string_field(field)) {
2244 flags |= HIST_FIELD_FL_STRING;
2246 hist_field->size = MAX_FILTER_STR_VAL;
2247 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2248 if (!hist_field->type)
2251 if (field->filter_type == FILTER_STATIC_STRING)
2252 hist_field->fn = hist_field_string;
2253 else if (field->filter_type == FILTER_DYN_STRING)
2254 hist_field->fn = hist_field_dynstring;
2256 hist_field->fn = hist_field_pstring;
2258 hist_field->size = field->size;
2259 hist_field->is_signed = field->is_signed;
2260 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2261 if (!hist_field->type)
2264 hist_field->fn = select_value_fn(field->size,
2266 if (!hist_field->fn) {
2267 destroy_hist_field(hist_field, 0);
2272 hist_field->field = field;
2273 hist_field->flags = flags;
2276 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2277 if (!hist_field->var.name)
2283 destroy_hist_field(hist_field, 0);
2287 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2291 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2292 if (hist_data->fields[i]) {
2293 destroy_hist_field(hist_data->fields[i], 0);
2294 hist_data->fields[i] = NULL;
2299 static int init_var_ref(struct hist_field *ref_field,
2300 struct hist_field *var_field,
2301 char *system, char *event_name)
2305 ref_field->var.idx = var_field->var.idx;
2306 ref_field->var.hist_data = var_field->hist_data;
2307 ref_field->size = var_field->size;
2308 ref_field->is_signed = var_field->is_signed;
2309 ref_field->flags |= var_field->flags &
2310 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2313 ref_field->system = kstrdup(system, GFP_KERNEL);
2314 if (!ref_field->system)
2319 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2320 if (!ref_field->event_name) {
2326 if (var_field->var.name) {
2327 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2328 if (!ref_field->name) {
2332 } else if (var_field->name) {
2333 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2334 if (!ref_field->name) {
2340 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2341 if (!ref_field->type) {
2348 kfree(ref_field->system);
2349 kfree(ref_field->event_name);
2350 kfree(ref_field->name);
2355 static struct hist_field *create_var_ref(struct hist_field *var_field,
2356 char *system, char *event_name)
2358 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2359 struct hist_field *ref_field;
2361 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2363 if (init_var_ref(ref_field, var_field, system, event_name)) {
2364 destroy_hist_field(ref_field, 0);
2372 static bool is_var_ref(char *var_name)
2374 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2380 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2386 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2387 name = hist_data->attrs->var_defs.name[i];
2389 if (strcmp(var_name, name) == 0) {
2390 field = hist_data->attrs->var_defs.expr[i];
2391 if (contains_operator(field) || is_var_ref(field))
2400 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2401 char *system, char *event_name,
2404 struct trace_event_call *call;
2406 if (system && event_name) {
2407 call = hist_data->event_file->event_call;
2409 if (strcmp(system, call->class->system) != 0)
2412 if (strcmp(event_name, trace_event_name(call)) != 0)
2416 if (!!system != !!event_name)
2419 if (!is_var_ref(var_name))
2424 return field_name_from_var(hist_data, var_name);
2427 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2428 char *system, char *event_name,
2431 struct hist_field *var_field = NULL, *ref_field = NULL;
2433 if (!is_var_ref(var_name))
2438 var_field = find_event_var(hist_data, system, event_name, var_name);
2440 ref_field = create_var_ref(var_field, system, event_name);
2443 hist_err_event("Couldn't find variable: $",
2444 system, event_name, var_name);
2449 static struct ftrace_event_field *
2450 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2451 char *field_str, unsigned long *flags)
2453 struct ftrace_event_field *field = NULL;
2454 char *field_name, *modifier, *str;
2456 modifier = str = kstrdup(field_str, GFP_KERNEL);
2458 return ERR_PTR(-ENOMEM);
2460 field_name = strsep(&modifier, ".");
2462 if (strcmp(modifier, "hex") == 0)
2463 *flags |= HIST_FIELD_FL_HEX;
2464 else if (strcmp(modifier, "sym") == 0)
2465 *flags |= HIST_FIELD_FL_SYM;
2466 else if (strcmp(modifier, "sym-offset") == 0)
2467 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2468 else if ((strcmp(modifier, "execname") == 0) &&
2469 (strcmp(field_name, "common_pid") == 0))
2470 *flags |= HIST_FIELD_FL_EXECNAME;
2471 else if (strcmp(modifier, "syscall") == 0)
2472 *flags |= HIST_FIELD_FL_SYSCALL;
2473 else if (strcmp(modifier, "log2") == 0)
2474 *flags |= HIST_FIELD_FL_LOG2;
2475 else if (strcmp(modifier, "usecs") == 0)
2476 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2478 hist_err("Invalid field modifier: ", modifier);
2479 field = ERR_PTR(-EINVAL);
2484 if (strcmp(field_name, "common_timestamp") == 0) {
2485 *flags |= HIST_FIELD_FL_TIMESTAMP;
2486 hist_data->enable_timestamps = true;
2487 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2488 hist_data->attrs->ts_in_usecs = true;
2489 } else if (strcmp(field_name, "cpu") == 0)
2490 *flags |= HIST_FIELD_FL_CPU;
2492 field = trace_find_event_field(file->event_call, field_name);
2493 if (!field || !field->size) {
2494 hist_err("Couldn't find field: ", field_name);
2495 field = ERR_PTR(-EINVAL);
2505 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2506 struct hist_field *var_ref,
2509 struct hist_field *alias = NULL;
2510 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2512 alias = create_hist_field(hist_data, NULL, flags, var_name);
2516 alias->fn = var_ref->fn;
2517 alias->operands[0] = var_ref;
2519 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2520 destroy_hist_field(alias, 0);
2527 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2528 struct trace_event_file *file, char *str,
2529 unsigned long *flags, char *var_name)
2531 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2532 struct ftrace_event_field *field = NULL;
2533 struct hist_field *hist_field = NULL;
2536 s = strchr(str, '.');
2538 s = strchr(++s, '.');
2540 ref_system = strsep(&str, ".");
2545 ref_event = strsep(&str, ".");
2554 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2556 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2558 hist_data->var_refs[hist_data->n_var_refs] = hist_field;
2559 hist_field->var_ref_idx = hist_data->n_var_refs++;
2561 hist_field = create_alias(hist_data, hist_field, var_name);
2572 field = parse_field(hist_data, file, str, flags);
2573 if (IS_ERR(field)) {
2574 ret = PTR_ERR(field);
2578 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2586 return ERR_PTR(ret);
2589 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2590 struct trace_event_file *file,
2591 char *str, unsigned long flags,
2592 char *var_name, unsigned int level);
2594 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2595 struct trace_event_file *file,
2596 char *str, unsigned long flags,
2597 char *var_name, unsigned int level)
2599 struct hist_field *operand1, *expr = NULL;
2600 unsigned long operand_flags;
2604 /* we support only -(xxx) i.e. explicit parens required */
2607 hist_err("Too many subexpressions (3 max): ", str);
2612 str++; /* skip leading '-' */
2614 s = strchr(str, '(');
2622 s = strrchr(str, ')');
2626 ret = -EINVAL; /* no closing ')' */
2630 flags |= HIST_FIELD_FL_EXPR;
2631 expr = create_hist_field(hist_data, NULL, flags, var_name);
2638 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2639 if (IS_ERR(operand1)) {
2640 ret = PTR_ERR(operand1);
2644 expr->flags |= operand1->flags &
2645 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2646 expr->fn = hist_field_unary_minus;
2647 expr->operands[0] = operand1;
2648 expr->operator = FIELD_OP_UNARY_MINUS;
2649 expr->name = expr_str(expr, 0);
2650 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2658 destroy_hist_field(expr, 0);
2659 return ERR_PTR(ret);
2662 static int check_expr_operands(struct hist_field *operand1,
2663 struct hist_field *operand2)
2665 unsigned long operand1_flags = operand1->flags;
2666 unsigned long operand2_flags = operand2->flags;
2668 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2669 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2670 struct hist_field *var;
2672 var = find_var_field(operand1->var.hist_data, operand1->name);
2675 operand1_flags = var->flags;
2678 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2679 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2680 struct hist_field *var;
2682 var = find_var_field(operand2->var.hist_data, operand2->name);
2685 operand2_flags = var->flags;
2688 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2689 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2690 hist_err("Timestamp units in expression don't match", NULL);
2697 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2698 struct trace_event_file *file,
2699 char *str, unsigned long flags,
2700 char *var_name, unsigned int level)
2702 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2703 unsigned long operand_flags;
2704 int field_op, ret = -EINVAL;
2705 char *sep, *operand1_str;
2708 hist_err("Too many subexpressions (3 max): ", str);
2709 return ERR_PTR(-EINVAL);
2712 field_op = contains_operator(str);
2714 if (field_op == FIELD_OP_NONE)
2715 return parse_atom(hist_data, file, str, &flags, var_name);
2717 if (field_op == FIELD_OP_UNARY_MINUS)
2718 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2721 case FIELD_OP_MINUS:
2731 operand1_str = strsep(&str, sep);
2732 if (!operand1_str || !str)
2736 operand1 = parse_atom(hist_data, file, operand1_str,
2737 &operand_flags, NULL);
2738 if (IS_ERR(operand1)) {
2739 ret = PTR_ERR(operand1);
2744 /* rest of string could be another expression e.g. b+c in a+b+c */
2746 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2747 if (IS_ERR(operand2)) {
2748 ret = PTR_ERR(operand2);
2753 ret = check_expr_operands(operand1, operand2);
2757 flags |= HIST_FIELD_FL_EXPR;
2759 flags |= operand1->flags &
2760 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2762 expr = create_hist_field(hist_data, NULL, flags, var_name);
2768 operand1->read_once = true;
2769 operand2->read_once = true;
2771 expr->operands[0] = operand1;
2772 expr->operands[1] = operand2;
2773 expr->operator = field_op;
2774 expr->name = expr_str(expr, 0);
2775 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2782 case FIELD_OP_MINUS:
2783 expr->fn = hist_field_minus;
2786 expr->fn = hist_field_plus;
2795 destroy_hist_field(operand1, 0);
2796 destroy_hist_field(operand2, 0);
2797 destroy_hist_field(expr, 0);
2799 return ERR_PTR(ret);
2802 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2803 struct trace_event_file *file)
2805 struct event_trigger_data *test;
2807 list_for_each_entry_rcu(test, &file->triggers, list) {
2808 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2809 if (test->private_data == hist_data)
2810 return test->filter_str;
2817 static struct event_command trigger_hist_cmd;
2818 static int event_hist_trigger_func(struct event_command *cmd_ops,
2819 struct trace_event_file *file,
2820 char *glob, char *cmd, char *param);
2822 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2823 struct hist_trigger_data *hist_data,
2824 unsigned int n_keys)
2826 struct hist_field *target_hist_field, *hist_field;
2827 unsigned int n, i, j;
2829 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2832 i = hist_data->n_vals;
2833 j = target_hist_data->n_vals;
2835 for (n = 0; n < n_keys; n++) {
2836 hist_field = hist_data->fields[i + n];
2837 target_hist_field = target_hist_data->fields[j + n];
2839 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2841 if (hist_field->size != target_hist_field->size)
2843 if (hist_field->is_signed != target_hist_field->is_signed)
2850 static struct hist_trigger_data *
2851 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2852 struct trace_event_file *file)
2854 struct hist_trigger_data *hist_data;
2855 struct event_trigger_data *test;
2856 unsigned int n_keys;
2858 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2860 list_for_each_entry_rcu(test, &file->triggers, list) {
2861 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2862 hist_data = test->private_data;
2864 if (compatible_keys(target_hist_data, hist_data, n_keys))
2872 static struct trace_event_file *event_file(struct trace_array *tr,
2873 char *system, char *event_name)
2875 struct trace_event_file *file;
2877 file = __find_event_file(tr, system, event_name);
2879 return ERR_PTR(-EINVAL);
2884 static struct hist_field *
2885 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2886 char *system, char *event_name, char *field_name)
2888 struct hist_field *event_var;
2889 char *synthetic_name;
2891 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2892 if (!synthetic_name)
2893 return ERR_PTR(-ENOMEM);
2895 strcpy(synthetic_name, "synthetic_");
2896 strcat(synthetic_name, field_name);
2898 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2900 kfree(synthetic_name);
2906 * create_field_var_hist - Automatically create a histogram and var for a field
2907 * @target_hist_data: The target hist trigger
2908 * @subsys_name: Optional subsystem name
2909 * @event_name: Optional event name
2910 * @field_name: The name of the field (and the resulting variable)
2912 * Hist trigger actions fetch data from variables, not directly from
2913 * events. However, for convenience, users are allowed to directly
2914 * specify an event field in an action, which will be automatically
2915 * converted into a variable on their behalf.
2917 * If a user specifies a field on an event that isn't the event the
2918 * histogram currently being defined (the target event histogram), the
2919 * only way that can be accomplished is if a new hist trigger is
2920 * created and the field variable defined on that.
2922 * This function creates a new histogram compatible with the target
2923 * event (meaning a histogram with the same key as the target
2924 * histogram), and creates a variable for the specified field, but
2925 * with 'synthetic_' prepended to the variable name in order to avoid
2926 * collision with normal field variables.
2928 * Return: The variable created for the field.
2930 static struct hist_field *
2931 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2932 char *subsys_name, char *event_name, char *field_name)
2934 struct trace_array *tr = target_hist_data->event_file->tr;
2935 struct hist_field *event_var = ERR_PTR(-EINVAL);
2936 struct hist_trigger_data *hist_data;
2937 unsigned int i, n, first = true;
2938 struct field_var_hist *var_hist;
2939 struct trace_event_file *file;
2940 struct hist_field *key_field;
2945 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2946 hist_err_event("onmatch: Too many field variables defined: ",
2947 subsys_name, event_name, field_name);
2948 return ERR_PTR(-EINVAL);
2951 file = event_file(tr, subsys_name, event_name);
2954 hist_err_event("onmatch: Event file not found: ",
2955 subsys_name, event_name, field_name);
2956 ret = PTR_ERR(file);
2957 return ERR_PTR(ret);
2961 * Look for a histogram compatible with target. We'll use the
2962 * found histogram specification to create a new matching
2963 * histogram with our variable on it. target_hist_data is not
2964 * yet a registered histogram so we can't use that.
2966 hist_data = find_compatible_hist(target_hist_data, file);
2968 hist_err_event("onmatch: Matching event histogram not found: ",
2969 subsys_name, event_name, field_name);
2970 return ERR_PTR(-EINVAL);
2973 /* See if a synthetic field variable has already been created */
2974 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2975 event_name, field_name);
2976 if (!IS_ERR_OR_NULL(event_var))
2979 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2981 return ERR_PTR(-ENOMEM);
2983 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2986 return ERR_PTR(-ENOMEM);
2989 /* Use the same keys as the compatible histogram */
2990 strcat(cmd, "keys=");
2992 for_each_hist_key_field(i, hist_data) {
2993 key_field = hist_data->fields[i];
2996 strcat(cmd, key_field->field->name);
3000 /* Create the synthetic field variable specification */
3001 strcat(cmd, ":synthetic_");
3002 strcat(cmd, field_name);
3004 strcat(cmd, field_name);
3006 /* Use the same filter as the compatible histogram */
3007 saved_filter = find_trigger_filter(hist_data, file);
3009 strcat(cmd, " if ");
3010 strcat(cmd, saved_filter);
3013 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3014 if (!var_hist->cmd) {
3017 return ERR_PTR(-ENOMEM);
3020 /* Save the compatible histogram information */
3021 var_hist->hist_data = hist_data;
3023 /* Create the new histogram with our variable */
3024 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3028 kfree(var_hist->cmd);
3030 hist_err_event("onmatch: Couldn't create histogram for field: ",
3031 subsys_name, event_name, field_name);
3032 return ERR_PTR(ret);
3037 /* If we can't find the variable, something went wrong */
3038 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3039 event_name, field_name);
3040 if (IS_ERR_OR_NULL(event_var)) {
3041 kfree(var_hist->cmd);
3043 hist_err_event("onmatch: Couldn't find synthetic variable: ",
3044 subsys_name, event_name, field_name);
3045 return ERR_PTR(-EINVAL);
3048 n = target_hist_data->n_field_var_hists;
3049 target_hist_data->field_var_hists[n] = var_hist;
3050 target_hist_data->n_field_var_hists++;
3055 static struct hist_field *
3056 find_target_event_var(struct hist_trigger_data *hist_data,
3057 char *subsys_name, char *event_name, char *var_name)
3059 struct trace_event_file *file = hist_data->event_file;
3060 struct hist_field *hist_field = NULL;
3063 struct trace_event_call *call;
3068 call = file->event_call;
3070 if (strcmp(subsys_name, call->class->system) != 0)
3073 if (strcmp(event_name, trace_event_name(call)) != 0)
3077 hist_field = find_var_field(hist_data, var_name);
3082 static inline void __update_field_vars(struct tracing_map_elt *elt,
3083 struct ring_buffer_event *rbe,
3085 struct field_var **field_vars,
3086 unsigned int n_field_vars,
3087 unsigned int field_var_str_start)
3089 struct hist_elt_data *elt_data = elt->private_data;
3090 unsigned int i, j, var_idx;
3093 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3094 struct field_var *field_var = field_vars[i];
3095 struct hist_field *var = field_var->var;
3096 struct hist_field *val = field_var->val;
3098 var_val = val->fn(val, elt, rbe, rec);
3099 var_idx = var->var.idx;
3101 if (val->flags & HIST_FIELD_FL_STRING) {
3102 char *str = elt_data->field_var_str[j++];
3103 char *val_str = (char *)(uintptr_t)var_val;
3105 strscpy(str, val_str, STR_VAR_LEN_MAX);
3106 var_val = (u64)(uintptr_t)str;
3108 tracing_map_set_var(elt, var_idx, var_val);
3112 static void update_field_vars(struct hist_trigger_data *hist_data,
3113 struct tracing_map_elt *elt,
3114 struct ring_buffer_event *rbe,
3117 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3118 hist_data->n_field_vars, 0);
3121 static void update_max_vars(struct hist_trigger_data *hist_data,
3122 struct tracing_map_elt *elt,
3123 struct ring_buffer_event *rbe,
3126 __update_field_vars(elt, rbe, rec, hist_data->max_vars,
3127 hist_data->n_max_vars, hist_data->n_field_var_str);
3130 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3131 struct trace_event_file *file,
3132 char *name, int size, const char *type)
3134 struct hist_field *var;
3137 if (find_var(hist_data, file, name) && !hist_data->remove) {
3138 var = ERR_PTR(-EINVAL);
3142 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3144 var = ERR_PTR(-ENOMEM);
3148 idx = tracing_map_add_var(hist_data->map);
3151 var = ERR_PTR(-EINVAL);
3155 var->flags = HIST_FIELD_FL_VAR;
3157 var->var.hist_data = var->hist_data = hist_data;
3159 var->var.name = kstrdup(name, GFP_KERNEL);
3160 var->type = kstrdup(type, GFP_KERNEL);
3161 if (!var->var.name || !var->type) {
3162 kfree(var->var.name);
3165 var = ERR_PTR(-ENOMEM);
3171 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3172 struct trace_event_file *file,
3175 struct hist_field *val = NULL, *var = NULL;
3176 unsigned long flags = HIST_FIELD_FL_VAR;
3177 struct field_var *field_var;
3180 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3181 hist_err("Too many field variables defined: ", field_name);
3186 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3188 hist_err("Couldn't parse field variable: ", field_name);
3193 var = create_var(hist_data, file, field_name, val->size, val->type);
3195 hist_err("Couldn't create or find variable: ", field_name);
3201 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3209 field_var->var = var;
3210 field_var->val = val;
3214 field_var = ERR_PTR(ret);
3219 * create_target_field_var - Automatically create a variable for a field
3220 * @target_hist_data: The target hist trigger
3221 * @subsys_name: Optional subsystem name
3222 * @event_name: Optional event name
3223 * @var_name: The name of the field (and the resulting variable)
3225 * Hist trigger actions fetch data from variables, not directly from
3226 * events. However, for convenience, users are allowed to directly
3227 * specify an event field in an action, which will be automatically
3228 * converted into a variable on their behalf.
3230 * This function creates a field variable with the name var_name on
3231 * the hist trigger currently being defined on the target event. If
3232 * subsys_name and event_name are specified, this function simply
3233 * verifies that they do in fact match the target event subsystem and
3236 * Return: The variable created for the field.
3238 static struct field_var *
3239 create_target_field_var(struct hist_trigger_data *target_hist_data,
3240 char *subsys_name, char *event_name, char *var_name)
3242 struct trace_event_file *file = target_hist_data->event_file;
3245 struct trace_event_call *call;
3250 call = file->event_call;
3252 if (strcmp(subsys_name, call->class->system) != 0)
3255 if (strcmp(event_name, trace_event_name(call)) != 0)
3259 return create_field_var(target_hist_data, file, var_name);
3262 static void onmax_print(struct seq_file *m,
3263 struct hist_trigger_data *hist_data,
3264 struct tracing_map_elt *elt,
3265 struct action_data *data)
3267 unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
3269 seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
3271 for (i = 0; i < hist_data->n_max_vars; i++) {
3272 struct hist_field *save_val = hist_data->max_vars[i]->val;
3273 struct hist_field *save_var = hist_data->max_vars[i]->var;
3276 save_var_idx = save_var->var.idx;
3278 val = tracing_map_read_var(elt, save_var_idx);
3280 if (save_val->flags & HIST_FIELD_FL_STRING) {
3281 seq_printf(m, " %s: %-32s", save_var->var.name,
3282 (char *)(uintptr_t)(val));
3284 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3288 static void onmax_save(struct hist_trigger_data *hist_data,
3289 struct tracing_map_elt *elt, void *rec,
3290 struct ring_buffer_event *rbe,
3291 struct action_data *data, u64 *var_ref_vals)
3293 unsigned int max_idx = data->onmax.max_var->var.idx;
3294 unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
3296 u64 var_val, max_val;
3298 var_val = var_ref_vals[max_var_ref_idx];
3299 max_val = tracing_map_read_var(elt, max_idx);
3301 if (var_val <= max_val)
3304 tracing_map_set_var(elt, max_idx, var_val);
3306 update_max_vars(hist_data, elt, rbe, rec);
3309 static void onmax_destroy(struct action_data *data)
3313 destroy_hist_field(data->onmax.max_var, 0);
3314 destroy_hist_field(data->onmax.var, 0);
3316 kfree(data->onmax.var_str);
3317 kfree(data->onmax.fn_name);
3319 for (i = 0; i < data->n_params; i++)
3320 kfree(data->params[i]);
3325 static int onmax_create(struct hist_trigger_data *hist_data,
3326 struct action_data *data)
3328 struct trace_event_file *file = hist_data->event_file;
3329 struct hist_field *var_field, *ref_field, *max_var;
3330 unsigned int var_ref_idx = hist_data->n_var_refs;
3331 struct field_var *field_var;
3332 char *onmax_var_str, *param;
3333 unsigned long flags;
3337 onmax_var_str = data->onmax.var_str;
3338 if (onmax_var_str[0] != '$') {
3339 hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
3344 var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
3346 hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
3350 flags = HIST_FIELD_FL_VAR_REF;
3351 ref_field = create_hist_field(hist_data, NULL, flags, NULL);
3355 if (init_var_ref(ref_field, var_field, NULL, NULL)) {
3356 destroy_hist_field(ref_field, 0);
3360 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3361 ref_field->var_ref_idx = hist_data->n_var_refs++;
3362 data->onmax.var = ref_field;
3364 data->fn = onmax_save;
3365 data->onmax.max_var_ref_idx = var_ref_idx;
3366 max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
3367 if (IS_ERR(max_var)) {
3368 hist_err("onmax: Couldn't create onmax variable: ", "max");
3369 ret = PTR_ERR(max_var);
3372 data->onmax.max_var = max_var;
3374 for (i = 0; i < data->n_params; i++) {
3375 param = kstrdup(data->params[i], GFP_KERNEL);
3381 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3382 if (IS_ERR(field_var)) {
3383 hist_err("onmax: Couldn't create field variable: ", param);
3384 ret = PTR_ERR(field_var);
3389 hist_data->max_vars[hist_data->n_max_vars++] = field_var;
3390 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3391 hist_data->n_max_var_str++;
3399 static int parse_action_params(char *params, struct action_data *data)
3401 char *param, *saved_param;
3405 if (data->n_params >= SYNTH_FIELDS_MAX)
3408 param = strsep(¶ms, ",");
3414 param = strstrip(param);
3415 if (strlen(param) < 2) {
3416 hist_err("Invalid action param: ", param);
3421 saved_param = kstrdup(param, GFP_KERNEL);
3427 data->params[data->n_params++] = saved_param;
3433 static struct action_data *onmax_parse(char *str)
3435 char *onmax_fn_name, *onmax_var_str;
3436 struct action_data *data;
3439 data = kzalloc(sizeof(*data), GFP_KERNEL);
3441 return ERR_PTR(-ENOMEM);
3443 onmax_var_str = strsep(&str, ")");
3444 if (!onmax_var_str || !str) {
3449 data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
3450 if (!data->onmax.var_str) {
3459 onmax_fn_name = strsep(&str, "(");
3460 if (!onmax_fn_name || !str)
3463 if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) {
3464 char *params = strsep(&str, ")");
3471 ret = parse_action_params(params, data);
3477 data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
3478 if (!data->onmax.fn_name) {
3485 onmax_destroy(data);
3486 data = ERR_PTR(ret);
3490 static void onmatch_destroy(struct action_data *data)
3494 mutex_lock(&synth_event_mutex);
3496 kfree(data->onmatch.match_event);
3497 kfree(data->onmatch.match_event_system);
3498 kfree(data->onmatch.synth_event_name);
3500 for (i = 0; i < data->n_params; i++)
3501 kfree(data->params[i]);
3503 if (data->onmatch.synth_event)
3504 data->onmatch.synth_event->ref--;
3508 mutex_unlock(&synth_event_mutex);
3511 static void destroy_field_var(struct field_var *field_var)
3516 destroy_hist_field(field_var->var, 0);
3517 destroy_hist_field(field_var->val, 0);
3522 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3526 for (i = 0; i < hist_data->n_field_vars; i++)
3527 destroy_field_var(hist_data->field_vars[i]);
3530 static void save_field_var(struct hist_trigger_data *hist_data,
3531 struct field_var *field_var)
3533 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3535 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3536 hist_data->n_field_var_str++;
3540 static void destroy_synth_var_refs(struct hist_trigger_data *hist_data)
3544 for (i = 0; i < hist_data->n_synth_var_refs; i++)
3545 destroy_hist_field(hist_data->synth_var_refs[i], 0);
3548 static void save_synth_var_ref(struct hist_trigger_data *hist_data,
3549 struct hist_field *var_ref)
3551 hist_data->synth_var_refs[hist_data->n_synth_var_refs++] = var_ref;
3553 hist_data->var_refs[hist_data->n_var_refs] = var_ref;
3554 var_ref->var_ref_idx = hist_data->n_var_refs++;
3557 static int check_synth_field(struct synth_event *event,
3558 struct hist_field *hist_field,
3559 unsigned int field_pos)
3561 struct synth_field *field;
3563 if (field_pos >= event->n_fields)
3566 field = event->fields[field_pos];
3568 if (strcmp(field->type, hist_field->type) != 0)
3574 static struct hist_field *
3575 onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
3576 char *system, char *event, char *var)
3578 struct hist_field *hist_field;
3580 var++; /* skip '$' */
3582 hist_field = find_target_event_var(hist_data, system, event, var);
3585 system = data->onmatch.match_event_system;
3586 event = data->onmatch.match_event;
3589 hist_field = find_event_var(hist_data, system, event, var);
3593 hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
3598 static struct hist_field *
3599 onmatch_create_field_var(struct hist_trigger_data *hist_data,
3600 struct action_data *data, char *system,
3601 char *event, char *var)
3603 struct hist_field *hist_field = NULL;
3604 struct field_var *field_var;
3607 * First try to create a field var on the target event (the
3608 * currently being defined). This will create a variable for
3609 * unqualified fields on the target event, or if qualified,
3610 * target fields that have qualified names matching the target.
3612 field_var = create_target_field_var(hist_data, system, event, var);
3614 if (field_var && !IS_ERR(field_var)) {
3615 save_field_var(hist_data, field_var);
3616 hist_field = field_var->var;
3620 * If no explicit system.event is specfied, default to
3621 * looking for fields on the onmatch(system.event.xxx)
3625 system = data->onmatch.match_event_system;
3626 event = data->onmatch.match_event;
3630 * At this point, we're looking at a field on another
3631 * event. Because we can't modify a hist trigger on
3632 * another event to add a variable for a field, we need
3633 * to create a new trigger on that event and create the
3634 * variable at the same time.
3636 hist_field = create_field_var_hist(hist_data, system, event, var);
3637 if (IS_ERR(hist_field))
3643 destroy_field_var(field_var);
3648 static int onmatch_create(struct hist_trigger_data *hist_data,
3649 struct trace_event_file *file,
3650 struct action_data *data)
3652 char *event_name, *param, *system = NULL;
3653 struct hist_field *hist_field, *var_ref;
3654 unsigned int i, var_ref_idx;
3655 unsigned int field_pos = 0;
3656 struct synth_event *event;
3659 mutex_lock(&synth_event_mutex);
3660 event = find_synth_event(data->onmatch.synth_event_name);
3662 hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
3663 mutex_unlock(&synth_event_mutex);
3667 mutex_unlock(&synth_event_mutex);
3669 var_ref_idx = hist_data->n_var_refs;
3671 for (i = 0; i < data->n_params; i++) {
3674 p = param = kstrdup(data->params[i], GFP_KERNEL);
3680 system = strsep(¶m, ".");
3682 param = (char *)system;
3683 system = event_name = NULL;
3685 event_name = strsep(¶m, ".");
3693 if (param[0] == '$')
3694 hist_field = onmatch_find_var(hist_data, data, system,
3697 hist_field = onmatch_create_field_var(hist_data, data,
3708 if (check_synth_field(event, hist_field, field_pos) == 0) {
3709 var_ref = create_var_ref(hist_field, system, event_name);
3716 save_synth_var_ref(hist_data, var_ref);
3722 hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
3723 system, event_name, param);
3729 if (field_pos != event->n_fields) {
3730 hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
3735 data->fn = action_trace;
3736 data->onmatch.synth_event = event;
3737 data->onmatch.var_ref_idx = var_ref_idx;
3741 mutex_lock(&synth_event_mutex);
3743 mutex_unlock(&synth_event_mutex);
3748 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3750 char *match_event, *match_event_system;
3751 char *synth_event_name, *params;
3752 struct action_data *data;
3755 data = kzalloc(sizeof(*data), GFP_KERNEL);
3757 return ERR_PTR(-ENOMEM);
3759 match_event = strsep(&str, ")");
3760 if (!match_event || !str) {
3761 hist_err("onmatch: Missing closing paren: ", match_event);
3765 match_event_system = strsep(&match_event, ".");
3767 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
3771 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3772 hist_err_event("onmatch: Invalid subsystem or event name: ",
3773 match_event_system, match_event, NULL);
3777 data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
3778 if (!data->onmatch.match_event) {
3783 data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
3784 if (!data->onmatch.match_event_system) {
3791 hist_err("onmatch: Missing . after onmatch(): ", str);
3795 synth_event_name = strsep(&str, "(");
3796 if (!synth_event_name || !str) {
3797 hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
3801 data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
3802 if (!data->onmatch.synth_event_name) {
3807 params = strsep(&str, ")");
3808 if (!params || !str || (str && strlen(str))) {
3809 hist_err("onmatch: Missing closing paramlist paren: ", params);
3813 ret = parse_action_params(params, data);
3819 onmatch_destroy(data);
3820 data = ERR_PTR(ret);
3824 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3826 hist_data->fields[HITCOUNT_IDX] =
3827 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3828 if (!hist_data->fields[HITCOUNT_IDX])
3831 hist_data->n_vals++;
3832 hist_data->n_fields++;
3834 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3840 static int __create_val_field(struct hist_trigger_data *hist_data,
3841 unsigned int val_idx,
3842 struct trace_event_file *file,
3843 char *var_name, char *field_str,
3844 unsigned long flags)
3846 struct hist_field *hist_field;
3849 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3850 if (IS_ERR(hist_field)) {
3851 ret = PTR_ERR(hist_field);
3855 hist_data->fields[val_idx] = hist_field;
3857 ++hist_data->n_vals;
3858 ++hist_data->n_fields;
3860 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3866 static int create_val_field(struct hist_trigger_data *hist_data,
3867 unsigned int val_idx,
3868 struct trace_event_file *file,
3871 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3874 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3877 static int create_var_field(struct hist_trigger_data *hist_data,
3878 unsigned int val_idx,
3879 struct trace_event_file *file,
3880 char *var_name, char *expr_str)
3882 unsigned long flags = 0;
3884 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3887 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3888 hist_err("Variable already defined: ", var_name);
3892 flags |= HIST_FIELD_FL_VAR;
3893 hist_data->n_vars++;
3894 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3897 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3900 static int create_val_fields(struct hist_trigger_data *hist_data,
3901 struct trace_event_file *file)
3903 char *fields_str, *field_str;
3904 unsigned int i, j = 1;
3907 ret = create_hitcount_val(hist_data);
3911 fields_str = hist_data->attrs->vals_str;
3915 strsep(&fields_str, "=");
3919 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3920 j < TRACING_MAP_VALS_MAX; i++) {
3921 field_str = strsep(&fields_str, ",");
3925 if (strcmp(field_str, "hitcount") == 0)
3928 ret = create_val_field(hist_data, j++, file, field_str);
3933 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3939 static int create_key_field(struct hist_trigger_data *hist_data,
3940 unsigned int key_idx,
3941 unsigned int key_offset,
3942 struct trace_event_file *file,
3945 struct hist_field *hist_field = NULL;
3947 unsigned long flags = 0;
3948 unsigned int key_size;
3951 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
3954 flags |= HIST_FIELD_FL_KEY;
3956 if (strcmp(field_str, "stacktrace") == 0) {
3957 flags |= HIST_FIELD_FL_STACKTRACE;
3958 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
3959 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
3961 hist_field = parse_expr(hist_data, file, field_str, flags,
3963 if (IS_ERR(hist_field)) {
3964 ret = PTR_ERR(hist_field);
3968 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
3969 hist_err("Using variable references as keys not supported: ", field_str);
3970 destroy_hist_field(hist_field, 0);
3975 key_size = hist_field->size;
3978 hist_data->fields[key_idx] = hist_field;
3980 key_size = ALIGN(key_size, sizeof(u64));
3981 hist_data->fields[key_idx]->size = key_size;
3982 hist_data->fields[key_idx]->offset = key_offset;
3984 hist_data->key_size += key_size;
3986 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3991 hist_data->n_keys++;
3992 hist_data->n_fields++;
3994 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4002 static int create_key_fields(struct hist_trigger_data *hist_data,
4003 struct trace_event_file *file)
4005 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4006 char *fields_str, *field_str;
4009 fields_str = hist_data->attrs->keys_str;
4013 strsep(&fields_str, "=");
4017 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4018 field_str = strsep(&fields_str, ",");
4021 ret = create_key_field(hist_data, i, key_offset,
4036 static int create_var_fields(struct hist_trigger_data *hist_data,
4037 struct trace_event_file *file)
4039 unsigned int i, j = hist_data->n_vals;
4042 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4044 for (i = 0; i < n_vars; i++) {
4045 char *var_name = hist_data->attrs->var_defs.name[i];
4046 char *expr = hist_data->attrs->var_defs.expr[i];
4048 ret = create_var_field(hist_data, j++, file, var_name, expr);
4056 static void free_var_defs(struct hist_trigger_data *hist_data)
4060 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4061 kfree(hist_data->attrs->var_defs.name[i]);
4062 kfree(hist_data->attrs->var_defs.expr[i]);
4065 hist_data->attrs->var_defs.n_vars = 0;
4068 static int parse_var_defs(struct hist_trigger_data *hist_data)
4070 char *s, *str, *var_name, *field_str;
4071 unsigned int i, j, n_vars = 0;
4074 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4075 str = hist_data->attrs->assignment_str[i];
4076 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4077 field_str = strsep(&str, ",");
4081 var_name = strsep(&field_str, "=");
4082 if (!var_name || !field_str) {
4083 hist_err("Malformed assignment: ", var_name);
4088 if (n_vars == TRACING_MAP_VARS_MAX) {
4089 hist_err("Too many variables defined: ", var_name);
4094 s = kstrdup(var_name, GFP_KERNEL);
4099 hist_data->attrs->var_defs.name[n_vars] = s;
4101 s = kstrdup(field_str, GFP_KERNEL);
4103 kfree(hist_data->attrs->var_defs.name[n_vars]);
4107 hist_data->attrs->var_defs.expr[n_vars++] = s;
4109 hist_data->attrs->var_defs.n_vars = n_vars;
4115 free_var_defs(hist_data);
4120 static int create_hist_fields(struct hist_trigger_data *hist_data,
4121 struct trace_event_file *file)
4125 ret = parse_var_defs(hist_data);
4129 ret = create_val_fields(hist_data, file);
4133 ret = create_var_fields(hist_data, file);
4137 ret = create_key_fields(hist_data, file);
4141 free_var_defs(hist_data);
4146 static int is_descending(const char *str)
4151 if (strcmp(str, "descending") == 0)
4154 if (strcmp(str, "ascending") == 0)
4160 static int create_sort_keys(struct hist_trigger_data *hist_data)
4162 char *fields_str = hist_data->attrs->sort_key_str;
4163 struct tracing_map_sort_key *sort_key;
4164 int descending, ret = 0;
4165 unsigned int i, j, k;
4167 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4172 strsep(&fields_str, "=");
4178 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4179 struct hist_field *hist_field;
4180 char *field_str, *field_name;
4181 const char *test_name;
4183 sort_key = &hist_data->sort_keys[i];
4185 field_str = strsep(&fields_str, ",");
4192 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4197 field_name = strsep(&field_str, ".");
4203 if (strcmp(field_name, "hitcount") == 0) {
4204 descending = is_descending(field_str);
4205 if (descending < 0) {
4209 sort_key->descending = descending;
4213 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4216 hist_field = hist_data->fields[j];
4217 if (hist_field->flags & HIST_FIELD_FL_VAR)
4222 test_name = hist_field_name(hist_field, 0);
4224 if (strcmp(field_name, test_name) == 0) {
4225 sort_key->field_idx = idx;
4226 descending = is_descending(field_str);
4227 if (descending < 0) {
4231 sort_key->descending = descending;
4235 if (j == hist_data->n_fields) {
4241 hist_data->n_sort_keys = i;
4246 static void destroy_actions(struct hist_trigger_data *hist_data)
4250 for (i = 0; i < hist_data->n_actions; i++) {
4251 struct action_data *data = hist_data->actions[i];
4253 if (data->fn == action_trace)
4254 onmatch_destroy(data);
4255 else if (data->fn == onmax_save)
4256 onmax_destroy(data);
4262 static int parse_actions(struct hist_trigger_data *hist_data)
4264 struct trace_array *tr = hist_data->event_file->tr;
4265 struct action_data *data;
4270 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4271 str = hist_data->attrs->action_str[i];
4273 if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) {
4274 char *action_str = str + strlen("onmatch(");
4276 data = onmatch_parse(tr, action_str);
4278 ret = PTR_ERR(data);
4281 data->fn = action_trace;
4282 } else if (strncmp(str, "onmax(", strlen("onmax(")) == 0) {
4283 char *action_str = str + strlen("onmax(");
4285 data = onmax_parse(action_str);
4287 ret = PTR_ERR(data);
4290 data->fn = onmax_save;
4296 hist_data->actions[hist_data->n_actions++] = data;
4302 static int create_actions(struct hist_trigger_data *hist_data,
4303 struct trace_event_file *file)
4305 struct action_data *data;
4309 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4310 data = hist_data->actions[i];
4312 if (data->fn == action_trace) {
4313 ret = onmatch_create(hist_data, file, data);
4316 } else if (data->fn == onmax_save) {
4317 ret = onmax_create(hist_data, data);
4326 static void print_actions(struct seq_file *m,
4327 struct hist_trigger_data *hist_data,
4328 struct tracing_map_elt *elt)
4332 for (i = 0; i < hist_data->n_actions; i++) {
4333 struct action_data *data = hist_data->actions[i];
4335 if (data->fn == onmax_save)
4336 onmax_print(m, hist_data, elt, data);
4340 static void print_onmax_spec(struct seq_file *m,
4341 struct hist_trigger_data *hist_data,
4342 struct action_data *data)
4346 seq_puts(m, ":onmax(");
4347 seq_printf(m, "%s", data->onmax.var_str);
4348 seq_printf(m, ").%s(", data->onmax.fn_name);
4350 for (i = 0; i < hist_data->n_max_vars; i++) {
4351 seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
4352 if (i < hist_data->n_max_vars - 1)
4358 static void print_onmatch_spec(struct seq_file *m,
4359 struct hist_trigger_data *hist_data,
4360 struct action_data *data)
4364 seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
4365 data->onmatch.match_event);
4367 seq_printf(m, "%s(", data->onmatch.synth_event->name);
4369 for (i = 0; i < data->n_params; i++) {
4372 seq_printf(m, "%s", data->params[i]);
4378 static bool actions_match(struct hist_trigger_data *hist_data,
4379 struct hist_trigger_data *hist_data_test)
4383 if (hist_data->n_actions != hist_data_test->n_actions)
4386 for (i = 0; i < hist_data->n_actions; i++) {
4387 struct action_data *data = hist_data->actions[i];
4388 struct action_data *data_test = hist_data_test->actions[i];
4390 if (data->fn != data_test->fn)
4393 if (data->n_params != data_test->n_params)
4396 for (j = 0; j < data->n_params; j++) {
4397 if (strcmp(data->params[j], data_test->params[j]) != 0)
4401 if (data->fn == action_trace) {
4402 if (strcmp(data->onmatch.synth_event_name,
4403 data_test->onmatch.synth_event_name) != 0)
4405 if (strcmp(data->onmatch.match_event_system,
4406 data_test->onmatch.match_event_system) != 0)
4408 if (strcmp(data->onmatch.match_event,
4409 data_test->onmatch.match_event) != 0)
4411 } else if (data->fn == onmax_save) {
4412 if (strcmp(data->onmax.var_str,
4413 data_test->onmax.var_str) != 0)
4415 if (strcmp(data->onmax.fn_name,
4416 data_test->onmax.fn_name) != 0)
4425 static void print_actions_spec(struct seq_file *m,
4426 struct hist_trigger_data *hist_data)
4430 for (i = 0; i < hist_data->n_actions; i++) {
4431 struct action_data *data = hist_data->actions[i];
4433 if (data->fn == action_trace)
4434 print_onmatch_spec(m, hist_data, data);
4435 else if (data->fn == onmax_save)
4436 print_onmax_spec(m, hist_data, data);
4440 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4444 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4445 kfree(hist_data->field_var_hists[i]->cmd);
4446 kfree(hist_data->field_var_hists[i]);
4450 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4455 destroy_hist_trigger_attrs(hist_data->attrs);
4456 destroy_hist_fields(hist_data);
4457 tracing_map_destroy(hist_data->map);
4459 destroy_actions(hist_data);
4460 destroy_field_vars(hist_data);
4461 destroy_field_var_hists(hist_data);
4462 destroy_synth_var_refs(hist_data);
4467 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4469 struct tracing_map *map = hist_data->map;
4470 struct ftrace_event_field *field;
4471 struct hist_field *hist_field;
4474 for_each_hist_field(i, hist_data) {
4475 hist_field = hist_data->fields[i];
4476 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4477 tracing_map_cmp_fn_t cmp_fn;
4479 field = hist_field->field;
4481 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4482 cmp_fn = tracing_map_cmp_none;
4484 cmp_fn = tracing_map_cmp_num(hist_field->size,
4485 hist_field->is_signed);
4486 else if (is_string_field(field))
4487 cmp_fn = tracing_map_cmp_string;
4489 cmp_fn = tracing_map_cmp_num(field->size,
4491 idx = tracing_map_add_key_field(map,
4494 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4495 idx = tracing_map_add_sum_field(map);
4500 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4501 idx = tracing_map_add_var(map);
4504 hist_field->var.idx = idx;
4505 hist_field->var.hist_data = hist_data;
4512 static struct hist_trigger_data *
4513 create_hist_data(unsigned int map_bits,
4514 struct hist_trigger_attrs *attrs,
4515 struct trace_event_file *file,
4518 const struct tracing_map_ops *map_ops = NULL;
4519 struct hist_trigger_data *hist_data;
4522 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4524 return ERR_PTR(-ENOMEM);
4526 hist_data->attrs = attrs;
4527 hist_data->remove = remove;
4528 hist_data->event_file = file;
4530 ret = parse_actions(hist_data);
4534 ret = create_hist_fields(hist_data, file);
4538 ret = create_sort_keys(hist_data);
4542 map_ops = &hist_trigger_elt_data_ops;
4544 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4545 map_ops, hist_data);
4546 if (IS_ERR(hist_data->map)) {
4547 ret = PTR_ERR(hist_data->map);
4548 hist_data->map = NULL;
4552 ret = create_tracing_map_fields(hist_data);
4558 hist_data->attrs = NULL;
4560 destroy_hist_data(hist_data);
4562 hist_data = ERR_PTR(ret);
4567 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4568 struct tracing_map_elt *elt, void *rec,
4569 struct ring_buffer_event *rbe,
4572 struct hist_elt_data *elt_data;
4573 struct hist_field *hist_field;
4574 unsigned int i, var_idx;
4577 elt_data = elt->private_data;
4578 elt_data->var_ref_vals = var_ref_vals;
4580 for_each_hist_val_field(i, hist_data) {
4581 hist_field = hist_data->fields[i];
4582 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4583 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4584 var_idx = hist_field->var.idx;
4585 tracing_map_set_var(elt, var_idx, hist_val);
4588 tracing_map_update_sum(elt, i, hist_val);
4591 for_each_hist_key_field(i, hist_data) {
4592 hist_field = hist_data->fields[i];
4593 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4594 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4595 var_idx = hist_field->var.idx;
4596 tracing_map_set_var(elt, var_idx, hist_val);
4600 update_field_vars(hist_data, elt, rbe, rec);
4603 static inline void add_to_key(char *compound_key, void *key,
4604 struct hist_field *key_field, void *rec)
4606 size_t size = key_field->size;
4608 if (key_field->flags & HIST_FIELD_FL_STRING) {
4609 struct ftrace_event_field *field;
4611 field = key_field->field;
4612 if (field->filter_type == FILTER_DYN_STRING)
4613 size = *(u32 *)(rec + field->offset) >> 16;
4614 else if (field->filter_type == FILTER_PTR_STRING)
4616 else if (field->filter_type == FILTER_STATIC_STRING)
4619 /* ensure NULL-termination */
4620 if (size > key_field->size - 1)
4621 size = key_field->size - 1;
4624 memcpy(compound_key + key_field->offset, key, size);
4628 hist_trigger_actions(struct hist_trigger_data *hist_data,
4629 struct tracing_map_elt *elt, void *rec,
4630 struct ring_buffer_event *rbe, u64 *var_ref_vals)
4632 struct action_data *data;
4635 for (i = 0; i < hist_data->n_actions; i++) {
4636 data = hist_data->actions[i];
4637 data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
4641 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4642 struct ring_buffer_event *rbe)
4644 struct hist_trigger_data *hist_data = data->private_data;
4645 bool use_compound_key = (hist_data->n_keys > 1);
4646 unsigned long entries[HIST_STACKTRACE_DEPTH];
4647 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4648 char compound_key[HIST_KEY_SIZE_MAX];
4649 struct tracing_map_elt *elt = NULL;
4650 struct stack_trace stacktrace;
4651 struct hist_field *key_field;
4656 memset(compound_key, 0, hist_data->key_size);
4658 for_each_hist_key_field(i, hist_data) {
4659 key_field = hist_data->fields[i];
4661 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4662 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
4663 stacktrace.entries = entries;
4664 stacktrace.nr_entries = 0;
4665 stacktrace.skip = HIST_STACKTRACE_SKIP;
4667 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
4668 save_stack_trace(&stacktrace);
4672 field_contents = key_field->fn(key_field, elt, rbe, rec);
4673 if (key_field->flags & HIST_FIELD_FL_STRING) {
4674 key = (void *)(unsigned long)field_contents;
4675 use_compound_key = true;
4677 key = (void *)&field_contents;
4680 if (use_compound_key)
4681 add_to_key(compound_key, key, key_field, rec);
4684 if (use_compound_key)
4687 if (hist_data->n_var_refs &&
4688 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4691 elt = tracing_map_insert(hist_data->map, key);
4695 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4697 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4698 hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
4701 static void hist_trigger_stacktrace_print(struct seq_file *m,
4702 unsigned long *stacktrace_entries,
4703 unsigned int max_entries)
4705 char str[KSYM_SYMBOL_LEN];
4706 unsigned int spaces = 8;
4709 for (i = 0; i < max_entries; i++) {
4710 if (stacktrace_entries[i] == ULONG_MAX)
4713 seq_printf(m, "%*c", 1 + spaces, ' ');
4714 sprint_symbol(str, stacktrace_entries[i]);
4715 seq_printf(m, "%s\n", str);
4720 hist_trigger_entry_print(struct seq_file *m,
4721 struct hist_trigger_data *hist_data, void *key,
4722 struct tracing_map_elt *elt)
4724 struct hist_field *key_field;
4725 char str[KSYM_SYMBOL_LEN];
4726 bool multiline = false;
4727 const char *field_name;
4733 for_each_hist_key_field(i, hist_data) {
4734 key_field = hist_data->fields[i];
4736 if (i > hist_data->n_vals)
4739 field_name = hist_field_name(key_field, 0);
4741 if (key_field->flags & HIST_FIELD_FL_HEX) {
4742 uval = *(u64 *)(key + key_field->offset);
4743 seq_printf(m, "%s: %llx", field_name, uval);
4744 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4745 uval = *(u64 *)(key + key_field->offset);
4746 sprint_symbol_no_offset(str, uval);
4747 seq_printf(m, "%s: [%llx] %-45s", field_name,
4749 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4750 uval = *(u64 *)(key + key_field->offset);
4751 sprint_symbol(str, uval);
4752 seq_printf(m, "%s: [%llx] %-55s", field_name,
4754 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4755 struct hist_elt_data *elt_data = elt->private_data;
4758 if (WARN_ON_ONCE(!elt_data))
4761 comm = elt_data->comm;
4763 uval = *(u64 *)(key + key_field->offset);
4764 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4766 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4767 const char *syscall_name;
4769 uval = *(u64 *)(key + key_field->offset);
4770 syscall_name = get_syscall_name(uval);
4772 syscall_name = "unknown_syscall";
4774 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4775 syscall_name, uval);
4776 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4777 seq_puts(m, "stacktrace:\n");
4778 hist_trigger_stacktrace_print(m,
4779 key + key_field->offset,
4780 HIST_STACKTRACE_DEPTH);
4782 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4783 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4784 *(u64 *)(key + key_field->offset));
4785 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4786 seq_printf(m, "%s: %-50s", field_name,
4787 (char *)(key + key_field->offset));
4789 uval = *(u64 *)(key + key_field->offset);
4790 seq_printf(m, "%s: %10llu", field_name, uval);
4799 seq_printf(m, " hitcount: %10llu",
4800 tracing_map_read_sum(elt, HITCOUNT_IDX));
4802 for (i = 1; i < hist_data->n_vals; i++) {
4803 field_name = hist_field_name(hist_data->fields[i], 0);
4805 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4806 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4809 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4810 seq_printf(m, " %s: %10llx", field_name,
4811 tracing_map_read_sum(elt, i));
4813 seq_printf(m, " %s: %10llu", field_name,
4814 tracing_map_read_sum(elt, i));
4818 print_actions(m, hist_data, elt);
4823 static int print_entries(struct seq_file *m,
4824 struct hist_trigger_data *hist_data)
4826 struct tracing_map_sort_entry **sort_entries = NULL;
4827 struct tracing_map *map = hist_data->map;
4830 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4831 hist_data->n_sort_keys,
4836 for (i = 0; i < n_entries; i++)
4837 hist_trigger_entry_print(m, hist_data,
4838 sort_entries[i]->key,
4839 sort_entries[i]->elt);
4841 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4846 static void hist_trigger_show(struct seq_file *m,
4847 struct event_trigger_data *data, int n)
4849 struct hist_trigger_data *hist_data;
4853 seq_puts(m, "\n\n");
4855 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4856 data->ops->print(m, data->ops, data);
4857 seq_puts(m, "#\n\n");
4859 hist_data = data->private_data;
4860 n_entries = print_entries(m, hist_data);
4864 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4865 (u64)atomic64_read(&hist_data->map->hits),
4866 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4869 static int hist_show(struct seq_file *m, void *v)
4871 struct event_trigger_data *data;
4872 struct trace_event_file *event_file;
4875 mutex_lock(&event_mutex);
4877 event_file = event_file_data(m->private);
4878 if (unlikely(!event_file)) {
4883 list_for_each_entry_rcu(data, &event_file->triggers, list) {
4884 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4885 hist_trigger_show(m, data, n++);
4888 if (have_hist_err()) {
4889 seq_printf(m, "\nERROR: %s\n", hist_err_str);
4890 seq_printf(m, " Last command: %s\n", last_hist_cmd);
4894 mutex_unlock(&event_mutex);
4899 static int event_hist_open(struct inode *inode, struct file *file)
4901 return single_open(file, hist_show, file);
4904 const struct file_operations event_hist_fops = {
4905 .open = event_hist_open,
4907 .llseek = seq_lseek,
4908 .release = single_release,
4911 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
4913 const char *field_name = hist_field_name(hist_field, 0);
4915 if (hist_field->var.name)
4916 seq_printf(m, "%s=", hist_field->var.name);
4918 if (hist_field->flags & HIST_FIELD_FL_CPU)
4920 else if (field_name) {
4921 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
4922 hist_field->flags & HIST_FIELD_FL_ALIAS)
4924 seq_printf(m, "%s", field_name);
4925 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
4926 seq_puts(m, "common_timestamp");
4928 if (hist_field->flags) {
4929 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
4930 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
4931 const char *flags = get_hist_field_flags(hist_field);
4934 seq_printf(m, ".%s", flags);
4939 static int event_hist_trigger_print(struct seq_file *m,
4940 struct event_trigger_ops *ops,
4941 struct event_trigger_data *data)
4943 struct hist_trigger_data *hist_data = data->private_data;
4944 struct hist_field *field;
4945 bool have_var = false;
4948 seq_puts(m, "hist:");
4951 seq_printf(m, "%s:", data->name);
4953 seq_puts(m, "keys=");
4955 for_each_hist_key_field(i, hist_data) {
4956 field = hist_data->fields[i];
4958 if (i > hist_data->n_vals)
4961 if (field->flags & HIST_FIELD_FL_STACKTRACE)
4962 seq_puts(m, "stacktrace");
4964 hist_field_print(m, field);
4967 seq_puts(m, ":vals=");
4969 for_each_hist_val_field(i, hist_data) {
4970 field = hist_data->fields[i];
4971 if (field->flags & HIST_FIELD_FL_VAR) {
4976 if (i == HITCOUNT_IDX)
4977 seq_puts(m, "hitcount");
4980 hist_field_print(m, field);
4989 for_each_hist_val_field(i, hist_data) {
4990 field = hist_data->fields[i];
4992 if (field->flags & HIST_FIELD_FL_VAR) {
4995 hist_field_print(m, field);
5000 seq_puts(m, ":sort=");
5002 for (i = 0; i < hist_data->n_sort_keys; i++) {
5003 struct tracing_map_sort_key *sort_key;
5004 unsigned int idx, first_key_idx;
5007 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5009 sort_key = &hist_data->sort_keys[i];
5010 idx = sort_key->field_idx;
5012 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5018 if (idx == HITCOUNT_IDX)
5019 seq_puts(m, "hitcount");
5021 if (idx >= first_key_idx)
5022 idx += hist_data->n_vars;
5023 hist_field_print(m, hist_data->fields[idx]);
5026 if (sort_key->descending)
5027 seq_puts(m, ".descending");
5029 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5030 if (hist_data->enable_timestamps)
5031 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5033 print_actions_spec(m, hist_data);
5035 if (data->filter_str)
5036 seq_printf(m, " if %s", data->filter_str);
5039 seq_puts(m, " [paused]");
5041 seq_puts(m, " [active]");
5048 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5049 struct event_trigger_data *data)
5051 struct hist_trigger_data *hist_data = data->private_data;
5053 if (!data->ref && hist_data->attrs->name)
5054 save_named_trigger(hist_data->attrs->name, data);
5061 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5063 struct trace_event_file *file;
5068 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5069 file = hist_data->field_var_hists[i]->hist_data->event_file;
5070 cmd = hist_data->field_var_hists[i]->cmd;
5071 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5072 "!hist", "hist", cmd);
5076 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5077 struct event_trigger_data *data)
5079 struct hist_trigger_data *hist_data = data->private_data;
5081 if (WARN_ON_ONCE(data->ref <= 0))
5087 del_named_trigger(data);
5089 trigger_data_free(data);
5091 remove_hist_vars(hist_data);
5093 unregister_field_var_hists(hist_data);
5095 destroy_hist_data(hist_data);
5099 static struct event_trigger_ops event_hist_trigger_ops = {
5100 .func = event_hist_trigger,
5101 .print = event_hist_trigger_print,
5102 .init = event_hist_trigger_init,
5103 .free = event_hist_trigger_free,
5106 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5107 struct event_trigger_data *data)
5111 save_named_trigger(data->named_data->name, data);
5113 event_hist_trigger_init(ops, data->named_data);
5118 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5119 struct event_trigger_data *data)
5121 if (WARN_ON_ONCE(data->ref <= 0))
5124 event_hist_trigger_free(ops, data->named_data);
5128 del_named_trigger(data);
5129 trigger_data_free(data);
5133 static struct event_trigger_ops event_hist_trigger_named_ops = {
5134 .func = event_hist_trigger,
5135 .print = event_hist_trigger_print,
5136 .init = event_hist_trigger_named_init,
5137 .free = event_hist_trigger_named_free,
5140 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5143 return &event_hist_trigger_ops;
5146 static void hist_clear(struct event_trigger_data *data)
5148 struct hist_trigger_data *hist_data = data->private_data;
5151 pause_named_trigger(data);
5153 tracepoint_synchronize_unregister();
5155 tracing_map_clear(hist_data->map);
5158 unpause_named_trigger(data);
5161 static bool compatible_field(struct ftrace_event_field *field,
5162 struct ftrace_event_field *test_field)
5164 if (field == test_field)
5166 if (field == NULL || test_field == NULL)
5168 if (strcmp(field->name, test_field->name) != 0)
5170 if (strcmp(field->type, test_field->type) != 0)
5172 if (field->size != test_field->size)
5174 if (field->is_signed != test_field->is_signed)
5180 static bool hist_trigger_match(struct event_trigger_data *data,
5181 struct event_trigger_data *data_test,
5182 struct event_trigger_data *named_data,
5185 struct tracing_map_sort_key *sort_key, *sort_key_test;
5186 struct hist_trigger_data *hist_data, *hist_data_test;
5187 struct hist_field *key_field, *key_field_test;
5190 if (named_data && (named_data != data_test) &&
5191 (named_data != data_test->named_data))
5194 if (!named_data && is_named_trigger(data_test))
5197 hist_data = data->private_data;
5198 hist_data_test = data_test->private_data;
5200 if (hist_data->n_vals != hist_data_test->n_vals ||
5201 hist_data->n_fields != hist_data_test->n_fields ||
5202 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5205 if (!ignore_filter) {
5206 if ((data->filter_str && !data_test->filter_str) ||
5207 (!data->filter_str && data_test->filter_str))
5211 for_each_hist_field(i, hist_data) {
5212 key_field = hist_data->fields[i];
5213 key_field_test = hist_data_test->fields[i];
5215 if (key_field->flags != key_field_test->flags)
5217 if (!compatible_field(key_field->field, key_field_test->field))
5219 if (key_field->offset != key_field_test->offset)
5221 if (key_field->size != key_field_test->size)
5223 if (key_field->is_signed != key_field_test->is_signed)
5225 if (!!key_field->var.name != !!key_field_test->var.name)
5227 if (key_field->var.name &&
5228 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5232 for (i = 0; i < hist_data->n_sort_keys; i++) {
5233 sort_key = &hist_data->sort_keys[i];
5234 sort_key_test = &hist_data_test->sort_keys[i];
5236 if (sort_key->field_idx != sort_key_test->field_idx ||
5237 sort_key->descending != sort_key_test->descending)
5241 if (!ignore_filter && data->filter_str &&
5242 (strcmp(data->filter_str, data_test->filter_str) != 0))
5245 if (!actions_match(hist_data, hist_data_test))
5251 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5252 struct event_trigger_data *data,
5253 struct trace_event_file *file)
5255 struct hist_trigger_data *hist_data = data->private_data;
5256 struct event_trigger_data *test, *named_data = NULL;
5259 if (hist_data->attrs->name) {
5260 named_data = find_named_trigger(hist_data->attrs->name);
5262 if (!hist_trigger_match(data, named_data, named_data,
5264 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
5271 if (hist_data->attrs->name && !named_data)
5274 list_for_each_entry_rcu(test, &file->triggers, list) {
5275 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5276 if (!hist_trigger_match(data, test, named_data, false))
5278 if (hist_data->attrs->pause)
5279 test->paused = true;
5280 else if (hist_data->attrs->cont)
5281 test->paused = false;
5282 else if (hist_data->attrs->clear)
5285 hist_err("Hist trigger already exists", NULL);
5292 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5293 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
5298 if (hist_data->attrs->pause)
5299 data->paused = true;
5302 data->private_data = named_data->private_data;
5303 set_named_trigger_data(data, named_data);
5304 data->ops = &event_hist_trigger_named_ops;
5307 if (data->ops->init) {
5308 ret = data->ops->init(data->ops, data);
5313 if (hist_data->enable_timestamps) {
5314 char *clock = hist_data->attrs->clock;
5316 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5318 hist_err("Couldn't set trace_clock: ", clock);
5322 tracing_set_time_stamp_abs(file->tr, true);
5326 destroy_hist_data(hist_data);
5333 static int hist_trigger_enable(struct event_trigger_data *data,
5334 struct trace_event_file *file)
5338 list_add_tail_rcu(&data->list, &file->triggers);
5340 update_cond_flag(file);
5342 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5343 list_del_rcu(&data->list);
5344 update_cond_flag(file);
5351 static bool have_hist_trigger_match(struct event_trigger_data *data,
5352 struct trace_event_file *file)
5354 struct hist_trigger_data *hist_data = data->private_data;
5355 struct event_trigger_data *test, *named_data = NULL;
5358 if (hist_data->attrs->name)
5359 named_data = find_named_trigger(hist_data->attrs->name);
5361 list_for_each_entry_rcu(test, &file->triggers, list) {
5362 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5363 if (hist_trigger_match(data, test, named_data, false)) {
5373 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5374 struct trace_event_file *file)
5376 struct hist_trigger_data *hist_data = data->private_data;
5377 struct event_trigger_data *test, *named_data = NULL;
5379 if (hist_data->attrs->name)
5380 named_data = find_named_trigger(hist_data->attrs->name);
5382 list_for_each_entry_rcu(test, &file->triggers, list) {
5383 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5384 if (!hist_trigger_match(data, test, named_data, false))
5386 hist_data = test->private_data;
5387 if (check_var_refs(hist_data))
5396 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5397 struct event_trigger_data *data,
5398 struct trace_event_file *file)
5400 struct hist_trigger_data *hist_data = data->private_data;
5401 struct event_trigger_data *test, *named_data = NULL;
5402 bool unregistered = false;
5404 if (hist_data->attrs->name)
5405 named_data = find_named_trigger(hist_data->attrs->name);
5407 list_for_each_entry_rcu(test, &file->triggers, list) {
5408 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5409 if (!hist_trigger_match(data, test, named_data, false))
5411 unregistered = true;
5412 list_del_rcu(&test->list);
5413 trace_event_trigger_enable_disable(file, 0);
5414 update_cond_flag(file);
5419 if (unregistered && test->ops->free)
5420 test->ops->free(test->ops, test);
5422 if (hist_data->enable_timestamps) {
5423 if (!hist_data->remove || unregistered)
5424 tracing_set_time_stamp_abs(file->tr, false);
5428 static bool hist_file_check_refs(struct trace_event_file *file)
5430 struct hist_trigger_data *hist_data;
5431 struct event_trigger_data *test;
5433 list_for_each_entry_rcu(test, &file->triggers, list) {
5434 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5435 hist_data = test->private_data;
5436 if (check_var_refs(hist_data))
5444 static void hist_unreg_all(struct trace_event_file *file)
5446 struct event_trigger_data *test, *n;
5447 struct hist_trigger_data *hist_data;
5448 struct synth_event *se;
5449 const char *se_name;
5451 if (hist_file_check_refs(file))
5454 list_for_each_entry_safe(test, n, &file->triggers, list) {
5455 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5456 hist_data = test->private_data;
5457 list_del_rcu(&test->list);
5458 trace_event_trigger_enable_disable(file, 0);
5460 mutex_lock(&synth_event_mutex);
5461 se_name = trace_event_name(file->event_call);
5462 se = find_synth_event(se_name);
5465 mutex_unlock(&synth_event_mutex);
5467 update_cond_flag(file);
5468 if (hist_data->enable_timestamps)
5469 tracing_set_time_stamp_abs(file->tr, false);
5470 if (test->ops->free)
5471 test->ops->free(test->ops, test);
5476 static int event_hist_trigger_func(struct event_command *cmd_ops,
5477 struct trace_event_file *file,
5478 char *glob, char *cmd, char *param)
5480 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5481 struct event_trigger_data *trigger_data;
5482 struct hist_trigger_attrs *attrs;
5483 struct event_trigger_ops *trigger_ops;
5484 struct hist_trigger_data *hist_data;
5485 struct synth_event *se;
5486 const char *se_name;
5487 bool remove = false;
5491 if (glob && strlen(glob)) {
5492 last_cmd_set(param);
5503 * separate the trigger from the filter (k:v [if filter])
5504 * allowing for whitespace in the trigger
5506 p = trigger = param;
5508 p = strstr(p, "if");
5513 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5517 if (p >= param + strlen(param) - strlen("if") - 1)
5519 if (*(p + strlen("if")) != ' ' && *(p + strlen("if")) != '\t') {
5530 param = strstrip(p);
5531 trigger = strstrip(trigger);
5534 attrs = parse_hist_trigger_attrs(trigger);
5536 return PTR_ERR(attrs);
5538 if (attrs->map_bits)
5539 hist_trigger_bits = attrs->map_bits;
5541 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5542 if (IS_ERR(hist_data)) {
5543 destroy_hist_trigger_attrs(attrs);
5544 return PTR_ERR(hist_data);
5547 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5549 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5550 if (!trigger_data) {
5555 trigger_data->count = -1;
5556 trigger_data->ops = trigger_ops;
5557 trigger_data->cmd_ops = cmd_ops;
5559 INIT_LIST_HEAD(&trigger_data->list);
5560 RCU_INIT_POINTER(trigger_data->filter, NULL);
5562 trigger_data->private_data = hist_data;
5564 /* if param is non-empty, it's supposed to be a filter */
5565 if (param && cmd_ops->set_filter) {
5566 ret = cmd_ops->set_filter(param, trigger_data, file);
5572 if (!have_hist_trigger_match(trigger_data, file))
5575 if (hist_trigger_check_refs(trigger_data, file)) {
5580 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5582 mutex_lock(&synth_event_mutex);
5583 se_name = trace_event_name(file->event_call);
5584 se = find_synth_event(se_name);
5587 mutex_unlock(&synth_event_mutex);
5593 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5595 * The above returns on success the # of triggers registered,
5596 * but if it didn't register any it returns zero. Consider no
5597 * triggers registered a failure too.
5600 if (!(attrs->pause || attrs->cont || attrs->clear))
5606 if (get_named_trigger_data(trigger_data))
5609 if (has_hist_vars(hist_data))
5610 save_hist_vars(hist_data);
5612 ret = create_actions(hist_data, file);
5616 ret = tracing_map_init(hist_data->map);
5620 ret = hist_trigger_enable(trigger_data, file);
5624 mutex_lock(&synth_event_mutex);
5625 se_name = trace_event_name(file->event_call);
5626 se = find_synth_event(se_name);
5629 mutex_unlock(&synth_event_mutex);
5631 /* Just return zero, not the number of registered triggers */
5639 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5641 if (cmd_ops->set_filter)
5642 cmd_ops->set_filter(NULL, trigger_data, NULL);
5644 remove_hist_vars(hist_data);
5646 kfree(trigger_data);
5648 destroy_hist_data(hist_data);
5652 static struct event_command trigger_hist_cmd = {
5654 .trigger_type = ETT_EVENT_HIST,
5655 .flags = EVENT_CMD_FL_NEEDS_REC,
5656 .func = event_hist_trigger_func,
5657 .reg = hist_register_trigger,
5658 .unreg = hist_unregister_trigger,
5659 .unreg_all = hist_unreg_all,
5660 .get_trigger_ops = event_hist_get_trigger_ops,
5661 .set_filter = set_trigger_filter,
5664 __init int register_trigger_hist_cmd(void)
5668 ret = register_event_command(&trigger_hist_cmd);
5675 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5676 struct ring_buffer_event *event)
5678 struct enable_trigger_data *enable_data = data->private_data;
5679 struct event_trigger_data *test;
5681 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
5682 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5683 if (enable_data->enable)
5684 test->paused = false;
5686 test->paused = true;
5692 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5693 struct ring_buffer_event *event)
5698 if (data->count != -1)
5701 hist_enable_trigger(data, rec, event);
5704 static struct event_trigger_ops hist_enable_trigger_ops = {
5705 .func = hist_enable_trigger,
5706 .print = event_enable_trigger_print,
5707 .init = event_trigger_init,
5708 .free = event_enable_trigger_free,
5711 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5712 .func = hist_enable_count_trigger,
5713 .print = event_enable_trigger_print,
5714 .init = event_trigger_init,
5715 .free = event_enable_trigger_free,
5718 static struct event_trigger_ops hist_disable_trigger_ops = {
5719 .func = hist_enable_trigger,
5720 .print = event_enable_trigger_print,
5721 .init = event_trigger_init,
5722 .free = event_enable_trigger_free,
5725 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5726 .func = hist_enable_count_trigger,
5727 .print = event_enable_trigger_print,
5728 .init = event_trigger_init,
5729 .free = event_enable_trigger_free,
5732 static struct event_trigger_ops *
5733 hist_enable_get_trigger_ops(char *cmd, char *param)
5735 struct event_trigger_ops *ops;
5738 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5741 ops = param ? &hist_enable_count_trigger_ops :
5742 &hist_enable_trigger_ops;
5744 ops = param ? &hist_disable_count_trigger_ops :
5745 &hist_disable_trigger_ops;
5750 static void hist_enable_unreg_all(struct trace_event_file *file)
5752 struct event_trigger_data *test, *n;
5754 list_for_each_entry_safe(test, n, &file->triggers, list) {
5755 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5756 list_del_rcu(&test->list);
5757 update_cond_flag(file);
5758 trace_event_trigger_enable_disable(file, 0);
5759 if (test->ops->free)
5760 test->ops->free(test->ops, test);
5765 static struct event_command trigger_hist_enable_cmd = {
5766 .name = ENABLE_HIST_STR,
5767 .trigger_type = ETT_HIST_ENABLE,
5768 .func = event_enable_trigger_func,
5769 .reg = event_enable_register_trigger,
5770 .unreg = event_enable_unregister_trigger,
5771 .unreg_all = hist_enable_unreg_all,
5772 .get_trigger_ops = hist_enable_get_trigger_ops,
5773 .set_filter = set_trigger_filter,
5776 static struct event_command trigger_hist_disable_cmd = {
5777 .name = DISABLE_HIST_STR,
5778 .trigger_type = ETT_HIST_ENABLE,
5779 .func = event_enable_trigger_func,
5780 .reg = event_enable_register_trigger,
5781 .unreg = event_enable_unregister_trigger,
5782 .unreg_all = hist_enable_unreg_all,
5783 .get_trigger_ops = hist_enable_get_trigger_ops,
5784 .set_filter = set_trigger_filter,
5787 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5789 unregister_event_command(&trigger_hist_enable_cmd);
5790 unregister_event_command(&trigger_hist_disable_cmd);
5793 __init int register_trigger_hist_enable_disable_cmds(void)
5797 ret = register_event_command(&trigger_hist_enable_cmd);
5798 if (WARN_ON(ret < 0))
5800 ret = register_event_command(&trigger_hist_disable_cmd);
5801 if (WARN_ON(ret < 0))
5802 unregister_trigger_hist_enable_disable_cmds();
5807 static __init int trace_events_hist_init(void)
5809 struct dentry *entry = NULL;
5810 struct dentry *d_tracer;
5813 d_tracer = tracing_init_dentry();
5814 if (IS_ERR(d_tracer)) {
5815 err = PTR_ERR(d_tracer);
5819 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
5820 NULL, &synth_events_fops);
5828 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
5833 fs_initcall(trace_events_hist_init);