x86, ftrace, hw-branch-tracer: dump trace on oops
[safe/jmp/linux-2.6] / kernel / trace / trace_hw_branches.c
1 /*
2  * h/w branch tracer for x86 based on bts
3  *
4  * Copyright (C) 2008-2009 Intel Corporation.
5  * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
6  *
7  */
8
9 #include <linux/module.h>
10 #include <linux/fs.h>
11 #include <linux/debugfs.h>
12 #include <linux/ftrace.h>
13 #include <linux/kallsyms.h>
14 #include <linux/mutex.h>
15 #include <linux/cpu.h>
16 #include <linux/smp.h>
17
18 #include <asm/ds.h>
19
20 #include "trace.h"
21 #include "trace_output.h"
22
23
24 #define SIZEOF_BTS (1 << 13)
25
26 /* The tracer mutex protects the below per-cpu tracer array.
27    It needs to be held to:
28    - start tracing on all cpus
29    - stop tracing on all cpus
30    - start tracing on a single hotplug cpu
31    - stop tracing on a single hotplug cpu
32    - read the trace from all cpus
33    - read the trace from a single cpu
34 */
35 static DEFINE_MUTEX(bts_tracer_mutex);
36 static DEFINE_PER_CPU(struct bts_tracer *, tracer);
37 static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
38
39 #define this_tracer per_cpu(tracer, smp_processor_id())
40 #define this_buffer per_cpu(buffer, smp_processor_id())
41
42 static int __read_mostly trace_hw_branches_enabled;
43 static struct trace_array *hw_branch_trace __read_mostly;
44
45
46 /*
47  * Start tracing on the current cpu.
48  * The argument is ignored.
49  *
50  * pre: bts_tracer_mutex must be locked.
51  */
52 static void bts_trace_start_cpu(void *arg)
53 {
54         if (this_tracer)
55                 ds_release_bts(this_tracer);
56
57         this_tracer =
58                 ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
59                                /* ovfl = */ NULL, /* th = */ (size_t)-1,
60                                BTS_KERNEL);
61         if (IS_ERR(this_tracer)) {
62                 this_tracer = NULL;
63                 return;
64         }
65 }
66
67 static void bts_trace_start(struct trace_array *tr)
68 {
69         mutex_lock(&bts_tracer_mutex);
70
71         on_each_cpu(bts_trace_start_cpu, NULL, 1);
72         trace_hw_branches_enabled = 1;
73
74         mutex_unlock(&bts_tracer_mutex);
75 }
76
77 /*
78  * Start tracing on the current cpu.
79  * The argument is ignored.
80  *
81  * pre: bts_tracer_mutex must be locked.
82  */
83 static void bts_trace_stop_cpu(void *arg)
84 {
85         if (this_tracer) {
86                 ds_release_bts(this_tracer);
87                 this_tracer = NULL;
88         }
89 }
90
91 static void bts_trace_stop(struct trace_array *tr)
92 {
93         mutex_lock(&bts_tracer_mutex);
94
95         trace_hw_branches_enabled = 0;
96         on_each_cpu(bts_trace_stop_cpu, NULL, 1);
97
98         mutex_unlock(&bts_tracer_mutex);
99 }
100
101 static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
102                                      unsigned long action, void *hcpu)
103 {
104         unsigned int cpu = (unsigned long)hcpu;
105
106         mutex_lock(&bts_tracer_mutex);
107
108         if (!trace_hw_branches_enabled)
109                 goto out;
110
111         switch (action) {
112         case CPU_ONLINE:
113         case CPU_DOWN_FAILED:
114                 smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
115                 break;
116         case CPU_DOWN_PREPARE:
117                 smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
118                 break;
119         }
120
121  out:
122         mutex_unlock(&bts_tracer_mutex);
123         return NOTIFY_DONE;
124 }
125
126 static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
127         .notifier_call = bts_hotcpu_handler
128 };
129
130 static int bts_trace_init(struct trace_array *tr)
131 {
132         hw_branch_trace = tr;
133
134         register_hotcpu_notifier(&bts_hotcpu_notifier);
135         tracing_reset_online_cpus(tr);
136         bts_trace_start(tr);
137
138         return 0;
139 }
140
141 static void bts_trace_reset(struct trace_array *tr)
142 {
143         bts_trace_stop(tr);
144         unregister_hotcpu_notifier(&bts_hotcpu_notifier);
145 }
146
147 static void bts_trace_print_header(struct seq_file *m)
148 {
149         seq_puts(m,
150                  "# CPU#        FROM                   TO         FUNCTION\n");
151         seq_puts(m,
152                  "#  |           |                     |             |\n");
153 }
154
155 static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
156 {
157         struct trace_entry *entry = iter->ent;
158         struct trace_seq *seq = &iter->seq;
159         struct hw_branch_entry *it;
160
161         trace_assign_type(it, entry);
162
163         if (entry->type == TRACE_HW_BRANCHES) {
164                 if (trace_seq_printf(seq, "%4d  ", entry->cpu) &&
165                     trace_seq_printf(seq, "0x%016llx -> 0x%016llx ",
166                                      it->from, it->to) &&
167                     (!it->from ||
168                      seq_print_ip_sym(seq, it->from, /* sym_flags = */ 0)) &&
169                     trace_seq_printf(seq, "\n"))
170                         return TRACE_TYPE_HANDLED;
171                 return TRACE_TYPE_PARTIAL_LINE;;
172         }
173         return TRACE_TYPE_UNHANDLED;
174 }
175
176 void trace_hw_branch(u64 from, u64 to)
177 {
178         struct trace_array *tr = hw_branch_trace;
179         struct ring_buffer_event *event;
180         struct hw_branch_entry *entry;
181         unsigned long irq1, irq2;
182         int cpu;
183
184         if (unlikely(!tr))
185                 return;
186
187         if (unlikely(!trace_hw_branches_enabled))
188                 return;
189
190         local_irq_save(irq1);
191         cpu = raw_smp_processor_id();
192         if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
193                 goto out;
194
195         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq2);
196         if (!event)
197                 goto out;
198         entry   = ring_buffer_event_data(event);
199         tracing_generic_entry_update(&entry->ent, 0, from);
200         entry->ent.type = TRACE_HW_BRANCHES;
201         entry->ent.cpu = cpu;
202         entry->from = from;
203         entry->to   = to;
204         ring_buffer_unlock_commit(tr->buffer, event, irq2);
205
206  out:
207         atomic_dec(&tr->data[cpu]->disabled);
208         local_irq_restore(irq1);
209 }
210
211 static void trace_bts_at(const struct bts_trace *trace, void *at)
212 {
213         struct bts_struct bts;
214         int err = 0;
215
216         WARN_ON_ONCE(!trace->read);
217         if (!trace->read)
218                 return;
219
220         err = trace->read(this_tracer, at, &bts);
221         if (err < 0)
222                 return;
223
224         switch (bts.qualifier) {
225         case BTS_BRANCH:
226                 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
227                 break;
228         }
229 }
230
231 /*
232  * Collect the trace on the current cpu and write it into the ftrace buffer.
233  *
234  * pre: bts_tracer_mutex must be locked
235  */
236 static void trace_bts_cpu(void *arg)
237 {
238         struct trace_array *tr = (struct trace_array *) arg;
239         const struct bts_trace *trace;
240         unsigned char *at;
241
242         if (unlikely(!tr))
243                 return;
244
245         if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
246                 return;
247
248         if (unlikely(!this_tracer))
249                 return;
250
251         ds_suspend_bts(this_tracer);
252         trace = ds_read_bts(this_tracer);
253         if (!trace)
254                 goto out;
255
256         for (at = trace->ds.top; (void *)at < trace->ds.end;
257              at += trace->ds.size)
258                 trace_bts_at(trace, at);
259
260         for (at = trace->ds.begin; (void *)at < trace->ds.top;
261              at += trace->ds.size)
262                 trace_bts_at(trace, at);
263
264 out:
265         ds_resume_bts(this_tracer);
266 }
267
268 static void trace_bts_prepare(struct trace_iterator *iter)
269 {
270         mutex_lock(&bts_tracer_mutex);
271
272         on_each_cpu(trace_bts_cpu, iter->tr, 1);
273
274         mutex_unlock(&bts_tracer_mutex);
275 }
276
277 void trace_hw_branch_oops(void)
278 {
279         mutex_lock(&bts_tracer_mutex);
280
281         trace_bts_cpu(hw_branch_trace);
282
283         mutex_unlock(&bts_tracer_mutex);
284 }
285
286 struct tracer bts_tracer __read_mostly =
287 {
288         .name           = "hw-branch-tracer",
289         .init           = bts_trace_init,
290         .reset          = bts_trace_reset,
291         .print_header   = bts_trace_print_header,
292         .print_line     = bts_trace_print_line,
293         .start          = bts_trace_start,
294         .stop           = bts_trace_stop,
295         .open           = trace_bts_prepare
296 };
297
298 __init static int init_bts_trace(void)
299 {
300         return register_tracer(&bts_tracer);
301 }
302 device_initcall(init_bts_trace);