]>
Commit | Line | Data |
---|---|---|
7ef224d1 TZ |
1 | /* |
2 | * trace_events_hist - trace event hist triggers | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * Copyright (C) 2015 Tom Zanussi <[email protected]> | |
15 | */ | |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/kallsyms.h> | |
19 | #include <linux/mutex.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/stacktrace.h> | |
22 | ||
23 | #include "tracing_map.h" | |
24 | #include "trace.h" | |
25 | ||
26 | struct hist_field; | |
27 | ||
28 | typedef u64 (*hist_field_fn_t) (struct hist_field *field, void *event); | |
29 | ||
30 | struct hist_field { | |
31 | struct ftrace_event_field *field; | |
32 | unsigned long flags; | |
33 | hist_field_fn_t fn; | |
34 | unsigned int size; | |
76a3b0c8 | 35 | unsigned int offset; |
7ef224d1 TZ |
36 | }; |
37 | ||
69a0200c TZ |
38 | static u64 hist_field_none(struct hist_field *field, void *event) |
39 | { | |
40 | return 0; | |
41 | } | |
42 | ||
7ef224d1 TZ |
43 | static u64 hist_field_counter(struct hist_field *field, void *event) |
44 | { | |
45 | return 1; | |
46 | } | |
47 | ||
48 | static u64 hist_field_string(struct hist_field *hist_field, void *event) | |
49 | { | |
50 | char *addr = (char *)(event + hist_field->field->offset); | |
51 | ||
52 | return (u64)(unsigned long)addr; | |
53 | } | |
54 | ||
79e577cb NK |
55 | static u64 hist_field_dynstring(struct hist_field *hist_field, void *event) |
56 | { | |
57 | u32 str_item = *(u32 *)(event + hist_field->field->offset); | |
58 | int str_loc = str_item & 0xffff; | |
59 | char *addr = (char *)(event + str_loc); | |
60 | ||
61 | return (u64)(unsigned long)addr; | |
62 | } | |
63 | ||
64 | static u64 hist_field_pstring(struct hist_field *hist_field, void *event) | |
65 | { | |
66 | char **addr = (char **)(event + hist_field->field->offset); | |
67 | ||
68 | return (u64)(unsigned long)*addr; | |
69 | } | |
70 | ||
7ef224d1 TZ |
71 | #define DEFINE_HIST_FIELD_FN(type) \ |
72 | static u64 hist_field_##type(struct hist_field *hist_field, void *event)\ | |
73 | { \ | |
74 | type *addr = (type *)(event + hist_field->field->offset); \ | |
75 | \ | |
79e577cb | 76 | return (u64)(unsigned long)*addr; \ |
7ef224d1 TZ |
77 | } |
78 | ||
79 | DEFINE_HIST_FIELD_FN(s64); | |
80 | DEFINE_HIST_FIELD_FN(u64); | |
81 | DEFINE_HIST_FIELD_FN(s32); | |
82 | DEFINE_HIST_FIELD_FN(u32); | |
83 | DEFINE_HIST_FIELD_FN(s16); | |
84 | DEFINE_HIST_FIELD_FN(u16); | |
85 | DEFINE_HIST_FIELD_FN(s8); | |
86 | DEFINE_HIST_FIELD_FN(u8); | |
87 | ||
88 | #define for_each_hist_field(i, hist_data) \ | |
89 | for ((i) = 0; (i) < (hist_data)->n_fields; (i)++) | |
90 | ||
91 | #define for_each_hist_val_field(i, hist_data) \ | |
92 | for ((i) = 0; (i) < (hist_data)->n_vals; (i)++) | |
93 | ||
94 | #define for_each_hist_key_field(i, hist_data) \ | |
95 | for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++) | |
96 | ||
69a0200c TZ |
97 | #define HIST_STACKTRACE_DEPTH 16 |
98 | #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long)) | |
99 | #define HIST_STACKTRACE_SKIP 5 | |
100 | ||
7ef224d1 | 101 | #define HITCOUNT_IDX 0 |
69a0200c | 102 | #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE) |
7ef224d1 TZ |
103 | |
104 | enum hist_field_flags { | |
c6afad49 TZ |
105 | HIST_FIELD_FL_HITCOUNT = 1, |
106 | HIST_FIELD_FL_KEY = 2, | |
107 | HIST_FIELD_FL_STRING = 4, | |
108 | HIST_FIELD_FL_HEX = 8, | |
109 | HIST_FIELD_FL_SYM = 16, | |
110 | HIST_FIELD_FL_SYM_OFFSET = 32, | |
6b4827ad | 111 | HIST_FIELD_FL_EXECNAME = 64, |
31696198 | 112 | HIST_FIELD_FL_SYSCALL = 128, |
69a0200c | 113 | HIST_FIELD_FL_STACKTRACE = 256, |
7ef224d1 TZ |
114 | }; |
115 | ||
116 | struct hist_trigger_attrs { | |
117 | char *keys_str; | |
f2606835 | 118 | char *vals_str; |
e62347d2 | 119 | char *sort_key_str; |
83e99914 TZ |
120 | bool pause; |
121 | bool cont; | |
e86ae9ba | 122 | bool clear; |
7ef224d1 TZ |
123 | unsigned int map_bits; |
124 | }; | |
125 | ||
126 | struct hist_trigger_data { | |
127 | struct hist_field *fields[TRACING_MAP_FIELDS_MAX]; | |
128 | unsigned int n_vals; | |
129 | unsigned int n_keys; | |
130 | unsigned int n_fields; | |
131 | unsigned int key_size; | |
132 | struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX]; | |
133 | unsigned int n_sort_keys; | |
134 | struct trace_event_file *event_file; | |
135 | struct hist_trigger_attrs *attrs; | |
136 | struct tracing_map *map; | |
137 | }; | |
138 | ||
139 | static hist_field_fn_t select_value_fn(int field_size, int field_is_signed) | |
140 | { | |
141 | hist_field_fn_t fn = NULL; | |
142 | ||
143 | switch (field_size) { | |
144 | case 8: | |
145 | if (field_is_signed) | |
146 | fn = hist_field_s64; | |
147 | else | |
148 | fn = hist_field_u64; | |
149 | break; | |
150 | case 4: | |
151 | if (field_is_signed) | |
152 | fn = hist_field_s32; | |
153 | else | |
154 | fn = hist_field_u32; | |
155 | break; | |
156 | case 2: | |
157 | if (field_is_signed) | |
158 | fn = hist_field_s16; | |
159 | else | |
160 | fn = hist_field_u16; | |
161 | break; | |
162 | case 1: | |
163 | if (field_is_signed) | |
164 | fn = hist_field_s8; | |
165 | else | |
166 | fn = hist_field_u8; | |
167 | break; | |
168 | } | |
169 | ||
170 | return fn; | |
171 | } | |
172 | ||
173 | static int parse_map_size(char *str) | |
174 | { | |
175 | unsigned long size, map_bits; | |
176 | int ret; | |
177 | ||
178 | strsep(&str, "="); | |
179 | if (!str) { | |
180 | ret = -EINVAL; | |
181 | goto out; | |
182 | } | |
183 | ||
184 | ret = kstrtoul(str, 0, &size); | |
185 | if (ret) | |
186 | goto out; | |
187 | ||
188 | map_bits = ilog2(roundup_pow_of_two(size)); | |
189 | if (map_bits < TRACING_MAP_BITS_MIN || | |
190 | map_bits > TRACING_MAP_BITS_MAX) | |
191 | ret = -EINVAL; | |
192 | else | |
193 | ret = map_bits; | |
194 | out: | |
195 | return ret; | |
196 | } | |
197 | ||
198 | static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs) | |
199 | { | |
200 | if (!attrs) | |
201 | return; | |
202 | ||
e62347d2 | 203 | kfree(attrs->sort_key_str); |
7ef224d1 | 204 | kfree(attrs->keys_str); |
f2606835 | 205 | kfree(attrs->vals_str); |
7ef224d1 TZ |
206 | kfree(attrs); |
207 | } | |
208 | ||
209 | static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str) | |
210 | { | |
211 | struct hist_trigger_attrs *attrs; | |
212 | int ret = 0; | |
213 | ||
214 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); | |
215 | if (!attrs) | |
216 | return ERR_PTR(-ENOMEM); | |
217 | ||
218 | while (trigger_str) { | |
219 | char *str = strsep(&trigger_str, ":"); | |
220 | ||
221 | if ((strncmp(str, "key=", strlen("key=")) == 0) || | |
222 | (strncmp(str, "keys=", strlen("keys=")) == 0)) | |
223 | attrs->keys_str = kstrdup(str, GFP_KERNEL); | |
f2606835 TZ |
224 | else if ((strncmp(str, "val=", strlen("val=")) == 0) || |
225 | (strncmp(str, "vals=", strlen("vals=")) == 0) || | |
226 | (strncmp(str, "values=", strlen("values=")) == 0)) | |
227 | attrs->vals_str = kstrdup(str, GFP_KERNEL); | |
e62347d2 TZ |
228 | else if (strncmp(str, "sort=", strlen("sort=")) == 0) |
229 | attrs->sort_key_str = kstrdup(str, GFP_KERNEL); | |
83e99914 TZ |
230 | else if (strcmp(str, "pause") == 0) |
231 | attrs->pause = true; | |
232 | else if ((strcmp(str, "cont") == 0) || | |
233 | (strcmp(str, "continue") == 0)) | |
234 | attrs->cont = true; | |
e86ae9ba TZ |
235 | else if (strcmp(str, "clear") == 0) |
236 | attrs->clear = true; | |
7ef224d1 TZ |
237 | else if (strncmp(str, "size=", strlen("size=")) == 0) { |
238 | int map_bits = parse_map_size(str); | |
239 | ||
240 | if (map_bits < 0) { | |
241 | ret = map_bits; | |
242 | goto free; | |
243 | } | |
244 | attrs->map_bits = map_bits; | |
245 | } else { | |
246 | ret = -EINVAL; | |
247 | goto free; | |
248 | } | |
249 | } | |
250 | ||
251 | if (!attrs->keys_str) { | |
252 | ret = -EINVAL; | |
253 | goto free; | |
254 | } | |
255 | ||
256 | return attrs; | |
257 | free: | |
258 | destroy_hist_trigger_attrs(attrs); | |
259 | ||
260 | return ERR_PTR(ret); | |
261 | } | |
262 | ||
6b4827ad TZ |
263 | static inline void save_comm(char *comm, struct task_struct *task) |
264 | { | |
265 | if (!task->pid) { | |
266 | strcpy(comm, "<idle>"); | |
267 | return; | |
268 | } | |
269 | ||
270 | if (WARN_ON_ONCE(task->pid < 0)) { | |
271 | strcpy(comm, "<XXX>"); | |
272 | return; | |
273 | } | |
274 | ||
275 | memcpy(comm, task->comm, TASK_COMM_LEN); | |
276 | } | |
277 | ||
278 | static void hist_trigger_elt_comm_free(struct tracing_map_elt *elt) | |
279 | { | |
280 | kfree((char *)elt->private_data); | |
281 | } | |
282 | ||
283 | static int hist_trigger_elt_comm_alloc(struct tracing_map_elt *elt) | |
284 | { | |
285 | struct hist_trigger_data *hist_data = elt->map->private_data; | |
286 | struct hist_field *key_field; | |
287 | unsigned int i; | |
288 | ||
289 | for_each_hist_key_field(i, hist_data) { | |
290 | key_field = hist_data->fields[i]; | |
291 | ||
292 | if (key_field->flags & HIST_FIELD_FL_EXECNAME) { | |
293 | unsigned int size = TASK_COMM_LEN + 1; | |
294 | ||
295 | elt->private_data = kzalloc(size, GFP_KERNEL); | |
296 | if (!elt->private_data) | |
297 | return -ENOMEM; | |
298 | break; | |
299 | } | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static void hist_trigger_elt_comm_copy(struct tracing_map_elt *to, | |
306 | struct tracing_map_elt *from) | |
307 | { | |
308 | char *comm_from = from->private_data; | |
309 | char *comm_to = to->private_data; | |
310 | ||
311 | if (comm_from) | |
312 | memcpy(comm_to, comm_from, TASK_COMM_LEN + 1); | |
313 | } | |
314 | ||
315 | static void hist_trigger_elt_comm_init(struct tracing_map_elt *elt) | |
316 | { | |
317 | char *comm = elt->private_data; | |
318 | ||
319 | if (comm) | |
320 | save_comm(comm, current); | |
321 | } | |
322 | ||
323 | static const struct tracing_map_ops hist_trigger_elt_comm_ops = { | |
324 | .elt_alloc = hist_trigger_elt_comm_alloc, | |
325 | .elt_copy = hist_trigger_elt_comm_copy, | |
326 | .elt_free = hist_trigger_elt_comm_free, | |
327 | .elt_init = hist_trigger_elt_comm_init, | |
328 | }; | |
329 | ||
7ef224d1 TZ |
330 | static void destroy_hist_field(struct hist_field *hist_field) |
331 | { | |
332 | kfree(hist_field); | |
333 | } | |
334 | ||
335 | static struct hist_field *create_hist_field(struct ftrace_event_field *field, | |
336 | unsigned long flags) | |
337 | { | |
338 | struct hist_field *hist_field; | |
339 | ||
340 | if (field && is_function_field(field)) | |
341 | return NULL; | |
342 | ||
343 | hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL); | |
344 | if (!hist_field) | |
345 | return NULL; | |
346 | ||
347 | if (flags & HIST_FIELD_FL_HITCOUNT) { | |
348 | hist_field->fn = hist_field_counter; | |
349 | goto out; | |
350 | } | |
351 | ||
69a0200c TZ |
352 | if (flags & HIST_FIELD_FL_STACKTRACE) { |
353 | hist_field->fn = hist_field_none; | |
354 | goto out; | |
355 | } | |
356 | ||
7ef224d1 TZ |
357 | if (is_string_field(field)) { |
358 | flags |= HIST_FIELD_FL_STRING; | |
79e577cb NK |
359 | |
360 | if (field->filter_type == FILTER_STATIC_STRING) | |
361 | hist_field->fn = hist_field_string; | |
362 | else if (field->filter_type == FILTER_DYN_STRING) | |
363 | hist_field->fn = hist_field_dynstring; | |
364 | else | |
365 | hist_field->fn = hist_field_pstring; | |
7ef224d1 TZ |
366 | } else { |
367 | hist_field->fn = select_value_fn(field->size, | |
368 | field->is_signed); | |
369 | if (!hist_field->fn) { | |
370 | destroy_hist_field(hist_field); | |
371 | return NULL; | |
372 | } | |
373 | } | |
374 | out: | |
375 | hist_field->field = field; | |
376 | hist_field->flags = flags; | |
377 | ||
378 | return hist_field; | |
379 | } | |
380 | ||
381 | static void destroy_hist_fields(struct hist_trigger_data *hist_data) | |
382 | { | |
383 | unsigned int i; | |
384 | ||
385 | for (i = 0; i < TRACING_MAP_FIELDS_MAX; i++) { | |
386 | if (hist_data->fields[i]) { | |
387 | destroy_hist_field(hist_data->fields[i]); | |
388 | hist_data->fields[i] = NULL; | |
389 | } | |
390 | } | |
391 | } | |
392 | ||
393 | static int create_hitcount_val(struct hist_trigger_data *hist_data) | |
394 | { | |
395 | hist_data->fields[HITCOUNT_IDX] = | |
396 | create_hist_field(NULL, HIST_FIELD_FL_HITCOUNT); | |
397 | if (!hist_data->fields[HITCOUNT_IDX]) | |
398 | return -ENOMEM; | |
399 | ||
400 | hist_data->n_vals++; | |
401 | ||
402 | if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX)) | |
403 | return -EINVAL; | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
f2606835 TZ |
408 | static int create_val_field(struct hist_trigger_data *hist_data, |
409 | unsigned int val_idx, | |
410 | struct trace_event_file *file, | |
411 | char *field_str) | |
412 | { | |
413 | struct ftrace_event_field *field = NULL; | |
414 | unsigned long flags = 0; | |
0c4a6b46 | 415 | char *field_name; |
f2606835 TZ |
416 | int ret = 0; |
417 | ||
418 | if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX)) | |
419 | return -EINVAL; | |
0c4a6b46 TZ |
420 | |
421 | field_name = strsep(&field_str, "."); | |
422 | if (field_str) { | |
423 | if (strcmp(field_str, "hex") == 0) | |
424 | flags |= HIST_FIELD_FL_HEX; | |
425 | else { | |
426 | ret = -EINVAL; | |
427 | goto out; | |
428 | } | |
429 | } | |
430 | ||
431 | field = trace_find_event_field(file->event_call, field_name); | |
f2606835 TZ |
432 | if (!field) { |
433 | ret = -EINVAL; | |
434 | goto out; | |
435 | } | |
436 | ||
437 | hist_data->fields[val_idx] = create_hist_field(field, flags); | |
438 | if (!hist_data->fields[val_idx]) { | |
439 | ret = -ENOMEM; | |
440 | goto out; | |
441 | } | |
442 | ||
443 | ++hist_data->n_vals; | |
444 | ||
445 | if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX)) | |
446 | ret = -EINVAL; | |
447 | out: | |
448 | return ret; | |
449 | } | |
450 | ||
7ef224d1 TZ |
451 | static int create_val_fields(struct hist_trigger_data *hist_data, |
452 | struct trace_event_file *file) | |
453 | { | |
f2606835 TZ |
454 | char *fields_str, *field_str; |
455 | unsigned int i, j; | |
7ef224d1 TZ |
456 | int ret; |
457 | ||
458 | ret = create_hitcount_val(hist_data); | |
f2606835 TZ |
459 | if (ret) |
460 | goto out; | |
7ef224d1 | 461 | |
f2606835 TZ |
462 | fields_str = hist_data->attrs->vals_str; |
463 | if (!fields_str) | |
464 | goto out; | |
465 | ||
466 | strsep(&fields_str, "="); | |
467 | if (!fields_str) | |
468 | goto out; | |
469 | ||
470 | for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX && | |
471 | j < TRACING_MAP_VALS_MAX; i++) { | |
472 | field_str = strsep(&fields_str, ","); | |
473 | if (!field_str) | |
474 | break; | |
475 | if (strcmp(field_str, "hitcount") == 0) | |
476 | continue; | |
477 | ret = create_val_field(hist_data, j++, file, field_str); | |
478 | if (ret) | |
479 | goto out; | |
480 | } | |
481 | if (fields_str && (strcmp(fields_str, "hitcount") != 0)) | |
482 | ret = -EINVAL; | |
483 | out: | |
7ef224d1 TZ |
484 | return ret; |
485 | } | |
486 | ||
487 | static int create_key_field(struct hist_trigger_data *hist_data, | |
488 | unsigned int key_idx, | |
76a3b0c8 | 489 | unsigned int key_offset, |
7ef224d1 TZ |
490 | struct trace_event_file *file, |
491 | char *field_str) | |
492 | { | |
493 | struct ftrace_event_field *field = NULL; | |
494 | unsigned long flags = 0; | |
495 | unsigned int key_size; | |
496 | int ret = 0; | |
497 | ||
498 | if (WARN_ON(key_idx >= TRACING_MAP_FIELDS_MAX)) | |
499 | return -EINVAL; | |
500 | ||
501 | flags |= HIST_FIELD_FL_KEY; | |
502 | ||
69a0200c TZ |
503 | if (strcmp(field_str, "stacktrace") == 0) { |
504 | flags |= HIST_FIELD_FL_STACKTRACE; | |
505 | key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH; | |
506 | } else { | |
507 | char *field_name = strsep(&field_str, "."); | |
508 | ||
509 | if (field_str) { | |
510 | if (strcmp(field_str, "hex") == 0) | |
511 | flags |= HIST_FIELD_FL_HEX; | |
512 | else if (strcmp(field_str, "sym") == 0) | |
513 | flags |= HIST_FIELD_FL_SYM; | |
514 | else if (strcmp(field_str, "sym-offset") == 0) | |
515 | flags |= HIST_FIELD_FL_SYM_OFFSET; | |
516 | else if ((strcmp(field_str, "execname") == 0) && | |
517 | (strcmp(field_name, "common_pid") == 0)) | |
518 | flags |= HIST_FIELD_FL_EXECNAME; | |
519 | else if (strcmp(field_str, "syscall") == 0) | |
520 | flags |= HIST_FIELD_FL_SYSCALL; | |
521 | else { | |
522 | ret = -EINVAL; | |
523 | goto out; | |
524 | } | |
525 | } | |
526 | ||
527 | field = trace_find_event_field(file->event_call, field_name); | |
528 | if (!field) { | |
0c4a6b46 TZ |
529 | ret = -EINVAL; |
530 | goto out; | |
531 | } | |
0c4a6b46 | 532 | |
79e577cb NK |
533 | if (is_string_field(field)) /* should be last key field */ |
534 | key_size = HIST_KEY_SIZE_MAX - key_offset; | |
535 | else | |
536 | key_size = field->size; | |
7ef224d1 TZ |
537 | } |
538 | ||
7ef224d1 TZ |
539 | hist_data->fields[key_idx] = create_hist_field(field, flags); |
540 | if (!hist_data->fields[key_idx]) { | |
541 | ret = -ENOMEM; | |
542 | goto out; | |
543 | } | |
544 | ||
545 | key_size = ALIGN(key_size, sizeof(u64)); | |
546 | hist_data->fields[key_idx]->size = key_size; | |
76a3b0c8 TZ |
547 | hist_data->fields[key_idx]->offset = key_offset; |
548 | hist_data->key_size += key_size; | |
7ef224d1 TZ |
549 | if (hist_data->key_size > HIST_KEY_SIZE_MAX) { |
550 | ret = -EINVAL; | |
551 | goto out; | |
552 | } | |
553 | ||
554 | hist_data->n_keys++; | |
555 | ||
556 | if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX)) | |
557 | return -EINVAL; | |
558 | ||
559 | ret = key_size; | |
560 | out: | |
561 | return ret; | |
562 | } | |
563 | ||
564 | static int create_key_fields(struct hist_trigger_data *hist_data, | |
565 | struct trace_event_file *file) | |
566 | { | |
76a3b0c8 | 567 | unsigned int i, key_offset = 0, n_vals = hist_data->n_vals; |
7ef224d1 TZ |
568 | char *fields_str, *field_str; |
569 | int ret = -EINVAL; | |
570 | ||
571 | fields_str = hist_data->attrs->keys_str; | |
572 | if (!fields_str) | |
573 | goto out; | |
574 | ||
575 | strsep(&fields_str, "="); | |
576 | if (!fields_str) | |
577 | goto out; | |
578 | ||
76a3b0c8 | 579 | for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) { |
7ef224d1 TZ |
580 | field_str = strsep(&fields_str, ","); |
581 | if (!field_str) | |
582 | break; | |
76a3b0c8 TZ |
583 | ret = create_key_field(hist_data, i, key_offset, |
584 | file, field_str); | |
7ef224d1 TZ |
585 | if (ret < 0) |
586 | goto out; | |
76a3b0c8 | 587 | key_offset += ret; |
7ef224d1 TZ |
588 | } |
589 | if (fields_str) { | |
590 | ret = -EINVAL; | |
591 | goto out; | |
592 | } | |
593 | ret = 0; | |
594 | out: | |
595 | return ret; | |
596 | } | |
597 | ||
598 | static int create_hist_fields(struct hist_trigger_data *hist_data, | |
599 | struct trace_event_file *file) | |
600 | { | |
601 | int ret; | |
602 | ||
603 | ret = create_val_fields(hist_data, file); | |
604 | if (ret) | |
605 | goto out; | |
606 | ||
607 | ret = create_key_fields(hist_data, file); | |
608 | if (ret) | |
609 | goto out; | |
610 | ||
611 | hist_data->n_fields = hist_data->n_vals + hist_data->n_keys; | |
612 | out: | |
613 | return ret; | |
614 | } | |
615 | ||
e62347d2 TZ |
616 | static int is_descending(const char *str) |
617 | { | |
618 | if (!str) | |
619 | return 0; | |
620 | ||
621 | if (strcmp(str, "descending") == 0) | |
622 | return 1; | |
623 | ||
624 | if (strcmp(str, "ascending") == 0) | |
625 | return 0; | |
626 | ||
627 | return -EINVAL; | |
628 | } | |
629 | ||
7ef224d1 TZ |
630 | static int create_sort_keys(struct hist_trigger_data *hist_data) |
631 | { | |
e62347d2 TZ |
632 | char *fields_str = hist_data->attrs->sort_key_str; |
633 | struct ftrace_event_field *field = NULL; | |
634 | struct tracing_map_sort_key *sort_key; | |
635 | int descending, ret = 0; | |
636 | unsigned int i, j; | |
637 | ||
638 | hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */ | |
639 | ||
640 | if (!fields_str) | |
641 | goto out; | |
642 | ||
643 | strsep(&fields_str, "="); | |
644 | if (!fields_str) { | |
645 | ret = -EINVAL; | |
646 | goto out; | |
647 | } | |
648 | ||
649 | for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) { | |
650 | char *field_str, *field_name; | |
651 | ||
652 | sort_key = &hist_data->sort_keys[i]; | |
653 | ||
654 | field_str = strsep(&fields_str, ","); | |
655 | if (!field_str) { | |
656 | if (i == 0) | |
657 | ret = -EINVAL; | |
658 | break; | |
659 | } | |
660 | ||
661 | if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) { | |
662 | ret = -EINVAL; | |
663 | break; | |
664 | } | |
7ef224d1 | 665 | |
e62347d2 TZ |
666 | field_name = strsep(&field_str, "."); |
667 | if (!field_name) { | |
668 | ret = -EINVAL; | |
669 | break; | |
670 | } | |
671 | ||
672 | if (strcmp(field_name, "hitcount") == 0) { | |
673 | descending = is_descending(field_str); | |
674 | if (descending < 0) { | |
675 | ret = descending; | |
676 | break; | |
677 | } | |
678 | sort_key->descending = descending; | |
679 | continue; | |
680 | } | |
7ef224d1 | 681 | |
e62347d2 TZ |
682 | for (j = 1; j < hist_data->n_fields; j++) { |
683 | field = hist_data->fields[j]->field; | |
684 | if (field && (strcmp(field_name, field->name) == 0)) { | |
685 | sort_key->field_idx = j; | |
686 | descending = is_descending(field_str); | |
687 | if (descending < 0) { | |
688 | ret = descending; | |
689 | goto out; | |
690 | } | |
691 | sort_key->descending = descending; | |
692 | break; | |
693 | } | |
694 | } | |
695 | if (j == hist_data->n_fields) { | |
696 | ret = -EINVAL; | |
697 | break; | |
698 | } | |
699 | } | |
700 | hist_data->n_sort_keys = i; | |
701 | out: | |
7ef224d1 TZ |
702 | return ret; |
703 | } | |
704 | ||
705 | static void destroy_hist_data(struct hist_trigger_data *hist_data) | |
706 | { | |
707 | destroy_hist_trigger_attrs(hist_data->attrs); | |
708 | destroy_hist_fields(hist_data); | |
709 | tracing_map_destroy(hist_data->map); | |
710 | kfree(hist_data); | |
711 | } | |
712 | ||
713 | static int create_tracing_map_fields(struct hist_trigger_data *hist_data) | |
714 | { | |
715 | struct tracing_map *map = hist_data->map; | |
716 | struct ftrace_event_field *field; | |
717 | struct hist_field *hist_field; | |
718 | unsigned int i, idx; | |
719 | ||
720 | for_each_hist_field(i, hist_data) { | |
721 | hist_field = hist_data->fields[i]; | |
722 | if (hist_field->flags & HIST_FIELD_FL_KEY) { | |
723 | tracing_map_cmp_fn_t cmp_fn; | |
724 | ||
725 | field = hist_field->field; | |
726 | ||
69a0200c TZ |
727 | if (hist_field->flags & HIST_FIELD_FL_STACKTRACE) |
728 | cmp_fn = tracing_map_cmp_none; | |
729 | else if (is_string_field(field)) | |
7ef224d1 TZ |
730 | cmp_fn = tracing_map_cmp_string; |
731 | else | |
732 | cmp_fn = tracing_map_cmp_num(field->size, | |
733 | field->is_signed); | |
76a3b0c8 TZ |
734 | idx = tracing_map_add_key_field(map, |
735 | hist_field->offset, | |
736 | cmp_fn); | |
737 | ||
7ef224d1 TZ |
738 | } else |
739 | idx = tracing_map_add_sum_field(map); | |
740 | ||
741 | if (idx < 0) | |
742 | return idx; | |
743 | } | |
744 | ||
745 | return 0; | |
746 | } | |
747 | ||
6b4827ad TZ |
748 | static bool need_tracing_map_ops(struct hist_trigger_data *hist_data) |
749 | { | |
750 | struct hist_field *key_field; | |
751 | unsigned int i; | |
752 | ||
753 | for_each_hist_key_field(i, hist_data) { | |
754 | key_field = hist_data->fields[i]; | |
755 | ||
756 | if (key_field->flags & HIST_FIELD_FL_EXECNAME) | |
757 | return true; | |
758 | } | |
759 | ||
760 | return false; | |
761 | } | |
762 | ||
7ef224d1 TZ |
763 | static struct hist_trigger_data * |
764 | create_hist_data(unsigned int map_bits, | |
765 | struct hist_trigger_attrs *attrs, | |
766 | struct trace_event_file *file) | |
767 | { | |
6b4827ad | 768 | const struct tracing_map_ops *map_ops = NULL; |
7ef224d1 TZ |
769 | struct hist_trigger_data *hist_data; |
770 | int ret = 0; | |
771 | ||
772 | hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL); | |
773 | if (!hist_data) | |
774 | return ERR_PTR(-ENOMEM); | |
775 | ||
776 | hist_data->attrs = attrs; | |
777 | ||
778 | ret = create_hist_fields(hist_data, file); | |
779 | if (ret) | |
780 | goto free; | |
781 | ||
782 | ret = create_sort_keys(hist_data); | |
783 | if (ret) | |
784 | goto free; | |
785 | ||
6b4827ad TZ |
786 | if (need_tracing_map_ops(hist_data)) |
787 | map_ops = &hist_trigger_elt_comm_ops; | |
788 | ||
7ef224d1 | 789 | hist_data->map = tracing_map_create(map_bits, hist_data->key_size, |
6b4827ad | 790 | map_ops, hist_data); |
7ef224d1 TZ |
791 | if (IS_ERR(hist_data->map)) { |
792 | ret = PTR_ERR(hist_data->map); | |
793 | hist_data->map = NULL; | |
794 | goto free; | |
795 | } | |
796 | ||
797 | ret = create_tracing_map_fields(hist_data); | |
798 | if (ret) | |
799 | goto free; | |
800 | ||
801 | ret = tracing_map_init(hist_data->map); | |
802 | if (ret) | |
803 | goto free; | |
804 | ||
805 | hist_data->event_file = file; | |
806 | out: | |
807 | return hist_data; | |
808 | free: | |
809 | hist_data->attrs = NULL; | |
810 | ||
811 | destroy_hist_data(hist_data); | |
812 | ||
813 | hist_data = ERR_PTR(ret); | |
814 | ||
815 | goto out; | |
816 | } | |
817 | ||
818 | static void hist_trigger_elt_update(struct hist_trigger_data *hist_data, | |
819 | struct tracing_map_elt *elt, | |
820 | void *rec) | |
821 | { | |
822 | struct hist_field *hist_field; | |
823 | unsigned int i; | |
824 | u64 hist_val; | |
825 | ||
826 | for_each_hist_val_field(i, hist_data) { | |
827 | hist_field = hist_data->fields[i]; | |
828 | hist_val = hist_field->fn(hist_field, rec); | |
829 | tracing_map_update_sum(elt, i, hist_val); | |
830 | } | |
831 | } | |
832 | ||
833 | static void event_hist_trigger(struct event_trigger_data *data, void *rec) | |
834 | { | |
835 | struct hist_trigger_data *hist_data = data->private_data; | |
69a0200c | 836 | unsigned long entries[HIST_STACKTRACE_DEPTH]; |
76a3b0c8 | 837 | char compound_key[HIST_KEY_SIZE_MAX]; |
69a0200c | 838 | struct stack_trace stacktrace; |
7ef224d1 TZ |
839 | struct hist_field *key_field; |
840 | struct tracing_map_elt *elt; | |
841 | u64 field_contents; | |
842 | void *key = NULL; | |
843 | unsigned int i; | |
844 | ||
76a3b0c8 TZ |
845 | if (hist_data->n_keys > 1) |
846 | memset(compound_key, 0, hist_data->key_size); | |
847 | ||
7ef224d1 TZ |
848 | for_each_hist_key_field(i, hist_data) { |
849 | key_field = hist_data->fields[i]; | |
850 | ||
69a0200c TZ |
851 | if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { |
852 | stacktrace.max_entries = HIST_STACKTRACE_DEPTH; | |
853 | stacktrace.entries = entries; | |
854 | stacktrace.nr_entries = 0; | |
855 | stacktrace.skip = HIST_STACKTRACE_SKIP; | |
76a3b0c8 | 856 | |
69a0200c TZ |
857 | memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE); |
858 | save_stack_trace(&stacktrace); | |
859 | ||
860 | key = entries; | |
861 | } else { | |
862 | field_contents = key_field->fn(key_field, rec); | |
863 | if (key_field->flags & HIST_FIELD_FL_STRING) | |
864 | key = (void *)(unsigned long)field_contents; | |
865 | else | |
866 | key = (void *)&field_contents; | |
867 | ||
868 | if (hist_data->n_keys > 1) { | |
79e577cb NK |
869 | /* ensure NULL-termination */ |
870 | size_t size = key_field->size - 1; | |
871 | ||
872 | if (key_field->flags & HIST_FIELD_FL_STRING) { | |
873 | struct ftrace_event_field *field; | |
874 | ||
875 | field = key_field->field; | |
876 | if (field->filter_type == FILTER_DYN_STRING) | |
877 | size = *(u32 *)(rec + field->offset) >> 16; | |
878 | else if (field->filter_type == FILTER_PTR_STRING) | |
879 | size = strlen(key); | |
880 | ||
881 | if (size > key_field->size - 1) | |
882 | size = key_field->size - 1; | |
883 | } | |
884 | ||
69a0200c | 885 | memcpy(compound_key + key_field->offset, key, |
79e577cb | 886 | size); |
69a0200c | 887 | } |
76a3b0c8 | 888 | } |
7ef224d1 TZ |
889 | } |
890 | ||
76a3b0c8 TZ |
891 | if (hist_data->n_keys > 1) |
892 | key = compound_key; | |
893 | ||
7ef224d1 TZ |
894 | elt = tracing_map_insert(hist_data->map, key); |
895 | if (elt) | |
896 | hist_trigger_elt_update(hist_data, elt, rec); | |
897 | } | |
898 | ||
69a0200c TZ |
899 | static void hist_trigger_stacktrace_print(struct seq_file *m, |
900 | unsigned long *stacktrace_entries, | |
901 | unsigned int max_entries) | |
902 | { | |
903 | char str[KSYM_SYMBOL_LEN]; | |
904 | unsigned int spaces = 8; | |
905 | unsigned int i; | |
906 | ||
907 | for (i = 0; i < max_entries; i++) { | |
908 | if (stacktrace_entries[i] == ULONG_MAX) | |
909 | return; | |
910 | ||
911 | seq_printf(m, "%*c", 1 + spaces, ' '); | |
912 | sprint_symbol(str, stacktrace_entries[i]); | |
913 | seq_printf(m, "%s\n", str); | |
914 | } | |
915 | } | |
916 | ||
7ef224d1 TZ |
917 | static void |
918 | hist_trigger_entry_print(struct seq_file *m, | |
919 | struct hist_trigger_data *hist_data, void *key, | |
920 | struct tracing_map_elt *elt) | |
921 | { | |
922 | struct hist_field *key_field; | |
c6afad49 | 923 | char str[KSYM_SYMBOL_LEN]; |
69a0200c | 924 | bool multiline = false; |
7ef224d1 TZ |
925 | unsigned int i; |
926 | u64 uval; | |
927 | ||
928 | seq_puts(m, "{ "); | |
929 | ||
930 | for_each_hist_key_field(i, hist_data) { | |
931 | key_field = hist_data->fields[i]; | |
932 | ||
933 | if (i > hist_data->n_vals) | |
934 | seq_puts(m, ", "); | |
935 | ||
0c4a6b46 TZ |
936 | if (key_field->flags & HIST_FIELD_FL_HEX) { |
937 | uval = *(u64 *)(key + key_field->offset); | |
938 | seq_printf(m, "%s: %llx", | |
939 | key_field->field->name, uval); | |
c6afad49 TZ |
940 | } else if (key_field->flags & HIST_FIELD_FL_SYM) { |
941 | uval = *(u64 *)(key + key_field->offset); | |
942 | sprint_symbol_no_offset(str, uval); | |
943 | seq_printf(m, "%s: [%llx] %-45s", | |
944 | key_field->field->name, uval, str); | |
945 | } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) { | |
946 | uval = *(u64 *)(key + key_field->offset); | |
947 | sprint_symbol(str, uval); | |
948 | seq_printf(m, "%s: [%llx] %-55s", | |
949 | key_field->field->name, uval, str); | |
6b4827ad TZ |
950 | } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) { |
951 | char *comm = elt->private_data; | |
952 | ||
953 | uval = *(u64 *)(key + key_field->offset); | |
954 | seq_printf(m, "%s: %-16s[%10llu]", | |
955 | key_field->field->name, comm, uval); | |
31696198 TZ |
956 | } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) { |
957 | const char *syscall_name; | |
958 | ||
959 | uval = *(u64 *)(key + key_field->offset); | |
960 | syscall_name = get_syscall_name(uval); | |
961 | if (!syscall_name) | |
962 | syscall_name = "unknown_syscall"; | |
963 | ||
964 | seq_printf(m, "%s: %-30s[%3llu]", | |
965 | key_field->field->name, syscall_name, uval); | |
69a0200c TZ |
966 | } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { |
967 | seq_puts(m, "stacktrace:\n"); | |
968 | hist_trigger_stacktrace_print(m, | |
969 | key + key_field->offset, | |
970 | HIST_STACKTRACE_DEPTH); | |
971 | multiline = true; | |
0c4a6b46 | 972 | } else if (key_field->flags & HIST_FIELD_FL_STRING) { |
7ef224d1 | 973 | seq_printf(m, "%s: %-50s", key_field->field->name, |
76a3b0c8 | 974 | (char *)(key + key_field->offset)); |
7ef224d1 | 975 | } else { |
76a3b0c8 TZ |
976 | uval = *(u64 *)(key + key_field->offset); |
977 | seq_printf(m, "%s: %10llu", key_field->field->name, | |
978 | uval); | |
7ef224d1 TZ |
979 | } |
980 | } | |
981 | ||
69a0200c TZ |
982 | if (!multiline) |
983 | seq_puts(m, " "); | |
984 | ||
985 | seq_puts(m, "}"); | |
7ef224d1 TZ |
986 | |
987 | seq_printf(m, " hitcount: %10llu", | |
988 | tracing_map_read_sum(elt, HITCOUNT_IDX)); | |
989 | ||
f2606835 | 990 | for (i = 1; i < hist_data->n_vals; i++) { |
0c4a6b46 TZ |
991 | if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) { |
992 | seq_printf(m, " %s: %10llx", | |
993 | hist_data->fields[i]->field->name, | |
994 | tracing_map_read_sum(elt, i)); | |
995 | } else { | |
996 | seq_printf(m, " %s: %10llu", | |
997 | hist_data->fields[i]->field->name, | |
998 | tracing_map_read_sum(elt, i)); | |
999 | } | |
f2606835 TZ |
1000 | } |
1001 | ||
7ef224d1 TZ |
1002 | seq_puts(m, "\n"); |
1003 | } | |
1004 | ||
1005 | static int print_entries(struct seq_file *m, | |
1006 | struct hist_trigger_data *hist_data) | |
1007 | { | |
1008 | struct tracing_map_sort_entry **sort_entries = NULL; | |
1009 | struct tracing_map *map = hist_data->map; | |
1010 | unsigned int i, n_entries; | |
1011 | ||
1012 | n_entries = tracing_map_sort_entries(map, hist_data->sort_keys, | |
1013 | hist_data->n_sort_keys, | |
1014 | &sort_entries); | |
1015 | if (n_entries < 0) | |
1016 | return n_entries; | |
1017 | ||
1018 | for (i = 0; i < n_entries; i++) | |
1019 | hist_trigger_entry_print(m, hist_data, | |
1020 | sort_entries[i]->key, | |
1021 | sort_entries[i]->elt); | |
1022 | ||
1023 | tracing_map_destroy_sort_entries(sort_entries, n_entries); | |
1024 | ||
1025 | return n_entries; | |
1026 | } | |
1027 | ||
1028 | static int hist_show(struct seq_file *m, void *v) | |
1029 | { | |
1030 | struct event_trigger_data *test, *data = NULL; | |
1031 | struct trace_event_file *event_file; | |
1032 | struct hist_trigger_data *hist_data; | |
1033 | int n_entries, ret = 0; | |
1034 | ||
1035 | mutex_lock(&event_mutex); | |
1036 | ||
1037 | event_file = event_file_data(m->private); | |
1038 | if (unlikely(!event_file)) { | |
1039 | ret = -ENODEV; | |
1040 | goto out_unlock; | |
1041 | } | |
1042 | ||
1043 | list_for_each_entry_rcu(test, &event_file->triggers, list) { | |
1044 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { | |
1045 | data = test; | |
1046 | break; | |
1047 | } | |
1048 | } | |
1049 | if (!data) | |
1050 | goto out_unlock; | |
1051 | ||
1052 | seq_puts(m, "# event histogram\n#\n# trigger info: "); | |
1053 | data->ops->print(m, data->ops, data); | |
1054 | seq_puts(m, "\n"); | |
1055 | ||
1056 | hist_data = data->private_data; | |
1057 | n_entries = print_entries(m, hist_data); | |
1058 | if (n_entries < 0) { | |
1059 | ret = n_entries; | |
1060 | n_entries = 0; | |
1061 | } | |
1062 | ||
1063 | seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n", | |
1064 | (u64)atomic64_read(&hist_data->map->hits), | |
1065 | n_entries, (u64)atomic64_read(&hist_data->map->drops)); | |
1066 | out_unlock: | |
1067 | mutex_unlock(&event_mutex); | |
1068 | ||
1069 | return ret; | |
1070 | } | |
1071 | ||
1072 | static int event_hist_open(struct inode *inode, struct file *file) | |
1073 | { | |
1074 | return single_open(file, hist_show, file); | |
1075 | } | |
1076 | ||
1077 | const struct file_operations event_hist_fops = { | |
1078 | .open = event_hist_open, | |
1079 | .read = seq_read, | |
1080 | .llseek = seq_lseek, | |
1081 | .release = single_release, | |
1082 | }; | |
1083 | ||
0c4a6b46 TZ |
1084 | static const char *get_hist_field_flags(struct hist_field *hist_field) |
1085 | { | |
1086 | const char *flags_str = NULL; | |
1087 | ||
1088 | if (hist_field->flags & HIST_FIELD_FL_HEX) | |
1089 | flags_str = "hex"; | |
c6afad49 TZ |
1090 | else if (hist_field->flags & HIST_FIELD_FL_SYM) |
1091 | flags_str = "sym"; | |
1092 | else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET) | |
1093 | flags_str = "sym-offset"; | |
6b4827ad TZ |
1094 | else if (hist_field->flags & HIST_FIELD_FL_EXECNAME) |
1095 | flags_str = "execname"; | |
31696198 TZ |
1096 | else if (hist_field->flags & HIST_FIELD_FL_SYSCALL) |
1097 | flags_str = "syscall"; | |
0c4a6b46 TZ |
1098 | |
1099 | return flags_str; | |
1100 | } | |
1101 | ||
7ef224d1 TZ |
1102 | static void hist_field_print(struct seq_file *m, struct hist_field *hist_field) |
1103 | { | |
1104 | seq_printf(m, "%s", hist_field->field->name); | |
0c4a6b46 TZ |
1105 | if (hist_field->flags) { |
1106 | const char *flags_str = get_hist_field_flags(hist_field); | |
1107 | ||
1108 | if (flags_str) | |
1109 | seq_printf(m, ".%s", flags_str); | |
1110 | } | |
7ef224d1 TZ |
1111 | } |
1112 | ||
1113 | static int event_hist_trigger_print(struct seq_file *m, | |
1114 | struct event_trigger_ops *ops, | |
1115 | struct event_trigger_data *data) | |
1116 | { | |
1117 | struct hist_trigger_data *hist_data = data->private_data; | |
1118 | struct hist_field *key_field; | |
1119 | unsigned int i; | |
1120 | ||
1121 | seq_puts(m, "hist:keys="); | |
1122 | ||
1123 | for_each_hist_key_field(i, hist_data) { | |
1124 | key_field = hist_data->fields[i]; | |
1125 | ||
1126 | if (i > hist_data->n_vals) | |
1127 | seq_puts(m, ","); | |
1128 | ||
69a0200c TZ |
1129 | if (key_field->flags & HIST_FIELD_FL_STACKTRACE) |
1130 | seq_puts(m, "stacktrace"); | |
1131 | else | |
1132 | hist_field_print(m, key_field); | |
7ef224d1 TZ |
1133 | } |
1134 | ||
1135 | seq_puts(m, ":vals="); | |
f2606835 TZ |
1136 | |
1137 | for_each_hist_val_field(i, hist_data) { | |
1138 | if (i == HITCOUNT_IDX) | |
1139 | seq_puts(m, "hitcount"); | |
1140 | else { | |
1141 | seq_puts(m, ","); | |
1142 | hist_field_print(m, hist_data->fields[i]); | |
1143 | } | |
1144 | } | |
7ef224d1 TZ |
1145 | |
1146 | seq_puts(m, ":sort="); | |
e62347d2 TZ |
1147 | |
1148 | for (i = 0; i < hist_data->n_sort_keys; i++) { | |
1149 | struct tracing_map_sort_key *sort_key; | |
1150 | ||
1151 | sort_key = &hist_data->sort_keys[i]; | |
1152 | ||
1153 | if (i > 0) | |
1154 | seq_puts(m, ","); | |
1155 | ||
1156 | if (sort_key->field_idx == HITCOUNT_IDX) | |
1157 | seq_puts(m, "hitcount"); | |
1158 | else { | |
1159 | unsigned int idx = sort_key->field_idx; | |
1160 | ||
1161 | if (WARN_ON(idx >= TRACING_MAP_FIELDS_MAX)) | |
1162 | return -EINVAL; | |
1163 | ||
1164 | hist_field_print(m, hist_data->fields[idx]); | |
1165 | } | |
1166 | ||
1167 | if (sort_key->descending) | |
1168 | seq_puts(m, ".descending"); | |
1169 | } | |
7ef224d1 TZ |
1170 | |
1171 | seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits)); | |
1172 | ||
1173 | if (data->filter_str) | |
1174 | seq_printf(m, " if %s", data->filter_str); | |
1175 | ||
83e99914 TZ |
1176 | if (data->paused) |
1177 | seq_puts(m, " [paused]"); | |
1178 | else | |
1179 | seq_puts(m, " [active]"); | |
7ef224d1 TZ |
1180 | |
1181 | seq_putc(m, '\n'); | |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | static void event_hist_trigger_free(struct event_trigger_ops *ops, | |
1187 | struct event_trigger_data *data) | |
1188 | { | |
1189 | struct hist_trigger_data *hist_data = data->private_data; | |
1190 | ||
1191 | if (WARN_ON_ONCE(data->ref <= 0)) | |
1192 | return; | |
1193 | ||
1194 | data->ref--; | |
1195 | if (!data->ref) { | |
1196 | trigger_data_free(data); | |
1197 | destroy_hist_data(hist_data); | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | static struct event_trigger_ops event_hist_trigger_ops = { | |
1202 | .func = event_hist_trigger, | |
1203 | .print = event_hist_trigger_print, | |
1204 | .init = event_trigger_init, | |
1205 | .free = event_hist_trigger_free, | |
1206 | }; | |
1207 | ||
1208 | static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd, | |
1209 | char *param) | |
1210 | { | |
1211 | return &event_hist_trigger_ops; | |
1212 | } | |
1213 | ||
e86ae9ba TZ |
1214 | static void hist_clear(struct event_trigger_data *data) |
1215 | { | |
1216 | struct hist_trigger_data *hist_data = data->private_data; | |
1217 | bool paused; | |
1218 | ||
1219 | paused = data->paused; | |
1220 | data->paused = true; | |
1221 | ||
1222 | synchronize_sched(); | |
1223 | ||
1224 | tracing_map_clear(hist_data->map); | |
1225 | ||
1226 | data->paused = paused; | |
1227 | } | |
1228 | ||
7ef224d1 TZ |
1229 | static int hist_register_trigger(char *glob, struct event_trigger_ops *ops, |
1230 | struct event_trigger_data *data, | |
1231 | struct trace_event_file *file) | |
1232 | { | |
83e99914 | 1233 | struct hist_trigger_data *hist_data = data->private_data; |
7ef224d1 TZ |
1234 | struct event_trigger_data *test; |
1235 | int ret = 0; | |
1236 | ||
1237 | list_for_each_entry_rcu(test, &file->triggers, list) { | |
1238 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { | |
83e99914 TZ |
1239 | if (hist_data->attrs->pause) |
1240 | test->paused = true; | |
1241 | else if (hist_data->attrs->cont) | |
1242 | test->paused = false; | |
e86ae9ba TZ |
1243 | else if (hist_data->attrs->clear) |
1244 | hist_clear(test); | |
83e99914 TZ |
1245 | else |
1246 | ret = -EEXIST; | |
7ef224d1 TZ |
1247 | goto out; |
1248 | } | |
1249 | } | |
1250 | ||
e86ae9ba | 1251 | if (hist_data->attrs->cont || hist_data->attrs->clear) { |
83e99914 TZ |
1252 | ret = -ENOENT; |
1253 | goto out; | |
1254 | } | |
1255 | ||
1256 | if (hist_data->attrs->pause) | |
1257 | data->paused = true; | |
1258 | ||
7ef224d1 TZ |
1259 | if (data->ops->init) { |
1260 | ret = data->ops->init(data->ops, data); | |
1261 | if (ret < 0) | |
1262 | goto out; | |
1263 | } | |
1264 | ||
1265 | list_add_rcu(&data->list, &file->triggers); | |
1266 | ret++; | |
1267 | ||
1268 | update_cond_flag(file); | |
1269 | if (trace_event_trigger_enable_disable(file, 1) < 0) { | |
1270 | list_del_rcu(&data->list); | |
1271 | update_cond_flag(file); | |
1272 | ret--; | |
1273 | } | |
1274 | out: | |
1275 | return ret; | |
1276 | } | |
1277 | ||
1278 | static int event_hist_trigger_func(struct event_command *cmd_ops, | |
1279 | struct trace_event_file *file, | |
1280 | char *glob, char *cmd, char *param) | |
1281 | { | |
1282 | unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT; | |
1283 | struct event_trigger_data *trigger_data; | |
1284 | struct hist_trigger_attrs *attrs; | |
1285 | struct event_trigger_ops *trigger_ops; | |
1286 | struct hist_trigger_data *hist_data; | |
1287 | char *trigger; | |
1288 | int ret = 0; | |
1289 | ||
1290 | if (!param) | |
1291 | return -EINVAL; | |
1292 | ||
1293 | /* separate the trigger from the filter (k:v [if filter]) */ | |
1294 | trigger = strsep(¶m, " \t"); | |
1295 | if (!trigger) | |
1296 | return -EINVAL; | |
1297 | ||
1298 | attrs = parse_hist_trigger_attrs(trigger); | |
1299 | if (IS_ERR(attrs)) | |
1300 | return PTR_ERR(attrs); | |
1301 | ||
1302 | if (attrs->map_bits) | |
1303 | hist_trigger_bits = attrs->map_bits; | |
1304 | ||
1305 | hist_data = create_hist_data(hist_trigger_bits, attrs, file); | |
1306 | if (IS_ERR(hist_data)) { | |
1307 | destroy_hist_trigger_attrs(attrs); | |
1308 | return PTR_ERR(hist_data); | |
1309 | } | |
1310 | ||
1311 | trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger); | |
1312 | ||
1313 | ret = -ENOMEM; | |
1314 | trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL); | |
1315 | if (!trigger_data) | |
1316 | goto out_free; | |
1317 | ||
1318 | trigger_data->count = -1; | |
1319 | trigger_data->ops = trigger_ops; | |
1320 | trigger_data->cmd_ops = cmd_ops; | |
1321 | ||
1322 | INIT_LIST_HEAD(&trigger_data->list); | |
1323 | RCU_INIT_POINTER(trigger_data->filter, NULL); | |
1324 | ||
1325 | trigger_data->private_data = hist_data; | |
1326 | ||
1327 | if (glob[0] == '!') { | |
1328 | cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); | |
1329 | ret = 0; | |
1330 | goto out_free; | |
1331 | } | |
1332 | ||
1333 | if (!param) /* if param is non-empty, it's supposed to be a filter */ | |
1334 | goto out_reg; | |
1335 | ||
1336 | if (!cmd_ops->set_filter) | |
1337 | goto out_reg; | |
1338 | ||
1339 | ret = cmd_ops->set_filter(param, trigger_data, file); | |
1340 | if (ret < 0) | |
1341 | goto out_free; | |
1342 | out_reg: | |
1343 | ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); | |
1344 | /* | |
1345 | * The above returns on success the # of triggers registered, | |
1346 | * but if it didn't register any it returns zero. Consider no | |
1347 | * triggers registered a failure too. | |
1348 | */ | |
1349 | if (!ret) { | |
e86ae9ba | 1350 | if (!(attrs->pause || attrs->cont || attrs->clear)) |
83e99914 | 1351 | ret = -ENOENT; |
7ef224d1 TZ |
1352 | goto out_free; |
1353 | } else if (ret < 0) | |
1354 | goto out_free; | |
1355 | /* Just return zero, not the number of registered triggers */ | |
1356 | ret = 0; | |
1357 | out: | |
1358 | return ret; | |
1359 | out_free: | |
1360 | if (cmd_ops->set_filter) | |
1361 | cmd_ops->set_filter(NULL, trigger_data, NULL); | |
1362 | ||
1363 | kfree(trigger_data); | |
1364 | ||
1365 | destroy_hist_data(hist_data); | |
1366 | goto out; | |
1367 | } | |
1368 | ||
1369 | static struct event_command trigger_hist_cmd = { | |
1370 | .name = "hist", | |
1371 | .trigger_type = ETT_EVENT_HIST, | |
1372 | .flags = EVENT_CMD_FL_NEEDS_REC, | |
1373 | .func = event_hist_trigger_func, | |
1374 | .reg = hist_register_trigger, | |
1375 | .unreg = unregister_trigger, | |
1376 | .get_trigger_ops = event_hist_get_trigger_ops, | |
1377 | .set_filter = set_trigger_filter, | |
1378 | }; | |
1379 | ||
1380 | __init int register_trigger_hist_cmd(void) | |
1381 | { | |
1382 | int ret; | |
1383 | ||
1384 | ret = register_event_command(&trigger_hist_cmd); | |
1385 | WARN_ON(ret < 0); | |
1386 | ||
1387 | return ret; | |
1388 | } |