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(char *field_type,
744 struct synth_field *field;
748 if (field_type[0] == ';')
751 len = strlen(field_name);
752 if (field_name[len - 1] == ';')
753 field_name[len - 1] = '\0';
755 field = kzalloc(sizeof(*field), GFP_KERNEL);
757 return ERR_PTR(-ENOMEM);
759 len = strlen(field_type) + 1;
760 array = strchr(field_name, '[');
762 len += strlen(array);
763 field->type = kzalloc(len, GFP_KERNEL);
768 strcat(field->type, field_type);
770 strcat(field->type, array);
774 field->size = synth_field_size(field->type);
780 if (synth_field_is_string(field->type))
781 field->is_string = true;
783 field->is_signed = synth_field_signed(field->type);
785 field->name = kstrdup(field_name, GFP_KERNEL);
793 free_synth_field(field);
794 field = ERR_PTR(ret);
798 static void free_synth_tracepoint(struct tracepoint *tp)
807 static struct tracepoint *alloc_synth_tracepoint(char *name)
809 struct tracepoint *tp;
811 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
813 return ERR_PTR(-ENOMEM);
815 tp->name = kstrdup(name, GFP_KERNEL);
818 return ERR_PTR(-ENOMEM);
824 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
825 unsigned int var_ref_idx);
827 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
828 unsigned int var_ref_idx)
830 struct tracepoint *tp = event->tp;
832 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
833 struct tracepoint_func *probe_func_ptr;
834 synth_probe_func_t probe_func;
837 if (!(cpu_online(raw_smp_processor_id())))
840 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
841 if (probe_func_ptr) {
843 probe_func = probe_func_ptr->func;
844 __data = probe_func_ptr->data;
845 probe_func(__data, var_ref_vals, var_ref_idx);
846 } while ((++probe_func_ptr)->func);
851 static struct synth_event *find_synth_event(const char *name)
853 struct synth_event *event;
855 list_for_each_entry(event, &synth_event_list, list) {
856 if (strcmp(event->name, name) == 0)
863 static int register_synth_event(struct synth_event *event)
865 struct trace_event_call *call = &event->call;
868 event->call.class = &event->class;
869 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
870 if (!event->class.system) {
875 event->tp = alloc_synth_tracepoint(event->name);
876 if (IS_ERR(event->tp)) {
877 ret = PTR_ERR(event->tp);
882 INIT_LIST_HEAD(&call->class->fields);
883 call->event.funcs = &synth_event_funcs;
884 call->class->define_fields = synth_event_define_fields;
886 ret = register_trace_event(&call->event);
891 call->flags = TRACE_EVENT_FL_TRACEPOINT;
892 call->class->reg = trace_event_reg;
893 call->class->probe = trace_event_raw_event_synth;
895 call->tp = event->tp;
897 ret = trace_add_event_call(call);
899 pr_warn("Failed to register synthetic event: %s\n",
900 trace_event_name(call));
904 ret = set_synth_event_print_fmt(call);
906 trace_remove_event_call(call);
912 unregister_trace_event(&call->event);
916 static int unregister_synth_event(struct synth_event *event)
918 struct trace_event_call *call = &event->call;
921 ret = trace_remove_event_call(call);
926 static void free_synth_event(struct synth_event *event)
933 for (i = 0; i < event->n_fields; i++)
934 free_synth_field(event->fields[i]);
936 kfree(event->fields);
938 kfree(event->class.system);
939 free_synth_tracepoint(event->tp);
940 free_synth_event_print_fmt(&event->call);
944 static struct synth_event *alloc_synth_event(char *event_name, int n_fields,
945 struct synth_field **fields)
947 struct synth_event *event;
950 event = kzalloc(sizeof(*event), GFP_KERNEL);
952 event = ERR_PTR(-ENOMEM);
956 event->name = kstrdup(event_name, GFP_KERNEL);
959 event = ERR_PTR(-ENOMEM);
963 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
964 if (!event->fields) {
965 free_synth_event(event);
966 event = ERR_PTR(-ENOMEM);
970 for (i = 0; i < n_fields; i++)
971 event->fields[i] = fields[i];
973 event->n_fields = n_fields;
978 static void action_trace(struct hist_trigger_data *hist_data,
979 struct tracing_map_elt *elt, void *rec,
980 struct ring_buffer_event *rbe,
981 struct action_data *data, u64 *var_ref_vals)
983 struct synth_event *event = data->onmatch.synth_event;
985 trace_synth(event, var_ref_vals, data->onmatch.var_ref_idx);
988 struct hist_var_data {
989 struct list_head list;
990 struct hist_trigger_data *hist_data;
993 static void add_or_delete_synth_event(struct synth_event *event, int delete)
996 free_synth_event(event);
998 mutex_lock(&synth_event_mutex);
999 if (!find_synth_event(event->name))
1000 list_add(&event->list, &synth_event_list);
1002 free_synth_event(event);
1003 mutex_unlock(&synth_event_mutex);
1007 static int create_synth_event(int argc, char **argv)
1009 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1010 struct synth_event *event = NULL;
1011 bool delete_event = false;
1012 int i, n_fields = 0, ret = 0;
1015 mutex_lock(&synth_event_mutex);
1019 * - Add synthetic event: <event_name> field[;field] ...
1020 * - Remove synthetic event: !<event_name> field[;field] ...
1021 * where 'field' = type field_name
1029 if (name[0] == '!') {
1030 delete_event = true;
1034 event = find_synth_event(name);
1042 list_del(&event->list);
1048 } else if (delete_event)
1056 for (i = 1; i < argc - 1; i++) {
1057 if (strcmp(argv[i], ";") == 0)
1059 if (n_fields == SYNTH_FIELDS_MAX) {
1064 field = parse_synth_field(argv[i], argv[i + 1]);
1065 if (IS_ERR(field)) {
1066 ret = PTR_ERR(field);
1069 fields[n_fields] = field;
1078 event = alloc_synth_event(name, n_fields, fields);
1079 if (IS_ERR(event)) {
1080 ret = PTR_ERR(event);
1085 mutex_unlock(&synth_event_mutex);
1089 ret = unregister_synth_event(event);
1090 add_or_delete_synth_event(event, !ret);
1092 ret = register_synth_event(event);
1093 add_or_delete_synth_event(event, ret);
1099 mutex_unlock(&synth_event_mutex);
1101 for (i = 0; i < n_fields; i++)
1102 free_synth_field(fields[i]);
1103 free_synth_event(event);
1108 static int release_all_synth_events(void)
1110 struct list_head release_events;
1111 struct synth_event *event, *e;
1114 INIT_LIST_HEAD(&release_events);
1116 mutex_lock(&synth_event_mutex);
1118 list_for_each_entry(event, &synth_event_list, list) {
1120 mutex_unlock(&synth_event_mutex);
1125 list_splice_init(&event->list, &release_events);
1127 mutex_unlock(&synth_event_mutex);
1129 list_for_each_entry_safe(event, e, &release_events, list) {
1130 list_del(&event->list);
1132 ret = unregister_synth_event(event);
1133 add_or_delete_synth_event(event, !ret);
1140 static void *synth_events_seq_start(struct seq_file *m, loff_t *pos)
1142 mutex_lock(&synth_event_mutex);
1144 return seq_list_start(&synth_event_list, *pos);
1147 static void *synth_events_seq_next(struct seq_file *m, void *v, loff_t *pos)
1149 return seq_list_next(v, &synth_event_list, pos);
1152 static void synth_events_seq_stop(struct seq_file *m, void *v)
1154 mutex_unlock(&synth_event_mutex);
1157 static int synth_events_seq_show(struct seq_file *m, void *v)
1159 struct synth_field *field;
1160 struct synth_event *event = v;
1163 seq_printf(m, "%s\t", event->name);
1165 for (i = 0; i < event->n_fields; i++) {
1166 field = event->fields[i];
1168 /* parameter values */
1169 seq_printf(m, "%s %s%s", field->type, field->name,
1170 i == event->n_fields - 1 ? "" : "; ");
1178 static const struct seq_operations synth_events_seq_op = {
1179 .start = synth_events_seq_start,
1180 .next = synth_events_seq_next,
1181 .stop = synth_events_seq_stop,
1182 .show = synth_events_seq_show
1185 static int synth_events_open(struct inode *inode, struct file *file)
1189 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1190 ret = release_all_synth_events();
1195 return seq_open(file, &synth_events_seq_op);
1198 static ssize_t synth_events_write(struct file *file,
1199 const char __user *buffer,
1200 size_t count, loff_t *ppos)
1202 return trace_parse_run_command(file, buffer, count, ppos,
1203 create_synth_event);
1206 static const struct file_operations synth_events_fops = {
1207 .open = synth_events_open,
1208 .write = synth_events_write,
1210 .llseek = seq_lseek,
1211 .release = seq_release,
1214 static u64 hist_field_timestamp(struct hist_field *hist_field,
1215 struct tracing_map_elt *elt,
1216 struct ring_buffer_event *rbe,
1219 struct hist_trigger_data *hist_data = hist_field->hist_data;
1220 struct trace_array *tr = hist_data->event_file->tr;
1222 u64 ts = ring_buffer_event_time_stamp(rbe);
1224 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1230 static u64 hist_field_cpu(struct hist_field *hist_field,
1231 struct tracing_map_elt *elt,
1232 struct ring_buffer_event *rbe,
1235 int cpu = smp_processor_id();
1240 static struct hist_field *
1241 check_field_for_var_ref(struct hist_field *hist_field,
1242 struct hist_trigger_data *var_data,
1243 unsigned int var_idx)
1245 struct hist_field *found = NULL;
1247 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF) {
1248 if (hist_field->var.idx == var_idx &&
1249 hist_field->var.hist_data == var_data) {
1257 static struct hist_field *
1258 check_field_for_var_refs(struct hist_trigger_data *hist_data,
1259 struct hist_field *hist_field,
1260 struct hist_trigger_data *var_data,
1261 unsigned int var_idx,
1264 struct hist_field *found = NULL;
1273 found = check_field_for_var_ref(hist_field, var_data, var_idx);
1277 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1278 struct hist_field *operand;
1280 operand = hist_field->operands[i];
1281 found = check_field_for_var_refs(hist_data, operand, var_data,
1282 var_idx, level + 1);
1290 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1291 struct hist_trigger_data *var_data,
1292 unsigned int var_idx)
1294 struct hist_field *hist_field, *found = NULL;
1297 for_each_hist_field(i, hist_data) {
1298 hist_field = hist_data->fields[i];
1299 found = check_field_for_var_refs(hist_data, hist_field,
1300 var_data, var_idx, 0);
1305 for (i = 0; i < hist_data->n_synth_var_refs; i++) {
1306 hist_field = hist_data->synth_var_refs[i];
1307 found = check_field_for_var_refs(hist_data, hist_field,
1308 var_data, var_idx, 0);
1316 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1317 unsigned int var_idx)
1319 struct trace_array *tr = hist_data->event_file->tr;
1320 struct hist_field *found = NULL;
1321 struct hist_var_data *var_data;
1323 list_for_each_entry(var_data, &tr->hist_vars, list) {
1324 if (var_data->hist_data == hist_data)
1326 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1334 static bool check_var_refs(struct hist_trigger_data *hist_data)
1336 struct hist_field *field;
1340 for_each_hist_field(i, hist_data) {
1341 field = hist_data->fields[i];
1342 if (field && field->flags & HIST_FIELD_FL_VAR) {
1343 if (find_any_var_ref(hist_data, field->var.idx)) {
1353 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1355 struct trace_array *tr = hist_data->event_file->tr;
1356 struct hist_var_data *var_data, *found = NULL;
1358 list_for_each_entry(var_data, &tr->hist_vars, list) {
1359 if (var_data->hist_data == hist_data) {
1368 static bool field_has_hist_vars(struct hist_field *hist_field,
1379 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1380 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1383 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1384 struct hist_field *operand;
1386 operand = hist_field->operands[i];
1387 if (field_has_hist_vars(operand, level + 1))
1394 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1396 struct hist_field *hist_field;
1399 for_each_hist_field(i, hist_data) {
1400 hist_field = hist_data->fields[i];
1401 if (field_has_hist_vars(hist_field, 0))
1408 static int save_hist_vars(struct hist_trigger_data *hist_data)
1410 struct trace_array *tr = hist_data->event_file->tr;
1411 struct hist_var_data *var_data;
1413 var_data = find_hist_vars(hist_data);
1417 if (trace_array_get(tr) < 0)
1420 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1422 trace_array_put(tr);
1426 var_data->hist_data = hist_data;
1427 list_add(&var_data->list, &tr->hist_vars);
1432 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1434 struct trace_array *tr = hist_data->event_file->tr;
1435 struct hist_var_data *var_data;
1437 var_data = find_hist_vars(hist_data);
1441 if (WARN_ON(check_var_refs(hist_data)))
1444 list_del(&var_data->list);
1448 trace_array_put(tr);
1451 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1452 const char *var_name)
1454 struct hist_field *hist_field, *found = NULL;
1457 for_each_hist_field(i, hist_data) {
1458 hist_field = hist_data->fields[i];
1459 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1460 strcmp(hist_field->var.name, var_name) == 0) {
1469 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1470 struct trace_event_file *file,
1471 const char *var_name)
1473 struct hist_trigger_data *test_data;
1474 struct event_trigger_data *test;
1475 struct hist_field *hist_field;
1477 hist_field = find_var_field(hist_data, var_name);
1481 list_for_each_entry_rcu(test, &file->triggers, list) {
1482 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1483 test_data = test->private_data;
1484 hist_field = find_var_field(test_data, var_name);
1493 static struct trace_event_file *find_var_file(struct trace_array *tr,
1498 struct hist_trigger_data *var_hist_data;
1499 struct hist_var_data *var_data;
1500 struct trace_event_file *file, *found = NULL;
1503 return find_event_file(tr, system, event_name);
1505 list_for_each_entry(var_data, &tr->hist_vars, list) {
1506 var_hist_data = var_data->hist_data;
1507 file = var_hist_data->event_file;
1511 if (find_var_field(var_hist_data, var_name)) {
1513 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1524 static struct hist_field *find_file_var(struct trace_event_file *file,
1525 const char *var_name)
1527 struct hist_trigger_data *test_data;
1528 struct event_trigger_data *test;
1529 struct hist_field *hist_field;
1531 list_for_each_entry_rcu(test, &file->triggers, list) {
1532 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1533 test_data = test->private_data;
1534 hist_field = find_var_field(test_data, var_name);
1543 static struct hist_field *
1544 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1546 struct trace_array *tr = hist_data->event_file->tr;
1547 struct hist_field *hist_field, *found = NULL;
1548 struct trace_event_file *file;
1551 for (i = 0; i < hist_data->n_actions; i++) {
1552 struct action_data *data = hist_data->actions[i];
1554 if (data->fn == action_trace) {
1555 char *system = data->onmatch.match_event_system;
1556 char *event_name = data->onmatch.match_event;
1558 file = find_var_file(tr, system, event_name, var_name);
1561 hist_field = find_file_var(file, var_name);
1564 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1565 return ERR_PTR(-EINVAL);
1575 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1580 struct trace_array *tr = hist_data->event_file->tr;
1581 struct hist_field *hist_field = NULL;
1582 struct trace_event_file *file;
1584 if (!system || !event_name) {
1585 hist_field = find_match_var(hist_data, var_name);
1586 if (IS_ERR(hist_field))
1592 file = find_var_file(tr, system, event_name, var_name);
1596 hist_field = find_file_var(file, var_name);
1601 struct hist_elt_data {
1604 char *field_var_str[SYNTH_FIELDS_MAX];
1607 static u64 hist_field_var_ref(struct hist_field *hist_field,
1608 struct tracing_map_elt *elt,
1609 struct ring_buffer_event *rbe,
1612 struct hist_elt_data *elt_data;
1615 elt_data = elt->private_data;
1616 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1621 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1622 u64 *var_ref_vals, bool self)
1624 struct hist_trigger_data *var_data;
1625 struct tracing_map_elt *var_elt;
1626 struct hist_field *hist_field;
1627 unsigned int i, var_idx;
1628 bool resolved = true;
1631 for (i = 0; i < hist_data->n_var_refs; i++) {
1632 hist_field = hist_data->var_refs[i];
1633 var_idx = hist_field->var.idx;
1634 var_data = hist_field->var.hist_data;
1636 if (var_data == NULL) {
1641 if ((self && var_data != hist_data) ||
1642 (!self && var_data == hist_data))
1645 var_elt = tracing_map_lookup(var_data->map, key);
1651 if (!tracing_map_var_set(var_elt, var_idx)) {
1656 if (self || !hist_field->read_once)
1657 var_val = tracing_map_read_var(var_elt, var_idx);
1659 var_val = tracing_map_read_var_once(var_elt, var_idx);
1661 var_ref_vals[i] = var_val;
1667 static const char *hist_field_name(struct hist_field *field,
1670 const char *field_name = "";
1676 field_name = field->field->name;
1677 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1678 field->flags & HIST_FIELD_FL_ALIAS)
1679 field_name = hist_field_name(field->operands[0], ++level);
1680 else if (field->flags & HIST_FIELD_FL_CPU)
1682 else if (field->flags & HIST_FIELD_FL_EXPR ||
1683 field->flags & HIST_FIELD_FL_VAR_REF) {
1684 if (field->system) {
1685 static char full_name[MAX_FILTER_STR_VAL];
1687 strcat(full_name, field->system);
1688 strcat(full_name, ".");
1689 strcat(full_name, field->event_name);
1690 strcat(full_name, ".");
1691 strcat(full_name, field->name);
1692 field_name = full_name;
1694 field_name = field->name;
1695 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1696 field_name = "common_timestamp";
1698 if (field_name == NULL)
1704 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1706 hist_field_fn_t fn = NULL;
1708 switch (field_size) {
1710 if (field_is_signed)
1711 fn = hist_field_s64;
1713 fn = hist_field_u64;
1716 if (field_is_signed)
1717 fn = hist_field_s32;
1719 fn = hist_field_u32;
1722 if (field_is_signed)
1723 fn = hist_field_s16;
1725 fn = hist_field_u16;
1728 if (field_is_signed)
1738 static int parse_map_size(char *str)
1740 unsigned long size, map_bits;
1749 ret = kstrtoul(str, 0, &size);
1753 map_bits = ilog2(roundup_pow_of_two(size));
1754 if (map_bits < TRACING_MAP_BITS_MIN ||
1755 map_bits > TRACING_MAP_BITS_MAX)
1763 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1770 for (i = 0; i < attrs->n_assignments; i++)
1771 kfree(attrs->assignment_str[i]);
1773 for (i = 0; i < attrs->n_actions; i++)
1774 kfree(attrs->action_str[i]);
1777 kfree(attrs->sort_key_str);
1778 kfree(attrs->keys_str);
1779 kfree(attrs->vals_str);
1780 kfree(attrs->clock);
1784 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1788 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1791 if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) ||
1792 (strncmp(str, "onmax(", strlen("onmax(")) == 0)) {
1793 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1794 if (!attrs->action_str[attrs->n_actions]) {
1805 static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
1809 if ((strncmp(str, "key=", strlen("key=")) == 0) ||
1810 (strncmp(str, "keys=", strlen("keys=")) == 0)) {
1811 attrs->keys_str = kstrdup(str, GFP_KERNEL);
1812 if (!attrs->keys_str) {
1816 } else if ((strncmp(str, "val=", strlen("val=")) == 0) ||
1817 (strncmp(str, "vals=", strlen("vals=")) == 0) ||
1818 (strncmp(str, "values=", strlen("values=")) == 0)) {
1819 attrs->vals_str = kstrdup(str, GFP_KERNEL);
1820 if (!attrs->vals_str) {
1824 } else if (strncmp(str, "sort=", strlen("sort=")) == 0) {
1825 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
1826 if (!attrs->sort_key_str) {
1830 } else if (strncmp(str, "name=", strlen("name=")) == 0) {
1831 attrs->name = kstrdup(str, GFP_KERNEL);
1836 } else if (strncmp(str, "clock=", strlen("clock=")) == 0) {
1843 str = strstrip(str);
1844 attrs->clock = kstrdup(str, GFP_KERNEL);
1845 if (!attrs->clock) {
1849 } else if (strncmp(str, "size=", strlen("size=")) == 0) {
1850 int map_bits = parse_map_size(str);
1856 attrs->map_bits = map_bits;
1860 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1861 hist_err("Too many variables defined: ", str);
1866 assignment = kstrdup(str, GFP_KERNEL);
1872 attrs->assignment_str[attrs->n_assignments++] = assignment;
1878 static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
1880 struct hist_trigger_attrs *attrs;
1883 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1885 return ERR_PTR(-ENOMEM);
1887 while (trigger_str) {
1888 char *str = strsep(&trigger_str, ":");
1890 if (strchr(str, '=')) {
1891 ret = parse_assignment(str, attrs);
1894 } else if (strcmp(str, "pause") == 0)
1895 attrs->pause = true;
1896 else if ((strcmp(str, "cont") == 0) ||
1897 (strcmp(str, "continue") == 0))
1899 else if (strcmp(str, "clear") == 0)
1900 attrs->clear = true;
1902 ret = parse_action(str, attrs);
1908 if (!attrs->keys_str) {
1913 if (!attrs->clock) {
1914 attrs->clock = kstrdup("global", GFP_KERNEL);
1915 if (!attrs->clock) {
1923 destroy_hist_trigger_attrs(attrs);
1925 return ERR_PTR(ret);
1928 static inline void save_comm(char *comm, struct task_struct *task)
1931 strcpy(comm, "<idle>");
1935 if (WARN_ON_ONCE(task->pid < 0)) {
1936 strcpy(comm, "<XXX>");
1940 memcpy(comm, task->comm, TASK_COMM_LEN);
1943 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1947 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1948 kfree(elt_data->field_var_str[i]);
1950 kfree(elt_data->comm);
1954 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1956 struct hist_elt_data *elt_data = elt->private_data;
1958 hist_elt_data_free(elt_data);
1961 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1963 struct hist_trigger_data *hist_data = elt->map->private_data;
1964 unsigned int size = TASK_COMM_LEN;
1965 struct hist_elt_data *elt_data;
1966 struct hist_field *key_field;
1967 unsigned int i, n_str;
1969 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1973 for_each_hist_key_field(i, hist_data) {
1974 key_field = hist_data->fields[i];
1976 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
1977 elt_data->comm = kzalloc(size, GFP_KERNEL);
1978 if (!elt_data->comm) {
1986 n_str = hist_data->n_field_var_str + hist_data->n_max_var_str;
1988 size = STR_VAR_LEN_MAX;
1990 for (i = 0; i < n_str; i++) {
1991 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1992 if (!elt_data->field_var_str[i]) {
1993 hist_elt_data_free(elt_data);
1998 elt->private_data = elt_data;
2003 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2005 struct hist_elt_data *elt_data = elt->private_data;
2008 save_comm(elt_data->comm, current);
2011 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2012 .elt_alloc = hist_trigger_elt_data_alloc,
2013 .elt_free = hist_trigger_elt_data_free,
2014 .elt_init = hist_trigger_elt_data_init,
2017 static const char *get_hist_field_flags(struct hist_field *hist_field)
2019 const char *flags_str = NULL;
2021 if (hist_field->flags & HIST_FIELD_FL_HEX)
2023 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2025 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2026 flags_str = "sym-offset";
2027 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2028 flags_str = "execname";
2029 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2030 flags_str = "syscall";
2031 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2033 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2034 flags_str = "usecs";
2039 static void expr_field_str(struct hist_field *field, char *expr)
2041 if (field->flags & HIST_FIELD_FL_VAR_REF)
2044 strcat(expr, hist_field_name(field, 0));
2046 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2047 const char *flags_str = get_hist_field_flags(field);
2051 strcat(expr, flags_str);
2056 static char *expr_str(struct hist_field *field, unsigned int level)
2063 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2067 if (!field->operands[0]) {
2068 expr_field_str(field, expr);
2072 if (field->operator == FIELD_OP_UNARY_MINUS) {
2076 subexpr = expr_str(field->operands[0], ++level);
2081 strcat(expr, subexpr);
2089 expr_field_str(field->operands[0], expr);
2091 switch (field->operator) {
2092 case FIELD_OP_MINUS:
2103 expr_field_str(field->operands[1], expr);
2108 static int contains_operator(char *str)
2110 enum field_op_id field_op = FIELD_OP_NONE;
2113 op = strpbrk(str, "+-");
2115 return FIELD_OP_NONE;
2120 field_op = FIELD_OP_UNARY_MINUS;
2122 field_op = FIELD_OP_MINUS;
2125 field_op = FIELD_OP_PLUS;
2134 static void destroy_hist_field(struct hist_field *hist_field,
2145 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2146 destroy_hist_field(hist_field->operands[i], level + 1);
2148 kfree(hist_field->var.name);
2149 kfree(hist_field->name);
2150 kfree(hist_field->type);
2155 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2156 struct ftrace_event_field *field,
2157 unsigned long flags,
2160 struct hist_field *hist_field;
2162 if (field && is_function_field(field))
2165 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2169 hist_field->hist_data = hist_data;
2171 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2172 goto out; /* caller will populate */
2174 if (flags & HIST_FIELD_FL_VAR_REF) {
2175 hist_field->fn = hist_field_var_ref;
2179 if (flags & HIST_FIELD_FL_HITCOUNT) {
2180 hist_field->fn = hist_field_counter;
2181 hist_field->size = sizeof(u64);
2182 hist_field->type = kstrdup("u64", GFP_KERNEL);
2183 if (!hist_field->type)
2188 if (flags & HIST_FIELD_FL_STACKTRACE) {
2189 hist_field->fn = hist_field_none;
2193 if (flags & HIST_FIELD_FL_LOG2) {
2194 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2195 hist_field->fn = hist_field_log2;
2196 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2197 hist_field->size = hist_field->operands[0]->size;
2198 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2199 if (!hist_field->type)
2204 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2205 hist_field->fn = hist_field_timestamp;
2206 hist_field->size = sizeof(u64);
2207 hist_field->type = kstrdup("u64", GFP_KERNEL);
2208 if (!hist_field->type)
2213 if (flags & HIST_FIELD_FL_CPU) {
2214 hist_field->fn = hist_field_cpu;
2215 hist_field->size = sizeof(int);
2216 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2217 if (!hist_field->type)
2222 if (WARN_ON_ONCE(!field))
2225 if (is_string_field(field)) {
2226 flags |= HIST_FIELD_FL_STRING;
2228 hist_field->size = MAX_FILTER_STR_VAL;
2229 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2230 if (!hist_field->type)
2233 if (field->filter_type == FILTER_STATIC_STRING)
2234 hist_field->fn = hist_field_string;
2235 else if (field->filter_type == FILTER_DYN_STRING)
2236 hist_field->fn = hist_field_dynstring;
2238 hist_field->fn = hist_field_pstring;
2240 hist_field->size = field->size;
2241 hist_field->is_signed = field->is_signed;
2242 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2243 if (!hist_field->type)
2246 hist_field->fn = select_value_fn(field->size,
2248 if (!hist_field->fn) {
2249 destroy_hist_field(hist_field, 0);
2254 hist_field->field = field;
2255 hist_field->flags = flags;
2258 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2259 if (!hist_field->var.name)
2265 destroy_hist_field(hist_field, 0);
2269 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2273 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2274 if (hist_data->fields[i]) {
2275 destroy_hist_field(hist_data->fields[i], 0);
2276 hist_data->fields[i] = NULL;
2281 static int init_var_ref(struct hist_field *ref_field,
2282 struct hist_field *var_field,
2283 char *system, char *event_name)
2287 ref_field->var.idx = var_field->var.idx;
2288 ref_field->var.hist_data = var_field->hist_data;
2289 ref_field->size = var_field->size;
2290 ref_field->is_signed = var_field->is_signed;
2291 ref_field->flags |= var_field->flags &
2292 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2295 ref_field->system = kstrdup(system, GFP_KERNEL);
2296 if (!ref_field->system)
2301 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2302 if (!ref_field->event_name) {
2308 if (var_field->var.name) {
2309 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2310 if (!ref_field->name) {
2314 } else if (var_field->name) {
2315 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2316 if (!ref_field->name) {
2322 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2323 if (!ref_field->type) {
2330 kfree(ref_field->system);
2331 kfree(ref_field->event_name);
2332 kfree(ref_field->name);
2337 static struct hist_field *create_var_ref(struct hist_field *var_field,
2338 char *system, char *event_name)
2340 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2341 struct hist_field *ref_field;
2343 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2345 if (init_var_ref(ref_field, var_field, system, event_name)) {
2346 destroy_hist_field(ref_field, 0);
2354 static bool is_var_ref(char *var_name)
2356 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2362 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2368 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2369 name = hist_data->attrs->var_defs.name[i];
2371 if (strcmp(var_name, name) == 0) {
2372 field = hist_data->attrs->var_defs.expr[i];
2373 if (contains_operator(field) || is_var_ref(field))
2382 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2383 char *system, char *event_name,
2386 struct trace_event_call *call;
2388 if (system && event_name) {
2389 call = hist_data->event_file->event_call;
2391 if (strcmp(system, call->class->system) != 0)
2394 if (strcmp(event_name, trace_event_name(call)) != 0)
2398 if (!!system != !!event_name)
2401 if (!is_var_ref(var_name))
2406 return field_name_from_var(hist_data, var_name);
2409 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2410 char *system, char *event_name,
2413 struct hist_field *var_field = NULL, *ref_field = NULL;
2415 if (!is_var_ref(var_name))
2420 var_field = find_event_var(hist_data, system, event_name, var_name);
2422 ref_field = create_var_ref(var_field, system, event_name);
2425 hist_err_event("Couldn't find variable: $",
2426 system, event_name, var_name);
2431 static struct ftrace_event_field *
2432 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2433 char *field_str, unsigned long *flags)
2435 struct ftrace_event_field *field = NULL;
2436 char *field_name, *modifier, *str;
2438 modifier = str = kstrdup(field_str, GFP_KERNEL);
2440 return ERR_PTR(-ENOMEM);
2442 field_name = strsep(&modifier, ".");
2444 if (strcmp(modifier, "hex") == 0)
2445 *flags |= HIST_FIELD_FL_HEX;
2446 else if (strcmp(modifier, "sym") == 0)
2447 *flags |= HIST_FIELD_FL_SYM;
2448 else if (strcmp(modifier, "sym-offset") == 0)
2449 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2450 else if ((strcmp(modifier, "execname") == 0) &&
2451 (strcmp(field_name, "common_pid") == 0))
2452 *flags |= HIST_FIELD_FL_EXECNAME;
2453 else if (strcmp(modifier, "syscall") == 0)
2454 *flags |= HIST_FIELD_FL_SYSCALL;
2455 else if (strcmp(modifier, "log2") == 0)
2456 *flags |= HIST_FIELD_FL_LOG2;
2457 else if (strcmp(modifier, "usecs") == 0)
2458 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2460 hist_err("Invalid field modifier: ", modifier);
2461 field = ERR_PTR(-EINVAL);
2466 if (strcmp(field_name, "common_timestamp") == 0) {
2467 *flags |= HIST_FIELD_FL_TIMESTAMP;
2468 hist_data->enable_timestamps = true;
2469 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2470 hist_data->attrs->ts_in_usecs = true;
2471 } else if (strcmp(field_name, "cpu") == 0)
2472 *flags |= HIST_FIELD_FL_CPU;
2474 field = trace_find_event_field(file->event_call, field_name);
2475 if (!field || !field->size) {
2476 hist_err("Couldn't find field: ", field_name);
2477 field = ERR_PTR(-EINVAL);
2487 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2488 struct hist_field *var_ref,
2491 struct hist_field *alias = NULL;
2492 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2494 alias = create_hist_field(hist_data, NULL, flags, var_name);
2498 alias->fn = var_ref->fn;
2499 alias->operands[0] = var_ref;
2501 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2502 destroy_hist_field(alias, 0);
2509 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2510 struct trace_event_file *file, char *str,
2511 unsigned long *flags, char *var_name)
2513 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2514 struct ftrace_event_field *field = NULL;
2515 struct hist_field *hist_field = NULL;
2518 s = strchr(str, '.');
2520 s = strchr(++s, '.');
2522 ref_system = strsep(&str, ".");
2527 ref_event = strsep(&str, ".");
2536 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2538 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2540 hist_data->var_refs[hist_data->n_var_refs] = hist_field;
2541 hist_field->var_ref_idx = hist_data->n_var_refs++;
2543 hist_field = create_alias(hist_data, hist_field, var_name);
2554 field = parse_field(hist_data, file, str, flags);
2555 if (IS_ERR(field)) {
2556 ret = PTR_ERR(field);
2560 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2568 return ERR_PTR(ret);
2571 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2572 struct trace_event_file *file,
2573 char *str, unsigned long flags,
2574 char *var_name, unsigned int level);
2576 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2577 struct trace_event_file *file,
2578 char *str, unsigned long flags,
2579 char *var_name, unsigned int level)
2581 struct hist_field *operand1, *expr = NULL;
2582 unsigned long operand_flags;
2586 /* we support only -(xxx) i.e. explicit parens required */
2589 hist_err("Too many subexpressions (3 max): ", str);
2594 str++; /* skip leading '-' */
2596 s = strchr(str, '(');
2604 s = strrchr(str, ')');
2608 ret = -EINVAL; /* no closing ')' */
2612 flags |= HIST_FIELD_FL_EXPR;
2613 expr = create_hist_field(hist_data, NULL, flags, var_name);
2620 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2621 if (IS_ERR(operand1)) {
2622 ret = PTR_ERR(operand1);
2626 expr->flags |= operand1->flags &
2627 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2628 expr->fn = hist_field_unary_minus;
2629 expr->operands[0] = operand1;
2630 expr->operator = FIELD_OP_UNARY_MINUS;
2631 expr->name = expr_str(expr, 0);
2632 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2640 destroy_hist_field(expr, 0);
2641 return ERR_PTR(ret);
2644 static int check_expr_operands(struct hist_field *operand1,
2645 struct hist_field *operand2)
2647 unsigned long operand1_flags = operand1->flags;
2648 unsigned long operand2_flags = operand2->flags;
2650 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2651 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2652 struct hist_field *var;
2654 var = find_var_field(operand1->var.hist_data, operand1->name);
2657 operand1_flags = var->flags;
2660 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2661 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2662 struct hist_field *var;
2664 var = find_var_field(operand2->var.hist_data, operand2->name);
2667 operand2_flags = var->flags;
2670 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2671 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2672 hist_err("Timestamp units in expression don't match", NULL);
2679 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2680 struct trace_event_file *file,
2681 char *str, unsigned long flags,
2682 char *var_name, unsigned int level)
2684 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2685 unsigned long operand_flags;
2686 int field_op, ret = -EINVAL;
2687 char *sep, *operand1_str;
2690 hist_err("Too many subexpressions (3 max): ", str);
2691 return ERR_PTR(-EINVAL);
2694 field_op = contains_operator(str);
2696 if (field_op == FIELD_OP_NONE)
2697 return parse_atom(hist_data, file, str, &flags, var_name);
2699 if (field_op == FIELD_OP_UNARY_MINUS)
2700 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2703 case FIELD_OP_MINUS:
2713 operand1_str = strsep(&str, sep);
2714 if (!operand1_str || !str)
2718 operand1 = parse_atom(hist_data, file, operand1_str,
2719 &operand_flags, NULL);
2720 if (IS_ERR(operand1)) {
2721 ret = PTR_ERR(operand1);
2726 /* rest of string could be another expression e.g. b+c in a+b+c */
2728 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2729 if (IS_ERR(operand2)) {
2730 ret = PTR_ERR(operand2);
2735 ret = check_expr_operands(operand1, operand2);
2739 flags |= HIST_FIELD_FL_EXPR;
2741 flags |= operand1->flags &
2742 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2744 expr = create_hist_field(hist_data, NULL, flags, var_name);
2750 operand1->read_once = true;
2751 operand2->read_once = true;
2753 expr->operands[0] = operand1;
2754 expr->operands[1] = operand2;
2755 expr->operator = field_op;
2756 expr->name = expr_str(expr, 0);
2757 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2764 case FIELD_OP_MINUS:
2765 expr->fn = hist_field_minus;
2768 expr->fn = hist_field_plus;
2777 destroy_hist_field(operand1, 0);
2778 destroy_hist_field(operand2, 0);
2779 destroy_hist_field(expr, 0);
2781 return ERR_PTR(ret);
2784 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2785 struct trace_event_file *file)
2787 struct event_trigger_data *test;
2789 list_for_each_entry_rcu(test, &file->triggers, list) {
2790 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2791 if (test->private_data == hist_data)
2792 return test->filter_str;
2799 static struct event_command trigger_hist_cmd;
2800 static int event_hist_trigger_func(struct event_command *cmd_ops,
2801 struct trace_event_file *file,
2802 char *glob, char *cmd, char *param);
2804 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2805 struct hist_trigger_data *hist_data,
2806 unsigned int n_keys)
2808 struct hist_field *target_hist_field, *hist_field;
2809 unsigned int n, i, j;
2811 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2814 i = hist_data->n_vals;
2815 j = target_hist_data->n_vals;
2817 for (n = 0; n < n_keys; n++) {
2818 hist_field = hist_data->fields[i + n];
2819 target_hist_field = target_hist_data->fields[j + n];
2821 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2823 if (hist_field->size != target_hist_field->size)
2825 if (hist_field->is_signed != target_hist_field->is_signed)
2832 static struct hist_trigger_data *
2833 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2834 struct trace_event_file *file)
2836 struct hist_trigger_data *hist_data;
2837 struct event_trigger_data *test;
2838 unsigned int n_keys;
2840 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2842 list_for_each_entry_rcu(test, &file->triggers, list) {
2843 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2844 hist_data = test->private_data;
2846 if (compatible_keys(target_hist_data, hist_data, n_keys))
2854 static struct trace_event_file *event_file(struct trace_array *tr,
2855 char *system, char *event_name)
2857 struct trace_event_file *file;
2859 file = __find_event_file(tr, system, event_name);
2861 return ERR_PTR(-EINVAL);
2866 static struct hist_field *
2867 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2868 char *system, char *event_name, char *field_name)
2870 struct hist_field *event_var;
2871 char *synthetic_name;
2873 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2874 if (!synthetic_name)
2875 return ERR_PTR(-ENOMEM);
2877 strcpy(synthetic_name, "synthetic_");
2878 strcat(synthetic_name, field_name);
2880 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2882 kfree(synthetic_name);
2888 * create_field_var_hist - Automatically create a histogram and var for a field
2889 * @target_hist_data: The target hist trigger
2890 * @subsys_name: Optional subsystem name
2891 * @event_name: Optional event name
2892 * @field_name: The name of the field (and the resulting variable)
2894 * Hist trigger actions fetch data from variables, not directly from
2895 * events. However, for convenience, users are allowed to directly
2896 * specify an event field in an action, which will be automatically
2897 * converted into a variable on their behalf.
2899 * If a user specifies a field on an event that isn't the event the
2900 * histogram currently being defined (the target event histogram), the
2901 * only way that can be accomplished is if a new hist trigger is
2902 * created and the field variable defined on that.
2904 * This function creates a new histogram compatible with the target
2905 * event (meaning a histogram with the same key as the target
2906 * histogram), and creates a variable for the specified field, but
2907 * with 'synthetic_' prepended to the variable name in order to avoid
2908 * collision with normal field variables.
2910 * Return: The variable created for the field.
2912 static struct hist_field *
2913 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2914 char *subsys_name, char *event_name, char *field_name)
2916 struct trace_array *tr = target_hist_data->event_file->tr;
2917 struct hist_field *event_var = ERR_PTR(-EINVAL);
2918 struct hist_trigger_data *hist_data;
2919 unsigned int i, n, first = true;
2920 struct field_var_hist *var_hist;
2921 struct trace_event_file *file;
2922 struct hist_field *key_field;
2927 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2928 hist_err_event("onmatch: Too many field variables defined: ",
2929 subsys_name, event_name, field_name);
2930 return ERR_PTR(-EINVAL);
2933 file = event_file(tr, subsys_name, event_name);
2936 hist_err_event("onmatch: Event file not found: ",
2937 subsys_name, event_name, field_name);
2938 ret = PTR_ERR(file);
2939 return ERR_PTR(ret);
2943 * Look for a histogram compatible with target. We'll use the
2944 * found histogram specification to create a new matching
2945 * histogram with our variable on it. target_hist_data is not
2946 * yet a registered histogram so we can't use that.
2948 hist_data = find_compatible_hist(target_hist_data, file);
2950 hist_err_event("onmatch: Matching event histogram not found: ",
2951 subsys_name, event_name, field_name);
2952 return ERR_PTR(-EINVAL);
2955 /* See if a synthetic field variable has already been created */
2956 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2957 event_name, field_name);
2958 if (!IS_ERR_OR_NULL(event_var))
2961 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2963 return ERR_PTR(-ENOMEM);
2965 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2968 return ERR_PTR(-ENOMEM);
2971 /* Use the same keys as the compatible histogram */
2972 strcat(cmd, "keys=");
2974 for_each_hist_key_field(i, hist_data) {
2975 key_field = hist_data->fields[i];
2978 strcat(cmd, key_field->field->name);
2982 /* Create the synthetic field variable specification */
2983 strcat(cmd, ":synthetic_");
2984 strcat(cmd, field_name);
2986 strcat(cmd, field_name);
2988 /* Use the same filter as the compatible histogram */
2989 saved_filter = find_trigger_filter(hist_data, file);
2991 strcat(cmd, " if ");
2992 strcat(cmd, saved_filter);
2995 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
2996 if (!var_hist->cmd) {
2999 return ERR_PTR(-ENOMEM);
3002 /* Save the compatible histogram information */
3003 var_hist->hist_data = hist_data;
3005 /* Create the new histogram with our variable */
3006 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3010 kfree(var_hist->cmd);
3012 hist_err_event("onmatch: Couldn't create histogram for field: ",
3013 subsys_name, event_name, field_name);
3014 return ERR_PTR(ret);
3019 /* If we can't find the variable, something went wrong */
3020 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3021 event_name, field_name);
3022 if (IS_ERR_OR_NULL(event_var)) {
3023 kfree(var_hist->cmd);
3025 hist_err_event("onmatch: Couldn't find synthetic variable: ",
3026 subsys_name, event_name, field_name);
3027 return ERR_PTR(-EINVAL);
3030 n = target_hist_data->n_field_var_hists;
3031 target_hist_data->field_var_hists[n] = var_hist;
3032 target_hist_data->n_field_var_hists++;
3037 static struct hist_field *
3038 find_target_event_var(struct hist_trigger_data *hist_data,
3039 char *subsys_name, char *event_name, char *var_name)
3041 struct trace_event_file *file = hist_data->event_file;
3042 struct hist_field *hist_field = NULL;
3045 struct trace_event_call *call;
3050 call = file->event_call;
3052 if (strcmp(subsys_name, call->class->system) != 0)
3055 if (strcmp(event_name, trace_event_name(call)) != 0)
3059 hist_field = find_var_field(hist_data, var_name);
3064 static inline void __update_field_vars(struct tracing_map_elt *elt,
3065 struct ring_buffer_event *rbe,
3067 struct field_var **field_vars,
3068 unsigned int n_field_vars,
3069 unsigned int field_var_str_start)
3071 struct hist_elt_data *elt_data = elt->private_data;
3072 unsigned int i, j, var_idx;
3075 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3076 struct field_var *field_var = field_vars[i];
3077 struct hist_field *var = field_var->var;
3078 struct hist_field *val = field_var->val;
3080 var_val = val->fn(val, elt, rbe, rec);
3081 var_idx = var->var.idx;
3083 if (val->flags & HIST_FIELD_FL_STRING) {
3084 char *str = elt_data->field_var_str[j++];
3085 char *val_str = (char *)(uintptr_t)var_val;
3087 strscpy(str, val_str, STR_VAR_LEN_MAX);
3088 var_val = (u64)(uintptr_t)str;
3090 tracing_map_set_var(elt, var_idx, var_val);
3094 static void update_field_vars(struct hist_trigger_data *hist_data,
3095 struct tracing_map_elt *elt,
3096 struct ring_buffer_event *rbe,
3099 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3100 hist_data->n_field_vars, 0);
3103 static void update_max_vars(struct hist_trigger_data *hist_data,
3104 struct tracing_map_elt *elt,
3105 struct ring_buffer_event *rbe,
3108 __update_field_vars(elt, rbe, rec, hist_data->max_vars,
3109 hist_data->n_max_vars, hist_data->n_field_var_str);
3112 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3113 struct trace_event_file *file,
3114 char *name, int size, const char *type)
3116 struct hist_field *var;
3119 if (find_var(hist_data, file, name) && !hist_data->remove) {
3120 var = ERR_PTR(-EINVAL);
3124 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3126 var = ERR_PTR(-ENOMEM);
3130 idx = tracing_map_add_var(hist_data->map);
3133 var = ERR_PTR(-EINVAL);
3137 var->flags = HIST_FIELD_FL_VAR;
3139 var->var.hist_data = var->hist_data = hist_data;
3141 var->var.name = kstrdup(name, GFP_KERNEL);
3142 var->type = kstrdup(type, GFP_KERNEL);
3143 if (!var->var.name || !var->type) {
3144 kfree(var->var.name);
3147 var = ERR_PTR(-ENOMEM);
3153 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3154 struct trace_event_file *file,
3157 struct hist_field *val = NULL, *var = NULL;
3158 unsigned long flags = HIST_FIELD_FL_VAR;
3159 struct field_var *field_var;
3162 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3163 hist_err("Too many field variables defined: ", field_name);
3168 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3170 hist_err("Couldn't parse field variable: ", field_name);
3175 var = create_var(hist_data, file, field_name, val->size, val->type);
3177 hist_err("Couldn't create or find variable: ", field_name);
3183 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3191 field_var->var = var;
3192 field_var->val = val;
3196 field_var = ERR_PTR(ret);
3201 * create_target_field_var - Automatically create a variable for a field
3202 * @target_hist_data: The target hist trigger
3203 * @subsys_name: Optional subsystem name
3204 * @event_name: Optional event name
3205 * @var_name: The name of the field (and the resulting variable)
3207 * Hist trigger actions fetch data from variables, not directly from
3208 * events. However, for convenience, users are allowed to directly
3209 * specify an event field in an action, which will be automatically
3210 * converted into a variable on their behalf.
3212 * This function creates a field variable with the name var_name on
3213 * the hist trigger currently being defined on the target event. If
3214 * subsys_name and event_name are specified, this function simply
3215 * verifies that they do in fact match the target event subsystem and
3218 * Return: The variable created for the field.
3220 static struct field_var *
3221 create_target_field_var(struct hist_trigger_data *target_hist_data,
3222 char *subsys_name, char *event_name, char *var_name)
3224 struct trace_event_file *file = target_hist_data->event_file;
3227 struct trace_event_call *call;
3232 call = file->event_call;
3234 if (strcmp(subsys_name, call->class->system) != 0)
3237 if (strcmp(event_name, trace_event_name(call)) != 0)
3241 return create_field_var(target_hist_data, file, var_name);
3244 static void onmax_print(struct seq_file *m,
3245 struct hist_trigger_data *hist_data,
3246 struct tracing_map_elt *elt,
3247 struct action_data *data)
3249 unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
3251 seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
3253 for (i = 0; i < hist_data->n_max_vars; i++) {
3254 struct hist_field *save_val = hist_data->max_vars[i]->val;
3255 struct hist_field *save_var = hist_data->max_vars[i]->var;
3258 save_var_idx = save_var->var.idx;
3260 val = tracing_map_read_var(elt, save_var_idx);
3262 if (save_val->flags & HIST_FIELD_FL_STRING) {
3263 seq_printf(m, " %s: %-32s", save_var->var.name,
3264 (char *)(uintptr_t)(val));
3266 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3270 static void onmax_save(struct hist_trigger_data *hist_data,
3271 struct tracing_map_elt *elt, void *rec,
3272 struct ring_buffer_event *rbe,
3273 struct action_data *data, u64 *var_ref_vals)
3275 unsigned int max_idx = data->onmax.max_var->var.idx;
3276 unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
3278 u64 var_val, max_val;
3280 var_val = var_ref_vals[max_var_ref_idx];
3281 max_val = tracing_map_read_var(elt, max_idx);
3283 if (var_val <= max_val)
3286 tracing_map_set_var(elt, max_idx, var_val);
3288 update_max_vars(hist_data, elt, rbe, rec);
3291 static void onmax_destroy(struct action_data *data)
3295 destroy_hist_field(data->onmax.max_var, 0);
3296 destroy_hist_field(data->onmax.var, 0);
3298 kfree(data->onmax.var_str);
3299 kfree(data->onmax.fn_name);
3301 for (i = 0; i < data->n_params; i++)
3302 kfree(data->params[i]);
3307 static int onmax_create(struct hist_trigger_data *hist_data,
3308 struct action_data *data)
3310 struct trace_event_file *file = hist_data->event_file;
3311 struct hist_field *var_field, *ref_field, *max_var;
3312 unsigned int var_ref_idx = hist_data->n_var_refs;
3313 struct field_var *field_var;
3314 char *onmax_var_str, *param;
3315 unsigned long flags;
3319 onmax_var_str = data->onmax.var_str;
3320 if (onmax_var_str[0] != '$') {
3321 hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
3326 var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
3328 hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
3332 flags = HIST_FIELD_FL_VAR_REF;
3333 ref_field = create_hist_field(hist_data, NULL, flags, NULL);
3337 if (init_var_ref(ref_field, var_field, NULL, NULL)) {
3338 destroy_hist_field(ref_field, 0);
3342 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3343 ref_field->var_ref_idx = hist_data->n_var_refs++;
3344 data->onmax.var = ref_field;
3346 data->fn = onmax_save;
3347 data->onmax.max_var_ref_idx = var_ref_idx;
3348 max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
3349 if (IS_ERR(max_var)) {
3350 hist_err("onmax: Couldn't create onmax variable: ", "max");
3351 ret = PTR_ERR(max_var);
3354 data->onmax.max_var = max_var;
3356 for (i = 0; i < data->n_params; i++) {
3357 param = kstrdup(data->params[i], GFP_KERNEL);
3363 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3364 if (IS_ERR(field_var)) {
3365 hist_err("onmax: Couldn't create field variable: ", param);
3366 ret = PTR_ERR(field_var);
3371 hist_data->max_vars[hist_data->n_max_vars++] = field_var;
3372 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3373 hist_data->n_max_var_str++;
3381 static int parse_action_params(char *params, struct action_data *data)
3383 char *param, *saved_param;
3387 if (data->n_params >= SYNTH_FIELDS_MAX)
3390 param = strsep(¶ms, ",");
3396 param = strstrip(param);
3397 if (strlen(param) < 2) {
3398 hist_err("Invalid action param: ", param);
3403 saved_param = kstrdup(param, GFP_KERNEL);
3409 data->params[data->n_params++] = saved_param;
3415 static struct action_data *onmax_parse(char *str)
3417 char *onmax_fn_name, *onmax_var_str;
3418 struct action_data *data;
3421 data = kzalloc(sizeof(*data), GFP_KERNEL);
3423 return ERR_PTR(-ENOMEM);
3425 onmax_var_str = strsep(&str, ")");
3426 if (!onmax_var_str || !str) {
3431 data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
3432 if (!data->onmax.var_str) {
3441 onmax_fn_name = strsep(&str, "(");
3442 if (!onmax_fn_name || !str)
3445 if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) {
3446 char *params = strsep(&str, ")");
3453 ret = parse_action_params(params, data);
3459 data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
3460 if (!data->onmax.fn_name) {
3467 onmax_destroy(data);
3468 data = ERR_PTR(ret);
3472 static void onmatch_destroy(struct action_data *data)
3476 mutex_lock(&synth_event_mutex);
3478 kfree(data->onmatch.match_event);
3479 kfree(data->onmatch.match_event_system);
3480 kfree(data->onmatch.synth_event_name);
3482 for (i = 0; i < data->n_params; i++)
3483 kfree(data->params[i]);
3485 if (data->onmatch.synth_event)
3486 data->onmatch.synth_event->ref--;
3490 mutex_unlock(&synth_event_mutex);
3493 static void destroy_field_var(struct field_var *field_var)
3498 destroy_hist_field(field_var->var, 0);
3499 destroy_hist_field(field_var->val, 0);
3504 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3508 for (i = 0; i < hist_data->n_field_vars; i++)
3509 destroy_field_var(hist_data->field_vars[i]);
3512 static void save_field_var(struct hist_trigger_data *hist_data,
3513 struct field_var *field_var)
3515 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3517 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3518 hist_data->n_field_var_str++;
3522 static void destroy_synth_var_refs(struct hist_trigger_data *hist_data)
3526 for (i = 0; i < hist_data->n_synth_var_refs; i++)
3527 destroy_hist_field(hist_data->synth_var_refs[i], 0);
3530 static void save_synth_var_ref(struct hist_trigger_data *hist_data,
3531 struct hist_field *var_ref)
3533 hist_data->synth_var_refs[hist_data->n_synth_var_refs++] = var_ref;
3535 hist_data->var_refs[hist_data->n_var_refs] = var_ref;
3536 var_ref->var_ref_idx = hist_data->n_var_refs++;
3539 static int check_synth_field(struct synth_event *event,
3540 struct hist_field *hist_field,
3541 unsigned int field_pos)
3543 struct synth_field *field;
3545 if (field_pos >= event->n_fields)
3548 field = event->fields[field_pos];
3550 if (strcmp(field->type, hist_field->type) != 0)
3556 static struct hist_field *
3557 onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
3558 char *system, char *event, char *var)
3560 struct hist_field *hist_field;
3562 var++; /* skip '$' */
3564 hist_field = find_target_event_var(hist_data, system, event, var);
3567 system = data->onmatch.match_event_system;
3568 event = data->onmatch.match_event;
3571 hist_field = find_event_var(hist_data, system, event, var);
3575 hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
3580 static struct hist_field *
3581 onmatch_create_field_var(struct hist_trigger_data *hist_data,
3582 struct action_data *data, char *system,
3583 char *event, char *var)
3585 struct hist_field *hist_field = NULL;
3586 struct field_var *field_var;
3589 * First try to create a field var on the target event (the
3590 * currently being defined). This will create a variable for
3591 * unqualified fields on the target event, or if qualified,
3592 * target fields that have qualified names matching the target.
3594 field_var = create_target_field_var(hist_data, system, event, var);
3596 if (field_var && !IS_ERR(field_var)) {
3597 save_field_var(hist_data, field_var);
3598 hist_field = field_var->var;
3602 * If no explicit system.event is specfied, default to
3603 * looking for fields on the onmatch(system.event.xxx)
3607 system = data->onmatch.match_event_system;
3608 event = data->onmatch.match_event;
3612 * At this point, we're looking at a field on another
3613 * event. Because we can't modify a hist trigger on
3614 * another event to add a variable for a field, we need
3615 * to create a new trigger on that event and create the
3616 * variable at the same time.
3618 hist_field = create_field_var_hist(hist_data, system, event, var);
3619 if (IS_ERR(hist_field))
3625 destroy_field_var(field_var);
3630 static int onmatch_create(struct hist_trigger_data *hist_data,
3631 struct trace_event_file *file,
3632 struct action_data *data)
3634 char *event_name, *param, *system = NULL;
3635 struct hist_field *hist_field, *var_ref;
3636 unsigned int i, var_ref_idx;
3637 unsigned int field_pos = 0;
3638 struct synth_event *event;
3641 mutex_lock(&synth_event_mutex);
3642 event = find_synth_event(data->onmatch.synth_event_name);
3644 hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
3645 mutex_unlock(&synth_event_mutex);
3649 mutex_unlock(&synth_event_mutex);
3651 var_ref_idx = hist_data->n_var_refs;
3653 for (i = 0; i < data->n_params; i++) {
3656 p = param = kstrdup(data->params[i], GFP_KERNEL);
3662 system = strsep(¶m, ".");
3664 param = (char *)system;
3665 system = event_name = NULL;
3667 event_name = strsep(¶m, ".");
3675 if (param[0] == '$')
3676 hist_field = onmatch_find_var(hist_data, data, system,
3679 hist_field = onmatch_create_field_var(hist_data, data,
3690 if (check_synth_field(event, hist_field, field_pos) == 0) {
3691 var_ref = create_var_ref(hist_field, system, event_name);
3698 save_synth_var_ref(hist_data, var_ref);
3704 hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
3705 system, event_name, param);
3711 if (field_pos != event->n_fields) {
3712 hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
3717 data->fn = action_trace;
3718 data->onmatch.synth_event = event;
3719 data->onmatch.var_ref_idx = var_ref_idx;
3723 mutex_lock(&synth_event_mutex);
3725 mutex_unlock(&synth_event_mutex);
3730 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3732 char *match_event, *match_event_system;
3733 char *synth_event_name, *params;
3734 struct action_data *data;
3737 data = kzalloc(sizeof(*data), GFP_KERNEL);
3739 return ERR_PTR(-ENOMEM);
3741 match_event = strsep(&str, ")");
3742 if (!match_event || !str) {
3743 hist_err("onmatch: Missing closing paren: ", match_event);
3747 match_event_system = strsep(&match_event, ".");
3749 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
3753 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3754 hist_err_event("onmatch: Invalid subsystem or event name: ",
3755 match_event_system, match_event, NULL);
3759 data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
3760 if (!data->onmatch.match_event) {
3765 data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
3766 if (!data->onmatch.match_event_system) {
3773 hist_err("onmatch: Missing . after onmatch(): ", str);
3777 synth_event_name = strsep(&str, "(");
3778 if (!synth_event_name || !str) {
3779 hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
3783 data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
3784 if (!data->onmatch.synth_event_name) {
3789 params = strsep(&str, ")");
3790 if (!params || !str || (str && strlen(str))) {
3791 hist_err("onmatch: Missing closing paramlist paren: ", params);
3795 ret = parse_action_params(params, data);
3801 onmatch_destroy(data);
3802 data = ERR_PTR(ret);
3806 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3808 hist_data->fields[HITCOUNT_IDX] =
3809 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3810 if (!hist_data->fields[HITCOUNT_IDX])
3813 hist_data->n_vals++;
3814 hist_data->n_fields++;
3816 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3822 static int __create_val_field(struct hist_trigger_data *hist_data,
3823 unsigned int val_idx,
3824 struct trace_event_file *file,
3825 char *var_name, char *field_str,
3826 unsigned long flags)
3828 struct hist_field *hist_field;
3831 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3832 if (IS_ERR(hist_field)) {
3833 ret = PTR_ERR(hist_field);
3837 hist_data->fields[val_idx] = hist_field;
3839 ++hist_data->n_vals;
3840 ++hist_data->n_fields;
3842 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3848 static int create_val_field(struct hist_trigger_data *hist_data,
3849 unsigned int val_idx,
3850 struct trace_event_file *file,
3853 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3856 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3859 static int create_var_field(struct hist_trigger_data *hist_data,
3860 unsigned int val_idx,
3861 struct trace_event_file *file,
3862 char *var_name, char *expr_str)
3864 unsigned long flags = 0;
3866 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3869 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3870 hist_err("Variable already defined: ", var_name);
3874 flags |= HIST_FIELD_FL_VAR;
3875 hist_data->n_vars++;
3876 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3879 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3882 static int create_val_fields(struct hist_trigger_data *hist_data,
3883 struct trace_event_file *file)
3885 char *fields_str, *field_str;
3886 unsigned int i, j = 1;
3889 ret = create_hitcount_val(hist_data);
3893 fields_str = hist_data->attrs->vals_str;
3897 strsep(&fields_str, "=");
3901 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3902 j < TRACING_MAP_VALS_MAX; i++) {
3903 field_str = strsep(&fields_str, ",");
3907 if (strcmp(field_str, "hitcount") == 0)
3910 ret = create_val_field(hist_data, j++, file, field_str);
3915 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3921 static int create_key_field(struct hist_trigger_data *hist_data,
3922 unsigned int key_idx,
3923 unsigned int key_offset,
3924 struct trace_event_file *file,
3927 struct hist_field *hist_field = NULL;
3929 unsigned long flags = 0;
3930 unsigned int key_size;
3933 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
3936 flags |= HIST_FIELD_FL_KEY;
3938 if (strcmp(field_str, "stacktrace") == 0) {
3939 flags |= HIST_FIELD_FL_STACKTRACE;
3940 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
3941 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
3943 hist_field = parse_expr(hist_data, file, field_str, flags,
3945 if (IS_ERR(hist_field)) {
3946 ret = PTR_ERR(hist_field);
3950 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
3951 hist_err("Using variable references as keys not supported: ", field_str);
3952 destroy_hist_field(hist_field, 0);
3957 key_size = hist_field->size;
3960 hist_data->fields[key_idx] = hist_field;
3962 key_size = ALIGN(key_size, sizeof(u64));
3963 hist_data->fields[key_idx]->size = key_size;
3964 hist_data->fields[key_idx]->offset = key_offset;
3966 hist_data->key_size += key_size;
3968 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3973 hist_data->n_keys++;
3974 hist_data->n_fields++;
3976 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
3984 static int create_key_fields(struct hist_trigger_data *hist_data,
3985 struct trace_event_file *file)
3987 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
3988 char *fields_str, *field_str;
3991 fields_str = hist_data->attrs->keys_str;
3995 strsep(&fields_str, "=");
3999 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4000 field_str = strsep(&fields_str, ",");
4003 ret = create_key_field(hist_data, i, key_offset,
4018 static int create_var_fields(struct hist_trigger_data *hist_data,
4019 struct trace_event_file *file)
4021 unsigned int i, j = hist_data->n_vals;
4024 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4026 for (i = 0; i < n_vars; i++) {
4027 char *var_name = hist_data->attrs->var_defs.name[i];
4028 char *expr = hist_data->attrs->var_defs.expr[i];
4030 ret = create_var_field(hist_data, j++, file, var_name, expr);
4038 static void free_var_defs(struct hist_trigger_data *hist_data)
4042 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4043 kfree(hist_data->attrs->var_defs.name[i]);
4044 kfree(hist_data->attrs->var_defs.expr[i]);
4047 hist_data->attrs->var_defs.n_vars = 0;
4050 static int parse_var_defs(struct hist_trigger_data *hist_data)
4052 char *s, *str, *var_name, *field_str;
4053 unsigned int i, j, n_vars = 0;
4056 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4057 str = hist_data->attrs->assignment_str[i];
4058 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4059 field_str = strsep(&str, ",");
4063 var_name = strsep(&field_str, "=");
4064 if (!var_name || !field_str) {
4065 hist_err("Malformed assignment: ", var_name);
4070 if (n_vars == TRACING_MAP_VARS_MAX) {
4071 hist_err("Too many variables defined: ", var_name);
4076 s = kstrdup(var_name, GFP_KERNEL);
4081 hist_data->attrs->var_defs.name[n_vars] = s;
4083 s = kstrdup(field_str, GFP_KERNEL);
4085 kfree(hist_data->attrs->var_defs.name[n_vars]);
4089 hist_data->attrs->var_defs.expr[n_vars++] = s;
4091 hist_data->attrs->var_defs.n_vars = n_vars;
4097 free_var_defs(hist_data);
4102 static int create_hist_fields(struct hist_trigger_data *hist_data,
4103 struct trace_event_file *file)
4107 ret = parse_var_defs(hist_data);
4111 ret = create_val_fields(hist_data, file);
4115 ret = create_var_fields(hist_data, file);
4119 ret = create_key_fields(hist_data, file);
4123 free_var_defs(hist_data);
4128 static int is_descending(const char *str)
4133 if (strcmp(str, "descending") == 0)
4136 if (strcmp(str, "ascending") == 0)
4142 static int create_sort_keys(struct hist_trigger_data *hist_data)
4144 char *fields_str = hist_data->attrs->sort_key_str;
4145 struct tracing_map_sort_key *sort_key;
4146 int descending, ret = 0;
4147 unsigned int i, j, k;
4149 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4154 strsep(&fields_str, "=");
4160 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4161 struct hist_field *hist_field;
4162 char *field_str, *field_name;
4163 const char *test_name;
4165 sort_key = &hist_data->sort_keys[i];
4167 field_str = strsep(&fields_str, ",");
4174 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4179 field_name = strsep(&field_str, ".");
4185 if (strcmp(field_name, "hitcount") == 0) {
4186 descending = is_descending(field_str);
4187 if (descending < 0) {
4191 sort_key->descending = descending;
4195 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4198 hist_field = hist_data->fields[j];
4199 if (hist_field->flags & HIST_FIELD_FL_VAR)
4204 test_name = hist_field_name(hist_field, 0);
4206 if (strcmp(field_name, test_name) == 0) {
4207 sort_key->field_idx = idx;
4208 descending = is_descending(field_str);
4209 if (descending < 0) {
4213 sort_key->descending = descending;
4217 if (j == hist_data->n_fields) {
4223 hist_data->n_sort_keys = i;
4228 static void destroy_actions(struct hist_trigger_data *hist_data)
4232 for (i = 0; i < hist_data->n_actions; i++) {
4233 struct action_data *data = hist_data->actions[i];
4235 if (data->fn == action_trace)
4236 onmatch_destroy(data);
4237 else if (data->fn == onmax_save)
4238 onmax_destroy(data);
4244 static int parse_actions(struct hist_trigger_data *hist_data)
4246 struct trace_array *tr = hist_data->event_file->tr;
4247 struct action_data *data;
4252 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4253 str = hist_data->attrs->action_str[i];
4255 if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) {
4256 char *action_str = str + strlen("onmatch(");
4258 data = onmatch_parse(tr, action_str);
4260 ret = PTR_ERR(data);
4263 data->fn = action_trace;
4264 } else if (strncmp(str, "onmax(", strlen("onmax(")) == 0) {
4265 char *action_str = str + strlen("onmax(");
4267 data = onmax_parse(action_str);
4269 ret = PTR_ERR(data);
4272 data->fn = onmax_save;
4278 hist_data->actions[hist_data->n_actions++] = data;
4284 static int create_actions(struct hist_trigger_data *hist_data,
4285 struct trace_event_file *file)
4287 struct action_data *data;
4291 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4292 data = hist_data->actions[i];
4294 if (data->fn == action_trace) {
4295 ret = onmatch_create(hist_data, file, data);
4298 } else if (data->fn == onmax_save) {
4299 ret = onmax_create(hist_data, data);
4308 static void print_actions(struct seq_file *m,
4309 struct hist_trigger_data *hist_data,
4310 struct tracing_map_elt *elt)
4314 for (i = 0; i < hist_data->n_actions; i++) {
4315 struct action_data *data = hist_data->actions[i];
4317 if (data->fn == onmax_save)
4318 onmax_print(m, hist_data, elt, data);
4322 static void print_onmax_spec(struct seq_file *m,
4323 struct hist_trigger_data *hist_data,
4324 struct action_data *data)
4328 seq_puts(m, ":onmax(");
4329 seq_printf(m, "%s", data->onmax.var_str);
4330 seq_printf(m, ").%s(", data->onmax.fn_name);
4332 for (i = 0; i < hist_data->n_max_vars; i++) {
4333 seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
4334 if (i < hist_data->n_max_vars - 1)
4340 static void print_onmatch_spec(struct seq_file *m,
4341 struct hist_trigger_data *hist_data,
4342 struct action_data *data)
4346 seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
4347 data->onmatch.match_event);
4349 seq_printf(m, "%s(", data->onmatch.synth_event->name);
4351 for (i = 0; i < data->n_params; i++) {
4354 seq_printf(m, "%s", data->params[i]);
4360 static bool actions_match(struct hist_trigger_data *hist_data,
4361 struct hist_trigger_data *hist_data_test)
4365 if (hist_data->n_actions != hist_data_test->n_actions)
4368 for (i = 0; i < hist_data->n_actions; i++) {
4369 struct action_data *data = hist_data->actions[i];
4370 struct action_data *data_test = hist_data_test->actions[i];
4372 if (data->fn != data_test->fn)
4375 if (data->n_params != data_test->n_params)
4378 for (j = 0; j < data->n_params; j++) {
4379 if (strcmp(data->params[j], data_test->params[j]) != 0)
4383 if (data->fn == action_trace) {
4384 if (strcmp(data->onmatch.synth_event_name,
4385 data_test->onmatch.synth_event_name) != 0)
4387 if (strcmp(data->onmatch.match_event_system,
4388 data_test->onmatch.match_event_system) != 0)
4390 if (strcmp(data->onmatch.match_event,
4391 data_test->onmatch.match_event) != 0)
4393 } else if (data->fn == onmax_save) {
4394 if (strcmp(data->onmax.var_str,
4395 data_test->onmax.var_str) != 0)
4397 if (strcmp(data->onmax.fn_name,
4398 data_test->onmax.fn_name) != 0)
4407 static void print_actions_spec(struct seq_file *m,
4408 struct hist_trigger_data *hist_data)
4412 for (i = 0; i < hist_data->n_actions; i++) {
4413 struct action_data *data = hist_data->actions[i];
4415 if (data->fn == action_trace)
4416 print_onmatch_spec(m, hist_data, data);
4417 else if (data->fn == onmax_save)
4418 print_onmax_spec(m, hist_data, data);
4422 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4426 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4427 kfree(hist_data->field_var_hists[i]->cmd);
4428 kfree(hist_data->field_var_hists[i]);
4432 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4437 destroy_hist_trigger_attrs(hist_data->attrs);
4438 destroy_hist_fields(hist_data);
4439 tracing_map_destroy(hist_data->map);
4441 destroy_actions(hist_data);
4442 destroy_field_vars(hist_data);
4443 destroy_field_var_hists(hist_data);
4444 destroy_synth_var_refs(hist_data);
4449 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4451 struct tracing_map *map = hist_data->map;
4452 struct ftrace_event_field *field;
4453 struct hist_field *hist_field;
4456 for_each_hist_field(i, hist_data) {
4457 hist_field = hist_data->fields[i];
4458 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4459 tracing_map_cmp_fn_t cmp_fn;
4461 field = hist_field->field;
4463 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4464 cmp_fn = tracing_map_cmp_none;
4466 cmp_fn = tracing_map_cmp_num(hist_field->size,
4467 hist_field->is_signed);
4468 else if (is_string_field(field))
4469 cmp_fn = tracing_map_cmp_string;
4471 cmp_fn = tracing_map_cmp_num(field->size,
4473 idx = tracing_map_add_key_field(map,
4476 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4477 idx = tracing_map_add_sum_field(map);
4482 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4483 idx = tracing_map_add_var(map);
4486 hist_field->var.idx = idx;
4487 hist_field->var.hist_data = hist_data;
4494 static struct hist_trigger_data *
4495 create_hist_data(unsigned int map_bits,
4496 struct hist_trigger_attrs *attrs,
4497 struct trace_event_file *file,
4500 const struct tracing_map_ops *map_ops = NULL;
4501 struct hist_trigger_data *hist_data;
4504 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4506 return ERR_PTR(-ENOMEM);
4508 hist_data->attrs = attrs;
4509 hist_data->remove = remove;
4510 hist_data->event_file = file;
4512 ret = parse_actions(hist_data);
4516 ret = create_hist_fields(hist_data, file);
4520 ret = create_sort_keys(hist_data);
4524 map_ops = &hist_trigger_elt_data_ops;
4526 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4527 map_ops, hist_data);
4528 if (IS_ERR(hist_data->map)) {
4529 ret = PTR_ERR(hist_data->map);
4530 hist_data->map = NULL;
4534 ret = create_tracing_map_fields(hist_data);
4540 hist_data->attrs = NULL;
4542 destroy_hist_data(hist_data);
4544 hist_data = ERR_PTR(ret);
4549 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4550 struct tracing_map_elt *elt, void *rec,
4551 struct ring_buffer_event *rbe,
4554 struct hist_elt_data *elt_data;
4555 struct hist_field *hist_field;
4556 unsigned int i, var_idx;
4559 elt_data = elt->private_data;
4560 elt_data->var_ref_vals = var_ref_vals;
4562 for_each_hist_val_field(i, hist_data) {
4563 hist_field = hist_data->fields[i];
4564 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4565 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4566 var_idx = hist_field->var.idx;
4567 tracing_map_set_var(elt, var_idx, hist_val);
4570 tracing_map_update_sum(elt, i, hist_val);
4573 for_each_hist_key_field(i, hist_data) {
4574 hist_field = hist_data->fields[i];
4575 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4576 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4577 var_idx = hist_field->var.idx;
4578 tracing_map_set_var(elt, var_idx, hist_val);
4582 update_field_vars(hist_data, elt, rbe, rec);
4585 static inline void add_to_key(char *compound_key, void *key,
4586 struct hist_field *key_field, void *rec)
4588 size_t size = key_field->size;
4590 if (key_field->flags & HIST_FIELD_FL_STRING) {
4591 struct ftrace_event_field *field;
4593 field = key_field->field;
4594 if (field->filter_type == FILTER_DYN_STRING)
4595 size = *(u32 *)(rec + field->offset) >> 16;
4596 else if (field->filter_type == FILTER_PTR_STRING)
4598 else if (field->filter_type == FILTER_STATIC_STRING)
4601 /* ensure NULL-termination */
4602 if (size > key_field->size - 1)
4603 size = key_field->size - 1;
4606 memcpy(compound_key + key_field->offset, key, size);
4610 hist_trigger_actions(struct hist_trigger_data *hist_data,
4611 struct tracing_map_elt *elt, void *rec,
4612 struct ring_buffer_event *rbe, u64 *var_ref_vals)
4614 struct action_data *data;
4617 for (i = 0; i < hist_data->n_actions; i++) {
4618 data = hist_data->actions[i];
4619 data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
4623 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4624 struct ring_buffer_event *rbe)
4626 struct hist_trigger_data *hist_data = data->private_data;
4627 bool use_compound_key = (hist_data->n_keys > 1);
4628 unsigned long entries[HIST_STACKTRACE_DEPTH];
4629 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4630 char compound_key[HIST_KEY_SIZE_MAX];
4631 struct tracing_map_elt *elt = NULL;
4632 struct stack_trace stacktrace;
4633 struct hist_field *key_field;
4638 memset(compound_key, 0, hist_data->key_size);
4640 for_each_hist_key_field(i, hist_data) {
4641 key_field = hist_data->fields[i];
4643 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4644 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
4645 stacktrace.entries = entries;
4646 stacktrace.nr_entries = 0;
4647 stacktrace.skip = HIST_STACKTRACE_SKIP;
4649 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
4650 save_stack_trace(&stacktrace);
4654 field_contents = key_field->fn(key_field, elt, rbe, rec);
4655 if (key_field->flags & HIST_FIELD_FL_STRING) {
4656 key = (void *)(unsigned long)field_contents;
4657 use_compound_key = true;
4659 key = (void *)&field_contents;
4662 if (use_compound_key)
4663 add_to_key(compound_key, key, key_field, rec);
4666 if (use_compound_key)
4669 if (hist_data->n_var_refs &&
4670 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4673 elt = tracing_map_insert(hist_data->map, key);
4677 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4679 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4680 hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
4683 static void hist_trigger_stacktrace_print(struct seq_file *m,
4684 unsigned long *stacktrace_entries,
4685 unsigned int max_entries)
4687 char str[KSYM_SYMBOL_LEN];
4688 unsigned int spaces = 8;
4691 for (i = 0; i < max_entries; i++) {
4692 if (stacktrace_entries[i] == ULONG_MAX)
4695 seq_printf(m, "%*c", 1 + spaces, ' ');
4696 sprint_symbol(str, stacktrace_entries[i]);
4697 seq_printf(m, "%s\n", str);
4702 hist_trigger_entry_print(struct seq_file *m,
4703 struct hist_trigger_data *hist_data, void *key,
4704 struct tracing_map_elt *elt)
4706 struct hist_field *key_field;
4707 char str[KSYM_SYMBOL_LEN];
4708 bool multiline = false;
4709 const char *field_name;
4715 for_each_hist_key_field(i, hist_data) {
4716 key_field = hist_data->fields[i];
4718 if (i > hist_data->n_vals)
4721 field_name = hist_field_name(key_field, 0);
4723 if (key_field->flags & HIST_FIELD_FL_HEX) {
4724 uval = *(u64 *)(key + key_field->offset);
4725 seq_printf(m, "%s: %llx", field_name, uval);
4726 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4727 uval = *(u64 *)(key + key_field->offset);
4728 sprint_symbol_no_offset(str, uval);
4729 seq_printf(m, "%s: [%llx] %-45s", field_name,
4731 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4732 uval = *(u64 *)(key + key_field->offset);
4733 sprint_symbol(str, uval);
4734 seq_printf(m, "%s: [%llx] %-55s", field_name,
4736 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4737 struct hist_elt_data *elt_data = elt->private_data;
4740 if (WARN_ON_ONCE(!elt_data))
4743 comm = elt_data->comm;
4745 uval = *(u64 *)(key + key_field->offset);
4746 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4748 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4749 const char *syscall_name;
4751 uval = *(u64 *)(key + key_field->offset);
4752 syscall_name = get_syscall_name(uval);
4754 syscall_name = "unknown_syscall";
4756 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4757 syscall_name, uval);
4758 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4759 seq_puts(m, "stacktrace:\n");
4760 hist_trigger_stacktrace_print(m,
4761 key + key_field->offset,
4762 HIST_STACKTRACE_DEPTH);
4764 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4765 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4766 *(u64 *)(key + key_field->offset));
4767 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4768 seq_printf(m, "%s: %-50s", field_name,
4769 (char *)(key + key_field->offset));
4771 uval = *(u64 *)(key + key_field->offset);
4772 seq_printf(m, "%s: %10llu", field_name, uval);
4781 seq_printf(m, " hitcount: %10llu",
4782 tracing_map_read_sum(elt, HITCOUNT_IDX));
4784 for (i = 1; i < hist_data->n_vals; i++) {
4785 field_name = hist_field_name(hist_data->fields[i], 0);
4787 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4788 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4791 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4792 seq_printf(m, " %s: %10llx", field_name,
4793 tracing_map_read_sum(elt, i));
4795 seq_printf(m, " %s: %10llu", field_name,
4796 tracing_map_read_sum(elt, i));
4800 print_actions(m, hist_data, elt);
4805 static int print_entries(struct seq_file *m,
4806 struct hist_trigger_data *hist_data)
4808 struct tracing_map_sort_entry **sort_entries = NULL;
4809 struct tracing_map *map = hist_data->map;
4812 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4813 hist_data->n_sort_keys,
4818 for (i = 0; i < n_entries; i++)
4819 hist_trigger_entry_print(m, hist_data,
4820 sort_entries[i]->key,
4821 sort_entries[i]->elt);
4823 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4828 static void hist_trigger_show(struct seq_file *m,
4829 struct event_trigger_data *data, int n)
4831 struct hist_trigger_data *hist_data;
4835 seq_puts(m, "\n\n");
4837 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4838 data->ops->print(m, data->ops, data);
4839 seq_puts(m, "#\n\n");
4841 hist_data = data->private_data;
4842 n_entries = print_entries(m, hist_data);
4846 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4847 (u64)atomic64_read(&hist_data->map->hits),
4848 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4851 static int hist_show(struct seq_file *m, void *v)
4853 struct event_trigger_data *data;
4854 struct trace_event_file *event_file;
4857 mutex_lock(&event_mutex);
4859 event_file = event_file_data(m->private);
4860 if (unlikely(!event_file)) {
4865 list_for_each_entry_rcu(data, &event_file->triggers, list) {
4866 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4867 hist_trigger_show(m, data, n++);
4870 if (have_hist_err()) {
4871 seq_printf(m, "\nERROR: %s\n", hist_err_str);
4872 seq_printf(m, " Last command: %s\n", last_hist_cmd);
4876 mutex_unlock(&event_mutex);
4881 static int event_hist_open(struct inode *inode, struct file *file)
4883 return single_open(file, hist_show, file);
4886 const struct file_operations event_hist_fops = {
4887 .open = event_hist_open,
4889 .llseek = seq_lseek,
4890 .release = single_release,
4893 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
4895 const char *field_name = hist_field_name(hist_field, 0);
4897 if (hist_field->var.name)
4898 seq_printf(m, "%s=", hist_field->var.name);
4900 if (hist_field->flags & HIST_FIELD_FL_CPU)
4902 else if (field_name) {
4903 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
4904 hist_field->flags & HIST_FIELD_FL_ALIAS)
4906 seq_printf(m, "%s", field_name);
4907 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
4908 seq_puts(m, "common_timestamp");
4910 if (hist_field->flags) {
4911 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
4912 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
4913 const char *flags = get_hist_field_flags(hist_field);
4916 seq_printf(m, ".%s", flags);
4921 static int event_hist_trigger_print(struct seq_file *m,
4922 struct event_trigger_ops *ops,
4923 struct event_trigger_data *data)
4925 struct hist_trigger_data *hist_data = data->private_data;
4926 struct hist_field *field;
4927 bool have_var = false;
4930 seq_puts(m, "hist:");
4933 seq_printf(m, "%s:", data->name);
4935 seq_puts(m, "keys=");
4937 for_each_hist_key_field(i, hist_data) {
4938 field = hist_data->fields[i];
4940 if (i > hist_data->n_vals)
4943 if (field->flags & HIST_FIELD_FL_STACKTRACE)
4944 seq_puts(m, "stacktrace");
4946 hist_field_print(m, field);
4949 seq_puts(m, ":vals=");
4951 for_each_hist_val_field(i, hist_data) {
4952 field = hist_data->fields[i];
4953 if (field->flags & HIST_FIELD_FL_VAR) {
4958 if (i == HITCOUNT_IDX)
4959 seq_puts(m, "hitcount");
4962 hist_field_print(m, field);
4971 for_each_hist_val_field(i, hist_data) {
4972 field = hist_data->fields[i];
4974 if (field->flags & HIST_FIELD_FL_VAR) {
4977 hist_field_print(m, field);
4982 seq_puts(m, ":sort=");
4984 for (i = 0; i < hist_data->n_sort_keys; i++) {
4985 struct tracing_map_sort_key *sort_key;
4986 unsigned int idx, first_key_idx;
4989 first_key_idx = hist_data->n_vals - hist_data->n_vars;
4991 sort_key = &hist_data->sort_keys[i];
4992 idx = sort_key->field_idx;
4994 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5000 if (idx == HITCOUNT_IDX)
5001 seq_puts(m, "hitcount");
5003 if (idx >= first_key_idx)
5004 idx += hist_data->n_vars;
5005 hist_field_print(m, hist_data->fields[idx]);
5008 if (sort_key->descending)
5009 seq_puts(m, ".descending");
5011 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5012 if (hist_data->enable_timestamps)
5013 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5015 print_actions_spec(m, hist_data);
5017 if (data->filter_str)
5018 seq_printf(m, " if %s", data->filter_str);
5021 seq_puts(m, " [paused]");
5023 seq_puts(m, " [active]");
5030 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5031 struct event_trigger_data *data)
5033 struct hist_trigger_data *hist_data = data->private_data;
5035 if (!data->ref && hist_data->attrs->name)
5036 save_named_trigger(hist_data->attrs->name, data);
5043 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5045 struct trace_event_file *file;
5050 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5051 file = hist_data->field_var_hists[i]->hist_data->event_file;
5052 cmd = hist_data->field_var_hists[i]->cmd;
5053 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5054 "!hist", "hist", cmd);
5058 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5059 struct event_trigger_data *data)
5061 struct hist_trigger_data *hist_data = data->private_data;
5063 if (WARN_ON_ONCE(data->ref <= 0))
5069 del_named_trigger(data);
5071 trigger_data_free(data);
5073 remove_hist_vars(hist_data);
5075 unregister_field_var_hists(hist_data);
5077 destroy_hist_data(hist_data);
5081 static struct event_trigger_ops event_hist_trigger_ops = {
5082 .func = event_hist_trigger,
5083 .print = event_hist_trigger_print,
5084 .init = event_hist_trigger_init,
5085 .free = event_hist_trigger_free,
5088 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5089 struct event_trigger_data *data)
5093 save_named_trigger(data->named_data->name, data);
5095 event_hist_trigger_init(ops, data->named_data);
5100 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5101 struct event_trigger_data *data)
5103 if (WARN_ON_ONCE(data->ref <= 0))
5106 event_hist_trigger_free(ops, data->named_data);
5110 del_named_trigger(data);
5111 trigger_data_free(data);
5115 static struct event_trigger_ops event_hist_trigger_named_ops = {
5116 .func = event_hist_trigger,
5117 .print = event_hist_trigger_print,
5118 .init = event_hist_trigger_named_init,
5119 .free = event_hist_trigger_named_free,
5122 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5125 return &event_hist_trigger_ops;
5128 static void hist_clear(struct event_trigger_data *data)
5130 struct hist_trigger_data *hist_data = data->private_data;
5133 pause_named_trigger(data);
5135 tracepoint_synchronize_unregister();
5137 tracing_map_clear(hist_data->map);
5140 unpause_named_trigger(data);
5143 static bool compatible_field(struct ftrace_event_field *field,
5144 struct ftrace_event_field *test_field)
5146 if (field == test_field)
5148 if (field == NULL || test_field == NULL)
5150 if (strcmp(field->name, test_field->name) != 0)
5152 if (strcmp(field->type, test_field->type) != 0)
5154 if (field->size != test_field->size)
5156 if (field->is_signed != test_field->is_signed)
5162 static bool hist_trigger_match(struct event_trigger_data *data,
5163 struct event_trigger_data *data_test,
5164 struct event_trigger_data *named_data,
5167 struct tracing_map_sort_key *sort_key, *sort_key_test;
5168 struct hist_trigger_data *hist_data, *hist_data_test;
5169 struct hist_field *key_field, *key_field_test;
5172 if (named_data && (named_data != data_test) &&
5173 (named_data != data_test->named_data))
5176 if (!named_data && is_named_trigger(data_test))
5179 hist_data = data->private_data;
5180 hist_data_test = data_test->private_data;
5182 if (hist_data->n_vals != hist_data_test->n_vals ||
5183 hist_data->n_fields != hist_data_test->n_fields ||
5184 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5187 if (!ignore_filter) {
5188 if ((data->filter_str && !data_test->filter_str) ||
5189 (!data->filter_str && data_test->filter_str))
5193 for_each_hist_field(i, hist_data) {
5194 key_field = hist_data->fields[i];
5195 key_field_test = hist_data_test->fields[i];
5197 if (key_field->flags != key_field_test->flags)
5199 if (!compatible_field(key_field->field, key_field_test->field))
5201 if (key_field->offset != key_field_test->offset)
5203 if (key_field->size != key_field_test->size)
5205 if (key_field->is_signed != key_field_test->is_signed)
5207 if (!!key_field->var.name != !!key_field_test->var.name)
5209 if (key_field->var.name &&
5210 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5214 for (i = 0; i < hist_data->n_sort_keys; i++) {
5215 sort_key = &hist_data->sort_keys[i];
5216 sort_key_test = &hist_data_test->sort_keys[i];
5218 if (sort_key->field_idx != sort_key_test->field_idx ||
5219 sort_key->descending != sort_key_test->descending)
5223 if (!ignore_filter && data->filter_str &&
5224 (strcmp(data->filter_str, data_test->filter_str) != 0))
5227 if (!actions_match(hist_data, hist_data_test))
5233 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5234 struct event_trigger_data *data,
5235 struct trace_event_file *file)
5237 struct hist_trigger_data *hist_data = data->private_data;
5238 struct event_trigger_data *test, *named_data = NULL;
5241 if (hist_data->attrs->name) {
5242 named_data = find_named_trigger(hist_data->attrs->name);
5244 if (!hist_trigger_match(data, named_data, named_data,
5246 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
5253 if (hist_data->attrs->name && !named_data)
5256 list_for_each_entry_rcu(test, &file->triggers, list) {
5257 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5258 if (!hist_trigger_match(data, test, named_data, false))
5260 if (hist_data->attrs->pause)
5261 test->paused = true;
5262 else if (hist_data->attrs->cont)
5263 test->paused = false;
5264 else if (hist_data->attrs->clear)
5267 hist_err("Hist trigger already exists", NULL);
5274 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5275 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
5280 if (hist_data->attrs->pause)
5281 data->paused = true;
5284 data->private_data = named_data->private_data;
5285 set_named_trigger_data(data, named_data);
5286 data->ops = &event_hist_trigger_named_ops;
5289 if (data->ops->init) {
5290 ret = data->ops->init(data->ops, data);
5295 if (hist_data->enable_timestamps) {
5296 char *clock = hist_data->attrs->clock;
5298 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5300 hist_err("Couldn't set trace_clock: ", clock);
5304 tracing_set_time_stamp_abs(file->tr, true);
5308 destroy_hist_data(hist_data);
5315 static int hist_trigger_enable(struct event_trigger_data *data,
5316 struct trace_event_file *file)
5320 list_add_tail_rcu(&data->list, &file->triggers);
5322 update_cond_flag(file);
5324 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5325 list_del_rcu(&data->list);
5326 update_cond_flag(file);
5333 static bool have_hist_trigger_match(struct event_trigger_data *data,
5334 struct trace_event_file *file)
5336 struct hist_trigger_data *hist_data = data->private_data;
5337 struct event_trigger_data *test, *named_data = NULL;
5340 if (hist_data->attrs->name)
5341 named_data = find_named_trigger(hist_data->attrs->name);
5343 list_for_each_entry_rcu(test, &file->triggers, list) {
5344 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5345 if (hist_trigger_match(data, test, named_data, false)) {
5355 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5356 struct trace_event_file *file)
5358 struct hist_trigger_data *hist_data = data->private_data;
5359 struct event_trigger_data *test, *named_data = NULL;
5361 if (hist_data->attrs->name)
5362 named_data = find_named_trigger(hist_data->attrs->name);
5364 list_for_each_entry_rcu(test, &file->triggers, list) {
5365 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5366 if (!hist_trigger_match(data, test, named_data, false))
5368 hist_data = test->private_data;
5369 if (check_var_refs(hist_data))
5378 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5379 struct event_trigger_data *data,
5380 struct trace_event_file *file)
5382 struct hist_trigger_data *hist_data = data->private_data;
5383 struct event_trigger_data *test, *named_data = NULL;
5384 bool unregistered = false;
5386 if (hist_data->attrs->name)
5387 named_data = find_named_trigger(hist_data->attrs->name);
5389 list_for_each_entry_rcu(test, &file->triggers, list) {
5390 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5391 if (!hist_trigger_match(data, test, named_data, false))
5393 unregistered = true;
5394 list_del_rcu(&test->list);
5395 trace_event_trigger_enable_disable(file, 0);
5396 update_cond_flag(file);
5401 if (unregistered && test->ops->free)
5402 test->ops->free(test->ops, test);
5404 if (hist_data->enable_timestamps) {
5405 if (!hist_data->remove || unregistered)
5406 tracing_set_time_stamp_abs(file->tr, false);
5410 static bool hist_file_check_refs(struct trace_event_file *file)
5412 struct hist_trigger_data *hist_data;
5413 struct event_trigger_data *test;
5415 list_for_each_entry_rcu(test, &file->triggers, list) {
5416 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5417 hist_data = test->private_data;
5418 if (check_var_refs(hist_data))
5426 static void hist_unreg_all(struct trace_event_file *file)
5428 struct event_trigger_data *test, *n;
5429 struct hist_trigger_data *hist_data;
5430 struct synth_event *se;
5431 const char *se_name;
5433 if (hist_file_check_refs(file))
5436 list_for_each_entry_safe(test, n, &file->triggers, list) {
5437 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5438 hist_data = test->private_data;
5439 list_del_rcu(&test->list);
5440 trace_event_trigger_enable_disable(file, 0);
5442 mutex_lock(&synth_event_mutex);
5443 se_name = trace_event_name(file->event_call);
5444 se = find_synth_event(se_name);
5447 mutex_unlock(&synth_event_mutex);
5449 update_cond_flag(file);
5450 if (hist_data->enable_timestamps)
5451 tracing_set_time_stamp_abs(file->tr, false);
5452 if (test->ops->free)
5453 test->ops->free(test->ops, test);
5458 static int event_hist_trigger_func(struct event_command *cmd_ops,
5459 struct trace_event_file *file,
5460 char *glob, char *cmd, char *param)
5462 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5463 struct event_trigger_data *trigger_data;
5464 struct hist_trigger_attrs *attrs;
5465 struct event_trigger_ops *trigger_ops;
5466 struct hist_trigger_data *hist_data;
5467 struct synth_event *se;
5468 const char *se_name;
5469 bool remove = false;
5473 if (glob && strlen(glob)) {
5474 last_cmd_set(param);
5485 * separate the trigger from the filter (k:v [if filter])
5486 * allowing for whitespace in the trigger
5488 p = trigger = param;
5490 p = strstr(p, "if");
5495 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5499 if (p >= param + strlen(param) - strlen("if") - 1)
5501 if (*(p + strlen("if")) != ' ' && *(p + strlen("if")) != '\t') {
5512 param = strstrip(p);
5513 trigger = strstrip(trigger);
5516 attrs = parse_hist_trigger_attrs(trigger);
5518 return PTR_ERR(attrs);
5520 if (attrs->map_bits)
5521 hist_trigger_bits = attrs->map_bits;
5523 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5524 if (IS_ERR(hist_data)) {
5525 destroy_hist_trigger_attrs(attrs);
5526 return PTR_ERR(hist_data);
5529 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5531 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5532 if (!trigger_data) {
5537 trigger_data->count = -1;
5538 trigger_data->ops = trigger_ops;
5539 trigger_data->cmd_ops = cmd_ops;
5541 INIT_LIST_HEAD(&trigger_data->list);
5542 RCU_INIT_POINTER(trigger_data->filter, NULL);
5544 trigger_data->private_data = hist_data;
5546 /* if param is non-empty, it's supposed to be a filter */
5547 if (param && cmd_ops->set_filter) {
5548 ret = cmd_ops->set_filter(param, trigger_data, file);
5554 if (!have_hist_trigger_match(trigger_data, file))
5557 if (hist_trigger_check_refs(trigger_data, file)) {
5562 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5564 mutex_lock(&synth_event_mutex);
5565 se_name = trace_event_name(file->event_call);
5566 se = find_synth_event(se_name);
5569 mutex_unlock(&synth_event_mutex);
5575 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5577 * The above returns on success the # of triggers registered,
5578 * but if it didn't register any it returns zero. Consider no
5579 * triggers registered a failure too.
5582 if (!(attrs->pause || attrs->cont || attrs->clear))
5588 if (get_named_trigger_data(trigger_data))
5591 if (has_hist_vars(hist_data))
5592 save_hist_vars(hist_data);
5594 ret = create_actions(hist_data, file);
5598 ret = tracing_map_init(hist_data->map);
5602 ret = hist_trigger_enable(trigger_data, file);
5606 mutex_lock(&synth_event_mutex);
5607 se_name = trace_event_name(file->event_call);
5608 se = find_synth_event(se_name);
5611 mutex_unlock(&synth_event_mutex);
5613 /* Just return zero, not the number of registered triggers */
5621 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5623 if (cmd_ops->set_filter)
5624 cmd_ops->set_filter(NULL, trigger_data, NULL);
5626 remove_hist_vars(hist_data);
5628 kfree(trigger_data);
5630 destroy_hist_data(hist_data);
5634 static struct event_command trigger_hist_cmd = {
5636 .trigger_type = ETT_EVENT_HIST,
5637 .flags = EVENT_CMD_FL_NEEDS_REC,
5638 .func = event_hist_trigger_func,
5639 .reg = hist_register_trigger,
5640 .unreg = hist_unregister_trigger,
5641 .unreg_all = hist_unreg_all,
5642 .get_trigger_ops = event_hist_get_trigger_ops,
5643 .set_filter = set_trigger_filter,
5646 __init int register_trigger_hist_cmd(void)
5650 ret = register_event_command(&trigger_hist_cmd);
5657 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5658 struct ring_buffer_event *event)
5660 struct enable_trigger_data *enable_data = data->private_data;
5661 struct event_trigger_data *test;
5663 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
5664 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5665 if (enable_data->enable)
5666 test->paused = false;
5668 test->paused = true;
5674 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5675 struct ring_buffer_event *event)
5680 if (data->count != -1)
5683 hist_enable_trigger(data, rec, event);
5686 static struct event_trigger_ops hist_enable_trigger_ops = {
5687 .func = hist_enable_trigger,
5688 .print = event_enable_trigger_print,
5689 .init = event_trigger_init,
5690 .free = event_enable_trigger_free,
5693 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5694 .func = hist_enable_count_trigger,
5695 .print = event_enable_trigger_print,
5696 .init = event_trigger_init,
5697 .free = event_enable_trigger_free,
5700 static struct event_trigger_ops hist_disable_trigger_ops = {
5701 .func = hist_enable_trigger,
5702 .print = event_enable_trigger_print,
5703 .init = event_trigger_init,
5704 .free = event_enable_trigger_free,
5707 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5708 .func = hist_enable_count_trigger,
5709 .print = event_enable_trigger_print,
5710 .init = event_trigger_init,
5711 .free = event_enable_trigger_free,
5714 static struct event_trigger_ops *
5715 hist_enable_get_trigger_ops(char *cmd, char *param)
5717 struct event_trigger_ops *ops;
5720 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5723 ops = param ? &hist_enable_count_trigger_ops :
5724 &hist_enable_trigger_ops;
5726 ops = param ? &hist_disable_count_trigger_ops :
5727 &hist_disable_trigger_ops;
5732 static void hist_enable_unreg_all(struct trace_event_file *file)
5734 struct event_trigger_data *test, *n;
5736 list_for_each_entry_safe(test, n, &file->triggers, list) {
5737 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5738 list_del_rcu(&test->list);
5739 update_cond_flag(file);
5740 trace_event_trigger_enable_disable(file, 0);
5741 if (test->ops->free)
5742 test->ops->free(test->ops, test);
5747 static struct event_command trigger_hist_enable_cmd = {
5748 .name = ENABLE_HIST_STR,
5749 .trigger_type = ETT_HIST_ENABLE,
5750 .func = event_enable_trigger_func,
5751 .reg = event_enable_register_trigger,
5752 .unreg = event_enable_unregister_trigger,
5753 .unreg_all = hist_enable_unreg_all,
5754 .get_trigger_ops = hist_enable_get_trigger_ops,
5755 .set_filter = set_trigger_filter,
5758 static struct event_command trigger_hist_disable_cmd = {
5759 .name = DISABLE_HIST_STR,
5760 .trigger_type = ETT_HIST_ENABLE,
5761 .func = event_enable_trigger_func,
5762 .reg = event_enable_register_trigger,
5763 .unreg = event_enable_unregister_trigger,
5764 .unreg_all = hist_enable_unreg_all,
5765 .get_trigger_ops = hist_enable_get_trigger_ops,
5766 .set_filter = set_trigger_filter,
5769 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5771 unregister_event_command(&trigger_hist_enable_cmd);
5772 unregister_event_command(&trigger_hist_disable_cmd);
5775 __init int register_trigger_hist_enable_disable_cmds(void)
5779 ret = register_event_command(&trigger_hist_enable_cmd);
5780 if (WARN_ON(ret < 0))
5782 ret = register_event_command(&trigger_hist_disable_cmd);
5783 if (WARN_ON(ret < 0))
5784 unregister_trigger_hist_enable_disable_cmds();
5789 static __init int trace_events_hist_init(void)
5791 struct dentry *entry = NULL;
5792 struct dentry *d_tracer;
5795 d_tracer = tracing_init_dentry();
5796 if (IS_ERR(d_tracer)) {
5797 err = PTR_ERR(d_tracer);
5801 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
5802 NULL, &synth_events_fops);
5810 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
5815 fs_initcall(trace_events_hist_init);