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 kfree(ref_field->event_name);
2460 kfree(ref_field->name);
2466 * create_var_ref - Create a variable reference and attach it to trigger
2467 * @hist_data: The trigger that will be referencing the variable
2468 * @var_field: The VAR field to create a reference to
2469 * @system: The optional system string
2470 * @event_name: The optional event_name string
2472 * Given a variable hist_field, create a VAR_REF hist_field that
2473 * represents a reference to it.
2475 * This function also adds the reference to the trigger that
2476 * now references the variable.
2478 * Return: The VAR_REF field if successful, NULL if not
2480 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2481 struct hist_field *var_field,
2482 char *system, char *event_name)
2484 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2485 struct hist_field *ref_field;
2488 /* Check if the variable already exists */
2489 for (i = 0; i < hist_data->n_var_refs; i++) {
2490 ref_field = hist_data->var_refs[i];
2491 if (ref_field->var.idx == var_field->var.idx &&
2492 ref_field->var.hist_data == var_field->hist_data) {
2493 get_hist_field(ref_field);
2498 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2500 if (init_var_ref(ref_field, var_field, system, event_name)) {
2501 destroy_hist_field(ref_field, 0);
2505 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2506 ref_field->var_ref_idx = hist_data->n_var_refs++;
2512 static bool is_var_ref(char *var_name)
2514 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2520 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2526 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2527 name = hist_data->attrs->var_defs.name[i];
2529 if (strcmp(var_name, name) == 0) {
2530 field = hist_data->attrs->var_defs.expr[i];
2531 if (contains_operator(field) || is_var_ref(field))
2540 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2541 char *system, char *event_name,
2544 struct trace_event_call *call;
2546 if (system && event_name) {
2547 call = hist_data->event_file->event_call;
2549 if (strcmp(system, call->class->system) != 0)
2552 if (strcmp(event_name, trace_event_name(call)) != 0)
2556 if (!!system != !!event_name)
2559 if (!is_var_ref(var_name))
2564 return field_name_from_var(hist_data, var_name);
2567 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2568 char *system, char *event_name,
2571 struct hist_field *var_field = NULL, *ref_field = NULL;
2573 if (!is_var_ref(var_name))
2578 var_field = find_event_var(hist_data, system, event_name, var_name);
2580 ref_field = create_var_ref(hist_data, var_field,
2581 system, event_name);
2584 hist_err_event("Couldn't find variable: $",
2585 system, event_name, var_name);
2590 static struct ftrace_event_field *
2591 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2592 char *field_str, unsigned long *flags)
2594 struct ftrace_event_field *field = NULL;
2595 char *field_name, *modifier, *str;
2597 modifier = str = kstrdup(field_str, GFP_KERNEL);
2599 return ERR_PTR(-ENOMEM);
2601 field_name = strsep(&modifier, ".");
2603 if (strcmp(modifier, "hex") == 0)
2604 *flags |= HIST_FIELD_FL_HEX;
2605 else if (strcmp(modifier, "sym") == 0)
2606 *flags |= HIST_FIELD_FL_SYM;
2607 else if (strcmp(modifier, "sym-offset") == 0)
2608 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2609 else if ((strcmp(modifier, "execname") == 0) &&
2610 (strcmp(field_name, "common_pid") == 0))
2611 *flags |= HIST_FIELD_FL_EXECNAME;
2612 else if (strcmp(modifier, "syscall") == 0)
2613 *flags |= HIST_FIELD_FL_SYSCALL;
2614 else if (strcmp(modifier, "log2") == 0)
2615 *flags |= HIST_FIELD_FL_LOG2;
2616 else if (strcmp(modifier, "usecs") == 0)
2617 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2619 hist_err("Invalid field modifier: ", modifier);
2620 field = ERR_PTR(-EINVAL);
2625 if (strcmp(field_name, "common_timestamp") == 0) {
2626 *flags |= HIST_FIELD_FL_TIMESTAMP;
2627 hist_data->enable_timestamps = true;
2628 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2629 hist_data->attrs->ts_in_usecs = true;
2630 } else if (strcmp(field_name, "common_cpu") == 0)
2631 *flags |= HIST_FIELD_FL_CPU;
2633 field = trace_find_event_field(file->event_call, field_name);
2634 if (!field || !field->size) {
2636 * For backward compatibility, if field_name
2637 * was "cpu", then we treat this the same as
2640 if (strcmp(field_name, "cpu") == 0) {
2641 *flags |= HIST_FIELD_FL_CPU;
2643 hist_err("Couldn't find field: ", field_name);
2644 field = ERR_PTR(-EINVAL);
2655 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2656 struct hist_field *var_ref,
2659 struct hist_field *alias = NULL;
2660 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2662 alias = create_hist_field(hist_data, NULL, flags, var_name);
2666 alias->fn = var_ref->fn;
2667 alias->operands[0] = var_ref;
2669 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2670 destroy_hist_field(alias, 0);
2674 alias->var_ref_idx = var_ref->var_ref_idx;
2679 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2680 struct trace_event_file *file, char *str,
2681 unsigned long *flags, char *var_name)
2683 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2684 struct ftrace_event_field *field = NULL;
2685 struct hist_field *hist_field = NULL;
2688 s = strchr(str, '.');
2690 s = strchr(++s, '.');
2692 ref_system = strsep(&str, ".");
2697 ref_event = strsep(&str, ".");
2706 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2708 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2711 hist_field = create_alias(hist_data, hist_field, var_name);
2722 field = parse_field(hist_data, file, str, flags);
2723 if (IS_ERR(field)) {
2724 ret = PTR_ERR(field);
2728 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2736 return ERR_PTR(ret);
2739 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2740 struct trace_event_file *file,
2741 char *str, unsigned long flags,
2742 char *var_name, unsigned int level);
2744 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2745 struct trace_event_file *file,
2746 char *str, unsigned long flags,
2747 char *var_name, unsigned int level)
2749 struct hist_field *operand1, *expr = NULL;
2750 unsigned long operand_flags;
2754 /* we support only -(xxx) i.e. explicit parens required */
2757 hist_err("Too many subexpressions (3 max): ", str);
2762 str++; /* skip leading '-' */
2764 s = strchr(str, '(');
2772 s = strrchr(str, ')');
2776 ret = -EINVAL; /* no closing ')' */
2780 flags |= HIST_FIELD_FL_EXPR;
2781 expr = create_hist_field(hist_data, NULL, flags, var_name);
2788 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2789 if (IS_ERR(operand1)) {
2790 ret = PTR_ERR(operand1);
2793 if (operand1->flags & HIST_FIELD_FL_STRING) {
2794 /* String type can not be the operand of unary operator. */
2795 destroy_hist_field(operand1, 0);
2800 expr->flags |= operand1->flags &
2801 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2802 expr->fn = hist_field_unary_minus;
2803 expr->operands[0] = operand1;
2804 expr->operator = FIELD_OP_UNARY_MINUS;
2805 expr->name = expr_str(expr, 0);
2806 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2814 destroy_hist_field(expr, 0);
2815 return ERR_PTR(ret);
2818 static int check_expr_operands(struct hist_field *operand1,
2819 struct hist_field *operand2)
2821 unsigned long operand1_flags = operand1->flags;
2822 unsigned long operand2_flags = operand2->flags;
2824 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2825 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2826 struct hist_field *var;
2828 var = find_var_field(operand1->var.hist_data, operand1->name);
2831 operand1_flags = var->flags;
2834 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2835 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2836 struct hist_field *var;
2838 var = find_var_field(operand2->var.hist_data, operand2->name);
2841 operand2_flags = var->flags;
2844 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2845 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2846 hist_err("Timestamp units in expression don't match", NULL);
2853 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2854 struct trace_event_file *file,
2855 char *str, unsigned long flags,
2856 char *var_name, unsigned int level)
2858 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2859 unsigned long operand_flags;
2860 int field_op, ret = -EINVAL;
2861 char *sep, *operand1_str;
2864 hist_err("Too many subexpressions (3 max): ", str);
2865 return ERR_PTR(-EINVAL);
2868 field_op = contains_operator(str);
2870 if (field_op == FIELD_OP_NONE)
2871 return parse_atom(hist_data, file, str, &flags, var_name);
2873 if (field_op == FIELD_OP_UNARY_MINUS)
2874 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2877 case FIELD_OP_MINUS:
2887 operand1_str = strsep(&str, sep);
2888 if (!operand1_str || !str)
2892 operand1 = parse_atom(hist_data, file, operand1_str,
2893 &operand_flags, NULL);
2894 if (IS_ERR(operand1)) {
2895 ret = PTR_ERR(operand1);
2899 if (operand1->flags & HIST_FIELD_FL_STRING) {
2904 /* rest of string could be another expression e.g. b+c in a+b+c */
2906 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2907 if (IS_ERR(operand2)) {
2908 ret = PTR_ERR(operand2);
2912 if (operand2->flags & HIST_FIELD_FL_STRING) {
2917 ret = check_expr_operands(operand1, operand2);
2921 flags |= HIST_FIELD_FL_EXPR;
2923 flags |= operand1->flags &
2924 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2926 expr = create_hist_field(hist_data, NULL, flags, var_name);
2932 operand1->read_once = true;
2933 operand2->read_once = true;
2935 expr->operands[0] = operand1;
2936 expr->operands[1] = operand2;
2938 /* The operand sizes should be the same, so just pick one */
2939 expr->size = operand1->size;
2941 expr->operator = field_op;
2942 expr->name = expr_str(expr, 0);
2943 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2950 case FIELD_OP_MINUS:
2951 expr->fn = hist_field_minus;
2954 expr->fn = hist_field_plus;
2963 destroy_hist_field(operand1, 0);
2964 destroy_hist_field(operand2, 0);
2965 destroy_hist_field(expr, 0);
2967 return ERR_PTR(ret);
2970 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2971 struct trace_event_file *file)
2973 struct event_trigger_data *test;
2975 lockdep_assert_held(&event_mutex);
2977 list_for_each_entry(test, &file->triggers, list) {
2978 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2979 if (test->private_data == hist_data)
2980 return test->filter_str;
2987 static struct event_command trigger_hist_cmd;
2988 static int event_hist_trigger_func(struct event_command *cmd_ops,
2989 struct trace_event_file *file,
2990 char *glob, char *cmd, char *param);
2992 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2993 struct hist_trigger_data *hist_data,
2994 unsigned int n_keys)
2996 struct hist_field *target_hist_field, *hist_field;
2997 unsigned int n, i, j;
2999 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3002 i = hist_data->n_vals;
3003 j = target_hist_data->n_vals;
3005 for (n = 0; n < n_keys; n++) {
3006 hist_field = hist_data->fields[i + n];
3007 target_hist_field = target_hist_data->fields[j + n];
3009 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3011 if (hist_field->size != target_hist_field->size)
3013 if (hist_field->is_signed != target_hist_field->is_signed)
3020 static struct hist_trigger_data *
3021 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3022 struct trace_event_file *file)
3024 struct hist_trigger_data *hist_data;
3025 struct event_trigger_data *test;
3026 unsigned int n_keys;
3028 lockdep_assert_held(&event_mutex);
3030 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3032 list_for_each_entry(test, &file->triggers, list) {
3033 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3034 hist_data = test->private_data;
3036 if (compatible_keys(target_hist_data, hist_data, n_keys))
3044 static struct trace_event_file *event_file(struct trace_array *tr,
3045 char *system, char *event_name)
3047 struct trace_event_file *file;
3049 file = __find_event_file(tr, system, event_name);
3051 return ERR_PTR(-EINVAL);
3056 static struct hist_field *
3057 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3058 char *system, char *event_name, char *field_name)
3060 struct hist_field *event_var;
3061 char *synthetic_name;
3063 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3064 if (!synthetic_name)
3065 return ERR_PTR(-ENOMEM);
3067 strcpy(synthetic_name, "synthetic_");
3068 strcat(synthetic_name, field_name);
3070 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3072 kfree(synthetic_name);
3078 * create_field_var_hist - Automatically create a histogram and var for a field
3079 * @target_hist_data: The target hist trigger
3080 * @subsys_name: Optional subsystem name
3081 * @event_name: Optional event name
3082 * @field_name: The name of the field (and the resulting variable)
3084 * Hist trigger actions fetch data from variables, not directly from
3085 * events. However, for convenience, users are allowed to directly
3086 * specify an event field in an action, which will be automatically
3087 * converted into a variable on their behalf.
3089 * If a user specifies a field on an event that isn't the event the
3090 * histogram currently being defined (the target event histogram), the
3091 * only way that can be accomplished is if a new hist trigger is
3092 * created and the field variable defined on that.
3094 * This function creates a new histogram compatible with the target
3095 * event (meaning a histogram with the same key as the target
3096 * histogram), and creates a variable for the specified field, but
3097 * with 'synthetic_' prepended to the variable name in order to avoid
3098 * collision with normal field variables.
3100 * Return: The variable created for the field.
3102 static struct hist_field *
3103 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3104 char *subsys_name, char *event_name, char *field_name)
3106 struct trace_array *tr = target_hist_data->event_file->tr;
3107 struct hist_field *event_var = ERR_PTR(-EINVAL);
3108 struct hist_trigger_data *hist_data;
3109 unsigned int i, n, first = true;
3110 struct field_var_hist *var_hist;
3111 struct trace_event_file *file;
3112 struct hist_field *key_field;
3117 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3118 hist_err_event("onmatch: Too many field variables defined: ",
3119 subsys_name, event_name, field_name);
3120 return ERR_PTR(-EINVAL);
3123 file = event_file(tr, subsys_name, event_name);
3126 hist_err_event("onmatch: Event file not found: ",
3127 subsys_name, event_name, field_name);
3128 ret = PTR_ERR(file);
3129 return ERR_PTR(ret);
3133 * Look for a histogram compatible with target. We'll use the
3134 * found histogram specification to create a new matching
3135 * histogram with our variable on it. target_hist_data is not
3136 * yet a registered histogram so we can't use that.
3138 hist_data = find_compatible_hist(target_hist_data, file);
3140 hist_err_event("onmatch: Matching event histogram not found: ",
3141 subsys_name, event_name, field_name);
3142 return ERR_PTR(-EINVAL);
3145 /* See if a synthetic field variable has already been created */
3146 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3147 event_name, field_name);
3148 if (!IS_ERR_OR_NULL(event_var))
3151 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3153 return ERR_PTR(-ENOMEM);
3155 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3158 return ERR_PTR(-ENOMEM);
3161 /* Use the same keys as the compatible histogram */
3162 strcat(cmd, "keys=");
3164 for_each_hist_key_field(i, hist_data) {
3165 key_field = hist_data->fields[i];
3168 strcat(cmd, key_field->field->name);
3172 /* Create the synthetic field variable specification */
3173 strcat(cmd, ":synthetic_");
3174 strcat(cmd, field_name);
3176 strcat(cmd, field_name);
3178 /* Use the same filter as the compatible histogram */
3179 saved_filter = find_trigger_filter(hist_data, file);
3181 strcat(cmd, " if ");
3182 strcat(cmd, saved_filter);
3185 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3186 if (!var_hist->cmd) {
3189 return ERR_PTR(-ENOMEM);
3192 /* Save the compatible histogram information */
3193 var_hist->hist_data = hist_data;
3195 /* Create the new histogram with our variable */
3196 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3200 kfree(var_hist->cmd);
3202 hist_err_event("onmatch: Couldn't create histogram for field: ",
3203 subsys_name, event_name, field_name);
3204 return ERR_PTR(ret);
3209 /* If we can't find the variable, something went wrong */
3210 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3211 event_name, field_name);
3212 if (IS_ERR_OR_NULL(event_var)) {
3213 kfree(var_hist->cmd);
3215 hist_err_event("onmatch: Couldn't find synthetic variable: ",
3216 subsys_name, event_name, field_name);
3217 return ERR_PTR(-EINVAL);
3220 n = target_hist_data->n_field_var_hists;
3221 target_hist_data->field_var_hists[n] = var_hist;
3222 target_hist_data->n_field_var_hists++;
3227 static struct hist_field *
3228 find_target_event_var(struct hist_trigger_data *hist_data,
3229 char *subsys_name, char *event_name, char *var_name)
3231 struct trace_event_file *file = hist_data->event_file;
3232 struct hist_field *hist_field = NULL;
3235 struct trace_event_call *call;
3240 call = file->event_call;
3242 if (strcmp(subsys_name, call->class->system) != 0)
3245 if (strcmp(event_name, trace_event_name(call)) != 0)
3249 hist_field = find_var_field(hist_data, var_name);
3254 static inline void __update_field_vars(struct tracing_map_elt *elt,
3255 struct ring_buffer_event *rbe,
3257 struct field_var **field_vars,
3258 unsigned int n_field_vars,
3259 unsigned int field_var_str_start)
3261 struct hist_elt_data *elt_data = elt->private_data;
3262 unsigned int i, j, var_idx;
3265 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3266 struct field_var *field_var = field_vars[i];
3267 struct hist_field *var = field_var->var;
3268 struct hist_field *val = field_var->val;
3270 var_val = val->fn(val, elt, rbe, rec);
3271 var_idx = var->var.idx;
3273 if (val->flags & HIST_FIELD_FL_STRING) {
3274 char *str = elt_data->field_var_str[j++];
3275 char *val_str = (char *)(uintptr_t)var_val;
3277 strscpy(str, val_str, STR_VAR_LEN_MAX);
3278 var_val = (u64)(uintptr_t)str;
3280 tracing_map_set_var(elt, var_idx, var_val);
3284 static void update_field_vars(struct hist_trigger_data *hist_data,
3285 struct tracing_map_elt *elt,
3286 struct ring_buffer_event *rbe,
3289 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3290 hist_data->n_field_vars, 0);
3293 static void update_max_vars(struct hist_trigger_data *hist_data,
3294 struct tracing_map_elt *elt,
3295 struct ring_buffer_event *rbe,
3298 __update_field_vars(elt, rbe, rec, hist_data->max_vars,
3299 hist_data->n_max_vars, hist_data->n_field_var_str);
3302 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3303 struct trace_event_file *file,
3304 char *name, int size, const char *type)
3306 struct hist_field *var;
3309 if (find_var(hist_data, file, name) && !hist_data->remove) {
3310 var = ERR_PTR(-EINVAL);
3314 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3316 var = ERR_PTR(-ENOMEM);
3320 idx = tracing_map_add_var(hist_data->map);
3323 var = ERR_PTR(-EINVAL);
3327 var->flags = HIST_FIELD_FL_VAR;
3329 var->var.hist_data = var->hist_data = hist_data;
3331 var->var.name = kstrdup(name, GFP_KERNEL);
3332 var->type = kstrdup(type, GFP_KERNEL);
3333 if (!var->var.name || !var->type) {
3334 kfree(var->var.name);
3337 var = ERR_PTR(-ENOMEM);
3343 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3344 struct trace_event_file *file,
3347 struct hist_field *val = NULL, *var = NULL;
3348 unsigned long flags = HIST_FIELD_FL_VAR;
3349 struct field_var *field_var;
3352 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3353 hist_err("Too many field variables defined: ", field_name);
3358 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3360 hist_err("Couldn't parse field variable: ", field_name);
3365 var = create_var(hist_data, file, field_name, val->size, val->type);
3367 hist_err("Couldn't create or find variable: ", field_name);
3373 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3381 field_var->var = var;
3382 field_var->val = val;
3386 field_var = ERR_PTR(ret);
3391 * create_target_field_var - Automatically create a variable for a field
3392 * @target_hist_data: The target hist trigger
3393 * @subsys_name: Optional subsystem name
3394 * @event_name: Optional event name
3395 * @var_name: The name of the field (and the resulting variable)
3397 * Hist trigger actions fetch data from variables, not directly from
3398 * events. However, for convenience, users are allowed to directly
3399 * specify an event field in an action, which will be automatically
3400 * converted into a variable on their behalf.
3402 * This function creates a field variable with the name var_name on
3403 * the hist trigger currently being defined on the target event. If
3404 * subsys_name and event_name are specified, this function simply
3405 * verifies that they do in fact match the target event subsystem and
3408 * Return: The variable created for the field.
3410 static struct field_var *
3411 create_target_field_var(struct hist_trigger_data *target_hist_data,
3412 char *subsys_name, char *event_name, char *var_name)
3414 struct trace_event_file *file = target_hist_data->event_file;
3417 struct trace_event_call *call;
3422 call = file->event_call;
3424 if (strcmp(subsys_name, call->class->system) != 0)
3427 if (strcmp(event_name, trace_event_name(call)) != 0)
3431 return create_field_var(target_hist_data, file, var_name);
3434 static void onmax_print(struct seq_file *m,
3435 struct hist_trigger_data *hist_data,
3436 struct tracing_map_elt *elt,
3437 struct action_data *data)
3439 unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
3441 seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
3443 for (i = 0; i < hist_data->n_max_vars; i++) {
3444 struct hist_field *save_val = hist_data->max_vars[i]->val;
3445 struct hist_field *save_var = hist_data->max_vars[i]->var;
3448 save_var_idx = save_var->var.idx;
3450 val = tracing_map_read_var(elt, save_var_idx);
3452 if (save_val->flags & HIST_FIELD_FL_STRING) {
3453 seq_printf(m, " %s: %-32s", save_var->var.name,
3454 (char *)(uintptr_t)(val));
3456 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3460 static void onmax_save(struct hist_trigger_data *hist_data,
3461 struct tracing_map_elt *elt, void *rec,
3462 struct ring_buffer_event *rbe,
3463 struct action_data *data, u64 *var_ref_vals)
3465 unsigned int max_idx = data->onmax.max_var->var.idx;
3466 unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
3468 u64 var_val, max_val;
3470 var_val = var_ref_vals[max_var_ref_idx];
3471 max_val = tracing_map_read_var(elt, max_idx);
3473 if (var_val <= max_val)
3476 tracing_map_set_var(elt, max_idx, var_val);
3478 update_max_vars(hist_data, elt, rbe, rec);
3481 static void onmax_destroy(struct action_data *data)
3485 destroy_hist_field(data->onmax.max_var, 0);
3486 destroy_hist_field(data->onmax.var, 0);
3488 kfree(data->onmax.var_str);
3489 kfree(data->onmax.fn_name);
3491 for (i = 0; i < data->n_params; i++)
3492 kfree(data->params[i]);
3497 static int onmax_create(struct hist_trigger_data *hist_data,
3498 struct action_data *data)
3500 struct trace_event_file *file = hist_data->event_file;
3501 struct hist_field *var_field, *ref_field, *max_var;
3502 unsigned int var_ref_idx = hist_data->n_var_refs;
3503 struct field_var *field_var;
3504 char *onmax_var_str, *param;
3508 onmax_var_str = data->onmax.var_str;
3509 if (onmax_var_str[0] != '$') {
3510 hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
3515 var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
3517 hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
3521 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3525 data->onmax.var = ref_field;
3527 data->fn = onmax_save;
3528 data->onmax.max_var_ref_idx = var_ref_idx;
3529 max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
3530 if (IS_ERR(max_var)) {
3531 hist_err("onmax: Couldn't create onmax variable: ", "max");
3532 ret = PTR_ERR(max_var);
3535 data->onmax.max_var = max_var;
3537 for (i = 0; i < data->n_params; i++) {
3538 param = kstrdup(data->params[i], GFP_KERNEL);
3544 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3545 if (IS_ERR(field_var)) {
3546 hist_err("onmax: Couldn't create field variable: ", param);
3547 ret = PTR_ERR(field_var);
3552 hist_data->max_vars[hist_data->n_max_vars++] = field_var;
3553 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3554 hist_data->n_max_var_str++;
3562 static int parse_action_params(char *params, struct action_data *data)
3564 char *param, *saved_param;
3568 if (data->n_params >= SYNTH_FIELDS_MAX)
3571 param = strsep(¶ms, ",");
3577 param = strstrip(param);
3578 if (strlen(param) < 2) {
3579 hist_err("Invalid action param: ", param);
3584 saved_param = kstrdup(param, GFP_KERNEL);
3590 data->params[data->n_params++] = saved_param;
3596 static struct action_data *onmax_parse(char *str)
3598 char *onmax_fn_name, *onmax_var_str;
3599 struct action_data *data;
3602 data = kzalloc(sizeof(*data), GFP_KERNEL);
3604 return ERR_PTR(-ENOMEM);
3606 onmax_var_str = strsep(&str, ")");
3607 if (!onmax_var_str || !str) {
3612 data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
3613 if (!data->onmax.var_str) {
3622 onmax_fn_name = strsep(&str, "(");
3623 if (!onmax_fn_name || !str)
3626 if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) {
3627 char *params = strsep(&str, ")");
3634 ret = parse_action_params(params, data);
3640 data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
3641 if (!data->onmax.fn_name) {
3648 onmax_destroy(data);
3649 data = ERR_PTR(ret);
3653 static void onmatch_destroy(struct action_data *data)
3657 mutex_lock(&synth_event_mutex);
3659 kfree(data->onmatch.match_event);
3660 kfree(data->onmatch.match_event_system);
3661 kfree(data->onmatch.synth_event_name);
3663 for (i = 0; i < data->n_params; i++)
3664 kfree(data->params[i]);
3666 if (data->onmatch.synth_event)
3667 data->onmatch.synth_event->ref--;
3671 mutex_unlock(&synth_event_mutex);
3674 static void destroy_field_var(struct field_var *field_var)
3679 destroy_hist_field(field_var->var, 0);
3680 destroy_hist_field(field_var->val, 0);
3685 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3689 for (i = 0; i < hist_data->n_field_vars; i++)
3690 destroy_field_var(hist_data->field_vars[i]);
3693 static void save_field_var(struct hist_trigger_data *hist_data,
3694 struct field_var *field_var)
3696 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3698 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3699 hist_data->n_field_var_str++;
3703 static void destroy_synth_var_refs(struct hist_trigger_data *hist_data)
3707 for (i = 0; i < hist_data->n_synth_var_refs; i++)
3708 destroy_hist_field(hist_data->synth_var_refs[i], 0);
3711 static void save_synth_var_ref(struct hist_trigger_data *hist_data,
3712 struct hist_field *var_ref)
3714 hist_data->synth_var_refs[hist_data->n_synth_var_refs++] = var_ref;
3717 static int check_synth_field(struct synth_event *event,
3718 struct hist_field *hist_field,
3719 unsigned int field_pos)
3721 struct synth_field *field;
3723 if (field_pos >= event->n_fields)
3726 field = event->fields[field_pos];
3728 if (strcmp(field->type, hist_field->type) != 0)
3734 static struct hist_field *
3735 onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
3736 char *system, char *event, char *var)
3738 struct hist_field *hist_field;
3740 var++; /* skip '$' */
3742 hist_field = find_target_event_var(hist_data, system, event, var);
3745 system = data->onmatch.match_event_system;
3746 event = data->onmatch.match_event;
3749 hist_field = find_event_var(hist_data, system, event, var);
3753 hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
3758 static struct hist_field *
3759 onmatch_create_field_var(struct hist_trigger_data *hist_data,
3760 struct action_data *data, char *system,
3761 char *event, char *var)
3763 struct hist_field *hist_field = NULL;
3764 struct field_var *field_var;
3767 * First try to create a field var on the target event (the
3768 * currently being defined). This will create a variable for
3769 * unqualified fields on the target event, or if qualified,
3770 * target fields that have qualified names matching the target.
3772 field_var = create_target_field_var(hist_data, system, event, var);
3774 if (field_var && !IS_ERR(field_var)) {
3775 save_field_var(hist_data, field_var);
3776 hist_field = field_var->var;
3780 * If no explicit system.event is specfied, default to
3781 * looking for fields on the onmatch(system.event.xxx)
3785 system = data->onmatch.match_event_system;
3786 event = data->onmatch.match_event;
3792 * At this point, we're looking at a field on another
3793 * event. Because we can't modify a hist trigger on
3794 * another event to add a variable for a field, we need
3795 * to create a new trigger on that event and create the
3796 * variable at the same time.
3798 hist_field = create_field_var_hist(hist_data, system, event, var);
3799 if (IS_ERR(hist_field))
3805 destroy_field_var(field_var);
3810 static int onmatch_create(struct hist_trigger_data *hist_data,
3811 struct trace_event_file *file,
3812 struct action_data *data)
3814 char *event_name, *param, *system = NULL;
3815 struct hist_field *hist_field, *var_ref;
3816 unsigned int i, var_ref_idx;
3817 unsigned int field_pos = 0;
3818 struct synth_event *event;
3821 mutex_lock(&synth_event_mutex);
3822 event = find_synth_event(data->onmatch.synth_event_name);
3824 hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
3825 mutex_unlock(&synth_event_mutex);
3829 mutex_unlock(&synth_event_mutex);
3831 var_ref_idx = hist_data->n_var_refs;
3833 for (i = 0; i < data->n_params; i++) {
3836 p = param = kstrdup(data->params[i], GFP_KERNEL);
3842 system = strsep(¶m, ".");
3844 param = (char *)system;
3845 system = event_name = NULL;
3847 event_name = strsep(¶m, ".");
3855 if (param[0] == '$')
3856 hist_field = onmatch_find_var(hist_data, data, system,
3859 hist_field = onmatch_create_field_var(hist_data, data,
3870 if (check_synth_field(event, hist_field, field_pos) == 0) {
3871 var_ref = create_var_ref(hist_data, hist_field,
3872 system, event_name);
3879 save_synth_var_ref(hist_data, var_ref);
3885 hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
3886 system, event_name, param);
3892 if (field_pos != event->n_fields) {
3893 hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
3898 data->fn = action_trace;
3899 data->onmatch.synth_event = event;
3900 data->onmatch.var_ref_idx = var_ref_idx;
3904 mutex_lock(&synth_event_mutex);
3906 mutex_unlock(&synth_event_mutex);
3911 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3913 char *match_event, *match_event_system;
3914 char *synth_event_name, *params;
3915 struct action_data *data;
3918 data = kzalloc(sizeof(*data), GFP_KERNEL);
3920 return ERR_PTR(-ENOMEM);
3922 match_event = strsep(&str, ")");
3923 if (!match_event || !str) {
3924 hist_err("onmatch: Missing closing paren: ", match_event);
3928 match_event_system = strsep(&match_event, ".");
3930 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
3934 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3935 hist_err_event("onmatch: Invalid subsystem or event name: ",
3936 match_event_system, match_event, NULL);
3940 data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
3941 if (!data->onmatch.match_event) {
3946 data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
3947 if (!data->onmatch.match_event_system) {
3954 hist_err("onmatch: Missing . after onmatch(): ", str);
3958 synth_event_name = strsep(&str, "(");
3959 if (!synth_event_name || !str) {
3960 hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
3964 data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
3965 if (!data->onmatch.synth_event_name) {
3970 params = strsep(&str, ")");
3971 if (!params || !str || (str && strlen(str))) {
3972 hist_err("onmatch: Missing closing paramlist paren: ", params);
3976 ret = parse_action_params(params, data);
3982 onmatch_destroy(data);
3983 data = ERR_PTR(ret);
3987 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3989 hist_data->fields[HITCOUNT_IDX] =
3990 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3991 if (!hist_data->fields[HITCOUNT_IDX])
3994 hist_data->n_vals++;
3995 hist_data->n_fields++;
3997 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4003 static int __create_val_field(struct hist_trigger_data *hist_data,
4004 unsigned int val_idx,
4005 struct trace_event_file *file,
4006 char *var_name, char *field_str,
4007 unsigned long flags)
4009 struct hist_field *hist_field;
4012 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4013 if (IS_ERR(hist_field)) {
4014 ret = PTR_ERR(hist_field);
4018 hist_data->fields[val_idx] = hist_field;
4020 ++hist_data->n_vals;
4021 ++hist_data->n_fields;
4023 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4029 static int create_val_field(struct hist_trigger_data *hist_data,
4030 unsigned int val_idx,
4031 struct trace_event_file *file,
4034 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4037 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4040 static int create_var_field(struct hist_trigger_data *hist_data,
4041 unsigned int val_idx,
4042 struct trace_event_file *file,
4043 char *var_name, char *expr_str)
4045 unsigned long flags = 0;
4047 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4050 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4051 hist_err("Variable already defined: ", var_name);
4055 flags |= HIST_FIELD_FL_VAR;
4056 hist_data->n_vars++;
4057 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4060 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4063 static int create_val_fields(struct hist_trigger_data *hist_data,
4064 struct trace_event_file *file)
4066 char *fields_str, *field_str;
4067 unsigned int i, j = 1;
4070 ret = create_hitcount_val(hist_data);
4074 fields_str = hist_data->attrs->vals_str;
4078 strsep(&fields_str, "=");
4082 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4083 j < TRACING_MAP_VALS_MAX; i++) {
4084 field_str = strsep(&fields_str, ",");
4088 if (strcmp(field_str, "hitcount") == 0)
4091 ret = create_val_field(hist_data, j++, file, field_str);
4096 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4102 static int create_key_field(struct hist_trigger_data *hist_data,
4103 unsigned int key_idx,
4104 unsigned int key_offset,
4105 struct trace_event_file *file,
4108 struct hist_field *hist_field = NULL;
4110 unsigned long flags = 0;
4111 unsigned int key_size;
4114 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4117 flags |= HIST_FIELD_FL_KEY;
4119 if (strcmp(field_str, "stacktrace") == 0) {
4120 flags |= HIST_FIELD_FL_STACKTRACE;
4121 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4122 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4124 hist_field = parse_expr(hist_data, file, field_str, flags,
4126 if (IS_ERR(hist_field)) {
4127 ret = PTR_ERR(hist_field);
4131 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
4132 hist_err("Using variable references as keys not supported: ", field_str);
4133 destroy_hist_field(hist_field, 0);
4138 key_size = hist_field->size;
4141 hist_data->fields[key_idx] = hist_field;
4143 key_size = ALIGN(key_size, sizeof(u64));
4144 hist_data->fields[key_idx]->size = key_size;
4145 hist_data->fields[key_idx]->offset = key_offset;
4147 hist_data->key_size += key_size;
4149 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4154 hist_data->n_keys++;
4155 hist_data->n_fields++;
4157 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4165 static int create_key_fields(struct hist_trigger_data *hist_data,
4166 struct trace_event_file *file)
4168 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4169 char *fields_str, *field_str;
4172 fields_str = hist_data->attrs->keys_str;
4176 strsep(&fields_str, "=");
4180 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4181 field_str = strsep(&fields_str, ",");
4184 ret = create_key_field(hist_data, i, key_offset,
4199 static int create_var_fields(struct hist_trigger_data *hist_data,
4200 struct trace_event_file *file)
4202 unsigned int i, j = hist_data->n_vals;
4205 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4207 for (i = 0; i < n_vars; i++) {
4208 char *var_name = hist_data->attrs->var_defs.name[i];
4209 char *expr = hist_data->attrs->var_defs.expr[i];
4211 ret = create_var_field(hist_data, j++, file, var_name, expr);
4219 static void free_var_defs(struct hist_trigger_data *hist_data)
4223 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4224 kfree(hist_data->attrs->var_defs.name[i]);
4225 kfree(hist_data->attrs->var_defs.expr[i]);
4228 hist_data->attrs->var_defs.n_vars = 0;
4231 static int parse_var_defs(struct hist_trigger_data *hist_data)
4233 char *s, *str, *var_name, *field_str;
4234 unsigned int i, j, n_vars = 0;
4237 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4238 str = hist_data->attrs->assignment_str[i];
4239 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4240 field_str = strsep(&str, ",");
4244 var_name = strsep(&field_str, "=");
4245 if (!var_name || !field_str) {
4246 hist_err("Malformed assignment: ", var_name);
4251 if (n_vars == TRACING_MAP_VARS_MAX) {
4252 hist_err("Too many variables defined: ", var_name);
4257 s = kstrdup(var_name, GFP_KERNEL);
4262 hist_data->attrs->var_defs.name[n_vars] = s;
4264 s = kstrdup(field_str, GFP_KERNEL);
4269 hist_data->attrs->var_defs.expr[n_vars++] = s;
4271 hist_data->attrs->var_defs.n_vars = n_vars;
4277 free_var_defs(hist_data);
4282 static int create_hist_fields(struct hist_trigger_data *hist_data,
4283 struct trace_event_file *file)
4287 ret = parse_var_defs(hist_data);
4291 ret = create_val_fields(hist_data, file);
4295 ret = create_var_fields(hist_data, file);
4299 ret = create_key_fields(hist_data, file);
4303 free_var_defs(hist_data);
4308 static int is_descending(const char *str)
4313 if (strcmp(str, "descending") == 0)
4316 if (strcmp(str, "ascending") == 0)
4322 static int create_sort_keys(struct hist_trigger_data *hist_data)
4324 char *fields_str = hist_data->attrs->sort_key_str;
4325 struct tracing_map_sort_key *sort_key;
4326 int descending, ret = 0;
4327 unsigned int i, j, k;
4329 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4334 strsep(&fields_str, "=");
4340 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4341 struct hist_field *hist_field;
4342 char *field_str, *field_name;
4343 const char *test_name;
4345 sort_key = &hist_data->sort_keys[i];
4347 field_str = strsep(&fields_str, ",");
4354 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4359 field_name = strsep(&field_str, ".");
4365 if (strcmp(field_name, "hitcount") == 0) {
4366 descending = is_descending(field_str);
4367 if (descending < 0) {
4371 sort_key->descending = descending;
4375 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4378 hist_field = hist_data->fields[j];
4379 if (hist_field->flags & HIST_FIELD_FL_VAR)
4384 test_name = hist_field_name(hist_field, 0);
4386 if (strcmp(field_name, test_name) == 0) {
4387 sort_key->field_idx = idx;
4388 descending = is_descending(field_str);
4389 if (descending < 0) {
4393 sort_key->descending = descending;
4397 if (j == hist_data->n_fields) {
4403 hist_data->n_sort_keys = i;
4408 static void destroy_actions(struct hist_trigger_data *hist_data)
4412 for (i = 0; i < hist_data->n_actions; i++) {
4413 struct action_data *data = hist_data->actions[i];
4415 if (data->fn == action_trace)
4416 onmatch_destroy(data);
4417 else if (data->fn == onmax_save)
4418 onmax_destroy(data);
4424 static int parse_actions(struct hist_trigger_data *hist_data)
4426 struct trace_array *tr = hist_data->event_file->tr;
4427 struct action_data *data;
4432 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4433 str = hist_data->attrs->action_str[i];
4435 if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) {
4436 char *action_str = str + strlen("onmatch(");
4438 data = onmatch_parse(tr, action_str);
4440 ret = PTR_ERR(data);
4443 data->fn = action_trace;
4444 } else if (strncmp(str, "onmax(", strlen("onmax(")) == 0) {
4445 char *action_str = str + strlen("onmax(");
4447 data = onmax_parse(action_str);
4449 ret = PTR_ERR(data);
4452 data->fn = onmax_save;
4458 hist_data->actions[hist_data->n_actions++] = data;
4464 static int create_actions(struct hist_trigger_data *hist_data,
4465 struct trace_event_file *file)
4467 struct action_data *data;
4471 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4472 data = hist_data->actions[i];
4474 if (data->fn == action_trace) {
4475 ret = onmatch_create(hist_data, file, data);
4478 } else if (data->fn == onmax_save) {
4479 ret = onmax_create(hist_data, data);
4488 static void print_actions(struct seq_file *m,
4489 struct hist_trigger_data *hist_data,
4490 struct tracing_map_elt *elt)
4494 for (i = 0; i < hist_data->n_actions; i++) {
4495 struct action_data *data = hist_data->actions[i];
4497 if (data->fn == onmax_save)
4498 onmax_print(m, hist_data, elt, data);
4502 static void print_onmax_spec(struct seq_file *m,
4503 struct hist_trigger_data *hist_data,
4504 struct action_data *data)
4508 seq_puts(m, ":onmax(");
4509 seq_printf(m, "%s", data->onmax.var_str);
4510 seq_printf(m, ").%s(", data->onmax.fn_name);
4512 for (i = 0; i < hist_data->n_max_vars; i++) {
4513 seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
4514 if (i < hist_data->n_max_vars - 1)
4520 static void print_onmatch_spec(struct seq_file *m,
4521 struct hist_trigger_data *hist_data,
4522 struct action_data *data)
4526 seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
4527 data->onmatch.match_event);
4529 seq_printf(m, "%s(", data->onmatch.synth_event->name);
4531 for (i = 0; i < data->n_params; i++) {
4534 seq_printf(m, "%s", data->params[i]);
4540 static bool actions_match(struct hist_trigger_data *hist_data,
4541 struct hist_trigger_data *hist_data_test)
4545 if (hist_data->n_actions != hist_data_test->n_actions)
4548 for (i = 0; i < hist_data->n_actions; i++) {
4549 struct action_data *data = hist_data->actions[i];
4550 struct action_data *data_test = hist_data_test->actions[i];
4552 if (data->fn != data_test->fn)
4555 if (data->n_params != data_test->n_params)
4558 for (j = 0; j < data->n_params; j++) {
4559 if (strcmp(data->params[j], data_test->params[j]) != 0)
4563 if (data->fn == action_trace) {
4564 if (strcmp(data->onmatch.synth_event_name,
4565 data_test->onmatch.synth_event_name) != 0)
4567 if (strcmp(data->onmatch.match_event_system,
4568 data_test->onmatch.match_event_system) != 0)
4570 if (strcmp(data->onmatch.match_event,
4571 data_test->onmatch.match_event) != 0)
4573 } else if (data->fn == onmax_save) {
4574 if (strcmp(data->onmax.var_str,
4575 data_test->onmax.var_str) != 0)
4577 if (strcmp(data->onmax.fn_name,
4578 data_test->onmax.fn_name) != 0)
4587 static void print_actions_spec(struct seq_file *m,
4588 struct hist_trigger_data *hist_data)
4592 for (i = 0; i < hist_data->n_actions; i++) {
4593 struct action_data *data = hist_data->actions[i];
4595 if (data->fn == action_trace)
4596 print_onmatch_spec(m, hist_data, data);
4597 else if (data->fn == onmax_save)
4598 print_onmax_spec(m, hist_data, data);
4602 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4606 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4607 kfree(hist_data->field_var_hists[i]->cmd);
4608 kfree(hist_data->field_var_hists[i]);
4612 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4617 destroy_hist_trigger_attrs(hist_data->attrs);
4618 destroy_hist_fields(hist_data);
4619 tracing_map_destroy(hist_data->map);
4621 destroy_actions(hist_data);
4622 destroy_field_vars(hist_data);
4623 destroy_field_var_hists(hist_data);
4624 destroy_synth_var_refs(hist_data);
4629 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4631 struct tracing_map *map = hist_data->map;
4632 struct ftrace_event_field *field;
4633 struct hist_field *hist_field;
4636 for_each_hist_field(i, hist_data) {
4637 hist_field = hist_data->fields[i];
4638 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4639 tracing_map_cmp_fn_t cmp_fn;
4641 field = hist_field->field;
4643 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4644 cmp_fn = tracing_map_cmp_none;
4646 cmp_fn = tracing_map_cmp_num(hist_field->size,
4647 hist_field->is_signed);
4648 else if (is_string_field(field))
4649 cmp_fn = tracing_map_cmp_string;
4651 cmp_fn = tracing_map_cmp_num(field->size,
4653 idx = tracing_map_add_key_field(map,
4656 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4657 idx = tracing_map_add_sum_field(map);
4662 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4663 idx = tracing_map_add_var(map);
4666 hist_field->var.idx = idx;
4667 hist_field->var.hist_data = hist_data;
4674 static struct hist_trigger_data *
4675 create_hist_data(unsigned int map_bits,
4676 struct hist_trigger_attrs *attrs,
4677 struct trace_event_file *file,
4680 const struct tracing_map_ops *map_ops = NULL;
4681 struct hist_trigger_data *hist_data;
4684 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4686 return ERR_PTR(-ENOMEM);
4688 hist_data->attrs = attrs;
4689 hist_data->remove = remove;
4690 hist_data->event_file = file;
4692 ret = parse_actions(hist_data);
4696 ret = create_hist_fields(hist_data, file);
4700 ret = create_sort_keys(hist_data);
4704 map_ops = &hist_trigger_elt_data_ops;
4706 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4707 map_ops, hist_data);
4708 if (IS_ERR(hist_data->map)) {
4709 ret = PTR_ERR(hist_data->map);
4710 hist_data->map = NULL;
4714 ret = create_tracing_map_fields(hist_data);
4720 hist_data->attrs = NULL;
4722 destroy_hist_data(hist_data);
4724 hist_data = ERR_PTR(ret);
4729 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4730 struct tracing_map_elt *elt, void *rec,
4731 struct ring_buffer_event *rbe,
4734 struct hist_elt_data *elt_data;
4735 struct hist_field *hist_field;
4736 unsigned int i, var_idx;
4739 elt_data = elt->private_data;
4740 elt_data->var_ref_vals = var_ref_vals;
4742 for_each_hist_val_field(i, hist_data) {
4743 hist_field = hist_data->fields[i];
4744 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4745 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4746 var_idx = hist_field->var.idx;
4747 tracing_map_set_var(elt, var_idx, hist_val);
4750 tracing_map_update_sum(elt, i, hist_val);
4753 for_each_hist_key_field(i, hist_data) {
4754 hist_field = hist_data->fields[i];
4755 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4756 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4757 var_idx = hist_field->var.idx;
4758 tracing_map_set_var(elt, var_idx, hist_val);
4762 update_field_vars(hist_data, elt, rbe, rec);
4765 static inline void add_to_key(char *compound_key, void *key,
4766 struct hist_field *key_field, void *rec)
4768 size_t size = key_field->size;
4770 if (key_field->flags & HIST_FIELD_FL_STRING) {
4771 struct ftrace_event_field *field;
4773 field = key_field->field;
4774 if (field->filter_type == FILTER_DYN_STRING)
4775 size = *(u32 *)(rec + field->offset) >> 16;
4776 else if (field->filter_type == FILTER_STATIC_STRING)
4779 /* ensure NULL-termination */
4780 if (size > key_field->size - 1)
4781 size = key_field->size - 1;
4783 strncpy(compound_key + key_field->offset, (char *)key, size);
4785 memcpy(compound_key + key_field->offset, key, size);
4789 hist_trigger_actions(struct hist_trigger_data *hist_data,
4790 struct tracing_map_elt *elt, void *rec,
4791 struct ring_buffer_event *rbe, u64 *var_ref_vals)
4793 struct action_data *data;
4796 for (i = 0; i < hist_data->n_actions; i++) {
4797 data = hist_data->actions[i];
4798 data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
4802 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4803 struct ring_buffer_event *rbe)
4805 struct hist_trigger_data *hist_data = data->private_data;
4806 bool use_compound_key = (hist_data->n_keys > 1);
4807 unsigned long entries[HIST_STACKTRACE_DEPTH];
4808 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4809 char compound_key[HIST_KEY_SIZE_MAX];
4810 struct tracing_map_elt *elt = NULL;
4811 struct stack_trace stacktrace;
4812 struct hist_field *key_field;
4817 memset(compound_key, 0, hist_data->key_size);
4819 for_each_hist_key_field(i, hist_data) {
4820 key_field = hist_data->fields[i];
4822 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4823 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
4824 stacktrace.entries = entries;
4825 stacktrace.nr_entries = 0;
4826 stacktrace.skip = HIST_STACKTRACE_SKIP;
4828 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
4829 save_stack_trace(&stacktrace);
4833 field_contents = key_field->fn(key_field, elt, rbe, rec);
4834 if (key_field->flags & HIST_FIELD_FL_STRING) {
4835 key = (void *)(unsigned long)field_contents;
4836 use_compound_key = true;
4838 key = (void *)&field_contents;
4841 if (use_compound_key)
4842 add_to_key(compound_key, key, key_field, rec);
4845 if (use_compound_key)
4848 if (hist_data->n_var_refs &&
4849 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4852 elt = tracing_map_insert(hist_data->map, key);
4856 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4858 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4859 hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
4862 static void hist_trigger_stacktrace_print(struct seq_file *m,
4863 unsigned long *stacktrace_entries,
4864 unsigned int max_entries)
4866 char str[KSYM_SYMBOL_LEN];
4867 unsigned int spaces = 8;
4870 for (i = 0; i < max_entries; i++) {
4871 if (stacktrace_entries[i] == ULONG_MAX)
4874 seq_printf(m, "%*c", 1 + spaces, ' ');
4875 sprint_symbol(str, stacktrace_entries[i]);
4876 seq_printf(m, "%s\n", str);
4881 hist_trigger_entry_print(struct seq_file *m,
4882 struct hist_trigger_data *hist_data, void *key,
4883 struct tracing_map_elt *elt)
4885 struct hist_field *key_field;
4886 char str[KSYM_SYMBOL_LEN];
4887 bool multiline = false;
4888 const char *field_name;
4894 for_each_hist_key_field(i, hist_data) {
4895 key_field = hist_data->fields[i];
4897 if (i > hist_data->n_vals)
4900 field_name = hist_field_name(key_field, 0);
4902 if (key_field->flags & HIST_FIELD_FL_HEX) {
4903 uval = *(u64 *)(key + key_field->offset);
4904 seq_printf(m, "%s: %llx", field_name, uval);
4905 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4906 uval = *(u64 *)(key + key_field->offset);
4907 sprint_symbol_no_offset(str, uval);
4908 seq_printf(m, "%s: [%llx] %-45s", field_name,
4910 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4911 uval = *(u64 *)(key + key_field->offset);
4912 sprint_symbol(str, uval);
4913 seq_printf(m, "%s: [%llx] %-55s", field_name,
4915 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4916 struct hist_elt_data *elt_data = elt->private_data;
4919 if (WARN_ON_ONCE(!elt_data))
4922 comm = elt_data->comm;
4924 uval = *(u64 *)(key + key_field->offset);
4925 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4927 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4928 const char *syscall_name;
4930 uval = *(u64 *)(key + key_field->offset);
4931 syscall_name = get_syscall_name(uval);
4933 syscall_name = "unknown_syscall";
4935 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4936 syscall_name, uval);
4937 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4938 seq_puts(m, "stacktrace:\n");
4939 hist_trigger_stacktrace_print(m,
4940 key + key_field->offset,
4941 HIST_STACKTRACE_DEPTH);
4943 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4944 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4945 *(u64 *)(key + key_field->offset));
4946 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4947 seq_printf(m, "%s: %-50s", field_name,
4948 (char *)(key + key_field->offset));
4950 uval = *(u64 *)(key + key_field->offset);
4951 seq_printf(m, "%s: %10llu", field_name, uval);
4960 seq_printf(m, " hitcount: %10llu",
4961 tracing_map_read_sum(elt, HITCOUNT_IDX));
4963 for (i = 1; i < hist_data->n_vals; i++) {
4964 field_name = hist_field_name(hist_data->fields[i], 0);
4966 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4967 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4970 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4971 seq_printf(m, " %s: %10llx", field_name,
4972 tracing_map_read_sum(elt, i));
4974 seq_printf(m, " %s: %10llu", field_name,
4975 tracing_map_read_sum(elt, i));
4979 print_actions(m, hist_data, elt);
4984 static int print_entries(struct seq_file *m,
4985 struct hist_trigger_data *hist_data)
4987 struct tracing_map_sort_entry **sort_entries = NULL;
4988 struct tracing_map *map = hist_data->map;
4991 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4992 hist_data->n_sort_keys,
4997 for (i = 0; i < n_entries; i++)
4998 hist_trigger_entry_print(m, hist_data,
4999 sort_entries[i]->key,
5000 sort_entries[i]->elt);
5002 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5007 static void hist_trigger_show(struct seq_file *m,
5008 struct event_trigger_data *data, int n)
5010 struct hist_trigger_data *hist_data;
5014 seq_puts(m, "\n\n");
5016 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5017 data->ops->print(m, data->ops, data);
5018 seq_puts(m, "#\n\n");
5020 hist_data = data->private_data;
5021 n_entries = print_entries(m, hist_data);
5025 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5026 (u64)atomic64_read(&hist_data->map->hits),
5027 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5030 static int hist_show(struct seq_file *m, void *v)
5032 struct event_trigger_data *data;
5033 struct trace_event_file *event_file;
5036 mutex_lock(&event_mutex);
5038 event_file = event_file_data(m->private);
5039 if (unlikely(!event_file)) {
5044 list_for_each_entry(data, &event_file->triggers, list) {
5045 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5046 hist_trigger_show(m, data, n++);
5049 if (have_hist_err()) {
5050 seq_printf(m, "\nERROR: %s\n", hist_err_str);
5051 seq_printf(m, " Last command: %s\n", last_hist_cmd);
5055 mutex_unlock(&event_mutex);
5060 static int event_hist_open(struct inode *inode, struct file *file)
5062 return single_open(file, hist_show, file);
5065 const struct file_operations event_hist_fops = {
5066 .open = event_hist_open,
5068 .llseek = seq_lseek,
5069 .release = single_release,
5072 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5074 const char *field_name = hist_field_name(hist_field, 0);
5076 if (hist_field->var.name)
5077 seq_printf(m, "%s=", hist_field->var.name);
5079 if (hist_field->flags & HIST_FIELD_FL_CPU)
5080 seq_puts(m, "common_cpu");
5081 else if (field_name) {
5082 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5083 hist_field->flags & HIST_FIELD_FL_ALIAS)
5085 seq_printf(m, "%s", field_name);
5086 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5087 seq_puts(m, "common_timestamp");
5089 if (hist_field->flags) {
5090 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5091 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5092 const char *flags = get_hist_field_flags(hist_field);
5095 seq_printf(m, ".%s", flags);
5100 static int event_hist_trigger_print(struct seq_file *m,
5101 struct event_trigger_ops *ops,
5102 struct event_trigger_data *data)
5104 struct hist_trigger_data *hist_data = data->private_data;
5105 struct hist_field *field;
5106 bool have_var = false;
5109 seq_puts(m, "hist:");
5112 seq_printf(m, "%s:", data->name);
5114 seq_puts(m, "keys=");
5116 for_each_hist_key_field(i, hist_data) {
5117 field = hist_data->fields[i];
5119 if (i > hist_data->n_vals)
5122 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5123 seq_puts(m, "stacktrace");
5125 hist_field_print(m, field);
5128 seq_puts(m, ":vals=");
5130 for_each_hist_val_field(i, hist_data) {
5131 field = hist_data->fields[i];
5132 if (field->flags & HIST_FIELD_FL_VAR) {
5137 if (i == HITCOUNT_IDX)
5138 seq_puts(m, "hitcount");
5141 hist_field_print(m, field);
5150 for_each_hist_val_field(i, hist_data) {
5151 field = hist_data->fields[i];
5153 if (field->flags & HIST_FIELD_FL_VAR) {
5156 hist_field_print(m, field);
5161 seq_puts(m, ":sort=");
5163 for (i = 0; i < hist_data->n_sort_keys; i++) {
5164 struct tracing_map_sort_key *sort_key;
5165 unsigned int idx, first_key_idx;
5168 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5170 sort_key = &hist_data->sort_keys[i];
5171 idx = sort_key->field_idx;
5173 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5179 if (idx == HITCOUNT_IDX)
5180 seq_puts(m, "hitcount");
5182 if (idx >= first_key_idx)
5183 idx += hist_data->n_vars;
5184 hist_field_print(m, hist_data->fields[idx]);
5187 if (sort_key->descending)
5188 seq_puts(m, ".descending");
5190 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5191 if (hist_data->enable_timestamps)
5192 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5194 print_actions_spec(m, hist_data);
5196 if (data->filter_str)
5197 seq_printf(m, " if %s", data->filter_str);
5200 seq_puts(m, " [paused]");
5202 seq_puts(m, " [active]");
5209 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5210 struct event_trigger_data *data)
5212 struct hist_trigger_data *hist_data = data->private_data;
5214 if (!data->ref && hist_data->attrs->name)
5215 save_named_trigger(hist_data->attrs->name, data);
5222 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5224 struct trace_event_file *file;
5229 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5230 file = hist_data->field_var_hists[i]->hist_data->event_file;
5231 cmd = hist_data->field_var_hists[i]->cmd;
5232 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5233 "!hist", "hist", cmd);
5237 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5238 struct event_trigger_data *data)
5240 struct hist_trigger_data *hist_data = data->private_data;
5242 if (WARN_ON_ONCE(data->ref <= 0))
5248 del_named_trigger(data);
5250 trigger_data_free(data);
5252 remove_hist_vars(hist_data);
5254 unregister_field_var_hists(hist_data);
5256 destroy_hist_data(hist_data);
5260 static struct event_trigger_ops event_hist_trigger_ops = {
5261 .func = event_hist_trigger,
5262 .print = event_hist_trigger_print,
5263 .init = event_hist_trigger_init,
5264 .free = event_hist_trigger_free,
5267 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5268 struct event_trigger_data *data)
5272 save_named_trigger(data->named_data->name, data);
5274 event_hist_trigger_init(ops, data->named_data);
5279 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5280 struct event_trigger_data *data)
5282 if (WARN_ON_ONCE(data->ref <= 0))
5285 event_hist_trigger_free(ops, data->named_data);
5289 del_named_trigger(data);
5290 trigger_data_free(data);
5294 static struct event_trigger_ops event_hist_trigger_named_ops = {
5295 .func = event_hist_trigger,
5296 .print = event_hist_trigger_print,
5297 .init = event_hist_trigger_named_init,
5298 .free = event_hist_trigger_named_free,
5301 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5304 return &event_hist_trigger_ops;
5307 static void hist_clear(struct event_trigger_data *data)
5309 struct hist_trigger_data *hist_data = data->private_data;
5312 pause_named_trigger(data);
5314 tracepoint_synchronize_unregister();
5316 tracing_map_clear(hist_data->map);
5319 unpause_named_trigger(data);
5322 static bool compatible_field(struct ftrace_event_field *field,
5323 struct ftrace_event_field *test_field)
5325 if (field == test_field)
5327 if (field == NULL || test_field == NULL)
5329 if (strcmp(field->name, test_field->name) != 0)
5331 if (strcmp(field->type, test_field->type) != 0)
5333 if (field->size != test_field->size)
5335 if (field->is_signed != test_field->is_signed)
5341 static bool hist_trigger_match(struct event_trigger_data *data,
5342 struct event_trigger_data *data_test,
5343 struct event_trigger_data *named_data,
5346 struct tracing_map_sort_key *sort_key, *sort_key_test;
5347 struct hist_trigger_data *hist_data, *hist_data_test;
5348 struct hist_field *key_field, *key_field_test;
5351 if (named_data && (named_data != data_test) &&
5352 (named_data != data_test->named_data))
5355 if (!named_data && is_named_trigger(data_test))
5358 hist_data = data->private_data;
5359 hist_data_test = data_test->private_data;
5361 if (hist_data->n_vals != hist_data_test->n_vals ||
5362 hist_data->n_fields != hist_data_test->n_fields ||
5363 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5366 if (!ignore_filter) {
5367 if ((data->filter_str && !data_test->filter_str) ||
5368 (!data->filter_str && data_test->filter_str))
5372 for_each_hist_field(i, hist_data) {
5373 key_field = hist_data->fields[i];
5374 key_field_test = hist_data_test->fields[i];
5376 if (key_field->flags != key_field_test->flags)
5378 if (!compatible_field(key_field->field, key_field_test->field))
5380 if (key_field->offset != key_field_test->offset)
5382 if (key_field->size != key_field_test->size)
5384 if (key_field->is_signed != key_field_test->is_signed)
5386 if (!!key_field->var.name != !!key_field_test->var.name)
5388 if (key_field->var.name &&
5389 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5393 for (i = 0; i < hist_data->n_sort_keys; i++) {
5394 sort_key = &hist_data->sort_keys[i];
5395 sort_key_test = &hist_data_test->sort_keys[i];
5397 if (sort_key->field_idx != sort_key_test->field_idx ||
5398 sort_key->descending != sort_key_test->descending)
5402 if (!ignore_filter && data->filter_str &&
5403 (strcmp(data->filter_str, data_test->filter_str) != 0))
5406 if (!actions_match(hist_data, hist_data_test))
5412 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5413 struct event_trigger_data *data,
5414 struct trace_event_file *file)
5416 struct hist_trigger_data *hist_data = data->private_data;
5417 struct event_trigger_data *test, *named_data = NULL;
5420 if (hist_data->attrs->name) {
5421 named_data = find_named_trigger(hist_data->attrs->name);
5423 if (!hist_trigger_match(data, named_data, named_data,
5425 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
5432 if (hist_data->attrs->name && !named_data)
5435 lockdep_assert_held(&event_mutex);
5437 list_for_each_entry(test, &file->triggers, list) {
5438 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5439 if (!hist_trigger_match(data, test, named_data, false))
5441 if (hist_data->attrs->pause)
5442 test->paused = true;
5443 else if (hist_data->attrs->cont)
5444 test->paused = false;
5445 else if (hist_data->attrs->clear)
5448 hist_err("Hist trigger already exists", NULL);
5455 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5456 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
5461 if (hist_data->attrs->pause)
5462 data->paused = true;
5465 data->private_data = named_data->private_data;
5466 set_named_trigger_data(data, named_data);
5467 data->ops = &event_hist_trigger_named_ops;
5470 if (data->ops->init) {
5471 ret = data->ops->init(data->ops, data);
5476 if (hist_data->enable_timestamps) {
5477 char *clock = hist_data->attrs->clock;
5479 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5481 hist_err("Couldn't set trace_clock: ", clock);
5485 tracing_set_time_stamp_abs(file->tr, true);
5489 destroy_hist_data(hist_data);
5496 static int hist_trigger_enable(struct event_trigger_data *data,
5497 struct trace_event_file *file)
5501 list_add_tail_rcu(&data->list, &file->triggers);
5503 update_cond_flag(file);
5505 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5506 list_del_rcu(&data->list);
5507 update_cond_flag(file);
5514 static bool have_hist_trigger_match(struct event_trigger_data *data,
5515 struct trace_event_file *file)
5517 struct hist_trigger_data *hist_data = data->private_data;
5518 struct event_trigger_data *test, *named_data = NULL;
5521 lockdep_assert_held(&event_mutex);
5523 if (hist_data->attrs->name)
5524 named_data = find_named_trigger(hist_data->attrs->name);
5526 list_for_each_entry(test, &file->triggers, list) {
5527 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5528 if (hist_trigger_match(data, test, named_data, false)) {
5538 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5539 struct trace_event_file *file)
5541 struct hist_trigger_data *hist_data = data->private_data;
5542 struct event_trigger_data *test, *named_data = NULL;
5544 lockdep_assert_held(&event_mutex);
5546 if (hist_data->attrs->name)
5547 named_data = find_named_trigger(hist_data->attrs->name);
5549 list_for_each_entry(test, &file->triggers, list) {
5550 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5551 if (!hist_trigger_match(data, test, named_data, false))
5553 hist_data = test->private_data;
5554 if (check_var_refs(hist_data))
5563 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5564 struct event_trigger_data *data,
5565 struct trace_event_file *file)
5567 struct hist_trigger_data *hist_data = data->private_data;
5568 struct event_trigger_data *test, *named_data = NULL;
5569 bool unregistered = false;
5571 lockdep_assert_held(&event_mutex);
5573 if (hist_data->attrs->name)
5574 named_data = find_named_trigger(hist_data->attrs->name);
5576 list_for_each_entry(test, &file->triggers, list) {
5577 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5578 if (!hist_trigger_match(data, test, named_data, false))
5580 unregistered = true;
5581 list_del_rcu(&test->list);
5582 trace_event_trigger_enable_disable(file, 0);
5583 update_cond_flag(file);
5588 if (unregistered && test->ops->free)
5589 test->ops->free(test->ops, test);
5591 if (hist_data->enable_timestamps) {
5592 if (!hist_data->remove || unregistered)
5593 tracing_set_time_stamp_abs(file->tr, false);
5597 static bool hist_file_check_refs(struct trace_event_file *file)
5599 struct hist_trigger_data *hist_data;
5600 struct event_trigger_data *test;
5602 lockdep_assert_held(&event_mutex);
5604 list_for_each_entry(test, &file->triggers, list) {
5605 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5606 hist_data = test->private_data;
5607 if (check_var_refs(hist_data))
5615 static void hist_unreg_all(struct trace_event_file *file)
5617 struct event_trigger_data *test, *n;
5618 struct hist_trigger_data *hist_data;
5619 struct synth_event *se;
5620 const char *se_name;
5622 if (hist_file_check_refs(file))
5625 list_for_each_entry_safe(test, n, &file->triggers, list) {
5626 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5627 hist_data = test->private_data;
5628 list_del_rcu(&test->list);
5629 trace_event_trigger_enable_disable(file, 0);
5631 mutex_lock(&synth_event_mutex);
5632 se_name = trace_event_name(file->event_call);
5633 se = find_synth_event(se_name);
5636 mutex_unlock(&synth_event_mutex);
5638 update_cond_flag(file);
5639 if (hist_data->enable_timestamps)
5640 tracing_set_time_stamp_abs(file->tr, false);
5641 if (test->ops->free)
5642 test->ops->free(test->ops, test);
5647 static int event_hist_trigger_func(struct event_command *cmd_ops,
5648 struct trace_event_file *file,
5649 char *glob, char *cmd, char *param)
5651 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5652 struct event_trigger_data *trigger_data;
5653 struct hist_trigger_attrs *attrs;
5654 struct event_trigger_ops *trigger_ops;
5655 struct hist_trigger_data *hist_data;
5656 struct synth_event *se;
5657 const char *se_name;
5658 bool remove = false;
5662 if (glob && strlen(glob)) {
5663 last_cmd_set(param);
5674 * separate the trigger from the filter (k:v [if filter])
5675 * allowing for whitespace in the trigger
5677 p = trigger = param;
5679 p = strstr(p, "if");
5684 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5688 if (p >= param + strlen(param) - strlen("if") - 1)
5690 if (*(p + strlen("if")) != ' ' && *(p + strlen("if")) != '\t') {
5701 param = strstrip(p);
5702 trigger = strstrip(trigger);
5705 attrs = parse_hist_trigger_attrs(trigger);
5707 return PTR_ERR(attrs);
5709 if (attrs->map_bits)
5710 hist_trigger_bits = attrs->map_bits;
5712 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5713 if (IS_ERR(hist_data)) {
5714 destroy_hist_trigger_attrs(attrs);
5715 return PTR_ERR(hist_data);
5718 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5720 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5721 if (!trigger_data) {
5726 trigger_data->count = -1;
5727 trigger_data->ops = trigger_ops;
5728 trigger_data->cmd_ops = cmd_ops;
5730 INIT_LIST_HEAD(&trigger_data->list);
5731 RCU_INIT_POINTER(trigger_data->filter, NULL);
5733 trigger_data->private_data = hist_data;
5735 /* if param is non-empty, it's supposed to be a filter */
5736 if (param && cmd_ops->set_filter) {
5737 ret = cmd_ops->set_filter(param, trigger_data, file);
5743 if (!have_hist_trigger_match(trigger_data, file))
5746 if (hist_trigger_check_refs(trigger_data, file)) {
5751 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5753 mutex_lock(&synth_event_mutex);
5754 se_name = trace_event_name(file->event_call);
5755 se = find_synth_event(se_name);
5758 mutex_unlock(&synth_event_mutex);
5764 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5766 * The above returns on success the # of triggers registered,
5767 * but if it didn't register any it returns zero. Consider no
5768 * triggers registered a failure too.
5771 if (!(attrs->pause || attrs->cont || attrs->clear))
5777 if (get_named_trigger_data(trigger_data))
5780 if (has_hist_vars(hist_data))
5781 save_hist_vars(hist_data);
5783 ret = create_actions(hist_data, file);
5787 ret = tracing_map_init(hist_data->map);
5791 ret = hist_trigger_enable(trigger_data, file);
5795 mutex_lock(&synth_event_mutex);
5796 se_name = trace_event_name(file->event_call);
5797 se = find_synth_event(se_name);
5800 mutex_unlock(&synth_event_mutex);
5802 /* Just return zero, not the number of registered triggers */
5810 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5812 if (cmd_ops->set_filter)
5813 cmd_ops->set_filter(NULL, trigger_data, NULL);
5815 remove_hist_vars(hist_data);
5817 kfree(trigger_data);
5819 destroy_hist_data(hist_data);
5823 static struct event_command trigger_hist_cmd = {
5825 .trigger_type = ETT_EVENT_HIST,
5826 .flags = EVENT_CMD_FL_NEEDS_REC,
5827 .func = event_hist_trigger_func,
5828 .reg = hist_register_trigger,
5829 .unreg = hist_unregister_trigger,
5830 .unreg_all = hist_unreg_all,
5831 .get_trigger_ops = event_hist_get_trigger_ops,
5832 .set_filter = set_trigger_filter,
5835 __init int register_trigger_hist_cmd(void)
5839 ret = register_event_command(&trigger_hist_cmd);
5846 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5847 struct ring_buffer_event *event)
5849 struct enable_trigger_data *enable_data = data->private_data;
5850 struct event_trigger_data *test;
5852 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
5853 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5854 if (enable_data->enable)
5855 test->paused = false;
5857 test->paused = true;
5863 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5864 struct ring_buffer_event *event)
5869 if (data->count != -1)
5872 hist_enable_trigger(data, rec, event);
5875 static struct event_trigger_ops hist_enable_trigger_ops = {
5876 .func = hist_enable_trigger,
5877 .print = event_enable_trigger_print,
5878 .init = event_trigger_init,
5879 .free = event_enable_trigger_free,
5882 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5883 .func = hist_enable_count_trigger,
5884 .print = event_enable_trigger_print,
5885 .init = event_trigger_init,
5886 .free = event_enable_trigger_free,
5889 static struct event_trigger_ops hist_disable_trigger_ops = {
5890 .func = hist_enable_trigger,
5891 .print = event_enable_trigger_print,
5892 .init = event_trigger_init,
5893 .free = event_enable_trigger_free,
5896 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5897 .func = hist_enable_count_trigger,
5898 .print = event_enable_trigger_print,
5899 .init = event_trigger_init,
5900 .free = event_enable_trigger_free,
5903 static struct event_trigger_ops *
5904 hist_enable_get_trigger_ops(char *cmd, char *param)
5906 struct event_trigger_ops *ops;
5909 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5912 ops = param ? &hist_enable_count_trigger_ops :
5913 &hist_enable_trigger_ops;
5915 ops = param ? &hist_disable_count_trigger_ops :
5916 &hist_disable_trigger_ops;
5921 static void hist_enable_unreg_all(struct trace_event_file *file)
5923 struct event_trigger_data *test, *n;
5925 list_for_each_entry_safe(test, n, &file->triggers, list) {
5926 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5927 list_del_rcu(&test->list);
5928 update_cond_flag(file);
5929 trace_event_trigger_enable_disable(file, 0);
5930 if (test->ops->free)
5931 test->ops->free(test->ops, test);
5936 static struct event_command trigger_hist_enable_cmd = {
5937 .name = ENABLE_HIST_STR,
5938 .trigger_type = ETT_HIST_ENABLE,
5939 .func = event_enable_trigger_func,
5940 .reg = event_enable_register_trigger,
5941 .unreg = event_enable_unregister_trigger,
5942 .unreg_all = hist_enable_unreg_all,
5943 .get_trigger_ops = hist_enable_get_trigger_ops,
5944 .set_filter = set_trigger_filter,
5947 static struct event_command trigger_hist_disable_cmd = {
5948 .name = DISABLE_HIST_STR,
5949 .trigger_type = ETT_HIST_ENABLE,
5950 .func = event_enable_trigger_func,
5951 .reg = event_enable_register_trigger,
5952 .unreg = event_enable_unregister_trigger,
5953 .unreg_all = hist_enable_unreg_all,
5954 .get_trigger_ops = hist_enable_get_trigger_ops,
5955 .set_filter = set_trigger_filter,
5958 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5960 unregister_event_command(&trigger_hist_enable_cmd);
5961 unregister_event_command(&trigger_hist_disable_cmd);
5964 __init int register_trigger_hist_enable_disable_cmds(void)
5968 ret = register_event_command(&trigger_hist_enable_cmd);
5969 if (WARN_ON(ret < 0))
5971 ret = register_event_command(&trigger_hist_disable_cmd);
5972 if (WARN_ON(ret < 0))
5973 unregister_trigger_hist_enable_disable_cmds();
5978 static __init int trace_events_hist_init(void)
5980 struct dentry *entry = NULL;
5981 struct dentry *d_tracer;
5984 d_tracer = tracing_init_dentry();
5985 if (IS_ERR(d_tracer)) {
5986 err = PTR_ERR(d_tracer);
5990 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
5991 NULL, &synth_events_fops);
5999 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6004 fs_initcall(trace_events_hist_init);