trace_functions_graph.c 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *
 * Function graph tracer.
 * Copyright (c) 2008 Frederic Weisbecker <fweisbec@gmail.com>
 * Mostly borrowed from function tracer which
 * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
 *
 */
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/ftrace.h>
#include <linux/fs.h>

#include "trace.h"

16
#define TRACE_GRAPH_INDENT	2
17 18 19 20 21 22 23 24 25 26 27 28 29

#define TRACE_GRAPH_PRINT_OVERRUN	0x1
static struct tracer_opt trace_opts[] = {
	/* Display overruns or not */
	{ TRACER_OPT(overrun, TRACE_GRAPH_PRINT_OVERRUN) },
	{ } /* Empty entry */
};

static struct tracer_flags tracer_flags = {
	.val = 0, /* Don't display overruns by default */
	.opts = trace_opts
};

30
/* pid on the last trace processed */
31
static pid_t last_pid[NR_CPUS] = { [0 ... NR_CPUS-1] = -1 };
32 33 34

static int graph_trace_init(struct trace_array *tr)
{
35 36
	int cpu, ret;

37 38 39
	for_each_online_cpu(cpu)
		tracing_reset(tr, cpu);

40
	ret = register_ftrace_graph(&trace_graph_return,
41
					&trace_graph_entry);
42 43 44 45 46
	if (ret)
		return ret;
	tracing_start_cmdline_record();

	return 0;
47 48 49 50
}

static void graph_trace_reset(struct trace_array *tr)
{
51 52
	tracing_stop_cmdline_record();
	unregister_ftrace_graph();
53 54
}

55
/* If the pid changed since the last trace, output this event */
56
static int verif_pid(struct trace_seq *s, pid_t pid, int cpu)
57
{
58 59
	char *comm;

60
	if (last_pid[cpu] != -1 && last_pid[cpu] == pid)
61
		return 1;
62

63
	last_pid[cpu] = pid;
64 65
	comm = trace_find_cmdline(pid);

66 67
	return trace_seq_printf(s, "\nCPU[%03d]"
				    " ------------8<---------- thread %s-%d"
68
				    " ------------8<----------\n\n",
69
				    cpu, comm, pid);
70 71 72 73
}

static enum print_line_t
print_graph_entry(struct ftrace_graph_ent *call, struct trace_seq *s,
74
		  struct trace_entry *ent, int cpu)
75
{
76
	int i;
77 78
	int ret;

79 80 81 82 83
	if (!verif_pid(s, ent->pid, cpu))
		return TRACE_TYPE_PARTIAL_LINE;

	ret = trace_seq_printf(s, "CPU[%03d] ", cpu);
	if (!ret)
84
		return TRACE_TYPE_PARTIAL_LINE;
85

86 87
	for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
		ret = trace_seq_printf(s, " ");
88 89
		if (!ret)
			return TRACE_TYPE_PARTIAL_LINE;
90 91 92 93 94 95 96 97 98 99 100 101 102 103
	}

	ret = seq_print_ip_sym(s, call->func, 0);
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

	ret = trace_seq_printf(s, "() {\n");
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;
	return TRACE_TYPE_HANDLED;
}

static enum print_line_t
print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
104
		   struct trace_entry *ent, int cpu)
105 106 107 108
{
	int i;
	int ret;

109 110 111 112 113
	if (!verif_pid(s, ent->pid, cpu))
		return TRACE_TYPE_PARTIAL_LINE;

	ret = trace_seq_printf(s, "CPU[%03d] ", cpu);
	if (!ret)
114
		return TRACE_TYPE_PARTIAL_LINE;
115

116 117
	for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
		ret = trace_seq_printf(s, " ");
118 119
		if (!ret)
			return TRACE_TYPE_PARTIAL_LINE;
120 121 122 123 124
	}

	ret = trace_seq_printf(s, "} ");
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;
125

126 127 128
	ret = trace_seq_printf(s, "%llu\n", trace->rettime - trace->calltime);
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;
129

130 131 132
	if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
		ret = trace_seq_printf(s, " (Overruns: %lu)\n",
					trace->overrun);
133 134
		if (!ret)
			return TRACE_TYPE_PARTIAL_LINE;
135 136 137 138 139 140 141 142 143
	}
	return TRACE_TYPE_HANDLED;
}

enum print_line_t
print_graph_function(struct trace_iterator *iter)
{
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
144

145 146 147 148
	switch (entry->type) {
	case TRACE_GRAPH_ENT: {
		struct ftrace_graph_ent_entry *field;
		trace_assign_type(field, entry);
149 150
		return print_graph_entry(&field->graph_ent, s, entry,
					 iter->cpu);
151 152 153 154
	}
	case TRACE_GRAPH_RET: {
		struct ftrace_graph_ret_entry *field;
		trace_assign_type(field, entry);
155
		return print_graph_return(&field->ret, s, entry, iter->cpu);
156 157 158
	}
	default:
		return TRACE_TYPE_UNHANDLED;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	}
}

static struct tracer graph_trace __read_mostly = {
	.name	     = "function-graph",
	.init	     = graph_trace_init,
	.reset	     = graph_trace_reset,
	.print_line = print_graph_function,
	.flags		= &tracer_flags,
};

static __init int init_graph_trace(void)
{
	return register_tracer(&graph_trace);
}

device_initcall(init_graph_trace);