1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_hist - trace event hist triggers
5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
21 #include "tracing_map.h"
23 #include "trace_dynevent.h"
25 #define SYNTH_SYSTEM "synthetic"
26 #define SYNTH_FIELDS_MAX 32
28 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
31 C(NONE, "No error"), \
32 C(DUPLICATE_VAR, "Variable already defined"), \
33 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
34 C(TOO_MANY_VARS, "Too many variables defined"), \
35 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
36 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
37 C(TRIGGER_EEXIST, "Hist trigger already exists"), \
38 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
39 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
40 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
41 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
42 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
43 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
44 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
45 C(HIST_NOT_FOUND, "Matching event histogram not found"), \
46 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
47 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
48 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
49 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
50 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
51 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
52 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
53 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
54 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
55 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
56 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
57 C(TOO_MANY_PARAMS, "Too many action params"), \
58 C(PARAM_NOT_FOUND, "Couldn't find param"), \
59 C(INVALID_PARAM, "Invalid action param"), \
60 C(ACTION_NOT_FOUND, "No action found"), \
61 C(NO_SAVE_PARAMS, "No params found for save()"), \
62 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
63 C(ACTION_MISMATCH, "Handler doesn't support action"), \
64 C(NO_CLOSING_PAREN, "No closing paren found"), \
65 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
66 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
67 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
68 C(VAR_NOT_FOUND, "Couldn't find variable"), \
69 C(FIELD_NOT_FOUND, "Couldn't find field"), \
70 C(EMPTY_ASSIGNMENT, "Empty assignment"), \
71 C(INVALID_SORT_MODIFIER,"Invalid sort modifier"), \
72 C(EMPTY_SORT_FIELD, "Empty sort field"), \
73 C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \
74 C(INVALID_SORT_FIELD, "Sort field must be a key or a val"),
77 #define C(a, b) HIST_ERR_##a
84 static const char *err_text[] = { ERRORS };
88 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
89 struct tracing_map_elt *elt,
90 struct ring_buffer_event *rbe,
93 #define HIST_FIELD_OPERANDS_MAX 2
94 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
95 #define HIST_ACTIONS_MAX 8
101 FIELD_OP_UNARY_MINUS,
105 * A hist_var (histogram variable) contains variable information for
106 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
107 * flag set. A hist_var has a variable name e.g. ts0, and is
108 * associated with a given histogram trigger, as specified by
109 * hist_data. The hist_var idx is the unique index assigned to the
110 * variable by the hist trigger's tracing_map. The idx is what is
111 * used to set a variable's value and, by a variable reference, to
116 struct hist_trigger_data *hist_data;
121 struct ftrace_event_field *field;
127 unsigned int is_signed;
129 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
130 struct hist_trigger_data *hist_data;
133 * Variable fields contain variable-specific info in var.
136 enum field_op_id operator;
141 * The name field is used for EXPR and VAR_REF fields. VAR
142 * fields contain the variable name in var.name.
147 * When a histogram trigger is hit, if it has any references
148 * to variables, the values of those variables are collected
149 * into a var_ref_vals array by resolve_var_refs(). The
150 * current value of each variable is read from the tracing_map
151 * using the hist field's hist_var.idx and entered into the
152 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
154 unsigned int var_ref_idx;
158 static u64 hist_field_none(struct hist_field *field,
159 struct tracing_map_elt *elt,
160 struct ring_buffer_event *rbe,
166 static u64 hist_field_counter(struct hist_field *field,
167 struct tracing_map_elt *elt,
168 struct ring_buffer_event *rbe,
174 static u64 hist_field_string(struct hist_field *hist_field,
175 struct tracing_map_elt *elt,
176 struct ring_buffer_event *rbe,
179 char *addr = (char *)(event + hist_field->field->offset);
181 return (u64)(unsigned long)addr;
184 static u64 hist_field_dynstring(struct hist_field *hist_field,
185 struct tracing_map_elt *elt,
186 struct ring_buffer_event *rbe,
189 u32 str_item = *(u32 *)(event + hist_field->field->offset);
190 int str_loc = str_item & 0xffff;
191 char *addr = (char *)(event + str_loc);
193 return (u64)(unsigned long)addr;
196 static u64 hist_field_pstring(struct hist_field *hist_field,
197 struct tracing_map_elt *elt,
198 struct ring_buffer_event *rbe,
201 char **addr = (char **)(event + hist_field->field->offset);
203 return (u64)(unsigned long)*addr;
206 static u64 hist_field_log2(struct hist_field *hist_field,
207 struct tracing_map_elt *elt,
208 struct ring_buffer_event *rbe,
211 struct hist_field *operand = hist_field->operands[0];
213 u64 val = operand->fn(operand, elt, rbe, event);
215 return (u64) ilog2(roundup_pow_of_two(val));
218 static u64 hist_field_plus(struct hist_field *hist_field,
219 struct tracing_map_elt *elt,
220 struct ring_buffer_event *rbe,
223 struct hist_field *operand1 = hist_field->operands[0];
224 struct hist_field *operand2 = hist_field->operands[1];
226 u64 val1 = operand1->fn(operand1, elt, rbe, event);
227 u64 val2 = operand2->fn(operand2, elt, rbe, event);
232 static u64 hist_field_minus(struct hist_field *hist_field,
233 struct tracing_map_elt *elt,
234 struct ring_buffer_event *rbe,
237 struct hist_field *operand1 = hist_field->operands[0];
238 struct hist_field *operand2 = hist_field->operands[1];
240 u64 val1 = operand1->fn(operand1, elt, rbe, event);
241 u64 val2 = operand2->fn(operand2, elt, rbe, event);
246 static u64 hist_field_unary_minus(struct hist_field *hist_field,
247 struct tracing_map_elt *elt,
248 struct ring_buffer_event *rbe,
251 struct hist_field *operand = hist_field->operands[0];
253 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
254 u64 val = (u64)-sval;
259 #define DEFINE_HIST_FIELD_FN(type) \
260 static u64 hist_field_##type(struct hist_field *hist_field, \
261 struct tracing_map_elt *elt, \
262 struct ring_buffer_event *rbe, \
265 type *addr = (type *)(event + hist_field->field->offset); \
267 return (u64)(unsigned long)*addr; \
270 DEFINE_HIST_FIELD_FN(s64);
271 DEFINE_HIST_FIELD_FN(u64);
272 DEFINE_HIST_FIELD_FN(s32);
273 DEFINE_HIST_FIELD_FN(u32);
274 DEFINE_HIST_FIELD_FN(s16);
275 DEFINE_HIST_FIELD_FN(u16);
276 DEFINE_HIST_FIELD_FN(s8);
277 DEFINE_HIST_FIELD_FN(u8);
279 #define for_each_hist_field(i, hist_data) \
280 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
282 #define for_each_hist_val_field(i, hist_data) \
283 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
285 #define for_each_hist_key_field(i, hist_data) \
286 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
288 #define HIST_STACKTRACE_DEPTH 16
289 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
290 #define HIST_STACKTRACE_SKIP 5
292 #define HITCOUNT_IDX 0
293 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
295 enum hist_field_flags {
296 HIST_FIELD_FL_HITCOUNT = 1 << 0,
297 HIST_FIELD_FL_KEY = 1 << 1,
298 HIST_FIELD_FL_STRING = 1 << 2,
299 HIST_FIELD_FL_HEX = 1 << 3,
300 HIST_FIELD_FL_SYM = 1 << 4,
301 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
302 HIST_FIELD_FL_EXECNAME = 1 << 6,
303 HIST_FIELD_FL_SYSCALL = 1 << 7,
304 HIST_FIELD_FL_STACKTRACE = 1 << 8,
305 HIST_FIELD_FL_LOG2 = 1 << 9,
306 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
307 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
308 HIST_FIELD_FL_VAR = 1 << 12,
309 HIST_FIELD_FL_EXPR = 1 << 13,
310 HIST_FIELD_FL_VAR_REF = 1 << 14,
311 HIST_FIELD_FL_CPU = 1 << 15,
312 HIST_FIELD_FL_ALIAS = 1 << 16,
317 char *name[TRACING_MAP_VARS_MAX];
318 char *expr[TRACING_MAP_VARS_MAX];
321 struct hist_trigger_attrs {
331 unsigned int map_bits;
333 char *assignment_str[TRACING_MAP_VARS_MAX];
334 unsigned int n_assignments;
336 char *action_str[HIST_ACTIONS_MAX];
337 unsigned int n_actions;
339 struct var_defs var_defs;
343 struct hist_field *var;
344 struct hist_field *val;
347 struct field_var_hist {
348 struct hist_trigger_data *hist_data;
352 struct hist_trigger_data {
353 struct hist_field *fields[HIST_FIELDS_MAX];
356 unsigned int n_fields;
358 unsigned int key_size;
359 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
360 unsigned int n_sort_keys;
361 struct trace_event_file *event_file;
362 struct hist_trigger_attrs *attrs;
363 struct tracing_map *map;
364 bool enable_timestamps;
366 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
367 unsigned int n_var_refs;
369 struct action_data *actions[HIST_ACTIONS_MAX];
370 unsigned int n_actions;
372 struct field_var *field_vars[SYNTH_FIELDS_MAX];
373 unsigned int n_field_vars;
374 unsigned int n_field_var_str;
375 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
376 unsigned int n_field_var_hists;
378 struct field_var *save_vars[SYNTH_FIELDS_MAX];
379 unsigned int n_save_vars;
380 unsigned int n_save_var_str;
383 static int create_synth_event(int argc, const char **argv);
384 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
385 static int synth_event_release(struct dyn_event *ev);
386 static bool synth_event_is_busy(struct dyn_event *ev);
387 static bool synth_event_match(const char *system, const char *event,
388 int argc, const char **argv, struct dyn_event *ev);
390 static struct dyn_event_operations synth_event_ops = {
391 .create = create_synth_event,
392 .show = synth_event_show,
393 .is_busy = synth_event_is_busy,
394 .free = synth_event_release,
395 .match = synth_event_match,
408 struct dyn_event devent;
411 struct synth_field **fields;
412 unsigned int n_fields;
414 struct trace_event_class class;
415 struct trace_event_call call;
416 struct tracepoint *tp;
420 static bool is_synth_event(struct dyn_event *ev)
422 return ev->ops == &synth_event_ops;
425 static struct synth_event *to_synth_event(struct dyn_event *ev)
427 return container_of(ev, struct synth_event, devent);
430 static bool synth_event_is_busy(struct dyn_event *ev)
432 struct synth_event *event = to_synth_event(ev);
434 return event->ref != 0;
437 static bool synth_event_match(const char *system, const char *event,
438 int argc, const char **argv, struct dyn_event *ev)
440 struct synth_event *sev = to_synth_event(ev);
442 return strcmp(sev->name, event) == 0 &&
443 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
448 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
449 struct tracing_map_elt *elt, void *rec,
450 struct ring_buffer_event *rbe, void *key,
451 struct action_data *data, u64 *var_ref_vals);
453 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
468 enum handler_id handler;
469 enum action_id action;
473 unsigned int n_params;
474 char *params[SYNTH_FIELDS_MAX];
477 * When a histogram trigger is hit, the values of any
478 * references to variables, including variables being passed
479 * as parameters to synthetic events, are collected into a
480 * var_ref_vals array. This var_ref_idx array is an array of
481 * indices into the var_ref_vals array, one for each synthetic
482 * event param, and is passed to the synthetic event
485 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
486 struct synth_event *synth_event;
487 bool use_trace_keyword;
488 char *synth_event_name;
498 * var_str contains the $-unstripped variable
499 * name referenced by var_ref, and used when
500 * printing the action. Because var_ref
501 * creation is deferred to create_actions(),
502 * we need a per-action way to save it until
503 * then, thus var_str.
508 * var_ref refers to the variable being
509 * tracked e.g onmax($var).
511 struct hist_field *var_ref;
514 * track_var contains the 'invisible' tracking
515 * variable created to keep the current
518 struct hist_field *track_var;
520 check_track_val_fn_t check_val;
521 action_fn_t save_data;
530 unsigned int key_len;
532 struct tracing_map_elt elt;
534 struct action_data *action_data;
535 struct hist_trigger_data *hist_data;
538 struct hist_elt_data {
541 char *field_var_str[SYNTH_FIELDS_MAX];
544 struct snapshot_context {
545 struct tracing_map_elt *elt;
549 static void track_data_free(struct track_data *track_data)
551 struct hist_elt_data *elt_data;
556 kfree(track_data->key);
558 elt_data = track_data->elt.private_data;
560 kfree(elt_data->comm);
567 static struct track_data *track_data_alloc(unsigned int key_len,
568 struct action_data *action_data,
569 struct hist_trigger_data *hist_data)
571 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
572 struct hist_elt_data *elt_data;
575 return ERR_PTR(-ENOMEM);
577 data->key = kzalloc(key_len, GFP_KERNEL);
579 track_data_free(data);
580 return ERR_PTR(-ENOMEM);
583 data->key_len = key_len;
584 data->action_data = action_data;
585 data->hist_data = hist_data;
587 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
589 track_data_free(data);
590 return ERR_PTR(-ENOMEM);
592 data->elt.private_data = elt_data;
594 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
595 if (!elt_data->comm) {
596 track_data_free(data);
597 return ERR_PTR(-ENOMEM);
603 static char last_cmd[MAX_FILTER_STR_VAL];
604 static char last_cmd_loc[MAX_FILTER_STR_VAL];
606 static int errpos(char *str)
608 return err_pos(last_cmd, str);
611 static void last_cmd_set(struct trace_event_file *file, char *str)
613 const char *system = NULL, *name = NULL;
614 struct trace_event_call *call;
619 strcpy(last_cmd, "hist:");
620 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
623 call = file->event_call;
625 system = call->class->system;
627 name = trace_event_name(call);
634 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
637 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
639 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
643 static void hist_err_clear(void)
646 last_cmd_loc[0] = '\0';
649 struct synth_trace_event {
650 struct trace_entry ent;
654 static int synth_event_define_fields(struct trace_event_call *call)
656 struct synth_trace_event trace;
657 int offset = offsetof(typeof(trace), fields);
658 struct synth_event *event = call->data;
659 unsigned int i, size, n_u64;
664 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
665 size = event->fields[i]->size;
666 is_signed = event->fields[i]->is_signed;
667 type = event->fields[i]->type;
668 name = event->fields[i]->name;
669 ret = trace_define_field(call, type, name, offset, size,
670 is_signed, FILTER_OTHER);
674 event->fields[i]->offset = n_u64;
676 if (event->fields[i]->is_string) {
677 offset += STR_VAR_LEN_MAX;
678 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
680 offset += sizeof(u64);
685 event->n_u64 = n_u64;
690 static bool synth_field_signed(char *type)
692 if (str_has_prefix(type, "u"))
694 if (strcmp(type, "gfp_t") == 0)
700 static int synth_field_is_string(char *type)
702 if (strstr(type, "char[") != NULL)
708 static int synth_field_string_size(char *type)
710 char buf[4], *end, *start;
714 start = strstr(type, "char[");
717 start += sizeof("char[") - 1;
719 end = strchr(type, ']');
720 if (!end || end < start)
727 strncpy(buf, start, len);
730 err = kstrtouint(buf, 0, &size);
734 if (size > STR_VAR_LEN_MAX)
740 static int synth_field_size(char *type)
744 if (strcmp(type, "s64") == 0)
746 else if (strcmp(type, "u64") == 0)
748 else if (strcmp(type, "s32") == 0)
750 else if (strcmp(type, "u32") == 0)
752 else if (strcmp(type, "s16") == 0)
754 else if (strcmp(type, "u16") == 0)
756 else if (strcmp(type, "s8") == 0)
758 else if (strcmp(type, "u8") == 0)
760 else if (strcmp(type, "char") == 0)
762 else if (strcmp(type, "unsigned char") == 0)
763 size = sizeof(unsigned char);
764 else if (strcmp(type, "int") == 0)
766 else if (strcmp(type, "unsigned int") == 0)
767 size = sizeof(unsigned int);
768 else if (strcmp(type, "long") == 0)
770 else if (strcmp(type, "unsigned long") == 0)
771 size = sizeof(unsigned long);
772 else if (strcmp(type, "pid_t") == 0)
773 size = sizeof(pid_t);
774 else if (strcmp(type, "gfp_t") == 0)
775 size = sizeof(gfp_t);
776 else if (synth_field_is_string(type))
777 size = synth_field_string_size(type);
782 static const char *synth_field_fmt(char *type)
784 const char *fmt = "%llu";
786 if (strcmp(type, "s64") == 0)
788 else if (strcmp(type, "u64") == 0)
790 else if (strcmp(type, "s32") == 0)
792 else if (strcmp(type, "u32") == 0)
794 else if (strcmp(type, "s16") == 0)
796 else if (strcmp(type, "u16") == 0)
798 else if (strcmp(type, "s8") == 0)
800 else if (strcmp(type, "u8") == 0)
802 else if (strcmp(type, "char") == 0)
804 else if (strcmp(type, "unsigned char") == 0)
806 else if (strcmp(type, "int") == 0)
808 else if (strcmp(type, "unsigned int") == 0)
810 else if (strcmp(type, "long") == 0)
812 else if (strcmp(type, "unsigned long") == 0)
814 else if (strcmp(type, "pid_t") == 0)
816 else if (strcmp(type, "gfp_t") == 0)
818 else if (synth_field_is_string(type))
824 static void print_synth_event_num_val(struct trace_seq *s,
825 char *print_fmt, char *name,
826 int size, u64 val, char *space)
830 trace_seq_printf(s, print_fmt, name, (u8)val, space);
834 trace_seq_printf(s, print_fmt, name, (u16)val, space);
838 trace_seq_printf(s, print_fmt, name, (u32)val, space);
842 trace_seq_printf(s, print_fmt, name, val, space);
847 static enum print_line_t print_synth_event(struct trace_iterator *iter,
849 struct trace_event *event)
851 struct trace_array *tr = iter->tr;
852 struct trace_seq *s = &iter->seq;
853 struct synth_trace_event *entry;
854 struct synth_event *se;
855 unsigned int i, n_u64;
859 entry = (struct synth_trace_event *)iter->ent;
860 se = container_of(event, struct synth_event, call.event);
862 trace_seq_printf(s, "%s: ", se->name);
864 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
865 if (trace_seq_has_overflowed(s))
868 fmt = synth_field_fmt(se->fields[i]->type);
870 /* parameter types */
871 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
872 trace_seq_printf(s, "%s ", fmt);
874 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
876 /* parameter values */
877 if (se->fields[i]->is_string) {
878 trace_seq_printf(s, print_fmt, se->fields[i]->name,
879 (char *)&entry->fields[n_u64],
880 i == se->n_fields - 1 ? "" : " ");
881 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
883 struct trace_print_flags __flags[] = {
884 __def_gfpflag_names, {-1, NULL} };
885 char *space = (i == se->n_fields - 1 ? "" : " ");
887 print_synth_event_num_val(s, print_fmt,
890 entry->fields[n_u64],
893 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
894 trace_seq_puts(s, " (");
895 trace_print_flags_seq(s, "|",
896 entry->fields[n_u64],
898 trace_seq_putc(s, ')');
904 trace_seq_putc(s, '\n');
906 return trace_handle_return(s);
909 static struct trace_event_functions synth_event_funcs = {
910 .trace = print_synth_event
913 static notrace void trace_event_raw_event_synth(void *__data,
915 unsigned int *var_ref_idx)
917 struct trace_event_file *trace_file = __data;
918 struct synth_trace_event *entry;
919 struct trace_event_buffer fbuffer;
920 struct trace_buffer *buffer;
921 struct synth_event *event;
922 unsigned int i, n_u64, val_idx;
925 event = trace_file->event_call->data;
927 if (trace_trigger_soft_disabled(trace_file))
930 fields_size = event->n_u64 * sizeof(u64);
933 * Avoid ring buffer recursion detection, as this event
934 * is being performed within another event.
936 buffer = trace_file->tr->array_buffer.buffer;
937 ring_buffer_nest_start(buffer);
939 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
940 sizeof(*entry) + fields_size);
944 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
945 val_idx = var_ref_idx[i];
946 if (event->fields[i]->is_string) {
947 char *str_val = (char *)(long)var_ref_vals[val_idx];
948 char *str_field = (char *)&entry->fields[n_u64];
950 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
951 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
953 struct synth_field *field = event->fields[i];
954 u64 val = var_ref_vals[val_idx];
956 switch (field->size) {
958 *(u8 *)&entry->fields[n_u64] = (u8)val;
962 *(u16 *)&entry->fields[n_u64] = (u16)val;
966 *(u32 *)&entry->fields[n_u64] = (u32)val;
970 entry->fields[n_u64] = val;
977 trace_event_buffer_commit(&fbuffer);
979 ring_buffer_nest_end(buffer);
982 static void free_synth_event_print_fmt(struct trace_event_call *call)
985 kfree(call->print_fmt);
986 call->print_fmt = NULL;
990 static int __set_synth_event_print_fmt(struct synth_event *event,
997 /* When len=0, we just calculate the needed length */
998 #define LEN_OR_ZERO (len ? len - pos : 0)
1000 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
1001 for (i = 0; i < event->n_fields; i++) {
1002 fmt = synth_field_fmt(event->fields[i]->type);
1003 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
1004 event->fields[i]->name, fmt,
1005 i == event->n_fields - 1 ? "" : ", ");
1007 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
1009 for (i = 0; i < event->n_fields; i++) {
1010 pos += snprintf(buf + pos, LEN_OR_ZERO,
1011 ", REC->%s", event->fields[i]->name);
1016 /* return the length of print_fmt */
1020 static int set_synth_event_print_fmt(struct trace_event_call *call)
1022 struct synth_event *event = call->data;
1026 /* First: called with 0 length to calculate the needed length */
1027 len = __set_synth_event_print_fmt(event, NULL, 0);
1029 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1033 /* Second: actually write the @print_fmt */
1034 __set_synth_event_print_fmt(event, print_fmt, len + 1);
1035 call->print_fmt = print_fmt;
1040 static void free_synth_field(struct synth_field *field)
1047 static struct synth_field *parse_synth_field(int argc, const char **argv,
1050 struct synth_field *field;
1051 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
1054 if (field_type[0] == ';')
1057 if (!strcmp(field_type, "unsigned")) {
1059 return ERR_PTR(-EINVAL);
1060 prefix = "unsigned ";
1061 field_type = argv[1];
1062 field_name = argv[2];
1065 field_name = argv[1];
1069 field = kzalloc(sizeof(*field), GFP_KERNEL);
1071 return ERR_PTR(-ENOMEM);
1073 len = strlen(field_name);
1074 array = strchr(field_name, '[');
1076 len -= strlen(array);
1077 else if (field_name[len - 1] == ';')
1080 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1086 if (field_type[0] == ';')
1088 len = strlen(field_type) + 1;
1090 len += strlen(array);
1092 len += strlen(prefix);
1094 field->type = kzalloc(len, GFP_KERNEL);
1100 strcat(field->type, prefix);
1101 strcat(field->type, field_type);
1103 strcat(field->type, array);
1104 if (field->type[len - 1] == ';')
1105 field->type[len - 1] = '\0';
1108 field->size = synth_field_size(field->type);
1114 if (synth_field_is_string(field->type))
1115 field->is_string = true;
1117 field->is_signed = synth_field_signed(field->type);
1122 free_synth_field(field);
1123 field = ERR_PTR(ret);
1127 static void free_synth_tracepoint(struct tracepoint *tp)
1136 static struct tracepoint *alloc_synth_tracepoint(char *name)
1138 struct tracepoint *tp;
1140 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1142 return ERR_PTR(-ENOMEM);
1144 tp->name = kstrdup(name, GFP_KERNEL);
1147 return ERR_PTR(-ENOMEM);
1153 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1154 unsigned int *var_ref_idx);
1156 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1157 unsigned int *var_ref_idx)
1159 struct tracepoint *tp = event->tp;
1161 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1162 struct tracepoint_func *probe_func_ptr;
1163 synth_probe_func_t probe_func;
1166 if (!(cpu_online(raw_smp_processor_id())))
1169 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1170 if (probe_func_ptr) {
1172 probe_func = probe_func_ptr->func;
1173 __data = probe_func_ptr->data;
1174 probe_func(__data, var_ref_vals, var_ref_idx);
1175 } while ((++probe_func_ptr)->func);
1180 static struct synth_event *find_synth_event(const char *name)
1182 struct dyn_event *pos;
1183 struct synth_event *event;
1185 for_each_dyn_event(pos) {
1186 if (!is_synth_event(pos))
1188 event = to_synth_event(pos);
1189 if (strcmp(event->name, name) == 0)
1196 static struct trace_event_fields synth_event_fields_array[] = {
1197 { .type = TRACE_FUNCTION_TYPE,
1198 .define_fields = synth_event_define_fields },
1202 static int register_synth_event(struct synth_event *event)
1204 struct trace_event_call *call = &event->call;
1207 event->call.class = &event->class;
1208 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1209 if (!event->class.system) {
1214 event->tp = alloc_synth_tracepoint(event->name);
1215 if (IS_ERR(event->tp)) {
1216 ret = PTR_ERR(event->tp);
1221 INIT_LIST_HEAD(&call->class->fields);
1222 call->event.funcs = &synth_event_funcs;
1223 call->class->fields_array = synth_event_fields_array;
1225 ret = register_trace_event(&call->event);
1230 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1231 call->class->reg = trace_event_reg;
1232 call->class->probe = trace_event_raw_event_synth;
1234 call->tp = event->tp;
1236 ret = trace_add_event_call(call);
1238 pr_warn("Failed to register synthetic event: %s\n",
1239 trace_event_name(call));
1243 ret = set_synth_event_print_fmt(call);
1245 trace_remove_event_call(call);
1251 unregister_trace_event(&call->event);
1255 static int unregister_synth_event(struct synth_event *event)
1257 struct trace_event_call *call = &event->call;
1260 ret = trace_remove_event_call(call);
1265 static void free_synth_event(struct synth_event *event)
1272 for (i = 0; i < event->n_fields; i++)
1273 free_synth_field(event->fields[i]);
1275 kfree(event->fields);
1277 kfree(event->class.system);
1278 free_synth_tracepoint(event->tp);
1279 free_synth_event_print_fmt(&event->call);
1283 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1284 struct synth_field **fields)
1286 struct synth_event *event;
1289 event = kzalloc(sizeof(*event), GFP_KERNEL);
1291 event = ERR_PTR(-ENOMEM);
1295 event->name = kstrdup(name, GFP_KERNEL);
1298 event = ERR_PTR(-ENOMEM);
1302 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1303 if (!event->fields) {
1304 free_synth_event(event);
1305 event = ERR_PTR(-ENOMEM);
1309 dyn_event_init(&event->devent, &synth_event_ops);
1311 for (i = 0; i < n_fields; i++)
1312 event->fields[i] = fields[i];
1314 event->n_fields = n_fields;
1319 static void action_trace(struct hist_trigger_data *hist_data,
1320 struct tracing_map_elt *elt, void *rec,
1321 struct ring_buffer_event *rbe, void *key,
1322 struct action_data *data, u64 *var_ref_vals)
1324 struct synth_event *event = data->synth_event;
1326 trace_synth(event, var_ref_vals, data->var_ref_idx);
1329 struct hist_var_data {
1330 struct list_head list;
1331 struct hist_trigger_data *hist_data;
1334 static int synth_event_check_arg_fn(void *data)
1336 struct dynevent_arg_pair *arg_pair = data;
1339 size = synth_field_size((char *)arg_pair->lhs);
1341 return size ? 0 : -EINVAL;
1345 * synth_event_add_field - Add a new field to a synthetic event cmd
1346 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1347 * @type: The type of the new field to add
1348 * @name: The name of the new field to add
1350 * Add a new field to a synthetic event cmd object. Field ordering is in
1351 * the same order the fields are added.
1353 * See synth_field_size() for available types. If field_name contains
1354 * [n] the field is considered to be an array.
1356 * Return: 0 if successful, error otherwise.
1358 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
1361 struct dynevent_arg_pair arg_pair;
1364 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1370 dynevent_arg_pair_init(&arg_pair, 0, ';');
1372 arg_pair.lhs = type;
1373 arg_pair.rhs = name;
1375 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
1379 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1384 EXPORT_SYMBOL_GPL(synth_event_add_field);
1387 * synth_event_add_field_str - Add a new field to a synthetic event cmd
1388 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1389 * @type_name: The type and name of the new field to add, as a single string
1391 * Add a new field to a synthetic event cmd object, as a single
1392 * string. The @type_name string is expected to be of the form 'type
1393 * name', which will be appended by ';'. No sanity checking is done -
1394 * what's passed in is assumed to already be well-formed. Field
1395 * ordering is in the same order the fields are added.
1397 * See synth_field_size() for available types. If field_name contains
1398 * [n] the field is considered to be an array.
1400 * Return: 0 if successful, error otherwise.
1402 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1404 struct dynevent_arg arg;
1407 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1413 dynevent_arg_init(&arg, ';');
1415 arg.str = type_name;
1417 ret = dynevent_arg_add(cmd, &arg, NULL);
1421 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1426 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1429 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1430 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1431 * @fields: An array of type/name field descriptions
1432 * @n_fields: The number of field descriptions contained in the fields array
1434 * Add a new set of fields to a synthetic event cmd object. The event
1435 * fields that will be defined for the event should be passed in as an
1436 * array of struct synth_field_desc, and the number of elements in the
1437 * array passed in as n_fields. Field ordering will retain the
1438 * ordering given in the fields array.
1440 * See synth_field_size() for available types. If field_name contains
1441 * [n] the field is considered to be an array.
1443 * Return: 0 if successful, error otherwise.
1445 int synth_event_add_fields(struct dynevent_cmd *cmd,
1446 struct synth_field_desc *fields,
1447 unsigned int n_fields)
1452 for (i = 0; i < n_fields; i++) {
1453 if (fields[i].type == NULL || fields[i].name == NULL) {
1458 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1465 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1468 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1469 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1470 * @name: The name of the synthetic event
1471 * @mod: The module creating the event, NULL if not created from a module
1472 * @args: Variable number of arg (pairs), one pair for each field
1474 * NOTE: Users normally won't want to call this function directly, but
1475 * rather use the synth_event_gen_cmd_start() wrapper, which
1476 * automatically adds a NULL to the end of the arg list. If this
1477 * function is used directly, make sure the last arg in the variable
1480 * Generate a synthetic event command to be executed by
1481 * synth_event_gen_cmd_end(). This function can be used to generate
1482 * the complete command or only the first part of it; in the latter
1483 * case, synth_event_add_field(), synth_event_add_field_str(), or
1484 * synth_event_add_fields() can be used to add more fields following
1487 * There should be an even number variable args, each pair consisting
1488 * of a type followed by a field name.
1490 * See synth_field_size() for available types. If field_name contains
1491 * [n] the field is considered to be an array.
1493 * Return: 0 if successful, error otherwise.
1495 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1496 struct module *mod, ...)
1498 struct dynevent_arg arg;
1502 cmd->event_name = name;
1503 cmd->private_data = mod;
1505 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1508 dynevent_arg_init(&arg, 0);
1510 ret = dynevent_arg_add(cmd, &arg, NULL);
1514 va_start(args, mod);
1516 const char *type, *name;
1518 type = va_arg(args, const char *);
1521 name = va_arg(args, const char *);
1525 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1530 ret = synth_event_add_field(cmd, type, name);
1538 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1541 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1542 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1543 * @name: The name of the synthetic event
1544 * @fields: An array of type/name field descriptions
1545 * @n_fields: The number of field descriptions contained in the fields array
1547 * Generate a synthetic event command to be executed by
1548 * synth_event_gen_cmd_end(). This function can be used to generate
1549 * the complete command or only the first part of it; in the latter
1550 * case, synth_event_add_field(), synth_event_add_field_str(), or
1551 * synth_event_add_fields() can be used to add more fields following
1554 * The event fields that will be defined for the event should be
1555 * passed in as an array of struct synth_field_desc, and the number of
1556 * elements in the array passed in as n_fields. Field ordering will
1557 * retain the ordering given in the fields array.
1559 * See synth_field_size() for available types. If field_name contains
1560 * [n] the field is considered to be an array.
1562 * Return: 0 if successful, error otherwise.
1564 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1566 struct synth_field_desc *fields,
1567 unsigned int n_fields)
1569 struct dynevent_arg arg;
1573 cmd->event_name = name;
1574 cmd->private_data = mod;
1576 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1579 if (n_fields > SYNTH_FIELDS_MAX)
1582 dynevent_arg_init(&arg, 0);
1584 ret = dynevent_arg_add(cmd, &arg, NULL);
1588 for (i = 0; i < n_fields; i++) {
1589 if (fields[i].type == NULL || fields[i].name == NULL)
1592 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1599 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1601 static int __create_synth_event(int argc, const char *name, const char **argv)
1603 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1604 struct synth_event *event = NULL;
1605 int i, consumed = 0, n_fields = 0, ret = 0;
1609 * - Add synthetic event: <event_name> field[;field] ...
1610 * - Remove synthetic event: !<event_name> field[;field] ...
1611 * where 'field' = type field_name
1614 if (name[0] == '\0' || argc < 1)
1617 mutex_lock(&event_mutex);
1619 event = find_synth_event(name);
1625 for (i = 0; i < argc - 1; i++) {
1626 if (strcmp(argv[i], ";") == 0)
1628 if (n_fields == SYNTH_FIELDS_MAX) {
1633 field = parse_synth_field(argc - i, &argv[i], &consumed);
1634 if (IS_ERR(field)) {
1635 ret = PTR_ERR(field);
1638 fields[n_fields++] = field;
1642 if (i < argc && strcmp(argv[i], ";") != 0) {
1647 event = alloc_synth_event(name, n_fields, fields);
1648 if (IS_ERR(event)) {
1649 ret = PTR_ERR(event);
1653 ret = register_synth_event(event);
1655 dyn_event_add(&event->devent);
1657 free_synth_event(event);
1659 mutex_unlock(&event_mutex);
1663 for (i = 0; i < n_fields; i++)
1664 free_synth_field(fields[i]);
1670 * synth_event_create - Create a new synthetic event
1671 * @name: The name of the new sythetic event
1672 * @fields: An array of type/name field descriptions
1673 * @n_fields: The number of field descriptions contained in the fields array
1674 * @mod: The module creating the event, NULL if not created from a module
1676 * Create a new synthetic event with the given name under the
1677 * trace/events/synthetic/ directory. The event fields that will be
1678 * defined for the event should be passed in as an array of struct
1679 * synth_field_desc, and the number elements in the array passed in as
1680 * n_fields. Field ordering will retain the ordering given in the
1683 * If the new synthetic event is being created from a module, the mod
1684 * param must be non-NULL. This will ensure that the trace buffer
1685 * won't contain unreadable events.
1687 * The new synth event should be deleted using synth_event_delete()
1688 * function. The new synthetic event can be generated from modules or
1689 * other kernel code using trace_synth_event() and related functions.
1691 * Return: 0 if successful, error otherwise.
1693 int synth_event_create(const char *name, struct synth_field_desc *fields,
1694 unsigned int n_fields, struct module *mod)
1696 struct dynevent_cmd cmd;
1700 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1704 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1706 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1711 ret = synth_event_gen_cmd_end(&cmd);
1717 EXPORT_SYMBOL_GPL(synth_event_create);
1719 static int destroy_synth_event(struct synth_event *se)
1726 ret = unregister_synth_event(se);
1728 dyn_event_remove(&se->devent);
1729 free_synth_event(se);
1737 * synth_event_delete - Delete a synthetic event
1738 * @event_name: The name of the new sythetic event
1740 * Delete a synthetic event that was created with synth_event_create().
1742 * Return: 0 if successful, error otherwise.
1744 int synth_event_delete(const char *event_name)
1746 struct synth_event *se = NULL;
1747 struct module *mod = NULL;
1750 mutex_lock(&event_mutex);
1751 se = find_synth_event(event_name);
1754 ret = destroy_synth_event(se);
1756 mutex_unlock(&event_mutex);
1759 mutex_lock(&trace_types_lock);
1761 * It is safest to reset the ring buffer if the module
1762 * being unloaded registered any events that were
1763 * used. The only worry is if a new module gets
1764 * loaded, and takes on the same id as the events of
1765 * this module. When printing out the buffer, traced
1766 * events left over from this module may be passed to
1767 * the new module events and unexpected results may
1770 tracing_reset_all_online_cpus();
1771 mutex_unlock(&trace_types_lock);
1776 EXPORT_SYMBOL_GPL(synth_event_delete);
1778 static int create_or_delete_synth_event(int argc, char **argv)
1780 const char *name = argv[0];
1783 /* trace_run_command() ensures argc != 0 */
1784 if (name[0] == '!') {
1785 ret = synth_event_delete(name + 1);
1789 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1790 return ret == -ECANCELED ? -EINVAL : ret;
1793 static int synth_event_run_command(struct dynevent_cmd *cmd)
1795 struct synth_event *se;
1798 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1802 se = find_synth_event(cmd->event_name);
1806 se->mod = cmd->private_data;
1812 * synth_event_cmd_init - Initialize a synthetic event command object
1813 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1814 * @buf: A pointer to the buffer used to build the command
1815 * @maxlen: The length of the buffer passed in @buf
1817 * Initialize a synthetic event command object. Use this before
1818 * calling any of the other dyenvent_cmd functions.
1820 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1822 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1823 synth_event_run_command);
1825 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1828 __synth_event_trace_start(struct trace_event_file *file,
1829 struct synth_event_trace_state *trace_state)
1831 int entry_size, fields_size = 0;
1834 memset(trace_state, '\0', sizeof(*trace_state));
1837 * Normal event tracing doesn't get called at all unless the
1838 * ENABLED bit is set (which attaches the probe thus allowing
1839 * this code to be called, etc). Because this is called
1840 * directly by the user, we don't have that but we still need
1841 * to honor not logging when disabled. For the the iterated
1842 * trace case, we save the enabed state upon start and just
1843 * ignore the following data calls.
1845 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1846 trace_trigger_soft_disabled(file)) {
1847 trace_state->disabled = true;
1852 trace_state->event = file->event_call->data;
1854 fields_size = trace_state->event->n_u64 * sizeof(u64);
1857 * Avoid ring buffer recursion detection, as this event
1858 * is being performed within another event.
1860 trace_state->buffer = file->tr->array_buffer.buffer;
1861 ring_buffer_nest_start(trace_state->buffer);
1863 entry_size = sizeof(*trace_state->entry) + fields_size;
1864 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1867 if (!trace_state->entry) {
1868 ring_buffer_nest_end(trace_state->buffer);
1876 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1878 trace_event_buffer_commit(&trace_state->fbuffer);
1880 ring_buffer_nest_end(trace_state->buffer);
1884 * synth_event_trace - Trace a synthetic event
1885 * @file: The trace_event_file representing the synthetic event
1886 * @n_vals: The number of values in vals
1887 * @args: Variable number of args containing the event values
1889 * Trace a synthetic event using the values passed in the variable
1892 * The argument list should be a list 'n_vals' u64 values. The number
1893 * of vals must match the number of field in the synthetic event, and
1894 * must be in the same order as the synthetic event fields.
1896 * All vals should be cast to u64, and string vals are just pointers
1897 * to strings, cast to u64. Strings will be copied into space
1898 * reserved in the event for the string, using these pointers.
1900 * Return: 0 on success, err otherwise.
1902 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1904 struct synth_event_trace_state state;
1905 unsigned int i, n_u64;
1909 ret = __synth_event_trace_start(file, &state);
1912 ret = 0; /* just disabled, not really an error */
1916 if (n_vals != state.event->n_fields) {
1921 va_start(args, n_vals);
1922 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1925 val = va_arg(args, u64);
1927 if (state.event->fields[i]->is_string) {
1928 char *str_val = (char *)(long)val;
1929 char *str_field = (char *)&state.entry->fields[n_u64];
1931 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1932 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1934 struct synth_field *field = state.event->fields[i];
1936 switch (field->size) {
1938 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1942 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1946 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1950 state.entry->fields[n_u64] = val;
1958 __synth_event_trace_end(&state);
1962 EXPORT_SYMBOL_GPL(synth_event_trace);
1965 * synth_event_trace_array - Trace a synthetic event from an array
1966 * @file: The trace_event_file representing the synthetic event
1967 * @vals: Array of values
1968 * @n_vals: The number of values in vals
1970 * Trace a synthetic event using the values passed in as 'vals'.
1972 * The 'vals' array is just an array of 'n_vals' u64. The number of
1973 * vals must match the number of field in the synthetic event, and
1974 * must be in the same order as the synthetic event fields.
1976 * All vals should be cast to u64, and string vals are just pointers
1977 * to strings, cast to u64. Strings will be copied into space
1978 * reserved in the event for the string, using these pointers.
1980 * Return: 0 on success, err otherwise.
1982 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1983 unsigned int n_vals)
1985 struct synth_event_trace_state state;
1986 unsigned int i, n_u64;
1989 ret = __synth_event_trace_start(file, &state);
1992 ret = 0; /* just disabled, not really an error */
1996 if (n_vals != state.event->n_fields) {
2001 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
2002 if (state.event->fields[i]->is_string) {
2003 char *str_val = (char *)(long)vals[i];
2004 char *str_field = (char *)&state.entry->fields[n_u64];
2006 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2007 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
2009 struct synth_field *field = state.event->fields[i];
2012 switch (field->size) {
2014 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
2018 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
2022 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
2026 state.entry->fields[n_u64] = val;
2033 __synth_event_trace_end(&state);
2037 EXPORT_SYMBOL_GPL(synth_event_trace_array);
2040 * synth_event_trace_start - Start piecewise synthetic event trace
2041 * @file: The trace_event_file representing the synthetic event
2042 * @trace_state: A pointer to object tracking the piecewise trace state
2044 * Start the trace of a synthetic event field-by-field rather than all
2047 * This function 'opens' an event trace, which means space is reserved
2048 * for the event in the trace buffer, after which the event's
2049 * individual field values can be set through either
2050 * synth_event_add_next_val() or synth_event_add_val().
2052 * A pointer to a trace_state object is passed in, which will keep
2053 * track of the current event trace state until the event trace is
2054 * closed (and the event finally traced) using
2055 * synth_event_trace_end().
2057 * Note that synth_event_trace_end() must be called after all values
2058 * have been added for each event trace, regardless of whether adding
2059 * all field values succeeded or not.
2061 * Note also that for a given event trace, all fields must be added
2062 * using either synth_event_add_next_val() or synth_event_add_val()
2063 * but not both together or interleaved.
2065 * Return: 0 on success, err otherwise.
2067 int synth_event_trace_start(struct trace_event_file *file,
2068 struct synth_event_trace_state *trace_state)
2075 ret = __synth_event_trace_start(file, trace_state);
2077 ret = 0; /* just disabled, not really an error */
2081 EXPORT_SYMBOL_GPL(synth_event_trace_start);
2083 static int __synth_event_add_val(const char *field_name, u64 val,
2084 struct synth_event_trace_state *trace_state)
2086 struct synth_field *field = NULL;
2087 struct synth_trace_event *entry;
2088 struct synth_event *event;
2096 /* can't mix add_next_synth_val() with add_synth_val() */
2098 if (trace_state->add_next) {
2102 trace_state->add_name = true;
2104 if (trace_state->add_name) {
2108 trace_state->add_next = true;
2111 if (trace_state->disabled)
2114 event = trace_state->event;
2115 if (trace_state->add_name) {
2116 for (i = 0; i < event->n_fields; i++) {
2117 field = event->fields[i];
2118 if (strcmp(field->name, field_name) == 0)
2126 if (trace_state->cur_field >= event->n_fields) {
2130 field = event->fields[trace_state->cur_field++];
2133 entry = trace_state->entry;
2134 if (field->is_string) {
2135 char *str_val = (char *)(long)val;
2143 str_field = (char *)&entry->fields[field->offset];
2144 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2146 switch (field->size) {
2148 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
2152 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
2156 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
2160 trace_state->entry->fields[field->offset] = val;
2169 * synth_event_add_next_val - Add the next field's value to an open synth trace
2170 * @val: The value to set the next field to
2171 * @trace_state: A pointer to object tracking the piecewise trace state
2173 * Set the value of the next field in an event that's been opened by
2174 * synth_event_trace_start().
2176 * The val param should be the value cast to u64. If the value points
2177 * to a string, the val param should be a char * cast to u64.
2179 * This function assumes all the fields in an event are to be set one
2180 * after another - successive calls to this function are made, one for
2181 * each field, in the order of the fields in the event, until all
2182 * fields have been set. If you'd rather set each field individually
2183 * without regard to ordering, synth_event_add_val() can be used
2186 * Note however that synth_event_add_next_val() and
2187 * synth_event_add_val() can't be intermixed for a given event trace -
2188 * one or the other but not both can be used at the same time.
2190 * Note also that synth_event_trace_end() must be called after all
2191 * values have been added for each event trace, regardless of whether
2192 * adding all field values succeeded or not.
2194 * Return: 0 on success, err otherwise.
2196 int synth_event_add_next_val(u64 val,
2197 struct synth_event_trace_state *trace_state)
2199 return __synth_event_add_val(NULL, val, trace_state);
2201 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2204 * synth_event_add_val - Add a named field's value to an open synth trace
2205 * @field_name: The name of the synthetic event field value to set
2206 * @val: The value to set the next field to
2207 * @trace_state: A pointer to object tracking the piecewise trace state
2209 * Set the value of the named field in an event that's been opened by
2210 * synth_event_trace_start().
2212 * The val param should be the value cast to u64. If the value points
2213 * to a string, the val param should be a char * cast to u64.
2215 * This function looks up the field name, and if found, sets the field
2216 * to the specified value. This lookup makes this function more
2217 * expensive than synth_event_add_next_val(), so use that or the
2218 * none-piecewise synth_event_trace() instead if efficiency is more
2221 * Note however that synth_event_add_next_val() and
2222 * synth_event_add_val() can't be intermixed for a given event trace -
2223 * one or the other but not both can be used at the same time.
2225 * Note also that synth_event_trace_end() must be called after all
2226 * values have been added for each event trace, regardless of whether
2227 * adding all field values succeeded or not.
2229 * Return: 0 on success, err otherwise.
2231 int synth_event_add_val(const char *field_name, u64 val,
2232 struct synth_event_trace_state *trace_state)
2234 return __synth_event_add_val(field_name, val, trace_state);
2236 EXPORT_SYMBOL_GPL(synth_event_add_val);
2239 * synth_event_trace_end - End piecewise synthetic event trace
2240 * @trace_state: A pointer to object tracking the piecewise trace state
2242 * End the trace of a synthetic event opened by
2243 * synth_event_trace__start().
2245 * This function 'closes' an event trace, which basically means that
2246 * it commits the reserved event and cleans up other loose ends.
2248 * A pointer to a trace_state object is passed in, which will keep
2249 * track of the current event trace state opened with
2250 * synth_event_trace_start().
2252 * Note that this function must be called after all values have been
2253 * added for each event trace, regardless of whether adding all field
2254 * values succeeded or not.
2256 * Return: 0 on success, err otherwise.
2258 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2263 __synth_event_trace_end(trace_state);
2267 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2269 static int create_synth_event(int argc, const char **argv)
2271 const char *name = argv[0];
2274 if (name[0] != 's' || name[1] != ':')
2278 /* This interface accepts group name prefix */
2279 if (strchr(name, '/')) {
2280 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2285 return __create_synth_event(argc - 1, name, argv + 1);
2288 static int synth_event_release(struct dyn_event *ev)
2290 struct synth_event *event = to_synth_event(ev);
2296 ret = unregister_synth_event(event);
2300 dyn_event_remove(ev);
2301 free_synth_event(event);
2305 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2307 struct synth_field *field;
2310 seq_printf(m, "%s\t", event->name);
2312 for (i = 0; i < event->n_fields; i++) {
2313 field = event->fields[i];
2315 /* parameter values */
2316 seq_printf(m, "%s %s%s", field->type, field->name,
2317 i == event->n_fields - 1 ? "" : "; ");
2325 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2327 struct synth_event *event = to_synth_event(ev);
2329 seq_printf(m, "s:%s/", event->class.system);
2331 return __synth_event_show(m, event);
2334 static int synth_events_seq_show(struct seq_file *m, void *v)
2336 struct dyn_event *ev = v;
2338 if (!is_synth_event(ev))
2341 return __synth_event_show(m, to_synth_event(ev));
2344 static const struct seq_operations synth_events_seq_op = {
2345 .start = dyn_event_seq_start,
2346 .next = dyn_event_seq_next,
2347 .stop = dyn_event_seq_stop,
2348 .show = synth_events_seq_show,
2351 static int synth_events_open(struct inode *inode, struct file *file)
2355 ret = security_locked_down(LOCKDOWN_TRACEFS);
2359 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2360 ret = dyn_events_release_all(&synth_event_ops);
2365 return seq_open(file, &synth_events_seq_op);
2368 static ssize_t synth_events_write(struct file *file,
2369 const char __user *buffer,
2370 size_t count, loff_t *ppos)
2372 return trace_parse_run_command(file, buffer, count, ppos,
2373 create_or_delete_synth_event);
2376 static const struct file_operations synth_events_fops = {
2377 .open = synth_events_open,
2378 .write = synth_events_write,
2380 .llseek = seq_lseek,
2381 .release = seq_release,
2384 static u64 hist_field_timestamp(struct hist_field *hist_field,
2385 struct tracing_map_elt *elt,
2386 struct ring_buffer_event *rbe,
2389 struct hist_trigger_data *hist_data = hist_field->hist_data;
2390 struct trace_array *tr = hist_data->event_file->tr;
2392 u64 ts = ring_buffer_event_time_stamp(rbe);
2394 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
2400 static u64 hist_field_cpu(struct hist_field *hist_field,
2401 struct tracing_map_elt *elt,
2402 struct ring_buffer_event *rbe,
2405 int cpu = smp_processor_id();
2411 * check_field_for_var_ref - Check if a VAR_REF field references a variable
2412 * @hist_field: The VAR_REF field to check
2413 * @var_data: The hist trigger that owns the variable
2414 * @var_idx: The trigger variable identifier
2416 * Check the given VAR_REF field to see whether or not it references
2417 * the given variable associated with the given trigger.
2419 * Return: The VAR_REF field if it does reference the variable, NULL if not
2421 static struct hist_field *
2422 check_field_for_var_ref(struct hist_field *hist_field,
2423 struct hist_trigger_data *var_data,
2424 unsigned int var_idx)
2426 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
2428 if (hist_field && hist_field->var.idx == var_idx &&
2429 hist_field->var.hist_data == var_data)
2436 * find_var_ref - Check if a trigger has a reference to a trigger variable
2437 * @hist_data: The hist trigger that might have a reference to the variable
2438 * @var_data: The hist trigger that owns the variable
2439 * @var_idx: The trigger variable identifier
2441 * Check the list of var_refs[] on the first hist trigger to see
2442 * whether any of them are references to the variable on the second
2445 * Return: The VAR_REF field referencing the variable if so, NULL if not
2447 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
2448 struct hist_trigger_data *var_data,
2449 unsigned int var_idx)
2451 struct hist_field *hist_field;
2454 for (i = 0; i < hist_data->n_var_refs; i++) {
2455 hist_field = hist_data->var_refs[i];
2456 if (check_field_for_var_ref(hist_field, var_data, var_idx))
2464 * find_any_var_ref - Check if there is a reference to a given trigger variable
2465 * @hist_data: The hist trigger
2466 * @var_idx: The trigger variable identifier
2468 * Check to see whether the given variable is currently referenced by
2469 * any other trigger.
2471 * The trigger the variable is defined on is explicitly excluded - the
2472 * assumption being that a self-reference doesn't prevent a trigger
2473 * from being removed.
2475 * Return: The VAR_REF field referencing the variable if so, NULL if not
2477 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
2478 unsigned int var_idx)
2480 struct trace_array *tr = hist_data->event_file->tr;
2481 struct hist_field *found = NULL;
2482 struct hist_var_data *var_data;
2484 list_for_each_entry(var_data, &tr->hist_vars, list) {
2485 if (var_data->hist_data == hist_data)
2487 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
2496 * check_var_refs - Check if there is a reference to any of trigger's variables
2497 * @hist_data: The hist trigger
2499 * A trigger can define one or more variables. If any one of them is
2500 * currently referenced by any other trigger, this function will
2503 * Typically used to determine whether or not a trigger can be removed
2504 * - if there are any references to a trigger's variables, it cannot.
2506 * Return: True if there is a reference to any of trigger's variables
2508 static bool check_var_refs(struct hist_trigger_data *hist_data)
2510 struct hist_field *field;
2514 for_each_hist_field(i, hist_data) {
2515 field = hist_data->fields[i];
2516 if (field && field->flags & HIST_FIELD_FL_VAR) {
2517 if (find_any_var_ref(hist_data, field->var.idx)) {
2527 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
2529 struct trace_array *tr = hist_data->event_file->tr;
2530 struct hist_var_data *var_data, *found = NULL;
2532 list_for_each_entry(var_data, &tr->hist_vars, list) {
2533 if (var_data->hist_data == hist_data) {
2542 static bool field_has_hist_vars(struct hist_field *hist_field,
2553 if (hist_field->flags & HIST_FIELD_FL_VAR ||
2554 hist_field->flags & HIST_FIELD_FL_VAR_REF)
2557 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
2558 struct hist_field *operand;
2560 operand = hist_field->operands[i];
2561 if (field_has_hist_vars(operand, level + 1))
2568 static bool has_hist_vars(struct hist_trigger_data *hist_data)
2570 struct hist_field *hist_field;
2573 for_each_hist_field(i, hist_data) {
2574 hist_field = hist_data->fields[i];
2575 if (field_has_hist_vars(hist_field, 0))
2582 static int save_hist_vars(struct hist_trigger_data *hist_data)
2584 struct trace_array *tr = hist_data->event_file->tr;
2585 struct hist_var_data *var_data;
2587 var_data = find_hist_vars(hist_data);
2591 if (tracing_check_open_get_tr(tr))
2594 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
2596 trace_array_put(tr);
2600 var_data->hist_data = hist_data;
2601 list_add(&var_data->list, &tr->hist_vars);
2606 static void remove_hist_vars(struct hist_trigger_data *hist_data)
2608 struct trace_array *tr = hist_data->event_file->tr;
2609 struct hist_var_data *var_data;
2611 var_data = find_hist_vars(hist_data);
2615 if (WARN_ON(check_var_refs(hist_data)))
2618 list_del(&var_data->list);
2622 trace_array_put(tr);
2625 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
2626 const char *var_name)
2628 struct hist_field *hist_field, *found = NULL;
2631 for_each_hist_field(i, hist_data) {
2632 hist_field = hist_data->fields[i];
2633 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
2634 strcmp(hist_field->var.name, var_name) == 0) {
2643 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
2644 struct trace_event_file *file,
2645 const char *var_name)
2647 struct hist_trigger_data *test_data;
2648 struct event_trigger_data *test;
2649 struct hist_field *hist_field;
2651 lockdep_assert_held(&event_mutex);
2653 hist_field = find_var_field(hist_data, var_name);
2657 list_for_each_entry(test, &file->triggers, list) {
2658 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2659 test_data = test->private_data;
2660 hist_field = find_var_field(test_data, var_name);
2669 static struct trace_event_file *find_var_file(struct trace_array *tr,
2674 struct hist_trigger_data *var_hist_data;
2675 struct hist_var_data *var_data;
2676 struct trace_event_file *file, *found = NULL;
2679 return find_event_file(tr, system, event_name);
2681 list_for_each_entry(var_data, &tr->hist_vars, list) {
2682 var_hist_data = var_data->hist_data;
2683 file = var_hist_data->event_file;
2687 if (find_var_field(var_hist_data, var_name)) {
2689 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
2700 static struct hist_field *find_file_var(struct trace_event_file *file,
2701 const char *var_name)
2703 struct hist_trigger_data *test_data;
2704 struct event_trigger_data *test;
2705 struct hist_field *hist_field;
2707 lockdep_assert_held(&event_mutex);
2709 list_for_each_entry(test, &file->triggers, list) {
2710 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2711 test_data = test->private_data;
2712 hist_field = find_var_field(test_data, var_name);
2721 static struct hist_field *
2722 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
2724 struct trace_array *tr = hist_data->event_file->tr;
2725 struct hist_field *hist_field, *found = NULL;
2726 struct trace_event_file *file;
2729 for (i = 0; i < hist_data->n_actions; i++) {
2730 struct action_data *data = hist_data->actions[i];
2732 if (data->handler == HANDLER_ONMATCH) {
2733 char *system = data->match_data.event_system;
2734 char *event_name = data->match_data.event;
2736 file = find_var_file(tr, system, event_name, var_name);
2739 hist_field = find_file_var(file, var_name);
2742 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
2744 return ERR_PTR(-EINVAL);
2754 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
2759 struct trace_array *tr = hist_data->event_file->tr;
2760 struct hist_field *hist_field = NULL;
2761 struct trace_event_file *file;
2763 if (!system || !event_name) {
2764 hist_field = find_match_var(hist_data, var_name);
2765 if (IS_ERR(hist_field))
2771 file = find_var_file(tr, system, event_name, var_name);
2775 hist_field = find_file_var(file, var_name);
2780 static u64 hist_field_var_ref(struct hist_field *hist_field,
2781 struct tracing_map_elt *elt,
2782 struct ring_buffer_event *rbe,
2785 struct hist_elt_data *elt_data;
2788 if (WARN_ON_ONCE(!elt))
2791 elt_data = elt->private_data;
2792 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
2797 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
2798 u64 *var_ref_vals, bool self)
2800 struct hist_trigger_data *var_data;
2801 struct tracing_map_elt *var_elt;
2802 struct hist_field *hist_field;
2803 unsigned int i, var_idx;
2804 bool resolved = true;
2807 for (i = 0; i < hist_data->n_var_refs; i++) {
2808 hist_field = hist_data->var_refs[i];
2809 var_idx = hist_field->var.idx;
2810 var_data = hist_field->var.hist_data;
2812 if (var_data == NULL) {
2817 if ((self && var_data != hist_data) ||
2818 (!self && var_data == hist_data))
2821 var_elt = tracing_map_lookup(var_data->map, key);
2827 if (!tracing_map_var_set(var_elt, var_idx)) {
2832 if (self || !hist_field->read_once)
2833 var_val = tracing_map_read_var(var_elt, var_idx);
2835 var_val = tracing_map_read_var_once(var_elt, var_idx);
2837 var_ref_vals[i] = var_val;
2843 static const char *hist_field_name(struct hist_field *field,
2846 const char *field_name = "";
2852 field_name = field->field->name;
2853 else if (field->flags & HIST_FIELD_FL_LOG2 ||
2854 field->flags & HIST_FIELD_FL_ALIAS)
2855 field_name = hist_field_name(field->operands[0], ++level);
2856 else if (field->flags & HIST_FIELD_FL_CPU)
2858 else if (field->flags & HIST_FIELD_FL_EXPR ||
2859 field->flags & HIST_FIELD_FL_VAR_REF) {
2860 if (field->system) {
2861 static char full_name[MAX_FILTER_STR_VAL];
2863 strcat(full_name, field->system);
2864 strcat(full_name, ".");
2865 strcat(full_name, field->event_name);
2866 strcat(full_name, ".");
2867 strcat(full_name, field->name);
2868 field_name = full_name;
2870 field_name = field->name;
2871 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
2872 field_name = "common_timestamp";
2874 if (field_name == NULL)
2880 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
2882 hist_field_fn_t fn = NULL;
2884 switch (field_size) {
2886 if (field_is_signed)
2887 fn = hist_field_s64;
2889 fn = hist_field_u64;
2892 if (field_is_signed)
2893 fn = hist_field_s32;
2895 fn = hist_field_u32;
2898 if (field_is_signed)
2899 fn = hist_field_s16;
2901 fn = hist_field_u16;
2904 if (field_is_signed)
2914 static int parse_map_size(char *str)
2916 unsigned long size, map_bits;
2919 ret = kstrtoul(str, 0, &size);
2923 map_bits = ilog2(roundup_pow_of_two(size));
2924 if (map_bits < TRACING_MAP_BITS_MIN ||
2925 map_bits > TRACING_MAP_BITS_MAX)
2933 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2940 for (i = 0; i < attrs->n_assignments; i++)
2941 kfree(attrs->assignment_str[i]);
2943 for (i = 0; i < attrs->n_actions; i++)
2944 kfree(attrs->action_str[i]);
2947 kfree(attrs->sort_key_str);
2948 kfree(attrs->keys_str);
2949 kfree(attrs->vals_str);
2950 kfree(attrs->clock);
2954 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2958 if (attrs->n_actions >= HIST_ACTIONS_MAX)
2961 if ((str_has_prefix(str, "onmatch(")) ||
2962 (str_has_prefix(str, "onmax(")) ||
2963 (str_has_prefix(str, "onchange("))) {
2964 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2965 if (!attrs->action_str[attrs->n_actions]) {
2975 static int parse_assignment(struct trace_array *tr,
2976 char *str, struct hist_trigger_attrs *attrs)
2980 if ((len = str_has_prefix(str, "key=")) ||
2981 (len = str_has_prefix(str, "keys="))) {
2982 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
2983 if (!attrs->keys_str) {
2987 } else if ((len = str_has_prefix(str, "val=")) ||
2988 (len = str_has_prefix(str, "vals=")) ||
2989 (len = str_has_prefix(str, "values="))) {
2990 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
2991 if (!attrs->vals_str) {
2995 } else if ((len = str_has_prefix(str, "sort="))) {
2996 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
2997 if (!attrs->sort_key_str) {
3001 } else if (str_has_prefix(str, "name=")) {
3002 attrs->name = kstrdup(str, GFP_KERNEL);
3007 } else if ((len = str_has_prefix(str, "clock="))) {
3010 str = strstrip(str);
3011 attrs->clock = kstrdup(str, GFP_KERNEL);
3012 if (!attrs->clock) {
3016 } else if ((len = str_has_prefix(str, "size="))) {
3017 int map_bits = parse_map_size(str + len);
3023 attrs->map_bits = map_bits;
3027 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
3028 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
3033 assignment = kstrdup(str, GFP_KERNEL);
3039 attrs->assignment_str[attrs->n_assignments++] = assignment;
3045 static struct hist_trigger_attrs *
3046 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
3048 struct hist_trigger_attrs *attrs;
3051 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
3053 return ERR_PTR(-ENOMEM);
3055 while (trigger_str) {
3056 char *str = strsep(&trigger_str, ":");
3059 rhs = strchr(str, '=');
3061 if (!strlen(++rhs)) {
3063 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
3066 ret = parse_assignment(tr, str, attrs);
3069 } else if (strcmp(str, "pause") == 0)
3070 attrs->pause = true;
3071 else if ((strcmp(str, "cont") == 0) ||
3072 (strcmp(str, "continue") == 0))
3074 else if (strcmp(str, "clear") == 0)
3075 attrs->clear = true;
3077 ret = parse_action(str, attrs);
3083 if (!attrs->keys_str) {
3088 if (!attrs->clock) {
3089 attrs->clock = kstrdup("global", GFP_KERNEL);
3090 if (!attrs->clock) {
3098 destroy_hist_trigger_attrs(attrs);
3100 return ERR_PTR(ret);
3103 static inline void save_comm(char *comm, struct task_struct *task)
3106 strcpy(comm, "<idle>");
3110 if (WARN_ON_ONCE(task->pid < 0)) {
3111 strcpy(comm, "<XXX>");
3115 strncpy(comm, task->comm, TASK_COMM_LEN);
3118 static void hist_elt_data_free(struct hist_elt_data *elt_data)
3122 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
3123 kfree(elt_data->field_var_str[i]);
3125 kfree(elt_data->comm);
3129 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
3131 struct hist_elt_data *elt_data = elt->private_data;
3133 hist_elt_data_free(elt_data);
3136 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
3138 struct hist_trigger_data *hist_data = elt->map->private_data;
3139 unsigned int size = TASK_COMM_LEN;
3140 struct hist_elt_data *elt_data;
3141 struct hist_field *key_field;
3142 unsigned int i, n_str;
3144 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
3148 for_each_hist_key_field(i, hist_data) {
3149 key_field = hist_data->fields[i];
3151 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
3152 elt_data->comm = kzalloc(size, GFP_KERNEL);
3153 if (!elt_data->comm) {
3161 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
3163 size = STR_VAR_LEN_MAX;
3165 for (i = 0; i < n_str; i++) {
3166 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
3167 if (!elt_data->field_var_str[i]) {
3168 hist_elt_data_free(elt_data);
3173 elt->private_data = elt_data;
3178 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
3180 struct hist_elt_data *elt_data = elt->private_data;
3183 save_comm(elt_data->comm, current);
3186 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
3187 .elt_alloc = hist_trigger_elt_data_alloc,
3188 .elt_free = hist_trigger_elt_data_free,
3189 .elt_init = hist_trigger_elt_data_init,
3192 static const char *get_hist_field_flags(struct hist_field *hist_field)
3194 const char *flags_str = NULL;
3196 if (hist_field->flags & HIST_FIELD_FL_HEX)
3198 else if (hist_field->flags & HIST_FIELD_FL_SYM)
3200 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
3201 flags_str = "sym-offset";
3202 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
3203 flags_str = "execname";
3204 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
3205 flags_str = "syscall";
3206 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
3208 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3209 flags_str = "usecs";
3214 static void expr_field_str(struct hist_field *field, char *expr)
3216 if (field->flags & HIST_FIELD_FL_VAR_REF)
3219 strcat(expr, hist_field_name(field, 0));
3221 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
3222 const char *flags_str = get_hist_field_flags(field);
3226 strcat(expr, flags_str);
3231 static char *expr_str(struct hist_field *field, unsigned int level)
3238 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3242 if (!field->operands[0]) {
3243 expr_field_str(field, expr);
3247 if (field->operator == FIELD_OP_UNARY_MINUS) {
3251 subexpr = expr_str(field->operands[0], ++level);
3256 strcat(expr, subexpr);
3264 expr_field_str(field->operands[0], expr);
3266 switch (field->operator) {
3267 case FIELD_OP_MINUS:
3278 expr_field_str(field->operands[1], expr);
3283 static int contains_operator(char *str)
3285 enum field_op_id field_op = FIELD_OP_NONE;
3288 op = strpbrk(str, "+-");
3290 return FIELD_OP_NONE;
3295 field_op = FIELD_OP_UNARY_MINUS;
3297 field_op = FIELD_OP_MINUS;
3300 field_op = FIELD_OP_PLUS;
3309 static void get_hist_field(struct hist_field *hist_field)
3314 static void __destroy_hist_field(struct hist_field *hist_field)
3316 if (--hist_field->ref > 1)
3319 kfree(hist_field->var.name);
3320 kfree(hist_field->name);
3321 kfree(hist_field->type);
3323 kfree(hist_field->system);
3324 kfree(hist_field->event_name);
3329 static void destroy_hist_field(struct hist_field *hist_field,
3340 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
3341 return; /* var refs will be destroyed separately */
3343 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
3344 destroy_hist_field(hist_field->operands[i], level + 1);
3346 __destroy_hist_field(hist_field);
3349 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
3350 struct ftrace_event_field *field,
3351 unsigned long flags,
3354 struct hist_field *hist_field;
3356 if (field && is_function_field(field))
3359 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3363 hist_field->ref = 1;
3365 hist_field->hist_data = hist_data;
3367 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
3368 goto out; /* caller will populate */
3370 if (flags & HIST_FIELD_FL_VAR_REF) {
3371 hist_field->fn = hist_field_var_ref;
3375 if (flags & HIST_FIELD_FL_HITCOUNT) {
3376 hist_field->fn = hist_field_counter;
3377 hist_field->size = sizeof(u64);
3378 hist_field->type = kstrdup("u64", GFP_KERNEL);
3379 if (!hist_field->type)
3384 if (flags & HIST_FIELD_FL_STACKTRACE) {
3385 hist_field->fn = hist_field_none;
3389 if (flags & HIST_FIELD_FL_LOG2) {
3390 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
3391 hist_field->fn = hist_field_log2;
3392 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
3393 hist_field->size = hist_field->operands[0]->size;
3394 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
3395 if (!hist_field->type)
3400 if (flags & HIST_FIELD_FL_TIMESTAMP) {
3401 hist_field->fn = hist_field_timestamp;
3402 hist_field->size = sizeof(u64);
3403 hist_field->type = kstrdup("u64", GFP_KERNEL);
3404 if (!hist_field->type)
3409 if (flags & HIST_FIELD_FL_CPU) {
3410 hist_field->fn = hist_field_cpu;
3411 hist_field->size = sizeof(int);
3412 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
3413 if (!hist_field->type)
3418 if (WARN_ON_ONCE(!field))
3421 if (is_string_field(field)) {
3422 flags |= HIST_FIELD_FL_STRING;
3424 hist_field->size = MAX_FILTER_STR_VAL;
3425 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3426 if (!hist_field->type)
3429 if (field->filter_type == FILTER_STATIC_STRING)
3430 hist_field->fn = hist_field_string;
3431 else if (field->filter_type == FILTER_DYN_STRING)
3432 hist_field->fn = hist_field_dynstring;
3434 hist_field->fn = hist_field_pstring;
3436 hist_field->size = field->size;
3437 hist_field->is_signed = field->is_signed;
3438 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3439 if (!hist_field->type)
3442 hist_field->fn = select_value_fn(field->size,
3444 if (!hist_field->fn) {
3445 destroy_hist_field(hist_field, 0);
3450 hist_field->field = field;
3451 hist_field->flags = flags;
3454 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
3455 if (!hist_field->var.name)
3461 destroy_hist_field(hist_field, 0);
3465 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
3469 for (i = 0; i < HIST_FIELDS_MAX; i++) {
3470 if (hist_data->fields[i]) {
3471 destroy_hist_field(hist_data->fields[i], 0);
3472 hist_data->fields[i] = NULL;
3476 for (i = 0; i < hist_data->n_var_refs; i++) {
3477 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
3478 __destroy_hist_field(hist_data->var_refs[i]);
3479 hist_data->var_refs[i] = NULL;
3483 static int init_var_ref(struct hist_field *ref_field,
3484 struct hist_field *var_field,
3485 char *system, char *event_name)
3489 ref_field->var.idx = var_field->var.idx;
3490 ref_field->var.hist_data = var_field->hist_data;
3491 ref_field->size = var_field->size;
3492 ref_field->is_signed = var_field->is_signed;
3493 ref_field->flags |= var_field->flags &
3494 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3497 ref_field->system = kstrdup(system, GFP_KERNEL);
3498 if (!ref_field->system)
3503 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
3504 if (!ref_field->event_name) {
3510 if (var_field->var.name) {
3511 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
3512 if (!ref_field->name) {
3516 } else if (var_field->name) {
3517 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
3518 if (!ref_field->name) {
3524 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
3525 if (!ref_field->type) {
3532 kfree(ref_field->system);
3533 kfree(ref_field->event_name);
3534 kfree(ref_field->name);
3539 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
3540 struct hist_field *var_field)
3542 struct hist_field *ref_field;
3545 for (i = 0; i < hist_data->n_var_refs; i++) {
3546 ref_field = hist_data->var_refs[i];
3547 if (ref_field->var.idx == var_field->var.idx &&
3548 ref_field->var.hist_data == var_field->hist_data)
3556 * create_var_ref - Create a variable reference and attach it to trigger
3557 * @hist_data: The trigger that will be referencing the variable
3558 * @var_field: The VAR field to create a reference to
3559 * @system: The optional system string
3560 * @event_name: The optional event_name string
3562 * Given a variable hist_field, create a VAR_REF hist_field that
3563 * represents a reference to it.
3565 * This function also adds the reference to the trigger that
3566 * now references the variable.
3568 * Return: The VAR_REF field if successful, NULL if not
3570 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
3571 struct hist_field *var_field,
3572 char *system, char *event_name)
3574 unsigned long flags = HIST_FIELD_FL_VAR_REF;
3575 struct hist_field *ref_field;
3578 /* Check if the variable already exists */
3579 for (i = 0; i < hist_data->n_var_refs; i++) {
3580 ref_field = hist_data->var_refs[i];
3581 if (ref_field->var.idx == var_field->var.idx &&
3582 ref_field->var.hist_data == var_field->hist_data) {
3583 get_hist_field(ref_field);
3588 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
3590 if (init_var_ref(ref_field, var_field, system, event_name)) {
3591 destroy_hist_field(ref_field, 0);
3595 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3596 ref_field->var_ref_idx = hist_data->n_var_refs++;
3602 static bool is_var_ref(char *var_name)
3604 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
3610 static char *field_name_from_var(struct hist_trigger_data *hist_data,
3616 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3617 name = hist_data->attrs->var_defs.name[i];
3619 if (strcmp(var_name, name) == 0) {
3620 field = hist_data->attrs->var_defs.expr[i];
3621 if (contains_operator(field) || is_var_ref(field))
3630 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
3631 char *system, char *event_name,
3634 struct trace_event_call *call;
3636 if (system && event_name) {
3637 call = hist_data->event_file->event_call;
3639 if (strcmp(system, call->class->system) != 0)
3642 if (strcmp(event_name, trace_event_name(call)) != 0)
3646 if (!!system != !!event_name)
3649 if (!is_var_ref(var_name))
3654 return field_name_from_var(hist_data, var_name);
3657 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
3658 char *system, char *event_name,
3661 struct hist_field *var_field = NULL, *ref_field = NULL;
3662 struct trace_array *tr = hist_data->event_file->tr;
3664 if (!is_var_ref(var_name))
3669 var_field = find_event_var(hist_data, system, event_name, var_name);
3671 ref_field = create_var_ref(hist_data, var_field,
3672 system, event_name);
3675 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
3680 static struct ftrace_event_field *
3681 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
3682 char *field_str, unsigned long *flags)
3684 struct ftrace_event_field *field = NULL;
3685 char *field_name, *modifier, *str;
3686 struct trace_array *tr = file->tr;
3688 modifier = str = kstrdup(field_str, GFP_KERNEL);
3690 return ERR_PTR(-ENOMEM);
3692 field_name = strsep(&modifier, ".");
3694 if (strcmp(modifier, "hex") == 0)
3695 *flags |= HIST_FIELD_FL_HEX;
3696 else if (strcmp(modifier, "sym") == 0)
3697 *flags |= HIST_FIELD_FL_SYM;
3698 else if (strcmp(modifier, "sym-offset") == 0)
3699 *flags |= HIST_FIELD_FL_SYM_OFFSET;
3700 else if ((strcmp(modifier, "execname") == 0) &&
3701 (strcmp(field_name, "common_pid") == 0))
3702 *flags |= HIST_FIELD_FL_EXECNAME;
3703 else if (strcmp(modifier, "syscall") == 0)
3704 *flags |= HIST_FIELD_FL_SYSCALL;
3705 else if (strcmp(modifier, "log2") == 0)
3706 *flags |= HIST_FIELD_FL_LOG2;
3707 else if (strcmp(modifier, "usecs") == 0)
3708 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
3710 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
3711 field = ERR_PTR(-EINVAL);
3716 if (strcmp(field_name, "common_timestamp") == 0) {
3717 *flags |= HIST_FIELD_FL_TIMESTAMP;
3718 hist_data->enable_timestamps = true;
3719 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3720 hist_data->attrs->ts_in_usecs = true;
3721 } else if (strcmp(field_name, "cpu") == 0)
3722 *flags |= HIST_FIELD_FL_CPU;
3724 field = trace_find_event_field(file->event_call, field_name);
3725 if (!field || !field->size) {
3726 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
3727 field = ERR_PTR(-EINVAL);
3737 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
3738 struct hist_field *var_ref,
3741 struct hist_field *alias = NULL;
3742 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
3744 alias = create_hist_field(hist_data, NULL, flags, var_name);
3748 alias->fn = var_ref->fn;
3749 alias->operands[0] = var_ref;
3751 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
3752 destroy_hist_field(alias, 0);
3756 alias->var_ref_idx = var_ref->var_ref_idx;
3761 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
3762 struct trace_event_file *file, char *str,
3763 unsigned long *flags, char *var_name)
3765 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
3766 struct ftrace_event_field *field = NULL;
3767 struct hist_field *hist_field = NULL;
3770 s = strchr(str, '.');
3772 s = strchr(++s, '.');
3774 ref_system = strsep(&str, ".");
3779 ref_event = strsep(&str, ".");
3788 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
3790 hist_field = parse_var_ref(hist_data, ref_system,
3791 ref_event, ref_var);
3794 hist_field = create_alias(hist_data, hist_field, var_name);
3805 field = parse_field(hist_data, file, str, flags);
3806 if (IS_ERR(field)) {
3807 ret = PTR_ERR(field);
3811 hist_field = create_hist_field(hist_data, field, *flags, var_name);
3819 return ERR_PTR(ret);
3822 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3823 struct trace_event_file *file,
3824 char *str, unsigned long flags,
3825 char *var_name, unsigned int level);
3827 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
3828 struct trace_event_file *file,
3829 char *str, unsigned long flags,
3830 char *var_name, unsigned int level)
3832 struct hist_field *operand1, *expr = NULL;
3833 unsigned long operand_flags;
3837 /* we support only -(xxx) i.e. explicit parens required */
3840 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3845 str++; /* skip leading '-' */
3847 s = strchr(str, '(');
3855 s = strrchr(str, ')');
3859 ret = -EINVAL; /* no closing ')' */
3863 flags |= HIST_FIELD_FL_EXPR;
3864 expr = create_hist_field(hist_data, NULL, flags, var_name);
3871 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3872 if (IS_ERR(operand1)) {
3873 ret = PTR_ERR(operand1);
3877 expr->flags |= operand1->flags &
3878 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3879 expr->fn = hist_field_unary_minus;
3880 expr->operands[0] = operand1;
3881 expr->operator = FIELD_OP_UNARY_MINUS;
3882 expr->name = expr_str(expr, 0);
3883 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3891 destroy_hist_field(expr, 0);
3892 return ERR_PTR(ret);
3895 static int check_expr_operands(struct trace_array *tr,
3896 struct hist_field *operand1,
3897 struct hist_field *operand2)
3899 unsigned long operand1_flags = operand1->flags;
3900 unsigned long operand2_flags = operand2->flags;
3902 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
3903 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
3904 struct hist_field *var;
3906 var = find_var_field(operand1->var.hist_data, operand1->name);
3909 operand1_flags = var->flags;
3912 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
3913 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
3914 struct hist_field *var;
3916 var = find_var_field(operand2->var.hist_data, operand2->name);
3919 operand2_flags = var->flags;
3922 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
3923 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
3924 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
3931 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3932 struct trace_event_file *file,
3933 char *str, unsigned long flags,
3934 char *var_name, unsigned int level)
3936 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
3937 unsigned long operand_flags;
3938 int field_op, ret = -EINVAL;
3939 char *sep, *operand1_str;
3942 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3943 return ERR_PTR(-EINVAL);
3946 field_op = contains_operator(str);
3948 if (field_op == FIELD_OP_NONE)
3949 return parse_atom(hist_data, file, str, &flags, var_name);
3951 if (field_op == FIELD_OP_UNARY_MINUS)
3952 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3955 case FIELD_OP_MINUS:
3965 operand1_str = strsep(&str, sep);
3966 if (!operand1_str || !str)
3970 operand1 = parse_atom(hist_data, file, operand1_str,
3971 &operand_flags, NULL);
3972 if (IS_ERR(operand1)) {
3973 ret = PTR_ERR(operand1);
3978 /* rest of string could be another expression e.g. b+c in a+b+c */
3980 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3981 if (IS_ERR(operand2)) {
3982 ret = PTR_ERR(operand2);
3987 ret = check_expr_operands(file->tr, operand1, operand2);
3991 flags |= HIST_FIELD_FL_EXPR;
3993 flags |= operand1->flags &
3994 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3996 expr = create_hist_field(hist_data, NULL, flags, var_name);
4002 operand1->read_once = true;
4003 operand2->read_once = true;
4005 expr->operands[0] = operand1;
4006 expr->operands[1] = operand2;
4007 expr->operator = field_op;
4008 expr->name = expr_str(expr, 0);
4009 expr->type = kstrdup(operand1->type, GFP_KERNEL);
4016 case FIELD_OP_MINUS:
4017 expr->fn = hist_field_minus;
4020 expr->fn = hist_field_plus;
4029 destroy_hist_field(operand1, 0);
4030 destroy_hist_field(operand2, 0);
4031 destroy_hist_field(expr, 0);
4033 return ERR_PTR(ret);
4036 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
4037 struct trace_event_file *file)
4039 struct event_trigger_data *test;
4041 lockdep_assert_held(&event_mutex);
4043 list_for_each_entry(test, &file->triggers, list) {
4044 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
4045 if (test->private_data == hist_data)
4046 return test->filter_str;
4053 static struct event_command trigger_hist_cmd;
4054 static int event_hist_trigger_func(struct event_command *cmd_ops,
4055 struct trace_event_file *file,
4056 char *glob, char *cmd, char *param);
4058 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
4059 struct hist_trigger_data *hist_data,
4060 unsigned int n_keys)
4062 struct hist_field *target_hist_field, *hist_field;
4063 unsigned int n, i, j;
4065 if (hist_data->n_fields - hist_data->n_vals != n_keys)
4068 i = hist_data->n_vals;
4069 j = target_hist_data->n_vals;
4071 for (n = 0; n < n_keys; n++) {
4072 hist_field = hist_data->fields[i + n];
4073 target_hist_field = target_hist_data->fields[j + n];
4075 if (strcmp(hist_field->type, target_hist_field->type) != 0)
4077 if (hist_field->size != target_hist_field->size)
4079 if (hist_field->is_signed != target_hist_field->is_signed)
4086 static struct hist_trigger_data *
4087 find_compatible_hist(struct hist_trigger_data *target_hist_data,
4088 struct trace_event_file *file)
4090 struct hist_trigger_data *hist_data;
4091 struct event_trigger_data *test;
4092 unsigned int n_keys;
4094 lockdep_assert_held(&event_mutex);
4096 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
4098 list_for_each_entry(test, &file->triggers, list) {
4099 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
4100 hist_data = test->private_data;
4102 if (compatible_keys(target_hist_data, hist_data, n_keys))
4110 static struct trace_event_file *event_file(struct trace_array *tr,
4111 char *system, char *event_name)
4113 struct trace_event_file *file;
4115 file = __find_event_file(tr, system, event_name);
4117 return ERR_PTR(-EINVAL);
4122 static struct hist_field *
4123 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
4124 char *system, char *event_name, char *field_name)
4126 struct hist_field *event_var;
4127 char *synthetic_name;
4129 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4130 if (!synthetic_name)
4131 return ERR_PTR(-ENOMEM);
4133 strcpy(synthetic_name, "synthetic_");
4134 strcat(synthetic_name, field_name);
4136 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
4138 kfree(synthetic_name);
4144 * create_field_var_hist - Automatically create a histogram and var for a field
4145 * @target_hist_data: The target hist trigger
4146 * @subsys_name: Optional subsystem name
4147 * @event_name: Optional event name
4148 * @field_name: The name of the field (and the resulting variable)
4150 * Hist trigger actions fetch data from variables, not directly from
4151 * events. However, for convenience, users are allowed to directly
4152 * specify an event field in an action, which will be automatically
4153 * converted into a variable on their behalf.
4155 * If a user specifies a field on an event that isn't the event the
4156 * histogram currently being defined (the target event histogram), the
4157 * only way that can be accomplished is if a new hist trigger is
4158 * created and the field variable defined on that.
4160 * This function creates a new histogram compatible with the target
4161 * event (meaning a histogram with the same key as the target
4162 * histogram), and creates a variable for the specified field, but
4163 * with 'synthetic_' prepended to the variable name in order to avoid
4164 * collision with normal field variables.
4166 * Return: The variable created for the field.
4168 static struct hist_field *
4169 create_field_var_hist(struct hist_trigger_data *target_hist_data,
4170 char *subsys_name, char *event_name, char *field_name)
4172 struct trace_array *tr = target_hist_data->event_file->tr;
4173 struct hist_field *event_var = ERR_PTR(-EINVAL);
4174 struct hist_trigger_data *hist_data;
4175 unsigned int i, n, first = true;
4176 struct field_var_hist *var_hist;
4177 struct trace_event_file *file;
4178 struct hist_field *key_field;
4183 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
4184 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4185 return ERR_PTR(-EINVAL);
4188 file = event_file(tr, subsys_name, event_name);
4191 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
4192 ret = PTR_ERR(file);
4193 return ERR_PTR(ret);
4197 * Look for a histogram compatible with target. We'll use the
4198 * found histogram specification to create a new matching
4199 * histogram with our variable on it. target_hist_data is not
4200 * yet a registered histogram so we can't use that.
4202 hist_data = find_compatible_hist(target_hist_data, file);
4204 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
4205 return ERR_PTR(-EINVAL);
4208 /* See if a synthetic field variable has already been created */
4209 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4210 event_name, field_name);
4211 if (!IS_ERR_OR_NULL(event_var))
4214 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
4216 return ERR_PTR(-ENOMEM);
4218 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4221 return ERR_PTR(-ENOMEM);
4224 /* Use the same keys as the compatible histogram */
4225 strcat(cmd, "keys=");
4227 for_each_hist_key_field(i, hist_data) {
4228 key_field = hist_data->fields[i];
4231 strcat(cmd, key_field->field->name);
4235 /* Create the synthetic field variable specification */
4236 strcat(cmd, ":synthetic_");
4237 strcat(cmd, field_name);
4239 strcat(cmd, field_name);
4241 /* Use the same filter as the compatible histogram */
4242 saved_filter = find_trigger_filter(hist_data, file);
4244 strcat(cmd, " if ");
4245 strcat(cmd, saved_filter);
4248 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
4249 if (!var_hist->cmd) {
4252 return ERR_PTR(-ENOMEM);
4255 /* Save the compatible histogram information */
4256 var_hist->hist_data = hist_data;
4258 /* Create the new histogram with our variable */
4259 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
4263 kfree(var_hist->cmd);
4265 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
4266 return ERR_PTR(ret);
4271 /* If we can't find the variable, something went wrong */
4272 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4273 event_name, field_name);
4274 if (IS_ERR_OR_NULL(event_var)) {
4275 kfree(var_hist->cmd);
4277 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
4278 return ERR_PTR(-EINVAL);
4281 n = target_hist_data->n_field_var_hists;
4282 target_hist_data->field_var_hists[n] = var_hist;
4283 target_hist_data->n_field_var_hists++;
4288 static struct hist_field *
4289 find_target_event_var(struct hist_trigger_data *hist_data,
4290 char *subsys_name, char *event_name, char *var_name)
4292 struct trace_event_file *file = hist_data->event_file;
4293 struct hist_field *hist_field = NULL;
4296 struct trace_event_call *call;
4301 call = file->event_call;
4303 if (strcmp(subsys_name, call->class->system) != 0)
4306 if (strcmp(event_name, trace_event_name(call)) != 0)
4310 hist_field = find_var_field(hist_data, var_name);
4315 static inline void __update_field_vars(struct tracing_map_elt *elt,
4316 struct ring_buffer_event *rbe,
4318 struct field_var **field_vars,
4319 unsigned int n_field_vars,
4320 unsigned int field_var_str_start)
4322 struct hist_elt_data *elt_data = elt->private_data;
4323 unsigned int i, j, var_idx;
4326 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
4327 struct field_var *field_var = field_vars[i];
4328 struct hist_field *var = field_var->var;
4329 struct hist_field *val = field_var->val;
4331 var_val = val->fn(val, elt, rbe, rec);
4332 var_idx = var->var.idx;
4334 if (val->flags & HIST_FIELD_FL_STRING) {
4335 char *str = elt_data->field_var_str[j++];
4336 char *val_str = (char *)(uintptr_t)var_val;
4338 strscpy(str, val_str, STR_VAR_LEN_MAX);
4339 var_val = (u64)(uintptr_t)str;
4341 tracing_map_set_var(elt, var_idx, var_val);
4345 static void update_field_vars(struct hist_trigger_data *hist_data,
4346 struct tracing_map_elt *elt,
4347 struct ring_buffer_event *rbe,
4350 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
4351 hist_data->n_field_vars, 0);
4354 static void save_track_data_vars(struct hist_trigger_data *hist_data,
4355 struct tracing_map_elt *elt, void *rec,
4356 struct ring_buffer_event *rbe, void *key,
4357 struct action_data *data, u64 *var_ref_vals)
4359 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
4360 hist_data->n_save_vars, hist_data->n_field_var_str);
4363 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
4364 struct trace_event_file *file,
4365 char *name, int size, const char *type)
4367 struct hist_field *var;
4370 if (find_var(hist_data, file, name) && !hist_data->remove) {
4371 var = ERR_PTR(-EINVAL);
4375 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
4377 var = ERR_PTR(-ENOMEM);
4381 idx = tracing_map_add_var(hist_data->map);
4384 var = ERR_PTR(-EINVAL);
4389 var->flags = HIST_FIELD_FL_VAR;
4391 var->var.hist_data = var->hist_data = hist_data;
4393 var->var.name = kstrdup(name, GFP_KERNEL);
4394 var->type = kstrdup(type, GFP_KERNEL);
4395 if (!var->var.name || !var->type) {
4396 kfree(var->var.name);
4399 var = ERR_PTR(-ENOMEM);
4405 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
4406 struct trace_event_file *file,
4409 struct hist_field *val = NULL, *var = NULL;
4410 unsigned long flags = HIST_FIELD_FL_VAR;
4411 struct trace_array *tr = file->tr;
4412 struct field_var *field_var;
4415 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
4416 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4421 val = parse_atom(hist_data, file, field_name, &flags, NULL);
4423 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
4428 var = create_var(hist_data, file, field_name, val->size, val->type);
4430 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
4436 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
4444 field_var->var = var;
4445 field_var->val = val;
4449 field_var = ERR_PTR(ret);
4454 * create_target_field_var - Automatically create a variable for a field
4455 * @target_hist_data: The target hist trigger
4456 * @subsys_name: Optional subsystem name
4457 * @event_name: Optional event name
4458 * @var_name: The name of the field (and the resulting variable)
4460 * Hist trigger actions fetch data from variables, not directly from
4461 * events. However, for convenience, users are allowed to directly
4462 * specify an event field in an action, which will be automatically
4463 * converted into a variable on their behalf.
4465 * This function creates a field variable with the name var_name on
4466 * the hist trigger currently being defined on the target event. If
4467 * subsys_name and event_name are specified, this function simply
4468 * verifies that they do in fact match the target event subsystem and
4471 * Return: The variable created for the field.
4473 static struct field_var *
4474 create_target_field_var(struct hist_trigger_data *target_hist_data,
4475 char *subsys_name, char *event_name, char *var_name)
4477 struct trace_event_file *file = target_hist_data->event_file;
4480 struct trace_event_call *call;
4485 call = file->event_call;
4487 if (strcmp(subsys_name, call->class->system) != 0)
4490 if (strcmp(event_name, trace_event_name(call)) != 0)
4494 return create_field_var(target_hist_data, file, var_name);
4497 static bool check_track_val_max(u64 track_val, u64 var_val)
4499 if (var_val <= track_val)
4505 static bool check_track_val_changed(u64 track_val, u64 var_val)
4507 if (var_val == track_val)
4513 static u64 get_track_val(struct hist_trigger_data *hist_data,
4514 struct tracing_map_elt *elt,
4515 struct action_data *data)
4517 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4520 track_val = tracing_map_read_var(elt, track_var_idx);
4525 static void save_track_val(struct hist_trigger_data *hist_data,
4526 struct tracing_map_elt *elt,
4527 struct action_data *data, u64 var_val)
4529 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4531 tracing_map_set_var(elt, track_var_idx, var_val);
4534 static void save_track_data(struct hist_trigger_data *hist_data,
4535 struct tracing_map_elt *elt, void *rec,
4536 struct ring_buffer_event *rbe, void *key,
4537 struct action_data *data, u64 *var_ref_vals)
4539 if (data->track_data.save_data)
4540 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4543 static bool check_track_val(struct tracing_map_elt *elt,
4544 struct action_data *data,
4547 struct hist_trigger_data *hist_data;
4550 hist_data = data->track_data.track_var->hist_data;
4551 track_val = get_track_val(hist_data, elt, data);
4553 return data->track_data.check_val(track_val, var_val);
4556 #ifdef CONFIG_TRACER_SNAPSHOT
4557 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4559 /* called with tr->max_lock held */
4560 struct track_data *track_data = tr->cond_snapshot->cond_data;
4561 struct hist_elt_data *elt_data, *track_elt_data;
4562 struct snapshot_context *context = cond_data;
4563 struct action_data *action;
4569 action = track_data->action_data;
4571 track_val = get_track_val(track_data->hist_data, context->elt,
4572 track_data->action_data);
4574 if (!action->track_data.check_val(track_data->track_val, track_val))
4577 track_data->track_val = track_val;
4578 memcpy(track_data->key, context->key, track_data->key_len);
4580 elt_data = context->elt->private_data;
4581 track_elt_data = track_data->elt.private_data;
4583 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
4585 track_data->updated = true;
4590 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4591 struct tracing_map_elt *elt, void *rec,
4592 struct ring_buffer_event *rbe, void *key,
4593 struct action_data *data,
4596 struct trace_event_file *file = hist_data->event_file;
4597 struct snapshot_context context;
4602 tracing_snapshot_cond(file->tr, &context);
4605 static void hist_trigger_print_key(struct seq_file *m,
4606 struct hist_trigger_data *hist_data,
4608 struct tracing_map_elt *elt);
4610 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
4614 if (!hist_data->n_actions)
4617 for (i = 0; i < hist_data->n_actions; i++) {
4618 struct action_data *data = hist_data->actions[i];
4620 if (data->action == ACTION_SNAPSHOT)
4627 static void track_data_snapshot_print(struct seq_file *m,
4628 struct hist_trigger_data *hist_data)
4630 struct trace_event_file *file = hist_data->event_file;
4631 struct track_data *track_data;
4632 struct action_data *action;
4634 track_data = tracing_cond_snapshot_data(file->tr);
4638 if (!track_data->updated)
4641 action = snapshot_action(hist_data);
4645 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
4646 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
4647 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
4648 action->track_data.var_str, track_data->track_val);
4650 seq_puts(m, "\ttriggered by event with key: ");
4651 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
4655 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4659 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4660 struct tracing_map_elt *elt, void *rec,
4661 struct ring_buffer_event *rbe, void *key,
4662 struct action_data *data,
4663 u64 *var_ref_vals) {}
4664 static void track_data_snapshot_print(struct seq_file *m,
4665 struct hist_trigger_data *hist_data) {}
4666 #endif /* CONFIG_TRACER_SNAPSHOT */
4668 static void track_data_print(struct seq_file *m,
4669 struct hist_trigger_data *hist_data,
4670 struct tracing_map_elt *elt,
4671 struct action_data *data)
4673 u64 track_val = get_track_val(hist_data, elt, data);
4674 unsigned int i, save_var_idx;
4676 if (data->handler == HANDLER_ONMAX)
4677 seq_printf(m, "\n\tmax: %10llu", track_val);
4678 else if (data->handler == HANDLER_ONCHANGE)
4679 seq_printf(m, "\n\tchanged: %10llu", track_val);
4681 if (data->action == ACTION_SNAPSHOT)
4684 for (i = 0; i < hist_data->n_save_vars; i++) {
4685 struct hist_field *save_val = hist_data->save_vars[i]->val;
4686 struct hist_field *save_var = hist_data->save_vars[i]->var;
4689 save_var_idx = save_var->var.idx;
4691 val = tracing_map_read_var(elt, save_var_idx);
4693 if (save_val->flags & HIST_FIELD_FL_STRING) {
4694 seq_printf(m, " %s: %-32s", save_var->var.name,
4695 (char *)(uintptr_t)(val));
4697 seq_printf(m, " %s: %10llu", save_var->var.name, val);
4701 static void ontrack_action(struct hist_trigger_data *hist_data,
4702 struct tracing_map_elt *elt, void *rec,
4703 struct ring_buffer_event *rbe, void *key,
4704 struct action_data *data, u64 *var_ref_vals)
4706 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
4708 if (check_track_val(elt, data, var_val)) {
4709 save_track_val(hist_data, elt, data, var_val);
4710 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4714 static void action_data_destroy(struct action_data *data)
4718 lockdep_assert_held(&event_mutex);
4720 kfree(data->action_name);
4722 for (i = 0; i < data->n_params; i++)
4723 kfree(data->params[i]);
4725 if (data->synth_event)
4726 data->synth_event->ref--;
4728 kfree(data->synth_event_name);
4733 static void track_data_destroy(struct hist_trigger_data *hist_data,
4734 struct action_data *data)
4736 struct trace_event_file *file = hist_data->event_file;
4738 destroy_hist_field(data->track_data.track_var, 0);
4740 if (data->action == ACTION_SNAPSHOT) {
4741 struct track_data *track_data;
4743 track_data = tracing_cond_snapshot_data(file->tr);
4744 if (track_data && track_data->hist_data == hist_data) {
4745 tracing_snapshot_cond_disable(file->tr);
4746 track_data_free(track_data);
4750 kfree(data->track_data.var_str);
4752 action_data_destroy(data);
4755 static int action_create(struct hist_trigger_data *hist_data,
4756 struct action_data *data);
4758 static int track_data_create(struct hist_trigger_data *hist_data,
4759 struct action_data *data)
4761 struct hist_field *var_field, *ref_field, *track_var = NULL;
4762 struct trace_event_file *file = hist_data->event_file;
4763 struct trace_array *tr = file->tr;
4764 char *track_data_var_str;
4767 track_data_var_str = data->track_data.var_str;
4768 if (track_data_var_str[0] != '$') {
4769 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
4772 track_data_var_str++;
4774 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
4776 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
4780 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
4784 data->track_data.var_ref = ref_field;
4786 if (data->handler == HANDLER_ONMAX)
4787 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
4788 if (IS_ERR(track_var)) {
4789 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4790 ret = PTR_ERR(track_var);
4794 if (data->handler == HANDLER_ONCHANGE)
4795 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
4796 if (IS_ERR(track_var)) {
4797 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4798 ret = PTR_ERR(track_var);
4801 data->track_data.track_var = track_var;
4803 ret = action_create(hist_data, data);
4808 static int parse_action_params(struct trace_array *tr, char *params,
4809 struct action_data *data)
4811 char *param, *saved_param;
4812 bool first_param = true;
4816 if (data->n_params >= SYNTH_FIELDS_MAX) {
4817 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
4821 param = strsep(¶ms, ",");
4823 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
4828 param = strstrip(param);
4829 if (strlen(param) < 2) {
4830 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
4835 saved_param = kstrdup(param, GFP_KERNEL);
4841 if (first_param && data->use_trace_keyword) {
4842 data->synth_event_name = saved_param;
4843 first_param = false;
4846 first_param = false;
4848 data->params[data->n_params++] = saved_param;
4854 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
4855 enum handler_id handler)
4862 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4867 action_name = strsep(&str, "(");
4868 if (!action_name || !str) {
4869 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4874 if (str_has_prefix(action_name, "save")) {
4875 char *params = strsep(&str, ")");
4878 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
4883 ret = parse_action_params(tr, params, data);
4887 if (handler == HANDLER_ONMAX)
4888 data->track_data.check_val = check_track_val_max;
4889 else if (handler == HANDLER_ONCHANGE)
4890 data->track_data.check_val = check_track_val_changed;
4892 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4897 data->track_data.save_data = save_track_data_vars;
4898 data->fn = ontrack_action;
4899 data->action = ACTION_SAVE;
4900 } else if (str_has_prefix(action_name, "snapshot")) {
4901 char *params = strsep(&str, ")");
4904 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
4909 if (handler == HANDLER_ONMAX)
4910 data->track_data.check_val = check_track_val_max;
4911 else if (handler == HANDLER_ONCHANGE)
4912 data->track_data.check_val = check_track_val_changed;
4914 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4919 data->track_data.save_data = save_track_data_snapshot;
4920 data->fn = ontrack_action;
4921 data->action = ACTION_SNAPSHOT;
4923 char *params = strsep(&str, ")");
4925 if (str_has_prefix(action_name, "trace"))
4926 data->use_trace_keyword = true;
4929 ret = parse_action_params(tr, params, data);
4934 if (handler == HANDLER_ONMAX)
4935 data->track_data.check_val = check_track_val_max;
4936 else if (handler == HANDLER_ONCHANGE)
4937 data->track_data.check_val = check_track_val_changed;
4939 if (handler != HANDLER_ONMATCH) {
4940 data->track_data.save_data = action_trace;
4941 data->fn = ontrack_action;
4943 data->fn = action_trace;
4945 data->action = ACTION_TRACE;
4948 data->action_name = kstrdup(action_name, GFP_KERNEL);
4949 if (!data->action_name) {
4954 data->handler = handler;
4959 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4960 char *str, enum handler_id handler)
4962 struct action_data *data;
4966 data = kzalloc(sizeof(*data), GFP_KERNEL);
4968 return ERR_PTR(-ENOMEM);
4970 var_str = strsep(&str, ")");
4971 if (!var_str || !str) {
4976 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4977 if (!data->track_data.var_str) {
4982 ret = action_parse(hist_data->event_file->tr, str, data, handler);
4988 track_data_destroy(hist_data, data);
4989 data = ERR_PTR(ret);
4993 static void onmatch_destroy(struct action_data *data)
4995 kfree(data->match_data.event);
4996 kfree(data->match_data.event_system);
4998 action_data_destroy(data);
5001 static void destroy_field_var(struct field_var *field_var)
5006 destroy_hist_field(field_var->var, 0);
5007 destroy_hist_field(field_var->val, 0);
5012 static void destroy_field_vars(struct hist_trigger_data *hist_data)
5016 for (i = 0; i < hist_data->n_field_vars; i++)
5017 destroy_field_var(hist_data->field_vars[i]);
5019 for (i = 0; i < hist_data->n_save_vars; i++)
5020 destroy_field_var(hist_data->save_vars[i]);
5023 static void save_field_var(struct hist_trigger_data *hist_data,
5024 struct field_var *field_var)
5026 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
5028 if (field_var->val->flags & HIST_FIELD_FL_STRING)
5029 hist_data->n_field_var_str++;
5033 static int check_synth_field(struct synth_event *event,
5034 struct hist_field *hist_field,
5035 unsigned int field_pos)
5037 struct synth_field *field;
5039 if (field_pos >= event->n_fields)
5042 field = event->fields[field_pos];
5044 if (strcmp(field->type, hist_field->type) != 0) {
5045 if (field->size != hist_field->size ||
5046 field->is_signed != hist_field->is_signed)
5053 static struct hist_field *
5054 trace_action_find_var(struct hist_trigger_data *hist_data,
5055 struct action_data *data,
5056 char *system, char *event, char *var)
5058 struct trace_array *tr = hist_data->event_file->tr;
5059 struct hist_field *hist_field;
5061 var++; /* skip '$' */
5063 hist_field = find_target_event_var(hist_data, system, event, var);
5065 if (!system && data->handler == HANDLER_ONMATCH) {
5066 system = data->match_data.event_system;
5067 event = data->match_data.event;
5070 hist_field = find_event_var(hist_data, system, event, var);
5074 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
5079 static struct hist_field *
5080 trace_action_create_field_var(struct hist_trigger_data *hist_data,
5081 struct action_data *data, char *system,
5082 char *event, char *var)
5084 struct hist_field *hist_field = NULL;
5085 struct field_var *field_var;
5088 * First try to create a field var on the target event (the
5089 * currently being defined). This will create a variable for
5090 * unqualified fields on the target event, or if qualified,
5091 * target fields that have qualified names matching the target.
5093 field_var = create_target_field_var(hist_data, system, event, var);
5095 if (field_var && !IS_ERR(field_var)) {
5096 save_field_var(hist_data, field_var);
5097 hist_field = field_var->var;
5101 * If no explicit system.event is specfied, default to
5102 * looking for fields on the onmatch(system.event.xxx)
5105 if (!system && data->handler == HANDLER_ONMATCH) {
5106 system = data->match_data.event_system;
5107 event = data->match_data.event;
5111 * At this point, we're looking at a field on another
5112 * event. Because we can't modify a hist trigger on
5113 * another event to add a variable for a field, we need
5114 * to create a new trigger on that event and create the
5115 * variable at the same time.
5117 hist_field = create_field_var_hist(hist_data, system, event, var);
5118 if (IS_ERR(hist_field))
5124 destroy_field_var(field_var);
5129 static int trace_action_create(struct hist_trigger_data *hist_data,
5130 struct action_data *data)
5132 struct trace_array *tr = hist_data->event_file->tr;
5133 char *event_name, *param, *system = NULL;
5134 struct hist_field *hist_field, *var_ref;
5136 unsigned int field_pos = 0;
5137 struct synth_event *event;
5138 char *synth_event_name;
5139 int var_ref_idx, ret = 0;
5141 lockdep_assert_held(&event_mutex);
5143 if (data->use_trace_keyword)
5144 synth_event_name = data->synth_event_name;
5146 synth_event_name = data->action_name;
5148 event = find_synth_event(synth_event_name);
5150 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
5156 for (i = 0; i < data->n_params; i++) {
5159 p = param = kstrdup(data->params[i], GFP_KERNEL);
5165 system = strsep(¶m, ".");
5167 param = (char *)system;
5168 system = event_name = NULL;
5170 event_name = strsep(¶m, ".");
5178 if (param[0] == '$')
5179 hist_field = trace_action_find_var(hist_data, data,
5183 hist_field = trace_action_create_field_var(hist_data,
5195 if (check_synth_field(event, hist_field, field_pos) == 0) {
5196 var_ref = create_var_ref(hist_data, hist_field,
5197 system, event_name);
5204 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
5205 if (WARN_ON(var_ref_idx < 0)) {
5210 data->var_ref_idx[i] = var_ref_idx;
5217 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
5223 if (field_pos != event->n_fields) {
5224 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
5229 data->synth_event = event;
5238 static int action_create(struct hist_trigger_data *hist_data,
5239 struct action_data *data)
5241 struct trace_event_file *file = hist_data->event_file;
5242 struct trace_array *tr = file->tr;
5243 struct track_data *track_data;
5244 struct field_var *field_var;
5249 if (data->action == ACTION_TRACE)
5250 return trace_action_create(hist_data, data);
5252 if (data->action == ACTION_SNAPSHOT) {
5253 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
5254 if (IS_ERR(track_data)) {
5255 ret = PTR_ERR(track_data);
5259 ret = tracing_snapshot_cond_enable(file->tr, track_data,
5260 cond_snapshot_update);
5262 track_data_free(track_data);
5267 if (data->action == ACTION_SAVE) {
5268 if (hist_data->n_save_vars) {
5270 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
5274 for (i = 0; i < data->n_params; i++) {
5275 param = kstrdup(data->params[i], GFP_KERNEL);
5281 field_var = create_target_field_var(hist_data, NULL, NULL, param);
5282 if (IS_ERR(field_var)) {
5283 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
5285 ret = PTR_ERR(field_var);
5290 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
5291 if (field_var->val->flags & HIST_FIELD_FL_STRING)
5292 hist_data->n_save_var_str++;
5300 static int onmatch_create(struct hist_trigger_data *hist_data,
5301 struct action_data *data)
5303 return action_create(hist_data, data);
5306 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
5308 char *match_event, *match_event_system;
5309 struct action_data *data;
5312 data = kzalloc(sizeof(*data), GFP_KERNEL);
5314 return ERR_PTR(-ENOMEM);
5316 match_event = strsep(&str, ")");
5317 if (!match_event || !str) {
5318 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
5322 match_event_system = strsep(&match_event, ".");
5324 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
5328 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
5329 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
5333 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
5334 if (!data->match_data.event) {
5339 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
5340 if (!data->match_data.event_system) {
5345 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
5351 onmatch_destroy(data);
5352 data = ERR_PTR(ret);
5356 static int create_hitcount_val(struct hist_trigger_data *hist_data)
5358 hist_data->fields[HITCOUNT_IDX] =
5359 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
5360 if (!hist_data->fields[HITCOUNT_IDX])
5363 hist_data->n_vals++;
5364 hist_data->n_fields++;
5366 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
5372 static int __create_val_field(struct hist_trigger_data *hist_data,
5373 unsigned int val_idx,
5374 struct trace_event_file *file,
5375 char *var_name, char *field_str,
5376 unsigned long flags)
5378 struct hist_field *hist_field;
5381 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
5382 if (IS_ERR(hist_field)) {
5383 ret = PTR_ERR(hist_field);
5387 hist_data->fields[val_idx] = hist_field;
5389 ++hist_data->n_vals;
5390 ++hist_data->n_fields;
5392 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5398 static int create_val_field(struct hist_trigger_data *hist_data,
5399 unsigned int val_idx,
5400 struct trace_event_file *file,
5403 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
5406 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
5409 static int create_var_field(struct hist_trigger_data *hist_data,
5410 unsigned int val_idx,
5411 struct trace_event_file *file,
5412 char *var_name, char *expr_str)
5414 struct trace_array *tr = hist_data->event_file->tr;
5415 unsigned long flags = 0;
5417 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5420 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
5421 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
5425 flags |= HIST_FIELD_FL_VAR;
5426 hist_data->n_vars++;
5427 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
5430 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
5433 static int create_val_fields(struct hist_trigger_data *hist_data,
5434 struct trace_event_file *file)
5436 char *fields_str, *field_str;
5437 unsigned int i, j = 1;
5440 ret = create_hitcount_val(hist_data);
5444 fields_str = hist_data->attrs->vals_str;
5448 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
5449 j < TRACING_MAP_VALS_MAX; i++) {
5450 field_str = strsep(&fields_str, ",");
5454 if (strcmp(field_str, "hitcount") == 0)
5457 ret = create_val_field(hist_data, j++, file, field_str);
5462 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
5468 static int create_key_field(struct hist_trigger_data *hist_data,
5469 unsigned int key_idx,
5470 unsigned int key_offset,
5471 struct trace_event_file *file,
5474 struct trace_array *tr = hist_data->event_file->tr;
5475 struct hist_field *hist_field = NULL;
5476 unsigned long flags = 0;
5477 unsigned int key_size;
5480 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
5483 flags |= HIST_FIELD_FL_KEY;
5485 if (strcmp(field_str, "stacktrace") == 0) {
5486 flags |= HIST_FIELD_FL_STACKTRACE;
5487 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
5488 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
5490 hist_field = parse_expr(hist_data, file, field_str, flags,
5492 if (IS_ERR(hist_field)) {
5493 ret = PTR_ERR(hist_field);
5497 if (field_has_hist_vars(hist_field, 0)) {
5498 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
5499 destroy_hist_field(hist_field, 0);
5504 key_size = hist_field->size;
5507 hist_data->fields[key_idx] = hist_field;
5509 key_size = ALIGN(key_size, sizeof(u64));
5510 hist_data->fields[key_idx]->size = key_size;
5511 hist_data->fields[key_idx]->offset = key_offset;
5513 hist_data->key_size += key_size;
5515 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
5520 hist_data->n_keys++;
5521 hist_data->n_fields++;
5523 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
5531 static int create_key_fields(struct hist_trigger_data *hist_data,
5532 struct trace_event_file *file)
5534 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
5535 char *fields_str, *field_str;
5538 fields_str = hist_data->attrs->keys_str;
5542 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
5543 field_str = strsep(&fields_str, ",");
5546 ret = create_key_field(hist_data, i, key_offset,
5561 static int create_var_fields(struct hist_trigger_data *hist_data,
5562 struct trace_event_file *file)
5564 unsigned int i, j = hist_data->n_vals;
5567 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
5569 for (i = 0; i < n_vars; i++) {
5570 char *var_name = hist_data->attrs->var_defs.name[i];
5571 char *expr = hist_data->attrs->var_defs.expr[i];
5573 ret = create_var_field(hist_data, j++, file, var_name, expr);
5581 static void free_var_defs(struct hist_trigger_data *hist_data)
5585 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
5586 kfree(hist_data->attrs->var_defs.name[i]);
5587 kfree(hist_data->attrs->var_defs.expr[i]);
5590 hist_data->attrs->var_defs.n_vars = 0;
5593 static int parse_var_defs(struct hist_trigger_data *hist_data)
5595 struct trace_array *tr = hist_data->event_file->tr;
5596 char *s, *str, *var_name, *field_str;
5597 unsigned int i, j, n_vars = 0;
5600 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
5601 str = hist_data->attrs->assignment_str[i];
5602 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
5603 field_str = strsep(&str, ",");
5607 var_name = strsep(&field_str, "=");
5608 if (!var_name || !field_str) {
5609 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
5615 if (n_vars == TRACING_MAP_VARS_MAX) {
5616 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
5621 s = kstrdup(var_name, GFP_KERNEL);
5626 hist_data->attrs->var_defs.name[n_vars] = s;
5628 s = kstrdup(field_str, GFP_KERNEL);
5630 kfree(hist_data->attrs->var_defs.name[n_vars]);
5634 hist_data->attrs->var_defs.expr[n_vars++] = s;
5636 hist_data->attrs->var_defs.n_vars = n_vars;
5642 free_var_defs(hist_data);
5647 static int create_hist_fields(struct hist_trigger_data *hist_data,
5648 struct trace_event_file *file)
5652 ret = parse_var_defs(hist_data);
5656 ret = create_val_fields(hist_data, file);
5660 ret = create_var_fields(hist_data, file);
5664 ret = create_key_fields(hist_data, file);
5668 free_var_defs(hist_data);
5673 static int is_descending(struct trace_array *tr, const char *str)
5678 if (strcmp(str, "descending") == 0)
5681 if (strcmp(str, "ascending") == 0)
5684 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
5689 static int create_sort_keys(struct hist_trigger_data *hist_data)
5691 struct trace_array *tr = hist_data->event_file->tr;
5692 char *fields_str = hist_data->attrs->sort_key_str;
5693 struct tracing_map_sort_key *sort_key;
5694 int descending, ret = 0;
5695 unsigned int i, j, k;
5697 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
5702 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
5703 struct hist_field *hist_field;
5704 char *field_str, *field_name;
5705 const char *test_name;
5707 sort_key = &hist_data->sort_keys[i];
5709 field_str = strsep(&fields_str, ",");
5715 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5719 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
5720 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
5725 field_name = strsep(&field_str, ".");
5726 if (!field_name || !*field_name) {
5728 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5732 if (strcmp(field_name, "hitcount") == 0) {
5733 descending = is_descending(tr, field_str);
5734 if (descending < 0) {
5738 sort_key->descending = descending;
5742 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
5745 hist_field = hist_data->fields[j];
5746 if (hist_field->flags & HIST_FIELD_FL_VAR)
5751 test_name = hist_field_name(hist_field, 0);
5753 if (strcmp(field_name, test_name) == 0) {
5754 sort_key->field_idx = idx;
5755 descending = is_descending(tr, field_str);
5756 if (descending < 0) {
5760 sort_key->descending = descending;
5764 if (j == hist_data->n_fields) {
5766 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
5771 hist_data->n_sort_keys = i;
5776 static void destroy_actions(struct hist_trigger_data *hist_data)
5780 for (i = 0; i < hist_data->n_actions; i++) {
5781 struct action_data *data = hist_data->actions[i];
5783 if (data->handler == HANDLER_ONMATCH)
5784 onmatch_destroy(data);
5785 else if (data->handler == HANDLER_ONMAX ||
5786 data->handler == HANDLER_ONCHANGE)
5787 track_data_destroy(hist_data, data);
5793 static int parse_actions(struct hist_trigger_data *hist_data)
5795 struct trace_array *tr = hist_data->event_file->tr;
5796 struct action_data *data;
5802 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5803 str = hist_data->attrs->action_str[i];
5805 if ((len = str_has_prefix(str, "onmatch("))) {
5806 char *action_str = str + len;
5808 data = onmatch_parse(tr, action_str);
5810 ret = PTR_ERR(data);
5813 } else if ((len = str_has_prefix(str, "onmax("))) {
5814 char *action_str = str + len;
5816 data = track_data_parse(hist_data, action_str,
5819 ret = PTR_ERR(data);
5822 } else if ((len = str_has_prefix(str, "onchange("))) {
5823 char *action_str = str + len;
5825 data = track_data_parse(hist_data, action_str,
5828 ret = PTR_ERR(data);
5836 hist_data->actions[hist_data->n_actions++] = data;
5842 static int create_actions(struct hist_trigger_data *hist_data)
5844 struct action_data *data;
5848 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5849 data = hist_data->actions[i];
5851 if (data->handler == HANDLER_ONMATCH) {
5852 ret = onmatch_create(hist_data, data);
5855 } else if (data->handler == HANDLER_ONMAX ||
5856 data->handler == HANDLER_ONCHANGE) {
5857 ret = track_data_create(hist_data, data);
5869 static void print_actions(struct seq_file *m,
5870 struct hist_trigger_data *hist_data,
5871 struct tracing_map_elt *elt)
5875 for (i = 0; i < hist_data->n_actions; i++) {
5876 struct action_data *data = hist_data->actions[i];
5878 if (data->action == ACTION_SNAPSHOT)
5881 if (data->handler == HANDLER_ONMAX ||
5882 data->handler == HANDLER_ONCHANGE)
5883 track_data_print(m, hist_data, elt, data);
5887 static void print_action_spec(struct seq_file *m,
5888 struct hist_trigger_data *hist_data,
5889 struct action_data *data)
5893 if (data->action == ACTION_SAVE) {
5894 for (i = 0; i < hist_data->n_save_vars; i++) {
5895 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
5896 if (i < hist_data->n_save_vars - 1)
5899 } else if (data->action == ACTION_TRACE) {
5900 if (data->use_trace_keyword)
5901 seq_printf(m, "%s", data->synth_event_name);
5902 for (i = 0; i < data->n_params; i++) {
5903 if (i || data->use_trace_keyword)
5905 seq_printf(m, "%s", data->params[i]);
5910 static void print_track_data_spec(struct seq_file *m,
5911 struct hist_trigger_data *hist_data,
5912 struct action_data *data)
5914 if (data->handler == HANDLER_ONMAX)
5915 seq_puts(m, ":onmax(");
5916 else if (data->handler == HANDLER_ONCHANGE)
5917 seq_puts(m, ":onchange(");
5918 seq_printf(m, "%s", data->track_data.var_str);
5919 seq_printf(m, ").%s(", data->action_name);
5921 print_action_spec(m, hist_data, data);
5926 static void print_onmatch_spec(struct seq_file *m,
5927 struct hist_trigger_data *hist_data,
5928 struct action_data *data)
5930 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
5931 data->match_data.event);
5933 seq_printf(m, "%s(", data->action_name);
5935 print_action_spec(m, hist_data, data);
5940 static bool actions_match(struct hist_trigger_data *hist_data,
5941 struct hist_trigger_data *hist_data_test)
5945 if (hist_data->n_actions != hist_data_test->n_actions)
5948 for (i = 0; i < hist_data->n_actions; i++) {
5949 struct action_data *data = hist_data->actions[i];
5950 struct action_data *data_test = hist_data_test->actions[i];
5951 char *action_name, *action_name_test;
5953 if (data->handler != data_test->handler)
5955 if (data->action != data_test->action)
5958 if (data->n_params != data_test->n_params)
5961 for (j = 0; j < data->n_params; j++) {
5962 if (strcmp(data->params[j], data_test->params[j]) != 0)
5966 if (data->use_trace_keyword)
5967 action_name = data->synth_event_name;
5969 action_name = data->action_name;
5971 if (data_test->use_trace_keyword)
5972 action_name_test = data_test->synth_event_name;
5974 action_name_test = data_test->action_name;
5976 if (strcmp(action_name, action_name_test) != 0)
5979 if (data->handler == HANDLER_ONMATCH) {
5980 if (strcmp(data->match_data.event_system,
5981 data_test->match_data.event_system) != 0)
5983 if (strcmp(data->match_data.event,
5984 data_test->match_data.event) != 0)
5986 } else if (data->handler == HANDLER_ONMAX ||
5987 data->handler == HANDLER_ONCHANGE) {
5988 if (strcmp(data->track_data.var_str,
5989 data_test->track_data.var_str) != 0)
5998 static void print_actions_spec(struct seq_file *m,
5999 struct hist_trigger_data *hist_data)
6003 for (i = 0; i < hist_data->n_actions; i++) {
6004 struct action_data *data = hist_data->actions[i];
6006 if (data->handler == HANDLER_ONMATCH)
6007 print_onmatch_spec(m, hist_data, data);
6008 else if (data->handler == HANDLER_ONMAX ||
6009 data->handler == HANDLER_ONCHANGE)
6010 print_track_data_spec(m, hist_data, data);
6014 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
6018 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6019 kfree(hist_data->field_var_hists[i]->cmd);
6020 kfree(hist_data->field_var_hists[i]);
6024 static void destroy_hist_data(struct hist_trigger_data *hist_data)
6029 destroy_hist_trigger_attrs(hist_data->attrs);
6030 destroy_hist_fields(hist_data);
6031 tracing_map_destroy(hist_data->map);
6033 destroy_actions(hist_data);
6034 destroy_field_vars(hist_data);
6035 destroy_field_var_hists(hist_data);
6040 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
6042 struct tracing_map *map = hist_data->map;
6043 struct ftrace_event_field *field;
6044 struct hist_field *hist_field;
6047 for_each_hist_field(i, hist_data) {
6048 hist_field = hist_data->fields[i];
6049 if (hist_field->flags & HIST_FIELD_FL_KEY) {
6050 tracing_map_cmp_fn_t cmp_fn;
6052 field = hist_field->field;
6054 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
6055 cmp_fn = tracing_map_cmp_none;
6057 cmp_fn = tracing_map_cmp_num(hist_field->size,
6058 hist_field->is_signed);
6059 else if (is_string_field(field))
6060 cmp_fn = tracing_map_cmp_string;
6062 cmp_fn = tracing_map_cmp_num(field->size,
6064 idx = tracing_map_add_key_field(map,
6067 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
6068 idx = tracing_map_add_sum_field(map);
6073 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6074 idx = tracing_map_add_var(map);
6077 hist_field->var.idx = idx;
6078 hist_field->var.hist_data = hist_data;
6085 static struct hist_trigger_data *
6086 create_hist_data(unsigned int map_bits,
6087 struct hist_trigger_attrs *attrs,
6088 struct trace_event_file *file,
6091 const struct tracing_map_ops *map_ops = NULL;
6092 struct hist_trigger_data *hist_data;
6095 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
6097 return ERR_PTR(-ENOMEM);
6099 hist_data->attrs = attrs;
6100 hist_data->remove = remove;
6101 hist_data->event_file = file;
6103 ret = parse_actions(hist_data);
6107 ret = create_hist_fields(hist_data, file);
6111 ret = create_sort_keys(hist_data);
6115 map_ops = &hist_trigger_elt_data_ops;
6117 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
6118 map_ops, hist_data);
6119 if (IS_ERR(hist_data->map)) {
6120 ret = PTR_ERR(hist_data->map);
6121 hist_data->map = NULL;
6125 ret = create_tracing_map_fields(hist_data);
6131 hist_data->attrs = NULL;
6133 destroy_hist_data(hist_data);
6135 hist_data = ERR_PTR(ret);
6140 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
6141 struct tracing_map_elt *elt, void *rec,
6142 struct ring_buffer_event *rbe,
6145 struct hist_elt_data *elt_data;
6146 struct hist_field *hist_field;
6147 unsigned int i, var_idx;
6150 elt_data = elt->private_data;
6151 elt_data->var_ref_vals = var_ref_vals;
6153 for_each_hist_val_field(i, hist_data) {
6154 hist_field = hist_data->fields[i];
6155 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6156 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6157 var_idx = hist_field->var.idx;
6158 tracing_map_set_var(elt, var_idx, hist_val);
6161 tracing_map_update_sum(elt, i, hist_val);
6164 for_each_hist_key_field(i, hist_data) {
6165 hist_field = hist_data->fields[i];
6166 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6167 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6168 var_idx = hist_field->var.idx;
6169 tracing_map_set_var(elt, var_idx, hist_val);
6173 update_field_vars(hist_data, elt, rbe, rec);
6176 static inline void add_to_key(char *compound_key, void *key,
6177 struct hist_field *key_field, void *rec)
6179 size_t size = key_field->size;
6181 if (key_field->flags & HIST_FIELD_FL_STRING) {
6182 struct ftrace_event_field *field;
6184 field = key_field->field;
6185 if (field->filter_type == FILTER_DYN_STRING)
6186 size = *(u32 *)(rec + field->offset) >> 16;
6187 else if (field->filter_type == FILTER_PTR_STRING)
6189 else if (field->filter_type == FILTER_STATIC_STRING)
6192 /* ensure NULL-termination */
6193 if (size > key_field->size - 1)
6194 size = key_field->size - 1;
6196 strncpy(compound_key + key_field->offset, (char *)key, size);
6198 memcpy(compound_key + key_field->offset, key, size);
6202 hist_trigger_actions(struct hist_trigger_data *hist_data,
6203 struct tracing_map_elt *elt, void *rec,
6204 struct ring_buffer_event *rbe, void *key,
6207 struct action_data *data;
6210 for (i = 0; i < hist_data->n_actions; i++) {
6211 data = hist_data->actions[i];
6212 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
6216 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
6217 struct ring_buffer_event *rbe)
6219 struct hist_trigger_data *hist_data = data->private_data;
6220 bool use_compound_key = (hist_data->n_keys > 1);
6221 unsigned long entries[HIST_STACKTRACE_DEPTH];
6222 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
6223 char compound_key[HIST_KEY_SIZE_MAX];
6224 struct tracing_map_elt *elt = NULL;
6225 struct hist_field *key_field;
6230 memset(compound_key, 0, hist_data->key_size);
6232 for_each_hist_key_field(i, hist_data) {
6233 key_field = hist_data->fields[i];
6235 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6236 memset(entries, 0, HIST_STACKTRACE_SIZE);
6237 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
6238 HIST_STACKTRACE_SKIP);
6241 field_contents = key_field->fn(key_field, elt, rbe, rec);
6242 if (key_field->flags & HIST_FIELD_FL_STRING) {
6243 key = (void *)(unsigned long)field_contents;
6244 use_compound_key = true;
6246 key = (void *)&field_contents;
6249 if (use_compound_key)
6250 add_to_key(compound_key, key, key_field, rec);
6253 if (use_compound_key)
6256 if (hist_data->n_var_refs &&
6257 !resolve_var_refs(hist_data, key, var_ref_vals, false))
6260 elt = tracing_map_insert(hist_data->map, key);
6264 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
6266 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
6267 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
6270 static void hist_trigger_stacktrace_print(struct seq_file *m,
6271 unsigned long *stacktrace_entries,
6272 unsigned int max_entries)
6274 char str[KSYM_SYMBOL_LEN];
6275 unsigned int spaces = 8;
6278 for (i = 0; i < max_entries; i++) {
6279 if (!stacktrace_entries[i])
6282 seq_printf(m, "%*c", 1 + spaces, ' ');
6283 sprint_symbol(str, stacktrace_entries[i]);
6284 seq_printf(m, "%s\n", str);
6288 static void hist_trigger_print_key(struct seq_file *m,
6289 struct hist_trigger_data *hist_data,
6291 struct tracing_map_elt *elt)
6293 struct hist_field *key_field;
6294 char str[KSYM_SYMBOL_LEN];
6295 bool multiline = false;
6296 const char *field_name;
6302 for_each_hist_key_field(i, hist_data) {
6303 key_field = hist_data->fields[i];
6305 if (i > hist_data->n_vals)
6308 field_name = hist_field_name(key_field, 0);
6310 if (key_field->flags & HIST_FIELD_FL_HEX) {
6311 uval = *(u64 *)(key + key_field->offset);
6312 seq_printf(m, "%s: %llx", field_name, uval);
6313 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
6314 uval = *(u64 *)(key + key_field->offset);
6315 sprint_symbol_no_offset(str, uval);
6316 seq_printf(m, "%s: [%llx] %-45s", field_name,
6318 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
6319 uval = *(u64 *)(key + key_field->offset);
6320 sprint_symbol(str, uval);
6321 seq_printf(m, "%s: [%llx] %-55s", field_name,
6323 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
6324 struct hist_elt_data *elt_data = elt->private_data;
6327 if (WARN_ON_ONCE(!elt_data))
6330 comm = elt_data->comm;
6332 uval = *(u64 *)(key + key_field->offset);
6333 seq_printf(m, "%s: %-16s[%10llu]", field_name,
6335 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
6336 const char *syscall_name;
6338 uval = *(u64 *)(key + key_field->offset);
6339 syscall_name = get_syscall_name(uval);
6341 syscall_name = "unknown_syscall";
6343 seq_printf(m, "%s: %-30s[%3llu]", field_name,
6344 syscall_name, uval);
6345 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6346 seq_puts(m, "stacktrace:\n");
6347 hist_trigger_stacktrace_print(m,
6348 key + key_field->offset,
6349 HIST_STACKTRACE_DEPTH);
6351 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
6352 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
6353 *(u64 *)(key + key_field->offset));
6354 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
6355 seq_printf(m, "%s: %-50s", field_name,
6356 (char *)(key + key_field->offset));
6358 uval = *(u64 *)(key + key_field->offset);
6359 seq_printf(m, "%s: %10llu", field_name, uval);
6369 static void hist_trigger_entry_print(struct seq_file *m,
6370 struct hist_trigger_data *hist_data,
6372 struct tracing_map_elt *elt)
6374 const char *field_name;
6377 hist_trigger_print_key(m, hist_data, key, elt);
6379 seq_printf(m, " hitcount: %10llu",
6380 tracing_map_read_sum(elt, HITCOUNT_IDX));
6382 for (i = 1; i < hist_data->n_vals; i++) {
6383 field_name = hist_field_name(hist_data->fields[i], 0);
6385 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
6386 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
6389 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
6390 seq_printf(m, " %s: %10llx", field_name,
6391 tracing_map_read_sum(elt, i));
6393 seq_printf(m, " %s: %10llu", field_name,
6394 tracing_map_read_sum(elt, i));
6398 print_actions(m, hist_data, elt);
6403 static int print_entries(struct seq_file *m,
6404 struct hist_trigger_data *hist_data)
6406 struct tracing_map_sort_entry **sort_entries = NULL;
6407 struct tracing_map *map = hist_data->map;
6410 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
6411 hist_data->n_sort_keys,
6416 for (i = 0; i < n_entries; i++)
6417 hist_trigger_entry_print(m, hist_data,
6418 sort_entries[i]->key,
6419 sort_entries[i]->elt);
6421 tracing_map_destroy_sort_entries(sort_entries, n_entries);
6426 static void hist_trigger_show(struct seq_file *m,
6427 struct event_trigger_data *data, int n)
6429 struct hist_trigger_data *hist_data;
6433 seq_puts(m, "\n\n");
6435 seq_puts(m, "# event histogram\n#\n# trigger info: ");
6436 data->ops->print(m, data->ops, data);
6437 seq_puts(m, "#\n\n");
6439 hist_data = data->private_data;
6440 n_entries = print_entries(m, hist_data);
6444 track_data_snapshot_print(m, hist_data);
6446 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
6447 (u64)atomic64_read(&hist_data->map->hits),
6448 n_entries, (u64)atomic64_read(&hist_data->map->drops));
6451 static int hist_show(struct seq_file *m, void *v)
6453 struct event_trigger_data *data;
6454 struct trace_event_file *event_file;
6457 mutex_lock(&event_mutex);
6459 event_file = event_file_data(m->private);
6460 if (unlikely(!event_file)) {
6465 list_for_each_entry(data, &event_file->triggers, list) {
6466 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
6467 hist_trigger_show(m, data, n++);
6471 mutex_unlock(&event_mutex);
6476 static int event_hist_open(struct inode *inode, struct file *file)
6480 ret = security_locked_down(LOCKDOWN_TRACEFS);
6484 return single_open(file, hist_show, file);
6487 const struct file_operations event_hist_fops = {
6488 .open = event_hist_open,
6490 .llseek = seq_lseek,
6491 .release = single_release,
6494 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
6496 const char *field_name = hist_field_name(hist_field, 0);
6498 if (hist_field->var.name)
6499 seq_printf(m, "%s=", hist_field->var.name);
6501 if (hist_field->flags & HIST_FIELD_FL_CPU)
6503 else if (field_name) {
6504 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
6505 hist_field->flags & HIST_FIELD_FL_ALIAS)
6507 seq_printf(m, "%s", field_name);
6508 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
6509 seq_puts(m, "common_timestamp");
6511 if (hist_field->flags) {
6512 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
6513 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
6514 const char *flags = get_hist_field_flags(hist_field);
6517 seq_printf(m, ".%s", flags);
6522 static int event_hist_trigger_print(struct seq_file *m,
6523 struct event_trigger_ops *ops,
6524 struct event_trigger_data *data)
6526 struct hist_trigger_data *hist_data = data->private_data;
6527 struct hist_field *field;
6528 bool have_var = false;
6531 seq_puts(m, "hist:");
6534 seq_printf(m, "%s:", data->name);
6536 seq_puts(m, "keys=");
6538 for_each_hist_key_field(i, hist_data) {
6539 field = hist_data->fields[i];
6541 if (i > hist_data->n_vals)
6544 if (field->flags & HIST_FIELD_FL_STACKTRACE)
6545 seq_puts(m, "stacktrace");
6547 hist_field_print(m, field);
6550 seq_puts(m, ":vals=");
6552 for_each_hist_val_field(i, hist_data) {
6553 field = hist_data->fields[i];
6554 if (field->flags & HIST_FIELD_FL_VAR) {
6559 if (i == HITCOUNT_IDX)
6560 seq_puts(m, "hitcount");
6563 hist_field_print(m, field);
6572 for_each_hist_val_field(i, hist_data) {
6573 field = hist_data->fields[i];
6575 if (field->flags & HIST_FIELD_FL_VAR) {
6578 hist_field_print(m, field);
6583 seq_puts(m, ":sort=");
6585 for (i = 0; i < hist_data->n_sort_keys; i++) {
6586 struct tracing_map_sort_key *sort_key;
6587 unsigned int idx, first_key_idx;
6590 first_key_idx = hist_data->n_vals - hist_data->n_vars;
6592 sort_key = &hist_data->sort_keys[i];
6593 idx = sort_key->field_idx;
6595 if (WARN_ON(idx >= HIST_FIELDS_MAX))
6601 if (idx == HITCOUNT_IDX)
6602 seq_puts(m, "hitcount");
6604 if (idx >= first_key_idx)
6605 idx += hist_data->n_vars;
6606 hist_field_print(m, hist_data->fields[idx]);
6609 if (sort_key->descending)
6610 seq_puts(m, ".descending");
6612 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
6613 if (hist_data->enable_timestamps)
6614 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
6616 print_actions_spec(m, hist_data);
6618 if (data->filter_str)
6619 seq_printf(m, " if %s", data->filter_str);
6622 seq_puts(m, " [paused]");
6624 seq_puts(m, " [active]");
6631 static int event_hist_trigger_init(struct event_trigger_ops *ops,
6632 struct event_trigger_data *data)
6634 struct hist_trigger_data *hist_data = data->private_data;
6636 if (!data->ref && hist_data->attrs->name)
6637 save_named_trigger(hist_data->attrs->name, data);
6644 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
6646 struct trace_event_file *file;
6651 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6652 file = hist_data->field_var_hists[i]->hist_data->event_file;
6653 cmd = hist_data->field_var_hists[i]->cmd;
6654 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
6655 "!hist", "hist", cmd);
6659 static void event_hist_trigger_free(struct event_trigger_ops *ops,
6660 struct event_trigger_data *data)
6662 struct hist_trigger_data *hist_data = data->private_data;
6664 if (WARN_ON_ONCE(data->ref <= 0))
6670 del_named_trigger(data);
6672 trigger_data_free(data);
6674 remove_hist_vars(hist_data);
6676 unregister_field_var_hists(hist_data);
6678 destroy_hist_data(hist_data);
6682 static struct event_trigger_ops event_hist_trigger_ops = {
6683 .func = event_hist_trigger,
6684 .print = event_hist_trigger_print,
6685 .init = event_hist_trigger_init,
6686 .free = event_hist_trigger_free,
6689 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
6690 struct event_trigger_data *data)
6694 save_named_trigger(data->named_data->name, data);
6696 event_hist_trigger_init(ops, data->named_data);
6701 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
6702 struct event_trigger_data *data)
6704 if (WARN_ON_ONCE(data->ref <= 0))
6707 event_hist_trigger_free(ops, data->named_data);
6711 del_named_trigger(data);
6712 trigger_data_free(data);
6716 static struct event_trigger_ops event_hist_trigger_named_ops = {
6717 .func = event_hist_trigger,
6718 .print = event_hist_trigger_print,
6719 .init = event_hist_trigger_named_init,
6720 .free = event_hist_trigger_named_free,
6723 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6726 return &event_hist_trigger_ops;
6729 static void hist_clear(struct event_trigger_data *data)
6731 struct hist_trigger_data *hist_data = data->private_data;
6734 pause_named_trigger(data);
6736 tracepoint_synchronize_unregister();
6738 tracing_map_clear(hist_data->map);
6741 unpause_named_trigger(data);
6744 static bool compatible_field(struct ftrace_event_field *field,
6745 struct ftrace_event_field *test_field)
6747 if (field == test_field)
6749 if (field == NULL || test_field == NULL)
6751 if (strcmp(field->name, test_field->name) != 0)
6753 if (strcmp(field->type, test_field->type) != 0)
6755 if (field->size != test_field->size)
6757 if (field->is_signed != test_field->is_signed)
6763 static bool hist_trigger_match(struct event_trigger_data *data,
6764 struct event_trigger_data *data_test,
6765 struct event_trigger_data *named_data,
6768 struct tracing_map_sort_key *sort_key, *sort_key_test;
6769 struct hist_trigger_data *hist_data, *hist_data_test;
6770 struct hist_field *key_field, *key_field_test;
6773 if (named_data && (named_data != data_test) &&
6774 (named_data != data_test->named_data))
6777 if (!named_data && is_named_trigger(data_test))
6780 hist_data = data->private_data;
6781 hist_data_test = data_test->private_data;
6783 if (hist_data->n_vals != hist_data_test->n_vals ||
6784 hist_data->n_fields != hist_data_test->n_fields ||
6785 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6788 if (!ignore_filter) {
6789 if ((data->filter_str && !data_test->filter_str) ||
6790 (!data->filter_str && data_test->filter_str))
6794 for_each_hist_field(i, hist_data) {
6795 key_field = hist_data->fields[i];
6796 key_field_test = hist_data_test->fields[i];
6798 if (key_field->flags != key_field_test->flags)
6800 if (!compatible_field(key_field->field, key_field_test->field))
6802 if (key_field->offset != key_field_test->offset)
6804 if (key_field->size != key_field_test->size)
6806 if (key_field->is_signed != key_field_test->is_signed)
6808 if (!!key_field->var.name != !!key_field_test->var.name)
6810 if (key_field->var.name &&
6811 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6815 for (i = 0; i < hist_data->n_sort_keys; i++) {
6816 sort_key = &hist_data->sort_keys[i];
6817 sort_key_test = &hist_data_test->sort_keys[i];
6819 if (sort_key->field_idx != sort_key_test->field_idx ||
6820 sort_key->descending != sort_key_test->descending)
6824 if (!ignore_filter && data->filter_str &&
6825 (strcmp(data->filter_str, data_test->filter_str) != 0))
6828 if (!actions_match(hist_data, hist_data_test))
6834 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
6835 struct event_trigger_data *data,
6836 struct trace_event_file *file)
6838 struct hist_trigger_data *hist_data = data->private_data;
6839 struct event_trigger_data *test, *named_data = NULL;
6840 struct trace_array *tr = file->tr;
6843 if (hist_data->attrs->name) {
6844 named_data = find_named_trigger(hist_data->attrs->name);
6846 if (!hist_trigger_match(data, named_data, named_data,
6848 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6855 if (hist_data->attrs->name && !named_data)
6858 lockdep_assert_held(&event_mutex);
6860 list_for_each_entry(test, &file->triggers, list) {
6861 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6862 if (!hist_trigger_match(data, test, named_data, false))
6864 if (hist_data->attrs->pause)
6865 test->paused = true;
6866 else if (hist_data->attrs->cont)
6867 test->paused = false;
6868 else if (hist_data->attrs->clear)
6871 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6878 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6879 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6884 if (hist_data->attrs->pause)
6885 data->paused = true;
6888 data->private_data = named_data->private_data;
6889 set_named_trigger_data(data, named_data);
6890 data->ops = &event_hist_trigger_named_ops;
6893 if (data->ops->init) {
6894 ret = data->ops->init(data->ops, data);
6899 if (hist_data->enable_timestamps) {
6900 char *clock = hist_data->attrs->clock;
6902 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6904 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6908 tracing_set_time_stamp_abs(file->tr, true);
6912 destroy_hist_data(hist_data);
6919 static int hist_trigger_enable(struct event_trigger_data *data,
6920 struct trace_event_file *file)
6924 list_add_tail_rcu(&data->list, &file->triggers);
6926 update_cond_flag(file);
6928 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6929 list_del_rcu(&data->list);
6930 update_cond_flag(file);
6937 static bool have_hist_trigger_match(struct event_trigger_data *data,
6938 struct trace_event_file *file)
6940 struct hist_trigger_data *hist_data = data->private_data;
6941 struct event_trigger_data *test, *named_data = NULL;
6944 lockdep_assert_held(&event_mutex);
6946 if (hist_data->attrs->name)
6947 named_data = find_named_trigger(hist_data->attrs->name);
6949 list_for_each_entry(test, &file->triggers, list) {
6950 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6951 if (hist_trigger_match(data, test, named_data, false)) {
6961 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6962 struct trace_event_file *file)
6964 struct hist_trigger_data *hist_data = data->private_data;
6965 struct event_trigger_data *test, *named_data = NULL;
6967 lockdep_assert_held(&event_mutex);
6969 if (hist_data->attrs->name)
6970 named_data = find_named_trigger(hist_data->attrs->name);
6972 list_for_each_entry(test, &file->triggers, list) {
6973 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6974 if (!hist_trigger_match(data, test, named_data, false))
6976 hist_data = test->private_data;
6977 if (check_var_refs(hist_data))
6986 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6987 struct event_trigger_data *data,
6988 struct trace_event_file *file)
6990 struct hist_trigger_data *hist_data = data->private_data;
6991 struct event_trigger_data *test, *named_data = NULL;
6992 bool unregistered = false;
6994 lockdep_assert_held(&event_mutex);
6996 if (hist_data->attrs->name)
6997 named_data = find_named_trigger(hist_data->attrs->name);
6999 list_for_each_entry(test, &file->triggers, list) {
7000 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7001 if (!hist_trigger_match(data, test, named_data, false))
7003 unregistered = true;
7004 list_del_rcu(&test->list);
7005 trace_event_trigger_enable_disable(file, 0);
7006 update_cond_flag(file);
7011 if (unregistered && test->ops->free)
7012 test->ops->free(test->ops, test);
7014 if (hist_data->enable_timestamps) {
7015 if (!hist_data->remove || unregistered)
7016 tracing_set_time_stamp_abs(file->tr, false);
7020 static bool hist_file_check_refs(struct trace_event_file *file)
7022 struct hist_trigger_data *hist_data;
7023 struct event_trigger_data *test;
7025 lockdep_assert_held(&event_mutex);
7027 list_for_each_entry(test, &file->triggers, list) {
7028 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7029 hist_data = test->private_data;
7030 if (check_var_refs(hist_data))
7038 static void hist_unreg_all(struct trace_event_file *file)
7040 struct event_trigger_data *test, *n;
7041 struct hist_trigger_data *hist_data;
7042 struct synth_event *se;
7043 const char *se_name;
7045 lockdep_assert_held(&event_mutex);
7047 if (hist_file_check_refs(file))
7050 list_for_each_entry_safe(test, n, &file->triggers, list) {
7051 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7052 hist_data = test->private_data;
7053 list_del_rcu(&test->list);
7054 trace_event_trigger_enable_disable(file, 0);
7056 se_name = trace_event_name(file->event_call);
7057 se = find_synth_event(se_name);
7061 update_cond_flag(file);
7062 if (hist_data->enable_timestamps)
7063 tracing_set_time_stamp_abs(file->tr, false);
7064 if (test->ops->free)
7065 test->ops->free(test->ops, test);
7070 static int event_hist_trigger_func(struct event_command *cmd_ops,
7071 struct trace_event_file *file,
7072 char *glob, char *cmd, char *param)
7074 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
7075 struct event_trigger_data *trigger_data;
7076 struct hist_trigger_attrs *attrs;
7077 struct event_trigger_ops *trigger_ops;
7078 struct hist_trigger_data *hist_data;
7079 struct synth_event *se;
7080 const char *se_name;
7081 bool remove = false;
7085 lockdep_assert_held(&event_mutex);
7087 if (glob && strlen(glob)) {
7089 last_cmd_set(file, param);
7099 * separate the trigger from the filter (k:v [if filter])
7100 * allowing for whitespace in the trigger
7102 p = trigger = param;
7104 p = strstr(p, "if");
7109 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
7113 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
7115 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
7126 param = strstrip(p);
7127 trigger = strstrip(trigger);
7130 attrs = parse_hist_trigger_attrs(file->tr, trigger);
7132 return PTR_ERR(attrs);
7134 if (attrs->map_bits)
7135 hist_trigger_bits = attrs->map_bits;
7137 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
7138 if (IS_ERR(hist_data)) {
7139 destroy_hist_trigger_attrs(attrs);
7140 return PTR_ERR(hist_data);
7143 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
7145 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
7146 if (!trigger_data) {
7151 trigger_data->count = -1;
7152 trigger_data->ops = trigger_ops;
7153 trigger_data->cmd_ops = cmd_ops;
7155 INIT_LIST_HEAD(&trigger_data->list);
7156 RCU_INIT_POINTER(trigger_data->filter, NULL);
7158 trigger_data->private_data = hist_data;
7160 /* if param is non-empty, it's supposed to be a filter */
7161 if (param && cmd_ops->set_filter) {
7162 ret = cmd_ops->set_filter(param, trigger_data, file);
7168 if (!have_hist_trigger_match(trigger_data, file))
7171 if (hist_trigger_check_refs(trigger_data, file)) {
7176 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7177 se_name = trace_event_name(file->event_call);
7178 se = find_synth_event(se_name);
7185 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
7187 * The above returns on success the # of triggers registered,
7188 * but if it didn't register any it returns zero. Consider no
7189 * triggers registered a failure too.
7192 if (!(attrs->pause || attrs->cont || attrs->clear))
7198 if (get_named_trigger_data(trigger_data))
7201 if (has_hist_vars(hist_data))
7202 save_hist_vars(hist_data);
7204 ret = create_actions(hist_data);
7208 ret = tracing_map_init(hist_data->map);
7212 ret = hist_trigger_enable(trigger_data, file);
7216 se_name = trace_event_name(file->event_call);
7217 se = find_synth_event(se_name);
7220 /* Just return zero, not the number of registered triggers */
7228 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7230 if (cmd_ops->set_filter)
7231 cmd_ops->set_filter(NULL, trigger_data, NULL);
7233 remove_hist_vars(hist_data);
7235 kfree(trigger_data);
7237 destroy_hist_data(hist_data);
7241 static struct event_command trigger_hist_cmd = {
7243 .trigger_type = ETT_EVENT_HIST,
7244 .flags = EVENT_CMD_FL_NEEDS_REC,
7245 .func = event_hist_trigger_func,
7246 .reg = hist_register_trigger,
7247 .unreg = hist_unregister_trigger,
7248 .unreg_all = hist_unreg_all,
7249 .get_trigger_ops = event_hist_get_trigger_ops,
7250 .set_filter = set_trigger_filter,
7253 __init int register_trigger_hist_cmd(void)
7257 ret = register_event_command(&trigger_hist_cmd);
7264 hist_enable_trigger(struct event_trigger_data *data, void *rec,
7265 struct ring_buffer_event *event)
7267 struct enable_trigger_data *enable_data = data->private_data;
7268 struct event_trigger_data *test;
7270 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
7271 lockdep_is_held(&event_mutex)) {
7272 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7273 if (enable_data->enable)
7274 test->paused = false;
7276 test->paused = true;
7282 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
7283 struct ring_buffer_event *event)
7288 if (data->count != -1)
7291 hist_enable_trigger(data, rec, event);
7294 static struct event_trigger_ops hist_enable_trigger_ops = {
7295 .func = hist_enable_trigger,
7296 .print = event_enable_trigger_print,
7297 .init = event_trigger_init,
7298 .free = event_enable_trigger_free,
7301 static struct event_trigger_ops hist_enable_count_trigger_ops = {
7302 .func = hist_enable_count_trigger,
7303 .print = event_enable_trigger_print,
7304 .init = event_trigger_init,
7305 .free = event_enable_trigger_free,
7308 static struct event_trigger_ops hist_disable_trigger_ops = {
7309 .func = hist_enable_trigger,
7310 .print = event_enable_trigger_print,
7311 .init = event_trigger_init,
7312 .free = event_enable_trigger_free,
7315 static struct event_trigger_ops hist_disable_count_trigger_ops = {
7316 .func = hist_enable_count_trigger,
7317 .print = event_enable_trigger_print,
7318 .init = event_trigger_init,
7319 .free = event_enable_trigger_free,
7322 static struct event_trigger_ops *
7323 hist_enable_get_trigger_ops(char *cmd, char *param)
7325 struct event_trigger_ops *ops;
7328 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
7331 ops = param ? &hist_enable_count_trigger_ops :
7332 &hist_enable_trigger_ops;
7334 ops = param ? &hist_disable_count_trigger_ops :
7335 &hist_disable_trigger_ops;
7340 static void hist_enable_unreg_all(struct trace_event_file *file)
7342 struct event_trigger_data *test, *n;
7344 list_for_each_entry_safe(test, n, &file->triggers, list) {
7345 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
7346 list_del_rcu(&test->list);
7347 update_cond_flag(file);
7348 trace_event_trigger_enable_disable(file, 0);
7349 if (test->ops->free)
7350 test->ops->free(test->ops, test);
7355 static struct event_command trigger_hist_enable_cmd = {
7356 .name = ENABLE_HIST_STR,
7357 .trigger_type = ETT_HIST_ENABLE,
7358 .func = event_enable_trigger_func,
7359 .reg = event_enable_register_trigger,
7360 .unreg = event_enable_unregister_trigger,
7361 .unreg_all = hist_enable_unreg_all,
7362 .get_trigger_ops = hist_enable_get_trigger_ops,
7363 .set_filter = set_trigger_filter,
7366 static struct event_command trigger_hist_disable_cmd = {
7367 .name = DISABLE_HIST_STR,
7368 .trigger_type = ETT_HIST_ENABLE,
7369 .func = event_enable_trigger_func,
7370 .reg = event_enable_register_trigger,
7371 .unreg = event_enable_unregister_trigger,
7372 .unreg_all = hist_enable_unreg_all,
7373 .get_trigger_ops = hist_enable_get_trigger_ops,
7374 .set_filter = set_trigger_filter,
7377 static __init void unregister_trigger_hist_enable_disable_cmds(void)
7379 unregister_event_command(&trigger_hist_enable_cmd);
7380 unregister_event_command(&trigger_hist_disable_cmd);
7383 __init int register_trigger_hist_enable_disable_cmds(void)
7387 ret = register_event_command(&trigger_hist_enable_cmd);
7388 if (WARN_ON(ret < 0))
7390 ret = register_event_command(&trigger_hist_disable_cmd);
7391 if (WARN_ON(ret < 0))
7392 unregister_trigger_hist_enable_disable_cmds();
7397 static __init int trace_events_hist_init(void)
7399 struct dentry *entry = NULL;
7400 struct dentry *d_tracer;
7403 err = dyn_event_register(&synth_event_ops);
7405 pr_warn("Could not register synth_event_ops\n");
7409 d_tracer = tracing_init_dentry();
7410 if (IS_ERR(d_tracer)) {
7411 err = PTR_ERR(d_tracer);
7415 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
7416 NULL, &synth_events_fops);
7424 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
7429 fs_initcall(trace_events_hist_init);