• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Function graph tracer.
5  * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
6  * Mostly borrowed from function tracer which
7  * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
8  *
9  */
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 
16 #include "trace.h"
17 #include "trace_output.h"
18 
19 static bool kill_ftrace_graph;
20 
21 /**
22  * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
23  *
24  * ftrace_graph_stop() is called when a severe error is detected in
25  * the function graph tracing. This function is called by the critical
26  * paths of function graph to keep those paths from doing any more harm.
27  */
ftrace_graph_is_dead(void)28 bool ftrace_graph_is_dead(void)
29 {
30 	return kill_ftrace_graph;
31 }
32 
33 /**
34  * ftrace_graph_stop - set to permanently disable function graph tracincg
35  *
36  * In case of an error int function graph tracing, this is called
37  * to try to keep function graph tracing from causing any more harm.
38  * Usually this is pretty severe and this is called to try to at least
39  * get a warning out to the user.
40  */
ftrace_graph_stop(void)41 void ftrace_graph_stop(void)
42 {
43 	kill_ftrace_graph = true;
44 }
45 
46 /* When set, irq functions will be ignored */
47 static int ftrace_graph_skip_irqs;
48 
49 struct fgraph_cpu_data {
50 	pid_t		last_pid;
51 	int		depth;
52 	int		depth_irq;
53 	int		ignore;
54 	unsigned long	enter_funcs[FTRACE_RETFUNC_DEPTH];
55 };
56 
57 struct fgraph_data {
58 	struct fgraph_cpu_data __percpu *cpu_data;
59 
60 	/* Place to preserve last processed entry. */
61 	struct ftrace_graph_ent_entry	ent;
62 	struct ftrace_graph_ret_entry	ret;
63 	int				failed;
64 	int				cpu;
65 };
66 
67 #define TRACE_GRAPH_INDENT	2
68 
69 /* Flag options */
70 #define TRACE_GRAPH_PRINT_FLAT		0x80
71 
72 unsigned int fgraph_max_depth;
73 
74 static struct tracer_opt trace_opts[] = {
75 	/* Display overruns? (for self-debug purpose) */
76 	{ TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
77 	/* Display CPU ? */
78 	{ TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
79 	/* Display Overhead ? */
80 	{ TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
81 	/* Display proc name/pid */
82 	{ TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
83 	/* Display duration of execution */
84 	{ TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
85 	/* Display absolute time of an entry */
86 	{ TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
87 	/* Display interrupts */
88 	{ TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
89 	/* Display function name after trailing } */
90 	{ TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
91 	/* Include sleep time (scheduled out) between entry and return */
92 	{ TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) },
93 	/* Include time within nested functions */
94 	{ TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) },
95 	/* Use standard trace formatting rather than hierarchical */
96 	{ TRACER_OPT(funcgraph-flat, TRACE_GRAPH_PRINT_FLAT) },
97 	{ } /* Empty entry */
98 };
99 
100 static struct tracer_flags tracer_flags = {
101 	/* Don't display overruns, proc, or tail by default */
102 	.val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
103 	       TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS |
104 	       TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME,
105 	.opts = trace_opts
106 };
107 
108 static struct trace_array *graph_array;
109 
110 /*
111  * DURATION column is being also used to display IRQ signs,
112  * following values are used by print_graph_irq and others
113  * to fill in space into DURATION column.
114  */
115 enum {
116 	FLAGS_FILL_FULL  = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
117 	FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
118 	FLAGS_FILL_END   = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
119 };
120 
121 static void
122 print_graph_duration(struct trace_array *tr, unsigned long long duration,
123 		     struct trace_seq *s, u32 flags);
124 
125 /* Add a function return address to the trace stack on thread info.*/
126 int
ftrace_push_return_trace(unsigned long ret,unsigned long func,int * depth,unsigned long frame_pointer,unsigned long * retp)127 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
128 			 unsigned long frame_pointer, unsigned long *retp)
129 {
130 	unsigned long long calltime;
131 	int index;
132 
133 	if (unlikely(ftrace_graph_is_dead()))
134 		return -EBUSY;
135 
136 	if (!current->ret_stack)
137 		return -EBUSY;
138 
139 	/*
140 	 * We must make sure the ret_stack is tested before we read
141 	 * anything else.
142 	 */
143 	smp_rmb();
144 
145 	/* The return trace stack is full */
146 	if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
147 		atomic_inc(&current->trace_overrun);
148 		return -EBUSY;
149 	}
150 
151 	/*
152 	 * The curr_ret_stack is an index to ftrace return stack of
153 	 * current task.  Its value should be in [0, FTRACE_RETFUNC_
154 	 * DEPTH) when the function graph tracer is used.  To support
155 	 * filtering out specific functions, it makes the index
156 	 * negative by subtracting huge value (FTRACE_NOTRACE_DEPTH)
157 	 * so when it sees a negative index the ftrace will ignore
158 	 * the record.  And the index gets recovered when returning
159 	 * from the filtered function by adding the FTRACE_NOTRACE_
160 	 * DEPTH and then it'll continue to record functions normally.
161 	 *
162 	 * The curr_ret_stack is initialized to -1 and get increased
163 	 * in this function.  So it can be less than -1 only if it was
164 	 * filtered out via ftrace_graph_notrace_addr() which can be
165 	 * set from set_graph_notrace file in tracefs by user.
166 	 */
167 	if (current->curr_ret_stack < -1)
168 		return -EBUSY;
169 
170 	calltime = trace_clock_local();
171 
172 	index = ++current->curr_ret_stack;
173 	if (ftrace_graph_notrace_addr(func))
174 		current->curr_ret_stack -= FTRACE_NOTRACE_DEPTH;
175 	barrier();
176 	current->ret_stack[index].ret = ret;
177 	current->ret_stack[index].func = func;
178 	current->ret_stack[index].calltime = calltime;
179 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
180 	current->ret_stack[index].fp = frame_pointer;
181 #endif
182 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
183 	current->ret_stack[index].retp = retp;
184 #endif
185 	*depth = current->curr_ret_stack;
186 
187 	return 0;
188 }
189 
190 /* Retrieve a function return address to the trace stack on thread info.*/
191 static void
ftrace_pop_return_trace(struct ftrace_graph_ret * trace,unsigned long * ret,unsigned long frame_pointer)192 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
193 			unsigned long frame_pointer)
194 {
195 	int index;
196 
197 	index = current->curr_ret_stack;
198 
199 	/*
200 	 * A negative index here means that it's just returned from a
201 	 * notrace'd function.  Recover index to get an original
202 	 * return address.  See ftrace_push_return_trace().
203 	 *
204 	 * TODO: Need to check whether the stack gets corrupted.
205 	 */
206 	if (index < 0)
207 		index += FTRACE_NOTRACE_DEPTH;
208 
209 	if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
210 		ftrace_graph_stop();
211 		WARN_ON(1);
212 		/* Might as well panic, otherwise we have no where to go */
213 		*ret = (unsigned long)panic;
214 		return;
215 	}
216 
217 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
218 	/*
219 	 * The arch may choose to record the frame pointer used
220 	 * and check it here to make sure that it is what we expect it
221 	 * to be. If gcc does not set the place holder of the return
222 	 * address in the frame pointer, and does a copy instead, then
223 	 * the function graph trace will fail. This test detects this
224 	 * case.
225 	 *
226 	 * Currently, x86_32 with optimize for size (-Os) makes the latest
227 	 * gcc do the above.
228 	 *
229 	 * Note, -mfentry does not use frame pointers, and this test
230 	 *  is not needed if CC_USING_FENTRY is set.
231 	 */
232 	if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
233 		ftrace_graph_stop();
234 		WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
235 		     "  from func %ps return to %lx\n",
236 		     current->ret_stack[index].fp,
237 		     frame_pointer,
238 		     (void *)current->ret_stack[index].func,
239 		     current->ret_stack[index].ret);
240 		*ret = (unsigned long)panic;
241 		return;
242 	}
243 #endif
244 
245 	*ret = current->ret_stack[index].ret;
246 	trace->func = current->ret_stack[index].func;
247 	trace->calltime = current->ret_stack[index].calltime;
248 	trace->overrun = atomic_read(&current->trace_overrun);
249 	trace->depth = index;
250 }
251 
252 /*
253  * Send the trace to the ring-buffer.
254  * @return the original return address.
255  */
ftrace_return_to_handler(unsigned long frame_pointer)256 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
257 {
258 	struct ftrace_graph_ret trace;
259 	unsigned long ret;
260 
261 	ftrace_pop_return_trace(&trace, &ret, frame_pointer);
262 	trace.rettime = trace_clock_local();
263 	barrier();
264 	current->curr_ret_stack--;
265 	/*
266 	 * The curr_ret_stack can be less than -1 only if it was
267 	 * filtered out and it's about to return from the function.
268 	 * Recover the index and continue to trace normal functions.
269 	 */
270 	if (current->curr_ret_stack < -1) {
271 		current->curr_ret_stack += FTRACE_NOTRACE_DEPTH;
272 		return ret;
273 	}
274 
275 	/*
276 	 * The trace should run after decrementing the ret counter
277 	 * in case an interrupt were to come in. We don't want to
278 	 * lose the interrupt if max_depth is set.
279 	 */
280 	ftrace_graph_return(&trace);
281 
282 	if (unlikely(!ret)) {
283 		ftrace_graph_stop();
284 		WARN_ON(1);
285 		/* Might as well panic. What else to do? */
286 		ret = (unsigned long)panic;
287 	}
288 
289 	return ret;
290 }
291 
292 /**
293  * ftrace_graph_ret_addr - convert a potentially modified stack return address
294  *			   to its original value
295  *
296  * This function can be called by stack unwinding code to convert a found stack
297  * return address ('ret') to its original value, in case the function graph
298  * tracer has modified it to be 'return_to_handler'.  If the address hasn't
299  * been modified, the unchanged value of 'ret' is returned.
300  *
301  * 'idx' is a state variable which should be initialized by the caller to zero
302  * before the first call.
303  *
304  * 'retp' is a pointer to the return address on the stack.  It's ignored if
305  * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
306  */
307 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
ftrace_graph_ret_addr(struct task_struct * task,int * idx,unsigned long ret,unsigned long * retp)308 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
309 				    unsigned long ret, unsigned long *retp)
310 {
311 	int index = task->curr_ret_stack;
312 	int i;
313 
314 	if (ret != (unsigned long)return_to_handler)
315 		return ret;
316 
317 	if (index < -1)
318 		index += FTRACE_NOTRACE_DEPTH;
319 
320 	if (index < 0)
321 		return ret;
322 
323 	for (i = 0; i <= index; i++)
324 		if (task->ret_stack[i].retp == retp)
325 			return task->ret_stack[i].ret;
326 
327 	return ret;
328 }
329 #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
ftrace_graph_ret_addr(struct task_struct * task,int * idx,unsigned long ret,unsigned long * retp)330 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
331 				    unsigned long ret, unsigned long *retp)
332 {
333 	int task_idx;
334 
335 	if (ret != (unsigned long)return_to_handler)
336 		return ret;
337 
338 	task_idx = task->curr_ret_stack;
339 
340 	if (!task->ret_stack || task_idx < *idx)
341 		return ret;
342 
343 	task_idx -= *idx;
344 	(*idx)++;
345 
346 	return task->ret_stack[task_idx].ret;
347 }
348 #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
349 
__trace_graph_entry(struct trace_array * tr,struct ftrace_graph_ent * trace,unsigned long flags,int pc)350 int __trace_graph_entry(struct trace_array *tr,
351 				struct ftrace_graph_ent *trace,
352 				unsigned long flags,
353 				int pc)
354 {
355 	struct trace_event_call *call = &event_funcgraph_entry;
356 	struct ring_buffer_event *event;
357 	struct ring_buffer *buffer = tr->trace_buffer.buffer;
358 	struct ftrace_graph_ent_entry *entry;
359 
360 	event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
361 					  sizeof(*entry), flags, pc);
362 	if (!event)
363 		return 0;
364 	entry	= ring_buffer_event_data(event);
365 	entry->graph_ent			= *trace;
366 	if (!call_filter_check_discard(call, entry, buffer, event))
367 		trace_buffer_unlock_commit_nostack(buffer, event);
368 
369 	return 1;
370 }
371 
ftrace_graph_ignore_irqs(void)372 static inline int ftrace_graph_ignore_irqs(void)
373 {
374 	if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
375 		return 0;
376 
377 	return in_irq();
378 }
379 
trace_graph_entry(struct ftrace_graph_ent * trace)380 int trace_graph_entry(struct ftrace_graph_ent *trace)
381 {
382 	struct trace_array *tr = graph_array;
383 	struct trace_array_cpu *data;
384 	unsigned long flags;
385 	long disabled;
386 	int ret;
387 	int cpu;
388 	int pc;
389 
390 	if (!ftrace_trace_task(tr))
391 		return 0;
392 
393 	if (ftrace_graph_ignore_func(trace))
394 		return 0;
395 
396 	if (ftrace_graph_ignore_irqs())
397 		return 0;
398 
399 	/*
400 	 * Do not trace a function if it's filtered by set_graph_notrace.
401 	 * Make the index of ret stack negative to indicate that it should
402 	 * ignore further functions.  But it needs its own ret stack entry
403 	 * to recover the original index in order to continue tracing after
404 	 * returning from the function.
405 	 */
406 	if (ftrace_graph_notrace_addr(trace->func))
407 		return 1;
408 
409 	/*
410 	 * Stop here if tracing_threshold is set. We only write function return
411 	 * events to the ring buffer.
412 	 */
413 	if (tracing_thresh)
414 		return 1;
415 
416 	local_irq_save(flags);
417 	cpu = raw_smp_processor_id();
418 	data = per_cpu_ptr(tr->trace_buffer.data, cpu);
419 	disabled = atomic_inc_return(&data->disabled);
420 	if (likely(disabled == 1)) {
421 		pc = preempt_count();
422 		ret = __trace_graph_entry(tr, trace, flags, pc);
423 	} else {
424 		ret = 0;
425 	}
426 
427 	atomic_dec(&data->disabled);
428 	local_irq_restore(flags);
429 
430 	return ret;
431 }
432 
433 static void
__trace_graph_function(struct trace_array * tr,unsigned long ip,unsigned long flags,int pc)434 __trace_graph_function(struct trace_array *tr,
435 		unsigned long ip, unsigned long flags, int pc)
436 {
437 	u64 time = trace_clock_local();
438 	struct ftrace_graph_ent ent = {
439 		.func  = ip,
440 		.depth = 0,
441 	};
442 	struct ftrace_graph_ret ret = {
443 		.func     = ip,
444 		.depth    = 0,
445 		.calltime = time,
446 		.rettime  = time,
447 	};
448 
449 	__trace_graph_entry(tr, &ent, flags, pc);
450 	__trace_graph_return(tr, &ret, flags, pc);
451 }
452 
453 void
trace_graph_function(struct trace_array * tr,unsigned long ip,unsigned long parent_ip,unsigned long flags,int pc)454 trace_graph_function(struct trace_array *tr,
455 		unsigned long ip, unsigned long parent_ip,
456 		unsigned long flags, int pc)
457 {
458 	__trace_graph_function(tr, ip, flags, pc);
459 }
460 
__trace_graph_return(struct trace_array * tr,struct ftrace_graph_ret * trace,unsigned long flags,int pc)461 void __trace_graph_return(struct trace_array *tr,
462 				struct ftrace_graph_ret *trace,
463 				unsigned long flags,
464 				int pc)
465 {
466 	struct trace_event_call *call = &event_funcgraph_exit;
467 	struct ring_buffer_event *event;
468 	struct ring_buffer *buffer = tr->trace_buffer.buffer;
469 	struct ftrace_graph_ret_entry *entry;
470 
471 	event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
472 					  sizeof(*entry), flags, pc);
473 	if (!event)
474 		return;
475 	entry	= ring_buffer_event_data(event);
476 	entry->ret				= *trace;
477 	if (!call_filter_check_discard(call, entry, buffer, event))
478 		trace_buffer_unlock_commit_nostack(buffer, event);
479 }
480 
trace_graph_return(struct ftrace_graph_ret * trace)481 void trace_graph_return(struct ftrace_graph_ret *trace)
482 {
483 	struct trace_array *tr = graph_array;
484 	struct trace_array_cpu *data;
485 	unsigned long flags;
486 	long disabled;
487 	int cpu;
488 	int pc;
489 
490 	ftrace_graph_addr_finish(trace);
491 
492 	local_irq_save(flags);
493 	cpu = raw_smp_processor_id();
494 	data = per_cpu_ptr(tr->trace_buffer.data, cpu);
495 	disabled = atomic_inc_return(&data->disabled);
496 	if (likely(disabled == 1)) {
497 		pc = preempt_count();
498 		__trace_graph_return(tr, trace, flags, pc);
499 	}
500 	atomic_dec(&data->disabled);
501 	local_irq_restore(flags);
502 }
503 
set_graph_array(struct trace_array * tr)504 void set_graph_array(struct trace_array *tr)
505 {
506 	graph_array = tr;
507 
508 	/* Make graph_array visible before we start tracing */
509 
510 	smp_mb();
511 }
512 
trace_graph_thresh_return(struct ftrace_graph_ret * trace)513 static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
514 {
515 	ftrace_graph_addr_finish(trace);
516 
517 	if (tracing_thresh &&
518 	    (trace->rettime - trace->calltime < tracing_thresh))
519 		return;
520 	else
521 		trace_graph_return(trace);
522 }
523 
graph_trace_init(struct trace_array * tr)524 static int graph_trace_init(struct trace_array *tr)
525 {
526 	int ret;
527 
528 	set_graph_array(tr);
529 	if (tracing_thresh)
530 		ret = register_ftrace_graph(&trace_graph_thresh_return,
531 					    &trace_graph_entry);
532 	else
533 		ret = register_ftrace_graph(&trace_graph_return,
534 					    &trace_graph_entry);
535 	if (ret)
536 		return ret;
537 	tracing_start_cmdline_record();
538 
539 	return 0;
540 }
541 
graph_trace_reset(struct trace_array * tr)542 static void graph_trace_reset(struct trace_array *tr)
543 {
544 	tracing_stop_cmdline_record();
545 	unregister_ftrace_graph();
546 }
547 
graph_trace_update_thresh(struct trace_array * tr)548 static int graph_trace_update_thresh(struct trace_array *tr)
549 {
550 	graph_trace_reset(tr);
551 	return graph_trace_init(tr);
552 }
553 
554 static int max_bytes_for_cpu;
555 
print_graph_cpu(struct trace_seq * s,int cpu)556 static void print_graph_cpu(struct trace_seq *s, int cpu)
557 {
558 	/*
559 	 * Start with a space character - to make it stand out
560 	 * to the right a bit when trace output is pasted into
561 	 * email:
562 	 */
563 	trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
564 }
565 
566 #define TRACE_GRAPH_PROCINFO_LENGTH	14
567 
print_graph_proc(struct trace_seq * s,pid_t pid)568 static void print_graph_proc(struct trace_seq *s, pid_t pid)
569 {
570 	char comm[TASK_COMM_LEN];
571 	/* sign + log10(MAX_INT) + '\0' */
572 	char pid_str[11];
573 	int spaces = 0;
574 	int len;
575 	int i;
576 
577 	trace_find_cmdline(pid, comm);
578 	comm[7] = '\0';
579 	sprintf(pid_str, "%d", pid);
580 
581 	/* 1 stands for the "-" character */
582 	len = strlen(comm) + strlen(pid_str) + 1;
583 
584 	if (len < TRACE_GRAPH_PROCINFO_LENGTH)
585 		spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
586 
587 	/* First spaces to align center */
588 	for (i = 0; i < spaces / 2; i++)
589 		trace_seq_putc(s, ' ');
590 
591 	trace_seq_printf(s, "%s-%s", comm, pid_str);
592 
593 	/* Last spaces to align center */
594 	for (i = 0; i < spaces - (spaces / 2); i++)
595 		trace_seq_putc(s, ' ');
596 }
597 
598 
print_graph_lat_fmt(struct trace_seq * s,struct trace_entry * entry)599 static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
600 {
601 	trace_seq_putc(s, ' ');
602 	trace_print_lat_fmt(s, entry);
603 }
604 
605 /* If the pid changed since the last trace, output this event */
606 static void
verif_pid(struct trace_seq * s,pid_t pid,int cpu,struct fgraph_data * data)607 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
608 {
609 	pid_t prev_pid;
610 	pid_t *last_pid;
611 
612 	if (!data)
613 		return;
614 
615 	last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
616 
617 	if (*last_pid == pid)
618 		return;
619 
620 	prev_pid = *last_pid;
621 	*last_pid = pid;
622 
623 	if (prev_pid == -1)
624 		return;
625 /*
626  * Context-switch trace line:
627 
628  ------------------------------------------
629  | 1)  migration/0--1  =>  sshd-1755
630  ------------------------------------------
631 
632  */
633 	trace_seq_puts(s, " ------------------------------------------\n");
634 	print_graph_cpu(s, cpu);
635 	print_graph_proc(s, prev_pid);
636 	trace_seq_puts(s, " => ");
637 	print_graph_proc(s, pid);
638 	trace_seq_puts(s, "\n ------------------------------------------\n\n");
639 }
640 
641 static struct ftrace_graph_ret_entry *
get_return_for_leaf(struct trace_iterator * iter,struct ftrace_graph_ent_entry * curr)642 get_return_for_leaf(struct trace_iterator *iter,
643 		struct ftrace_graph_ent_entry *curr)
644 {
645 	struct fgraph_data *data = iter->private;
646 	struct ring_buffer_iter *ring_iter = NULL;
647 	struct ring_buffer_event *event;
648 	struct ftrace_graph_ret_entry *next;
649 
650 	/*
651 	 * If the previous output failed to write to the seq buffer,
652 	 * then we just reuse the data from before.
653 	 */
654 	if (data && data->failed) {
655 		curr = &data->ent;
656 		next = &data->ret;
657 	} else {
658 
659 		ring_iter = trace_buffer_iter(iter, iter->cpu);
660 
661 		/* First peek to compare current entry and the next one */
662 		if (ring_iter)
663 			event = ring_buffer_iter_peek(ring_iter, NULL);
664 		else {
665 			/*
666 			 * We need to consume the current entry to see
667 			 * the next one.
668 			 */
669 			ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
670 					    NULL, NULL);
671 			event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
672 						 NULL, NULL);
673 		}
674 
675 		if (!event)
676 			return NULL;
677 
678 		next = ring_buffer_event_data(event);
679 
680 		if (data) {
681 			/*
682 			 * Save current and next entries for later reference
683 			 * if the output fails.
684 			 */
685 			data->ent = *curr;
686 			/*
687 			 * If the next event is not a return type, then
688 			 * we only care about what type it is. Otherwise we can
689 			 * safely copy the entire event.
690 			 */
691 			if (next->ent.type == TRACE_GRAPH_RET)
692 				data->ret = *next;
693 			else
694 				data->ret.ent.type = next->ent.type;
695 		}
696 	}
697 
698 	if (next->ent.type != TRACE_GRAPH_RET)
699 		return NULL;
700 
701 	if (curr->ent.pid != next->ent.pid ||
702 			curr->graph_ent.func != next->ret.func)
703 		return NULL;
704 
705 	/* this is a leaf, now advance the iterator */
706 	if (ring_iter)
707 		ring_buffer_read(ring_iter, NULL);
708 
709 	return next;
710 }
711 
print_graph_abs_time(u64 t,struct trace_seq * s)712 static void print_graph_abs_time(u64 t, struct trace_seq *s)
713 {
714 	unsigned long usecs_rem;
715 
716 	usecs_rem = do_div(t, NSEC_PER_SEC);
717 	usecs_rem /= 1000;
718 
719 	trace_seq_printf(s, "%5lu.%06lu |  ",
720 			 (unsigned long)t, usecs_rem);
721 }
722 
723 static void
print_graph_irq(struct trace_iterator * iter,unsigned long addr,enum trace_type type,int cpu,pid_t pid,u32 flags)724 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
725 		enum trace_type type, int cpu, pid_t pid, u32 flags)
726 {
727 	struct trace_array *tr = iter->tr;
728 	struct trace_seq *s = &iter->seq;
729 	struct trace_entry *ent = iter->ent;
730 
731 	if (addr < (unsigned long)__irqentry_text_start ||
732 		addr >= (unsigned long)__irqentry_text_end)
733 		return;
734 
735 	if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
736 		/* Absolute time */
737 		if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
738 			print_graph_abs_time(iter->ts, s);
739 
740 		/* Cpu */
741 		if (flags & TRACE_GRAPH_PRINT_CPU)
742 			print_graph_cpu(s, cpu);
743 
744 		/* Proc */
745 		if (flags & TRACE_GRAPH_PRINT_PROC) {
746 			print_graph_proc(s, pid);
747 			trace_seq_puts(s, " | ");
748 		}
749 
750 		/* Latency format */
751 		if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
752 			print_graph_lat_fmt(s, ent);
753 	}
754 
755 	/* No overhead */
756 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
757 
758 	if (type == TRACE_GRAPH_ENT)
759 		trace_seq_puts(s, "==========>");
760 	else
761 		trace_seq_puts(s, "<==========");
762 
763 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
764 	trace_seq_putc(s, '\n');
765 }
766 
767 void
trace_print_graph_duration(unsigned long long duration,struct trace_seq * s)768 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
769 {
770 	unsigned long nsecs_rem = do_div(duration, 1000);
771 	/* log10(ULONG_MAX) + '\0' */
772 	char usecs_str[21];
773 	char nsecs_str[5];
774 	int len;
775 	int i;
776 
777 	sprintf(usecs_str, "%lu", (unsigned long) duration);
778 
779 	/* Print msecs */
780 	trace_seq_printf(s, "%s", usecs_str);
781 
782 	len = strlen(usecs_str);
783 
784 	/* Print nsecs (we don't want to exceed 7 numbers) */
785 	if (len < 7) {
786 		size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
787 
788 		snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
789 		trace_seq_printf(s, ".%s", nsecs_str);
790 		len += strlen(nsecs_str) + 1;
791 	}
792 
793 	trace_seq_puts(s, " us ");
794 
795 	/* Print remaining spaces to fit the row's width */
796 	for (i = len; i < 8; i++)
797 		trace_seq_putc(s, ' ');
798 }
799 
800 static void
print_graph_duration(struct trace_array * tr,unsigned long long duration,struct trace_seq * s,u32 flags)801 print_graph_duration(struct trace_array *tr, unsigned long long duration,
802 		     struct trace_seq *s, u32 flags)
803 {
804 	if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
805 	    !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
806 		return;
807 
808 	/* No real adata, just filling the column with spaces */
809 	switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
810 	case FLAGS_FILL_FULL:
811 		trace_seq_puts(s, "              |  ");
812 		return;
813 	case FLAGS_FILL_START:
814 		trace_seq_puts(s, "  ");
815 		return;
816 	case FLAGS_FILL_END:
817 		trace_seq_puts(s, " |");
818 		return;
819 	}
820 
821 	/* Signal a overhead of time execution to the output */
822 	if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
823 		trace_seq_printf(s, "%c ", trace_find_mark(duration));
824 	else
825 		trace_seq_puts(s, "  ");
826 
827 	trace_print_graph_duration(duration, s);
828 	trace_seq_puts(s, "|  ");
829 }
830 
831 /* Case of a leaf function on its call entry */
832 static enum print_line_t
print_graph_entry_leaf(struct trace_iterator * iter,struct ftrace_graph_ent_entry * entry,struct ftrace_graph_ret_entry * ret_entry,struct trace_seq * s,u32 flags)833 print_graph_entry_leaf(struct trace_iterator *iter,
834 		struct ftrace_graph_ent_entry *entry,
835 		struct ftrace_graph_ret_entry *ret_entry,
836 		struct trace_seq *s, u32 flags)
837 {
838 	struct fgraph_data *data = iter->private;
839 	struct trace_array *tr = iter->tr;
840 	struct ftrace_graph_ret *graph_ret;
841 	struct ftrace_graph_ent *call;
842 	unsigned long long duration;
843 	int cpu = iter->cpu;
844 	int i;
845 
846 	graph_ret = &ret_entry->ret;
847 	call = &entry->graph_ent;
848 	duration = graph_ret->rettime - graph_ret->calltime;
849 
850 	if (data) {
851 		struct fgraph_cpu_data *cpu_data;
852 
853 		cpu_data = per_cpu_ptr(data->cpu_data, cpu);
854 
855 		/* If a graph tracer ignored set_graph_notrace */
856 		if (call->depth < -1)
857 			call->depth += FTRACE_NOTRACE_DEPTH;
858 
859 		/*
860 		 * Comments display at + 1 to depth. Since
861 		 * this is a leaf function, keep the comments
862 		 * equal to this depth.
863 		 */
864 		cpu_data->depth = call->depth - 1;
865 
866 		/* No need to keep this function around for this depth */
867 		if (call->depth < FTRACE_RETFUNC_DEPTH &&
868 		    !WARN_ON_ONCE(call->depth < 0))
869 			cpu_data->enter_funcs[call->depth] = 0;
870 	}
871 
872 	/* Overhead and duration */
873 	print_graph_duration(tr, duration, s, flags);
874 
875 	/* Function */
876 	for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
877 		trace_seq_putc(s, ' ');
878 
879 	trace_seq_printf(s, "%ps();\n", (void *)call->func);
880 
881 	print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET,
882 			cpu, iter->ent->pid, flags);
883 
884 	return trace_handle_return(s);
885 }
886 
887 static enum print_line_t
print_graph_entry_nested(struct trace_iterator * iter,struct ftrace_graph_ent_entry * entry,struct trace_seq * s,int cpu,u32 flags)888 print_graph_entry_nested(struct trace_iterator *iter,
889 			 struct ftrace_graph_ent_entry *entry,
890 			 struct trace_seq *s, int cpu, u32 flags)
891 {
892 	struct ftrace_graph_ent *call = &entry->graph_ent;
893 	struct fgraph_data *data = iter->private;
894 	struct trace_array *tr = iter->tr;
895 	int i;
896 
897 	if (data) {
898 		struct fgraph_cpu_data *cpu_data;
899 		int cpu = iter->cpu;
900 
901 		/* If a graph tracer ignored set_graph_notrace */
902 		if (call->depth < -1)
903 			call->depth += FTRACE_NOTRACE_DEPTH;
904 
905 		cpu_data = per_cpu_ptr(data->cpu_data, cpu);
906 		cpu_data->depth = call->depth;
907 
908 		/* Save this function pointer to see if the exit matches */
909 		if (call->depth < FTRACE_RETFUNC_DEPTH &&
910 		    !WARN_ON_ONCE(call->depth < 0))
911 			cpu_data->enter_funcs[call->depth] = call->func;
912 	}
913 
914 	/* No time */
915 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
916 
917 	/* Function */
918 	for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
919 		trace_seq_putc(s, ' ');
920 
921 	trace_seq_printf(s, "%ps() {\n", (void *)call->func);
922 
923 	if (trace_seq_has_overflowed(s))
924 		return TRACE_TYPE_PARTIAL_LINE;
925 
926 	/*
927 	 * we already consumed the current entry to check the next one
928 	 * and see if this is a leaf.
929 	 */
930 	return TRACE_TYPE_NO_CONSUME;
931 }
932 
933 static void
print_graph_prologue(struct trace_iterator * iter,struct trace_seq * s,int type,unsigned long addr,u32 flags)934 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
935 		     int type, unsigned long addr, u32 flags)
936 {
937 	struct fgraph_data *data = iter->private;
938 	struct trace_entry *ent = iter->ent;
939 	struct trace_array *tr = iter->tr;
940 	int cpu = iter->cpu;
941 
942 	/* Pid */
943 	verif_pid(s, ent->pid, cpu, data);
944 
945 	if (type)
946 		/* Interrupt */
947 		print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
948 
949 	if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
950 		return;
951 
952 	/* Absolute time */
953 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
954 		print_graph_abs_time(iter->ts, s);
955 
956 	/* Cpu */
957 	if (flags & TRACE_GRAPH_PRINT_CPU)
958 		print_graph_cpu(s, cpu);
959 
960 	/* Proc */
961 	if (flags & TRACE_GRAPH_PRINT_PROC) {
962 		print_graph_proc(s, ent->pid);
963 		trace_seq_puts(s, " | ");
964 	}
965 
966 	/* Latency format */
967 	if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
968 		print_graph_lat_fmt(s, ent);
969 
970 	return;
971 }
972 
973 /*
974  * Entry check for irq code
975  *
976  * returns 1 if
977  *  - we are inside irq code
978  *  - we just entered irq code
979  *
980  * retunns 0 if
981  *  - funcgraph-interrupts option is set
982  *  - we are not inside irq code
983  */
984 static int
check_irq_entry(struct trace_iterator * iter,u32 flags,unsigned long addr,int depth)985 check_irq_entry(struct trace_iterator *iter, u32 flags,
986 		unsigned long addr, int depth)
987 {
988 	int cpu = iter->cpu;
989 	int *depth_irq;
990 	struct fgraph_data *data = iter->private;
991 
992 	/*
993 	 * If we are either displaying irqs, or we got called as
994 	 * a graph event and private data does not exist,
995 	 * then we bypass the irq check.
996 	 */
997 	if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
998 	    (!data))
999 		return 0;
1000 
1001 	depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1002 
1003 	/*
1004 	 * We are inside the irq code
1005 	 */
1006 	if (*depth_irq >= 0)
1007 		return 1;
1008 
1009 	if ((addr < (unsigned long)__irqentry_text_start) ||
1010 	    (addr >= (unsigned long)__irqentry_text_end))
1011 		return 0;
1012 
1013 	/*
1014 	 * We are entering irq code.
1015 	 */
1016 	*depth_irq = depth;
1017 	return 1;
1018 }
1019 
1020 /*
1021  * Return check for irq code
1022  *
1023  * returns 1 if
1024  *  - we are inside irq code
1025  *  - we just left irq code
1026  *
1027  * returns 0 if
1028  *  - funcgraph-interrupts option is set
1029  *  - we are not inside irq code
1030  */
1031 static int
check_irq_return(struct trace_iterator * iter,u32 flags,int depth)1032 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
1033 {
1034 	int cpu = iter->cpu;
1035 	int *depth_irq;
1036 	struct fgraph_data *data = iter->private;
1037 
1038 	/*
1039 	 * If we are either displaying irqs, or we got called as
1040 	 * a graph event and private data does not exist,
1041 	 * then we bypass the irq check.
1042 	 */
1043 	if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
1044 	    (!data))
1045 		return 0;
1046 
1047 	depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1048 
1049 	/*
1050 	 * We are not inside the irq code.
1051 	 */
1052 	if (*depth_irq == -1)
1053 		return 0;
1054 
1055 	/*
1056 	 * We are inside the irq code, and this is returning entry.
1057 	 * Let's not trace it and clear the entry depth, since
1058 	 * we are out of irq code.
1059 	 *
1060 	 * This condition ensures that we 'leave the irq code' once
1061 	 * we are out of the entry depth. Thus protecting us from
1062 	 * the RETURN entry loss.
1063 	 */
1064 	if (*depth_irq >= depth) {
1065 		*depth_irq = -1;
1066 		return 1;
1067 	}
1068 
1069 	/*
1070 	 * We are inside the irq code, and this is not the entry.
1071 	 */
1072 	return 1;
1073 }
1074 
1075 static enum print_line_t
print_graph_entry(struct ftrace_graph_ent_entry * field,struct trace_seq * s,struct trace_iterator * iter,u32 flags)1076 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
1077 			struct trace_iterator *iter, u32 flags)
1078 {
1079 	struct fgraph_data *data = iter->private;
1080 	struct ftrace_graph_ent *call = &field->graph_ent;
1081 	struct ftrace_graph_ret_entry *leaf_ret;
1082 	static enum print_line_t ret;
1083 	int cpu = iter->cpu;
1084 
1085 	if (check_irq_entry(iter, flags, call->func, call->depth))
1086 		return TRACE_TYPE_HANDLED;
1087 
1088 	print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
1089 
1090 	leaf_ret = get_return_for_leaf(iter, field);
1091 	if (leaf_ret)
1092 		ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
1093 	else
1094 		ret = print_graph_entry_nested(iter, field, s, cpu, flags);
1095 
1096 	if (data) {
1097 		/*
1098 		 * If we failed to write our output, then we need to make
1099 		 * note of it. Because we already consumed our entry.
1100 		 */
1101 		if (s->full) {
1102 			data->failed = 1;
1103 			data->cpu = cpu;
1104 		} else
1105 			data->failed = 0;
1106 	}
1107 
1108 	return ret;
1109 }
1110 
1111 static enum print_line_t
print_graph_return(struct ftrace_graph_ret * trace,struct trace_seq * s,struct trace_entry * ent,struct trace_iterator * iter,u32 flags)1112 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
1113 		   struct trace_entry *ent, struct trace_iterator *iter,
1114 		   u32 flags)
1115 {
1116 	unsigned long long duration = trace->rettime - trace->calltime;
1117 	struct fgraph_data *data = iter->private;
1118 	struct trace_array *tr = iter->tr;
1119 	pid_t pid = ent->pid;
1120 	int cpu = iter->cpu;
1121 	int func_match = 1;
1122 	int i;
1123 
1124 	if (check_irq_return(iter, flags, trace->depth))
1125 		return TRACE_TYPE_HANDLED;
1126 
1127 	if (data) {
1128 		struct fgraph_cpu_data *cpu_data;
1129 		int cpu = iter->cpu;
1130 
1131 		cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1132 
1133 		/*
1134 		 * Comments display at + 1 to depth. This is the
1135 		 * return from a function, we now want the comments
1136 		 * to display at the same level of the bracket.
1137 		 */
1138 		cpu_data->depth = trace->depth - 1;
1139 
1140 		if (trace->depth < FTRACE_RETFUNC_DEPTH &&
1141 		    !WARN_ON_ONCE(trace->depth < 0)) {
1142 			if (cpu_data->enter_funcs[trace->depth] != trace->func)
1143 				func_match = 0;
1144 			cpu_data->enter_funcs[trace->depth] = 0;
1145 		}
1146 	}
1147 
1148 	print_graph_prologue(iter, s, 0, 0, flags);
1149 
1150 	/* Overhead and duration */
1151 	print_graph_duration(tr, duration, s, flags);
1152 
1153 	/* Closing brace */
1154 	for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
1155 		trace_seq_putc(s, ' ');
1156 
1157 	/*
1158 	 * If the return function does not have a matching entry,
1159 	 * then the entry was lost. Instead of just printing
1160 	 * the '}' and letting the user guess what function this
1161 	 * belongs to, write out the function name. Always do
1162 	 * that if the funcgraph-tail option is enabled.
1163 	 */
1164 	if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
1165 		trace_seq_puts(s, "}\n");
1166 	else
1167 		trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1168 
1169 	/* Overrun */
1170 	if (flags & TRACE_GRAPH_PRINT_OVERRUN)
1171 		trace_seq_printf(s, " (Overruns: %lu)\n",
1172 				 trace->overrun);
1173 
1174 	print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1175 			cpu, pid, flags);
1176 
1177 	return trace_handle_return(s);
1178 }
1179 
1180 static enum print_line_t
print_graph_comment(struct trace_seq * s,struct trace_entry * ent,struct trace_iterator * iter,u32 flags)1181 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1182 		    struct trace_iterator *iter, u32 flags)
1183 {
1184 	struct trace_array *tr = iter->tr;
1185 	unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
1186 	struct fgraph_data *data = iter->private;
1187 	struct trace_event *event;
1188 	int depth = 0;
1189 	int ret;
1190 	int i;
1191 
1192 	if (data)
1193 		depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1194 
1195 	print_graph_prologue(iter, s, 0, 0, flags);
1196 
1197 	/* No time */
1198 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
1199 
1200 	/* Indentation */
1201 	if (depth > 0)
1202 		for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
1203 			trace_seq_putc(s, ' ');
1204 
1205 	/* The comment */
1206 	trace_seq_puts(s, "/* ");
1207 
1208 	switch (iter->ent->type) {
1209 	case TRACE_BPUTS:
1210 		ret = trace_print_bputs_msg_only(iter);
1211 		if (ret != TRACE_TYPE_HANDLED)
1212 			return ret;
1213 		break;
1214 	case TRACE_BPRINT:
1215 		ret = trace_print_bprintk_msg_only(iter);
1216 		if (ret != TRACE_TYPE_HANDLED)
1217 			return ret;
1218 		break;
1219 	case TRACE_PRINT:
1220 		ret = trace_print_printk_msg_only(iter);
1221 		if (ret != TRACE_TYPE_HANDLED)
1222 			return ret;
1223 		break;
1224 	default:
1225 		event = ftrace_find_event(ent->type);
1226 		if (!event)
1227 			return TRACE_TYPE_UNHANDLED;
1228 
1229 		ret = event->funcs->trace(iter, sym_flags, event);
1230 		if (ret != TRACE_TYPE_HANDLED)
1231 			return ret;
1232 	}
1233 
1234 	if (trace_seq_has_overflowed(s))
1235 		goto out;
1236 
1237 	/* Strip ending newline */
1238 	if (s->buffer[s->seq.len - 1] == '\n') {
1239 		s->buffer[s->seq.len - 1] = '\0';
1240 		s->seq.len--;
1241 	}
1242 
1243 	trace_seq_puts(s, " */\n");
1244  out:
1245 	return trace_handle_return(s);
1246 }
1247 
1248 
1249 enum print_line_t
print_graph_function_flags(struct trace_iterator * iter,u32 flags)1250 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1251 {
1252 	struct ftrace_graph_ent_entry *field;
1253 	struct fgraph_data *data = iter->private;
1254 	struct trace_entry *entry = iter->ent;
1255 	struct trace_seq *s = &iter->seq;
1256 	int cpu = iter->cpu;
1257 	int ret;
1258 
1259 	if (flags & TRACE_GRAPH_PRINT_FLAT)
1260 		return TRACE_TYPE_UNHANDLED;
1261 
1262 	if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1263 		per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1264 		return TRACE_TYPE_HANDLED;
1265 	}
1266 
1267 	/*
1268 	 * If the last output failed, there's a possibility we need
1269 	 * to print out the missing entry which would never go out.
1270 	 */
1271 	if (data && data->failed) {
1272 		field = &data->ent;
1273 		iter->cpu = data->cpu;
1274 		ret = print_graph_entry(field, s, iter, flags);
1275 		if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1276 			per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1277 			ret = TRACE_TYPE_NO_CONSUME;
1278 		}
1279 		iter->cpu = cpu;
1280 		return ret;
1281 	}
1282 
1283 	switch (entry->type) {
1284 	case TRACE_GRAPH_ENT: {
1285 		/*
1286 		 * print_graph_entry() may consume the current event,
1287 		 * thus @field may become invalid, so we need to save it.
1288 		 * sizeof(struct ftrace_graph_ent_entry) is very small,
1289 		 * it can be safely saved at the stack.
1290 		 */
1291 		struct ftrace_graph_ent_entry saved;
1292 		trace_assign_type(field, entry);
1293 		saved = *field;
1294 		return print_graph_entry(&saved, s, iter, flags);
1295 	}
1296 	case TRACE_GRAPH_RET: {
1297 		struct ftrace_graph_ret_entry *field;
1298 		trace_assign_type(field, entry);
1299 		return print_graph_return(&field->ret, s, entry, iter, flags);
1300 	}
1301 	case TRACE_STACK:
1302 	case TRACE_FN:
1303 		/* dont trace stack and functions as comments */
1304 		return TRACE_TYPE_UNHANDLED;
1305 
1306 	default:
1307 		return print_graph_comment(s, entry, iter, flags);
1308 	}
1309 
1310 	return TRACE_TYPE_HANDLED;
1311 }
1312 
1313 static enum print_line_t
print_graph_function(struct trace_iterator * iter)1314 print_graph_function(struct trace_iterator *iter)
1315 {
1316 	return print_graph_function_flags(iter, tracer_flags.val);
1317 }
1318 
print_lat_header(struct seq_file * s,u32 flags)1319 static void print_lat_header(struct seq_file *s, u32 flags)
1320 {
1321 	static const char spaces[] = "                "	/* 16 spaces */
1322 		"    "					/* 4 spaces */
1323 		"                 ";			/* 17 spaces */
1324 	int size = 0;
1325 
1326 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1327 		size += 16;
1328 	if (flags & TRACE_GRAPH_PRINT_CPU)
1329 		size += 4;
1330 	if (flags & TRACE_GRAPH_PRINT_PROC)
1331 		size += 17;
1332 
1333 	seq_printf(s, "#%.*s  _-----=> irqs-off        \n", size, spaces);
1334 	seq_printf(s, "#%.*s / _----=> need-resched    \n", size, spaces);
1335 	seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1336 	seq_printf(s, "#%.*s|| / _--=> preempt-depth   \n", size, spaces);
1337 	seq_printf(s, "#%.*s||| /                      \n", size, spaces);
1338 }
1339 
__print_graph_headers_flags(struct trace_array * tr,struct seq_file * s,u32 flags)1340 static void __print_graph_headers_flags(struct trace_array *tr,
1341 					struct seq_file *s, u32 flags)
1342 {
1343 	int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
1344 
1345 	if (lat)
1346 		print_lat_header(s, flags);
1347 
1348 	/* 1st line */
1349 	seq_putc(s, '#');
1350 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1351 		seq_puts(s, "     TIME       ");
1352 	if (flags & TRACE_GRAPH_PRINT_CPU)
1353 		seq_puts(s, " CPU");
1354 	if (flags & TRACE_GRAPH_PRINT_PROC)
1355 		seq_puts(s, "  TASK/PID       ");
1356 	if (lat)
1357 		seq_puts(s, "||||");
1358 	if (flags & TRACE_GRAPH_PRINT_DURATION)
1359 		seq_puts(s, "  DURATION   ");
1360 	seq_puts(s, "               FUNCTION CALLS\n");
1361 
1362 	/* 2nd line */
1363 	seq_putc(s, '#');
1364 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1365 		seq_puts(s, "      |         ");
1366 	if (flags & TRACE_GRAPH_PRINT_CPU)
1367 		seq_puts(s, " |  ");
1368 	if (flags & TRACE_GRAPH_PRINT_PROC)
1369 		seq_puts(s, "   |    |        ");
1370 	if (lat)
1371 		seq_puts(s, "||||");
1372 	if (flags & TRACE_GRAPH_PRINT_DURATION)
1373 		seq_puts(s, "   |   |      ");
1374 	seq_puts(s, "               |   |   |   |\n");
1375 }
1376 
print_graph_headers(struct seq_file * s)1377 static void print_graph_headers(struct seq_file *s)
1378 {
1379 	print_graph_headers_flags(s, tracer_flags.val);
1380 }
1381 
print_graph_headers_flags(struct seq_file * s,u32 flags)1382 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1383 {
1384 	struct trace_iterator *iter = s->private;
1385 	struct trace_array *tr = iter->tr;
1386 
1387 	if (flags & TRACE_GRAPH_PRINT_FLAT) {
1388 		trace_default_header(s);
1389 		return;
1390 	}
1391 
1392 	if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
1393 		return;
1394 
1395 	if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
1396 		/* print nothing if the buffers are empty */
1397 		if (trace_empty(iter))
1398 			return;
1399 
1400 		print_trace_header(s, iter);
1401 	}
1402 
1403 	__print_graph_headers_flags(tr, s, flags);
1404 }
1405 
graph_trace_open(struct trace_iterator * iter)1406 void graph_trace_open(struct trace_iterator *iter)
1407 {
1408 	/* pid and depth on the last trace processed */
1409 	struct fgraph_data *data;
1410 	gfp_t gfpflags;
1411 	int cpu;
1412 
1413 	iter->private = NULL;
1414 
1415 	/* We can be called in atomic context via ftrace_dump() */
1416 	gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
1417 
1418 	data = kzalloc(sizeof(*data), gfpflags);
1419 	if (!data)
1420 		goto out_err;
1421 
1422 	data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
1423 	if (!data->cpu_data)
1424 		goto out_err_free;
1425 
1426 	for_each_possible_cpu(cpu) {
1427 		pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1428 		int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1429 		int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1430 		int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1431 
1432 		*pid = -1;
1433 		*depth = 0;
1434 		*ignore = 0;
1435 		*depth_irq = -1;
1436 	}
1437 
1438 	iter->private = data;
1439 
1440 	return;
1441 
1442  out_err_free:
1443 	kfree(data);
1444  out_err:
1445 	pr_warn("function graph tracer: not enough memory\n");
1446 }
1447 
graph_trace_close(struct trace_iterator * iter)1448 void graph_trace_close(struct trace_iterator *iter)
1449 {
1450 	struct fgraph_data *data = iter->private;
1451 
1452 	if (data) {
1453 		free_percpu(data->cpu_data);
1454 		kfree(data);
1455 	}
1456 }
1457 
1458 static int
func_graph_set_flag(struct trace_array * tr,u32 old_flags,u32 bit,int set)1459 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1460 {
1461 	if (bit == TRACE_GRAPH_PRINT_IRQS)
1462 		ftrace_graph_skip_irqs = !set;
1463 
1464 	if (bit == TRACE_GRAPH_SLEEP_TIME)
1465 		ftrace_graph_sleep_time_control(set);
1466 
1467 	if (bit == TRACE_GRAPH_GRAPH_TIME)
1468 		ftrace_graph_graph_time_control(set);
1469 
1470 	return 0;
1471 }
1472 
1473 
1474 static struct tracer graph_trace __tracer_data = {
1475 	.name		= "function_graph",
1476 	.update_thresh	= graph_trace_update_thresh,
1477 	.open		= graph_trace_open,
1478 	.pipe_open	= graph_trace_open,
1479 	.close		= graph_trace_close,
1480 	.pipe_close	= graph_trace_close,
1481 	.init		= graph_trace_init,
1482 	.reset		= graph_trace_reset,
1483 	.print_line	= print_graph_function,
1484 	.print_header	= print_graph_headers,
1485 	.flags		= &tracer_flags,
1486 	.set_flag	= func_graph_set_flag,
1487 #ifdef CONFIG_FTRACE_SELFTEST
1488 	.selftest	= trace_selftest_startup_function_graph,
1489 #endif
1490 };
1491 
1492 
1493 static ssize_t
graph_depth_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1494 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1495 		  loff_t *ppos)
1496 {
1497 	unsigned long val;
1498 	int ret;
1499 
1500 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1501 	if (ret)
1502 		return ret;
1503 
1504 	fgraph_max_depth = val;
1505 
1506 	*ppos += cnt;
1507 
1508 	return cnt;
1509 }
1510 
1511 static ssize_t
graph_depth_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1512 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1513 		 loff_t *ppos)
1514 {
1515 	char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1516 	int n;
1517 
1518 	n = sprintf(buf, "%d\n", fgraph_max_depth);
1519 
1520 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1521 }
1522 
1523 static const struct file_operations graph_depth_fops = {
1524 	.open		= tracing_open_generic,
1525 	.write		= graph_depth_write,
1526 	.read		= graph_depth_read,
1527 	.llseek		= generic_file_llseek,
1528 };
1529 
init_graph_tracefs(void)1530 static __init int init_graph_tracefs(void)
1531 {
1532 	struct dentry *d_tracer;
1533 
1534 	d_tracer = tracing_init_dentry();
1535 	if (IS_ERR(d_tracer))
1536 		return 0;
1537 
1538 	trace_create_file("max_graph_depth", 0644, d_tracer,
1539 			  NULL, &graph_depth_fops);
1540 
1541 	return 0;
1542 }
1543 fs_initcall(init_graph_tracefs);
1544 
init_graph_trace(void)1545 static __init int init_graph_trace(void)
1546 {
1547 	max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1);
1548 
1549 	return register_tracer(&graph_trace);
1550 }
1551 
1552 core_initcall(init_graph_trace);
1553