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 ring_buffer_event *rbe,
88 #define HIST_FIELD_OPERANDS_MAX 2
89 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
90 #define HIST_ACTIONS_MAX 8
100 * A hist_var (histogram variable) contains variable information for
101 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
102 * flag set. A hist_var has a variable name e.g. ts0, and is
103 * associated with a given histogram trigger, as specified by
104 * hist_data. The hist_var idx is the unique index assigned to the
105 * variable by the hist trigger's tracing_map. The idx is what is
106 * used to set a variable's value and, by a variable reference, to
111 struct hist_trigger_data *hist_data;
116 struct ftrace_event_field *field;
122 unsigned int is_signed;
124 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
125 struct hist_trigger_data *hist_data;
128 * Variable fields contain variable-specific info in var.
131 enum field_op_id operator;
136 * The name field is used for EXPR and VAR_REF fields. VAR
137 * fields contain the variable name in var.name.
142 * When a histogram trigger is hit, if it has any references
143 * to variables, the values of those variables are collected
144 * into a var_ref_vals array by resolve_var_refs(). The
145 * current value of each variable is read from the tracing_map
146 * using the hist field's hist_var.idx and entered into the
147 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
149 unsigned int var_ref_idx;
152 unsigned int var_str_idx;
155 static u64 hist_field_none(struct hist_field *field,
156 struct tracing_map_elt *elt,
157 struct ring_buffer_event *rbe,
163 static u64 hist_field_counter(struct hist_field *field,
164 struct tracing_map_elt *elt,
165 struct ring_buffer_event *rbe,
171 static u64 hist_field_string(struct hist_field *hist_field,
172 struct tracing_map_elt *elt,
173 struct ring_buffer_event *rbe,
176 char *addr = (char *)(event + hist_field->field->offset);
178 return (u64)(unsigned long)addr;
181 static u64 hist_field_dynstring(struct hist_field *hist_field,
182 struct tracing_map_elt *elt,
183 struct ring_buffer_event *rbe,
186 u32 str_item = *(u32 *)(event + hist_field->field->offset);
187 int str_loc = str_item & 0xffff;
188 char *addr = (char *)(event + str_loc);
190 return (u64)(unsigned long)addr;
193 static u64 hist_field_pstring(struct hist_field *hist_field,
194 struct tracing_map_elt *elt,
195 struct ring_buffer_event *rbe,
198 char **addr = (char **)(event + hist_field->field->offset);
200 return (u64)(unsigned long)*addr;
203 static u64 hist_field_log2(struct hist_field *hist_field,
204 struct tracing_map_elt *elt,
205 struct ring_buffer_event *rbe,
208 struct hist_field *operand = hist_field->operands[0];
210 u64 val = operand->fn(operand, elt, rbe, event);
212 return (u64) ilog2(roundup_pow_of_two(val));
215 static u64 hist_field_plus(struct hist_field *hist_field,
216 struct tracing_map_elt *elt,
217 struct ring_buffer_event *rbe,
220 struct hist_field *operand1 = hist_field->operands[0];
221 struct hist_field *operand2 = hist_field->operands[1];
223 u64 val1 = operand1->fn(operand1, elt, rbe, event);
224 u64 val2 = operand2->fn(operand2, elt, rbe, event);
229 static u64 hist_field_minus(struct hist_field *hist_field,
230 struct tracing_map_elt *elt,
231 struct ring_buffer_event *rbe,
234 struct hist_field *operand1 = hist_field->operands[0];
235 struct hist_field *operand2 = hist_field->operands[1];
237 u64 val1 = operand1->fn(operand1, elt, rbe, event);
238 u64 val2 = operand2->fn(operand2, elt, rbe, event);
243 static u64 hist_field_unary_minus(struct hist_field *hist_field,
244 struct tracing_map_elt *elt,
245 struct ring_buffer_event *rbe,
248 struct hist_field *operand = hist_field->operands[0];
250 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
251 u64 val = (u64)-sval;
256 #define DEFINE_HIST_FIELD_FN(type) \
257 static u64 hist_field_##type(struct hist_field *hist_field, \
258 struct tracing_map_elt *elt, \
259 struct ring_buffer_event *rbe, \
262 type *addr = (type *)(event + hist_field->field->offset); \
264 return (u64)(unsigned long)*addr; \
267 DEFINE_HIST_FIELD_FN(s64);
268 DEFINE_HIST_FIELD_FN(u64);
269 DEFINE_HIST_FIELD_FN(s32);
270 DEFINE_HIST_FIELD_FN(u32);
271 DEFINE_HIST_FIELD_FN(s16);
272 DEFINE_HIST_FIELD_FN(u16);
273 DEFINE_HIST_FIELD_FN(s8);
274 DEFINE_HIST_FIELD_FN(u8);
276 #define for_each_hist_field(i, hist_data) \
277 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
279 #define for_each_hist_val_field(i, hist_data) \
280 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
282 #define for_each_hist_key_field(i, hist_data) \
283 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
285 #define HIST_STACKTRACE_DEPTH 16
286 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
287 #define HIST_STACKTRACE_SKIP 5
289 #define HITCOUNT_IDX 0
290 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
292 enum hist_field_flags {
293 HIST_FIELD_FL_HITCOUNT = 1 << 0,
294 HIST_FIELD_FL_KEY = 1 << 1,
295 HIST_FIELD_FL_STRING = 1 << 2,
296 HIST_FIELD_FL_HEX = 1 << 3,
297 HIST_FIELD_FL_SYM = 1 << 4,
298 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
299 HIST_FIELD_FL_EXECNAME = 1 << 6,
300 HIST_FIELD_FL_SYSCALL = 1 << 7,
301 HIST_FIELD_FL_STACKTRACE = 1 << 8,
302 HIST_FIELD_FL_LOG2 = 1 << 9,
303 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
304 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
305 HIST_FIELD_FL_VAR = 1 << 12,
306 HIST_FIELD_FL_EXPR = 1 << 13,
307 HIST_FIELD_FL_VAR_REF = 1 << 14,
308 HIST_FIELD_FL_CPU = 1 << 15,
309 HIST_FIELD_FL_ALIAS = 1 << 16,
314 char *name[TRACING_MAP_VARS_MAX];
315 char *expr[TRACING_MAP_VARS_MAX];
318 struct hist_trigger_attrs {
328 unsigned int map_bits;
330 char *assignment_str[TRACING_MAP_VARS_MAX];
331 unsigned int n_assignments;
333 char *action_str[HIST_ACTIONS_MAX];
334 unsigned int n_actions;
336 struct var_defs var_defs;
340 struct hist_field *var;
341 struct hist_field *val;
344 struct field_var_hist {
345 struct hist_trigger_data *hist_data;
349 struct hist_trigger_data {
350 struct hist_field *fields[HIST_FIELDS_MAX];
353 unsigned int n_fields;
355 unsigned int n_var_str;
356 unsigned int key_size;
357 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
358 unsigned int n_sort_keys;
359 struct trace_event_file *event_file;
360 struct hist_trigger_attrs *attrs;
361 struct tracing_map *map;
362 bool enable_timestamps;
364 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
365 unsigned int n_var_refs;
367 struct action_data *actions[HIST_ACTIONS_MAX];
368 unsigned int n_actions;
370 struct field_var *field_vars[SYNTH_FIELDS_MAX];
371 unsigned int n_field_vars;
372 unsigned int n_field_var_str;
373 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
374 unsigned int n_field_var_hists;
376 struct field_var *save_vars[SYNTH_FIELDS_MAX];
377 unsigned int n_save_vars;
378 unsigned int n_save_var_str;
383 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
384 struct tracing_map_elt *elt, void *rec,
385 struct ring_buffer_event *rbe, void *key,
386 struct action_data *data, u64 *var_ref_vals);
388 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
403 enum handler_id handler;
404 enum action_id action;
408 unsigned int n_params;
409 char *params[SYNTH_FIELDS_MAX];
412 * When a histogram trigger is hit, the values of any
413 * references to variables, including variables being passed
414 * as parameters to synthetic events, are collected into a
415 * var_ref_vals array. This var_ref_idx array is an array of
416 * indices into the var_ref_vals array, one for each synthetic
417 * event param, and is passed to the synthetic event
420 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
421 struct synth_event *synth_event;
422 bool use_trace_keyword;
423 char *synth_event_name;
433 * var_str contains the $-unstripped variable
434 * name referenced by var_ref, and used when
435 * printing the action. Because var_ref
436 * creation is deferred to create_actions(),
437 * we need a per-action way to save it until
438 * then, thus var_str.
443 * var_ref refers to the variable being
444 * tracked e.g onmax($var).
446 struct hist_field *var_ref;
449 * track_var contains the 'invisible' tracking
450 * variable created to keep the current
453 struct hist_field *track_var;
455 check_track_val_fn_t check_val;
456 action_fn_t save_data;
465 unsigned int key_len;
467 struct tracing_map_elt elt;
469 struct action_data *action_data;
470 struct hist_trigger_data *hist_data;
473 struct hist_elt_data {
476 char *field_var_str[SYNTH_FIELDS_MAX];
479 struct snapshot_context {
480 struct tracing_map_elt *elt;
484 static void track_data_free(struct track_data *track_data)
486 struct hist_elt_data *elt_data;
491 kfree(track_data->key);
493 elt_data = track_data->elt.private_data;
495 kfree(elt_data->comm);
502 static struct track_data *track_data_alloc(unsigned int key_len,
503 struct action_data *action_data,
504 struct hist_trigger_data *hist_data)
506 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
507 struct hist_elt_data *elt_data;
510 return ERR_PTR(-ENOMEM);
512 data->key = kzalloc(key_len, GFP_KERNEL);
514 track_data_free(data);
515 return ERR_PTR(-ENOMEM);
518 data->key_len = key_len;
519 data->action_data = action_data;
520 data->hist_data = hist_data;
522 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
524 track_data_free(data);
525 return ERR_PTR(-ENOMEM);
528 data->elt.private_data = elt_data;
530 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
531 if (!elt_data->comm) {
532 track_data_free(data);
533 return ERR_PTR(-ENOMEM);
539 static char last_cmd[MAX_FILTER_STR_VAL];
540 static char last_cmd_loc[MAX_FILTER_STR_VAL];
542 static int errpos(char *str)
544 return err_pos(last_cmd, str);
547 static void last_cmd_set(struct trace_event_file *file, char *str)
549 const char *system = NULL, *name = NULL;
550 struct trace_event_call *call;
555 strcpy(last_cmd, "hist:");
556 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
559 call = file->event_call;
560 system = call->class->system;
562 name = trace_event_name(call);
569 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
572 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
574 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
578 static void hist_err_clear(void)
581 last_cmd_loc[0] = '\0';
584 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
585 unsigned int *var_ref_idx);
587 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
588 unsigned int *var_ref_idx)
590 struct tracepoint *tp = event->tp;
592 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
593 struct tracepoint_func *probe_func_ptr;
594 synth_probe_func_t probe_func;
597 if (!(cpu_online(raw_smp_processor_id())))
600 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
601 if (probe_func_ptr) {
603 probe_func = probe_func_ptr->func;
604 __data = probe_func_ptr->data;
605 probe_func(__data, var_ref_vals, var_ref_idx);
606 } while ((++probe_func_ptr)->func);
611 static void action_trace(struct hist_trigger_data *hist_data,
612 struct tracing_map_elt *elt, void *rec,
613 struct ring_buffer_event *rbe, void *key,
614 struct action_data *data, u64 *var_ref_vals)
616 struct synth_event *event = data->synth_event;
618 trace_synth(event, var_ref_vals, data->var_ref_idx);
621 struct hist_var_data {
622 struct list_head list;
623 struct hist_trigger_data *hist_data;
626 static u64 hist_field_timestamp(struct hist_field *hist_field,
627 struct tracing_map_elt *elt,
628 struct ring_buffer_event *rbe,
631 struct hist_trigger_data *hist_data = hist_field->hist_data;
632 struct trace_array *tr = hist_data->event_file->tr;
634 u64 ts = ring_buffer_event_time_stamp(rbe);
636 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
642 static u64 hist_field_cpu(struct hist_field *hist_field,
643 struct tracing_map_elt *elt,
644 struct ring_buffer_event *rbe,
647 int cpu = smp_processor_id();
653 * check_field_for_var_ref - Check if a VAR_REF field references a variable
654 * @hist_field: The VAR_REF field to check
655 * @var_data: The hist trigger that owns the variable
656 * @var_idx: The trigger variable identifier
658 * Check the given VAR_REF field to see whether or not it references
659 * the given variable associated with the given trigger.
661 * Return: The VAR_REF field if it does reference the variable, NULL if not
663 static struct hist_field *
664 check_field_for_var_ref(struct hist_field *hist_field,
665 struct hist_trigger_data *var_data,
666 unsigned int var_idx)
668 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
670 if (hist_field && hist_field->var.idx == var_idx &&
671 hist_field->var.hist_data == var_data)
678 * find_var_ref - Check if a trigger has a reference to a trigger variable
679 * @hist_data: The hist trigger that might have a reference to the variable
680 * @var_data: The hist trigger that owns the variable
681 * @var_idx: The trigger variable identifier
683 * Check the list of var_refs[] on the first hist trigger to see
684 * whether any of them are references to the variable on the second
687 * Return: The VAR_REF field referencing the variable if so, NULL if not
689 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
690 struct hist_trigger_data *var_data,
691 unsigned int var_idx)
693 struct hist_field *hist_field;
696 for (i = 0; i < hist_data->n_var_refs; i++) {
697 hist_field = hist_data->var_refs[i];
698 if (check_field_for_var_ref(hist_field, var_data, var_idx))
706 * find_any_var_ref - Check if there is a reference to a given trigger variable
707 * @hist_data: The hist trigger
708 * @var_idx: The trigger variable identifier
710 * Check to see whether the given variable is currently referenced by
713 * The trigger the variable is defined on is explicitly excluded - the
714 * assumption being that a self-reference doesn't prevent a trigger
715 * from being removed.
717 * Return: The VAR_REF field referencing the variable if so, NULL if not
719 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
720 unsigned int var_idx)
722 struct trace_array *tr = hist_data->event_file->tr;
723 struct hist_field *found = NULL;
724 struct hist_var_data *var_data;
726 list_for_each_entry(var_data, &tr->hist_vars, list) {
727 if (var_data->hist_data == hist_data)
729 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
738 * check_var_refs - Check if there is a reference to any of trigger's variables
739 * @hist_data: The hist trigger
741 * A trigger can define one or more variables. If any one of them is
742 * currently referenced by any other trigger, this function will
745 * Typically used to determine whether or not a trigger can be removed
746 * - if there are any references to a trigger's variables, it cannot.
748 * Return: True if there is a reference to any of trigger's variables
750 static bool check_var_refs(struct hist_trigger_data *hist_data)
752 struct hist_field *field;
756 for_each_hist_field(i, hist_data) {
757 field = hist_data->fields[i];
758 if (field && field->flags & HIST_FIELD_FL_VAR) {
759 if (find_any_var_ref(hist_data, field->var.idx)) {
769 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
771 struct trace_array *tr = hist_data->event_file->tr;
772 struct hist_var_data *var_data, *found = NULL;
774 list_for_each_entry(var_data, &tr->hist_vars, list) {
775 if (var_data->hist_data == hist_data) {
784 static bool field_has_hist_vars(struct hist_field *hist_field,
795 if (hist_field->flags & HIST_FIELD_FL_VAR ||
796 hist_field->flags & HIST_FIELD_FL_VAR_REF)
799 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
800 struct hist_field *operand;
802 operand = hist_field->operands[i];
803 if (field_has_hist_vars(operand, level + 1))
810 static bool has_hist_vars(struct hist_trigger_data *hist_data)
812 struct hist_field *hist_field;
815 for_each_hist_field(i, hist_data) {
816 hist_field = hist_data->fields[i];
817 if (field_has_hist_vars(hist_field, 0))
824 static int save_hist_vars(struct hist_trigger_data *hist_data)
826 struct trace_array *tr = hist_data->event_file->tr;
827 struct hist_var_data *var_data;
829 var_data = find_hist_vars(hist_data);
833 if (tracing_check_open_get_tr(tr))
836 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
842 var_data->hist_data = hist_data;
843 list_add(&var_data->list, &tr->hist_vars);
848 static void remove_hist_vars(struct hist_trigger_data *hist_data)
850 struct trace_array *tr = hist_data->event_file->tr;
851 struct hist_var_data *var_data;
853 var_data = find_hist_vars(hist_data);
857 if (WARN_ON(check_var_refs(hist_data)))
860 list_del(&var_data->list);
867 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
868 const char *var_name)
870 struct hist_field *hist_field, *found = NULL;
873 for_each_hist_field(i, hist_data) {
874 hist_field = hist_data->fields[i];
875 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
876 strcmp(hist_field->var.name, var_name) == 0) {
885 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
886 struct trace_event_file *file,
887 const char *var_name)
889 struct hist_trigger_data *test_data;
890 struct event_trigger_data *test;
891 struct hist_field *hist_field;
893 lockdep_assert_held(&event_mutex);
895 hist_field = find_var_field(hist_data, var_name);
899 list_for_each_entry(test, &file->triggers, list) {
900 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
901 test_data = test->private_data;
902 hist_field = find_var_field(test_data, var_name);
911 static struct trace_event_file *find_var_file(struct trace_array *tr,
916 struct hist_trigger_data *var_hist_data;
917 struct hist_var_data *var_data;
918 struct trace_event_file *file, *found = NULL;
921 return find_event_file(tr, system, event_name);
923 list_for_each_entry(var_data, &tr->hist_vars, list) {
924 var_hist_data = var_data->hist_data;
925 file = var_hist_data->event_file;
929 if (find_var_field(var_hist_data, var_name)) {
931 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
942 static struct hist_field *find_file_var(struct trace_event_file *file,
943 const char *var_name)
945 struct hist_trigger_data *test_data;
946 struct event_trigger_data *test;
947 struct hist_field *hist_field;
949 lockdep_assert_held(&event_mutex);
951 list_for_each_entry(test, &file->triggers, list) {
952 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
953 test_data = test->private_data;
954 hist_field = find_var_field(test_data, var_name);
963 static struct hist_field *
964 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
966 struct trace_array *tr = hist_data->event_file->tr;
967 struct hist_field *hist_field, *found = NULL;
968 struct trace_event_file *file;
971 for (i = 0; i < hist_data->n_actions; i++) {
972 struct action_data *data = hist_data->actions[i];
974 if (data->handler == HANDLER_ONMATCH) {
975 char *system = data->match_data.event_system;
976 char *event_name = data->match_data.event;
978 file = find_var_file(tr, system, event_name, var_name);
981 hist_field = find_file_var(file, var_name);
984 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
986 return ERR_PTR(-EINVAL);
996 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1001 struct trace_array *tr = hist_data->event_file->tr;
1002 struct hist_field *hist_field = NULL;
1003 struct trace_event_file *file;
1005 if (!system || !event_name) {
1006 hist_field = find_match_var(hist_data, var_name);
1007 if (IS_ERR(hist_field))
1013 file = find_var_file(tr, system, event_name, var_name);
1017 hist_field = find_file_var(file, var_name);
1022 static u64 hist_field_var_ref(struct hist_field *hist_field,
1023 struct tracing_map_elt *elt,
1024 struct ring_buffer_event *rbe,
1027 struct hist_elt_data *elt_data;
1030 if (WARN_ON_ONCE(!elt))
1033 elt_data = elt->private_data;
1034 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1039 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1040 u64 *var_ref_vals, bool self)
1042 struct hist_trigger_data *var_data;
1043 struct tracing_map_elt *var_elt;
1044 struct hist_field *hist_field;
1045 unsigned int i, var_idx;
1046 bool resolved = true;
1049 for (i = 0; i < hist_data->n_var_refs; i++) {
1050 hist_field = hist_data->var_refs[i];
1051 var_idx = hist_field->var.idx;
1052 var_data = hist_field->var.hist_data;
1054 if (var_data == NULL) {
1059 if ((self && var_data != hist_data) ||
1060 (!self && var_data == hist_data))
1063 var_elt = tracing_map_lookup(var_data->map, key);
1069 if (!tracing_map_var_set(var_elt, var_idx)) {
1074 if (self || !hist_field->read_once)
1075 var_val = tracing_map_read_var(var_elt, var_idx);
1077 var_val = tracing_map_read_var_once(var_elt, var_idx);
1079 var_ref_vals[i] = var_val;
1085 static const char *hist_field_name(struct hist_field *field,
1088 const char *field_name = "";
1094 field_name = field->field->name;
1095 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1096 field->flags & HIST_FIELD_FL_ALIAS)
1097 field_name = hist_field_name(field->operands[0], ++level);
1098 else if (field->flags & HIST_FIELD_FL_CPU)
1099 field_name = "common_cpu";
1100 else if (field->flags & HIST_FIELD_FL_EXPR ||
1101 field->flags & HIST_FIELD_FL_VAR_REF) {
1102 if (field->system) {
1103 static char full_name[MAX_FILTER_STR_VAL];
1105 strcat(full_name, field->system);
1106 strcat(full_name, ".");
1107 strcat(full_name, field->event_name);
1108 strcat(full_name, ".");
1109 strcat(full_name, field->name);
1110 field_name = full_name;
1112 field_name = field->name;
1113 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1114 field_name = "common_timestamp";
1116 if (field_name == NULL)
1122 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1124 hist_field_fn_t fn = NULL;
1126 switch (field_size) {
1128 if (field_is_signed)
1129 fn = hist_field_s64;
1131 fn = hist_field_u64;
1134 if (field_is_signed)
1135 fn = hist_field_s32;
1137 fn = hist_field_u32;
1140 if (field_is_signed)
1141 fn = hist_field_s16;
1143 fn = hist_field_u16;
1146 if (field_is_signed)
1156 static int parse_map_size(char *str)
1158 unsigned long size, map_bits;
1161 ret = kstrtoul(str, 0, &size);
1165 map_bits = ilog2(roundup_pow_of_two(size));
1166 if (map_bits < TRACING_MAP_BITS_MIN ||
1167 map_bits > TRACING_MAP_BITS_MAX)
1175 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1182 for (i = 0; i < attrs->n_assignments; i++)
1183 kfree(attrs->assignment_str[i]);
1185 for (i = 0; i < attrs->n_actions; i++)
1186 kfree(attrs->action_str[i]);
1189 kfree(attrs->sort_key_str);
1190 kfree(attrs->keys_str);
1191 kfree(attrs->vals_str);
1192 kfree(attrs->clock);
1196 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1200 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1203 if ((str_has_prefix(str, "onmatch(")) ||
1204 (str_has_prefix(str, "onmax(")) ||
1205 (str_has_prefix(str, "onchange("))) {
1206 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1207 if (!attrs->action_str[attrs->n_actions]) {
1217 static int parse_assignment(struct trace_array *tr,
1218 char *str, struct hist_trigger_attrs *attrs)
1222 if ((len = str_has_prefix(str, "key=")) ||
1223 (len = str_has_prefix(str, "keys="))) {
1224 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1225 if (!attrs->keys_str) {
1229 } else if ((len = str_has_prefix(str, "val=")) ||
1230 (len = str_has_prefix(str, "vals=")) ||
1231 (len = str_has_prefix(str, "values="))) {
1232 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1233 if (!attrs->vals_str) {
1237 } else if ((len = str_has_prefix(str, "sort="))) {
1238 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1239 if (!attrs->sort_key_str) {
1243 } else if (str_has_prefix(str, "name=")) {
1244 attrs->name = kstrdup(str, GFP_KERNEL);
1249 } else if ((len = str_has_prefix(str, "clock="))) {
1252 str = strstrip(str);
1253 attrs->clock = kstrdup(str, GFP_KERNEL);
1254 if (!attrs->clock) {
1258 } else if ((len = str_has_prefix(str, "size="))) {
1259 int map_bits = parse_map_size(str + len);
1265 attrs->map_bits = map_bits;
1269 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1270 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1275 assignment = kstrdup(str, GFP_KERNEL);
1281 attrs->assignment_str[attrs->n_assignments++] = assignment;
1287 static struct hist_trigger_attrs *
1288 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1290 struct hist_trigger_attrs *attrs;
1293 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1295 return ERR_PTR(-ENOMEM);
1297 while (trigger_str) {
1298 char *str = strsep(&trigger_str, ":");
1301 rhs = strchr(str, '=');
1303 if (!strlen(++rhs)) {
1305 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1308 ret = parse_assignment(tr, str, attrs);
1311 } else if (strcmp(str, "pause") == 0)
1312 attrs->pause = true;
1313 else if ((strcmp(str, "cont") == 0) ||
1314 (strcmp(str, "continue") == 0))
1316 else if (strcmp(str, "clear") == 0)
1317 attrs->clear = true;
1319 ret = parse_action(str, attrs);
1325 if (!attrs->keys_str) {
1330 if (!attrs->clock) {
1331 attrs->clock = kstrdup("global", GFP_KERNEL);
1332 if (!attrs->clock) {
1340 destroy_hist_trigger_attrs(attrs);
1342 return ERR_PTR(ret);
1345 static inline void save_comm(char *comm, struct task_struct *task)
1348 strcpy(comm, "<idle>");
1352 if (WARN_ON_ONCE(task->pid < 0)) {
1353 strcpy(comm, "<XXX>");
1357 strncpy(comm, task->comm, TASK_COMM_LEN);
1360 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1364 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1365 kfree(elt_data->field_var_str[i]);
1367 kfree(elt_data->comm);
1371 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1373 struct hist_elt_data *elt_data = elt->private_data;
1375 hist_elt_data_free(elt_data);
1378 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1380 struct hist_trigger_data *hist_data = elt->map->private_data;
1381 unsigned int size = TASK_COMM_LEN;
1382 struct hist_elt_data *elt_data;
1383 struct hist_field *key_field;
1384 unsigned int i, n_str;
1386 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1390 for_each_hist_key_field(i, hist_data) {
1391 key_field = hist_data->fields[i];
1393 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
1394 elt_data->comm = kzalloc(size, GFP_KERNEL);
1395 if (!elt_data->comm) {
1403 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1404 hist_data->n_var_str;
1405 if (n_str > SYNTH_FIELDS_MAX) {
1406 hist_elt_data_free(elt_data);
1410 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1412 size = STR_VAR_LEN_MAX;
1414 for (i = 0; i < n_str; i++) {
1415 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1416 if (!elt_data->field_var_str[i]) {
1417 hist_elt_data_free(elt_data);
1422 elt->private_data = elt_data;
1427 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1429 struct hist_elt_data *elt_data = elt->private_data;
1432 save_comm(elt_data->comm, current);
1435 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1436 .elt_alloc = hist_trigger_elt_data_alloc,
1437 .elt_free = hist_trigger_elt_data_free,
1438 .elt_init = hist_trigger_elt_data_init,
1441 static const char *get_hist_field_flags(struct hist_field *hist_field)
1443 const char *flags_str = NULL;
1445 if (hist_field->flags & HIST_FIELD_FL_HEX)
1447 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1449 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1450 flags_str = "sym-offset";
1451 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1452 flags_str = "execname";
1453 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1454 flags_str = "syscall";
1455 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1457 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1458 flags_str = "usecs";
1463 static void expr_field_str(struct hist_field *field, char *expr)
1465 if (field->flags & HIST_FIELD_FL_VAR_REF)
1468 strcat(expr, hist_field_name(field, 0));
1470 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1471 const char *flags_str = get_hist_field_flags(field);
1475 strcat(expr, flags_str);
1480 static char *expr_str(struct hist_field *field, unsigned int level)
1487 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1491 if (!field->operands[0]) {
1492 expr_field_str(field, expr);
1496 if (field->operator == FIELD_OP_UNARY_MINUS) {
1500 subexpr = expr_str(field->operands[0], ++level);
1505 strcat(expr, subexpr);
1513 expr_field_str(field->operands[0], expr);
1515 switch (field->operator) {
1516 case FIELD_OP_MINUS:
1527 expr_field_str(field->operands[1], expr);
1532 static int contains_operator(char *str)
1534 enum field_op_id field_op = FIELD_OP_NONE;
1537 op = strpbrk(str, "+-");
1539 return FIELD_OP_NONE;
1544 * Unfortunately, the modifier ".sym-offset"
1545 * can confuse things.
1547 if (op - str >= 4 && !strncmp(op - 4, ".sym-offset", 11))
1548 return FIELD_OP_NONE;
1551 field_op = FIELD_OP_UNARY_MINUS;
1553 field_op = FIELD_OP_MINUS;
1556 field_op = FIELD_OP_PLUS;
1565 static void get_hist_field(struct hist_field *hist_field)
1570 static void __destroy_hist_field(struct hist_field *hist_field)
1572 if (--hist_field->ref > 1)
1575 kfree(hist_field->var.name);
1576 kfree(hist_field->name);
1577 kfree(hist_field->type);
1579 kfree(hist_field->system);
1580 kfree(hist_field->event_name);
1585 static void destroy_hist_field(struct hist_field *hist_field,
1596 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1597 return; /* var refs will be destroyed separately */
1599 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1600 destroy_hist_field(hist_field->operands[i], level + 1);
1602 __destroy_hist_field(hist_field);
1605 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1606 struct ftrace_event_field *field,
1607 unsigned long flags,
1610 struct hist_field *hist_field;
1612 if (field && is_function_field(field))
1615 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1619 hist_field->ref = 1;
1621 hist_field->hist_data = hist_data;
1623 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1624 goto out; /* caller will populate */
1626 if (flags & HIST_FIELD_FL_VAR_REF) {
1627 hist_field->fn = hist_field_var_ref;
1631 if (flags & HIST_FIELD_FL_HITCOUNT) {
1632 hist_field->fn = hist_field_counter;
1633 hist_field->size = sizeof(u64);
1634 hist_field->type = kstrdup("u64", GFP_KERNEL);
1635 if (!hist_field->type)
1640 if (flags & HIST_FIELD_FL_STACKTRACE) {
1641 hist_field->fn = hist_field_none;
1645 if (flags & HIST_FIELD_FL_LOG2) {
1646 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
1647 hist_field->fn = hist_field_log2;
1648 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1649 hist_field->size = hist_field->operands[0]->size;
1650 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
1651 if (!hist_field->type)
1656 if (flags & HIST_FIELD_FL_TIMESTAMP) {
1657 hist_field->fn = hist_field_timestamp;
1658 hist_field->size = sizeof(u64);
1659 hist_field->type = kstrdup("u64", GFP_KERNEL);
1660 if (!hist_field->type)
1665 if (flags & HIST_FIELD_FL_CPU) {
1666 hist_field->fn = hist_field_cpu;
1667 hist_field->size = sizeof(int);
1668 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
1669 if (!hist_field->type)
1674 if (WARN_ON_ONCE(!field))
1677 /* Pointers to strings are just pointers and dangerous to dereference */
1678 if (is_string_field(field) &&
1679 (field->filter_type != FILTER_PTR_STRING)) {
1680 flags |= HIST_FIELD_FL_STRING;
1682 hist_field->size = MAX_FILTER_STR_VAL;
1683 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1684 if (!hist_field->type)
1687 if (field->filter_type == FILTER_STATIC_STRING) {
1688 hist_field->fn = hist_field_string;
1689 hist_field->size = field->size;
1690 } else if (field->filter_type == FILTER_DYN_STRING)
1691 hist_field->fn = hist_field_dynstring;
1693 hist_field->fn = hist_field_pstring;
1695 hist_field->size = field->size;
1696 hist_field->is_signed = field->is_signed;
1697 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1698 if (!hist_field->type)
1701 hist_field->fn = select_value_fn(field->size,
1703 if (!hist_field->fn) {
1704 destroy_hist_field(hist_field, 0);
1709 hist_field->field = field;
1710 hist_field->flags = flags;
1713 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
1714 if (!hist_field->var.name)
1720 destroy_hist_field(hist_field, 0);
1724 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
1728 for (i = 0; i < HIST_FIELDS_MAX; i++) {
1729 if (hist_data->fields[i]) {
1730 destroy_hist_field(hist_data->fields[i], 0);
1731 hist_data->fields[i] = NULL;
1735 for (i = 0; i < hist_data->n_var_refs; i++) {
1736 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
1737 __destroy_hist_field(hist_data->var_refs[i]);
1738 hist_data->var_refs[i] = NULL;
1742 static int init_var_ref(struct hist_field *ref_field,
1743 struct hist_field *var_field,
1744 char *system, char *event_name)
1748 ref_field->var.idx = var_field->var.idx;
1749 ref_field->var.hist_data = var_field->hist_data;
1750 ref_field->size = var_field->size;
1751 ref_field->is_signed = var_field->is_signed;
1752 ref_field->flags |= var_field->flags &
1753 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
1756 ref_field->system = kstrdup(system, GFP_KERNEL);
1757 if (!ref_field->system)
1762 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
1763 if (!ref_field->event_name) {
1769 if (var_field->var.name) {
1770 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
1771 if (!ref_field->name) {
1775 } else if (var_field->name) {
1776 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
1777 if (!ref_field->name) {
1783 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
1784 if (!ref_field->type) {
1791 kfree(ref_field->system);
1792 kfree(ref_field->event_name);
1793 kfree(ref_field->name);
1798 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
1799 struct hist_field *var_field)
1801 struct hist_field *ref_field;
1804 for (i = 0; i < hist_data->n_var_refs; i++) {
1805 ref_field = hist_data->var_refs[i];
1806 if (ref_field->var.idx == var_field->var.idx &&
1807 ref_field->var.hist_data == var_field->hist_data)
1815 * create_var_ref - Create a variable reference and attach it to trigger
1816 * @hist_data: The trigger that will be referencing the variable
1817 * @var_field: The VAR field to create a reference to
1818 * @system: The optional system string
1819 * @event_name: The optional event_name string
1821 * Given a variable hist_field, create a VAR_REF hist_field that
1822 * represents a reference to it.
1824 * This function also adds the reference to the trigger that
1825 * now references the variable.
1827 * Return: The VAR_REF field if successful, NULL if not
1829 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
1830 struct hist_field *var_field,
1831 char *system, char *event_name)
1833 unsigned long flags = HIST_FIELD_FL_VAR_REF;
1834 struct hist_field *ref_field;
1837 /* Check if the variable already exists */
1838 for (i = 0; i < hist_data->n_var_refs; i++) {
1839 ref_field = hist_data->var_refs[i];
1840 if (ref_field->var.idx == var_field->var.idx &&
1841 ref_field->var.hist_data == var_field->hist_data) {
1842 get_hist_field(ref_field);
1847 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
1849 if (init_var_ref(ref_field, var_field, system, event_name)) {
1850 destroy_hist_field(ref_field, 0);
1854 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
1855 ref_field->var_ref_idx = hist_data->n_var_refs++;
1861 static bool is_var_ref(char *var_name)
1863 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
1869 static char *field_name_from_var(struct hist_trigger_data *hist_data,
1875 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
1876 name = hist_data->attrs->var_defs.name[i];
1878 if (strcmp(var_name, name) == 0) {
1879 field = hist_data->attrs->var_defs.expr[i];
1880 if (contains_operator(field) || is_var_ref(field))
1889 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
1890 char *system, char *event_name,
1893 struct trace_event_call *call;
1895 if (system && event_name) {
1896 call = hist_data->event_file->event_call;
1898 if (strcmp(system, call->class->system) != 0)
1901 if (strcmp(event_name, trace_event_name(call)) != 0)
1905 if (!!system != !!event_name)
1908 if (!is_var_ref(var_name))
1913 return field_name_from_var(hist_data, var_name);
1916 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
1917 char *system, char *event_name,
1920 struct hist_field *var_field = NULL, *ref_field = NULL;
1921 struct trace_array *tr = hist_data->event_file->tr;
1923 if (!is_var_ref(var_name))
1928 var_field = find_event_var(hist_data, system, event_name, var_name);
1930 ref_field = create_var_ref(hist_data, var_field,
1931 system, event_name);
1934 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
1939 static struct ftrace_event_field *
1940 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
1941 char *field_str, unsigned long *flags)
1943 struct ftrace_event_field *field = NULL;
1944 char *field_name, *modifier, *str;
1945 struct trace_array *tr = file->tr;
1947 modifier = str = kstrdup(field_str, GFP_KERNEL);
1949 return ERR_PTR(-ENOMEM);
1951 field_name = strsep(&modifier, ".");
1953 if (strcmp(modifier, "hex") == 0)
1954 *flags |= HIST_FIELD_FL_HEX;
1955 else if (strcmp(modifier, "sym") == 0)
1956 *flags |= HIST_FIELD_FL_SYM;
1957 else if (strcmp(modifier, "sym-offset") == 0)
1958 *flags |= HIST_FIELD_FL_SYM_OFFSET;
1959 else if ((strcmp(modifier, "execname") == 0) &&
1960 (strcmp(field_name, "common_pid") == 0))
1961 *flags |= HIST_FIELD_FL_EXECNAME;
1962 else if (strcmp(modifier, "syscall") == 0)
1963 *flags |= HIST_FIELD_FL_SYSCALL;
1964 else if (strcmp(modifier, "log2") == 0)
1965 *flags |= HIST_FIELD_FL_LOG2;
1966 else if (strcmp(modifier, "usecs") == 0)
1967 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
1969 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
1970 field = ERR_PTR(-EINVAL);
1975 if (strcmp(field_name, "common_timestamp") == 0) {
1976 *flags |= HIST_FIELD_FL_TIMESTAMP;
1977 hist_data->enable_timestamps = true;
1978 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1979 hist_data->attrs->ts_in_usecs = true;
1980 } else if (strcmp(field_name, "common_cpu") == 0)
1981 *flags |= HIST_FIELD_FL_CPU;
1983 field = trace_find_event_field(file->event_call, field_name);
1984 if (!field || !field->size) {
1986 * For backward compatibility, if field_name
1987 * was "cpu", then we treat this the same as
1990 if (strcmp(field_name, "cpu") == 0) {
1991 *flags |= HIST_FIELD_FL_CPU;
1993 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
1994 errpos(field_name));
1995 field = ERR_PTR(-EINVAL);
2006 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2007 struct hist_field *var_ref,
2010 struct hist_field *alias = NULL;
2011 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2013 alias = create_hist_field(hist_data, NULL, flags, var_name);
2017 alias->fn = var_ref->fn;
2018 alias->operands[0] = var_ref;
2020 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2021 destroy_hist_field(alias, 0);
2025 alias->var_ref_idx = var_ref->var_ref_idx;
2030 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2031 struct trace_event_file *file, char *str,
2032 unsigned long *flags, char *var_name)
2034 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2035 struct ftrace_event_field *field = NULL;
2036 struct hist_field *hist_field = NULL;
2039 s = strchr(str, '.');
2041 s = strchr(++s, '.');
2043 ref_system = strsep(&str, ".");
2048 ref_event = strsep(&str, ".");
2057 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2059 hist_field = parse_var_ref(hist_data, ref_system,
2060 ref_event, ref_var);
2063 hist_field = create_alias(hist_data, hist_field, var_name);
2074 field = parse_field(hist_data, file, str, flags);
2075 if (IS_ERR(field)) {
2076 ret = PTR_ERR(field);
2080 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2088 return ERR_PTR(ret);
2091 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2092 struct trace_event_file *file,
2093 char *str, unsigned long flags,
2094 char *var_name, unsigned int level);
2096 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2097 struct trace_event_file *file,
2098 char *str, unsigned long flags,
2099 char *var_name, unsigned int level)
2101 struct hist_field *operand1, *expr = NULL;
2102 unsigned long operand_flags;
2106 /* we support only -(xxx) i.e. explicit parens required */
2109 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2114 str++; /* skip leading '-' */
2116 s = strchr(str, '(');
2124 s = strrchr(str, ')');
2128 ret = -EINVAL; /* no closing ')' */
2132 flags |= HIST_FIELD_FL_EXPR;
2133 expr = create_hist_field(hist_data, NULL, flags, var_name);
2140 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2141 if (IS_ERR(operand1)) {
2142 ret = PTR_ERR(operand1);
2145 if (operand1->flags & HIST_FIELD_FL_STRING) {
2146 /* String type can not be the operand of unary operator. */
2147 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2148 destroy_hist_field(operand1, 0);
2153 expr->flags |= operand1->flags &
2154 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2155 expr->fn = hist_field_unary_minus;
2156 expr->operands[0] = operand1;
2157 expr->operator = FIELD_OP_UNARY_MINUS;
2158 expr->name = expr_str(expr, 0);
2159 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2167 destroy_hist_field(expr, 0);
2168 return ERR_PTR(ret);
2171 static int check_expr_operands(struct trace_array *tr,
2172 struct hist_field *operand1,
2173 struct hist_field *operand2)
2175 unsigned long operand1_flags = operand1->flags;
2176 unsigned long operand2_flags = operand2->flags;
2178 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2179 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2180 struct hist_field *var;
2182 var = find_var_field(operand1->var.hist_data, operand1->name);
2185 operand1_flags = var->flags;
2188 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2189 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2190 struct hist_field *var;
2192 var = find_var_field(operand2->var.hist_data, operand2->name);
2195 operand2_flags = var->flags;
2198 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2199 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2200 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2207 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2208 struct trace_event_file *file,
2209 char *str, unsigned long flags,
2210 char *var_name, unsigned int level)
2212 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2213 unsigned long operand_flags;
2214 int field_op, ret = -EINVAL;
2215 char *sep, *operand1_str;
2218 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2219 return ERR_PTR(-EINVAL);
2222 field_op = contains_operator(str);
2224 if (field_op == FIELD_OP_NONE)
2225 return parse_atom(hist_data, file, str, &flags, var_name);
2227 if (field_op == FIELD_OP_UNARY_MINUS)
2228 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2231 case FIELD_OP_MINUS:
2241 operand1_str = strsep(&str, sep);
2242 if (!operand1_str || !str)
2246 operand1 = parse_atom(hist_data, file, operand1_str,
2247 &operand_flags, NULL);
2248 if (IS_ERR(operand1)) {
2249 ret = PTR_ERR(operand1);
2253 if (operand1->flags & HIST_FIELD_FL_STRING) {
2254 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2259 /* rest of string could be another expression e.g. b+c in a+b+c */
2261 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2262 if (IS_ERR(operand2)) {
2263 ret = PTR_ERR(operand2);
2267 if (operand2->flags & HIST_FIELD_FL_STRING) {
2268 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2273 ret = check_expr_operands(file->tr, operand1, operand2);
2277 flags |= HIST_FIELD_FL_EXPR;
2279 flags |= operand1->flags &
2280 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2282 expr = create_hist_field(hist_data, NULL, flags, var_name);
2288 operand1->read_once = true;
2289 operand2->read_once = true;
2291 expr->operands[0] = operand1;
2292 expr->operands[1] = operand2;
2294 /* The operand sizes should be the same, so just pick one */
2295 expr->size = operand1->size;
2297 expr->operator = field_op;
2298 expr->name = expr_str(expr, 0);
2299 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2306 case FIELD_OP_MINUS:
2307 expr->fn = hist_field_minus;
2310 expr->fn = hist_field_plus;
2319 destroy_hist_field(operand1, 0);
2320 destroy_hist_field(operand2, 0);
2321 destroy_hist_field(expr, 0);
2323 return ERR_PTR(ret);
2326 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2327 struct trace_event_file *file)
2329 struct event_trigger_data *test;
2331 lockdep_assert_held(&event_mutex);
2333 list_for_each_entry(test, &file->triggers, list) {
2334 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2335 if (test->private_data == hist_data)
2336 return test->filter_str;
2343 static struct event_command trigger_hist_cmd;
2344 static int event_hist_trigger_func(struct event_command *cmd_ops,
2345 struct trace_event_file *file,
2346 char *glob, char *cmd, char *param);
2348 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2349 struct hist_trigger_data *hist_data,
2350 unsigned int n_keys)
2352 struct hist_field *target_hist_field, *hist_field;
2353 unsigned int n, i, j;
2355 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2358 i = hist_data->n_vals;
2359 j = target_hist_data->n_vals;
2361 for (n = 0; n < n_keys; n++) {
2362 hist_field = hist_data->fields[i + n];
2363 target_hist_field = target_hist_data->fields[j + n];
2365 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2367 if (hist_field->size != target_hist_field->size)
2369 if (hist_field->is_signed != target_hist_field->is_signed)
2376 static struct hist_trigger_data *
2377 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2378 struct trace_event_file *file)
2380 struct hist_trigger_data *hist_data;
2381 struct event_trigger_data *test;
2382 unsigned int n_keys;
2384 lockdep_assert_held(&event_mutex);
2386 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2388 list_for_each_entry(test, &file->triggers, list) {
2389 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2390 hist_data = test->private_data;
2392 if (compatible_keys(target_hist_data, hist_data, n_keys))
2400 static struct trace_event_file *event_file(struct trace_array *tr,
2401 char *system, char *event_name)
2403 struct trace_event_file *file;
2405 file = __find_event_file(tr, system, event_name);
2407 return ERR_PTR(-EINVAL);
2412 static struct hist_field *
2413 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2414 char *system, char *event_name, char *field_name)
2416 struct hist_field *event_var;
2417 char *synthetic_name;
2419 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2420 if (!synthetic_name)
2421 return ERR_PTR(-ENOMEM);
2423 strcpy(synthetic_name, "synthetic_");
2424 strcat(synthetic_name, field_name);
2426 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2428 kfree(synthetic_name);
2434 * create_field_var_hist - Automatically create a histogram and var for a field
2435 * @target_hist_data: The target hist trigger
2436 * @subsys_name: Optional subsystem name
2437 * @event_name: Optional event name
2438 * @field_name: The name of the field (and the resulting variable)
2440 * Hist trigger actions fetch data from variables, not directly from
2441 * events. However, for convenience, users are allowed to directly
2442 * specify an event field in an action, which will be automatically
2443 * converted into a variable on their behalf.
2445 * If a user specifies a field on an event that isn't the event the
2446 * histogram currently being defined (the target event histogram), the
2447 * only way that can be accomplished is if a new hist trigger is
2448 * created and the field variable defined on that.
2450 * This function creates a new histogram compatible with the target
2451 * event (meaning a histogram with the same key as the target
2452 * histogram), and creates a variable for the specified field, but
2453 * with 'synthetic_' prepended to the variable name in order to avoid
2454 * collision with normal field variables.
2456 * Return: The variable created for the field.
2458 static struct hist_field *
2459 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2460 char *subsys_name, char *event_name, char *field_name)
2462 struct trace_array *tr = target_hist_data->event_file->tr;
2463 struct hist_field *event_var = ERR_PTR(-EINVAL);
2464 struct hist_trigger_data *hist_data;
2465 unsigned int i, n, first = true;
2466 struct field_var_hist *var_hist;
2467 struct trace_event_file *file;
2468 struct hist_field *key_field;
2473 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2474 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2475 return ERR_PTR(-EINVAL);
2478 file = event_file(tr, subsys_name, event_name);
2481 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2482 ret = PTR_ERR(file);
2483 return ERR_PTR(ret);
2487 * Look for a histogram compatible with target. We'll use the
2488 * found histogram specification to create a new matching
2489 * histogram with our variable on it. target_hist_data is not
2490 * yet a registered histogram so we can't use that.
2492 hist_data = find_compatible_hist(target_hist_data, file);
2494 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2495 return ERR_PTR(-EINVAL);
2498 /* See if a synthetic field variable has already been created */
2499 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2500 event_name, field_name);
2501 if (!IS_ERR_OR_NULL(event_var))
2504 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2506 return ERR_PTR(-ENOMEM);
2508 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2511 return ERR_PTR(-ENOMEM);
2514 /* Use the same keys as the compatible histogram */
2515 strcat(cmd, "keys=");
2517 for_each_hist_key_field(i, hist_data) {
2518 key_field = hist_data->fields[i];
2521 strcat(cmd, key_field->field->name);
2525 /* Create the synthetic field variable specification */
2526 strcat(cmd, ":synthetic_");
2527 strcat(cmd, field_name);
2529 strcat(cmd, field_name);
2531 /* Use the same filter as the compatible histogram */
2532 saved_filter = find_trigger_filter(hist_data, file);
2534 strcat(cmd, " if ");
2535 strcat(cmd, saved_filter);
2538 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
2539 if (!var_hist->cmd) {
2542 return ERR_PTR(-ENOMEM);
2545 /* Save the compatible histogram information */
2546 var_hist->hist_data = hist_data;
2548 /* Create the new histogram with our variable */
2549 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
2553 kfree(var_hist->cmd);
2555 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
2556 return ERR_PTR(ret);
2561 /* If we can't find the variable, something went wrong */
2562 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2563 event_name, field_name);
2564 if (IS_ERR_OR_NULL(event_var)) {
2565 kfree(var_hist->cmd);
2567 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
2568 return ERR_PTR(-EINVAL);
2571 n = target_hist_data->n_field_var_hists;
2572 target_hist_data->field_var_hists[n] = var_hist;
2573 target_hist_data->n_field_var_hists++;
2578 static struct hist_field *
2579 find_target_event_var(struct hist_trigger_data *hist_data,
2580 char *subsys_name, char *event_name, char *var_name)
2582 struct trace_event_file *file = hist_data->event_file;
2583 struct hist_field *hist_field = NULL;
2586 struct trace_event_call *call;
2591 call = file->event_call;
2593 if (strcmp(subsys_name, call->class->system) != 0)
2596 if (strcmp(event_name, trace_event_name(call)) != 0)
2600 hist_field = find_var_field(hist_data, var_name);
2605 static inline void __update_field_vars(struct tracing_map_elt *elt,
2606 struct ring_buffer_event *rbe,
2608 struct field_var **field_vars,
2609 unsigned int n_field_vars,
2610 unsigned int field_var_str_start)
2612 struct hist_elt_data *elt_data = elt->private_data;
2613 unsigned int i, j, var_idx;
2616 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
2617 struct field_var *field_var = field_vars[i];
2618 struct hist_field *var = field_var->var;
2619 struct hist_field *val = field_var->val;
2621 var_val = val->fn(val, elt, rbe, rec);
2622 var_idx = var->var.idx;
2624 if (val->flags & HIST_FIELD_FL_STRING) {
2625 char *str = elt_data->field_var_str[j++];
2626 char *val_str = (char *)(uintptr_t)var_val;
2628 strscpy(str, val_str, val->size);
2629 var_val = (u64)(uintptr_t)str;
2631 tracing_map_set_var(elt, var_idx, var_val);
2635 static void update_field_vars(struct hist_trigger_data *hist_data,
2636 struct tracing_map_elt *elt,
2637 struct ring_buffer_event *rbe,
2640 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
2641 hist_data->n_field_vars, 0);
2644 static void save_track_data_vars(struct hist_trigger_data *hist_data,
2645 struct tracing_map_elt *elt, void *rec,
2646 struct ring_buffer_event *rbe, void *key,
2647 struct action_data *data, u64 *var_ref_vals)
2649 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
2650 hist_data->n_save_vars, hist_data->n_field_var_str);
2653 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
2654 struct trace_event_file *file,
2655 char *name, int size, const char *type)
2657 struct hist_field *var;
2660 if (find_var(hist_data, file, name) && !hist_data->remove) {
2661 var = ERR_PTR(-EINVAL);
2665 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2667 var = ERR_PTR(-ENOMEM);
2671 idx = tracing_map_add_var(hist_data->map);
2674 var = ERR_PTR(-EINVAL);
2679 var->flags = HIST_FIELD_FL_VAR;
2681 var->var.hist_data = var->hist_data = hist_data;
2683 var->var.name = kstrdup(name, GFP_KERNEL);
2684 var->type = kstrdup(type, GFP_KERNEL);
2685 if (!var->var.name || !var->type) {
2686 kfree(var->var.name);
2689 var = ERR_PTR(-ENOMEM);
2695 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
2696 struct trace_event_file *file,
2699 struct hist_field *val = NULL, *var = NULL;
2700 unsigned long flags = HIST_FIELD_FL_VAR;
2701 struct trace_array *tr = file->tr;
2702 struct field_var *field_var;
2705 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
2706 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2711 val = parse_atom(hist_data, file, field_name, &flags, NULL);
2713 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
2718 var = create_var(hist_data, file, field_name, val->size, val->type);
2720 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
2726 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
2734 field_var->var = var;
2735 field_var->val = val;
2739 field_var = ERR_PTR(ret);
2744 * create_target_field_var - Automatically create a variable for a field
2745 * @target_hist_data: The target hist trigger
2746 * @subsys_name: Optional subsystem name
2747 * @event_name: Optional event name
2748 * @var_name: The name of the field (and the resulting variable)
2750 * Hist trigger actions fetch data from variables, not directly from
2751 * events. However, for convenience, users are allowed to directly
2752 * specify an event field in an action, which will be automatically
2753 * converted into a variable on their behalf.
2755 * This function creates a field variable with the name var_name on
2756 * the hist trigger currently being defined on the target event. If
2757 * subsys_name and event_name are specified, this function simply
2758 * verifies that they do in fact match the target event subsystem and
2761 * Return: The variable created for the field.
2763 static struct field_var *
2764 create_target_field_var(struct hist_trigger_data *target_hist_data,
2765 char *subsys_name, char *event_name, char *var_name)
2767 struct trace_event_file *file = target_hist_data->event_file;
2770 struct trace_event_call *call;
2775 call = file->event_call;
2777 if (strcmp(subsys_name, call->class->system) != 0)
2780 if (strcmp(event_name, trace_event_name(call)) != 0)
2784 return create_field_var(target_hist_data, file, var_name);
2787 static bool check_track_val_max(u64 track_val, u64 var_val)
2789 if (var_val <= track_val)
2795 static bool check_track_val_changed(u64 track_val, u64 var_val)
2797 if (var_val == track_val)
2803 static u64 get_track_val(struct hist_trigger_data *hist_data,
2804 struct tracing_map_elt *elt,
2805 struct action_data *data)
2807 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2810 track_val = tracing_map_read_var(elt, track_var_idx);
2815 static void save_track_val(struct hist_trigger_data *hist_data,
2816 struct tracing_map_elt *elt,
2817 struct action_data *data, u64 var_val)
2819 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2821 tracing_map_set_var(elt, track_var_idx, var_val);
2824 static void save_track_data(struct hist_trigger_data *hist_data,
2825 struct tracing_map_elt *elt, void *rec,
2826 struct ring_buffer_event *rbe, void *key,
2827 struct action_data *data, u64 *var_ref_vals)
2829 if (data->track_data.save_data)
2830 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
2833 static bool check_track_val(struct tracing_map_elt *elt,
2834 struct action_data *data,
2837 struct hist_trigger_data *hist_data;
2840 hist_data = data->track_data.track_var->hist_data;
2841 track_val = get_track_val(hist_data, elt, data);
2843 return data->track_data.check_val(track_val, var_val);
2846 #ifdef CONFIG_TRACER_SNAPSHOT
2847 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2849 /* called with tr->max_lock held */
2850 struct track_data *track_data = tr->cond_snapshot->cond_data;
2851 struct hist_elt_data *elt_data, *track_elt_data;
2852 struct snapshot_context *context = cond_data;
2853 struct action_data *action;
2859 action = track_data->action_data;
2861 track_val = get_track_val(track_data->hist_data, context->elt,
2862 track_data->action_data);
2864 if (!action->track_data.check_val(track_data->track_val, track_val))
2867 track_data->track_val = track_val;
2868 memcpy(track_data->key, context->key, track_data->key_len);
2870 elt_data = context->elt->private_data;
2871 track_elt_data = track_data->elt.private_data;
2873 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
2875 track_data->updated = true;
2880 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2881 struct tracing_map_elt *elt, void *rec,
2882 struct ring_buffer_event *rbe, void *key,
2883 struct action_data *data,
2886 struct trace_event_file *file = hist_data->event_file;
2887 struct snapshot_context context;
2892 tracing_snapshot_cond(file->tr, &context);
2895 static void hist_trigger_print_key(struct seq_file *m,
2896 struct hist_trigger_data *hist_data,
2898 struct tracing_map_elt *elt);
2900 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
2904 if (!hist_data->n_actions)
2907 for (i = 0; i < hist_data->n_actions; i++) {
2908 struct action_data *data = hist_data->actions[i];
2910 if (data->action == ACTION_SNAPSHOT)
2917 static void track_data_snapshot_print(struct seq_file *m,
2918 struct hist_trigger_data *hist_data)
2920 struct trace_event_file *file = hist_data->event_file;
2921 struct track_data *track_data;
2922 struct action_data *action;
2924 track_data = tracing_cond_snapshot_data(file->tr);
2928 if (!track_data->updated)
2931 action = snapshot_action(hist_data);
2935 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
2936 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
2937 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
2938 action->track_data.var_str, track_data->track_val);
2940 seq_puts(m, "\ttriggered by event with key: ");
2941 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
2945 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2949 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2950 struct tracing_map_elt *elt, void *rec,
2951 struct ring_buffer_event *rbe, void *key,
2952 struct action_data *data,
2953 u64 *var_ref_vals) {}
2954 static void track_data_snapshot_print(struct seq_file *m,
2955 struct hist_trigger_data *hist_data) {}
2956 #endif /* CONFIG_TRACER_SNAPSHOT */
2958 static void track_data_print(struct seq_file *m,
2959 struct hist_trigger_data *hist_data,
2960 struct tracing_map_elt *elt,
2961 struct action_data *data)
2963 u64 track_val = get_track_val(hist_data, elt, data);
2964 unsigned int i, save_var_idx;
2966 if (data->handler == HANDLER_ONMAX)
2967 seq_printf(m, "\n\tmax: %10llu", track_val);
2968 else if (data->handler == HANDLER_ONCHANGE)
2969 seq_printf(m, "\n\tchanged: %10llu", track_val);
2971 if (data->action == ACTION_SNAPSHOT)
2974 for (i = 0; i < hist_data->n_save_vars; i++) {
2975 struct hist_field *save_val = hist_data->save_vars[i]->val;
2976 struct hist_field *save_var = hist_data->save_vars[i]->var;
2979 save_var_idx = save_var->var.idx;
2981 val = tracing_map_read_var(elt, save_var_idx);
2983 if (save_val->flags & HIST_FIELD_FL_STRING) {
2984 seq_printf(m, " %s: %-32s", save_var->var.name,
2985 (char *)(uintptr_t)(val));
2987 seq_printf(m, " %s: %10llu", save_var->var.name, val);
2991 static void ontrack_action(struct hist_trigger_data *hist_data,
2992 struct tracing_map_elt *elt, void *rec,
2993 struct ring_buffer_event *rbe, void *key,
2994 struct action_data *data, u64 *var_ref_vals)
2996 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
2998 if (check_track_val(elt, data, var_val)) {
2999 save_track_val(hist_data, elt, data, var_val);
3000 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3004 static void action_data_destroy(struct action_data *data)
3008 lockdep_assert_held(&event_mutex);
3010 kfree(data->action_name);
3012 for (i = 0; i < data->n_params; i++)
3013 kfree(data->params[i]);
3015 if (data->synth_event)
3016 data->synth_event->ref--;
3018 kfree(data->synth_event_name);
3023 static void track_data_destroy(struct hist_trigger_data *hist_data,
3024 struct action_data *data)
3026 struct trace_event_file *file = hist_data->event_file;
3028 destroy_hist_field(data->track_data.track_var, 0);
3030 if (data->action == ACTION_SNAPSHOT) {
3031 struct track_data *track_data;
3033 track_data = tracing_cond_snapshot_data(file->tr);
3034 if (track_data && track_data->hist_data == hist_data) {
3035 tracing_snapshot_cond_disable(file->tr);
3036 track_data_free(track_data);
3040 kfree(data->track_data.var_str);
3042 action_data_destroy(data);
3045 static int action_create(struct hist_trigger_data *hist_data,
3046 struct action_data *data);
3048 static int track_data_create(struct hist_trigger_data *hist_data,
3049 struct action_data *data)
3051 struct hist_field *var_field, *ref_field, *track_var = NULL;
3052 struct trace_event_file *file = hist_data->event_file;
3053 struct trace_array *tr = file->tr;
3054 char *track_data_var_str;
3057 track_data_var_str = data->track_data.var_str;
3058 if (track_data_var_str[0] != '$') {
3059 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3062 track_data_var_str++;
3064 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3066 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3070 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3074 data->track_data.var_ref = ref_field;
3076 if (data->handler == HANDLER_ONMAX)
3077 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3078 if (IS_ERR(track_var)) {
3079 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3080 ret = PTR_ERR(track_var);
3084 if (data->handler == HANDLER_ONCHANGE)
3085 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3086 if (IS_ERR(track_var)) {
3087 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3088 ret = PTR_ERR(track_var);
3091 data->track_data.track_var = track_var;
3093 ret = action_create(hist_data, data);
3098 static int parse_action_params(struct trace_array *tr, char *params,
3099 struct action_data *data)
3101 char *param, *saved_param;
3102 bool first_param = true;
3106 if (data->n_params >= SYNTH_FIELDS_MAX) {
3107 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3111 param = strsep(¶ms, ",");
3113 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3118 param = strstrip(param);
3119 if (strlen(param) < 2) {
3120 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3125 saved_param = kstrdup(param, GFP_KERNEL);
3131 if (first_param && data->use_trace_keyword) {
3132 data->synth_event_name = saved_param;
3133 first_param = false;
3136 first_param = false;
3138 data->params[data->n_params++] = saved_param;
3144 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3145 enum handler_id handler)
3152 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3157 action_name = strsep(&str, "(");
3158 if (!action_name || !str) {
3159 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3164 if (str_has_prefix(action_name, "save")) {
3165 char *params = strsep(&str, ")");
3168 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3173 ret = parse_action_params(tr, params, data);
3177 if (handler == HANDLER_ONMAX)
3178 data->track_data.check_val = check_track_val_max;
3179 else if (handler == HANDLER_ONCHANGE)
3180 data->track_data.check_val = check_track_val_changed;
3182 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3187 data->track_data.save_data = save_track_data_vars;
3188 data->fn = ontrack_action;
3189 data->action = ACTION_SAVE;
3190 } else if (str_has_prefix(action_name, "snapshot")) {
3191 char *params = strsep(&str, ")");
3194 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3199 if (handler == HANDLER_ONMAX)
3200 data->track_data.check_val = check_track_val_max;
3201 else if (handler == HANDLER_ONCHANGE)
3202 data->track_data.check_val = check_track_val_changed;
3204 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3209 data->track_data.save_data = save_track_data_snapshot;
3210 data->fn = ontrack_action;
3211 data->action = ACTION_SNAPSHOT;
3213 char *params = strsep(&str, ")");
3215 if (str_has_prefix(action_name, "trace"))
3216 data->use_trace_keyword = true;
3219 ret = parse_action_params(tr, params, data);
3224 if (handler == HANDLER_ONMAX)
3225 data->track_data.check_val = check_track_val_max;
3226 else if (handler == HANDLER_ONCHANGE)
3227 data->track_data.check_val = check_track_val_changed;
3229 if (handler != HANDLER_ONMATCH) {
3230 data->track_data.save_data = action_trace;
3231 data->fn = ontrack_action;
3233 data->fn = action_trace;
3235 data->action = ACTION_TRACE;
3238 data->action_name = kstrdup(action_name, GFP_KERNEL);
3239 if (!data->action_name) {
3244 data->handler = handler;
3249 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3250 char *str, enum handler_id handler)
3252 struct action_data *data;
3256 data = kzalloc(sizeof(*data), GFP_KERNEL);
3258 return ERR_PTR(-ENOMEM);
3260 var_str = strsep(&str, ")");
3261 if (!var_str || !str) {
3266 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3267 if (!data->track_data.var_str) {
3272 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3278 track_data_destroy(hist_data, data);
3279 data = ERR_PTR(ret);
3283 static void onmatch_destroy(struct action_data *data)
3285 kfree(data->match_data.event);
3286 kfree(data->match_data.event_system);
3288 action_data_destroy(data);
3291 static void destroy_field_var(struct field_var *field_var)
3296 destroy_hist_field(field_var->var, 0);
3297 destroy_hist_field(field_var->val, 0);
3302 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3306 for (i = 0; i < hist_data->n_field_vars; i++)
3307 destroy_field_var(hist_data->field_vars[i]);
3309 for (i = 0; i < hist_data->n_save_vars; i++)
3310 destroy_field_var(hist_data->save_vars[i]);
3313 static void save_field_var(struct hist_trigger_data *hist_data,
3314 struct field_var *field_var)
3316 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3318 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3319 hist_data->n_field_var_str++;
3323 static int check_synth_field(struct synth_event *event,
3324 struct hist_field *hist_field,
3325 unsigned int field_pos)
3327 struct synth_field *field;
3329 if (field_pos >= event->n_fields)
3332 field = event->fields[field_pos];
3335 * A dynamic string synth field can accept static or
3336 * dynamic. A static string synth field can only accept a
3337 * same-sized static string, which is checked for later.
3339 if (strstr(hist_field->type, "char[") && field->is_string
3340 && field->is_dynamic)
3343 if (strcmp(field->type, hist_field->type) != 0) {
3344 if (field->size != hist_field->size ||
3345 field->is_signed != hist_field->is_signed)
3352 static struct hist_field *
3353 trace_action_find_var(struct hist_trigger_data *hist_data,
3354 struct action_data *data,
3355 char *system, char *event, char *var)
3357 struct trace_array *tr = hist_data->event_file->tr;
3358 struct hist_field *hist_field;
3360 var++; /* skip '$' */
3362 hist_field = find_target_event_var(hist_data, system, event, var);
3364 if (!system && data->handler == HANDLER_ONMATCH) {
3365 system = data->match_data.event_system;
3366 event = data->match_data.event;
3369 hist_field = find_event_var(hist_data, system, event, var);
3373 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3378 static struct hist_field *
3379 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3380 struct action_data *data, char *system,
3381 char *event, char *var)
3383 struct hist_field *hist_field = NULL;
3384 struct field_var *field_var;
3387 * First try to create a field var on the target event (the
3388 * currently being defined). This will create a variable for
3389 * unqualified fields on the target event, or if qualified,
3390 * target fields that have qualified names matching the target.
3392 field_var = create_target_field_var(hist_data, system, event, var);
3394 if (field_var && !IS_ERR(field_var)) {
3395 save_field_var(hist_data, field_var);
3396 hist_field = field_var->var;
3400 * If no explicit system.event is specfied, default to
3401 * looking for fields on the onmatch(system.event.xxx)
3404 if (!system && data->handler == HANDLER_ONMATCH) {
3405 system = data->match_data.event_system;
3406 event = data->match_data.event;
3412 * At this point, we're looking at a field on another
3413 * event. Because we can't modify a hist trigger on
3414 * another event to add a variable for a field, we need
3415 * to create a new trigger on that event and create the
3416 * variable at the same time.
3418 hist_field = create_field_var_hist(hist_data, system, event, var);
3419 if (IS_ERR(hist_field))
3425 destroy_field_var(field_var);
3430 static int trace_action_create(struct hist_trigger_data *hist_data,
3431 struct action_data *data)
3433 struct trace_array *tr = hist_data->event_file->tr;
3434 char *event_name, *param, *system = NULL;
3435 struct hist_field *hist_field, *var_ref;
3437 unsigned int field_pos = 0;
3438 struct synth_event *event;
3439 char *synth_event_name;
3440 int var_ref_idx, ret = 0;
3442 lockdep_assert_held(&event_mutex);
3444 if (data->use_trace_keyword)
3445 synth_event_name = data->synth_event_name;
3447 synth_event_name = data->action_name;
3449 event = find_synth_event(synth_event_name);
3451 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3457 for (i = 0; i < data->n_params; i++) {
3460 p = param = kstrdup(data->params[i], GFP_KERNEL);
3466 system = strsep(¶m, ".");
3468 param = (char *)system;
3469 system = event_name = NULL;
3471 event_name = strsep(¶m, ".");
3479 if (param[0] == '$')
3480 hist_field = trace_action_find_var(hist_data, data,
3484 hist_field = trace_action_create_field_var(hist_data,
3496 if (check_synth_field(event, hist_field, field_pos) == 0) {
3497 var_ref = create_var_ref(hist_data, hist_field,
3498 system, event_name);
3505 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
3506 if (WARN_ON(var_ref_idx < 0)) {
3511 data->var_ref_idx[i] = var_ref_idx;
3518 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
3524 if (field_pos != event->n_fields) {
3525 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
3530 data->synth_event = event;
3539 static int action_create(struct hist_trigger_data *hist_data,
3540 struct action_data *data)
3542 struct trace_event_file *file = hist_data->event_file;
3543 struct trace_array *tr = file->tr;
3544 struct track_data *track_data;
3545 struct field_var *field_var;
3550 if (data->action == ACTION_TRACE)
3551 return trace_action_create(hist_data, data);
3553 if (data->action == ACTION_SNAPSHOT) {
3554 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
3555 if (IS_ERR(track_data)) {
3556 ret = PTR_ERR(track_data);
3560 ret = tracing_snapshot_cond_enable(file->tr, track_data,
3561 cond_snapshot_update);
3563 track_data_free(track_data);
3568 if (data->action == ACTION_SAVE) {
3569 if (hist_data->n_save_vars) {
3571 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
3575 for (i = 0; i < data->n_params; i++) {
3576 param = kstrdup(data->params[i], GFP_KERNEL);
3582 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3583 if (IS_ERR(field_var)) {
3584 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
3586 ret = PTR_ERR(field_var);
3591 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
3592 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3593 hist_data->n_save_var_str++;
3601 static int onmatch_create(struct hist_trigger_data *hist_data,
3602 struct action_data *data)
3604 return action_create(hist_data, data);
3607 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3609 char *match_event, *match_event_system;
3610 struct action_data *data;
3613 data = kzalloc(sizeof(*data), GFP_KERNEL);
3615 return ERR_PTR(-ENOMEM);
3617 match_event = strsep(&str, ")");
3618 if (!match_event || !str) {
3619 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
3623 match_event_system = strsep(&match_event, ".");
3625 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
3629 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3630 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
3634 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
3635 if (!data->match_data.event) {
3640 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
3641 if (!data->match_data.event_system) {
3646 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
3652 onmatch_destroy(data);
3653 data = ERR_PTR(ret);
3657 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3659 hist_data->fields[HITCOUNT_IDX] =
3660 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3661 if (!hist_data->fields[HITCOUNT_IDX])
3664 hist_data->n_vals++;
3665 hist_data->n_fields++;
3667 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3673 static int __create_val_field(struct hist_trigger_data *hist_data,
3674 unsigned int val_idx,
3675 struct trace_event_file *file,
3676 char *var_name, char *field_str,
3677 unsigned long flags)
3679 struct hist_field *hist_field;
3682 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3683 if (IS_ERR(hist_field)) {
3684 ret = PTR_ERR(hist_field);
3688 hist_data->fields[val_idx] = hist_field;
3690 ++hist_data->n_vals;
3691 ++hist_data->n_fields;
3693 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3699 static int create_val_field(struct hist_trigger_data *hist_data,
3700 unsigned int val_idx,
3701 struct trace_event_file *file,
3704 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3707 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3710 static int create_var_field(struct hist_trigger_data *hist_data,
3711 unsigned int val_idx,
3712 struct trace_event_file *file,
3713 char *var_name, char *expr_str)
3715 struct trace_array *tr = hist_data->event_file->tr;
3716 unsigned long flags = 0;
3719 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3722 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3723 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
3727 flags |= HIST_FIELD_FL_VAR;
3728 hist_data->n_vars++;
3729 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3732 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3734 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
3735 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
3740 static int create_val_fields(struct hist_trigger_data *hist_data,
3741 struct trace_event_file *file)
3743 char *fields_str, *field_str;
3744 unsigned int i, j = 1;
3747 ret = create_hitcount_val(hist_data);
3751 fields_str = hist_data->attrs->vals_str;
3755 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3756 j < TRACING_MAP_VALS_MAX; i++) {
3757 field_str = strsep(&fields_str, ",");
3761 if (strcmp(field_str, "hitcount") == 0)
3764 ret = create_val_field(hist_data, j++, file, field_str);
3769 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3775 static int create_key_field(struct hist_trigger_data *hist_data,
3776 unsigned int key_idx,
3777 unsigned int key_offset,
3778 struct trace_event_file *file,
3781 struct trace_array *tr = hist_data->event_file->tr;
3782 struct hist_field *hist_field = NULL;
3783 unsigned long flags = 0;
3784 unsigned int key_size;
3787 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
3790 flags |= HIST_FIELD_FL_KEY;
3792 if (strcmp(field_str, "stacktrace") == 0) {
3793 flags |= HIST_FIELD_FL_STACKTRACE;
3794 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
3795 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
3797 hist_field = parse_expr(hist_data, file, field_str, flags,
3799 if (IS_ERR(hist_field)) {
3800 ret = PTR_ERR(hist_field);
3804 if (field_has_hist_vars(hist_field, 0)) {
3805 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
3806 destroy_hist_field(hist_field, 0);
3811 key_size = hist_field->size;
3814 hist_data->fields[key_idx] = hist_field;
3816 key_size = ALIGN(key_size, sizeof(u64));
3817 hist_data->fields[key_idx]->size = key_size;
3818 hist_data->fields[key_idx]->offset = key_offset;
3820 hist_data->key_size += key_size;
3822 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3827 hist_data->n_keys++;
3828 hist_data->n_fields++;
3830 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
3838 static int create_key_fields(struct hist_trigger_data *hist_data,
3839 struct trace_event_file *file)
3841 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
3842 char *fields_str, *field_str;
3845 fields_str = hist_data->attrs->keys_str;
3849 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
3850 field_str = strsep(&fields_str, ",");
3853 ret = create_key_field(hist_data, i, key_offset,
3868 static int create_var_fields(struct hist_trigger_data *hist_data,
3869 struct trace_event_file *file)
3871 unsigned int i, j = hist_data->n_vals;
3874 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
3876 for (i = 0; i < n_vars; i++) {
3877 char *var_name = hist_data->attrs->var_defs.name[i];
3878 char *expr = hist_data->attrs->var_defs.expr[i];
3880 ret = create_var_field(hist_data, j++, file, var_name, expr);
3888 static void free_var_defs(struct hist_trigger_data *hist_data)
3892 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3893 kfree(hist_data->attrs->var_defs.name[i]);
3894 kfree(hist_data->attrs->var_defs.expr[i]);
3897 hist_data->attrs->var_defs.n_vars = 0;
3900 static int parse_var_defs(struct hist_trigger_data *hist_data)
3902 struct trace_array *tr = hist_data->event_file->tr;
3903 char *s, *str, *var_name, *field_str;
3904 unsigned int i, j, n_vars = 0;
3907 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
3908 str = hist_data->attrs->assignment_str[i];
3909 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
3910 field_str = strsep(&str, ",");
3914 var_name = strsep(&field_str, "=");
3915 if (!var_name || !field_str) {
3916 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
3922 if (n_vars == TRACING_MAP_VARS_MAX) {
3923 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
3928 s = kstrdup(var_name, GFP_KERNEL);
3933 hist_data->attrs->var_defs.name[n_vars] = s;
3935 s = kstrdup(field_str, GFP_KERNEL);
3940 hist_data->attrs->var_defs.expr[n_vars++] = s;
3942 hist_data->attrs->var_defs.n_vars = n_vars;
3948 free_var_defs(hist_data);
3953 static int create_hist_fields(struct hist_trigger_data *hist_data,
3954 struct trace_event_file *file)
3958 ret = parse_var_defs(hist_data);
3962 ret = create_val_fields(hist_data, file);
3966 ret = create_var_fields(hist_data, file);
3970 ret = create_key_fields(hist_data, file);
3974 free_var_defs(hist_data);
3979 static int is_descending(struct trace_array *tr, const char *str)
3984 if (strcmp(str, "descending") == 0)
3987 if (strcmp(str, "ascending") == 0)
3990 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
3995 static int create_sort_keys(struct hist_trigger_data *hist_data)
3997 struct trace_array *tr = hist_data->event_file->tr;
3998 char *fields_str = hist_data->attrs->sort_key_str;
3999 struct tracing_map_sort_key *sort_key;
4000 int descending, ret = 0;
4001 unsigned int i, j, k;
4003 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4008 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4009 struct hist_field *hist_field;
4010 char *field_str, *field_name;
4011 const char *test_name;
4013 sort_key = &hist_data->sort_keys[i];
4015 field_str = strsep(&fields_str, ",");
4021 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4025 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4026 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4031 field_name = strsep(&field_str, ".");
4032 if (!field_name || !*field_name) {
4034 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4038 if (strcmp(field_name, "hitcount") == 0) {
4039 descending = is_descending(tr, field_str);
4040 if (descending < 0) {
4044 sort_key->descending = descending;
4048 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4051 hist_field = hist_data->fields[j];
4052 if (hist_field->flags & HIST_FIELD_FL_VAR)
4057 test_name = hist_field_name(hist_field, 0);
4059 if (strcmp(field_name, test_name) == 0) {
4060 sort_key->field_idx = idx;
4061 descending = is_descending(tr, field_str);
4062 if (descending < 0) {
4066 sort_key->descending = descending;
4070 if (j == hist_data->n_fields) {
4072 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4077 hist_data->n_sort_keys = i;
4082 static void destroy_actions(struct hist_trigger_data *hist_data)
4086 for (i = 0; i < hist_data->n_actions; i++) {
4087 struct action_data *data = hist_data->actions[i];
4089 if (data->handler == HANDLER_ONMATCH)
4090 onmatch_destroy(data);
4091 else if (data->handler == HANDLER_ONMAX ||
4092 data->handler == HANDLER_ONCHANGE)
4093 track_data_destroy(hist_data, data);
4099 static int parse_actions(struct hist_trigger_data *hist_data)
4101 struct trace_array *tr = hist_data->event_file->tr;
4102 struct action_data *data;
4108 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4109 str = hist_data->attrs->action_str[i];
4111 if ((len = str_has_prefix(str, "onmatch("))) {
4112 char *action_str = str + len;
4114 data = onmatch_parse(tr, action_str);
4116 ret = PTR_ERR(data);
4119 } else if ((len = str_has_prefix(str, "onmax("))) {
4120 char *action_str = str + len;
4122 data = track_data_parse(hist_data, action_str,
4125 ret = PTR_ERR(data);
4128 } else if ((len = str_has_prefix(str, "onchange("))) {
4129 char *action_str = str + len;
4131 data = track_data_parse(hist_data, action_str,
4134 ret = PTR_ERR(data);
4142 hist_data->actions[hist_data->n_actions++] = data;
4148 static int create_actions(struct hist_trigger_data *hist_data)
4150 struct action_data *data;
4154 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4155 data = hist_data->actions[i];
4157 if (data->handler == HANDLER_ONMATCH) {
4158 ret = onmatch_create(hist_data, data);
4161 } else if (data->handler == HANDLER_ONMAX ||
4162 data->handler == HANDLER_ONCHANGE) {
4163 ret = track_data_create(hist_data, data);
4175 static void print_actions(struct seq_file *m,
4176 struct hist_trigger_data *hist_data,
4177 struct tracing_map_elt *elt)
4181 for (i = 0; i < hist_data->n_actions; i++) {
4182 struct action_data *data = hist_data->actions[i];
4184 if (data->action == ACTION_SNAPSHOT)
4187 if (data->handler == HANDLER_ONMAX ||
4188 data->handler == HANDLER_ONCHANGE)
4189 track_data_print(m, hist_data, elt, data);
4193 static void print_action_spec(struct seq_file *m,
4194 struct hist_trigger_data *hist_data,
4195 struct action_data *data)
4199 if (data->action == ACTION_SAVE) {
4200 for (i = 0; i < hist_data->n_save_vars; i++) {
4201 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4202 if (i < hist_data->n_save_vars - 1)
4205 } else if (data->action == ACTION_TRACE) {
4206 if (data->use_trace_keyword)
4207 seq_printf(m, "%s", data->synth_event_name);
4208 for (i = 0; i < data->n_params; i++) {
4209 if (i || data->use_trace_keyword)
4211 seq_printf(m, "%s", data->params[i]);
4216 static void print_track_data_spec(struct seq_file *m,
4217 struct hist_trigger_data *hist_data,
4218 struct action_data *data)
4220 if (data->handler == HANDLER_ONMAX)
4221 seq_puts(m, ":onmax(");
4222 else if (data->handler == HANDLER_ONCHANGE)
4223 seq_puts(m, ":onchange(");
4224 seq_printf(m, "%s", data->track_data.var_str);
4225 seq_printf(m, ").%s(", data->action_name);
4227 print_action_spec(m, hist_data, data);
4232 static void print_onmatch_spec(struct seq_file *m,
4233 struct hist_trigger_data *hist_data,
4234 struct action_data *data)
4236 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4237 data->match_data.event);
4239 seq_printf(m, "%s(", data->action_name);
4241 print_action_spec(m, hist_data, data);
4246 static bool actions_match(struct hist_trigger_data *hist_data,
4247 struct hist_trigger_data *hist_data_test)
4251 if (hist_data->n_actions != hist_data_test->n_actions)
4254 for (i = 0; i < hist_data->n_actions; i++) {
4255 struct action_data *data = hist_data->actions[i];
4256 struct action_data *data_test = hist_data_test->actions[i];
4257 char *action_name, *action_name_test;
4259 if (data->handler != data_test->handler)
4261 if (data->action != data_test->action)
4264 if (data->n_params != data_test->n_params)
4267 for (j = 0; j < data->n_params; j++) {
4268 if (strcmp(data->params[j], data_test->params[j]) != 0)
4272 if (data->use_trace_keyword)
4273 action_name = data->synth_event_name;
4275 action_name = data->action_name;
4277 if (data_test->use_trace_keyword)
4278 action_name_test = data_test->synth_event_name;
4280 action_name_test = data_test->action_name;
4282 if (strcmp(action_name, action_name_test) != 0)
4285 if (data->handler == HANDLER_ONMATCH) {
4286 if (strcmp(data->match_data.event_system,
4287 data_test->match_data.event_system) != 0)
4289 if (strcmp(data->match_data.event,
4290 data_test->match_data.event) != 0)
4292 } else if (data->handler == HANDLER_ONMAX ||
4293 data->handler == HANDLER_ONCHANGE) {
4294 if (strcmp(data->track_data.var_str,
4295 data_test->track_data.var_str) != 0)
4304 static void print_actions_spec(struct seq_file *m,
4305 struct hist_trigger_data *hist_data)
4309 for (i = 0; i < hist_data->n_actions; i++) {
4310 struct action_data *data = hist_data->actions[i];
4312 if (data->handler == HANDLER_ONMATCH)
4313 print_onmatch_spec(m, hist_data, data);
4314 else if (data->handler == HANDLER_ONMAX ||
4315 data->handler == HANDLER_ONCHANGE)
4316 print_track_data_spec(m, hist_data, data);
4320 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4324 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4325 kfree(hist_data->field_var_hists[i]->cmd);
4326 kfree(hist_data->field_var_hists[i]);
4330 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4335 destroy_hist_trigger_attrs(hist_data->attrs);
4336 destroy_hist_fields(hist_data);
4337 tracing_map_destroy(hist_data->map);
4339 destroy_actions(hist_data);
4340 destroy_field_vars(hist_data);
4341 destroy_field_var_hists(hist_data);
4346 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4348 struct tracing_map *map = hist_data->map;
4349 struct ftrace_event_field *field;
4350 struct hist_field *hist_field;
4353 for_each_hist_field(i, hist_data) {
4354 hist_field = hist_data->fields[i];
4355 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4356 tracing_map_cmp_fn_t cmp_fn;
4358 field = hist_field->field;
4360 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4361 cmp_fn = tracing_map_cmp_none;
4363 cmp_fn = tracing_map_cmp_num(hist_field->size,
4364 hist_field->is_signed);
4365 else if (is_string_field(field))
4366 cmp_fn = tracing_map_cmp_string;
4368 cmp_fn = tracing_map_cmp_num(field->size,
4370 idx = tracing_map_add_key_field(map,
4373 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4374 idx = tracing_map_add_sum_field(map);
4379 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4380 idx = tracing_map_add_var(map);
4383 hist_field->var.idx = idx;
4384 hist_field->var.hist_data = hist_data;
4391 static struct hist_trigger_data *
4392 create_hist_data(unsigned int map_bits,
4393 struct hist_trigger_attrs *attrs,
4394 struct trace_event_file *file,
4397 const struct tracing_map_ops *map_ops = NULL;
4398 struct hist_trigger_data *hist_data;
4401 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4403 return ERR_PTR(-ENOMEM);
4405 hist_data->attrs = attrs;
4406 hist_data->remove = remove;
4407 hist_data->event_file = file;
4409 ret = parse_actions(hist_data);
4413 ret = create_hist_fields(hist_data, file);
4417 ret = create_sort_keys(hist_data);
4421 map_ops = &hist_trigger_elt_data_ops;
4423 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4424 map_ops, hist_data);
4425 if (IS_ERR(hist_data->map)) {
4426 ret = PTR_ERR(hist_data->map);
4427 hist_data->map = NULL;
4431 ret = create_tracing_map_fields(hist_data);
4437 hist_data->attrs = NULL;
4439 destroy_hist_data(hist_data);
4441 hist_data = ERR_PTR(ret);
4446 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4447 struct tracing_map_elt *elt, void *rec,
4448 struct ring_buffer_event *rbe,
4451 struct hist_elt_data *elt_data;
4452 struct hist_field *hist_field;
4453 unsigned int i, var_idx;
4456 elt_data = elt->private_data;
4457 elt_data->var_ref_vals = var_ref_vals;
4459 for_each_hist_val_field(i, hist_data) {
4460 hist_field = hist_data->fields[i];
4461 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4462 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4463 var_idx = hist_field->var.idx;
4465 if (hist_field->flags & HIST_FIELD_FL_STRING) {
4466 unsigned int str_start, var_str_idx, idx;
4467 char *str, *val_str;
4469 str_start = hist_data->n_field_var_str +
4470 hist_data->n_save_var_str;
4471 var_str_idx = hist_field->var_str_idx;
4472 idx = str_start + var_str_idx;
4474 str = elt_data->field_var_str[idx];
4475 val_str = (char *)(uintptr_t)hist_val;
4476 strscpy(str, val_str, hist_field->size);
4478 hist_val = (u64)(uintptr_t)str;
4480 tracing_map_set_var(elt, var_idx, hist_val);
4483 tracing_map_update_sum(elt, i, hist_val);
4486 for_each_hist_key_field(i, hist_data) {
4487 hist_field = hist_data->fields[i];
4488 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4489 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4490 var_idx = hist_field->var.idx;
4491 tracing_map_set_var(elt, var_idx, hist_val);
4495 update_field_vars(hist_data, elt, rbe, rec);
4498 static inline void add_to_key(char *compound_key, void *key,
4499 struct hist_field *key_field, void *rec)
4501 size_t size = key_field->size;
4503 if (key_field->flags & HIST_FIELD_FL_STRING) {
4504 struct ftrace_event_field *field;
4506 field = key_field->field;
4507 if (field->filter_type == FILTER_DYN_STRING)
4508 size = *(u32 *)(rec + field->offset) >> 16;
4509 else if (field->filter_type == FILTER_STATIC_STRING)
4512 /* ensure NULL-termination */
4513 if (size > key_field->size - 1)
4514 size = key_field->size - 1;
4516 strncpy(compound_key + key_field->offset, (char *)key, size);
4518 memcpy(compound_key + key_field->offset, key, size);
4522 hist_trigger_actions(struct hist_trigger_data *hist_data,
4523 struct tracing_map_elt *elt, void *rec,
4524 struct ring_buffer_event *rbe, void *key,
4527 struct action_data *data;
4530 for (i = 0; i < hist_data->n_actions; i++) {
4531 data = hist_data->actions[i];
4532 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4536 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4537 struct ring_buffer_event *rbe)
4539 struct hist_trigger_data *hist_data = data->private_data;
4540 bool use_compound_key = (hist_data->n_keys > 1);
4541 unsigned long entries[HIST_STACKTRACE_DEPTH];
4542 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4543 char compound_key[HIST_KEY_SIZE_MAX];
4544 struct tracing_map_elt *elt = NULL;
4545 struct hist_field *key_field;
4550 memset(compound_key, 0, hist_data->key_size);
4552 for_each_hist_key_field(i, hist_data) {
4553 key_field = hist_data->fields[i];
4555 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4556 memset(entries, 0, HIST_STACKTRACE_SIZE);
4557 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
4558 HIST_STACKTRACE_SKIP);
4561 field_contents = key_field->fn(key_field, elt, rbe, rec);
4562 if (key_field->flags & HIST_FIELD_FL_STRING) {
4563 key = (void *)(unsigned long)field_contents;
4564 use_compound_key = true;
4566 key = (void *)&field_contents;
4569 if (use_compound_key)
4570 add_to_key(compound_key, key, key_field, rec);
4573 if (use_compound_key)
4576 if (hist_data->n_var_refs &&
4577 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4580 elt = tracing_map_insert(hist_data->map, key);
4584 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4586 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4587 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
4590 static void hist_trigger_stacktrace_print(struct seq_file *m,
4591 unsigned long *stacktrace_entries,
4592 unsigned int max_entries)
4594 char str[KSYM_SYMBOL_LEN];
4595 unsigned int spaces = 8;
4598 for (i = 0; i < max_entries; i++) {
4599 if (!stacktrace_entries[i])
4602 seq_printf(m, "%*c", 1 + spaces, ' ');
4603 sprint_symbol(str, stacktrace_entries[i]);
4604 seq_printf(m, "%s\n", str);
4608 static void hist_trigger_print_key(struct seq_file *m,
4609 struct hist_trigger_data *hist_data,
4611 struct tracing_map_elt *elt)
4613 struct hist_field *key_field;
4614 char str[KSYM_SYMBOL_LEN];
4615 bool multiline = false;
4616 const char *field_name;
4622 for_each_hist_key_field(i, hist_data) {
4623 key_field = hist_data->fields[i];
4625 if (i > hist_data->n_vals)
4628 field_name = hist_field_name(key_field, 0);
4630 if (key_field->flags & HIST_FIELD_FL_HEX) {
4631 uval = *(u64 *)(key + key_field->offset);
4632 seq_printf(m, "%s: %llx", field_name, uval);
4633 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4634 uval = *(u64 *)(key + key_field->offset);
4635 sprint_symbol_no_offset(str, uval);
4636 seq_printf(m, "%s: [%llx] %-45s", field_name,
4638 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4639 uval = *(u64 *)(key + key_field->offset);
4640 sprint_symbol(str, uval);
4641 seq_printf(m, "%s: [%llx] %-55s", field_name,
4643 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4644 struct hist_elt_data *elt_data = elt->private_data;
4647 if (WARN_ON_ONCE(!elt_data))
4650 comm = elt_data->comm;
4652 uval = *(u64 *)(key + key_field->offset);
4653 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4655 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4656 const char *syscall_name;
4658 uval = *(u64 *)(key + key_field->offset);
4659 syscall_name = get_syscall_name(uval);
4661 syscall_name = "unknown_syscall";
4663 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4664 syscall_name, uval);
4665 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4666 seq_puts(m, "stacktrace:\n");
4667 hist_trigger_stacktrace_print(m,
4668 key + key_field->offset,
4669 HIST_STACKTRACE_DEPTH);
4671 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4672 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4673 *(u64 *)(key + key_field->offset));
4674 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4675 seq_printf(m, "%s: %-50s", field_name,
4676 (char *)(key + key_field->offset));
4678 uval = *(u64 *)(key + key_field->offset);
4679 seq_printf(m, "%s: %10llu", field_name, uval);
4689 static void hist_trigger_entry_print(struct seq_file *m,
4690 struct hist_trigger_data *hist_data,
4692 struct tracing_map_elt *elt)
4694 const char *field_name;
4697 hist_trigger_print_key(m, hist_data, key, elt);
4699 seq_printf(m, " hitcount: %10llu",
4700 tracing_map_read_sum(elt, HITCOUNT_IDX));
4702 for (i = 1; i < hist_data->n_vals; i++) {
4703 field_name = hist_field_name(hist_data->fields[i], 0);
4705 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4706 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4709 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4710 seq_printf(m, " %s: %10llx", field_name,
4711 tracing_map_read_sum(elt, i));
4713 seq_printf(m, " %s: %10llu", field_name,
4714 tracing_map_read_sum(elt, i));
4718 print_actions(m, hist_data, elt);
4723 static int print_entries(struct seq_file *m,
4724 struct hist_trigger_data *hist_data)
4726 struct tracing_map_sort_entry **sort_entries = NULL;
4727 struct tracing_map *map = hist_data->map;
4730 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4731 hist_data->n_sort_keys,
4736 for (i = 0; i < n_entries; i++)
4737 hist_trigger_entry_print(m, hist_data,
4738 sort_entries[i]->key,
4739 sort_entries[i]->elt);
4741 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4746 static void hist_trigger_show(struct seq_file *m,
4747 struct event_trigger_data *data, int n)
4749 struct hist_trigger_data *hist_data;
4753 seq_puts(m, "\n\n");
4755 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4756 data->ops->print(m, data->ops, data);
4757 seq_puts(m, "#\n\n");
4759 hist_data = data->private_data;
4760 n_entries = print_entries(m, hist_data);
4764 track_data_snapshot_print(m, hist_data);
4766 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4767 (u64)atomic64_read(&hist_data->map->hits),
4768 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4771 static int hist_show(struct seq_file *m, void *v)
4773 struct event_trigger_data *data;
4774 struct trace_event_file *event_file;
4777 mutex_lock(&event_mutex);
4779 event_file = event_file_data(m->private);
4780 if (unlikely(!event_file)) {
4785 list_for_each_entry(data, &event_file->triggers, list) {
4786 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4787 hist_trigger_show(m, data, n++);
4791 mutex_unlock(&event_mutex);
4796 static int event_hist_open(struct inode *inode, struct file *file)
4800 ret = security_locked_down(LOCKDOWN_TRACEFS);
4804 return single_open(file, hist_show, file);
4807 const struct file_operations event_hist_fops = {
4808 .open = event_hist_open,
4810 .llseek = seq_lseek,
4811 .release = single_release,
4814 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
4815 static void hist_field_debug_show_flags(struct seq_file *m,
4816 unsigned long flags)
4818 seq_puts(m, " flags:\n");
4820 if (flags & HIST_FIELD_FL_KEY)
4821 seq_puts(m, " HIST_FIELD_FL_KEY\n");
4822 else if (flags & HIST_FIELD_FL_HITCOUNT)
4823 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
4824 else if (flags & HIST_FIELD_FL_VAR)
4825 seq_puts(m, " HIST_FIELD_FL_VAR\n");
4826 else if (flags & HIST_FIELD_FL_VAR_REF)
4827 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
4829 seq_puts(m, " VAL: normal u64 value\n");
4831 if (flags & HIST_FIELD_FL_ALIAS)
4832 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
4835 static int hist_field_debug_show(struct seq_file *m,
4836 struct hist_field *field, unsigned long flags)
4838 if ((field->flags & flags) != flags) {
4839 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
4843 hist_field_debug_show_flags(m, field->flags);
4845 seq_printf(m, " ftrace_event_field name: %s\n",
4846 field->field->name);
4848 if (field->flags & HIST_FIELD_FL_VAR) {
4849 seq_printf(m, " var.name: %s\n", field->var.name);
4850 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4854 if (field->flags & HIST_FIELD_FL_ALIAS)
4855 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4856 field->var_ref_idx);
4858 if (field->flags & HIST_FIELD_FL_VAR_REF) {
4859 seq_printf(m, " name: %s\n", field->name);
4860 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4862 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
4863 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4864 field->var_ref_idx);
4866 seq_printf(m, " system: %s\n", field->system);
4867 if (field->event_name)
4868 seq_printf(m, " event_name: %s\n", field->event_name);
4871 seq_printf(m, " type: %s\n", field->type);
4872 seq_printf(m, " size: %u\n", field->size);
4873 seq_printf(m, " is_signed: %u\n", field->is_signed);
4878 static int field_var_debug_show(struct seq_file *m,
4879 struct field_var *field_var, unsigned int i,
4882 const char *vars_name = save_vars ? "save_vars" : "field_vars";
4883 struct hist_field *field;
4886 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
4888 field = field_var->var;
4890 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
4892 hist_field_debug_show_flags(m, field->flags);
4893 seq_printf(m, " var.name: %s\n", field->var.name);
4894 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4897 field = field_var->val;
4899 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
4901 seq_printf(m, " ftrace_event_field name: %s\n",
4902 field->field->name);
4908 seq_printf(m, " type: %s\n", field->type);
4909 seq_printf(m, " size: %u\n", field->size);
4910 seq_printf(m, " is_signed: %u\n", field->is_signed);
4915 static int hist_action_debug_show(struct seq_file *m,
4916 struct action_data *data, int i)
4920 if (data->handler == HANDLER_ONMAX ||
4921 data->handler == HANDLER_ONCHANGE) {
4922 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
4923 ret = hist_field_debug_show(m, data->track_data.var_ref,
4924 HIST_FIELD_FL_VAR_REF);
4928 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
4929 ret = hist_field_debug_show(m, data->track_data.track_var,
4935 if (data->handler == HANDLER_ONMATCH) {
4936 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
4937 i, data->match_data.event_system);
4938 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
4939 i, data->match_data.event);
4945 static int hist_actions_debug_show(struct seq_file *m,
4946 struct hist_trigger_data *hist_data)
4950 if (hist_data->n_actions)
4951 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
4953 for (i = 0; i < hist_data->n_actions; i++) {
4954 struct action_data *action = hist_data->actions[i];
4956 ret = hist_action_debug_show(m, action, i);
4961 if (hist_data->n_save_vars)
4962 seq_puts(m, "\n save action variables (save() params):\n");
4964 for (i = 0; i < hist_data->n_save_vars; i++) {
4965 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
4973 static void hist_trigger_debug_show(struct seq_file *m,
4974 struct event_trigger_data *data, int n)
4976 struct hist_trigger_data *hist_data;
4980 seq_puts(m, "\n\n");
4982 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4983 data->ops->print(m, data->ops, data);
4984 seq_puts(m, "#\n\n");
4986 hist_data = data->private_data;
4988 seq_printf(m, "hist_data: %p\n\n", hist_data);
4989 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
4990 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
4991 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
4993 seq_puts(m, "\n val fields:\n\n");
4995 seq_puts(m, " hist_data->fields[0]:\n");
4996 ret = hist_field_debug_show(m, hist_data->fields[0],
4997 HIST_FIELD_FL_HITCOUNT);
5001 for (i = 1; i < hist_data->n_vals; i++) {
5002 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5003 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5008 seq_puts(m, "\n key fields:\n");
5010 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5011 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5012 ret = hist_field_debug_show(m, hist_data->fields[i],
5018 if (hist_data->n_var_refs)
5019 seq_puts(m, "\n variable reference fields:\n");
5021 for (i = 0; i < hist_data->n_var_refs; i++) {
5022 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5023 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5024 HIST_FIELD_FL_VAR_REF);
5029 if (hist_data->n_field_vars)
5030 seq_puts(m, "\n field variables:\n");
5032 for (i = 0; i < hist_data->n_field_vars; i++) {
5033 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5038 ret = hist_actions_debug_show(m, hist_data);
5043 static int hist_debug_show(struct seq_file *m, void *v)
5045 struct event_trigger_data *data;
5046 struct trace_event_file *event_file;
5049 mutex_lock(&event_mutex);
5051 event_file = event_file_data(m->private);
5052 if (unlikely(!event_file)) {
5057 list_for_each_entry(data, &event_file->triggers, list) {
5058 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5059 hist_trigger_debug_show(m, data, n++);
5063 mutex_unlock(&event_mutex);
5068 static int event_hist_debug_open(struct inode *inode, struct file *file)
5072 ret = security_locked_down(LOCKDOWN_TRACEFS);
5076 return single_open(file, hist_debug_show, file);
5079 const struct file_operations event_hist_debug_fops = {
5080 .open = event_hist_debug_open,
5082 .llseek = seq_lseek,
5083 .release = single_release,
5087 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5089 const char *field_name = hist_field_name(hist_field, 0);
5091 if (hist_field->var.name)
5092 seq_printf(m, "%s=", hist_field->var.name);
5094 if (hist_field->flags & HIST_FIELD_FL_CPU)
5095 seq_puts(m, "common_cpu");
5096 else if (field_name) {
5097 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5098 hist_field->flags & HIST_FIELD_FL_ALIAS)
5100 seq_printf(m, "%s", field_name);
5101 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5102 seq_puts(m, "common_timestamp");
5104 if (hist_field->flags) {
5105 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5106 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5107 const char *flags = get_hist_field_flags(hist_field);
5110 seq_printf(m, ".%s", flags);
5115 static int event_hist_trigger_print(struct seq_file *m,
5116 struct event_trigger_ops *ops,
5117 struct event_trigger_data *data)
5119 struct hist_trigger_data *hist_data = data->private_data;
5120 struct hist_field *field;
5121 bool have_var = false;
5124 seq_puts(m, "hist:");
5127 seq_printf(m, "%s:", data->name);
5129 seq_puts(m, "keys=");
5131 for_each_hist_key_field(i, hist_data) {
5132 field = hist_data->fields[i];
5134 if (i > hist_data->n_vals)
5137 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5138 seq_puts(m, "stacktrace");
5140 hist_field_print(m, field);
5143 seq_puts(m, ":vals=");
5145 for_each_hist_val_field(i, hist_data) {
5146 field = hist_data->fields[i];
5147 if (field->flags & HIST_FIELD_FL_VAR) {
5152 if (i == HITCOUNT_IDX)
5153 seq_puts(m, "hitcount");
5156 hist_field_print(m, field);
5165 for_each_hist_val_field(i, hist_data) {
5166 field = hist_data->fields[i];
5168 if (field->flags & HIST_FIELD_FL_VAR) {
5171 hist_field_print(m, field);
5176 seq_puts(m, ":sort=");
5178 for (i = 0; i < hist_data->n_sort_keys; i++) {
5179 struct tracing_map_sort_key *sort_key;
5180 unsigned int idx, first_key_idx;
5183 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5185 sort_key = &hist_data->sort_keys[i];
5186 idx = sort_key->field_idx;
5188 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5194 if (idx == HITCOUNT_IDX)
5195 seq_puts(m, "hitcount");
5197 if (idx >= first_key_idx)
5198 idx += hist_data->n_vars;
5199 hist_field_print(m, hist_data->fields[idx]);
5202 if (sort_key->descending)
5203 seq_puts(m, ".descending");
5205 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5206 if (hist_data->enable_timestamps)
5207 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5209 print_actions_spec(m, hist_data);
5211 if (data->filter_str)
5212 seq_printf(m, " if %s", data->filter_str);
5215 seq_puts(m, " [paused]");
5217 seq_puts(m, " [active]");
5224 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5225 struct event_trigger_data *data)
5227 struct hist_trigger_data *hist_data = data->private_data;
5229 if (!data->ref && hist_data->attrs->name)
5230 save_named_trigger(hist_data->attrs->name, data);
5237 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5239 struct trace_event_file *file;
5244 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5245 file = hist_data->field_var_hists[i]->hist_data->event_file;
5246 cmd = hist_data->field_var_hists[i]->cmd;
5247 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5248 "!hist", "hist", cmd);
5252 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5253 struct event_trigger_data *data)
5255 struct hist_trigger_data *hist_data = data->private_data;
5257 if (WARN_ON_ONCE(data->ref <= 0))
5263 del_named_trigger(data);
5265 trigger_data_free(data);
5267 remove_hist_vars(hist_data);
5269 unregister_field_var_hists(hist_data);
5271 destroy_hist_data(hist_data);
5275 static struct event_trigger_ops event_hist_trigger_ops = {
5276 .func = event_hist_trigger,
5277 .print = event_hist_trigger_print,
5278 .init = event_hist_trigger_init,
5279 .free = event_hist_trigger_free,
5282 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5283 struct event_trigger_data *data)
5287 save_named_trigger(data->named_data->name, data);
5289 event_hist_trigger_init(ops, data->named_data);
5294 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5295 struct event_trigger_data *data)
5297 if (WARN_ON_ONCE(data->ref <= 0))
5300 event_hist_trigger_free(ops, data->named_data);
5304 del_named_trigger(data);
5305 trigger_data_free(data);
5309 static struct event_trigger_ops event_hist_trigger_named_ops = {
5310 .func = event_hist_trigger,
5311 .print = event_hist_trigger_print,
5312 .init = event_hist_trigger_named_init,
5313 .free = event_hist_trigger_named_free,
5316 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5319 return &event_hist_trigger_ops;
5322 static void hist_clear(struct event_trigger_data *data)
5324 struct hist_trigger_data *hist_data = data->private_data;
5327 pause_named_trigger(data);
5329 tracepoint_synchronize_unregister();
5331 tracing_map_clear(hist_data->map);
5334 unpause_named_trigger(data);
5337 static bool compatible_field(struct ftrace_event_field *field,
5338 struct ftrace_event_field *test_field)
5340 if (field == test_field)
5342 if (field == NULL || test_field == NULL)
5344 if (strcmp(field->name, test_field->name) != 0)
5346 if (strcmp(field->type, test_field->type) != 0)
5348 if (field->size != test_field->size)
5350 if (field->is_signed != test_field->is_signed)
5356 static bool hist_trigger_match(struct event_trigger_data *data,
5357 struct event_trigger_data *data_test,
5358 struct event_trigger_data *named_data,
5361 struct tracing_map_sort_key *sort_key, *sort_key_test;
5362 struct hist_trigger_data *hist_data, *hist_data_test;
5363 struct hist_field *key_field, *key_field_test;
5366 if (named_data && (named_data != data_test) &&
5367 (named_data != data_test->named_data))
5370 if (!named_data && is_named_trigger(data_test))
5373 hist_data = data->private_data;
5374 hist_data_test = data_test->private_data;
5376 if (hist_data->n_vals != hist_data_test->n_vals ||
5377 hist_data->n_fields != hist_data_test->n_fields ||
5378 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5381 if (!ignore_filter) {
5382 if ((data->filter_str && !data_test->filter_str) ||
5383 (!data->filter_str && data_test->filter_str))
5387 for_each_hist_field(i, hist_data) {
5388 key_field = hist_data->fields[i];
5389 key_field_test = hist_data_test->fields[i];
5391 if (key_field->flags != key_field_test->flags)
5393 if (!compatible_field(key_field->field, key_field_test->field))
5395 if (key_field->offset != key_field_test->offset)
5397 if (key_field->size != key_field_test->size)
5399 if (key_field->is_signed != key_field_test->is_signed)
5401 if (!!key_field->var.name != !!key_field_test->var.name)
5403 if (key_field->var.name &&
5404 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5408 for (i = 0; i < hist_data->n_sort_keys; i++) {
5409 sort_key = &hist_data->sort_keys[i];
5410 sort_key_test = &hist_data_test->sort_keys[i];
5412 if (sort_key->field_idx != sort_key_test->field_idx ||
5413 sort_key->descending != sort_key_test->descending)
5417 if (!ignore_filter && data->filter_str &&
5418 (strcmp(data->filter_str, data_test->filter_str) != 0))
5421 if (!actions_match(hist_data, hist_data_test))
5427 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5428 struct event_trigger_data *data,
5429 struct trace_event_file *file)
5431 struct hist_trigger_data *hist_data = data->private_data;
5432 struct event_trigger_data *test, *named_data = NULL;
5433 struct trace_array *tr = file->tr;
5436 if (hist_data->attrs->name) {
5437 named_data = find_named_trigger(hist_data->attrs->name);
5439 if (!hist_trigger_match(data, named_data, named_data,
5441 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5448 if (hist_data->attrs->name && !named_data)
5451 lockdep_assert_held(&event_mutex);
5453 list_for_each_entry(test, &file->triggers, list) {
5454 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5455 if (!hist_trigger_match(data, test, named_data, false))
5457 if (hist_data->attrs->pause)
5458 test->paused = true;
5459 else if (hist_data->attrs->cont)
5460 test->paused = false;
5461 else if (hist_data->attrs->clear)
5464 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5471 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5472 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5477 if (hist_data->attrs->pause)
5478 data->paused = true;
5481 data->private_data = named_data->private_data;
5482 set_named_trigger_data(data, named_data);
5483 data->ops = &event_hist_trigger_named_ops;
5486 if (data->ops->init) {
5487 ret = data->ops->init(data->ops, data);
5492 if (hist_data->enable_timestamps) {
5493 char *clock = hist_data->attrs->clock;
5495 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5497 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5501 tracing_set_time_stamp_abs(file->tr, true);
5505 destroy_hist_data(hist_data);
5512 static int hist_trigger_enable(struct event_trigger_data *data,
5513 struct trace_event_file *file)
5517 list_add_tail_rcu(&data->list, &file->triggers);
5519 update_cond_flag(file);
5521 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5522 list_del_rcu(&data->list);
5523 update_cond_flag(file);
5530 static bool have_hist_trigger_match(struct event_trigger_data *data,
5531 struct trace_event_file *file)
5533 struct hist_trigger_data *hist_data = data->private_data;
5534 struct event_trigger_data *test, *named_data = NULL;
5537 lockdep_assert_held(&event_mutex);
5539 if (hist_data->attrs->name)
5540 named_data = find_named_trigger(hist_data->attrs->name);
5542 list_for_each_entry(test, &file->triggers, list) {
5543 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5544 if (hist_trigger_match(data, test, named_data, false)) {
5554 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5555 struct trace_event_file *file)
5557 struct hist_trigger_data *hist_data = data->private_data;
5558 struct event_trigger_data *test, *named_data = NULL;
5560 lockdep_assert_held(&event_mutex);
5562 if (hist_data->attrs->name)
5563 named_data = find_named_trigger(hist_data->attrs->name);
5565 list_for_each_entry(test, &file->triggers, list) {
5566 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5567 if (!hist_trigger_match(data, test, named_data, false))
5569 hist_data = test->private_data;
5570 if (check_var_refs(hist_data))
5579 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5580 struct event_trigger_data *data,
5581 struct trace_event_file *file)
5583 struct hist_trigger_data *hist_data = data->private_data;
5584 struct event_trigger_data *test, *named_data = NULL;
5585 bool unregistered = false;
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 unregistered = true;
5597 list_del_rcu(&test->list);
5598 trace_event_trigger_enable_disable(file, 0);
5599 update_cond_flag(file);
5604 if (unregistered && test->ops->free)
5605 test->ops->free(test->ops, test);
5607 if (hist_data->enable_timestamps) {
5608 if (!hist_data->remove || unregistered)
5609 tracing_set_time_stamp_abs(file->tr, false);
5613 static bool hist_file_check_refs(struct trace_event_file *file)
5615 struct hist_trigger_data *hist_data;
5616 struct event_trigger_data *test;
5618 lockdep_assert_held(&event_mutex);
5620 list_for_each_entry(test, &file->triggers, list) {
5621 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5622 hist_data = test->private_data;
5623 if (check_var_refs(hist_data))
5631 static void hist_unreg_all(struct trace_event_file *file)
5633 struct event_trigger_data *test, *n;
5634 struct hist_trigger_data *hist_data;
5635 struct synth_event *se;
5636 const char *se_name;
5638 lockdep_assert_held(&event_mutex);
5640 if (hist_file_check_refs(file))
5643 list_for_each_entry_safe(test, n, &file->triggers, list) {
5644 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5645 hist_data = test->private_data;
5646 list_del_rcu(&test->list);
5647 trace_event_trigger_enable_disable(file, 0);
5649 se_name = trace_event_name(file->event_call);
5650 se = find_synth_event(se_name);
5654 update_cond_flag(file);
5655 if (hist_data->enable_timestamps)
5656 tracing_set_time_stamp_abs(file->tr, false);
5657 if (test->ops->free)
5658 test->ops->free(test->ops, test);
5663 static int event_hist_trigger_func(struct event_command *cmd_ops,
5664 struct trace_event_file *file,
5665 char *glob, char *cmd, char *param)
5667 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5668 struct event_trigger_data *trigger_data;
5669 struct hist_trigger_attrs *attrs;
5670 struct event_trigger_ops *trigger_ops;
5671 struct hist_trigger_data *hist_data;
5672 struct synth_event *se;
5673 const char *se_name;
5674 bool remove = false;
5678 lockdep_assert_held(&event_mutex);
5680 if (glob && strlen(glob)) {
5682 last_cmd_set(file, param);
5692 * separate the trigger from the filter (k:v [if filter])
5693 * allowing for whitespace in the trigger
5695 p = trigger = param;
5697 p = strstr(p, "if");
5702 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5706 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
5708 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
5719 param = strstrip(p);
5720 trigger = strstrip(trigger);
5723 attrs = parse_hist_trigger_attrs(file->tr, trigger);
5725 return PTR_ERR(attrs);
5727 if (attrs->map_bits)
5728 hist_trigger_bits = attrs->map_bits;
5730 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5731 if (IS_ERR(hist_data)) {
5732 destroy_hist_trigger_attrs(attrs);
5733 return PTR_ERR(hist_data);
5736 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5738 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5739 if (!trigger_data) {
5744 trigger_data->count = -1;
5745 trigger_data->ops = trigger_ops;
5746 trigger_data->cmd_ops = cmd_ops;
5748 INIT_LIST_HEAD(&trigger_data->list);
5749 RCU_INIT_POINTER(trigger_data->filter, NULL);
5751 trigger_data->private_data = hist_data;
5753 /* if param is non-empty, it's supposed to be a filter */
5754 if (param && cmd_ops->set_filter) {
5755 ret = cmd_ops->set_filter(param, trigger_data, file);
5761 if (!have_hist_trigger_match(trigger_data, file))
5764 if (hist_trigger_check_refs(trigger_data, file)) {
5769 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5770 se_name = trace_event_name(file->event_call);
5771 se = find_synth_event(se_name);
5778 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5780 * The above returns on success the # of triggers registered,
5781 * but if it didn't register any it returns zero. Consider no
5782 * triggers registered a failure too.
5785 if (!(attrs->pause || attrs->cont || attrs->clear))
5791 if (get_named_trigger_data(trigger_data))
5794 if (has_hist_vars(hist_data))
5795 save_hist_vars(hist_data);
5797 ret = create_actions(hist_data);
5801 ret = tracing_map_init(hist_data->map);
5805 ret = hist_trigger_enable(trigger_data, file);
5809 se_name = trace_event_name(file->event_call);
5810 se = find_synth_event(se_name);
5813 /* Just return zero, not the number of registered triggers */
5821 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5823 if (cmd_ops->set_filter)
5824 cmd_ops->set_filter(NULL, trigger_data, NULL);
5826 remove_hist_vars(hist_data);
5828 kfree(trigger_data);
5830 destroy_hist_data(hist_data);
5834 static struct event_command trigger_hist_cmd = {
5836 .trigger_type = ETT_EVENT_HIST,
5837 .flags = EVENT_CMD_FL_NEEDS_REC,
5838 .func = event_hist_trigger_func,
5839 .reg = hist_register_trigger,
5840 .unreg = hist_unregister_trigger,
5841 .unreg_all = hist_unreg_all,
5842 .get_trigger_ops = event_hist_get_trigger_ops,
5843 .set_filter = set_trigger_filter,
5846 __init int register_trigger_hist_cmd(void)
5850 ret = register_event_command(&trigger_hist_cmd);
5857 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5858 struct ring_buffer_event *event)
5860 struct enable_trigger_data *enable_data = data->private_data;
5861 struct event_trigger_data *test;
5863 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
5864 lockdep_is_held(&event_mutex)) {
5865 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5866 if (enable_data->enable)
5867 test->paused = false;
5869 test->paused = true;
5875 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5876 struct ring_buffer_event *event)
5881 if (data->count != -1)
5884 hist_enable_trigger(data, rec, event);
5887 static struct event_trigger_ops hist_enable_trigger_ops = {
5888 .func = hist_enable_trigger,
5889 .print = event_enable_trigger_print,
5890 .init = event_trigger_init,
5891 .free = event_enable_trigger_free,
5894 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5895 .func = hist_enable_count_trigger,
5896 .print = event_enable_trigger_print,
5897 .init = event_trigger_init,
5898 .free = event_enable_trigger_free,
5901 static struct event_trigger_ops hist_disable_trigger_ops = {
5902 .func = hist_enable_trigger,
5903 .print = event_enable_trigger_print,
5904 .init = event_trigger_init,
5905 .free = event_enable_trigger_free,
5908 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5909 .func = hist_enable_count_trigger,
5910 .print = event_enable_trigger_print,
5911 .init = event_trigger_init,
5912 .free = event_enable_trigger_free,
5915 static struct event_trigger_ops *
5916 hist_enable_get_trigger_ops(char *cmd, char *param)
5918 struct event_trigger_ops *ops;
5921 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5924 ops = param ? &hist_enable_count_trigger_ops :
5925 &hist_enable_trigger_ops;
5927 ops = param ? &hist_disable_count_trigger_ops :
5928 &hist_disable_trigger_ops;
5933 static void hist_enable_unreg_all(struct trace_event_file *file)
5935 struct event_trigger_data *test, *n;
5937 list_for_each_entry_safe(test, n, &file->triggers, list) {
5938 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5939 list_del_rcu(&test->list);
5940 update_cond_flag(file);
5941 trace_event_trigger_enable_disable(file, 0);
5942 if (test->ops->free)
5943 test->ops->free(test->ops, test);
5948 static struct event_command trigger_hist_enable_cmd = {
5949 .name = ENABLE_HIST_STR,
5950 .trigger_type = ETT_HIST_ENABLE,
5951 .func = event_enable_trigger_func,
5952 .reg = event_enable_register_trigger,
5953 .unreg = event_enable_unregister_trigger,
5954 .unreg_all = hist_enable_unreg_all,
5955 .get_trigger_ops = hist_enable_get_trigger_ops,
5956 .set_filter = set_trigger_filter,
5959 static struct event_command trigger_hist_disable_cmd = {
5960 .name = DISABLE_HIST_STR,
5961 .trigger_type = ETT_HIST_ENABLE,
5962 .func = event_enable_trigger_func,
5963 .reg = event_enable_register_trigger,
5964 .unreg = event_enable_unregister_trigger,
5965 .unreg_all = hist_enable_unreg_all,
5966 .get_trigger_ops = hist_enable_get_trigger_ops,
5967 .set_filter = set_trigger_filter,
5970 static __init void unregister_trigger_hist_enable_disable_cmds(void)
5972 unregister_event_command(&trigger_hist_enable_cmd);
5973 unregister_event_command(&trigger_hist_disable_cmd);
5976 __init int register_trigger_hist_enable_disable_cmds(void)
5980 ret = register_event_command(&trigger_hist_enable_cmd);
5981 if (WARN_ON(ret < 0))
5983 ret = register_event_command(&trigger_hist_disable_cmd);
5984 if (WARN_ON(ret < 0))
5985 unregister_trigger_hist_enable_disable_cmds();