1367 lines · 32.5 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * * Function graph tracer. * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com> * Mostly borrowed from function tracer which * is Copyright (c) Steven Rostedt <srostedt@redhat.com> * */#include <linux/uaccess.h>#include <linux/ftrace.h>#include <linux/interrupt.h>#include <linux/slab.h>#include <linux/fs.h> #include "trace.h"#include "trace_output.h" /* When set, irq functions will be ignored */static int ftrace_graph_skip_irqs; struct fgraph_cpu_data {	pid_t		last_pid;	int		depth;	int		depth_irq;	int		ignore;	unsigned long	enter_funcs[FTRACE_RETFUNC_DEPTH];}; struct fgraph_data {	struct fgraph_cpu_data __percpu *cpu_data; 	/* Place to preserve last processed entry. */	struct ftrace_graph_ent_entry	ent;	struct ftrace_graph_ret_entry	ret;	int				failed;	int				cpu;}; #define TRACE_GRAPH_INDENT	2 unsigned int fgraph_max_depth; static struct tracer_opt trace_opts[] = {	/* Display overruns? (for self-debug purpose) */	{ TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },	/* Display CPU ? */	{ TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },	/* Display Overhead ? */	{ TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },	/* Display proc name/pid */	{ TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },	/* Display duration of execution */	{ TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },	/* Display absolute time of an entry */	{ TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },	/* Display interrupts */	{ TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },	/* Display function name after trailing } */	{ TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },	/* Include sleep time (scheduled out) between entry and return */	{ TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) }, #ifdef CONFIG_FUNCTION_PROFILER	/* Include time within nested functions */	{ TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) },#endif 	{ } /* Empty entry */}; static struct tracer_flags tracer_flags = {	/* Don't display overruns, proc, or tail by default */	.val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |	       TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS |	       TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME,	.opts = trace_opts}; static struct trace_array *graph_array; /* * DURATION column is being also used to display IRQ signs, * following values are used by print_graph_irq and others * to fill in space into DURATION column. */enum {	FLAGS_FILL_FULL  = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,	FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,	FLAGS_FILL_END   = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,}; static voidprint_graph_duration(struct trace_array *tr, unsigned long long duration,		     struct trace_seq *s, u32 flags); int __trace_graph_entry(struct trace_array *tr,				struct ftrace_graph_ent *trace,				unsigned int trace_ctx){	struct trace_event_call *call = &event_funcgraph_entry;	struct ring_buffer_event *event;	struct trace_buffer *buffer = tr->array_buffer.buffer;	struct ftrace_graph_ent_entry *entry; 	event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,					  sizeof(*entry), trace_ctx);	if (!event)		return 0;	entry	= ring_buffer_event_data(event);	entry->graph_ent			= *trace;	if (!call_filter_check_discard(call, entry, buffer, event))		trace_buffer_unlock_commit_nostack(buffer, event); 	return 1;} static inline int ftrace_graph_ignore_irqs(void){	if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))		return 0; 	return in_irq();} int trace_graph_entry(struct ftrace_graph_ent *trace){	struct trace_array *tr = graph_array;	struct trace_array_cpu *data;	unsigned long flags;	unsigned int trace_ctx;	long disabled;	int ret;	int cpu; 	if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT))		return 0; 	/*	 * Do not trace a function if it's filtered by set_graph_notrace.	 * Make the index of ret stack negative to indicate that it should	 * ignore further functions.  But it needs its own ret stack entry	 * to recover the original index in order to continue tracing after	 * returning from the function.	 */	if (ftrace_graph_notrace_addr(trace->func)) {		trace_recursion_set(TRACE_GRAPH_NOTRACE_BIT);		/*		 * Need to return 1 to have the return called		 * that will clear the NOTRACE bit.		 */		return 1;	} 	if (!ftrace_trace_task(tr))		return 0; 	if (ftrace_graph_ignore_func(trace))		return 0; 	if (ftrace_graph_ignore_irqs())		return 0; 	/*	 * Stop here if tracing_threshold is set. We only write function return	 * events to the ring buffer.	 */	if (tracing_thresh)		return 1; 	local_irq_save(flags);	cpu = raw_smp_processor_id();	data = per_cpu_ptr(tr->array_buffer.data, cpu);	disabled = atomic_inc_return(&data->disabled);	if (likely(disabled == 1)) {		trace_ctx = tracing_gen_ctx_flags(flags);		ret = __trace_graph_entry(tr, trace, trace_ctx);	} else {		ret = 0;	} 	atomic_dec(&data->disabled);	local_irq_restore(flags); 	return ret;} static void__trace_graph_function(struct trace_array *tr,		unsigned long ip, unsigned int trace_ctx){	u64 time = trace_clock_local();	struct ftrace_graph_ent ent = {		.func  = ip,		.depth = 0,	};	struct ftrace_graph_ret ret = {		.func     = ip,		.depth    = 0,		.calltime = time,		.rettime  = time,	}; 	__trace_graph_entry(tr, &ent, trace_ctx);	__trace_graph_return(tr, &ret, trace_ctx);} voidtrace_graph_function(struct trace_array *tr,		unsigned long ip, unsigned long parent_ip,		unsigned int trace_ctx){	__trace_graph_function(tr, ip, trace_ctx);} void __trace_graph_return(struct trace_array *tr,				struct ftrace_graph_ret *trace,				unsigned int trace_ctx){	struct trace_event_call *call = &event_funcgraph_exit;	struct ring_buffer_event *event;	struct trace_buffer *buffer = tr->array_buffer.buffer;	struct ftrace_graph_ret_entry *entry; 	event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,					  sizeof(*entry), trace_ctx);	if (!event)		return;	entry	= ring_buffer_event_data(event);	entry->ret				= *trace;	if (!call_filter_check_discard(call, entry, buffer, event))		trace_buffer_unlock_commit_nostack(buffer, event);} void trace_graph_return(struct ftrace_graph_ret *trace){	struct trace_array *tr = graph_array;	struct trace_array_cpu *data;	unsigned long flags;	unsigned int trace_ctx;	long disabled;	int cpu; 	ftrace_graph_addr_finish(trace); 	if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {		trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);		return;	} 	local_irq_save(flags);	cpu = raw_smp_processor_id();	data = per_cpu_ptr(tr->array_buffer.data, cpu);	disabled = atomic_inc_return(&data->disabled);	if (likely(disabled == 1)) {		trace_ctx = tracing_gen_ctx_flags(flags);		__trace_graph_return(tr, trace, trace_ctx);	}	atomic_dec(&data->disabled);	local_irq_restore(flags);} void set_graph_array(struct trace_array *tr){	graph_array = tr; 	/* Make graph_array visible before we start tracing */ 	smp_mb();} static void trace_graph_thresh_return(struct ftrace_graph_ret *trace){	ftrace_graph_addr_finish(trace); 	if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {		trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);		return;	} 	if (tracing_thresh &&	    (trace->rettime - trace->calltime < tracing_thresh))		return;	else		trace_graph_return(trace);} static struct fgraph_ops funcgraph_thresh_ops = {	.entryfunc = &trace_graph_entry,	.retfunc = &trace_graph_thresh_return,}; static struct fgraph_ops funcgraph_ops = {	.entryfunc = &trace_graph_entry,	.retfunc = &trace_graph_return,}; static int graph_trace_init(struct trace_array *tr){	int ret; 	set_graph_array(tr);	if (tracing_thresh)		ret = register_ftrace_graph(&funcgraph_thresh_ops);	else		ret = register_ftrace_graph(&funcgraph_ops);	if (ret)		return ret;	tracing_start_cmdline_record(); 	return 0;} static void graph_trace_reset(struct trace_array *tr){	tracing_stop_cmdline_record();	if (tracing_thresh)		unregister_ftrace_graph(&funcgraph_thresh_ops);	else		unregister_ftrace_graph(&funcgraph_ops);} static int graph_trace_update_thresh(struct trace_array *tr){	graph_trace_reset(tr);	return graph_trace_init(tr);} static int max_bytes_for_cpu; static void print_graph_cpu(struct trace_seq *s, int cpu){	/*	 * Start with a space character - to make it stand out	 * to the right a bit when trace output is pasted into	 * email:	 */	trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);} #define TRACE_GRAPH_PROCINFO_LENGTH	14 static void print_graph_proc(struct trace_seq *s, pid_t pid){	char comm[TASK_COMM_LEN];	/* sign + log10(MAX_INT) + '\0' */	char pid_str[11];	int spaces = 0;	int len;	int i; 	trace_find_cmdline(pid, comm);	comm[7] = '\0';	sprintf(pid_str, "%d", pid); 	/* 1 stands for the "-" character */	len = strlen(comm) + strlen(pid_str) + 1; 	if (len < TRACE_GRAPH_PROCINFO_LENGTH)		spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; 	/* First spaces to align center */	for (i = 0; i < spaces / 2; i++)		trace_seq_putc(s, ' '); 	trace_seq_printf(s, "%s-%s", comm, pid_str); 	/* Last spaces to align center */	for (i = 0; i < spaces - (spaces / 2); i++)		trace_seq_putc(s, ' ');}  static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry){	trace_seq_putc(s, ' ');	trace_print_lat_fmt(s, entry);	trace_seq_puts(s, " | ");} /* If the pid changed since the last trace, output this event */static voidverif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data){	pid_t prev_pid;	pid_t *last_pid; 	if (!data)		return; 	last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid); 	if (*last_pid == pid)		return; 	prev_pid = *last_pid;	*last_pid = pid; 	if (prev_pid == -1)		return;/* * Context-switch trace line:  ------------------------------------------ | 1)  migration/0--1  =>  sshd-1755 ------------------------------------------  */	trace_seq_puts(s, " ------------------------------------------\n");	print_graph_cpu(s, cpu);	print_graph_proc(s, prev_pid);	trace_seq_puts(s, " => ");	print_graph_proc(s, pid);	trace_seq_puts(s, "\n ------------------------------------------\n\n");} static struct ftrace_graph_ret_entry *get_return_for_leaf(struct trace_iterator *iter,		struct ftrace_graph_ent_entry *curr){	struct fgraph_data *data = iter->private;	struct ring_buffer_iter *ring_iter = NULL;	struct ring_buffer_event *event;	struct ftrace_graph_ret_entry *next; 	/*	 * If the previous output failed to write to the seq buffer,	 * then we just reuse the data from before.	 */	if (data && data->failed) {		curr = &data->ent;		next = &data->ret;	} else { 		ring_iter = trace_buffer_iter(iter, iter->cpu); 		/* First peek to compare current entry and the next one */		if (ring_iter)			event = ring_buffer_iter_peek(ring_iter, NULL);		else {			/*			 * We need to consume the current entry to see			 * the next one.			 */			ring_buffer_consume(iter->array_buffer->buffer, iter->cpu,					    NULL, NULL);			event = ring_buffer_peek(iter->array_buffer->buffer, iter->cpu,						 NULL, NULL);		} 		if (!event)			return NULL; 		next = ring_buffer_event_data(event); 		if (data) {			/*			 * Save current and next entries for later reference			 * if the output fails.			 */			data->ent = *curr;			/*			 * If the next event is not a return type, then			 * we only care about what type it is. Otherwise we can			 * safely copy the entire event.			 */			if (next->ent.type == TRACE_GRAPH_RET)				data->ret = *next;			else				data->ret.ent.type = next->ent.type;		}	} 	if (next->ent.type != TRACE_GRAPH_RET)		return NULL; 	if (curr->ent.pid != next->ent.pid ||			curr->graph_ent.func != next->ret.func)		return NULL; 	/* this is a leaf, now advance the iterator */	if (ring_iter)		ring_buffer_iter_advance(ring_iter); 	return next;} static void print_graph_abs_time(u64 t, struct trace_seq *s){	unsigned long usecs_rem; 	usecs_rem = do_div(t, NSEC_PER_SEC);	usecs_rem /= 1000; 	trace_seq_printf(s, "%5lu.%06lu |  ",			 (unsigned long)t, usecs_rem);} static voidprint_graph_rel_time(struct trace_iterator *iter, struct trace_seq *s)

Lines 1–500 of 1367.