Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * h/w branch tracer for x86 based on bts |
| 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" |
Steven Rostedt | f0868d1 | 2008-12-23 23:24:12 -0500 | [diff] [blame^] | 17 | #include "trace_output.h" |
Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 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 | |
Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 29 | static void bts_trace_start_cpu(void *arg) |
| 30 | { |
| 31 | if (this_tracer) |
| 32 | ds_release_bts(this_tracer); |
| 33 | |
| 34 | this_tracer = |
| 35 | ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS, |
| 36 | /* ovfl = */ NULL, /* th = */ (size_t)-1, |
| 37 | BTS_KERNEL); |
| 38 | if (IS_ERR(this_tracer)) { |
| 39 | this_tracer = NULL; |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static void bts_trace_start(struct trace_array *tr) |
| 45 | { |
| 46 | int cpu; |
| 47 | |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 48 | tracing_reset_online_cpus(tr); |
Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 49 | |
| 50 | for_each_cpu_mask(cpu, cpu_possible_map) |
| 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) { |
| 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 | |
| 66 | for_each_cpu_mask(cpu, cpu_possible_map) |
| 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 | { |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 72 | tracing_reset_online_cpus(tr); |
Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 73 | bts_trace_start(tr); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static void bts_trace_print_header(struct seq_file *m) |
| 79 | { |
| 80 | seq_puts(m, |
| 81 | "# CPU# FROM TO FUNCTION\n"); |
| 82 | seq_puts(m, |
| 83 | "# | | | |\n"); |
| 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; |
| 90 | struct hw_branch_entry *it; |
| 91 | |
| 92 | trace_assign_type(it, entry); |
| 93 | |
| 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;; |
| 103 | } |
| 104 | return TRACE_TYPE_UNHANDLED; |
| 105 | } |
| 106 | |
| 107 | void trace_hw_branch(struct trace_array *tr, u64 from, u64 to) |
| 108 | { |
| 109 | struct ring_buffer_event *event; |
| 110 | struct hw_branch_entry *entry; |
| 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); |
| 118 | entry->ent.type = TRACE_HW_BRANCHES; |
| 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 | |
| 125 | static void trace_bts_at(struct trace_array *tr, |
| 126 | const struct bts_trace *trace, void *at) |
| 127 | { |
| 128 | struct bts_struct bts; |
| 129 | int err = 0; |
| 130 | |
| 131 | WARN_ON_ONCE(!trace->read); |
| 132 | if (!trace->read) |
| 133 | return; |
| 134 | |
| 135 | err = trace->read(this_tracer, at, &bts); |
| 136 | if (err < 0) |
| 137 | return; |
| 138 | |
| 139 | switch (bts.qualifier) { |
| 140 | case BTS_BRANCH: |
| 141 | trace_hw_branch(tr, bts.variant.lbr.from, bts.variant.lbr.to); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void trace_bts_cpu(void *arg) |
| 147 | { |
| 148 | struct trace_array *tr = (struct trace_array *) arg; |
| 149 | const struct bts_trace *trace; |
| 150 | unsigned char *at; |
| 151 | |
| 152 | if (!this_tracer) |
| 153 | return; |
| 154 | |
| 155 | ds_suspend_bts(this_tracer); |
| 156 | trace = ds_read_bts(this_tracer); |
| 157 | if (!trace) |
| 158 | goto out; |
| 159 | |
| 160 | for (at = trace->ds.top; (void *)at < trace->ds.end; |
| 161 | at += trace->ds.size) |
| 162 | trace_bts_at(tr, trace, at); |
| 163 | |
| 164 | for (at = trace->ds.begin; (void *)at < trace->ds.top; |
| 165 | at += trace->ds.size) |
| 166 | trace_bts_at(tr, trace, at); |
| 167 | |
| 168 | out: |
| 169 | ds_resume_bts(this_tracer); |
| 170 | } |
| 171 | |
| 172 | static void trace_bts_prepare(struct trace_iterator *iter) |
| 173 | { |
| 174 | int cpu; |
| 175 | |
| 176 | for_each_cpu_mask(cpu, cpu_possible_map) |
| 177 | smp_call_function_single(cpu, trace_bts_cpu, iter->tr, 1); |
| 178 | } |
| 179 | |
| 180 | struct tracer bts_tracer __read_mostly = |
| 181 | { |
| 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 |
| 190 | }; |
| 191 | |
| 192 | __init static int init_bts_trace(void) |
| 193 | { |
| 194 | return register_tracer(&bts_tracer); |
| 195 | } |
| 196 | device_initcall(init_bts_trace); |