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/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/stacktrace.h>
13 #include <linux/rculist.h>
14 #include <linux/tracefs.h>
16 /* for gfp flag names */
17 #include <linux/trace_events.h>
18 #include <trace/events/mmflags.h>
20 #include "tracing_map.h"
22 #include "trace_dynevent.h"
24 #define SYNTH_SYSTEM "synthetic"
25 #define SYNTH_FIELDS_MAX 16
27 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
30 C(NONE, "No error"), \
31 C(DUPLICATE_VAR, "Variable already defined"), \
32 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
33 C(TOO_MANY_VARS, "Too many variables defined"), \
34 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
35 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
36 C(TRIGGER_EEXIST, "Hist trigger already exists"), \
37 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
38 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
39 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
40 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
41 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
42 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
43 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
44 C(HIST_NOT_FOUND, "Matching event histogram not found"), \
45 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
46 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
47 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
48 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
49 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
50 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
51 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
52 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
53 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
54 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
55 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
56 C(TOO_MANY_PARAMS, "Too many action params"), \
57 C(PARAM_NOT_FOUND, "Couldn't find param"), \
58 C(INVALID_PARAM, "Invalid action param"), \
59 C(ACTION_NOT_FOUND, "No action found"), \
60 C(NO_SAVE_PARAMS, "No params found for save()"), \
61 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
62 C(ACTION_MISMATCH, "Handler doesn't support action"), \
63 C(NO_CLOSING_PAREN, "No closing paren found"), \
64 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
65 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
66 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
67 C(VAR_NOT_FOUND, "Couldn't find variable"), \
68 C(FIELD_NOT_FOUND, "Couldn't find field"),
71 #define C(a, b) HIST_ERR_##a
78 static const char *err_text[] = { ERRORS };
82 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
83 struct tracing_map_elt *elt,
84 struct ring_buffer_event *rbe,
87 #define HIST_FIELD_OPERANDS_MAX 2
88 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
89 #define HIST_ACTIONS_MAX 8
99 * A hist_var (histogram variable) contains variable information for
100 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
101 * flag set. A hist_var has a variable name e.g. ts0, and is
102 * associated with a given histogram trigger, as specified by
103 * hist_data. The hist_var idx is the unique index assigned to the
104 * variable by the hist trigger's tracing_map. The idx is what is
105 * used to set a variable's value and, by a variable reference, to
110 struct hist_trigger_data *hist_data;
115 struct ftrace_event_field *field;
120 unsigned int is_signed;
122 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
123 struct hist_trigger_data *hist_data;
126 * Variable fields contain variable-specific info in var.
129 enum field_op_id operator;
134 * The name field is used for EXPR and VAR_REF fields. VAR
135 * fields contain the variable name in var.name.
140 * When a histogram trigger is hit, if it has any references
141 * to variables, the values of those variables are collected
142 * into a var_ref_vals array by resolve_var_refs(). The
143 * current value of each variable is read from the tracing_map
144 * using the hist field's hist_var.idx and entered into the
145 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
147 unsigned int var_ref_idx;
151 static u64 hist_field_none(struct hist_field *field,
152 struct tracing_map_elt *elt,
153 struct ring_buffer_event *rbe,
159 static u64 hist_field_counter(struct hist_field *field,
160 struct tracing_map_elt *elt,
161 struct ring_buffer_event *rbe,
167 static u64 hist_field_string(struct hist_field *hist_field,
168 struct tracing_map_elt *elt,
169 struct ring_buffer_event *rbe,
172 char *addr = (char *)(event + hist_field->field->offset);
174 return (u64)(unsigned long)addr;
177 static u64 hist_field_dynstring(struct hist_field *hist_field,
178 struct tracing_map_elt *elt,
179 struct ring_buffer_event *rbe,
182 u32 str_item = *(u32 *)(event + hist_field->field->offset);
183 int str_loc = str_item & 0xffff;
184 char *addr = (char *)(event + str_loc);
186 return (u64)(unsigned long)addr;
189 static u64 hist_field_pstring(struct hist_field *hist_field,
190 struct tracing_map_elt *elt,
191 struct ring_buffer_event *rbe,
194 char **addr = (char **)(event + hist_field->field->offset);
196 return (u64)(unsigned long)*addr;
199 static u64 hist_field_log2(struct hist_field *hist_field,
200 struct tracing_map_elt *elt,
201 struct ring_buffer_event *rbe,
204 struct hist_field *operand = hist_field->operands[0];
206 u64 val = operand->fn(operand, elt, rbe, event);
208 return (u64) ilog2(roundup_pow_of_two(val));
211 static u64 hist_field_plus(struct hist_field *hist_field,
212 struct tracing_map_elt *elt,
213 struct ring_buffer_event *rbe,
216 struct hist_field *operand1 = hist_field->operands[0];
217 struct hist_field *operand2 = hist_field->operands[1];
219 u64 val1 = operand1->fn(operand1, elt, rbe, event);
220 u64 val2 = operand2->fn(operand2, elt, rbe, event);
225 static u64 hist_field_minus(struct hist_field *hist_field,
226 struct tracing_map_elt *elt,
227 struct ring_buffer_event *rbe,
230 struct hist_field *operand1 = hist_field->operands[0];
231 struct hist_field *operand2 = hist_field->operands[1];
233 u64 val1 = operand1->fn(operand1, elt, rbe, event);
234 u64 val2 = operand2->fn(operand2, elt, rbe, event);
239 static u64 hist_field_unary_minus(struct hist_field *hist_field,
240 struct tracing_map_elt *elt,
241 struct ring_buffer_event *rbe,
244 struct hist_field *operand = hist_field->operands[0];
246 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
247 u64 val = (u64)-sval;
252 #define DEFINE_HIST_FIELD_FN(type) \
253 static u64 hist_field_##type(struct hist_field *hist_field, \
254 struct tracing_map_elt *elt, \
255 struct ring_buffer_event *rbe, \
258 type *addr = (type *)(event + hist_field->field->offset); \
260 return (u64)(unsigned long)*addr; \
263 DEFINE_HIST_FIELD_FN(s64);
264 DEFINE_HIST_FIELD_FN(u64);
265 DEFINE_HIST_FIELD_FN(s32);
266 DEFINE_HIST_FIELD_FN(u32);
267 DEFINE_HIST_FIELD_FN(s16);
268 DEFINE_HIST_FIELD_FN(u16);
269 DEFINE_HIST_FIELD_FN(s8);
270 DEFINE_HIST_FIELD_FN(u8);
272 #define for_each_hist_field(i, hist_data) \
273 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
275 #define for_each_hist_val_field(i, hist_data) \
276 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
278 #define for_each_hist_key_field(i, hist_data) \
279 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
281 #define HIST_STACKTRACE_DEPTH 16
282 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
283 #define HIST_STACKTRACE_SKIP 5
285 #define HITCOUNT_IDX 0
286 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
288 enum hist_field_flags {
289 HIST_FIELD_FL_HITCOUNT = 1 << 0,
290 HIST_FIELD_FL_KEY = 1 << 1,
291 HIST_FIELD_FL_STRING = 1 << 2,
292 HIST_FIELD_FL_HEX = 1 << 3,
293 HIST_FIELD_FL_SYM = 1 << 4,
294 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
295 HIST_FIELD_FL_EXECNAME = 1 << 6,
296 HIST_FIELD_FL_SYSCALL = 1 << 7,
297 HIST_FIELD_FL_STACKTRACE = 1 << 8,
298 HIST_FIELD_FL_LOG2 = 1 << 9,
299 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
300 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
301 HIST_FIELD_FL_VAR = 1 << 12,
302 HIST_FIELD_FL_EXPR = 1 << 13,
303 HIST_FIELD_FL_VAR_REF = 1 << 14,
304 HIST_FIELD_FL_CPU = 1 << 15,
305 HIST_FIELD_FL_ALIAS = 1 << 16,
310 char *name[TRACING_MAP_VARS_MAX];
311 char *expr[TRACING_MAP_VARS_MAX];
314 struct hist_trigger_attrs {
324 unsigned int map_bits;
326 char *assignment_str[TRACING_MAP_VARS_MAX];
327 unsigned int n_assignments;
329 char *action_str[HIST_ACTIONS_MAX];
330 unsigned int n_actions;
332 struct var_defs var_defs;
336 struct hist_field *var;
337 struct hist_field *val;
340 struct field_var_hist {
341 struct hist_trigger_data *hist_data;
345 struct hist_trigger_data {
346 struct hist_field *fields[HIST_FIELDS_MAX];
349 unsigned int n_fields;
351 unsigned int key_size;
352 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
353 unsigned int n_sort_keys;
354 struct trace_event_file *event_file;
355 struct hist_trigger_attrs *attrs;
356 struct tracing_map *map;
357 bool enable_timestamps;
359 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
360 unsigned int n_var_refs;
362 struct action_data *actions[HIST_ACTIONS_MAX];
363 unsigned int n_actions;
365 struct field_var *field_vars[SYNTH_FIELDS_MAX];
366 unsigned int n_field_vars;
367 unsigned int n_field_var_str;
368 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
369 unsigned int n_field_var_hists;
371 struct field_var *save_vars[SYNTH_FIELDS_MAX];
372 unsigned int n_save_vars;
373 unsigned int n_save_var_str;
376 static int synth_event_create(int argc, const char **argv);
377 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
378 static int synth_event_release(struct dyn_event *ev);
379 static bool synth_event_is_busy(struct dyn_event *ev);
380 static bool synth_event_match(const char *system, const char *event,
381 int argc, const char **argv, struct dyn_event *ev);
383 static struct dyn_event_operations synth_event_ops = {
384 .create = synth_event_create,
385 .show = synth_event_show,
386 .is_busy = synth_event_is_busy,
387 .free = synth_event_release,
388 .match = synth_event_match,
400 struct dyn_event devent;
403 struct synth_field **fields;
404 unsigned int n_fields;
406 struct trace_event_class class;
407 struct trace_event_call call;
408 struct tracepoint *tp;
411 static bool is_synth_event(struct dyn_event *ev)
413 return ev->ops == &synth_event_ops;
416 static struct synth_event *to_synth_event(struct dyn_event *ev)
418 return container_of(ev, struct synth_event, devent);
421 static bool synth_event_is_busy(struct dyn_event *ev)
423 struct synth_event *event = to_synth_event(ev);
425 return event->ref != 0;
428 static bool synth_event_match(const char *system, const char *event,
429 int argc, const char **argv, struct dyn_event *ev)
431 struct synth_event *sev = to_synth_event(ev);
433 return strcmp(sev->name, event) == 0 &&
434 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
439 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
440 struct tracing_map_elt *elt, void *rec,
441 struct ring_buffer_event *rbe, void *key,
442 struct action_data *data, u64 *var_ref_vals);
444 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
459 enum handler_id handler;
460 enum action_id action;
464 unsigned int n_params;
465 char *params[SYNTH_FIELDS_MAX];
468 * When a histogram trigger is hit, the values of any
469 * references to variables, including variables being passed
470 * as parameters to synthetic events, are collected into a
471 * var_ref_vals array. This var_ref_idx is the index of the
472 * first param in the array to be passed to the synthetic
475 unsigned int var_ref_idx;
476 struct synth_event *synth_event;
477 bool use_trace_keyword;
478 char *synth_event_name;
488 * var_str contains the $-unstripped variable
489 * name referenced by var_ref, and used when
490 * printing the action. Because var_ref
491 * creation is deferred to create_actions(),
492 * we need a per-action way to save it until
493 * then, thus var_str.
498 * var_ref refers to the variable being
499 * tracked e.g onmax($var).
501 struct hist_field *var_ref;
504 * track_var contains the 'invisible' tracking
505 * variable created to keep the current
508 struct hist_field *track_var;
510 check_track_val_fn_t check_val;
511 action_fn_t save_data;
520 unsigned int key_len;
522 struct tracing_map_elt elt;
524 struct action_data *action_data;
525 struct hist_trigger_data *hist_data;
528 struct hist_elt_data {
531 char *field_var_str[SYNTH_FIELDS_MAX];
534 struct snapshot_context {
535 struct tracing_map_elt *elt;
539 static void track_data_free(struct track_data *track_data)
541 struct hist_elt_data *elt_data;
546 kfree(track_data->key);
548 elt_data = track_data->elt.private_data;
550 kfree(elt_data->comm);
557 static struct track_data *track_data_alloc(unsigned int key_len,
558 struct action_data *action_data,
559 struct hist_trigger_data *hist_data)
561 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
562 struct hist_elt_data *elt_data;
565 return ERR_PTR(-ENOMEM);
567 data->key = kzalloc(key_len, GFP_KERNEL);
569 track_data_free(data);
570 return ERR_PTR(-ENOMEM);
573 data->key_len = key_len;
574 data->action_data = action_data;
575 data->hist_data = hist_data;
577 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
579 track_data_free(data);
580 return ERR_PTR(-ENOMEM);
582 data->elt.private_data = elt_data;
584 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
585 if (!elt_data->comm) {
586 track_data_free(data);
587 return ERR_PTR(-ENOMEM);
593 static char last_cmd[MAX_FILTER_STR_VAL];
594 static char last_cmd_loc[MAX_FILTER_STR_VAL];
596 static int errpos(char *str)
598 return err_pos(last_cmd, str);
601 static void last_cmd_set(struct trace_event_file *file, char *str)
603 const char *system = NULL, *name = NULL;
604 struct trace_event_call *call;
609 strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1);
612 call = file->event_call;
614 system = call->class->system;
616 name = trace_event_name(call);
623 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
626 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
628 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
632 static void hist_err_clear(void)
635 last_cmd_loc[0] = '\0';
638 struct synth_trace_event {
639 struct trace_entry ent;
643 static int synth_event_define_fields(struct trace_event_call *call)
645 struct synth_trace_event trace;
646 int offset = offsetof(typeof(trace), fields);
647 struct synth_event *event = call->data;
648 unsigned int i, size, n_u64;
653 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
654 size = event->fields[i]->size;
655 is_signed = event->fields[i]->is_signed;
656 type = event->fields[i]->type;
657 name = event->fields[i]->name;
658 ret = trace_define_field(call, type, name, offset, size,
659 is_signed, FILTER_OTHER);
663 if (event->fields[i]->is_string) {
664 offset += STR_VAR_LEN_MAX;
665 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
667 offset += sizeof(u64);
672 event->n_u64 = n_u64;
677 static bool synth_field_signed(char *type)
679 if (str_has_prefix(type, "u"))
685 static int synth_field_is_string(char *type)
687 if (strstr(type, "char[") != NULL)
693 static int synth_field_string_size(char *type)
695 char buf[4], *end, *start;
699 start = strstr(type, "char[");
702 start += sizeof("char[") - 1;
704 end = strchr(type, ']');
705 if (!end || end < start)
712 strncpy(buf, start, len);
715 err = kstrtouint(buf, 0, &size);
719 if (size > STR_VAR_LEN_MAX)
725 static int synth_field_size(char *type)
729 if (strcmp(type, "s64") == 0)
731 else if (strcmp(type, "u64") == 0)
733 else if (strcmp(type, "s32") == 0)
735 else if (strcmp(type, "u32") == 0)
737 else if (strcmp(type, "s16") == 0)
739 else if (strcmp(type, "u16") == 0)
741 else if (strcmp(type, "s8") == 0)
743 else if (strcmp(type, "u8") == 0)
745 else if (strcmp(type, "char") == 0)
747 else if (strcmp(type, "unsigned char") == 0)
748 size = sizeof(unsigned char);
749 else if (strcmp(type, "int") == 0)
751 else if (strcmp(type, "unsigned int") == 0)
752 size = sizeof(unsigned int);
753 else if (strcmp(type, "long") == 0)
755 else if (strcmp(type, "unsigned long") == 0)
756 size = sizeof(unsigned long);
757 else if (strcmp(type, "pid_t") == 0)
758 size = sizeof(pid_t);
759 else if (strcmp(type, "gfp_t") == 0)
760 size = sizeof(gfp_t);
761 else if (synth_field_is_string(type))
762 size = synth_field_string_size(type);
767 static const char *synth_field_fmt(char *type)
769 const char *fmt = "%llu";
771 if (strcmp(type, "s64") == 0)
773 else if (strcmp(type, "u64") == 0)
775 else if (strcmp(type, "s32") == 0)
777 else if (strcmp(type, "u32") == 0)
779 else if (strcmp(type, "s16") == 0)
781 else if (strcmp(type, "u16") == 0)
783 else if (strcmp(type, "s8") == 0)
785 else if (strcmp(type, "u8") == 0)
787 else if (strcmp(type, "char") == 0)
789 else if (strcmp(type, "unsigned char") == 0)
791 else if (strcmp(type, "int") == 0)
793 else if (strcmp(type, "unsigned int") == 0)
795 else if (strcmp(type, "long") == 0)
797 else if (strcmp(type, "unsigned long") == 0)
799 else if (strcmp(type, "pid_t") == 0)
801 else if (strcmp(type, "gfp_t") == 0)
803 else if (synth_field_is_string(type))
809 static enum print_line_t print_synth_event(struct trace_iterator *iter,
811 struct trace_event *event)
813 struct trace_array *tr = iter->tr;
814 struct trace_seq *s = &iter->seq;
815 struct synth_trace_event *entry;
816 struct synth_event *se;
817 unsigned int i, n_u64;
821 entry = (struct synth_trace_event *)iter->ent;
822 se = container_of(event, struct synth_event, call.event);
824 trace_seq_printf(s, "%s: ", se->name);
826 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
827 if (trace_seq_has_overflowed(s))
830 fmt = synth_field_fmt(se->fields[i]->type);
832 /* parameter types */
833 if (tr->trace_flags & TRACE_ITER_VERBOSE)
834 trace_seq_printf(s, "%s ", fmt);
836 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
838 /* parameter values */
839 if (se->fields[i]->is_string) {
840 trace_seq_printf(s, print_fmt, se->fields[i]->name,
841 (char *)&entry->fields[n_u64],
842 i == se->n_fields - 1 ? "" : " ");
843 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
845 struct trace_print_flags __flags[] = {
846 __def_gfpflag_names, {-1, NULL} };
848 trace_seq_printf(s, print_fmt, se->fields[i]->name,
849 entry->fields[n_u64],
850 i == se->n_fields - 1 ? "" : " ");
852 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
853 trace_seq_puts(s, " (");
854 trace_print_flags_seq(s, "|",
855 entry->fields[n_u64],
857 trace_seq_putc(s, ')');
863 trace_seq_putc(s, '\n');
865 return trace_handle_return(s);
868 static struct trace_event_functions synth_event_funcs = {
869 .trace = print_synth_event
872 static notrace void trace_event_raw_event_synth(void *__data,
874 unsigned int var_ref_idx)
876 struct trace_event_file *trace_file = __data;
877 struct synth_trace_event *entry;
878 struct trace_event_buffer fbuffer;
879 struct ring_buffer *buffer;
880 struct synth_event *event;
881 unsigned int i, n_u64;
884 event = trace_file->event_call->data;
886 if (trace_trigger_soft_disabled(trace_file))
889 fields_size = event->n_u64 * sizeof(u64);
892 * Avoid ring buffer recursion detection, as this event
893 * is being performed within another event.
895 buffer = trace_file->tr->trace_buffer.buffer;
896 ring_buffer_nest_start(buffer);
898 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
899 sizeof(*entry) + fields_size);
903 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
904 if (event->fields[i]->is_string) {
905 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
906 char *str_field = (char *)&entry->fields[n_u64];
908 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
909 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
911 entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
916 trace_event_buffer_commit(&fbuffer);
918 ring_buffer_nest_end(buffer);
921 static void free_synth_event_print_fmt(struct trace_event_call *call)
924 kfree(call->print_fmt);
925 call->print_fmt = NULL;
929 static int __set_synth_event_print_fmt(struct synth_event *event,
936 /* When len=0, we just calculate the needed length */
937 #define LEN_OR_ZERO (len ? len - pos : 0)
939 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
940 for (i = 0; i < event->n_fields; i++) {
941 fmt = synth_field_fmt(event->fields[i]->type);
942 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
943 event->fields[i]->name, fmt,
944 i == event->n_fields - 1 ? "" : ", ");
946 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
948 for (i = 0; i < event->n_fields; i++) {
949 pos += snprintf(buf + pos, LEN_OR_ZERO,
950 ", REC->%s", event->fields[i]->name);
955 /* return the length of print_fmt */
959 static int set_synth_event_print_fmt(struct trace_event_call *call)
961 struct synth_event *event = call->data;
965 /* First: called with 0 length to calculate the needed length */
966 len = __set_synth_event_print_fmt(event, NULL, 0);
968 print_fmt = kmalloc(len + 1, GFP_KERNEL);
972 /* Second: actually write the @print_fmt */
973 __set_synth_event_print_fmt(event, print_fmt, len + 1);
974 call->print_fmt = print_fmt;
979 static void free_synth_field(struct synth_field *field)
986 static struct synth_field *parse_synth_field(int argc, const char **argv,
989 struct synth_field *field;
990 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
993 if (field_type[0] == ';')
996 if (!strcmp(field_type, "unsigned")) {
998 return ERR_PTR(-EINVAL);
999 prefix = "unsigned ";
1000 field_type = argv[1];
1001 field_name = argv[2];
1004 field_name = argv[1];
1008 field = kzalloc(sizeof(*field), GFP_KERNEL);
1010 return ERR_PTR(-ENOMEM);
1012 len = strlen(field_name);
1013 array = strchr(field_name, '[');
1015 len -= strlen(array);
1016 else if (field_name[len - 1] == ';')
1019 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1025 if (field_type[0] == ';')
1027 len = strlen(field_type) + 1;
1029 len += strlen(array);
1031 len += strlen(prefix);
1033 field->type = kzalloc(len, GFP_KERNEL);
1039 strcat(field->type, prefix);
1040 strcat(field->type, field_type);
1042 strcat(field->type, array);
1043 if (field->type[len - 1] == ';')
1044 field->type[len - 1] = '\0';
1047 field->size = synth_field_size(field->type);
1053 if (synth_field_is_string(field->type))
1054 field->is_string = true;
1056 field->is_signed = synth_field_signed(field->type);
1061 free_synth_field(field);
1062 field = ERR_PTR(ret);
1066 static void free_synth_tracepoint(struct tracepoint *tp)
1075 static struct tracepoint *alloc_synth_tracepoint(char *name)
1077 struct tracepoint *tp;
1079 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1081 return ERR_PTR(-ENOMEM);
1083 tp->name = kstrdup(name, GFP_KERNEL);
1086 return ERR_PTR(-ENOMEM);
1092 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1093 unsigned int var_ref_idx);
1095 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1096 unsigned int var_ref_idx)
1098 struct tracepoint *tp = event->tp;
1100 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1101 struct tracepoint_func *probe_func_ptr;
1102 synth_probe_func_t probe_func;
1105 if (!(cpu_online(raw_smp_processor_id())))
1108 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1109 if (probe_func_ptr) {
1111 probe_func = probe_func_ptr->func;
1112 __data = probe_func_ptr->data;
1113 probe_func(__data, var_ref_vals, var_ref_idx);
1114 } while ((++probe_func_ptr)->func);
1119 static struct synth_event *find_synth_event(const char *name)
1121 struct dyn_event *pos;
1122 struct synth_event *event;
1124 for_each_dyn_event(pos) {
1125 if (!is_synth_event(pos))
1127 event = to_synth_event(pos);
1128 if (strcmp(event->name, name) == 0)
1135 static int register_synth_event(struct synth_event *event)
1137 struct trace_event_call *call = &event->call;
1140 event->call.class = &event->class;
1141 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1142 if (!event->class.system) {
1147 event->tp = alloc_synth_tracepoint(event->name);
1148 if (IS_ERR(event->tp)) {
1149 ret = PTR_ERR(event->tp);
1154 INIT_LIST_HEAD(&call->class->fields);
1155 call->event.funcs = &synth_event_funcs;
1156 call->class->define_fields = synth_event_define_fields;
1158 ret = register_trace_event(&call->event);
1163 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1164 call->class->reg = trace_event_reg;
1165 call->class->probe = trace_event_raw_event_synth;
1167 call->tp = event->tp;
1169 ret = trace_add_event_call(call);
1171 pr_warn("Failed to register synthetic event: %s\n",
1172 trace_event_name(call));
1176 ret = set_synth_event_print_fmt(call);
1178 trace_remove_event_call(call);
1184 unregister_trace_event(&call->event);
1188 static int unregister_synth_event(struct synth_event *event)
1190 struct trace_event_call *call = &event->call;
1193 ret = trace_remove_event_call(call);
1198 static void free_synth_event(struct synth_event *event)
1205 for (i = 0; i < event->n_fields; i++)
1206 free_synth_field(event->fields[i]);
1208 kfree(event->fields);
1210 kfree(event->class.system);
1211 free_synth_tracepoint(event->tp);
1212 free_synth_event_print_fmt(&event->call);
1216 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1217 struct synth_field **fields)
1219 struct synth_event *event;
1222 event = kzalloc(sizeof(*event), GFP_KERNEL);
1224 event = ERR_PTR(-ENOMEM);
1228 event->name = kstrdup(name, GFP_KERNEL);
1231 event = ERR_PTR(-ENOMEM);
1235 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1236 if (!event->fields) {
1237 free_synth_event(event);
1238 event = ERR_PTR(-ENOMEM);
1242 dyn_event_init(&event->devent, &synth_event_ops);
1244 for (i = 0; i < n_fields; i++)
1245 event->fields[i] = fields[i];
1247 event->n_fields = n_fields;
1252 static void action_trace(struct hist_trigger_data *hist_data,
1253 struct tracing_map_elt *elt, void *rec,
1254 struct ring_buffer_event *rbe, void *key,
1255 struct action_data *data, u64 *var_ref_vals)
1257 struct synth_event *event = data->synth_event;
1259 trace_synth(event, var_ref_vals, data->var_ref_idx);
1262 struct hist_var_data {
1263 struct list_head list;
1264 struct hist_trigger_data *hist_data;
1267 static int __create_synth_event(int argc, const char *name, const char **argv)
1269 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1270 struct synth_event *event = NULL;
1271 int i, consumed = 0, n_fields = 0, ret = 0;
1275 * - Add synthetic event: <event_name> field[;field] ...
1276 * - Remove synthetic event: !<event_name> field[;field] ...
1277 * where 'field' = type field_name
1280 if (name[0] == '\0' || argc < 1)
1283 mutex_lock(&event_mutex);
1285 event = find_synth_event(name);
1291 for (i = 0; i < argc - 1; i++) {
1292 if (strcmp(argv[i], ";") == 0)
1294 if (n_fields == SYNTH_FIELDS_MAX) {
1299 field = parse_synth_field(argc - i, &argv[i], &consumed);
1300 if (IS_ERR(field)) {
1301 ret = PTR_ERR(field);
1304 fields[n_fields++] = field;
1308 if (i < argc && strcmp(argv[i], ";") != 0) {
1313 event = alloc_synth_event(name, n_fields, fields);
1314 if (IS_ERR(event)) {
1315 ret = PTR_ERR(event);
1319 ret = register_synth_event(event);
1321 dyn_event_add(&event->devent);
1323 free_synth_event(event);
1325 mutex_unlock(&event_mutex);
1329 for (i = 0; i < n_fields; i++)
1330 free_synth_field(fields[i]);
1335 static int create_or_delete_synth_event(int argc, char **argv)
1337 const char *name = argv[0];
1338 struct synth_event *event = NULL;
1341 /* trace_run_command() ensures argc != 0 */
1342 if (name[0] == '!') {
1343 mutex_lock(&event_mutex);
1344 event = find_synth_event(name + 1);
1349 ret = unregister_synth_event(event);
1351 dyn_event_remove(&event->devent);
1352 free_synth_event(event);
1357 mutex_unlock(&event_mutex);
1361 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1362 return ret == -ECANCELED ? -EINVAL : ret;
1365 static int synth_event_create(int argc, const char **argv)
1367 const char *name = argv[0];
1370 if (name[0] != 's' || name[1] != ':')
1374 /* This interface accepts group name prefix */
1375 if (strchr(name, '/')) {
1376 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1381 return __create_synth_event(argc - 1, name, argv + 1);
1384 static int synth_event_release(struct dyn_event *ev)
1386 struct synth_event *event = to_synth_event(ev);
1392 ret = unregister_synth_event(event);
1396 dyn_event_remove(ev);
1397 free_synth_event(event);
1401 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1403 struct synth_field *field;
1406 seq_printf(m, "%s\t", event->name);
1408 for (i = 0; i < event->n_fields; i++) {
1409 field = event->fields[i];
1411 /* parameter values */
1412 seq_printf(m, "%s %s%s", field->type, field->name,
1413 i == event->n_fields - 1 ? "" : "; ");
1421 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1423 struct synth_event *event = to_synth_event(ev);
1425 seq_printf(m, "s:%s/", event->class.system);
1427 return __synth_event_show(m, event);
1430 static int synth_events_seq_show(struct seq_file *m, void *v)
1432 struct dyn_event *ev = v;
1434 if (!is_synth_event(ev))
1437 return __synth_event_show(m, to_synth_event(ev));
1440 static const struct seq_operations synth_events_seq_op = {
1441 .start = dyn_event_seq_start,
1442 .next = dyn_event_seq_next,
1443 .stop = dyn_event_seq_stop,
1444 .show = synth_events_seq_show,
1447 static int synth_events_open(struct inode *inode, struct file *file)
1451 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1452 ret = dyn_events_release_all(&synth_event_ops);
1457 return seq_open(file, &synth_events_seq_op);
1460 static ssize_t synth_events_write(struct file *file,
1461 const char __user *buffer,
1462 size_t count, loff_t *ppos)
1464 return trace_parse_run_command(file, buffer, count, ppos,
1465 create_or_delete_synth_event);
1468 static const struct file_operations synth_events_fops = {
1469 .open = synth_events_open,
1470 .write = synth_events_write,
1472 .llseek = seq_lseek,
1473 .release = seq_release,
1476 static u64 hist_field_timestamp(struct hist_field *hist_field,
1477 struct tracing_map_elt *elt,
1478 struct ring_buffer_event *rbe,
1481 struct hist_trigger_data *hist_data = hist_field->hist_data;
1482 struct trace_array *tr = hist_data->event_file->tr;
1484 u64 ts = ring_buffer_event_time_stamp(rbe);
1486 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1492 static u64 hist_field_cpu(struct hist_field *hist_field,
1493 struct tracing_map_elt *elt,
1494 struct ring_buffer_event *rbe,
1497 int cpu = smp_processor_id();
1503 * check_field_for_var_ref - Check if a VAR_REF field references a variable
1504 * @hist_field: The VAR_REF field to check
1505 * @var_data: The hist trigger that owns the variable
1506 * @var_idx: The trigger variable identifier
1508 * Check the given VAR_REF field to see whether or not it references
1509 * the given variable associated with the given trigger.
1511 * Return: The VAR_REF field if it does reference the variable, NULL if not
1513 static struct hist_field *
1514 check_field_for_var_ref(struct hist_field *hist_field,
1515 struct hist_trigger_data *var_data,
1516 unsigned int var_idx)
1518 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
1520 if (hist_field && hist_field->var.idx == var_idx &&
1521 hist_field->var.hist_data == var_data)
1528 * find_var_ref - Check if a trigger has a reference to a trigger variable
1529 * @hist_data: The hist trigger that might have a reference to the variable
1530 * @var_data: The hist trigger that owns the variable
1531 * @var_idx: The trigger variable identifier
1533 * Check the list of var_refs[] on the first hist trigger to see
1534 * whether any of them are references to the variable on the second
1537 * Return: The VAR_REF field referencing the variable if so, NULL if not
1539 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1540 struct hist_trigger_data *var_data,
1541 unsigned int var_idx)
1543 struct hist_field *hist_field;
1546 for (i = 0; i < hist_data->n_var_refs; i++) {
1547 hist_field = hist_data->var_refs[i];
1548 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1556 * find_any_var_ref - Check if there is a reference to a given trigger variable
1557 * @hist_data: The hist trigger
1558 * @var_idx: The trigger variable identifier
1560 * Check to see whether the given variable is currently referenced by
1561 * any other trigger.
1563 * The trigger the variable is defined on is explicitly excluded - the
1564 * assumption being that a self-reference doesn't prevent a trigger
1565 * from being removed.
1567 * Return: The VAR_REF field referencing the variable if so, NULL if not
1569 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1570 unsigned int var_idx)
1572 struct trace_array *tr = hist_data->event_file->tr;
1573 struct hist_field *found = NULL;
1574 struct hist_var_data *var_data;
1576 list_for_each_entry(var_data, &tr->hist_vars, list) {
1577 if (var_data->hist_data == hist_data)
1579 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1588 * check_var_refs - Check if there is a reference to any of trigger's variables
1589 * @hist_data: The hist trigger
1591 * A trigger can define one or more variables. If any one of them is
1592 * currently referenced by any other trigger, this function will
1595 * Typically used to determine whether or not a trigger can be removed
1596 * - if there are any references to a trigger's variables, it cannot.
1598 * Return: True if there is a reference to any of trigger's variables
1600 static bool check_var_refs(struct hist_trigger_data *hist_data)
1602 struct hist_field *field;
1606 for_each_hist_field(i, hist_data) {
1607 field = hist_data->fields[i];
1608 if (field && field->flags & HIST_FIELD_FL_VAR) {
1609 if (find_any_var_ref(hist_data, field->var.idx)) {
1619 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1621 struct trace_array *tr = hist_data->event_file->tr;
1622 struct hist_var_data *var_data, *found = NULL;
1624 list_for_each_entry(var_data, &tr->hist_vars, list) {
1625 if (var_data->hist_data == hist_data) {
1634 static bool field_has_hist_vars(struct hist_field *hist_field,
1645 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1646 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1649 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1650 struct hist_field *operand;
1652 operand = hist_field->operands[i];
1653 if (field_has_hist_vars(operand, level + 1))
1660 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1662 struct hist_field *hist_field;
1665 for_each_hist_field(i, hist_data) {
1666 hist_field = hist_data->fields[i];
1667 if (field_has_hist_vars(hist_field, 0))
1674 static int save_hist_vars(struct hist_trigger_data *hist_data)
1676 struct trace_array *tr = hist_data->event_file->tr;
1677 struct hist_var_data *var_data;
1679 var_data = find_hist_vars(hist_data);
1683 if (trace_array_get(tr) < 0)
1686 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1688 trace_array_put(tr);
1692 var_data->hist_data = hist_data;
1693 list_add(&var_data->list, &tr->hist_vars);
1698 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1700 struct trace_array *tr = hist_data->event_file->tr;
1701 struct hist_var_data *var_data;
1703 var_data = find_hist_vars(hist_data);
1707 if (WARN_ON(check_var_refs(hist_data)))
1710 list_del(&var_data->list);
1714 trace_array_put(tr);
1717 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1718 const char *var_name)
1720 struct hist_field *hist_field, *found = NULL;
1723 for_each_hist_field(i, hist_data) {
1724 hist_field = hist_data->fields[i];
1725 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1726 strcmp(hist_field->var.name, var_name) == 0) {
1735 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1736 struct trace_event_file *file,
1737 const char *var_name)
1739 struct hist_trigger_data *test_data;
1740 struct event_trigger_data *test;
1741 struct hist_field *hist_field;
1743 hist_field = find_var_field(hist_data, var_name);
1747 list_for_each_entry_rcu(test, &file->triggers, list) {
1748 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1749 test_data = test->private_data;
1750 hist_field = find_var_field(test_data, var_name);
1759 static struct trace_event_file *find_var_file(struct trace_array *tr,
1764 struct hist_trigger_data *var_hist_data;
1765 struct hist_var_data *var_data;
1766 struct trace_event_file *file, *found = NULL;
1769 return find_event_file(tr, system, event_name);
1771 list_for_each_entry(var_data, &tr->hist_vars, list) {
1772 var_hist_data = var_data->hist_data;
1773 file = var_hist_data->event_file;
1777 if (find_var_field(var_hist_data, var_name)) {
1779 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1790 static struct hist_field *find_file_var(struct trace_event_file *file,
1791 const char *var_name)
1793 struct hist_trigger_data *test_data;
1794 struct event_trigger_data *test;
1795 struct hist_field *hist_field;
1797 list_for_each_entry_rcu(test, &file->triggers, list) {
1798 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1799 test_data = test->private_data;
1800 hist_field = find_var_field(test_data, var_name);
1809 static struct hist_field *
1810 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1812 struct trace_array *tr = hist_data->event_file->tr;
1813 struct hist_field *hist_field, *found = NULL;
1814 struct trace_event_file *file;
1817 for (i = 0; i < hist_data->n_actions; i++) {
1818 struct action_data *data = hist_data->actions[i];
1820 if (data->handler == HANDLER_ONMATCH) {
1821 char *system = data->match_data.event_system;
1822 char *event_name = data->match_data.event;
1824 file = find_var_file(tr, system, event_name, var_name);
1827 hist_field = find_file_var(file, var_name);
1830 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1832 return ERR_PTR(-EINVAL);
1842 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1847 struct trace_array *tr = hist_data->event_file->tr;
1848 struct hist_field *hist_field = NULL;
1849 struct trace_event_file *file;
1851 if (!system || !event_name) {
1852 hist_field = find_match_var(hist_data, var_name);
1853 if (IS_ERR(hist_field))
1859 file = find_var_file(tr, system, event_name, var_name);
1863 hist_field = find_file_var(file, var_name);
1868 static u64 hist_field_var_ref(struct hist_field *hist_field,
1869 struct tracing_map_elt *elt,
1870 struct ring_buffer_event *rbe,
1873 struct hist_elt_data *elt_data;
1876 if (WARN_ON_ONCE(!elt))
1879 elt_data = elt->private_data;
1880 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1885 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1886 u64 *var_ref_vals, bool self)
1888 struct hist_trigger_data *var_data;
1889 struct tracing_map_elt *var_elt;
1890 struct hist_field *hist_field;
1891 unsigned int i, var_idx;
1892 bool resolved = true;
1895 for (i = 0; i < hist_data->n_var_refs; i++) {
1896 hist_field = hist_data->var_refs[i];
1897 var_idx = hist_field->var.idx;
1898 var_data = hist_field->var.hist_data;
1900 if (var_data == NULL) {
1905 if ((self && var_data != hist_data) ||
1906 (!self && var_data == hist_data))
1909 var_elt = tracing_map_lookup(var_data->map, key);
1915 if (!tracing_map_var_set(var_elt, var_idx)) {
1920 if (self || !hist_field->read_once)
1921 var_val = tracing_map_read_var(var_elt, var_idx);
1923 var_val = tracing_map_read_var_once(var_elt, var_idx);
1925 var_ref_vals[i] = var_val;
1931 static const char *hist_field_name(struct hist_field *field,
1934 const char *field_name = "";
1940 field_name = field->field->name;
1941 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1942 field->flags & HIST_FIELD_FL_ALIAS)
1943 field_name = hist_field_name(field->operands[0], ++level);
1944 else if (field->flags & HIST_FIELD_FL_CPU)
1946 else if (field->flags & HIST_FIELD_FL_EXPR ||
1947 field->flags & HIST_FIELD_FL_VAR_REF) {
1948 if (field->system) {
1949 static char full_name[MAX_FILTER_STR_VAL];
1951 strcat(full_name, field->system);
1952 strcat(full_name, ".");
1953 strcat(full_name, field->event_name);
1954 strcat(full_name, ".");
1955 strcat(full_name, field->name);
1956 field_name = full_name;
1958 field_name = field->name;
1959 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1960 field_name = "common_timestamp";
1962 if (field_name == NULL)
1968 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1970 hist_field_fn_t fn = NULL;
1972 switch (field_size) {
1974 if (field_is_signed)
1975 fn = hist_field_s64;
1977 fn = hist_field_u64;
1980 if (field_is_signed)
1981 fn = hist_field_s32;
1983 fn = hist_field_u32;
1986 if (field_is_signed)
1987 fn = hist_field_s16;
1989 fn = hist_field_u16;
1992 if (field_is_signed)
2002 static int parse_map_size(char *str)
2004 unsigned long size, map_bits;
2013 ret = kstrtoul(str, 0, &size);
2017 map_bits = ilog2(roundup_pow_of_two(size));
2018 if (map_bits < TRACING_MAP_BITS_MIN ||
2019 map_bits > TRACING_MAP_BITS_MAX)
2027 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2034 for (i = 0; i < attrs->n_assignments; i++)
2035 kfree(attrs->assignment_str[i]);
2037 for (i = 0; i < attrs->n_actions; i++)
2038 kfree(attrs->action_str[i]);
2041 kfree(attrs->sort_key_str);
2042 kfree(attrs->keys_str);
2043 kfree(attrs->vals_str);
2044 kfree(attrs->clock);
2048 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2052 if (attrs->n_actions >= HIST_ACTIONS_MAX)
2055 if ((str_has_prefix(str, "onmatch(")) ||
2056 (str_has_prefix(str, "onmax(")) ||
2057 (str_has_prefix(str, "onchange("))) {
2058 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2059 if (!attrs->action_str[attrs->n_actions]) {
2069 static int parse_assignment(struct trace_array *tr,
2070 char *str, struct hist_trigger_attrs *attrs)
2074 if ((str_has_prefix(str, "key=")) ||
2075 (str_has_prefix(str, "keys="))) {
2076 attrs->keys_str = kstrdup(str, GFP_KERNEL);
2077 if (!attrs->keys_str) {
2081 } else if ((str_has_prefix(str, "val=")) ||
2082 (str_has_prefix(str, "vals=")) ||
2083 (str_has_prefix(str, "values="))) {
2084 attrs->vals_str = kstrdup(str, GFP_KERNEL);
2085 if (!attrs->vals_str) {
2089 } else if (str_has_prefix(str, "sort=")) {
2090 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
2091 if (!attrs->sort_key_str) {
2095 } else if (str_has_prefix(str, "name=")) {
2096 attrs->name = kstrdup(str, GFP_KERNEL);
2101 } else if (str_has_prefix(str, "clock=")) {
2108 str = strstrip(str);
2109 attrs->clock = kstrdup(str, GFP_KERNEL);
2110 if (!attrs->clock) {
2114 } else if (str_has_prefix(str, "size=")) {
2115 int map_bits = parse_map_size(str);
2121 attrs->map_bits = map_bits;
2125 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2126 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
2131 assignment = kstrdup(str, GFP_KERNEL);
2137 attrs->assignment_str[attrs->n_assignments++] = assignment;
2143 static struct hist_trigger_attrs *
2144 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
2146 struct hist_trigger_attrs *attrs;
2149 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2151 return ERR_PTR(-ENOMEM);
2153 while (trigger_str) {
2154 char *str = strsep(&trigger_str, ":");
2156 if (strchr(str, '=')) {
2157 ret = parse_assignment(tr, str, attrs);
2160 } else if (strcmp(str, "pause") == 0)
2161 attrs->pause = true;
2162 else if ((strcmp(str, "cont") == 0) ||
2163 (strcmp(str, "continue") == 0))
2165 else if (strcmp(str, "clear") == 0)
2166 attrs->clear = true;
2168 ret = parse_action(str, attrs);
2174 if (!attrs->keys_str) {
2179 if (!attrs->clock) {
2180 attrs->clock = kstrdup("global", GFP_KERNEL);
2181 if (!attrs->clock) {
2189 destroy_hist_trigger_attrs(attrs);
2191 return ERR_PTR(ret);
2194 static inline void save_comm(char *comm, struct task_struct *task)
2197 strcpy(comm, "<idle>");
2201 if (WARN_ON_ONCE(task->pid < 0)) {
2202 strcpy(comm, "<XXX>");
2206 strncpy(comm, task->comm, TASK_COMM_LEN);
2209 static void hist_elt_data_free(struct hist_elt_data *elt_data)
2213 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2214 kfree(elt_data->field_var_str[i]);
2216 kfree(elt_data->comm);
2220 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2222 struct hist_elt_data *elt_data = elt->private_data;
2224 hist_elt_data_free(elt_data);
2227 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
2229 struct hist_trigger_data *hist_data = elt->map->private_data;
2230 unsigned int size = TASK_COMM_LEN;
2231 struct hist_elt_data *elt_data;
2232 struct hist_field *key_field;
2233 unsigned int i, n_str;
2235 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2239 for_each_hist_key_field(i, hist_data) {
2240 key_field = hist_data->fields[i];
2242 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
2243 elt_data->comm = kzalloc(size, GFP_KERNEL);
2244 if (!elt_data->comm) {
2252 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
2254 size = STR_VAR_LEN_MAX;
2256 for (i = 0; i < n_str; i++) {
2257 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2258 if (!elt_data->field_var_str[i]) {
2259 hist_elt_data_free(elt_data);
2264 elt->private_data = elt_data;
2269 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2271 struct hist_elt_data *elt_data = elt->private_data;
2274 save_comm(elt_data->comm, current);
2277 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2278 .elt_alloc = hist_trigger_elt_data_alloc,
2279 .elt_free = hist_trigger_elt_data_free,
2280 .elt_init = hist_trigger_elt_data_init,
2283 static const char *get_hist_field_flags(struct hist_field *hist_field)
2285 const char *flags_str = NULL;
2287 if (hist_field->flags & HIST_FIELD_FL_HEX)
2289 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2291 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2292 flags_str = "sym-offset";
2293 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2294 flags_str = "execname";
2295 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2296 flags_str = "syscall";
2297 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2299 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2300 flags_str = "usecs";
2305 static void expr_field_str(struct hist_field *field, char *expr)
2307 if (field->flags & HIST_FIELD_FL_VAR_REF)
2310 strcat(expr, hist_field_name(field, 0));
2312 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2313 const char *flags_str = get_hist_field_flags(field);
2317 strcat(expr, flags_str);
2322 static char *expr_str(struct hist_field *field, unsigned int level)
2329 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2333 if (!field->operands[0]) {
2334 expr_field_str(field, expr);
2338 if (field->operator == FIELD_OP_UNARY_MINUS) {
2342 subexpr = expr_str(field->operands[0], ++level);
2347 strcat(expr, subexpr);
2355 expr_field_str(field->operands[0], expr);
2357 switch (field->operator) {
2358 case FIELD_OP_MINUS:
2369 expr_field_str(field->operands[1], expr);
2374 static int contains_operator(char *str)
2376 enum field_op_id field_op = FIELD_OP_NONE;
2379 op = strpbrk(str, "+-");
2381 return FIELD_OP_NONE;
2386 field_op = FIELD_OP_UNARY_MINUS;
2388 field_op = FIELD_OP_MINUS;
2391 field_op = FIELD_OP_PLUS;
2400 static void __destroy_hist_field(struct hist_field *hist_field)
2402 kfree(hist_field->var.name);
2403 kfree(hist_field->name);
2404 kfree(hist_field->type);
2409 static void destroy_hist_field(struct hist_field *hist_field,
2420 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2421 return; /* var refs will be destroyed separately */
2423 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2424 destroy_hist_field(hist_field->operands[i], level + 1);
2426 __destroy_hist_field(hist_field);
2429 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2430 struct ftrace_event_field *field,
2431 unsigned long flags,
2434 struct hist_field *hist_field;
2436 if (field && is_function_field(field))
2439 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2443 hist_field->hist_data = hist_data;
2445 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2446 goto out; /* caller will populate */
2448 if (flags & HIST_FIELD_FL_VAR_REF) {
2449 hist_field->fn = hist_field_var_ref;
2453 if (flags & HIST_FIELD_FL_HITCOUNT) {
2454 hist_field->fn = hist_field_counter;
2455 hist_field->size = sizeof(u64);
2456 hist_field->type = kstrdup("u64", GFP_KERNEL);
2457 if (!hist_field->type)
2462 if (flags & HIST_FIELD_FL_STACKTRACE) {
2463 hist_field->fn = hist_field_none;
2467 if (flags & HIST_FIELD_FL_LOG2) {
2468 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2469 hist_field->fn = hist_field_log2;
2470 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2471 hist_field->size = hist_field->operands[0]->size;
2472 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2473 if (!hist_field->type)
2478 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2479 hist_field->fn = hist_field_timestamp;
2480 hist_field->size = sizeof(u64);
2481 hist_field->type = kstrdup("u64", GFP_KERNEL);
2482 if (!hist_field->type)
2487 if (flags & HIST_FIELD_FL_CPU) {
2488 hist_field->fn = hist_field_cpu;
2489 hist_field->size = sizeof(int);
2490 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2491 if (!hist_field->type)
2496 if (WARN_ON_ONCE(!field))
2499 if (is_string_field(field)) {
2500 flags |= HIST_FIELD_FL_STRING;
2502 hist_field->size = MAX_FILTER_STR_VAL;
2503 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2504 if (!hist_field->type)
2507 if (field->filter_type == FILTER_STATIC_STRING)
2508 hist_field->fn = hist_field_string;
2509 else if (field->filter_type == FILTER_DYN_STRING)
2510 hist_field->fn = hist_field_dynstring;
2512 hist_field->fn = hist_field_pstring;
2514 hist_field->size = field->size;
2515 hist_field->is_signed = field->is_signed;
2516 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2517 if (!hist_field->type)
2520 hist_field->fn = select_value_fn(field->size,
2522 if (!hist_field->fn) {
2523 destroy_hist_field(hist_field, 0);
2528 hist_field->field = field;
2529 hist_field->flags = flags;
2532 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2533 if (!hist_field->var.name)
2539 destroy_hist_field(hist_field, 0);
2543 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2547 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2548 if (hist_data->fields[i]) {
2549 destroy_hist_field(hist_data->fields[i], 0);
2550 hist_data->fields[i] = NULL;
2554 for (i = 0; i < hist_data->n_var_refs; i++) {
2555 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2556 __destroy_hist_field(hist_data->var_refs[i]);
2557 hist_data->var_refs[i] = NULL;
2561 static int init_var_ref(struct hist_field *ref_field,
2562 struct hist_field *var_field,
2563 char *system, char *event_name)
2567 ref_field->var.idx = var_field->var.idx;
2568 ref_field->var.hist_data = var_field->hist_data;
2569 ref_field->size = var_field->size;
2570 ref_field->is_signed = var_field->is_signed;
2571 ref_field->flags |= var_field->flags &
2572 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2575 ref_field->system = kstrdup(system, GFP_KERNEL);
2576 if (!ref_field->system)
2581 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2582 if (!ref_field->event_name) {
2588 if (var_field->var.name) {
2589 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2590 if (!ref_field->name) {
2594 } else if (var_field->name) {
2595 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2596 if (!ref_field->name) {
2602 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2603 if (!ref_field->type) {
2610 kfree(ref_field->system);
2611 kfree(ref_field->event_name);
2612 kfree(ref_field->name);
2618 * create_var_ref - Create a variable reference and attach it to trigger
2619 * @hist_data: The trigger that will be referencing the variable
2620 * @var_field: The VAR field to create a reference to
2621 * @system: The optional system string
2622 * @event_name: The optional event_name string
2624 * Given a variable hist_field, create a VAR_REF hist_field that
2625 * represents a reference to it.
2627 * This function also adds the reference to the trigger that
2628 * now references the variable.
2630 * Return: The VAR_REF field if successful, NULL if not
2632 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2633 struct hist_field *var_field,
2634 char *system, char *event_name)
2636 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2637 struct hist_field *ref_field;
2639 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2641 if (init_var_ref(ref_field, var_field, system, event_name)) {
2642 destroy_hist_field(ref_field, 0);
2646 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2647 ref_field->var_ref_idx = hist_data->n_var_refs++;
2653 static bool is_var_ref(char *var_name)
2655 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2661 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2667 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2668 name = hist_data->attrs->var_defs.name[i];
2670 if (strcmp(var_name, name) == 0) {
2671 field = hist_data->attrs->var_defs.expr[i];
2672 if (contains_operator(field) || is_var_ref(field))
2681 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2682 char *system, char *event_name,
2685 struct trace_event_call *call;
2687 if (system && event_name) {
2688 call = hist_data->event_file->event_call;
2690 if (strcmp(system, call->class->system) != 0)
2693 if (strcmp(event_name, trace_event_name(call)) != 0)
2697 if (!!system != !!event_name)
2700 if (!is_var_ref(var_name))
2705 return field_name_from_var(hist_data, var_name);
2708 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2709 char *system, char *event_name,
2712 struct hist_field *var_field = NULL, *ref_field = NULL;
2713 struct trace_array *tr = hist_data->event_file->tr;
2715 if (!is_var_ref(var_name))
2720 var_field = find_event_var(hist_data, system, event_name, var_name);
2722 ref_field = create_var_ref(hist_data, var_field,
2723 system, event_name);
2726 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2731 static struct ftrace_event_field *
2732 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2733 char *field_str, unsigned long *flags)
2735 struct ftrace_event_field *field = NULL;
2736 char *field_name, *modifier, *str;
2737 struct trace_array *tr = file->tr;
2739 modifier = str = kstrdup(field_str, GFP_KERNEL);
2741 return ERR_PTR(-ENOMEM);
2743 field_name = strsep(&modifier, ".");
2745 if (strcmp(modifier, "hex") == 0)
2746 *flags |= HIST_FIELD_FL_HEX;
2747 else if (strcmp(modifier, "sym") == 0)
2748 *flags |= HIST_FIELD_FL_SYM;
2749 else if (strcmp(modifier, "sym-offset") == 0)
2750 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2751 else if ((strcmp(modifier, "execname") == 0) &&
2752 (strcmp(field_name, "common_pid") == 0))
2753 *flags |= HIST_FIELD_FL_EXECNAME;
2754 else if (strcmp(modifier, "syscall") == 0)
2755 *flags |= HIST_FIELD_FL_SYSCALL;
2756 else if (strcmp(modifier, "log2") == 0)
2757 *flags |= HIST_FIELD_FL_LOG2;
2758 else if (strcmp(modifier, "usecs") == 0)
2759 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2761 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2762 field = ERR_PTR(-EINVAL);
2767 if (strcmp(field_name, "common_timestamp") == 0) {
2768 *flags |= HIST_FIELD_FL_TIMESTAMP;
2769 hist_data->enable_timestamps = true;
2770 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2771 hist_data->attrs->ts_in_usecs = true;
2772 } else if (strcmp(field_name, "cpu") == 0)
2773 *flags |= HIST_FIELD_FL_CPU;
2775 field = trace_find_event_field(file->event_call, field_name);
2776 if (!field || !field->size) {
2777 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
2778 field = ERR_PTR(-EINVAL);
2788 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2789 struct hist_field *var_ref,
2792 struct hist_field *alias = NULL;
2793 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2795 alias = create_hist_field(hist_data, NULL, flags, var_name);
2799 alias->fn = var_ref->fn;
2800 alias->operands[0] = var_ref;
2802 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2803 destroy_hist_field(alias, 0);
2807 alias->var_ref_idx = var_ref->var_ref_idx;
2812 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2813 struct trace_event_file *file, char *str,
2814 unsigned long *flags, char *var_name)
2816 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2817 struct ftrace_event_field *field = NULL;
2818 struct hist_field *hist_field = NULL;
2821 s = strchr(str, '.');
2823 s = strchr(++s, '.');
2825 ref_system = strsep(&str, ".");
2830 ref_event = strsep(&str, ".");
2839 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2841 hist_field = parse_var_ref(hist_data, ref_system,
2842 ref_event, ref_var);
2845 hist_field = create_alias(hist_data, hist_field, var_name);
2856 field = parse_field(hist_data, file, str, flags);
2857 if (IS_ERR(field)) {
2858 ret = PTR_ERR(field);
2862 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2870 return ERR_PTR(ret);
2873 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2874 struct trace_event_file *file,
2875 char *str, unsigned long flags,
2876 char *var_name, unsigned int level);
2878 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2879 struct trace_event_file *file,
2880 char *str, unsigned long flags,
2881 char *var_name, unsigned int level)
2883 struct hist_field *operand1, *expr = NULL;
2884 unsigned long operand_flags;
2888 /* we support only -(xxx) i.e. explicit parens required */
2891 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2896 str++; /* skip leading '-' */
2898 s = strchr(str, '(');
2906 s = strrchr(str, ')');
2910 ret = -EINVAL; /* no closing ')' */
2914 flags |= HIST_FIELD_FL_EXPR;
2915 expr = create_hist_field(hist_data, NULL, flags, var_name);
2922 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2923 if (IS_ERR(operand1)) {
2924 ret = PTR_ERR(operand1);
2928 expr->flags |= operand1->flags &
2929 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2930 expr->fn = hist_field_unary_minus;
2931 expr->operands[0] = operand1;
2932 expr->operator = FIELD_OP_UNARY_MINUS;
2933 expr->name = expr_str(expr, 0);
2934 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2942 destroy_hist_field(expr, 0);
2943 return ERR_PTR(ret);
2946 static int check_expr_operands(struct trace_array *tr,
2947 struct hist_field *operand1,
2948 struct hist_field *operand2)
2950 unsigned long operand1_flags = operand1->flags;
2951 unsigned long operand2_flags = operand2->flags;
2953 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2954 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2955 struct hist_field *var;
2957 var = find_var_field(operand1->var.hist_data, operand1->name);
2960 operand1_flags = var->flags;
2963 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2964 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2965 struct hist_field *var;
2967 var = find_var_field(operand2->var.hist_data, operand2->name);
2970 operand2_flags = var->flags;
2973 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2974 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2975 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2982 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2983 struct trace_event_file *file,
2984 char *str, unsigned long flags,
2985 char *var_name, unsigned int level)
2987 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2988 unsigned long operand_flags;
2989 int field_op, ret = -EINVAL;
2990 char *sep, *operand1_str;
2993 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2994 return ERR_PTR(-EINVAL);
2997 field_op = contains_operator(str);
2999 if (field_op == FIELD_OP_NONE)
3000 return parse_atom(hist_data, file, str, &flags, var_name);
3002 if (field_op == FIELD_OP_UNARY_MINUS)
3003 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3006 case FIELD_OP_MINUS:
3016 operand1_str = strsep(&str, sep);
3017 if (!operand1_str || !str)
3021 operand1 = parse_atom(hist_data, file, operand1_str,
3022 &operand_flags, NULL);
3023 if (IS_ERR(operand1)) {
3024 ret = PTR_ERR(operand1);
3029 /* rest of string could be another expression e.g. b+c in a+b+c */
3031 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3032 if (IS_ERR(operand2)) {
3033 ret = PTR_ERR(operand2);
3038 ret = check_expr_operands(file->tr, operand1, operand2);
3042 flags |= HIST_FIELD_FL_EXPR;
3044 flags |= operand1->flags &
3045 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3047 expr = create_hist_field(hist_data, NULL, flags, var_name);
3053 operand1->read_once = true;
3054 operand2->read_once = true;
3056 expr->operands[0] = operand1;
3057 expr->operands[1] = operand2;
3058 expr->operator = field_op;
3059 expr->name = expr_str(expr, 0);
3060 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3067 case FIELD_OP_MINUS:
3068 expr->fn = hist_field_minus;
3071 expr->fn = hist_field_plus;
3080 destroy_hist_field(operand1, 0);
3081 destroy_hist_field(operand2, 0);
3082 destroy_hist_field(expr, 0);
3084 return ERR_PTR(ret);
3087 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3088 struct trace_event_file *file)
3090 struct event_trigger_data *test;
3092 list_for_each_entry_rcu(test, &file->triggers, list) {
3093 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3094 if (test->private_data == hist_data)
3095 return test->filter_str;
3102 static struct event_command trigger_hist_cmd;
3103 static int event_hist_trigger_func(struct event_command *cmd_ops,
3104 struct trace_event_file *file,
3105 char *glob, char *cmd, char *param);
3107 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3108 struct hist_trigger_data *hist_data,
3109 unsigned int n_keys)
3111 struct hist_field *target_hist_field, *hist_field;
3112 unsigned int n, i, j;
3114 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3117 i = hist_data->n_vals;
3118 j = target_hist_data->n_vals;
3120 for (n = 0; n < n_keys; n++) {
3121 hist_field = hist_data->fields[i + n];
3122 target_hist_field = target_hist_data->fields[j + n];
3124 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3126 if (hist_field->size != target_hist_field->size)
3128 if (hist_field->is_signed != target_hist_field->is_signed)
3135 static struct hist_trigger_data *
3136 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3137 struct trace_event_file *file)
3139 struct hist_trigger_data *hist_data;
3140 struct event_trigger_data *test;
3141 unsigned int n_keys;
3143 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3145 list_for_each_entry_rcu(test, &file->triggers, list) {
3146 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3147 hist_data = test->private_data;
3149 if (compatible_keys(target_hist_data, hist_data, n_keys))
3157 static struct trace_event_file *event_file(struct trace_array *tr,
3158 char *system, char *event_name)
3160 struct trace_event_file *file;
3162 file = __find_event_file(tr, system, event_name);
3164 return ERR_PTR(-EINVAL);
3169 static struct hist_field *
3170 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3171 char *system, char *event_name, char *field_name)
3173 struct hist_field *event_var;
3174 char *synthetic_name;
3176 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3177 if (!synthetic_name)
3178 return ERR_PTR(-ENOMEM);
3180 strcpy(synthetic_name, "synthetic_");
3181 strcat(synthetic_name, field_name);
3183 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3185 kfree(synthetic_name);
3191 * create_field_var_hist - Automatically create a histogram and var for a field
3192 * @target_hist_data: The target hist trigger
3193 * @subsys_name: Optional subsystem name
3194 * @event_name: Optional event name
3195 * @field_name: The name of the field (and the resulting variable)
3197 * Hist trigger actions fetch data from variables, not directly from
3198 * events. However, for convenience, users are allowed to directly
3199 * specify an event field in an action, which will be automatically
3200 * converted into a variable on their behalf.
3202 * If a user specifies a field on an event that isn't the event the
3203 * histogram currently being defined (the target event histogram), the
3204 * only way that can be accomplished is if a new hist trigger is
3205 * created and the field variable defined on that.
3207 * This function creates a new histogram compatible with the target
3208 * event (meaning a histogram with the same key as the target
3209 * histogram), and creates a variable for the specified field, but
3210 * with 'synthetic_' prepended to the variable name in order to avoid
3211 * collision with normal field variables.
3213 * Return: The variable created for the field.
3215 static struct hist_field *
3216 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3217 char *subsys_name, char *event_name, char *field_name)
3219 struct trace_array *tr = target_hist_data->event_file->tr;
3220 struct hist_field *event_var = ERR_PTR(-EINVAL);
3221 struct hist_trigger_data *hist_data;
3222 unsigned int i, n, first = true;
3223 struct field_var_hist *var_hist;
3224 struct trace_event_file *file;
3225 struct hist_field *key_field;
3230 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3231 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3232 return ERR_PTR(-EINVAL);
3235 file = event_file(tr, subsys_name, event_name);
3238 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
3239 ret = PTR_ERR(file);
3240 return ERR_PTR(ret);
3244 * Look for a histogram compatible with target. We'll use the
3245 * found histogram specification to create a new matching
3246 * histogram with our variable on it. target_hist_data is not
3247 * yet a registered histogram so we can't use that.
3249 hist_data = find_compatible_hist(target_hist_data, file);
3251 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
3252 return ERR_PTR(-EINVAL);
3255 /* See if a synthetic field variable has already been created */
3256 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3257 event_name, field_name);
3258 if (!IS_ERR_OR_NULL(event_var))
3261 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3263 return ERR_PTR(-ENOMEM);
3265 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3268 return ERR_PTR(-ENOMEM);
3271 /* Use the same keys as the compatible histogram */
3272 strcat(cmd, "keys=");
3274 for_each_hist_key_field(i, hist_data) {
3275 key_field = hist_data->fields[i];
3278 strcat(cmd, key_field->field->name);
3282 /* Create the synthetic field variable specification */
3283 strcat(cmd, ":synthetic_");
3284 strcat(cmd, field_name);
3286 strcat(cmd, field_name);
3288 /* Use the same filter as the compatible histogram */
3289 saved_filter = find_trigger_filter(hist_data, file);
3291 strcat(cmd, " if ");
3292 strcat(cmd, saved_filter);
3295 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3296 if (!var_hist->cmd) {
3299 return ERR_PTR(-ENOMEM);
3302 /* Save the compatible histogram information */
3303 var_hist->hist_data = hist_data;
3305 /* Create the new histogram with our variable */
3306 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3310 kfree(var_hist->cmd);
3312 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3313 return ERR_PTR(ret);
3318 /* If we can't find the variable, something went wrong */
3319 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3320 event_name, field_name);
3321 if (IS_ERR_OR_NULL(event_var)) {
3322 kfree(var_hist->cmd);
3324 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3325 return ERR_PTR(-EINVAL);
3328 n = target_hist_data->n_field_var_hists;
3329 target_hist_data->field_var_hists[n] = var_hist;
3330 target_hist_data->n_field_var_hists++;
3335 static struct hist_field *
3336 find_target_event_var(struct hist_trigger_data *hist_data,
3337 char *subsys_name, char *event_name, char *var_name)
3339 struct trace_event_file *file = hist_data->event_file;
3340 struct hist_field *hist_field = NULL;
3343 struct trace_event_call *call;
3348 call = file->event_call;
3350 if (strcmp(subsys_name, call->class->system) != 0)
3353 if (strcmp(event_name, trace_event_name(call)) != 0)
3357 hist_field = find_var_field(hist_data, var_name);
3362 static inline void __update_field_vars(struct tracing_map_elt *elt,
3363 struct ring_buffer_event *rbe,
3365 struct field_var **field_vars,
3366 unsigned int n_field_vars,
3367 unsigned int field_var_str_start)
3369 struct hist_elt_data *elt_data = elt->private_data;
3370 unsigned int i, j, var_idx;
3373 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3374 struct field_var *field_var = field_vars[i];
3375 struct hist_field *var = field_var->var;
3376 struct hist_field *val = field_var->val;
3378 var_val = val->fn(val, elt, rbe, rec);
3379 var_idx = var->var.idx;
3381 if (val->flags & HIST_FIELD_FL_STRING) {
3382 char *str = elt_data->field_var_str[j++];
3383 char *val_str = (char *)(uintptr_t)var_val;
3385 strscpy(str, val_str, STR_VAR_LEN_MAX);
3386 var_val = (u64)(uintptr_t)str;
3388 tracing_map_set_var(elt, var_idx, var_val);
3392 static void update_field_vars(struct hist_trigger_data *hist_data,
3393 struct tracing_map_elt *elt,
3394 struct ring_buffer_event *rbe,
3397 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3398 hist_data->n_field_vars, 0);
3401 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3402 struct tracing_map_elt *elt, void *rec,
3403 struct ring_buffer_event *rbe, void *key,
3404 struct action_data *data, u64 *var_ref_vals)
3406 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
3407 hist_data->n_save_vars, hist_data->n_field_var_str);
3410 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3411 struct trace_event_file *file,
3412 char *name, int size, const char *type)
3414 struct hist_field *var;
3417 if (find_var(hist_data, file, name) && !hist_data->remove) {
3418 var = ERR_PTR(-EINVAL);
3422 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3424 var = ERR_PTR(-ENOMEM);
3428 idx = tracing_map_add_var(hist_data->map);
3431 var = ERR_PTR(-EINVAL);
3435 var->flags = HIST_FIELD_FL_VAR;
3437 var->var.hist_data = var->hist_data = hist_data;
3439 var->var.name = kstrdup(name, GFP_KERNEL);
3440 var->type = kstrdup(type, GFP_KERNEL);
3441 if (!var->var.name || !var->type) {
3442 kfree(var->var.name);
3445 var = ERR_PTR(-ENOMEM);
3451 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3452 struct trace_event_file *file,
3455 struct hist_field *val = NULL, *var = NULL;
3456 unsigned long flags = HIST_FIELD_FL_VAR;
3457 struct trace_array *tr = file->tr;
3458 struct field_var *field_var;
3461 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3462 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3467 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3469 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3474 var = create_var(hist_data, file, field_name, val->size, val->type);
3476 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3482 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3490 field_var->var = var;
3491 field_var->val = val;
3495 field_var = ERR_PTR(ret);
3500 * create_target_field_var - Automatically create a variable for a field
3501 * @target_hist_data: The target hist trigger
3502 * @subsys_name: Optional subsystem name
3503 * @event_name: Optional event name
3504 * @var_name: The name of the field (and the resulting variable)
3506 * Hist trigger actions fetch data from variables, not directly from
3507 * events. However, for convenience, users are allowed to directly
3508 * specify an event field in an action, which will be automatically
3509 * converted into a variable on their behalf.
3511 * This function creates a field variable with the name var_name on
3512 * the hist trigger currently being defined on the target event. If
3513 * subsys_name and event_name are specified, this function simply
3514 * verifies that they do in fact match the target event subsystem and
3517 * Return: The variable created for the field.
3519 static struct field_var *
3520 create_target_field_var(struct hist_trigger_data *target_hist_data,
3521 char *subsys_name, char *event_name, char *var_name)
3523 struct trace_event_file *file = target_hist_data->event_file;
3526 struct trace_event_call *call;
3531 call = file->event_call;
3533 if (strcmp(subsys_name, call->class->system) != 0)
3536 if (strcmp(event_name, trace_event_name(call)) != 0)
3540 return create_field_var(target_hist_data, file, var_name);
3543 static bool check_track_val_max(u64 track_val, u64 var_val)
3545 if (var_val <= track_val)
3551 static bool check_track_val_changed(u64 track_val, u64 var_val)
3553 if (var_val == track_val)
3559 static u64 get_track_val(struct hist_trigger_data *hist_data,
3560 struct tracing_map_elt *elt,
3561 struct action_data *data)
3563 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3566 track_val = tracing_map_read_var(elt, track_var_idx);
3571 static void save_track_val(struct hist_trigger_data *hist_data,
3572 struct tracing_map_elt *elt,
3573 struct action_data *data, u64 var_val)
3575 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3577 tracing_map_set_var(elt, track_var_idx, var_val);
3580 static void save_track_data(struct hist_trigger_data *hist_data,
3581 struct tracing_map_elt *elt, void *rec,
3582 struct ring_buffer_event *rbe, void *key,
3583 struct action_data *data, u64 *var_ref_vals)
3585 if (data->track_data.save_data)
3586 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3589 static bool check_track_val(struct tracing_map_elt *elt,
3590 struct action_data *data,
3593 struct hist_trigger_data *hist_data;
3596 hist_data = data->track_data.track_var->hist_data;
3597 track_val = get_track_val(hist_data, elt, data);
3599 return data->track_data.check_val(track_val, var_val);
3602 #ifdef CONFIG_TRACER_SNAPSHOT
3603 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3605 /* called with tr->max_lock held */
3606 struct track_data *track_data = tr->cond_snapshot->cond_data;
3607 struct hist_elt_data *elt_data, *track_elt_data;
3608 struct snapshot_context *context = cond_data;
3609 struct action_data *action;
3615 action = track_data->action_data;
3617 track_val = get_track_val(track_data->hist_data, context->elt,
3618 track_data->action_data);
3620 if (!action->track_data.check_val(track_data->track_val, track_val))
3623 track_data->track_val = track_val;
3624 memcpy(track_data->key, context->key, track_data->key_len);
3626 elt_data = context->elt->private_data;
3627 track_elt_data = track_data->elt.private_data;
3629 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3631 track_data->updated = true;
3636 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3637 struct tracing_map_elt *elt, void *rec,
3638 struct ring_buffer_event *rbe, void *key,
3639 struct action_data *data,
3642 struct trace_event_file *file = hist_data->event_file;
3643 struct snapshot_context context;
3648 tracing_snapshot_cond(file->tr, &context);
3651 static void hist_trigger_print_key(struct seq_file *m,
3652 struct hist_trigger_data *hist_data,
3654 struct tracing_map_elt *elt);
3656 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3660 if (!hist_data->n_actions)
3663 for (i = 0; i < hist_data->n_actions; i++) {
3664 struct action_data *data = hist_data->actions[i];
3666 if (data->action == ACTION_SNAPSHOT)
3673 static void track_data_snapshot_print(struct seq_file *m,
3674 struct hist_trigger_data *hist_data)
3676 struct trace_event_file *file = hist_data->event_file;
3677 struct track_data *track_data;
3678 struct action_data *action;
3680 track_data = tracing_cond_snapshot_data(file->tr);
3684 if (!track_data->updated)
3687 action = snapshot_action(hist_data);
3691 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3692 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3693 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3694 action->track_data.var_str, track_data->track_val);
3696 seq_puts(m, "\ttriggered by event with key: ");
3697 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3701 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3705 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3706 struct tracing_map_elt *elt, void *rec,
3707 struct ring_buffer_event *rbe, void *key,
3708 struct action_data *data,
3709 u64 *var_ref_vals) {}
3710 static void track_data_snapshot_print(struct seq_file *m,
3711 struct hist_trigger_data *hist_data) {}
3712 #endif /* CONFIG_TRACER_SNAPSHOT */
3714 static void track_data_print(struct seq_file *m,
3715 struct hist_trigger_data *hist_data,
3716 struct tracing_map_elt *elt,
3717 struct action_data *data)
3719 u64 track_val = get_track_val(hist_data, elt, data);
3720 unsigned int i, save_var_idx;
3722 if (data->handler == HANDLER_ONMAX)
3723 seq_printf(m, "\n\tmax: %10llu", track_val);
3724 else if (data->handler == HANDLER_ONCHANGE)
3725 seq_printf(m, "\n\tchanged: %10llu", track_val);
3727 if (data->action == ACTION_SNAPSHOT)
3730 for (i = 0; i < hist_data->n_save_vars; i++) {
3731 struct hist_field *save_val = hist_data->save_vars[i]->val;
3732 struct hist_field *save_var = hist_data->save_vars[i]->var;
3735 save_var_idx = save_var->var.idx;
3737 val = tracing_map_read_var(elt, save_var_idx);
3739 if (save_val->flags & HIST_FIELD_FL_STRING) {
3740 seq_printf(m, " %s: %-32s", save_var->var.name,
3741 (char *)(uintptr_t)(val));
3743 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3747 static void ontrack_action(struct hist_trigger_data *hist_data,
3748 struct tracing_map_elt *elt, void *rec,
3749 struct ring_buffer_event *rbe, void *key,
3750 struct action_data *data, u64 *var_ref_vals)
3752 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3754 if (check_track_val(elt, data, var_val)) {
3755 save_track_val(hist_data, elt, data, var_val);
3756 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3760 static void action_data_destroy(struct action_data *data)
3764 lockdep_assert_held(&event_mutex);
3766 kfree(data->action_name);
3768 for (i = 0; i < data->n_params; i++)
3769 kfree(data->params[i]);
3771 if (data->synth_event)
3772 data->synth_event->ref--;
3774 kfree(data->synth_event_name);
3779 static void track_data_destroy(struct hist_trigger_data *hist_data,
3780 struct action_data *data)
3782 struct trace_event_file *file = hist_data->event_file;
3784 destroy_hist_field(data->track_data.track_var, 0);
3786 if (data->action == ACTION_SNAPSHOT) {
3787 struct track_data *track_data;
3789 track_data = tracing_cond_snapshot_data(file->tr);
3790 if (track_data && track_data->hist_data == hist_data) {
3791 tracing_snapshot_cond_disable(file->tr);
3792 track_data_free(track_data);
3796 kfree(data->track_data.var_str);
3798 action_data_destroy(data);
3801 static int action_create(struct hist_trigger_data *hist_data,
3802 struct action_data *data);
3804 static int track_data_create(struct hist_trigger_data *hist_data,
3805 struct action_data *data)
3807 struct hist_field *var_field, *ref_field, *track_var = NULL;
3808 struct trace_event_file *file = hist_data->event_file;
3809 struct trace_array *tr = file->tr;
3810 char *track_data_var_str;
3813 track_data_var_str = data->track_data.var_str;
3814 if (track_data_var_str[0] != '$') {
3815 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3818 track_data_var_str++;
3820 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3822 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3826 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3830 data->track_data.var_ref = ref_field;
3832 if (data->handler == HANDLER_ONMAX)
3833 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3834 if (IS_ERR(track_var)) {
3835 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3836 ret = PTR_ERR(track_var);
3840 if (data->handler == HANDLER_ONCHANGE)
3841 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3842 if (IS_ERR(track_var)) {
3843 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3844 ret = PTR_ERR(track_var);
3847 data->track_data.track_var = track_var;
3849 ret = action_create(hist_data, data);
3854 static int parse_action_params(struct trace_array *tr, char *params,
3855 struct action_data *data)
3857 char *param, *saved_param;
3858 bool first_param = true;
3862 if (data->n_params >= SYNTH_FIELDS_MAX) {
3863 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3867 param = strsep(¶ms, ",");
3869 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3874 param = strstrip(param);
3875 if (strlen(param) < 2) {
3876 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3881 saved_param = kstrdup(param, GFP_KERNEL);
3887 if (first_param && data->use_trace_keyword) {
3888 data->synth_event_name = saved_param;
3889 first_param = false;
3892 first_param = false;
3894 data->params[data->n_params++] = saved_param;
3900 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3901 enum handler_id handler)
3908 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3913 action_name = strsep(&str, "(");
3914 if (!action_name || !str) {
3915 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3920 if (str_has_prefix(action_name, "save")) {
3921 char *params = strsep(&str, ")");
3924 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3929 ret = parse_action_params(tr, params, data);
3933 if (handler == HANDLER_ONMAX)
3934 data->track_data.check_val = check_track_val_max;
3935 else if (handler == HANDLER_ONCHANGE)
3936 data->track_data.check_val = check_track_val_changed;
3938 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3943 data->track_data.save_data = save_track_data_vars;
3944 data->fn = ontrack_action;
3945 data->action = ACTION_SAVE;
3946 } else if (str_has_prefix(action_name, "snapshot")) {
3947 char *params = strsep(&str, ")");
3950 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3955 if (handler == HANDLER_ONMAX)
3956 data->track_data.check_val = check_track_val_max;
3957 else if (handler == HANDLER_ONCHANGE)
3958 data->track_data.check_val = check_track_val_changed;
3960 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3965 data->track_data.save_data = save_track_data_snapshot;
3966 data->fn = ontrack_action;
3967 data->action = ACTION_SNAPSHOT;
3969 char *params = strsep(&str, ")");
3971 if (str_has_prefix(action_name, "trace"))
3972 data->use_trace_keyword = true;
3975 ret = parse_action_params(tr, params, data);
3980 if (handler == HANDLER_ONMAX)
3981 data->track_data.check_val = check_track_val_max;
3982 else if (handler == HANDLER_ONCHANGE)
3983 data->track_data.check_val = check_track_val_changed;
3985 if (handler != HANDLER_ONMATCH) {
3986 data->track_data.save_data = action_trace;
3987 data->fn = ontrack_action;
3989 data->fn = action_trace;
3991 data->action = ACTION_TRACE;
3994 data->action_name = kstrdup(action_name, GFP_KERNEL);
3995 if (!data->action_name) {
4000 data->handler = handler;
4005 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4006 char *str, enum handler_id handler)
4008 struct action_data *data;
4012 data = kzalloc(sizeof(*data), GFP_KERNEL);
4014 return ERR_PTR(-ENOMEM);
4016 var_str = strsep(&str, ")");
4017 if (!var_str || !str) {
4022 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4023 if (!data->track_data.var_str) {
4028 ret = action_parse(hist_data->event_file->tr, str, data, handler);
4034 track_data_destroy(hist_data, data);
4035 data = ERR_PTR(ret);
4039 static void onmatch_destroy(struct action_data *data)
4041 kfree(data->match_data.event);
4042 kfree(data->match_data.event_system);
4044 action_data_destroy(data);
4047 static void destroy_field_var(struct field_var *field_var)
4052 destroy_hist_field(field_var->var, 0);
4053 destroy_hist_field(field_var->val, 0);
4058 static void destroy_field_vars(struct hist_trigger_data *hist_data)
4062 for (i = 0; i < hist_data->n_field_vars; i++)
4063 destroy_field_var(hist_data->field_vars[i]);
4066 static void save_field_var(struct hist_trigger_data *hist_data,
4067 struct field_var *field_var)
4069 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4071 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4072 hist_data->n_field_var_str++;
4076 static int check_synth_field(struct synth_event *event,
4077 struct hist_field *hist_field,
4078 unsigned int field_pos)
4080 struct synth_field *field;
4082 if (field_pos >= event->n_fields)
4085 field = event->fields[field_pos];
4087 if (strcmp(field->type, hist_field->type) != 0)
4093 static struct hist_field *
4094 trace_action_find_var(struct hist_trigger_data *hist_data,
4095 struct action_data *data,
4096 char *system, char *event, char *var)
4098 struct trace_array *tr = hist_data->event_file->tr;
4099 struct hist_field *hist_field;
4101 var++; /* skip '$' */
4103 hist_field = find_target_event_var(hist_data, system, event, var);
4105 if (!system && data->handler == HANDLER_ONMATCH) {
4106 system = data->match_data.event_system;
4107 event = data->match_data.event;
4110 hist_field = find_event_var(hist_data, system, event, var);
4114 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
4119 static struct hist_field *
4120 trace_action_create_field_var(struct hist_trigger_data *hist_data,
4121 struct action_data *data, char *system,
4122 char *event, char *var)
4124 struct hist_field *hist_field = NULL;
4125 struct field_var *field_var;
4128 * First try to create a field var on the target event (the
4129 * currently being defined). This will create a variable for
4130 * unqualified fields on the target event, or if qualified,
4131 * target fields that have qualified names matching the target.
4133 field_var = create_target_field_var(hist_data, system, event, var);
4135 if (field_var && !IS_ERR(field_var)) {
4136 save_field_var(hist_data, field_var);
4137 hist_field = field_var->var;
4141 * If no explicit system.event is specfied, default to
4142 * looking for fields on the onmatch(system.event.xxx)
4145 if (!system && data->handler == HANDLER_ONMATCH) {
4146 system = data->match_data.event_system;
4147 event = data->match_data.event;
4151 * At this point, we're looking at a field on another
4152 * event. Because we can't modify a hist trigger on
4153 * another event to add a variable for a field, we need
4154 * to create a new trigger on that event and create the
4155 * variable at the same time.
4157 hist_field = create_field_var_hist(hist_data, system, event, var);
4158 if (IS_ERR(hist_field))
4164 destroy_field_var(field_var);
4169 static int trace_action_create(struct hist_trigger_data *hist_data,
4170 struct action_data *data)
4172 struct trace_array *tr = hist_data->event_file->tr;
4173 char *event_name, *param, *system = NULL;
4174 struct hist_field *hist_field, *var_ref;
4175 unsigned int i, var_ref_idx;
4176 unsigned int field_pos = 0;
4177 struct synth_event *event;
4178 char *synth_event_name;
4181 lockdep_assert_held(&event_mutex);
4183 if (data->use_trace_keyword)
4184 synth_event_name = data->synth_event_name;
4186 synth_event_name = data->action_name;
4188 event = find_synth_event(synth_event_name);
4190 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
4196 var_ref_idx = hist_data->n_var_refs;
4198 for (i = 0; i < data->n_params; i++) {
4201 p = param = kstrdup(data->params[i], GFP_KERNEL);
4207 system = strsep(¶m, ".");
4209 param = (char *)system;
4210 system = event_name = NULL;
4212 event_name = strsep(¶m, ".");
4220 if (param[0] == '$')
4221 hist_field = trace_action_find_var(hist_data, data,
4225 hist_field = trace_action_create_field_var(hist_data,
4237 if (check_synth_field(event, hist_field, field_pos) == 0) {
4238 var_ref = create_var_ref(hist_data, hist_field,
4239 system, event_name);
4251 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4257 if (field_pos != event->n_fields) {
4258 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4263 data->synth_event = event;
4264 data->var_ref_idx = var_ref_idx;
4273 static int action_create(struct hist_trigger_data *hist_data,
4274 struct action_data *data)
4276 struct trace_event_file *file = hist_data->event_file;
4277 struct trace_array *tr = file->tr;
4278 struct track_data *track_data;
4279 struct field_var *field_var;
4284 if (data->action == ACTION_TRACE)
4285 return trace_action_create(hist_data, data);
4287 if (data->action == ACTION_SNAPSHOT) {
4288 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4289 if (IS_ERR(track_data)) {
4290 ret = PTR_ERR(track_data);
4294 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4295 cond_snapshot_update);
4297 track_data_free(track_data);
4302 if (data->action == ACTION_SAVE) {
4303 if (hist_data->n_save_vars) {
4305 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4309 for (i = 0; i < data->n_params; i++) {
4310 param = kstrdup(data->params[i], GFP_KERNEL);
4316 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4317 if (IS_ERR(field_var)) {
4318 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4320 ret = PTR_ERR(field_var);
4325 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4326 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4327 hist_data->n_save_var_str++;
4335 static int onmatch_create(struct hist_trigger_data *hist_data,
4336 struct action_data *data)
4338 return action_create(hist_data, data);
4341 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4343 char *match_event, *match_event_system;
4344 struct action_data *data;
4347 data = kzalloc(sizeof(*data), GFP_KERNEL);
4349 return ERR_PTR(-ENOMEM);
4351 match_event = strsep(&str, ")");
4352 if (!match_event || !str) {
4353 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4357 match_event_system = strsep(&match_event, ".");
4359 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4363 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4364 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4368 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4369 if (!data->match_data.event) {
4374 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4375 if (!data->match_data.event_system) {
4380 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4386 onmatch_destroy(data);
4387 data = ERR_PTR(ret);
4391 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4393 hist_data->fields[HITCOUNT_IDX] =
4394 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4395 if (!hist_data->fields[HITCOUNT_IDX])
4398 hist_data->n_vals++;
4399 hist_data->n_fields++;
4401 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4407 static int __create_val_field(struct hist_trigger_data *hist_data,
4408 unsigned int val_idx,
4409 struct trace_event_file *file,
4410 char *var_name, char *field_str,
4411 unsigned long flags)
4413 struct hist_field *hist_field;
4416 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4417 if (IS_ERR(hist_field)) {
4418 ret = PTR_ERR(hist_field);
4422 hist_data->fields[val_idx] = hist_field;
4424 ++hist_data->n_vals;
4425 ++hist_data->n_fields;
4427 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4433 static int create_val_field(struct hist_trigger_data *hist_data,
4434 unsigned int val_idx,
4435 struct trace_event_file *file,
4438 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4441 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4444 static int create_var_field(struct hist_trigger_data *hist_data,
4445 unsigned int val_idx,
4446 struct trace_event_file *file,
4447 char *var_name, char *expr_str)
4449 struct trace_array *tr = hist_data->event_file->tr;
4450 unsigned long flags = 0;
4452 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4455 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4456 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4460 flags |= HIST_FIELD_FL_VAR;
4461 hist_data->n_vars++;
4462 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4465 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4468 static int create_val_fields(struct hist_trigger_data *hist_data,
4469 struct trace_event_file *file)
4471 char *fields_str, *field_str;
4472 unsigned int i, j = 1;
4475 ret = create_hitcount_val(hist_data);
4479 fields_str = hist_data->attrs->vals_str;
4483 strsep(&fields_str, "=");
4487 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4488 j < TRACING_MAP_VALS_MAX; i++) {
4489 field_str = strsep(&fields_str, ",");
4493 if (strcmp(field_str, "hitcount") == 0)
4496 ret = create_val_field(hist_data, j++, file, field_str);
4501 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4507 static int create_key_field(struct hist_trigger_data *hist_data,
4508 unsigned int key_idx,
4509 unsigned int key_offset,
4510 struct trace_event_file *file,
4513 struct trace_array *tr = hist_data->event_file->tr;
4514 struct hist_field *hist_field = NULL;
4515 unsigned long flags = 0;
4516 unsigned int key_size;
4519 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4522 flags |= HIST_FIELD_FL_KEY;
4524 if (strcmp(field_str, "stacktrace") == 0) {
4525 flags |= HIST_FIELD_FL_STACKTRACE;
4526 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4527 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4529 hist_field = parse_expr(hist_data, file, field_str, flags,
4531 if (IS_ERR(hist_field)) {
4532 ret = PTR_ERR(hist_field);
4536 if (field_has_hist_vars(hist_field, 0)) {
4537 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4538 destroy_hist_field(hist_field, 0);
4543 key_size = hist_field->size;
4546 hist_data->fields[key_idx] = hist_field;
4548 key_size = ALIGN(key_size, sizeof(u64));
4549 hist_data->fields[key_idx]->size = key_size;
4550 hist_data->fields[key_idx]->offset = key_offset;
4552 hist_data->key_size += key_size;
4554 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4559 hist_data->n_keys++;
4560 hist_data->n_fields++;
4562 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4570 static int create_key_fields(struct hist_trigger_data *hist_data,
4571 struct trace_event_file *file)
4573 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4574 char *fields_str, *field_str;
4577 fields_str = hist_data->attrs->keys_str;
4581 strsep(&fields_str, "=");
4585 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4586 field_str = strsep(&fields_str, ",");
4589 ret = create_key_field(hist_data, i, key_offset,
4604 static int create_var_fields(struct hist_trigger_data *hist_data,
4605 struct trace_event_file *file)
4607 unsigned int i, j = hist_data->n_vals;
4610 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4612 for (i = 0; i < n_vars; i++) {
4613 char *var_name = hist_data->attrs->var_defs.name[i];
4614 char *expr = hist_data->attrs->var_defs.expr[i];
4616 ret = create_var_field(hist_data, j++, file, var_name, expr);
4624 static void free_var_defs(struct hist_trigger_data *hist_data)
4628 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4629 kfree(hist_data->attrs->var_defs.name[i]);
4630 kfree(hist_data->attrs->var_defs.expr[i]);
4633 hist_data->attrs->var_defs.n_vars = 0;
4636 static int parse_var_defs(struct hist_trigger_data *hist_data)
4638 struct trace_array *tr = hist_data->event_file->tr;
4639 char *s, *str, *var_name, *field_str;
4640 unsigned int i, j, n_vars = 0;
4643 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4644 str = hist_data->attrs->assignment_str[i];
4645 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4646 field_str = strsep(&str, ",");
4650 var_name = strsep(&field_str, "=");
4651 if (!var_name || !field_str) {
4652 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4658 if (n_vars == TRACING_MAP_VARS_MAX) {
4659 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4664 s = kstrdup(var_name, GFP_KERNEL);
4669 hist_data->attrs->var_defs.name[n_vars] = s;
4671 s = kstrdup(field_str, GFP_KERNEL);
4673 kfree(hist_data->attrs->var_defs.name[n_vars]);
4677 hist_data->attrs->var_defs.expr[n_vars++] = s;
4679 hist_data->attrs->var_defs.n_vars = n_vars;
4685 free_var_defs(hist_data);
4690 static int create_hist_fields(struct hist_trigger_data *hist_data,
4691 struct trace_event_file *file)
4695 ret = parse_var_defs(hist_data);
4699 ret = create_val_fields(hist_data, file);
4703 ret = create_var_fields(hist_data, file);
4707 ret = create_key_fields(hist_data, file);
4711 free_var_defs(hist_data);
4716 static int is_descending(const char *str)
4721 if (strcmp(str, "descending") == 0)
4724 if (strcmp(str, "ascending") == 0)
4730 static int create_sort_keys(struct hist_trigger_data *hist_data)
4732 char *fields_str = hist_data->attrs->sort_key_str;
4733 struct tracing_map_sort_key *sort_key;
4734 int descending, ret = 0;
4735 unsigned int i, j, k;
4737 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4742 strsep(&fields_str, "=");
4748 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4749 struct hist_field *hist_field;
4750 char *field_str, *field_name;
4751 const char *test_name;
4753 sort_key = &hist_data->sort_keys[i];
4755 field_str = strsep(&fields_str, ",");
4762 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4767 field_name = strsep(&field_str, ".");
4773 if (strcmp(field_name, "hitcount") == 0) {
4774 descending = is_descending(field_str);
4775 if (descending < 0) {
4779 sort_key->descending = descending;
4783 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4786 hist_field = hist_data->fields[j];
4787 if (hist_field->flags & HIST_FIELD_FL_VAR)
4792 test_name = hist_field_name(hist_field, 0);
4794 if (strcmp(field_name, test_name) == 0) {
4795 sort_key->field_idx = idx;
4796 descending = is_descending(field_str);
4797 if (descending < 0) {
4801 sort_key->descending = descending;
4805 if (j == hist_data->n_fields) {
4811 hist_data->n_sort_keys = i;
4816 static void destroy_actions(struct hist_trigger_data *hist_data)
4820 for (i = 0; i < hist_data->n_actions; i++) {
4821 struct action_data *data = hist_data->actions[i];
4823 if (data->handler == HANDLER_ONMATCH)
4824 onmatch_destroy(data);
4825 else if (data->handler == HANDLER_ONMAX ||
4826 data->handler == HANDLER_ONCHANGE)
4827 track_data_destroy(hist_data, data);
4833 static int parse_actions(struct hist_trigger_data *hist_data)
4835 struct trace_array *tr = hist_data->event_file->tr;
4836 struct action_data *data;
4842 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4843 str = hist_data->attrs->action_str[i];
4845 if ((len = str_has_prefix(str, "onmatch("))) {
4846 char *action_str = str + len;
4848 data = onmatch_parse(tr, action_str);
4850 ret = PTR_ERR(data);
4853 } else if ((len = str_has_prefix(str, "onmax("))) {
4854 char *action_str = str + len;
4856 data = track_data_parse(hist_data, action_str,
4859 ret = PTR_ERR(data);
4862 } else if ((len = str_has_prefix(str, "onchange("))) {
4863 char *action_str = str + len;
4865 data = track_data_parse(hist_data, action_str,
4868 ret = PTR_ERR(data);
4876 hist_data->actions[hist_data->n_actions++] = data;
4882 static int create_actions(struct hist_trigger_data *hist_data)
4884 struct action_data *data;
4888 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4889 data = hist_data->actions[i];
4891 if (data->handler == HANDLER_ONMATCH) {
4892 ret = onmatch_create(hist_data, data);
4895 } else if (data->handler == HANDLER_ONMAX ||
4896 data->handler == HANDLER_ONCHANGE) {
4897 ret = track_data_create(hist_data, data);
4909 static void print_actions(struct seq_file *m,
4910 struct hist_trigger_data *hist_data,
4911 struct tracing_map_elt *elt)
4915 for (i = 0; i < hist_data->n_actions; i++) {
4916 struct action_data *data = hist_data->actions[i];
4918 if (data->action == ACTION_SNAPSHOT)
4921 if (data->handler == HANDLER_ONMAX ||
4922 data->handler == HANDLER_ONCHANGE)
4923 track_data_print(m, hist_data, elt, data);
4927 static void print_action_spec(struct seq_file *m,
4928 struct hist_trigger_data *hist_data,
4929 struct action_data *data)
4933 if (data->action == ACTION_SAVE) {
4934 for (i = 0; i < hist_data->n_save_vars; i++) {
4935 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4936 if (i < hist_data->n_save_vars - 1)
4939 } else if (data->action == ACTION_TRACE) {
4940 if (data->use_trace_keyword)
4941 seq_printf(m, "%s", data->synth_event_name);
4942 for (i = 0; i < data->n_params; i++) {
4943 if (i || data->use_trace_keyword)
4945 seq_printf(m, "%s", data->params[i]);
4950 static void print_track_data_spec(struct seq_file *m,
4951 struct hist_trigger_data *hist_data,
4952 struct action_data *data)
4954 if (data->handler == HANDLER_ONMAX)
4955 seq_puts(m, ":onmax(");
4956 else if (data->handler == HANDLER_ONCHANGE)
4957 seq_puts(m, ":onchange(");
4958 seq_printf(m, "%s", data->track_data.var_str);
4959 seq_printf(m, ").%s(", data->action_name);
4961 print_action_spec(m, hist_data, data);
4966 static void print_onmatch_spec(struct seq_file *m,
4967 struct hist_trigger_data *hist_data,
4968 struct action_data *data)
4970 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4971 data->match_data.event);
4973 seq_printf(m, "%s(", data->action_name);
4975 print_action_spec(m, hist_data, data);
4980 static bool actions_match(struct hist_trigger_data *hist_data,
4981 struct hist_trigger_data *hist_data_test)
4985 if (hist_data->n_actions != hist_data_test->n_actions)
4988 for (i = 0; i < hist_data->n_actions; i++) {
4989 struct action_data *data = hist_data->actions[i];
4990 struct action_data *data_test = hist_data_test->actions[i];
4991 char *action_name, *action_name_test;
4993 if (data->handler != data_test->handler)
4995 if (data->action != data_test->action)
4998 if (data->n_params != data_test->n_params)
5001 for (j = 0; j < data->n_params; j++) {
5002 if (strcmp(data->params[j], data_test->params[j]) != 0)
5006 if (data->use_trace_keyword)
5007 action_name = data->synth_event_name;
5009 action_name = data->action_name;
5011 if (data_test->use_trace_keyword)
5012 action_name_test = data_test->synth_event_name;
5014 action_name_test = data_test->action_name;
5016 if (strcmp(action_name, action_name_test) != 0)
5019 if (data->handler == HANDLER_ONMATCH) {
5020 if (strcmp(data->match_data.event_system,
5021 data_test->match_data.event_system) != 0)
5023 if (strcmp(data->match_data.event,
5024 data_test->match_data.event) != 0)
5026 } else if (data->handler == HANDLER_ONMAX ||
5027 data->handler == HANDLER_ONCHANGE) {
5028 if (strcmp(data->track_data.var_str,
5029 data_test->track_data.var_str) != 0)
5038 static void print_actions_spec(struct seq_file *m,
5039 struct hist_trigger_data *hist_data)
5043 for (i = 0; i < hist_data->n_actions; i++) {
5044 struct action_data *data = hist_data->actions[i];
5046 if (data->handler == HANDLER_ONMATCH)
5047 print_onmatch_spec(m, hist_data, data);
5048 else if (data->handler == HANDLER_ONMAX ||
5049 data->handler == HANDLER_ONCHANGE)
5050 print_track_data_spec(m, hist_data, data);
5054 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5058 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5059 kfree(hist_data->field_var_hists[i]->cmd);
5060 kfree(hist_data->field_var_hists[i]);
5064 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5069 destroy_hist_trigger_attrs(hist_data->attrs);
5070 destroy_hist_fields(hist_data);
5071 tracing_map_destroy(hist_data->map);
5073 destroy_actions(hist_data);
5074 destroy_field_vars(hist_data);
5075 destroy_field_var_hists(hist_data);
5080 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5082 struct tracing_map *map = hist_data->map;
5083 struct ftrace_event_field *field;
5084 struct hist_field *hist_field;
5087 for_each_hist_field(i, hist_data) {
5088 hist_field = hist_data->fields[i];
5089 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5090 tracing_map_cmp_fn_t cmp_fn;
5092 field = hist_field->field;
5094 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5095 cmp_fn = tracing_map_cmp_none;
5097 cmp_fn = tracing_map_cmp_num(hist_field->size,
5098 hist_field->is_signed);
5099 else if (is_string_field(field))
5100 cmp_fn = tracing_map_cmp_string;
5102 cmp_fn = tracing_map_cmp_num(field->size,
5104 idx = tracing_map_add_key_field(map,
5107 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5108 idx = tracing_map_add_sum_field(map);
5113 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5114 idx = tracing_map_add_var(map);
5117 hist_field->var.idx = idx;
5118 hist_field->var.hist_data = hist_data;
5125 static struct hist_trigger_data *
5126 create_hist_data(unsigned int map_bits,
5127 struct hist_trigger_attrs *attrs,
5128 struct trace_event_file *file,
5131 const struct tracing_map_ops *map_ops = NULL;
5132 struct hist_trigger_data *hist_data;
5135 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5137 return ERR_PTR(-ENOMEM);
5139 hist_data->attrs = attrs;
5140 hist_data->remove = remove;
5141 hist_data->event_file = file;
5143 ret = parse_actions(hist_data);
5147 ret = create_hist_fields(hist_data, file);
5151 ret = create_sort_keys(hist_data);
5155 map_ops = &hist_trigger_elt_data_ops;
5157 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5158 map_ops, hist_data);
5159 if (IS_ERR(hist_data->map)) {
5160 ret = PTR_ERR(hist_data->map);
5161 hist_data->map = NULL;
5165 ret = create_tracing_map_fields(hist_data);
5171 hist_data->attrs = NULL;
5173 destroy_hist_data(hist_data);
5175 hist_data = ERR_PTR(ret);
5180 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5181 struct tracing_map_elt *elt, void *rec,
5182 struct ring_buffer_event *rbe,
5185 struct hist_elt_data *elt_data;
5186 struct hist_field *hist_field;
5187 unsigned int i, var_idx;
5190 elt_data = elt->private_data;
5191 elt_data->var_ref_vals = var_ref_vals;
5193 for_each_hist_val_field(i, hist_data) {
5194 hist_field = hist_data->fields[i];
5195 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5196 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5197 var_idx = hist_field->var.idx;
5198 tracing_map_set_var(elt, var_idx, hist_val);
5201 tracing_map_update_sum(elt, i, hist_val);
5204 for_each_hist_key_field(i, hist_data) {
5205 hist_field = hist_data->fields[i];
5206 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5207 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5208 var_idx = hist_field->var.idx;
5209 tracing_map_set_var(elt, var_idx, hist_val);
5213 update_field_vars(hist_data, elt, rbe, rec);
5216 static inline void add_to_key(char *compound_key, void *key,
5217 struct hist_field *key_field, void *rec)
5219 size_t size = key_field->size;
5221 if (key_field->flags & HIST_FIELD_FL_STRING) {
5222 struct ftrace_event_field *field;
5224 field = key_field->field;
5225 if (field->filter_type == FILTER_DYN_STRING)
5226 size = *(u32 *)(rec + field->offset) >> 16;
5227 else if (field->filter_type == FILTER_PTR_STRING)
5229 else if (field->filter_type == FILTER_STATIC_STRING)
5232 /* ensure NULL-termination */
5233 if (size > key_field->size - 1)
5234 size = key_field->size - 1;
5236 strncpy(compound_key + key_field->offset, (char *)key, size);
5238 memcpy(compound_key + key_field->offset, key, size);
5242 hist_trigger_actions(struct hist_trigger_data *hist_data,
5243 struct tracing_map_elt *elt, void *rec,
5244 struct ring_buffer_event *rbe, void *key,
5247 struct action_data *data;
5250 for (i = 0; i < hist_data->n_actions; i++) {
5251 data = hist_data->actions[i];
5252 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
5256 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
5257 struct ring_buffer_event *rbe)
5259 struct hist_trigger_data *hist_data = data->private_data;
5260 bool use_compound_key = (hist_data->n_keys > 1);
5261 unsigned long entries[HIST_STACKTRACE_DEPTH];
5262 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5263 char compound_key[HIST_KEY_SIZE_MAX];
5264 struct tracing_map_elt *elt = NULL;
5265 struct hist_field *key_field;
5270 memset(compound_key, 0, hist_data->key_size);
5272 for_each_hist_key_field(i, hist_data) {
5273 key_field = hist_data->fields[i];
5275 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5276 memset(entries, 0, HIST_STACKTRACE_SIZE);
5277 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5278 HIST_STACKTRACE_SKIP);
5281 field_contents = key_field->fn(key_field, elt, rbe, rec);
5282 if (key_field->flags & HIST_FIELD_FL_STRING) {
5283 key = (void *)(unsigned long)field_contents;
5284 use_compound_key = true;
5286 key = (void *)&field_contents;
5289 if (use_compound_key)
5290 add_to_key(compound_key, key, key_field, rec);
5293 if (use_compound_key)
5296 if (hist_data->n_var_refs &&
5297 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5300 elt = tracing_map_insert(hist_data->map, key);
5304 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
5306 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5307 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
5310 static void hist_trigger_stacktrace_print(struct seq_file *m,
5311 unsigned long *stacktrace_entries,
5312 unsigned int max_entries)
5314 char str[KSYM_SYMBOL_LEN];
5315 unsigned int spaces = 8;
5318 for (i = 0; i < max_entries; i++) {
5319 if (!stacktrace_entries[i])
5322 seq_printf(m, "%*c", 1 + spaces, ' ');
5323 sprint_symbol(str, stacktrace_entries[i]);
5324 seq_printf(m, "%s\n", str);
5328 static void hist_trigger_print_key(struct seq_file *m,
5329 struct hist_trigger_data *hist_data,
5331 struct tracing_map_elt *elt)
5333 struct hist_field *key_field;
5334 char str[KSYM_SYMBOL_LEN];
5335 bool multiline = false;
5336 const char *field_name;
5342 for_each_hist_key_field(i, hist_data) {
5343 key_field = hist_data->fields[i];
5345 if (i > hist_data->n_vals)
5348 field_name = hist_field_name(key_field, 0);
5350 if (key_field->flags & HIST_FIELD_FL_HEX) {
5351 uval = *(u64 *)(key + key_field->offset);
5352 seq_printf(m, "%s: %llx", field_name, uval);
5353 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5354 uval = *(u64 *)(key + key_field->offset);
5355 sprint_symbol_no_offset(str, uval);
5356 seq_printf(m, "%s: [%llx] %-45s", field_name,
5358 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5359 uval = *(u64 *)(key + key_field->offset);
5360 sprint_symbol(str, uval);
5361 seq_printf(m, "%s: [%llx] %-55s", field_name,
5363 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5364 struct hist_elt_data *elt_data = elt->private_data;
5367 if (WARN_ON_ONCE(!elt_data))
5370 comm = elt_data->comm;
5372 uval = *(u64 *)(key + key_field->offset);
5373 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5375 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5376 const char *syscall_name;
5378 uval = *(u64 *)(key + key_field->offset);
5379 syscall_name = get_syscall_name(uval);
5381 syscall_name = "unknown_syscall";
5383 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5384 syscall_name, uval);
5385 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5386 seq_puts(m, "stacktrace:\n");
5387 hist_trigger_stacktrace_print(m,
5388 key + key_field->offset,
5389 HIST_STACKTRACE_DEPTH);
5391 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5392 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5393 *(u64 *)(key + key_field->offset));
5394 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5395 seq_printf(m, "%s: %-50s", field_name,
5396 (char *)(key + key_field->offset));
5398 uval = *(u64 *)(key + key_field->offset);
5399 seq_printf(m, "%s: %10llu", field_name, uval);
5409 static void hist_trigger_entry_print(struct seq_file *m,
5410 struct hist_trigger_data *hist_data,
5412 struct tracing_map_elt *elt)
5414 const char *field_name;
5417 hist_trigger_print_key(m, hist_data, key, elt);
5419 seq_printf(m, " hitcount: %10llu",
5420 tracing_map_read_sum(elt, HITCOUNT_IDX));
5422 for (i = 1; i < hist_data->n_vals; i++) {
5423 field_name = hist_field_name(hist_data->fields[i], 0);
5425 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5426 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
5429 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
5430 seq_printf(m, " %s: %10llx", field_name,
5431 tracing_map_read_sum(elt, i));
5433 seq_printf(m, " %s: %10llu", field_name,
5434 tracing_map_read_sum(elt, i));
5438 print_actions(m, hist_data, elt);
5443 static int print_entries(struct seq_file *m,
5444 struct hist_trigger_data *hist_data)
5446 struct tracing_map_sort_entry **sort_entries = NULL;
5447 struct tracing_map *map = hist_data->map;
5450 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5451 hist_data->n_sort_keys,
5456 for (i = 0; i < n_entries; i++)
5457 hist_trigger_entry_print(m, hist_data,
5458 sort_entries[i]->key,
5459 sort_entries[i]->elt);
5461 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5466 static void hist_trigger_show(struct seq_file *m,
5467 struct event_trigger_data *data, int n)
5469 struct hist_trigger_data *hist_data;
5473 seq_puts(m, "\n\n");
5475 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5476 data->ops->print(m, data->ops, data);
5477 seq_puts(m, "#\n\n");
5479 hist_data = data->private_data;
5480 n_entries = print_entries(m, hist_data);
5484 track_data_snapshot_print(m, hist_data);
5486 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5487 (u64)atomic64_read(&hist_data->map->hits),
5488 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5491 static int hist_show(struct seq_file *m, void *v)
5493 struct event_trigger_data *data;
5494 struct trace_event_file *event_file;
5497 mutex_lock(&event_mutex);
5499 event_file = event_file_data(m->private);
5500 if (unlikely(!event_file)) {
5505 list_for_each_entry_rcu(data, &event_file->triggers, list) {
5506 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5507 hist_trigger_show(m, data, n++);
5511 mutex_unlock(&event_mutex);
5516 static int event_hist_open(struct inode *inode, struct file *file)
5518 return single_open(file, hist_show, file);
5521 const struct file_operations event_hist_fops = {
5522 .open = event_hist_open,
5524 .llseek = seq_lseek,
5525 .release = single_release,
5528 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5530 const char *field_name = hist_field_name(hist_field, 0);
5532 if (hist_field->var.name)
5533 seq_printf(m, "%s=", hist_field->var.name);
5535 if (hist_field->flags & HIST_FIELD_FL_CPU)
5537 else if (field_name) {
5538 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5539 hist_field->flags & HIST_FIELD_FL_ALIAS)
5541 seq_printf(m, "%s", field_name);
5542 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5543 seq_puts(m, "common_timestamp");
5545 if (hist_field->flags) {
5546 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5547 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5548 const char *flags = get_hist_field_flags(hist_field);
5551 seq_printf(m, ".%s", flags);
5556 static int event_hist_trigger_print(struct seq_file *m,
5557 struct event_trigger_ops *ops,
5558 struct event_trigger_data *data)
5560 struct hist_trigger_data *hist_data = data->private_data;
5561 struct hist_field *field;
5562 bool have_var = false;
5565 seq_puts(m, "hist:");
5568 seq_printf(m, "%s:", data->name);
5570 seq_puts(m, "keys=");
5572 for_each_hist_key_field(i, hist_data) {
5573 field = hist_data->fields[i];
5575 if (i > hist_data->n_vals)
5578 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5579 seq_puts(m, "stacktrace");
5581 hist_field_print(m, field);
5584 seq_puts(m, ":vals=");
5586 for_each_hist_val_field(i, hist_data) {
5587 field = hist_data->fields[i];
5588 if (field->flags & HIST_FIELD_FL_VAR) {
5593 if (i == HITCOUNT_IDX)
5594 seq_puts(m, "hitcount");
5597 hist_field_print(m, field);
5606 for_each_hist_val_field(i, hist_data) {
5607 field = hist_data->fields[i];
5609 if (field->flags & HIST_FIELD_FL_VAR) {
5612 hist_field_print(m, field);
5617 seq_puts(m, ":sort=");
5619 for (i = 0; i < hist_data->n_sort_keys; i++) {
5620 struct tracing_map_sort_key *sort_key;
5621 unsigned int idx, first_key_idx;
5624 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5626 sort_key = &hist_data->sort_keys[i];
5627 idx = sort_key->field_idx;
5629 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5635 if (idx == HITCOUNT_IDX)
5636 seq_puts(m, "hitcount");
5638 if (idx >= first_key_idx)
5639 idx += hist_data->n_vars;
5640 hist_field_print(m, hist_data->fields[idx]);
5643 if (sort_key->descending)
5644 seq_puts(m, ".descending");
5646 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5647 if (hist_data->enable_timestamps)
5648 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5650 print_actions_spec(m, hist_data);
5652 if (data->filter_str)
5653 seq_printf(m, " if %s", data->filter_str);
5656 seq_puts(m, " [paused]");
5658 seq_puts(m, " [active]");
5665 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5666 struct event_trigger_data *data)
5668 struct hist_trigger_data *hist_data = data->private_data;
5670 if (!data->ref && hist_data->attrs->name)
5671 save_named_trigger(hist_data->attrs->name, data);
5678 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5680 struct trace_event_file *file;
5685 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5686 file = hist_data->field_var_hists[i]->hist_data->event_file;
5687 cmd = hist_data->field_var_hists[i]->cmd;
5688 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5689 "!hist", "hist", cmd);
5693 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5694 struct event_trigger_data *data)
5696 struct hist_trigger_data *hist_data = data->private_data;
5698 if (WARN_ON_ONCE(data->ref <= 0))
5704 del_named_trigger(data);
5706 trigger_data_free(data);
5708 remove_hist_vars(hist_data);
5710 unregister_field_var_hists(hist_data);
5712 destroy_hist_data(hist_data);
5716 static struct event_trigger_ops event_hist_trigger_ops = {
5717 .func = event_hist_trigger,
5718 .print = event_hist_trigger_print,
5719 .init = event_hist_trigger_init,
5720 .free = event_hist_trigger_free,
5723 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5724 struct event_trigger_data *data)
5728 save_named_trigger(data->named_data->name, data);
5730 event_hist_trigger_init(ops, data->named_data);
5735 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5736 struct event_trigger_data *data)
5738 if (WARN_ON_ONCE(data->ref <= 0))
5741 event_hist_trigger_free(ops, data->named_data);
5745 del_named_trigger(data);
5746 trigger_data_free(data);
5750 static struct event_trigger_ops event_hist_trigger_named_ops = {
5751 .func = event_hist_trigger,
5752 .print = event_hist_trigger_print,
5753 .init = event_hist_trigger_named_init,
5754 .free = event_hist_trigger_named_free,
5757 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5760 return &event_hist_trigger_ops;
5763 static void hist_clear(struct event_trigger_data *data)
5765 struct hist_trigger_data *hist_data = data->private_data;
5768 pause_named_trigger(data);
5770 tracepoint_synchronize_unregister();
5772 tracing_map_clear(hist_data->map);
5775 unpause_named_trigger(data);
5778 static bool compatible_field(struct ftrace_event_field *field,
5779 struct ftrace_event_field *test_field)
5781 if (field == test_field)
5783 if (field == NULL || test_field == NULL)
5785 if (strcmp(field->name, test_field->name) != 0)
5787 if (strcmp(field->type, test_field->type) != 0)
5789 if (field->size != test_field->size)
5791 if (field->is_signed != test_field->is_signed)
5797 static bool hist_trigger_match(struct event_trigger_data *data,
5798 struct event_trigger_data *data_test,
5799 struct event_trigger_data *named_data,
5802 struct tracing_map_sort_key *sort_key, *sort_key_test;
5803 struct hist_trigger_data *hist_data, *hist_data_test;
5804 struct hist_field *key_field, *key_field_test;
5807 if (named_data && (named_data != data_test) &&
5808 (named_data != data_test->named_data))
5811 if (!named_data && is_named_trigger(data_test))
5814 hist_data = data->private_data;
5815 hist_data_test = data_test->private_data;
5817 if (hist_data->n_vals != hist_data_test->n_vals ||
5818 hist_data->n_fields != hist_data_test->n_fields ||
5819 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5822 if (!ignore_filter) {
5823 if ((data->filter_str && !data_test->filter_str) ||
5824 (!data->filter_str && data_test->filter_str))
5828 for_each_hist_field(i, hist_data) {
5829 key_field = hist_data->fields[i];
5830 key_field_test = hist_data_test->fields[i];
5832 if (key_field->flags != key_field_test->flags)
5834 if (!compatible_field(key_field->field, key_field_test->field))
5836 if (key_field->offset != key_field_test->offset)
5838 if (key_field->size != key_field_test->size)
5840 if (key_field->is_signed != key_field_test->is_signed)
5842 if (!!key_field->var.name != !!key_field_test->var.name)
5844 if (key_field->var.name &&
5845 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5849 for (i = 0; i < hist_data->n_sort_keys; i++) {
5850 sort_key = &hist_data->sort_keys[i];
5851 sort_key_test = &hist_data_test->sort_keys[i];
5853 if (sort_key->field_idx != sort_key_test->field_idx ||
5854 sort_key->descending != sort_key_test->descending)
5858 if (!ignore_filter && data->filter_str &&
5859 (strcmp(data->filter_str, data_test->filter_str) != 0))
5862 if (!actions_match(hist_data, hist_data_test))
5868 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5869 struct event_trigger_data *data,
5870 struct trace_event_file *file)
5872 struct hist_trigger_data *hist_data = data->private_data;
5873 struct event_trigger_data *test, *named_data = NULL;
5874 struct trace_array *tr = file->tr;
5877 if (hist_data->attrs->name) {
5878 named_data = find_named_trigger(hist_data->attrs->name);
5880 if (!hist_trigger_match(data, named_data, named_data,
5882 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5889 if (hist_data->attrs->name && !named_data)
5892 list_for_each_entry_rcu(test, &file->triggers, list) {
5893 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5894 if (!hist_trigger_match(data, test, named_data, false))
5896 if (hist_data->attrs->pause)
5897 test->paused = true;
5898 else if (hist_data->attrs->cont)
5899 test->paused = false;
5900 else if (hist_data->attrs->clear)
5903 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5910 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5911 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5916 if (hist_data->attrs->pause)
5917 data->paused = true;
5920 data->private_data = named_data->private_data;
5921 set_named_trigger_data(data, named_data);
5922 data->ops = &event_hist_trigger_named_ops;
5925 if (data->ops->init) {
5926 ret = data->ops->init(data->ops, data);
5931 if (hist_data->enable_timestamps) {
5932 char *clock = hist_data->attrs->clock;
5934 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5936 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5940 tracing_set_time_stamp_abs(file->tr, true);
5944 destroy_hist_data(hist_data);
5951 static int hist_trigger_enable(struct event_trigger_data *data,
5952 struct trace_event_file *file)
5956 list_add_tail_rcu(&data->list, &file->triggers);
5958 update_cond_flag(file);
5960 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5961 list_del_rcu(&data->list);
5962 update_cond_flag(file);
5969 static bool have_hist_trigger_match(struct event_trigger_data *data,
5970 struct trace_event_file *file)
5972 struct hist_trigger_data *hist_data = data->private_data;
5973 struct event_trigger_data *test, *named_data = NULL;
5976 if (hist_data->attrs->name)
5977 named_data = find_named_trigger(hist_data->attrs->name);
5979 list_for_each_entry_rcu(test, &file->triggers, list) {
5980 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5981 if (hist_trigger_match(data, test, named_data, false)) {
5991 static bool hist_trigger_check_refs(struct event_trigger_data *data,
5992 struct trace_event_file *file)
5994 struct hist_trigger_data *hist_data = data->private_data;
5995 struct event_trigger_data *test, *named_data = NULL;
5997 if (hist_data->attrs->name)
5998 named_data = find_named_trigger(hist_data->attrs->name);
6000 list_for_each_entry_rcu(test, &file->triggers, list) {
6001 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6002 if (!hist_trigger_match(data, test, named_data, false))
6004 hist_data = test->private_data;
6005 if (check_var_refs(hist_data))
6014 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6015 struct event_trigger_data *data,
6016 struct trace_event_file *file)
6018 struct hist_trigger_data *hist_data = data->private_data;
6019 struct event_trigger_data *test, *named_data = NULL;
6020 bool unregistered = false;
6022 if (hist_data->attrs->name)
6023 named_data = find_named_trigger(hist_data->attrs->name);
6025 list_for_each_entry_rcu(test, &file->triggers, list) {
6026 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6027 if (!hist_trigger_match(data, test, named_data, false))
6029 unregistered = true;
6030 list_del_rcu(&test->list);
6031 trace_event_trigger_enable_disable(file, 0);
6032 update_cond_flag(file);
6037 if (unregistered && test->ops->free)
6038 test->ops->free(test->ops, test);
6040 if (hist_data->enable_timestamps) {
6041 if (!hist_data->remove || unregistered)
6042 tracing_set_time_stamp_abs(file->tr, false);
6046 static bool hist_file_check_refs(struct trace_event_file *file)
6048 struct hist_trigger_data *hist_data;
6049 struct event_trigger_data *test;
6051 list_for_each_entry_rcu(test, &file->triggers, list) {
6052 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6053 hist_data = test->private_data;
6054 if (check_var_refs(hist_data))
6062 static void hist_unreg_all(struct trace_event_file *file)
6064 struct event_trigger_data *test, *n;
6065 struct hist_trigger_data *hist_data;
6066 struct synth_event *se;
6067 const char *se_name;
6069 lockdep_assert_held(&event_mutex);
6071 if (hist_file_check_refs(file))
6074 list_for_each_entry_safe(test, n, &file->triggers, list) {
6075 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6076 hist_data = test->private_data;
6077 list_del_rcu(&test->list);
6078 trace_event_trigger_enable_disable(file, 0);
6080 se_name = trace_event_name(file->event_call);
6081 se = find_synth_event(se_name);
6085 update_cond_flag(file);
6086 if (hist_data->enable_timestamps)
6087 tracing_set_time_stamp_abs(file->tr, false);
6088 if (test->ops->free)
6089 test->ops->free(test->ops, test);
6094 static int event_hist_trigger_func(struct event_command *cmd_ops,
6095 struct trace_event_file *file,
6096 char *glob, char *cmd, char *param)
6098 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6099 struct event_trigger_data *trigger_data;
6100 struct hist_trigger_attrs *attrs;
6101 struct event_trigger_ops *trigger_ops;
6102 struct hist_trigger_data *hist_data;
6103 struct synth_event *se;
6104 const char *se_name;
6105 bool remove = false;
6109 lockdep_assert_held(&event_mutex);
6111 if (glob && strlen(glob)) {
6113 last_cmd_set(file, param);
6123 * separate the trigger from the filter (k:v [if filter])
6124 * allowing for whitespace in the trigger
6126 p = trigger = param;
6128 p = strstr(p, "if");
6133 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6137 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
6139 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6150 param = strstrip(p);
6151 trigger = strstrip(trigger);
6154 attrs = parse_hist_trigger_attrs(file->tr, trigger);
6156 return PTR_ERR(attrs);
6158 if (attrs->map_bits)
6159 hist_trigger_bits = attrs->map_bits;
6161 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6162 if (IS_ERR(hist_data)) {
6163 destroy_hist_trigger_attrs(attrs);
6164 return PTR_ERR(hist_data);
6167 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6169 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
6170 if (!trigger_data) {
6175 trigger_data->count = -1;
6176 trigger_data->ops = trigger_ops;
6177 trigger_data->cmd_ops = cmd_ops;
6179 INIT_LIST_HEAD(&trigger_data->list);
6180 RCU_INIT_POINTER(trigger_data->filter, NULL);
6182 trigger_data->private_data = hist_data;
6184 /* if param is non-empty, it's supposed to be a filter */
6185 if (param && cmd_ops->set_filter) {
6186 ret = cmd_ops->set_filter(param, trigger_data, file);
6192 if (!have_hist_trigger_match(trigger_data, file))
6195 if (hist_trigger_check_refs(trigger_data, file)) {
6200 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6201 se_name = trace_event_name(file->event_call);
6202 se = find_synth_event(se_name);
6209 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6211 * The above returns on success the # of triggers registered,
6212 * but if it didn't register any it returns zero. Consider no
6213 * triggers registered a failure too.
6216 if (!(attrs->pause || attrs->cont || attrs->clear))
6222 if (get_named_trigger_data(trigger_data))
6225 if (has_hist_vars(hist_data))
6226 save_hist_vars(hist_data);
6228 ret = create_actions(hist_data);
6232 ret = tracing_map_init(hist_data->map);
6236 ret = hist_trigger_enable(trigger_data, file);
6240 se_name = trace_event_name(file->event_call);
6241 se = find_synth_event(se_name);
6244 /* Just return zero, not the number of registered triggers */
6252 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6254 if (cmd_ops->set_filter)
6255 cmd_ops->set_filter(NULL, trigger_data, NULL);
6257 remove_hist_vars(hist_data);
6259 kfree(trigger_data);
6261 destroy_hist_data(hist_data);
6265 static struct event_command trigger_hist_cmd = {
6267 .trigger_type = ETT_EVENT_HIST,
6268 .flags = EVENT_CMD_FL_NEEDS_REC,
6269 .func = event_hist_trigger_func,
6270 .reg = hist_register_trigger,
6271 .unreg = hist_unregister_trigger,
6272 .unreg_all = hist_unreg_all,
6273 .get_trigger_ops = event_hist_get_trigger_ops,
6274 .set_filter = set_trigger_filter,
6277 __init int register_trigger_hist_cmd(void)
6281 ret = register_event_command(&trigger_hist_cmd);
6288 hist_enable_trigger(struct event_trigger_data *data, void *rec,
6289 struct ring_buffer_event *event)
6291 struct enable_trigger_data *enable_data = data->private_data;
6292 struct event_trigger_data *test;
6294 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
6295 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6296 if (enable_data->enable)
6297 test->paused = false;
6299 test->paused = true;
6305 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
6306 struct ring_buffer_event *event)
6311 if (data->count != -1)
6314 hist_enable_trigger(data, rec, event);
6317 static struct event_trigger_ops hist_enable_trigger_ops = {
6318 .func = hist_enable_trigger,
6319 .print = event_enable_trigger_print,
6320 .init = event_trigger_init,
6321 .free = event_enable_trigger_free,
6324 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6325 .func = hist_enable_count_trigger,
6326 .print = event_enable_trigger_print,
6327 .init = event_trigger_init,
6328 .free = event_enable_trigger_free,
6331 static struct event_trigger_ops hist_disable_trigger_ops = {
6332 .func = hist_enable_trigger,
6333 .print = event_enable_trigger_print,
6334 .init = event_trigger_init,
6335 .free = event_enable_trigger_free,
6338 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6339 .func = hist_enable_count_trigger,
6340 .print = event_enable_trigger_print,
6341 .init = event_trigger_init,
6342 .free = event_enable_trigger_free,
6345 static struct event_trigger_ops *
6346 hist_enable_get_trigger_ops(char *cmd, char *param)
6348 struct event_trigger_ops *ops;
6351 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6354 ops = param ? &hist_enable_count_trigger_ops :
6355 &hist_enable_trigger_ops;
6357 ops = param ? &hist_disable_count_trigger_ops :
6358 &hist_disable_trigger_ops;
6363 static void hist_enable_unreg_all(struct trace_event_file *file)
6365 struct event_trigger_data *test, *n;
6367 list_for_each_entry_safe(test, n, &file->triggers, list) {
6368 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6369 list_del_rcu(&test->list);
6370 update_cond_flag(file);
6371 trace_event_trigger_enable_disable(file, 0);
6372 if (test->ops->free)
6373 test->ops->free(test->ops, test);
6378 static struct event_command trigger_hist_enable_cmd = {
6379 .name = ENABLE_HIST_STR,
6380 .trigger_type = ETT_HIST_ENABLE,
6381 .func = event_enable_trigger_func,
6382 .reg = event_enable_register_trigger,
6383 .unreg = event_enable_unregister_trigger,
6384 .unreg_all = hist_enable_unreg_all,
6385 .get_trigger_ops = hist_enable_get_trigger_ops,
6386 .set_filter = set_trigger_filter,
6389 static struct event_command trigger_hist_disable_cmd = {
6390 .name = DISABLE_HIST_STR,
6391 .trigger_type = ETT_HIST_ENABLE,
6392 .func = event_enable_trigger_func,
6393 .reg = event_enable_register_trigger,
6394 .unreg = event_enable_unregister_trigger,
6395 .unreg_all = hist_enable_unreg_all,
6396 .get_trigger_ops = hist_enable_get_trigger_ops,
6397 .set_filter = set_trigger_filter,
6400 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6402 unregister_event_command(&trigger_hist_enable_cmd);
6403 unregister_event_command(&trigger_hist_disable_cmd);
6406 __init int register_trigger_hist_enable_disable_cmds(void)
6410 ret = register_event_command(&trigger_hist_enable_cmd);
6411 if (WARN_ON(ret < 0))
6413 ret = register_event_command(&trigger_hist_disable_cmd);
6414 if (WARN_ON(ret < 0))
6415 unregister_trigger_hist_enable_disable_cmds();
6420 static __init int trace_events_hist_init(void)
6422 struct dentry *entry = NULL;
6423 struct dentry *d_tracer;
6426 err = dyn_event_register(&synth_event_ops);
6428 pr_warn("Could not register synth_event_ops\n");
6432 d_tracer = tracing_init_dentry();
6433 if (IS_ERR(d_tracer)) {
6434 err = PTR_ERR(d_tracer);
6438 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
6439 NULL, &synth_events_fops);
6447 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6452 fs_initcall(trace_events_hist_init);