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[SYNTH_FIELDS_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 = "";
1090 if (WARN_ON_ONCE(!field))
1097 field_name = field->field->name;
1098 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1099 field->flags & HIST_FIELD_FL_ALIAS)
1100 field_name = hist_field_name(field->operands[0], ++level);
1101 else if (field->flags & HIST_FIELD_FL_CPU)
1102 field_name = "common_cpu";
1103 else if (field->flags & HIST_FIELD_FL_EXPR ||
1104 field->flags & HIST_FIELD_FL_VAR_REF) {
1105 if (field->system) {
1106 static char full_name[MAX_FILTER_STR_VAL];
1108 strcat(full_name, field->system);
1109 strcat(full_name, ".");
1110 strcat(full_name, field->event_name);
1111 strcat(full_name, ".");
1112 strcat(full_name, field->name);
1113 field_name = full_name;
1115 field_name = field->name;
1116 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1117 field_name = "common_timestamp";
1119 if (field_name == NULL)
1125 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1127 hist_field_fn_t fn = NULL;
1129 switch (field_size) {
1131 if (field_is_signed)
1132 fn = hist_field_s64;
1134 fn = hist_field_u64;
1137 if (field_is_signed)
1138 fn = hist_field_s32;
1140 fn = hist_field_u32;
1143 if (field_is_signed)
1144 fn = hist_field_s16;
1146 fn = hist_field_u16;
1149 if (field_is_signed)
1159 static int parse_map_size(char *str)
1161 unsigned long size, map_bits;
1164 ret = kstrtoul(str, 0, &size);
1168 map_bits = ilog2(roundup_pow_of_two(size));
1169 if (map_bits < TRACING_MAP_BITS_MIN ||
1170 map_bits > TRACING_MAP_BITS_MAX)
1178 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1185 for (i = 0; i < attrs->n_assignments; i++)
1186 kfree(attrs->assignment_str[i]);
1188 for (i = 0; i < attrs->n_actions; i++)
1189 kfree(attrs->action_str[i]);
1192 kfree(attrs->sort_key_str);
1193 kfree(attrs->keys_str);
1194 kfree(attrs->vals_str);
1195 kfree(attrs->clock);
1199 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1203 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1206 if ((str_has_prefix(str, "onmatch(")) ||
1207 (str_has_prefix(str, "onmax(")) ||
1208 (str_has_prefix(str, "onchange("))) {
1209 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1210 if (!attrs->action_str[attrs->n_actions]) {
1220 static int parse_assignment(struct trace_array *tr,
1221 char *str, struct hist_trigger_attrs *attrs)
1225 if ((len = str_has_prefix(str, "key=")) ||
1226 (len = str_has_prefix(str, "keys="))) {
1227 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1228 if (!attrs->keys_str) {
1232 } else if ((len = str_has_prefix(str, "val=")) ||
1233 (len = str_has_prefix(str, "vals=")) ||
1234 (len = str_has_prefix(str, "values="))) {
1235 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1236 if (!attrs->vals_str) {
1240 } else if ((len = str_has_prefix(str, "sort="))) {
1241 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1242 if (!attrs->sort_key_str) {
1246 } else if (str_has_prefix(str, "name=")) {
1247 attrs->name = kstrdup(str, GFP_KERNEL);
1252 } else if ((len = str_has_prefix(str, "clock="))) {
1255 str = strstrip(str);
1256 attrs->clock = kstrdup(str, GFP_KERNEL);
1257 if (!attrs->clock) {
1261 } else if ((len = str_has_prefix(str, "size="))) {
1262 int map_bits = parse_map_size(str + len);
1268 attrs->map_bits = map_bits;
1272 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1273 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1278 assignment = kstrdup(str, GFP_KERNEL);
1284 attrs->assignment_str[attrs->n_assignments++] = assignment;
1290 static struct hist_trigger_attrs *
1291 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1293 struct hist_trigger_attrs *attrs;
1296 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1298 return ERR_PTR(-ENOMEM);
1300 while (trigger_str) {
1301 char *str = strsep(&trigger_str, ":");
1304 rhs = strchr(str, '=');
1306 if (!strlen(++rhs)) {
1308 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1311 ret = parse_assignment(tr, str, attrs);
1314 } else if (strcmp(str, "pause") == 0)
1315 attrs->pause = true;
1316 else if ((strcmp(str, "cont") == 0) ||
1317 (strcmp(str, "continue") == 0))
1319 else if (strcmp(str, "clear") == 0)
1320 attrs->clear = true;
1322 ret = parse_action(str, attrs);
1328 if (!attrs->keys_str) {
1333 if (!attrs->clock) {
1334 attrs->clock = kstrdup("global", GFP_KERNEL);
1335 if (!attrs->clock) {
1343 destroy_hist_trigger_attrs(attrs);
1345 return ERR_PTR(ret);
1348 static inline void save_comm(char *comm, struct task_struct *task)
1351 strcpy(comm, "<idle>");
1355 if (WARN_ON_ONCE(task->pid < 0)) {
1356 strcpy(comm, "<XXX>");
1360 strncpy(comm, task->comm, TASK_COMM_LEN);
1363 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1367 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1368 kfree(elt_data->field_var_str[i]);
1370 kfree(elt_data->comm);
1374 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1376 struct hist_elt_data *elt_data = elt->private_data;
1378 hist_elt_data_free(elt_data);
1381 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1383 struct hist_trigger_data *hist_data = elt->map->private_data;
1384 unsigned int size = TASK_COMM_LEN;
1385 struct hist_elt_data *elt_data;
1386 struct hist_field *key_field;
1387 unsigned int i, n_str;
1389 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1393 for_each_hist_key_field(i, hist_data) {
1394 key_field = hist_data->fields[i];
1396 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
1397 elt_data->comm = kzalloc(size, GFP_KERNEL);
1398 if (!elt_data->comm) {
1406 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1407 hist_data->n_var_str;
1408 if (n_str > SYNTH_FIELDS_MAX) {
1409 hist_elt_data_free(elt_data);
1413 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1415 size = STR_VAR_LEN_MAX;
1417 for (i = 0; i < n_str; i++) {
1418 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1419 if (!elt_data->field_var_str[i]) {
1420 hist_elt_data_free(elt_data);
1425 elt->private_data = elt_data;
1430 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1432 struct hist_elt_data *elt_data = elt->private_data;
1435 save_comm(elt_data->comm, current);
1438 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1439 .elt_alloc = hist_trigger_elt_data_alloc,
1440 .elt_free = hist_trigger_elt_data_free,
1441 .elt_init = hist_trigger_elt_data_init,
1444 static const char *get_hist_field_flags(struct hist_field *hist_field)
1446 const char *flags_str = NULL;
1448 if (hist_field->flags & HIST_FIELD_FL_HEX)
1450 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1452 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1453 flags_str = "sym-offset";
1454 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1455 flags_str = "execname";
1456 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1457 flags_str = "syscall";
1458 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1460 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1461 flags_str = "usecs";
1466 static void expr_field_str(struct hist_field *field, char *expr)
1468 if (field->flags & HIST_FIELD_FL_VAR_REF)
1471 strcat(expr, hist_field_name(field, 0));
1473 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1474 const char *flags_str = get_hist_field_flags(field);
1478 strcat(expr, flags_str);
1483 static char *expr_str(struct hist_field *field, unsigned int level)
1490 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1494 if (!field->operands[0]) {
1495 expr_field_str(field, expr);
1499 if (field->operator == FIELD_OP_UNARY_MINUS) {
1503 subexpr = expr_str(field->operands[0], ++level);
1508 strcat(expr, subexpr);
1516 expr_field_str(field->operands[0], expr);
1518 switch (field->operator) {
1519 case FIELD_OP_MINUS:
1530 expr_field_str(field->operands[1], expr);
1535 static int contains_operator(char *str)
1537 enum field_op_id field_op = FIELD_OP_NONE;
1540 op = strpbrk(str, "+-");
1542 return FIELD_OP_NONE;
1547 * Unfortunately, the modifier ".sym-offset"
1548 * can confuse things.
1550 if (op - str >= 4 && !strncmp(op - 4, ".sym-offset", 11))
1551 return FIELD_OP_NONE;
1554 field_op = FIELD_OP_UNARY_MINUS;
1556 field_op = FIELD_OP_MINUS;
1559 field_op = FIELD_OP_PLUS;
1568 static void get_hist_field(struct hist_field *hist_field)
1573 static void __destroy_hist_field(struct hist_field *hist_field)
1575 if (--hist_field->ref > 1)
1578 kfree(hist_field->var.name);
1579 kfree(hist_field->name);
1580 kfree(hist_field->type);
1582 kfree(hist_field->system);
1583 kfree(hist_field->event_name);
1588 static void destroy_hist_field(struct hist_field *hist_field,
1599 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1600 return; /* var refs will be destroyed separately */
1602 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1603 destroy_hist_field(hist_field->operands[i], level + 1);
1605 __destroy_hist_field(hist_field);
1608 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1609 struct ftrace_event_field *field,
1610 unsigned long flags,
1613 struct hist_field *hist_field;
1615 if (field && is_function_field(field))
1618 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1622 hist_field->ref = 1;
1624 hist_field->hist_data = hist_data;
1626 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1627 goto out; /* caller will populate */
1629 if (flags & HIST_FIELD_FL_VAR_REF) {
1630 hist_field->fn = hist_field_var_ref;
1634 if (flags & HIST_FIELD_FL_HITCOUNT) {
1635 hist_field->fn = hist_field_counter;
1636 hist_field->size = sizeof(u64);
1637 hist_field->type = kstrdup("u64", GFP_KERNEL);
1638 if (!hist_field->type)
1643 if (flags & HIST_FIELD_FL_STACKTRACE) {
1644 hist_field->fn = hist_field_none;
1648 if (flags & HIST_FIELD_FL_LOG2) {
1649 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
1650 hist_field->fn = hist_field_log2;
1651 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1652 if (!hist_field->operands[0])
1654 hist_field->size = hist_field->operands[0]->size;
1655 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
1656 if (!hist_field->type)
1661 if (flags & HIST_FIELD_FL_TIMESTAMP) {
1662 hist_field->fn = hist_field_timestamp;
1663 hist_field->size = sizeof(u64);
1664 hist_field->type = kstrdup("u64", GFP_KERNEL);
1665 if (!hist_field->type)
1670 if (flags & HIST_FIELD_FL_CPU) {
1671 hist_field->fn = hist_field_cpu;
1672 hist_field->size = sizeof(int);
1673 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
1674 if (!hist_field->type)
1679 if (WARN_ON_ONCE(!field))
1682 /* Pointers to strings are just pointers and dangerous to dereference */
1683 if (is_string_field(field) &&
1684 (field->filter_type != FILTER_PTR_STRING)) {
1685 flags |= HIST_FIELD_FL_STRING;
1687 hist_field->size = MAX_FILTER_STR_VAL;
1688 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1689 if (!hist_field->type)
1692 if (field->filter_type == FILTER_STATIC_STRING) {
1693 hist_field->fn = hist_field_string;
1694 hist_field->size = field->size;
1695 } else if (field->filter_type == FILTER_DYN_STRING)
1696 hist_field->fn = hist_field_dynstring;
1698 hist_field->fn = hist_field_pstring;
1700 hist_field->size = field->size;
1701 hist_field->is_signed = field->is_signed;
1702 hist_field->type = kstrdup(field->type, GFP_KERNEL);
1703 if (!hist_field->type)
1706 hist_field->fn = select_value_fn(field->size,
1708 if (!hist_field->fn) {
1709 destroy_hist_field(hist_field, 0);
1714 hist_field->field = field;
1715 hist_field->flags = flags;
1718 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
1719 if (!hist_field->var.name)
1725 destroy_hist_field(hist_field, 0);
1729 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
1733 for (i = 0; i < HIST_FIELDS_MAX; i++) {
1734 if (hist_data->fields[i]) {
1735 destroy_hist_field(hist_data->fields[i], 0);
1736 hist_data->fields[i] = NULL;
1740 for (i = 0; i < hist_data->n_var_refs; i++) {
1741 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
1742 __destroy_hist_field(hist_data->var_refs[i]);
1743 hist_data->var_refs[i] = NULL;
1747 static int init_var_ref(struct hist_field *ref_field,
1748 struct hist_field *var_field,
1749 char *system, char *event_name)
1753 ref_field->var.idx = var_field->var.idx;
1754 ref_field->var.hist_data = var_field->hist_data;
1755 ref_field->size = var_field->size;
1756 ref_field->is_signed = var_field->is_signed;
1757 ref_field->flags |= var_field->flags &
1758 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
1761 ref_field->system = kstrdup(system, GFP_KERNEL);
1762 if (!ref_field->system)
1767 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
1768 if (!ref_field->event_name) {
1774 if (var_field->var.name) {
1775 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
1776 if (!ref_field->name) {
1780 } else if (var_field->name) {
1781 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
1782 if (!ref_field->name) {
1788 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
1789 if (!ref_field->type) {
1796 kfree(ref_field->system);
1797 ref_field->system = NULL;
1798 kfree(ref_field->event_name);
1799 ref_field->event_name = NULL;
1800 kfree(ref_field->name);
1801 ref_field->name = NULL;
1806 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
1807 struct hist_field *var_field)
1809 struct hist_field *ref_field;
1812 for (i = 0; i < hist_data->n_var_refs; i++) {
1813 ref_field = hist_data->var_refs[i];
1814 if (ref_field->var.idx == var_field->var.idx &&
1815 ref_field->var.hist_data == var_field->hist_data)
1823 * create_var_ref - Create a variable reference and attach it to trigger
1824 * @hist_data: The trigger that will be referencing the variable
1825 * @var_field: The VAR field to create a reference to
1826 * @system: The optional system string
1827 * @event_name: The optional event_name string
1829 * Given a variable hist_field, create a VAR_REF hist_field that
1830 * represents a reference to it.
1832 * This function also adds the reference to the trigger that
1833 * now references the variable.
1835 * Return: The VAR_REF field if successful, NULL if not
1837 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
1838 struct hist_field *var_field,
1839 char *system, char *event_name)
1841 unsigned long flags = HIST_FIELD_FL_VAR_REF;
1842 struct hist_field *ref_field;
1845 /* Check if the variable already exists */
1846 for (i = 0; i < hist_data->n_var_refs; i++) {
1847 ref_field = hist_data->var_refs[i];
1848 if (ref_field->var.idx == var_field->var.idx &&
1849 ref_field->var.hist_data == var_field->hist_data) {
1850 get_hist_field(ref_field);
1854 /* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
1855 if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
1857 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
1859 if (init_var_ref(ref_field, var_field, system, event_name)) {
1860 destroy_hist_field(ref_field, 0);
1864 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
1865 ref_field->var_ref_idx = hist_data->n_var_refs++;
1871 static bool is_var_ref(char *var_name)
1873 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
1879 static char *field_name_from_var(struct hist_trigger_data *hist_data,
1885 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
1886 name = hist_data->attrs->var_defs.name[i];
1888 if (strcmp(var_name, name) == 0) {
1889 field = hist_data->attrs->var_defs.expr[i];
1890 if (contains_operator(field) || is_var_ref(field))
1899 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
1900 char *system, char *event_name,
1903 struct trace_event_call *call;
1905 if (system && event_name) {
1906 call = hist_data->event_file->event_call;
1908 if (strcmp(system, call->class->system) != 0)
1911 if (strcmp(event_name, trace_event_name(call)) != 0)
1915 if (!!system != !!event_name)
1918 if (!is_var_ref(var_name))
1923 return field_name_from_var(hist_data, var_name);
1926 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
1927 char *system, char *event_name,
1930 struct hist_field *var_field = NULL, *ref_field = NULL;
1931 struct trace_array *tr = hist_data->event_file->tr;
1933 if (!is_var_ref(var_name))
1938 var_field = find_event_var(hist_data, system, event_name, var_name);
1940 ref_field = create_var_ref(hist_data, var_field,
1941 system, event_name);
1944 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
1949 static struct ftrace_event_field *
1950 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
1951 char *field_str, unsigned long *flags)
1953 struct ftrace_event_field *field = NULL;
1954 char *field_name, *modifier, *str;
1955 struct trace_array *tr = file->tr;
1957 modifier = str = kstrdup(field_str, GFP_KERNEL);
1959 return ERR_PTR(-ENOMEM);
1961 field_name = strsep(&modifier, ".");
1963 if (strcmp(modifier, "hex") == 0)
1964 *flags |= HIST_FIELD_FL_HEX;
1965 else if (strcmp(modifier, "sym") == 0)
1966 *flags |= HIST_FIELD_FL_SYM;
1967 else if (strcmp(modifier, "sym-offset") == 0)
1968 *flags |= HIST_FIELD_FL_SYM_OFFSET;
1969 else if ((strcmp(modifier, "execname") == 0) &&
1970 (strcmp(field_name, "common_pid") == 0))
1971 *flags |= HIST_FIELD_FL_EXECNAME;
1972 else if (strcmp(modifier, "syscall") == 0)
1973 *flags |= HIST_FIELD_FL_SYSCALL;
1974 else if (strcmp(modifier, "log2") == 0)
1975 *flags |= HIST_FIELD_FL_LOG2;
1976 else if (strcmp(modifier, "usecs") == 0)
1977 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
1979 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
1980 field = ERR_PTR(-EINVAL);
1985 if (strcmp(field_name, "common_timestamp") == 0) {
1986 *flags |= HIST_FIELD_FL_TIMESTAMP;
1987 hist_data->enable_timestamps = true;
1988 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1989 hist_data->attrs->ts_in_usecs = true;
1990 } else if (strcmp(field_name, "common_cpu") == 0)
1991 *flags |= HIST_FIELD_FL_CPU;
1993 field = trace_find_event_field(file->event_call, field_name);
1994 if (!field || !field->size) {
1996 * For backward compatibility, if field_name
1997 * was "cpu", then we treat this the same as
1998 * common_cpu. This also works for "CPU".
2000 if (field && field->filter_type == FILTER_CPU) {
2001 *flags |= HIST_FIELD_FL_CPU;
2003 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2004 errpos(field_name));
2005 field = ERR_PTR(-EINVAL);
2016 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2017 struct hist_field *var_ref,
2020 struct hist_field *alias = NULL;
2021 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2023 alias = create_hist_field(hist_data, NULL, flags, var_name);
2027 alias->fn = var_ref->fn;
2028 alias->operands[0] = var_ref;
2030 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2031 destroy_hist_field(alias, 0);
2035 alias->var_ref_idx = var_ref->var_ref_idx;
2040 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2041 struct trace_event_file *file, char *str,
2042 unsigned long *flags, char *var_name)
2044 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2045 struct ftrace_event_field *field = NULL;
2046 struct hist_field *hist_field = NULL;
2049 s = strchr(str, '.');
2051 s = strchr(++s, '.');
2053 ref_system = strsep(&str, ".");
2058 ref_event = strsep(&str, ".");
2067 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2069 hist_field = parse_var_ref(hist_data, ref_system,
2070 ref_event, ref_var);
2073 hist_field = create_alias(hist_data, hist_field, var_name);
2084 field = parse_field(hist_data, file, str, flags);
2085 if (IS_ERR(field)) {
2086 ret = PTR_ERR(field);
2090 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2098 return ERR_PTR(ret);
2101 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2102 struct trace_event_file *file,
2103 char *str, unsigned long flags,
2104 char *var_name, unsigned int level);
2106 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2107 struct trace_event_file *file,
2108 char *str, unsigned long flags,
2109 char *var_name, unsigned int level)
2111 struct hist_field *operand1, *expr = NULL;
2112 unsigned long operand_flags;
2116 /* we support only -(xxx) i.e. explicit parens required */
2119 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2124 str++; /* skip leading '-' */
2126 s = strchr(str, '(');
2134 s = strrchr(str, ')');
2138 ret = -EINVAL; /* no closing ')' */
2142 flags |= HIST_FIELD_FL_EXPR;
2143 expr = create_hist_field(hist_data, NULL, flags, var_name);
2150 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2151 if (IS_ERR(operand1)) {
2152 ret = PTR_ERR(operand1);
2155 if (operand1->flags & HIST_FIELD_FL_STRING) {
2156 /* String type can not be the operand of unary operator. */
2157 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2158 destroy_hist_field(operand1, 0);
2163 expr->flags |= operand1->flags &
2164 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2165 expr->fn = hist_field_unary_minus;
2166 expr->operands[0] = operand1;
2167 expr->size = operand1->size;
2168 expr->is_signed = operand1->is_signed;
2169 expr->operator = FIELD_OP_UNARY_MINUS;
2170 expr->name = expr_str(expr, 0);
2171 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2179 destroy_hist_field(expr, 0);
2180 return ERR_PTR(ret);
2183 static int check_expr_operands(struct trace_array *tr,
2184 struct hist_field *operand1,
2185 struct hist_field *operand2)
2187 unsigned long operand1_flags = operand1->flags;
2188 unsigned long operand2_flags = operand2->flags;
2190 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2191 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2192 struct hist_field *var;
2194 var = find_var_field(operand1->var.hist_data, operand1->name);
2197 operand1_flags = var->flags;
2200 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2201 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2202 struct hist_field *var;
2204 var = find_var_field(operand2->var.hist_data, operand2->name);
2207 operand2_flags = var->flags;
2210 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2211 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2212 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2219 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2220 struct trace_event_file *file,
2221 char *str, unsigned long flags,
2222 char *var_name, unsigned int level)
2224 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2225 unsigned long operand_flags;
2226 int field_op, ret = -EINVAL;
2227 char *sep, *operand1_str;
2230 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2231 return ERR_PTR(-EINVAL);
2234 field_op = contains_operator(str);
2236 if (field_op == FIELD_OP_NONE)
2237 return parse_atom(hist_data, file, str, &flags, var_name);
2239 if (field_op == FIELD_OP_UNARY_MINUS)
2240 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2243 case FIELD_OP_MINUS:
2253 operand1_str = strsep(&str, sep);
2254 if (!operand1_str || !str)
2258 operand1 = parse_atom(hist_data, file, operand1_str,
2259 &operand_flags, NULL);
2260 if (IS_ERR(operand1)) {
2261 ret = PTR_ERR(operand1);
2265 if (operand1->flags & HIST_FIELD_FL_STRING) {
2266 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2271 /* rest of string could be another expression e.g. b+c in a+b+c */
2273 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2274 if (IS_ERR(operand2)) {
2275 ret = PTR_ERR(operand2);
2279 if (operand2->flags & HIST_FIELD_FL_STRING) {
2280 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2285 ret = check_expr_operands(file->tr, operand1, operand2);
2289 flags |= HIST_FIELD_FL_EXPR;
2291 flags |= operand1->flags &
2292 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2294 expr = create_hist_field(hist_data, NULL, flags, var_name);
2300 operand1->read_once = true;
2301 operand2->read_once = true;
2303 expr->operands[0] = operand1;
2304 expr->operands[1] = operand2;
2306 /* The operand sizes should be the same, so just pick one */
2307 expr->size = operand1->size;
2308 expr->is_signed = operand1->is_signed;
2310 expr->operator = field_op;
2311 expr->name = expr_str(expr, 0);
2312 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2319 case FIELD_OP_MINUS:
2320 expr->fn = hist_field_minus;
2323 expr->fn = hist_field_plus;
2332 destroy_hist_field(operand1, 0);
2333 destroy_hist_field(operand2, 0);
2334 destroy_hist_field(expr, 0);
2336 return ERR_PTR(ret);
2339 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2340 struct trace_event_file *file)
2342 struct event_trigger_data *test;
2344 lockdep_assert_held(&event_mutex);
2346 list_for_each_entry(test, &file->triggers, list) {
2347 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2348 if (test->private_data == hist_data)
2349 return test->filter_str;
2356 static struct event_command trigger_hist_cmd;
2357 static int event_hist_trigger_func(struct event_command *cmd_ops,
2358 struct trace_event_file *file,
2359 char *glob, char *cmd, char *param);
2361 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2362 struct hist_trigger_data *hist_data,
2363 unsigned int n_keys)
2365 struct hist_field *target_hist_field, *hist_field;
2366 unsigned int n, i, j;
2368 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2371 i = hist_data->n_vals;
2372 j = target_hist_data->n_vals;
2374 for (n = 0; n < n_keys; n++) {
2375 hist_field = hist_data->fields[i + n];
2376 target_hist_field = target_hist_data->fields[j + n];
2378 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2380 if (hist_field->size != target_hist_field->size)
2382 if (hist_field->is_signed != target_hist_field->is_signed)
2389 static struct hist_trigger_data *
2390 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2391 struct trace_event_file *file)
2393 struct hist_trigger_data *hist_data;
2394 struct event_trigger_data *test;
2395 unsigned int n_keys;
2397 lockdep_assert_held(&event_mutex);
2399 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2401 list_for_each_entry(test, &file->triggers, list) {
2402 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2403 hist_data = test->private_data;
2405 if (compatible_keys(target_hist_data, hist_data, n_keys))
2413 static struct trace_event_file *event_file(struct trace_array *tr,
2414 char *system, char *event_name)
2416 struct trace_event_file *file;
2418 file = __find_event_file(tr, system, event_name);
2420 return ERR_PTR(-EINVAL);
2425 static struct hist_field *
2426 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2427 char *system, char *event_name, char *field_name)
2429 struct hist_field *event_var;
2430 char *synthetic_name;
2432 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2433 if (!synthetic_name)
2434 return ERR_PTR(-ENOMEM);
2436 strcpy(synthetic_name, "synthetic_");
2437 strcat(synthetic_name, field_name);
2439 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2441 kfree(synthetic_name);
2447 * create_field_var_hist - Automatically create a histogram and var for a field
2448 * @target_hist_data: The target hist trigger
2449 * @subsys_name: Optional subsystem name
2450 * @event_name: Optional event name
2451 * @field_name: The name of the field (and the resulting variable)
2453 * Hist trigger actions fetch data from variables, not directly from
2454 * events. However, for convenience, users are allowed to directly
2455 * specify an event field in an action, which will be automatically
2456 * converted into a variable on their behalf.
2458 * If a user specifies a field on an event that isn't the event the
2459 * histogram currently being defined (the target event histogram), the
2460 * only way that can be accomplished is if a new hist trigger is
2461 * created and the field variable defined on that.
2463 * This function creates a new histogram compatible with the target
2464 * event (meaning a histogram with the same key as the target
2465 * histogram), and creates a variable for the specified field, but
2466 * with 'synthetic_' prepended to the variable name in order to avoid
2467 * collision with normal field variables.
2469 * Return: The variable created for the field.
2471 static struct hist_field *
2472 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2473 char *subsys_name, char *event_name, char *field_name)
2475 struct trace_array *tr = target_hist_data->event_file->tr;
2476 struct hist_field *event_var = ERR_PTR(-EINVAL);
2477 struct hist_trigger_data *hist_data;
2478 unsigned int i, n, first = true;
2479 struct field_var_hist *var_hist;
2480 struct trace_event_file *file;
2481 struct hist_field *key_field;
2486 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2487 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2488 return ERR_PTR(-EINVAL);
2491 file = event_file(tr, subsys_name, event_name);
2494 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2495 ret = PTR_ERR(file);
2496 return ERR_PTR(ret);
2500 * Look for a histogram compatible with target. We'll use the
2501 * found histogram specification to create a new matching
2502 * histogram with our variable on it. target_hist_data is not
2503 * yet a registered histogram so we can't use that.
2505 hist_data = find_compatible_hist(target_hist_data, file);
2507 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2508 return ERR_PTR(-EINVAL);
2511 /* See if a synthetic field variable has already been created */
2512 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2513 event_name, field_name);
2514 if (!IS_ERR_OR_NULL(event_var))
2517 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2519 return ERR_PTR(-ENOMEM);
2521 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2524 return ERR_PTR(-ENOMEM);
2527 /* Use the same keys as the compatible histogram */
2528 strcat(cmd, "keys=");
2530 for_each_hist_key_field(i, hist_data) {
2531 key_field = hist_data->fields[i];
2534 strcat(cmd, key_field->field->name);
2538 /* Create the synthetic field variable specification */
2539 strcat(cmd, ":synthetic_");
2540 strcat(cmd, field_name);
2542 strcat(cmd, field_name);
2544 /* Use the same filter as the compatible histogram */
2545 saved_filter = find_trigger_filter(hist_data, file);
2547 strcat(cmd, " if ");
2548 strcat(cmd, saved_filter);
2551 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
2552 if (!var_hist->cmd) {
2555 return ERR_PTR(-ENOMEM);
2558 /* Save the compatible histogram information */
2559 var_hist->hist_data = hist_data;
2561 /* Create the new histogram with our variable */
2562 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
2566 kfree(var_hist->cmd);
2568 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
2569 return ERR_PTR(ret);
2574 /* If we can't find the variable, something went wrong */
2575 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2576 event_name, field_name);
2577 if (IS_ERR_OR_NULL(event_var)) {
2578 kfree(var_hist->cmd);
2580 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
2581 return ERR_PTR(-EINVAL);
2584 n = target_hist_data->n_field_var_hists;
2585 target_hist_data->field_var_hists[n] = var_hist;
2586 target_hist_data->n_field_var_hists++;
2591 static struct hist_field *
2592 find_target_event_var(struct hist_trigger_data *hist_data,
2593 char *subsys_name, char *event_name, char *var_name)
2595 struct trace_event_file *file = hist_data->event_file;
2596 struct hist_field *hist_field = NULL;
2599 struct trace_event_call *call;
2604 call = file->event_call;
2606 if (strcmp(subsys_name, call->class->system) != 0)
2609 if (strcmp(event_name, trace_event_name(call)) != 0)
2613 hist_field = find_var_field(hist_data, var_name);
2618 static inline void __update_field_vars(struct tracing_map_elt *elt,
2619 struct ring_buffer_event *rbe,
2621 struct field_var **field_vars,
2622 unsigned int n_field_vars,
2623 unsigned int field_var_str_start)
2625 struct hist_elt_data *elt_data = elt->private_data;
2626 unsigned int i, j, var_idx;
2629 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
2630 struct field_var *field_var = field_vars[i];
2631 struct hist_field *var = field_var->var;
2632 struct hist_field *val = field_var->val;
2634 var_val = val->fn(val, elt, rbe, rec);
2635 var_idx = var->var.idx;
2637 if (val->flags & HIST_FIELD_FL_STRING) {
2638 char *str = elt_data->field_var_str[j++];
2639 char *val_str = (char *)(uintptr_t)var_val;
2642 size = min(val->size, STR_VAR_LEN_MAX);
2643 strscpy(str, val_str, size);
2644 var_val = (u64)(uintptr_t)str;
2646 tracing_map_set_var(elt, var_idx, var_val);
2650 static void update_field_vars(struct hist_trigger_data *hist_data,
2651 struct tracing_map_elt *elt,
2652 struct ring_buffer_event *rbe,
2655 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
2656 hist_data->n_field_vars, 0);
2659 static void save_track_data_vars(struct hist_trigger_data *hist_data,
2660 struct tracing_map_elt *elt, void *rec,
2661 struct ring_buffer_event *rbe, void *key,
2662 struct action_data *data, u64 *var_ref_vals)
2664 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
2665 hist_data->n_save_vars, hist_data->n_field_var_str);
2668 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
2669 struct trace_event_file *file,
2670 char *name, int size, const char *type)
2672 struct hist_field *var;
2675 if (find_var(hist_data, file, name) && !hist_data->remove) {
2676 var = ERR_PTR(-EINVAL);
2680 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2682 var = ERR_PTR(-ENOMEM);
2686 idx = tracing_map_add_var(hist_data->map);
2689 var = ERR_PTR(-EINVAL);
2694 var->flags = HIST_FIELD_FL_VAR;
2696 var->var.hist_data = var->hist_data = hist_data;
2698 var->var.name = kstrdup(name, GFP_KERNEL);
2699 var->type = kstrdup(type, GFP_KERNEL);
2700 if (!var->var.name || !var->type) {
2701 kfree(var->var.name);
2704 var = ERR_PTR(-ENOMEM);
2710 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
2711 struct trace_event_file *file,
2714 struct hist_field *val = NULL, *var = NULL;
2715 unsigned long flags = HIST_FIELD_FL_VAR;
2716 struct trace_array *tr = file->tr;
2717 struct field_var *field_var;
2720 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
2721 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2726 val = parse_atom(hist_data, file, field_name, &flags, NULL);
2728 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
2733 var = create_var(hist_data, file, field_name, val->size, val->type);
2735 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
2741 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
2749 field_var->var = var;
2750 field_var->val = val;
2754 field_var = ERR_PTR(ret);
2759 * create_target_field_var - Automatically create a variable for a field
2760 * @target_hist_data: The target hist trigger
2761 * @subsys_name: Optional subsystem name
2762 * @event_name: Optional event name
2763 * @var_name: The name of the field (and the resulting variable)
2765 * Hist trigger actions fetch data from variables, not directly from
2766 * events. However, for convenience, users are allowed to directly
2767 * specify an event field in an action, which will be automatically
2768 * converted into a variable on their behalf.
2770 * This function creates a field variable with the name var_name on
2771 * the hist trigger currently being defined on the target event. If
2772 * subsys_name and event_name are specified, this function simply
2773 * verifies that they do in fact match the target event subsystem and
2776 * Return: The variable created for the field.
2778 static struct field_var *
2779 create_target_field_var(struct hist_trigger_data *target_hist_data,
2780 char *subsys_name, char *event_name, char *var_name)
2782 struct trace_event_file *file = target_hist_data->event_file;
2785 struct trace_event_call *call;
2790 call = file->event_call;
2792 if (strcmp(subsys_name, call->class->system) != 0)
2795 if (strcmp(event_name, trace_event_name(call)) != 0)
2799 return create_field_var(target_hist_data, file, var_name);
2802 static bool check_track_val_max(u64 track_val, u64 var_val)
2804 if (var_val <= track_val)
2810 static bool check_track_val_changed(u64 track_val, u64 var_val)
2812 if (var_val == track_val)
2818 static u64 get_track_val(struct hist_trigger_data *hist_data,
2819 struct tracing_map_elt *elt,
2820 struct action_data *data)
2822 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2825 track_val = tracing_map_read_var(elt, track_var_idx);
2830 static void save_track_val(struct hist_trigger_data *hist_data,
2831 struct tracing_map_elt *elt,
2832 struct action_data *data, u64 var_val)
2834 unsigned int track_var_idx = data->track_data.track_var->var.idx;
2836 tracing_map_set_var(elt, track_var_idx, var_val);
2839 static void save_track_data(struct hist_trigger_data *hist_data,
2840 struct tracing_map_elt *elt, void *rec,
2841 struct ring_buffer_event *rbe, void *key,
2842 struct action_data *data, u64 *var_ref_vals)
2844 if (data->track_data.save_data)
2845 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
2848 static bool check_track_val(struct tracing_map_elt *elt,
2849 struct action_data *data,
2852 struct hist_trigger_data *hist_data;
2855 hist_data = data->track_data.track_var->hist_data;
2856 track_val = get_track_val(hist_data, elt, data);
2858 return data->track_data.check_val(track_val, var_val);
2861 #ifdef CONFIG_TRACER_SNAPSHOT
2862 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2864 /* called with tr->max_lock held */
2865 struct track_data *track_data = tr->cond_snapshot->cond_data;
2866 struct hist_elt_data *elt_data, *track_elt_data;
2867 struct snapshot_context *context = cond_data;
2868 struct action_data *action;
2874 action = track_data->action_data;
2876 track_val = get_track_val(track_data->hist_data, context->elt,
2877 track_data->action_data);
2879 if (!action->track_data.check_val(track_data->track_val, track_val))
2882 track_data->track_val = track_val;
2883 memcpy(track_data->key, context->key, track_data->key_len);
2885 elt_data = context->elt->private_data;
2886 track_elt_data = track_data->elt.private_data;
2888 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
2890 track_data->updated = true;
2895 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2896 struct tracing_map_elt *elt, void *rec,
2897 struct ring_buffer_event *rbe, void *key,
2898 struct action_data *data,
2901 struct trace_event_file *file = hist_data->event_file;
2902 struct snapshot_context context;
2907 tracing_snapshot_cond(file->tr, &context);
2910 static void hist_trigger_print_key(struct seq_file *m,
2911 struct hist_trigger_data *hist_data,
2913 struct tracing_map_elt *elt);
2915 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
2919 if (!hist_data->n_actions)
2922 for (i = 0; i < hist_data->n_actions; i++) {
2923 struct action_data *data = hist_data->actions[i];
2925 if (data->action == ACTION_SNAPSHOT)
2932 static void track_data_snapshot_print(struct seq_file *m,
2933 struct hist_trigger_data *hist_data)
2935 struct trace_event_file *file = hist_data->event_file;
2936 struct track_data *track_data;
2937 struct action_data *action;
2939 track_data = tracing_cond_snapshot_data(file->tr);
2943 if (!track_data->updated)
2946 action = snapshot_action(hist_data);
2950 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
2951 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
2952 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
2953 action->track_data.var_str, track_data->track_val);
2955 seq_puts(m, "\ttriggered by event with key: ");
2956 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
2960 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
2964 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
2965 struct tracing_map_elt *elt, void *rec,
2966 struct ring_buffer_event *rbe, void *key,
2967 struct action_data *data,
2968 u64 *var_ref_vals) {}
2969 static void track_data_snapshot_print(struct seq_file *m,
2970 struct hist_trigger_data *hist_data) {}
2971 #endif /* CONFIG_TRACER_SNAPSHOT */
2973 static void track_data_print(struct seq_file *m,
2974 struct hist_trigger_data *hist_data,
2975 struct tracing_map_elt *elt,
2976 struct action_data *data)
2978 u64 track_val = get_track_val(hist_data, elt, data);
2979 unsigned int i, save_var_idx;
2981 if (data->handler == HANDLER_ONMAX)
2982 seq_printf(m, "\n\tmax: %10llu", track_val);
2983 else if (data->handler == HANDLER_ONCHANGE)
2984 seq_printf(m, "\n\tchanged: %10llu", track_val);
2986 if (data->action == ACTION_SNAPSHOT)
2989 for (i = 0; i < hist_data->n_save_vars; i++) {
2990 struct hist_field *save_val = hist_data->save_vars[i]->val;
2991 struct hist_field *save_var = hist_data->save_vars[i]->var;
2994 save_var_idx = save_var->var.idx;
2996 val = tracing_map_read_var(elt, save_var_idx);
2998 if (save_val->flags & HIST_FIELD_FL_STRING) {
2999 seq_printf(m, " %s: %-32s", save_var->var.name,
3000 (char *)(uintptr_t)(val));
3002 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3006 static void ontrack_action(struct hist_trigger_data *hist_data,
3007 struct tracing_map_elt *elt, void *rec,
3008 struct ring_buffer_event *rbe, void *key,
3009 struct action_data *data, u64 *var_ref_vals)
3011 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3013 if (check_track_val(elt, data, var_val)) {
3014 save_track_val(hist_data, elt, data, var_val);
3015 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3019 static void action_data_destroy(struct action_data *data)
3023 lockdep_assert_held(&event_mutex);
3025 kfree(data->action_name);
3027 for (i = 0; i < data->n_params; i++)
3028 kfree(data->params[i]);
3030 if (data->synth_event)
3031 data->synth_event->ref--;
3033 kfree(data->synth_event_name);
3038 static void track_data_destroy(struct hist_trigger_data *hist_data,
3039 struct action_data *data)
3041 struct trace_event_file *file = hist_data->event_file;
3043 destroy_hist_field(data->track_data.track_var, 0);
3045 if (data->action == ACTION_SNAPSHOT) {
3046 struct track_data *track_data;
3048 track_data = tracing_cond_snapshot_data(file->tr);
3049 if (track_data && track_data->hist_data == hist_data) {
3050 tracing_snapshot_cond_disable(file->tr);
3051 track_data_free(track_data);
3055 kfree(data->track_data.var_str);
3057 action_data_destroy(data);
3060 static int action_create(struct hist_trigger_data *hist_data,
3061 struct action_data *data);
3063 static int track_data_create(struct hist_trigger_data *hist_data,
3064 struct action_data *data)
3066 struct hist_field *var_field, *ref_field, *track_var = NULL;
3067 struct trace_event_file *file = hist_data->event_file;
3068 struct trace_array *tr = file->tr;
3069 char *track_data_var_str;
3072 track_data_var_str = data->track_data.var_str;
3073 if (track_data_var_str[0] != '$') {
3074 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3077 track_data_var_str++;
3079 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3081 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3085 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3089 data->track_data.var_ref = ref_field;
3091 if (data->handler == HANDLER_ONMAX)
3092 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3093 if (IS_ERR(track_var)) {
3094 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3095 ret = PTR_ERR(track_var);
3099 if (data->handler == HANDLER_ONCHANGE)
3100 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3101 if (IS_ERR(track_var)) {
3102 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3103 ret = PTR_ERR(track_var);
3106 data->track_data.track_var = track_var;
3108 ret = action_create(hist_data, data);
3113 static int parse_action_params(struct trace_array *tr, char *params,
3114 struct action_data *data)
3116 char *param, *saved_param;
3117 bool first_param = true;
3121 if (data->n_params >= SYNTH_FIELDS_MAX) {
3122 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3127 param = strsep(¶ms, ",");
3129 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3134 param = strstrip(param);
3135 if (strlen(param) < 2) {
3136 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3141 saved_param = kstrdup(param, GFP_KERNEL);
3147 if (first_param && data->use_trace_keyword) {
3148 data->synth_event_name = saved_param;
3149 first_param = false;
3152 first_param = false;
3154 data->params[data->n_params++] = saved_param;
3160 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3161 enum handler_id handler)
3168 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3173 action_name = strsep(&str, "(");
3174 if (!action_name || !str) {
3175 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3180 if (str_has_prefix(action_name, "save")) {
3181 char *params = strsep(&str, ")");
3184 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3189 ret = parse_action_params(tr, params, data);
3193 if (handler == HANDLER_ONMAX)
3194 data->track_data.check_val = check_track_val_max;
3195 else if (handler == HANDLER_ONCHANGE)
3196 data->track_data.check_val = check_track_val_changed;
3198 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3203 data->track_data.save_data = save_track_data_vars;
3204 data->fn = ontrack_action;
3205 data->action = ACTION_SAVE;
3206 } else if (str_has_prefix(action_name, "snapshot")) {
3207 char *params = strsep(&str, ")");
3210 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3215 if (handler == HANDLER_ONMAX)
3216 data->track_data.check_val = check_track_val_max;
3217 else if (handler == HANDLER_ONCHANGE)
3218 data->track_data.check_val = check_track_val_changed;
3220 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3225 data->track_data.save_data = save_track_data_snapshot;
3226 data->fn = ontrack_action;
3227 data->action = ACTION_SNAPSHOT;
3229 char *params = strsep(&str, ")");
3231 if (str_has_prefix(action_name, "trace"))
3232 data->use_trace_keyword = true;
3235 ret = parse_action_params(tr, params, data);
3240 if (handler == HANDLER_ONMAX)
3241 data->track_data.check_val = check_track_val_max;
3242 else if (handler == HANDLER_ONCHANGE)
3243 data->track_data.check_val = check_track_val_changed;
3245 if (handler != HANDLER_ONMATCH) {
3246 data->track_data.save_data = action_trace;
3247 data->fn = ontrack_action;
3249 data->fn = action_trace;
3251 data->action = ACTION_TRACE;
3254 data->action_name = kstrdup(action_name, GFP_KERNEL);
3255 if (!data->action_name) {
3260 data->handler = handler;
3265 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3266 char *str, enum handler_id handler)
3268 struct action_data *data;
3272 data = kzalloc(sizeof(*data), GFP_KERNEL);
3274 return ERR_PTR(-ENOMEM);
3276 var_str = strsep(&str, ")");
3277 if (!var_str || !str) {
3282 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3283 if (!data->track_data.var_str) {
3288 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3294 track_data_destroy(hist_data, data);
3295 data = ERR_PTR(ret);
3299 static void onmatch_destroy(struct action_data *data)
3301 kfree(data->match_data.event);
3302 kfree(data->match_data.event_system);
3304 action_data_destroy(data);
3307 static void destroy_field_var(struct field_var *field_var)
3312 destroy_hist_field(field_var->var, 0);
3313 destroy_hist_field(field_var->val, 0);
3318 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3322 for (i = 0; i < hist_data->n_field_vars; i++)
3323 destroy_field_var(hist_data->field_vars[i]);
3325 for (i = 0; i < hist_data->n_save_vars; i++)
3326 destroy_field_var(hist_data->save_vars[i]);
3329 static void save_field_var(struct hist_trigger_data *hist_data,
3330 struct field_var *field_var)
3332 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3334 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3335 hist_data->n_field_var_str++;
3339 static int check_synth_field(struct synth_event *event,
3340 struct hist_field *hist_field,
3341 unsigned int field_pos)
3343 struct synth_field *field;
3345 if (field_pos >= event->n_fields)
3348 field = event->fields[field_pos];
3351 * A dynamic string synth field can accept static or
3352 * dynamic. A static string synth field can only accept a
3353 * same-sized static string, which is checked for later.
3355 if (strstr(hist_field->type, "char[") && field->is_string
3356 && field->is_dynamic)
3359 if (strcmp(field->type, hist_field->type) != 0) {
3360 if (field->size != hist_field->size ||
3361 (!field->is_string && field->is_signed != hist_field->is_signed))
3368 static struct hist_field *
3369 trace_action_find_var(struct hist_trigger_data *hist_data,
3370 struct action_data *data,
3371 char *system, char *event, char *var)
3373 struct trace_array *tr = hist_data->event_file->tr;
3374 struct hist_field *hist_field;
3376 var++; /* skip '$' */
3378 hist_field = find_target_event_var(hist_data, system, event, var);
3380 if (!system && data->handler == HANDLER_ONMATCH) {
3381 system = data->match_data.event_system;
3382 event = data->match_data.event;
3385 hist_field = find_event_var(hist_data, system, event, var);
3389 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3394 static struct hist_field *
3395 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3396 struct action_data *data, char *system,
3397 char *event, char *var)
3399 struct hist_field *hist_field = NULL;
3400 struct field_var *field_var;
3403 * First try to create a field var on the target event (the
3404 * currently being defined). This will create a variable for
3405 * unqualified fields on the target event, or if qualified,
3406 * target fields that have qualified names matching the target.
3408 field_var = create_target_field_var(hist_data, system, event, var);
3410 if (field_var && !IS_ERR(field_var)) {
3411 save_field_var(hist_data, field_var);
3412 hist_field = field_var->var;
3416 * If no explicit system.event is specfied, default to
3417 * looking for fields on the onmatch(system.event.xxx)
3420 if (!system && data->handler == HANDLER_ONMATCH) {
3421 system = data->match_data.event_system;
3422 event = data->match_data.event;
3428 * At this point, we're looking at a field on another
3429 * event. Because we can't modify a hist trigger on
3430 * another event to add a variable for a field, we need
3431 * to create a new trigger on that event and create the
3432 * variable at the same time.
3434 hist_field = create_field_var_hist(hist_data, system, event, var);
3435 if (IS_ERR(hist_field))
3441 destroy_field_var(field_var);
3446 static int trace_action_create(struct hist_trigger_data *hist_data,
3447 struct action_data *data)
3449 struct trace_array *tr = hist_data->event_file->tr;
3450 char *event_name, *param, *system = NULL;
3451 struct hist_field *hist_field, *var_ref;
3453 unsigned int field_pos = 0;
3454 struct synth_event *event;
3455 char *synth_event_name;
3456 int var_ref_idx, ret = 0;
3458 lockdep_assert_held(&event_mutex);
3460 /* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
3461 if (data->n_params > SYNTH_FIELDS_MAX)
3464 if (data->use_trace_keyword)
3465 synth_event_name = data->synth_event_name;
3467 synth_event_name = data->action_name;
3469 event = find_synth_event(synth_event_name);
3471 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3477 for (i = 0; i < data->n_params; i++) {
3480 p = param = kstrdup(data->params[i], GFP_KERNEL);
3486 system = strsep(¶m, ".");
3488 param = (char *)system;
3489 system = event_name = NULL;
3491 event_name = strsep(¶m, ".");
3499 if (param[0] == '$')
3500 hist_field = trace_action_find_var(hist_data, data,
3504 hist_field = trace_action_create_field_var(hist_data,
3516 if (check_synth_field(event, hist_field, field_pos) == 0) {
3517 var_ref = create_var_ref(hist_data, hist_field,
3518 system, event_name);
3525 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
3526 if (WARN_ON(var_ref_idx < 0)) {
3532 data->var_ref_idx[i] = var_ref_idx;
3539 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
3545 if (field_pos != event->n_fields) {
3546 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
3551 data->synth_event = event;
3560 static int action_create(struct hist_trigger_data *hist_data,
3561 struct action_data *data)
3563 struct trace_event_file *file = hist_data->event_file;
3564 struct trace_array *tr = file->tr;
3565 struct track_data *track_data;
3566 struct field_var *field_var;
3571 if (data->action == ACTION_TRACE)
3572 return trace_action_create(hist_data, data);
3574 if (data->action == ACTION_SNAPSHOT) {
3575 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
3576 if (IS_ERR(track_data)) {
3577 ret = PTR_ERR(track_data);
3581 ret = tracing_snapshot_cond_enable(file->tr, track_data,
3582 cond_snapshot_update);
3584 track_data_free(track_data);
3589 if (data->action == ACTION_SAVE) {
3590 if (hist_data->n_save_vars) {
3592 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
3596 for (i = 0; i < data->n_params; i++) {
3597 param = kstrdup(data->params[i], GFP_KERNEL);
3603 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3604 if (IS_ERR(field_var)) {
3605 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
3607 ret = PTR_ERR(field_var);
3612 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
3613 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3614 hist_data->n_save_var_str++;
3622 static int onmatch_create(struct hist_trigger_data *hist_data,
3623 struct action_data *data)
3625 return action_create(hist_data, data);
3628 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3630 char *match_event, *match_event_system;
3631 struct action_data *data;
3634 data = kzalloc(sizeof(*data), GFP_KERNEL);
3636 return ERR_PTR(-ENOMEM);
3638 match_event = strsep(&str, ")");
3639 if (!match_event || !str) {
3640 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
3644 match_event_system = strsep(&match_event, ".");
3646 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
3650 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3651 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
3655 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
3656 if (!data->match_data.event) {
3661 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
3662 if (!data->match_data.event_system) {
3667 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
3673 onmatch_destroy(data);
3674 data = ERR_PTR(ret);
3678 static int create_hitcount_val(struct hist_trigger_data *hist_data)
3680 hist_data->fields[HITCOUNT_IDX] =
3681 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
3682 if (!hist_data->fields[HITCOUNT_IDX])
3685 hist_data->n_vals++;
3686 hist_data->n_fields++;
3688 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3694 static int __create_val_field(struct hist_trigger_data *hist_data,
3695 unsigned int val_idx,
3696 struct trace_event_file *file,
3697 char *var_name, char *field_str,
3698 unsigned long flags)
3700 struct hist_field *hist_field;
3703 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3704 if (IS_ERR(hist_field)) {
3705 ret = PTR_ERR(hist_field);
3709 hist_data->fields[val_idx] = hist_field;
3711 ++hist_data->n_vals;
3712 ++hist_data->n_fields;
3714 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3720 static int create_val_field(struct hist_trigger_data *hist_data,
3721 unsigned int val_idx,
3722 struct trace_event_file *file,
3725 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3728 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3731 static int create_var_field(struct hist_trigger_data *hist_data,
3732 unsigned int val_idx,
3733 struct trace_event_file *file,
3734 char *var_name, char *expr_str)
3736 struct trace_array *tr = hist_data->event_file->tr;
3737 unsigned long flags = 0;
3740 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3743 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
3744 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
3748 flags |= HIST_FIELD_FL_VAR;
3749 hist_data->n_vars++;
3750 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3753 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3755 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
3756 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
3761 static int create_val_fields(struct hist_trigger_data *hist_data,
3762 struct trace_event_file *file)
3764 char *fields_str, *field_str;
3765 unsigned int i, j = 1;
3768 ret = create_hitcount_val(hist_data);
3772 fields_str = hist_data->attrs->vals_str;
3776 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3777 j < TRACING_MAP_VALS_MAX; i++) {
3778 field_str = strsep(&fields_str, ",");
3782 if (strcmp(field_str, "hitcount") == 0)
3785 ret = create_val_field(hist_data, j++, file, field_str);
3790 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3796 static int create_key_field(struct hist_trigger_data *hist_data,
3797 unsigned int key_idx,
3798 unsigned int key_offset,
3799 struct trace_event_file *file,
3802 struct trace_array *tr = hist_data->event_file->tr;
3803 struct hist_field *hist_field = NULL;
3804 unsigned long flags = 0;
3805 unsigned int key_size;
3808 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
3811 flags |= HIST_FIELD_FL_KEY;
3813 if (strcmp(field_str, "stacktrace") == 0) {
3814 flags |= HIST_FIELD_FL_STACKTRACE;
3815 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
3816 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
3818 hist_field = parse_expr(hist_data, file, field_str, flags,
3820 if (IS_ERR(hist_field)) {
3821 ret = PTR_ERR(hist_field);
3825 if (field_has_hist_vars(hist_field, 0)) {
3826 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
3827 destroy_hist_field(hist_field, 0);
3832 key_size = hist_field->size;
3835 hist_data->fields[key_idx] = hist_field;
3837 key_size = ALIGN(key_size, sizeof(u64));
3838 hist_data->fields[key_idx]->size = key_size;
3839 hist_data->fields[key_idx]->offset = key_offset;
3841 hist_data->key_size += key_size;
3843 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3848 hist_data->n_keys++;
3849 hist_data->n_fields++;
3851 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
3859 static int create_key_fields(struct hist_trigger_data *hist_data,
3860 struct trace_event_file *file)
3862 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
3863 char *fields_str, *field_str;
3866 fields_str = hist_data->attrs->keys_str;
3870 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
3871 field_str = strsep(&fields_str, ",");
3874 ret = create_key_field(hist_data, i, key_offset,
3889 static int create_var_fields(struct hist_trigger_data *hist_data,
3890 struct trace_event_file *file)
3892 unsigned int i, j = hist_data->n_vals;
3895 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
3897 for (i = 0; i < n_vars; i++) {
3898 char *var_name = hist_data->attrs->var_defs.name[i];
3899 char *expr = hist_data->attrs->var_defs.expr[i];
3901 ret = create_var_field(hist_data, j++, file, var_name, expr);
3909 static void free_var_defs(struct hist_trigger_data *hist_data)
3913 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3914 kfree(hist_data->attrs->var_defs.name[i]);
3915 kfree(hist_data->attrs->var_defs.expr[i]);
3918 hist_data->attrs->var_defs.n_vars = 0;
3921 static int parse_var_defs(struct hist_trigger_data *hist_data)
3923 struct trace_array *tr = hist_data->event_file->tr;
3924 char *s, *str, *var_name, *field_str;
3925 unsigned int i, j, n_vars = 0;
3928 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
3929 str = hist_data->attrs->assignment_str[i];
3930 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
3931 field_str = strsep(&str, ",");
3935 var_name = strsep(&field_str, "=");
3936 if (!var_name || !field_str) {
3937 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
3943 if (n_vars == TRACING_MAP_VARS_MAX) {
3944 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
3949 s = kstrdup(var_name, GFP_KERNEL);
3954 hist_data->attrs->var_defs.name[n_vars] = s;
3956 s = kstrdup(field_str, GFP_KERNEL);
3958 kfree(hist_data->attrs->var_defs.name[n_vars]);
3959 hist_data->attrs->var_defs.name[n_vars] = NULL;
3963 hist_data->attrs->var_defs.expr[n_vars++] = s;
3965 hist_data->attrs->var_defs.n_vars = n_vars;
3971 free_var_defs(hist_data);
3976 static int create_hist_fields(struct hist_trigger_data *hist_data,
3977 struct trace_event_file *file)
3981 ret = parse_var_defs(hist_data);
3985 ret = create_val_fields(hist_data, file);
3989 ret = create_var_fields(hist_data, file);
3993 ret = create_key_fields(hist_data, file);
3997 free_var_defs(hist_data);
4002 static int is_descending(struct trace_array *tr, const char *str)
4007 if (strcmp(str, "descending") == 0)
4010 if (strcmp(str, "ascending") == 0)
4013 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4018 static int create_sort_keys(struct hist_trigger_data *hist_data)
4020 struct trace_array *tr = hist_data->event_file->tr;
4021 char *fields_str = hist_data->attrs->sort_key_str;
4022 struct tracing_map_sort_key *sort_key;
4023 int descending, ret = 0;
4024 unsigned int i, j, k;
4026 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4031 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4032 struct hist_field *hist_field;
4033 char *field_str, *field_name;
4034 const char *test_name;
4036 sort_key = &hist_data->sort_keys[i];
4038 field_str = strsep(&fields_str, ",");
4044 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4048 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4049 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4054 field_name = strsep(&field_str, ".");
4055 if (!field_name || !*field_name) {
4057 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4061 if (strcmp(field_name, "hitcount") == 0) {
4062 descending = is_descending(tr, field_str);
4063 if (descending < 0) {
4067 sort_key->descending = descending;
4071 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4074 hist_field = hist_data->fields[j];
4075 if (hist_field->flags & HIST_FIELD_FL_VAR)
4080 test_name = hist_field_name(hist_field, 0);
4082 if (strcmp(field_name, test_name) == 0) {
4083 sort_key->field_idx = idx;
4084 descending = is_descending(tr, field_str);
4085 if (descending < 0) {
4089 sort_key->descending = descending;
4093 if (j == hist_data->n_fields) {
4095 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4100 hist_data->n_sort_keys = i;
4105 static void destroy_actions(struct hist_trigger_data *hist_data)
4109 for (i = 0; i < hist_data->n_actions; i++) {
4110 struct action_data *data = hist_data->actions[i];
4112 if (data->handler == HANDLER_ONMATCH)
4113 onmatch_destroy(data);
4114 else if (data->handler == HANDLER_ONMAX ||
4115 data->handler == HANDLER_ONCHANGE)
4116 track_data_destroy(hist_data, data);
4122 static int parse_actions(struct hist_trigger_data *hist_data)
4124 struct trace_array *tr = hist_data->event_file->tr;
4125 struct action_data *data;
4131 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4132 str = hist_data->attrs->action_str[i];
4134 if ((len = str_has_prefix(str, "onmatch("))) {
4135 char *action_str = str + len;
4137 data = onmatch_parse(tr, action_str);
4139 ret = PTR_ERR(data);
4142 } else if ((len = str_has_prefix(str, "onmax("))) {
4143 char *action_str = str + len;
4145 data = track_data_parse(hist_data, action_str,
4148 ret = PTR_ERR(data);
4151 } else if ((len = str_has_prefix(str, "onchange("))) {
4152 char *action_str = str + len;
4154 data = track_data_parse(hist_data, action_str,
4157 ret = PTR_ERR(data);
4165 hist_data->actions[hist_data->n_actions++] = data;
4171 static int create_actions(struct hist_trigger_data *hist_data)
4173 struct action_data *data;
4177 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4178 data = hist_data->actions[i];
4180 if (data->handler == HANDLER_ONMATCH) {
4181 ret = onmatch_create(hist_data, data);
4184 } else if (data->handler == HANDLER_ONMAX ||
4185 data->handler == HANDLER_ONCHANGE) {
4186 ret = track_data_create(hist_data, data);
4198 static void print_actions(struct seq_file *m,
4199 struct hist_trigger_data *hist_data,
4200 struct tracing_map_elt *elt)
4204 for (i = 0; i < hist_data->n_actions; i++) {
4205 struct action_data *data = hist_data->actions[i];
4207 if (data->action == ACTION_SNAPSHOT)
4210 if (data->handler == HANDLER_ONMAX ||
4211 data->handler == HANDLER_ONCHANGE)
4212 track_data_print(m, hist_data, elt, data);
4216 static void print_action_spec(struct seq_file *m,
4217 struct hist_trigger_data *hist_data,
4218 struct action_data *data)
4222 if (data->action == ACTION_SAVE) {
4223 for (i = 0; i < hist_data->n_save_vars; i++) {
4224 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4225 if (i < hist_data->n_save_vars - 1)
4228 } else if (data->action == ACTION_TRACE) {
4229 if (data->use_trace_keyword)
4230 seq_printf(m, "%s", data->synth_event_name);
4231 for (i = 0; i < data->n_params; i++) {
4232 if (i || data->use_trace_keyword)
4234 seq_printf(m, "%s", data->params[i]);
4239 static void print_track_data_spec(struct seq_file *m,
4240 struct hist_trigger_data *hist_data,
4241 struct action_data *data)
4243 if (data->handler == HANDLER_ONMAX)
4244 seq_puts(m, ":onmax(");
4245 else if (data->handler == HANDLER_ONCHANGE)
4246 seq_puts(m, ":onchange(");
4247 seq_printf(m, "%s", data->track_data.var_str);
4248 seq_printf(m, ").%s(", data->action_name);
4250 print_action_spec(m, hist_data, data);
4255 static void print_onmatch_spec(struct seq_file *m,
4256 struct hist_trigger_data *hist_data,
4257 struct action_data *data)
4259 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4260 data->match_data.event);
4262 seq_printf(m, "%s(", data->action_name);
4264 print_action_spec(m, hist_data, data);
4269 static bool actions_match(struct hist_trigger_data *hist_data,
4270 struct hist_trigger_data *hist_data_test)
4274 if (hist_data->n_actions != hist_data_test->n_actions)
4277 for (i = 0; i < hist_data->n_actions; i++) {
4278 struct action_data *data = hist_data->actions[i];
4279 struct action_data *data_test = hist_data_test->actions[i];
4280 char *action_name, *action_name_test;
4282 if (data->handler != data_test->handler)
4284 if (data->action != data_test->action)
4287 if (data->n_params != data_test->n_params)
4290 for (j = 0; j < data->n_params; j++) {
4291 if (strcmp(data->params[j], data_test->params[j]) != 0)
4295 if (data->use_trace_keyword)
4296 action_name = data->synth_event_name;
4298 action_name = data->action_name;
4300 if (data_test->use_trace_keyword)
4301 action_name_test = data_test->synth_event_name;
4303 action_name_test = data_test->action_name;
4305 if (strcmp(action_name, action_name_test) != 0)
4308 if (data->handler == HANDLER_ONMATCH) {
4309 if (strcmp(data->match_data.event_system,
4310 data_test->match_data.event_system) != 0)
4312 if (strcmp(data->match_data.event,
4313 data_test->match_data.event) != 0)
4315 } else if (data->handler == HANDLER_ONMAX ||
4316 data->handler == HANDLER_ONCHANGE) {
4317 if (strcmp(data->track_data.var_str,
4318 data_test->track_data.var_str) != 0)
4327 static void print_actions_spec(struct seq_file *m,
4328 struct hist_trigger_data *hist_data)
4332 for (i = 0; i < hist_data->n_actions; i++) {
4333 struct action_data *data = hist_data->actions[i];
4335 if (data->handler == HANDLER_ONMATCH)
4336 print_onmatch_spec(m, hist_data, data);
4337 else if (data->handler == HANDLER_ONMAX ||
4338 data->handler == HANDLER_ONCHANGE)
4339 print_track_data_spec(m, hist_data, data);
4343 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4347 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4348 kfree(hist_data->field_var_hists[i]->cmd);
4349 kfree(hist_data->field_var_hists[i]);
4353 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4358 destroy_hist_trigger_attrs(hist_data->attrs);
4359 destroy_hist_fields(hist_data);
4360 tracing_map_destroy(hist_data->map);
4362 destroy_actions(hist_data);
4363 destroy_field_vars(hist_data);
4364 destroy_field_var_hists(hist_data);
4369 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4371 struct tracing_map *map = hist_data->map;
4372 struct ftrace_event_field *field;
4373 struct hist_field *hist_field;
4376 for_each_hist_field(i, hist_data) {
4377 hist_field = hist_data->fields[i];
4378 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4379 tracing_map_cmp_fn_t cmp_fn;
4381 field = hist_field->field;
4383 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4384 cmp_fn = tracing_map_cmp_none;
4385 else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
4386 cmp_fn = tracing_map_cmp_num(hist_field->size,
4387 hist_field->is_signed);
4388 else if (is_string_field(field))
4389 cmp_fn = tracing_map_cmp_string;
4391 cmp_fn = tracing_map_cmp_num(field->size,
4393 idx = tracing_map_add_key_field(map,
4396 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4397 idx = tracing_map_add_sum_field(map);
4402 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4403 idx = tracing_map_add_var(map);
4406 hist_field->var.idx = idx;
4407 hist_field->var.hist_data = hist_data;
4414 static struct hist_trigger_data *
4415 create_hist_data(unsigned int map_bits,
4416 struct hist_trigger_attrs *attrs,
4417 struct trace_event_file *file,
4420 const struct tracing_map_ops *map_ops = NULL;
4421 struct hist_trigger_data *hist_data;
4424 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4426 return ERR_PTR(-ENOMEM);
4428 hist_data->attrs = attrs;
4429 hist_data->remove = remove;
4430 hist_data->event_file = file;
4432 ret = parse_actions(hist_data);
4436 ret = create_hist_fields(hist_data, file);
4440 ret = create_sort_keys(hist_data);
4444 map_ops = &hist_trigger_elt_data_ops;
4446 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4447 map_ops, hist_data);
4448 if (IS_ERR(hist_data->map)) {
4449 ret = PTR_ERR(hist_data->map);
4450 hist_data->map = NULL;
4454 ret = create_tracing_map_fields(hist_data);
4460 hist_data->attrs = NULL;
4462 destroy_hist_data(hist_data);
4464 hist_data = ERR_PTR(ret);
4469 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4470 struct tracing_map_elt *elt, void *rec,
4471 struct ring_buffer_event *rbe,
4474 struct hist_elt_data *elt_data;
4475 struct hist_field *hist_field;
4476 unsigned int i, var_idx;
4479 elt_data = elt->private_data;
4480 elt_data->var_ref_vals = var_ref_vals;
4482 for_each_hist_val_field(i, hist_data) {
4483 hist_field = hist_data->fields[i];
4484 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4485 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4486 var_idx = hist_field->var.idx;
4488 if (hist_field->flags & HIST_FIELD_FL_STRING) {
4489 unsigned int str_start, var_str_idx, idx;
4490 char *str, *val_str;
4493 str_start = hist_data->n_field_var_str +
4494 hist_data->n_save_var_str;
4495 var_str_idx = hist_field->var_str_idx;
4496 idx = str_start + var_str_idx;
4498 str = elt_data->field_var_str[idx];
4499 val_str = (char *)(uintptr_t)hist_val;
4501 size = min(hist_field->size, STR_VAR_LEN_MAX);
4502 strscpy(str, val_str, size);
4504 hist_val = (u64)(uintptr_t)str;
4506 tracing_map_set_var(elt, var_idx, hist_val);
4509 tracing_map_update_sum(elt, i, hist_val);
4512 for_each_hist_key_field(i, hist_data) {
4513 hist_field = hist_data->fields[i];
4514 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4515 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
4516 var_idx = hist_field->var.idx;
4517 tracing_map_set_var(elt, var_idx, hist_val);
4521 update_field_vars(hist_data, elt, rbe, rec);
4524 static inline void add_to_key(char *compound_key, void *key,
4525 struct hist_field *key_field, void *rec)
4527 size_t size = key_field->size;
4529 if (key_field->flags & HIST_FIELD_FL_STRING) {
4530 struct ftrace_event_field *field;
4532 field = key_field->field;
4533 if (field->filter_type == FILTER_DYN_STRING)
4534 size = *(u32 *)(rec + field->offset) >> 16;
4535 else if (field->filter_type == FILTER_STATIC_STRING)
4538 /* ensure NULL-termination */
4539 if (size > key_field->size - 1)
4540 size = key_field->size - 1;
4542 strncpy(compound_key + key_field->offset, (char *)key, size);
4544 memcpy(compound_key + key_field->offset, key, size);
4548 hist_trigger_actions(struct hist_trigger_data *hist_data,
4549 struct tracing_map_elt *elt, void *rec,
4550 struct ring_buffer_event *rbe, void *key,
4553 struct action_data *data;
4556 for (i = 0; i < hist_data->n_actions; i++) {
4557 data = hist_data->actions[i];
4558 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4562 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
4563 struct ring_buffer_event *rbe)
4565 struct hist_trigger_data *hist_data = data->private_data;
4566 bool use_compound_key = (hist_data->n_keys > 1);
4567 unsigned long entries[HIST_STACKTRACE_DEPTH];
4568 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4569 char compound_key[HIST_KEY_SIZE_MAX];
4570 struct tracing_map_elt *elt = NULL;
4571 struct hist_field *key_field;
4576 memset(compound_key, 0, hist_data->key_size);
4578 for_each_hist_key_field(i, hist_data) {
4579 key_field = hist_data->fields[i];
4581 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4582 memset(entries, 0, HIST_STACKTRACE_SIZE);
4583 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
4584 HIST_STACKTRACE_SKIP);
4587 field_contents = key_field->fn(key_field, elt, rbe, rec);
4588 if (key_field->flags & HIST_FIELD_FL_STRING) {
4589 key = (void *)(unsigned long)field_contents;
4590 use_compound_key = true;
4592 key = (void *)&field_contents;
4595 if (use_compound_key)
4596 add_to_key(compound_key, key, key_field, rec);
4599 if (use_compound_key)
4602 if (hist_data->n_var_refs &&
4603 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4606 elt = tracing_map_insert(hist_data->map, key);
4610 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
4612 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4613 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
4616 static void hist_trigger_stacktrace_print(struct seq_file *m,
4617 unsigned long *stacktrace_entries,
4618 unsigned int max_entries)
4620 char str[KSYM_SYMBOL_LEN];
4621 unsigned int spaces = 8;
4624 for (i = 0; i < max_entries; i++) {
4625 if (!stacktrace_entries[i])
4628 seq_printf(m, "%*c", 1 + spaces, ' ');
4629 sprint_symbol(str, stacktrace_entries[i]);
4630 seq_printf(m, "%s\n", str);
4634 static void hist_trigger_print_key(struct seq_file *m,
4635 struct hist_trigger_data *hist_data,
4637 struct tracing_map_elt *elt)
4639 struct hist_field *key_field;
4640 char str[KSYM_SYMBOL_LEN];
4641 bool multiline = false;
4642 const char *field_name;
4648 for_each_hist_key_field(i, hist_data) {
4649 key_field = hist_data->fields[i];
4651 if (i > hist_data->n_vals)
4654 field_name = hist_field_name(key_field, 0);
4656 if (key_field->flags & HIST_FIELD_FL_HEX) {
4657 uval = *(u64 *)(key + key_field->offset);
4658 seq_printf(m, "%s: %llx", field_name, uval);
4659 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4660 uval = *(u64 *)(key + key_field->offset);
4661 sprint_symbol_no_offset(str, uval);
4662 seq_printf(m, "%s: [%llx] %-45s", field_name,
4664 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4665 uval = *(u64 *)(key + key_field->offset);
4666 sprint_symbol(str, uval);
4667 seq_printf(m, "%s: [%llx] %-55s", field_name,
4669 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
4670 struct hist_elt_data *elt_data = elt->private_data;
4673 if (WARN_ON_ONCE(!elt_data))
4676 comm = elt_data->comm;
4678 uval = *(u64 *)(key + key_field->offset);
4679 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4681 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4682 const char *syscall_name;
4684 uval = *(u64 *)(key + key_field->offset);
4685 syscall_name = get_syscall_name(uval);
4687 syscall_name = "unknown_syscall";
4689 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4690 syscall_name, uval);
4691 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4692 seq_puts(m, "stacktrace:\n");
4693 hist_trigger_stacktrace_print(m,
4694 key + key_field->offset,
4695 HIST_STACKTRACE_DEPTH);
4697 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
4698 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
4699 *(u64 *)(key + key_field->offset));
4700 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
4701 seq_printf(m, "%s: %-50s", field_name,
4702 (char *)(key + key_field->offset));
4704 uval = *(u64 *)(key + key_field->offset);
4705 seq_printf(m, "%s: %10llu", field_name, uval);
4715 static void hist_trigger_entry_print(struct seq_file *m,
4716 struct hist_trigger_data *hist_data,
4718 struct tracing_map_elt *elt)
4720 const char *field_name;
4723 hist_trigger_print_key(m, hist_data, key, elt);
4725 seq_printf(m, " hitcount: %10llu",
4726 tracing_map_read_sum(elt, HITCOUNT_IDX));
4728 for (i = 1; i < hist_data->n_vals; i++) {
4729 field_name = hist_field_name(hist_data->fields[i], 0);
4731 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4732 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
4735 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
4736 seq_printf(m, " %s: %10llx", field_name,
4737 tracing_map_read_sum(elt, i));
4739 seq_printf(m, " %s: %10llu", field_name,
4740 tracing_map_read_sum(elt, i));
4744 print_actions(m, hist_data, elt);
4749 static int print_entries(struct seq_file *m,
4750 struct hist_trigger_data *hist_data)
4752 struct tracing_map_sort_entry **sort_entries = NULL;
4753 struct tracing_map *map = hist_data->map;
4756 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4757 hist_data->n_sort_keys,
4762 for (i = 0; i < n_entries; i++)
4763 hist_trigger_entry_print(m, hist_data,
4764 sort_entries[i]->key,
4765 sort_entries[i]->elt);
4767 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4772 static void hist_trigger_show(struct seq_file *m,
4773 struct event_trigger_data *data, int n)
4775 struct hist_trigger_data *hist_data;
4779 seq_puts(m, "\n\n");
4781 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4782 data->ops->print(m, data->ops, data);
4783 seq_puts(m, "#\n\n");
4785 hist_data = data->private_data;
4786 n_entries = print_entries(m, hist_data);
4790 track_data_snapshot_print(m, hist_data);
4792 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4793 (u64)atomic64_read(&hist_data->map->hits),
4794 n_entries, (u64)atomic64_read(&hist_data->map->drops));
4797 static int hist_show(struct seq_file *m, void *v)
4799 struct event_trigger_data *data;
4800 struct trace_event_file *event_file;
4803 mutex_lock(&event_mutex);
4805 event_file = event_file_data(m->private);
4806 if (unlikely(!event_file)) {
4811 list_for_each_entry(data, &event_file->triggers, list) {
4812 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4813 hist_trigger_show(m, data, n++);
4817 mutex_unlock(&event_mutex);
4822 static int event_hist_open(struct inode *inode, struct file *file)
4826 ret = security_locked_down(LOCKDOWN_TRACEFS);
4830 return single_open(file, hist_show, file);
4833 const struct file_operations event_hist_fops = {
4834 .open = event_hist_open,
4836 .llseek = seq_lseek,
4837 .release = single_release,
4840 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
4841 static void hist_field_debug_show_flags(struct seq_file *m,
4842 unsigned long flags)
4844 seq_puts(m, " flags:\n");
4846 if (flags & HIST_FIELD_FL_KEY)
4847 seq_puts(m, " HIST_FIELD_FL_KEY\n");
4848 else if (flags & HIST_FIELD_FL_HITCOUNT)
4849 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
4850 else if (flags & HIST_FIELD_FL_VAR)
4851 seq_puts(m, " HIST_FIELD_FL_VAR\n");
4852 else if (flags & HIST_FIELD_FL_VAR_REF)
4853 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
4855 seq_puts(m, " VAL: normal u64 value\n");
4857 if (flags & HIST_FIELD_FL_ALIAS)
4858 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
4861 static int hist_field_debug_show(struct seq_file *m,
4862 struct hist_field *field, unsigned long flags)
4864 if ((field->flags & flags) != flags) {
4865 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
4869 hist_field_debug_show_flags(m, field->flags);
4871 seq_printf(m, " ftrace_event_field name: %s\n",
4872 field->field->name);
4874 if (field->flags & HIST_FIELD_FL_VAR) {
4875 seq_printf(m, " var.name: %s\n", field->var.name);
4876 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4880 if (field->flags & HIST_FIELD_FL_ALIAS)
4881 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4882 field->var_ref_idx);
4884 if (field->flags & HIST_FIELD_FL_VAR_REF) {
4885 seq_printf(m, " name: %s\n", field->name);
4886 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4888 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
4889 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
4890 field->var_ref_idx);
4892 seq_printf(m, " system: %s\n", field->system);
4893 if (field->event_name)
4894 seq_printf(m, " event_name: %s\n", field->event_name);
4897 seq_printf(m, " type: %s\n", field->type);
4898 seq_printf(m, " size: %u\n", field->size);
4899 seq_printf(m, " is_signed: %u\n", field->is_signed);
4904 static int field_var_debug_show(struct seq_file *m,
4905 struct field_var *field_var, unsigned int i,
4908 const char *vars_name = save_vars ? "save_vars" : "field_vars";
4909 struct hist_field *field;
4912 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
4914 field = field_var->var;
4916 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
4918 hist_field_debug_show_flags(m, field->flags);
4919 seq_printf(m, " var.name: %s\n", field->var.name);
4920 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
4923 field = field_var->val;
4925 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
4927 seq_printf(m, " ftrace_event_field name: %s\n",
4928 field->field->name);
4934 seq_printf(m, " type: %s\n", field->type);
4935 seq_printf(m, " size: %u\n", field->size);
4936 seq_printf(m, " is_signed: %u\n", field->is_signed);
4941 static int hist_action_debug_show(struct seq_file *m,
4942 struct action_data *data, int i)
4946 if (data->handler == HANDLER_ONMAX ||
4947 data->handler == HANDLER_ONCHANGE) {
4948 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
4949 ret = hist_field_debug_show(m, data->track_data.var_ref,
4950 HIST_FIELD_FL_VAR_REF);
4954 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
4955 ret = hist_field_debug_show(m, data->track_data.track_var,
4961 if (data->handler == HANDLER_ONMATCH) {
4962 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
4963 i, data->match_data.event_system);
4964 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
4965 i, data->match_data.event);
4971 static int hist_actions_debug_show(struct seq_file *m,
4972 struct hist_trigger_data *hist_data)
4976 if (hist_data->n_actions)
4977 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
4979 for (i = 0; i < hist_data->n_actions; i++) {
4980 struct action_data *action = hist_data->actions[i];
4982 ret = hist_action_debug_show(m, action, i);
4987 if (hist_data->n_save_vars)
4988 seq_puts(m, "\n save action variables (save() params):\n");
4990 for (i = 0; i < hist_data->n_save_vars; i++) {
4991 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
4999 static void hist_trigger_debug_show(struct seq_file *m,
5000 struct event_trigger_data *data, int n)
5002 struct hist_trigger_data *hist_data;
5006 seq_puts(m, "\n\n");
5008 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5009 data->ops->print(m, data->ops, data);
5010 seq_puts(m, "#\n\n");
5012 hist_data = data->private_data;
5014 seq_printf(m, "hist_data: %p\n\n", hist_data);
5015 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
5016 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
5017 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
5019 seq_puts(m, "\n val fields:\n\n");
5021 seq_puts(m, " hist_data->fields[0]:\n");
5022 ret = hist_field_debug_show(m, hist_data->fields[0],
5023 HIST_FIELD_FL_HITCOUNT);
5027 for (i = 1; i < hist_data->n_vals; i++) {
5028 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5029 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5034 seq_puts(m, "\n key fields:\n");
5036 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5037 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5038 ret = hist_field_debug_show(m, hist_data->fields[i],
5044 if (hist_data->n_var_refs)
5045 seq_puts(m, "\n variable reference fields:\n");
5047 for (i = 0; i < hist_data->n_var_refs; i++) {
5048 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5049 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5050 HIST_FIELD_FL_VAR_REF);
5055 if (hist_data->n_field_vars)
5056 seq_puts(m, "\n field variables:\n");
5058 for (i = 0; i < hist_data->n_field_vars; i++) {
5059 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5064 ret = hist_actions_debug_show(m, hist_data);
5069 static int hist_debug_show(struct seq_file *m, void *v)
5071 struct event_trigger_data *data;
5072 struct trace_event_file *event_file;
5075 mutex_lock(&event_mutex);
5077 event_file = event_file_data(m->private);
5078 if (unlikely(!event_file)) {
5083 list_for_each_entry(data, &event_file->triggers, list) {
5084 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5085 hist_trigger_debug_show(m, data, n++);
5089 mutex_unlock(&event_mutex);
5094 static int event_hist_debug_open(struct inode *inode, struct file *file)
5098 ret = security_locked_down(LOCKDOWN_TRACEFS);
5102 return single_open(file, hist_debug_show, file);
5105 const struct file_operations event_hist_debug_fops = {
5106 .open = event_hist_debug_open,
5108 .llseek = seq_lseek,
5109 .release = single_release,
5113 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5115 const char *field_name = hist_field_name(hist_field, 0);
5117 if (hist_field->var.name)
5118 seq_printf(m, "%s=", hist_field->var.name);
5120 if (hist_field->flags & HIST_FIELD_FL_CPU)
5121 seq_puts(m, "common_cpu");
5122 else if (field_name) {
5123 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5124 hist_field->flags & HIST_FIELD_FL_ALIAS)
5126 seq_printf(m, "%s", field_name);
5127 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5128 seq_puts(m, "common_timestamp");
5130 if (hist_field->flags) {
5131 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5132 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5133 const char *flags = get_hist_field_flags(hist_field);
5136 seq_printf(m, ".%s", flags);
5141 static int event_hist_trigger_print(struct seq_file *m,
5142 struct event_trigger_ops *ops,
5143 struct event_trigger_data *data)
5145 struct hist_trigger_data *hist_data = data->private_data;
5146 struct hist_field *field;
5147 bool have_var = false;
5150 seq_puts(m, "hist:");
5153 seq_printf(m, "%s:", data->name);
5155 seq_puts(m, "keys=");
5157 for_each_hist_key_field(i, hist_data) {
5158 field = hist_data->fields[i];
5160 if (i > hist_data->n_vals)
5163 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5164 seq_puts(m, "stacktrace");
5166 hist_field_print(m, field);
5169 seq_puts(m, ":vals=");
5171 for_each_hist_val_field(i, hist_data) {
5172 field = hist_data->fields[i];
5173 if (field->flags & HIST_FIELD_FL_VAR) {
5178 if (i == HITCOUNT_IDX)
5179 seq_puts(m, "hitcount");
5182 hist_field_print(m, field);
5191 for_each_hist_val_field(i, hist_data) {
5192 field = hist_data->fields[i];
5194 if (field->flags & HIST_FIELD_FL_VAR) {
5197 hist_field_print(m, field);
5202 seq_puts(m, ":sort=");
5204 for (i = 0; i < hist_data->n_sort_keys; i++) {
5205 struct tracing_map_sort_key *sort_key;
5206 unsigned int idx, first_key_idx;
5209 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5211 sort_key = &hist_data->sort_keys[i];
5212 idx = sort_key->field_idx;
5214 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5220 if (idx == HITCOUNT_IDX)
5221 seq_puts(m, "hitcount");
5223 if (idx >= first_key_idx)
5224 idx += hist_data->n_vars;
5225 hist_field_print(m, hist_data->fields[idx]);
5228 if (sort_key->descending)
5229 seq_puts(m, ".descending");
5231 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5232 if (hist_data->enable_timestamps)
5233 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5235 print_actions_spec(m, hist_data);
5237 if (data->filter_str)
5238 seq_printf(m, " if %s", data->filter_str);
5241 seq_puts(m, " [paused]");
5243 seq_puts(m, " [active]");
5250 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5251 struct event_trigger_data *data)
5253 struct hist_trigger_data *hist_data = data->private_data;
5255 if (!data->ref && hist_data->attrs->name)
5256 save_named_trigger(hist_data->attrs->name, data);
5263 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5265 struct trace_event_file *file;
5270 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5271 file = hist_data->field_var_hists[i]->hist_data->event_file;
5272 cmd = hist_data->field_var_hists[i]->cmd;
5273 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5274 "!hist", "hist", cmd);
5278 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5279 struct event_trigger_data *data)
5281 struct hist_trigger_data *hist_data = data->private_data;
5283 if (WARN_ON_ONCE(data->ref <= 0))
5289 del_named_trigger(data);
5291 trigger_data_free(data);
5293 remove_hist_vars(hist_data);
5295 unregister_field_var_hists(hist_data);
5297 destroy_hist_data(hist_data);
5301 static struct event_trigger_ops event_hist_trigger_ops = {
5302 .func = event_hist_trigger,
5303 .print = event_hist_trigger_print,
5304 .init = event_hist_trigger_init,
5305 .free = event_hist_trigger_free,
5308 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5309 struct event_trigger_data *data)
5313 save_named_trigger(data->named_data->name, data);
5315 event_hist_trigger_init(ops, data->named_data);
5320 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5321 struct event_trigger_data *data)
5323 if (WARN_ON_ONCE(data->ref <= 0))
5326 event_hist_trigger_free(ops, data->named_data);
5330 del_named_trigger(data);
5331 trigger_data_free(data);
5335 static struct event_trigger_ops event_hist_trigger_named_ops = {
5336 .func = event_hist_trigger,
5337 .print = event_hist_trigger_print,
5338 .init = event_hist_trigger_named_init,
5339 .free = event_hist_trigger_named_free,
5342 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5345 return &event_hist_trigger_ops;
5348 static void hist_clear(struct event_trigger_data *data)
5350 struct hist_trigger_data *hist_data = data->private_data;
5353 pause_named_trigger(data);
5355 tracepoint_synchronize_unregister();
5357 tracing_map_clear(hist_data->map);
5360 unpause_named_trigger(data);
5363 static bool compatible_field(struct ftrace_event_field *field,
5364 struct ftrace_event_field *test_field)
5366 if (field == test_field)
5368 if (field == NULL || test_field == NULL)
5370 if (strcmp(field->name, test_field->name) != 0)
5372 if (strcmp(field->type, test_field->type) != 0)
5374 if (field->size != test_field->size)
5376 if (field->is_signed != test_field->is_signed)
5382 static bool hist_trigger_match(struct event_trigger_data *data,
5383 struct event_trigger_data *data_test,
5384 struct event_trigger_data *named_data,
5387 struct tracing_map_sort_key *sort_key, *sort_key_test;
5388 struct hist_trigger_data *hist_data, *hist_data_test;
5389 struct hist_field *key_field, *key_field_test;
5392 if (named_data && (named_data != data_test) &&
5393 (named_data != data_test->named_data))
5396 if (!named_data && is_named_trigger(data_test))
5399 hist_data = data->private_data;
5400 hist_data_test = data_test->private_data;
5402 if (hist_data->n_vals != hist_data_test->n_vals ||
5403 hist_data->n_fields != hist_data_test->n_fields ||
5404 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5407 if (!ignore_filter) {
5408 if ((data->filter_str && !data_test->filter_str) ||
5409 (!data->filter_str && data_test->filter_str))
5413 for_each_hist_field(i, hist_data) {
5414 key_field = hist_data->fields[i];
5415 key_field_test = hist_data_test->fields[i];
5417 if (key_field->flags != key_field_test->flags)
5419 if (!compatible_field(key_field->field, key_field_test->field))
5421 if (key_field->offset != key_field_test->offset)
5423 if (key_field->size != key_field_test->size)
5425 if (key_field->is_signed != key_field_test->is_signed)
5427 if (!!key_field->var.name != !!key_field_test->var.name)
5429 if (key_field->var.name &&
5430 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5434 for (i = 0; i < hist_data->n_sort_keys; i++) {
5435 sort_key = &hist_data->sort_keys[i];
5436 sort_key_test = &hist_data_test->sort_keys[i];
5438 if (sort_key->field_idx != sort_key_test->field_idx ||
5439 sort_key->descending != sort_key_test->descending)
5443 if (!ignore_filter && data->filter_str &&
5444 (strcmp(data->filter_str, data_test->filter_str) != 0))
5447 if (!actions_match(hist_data, hist_data_test))
5453 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5454 struct event_trigger_data *data,
5455 struct trace_event_file *file)
5457 struct hist_trigger_data *hist_data = data->private_data;
5458 struct event_trigger_data *test, *named_data = NULL;
5459 struct trace_array *tr = file->tr;
5462 if (hist_data->attrs->name) {
5463 named_data = find_named_trigger(hist_data->attrs->name);
5465 if (!hist_trigger_match(data, named_data, named_data,
5467 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5474 if (hist_data->attrs->name && !named_data)
5477 lockdep_assert_held(&event_mutex);
5479 list_for_each_entry(test, &file->triggers, list) {
5480 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5481 if (!hist_trigger_match(data, test, named_data, false))
5483 if (hist_data->attrs->pause)
5484 test->paused = true;
5485 else if (hist_data->attrs->cont)
5486 test->paused = false;
5487 else if (hist_data->attrs->clear)
5490 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5497 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5498 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5503 if (hist_data->attrs->pause)
5504 data->paused = true;
5507 data->private_data = named_data->private_data;
5508 set_named_trigger_data(data, named_data);
5509 data->ops = &event_hist_trigger_named_ops;
5512 if (data->ops->init) {
5513 ret = data->ops->init(data->ops, data);
5518 if (hist_data->enable_timestamps) {
5519 char *clock = hist_data->attrs->clock;
5521 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5523 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5527 tracing_set_time_stamp_abs(file->tr, true);
5531 destroy_hist_data(hist_data);
5538 static int hist_trigger_enable(struct event_trigger_data *data,
5539 struct trace_event_file *file)
5543 list_add_tail_rcu(&data->list, &file->triggers);
5545 update_cond_flag(file);
5547 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5548 list_del_rcu(&data->list);
5549 update_cond_flag(file);
5556 static bool have_hist_trigger_match(struct event_trigger_data *data,
5557 struct trace_event_file *file)
5559 struct hist_trigger_data *hist_data = data->private_data;
5560 struct event_trigger_data *test, *named_data = NULL;
5563 lockdep_assert_held(&event_mutex);
5565 if (hist_data->attrs->name)
5566 named_data = find_named_trigger(hist_data->attrs->name);
5568 list_for_each_entry(test, &file->triggers, list) {
5569 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5570 if (hist_trigger_match(data, test, named_data, false)) {
5580 static bool hist_trigger_check_refs(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;
5586 lockdep_assert_held(&event_mutex);
5588 if (hist_data->attrs->name)
5589 named_data = find_named_trigger(hist_data->attrs->name);
5591 list_for_each_entry(test, &file->triggers, list) {
5592 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5593 if (!hist_trigger_match(data, test, named_data, false))
5595 hist_data = test->private_data;
5596 if (check_var_refs(hist_data))
5605 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5606 struct event_trigger_data *data,
5607 struct trace_event_file *file)
5609 struct hist_trigger_data *hist_data = data->private_data;
5610 struct event_trigger_data *test, *named_data = NULL;
5611 bool unregistered = false;
5613 lockdep_assert_held(&event_mutex);
5615 if (hist_data->attrs->name)
5616 named_data = find_named_trigger(hist_data->attrs->name);
5618 list_for_each_entry(test, &file->triggers, list) {
5619 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5620 if (!hist_trigger_match(data, test, named_data, false))
5622 unregistered = true;
5623 list_del_rcu(&test->list);
5624 trace_event_trigger_enable_disable(file, 0);
5625 update_cond_flag(file);
5630 if (unregistered && test->ops->free)
5631 test->ops->free(test->ops, test);
5633 if (hist_data->enable_timestamps) {
5634 if (!hist_data->remove || unregistered)
5635 tracing_set_time_stamp_abs(file->tr, false);
5639 static bool hist_file_check_refs(struct trace_event_file *file)
5641 struct hist_trigger_data *hist_data;
5642 struct event_trigger_data *test;
5644 lockdep_assert_held(&event_mutex);
5646 list_for_each_entry(test, &file->triggers, list) {
5647 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5648 hist_data = test->private_data;
5649 if (check_var_refs(hist_data))
5657 static void hist_unreg_all(struct trace_event_file *file)
5659 struct event_trigger_data *test, *n;
5660 struct hist_trigger_data *hist_data;
5661 struct synth_event *se;
5662 const char *se_name;
5664 lockdep_assert_held(&event_mutex);
5666 if (hist_file_check_refs(file))
5669 list_for_each_entry_safe(test, n, &file->triggers, list) {
5670 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5671 hist_data = test->private_data;
5672 list_del_rcu(&test->list);
5673 trace_event_trigger_enable_disable(file, 0);
5675 se_name = trace_event_name(file->event_call);
5676 se = find_synth_event(se_name);
5680 update_cond_flag(file);
5681 if (hist_data->enable_timestamps)
5682 tracing_set_time_stamp_abs(file->tr, false);
5683 if (test->ops->free)
5684 test->ops->free(test->ops, test);
5689 static int event_hist_trigger_func(struct event_command *cmd_ops,
5690 struct trace_event_file *file,
5691 char *glob, char *cmd, char *param)
5693 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5694 struct event_trigger_data *trigger_data;
5695 struct hist_trigger_attrs *attrs;
5696 struct event_trigger_ops *trigger_ops;
5697 struct hist_trigger_data *hist_data;
5698 struct synth_event *se;
5699 const char *se_name;
5700 bool remove = false;
5704 lockdep_assert_held(&event_mutex);
5706 if (glob && strlen(glob)) {
5708 last_cmd_set(file, param);
5718 * separate the trigger from the filter (k:v [if filter])
5719 * allowing for whitespace in the trigger
5721 p = trigger = param;
5723 p = strstr(p, "if");
5728 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5732 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
5734 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
5745 param = strstrip(p);
5746 trigger = strstrip(trigger);
5749 attrs = parse_hist_trigger_attrs(file->tr, trigger);
5751 return PTR_ERR(attrs);
5753 if (attrs->map_bits)
5754 hist_trigger_bits = attrs->map_bits;
5756 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
5757 if (IS_ERR(hist_data)) {
5758 destroy_hist_trigger_attrs(attrs);
5759 return PTR_ERR(hist_data);
5762 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5764 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
5765 if (!trigger_data) {
5770 trigger_data->count = -1;
5771 trigger_data->ops = trigger_ops;
5772 trigger_data->cmd_ops = cmd_ops;
5774 INIT_LIST_HEAD(&trigger_data->list);
5775 RCU_INIT_POINTER(trigger_data->filter, NULL);
5777 trigger_data->private_data = hist_data;
5779 /* if param is non-empty, it's supposed to be a filter */
5780 if (param && cmd_ops->set_filter) {
5781 ret = cmd_ops->set_filter(param, trigger_data, file);
5787 if (!have_hist_trigger_match(trigger_data, file))
5790 if (hist_trigger_check_refs(trigger_data, file)) {
5795 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5796 se_name = trace_event_name(file->event_call);
5797 se = find_synth_event(se_name);
5804 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5806 * The above returns on success the # of triggers registered,
5807 * but if it didn't register any it returns zero. Consider no
5808 * triggers registered a failure too.
5811 if (!(attrs->pause || attrs->cont || attrs->clear))
5817 if (get_named_trigger_data(trigger_data))
5820 ret = create_actions(hist_data);
5824 if (has_hist_vars(hist_data) || hist_data->n_var_refs) {
5825 ret = save_hist_vars(hist_data);
5830 ret = tracing_map_init(hist_data->map);
5834 ret = hist_trigger_enable(trigger_data, file);
5838 se_name = trace_event_name(file->event_call);
5839 se = find_synth_event(se_name);
5842 /* Just return zero, not the number of registered triggers */
5845 if (ret == 0 && glob[0])
5850 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
5852 if (cmd_ops->set_filter)
5853 cmd_ops->set_filter(NULL, trigger_data, NULL);
5855 remove_hist_vars(hist_data);
5857 kfree(trigger_data);
5859 destroy_hist_data(hist_data);
5863 static struct event_command trigger_hist_cmd = {
5865 .trigger_type = ETT_EVENT_HIST,
5866 .flags = EVENT_CMD_FL_NEEDS_REC,
5867 .func = event_hist_trigger_func,
5868 .reg = hist_register_trigger,
5869 .unreg = hist_unregister_trigger,
5870 .unreg_all = hist_unreg_all,
5871 .get_trigger_ops = event_hist_get_trigger_ops,
5872 .set_filter = set_trigger_filter,
5875 __init int register_trigger_hist_cmd(void)
5879 ret = register_event_command(&trigger_hist_cmd);
5886 hist_enable_trigger(struct event_trigger_data *data, void *rec,
5887 struct ring_buffer_event *event)
5889 struct enable_trigger_data *enable_data = data->private_data;
5890 struct event_trigger_data *test;
5892 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
5893 lockdep_is_held(&event_mutex)) {
5894 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5895 if (enable_data->enable)
5896 test->paused = false;
5898 test->paused = true;
5904 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5905 struct ring_buffer_event *event)
5910 if (data->count != -1)
5913 hist_enable_trigger(data, rec, event);
5916 static struct event_trigger_ops hist_enable_trigger_ops = {
5917 .func = hist_enable_trigger,
5918 .print = event_enable_trigger_print,
5919 .init = event_trigger_init,
5920 .free = event_enable_trigger_free,
5923 static struct event_trigger_ops hist_enable_count_trigger_ops = {
5924 .func = hist_enable_count_trigger,
5925 .print = event_enable_trigger_print,
5926 .init = event_trigger_init,
5927 .free = event_enable_trigger_free,
5930 static struct event_trigger_ops hist_disable_trigger_ops = {
5931 .func = hist_enable_trigger,
5932 .print = event_enable_trigger_print,
5933 .init = event_trigger_init,
5934 .free = event_enable_trigger_free,
5937 static struct event_trigger_ops hist_disable_count_trigger_ops = {
5938 .func = hist_enable_count_trigger,
5939 .print = event_enable_trigger_print,
5940 .init = event_trigger_init,
5941 .free = event_enable_trigger_free,
5944 static struct event_trigger_ops *
5945 hist_enable_get_trigger_ops(char *cmd, char *param)
5947 struct event_trigger_ops *ops;
5950 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5953 ops = param ? &hist_enable_count_trigger_ops :
5954 &hist_enable_trigger_ops;
5956 ops = param ? &hist_disable_count_trigger_ops :
5957 &hist_disable_trigger_ops;
5962 static void hist_enable_unreg_all(struct trace_event_file *file)
5964 struct event_trigger_data *test, *n;
5966 list_for_each_entry_safe(test, n, &file->triggers, list) {
5967 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5968 list_del_rcu(&test->list);
5969 update_cond_flag(file);
5970 trace_event_trigger_enable_disable(file, 0);
5971 if (test->ops->free)
5972 test->ops->free(test->ops, test);
5977 static struct event_command trigger_hist_enable_cmd = {
5978 .name = ENABLE_HIST_STR,
5979 .trigger_type = ETT_HIST_ENABLE,
5980 .func = event_enable_trigger_func,
5981 .reg = event_enable_register_trigger,
5982 .unreg = event_enable_unregister_trigger,
5983 .unreg_all = hist_enable_unreg_all,
5984 .get_trigger_ops = hist_enable_get_trigger_ops,
5985 .set_filter = set_trigger_filter,
5988 static struct event_command trigger_hist_disable_cmd = {
5989 .name = DISABLE_HIST_STR,
5990 .trigger_type = ETT_HIST_ENABLE,
5991 .func = event_enable_trigger_func,
5992 .reg = event_enable_register_trigger,
5993 .unreg = event_enable_unregister_trigger,
5994 .unreg_all = hist_enable_unreg_all,
5995 .get_trigger_ops = hist_enable_get_trigger_ops,
5996 .set_filter = set_trigger_filter,
5999 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6001 unregister_event_command(&trigger_hist_enable_cmd);
6002 unregister_event_command(&trigger_hist_disable_cmd);
6005 __init int register_trigger_hist_enable_disable_cmds(void)
6009 ret = register_event_command(&trigger_hist_enable_cmd);
6010 if (WARN_ON(ret < 0))
6012 ret = register_event_command(&trigger_hist_disable_cmd);
6013 if (WARN_ON(ret < 0))
6014 unregister_trigger_hist_enable_disable_cmds();