2 * h/w branch tracer for x86 based on bts
4 * Copyright (C) 2008 Markus Metzger <markus.t.metzger@gmail.com>
8 #include <linux/module.h>
10 #include <linux/debugfs.h>
11 #include <linux/ftrace.h>
12 #include <linux/kallsyms.h>
17 #include "trace_output.h"
20 #define SIZEOF_BTS (1 << 13)
22 static DEFINE_PER_CPU(struct bts_tracer
*, tracer
);
23 static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS
], buffer
);
25 #define this_tracer per_cpu(tracer, smp_processor_id())
26 #define this_buffer per_cpu(buffer, smp_processor_id())
29 static void bts_trace_start_cpu(void *arg
)
32 ds_release_bts(this_tracer
);
35 ds_request_bts(/* task = */ NULL
, this_buffer
, SIZEOF_BTS
,
36 /* ovfl = */ NULL
, /* th = */ (size_t)-1,
38 if (IS_ERR(this_tracer
)) {
44 static void bts_trace_start(struct trace_array
*tr
)
48 tracing_reset_online_cpus(tr
);
50 for_each_cpu(cpu
, cpu_possible_mask
)
51 smp_call_function_single(cpu
, bts_trace_start_cpu
, NULL
, 1);
54 static void bts_trace_stop_cpu(void *arg
)
57 ds_release_bts(this_tracer
);
62 static void bts_trace_stop(struct trace_array
*tr
)
66 for_each_cpu(cpu
, cpu_possible_mask
)
67 smp_call_function_single(cpu
, bts_trace_stop_cpu
, NULL
, 1);
70 static int bts_trace_init(struct trace_array
*tr
)
72 tracing_reset_online_cpus(tr
);
78 static void bts_trace_print_header(struct seq_file
*m
)
81 "# CPU# FROM TO FUNCTION\n");
86 static enum print_line_t
bts_trace_print_line(struct trace_iterator
*iter
)
88 struct trace_entry
*entry
= iter
->ent
;
89 struct trace_seq
*seq
= &iter
->seq
;
90 struct hw_branch_entry
*it
;
92 trace_assign_type(it
, entry
);
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 ",
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
;;
104 return TRACE_TYPE_UNHANDLED
;
107 void trace_hw_branch(struct trace_array
*tr
, u64 from
, u64 to
)
109 struct ring_buffer_event
*event
;
110 struct hw_branch_entry
*entry
;
113 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
), &irq
);
116 entry
= ring_buffer_event_data(event
);
117 tracing_generic_entry_update(&entry
->ent
, 0, from
);
118 entry
->ent
.type
= TRACE_HW_BRANCHES
;
119 entry
->ent
.cpu
= smp_processor_id();
122 ring_buffer_unlock_commit(tr
->buffer
, event
, irq
);
125 static void trace_bts_at(struct trace_array
*tr
,
126 const struct bts_trace
*trace
, void *at
)
128 struct bts_struct bts
;
131 WARN_ON_ONCE(!trace
->read
);
135 err
= trace
->read(this_tracer
, at
, &bts
);
139 switch (bts
.qualifier
) {
141 trace_hw_branch(tr
, bts
.variant
.lbr
.from
, bts
.variant
.lbr
.to
);
146 static void trace_bts_cpu(void *arg
)
148 struct trace_array
*tr
= (struct trace_array
*) arg
;
149 const struct bts_trace
*trace
;
155 ds_suspend_bts(this_tracer
);
156 trace
= ds_read_bts(this_tracer
);
160 for (at
= trace
->ds
.top
; (void *)at
< trace
->ds
.end
;
161 at
+= trace
->ds
.size
)
162 trace_bts_at(tr
, trace
, at
);
164 for (at
= trace
->ds
.begin
; (void *)at
< trace
->ds
.top
;
165 at
+= trace
->ds
.size
)
166 trace_bts_at(tr
, trace
, at
);
169 ds_resume_bts(this_tracer
);
172 static void trace_bts_prepare(struct trace_iterator
*iter
)
176 for_each_cpu(cpu
, cpu_possible_mask
)
177 smp_call_function_single(cpu
, trace_bts_cpu
, iter
->tr
, 1);
180 struct tracer bts_tracer __read_mostly
=
182 .name
= "hw-branch-tracer",
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
192 __init
static int init_bts_trace(void)
194 return register_tracer(&bts_tracer
);
196 device_initcall(init_bts_trace
);