Commit | Line | Data |
---|---|---|
fb52607a FW |
1 | /* |
2 | * | |
3 | * Function graph tracer. | |
9005f3eb | 4 | * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com> |
fb52607a FW |
5 | * Mostly borrowed from function tracer which |
6 | * is Copyright (c) Steven Rostedt <srostedt@redhat.com> | |
7 | * | |
8 | */ | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/uaccess.h> | |
11 | #include <linux/ftrace.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
fb52607a FW |
13 | #include <linux/fs.h> |
14 | ||
15 | #include "trace.h" | |
f0868d1e | 16 | #include "trace_output.h" |
fb52607a | 17 | |
be1eca39 | 18 | struct fgraph_cpu_data { |
2fbcdb35 SR |
19 | pid_t last_pid; |
20 | int depth; | |
be1eca39 | 21 | int ignore; |
f1c7f517 | 22 | unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH]; |
be1eca39 JO |
23 | }; |
24 | ||
25 | struct fgraph_data { | |
6016ee13 | 26 | struct fgraph_cpu_data __percpu *cpu_data; |
be1eca39 JO |
27 | |
28 | /* Place to preserve last processed entry. */ | |
29 | struct ftrace_graph_ent_entry ent; | |
30 | struct ftrace_graph_ret_entry ret; | |
31 | int failed; | |
32 | int cpu; | |
2fbcdb35 SR |
33 | }; |
34 | ||
287b6e68 | 35 | #define TRACE_GRAPH_INDENT 2 |
fb52607a | 36 | |
1a056155 | 37 | /* Flag options */ |
fb52607a | 38 | #define TRACE_GRAPH_PRINT_OVERRUN 0x1 |
1a056155 FW |
39 | #define TRACE_GRAPH_PRINT_CPU 0x2 |
40 | #define TRACE_GRAPH_PRINT_OVERHEAD 0x4 | |
11e84acc | 41 | #define TRACE_GRAPH_PRINT_PROC 0x8 |
9005f3eb | 42 | #define TRACE_GRAPH_PRINT_DURATION 0x10 |
9106b693 | 43 | #define TRACE_GRAPH_PRINT_ABS_TIME 0x20 |
1a056155 | 44 | |
fb52607a | 45 | static struct tracer_opt trace_opts[] = { |
9005f3eb | 46 | /* Display overruns? (for self-debug purpose) */ |
1a056155 FW |
47 | { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) }, |
48 | /* Display CPU ? */ | |
49 | { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) }, | |
50 | /* Display Overhead ? */ | |
51 | { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) }, | |
11e84acc FW |
52 | /* Display proc name/pid */ |
53 | { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) }, | |
9005f3eb FW |
54 | /* Display duration of execution */ |
55 | { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) }, | |
56 | /* Display absolute time of an entry */ | |
57 | { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) }, | |
fb52607a FW |
58 | { } /* Empty entry */ |
59 | }; | |
60 | ||
61 | static struct tracer_flags tracer_flags = { | |
11e84acc | 62 | /* Don't display overruns and proc by default */ |
9005f3eb FW |
63 | .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD | |
64 | TRACE_GRAPH_PRINT_DURATION, | |
fb52607a FW |
65 | .opts = trace_opts |
66 | }; | |
67 | ||
1a0799a8 | 68 | static struct trace_array *graph_array; |
9005f3eb | 69 | |
fb52607a | 70 | |
712406a6 SR |
71 | /* Add a function return address to the trace stack on thread info.*/ |
72 | int | |
71e308a2 SR |
73 | ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth, |
74 | unsigned long frame_pointer) | |
712406a6 | 75 | { |
5d1a03dc | 76 | unsigned long long calltime; |
712406a6 SR |
77 | int index; |
78 | ||
79 | if (!current->ret_stack) | |
80 | return -EBUSY; | |
81 | ||
82310a32 SR |
82 | /* |
83 | * We must make sure the ret_stack is tested before we read | |
84 | * anything else. | |
85 | */ | |
86 | smp_rmb(); | |
87 | ||
712406a6 SR |
88 | /* The return trace stack is full */ |
89 | if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) { | |
90 | atomic_inc(¤t->trace_overrun); | |
91 | return -EBUSY; | |
92 | } | |
93 | ||
5d1a03dc SR |
94 | calltime = trace_clock_local(); |
95 | ||
712406a6 SR |
96 | index = ++current->curr_ret_stack; |
97 | barrier(); | |
98 | current->ret_stack[index].ret = ret; | |
99 | current->ret_stack[index].func = func; | |
5d1a03dc | 100 | current->ret_stack[index].calltime = calltime; |
a2a16d6a | 101 | current->ret_stack[index].subtime = 0; |
71e308a2 | 102 | current->ret_stack[index].fp = frame_pointer; |
712406a6 SR |
103 | *depth = index; |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | /* Retrieve a function return address to the trace stack on thread info.*/ | |
a2a16d6a | 109 | static void |
71e308a2 SR |
110 | ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret, |
111 | unsigned long frame_pointer) | |
712406a6 SR |
112 | { |
113 | int index; | |
114 | ||
115 | index = current->curr_ret_stack; | |
116 | ||
117 | if (unlikely(index < 0)) { | |
118 | ftrace_graph_stop(); | |
119 | WARN_ON(1); | |
120 | /* Might as well panic, otherwise we have no where to go */ | |
121 | *ret = (unsigned long)panic; | |
122 | return; | |
123 | } | |
124 | ||
71e308a2 SR |
125 | #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST |
126 | /* | |
127 | * The arch may choose to record the frame pointer used | |
128 | * and check it here to make sure that it is what we expect it | |
129 | * to be. If gcc does not set the place holder of the return | |
130 | * address in the frame pointer, and does a copy instead, then | |
131 | * the function graph trace will fail. This test detects this | |
132 | * case. | |
133 | * | |
134 | * Currently, x86_32 with optimize for size (-Os) makes the latest | |
135 | * gcc do the above. | |
136 | */ | |
137 | if (unlikely(current->ret_stack[index].fp != frame_pointer)) { | |
138 | ftrace_graph_stop(); | |
139 | WARN(1, "Bad frame pointer: expected %lx, received %lx\n" | |
b375a11a | 140 | " from func %ps return to %lx\n", |
71e308a2 SR |
141 | current->ret_stack[index].fp, |
142 | frame_pointer, | |
143 | (void *)current->ret_stack[index].func, | |
144 | current->ret_stack[index].ret); | |
145 | *ret = (unsigned long)panic; | |
146 | return; | |
147 | } | |
148 | #endif | |
149 | ||
712406a6 SR |
150 | *ret = current->ret_stack[index].ret; |
151 | trace->func = current->ret_stack[index].func; | |
152 | trace->calltime = current->ret_stack[index].calltime; | |
153 | trace->overrun = atomic_read(¤t->trace_overrun); | |
154 | trace->depth = index; | |
712406a6 SR |
155 | } |
156 | ||
157 | /* | |
158 | * Send the trace to the ring-buffer. | |
159 | * @return the original return address. | |
160 | */ | |
71e308a2 | 161 | unsigned long ftrace_return_to_handler(unsigned long frame_pointer) |
712406a6 SR |
162 | { |
163 | struct ftrace_graph_ret trace; | |
164 | unsigned long ret; | |
165 | ||
71e308a2 | 166 | ftrace_pop_return_trace(&trace, &ret, frame_pointer); |
0012693a | 167 | trace.rettime = trace_clock_local(); |
712406a6 | 168 | ftrace_graph_return(&trace); |
a2a16d6a SR |
169 | barrier(); |
170 | current->curr_ret_stack--; | |
712406a6 SR |
171 | |
172 | if (unlikely(!ret)) { | |
173 | ftrace_graph_stop(); | |
174 | WARN_ON(1); | |
175 | /* Might as well panic. What else to do? */ | |
176 | ret = (unsigned long)panic; | |
177 | } | |
178 | ||
179 | return ret; | |
180 | } | |
181 | ||
62b915f1 | 182 | int __trace_graph_entry(struct trace_array *tr, |
1a0799a8 FW |
183 | struct ftrace_graph_ent *trace, |
184 | unsigned long flags, | |
185 | int pc) | |
186 | { | |
187 | struct ftrace_event_call *call = &event_funcgraph_entry; | |
188 | struct ring_buffer_event *event; | |
e77405ad | 189 | struct ring_buffer *buffer = tr->buffer; |
1a0799a8 FW |
190 | struct ftrace_graph_ent_entry *entry; |
191 | ||
dd17c8f7 | 192 | if (unlikely(__this_cpu_read(ftrace_cpu_disabled))) |
1a0799a8 FW |
193 | return 0; |
194 | ||
e77405ad | 195 | event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT, |
1a0799a8 FW |
196 | sizeof(*entry), flags, pc); |
197 | if (!event) | |
198 | return 0; | |
199 | entry = ring_buffer_event_data(event); | |
200 | entry->graph_ent = *trace; | |
e77405ad SR |
201 | if (!filter_current_check_discard(buffer, call, entry, event)) |
202 | ring_buffer_unlock_commit(buffer, event); | |
1a0799a8 FW |
203 | |
204 | return 1; | |
205 | } | |
206 | ||
207 | int trace_graph_entry(struct ftrace_graph_ent *trace) | |
208 | { | |
209 | struct trace_array *tr = graph_array; | |
210 | struct trace_array_cpu *data; | |
211 | unsigned long flags; | |
212 | long disabled; | |
213 | int ret; | |
214 | int cpu; | |
215 | int pc; | |
216 | ||
1a0799a8 FW |
217 | if (!ftrace_trace_task(current)) |
218 | return 0; | |
219 | ||
ea2c68a0 LJ |
220 | /* trace it when it is-nested-in or is a function enabled. */ |
221 | if (!(trace->depth || ftrace_graph_addr(trace->func))) | |
1a0799a8 FW |
222 | return 0; |
223 | ||
224 | local_irq_save(flags); | |
225 | cpu = raw_smp_processor_id(); | |
226 | data = tr->data[cpu]; | |
227 | disabled = atomic_inc_return(&data->disabled); | |
228 | if (likely(disabled == 1)) { | |
229 | pc = preempt_count(); | |
230 | ret = __trace_graph_entry(tr, trace, flags, pc); | |
231 | } else { | |
232 | ret = 0; | |
233 | } | |
1a0799a8 FW |
234 | |
235 | atomic_dec(&data->disabled); | |
236 | local_irq_restore(flags); | |
237 | ||
238 | return ret; | |
239 | } | |
240 | ||
0e950173 TB |
241 | int trace_graph_thresh_entry(struct ftrace_graph_ent *trace) |
242 | { | |
243 | if (tracing_thresh) | |
244 | return 1; | |
245 | else | |
246 | return trace_graph_entry(trace); | |
247 | } | |
248 | ||
62b915f1 | 249 | void __trace_graph_return(struct trace_array *tr, |
1a0799a8 FW |
250 | struct ftrace_graph_ret *trace, |
251 | unsigned long flags, | |
252 | int pc) | |
253 | { | |
254 | struct ftrace_event_call *call = &event_funcgraph_exit; | |
255 | struct ring_buffer_event *event; | |
e77405ad | 256 | struct ring_buffer *buffer = tr->buffer; |
1a0799a8 FW |
257 | struct ftrace_graph_ret_entry *entry; |
258 | ||
dd17c8f7 | 259 | if (unlikely(__this_cpu_read(ftrace_cpu_disabled))) |
1a0799a8 FW |
260 | return; |
261 | ||
e77405ad | 262 | event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET, |
1a0799a8 FW |
263 | sizeof(*entry), flags, pc); |
264 | if (!event) | |
265 | return; | |
266 | entry = ring_buffer_event_data(event); | |
267 | entry->ret = *trace; | |
e77405ad SR |
268 | if (!filter_current_check_discard(buffer, call, entry, event)) |
269 | ring_buffer_unlock_commit(buffer, event); | |
1a0799a8 FW |
270 | } |
271 | ||
272 | void trace_graph_return(struct ftrace_graph_ret *trace) | |
273 | { | |
274 | struct trace_array *tr = graph_array; | |
275 | struct trace_array_cpu *data; | |
276 | unsigned long flags; | |
277 | long disabled; | |
278 | int cpu; | |
279 | int pc; | |
280 | ||
281 | local_irq_save(flags); | |
282 | cpu = raw_smp_processor_id(); | |
283 | data = tr->data[cpu]; | |
284 | disabled = atomic_inc_return(&data->disabled); | |
285 | if (likely(disabled == 1)) { | |
286 | pc = preempt_count(); | |
287 | __trace_graph_return(tr, trace, flags, pc); | |
288 | } | |
1a0799a8 FW |
289 | atomic_dec(&data->disabled); |
290 | local_irq_restore(flags); | |
291 | } | |
292 | ||
24a53652 FW |
293 | void set_graph_array(struct trace_array *tr) |
294 | { | |
295 | graph_array = tr; | |
296 | ||
297 | /* Make graph_array visible before we start tracing */ | |
298 | ||
299 | smp_mb(); | |
300 | } | |
301 | ||
0e950173 TB |
302 | void trace_graph_thresh_return(struct ftrace_graph_ret *trace) |
303 | { | |
304 | if (tracing_thresh && | |
305 | (trace->rettime - trace->calltime < tracing_thresh)) | |
306 | return; | |
307 | else | |
308 | trace_graph_return(trace); | |
309 | } | |
310 | ||
fb52607a FW |
311 | static int graph_trace_init(struct trace_array *tr) |
312 | { | |
1a0799a8 FW |
313 | int ret; |
314 | ||
24a53652 | 315 | set_graph_array(tr); |
0e950173 TB |
316 | if (tracing_thresh) |
317 | ret = register_ftrace_graph(&trace_graph_thresh_return, | |
318 | &trace_graph_thresh_entry); | |
319 | else | |
320 | ret = register_ftrace_graph(&trace_graph_return, | |
321 | &trace_graph_entry); | |
660c7f9b SR |
322 | if (ret) |
323 | return ret; | |
324 | tracing_start_cmdline_record(); | |
325 | ||
326 | return 0; | |
fb52607a FW |
327 | } |
328 | ||
329 | static void graph_trace_reset(struct trace_array *tr) | |
330 | { | |
660c7f9b SR |
331 | tracing_stop_cmdline_record(); |
332 | unregister_ftrace_graph(); | |
fb52607a FW |
333 | } |
334 | ||
0c9e6f63 | 335 | static int max_bytes_for_cpu; |
1a056155 FW |
336 | |
337 | static enum print_line_t | |
338 | print_graph_cpu(struct trace_seq *s, int cpu) | |
339 | { | |
1a056155 | 340 | int ret; |
1a056155 | 341 | |
d51090b3 IM |
342 | /* |
343 | * Start with a space character - to make it stand out | |
344 | * to the right a bit when trace output is pasted into | |
345 | * email: | |
346 | */ | |
0c9e6f63 | 347 | ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu); |
1a056155 | 348 | if (!ret) |
d51090b3 IM |
349 | return TRACE_TYPE_PARTIAL_LINE; |
350 | ||
1a056155 FW |
351 | return TRACE_TYPE_HANDLED; |
352 | } | |
353 | ||
11e84acc FW |
354 | #define TRACE_GRAPH_PROCINFO_LENGTH 14 |
355 | ||
356 | static enum print_line_t | |
357 | print_graph_proc(struct trace_seq *s, pid_t pid) | |
358 | { | |
4ca53085 | 359 | char comm[TASK_COMM_LEN]; |
11e84acc FW |
360 | /* sign + log10(MAX_INT) + '\0' */ |
361 | char pid_str[11]; | |
4ca53085 SR |
362 | int spaces = 0; |
363 | int ret; | |
364 | int len; | |
365 | int i; | |
11e84acc | 366 | |
4ca53085 | 367 | trace_find_cmdline(pid, comm); |
11e84acc FW |
368 | comm[7] = '\0'; |
369 | sprintf(pid_str, "%d", pid); | |
370 | ||
371 | /* 1 stands for the "-" character */ | |
372 | len = strlen(comm) + strlen(pid_str) + 1; | |
373 | ||
374 | if (len < TRACE_GRAPH_PROCINFO_LENGTH) | |
375 | spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; | |
376 | ||
377 | /* First spaces to align center */ | |
378 | for (i = 0; i < spaces / 2; i++) { | |
379 | ret = trace_seq_printf(s, " "); | |
380 | if (!ret) | |
381 | return TRACE_TYPE_PARTIAL_LINE; | |
382 | } | |
383 | ||
384 | ret = trace_seq_printf(s, "%s-%s", comm, pid_str); | |
385 | if (!ret) | |
386 | return TRACE_TYPE_PARTIAL_LINE; | |
387 | ||
388 | /* Last spaces to align center */ | |
389 | for (i = 0; i < spaces - (spaces / 2); i++) { | |
390 | ret = trace_seq_printf(s, " "); | |
391 | if (!ret) | |
392 | return TRACE_TYPE_PARTIAL_LINE; | |
393 | } | |
394 | return TRACE_TYPE_HANDLED; | |
395 | } | |
396 | ||
1a056155 | 397 | |
49ff5903 SR |
398 | static enum print_line_t |
399 | print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry) | |
400 | { | |
f81c972d | 401 | if (!trace_seq_putc(s, ' ')) |
637e7e86 SR |
402 | return 0; |
403 | ||
f81c972d | 404 | return trace_print_lat_fmt(s, entry); |
49ff5903 SR |
405 | } |
406 | ||
287b6e68 | 407 | /* If the pid changed since the last trace, output this event */ |
11e84acc | 408 | static enum print_line_t |
2fbcdb35 | 409 | verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data) |
287b6e68 | 410 | { |
d51090b3 | 411 | pid_t prev_pid; |
9005f3eb | 412 | pid_t *last_pid; |
d51090b3 | 413 | int ret; |
660c7f9b | 414 | |
2fbcdb35 | 415 | if (!data) |
9005f3eb FW |
416 | return TRACE_TYPE_HANDLED; |
417 | ||
be1eca39 | 418 | last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid); |
9005f3eb FW |
419 | |
420 | if (*last_pid == pid) | |
11e84acc | 421 | return TRACE_TYPE_HANDLED; |
fb52607a | 422 | |
9005f3eb FW |
423 | prev_pid = *last_pid; |
424 | *last_pid = pid; | |
d51090b3 | 425 | |
9005f3eb FW |
426 | if (prev_pid == -1) |
427 | return TRACE_TYPE_HANDLED; | |
d51090b3 IM |
428 | /* |
429 | * Context-switch trace line: | |
430 | ||
431 | ------------------------------------------ | |
432 | | 1) migration/0--1 => sshd-1755 | |
433 | ------------------------------------------ | |
434 | ||
435 | */ | |
436 | ret = trace_seq_printf(s, | |
1fd8f2a3 | 437 | " ------------------------------------------\n"); |
11e84acc | 438 | if (!ret) |
810dc732 | 439 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
440 | |
441 | ret = print_graph_cpu(s, cpu); | |
442 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
810dc732 | 443 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
444 | |
445 | ret = print_graph_proc(s, prev_pid); | |
446 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
810dc732 | 447 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
448 | |
449 | ret = trace_seq_printf(s, " => "); | |
450 | if (!ret) | |
810dc732 | 451 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
452 | |
453 | ret = print_graph_proc(s, pid); | |
454 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
810dc732 | 455 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
456 | |
457 | ret = trace_seq_printf(s, | |
458 | "\n ------------------------------------------\n\n"); | |
459 | if (!ret) | |
810dc732 | 460 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc | 461 | |
810dc732 | 462 | return TRACE_TYPE_HANDLED; |
287b6e68 FW |
463 | } |
464 | ||
b91facc3 FW |
465 | static struct ftrace_graph_ret_entry * |
466 | get_return_for_leaf(struct trace_iterator *iter, | |
83a8df61 FW |
467 | struct ftrace_graph_ent_entry *curr) |
468 | { | |
be1eca39 JO |
469 | struct fgraph_data *data = iter->private; |
470 | struct ring_buffer_iter *ring_iter = NULL; | |
83a8df61 FW |
471 | struct ring_buffer_event *event; |
472 | struct ftrace_graph_ret_entry *next; | |
473 | ||
be1eca39 JO |
474 | /* |
475 | * If the previous output failed to write to the seq buffer, | |
476 | * then we just reuse the data from before. | |
477 | */ | |
478 | if (data && data->failed) { | |
479 | curr = &data->ent; | |
480 | next = &data->ret; | |
481 | } else { | |
83a8df61 | 482 | |
be1eca39 JO |
483 | ring_iter = iter->buffer_iter[iter->cpu]; |
484 | ||
485 | /* First peek to compare current entry and the next one */ | |
486 | if (ring_iter) | |
487 | event = ring_buffer_iter_peek(ring_iter, NULL); | |
488 | else { | |
489 | /* | |
490 | * We need to consume the current entry to see | |
491 | * the next one. | |
492 | */ | |
66a8cb95 SR |
493 | ring_buffer_consume(iter->tr->buffer, iter->cpu, |
494 | NULL, NULL); | |
be1eca39 | 495 | event = ring_buffer_peek(iter->tr->buffer, iter->cpu, |
66a8cb95 | 496 | NULL, NULL); |
be1eca39 | 497 | } |
83a8df61 | 498 | |
be1eca39 JO |
499 | if (!event) |
500 | return NULL; | |
501 | ||
502 | next = ring_buffer_event_data(event); | |
83a8df61 | 503 | |
be1eca39 JO |
504 | if (data) { |
505 | /* | |
506 | * Save current and next entries for later reference | |
507 | * if the output fails. | |
508 | */ | |
509 | data->ent = *curr; | |
575570f0 SL |
510 | /* |
511 | * If the next event is not a return type, then | |
512 | * we only care about what type it is. Otherwise we can | |
513 | * safely copy the entire event. | |
514 | */ | |
515 | if (next->ent.type == TRACE_GRAPH_RET) | |
516 | data->ret = *next; | |
517 | else | |
518 | data->ret.ent.type = next->ent.type; | |
be1eca39 JO |
519 | } |
520 | } | |
83a8df61 FW |
521 | |
522 | if (next->ent.type != TRACE_GRAPH_RET) | |
b91facc3 | 523 | return NULL; |
83a8df61 FW |
524 | |
525 | if (curr->ent.pid != next->ent.pid || | |
526 | curr->graph_ent.func != next->ret.func) | |
b91facc3 | 527 | return NULL; |
83a8df61 | 528 | |
b91facc3 FW |
529 | /* this is a leaf, now advance the iterator */ |
530 | if (ring_iter) | |
531 | ring_buffer_read(ring_iter, NULL); | |
532 | ||
533 | return next; | |
83a8df61 FW |
534 | } |
535 | ||
9005f3eb FW |
536 | /* Signal a overhead of time execution to the output */ |
537 | static int | |
d7a8d9e9 JO |
538 | print_graph_overhead(unsigned long long duration, struct trace_seq *s, |
539 | u32 flags) | |
9005f3eb FW |
540 | { |
541 | /* If duration disappear, we don't need anything */ | |
d7a8d9e9 | 542 | if (!(flags & TRACE_GRAPH_PRINT_DURATION)) |
9005f3eb FW |
543 | return 1; |
544 | ||
545 | /* Non nested entry or return */ | |
546 | if (duration == -1) | |
547 | return trace_seq_printf(s, " "); | |
548 | ||
d7a8d9e9 | 549 | if (flags & TRACE_GRAPH_PRINT_OVERHEAD) { |
9005f3eb FW |
550 | /* Duration exceeded 100 msecs */ |
551 | if (duration > 100000ULL) | |
552 | return trace_seq_printf(s, "! "); | |
553 | ||
554 | /* Duration exceeded 10 msecs */ | |
555 | if (duration > 10000ULL) | |
556 | return trace_seq_printf(s, "+ "); | |
557 | } | |
558 | ||
559 | return trace_seq_printf(s, " "); | |
560 | } | |
561 | ||
d1f9cbd7 FW |
562 | static int print_graph_abs_time(u64 t, struct trace_seq *s) |
563 | { | |
564 | unsigned long usecs_rem; | |
565 | ||
566 | usecs_rem = do_div(t, NSEC_PER_SEC); | |
567 | usecs_rem /= 1000; | |
568 | ||
569 | return trace_seq_printf(s, "%5lu.%06lu | ", | |
570 | (unsigned long)t, usecs_rem); | |
571 | } | |
572 | ||
f8b755ac | 573 | static enum print_line_t |
d1f9cbd7 | 574 | print_graph_irq(struct trace_iterator *iter, unsigned long addr, |
d7a8d9e9 | 575 | enum trace_type type, int cpu, pid_t pid, u32 flags) |
f8b755ac FW |
576 | { |
577 | int ret; | |
d1f9cbd7 | 578 | struct trace_seq *s = &iter->seq; |
f8b755ac FW |
579 | |
580 | if (addr < (unsigned long)__irqentry_text_start || | |
581 | addr >= (unsigned long)__irqentry_text_end) | |
582 | return TRACE_TYPE_UNHANDLED; | |
583 | ||
d1f9cbd7 | 584 | /* Absolute time */ |
d7a8d9e9 | 585 | if (flags & TRACE_GRAPH_PRINT_ABS_TIME) { |
d1f9cbd7 FW |
586 | ret = print_graph_abs_time(iter->ts, s); |
587 | if (!ret) | |
588 | return TRACE_TYPE_PARTIAL_LINE; | |
589 | } | |
590 | ||
9005f3eb | 591 | /* Cpu */ |
d7a8d9e9 | 592 | if (flags & TRACE_GRAPH_PRINT_CPU) { |
9005f3eb FW |
593 | ret = print_graph_cpu(s, cpu); |
594 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
595 | return TRACE_TYPE_PARTIAL_LINE; | |
596 | } | |
49ff5903 | 597 | |
9005f3eb | 598 | /* Proc */ |
d7a8d9e9 | 599 | if (flags & TRACE_GRAPH_PRINT_PROC) { |
9005f3eb FW |
600 | ret = print_graph_proc(s, pid); |
601 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
602 | return TRACE_TYPE_PARTIAL_LINE; | |
603 | ret = trace_seq_printf(s, " | "); | |
604 | if (!ret) | |
605 | return TRACE_TYPE_PARTIAL_LINE; | |
606 | } | |
f8b755ac | 607 | |
9005f3eb | 608 | /* No overhead */ |
d7a8d9e9 | 609 | ret = print_graph_overhead(-1, s, flags); |
9005f3eb FW |
610 | if (!ret) |
611 | return TRACE_TYPE_PARTIAL_LINE; | |
f8b755ac | 612 | |
9005f3eb FW |
613 | if (type == TRACE_GRAPH_ENT) |
614 | ret = trace_seq_printf(s, "==========>"); | |
615 | else | |
616 | ret = trace_seq_printf(s, "<=========="); | |
617 | ||
618 | if (!ret) | |
619 | return TRACE_TYPE_PARTIAL_LINE; | |
620 | ||
621 | /* Don't close the duration column if haven't one */ | |
d7a8d9e9 | 622 | if (flags & TRACE_GRAPH_PRINT_DURATION) |
9005f3eb FW |
623 | trace_seq_printf(s, " |"); |
624 | ret = trace_seq_printf(s, "\n"); | |
f8b755ac | 625 | |
f8b755ac FW |
626 | if (!ret) |
627 | return TRACE_TYPE_PARTIAL_LINE; | |
628 | return TRACE_TYPE_HANDLED; | |
629 | } | |
83a8df61 | 630 | |
0706f1c4 SR |
631 | enum print_line_t |
632 | trace_print_graph_duration(unsigned long long duration, struct trace_seq *s) | |
83a8df61 FW |
633 | { |
634 | unsigned long nsecs_rem = do_div(duration, 1000); | |
166d3c79 FW |
635 | /* log10(ULONG_MAX) + '\0' */ |
636 | char msecs_str[21]; | |
637 | char nsecs_str[5]; | |
638 | int ret, len; | |
639 | int i; | |
640 | ||
641 | sprintf(msecs_str, "%lu", (unsigned long) duration); | |
642 | ||
643 | /* Print msecs */ | |
9005f3eb | 644 | ret = trace_seq_printf(s, "%s", msecs_str); |
166d3c79 FW |
645 | if (!ret) |
646 | return TRACE_TYPE_PARTIAL_LINE; | |
647 | ||
648 | len = strlen(msecs_str); | |
649 | ||
650 | /* Print nsecs (we don't want to exceed 7 numbers) */ | |
651 | if (len < 7) { | |
d62f85d1 CD |
652 | snprintf(nsecs_str, min(sizeof(nsecs_str), 8UL - len), "%03lu", |
653 | nsecs_rem); | |
166d3c79 FW |
654 | ret = trace_seq_printf(s, ".%s", nsecs_str); |
655 | if (!ret) | |
656 | return TRACE_TYPE_PARTIAL_LINE; | |
657 | len += strlen(nsecs_str); | |
658 | } | |
659 | ||
660 | ret = trace_seq_printf(s, " us "); | |
661 | if (!ret) | |
662 | return TRACE_TYPE_PARTIAL_LINE; | |
663 | ||
664 | /* Print remaining spaces to fit the row's width */ | |
665 | for (i = len; i < 7; i++) { | |
666 | ret = trace_seq_printf(s, " "); | |
667 | if (!ret) | |
668 | return TRACE_TYPE_PARTIAL_LINE; | |
669 | } | |
0706f1c4 SR |
670 | return TRACE_TYPE_HANDLED; |
671 | } | |
672 | ||
673 | static enum print_line_t | |
674 | print_graph_duration(unsigned long long duration, struct trace_seq *s) | |
675 | { | |
676 | int ret; | |
677 | ||
678 | ret = trace_print_graph_duration(duration, s); | |
679 | if (ret != TRACE_TYPE_HANDLED) | |
680 | return ret; | |
166d3c79 FW |
681 | |
682 | ret = trace_seq_printf(s, "| "); | |
683 | if (!ret) | |
684 | return TRACE_TYPE_PARTIAL_LINE; | |
166d3c79 | 685 | |
0706f1c4 | 686 | return TRACE_TYPE_HANDLED; |
83a8df61 FW |
687 | } |
688 | ||
83a8df61 | 689 | /* Case of a leaf function on its call entry */ |
287b6e68 | 690 | static enum print_line_t |
83a8df61 | 691 | print_graph_entry_leaf(struct trace_iterator *iter, |
b91facc3 | 692 | struct ftrace_graph_ent_entry *entry, |
d7a8d9e9 JO |
693 | struct ftrace_graph_ret_entry *ret_entry, |
694 | struct trace_seq *s, u32 flags) | |
fb52607a | 695 | { |
2fbcdb35 | 696 | struct fgraph_data *data = iter->private; |
83a8df61 | 697 | struct ftrace_graph_ret *graph_ret; |
83a8df61 FW |
698 | struct ftrace_graph_ent *call; |
699 | unsigned long long duration; | |
fb52607a | 700 | int ret; |
1a056155 | 701 | int i; |
fb52607a | 702 | |
83a8df61 FW |
703 | graph_ret = &ret_entry->ret; |
704 | call = &entry->graph_ent; | |
705 | duration = graph_ret->rettime - graph_ret->calltime; | |
706 | ||
2fbcdb35 | 707 | if (data) { |
f1c7f517 | 708 | struct fgraph_cpu_data *cpu_data; |
2fbcdb35 | 709 | int cpu = iter->cpu; |
f1c7f517 SR |
710 | |
711 | cpu_data = per_cpu_ptr(data->cpu_data, cpu); | |
2fbcdb35 SR |
712 | |
713 | /* | |
714 | * Comments display at + 1 to depth. Since | |
715 | * this is a leaf function, keep the comments | |
716 | * equal to this depth. | |
717 | */ | |
f1c7f517 SR |
718 | cpu_data->depth = call->depth - 1; |
719 | ||
720 | /* No need to keep this function around for this depth */ | |
721 | if (call->depth < FTRACE_RETFUNC_DEPTH) | |
722 | cpu_data->enter_funcs[call->depth] = 0; | |
2fbcdb35 SR |
723 | } |
724 | ||
83a8df61 | 725 | /* Overhead */ |
d7a8d9e9 | 726 | ret = print_graph_overhead(duration, s, flags); |
9005f3eb FW |
727 | if (!ret) |
728 | return TRACE_TYPE_PARTIAL_LINE; | |
1a056155 FW |
729 | |
730 | /* Duration */ | |
d7a8d9e9 | 731 | if (flags & TRACE_GRAPH_PRINT_DURATION) { |
9005f3eb FW |
732 | ret = print_graph_duration(duration, s); |
733 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
734 | return TRACE_TYPE_PARTIAL_LINE; | |
735 | } | |
437f24fb | 736 | |
83a8df61 FW |
737 | /* Function */ |
738 | for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) { | |
739 | ret = trace_seq_printf(s, " "); | |
740 | if (!ret) | |
741 | return TRACE_TYPE_PARTIAL_LINE; | |
742 | } | |
743 | ||
b375a11a | 744 | ret = trace_seq_printf(s, "%ps();\n", (void *)call->func); |
83a8df61 FW |
745 | if (!ret) |
746 | return TRACE_TYPE_PARTIAL_LINE; | |
747 | ||
748 | return TRACE_TYPE_HANDLED; | |
749 | } | |
750 | ||
751 | static enum print_line_t | |
2fbcdb35 SR |
752 | print_graph_entry_nested(struct trace_iterator *iter, |
753 | struct ftrace_graph_ent_entry *entry, | |
d7a8d9e9 | 754 | struct trace_seq *s, int cpu, u32 flags) |
83a8df61 | 755 | { |
83a8df61 | 756 | struct ftrace_graph_ent *call = &entry->graph_ent; |
2fbcdb35 SR |
757 | struct fgraph_data *data = iter->private; |
758 | int ret; | |
759 | int i; | |
760 | ||
761 | if (data) { | |
f1c7f517 | 762 | struct fgraph_cpu_data *cpu_data; |
2fbcdb35 | 763 | int cpu = iter->cpu; |
2fbcdb35 | 764 | |
f1c7f517 SR |
765 | cpu_data = per_cpu_ptr(data->cpu_data, cpu); |
766 | cpu_data->depth = call->depth; | |
767 | ||
768 | /* Save this function pointer to see if the exit matches */ | |
769 | if (call->depth < FTRACE_RETFUNC_DEPTH) | |
770 | cpu_data->enter_funcs[call->depth] = call->func; | |
2fbcdb35 | 771 | } |
83a8df61 FW |
772 | |
773 | /* No overhead */ | |
d7a8d9e9 | 774 | ret = print_graph_overhead(-1, s, flags); |
9005f3eb FW |
775 | if (!ret) |
776 | return TRACE_TYPE_PARTIAL_LINE; | |
1a056155 | 777 | |
9005f3eb | 778 | /* No time */ |
d7a8d9e9 | 779 | if (flags & TRACE_GRAPH_PRINT_DURATION) { |
f8b755ac FW |
780 | ret = trace_seq_printf(s, " | "); |
781 | if (!ret) | |
782 | return TRACE_TYPE_PARTIAL_LINE; | |
f8b755ac FW |
783 | } |
784 | ||
83a8df61 | 785 | /* Function */ |
287b6e68 FW |
786 | for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) { |
787 | ret = trace_seq_printf(s, " "); | |
fb52607a FW |
788 | if (!ret) |
789 | return TRACE_TYPE_PARTIAL_LINE; | |
287b6e68 FW |
790 | } |
791 | ||
b375a11a | 792 | ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func); |
83a8df61 FW |
793 | if (!ret) |
794 | return TRACE_TYPE_PARTIAL_LINE; | |
795 | ||
b91facc3 FW |
796 | /* |
797 | * we already consumed the current entry to check the next one | |
798 | * and see if this is a leaf. | |
799 | */ | |
800 | return TRACE_TYPE_NO_CONSUME; | |
287b6e68 FW |
801 | } |
802 | ||
83a8df61 | 803 | static enum print_line_t |
ac5f6c96 | 804 | print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s, |
d7a8d9e9 | 805 | int type, unsigned long addr, u32 flags) |
83a8df61 | 806 | { |
2fbcdb35 | 807 | struct fgraph_data *data = iter->private; |
83a8df61 | 808 | struct trace_entry *ent = iter->ent; |
ac5f6c96 SR |
809 | int cpu = iter->cpu; |
810 | int ret; | |
83a8df61 | 811 | |
1a056155 | 812 | /* Pid */ |
2fbcdb35 | 813 | if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE) |
9005f3eb FW |
814 | return TRACE_TYPE_PARTIAL_LINE; |
815 | ||
ac5f6c96 SR |
816 | if (type) { |
817 | /* Interrupt */ | |
d7a8d9e9 | 818 | ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags); |
ac5f6c96 SR |
819 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
820 | return TRACE_TYPE_PARTIAL_LINE; | |
821 | } | |
83a8df61 | 822 | |
9005f3eb | 823 | /* Absolute time */ |
d7a8d9e9 | 824 | if (flags & TRACE_GRAPH_PRINT_ABS_TIME) { |
9005f3eb FW |
825 | ret = print_graph_abs_time(iter->ts, s); |
826 | if (!ret) | |
827 | return TRACE_TYPE_PARTIAL_LINE; | |
828 | } | |
829 | ||
1a056155 | 830 | /* Cpu */ |
d7a8d9e9 | 831 | if (flags & TRACE_GRAPH_PRINT_CPU) { |
1a056155 | 832 | ret = print_graph_cpu(s, cpu); |
11e84acc FW |
833 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
834 | return TRACE_TYPE_PARTIAL_LINE; | |
835 | } | |
836 | ||
837 | /* Proc */ | |
d7a8d9e9 | 838 | if (flags & TRACE_GRAPH_PRINT_PROC) { |
00a8bf85 | 839 | ret = print_graph_proc(s, ent->pid); |
11e84acc FW |
840 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
841 | return TRACE_TYPE_PARTIAL_LINE; | |
842 | ||
843 | ret = trace_seq_printf(s, " | "); | |
1a056155 FW |
844 | if (!ret) |
845 | return TRACE_TYPE_PARTIAL_LINE; | |
846 | } | |
83a8df61 | 847 | |
49ff5903 SR |
848 | /* Latency format */ |
849 | if (trace_flags & TRACE_ITER_LATENCY_FMT) { | |
850 | ret = print_graph_lat_fmt(s, ent); | |
851 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
852 | return TRACE_TYPE_PARTIAL_LINE; | |
853 | } | |
854 | ||
ac5f6c96 SR |
855 | return 0; |
856 | } | |
857 | ||
858 | static enum print_line_t | |
859 | print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s, | |
d7a8d9e9 | 860 | struct trace_iterator *iter, u32 flags) |
ac5f6c96 | 861 | { |
be1eca39 | 862 | struct fgraph_data *data = iter->private; |
ac5f6c96 SR |
863 | struct ftrace_graph_ent *call = &field->graph_ent; |
864 | struct ftrace_graph_ret_entry *leaf_ret; | |
be1eca39 JO |
865 | static enum print_line_t ret; |
866 | int cpu = iter->cpu; | |
ac5f6c96 | 867 | |
d7a8d9e9 | 868 | if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags)) |
ac5f6c96 SR |
869 | return TRACE_TYPE_PARTIAL_LINE; |
870 | ||
b91facc3 FW |
871 | leaf_ret = get_return_for_leaf(iter, field); |
872 | if (leaf_ret) | |
d7a8d9e9 | 873 | ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags); |
83a8df61 | 874 | else |
d7a8d9e9 | 875 | ret = print_graph_entry_nested(iter, field, s, cpu, flags); |
83a8df61 | 876 | |
be1eca39 JO |
877 | if (data) { |
878 | /* | |
879 | * If we failed to write our output, then we need to make | |
880 | * note of it. Because we already consumed our entry. | |
881 | */ | |
882 | if (s->full) { | |
883 | data->failed = 1; | |
884 | data->cpu = cpu; | |
885 | } else | |
886 | data->failed = 0; | |
887 | } | |
888 | ||
889 | return ret; | |
83a8df61 FW |
890 | } |
891 | ||
287b6e68 FW |
892 | static enum print_line_t |
893 | print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s, | |
d7a8d9e9 JO |
894 | struct trace_entry *ent, struct trace_iterator *iter, |
895 | u32 flags) | |
287b6e68 | 896 | { |
83a8df61 | 897 | unsigned long long duration = trace->rettime - trace->calltime; |
2fbcdb35 SR |
898 | struct fgraph_data *data = iter->private; |
899 | pid_t pid = ent->pid; | |
900 | int cpu = iter->cpu; | |
f1c7f517 | 901 | int func_match = 1; |
2fbcdb35 SR |
902 | int ret; |
903 | int i; | |
904 | ||
905 | if (data) { | |
f1c7f517 SR |
906 | struct fgraph_cpu_data *cpu_data; |
907 | int cpu = iter->cpu; | |
908 | ||
909 | cpu_data = per_cpu_ptr(data->cpu_data, cpu); | |
2fbcdb35 SR |
910 | |
911 | /* | |
912 | * Comments display at + 1 to depth. This is the | |
913 | * return from a function, we now want the comments | |
914 | * to display at the same level of the bracket. | |
915 | */ | |
f1c7f517 SR |
916 | cpu_data->depth = trace->depth - 1; |
917 | ||
918 | if (trace->depth < FTRACE_RETFUNC_DEPTH) { | |
919 | if (cpu_data->enter_funcs[trace->depth] != trace->func) | |
920 | func_match = 0; | |
921 | cpu_data->enter_funcs[trace->depth] = 0; | |
922 | } | |
2fbcdb35 | 923 | } |
287b6e68 | 924 | |
d7a8d9e9 | 925 | if (print_graph_prologue(iter, s, 0, 0, flags)) |
437f24fb SR |
926 | return TRACE_TYPE_PARTIAL_LINE; |
927 | ||
83a8df61 | 928 | /* Overhead */ |
d7a8d9e9 | 929 | ret = print_graph_overhead(duration, s, flags); |
9005f3eb FW |
930 | if (!ret) |
931 | return TRACE_TYPE_PARTIAL_LINE; | |
1a056155 FW |
932 | |
933 | /* Duration */ | |
d7a8d9e9 | 934 | if (flags & TRACE_GRAPH_PRINT_DURATION) { |
9005f3eb FW |
935 | ret = print_graph_duration(duration, s); |
936 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
937 | return TRACE_TYPE_PARTIAL_LINE; | |
938 | } | |
83a8df61 FW |
939 | |
940 | /* Closing brace */ | |
287b6e68 FW |
941 | for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) { |
942 | ret = trace_seq_printf(s, " "); | |
fb52607a FW |
943 | if (!ret) |
944 | return TRACE_TYPE_PARTIAL_LINE; | |
287b6e68 FW |
945 | } |
946 | ||
f1c7f517 SR |
947 | /* |
948 | * If the return function does not have a matching entry, | |
949 | * then the entry was lost. Instead of just printing | |
950 | * the '}' and letting the user guess what function this | |
951 | * belongs to, write out the function name. | |
952 | */ | |
953 | if (func_match) { | |
954 | ret = trace_seq_printf(s, "}\n"); | |
955 | if (!ret) | |
956 | return TRACE_TYPE_PARTIAL_LINE; | |
957 | } else { | |
a094fe04 | 958 | ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func); |
f1c7f517 SR |
959 | if (!ret) |
960 | return TRACE_TYPE_PARTIAL_LINE; | |
961 | } | |
fb52607a | 962 | |
83a8df61 | 963 | /* Overrun */ |
d7a8d9e9 | 964 | if (flags & TRACE_GRAPH_PRINT_OVERRUN) { |
287b6e68 FW |
965 | ret = trace_seq_printf(s, " (Overruns: %lu)\n", |
966 | trace->overrun); | |
fb52607a FW |
967 | if (!ret) |
968 | return TRACE_TYPE_PARTIAL_LINE; | |
287b6e68 | 969 | } |
f8b755ac | 970 | |
d7a8d9e9 JO |
971 | ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, |
972 | cpu, pid, flags); | |
f8b755ac FW |
973 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
974 | return TRACE_TYPE_PARTIAL_LINE; | |
975 | ||
287b6e68 FW |
976 | return TRACE_TYPE_HANDLED; |
977 | } | |
978 | ||
1fd8f2a3 | 979 | static enum print_line_t |
d7a8d9e9 JO |
980 | print_graph_comment(struct trace_seq *s, struct trace_entry *ent, |
981 | struct trace_iterator *iter, u32 flags) | |
1fd8f2a3 | 982 | { |
5087f8d2 | 983 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
2fbcdb35 | 984 | struct fgraph_data *data = iter->private; |
5087f8d2 | 985 | struct trace_event *event; |
2fbcdb35 | 986 | int depth = 0; |
1fd8f2a3 | 987 | int ret; |
2fbcdb35 SR |
988 | int i; |
989 | ||
990 | if (data) | |
be1eca39 | 991 | depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth; |
9005f3eb | 992 | |
d7a8d9e9 | 993 | if (print_graph_prologue(iter, s, 0, 0, flags)) |
d1f9cbd7 FW |
994 | return TRACE_TYPE_PARTIAL_LINE; |
995 | ||
1fd8f2a3 | 996 | /* No overhead */ |
d7a8d9e9 | 997 | ret = print_graph_overhead(-1, s, flags); |
9005f3eb FW |
998 | if (!ret) |
999 | return TRACE_TYPE_PARTIAL_LINE; | |
1000 | ||
1001 | /* No time */ | |
d7a8d9e9 | 1002 | if (flags & TRACE_GRAPH_PRINT_DURATION) { |
9005f3eb | 1003 | ret = trace_seq_printf(s, " | "); |
1fd8f2a3 FW |
1004 | if (!ret) |
1005 | return TRACE_TYPE_PARTIAL_LINE; | |
1006 | } | |
1007 | ||
1fd8f2a3 | 1008 | /* Indentation */ |
2fbcdb35 SR |
1009 | if (depth > 0) |
1010 | for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) { | |
1fd8f2a3 FW |
1011 | ret = trace_seq_printf(s, " "); |
1012 | if (!ret) | |
1013 | return TRACE_TYPE_PARTIAL_LINE; | |
1014 | } | |
1015 | ||
1016 | /* The comment */ | |
769b0441 FW |
1017 | ret = trace_seq_printf(s, "/* "); |
1018 | if (!ret) | |
1019 | return TRACE_TYPE_PARTIAL_LINE; | |
1020 | ||
5087f8d2 SR |
1021 | switch (iter->ent->type) { |
1022 | case TRACE_BPRINT: | |
1023 | ret = trace_print_bprintk_msg_only(iter); | |
1024 | if (ret != TRACE_TYPE_HANDLED) | |
1025 | return ret; | |
1026 | break; | |
1027 | case TRACE_PRINT: | |
1028 | ret = trace_print_printk_msg_only(iter); | |
1029 | if (ret != TRACE_TYPE_HANDLED) | |
1030 | return ret; | |
1031 | break; | |
1032 | default: | |
1033 | event = ftrace_find_event(ent->type); | |
1034 | if (!event) | |
1035 | return TRACE_TYPE_UNHANDLED; | |
1036 | ||
a9a57763 | 1037 | ret = event->funcs->trace(iter, sym_flags, event); |
5087f8d2 SR |
1038 | if (ret != TRACE_TYPE_HANDLED) |
1039 | return ret; | |
1040 | } | |
1fd8f2a3 | 1041 | |
412d0bb5 FW |
1042 | /* Strip ending newline */ |
1043 | if (s->buffer[s->len - 1] == '\n') { | |
1044 | s->buffer[s->len - 1] = '\0'; | |
1045 | s->len--; | |
1046 | } | |
1047 | ||
1fd8f2a3 FW |
1048 | ret = trace_seq_printf(s, " */\n"); |
1049 | if (!ret) | |
1050 | return TRACE_TYPE_PARTIAL_LINE; | |
1051 | ||
1052 | return TRACE_TYPE_HANDLED; | |
1053 | } | |
1054 | ||
1055 | ||
287b6e68 | 1056 | enum print_line_t |
d7a8d9e9 | 1057 | print_graph_function_flags(struct trace_iterator *iter, u32 flags) |
287b6e68 | 1058 | { |
be1eca39 JO |
1059 | struct ftrace_graph_ent_entry *field; |
1060 | struct fgraph_data *data = iter->private; | |
287b6e68 | 1061 | struct trace_entry *entry = iter->ent; |
5087f8d2 | 1062 | struct trace_seq *s = &iter->seq; |
be1eca39 JO |
1063 | int cpu = iter->cpu; |
1064 | int ret; | |
1065 | ||
1066 | if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) { | |
1067 | per_cpu_ptr(data->cpu_data, cpu)->ignore = 0; | |
1068 | return TRACE_TYPE_HANDLED; | |
1069 | } | |
1070 | ||
1071 | /* | |
1072 | * If the last output failed, there's a possibility we need | |
1073 | * to print out the missing entry which would never go out. | |
1074 | */ | |
1075 | if (data && data->failed) { | |
1076 | field = &data->ent; | |
1077 | iter->cpu = data->cpu; | |
d7a8d9e9 | 1078 | ret = print_graph_entry(field, s, iter, flags); |
be1eca39 JO |
1079 | if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) { |
1080 | per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1; | |
1081 | ret = TRACE_TYPE_NO_CONSUME; | |
1082 | } | |
1083 | iter->cpu = cpu; | |
1084 | return ret; | |
1085 | } | |
fb52607a | 1086 | |
287b6e68 FW |
1087 | switch (entry->type) { |
1088 | case TRACE_GRAPH_ENT: { | |
38ceb592 LJ |
1089 | /* |
1090 | * print_graph_entry() may consume the current event, | |
1091 | * thus @field may become invalid, so we need to save it. | |
1092 | * sizeof(struct ftrace_graph_ent_entry) is very small, | |
1093 | * it can be safely saved at the stack. | |
1094 | */ | |
be1eca39 | 1095 | struct ftrace_graph_ent_entry saved; |
287b6e68 | 1096 | trace_assign_type(field, entry); |
38ceb592 | 1097 | saved = *field; |
d7a8d9e9 | 1098 | return print_graph_entry(&saved, s, iter, flags); |
287b6e68 FW |
1099 | } |
1100 | case TRACE_GRAPH_RET: { | |
1101 | struct ftrace_graph_ret_entry *field; | |
1102 | trace_assign_type(field, entry); | |
d7a8d9e9 | 1103 | return print_graph_return(&field->ret, s, entry, iter, flags); |
287b6e68 | 1104 | } |
62b915f1 JO |
1105 | case TRACE_STACK: |
1106 | case TRACE_FN: | |
1107 | /* dont trace stack and functions as comments */ | |
1108 | return TRACE_TYPE_UNHANDLED; | |
1109 | ||
287b6e68 | 1110 | default: |
d7a8d9e9 | 1111 | return print_graph_comment(s, entry, iter, flags); |
fb52607a | 1112 | } |
5087f8d2 SR |
1113 | |
1114 | return TRACE_TYPE_HANDLED; | |
fb52607a FW |
1115 | } |
1116 | ||
d7a8d9e9 JO |
1117 | static enum print_line_t |
1118 | print_graph_function(struct trace_iterator *iter) | |
1119 | { | |
1120 | return print_graph_function_flags(iter, tracer_flags.val); | |
1121 | } | |
1122 | ||
9106b693 | 1123 | static enum print_line_t |
a9a57763 SR |
1124 | print_graph_function_event(struct trace_iterator *iter, int flags, |
1125 | struct trace_event *event) | |
9106b693 JO |
1126 | { |
1127 | return print_graph_function(iter); | |
1128 | } | |
1129 | ||
d7a8d9e9 | 1130 | static void print_lat_header(struct seq_file *s, u32 flags) |
49ff5903 SR |
1131 | { |
1132 | static const char spaces[] = " " /* 16 spaces */ | |
1133 | " " /* 4 spaces */ | |
1134 | " "; /* 17 spaces */ | |
1135 | int size = 0; | |
1136 | ||
d7a8d9e9 | 1137 | if (flags & TRACE_GRAPH_PRINT_ABS_TIME) |
49ff5903 | 1138 | size += 16; |
d7a8d9e9 | 1139 | if (flags & TRACE_GRAPH_PRINT_CPU) |
49ff5903 | 1140 | size += 4; |
d7a8d9e9 | 1141 | if (flags & TRACE_GRAPH_PRINT_PROC) |
49ff5903 SR |
1142 | size += 17; |
1143 | ||
1144 | seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces); | |
1145 | seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces); | |
1146 | seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces); | |
1147 | seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces); | |
637e7e86 SR |
1148 | seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces); |
1149 | seq_printf(s, "#%.*s|||| / \n", size, spaces); | |
49ff5903 SR |
1150 | } |
1151 | ||
d7a8d9e9 | 1152 | void print_graph_headers_flags(struct seq_file *s, u32 flags) |
decbec38 | 1153 | { |
49ff5903 SR |
1154 | int lat = trace_flags & TRACE_ITER_LATENCY_FMT; |
1155 | ||
1156 | if (lat) | |
d7a8d9e9 | 1157 | print_lat_header(s, flags); |
49ff5903 | 1158 | |
decbec38 | 1159 | /* 1st line */ |
49ff5903 | 1160 | seq_printf(s, "#"); |
d7a8d9e9 | 1161 | if (flags & TRACE_GRAPH_PRINT_ABS_TIME) |
9005f3eb | 1162 | seq_printf(s, " TIME "); |
d7a8d9e9 | 1163 | if (flags & TRACE_GRAPH_PRINT_CPU) |
49ff5903 | 1164 | seq_printf(s, " CPU"); |
d7a8d9e9 | 1165 | if (flags & TRACE_GRAPH_PRINT_PROC) |
49ff5903 SR |
1166 | seq_printf(s, " TASK/PID "); |
1167 | if (lat) | |
637e7e86 | 1168 | seq_printf(s, "|||||"); |
d7a8d9e9 | 1169 | if (flags & TRACE_GRAPH_PRINT_DURATION) |
9005f3eb FW |
1170 | seq_printf(s, " DURATION "); |
1171 | seq_printf(s, " FUNCTION CALLS\n"); | |
decbec38 FW |
1172 | |
1173 | /* 2nd line */ | |
49ff5903 | 1174 | seq_printf(s, "#"); |
d7a8d9e9 | 1175 | if (flags & TRACE_GRAPH_PRINT_ABS_TIME) |
9005f3eb | 1176 | seq_printf(s, " | "); |
d7a8d9e9 | 1177 | if (flags & TRACE_GRAPH_PRINT_CPU) |
49ff5903 | 1178 | seq_printf(s, " | "); |
d7a8d9e9 | 1179 | if (flags & TRACE_GRAPH_PRINT_PROC) |
49ff5903 SR |
1180 | seq_printf(s, " | | "); |
1181 | if (lat) | |
637e7e86 | 1182 | seq_printf(s, "|||||"); |
d7a8d9e9 | 1183 | if (flags & TRACE_GRAPH_PRINT_DURATION) |
9005f3eb FW |
1184 | seq_printf(s, " | | "); |
1185 | seq_printf(s, " | | | |\n"); | |
decbec38 | 1186 | } |
9005f3eb | 1187 | |
62b915f1 | 1188 | void print_graph_headers(struct seq_file *s) |
d7a8d9e9 JO |
1189 | { |
1190 | print_graph_headers_flags(s, tracer_flags.val); | |
1191 | } | |
1192 | ||
62b915f1 | 1193 | void graph_trace_open(struct trace_iterator *iter) |
9005f3eb | 1194 | { |
2fbcdb35 | 1195 | /* pid and depth on the last trace processed */ |
be1eca39 | 1196 | struct fgraph_data *data; |
9005f3eb FW |
1197 | int cpu; |
1198 | ||
be1eca39 JO |
1199 | iter->private = NULL; |
1200 | ||
1201 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
2fbcdb35 | 1202 | if (!data) |
be1eca39 JO |
1203 | goto out_err; |
1204 | ||
1205 | data->cpu_data = alloc_percpu(struct fgraph_cpu_data); | |
1206 | if (!data->cpu_data) | |
1207 | goto out_err_free; | |
1208 | ||
1209 | for_each_possible_cpu(cpu) { | |
1210 | pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid); | |
1211 | int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth); | |
1212 | int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore); | |
1213 | *pid = -1; | |
1214 | *depth = 0; | |
1215 | *ignore = 0; | |
1216 | } | |
9005f3eb | 1217 | |
2fbcdb35 | 1218 | iter->private = data; |
be1eca39 JO |
1219 | |
1220 | return; | |
1221 | ||
1222 | out_err_free: | |
1223 | kfree(data); | |
1224 | out_err: | |
1225 | pr_warning("function graph tracer: not enough memory\n"); | |
9005f3eb FW |
1226 | } |
1227 | ||
62b915f1 | 1228 | void graph_trace_close(struct trace_iterator *iter) |
9005f3eb | 1229 | { |
be1eca39 JO |
1230 | struct fgraph_data *data = iter->private; |
1231 | ||
1232 | if (data) { | |
1233 | free_percpu(data->cpu_data); | |
1234 | kfree(data); | |
1235 | } | |
9005f3eb FW |
1236 | } |
1237 | ||
a9a57763 SR |
1238 | static struct trace_event_functions graph_functions = { |
1239 | .trace = print_graph_function_event, | |
1240 | }; | |
1241 | ||
9106b693 JO |
1242 | static struct trace_event graph_trace_entry_event = { |
1243 | .type = TRACE_GRAPH_ENT, | |
a9a57763 | 1244 | .funcs = &graph_functions, |
9106b693 JO |
1245 | }; |
1246 | ||
1247 | static struct trace_event graph_trace_ret_event = { | |
1248 | .type = TRACE_GRAPH_RET, | |
a9a57763 | 1249 | .funcs = &graph_functions |
9106b693 JO |
1250 | }; |
1251 | ||
fb52607a | 1252 | static struct tracer graph_trace __read_mostly = { |
ef18012b | 1253 | .name = "function_graph", |
9005f3eb | 1254 | .open = graph_trace_open, |
be1eca39 | 1255 | .pipe_open = graph_trace_open, |
9005f3eb | 1256 | .close = graph_trace_close, |
be1eca39 | 1257 | .pipe_close = graph_trace_close, |
6eaaa5d5 | 1258 | .wait_pipe = poll_wait_pipe, |
ef18012b SR |
1259 | .init = graph_trace_init, |
1260 | .reset = graph_trace_reset, | |
decbec38 FW |
1261 | .print_line = print_graph_function, |
1262 | .print_header = print_graph_headers, | |
fb52607a | 1263 | .flags = &tracer_flags, |
7447dce9 FW |
1264 | #ifdef CONFIG_FTRACE_SELFTEST |
1265 | .selftest = trace_selftest_startup_function_graph, | |
1266 | #endif | |
fb52607a FW |
1267 | }; |
1268 | ||
1269 | static __init int init_graph_trace(void) | |
1270 | { | |
0c9e6f63 LJ |
1271 | max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1); |
1272 | ||
9106b693 JO |
1273 | if (!register_ftrace_event(&graph_trace_entry_event)) { |
1274 | pr_warning("Warning: could not register graph trace events\n"); | |
1275 | return 1; | |
1276 | } | |
1277 | ||
1278 | if (!register_ftrace_event(&graph_trace_ret_event)) { | |
1279 | pr_warning("Warning: could not register graph trace events\n"); | |
1280 | return 1; | |
1281 | } | |
1282 | ||
fb52607a FW |
1283 | return register_tracer(&graph_trace); |
1284 | } | |
1285 | ||
1286 | device_initcall(init_graph_trace); |