]>
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> | |
12 | #include <linux/fs.h> | |
13 | ||
14 | #include "trace.h" | |
f0868d1e | 15 | #include "trace_output.h" |
fb52607a | 16 | |
287b6e68 | 17 | #define TRACE_GRAPH_INDENT 2 |
fb52607a | 18 | |
1a056155 | 19 | /* Flag options */ |
fb52607a | 20 | #define TRACE_GRAPH_PRINT_OVERRUN 0x1 |
1a056155 FW |
21 | #define TRACE_GRAPH_PRINT_CPU 0x2 |
22 | #define TRACE_GRAPH_PRINT_OVERHEAD 0x4 | |
11e84acc | 23 | #define TRACE_GRAPH_PRINT_PROC 0x8 |
9005f3eb FW |
24 | #define TRACE_GRAPH_PRINT_DURATION 0x10 |
25 | #define TRACE_GRAPH_PRINT_ABS_TIME 0X20 | |
1a056155 | 26 | |
fb52607a | 27 | static struct tracer_opt trace_opts[] = { |
9005f3eb | 28 | /* Display overruns? (for self-debug purpose) */ |
1a056155 FW |
29 | { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) }, |
30 | /* Display CPU ? */ | |
31 | { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) }, | |
32 | /* Display Overhead ? */ | |
33 | { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) }, | |
11e84acc FW |
34 | /* Display proc name/pid */ |
35 | { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) }, | |
9005f3eb FW |
36 | /* Display duration of execution */ |
37 | { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) }, | |
38 | /* Display absolute time of an entry */ | |
39 | { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) }, | |
fb52607a FW |
40 | { } /* Empty entry */ |
41 | }; | |
42 | ||
43 | static struct tracer_flags tracer_flags = { | |
11e84acc | 44 | /* Don't display overruns and proc by default */ |
9005f3eb FW |
45 | .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD | |
46 | TRACE_GRAPH_PRINT_DURATION, | |
fb52607a FW |
47 | .opts = trace_opts |
48 | }; | |
49 | ||
287b6e68 | 50 | /* pid on the last trace processed */ |
9005f3eb | 51 | |
fb52607a | 52 | |
712406a6 SR |
53 | /* Add a function return address to the trace stack on thread info.*/ |
54 | int | |
55 | ftrace_push_return_trace(unsigned long ret, unsigned long long time, | |
56 | unsigned long func, int *depth) | |
57 | { | |
58 | int index; | |
59 | ||
60 | if (!current->ret_stack) | |
61 | return -EBUSY; | |
62 | ||
63 | /* The return trace stack is full */ | |
64 | if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) { | |
65 | atomic_inc(¤t->trace_overrun); | |
66 | return -EBUSY; | |
67 | } | |
68 | ||
69 | index = ++current->curr_ret_stack; | |
70 | barrier(); | |
71 | current->ret_stack[index].ret = ret; | |
72 | current->ret_stack[index].func = func; | |
73 | current->ret_stack[index].calltime = time; | |
74 | *depth = index; | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | /* Retrieve a function return address to the trace stack on thread info.*/ | |
80 | void | |
81 | ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret) | |
82 | { | |
83 | int index; | |
84 | ||
85 | index = current->curr_ret_stack; | |
86 | ||
87 | if (unlikely(index < 0)) { | |
88 | ftrace_graph_stop(); | |
89 | WARN_ON(1); | |
90 | /* Might as well panic, otherwise we have no where to go */ | |
91 | *ret = (unsigned long)panic; | |
92 | return; | |
93 | } | |
94 | ||
95 | *ret = current->ret_stack[index].ret; | |
96 | trace->func = current->ret_stack[index].func; | |
97 | trace->calltime = current->ret_stack[index].calltime; | |
98 | trace->overrun = atomic_read(¤t->trace_overrun); | |
99 | trace->depth = index; | |
100 | barrier(); | |
101 | current->curr_ret_stack--; | |
102 | ||
103 | } | |
104 | ||
105 | /* | |
106 | * Send the trace to the ring-buffer. | |
107 | * @return the original return address. | |
108 | */ | |
109 | unsigned long ftrace_return_to_handler(void) | |
110 | { | |
111 | struct ftrace_graph_ret trace; | |
112 | unsigned long ret; | |
113 | ||
114 | ftrace_pop_return_trace(&trace, &ret); | |
0012693a | 115 | trace.rettime = trace_clock_local(); |
712406a6 SR |
116 | ftrace_graph_return(&trace); |
117 | ||
118 | if (unlikely(!ret)) { | |
119 | ftrace_graph_stop(); | |
120 | WARN_ON(1); | |
121 | /* Might as well panic. What else to do? */ | |
122 | ret = (unsigned long)panic; | |
123 | } | |
124 | ||
125 | return ret; | |
126 | } | |
127 | ||
fb52607a FW |
128 | static int graph_trace_init(struct trace_array *tr) |
129 | { | |
f04109bf | 130 | int ret = register_ftrace_graph(&trace_graph_return, |
287b6e68 | 131 | &trace_graph_entry); |
660c7f9b SR |
132 | if (ret) |
133 | return ret; | |
134 | tracing_start_cmdline_record(); | |
135 | ||
136 | return 0; | |
fb52607a FW |
137 | } |
138 | ||
139 | static void graph_trace_reset(struct trace_array *tr) | |
140 | { | |
660c7f9b SR |
141 | tracing_stop_cmdline_record(); |
142 | unregister_ftrace_graph(); | |
fb52607a FW |
143 | } |
144 | ||
1a056155 FW |
145 | static inline int log10_cpu(int nb) |
146 | { | |
147 | if (nb / 100) | |
148 | return 3; | |
149 | if (nb / 10) | |
150 | return 2; | |
151 | return 1; | |
152 | } | |
153 | ||
154 | static enum print_line_t | |
155 | print_graph_cpu(struct trace_seq *s, int cpu) | |
156 | { | |
157 | int i; | |
158 | int ret; | |
159 | int log10_this = log10_cpu(cpu); | |
4462344e | 160 | int log10_all = log10_cpu(cpumask_weight(cpu_online_mask)); |
1a056155 FW |
161 | |
162 | ||
d51090b3 IM |
163 | /* |
164 | * Start with a space character - to make it stand out | |
165 | * to the right a bit when trace output is pasted into | |
166 | * email: | |
167 | */ | |
168 | ret = trace_seq_printf(s, " "); | |
169 | ||
170 | /* | |
171 | * Tricky - we space the CPU field according to the max | |
172 | * number of online CPUs. On a 2-cpu system it would take | |
173 | * a maximum of 1 digit - on a 128 cpu system it would | |
174 | * take up to 3 digits: | |
175 | */ | |
1a056155 FW |
176 | for (i = 0; i < log10_all - log10_this; i++) { |
177 | ret = trace_seq_printf(s, " "); | |
178 | if (!ret) | |
179 | return TRACE_TYPE_PARTIAL_LINE; | |
180 | } | |
181 | ret = trace_seq_printf(s, "%d) ", cpu); | |
182 | if (!ret) | |
d51090b3 IM |
183 | return TRACE_TYPE_PARTIAL_LINE; |
184 | ||
1a056155 FW |
185 | return TRACE_TYPE_HANDLED; |
186 | } | |
187 | ||
11e84acc FW |
188 | #define TRACE_GRAPH_PROCINFO_LENGTH 14 |
189 | ||
190 | static enum print_line_t | |
191 | print_graph_proc(struct trace_seq *s, pid_t pid) | |
192 | { | |
4ca53085 | 193 | char comm[TASK_COMM_LEN]; |
11e84acc FW |
194 | /* sign + log10(MAX_INT) + '\0' */ |
195 | char pid_str[11]; | |
4ca53085 SR |
196 | int spaces = 0; |
197 | int ret; | |
198 | int len; | |
199 | int i; | |
11e84acc | 200 | |
4ca53085 | 201 | trace_find_cmdline(pid, comm); |
11e84acc FW |
202 | comm[7] = '\0'; |
203 | sprintf(pid_str, "%d", pid); | |
204 | ||
205 | /* 1 stands for the "-" character */ | |
206 | len = strlen(comm) + strlen(pid_str) + 1; | |
207 | ||
208 | if (len < TRACE_GRAPH_PROCINFO_LENGTH) | |
209 | spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; | |
210 | ||
211 | /* First spaces to align center */ | |
212 | for (i = 0; i < spaces / 2; i++) { | |
213 | ret = trace_seq_printf(s, " "); | |
214 | if (!ret) | |
215 | return TRACE_TYPE_PARTIAL_LINE; | |
216 | } | |
217 | ||
218 | ret = trace_seq_printf(s, "%s-%s", comm, pid_str); | |
219 | if (!ret) | |
220 | return TRACE_TYPE_PARTIAL_LINE; | |
221 | ||
222 | /* Last spaces to align center */ | |
223 | for (i = 0; i < spaces - (spaces / 2); i++) { | |
224 | ret = trace_seq_printf(s, " "); | |
225 | if (!ret) | |
226 | return TRACE_TYPE_PARTIAL_LINE; | |
227 | } | |
228 | return TRACE_TYPE_HANDLED; | |
229 | } | |
230 | ||
1a056155 | 231 | |
287b6e68 | 232 | /* If the pid changed since the last trace, output this event */ |
11e84acc | 233 | static enum print_line_t |
9005f3eb | 234 | verif_pid(struct trace_seq *s, pid_t pid, int cpu, pid_t *last_pids_cpu) |
287b6e68 | 235 | { |
d51090b3 | 236 | pid_t prev_pid; |
9005f3eb | 237 | pid_t *last_pid; |
d51090b3 | 238 | int ret; |
660c7f9b | 239 | |
9005f3eb FW |
240 | if (!last_pids_cpu) |
241 | return TRACE_TYPE_HANDLED; | |
242 | ||
243 | last_pid = per_cpu_ptr(last_pids_cpu, cpu); | |
244 | ||
245 | if (*last_pid == pid) | |
11e84acc | 246 | return TRACE_TYPE_HANDLED; |
fb52607a | 247 | |
9005f3eb FW |
248 | prev_pid = *last_pid; |
249 | *last_pid = pid; | |
d51090b3 | 250 | |
9005f3eb FW |
251 | if (prev_pid == -1) |
252 | return TRACE_TYPE_HANDLED; | |
d51090b3 IM |
253 | /* |
254 | * Context-switch trace line: | |
255 | ||
256 | ------------------------------------------ | |
257 | | 1) migration/0--1 => sshd-1755 | |
258 | ------------------------------------------ | |
259 | ||
260 | */ | |
261 | ret = trace_seq_printf(s, | |
1fd8f2a3 | 262 | " ------------------------------------------\n"); |
11e84acc | 263 | if (!ret) |
810dc732 | 264 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
265 | |
266 | ret = print_graph_cpu(s, cpu); | |
267 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
810dc732 | 268 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
269 | |
270 | ret = print_graph_proc(s, prev_pid); | |
271 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
810dc732 | 272 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
273 | |
274 | ret = trace_seq_printf(s, " => "); | |
275 | if (!ret) | |
810dc732 | 276 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
277 | |
278 | ret = print_graph_proc(s, pid); | |
279 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
810dc732 | 280 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc FW |
281 | |
282 | ret = trace_seq_printf(s, | |
283 | "\n ------------------------------------------\n\n"); | |
284 | if (!ret) | |
810dc732 | 285 | return TRACE_TYPE_PARTIAL_LINE; |
11e84acc | 286 | |
810dc732 | 287 | return TRACE_TYPE_HANDLED; |
287b6e68 FW |
288 | } |
289 | ||
b91facc3 FW |
290 | static struct ftrace_graph_ret_entry * |
291 | get_return_for_leaf(struct trace_iterator *iter, | |
83a8df61 FW |
292 | struct ftrace_graph_ent_entry *curr) |
293 | { | |
294 | struct ring_buffer_iter *ring_iter; | |
295 | struct ring_buffer_event *event; | |
296 | struct ftrace_graph_ret_entry *next; | |
297 | ||
298 | ring_iter = iter->buffer_iter[iter->cpu]; | |
299 | ||
b91facc3 FW |
300 | /* First peek to compare current entry and the next one */ |
301 | if (ring_iter) | |
302 | event = ring_buffer_iter_peek(ring_iter, NULL); | |
303 | else { | |
304 | /* We need to consume the current entry to see the next one */ | |
305 | ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL); | |
306 | event = ring_buffer_peek(iter->tr->buffer, iter->cpu, | |
307 | NULL); | |
308 | } | |
83a8df61 FW |
309 | |
310 | if (!event) | |
b91facc3 | 311 | return NULL; |
83a8df61 FW |
312 | |
313 | next = ring_buffer_event_data(event); | |
314 | ||
315 | if (next->ent.type != TRACE_GRAPH_RET) | |
b91facc3 | 316 | return NULL; |
83a8df61 FW |
317 | |
318 | if (curr->ent.pid != next->ent.pid || | |
319 | curr->graph_ent.func != next->ret.func) | |
b91facc3 | 320 | return NULL; |
83a8df61 | 321 | |
b91facc3 FW |
322 | /* this is a leaf, now advance the iterator */ |
323 | if (ring_iter) | |
324 | ring_buffer_read(ring_iter, NULL); | |
325 | ||
326 | return next; | |
83a8df61 FW |
327 | } |
328 | ||
9005f3eb FW |
329 | /* Signal a overhead of time execution to the output */ |
330 | static int | |
331 | print_graph_overhead(unsigned long long duration, struct trace_seq *s) | |
332 | { | |
333 | /* If duration disappear, we don't need anything */ | |
334 | if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)) | |
335 | return 1; | |
336 | ||
337 | /* Non nested entry or return */ | |
338 | if (duration == -1) | |
339 | return trace_seq_printf(s, " "); | |
340 | ||
341 | if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) { | |
342 | /* Duration exceeded 100 msecs */ | |
343 | if (duration > 100000ULL) | |
344 | return trace_seq_printf(s, "! "); | |
345 | ||
346 | /* Duration exceeded 10 msecs */ | |
347 | if (duration > 10000ULL) | |
348 | return trace_seq_printf(s, "+ "); | |
349 | } | |
350 | ||
351 | return trace_seq_printf(s, " "); | |
352 | } | |
353 | ||
d1f9cbd7 FW |
354 | static int print_graph_abs_time(u64 t, struct trace_seq *s) |
355 | { | |
356 | unsigned long usecs_rem; | |
357 | ||
358 | usecs_rem = do_div(t, NSEC_PER_SEC); | |
359 | usecs_rem /= 1000; | |
360 | ||
361 | return trace_seq_printf(s, "%5lu.%06lu | ", | |
362 | (unsigned long)t, usecs_rem); | |
363 | } | |
364 | ||
f8b755ac | 365 | static enum print_line_t |
d1f9cbd7 | 366 | print_graph_irq(struct trace_iterator *iter, unsigned long addr, |
9005f3eb | 367 | enum trace_type type, int cpu, pid_t pid) |
f8b755ac FW |
368 | { |
369 | int ret; | |
d1f9cbd7 | 370 | struct trace_seq *s = &iter->seq; |
f8b755ac FW |
371 | |
372 | if (addr < (unsigned long)__irqentry_text_start || | |
373 | addr >= (unsigned long)__irqentry_text_end) | |
374 | return TRACE_TYPE_UNHANDLED; | |
375 | ||
d1f9cbd7 FW |
376 | /* Absolute time */ |
377 | if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) { | |
378 | ret = print_graph_abs_time(iter->ts, s); | |
379 | if (!ret) | |
380 | return TRACE_TYPE_PARTIAL_LINE; | |
381 | } | |
382 | ||
9005f3eb FW |
383 | /* Cpu */ |
384 | if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) { | |
385 | ret = print_graph_cpu(s, cpu); | |
386 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
387 | return TRACE_TYPE_PARTIAL_LINE; | |
388 | } | |
389 | /* Proc */ | |
390 | if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) { | |
391 | ret = print_graph_proc(s, pid); | |
392 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
393 | return TRACE_TYPE_PARTIAL_LINE; | |
394 | ret = trace_seq_printf(s, " | "); | |
395 | if (!ret) | |
396 | return TRACE_TYPE_PARTIAL_LINE; | |
397 | } | |
f8b755ac | 398 | |
9005f3eb FW |
399 | /* No overhead */ |
400 | ret = print_graph_overhead(-1, s); | |
401 | if (!ret) | |
402 | return TRACE_TYPE_PARTIAL_LINE; | |
f8b755ac | 403 | |
9005f3eb FW |
404 | if (type == TRACE_GRAPH_ENT) |
405 | ret = trace_seq_printf(s, "==========>"); | |
406 | else | |
407 | ret = trace_seq_printf(s, "<=========="); | |
408 | ||
409 | if (!ret) | |
410 | return TRACE_TYPE_PARTIAL_LINE; | |
411 | ||
412 | /* Don't close the duration column if haven't one */ | |
413 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) | |
414 | trace_seq_printf(s, " |"); | |
415 | ret = trace_seq_printf(s, "\n"); | |
f8b755ac | 416 | |
f8b755ac FW |
417 | if (!ret) |
418 | return TRACE_TYPE_PARTIAL_LINE; | |
419 | return TRACE_TYPE_HANDLED; | |
420 | } | |
83a8df61 | 421 | |
166d3c79 | 422 | static enum print_line_t |
83a8df61 FW |
423 | print_graph_duration(unsigned long long duration, struct trace_seq *s) |
424 | { | |
425 | unsigned long nsecs_rem = do_div(duration, 1000); | |
166d3c79 FW |
426 | /* log10(ULONG_MAX) + '\0' */ |
427 | char msecs_str[21]; | |
428 | char nsecs_str[5]; | |
429 | int ret, len; | |
430 | int i; | |
431 | ||
432 | sprintf(msecs_str, "%lu", (unsigned long) duration); | |
433 | ||
434 | /* Print msecs */ | |
9005f3eb | 435 | ret = trace_seq_printf(s, "%s", msecs_str); |
166d3c79 FW |
436 | if (!ret) |
437 | return TRACE_TYPE_PARTIAL_LINE; | |
438 | ||
439 | len = strlen(msecs_str); | |
440 | ||
441 | /* Print nsecs (we don't want to exceed 7 numbers) */ | |
442 | if (len < 7) { | |
443 | snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem); | |
444 | ret = trace_seq_printf(s, ".%s", nsecs_str); | |
445 | if (!ret) | |
446 | return TRACE_TYPE_PARTIAL_LINE; | |
447 | len += strlen(nsecs_str); | |
448 | } | |
449 | ||
450 | ret = trace_seq_printf(s, " us "); | |
451 | if (!ret) | |
452 | return TRACE_TYPE_PARTIAL_LINE; | |
453 | ||
454 | /* Print remaining spaces to fit the row's width */ | |
455 | for (i = len; i < 7; i++) { | |
456 | ret = trace_seq_printf(s, " "); | |
457 | if (!ret) | |
458 | return TRACE_TYPE_PARTIAL_LINE; | |
459 | } | |
460 | ||
461 | ret = trace_seq_printf(s, "| "); | |
462 | if (!ret) | |
463 | return TRACE_TYPE_PARTIAL_LINE; | |
464 | return TRACE_TYPE_HANDLED; | |
465 | ||
83a8df61 FW |
466 | } |
467 | ||
83a8df61 | 468 | /* Case of a leaf function on its call entry */ |
287b6e68 | 469 | static enum print_line_t |
83a8df61 | 470 | print_graph_entry_leaf(struct trace_iterator *iter, |
b91facc3 FW |
471 | struct ftrace_graph_ent_entry *entry, |
472 | struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s) | |
fb52607a | 473 | { |
83a8df61 | 474 | struct ftrace_graph_ret *graph_ret; |
83a8df61 FW |
475 | struct ftrace_graph_ent *call; |
476 | unsigned long long duration; | |
fb52607a | 477 | int ret; |
1a056155 | 478 | int i; |
fb52607a | 479 | |
83a8df61 FW |
480 | graph_ret = &ret_entry->ret; |
481 | call = &entry->graph_ent; | |
482 | duration = graph_ret->rettime - graph_ret->calltime; | |
483 | ||
484 | /* Overhead */ | |
9005f3eb FW |
485 | ret = print_graph_overhead(duration, s); |
486 | if (!ret) | |
487 | return TRACE_TYPE_PARTIAL_LINE; | |
1a056155 FW |
488 | |
489 | /* Duration */ | |
9005f3eb FW |
490 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) { |
491 | ret = print_graph_duration(duration, s); | |
492 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
493 | return TRACE_TYPE_PARTIAL_LINE; | |
494 | } | |
437f24fb | 495 | |
83a8df61 FW |
496 | /* Function */ |
497 | for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) { | |
498 | ret = trace_seq_printf(s, " "); | |
499 | if (!ret) | |
500 | return TRACE_TYPE_PARTIAL_LINE; | |
501 | } | |
502 | ||
503 | ret = seq_print_ip_sym(s, call->func, 0); | |
504 | if (!ret) | |
505 | return TRACE_TYPE_PARTIAL_LINE; | |
506 | ||
1a056155 | 507 | ret = trace_seq_printf(s, "();\n"); |
83a8df61 FW |
508 | if (!ret) |
509 | return TRACE_TYPE_PARTIAL_LINE; | |
510 | ||
511 | return TRACE_TYPE_HANDLED; | |
512 | } | |
513 | ||
514 | static enum print_line_t | |
515 | print_graph_entry_nested(struct ftrace_graph_ent_entry *entry, | |
f8b755ac | 516 | struct trace_seq *s, pid_t pid, int cpu) |
83a8df61 FW |
517 | { |
518 | int i; | |
519 | int ret; | |
520 | struct ftrace_graph_ent *call = &entry->graph_ent; | |
521 | ||
522 | /* No overhead */ | |
9005f3eb FW |
523 | ret = print_graph_overhead(-1, s); |
524 | if (!ret) | |
525 | return TRACE_TYPE_PARTIAL_LINE; | |
1a056155 | 526 | |
9005f3eb FW |
527 | /* No time */ |
528 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) { | |
f8b755ac FW |
529 | ret = trace_seq_printf(s, " | "); |
530 | if (!ret) | |
531 | return TRACE_TYPE_PARTIAL_LINE; | |
f8b755ac FW |
532 | } |
533 | ||
83a8df61 | 534 | /* Function */ |
287b6e68 FW |
535 | for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) { |
536 | ret = trace_seq_printf(s, " "); | |
fb52607a FW |
537 | if (!ret) |
538 | return TRACE_TYPE_PARTIAL_LINE; | |
287b6e68 FW |
539 | } |
540 | ||
541 | ret = seq_print_ip_sym(s, call->func, 0); | |
542 | if (!ret) | |
543 | return TRACE_TYPE_PARTIAL_LINE; | |
544 | ||
1a056155 | 545 | ret = trace_seq_printf(s, "() {\n"); |
83a8df61 FW |
546 | if (!ret) |
547 | return TRACE_TYPE_PARTIAL_LINE; | |
548 | ||
b91facc3 FW |
549 | /* |
550 | * we already consumed the current entry to check the next one | |
551 | * and see if this is a leaf. | |
552 | */ | |
553 | return TRACE_TYPE_NO_CONSUME; | |
287b6e68 FW |
554 | } |
555 | ||
83a8df61 | 556 | static enum print_line_t |
ac5f6c96 SR |
557 | print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s, |
558 | int type, unsigned long addr) | |
83a8df61 | 559 | { |
83a8df61 | 560 | struct trace_entry *ent = iter->ent; |
ac5f6c96 SR |
561 | pid_t *last_pid = iter->private; |
562 | int cpu = iter->cpu; | |
563 | int ret; | |
83a8df61 | 564 | |
1a056155 | 565 | /* Pid */ |
ac5f6c96 | 566 | if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE) |
9005f3eb FW |
567 | return TRACE_TYPE_PARTIAL_LINE; |
568 | ||
ac5f6c96 SR |
569 | if (type) { |
570 | /* Interrupt */ | |
571 | ret = print_graph_irq(iter, addr, type, cpu, ent->pid); | |
572 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
573 | return TRACE_TYPE_PARTIAL_LINE; | |
574 | } | |
83a8df61 | 575 | |
9005f3eb FW |
576 | /* Absolute time */ |
577 | if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) { | |
578 | ret = print_graph_abs_time(iter->ts, s); | |
579 | if (!ret) | |
580 | return TRACE_TYPE_PARTIAL_LINE; | |
581 | } | |
582 | ||
1a056155 FW |
583 | /* Cpu */ |
584 | if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) { | |
585 | ret = print_graph_cpu(s, cpu); | |
11e84acc FW |
586 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
587 | return TRACE_TYPE_PARTIAL_LINE; | |
588 | } | |
589 | ||
590 | /* Proc */ | |
591 | if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) { | |
00a8bf85 | 592 | ret = print_graph_proc(s, ent->pid); |
11e84acc FW |
593 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
594 | return TRACE_TYPE_PARTIAL_LINE; | |
595 | ||
596 | ret = trace_seq_printf(s, " | "); | |
1a056155 FW |
597 | if (!ret) |
598 | return TRACE_TYPE_PARTIAL_LINE; | |
599 | } | |
83a8df61 | 600 | |
ac5f6c96 SR |
601 | return 0; |
602 | } | |
603 | ||
604 | static enum print_line_t | |
605 | print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s, | |
606 | struct trace_iterator *iter) | |
607 | { | |
608 | int cpu = iter->cpu; | |
609 | struct ftrace_graph_ent *call = &field->graph_ent; | |
610 | struct ftrace_graph_ret_entry *leaf_ret; | |
611 | ||
612 | if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func)) | |
613 | return TRACE_TYPE_PARTIAL_LINE; | |
614 | ||
b91facc3 FW |
615 | leaf_ret = get_return_for_leaf(iter, field); |
616 | if (leaf_ret) | |
617 | return print_graph_entry_leaf(iter, field, leaf_ret, s); | |
83a8df61 | 618 | else |
f8b755ac | 619 | return print_graph_entry_nested(field, s, iter->ent->pid, cpu); |
83a8df61 FW |
620 | |
621 | } | |
622 | ||
287b6e68 FW |
623 | static enum print_line_t |
624 | print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s, | |
9005f3eb | 625 | struct trace_entry *ent, struct trace_iterator *iter) |
287b6e68 FW |
626 | { |
627 | int i; | |
628 | int ret; | |
9005f3eb | 629 | int cpu = iter->cpu; |
ac5f6c96 | 630 | pid_t pid = ent->pid; |
83a8df61 | 631 | unsigned long long duration = trace->rettime - trace->calltime; |
287b6e68 | 632 | |
ac5f6c96 | 633 | if (print_graph_prologue(iter, s, 0, 0)) |
437f24fb SR |
634 | return TRACE_TYPE_PARTIAL_LINE; |
635 | ||
83a8df61 | 636 | /* Overhead */ |
9005f3eb FW |
637 | ret = print_graph_overhead(duration, s); |
638 | if (!ret) | |
639 | return TRACE_TYPE_PARTIAL_LINE; | |
1a056155 FW |
640 | |
641 | /* Duration */ | |
9005f3eb FW |
642 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) { |
643 | ret = print_graph_duration(duration, s); | |
644 | if (ret == TRACE_TYPE_PARTIAL_LINE) | |
645 | return TRACE_TYPE_PARTIAL_LINE; | |
646 | } | |
83a8df61 FW |
647 | |
648 | /* Closing brace */ | |
287b6e68 FW |
649 | for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) { |
650 | ret = trace_seq_printf(s, " "); | |
fb52607a FW |
651 | if (!ret) |
652 | return TRACE_TYPE_PARTIAL_LINE; | |
287b6e68 FW |
653 | } |
654 | ||
1a056155 | 655 | ret = trace_seq_printf(s, "}\n"); |
287b6e68 FW |
656 | if (!ret) |
657 | return TRACE_TYPE_PARTIAL_LINE; | |
fb52607a | 658 | |
83a8df61 | 659 | /* Overrun */ |
287b6e68 FW |
660 | if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) { |
661 | ret = trace_seq_printf(s, " (Overruns: %lu)\n", | |
662 | trace->overrun); | |
fb52607a FW |
663 | if (!ret) |
664 | return TRACE_TYPE_PARTIAL_LINE; | |
287b6e68 | 665 | } |
f8b755ac | 666 | |
d1f9cbd7 | 667 | ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid); |
f8b755ac FW |
668 | if (ret == TRACE_TYPE_PARTIAL_LINE) |
669 | return TRACE_TYPE_PARTIAL_LINE; | |
670 | ||
287b6e68 FW |
671 | return TRACE_TYPE_HANDLED; |
672 | } | |
673 | ||
1fd8f2a3 | 674 | static enum print_line_t |
48ead020 | 675 | print_graph_comment(struct bprint_entry *trace, struct trace_seq *s, |
1fd8f2a3 FW |
676 | struct trace_entry *ent, struct trace_iterator *iter) |
677 | { | |
678 | int i; | |
679 | int ret; | |
9005f3eb | 680 | |
ac5f6c96 | 681 | if (print_graph_prologue(iter, s, 0, 0)) |
d1f9cbd7 FW |
682 | return TRACE_TYPE_PARTIAL_LINE; |
683 | ||
1fd8f2a3 | 684 | /* No overhead */ |
9005f3eb FW |
685 | ret = print_graph_overhead(-1, s); |
686 | if (!ret) | |
687 | return TRACE_TYPE_PARTIAL_LINE; | |
688 | ||
689 | /* No time */ | |
690 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) { | |
691 | ret = trace_seq_printf(s, " | "); | |
1fd8f2a3 FW |
692 | if (!ret) |
693 | return TRACE_TYPE_PARTIAL_LINE; | |
694 | } | |
695 | ||
1fd8f2a3 FW |
696 | /* Indentation */ |
697 | if (trace->depth > 0) | |
698 | for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) { | |
699 | ret = trace_seq_printf(s, " "); | |
700 | if (!ret) | |
701 | return TRACE_TYPE_PARTIAL_LINE; | |
702 | } | |
703 | ||
704 | /* The comment */ | |
769b0441 FW |
705 | ret = trace_seq_printf(s, "/* "); |
706 | if (!ret) | |
707 | return TRACE_TYPE_PARTIAL_LINE; | |
708 | ||
709 | ret = trace_seq_bprintf(s, trace->fmt, trace->buf); | |
1fd8f2a3 FW |
710 | if (!ret) |
711 | return TRACE_TYPE_PARTIAL_LINE; | |
712 | ||
412d0bb5 FW |
713 | /* Strip ending newline */ |
714 | if (s->buffer[s->len - 1] == '\n') { | |
715 | s->buffer[s->len - 1] = '\0'; | |
716 | s->len--; | |
717 | } | |
718 | ||
1fd8f2a3 FW |
719 | ret = trace_seq_printf(s, " */\n"); |
720 | if (!ret) | |
721 | return TRACE_TYPE_PARTIAL_LINE; | |
722 | ||
723 | return TRACE_TYPE_HANDLED; | |
724 | } | |
725 | ||
726 | ||
287b6e68 FW |
727 | enum print_line_t |
728 | print_graph_function(struct trace_iterator *iter) | |
729 | { | |
730 | struct trace_seq *s = &iter->seq; | |
731 | struct trace_entry *entry = iter->ent; | |
fb52607a | 732 | |
287b6e68 FW |
733 | switch (entry->type) { |
734 | case TRACE_GRAPH_ENT: { | |
735 | struct ftrace_graph_ent_entry *field; | |
736 | trace_assign_type(field, entry); | |
9005f3eb | 737 | return print_graph_entry(field, s, iter); |
287b6e68 FW |
738 | } |
739 | case TRACE_GRAPH_RET: { | |
740 | struct ftrace_graph_ret_entry *field; | |
741 | trace_assign_type(field, entry); | |
9005f3eb | 742 | return print_graph_return(&field->ret, s, entry, iter); |
287b6e68 | 743 | } |
48ead020 FW |
744 | case TRACE_BPRINT: { |
745 | struct bprint_entry *field; | |
1fd8f2a3 FW |
746 | trace_assign_type(field, entry); |
747 | return print_graph_comment(field, s, entry, iter); | |
748 | } | |
287b6e68 FW |
749 | default: |
750 | return TRACE_TYPE_UNHANDLED; | |
fb52607a | 751 | } |
fb52607a FW |
752 | } |
753 | ||
decbec38 FW |
754 | static void print_graph_headers(struct seq_file *s) |
755 | { | |
756 | /* 1st line */ | |
757 | seq_printf(s, "# "); | |
9005f3eb FW |
758 | if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) |
759 | seq_printf(s, " TIME "); | |
decbec38 | 760 | if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) |
9005f3eb | 761 | seq_printf(s, "CPU"); |
decbec38 | 762 | if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) |
9005f3eb FW |
763 | seq_printf(s, " TASK/PID "); |
764 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) | |
765 | seq_printf(s, " DURATION "); | |
766 | seq_printf(s, " FUNCTION CALLS\n"); | |
decbec38 FW |
767 | |
768 | /* 2nd line */ | |
769 | seq_printf(s, "# "); | |
9005f3eb FW |
770 | if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) |
771 | seq_printf(s, " | "); | |
decbec38 | 772 | if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) |
9005f3eb | 773 | seq_printf(s, "| "); |
decbec38 | 774 | if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) |
9005f3eb FW |
775 | seq_printf(s, " | | "); |
776 | if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) | |
777 | seq_printf(s, " | | "); | |
778 | seq_printf(s, " | | | |\n"); | |
decbec38 | 779 | } |
9005f3eb FW |
780 | |
781 | static void graph_trace_open(struct trace_iterator *iter) | |
782 | { | |
783 | /* pid on the last trace processed */ | |
784 | pid_t *last_pid = alloc_percpu(pid_t); | |
785 | int cpu; | |
786 | ||
787 | if (!last_pid) | |
788 | pr_warning("function graph tracer: not enough memory\n"); | |
789 | else | |
790 | for_each_possible_cpu(cpu) { | |
791 | pid_t *pid = per_cpu_ptr(last_pid, cpu); | |
792 | *pid = -1; | |
793 | } | |
794 | ||
795 | iter->private = last_pid; | |
796 | } | |
797 | ||
798 | static void graph_trace_close(struct trace_iterator *iter) | |
799 | { | |
8293dd6f | 800 | free_percpu(iter->private); |
9005f3eb FW |
801 | } |
802 | ||
fb52607a | 803 | static struct tracer graph_trace __read_mostly = { |
ef18012b | 804 | .name = "function_graph", |
9005f3eb FW |
805 | .open = graph_trace_open, |
806 | .close = graph_trace_close, | |
6eaaa5d5 | 807 | .wait_pipe = poll_wait_pipe, |
ef18012b SR |
808 | .init = graph_trace_init, |
809 | .reset = graph_trace_reset, | |
decbec38 FW |
810 | .print_line = print_graph_function, |
811 | .print_header = print_graph_headers, | |
fb52607a | 812 | .flags = &tracer_flags, |
7447dce9 FW |
813 | #ifdef CONFIG_FTRACE_SELFTEST |
814 | .selftest = trace_selftest_startup_function_graph, | |
815 | #endif | |
fb52607a FW |
816 | }; |
817 | ||
818 | static __init int init_graph_trace(void) | |
819 | { | |
820 | return register_tracer(&graph_trace); | |
821 | } | |
822 | ||
823 | device_initcall(init_graph_trace); |