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;
55 unsigned int is_signed;
57 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
58 struct hist_trigger_data *hist_data;
60 enum field_op_id operator;
65 unsigned int var_ref_idx;
69 static u64 hist_field_none(struct hist_field *field,
70 struct tracing_map_elt *elt,
71 struct ring_buffer_event *rbe,
77 static u64 hist_field_counter(struct hist_field *field,
78 struct tracing_map_elt *elt,
79 struct ring_buffer_event *rbe,
85 static u64 hist_field_string(struct hist_field *hist_field,
86 struct tracing_map_elt *elt,
87 struct ring_buffer_event *rbe,
90 char *addr = (char *)(event + hist_field->field->offset);
92 return (u64)(unsigned long)addr;
95 static u64 hist_field_dynstring(struct hist_field *hist_field,
96 struct tracing_map_elt *elt,
97 struct ring_buffer_event *rbe,
100 u32 str_item = *(u32 *)(event + hist_field->field->offset);
101 int str_loc = str_item & 0xffff;
102 char *addr = (char *)(event + str_loc);
104 return (u64)(unsigned long)addr;
107 static u64 hist_field_pstring(struct hist_field *hist_field,
108 struct tracing_map_elt *elt,
109 struct ring_buffer_event *rbe,
112 char **addr = (char **)(event + hist_field->field->offset);
114 return (u64)(unsigned long)*addr;
117 static u64 hist_field_log2(struct hist_field *hist_field,
118 struct tracing_map_elt *elt,
119 struct ring_buffer_event *rbe,
122 struct hist_field *operand = hist_field->operands[0];
124 u64 val = operand->fn(operand, elt, rbe, event);
126 return (u64) ilog2(roundup_pow_of_two(val));
129 static u64 hist_field_plus(struct hist_field *hist_field,
130 struct tracing_map_elt *elt,
131 struct ring_buffer_event *rbe,
134 struct hist_field *operand1 = hist_field->operands[0];
135 struct hist_field *operand2 = hist_field->operands[1];
137 u64 val1 = operand1->fn(operand1, elt, rbe, event);
138 u64 val2 = operand2->fn(operand2, elt, rbe, event);
143 static u64 hist_field_minus(struct hist_field *hist_field,
144 struct tracing_map_elt *elt,
145 struct ring_buffer_event *rbe,
148 struct hist_field *operand1 = hist_field->operands[0];
149 struct hist_field *operand2 = hist_field->operands[1];
151 u64 val1 = operand1->fn(operand1, elt, rbe, event);
152 u64 val2 = operand2->fn(operand2, elt, rbe, event);
157 static u64 hist_field_unary_minus(struct hist_field *hist_field,
158 struct tracing_map_elt *elt,
159 struct ring_buffer_event *rbe,
162 struct hist_field *operand = hist_field->operands[0];
164 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
165 u64 val = (u64)-sval;
170 #define DEFINE_HIST_FIELD_FN(type) \
171 static u64 hist_field_##type(struct hist_field *hist_field, \
172 struct tracing_map_elt *elt, \
173 struct ring_buffer_event *rbe, \
176 type *addr = (type *)(event + hist_field->field->offset); \
178 return (u64)(unsigned long)*addr; \
181 DEFINE_HIST_FIELD_FN(s64);
182 DEFINE_HIST_FIELD_FN(u64);
183 DEFINE_HIST_FIELD_FN(s32);
184 DEFINE_HIST_FIELD_FN(u32);
185 DEFINE_HIST_FIELD_FN(s16);
186 DEFINE_HIST_FIELD_FN(u16);
187 DEFINE_HIST_FIELD_FN(s8);
188 DEFINE_HIST_FIELD_FN(u8);
190 #define for_each_hist_field(i, hist_data) \
191 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
193 #define for_each_hist_val_field(i, hist_data) \
194 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
196 #define for_each_hist_key_field(i, hist_data) \
197 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
199 #define HIST_STACKTRACE_DEPTH 16
200 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
201 #define HIST_STACKTRACE_SKIP 5
203 #define HITCOUNT_IDX 0
204 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
206 enum hist_field_flags {
207 HIST_FIELD_FL_HITCOUNT = 1 << 0,
208 HIST_FIELD_FL_KEY = 1 << 1,
209 HIST_FIELD_FL_STRING = 1 << 2,
210 HIST_FIELD_FL_HEX = 1 << 3,
211 HIST_FIELD_FL_SYM = 1 << 4,
212 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
213 HIST_FIELD_FL_EXECNAME = 1 << 6,
214 HIST_FIELD_FL_SYSCALL = 1 << 7,
215 HIST_FIELD_FL_STACKTRACE = 1 << 8,
216 HIST_FIELD_FL_LOG2 = 1 << 9,
217 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
218 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
219 HIST_FIELD_FL_VAR = 1 << 12,
220 HIST_FIELD_FL_EXPR = 1 << 13,
221 HIST_FIELD_FL_VAR_REF = 1 << 14,
222 HIST_FIELD_FL_CPU = 1 << 15,
223 HIST_FIELD_FL_ALIAS = 1 << 16,
228 char *name[TRACING_MAP_VARS_MAX];
229 char *expr[TRACING_MAP_VARS_MAX];
232 struct hist_trigger_attrs {
242 unsigned int map_bits;
244 char *assignment_str[TRACING_MAP_VARS_MAX];
245 unsigned int n_assignments;
247 char *action_str[HIST_ACTIONS_MAX];
248 unsigned int n_actions;
250 struct var_defs var_defs;
254 struct hist_field *var;
255 struct hist_field *val;
258 struct field_var_hist {
259 struct hist_trigger_data *hist_data;
263 struct hist_trigger_data {
264 struct hist_field *fields[HIST_FIELDS_MAX];
267 unsigned int n_fields;
269 unsigned int key_size;
270 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
271 unsigned int n_sort_keys;
272 struct trace_event_file *event_file;
273 struct hist_trigger_attrs *attrs;
274 struct tracing_map *map;
275 bool enable_timestamps;
277 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
278 unsigned int n_var_refs;
280 struct action_data *actions[HIST_ACTIONS_MAX];
281 unsigned int n_actions;
283 struct hist_field *synth_var_refs[SYNTH_FIELDS_MAX];
284 unsigned int n_synth_var_refs;
285 struct field_var *field_vars[SYNTH_FIELDS_MAX];
286 unsigned int n_field_vars;
287 unsigned int n_field_var_str;
288 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
289 unsigned int n_field_var_hists;
291 struct field_var *max_vars[SYNTH_FIELDS_MAX];
292 unsigned int n_max_vars;
293 unsigned int n_max_var_str;
305 struct list_head list;
308 struct synth_field **fields;
309 unsigned int n_fields;
311 struct trace_event_class class;
312 struct trace_event_call call;
313 struct tracepoint *tp;
318 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
319 struct tracing_map_elt *elt, void *rec,
320 struct ring_buffer_event *rbe,
321 struct action_data *data, u64 *var_ref_vals);
325 unsigned int n_params;
326 char *params[SYNTH_FIELDS_MAX];
330 unsigned int var_ref_idx;
332 char *match_event_system;
333 char *synth_event_name;
334 struct synth_event *synth_event;
340 unsigned int max_var_ref_idx;
341 struct hist_field *max_var;
342 struct hist_field *var;
348 static char last_hist_cmd[MAX_FILTER_STR_VAL];
349 static char hist_err_str[MAX_FILTER_STR_VAL];
351 static void last_cmd_set(char *str)
356 strncpy(last_hist_cmd, str, MAX_FILTER_STR_VAL - 1);
359 static void hist_err(char *str, char *var)
361 int maxlen = MAX_FILTER_STR_VAL - 1;
366 if (strlen(hist_err_str))
372 if (strlen(hist_err_str) + strlen(str) + strlen(var) > maxlen)
375 strcat(hist_err_str, str);
376 strcat(hist_err_str, var);
379 static void hist_err_event(char *str, char *system, char *event, char *var)
381 char err[MAX_FILTER_STR_VAL];
384 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s.%s", system, event, var);
386 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
388 strscpy(err, var, MAX_FILTER_STR_VAL);
393 static void hist_err_clear(void)
395 hist_err_str[0] = '\0';
398 static bool have_hist_err(void)
400 if (strlen(hist_err_str))
406 static LIST_HEAD(synth_event_list);
407 static DEFINE_MUTEX(synth_event_mutex);
409 struct synth_trace_event {
410 struct trace_entry ent;
414 static int synth_event_define_fields(struct trace_event_call *call)
416 struct synth_trace_event trace;
417 int offset = offsetof(typeof(trace), fields);
418 struct synth_event *event = call->data;
419 unsigned int i, size, n_u64;
424 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
425 size = event->fields[i]->size;
426 is_signed = event->fields[i]->is_signed;
427 type = event->fields[i]->type;
428 name = event->fields[i]->name;
429 ret = trace_define_field(call, type, name, offset, size,
430 is_signed, FILTER_OTHER);
434 if (event->fields[i]->is_string) {
435 offset += STR_VAR_LEN_MAX;
436 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
438 offset += sizeof(u64);
443 event->n_u64 = n_u64;
448 static bool synth_field_signed(char *type)
450 if (strncmp(type, "u", 1) == 0)
452 if (strcmp(type, "gfp_t") == 0)
458 static int synth_field_is_string(char *type)
460 if (strstr(type, "char[") != NULL)
466 static int synth_field_string_size(char *type)
468 char buf[4], *end, *start;
472 start = strstr(type, "char[");
475 start += strlen("char[");
477 end = strchr(type, ']');
478 if (!end || end < start)
485 strncpy(buf, start, len);
488 err = kstrtouint(buf, 0, &size);
492 if (size > STR_VAR_LEN_MAX)
498 static int synth_field_size(char *type)
502 if (strcmp(type, "s64") == 0)
504 else if (strcmp(type, "u64") == 0)
506 else if (strcmp(type, "s32") == 0)
508 else if (strcmp(type, "u32") == 0)
510 else if (strcmp(type, "s16") == 0)
512 else if (strcmp(type, "u16") == 0)
514 else if (strcmp(type, "s8") == 0)
516 else if (strcmp(type, "u8") == 0)
518 else if (strcmp(type, "char") == 0)
520 else if (strcmp(type, "unsigned char") == 0)
521 size = sizeof(unsigned char);
522 else if (strcmp(type, "int") == 0)
524 else if (strcmp(type, "unsigned int") == 0)
525 size = sizeof(unsigned int);
526 else if (strcmp(type, "long") == 0)
528 else if (strcmp(type, "unsigned long") == 0)
529 size = sizeof(unsigned long);
530 else if (strcmp(type, "pid_t") == 0)
531 size = sizeof(pid_t);
532 else if (synth_field_is_string(type))
533 size = synth_field_string_size(type);
538 static const char *synth_field_fmt(char *type)
540 const char *fmt = "%llu";
542 if (strcmp(type, "s64") == 0)
544 else if (strcmp(type, "u64") == 0)
546 else if (strcmp(type, "s32") == 0)
548 else if (strcmp(type, "u32") == 0)
550 else if (strcmp(type, "s16") == 0)
552 else if (strcmp(type, "u16") == 0)
554 else if (strcmp(type, "s8") == 0)
556 else if (strcmp(type, "u8") == 0)
558 else if (strcmp(type, "char") == 0)
560 else if (strcmp(type, "unsigned char") == 0)
562 else if (strcmp(type, "int") == 0)
564 else if (strcmp(type, "unsigned int") == 0)
566 else if (strcmp(type, "long") == 0)
568 else if (strcmp(type, "unsigned long") == 0)
570 else if (strcmp(type, "pid_t") == 0)
572 else if (synth_field_is_string(type))
578 static enum print_line_t print_synth_event(struct trace_iterator *iter,
580 struct trace_event *event)
582 struct trace_array *tr = iter->tr;
583 struct trace_seq *s = &iter->seq;
584 struct synth_trace_event *entry;
585 struct synth_event *se;
586 unsigned int i, n_u64;
590 entry = (struct synth_trace_event *)iter->ent;
591 se = container_of(event, struct synth_event, call.event);
593 trace_seq_printf(s, "%s: ", se->name);
595 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
596 if (trace_seq_has_overflowed(s))
599 fmt = synth_field_fmt(se->fields[i]->type);
601 /* parameter types */
602 if (tr->trace_flags & TRACE_ITER_VERBOSE)
603 trace_seq_printf(s, "%s ", fmt);
605 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
607 /* parameter values */
608 if (se->fields[i]->is_string) {
609 trace_seq_printf(s, print_fmt, se->fields[i]->name,
610 (char *)&entry->fields[n_u64],
611 i == se->n_fields - 1 ? "" : " ");
612 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
614 trace_seq_printf(s, print_fmt, se->fields[i]->name,
615 entry->fields[n_u64],
616 i == se->n_fields - 1 ? "" : " ");
621 trace_seq_putc(s, '\n');
623 return trace_handle_return(s);
626 static struct trace_event_functions synth_event_funcs = {
627 .trace = print_synth_event
630 static notrace void trace_event_raw_event_synth(void *__data,
632 unsigned int var_ref_idx)
634 struct trace_event_file *trace_file = __data;
635 struct synth_trace_event *entry;
636 struct trace_event_buffer fbuffer;
637 struct ring_buffer *buffer;
638 struct synth_event *event;
639 unsigned int i, n_u64;
642 event = trace_file->event_call->data;
644 if (trace_trigger_soft_disabled(trace_file))
647 fields_size = event->n_u64 * sizeof(u64);
650 * Avoid ring buffer recursion detection, as this event
651 * is being performed within another event.
653 buffer = trace_file->tr->trace_buffer.buffer;
654 ring_buffer_nest_start(buffer);
656 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
657 sizeof(*entry) + fields_size);
661 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
662 if (event->fields[i]->is_string) {
663 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
664 char *str_field = (char *)&entry->fields[n_u64];
666 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
667 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
669 struct synth_field *field = event->fields[i];
670 u64 val = var_ref_vals[var_ref_idx + i];
672 switch (field->size) {
674 *(u8 *)&entry->fields[n_u64] = (u8)val;
678 *(u16 *)&entry->fields[n_u64] = (u16)val;
682 *(u32 *)&entry->fields[n_u64] = (u32)val;
686 entry->fields[n_u64] = val;
693 trace_event_buffer_commit(&fbuffer);
695 ring_buffer_nest_end(buffer);
698 static void free_synth_event_print_fmt(struct trace_event_call *call)
701 kfree(call->print_fmt);
702 call->print_fmt = NULL;
706 static int __set_synth_event_print_fmt(struct synth_event *event,
713 /* When len=0, we just calculate the needed length */
714 #define LEN_OR_ZERO (len ? len - pos : 0)
716 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
717 for (i = 0; i < event->n_fields; i++) {
718 fmt = synth_field_fmt(event->fields[i]->type);
719 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
720 event->fields[i]->name, fmt,
721 i == event->n_fields - 1 ? "" : ", ");
723 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
725 for (i = 0; i < event->n_fields; i++) {
726 pos += snprintf(buf + pos, LEN_OR_ZERO,
727 ", REC->%s", event->fields[i]->name);
732 /* return the length of print_fmt */
736 static int set_synth_event_print_fmt(struct trace_event_call *call)
738 struct synth_event *event = call->data;
742 /* First: called with 0 length to calculate the needed length */
743 len = __set_synth_event_print_fmt(event, NULL, 0);
745 print_fmt = kmalloc(len + 1, GFP_KERNEL);
749 /* Second: actually write the @print_fmt */
750 __set_synth_event_print_fmt(event, print_fmt, len + 1);
751 call->print_fmt = print_fmt;
756 static void free_synth_field(struct synth_field *field)
763 static struct synth_field *parse_synth_field(int argc, char **argv,
766 struct synth_field *field;
767 const char *prefix = NULL;
768 char *field_type = argv[0], *field_name;
772 if (field_type[0] == ';')
775 if (!strcmp(field_type, "unsigned")) {
777 return ERR_PTR(-EINVAL);
778 prefix = "unsigned ";
779 field_type = argv[1];
780 field_name = argv[2];
783 field_name = argv[1];
787 len = strlen(field_name);
788 if (field_name[len - 1] == ';')
789 field_name[len - 1] = '\0';
791 field = kzalloc(sizeof(*field), GFP_KERNEL);
793 return ERR_PTR(-ENOMEM);
795 len = strlen(field_type) + 1;
796 array = strchr(field_name, '[');
798 len += strlen(array);
800 len += strlen(prefix);
801 field->type = kzalloc(len, GFP_KERNEL);
807 strcat(field->type, prefix);
808 strcat(field->type, field_type);
810 strcat(field->type, array);
814 field->size = synth_field_size(field->type);
820 if (synth_field_is_string(field->type))
821 field->is_string = true;
823 field->is_signed = synth_field_signed(field->type);
825 field->name = kstrdup(field_name, GFP_KERNEL);
833 free_synth_field(field);
834 field = ERR_PTR(ret);
838 static void free_synth_tracepoint(struct tracepoint *tp)
847 static struct tracepoint *alloc_synth_tracepoint(char *name)
849 struct tracepoint *tp;
851 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
853 return ERR_PTR(-ENOMEM);
855 tp->name = kstrdup(name, GFP_KERNEL);
858 return ERR_PTR(-ENOMEM);
864 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
865 unsigned int var_ref_idx);
867 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
868 unsigned int var_ref_idx)
870 struct tracepoint *tp = event->tp;
872 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
873 struct tracepoint_func *probe_func_ptr;
874 synth_probe_func_t probe_func;
877 if (!(cpu_online(raw_smp_processor_id())))
880 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
881 if (probe_func_ptr) {
883 probe_func = probe_func_ptr->func;
884 __data = probe_func_ptr->data;
885 probe_func(__data, var_ref_vals, var_ref_idx);
886 } while ((++probe_func_ptr)->func);
891 static struct synth_event *find_synth_event(const char *name)
893 struct synth_event *event;
895 list_for_each_entry(event, &synth_event_list, list) {
896 if (strcmp(event->name, name) == 0)
903 static int register_synth_event(struct synth_event *event)
905 struct trace_event_call *call = &event->call;
908 event->call.class = &event->class;
909 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
910 if (!event->class.system) {
915 event->tp = alloc_synth_tracepoint(event->name);
916 if (IS_ERR(event->tp)) {
917 ret = PTR_ERR(event->tp);
922 INIT_LIST_HEAD(&call->class->fields);
923 call->event.funcs = &synth_event_funcs;
924 call->class->define_fields = synth_event_define_fields;
926 ret = register_trace_event(&call->event);
931 call->flags = TRACE_EVENT_FL_TRACEPOINT;
932 call->class->reg = trace_event_reg;
933 call->class->probe = trace_event_raw_event_synth;
935 call->tp = event->tp;
937 ret = trace_add_event_call_nolock(call);
939 pr_warn("Failed to register synthetic event: %s\n",
940 trace_event_name(call));
944 ret = set_synth_event_print_fmt(call);
946 trace_remove_event_call(call);
952 unregister_trace_event(&call->event);
956 static int unregister_synth_event(struct synth_event *event)
958 struct trace_event_call *call = &event->call;
961 ret = trace_remove_event_call_nolock(call);
966 static void free_synth_event(struct synth_event *event)
973 for (i = 0; i < event->n_fields; i++)
974 free_synth_field(event->fields[i]);
976 kfree(event->fields);
978 kfree(event->class.system);
979 free_synth_tracepoint(event->tp);
980 free_synth_event_print_fmt(&event->call);
984 static struct synth_event *alloc_synth_event(char *event_name, int n_fields,
985 struct synth_field **fields)
987 struct synth_event *event;
990 event = kzalloc(sizeof(*event), GFP_KERNEL);
992 event = ERR_PTR(-ENOMEM);
996 event->name = kstrdup(event_name, GFP_KERNEL);
999 event = ERR_PTR(-ENOMEM);
1003 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1004 if (!event->fields) {
1005 free_synth_event(event);
1006 event = ERR_PTR(-ENOMEM);
1010 for (i = 0; i < n_fields; i++)
1011 event->fields[i] = fields[i];
1013 event->n_fields = n_fields;
1018 static void action_trace(struct hist_trigger_data *hist_data,
1019 struct tracing_map_elt *elt, void *rec,
1020 struct ring_buffer_event *rbe,
1021 struct action_data *data, u64 *var_ref_vals)
1023 struct synth_event *event = data->onmatch.synth_event;
1025 trace_synth(event, var_ref_vals, data->onmatch.var_ref_idx);
1028 struct hist_var_data {
1029 struct list_head list;
1030 struct hist_trigger_data *hist_data;
1033 static void add_or_delete_synth_event(struct synth_event *event, int delete)
1036 free_synth_event(event);
1038 if (!find_synth_event(event->name))
1039 list_add(&event->list, &synth_event_list);
1041 free_synth_event(event);
1045 static int create_synth_event(int argc, char **argv)
1047 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1048 struct synth_event *event = NULL;
1049 bool delete_event = false;
1050 int i, consumed = 0, n_fields = 0, ret = 0;
1053 mutex_lock(&event_mutex);
1054 mutex_lock(&synth_event_mutex);
1058 * - Add synthetic event: <event_name> field[;field] ...
1059 * - Remove synthetic event: !<event_name> field[;field] ...
1060 * where 'field' = type field_name
1068 if (name[0] == '!') {
1069 delete_event = true;
1073 event = find_synth_event(name);
1081 list_del(&event->list);
1087 } else if (delete_event) {
1097 for (i = 1; i < argc - 1; i++) {
1098 if (strcmp(argv[i], ";") == 0)
1100 if (n_fields == SYNTH_FIELDS_MAX) {
1105 field = parse_synth_field(argc - i, &argv[i], &consumed);
1106 if (IS_ERR(field)) {
1107 ret = PTR_ERR(field);
1110 fields[n_fields++] = field;
1114 if (i < argc && strcmp(argv[i], ";") != 0) {
1119 event = alloc_synth_event(name, n_fields, fields);
1120 if (IS_ERR(event)) {
1121 ret = PTR_ERR(event);
1128 ret = unregister_synth_event(event);
1129 add_or_delete_synth_event(event, !ret);
1131 ret = register_synth_event(event);
1132 add_or_delete_synth_event(event, ret);
1135 mutex_unlock(&synth_event_mutex);
1136 mutex_unlock(&event_mutex);
1140 mutex_unlock(&synth_event_mutex);
1141 mutex_unlock(&event_mutex);
1143 for (i = 0; i < n_fields; i++)
1144 free_synth_field(fields[i]);
1145 free_synth_event(event);
1150 static int release_all_synth_events(void)
1152 struct synth_event *event, *e;
1155 mutex_lock(&event_mutex);
1156 mutex_lock(&synth_event_mutex);
1158 list_for_each_entry(event, &synth_event_list, list) {
1160 mutex_unlock(&synth_event_mutex);
1165 list_for_each_entry_safe(event, e, &synth_event_list, list) {
1166 list_del(&event->list);
1168 ret = unregister_synth_event(event);
1169 add_or_delete_synth_event(event, !ret);
1171 mutex_unlock(&synth_event_mutex);
1172 mutex_unlock(&event_mutex);
1178 static void *synth_events_seq_start(struct seq_file *m, loff_t *pos)
1180 mutex_lock(&synth_event_mutex);
1182 return seq_list_start(&synth_event_list, *pos);
1185 static void *synth_events_seq_next(struct seq_file *m, void *v, loff_t *pos)
1187 return seq_list_next(v, &synth_event_list, pos);
1190 static void synth_events_seq_stop(struct seq_file *m, void *v)
1192 mutex_unlock(&synth_event_mutex);
1195 static int synth_events_seq_show(struct seq_file *m, void *v)
1197 struct synth_field *field;
1198 struct synth_event *event = v;
1201 seq_printf(m, "%s\t", event->name);
1203 for (i = 0; i < event->n_fields; i++) {
1204 field = event->fields[i];
1206 /* parameter values */
1207 seq_printf(m, "%s %s%s", field->type, field->name,
1208 i == event->n_fields - 1 ? "" : "; ");
1216 static const struct seq_operations synth_events_seq_op = {
1217 .start = synth_events_seq_start,
1218 .next = synth_events_seq_next,
1219 .stop = synth_events_seq_stop,
1220 .show = synth_events_seq_show
1223 static int synth_events_open(struct inode *inode, struct file *file)
1227 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1228 ret = release_all_synth_events();
1233 return seq_open(file, &synth_events_seq_op);
1236 static ssize_t synth_events_write(struct file *file,
1237 const char __user *buffer,
1238 size_t count, loff_t *ppos)
1240 return trace_parse_run_command(file, buffer, count, ppos,
1241 create_synth_event);
1244 static const struct file_operations synth_events_fops = {
1245 .open = synth_events_open,
1246 .write = synth_events_write,
1248 .llseek = seq_lseek,
1249 .release = seq_release,
1252 static u64 hist_field_timestamp(struct hist_field *hist_field,
1253 struct tracing_map_elt *elt,
1254 struct ring_buffer_event *rbe,
1257 struct hist_trigger_data *hist_data = hist_field->hist_data;
1258 struct trace_array *tr = hist_data->event_file->tr;
1260 u64 ts = ring_buffer_event_time_stamp(rbe);
1262 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1268 static u64 hist_field_cpu(struct hist_field *hist_field,
1269 struct tracing_map_elt *elt,
1270 struct ring_buffer_event *rbe,
1273 int cpu = smp_processor_id();
1279 * check_field_for_var_ref - Check if a VAR_REF field references a variable
1280 * @hist_field: The VAR_REF field to check
1281 * @var_data: The hist trigger that owns the variable
1282 * @var_idx: The trigger variable identifier
1284 * Check the given VAR_REF field to see whether or not it references
1285 * the given variable associated with the given trigger.
1287 * Return: The VAR_REF field if it does reference the variable, NULL if not
1289 static struct hist_field *
1290 check_field_for_var_ref(struct hist_field *hist_field,
1291 struct hist_trigger_data *var_data,
1292 unsigned int var_idx)
1294 struct hist_field *found = NULL;
1296 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF) {
1297 if (hist_field->var.idx == var_idx &&
1298 hist_field->var.hist_data == var_data) {
1306 static struct hist_field *
1307 check_field_for_var_refs(struct hist_trigger_data *hist_data,
1308 struct hist_field *hist_field,
1309 struct hist_trigger_data *var_data,
1310 unsigned int var_idx,
1313 struct hist_field *found = NULL;
1322 found = check_field_for_var_ref(hist_field, var_data, var_idx);
1326 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1327 struct hist_field *operand;
1329 operand = hist_field->operands[i];
1330 found = check_field_for_var_refs(hist_data, operand, var_data,
1331 var_idx, level + 1);
1340 * find_var_ref - Check if a trigger has a reference to a trigger variable
1341 * @hist_data: The hist trigger that might have a reference to the variable
1342 * @var_data: The hist trigger that owns the variable
1343 * @var_idx: The trigger variable identifier
1345 * Check the list of var_refs[] on the first hist trigger to see
1346 * whether any of them are references to the variable on the second
1349 * Return: The VAR_REF field referencing the variable if so, NULL if not
1351 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1352 struct hist_trigger_data *var_data,
1353 unsigned int var_idx)
1355 struct hist_field *hist_field, *found = NULL;
1358 for_each_hist_field(i, hist_data) {
1359 hist_field = hist_data->fields[i];
1360 found = check_field_for_var_refs(hist_data, hist_field,
1361 var_data, var_idx, 0);
1366 for (i = 0; i < hist_data->n_synth_var_refs; i++) {
1367 hist_field = hist_data->synth_var_refs[i];
1368 found = check_field_for_var_refs(hist_data, hist_field,
1369 var_data, var_idx, 0);
1378 * find_any_var_ref - Check if there is a reference to a given trigger variable
1379 * @hist_data: The hist trigger
1380 * @var_idx: The trigger variable identifier
1382 * Check to see whether the given variable is currently referenced by
1383 * any other trigger.
1385 * The trigger the variable is defined on is explicitly excluded - the
1386 * assumption being that a self-reference doesn't prevent a trigger
1387 * from being removed.
1389 * Return: The VAR_REF field referencing the variable if so, NULL if not
1391 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1392 unsigned int var_idx)
1394 struct trace_array *tr = hist_data->event_file->tr;
1395 struct hist_field *found = NULL;
1396 struct hist_var_data *var_data;
1398 list_for_each_entry(var_data, &tr->hist_vars, list) {
1399 if (var_data->hist_data == hist_data)
1401 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1410 * check_var_refs - Check if there is a reference to any of trigger's variables
1411 * @hist_data: The hist trigger
1413 * A trigger can define one or more variables. If any one of them is
1414 * currently referenced by any other trigger, this function will
1417 * Typically used to determine whether or not a trigger can be removed
1418 * - if there are any references to a trigger's variables, it cannot.
1420 * Return: True if there is a reference to any of trigger's variables
1422 static bool check_var_refs(struct hist_trigger_data *hist_data)
1424 struct hist_field *field;
1428 for_each_hist_field(i, hist_data) {
1429 field = hist_data->fields[i];
1430 if (field && field->flags & HIST_FIELD_FL_VAR) {
1431 if (find_any_var_ref(hist_data, field->var.idx)) {
1441 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1443 struct trace_array *tr = hist_data->event_file->tr;
1444 struct hist_var_data *var_data, *found = NULL;
1446 list_for_each_entry(var_data, &tr->hist_vars, list) {
1447 if (var_data->hist_data == hist_data) {
1456 static bool field_has_hist_vars(struct hist_field *hist_field,
1467 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1468 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1471 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1472 struct hist_field *operand;
1474 operand = hist_field->operands[i];
1475 if (field_has_hist_vars(operand, level + 1))
1482 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1484 struct hist_field *hist_field;
1487 for_each_hist_field(i, hist_data) {
1488 hist_field = hist_data->fields[i];
1489 if (field_has_hist_vars(hist_field, 0))
1496 static int save_hist_vars(struct hist_trigger_data *hist_data)
1498 struct trace_array *tr = hist_data->event_file->tr;
1499 struct hist_var_data *var_data;
1501 var_data = find_hist_vars(hist_data);
1505 if (trace_array_get(tr) < 0)
1508 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1510 trace_array_put(tr);
1514 var_data->hist_data = hist_data;
1515 list_add(&var_data->list, &tr->hist_vars);
1520 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1522 struct trace_array *tr = hist_data->event_file->tr;
1523 struct hist_var_data *var_data;
1525 var_data = find_hist_vars(hist_data);
1529 if (WARN_ON(check_var_refs(hist_data)))
1532 list_del(&var_data->list);
1536 trace_array_put(tr);
1539 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1540 const char *var_name)
1542 struct hist_field *hist_field, *found = NULL;
1545 for_each_hist_field(i, hist_data) {
1546 hist_field = hist_data->fields[i];
1547 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1548 strcmp(hist_field->var.name, var_name) == 0) {
1557 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1558 struct trace_event_file *file,
1559 const char *var_name)
1561 struct hist_trigger_data *test_data;
1562 struct event_trigger_data *test;
1563 struct hist_field *hist_field;
1565 lockdep_assert_held(&event_mutex);
1567 hist_field = find_var_field(hist_data, var_name);
1571 list_for_each_entry(test, &file->triggers, list) {
1572 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1573 test_data = test->private_data;
1574 hist_field = find_var_field(test_data, var_name);
1583 static struct trace_event_file *find_var_file(struct trace_array *tr,
1588 struct hist_trigger_data *var_hist_data;
1589 struct hist_var_data *var_data;
1590 struct trace_event_file *file, *found = NULL;
1593 return find_event_file(tr, system, event_name);
1595 list_for_each_entry(var_data, &tr->hist_vars, list) {
1596 var_hist_data = var_data->hist_data;
1597 file = var_hist_data->event_file;
1601 if (find_var_field(var_hist_data, var_name)) {
1603 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1614 static struct hist_field *find_file_var(struct trace_event_file *file,
1615 const char *var_name)
1617 struct hist_trigger_data *test_data;
1618 struct event_trigger_data *test;
1619 struct hist_field *hist_field;
1621 lockdep_assert_held(&event_mutex);
1623 list_for_each_entry(test, &file->triggers, list) {
1624 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1625 test_data = test->private_data;
1626 hist_field = find_var_field(test_data, var_name);
1635 static struct hist_field *
1636 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1638 struct trace_array *tr = hist_data->event_file->tr;
1639 struct hist_field *hist_field, *found = NULL;
1640 struct trace_event_file *file;
1643 for (i = 0; i < hist_data->n_actions; i++) {
1644 struct action_data *data = hist_data->actions[i];
1646 if (data->fn == action_trace) {
1647 char *system = data->onmatch.match_event_system;
1648 char *event_name = data->onmatch.match_event;
1650 file = find_var_file(tr, system, event_name, var_name);
1653 hist_field = find_file_var(file, var_name);
1656 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1657 return ERR_PTR(-EINVAL);
1667 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1672 struct trace_array *tr = hist_data->event_file->tr;
1673 struct hist_field *hist_field = NULL;
1674 struct trace_event_file *file;
1676 if (!system || !event_name) {
1677 hist_field = find_match_var(hist_data, var_name);
1678 if (IS_ERR(hist_field))
1684 file = find_var_file(tr, system, event_name, var_name);
1688 hist_field = find_file_var(file, var_name);
1693 struct hist_elt_data {
1696 char *field_var_str[SYNTH_FIELDS_MAX];
1699 static u64 hist_field_var_ref(struct hist_field *hist_field,
1700 struct tracing_map_elt *elt,
1701 struct ring_buffer_event *rbe,
1704 struct hist_elt_data *elt_data;
1707 if (WARN_ON_ONCE(!elt))
1710 elt_data = elt->private_data;
1711 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1716 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1717 u64 *var_ref_vals, bool self)
1719 struct hist_trigger_data *var_data;
1720 struct tracing_map_elt *var_elt;
1721 struct hist_field *hist_field;
1722 unsigned int i, var_idx;
1723 bool resolved = true;
1726 for (i = 0; i < hist_data->n_var_refs; i++) {
1727 hist_field = hist_data->var_refs[i];
1728 var_idx = hist_field->var.idx;
1729 var_data = hist_field->var.hist_data;
1731 if (var_data == NULL) {
1736 if ((self && var_data != hist_data) ||
1737 (!self && var_data == hist_data))
1740 var_elt = tracing_map_lookup(var_data->map, key);
1746 if (!tracing_map_var_set(var_elt, var_idx)) {
1751 if (self || !hist_field->read_once)
1752 var_val = tracing_map_read_var(var_elt, var_idx);
1754 var_val = tracing_map_read_var_once(var_elt, var_idx);
1756 var_ref_vals[i] = var_val;
1762 static const char *hist_field_name(struct hist_field *field,
1765 const char *field_name = "";
1771 field_name = field->field->name;
1772 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1773 field->flags & HIST_FIELD_FL_ALIAS)
1774 field_name = hist_field_name(field->operands[0], ++level);
1775 else if (field->flags & HIST_FIELD_FL_CPU)
1776 field_name = "common_cpu";
1777 else if (field->flags & HIST_FIELD_FL_EXPR ||
1778 field->flags & HIST_FIELD_FL_VAR_REF) {
1779 if (field->system) {
1780 static char full_name[MAX_FILTER_STR_VAL];
1782 strcat(full_name, field->system);
1783 strcat(full_name, ".");
1784 strcat(full_name, field->event_name);
1785 strcat(full_name, ".");
1786 strcat(full_name, field->name);
1787 field_name = full_name;
1789 field_name = field->name;
1790 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1791 field_name = "common_timestamp";
1793 if (field_name == NULL)
1799 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1801 hist_field_fn_t fn = NULL;
1803 switch (field_size) {
1805 if (field_is_signed)
1806 fn = hist_field_s64;
1808 fn = hist_field_u64;
1811 if (field_is_signed)
1812 fn = hist_field_s32;
1814 fn = hist_field_u32;
1817 if (field_is_signed)
1818 fn = hist_field_s16;
1820 fn = hist_field_u16;
1823 if (field_is_signed)
1833 static int parse_map_size(char *str)
1835 unsigned long size, map_bits;
1844 ret = kstrtoul(str, 0, &size);
1848 map_bits = ilog2(roundup_pow_of_two(size));
1849 if (map_bits < TRACING_MAP_BITS_MIN ||
1850 map_bits > TRACING_MAP_BITS_MAX)
1858 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1865 for (i = 0; i < attrs->n_assignments; i++)
1866 kfree(attrs->assignment_str[i]);
1868 for (i = 0; i < attrs->n_actions; i++)
1869 kfree(attrs->action_str[i]);
1872 kfree(attrs->sort_key_str);
1873 kfree(attrs->keys_str);
1874 kfree(attrs->vals_str);
1875 kfree(attrs->clock);
1879 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1883 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1886 if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) ||
1887 (strncmp(str, "onmax(", strlen("onmax(")) == 0)) {
1888 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1889 if (!attrs->action_str[attrs->n_actions]) {
1900 static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
1904 if ((strncmp(str, "key=", strlen("key=")) == 0) ||
1905 (strncmp(str, "keys=", strlen("keys=")) == 0)) {
1906 attrs->keys_str = kstrdup(str, GFP_KERNEL);
1907 if (!attrs->keys_str) {
1911 } else if ((strncmp(str, "val=", strlen("val=")) == 0) ||
1912 (strncmp(str, "vals=", strlen("vals=")) == 0) ||
1913 (strncmp(str, "values=", strlen("values=")) == 0)) {
1914 attrs->vals_str = kstrdup(str, GFP_KERNEL);
1915 if (!attrs->vals_str) {
1919 } else if (strncmp(str, "sort=", strlen("sort=")) == 0) {
1920 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
1921 if (!attrs->sort_key_str) {
1925 } else if (strncmp(str, "name=", strlen("name=")) == 0) {
1926 attrs->name = kstrdup(str, GFP_KERNEL);
1931 } else if (strncmp(str, "clock=", strlen("clock=")) == 0) {
1938 str = strstrip(str);
1939 attrs->clock = kstrdup(str, GFP_KERNEL);
1940 if (!attrs->clock) {
1944 } else if (strncmp(str, "size=", strlen("size=")) == 0) {
1945 int map_bits = parse_map_size(str);
1951 attrs->map_bits = map_bits;
1955 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1956 hist_err("Too many variables defined: ", str);
1961 assignment = kstrdup(str, GFP_KERNEL);
1967 attrs->assignment_str[attrs->n_assignments++] = assignment;
1973 static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
1975 struct hist_trigger_attrs *attrs;
1978 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1980 return ERR_PTR(-ENOMEM);
1982 while (trigger_str) {
1983 char *str = strsep(&trigger_str, ":");
1985 if (strchr(str, '=')) {
1986 ret = parse_assignment(str, attrs);
1989 } else if (strcmp(str, "pause") == 0)
1990 attrs->pause = true;
1991 else if ((strcmp(str, "cont") == 0) ||
1992 (strcmp(str, "continue") == 0))
1994 else if (strcmp(str, "clear") == 0)
1995 attrs->clear = true;
1997 ret = parse_action(str, attrs);
2003 if (!attrs->keys_str) {
2008 if (!attrs->clock) {
2009 attrs->clock = kstrdup("global", GFP_KERNEL);
2010 if (!attrs->clock) {
2018 destroy_hist_trigger_attrs(attrs);
2020 return ERR_PTR(ret);
2023 static inline void save_comm(char *comm, struct task_struct *task)
2026 strcpy(comm, "<idle>");
2030 if (WARN_ON_ONCE(task->pid < 0)) {
2031 strcpy(comm, "<XXX>");
2035 memcpy(comm, task->comm, TASK_COMM_LEN);
2038 static void hist_elt_data_free(struct hist_elt_data *elt_data)
2042 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2043 kfree(elt_data->field_var_str[i]);
2045 kfree(elt_data->comm);
2049 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2051 struct hist_elt_data *elt_data = elt->private_data;
2053 hist_elt_data_free(elt_data);
2056 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
2058 struct hist_trigger_data *hist_data = elt->map->private_data;
2059 unsigned int size = TASK_COMM_LEN;
2060 struct hist_elt_data *elt_data;
2061 struct hist_field *key_field;
2062 unsigned int i, n_str;
2064 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2068 for_each_hist_key_field(i, hist_data) {
2069 key_field = hist_data->fields[i];
2071 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
2072 elt_data->comm = kzalloc(size, GFP_KERNEL);
2073 if (!elt_data->comm) {
2081 n_str = hist_data->n_field_var_str + hist_data->n_max_var_str;
2083 size = STR_VAR_LEN_MAX;
2085 for (i = 0; i < n_str; i++) {
2086 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2087 if (!elt_data->field_var_str[i]) {
2088 hist_elt_data_free(elt_data);
2093 elt->private_data = elt_data;
2098 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2100 struct hist_elt_data *elt_data = elt->private_data;
2103 save_comm(elt_data->comm, current);
2106 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2107 .elt_alloc = hist_trigger_elt_data_alloc,
2108 .elt_free = hist_trigger_elt_data_free,
2109 .elt_init = hist_trigger_elt_data_init,
2112 static const char *get_hist_field_flags(struct hist_field *hist_field)
2114 const char *flags_str = NULL;
2116 if (hist_field->flags & HIST_FIELD_FL_HEX)
2118 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2120 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2121 flags_str = "sym-offset";
2122 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2123 flags_str = "execname";
2124 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2125 flags_str = "syscall";
2126 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2128 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2129 flags_str = "usecs";
2134 static void expr_field_str(struct hist_field *field, char *expr)
2136 if (field->flags & HIST_FIELD_FL_VAR_REF)
2139 strcat(expr, hist_field_name(field, 0));
2141 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2142 const char *flags_str = get_hist_field_flags(field);
2146 strcat(expr, flags_str);
2151 static char *expr_str(struct hist_field *field, unsigned int level)
2158 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2162 if (!field->operands[0]) {
2163 expr_field_str(field, expr);
2167 if (field->operator == FIELD_OP_UNARY_MINUS) {
2171 subexpr = expr_str(field->operands[0], ++level);
2176 strcat(expr, subexpr);
2184 expr_field_str(field->operands[0], expr);
2186 switch (field->operator) {
2187 case FIELD_OP_MINUS:
2198 expr_field_str(field->operands[1], expr);
2203 static int contains_operator(char *str)
2205 enum field_op_id field_op = FIELD_OP_NONE;
2208 op = strpbrk(str, "+-");
2210 return FIELD_OP_NONE;
2215 * Unfortunately, the modifier ".sym-offset"
2216 * can confuse things.
2218 if (op - str >= 4 && !strncmp(op - 4, ".sym-offset", 11))
2219 return FIELD_OP_NONE;
2222 field_op = FIELD_OP_UNARY_MINUS;
2224 field_op = FIELD_OP_MINUS;
2227 field_op = FIELD_OP_PLUS;
2236 static void get_hist_field(struct hist_field *hist_field)
2241 static void __destroy_hist_field(struct hist_field *hist_field)
2243 if (--hist_field->ref > 1)
2246 kfree(hist_field->var.name);
2247 kfree(hist_field->name);
2248 kfree(hist_field->type);
2253 static void destroy_hist_field(struct hist_field *hist_field,
2264 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2265 return; /* var refs will be destroyed separately */
2267 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2268 destroy_hist_field(hist_field->operands[i], level + 1);
2270 __destroy_hist_field(hist_field);
2273 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2274 struct ftrace_event_field *field,
2275 unsigned long flags,
2278 struct hist_field *hist_field;
2280 if (field && is_function_field(field))
2283 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2287 hist_field->ref = 1;
2289 hist_field->hist_data = hist_data;
2291 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2292 goto out; /* caller will populate */
2294 if (flags & HIST_FIELD_FL_VAR_REF) {
2295 hist_field->fn = hist_field_var_ref;
2299 if (flags & HIST_FIELD_FL_HITCOUNT) {
2300 hist_field->fn = hist_field_counter;
2301 hist_field->size = sizeof(u64);
2302 hist_field->type = kstrdup("u64", GFP_KERNEL);
2303 if (!hist_field->type)
2308 if (flags & HIST_FIELD_FL_STACKTRACE) {
2309 hist_field->fn = hist_field_none;
2313 if (flags & HIST_FIELD_FL_LOG2) {
2314 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2315 hist_field->fn = hist_field_log2;
2316 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2317 hist_field->size = hist_field->operands[0]->size;
2318 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2319 if (!hist_field->type)
2324 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2325 hist_field->fn = hist_field_timestamp;
2326 hist_field->size = sizeof(u64);
2327 hist_field->type = kstrdup("u64", GFP_KERNEL);
2328 if (!hist_field->type)
2333 if (flags & HIST_FIELD_FL_CPU) {
2334 hist_field->fn = hist_field_cpu;
2335 hist_field->size = sizeof(int);
2336 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2337 if (!hist_field->type)
2342 if (WARN_ON_ONCE(!field))
2345 /* Pointers to strings are just pointers and dangerous to dereference */
2346 if (is_string_field(field) &&
2347 (field->filter_type != FILTER_PTR_STRING)) {
2348 flags |= HIST_FIELD_FL_STRING;
2350 hist_field->size = MAX_FILTER_STR_VAL;
2351 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2352 if (!hist_field->type)
2355 if (field->filter_type == FILTER_STATIC_STRING)
2356 hist_field->fn = hist_field_string;
2357 else if (field->filter_type == FILTER_DYN_STRING)
2358 hist_field->fn = hist_field_dynstring;
2360 hist_field->fn = hist_field_pstring;
2362 hist_field->size = field->size;
2363 hist_field->is_signed = field->is_signed;
2364 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2365 if (!hist_field->type)
2368 hist_field->fn = select_value_fn(field->size,
2370 if (!hist_field->fn) {
2371 destroy_hist_field(hist_field, 0);
2376 hist_field->field = field;
2377 hist_field->flags = flags;
2380 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2381 if (!hist_field->var.name)
2387 destroy_hist_field(hist_field, 0);
2391 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2395 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2396 if (hist_data->fields[i]) {
2397 destroy_hist_field(hist_data->fields[i], 0);
2398 hist_data->fields[i] = NULL;
2402 for (i = 0; i < hist_data->n_var_refs; i++) {
2403 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2404 __destroy_hist_field(hist_data->var_refs[i]);
2405 hist_data->var_refs[i] = NULL;
2409 static int init_var_ref(struct hist_field *ref_field,
2410 struct hist_field *var_field,
2411 char *system, char *event_name)
2415 ref_field->var.idx = var_field->var.idx;
2416 ref_field->var.hist_data = var_field->hist_data;
2417 ref_field->size = var_field->size;
2418 ref_field->is_signed = var_field->is_signed;
2419 ref_field->flags |= var_field->flags &
2420 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2423 ref_field->system = kstrdup(system, GFP_KERNEL);
2424 if (!ref_field->system)
2429 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2430 if (!ref_field->event_name) {
2436 if (var_field->var.name) {
2437 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2438 if (!ref_field->name) {
2442 } else if (var_field->name) {
2443 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2444 if (!ref_field->name) {
2450 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2451 if (!ref_field->type) {
2458 kfree(ref_field->system);
2459 ref_field->system = NULL;
2460 kfree(ref_field->event_name);
2461 ref_field->event_name = NULL;
2462 kfree(ref_field->name);
2463 ref_field->name = NULL;
2469 * create_var_ref - Create a variable reference and attach it to trigger
2470 * @hist_data: The trigger that will be referencing the variable
2471 * @var_field: The VAR field to create a reference to
2472 * @system: The optional system string
2473 * @event_name: The optional event_name string
2475 * Given a variable hist_field, create a VAR_REF hist_field that
2476 * represents a reference to it.
2478 * This function also adds the reference to the trigger that
2479 * now references the variable.
2481 * Return: The VAR_REF field if successful, NULL if not
2483 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2484 struct hist_field *var_field,
2485 char *system, char *event_name)
2487 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2488 struct hist_field *ref_field;
2491 /* Check if the variable already exists */
2492 for (i = 0; i < hist_data->n_var_refs; i++) {
2493 ref_field = hist_data->var_refs[i];
2494 if (ref_field->var.idx == var_field->var.idx &&
2495 ref_field->var.hist_data == var_field->hist_data) {
2496 get_hist_field(ref_field);
2501 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2503 if (init_var_ref(ref_field, var_field, system, event_name)) {
2504 destroy_hist_field(ref_field, 0);
2508 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2509 ref_field->var_ref_idx = hist_data->n_var_refs++;
2515 static bool is_var_ref(char *var_name)
2517 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2523 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2529 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2530 name = hist_data->attrs->var_defs.name[i];
2532 if (strcmp(var_name, name) == 0) {
2533 field = hist_data->attrs->var_defs.expr[i];
2534 if (contains_operator(field) || is_var_ref(field))
2543 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2544 char *system, char *event_name,
2547 struct trace_event_call *call;
2549 if (system && event_name) {
2550 call = hist_data->event_file->event_call;
2552 if (strcmp(system, call->class->system) != 0)
2555 if (strcmp(event_name, trace_event_name(call)) != 0)
2559 if (!!system != !!event_name)
2562 if (!is_var_ref(var_name))
2567 return field_name_from_var(hist_data, var_name);
2570 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2571 char *system, char *event_name,
2574 struct hist_field *var_field = NULL, *ref_field = NULL;
2576 if (!is_var_ref(var_name))
2581 var_field = find_event_var(hist_data, system, event_name, var_name);
2583 ref_field = create_var_ref(hist_data, var_field,
2584 system, event_name);
2587 hist_err_event("Couldn't find variable: $",
2588 system, event_name, var_name);
2593 static struct ftrace_event_field *
2594 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2595 char *field_str, unsigned long *flags)
2597 struct ftrace_event_field *field = NULL;
2598 char *field_name, *modifier, *str;
2600 modifier = str = kstrdup(field_str, GFP_KERNEL);
2602 return ERR_PTR(-ENOMEM);
2604 field_name = strsep(&modifier, ".");
2606 if (strcmp(modifier, "hex") == 0)
2607 *flags |= HIST_FIELD_FL_HEX;
2608 else if (strcmp(modifier, "sym") == 0)
2609 *flags |= HIST_FIELD_FL_SYM;
2610 else if (strcmp(modifier, "sym-offset") == 0)
2611 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2612 else if ((strcmp(modifier, "execname") == 0) &&
2613 (strcmp(field_name, "common_pid") == 0))
2614 *flags |= HIST_FIELD_FL_EXECNAME;
2615 else if (strcmp(modifier, "syscall") == 0)
2616 *flags |= HIST_FIELD_FL_SYSCALL;
2617 else if (strcmp(modifier, "log2") == 0)
2618 *flags |= HIST_FIELD_FL_LOG2;
2619 else if (strcmp(modifier, "usecs") == 0)
2620 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2622 hist_err("Invalid field modifier: ", modifier);
2623 field = ERR_PTR(-EINVAL);
2628 if (strcmp(field_name, "common_timestamp") == 0) {
2629 *flags |= HIST_FIELD_FL_TIMESTAMP;
2630 hist_data->enable_timestamps = true;
2631 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2632 hist_data->attrs->ts_in_usecs = true;
2633 } else if (strcmp(field_name, "common_cpu") == 0)
2634 *flags |= HIST_FIELD_FL_CPU;
2636 field = trace_find_event_field(file->event_call, field_name);
2637 if (!field || !field->size) {
2639 * For backward compatibility, if field_name
2640 * was "cpu", then we treat this the same as
2641 * common_cpu. This also works for "CPU".
2643 if (field && field->filter_type == FILTER_CPU) {
2644 *flags |= HIST_FIELD_FL_CPU;
2646 hist_err("Couldn't find field: ", field_name);
2647 field = ERR_PTR(-EINVAL);
2658 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2659 struct hist_field *var_ref,
2662 struct hist_field *alias = NULL;
2663 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2665 alias = create_hist_field(hist_data, NULL, flags, var_name);
2669 alias->fn = var_ref->fn;
2670 alias->operands[0] = var_ref;
2672 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2673 destroy_hist_field(alias, 0);
2677 alias->var_ref_idx = var_ref->var_ref_idx;
2682 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2683 struct trace_event_file *file, char *str,
2684 unsigned long *flags, char *var_name)
2686 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2687 struct ftrace_event_field *field = NULL;
2688 struct hist_field *hist_field = NULL;
2691 s = strchr(str, '.');
2693 s = strchr(++s, '.');
2695 ref_system = strsep(&str, ".");
2700 ref_event = strsep(&str, ".");
2709 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2711 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2714 hist_field = create_alias(hist_data, hist_field, var_name);
2725 field = parse_field(hist_data, file, str, flags);
2726 if (IS_ERR(field)) {
2727 ret = PTR_ERR(field);
2731 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2739 return ERR_PTR(ret);
2742 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2743 struct trace_event_file *file,
2744 char *str, unsigned long flags,
2745 char *var_name, unsigned int level);
2747 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2748 struct trace_event_file *file,
2749 char *str, unsigned long flags,
2750 char *var_name, unsigned int level)
2752 struct hist_field *operand1, *expr = NULL;
2753 unsigned long operand_flags;
2757 /* we support only -(xxx) i.e. explicit parens required */
2760 hist_err("Too many subexpressions (3 max): ", str);
2765 str++; /* skip leading '-' */
2767 s = strchr(str, '(');
2775 s = strrchr(str, ')');
2779 ret = -EINVAL; /* no closing ')' */
2783 flags |= HIST_FIELD_FL_EXPR;
2784 expr = create_hist_field(hist_data, NULL, flags, var_name);
2791 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2792 if (IS_ERR(operand1)) {
2793 ret = PTR_ERR(operand1);
2796 if (operand1->flags & HIST_FIELD_FL_STRING) {
2797 /* String type can not be the operand of unary operator. */
2798 destroy_hist_field(operand1, 0);
2803 expr->flags |= operand1->flags &
2804 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2805 expr->fn = hist_field_unary_minus;
2806 expr->operands[0] = operand1;
2807 expr->operator = FIELD_OP_UNARY_MINUS;
2808 expr->name = expr_str(expr, 0);
2809 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2817 destroy_hist_field(expr, 0);
2818 return ERR_PTR(ret);
2821 static int check_expr_operands(struct hist_field *operand1,
2822 struct hist_field *operand2)
2824 unsigned long operand1_flags = operand1->flags;
2825 unsigned long operand2_flags = operand2->flags;
2827 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2828 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2829 struct hist_field *var;
2831 var = find_var_field(operand1->var.hist_data, operand1->name);
2834 operand1_flags = var->flags;
2837 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2838 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2839 struct hist_field *var;
2841 var = find_var_field(operand2->var.hist_data, operand2->name);
2844 operand2_flags = var->flags;
2847 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2848 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2849 hist_err("Timestamp units in expression don't match", NULL);
2856 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2857 struct trace_event_file *file,
2858 char *str, unsigned long flags,
2859 char *var_name, unsigned int level)
2861 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2862 unsigned long operand_flags;
2863 int field_op, ret = -EINVAL;
2864 char *sep, *operand1_str;
2867 hist_err("Too many subexpressions (3 max): ", str);
2868 return ERR_PTR(-EINVAL);
2871 field_op = contains_operator(str);
2873 if (field_op == FIELD_OP_NONE)
2874 return parse_atom(hist_data, file, str, &flags, var_name);
2876 if (field_op == FIELD_OP_UNARY_MINUS)
2877 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2880 case FIELD_OP_MINUS:
2890 operand1_str = strsep(&str, sep);
2891 if (!operand1_str || !str)
2895 operand1 = parse_atom(hist_data, file, operand1_str,
2896 &operand_flags, NULL);
2897 if (IS_ERR(operand1)) {
2898 ret = PTR_ERR(operand1);
2902 if (operand1->flags & HIST_FIELD_FL_STRING) {
2907 /* rest of string could be another expression e.g. b+c in a+b+c */
2909 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2910 if (IS_ERR(operand2)) {
2911 ret = PTR_ERR(operand2);
2915 if (operand2->flags & HIST_FIELD_FL_STRING) {
2920 ret = check_expr_operands(operand1, operand2);
2924 flags |= HIST_FIELD_FL_EXPR;
2926 flags |= operand1->flags &
2927 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2929 expr = create_hist_field(hist_data, NULL, flags, var_name);
2935 operand1->read_once = true;
2936 operand2->read_once = true;
2938 expr->operands[0] = operand1;
2939 expr->operands[1] = operand2;
2941 /* The operand sizes should be the same, so just pick one */
2942 expr->size = operand1->size;
2944 expr->operator = field_op;
2945 expr->name = expr_str(expr, 0);
2946 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2953 case FIELD_OP_MINUS:
2954 expr->fn = hist_field_minus;
2957 expr->fn = hist_field_plus;
2966 destroy_hist_field(operand1, 0);
2967 destroy_hist_field(operand2, 0);
2968 destroy_hist_field(expr, 0);
2970 return ERR_PTR(ret);
2973 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2974 struct trace_event_file *file)
2976 struct event_trigger_data *test;
2978 lockdep_assert_held(&event_mutex);
2980 list_for_each_entry(test, &file->triggers, list) {
2981 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2982 if (test->private_data == hist_data)
2983 return test->filter_str;
2990 static struct event_command trigger_hist_cmd;
2991 static int event_hist_trigger_func(struct event_command *cmd_ops,
2992 struct trace_event_file *file,
2993 char *glob, char *cmd, char *param);
2995 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2996 struct hist_trigger_data *hist_data,
2997 unsigned int n_keys)
2999 struct hist_field *target_hist_field, *hist_field;
3000 unsigned int n, i, j;
3002 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3005 i = hist_data->n_vals;
3006 j = target_hist_data->n_vals;
3008 for (n = 0; n < n_keys; n++) {
3009 hist_field = hist_data->fields[i + n];
3010 target_hist_field = target_hist_data->fields[j + n];
3012 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3014 if (hist_field->size != target_hist_field->size)
3016 if (hist_field->is_signed != target_hist_field->is_signed)
3023 static struct hist_trigger_data *
3024 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3025 struct trace_event_file *file)
3027 struct hist_trigger_data *hist_data;
3028 struct event_trigger_data *test;
3029 unsigned int n_keys;
3031 lockdep_assert_held(&event_mutex);
3033 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3035 list_for_each_entry(test, &file->triggers, list) {
3036 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3037 hist_data = test->private_data;
3039 if (compatible_keys(target_hist_data, hist_data, n_keys))
3047 static struct trace_event_file *event_file(struct trace_array *tr,
3048 char *system, char *event_name)
3050 struct trace_event_file *file;
3052 file = __find_event_file(tr, system, event_name);
3054 return ERR_PTR(-EINVAL);
3059 static struct hist_field *
3060 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3061 char *system, char *event_name, char *field_name)
3063 struct hist_field *event_var;
3064 char *synthetic_name;
3066 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3067 if (!synthetic_name)
3068 return ERR_PTR(-ENOMEM);
3070 strcpy(synthetic_name, "synthetic_");
3071 strcat(synthetic_name, field_name);
3073 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3075 kfree(synthetic_name);
3081 * create_field_var_hist - Automatically create a histogram and var for a field
3082 * @target_hist_data: The target hist trigger
3083 * @subsys_name: Optional subsystem name
3084 * @event_name: Optional event name
3085 * @field_name: The name of the field (and the resulting variable)
3087 * Hist trigger actions fetch data from variables, not directly from
3088 * events. However, for convenience, users are allowed to directly
3089 * specify an event field in an action, which will be automatically
3090 * converted into a variable on their behalf.
3092 * If a user specifies a field on an event that isn't the event the
3093 * histogram currently being defined (the target event histogram), the
3094 * only way that can be accomplished is if a new hist trigger is
3095 * created and the field variable defined on that.
3097 * This function creates a new histogram compatible with the target
3098 * event (meaning a histogram with the same key as the target
3099 * histogram), and creates a variable for the specified field, but
3100 * with 'synthetic_' prepended to the variable name in order to avoid
3101 * collision with normal field variables.
3103 * Return: The variable created for the field.
3105 static struct hist_field *
3106 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3107 char *subsys_name, char *event_name, char *field_name)
3109 struct trace_array *tr = target_hist_data->event_file->tr;
3110 struct hist_field *event_var = ERR_PTR(-EINVAL);
3111 struct hist_trigger_data *hist_data;
3112 unsigned int i, n, first = true;
3113 struct field_var_hist *var_hist;
3114 struct trace_event_file *file;
3115 struct hist_field *key_field;
3120 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3121 hist_err_event("onmatch: Too many field variables defined: ",
3122 subsys_name, event_name, field_name);
3123 return ERR_PTR(-EINVAL);
3126 file = event_file(tr, subsys_name, event_name);
3129 hist_err_event("onmatch: Event file not found: ",
3130 subsys_name, event_name, field_name);
3131 ret = PTR_ERR(file);
3132 return ERR_PTR(ret);
3136 * Look for a histogram compatible with target. We'll use the
3137 * found histogram specification to create a new matching
3138 * histogram with our variable on it. target_hist_data is not
3139 * yet a registered histogram so we can't use that.
3141 hist_data = find_compatible_hist(target_hist_data, file);
3143 hist_err_event("onmatch: Matching event histogram not found: ",
3144 subsys_name, event_name, field_name);
3145 return ERR_PTR(-EINVAL);
3148 /* See if a synthetic field variable has already been created */
3149 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3150 event_name, field_name);
3151 if (!IS_ERR_OR_NULL(event_var))
3154 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3156 return ERR_PTR(-ENOMEM);
3158 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3161 return ERR_PTR(-ENOMEM);
3164 /* Use the same keys as the compatible histogram */
3165 strcat(cmd, "keys=");
3167 for_each_hist_key_field(i, hist_data) {
3168 key_field = hist_data->fields[i];
3171 strcat(cmd, key_field->field->name);
3175 /* Create the synthetic field variable specification */
3176 strcat(cmd, ":synthetic_");
3177 strcat(cmd, field_name);
3179 strcat(cmd, field_name);
3181 /* Use the same filter as the compatible histogram */
3182 saved_filter = find_trigger_filter(hist_data, file);
3184 strcat(cmd, " if ");
3185 strcat(cmd, saved_filter);
3188 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3189 if (!var_hist->cmd) {
3192 return ERR_PTR(-ENOMEM);
3195 /* Save the compatible histogram information */
3196 var_hist->hist_data = hist_data;
3198 /* Create the new histogram with our variable */
3199 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3203 kfree(var_hist->cmd);
3205 hist_err_event("onmatch: Couldn't create histogram for field: ",
3206 subsys_name, event_name, field_name);
3207 return ERR_PTR(ret);
3212 /* If we can't find the variable, something went wrong */
3213 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3214 event_name, field_name);
3215 if (IS_ERR_OR_NULL(event_var)) {
3216 kfree(var_hist->cmd);
3218 hist_err_event("onmatch: Couldn't find synthetic variable: ",
3219 subsys_name, event_name, field_name);
3220 return ERR_PTR(-EINVAL);
3223 n = target_hist_data->n_field_var_hists;
3224 target_hist_data->field_var_hists[n] = var_hist;
3225 target_hist_data->n_field_var_hists++;
3230 static struct hist_field *
3231 find_target_event_var(struct hist_trigger_data *hist_data,
3232 char *subsys_name, char *event_name, char *var_name)
3234 struct trace_event_file *file = hist_data->event_file;
3235 struct hist_field *hist_field = NULL;
3238 struct trace_event_call *call;
3243 call = file->event_call;
3245 if (strcmp(subsys_name, call->class->system) != 0)
3248 if (strcmp(event_name, trace_event_name(call)) != 0)
3252 hist_field = find_var_field(hist_data, var_name);
3257 static inline void __update_field_vars(struct tracing_map_elt *elt,
3258 struct ring_buffer_event *rbe,
3260 struct field_var **field_vars,
3261 unsigned int n_field_vars,
3262 unsigned int field_var_str_start)
3264 struct hist_elt_data *elt_data = elt->private_data;
3265 unsigned int i, j, var_idx;
3268 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3269 struct field_var *field_var = field_vars[i];
3270 struct hist_field *var = field_var->var;
3271 struct hist_field *val = field_var->val;
3273 var_val = val->fn(val, elt, rbe, rec);
3274 var_idx = var->var.idx;
3276 if (val->flags & HIST_FIELD_FL_STRING) {
3277 char *str = elt_data->field_var_str[j++];
3278 char *val_str = (char *)(uintptr_t)var_val;
3280 strscpy(str, val_str, STR_VAR_LEN_MAX);
3281 var_val = (u64)(uintptr_t)str;
3283 tracing_map_set_var(elt, var_idx, var_val);
3287 static void update_field_vars(struct hist_trigger_data *hist_data,
3288 struct tracing_map_elt *elt,
3289 struct ring_buffer_event *rbe,
3292 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3293 hist_data->n_field_vars, 0);
3296 static void update_max_vars(struct hist_trigger_data *hist_data,
3297 struct tracing_map_elt *elt,
3298 struct ring_buffer_event *rbe,
3301 __update_field_vars(elt, rbe, rec, hist_data->max_vars,
3302 hist_data->n_max_vars, hist_data->n_field_var_str);
3305 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3306 struct trace_event_file *file,
3307 char *name, int size, const char *type)
3309 struct hist_field *var;
3312 if (find_var(hist_data, file, name) && !hist_data->remove) {
3313 var = ERR_PTR(-EINVAL);
3317 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3319 var = ERR_PTR(-ENOMEM);
3323 idx = tracing_map_add_var(hist_data->map);
3326 var = ERR_PTR(-EINVAL);
3330 var->flags = HIST_FIELD_FL_VAR;
3332 var->var.hist_data = var->hist_data = hist_data;
3334 var->var.name = kstrdup(name, GFP_KERNEL);
3335 var->type = kstrdup(type, GFP_KERNEL);
3336 if (!var->var.name || !var->type) {
3337 kfree(var->var.name);
3340 var = ERR_PTR(-ENOMEM);
3346 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3347 struct trace_event_file *file,
3350 struct hist_field *val = NULL, *var = NULL;
3351 unsigned long flags = HIST_FIELD_FL_VAR;
3352 struct field_var *field_var;
3355 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3356 hist_err("Too many field variables defined: ", field_name);
3361 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3363 hist_err("Couldn't parse field variable: ", field_name);
3368 var = create_var(hist_data, file, field_name, val->size, val->type);
3370 hist_err("Couldn't create or find variable: ", field_name);
3376 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3384 field_var->var = var;
3385 field_var->val = val;
3389 field_var = ERR_PTR(ret);
3394 * create_target_field_var - Automatically create a variable for a field
3395 * @target_hist_data: The target hist trigger
3396 * @subsys_name: Optional subsystem name
3397 * @event_name: Optional event name
3398 * @var_name: The name of the field (and the resulting variable)
3400 * Hist trigger actions fetch data from variables, not directly from
3401 * events. However, for convenience, users are allowed to directly
3402 * specify an event field in an action, which will be automatically
3403 * converted into a variable on their behalf.
3405 * This function creates a field variable with the name var_name on
3406 * the hist trigger currently being defined on the target event. If
3407 * subsys_name and event_name are specified, this function simply
3408 * verifies that they do in fact match the target event subsystem and
3411 * Return: The variable created for the field.
3413 static struct field_var *
3414 create_target_field_var(struct hist_trigger_data *target_hist_data,
3415 char *subsys_name, char *event_name, char *var_name)
3417 struct trace_event_file *file = target_hist_data->event_file;
3420 struct trace_event_call *call;
3425 call = file->event_call;
3427 if (strcmp(subsys_name, call->class->system) != 0)
3430 if (strcmp(event_name, trace_event_name(call)) != 0)
3434 return create_field_var(target_hist_data, file, var_name);
3437 static void onmax_print(struct seq_file *m,
3438 struct hist_trigger_data *hist_data,
3439 struct tracing_map_elt *elt,
3440 struct action_data *data)
3442 unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
3444 seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
3446 for (i = 0; i < hist_data->n_max_vars; i++) {
3447 struct hist_field *save_val = hist_data->max_vars[i]->val;
3448 struct hist_field *save_var = hist_data->max_vars[i]->var;
3451 save_var_idx = save_var->var.idx;
3453 val = tracing_map_read_var(elt, save_var_idx);
3455 if (save_val->flags & HIST_FIELD_FL_STRING) {
3456 seq_printf(m, " %s: %-32s", save_var->var.name,
3457 (char *)(uintptr_t)(val));
3459 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3463 static void onmax_save(struct hist_trigger_data *hist_data,
3464 struct tracing_map_elt *elt, void *rec,
3465 struct ring_buffer_event *rbe,
3466 struct action_data *data, u64 *var_ref_vals)
3468 unsigned int max_idx = data->onmax.max_var->var.idx;
3469 unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
3471 u64 var_val, max_val;
3473 var_val = var_ref_vals[max_var_ref_idx];
3474 max_val = tracing_map_read_var(elt, max_idx);
3476 if (var_val <= max_val)
3479 tracing_map_set_var(elt, max_idx, var_val);
3481 update_max_vars(hist_data, elt, rbe, rec);
3484 static void onmax_destroy(struct action_data *data)
3488 destroy_hist_field(data->onmax.max_var, 0);
3489 destroy_hist_field(data->onmax.var, 0);
3491 kfree(data->onmax.var_str);
3492 kfree(data->onmax.fn_name);
3494 for (i = 0; i < data->n_params; i++)
3495 kfree(data->params[i]);
3500 static int onmax_create(struct hist_trigger_data *hist_data,
3501 struct action_data *data)
3503 struct trace_event_file *file = hist_data->event_file;
3504 struct hist_field *var_field, *ref_field, *max_var;
3505 unsigned int var_ref_idx = hist_data->n_var_refs;
3506 struct field_var *field_var;
3507 char *onmax_var_str, *param;
3511 onmax_var_str = data->onmax.var_str;
3512 if (onmax_var_str[0] != '$') {
3513 hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
3518 var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
3520 hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
3524 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3528 data->onmax.var = ref_field;
3530 data->fn = onmax_save;
3531 data->onmax.max_var_ref_idx = var_ref_idx;
3532 max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
3533 if (IS_ERR(max_var)) {
3534 hist_err("onmax: Couldn't create onmax variable: ", "max");
3535 ret = PTR_ERR(max_var);
3538 data->onmax.max_var = max_var;
3540 for (i = 0; i < data->n_params; i++) {
3541 param = kstrdup(data->params[i], GFP_KERNEL);
3547 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3548 if (IS_ERR(field_var)) {
3549 hist_err("onmax: Couldn't create field variable: ", param);
3550 ret = PTR_ERR(field_var);
3555 hist_data->max_vars[hist_data->n_max_vars++] = field_var;
3556 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3557 hist_data->n_max_var_str++;
3565 static int parse_action_params(char *params, struct action_data *data)
3567 char *param, *saved_param;
3571 if (data->n_params >= SYNTH_FIELDS_MAX)
3574 param = strsep(¶ms, ",");
3580 param = strstrip(param);
3581 if (strlen(param) < 2) {
3582 hist_err("Invalid action param: ", param);
3587 saved_param = kstrdup(param, GFP_KERNEL);
3593 data->params[data->n_params++] = saved_param;
3599 static struct action_data *onmax_parse(char *str)
3601 char *onmax_fn_name, *onmax_var_str;
3602 struct action_data *data;
3605 data = kzalloc(sizeof(*data), GFP_KERNEL);
3607 return ERR_PTR(-ENOMEM);
3609 onmax_var_str = strsep(&str, ")");
3610 if (!onmax_var_str || !str) {
3615 data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
3616 if (!data->onmax.var_str) {
3625 onmax_fn_name = strsep(&str, "(");
3626 if (!onmax_fn_name || !str)
3629 if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) {
3630 char *params = strsep(&str, ")");
3637 ret = parse_action_params(params, data);
3643 data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
3644 if (!data->onmax.fn_name) {
3651 onmax_destroy(data);
3652 data = ERR_PTR(ret);
3656 static void onmatch_destroy(struct action_data *data)
3660 mutex_lock(&synth_event_mutex);
3662 kfree(data->onmatch.match_event);
3663 kfree(data->onmatch.match_event_system);
3664 kfree(data->onmatch.synth_event_name);
3666 for (i = 0; i < data->n_params; i++)
3667 kfree(data->params[i]);
3669 if (data->onmatch.synth_event)
3670 data->onmatch.synth_event->ref--;
3674 mutex_unlock(&synth_event_mutex);
3677 static void destroy_field_var(struct field_var *field_var)
3682 destroy_hist_field(field_var->var, 0);
3683 destroy_hist_field(field_var->val, 0);
3688 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3692 for (i = 0; i < hist_data->n_field_vars; i++)
3693 destroy_field_var(hist_data->field_vars[i]);
3696 static void save_field_var(struct hist_trigger_data *hist_data,
3697 struct field_var *field_var)
3699 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3701 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3702 hist_data->n_field_var_str++;
3706 static void destroy_synth_var_refs(struct hist_trigger_data *hist_data)
3710 for (i = 0; i < hist_data->n_synth_var_refs; i++)
3711 destroy_hist_field(hist_data->synth_var_refs[i], 0);
3714 static void save_synth_var_ref(struct hist_trigger_data *hist_data,
3715 struct hist_field *var_ref)
3717 hist_data->synth_var_refs[hist_data->n_synth_var_refs++] = var_ref;
3720 static int check_synth_field(struct synth_event *event,
3721 struct hist_field *hist_field,
3722 unsigned int field_pos)
3724 struct synth_field *field;
3726 if (field_pos >= event->n_fields)
3729 field = event->fields[field_pos];
3731 if (strcmp(field->type, hist_field->type) != 0)
3737 static struct hist_field *
3738 onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
3739 char *system, char *event, char *var)
3741 struct hist_field *hist_field;
3743 var++; /* skip '$' */
3745 hist_field = find_target_event_var(hist_data, system, event, var);
3748 system = data->onmatch.match_event_system;
3749 event = data->onmatch.match_event;
3752 hist_field = find_event_var(hist_data, system, event, var);
3756 hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
3761 static struct hist_field *
3762 onmatch_create_field_var(struct hist_trigger_data *hist_data,
3763 struct action_data *data, char *system,
3764 char *event, char *var)
3766 struct hist_field *hist_field = NULL;
3767 struct field_var *field_var;
3770 * First try to create a field var on the target event (the
3771 * currently being defined). This will create a variable for
3772 * unqualified fields on the target event, or if qualified,
3773 * target fields that have qualified names matching the target.
3775 field_var = create_target_field_var(hist_data, system, event, var);
3777 if (field_var && !IS_ERR(field_var)) {
3778 save_field_var(hist_data, field_var);
3779 hist_field = field_var->var;
3783 * If no explicit system.event is specfied, default to
3784 * looking for fields on the onmatch(system.event.xxx)
3788 system = data->onmatch.match_event_system;
3789 event = data->onmatch.match_event;
3795 * At this point, we're looking at a field on another
3796 * event. Because we can't modify a hist trigger on
3797 * another event to add a variable for a field, we need
3798 * to create a new trigger on that event and create the
3799 * variable at the same time.
3801 hist_field = create_field_var_hist(hist_data, system, event, var);
3802 if (IS_ERR(hist_field))
3808 destroy_field_var(field_var);
3813 static int onmatch_create(struct hist_trigger_data *hist_data,
3814 struct trace_event_file *file,
3815 struct action_data *data)
3817 char *event_name, *param, *system = NULL;
3818 struct hist_field *hist_field, *var_ref;
3819 unsigned int i, var_ref_idx;
3820 unsigned int field_pos = 0;
3821 struct synth_event *event;
3824 mutex_lock(&synth_event_mutex);
3825 event = find_synth_event(data->onmatch.synth_event_name);
3827 hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
3828 mutex_unlock(&synth_event_mutex);
3832 mutex_unlock(&synth_event_mutex);
3834 var_ref_idx = hist_data->n_var_refs;
3836 for (i = 0; i < data->n_params; i++) {
3839 p = param = kstrdup(data->params[i], GFP_KERNEL);
3845 system = strsep(¶m, ".");
3847 param = (char *)system;
3848 system = event_name = NULL;
3850 event_name = strsep(¶m, ".");
3858 if (param[0] == '$')
3859 hist_field = onmatch_find_var(hist_data, data, system,
3862 hist_field = onmatch_create_field_var(hist_data, data,
3873 if (check_synth_field(event, hist_field, field_pos) == 0) {
3874 var_ref = create_var_ref(hist_data, hist_field,
3875 system, event_name);
3882 save_synth_var_ref(hist_data, var_ref);
3888 hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
3889 system, event_name, param);
3895 if (field_pos != event->n_fields) {
3896 hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
3901 data->fn = action_trace;
3902 data->onmatch.synth_event = event;
3903 data->onmatch.var_ref_idx = var_ref_idx;
3907 mutex_lock(&synth_event_mutex);
3909 mutex_unlock(&synth_event_mutex);
3914 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3916 char *match_event, *match_event_system;
3917 char *synth_event_name, *params;
3918 struct action_data *data;
3921 data = kzalloc(sizeof(*data), GFP_KERNEL);
3923 return ERR_PTR(-ENOMEM);
3925 match_event = strsep(&str, ")");
3926 if (!match_event || !str) {
3927 hist_err("onmatch: Missing closing paren: ", match_event);
3931 match_event_system = strsep(&match_event, ".");
3933 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
3937 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3938 hist_err_event("onmatch: Invalid subsystem or event name: ",
3939 match_event_system, match_event, NULL);
3943 data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
3944 if (!data->onmatch.match_event) {
3949 data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
3950 if (!data->onmatch.match_event_system) {
3957 hist_err("onmatch: Missing . after onmatch(): ", str);
3961 synth_event_name = strsep(&str, "(");
3962 if (!synth_event_name || !str) {
3963 hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
3967 data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
3968 if (!data->onmatch.synth_event_name) {
3973 params = strsep(&str, ")");
3974 if (!params || !str || (str && strlen(str))) {
3975 hist_err("onmatch: Missing closing paramlist paren: ", params);
3979 ret = parse_action_params(params, data);
3985 onmatch_destroy(data);
3986 data = ERR_PTR(ret);
3990 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3992 hist_data->fields[HITCOUNT_IDX] =
3993 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3994 if (!hist_data->fields[HITCOUNT_IDX])
3997 hist_data->n_vals++;
3998 hist_data->n_fields++;
4000 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4006 static int __create_val_field(struct hist_trigger_data *hist_data,
4007 unsigned int val_idx,
4008 struct trace_event_file *file,
4009 char *var_name, char *field_str,
4010 unsigned long flags)
4012 struct hist_field *hist_field;
4015 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4016 if (IS_ERR(hist_field)) {
4017 ret = PTR_ERR(hist_field);
4021 hist_data->fields[val_idx] = hist_field;
4023 ++hist_data->n_vals;
4024 ++hist_data->n_fields;
4026 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4032 static int create_val_field(struct hist_trigger_data *hist_data,
4033 unsigned int val_idx,
4034 struct trace_event_file *file,
4037 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4040 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4043 static int create_var_field(struct hist_trigger_data *hist_data,
4044 unsigned int val_idx,
4045 struct trace_event_file *file,
4046 char *var_name, char *expr_str)
4048 unsigned long flags = 0;
4050 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4053 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4054 hist_err("Variable already defined: ", var_name);
4058 flags |= HIST_FIELD_FL_VAR;
4059 hist_data->n_vars++;
4060 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4063 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4066 static int create_val_fields(struct hist_trigger_data *hist_data,
4067 struct trace_event_file *file)
4069 char *fields_str, *field_str;
4070 unsigned int i, j = 1;
4073 ret = create_hitcount_val(hist_data);
4077 fields_str = hist_data->attrs->vals_str;
4081 strsep(&fields_str, "=");
4085 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4086 j < TRACING_MAP_VALS_MAX; i++) {
4087 field_str = strsep(&fields_str, ",");
4091 if (strcmp(field_str, "hitcount") == 0)
4094 ret = create_val_field(hist_data, j++, file, field_str);
4099 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4105 static int create_key_field(struct hist_trigger_data *hist_data,
4106 unsigned int key_idx,
4107 unsigned int key_offset,
4108 struct trace_event_file *file,
4111 struct hist_field *hist_field = NULL;
4113 unsigned long flags = 0;
4114 unsigned int key_size;
4117 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4120 flags |= HIST_FIELD_FL_KEY;
4122 if (strcmp(field_str, "stacktrace") == 0) {
4123 flags |= HIST_FIELD_FL_STACKTRACE;
4124 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4125 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4127 hist_field = parse_expr(hist_data, file, field_str, flags,
4129 if (IS_ERR(hist_field)) {
4130 ret = PTR_ERR(hist_field);
4134 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
4135 hist_err("Using variable references as keys not supported: ", field_str);
4136 destroy_hist_field(hist_field, 0);
4141 key_size = hist_field->size;
4144 hist_data->fields[key_idx] = hist_field;
4146 key_size = ALIGN(key_size, sizeof(u64));
4147 hist_data->fields[key_idx]->size = key_size;
4148 hist_data->fields[key_idx]->offset = key_offset;
4150 hist_data->key_size += key_size;
4152 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4157 hist_data->n_keys++;
4158 hist_data->n_fields++;
4160 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4168 static int create_key_fields(struct hist_trigger_data *hist_data,
4169 struct trace_event_file *file)
4171 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4172 char *fields_str, *field_str;
4175 fields_str = hist_data->attrs->keys_str;
4179 strsep(&fields_str, "=");
4183 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4184 field_str = strsep(&fields_str, ",");
4187 ret = create_key_field(hist_data, i, key_offset,
4202 static int create_var_fields(struct hist_trigger_data *hist_data,
4203 struct trace_event_file *file)
4205 unsigned int i, j = hist_data->n_vals;
4208 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4210 for (i = 0; i < n_vars; i++) {
4211 char *var_name = hist_data->attrs->var_defs.name[i];
4212 char *expr = hist_data->attrs->var_defs.expr[i];
4214 ret = create_var_field(hist_data, j++, file, var_name, expr);
4222 static void free_var_defs(struct hist_trigger_data *hist_data)
4226 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4227 kfree(hist_data->attrs->var_defs.name[i]);
4228 kfree(hist_data->attrs->var_defs.expr[i]);
4231 hist_data->attrs->var_defs.n_vars = 0;
4234 static int parse_var_defs(struct hist_trigger_data *hist_data)
4236 char *s, *str, *var_name, *field_str;
4237 unsigned int i, j, n_vars = 0;
4240 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4241 str = hist_data->attrs->assignment_str[i];
4242 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4243 field_str = strsep(&str, ",");
4247 var_name = strsep(&field_str, "=");
4248 if (!var_name || !field_str) {
4249 hist_err("Malformed assignment: ", var_name);
4254 if (n_vars == TRACING_MAP_VARS_MAX) {
4255 hist_err("Too many variables defined: ", var_name);
4260 s = kstrdup(var_name, GFP_KERNEL);
4265 hist_data->attrs->var_defs.name[n_vars] = s;
4267 s = kstrdup(field_str, GFP_KERNEL);
4269 kfree(hist_data->attrs->var_defs.name[n_vars]);
4270 hist_data->attrs->var_defs.name[n_vars] = NULL;
4274 hist_data->attrs->var_defs.expr[n_vars++] = s;
4276 hist_data->attrs->var_defs.n_vars = n_vars;
4282 free_var_defs(hist_data);
4287 static int create_hist_fields(struct hist_trigger_data *hist_data,
4288 struct trace_event_file *file)
4292 ret = parse_var_defs(hist_data);
4296 ret = create_val_fields(hist_data, file);
4300 ret = create_var_fields(hist_data, file);
4304 ret = create_key_fields(hist_data, file);
4308 free_var_defs(hist_data);
4313 static int is_descending(const char *str)
4318 if (strcmp(str, "descending") == 0)
4321 if (strcmp(str, "ascending") == 0)
4327 static int create_sort_keys(struct hist_trigger_data *hist_data)
4329 char *fields_str = hist_data->attrs->sort_key_str;
4330 struct tracing_map_sort_key *sort_key;
4331 int descending, ret = 0;
4332 unsigned int i, j, k;
4334 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4339 strsep(&fields_str, "=");
4345 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4346 struct hist_field *hist_field;
4347 char *field_str, *field_name;
4348 const char *test_name;
4350 sort_key = &hist_data->sort_keys[i];
4352 field_str = strsep(&fields_str, ",");
4359 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4364 field_name = strsep(&field_str, ".");
4370 if (strcmp(field_name, "hitcount") == 0) {
4371 descending = is_descending(field_str);
4372 if (descending < 0) {
4376 sort_key->descending = descending;
4380 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4383 hist_field = hist_data->fields[j];
4384 if (hist_field->flags & HIST_FIELD_FL_VAR)
4389 test_name = hist_field_name(hist_field, 0);
4391 if (strcmp(field_name, test_name) == 0) {
4392 sort_key->field_idx = idx;
4393 descending = is_descending(field_str);
4394 if (descending < 0) {
4398 sort_key->descending = descending;
4402 if (j == hist_data->n_fields) {
4408 hist_data->n_sort_keys = i;
4413 static void destroy_actions(struct hist_trigger_data *hist_data)
4417 for (i = 0; i < hist_data->n_actions; i++) {
4418 struct action_data *data = hist_data->actions[i];
4420 if (data->fn == action_trace)
4421 onmatch_destroy(data);
4422 else if (data->fn == onmax_save)
4423 onmax_destroy(data);
4429 static int parse_actions(struct hist_trigger_data *hist_data)
4431 struct trace_array *tr = hist_data->event_file->tr;
4432 struct action_data *data;
4437 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4438 str = hist_data->attrs->action_str[i];
4440 if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) {
4441 char *action_str = str + strlen("onmatch(");
4443 data = onmatch_parse(tr, action_str);
4445 ret = PTR_ERR(data);
4448 data->fn = action_trace;
4449 } else if (strncmp(str, "onmax(", strlen("onmax(")) == 0) {
4450 char *action_str = str + strlen("onmax(");
4452 data = onmax_parse(action_str);
4454 ret = PTR_ERR(data);
4457 data->fn = onmax_save;
4463 hist_data->actions[hist_data->n_actions++] = data;
4469 static int create_actions(struct hist_trigger_data *hist_data,
4470 struct trace_event_file *file)
4472 struct action_data *data;
4476 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4477 data = hist_data->actions[i];
4479 if (data->fn == action_trace) {
4480 ret = onmatch_create(hist_data, file, data);
4483 } else if (data->fn == onmax_save) {
4484 ret = onmax_create(hist_data, data);
4493 static void print_actions(struct seq_file *m,
4494 struct hist_trigger_data *hist_data,
4495 struct tracing_map_elt *elt)
4499 for (i = 0; i < hist_data->n_actions; i++) {
4500 struct action_data *data = hist_data->actions[i];
4502 if (data->fn == onmax_save)
4503 onmax_print(m, hist_data, elt, data);
4507 static void print_onmax_spec(struct seq_file *m,
4508 struct hist_trigger_data *hist_data,
4509 struct action_data *data)
4513 seq_puts(m, ":onmax(");
4514 seq_printf(m, "%s", data->onmax.var_str);
4515 seq_printf(m, ").%s(", data->onmax.fn_name);
4517 for (i = 0; i < hist_data->n_max_vars; i++) {
4518 seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
4519 if (i < hist_data->n_max_vars - 1)
4525 static void print_onmatch_spec(struct seq_file *m,
4526 struct hist_trigger_data *hist_data,
4527 struct action_data *data)
4531 seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
4532 data->onmatch.match_event);
4534 seq_printf(m, "%s(", data->onmatch.synth_event->name);
4536 for (i = 0; i < data->n_params; i++) {
4539 seq_printf(m, "%s", data->params[i]);
4545 static bool actions_match(struct hist_trigger_data *hist_data,
4546 struct hist_trigger_data *hist_data_test)
4550 if (hist_data->n_actions != hist_data_test->n_actions)
4553 for (i = 0; i < hist_data->n_actions; i++) {
4554 struct action_data *data = hist_data->actions[i];
4555 struct action_data *data_test = hist_data_test->actions[i];
4557 if (data->fn != data_test->fn)
4560 if (data->n_params != data_test->n_params)
4563 for (j = 0; j < data->n_params; j++) {
4564 if (strcmp(data->params[j], data_test->params[j]) != 0)
4568 if (data->fn == action_trace) {
4569 if (strcmp(data->onmatch.synth_event_name,
4570 data_test->onmatch.synth_event_name) != 0)
4572 if (strcmp(data->onmatch.match_event_system,
4573 data_test->onmatch.match_event_system) != 0)
4575 if (strcmp(data->onmatch.match_event,
4576 data_test->onmatch.match_event) != 0)
4578 } else if (data->fn == onmax_save) {
4579 if (strcmp(data->onmax.var_str,
4580 data_test->onmax.var_str) != 0)
4582 if (strcmp(data->onmax.fn_name,
4583 data_test->onmax.fn_name) != 0)
4592 static void print_actions_spec(struct seq_file *m,
4593 struct hist_trigger_data *hist_data)
4597 for (i = 0; i < hist_data->n_actions; i++) {
4598 struct action_data *data = hist_data->actions[i];
4600 if (data->fn == action_trace)
4601 print_onmatch_spec(m, hist_data, data);
4602 else if (data->fn == onmax_save)
4603 print_onmax_spec(m, hist_data, data);
4607 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4611 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4612 kfree(hist_data->field_var_hists[i]->cmd);
4613 kfree(hist_data->field_var_hists[i]);
4617 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4622 destroy_hist_trigger_attrs(hist_data->attrs);
4623 destroy_hist_fields(hist_data);
4624 tracing_map_destroy(hist_data->map);
4626 destroy_actions(hist_data);
4627 destroy_field_vars(hist_data);
4628 destroy_field_var_hists(hist_data);
4629 destroy_synth_var_refs(hist_data);
4634 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4636 struct tracing_map *map = hist_data->map;
4637 struct ftrace_event_field *field;
4638 struct hist_field *hist_field;
4641 for_each_hist_field(i, hist_data) {
4642 hist_field = hist_data->fields[i];
4643 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4644 tracing_map_cmp_fn_t cmp_fn;
4646 field = hist_field->field;
4648 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4649 cmp_fn = tracing_map_cmp_none;
4650 else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
4651 cmp_fn = tracing_map_cmp_num(hist_field->size,
4652 hist_field->is_signed);
4653 else if (is_string_field(field))
4654 cmp_fn = tracing_map_cmp_string;
4656 cmp_fn = tracing_map_cmp_num(field->size,
4658 idx = tracing_map_add_key_field(map,
4661 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4662 idx = tracing_map_add_sum_field(map);
4667 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4668 idx = tracing_map_add_var(map);
4671 hist_field->var.idx = idx;
4672 hist_field->var.hist_data = hist_data;
4679 static struct hist_trigger_data *
4680 create_hist_data(unsigned int map_bits,
4681 struct hist_trigger_attrs *attrs,
4682 struct trace_event_file *file,
4685 const struct tracing_map_ops *map_ops = NULL;
4686 struct hist_trigger_data *hist_data;
4689 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4691 return ERR_PTR(-ENOMEM);
4693 hist_data->attrs = attrs;
4694 hist_data->remove = remove;
4695 hist_data->event_file = file;
4697 ret = parse_actions(hist_data);
4701 ret = create_hist_fields(hist_data, file);
4705 ret = create_sort_keys(hist_data);
4709 map_ops = &hist_trigger_elt_data_ops;
4711 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4712 map_ops, hist_data);
4713 if (IS_ERR(hist_data->map)) {
4714 ret = PTR_ERR(hist_data->map);
4715 hist_data->map = NULL;
4719 ret = create_tracing_map_fields(hist_data);
4725 hist_data->attrs = NULL;
4727 destroy_hist_data(hist_data);
4729 hist_data = ERR_PTR(ret);
4734 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4735 struct tracing_map_elt *elt, void *rec,
4736 struct ring_buffer_event *rbe,
4739 struct hist_elt_data *elt_data;
4740 struct hist_field *hist_field;
4741 unsigned int i, var_idx;
4744 elt_data = elt->private_data;
4745 elt_data->var_ref_vals = var_ref_vals;
4747 for_each_hist_val_field(i, hist_data) {
4748 hist_field = hist_data->fields[i];
4749 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4750 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4751 var_idx = hist_field->var.idx;
4752 tracing_map_set_var(elt, var_idx, hist_val);
4755 tracing_map_update_sum(elt, i, hist_val);
4758 for_each_hist_key_field(i, hist_data) {
4759 hist_field = hist_data->fields[i];
4760 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4761 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4762 var_idx = hist_field->var.idx;
4763 tracing_map_set_var(elt, var_idx, hist_val);
4767 update_field_vars(hist_data, elt, rbe, rec);
4770 static inline void add_to_key(char *compound_key, void *key,
4771 struct hist_field *key_field, void *rec)
4773 size_t size = key_field->size;
4775 if (key_field->flags & HIST_FIELD_FL_STRING) {
4776 struct ftrace_event_field *field;
4778 field = key_field->field;
4779 if (field->filter_type == FILTER_DYN_STRING)
4780 size = *(u32 *)(rec + field->offset) >> 16;
4781 else if (field->filter_type == FILTER_STATIC_STRING)
4784 /* ensure NULL-termination */
4785 if (size > key_field->size - 1)
4786 size = key_field->size - 1;
4788 strncpy(compound_key + key_field->offset, (char *)key, size);
4790 memcpy(compound_key + key_field->offset, key, size);
4794 hist_trigger_actions(struct hist_trigger_data *hist_data,
4795 struct tracing_map_elt *elt, void *rec,
4796 struct ring_buffer_event *rbe, u64 *var_ref_vals)
4798 struct action_data *data;
4801 for (i = 0; i < hist_data->n_actions; i++) {
4802 data = hist_data->actions[i];
4803 data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
4807 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4808 struct ring_buffer_event *rbe)
4810 struct hist_trigger_data *hist_data = data->private_data;
4811 bool use_compound_key = (hist_data->n_keys > 1);
4812 unsigned long entries[HIST_STACKTRACE_DEPTH];
4813 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4814 char compound_key[HIST_KEY_SIZE_MAX];
4815 struct tracing_map_elt *elt = NULL;
4816 struct stack_trace stacktrace;
4817 struct hist_field *key_field;
4822 memset(compound_key, 0, hist_data->key_size);
4824 for_each_hist_key_field(i, hist_data) {
4825 key_field = hist_data->fields[i];
4827 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4828 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
4829 stacktrace.entries = entries;
4830 stacktrace.nr_entries = 0;
4831 stacktrace.skip = HIST_STACKTRACE_SKIP;
4833 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
4834 save_stack_trace(&stacktrace);
4838 field_contents = key_field->fn(key_field, elt, rbe, rec);
4839 if (key_field->flags & HIST_FIELD_FL_STRING) {
4840 key = (void *)(unsigned long)field_contents;
4841 use_compound_key = true;
4843 key = (void *)&field_contents;
4846 if (use_compound_key)
4847 add_to_key(compound_key, key, key_field, rec);
4850 if (use_compound_key)
4853 if (hist_data->n_var_refs &&
4854 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4857 elt = tracing_map_insert(hist_data->map, key);
4861 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4863 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4864 hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
4867 static void hist_trigger_stacktrace_print(struct seq_file *m,
4868 unsigned long *stacktrace_entries,
4869 unsigned int max_entries)
4871 char str[KSYM_SYMBOL_LEN];
4872 unsigned int spaces = 8;
4875 for (i = 0; i < max_entries; i++) {
4876 if (stacktrace_entries[i] == ULONG_MAX)
4879 seq_printf(m, "%*c", 1 + spaces, ' ');
4880 sprint_symbol(str, stacktrace_entries[i]);
4881 seq_printf(m, "%s\n", str);
4886 hist_trigger_entry_print(struct seq_file *m,
4887 struct hist_trigger_data *hist_data, void *key,
4888 struct tracing_map_elt *elt)
4890 struct hist_field *key_field;
4891 char str[KSYM_SYMBOL_LEN];
4892 bool multiline = false;
4893 const char *field_name;
4899 for_each_hist_key_field(i, hist_data) {
4900 key_field = hist_data->fields[i];
4902 if (i > hist_data->n_vals)
4905 field_name = hist_field_name(key_field, 0);
4907 if (key_field->flags & HIST_FIELD_FL_HEX) {
4908 uval = *(u64 *)(key + key_field->offset);
4909 seq_printf(m, "%s: %llx", field_name, uval);
4910 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4911 uval = *(u64 *)(key + key_field->offset);
4912 sprint_symbol_no_offset(str, uval);
4913 seq_printf(m, "%s: [%llx] %-45s", field_name,
4915 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4916 uval = *(u64 *)(key + key_field->offset);
4917 sprint_symbol(str, uval);
4918 seq_printf(m, "%s: [%llx] %-55s", field_name,
4920 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4921 struct hist_elt_data *elt_data = elt->private_data;
4924 if (WARN_ON_ONCE(!elt_data))
4927 comm = elt_data->comm;
4929 uval = *(u64 *)(key + key_field->offset);
4930 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4932 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4933 const char *syscall_name;
4935 uval = *(u64 *)(key + key_field->offset);
4936 syscall_name = get_syscall_name(uval);
4938 syscall_name = "unknown_syscall";
4940 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4941 syscall_name, uval);
4942 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4943 seq_puts(m, "stacktrace:\n");
4944 hist_trigger_stacktrace_print(m,
4945 key + key_field->offset,
4946 HIST_STACKTRACE_DEPTH);
4948 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4949 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4950 *(u64 *)(key + key_field->offset));
4951 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4952 seq_printf(m, "%s: %-50s", field_name,
4953 (char *)(key + key_field->offset));
4955 uval = *(u64 *)(key + key_field->offset);
4956 seq_printf(m, "%s: %10llu", field_name, uval);
4965 seq_printf(m, " hitcount: %10llu",
4966 tracing_map_read_sum(elt, HITCOUNT_IDX));
4968 for (i = 1; i < hist_data->n_vals; i++) {
4969 field_name = hist_field_name(hist_data->fields[i], 0);
4971 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4972 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4975 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4976 seq_printf(m, " %s: %10llx", field_name,
4977 tracing_map_read_sum(elt, i));
4979 seq_printf(m, " %s: %10llu", field_name,
4980 tracing_map_read_sum(elt, i));
4984 print_actions(m, hist_data, elt);
4989 static int print_entries(struct seq_file *m,
4990 struct hist_trigger_data *hist_data)
4992 struct tracing_map_sort_entry **sort_entries = NULL;
4993 struct tracing_map *map = hist_data->map;
4996 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4997 hist_data->n_sort_keys,
5002 for (i = 0; i < n_entries; i++)
5003 hist_trigger_entry_print(m, hist_data,
5004 sort_entries[i]->key,
5005 sort_entries[i]->elt);
5007 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5012 static void hist_trigger_show(struct seq_file *m,
5013 struct event_trigger_data *data, int n)
5015 struct hist_trigger_data *hist_data;
5019 seq_puts(m, "\n\n");
5021 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5022 data->ops->print(m, data->ops, data);
5023 seq_puts(m, "#\n\n");
5025 hist_data = data->private_data;
5026 n_entries = print_entries(m, hist_data);
5030 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5031 (u64)atomic64_read(&hist_data->map->hits),
5032 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5035 static int hist_show(struct seq_file *m, void *v)
5037 struct event_trigger_data *data;
5038 struct trace_event_file *event_file;
5041 mutex_lock(&event_mutex);
5043 event_file = event_file_data(m->private);
5044 if (unlikely(!event_file)) {
5049 list_for_each_entry(data, &event_file->triggers, list) {
5050 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5051 hist_trigger_show(m, data, n++);
5054 if (have_hist_err()) {
5055 seq_printf(m, "\nERROR: %s\n", hist_err_str);
5056 seq_printf(m, " Last command: %s\n", last_hist_cmd);
5060 mutex_unlock(&event_mutex);
5065 static int event_hist_open(struct inode *inode, struct file *file)
5067 return single_open(file, hist_show, file);
5070 const struct file_operations event_hist_fops = {
5071 .open = event_hist_open,
5073 .llseek = seq_lseek,
5074 .release = single_release,
5077 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5079 const char *field_name = hist_field_name(hist_field, 0);
5081 if (hist_field->var.name)
5082 seq_printf(m, "%s=", hist_field->var.name);
5084 if (hist_field->flags & HIST_FIELD_FL_CPU)
5085 seq_puts(m, "common_cpu");
5086 else if (field_name) {
5087 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5088 hist_field->flags & HIST_FIELD_FL_ALIAS)
5090 seq_printf(m, "%s", field_name);
5091 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5092 seq_puts(m, "common_timestamp");
5094 if (hist_field->flags) {
5095 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5096 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5097 const char *flags = get_hist_field_flags(hist_field);
5100 seq_printf(m, ".%s", flags);
5105 static int event_hist_trigger_print(struct seq_file *m,
5106 struct event_trigger_ops *ops,
5107 struct event_trigger_data *data)
5109 struct hist_trigger_data *hist_data = data->private_data;
5110 struct hist_field *field;
5111 bool have_var = false;
5114 seq_puts(m, "hist:");
5117 seq_printf(m, "%s:", data->name);
5119 seq_puts(m, "keys=");
5121 for_each_hist_key_field(i, hist_data) {
5122 field = hist_data->fields[i];
5124 if (i > hist_data->n_vals)
5127 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5128 seq_puts(m, "stacktrace");
5130 hist_field_print(m, field);
5133 seq_puts(m, ":vals=");
5135 for_each_hist_val_field(i, hist_data) {
5136 field = hist_data->fields[i];
5137 if (field->flags & HIST_FIELD_FL_VAR) {
5142 if (i == HITCOUNT_IDX)
5143 seq_puts(m, "hitcount");
5146 hist_field_print(m, field);
5155 for_each_hist_val_field(i, hist_data) {
5156 field = hist_data->fields[i];
5158 if (field->flags & HIST_FIELD_FL_VAR) {
5161 hist_field_print(m, field);
5166 seq_puts(m, ":sort=");
5168 for (i = 0; i < hist_data->n_sort_keys; i++) {
5169 struct tracing_map_sort_key *sort_key;
5170 unsigned int idx, first_key_idx;
5173 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5175 sort_key = &hist_data->sort_keys[i];
5176 idx = sort_key->field_idx;
5178 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5184 if (idx == HITCOUNT_IDX)
5185 seq_puts(m, "hitcount");
5187 if (idx >= first_key_idx)
5188 idx += hist_data->n_vars;
5189 hist_field_print(m, hist_data->fields[idx]);
5192 if (sort_key->descending)
5193 seq_puts(m, ".descending");
5195 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5196 if (hist_data->enable_timestamps)
5197 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5199 print_actions_spec(m, hist_data);
5201 if (data->filter_str)
5202 seq_printf(m, " if %s", data->filter_str);
5205 seq_puts(m, " [paused]");
5207 seq_puts(m, " [active]");
5214 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5215 struct event_trigger_data *data)
5217 struct hist_trigger_data *hist_data = data->private_data;
5219 if (!data->ref && hist_data->attrs->name)
5220 save_named_trigger(hist_data->attrs->name, data);
5227 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5229 struct trace_event_file *file;
5234 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5235 file = hist_data->field_var_hists[i]->hist_data->event_file;
5236 cmd = hist_data->field_var_hists[i]->cmd;
5237 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5238 "!hist", "hist", cmd);
5242 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5243 struct event_trigger_data *data)
5245 struct hist_trigger_data *hist_data = data->private_data;
5247 if (WARN_ON_ONCE(data->ref <= 0))
5253 del_named_trigger(data);
5255 trigger_data_free(data);
5257 remove_hist_vars(hist_data);
5259 unregister_field_var_hists(hist_data);
5261 destroy_hist_data(hist_data);
5265 static struct event_trigger_ops event_hist_trigger_ops = {
5266 .func = event_hist_trigger,
5267 .print = event_hist_trigger_print,
5268 .init = event_hist_trigger_init,
5269 .free = event_hist_trigger_free,
5272 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5273 struct event_trigger_data *data)
5277 save_named_trigger(data->named_data->name, data);
5279 event_hist_trigger_init(ops, data->named_data);
5284 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5285 struct event_trigger_data *data)
5287 if (WARN_ON_ONCE(data->ref <= 0))
5290 event_hist_trigger_free(ops, data->named_data);
5294 del_named_trigger(data);
5295 trigger_data_free(data);
5299 static struct event_trigger_ops event_hist_trigger_named_ops = {
5300 .func = event_hist_trigger,
5301 .print = event_hist_trigger_print,
5302 .init = event_hist_trigger_named_init,
5303 .free = event_hist_trigger_named_free,
5306 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5309 return &event_hist_trigger_ops;
5312 static void hist_clear(struct event_trigger_data *data)
5314 struct hist_trigger_data *hist_data = data->private_data;
5317 pause_named_trigger(data);
5319 tracepoint_synchronize_unregister();
5321 tracing_map_clear(hist_data->map);
5324 unpause_named_trigger(data);
5327 static bool compatible_field(struct ftrace_event_field *field,
5328 struct ftrace_event_field *test_field)
5330 if (field == test_field)
5332 if (field == NULL || test_field == NULL)
5334 if (strcmp(field->name, test_field->name) != 0)
5336 if (strcmp(field->type, test_field->type) != 0)
5338 if (field->size != test_field->size)
5340 if (field->is_signed != test_field->is_signed)
5346 static bool hist_trigger_match(struct event_trigger_data *data,
5347 struct event_trigger_data *data_test,
5348 struct event_trigger_data *named_data,
5351 struct tracing_map_sort_key *sort_key, *sort_key_test;
5352 struct hist_trigger_data *hist_data, *hist_data_test;
5353 struct hist_field *key_field, *key_field_test;
5356 if (named_data && (named_data != data_test) &&
5357 (named_data != data_test->named_data))
5360 if (!named_data && is_named_trigger(data_test))
5363 hist_data = data->private_data;
5364 hist_data_test = data_test->private_data;
5366 if (hist_data->n_vals != hist_data_test->n_vals ||
5367 hist_data->n_fields != hist_data_test->n_fields ||
5368 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5371 if (!ignore_filter) {
5372 if ((data->filter_str && !data_test->filter_str) ||
5373 (!data->filter_str && data_test->filter_str))
5377 for_each_hist_field(i, hist_data) {
5378 key_field = hist_data->fields[i];
5379 key_field_test = hist_data_test->fields[i];
5381 if (key_field->flags != key_field_test->flags)
5383 if (!compatible_field(key_field->field, key_field_test->field))
5385 if (key_field->offset != key_field_test->offset)
5387 if (key_field->size != key_field_test->size)
5389 if (key_field->is_signed != key_field_test->is_signed)
5391 if (!!key_field->var.name != !!key_field_test->var.name)
5393 if (key_field->var.name &&
5394 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5398 for (i = 0; i < hist_data->n_sort_keys; i++) {
5399 sort_key = &hist_data->sort_keys[i];
5400 sort_key_test = &hist_data_test->sort_keys[i];
5402 if (sort_key->field_idx != sort_key_test->field_idx ||
5403 sort_key->descending != sort_key_test->descending)
5407 if (!ignore_filter && data->filter_str &&
5408 (strcmp(data->filter_str, data_test->filter_str) != 0))
5411 if (!actions_match(hist_data, hist_data_test))
5417 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5418 struct event_trigger_data *data,
5419 struct trace_event_file *file)
5421 struct hist_trigger_data *hist_data = data->private_data;
5422 struct event_trigger_data *test, *named_data = NULL;
5425 if (hist_data->attrs->name) {
5426 named_data = find_named_trigger(hist_data->attrs->name);
5428 if (!hist_trigger_match(data, named_data, named_data,
5430 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
5437 if (hist_data->attrs->name && !named_data)
5440 lockdep_assert_held(&event_mutex);
5442 list_for_each_entry(test, &file->triggers, list) {
5443 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5444 if (!hist_trigger_match(data, test, named_data, false))
5446 if (hist_data->attrs->pause)
5447 test->paused = true;
5448 else if (hist_data->attrs->cont)
5449 test->paused = false;
5450 else if (hist_data->attrs->clear)
5453 hist_err("Hist trigger already exists", NULL);
5460 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5461 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
5466 if (hist_data->attrs->pause)
5467 data->paused = true;
5470 data->private_data = named_data->private_data;
5471 set_named_trigger_data(data, named_data);
5472 data->ops = &event_hist_trigger_named_ops;
5475 if (data->ops->init) {
5476 ret = data->ops->init(data->ops, data);
5481 if (hist_data->enable_timestamps) {
5482 char *clock = hist_data->attrs->clock;
5484 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5486 hist_err("Couldn't set trace_clock: ", clock);
5490 tracing_set_time_stamp_abs(file->tr, true);
5494 destroy_hist_data(hist_data);
5501 static int hist_trigger_enable(struct event_trigger_data *data,
5502 struct trace_event_file *file)
5506 list_add_tail_rcu(&data->list, &file->triggers);
5508 update_cond_flag(file);
5510 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5511 list_del_rcu(&data->list);
5512 update_cond_flag(file);
5519 static bool have_hist_trigger_match(struct event_trigger_data *data,
5520 struct trace_event_file *file)
5522 struct hist_trigger_data *hist_data = data->private_data;
5523 struct event_trigger_data *test, *named_data = NULL;
5526 lockdep_assert_held(&event_mutex);
5528 if (hist_data->attrs->name)
5529 named_data = find_named_trigger(hist_data->attrs->name);
5531 list_for_each_entry(test, &file->triggers, list) {
5532 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5533 if (hist_trigger_match(data, test, named_data, false)) {
5543 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5544 struct trace_event_file *file)
5546 struct hist_trigger_data *hist_data = data->private_data;
5547 struct event_trigger_data *test, *named_data = NULL;
5549 lockdep_assert_held(&event_mutex);
5551 if (hist_data->attrs->name)
5552 named_data = find_named_trigger(hist_data->attrs->name);
5554 list_for_each_entry(test, &file->triggers, list) {
5555 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5556 if (!hist_trigger_match(data, test, named_data, false))
5558 hist_data = test->private_data;
5559 if (check_var_refs(hist_data))
5568 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5569 struct event_trigger_data *data,
5570 struct trace_event_file *file)
5572 struct hist_trigger_data *hist_data = data->private_data;
5573 struct event_trigger_data *test, *named_data = NULL;
5574 bool unregistered = false;
5576 lockdep_assert_held(&event_mutex);
5578 if (hist_data->attrs->name)
5579 named_data = find_named_trigger(hist_data->attrs->name);
5581 list_for_each_entry(test, &file->triggers, list) {
5582 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5583 if (!hist_trigger_match(data, test, named_data, false))
5585 unregistered = true;
5586 list_del_rcu(&test->list);
5587 trace_event_trigger_enable_disable(file, 0);
5588 update_cond_flag(file);
5593 if (unregistered && test->ops->free)
5594 test->ops->free(test->ops, test);
5596 if (hist_data->enable_timestamps) {
5597 if (!hist_data->remove || unregistered)
5598 tracing_set_time_stamp_abs(file->tr, false);
5602 static bool hist_file_check_refs(struct trace_event_file *file)
5604 struct hist_trigger_data *hist_data;
5605 struct event_trigger_data *test;
5607 lockdep_assert_held(&event_mutex);
5609 list_for_each_entry(test, &file->triggers, list) {
5610 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5611 hist_data = test->private_data;
5612 if (check_var_refs(hist_data))
5620 static void hist_unreg_all(struct trace_event_file *file)
5622 struct event_trigger_data *test, *n;
5623 struct hist_trigger_data *hist_data;
5624 struct synth_event *se;
5625 const char *se_name;
5627 if (hist_file_check_refs(file))
5630 list_for_each_entry_safe(test, n, &file->triggers, list) {
5631 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5632 hist_data = test->private_data;
5633 list_del_rcu(&test->list);
5634 trace_event_trigger_enable_disable(file, 0);
5636 mutex_lock(&synth_event_mutex);
5637 se_name = trace_event_name(file->event_call);
5638 se = find_synth_event(se_name);
5641 mutex_unlock(&synth_event_mutex);
5643 update_cond_flag(file);
5644 if (hist_data->enable_timestamps)
5645 tracing_set_time_stamp_abs(file->tr, false);
5646 if (test->ops->free)
5647 test->ops->free(test->ops, test);
5652 static int event_hist_trigger_func(struct event_command *cmd_ops,
5653 struct trace_event_file *file,
5654 char *glob, char *cmd, char *param)
5656 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5657 struct event_trigger_data *trigger_data;
5658 struct hist_trigger_attrs *attrs;
5659 struct event_trigger_ops *trigger_ops;
5660 struct hist_trigger_data *hist_data;
5661 struct synth_event *se;
5662 const char *se_name;
5663 bool remove = false;
5667 if (glob && strlen(glob)) {
5668 last_cmd_set(param);
5679 * separate the trigger from the filter (k:v [if filter])
5680 * allowing for whitespace in the trigger
5682 p = trigger = param;
5684 p = strstr(p, "if");
5689 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5693 if (p >= param + strlen(param) - strlen("if") - 1)
5695 if (*(p + strlen("if")) != ' ' && *(p + strlen("if")) != '\t') {
5706 param = strstrip(p);
5707 trigger = strstrip(trigger);
5710 attrs = parse_hist_trigger_attrs(trigger);
5712 return PTR_ERR(attrs);
5714 if (attrs->map_bits)
5715 hist_trigger_bits = attrs->map_bits;
5717 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5718 if (IS_ERR(hist_data)) {
5719 destroy_hist_trigger_attrs(attrs);
5720 return PTR_ERR(hist_data);
5723 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5725 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5726 if (!trigger_data) {
5731 trigger_data->count = -1;
5732 trigger_data->ops = trigger_ops;
5733 trigger_data->cmd_ops = cmd_ops;
5735 INIT_LIST_HEAD(&trigger_data->list);
5736 RCU_INIT_POINTER(trigger_data->filter, NULL);
5738 trigger_data->private_data = hist_data;
5740 /* if param is non-empty, it's supposed to be a filter */
5741 if (param && cmd_ops->set_filter) {
5742 ret = cmd_ops->set_filter(param, trigger_data, file);
5748 if (!have_hist_trigger_match(trigger_data, file))
5751 if (hist_trigger_check_refs(trigger_data, file)) {
5756 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5758 mutex_lock(&synth_event_mutex);
5759 se_name = trace_event_name(file->event_call);
5760 se = find_synth_event(se_name);
5763 mutex_unlock(&synth_event_mutex);
5769 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5771 * The above returns on success the # of triggers registered,
5772 * but if it didn't register any it returns zero. Consider no
5773 * triggers registered a failure too.
5776 if (!(attrs->pause || attrs->cont || attrs->clear))
5782 if (get_named_trigger_data(trigger_data))
5785 if (has_hist_vars(hist_data))
5786 save_hist_vars(hist_data);
5788 ret = create_actions(hist_data, file);
5792 ret = tracing_map_init(hist_data->map);
5796 ret = hist_trigger_enable(trigger_data, file);
5800 mutex_lock(&synth_event_mutex);
5801 se_name = trace_event_name(file->event_call);
5802 se = find_synth_event(se_name);
5805 mutex_unlock(&synth_event_mutex);
5807 /* Just return zero, not the number of registered triggers */
5815 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5817 if (cmd_ops->set_filter)
5818 cmd_ops->set_filter(NULL, trigger_data, NULL);
5820 remove_hist_vars(hist_data);
5822 kfree(trigger_data);
5824 destroy_hist_data(hist_data);
5828 static struct event_command trigger_hist_cmd = {
5830 .trigger_type = ETT_EVENT_HIST,
5831 .flags = EVENT_CMD_FL_NEEDS_REC,
5832 .func = event_hist_trigger_func,
5833 .reg = hist_register_trigger,
5834 .unreg = hist_unregister_trigger,
5835 .unreg_all = hist_unreg_all,
5836 .get_trigger_ops = event_hist_get_trigger_ops,
5837 .set_filter = set_trigger_filter,
5840 __init int register_trigger_hist_cmd(void)
5844 ret = register_event_command(&trigger_hist_cmd);
5851 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5852 struct ring_buffer_event *event)
5854 struct enable_trigger_data *enable_data = data->private_data;
5855 struct event_trigger_data *test;
5857 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
5858 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5859 if (enable_data->enable)
5860 test->paused = false;
5862 test->paused = true;
5868 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5869 struct ring_buffer_event *event)
5874 if (data->count != -1)
5877 hist_enable_trigger(data, rec, event);
5880 static struct event_trigger_ops hist_enable_trigger_ops = {
5881 .func = hist_enable_trigger,
5882 .print = event_enable_trigger_print,
5883 .init = event_trigger_init,
5884 .free = event_enable_trigger_free,
5887 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5888 .func = hist_enable_count_trigger,
5889 .print = event_enable_trigger_print,
5890 .init = event_trigger_init,
5891 .free = event_enable_trigger_free,
5894 static struct event_trigger_ops hist_disable_trigger_ops = {
5895 .func = hist_enable_trigger,
5896 .print = event_enable_trigger_print,
5897 .init = event_trigger_init,
5898 .free = event_enable_trigger_free,
5901 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5902 .func = hist_enable_count_trigger,
5903 .print = event_enable_trigger_print,
5904 .init = event_trigger_init,
5905 .free = event_enable_trigger_free,
5908 static struct event_trigger_ops *
5909 hist_enable_get_trigger_ops(char *cmd, char *param)
5911 struct event_trigger_ops *ops;
5914 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5917 ops = param ? &hist_enable_count_trigger_ops :
5918 &hist_enable_trigger_ops;
5920 ops = param ? &hist_disable_count_trigger_ops :
5921 &hist_disable_trigger_ops;
5926 static void hist_enable_unreg_all(struct trace_event_file *file)
5928 struct event_trigger_data *test, *n;
5930 list_for_each_entry_safe(test, n, &file->triggers, list) {
5931 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5932 list_del_rcu(&test->list);
5933 update_cond_flag(file);
5934 trace_event_trigger_enable_disable(file, 0);
5935 if (test->ops->free)
5936 test->ops->free(test->ops, test);
5941 static struct event_command trigger_hist_enable_cmd = {
5942 .name = ENABLE_HIST_STR,
5943 .trigger_type = ETT_HIST_ENABLE,
5944 .func = event_enable_trigger_func,
5945 .reg = event_enable_register_trigger,
5946 .unreg = event_enable_unregister_trigger,
5947 .unreg_all = hist_enable_unreg_all,
5948 .get_trigger_ops = hist_enable_get_trigger_ops,
5949 .set_filter = set_trigger_filter,
5952 static struct event_command trigger_hist_disable_cmd = {
5953 .name = DISABLE_HIST_STR,
5954 .trigger_type = ETT_HIST_ENABLE,
5955 .func = event_enable_trigger_func,
5956 .reg = event_enable_register_trigger,
5957 .unreg = event_enable_unregister_trigger,
5958 .unreg_all = hist_enable_unreg_all,
5959 .get_trigger_ops = hist_enable_get_trigger_ops,
5960 .set_filter = set_trigger_filter,
5963 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5965 unregister_event_command(&trigger_hist_enable_cmd);
5966 unregister_event_command(&trigger_hist_disable_cmd);
5969 __init int register_trigger_hist_enable_disable_cmds(void)
5973 ret = register_event_command(&trigger_hist_enable_cmd);
5974 if (WARN_ON(ret < 0))
5976 ret = register_event_command(&trigger_hist_disable_cmd);
5977 if (WARN_ON(ret < 0))
5978 unregister_trigger_hist_enable_disable_cmds();
5983 static __init int trace_events_hist_init(void)
5985 struct dentry *entry = NULL;
5986 struct dentry *d_tracer;
5989 d_tracer = tracing_init_dentry();
5990 if (IS_ERR(d_tracer)) {
5991 err = PTR_ERR(d_tracer);
5995 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
5996 NULL, &synth_events_fops);
6004 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6009 fs_initcall(trace_events_hist_init);