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"), \
70 C(EXPECT_NUMBER, "Expecting numeric literal"), \
71 C(UNARY_MINUS_SUBEXPR, "Unary minus not supported in sub-expressions"), \
72 C(DIVISION_BY_ZERO, "Division by zero"),
75 #define C(a, b) HIST_ERR_##a
82 static const char *err_text[] = { ERRORS };
86 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
87 struct tracing_map_elt *elt,
88 struct trace_buffer *buffer,
89 struct ring_buffer_event *rbe,
92 #define HIST_FIELD_OPERANDS_MAX 2
93 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
94 #define HIST_ACTIONS_MAX 8
95 #define HIST_CONST_DIGITS_MAX 21
96 #define HIST_DIV_SHIFT 20 /* For optimizing division by constants */
102 FIELD_OP_UNARY_MINUS,
109 HIST_FIELD_FN_VAR_REF,
110 HIST_FIELD_FN_COUNTER,
113 HIST_FIELD_FN_BUCKET,
114 HIST_FIELD_FN_TIMESTAMP,
116 HIST_FIELD_FN_STRING,
117 HIST_FIELD_FN_DYNSTRING,
118 HIST_FIELD_FN_RELDYNSTRING,
119 HIST_FIELD_FN_PSTRING,
128 HIST_FIELD_FN_UMINUS,
133 HIST_FIELD_FN_DIV_POWER2,
134 HIST_FIELD_FN_DIV_NOT_POWER2,
135 HIST_FIELD_FN_DIV_MULT_SHIFT,
136 HIST_FIELD_FN_EXECNAME,
140 * A hist_var (histogram variable) contains variable information for
141 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
142 * flag set. A hist_var has a variable name e.g. ts0, and is
143 * associated with a given histogram trigger, as specified by
144 * hist_data. The hist_var idx is the unique index assigned to the
145 * variable by the hist trigger's tracing_map. The idx is what is
146 * used to set a variable's value and, by a variable reference, to
151 struct hist_trigger_data *hist_data;
156 struct ftrace_event_field *field;
158 unsigned long buckets;
160 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
161 struct hist_trigger_data *hist_data;
162 enum hist_field_fn fn_num;
166 unsigned int is_signed;
169 * Variable fields contain variable-specific info in var.
172 enum field_op_id operator;
177 * The name field is used for EXPR and VAR_REF fields. VAR
178 * fields contain the variable name in var.name.
183 * When a histogram trigger is hit, if it has any references
184 * to variables, the values of those variables are collected
185 * into a var_ref_vals array by resolve_var_refs(). The
186 * current value of each variable is read from the tracing_map
187 * using the hist field's hist_var.idx and entered into the
188 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
190 unsigned int var_ref_idx;
193 unsigned int var_str_idx;
195 /* Numeric literals are represented as u64 */
197 /* Used to optimize division by constants */
201 static u64 hist_fn_call(struct hist_field *hist_field,
202 struct tracing_map_elt *elt,
203 struct trace_buffer *buffer,
204 struct ring_buffer_event *rbe,
207 static u64 hist_field_const(struct hist_field *field,
208 struct tracing_map_elt *elt,
209 struct trace_buffer *buffer,
210 struct ring_buffer_event *rbe,
213 return field->constant;
216 static u64 hist_field_counter(struct hist_field *field,
217 struct tracing_map_elt *elt,
218 struct trace_buffer *buffer,
219 struct ring_buffer_event *rbe,
225 static u64 hist_field_string(struct hist_field *hist_field,
226 struct tracing_map_elt *elt,
227 struct trace_buffer *buffer,
228 struct ring_buffer_event *rbe,
231 char *addr = (char *)(event + hist_field->field->offset);
233 return (u64)(unsigned long)addr;
236 static u64 hist_field_dynstring(struct hist_field *hist_field,
237 struct tracing_map_elt *elt,
238 struct trace_buffer *buffer,
239 struct ring_buffer_event *rbe,
242 u32 str_item = *(u32 *)(event + hist_field->field->offset);
243 int str_loc = str_item & 0xffff;
244 char *addr = (char *)(event + str_loc);
246 return (u64)(unsigned long)addr;
249 static u64 hist_field_reldynstring(struct hist_field *hist_field,
250 struct tracing_map_elt *elt,
251 struct trace_buffer *buffer,
252 struct ring_buffer_event *rbe,
255 u32 *item = event + hist_field->field->offset;
256 u32 str_item = *item;
257 int str_loc = str_item & 0xffff;
258 char *addr = (char *)&item[1] + str_loc;
260 return (u64)(unsigned long)addr;
263 static u64 hist_field_pstring(struct hist_field *hist_field,
264 struct tracing_map_elt *elt,
265 struct trace_buffer *buffer,
266 struct ring_buffer_event *rbe,
269 char **addr = (char **)(event + hist_field->field->offset);
271 return (u64)(unsigned long)*addr;
274 static u64 hist_field_log2(struct hist_field *hist_field,
275 struct tracing_map_elt *elt,
276 struct trace_buffer *buffer,
277 struct ring_buffer_event *rbe,
280 struct hist_field *operand = hist_field->operands[0];
282 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
284 return (u64) ilog2(roundup_pow_of_two(val));
287 static u64 hist_field_bucket(struct hist_field *hist_field,
288 struct tracing_map_elt *elt,
289 struct trace_buffer *buffer,
290 struct ring_buffer_event *rbe,
293 struct hist_field *operand = hist_field->operands[0];
294 unsigned long buckets = hist_field->buckets;
296 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
298 if (WARN_ON_ONCE(!buckets))
302 val = div64_ul(val, buckets);
304 val = (u64)((unsigned long)val / buckets);
305 return val * buckets;
308 static u64 hist_field_plus(struct hist_field *hist_field,
309 struct tracing_map_elt *elt,
310 struct trace_buffer *buffer,
311 struct ring_buffer_event *rbe,
314 struct hist_field *operand1 = hist_field->operands[0];
315 struct hist_field *operand2 = hist_field->operands[1];
317 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
318 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
323 static u64 hist_field_minus(struct hist_field *hist_field,
324 struct tracing_map_elt *elt,
325 struct trace_buffer *buffer,
326 struct ring_buffer_event *rbe,
329 struct hist_field *operand1 = hist_field->operands[0];
330 struct hist_field *operand2 = hist_field->operands[1];
332 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
333 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
338 static u64 hist_field_div(struct hist_field *hist_field,
339 struct tracing_map_elt *elt,
340 struct trace_buffer *buffer,
341 struct ring_buffer_event *rbe,
344 struct hist_field *operand1 = hist_field->operands[0];
345 struct hist_field *operand2 = hist_field->operands[1];
347 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
348 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
350 /* Return -1 for the undefined case */
354 /* Use shift if the divisor is a power of 2 */
355 if (!(val2 & (val2 - 1)))
356 return val1 >> __ffs64(val2);
358 return div64_u64(val1, val2);
361 static u64 div_by_power_of_two(struct hist_field *hist_field,
362 struct tracing_map_elt *elt,
363 struct trace_buffer *buffer,
364 struct ring_buffer_event *rbe,
367 struct hist_field *operand1 = hist_field->operands[0];
368 struct hist_field *operand2 = hist_field->operands[1];
370 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
372 return val1 >> __ffs64(operand2->constant);
375 static u64 div_by_not_power_of_two(struct hist_field *hist_field,
376 struct tracing_map_elt *elt,
377 struct trace_buffer *buffer,
378 struct ring_buffer_event *rbe,
381 struct hist_field *operand1 = hist_field->operands[0];
382 struct hist_field *operand2 = hist_field->operands[1];
384 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
386 return div64_u64(val1, operand2->constant);
389 static u64 div_by_mult_and_shift(struct hist_field *hist_field,
390 struct tracing_map_elt *elt,
391 struct trace_buffer *buffer,
392 struct ring_buffer_event *rbe,
395 struct hist_field *operand1 = hist_field->operands[0];
396 struct hist_field *operand2 = hist_field->operands[1];
398 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
401 * If the divisor is a constant, do a multiplication and shift instead.
403 * Choose Z = some power of 2. If Y <= Z, then:
404 * X / Y = (X * (Z / Y)) / Z
406 * (Z / Y) is a constant (mult) which is calculated at parse time, so:
407 * X / Y = (X * mult) / Z
409 * The division by Z can be replaced by a shift since Z is a power of 2:
410 * X / Y = (X * mult) >> HIST_DIV_SHIFT
412 * As long, as X < Z the results will not be off by more than 1.
414 if (val1 < (1 << HIST_DIV_SHIFT)) {
415 u64 mult = operand2->div_multiplier;
417 return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
420 return div64_u64(val1, operand2->constant);
423 static u64 hist_field_mult(struct hist_field *hist_field,
424 struct tracing_map_elt *elt,
425 struct trace_buffer *buffer,
426 struct ring_buffer_event *rbe,
429 struct hist_field *operand1 = hist_field->operands[0];
430 struct hist_field *operand2 = hist_field->operands[1];
432 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
433 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
438 static u64 hist_field_unary_minus(struct hist_field *hist_field,
439 struct tracing_map_elt *elt,
440 struct trace_buffer *buffer,
441 struct ring_buffer_event *rbe,
444 struct hist_field *operand = hist_field->operands[0];
446 s64 sval = (s64)hist_fn_call(operand, elt, buffer, rbe, event);
447 u64 val = (u64)-sval;
452 #define DEFINE_HIST_FIELD_FN(type) \
453 static u64 hist_field_##type(struct hist_field *hist_field, \
454 struct tracing_map_elt *elt, \
455 struct trace_buffer *buffer, \
456 struct ring_buffer_event *rbe, \
459 type *addr = (type *)(event + hist_field->field->offset); \
461 return (u64)(unsigned long)*addr; \
464 DEFINE_HIST_FIELD_FN(s64);
465 DEFINE_HIST_FIELD_FN(u64);
466 DEFINE_HIST_FIELD_FN(s32);
467 DEFINE_HIST_FIELD_FN(u32);
468 DEFINE_HIST_FIELD_FN(s16);
469 DEFINE_HIST_FIELD_FN(u16);
470 DEFINE_HIST_FIELD_FN(s8);
471 DEFINE_HIST_FIELD_FN(u8);
473 #define for_each_hist_field(i, hist_data) \
474 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
476 #define for_each_hist_val_field(i, hist_data) \
477 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
479 #define for_each_hist_key_field(i, hist_data) \
480 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
482 #define HIST_STACKTRACE_DEPTH 16
483 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
484 #define HIST_STACKTRACE_SKIP 5
486 #define HITCOUNT_IDX 0
487 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
489 enum hist_field_flags {
490 HIST_FIELD_FL_HITCOUNT = 1 << 0,
491 HIST_FIELD_FL_KEY = 1 << 1,
492 HIST_FIELD_FL_STRING = 1 << 2,
493 HIST_FIELD_FL_HEX = 1 << 3,
494 HIST_FIELD_FL_SYM = 1 << 4,
495 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
496 HIST_FIELD_FL_EXECNAME = 1 << 6,
497 HIST_FIELD_FL_SYSCALL = 1 << 7,
498 HIST_FIELD_FL_STACKTRACE = 1 << 8,
499 HIST_FIELD_FL_LOG2 = 1 << 9,
500 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
501 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
502 HIST_FIELD_FL_VAR = 1 << 12,
503 HIST_FIELD_FL_EXPR = 1 << 13,
504 HIST_FIELD_FL_VAR_REF = 1 << 14,
505 HIST_FIELD_FL_CPU = 1 << 15,
506 HIST_FIELD_FL_ALIAS = 1 << 16,
507 HIST_FIELD_FL_BUCKET = 1 << 17,
508 HIST_FIELD_FL_CONST = 1 << 18,
513 char *name[TRACING_MAP_VARS_MAX];
514 char *expr[TRACING_MAP_VARS_MAX];
517 struct hist_trigger_attrs {
527 unsigned int map_bits;
529 char *assignment_str[TRACING_MAP_VARS_MAX];
530 unsigned int n_assignments;
532 char *action_str[HIST_ACTIONS_MAX];
533 unsigned int n_actions;
535 struct var_defs var_defs;
539 struct hist_field *var;
540 struct hist_field *val;
543 struct field_var_hist {
544 struct hist_trigger_data *hist_data;
548 struct hist_trigger_data {
549 struct hist_field *fields[HIST_FIELDS_MAX];
552 unsigned int n_fields;
554 unsigned int n_var_str;
555 unsigned int key_size;
556 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
557 unsigned int n_sort_keys;
558 struct trace_event_file *event_file;
559 struct hist_trigger_attrs *attrs;
560 struct tracing_map *map;
561 bool enable_timestamps;
563 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
564 unsigned int n_var_refs;
566 struct action_data *actions[HIST_ACTIONS_MAX];
567 unsigned int n_actions;
569 struct field_var *field_vars[SYNTH_FIELDS_MAX];
570 unsigned int n_field_vars;
571 unsigned int n_field_var_str;
572 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
573 unsigned int n_field_var_hists;
575 struct field_var *save_vars[SYNTH_FIELDS_MAX];
576 unsigned int n_save_vars;
577 unsigned int n_save_var_str;
582 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
583 struct tracing_map_elt *elt,
584 struct trace_buffer *buffer, void *rec,
585 struct ring_buffer_event *rbe, void *key,
586 struct action_data *data, u64 *var_ref_vals);
588 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
603 enum handler_id handler;
604 enum action_id action;
608 unsigned int n_params;
609 char *params[SYNTH_FIELDS_MAX];
612 * When a histogram trigger is hit, the values of any
613 * references to variables, including variables being passed
614 * as parameters to synthetic events, are collected into a
615 * var_ref_vals array. This var_ref_idx array is an array of
616 * indices into the var_ref_vals array, one for each synthetic
617 * event param, and is passed to the synthetic event
620 unsigned int var_ref_idx[SYNTH_FIELDS_MAX];
621 struct synth_event *synth_event;
622 bool use_trace_keyword;
623 char *synth_event_name;
633 * var_str contains the $-unstripped variable
634 * name referenced by var_ref, and used when
635 * printing the action. Because var_ref
636 * creation is deferred to create_actions(),
637 * we need a per-action way to save it until
638 * then, thus var_str.
643 * var_ref refers to the variable being
644 * tracked e.g onmax($var).
646 struct hist_field *var_ref;
649 * track_var contains the 'invisible' tracking
650 * variable created to keep the current
653 struct hist_field *track_var;
655 check_track_val_fn_t check_val;
656 action_fn_t save_data;
665 unsigned int key_len;
667 struct tracing_map_elt elt;
669 struct action_data *action_data;
670 struct hist_trigger_data *hist_data;
673 struct hist_elt_data {
676 char **field_var_str;
680 struct snapshot_context {
681 struct tracing_map_elt *elt;
686 * Returns the specific division function to use if the divisor
687 * is constant. This avoids extra branches when the trigger is hit.
689 static enum hist_field_fn hist_field_get_div_fn(struct hist_field *divisor)
691 u64 div = divisor->constant;
693 if (!(div & (div - 1)))
694 return HIST_FIELD_FN_DIV_POWER2;
696 /* If the divisor is too large, do a regular division */
697 if (div > (1 << HIST_DIV_SHIFT))
698 return HIST_FIELD_FN_DIV_NOT_POWER2;
700 divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
701 return HIST_FIELD_FN_DIV_MULT_SHIFT;
704 static void track_data_free(struct track_data *track_data)
706 struct hist_elt_data *elt_data;
711 kfree(track_data->key);
713 elt_data = track_data->elt.private_data;
715 kfree(elt_data->comm);
722 static struct track_data *track_data_alloc(unsigned int key_len,
723 struct action_data *action_data,
724 struct hist_trigger_data *hist_data)
726 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
727 struct hist_elt_data *elt_data;
730 return ERR_PTR(-ENOMEM);
732 data->key = kzalloc(key_len, GFP_KERNEL);
734 track_data_free(data);
735 return ERR_PTR(-ENOMEM);
738 data->key_len = key_len;
739 data->action_data = action_data;
740 data->hist_data = hist_data;
742 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
744 track_data_free(data);
745 return ERR_PTR(-ENOMEM);
748 data->elt.private_data = elt_data;
750 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
751 if (!elt_data->comm) {
752 track_data_free(data);
753 return ERR_PTR(-ENOMEM);
759 #define HIST_PREFIX "hist:"
761 static char *last_cmd;
762 static char last_cmd_loc[MAX_FILTER_STR_VAL];
764 static int errpos(char *str)
766 if (!str || !last_cmd)
769 return err_pos(last_cmd, str);
772 static void last_cmd_set(struct trace_event_file *file, char *str)
774 const char *system = NULL, *name = NULL;
775 struct trace_event_call *call;
781 /* sizeof() contains the nul byte */
782 len = sizeof(HIST_PREFIX) + strlen(str);
784 last_cmd = kzalloc(len, GFP_KERNEL);
788 strcpy(last_cmd, HIST_PREFIX);
789 /* Again, sizeof() contains the nul byte */
790 len -= sizeof(HIST_PREFIX);
791 strncat(last_cmd, str, len);
794 call = file->event_call;
795 system = call->class->system;
797 name = trace_event_name(call);
804 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, HIST_PREFIX "%s:%s", system, name);
807 static void hist_err(struct trace_array *tr, u8 err_type, u16 err_pos)
812 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
816 static void hist_err_clear(void)
820 last_cmd_loc[0] = '\0';
823 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
824 unsigned int *var_ref_idx);
826 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
827 unsigned int *var_ref_idx)
829 struct tracepoint *tp = event->tp;
831 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
832 struct tracepoint_func *probe_func_ptr;
833 synth_probe_func_t probe_func;
836 if (!(cpu_online(raw_smp_processor_id())))
839 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
840 if (probe_func_ptr) {
842 probe_func = probe_func_ptr->func;
843 __data = probe_func_ptr->data;
844 probe_func(__data, var_ref_vals, var_ref_idx);
845 } while ((++probe_func_ptr)->func);
850 static void action_trace(struct hist_trigger_data *hist_data,
851 struct tracing_map_elt *elt,
852 struct trace_buffer *buffer, void *rec,
853 struct ring_buffer_event *rbe, void *key,
854 struct action_data *data, u64 *var_ref_vals)
856 struct synth_event *event = data->synth_event;
858 trace_synth(event, var_ref_vals, data->var_ref_idx);
861 struct hist_var_data {
862 struct list_head list;
863 struct hist_trigger_data *hist_data;
866 static u64 hist_field_timestamp(struct hist_field *hist_field,
867 struct tracing_map_elt *elt,
868 struct trace_buffer *buffer,
869 struct ring_buffer_event *rbe,
872 struct hist_trigger_data *hist_data = hist_field->hist_data;
873 struct trace_array *tr = hist_data->event_file->tr;
875 u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
877 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
883 static u64 hist_field_cpu(struct hist_field *hist_field,
884 struct tracing_map_elt *elt,
885 struct trace_buffer *buffer,
886 struct ring_buffer_event *rbe,
889 int cpu = smp_processor_id();
895 * check_field_for_var_ref - Check if a VAR_REF field references a variable
896 * @hist_field: The VAR_REF field to check
897 * @var_data: The hist trigger that owns the variable
898 * @var_idx: The trigger variable identifier
900 * Check the given VAR_REF field to see whether or not it references
901 * the given variable associated with the given trigger.
903 * Return: The VAR_REF field if it does reference the variable, NULL if not
905 static struct hist_field *
906 check_field_for_var_ref(struct hist_field *hist_field,
907 struct hist_trigger_data *var_data,
908 unsigned int var_idx)
910 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
912 if (hist_field && hist_field->var.idx == var_idx &&
913 hist_field->var.hist_data == var_data)
920 * find_var_ref - Check if a trigger has a reference to a trigger variable
921 * @hist_data: The hist trigger that might have a reference to the variable
922 * @var_data: The hist trigger that owns the variable
923 * @var_idx: The trigger variable identifier
925 * Check the list of var_refs[] on the first hist trigger to see
926 * whether any of them are references to the variable on the second
929 * Return: The VAR_REF field referencing the variable if so, NULL if not
931 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
932 struct hist_trigger_data *var_data,
933 unsigned int var_idx)
935 struct hist_field *hist_field;
938 for (i = 0; i < hist_data->n_var_refs; i++) {
939 hist_field = hist_data->var_refs[i];
940 if (check_field_for_var_ref(hist_field, var_data, var_idx))
948 * find_any_var_ref - Check if there is a reference to a given trigger variable
949 * @hist_data: The hist trigger
950 * @var_idx: The trigger variable identifier
952 * Check to see whether the given variable is currently referenced by
955 * The trigger the variable is defined on is explicitly excluded - the
956 * assumption being that a self-reference doesn't prevent a trigger
957 * from being removed.
959 * Return: The VAR_REF field referencing the variable if so, NULL if not
961 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
962 unsigned int var_idx)
964 struct trace_array *tr = hist_data->event_file->tr;
965 struct hist_field *found = NULL;
966 struct hist_var_data *var_data;
968 list_for_each_entry(var_data, &tr->hist_vars, list) {
969 if (var_data->hist_data == hist_data)
971 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
980 * check_var_refs - Check if there is a reference to any of trigger's variables
981 * @hist_data: The hist trigger
983 * A trigger can define one or more variables. If any one of them is
984 * currently referenced by any other trigger, this function will
987 * Typically used to determine whether or not a trigger can be removed
988 * - if there are any references to a trigger's variables, it cannot.
990 * Return: True if there is a reference to any of trigger's variables
992 static bool check_var_refs(struct hist_trigger_data *hist_data)
994 struct hist_field *field;
998 for_each_hist_field(i, hist_data) {
999 field = hist_data->fields[i];
1000 if (field && field->flags & HIST_FIELD_FL_VAR) {
1001 if (find_any_var_ref(hist_data, field->var.idx)) {
1011 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1013 struct trace_array *tr = hist_data->event_file->tr;
1014 struct hist_var_data *var_data, *found = NULL;
1016 list_for_each_entry(var_data, &tr->hist_vars, list) {
1017 if (var_data->hist_data == hist_data) {
1026 static bool field_has_hist_vars(struct hist_field *hist_field,
1037 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1038 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1041 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1042 struct hist_field *operand;
1044 operand = hist_field->operands[i];
1045 if (field_has_hist_vars(operand, level + 1))
1052 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1054 struct hist_field *hist_field;
1057 for_each_hist_field(i, hist_data) {
1058 hist_field = hist_data->fields[i];
1059 if (field_has_hist_vars(hist_field, 0))
1066 static int save_hist_vars(struct hist_trigger_data *hist_data)
1068 struct trace_array *tr = hist_data->event_file->tr;
1069 struct hist_var_data *var_data;
1071 var_data = find_hist_vars(hist_data);
1075 if (tracing_check_open_get_tr(tr))
1078 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1080 trace_array_put(tr);
1084 var_data->hist_data = hist_data;
1085 list_add(&var_data->list, &tr->hist_vars);
1090 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1092 struct trace_array *tr = hist_data->event_file->tr;
1093 struct hist_var_data *var_data;
1095 var_data = find_hist_vars(hist_data);
1099 if (WARN_ON(check_var_refs(hist_data)))
1102 list_del(&var_data->list);
1106 trace_array_put(tr);
1109 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1110 const char *var_name)
1112 struct hist_field *hist_field, *found = NULL;
1115 for_each_hist_field(i, hist_data) {
1116 hist_field = hist_data->fields[i];
1117 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1118 strcmp(hist_field->var.name, var_name) == 0) {
1127 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1128 struct trace_event_file *file,
1129 const char *var_name)
1131 struct hist_trigger_data *test_data;
1132 struct event_trigger_data *test;
1133 struct hist_field *hist_field;
1135 lockdep_assert_held(&event_mutex);
1137 hist_field = find_var_field(hist_data, var_name);
1141 list_for_each_entry(test, &file->triggers, list) {
1142 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1143 test_data = test->private_data;
1144 hist_field = find_var_field(test_data, var_name);
1153 static struct trace_event_file *find_var_file(struct trace_array *tr,
1158 struct hist_trigger_data *var_hist_data;
1159 struct hist_var_data *var_data;
1160 struct trace_event_file *file, *found = NULL;
1163 return find_event_file(tr, system, event_name);
1165 list_for_each_entry(var_data, &tr->hist_vars, list) {
1166 var_hist_data = var_data->hist_data;
1167 file = var_hist_data->event_file;
1171 if (find_var_field(var_hist_data, var_name)) {
1173 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1184 static struct hist_field *find_file_var(struct trace_event_file *file,
1185 const char *var_name)
1187 struct hist_trigger_data *test_data;
1188 struct event_trigger_data *test;
1189 struct hist_field *hist_field;
1191 lockdep_assert_held(&event_mutex);
1193 list_for_each_entry(test, &file->triggers, list) {
1194 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1195 test_data = test->private_data;
1196 hist_field = find_var_field(test_data, var_name);
1205 static struct hist_field *
1206 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1208 struct trace_array *tr = hist_data->event_file->tr;
1209 struct hist_field *hist_field, *found = NULL;
1210 struct trace_event_file *file;
1213 for (i = 0; i < hist_data->n_actions; i++) {
1214 struct action_data *data = hist_data->actions[i];
1216 if (data->handler == HANDLER_ONMATCH) {
1217 char *system = data->match_data.event_system;
1218 char *event_name = data->match_data.event;
1220 file = find_var_file(tr, system, event_name, var_name);
1223 hist_field = find_file_var(file, var_name);
1226 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1228 return ERR_PTR(-EINVAL);
1238 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1243 struct trace_array *tr = hist_data->event_file->tr;
1244 struct hist_field *hist_field = NULL;
1245 struct trace_event_file *file;
1247 if (!system || !event_name) {
1248 hist_field = find_match_var(hist_data, var_name);
1249 if (IS_ERR(hist_field))
1255 file = find_var_file(tr, system, event_name, var_name);
1259 hist_field = find_file_var(file, var_name);
1264 static u64 hist_field_var_ref(struct hist_field *hist_field,
1265 struct tracing_map_elt *elt,
1266 struct trace_buffer *buffer,
1267 struct ring_buffer_event *rbe,
1270 struct hist_elt_data *elt_data;
1273 if (WARN_ON_ONCE(!elt))
1276 elt_data = elt->private_data;
1277 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1282 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1283 u64 *var_ref_vals, bool self)
1285 struct hist_trigger_data *var_data;
1286 struct tracing_map_elt *var_elt;
1287 struct hist_field *hist_field;
1288 unsigned int i, var_idx;
1289 bool resolved = true;
1292 for (i = 0; i < hist_data->n_var_refs; i++) {
1293 hist_field = hist_data->var_refs[i];
1294 var_idx = hist_field->var.idx;
1295 var_data = hist_field->var.hist_data;
1297 if (var_data == NULL) {
1302 if ((self && var_data != hist_data) ||
1303 (!self && var_data == hist_data))
1306 var_elt = tracing_map_lookup(var_data->map, key);
1312 if (!tracing_map_var_set(var_elt, var_idx)) {
1317 if (self || !hist_field->read_once)
1318 var_val = tracing_map_read_var(var_elt, var_idx);
1320 var_val = tracing_map_read_var_once(var_elt, var_idx);
1322 var_ref_vals[i] = var_val;
1328 static const char *hist_field_name(struct hist_field *field,
1331 const char *field_name = "";
1337 field_name = field->field->name;
1338 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1339 field->flags & HIST_FIELD_FL_ALIAS ||
1340 field->flags & HIST_FIELD_FL_BUCKET)
1341 field_name = hist_field_name(field->operands[0], ++level);
1342 else if (field->flags & HIST_FIELD_FL_CPU)
1343 field_name = "common_cpu";
1344 else if (field->flags & HIST_FIELD_FL_EXPR ||
1345 field->flags & HIST_FIELD_FL_VAR_REF) {
1346 if (field->system) {
1347 static char full_name[MAX_FILTER_STR_VAL];
1349 strcat(full_name, field->system);
1350 strcat(full_name, ".");
1351 strcat(full_name, field->event_name);
1352 strcat(full_name, ".");
1353 strcat(full_name, field->name);
1354 field_name = full_name;
1356 field_name = field->name;
1357 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1358 field_name = "common_timestamp";
1360 if (field_name == NULL)
1366 static enum hist_field_fn select_value_fn(int field_size, int field_is_signed)
1368 switch (field_size) {
1370 if (field_is_signed)
1371 return HIST_FIELD_FN_S64;
1373 return HIST_FIELD_FN_U64;
1375 if (field_is_signed)
1376 return HIST_FIELD_FN_S32;
1378 return HIST_FIELD_FN_U32;
1380 if (field_is_signed)
1381 return HIST_FIELD_FN_S16;
1383 return HIST_FIELD_FN_U16;
1385 if (field_is_signed)
1386 return HIST_FIELD_FN_S8;
1388 return HIST_FIELD_FN_U8;
1391 return HIST_FIELD_FN_NOP;
1394 static int parse_map_size(char *str)
1396 unsigned long size, map_bits;
1399 ret = kstrtoul(str, 0, &size);
1403 map_bits = ilog2(roundup_pow_of_two(size));
1404 if (map_bits < TRACING_MAP_BITS_MIN ||
1405 map_bits > TRACING_MAP_BITS_MAX)
1413 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1420 for (i = 0; i < attrs->n_assignments; i++)
1421 kfree(attrs->assignment_str[i]);
1423 for (i = 0; i < attrs->n_actions; i++)
1424 kfree(attrs->action_str[i]);
1427 kfree(attrs->sort_key_str);
1428 kfree(attrs->keys_str);
1429 kfree(attrs->vals_str);
1430 kfree(attrs->clock);
1434 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1438 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1441 if ((str_has_prefix(str, "onmatch(")) ||
1442 (str_has_prefix(str, "onmax(")) ||
1443 (str_has_prefix(str, "onchange("))) {
1444 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1445 if (!attrs->action_str[attrs->n_actions]) {
1455 static int parse_assignment(struct trace_array *tr,
1456 char *str, struct hist_trigger_attrs *attrs)
1460 if ((len = str_has_prefix(str, "key=")) ||
1461 (len = str_has_prefix(str, "keys="))) {
1462 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1463 if (!attrs->keys_str) {
1467 } else if ((len = str_has_prefix(str, "val=")) ||
1468 (len = str_has_prefix(str, "vals=")) ||
1469 (len = str_has_prefix(str, "values="))) {
1470 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1471 if (!attrs->vals_str) {
1475 } else if ((len = str_has_prefix(str, "sort="))) {
1476 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1477 if (!attrs->sort_key_str) {
1481 } else if (str_has_prefix(str, "name=")) {
1482 attrs->name = kstrdup(str, GFP_KERNEL);
1487 } else if ((len = str_has_prefix(str, "clock="))) {
1490 str = strstrip(str);
1491 attrs->clock = kstrdup(str, GFP_KERNEL);
1492 if (!attrs->clock) {
1496 } else if ((len = str_has_prefix(str, "size="))) {
1497 int map_bits = parse_map_size(str + len);
1503 attrs->map_bits = map_bits;
1507 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1508 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1513 assignment = kstrdup(str, GFP_KERNEL);
1519 attrs->assignment_str[attrs->n_assignments++] = assignment;
1525 static struct hist_trigger_attrs *
1526 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1528 struct hist_trigger_attrs *attrs;
1531 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1533 return ERR_PTR(-ENOMEM);
1535 while (trigger_str) {
1536 char *str = strsep(&trigger_str, ":");
1539 rhs = strchr(str, '=');
1541 if (!strlen(++rhs)) {
1543 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1546 ret = parse_assignment(tr, str, attrs);
1549 } else if (strcmp(str, "pause") == 0)
1550 attrs->pause = true;
1551 else if ((strcmp(str, "cont") == 0) ||
1552 (strcmp(str, "continue") == 0))
1554 else if (strcmp(str, "clear") == 0)
1555 attrs->clear = true;
1557 ret = parse_action(str, attrs);
1563 if (!attrs->keys_str) {
1568 if (!attrs->clock) {
1569 attrs->clock = kstrdup("global", GFP_KERNEL);
1570 if (!attrs->clock) {
1578 destroy_hist_trigger_attrs(attrs);
1580 return ERR_PTR(ret);
1583 static inline void save_comm(char *comm, struct task_struct *task)
1586 strcpy(comm, "<idle>");
1590 if (WARN_ON_ONCE(task->pid < 0)) {
1591 strcpy(comm, "<XXX>");
1595 strncpy(comm, task->comm, TASK_COMM_LEN);
1598 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1602 for (i = 0; i < elt_data->n_field_var_str; i++)
1603 kfree(elt_data->field_var_str[i]);
1605 kfree(elt_data->field_var_str);
1607 kfree(elt_data->comm);
1611 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1613 struct hist_elt_data *elt_data = elt->private_data;
1615 hist_elt_data_free(elt_data);
1618 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1620 struct hist_trigger_data *hist_data = elt->map->private_data;
1621 unsigned int size = TASK_COMM_LEN;
1622 struct hist_elt_data *elt_data;
1623 struct hist_field *hist_field;
1624 unsigned int i, n_str;
1626 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1630 for_each_hist_field(i, hist_data) {
1631 hist_field = hist_data->fields[i];
1633 if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
1634 elt_data->comm = kzalloc(size, GFP_KERNEL);
1635 if (!elt_data->comm) {
1643 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1644 hist_data->n_var_str;
1645 if (n_str > SYNTH_FIELDS_MAX) {
1646 hist_elt_data_free(elt_data);
1650 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1652 size = STR_VAR_LEN_MAX;
1654 elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
1655 if (!elt_data->field_var_str) {
1656 hist_elt_data_free(elt_data);
1659 elt_data->n_field_var_str = n_str;
1661 for (i = 0; i < n_str; i++) {
1662 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1663 if (!elt_data->field_var_str[i]) {
1664 hist_elt_data_free(elt_data);
1669 elt->private_data = elt_data;
1674 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1676 struct hist_elt_data *elt_data = elt->private_data;
1679 save_comm(elt_data->comm, current);
1682 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1683 .elt_alloc = hist_trigger_elt_data_alloc,
1684 .elt_free = hist_trigger_elt_data_free,
1685 .elt_init = hist_trigger_elt_data_init,
1688 static const char *get_hist_field_flags(struct hist_field *hist_field)
1690 const char *flags_str = NULL;
1692 if (hist_field->flags & HIST_FIELD_FL_HEX)
1694 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1696 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1697 flags_str = "sym-offset";
1698 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1699 flags_str = "execname";
1700 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1701 flags_str = "syscall";
1702 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1704 else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
1705 flags_str = "buckets";
1706 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1707 flags_str = "usecs";
1712 static void expr_field_str(struct hist_field *field, char *expr)
1714 if (field->flags & HIST_FIELD_FL_VAR_REF)
1716 else if (field->flags & HIST_FIELD_FL_CONST) {
1717 char str[HIST_CONST_DIGITS_MAX];
1719 snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
1723 strcat(expr, hist_field_name(field, 0));
1725 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1726 const char *flags_str = get_hist_field_flags(field);
1730 strcat(expr, flags_str);
1735 static char *expr_str(struct hist_field *field, unsigned int level)
1742 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1746 if (!field->operands[0]) {
1747 expr_field_str(field, expr);
1751 if (field->operator == FIELD_OP_UNARY_MINUS) {
1755 subexpr = expr_str(field->operands[0], ++level);
1760 strcat(expr, subexpr);
1768 expr_field_str(field->operands[0], expr);
1770 switch (field->operator) {
1771 case FIELD_OP_MINUS:
1788 expr_field_str(field->operands[1], expr);
1794 * If field_op != FIELD_OP_NONE, *sep points to the root operator
1795 * of the expression tree to be evaluated.
1797 static int contains_operator(char *str, char **sep)
1799 enum field_op_id field_op = FIELD_OP_NONE;
1800 char *minus_op, *plus_op, *div_op, *mult_op;
1804 * Report the last occurrence of the operators first, so that the
1805 * expression is evaluated left to right. This is important since
1806 * subtraction and division are not associative.
1809 * 64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
1810 * 14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
1814 * First, find lower precedence addition and subtraction
1815 * since the expression will be evaluated recursively.
1817 minus_op = strrchr(str, '-');
1820 * Unary minus is not supported in sub-expressions. If
1821 * present, it is always the next root operator.
1823 if (minus_op == str) {
1824 field_op = FIELD_OP_UNARY_MINUS;
1828 field_op = FIELD_OP_MINUS;
1831 plus_op = strrchr(str, '+');
1832 if (plus_op || minus_op) {
1834 * For operators of the same precedence use to rightmost as the
1835 * root, so that the expression is evaluated left to right.
1837 if (plus_op > minus_op)
1838 field_op = FIELD_OP_PLUS;
1843 * Multiplication and division have higher precedence than addition and
1846 div_op = strrchr(str, '/');
1848 field_op = FIELD_OP_DIV;
1850 mult_op = strrchr(str, '*');
1852 * For operators of the same precedence use to rightmost as the
1853 * root, so that the expression is evaluated left to right.
1855 if (mult_op > div_op)
1856 field_op = FIELD_OP_MULT;
1861 case FIELD_OP_UNARY_MINUS:
1862 case FIELD_OP_MINUS:
1884 static void get_hist_field(struct hist_field *hist_field)
1889 static void __destroy_hist_field(struct hist_field *hist_field)
1891 if (--hist_field->ref > 1)
1894 kfree(hist_field->var.name);
1895 kfree(hist_field->name);
1897 /* Can likely be a const */
1898 kfree_const(hist_field->type);
1900 kfree(hist_field->system);
1901 kfree(hist_field->event_name);
1906 static void destroy_hist_field(struct hist_field *hist_field,
1917 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1918 return; /* var refs will be destroyed separately */
1920 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1921 destroy_hist_field(hist_field->operands[i], level + 1);
1923 __destroy_hist_field(hist_field);
1926 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1927 struct ftrace_event_field *field,
1928 unsigned long flags,
1931 struct hist_field *hist_field;
1933 if (field && is_function_field(field))
1936 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1940 hist_field->ref = 1;
1942 hist_field->hist_data = hist_data;
1944 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1945 goto out; /* caller will populate */
1947 if (flags & HIST_FIELD_FL_VAR_REF) {
1948 hist_field->fn_num = HIST_FIELD_FN_VAR_REF;
1952 if (flags & HIST_FIELD_FL_HITCOUNT) {
1953 hist_field->fn_num = HIST_FIELD_FN_COUNTER;
1954 hist_field->size = sizeof(u64);
1955 hist_field->type = "u64";
1959 if (flags & HIST_FIELD_FL_CONST) {
1960 hist_field->fn_num = HIST_FIELD_FN_CONST;
1961 hist_field->size = sizeof(u64);
1962 hist_field->type = kstrdup("u64", GFP_KERNEL);
1963 if (!hist_field->type)
1968 if (flags & HIST_FIELD_FL_STACKTRACE) {
1969 hist_field->fn_num = HIST_FIELD_FN_NOP;
1973 if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
1974 unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
1975 hist_field->fn_num = flags & HIST_FIELD_FL_LOG2 ? HIST_FIELD_FN_LOG2 :
1976 HIST_FIELD_FN_BUCKET;
1977 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1978 hist_field->size = hist_field->operands[0]->size;
1979 hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
1980 if (!hist_field->type)
1985 if (flags & HIST_FIELD_FL_TIMESTAMP) {
1986 hist_field->fn_num = HIST_FIELD_FN_TIMESTAMP;
1987 hist_field->size = sizeof(u64);
1988 hist_field->type = "u64";
1992 if (flags & HIST_FIELD_FL_CPU) {
1993 hist_field->fn_num = HIST_FIELD_FN_CPU;
1994 hist_field->size = sizeof(int);
1995 hist_field->type = "unsigned int";
1999 if (WARN_ON_ONCE(!field))
2002 /* Pointers to strings are just pointers and dangerous to dereference */
2003 if (is_string_field(field) &&
2004 (field->filter_type != FILTER_PTR_STRING)) {
2005 flags |= HIST_FIELD_FL_STRING;
2007 hist_field->size = MAX_FILTER_STR_VAL;
2008 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2009 if (!hist_field->type)
2012 if (field->filter_type == FILTER_STATIC_STRING) {
2013 hist_field->fn_num = HIST_FIELD_FN_STRING;
2014 hist_field->size = field->size;
2015 } else if (field->filter_type == FILTER_DYN_STRING) {
2016 hist_field->fn_num = HIST_FIELD_FN_DYNSTRING;
2017 } else if (field->filter_type == FILTER_RDYN_STRING)
2018 hist_field->fn_num = HIST_FIELD_FN_RELDYNSTRING;
2020 hist_field->fn_num = HIST_FIELD_FN_PSTRING;
2022 hist_field->size = field->size;
2023 hist_field->is_signed = field->is_signed;
2024 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2025 if (!hist_field->type)
2028 hist_field->fn_num = select_value_fn(field->size,
2030 if (hist_field->fn_num == HIST_FIELD_FN_NOP) {
2031 destroy_hist_field(hist_field, 0);
2036 hist_field->field = field;
2037 hist_field->flags = flags;
2040 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2041 if (!hist_field->var.name)
2047 destroy_hist_field(hist_field, 0);
2051 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2055 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2056 if (hist_data->fields[i]) {
2057 destroy_hist_field(hist_data->fields[i], 0);
2058 hist_data->fields[i] = NULL;
2062 for (i = 0; i < hist_data->n_var_refs; i++) {
2063 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2064 __destroy_hist_field(hist_data->var_refs[i]);
2065 hist_data->var_refs[i] = NULL;
2069 static int init_var_ref(struct hist_field *ref_field,
2070 struct hist_field *var_field,
2071 char *system, char *event_name)
2075 ref_field->var.idx = var_field->var.idx;
2076 ref_field->var.hist_data = var_field->hist_data;
2077 ref_field->size = var_field->size;
2078 ref_field->is_signed = var_field->is_signed;
2079 ref_field->flags |= var_field->flags &
2080 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2083 ref_field->system = kstrdup(system, GFP_KERNEL);
2084 if (!ref_field->system)
2089 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2090 if (!ref_field->event_name) {
2096 if (var_field->var.name) {
2097 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2098 if (!ref_field->name) {
2102 } else if (var_field->name) {
2103 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2104 if (!ref_field->name) {
2110 ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
2111 if (!ref_field->type) {
2118 kfree(ref_field->system);
2119 ref_field->system = NULL;
2120 kfree(ref_field->event_name);
2121 ref_field->event_name = NULL;
2122 kfree(ref_field->name);
2123 ref_field->name = NULL;
2128 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
2129 struct hist_field *var_field)
2131 struct hist_field *ref_field;
2134 for (i = 0; i < hist_data->n_var_refs; i++) {
2135 ref_field = hist_data->var_refs[i];
2136 if (ref_field->var.idx == var_field->var.idx &&
2137 ref_field->var.hist_data == var_field->hist_data)
2145 * create_var_ref - Create a variable reference and attach it to trigger
2146 * @hist_data: The trigger that will be referencing the variable
2147 * @var_field: The VAR field to create a reference to
2148 * @system: The optional system string
2149 * @event_name: The optional event_name string
2151 * Given a variable hist_field, create a VAR_REF hist_field that
2152 * represents a reference to it.
2154 * This function also adds the reference to the trigger that
2155 * now references the variable.
2157 * Return: The VAR_REF field if successful, NULL if not
2159 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2160 struct hist_field *var_field,
2161 char *system, char *event_name)
2163 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2164 struct hist_field *ref_field;
2167 /* Check if the variable already exists */
2168 for (i = 0; i < hist_data->n_var_refs; i++) {
2169 ref_field = hist_data->var_refs[i];
2170 if (ref_field->var.idx == var_field->var.idx &&
2171 ref_field->var.hist_data == var_field->hist_data) {
2172 get_hist_field(ref_field);
2176 /* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
2177 if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
2179 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2181 if (init_var_ref(ref_field, var_field, system, event_name)) {
2182 destroy_hist_field(ref_field, 0);
2186 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2187 ref_field->var_ref_idx = hist_data->n_var_refs++;
2193 static bool is_var_ref(char *var_name)
2195 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2201 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2207 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2208 name = hist_data->attrs->var_defs.name[i];
2210 if (strcmp(var_name, name) == 0) {
2211 field = hist_data->attrs->var_defs.expr[i];
2212 if (contains_operator(field, NULL) || is_var_ref(field))
2221 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2222 char *system, char *event_name,
2225 struct trace_event_call *call;
2227 if (system && event_name) {
2228 call = hist_data->event_file->event_call;
2230 if (strcmp(system, call->class->system) != 0)
2233 if (strcmp(event_name, trace_event_name(call)) != 0)
2237 if (!!system != !!event_name)
2240 if (!is_var_ref(var_name))
2245 return field_name_from_var(hist_data, var_name);
2248 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2249 char *system, char *event_name,
2252 struct hist_field *var_field = NULL, *ref_field = NULL;
2253 struct trace_array *tr = hist_data->event_file->tr;
2255 if (!is_var_ref(var_name))
2260 var_field = find_event_var(hist_data, system, event_name, var_name);
2262 ref_field = create_var_ref(hist_data, var_field,
2263 system, event_name);
2266 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2271 static struct ftrace_event_field *
2272 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2273 char *field_str, unsigned long *flags, unsigned long *buckets)
2275 struct ftrace_event_field *field = NULL;
2276 char *field_name, *modifier, *str;
2277 struct trace_array *tr = file->tr;
2279 modifier = str = kstrdup(field_str, GFP_KERNEL);
2281 return ERR_PTR(-ENOMEM);
2283 field_name = strsep(&modifier, ".");
2285 if (strcmp(modifier, "hex") == 0)
2286 *flags |= HIST_FIELD_FL_HEX;
2287 else if (strcmp(modifier, "sym") == 0)
2288 *flags |= HIST_FIELD_FL_SYM;
2290 * 'sym-offset' occurrences in the trigger string are modified
2291 * to 'symXoffset' to simplify arithmetic expression parsing.
2293 else if (strcmp(modifier, "symXoffset") == 0)
2294 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2295 else if ((strcmp(modifier, "execname") == 0) &&
2296 (strcmp(field_name, "common_pid") == 0))
2297 *flags |= HIST_FIELD_FL_EXECNAME;
2298 else if (strcmp(modifier, "syscall") == 0)
2299 *flags |= HIST_FIELD_FL_SYSCALL;
2300 else if (strcmp(modifier, "log2") == 0)
2301 *flags |= HIST_FIELD_FL_LOG2;
2302 else if (strcmp(modifier, "usecs") == 0)
2303 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2304 else if (strncmp(modifier, "bucket", 6) == 0) {
2309 if (*modifier == 's')
2311 if (*modifier != '=')
2314 ret = kstrtoul(modifier, 0, buckets);
2315 if (ret || !(*buckets))
2317 *flags |= HIST_FIELD_FL_BUCKET;
2320 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2321 field = ERR_PTR(-EINVAL);
2326 if (strcmp(field_name, "common_timestamp") == 0) {
2327 *flags |= HIST_FIELD_FL_TIMESTAMP;
2328 hist_data->enable_timestamps = true;
2329 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2330 hist_data->attrs->ts_in_usecs = true;
2331 } else if (strcmp(field_name, "common_cpu") == 0)
2332 *flags |= HIST_FIELD_FL_CPU;
2334 field = trace_find_event_field(file->event_call, field_name);
2335 if (!field || !field->size) {
2337 * For backward compatibility, if field_name
2338 * was "cpu", then we treat this the same as
2339 * common_cpu. This also works for "CPU".
2341 if (field && field->filter_type == FILTER_CPU) {
2342 *flags |= HIST_FIELD_FL_CPU;
2344 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2345 errpos(field_name));
2346 field = ERR_PTR(-EINVAL);
2357 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2358 struct hist_field *var_ref,
2361 struct hist_field *alias = NULL;
2362 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2364 alias = create_hist_field(hist_data, NULL, flags, var_name);
2368 alias->fn_num = var_ref->fn_num;
2369 alias->operands[0] = var_ref;
2371 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2372 destroy_hist_field(alias, 0);
2376 alias->var_ref_idx = var_ref->var_ref_idx;
2381 static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
2382 char *str, char *var_name,
2383 unsigned long *flags)
2385 struct trace_array *tr = hist_data->event_file->tr;
2386 struct hist_field *field = NULL;
2389 if (kstrtoull(str, 0, &constant)) {
2390 hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
2394 *flags |= HIST_FIELD_FL_CONST;
2395 field = create_hist_field(hist_data, NULL, *flags, var_name);
2399 field->constant = constant;
2404 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2405 struct trace_event_file *file, char *str,
2406 unsigned long *flags, char *var_name)
2408 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2409 struct ftrace_event_field *field = NULL;
2410 struct hist_field *hist_field = NULL;
2411 unsigned long buckets = 0;
2414 if (isdigit(str[0])) {
2415 hist_field = parse_const(hist_data, str, var_name, flags);
2423 s = strchr(str, '.');
2425 s = strchr(++s, '.');
2427 ref_system = strsep(&str, ".");
2432 ref_event = strsep(&str, ".");
2441 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2443 hist_field = parse_var_ref(hist_data, ref_system,
2444 ref_event, ref_var);
2447 hist_field = create_alias(hist_data, hist_field, var_name);
2458 field = parse_field(hist_data, file, str, flags, &buckets);
2459 if (IS_ERR(field)) {
2460 ret = PTR_ERR(field);
2464 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2469 hist_field->buckets = buckets;
2473 return ERR_PTR(ret);
2476 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2477 struct trace_event_file *file,
2478 char *str, unsigned long flags,
2479 char *var_name, unsigned int *n_subexprs);
2481 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2482 struct trace_event_file *file,
2483 char *str, unsigned long flags,
2484 char *var_name, unsigned int *n_subexprs)
2486 struct hist_field *operand1, *expr = NULL;
2487 unsigned long operand_flags;
2491 /* Unary minus operator, increment n_subexprs */
2494 /* we support only -(xxx) i.e. explicit parens required */
2496 if (*n_subexprs > 3) {
2497 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2502 str++; /* skip leading '-' */
2504 s = strchr(str, '(');
2512 s = strrchr(str, ')');
2514 /* unary minus not supported in sub-expressions */
2515 if (*(s+1) != '\0') {
2516 hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
2524 ret = -EINVAL; /* no closing ')' */
2528 flags |= HIST_FIELD_FL_EXPR;
2529 expr = create_hist_field(hist_data, NULL, flags, var_name);
2536 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2537 if (IS_ERR(operand1)) {
2538 ret = PTR_ERR(operand1);
2541 if (operand1->flags & HIST_FIELD_FL_STRING) {
2542 /* String type can not be the operand of unary operator. */
2543 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2544 destroy_hist_field(operand1, 0);
2549 expr->flags |= operand1->flags &
2550 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2551 expr->fn_num = HIST_FIELD_FN_UMINUS;
2552 expr->operands[0] = operand1;
2553 expr->size = operand1->size;
2554 expr->is_signed = operand1->is_signed;
2555 expr->operator = FIELD_OP_UNARY_MINUS;
2556 expr->name = expr_str(expr, 0);
2557 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2565 destroy_hist_field(expr, 0);
2566 return ERR_PTR(ret);
2570 * If the operands are var refs, return pointers the
2571 * variable(s) referenced in var1 and var2, else NULL.
2573 static int check_expr_operands(struct trace_array *tr,
2574 struct hist_field *operand1,
2575 struct hist_field *operand2,
2576 struct hist_field **var1,
2577 struct hist_field **var2)
2579 unsigned long operand1_flags = operand1->flags;
2580 unsigned long operand2_flags = operand2->flags;
2582 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2583 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2584 struct hist_field *var;
2586 var = find_var_field(operand1->var.hist_data, operand1->name);
2589 operand1_flags = var->flags;
2593 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2594 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2595 struct hist_field *var;
2597 var = find_var_field(operand2->var.hist_data, operand2->name);
2600 operand2_flags = var->flags;
2604 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2605 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2606 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2613 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2614 struct trace_event_file *file,
2615 char *str, unsigned long flags,
2616 char *var_name, unsigned int *n_subexprs)
2618 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2619 struct hist_field *var1 = NULL, *var2 = NULL;
2620 unsigned long operand_flags, operand2_flags;
2621 int field_op, ret = -EINVAL;
2622 char *sep, *operand1_str;
2623 enum hist_field_fn op_fn;
2624 bool combine_consts;
2626 if (*n_subexprs > 3) {
2627 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2628 return ERR_PTR(-EINVAL);
2631 field_op = contains_operator(str, &sep);
2633 if (field_op == FIELD_OP_NONE)
2634 return parse_atom(hist_data, file, str, &flags, var_name);
2636 if (field_op == FIELD_OP_UNARY_MINUS)
2637 return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
2639 /* Binary operator found, increment n_subexprs */
2642 /* Split the expression string at the root operator */
2644 return ERR_PTR(-EINVAL);
2650 /* Binary operator requires both operands */
2651 if (*operand1_str == '\0' || *str == '\0')
2652 return ERR_PTR(-EINVAL);
2656 /* LHS of string is an expression e.g. a+b in a+b+c */
2657 operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
2658 if (IS_ERR(operand1))
2659 return ERR_CAST(operand1);
2661 if (operand1->flags & HIST_FIELD_FL_STRING) {
2662 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2667 /* RHS of string is another expression e.g. c in a+b+c */
2669 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2670 if (IS_ERR(operand2)) {
2671 ret = PTR_ERR(operand2);
2674 if (operand2->flags & HIST_FIELD_FL_STRING) {
2675 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2681 case FIELD_OP_MINUS:
2682 op_fn = HIST_FIELD_FN_MINUS;
2685 op_fn = HIST_FIELD_FN_PLUS;
2688 op_fn = HIST_FIELD_FN_DIV;
2691 op_fn = HIST_FIELD_FN_MULT;
2698 ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
2702 operand_flags = var1 ? var1->flags : operand1->flags;
2703 operand2_flags = var2 ? var2->flags : operand2->flags;
2706 * If both operands are constant, the expression can be
2707 * collapsed to a single constant.
2709 combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
2711 flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
2713 flags |= operand1->flags &
2714 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2716 expr = create_hist_field(hist_data, NULL, flags, var_name);
2722 operand1->read_once = true;
2723 operand2->read_once = true;
2725 /* The operands are now owned and free'd by 'expr' */
2726 expr->operands[0] = operand1;
2727 expr->operands[1] = operand2;
2729 if (field_op == FIELD_OP_DIV &&
2730 operand2_flags & HIST_FIELD_FL_CONST) {
2731 u64 divisor = var2 ? var2->constant : operand2->constant;
2734 hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
2740 * Copy the divisor here so we don't have to look it up
2741 * later if this is a var ref
2743 operand2->constant = divisor;
2744 op_fn = hist_field_get_div_fn(operand2);
2747 expr->fn_num = op_fn;
2749 if (combine_consts) {
2751 expr->operands[0] = var1;
2753 expr->operands[1] = var2;
2755 expr->constant = hist_fn_call(expr, NULL, NULL, NULL, NULL);
2756 expr->fn_num = HIST_FIELD_FN_CONST;
2758 expr->operands[0] = NULL;
2759 expr->operands[1] = NULL;
2762 * var refs won't be destroyed immediately
2763 * See: destroy_hist_field()
2765 destroy_hist_field(operand2, 0);
2766 destroy_hist_field(operand1, 0);
2768 expr->name = expr_str(expr, 0);
2770 /* The operand sizes should be the same, so just pick one */
2771 expr->size = operand1->size;
2772 expr->is_signed = operand1->is_signed;
2774 expr->operator = field_op;
2775 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2781 expr->name = expr_str(expr, 0);
2787 destroy_hist_field(operand2, 0);
2789 destroy_hist_field(operand1, 0);
2790 return ERR_PTR(ret);
2793 destroy_hist_field(expr, 0);
2794 return ERR_PTR(ret);
2797 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2798 struct trace_event_file *file)
2800 struct event_trigger_data *test;
2802 lockdep_assert_held(&event_mutex);
2804 list_for_each_entry(test, &file->triggers, list) {
2805 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2806 if (test->private_data == hist_data)
2807 return test->filter_str;
2814 static struct event_command trigger_hist_cmd;
2815 static int event_hist_trigger_parse(struct event_command *cmd_ops,
2816 struct trace_event_file *file,
2817 char *glob, char *cmd,
2818 char *param_and_filter);
2820 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2821 struct hist_trigger_data *hist_data,
2822 unsigned int n_keys)
2824 struct hist_field *target_hist_field, *hist_field;
2825 unsigned int n, i, j;
2827 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2830 i = hist_data->n_vals;
2831 j = target_hist_data->n_vals;
2833 for (n = 0; n < n_keys; n++) {
2834 hist_field = hist_data->fields[i + n];
2835 target_hist_field = target_hist_data->fields[j + n];
2837 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2839 if (hist_field->size != target_hist_field->size)
2841 if (hist_field->is_signed != target_hist_field->is_signed)
2848 static struct hist_trigger_data *
2849 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2850 struct trace_event_file *file)
2852 struct hist_trigger_data *hist_data;
2853 struct event_trigger_data *test;
2854 unsigned int n_keys;
2856 lockdep_assert_held(&event_mutex);
2858 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2860 list_for_each_entry(test, &file->triggers, list) {
2861 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2862 hist_data = test->private_data;
2864 if (compatible_keys(target_hist_data, hist_data, n_keys))
2872 static struct trace_event_file *event_file(struct trace_array *tr,
2873 char *system, char *event_name)
2875 struct trace_event_file *file;
2877 file = __find_event_file(tr, system, event_name);
2879 return ERR_PTR(-EINVAL);
2884 static struct hist_field *
2885 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2886 char *system, char *event_name, char *field_name)
2888 struct hist_field *event_var;
2889 char *synthetic_name;
2891 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2892 if (!synthetic_name)
2893 return ERR_PTR(-ENOMEM);
2895 strcpy(synthetic_name, "synthetic_");
2896 strcat(synthetic_name, field_name);
2898 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2900 kfree(synthetic_name);
2906 * create_field_var_hist - Automatically create a histogram and var for a field
2907 * @target_hist_data: The target hist trigger
2908 * @subsys_name: Optional subsystem name
2909 * @event_name: Optional event name
2910 * @field_name: The name of the field (and the resulting variable)
2912 * Hist trigger actions fetch data from variables, not directly from
2913 * events. However, for convenience, users are allowed to directly
2914 * specify an event field in an action, which will be automatically
2915 * converted into a variable on their behalf.
2917 * If a user specifies a field on an event that isn't the event the
2918 * histogram currently being defined (the target event histogram), the
2919 * only way that can be accomplished is if a new hist trigger is
2920 * created and the field variable defined on that.
2922 * This function creates a new histogram compatible with the target
2923 * event (meaning a histogram with the same key as the target
2924 * histogram), and creates a variable for the specified field, but
2925 * with 'synthetic_' prepended to the variable name in order to avoid
2926 * collision with normal field variables.
2928 * Return: The variable created for the field.
2930 static struct hist_field *
2931 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2932 char *subsys_name, char *event_name, char *field_name)
2934 struct trace_array *tr = target_hist_data->event_file->tr;
2935 struct hist_trigger_data *hist_data;
2936 unsigned int i, n, first = true;
2937 struct field_var_hist *var_hist;
2938 struct trace_event_file *file;
2939 struct hist_field *key_field;
2940 struct hist_field *event_var;
2945 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2946 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2947 return ERR_PTR(-EINVAL);
2950 file = event_file(tr, subsys_name, event_name);
2953 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2954 ret = PTR_ERR(file);
2955 return ERR_PTR(ret);
2959 * Look for a histogram compatible with target. We'll use the
2960 * found histogram specification to create a new matching
2961 * histogram with our variable on it. target_hist_data is not
2962 * yet a registered histogram so we can't use that.
2964 hist_data = find_compatible_hist(target_hist_data, file);
2966 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2967 return ERR_PTR(-EINVAL);
2970 /* See if a synthetic field variable has already been created */
2971 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2972 event_name, field_name);
2973 if (!IS_ERR_OR_NULL(event_var))
2976 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2978 return ERR_PTR(-ENOMEM);
2980 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2983 return ERR_PTR(-ENOMEM);
2986 /* Use the same keys as the compatible histogram */
2987 strcat(cmd, "keys=");
2989 for_each_hist_key_field(i, hist_data) {
2990 key_field = hist_data->fields[i];
2993 strcat(cmd, key_field->field->name);
2997 /* Create the synthetic field variable specification */
2998 strcat(cmd, ":synthetic_");
2999 strcat(cmd, field_name);
3001 strcat(cmd, field_name);
3003 /* Use the same filter as the compatible histogram */
3004 saved_filter = find_trigger_filter(hist_data, file);
3006 strcat(cmd, " if ");
3007 strcat(cmd, saved_filter);
3010 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3011 if (!var_hist->cmd) {
3014 return ERR_PTR(-ENOMEM);
3017 /* Save the compatible histogram information */
3018 var_hist->hist_data = hist_data;
3020 /* Create the new histogram with our variable */
3021 ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
3025 kfree(var_hist->cmd);
3027 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3028 return ERR_PTR(ret);
3033 /* If we can't find the variable, something went wrong */
3034 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3035 event_name, field_name);
3036 if (IS_ERR_OR_NULL(event_var)) {
3037 kfree(var_hist->cmd);
3039 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3040 return ERR_PTR(-EINVAL);
3043 n = target_hist_data->n_field_var_hists;
3044 target_hist_data->field_var_hists[n] = var_hist;
3045 target_hist_data->n_field_var_hists++;
3050 static struct hist_field *
3051 find_target_event_var(struct hist_trigger_data *hist_data,
3052 char *subsys_name, char *event_name, char *var_name)
3054 struct trace_event_file *file = hist_data->event_file;
3055 struct hist_field *hist_field = NULL;
3058 struct trace_event_call *call;
3063 call = file->event_call;
3065 if (strcmp(subsys_name, call->class->system) != 0)
3068 if (strcmp(event_name, trace_event_name(call)) != 0)
3072 hist_field = find_var_field(hist_data, var_name);
3077 static inline void __update_field_vars(struct tracing_map_elt *elt,
3078 struct trace_buffer *buffer,
3079 struct ring_buffer_event *rbe,
3081 struct field_var **field_vars,
3082 unsigned int n_field_vars,
3083 unsigned int field_var_str_start)
3085 struct hist_elt_data *elt_data = elt->private_data;
3086 unsigned int i, j, var_idx;
3089 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3090 struct field_var *field_var = field_vars[i];
3091 struct hist_field *var = field_var->var;
3092 struct hist_field *val = field_var->val;
3094 var_val = hist_fn_call(val, elt, buffer, rbe, rec);
3095 var_idx = var->var.idx;
3097 if (val->flags & HIST_FIELD_FL_STRING) {
3098 char *str = elt_data->field_var_str[j++];
3099 char *val_str = (char *)(uintptr_t)var_val;
3102 size = min(val->size, STR_VAR_LEN_MAX);
3103 strscpy(str, val_str, size);
3104 var_val = (u64)(uintptr_t)str;
3106 tracing_map_set_var(elt, var_idx, var_val);
3110 static void update_field_vars(struct hist_trigger_data *hist_data,
3111 struct tracing_map_elt *elt,
3112 struct trace_buffer *buffer,
3113 struct ring_buffer_event *rbe,
3116 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
3117 hist_data->n_field_vars, 0);
3120 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3121 struct tracing_map_elt *elt,
3122 struct trace_buffer *buffer, void *rec,
3123 struct ring_buffer_event *rbe, void *key,
3124 struct action_data *data, u64 *var_ref_vals)
3126 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
3127 hist_data->n_save_vars, hist_data->n_field_var_str);
3130 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3131 struct trace_event_file *file,
3132 char *name, int size, const char *type)
3134 struct hist_field *var;
3137 if (find_var(hist_data, file, name) && !hist_data->remove) {
3138 var = ERR_PTR(-EINVAL);
3142 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3144 var = ERR_PTR(-ENOMEM);
3148 idx = tracing_map_add_var(hist_data->map);
3151 var = ERR_PTR(-EINVAL);
3156 var->flags = HIST_FIELD_FL_VAR;
3158 var->var.hist_data = var->hist_data = hist_data;
3160 var->var.name = kstrdup(name, GFP_KERNEL);
3161 var->type = kstrdup_const(type, GFP_KERNEL);
3162 if (!var->var.name || !var->type) {
3163 kfree_const(var->type);
3164 kfree(var->var.name);
3166 var = ERR_PTR(-ENOMEM);
3172 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3173 struct trace_event_file *file,
3176 struct hist_field *val = NULL, *var = NULL;
3177 unsigned long flags = HIST_FIELD_FL_VAR;
3178 struct trace_array *tr = file->tr;
3179 struct field_var *field_var;
3182 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3183 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3188 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3190 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3195 var = create_var(hist_data, file, field_name, val->size, val->type);
3197 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3203 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3211 field_var->var = var;
3212 field_var->val = val;
3216 field_var = ERR_PTR(ret);
3221 * create_target_field_var - Automatically create a variable for a field
3222 * @target_hist_data: The target hist trigger
3223 * @subsys_name: Optional subsystem name
3224 * @event_name: Optional event name
3225 * @var_name: The name of the field (and the resulting variable)
3227 * Hist trigger actions fetch data from variables, not directly from
3228 * events. However, for convenience, users are allowed to directly
3229 * specify an event field in an action, which will be automatically
3230 * converted into a variable on their behalf.
3232 * This function creates a field variable with the name var_name on
3233 * the hist trigger currently being defined on the target event. If
3234 * subsys_name and event_name are specified, this function simply
3235 * verifies that they do in fact match the target event subsystem and
3238 * Return: The variable created for the field.
3240 static struct field_var *
3241 create_target_field_var(struct hist_trigger_data *target_hist_data,
3242 char *subsys_name, char *event_name, char *var_name)
3244 struct trace_event_file *file = target_hist_data->event_file;
3247 struct trace_event_call *call;
3252 call = file->event_call;
3254 if (strcmp(subsys_name, call->class->system) != 0)
3257 if (strcmp(event_name, trace_event_name(call)) != 0)
3261 return create_field_var(target_hist_data, file, var_name);
3264 static bool check_track_val_max(u64 track_val, u64 var_val)
3266 if (var_val <= track_val)
3272 static bool check_track_val_changed(u64 track_val, u64 var_val)
3274 if (var_val == track_val)
3280 static u64 get_track_val(struct hist_trigger_data *hist_data,
3281 struct tracing_map_elt *elt,
3282 struct action_data *data)
3284 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3287 track_val = tracing_map_read_var(elt, track_var_idx);
3292 static void save_track_val(struct hist_trigger_data *hist_data,
3293 struct tracing_map_elt *elt,
3294 struct action_data *data, u64 var_val)
3296 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3298 tracing_map_set_var(elt, track_var_idx, var_val);
3301 static void save_track_data(struct hist_trigger_data *hist_data,
3302 struct tracing_map_elt *elt,
3303 struct trace_buffer *buffer, void *rec,
3304 struct ring_buffer_event *rbe, void *key,
3305 struct action_data *data, u64 *var_ref_vals)
3307 if (data->track_data.save_data)
3308 data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
3309 key, data, var_ref_vals);
3312 static bool check_track_val(struct tracing_map_elt *elt,
3313 struct action_data *data,
3316 struct hist_trigger_data *hist_data;
3319 hist_data = data->track_data.track_var->hist_data;
3320 track_val = get_track_val(hist_data, elt, data);
3322 return data->track_data.check_val(track_val, var_val);
3325 #ifdef CONFIG_TRACER_SNAPSHOT
3326 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3328 /* called with tr->max_lock held */
3329 struct track_data *track_data = tr->cond_snapshot->cond_data;
3330 struct hist_elt_data *elt_data, *track_elt_data;
3331 struct snapshot_context *context = cond_data;
3332 struct action_data *action;
3338 action = track_data->action_data;
3340 track_val = get_track_val(track_data->hist_data, context->elt,
3341 track_data->action_data);
3343 if (!action->track_data.check_val(track_data->track_val, track_val))
3346 track_data->track_val = track_val;
3347 memcpy(track_data->key, context->key, track_data->key_len);
3349 elt_data = context->elt->private_data;
3350 track_elt_data = track_data->elt.private_data;
3352 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3354 track_data->updated = true;
3359 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3360 struct tracing_map_elt *elt,
3361 struct trace_buffer *buffer, void *rec,
3362 struct ring_buffer_event *rbe, void *key,
3363 struct action_data *data,
3366 struct trace_event_file *file = hist_data->event_file;
3367 struct snapshot_context context;
3372 tracing_snapshot_cond(file->tr, &context);
3375 static void hist_trigger_print_key(struct seq_file *m,
3376 struct hist_trigger_data *hist_data,
3378 struct tracing_map_elt *elt);
3380 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3384 if (!hist_data->n_actions)
3387 for (i = 0; i < hist_data->n_actions; i++) {
3388 struct action_data *data = hist_data->actions[i];
3390 if (data->action == ACTION_SNAPSHOT)
3397 static void track_data_snapshot_print(struct seq_file *m,
3398 struct hist_trigger_data *hist_data)
3400 struct trace_event_file *file = hist_data->event_file;
3401 struct track_data *track_data;
3402 struct action_data *action;
3404 track_data = tracing_cond_snapshot_data(file->tr);
3408 if (!track_data->updated)
3411 action = snapshot_action(hist_data);
3415 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3416 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3417 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3418 action->track_data.var_str, track_data->track_val);
3420 seq_puts(m, "\ttriggered by event with key: ");
3421 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3425 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3429 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3430 struct tracing_map_elt *elt,
3431 struct trace_buffer *buffer, void *rec,
3432 struct ring_buffer_event *rbe, void *key,
3433 struct action_data *data,
3434 u64 *var_ref_vals) {}
3435 static void track_data_snapshot_print(struct seq_file *m,
3436 struct hist_trigger_data *hist_data) {}
3437 #endif /* CONFIG_TRACER_SNAPSHOT */
3439 static void track_data_print(struct seq_file *m,
3440 struct hist_trigger_data *hist_data,
3441 struct tracing_map_elt *elt,
3442 struct action_data *data)
3444 u64 track_val = get_track_val(hist_data, elt, data);
3445 unsigned int i, save_var_idx;
3447 if (data->handler == HANDLER_ONMAX)
3448 seq_printf(m, "\n\tmax: %10llu", track_val);
3449 else if (data->handler == HANDLER_ONCHANGE)
3450 seq_printf(m, "\n\tchanged: %10llu", track_val);
3452 if (data->action == ACTION_SNAPSHOT)
3455 for (i = 0; i < hist_data->n_save_vars; i++) {
3456 struct hist_field *save_val = hist_data->save_vars[i]->val;
3457 struct hist_field *save_var = hist_data->save_vars[i]->var;
3460 save_var_idx = save_var->var.idx;
3462 val = tracing_map_read_var(elt, save_var_idx);
3464 if (save_val->flags & HIST_FIELD_FL_STRING) {
3465 seq_printf(m, " %s: %-32s", save_var->var.name,
3466 (char *)(uintptr_t)(val));
3468 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3472 static void ontrack_action(struct hist_trigger_data *hist_data,
3473 struct tracing_map_elt *elt,
3474 struct trace_buffer *buffer, void *rec,
3475 struct ring_buffer_event *rbe, void *key,
3476 struct action_data *data, u64 *var_ref_vals)
3478 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3480 if (check_track_val(elt, data, var_val)) {
3481 save_track_val(hist_data, elt, data, var_val);
3482 save_track_data(hist_data, elt, buffer, rec, rbe,
3483 key, data, var_ref_vals);
3487 static void action_data_destroy(struct action_data *data)
3491 lockdep_assert_held(&event_mutex);
3493 kfree(data->action_name);
3495 for (i = 0; i < data->n_params; i++)
3496 kfree(data->params[i]);
3498 if (data->synth_event)
3499 data->synth_event->ref--;
3501 kfree(data->synth_event_name);
3506 static void track_data_destroy(struct hist_trigger_data *hist_data,
3507 struct action_data *data)
3509 struct trace_event_file *file = hist_data->event_file;
3511 destroy_hist_field(data->track_data.track_var, 0);
3513 if (data->action == ACTION_SNAPSHOT) {
3514 struct track_data *track_data;
3516 track_data = tracing_cond_snapshot_data(file->tr);
3517 if (track_data && track_data->hist_data == hist_data) {
3518 tracing_snapshot_cond_disable(file->tr);
3519 track_data_free(track_data);
3523 kfree(data->track_data.var_str);
3525 action_data_destroy(data);
3528 static int action_create(struct hist_trigger_data *hist_data,
3529 struct action_data *data);
3531 static int track_data_create(struct hist_trigger_data *hist_data,
3532 struct action_data *data)
3534 struct hist_field *var_field, *ref_field, *track_var = NULL;
3535 struct trace_event_file *file = hist_data->event_file;
3536 struct trace_array *tr = file->tr;
3537 char *track_data_var_str;
3540 track_data_var_str = data->track_data.var_str;
3541 if (track_data_var_str[0] != '$') {
3542 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3545 track_data_var_str++;
3547 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3549 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3553 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3557 data->track_data.var_ref = ref_field;
3559 if (data->handler == HANDLER_ONMAX)
3560 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3561 if (IS_ERR(track_var)) {
3562 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3563 ret = PTR_ERR(track_var);
3567 if (data->handler == HANDLER_ONCHANGE)
3568 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3569 if (IS_ERR(track_var)) {
3570 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3571 ret = PTR_ERR(track_var);
3574 data->track_data.track_var = track_var;
3576 ret = action_create(hist_data, data);
3581 static int parse_action_params(struct trace_array *tr, char *params,
3582 struct action_data *data)
3584 char *param, *saved_param;
3585 bool first_param = true;
3589 if (data->n_params >= SYNTH_FIELDS_MAX) {
3590 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3594 param = strsep(¶ms, ",");
3596 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3601 param = strstrip(param);
3602 if (strlen(param) < 2) {
3603 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3608 saved_param = kstrdup(param, GFP_KERNEL);
3614 if (first_param && data->use_trace_keyword) {
3615 data->synth_event_name = saved_param;
3616 first_param = false;
3619 first_param = false;
3621 data->params[data->n_params++] = saved_param;
3627 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3628 enum handler_id handler)
3635 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3640 action_name = strsep(&str, "(");
3641 if (!action_name || !str) {
3642 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3647 if (str_has_prefix(action_name, "save")) {
3648 char *params = strsep(&str, ")");
3651 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3656 ret = parse_action_params(tr, params, data);
3660 if (handler == HANDLER_ONMAX)
3661 data->track_data.check_val = check_track_val_max;
3662 else if (handler == HANDLER_ONCHANGE)
3663 data->track_data.check_val = check_track_val_changed;
3665 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3670 data->track_data.save_data = save_track_data_vars;
3671 data->fn = ontrack_action;
3672 data->action = ACTION_SAVE;
3673 } else if (str_has_prefix(action_name, "snapshot")) {
3674 char *params = strsep(&str, ")");
3677 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3682 if (handler == HANDLER_ONMAX)
3683 data->track_data.check_val = check_track_val_max;
3684 else if (handler == HANDLER_ONCHANGE)
3685 data->track_data.check_val = check_track_val_changed;
3687 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3692 data->track_data.save_data = save_track_data_snapshot;
3693 data->fn = ontrack_action;
3694 data->action = ACTION_SNAPSHOT;
3696 char *params = strsep(&str, ")");
3698 if (str_has_prefix(action_name, "trace"))
3699 data->use_trace_keyword = true;
3702 ret = parse_action_params(tr, params, data);
3707 if (handler == HANDLER_ONMAX)
3708 data->track_data.check_val = check_track_val_max;
3709 else if (handler == HANDLER_ONCHANGE)
3710 data->track_data.check_val = check_track_val_changed;
3712 if (handler != HANDLER_ONMATCH) {
3713 data->track_data.save_data = action_trace;
3714 data->fn = ontrack_action;
3716 data->fn = action_trace;
3718 data->action = ACTION_TRACE;
3721 data->action_name = kstrdup(action_name, GFP_KERNEL);
3722 if (!data->action_name) {
3727 data->handler = handler;
3732 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3733 char *str, enum handler_id handler)
3735 struct action_data *data;
3739 data = kzalloc(sizeof(*data), GFP_KERNEL);
3741 return ERR_PTR(-ENOMEM);
3743 var_str = strsep(&str, ")");
3744 if (!var_str || !str) {
3749 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3750 if (!data->track_data.var_str) {
3755 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3761 track_data_destroy(hist_data, data);
3762 data = ERR_PTR(ret);
3766 static void onmatch_destroy(struct action_data *data)
3768 kfree(data->match_data.event);
3769 kfree(data->match_data.event_system);
3771 action_data_destroy(data);
3774 static void destroy_field_var(struct field_var *field_var)
3779 destroy_hist_field(field_var->var, 0);
3780 destroy_hist_field(field_var->val, 0);
3785 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3789 for (i = 0; i < hist_data->n_field_vars; i++)
3790 destroy_field_var(hist_data->field_vars[i]);
3792 for (i = 0; i < hist_data->n_save_vars; i++)
3793 destroy_field_var(hist_data->save_vars[i]);
3796 static void save_field_var(struct hist_trigger_data *hist_data,
3797 struct field_var *field_var)
3799 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3801 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3802 hist_data->n_field_var_str++;
3806 static int check_synth_field(struct synth_event *event,
3807 struct hist_field *hist_field,
3808 unsigned int field_pos)
3810 struct synth_field *field;
3812 if (field_pos >= event->n_fields)
3815 field = event->fields[field_pos];
3818 * A dynamic string synth field can accept static or
3819 * dynamic. A static string synth field can only accept a
3820 * same-sized static string, which is checked for later.
3822 if (strstr(hist_field->type, "char[") && field->is_string
3823 && field->is_dynamic)
3826 if (strcmp(field->type, hist_field->type) != 0) {
3827 if (field->size != hist_field->size ||
3828 (!field->is_string && field->is_signed != hist_field->is_signed))
3835 static struct hist_field *
3836 trace_action_find_var(struct hist_trigger_data *hist_data,
3837 struct action_data *data,
3838 char *system, char *event, char *var)
3840 struct trace_array *tr = hist_data->event_file->tr;
3841 struct hist_field *hist_field;
3843 var++; /* skip '$' */
3845 hist_field = find_target_event_var(hist_data, system, event, var);
3847 if (!system && data->handler == HANDLER_ONMATCH) {
3848 system = data->match_data.event_system;
3849 event = data->match_data.event;
3852 hist_field = find_event_var(hist_data, system, event, var);
3856 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3861 static struct hist_field *
3862 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3863 struct action_data *data, char *system,
3864 char *event, char *var)
3866 struct hist_field *hist_field = NULL;
3867 struct field_var *field_var;
3870 * First try to create a field var on the target event (the
3871 * currently being defined). This will create a variable for
3872 * unqualified fields on the target event, or if qualified,
3873 * target fields that have qualified names matching the target.
3875 field_var = create_target_field_var(hist_data, system, event, var);
3877 if (field_var && !IS_ERR(field_var)) {
3878 save_field_var(hist_data, field_var);
3879 hist_field = field_var->var;
3883 * If no explicit system.event is specified, default to
3884 * looking for fields on the onmatch(system.event.xxx)
3887 if (!system && data->handler == HANDLER_ONMATCH) {
3888 system = data->match_data.event_system;
3889 event = data->match_data.event;
3895 * At this point, we're looking at a field on another
3896 * event. Because we can't modify a hist trigger on
3897 * another event to add a variable for a field, we need
3898 * to create a new trigger on that event and create the
3899 * variable at the same time.
3901 hist_field = create_field_var_hist(hist_data, system, event, var);
3902 if (IS_ERR(hist_field))
3908 destroy_field_var(field_var);
3913 static int trace_action_create(struct hist_trigger_data *hist_data,
3914 struct action_data *data)
3916 struct trace_array *tr = hist_data->event_file->tr;
3917 char *event_name, *param, *system = NULL;
3918 struct hist_field *hist_field, *var_ref;
3920 unsigned int field_pos = 0;
3921 struct synth_event *event;
3922 char *synth_event_name;
3923 int var_ref_idx, ret = 0;
3925 lockdep_assert_held(&event_mutex);
3927 /* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
3928 if (data->n_params > SYNTH_FIELDS_MAX)
3931 if (data->use_trace_keyword)
3932 synth_event_name = data->synth_event_name;
3934 synth_event_name = data->action_name;
3936 event = find_synth_event(synth_event_name);
3938 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3944 for (i = 0; i < data->n_params; i++) {
3947 p = param = kstrdup(data->params[i], GFP_KERNEL);
3953 system = strsep(¶m, ".");
3955 param = (char *)system;
3956 system = event_name = NULL;
3958 event_name = strsep(¶m, ".");
3966 if (param[0] == '$')
3967 hist_field = trace_action_find_var(hist_data, data,
3971 hist_field = trace_action_create_field_var(hist_data,
3983 if (check_synth_field(event, hist_field, field_pos) == 0) {
3984 var_ref = create_var_ref(hist_data, hist_field,
3985 system, event_name);
3992 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
3993 if (WARN_ON(var_ref_idx < 0)) {
3999 data->var_ref_idx[i] = var_ref_idx;
4006 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4012 if (field_pos != event->n_fields) {
4013 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4018 data->synth_event = event;
4027 static int action_create(struct hist_trigger_data *hist_data,
4028 struct action_data *data)
4030 struct trace_event_file *file = hist_data->event_file;
4031 struct trace_array *tr = file->tr;
4032 struct track_data *track_data;
4033 struct field_var *field_var;
4038 if (data->action == ACTION_TRACE)
4039 return trace_action_create(hist_data, data);
4041 if (data->action == ACTION_SNAPSHOT) {
4042 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4043 if (IS_ERR(track_data)) {
4044 ret = PTR_ERR(track_data);
4048 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4049 cond_snapshot_update);
4051 track_data_free(track_data);
4056 if (data->action == ACTION_SAVE) {
4057 if (hist_data->n_save_vars) {
4059 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4063 for (i = 0; i < data->n_params; i++) {
4064 param = kstrdup(data->params[i], GFP_KERNEL);
4070 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4071 if (IS_ERR(field_var)) {
4072 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4074 ret = PTR_ERR(field_var);
4079 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4080 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4081 hist_data->n_save_var_str++;
4089 static int onmatch_create(struct hist_trigger_data *hist_data,
4090 struct action_data *data)
4092 return action_create(hist_data, data);
4095 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4097 char *match_event, *match_event_system;
4098 struct action_data *data;
4101 data = kzalloc(sizeof(*data), GFP_KERNEL);
4103 return ERR_PTR(-ENOMEM);
4105 match_event = strsep(&str, ")");
4106 if (!match_event || !str) {
4107 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4111 match_event_system = strsep(&match_event, ".");
4113 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4117 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4118 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4122 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4123 if (!data->match_data.event) {
4128 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4129 if (!data->match_data.event_system) {
4134 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4140 onmatch_destroy(data);
4141 data = ERR_PTR(ret);
4145 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4147 hist_data->fields[HITCOUNT_IDX] =
4148 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4149 if (!hist_data->fields[HITCOUNT_IDX])
4152 hist_data->n_vals++;
4153 hist_data->n_fields++;
4155 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4161 static int __create_val_field(struct hist_trigger_data *hist_data,
4162 unsigned int val_idx,
4163 struct trace_event_file *file,
4164 char *var_name, char *field_str,
4165 unsigned long flags)
4167 struct hist_field *hist_field;
4168 int ret = 0, n_subexprs = 0;
4170 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
4171 if (IS_ERR(hist_field)) {
4172 ret = PTR_ERR(hist_field);
4176 hist_data->fields[val_idx] = hist_field;
4178 ++hist_data->n_vals;
4179 ++hist_data->n_fields;
4181 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4187 static int create_val_field(struct hist_trigger_data *hist_data,
4188 unsigned int val_idx,
4189 struct trace_event_file *file,
4192 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4195 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4198 static const char no_comm[] = "(no comm)";
4200 static u64 hist_field_execname(struct hist_field *hist_field,
4201 struct tracing_map_elt *elt,
4202 struct trace_buffer *buffer,
4203 struct ring_buffer_event *rbe,
4206 struct hist_elt_data *elt_data;
4208 if (WARN_ON_ONCE(!elt))
4209 return (u64)(unsigned long)no_comm;
4211 elt_data = elt->private_data;
4213 if (WARN_ON_ONCE(!elt_data->comm))
4214 return (u64)(unsigned long)no_comm;
4216 return (u64)(unsigned long)(elt_data->comm);
4219 static u64 hist_fn_call(struct hist_field *hist_field,
4220 struct tracing_map_elt *elt,
4221 struct trace_buffer *buffer,
4222 struct ring_buffer_event *rbe,
4225 switch (hist_field->fn_num) {
4226 case HIST_FIELD_FN_VAR_REF:
4227 return hist_field_var_ref(hist_field, elt, buffer, rbe, event);
4228 case HIST_FIELD_FN_COUNTER:
4229 return hist_field_counter(hist_field, elt, buffer, rbe, event);
4230 case HIST_FIELD_FN_CONST:
4231 return hist_field_const(hist_field, elt, buffer, rbe, event);
4232 case HIST_FIELD_FN_LOG2:
4233 return hist_field_log2(hist_field, elt, buffer, rbe, event);
4234 case HIST_FIELD_FN_BUCKET:
4235 return hist_field_bucket(hist_field, elt, buffer, rbe, event);
4236 case HIST_FIELD_FN_TIMESTAMP:
4237 return hist_field_timestamp(hist_field, elt, buffer, rbe, event);
4238 case HIST_FIELD_FN_CPU:
4239 return hist_field_cpu(hist_field, elt, buffer, rbe, event);
4240 case HIST_FIELD_FN_STRING:
4241 return hist_field_string(hist_field, elt, buffer, rbe, event);
4242 case HIST_FIELD_FN_DYNSTRING:
4243 return hist_field_dynstring(hist_field, elt, buffer, rbe, event);
4244 case HIST_FIELD_FN_RELDYNSTRING:
4245 return hist_field_reldynstring(hist_field, elt, buffer, rbe, event);
4246 case HIST_FIELD_FN_PSTRING:
4247 return hist_field_pstring(hist_field, elt, buffer, rbe, event);
4248 case HIST_FIELD_FN_S64:
4249 return hist_field_s64(hist_field, elt, buffer, rbe, event);
4250 case HIST_FIELD_FN_U64:
4251 return hist_field_u64(hist_field, elt, buffer, rbe, event);
4252 case HIST_FIELD_FN_S32:
4253 return hist_field_s32(hist_field, elt, buffer, rbe, event);
4254 case HIST_FIELD_FN_U32:
4255 return hist_field_u32(hist_field, elt, buffer, rbe, event);
4256 case HIST_FIELD_FN_S16:
4257 return hist_field_s16(hist_field, elt, buffer, rbe, event);
4258 case HIST_FIELD_FN_U16:
4259 return hist_field_u16(hist_field, elt, buffer, rbe, event);
4260 case HIST_FIELD_FN_S8:
4261 return hist_field_s8(hist_field, elt, buffer, rbe, event);
4262 case HIST_FIELD_FN_U8:
4263 return hist_field_u8(hist_field, elt, buffer, rbe, event);
4264 case HIST_FIELD_FN_UMINUS:
4265 return hist_field_unary_minus(hist_field, elt, buffer, rbe, event);
4266 case HIST_FIELD_FN_MINUS:
4267 return hist_field_minus(hist_field, elt, buffer, rbe, event);
4268 case HIST_FIELD_FN_PLUS:
4269 return hist_field_plus(hist_field, elt, buffer, rbe, event);
4270 case HIST_FIELD_FN_DIV:
4271 return hist_field_div(hist_field, elt, buffer, rbe, event);
4272 case HIST_FIELD_FN_MULT:
4273 return hist_field_mult(hist_field, elt, buffer, rbe, event);
4274 case HIST_FIELD_FN_DIV_POWER2:
4275 return div_by_power_of_two(hist_field, elt, buffer, rbe, event);
4276 case HIST_FIELD_FN_DIV_NOT_POWER2:
4277 return div_by_not_power_of_two(hist_field, elt, buffer, rbe, event);
4278 case HIST_FIELD_FN_DIV_MULT_SHIFT:
4279 return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event);
4280 case HIST_FIELD_FN_EXECNAME:
4281 return hist_field_execname(hist_field, elt, buffer, rbe, event);
4287 /* Convert a var that points to common_pid.execname to a string */
4288 static void update_var_execname(struct hist_field *hist_field)
4290 hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
4291 HIST_FIELD_FL_EXECNAME;
4292 hist_field->size = MAX_FILTER_STR_VAL;
4293 hist_field->is_signed = 0;
4295 kfree_const(hist_field->type);
4296 hist_field->type = "char[]";
4298 hist_field->fn_num = HIST_FIELD_FN_EXECNAME;
4301 static int create_var_field(struct hist_trigger_data *hist_data,
4302 unsigned int val_idx,
4303 struct trace_event_file *file,
4304 char *var_name, char *expr_str)
4306 struct trace_array *tr = hist_data->event_file->tr;
4307 unsigned long flags = 0;
4310 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4313 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4314 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4318 flags |= HIST_FIELD_FL_VAR;
4319 hist_data->n_vars++;
4320 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4323 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4325 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
4326 update_var_execname(hist_data->fields[val_idx]);
4328 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
4329 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
4334 static int create_val_fields(struct hist_trigger_data *hist_data,
4335 struct trace_event_file *file)
4337 char *fields_str, *field_str;
4338 unsigned int i, j = 1;
4341 ret = create_hitcount_val(hist_data);
4345 fields_str = hist_data->attrs->vals_str;
4349 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4350 j < TRACING_MAP_VALS_MAX; i++) {
4351 field_str = strsep(&fields_str, ",");
4355 if (strcmp(field_str, "hitcount") == 0)
4358 ret = create_val_field(hist_data, j++, file, field_str);
4363 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4369 static int create_key_field(struct hist_trigger_data *hist_data,
4370 unsigned int key_idx,
4371 unsigned int key_offset,
4372 struct trace_event_file *file,
4375 struct trace_array *tr = hist_data->event_file->tr;
4376 struct hist_field *hist_field = NULL;
4377 unsigned long flags = 0;
4378 unsigned int key_size;
4379 int ret = 0, n_subexprs = 0;
4381 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4384 flags |= HIST_FIELD_FL_KEY;
4386 if (strcmp(field_str, "stacktrace") == 0) {
4387 flags |= HIST_FIELD_FL_STACKTRACE;
4388 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4389 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4391 hist_field = parse_expr(hist_data, file, field_str, flags,
4393 if (IS_ERR(hist_field)) {
4394 ret = PTR_ERR(hist_field);
4398 if (field_has_hist_vars(hist_field, 0)) {
4399 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4400 destroy_hist_field(hist_field, 0);
4405 key_size = hist_field->size;
4408 hist_data->fields[key_idx] = hist_field;
4410 key_size = ALIGN(key_size, sizeof(u64));
4411 hist_data->fields[key_idx]->size = key_size;
4412 hist_data->fields[key_idx]->offset = key_offset;
4414 hist_data->key_size += key_size;
4416 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4421 hist_data->n_keys++;
4422 hist_data->n_fields++;
4424 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4432 static int create_key_fields(struct hist_trigger_data *hist_data,
4433 struct trace_event_file *file)
4435 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4436 char *fields_str, *field_str;
4439 fields_str = hist_data->attrs->keys_str;
4443 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4444 field_str = strsep(&fields_str, ",");
4447 ret = create_key_field(hist_data, i, key_offset,
4462 static int create_var_fields(struct hist_trigger_data *hist_data,
4463 struct trace_event_file *file)
4465 unsigned int i, j = hist_data->n_vals;
4468 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4470 for (i = 0; i < n_vars; i++) {
4471 char *var_name = hist_data->attrs->var_defs.name[i];
4472 char *expr = hist_data->attrs->var_defs.expr[i];
4474 ret = create_var_field(hist_data, j++, file, var_name, expr);
4482 static void free_var_defs(struct hist_trigger_data *hist_data)
4486 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4487 kfree(hist_data->attrs->var_defs.name[i]);
4488 kfree(hist_data->attrs->var_defs.expr[i]);
4491 hist_data->attrs->var_defs.n_vars = 0;
4494 static int parse_var_defs(struct hist_trigger_data *hist_data)
4496 struct trace_array *tr = hist_data->event_file->tr;
4497 char *s, *str, *var_name, *field_str;
4498 unsigned int i, j, n_vars = 0;
4501 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4502 str = hist_data->attrs->assignment_str[i];
4503 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4504 field_str = strsep(&str, ",");
4508 var_name = strsep(&field_str, "=");
4509 if (!var_name || !field_str) {
4510 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4516 if (n_vars == TRACING_MAP_VARS_MAX) {
4517 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4522 s = kstrdup(var_name, GFP_KERNEL);
4527 hist_data->attrs->var_defs.name[n_vars] = s;
4529 s = kstrdup(field_str, GFP_KERNEL);
4531 kfree(hist_data->attrs->var_defs.name[n_vars]);
4532 hist_data->attrs->var_defs.name[n_vars] = NULL;
4536 hist_data->attrs->var_defs.expr[n_vars++] = s;
4538 hist_data->attrs->var_defs.n_vars = n_vars;
4544 free_var_defs(hist_data);
4549 static int create_hist_fields(struct hist_trigger_data *hist_data,
4550 struct trace_event_file *file)
4554 ret = parse_var_defs(hist_data);
4558 ret = create_val_fields(hist_data, file);
4562 ret = create_var_fields(hist_data, file);
4566 ret = create_key_fields(hist_data, file);
4569 free_var_defs(hist_data);
4574 static int is_descending(struct trace_array *tr, const char *str)
4579 if (strcmp(str, "descending") == 0)
4582 if (strcmp(str, "ascending") == 0)
4585 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4590 static int create_sort_keys(struct hist_trigger_data *hist_data)
4592 struct trace_array *tr = hist_data->event_file->tr;
4593 char *fields_str = hist_data->attrs->sort_key_str;
4594 struct tracing_map_sort_key *sort_key;
4595 int descending, ret = 0;
4596 unsigned int i, j, k;
4598 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4603 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4604 struct hist_field *hist_field;
4605 char *field_str, *field_name;
4606 const char *test_name;
4608 sort_key = &hist_data->sort_keys[i];
4610 field_str = strsep(&fields_str, ",");
4616 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4620 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4621 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4626 field_name = strsep(&field_str, ".");
4627 if (!field_name || !*field_name) {
4629 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4633 if (strcmp(field_name, "hitcount") == 0) {
4634 descending = is_descending(tr, field_str);
4635 if (descending < 0) {
4639 sort_key->descending = descending;
4643 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4646 hist_field = hist_data->fields[j];
4647 if (hist_field->flags & HIST_FIELD_FL_VAR)
4652 test_name = hist_field_name(hist_field, 0);
4654 if (strcmp(field_name, test_name) == 0) {
4655 sort_key->field_idx = idx;
4656 descending = is_descending(tr, field_str);
4657 if (descending < 0) {
4661 sort_key->descending = descending;
4665 if (j == hist_data->n_fields) {
4667 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4672 hist_data->n_sort_keys = i;
4677 static void destroy_actions(struct hist_trigger_data *hist_data)
4681 for (i = 0; i < hist_data->n_actions; i++) {
4682 struct action_data *data = hist_data->actions[i];
4684 if (data->handler == HANDLER_ONMATCH)
4685 onmatch_destroy(data);
4686 else if (data->handler == HANDLER_ONMAX ||
4687 data->handler == HANDLER_ONCHANGE)
4688 track_data_destroy(hist_data, data);
4694 static int parse_actions(struct hist_trigger_data *hist_data)
4696 struct trace_array *tr = hist_data->event_file->tr;
4697 struct action_data *data;
4703 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4704 str = hist_data->attrs->action_str[i];
4706 if ((len = str_has_prefix(str, "onmatch("))) {
4707 char *action_str = str + len;
4709 data = onmatch_parse(tr, action_str);
4711 ret = PTR_ERR(data);
4714 } else if ((len = str_has_prefix(str, "onmax("))) {
4715 char *action_str = str + len;
4717 data = track_data_parse(hist_data, action_str,
4720 ret = PTR_ERR(data);
4723 } else if ((len = str_has_prefix(str, "onchange("))) {
4724 char *action_str = str + len;
4726 data = track_data_parse(hist_data, action_str,
4729 ret = PTR_ERR(data);
4737 hist_data->actions[hist_data->n_actions++] = data;
4743 static int create_actions(struct hist_trigger_data *hist_data)
4745 struct action_data *data;
4749 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4750 data = hist_data->actions[i];
4752 if (data->handler == HANDLER_ONMATCH) {
4753 ret = onmatch_create(hist_data, data);
4756 } else if (data->handler == HANDLER_ONMAX ||
4757 data->handler == HANDLER_ONCHANGE) {
4758 ret = track_data_create(hist_data, data);
4770 static void print_actions(struct seq_file *m,
4771 struct hist_trigger_data *hist_data,
4772 struct tracing_map_elt *elt)
4776 for (i = 0; i < hist_data->n_actions; i++) {
4777 struct action_data *data = hist_data->actions[i];
4779 if (data->action == ACTION_SNAPSHOT)
4782 if (data->handler == HANDLER_ONMAX ||
4783 data->handler == HANDLER_ONCHANGE)
4784 track_data_print(m, hist_data, elt, data);
4788 static void print_action_spec(struct seq_file *m,
4789 struct hist_trigger_data *hist_data,
4790 struct action_data *data)
4794 if (data->action == ACTION_SAVE) {
4795 for (i = 0; i < hist_data->n_save_vars; i++) {
4796 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4797 if (i < hist_data->n_save_vars - 1)
4800 } else if (data->action == ACTION_TRACE) {
4801 if (data->use_trace_keyword)
4802 seq_printf(m, "%s", data->synth_event_name);
4803 for (i = 0; i < data->n_params; i++) {
4804 if (i || data->use_trace_keyword)
4806 seq_printf(m, "%s", data->params[i]);
4811 static void print_track_data_spec(struct seq_file *m,
4812 struct hist_trigger_data *hist_data,
4813 struct action_data *data)
4815 if (data->handler == HANDLER_ONMAX)
4816 seq_puts(m, ":onmax(");
4817 else if (data->handler == HANDLER_ONCHANGE)
4818 seq_puts(m, ":onchange(");
4819 seq_printf(m, "%s", data->track_data.var_str);
4820 seq_printf(m, ").%s(", data->action_name);
4822 print_action_spec(m, hist_data, data);
4827 static void print_onmatch_spec(struct seq_file *m,
4828 struct hist_trigger_data *hist_data,
4829 struct action_data *data)
4831 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4832 data->match_data.event);
4834 seq_printf(m, "%s(", data->action_name);
4836 print_action_spec(m, hist_data, data);
4841 static bool actions_match(struct hist_trigger_data *hist_data,
4842 struct hist_trigger_data *hist_data_test)
4846 if (hist_data->n_actions != hist_data_test->n_actions)
4849 for (i = 0; i < hist_data->n_actions; i++) {
4850 struct action_data *data = hist_data->actions[i];
4851 struct action_data *data_test = hist_data_test->actions[i];
4852 char *action_name, *action_name_test;
4854 if (data->handler != data_test->handler)
4856 if (data->action != data_test->action)
4859 if (data->n_params != data_test->n_params)
4862 for (j = 0; j < data->n_params; j++) {
4863 if (strcmp(data->params[j], data_test->params[j]) != 0)
4867 if (data->use_trace_keyword)
4868 action_name = data->synth_event_name;
4870 action_name = data->action_name;
4872 if (data_test->use_trace_keyword)
4873 action_name_test = data_test->synth_event_name;
4875 action_name_test = data_test->action_name;
4877 if (strcmp(action_name, action_name_test) != 0)
4880 if (data->handler == HANDLER_ONMATCH) {
4881 if (strcmp(data->match_data.event_system,
4882 data_test->match_data.event_system) != 0)
4884 if (strcmp(data->match_data.event,
4885 data_test->match_data.event) != 0)
4887 } else if (data->handler == HANDLER_ONMAX ||
4888 data->handler == HANDLER_ONCHANGE) {
4889 if (strcmp(data->track_data.var_str,
4890 data_test->track_data.var_str) != 0)
4899 static void print_actions_spec(struct seq_file *m,
4900 struct hist_trigger_data *hist_data)
4904 for (i = 0; i < hist_data->n_actions; i++) {
4905 struct action_data *data = hist_data->actions[i];
4907 if (data->handler == HANDLER_ONMATCH)
4908 print_onmatch_spec(m, hist_data, data);
4909 else if (data->handler == HANDLER_ONMAX ||
4910 data->handler == HANDLER_ONCHANGE)
4911 print_track_data_spec(m, hist_data, data);
4915 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4919 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4920 kfree(hist_data->field_var_hists[i]->cmd);
4921 kfree(hist_data->field_var_hists[i]);
4925 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4930 destroy_hist_trigger_attrs(hist_data->attrs);
4931 destroy_hist_fields(hist_data);
4932 tracing_map_destroy(hist_data->map);
4934 destroy_actions(hist_data);
4935 destroy_field_vars(hist_data);
4936 destroy_field_var_hists(hist_data);
4941 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4943 struct tracing_map *map = hist_data->map;
4944 struct ftrace_event_field *field;
4945 struct hist_field *hist_field;
4948 for_each_hist_field(i, hist_data) {
4949 hist_field = hist_data->fields[i];
4950 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4951 tracing_map_cmp_fn_t cmp_fn;
4953 field = hist_field->field;
4955 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4956 cmp_fn = tracing_map_cmp_none;
4957 else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
4958 cmp_fn = tracing_map_cmp_num(hist_field->size,
4959 hist_field->is_signed);
4960 else if (is_string_field(field))
4961 cmp_fn = tracing_map_cmp_string;
4963 cmp_fn = tracing_map_cmp_num(field->size,
4965 idx = tracing_map_add_key_field(map,
4968 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4969 idx = tracing_map_add_sum_field(map);
4974 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4975 idx = tracing_map_add_var(map);
4978 hist_field->var.idx = idx;
4979 hist_field->var.hist_data = hist_data;
4986 static struct hist_trigger_data *
4987 create_hist_data(unsigned int map_bits,
4988 struct hist_trigger_attrs *attrs,
4989 struct trace_event_file *file,
4992 const struct tracing_map_ops *map_ops = NULL;
4993 struct hist_trigger_data *hist_data;
4996 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4998 return ERR_PTR(-ENOMEM);
5000 hist_data->attrs = attrs;
5001 hist_data->remove = remove;
5002 hist_data->event_file = file;
5004 ret = parse_actions(hist_data);
5008 ret = create_hist_fields(hist_data, file);
5012 ret = create_sort_keys(hist_data);
5016 map_ops = &hist_trigger_elt_data_ops;
5018 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5019 map_ops, hist_data);
5020 if (IS_ERR(hist_data->map)) {
5021 ret = PTR_ERR(hist_data->map);
5022 hist_data->map = NULL;
5026 ret = create_tracing_map_fields(hist_data);
5032 hist_data->attrs = NULL;
5034 destroy_hist_data(hist_data);
5036 hist_data = ERR_PTR(ret);
5041 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5042 struct tracing_map_elt *elt,
5043 struct trace_buffer *buffer, void *rec,
5044 struct ring_buffer_event *rbe,
5047 struct hist_elt_data *elt_data;
5048 struct hist_field *hist_field;
5049 unsigned int i, var_idx;
5052 elt_data = elt->private_data;
5053 elt_data->var_ref_vals = var_ref_vals;
5055 for_each_hist_val_field(i, hist_data) {
5056 hist_field = hist_data->fields[i];
5057 hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5058 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5059 var_idx = hist_field->var.idx;
5061 if (hist_field->flags & HIST_FIELD_FL_STRING) {
5062 unsigned int str_start, var_str_idx, idx;
5063 char *str, *val_str;
5066 str_start = hist_data->n_field_var_str +
5067 hist_data->n_save_var_str;
5068 var_str_idx = hist_field->var_str_idx;
5069 idx = str_start + var_str_idx;
5071 str = elt_data->field_var_str[idx];
5072 val_str = (char *)(uintptr_t)hist_val;
5074 size = min(hist_field->size, STR_VAR_LEN_MAX);
5075 strscpy(str, val_str, size);
5077 hist_val = (u64)(uintptr_t)str;
5079 tracing_map_set_var(elt, var_idx, hist_val);
5082 tracing_map_update_sum(elt, i, hist_val);
5085 for_each_hist_key_field(i, hist_data) {
5086 hist_field = hist_data->fields[i];
5087 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5088 hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5089 var_idx = hist_field->var.idx;
5090 tracing_map_set_var(elt, var_idx, hist_val);
5094 update_field_vars(hist_data, elt, buffer, rbe, rec);
5097 static inline void add_to_key(char *compound_key, void *key,
5098 struct hist_field *key_field, void *rec)
5100 size_t size = key_field->size;
5102 if (key_field->flags & HIST_FIELD_FL_STRING) {
5103 struct ftrace_event_field *field;
5105 field = key_field->field;
5106 if (field->filter_type == FILTER_DYN_STRING ||
5107 field->filter_type == FILTER_RDYN_STRING)
5108 size = *(u32 *)(rec + field->offset) >> 16;
5109 else if (field->filter_type == FILTER_STATIC_STRING)
5112 /* ensure NULL-termination */
5113 if (size > key_field->size - 1)
5114 size = key_field->size - 1;
5116 strncpy(compound_key + key_field->offset, (char *)key, size);
5118 memcpy(compound_key + key_field->offset, key, size);
5122 hist_trigger_actions(struct hist_trigger_data *hist_data,
5123 struct tracing_map_elt *elt,
5124 struct trace_buffer *buffer, void *rec,
5125 struct ring_buffer_event *rbe, void *key,
5128 struct action_data *data;
5131 for (i = 0; i < hist_data->n_actions; i++) {
5132 data = hist_data->actions[i];
5133 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
5137 static void event_hist_trigger(struct event_trigger_data *data,
5138 struct trace_buffer *buffer, void *rec,
5139 struct ring_buffer_event *rbe)
5141 struct hist_trigger_data *hist_data = data->private_data;
5142 bool use_compound_key = (hist_data->n_keys > 1);
5143 unsigned long entries[HIST_STACKTRACE_DEPTH];
5144 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5145 char compound_key[HIST_KEY_SIZE_MAX];
5146 struct tracing_map_elt *elt = NULL;
5147 struct hist_field *key_field;
5155 memset(compound_key, 0, hist_data->key_size);
5157 for_each_hist_key_field(i, hist_data) {
5158 key_field = hist_data->fields[i];
5160 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5161 memset(entries, 0, HIST_STACKTRACE_SIZE);
5162 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5163 HIST_STACKTRACE_SKIP);
5166 field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
5167 if (key_field->flags & HIST_FIELD_FL_STRING) {
5168 key = (void *)(unsigned long)field_contents;
5169 use_compound_key = true;
5171 key = (void *)&field_contents;
5174 if (use_compound_key)
5175 add_to_key(compound_key, key, key_field, rec);
5178 if (use_compound_key)
5181 if (hist_data->n_var_refs &&
5182 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5185 elt = tracing_map_insert(hist_data->map, key);
5189 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
5191 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5192 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
5195 static void hist_trigger_stacktrace_print(struct seq_file *m,
5196 unsigned long *stacktrace_entries,
5197 unsigned int max_entries)
5199 unsigned int spaces = 8;
5202 for (i = 0; i < max_entries; i++) {
5203 if (!stacktrace_entries[i])
5206 seq_printf(m, "%*c", 1 + spaces, ' ');
5207 seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
5211 static void hist_trigger_print_key(struct seq_file *m,
5212 struct hist_trigger_data *hist_data,
5214 struct tracing_map_elt *elt)
5216 struct hist_field *key_field;
5217 bool multiline = false;
5218 const char *field_name;
5224 for_each_hist_key_field(i, hist_data) {
5225 key_field = hist_data->fields[i];
5227 if (i > hist_data->n_vals)
5230 field_name = hist_field_name(key_field, 0);
5232 if (key_field->flags & HIST_FIELD_FL_HEX) {
5233 uval = *(u64 *)(key + key_field->offset);
5234 seq_printf(m, "%s: %llx", field_name, uval);
5235 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5236 uval = *(u64 *)(key + key_field->offset);
5237 seq_printf(m, "%s: [%llx] %-45ps", field_name,
5238 uval, (void *)(uintptr_t)uval);
5239 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5240 uval = *(u64 *)(key + key_field->offset);
5241 seq_printf(m, "%s: [%llx] %-55pS", field_name,
5242 uval, (void *)(uintptr_t)uval);
5243 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5244 struct hist_elt_data *elt_data = elt->private_data;
5247 if (WARN_ON_ONCE(!elt_data))
5250 comm = elt_data->comm;
5252 uval = *(u64 *)(key + key_field->offset);
5253 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5255 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5256 const char *syscall_name;
5258 uval = *(u64 *)(key + key_field->offset);
5259 syscall_name = get_syscall_name(uval);
5261 syscall_name = "unknown_syscall";
5263 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5264 syscall_name, uval);
5265 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5266 seq_puts(m, "stacktrace:\n");
5267 hist_trigger_stacktrace_print(m,
5268 key + key_field->offset,
5269 HIST_STACKTRACE_DEPTH);
5271 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5272 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5273 *(u64 *)(key + key_field->offset));
5274 } else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
5275 unsigned long buckets = key_field->buckets;
5276 uval = *(u64 *)(key + key_field->offset);
5277 seq_printf(m, "%s: ~ %llu-%llu", field_name,
5278 uval, uval + buckets -1);
5279 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5280 seq_printf(m, "%s: %-50s", field_name,
5281 (char *)(key + key_field->offset));
5283 uval = *(u64 *)(key + key_field->offset);
5284 seq_printf(m, "%s: %10llu", field_name, uval);
5294 static void hist_trigger_entry_print(struct seq_file *m,
5295 struct hist_trigger_data *hist_data,
5297 struct tracing_map_elt *elt)
5299 const char *field_name;
5302 hist_trigger_print_key(m, hist_data, key, elt);
5304 seq_printf(m, " hitcount: %10llu",
5305 tracing_map_read_sum(elt, HITCOUNT_IDX));
5307 for (i = 1; i < hist_data->n_vals; i++) {
5308 field_name = hist_field_name(hist_data->fields[i], 0);
5310 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5311 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
5314 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
5315 seq_printf(m, " %s: %10llx", field_name,
5316 tracing_map_read_sum(elt, i));
5318 seq_printf(m, " %s: %10llu", field_name,
5319 tracing_map_read_sum(elt, i));
5323 print_actions(m, hist_data, elt);
5328 static int print_entries(struct seq_file *m,
5329 struct hist_trigger_data *hist_data)
5331 struct tracing_map_sort_entry **sort_entries = NULL;
5332 struct tracing_map *map = hist_data->map;
5335 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5336 hist_data->n_sort_keys,
5341 for (i = 0; i < n_entries; i++)
5342 hist_trigger_entry_print(m, hist_data,
5343 sort_entries[i]->key,
5344 sort_entries[i]->elt);
5346 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5351 static void hist_trigger_show(struct seq_file *m,
5352 struct event_trigger_data *data, int n)
5354 struct hist_trigger_data *hist_data;
5358 seq_puts(m, "\n\n");
5360 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5361 data->ops->print(m, data);
5362 seq_puts(m, "#\n\n");
5364 hist_data = data->private_data;
5365 n_entries = print_entries(m, hist_data);
5369 track_data_snapshot_print(m, hist_data);
5371 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5372 (u64)atomic64_read(&hist_data->map->hits),
5373 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5376 static int hist_show(struct seq_file *m, void *v)
5378 struct event_trigger_data *data;
5379 struct trace_event_file *event_file;
5382 mutex_lock(&event_mutex);
5384 event_file = event_file_data(m->private);
5385 if (unlikely(!event_file)) {
5390 list_for_each_entry(data, &event_file->triggers, list) {
5391 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5392 hist_trigger_show(m, data, n++);
5396 mutex_unlock(&event_mutex);
5401 static int event_hist_open(struct inode *inode, struct file *file)
5405 ret = security_locked_down(LOCKDOWN_TRACEFS);
5409 return single_open(file, hist_show, file);
5412 const struct file_operations event_hist_fops = {
5413 .open = event_hist_open,
5415 .llseek = seq_lseek,
5416 .release = single_release,
5419 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
5420 static void hist_field_debug_show_flags(struct seq_file *m,
5421 unsigned long flags)
5423 seq_puts(m, " flags:\n");
5425 if (flags & HIST_FIELD_FL_KEY)
5426 seq_puts(m, " HIST_FIELD_FL_KEY\n");
5427 else if (flags & HIST_FIELD_FL_HITCOUNT)
5428 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
5429 else if (flags & HIST_FIELD_FL_VAR)
5430 seq_puts(m, " HIST_FIELD_FL_VAR\n");
5431 else if (flags & HIST_FIELD_FL_VAR_REF)
5432 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
5434 seq_puts(m, " VAL: normal u64 value\n");
5436 if (flags & HIST_FIELD_FL_ALIAS)
5437 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
5438 else if (flags & HIST_FIELD_FL_CONST)
5439 seq_puts(m, " HIST_FIELD_FL_CONST\n");
5442 static int hist_field_debug_show(struct seq_file *m,
5443 struct hist_field *field, unsigned long flags)
5445 if ((field->flags & flags) != flags) {
5446 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
5450 hist_field_debug_show_flags(m, field->flags);
5452 seq_printf(m, " ftrace_event_field name: %s\n",
5453 field->field->name);
5455 if (field->flags & HIST_FIELD_FL_VAR) {
5456 seq_printf(m, " var.name: %s\n", field->var.name);
5457 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5461 if (field->flags & HIST_FIELD_FL_CONST)
5462 seq_printf(m, " constant: %llu\n", field->constant);
5464 if (field->flags & HIST_FIELD_FL_ALIAS)
5465 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5466 field->var_ref_idx);
5468 if (field->flags & HIST_FIELD_FL_VAR_REF) {
5469 seq_printf(m, " name: %s\n", field->name);
5470 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5472 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
5473 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5474 field->var_ref_idx);
5476 seq_printf(m, " system: %s\n", field->system);
5477 if (field->event_name)
5478 seq_printf(m, " event_name: %s\n", field->event_name);
5481 seq_printf(m, " type: %s\n", field->type);
5482 seq_printf(m, " size: %u\n", field->size);
5483 seq_printf(m, " is_signed: %u\n", field->is_signed);
5488 static int field_var_debug_show(struct seq_file *m,
5489 struct field_var *field_var, unsigned int i,
5492 const char *vars_name = save_vars ? "save_vars" : "field_vars";
5493 struct hist_field *field;
5496 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
5498 field = field_var->var;
5500 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
5502 hist_field_debug_show_flags(m, field->flags);
5503 seq_printf(m, " var.name: %s\n", field->var.name);
5504 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5507 field = field_var->val;
5509 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
5511 seq_printf(m, " ftrace_event_field name: %s\n",
5512 field->field->name);
5518 seq_printf(m, " type: %s\n", field->type);
5519 seq_printf(m, " size: %u\n", field->size);
5520 seq_printf(m, " is_signed: %u\n", field->is_signed);
5525 static int hist_action_debug_show(struct seq_file *m,
5526 struct action_data *data, int i)
5530 if (data->handler == HANDLER_ONMAX ||
5531 data->handler == HANDLER_ONCHANGE) {
5532 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
5533 ret = hist_field_debug_show(m, data->track_data.var_ref,
5534 HIST_FIELD_FL_VAR_REF);
5538 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
5539 ret = hist_field_debug_show(m, data->track_data.track_var,
5545 if (data->handler == HANDLER_ONMATCH) {
5546 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
5547 i, data->match_data.event_system);
5548 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
5549 i, data->match_data.event);
5555 static int hist_actions_debug_show(struct seq_file *m,
5556 struct hist_trigger_data *hist_data)
5560 if (hist_data->n_actions)
5561 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
5563 for (i = 0; i < hist_data->n_actions; i++) {
5564 struct action_data *action = hist_data->actions[i];
5566 ret = hist_action_debug_show(m, action, i);
5571 if (hist_data->n_save_vars)
5572 seq_puts(m, "\n save action variables (save() params):\n");
5574 for (i = 0; i < hist_data->n_save_vars; i++) {
5575 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
5583 static void hist_trigger_debug_show(struct seq_file *m,
5584 struct event_trigger_data *data, int n)
5586 struct hist_trigger_data *hist_data;
5590 seq_puts(m, "\n\n");
5592 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5593 data->ops->print(m, data);
5594 seq_puts(m, "#\n\n");
5596 hist_data = data->private_data;
5598 seq_printf(m, "hist_data: %p\n\n", hist_data);
5599 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
5600 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
5601 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
5603 seq_puts(m, "\n val fields:\n\n");
5605 seq_puts(m, " hist_data->fields[0]:\n");
5606 ret = hist_field_debug_show(m, hist_data->fields[0],
5607 HIST_FIELD_FL_HITCOUNT);
5611 for (i = 1; i < hist_data->n_vals; i++) {
5612 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5613 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5618 seq_puts(m, "\n key fields:\n");
5620 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5621 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5622 ret = hist_field_debug_show(m, hist_data->fields[i],
5628 if (hist_data->n_var_refs)
5629 seq_puts(m, "\n variable reference fields:\n");
5631 for (i = 0; i < hist_data->n_var_refs; i++) {
5632 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5633 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5634 HIST_FIELD_FL_VAR_REF);
5639 if (hist_data->n_field_vars)
5640 seq_puts(m, "\n field variables:\n");
5642 for (i = 0; i < hist_data->n_field_vars; i++) {
5643 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5648 ret = hist_actions_debug_show(m, hist_data);
5653 static int hist_debug_show(struct seq_file *m, void *v)
5655 struct event_trigger_data *data;
5656 struct trace_event_file *event_file;
5659 mutex_lock(&event_mutex);
5661 event_file = event_file_data(m->private);
5662 if (unlikely(!event_file)) {
5667 list_for_each_entry(data, &event_file->triggers, list) {
5668 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5669 hist_trigger_debug_show(m, data, n++);
5673 mutex_unlock(&event_mutex);
5678 static int event_hist_debug_open(struct inode *inode, struct file *file)
5682 ret = security_locked_down(LOCKDOWN_TRACEFS);
5686 return single_open(file, hist_debug_show, file);
5689 const struct file_operations event_hist_debug_fops = {
5690 .open = event_hist_debug_open,
5692 .llseek = seq_lseek,
5693 .release = single_release,
5697 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5699 const char *field_name = hist_field_name(hist_field, 0);
5701 if (hist_field->var.name)
5702 seq_printf(m, "%s=", hist_field->var.name);
5704 if (hist_field->flags & HIST_FIELD_FL_CPU)
5705 seq_puts(m, "common_cpu");
5706 else if (hist_field->flags & HIST_FIELD_FL_CONST)
5707 seq_printf(m, "%llu", hist_field->constant);
5708 else if (field_name) {
5709 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5710 hist_field->flags & HIST_FIELD_FL_ALIAS)
5712 seq_printf(m, "%s", field_name);
5713 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5714 seq_puts(m, "common_timestamp");
5716 if (hist_field->flags) {
5717 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5718 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5719 const char *flags = get_hist_field_flags(hist_field);
5722 seq_printf(m, ".%s", flags);
5725 if (hist_field->buckets)
5726 seq_printf(m, "=%ld", hist_field->buckets);
5729 static int event_hist_trigger_print(struct seq_file *m,
5730 struct event_trigger_data *data)
5732 struct hist_trigger_data *hist_data = data->private_data;
5733 struct hist_field *field;
5734 bool have_var = false;
5737 seq_puts(m, HIST_PREFIX);
5740 seq_printf(m, "%s:", data->name);
5742 seq_puts(m, "keys=");
5744 for_each_hist_key_field(i, hist_data) {
5745 field = hist_data->fields[i];
5747 if (i > hist_data->n_vals)
5750 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5751 seq_puts(m, "stacktrace");
5753 hist_field_print(m, field);
5756 seq_puts(m, ":vals=");
5758 for_each_hist_val_field(i, hist_data) {
5759 field = hist_data->fields[i];
5760 if (field->flags & HIST_FIELD_FL_VAR) {
5765 if (i == HITCOUNT_IDX)
5766 seq_puts(m, "hitcount");
5769 hist_field_print(m, field);
5778 for_each_hist_val_field(i, hist_data) {
5779 field = hist_data->fields[i];
5781 if (field->flags & HIST_FIELD_FL_VAR) {
5784 hist_field_print(m, field);
5789 seq_puts(m, ":sort=");
5791 for (i = 0; i < hist_data->n_sort_keys; i++) {
5792 struct tracing_map_sort_key *sort_key;
5793 unsigned int idx, first_key_idx;
5796 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5798 sort_key = &hist_data->sort_keys[i];
5799 idx = sort_key->field_idx;
5801 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5807 if (idx == HITCOUNT_IDX)
5808 seq_puts(m, "hitcount");
5810 if (idx >= first_key_idx)
5811 idx += hist_data->n_vars;
5812 hist_field_print(m, hist_data->fields[idx]);
5815 if (sort_key->descending)
5816 seq_puts(m, ".descending");
5818 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5819 if (hist_data->enable_timestamps)
5820 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5822 print_actions_spec(m, hist_data);
5824 if (data->filter_str)
5825 seq_printf(m, " if %s", data->filter_str);
5828 seq_puts(m, " [paused]");
5830 seq_puts(m, " [active]");
5837 static int event_hist_trigger_init(struct event_trigger_data *data)
5839 struct hist_trigger_data *hist_data = data->private_data;
5841 if (!data->ref && hist_data->attrs->name)
5842 save_named_trigger(hist_data->attrs->name, data);
5849 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5851 struct trace_event_file *file;
5856 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5857 file = hist_data->field_var_hists[i]->hist_data->event_file;
5858 cmd = hist_data->field_var_hists[i]->cmd;
5859 ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
5860 "!hist", "hist", cmd);
5861 WARN_ON_ONCE(ret < 0);
5865 static void event_hist_trigger_free(struct event_trigger_data *data)
5867 struct hist_trigger_data *hist_data = data->private_data;
5869 if (WARN_ON_ONCE(data->ref <= 0))
5875 del_named_trigger(data);
5877 trigger_data_free(data);
5879 remove_hist_vars(hist_data);
5881 unregister_field_var_hists(hist_data);
5883 destroy_hist_data(hist_data);
5887 static struct event_trigger_ops event_hist_trigger_ops = {
5888 .trigger = event_hist_trigger,
5889 .print = event_hist_trigger_print,
5890 .init = event_hist_trigger_init,
5891 .free = event_hist_trigger_free,
5894 static int event_hist_trigger_named_init(struct event_trigger_data *data)
5898 save_named_trigger(data->named_data->name, data);
5900 event_hist_trigger_init(data->named_data);
5905 static void event_hist_trigger_named_free(struct event_trigger_data *data)
5907 if (WARN_ON_ONCE(data->ref <= 0))
5910 event_hist_trigger_free(data->named_data);
5914 del_named_trigger(data);
5915 trigger_data_free(data);
5919 static struct event_trigger_ops event_hist_trigger_named_ops = {
5920 .trigger = event_hist_trigger,
5921 .print = event_hist_trigger_print,
5922 .init = event_hist_trigger_named_init,
5923 .free = event_hist_trigger_named_free,
5926 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5929 return &event_hist_trigger_ops;
5932 static void hist_clear(struct event_trigger_data *data)
5934 struct hist_trigger_data *hist_data = data->private_data;
5937 pause_named_trigger(data);
5939 tracepoint_synchronize_unregister();
5941 tracing_map_clear(hist_data->map);
5944 unpause_named_trigger(data);
5947 static bool compatible_field(struct ftrace_event_field *field,
5948 struct ftrace_event_field *test_field)
5950 if (field == test_field)
5952 if (field == NULL || test_field == NULL)
5954 if (strcmp(field->name, test_field->name) != 0)
5956 if (strcmp(field->type, test_field->type) != 0)
5958 if (field->size != test_field->size)
5960 if (field->is_signed != test_field->is_signed)
5966 static bool hist_trigger_match(struct event_trigger_data *data,
5967 struct event_trigger_data *data_test,
5968 struct event_trigger_data *named_data,
5971 struct tracing_map_sort_key *sort_key, *sort_key_test;
5972 struct hist_trigger_data *hist_data, *hist_data_test;
5973 struct hist_field *key_field, *key_field_test;
5976 if (named_data && (named_data != data_test) &&
5977 (named_data != data_test->named_data))
5980 if (!named_data && is_named_trigger(data_test))
5983 hist_data = data->private_data;
5984 hist_data_test = data_test->private_data;
5986 if (hist_data->n_vals != hist_data_test->n_vals ||
5987 hist_data->n_fields != hist_data_test->n_fields ||
5988 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5991 if (!ignore_filter) {
5992 if ((data->filter_str && !data_test->filter_str) ||
5993 (!data->filter_str && data_test->filter_str))
5997 for_each_hist_field(i, hist_data) {
5998 key_field = hist_data->fields[i];
5999 key_field_test = hist_data_test->fields[i];
6001 if (key_field->flags != key_field_test->flags)
6003 if (!compatible_field(key_field->field, key_field_test->field))
6005 if (key_field->offset != key_field_test->offset)
6007 if (key_field->size != key_field_test->size)
6009 if (key_field->is_signed != key_field_test->is_signed)
6011 if (!!key_field->var.name != !!key_field_test->var.name)
6013 if (key_field->var.name &&
6014 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6018 for (i = 0; i < hist_data->n_sort_keys; i++) {
6019 sort_key = &hist_data->sort_keys[i];
6020 sort_key_test = &hist_data_test->sort_keys[i];
6022 if (sort_key->field_idx != sort_key_test->field_idx ||
6023 sort_key->descending != sort_key_test->descending)
6027 if (!ignore_filter && data->filter_str &&
6028 (strcmp(data->filter_str, data_test->filter_str) != 0))
6031 if (!actions_match(hist_data, hist_data_test))
6037 static bool existing_hist_update_only(char *glob,
6038 struct event_trigger_data *data,
6039 struct trace_event_file *file)
6041 struct hist_trigger_data *hist_data = data->private_data;
6042 struct event_trigger_data *test, *named_data = NULL;
6043 bool updated = false;
6045 if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
6046 !hist_data->attrs->clear)
6049 if (hist_data->attrs->name) {
6050 named_data = find_named_trigger(hist_data->attrs->name);
6052 if (!hist_trigger_match(data, named_data, named_data,
6058 if (hist_data->attrs->name && !named_data)
6061 list_for_each_entry(test, &file->triggers, list) {
6062 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6063 if (!hist_trigger_match(data, test, named_data, false))
6065 if (hist_data->attrs->pause)
6066 test->paused = true;
6067 else if (hist_data->attrs->cont)
6068 test->paused = false;
6069 else if (hist_data->attrs->clear)
6079 static int hist_register_trigger(char *glob,
6080 struct event_trigger_data *data,
6081 struct trace_event_file *file)
6083 struct hist_trigger_data *hist_data = data->private_data;
6084 struct event_trigger_data *test, *named_data = NULL;
6085 struct trace_array *tr = file->tr;
6088 if (hist_data->attrs->name) {
6089 named_data = find_named_trigger(hist_data->attrs->name);
6091 if (!hist_trigger_match(data, named_data, named_data,
6093 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6100 if (hist_data->attrs->name && !named_data)
6103 lockdep_assert_held(&event_mutex);
6105 list_for_each_entry(test, &file->triggers, list) {
6106 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6107 if (hist_trigger_match(data, test, named_data, false)) {
6108 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6115 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6116 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6121 if (hist_data->attrs->pause)
6122 data->paused = true;
6125 data->private_data = named_data->private_data;
6126 set_named_trigger_data(data, named_data);
6127 data->ops = &event_hist_trigger_named_ops;
6130 if (data->ops->init) {
6131 ret = data->ops->init(data);
6136 if (hist_data->enable_timestamps) {
6137 char *clock = hist_data->attrs->clock;
6139 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6141 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6145 tracing_set_filter_buffering(file->tr, true);
6149 destroy_hist_data(hist_data);
6154 static int hist_trigger_enable(struct event_trigger_data *data,
6155 struct trace_event_file *file)
6159 list_add_tail_rcu(&data->list, &file->triggers);
6161 update_cond_flag(file);
6163 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6164 list_del_rcu(&data->list);
6165 update_cond_flag(file);
6172 static bool have_hist_trigger_match(struct event_trigger_data *data,
6173 struct trace_event_file *file)
6175 struct hist_trigger_data *hist_data = data->private_data;
6176 struct event_trigger_data *test, *named_data = NULL;
6179 lockdep_assert_held(&event_mutex);
6181 if (hist_data->attrs->name)
6182 named_data = find_named_trigger(hist_data->attrs->name);
6184 list_for_each_entry(test, &file->triggers, list) {
6185 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6186 if (hist_trigger_match(data, test, named_data, false)) {
6196 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6197 struct trace_event_file *file)
6199 struct hist_trigger_data *hist_data = data->private_data;
6200 struct event_trigger_data *test, *named_data = NULL;
6202 lockdep_assert_held(&event_mutex);
6204 if (hist_data->attrs->name)
6205 named_data = find_named_trigger(hist_data->attrs->name);
6207 list_for_each_entry(test, &file->triggers, list) {
6208 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6209 if (!hist_trigger_match(data, test, named_data, false))
6211 hist_data = test->private_data;
6212 if (check_var_refs(hist_data))
6221 static void hist_unregister_trigger(char *glob,
6222 struct event_trigger_data *data,
6223 struct trace_event_file *file)
6225 struct event_trigger_data *test = NULL, *iter, *named_data = NULL;
6226 struct hist_trigger_data *hist_data = data->private_data;
6228 lockdep_assert_held(&event_mutex);
6230 if (hist_data->attrs->name)
6231 named_data = find_named_trigger(hist_data->attrs->name);
6233 list_for_each_entry(iter, &file->triggers, list) {
6234 if (iter->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6235 if (!hist_trigger_match(data, iter, named_data, false))
6238 list_del_rcu(&test->list);
6239 trace_event_trigger_enable_disable(file, 0);
6240 update_cond_flag(file);
6245 if (test && test->ops->free)
6246 test->ops->free(test);
6248 if (hist_data->enable_timestamps) {
6249 if (!hist_data->remove || test)
6250 tracing_set_filter_buffering(file->tr, false);
6254 static bool hist_file_check_refs(struct trace_event_file *file)
6256 struct hist_trigger_data *hist_data;
6257 struct event_trigger_data *test;
6259 lockdep_assert_held(&event_mutex);
6261 list_for_each_entry(test, &file->triggers, list) {
6262 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6263 hist_data = test->private_data;
6264 if (check_var_refs(hist_data))
6272 static void hist_unreg_all(struct trace_event_file *file)
6274 struct event_trigger_data *test, *n;
6275 struct hist_trigger_data *hist_data;
6276 struct synth_event *se;
6277 const char *se_name;
6279 lockdep_assert_held(&event_mutex);
6281 if (hist_file_check_refs(file))
6284 list_for_each_entry_safe(test, n, &file->triggers, list) {
6285 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6286 hist_data = test->private_data;
6287 list_del_rcu(&test->list);
6288 trace_event_trigger_enable_disable(file, 0);
6290 se_name = trace_event_name(file->event_call);
6291 se = find_synth_event(se_name);
6295 update_cond_flag(file);
6296 if (hist_data->enable_timestamps)
6297 tracing_set_filter_buffering(file->tr, false);
6298 if (test->ops->free)
6299 test->ops->free(test);
6304 static int event_hist_trigger_parse(struct event_command *cmd_ops,
6305 struct trace_event_file *file,
6306 char *glob, char *cmd,
6307 char *param_and_filter)
6309 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6310 struct event_trigger_data *trigger_data;
6311 struct hist_trigger_attrs *attrs;
6312 struct hist_trigger_data *hist_data;
6313 char *param, *filter, *p, *start;
6314 struct synth_event *se;
6315 const char *se_name;
6319 lockdep_assert_held(&event_mutex);
6326 last_cmd_set(file, param_and_filter);
6329 remove = event_trigger_check_remove(glob);
6331 if (event_trigger_empty_param(param_and_filter))
6335 * separate the trigger from the filter (k:v [if filter])
6336 * allowing for whitespace in the trigger
6338 p = param = param_and_filter;
6340 p = strstr(p, "if");
6343 if (p == param_and_filter)
6345 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6349 if (p >= param_and_filter + strlen(param_and_filter) - (sizeof("if") - 1) - 1)
6351 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6362 filter = strstrip(p);
6363 param = strstrip(param);
6367 * To simplify arithmetic expression parsing, replace occurrences of
6368 * '.sym-offset' modifier with '.symXoffset'
6370 start = strstr(param, ".sym-offset");
6373 start = strstr(start + 11, ".sym-offset");
6376 attrs = parse_hist_trigger_attrs(file->tr, param);
6378 return PTR_ERR(attrs);
6380 if (attrs->map_bits)
6381 hist_trigger_bits = attrs->map_bits;
6383 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6384 if (IS_ERR(hist_data)) {
6385 destroy_hist_trigger_attrs(attrs);
6386 return PTR_ERR(hist_data);
6389 trigger_data = event_trigger_alloc(cmd_ops, cmd, param, hist_data);
6390 if (!trigger_data) {
6395 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
6400 if (!have_hist_trigger_match(trigger_data, file))
6403 if (hist_trigger_check_refs(trigger_data, file)) {
6408 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6409 se_name = trace_event_name(file->event_call);
6410 se = find_synth_event(se_name);
6417 if (existing_hist_update_only(glob, trigger_data, file))
6420 ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
6424 if (get_named_trigger_data(trigger_data))
6427 if (has_hist_vars(hist_data))
6428 save_hist_vars(hist_data);
6430 ret = create_actions(hist_data);
6434 ret = tracing_map_init(hist_data->map);
6438 ret = hist_trigger_enable(trigger_data, file);
6442 se_name = trace_event_name(file->event_call);
6443 se = find_synth_event(se_name);
6447 if (ret == 0 && glob[0])
6452 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6454 event_trigger_reset_filter(cmd_ops, trigger_data);
6456 remove_hist_vars(hist_data);
6458 kfree(trigger_data);
6460 destroy_hist_data(hist_data);
6464 static struct event_command trigger_hist_cmd = {
6466 .trigger_type = ETT_EVENT_HIST,
6467 .flags = EVENT_CMD_FL_NEEDS_REC,
6468 .parse = event_hist_trigger_parse,
6469 .reg = hist_register_trigger,
6470 .unreg = hist_unregister_trigger,
6471 .unreg_all = hist_unreg_all,
6472 .get_trigger_ops = event_hist_get_trigger_ops,
6473 .set_filter = set_trigger_filter,
6476 __init int register_trigger_hist_cmd(void)
6480 ret = register_event_command(&trigger_hist_cmd);
6487 hist_enable_trigger(struct event_trigger_data *data,
6488 struct trace_buffer *buffer, void *rec,
6489 struct ring_buffer_event *event)
6491 struct enable_trigger_data *enable_data = data->private_data;
6492 struct event_trigger_data *test;
6494 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6495 lockdep_is_held(&event_mutex)) {
6496 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6497 if (enable_data->enable)
6498 test->paused = false;
6500 test->paused = true;
6506 hist_enable_count_trigger(struct event_trigger_data *data,
6507 struct trace_buffer *buffer, void *rec,
6508 struct ring_buffer_event *event)
6513 if (data->count != -1)
6516 hist_enable_trigger(data, buffer, rec, event);
6519 static struct event_trigger_ops hist_enable_trigger_ops = {
6520 .trigger = hist_enable_trigger,
6521 .print = event_enable_trigger_print,
6522 .init = event_trigger_init,
6523 .free = event_enable_trigger_free,
6526 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6527 .trigger = hist_enable_count_trigger,
6528 .print = event_enable_trigger_print,
6529 .init = event_trigger_init,
6530 .free = event_enable_trigger_free,
6533 static struct event_trigger_ops hist_disable_trigger_ops = {
6534 .trigger = hist_enable_trigger,
6535 .print = event_enable_trigger_print,
6536 .init = event_trigger_init,
6537 .free = event_enable_trigger_free,
6540 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6541 .trigger = hist_enable_count_trigger,
6542 .print = event_enable_trigger_print,
6543 .init = event_trigger_init,
6544 .free = event_enable_trigger_free,
6547 static struct event_trigger_ops *
6548 hist_enable_get_trigger_ops(char *cmd, char *param)
6550 struct event_trigger_ops *ops;
6553 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6556 ops = param ? &hist_enable_count_trigger_ops :
6557 &hist_enable_trigger_ops;
6559 ops = param ? &hist_disable_count_trigger_ops :
6560 &hist_disable_trigger_ops;
6565 static void hist_enable_unreg_all(struct trace_event_file *file)
6567 struct event_trigger_data *test, *n;
6569 list_for_each_entry_safe(test, n, &file->triggers, list) {
6570 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6571 list_del_rcu(&test->list);
6572 update_cond_flag(file);
6573 trace_event_trigger_enable_disable(file, 0);
6574 if (test->ops->free)
6575 test->ops->free(test);
6580 static struct event_command trigger_hist_enable_cmd = {
6581 .name = ENABLE_HIST_STR,
6582 .trigger_type = ETT_HIST_ENABLE,
6583 .parse = event_enable_trigger_parse,
6584 .reg = event_enable_register_trigger,
6585 .unreg = event_enable_unregister_trigger,
6586 .unreg_all = hist_enable_unreg_all,
6587 .get_trigger_ops = hist_enable_get_trigger_ops,
6588 .set_filter = set_trigger_filter,
6591 static struct event_command trigger_hist_disable_cmd = {
6592 .name = DISABLE_HIST_STR,
6593 .trigger_type = ETT_HIST_ENABLE,
6594 .parse = event_enable_trigger_parse,
6595 .reg = event_enable_register_trigger,
6596 .unreg = event_enable_unregister_trigger,
6597 .unreg_all = hist_enable_unreg_all,
6598 .get_trigger_ops = hist_enable_get_trigger_ops,
6599 .set_filter = set_trigger_filter,
6602 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6604 unregister_event_command(&trigger_hist_enable_cmd);
6605 unregister_event_command(&trigger_hist_disable_cmd);
6608 __init int register_trigger_hist_enable_disable_cmds(void)
6612 ret = register_event_command(&trigger_hist_enable_cmd);
6613 if (WARN_ON(ret < 0))
6615 ret = register_event_command(&trigger_hist_disable_cmd);
6616 if (WARN_ON(ret < 0))
6617 unregister_trigger_hist_enable_disable_cmds();