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"), \
69 C(INVALID_STR_OPERAND, "String type can not be an operand in expression"),
72 #define C(a, b) HIST_ERR_##a
79 static const char *err_text[] = { ERRORS };
83 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
84 struct tracing_map_elt *elt,
85 struct trace_buffer *buffer,
86 struct ring_buffer_event *rbe,
89 #define HIST_FIELD_OPERANDS_MAX 2
90 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
91 #define HIST_ACTIONS_MAX 8
101 * A hist_var (histogram variable) contains variable information for
102 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
103 * flag set. A hist_var has a variable name e.g. ts0, and is
104 * associated with a given histogram trigger, as specified by
105 * hist_data. The hist_var idx is the unique index assigned to the
106 * variable by the hist trigger's tracing_map. The idx is what is
107 * used to set a variable's value and, by a variable reference, to
112 struct hist_trigger_data *hist_data;
117 struct ftrace_event_field *field;
123 unsigned int is_signed;
125 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
126 struct hist_trigger_data *hist_data;
129 * Variable fields contain variable-specific info in var.
132 enum field_op_id operator;
137 * The name field is used for EXPR and VAR_REF fields. VAR
138 * fields contain the variable name in var.name.
143 * When a histogram trigger is hit, if it has any references
144 * to variables, the values of those variables are collected
145 * into a var_ref_vals array by resolve_var_refs(). The
146 * current value of each variable is read from the tracing_map
147 * using the hist field's hist_var.idx and entered into the
148 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
150 unsigned int var_ref_idx;
153 unsigned int var_str_idx;
156 static u64 hist_field_none(struct hist_field *field,
157 struct tracing_map_elt *elt,
158 struct trace_buffer *buffer,
159 struct ring_buffer_event *rbe,
165 static u64 hist_field_counter(struct hist_field *field,
166 struct tracing_map_elt *elt,
167 struct trace_buffer *buffer,
168 struct ring_buffer_event *rbe,
174 static u64 hist_field_string(struct hist_field *hist_field,
175 struct tracing_map_elt *elt,
176 struct trace_buffer *buffer,
177 struct ring_buffer_event *rbe,
180 char *addr = (char *)(event + hist_field->field->offset);
182 return (u64)(unsigned long)addr;
185 static u64 hist_field_dynstring(struct hist_field *hist_field,
186 struct tracing_map_elt *elt,
187 struct trace_buffer *buffer,
188 struct ring_buffer_event *rbe,
191 u32 str_item = *(u32 *)(event + hist_field->field->offset);
192 int str_loc = str_item & 0xffff;
193 char *addr = (char *)(event + str_loc);
195 return (u64)(unsigned long)addr;
198 static u64 hist_field_pstring(struct hist_field *hist_field,
199 struct tracing_map_elt *elt,
200 struct trace_buffer *buffer,
201 struct ring_buffer_event *rbe,
204 char **addr = (char **)(event + hist_field->field->offset);
206 return (u64)(unsigned long)*addr;
209 static u64 hist_field_log2(struct hist_field *hist_field,
210 struct tracing_map_elt *elt,
211 struct trace_buffer *buffer,
212 struct ring_buffer_event *rbe,
215 struct hist_field *operand = hist_field->operands[0];
217 u64 val = operand->fn(operand, elt, buffer, rbe, event);
219 return (u64) ilog2(roundup_pow_of_two(val));
222 static u64 hist_field_plus(struct hist_field *hist_field,
223 struct tracing_map_elt *elt,
224 struct trace_buffer *buffer,
225 struct ring_buffer_event *rbe,
228 struct hist_field *operand1 = hist_field->operands[0];
229 struct hist_field *operand2 = hist_field->operands[1];
231 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
232 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
237 static u64 hist_field_minus(struct hist_field *hist_field,
238 struct tracing_map_elt *elt,
239 struct trace_buffer *buffer,
240 struct ring_buffer_event *rbe,
243 struct hist_field *operand1 = hist_field->operands[0];
244 struct hist_field *operand2 = hist_field->operands[1];
246 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
247 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
252 static u64 hist_field_unary_minus(struct hist_field *hist_field,
253 struct tracing_map_elt *elt,
254 struct trace_buffer *buffer,
255 struct ring_buffer_event *rbe,
258 struct hist_field *operand = hist_field->operands[0];
260 s64 sval = (s64)operand->fn(operand, elt, buffer, rbe, event);
261 u64 val = (u64)-sval;
266 #define DEFINE_HIST_FIELD_FN(type) \
267 static u64 hist_field_##type(struct hist_field *hist_field, \
268 struct tracing_map_elt *elt, \
269 struct trace_buffer *buffer, \
270 struct ring_buffer_event *rbe, \
273 type *addr = (type *)(event + hist_field->field->offset); \
275 return (u64)(unsigned long)*addr; \
278 DEFINE_HIST_FIELD_FN(s64);
279 DEFINE_HIST_FIELD_FN(u64);
280 DEFINE_HIST_FIELD_FN(s32);
281 DEFINE_HIST_FIELD_FN(u32);
282 DEFINE_HIST_FIELD_FN(s16);
283 DEFINE_HIST_FIELD_FN(u16);
284 DEFINE_HIST_FIELD_FN(s8);
285 DEFINE_HIST_FIELD_FN(u8);
287 #define for_each_hist_field(i, hist_data) \
288 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
290 #define for_each_hist_val_field(i, hist_data) \
291 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
293 #define for_each_hist_key_field(i, hist_data) \
294 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
296 #define HIST_STACKTRACE_DEPTH 16
297 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
298 #define HIST_STACKTRACE_SKIP 5
300 #define HITCOUNT_IDX 0
301 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
303 enum hist_field_flags {
304 HIST_FIELD_FL_HITCOUNT = 1 << 0,
305 HIST_FIELD_FL_KEY = 1 << 1,
306 HIST_FIELD_FL_STRING = 1 << 2,
307 HIST_FIELD_FL_HEX = 1 << 3,
308 HIST_FIELD_FL_SYM = 1 << 4,
309 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
310 HIST_FIELD_FL_EXECNAME = 1 << 6,
311 HIST_FIELD_FL_SYSCALL = 1 << 7,
312 HIST_FIELD_FL_STACKTRACE = 1 << 8,
313 HIST_FIELD_FL_LOG2 = 1 << 9,
314 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
315 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
316 HIST_FIELD_FL_VAR = 1 << 12,
317 HIST_FIELD_FL_EXPR = 1 << 13,
318 HIST_FIELD_FL_VAR_REF = 1 << 14,
319 HIST_FIELD_FL_CPU = 1 << 15,
320 HIST_FIELD_FL_ALIAS = 1 << 16,
325 char *name[TRACING_MAP_VARS_MAX];
326 char *expr[TRACING_MAP_VARS_MAX];
329 struct hist_trigger_attrs {
339 unsigned int map_bits;
341 char *assignment_str[TRACING_MAP_VARS_MAX];
342 unsigned int n_assignments;
344 char *action_str[HIST_ACTIONS_MAX];
345 unsigned int n_actions;
347 struct var_defs var_defs;
351 struct hist_field *var;
352 struct hist_field *val;
355 struct field_var_hist {
356 struct hist_trigger_data *hist_data;
360 struct hist_trigger_data {
361 struct hist_field *fields[HIST_FIELDS_MAX];
364 unsigned int n_fields;
366 unsigned int n_var_str;
367 unsigned int key_size;
368 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
369 unsigned int n_sort_keys;
370 struct trace_event_file *event_file;
371 struct hist_trigger_attrs *attrs;
372 struct tracing_map *map;
373 bool enable_timestamps;
375 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
376 unsigned int n_var_refs;
378 struct action_data *actions[HIST_ACTIONS_MAX];
379 unsigned int n_actions;
381 struct field_var *field_vars[SYNTH_FIELDS_MAX];
382 unsigned int n_field_vars;
383 unsigned int n_field_var_str;
384 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
385 unsigned int n_field_var_hists;
387 struct field_var *save_vars[SYNTH_FIELDS_MAX];
388 unsigned int n_save_vars;
389 unsigned int n_save_var_str;
394 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
395 struct tracing_map_elt *elt,
396 struct trace_buffer *buffer, void *rec,
397 struct ring_buffer_event *rbe, void *key,
398 struct action_data *data, u64 *var_ref_vals);
400 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
415 enum handler_id handler;
416 enum action_id action;
420 unsigned int n_params;
421 char *params[SYNTH_FIELDS_MAX];
424 * When a histogram trigger is hit, the values of any
425 * references to variables, including variables being passed
426 * as parameters to synthetic events, are collected into a
427 * var_ref_vals array. This var_ref_idx array is an array of
428 * indices into the var_ref_vals array, one for each synthetic
429 * event param, and is passed to the synthetic event
432 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
433 struct synth_event *synth_event;
434 bool use_trace_keyword;
435 char *synth_event_name;
445 * var_str contains the $-unstripped variable
446 * name referenced by var_ref, and used when
447 * printing the action. Because var_ref
448 * creation is deferred to create_actions(),
449 * we need a per-action way to save it until
450 * then, thus var_str.
455 * var_ref refers to the variable being
456 * tracked e.g onmax($var).
458 struct hist_field *var_ref;
461 * track_var contains the 'invisible' tracking
462 * variable created to keep the current
465 struct hist_field *track_var;
467 check_track_val_fn_t check_val;
468 action_fn_t save_data;
477 unsigned int key_len;
479 struct tracing_map_elt elt;
481 struct action_data *action_data;
482 struct hist_trigger_data *hist_data;
485 struct hist_elt_data {
488 char *field_var_str[SYNTH_FIELDS_MAX];
491 struct snapshot_context {
492 struct tracing_map_elt *elt;
496 static void track_data_free(struct track_data *track_data)
498 struct hist_elt_data *elt_data;
503 kfree(track_data->key);
505 elt_data = track_data->elt.private_data;
507 kfree(elt_data->comm);
514 static struct track_data *track_data_alloc(unsigned int key_len,
515 struct action_data *action_data,
516 struct hist_trigger_data *hist_data)
518 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
519 struct hist_elt_data *elt_data;
522 return ERR_PTR(-ENOMEM);
524 data->key = kzalloc(key_len, GFP_KERNEL);
526 track_data_free(data);
527 return ERR_PTR(-ENOMEM);
530 data->key_len = key_len;
531 data->action_data = action_data;
532 data->hist_data = hist_data;
534 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
536 track_data_free(data);
537 return ERR_PTR(-ENOMEM);
540 data->elt.private_data = elt_data;
542 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
543 if (!elt_data->comm) {
544 track_data_free(data);
545 return ERR_PTR(-ENOMEM);
551 static char last_cmd[MAX_FILTER_STR_VAL];
552 static char last_cmd_loc[MAX_FILTER_STR_VAL];
554 static int errpos(char *str)
556 return err_pos(last_cmd, str);
559 static void last_cmd_set(struct trace_event_file *file, char *str)
561 const char *system = NULL, *name = NULL;
562 struct trace_event_call *call;
567 strcpy(last_cmd, "hist:");
568 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
571 call = file->event_call;
572 system = call->class->system;
574 name = trace_event_name(call);
581 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
584 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
586 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
590 static void hist_err_clear(void)
593 last_cmd_loc[0] = '\0';
596 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
597 unsigned int *var_ref_idx);
599 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
600 unsigned int *var_ref_idx)
602 struct tracepoint *tp = event->tp;
604 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
605 struct tracepoint_func *probe_func_ptr;
606 synth_probe_func_t probe_func;
609 if (!(cpu_online(raw_smp_processor_id())))
612 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
613 if (probe_func_ptr) {
615 probe_func = probe_func_ptr->func;
616 __data = probe_func_ptr->data;
617 probe_func(__data, var_ref_vals, var_ref_idx);
618 } while ((++probe_func_ptr)->func);
623 static void action_trace(struct hist_trigger_data *hist_data,
624 struct tracing_map_elt *elt,
625 struct trace_buffer *buffer, void *rec,
626 struct ring_buffer_event *rbe, void *key,
627 struct action_data *data, u64 *var_ref_vals)
629 struct synth_event *event = data->synth_event;
631 trace_synth(event, var_ref_vals, data->var_ref_idx);
634 struct hist_var_data {
635 struct list_head list;
636 struct hist_trigger_data *hist_data;
639 static u64 hist_field_timestamp(struct hist_field *hist_field,
640 struct tracing_map_elt *elt,
641 struct trace_buffer *buffer,
642 struct ring_buffer_event *rbe,
645 struct hist_trigger_data *hist_data = hist_field->hist_data;
646 struct trace_array *tr = hist_data->event_file->tr;
648 u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
650 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
656 static u64 hist_field_cpu(struct hist_field *hist_field,
657 struct tracing_map_elt *elt,
658 struct trace_buffer *buffer,
659 struct ring_buffer_event *rbe,
662 int cpu = smp_processor_id();
668 * check_field_for_var_ref - Check if a VAR_REF field references a variable
669 * @hist_field: The VAR_REF field to check
670 * @var_data: The hist trigger that owns the variable
671 * @var_idx: The trigger variable identifier
673 * Check the given VAR_REF field to see whether or not it references
674 * the given variable associated with the given trigger.
676 * Return: The VAR_REF field if it does reference the variable, NULL if not
678 static struct hist_field *
679 check_field_for_var_ref(struct hist_field *hist_field,
680 struct hist_trigger_data *var_data,
681 unsigned int var_idx)
683 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
685 if (hist_field && hist_field->var.idx == var_idx &&
686 hist_field->var.hist_data == var_data)
693 * find_var_ref - Check if a trigger has a reference to a trigger variable
694 * @hist_data: The hist trigger that might have a reference to the variable
695 * @var_data: The hist trigger that owns the variable
696 * @var_idx: The trigger variable identifier
698 * Check the list of var_refs[] on the first hist trigger to see
699 * whether any of them are references to the variable on the second
702 * Return: The VAR_REF field referencing the variable if so, NULL if not
704 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
705 struct hist_trigger_data *var_data,
706 unsigned int var_idx)
708 struct hist_field *hist_field;
711 for (i = 0; i < hist_data->n_var_refs; i++) {
712 hist_field = hist_data->var_refs[i];
713 if (check_field_for_var_ref(hist_field, var_data, var_idx))
721 * find_any_var_ref - Check if there is a reference to a given trigger variable
722 * @hist_data: The hist trigger
723 * @var_idx: The trigger variable identifier
725 * Check to see whether the given variable is currently referenced by
728 * The trigger the variable is defined on is explicitly excluded - the
729 * assumption being that a self-reference doesn't prevent a trigger
730 * from being removed.
732 * Return: The VAR_REF field referencing the variable if so, NULL if not
734 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
735 unsigned int var_idx)
737 struct trace_array *tr = hist_data->event_file->tr;
738 struct hist_field *found = NULL;
739 struct hist_var_data *var_data;
741 list_for_each_entry(var_data, &tr->hist_vars, list) {
742 if (var_data->hist_data == hist_data)
744 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
753 * check_var_refs - Check if there is a reference to any of trigger's variables
754 * @hist_data: The hist trigger
756 * A trigger can define one or more variables. If any one of them is
757 * currently referenced by any other trigger, this function will
760 * Typically used to determine whether or not a trigger can be removed
761 * - if there are any references to a trigger's variables, it cannot.
763 * Return: True if there is a reference to any of trigger's variables
765 static bool check_var_refs(struct hist_trigger_data *hist_data)
767 struct hist_field *field;
771 for_each_hist_field(i, hist_data) {
772 field = hist_data->fields[i];
773 if (field && field->flags & HIST_FIELD_FL_VAR) {
774 if (find_any_var_ref(hist_data, field->var.idx)) {
784 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
786 struct trace_array *tr = hist_data->event_file->tr;
787 struct hist_var_data *var_data, *found = NULL;
789 list_for_each_entry(var_data, &tr->hist_vars, list) {
790 if (var_data->hist_data == hist_data) {
799 static bool field_has_hist_vars(struct hist_field *hist_field,
810 if (hist_field->flags & HIST_FIELD_FL_VAR ||
811 hist_field->flags & HIST_FIELD_FL_VAR_REF)
814 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
815 struct hist_field *operand;
817 operand = hist_field->operands[i];
818 if (field_has_hist_vars(operand, level + 1))
825 static bool has_hist_vars(struct hist_trigger_data *hist_data)
827 struct hist_field *hist_field;
830 for_each_hist_field(i, hist_data) {
831 hist_field = hist_data->fields[i];
832 if (field_has_hist_vars(hist_field, 0))
839 static int save_hist_vars(struct hist_trigger_data *hist_data)
841 struct trace_array *tr = hist_data->event_file->tr;
842 struct hist_var_data *var_data;
844 var_data = find_hist_vars(hist_data);
848 if (tracing_check_open_get_tr(tr))
851 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
857 var_data->hist_data = hist_data;
858 list_add(&var_data->list, &tr->hist_vars);
863 static void remove_hist_vars(struct hist_trigger_data *hist_data)
865 struct trace_array *tr = hist_data->event_file->tr;
866 struct hist_var_data *var_data;
868 var_data = find_hist_vars(hist_data);
872 if (WARN_ON(check_var_refs(hist_data)))
875 list_del(&var_data->list);
882 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
883 const char *var_name)
885 struct hist_field *hist_field, *found = NULL;
888 for_each_hist_field(i, hist_data) {
889 hist_field = hist_data->fields[i];
890 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
891 strcmp(hist_field->var.name, var_name) == 0) {
900 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
901 struct trace_event_file *file,
902 const char *var_name)
904 struct hist_trigger_data *test_data;
905 struct event_trigger_data *test;
906 struct hist_field *hist_field;
908 lockdep_assert_held(&event_mutex);
910 hist_field = find_var_field(hist_data, var_name);
914 list_for_each_entry(test, &file->triggers, list) {
915 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
916 test_data = test->private_data;
917 hist_field = find_var_field(test_data, var_name);
926 static struct trace_event_file *find_var_file(struct trace_array *tr,
931 struct hist_trigger_data *var_hist_data;
932 struct hist_var_data *var_data;
933 struct trace_event_file *file, *found = NULL;
936 return find_event_file(tr, system, event_name);
938 list_for_each_entry(var_data, &tr->hist_vars, list) {
939 var_hist_data = var_data->hist_data;
940 file = var_hist_data->event_file;
944 if (find_var_field(var_hist_data, var_name)) {
946 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
957 static struct hist_field *find_file_var(struct trace_event_file *file,
958 const char *var_name)
960 struct hist_trigger_data *test_data;
961 struct event_trigger_data *test;
962 struct hist_field *hist_field;
964 lockdep_assert_held(&event_mutex);
966 list_for_each_entry(test, &file->triggers, list) {
967 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
968 test_data = test->private_data;
969 hist_field = find_var_field(test_data, var_name);
978 static struct hist_field *
979 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
981 struct trace_array *tr = hist_data->event_file->tr;
982 struct hist_field *hist_field, *found = NULL;
983 struct trace_event_file *file;
986 for (i = 0; i < hist_data->n_actions; i++) {
987 struct action_data *data = hist_data->actions[i];
989 if (data->handler == HANDLER_ONMATCH) {
990 char *system = data->match_data.event_system;
991 char *event_name = data->match_data.event;
993 file = find_var_file(tr, system, event_name, var_name);
996 hist_field = find_file_var(file, var_name);
999 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1001 return ERR_PTR(-EINVAL);
1011 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1016 struct trace_array *tr = hist_data->event_file->tr;
1017 struct hist_field *hist_field = NULL;
1018 struct trace_event_file *file;
1020 if (!system || !event_name) {
1021 hist_field = find_match_var(hist_data, var_name);
1022 if (IS_ERR(hist_field))
1028 file = find_var_file(tr, system, event_name, var_name);
1032 hist_field = find_file_var(file, var_name);
1037 static u64 hist_field_var_ref(struct hist_field *hist_field,
1038 struct tracing_map_elt *elt,
1039 struct trace_buffer *buffer,
1040 struct ring_buffer_event *rbe,
1043 struct hist_elt_data *elt_data;
1046 if (WARN_ON_ONCE(!elt))
1049 elt_data = elt->private_data;
1050 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1055 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1056 u64 *var_ref_vals, bool self)
1058 struct hist_trigger_data *var_data;
1059 struct tracing_map_elt *var_elt;
1060 struct hist_field *hist_field;
1061 unsigned int i, var_idx;
1062 bool resolved = true;
1065 for (i = 0; i < hist_data->n_var_refs; i++) {
1066 hist_field = hist_data->var_refs[i];
1067 var_idx = hist_field->var.idx;
1068 var_data = hist_field->var.hist_data;
1070 if (var_data == NULL) {
1075 if ((self && var_data != hist_data) ||
1076 (!self && var_data == hist_data))
1079 var_elt = tracing_map_lookup(var_data->map, key);
1085 if (!tracing_map_var_set(var_elt, var_idx)) {
1090 if (self || !hist_field->read_once)
1091 var_val = tracing_map_read_var(var_elt, var_idx);
1093 var_val = tracing_map_read_var_once(var_elt, var_idx);
1095 var_ref_vals[i] = var_val;
1101 static const char *hist_field_name(struct hist_field *field,
1104 const char *field_name = "";
1110 field_name = field->field->name;
1111 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1112 field->flags & HIST_FIELD_FL_ALIAS)
1113 field_name = hist_field_name(field->operands[0], ++level);
1114 else if (field->flags & HIST_FIELD_FL_CPU)
1115 field_name = "common_cpu";
1116 else if (field->flags & HIST_FIELD_FL_EXPR ||
1117 field->flags & HIST_FIELD_FL_VAR_REF) {
1118 if (field->system) {
1119 static char full_name[MAX_FILTER_STR_VAL];
1121 strcat(full_name, field->system);
1122 strcat(full_name, ".");
1123 strcat(full_name, field->event_name);
1124 strcat(full_name, ".");
1125 strcat(full_name, field->name);
1126 field_name = full_name;
1128 field_name = field->name;
1129 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1130 field_name = "common_timestamp";
1132 if (field_name == NULL)
1138 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1140 hist_field_fn_t fn = NULL;
1142 switch (field_size) {
1144 if (field_is_signed)
1145 fn = hist_field_s64;
1147 fn = hist_field_u64;
1150 if (field_is_signed)
1151 fn = hist_field_s32;
1153 fn = hist_field_u32;
1156 if (field_is_signed)
1157 fn = hist_field_s16;
1159 fn = hist_field_u16;
1162 if (field_is_signed)
1172 static int parse_map_size(char *str)
1174 unsigned long size, map_bits;
1177 ret = kstrtoul(str, 0, &size);
1181 map_bits = ilog2(roundup_pow_of_two(size));
1182 if (map_bits < TRACING_MAP_BITS_MIN ||
1183 map_bits > TRACING_MAP_BITS_MAX)
1191 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1198 for (i = 0; i < attrs->n_assignments; i++)
1199 kfree(attrs->assignment_str[i]);
1201 for (i = 0; i < attrs->n_actions; i++)
1202 kfree(attrs->action_str[i]);
1205 kfree(attrs->sort_key_str);
1206 kfree(attrs->keys_str);
1207 kfree(attrs->vals_str);
1208 kfree(attrs->clock);
1212 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1216 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1219 if ((str_has_prefix(str, "onmatch(")) ||
1220 (str_has_prefix(str, "onmax(")) ||
1221 (str_has_prefix(str, "onchange("))) {
1222 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1223 if (!attrs->action_str[attrs->n_actions]) {
1233 static int parse_assignment(struct trace_array *tr,
1234 char *str, struct hist_trigger_attrs *attrs)
1238 if ((len = str_has_prefix(str, "key=")) ||
1239 (len = str_has_prefix(str, "keys="))) {
1240 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1241 if (!attrs->keys_str) {
1245 } else if ((len = str_has_prefix(str, "val=")) ||
1246 (len = str_has_prefix(str, "vals=")) ||
1247 (len = str_has_prefix(str, "values="))) {
1248 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1249 if (!attrs->vals_str) {
1253 } else if ((len = str_has_prefix(str, "sort="))) {
1254 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1255 if (!attrs->sort_key_str) {
1259 } else if (str_has_prefix(str, "name=")) {
1260 attrs->name = kstrdup(str, GFP_KERNEL);
1265 } else if ((len = str_has_prefix(str, "clock="))) {
1268 str = strstrip(str);
1269 attrs->clock = kstrdup(str, GFP_KERNEL);
1270 if (!attrs->clock) {
1274 } else if ((len = str_has_prefix(str, "size="))) {
1275 int map_bits = parse_map_size(str + len);
1281 attrs->map_bits = map_bits;
1285 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1286 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1291 assignment = kstrdup(str, GFP_KERNEL);
1297 attrs->assignment_str[attrs->n_assignments++] = assignment;
1303 static struct hist_trigger_attrs *
1304 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1306 struct hist_trigger_attrs *attrs;
1309 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1311 return ERR_PTR(-ENOMEM);
1313 while (trigger_str) {
1314 char *str = strsep(&trigger_str, ":");
1317 rhs = strchr(str, '=');
1319 if (!strlen(++rhs)) {
1321 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1324 ret = parse_assignment(tr, str, attrs);
1327 } else if (strcmp(str, "pause") == 0)
1328 attrs->pause = true;
1329 else if ((strcmp(str, "cont") == 0) ||
1330 (strcmp(str, "continue") == 0))
1332 else if (strcmp(str, "clear") == 0)
1333 attrs->clear = true;
1335 ret = parse_action(str, attrs);
1341 if (!attrs->keys_str) {
1346 if (!attrs->clock) {
1347 attrs->clock = kstrdup("global", GFP_KERNEL);
1348 if (!attrs->clock) {
1356 destroy_hist_trigger_attrs(attrs);
1358 return ERR_PTR(ret);
1361 static inline void save_comm(char *comm, struct task_struct *task)
1364 strcpy(comm, "<idle>");
1368 if (WARN_ON_ONCE(task->pid < 0)) {
1369 strcpy(comm, "<XXX>");
1373 strncpy(comm, task->comm, TASK_COMM_LEN);
1376 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1380 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1381 kfree(elt_data->field_var_str[i]);
1383 kfree(elt_data->comm);
1387 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1389 struct hist_elt_data *elt_data = elt->private_data;
1391 hist_elt_data_free(elt_data);
1394 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1396 struct hist_trigger_data *hist_data = elt->map->private_data;
1397 unsigned int size = TASK_COMM_LEN;
1398 struct hist_elt_data *elt_data;
1399 struct hist_field *key_field;
1400 unsigned int i, n_str;
1402 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1406 for_each_hist_key_field(i, hist_data) {
1407 key_field = hist_data->fields[i];
1409 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
1410 elt_data->comm = kzalloc(size, GFP_KERNEL);
1411 if (!elt_data->comm) {
1419 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1420 hist_data->n_var_str;
1421 if (n_str > SYNTH_FIELDS_MAX) {
1422 hist_elt_data_free(elt_data);
1426 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1428 size = STR_VAR_LEN_MAX;
1430 for (i = 0; i < n_str; i++) {
1431 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1432 if (!elt_data->field_var_str[i]) {
1433 hist_elt_data_free(elt_data);
1438 elt->private_data = elt_data;
1443 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1445 struct hist_elt_data *elt_data = elt->private_data;
1448 save_comm(elt_data->comm, current);
1451 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1452 .elt_alloc = hist_trigger_elt_data_alloc,
1453 .elt_free = hist_trigger_elt_data_free,
1454 .elt_init = hist_trigger_elt_data_init,
1457 static const char *get_hist_field_flags(struct hist_field *hist_field)
1459 const char *flags_str = NULL;
1461 if (hist_field->flags & HIST_FIELD_FL_HEX)
1463 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1465 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1466 flags_str = "sym-offset";
1467 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1468 flags_str = "execname";
1469 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1470 flags_str = "syscall";
1471 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1473 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1474 flags_str = "usecs";
1479 static void expr_field_str(struct hist_field *field, char *expr)
1481 if (field->flags & HIST_FIELD_FL_VAR_REF)
1484 strcat(expr, hist_field_name(field, 0));
1486 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1487 const char *flags_str = get_hist_field_flags(field);
1491 strcat(expr, flags_str);
1496 static char *expr_str(struct hist_field *field, unsigned int level)
1503 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1507 if (!field->operands[0]) {
1508 expr_field_str(field, expr);
1512 if (field->operator == FIELD_OP_UNARY_MINUS) {
1516 subexpr = expr_str(field->operands[0], ++level);
1521 strcat(expr, subexpr);
1529 expr_field_str(field->operands[0], expr);
1531 switch (field->operator) {
1532 case FIELD_OP_MINUS:
1543 expr_field_str(field->operands[1], expr);
1548 static int contains_operator(char *str)
1550 enum field_op_id field_op = FIELD_OP_NONE;
1553 op = strpbrk(str, "+-");
1555 return FIELD_OP_NONE;
1560 * Unfortunately, the modifier ".sym-offset"
1561 * can confuse things.
1563 if (op - str >= 4 && !strncmp(op - 4, ".sym-offset", 11))
1564 return FIELD_OP_NONE;
1567 field_op = FIELD_OP_UNARY_MINUS;
1569 field_op = FIELD_OP_MINUS;
1572 field_op = FIELD_OP_PLUS;
1581 static void get_hist_field(struct hist_field *hist_field)
1586 static void __destroy_hist_field(struct hist_field *hist_field)
1588 if (--hist_field->ref > 1)
1591 kfree(hist_field->var.name);
1592 kfree(hist_field->name);
1593 kfree(hist_field->type);
1595 kfree(hist_field->system);
1596 kfree(hist_field->event_name);
1601 static void destroy_hist_field(struct hist_field *hist_field,
1612 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1613 return; /* var refs will be destroyed separately */
1615 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1616 destroy_hist_field(hist_field->operands[i], level + 1);
1618 __destroy_hist_field(hist_field);
1621 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1622 struct ftrace_event_field *field,
1623 unsigned long flags,
1626 struct hist_field *hist_field;
1628 if (field && is_function_field(field))
1631 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1635 hist_field->ref = 1;
1637 hist_field->hist_data = hist_data;
1639 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1640 goto out; /* caller will populate */
1642 if (flags & HIST_FIELD_FL_VAR_REF) {
1643 hist_field->fn = hist_field_var_ref;
1647 if (flags & HIST_FIELD_FL_HITCOUNT) {
1648 hist_field->fn = hist_field_counter;
1649 hist_field->size = sizeof(u64);
1650 hist_field->type = kstrdup("u64", GFP_KERNEL);
1651 if (!hist_field->type)
1656 if (flags & HIST_FIELD_FL_STACKTRACE) {
1657 hist_field->fn = hist_field_none;
1661 if (flags & HIST_FIELD_FL_LOG2) {
1662 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
1663 hist_field->fn = hist_field_log2;
1664 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1665 hist_field->size = hist_field->operands[0]->size;
1666 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
1667 if (!hist_field->type)
1672 if (flags & HIST_FIELD_FL_TIMESTAMP) {
1673 hist_field->fn = hist_field_timestamp;
1674 hist_field->size = sizeof(u64);
1675 hist_field->type = kstrdup("u64", GFP_KERNEL);
1676 if (!hist_field->type)
1681 if (flags & HIST_FIELD_FL_CPU) {
1682 hist_field->fn = hist_field_cpu;
1683 hist_field->size = sizeof(int);
1684 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
1685 if (!hist_field->type)
1690 if (WARN_ON_ONCE(!field))
1693 /* Pointers to strings are just pointers and dangerous to dereference */
1694 if (is_string_field(field) &&
1695 (field->filter_type != FILTER_PTR_STRING)) {
1696 flags |= HIST_FIELD_FL_STRING;
1698 hist_field->size = MAX_FILTER_STR_VAL;
1699 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1700 if (!hist_field->type)
1703 if (field->filter_type == FILTER_STATIC_STRING)
1704 hist_field->fn = hist_field_string;
1705 else if (field->filter_type == FILTER_DYN_STRING)
1706 hist_field->fn = hist_field_dynstring;
1708 hist_field->fn = hist_field_pstring;
1710 hist_field->size = field->size;
1711 hist_field->is_signed = field->is_signed;
1712 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1713 if (!hist_field->type)
1716 hist_field->fn = select_value_fn(field->size,
1718 if (!hist_field->fn) {
1719 destroy_hist_field(hist_field, 0);
1724 hist_field->field = field;
1725 hist_field->flags = flags;
1728 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
1729 if (!hist_field->var.name)
1735 destroy_hist_field(hist_field, 0);
1739 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
1743 for (i = 0; i < HIST_FIELDS_MAX; i++) {
1744 if (hist_data->fields[i]) {
1745 destroy_hist_field(hist_data->fields[i], 0);
1746 hist_data->fields[i] = NULL;
1750 for (i = 0; i < hist_data->n_var_refs; i++) {
1751 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
1752 __destroy_hist_field(hist_data->var_refs[i]);
1753 hist_data->var_refs[i] = NULL;
1757 static int init_var_ref(struct hist_field *ref_field,
1758 struct hist_field *var_field,
1759 char *system, char *event_name)
1763 ref_field->var.idx = var_field->var.idx;
1764 ref_field->var.hist_data = var_field->hist_data;
1765 ref_field->size = var_field->size;
1766 ref_field->is_signed = var_field->is_signed;
1767 ref_field->flags |= var_field->flags &
1768 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
1771 ref_field->system = kstrdup(system, GFP_KERNEL);
1772 if (!ref_field->system)
1777 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
1778 if (!ref_field->event_name) {
1784 if (var_field->var.name) {
1785 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
1786 if (!ref_field->name) {
1790 } else if (var_field->name) {
1791 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
1792 if (!ref_field->name) {
1798 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
1799 if (!ref_field->type) {
1806 kfree(ref_field->system);
1807 kfree(ref_field->event_name);
1808 kfree(ref_field->name);
1813 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
1814 struct hist_field *var_field)
1816 struct hist_field *ref_field;
1819 for (i = 0; i < hist_data->n_var_refs; i++) {
1820 ref_field = hist_data->var_refs[i];
1821 if (ref_field->var.idx == var_field->var.idx &&
1822 ref_field->var.hist_data == var_field->hist_data)
1830 * create_var_ref - Create a variable reference and attach it to trigger
1831 * @hist_data: The trigger that will be referencing the variable
1832 * @var_field: The VAR field to create a reference to
1833 * @system: The optional system string
1834 * @event_name: The optional event_name string
1836 * Given a variable hist_field, create a VAR_REF hist_field that
1837 * represents a reference to it.
1839 * This function also adds the reference to the trigger that
1840 * now references the variable.
1842 * Return: The VAR_REF field if successful, NULL if not
1844 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
1845 struct hist_field *var_field,
1846 char *system, char *event_name)
1848 unsigned long flags = HIST_FIELD_FL_VAR_REF;
1849 struct hist_field *ref_field;
1852 /* Check if the variable already exists */
1853 for (i = 0; i < hist_data->n_var_refs; i++) {
1854 ref_field = hist_data->var_refs[i];
1855 if (ref_field->var.idx == var_field->var.idx &&
1856 ref_field->var.hist_data == var_field->hist_data) {
1857 get_hist_field(ref_field);
1862 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
1864 if (init_var_ref(ref_field, var_field, system, event_name)) {
1865 destroy_hist_field(ref_field, 0);
1869 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
1870 ref_field->var_ref_idx = hist_data->n_var_refs++;
1876 static bool is_var_ref(char *var_name)
1878 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
1884 static char *field_name_from_var(struct hist_trigger_data *hist_data,
1890 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
1891 name = hist_data->attrs->var_defs.name[i];
1893 if (strcmp(var_name, name) == 0) {
1894 field = hist_data->attrs->var_defs.expr[i];
1895 if (contains_operator(field) || is_var_ref(field))
1904 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
1905 char *system, char *event_name,
1908 struct trace_event_call *call;
1910 if (system && event_name) {
1911 call = hist_data->event_file->event_call;
1913 if (strcmp(system, call->class->system) != 0)
1916 if (strcmp(event_name, trace_event_name(call)) != 0)
1920 if (!!system != !!event_name)
1923 if (!is_var_ref(var_name))
1928 return field_name_from_var(hist_data, var_name);
1931 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
1932 char *system, char *event_name,
1935 struct hist_field *var_field = NULL, *ref_field = NULL;
1936 struct trace_array *tr = hist_data->event_file->tr;
1938 if (!is_var_ref(var_name))
1943 var_field = find_event_var(hist_data, system, event_name, var_name);
1945 ref_field = create_var_ref(hist_data, var_field,
1946 system, event_name);
1949 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
1954 static struct ftrace_event_field *
1955 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
1956 char *field_str, unsigned long *flags)
1958 struct ftrace_event_field *field = NULL;
1959 char *field_name, *modifier, *str;
1960 struct trace_array *tr = file->tr;
1962 modifier = str = kstrdup(field_str, GFP_KERNEL);
1964 return ERR_PTR(-ENOMEM);
1966 field_name = strsep(&modifier, ".");
1968 if (strcmp(modifier, "hex") == 0)
1969 *flags |= HIST_FIELD_FL_HEX;
1970 else if (strcmp(modifier, "sym") == 0)
1971 *flags |= HIST_FIELD_FL_SYM;
1972 else if (strcmp(modifier, "sym-offset") == 0)
1973 *flags |= HIST_FIELD_FL_SYM_OFFSET;
1974 else if ((strcmp(modifier, "execname") == 0) &&
1975 (strcmp(field_name, "common_pid") == 0))
1976 *flags |= HIST_FIELD_FL_EXECNAME;
1977 else if (strcmp(modifier, "syscall") == 0)
1978 *flags |= HIST_FIELD_FL_SYSCALL;
1979 else if (strcmp(modifier, "log2") == 0)
1980 *flags |= HIST_FIELD_FL_LOG2;
1981 else if (strcmp(modifier, "usecs") == 0)
1982 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
1984 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
1985 field = ERR_PTR(-EINVAL);
1990 if (strcmp(field_name, "common_timestamp") == 0) {
1991 *flags |= HIST_FIELD_FL_TIMESTAMP;
1992 hist_data->enable_timestamps = true;
1993 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1994 hist_data->attrs->ts_in_usecs = true;
1995 } else if (strcmp(field_name, "common_cpu") == 0)
1996 *flags |= HIST_FIELD_FL_CPU;
1998 field = trace_find_event_field(file->event_call, field_name);
1999 if (!field || !field->size) {
2001 * For backward compatibility, if field_name
2002 * was "cpu", then we treat this the same as
2005 if (strcmp(field_name, "cpu") == 0) {
2006 *flags |= HIST_FIELD_FL_CPU;
2008 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2009 errpos(field_name));
2010 field = ERR_PTR(-EINVAL);
2021 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2022 struct hist_field *var_ref,
2025 struct hist_field *alias = NULL;
2026 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2028 alias = create_hist_field(hist_data, NULL, flags, var_name);
2032 alias->fn = var_ref->fn;
2033 alias->operands[0] = var_ref;
2035 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2036 destroy_hist_field(alias, 0);
2040 alias->var_ref_idx = var_ref->var_ref_idx;
2045 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2046 struct trace_event_file *file, char *str,
2047 unsigned long *flags, char *var_name)
2049 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2050 struct ftrace_event_field *field = NULL;
2051 struct hist_field *hist_field = NULL;
2054 s = strchr(str, '.');
2056 s = strchr(++s, '.');
2058 ref_system = strsep(&str, ".");
2063 ref_event = strsep(&str, ".");
2072 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2074 hist_field = parse_var_ref(hist_data, ref_system,
2075 ref_event, ref_var);
2078 hist_field = create_alias(hist_data, hist_field, var_name);
2089 field = parse_field(hist_data, file, str, flags);
2090 if (IS_ERR(field)) {
2091 ret = PTR_ERR(field);
2095 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2103 return ERR_PTR(ret);
2106 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2107 struct trace_event_file *file,
2108 char *str, unsigned long flags,
2109 char *var_name, unsigned int level);
2111 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2112 struct trace_event_file *file,
2113 char *str, unsigned long flags,
2114 char *var_name, unsigned int level)
2116 struct hist_field *operand1, *expr = NULL;
2117 unsigned long operand_flags;
2121 /* we support only -(xxx) i.e. explicit parens required */
2124 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2129 str++; /* skip leading '-' */
2131 s = strchr(str, '(');
2139 s = strrchr(str, ')');
2143 ret = -EINVAL; /* no closing ')' */
2147 flags |= HIST_FIELD_FL_EXPR;
2148 expr = create_hist_field(hist_data, NULL, flags, var_name);
2155 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2156 if (IS_ERR(operand1)) {
2157 ret = PTR_ERR(operand1);
2160 if (operand1->flags & HIST_FIELD_FL_STRING) {
2161 /* String type can not be the operand of unary operator. */
2162 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2163 destroy_hist_field(operand1, 0);
2168 expr->flags |= operand1->flags &
2169 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2170 expr->fn = hist_field_unary_minus;
2171 expr->operands[0] = operand1;
2172 expr->operator = FIELD_OP_UNARY_MINUS;
2173 expr->name = expr_str(expr, 0);
2174 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2182 destroy_hist_field(expr, 0);
2183 return ERR_PTR(ret);
2186 static int check_expr_operands(struct trace_array *tr,
2187 struct hist_field *operand1,
2188 struct hist_field *operand2)
2190 unsigned long operand1_flags = operand1->flags;
2191 unsigned long operand2_flags = operand2->flags;
2193 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2194 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2195 struct hist_field *var;
2197 var = find_var_field(operand1->var.hist_data, operand1->name);
2200 operand1_flags = var->flags;
2203 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2204 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2205 struct hist_field *var;
2207 var = find_var_field(operand2->var.hist_data, operand2->name);
2210 operand2_flags = var->flags;
2213 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2214 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2215 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2222 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2223 struct trace_event_file *file,
2224 char *str, unsigned long flags,
2225 char *var_name, unsigned int level)
2227 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2228 unsigned long operand_flags;
2229 int field_op, ret = -EINVAL;
2230 char *sep, *operand1_str;
2233 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2234 return ERR_PTR(-EINVAL);
2237 field_op = contains_operator(str);
2239 if (field_op == FIELD_OP_NONE)
2240 return parse_atom(hist_data, file, str, &flags, var_name);
2242 if (field_op == FIELD_OP_UNARY_MINUS)
2243 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2246 case FIELD_OP_MINUS:
2256 operand1_str = strsep(&str, sep);
2257 if (!operand1_str || !str)
2261 operand1 = parse_atom(hist_data, file, operand1_str,
2262 &operand_flags, NULL);
2263 if (IS_ERR(operand1)) {
2264 ret = PTR_ERR(operand1);
2268 if (operand1->flags & HIST_FIELD_FL_STRING) {
2269 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2274 /* rest of string could be another expression e.g. b+c in a+b+c */
2276 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2277 if (IS_ERR(operand2)) {
2278 ret = PTR_ERR(operand2);
2282 if (operand2->flags & HIST_FIELD_FL_STRING) {
2283 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2288 ret = check_expr_operands(file->tr, operand1, operand2);
2292 flags |= HIST_FIELD_FL_EXPR;
2294 flags |= operand1->flags &
2295 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2297 expr = create_hist_field(hist_data, NULL, flags, var_name);
2303 operand1->read_once = true;
2304 operand2->read_once = true;
2306 expr->operands[0] = operand1;
2307 expr->operands[1] = operand2;
2309 /* The operand sizes should be the same, so just pick one */
2310 expr->size = operand1->size;
2312 expr->operator = field_op;
2313 expr->name = expr_str(expr, 0);
2314 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2321 case FIELD_OP_MINUS:
2322 expr->fn = hist_field_minus;
2325 expr->fn = hist_field_plus;
2334 destroy_hist_field(operand1, 0);
2335 destroy_hist_field(operand2, 0);
2336 destroy_hist_field(expr, 0);
2338 return ERR_PTR(ret);
2341 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2342 struct trace_event_file *file)
2344 struct event_trigger_data *test;
2346 lockdep_assert_held(&event_mutex);
2348 list_for_each_entry(test, &file->triggers, list) {
2349 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2350 if (test->private_data == hist_data)
2351 return test->filter_str;
2358 static struct event_command trigger_hist_cmd;
2359 static int event_hist_trigger_func(struct event_command *cmd_ops,
2360 struct trace_event_file *file,
2361 char *glob, char *cmd, char *param);
2363 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2364 struct hist_trigger_data *hist_data,
2365 unsigned int n_keys)
2367 struct hist_field *target_hist_field, *hist_field;
2368 unsigned int n, i, j;
2370 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2373 i = hist_data->n_vals;
2374 j = target_hist_data->n_vals;
2376 for (n = 0; n < n_keys; n++) {
2377 hist_field = hist_data->fields[i + n];
2378 target_hist_field = target_hist_data->fields[j + n];
2380 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2382 if (hist_field->size != target_hist_field->size)
2384 if (hist_field->is_signed != target_hist_field->is_signed)
2391 static struct hist_trigger_data *
2392 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2393 struct trace_event_file *file)
2395 struct hist_trigger_data *hist_data;
2396 struct event_trigger_data *test;
2397 unsigned int n_keys;
2399 lockdep_assert_held(&event_mutex);
2401 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2403 list_for_each_entry(test, &file->triggers, list) {
2404 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2405 hist_data = test->private_data;
2407 if (compatible_keys(target_hist_data, hist_data, n_keys))
2415 static struct trace_event_file *event_file(struct trace_array *tr,
2416 char *system, char *event_name)
2418 struct trace_event_file *file;
2420 file = __find_event_file(tr, system, event_name);
2422 return ERR_PTR(-EINVAL);
2427 static struct hist_field *
2428 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2429 char *system, char *event_name, char *field_name)
2431 struct hist_field *event_var;
2432 char *synthetic_name;
2434 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2435 if (!synthetic_name)
2436 return ERR_PTR(-ENOMEM);
2438 strcpy(synthetic_name, "synthetic_");
2439 strcat(synthetic_name, field_name);
2441 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2443 kfree(synthetic_name);
2449 * create_field_var_hist - Automatically create a histogram and var for a field
2450 * @target_hist_data: The target hist trigger
2451 * @subsys_name: Optional subsystem name
2452 * @event_name: Optional event name
2453 * @field_name: The name of the field (and the resulting variable)
2455 * Hist trigger actions fetch data from variables, not directly from
2456 * events. However, for convenience, users are allowed to directly
2457 * specify an event field in an action, which will be automatically
2458 * converted into a variable on their behalf.
2460 * If a user specifies a field on an event that isn't the event the
2461 * histogram currently being defined (the target event histogram), the
2462 * only way that can be accomplished is if a new hist trigger is
2463 * created and the field variable defined on that.
2465 * This function creates a new histogram compatible with the target
2466 * event (meaning a histogram with the same key as the target
2467 * histogram), and creates a variable for the specified field, but
2468 * with 'synthetic_' prepended to the variable name in order to avoid
2469 * collision with normal field variables.
2471 * Return: The variable created for the field.
2473 static struct hist_field *
2474 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2475 char *subsys_name, char *event_name, char *field_name)
2477 struct trace_array *tr = target_hist_data->event_file->tr;
2478 struct hist_field *event_var = ERR_PTR(-EINVAL);
2479 struct hist_trigger_data *hist_data;
2480 unsigned int i, n, first = true;
2481 struct field_var_hist *var_hist;
2482 struct trace_event_file *file;
2483 struct hist_field *key_field;
2488 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2489 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2490 return ERR_PTR(-EINVAL);
2493 file = event_file(tr, subsys_name, event_name);
2496 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2497 ret = PTR_ERR(file);
2498 return ERR_PTR(ret);
2502 * Look for a histogram compatible with target. We'll use the
2503 * found histogram specification to create a new matching
2504 * histogram with our variable on it. target_hist_data is not
2505 * yet a registered histogram so we can't use that.
2507 hist_data = find_compatible_hist(target_hist_data, file);
2509 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2510 return ERR_PTR(-EINVAL);
2513 /* See if a synthetic field variable has already been created */
2514 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2515 event_name, field_name);
2516 if (!IS_ERR_OR_NULL(event_var))
2519 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2521 return ERR_PTR(-ENOMEM);
2523 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2526 return ERR_PTR(-ENOMEM);
2529 /* Use the same keys as the compatible histogram */
2530 strcat(cmd, "keys=");
2532 for_each_hist_key_field(i, hist_data) {
2533 key_field = hist_data->fields[i];
2536 strcat(cmd, key_field->field->name);
2540 /* Create the synthetic field variable specification */
2541 strcat(cmd, ":synthetic_");
2542 strcat(cmd, field_name);
2544 strcat(cmd, field_name);
2546 /* Use the same filter as the compatible histogram */
2547 saved_filter = find_trigger_filter(hist_data, file);
2549 strcat(cmd, " if ");
2550 strcat(cmd, saved_filter);
2553 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
2554 if (!var_hist->cmd) {
2557 return ERR_PTR(-ENOMEM);
2560 /* Save the compatible histogram information */
2561 var_hist->hist_data = hist_data;
2563 /* Create the new histogram with our variable */
2564 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
2568 kfree(var_hist->cmd);
2570 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
2571 return ERR_PTR(ret);
2576 /* If we can't find the variable, something went wrong */
2577 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2578 event_name, field_name);
2579 if (IS_ERR_OR_NULL(event_var)) {
2580 kfree(var_hist->cmd);
2582 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
2583 return ERR_PTR(-EINVAL);
2586 n = target_hist_data->n_field_var_hists;
2587 target_hist_data->field_var_hists[n] = var_hist;
2588 target_hist_data->n_field_var_hists++;
2593 static struct hist_field *
2594 find_target_event_var(struct hist_trigger_data *hist_data,
2595 char *subsys_name, char *event_name, char *var_name)
2597 struct trace_event_file *file = hist_data->event_file;
2598 struct hist_field *hist_field = NULL;
2601 struct trace_event_call *call;
2606 call = file->event_call;
2608 if (strcmp(subsys_name, call->class->system) != 0)
2611 if (strcmp(event_name, trace_event_name(call)) != 0)
2615 hist_field = find_var_field(hist_data, var_name);
2620 static inline void __update_field_vars(struct tracing_map_elt *elt,
2621 struct trace_buffer *buffer,
2622 struct ring_buffer_event *rbe,
2624 struct field_var **field_vars,
2625 unsigned int n_field_vars,
2626 unsigned int field_var_str_start)
2628 struct hist_elt_data *elt_data = elt->private_data;
2629 unsigned int i, j, var_idx;
2632 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
2633 struct field_var *field_var = field_vars[i];
2634 struct hist_field *var = field_var->var;
2635 struct hist_field *val = field_var->val;
2637 var_val = val->fn(val, elt, buffer, rbe, rec);
2638 var_idx = var->var.idx;
2640 if (val->flags & HIST_FIELD_FL_STRING) {
2641 char *str = elt_data->field_var_str[j++];
2642 char *val_str = (char *)(uintptr_t)var_val;
2644 strscpy(str, val_str, STR_VAR_LEN_MAX);
2645 var_val = (u64)(uintptr_t)str;
2647 tracing_map_set_var(elt, var_idx, var_val);
2651 static void update_field_vars(struct hist_trigger_data *hist_data,
2652 struct tracing_map_elt *elt,
2653 struct trace_buffer *buffer,
2654 struct ring_buffer_event *rbe,
2657 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
2658 hist_data->n_field_vars, 0);
2661 static void save_track_data_vars(struct hist_trigger_data *hist_data,
2662 struct tracing_map_elt *elt,
2663 struct trace_buffer *buffer, void *rec,
2664 struct ring_buffer_event *rbe, void *key,
2665 struct action_data *data, u64 *var_ref_vals)
2667 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
2668 hist_data->n_save_vars, hist_data->n_field_var_str);
2671 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
2672 struct trace_event_file *file,
2673 char *name, int size, const char *type)
2675 struct hist_field *var;
2678 if (find_var(hist_data, file, name) && !hist_data->remove) {
2679 var = ERR_PTR(-EINVAL);
2683 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2685 var = ERR_PTR(-ENOMEM);
2689 idx = tracing_map_add_var(hist_data->map);
2692 var = ERR_PTR(-EINVAL);
2697 var->flags = HIST_FIELD_FL_VAR;
2699 var->var.hist_data = var->hist_data = hist_data;
2701 var->var.name = kstrdup(name, GFP_KERNEL);
2702 var->type = kstrdup(type, GFP_KERNEL);
2703 if (!var->var.name || !var->type) {
2704 kfree(var->var.name);
2707 var = ERR_PTR(-ENOMEM);
2713 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
2714 struct trace_event_file *file,
2717 struct hist_field *val = NULL, *var = NULL;
2718 unsigned long flags = HIST_FIELD_FL_VAR;
2719 struct trace_array *tr = file->tr;
2720 struct field_var *field_var;
2723 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
2724 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2729 val = parse_atom(hist_data, file, field_name, &flags, NULL);
2731 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
2736 var = create_var(hist_data, file, field_name, val->size, val->type);
2738 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
2744 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
2752 field_var->var = var;
2753 field_var->val = val;
2757 field_var = ERR_PTR(ret);
2762 * create_target_field_var - Automatically create a variable for a field
2763 * @target_hist_data: The target hist trigger
2764 * @subsys_name: Optional subsystem name
2765 * @event_name: Optional event name
2766 * @var_name: The name of the field (and the resulting variable)
2768 * Hist trigger actions fetch data from variables, not directly from
2769 * events. However, for convenience, users are allowed to directly
2770 * specify an event field in an action, which will be automatically
2771 * converted into a variable on their behalf.
2773 * This function creates a field variable with the name var_name on
2774 * the hist trigger currently being defined on the target event. If
2775 * subsys_name and event_name are specified, this function simply
2776 * verifies that they do in fact match the target event subsystem and
2779 * Return: The variable created for the field.
2781 static struct field_var *
2782 create_target_field_var(struct hist_trigger_data *target_hist_data,
2783 char *subsys_name, char *event_name, char *var_name)
2785 struct trace_event_file *file = target_hist_data->event_file;
2788 struct trace_event_call *call;
2793 call = file->event_call;
2795 if (strcmp(subsys_name, call->class->system) != 0)
2798 if (strcmp(event_name, trace_event_name(call)) != 0)
2802 return create_field_var(target_hist_data, file, var_name);
2805 static bool check_track_val_max(u64 track_val, u64 var_val)
2807 if (var_val <= track_val)
2813 static bool check_track_val_changed(u64 track_val, u64 var_val)
2815 if (var_val == track_val)
2821 static u64 get_track_val(struct hist_trigger_data *hist_data,
2822 struct tracing_map_elt *elt,
2823 struct action_data *data)
2825 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2828 track_val = tracing_map_read_var(elt, track_var_idx);
2833 static void save_track_val(struct hist_trigger_data *hist_data,
2834 struct tracing_map_elt *elt,
2835 struct action_data *data, u64 var_val)
2837 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2839 tracing_map_set_var(elt, track_var_idx, var_val);
2842 static void save_track_data(struct hist_trigger_data *hist_data,
2843 struct tracing_map_elt *elt,
2844 struct trace_buffer *buffer, void *rec,
2845 struct ring_buffer_event *rbe, void *key,
2846 struct action_data *data, u64 *var_ref_vals)
2848 if (data->track_data.save_data)
2849 data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
2850 key, data, var_ref_vals);
2853 static bool check_track_val(struct tracing_map_elt *elt,
2854 struct action_data *data,
2857 struct hist_trigger_data *hist_data;
2860 hist_data = data->track_data.track_var->hist_data;
2861 track_val = get_track_val(hist_data, elt, data);
2863 return data->track_data.check_val(track_val, var_val);
2866 #ifdef CONFIG_TRACER_SNAPSHOT
2867 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2869 /* called with tr->max_lock held */
2870 struct track_data *track_data = tr->cond_snapshot->cond_data;
2871 struct hist_elt_data *elt_data, *track_elt_data;
2872 struct snapshot_context *context = cond_data;
2873 struct action_data *action;
2879 action = track_data->action_data;
2881 track_val = get_track_val(track_data->hist_data, context->elt,
2882 track_data->action_data);
2884 if (!action->track_data.check_val(track_data->track_val, track_val))
2887 track_data->track_val = track_val;
2888 memcpy(track_data->key, context->key, track_data->key_len);
2890 elt_data = context->elt->private_data;
2891 track_elt_data = track_data->elt.private_data;
2893 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
2895 track_data->updated = true;
2900 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2901 struct tracing_map_elt *elt,
2902 struct trace_buffer *buffer, void *rec,
2903 struct ring_buffer_event *rbe, void *key,
2904 struct action_data *data,
2907 struct trace_event_file *file = hist_data->event_file;
2908 struct snapshot_context context;
2913 tracing_snapshot_cond(file->tr, &context);
2916 static void hist_trigger_print_key(struct seq_file *m,
2917 struct hist_trigger_data *hist_data,
2919 struct tracing_map_elt *elt);
2921 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
2925 if (!hist_data->n_actions)
2928 for (i = 0; i < hist_data->n_actions; i++) {
2929 struct action_data *data = hist_data->actions[i];
2931 if (data->action == ACTION_SNAPSHOT)
2938 static void track_data_snapshot_print(struct seq_file *m,
2939 struct hist_trigger_data *hist_data)
2941 struct trace_event_file *file = hist_data->event_file;
2942 struct track_data *track_data;
2943 struct action_data *action;
2945 track_data = tracing_cond_snapshot_data(file->tr);
2949 if (!track_data->updated)
2952 action = snapshot_action(hist_data);
2956 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
2957 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
2958 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
2959 action->track_data.var_str, track_data->track_val);
2961 seq_puts(m, "\ttriggered by event with key: ");
2962 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
2966 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2970 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2971 struct tracing_map_elt *elt,
2972 struct trace_buffer *buffer, void *rec,
2973 struct ring_buffer_event *rbe, void *key,
2974 struct action_data *data,
2975 u64 *var_ref_vals) {}
2976 static void track_data_snapshot_print(struct seq_file *m,
2977 struct hist_trigger_data *hist_data) {}
2978 #endif /* CONFIG_TRACER_SNAPSHOT */
2980 static void track_data_print(struct seq_file *m,
2981 struct hist_trigger_data *hist_data,
2982 struct tracing_map_elt *elt,
2983 struct action_data *data)
2985 u64 track_val = get_track_val(hist_data, elt, data);
2986 unsigned int i, save_var_idx;
2988 if (data->handler == HANDLER_ONMAX)
2989 seq_printf(m, "\n\tmax: %10llu", track_val);
2990 else if (data->handler == HANDLER_ONCHANGE)
2991 seq_printf(m, "\n\tchanged: %10llu", track_val);
2993 if (data->action == ACTION_SNAPSHOT)
2996 for (i = 0; i < hist_data->n_save_vars; i++) {
2997 struct hist_field *save_val = hist_data->save_vars[i]->val;
2998 struct hist_field *save_var = hist_data->save_vars[i]->var;
3001 save_var_idx = save_var->var.idx;
3003 val = tracing_map_read_var(elt, save_var_idx);
3005 if (save_val->flags & HIST_FIELD_FL_STRING) {
3006 seq_printf(m, " %s: %-32s", save_var->var.name,
3007 (char *)(uintptr_t)(val));
3009 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3013 static void ontrack_action(struct hist_trigger_data *hist_data,
3014 struct tracing_map_elt *elt,
3015 struct trace_buffer *buffer, void *rec,
3016 struct ring_buffer_event *rbe, void *key,
3017 struct action_data *data, u64 *var_ref_vals)
3019 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3021 if (check_track_val(elt, data, var_val)) {
3022 save_track_val(hist_data, elt, data, var_val);
3023 save_track_data(hist_data, elt, buffer, rec, rbe,
3024 key, data, var_ref_vals);
3028 static void action_data_destroy(struct action_data *data)
3032 lockdep_assert_held(&event_mutex);
3034 kfree(data->action_name);
3036 for (i = 0; i < data->n_params; i++)
3037 kfree(data->params[i]);
3039 if (data->synth_event)
3040 data->synth_event->ref--;
3042 kfree(data->synth_event_name);
3047 static void track_data_destroy(struct hist_trigger_data *hist_data,
3048 struct action_data *data)
3050 struct trace_event_file *file = hist_data->event_file;
3052 destroy_hist_field(data->track_data.track_var, 0);
3054 if (data->action == ACTION_SNAPSHOT) {
3055 struct track_data *track_data;
3057 track_data = tracing_cond_snapshot_data(file->tr);
3058 if (track_data && track_data->hist_data == hist_data) {
3059 tracing_snapshot_cond_disable(file->tr);
3060 track_data_free(track_data);
3064 kfree(data->track_data.var_str);
3066 action_data_destroy(data);
3069 static int action_create(struct hist_trigger_data *hist_data,
3070 struct action_data *data);
3072 static int track_data_create(struct hist_trigger_data *hist_data,
3073 struct action_data *data)
3075 struct hist_field *var_field, *ref_field, *track_var = NULL;
3076 struct trace_event_file *file = hist_data->event_file;
3077 struct trace_array *tr = file->tr;
3078 char *track_data_var_str;
3081 track_data_var_str = data->track_data.var_str;
3082 if (track_data_var_str[0] != '$') {
3083 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3086 track_data_var_str++;
3088 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3090 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3094 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3098 data->track_data.var_ref = ref_field;
3100 if (data->handler == HANDLER_ONMAX)
3101 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3102 if (IS_ERR(track_var)) {
3103 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3104 ret = PTR_ERR(track_var);
3108 if (data->handler == HANDLER_ONCHANGE)
3109 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3110 if (IS_ERR(track_var)) {
3111 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3112 ret = PTR_ERR(track_var);
3115 data->track_data.track_var = track_var;
3117 ret = action_create(hist_data, data);
3122 static int parse_action_params(struct trace_array *tr, char *params,
3123 struct action_data *data)
3125 char *param, *saved_param;
3126 bool first_param = true;
3130 if (data->n_params >= SYNTH_FIELDS_MAX) {
3131 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3135 param = strsep(¶ms, ",");
3137 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3142 param = strstrip(param);
3143 if (strlen(param) < 2) {
3144 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3149 saved_param = kstrdup(param, GFP_KERNEL);
3155 if (first_param && data->use_trace_keyword) {
3156 data->synth_event_name = saved_param;
3157 first_param = false;
3160 first_param = false;
3162 data->params[data->n_params++] = saved_param;
3168 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3169 enum handler_id handler)
3176 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3181 action_name = strsep(&str, "(");
3182 if (!action_name || !str) {
3183 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3188 if (str_has_prefix(action_name, "save")) {
3189 char *params = strsep(&str, ")");
3192 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3197 ret = parse_action_params(tr, params, data);
3201 if (handler == HANDLER_ONMAX)
3202 data->track_data.check_val = check_track_val_max;
3203 else if (handler == HANDLER_ONCHANGE)
3204 data->track_data.check_val = check_track_val_changed;
3206 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3211 data->track_data.save_data = save_track_data_vars;
3212 data->fn = ontrack_action;
3213 data->action = ACTION_SAVE;
3214 } else if (str_has_prefix(action_name, "snapshot")) {
3215 char *params = strsep(&str, ")");
3218 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3223 if (handler == HANDLER_ONMAX)
3224 data->track_data.check_val = check_track_val_max;
3225 else if (handler == HANDLER_ONCHANGE)
3226 data->track_data.check_val = check_track_val_changed;
3228 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3233 data->track_data.save_data = save_track_data_snapshot;
3234 data->fn = ontrack_action;
3235 data->action = ACTION_SNAPSHOT;
3237 char *params = strsep(&str, ")");
3239 if (str_has_prefix(action_name, "trace"))
3240 data->use_trace_keyword = true;
3243 ret = parse_action_params(tr, params, data);
3248 if (handler == HANDLER_ONMAX)
3249 data->track_data.check_val = check_track_val_max;
3250 else if (handler == HANDLER_ONCHANGE)
3251 data->track_data.check_val = check_track_val_changed;
3253 if (handler != HANDLER_ONMATCH) {
3254 data->track_data.save_data = action_trace;
3255 data->fn = ontrack_action;
3257 data->fn = action_trace;
3259 data->action = ACTION_TRACE;
3262 data->action_name = kstrdup(action_name, GFP_KERNEL);
3263 if (!data->action_name) {
3268 data->handler = handler;
3273 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3274 char *str, enum handler_id handler)
3276 struct action_data *data;
3280 data = kzalloc(sizeof(*data), GFP_KERNEL);
3282 return ERR_PTR(-ENOMEM);
3284 var_str = strsep(&str, ")");
3285 if (!var_str || !str) {
3290 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3291 if (!data->track_data.var_str) {
3296 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3302 track_data_destroy(hist_data, data);
3303 data = ERR_PTR(ret);
3307 static void onmatch_destroy(struct action_data *data)
3309 kfree(data->match_data.event);
3310 kfree(data->match_data.event_system);
3312 action_data_destroy(data);
3315 static void destroy_field_var(struct field_var *field_var)
3320 destroy_hist_field(field_var->var, 0);
3321 destroy_hist_field(field_var->val, 0);
3326 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3330 for (i = 0; i < hist_data->n_field_vars; i++)
3331 destroy_field_var(hist_data->field_vars[i]);
3333 for (i = 0; i < hist_data->n_save_vars; i++)
3334 destroy_field_var(hist_data->save_vars[i]);
3337 static void save_field_var(struct hist_trigger_data *hist_data,
3338 struct field_var *field_var)
3340 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3342 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3343 hist_data->n_field_var_str++;
3347 static int check_synth_field(struct synth_event *event,
3348 struct hist_field *hist_field,
3349 unsigned int field_pos)
3351 struct synth_field *field;
3353 if (field_pos >= event->n_fields)
3356 field = event->fields[field_pos];
3359 * A dynamic string synth field can accept static or
3360 * dynamic. A static string synth field can only accept a
3361 * same-sized static string, which is checked for later.
3363 if (strstr(hist_field->type, "char[") && field->is_string
3364 && field->is_dynamic)
3367 if (strcmp(field->type, hist_field->type) != 0) {
3368 if (field->size != hist_field->size ||
3369 field->is_signed != hist_field->is_signed)
3376 static struct hist_field *
3377 trace_action_find_var(struct hist_trigger_data *hist_data,
3378 struct action_data *data,
3379 char *system, char *event, char *var)
3381 struct trace_array *tr = hist_data->event_file->tr;
3382 struct hist_field *hist_field;
3384 var++; /* skip '$' */
3386 hist_field = find_target_event_var(hist_data, system, event, var);
3388 if (!system && data->handler == HANDLER_ONMATCH) {
3389 system = data->match_data.event_system;
3390 event = data->match_data.event;
3393 hist_field = find_event_var(hist_data, system, event, var);
3397 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3402 static struct hist_field *
3403 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3404 struct action_data *data, char *system,
3405 char *event, char *var)
3407 struct hist_field *hist_field = NULL;
3408 struct field_var *field_var;
3411 * First try to create a field var on the target event (the
3412 * currently being defined). This will create a variable for
3413 * unqualified fields on the target event, or if qualified,
3414 * target fields that have qualified names matching the target.
3416 field_var = create_target_field_var(hist_data, system, event, var);
3418 if (field_var && !IS_ERR(field_var)) {
3419 save_field_var(hist_data, field_var);
3420 hist_field = field_var->var;
3424 * If no explicit system.event is specified, default to
3425 * looking for fields on the onmatch(system.event.xxx)
3428 if (!system && data->handler == HANDLER_ONMATCH) {
3429 system = data->match_data.event_system;
3430 event = data->match_data.event;
3436 * At this point, we're looking at a field on another
3437 * event. Because we can't modify a hist trigger on
3438 * another event to add a variable for a field, we need
3439 * to create a new trigger on that event and create the
3440 * variable at the same time.
3442 hist_field = create_field_var_hist(hist_data, system, event, var);
3443 if (IS_ERR(hist_field))
3449 destroy_field_var(field_var);
3454 static int trace_action_create(struct hist_trigger_data *hist_data,
3455 struct action_data *data)
3457 struct trace_array *tr = hist_data->event_file->tr;
3458 char *event_name, *param, *system = NULL;
3459 struct hist_field *hist_field, *var_ref;
3461 unsigned int field_pos = 0;
3462 struct synth_event *event;
3463 char *synth_event_name;
3464 int var_ref_idx, ret = 0;
3466 lockdep_assert_held(&event_mutex);
3468 if (data->use_trace_keyword)
3469 synth_event_name = data->synth_event_name;
3471 synth_event_name = data->action_name;
3473 event = find_synth_event(synth_event_name);
3475 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3481 for (i = 0; i < data->n_params; i++) {
3484 p = param = kstrdup(data->params[i], GFP_KERNEL);
3490 system = strsep(¶m, ".");
3492 param = (char *)system;
3493 system = event_name = NULL;
3495 event_name = strsep(¶m, ".");
3503 if (param[0] == '$')
3504 hist_field = trace_action_find_var(hist_data, data,
3508 hist_field = trace_action_create_field_var(hist_data,
3520 if (check_synth_field(event, hist_field, field_pos) == 0) {
3521 var_ref = create_var_ref(hist_data, hist_field,
3522 system, event_name);
3529 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
3530 if (WARN_ON(var_ref_idx < 0)) {
3535 data->var_ref_idx[i] = var_ref_idx;
3542 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
3548 if (field_pos != event->n_fields) {
3549 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
3554 data->synth_event = event;
3563 static int action_create(struct hist_trigger_data *hist_data,
3564 struct action_data *data)
3566 struct trace_event_file *file = hist_data->event_file;
3567 struct trace_array *tr = file->tr;
3568 struct track_data *track_data;
3569 struct field_var *field_var;
3574 if (data->action == ACTION_TRACE)
3575 return trace_action_create(hist_data, data);
3577 if (data->action == ACTION_SNAPSHOT) {
3578 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
3579 if (IS_ERR(track_data)) {
3580 ret = PTR_ERR(track_data);
3584 ret = tracing_snapshot_cond_enable(file->tr, track_data,
3585 cond_snapshot_update);
3587 track_data_free(track_data);
3592 if (data->action == ACTION_SAVE) {
3593 if (hist_data->n_save_vars) {
3595 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
3599 for (i = 0; i < data->n_params; i++) {
3600 param = kstrdup(data->params[i], GFP_KERNEL);
3606 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3607 if (IS_ERR(field_var)) {
3608 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
3610 ret = PTR_ERR(field_var);
3615 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
3616 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3617 hist_data->n_save_var_str++;
3625 static int onmatch_create(struct hist_trigger_data *hist_data,
3626 struct action_data *data)
3628 return action_create(hist_data, data);
3631 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3633 char *match_event, *match_event_system;
3634 struct action_data *data;
3637 data = kzalloc(sizeof(*data), GFP_KERNEL);
3639 return ERR_PTR(-ENOMEM);
3641 match_event = strsep(&str, ")");
3642 if (!match_event || !str) {
3643 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
3647 match_event_system = strsep(&match_event, ".");
3649 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
3653 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3654 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
3658 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
3659 if (!data->match_data.event) {
3664 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
3665 if (!data->match_data.event_system) {
3670 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
3676 onmatch_destroy(data);
3677 data = ERR_PTR(ret);
3681 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3683 hist_data->fields[HITCOUNT_IDX] =
3684 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3685 if (!hist_data->fields[HITCOUNT_IDX])
3688 hist_data->n_vals++;
3689 hist_data->n_fields++;
3691 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3697 static int __create_val_field(struct hist_trigger_data *hist_data,
3698 unsigned int val_idx,
3699 struct trace_event_file *file,
3700 char *var_name, char *field_str,
3701 unsigned long flags)
3703 struct hist_field *hist_field;
3706 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3707 if (IS_ERR(hist_field)) {
3708 ret = PTR_ERR(hist_field);
3712 hist_data->fields[val_idx] = hist_field;
3714 ++hist_data->n_vals;
3715 ++hist_data->n_fields;
3717 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3723 static int create_val_field(struct hist_trigger_data *hist_data,
3724 unsigned int val_idx,
3725 struct trace_event_file *file,
3728 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3731 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3734 static int create_var_field(struct hist_trigger_data *hist_data,
3735 unsigned int val_idx,
3736 struct trace_event_file *file,
3737 char *var_name, char *expr_str)
3739 struct trace_array *tr = hist_data->event_file->tr;
3740 unsigned long flags = 0;
3743 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3746 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3747 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
3751 flags |= HIST_FIELD_FL_VAR;
3752 hist_data->n_vars++;
3753 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3756 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3758 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
3759 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
3764 static int create_val_fields(struct hist_trigger_data *hist_data,
3765 struct trace_event_file *file)
3767 char *fields_str, *field_str;
3768 unsigned int i, j = 1;
3771 ret = create_hitcount_val(hist_data);
3775 fields_str = hist_data->attrs->vals_str;
3779 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3780 j < TRACING_MAP_VALS_MAX; i++) {
3781 field_str = strsep(&fields_str, ",");
3785 if (strcmp(field_str, "hitcount") == 0)
3788 ret = create_val_field(hist_data, j++, file, field_str);
3793 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3799 static int create_key_field(struct hist_trigger_data *hist_data,
3800 unsigned int key_idx,
3801 unsigned int key_offset,
3802 struct trace_event_file *file,
3805 struct trace_array *tr = hist_data->event_file->tr;
3806 struct hist_field *hist_field = NULL;
3807 unsigned long flags = 0;
3808 unsigned int key_size;
3811 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
3814 flags |= HIST_FIELD_FL_KEY;
3816 if (strcmp(field_str, "stacktrace") == 0) {
3817 flags |= HIST_FIELD_FL_STACKTRACE;
3818 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
3819 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
3821 hist_field = parse_expr(hist_data, file, field_str, flags,
3823 if (IS_ERR(hist_field)) {
3824 ret = PTR_ERR(hist_field);
3828 if (field_has_hist_vars(hist_field, 0)) {
3829 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
3830 destroy_hist_field(hist_field, 0);
3835 key_size = hist_field->size;
3838 hist_data->fields[key_idx] = hist_field;
3840 key_size = ALIGN(key_size, sizeof(u64));
3841 hist_data->fields[key_idx]->size = key_size;
3842 hist_data->fields[key_idx]->offset = key_offset;
3844 hist_data->key_size += key_size;
3846 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3851 hist_data->n_keys++;
3852 hist_data->n_fields++;
3854 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
3862 static int create_key_fields(struct hist_trigger_data *hist_data,
3863 struct trace_event_file *file)
3865 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
3866 char *fields_str, *field_str;
3869 fields_str = hist_data->attrs->keys_str;
3873 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
3874 field_str = strsep(&fields_str, ",");
3877 ret = create_key_field(hist_data, i, key_offset,
3892 static int create_var_fields(struct hist_trigger_data *hist_data,
3893 struct trace_event_file *file)
3895 unsigned int i, j = hist_data->n_vals;
3898 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
3900 for (i = 0; i < n_vars; i++) {
3901 char *var_name = hist_data->attrs->var_defs.name[i];
3902 char *expr = hist_data->attrs->var_defs.expr[i];
3904 ret = create_var_field(hist_data, j++, file, var_name, expr);
3912 static void free_var_defs(struct hist_trigger_data *hist_data)
3916 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3917 kfree(hist_data->attrs->var_defs.name[i]);
3918 kfree(hist_data->attrs->var_defs.expr[i]);
3921 hist_data->attrs->var_defs.n_vars = 0;
3924 static int parse_var_defs(struct hist_trigger_data *hist_data)
3926 struct trace_array *tr = hist_data->event_file->tr;
3927 char *s, *str, *var_name, *field_str;
3928 unsigned int i, j, n_vars = 0;
3931 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
3932 str = hist_data->attrs->assignment_str[i];
3933 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
3934 field_str = strsep(&str, ",");
3938 var_name = strsep(&field_str, "=");
3939 if (!var_name || !field_str) {
3940 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
3946 if (n_vars == TRACING_MAP_VARS_MAX) {
3947 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
3952 s = kstrdup(var_name, GFP_KERNEL);
3957 hist_data->attrs->var_defs.name[n_vars] = s;
3959 s = kstrdup(field_str, GFP_KERNEL);
3964 hist_data->attrs->var_defs.expr[n_vars++] = s;
3966 hist_data->attrs->var_defs.n_vars = n_vars;
3972 free_var_defs(hist_data);
3977 static int create_hist_fields(struct hist_trigger_data *hist_data,
3978 struct trace_event_file *file)
3982 ret = parse_var_defs(hist_data);
3986 ret = create_val_fields(hist_data, file);
3990 ret = create_var_fields(hist_data, file);
3994 ret = create_key_fields(hist_data, file);
3998 free_var_defs(hist_data);
4003 static int is_descending(struct trace_array *tr, const char *str)
4008 if (strcmp(str, "descending") == 0)
4011 if (strcmp(str, "ascending") == 0)
4014 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4019 static int create_sort_keys(struct hist_trigger_data *hist_data)
4021 struct trace_array *tr = hist_data->event_file->tr;
4022 char *fields_str = hist_data->attrs->sort_key_str;
4023 struct tracing_map_sort_key *sort_key;
4024 int descending, ret = 0;
4025 unsigned int i, j, k;
4027 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4032 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4033 struct hist_field *hist_field;
4034 char *field_str, *field_name;
4035 const char *test_name;
4037 sort_key = &hist_data->sort_keys[i];
4039 field_str = strsep(&fields_str, ",");
4045 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4049 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4050 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4055 field_name = strsep(&field_str, ".");
4056 if (!field_name || !*field_name) {
4058 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4062 if (strcmp(field_name, "hitcount") == 0) {
4063 descending = is_descending(tr, field_str);
4064 if (descending < 0) {
4068 sort_key->descending = descending;
4072 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4075 hist_field = hist_data->fields[j];
4076 if (hist_field->flags & HIST_FIELD_FL_VAR)
4081 test_name = hist_field_name(hist_field, 0);
4083 if (strcmp(field_name, test_name) == 0) {
4084 sort_key->field_idx = idx;
4085 descending = is_descending(tr, field_str);
4086 if (descending < 0) {
4090 sort_key->descending = descending;
4094 if (j == hist_data->n_fields) {
4096 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4101 hist_data->n_sort_keys = i;
4106 static void destroy_actions(struct hist_trigger_data *hist_data)
4110 for (i = 0; i < hist_data->n_actions; i++) {
4111 struct action_data *data = hist_data->actions[i];
4113 if (data->handler == HANDLER_ONMATCH)
4114 onmatch_destroy(data);
4115 else if (data->handler == HANDLER_ONMAX ||
4116 data->handler == HANDLER_ONCHANGE)
4117 track_data_destroy(hist_data, data);
4123 static int parse_actions(struct hist_trigger_data *hist_data)
4125 struct trace_array *tr = hist_data->event_file->tr;
4126 struct action_data *data;
4132 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4133 str = hist_data->attrs->action_str[i];
4135 if ((len = str_has_prefix(str, "onmatch("))) {
4136 char *action_str = str + len;
4138 data = onmatch_parse(tr, action_str);
4140 ret = PTR_ERR(data);
4143 } else if ((len = str_has_prefix(str, "onmax("))) {
4144 char *action_str = str + len;
4146 data = track_data_parse(hist_data, action_str,
4149 ret = PTR_ERR(data);
4152 } else if ((len = str_has_prefix(str, "onchange("))) {
4153 char *action_str = str + len;
4155 data = track_data_parse(hist_data, action_str,
4158 ret = PTR_ERR(data);
4166 hist_data->actions[hist_data->n_actions++] = data;
4172 static int create_actions(struct hist_trigger_data *hist_data)
4174 struct action_data *data;
4178 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4179 data = hist_data->actions[i];
4181 if (data->handler == HANDLER_ONMATCH) {
4182 ret = onmatch_create(hist_data, data);
4185 } else if (data->handler == HANDLER_ONMAX ||
4186 data->handler == HANDLER_ONCHANGE) {
4187 ret = track_data_create(hist_data, data);
4199 static void print_actions(struct seq_file *m,
4200 struct hist_trigger_data *hist_data,
4201 struct tracing_map_elt *elt)
4205 for (i = 0; i < hist_data->n_actions; i++) {
4206 struct action_data *data = hist_data->actions[i];
4208 if (data->action == ACTION_SNAPSHOT)
4211 if (data->handler == HANDLER_ONMAX ||
4212 data->handler == HANDLER_ONCHANGE)
4213 track_data_print(m, hist_data, elt, data);
4217 static void print_action_spec(struct seq_file *m,
4218 struct hist_trigger_data *hist_data,
4219 struct action_data *data)
4223 if (data->action == ACTION_SAVE) {
4224 for (i = 0; i < hist_data->n_save_vars; i++) {
4225 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4226 if (i < hist_data->n_save_vars - 1)
4229 } else if (data->action == ACTION_TRACE) {
4230 if (data->use_trace_keyword)
4231 seq_printf(m, "%s", data->synth_event_name);
4232 for (i = 0; i < data->n_params; i++) {
4233 if (i || data->use_trace_keyword)
4235 seq_printf(m, "%s", data->params[i]);
4240 static void print_track_data_spec(struct seq_file *m,
4241 struct hist_trigger_data *hist_data,
4242 struct action_data *data)
4244 if (data->handler == HANDLER_ONMAX)
4245 seq_puts(m, ":onmax(");
4246 else if (data->handler == HANDLER_ONCHANGE)
4247 seq_puts(m, ":onchange(");
4248 seq_printf(m, "%s", data->track_data.var_str);
4249 seq_printf(m, ").%s(", data->action_name);
4251 print_action_spec(m, hist_data, data);
4256 static void print_onmatch_spec(struct seq_file *m,
4257 struct hist_trigger_data *hist_data,
4258 struct action_data *data)
4260 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4261 data->match_data.event);
4263 seq_printf(m, "%s(", data->action_name);
4265 print_action_spec(m, hist_data, data);
4270 static bool actions_match(struct hist_trigger_data *hist_data,
4271 struct hist_trigger_data *hist_data_test)
4275 if (hist_data->n_actions != hist_data_test->n_actions)
4278 for (i = 0; i < hist_data->n_actions; i++) {
4279 struct action_data *data = hist_data->actions[i];
4280 struct action_data *data_test = hist_data_test->actions[i];
4281 char *action_name, *action_name_test;
4283 if (data->handler != data_test->handler)
4285 if (data->action != data_test->action)
4288 if (data->n_params != data_test->n_params)
4291 for (j = 0; j < data->n_params; j++) {
4292 if (strcmp(data->params[j], data_test->params[j]) != 0)
4296 if (data->use_trace_keyword)
4297 action_name = data->synth_event_name;
4299 action_name = data->action_name;
4301 if (data_test->use_trace_keyword)
4302 action_name_test = data_test->synth_event_name;
4304 action_name_test = data_test->action_name;
4306 if (strcmp(action_name, action_name_test) != 0)
4309 if (data->handler == HANDLER_ONMATCH) {
4310 if (strcmp(data->match_data.event_system,
4311 data_test->match_data.event_system) != 0)
4313 if (strcmp(data->match_data.event,
4314 data_test->match_data.event) != 0)
4316 } else if (data->handler == HANDLER_ONMAX ||
4317 data->handler == HANDLER_ONCHANGE) {
4318 if (strcmp(data->track_data.var_str,
4319 data_test->track_data.var_str) != 0)
4328 static void print_actions_spec(struct seq_file *m,
4329 struct hist_trigger_data *hist_data)
4333 for (i = 0; i < hist_data->n_actions; i++) {
4334 struct action_data *data = hist_data->actions[i];
4336 if (data->handler == HANDLER_ONMATCH)
4337 print_onmatch_spec(m, hist_data, data);
4338 else if (data->handler == HANDLER_ONMAX ||
4339 data->handler == HANDLER_ONCHANGE)
4340 print_track_data_spec(m, hist_data, data);
4344 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4348 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4349 kfree(hist_data->field_var_hists[i]->cmd);
4350 kfree(hist_data->field_var_hists[i]);
4354 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4359 destroy_hist_trigger_attrs(hist_data->attrs);
4360 destroy_hist_fields(hist_data);
4361 tracing_map_destroy(hist_data->map);
4363 destroy_actions(hist_data);
4364 destroy_field_vars(hist_data);
4365 destroy_field_var_hists(hist_data);
4370 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4372 struct tracing_map *map = hist_data->map;
4373 struct ftrace_event_field *field;
4374 struct hist_field *hist_field;
4377 for_each_hist_field(i, hist_data) {
4378 hist_field = hist_data->fields[i];
4379 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4380 tracing_map_cmp_fn_t cmp_fn;
4382 field = hist_field->field;
4384 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4385 cmp_fn = tracing_map_cmp_none;
4387 cmp_fn = tracing_map_cmp_num(hist_field->size,
4388 hist_field->is_signed);
4389 else if (is_string_field(field))
4390 cmp_fn = tracing_map_cmp_string;
4392 cmp_fn = tracing_map_cmp_num(field->size,
4394 idx = tracing_map_add_key_field(map,
4397 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4398 idx = tracing_map_add_sum_field(map);
4403 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4404 idx = tracing_map_add_var(map);
4407 hist_field->var.idx = idx;
4408 hist_field->var.hist_data = hist_data;
4415 static struct hist_trigger_data *
4416 create_hist_data(unsigned int map_bits,
4417 struct hist_trigger_attrs *attrs,
4418 struct trace_event_file *file,
4421 const struct tracing_map_ops *map_ops = NULL;
4422 struct hist_trigger_data *hist_data;
4425 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4427 return ERR_PTR(-ENOMEM);
4429 hist_data->attrs = attrs;
4430 hist_data->remove = remove;
4431 hist_data->event_file = file;
4433 ret = parse_actions(hist_data);
4437 ret = create_hist_fields(hist_data, file);
4441 ret = create_sort_keys(hist_data);
4445 map_ops = &hist_trigger_elt_data_ops;
4447 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4448 map_ops, hist_data);
4449 if (IS_ERR(hist_data->map)) {
4450 ret = PTR_ERR(hist_data->map);
4451 hist_data->map = NULL;
4455 ret = create_tracing_map_fields(hist_data);
4461 hist_data->attrs = NULL;
4463 destroy_hist_data(hist_data);
4465 hist_data = ERR_PTR(ret);
4470 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4471 struct tracing_map_elt *elt,
4472 struct trace_buffer *buffer, void *rec,
4473 struct ring_buffer_event *rbe,
4476 struct hist_elt_data *elt_data;
4477 struct hist_field *hist_field;
4478 unsigned int i, var_idx;
4481 elt_data = elt->private_data;
4482 elt_data->var_ref_vals = var_ref_vals;
4484 for_each_hist_val_field(i, hist_data) {
4485 hist_field = hist_data->fields[i];
4486 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec);
4487 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4488 var_idx = hist_field->var.idx;
4490 if (hist_field->flags & HIST_FIELD_FL_STRING) {
4491 unsigned int str_start, var_str_idx, idx;
4492 char *str, *val_str;
4494 str_start = hist_data->n_field_var_str +
4495 hist_data->n_save_var_str;
4496 var_str_idx = hist_field->var_str_idx;
4497 idx = str_start + var_str_idx;
4499 str = elt_data->field_var_str[idx];
4500 val_str = (char *)(uintptr_t)hist_val;
4501 strscpy(str, val_str, STR_VAR_LEN_MAX);
4503 hist_val = (u64)(uintptr_t)str;
4505 tracing_map_set_var(elt, var_idx, hist_val);
4508 tracing_map_update_sum(elt, i, hist_val);
4511 for_each_hist_key_field(i, hist_data) {
4512 hist_field = hist_data->fields[i];
4513 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4514 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec);
4515 var_idx = hist_field->var.idx;
4516 tracing_map_set_var(elt, var_idx, hist_val);
4520 update_field_vars(hist_data, elt, buffer, rbe, rec);
4523 static inline void add_to_key(char *compound_key, void *key,
4524 struct hist_field *key_field, void *rec)
4526 size_t size = key_field->size;
4528 if (key_field->flags & HIST_FIELD_FL_STRING) {
4529 struct ftrace_event_field *field;
4531 field = key_field->field;
4532 if (field->filter_type == FILTER_DYN_STRING)
4533 size = *(u32 *)(rec + field->offset) >> 16;
4534 else if (field->filter_type == FILTER_STATIC_STRING)
4537 /* ensure NULL-termination */
4538 if (size > key_field->size - 1)
4539 size = key_field->size - 1;
4541 strncpy(compound_key + key_field->offset, (char *)key, size);
4543 memcpy(compound_key + key_field->offset, key, size);
4547 hist_trigger_actions(struct hist_trigger_data *hist_data,
4548 struct tracing_map_elt *elt,
4549 struct trace_buffer *buffer, void *rec,
4550 struct ring_buffer_event *rbe, void *key,
4553 struct action_data *data;
4556 for (i = 0; i < hist_data->n_actions; i++) {
4557 data = hist_data->actions[i];
4558 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
4562 static void event_hist_trigger(struct event_trigger_data *data,
4563 struct trace_buffer *buffer, void *rec,
4564 struct ring_buffer_event *rbe)
4566 struct hist_trigger_data *hist_data = data->private_data;
4567 bool use_compound_key = (hist_data->n_keys > 1);
4568 unsigned long entries[HIST_STACKTRACE_DEPTH];
4569 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4570 char compound_key[HIST_KEY_SIZE_MAX];
4571 struct tracing_map_elt *elt = NULL;
4572 struct hist_field *key_field;
4577 memset(compound_key, 0, hist_data->key_size);
4579 for_each_hist_key_field(i, hist_data) {
4580 key_field = hist_data->fields[i];
4582 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4583 memset(entries, 0, HIST_STACKTRACE_SIZE);
4584 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
4585 HIST_STACKTRACE_SKIP);
4588 field_contents = key_field->fn(key_field, elt, buffer, rbe, rec);
4589 if (key_field->flags & HIST_FIELD_FL_STRING) {
4590 key = (void *)(unsigned long)field_contents;
4591 use_compound_key = true;
4593 key = (void *)&field_contents;
4596 if (use_compound_key)
4597 add_to_key(compound_key, key, key_field, rec);
4600 if (use_compound_key)
4603 if (hist_data->n_var_refs &&
4604 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4607 elt = tracing_map_insert(hist_data->map, key);
4611 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
4613 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4614 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
4617 static void hist_trigger_stacktrace_print(struct seq_file *m,
4618 unsigned long *stacktrace_entries,
4619 unsigned int max_entries)
4621 char str[KSYM_SYMBOL_LEN];
4622 unsigned int spaces = 8;
4625 for (i = 0; i < max_entries; i++) {
4626 if (!stacktrace_entries[i])
4629 seq_printf(m, "%*c", 1 + spaces, ' ');
4630 sprint_symbol(str, stacktrace_entries[i]);
4631 seq_printf(m, "%s\n", str);
4635 static void hist_trigger_print_key(struct seq_file *m,
4636 struct hist_trigger_data *hist_data,
4638 struct tracing_map_elt *elt)
4640 struct hist_field *key_field;
4641 char str[KSYM_SYMBOL_LEN];
4642 bool multiline = false;
4643 const char *field_name;
4649 for_each_hist_key_field(i, hist_data) {
4650 key_field = hist_data->fields[i];
4652 if (i > hist_data->n_vals)
4655 field_name = hist_field_name(key_field, 0);
4657 if (key_field->flags & HIST_FIELD_FL_HEX) {
4658 uval = *(u64 *)(key + key_field->offset);
4659 seq_printf(m, "%s: %llx", field_name, uval);
4660 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4661 uval = *(u64 *)(key + key_field->offset);
4662 sprint_symbol_no_offset(str, uval);
4663 seq_printf(m, "%s: [%llx] %-45s", field_name,
4665 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4666 uval = *(u64 *)(key + key_field->offset);
4667 sprint_symbol(str, uval);
4668 seq_printf(m, "%s: [%llx] %-55s", field_name,
4670 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4671 struct hist_elt_data *elt_data = elt->private_data;
4674 if (WARN_ON_ONCE(!elt_data))
4677 comm = elt_data->comm;
4679 uval = *(u64 *)(key + key_field->offset);
4680 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4682 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4683 const char *syscall_name;
4685 uval = *(u64 *)(key + key_field->offset);
4686 syscall_name = get_syscall_name(uval);
4688 syscall_name = "unknown_syscall";
4690 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4691 syscall_name, uval);
4692 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4693 seq_puts(m, "stacktrace:\n");
4694 hist_trigger_stacktrace_print(m,
4695 key + key_field->offset,
4696 HIST_STACKTRACE_DEPTH);
4698 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4699 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4700 *(u64 *)(key + key_field->offset));
4701 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4702 seq_printf(m, "%s: %-50s", field_name,
4703 (char *)(key + key_field->offset));
4705 uval = *(u64 *)(key + key_field->offset);
4706 seq_printf(m, "%s: %10llu", field_name, uval);
4716 static void hist_trigger_entry_print(struct seq_file *m,
4717 struct hist_trigger_data *hist_data,
4719 struct tracing_map_elt *elt)
4721 const char *field_name;
4724 hist_trigger_print_key(m, hist_data, key, elt);
4726 seq_printf(m, " hitcount: %10llu",
4727 tracing_map_read_sum(elt, HITCOUNT_IDX));
4729 for (i = 1; i < hist_data->n_vals; i++) {
4730 field_name = hist_field_name(hist_data->fields[i], 0);
4732 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4733 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4736 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4737 seq_printf(m, " %s: %10llx", field_name,
4738 tracing_map_read_sum(elt, i));
4740 seq_printf(m, " %s: %10llu", field_name,
4741 tracing_map_read_sum(elt, i));
4745 print_actions(m, hist_data, elt);
4750 static int print_entries(struct seq_file *m,
4751 struct hist_trigger_data *hist_data)
4753 struct tracing_map_sort_entry **sort_entries = NULL;
4754 struct tracing_map *map = hist_data->map;
4757 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4758 hist_data->n_sort_keys,
4763 for (i = 0; i < n_entries; i++)
4764 hist_trigger_entry_print(m, hist_data,
4765 sort_entries[i]->key,
4766 sort_entries[i]->elt);
4768 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4773 static void hist_trigger_show(struct seq_file *m,
4774 struct event_trigger_data *data, int n)
4776 struct hist_trigger_data *hist_data;
4780 seq_puts(m, "\n\n");
4782 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4783 data->ops->print(m, data->ops, data);
4784 seq_puts(m, "#\n\n");
4786 hist_data = data->private_data;
4787 n_entries = print_entries(m, hist_data);
4791 track_data_snapshot_print(m, hist_data);
4793 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4794 (u64)atomic64_read(&hist_data->map->hits),
4795 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4798 static int hist_show(struct seq_file *m, void *v)
4800 struct event_trigger_data *data;
4801 struct trace_event_file *event_file;
4804 mutex_lock(&event_mutex);
4806 event_file = event_file_data(m->private);
4807 if (unlikely(!event_file)) {
4812 list_for_each_entry(data, &event_file->triggers, list) {
4813 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4814 hist_trigger_show(m, data, n++);
4818 mutex_unlock(&event_mutex);
4823 static int event_hist_open(struct inode *inode, struct file *file)
4827 ret = security_locked_down(LOCKDOWN_TRACEFS);
4831 return single_open(file, hist_show, file);
4834 const struct file_operations event_hist_fops = {
4835 .open = event_hist_open,
4837 .llseek = seq_lseek,
4838 .release = single_release,
4841 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
4842 static void hist_field_debug_show_flags(struct seq_file *m,
4843 unsigned long flags)
4845 seq_puts(m, " flags:\n");
4847 if (flags & HIST_FIELD_FL_KEY)
4848 seq_puts(m, " HIST_FIELD_FL_KEY\n");
4849 else if (flags & HIST_FIELD_FL_HITCOUNT)
4850 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
4851 else if (flags & HIST_FIELD_FL_VAR)
4852 seq_puts(m, " HIST_FIELD_FL_VAR\n");
4853 else if (flags & HIST_FIELD_FL_VAR_REF)
4854 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
4856 seq_puts(m, " VAL: normal u64 value\n");
4858 if (flags & HIST_FIELD_FL_ALIAS)
4859 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
4862 static int hist_field_debug_show(struct seq_file *m,
4863 struct hist_field *field, unsigned long flags)
4865 if ((field->flags & flags) != flags) {
4866 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
4870 hist_field_debug_show_flags(m, field->flags);
4872 seq_printf(m, " ftrace_event_field name: %s\n",
4873 field->field->name);
4875 if (field->flags & HIST_FIELD_FL_VAR) {
4876 seq_printf(m, " var.name: %s\n", field->var.name);
4877 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4881 if (field->flags & HIST_FIELD_FL_ALIAS)
4882 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4883 field->var_ref_idx);
4885 if (field->flags & HIST_FIELD_FL_VAR_REF) {
4886 seq_printf(m, " name: %s\n", field->name);
4887 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4889 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
4890 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4891 field->var_ref_idx);
4893 seq_printf(m, " system: %s\n", field->system);
4894 if (field->event_name)
4895 seq_printf(m, " event_name: %s\n", field->event_name);
4898 seq_printf(m, " type: %s\n", field->type);
4899 seq_printf(m, " size: %u\n", field->size);
4900 seq_printf(m, " is_signed: %u\n", field->is_signed);
4905 static int field_var_debug_show(struct seq_file *m,
4906 struct field_var *field_var, unsigned int i,
4909 const char *vars_name = save_vars ? "save_vars" : "field_vars";
4910 struct hist_field *field;
4913 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
4915 field = field_var->var;
4917 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
4919 hist_field_debug_show_flags(m, field->flags);
4920 seq_printf(m, " var.name: %s\n", field->var.name);
4921 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4924 field = field_var->val;
4926 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
4928 seq_printf(m, " ftrace_event_field name: %s\n",
4929 field->field->name);
4935 seq_printf(m, " type: %s\n", field->type);
4936 seq_printf(m, " size: %u\n", field->size);
4937 seq_printf(m, " is_signed: %u\n", field->is_signed);
4942 static int hist_action_debug_show(struct seq_file *m,
4943 struct action_data *data, int i)
4947 if (data->handler == HANDLER_ONMAX ||
4948 data->handler == HANDLER_ONCHANGE) {
4949 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
4950 ret = hist_field_debug_show(m, data->track_data.var_ref,
4951 HIST_FIELD_FL_VAR_REF);
4955 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
4956 ret = hist_field_debug_show(m, data->track_data.track_var,
4962 if (data->handler == HANDLER_ONMATCH) {
4963 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
4964 i, data->match_data.event_system);
4965 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
4966 i, data->match_data.event);
4972 static int hist_actions_debug_show(struct seq_file *m,
4973 struct hist_trigger_data *hist_data)
4977 if (hist_data->n_actions)
4978 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
4980 for (i = 0; i < hist_data->n_actions; i++) {
4981 struct action_data *action = hist_data->actions[i];
4983 ret = hist_action_debug_show(m, action, i);
4988 if (hist_data->n_save_vars)
4989 seq_puts(m, "\n save action variables (save() params):\n");
4991 for (i = 0; i < hist_data->n_save_vars; i++) {
4992 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
5000 static void hist_trigger_debug_show(struct seq_file *m,
5001 struct event_trigger_data *data, int n)
5003 struct hist_trigger_data *hist_data;
5007 seq_puts(m, "\n\n");
5009 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5010 data->ops->print(m, data->ops, data);
5011 seq_puts(m, "#\n\n");
5013 hist_data = data->private_data;
5015 seq_printf(m, "hist_data: %p\n\n", hist_data);
5016 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
5017 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
5018 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
5020 seq_puts(m, "\n val fields:\n\n");
5022 seq_puts(m, " hist_data->fields[0]:\n");
5023 ret = hist_field_debug_show(m, hist_data->fields[0],
5024 HIST_FIELD_FL_HITCOUNT);
5028 for (i = 1; i < hist_data->n_vals; i++) {
5029 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5030 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5035 seq_puts(m, "\n key fields:\n");
5037 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5038 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5039 ret = hist_field_debug_show(m, hist_data->fields[i],
5045 if (hist_data->n_var_refs)
5046 seq_puts(m, "\n variable reference fields:\n");
5048 for (i = 0; i < hist_data->n_var_refs; i++) {
5049 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5050 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5051 HIST_FIELD_FL_VAR_REF);
5056 if (hist_data->n_field_vars)
5057 seq_puts(m, "\n field variables:\n");
5059 for (i = 0; i < hist_data->n_field_vars; i++) {
5060 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5065 ret = hist_actions_debug_show(m, hist_data);
5070 static int hist_debug_show(struct seq_file *m, void *v)
5072 struct event_trigger_data *data;
5073 struct trace_event_file *event_file;
5076 mutex_lock(&event_mutex);
5078 event_file = event_file_data(m->private);
5079 if (unlikely(!event_file)) {
5084 list_for_each_entry(data, &event_file->triggers, list) {
5085 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5086 hist_trigger_debug_show(m, data, n++);
5090 mutex_unlock(&event_mutex);
5095 static int event_hist_debug_open(struct inode *inode, struct file *file)
5099 ret = security_locked_down(LOCKDOWN_TRACEFS);
5103 return single_open(file, hist_debug_show, file);
5106 const struct file_operations event_hist_debug_fops = {
5107 .open = event_hist_debug_open,
5109 .llseek = seq_lseek,
5110 .release = single_release,
5114 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5116 const char *field_name = hist_field_name(hist_field, 0);
5118 if (hist_field->var.name)
5119 seq_printf(m, "%s=", hist_field->var.name);
5121 if (hist_field->flags & HIST_FIELD_FL_CPU)
5122 seq_puts(m, "common_cpu");
5123 else if (field_name) {
5124 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5125 hist_field->flags & HIST_FIELD_FL_ALIAS)
5127 seq_printf(m, "%s", field_name);
5128 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5129 seq_puts(m, "common_timestamp");
5131 if (hist_field->flags) {
5132 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5133 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5134 const char *flags = get_hist_field_flags(hist_field);
5137 seq_printf(m, ".%s", flags);
5142 static int event_hist_trigger_print(struct seq_file *m,
5143 struct event_trigger_ops *ops,
5144 struct event_trigger_data *data)
5146 struct hist_trigger_data *hist_data = data->private_data;
5147 struct hist_field *field;
5148 bool have_var = false;
5151 seq_puts(m, "hist:");
5154 seq_printf(m, "%s:", data->name);
5156 seq_puts(m, "keys=");
5158 for_each_hist_key_field(i, hist_data) {
5159 field = hist_data->fields[i];
5161 if (i > hist_data->n_vals)
5164 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5165 seq_puts(m, "stacktrace");
5167 hist_field_print(m, field);
5170 seq_puts(m, ":vals=");
5172 for_each_hist_val_field(i, hist_data) {
5173 field = hist_data->fields[i];
5174 if (field->flags & HIST_FIELD_FL_VAR) {
5179 if (i == HITCOUNT_IDX)
5180 seq_puts(m, "hitcount");
5183 hist_field_print(m, field);
5192 for_each_hist_val_field(i, hist_data) {
5193 field = hist_data->fields[i];
5195 if (field->flags & HIST_FIELD_FL_VAR) {
5198 hist_field_print(m, field);
5203 seq_puts(m, ":sort=");
5205 for (i = 0; i < hist_data->n_sort_keys; i++) {
5206 struct tracing_map_sort_key *sort_key;
5207 unsigned int idx, first_key_idx;
5210 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5212 sort_key = &hist_data->sort_keys[i];
5213 idx = sort_key->field_idx;
5215 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5221 if (idx == HITCOUNT_IDX)
5222 seq_puts(m, "hitcount");
5224 if (idx >= first_key_idx)
5225 idx += hist_data->n_vars;
5226 hist_field_print(m, hist_data->fields[idx]);
5229 if (sort_key->descending)
5230 seq_puts(m, ".descending");
5232 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5233 if (hist_data->enable_timestamps)
5234 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5236 print_actions_spec(m, hist_data);
5238 if (data->filter_str)
5239 seq_printf(m, " if %s", data->filter_str);
5242 seq_puts(m, " [paused]");
5244 seq_puts(m, " [active]");
5251 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5252 struct event_trigger_data *data)
5254 struct hist_trigger_data *hist_data = data->private_data;
5256 if (!data->ref && hist_data->attrs->name)
5257 save_named_trigger(hist_data->attrs->name, data);
5264 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5266 struct trace_event_file *file;
5271 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5272 file = hist_data->field_var_hists[i]->hist_data->event_file;
5273 cmd = hist_data->field_var_hists[i]->cmd;
5274 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5275 "!hist", "hist", cmd);
5279 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5280 struct event_trigger_data *data)
5282 struct hist_trigger_data *hist_data = data->private_data;
5284 if (WARN_ON_ONCE(data->ref <= 0))
5290 del_named_trigger(data);
5292 trigger_data_free(data);
5294 remove_hist_vars(hist_data);
5296 unregister_field_var_hists(hist_data);
5298 destroy_hist_data(hist_data);
5302 static struct event_trigger_ops event_hist_trigger_ops = {
5303 .func = event_hist_trigger,
5304 .print = event_hist_trigger_print,
5305 .init = event_hist_trigger_init,
5306 .free = event_hist_trigger_free,
5309 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5310 struct event_trigger_data *data)
5314 save_named_trigger(data->named_data->name, data);
5316 event_hist_trigger_init(ops, data->named_data);
5321 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5322 struct event_trigger_data *data)
5324 if (WARN_ON_ONCE(data->ref <= 0))
5327 event_hist_trigger_free(ops, data->named_data);
5331 del_named_trigger(data);
5332 trigger_data_free(data);
5336 static struct event_trigger_ops event_hist_trigger_named_ops = {
5337 .func = event_hist_trigger,
5338 .print = event_hist_trigger_print,
5339 .init = event_hist_trigger_named_init,
5340 .free = event_hist_trigger_named_free,
5343 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5346 return &event_hist_trigger_ops;
5349 static void hist_clear(struct event_trigger_data *data)
5351 struct hist_trigger_data *hist_data = data->private_data;
5354 pause_named_trigger(data);
5356 tracepoint_synchronize_unregister();
5358 tracing_map_clear(hist_data->map);
5361 unpause_named_trigger(data);
5364 static bool compatible_field(struct ftrace_event_field *field,
5365 struct ftrace_event_field *test_field)
5367 if (field == test_field)
5369 if (field == NULL || test_field == NULL)
5371 if (strcmp(field->name, test_field->name) != 0)
5373 if (strcmp(field->type, test_field->type) != 0)
5375 if (field->size != test_field->size)
5377 if (field->is_signed != test_field->is_signed)
5383 static bool hist_trigger_match(struct event_trigger_data *data,
5384 struct event_trigger_data *data_test,
5385 struct event_trigger_data *named_data,
5388 struct tracing_map_sort_key *sort_key, *sort_key_test;
5389 struct hist_trigger_data *hist_data, *hist_data_test;
5390 struct hist_field *key_field, *key_field_test;
5393 if (named_data && (named_data != data_test) &&
5394 (named_data != data_test->named_data))
5397 if (!named_data && is_named_trigger(data_test))
5400 hist_data = data->private_data;
5401 hist_data_test = data_test->private_data;
5403 if (hist_data->n_vals != hist_data_test->n_vals ||
5404 hist_data->n_fields != hist_data_test->n_fields ||
5405 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5408 if (!ignore_filter) {
5409 if ((data->filter_str && !data_test->filter_str) ||
5410 (!data->filter_str && data_test->filter_str))
5414 for_each_hist_field(i, hist_data) {
5415 key_field = hist_data->fields[i];
5416 key_field_test = hist_data_test->fields[i];
5418 if (key_field->flags != key_field_test->flags)
5420 if (!compatible_field(key_field->field, key_field_test->field))
5422 if (key_field->offset != key_field_test->offset)
5424 if (key_field->size != key_field_test->size)
5426 if (key_field->is_signed != key_field_test->is_signed)
5428 if (!!key_field->var.name != !!key_field_test->var.name)
5430 if (key_field->var.name &&
5431 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5435 for (i = 0; i < hist_data->n_sort_keys; i++) {
5436 sort_key = &hist_data->sort_keys[i];
5437 sort_key_test = &hist_data_test->sort_keys[i];
5439 if (sort_key->field_idx != sort_key_test->field_idx ||
5440 sort_key->descending != sort_key_test->descending)
5444 if (!ignore_filter && data->filter_str &&
5445 (strcmp(data->filter_str, data_test->filter_str) != 0))
5448 if (!actions_match(hist_data, hist_data_test))
5454 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5455 struct event_trigger_data *data,
5456 struct trace_event_file *file)
5458 struct hist_trigger_data *hist_data = data->private_data;
5459 struct event_trigger_data *test, *named_data = NULL;
5460 struct trace_array *tr = file->tr;
5463 if (hist_data->attrs->name) {
5464 named_data = find_named_trigger(hist_data->attrs->name);
5466 if (!hist_trigger_match(data, named_data, named_data,
5468 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5475 if (hist_data->attrs->name && !named_data)
5478 lockdep_assert_held(&event_mutex);
5480 list_for_each_entry(test, &file->triggers, list) {
5481 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5482 if (!hist_trigger_match(data, test, named_data, false))
5484 if (hist_data->attrs->pause)
5485 test->paused = true;
5486 else if (hist_data->attrs->cont)
5487 test->paused = false;
5488 else if (hist_data->attrs->clear)
5491 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5498 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5499 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5504 if (hist_data->attrs->pause)
5505 data->paused = true;
5508 data->private_data = named_data->private_data;
5509 set_named_trigger_data(data, named_data);
5510 data->ops = &event_hist_trigger_named_ops;
5513 if (data->ops->init) {
5514 ret = data->ops->init(data->ops, data);
5519 if (hist_data->enable_timestamps) {
5520 char *clock = hist_data->attrs->clock;
5522 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5524 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5528 tracing_set_filter_buffering(file->tr, true);
5532 destroy_hist_data(hist_data);
5539 static int hist_trigger_enable(struct event_trigger_data *data,
5540 struct trace_event_file *file)
5544 list_add_tail_rcu(&data->list, &file->triggers);
5546 update_cond_flag(file);
5548 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5549 list_del_rcu(&data->list);
5550 update_cond_flag(file);
5557 static bool have_hist_trigger_match(struct event_trigger_data *data,
5558 struct trace_event_file *file)
5560 struct hist_trigger_data *hist_data = data->private_data;
5561 struct event_trigger_data *test, *named_data = NULL;
5564 lockdep_assert_held(&event_mutex);
5566 if (hist_data->attrs->name)
5567 named_data = find_named_trigger(hist_data->attrs->name);
5569 list_for_each_entry(test, &file->triggers, list) {
5570 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5571 if (hist_trigger_match(data, test, named_data, false)) {
5581 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5582 struct trace_event_file *file)
5584 struct hist_trigger_data *hist_data = data->private_data;
5585 struct event_trigger_data *test, *named_data = NULL;
5587 lockdep_assert_held(&event_mutex);
5589 if (hist_data->attrs->name)
5590 named_data = find_named_trigger(hist_data->attrs->name);
5592 list_for_each_entry(test, &file->triggers, list) {
5593 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5594 if (!hist_trigger_match(data, test, named_data, false))
5596 hist_data = test->private_data;
5597 if (check_var_refs(hist_data))
5606 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5607 struct event_trigger_data *data,
5608 struct trace_event_file *file)
5610 struct hist_trigger_data *hist_data = data->private_data;
5611 struct event_trigger_data *test, *named_data = NULL;
5612 bool unregistered = false;
5614 lockdep_assert_held(&event_mutex);
5616 if (hist_data->attrs->name)
5617 named_data = find_named_trigger(hist_data->attrs->name);
5619 list_for_each_entry(test, &file->triggers, list) {
5620 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5621 if (!hist_trigger_match(data, test, named_data, false))
5623 unregistered = true;
5624 list_del_rcu(&test->list);
5625 trace_event_trigger_enable_disable(file, 0);
5626 update_cond_flag(file);
5631 if (unregistered && test->ops->free)
5632 test->ops->free(test->ops, test);
5634 if (hist_data->enable_timestamps) {
5635 if (!hist_data->remove || unregistered)
5636 tracing_set_filter_buffering(file->tr, false);
5640 static bool hist_file_check_refs(struct trace_event_file *file)
5642 struct hist_trigger_data *hist_data;
5643 struct event_trigger_data *test;
5645 lockdep_assert_held(&event_mutex);
5647 list_for_each_entry(test, &file->triggers, list) {
5648 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5649 hist_data = test->private_data;
5650 if (check_var_refs(hist_data))
5658 static void hist_unreg_all(struct trace_event_file *file)
5660 struct event_trigger_data *test, *n;
5661 struct hist_trigger_data *hist_data;
5662 struct synth_event *se;
5663 const char *se_name;
5665 lockdep_assert_held(&event_mutex);
5667 if (hist_file_check_refs(file))
5670 list_for_each_entry_safe(test, n, &file->triggers, list) {
5671 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5672 hist_data = test->private_data;
5673 list_del_rcu(&test->list);
5674 trace_event_trigger_enable_disable(file, 0);
5676 se_name = trace_event_name(file->event_call);
5677 se = find_synth_event(se_name);
5681 update_cond_flag(file);
5682 if (hist_data->enable_timestamps)
5683 tracing_set_filter_buffering(file->tr, false);
5684 if (test->ops->free)
5685 test->ops->free(test->ops, test);
5690 static int event_hist_trigger_func(struct event_command *cmd_ops,
5691 struct trace_event_file *file,
5692 char *glob, char *cmd, char *param)
5694 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5695 struct event_trigger_data *trigger_data;
5696 struct hist_trigger_attrs *attrs;
5697 struct event_trigger_ops *trigger_ops;
5698 struct hist_trigger_data *hist_data;
5699 struct synth_event *se;
5700 const char *se_name;
5701 bool remove = false;
5705 lockdep_assert_held(&event_mutex);
5707 if (glob && strlen(glob)) {
5709 last_cmd_set(file, param);
5719 * separate the trigger from the filter (k:v [if filter])
5720 * allowing for whitespace in the trigger
5722 p = trigger = param;
5724 p = strstr(p, "if");
5729 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5733 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
5735 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
5746 param = strstrip(p);
5747 trigger = strstrip(trigger);
5750 attrs = parse_hist_trigger_attrs(file->tr, trigger);
5752 return PTR_ERR(attrs);
5754 if (attrs->map_bits)
5755 hist_trigger_bits = attrs->map_bits;
5757 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5758 if (IS_ERR(hist_data)) {
5759 destroy_hist_trigger_attrs(attrs);
5760 return PTR_ERR(hist_data);
5763 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5765 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5766 if (!trigger_data) {
5771 trigger_data->count = -1;
5772 trigger_data->ops = trigger_ops;
5773 trigger_data->cmd_ops = cmd_ops;
5775 INIT_LIST_HEAD(&trigger_data->list);
5776 RCU_INIT_POINTER(trigger_data->filter, NULL);
5778 trigger_data->private_data = hist_data;
5780 /* if param is non-empty, it's supposed to be a filter */
5781 if (param && cmd_ops->set_filter) {
5782 ret = cmd_ops->set_filter(param, trigger_data, file);
5788 if (!have_hist_trigger_match(trigger_data, file))
5791 if (hist_trigger_check_refs(trigger_data, file)) {
5796 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5797 se_name = trace_event_name(file->event_call);
5798 se = find_synth_event(se_name);
5805 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5807 * The above returns on success the # of triggers registered,
5808 * but if it didn't register any it returns zero. Consider no
5809 * triggers registered a failure too.
5812 if (!(attrs->pause || attrs->cont || attrs->clear))
5818 if (get_named_trigger_data(trigger_data))
5821 if (has_hist_vars(hist_data))
5822 save_hist_vars(hist_data);
5824 ret = create_actions(hist_data);
5828 ret = tracing_map_init(hist_data->map);
5832 ret = hist_trigger_enable(trigger_data, file);
5836 se_name = trace_event_name(file->event_call);
5837 se = find_synth_event(se_name);
5840 /* Just return zero, not the number of registered triggers */
5848 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5850 if (cmd_ops->set_filter)
5851 cmd_ops->set_filter(NULL, trigger_data, NULL);
5853 remove_hist_vars(hist_data);
5855 kfree(trigger_data);
5857 destroy_hist_data(hist_data);
5861 static struct event_command trigger_hist_cmd = {
5863 .trigger_type = ETT_EVENT_HIST,
5864 .flags = EVENT_CMD_FL_NEEDS_REC,
5865 .func = event_hist_trigger_func,
5866 .reg = hist_register_trigger,
5867 .unreg = hist_unregister_trigger,
5868 .unreg_all = hist_unreg_all,
5869 .get_trigger_ops = event_hist_get_trigger_ops,
5870 .set_filter = set_trigger_filter,
5873 __init int register_trigger_hist_cmd(void)
5877 ret = register_event_command(&trigger_hist_cmd);
5884 hist_enable_trigger(struct event_trigger_data *data,
5885 struct trace_buffer *buffer, void *rec,
5886 struct ring_buffer_event *event)
5888 struct enable_trigger_data *enable_data = data->private_data;
5889 struct event_trigger_data *test;
5891 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
5892 lockdep_is_held(&event_mutex)) {
5893 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5894 if (enable_data->enable)
5895 test->paused = false;
5897 test->paused = true;
5903 hist_enable_count_trigger(struct event_trigger_data *data,
5904 struct trace_buffer *buffer, void *rec,
5905 struct ring_buffer_event *event)
5910 if (data->count != -1)
5913 hist_enable_trigger(data, buffer, rec, event);
5916 static struct event_trigger_ops hist_enable_trigger_ops = {
5917 .func = hist_enable_trigger,
5918 .print = event_enable_trigger_print,
5919 .init = event_trigger_init,
5920 .free = event_enable_trigger_free,
5923 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5924 .func = hist_enable_count_trigger,
5925 .print = event_enable_trigger_print,
5926 .init = event_trigger_init,
5927 .free = event_enable_trigger_free,
5930 static struct event_trigger_ops hist_disable_trigger_ops = {
5931 .func = hist_enable_trigger,
5932 .print = event_enable_trigger_print,
5933 .init = event_trigger_init,
5934 .free = event_enable_trigger_free,
5937 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5938 .func = hist_enable_count_trigger,
5939 .print = event_enable_trigger_print,
5940 .init = event_trigger_init,
5941 .free = event_enable_trigger_free,
5944 static struct event_trigger_ops *
5945 hist_enable_get_trigger_ops(char *cmd, char *param)
5947 struct event_trigger_ops *ops;
5950 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5953 ops = param ? &hist_enable_count_trigger_ops :
5954 &hist_enable_trigger_ops;
5956 ops = param ? &hist_disable_count_trigger_ops :
5957 &hist_disable_trigger_ops;
5962 static void hist_enable_unreg_all(struct trace_event_file *file)
5964 struct event_trigger_data *test, *n;
5966 list_for_each_entry_safe(test, n, &file->triggers, list) {
5967 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5968 list_del_rcu(&test->list);
5969 update_cond_flag(file);
5970 trace_event_trigger_enable_disable(file, 0);
5971 if (test->ops->free)
5972 test->ops->free(test->ops, test);
5977 static struct event_command trigger_hist_enable_cmd = {
5978 .name = ENABLE_HIST_STR,
5979 .trigger_type = ETT_HIST_ENABLE,
5980 .func = event_enable_trigger_func,
5981 .reg = event_enable_register_trigger,
5982 .unreg = event_enable_unregister_trigger,
5983 .unreg_all = hist_enable_unreg_all,
5984 .get_trigger_ops = hist_enable_get_trigger_ops,
5985 .set_filter = set_trigger_filter,
5988 static struct event_command trigger_hist_disable_cmd = {
5989 .name = DISABLE_HIST_STR,
5990 .trigger_type = ETT_HIST_ENABLE,
5991 .func = event_enable_trigger_func,
5992 .reg = event_enable_register_trigger,
5993 .unreg = event_enable_unregister_trigger,
5994 .unreg_all = hist_enable_unreg_all,
5995 .get_trigger_ops = hist_enable_get_trigger_ops,
5996 .set_filter = set_trigger_filter,
5999 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6001 unregister_event_command(&trigger_hist_enable_cmd);
6002 unregister_event_command(&trigger_hist_disable_cmd);
6005 __init int register_trigger_hist_enable_disable_cmds(void)
6009 ret = register_event_command(&trigger_hist_enable_cmd);
6010 if (WARN_ON(ret < 0))
6012 ret = register_event_command(&trigger_hist_disable_cmd);
6013 if (WARN_ON(ret < 0))
6014 unregister_trigger_hist_enable_disable_cmds();