1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_hist - trace event hist triggers
5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/stacktrace.h>
13 #include <linux/rculist.h>
14 #include <linux/tracefs.h>
16 #include "tracing_map.h"
18 #include "trace_dynevent.h"
20 #define SYNTH_SYSTEM "synthetic"
21 #define SYNTH_FIELDS_MAX 16
23 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
27 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
28 struct tracing_map_elt *elt,
29 struct ring_buffer_event *rbe,
32 #define HIST_FIELD_OPERANDS_MAX 2
33 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
34 #define HIST_ACTIONS_MAX 8
44 * A hist_var (histogram variable) contains variable information for
45 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
46 * flag set. A hist_var has a variable name e.g. ts0, and is
47 * associated with a given histogram trigger, as specified by
48 * hist_data. The hist_var idx is the unique index assigned to the
49 * variable by the hist trigger's tracing_map. The idx is what is
50 * used to set a variable's value and, by a variable reference, to
55 struct hist_trigger_data *hist_data;
60 struct ftrace_event_field *field;
65 unsigned int is_signed;
67 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
68 struct hist_trigger_data *hist_data;
71 * Variable fields contain variable-specific info in var.
74 enum field_op_id operator;
79 * The name field is used for EXPR and VAR_REF fields. VAR
80 * fields contain the variable name in var.name.
85 * When a histogram trigger is hit, if it has any references
86 * to variables, the values of those variables are collected
87 * into a var_ref_vals array by resolve_var_refs(). The
88 * current value of each variable is read from the tracing_map
89 * using the hist field's hist_var.idx and entered into the
90 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
92 unsigned int var_ref_idx;
96 static u64 hist_field_none(struct hist_field *field,
97 struct tracing_map_elt *elt,
98 struct ring_buffer_event *rbe,
104 static u64 hist_field_counter(struct hist_field *field,
105 struct tracing_map_elt *elt,
106 struct ring_buffer_event *rbe,
112 static u64 hist_field_string(struct hist_field *hist_field,
113 struct tracing_map_elt *elt,
114 struct ring_buffer_event *rbe,
117 char *addr = (char *)(event + hist_field->field->offset);
119 return (u64)(unsigned long)addr;
122 static u64 hist_field_dynstring(struct hist_field *hist_field,
123 struct tracing_map_elt *elt,
124 struct ring_buffer_event *rbe,
127 u32 str_item = *(u32 *)(event + hist_field->field->offset);
128 int str_loc = str_item & 0xffff;
129 char *addr = (char *)(event + str_loc);
131 return (u64)(unsigned long)addr;
134 static u64 hist_field_pstring(struct hist_field *hist_field,
135 struct tracing_map_elt *elt,
136 struct ring_buffer_event *rbe,
139 char **addr = (char **)(event + hist_field->field->offset);
141 return (u64)(unsigned long)*addr;
144 static u64 hist_field_log2(struct hist_field *hist_field,
145 struct tracing_map_elt *elt,
146 struct ring_buffer_event *rbe,
149 struct hist_field *operand = hist_field->operands[0];
151 u64 val = operand->fn(operand, elt, rbe, event);
153 return (u64) ilog2(roundup_pow_of_two(val));
156 static u64 hist_field_plus(struct hist_field *hist_field,
157 struct tracing_map_elt *elt,
158 struct ring_buffer_event *rbe,
161 struct hist_field *operand1 = hist_field->operands[0];
162 struct hist_field *operand2 = hist_field->operands[1];
164 u64 val1 = operand1->fn(operand1, elt, rbe, event);
165 u64 val2 = operand2->fn(operand2, elt, rbe, event);
170 static u64 hist_field_minus(struct hist_field *hist_field,
171 struct tracing_map_elt *elt,
172 struct ring_buffer_event *rbe,
175 struct hist_field *operand1 = hist_field->operands[0];
176 struct hist_field *operand2 = hist_field->operands[1];
178 u64 val1 = operand1->fn(operand1, elt, rbe, event);
179 u64 val2 = operand2->fn(operand2, elt, rbe, event);
184 static u64 hist_field_unary_minus(struct hist_field *hist_field,
185 struct tracing_map_elt *elt,
186 struct ring_buffer_event *rbe,
189 struct hist_field *operand = hist_field->operands[0];
191 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
192 u64 val = (u64)-sval;
197 #define DEFINE_HIST_FIELD_FN(type) \
198 static u64 hist_field_##type(struct hist_field *hist_field, \
199 struct tracing_map_elt *elt, \
200 struct ring_buffer_event *rbe, \
203 type *addr = (type *)(event + hist_field->field->offset); \
205 return (u64)(unsigned long)*addr; \
208 DEFINE_HIST_FIELD_FN(s64);
209 DEFINE_HIST_FIELD_FN(u64);
210 DEFINE_HIST_FIELD_FN(s32);
211 DEFINE_HIST_FIELD_FN(u32);
212 DEFINE_HIST_FIELD_FN(s16);
213 DEFINE_HIST_FIELD_FN(u16);
214 DEFINE_HIST_FIELD_FN(s8);
215 DEFINE_HIST_FIELD_FN(u8);
217 #define for_each_hist_field(i, hist_data) \
218 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
220 #define for_each_hist_val_field(i, hist_data) \
221 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
223 #define for_each_hist_key_field(i, hist_data) \
224 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
226 #define HIST_STACKTRACE_DEPTH 16
227 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
228 #define HIST_STACKTRACE_SKIP 5
230 #define HITCOUNT_IDX 0
231 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
233 enum hist_field_flags {
234 HIST_FIELD_FL_HITCOUNT = 1 << 0,
235 HIST_FIELD_FL_KEY = 1 << 1,
236 HIST_FIELD_FL_STRING = 1 << 2,
237 HIST_FIELD_FL_HEX = 1 << 3,
238 HIST_FIELD_FL_SYM = 1 << 4,
239 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
240 HIST_FIELD_FL_EXECNAME = 1 << 6,
241 HIST_FIELD_FL_SYSCALL = 1 << 7,
242 HIST_FIELD_FL_STACKTRACE = 1 << 8,
243 HIST_FIELD_FL_LOG2 = 1 << 9,
244 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
245 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
246 HIST_FIELD_FL_VAR = 1 << 12,
247 HIST_FIELD_FL_EXPR = 1 << 13,
248 HIST_FIELD_FL_VAR_REF = 1 << 14,
249 HIST_FIELD_FL_CPU = 1 << 15,
250 HIST_FIELD_FL_ALIAS = 1 << 16,
255 char *name[TRACING_MAP_VARS_MAX];
256 char *expr[TRACING_MAP_VARS_MAX];
259 struct hist_trigger_attrs {
269 unsigned int map_bits;
271 char *assignment_str[TRACING_MAP_VARS_MAX];
272 unsigned int n_assignments;
274 char *action_str[HIST_ACTIONS_MAX];
275 unsigned int n_actions;
277 struct var_defs var_defs;
281 struct hist_field *var;
282 struct hist_field *val;
285 struct field_var_hist {
286 struct hist_trigger_data *hist_data;
290 struct hist_trigger_data {
291 struct hist_field *fields[HIST_FIELDS_MAX];
294 unsigned int n_fields;
296 unsigned int key_size;
297 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
298 unsigned int n_sort_keys;
299 struct trace_event_file *event_file;
300 struct hist_trigger_attrs *attrs;
301 struct tracing_map *map;
302 bool enable_timestamps;
304 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
305 unsigned int n_var_refs;
307 struct action_data *actions[HIST_ACTIONS_MAX];
308 unsigned int n_actions;
310 struct field_var *field_vars[SYNTH_FIELDS_MAX];
311 unsigned int n_field_vars;
312 unsigned int n_field_var_str;
313 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
314 unsigned int n_field_var_hists;
316 struct field_var *save_vars[SYNTH_FIELDS_MAX];
317 unsigned int n_save_vars;
318 unsigned int n_save_var_str;
321 static int synth_event_create(int argc, const char **argv);
322 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
323 static int synth_event_release(struct dyn_event *ev);
324 static bool synth_event_is_busy(struct dyn_event *ev);
325 static bool synth_event_match(const char *system, const char *event,
326 struct dyn_event *ev);
328 static struct dyn_event_operations synth_event_ops = {
329 .create = synth_event_create,
330 .show = synth_event_show,
331 .is_busy = synth_event_is_busy,
332 .free = synth_event_release,
333 .match = synth_event_match,
345 struct dyn_event devent;
348 struct synth_field **fields;
349 unsigned int n_fields;
351 struct trace_event_class class;
352 struct trace_event_call call;
353 struct tracepoint *tp;
356 static bool is_synth_event(struct dyn_event *ev)
358 return ev->ops == &synth_event_ops;
361 static struct synth_event *to_synth_event(struct dyn_event *ev)
363 return container_of(ev, struct synth_event, devent);
366 static bool synth_event_is_busy(struct dyn_event *ev)
368 struct synth_event *event = to_synth_event(ev);
370 return event->ref != 0;
373 static bool synth_event_match(const char *system, const char *event,
374 struct dyn_event *ev)
376 struct synth_event *sev = to_synth_event(ev);
378 return strcmp(sev->name, event) == 0 &&
379 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
384 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
385 struct tracing_map_elt *elt, void *rec,
386 struct ring_buffer_event *rbe, void *key,
387 struct action_data *data, u64 *var_ref_vals);
389 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
404 enum handler_id handler;
405 enum action_id action;
409 unsigned int n_params;
410 char *params[SYNTH_FIELDS_MAX];
413 * When a histogram trigger is hit, the values of any
414 * references to variables, including variables being passed
415 * as parameters to synthetic events, are collected into a
416 * var_ref_vals array. This var_ref_idx is the index of the
417 * first param in the array to be passed to the synthetic
420 unsigned int var_ref_idx;
421 struct synth_event *synth_event;
422 bool use_trace_keyword;
423 char *synth_event_name;
433 * var_str contains the $-unstripped variable
434 * name referenced by var_ref, and used when
435 * printing the action. Because var_ref
436 * creation is deferred to create_actions(),
437 * we need a per-action way to save it until
438 * then, thus var_str.
443 * var_ref refers to the variable being
444 * tracked e.g onmax($var).
446 struct hist_field *var_ref;
449 * track_var contains the 'invisible' tracking
450 * variable created to keep the current
453 struct hist_field *track_var;
455 check_track_val_fn_t check_val;
456 action_fn_t save_data;
465 unsigned int key_len;
467 struct tracing_map_elt elt;
469 struct action_data *action_data;
470 struct hist_trigger_data *hist_data;
473 struct hist_elt_data {
476 char *field_var_str[SYNTH_FIELDS_MAX];
479 struct snapshot_context {
480 struct tracing_map_elt *elt;
484 static void track_data_free(struct track_data *track_data)
486 struct hist_elt_data *elt_data;
491 kfree(track_data->key);
493 elt_data = track_data->elt.private_data;
495 kfree(elt_data->comm);
502 static struct track_data *track_data_alloc(unsigned int key_len,
503 struct action_data *action_data,
504 struct hist_trigger_data *hist_data)
506 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
507 struct hist_elt_data *elt_data;
510 return ERR_PTR(-ENOMEM);
512 data->key = kzalloc(key_len, GFP_KERNEL);
514 track_data_free(data);
515 return ERR_PTR(-ENOMEM);
518 data->key_len = key_len;
519 data->action_data = action_data;
520 data->hist_data = hist_data;
522 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
524 track_data_free(data);
525 return ERR_PTR(-ENOMEM);
527 data->elt.private_data = elt_data;
529 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
530 if (!elt_data->comm) {
531 track_data_free(data);
532 return ERR_PTR(-ENOMEM);
538 static char last_hist_cmd[MAX_FILTER_STR_VAL];
539 static char hist_err_str[MAX_FILTER_STR_VAL];
541 static void last_cmd_set(char *str)
546 strncpy(last_hist_cmd, str, MAX_FILTER_STR_VAL - 1);
549 static void hist_err(char *str, char *var)
551 int maxlen = MAX_FILTER_STR_VAL - 1;
556 if (strlen(hist_err_str))
562 if (strlen(hist_err_str) + strlen(str) + strlen(var) > maxlen)
565 strcat(hist_err_str, str);
566 strcat(hist_err_str, var);
569 static void hist_err_event(char *str, char *system, char *event, char *var)
571 char err[MAX_FILTER_STR_VAL];
574 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s.%s", system, event, var);
576 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
578 strscpy(err, var, MAX_FILTER_STR_VAL);
583 static void hist_err_clear(void)
585 hist_err_str[0] = '\0';
588 static bool have_hist_err(void)
590 if (strlen(hist_err_str))
596 struct synth_trace_event {
597 struct trace_entry ent;
601 static int synth_event_define_fields(struct trace_event_call *call)
603 struct synth_trace_event trace;
604 int offset = offsetof(typeof(trace), fields);
605 struct synth_event *event = call->data;
606 unsigned int i, size, n_u64;
611 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
612 size = event->fields[i]->size;
613 is_signed = event->fields[i]->is_signed;
614 type = event->fields[i]->type;
615 name = event->fields[i]->name;
616 ret = trace_define_field(call, type, name, offset, size,
617 is_signed, FILTER_OTHER);
621 if (event->fields[i]->is_string) {
622 offset += STR_VAR_LEN_MAX;
623 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
625 offset += sizeof(u64);
630 event->n_u64 = n_u64;
635 static bool synth_field_signed(char *type)
637 if (str_has_prefix(type, "u"))
643 static int synth_field_is_string(char *type)
645 if (strstr(type, "char[") != NULL)
651 static int synth_field_string_size(char *type)
653 char buf[4], *end, *start;
657 start = strstr(type, "char[");
660 start += sizeof("char[") - 1;
662 end = strchr(type, ']');
663 if (!end || end < start)
670 strncpy(buf, start, len);
673 err = kstrtouint(buf, 0, &size);
677 if (size > STR_VAR_LEN_MAX)
683 static int synth_field_size(char *type)
687 if (strcmp(type, "s64") == 0)
689 else if (strcmp(type, "u64") == 0)
691 else if (strcmp(type, "s32") == 0)
693 else if (strcmp(type, "u32") == 0)
695 else if (strcmp(type, "s16") == 0)
697 else if (strcmp(type, "u16") == 0)
699 else if (strcmp(type, "s8") == 0)
701 else if (strcmp(type, "u8") == 0)
703 else if (strcmp(type, "char") == 0)
705 else if (strcmp(type, "unsigned char") == 0)
706 size = sizeof(unsigned char);
707 else if (strcmp(type, "int") == 0)
709 else if (strcmp(type, "unsigned int") == 0)
710 size = sizeof(unsigned int);
711 else if (strcmp(type, "long") == 0)
713 else if (strcmp(type, "unsigned long") == 0)
714 size = sizeof(unsigned long);
715 else if (strcmp(type, "pid_t") == 0)
716 size = sizeof(pid_t);
717 else if (synth_field_is_string(type))
718 size = synth_field_string_size(type);
723 static const char *synth_field_fmt(char *type)
725 const char *fmt = "%llu";
727 if (strcmp(type, "s64") == 0)
729 else if (strcmp(type, "u64") == 0)
731 else if (strcmp(type, "s32") == 0)
733 else if (strcmp(type, "u32") == 0)
735 else if (strcmp(type, "s16") == 0)
737 else if (strcmp(type, "u16") == 0)
739 else if (strcmp(type, "s8") == 0)
741 else if (strcmp(type, "u8") == 0)
743 else if (strcmp(type, "char") == 0)
745 else if (strcmp(type, "unsigned char") == 0)
747 else if (strcmp(type, "int") == 0)
749 else if (strcmp(type, "unsigned int") == 0)
751 else if (strcmp(type, "long") == 0)
753 else if (strcmp(type, "unsigned long") == 0)
755 else if (strcmp(type, "pid_t") == 0)
757 else if (synth_field_is_string(type))
763 static enum print_line_t print_synth_event(struct trace_iterator *iter,
765 struct trace_event *event)
767 struct trace_array *tr = iter->tr;
768 struct trace_seq *s = &iter->seq;
769 struct synth_trace_event *entry;
770 struct synth_event *se;
771 unsigned int i, n_u64;
775 entry = (struct synth_trace_event *)iter->ent;
776 se = container_of(event, struct synth_event, call.event);
778 trace_seq_printf(s, "%s: ", se->name);
780 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
781 if (trace_seq_has_overflowed(s))
784 fmt = synth_field_fmt(se->fields[i]->type);
786 /* parameter types */
787 if (tr->trace_flags & TRACE_ITER_VERBOSE)
788 trace_seq_printf(s, "%s ", fmt);
790 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
792 /* parameter values */
793 if (se->fields[i]->is_string) {
794 trace_seq_printf(s, print_fmt, se->fields[i]->name,
795 (char *)&entry->fields[n_u64],
796 i == se->n_fields - 1 ? "" : " ");
797 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
799 trace_seq_printf(s, print_fmt, se->fields[i]->name,
800 entry->fields[n_u64],
801 i == se->n_fields - 1 ? "" : " ");
806 trace_seq_putc(s, '\n');
808 return trace_handle_return(s);
811 static struct trace_event_functions synth_event_funcs = {
812 .trace = print_synth_event
815 static notrace void trace_event_raw_event_synth(void *__data,
817 unsigned int var_ref_idx)
819 struct trace_event_file *trace_file = __data;
820 struct synth_trace_event *entry;
821 struct trace_event_buffer fbuffer;
822 struct ring_buffer *buffer;
823 struct synth_event *event;
824 unsigned int i, n_u64;
827 event = trace_file->event_call->data;
829 if (trace_trigger_soft_disabled(trace_file))
832 fields_size = event->n_u64 * sizeof(u64);
835 * Avoid ring buffer recursion detection, as this event
836 * is being performed within another event.
838 buffer = trace_file->tr->trace_buffer.buffer;
839 ring_buffer_nest_start(buffer);
841 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
842 sizeof(*entry) + fields_size);
846 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
847 if (event->fields[i]->is_string) {
848 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
849 char *str_field = (char *)&entry->fields[n_u64];
851 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
852 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
854 entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
859 trace_event_buffer_commit(&fbuffer);
861 ring_buffer_nest_end(buffer);
864 static void free_synth_event_print_fmt(struct trace_event_call *call)
867 kfree(call->print_fmt);
868 call->print_fmt = NULL;
872 static int __set_synth_event_print_fmt(struct synth_event *event,
879 /* When len=0, we just calculate the needed length */
880 #define LEN_OR_ZERO (len ? len - pos : 0)
882 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
883 for (i = 0; i < event->n_fields; i++) {
884 fmt = synth_field_fmt(event->fields[i]->type);
885 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
886 event->fields[i]->name, fmt,
887 i == event->n_fields - 1 ? "" : ", ");
889 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
891 for (i = 0; i < event->n_fields; i++) {
892 pos += snprintf(buf + pos, LEN_OR_ZERO,
893 ", REC->%s", event->fields[i]->name);
898 /* return the length of print_fmt */
902 static int set_synth_event_print_fmt(struct trace_event_call *call)
904 struct synth_event *event = call->data;
908 /* First: called with 0 length to calculate the needed length */
909 len = __set_synth_event_print_fmt(event, NULL, 0);
911 print_fmt = kmalloc(len + 1, GFP_KERNEL);
915 /* Second: actually write the @print_fmt */
916 __set_synth_event_print_fmt(event, print_fmt, len + 1);
917 call->print_fmt = print_fmt;
922 static void free_synth_field(struct synth_field *field)
929 static struct synth_field *parse_synth_field(int argc, const char **argv,
932 struct synth_field *field;
933 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
936 if (field_type[0] == ';')
939 if (!strcmp(field_type, "unsigned")) {
941 return ERR_PTR(-EINVAL);
942 prefix = "unsigned ";
943 field_type = argv[1];
944 field_name = argv[2];
947 field_name = argv[1];
951 field = kzalloc(sizeof(*field), GFP_KERNEL);
953 return ERR_PTR(-ENOMEM);
955 len = strlen(field_name);
956 array = strchr(field_name, '[');
958 len -= strlen(array);
959 else if (field_name[len - 1] == ';')
962 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
968 if (field_type[0] == ';')
970 len = strlen(field_type) + 1;
972 len += strlen(array);
974 len += strlen(prefix);
976 field->type = kzalloc(len, GFP_KERNEL);
982 strcat(field->type, prefix);
983 strcat(field->type, field_type);
985 strcat(field->type, array);
986 if (field->type[len - 1] == ';')
987 field->type[len - 1] = '\0';
990 field->size = synth_field_size(field->type);
996 if (synth_field_is_string(field->type))
997 field->is_string = true;
999 field->is_signed = synth_field_signed(field->type);
1004 free_synth_field(field);
1005 field = ERR_PTR(ret);
1009 static void free_synth_tracepoint(struct tracepoint *tp)
1018 static struct tracepoint *alloc_synth_tracepoint(char *name)
1020 struct tracepoint *tp;
1022 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1024 return ERR_PTR(-ENOMEM);
1026 tp->name = kstrdup(name, GFP_KERNEL);
1029 return ERR_PTR(-ENOMEM);
1035 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1036 unsigned int var_ref_idx);
1038 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1039 unsigned int var_ref_idx)
1041 struct tracepoint *tp = event->tp;
1043 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1044 struct tracepoint_func *probe_func_ptr;
1045 synth_probe_func_t probe_func;
1048 if (!(cpu_online(raw_smp_processor_id())))
1051 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1052 if (probe_func_ptr) {
1054 probe_func = probe_func_ptr->func;
1055 __data = probe_func_ptr->data;
1056 probe_func(__data, var_ref_vals, var_ref_idx);
1057 } while ((++probe_func_ptr)->func);
1062 static struct synth_event *find_synth_event(const char *name)
1064 struct dyn_event *pos;
1065 struct synth_event *event;
1067 for_each_dyn_event(pos) {
1068 if (!is_synth_event(pos))
1070 event = to_synth_event(pos);
1071 if (strcmp(event->name, name) == 0)
1078 static int register_synth_event(struct synth_event *event)
1080 struct trace_event_call *call = &event->call;
1083 event->call.class = &event->class;
1084 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1085 if (!event->class.system) {
1090 event->tp = alloc_synth_tracepoint(event->name);
1091 if (IS_ERR(event->tp)) {
1092 ret = PTR_ERR(event->tp);
1097 INIT_LIST_HEAD(&call->class->fields);
1098 call->event.funcs = &synth_event_funcs;
1099 call->class->define_fields = synth_event_define_fields;
1101 ret = register_trace_event(&call->event);
1106 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1107 call->class->reg = trace_event_reg;
1108 call->class->probe = trace_event_raw_event_synth;
1110 call->tp = event->tp;
1112 ret = trace_add_event_call(call);
1114 pr_warn("Failed to register synthetic event: %s\n",
1115 trace_event_name(call));
1119 ret = set_synth_event_print_fmt(call);
1121 trace_remove_event_call(call);
1127 unregister_trace_event(&call->event);
1131 static int unregister_synth_event(struct synth_event *event)
1133 struct trace_event_call *call = &event->call;
1136 ret = trace_remove_event_call(call);
1141 static void free_synth_event(struct synth_event *event)
1148 for (i = 0; i < event->n_fields; i++)
1149 free_synth_field(event->fields[i]);
1151 kfree(event->fields);
1153 kfree(event->class.system);
1154 free_synth_tracepoint(event->tp);
1155 free_synth_event_print_fmt(&event->call);
1159 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1160 struct synth_field **fields)
1162 struct synth_event *event;
1165 event = kzalloc(sizeof(*event), GFP_KERNEL);
1167 event = ERR_PTR(-ENOMEM);
1171 event->name = kstrdup(name, GFP_KERNEL);
1174 event = ERR_PTR(-ENOMEM);
1178 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1179 if (!event->fields) {
1180 free_synth_event(event);
1181 event = ERR_PTR(-ENOMEM);
1185 dyn_event_init(&event->devent, &synth_event_ops);
1187 for (i = 0; i < n_fields; i++)
1188 event->fields[i] = fields[i];
1190 event->n_fields = n_fields;
1195 static void action_trace(struct hist_trigger_data *hist_data,
1196 struct tracing_map_elt *elt, void *rec,
1197 struct ring_buffer_event *rbe, void *key,
1198 struct action_data *data, u64 *var_ref_vals)
1200 struct synth_event *event = data->synth_event;
1202 trace_synth(event, var_ref_vals, data->var_ref_idx);
1205 struct hist_var_data {
1206 struct list_head list;
1207 struct hist_trigger_data *hist_data;
1210 static int __create_synth_event(int argc, const char *name, const char **argv)
1212 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1213 struct synth_event *event = NULL;
1214 int i, consumed = 0, n_fields = 0, ret = 0;
1218 * - Add synthetic event: <event_name> field[;field] ...
1219 * - Remove synthetic event: !<event_name> field[;field] ...
1220 * where 'field' = type field_name
1223 if (name[0] == '\0' || argc < 1)
1226 mutex_lock(&event_mutex);
1228 event = find_synth_event(name);
1234 for (i = 0; i < argc - 1; i++) {
1235 if (strcmp(argv[i], ";") == 0)
1237 if (n_fields == SYNTH_FIELDS_MAX) {
1242 field = parse_synth_field(argc - i, &argv[i], &consumed);
1243 if (IS_ERR(field)) {
1244 ret = PTR_ERR(field);
1247 fields[n_fields++] = field;
1251 if (i < argc && strcmp(argv[i], ";") != 0) {
1256 event = alloc_synth_event(name, n_fields, fields);
1257 if (IS_ERR(event)) {
1258 ret = PTR_ERR(event);
1262 ret = register_synth_event(event);
1264 dyn_event_add(&event->devent);
1266 free_synth_event(event);
1268 mutex_unlock(&event_mutex);
1272 for (i = 0; i < n_fields; i++)
1273 free_synth_field(fields[i]);
1278 static int create_or_delete_synth_event(int argc, char **argv)
1280 const char *name = argv[0];
1281 struct synth_event *event = NULL;
1284 /* trace_run_command() ensures argc != 0 */
1285 if (name[0] == '!') {
1286 mutex_lock(&event_mutex);
1287 event = find_synth_event(name + 1);
1292 ret = unregister_synth_event(event);
1294 dyn_event_remove(&event->devent);
1295 free_synth_event(event);
1300 mutex_unlock(&event_mutex);
1304 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1305 return ret == -ECANCELED ? -EINVAL : ret;
1308 static int synth_event_create(int argc, const char **argv)
1310 const char *name = argv[0];
1313 if (name[0] != 's' || name[1] != ':')
1317 /* This interface accepts group name prefix */
1318 if (strchr(name, '/')) {
1319 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1324 return __create_synth_event(argc - 1, name, argv + 1);
1327 static int synth_event_release(struct dyn_event *ev)
1329 struct synth_event *event = to_synth_event(ev);
1335 ret = unregister_synth_event(event);
1339 dyn_event_remove(ev);
1340 free_synth_event(event);
1344 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1346 struct synth_field *field;
1349 seq_printf(m, "%s\t", event->name);
1351 for (i = 0; i < event->n_fields; i++) {
1352 field = event->fields[i];
1354 /* parameter values */
1355 seq_printf(m, "%s %s%s", field->type, field->name,
1356 i == event->n_fields - 1 ? "" : "; ");
1364 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1366 struct synth_event *event = to_synth_event(ev);
1368 seq_printf(m, "s:%s/", event->class.system);
1370 return __synth_event_show(m, event);
1373 static int synth_events_seq_show(struct seq_file *m, void *v)
1375 struct dyn_event *ev = v;
1377 if (!is_synth_event(ev))
1380 return __synth_event_show(m, to_synth_event(ev));
1383 static const struct seq_operations synth_events_seq_op = {
1384 .start = dyn_event_seq_start,
1385 .next = dyn_event_seq_next,
1386 .stop = dyn_event_seq_stop,
1387 .show = synth_events_seq_show,
1390 static int synth_events_open(struct inode *inode, struct file *file)
1394 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1395 ret = dyn_events_release_all(&synth_event_ops);
1400 return seq_open(file, &synth_events_seq_op);
1403 static ssize_t synth_events_write(struct file *file,
1404 const char __user *buffer,
1405 size_t count, loff_t *ppos)
1407 return trace_parse_run_command(file, buffer, count, ppos,
1408 create_or_delete_synth_event);
1411 static const struct file_operations synth_events_fops = {
1412 .open = synth_events_open,
1413 .write = synth_events_write,
1415 .llseek = seq_lseek,
1416 .release = seq_release,
1419 static u64 hist_field_timestamp(struct hist_field *hist_field,
1420 struct tracing_map_elt *elt,
1421 struct ring_buffer_event *rbe,
1424 struct hist_trigger_data *hist_data = hist_field->hist_data;
1425 struct trace_array *tr = hist_data->event_file->tr;
1427 u64 ts = ring_buffer_event_time_stamp(rbe);
1429 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1435 static u64 hist_field_cpu(struct hist_field *hist_field,
1436 struct tracing_map_elt *elt,
1437 struct ring_buffer_event *rbe,
1440 int cpu = smp_processor_id();
1446 * check_field_for_var_ref - Check if a VAR_REF field references a variable
1447 * @hist_field: The VAR_REF field to check
1448 * @var_data: The hist trigger that owns the variable
1449 * @var_idx: The trigger variable identifier
1451 * Check the given VAR_REF field to see whether or not it references
1452 * the given variable associated with the given trigger.
1454 * Return: The VAR_REF field if it does reference the variable, NULL if not
1456 static struct hist_field *
1457 check_field_for_var_ref(struct hist_field *hist_field,
1458 struct hist_trigger_data *var_data,
1459 unsigned int var_idx)
1461 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
1463 if (hist_field && hist_field->var.idx == var_idx &&
1464 hist_field->var.hist_data == var_data)
1471 * find_var_ref - Check if a trigger has a reference to a trigger variable
1472 * @hist_data: The hist trigger that might have a reference to the variable
1473 * @var_data: The hist trigger that owns the variable
1474 * @var_idx: The trigger variable identifier
1476 * Check the list of var_refs[] on the first hist trigger to see
1477 * whether any of them are references to the variable on the second
1480 * Return: The VAR_REF field referencing the variable if so, NULL if not
1482 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1483 struct hist_trigger_data *var_data,
1484 unsigned int var_idx)
1486 struct hist_field *hist_field;
1489 for (i = 0; i < hist_data->n_var_refs; i++) {
1490 hist_field = hist_data->var_refs[i];
1491 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1499 * find_any_var_ref - Check if there is a reference to a given trigger variable
1500 * @hist_data: The hist trigger
1501 * @var_idx: The trigger variable identifier
1503 * Check to see whether the given variable is currently referenced by
1504 * any other trigger.
1506 * The trigger the variable is defined on is explicitly excluded - the
1507 * assumption being that a self-reference doesn't prevent a trigger
1508 * from being removed.
1510 * Return: The VAR_REF field referencing the variable if so, NULL if not
1512 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1513 unsigned int var_idx)
1515 struct trace_array *tr = hist_data->event_file->tr;
1516 struct hist_field *found = NULL;
1517 struct hist_var_data *var_data;
1519 list_for_each_entry(var_data, &tr->hist_vars, list) {
1520 if (var_data->hist_data == hist_data)
1522 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1531 * check_var_refs - Check if there is a reference to any of trigger's variables
1532 * @hist_data: The hist trigger
1534 * A trigger can define one or more variables. If any one of them is
1535 * currently referenced by any other trigger, this function will
1538 * Typically used to determine whether or not a trigger can be removed
1539 * - if there are any references to a trigger's variables, it cannot.
1541 * Return: True if there is a reference to any of trigger's variables
1543 static bool check_var_refs(struct hist_trigger_data *hist_data)
1545 struct hist_field *field;
1549 for_each_hist_field(i, hist_data) {
1550 field = hist_data->fields[i];
1551 if (field && field->flags & HIST_FIELD_FL_VAR) {
1552 if (find_any_var_ref(hist_data, field->var.idx)) {
1562 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1564 struct trace_array *tr = hist_data->event_file->tr;
1565 struct hist_var_data *var_data, *found = NULL;
1567 list_for_each_entry(var_data, &tr->hist_vars, list) {
1568 if (var_data->hist_data == hist_data) {
1577 static bool field_has_hist_vars(struct hist_field *hist_field,
1588 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1589 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1592 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1593 struct hist_field *operand;
1595 operand = hist_field->operands[i];
1596 if (field_has_hist_vars(operand, level + 1))
1603 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1605 struct hist_field *hist_field;
1608 for_each_hist_field(i, hist_data) {
1609 hist_field = hist_data->fields[i];
1610 if (field_has_hist_vars(hist_field, 0))
1617 static int save_hist_vars(struct hist_trigger_data *hist_data)
1619 struct trace_array *tr = hist_data->event_file->tr;
1620 struct hist_var_data *var_data;
1622 var_data = find_hist_vars(hist_data);
1626 if (trace_array_get(tr) < 0)
1629 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1631 trace_array_put(tr);
1635 var_data->hist_data = hist_data;
1636 list_add(&var_data->list, &tr->hist_vars);
1641 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1643 struct trace_array *tr = hist_data->event_file->tr;
1644 struct hist_var_data *var_data;
1646 var_data = find_hist_vars(hist_data);
1650 if (WARN_ON(check_var_refs(hist_data)))
1653 list_del(&var_data->list);
1657 trace_array_put(tr);
1660 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1661 const char *var_name)
1663 struct hist_field *hist_field, *found = NULL;
1666 for_each_hist_field(i, hist_data) {
1667 hist_field = hist_data->fields[i];
1668 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1669 strcmp(hist_field->var.name, var_name) == 0) {
1678 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1679 struct trace_event_file *file,
1680 const char *var_name)
1682 struct hist_trigger_data *test_data;
1683 struct event_trigger_data *test;
1684 struct hist_field *hist_field;
1686 hist_field = find_var_field(hist_data, var_name);
1690 list_for_each_entry_rcu(test, &file->triggers, list) {
1691 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1692 test_data = test->private_data;
1693 hist_field = find_var_field(test_data, var_name);
1702 static struct trace_event_file *find_var_file(struct trace_array *tr,
1707 struct hist_trigger_data *var_hist_data;
1708 struct hist_var_data *var_data;
1709 struct trace_event_file *file, *found = NULL;
1712 return find_event_file(tr, system, event_name);
1714 list_for_each_entry(var_data, &tr->hist_vars, list) {
1715 var_hist_data = var_data->hist_data;
1716 file = var_hist_data->event_file;
1720 if (find_var_field(var_hist_data, var_name)) {
1722 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1733 static struct hist_field *find_file_var(struct trace_event_file *file,
1734 const char *var_name)
1736 struct hist_trigger_data *test_data;
1737 struct event_trigger_data *test;
1738 struct hist_field *hist_field;
1740 list_for_each_entry_rcu(test, &file->triggers, list) {
1741 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1742 test_data = test->private_data;
1743 hist_field = find_var_field(test_data, var_name);
1752 static struct hist_field *
1753 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1755 struct trace_array *tr = hist_data->event_file->tr;
1756 struct hist_field *hist_field, *found = NULL;
1757 struct trace_event_file *file;
1760 for (i = 0; i < hist_data->n_actions; i++) {
1761 struct action_data *data = hist_data->actions[i];
1763 if (data->handler == HANDLER_ONMATCH) {
1764 char *system = data->match_data.event_system;
1765 char *event_name = data->match_data.event;
1767 file = find_var_file(tr, system, event_name, var_name);
1770 hist_field = find_file_var(file, var_name);
1773 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
1774 return ERR_PTR(-EINVAL);
1784 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1789 struct trace_array *tr = hist_data->event_file->tr;
1790 struct hist_field *hist_field = NULL;
1791 struct trace_event_file *file;
1793 if (!system || !event_name) {
1794 hist_field = find_match_var(hist_data, var_name);
1795 if (IS_ERR(hist_field))
1801 file = find_var_file(tr, system, event_name, var_name);
1805 hist_field = find_file_var(file, var_name);
1810 static u64 hist_field_var_ref(struct hist_field *hist_field,
1811 struct tracing_map_elt *elt,
1812 struct ring_buffer_event *rbe,
1815 struct hist_elt_data *elt_data;
1818 elt_data = elt->private_data;
1819 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1824 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1825 u64 *var_ref_vals, bool self)
1827 struct hist_trigger_data *var_data;
1828 struct tracing_map_elt *var_elt;
1829 struct hist_field *hist_field;
1830 unsigned int i, var_idx;
1831 bool resolved = true;
1834 for (i = 0; i < hist_data->n_var_refs; i++) {
1835 hist_field = hist_data->var_refs[i];
1836 var_idx = hist_field->var.idx;
1837 var_data = hist_field->var.hist_data;
1839 if (var_data == NULL) {
1844 if ((self && var_data != hist_data) ||
1845 (!self && var_data == hist_data))
1848 var_elt = tracing_map_lookup(var_data->map, key);
1854 if (!tracing_map_var_set(var_elt, var_idx)) {
1859 if (self || !hist_field->read_once)
1860 var_val = tracing_map_read_var(var_elt, var_idx);
1862 var_val = tracing_map_read_var_once(var_elt, var_idx);
1864 var_ref_vals[i] = var_val;
1870 static const char *hist_field_name(struct hist_field *field,
1873 const char *field_name = "";
1879 field_name = field->field->name;
1880 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1881 field->flags & HIST_FIELD_FL_ALIAS)
1882 field_name = hist_field_name(field->operands[0], ++level);
1883 else if (field->flags & HIST_FIELD_FL_CPU)
1885 else if (field->flags & HIST_FIELD_FL_EXPR ||
1886 field->flags & HIST_FIELD_FL_VAR_REF) {
1887 if (field->system) {
1888 static char full_name[MAX_FILTER_STR_VAL];
1890 strcat(full_name, field->system);
1891 strcat(full_name, ".");
1892 strcat(full_name, field->event_name);
1893 strcat(full_name, ".");
1894 strcat(full_name, field->name);
1895 field_name = full_name;
1897 field_name = field->name;
1898 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1899 field_name = "common_timestamp";
1901 if (field_name == NULL)
1907 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1909 hist_field_fn_t fn = NULL;
1911 switch (field_size) {
1913 if (field_is_signed)
1914 fn = hist_field_s64;
1916 fn = hist_field_u64;
1919 if (field_is_signed)
1920 fn = hist_field_s32;
1922 fn = hist_field_u32;
1925 if (field_is_signed)
1926 fn = hist_field_s16;
1928 fn = hist_field_u16;
1931 if (field_is_signed)
1941 static int parse_map_size(char *str)
1943 unsigned long size, map_bits;
1952 ret = kstrtoul(str, 0, &size);
1956 map_bits = ilog2(roundup_pow_of_two(size));
1957 if (map_bits < TRACING_MAP_BITS_MIN ||
1958 map_bits > TRACING_MAP_BITS_MAX)
1966 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1973 for (i = 0; i < attrs->n_assignments; i++)
1974 kfree(attrs->assignment_str[i]);
1976 for (i = 0; i < attrs->n_actions; i++)
1977 kfree(attrs->action_str[i]);
1980 kfree(attrs->sort_key_str);
1981 kfree(attrs->keys_str);
1982 kfree(attrs->vals_str);
1983 kfree(attrs->clock);
1987 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1991 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1994 if ((str_has_prefix(str, "onmatch(")) ||
1995 (str_has_prefix(str, "onmax(")) ||
1996 (str_has_prefix(str, "onchange("))) {
1997 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1998 if (!attrs->action_str[attrs->n_actions]) {
2009 static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
2013 if ((str_has_prefix(str, "key=")) ||
2014 (str_has_prefix(str, "keys="))) {
2015 attrs->keys_str = kstrdup(str, GFP_KERNEL);
2016 if (!attrs->keys_str) {
2020 } else if ((str_has_prefix(str, "val=")) ||
2021 (str_has_prefix(str, "vals=")) ||
2022 (str_has_prefix(str, "values="))) {
2023 attrs->vals_str = kstrdup(str, GFP_KERNEL);
2024 if (!attrs->vals_str) {
2028 } else if (str_has_prefix(str, "sort=")) {
2029 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
2030 if (!attrs->sort_key_str) {
2034 } else if (str_has_prefix(str, "name=")) {
2035 attrs->name = kstrdup(str, GFP_KERNEL);
2040 } else if (str_has_prefix(str, "clock=")) {
2047 str = strstrip(str);
2048 attrs->clock = kstrdup(str, GFP_KERNEL);
2049 if (!attrs->clock) {
2053 } else if (str_has_prefix(str, "size=")) {
2054 int map_bits = parse_map_size(str);
2060 attrs->map_bits = map_bits;
2064 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2065 hist_err("Too many variables defined: ", str);
2070 assignment = kstrdup(str, GFP_KERNEL);
2076 attrs->assignment_str[attrs->n_assignments++] = assignment;
2082 static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
2084 struct hist_trigger_attrs *attrs;
2087 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2089 return ERR_PTR(-ENOMEM);
2091 while (trigger_str) {
2092 char *str = strsep(&trigger_str, ":");
2094 if (strchr(str, '=')) {
2095 ret = parse_assignment(str, attrs);
2098 } else if (strcmp(str, "pause") == 0)
2099 attrs->pause = true;
2100 else if ((strcmp(str, "cont") == 0) ||
2101 (strcmp(str, "continue") == 0))
2103 else if (strcmp(str, "clear") == 0)
2104 attrs->clear = true;
2106 ret = parse_action(str, attrs);
2112 if (!attrs->keys_str) {
2117 if (!attrs->clock) {
2118 attrs->clock = kstrdup("global", GFP_KERNEL);
2119 if (!attrs->clock) {
2127 destroy_hist_trigger_attrs(attrs);
2129 return ERR_PTR(ret);
2132 static inline void save_comm(char *comm, struct task_struct *task)
2135 strcpy(comm, "<idle>");
2139 if (WARN_ON_ONCE(task->pid < 0)) {
2140 strcpy(comm, "<XXX>");
2144 strncpy(comm, task->comm, TASK_COMM_LEN);
2147 static void hist_elt_data_free(struct hist_elt_data *elt_data)
2151 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2152 kfree(elt_data->field_var_str[i]);
2154 kfree(elt_data->comm);
2158 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2160 struct hist_elt_data *elt_data = elt->private_data;
2162 hist_elt_data_free(elt_data);
2165 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
2167 struct hist_trigger_data *hist_data = elt->map->private_data;
2168 unsigned int size = TASK_COMM_LEN;
2169 struct hist_elt_data *elt_data;
2170 struct hist_field *key_field;
2171 unsigned int i, n_str;
2173 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2177 for_each_hist_key_field(i, hist_data) {
2178 key_field = hist_data->fields[i];
2180 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
2181 elt_data->comm = kzalloc(size, GFP_KERNEL);
2182 if (!elt_data->comm) {
2190 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
2192 size = STR_VAR_LEN_MAX;
2194 for (i = 0; i < n_str; i++) {
2195 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2196 if (!elt_data->field_var_str[i]) {
2197 hist_elt_data_free(elt_data);
2202 elt->private_data = elt_data;
2207 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2209 struct hist_elt_data *elt_data = elt->private_data;
2212 save_comm(elt_data->comm, current);
2215 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2216 .elt_alloc = hist_trigger_elt_data_alloc,
2217 .elt_free = hist_trigger_elt_data_free,
2218 .elt_init = hist_trigger_elt_data_init,
2221 static const char *get_hist_field_flags(struct hist_field *hist_field)
2223 const char *flags_str = NULL;
2225 if (hist_field->flags & HIST_FIELD_FL_HEX)
2227 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2229 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2230 flags_str = "sym-offset";
2231 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2232 flags_str = "execname";
2233 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2234 flags_str = "syscall";
2235 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2237 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2238 flags_str = "usecs";
2243 static void expr_field_str(struct hist_field *field, char *expr)
2245 if (field->flags & HIST_FIELD_FL_VAR_REF)
2248 strcat(expr, hist_field_name(field, 0));
2250 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2251 const char *flags_str = get_hist_field_flags(field);
2255 strcat(expr, flags_str);
2260 static char *expr_str(struct hist_field *field, unsigned int level)
2267 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2271 if (!field->operands[0]) {
2272 expr_field_str(field, expr);
2276 if (field->operator == FIELD_OP_UNARY_MINUS) {
2280 subexpr = expr_str(field->operands[0], ++level);
2285 strcat(expr, subexpr);
2293 expr_field_str(field->operands[0], expr);
2295 switch (field->operator) {
2296 case FIELD_OP_MINUS:
2307 expr_field_str(field->operands[1], expr);
2312 static int contains_operator(char *str)
2314 enum field_op_id field_op = FIELD_OP_NONE;
2317 op = strpbrk(str, "+-");
2319 return FIELD_OP_NONE;
2324 field_op = FIELD_OP_UNARY_MINUS;
2326 field_op = FIELD_OP_MINUS;
2329 field_op = FIELD_OP_PLUS;
2338 static void __destroy_hist_field(struct hist_field *hist_field)
2340 kfree(hist_field->var.name);
2341 kfree(hist_field->name);
2342 kfree(hist_field->type);
2347 static void destroy_hist_field(struct hist_field *hist_field,
2358 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2359 return; /* var refs will be destroyed separately */
2361 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2362 destroy_hist_field(hist_field->operands[i], level + 1);
2364 __destroy_hist_field(hist_field);
2367 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2368 struct ftrace_event_field *field,
2369 unsigned long flags,
2372 struct hist_field *hist_field;
2374 if (field && is_function_field(field))
2377 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2381 hist_field->hist_data = hist_data;
2383 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2384 goto out; /* caller will populate */
2386 if (flags & HIST_FIELD_FL_VAR_REF) {
2387 hist_field->fn = hist_field_var_ref;
2391 if (flags & HIST_FIELD_FL_HITCOUNT) {
2392 hist_field->fn = hist_field_counter;
2393 hist_field->size = sizeof(u64);
2394 hist_field->type = kstrdup("u64", GFP_KERNEL);
2395 if (!hist_field->type)
2400 if (flags & HIST_FIELD_FL_STACKTRACE) {
2401 hist_field->fn = hist_field_none;
2405 if (flags & HIST_FIELD_FL_LOG2) {
2406 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2407 hist_field->fn = hist_field_log2;
2408 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2409 hist_field->size = hist_field->operands[0]->size;
2410 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2411 if (!hist_field->type)
2416 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2417 hist_field->fn = hist_field_timestamp;
2418 hist_field->size = sizeof(u64);
2419 hist_field->type = kstrdup("u64", GFP_KERNEL);
2420 if (!hist_field->type)
2425 if (flags & HIST_FIELD_FL_CPU) {
2426 hist_field->fn = hist_field_cpu;
2427 hist_field->size = sizeof(int);
2428 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2429 if (!hist_field->type)
2434 if (WARN_ON_ONCE(!field))
2437 if (is_string_field(field)) {
2438 flags |= HIST_FIELD_FL_STRING;
2440 hist_field->size = MAX_FILTER_STR_VAL;
2441 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2442 if (!hist_field->type)
2445 if (field->filter_type == FILTER_STATIC_STRING)
2446 hist_field->fn = hist_field_string;
2447 else if (field->filter_type == FILTER_DYN_STRING)
2448 hist_field->fn = hist_field_dynstring;
2450 hist_field->fn = hist_field_pstring;
2452 hist_field->size = field->size;
2453 hist_field->is_signed = field->is_signed;
2454 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2455 if (!hist_field->type)
2458 hist_field->fn = select_value_fn(field->size,
2460 if (!hist_field->fn) {
2461 destroy_hist_field(hist_field, 0);
2466 hist_field->field = field;
2467 hist_field->flags = flags;
2470 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2471 if (!hist_field->var.name)
2477 destroy_hist_field(hist_field, 0);
2481 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2485 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2486 if (hist_data->fields[i]) {
2487 destroy_hist_field(hist_data->fields[i], 0);
2488 hist_data->fields[i] = NULL;
2492 for (i = 0; i < hist_data->n_var_refs; i++) {
2493 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2494 __destroy_hist_field(hist_data->var_refs[i]);
2495 hist_data->var_refs[i] = NULL;
2499 static int init_var_ref(struct hist_field *ref_field,
2500 struct hist_field *var_field,
2501 char *system, char *event_name)
2505 ref_field->var.idx = var_field->var.idx;
2506 ref_field->var.hist_data = var_field->hist_data;
2507 ref_field->size = var_field->size;
2508 ref_field->is_signed = var_field->is_signed;
2509 ref_field->flags |= var_field->flags &
2510 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2513 ref_field->system = kstrdup(system, GFP_KERNEL);
2514 if (!ref_field->system)
2519 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2520 if (!ref_field->event_name) {
2526 if (var_field->var.name) {
2527 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2528 if (!ref_field->name) {
2532 } else if (var_field->name) {
2533 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2534 if (!ref_field->name) {
2540 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2541 if (!ref_field->type) {
2548 kfree(ref_field->system);
2549 kfree(ref_field->event_name);
2550 kfree(ref_field->name);
2556 * create_var_ref - Create a variable reference and attach it to trigger
2557 * @hist_data: The trigger that will be referencing the variable
2558 * @var_field: The VAR field to create a reference to
2559 * @system: The optional system string
2560 * @event_name: The optional event_name string
2562 * Given a variable hist_field, create a VAR_REF hist_field that
2563 * represents a reference to it.
2565 * This function also adds the reference to the trigger that
2566 * now references the variable.
2568 * Return: The VAR_REF field if successful, NULL if not
2570 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2571 struct hist_field *var_field,
2572 char *system, char *event_name)
2574 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2575 struct hist_field *ref_field;
2577 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2579 if (init_var_ref(ref_field, var_field, system, event_name)) {
2580 destroy_hist_field(ref_field, 0);
2584 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2585 ref_field->var_ref_idx = hist_data->n_var_refs++;
2591 static bool is_var_ref(char *var_name)
2593 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2599 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2605 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2606 name = hist_data->attrs->var_defs.name[i];
2608 if (strcmp(var_name, name) == 0) {
2609 field = hist_data->attrs->var_defs.expr[i];
2610 if (contains_operator(field) || is_var_ref(field))
2619 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2620 char *system, char *event_name,
2623 struct trace_event_call *call;
2625 if (system && event_name) {
2626 call = hist_data->event_file->event_call;
2628 if (strcmp(system, call->class->system) != 0)
2631 if (strcmp(event_name, trace_event_name(call)) != 0)
2635 if (!!system != !!event_name)
2638 if (!is_var_ref(var_name))
2643 return field_name_from_var(hist_data, var_name);
2646 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2647 char *system, char *event_name,
2650 struct hist_field *var_field = NULL, *ref_field = NULL;
2652 if (!is_var_ref(var_name))
2657 var_field = find_event_var(hist_data, system, event_name, var_name);
2659 ref_field = create_var_ref(hist_data, var_field,
2660 system, event_name);
2663 hist_err_event("Couldn't find variable: $",
2664 system, event_name, var_name);
2669 static struct ftrace_event_field *
2670 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2671 char *field_str, unsigned long *flags)
2673 struct ftrace_event_field *field = NULL;
2674 char *field_name, *modifier, *str;
2676 modifier = str = kstrdup(field_str, GFP_KERNEL);
2678 return ERR_PTR(-ENOMEM);
2680 field_name = strsep(&modifier, ".");
2682 if (strcmp(modifier, "hex") == 0)
2683 *flags |= HIST_FIELD_FL_HEX;
2684 else if (strcmp(modifier, "sym") == 0)
2685 *flags |= HIST_FIELD_FL_SYM;
2686 else if (strcmp(modifier, "sym-offset") == 0)
2687 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2688 else if ((strcmp(modifier, "execname") == 0) &&
2689 (strcmp(field_name, "common_pid") == 0))
2690 *flags |= HIST_FIELD_FL_EXECNAME;
2691 else if (strcmp(modifier, "syscall") == 0)
2692 *flags |= HIST_FIELD_FL_SYSCALL;
2693 else if (strcmp(modifier, "log2") == 0)
2694 *flags |= HIST_FIELD_FL_LOG2;
2695 else if (strcmp(modifier, "usecs") == 0)
2696 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2698 hist_err("Invalid field modifier: ", modifier);
2699 field = ERR_PTR(-EINVAL);
2704 if (strcmp(field_name, "common_timestamp") == 0) {
2705 *flags |= HIST_FIELD_FL_TIMESTAMP;
2706 hist_data->enable_timestamps = true;
2707 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2708 hist_data->attrs->ts_in_usecs = true;
2709 } else if (strcmp(field_name, "cpu") == 0)
2710 *flags |= HIST_FIELD_FL_CPU;
2712 field = trace_find_event_field(file->event_call, field_name);
2713 if (!field || !field->size) {
2714 hist_err("Couldn't find field: ", field_name);
2715 field = ERR_PTR(-EINVAL);
2725 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2726 struct hist_field *var_ref,
2729 struct hist_field *alias = NULL;
2730 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2732 alias = create_hist_field(hist_data, NULL, flags, var_name);
2736 alias->fn = var_ref->fn;
2737 alias->operands[0] = var_ref;
2739 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2740 destroy_hist_field(alias, 0);
2747 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2748 struct trace_event_file *file, char *str,
2749 unsigned long *flags, char *var_name)
2751 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2752 struct ftrace_event_field *field = NULL;
2753 struct hist_field *hist_field = NULL;
2756 s = strchr(str, '.');
2758 s = strchr(++s, '.');
2760 ref_system = strsep(&str, ".");
2765 ref_event = strsep(&str, ".");
2774 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2776 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2779 hist_field = create_alias(hist_data, hist_field, var_name);
2790 field = parse_field(hist_data, file, str, flags);
2791 if (IS_ERR(field)) {
2792 ret = PTR_ERR(field);
2796 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2804 return ERR_PTR(ret);
2807 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2808 struct trace_event_file *file,
2809 char *str, unsigned long flags,
2810 char *var_name, unsigned int level);
2812 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2813 struct trace_event_file *file,
2814 char *str, unsigned long flags,
2815 char *var_name, unsigned int level)
2817 struct hist_field *operand1, *expr = NULL;
2818 unsigned long operand_flags;
2822 /* we support only -(xxx) i.e. explicit parens required */
2825 hist_err("Too many subexpressions (3 max): ", str);
2830 str++; /* skip leading '-' */
2832 s = strchr(str, '(');
2840 s = strrchr(str, ')');
2844 ret = -EINVAL; /* no closing ')' */
2848 flags |= HIST_FIELD_FL_EXPR;
2849 expr = create_hist_field(hist_data, NULL, flags, var_name);
2856 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2857 if (IS_ERR(operand1)) {
2858 ret = PTR_ERR(operand1);
2862 expr->flags |= operand1->flags &
2863 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2864 expr->fn = hist_field_unary_minus;
2865 expr->operands[0] = operand1;
2866 expr->operator = FIELD_OP_UNARY_MINUS;
2867 expr->name = expr_str(expr, 0);
2868 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2876 destroy_hist_field(expr, 0);
2877 return ERR_PTR(ret);
2880 static int check_expr_operands(struct hist_field *operand1,
2881 struct hist_field *operand2)
2883 unsigned long operand1_flags = operand1->flags;
2884 unsigned long operand2_flags = operand2->flags;
2886 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2887 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2888 struct hist_field *var;
2890 var = find_var_field(operand1->var.hist_data, operand1->name);
2893 operand1_flags = var->flags;
2896 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2897 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2898 struct hist_field *var;
2900 var = find_var_field(operand2->var.hist_data, operand2->name);
2903 operand2_flags = var->flags;
2906 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2907 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2908 hist_err("Timestamp units in expression don't match", NULL);
2915 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2916 struct trace_event_file *file,
2917 char *str, unsigned long flags,
2918 char *var_name, unsigned int level)
2920 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2921 unsigned long operand_flags;
2922 int field_op, ret = -EINVAL;
2923 char *sep, *operand1_str;
2926 hist_err("Too many subexpressions (3 max): ", str);
2927 return ERR_PTR(-EINVAL);
2930 field_op = contains_operator(str);
2932 if (field_op == FIELD_OP_NONE)
2933 return parse_atom(hist_data, file, str, &flags, var_name);
2935 if (field_op == FIELD_OP_UNARY_MINUS)
2936 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2939 case FIELD_OP_MINUS:
2949 operand1_str = strsep(&str, sep);
2950 if (!operand1_str || !str)
2954 operand1 = parse_atom(hist_data, file, operand1_str,
2955 &operand_flags, NULL);
2956 if (IS_ERR(operand1)) {
2957 ret = PTR_ERR(operand1);
2962 /* rest of string could be another expression e.g. b+c in a+b+c */
2964 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2965 if (IS_ERR(operand2)) {
2966 ret = PTR_ERR(operand2);
2971 ret = check_expr_operands(operand1, operand2);
2975 flags |= HIST_FIELD_FL_EXPR;
2977 flags |= operand1->flags &
2978 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2980 expr = create_hist_field(hist_data, NULL, flags, var_name);
2986 operand1->read_once = true;
2987 operand2->read_once = true;
2989 expr->operands[0] = operand1;
2990 expr->operands[1] = operand2;
2991 expr->operator = field_op;
2992 expr->name = expr_str(expr, 0);
2993 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3000 case FIELD_OP_MINUS:
3001 expr->fn = hist_field_minus;
3004 expr->fn = hist_field_plus;
3013 destroy_hist_field(operand1, 0);
3014 destroy_hist_field(operand2, 0);
3015 destroy_hist_field(expr, 0);
3017 return ERR_PTR(ret);
3020 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3021 struct trace_event_file *file)
3023 struct event_trigger_data *test;
3025 list_for_each_entry_rcu(test, &file->triggers, list) {
3026 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3027 if (test->private_data == hist_data)
3028 return test->filter_str;
3035 static struct event_command trigger_hist_cmd;
3036 static int event_hist_trigger_func(struct event_command *cmd_ops,
3037 struct trace_event_file *file,
3038 char *glob, char *cmd, char *param);
3040 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3041 struct hist_trigger_data *hist_data,
3042 unsigned int n_keys)
3044 struct hist_field *target_hist_field, *hist_field;
3045 unsigned int n, i, j;
3047 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3050 i = hist_data->n_vals;
3051 j = target_hist_data->n_vals;
3053 for (n = 0; n < n_keys; n++) {
3054 hist_field = hist_data->fields[i + n];
3055 target_hist_field = target_hist_data->fields[j + n];
3057 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3059 if (hist_field->size != target_hist_field->size)
3061 if (hist_field->is_signed != target_hist_field->is_signed)
3068 static struct hist_trigger_data *
3069 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3070 struct trace_event_file *file)
3072 struct hist_trigger_data *hist_data;
3073 struct event_trigger_data *test;
3074 unsigned int n_keys;
3076 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3078 list_for_each_entry_rcu(test, &file->triggers, list) {
3079 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3080 hist_data = test->private_data;
3082 if (compatible_keys(target_hist_data, hist_data, n_keys))
3090 static struct trace_event_file *event_file(struct trace_array *tr,
3091 char *system, char *event_name)
3093 struct trace_event_file *file;
3095 file = __find_event_file(tr, system, event_name);
3097 return ERR_PTR(-EINVAL);
3102 static struct hist_field *
3103 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3104 char *system, char *event_name, char *field_name)
3106 struct hist_field *event_var;
3107 char *synthetic_name;
3109 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3110 if (!synthetic_name)
3111 return ERR_PTR(-ENOMEM);
3113 strcpy(synthetic_name, "synthetic_");
3114 strcat(synthetic_name, field_name);
3116 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3118 kfree(synthetic_name);
3124 * create_field_var_hist - Automatically create a histogram and var for a field
3125 * @target_hist_data: The target hist trigger
3126 * @subsys_name: Optional subsystem name
3127 * @event_name: Optional event name
3128 * @field_name: The name of the field (and the resulting variable)
3130 * Hist trigger actions fetch data from variables, not directly from
3131 * events. However, for convenience, users are allowed to directly
3132 * specify an event field in an action, which will be automatically
3133 * converted into a variable on their behalf.
3135 * If a user specifies a field on an event that isn't the event the
3136 * histogram currently being defined (the target event histogram), the
3137 * only way that can be accomplished is if a new hist trigger is
3138 * created and the field variable defined on that.
3140 * This function creates a new histogram compatible with the target
3141 * event (meaning a histogram with the same key as the target
3142 * histogram), and creates a variable for the specified field, but
3143 * with 'synthetic_' prepended to the variable name in order to avoid
3144 * collision with normal field variables.
3146 * Return: The variable created for the field.
3148 static struct hist_field *
3149 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3150 char *subsys_name, char *event_name, char *field_name)
3152 struct trace_array *tr = target_hist_data->event_file->tr;
3153 struct hist_field *event_var = ERR_PTR(-EINVAL);
3154 struct hist_trigger_data *hist_data;
3155 unsigned int i, n, first = true;
3156 struct field_var_hist *var_hist;
3157 struct trace_event_file *file;
3158 struct hist_field *key_field;
3163 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3164 hist_err_event("trace action: Too many field variables defined: ",
3165 subsys_name, event_name, field_name);
3166 return ERR_PTR(-EINVAL);
3169 file = event_file(tr, subsys_name, event_name);
3172 hist_err_event("trace action: Event file not found: ",
3173 subsys_name, event_name, field_name);
3174 ret = PTR_ERR(file);
3175 return ERR_PTR(ret);
3179 * Look for a histogram compatible with target. We'll use the
3180 * found histogram specification to create a new matching
3181 * histogram with our variable on it. target_hist_data is not
3182 * yet a registered histogram so we can't use that.
3184 hist_data = find_compatible_hist(target_hist_data, file);
3186 hist_err_event("trace action: Matching event histogram not found: ",
3187 subsys_name, event_name, field_name);
3188 return ERR_PTR(-EINVAL);
3191 /* See if a synthetic field variable has already been created */
3192 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3193 event_name, field_name);
3194 if (!IS_ERR_OR_NULL(event_var))
3197 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3199 return ERR_PTR(-ENOMEM);
3201 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3204 return ERR_PTR(-ENOMEM);
3207 /* Use the same keys as the compatible histogram */
3208 strcat(cmd, "keys=");
3210 for_each_hist_key_field(i, hist_data) {
3211 key_field = hist_data->fields[i];
3214 strcat(cmd, key_field->field->name);
3218 /* Create the synthetic field variable specification */
3219 strcat(cmd, ":synthetic_");
3220 strcat(cmd, field_name);
3222 strcat(cmd, field_name);
3224 /* Use the same filter as the compatible histogram */
3225 saved_filter = find_trigger_filter(hist_data, file);
3227 strcat(cmd, " if ");
3228 strcat(cmd, saved_filter);
3231 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3232 if (!var_hist->cmd) {
3235 return ERR_PTR(-ENOMEM);
3238 /* Save the compatible histogram information */
3239 var_hist->hist_data = hist_data;
3241 /* Create the new histogram with our variable */
3242 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3246 kfree(var_hist->cmd);
3248 hist_err_event("trace action: Couldn't create histogram for field: ",
3249 subsys_name, event_name, field_name);
3250 return ERR_PTR(ret);
3255 /* If we can't find the variable, something went wrong */
3256 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3257 event_name, field_name);
3258 if (IS_ERR_OR_NULL(event_var)) {
3259 kfree(var_hist->cmd);
3261 hist_err_event("trace action: Couldn't find synthetic variable: ",
3262 subsys_name, event_name, field_name);
3263 return ERR_PTR(-EINVAL);
3266 n = target_hist_data->n_field_var_hists;
3267 target_hist_data->field_var_hists[n] = var_hist;
3268 target_hist_data->n_field_var_hists++;
3273 static struct hist_field *
3274 find_target_event_var(struct hist_trigger_data *hist_data,
3275 char *subsys_name, char *event_name, char *var_name)
3277 struct trace_event_file *file = hist_data->event_file;
3278 struct hist_field *hist_field = NULL;
3281 struct trace_event_call *call;
3286 call = file->event_call;
3288 if (strcmp(subsys_name, call->class->system) != 0)
3291 if (strcmp(event_name, trace_event_name(call)) != 0)
3295 hist_field = find_var_field(hist_data, var_name);
3300 static inline void __update_field_vars(struct tracing_map_elt *elt,
3301 struct ring_buffer_event *rbe,
3303 struct field_var **field_vars,
3304 unsigned int n_field_vars,
3305 unsigned int field_var_str_start)
3307 struct hist_elt_data *elt_data = elt->private_data;
3308 unsigned int i, j, var_idx;
3311 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3312 struct field_var *field_var = field_vars[i];
3313 struct hist_field *var = field_var->var;
3314 struct hist_field *val = field_var->val;
3316 var_val = val->fn(val, elt, rbe, rec);
3317 var_idx = var->var.idx;
3319 if (val->flags & HIST_FIELD_FL_STRING) {
3320 char *str = elt_data->field_var_str[j++];
3321 char *val_str = (char *)(uintptr_t)var_val;
3323 strscpy(str, val_str, STR_VAR_LEN_MAX);
3324 var_val = (u64)(uintptr_t)str;
3326 tracing_map_set_var(elt, var_idx, var_val);
3330 static void update_field_vars(struct hist_trigger_data *hist_data,
3331 struct tracing_map_elt *elt,
3332 struct ring_buffer_event *rbe,
3335 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3336 hist_data->n_field_vars, 0);
3339 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3340 struct tracing_map_elt *elt, void *rec,
3341 struct ring_buffer_event *rbe, void *key,
3342 struct action_data *data, u64 *var_ref_vals)
3344 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
3345 hist_data->n_save_vars, hist_data->n_field_var_str);
3348 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3349 struct trace_event_file *file,
3350 char *name, int size, const char *type)
3352 struct hist_field *var;
3355 if (find_var(hist_data, file, name) && !hist_data->remove) {
3356 var = ERR_PTR(-EINVAL);
3360 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3362 var = ERR_PTR(-ENOMEM);
3366 idx = tracing_map_add_var(hist_data->map);
3369 var = ERR_PTR(-EINVAL);
3373 var->flags = HIST_FIELD_FL_VAR;
3375 var->var.hist_data = var->hist_data = hist_data;
3377 var->var.name = kstrdup(name, GFP_KERNEL);
3378 var->type = kstrdup(type, GFP_KERNEL);
3379 if (!var->var.name || !var->type) {
3380 kfree(var->var.name);
3383 var = ERR_PTR(-ENOMEM);
3389 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3390 struct trace_event_file *file,
3393 struct hist_field *val = NULL, *var = NULL;
3394 unsigned long flags = HIST_FIELD_FL_VAR;
3395 struct field_var *field_var;
3398 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3399 hist_err("Too many field variables defined: ", field_name);
3404 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3406 hist_err("Couldn't parse field variable: ", field_name);
3411 var = create_var(hist_data, file, field_name, val->size, val->type);
3413 hist_err("Couldn't create or find variable: ", field_name);
3419 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3427 field_var->var = var;
3428 field_var->val = val;
3432 field_var = ERR_PTR(ret);
3437 * create_target_field_var - Automatically create a variable for a field
3438 * @target_hist_data: The target hist trigger
3439 * @subsys_name: Optional subsystem name
3440 * @event_name: Optional event name
3441 * @var_name: The name of the field (and the resulting variable)
3443 * Hist trigger actions fetch data from variables, not directly from
3444 * events. However, for convenience, users are allowed to directly
3445 * specify an event field in an action, which will be automatically
3446 * converted into a variable on their behalf.
3448 * This function creates a field variable with the name var_name on
3449 * the hist trigger currently being defined on the target event. If
3450 * subsys_name and event_name are specified, this function simply
3451 * verifies that they do in fact match the target event subsystem and
3454 * Return: The variable created for the field.
3456 static struct field_var *
3457 create_target_field_var(struct hist_trigger_data *target_hist_data,
3458 char *subsys_name, char *event_name, char *var_name)
3460 struct trace_event_file *file = target_hist_data->event_file;
3463 struct trace_event_call *call;
3468 call = file->event_call;
3470 if (strcmp(subsys_name, call->class->system) != 0)
3473 if (strcmp(event_name, trace_event_name(call)) != 0)
3477 return create_field_var(target_hist_data, file, var_name);
3480 static bool check_track_val_max(u64 track_val, u64 var_val)
3482 if (var_val <= track_val)
3488 static bool check_track_val_changed(u64 track_val, u64 var_val)
3490 if (var_val == track_val)
3496 static u64 get_track_val(struct hist_trigger_data *hist_data,
3497 struct tracing_map_elt *elt,
3498 struct action_data *data)
3500 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3503 track_val = tracing_map_read_var(elt, track_var_idx);
3508 static void save_track_val(struct hist_trigger_data *hist_data,
3509 struct tracing_map_elt *elt,
3510 struct action_data *data, u64 var_val)
3512 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3514 tracing_map_set_var(elt, track_var_idx, var_val);
3517 static void save_track_data(struct hist_trigger_data *hist_data,
3518 struct tracing_map_elt *elt, void *rec,
3519 struct ring_buffer_event *rbe, void *key,
3520 struct action_data *data, u64 *var_ref_vals)
3522 if (data->track_data.save_data)
3523 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3526 static bool check_track_val(struct tracing_map_elt *elt,
3527 struct action_data *data,
3530 struct hist_trigger_data *hist_data;
3533 hist_data = data->track_data.track_var->hist_data;
3534 track_val = get_track_val(hist_data, elt, data);
3536 return data->track_data.check_val(track_val, var_val);
3539 #ifdef CONFIG_TRACER_SNAPSHOT
3540 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3542 /* called with tr->max_lock held */
3543 struct track_data *track_data = tr->cond_snapshot->cond_data;
3544 struct hist_elt_data *elt_data, *track_elt_data;
3545 struct snapshot_context *context = cond_data;
3551 track_val = get_track_val(track_data->hist_data, context->elt,
3552 track_data->action_data);
3554 track_data->track_val = track_val;
3555 memcpy(track_data->key, context->key, track_data->key_len);
3557 elt_data = context->elt->private_data;
3558 track_elt_data = track_data->elt.private_data;
3560 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3562 track_data->updated = true;
3567 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3568 struct tracing_map_elt *elt, void *rec,
3569 struct ring_buffer_event *rbe, void *key,
3570 struct action_data *data,
3573 struct trace_event_file *file = hist_data->event_file;
3574 struct snapshot_context context;
3579 tracing_snapshot_cond(file->tr, &context);
3582 static void hist_trigger_print_key(struct seq_file *m,
3583 struct hist_trigger_data *hist_data,
3585 struct tracing_map_elt *elt);
3587 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3591 if (!hist_data->n_actions)
3594 for (i = 0; i < hist_data->n_actions; i++) {
3595 struct action_data *data = hist_data->actions[i];
3597 if (data->action == ACTION_SNAPSHOT)
3604 static void track_data_snapshot_print(struct seq_file *m,
3605 struct hist_trigger_data *hist_data)
3607 struct trace_event_file *file = hist_data->event_file;
3608 struct track_data *track_data;
3609 struct action_data *action;
3611 track_data = tracing_cond_snapshot_data(file->tr);
3615 if (!track_data->updated)
3618 action = snapshot_action(hist_data);
3622 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3623 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3624 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3625 action->track_data.var_str, track_data->track_val);
3627 seq_puts(m, "\ttriggered by event with key: ");
3628 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3632 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3636 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3637 struct tracing_map_elt *elt, void *rec,
3638 struct ring_buffer_event *rbe, void *key,
3639 struct action_data *data,
3640 u64 *var_ref_vals) {}
3641 static void track_data_snapshot_print(struct seq_file *m,
3642 struct hist_trigger_data *hist_data) {}
3643 #endif /* CONFIG_TRACER_SNAPSHOT */
3645 static void track_data_print(struct seq_file *m,
3646 struct hist_trigger_data *hist_data,
3647 struct tracing_map_elt *elt,
3648 struct action_data *data)
3650 u64 track_val = get_track_val(hist_data, elt, data);
3651 unsigned int i, save_var_idx;
3653 if (data->handler == HANDLER_ONMAX)
3654 seq_printf(m, "\n\tmax: %10llu", track_val);
3655 else if (data->handler == HANDLER_ONCHANGE)
3656 seq_printf(m, "\n\tchanged: %10llu", track_val);
3658 if (data->action == ACTION_SNAPSHOT)
3661 for (i = 0; i < hist_data->n_save_vars; i++) {
3662 struct hist_field *save_val = hist_data->save_vars[i]->val;
3663 struct hist_field *save_var = hist_data->save_vars[i]->var;
3666 save_var_idx = save_var->var.idx;
3668 val = tracing_map_read_var(elt, save_var_idx);
3670 if (save_val->flags & HIST_FIELD_FL_STRING) {
3671 seq_printf(m, " %s: %-32s", save_var->var.name,
3672 (char *)(uintptr_t)(val));
3674 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3678 static void ontrack_action(struct hist_trigger_data *hist_data,
3679 struct tracing_map_elt *elt, void *rec,
3680 struct ring_buffer_event *rbe, void *key,
3681 struct action_data *data, u64 *var_ref_vals)
3683 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3685 if (check_track_val(elt, data, var_val)) {
3686 save_track_val(hist_data, elt, data, var_val);
3687 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3691 static void action_data_destroy(struct action_data *data)
3695 lockdep_assert_held(&event_mutex);
3697 kfree(data->action_name);
3699 for (i = 0; i < data->n_params; i++)
3700 kfree(data->params[i]);
3702 if (data->synth_event)
3703 data->synth_event->ref--;
3705 kfree(data->synth_event_name);
3710 static void track_data_destroy(struct hist_trigger_data *hist_data,
3711 struct action_data *data)
3713 struct trace_event_file *file = hist_data->event_file;
3715 destroy_hist_field(data->track_data.track_var, 0);
3717 if (data->action == ACTION_SNAPSHOT) {
3718 struct track_data *track_data;
3720 track_data = tracing_cond_snapshot_data(file->tr);
3721 if (track_data && track_data->hist_data == hist_data) {
3722 tracing_snapshot_cond_disable(file->tr);
3723 track_data_free(track_data);
3727 kfree(data->track_data.var_str);
3729 action_data_destroy(data);
3732 static int action_create(struct hist_trigger_data *hist_data,
3733 struct action_data *data);
3735 static int track_data_create(struct hist_trigger_data *hist_data,
3736 struct action_data *data)
3738 struct hist_field *var_field, *ref_field, *track_var = NULL;
3739 struct trace_event_file *file = hist_data->event_file;
3740 char *track_data_var_str;
3743 track_data_var_str = data->track_data.var_str;
3744 if (track_data_var_str[0] != '$') {
3745 hist_err("For onmax(x) or onchange(x), x must be a variable: ", track_data_var_str);
3748 track_data_var_str++;
3750 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3752 hist_err("Couldn't find onmax or onchange variable: ", track_data_var_str);
3756 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3760 data->track_data.var_ref = ref_field;
3762 if (data->handler == HANDLER_ONMAX)
3763 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3764 if (IS_ERR(track_var)) {
3765 hist_err("Couldn't create onmax variable: ", "__max");
3766 ret = PTR_ERR(track_var);
3770 if (data->handler == HANDLER_ONCHANGE)
3771 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3772 if (IS_ERR(track_var)) {
3773 hist_err("Couldn't create onchange variable: ", "__change");
3774 ret = PTR_ERR(track_var);
3777 data->track_data.track_var = track_var;
3779 ret = action_create(hist_data, data);
3784 static int parse_action_params(char *params, struct action_data *data)
3786 char *param, *saved_param;
3787 bool first_param = true;
3791 if (data->n_params >= SYNTH_FIELDS_MAX) {
3792 hist_err("Too many action params", "");
3796 param = strsep(¶ms, ",");
3798 hist_err("No action param found", "");
3803 param = strstrip(param);
3804 if (strlen(param) < 2) {
3805 hist_err("Invalid action param: ", param);
3810 saved_param = kstrdup(param, GFP_KERNEL);
3816 if (first_param && data->use_trace_keyword) {
3817 data->synth_event_name = saved_param;
3818 first_param = false;
3821 first_param = false;
3823 data->params[data->n_params++] = saved_param;
3829 static int action_parse(char *str, struct action_data *data,
3830 enum handler_id handler)
3837 hist_err("action parsing: No action found", "");
3842 action_name = strsep(&str, "(");
3843 if (!action_name || !str) {
3844 hist_err("action parsing: No action found", "");
3849 if (str_has_prefix(action_name, "save")) {
3850 char *params = strsep(&str, ")");
3853 hist_err("action parsing: No params found for %s", "save");
3858 ret = parse_action_params(params, data);
3862 if (handler == HANDLER_ONMAX)
3863 data->track_data.check_val = check_track_val_max;
3864 else if (handler == HANDLER_ONCHANGE)
3865 data->track_data.check_val = check_track_val_changed;
3867 hist_err("action parsing: Handler doesn't support action: ", action_name);
3872 data->track_data.save_data = save_track_data_vars;
3873 data->fn = ontrack_action;
3874 data->action = ACTION_SAVE;
3875 } else if (str_has_prefix(action_name, "snapshot")) {
3876 char *params = strsep(&str, ")");
3879 hist_err("action parsing: No closing paren found: %s", params);
3884 if (handler == HANDLER_ONMAX)
3885 data->track_data.check_val = check_track_val_max;
3886 else if (handler == HANDLER_ONCHANGE)
3887 data->track_data.check_val = check_track_val_changed;
3889 hist_err("action parsing: Handler doesn't support action: ", action_name);
3894 data->track_data.save_data = save_track_data_snapshot;
3895 data->fn = ontrack_action;
3896 data->action = ACTION_SNAPSHOT;
3898 char *params = strsep(&str, ")");
3900 if (str_has_prefix(action_name, "trace"))
3901 data->use_trace_keyword = true;
3904 ret = parse_action_params(params, data);
3909 if (handler == HANDLER_ONMAX)
3910 data->track_data.check_val = check_track_val_max;
3911 else if (handler == HANDLER_ONCHANGE)
3912 data->track_data.check_val = check_track_val_changed;
3914 if (handler != HANDLER_ONMATCH) {
3915 data->track_data.save_data = action_trace;
3916 data->fn = ontrack_action;
3918 data->fn = action_trace;
3920 data->action = ACTION_TRACE;
3923 data->action_name = kstrdup(action_name, GFP_KERNEL);
3924 if (!data->action_name) {
3929 data->handler = handler;
3934 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3935 char *str, enum handler_id handler)
3937 struct action_data *data;
3941 data = kzalloc(sizeof(*data), GFP_KERNEL);
3943 return ERR_PTR(-ENOMEM);
3945 var_str = strsep(&str, ")");
3946 if (!var_str || !str) {
3951 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3952 if (!data->track_data.var_str) {
3957 ret = action_parse(str, data, handler);
3963 track_data_destroy(hist_data, data);
3964 data = ERR_PTR(ret);
3968 static void onmatch_destroy(struct action_data *data)
3970 kfree(data->match_data.event);
3971 kfree(data->match_data.event_system);
3973 action_data_destroy(data);
3976 static void destroy_field_var(struct field_var *field_var)
3981 destroy_hist_field(field_var->var, 0);
3982 destroy_hist_field(field_var->val, 0);
3987 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3991 for (i = 0; i < hist_data->n_field_vars; i++)
3992 destroy_field_var(hist_data->field_vars[i]);
3995 static void save_field_var(struct hist_trigger_data *hist_data,
3996 struct field_var *field_var)
3998 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4000 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4001 hist_data->n_field_var_str++;
4005 static int check_synth_field(struct synth_event *event,
4006 struct hist_field *hist_field,
4007 unsigned int field_pos)
4009 struct synth_field *field;
4011 if (field_pos >= event->n_fields)
4014 field = event->fields[field_pos];
4016 if (strcmp(field->type, hist_field->type) != 0)
4022 static struct hist_field *
4023 trace_action_find_var(struct hist_trigger_data *hist_data,
4024 struct action_data *data,
4025 char *system, char *event, char *var)
4027 struct hist_field *hist_field;
4029 var++; /* skip '$' */
4031 hist_field = find_target_event_var(hist_data, system, event, var);
4033 if (!system && data->handler == HANDLER_ONMATCH) {
4034 system = data->match_data.event_system;
4035 event = data->match_data.event;
4038 hist_field = find_event_var(hist_data, system, event, var);
4042 hist_err_event("trace action: Couldn't find param: $", system, event, var);
4047 static struct hist_field *
4048 trace_action_create_field_var(struct hist_trigger_data *hist_data,
4049 struct action_data *data, char *system,
4050 char *event, char *var)
4052 struct hist_field *hist_field = NULL;
4053 struct field_var *field_var;
4056 * First try to create a field var on the target event (the
4057 * currently being defined). This will create a variable for
4058 * unqualified fields on the target event, or if qualified,
4059 * target fields that have qualified names matching the target.
4061 field_var = create_target_field_var(hist_data, system, event, var);
4063 if (field_var && !IS_ERR(field_var)) {
4064 save_field_var(hist_data, field_var);
4065 hist_field = field_var->var;
4069 * If no explicit system.event is specfied, default to
4070 * looking for fields on the onmatch(system.event.xxx)
4073 if (!system && data->handler == HANDLER_ONMATCH) {
4074 system = data->match_data.event_system;
4075 event = data->match_data.event;
4079 * At this point, we're looking at a field on another
4080 * event. Because we can't modify a hist trigger on
4081 * another event to add a variable for a field, we need
4082 * to create a new trigger on that event and create the
4083 * variable at the same time.
4085 hist_field = create_field_var_hist(hist_data, system, event, var);
4086 if (IS_ERR(hist_field))
4092 destroy_field_var(field_var);
4097 static int trace_action_create(struct hist_trigger_data *hist_data,
4098 struct action_data *data)
4100 char *event_name, *param, *system = NULL;
4101 struct hist_field *hist_field, *var_ref;
4102 unsigned int i, var_ref_idx;
4103 unsigned int field_pos = 0;
4104 struct synth_event *event;
4105 char *synth_event_name;
4108 lockdep_assert_held(&event_mutex);
4110 if (data->use_trace_keyword)
4111 synth_event_name = data->synth_event_name;
4113 synth_event_name = data->action_name;
4115 event = find_synth_event(synth_event_name);
4117 hist_err("trace action: Couldn't find synthetic event: ", synth_event_name);
4123 var_ref_idx = hist_data->n_var_refs;
4125 for (i = 0; i < data->n_params; i++) {
4128 p = param = kstrdup(data->params[i], GFP_KERNEL);
4134 system = strsep(¶m, ".");
4136 param = (char *)system;
4137 system = event_name = NULL;
4139 event_name = strsep(¶m, ".");
4147 if (param[0] == '$')
4148 hist_field = trace_action_find_var(hist_data, data,
4152 hist_field = trace_action_create_field_var(hist_data,
4164 if (check_synth_field(event, hist_field, field_pos) == 0) {
4165 var_ref = create_var_ref(hist_data, hist_field,
4166 system, event_name);
4178 hist_err_event("trace action: Param type doesn't match synthetic event field type: ",
4179 system, event_name, param);
4185 if (field_pos != event->n_fields) {
4186 hist_err("trace action: Param count doesn't match synthetic event field count: ", event->name);
4191 data->synth_event = event;
4192 data->var_ref_idx = var_ref_idx;
4201 static int action_create(struct hist_trigger_data *hist_data,
4202 struct action_data *data)
4204 struct trace_event_file *file = hist_data->event_file;
4205 struct track_data *track_data;
4206 struct field_var *field_var;
4211 if (data->action == ACTION_TRACE)
4212 return trace_action_create(hist_data, data);
4214 if (data->action == ACTION_SNAPSHOT) {
4215 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4216 if (IS_ERR(track_data)) {
4217 ret = PTR_ERR(track_data);
4221 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4222 cond_snapshot_update);
4224 track_data_free(track_data);
4229 if (data->action == ACTION_SAVE) {
4230 if (hist_data->n_save_vars) {
4232 hist_err("save action: Can't have more than one save() action per hist", "");
4236 for (i = 0; i < data->n_params; i++) {
4237 param = kstrdup(data->params[i], GFP_KERNEL);
4243 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4244 if (IS_ERR(field_var)) {
4245 hist_err("save action: Couldn't create field variable: ", param);
4246 ret = PTR_ERR(field_var);
4251 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4252 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4253 hist_data->n_save_var_str++;
4261 static int onmatch_create(struct hist_trigger_data *hist_data,
4262 struct action_data *data)
4264 return action_create(hist_data, data);
4267 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4269 char *match_event, *match_event_system;
4270 struct action_data *data;
4273 data = kzalloc(sizeof(*data), GFP_KERNEL);
4275 return ERR_PTR(-ENOMEM);
4277 match_event = strsep(&str, ")");
4278 if (!match_event || !str) {
4279 hist_err("onmatch: Missing closing paren: ", match_event);
4283 match_event_system = strsep(&match_event, ".");
4285 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
4289 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4290 hist_err_event("onmatch: Invalid subsystem or event name: ",
4291 match_event_system, match_event, NULL);
4295 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4296 if (!data->match_data.event) {
4301 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4302 if (!data->match_data.event_system) {
4307 ret = action_parse(str, data, HANDLER_ONMATCH);
4313 onmatch_destroy(data);
4314 data = ERR_PTR(ret);
4318 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4320 hist_data->fields[HITCOUNT_IDX] =
4321 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4322 if (!hist_data->fields[HITCOUNT_IDX])
4325 hist_data->n_vals++;
4326 hist_data->n_fields++;
4328 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4334 static int __create_val_field(struct hist_trigger_data *hist_data,
4335 unsigned int val_idx,
4336 struct trace_event_file *file,
4337 char *var_name, char *field_str,
4338 unsigned long flags)
4340 struct hist_field *hist_field;
4343 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4344 if (IS_ERR(hist_field)) {
4345 ret = PTR_ERR(hist_field);
4349 hist_data->fields[val_idx] = hist_field;
4351 ++hist_data->n_vals;
4352 ++hist_data->n_fields;
4354 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4360 static int create_val_field(struct hist_trigger_data *hist_data,
4361 unsigned int val_idx,
4362 struct trace_event_file *file,
4365 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4368 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4371 static int create_var_field(struct hist_trigger_data *hist_data,
4372 unsigned int val_idx,
4373 struct trace_event_file *file,
4374 char *var_name, char *expr_str)
4376 unsigned long flags = 0;
4378 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4381 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4382 hist_err("Variable already defined: ", var_name);
4386 flags |= HIST_FIELD_FL_VAR;
4387 hist_data->n_vars++;
4388 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4391 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4394 static int create_val_fields(struct hist_trigger_data *hist_data,
4395 struct trace_event_file *file)
4397 char *fields_str, *field_str;
4398 unsigned int i, j = 1;
4401 ret = create_hitcount_val(hist_data);
4405 fields_str = hist_data->attrs->vals_str;
4409 strsep(&fields_str, "=");
4413 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4414 j < TRACING_MAP_VALS_MAX; i++) {
4415 field_str = strsep(&fields_str, ",");
4419 if (strcmp(field_str, "hitcount") == 0)
4422 ret = create_val_field(hist_data, j++, file, field_str);
4427 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4433 static int create_key_field(struct hist_trigger_data *hist_data,
4434 unsigned int key_idx,
4435 unsigned int key_offset,
4436 struct trace_event_file *file,
4439 struct hist_field *hist_field = NULL;
4441 unsigned long flags = 0;
4442 unsigned int key_size;
4445 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4448 flags |= HIST_FIELD_FL_KEY;
4450 if (strcmp(field_str, "stacktrace") == 0) {
4451 flags |= HIST_FIELD_FL_STACKTRACE;
4452 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4453 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4455 hist_field = parse_expr(hist_data, file, field_str, flags,
4457 if (IS_ERR(hist_field)) {
4458 ret = PTR_ERR(hist_field);
4462 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
4463 hist_err("Using variable references as keys not supported: ", field_str);
4464 destroy_hist_field(hist_field, 0);
4469 key_size = hist_field->size;
4472 hist_data->fields[key_idx] = hist_field;
4474 key_size = ALIGN(key_size, sizeof(u64));
4475 hist_data->fields[key_idx]->size = key_size;
4476 hist_data->fields[key_idx]->offset = key_offset;
4478 hist_data->key_size += key_size;
4480 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4485 hist_data->n_keys++;
4486 hist_data->n_fields++;
4488 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4496 static int create_key_fields(struct hist_trigger_data *hist_data,
4497 struct trace_event_file *file)
4499 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4500 char *fields_str, *field_str;
4503 fields_str = hist_data->attrs->keys_str;
4507 strsep(&fields_str, "=");
4511 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4512 field_str = strsep(&fields_str, ",");
4515 ret = create_key_field(hist_data, i, key_offset,
4530 static int create_var_fields(struct hist_trigger_data *hist_data,
4531 struct trace_event_file *file)
4533 unsigned int i, j = hist_data->n_vals;
4536 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4538 for (i = 0; i < n_vars; i++) {
4539 char *var_name = hist_data->attrs->var_defs.name[i];
4540 char *expr = hist_data->attrs->var_defs.expr[i];
4542 ret = create_var_field(hist_data, j++, file, var_name, expr);
4550 static void free_var_defs(struct hist_trigger_data *hist_data)
4554 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4555 kfree(hist_data->attrs->var_defs.name[i]);
4556 kfree(hist_data->attrs->var_defs.expr[i]);
4559 hist_data->attrs->var_defs.n_vars = 0;
4562 static int parse_var_defs(struct hist_trigger_data *hist_data)
4564 char *s, *str, *var_name, *field_str;
4565 unsigned int i, j, n_vars = 0;
4568 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4569 str = hist_data->attrs->assignment_str[i];
4570 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4571 field_str = strsep(&str, ",");
4575 var_name = strsep(&field_str, "=");
4576 if (!var_name || !field_str) {
4577 hist_err("Malformed assignment: ", var_name);
4582 if (n_vars == TRACING_MAP_VARS_MAX) {
4583 hist_err("Too many variables defined: ", var_name);
4588 s = kstrdup(var_name, GFP_KERNEL);
4593 hist_data->attrs->var_defs.name[n_vars] = s;
4595 s = kstrdup(field_str, GFP_KERNEL);
4597 kfree(hist_data->attrs->var_defs.name[n_vars]);
4601 hist_data->attrs->var_defs.expr[n_vars++] = s;
4603 hist_data->attrs->var_defs.n_vars = n_vars;
4609 free_var_defs(hist_data);
4614 static int create_hist_fields(struct hist_trigger_data *hist_data,
4615 struct trace_event_file *file)
4619 ret = parse_var_defs(hist_data);
4623 ret = create_val_fields(hist_data, file);
4627 ret = create_var_fields(hist_data, file);
4631 ret = create_key_fields(hist_data, file);
4635 free_var_defs(hist_data);
4640 static int is_descending(const char *str)
4645 if (strcmp(str, "descending") == 0)
4648 if (strcmp(str, "ascending") == 0)
4654 static int create_sort_keys(struct hist_trigger_data *hist_data)
4656 char *fields_str = hist_data->attrs->sort_key_str;
4657 struct tracing_map_sort_key *sort_key;
4658 int descending, ret = 0;
4659 unsigned int i, j, k;
4661 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4666 strsep(&fields_str, "=");
4672 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4673 struct hist_field *hist_field;
4674 char *field_str, *field_name;
4675 const char *test_name;
4677 sort_key = &hist_data->sort_keys[i];
4679 field_str = strsep(&fields_str, ",");
4686 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4691 field_name = strsep(&field_str, ".");
4697 if (strcmp(field_name, "hitcount") == 0) {
4698 descending = is_descending(field_str);
4699 if (descending < 0) {
4703 sort_key->descending = descending;
4707 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4710 hist_field = hist_data->fields[j];
4711 if (hist_field->flags & HIST_FIELD_FL_VAR)
4716 test_name = hist_field_name(hist_field, 0);
4718 if (strcmp(field_name, test_name) == 0) {
4719 sort_key->field_idx = idx;
4720 descending = is_descending(field_str);
4721 if (descending < 0) {
4725 sort_key->descending = descending;
4729 if (j == hist_data->n_fields) {
4735 hist_data->n_sort_keys = i;
4740 static void destroy_actions(struct hist_trigger_data *hist_data)
4744 for (i = 0; i < hist_data->n_actions; i++) {
4745 struct action_data *data = hist_data->actions[i];
4747 if (data->handler == HANDLER_ONMATCH)
4748 onmatch_destroy(data);
4749 else if (data->handler == HANDLER_ONMAX ||
4750 data->handler == HANDLER_ONCHANGE)
4751 track_data_destroy(hist_data, data);
4757 static int parse_actions(struct hist_trigger_data *hist_data)
4759 struct trace_array *tr = hist_data->event_file->tr;
4760 struct action_data *data;
4766 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4767 str = hist_data->attrs->action_str[i];
4769 if ((len = str_has_prefix(str, "onmatch("))) {
4770 char *action_str = str + len;
4772 data = onmatch_parse(tr, action_str);
4774 ret = PTR_ERR(data);
4777 } else if ((len = str_has_prefix(str, "onmax("))) {
4778 char *action_str = str + len;
4780 data = track_data_parse(hist_data, action_str,
4783 ret = PTR_ERR(data);
4786 } else if ((len = str_has_prefix(str, "onchange("))) {
4787 char *action_str = str + len;
4789 data = track_data_parse(hist_data, action_str,
4792 ret = PTR_ERR(data);
4800 hist_data->actions[hist_data->n_actions++] = data;
4806 static int create_actions(struct hist_trigger_data *hist_data)
4808 struct action_data *data;
4812 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4813 data = hist_data->actions[i];
4815 if (data->handler == HANDLER_ONMATCH) {
4816 ret = onmatch_create(hist_data, data);
4819 } else if (data->handler == HANDLER_ONMAX ||
4820 data->handler == HANDLER_ONCHANGE) {
4821 ret = track_data_create(hist_data, data);
4833 static void print_actions(struct seq_file *m,
4834 struct hist_trigger_data *hist_data,
4835 struct tracing_map_elt *elt)
4839 for (i = 0; i < hist_data->n_actions; i++) {
4840 struct action_data *data = hist_data->actions[i];
4842 if (data->action == ACTION_SNAPSHOT)
4845 if (data->handler == HANDLER_ONMAX ||
4846 data->handler == HANDLER_ONCHANGE)
4847 track_data_print(m, hist_data, elt, data);
4851 static void print_action_spec(struct seq_file *m,
4852 struct hist_trigger_data *hist_data,
4853 struct action_data *data)
4857 if (data->action == ACTION_SAVE) {
4858 for (i = 0; i < hist_data->n_save_vars; i++) {
4859 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4860 if (i < hist_data->n_save_vars - 1)
4863 } else if (data->action == ACTION_TRACE) {
4864 if (data->use_trace_keyword)
4865 seq_printf(m, "%s", data->synth_event_name);
4866 for (i = 0; i < data->n_params; i++) {
4867 if (i || data->use_trace_keyword)
4869 seq_printf(m, "%s", data->params[i]);
4874 static void print_track_data_spec(struct seq_file *m,
4875 struct hist_trigger_data *hist_data,
4876 struct action_data *data)
4878 if (data->handler == HANDLER_ONMAX)
4879 seq_puts(m, ":onmax(");
4880 else if (data->handler == HANDLER_ONCHANGE)
4881 seq_puts(m, ":onchange(");
4882 seq_printf(m, "%s", data->track_data.var_str);
4883 seq_printf(m, ").%s(", data->action_name);
4885 print_action_spec(m, hist_data, data);
4890 static void print_onmatch_spec(struct seq_file *m,
4891 struct hist_trigger_data *hist_data,
4892 struct action_data *data)
4894 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4895 data->match_data.event);
4897 seq_printf(m, "%s(", data->action_name);
4899 print_action_spec(m, hist_data, data);
4904 static bool actions_match(struct hist_trigger_data *hist_data,
4905 struct hist_trigger_data *hist_data_test)
4909 if (hist_data->n_actions != hist_data_test->n_actions)
4912 for (i = 0; i < hist_data->n_actions; i++) {
4913 struct action_data *data = hist_data->actions[i];
4914 struct action_data *data_test = hist_data_test->actions[i];
4915 char *action_name, *action_name_test;
4917 if (data->handler != data_test->handler)
4919 if (data->action != data_test->action)
4922 if (data->n_params != data_test->n_params)
4925 for (j = 0; j < data->n_params; j++) {
4926 if (strcmp(data->params[j], data_test->params[j]) != 0)
4930 if (data->use_trace_keyword)
4931 action_name = data->synth_event_name;
4933 action_name = data->action_name;
4935 if (data_test->use_trace_keyword)
4936 action_name_test = data_test->synth_event_name;
4938 action_name_test = data_test->action_name;
4940 if (strcmp(action_name, action_name_test) != 0)
4943 if (data->handler == HANDLER_ONMATCH) {
4944 if (strcmp(data->match_data.event_system,
4945 data_test->match_data.event_system) != 0)
4947 if (strcmp(data->match_data.event,
4948 data_test->match_data.event) != 0)
4950 } else if (data->handler == HANDLER_ONMAX ||
4951 data->handler == HANDLER_ONCHANGE) {
4952 if (strcmp(data->track_data.var_str,
4953 data_test->track_data.var_str) != 0)
4962 static void print_actions_spec(struct seq_file *m,
4963 struct hist_trigger_data *hist_data)
4967 for (i = 0; i < hist_data->n_actions; i++) {
4968 struct action_data *data = hist_data->actions[i];
4970 if (data->handler == HANDLER_ONMATCH)
4971 print_onmatch_spec(m, hist_data, data);
4972 else if (data->handler == HANDLER_ONMAX ||
4973 data->handler == HANDLER_ONCHANGE)
4974 print_track_data_spec(m, hist_data, data);
4978 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4982 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4983 kfree(hist_data->field_var_hists[i]->cmd);
4984 kfree(hist_data->field_var_hists[i]);
4988 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4993 destroy_hist_trigger_attrs(hist_data->attrs);
4994 destroy_hist_fields(hist_data);
4995 tracing_map_destroy(hist_data->map);
4997 destroy_actions(hist_data);
4998 destroy_field_vars(hist_data);
4999 destroy_field_var_hists(hist_data);
5004 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5006 struct tracing_map *map = hist_data->map;
5007 struct ftrace_event_field *field;
5008 struct hist_field *hist_field;
5011 for_each_hist_field(i, hist_data) {
5012 hist_field = hist_data->fields[i];
5013 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5014 tracing_map_cmp_fn_t cmp_fn;
5016 field = hist_field->field;
5018 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5019 cmp_fn = tracing_map_cmp_none;
5021 cmp_fn = tracing_map_cmp_num(hist_field->size,
5022 hist_field->is_signed);
5023 else if (is_string_field(field))
5024 cmp_fn = tracing_map_cmp_string;
5026 cmp_fn = tracing_map_cmp_num(field->size,
5028 idx = tracing_map_add_key_field(map,
5031 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5032 idx = tracing_map_add_sum_field(map);
5037 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5038 idx = tracing_map_add_var(map);
5041 hist_field->var.idx = idx;
5042 hist_field->var.hist_data = hist_data;
5049 static struct hist_trigger_data *
5050 create_hist_data(unsigned int map_bits,
5051 struct hist_trigger_attrs *attrs,
5052 struct trace_event_file *file,
5055 const struct tracing_map_ops *map_ops = NULL;
5056 struct hist_trigger_data *hist_data;
5059 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5061 return ERR_PTR(-ENOMEM);
5063 hist_data->attrs = attrs;
5064 hist_data->remove = remove;
5065 hist_data->event_file = file;
5067 ret = parse_actions(hist_data);
5071 ret = create_hist_fields(hist_data, file);
5075 ret = create_sort_keys(hist_data);
5079 map_ops = &hist_trigger_elt_data_ops;
5081 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5082 map_ops, hist_data);
5083 if (IS_ERR(hist_data->map)) {
5084 ret = PTR_ERR(hist_data->map);
5085 hist_data->map = NULL;
5089 ret = create_tracing_map_fields(hist_data);
5095 hist_data->attrs = NULL;
5097 destroy_hist_data(hist_data);
5099 hist_data = ERR_PTR(ret);
5104 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5105 struct tracing_map_elt *elt, void *rec,
5106 struct ring_buffer_event *rbe,
5109 struct hist_elt_data *elt_data;
5110 struct hist_field *hist_field;
5111 unsigned int i, var_idx;
5114 elt_data = elt->private_data;
5115 elt_data->var_ref_vals = var_ref_vals;
5117 for_each_hist_val_field(i, hist_data) {
5118 hist_field = hist_data->fields[i];
5119 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5120 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5121 var_idx = hist_field->var.idx;
5122 tracing_map_set_var(elt, var_idx, hist_val);
5125 tracing_map_update_sum(elt, i, hist_val);
5128 for_each_hist_key_field(i, hist_data) {
5129 hist_field = hist_data->fields[i];
5130 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5131 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5132 var_idx = hist_field->var.idx;
5133 tracing_map_set_var(elt, var_idx, hist_val);
5137 update_field_vars(hist_data, elt, rbe, rec);
5140 static inline void add_to_key(char *compound_key, void *key,
5141 struct hist_field *key_field, void *rec)
5143 size_t size = key_field->size;
5145 if (key_field->flags & HIST_FIELD_FL_STRING) {
5146 struct ftrace_event_field *field;
5148 field = key_field->field;
5149 if (field->filter_type == FILTER_DYN_STRING)
5150 size = *(u32 *)(rec + field->offset) >> 16;
5151 else if (field->filter_type == FILTER_PTR_STRING)
5153 else if (field->filter_type == FILTER_STATIC_STRING)
5156 /* ensure NULL-termination */
5157 if (size > key_field->size - 1)
5158 size = key_field->size - 1;
5160 strncpy(compound_key + key_field->offset, (char *)key, size);
5162 memcpy(compound_key + key_field->offset, key, size);
5166 hist_trigger_actions(struct hist_trigger_data *hist_data,
5167 struct tracing_map_elt *elt, void *rec,
5168 struct ring_buffer_event *rbe, void *key,
5171 struct action_data *data;
5174 for (i = 0; i < hist_data->n_actions; i++) {
5175 data = hist_data->actions[i];
5176 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
5180 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
5181 struct ring_buffer_event *rbe)
5183 struct hist_trigger_data *hist_data = data->private_data;
5184 bool use_compound_key = (hist_data->n_keys > 1);
5185 unsigned long entries[HIST_STACKTRACE_DEPTH];
5186 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5187 char compound_key[HIST_KEY_SIZE_MAX];
5188 struct tracing_map_elt *elt = NULL;
5189 struct hist_field *key_field;
5194 memset(compound_key, 0, hist_data->key_size);
5196 for_each_hist_key_field(i, hist_data) {
5197 key_field = hist_data->fields[i];
5199 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5200 memset(entries, 0, HIST_STACKTRACE_SIZE);
5201 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5202 HIST_STACKTRACE_SKIP);
5205 field_contents = key_field->fn(key_field, elt, rbe, rec);
5206 if (key_field->flags & HIST_FIELD_FL_STRING) {
5207 key = (void *)(unsigned long)field_contents;
5208 use_compound_key = true;
5210 key = (void *)&field_contents;
5213 if (use_compound_key)
5214 add_to_key(compound_key, key, key_field, rec);
5217 if (use_compound_key)
5220 if (hist_data->n_var_refs &&
5221 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5224 elt = tracing_map_insert(hist_data->map, key);
5228 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
5230 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5231 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
5234 static void hist_trigger_stacktrace_print(struct seq_file *m,
5235 unsigned long *stacktrace_entries,
5236 unsigned int max_entries)
5238 char str[KSYM_SYMBOL_LEN];
5239 unsigned int spaces = 8;
5242 for (i = 0; i < max_entries; i++) {
5243 if (!stacktrace_entries[i])
5246 seq_printf(m, "%*c", 1 + spaces, ' ');
5247 sprint_symbol(str, stacktrace_entries[i]);
5248 seq_printf(m, "%s\n", str);
5252 static void hist_trigger_print_key(struct seq_file *m,
5253 struct hist_trigger_data *hist_data,
5255 struct tracing_map_elt *elt)
5257 struct hist_field *key_field;
5258 char str[KSYM_SYMBOL_LEN];
5259 bool multiline = false;
5260 const char *field_name;
5266 for_each_hist_key_field(i, hist_data) {
5267 key_field = hist_data->fields[i];
5269 if (i > hist_data->n_vals)
5272 field_name = hist_field_name(key_field, 0);
5274 if (key_field->flags & HIST_FIELD_FL_HEX) {
5275 uval = *(u64 *)(key + key_field->offset);
5276 seq_printf(m, "%s: %llx", field_name, uval);
5277 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5278 uval = *(u64 *)(key + key_field->offset);
5279 sprint_symbol_no_offset(str, uval);
5280 seq_printf(m, "%s: [%llx] %-45s", field_name,
5282 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5283 uval = *(u64 *)(key + key_field->offset);
5284 sprint_symbol(str, uval);
5285 seq_printf(m, "%s: [%llx] %-55s", field_name,
5287 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5288 struct hist_elt_data *elt_data = elt->private_data;
5291 if (WARN_ON_ONCE(!elt_data))
5294 comm = elt_data->comm;
5296 uval = *(u64 *)(key + key_field->offset);
5297 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5299 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5300 const char *syscall_name;
5302 uval = *(u64 *)(key + key_field->offset);
5303 syscall_name = get_syscall_name(uval);
5305 syscall_name = "unknown_syscall";
5307 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5308 syscall_name, uval);
5309 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5310 seq_puts(m, "stacktrace:\n");
5311 hist_trigger_stacktrace_print(m,
5312 key + key_field->offset,
5313 HIST_STACKTRACE_DEPTH);
5315 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5316 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5317 *(u64 *)(key + key_field->offset));
5318 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5319 seq_printf(m, "%s: %-50s", field_name,
5320 (char *)(key + key_field->offset));
5322 uval = *(u64 *)(key + key_field->offset);
5323 seq_printf(m, "%s: %10llu", field_name, uval);
5333 static void hist_trigger_entry_print(struct seq_file *m,
5334 struct hist_trigger_data *hist_data,
5336 struct tracing_map_elt *elt)
5338 const char *field_name;
5341 hist_trigger_print_key(m, hist_data, key, elt);
5343 seq_printf(m, " hitcount: %10llu",
5344 tracing_map_read_sum(elt, HITCOUNT_IDX));
5346 for (i = 1; i < hist_data->n_vals; i++) {
5347 field_name = hist_field_name(hist_data->fields[i], 0);
5349 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5350 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
5353 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
5354 seq_printf(m, " %s: %10llx", field_name,
5355 tracing_map_read_sum(elt, i));
5357 seq_printf(m, " %s: %10llu", field_name,
5358 tracing_map_read_sum(elt, i));
5362 print_actions(m, hist_data, elt);
5367 static int print_entries(struct seq_file *m,
5368 struct hist_trigger_data *hist_data)
5370 struct tracing_map_sort_entry **sort_entries = NULL;
5371 struct tracing_map *map = hist_data->map;
5374 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5375 hist_data->n_sort_keys,
5380 for (i = 0; i < n_entries; i++)
5381 hist_trigger_entry_print(m, hist_data,
5382 sort_entries[i]->key,
5383 sort_entries[i]->elt);
5385 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5390 static void hist_trigger_show(struct seq_file *m,
5391 struct event_trigger_data *data, int n)
5393 struct hist_trigger_data *hist_data;
5397 seq_puts(m, "\n\n");
5399 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5400 data->ops->print(m, data->ops, data);
5401 seq_puts(m, "#\n\n");
5403 hist_data = data->private_data;
5404 n_entries = print_entries(m, hist_data);
5408 track_data_snapshot_print(m, hist_data);
5410 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5411 (u64)atomic64_read(&hist_data->map->hits),
5412 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5415 static int hist_show(struct seq_file *m, void *v)
5417 struct event_trigger_data *data;
5418 struct trace_event_file *event_file;
5421 mutex_lock(&event_mutex);
5423 event_file = event_file_data(m->private);
5424 if (unlikely(!event_file)) {
5429 list_for_each_entry_rcu(data, &event_file->triggers, list) {
5430 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5431 hist_trigger_show(m, data, n++);
5434 if (have_hist_err()) {
5435 seq_printf(m, "\nERROR: %s\n", hist_err_str);
5436 seq_printf(m, " Last command: %s\n", last_hist_cmd);
5440 mutex_unlock(&event_mutex);
5445 static int event_hist_open(struct inode *inode, struct file *file)
5447 return single_open(file, hist_show, file);
5450 const struct file_operations event_hist_fops = {
5451 .open = event_hist_open,
5453 .llseek = seq_lseek,
5454 .release = single_release,
5457 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5459 const char *field_name = hist_field_name(hist_field, 0);
5461 if (hist_field->var.name)
5462 seq_printf(m, "%s=", hist_field->var.name);
5464 if (hist_field->flags & HIST_FIELD_FL_CPU)
5466 else if (field_name) {
5467 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5468 hist_field->flags & HIST_FIELD_FL_ALIAS)
5470 seq_printf(m, "%s", field_name);
5471 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5472 seq_puts(m, "common_timestamp");
5474 if (hist_field->flags) {
5475 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5476 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5477 const char *flags = get_hist_field_flags(hist_field);
5480 seq_printf(m, ".%s", flags);
5485 static int event_hist_trigger_print(struct seq_file *m,
5486 struct event_trigger_ops *ops,
5487 struct event_trigger_data *data)
5489 struct hist_trigger_data *hist_data = data->private_data;
5490 struct hist_field *field;
5491 bool have_var = false;
5494 seq_puts(m, "hist:");
5497 seq_printf(m, "%s:", data->name);
5499 seq_puts(m, "keys=");
5501 for_each_hist_key_field(i, hist_data) {
5502 field = hist_data->fields[i];
5504 if (i > hist_data->n_vals)
5507 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5508 seq_puts(m, "stacktrace");
5510 hist_field_print(m, field);
5513 seq_puts(m, ":vals=");
5515 for_each_hist_val_field(i, hist_data) {
5516 field = hist_data->fields[i];
5517 if (field->flags & HIST_FIELD_FL_VAR) {
5522 if (i == HITCOUNT_IDX)
5523 seq_puts(m, "hitcount");
5526 hist_field_print(m, field);
5535 for_each_hist_val_field(i, hist_data) {
5536 field = hist_data->fields[i];
5538 if (field->flags & HIST_FIELD_FL_VAR) {
5541 hist_field_print(m, field);
5546 seq_puts(m, ":sort=");
5548 for (i = 0; i < hist_data->n_sort_keys; i++) {
5549 struct tracing_map_sort_key *sort_key;
5550 unsigned int idx, first_key_idx;
5553 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5555 sort_key = &hist_data->sort_keys[i];
5556 idx = sort_key->field_idx;
5558 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5564 if (idx == HITCOUNT_IDX)
5565 seq_puts(m, "hitcount");
5567 if (idx >= first_key_idx)
5568 idx += hist_data->n_vars;
5569 hist_field_print(m, hist_data->fields[idx]);
5572 if (sort_key->descending)
5573 seq_puts(m, ".descending");
5575 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5576 if (hist_data->enable_timestamps)
5577 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5579 print_actions_spec(m, hist_data);
5581 if (data->filter_str)
5582 seq_printf(m, " if %s", data->filter_str);
5585 seq_puts(m, " [paused]");
5587 seq_puts(m, " [active]");
5594 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5595 struct event_trigger_data *data)
5597 struct hist_trigger_data *hist_data = data->private_data;
5599 if (!data->ref && hist_data->attrs->name)
5600 save_named_trigger(hist_data->attrs->name, data);
5607 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5609 struct trace_event_file *file;
5614 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5615 file = hist_data->field_var_hists[i]->hist_data->event_file;
5616 cmd = hist_data->field_var_hists[i]->cmd;
5617 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5618 "!hist", "hist", cmd);
5622 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5623 struct event_trigger_data *data)
5625 struct hist_trigger_data *hist_data = data->private_data;
5627 if (WARN_ON_ONCE(data->ref <= 0))
5633 del_named_trigger(data);
5635 trigger_data_free(data);
5637 remove_hist_vars(hist_data);
5639 unregister_field_var_hists(hist_data);
5641 destroy_hist_data(hist_data);
5645 static struct event_trigger_ops event_hist_trigger_ops = {
5646 .func = event_hist_trigger,
5647 .print = event_hist_trigger_print,
5648 .init = event_hist_trigger_init,
5649 .free = event_hist_trigger_free,
5652 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5653 struct event_trigger_data *data)
5657 save_named_trigger(data->named_data->name, data);
5659 event_hist_trigger_init(ops, data->named_data);
5664 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5665 struct event_trigger_data *data)
5667 if (WARN_ON_ONCE(data->ref <= 0))
5670 event_hist_trigger_free(ops, data->named_data);
5674 del_named_trigger(data);
5675 trigger_data_free(data);
5679 static struct event_trigger_ops event_hist_trigger_named_ops = {
5680 .func = event_hist_trigger,
5681 .print = event_hist_trigger_print,
5682 .init = event_hist_trigger_named_init,
5683 .free = event_hist_trigger_named_free,
5686 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5689 return &event_hist_trigger_ops;
5692 static void hist_clear(struct event_trigger_data *data)
5694 struct hist_trigger_data *hist_data = data->private_data;
5697 pause_named_trigger(data);
5699 tracepoint_synchronize_unregister();
5701 tracing_map_clear(hist_data->map);
5704 unpause_named_trigger(data);
5707 static bool compatible_field(struct ftrace_event_field *field,
5708 struct ftrace_event_field *test_field)
5710 if (field == test_field)
5712 if (field == NULL || test_field == NULL)
5714 if (strcmp(field->name, test_field->name) != 0)
5716 if (strcmp(field->type, test_field->type) != 0)
5718 if (field->size != test_field->size)
5720 if (field->is_signed != test_field->is_signed)
5726 static bool hist_trigger_match(struct event_trigger_data *data,
5727 struct event_trigger_data *data_test,
5728 struct event_trigger_data *named_data,
5731 struct tracing_map_sort_key *sort_key, *sort_key_test;
5732 struct hist_trigger_data *hist_data, *hist_data_test;
5733 struct hist_field *key_field, *key_field_test;
5736 if (named_data && (named_data != data_test) &&
5737 (named_data != data_test->named_data))
5740 if (!named_data && is_named_trigger(data_test))
5743 hist_data = data->private_data;
5744 hist_data_test = data_test->private_data;
5746 if (hist_data->n_vals != hist_data_test->n_vals ||
5747 hist_data->n_fields != hist_data_test->n_fields ||
5748 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5751 if (!ignore_filter) {
5752 if ((data->filter_str && !data_test->filter_str) ||
5753 (!data->filter_str && data_test->filter_str))
5757 for_each_hist_field(i, hist_data) {
5758 key_field = hist_data->fields[i];
5759 key_field_test = hist_data_test->fields[i];
5761 if (key_field->flags != key_field_test->flags)
5763 if (!compatible_field(key_field->field, key_field_test->field))
5765 if (key_field->offset != key_field_test->offset)
5767 if (key_field->size != key_field_test->size)
5769 if (key_field->is_signed != key_field_test->is_signed)
5771 if (!!key_field->var.name != !!key_field_test->var.name)
5773 if (key_field->var.name &&
5774 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5778 for (i = 0; i < hist_data->n_sort_keys; i++) {
5779 sort_key = &hist_data->sort_keys[i];
5780 sort_key_test = &hist_data_test->sort_keys[i];
5782 if (sort_key->field_idx != sort_key_test->field_idx ||
5783 sort_key->descending != sort_key_test->descending)
5787 if (!ignore_filter && data->filter_str &&
5788 (strcmp(data->filter_str, data_test->filter_str) != 0))
5791 if (!actions_match(hist_data, hist_data_test))
5797 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5798 struct event_trigger_data *data,
5799 struct trace_event_file *file)
5801 struct hist_trigger_data *hist_data = data->private_data;
5802 struct event_trigger_data *test, *named_data = NULL;
5805 if (hist_data->attrs->name) {
5806 named_data = find_named_trigger(hist_data->attrs->name);
5808 if (!hist_trigger_match(data, named_data, named_data,
5810 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
5817 if (hist_data->attrs->name && !named_data)
5820 list_for_each_entry_rcu(test, &file->triggers, list) {
5821 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5822 if (!hist_trigger_match(data, test, named_data, false))
5824 if (hist_data->attrs->pause)
5825 test->paused = true;
5826 else if (hist_data->attrs->cont)
5827 test->paused = false;
5828 else if (hist_data->attrs->clear)
5831 hist_err("Hist trigger already exists", NULL);
5838 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5839 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
5844 if (hist_data->attrs->pause)
5845 data->paused = true;
5848 data->private_data = named_data->private_data;
5849 set_named_trigger_data(data, named_data);
5850 data->ops = &event_hist_trigger_named_ops;
5853 if (data->ops->init) {
5854 ret = data->ops->init(data->ops, data);
5859 if (hist_data->enable_timestamps) {
5860 char *clock = hist_data->attrs->clock;
5862 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5864 hist_err("Couldn't set trace_clock: ", clock);
5868 tracing_set_time_stamp_abs(file->tr, true);
5872 destroy_hist_data(hist_data);
5879 static int hist_trigger_enable(struct event_trigger_data *data,
5880 struct trace_event_file *file)
5884 list_add_tail_rcu(&data->list, &file->triggers);
5886 update_cond_flag(file);
5888 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5889 list_del_rcu(&data->list);
5890 update_cond_flag(file);
5897 static bool have_hist_trigger_match(struct event_trigger_data *data,
5898 struct trace_event_file *file)
5900 struct hist_trigger_data *hist_data = data->private_data;
5901 struct event_trigger_data *test, *named_data = NULL;
5904 if (hist_data->attrs->name)
5905 named_data = find_named_trigger(hist_data->attrs->name);
5907 list_for_each_entry_rcu(test, &file->triggers, list) {
5908 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5909 if (hist_trigger_match(data, test, named_data, false)) {
5919 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5920 struct trace_event_file *file)
5922 struct hist_trigger_data *hist_data = data->private_data;
5923 struct event_trigger_data *test, *named_data = NULL;
5925 if (hist_data->attrs->name)
5926 named_data = find_named_trigger(hist_data->attrs->name);
5928 list_for_each_entry_rcu(test, &file->triggers, list) {
5929 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5930 if (!hist_trigger_match(data, test, named_data, false))
5932 hist_data = test->private_data;
5933 if (check_var_refs(hist_data))
5942 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5943 struct event_trigger_data *data,
5944 struct trace_event_file *file)
5946 struct hist_trigger_data *hist_data = data->private_data;
5947 struct event_trigger_data *test, *named_data = NULL;
5948 bool unregistered = false;
5950 if (hist_data->attrs->name)
5951 named_data = find_named_trigger(hist_data->attrs->name);
5953 list_for_each_entry_rcu(test, &file->triggers, list) {
5954 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5955 if (!hist_trigger_match(data, test, named_data, false))
5957 unregistered = true;
5958 list_del_rcu(&test->list);
5959 trace_event_trigger_enable_disable(file, 0);
5960 update_cond_flag(file);
5965 if (unregistered && test->ops->free)
5966 test->ops->free(test->ops, test);
5968 if (hist_data->enable_timestamps) {
5969 if (!hist_data->remove || unregistered)
5970 tracing_set_time_stamp_abs(file->tr, false);
5974 static bool hist_file_check_refs(struct trace_event_file *file)
5976 struct hist_trigger_data *hist_data;
5977 struct event_trigger_data *test;
5979 list_for_each_entry_rcu(test, &file->triggers, list) {
5980 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5981 hist_data = test->private_data;
5982 if (check_var_refs(hist_data))
5990 static void hist_unreg_all(struct trace_event_file *file)
5992 struct event_trigger_data *test, *n;
5993 struct hist_trigger_data *hist_data;
5994 struct synth_event *se;
5995 const char *se_name;
5997 lockdep_assert_held(&event_mutex);
5999 if (hist_file_check_refs(file))
6002 list_for_each_entry_safe(test, n, &file->triggers, list) {
6003 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6004 hist_data = test->private_data;
6005 list_del_rcu(&test->list);
6006 trace_event_trigger_enable_disable(file, 0);
6008 se_name = trace_event_name(file->event_call);
6009 se = find_synth_event(se_name);
6013 update_cond_flag(file);
6014 if (hist_data->enable_timestamps)
6015 tracing_set_time_stamp_abs(file->tr, false);
6016 if (test->ops->free)
6017 test->ops->free(test->ops, test);
6022 static int event_hist_trigger_func(struct event_command *cmd_ops,
6023 struct trace_event_file *file,
6024 char *glob, char *cmd, char *param)
6026 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6027 struct event_trigger_data *trigger_data;
6028 struct hist_trigger_attrs *attrs;
6029 struct event_trigger_ops *trigger_ops;
6030 struct hist_trigger_data *hist_data;
6031 struct synth_event *se;
6032 const char *se_name;
6033 bool remove = false;
6037 lockdep_assert_held(&event_mutex);
6039 if (glob && strlen(glob)) {
6040 last_cmd_set(param);
6051 * separate the trigger from the filter (k:v [if filter])
6052 * allowing for whitespace in the trigger
6054 p = trigger = param;
6056 p = strstr(p, "if");
6061 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6065 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
6067 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6078 param = strstrip(p);
6079 trigger = strstrip(trigger);
6082 attrs = parse_hist_trigger_attrs(trigger);
6084 return PTR_ERR(attrs);
6086 if (attrs->map_bits)
6087 hist_trigger_bits = attrs->map_bits;
6089 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6090 if (IS_ERR(hist_data)) {
6091 destroy_hist_trigger_attrs(attrs);
6092 return PTR_ERR(hist_data);
6095 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6097 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
6098 if (!trigger_data) {
6103 trigger_data->count = -1;
6104 trigger_data->ops = trigger_ops;
6105 trigger_data->cmd_ops = cmd_ops;
6107 INIT_LIST_HEAD(&trigger_data->list);
6108 RCU_INIT_POINTER(trigger_data->filter, NULL);
6110 trigger_data->private_data = hist_data;
6112 /* if param is non-empty, it's supposed to be a filter */
6113 if (param && cmd_ops->set_filter) {
6114 ret = cmd_ops->set_filter(param, trigger_data, file);
6120 if (!have_hist_trigger_match(trigger_data, file))
6123 if (hist_trigger_check_refs(trigger_data, file)) {
6128 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6129 se_name = trace_event_name(file->event_call);
6130 se = find_synth_event(se_name);
6137 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6139 * The above returns on success the # of triggers registered,
6140 * but if it didn't register any it returns zero. Consider no
6141 * triggers registered a failure too.
6144 if (!(attrs->pause || attrs->cont || attrs->clear))
6150 if (get_named_trigger_data(trigger_data))
6153 if (has_hist_vars(hist_data))
6154 save_hist_vars(hist_data);
6156 ret = create_actions(hist_data);
6160 ret = tracing_map_init(hist_data->map);
6164 ret = hist_trigger_enable(trigger_data, file);
6168 se_name = trace_event_name(file->event_call);
6169 se = find_synth_event(se_name);
6172 /* Just return zero, not the number of registered triggers */
6180 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6182 if (cmd_ops->set_filter)
6183 cmd_ops->set_filter(NULL, trigger_data, NULL);
6185 remove_hist_vars(hist_data);
6187 kfree(trigger_data);
6189 destroy_hist_data(hist_data);
6193 static struct event_command trigger_hist_cmd = {
6195 .trigger_type = ETT_EVENT_HIST,
6196 .flags = EVENT_CMD_FL_NEEDS_REC,
6197 .func = event_hist_trigger_func,
6198 .reg = hist_register_trigger,
6199 .unreg = hist_unregister_trigger,
6200 .unreg_all = hist_unreg_all,
6201 .get_trigger_ops = event_hist_get_trigger_ops,
6202 .set_filter = set_trigger_filter,
6205 __init int register_trigger_hist_cmd(void)
6209 ret = register_event_command(&trigger_hist_cmd);
6216 hist_enable_trigger(struct event_trigger_data *data, void *rec,
6217 struct ring_buffer_event *event)
6219 struct enable_trigger_data *enable_data = data->private_data;
6220 struct event_trigger_data *test;
6222 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
6223 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6224 if (enable_data->enable)
6225 test->paused = false;
6227 test->paused = true;
6233 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
6234 struct ring_buffer_event *event)
6239 if (data->count != -1)
6242 hist_enable_trigger(data, rec, event);
6245 static struct event_trigger_ops hist_enable_trigger_ops = {
6246 .func = hist_enable_trigger,
6247 .print = event_enable_trigger_print,
6248 .init = event_trigger_init,
6249 .free = event_enable_trigger_free,
6252 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6253 .func = hist_enable_count_trigger,
6254 .print = event_enable_trigger_print,
6255 .init = event_trigger_init,
6256 .free = event_enable_trigger_free,
6259 static struct event_trigger_ops hist_disable_trigger_ops = {
6260 .func = hist_enable_trigger,
6261 .print = event_enable_trigger_print,
6262 .init = event_trigger_init,
6263 .free = event_enable_trigger_free,
6266 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6267 .func = hist_enable_count_trigger,
6268 .print = event_enable_trigger_print,
6269 .init = event_trigger_init,
6270 .free = event_enable_trigger_free,
6273 static struct event_trigger_ops *
6274 hist_enable_get_trigger_ops(char *cmd, char *param)
6276 struct event_trigger_ops *ops;
6279 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6282 ops = param ? &hist_enable_count_trigger_ops :
6283 &hist_enable_trigger_ops;
6285 ops = param ? &hist_disable_count_trigger_ops :
6286 &hist_disable_trigger_ops;
6291 static void hist_enable_unreg_all(struct trace_event_file *file)
6293 struct event_trigger_data *test, *n;
6295 list_for_each_entry_safe(test, n, &file->triggers, list) {
6296 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6297 list_del_rcu(&test->list);
6298 update_cond_flag(file);
6299 trace_event_trigger_enable_disable(file, 0);
6300 if (test->ops->free)
6301 test->ops->free(test->ops, test);
6306 static struct event_command trigger_hist_enable_cmd = {
6307 .name = ENABLE_HIST_STR,
6308 .trigger_type = ETT_HIST_ENABLE,
6309 .func = event_enable_trigger_func,
6310 .reg = event_enable_register_trigger,
6311 .unreg = event_enable_unregister_trigger,
6312 .unreg_all = hist_enable_unreg_all,
6313 .get_trigger_ops = hist_enable_get_trigger_ops,
6314 .set_filter = set_trigger_filter,
6317 static struct event_command trigger_hist_disable_cmd = {
6318 .name = DISABLE_HIST_STR,
6319 .trigger_type = ETT_HIST_ENABLE,
6320 .func = event_enable_trigger_func,
6321 .reg = event_enable_register_trigger,
6322 .unreg = event_enable_unregister_trigger,
6323 .unreg_all = hist_enable_unreg_all,
6324 .get_trigger_ops = hist_enable_get_trigger_ops,
6325 .set_filter = set_trigger_filter,
6328 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6330 unregister_event_command(&trigger_hist_enable_cmd);
6331 unregister_event_command(&trigger_hist_disable_cmd);
6334 __init int register_trigger_hist_enable_disable_cmds(void)
6338 ret = register_event_command(&trigger_hist_enable_cmd);
6339 if (WARN_ON(ret < 0))
6341 ret = register_event_command(&trigger_hist_disable_cmd);
6342 if (WARN_ON(ret < 0))
6343 unregister_trigger_hist_enable_disable_cmds();
6348 static __init int trace_events_hist_init(void)
6350 struct dentry *entry = NULL;
6351 struct dentry *d_tracer;
6354 err = dyn_event_register(&synth_event_ops);
6356 pr_warn("Could not register synth_event_ops\n");
6360 d_tracer = tracing_init_dentry();
6361 if (IS_ERR(d_tracer)) {
6362 err = PTR_ERR(d_tracer);
6366 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
6367 NULL, &synth_events_fops);
6375 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6380 fs_initcall(trace_events_hist_init);