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/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
21 #include "tracing_map.h"
22 #include "trace_synth.h"
25 C(NONE, "No error"), \
26 C(DUPLICATE_VAR, "Variable already defined"), \
27 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
28 C(TOO_MANY_VARS, "Too many variables defined"), \
29 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
30 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
31 C(TRIGGER_EEXIST, "Hist trigger already exists"), \
32 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
33 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
34 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
35 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
36 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
37 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
38 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
39 C(HIST_NOT_FOUND, "Matching event histogram not found"), \
40 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
41 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
42 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
43 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
44 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
45 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
46 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
47 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
48 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
49 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
50 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
51 C(TOO_MANY_PARAMS, "Too many action params"), \
52 C(PARAM_NOT_FOUND, "Couldn't find param"), \
53 C(INVALID_PARAM, "Invalid action param"), \
54 C(ACTION_NOT_FOUND, "No action found"), \
55 C(NO_SAVE_PARAMS, "No params found for save()"), \
56 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
57 C(ACTION_MISMATCH, "Handler doesn't support action"), \
58 C(NO_CLOSING_PAREN, "No closing paren found"), \
59 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
60 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
61 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
62 C(VAR_NOT_FOUND, "Couldn't find variable"), \
63 C(FIELD_NOT_FOUND, "Couldn't find field"), \
64 C(EMPTY_ASSIGNMENT, "Empty assignment"), \
65 C(INVALID_SORT_MODIFIER,"Invalid sort modifier"), \
66 C(EMPTY_SORT_FIELD, "Empty sort field"), \
67 C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \
68 C(INVALID_SORT_FIELD, "Sort field must be a key or a val"),
71 #define C(a, b) HIST_ERR_##a
78 static const char *err_text[] = { ERRORS };
82 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
83 struct tracing_map_elt *elt,
84 struct trace_buffer *buffer,
85 struct ring_buffer_event *rbe,
88 #define HIST_FIELD_OPERANDS_MAX 2
89 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
90 #define HIST_ACTIONS_MAX 8
100 * A hist_var (histogram variable) contains variable information for
101 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
102 * flag set. A hist_var has a variable name e.g. ts0, and is
103 * associated with a given histogram trigger, as specified by
104 * hist_data. The hist_var idx is the unique index assigned to the
105 * variable by the hist trigger's tracing_map. The idx is what is
106 * used to set a variable's value and, by a variable reference, to
111 struct hist_trigger_data *hist_data;
116 struct ftrace_event_field *field;
122 unsigned int is_signed;
124 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
125 struct hist_trigger_data *hist_data;
128 * Variable fields contain variable-specific info in var.
131 enum field_op_id operator;
136 * The name field is used for EXPR and VAR_REF fields. VAR
137 * fields contain the variable name in var.name.
142 * When a histogram trigger is hit, if it has any references
143 * to variables, the values of those variables are collected
144 * into a var_ref_vals array by resolve_var_refs(). The
145 * current value of each variable is read from the tracing_map
146 * using the hist field's hist_var.idx and entered into the
147 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
149 unsigned int var_ref_idx;
152 unsigned int var_str_idx;
155 static u64 hist_field_none(struct hist_field *field,
156 struct tracing_map_elt *elt,
157 struct trace_buffer *buffer,
158 struct ring_buffer_event *rbe,
164 static u64 hist_field_counter(struct hist_field *field,
165 struct tracing_map_elt *elt,
166 struct trace_buffer *buffer,
167 struct ring_buffer_event *rbe,
173 static u64 hist_field_string(struct hist_field *hist_field,
174 struct tracing_map_elt *elt,
175 struct trace_buffer *buffer,
176 struct ring_buffer_event *rbe,
179 char *addr = (char *)(event + hist_field->field->offset);
181 return (u64)(unsigned long)addr;
184 static u64 hist_field_dynstring(struct hist_field *hist_field,
185 struct tracing_map_elt *elt,
186 struct trace_buffer *buffer,
187 struct ring_buffer_event *rbe,
190 u32 str_item = *(u32 *)(event + hist_field->field->offset);
191 int str_loc = str_item & 0xffff;
192 char *addr = (char *)(event + str_loc);
194 return (u64)(unsigned long)addr;
197 static u64 hist_field_pstring(struct hist_field *hist_field,
198 struct tracing_map_elt *elt,
199 struct trace_buffer *buffer,
200 struct ring_buffer_event *rbe,
203 char **addr = (char **)(event + hist_field->field->offset);
205 return (u64)(unsigned long)*addr;
208 static u64 hist_field_log2(struct hist_field *hist_field,
209 struct tracing_map_elt *elt,
210 struct trace_buffer *buffer,
211 struct ring_buffer_event *rbe,
214 struct hist_field *operand = hist_field->operands[0];
216 u64 val = operand->fn(operand, elt, buffer, rbe, event);
218 return (u64) ilog2(roundup_pow_of_two(val));
221 static u64 hist_field_plus(struct hist_field *hist_field,
222 struct tracing_map_elt *elt,
223 struct trace_buffer *buffer,
224 struct ring_buffer_event *rbe,
227 struct hist_field *operand1 = hist_field->operands[0];
228 struct hist_field *operand2 = hist_field->operands[1];
230 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
231 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
236 static u64 hist_field_minus(struct hist_field *hist_field,
237 struct tracing_map_elt *elt,
238 struct trace_buffer *buffer,
239 struct ring_buffer_event *rbe,
242 struct hist_field *operand1 = hist_field->operands[0];
243 struct hist_field *operand2 = hist_field->operands[1];
245 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
246 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
251 static u64 hist_field_unary_minus(struct hist_field *hist_field,
252 struct tracing_map_elt *elt,
253 struct trace_buffer *buffer,
254 struct ring_buffer_event *rbe,
257 struct hist_field *operand = hist_field->operands[0];
259 s64 sval = (s64)operand->fn(operand, elt, buffer, rbe, event);
260 u64 val = (u64)-sval;
265 #define DEFINE_HIST_FIELD_FN(type) \
266 static u64 hist_field_##type(struct hist_field *hist_field, \
267 struct tracing_map_elt *elt, \
268 struct trace_buffer *buffer, \
269 struct ring_buffer_event *rbe, \
272 type *addr = (type *)(event + hist_field->field->offset); \
274 return (u64)(unsigned long)*addr; \
277 DEFINE_HIST_FIELD_FN(s64);
278 DEFINE_HIST_FIELD_FN(u64);
279 DEFINE_HIST_FIELD_FN(s32);
280 DEFINE_HIST_FIELD_FN(u32);
281 DEFINE_HIST_FIELD_FN(s16);
282 DEFINE_HIST_FIELD_FN(u16);
283 DEFINE_HIST_FIELD_FN(s8);
284 DEFINE_HIST_FIELD_FN(u8);
286 #define for_each_hist_field(i, hist_data) \
287 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
289 #define for_each_hist_val_field(i, hist_data) \
290 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
292 #define for_each_hist_key_field(i, hist_data) \
293 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
295 #define HIST_STACKTRACE_DEPTH 16
296 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
297 #define HIST_STACKTRACE_SKIP 5
299 #define HITCOUNT_IDX 0
300 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
302 enum hist_field_flags {
303 HIST_FIELD_FL_HITCOUNT = 1 << 0,
304 HIST_FIELD_FL_KEY = 1 << 1,
305 HIST_FIELD_FL_STRING = 1 << 2,
306 HIST_FIELD_FL_HEX = 1 << 3,
307 HIST_FIELD_FL_SYM = 1 << 4,
308 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
309 HIST_FIELD_FL_EXECNAME = 1 << 6,
310 HIST_FIELD_FL_SYSCALL = 1 << 7,
311 HIST_FIELD_FL_STACKTRACE = 1 << 8,
312 HIST_FIELD_FL_LOG2 = 1 << 9,
313 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
314 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
315 HIST_FIELD_FL_VAR = 1 << 12,
316 HIST_FIELD_FL_EXPR = 1 << 13,
317 HIST_FIELD_FL_VAR_REF = 1 << 14,
318 HIST_FIELD_FL_CPU = 1 << 15,
319 HIST_FIELD_FL_ALIAS = 1 << 16,
324 char *name[TRACING_MAP_VARS_MAX];
325 char *expr[TRACING_MAP_VARS_MAX];
328 struct hist_trigger_attrs {
338 unsigned int map_bits;
340 char *assignment_str[TRACING_MAP_VARS_MAX];
341 unsigned int n_assignments;
343 char *action_str[HIST_ACTIONS_MAX];
344 unsigned int n_actions;
346 struct var_defs var_defs;
350 struct hist_field *var;
351 struct hist_field *val;
354 struct field_var_hist {
355 struct hist_trigger_data *hist_data;
359 struct hist_trigger_data {
360 struct hist_field *fields[HIST_FIELDS_MAX];
363 unsigned int n_fields;
365 unsigned int n_var_str;
366 unsigned int key_size;
367 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
368 unsigned int n_sort_keys;
369 struct trace_event_file *event_file;
370 struct hist_trigger_attrs *attrs;
371 struct tracing_map *map;
372 bool enable_timestamps;
374 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
375 unsigned int n_var_refs;
377 struct action_data *actions[HIST_ACTIONS_MAX];
378 unsigned int n_actions;
380 struct field_var *field_vars[SYNTH_FIELDS_MAX];
381 unsigned int n_field_vars;
382 unsigned int n_field_var_str;
383 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
384 unsigned int n_field_var_hists;
386 struct field_var *save_vars[SYNTH_FIELDS_MAX];
387 unsigned int n_save_vars;
388 unsigned int n_save_var_str;
393 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
394 struct tracing_map_elt *elt,
395 struct trace_buffer *buffer, void *rec,
396 struct ring_buffer_event *rbe, void *key,
397 struct action_data *data, u64 *var_ref_vals);
399 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
414 enum handler_id handler;
415 enum action_id action;
419 unsigned int n_params;
420 char *params[SYNTH_FIELDS_MAX];
423 * When a histogram trigger is hit, the values of any
424 * references to variables, including variables being passed
425 * as parameters to synthetic events, are collected into a
426 * var_ref_vals array. This var_ref_idx array is an array of
427 * indices into the var_ref_vals array, one for each synthetic
428 * event param, and is passed to the synthetic event
431 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
432 struct synth_event *synth_event;
433 bool use_trace_keyword;
434 char *synth_event_name;
444 * var_str contains the $-unstripped variable
445 * name referenced by var_ref, and used when
446 * printing the action. Because var_ref
447 * creation is deferred to create_actions(),
448 * we need a per-action way to save it until
449 * then, thus var_str.
454 * var_ref refers to the variable being
455 * tracked e.g onmax($var).
457 struct hist_field *var_ref;
460 * track_var contains the 'invisible' tracking
461 * variable created to keep the current
464 struct hist_field *track_var;
466 check_track_val_fn_t check_val;
467 action_fn_t save_data;
476 unsigned int key_len;
478 struct tracing_map_elt elt;
480 struct action_data *action_data;
481 struct hist_trigger_data *hist_data;
484 struct hist_elt_data {
487 char *field_var_str[SYNTH_FIELDS_MAX];
490 struct snapshot_context {
491 struct tracing_map_elt *elt;
495 static void track_data_free(struct track_data *track_data)
497 struct hist_elt_data *elt_data;
502 kfree(track_data->key);
504 elt_data = track_data->elt.private_data;
506 kfree(elt_data->comm);
513 static struct track_data *track_data_alloc(unsigned int key_len,
514 struct action_data *action_data,
515 struct hist_trigger_data *hist_data)
517 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
518 struct hist_elt_data *elt_data;
521 return ERR_PTR(-ENOMEM);
523 data->key = kzalloc(key_len, GFP_KERNEL);
525 track_data_free(data);
526 return ERR_PTR(-ENOMEM);
529 data->key_len = key_len;
530 data->action_data = action_data;
531 data->hist_data = hist_data;
533 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
535 track_data_free(data);
536 return ERR_PTR(-ENOMEM);
539 data->elt.private_data = elt_data;
541 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
542 if (!elt_data->comm) {
543 track_data_free(data);
544 return ERR_PTR(-ENOMEM);
550 static char last_cmd[MAX_FILTER_STR_VAL];
551 static char last_cmd_loc[MAX_FILTER_STR_VAL];
553 static int errpos(char *str)
555 return err_pos(last_cmd, str);
558 static void last_cmd_set(struct trace_event_file *file, char *str)
560 const char *system = NULL, *name = NULL;
561 struct trace_event_call *call;
566 strcpy(last_cmd, "hist:");
567 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
570 call = file->event_call;
571 system = call->class->system;
573 name = trace_event_name(call);
580 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
583 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
585 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
589 static void hist_err_clear(void)
592 last_cmd_loc[0] = '\0';
595 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
596 unsigned int *var_ref_idx);
598 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
599 unsigned int *var_ref_idx)
601 struct tracepoint *tp = event->tp;
603 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
604 struct tracepoint_func *probe_func_ptr;
605 synth_probe_func_t probe_func;
608 if (!(cpu_online(raw_smp_processor_id())))
611 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
612 if (probe_func_ptr) {
614 probe_func = probe_func_ptr->func;
615 __data = probe_func_ptr->data;
616 probe_func(__data, var_ref_vals, var_ref_idx);
617 } while ((++probe_func_ptr)->func);
622 static void action_trace(struct hist_trigger_data *hist_data,
623 struct tracing_map_elt *elt,
624 struct trace_buffer *buffer, void *rec,
625 struct ring_buffer_event *rbe, void *key,
626 struct action_data *data, u64 *var_ref_vals)
628 struct synth_event *event = data->synth_event;
630 trace_synth(event, var_ref_vals, data->var_ref_idx);
633 struct hist_var_data {
634 struct list_head list;
635 struct hist_trigger_data *hist_data;
638 static u64 hist_field_timestamp(struct hist_field *hist_field,
639 struct tracing_map_elt *elt,
640 struct trace_buffer *buffer,
641 struct ring_buffer_event *rbe,
644 struct hist_trigger_data *hist_data = hist_field->hist_data;
645 struct trace_array *tr = hist_data->event_file->tr;
647 u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
649 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
655 static u64 hist_field_cpu(struct hist_field *hist_field,
656 struct tracing_map_elt *elt,
657 struct trace_buffer *buffer,
658 struct ring_buffer_event *rbe,
661 int cpu = smp_processor_id();
667 * check_field_for_var_ref - Check if a VAR_REF field references a variable
668 * @hist_field: The VAR_REF field to check
669 * @var_data: The hist trigger that owns the variable
670 * @var_idx: The trigger variable identifier
672 * Check the given VAR_REF field to see whether or not it references
673 * the given variable associated with the given trigger.
675 * Return: The VAR_REF field if it does reference the variable, NULL if not
677 static struct hist_field *
678 check_field_for_var_ref(struct hist_field *hist_field,
679 struct hist_trigger_data *var_data,
680 unsigned int var_idx)
682 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
684 if (hist_field && hist_field->var.idx == var_idx &&
685 hist_field->var.hist_data == var_data)
692 * find_var_ref - Check if a trigger has a reference to a trigger variable
693 * @hist_data: The hist trigger that might have a reference to the variable
694 * @var_data: The hist trigger that owns the variable
695 * @var_idx: The trigger variable identifier
697 * Check the list of var_refs[] on the first hist trigger to see
698 * whether any of them are references to the variable on the second
701 * Return: The VAR_REF field referencing the variable if so, NULL if not
703 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
704 struct hist_trigger_data *var_data,
705 unsigned int var_idx)
707 struct hist_field *hist_field;
710 for (i = 0; i < hist_data->n_var_refs; i++) {
711 hist_field = hist_data->var_refs[i];
712 if (check_field_for_var_ref(hist_field, var_data, var_idx))
720 * find_any_var_ref - Check if there is a reference to a given trigger variable
721 * @hist_data: The hist trigger
722 * @var_idx: The trigger variable identifier
724 * Check to see whether the given variable is currently referenced by
727 * The trigger the variable is defined on is explicitly excluded - the
728 * assumption being that a self-reference doesn't prevent a trigger
729 * from being removed.
731 * Return: The VAR_REF field referencing the variable if so, NULL if not
733 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
734 unsigned int var_idx)
736 struct trace_array *tr = hist_data->event_file->tr;
737 struct hist_field *found = NULL;
738 struct hist_var_data *var_data;
740 list_for_each_entry(var_data, &tr->hist_vars, list) {
741 if (var_data->hist_data == hist_data)
743 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
752 * check_var_refs - Check if there is a reference to any of trigger's variables
753 * @hist_data: The hist trigger
755 * A trigger can define one or more variables. If any one of them is
756 * currently referenced by any other trigger, this function will
759 * Typically used to determine whether or not a trigger can be removed
760 * - if there are any references to a trigger's variables, it cannot.
762 * Return: True if there is a reference to any of trigger's variables
764 static bool check_var_refs(struct hist_trigger_data *hist_data)
766 struct hist_field *field;
770 for_each_hist_field(i, hist_data) {
771 field = hist_data->fields[i];
772 if (field && field->flags & HIST_FIELD_FL_VAR) {
773 if (find_any_var_ref(hist_data, field->var.idx)) {
783 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
785 struct trace_array *tr = hist_data->event_file->tr;
786 struct hist_var_data *var_data, *found = NULL;
788 list_for_each_entry(var_data, &tr->hist_vars, list) {
789 if (var_data->hist_data == hist_data) {
798 static bool field_has_hist_vars(struct hist_field *hist_field,
809 if (hist_field->flags & HIST_FIELD_FL_VAR ||
810 hist_field->flags & HIST_FIELD_FL_VAR_REF)
813 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
814 struct hist_field *operand;
816 operand = hist_field->operands[i];
817 if (field_has_hist_vars(operand, level + 1))
824 static bool has_hist_vars(struct hist_trigger_data *hist_data)
826 struct hist_field *hist_field;
829 for_each_hist_field(i, hist_data) {
830 hist_field = hist_data->fields[i];
831 if (field_has_hist_vars(hist_field, 0))
838 static int save_hist_vars(struct hist_trigger_data *hist_data)
840 struct trace_array *tr = hist_data->event_file->tr;
841 struct hist_var_data *var_data;
843 var_data = find_hist_vars(hist_data);
847 if (tracing_check_open_get_tr(tr))
850 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
856 var_data->hist_data = hist_data;
857 list_add(&var_data->list, &tr->hist_vars);
862 static void remove_hist_vars(struct hist_trigger_data *hist_data)
864 struct trace_array *tr = hist_data->event_file->tr;
865 struct hist_var_data *var_data;
867 var_data = find_hist_vars(hist_data);
871 if (WARN_ON(check_var_refs(hist_data)))
874 list_del(&var_data->list);
881 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
882 const char *var_name)
884 struct hist_field *hist_field, *found = NULL;
887 for_each_hist_field(i, hist_data) {
888 hist_field = hist_data->fields[i];
889 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
890 strcmp(hist_field->var.name, var_name) == 0) {
899 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
900 struct trace_event_file *file,
901 const char *var_name)
903 struct hist_trigger_data *test_data;
904 struct event_trigger_data *test;
905 struct hist_field *hist_field;
907 lockdep_assert_held(&event_mutex);
909 hist_field = find_var_field(hist_data, var_name);
913 list_for_each_entry(test, &file->triggers, list) {
914 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
915 test_data = test->private_data;
916 hist_field = find_var_field(test_data, var_name);
925 static struct trace_event_file *find_var_file(struct trace_array *tr,
930 struct hist_trigger_data *var_hist_data;
931 struct hist_var_data *var_data;
932 struct trace_event_file *file, *found = NULL;
935 return find_event_file(tr, system, event_name);
937 list_for_each_entry(var_data, &tr->hist_vars, list) {
938 var_hist_data = var_data->hist_data;
939 file = var_hist_data->event_file;
943 if (find_var_field(var_hist_data, var_name)) {
945 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
956 static struct hist_field *find_file_var(struct trace_event_file *file,
957 const char *var_name)
959 struct hist_trigger_data *test_data;
960 struct event_trigger_data *test;
961 struct hist_field *hist_field;
963 lockdep_assert_held(&event_mutex);
965 list_for_each_entry(test, &file->triggers, list) {
966 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
967 test_data = test->private_data;
968 hist_field = find_var_field(test_data, var_name);
977 static struct hist_field *
978 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
980 struct trace_array *tr = hist_data->event_file->tr;
981 struct hist_field *hist_field, *found = NULL;
982 struct trace_event_file *file;
985 for (i = 0; i < hist_data->n_actions; i++) {
986 struct action_data *data = hist_data->actions[i];
988 if (data->handler == HANDLER_ONMATCH) {
989 char *system = data->match_data.event_system;
990 char *event_name = data->match_data.event;
992 file = find_var_file(tr, system, event_name, var_name);
995 hist_field = find_file_var(file, var_name);
998 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1000 return ERR_PTR(-EINVAL);
1010 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1015 struct trace_array *tr = hist_data->event_file->tr;
1016 struct hist_field *hist_field = NULL;
1017 struct trace_event_file *file;
1019 if (!system || !event_name) {
1020 hist_field = find_match_var(hist_data, var_name);
1021 if (IS_ERR(hist_field))
1027 file = find_var_file(tr, system, event_name, var_name);
1031 hist_field = find_file_var(file, var_name);
1036 static u64 hist_field_var_ref(struct hist_field *hist_field,
1037 struct tracing_map_elt *elt,
1038 struct trace_buffer *buffer,
1039 struct ring_buffer_event *rbe,
1042 struct hist_elt_data *elt_data;
1045 if (WARN_ON_ONCE(!elt))
1048 elt_data = elt->private_data;
1049 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1054 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1055 u64 *var_ref_vals, bool self)
1057 struct hist_trigger_data *var_data;
1058 struct tracing_map_elt *var_elt;
1059 struct hist_field *hist_field;
1060 unsigned int i, var_idx;
1061 bool resolved = true;
1064 for (i = 0; i < hist_data->n_var_refs; i++) {
1065 hist_field = hist_data->var_refs[i];
1066 var_idx = hist_field->var.idx;
1067 var_data = hist_field->var.hist_data;
1069 if (var_data == NULL) {
1074 if ((self && var_data != hist_data) ||
1075 (!self && var_data == hist_data))
1078 var_elt = tracing_map_lookup(var_data->map, key);
1084 if (!tracing_map_var_set(var_elt, var_idx)) {
1089 if (self || !hist_field->read_once)
1090 var_val = tracing_map_read_var(var_elt, var_idx);
1092 var_val = tracing_map_read_var_once(var_elt, var_idx);
1094 var_ref_vals[i] = var_val;
1100 static const char *hist_field_name(struct hist_field *field,
1103 const char *field_name = "";
1109 field_name = field->field->name;
1110 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1111 field->flags & HIST_FIELD_FL_ALIAS)
1112 field_name = hist_field_name(field->operands[0], ++level);
1113 else if (field->flags & HIST_FIELD_FL_CPU)
1115 else if (field->flags & HIST_FIELD_FL_EXPR ||
1116 field->flags & HIST_FIELD_FL_VAR_REF) {
1117 if (field->system) {
1118 static char full_name[MAX_FILTER_STR_VAL];
1120 strcat(full_name, field->system);
1121 strcat(full_name, ".");
1122 strcat(full_name, field->event_name);
1123 strcat(full_name, ".");
1124 strcat(full_name, field->name);
1125 field_name = full_name;
1127 field_name = field->name;
1128 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1129 field_name = "common_timestamp";
1131 if (field_name == NULL)
1137 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1139 hist_field_fn_t fn = NULL;
1141 switch (field_size) {
1143 if (field_is_signed)
1144 fn = hist_field_s64;
1146 fn = hist_field_u64;
1149 if (field_is_signed)
1150 fn = hist_field_s32;
1152 fn = hist_field_u32;
1155 if (field_is_signed)
1156 fn = hist_field_s16;
1158 fn = hist_field_u16;
1161 if (field_is_signed)
1171 static int parse_map_size(char *str)
1173 unsigned long size, map_bits;
1176 ret = kstrtoul(str, 0, &size);
1180 map_bits = ilog2(roundup_pow_of_two(size));
1181 if (map_bits < TRACING_MAP_BITS_MIN ||
1182 map_bits > TRACING_MAP_BITS_MAX)
1190 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1197 for (i = 0; i < attrs->n_assignments; i++)
1198 kfree(attrs->assignment_str[i]);
1200 for (i = 0; i < attrs->n_actions; i++)
1201 kfree(attrs->action_str[i]);
1204 kfree(attrs->sort_key_str);
1205 kfree(attrs->keys_str);
1206 kfree(attrs->vals_str);
1207 kfree(attrs->clock);
1211 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1215 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1218 if ((str_has_prefix(str, "onmatch(")) ||
1219 (str_has_prefix(str, "onmax(")) ||
1220 (str_has_prefix(str, "onchange("))) {
1221 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1222 if (!attrs->action_str[attrs->n_actions]) {
1232 static int parse_assignment(struct trace_array *tr,
1233 char *str, struct hist_trigger_attrs *attrs)
1237 if ((len = str_has_prefix(str, "key=")) ||
1238 (len = str_has_prefix(str, "keys="))) {
1239 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1240 if (!attrs->keys_str) {
1244 } else if ((len = str_has_prefix(str, "val=")) ||
1245 (len = str_has_prefix(str, "vals=")) ||
1246 (len = str_has_prefix(str, "values="))) {
1247 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1248 if (!attrs->vals_str) {
1252 } else if ((len = str_has_prefix(str, "sort="))) {
1253 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1254 if (!attrs->sort_key_str) {
1258 } else if (str_has_prefix(str, "name=")) {
1259 attrs->name = kstrdup(str, GFP_KERNEL);
1264 } else if ((len = str_has_prefix(str, "clock="))) {
1267 str = strstrip(str);
1268 attrs->clock = kstrdup(str, GFP_KERNEL);
1269 if (!attrs->clock) {
1273 } else if ((len = str_has_prefix(str, "size="))) {
1274 int map_bits = parse_map_size(str + len);
1280 attrs->map_bits = map_bits;
1284 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1285 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1290 assignment = kstrdup(str, GFP_KERNEL);
1296 attrs->assignment_str[attrs->n_assignments++] = assignment;
1302 static struct hist_trigger_attrs *
1303 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1305 struct hist_trigger_attrs *attrs;
1308 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1310 return ERR_PTR(-ENOMEM);
1312 while (trigger_str) {
1313 char *str = strsep(&trigger_str, ":");
1316 rhs = strchr(str, '=');
1318 if (!strlen(++rhs)) {
1320 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1323 ret = parse_assignment(tr, str, attrs);
1326 } else if (strcmp(str, "pause") == 0)
1327 attrs->pause = true;
1328 else if ((strcmp(str, "cont") == 0) ||
1329 (strcmp(str, "continue") == 0))
1331 else if (strcmp(str, "clear") == 0)
1332 attrs->clear = true;
1334 ret = parse_action(str, attrs);
1340 if (!attrs->keys_str) {
1345 if (!attrs->clock) {
1346 attrs->clock = kstrdup("global", GFP_KERNEL);
1347 if (!attrs->clock) {
1355 destroy_hist_trigger_attrs(attrs);
1357 return ERR_PTR(ret);
1360 static inline void save_comm(char *comm, struct task_struct *task)
1363 strcpy(comm, "<idle>");
1367 if (WARN_ON_ONCE(task->pid < 0)) {
1368 strcpy(comm, "<XXX>");
1372 strncpy(comm, task->comm, TASK_COMM_LEN);
1375 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1379 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1380 kfree(elt_data->field_var_str[i]);
1382 kfree(elt_data->comm);
1386 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1388 struct hist_elt_data *elt_data = elt->private_data;
1390 hist_elt_data_free(elt_data);
1393 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1395 struct hist_trigger_data *hist_data = elt->map->private_data;
1396 unsigned int size = TASK_COMM_LEN;
1397 struct hist_elt_data *elt_data;
1398 struct hist_field *key_field;
1399 unsigned int i, n_str;
1401 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1405 for_each_hist_key_field(i, hist_data) {
1406 key_field = hist_data->fields[i];
1408 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
1409 elt_data->comm = kzalloc(size, GFP_KERNEL);
1410 if (!elt_data->comm) {
1418 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1419 hist_data->n_var_str;
1420 if (n_str > SYNTH_FIELDS_MAX) {
1421 hist_elt_data_free(elt_data);
1425 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1427 size = STR_VAR_LEN_MAX;
1429 for (i = 0; i < n_str; i++) {
1430 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1431 if (!elt_data->field_var_str[i]) {
1432 hist_elt_data_free(elt_data);
1437 elt->private_data = elt_data;
1442 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1444 struct hist_elt_data *elt_data = elt->private_data;
1447 save_comm(elt_data->comm, current);
1450 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1451 .elt_alloc = hist_trigger_elt_data_alloc,
1452 .elt_free = hist_trigger_elt_data_free,
1453 .elt_init = hist_trigger_elt_data_init,
1456 static const char *get_hist_field_flags(struct hist_field *hist_field)
1458 const char *flags_str = NULL;
1460 if (hist_field->flags & HIST_FIELD_FL_HEX)
1462 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1464 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1465 flags_str = "sym-offset";
1466 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1467 flags_str = "execname";
1468 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1469 flags_str = "syscall";
1470 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1472 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1473 flags_str = "usecs";
1478 static void expr_field_str(struct hist_field *field, char *expr)
1480 if (field->flags & HIST_FIELD_FL_VAR_REF)
1483 strcat(expr, hist_field_name(field, 0));
1485 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1486 const char *flags_str = get_hist_field_flags(field);
1490 strcat(expr, flags_str);
1495 static char *expr_str(struct hist_field *field, unsigned int level)
1502 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1506 if (!field->operands[0]) {
1507 expr_field_str(field, expr);
1511 if (field->operator == FIELD_OP_UNARY_MINUS) {
1515 subexpr = expr_str(field->operands[0], ++level);
1520 strcat(expr, subexpr);
1528 expr_field_str(field->operands[0], expr);
1530 switch (field->operator) {
1531 case FIELD_OP_MINUS:
1542 expr_field_str(field->operands[1], expr);
1547 static int contains_operator(char *str)
1549 enum field_op_id field_op = FIELD_OP_NONE;
1552 op = strpbrk(str, "+-");
1554 return FIELD_OP_NONE;
1559 field_op = FIELD_OP_UNARY_MINUS;
1561 field_op = FIELD_OP_MINUS;
1564 field_op = FIELD_OP_PLUS;
1573 static void get_hist_field(struct hist_field *hist_field)
1578 static void __destroy_hist_field(struct hist_field *hist_field)
1580 if (--hist_field->ref > 1)
1583 kfree(hist_field->var.name);
1584 kfree(hist_field->name);
1585 kfree(hist_field->type);
1587 kfree(hist_field->system);
1588 kfree(hist_field->event_name);
1593 static void destroy_hist_field(struct hist_field *hist_field,
1604 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1605 return; /* var refs will be destroyed separately */
1607 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1608 destroy_hist_field(hist_field->operands[i], level + 1);
1610 __destroy_hist_field(hist_field);
1613 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1614 struct ftrace_event_field *field,
1615 unsigned long flags,
1618 struct hist_field *hist_field;
1620 if (field && is_function_field(field))
1623 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1627 hist_field->ref = 1;
1629 hist_field->hist_data = hist_data;
1631 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1632 goto out; /* caller will populate */
1634 if (flags & HIST_FIELD_FL_VAR_REF) {
1635 hist_field->fn = hist_field_var_ref;
1639 if (flags & HIST_FIELD_FL_HITCOUNT) {
1640 hist_field->fn = hist_field_counter;
1641 hist_field->size = sizeof(u64);
1642 hist_field->type = kstrdup("u64", GFP_KERNEL);
1643 if (!hist_field->type)
1648 if (flags & HIST_FIELD_FL_STACKTRACE) {
1649 hist_field->fn = hist_field_none;
1653 if (flags & HIST_FIELD_FL_LOG2) {
1654 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
1655 hist_field->fn = hist_field_log2;
1656 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1657 hist_field->size = hist_field->operands[0]->size;
1658 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
1659 if (!hist_field->type)
1664 if (flags & HIST_FIELD_FL_TIMESTAMP) {
1665 hist_field->fn = hist_field_timestamp;
1666 hist_field->size = sizeof(u64);
1667 hist_field->type = kstrdup("u64", GFP_KERNEL);
1668 if (!hist_field->type)
1673 if (flags & HIST_FIELD_FL_CPU) {
1674 hist_field->fn = hist_field_cpu;
1675 hist_field->size = sizeof(int);
1676 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
1677 if (!hist_field->type)
1682 if (WARN_ON_ONCE(!field))
1685 if (is_string_field(field)) {
1686 flags |= HIST_FIELD_FL_STRING;
1688 hist_field->size = MAX_FILTER_STR_VAL;
1689 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1690 if (!hist_field->type)
1693 if (field->filter_type == FILTER_STATIC_STRING)
1694 hist_field->fn = hist_field_string;
1695 else if (field->filter_type == FILTER_DYN_STRING)
1696 hist_field->fn = hist_field_dynstring;
1698 hist_field->fn = hist_field_pstring;
1700 hist_field->size = field->size;
1701 hist_field->is_signed = field->is_signed;
1702 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1703 if (!hist_field->type)
1706 hist_field->fn = select_value_fn(field->size,
1708 if (!hist_field->fn) {
1709 destroy_hist_field(hist_field, 0);
1714 hist_field->field = field;
1715 hist_field->flags = flags;
1718 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
1719 if (!hist_field->var.name)
1725 destroy_hist_field(hist_field, 0);
1729 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
1733 for (i = 0; i < HIST_FIELDS_MAX; i++) {
1734 if (hist_data->fields[i]) {
1735 destroy_hist_field(hist_data->fields[i], 0);
1736 hist_data->fields[i] = NULL;
1740 for (i = 0; i < hist_data->n_var_refs; i++) {
1741 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
1742 __destroy_hist_field(hist_data->var_refs[i]);
1743 hist_data->var_refs[i] = NULL;
1747 static int init_var_ref(struct hist_field *ref_field,
1748 struct hist_field *var_field,
1749 char *system, char *event_name)
1753 ref_field->var.idx = var_field->var.idx;
1754 ref_field->var.hist_data = var_field->hist_data;
1755 ref_field->size = var_field->size;
1756 ref_field->is_signed = var_field->is_signed;
1757 ref_field->flags |= var_field->flags &
1758 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
1761 ref_field->system = kstrdup(system, GFP_KERNEL);
1762 if (!ref_field->system)
1767 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
1768 if (!ref_field->event_name) {
1774 if (var_field->var.name) {
1775 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
1776 if (!ref_field->name) {
1780 } else if (var_field->name) {
1781 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
1782 if (!ref_field->name) {
1788 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
1789 if (!ref_field->type) {
1796 kfree(ref_field->system);
1797 kfree(ref_field->event_name);
1798 kfree(ref_field->name);
1803 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
1804 struct hist_field *var_field)
1806 struct hist_field *ref_field;
1809 for (i = 0; i < hist_data->n_var_refs; i++) {
1810 ref_field = hist_data->var_refs[i];
1811 if (ref_field->var.idx == var_field->var.idx &&
1812 ref_field->var.hist_data == var_field->hist_data)
1820 * create_var_ref - Create a variable reference and attach it to trigger
1821 * @hist_data: The trigger that will be referencing the variable
1822 * @var_field: The VAR field to create a reference to
1823 * @system: The optional system string
1824 * @event_name: The optional event_name string
1826 * Given a variable hist_field, create a VAR_REF hist_field that
1827 * represents a reference to it.
1829 * This function also adds the reference to the trigger that
1830 * now references the variable.
1832 * Return: The VAR_REF field if successful, NULL if not
1834 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
1835 struct hist_field *var_field,
1836 char *system, char *event_name)
1838 unsigned long flags = HIST_FIELD_FL_VAR_REF;
1839 struct hist_field *ref_field;
1842 /* Check if the variable already exists */
1843 for (i = 0; i < hist_data->n_var_refs; i++) {
1844 ref_field = hist_data->var_refs[i];
1845 if (ref_field->var.idx == var_field->var.idx &&
1846 ref_field->var.hist_data == var_field->hist_data) {
1847 get_hist_field(ref_field);
1852 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
1854 if (init_var_ref(ref_field, var_field, system, event_name)) {
1855 destroy_hist_field(ref_field, 0);
1859 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
1860 ref_field->var_ref_idx = hist_data->n_var_refs++;
1866 static bool is_var_ref(char *var_name)
1868 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
1874 static char *field_name_from_var(struct hist_trigger_data *hist_data,
1880 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
1881 name = hist_data->attrs->var_defs.name[i];
1883 if (strcmp(var_name, name) == 0) {
1884 field = hist_data->attrs->var_defs.expr[i];
1885 if (contains_operator(field) || is_var_ref(field))
1894 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
1895 char *system, char *event_name,
1898 struct trace_event_call *call;
1900 if (system && event_name) {
1901 call = hist_data->event_file->event_call;
1903 if (strcmp(system, call->class->system) != 0)
1906 if (strcmp(event_name, trace_event_name(call)) != 0)
1910 if (!!system != !!event_name)
1913 if (!is_var_ref(var_name))
1918 return field_name_from_var(hist_data, var_name);
1921 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
1922 char *system, char *event_name,
1925 struct hist_field *var_field = NULL, *ref_field = NULL;
1926 struct trace_array *tr = hist_data->event_file->tr;
1928 if (!is_var_ref(var_name))
1933 var_field = find_event_var(hist_data, system, event_name, var_name);
1935 ref_field = create_var_ref(hist_data, var_field,
1936 system, event_name);
1939 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
1944 static struct ftrace_event_field *
1945 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
1946 char *field_str, unsigned long *flags)
1948 struct ftrace_event_field *field = NULL;
1949 char *field_name, *modifier, *str;
1950 struct trace_array *tr = file->tr;
1952 modifier = str = kstrdup(field_str, GFP_KERNEL);
1954 return ERR_PTR(-ENOMEM);
1956 field_name = strsep(&modifier, ".");
1958 if (strcmp(modifier, "hex") == 0)
1959 *flags |= HIST_FIELD_FL_HEX;
1960 else if (strcmp(modifier, "sym") == 0)
1961 *flags |= HIST_FIELD_FL_SYM;
1962 else if (strcmp(modifier, "sym-offset") == 0)
1963 *flags |= HIST_FIELD_FL_SYM_OFFSET;
1964 else if ((strcmp(modifier, "execname") == 0) &&
1965 (strcmp(field_name, "common_pid") == 0))
1966 *flags |= HIST_FIELD_FL_EXECNAME;
1967 else if (strcmp(modifier, "syscall") == 0)
1968 *flags |= HIST_FIELD_FL_SYSCALL;
1969 else if (strcmp(modifier, "log2") == 0)
1970 *flags |= HIST_FIELD_FL_LOG2;
1971 else if (strcmp(modifier, "usecs") == 0)
1972 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
1974 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
1975 field = ERR_PTR(-EINVAL);
1980 if (strcmp(field_name, "common_timestamp") == 0) {
1981 *flags |= HIST_FIELD_FL_TIMESTAMP;
1982 hist_data->enable_timestamps = true;
1983 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1984 hist_data->attrs->ts_in_usecs = true;
1985 } else if (strcmp(field_name, "cpu") == 0)
1986 *flags |= HIST_FIELD_FL_CPU;
1988 field = trace_find_event_field(file->event_call, field_name);
1989 if (!field || !field->size) {
1990 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
1991 field = ERR_PTR(-EINVAL);
2001 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2002 struct hist_field *var_ref,
2005 struct hist_field *alias = NULL;
2006 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2008 alias = create_hist_field(hist_data, NULL, flags, var_name);
2012 alias->fn = var_ref->fn;
2013 alias->operands[0] = var_ref;
2015 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2016 destroy_hist_field(alias, 0);
2020 alias->var_ref_idx = var_ref->var_ref_idx;
2025 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2026 struct trace_event_file *file, char *str,
2027 unsigned long *flags, char *var_name)
2029 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2030 struct ftrace_event_field *field = NULL;
2031 struct hist_field *hist_field = NULL;
2034 s = strchr(str, '.');
2036 s = strchr(++s, '.');
2038 ref_system = strsep(&str, ".");
2043 ref_event = strsep(&str, ".");
2052 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2054 hist_field = parse_var_ref(hist_data, ref_system,
2055 ref_event, ref_var);
2058 hist_field = create_alias(hist_data, hist_field, var_name);
2069 field = parse_field(hist_data, file, str, flags);
2070 if (IS_ERR(field)) {
2071 ret = PTR_ERR(field);
2075 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2083 return ERR_PTR(ret);
2086 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2087 struct trace_event_file *file,
2088 char *str, unsigned long flags,
2089 char *var_name, unsigned int level);
2091 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2092 struct trace_event_file *file,
2093 char *str, unsigned long flags,
2094 char *var_name, unsigned int level)
2096 struct hist_field *operand1, *expr = NULL;
2097 unsigned long operand_flags;
2101 /* we support only -(xxx) i.e. explicit parens required */
2104 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2109 str++; /* skip leading '-' */
2111 s = strchr(str, '(');
2119 s = strrchr(str, ')');
2123 ret = -EINVAL; /* no closing ')' */
2127 flags |= HIST_FIELD_FL_EXPR;
2128 expr = create_hist_field(hist_data, NULL, flags, var_name);
2135 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2136 if (IS_ERR(operand1)) {
2137 ret = PTR_ERR(operand1);
2141 expr->flags |= operand1->flags &
2142 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2143 expr->fn = hist_field_unary_minus;
2144 expr->operands[0] = operand1;
2145 expr->operator = FIELD_OP_UNARY_MINUS;
2146 expr->name = expr_str(expr, 0);
2147 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2155 destroy_hist_field(expr, 0);
2156 return ERR_PTR(ret);
2159 static int check_expr_operands(struct trace_array *tr,
2160 struct hist_field *operand1,
2161 struct hist_field *operand2)
2163 unsigned long operand1_flags = operand1->flags;
2164 unsigned long operand2_flags = operand2->flags;
2166 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2167 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2168 struct hist_field *var;
2170 var = find_var_field(operand1->var.hist_data, operand1->name);
2173 operand1_flags = var->flags;
2176 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2177 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2178 struct hist_field *var;
2180 var = find_var_field(operand2->var.hist_data, operand2->name);
2183 operand2_flags = var->flags;
2186 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2187 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2188 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2195 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2196 struct trace_event_file *file,
2197 char *str, unsigned long flags,
2198 char *var_name, unsigned int level)
2200 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2201 unsigned long operand_flags;
2202 int field_op, ret = -EINVAL;
2203 char *sep, *operand1_str;
2206 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2207 return ERR_PTR(-EINVAL);
2210 field_op = contains_operator(str);
2212 if (field_op == FIELD_OP_NONE)
2213 return parse_atom(hist_data, file, str, &flags, var_name);
2215 if (field_op == FIELD_OP_UNARY_MINUS)
2216 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2219 case FIELD_OP_MINUS:
2229 operand1_str = strsep(&str, sep);
2230 if (!operand1_str || !str)
2234 operand1 = parse_atom(hist_data, file, operand1_str,
2235 &operand_flags, NULL);
2236 if (IS_ERR(operand1)) {
2237 ret = PTR_ERR(operand1);
2242 /* rest of string could be another expression e.g. b+c in a+b+c */
2244 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2245 if (IS_ERR(operand2)) {
2246 ret = PTR_ERR(operand2);
2251 ret = check_expr_operands(file->tr, operand1, operand2);
2255 flags |= HIST_FIELD_FL_EXPR;
2257 flags |= operand1->flags &
2258 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2260 expr = create_hist_field(hist_data, NULL, flags, var_name);
2266 operand1->read_once = true;
2267 operand2->read_once = true;
2269 expr->operands[0] = operand1;
2270 expr->operands[1] = operand2;
2271 expr->operator = field_op;
2272 expr->name = expr_str(expr, 0);
2273 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2280 case FIELD_OP_MINUS:
2281 expr->fn = hist_field_minus;
2284 expr->fn = hist_field_plus;
2293 destroy_hist_field(operand1, 0);
2294 destroy_hist_field(operand2, 0);
2295 destroy_hist_field(expr, 0);
2297 return ERR_PTR(ret);
2300 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2301 struct trace_event_file *file)
2303 struct event_trigger_data *test;
2305 lockdep_assert_held(&event_mutex);
2307 list_for_each_entry(test, &file->triggers, list) {
2308 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2309 if (test->private_data == hist_data)
2310 return test->filter_str;
2317 static struct event_command trigger_hist_cmd;
2318 static int event_hist_trigger_func(struct event_command *cmd_ops,
2319 struct trace_event_file *file,
2320 char *glob, char *cmd, char *param);
2322 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2323 struct hist_trigger_data *hist_data,
2324 unsigned int n_keys)
2326 struct hist_field *target_hist_field, *hist_field;
2327 unsigned int n, i, j;
2329 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2332 i = hist_data->n_vals;
2333 j = target_hist_data->n_vals;
2335 for (n = 0; n < n_keys; n++) {
2336 hist_field = hist_data->fields[i + n];
2337 target_hist_field = target_hist_data->fields[j + n];
2339 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2341 if (hist_field->size != target_hist_field->size)
2343 if (hist_field->is_signed != target_hist_field->is_signed)
2350 static struct hist_trigger_data *
2351 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2352 struct trace_event_file *file)
2354 struct hist_trigger_data *hist_data;
2355 struct event_trigger_data *test;
2356 unsigned int n_keys;
2358 lockdep_assert_held(&event_mutex);
2360 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2362 list_for_each_entry(test, &file->triggers, list) {
2363 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2364 hist_data = test->private_data;
2366 if (compatible_keys(target_hist_data, hist_data, n_keys))
2374 static struct trace_event_file *event_file(struct trace_array *tr,
2375 char *system, char *event_name)
2377 struct trace_event_file *file;
2379 file = __find_event_file(tr, system, event_name);
2381 return ERR_PTR(-EINVAL);
2386 static struct hist_field *
2387 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2388 char *system, char *event_name, char *field_name)
2390 struct hist_field *event_var;
2391 char *synthetic_name;
2393 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2394 if (!synthetic_name)
2395 return ERR_PTR(-ENOMEM);
2397 strcpy(synthetic_name, "synthetic_");
2398 strcat(synthetic_name, field_name);
2400 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2402 kfree(synthetic_name);
2408 * create_field_var_hist - Automatically create a histogram and var for a field
2409 * @target_hist_data: The target hist trigger
2410 * @subsys_name: Optional subsystem name
2411 * @event_name: Optional event name
2412 * @field_name: The name of the field (and the resulting variable)
2414 * Hist trigger actions fetch data from variables, not directly from
2415 * events. However, for convenience, users are allowed to directly
2416 * specify an event field in an action, which will be automatically
2417 * converted into a variable on their behalf.
2419 * If a user specifies a field on an event that isn't the event the
2420 * histogram currently being defined (the target event histogram), the
2421 * only way that can be accomplished is if a new hist trigger is
2422 * created and the field variable defined on that.
2424 * This function creates a new histogram compatible with the target
2425 * event (meaning a histogram with the same key as the target
2426 * histogram), and creates a variable for the specified field, but
2427 * with 'synthetic_' prepended to the variable name in order to avoid
2428 * collision with normal field variables.
2430 * Return: The variable created for the field.
2432 static struct hist_field *
2433 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2434 char *subsys_name, char *event_name, char *field_name)
2436 struct trace_array *tr = target_hist_data->event_file->tr;
2437 struct hist_field *event_var = ERR_PTR(-EINVAL);
2438 struct hist_trigger_data *hist_data;
2439 unsigned int i, n, first = true;
2440 struct field_var_hist *var_hist;
2441 struct trace_event_file *file;
2442 struct hist_field *key_field;
2447 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2448 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2449 return ERR_PTR(-EINVAL);
2452 file = event_file(tr, subsys_name, event_name);
2455 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2456 ret = PTR_ERR(file);
2457 return ERR_PTR(ret);
2461 * Look for a histogram compatible with target. We'll use the
2462 * found histogram specification to create a new matching
2463 * histogram with our variable on it. target_hist_data is not
2464 * yet a registered histogram so we can't use that.
2466 hist_data = find_compatible_hist(target_hist_data, file);
2468 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2469 return ERR_PTR(-EINVAL);
2472 /* See if a synthetic field variable has already been created */
2473 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2474 event_name, field_name);
2475 if (!IS_ERR_OR_NULL(event_var))
2478 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2480 return ERR_PTR(-ENOMEM);
2482 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2485 return ERR_PTR(-ENOMEM);
2488 /* Use the same keys as the compatible histogram */
2489 strcat(cmd, "keys=");
2491 for_each_hist_key_field(i, hist_data) {
2492 key_field = hist_data->fields[i];
2495 strcat(cmd, key_field->field->name);
2499 /* Create the synthetic field variable specification */
2500 strcat(cmd, ":synthetic_");
2501 strcat(cmd, field_name);
2503 strcat(cmd, field_name);
2505 /* Use the same filter as the compatible histogram */
2506 saved_filter = find_trigger_filter(hist_data, file);
2508 strcat(cmd, " if ");
2509 strcat(cmd, saved_filter);
2512 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
2513 if (!var_hist->cmd) {
2516 return ERR_PTR(-ENOMEM);
2519 /* Save the compatible histogram information */
2520 var_hist->hist_data = hist_data;
2522 /* Create the new histogram with our variable */
2523 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
2527 kfree(var_hist->cmd);
2529 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
2530 return ERR_PTR(ret);
2535 /* If we can't find the variable, something went wrong */
2536 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2537 event_name, field_name);
2538 if (IS_ERR_OR_NULL(event_var)) {
2539 kfree(var_hist->cmd);
2541 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
2542 return ERR_PTR(-EINVAL);
2545 n = target_hist_data->n_field_var_hists;
2546 target_hist_data->field_var_hists[n] = var_hist;
2547 target_hist_data->n_field_var_hists++;
2552 static struct hist_field *
2553 find_target_event_var(struct hist_trigger_data *hist_data,
2554 char *subsys_name, char *event_name, char *var_name)
2556 struct trace_event_file *file = hist_data->event_file;
2557 struct hist_field *hist_field = NULL;
2560 struct trace_event_call *call;
2565 call = file->event_call;
2567 if (strcmp(subsys_name, call->class->system) != 0)
2570 if (strcmp(event_name, trace_event_name(call)) != 0)
2574 hist_field = find_var_field(hist_data, var_name);
2579 static inline void __update_field_vars(struct tracing_map_elt *elt,
2580 struct trace_buffer *buffer,
2581 struct ring_buffer_event *rbe,
2583 struct field_var **field_vars,
2584 unsigned int n_field_vars,
2585 unsigned int field_var_str_start)
2587 struct hist_elt_data *elt_data = elt->private_data;
2588 unsigned int i, j, var_idx;
2591 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
2592 struct field_var *field_var = field_vars[i];
2593 struct hist_field *var = field_var->var;
2594 struct hist_field *val = field_var->val;
2596 var_val = val->fn(val, elt, buffer, rbe, rec);
2597 var_idx = var->var.idx;
2599 if (val->flags & HIST_FIELD_FL_STRING) {
2600 char *str = elt_data->field_var_str[j++];
2601 char *val_str = (char *)(uintptr_t)var_val;
2603 strscpy(str, val_str, STR_VAR_LEN_MAX);
2604 var_val = (u64)(uintptr_t)str;
2606 tracing_map_set_var(elt, var_idx, var_val);
2610 static void update_field_vars(struct hist_trigger_data *hist_data,
2611 struct tracing_map_elt *elt,
2612 struct trace_buffer *buffer,
2613 struct ring_buffer_event *rbe,
2616 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
2617 hist_data->n_field_vars, 0);
2620 static void save_track_data_vars(struct hist_trigger_data *hist_data,
2621 struct tracing_map_elt *elt,
2622 struct trace_buffer *buffer, void *rec,
2623 struct ring_buffer_event *rbe, void *key,
2624 struct action_data *data, u64 *var_ref_vals)
2626 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
2627 hist_data->n_save_vars, hist_data->n_field_var_str);
2630 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
2631 struct trace_event_file *file,
2632 char *name, int size, const char *type)
2634 struct hist_field *var;
2637 if (find_var(hist_data, file, name) && !hist_data->remove) {
2638 var = ERR_PTR(-EINVAL);
2642 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2644 var = ERR_PTR(-ENOMEM);
2648 idx = tracing_map_add_var(hist_data->map);
2651 var = ERR_PTR(-EINVAL);
2656 var->flags = HIST_FIELD_FL_VAR;
2658 var->var.hist_data = var->hist_data = hist_data;
2660 var->var.name = kstrdup(name, GFP_KERNEL);
2661 var->type = kstrdup(type, GFP_KERNEL);
2662 if (!var->var.name || !var->type) {
2663 kfree(var->var.name);
2666 var = ERR_PTR(-ENOMEM);
2672 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
2673 struct trace_event_file *file,
2676 struct hist_field *val = NULL, *var = NULL;
2677 unsigned long flags = HIST_FIELD_FL_VAR;
2678 struct trace_array *tr = file->tr;
2679 struct field_var *field_var;
2682 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
2683 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2688 val = parse_atom(hist_data, file, field_name, &flags, NULL);
2690 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
2695 var = create_var(hist_data, file, field_name, val->size, val->type);
2697 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
2703 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
2711 field_var->var = var;
2712 field_var->val = val;
2716 field_var = ERR_PTR(ret);
2721 * create_target_field_var - Automatically create a variable for a field
2722 * @target_hist_data: The target hist trigger
2723 * @subsys_name: Optional subsystem name
2724 * @event_name: Optional event name
2725 * @var_name: The name of the field (and the resulting variable)
2727 * Hist trigger actions fetch data from variables, not directly from
2728 * events. However, for convenience, users are allowed to directly
2729 * specify an event field in an action, which will be automatically
2730 * converted into a variable on their behalf.
2732 * This function creates a field variable with the name var_name on
2733 * the hist trigger currently being defined on the target event. If
2734 * subsys_name and event_name are specified, this function simply
2735 * verifies that they do in fact match the target event subsystem and
2738 * Return: The variable created for the field.
2740 static struct field_var *
2741 create_target_field_var(struct hist_trigger_data *target_hist_data,
2742 char *subsys_name, char *event_name, char *var_name)
2744 struct trace_event_file *file = target_hist_data->event_file;
2747 struct trace_event_call *call;
2752 call = file->event_call;
2754 if (strcmp(subsys_name, call->class->system) != 0)
2757 if (strcmp(event_name, trace_event_name(call)) != 0)
2761 return create_field_var(target_hist_data, file, var_name);
2764 static bool check_track_val_max(u64 track_val, u64 var_val)
2766 if (var_val <= track_val)
2772 static bool check_track_val_changed(u64 track_val, u64 var_val)
2774 if (var_val == track_val)
2780 static u64 get_track_val(struct hist_trigger_data *hist_data,
2781 struct tracing_map_elt *elt,
2782 struct action_data *data)
2784 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2787 track_val = tracing_map_read_var(elt, track_var_idx);
2792 static void save_track_val(struct hist_trigger_data *hist_data,
2793 struct tracing_map_elt *elt,
2794 struct action_data *data, u64 var_val)
2796 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2798 tracing_map_set_var(elt, track_var_idx, var_val);
2801 static void save_track_data(struct hist_trigger_data *hist_data,
2802 struct tracing_map_elt *elt,
2803 struct trace_buffer *buffer, void *rec,
2804 struct ring_buffer_event *rbe, void *key,
2805 struct action_data *data, u64 *var_ref_vals)
2807 if (data->track_data.save_data)
2808 data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
2809 key, data, var_ref_vals);
2812 static bool check_track_val(struct tracing_map_elt *elt,
2813 struct action_data *data,
2816 struct hist_trigger_data *hist_data;
2819 hist_data = data->track_data.track_var->hist_data;
2820 track_val = get_track_val(hist_data, elt, data);
2822 return data->track_data.check_val(track_val, var_val);
2825 #ifdef CONFIG_TRACER_SNAPSHOT
2826 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2828 /* called with tr->max_lock held */
2829 struct track_data *track_data = tr->cond_snapshot->cond_data;
2830 struct hist_elt_data *elt_data, *track_elt_data;
2831 struct snapshot_context *context = cond_data;
2832 struct action_data *action;
2838 action = track_data->action_data;
2840 track_val = get_track_val(track_data->hist_data, context->elt,
2841 track_data->action_data);
2843 if (!action->track_data.check_val(track_data->track_val, track_val))
2846 track_data->track_val = track_val;
2847 memcpy(track_data->key, context->key, track_data->key_len);
2849 elt_data = context->elt->private_data;
2850 track_elt_data = track_data->elt.private_data;
2852 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
2854 track_data->updated = true;
2859 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2860 struct tracing_map_elt *elt,
2861 struct trace_buffer *buffer, void *rec,
2862 struct ring_buffer_event *rbe, void *key,
2863 struct action_data *data,
2866 struct trace_event_file *file = hist_data->event_file;
2867 struct snapshot_context context;
2872 tracing_snapshot_cond(file->tr, &context);
2875 static void hist_trigger_print_key(struct seq_file *m,
2876 struct hist_trigger_data *hist_data,
2878 struct tracing_map_elt *elt);
2880 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
2884 if (!hist_data->n_actions)
2887 for (i = 0; i < hist_data->n_actions; i++) {
2888 struct action_data *data = hist_data->actions[i];
2890 if (data->action == ACTION_SNAPSHOT)
2897 static void track_data_snapshot_print(struct seq_file *m,
2898 struct hist_trigger_data *hist_data)
2900 struct trace_event_file *file = hist_data->event_file;
2901 struct track_data *track_data;
2902 struct action_data *action;
2904 track_data = tracing_cond_snapshot_data(file->tr);
2908 if (!track_data->updated)
2911 action = snapshot_action(hist_data);
2915 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
2916 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
2917 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
2918 action->track_data.var_str, track_data->track_val);
2920 seq_puts(m, "\ttriggered by event with key: ");
2921 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
2925 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2929 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2930 struct tracing_map_elt *elt,
2931 struct trace_buffer *buffer, void *rec,
2932 struct ring_buffer_event *rbe, void *key,
2933 struct action_data *data,
2934 u64 *var_ref_vals) {}
2935 static void track_data_snapshot_print(struct seq_file *m,
2936 struct hist_trigger_data *hist_data) {}
2937 #endif /* CONFIG_TRACER_SNAPSHOT */
2939 static void track_data_print(struct seq_file *m,
2940 struct hist_trigger_data *hist_data,
2941 struct tracing_map_elt *elt,
2942 struct action_data *data)
2944 u64 track_val = get_track_val(hist_data, elt, data);
2945 unsigned int i, save_var_idx;
2947 if (data->handler == HANDLER_ONMAX)
2948 seq_printf(m, "\n\tmax: %10llu", track_val);
2949 else if (data->handler == HANDLER_ONCHANGE)
2950 seq_printf(m, "\n\tchanged: %10llu", track_val);
2952 if (data->action == ACTION_SNAPSHOT)
2955 for (i = 0; i < hist_data->n_save_vars; i++) {
2956 struct hist_field *save_val = hist_data->save_vars[i]->val;
2957 struct hist_field *save_var = hist_data->save_vars[i]->var;
2960 save_var_idx = save_var->var.idx;
2962 val = tracing_map_read_var(elt, save_var_idx);
2964 if (save_val->flags & HIST_FIELD_FL_STRING) {
2965 seq_printf(m, " %s: %-32s", save_var->var.name,
2966 (char *)(uintptr_t)(val));
2968 seq_printf(m, " %s: %10llu", save_var->var.name, val);
2972 static void ontrack_action(struct hist_trigger_data *hist_data,
2973 struct tracing_map_elt *elt,
2974 struct trace_buffer *buffer, void *rec,
2975 struct ring_buffer_event *rbe, void *key,
2976 struct action_data *data, u64 *var_ref_vals)
2978 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
2980 if (check_track_val(elt, data, var_val)) {
2981 save_track_val(hist_data, elt, data, var_val);
2982 save_track_data(hist_data, elt, buffer, rec, rbe,
2983 key, data, var_ref_vals);
2987 static void action_data_destroy(struct action_data *data)
2991 lockdep_assert_held(&event_mutex);
2993 kfree(data->action_name);
2995 for (i = 0; i < data->n_params; i++)
2996 kfree(data->params[i]);
2998 if (data->synth_event)
2999 data->synth_event->ref--;
3001 kfree(data->synth_event_name);
3006 static void track_data_destroy(struct hist_trigger_data *hist_data,
3007 struct action_data *data)
3009 struct trace_event_file *file = hist_data->event_file;
3011 destroy_hist_field(data->track_data.track_var, 0);
3013 if (data->action == ACTION_SNAPSHOT) {
3014 struct track_data *track_data;
3016 track_data = tracing_cond_snapshot_data(file->tr);
3017 if (track_data && track_data->hist_data == hist_data) {
3018 tracing_snapshot_cond_disable(file->tr);
3019 track_data_free(track_data);
3023 kfree(data->track_data.var_str);
3025 action_data_destroy(data);
3028 static int action_create(struct hist_trigger_data *hist_data,
3029 struct action_data *data);
3031 static int track_data_create(struct hist_trigger_data *hist_data,
3032 struct action_data *data)
3034 struct hist_field *var_field, *ref_field, *track_var = NULL;
3035 struct trace_event_file *file = hist_data->event_file;
3036 struct trace_array *tr = file->tr;
3037 char *track_data_var_str;
3040 track_data_var_str = data->track_data.var_str;
3041 if (track_data_var_str[0] != '$') {
3042 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3045 track_data_var_str++;
3047 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3049 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3053 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3057 data->track_data.var_ref = ref_field;
3059 if (data->handler == HANDLER_ONMAX)
3060 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3061 if (IS_ERR(track_var)) {
3062 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3063 ret = PTR_ERR(track_var);
3067 if (data->handler == HANDLER_ONCHANGE)
3068 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3069 if (IS_ERR(track_var)) {
3070 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3071 ret = PTR_ERR(track_var);
3074 data->track_data.track_var = track_var;
3076 ret = action_create(hist_data, data);
3081 static int parse_action_params(struct trace_array *tr, char *params,
3082 struct action_data *data)
3084 char *param, *saved_param;
3085 bool first_param = true;
3089 if (data->n_params >= SYNTH_FIELDS_MAX) {
3090 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3094 param = strsep(¶ms, ",");
3096 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3101 param = strstrip(param);
3102 if (strlen(param) < 2) {
3103 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3108 saved_param = kstrdup(param, GFP_KERNEL);
3114 if (first_param && data->use_trace_keyword) {
3115 data->synth_event_name = saved_param;
3116 first_param = false;
3119 first_param = false;
3121 data->params[data->n_params++] = saved_param;
3127 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3128 enum handler_id handler)
3135 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3140 action_name = strsep(&str, "(");
3141 if (!action_name || !str) {
3142 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3147 if (str_has_prefix(action_name, "save")) {
3148 char *params = strsep(&str, ")");
3151 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3156 ret = parse_action_params(tr, params, data);
3160 if (handler == HANDLER_ONMAX)
3161 data->track_data.check_val = check_track_val_max;
3162 else if (handler == HANDLER_ONCHANGE)
3163 data->track_data.check_val = check_track_val_changed;
3165 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3170 data->track_data.save_data = save_track_data_vars;
3171 data->fn = ontrack_action;
3172 data->action = ACTION_SAVE;
3173 } else if (str_has_prefix(action_name, "snapshot")) {
3174 char *params = strsep(&str, ")");
3177 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3182 if (handler == HANDLER_ONMAX)
3183 data->track_data.check_val = check_track_val_max;
3184 else if (handler == HANDLER_ONCHANGE)
3185 data->track_data.check_val = check_track_val_changed;
3187 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3192 data->track_data.save_data = save_track_data_snapshot;
3193 data->fn = ontrack_action;
3194 data->action = ACTION_SNAPSHOT;
3196 char *params = strsep(&str, ")");
3198 if (str_has_prefix(action_name, "trace"))
3199 data->use_trace_keyword = true;
3202 ret = parse_action_params(tr, params, data);
3207 if (handler == HANDLER_ONMAX)
3208 data->track_data.check_val = check_track_val_max;
3209 else if (handler == HANDLER_ONCHANGE)
3210 data->track_data.check_val = check_track_val_changed;
3212 if (handler != HANDLER_ONMATCH) {
3213 data->track_data.save_data = action_trace;
3214 data->fn = ontrack_action;
3216 data->fn = action_trace;
3218 data->action = ACTION_TRACE;
3221 data->action_name = kstrdup(action_name, GFP_KERNEL);
3222 if (!data->action_name) {
3227 data->handler = handler;
3232 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3233 char *str, enum handler_id handler)
3235 struct action_data *data;
3239 data = kzalloc(sizeof(*data), GFP_KERNEL);
3241 return ERR_PTR(-ENOMEM);
3243 var_str = strsep(&str, ")");
3244 if (!var_str || !str) {
3249 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3250 if (!data->track_data.var_str) {
3255 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3261 track_data_destroy(hist_data, data);
3262 data = ERR_PTR(ret);
3266 static void onmatch_destroy(struct action_data *data)
3268 kfree(data->match_data.event);
3269 kfree(data->match_data.event_system);
3271 action_data_destroy(data);
3274 static void destroy_field_var(struct field_var *field_var)
3279 destroy_hist_field(field_var->var, 0);
3280 destroy_hist_field(field_var->val, 0);
3285 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3289 for (i = 0; i < hist_data->n_field_vars; i++)
3290 destroy_field_var(hist_data->field_vars[i]);
3292 for (i = 0; i < hist_data->n_save_vars; i++)
3293 destroy_field_var(hist_data->save_vars[i]);
3296 static void save_field_var(struct hist_trigger_data *hist_data,
3297 struct field_var *field_var)
3299 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3301 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3302 hist_data->n_field_var_str++;
3306 static int check_synth_field(struct synth_event *event,
3307 struct hist_field *hist_field,
3308 unsigned int field_pos)
3310 struct synth_field *field;
3312 if (field_pos >= event->n_fields)
3315 field = event->fields[field_pos];
3318 * A dynamic string synth field can accept static or
3319 * dynamic. A static string synth field can only accept a
3320 * same-sized static string, which is checked for later.
3322 if (strstr(hist_field->type, "char[") && field->is_string
3323 && field->is_dynamic)
3326 if (strcmp(field->type, hist_field->type) != 0) {
3327 if (field->size != hist_field->size ||
3328 field->is_signed != hist_field->is_signed)
3335 static struct hist_field *
3336 trace_action_find_var(struct hist_trigger_data *hist_data,
3337 struct action_data *data,
3338 char *system, char *event, char *var)
3340 struct trace_array *tr = hist_data->event_file->tr;
3341 struct hist_field *hist_field;
3343 var++; /* skip '$' */
3345 hist_field = find_target_event_var(hist_data, system, event, var);
3347 if (!system && data->handler == HANDLER_ONMATCH) {
3348 system = data->match_data.event_system;
3349 event = data->match_data.event;
3352 hist_field = find_event_var(hist_data, system, event, var);
3356 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3361 static struct hist_field *
3362 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3363 struct action_data *data, char *system,
3364 char *event, char *var)
3366 struct hist_field *hist_field = NULL;
3367 struct field_var *field_var;
3370 * First try to create a field var on the target event (the
3371 * currently being defined). This will create a variable for
3372 * unqualified fields on the target event, or if qualified,
3373 * target fields that have qualified names matching the target.
3375 field_var = create_target_field_var(hist_data, system, event, var);
3377 if (field_var && !IS_ERR(field_var)) {
3378 save_field_var(hist_data, field_var);
3379 hist_field = field_var->var;
3383 * If no explicit system.event is specified, default to
3384 * looking for fields on the onmatch(system.event.xxx)
3387 if (!system && data->handler == HANDLER_ONMATCH) {
3388 system = data->match_data.event_system;
3389 event = data->match_data.event;
3393 * At this point, we're looking at a field on another
3394 * event. Because we can't modify a hist trigger on
3395 * another event to add a variable for a field, we need
3396 * to create a new trigger on that event and create the
3397 * variable at the same time.
3399 hist_field = create_field_var_hist(hist_data, system, event, var);
3400 if (IS_ERR(hist_field))
3406 destroy_field_var(field_var);
3411 static int trace_action_create(struct hist_trigger_data *hist_data,
3412 struct action_data *data)
3414 struct trace_array *tr = hist_data->event_file->tr;
3415 char *event_name, *param, *system = NULL;
3416 struct hist_field *hist_field, *var_ref;
3418 unsigned int field_pos = 0;
3419 struct synth_event *event;
3420 char *synth_event_name;
3421 int var_ref_idx, ret = 0;
3423 lockdep_assert_held(&event_mutex);
3425 if (data->use_trace_keyword)
3426 synth_event_name = data->synth_event_name;
3428 synth_event_name = data->action_name;
3430 event = find_synth_event(synth_event_name);
3432 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3438 for (i = 0; i < data->n_params; i++) {
3441 p = param = kstrdup(data->params[i], GFP_KERNEL);
3447 system = strsep(¶m, ".");
3449 param = (char *)system;
3450 system = event_name = NULL;
3452 event_name = strsep(¶m, ".");
3460 if (param[0] == '$')
3461 hist_field = trace_action_find_var(hist_data, data,
3465 hist_field = trace_action_create_field_var(hist_data,
3477 if (check_synth_field(event, hist_field, field_pos) == 0) {
3478 var_ref = create_var_ref(hist_data, hist_field,
3479 system, event_name);
3486 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
3487 if (WARN_ON(var_ref_idx < 0)) {
3492 data->var_ref_idx[i] = var_ref_idx;
3499 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
3505 if (field_pos != event->n_fields) {
3506 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
3511 data->synth_event = event;
3520 static int action_create(struct hist_trigger_data *hist_data,
3521 struct action_data *data)
3523 struct trace_event_file *file = hist_data->event_file;
3524 struct trace_array *tr = file->tr;
3525 struct track_data *track_data;
3526 struct field_var *field_var;
3531 if (data->action == ACTION_TRACE)
3532 return trace_action_create(hist_data, data);
3534 if (data->action == ACTION_SNAPSHOT) {
3535 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
3536 if (IS_ERR(track_data)) {
3537 ret = PTR_ERR(track_data);
3541 ret = tracing_snapshot_cond_enable(file->tr, track_data,
3542 cond_snapshot_update);
3544 track_data_free(track_data);
3549 if (data->action == ACTION_SAVE) {
3550 if (hist_data->n_save_vars) {
3552 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
3556 for (i = 0; i < data->n_params; i++) {
3557 param = kstrdup(data->params[i], GFP_KERNEL);
3563 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3564 if (IS_ERR(field_var)) {
3565 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
3567 ret = PTR_ERR(field_var);
3572 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
3573 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3574 hist_data->n_save_var_str++;
3582 static int onmatch_create(struct hist_trigger_data *hist_data,
3583 struct action_data *data)
3585 return action_create(hist_data, data);
3588 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3590 char *match_event, *match_event_system;
3591 struct action_data *data;
3594 data = kzalloc(sizeof(*data), GFP_KERNEL);
3596 return ERR_PTR(-ENOMEM);
3598 match_event = strsep(&str, ")");
3599 if (!match_event || !str) {
3600 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
3604 match_event_system = strsep(&match_event, ".");
3606 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
3610 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3611 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
3615 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
3616 if (!data->match_data.event) {
3621 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
3622 if (!data->match_data.event_system) {
3627 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
3633 onmatch_destroy(data);
3634 data = ERR_PTR(ret);
3638 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3640 hist_data->fields[HITCOUNT_IDX] =
3641 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3642 if (!hist_data->fields[HITCOUNT_IDX])
3645 hist_data->n_vals++;
3646 hist_data->n_fields++;
3648 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3654 static int __create_val_field(struct hist_trigger_data *hist_data,
3655 unsigned int val_idx,
3656 struct trace_event_file *file,
3657 char *var_name, char *field_str,
3658 unsigned long flags)
3660 struct hist_field *hist_field;
3663 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3664 if (IS_ERR(hist_field)) {
3665 ret = PTR_ERR(hist_field);
3669 hist_data->fields[val_idx] = hist_field;
3671 ++hist_data->n_vals;
3672 ++hist_data->n_fields;
3674 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3680 static int create_val_field(struct hist_trigger_data *hist_data,
3681 unsigned int val_idx,
3682 struct trace_event_file *file,
3685 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3688 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3691 static int create_var_field(struct hist_trigger_data *hist_data,
3692 unsigned int val_idx,
3693 struct trace_event_file *file,
3694 char *var_name, char *expr_str)
3696 struct trace_array *tr = hist_data->event_file->tr;
3697 unsigned long flags = 0;
3700 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3703 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3704 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
3708 flags |= HIST_FIELD_FL_VAR;
3709 hist_data->n_vars++;
3710 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3713 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3715 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
3716 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
3721 static int create_val_fields(struct hist_trigger_data *hist_data,
3722 struct trace_event_file *file)
3724 char *fields_str, *field_str;
3725 unsigned int i, j = 1;
3728 ret = create_hitcount_val(hist_data);
3732 fields_str = hist_data->attrs->vals_str;
3736 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3737 j < TRACING_MAP_VALS_MAX; i++) {
3738 field_str = strsep(&fields_str, ",");
3742 if (strcmp(field_str, "hitcount") == 0)
3745 ret = create_val_field(hist_data, j++, file, field_str);
3750 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3756 static int create_key_field(struct hist_trigger_data *hist_data,
3757 unsigned int key_idx,
3758 unsigned int key_offset,
3759 struct trace_event_file *file,
3762 struct trace_array *tr = hist_data->event_file->tr;
3763 struct hist_field *hist_field = NULL;
3764 unsigned long flags = 0;
3765 unsigned int key_size;
3768 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
3771 flags |= HIST_FIELD_FL_KEY;
3773 if (strcmp(field_str, "stacktrace") == 0) {
3774 flags |= HIST_FIELD_FL_STACKTRACE;
3775 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
3776 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
3778 hist_field = parse_expr(hist_data, file, field_str, flags,
3780 if (IS_ERR(hist_field)) {
3781 ret = PTR_ERR(hist_field);
3785 if (field_has_hist_vars(hist_field, 0)) {
3786 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
3787 destroy_hist_field(hist_field, 0);
3792 key_size = hist_field->size;
3795 hist_data->fields[key_idx] = hist_field;
3797 key_size = ALIGN(key_size, sizeof(u64));
3798 hist_data->fields[key_idx]->size = key_size;
3799 hist_data->fields[key_idx]->offset = key_offset;
3801 hist_data->key_size += key_size;
3803 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3808 hist_data->n_keys++;
3809 hist_data->n_fields++;
3811 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
3819 static int create_key_fields(struct hist_trigger_data *hist_data,
3820 struct trace_event_file *file)
3822 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
3823 char *fields_str, *field_str;
3826 fields_str = hist_data->attrs->keys_str;
3830 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
3831 field_str = strsep(&fields_str, ",");
3834 ret = create_key_field(hist_data, i, key_offset,
3849 static int create_var_fields(struct hist_trigger_data *hist_data,
3850 struct trace_event_file *file)
3852 unsigned int i, j = hist_data->n_vals;
3855 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
3857 for (i = 0; i < n_vars; i++) {
3858 char *var_name = hist_data->attrs->var_defs.name[i];
3859 char *expr = hist_data->attrs->var_defs.expr[i];
3861 ret = create_var_field(hist_data, j++, file, var_name, expr);
3869 static void free_var_defs(struct hist_trigger_data *hist_data)
3873 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3874 kfree(hist_data->attrs->var_defs.name[i]);
3875 kfree(hist_data->attrs->var_defs.expr[i]);
3878 hist_data->attrs->var_defs.n_vars = 0;
3881 static int parse_var_defs(struct hist_trigger_data *hist_data)
3883 struct trace_array *tr = hist_data->event_file->tr;
3884 char *s, *str, *var_name, *field_str;
3885 unsigned int i, j, n_vars = 0;
3888 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
3889 str = hist_data->attrs->assignment_str[i];
3890 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
3891 field_str = strsep(&str, ",");
3895 var_name = strsep(&field_str, "=");
3896 if (!var_name || !field_str) {
3897 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
3903 if (n_vars == TRACING_MAP_VARS_MAX) {
3904 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
3909 s = kstrdup(var_name, GFP_KERNEL);
3914 hist_data->attrs->var_defs.name[n_vars] = s;
3916 s = kstrdup(field_str, GFP_KERNEL);
3921 hist_data->attrs->var_defs.expr[n_vars++] = s;
3923 hist_data->attrs->var_defs.n_vars = n_vars;
3929 free_var_defs(hist_data);
3934 static int create_hist_fields(struct hist_trigger_data *hist_data,
3935 struct trace_event_file *file)
3939 ret = parse_var_defs(hist_data);
3943 ret = create_val_fields(hist_data, file);
3947 ret = create_var_fields(hist_data, file);
3951 ret = create_key_fields(hist_data, file);
3955 free_var_defs(hist_data);
3960 static int is_descending(struct trace_array *tr, const char *str)
3965 if (strcmp(str, "descending") == 0)
3968 if (strcmp(str, "ascending") == 0)
3971 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
3976 static int create_sort_keys(struct hist_trigger_data *hist_data)
3978 struct trace_array *tr = hist_data->event_file->tr;
3979 char *fields_str = hist_data->attrs->sort_key_str;
3980 struct tracing_map_sort_key *sort_key;
3981 int descending, ret = 0;
3982 unsigned int i, j, k;
3984 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
3989 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
3990 struct hist_field *hist_field;
3991 char *field_str, *field_name;
3992 const char *test_name;
3994 sort_key = &hist_data->sort_keys[i];
3996 field_str = strsep(&fields_str, ",");
4002 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4006 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4007 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4012 field_name = strsep(&field_str, ".");
4013 if (!field_name || !*field_name) {
4015 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4019 if (strcmp(field_name, "hitcount") == 0) {
4020 descending = is_descending(tr, field_str);
4021 if (descending < 0) {
4025 sort_key->descending = descending;
4029 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4032 hist_field = hist_data->fields[j];
4033 if (hist_field->flags & HIST_FIELD_FL_VAR)
4038 test_name = hist_field_name(hist_field, 0);
4040 if (strcmp(field_name, test_name) == 0) {
4041 sort_key->field_idx = idx;
4042 descending = is_descending(tr, field_str);
4043 if (descending < 0) {
4047 sort_key->descending = descending;
4051 if (j == hist_data->n_fields) {
4053 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4058 hist_data->n_sort_keys = i;
4063 static void destroy_actions(struct hist_trigger_data *hist_data)
4067 for (i = 0; i < hist_data->n_actions; i++) {
4068 struct action_data *data = hist_data->actions[i];
4070 if (data->handler == HANDLER_ONMATCH)
4071 onmatch_destroy(data);
4072 else if (data->handler == HANDLER_ONMAX ||
4073 data->handler == HANDLER_ONCHANGE)
4074 track_data_destroy(hist_data, data);
4080 static int parse_actions(struct hist_trigger_data *hist_data)
4082 struct trace_array *tr = hist_data->event_file->tr;
4083 struct action_data *data;
4089 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4090 str = hist_data->attrs->action_str[i];
4092 if ((len = str_has_prefix(str, "onmatch("))) {
4093 char *action_str = str + len;
4095 data = onmatch_parse(tr, action_str);
4097 ret = PTR_ERR(data);
4100 } else if ((len = str_has_prefix(str, "onmax("))) {
4101 char *action_str = str + len;
4103 data = track_data_parse(hist_data, action_str,
4106 ret = PTR_ERR(data);
4109 } else if ((len = str_has_prefix(str, "onchange("))) {
4110 char *action_str = str + len;
4112 data = track_data_parse(hist_data, action_str,
4115 ret = PTR_ERR(data);
4123 hist_data->actions[hist_data->n_actions++] = data;
4129 static int create_actions(struct hist_trigger_data *hist_data)
4131 struct action_data *data;
4135 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4136 data = hist_data->actions[i];
4138 if (data->handler == HANDLER_ONMATCH) {
4139 ret = onmatch_create(hist_data, data);
4142 } else if (data->handler == HANDLER_ONMAX ||
4143 data->handler == HANDLER_ONCHANGE) {
4144 ret = track_data_create(hist_data, data);
4156 static void print_actions(struct seq_file *m,
4157 struct hist_trigger_data *hist_data,
4158 struct tracing_map_elt *elt)
4162 for (i = 0; i < hist_data->n_actions; i++) {
4163 struct action_data *data = hist_data->actions[i];
4165 if (data->action == ACTION_SNAPSHOT)
4168 if (data->handler == HANDLER_ONMAX ||
4169 data->handler == HANDLER_ONCHANGE)
4170 track_data_print(m, hist_data, elt, data);
4174 static void print_action_spec(struct seq_file *m,
4175 struct hist_trigger_data *hist_data,
4176 struct action_data *data)
4180 if (data->action == ACTION_SAVE) {
4181 for (i = 0; i < hist_data->n_save_vars; i++) {
4182 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4183 if (i < hist_data->n_save_vars - 1)
4186 } else if (data->action == ACTION_TRACE) {
4187 if (data->use_trace_keyword)
4188 seq_printf(m, "%s", data->synth_event_name);
4189 for (i = 0; i < data->n_params; i++) {
4190 if (i || data->use_trace_keyword)
4192 seq_printf(m, "%s", data->params[i]);
4197 static void print_track_data_spec(struct seq_file *m,
4198 struct hist_trigger_data *hist_data,
4199 struct action_data *data)
4201 if (data->handler == HANDLER_ONMAX)
4202 seq_puts(m, ":onmax(");
4203 else if (data->handler == HANDLER_ONCHANGE)
4204 seq_puts(m, ":onchange(");
4205 seq_printf(m, "%s", data->track_data.var_str);
4206 seq_printf(m, ").%s(", data->action_name);
4208 print_action_spec(m, hist_data, data);
4213 static void print_onmatch_spec(struct seq_file *m,
4214 struct hist_trigger_data *hist_data,
4215 struct action_data *data)
4217 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4218 data->match_data.event);
4220 seq_printf(m, "%s(", data->action_name);
4222 print_action_spec(m, hist_data, data);
4227 static bool actions_match(struct hist_trigger_data *hist_data,
4228 struct hist_trigger_data *hist_data_test)
4232 if (hist_data->n_actions != hist_data_test->n_actions)
4235 for (i = 0; i < hist_data->n_actions; i++) {
4236 struct action_data *data = hist_data->actions[i];
4237 struct action_data *data_test = hist_data_test->actions[i];
4238 char *action_name, *action_name_test;
4240 if (data->handler != data_test->handler)
4242 if (data->action != data_test->action)
4245 if (data->n_params != data_test->n_params)
4248 for (j = 0; j < data->n_params; j++) {
4249 if (strcmp(data->params[j], data_test->params[j]) != 0)
4253 if (data->use_trace_keyword)
4254 action_name = data->synth_event_name;
4256 action_name = data->action_name;
4258 if (data_test->use_trace_keyword)
4259 action_name_test = data_test->synth_event_name;
4261 action_name_test = data_test->action_name;
4263 if (strcmp(action_name, action_name_test) != 0)
4266 if (data->handler == HANDLER_ONMATCH) {
4267 if (strcmp(data->match_data.event_system,
4268 data_test->match_data.event_system) != 0)
4270 if (strcmp(data->match_data.event,
4271 data_test->match_data.event) != 0)
4273 } else if (data->handler == HANDLER_ONMAX ||
4274 data->handler == HANDLER_ONCHANGE) {
4275 if (strcmp(data->track_data.var_str,
4276 data_test->track_data.var_str) != 0)
4285 static void print_actions_spec(struct seq_file *m,
4286 struct hist_trigger_data *hist_data)
4290 for (i = 0; i < hist_data->n_actions; i++) {
4291 struct action_data *data = hist_data->actions[i];
4293 if (data->handler == HANDLER_ONMATCH)
4294 print_onmatch_spec(m, hist_data, data);
4295 else if (data->handler == HANDLER_ONMAX ||
4296 data->handler == HANDLER_ONCHANGE)
4297 print_track_data_spec(m, hist_data, data);
4301 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4305 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4306 kfree(hist_data->field_var_hists[i]->cmd);
4307 kfree(hist_data->field_var_hists[i]);
4311 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4316 destroy_hist_trigger_attrs(hist_data->attrs);
4317 destroy_hist_fields(hist_data);
4318 tracing_map_destroy(hist_data->map);
4320 destroy_actions(hist_data);
4321 destroy_field_vars(hist_data);
4322 destroy_field_var_hists(hist_data);
4327 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4329 struct tracing_map *map = hist_data->map;
4330 struct ftrace_event_field *field;
4331 struct hist_field *hist_field;
4334 for_each_hist_field(i, hist_data) {
4335 hist_field = hist_data->fields[i];
4336 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4337 tracing_map_cmp_fn_t cmp_fn;
4339 field = hist_field->field;
4341 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4342 cmp_fn = tracing_map_cmp_none;
4344 cmp_fn = tracing_map_cmp_num(hist_field->size,
4345 hist_field->is_signed);
4346 else if (is_string_field(field))
4347 cmp_fn = tracing_map_cmp_string;
4349 cmp_fn = tracing_map_cmp_num(field->size,
4351 idx = tracing_map_add_key_field(map,
4354 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4355 idx = tracing_map_add_sum_field(map);
4360 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4361 idx = tracing_map_add_var(map);
4364 hist_field->var.idx = idx;
4365 hist_field->var.hist_data = hist_data;
4372 static struct hist_trigger_data *
4373 create_hist_data(unsigned int map_bits,
4374 struct hist_trigger_attrs *attrs,
4375 struct trace_event_file *file,
4378 const struct tracing_map_ops *map_ops = NULL;
4379 struct hist_trigger_data *hist_data;
4382 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4384 return ERR_PTR(-ENOMEM);
4386 hist_data->attrs = attrs;
4387 hist_data->remove = remove;
4388 hist_data->event_file = file;
4390 ret = parse_actions(hist_data);
4394 ret = create_hist_fields(hist_data, file);
4398 ret = create_sort_keys(hist_data);
4402 map_ops = &hist_trigger_elt_data_ops;
4404 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4405 map_ops, hist_data);
4406 if (IS_ERR(hist_data->map)) {
4407 ret = PTR_ERR(hist_data->map);
4408 hist_data->map = NULL;
4412 ret = create_tracing_map_fields(hist_data);
4418 hist_data->attrs = NULL;
4420 destroy_hist_data(hist_data);
4422 hist_data = ERR_PTR(ret);
4427 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4428 struct tracing_map_elt *elt,
4429 struct trace_buffer *buffer, void *rec,
4430 struct ring_buffer_event *rbe,
4433 struct hist_elt_data *elt_data;
4434 struct hist_field *hist_field;
4435 unsigned int i, var_idx;
4438 elt_data = elt->private_data;
4439 elt_data->var_ref_vals = var_ref_vals;
4441 for_each_hist_val_field(i, hist_data) {
4442 hist_field = hist_data->fields[i];
4443 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec);
4444 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4445 var_idx = hist_field->var.idx;
4447 if (hist_field->flags & HIST_FIELD_FL_STRING) {
4448 unsigned int str_start, var_str_idx, idx;
4449 char *str, *val_str;
4451 str_start = hist_data->n_field_var_str +
4452 hist_data->n_save_var_str;
4453 var_str_idx = hist_field->var_str_idx;
4454 idx = str_start + var_str_idx;
4456 str = elt_data->field_var_str[idx];
4457 val_str = (char *)(uintptr_t)hist_val;
4458 strscpy(str, val_str, STR_VAR_LEN_MAX);
4460 hist_val = (u64)(uintptr_t)str;
4462 tracing_map_set_var(elt, var_idx, hist_val);
4465 tracing_map_update_sum(elt, i, hist_val);
4468 for_each_hist_key_field(i, hist_data) {
4469 hist_field = hist_data->fields[i];
4470 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4471 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec);
4472 var_idx = hist_field->var.idx;
4473 tracing_map_set_var(elt, var_idx, hist_val);
4477 update_field_vars(hist_data, elt, buffer, rbe, rec);
4480 static inline void add_to_key(char *compound_key, void *key,
4481 struct hist_field *key_field, void *rec)
4483 size_t size = key_field->size;
4485 if (key_field->flags & HIST_FIELD_FL_STRING) {
4486 struct ftrace_event_field *field;
4488 field = key_field->field;
4489 if (field->filter_type == FILTER_DYN_STRING)
4490 size = *(u32 *)(rec + field->offset) >> 16;
4491 else if (field->filter_type == FILTER_PTR_STRING)
4493 else if (field->filter_type == FILTER_STATIC_STRING)
4496 /* ensure NULL-termination */
4497 if (size > key_field->size - 1)
4498 size = key_field->size - 1;
4500 strncpy(compound_key + key_field->offset, (char *)key, size);
4502 memcpy(compound_key + key_field->offset, key, size);
4506 hist_trigger_actions(struct hist_trigger_data *hist_data,
4507 struct tracing_map_elt *elt,
4508 struct trace_buffer *buffer, void *rec,
4509 struct ring_buffer_event *rbe, void *key,
4512 struct action_data *data;
4515 for (i = 0; i < hist_data->n_actions; i++) {
4516 data = hist_data->actions[i];
4517 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
4521 static void event_hist_trigger(struct event_trigger_data *data,
4522 struct trace_buffer *buffer, void *rec,
4523 struct ring_buffer_event *rbe)
4525 struct hist_trigger_data *hist_data = data->private_data;
4526 bool use_compound_key = (hist_data->n_keys > 1);
4527 unsigned long entries[HIST_STACKTRACE_DEPTH];
4528 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4529 char compound_key[HIST_KEY_SIZE_MAX];
4530 struct tracing_map_elt *elt = NULL;
4531 struct hist_field *key_field;
4536 memset(compound_key, 0, hist_data->key_size);
4538 for_each_hist_key_field(i, hist_data) {
4539 key_field = hist_data->fields[i];
4541 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4542 memset(entries, 0, HIST_STACKTRACE_SIZE);
4543 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
4544 HIST_STACKTRACE_SKIP);
4547 field_contents = key_field->fn(key_field, elt, buffer, rbe, rec);
4548 if (key_field->flags & HIST_FIELD_FL_STRING) {
4549 key = (void *)(unsigned long)field_contents;
4550 use_compound_key = true;
4552 key = (void *)&field_contents;
4555 if (use_compound_key)
4556 add_to_key(compound_key, key, key_field, rec);
4559 if (use_compound_key)
4562 if (hist_data->n_var_refs &&
4563 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4566 elt = tracing_map_insert(hist_data->map, key);
4570 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
4572 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4573 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
4576 static void hist_trigger_stacktrace_print(struct seq_file *m,
4577 unsigned long *stacktrace_entries,
4578 unsigned int max_entries)
4580 char str[KSYM_SYMBOL_LEN];
4581 unsigned int spaces = 8;
4584 for (i = 0; i < max_entries; i++) {
4585 if (!stacktrace_entries[i])
4588 seq_printf(m, "%*c", 1 + spaces, ' ');
4589 sprint_symbol(str, stacktrace_entries[i]);
4590 seq_printf(m, "%s\n", str);
4594 static void hist_trigger_print_key(struct seq_file *m,
4595 struct hist_trigger_data *hist_data,
4597 struct tracing_map_elt *elt)
4599 struct hist_field *key_field;
4600 char str[KSYM_SYMBOL_LEN];
4601 bool multiline = false;
4602 const char *field_name;
4608 for_each_hist_key_field(i, hist_data) {
4609 key_field = hist_data->fields[i];
4611 if (i > hist_data->n_vals)
4614 field_name = hist_field_name(key_field, 0);
4616 if (key_field->flags & HIST_FIELD_FL_HEX) {
4617 uval = *(u64 *)(key + key_field->offset);
4618 seq_printf(m, "%s: %llx", field_name, uval);
4619 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4620 uval = *(u64 *)(key + key_field->offset);
4621 sprint_symbol_no_offset(str, uval);
4622 seq_printf(m, "%s: [%llx] %-45s", field_name,
4624 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4625 uval = *(u64 *)(key + key_field->offset);
4626 sprint_symbol(str, uval);
4627 seq_printf(m, "%s: [%llx] %-55s", field_name,
4629 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4630 struct hist_elt_data *elt_data = elt->private_data;
4633 if (WARN_ON_ONCE(!elt_data))
4636 comm = elt_data->comm;
4638 uval = *(u64 *)(key + key_field->offset);
4639 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4641 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4642 const char *syscall_name;
4644 uval = *(u64 *)(key + key_field->offset);
4645 syscall_name = get_syscall_name(uval);
4647 syscall_name = "unknown_syscall";
4649 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4650 syscall_name, uval);
4651 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4652 seq_puts(m, "stacktrace:\n");
4653 hist_trigger_stacktrace_print(m,
4654 key + key_field->offset,
4655 HIST_STACKTRACE_DEPTH);
4657 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4658 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4659 *(u64 *)(key + key_field->offset));
4660 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4661 seq_printf(m, "%s: %-50s", field_name,
4662 (char *)(key + key_field->offset));
4664 uval = *(u64 *)(key + key_field->offset);
4665 seq_printf(m, "%s: %10llu", field_name, uval);
4675 static void hist_trigger_entry_print(struct seq_file *m,
4676 struct hist_trigger_data *hist_data,
4678 struct tracing_map_elt *elt)
4680 const char *field_name;
4683 hist_trigger_print_key(m, hist_data, key, elt);
4685 seq_printf(m, " hitcount: %10llu",
4686 tracing_map_read_sum(elt, HITCOUNT_IDX));
4688 for (i = 1; i < hist_data->n_vals; i++) {
4689 field_name = hist_field_name(hist_data->fields[i], 0);
4691 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4692 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4695 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4696 seq_printf(m, " %s: %10llx", field_name,
4697 tracing_map_read_sum(elt, i));
4699 seq_printf(m, " %s: %10llu", field_name,
4700 tracing_map_read_sum(elt, i));
4704 print_actions(m, hist_data, elt);
4709 static int print_entries(struct seq_file *m,
4710 struct hist_trigger_data *hist_data)
4712 struct tracing_map_sort_entry **sort_entries = NULL;
4713 struct tracing_map *map = hist_data->map;
4716 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4717 hist_data->n_sort_keys,
4722 for (i = 0; i < n_entries; i++)
4723 hist_trigger_entry_print(m, hist_data,
4724 sort_entries[i]->key,
4725 sort_entries[i]->elt);
4727 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4732 static void hist_trigger_show(struct seq_file *m,
4733 struct event_trigger_data *data, int n)
4735 struct hist_trigger_data *hist_data;
4739 seq_puts(m, "\n\n");
4741 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4742 data->ops->print(m, data->ops, data);
4743 seq_puts(m, "#\n\n");
4745 hist_data = data->private_data;
4746 n_entries = print_entries(m, hist_data);
4750 track_data_snapshot_print(m, hist_data);
4752 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4753 (u64)atomic64_read(&hist_data->map->hits),
4754 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4757 static int hist_show(struct seq_file *m, void *v)
4759 struct event_trigger_data *data;
4760 struct trace_event_file *event_file;
4763 mutex_lock(&event_mutex);
4765 event_file = event_file_data(m->private);
4766 if (unlikely(!event_file)) {
4771 list_for_each_entry(data, &event_file->triggers, list) {
4772 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4773 hist_trigger_show(m, data, n++);
4777 mutex_unlock(&event_mutex);
4782 static int event_hist_open(struct inode *inode, struct file *file)
4786 ret = security_locked_down(LOCKDOWN_TRACEFS);
4790 return single_open(file, hist_show, file);
4793 const struct file_operations event_hist_fops = {
4794 .open = event_hist_open,
4796 .llseek = seq_lseek,
4797 .release = single_release,
4800 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
4801 static void hist_field_debug_show_flags(struct seq_file *m,
4802 unsigned long flags)
4804 seq_puts(m, " flags:\n");
4806 if (flags & HIST_FIELD_FL_KEY)
4807 seq_puts(m, " HIST_FIELD_FL_KEY\n");
4808 else if (flags & HIST_FIELD_FL_HITCOUNT)
4809 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
4810 else if (flags & HIST_FIELD_FL_VAR)
4811 seq_puts(m, " HIST_FIELD_FL_VAR\n");
4812 else if (flags & HIST_FIELD_FL_VAR_REF)
4813 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
4815 seq_puts(m, " VAL: normal u64 value\n");
4817 if (flags & HIST_FIELD_FL_ALIAS)
4818 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
4821 static int hist_field_debug_show(struct seq_file *m,
4822 struct hist_field *field, unsigned long flags)
4824 if ((field->flags & flags) != flags) {
4825 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
4829 hist_field_debug_show_flags(m, field->flags);
4831 seq_printf(m, " ftrace_event_field name: %s\n",
4832 field->field->name);
4834 if (field->flags & HIST_FIELD_FL_VAR) {
4835 seq_printf(m, " var.name: %s\n", field->var.name);
4836 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4840 if (field->flags & HIST_FIELD_FL_ALIAS)
4841 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4842 field->var_ref_idx);
4844 if (field->flags & HIST_FIELD_FL_VAR_REF) {
4845 seq_printf(m, " name: %s\n", field->name);
4846 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4848 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
4849 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4850 field->var_ref_idx);
4852 seq_printf(m, " system: %s\n", field->system);
4853 if (field->event_name)
4854 seq_printf(m, " event_name: %s\n", field->event_name);
4857 seq_printf(m, " type: %s\n", field->type);
4858 seq_printf(m, " size: %u\n", field->size);
4859 seq_printf(m, " is_signed: %u\n", field->is_signed);
4864 static int field_var_debug_show(struct seq_file *m,
4865 struct field_var *field_var, unsigned int i,
4868 const char *vars_name = save_vars ? "save_vars" : "field_vars";
4869 struct hist_field *field;
4872 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
4874 field = field_var->var;
4876 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
4878 hist_field_debug_show_flags(m, field->flags);
4879 seq_printf(m, " var.name: %s\n", field->var.name);
4880 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4883 field = field_var->val;
4885 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
4887 seq_printf(m, " ftrace_event_field name: %s\n",
4888 field->field->name);
4894 seq_printf(m, " type: %s\n", field->type);
4895 seq_printf(m, " size: %u\n", field->size);
4896 seq_printf(m, " is_signed: %u\n", field->is_signed);
4901 static int hist_action_debug_show(struct seq_file *m,
4902 struct action_data *data, int i)
4906 if (data->handler == HANDLER_ONMAX ||
4907 data->handler == HANDLER_ONCHANGE) {
4908 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
4909 ret = hist_field_debug_show(m, data->track_data.var_ref,
4910 HIST_FIELD_FL_VAR_REF);
4914 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
4915 ret = hist_field_debug_show(m, data->track_data.track_var,
4921 if (data->handler == HANDLER_ONMATCH) {
4922 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
4923 i, data->match_data.event_system);
4924 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
4925 i, data->match_data.event);
4931 static int hist_actions_debug_show(struct seq_file *m,
4932 struct hist_trigger_data *hist_data)
4936 if (hist_data->n_actions)
4937 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
4939 for (i = 0; i < hist_data->n_actions; i++) {
4940 struct action_data *action = hist_data->actions[i];
4942 ret = hist_action_debug_show(m, action, i);
4947 if (hist_data->n_save_vars)
4948 seq_puts(m, "\n save action variables (save() params):\n");
4950 for (i = 0; i < hist_data->n_save_vars; i++) {
4951 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
4959 static void hist_trigger_debug_show(struct seq_file *m,
4960 struct event_trigger_data *data, int n)
4962 struct hist_trigger_data *hist_data;
4966 seq_puts(m, "\n\n");
4968 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4969 data->ops->print(m, data->ops, data);
4970 seq_puts(m, "#\n\n");
4972 hist_data = data->private_data;
4974 seq_printf(m, "hist_data: %p\n\n", hist_data);
4975 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
4976 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
4977 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
4979 seq_puts(m, "\n val fields:\n\n");
4981 seq_puts(m, " hist_data->fields[0]:\n");
4982 ret = hist_field_debug_show(m, hist_data->fields[0],
4983 HIST_FIELD_FL_HITCOUNT);
4987 for (i = 1; i < hist_data->n_vals; i++) {
4988 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
4989 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
4994 seq_puts(m, "\n key fields:\n");
4996 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
4997 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
4998 ret = hist_field_debug_show(m, hist_data->fields[i],
5004 if (hist_data->n_var_refs)
5005 seq_puts(m, "\n variable reference fields:\n");
5007 for (i = 0; i < hist_data->n_var_refs; i++) {
5008 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5009 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5010 HIST_FIELD_FL_VAR_REF);
5015 if (hist_data->n_field_vars)
5016 seq_puts(m, "\n field variables:\n");
5018 for (i = 0; i < hist_data->n_field_vars; i++) {
5019 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5024 ret = hist_actions_debug_show(m, hist_data);
5029 static int hist_debug_show(struct seq_file *m, void *v)
5031 struct event_trigger_data *data;
5032 struct trace_event_file *event_file;
5035 mutex_lock(&event_mutex);
5037 event_file = event_file_data(m->private);
5038 if (unlikely(!event_file)) {
5043 list_for_each_entry(data, &event_file->triggers, list) {
5044 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5045 hist_trigger_debug_show(m, data, n++);
5049 mutex_unlock(&event_mutex);
5054 static int event_hist_debug_open(struct inode *inode, struct file *file)
5058 ret = security_locked_down(LOCKDOWN_TRACEFS);
5062 return single_open(file, hist_debug_show, file);
5065 const struct file_operations event_hist_debug_fops = {
5066 .open = event_hist_debug_open,
5068 .llseek = seq_lseek,
5069 .release = single_release,
5073 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5075 const char *field_name = hist_field_name(hist_field, 0);
5077 if (hist_field->var.name)
5078 seq_printf(m, "%s=", hist_field->var.name);
5080 if (hist_field->flags & HIST_FIELD_FL_CPU)
5082 else if (field_name) {
5083 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5084 hist_field->flags & HIST_FIELD_FL_ALIAS)
5086 seq_printf(m, "%s", field_name);
5087 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5088 seq_puts(m, "common_timestamp");
5090 if (hist_field->flags) {
5091 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5092 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5093 const char *flags = get_hist_field_flags(hist_field);
5096 seq_printf(m, ".%s", flags);
5101 static int event_hist_trigger_print(struct seq_file *m,
5102 struct event_trigger_ops *ops,
5103 struct event_trigger_data *data)
5105 struct hist_trigger_data *hist_data = data->private_data;
5106 struct hist_field *field;
5107 bool have_var = false;
5110 seq_puts(m, "hist:");
5113 seq_printf(m, "%s:", data->name);
5115 seq_puts(m, "keys=");
5117 for_each_hist_key_field(i, hist_data) {
5118 field = hist_data->fields[i];
5120 if (i > hist_data->n_vals)
5123 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5124 seq_puts(m, "stacktrace");
5126 hist_field_print(m, field);
5129 seq_puts(m, ":vals=");
5131 for_each_hist_val_field(i, hist_data) {
5132 field = hist_data->fields[i];
5133 if (field->flags & HIST_FIELD_FL_VAR) {
5138 if (i == HITCOUNT_IDX)
5139 seq_puts(m, "hitcount");
5142 hist_field_print(m, field);
5151 for_each_hist_val_field(i, hist_data) {
5152 field = hist_data->fields[i];
5154 if (field->flags & HIST_FIELD_FL_VAR) {
5157 hist_field_print(m, field);
5162 seq_puts(m, ":sort=");
5164 for (i = 0; i < hist_data->n_sort_keys; i++) {
5165 struct tracing_map_sort_key *sort_key;
5166 unsigned int idx, first_key_idx;
5169 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5171 sort_key = &hist_data->sort_keys[i];
5172 idx = sort_key->field_idx;
5174 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5180 if (idx == HITCOUNT_IDX)
5181 seq_puts(m, "hitcount");
5183 if (idx >= first_key_idx)
5184 idx += hist_data->n_vars;
5185 hist_field_print(m, hist_data->fields[idx]);
5188 if (sort_key->descending)
5189 seq_puts(m, ".descending");
5191 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5192 if (hist_data->enable_timestamps)
5193 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5195 print_actions_spec(m, hist_data);
5197 if (data->filter_str)
5198 seq_printf(m, " if %s", data->filter_str);
5201 seq_puts(m, " [paused]");
5203 seq_puts(m, " [active]");
5210 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5211 struct event_trigger_data *data)
5213 struct hist_trigger_data *hist_data = data->private_data;
5215 if (!data->ref && hist_data->attrs->name)
5216 save_named_trigger(hist_data->attrs->name, data);
5223 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5225 struct trace_event_file *file;
5230 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5231 file = hist_data->field_var_hists[i]->hist_data->event_file;
5232 cmd = hist_data->field_var_hists[i]->cmd;
5233 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5234 "!hist", "hist", cmd);
5238 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5239 struct event_trigger_data *data)
5241 struct hist_trigger_data *hist_data = data->private_data;
5243 if (WARN_ON_ONCE(data->ref <= 0))
5249 del_named_trigger(data);
5251 trigger_data_free(data);
5253 remove_hist_vars(hist_data);
5255 unregister_field_var_hists(hist_data);
5257 destroy_hist_data(hist_data);
5261 static struct event_trigger_ops event_hist_trigger_ops = {
5262 .func = event_hist_trigger,
5263 .print = event_hist_trigger_print,
5264 .init = event_hist_trigger_init,
5265 .free = event_hist_trigger_free,
5268 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5269 struct event_trigger_data *data)
5273 save_named_trigger(data->named_data->name, data);
5275 event_hist_trigger_init(ops, data->named_data);
5280 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5281 struct event_trigger_data *data)
5283 if (WARN_ON_ONCE(data->ref <= 0))
5286 event_hist_trigger_free(ops, data->named_data);
5290 del_named_trigger(data);
5291 trigger_data_free(data);
5295 static struct event_trigger_ops event_hist_trigger_named_ops = {
5296 .func = event_hist_trigger,
5297 .print = event_hist_trigger_print,
5298 .init = event_hist_trigger_named_init,
5299 .free = event_hist_trigger_named_free,
5302 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5305 return &event_hist_trigger_ops;
5308 static void hist_clear(struct event_trigger_data *data)
5310 struct hist_trigger_data *hist_data = data->private_data;
5313 pause_named_trigger(data);
5315 tracepoint_synchronize_unregister();
5317 tracing_map_clear(hist_data->map);
5320 unpause_named_trigger(data);
5323 static bool compatible_field(struct ftrace_event_field *field,
5324 struct ftrace_event_field *test_field)
5326 if (field == test_field)
5328 if (field == NULL || test_field == NULL)
5330 if (strcmp(field->name, test_field->name) != 0)
5332 if (strcmp(field->type, test_field->type) != 0)
5334 if (field->size != test_field->size)
5336 if (field->is_signed != test_field->is_signed)
5342 static bool hist_trigger_match(struct event_trigger_data *data,
5343 struct event_trigger_data *data_test,
5344 struct event_trigger_data *named_data,
5347 struct tracing_map_sort_key *sort_key, *sort_key_test;
5348 struct hist_trigger_data *hist_data, *hist_data_test;
5349 struct hist_field *key_field, *key_field_test;
5352 if (named_data && (named_data != data_test) &&
5353 (named_data != data_test->named_data))
5356 if (!named_data && is_named_trigger(data_test))
5359 hist_data = data->private_data;
5360 hist_data_test = data_test->private_data;
5362 if (hist_data->n_vals != hist_data_test->n_vals ||
5363 hist_data->n_fields != hist_data_test->n_fields ||
5364 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5367 if (!ignore_filter) {
5368 if ((data->filter_str && !data_test->filter_str) ||
5369 (!data->filter_str && data_test->filter_str))
5373 for_each_hist_field(i, hist_data) {
5374 key_field = hist_data->fields[i];
5375 key_field_test = hist_data_test->fields[i];
5377 if (key_field->flags != key_field_test->flags)
5379 if (!compatible_field(key_field->field, key_field_test->field))
5381 if (key_field->offset != key_field_test->offset)
5383 if (key_field->size != key_field_test->size)
5385 if (key_field->is_signed != key_field_test->is_signed)
5387 if (!!key_field->var.name != !!key_field_test->var.name)
5389 if (key_field->var.name &&
5390 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5394 for (i = 0; i < hist_data->n_sort_keys; i++) {
5395 sort_key = &hist_data->sort_keys[i];
5396 sort_key_test = &hist_data_test->sort_keys[i];
5398 if (sort_key->field_idx != sort_key_test->field_idx ||
5399 sort_key->descending != sort_key_test->descending)
5403 if (!ignore_filter && data->filter_str &&
5404 (strcmp(data->filter_str, data_test->filter_str) != 0))
5407 if (!actions_match(hist_data, hist_data_test))
5413 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5414 struct event_trigger_data *data,
5415 struct trace_event_file *file)
5417 struct hist_trigger_data *hist_data = data->private_data;
5418 struct event_trigger_data *test, *named_data = NULL;
5419 struct trace_array *tr = file->tr;
5422 if (hist_data->attrs->name) {
5423 named_data = find_named_trigger(hist_data->attrs->name);
5425 if (!hist_trigger_match(data, named_data, named_data,
5427 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5434 if (hist_data->attrs->name && !named_data)
5437 lockdep_assert_held(&event_mutex);
5439 list_for_each_entry(test, &file->triggers, list) {
5440 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5441 if (!hist_trigger_match(data, test, named_data, false))
5443 if (hist_data->attrs->pause)
5444 test->paused = true;
5445 else if (hist_data->attrs->cont)
5446 test->paused = false;
5447 else if (hist_data->attrs->clear)
5450 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5457 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5458 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5463 if (hist_data->attrs->pause)
5464 data->paused = true;
5467 data->private_data = named_data->private_data;
5468 set_named_trigger_data(data, named_data);
5469 data->ops = &event_hist_trigger_named_ops;
5472 if (data->ops->init) {
5473 ret = data->ops->init(data->ops, data);
5478 if (hist_data->enable_timestamps) {
5479 char *clock = hist_data->attrs->clock;
5481 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5483 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5487 tracing_set_filter_buffering(file->tr, true);
5491 destroy_hist_data(hist_data);
5498 static int hist_trigger_enable(struct event_trigger_data *data,
5499 struct trace_event_file *file)
5503 list_add_tail_rcu(&data->list, &file->triggers);
5505 update_cond_flag(file);
5507 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5508 list_del_rcu(&data->list);
5509 update_cond_flag(file);
5516 static bool have_hist_trigger_match(struct event_trigger_data *data,
5517 struct trace_event_file *file)
5519 struct hist_trigger_data *hist_data = data->private_data;
5520 struct event_trigger_data *test, *named_data = NULL;
5523 lockdep_assert_held(&event_mutex);
5525 if (hist_data->attrs->name)
5526 named_data = find_named_trigger(hist_data->attrs->name);
5528 list_for_each_entry(test, &file->triggers, list) {
5529 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5530 if (hist_trigger_match(data, test, named_data, false)) {
5540 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5541 struct trace_event_file *file)
5543 struct hist_trigger_data *hist_data = data->private_data;
5544 struct event_trigger_data *test, *named_data = NULL;
5546 lockdep_assert_held(&event_mutex);
5548 if (hist_data->attrs->name)
5549 named_data = find_named_trigger(hist_data->attrs->name);
5551 list_for_each_entry(test, &file->triggers, list) {
5552 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5553 if (!hist_trigger_match(data, test, named_data, false))
5555 hist_data = test->private_data;
5556 if (check_var_refs(hist_data))
5565 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5566 struct event_trigger_data *data,
5567 struct trace_event_file *file)
5569 struct hist_trigger_data *hist_data = data->private_data;
5570 struct event_trigger_data *test, *named_data = NULL;
5571 bool unregistered = false;
5573 lockdep_assert_held(&event_mutex);
5575 if (hist_data->attrs->name)
5576 named_data = find_named_trigger(hist_data->attrs->name);
5578 list_for_each_entry(test, &file->triggers, list) {
5579 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5580 if (!hist_trigger_match(data, test, named_data, false))
5582 unregistered = true;
5583 list_del_rcu(&test->list);
5584 trace_event_trigger_enable_disable(file, 0);
5585 update_cond_flag(file);
5590 if (unregistered && test->ops->free)
5591 test->ops->free(test->ops, test);
5593 if (hist_data->enable_timestamps) {
5594 if (!hist_data->remove || unregistered)
5595 tracing_set_filter_buffering(file->tr, false);
5599 static bool hist_file_check_refs(struct trace_event_file *file)
5601 struct hist_trigger_data *hist_data;
5602 struct event_trigger_data *test;
5604 lockdep_assert_held(&event_mutex);
5606 list_for_each_entry(test, &file->triggers, list) {
5607 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5608 hist_data = test->private_data;
5609 if (check_var_refs(hist_data))
5617 static void hist_unreg_all(struct trace_event_file *file)
5619 struct event_trigger_data *test, *n;
5620 struct hist_trigger_data *hist_data;
5621 struct synth_event *se;
5622 const char *se_name;
5624 lockdep_assert_held(&event_mutex);
5626 if (hist_file_check_refs(file))
5629 list_for_each_entry_safe(test, n, &file->triggers, list) {
5630 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5631 hist_data = test->private_data;
5632 list_del_rcu(&test->list);
5633 trace_event_trigger_enable_disable(file, 0);
5635 se_name = trace_event_name(file->event_call);
5636 se = find_synth_event(se_name);
5640 update_cond_flag(file);
5641 if (hist_data->enable_timestamps)
5642 tracing_set_filter_buffering(file->tr, false);
5643 if (test->ops->free)
5644 test->ops->free(test->ops, test);
5649 static int event_hist_trigger_func(struct event_command *cmd_ops,
5650 struct trace_event_file *file,
5651 char *glob, char *cmd, char *param)
5653 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5654 struct event_trigger_data *trigger_data;
5655 struct hist_trigger_attrs *attrs;
5656 struct event_trigger_ops *trigger_ops;
5657 struct hist_trigger_data *hist_data;
5658 struct synth_event *se;
5659 const char *se_name;
5660 bool remove = false;
5664 lockdep_assert_held(&event_mutex);
5666 if (glob && strlen(glob)) {
5668 last_cmd_set(file, param);
5678 * separate the trigger from the filter (k:v [if filter])
5679 * allowing for whitespace in the trigger
5681 p = trigger = param;
5683 p = strstr(p, "if");
5688 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5692 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
5694 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
5705 param = strstrip(p);
5706 trigger = strstrip(trigger);
5709 attrs = parse_hist_trigger_attrs(file->tr, trigger);
5711 return PTR_ERR(attrs);
5713 if (attrs->map_bits)
5714 hist_trigger_bits = attrs->map_bits;
5716 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5717 if (IS_ERR(hist_data)) {
5718 destroy_hist_trigger_attrs(attrs);
5719 return PTR_ERR(hist_data);
5722 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5724 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5725 if (!trigger_data) {
5730 trigger_data->count = -1;
5731 trigger_data->ops = trigger_ops;
5732 trigger_data->cmd_ops = cmd_ops;
5734 INIT_LIST_HEAD(&trigger_data->list);
5735 RCU_INIT_POINTER(trigger_data->filter, NULL);
5737 trigger_data->private_data = hist_data;
5739 /* if param is non-empty, it's supposed to be a filter */
5740 if (param && cmd_ops->set_filter) {
5741 ret = cmd_ops->set_filter(param, trigger_data, file);
5747 if (!have_hist_trigger_match(trigger_data, file))
5750 if (hist_trigger_check_refs(trigger_data, file)) {
5755 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5756 se_name = trace_event_name(file->event_call);
5757 se = find_synth_event(se_name);
5764 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5766 * The above returns on success the # of triggers registered,
5767 * but if it didn't register any it returns zero. Consider no
5768 * triggers registered a failure too.
5771 if (!(attrs->pause || attrs->cont || attrs->clear))
5777 if (get_named_trigger_data(trigger_data))
5780 if (has_hist_vars(hist_data))
5781 save_hist_vars(hist_data);
5783 ret = create_actions(hist_data);
5787 ret = tracing_map_init(hist_data->map);
5791 ret = hist_trigger_enable(trigger_data, file);
5795 se_name = trace_event_name(file->event_call);
5796 se = find_synth_event(se_name);
5799 /* Just return zero, not the number of registered triggers */
5807 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5809 if (cmd_ops->set_filter)
5810 cmd_ops->set_filter(NULL, trigger_data, NULL);
5812 remove_hist_vars(hist_data);
5814 kfree(trigger_data);
5816 destroy_hist_data(hist_data);
5820 static struct event_command trigger_hist_cmd = {
5822 .trigger_type = ETT_EVENT_HIST,
5823 .flags = EVENT_CMD_FL_NEEDS_REC,
5824 .func = event_hist_trigger_func,
5825 .reg = hist_register_trigger,
5826 .unreg = hist_unregister_trigger,
5827 .unreg_all = hist_unreg_all,
5828 .get_trigger_ops = event_hist_get_trigger_ops,
5829 .set_filter = set_trigger_filter,
5832 __init int register_trigger_hist_cmd(void)
5836 ret = register_event_command(&trigger_hist_cmd);
5843 hist_enable_trigger(struct event_trigger_data *data,
5844 struct trace_buffer *buffer, void *rec,
5845 struct ring_buffer_event *event)
5847 struct enable_trigger_data *enable_data = data->private_data;
5848 struct event_trigger_data *test;
5850 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
5851 lockdep_is_held(&event_mutex)) {
5852 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5853 if (enable_data->enable)
5854 test->paused = false;
5856 test->paused = true;
5862 hist_enable_count_trigger(struct event_trigger_data *data,
5863 struct trace_buffer *buffer, void *rec,
5864 struct ring_buffer_event *event)
5869 if (data->count != -1)
5872 hist_enable_trigger(data, buffer, rec, event);
5875 static struct event_trigger_ops hist_enable_trigger_ops = {
5876 .func = hist_enable_trigger,
5877 .print = event_enable_trigger_print,
5878 .init = event_trigger_init,
5879 .free = event_enable_trigger_free,
5882 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5883 .func = hist_enable_count_trigger,
5884 .print = event_enable_trigger_print,
5885 .init = event_trigger_init,
5886 .free = event_enable_trigger_free,
5889 static struct event_trigger_ops hist_disable_trigger_ops = {
5890 .func = hist_enable_trigger,
5891 .print = event_enable_trigger_print,
5892 .init = event_trigger_init,
5893 .free = event_enable_trigger_free,
5896 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5897 .func = hist_enable_count_trigger,
5898 .print = event_enable_trigger_print,
5899 .init = event_trigger_init,
5900 .free = event_enable_trigger_free,
5903 static struct event_trigger_ops *
5904 hist_enable_get_trigger_ops(char *cmd, char *param)
5906 struct event_trigger_ops *ops;
5909 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5912 ops = param ? &hist_enable_count_trigger_ops :
5913 &hist_enable_trigger_ops;
5915 ops = param ? &hist_disable_count_trigger_ops :
5916 &hist_disable_trigger_ops;
5921 static void hist_enable_unreg_all(struct trace_event_file *file)
5923 struct event_trigger_data *test, *n;
5925 list_for_each_entry_safe(test, n, &file->triggers, list) {
5926 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5927 list_del_rcu(&test->list);
5928 update_cond_flag(file);
5929 trace_event_trigger_enable_disable(file, 0);
5930 if (test->ops->free)
5931 test->ops->free(test->ops, test);
5936 static struct event_command trigger_hist_enable_cmd = {
5937 .name = ENABLE_HIST_STR,
5938 .trigger_type = ETT_HIST_ENABLE,
5939 .func = event_enable_trigger_func,
5940 .reg = event_enable_register_trigger,
5941 .unreg = event_enable_unregister_trigger,
5942 .unreg_all = hist_enable_unreg_all,
5943 .get_trigger_ops = hist_enable_get_trigger_ops,
5944 .set_filter = set_trigger_filter,
5947 static struct event_command trigger_hist_disable_cmd = {
5948 .name = DISABLE_HIST_STR,
5949 .trigger_type = ETT_HIST_ENABLE,
5950 .func = event_enable_trigger_func,
5951 .reg = event_enable_register_trigger,
5952 .unreg = event_enable_unregister_trigger,
5953 .unreg_all = hist_enable_unreg_all,
5954 .get_trigger_ops = hist_enable_get_trigger_ops,
5955 .set_filter = set_trigger_filter,
5958 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5960 unregister_event_command(&trigger_hist_enable_cmd);
5961 unregister_event_command(&trigger_hist_disable_cmd);
5964 __init int register_trigger_hist_enable_disable_cmds(void)
5968 ret = register_event_command(&trigger_hist_enable_cmd);
5969 if (WARN_ON(ret < 0))
5971 ret = register_event_command(&trigger_hist_disable_cmd);
5972 if (WARN_ON(ret < 0))
5973 unregister_trigger_hist_enable_disable_cmds();