Commit | Line | Data |
---|---|---|
1e9b51c2 | 1 | /* |
a93751ca | 2 | * h/w branch tracer for x86 based on bts |
1e9b51c2 MM |
3 | * |
4 | * Copyright (C) 2008 Markus Metzger <markus.t.metzger@gmail.com> | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/fs.h> | |
10 | #include <linux/debugfs.h> | |
11 | #include <linux/ftrace.h> | |
12 | #include <linux/kallsyms.h> | |
13 | ||
14 | #include <asm/ds.h> | |
15 | ||
16 | #include "trace.h" | |
f0868d1e | 17 | #include "trace_output.h" |
1e9b51c2 MM |
18 | |
19 | ||
20 | #define SIZEOF_BTS (1 << 13) | |
21 | ||
22 | static DEFINE_PER_CPU(struct bts_tracer *, tracer); | |
23 | static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer); | |
24 | ||
25 | #define this_tracer per_cpu(tracer, smp_processor_id()) | |
26 | #define this_buffer per_cpu(buffer, smp_processor_id()) | |
27 | ||
28 | ||
1e9b51c2 MM |
29 | static void bts_trace_start_cpu(void *arg) |
30 | { | |
a93751ca MM |
31 | if (this_tracer) |
32 | ds_release_bts(this_tracer); | |
33 | ||
1e9b51c2 MM |
34 | this_tracer = |
35 | ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS, | |
a93751ca MM |
36 | /* ovfl = */ NULL, /* th = */ (size_t)-1, |
37 | BTS_KERNEL); | |
1e9b51c2 MM |
38 | if (IS_ERR(this_tracer)) { |
39 | this_tracer = NULL; | |
40 | return; | |
41 | } | |
1e9b51c2 MM |
42 | } |
43 | ||
44 | static void bts_trace_start(struct trace_array *tr) | |
45 | { | |
46 | int cpu; | |
47 | ||
213cc060 | 48 | tracing_reset_online_cpus(tr); |
1e9b51c2 | 49 | |
4462344e | 50 | for_each_cpu(cpu, cpu_possible_mask) |
1e9b51c2 MM |
51 | smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1); |
52 | } | |
53 | ||
54 | static void bts_trace_stop_cpu(void *arg) | |
55 | { | |
56 | if (this_tracer) { | |
1e9b51c2 MM |
57 | ds_release_bts(this_tracer); |
58 | this_tracer = NULL; | |
59 | } | |
60 | } | |
61 | ||
62 | static void bts_trace_stop(struct trace_array *tr) | |
63 | { | |
64 | int cpu; | |
65 | ||
4462344e | 66 | for_each_cpu(cpu, cpu_possible_mask) |
1e9b51c2 MM |
67 | smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1); |
68 | } | |
69 | ||
70 | static int bts_trace_init(struct trace_array *tr) | |
71 | { | |
213cc060 | 72 | tracing_reset_online_cpus(tr); |
1e9b51c2 MM |
73 | bts_trace_start(tr); |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | static void bts_trace_print_header(struct seq_file *m) | |
79 | { | |
1e9b51c2 MM |
80 | seq_puts(m, |
81 | "# CPU# FROM TO FUNCTION\n"); | |
82 | seq_puts(m, | |
83 | "# | | | |\n"); | |
1e9b51c2 MM |
84 | } |
85 | ||
86 | static enum print_line_t bts_trace_print_line(struct trace_iterator *iter) | |
87 | { | |
88 | struct trace_entry *entry = iter->ent; | |
89 | struct trace_seq *seq = &iter->seq; | |
a93751ca | 90 | struct hw_branch_entry *it; |
1e9b51c2 MM |
91 | |
92 | trace_assign_type(it, entry); | |
93 | ||
a93751ca MM |
94 | if (entry->type == TRACE_HW_BRANCHES) { |
95 | if (trace_seq_printf(seq, "%4d ", entry->cpu) && | |
96 | trace_seq_printf(seq, "0x%016llx -> 0x%016llx ", | |
97 | it->from, it->to) && | |
98 | (!it->from || | |
99 | seq_print_ip_sym(seq, it->from, /* sym_flags = */ 0)) && | |
100 | trace_seq_printf(seq, "\n")) | |
101 | return TRACE_TYPE_HANDLED; | |
102 | return TRACE_TYPE_PARTIAL_LINE;; | |
1e9b51c2 MM |
103 | } |
104 | return TRACE_TYPE_UNHANDLED; | |
105 | } | |
106 | ||
a93751ca | 107 | void trace_hw_branch(struct trace_array *tr, u64 from, u64 to) |
1e9b51c2 MM |
108 | { |
109 | struct ring_buffer_event *event; | |
a93751ca | 110 | struct hw_branch_entry *entry; |
1e9b51c2 MM |
111 | unsigned long irq; |
112 | ||
113 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq); | |
114 | if (!event) | |
115 | return; | |
116 | entry = ring_buffer_event_data(event); | |
117 | tracing_generic_entry_update(&entry->ent, 0, from); | |
a93751ca | 118 | entry->ent.type = TRACE_HW_BRANCHES; |
1e9b51c2 MM |
119 | entry->ent.cpu = smp_processor_id(); |
120 | entry->from = from; | |
121 | entry->to = to; | |
122 | ring_buffer_unlock_commit(tr->buffer, event, irq); | |
123 | } | |
124 | ||
a93751ca MM |
125 | static void trace_bts_at(struct trace_array *tr, |
126 | const struct bts_trace *trace, void *at) | |
1e9b51c2 | 127 | { |
a93751ca MM |
128 | struct bts_struct bts; |
129 | int err = 0; | |
1e9b51c2 | 130 | |
a93751ca MM |
131 | WARN_ON_ONCE(!trace->read); |
132 | if (!trace->read) | |
1e9b51c2 MM |
133 | return; |
134 | ||
a93751ca MM |
135 | err = trace->read(this_tracer, at, &bts); |
136 | if (err < 0) | |
137 | return; | |
1e9b51c2 | 138 | |
a93751ca MM |
139 | switch (bts.qualifier) { |
140 | case BTS_BRANCH: | |
141 | trace_hw_branch(tr, bts.variant.lbr.from, bts.variant.lbr.to); | |
142 | break; | |
143 | } | |
1e9b51c2 MM |
144 | } |
145 | ||
146 | static void trace_bts_cpu(void *arg) | |
147 | { | |
148 | struct trace_array *tr = (struct trace_array *) arg; | |
a93751ca MM |
149 | const struct bts_trace *trace; |
150 | unsigned char *at; | |
1e9b51c2 MM |
151 | |
152 | if (!this_tracer) | |
153 | return; | |
154 | ||
a93751ca MM |
155 | ds_suspend_bts(this_tracer); |
156 | trace = ds_read_bts(this_tracer); | |
157 | if (!trace) | |
1e9b51c2 MM |
158 | goto out; |
159 | ||
a93751ca MM |
160 | for (at = trace->ds.top; (void *)at < trace->ds.end; |
161 | at += trace->ds.size) | |
162 | trace_bts_at(tr, trace, at); | |
1e9b51c2 | 163 | |
a93751ca MM |
164 | for (at = trace->ds.begin; (void *)at < trace->ds.top; |
165 | at += trace->ds.size) | |
166 | trace_bts_at(tr, trace, at); | |
1e9b51c2 MM |
167 | |
168 | out: | |
a93751ca | 169 | ds_resume_bts(this_tracer); |
1e9b51c2 MM |
170 | } |
171 | ||
172 | static void trace_bts_prepare(struct trace_iterator *iter) | |
173 | { | |
174 | int cpu; | |
175 | ||
4462344e | 176 | for_each_cpu(cpu, cpu_possible_mask) |
1e9b51c2 MM |
177 | smp_call_function_single(cpu, trace_bts_cpu, iter->tr, 1); |
178 | } | |
179 | ||
180 | struct tracer bts_tracer __read_mostly = | |
181 | { | |
a93751ca | 182 | .name = "hw-branch-tracer", |
1e9b51c2 MM |
183 | .init = bts_trace_init, |
184 | .reset = bts_trace_stop, | |
185 | .print_header = bts_trace_print_header, | |
186 | .print_line = bts_trace_print_line, | |
187 | .start = bts_trace_start, | |
188 | .stop = bts_trace_stop, | |
189 | .open = trace_bts_prepare | |
190 | }; | |
191 | ||
192 | __init static int init_bts_trace(void) | |
193 | { | |
194 | return register_tracer(&bts_tracer); | |
195 | } | |
196 | device_initcall(init_bts_trace); |