Commit | Line | Data |
---|---|---|
0722db01 P |
1 | /* |
2 | * trace_ksym.c - Kernel Symbol Tracer | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * Copyright (C) IBM Corporation, 2009 | |
19 | */ | |
20 | ||
21 | #include <linux/kallsyms.h> | |
22 | #include <linux/uaccess.h> | |
23 | #include <linux/debugfs.h> | |
24 | #include <linux/ftrace.h> | |
25 | #include <linux/module.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
0722db01 P |
27 | #include <linux/fs.h> |
28 | ||
29 | #include "trace_output.h" | |
0722db01 P |
30 | #include "trace.h" |
31 | ||
24f1e32c FW |
32 | #include <linux/hw_breakpoint.h> |
33 | #include <asm/hw_breakpoint.h> | |
34 | ||
e6d9491b LZ |
35 | #include <asm/atomic.h> |
36 | ||
0722db01 | 37 | #define KSYM_TRACER_OP_LEN 3 /* rw- */ |
0722db01 | 38 | |
db59504d | 39 | struct trace_ksym { |
24f1e32c | 40 | struct perf_event **ksym_hbp; |
dd1853c3 | 41 | struct perf_event_attr attr; |
db59504d | 42 | #ifdef CONFIG_PROFILE_KSYM_TRACER |
e6d9491b | 43 | atomic64_t counter; |
db59504d LZ |
44 | #endif |
45 | struct hlist_node ksym_hlist; | |
46 | }; | |
47 | ||
0722db01 P |
48 | static struct trace_array *ksym_trace_array; |
49 | ||
0722db01 P |
50 | static unsigned int ksym_tracing_enabled; |
51 | ||
52 | static HLIST_HEAD(ksym_filter_head); | |
53 | ||
73874005 FW |
54 | static DEFINE_MUTEX(ksym_tracer_mutex); |
55 | ||
0722db01 P |
56 | #ifdef CONFIG_PROFILE_KSYM_TRACER |
57 | ||
58 | #define MAX_UL_INT 0xffffffff | |
59 | ||
0722db01 P |
60 | void ksym_collect_stats(unsigned long hbp_hit_addr) |
61 | { | |
62 | struct hlist_node *node; | |
63 | struct trace_ksym *entry; | |
64 | ||
65 | rcu_read_lock(); | |
66 | hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) { | |
e6d9491b LZ |
67 | if (entry->attr.bp_addr == hbp_hit_addr) { |
68 | atomic64_inc(&entry->counter); | |
0722db01 P |
69 | break; |
70 | } | |
71 | } | |
72 | rcu_read_unlock(); | |
73 | } | |
74 | #endif /* CONFIG_PROFILE_KSYM_TRACER */ | |
75 | ||
b326e956 FW |
76 | void ksym_hbp_handler(struct perf_event *hbp, int nmi, |
77 | struct perf_sample_data *data, | |
78 | struct pt_regs *regs) | |
0722db01 P |
79 | { |
80 | struct ring_buffer_event *event; | |
db59504d | 81 | struct ksym_trace_entry *entry; |
d6a65dff | 82 | struct ring_buffer *buffer; |
0722db01 P |
83 | int pc; |
84 | ||
85 | if (!ksym_tracing_enabled) | |
86 | return; | |
87 | ||
d6a65dff FW |
88 | buffer = ksym_trace_array->buffer; |
89 | ||
0722db01 P |
90 | pc = preempt_count(); |
91 | ||
d6a65dff | 92 | event = trace_buffer_lock_reserve(buffer, TRACE_KSYM, |
0722db01 P |
93 | sizeof(*entry), 0, pc); |
94 | if (!event) | |
95 | return; | |
96 | ||
db59504d LZ |
97 | entry = ring_buffer_event_data(event); |
98 | entry->ip = instruction_pointer(regs); | |
24f1e32c FW |
99 | entry->type = hw_breakpoint_type(hbp); |
100 | entry->addr = hw_breakpoint_addr(hbp); | |
db59504d LZ |
101 | strlcpy(entry->cmd, current->comm, TASK_COMM_LEN); |
102 | ||
0722db01 | 103 | #ifdef CONFIG_PROFILE_KSYM_TRACER |
24f1e32c | 104 | ksym_collect_stats(hw_breakpoint_addr(hbp)); |
0722db01 P |
105 | #endif /* CONFIG_PROFILE_KSYM_TRACER */ |
106 | ||
d6a65dff | 107 | trace_buffer_unlock_commit(buffer, event, 0, pc); |
0722db01 P |
108 | } |
109 | ||
110 | /* Valid access types are represented as | |
111 | * | |
112 | * rw- : Set Read/Write Access Breakpoint | |
113 | * -w- : Set Write Access Breakpoint | |
114 | * --- : Clear Breakpoints | |
115 | * --x : Set Execution Break points (Not available yet) | |
116 | * | |
117 | */ | |
f088e547 | 118 | static int ksym_trace_get_access_type(char *str) |
0722db01 | 119 | { |
f088e547 | 120 | int access = 0; |
0722db01 | 121 | |
f088e547 | 122 | if (str[0] == 'r') |
24f1e32c | 123 | access |= HW_BREAKPOINT_R; |
f088e547 LZ |
124 | |
125 | if (str[1] == 'w') | |
24f1e32c | 126 | access |= HW_BREAKPOINT_W; |
f088e547 | 127 | |
24f1e32c FW |
128 | if (str[2] == 'x') |
129 | access |= HW_BREAKPOINT_X; | |
0722db01 P |
130 | |
131 | switch (access) { | |
676c0dbe | 132 | case HW_BREAKPOINT_R: |
24f1e32c FW |
133 | case HW_BREAKPOINT_W: |
134 | case HW_BREAKPOINT_W | HW_BREAKPOINT_R: | |
135 | return access; | |
136 | default: | |
137 | return -EINVAL; | |
0722db01 | 138 | } |
0722db01 P |
139 | } |
140 | ||
141 | /* | |
142 | * There can be several possible malformed requests and we attempt to capture | |
143 | * all of them. We enumerate some of the rules | |
144 | * 1. We will not allow kernel symbols with ':' since it is used as a delimiter. | |
145 | * i.e. multiple ':' symbols disallowed. Possible uses are of the form | |
146 | * <module>:<ksym_name>:<op>. | |
147 | * 2. No delimiter symbol ':' in the input string | |
148 | * 3. Spurious operator symbols or symbols not in their respective positions | |
149 | * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file | |
150 | * 5. Kernel symbol not a part of /proc/kallsyms | |
151 | * 6. Duplicate requests | |
152 | */ | |
153 | static int parse_ksym_trace_str(char *input_string, char **ksymname, | |
154 | unsigned long *addr) | |
155 | { | |
0722db01 P |
156 | int ret; |
157 | ||
92cf9f8f | 158 | *ksymname = strsep(&input_string, ":"); |
0722db01 P |
159 | *addr = kallsyms_lookup_name(*ksymname); |
160 | ||
161 | /* Check for malformed request: (2), (1) and (5) */ | |
162 | if ((!input_string) || | |
92cf9f8f LZ |
163 | (strlen(input_string) != KSYM_TRACER_OP_LEN) || |
164 | (*addr == 0)) | |
165 | return -EINVAL;; | |
166 | ||
0722db01 P |
167 | ret = ksym_trace_get_access_type(input_string); |
168 | ||
0722db01 P |
169 | return ret; |
170 | } | |
171 | ||
172 | int process_new_ksym_entry(char *ksymname, int op, unsigned long addr) | |
173 | { | |
174 | struct trace_ksym *entry; | |
558df6c8 | 175 | int ret = -ENOMEM; |
0722db01 | 176 | |
0722db01 P |
177 | entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL); |
178 | if (!entry) | |
179 | return -ENOMEM; | |
180 | ||
dd1853c3 FW |
181 | hw_breakpoint_init(&entry->attr); |
182 | ||
183 | entry->attr.bp_type = op; | |
184 | entry->attr.bp_addr = addr; | |
185 | entry->attr.bp_len = HW_BREAKPOINT_LEN_4; | |
24f1e32c | 186 | |
dd1853c3 FW |
187 | entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr, |
188 | ksym_hbp_handler); | |
605bfaee | 189 | |
24f1e32c | 190 | if (IS_ERR(entry->ksym_hbp)) { |
24f1e32c | 191 | ret = PTR_ERR(entry->ksym_hbp); |
feef47d0 FW |
192 | if (ret == -ENOSPC) { |
193 | printk(KERN_ERR "ksym_tracer: Maximum limit reached." | |
194 | " No new requests for tracing can be accepted now.\n"); | |
195 | } else { | |
196 | printk(KERN_INFO "ksym_tracer request failed. Try again" | |
197 | " later!!\n"); | |
198 | } | |
558df6c8 | 199 | goto err; |
0722db01 | 200 | } |
24f1e32c | 201 | |
0722db01 | 202 | hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head); |
24f1e32c | 203 | |
0722db01 | 204 | return 0; |
24f1e32c | 205 | |
558df6c8 | 206 | err: |
558df6c8 | 207 | kfree(entry); |
24f1e32c | 208 | |
558df6c8 | 209 | return ret; |
0722db01 P |
210 | } |
211 | ||
212 | static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf, | |
213 | size_t count, loff_t *ppos) | |
214 | { | |
215 | struct trace_ksym *entry; | |
216 | struct hlist_node *node; | |
be9742e6 LZ |
217 | struct trace_seq *s; |
218 | ssize_t cnt = 0; | |
219 | int ret; | |
220 | ||
221 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
222 | if (!s) | |
223 | return -ENOMEM; | |
224 | trace_seq_init(s); | |
0722db01 P |
225 | |
226 | mutex_lock(&ksym_tracer_mutex); | |
227 | ||
228 | hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) { | |
16620e0f LZ |
229 | ret = trace_seq_printf(s, "%pS:", |
230 | (void *)(unsigned long)entry->attr.bp_addr); | |
dd1853c3 | 231 | if (entry->attr.bp_type == HW_BREAKPOINT_R) |
676c0dbe | 232 | ret = trace_seq_puts(s, "r--\n"); |
dd1853c3 | 233 | else if (entry->attr.bp_type == HW_BREAKPOINT_W) |
be9742e6 | 234 | ret = trace_seq_puts(s, "-w-\n"); |
dd1853c3 | 235 | else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R)) |
be9742e6 LZ |
236 | ret = trace_seq_puts(s, "rw-\n"); |
237 | WARN_ON_ONCE(!ret); | |
0722db01 | 238 | } |
be9742e6 LZ |
239 | |
240 | cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len); | |
241 | ||
0722db01 P |
242 | mutex_unlock(&ksym_tracer_mutex); |
243 | ||
be9742e6 LZ |
244 | kfree(s); |
245 | ||
246 | return cnt; | |
0722db01 P |
247 | } |
248 | ||
75e33751 XG |
249 | static void __ksym_trace_reset(void) |
250 | { | |
251 | struct trace_ksym *entry; | |
252 | struct hlist_node *node, *node1; | |
253 | ||
254 | mutex_lock(&ksym_tracer_mutex); | |
255 | hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head, | |
256 | ksym_hlist) { | |
24f1e32c | 257 | unregister_wide_hw_breakpoint(entry->ksym_hbp); |
75e33751 XG |
258 | hlist_del_rcu(&(entry->ksym_hlist)); |
259 | synchronize_rcu(); | |
75e33751 XG |
260 | kfree(entry); |
261 | } | |
262 | mutex_unlock(&ksym_tracer_mutex); | |
263 | } | |
264 | ||
0722db01 P |
265 | static ssize_t ksym_trace_filter_write(struct file *file, |
266 | const char __user *buffer, | |
267 | size_t count, loff_t *ppos) | |
268 | { | |
269 | struct trace_ksym *entry; | |
270 | struct hlist_node *node; | |
d954fbf0 | 271 | char *buf, *input_string, *ksymname = NULL; |
0722db01 P |
272 | unsigned long ksym_addr = 0; |
273 | int ret, op, changed = 0; | |
274 | ||
d954fbf0 TG |
275 | buf = kzalloc(count + 1, GFP_KERNEL); |
276 | if (!buf) | |
0722db01 P |
277 | return -ENOMEM; |
278 | ||
d954fbf0 TG |
279 | ret = -EFAULT; |
280 | if (copy_from_user(buf, buffer, count)) | |
281 | goto out; | |
0722db01 | 282 | |
d954fbf0 TG |
283 | buf[count] = '\0'; |
284 | input_string = strstrip(buf); | |
75e33751 XG |
285 | |
286 | /* | |
287 | * Clear all breakpoints if: | |
288 | * 1: echo > ksym_trace_filter | |
289 | * 2: echo 0 > ksym_trace_filter | |
290 | * 3: echo "*:---" > ksym_trace_filter | |
291 | */ | |
3d13ec2e LZ |
292 | if (!input_string[0] || !strcmp(input_string, "0") || |
293 | !strcmp(input_string, "*:---")) { | |
75e33751 | 294 | __ksym_trace_reset(); |
d954fbf0 TG |
295 | ret = 0; |
296 | goto out; | |
75e33751 XG |
297 | } |
298 | ||
0722db01 | 299 | ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr); |
d954fbf0 TG |
300 | if (ret < 0) |
301 | goto out; | |
0722db01 P |
302 | |
303 | mutex_lock(&ksym_tracer_mutex); | |
304 | ||
305 | ret = -EINVAL; | |
306 | hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) { | |
dd1853c3 | 307 | if (entry->attr.bp_addr == ksym_addr) { |
0722db01 | 308 | /* Check for malformed request: (6) */ |
dd1853c3 | 309 | if (entry->attr.bp_type != op) |
0722db01 P |
310 | changed = 1; |
311 | else | |
d954fbf0 | 312 | goto out_unlock; |
0722db01 P |
313 | break; |
314 | } | |
315 | } | |
316 | if (changed) { | |
24f1e32c | 317 | unregister_wide_hw_breakpoint(entry->ksym_hbp); |
dd1853c3 | 318 | entry->attr.bp_type = op; |
605bfaee | 319 | ret = 0; |
0722db01 | 320 | if (op > 0) { |
24f1e32c | 321 | entry->ksym_hbp = |
dd1853c3 FW |
322 | register_wide_hw_breakpoint(&entry->attr, |
323 | ksym_hbp_handler); | |
24f1e32c | 324 | if (IS_ERR(entry->ksym_hbp)) |
605bfaee FW |
325 | ret = PTR_ERR(entry->ksym_hbp); |
326 | else | |
d954fbf0 | 327 | goto out_unlock; |
558df6c8 | 328 | } |
d99be40a | 329 | /* Error or "symbol:---" case: drop it */ |
0722db01 P |
330 | hlist_del_rcu(&(entry->ksym_hlist)); |
331 | synchronize_rcu(); | |
0722db01 | 332 | kfree(entry); |
d954fbf0 | 333 | goto out_unlock; |
0722db01 P |
334 | } else { |
335 | /* Check for malformed request: (4) */ | |
d954fbf0 TG |
336 | if (op) |
337 | ret = process_new_ksym_entry(ksymname, op, ksym_addr); | |
0722db01 | 338 | } |
d954fbf0 | 339 | out_unlock: |
558df6c8 | 340 | mutex_unlock(&ksym_tracer_mutex); |
d954fbf0 TG |
341 | out: |
342 | kfree(buf); | |
343 | return !ret ? count : ret; | |
0722db01 P |
344 | } |
345 | ||
346 | static const struct file_operations ksym_tracing_fops = { | |
347 | .open = tracing_open_generic, | |
348 | .read = ksym_trace_filter_read, | |
349 | .write = ksym_trace_filter_write, | |
350 | }; | |
351 | ||
352 | static void ksym_trace_reset(struct trace_array *tr) | |
353 | { | |
0722db01 | 354 | ksym_tracing_enabled = 0; |
75e33751 | 355 | __ksym_trace_reset(); |
0722db01 P |
356 | } |
357 | ||
358 | static int ksym_trace_init(struct trace_array *tr) | |
359 | { | |
360 | int cpu, ret = 0; | |
361 | ||
362 | for_each_online_cpu(cpu) | |
363 | tracing_reset(tr, cpu); | |
364 | ksym_tracing_enabled = 1; | |
365 | ksym_trace_array = tr; | |
366 | ||
367 | return ret; | |
368 | } | |
369 | ||
370 | static void ksym_trace_print_header(struct seq_file *m) | |
371 | { | |
0722db01 | 372 | seq_puts(m, |
d857ace1 XG |
373 | "# TASK-PID CPU# Symbol " |
374 | "Type Function\n"); | |
0722db01 | 375 | seq_puts(m, |
d857ace1 XG |
376 | "# | | | " |
377 | " | |\n"); | |
0722db01 P |
378 | } |
379 | ||
380 | static enum print_line_t ksym_trace_output(struct trace_iterator *iter) | |
381 | { | |
382 | struct trace_entry *entry = iter->ent; | |
383 | struct trace_seq *s = &iter->seq; | |
db59504d | 384 | struct ksym_trace_entry *field; |
0722db01 P |
385 | char str[KSYM_SYMBOL_LEN]; |
386 | int ret; | |
387 | ||
388 | if (entry->type != TRACE_KSYM) | |
389 | return TRACE_TYPE_UNHANDLED; | |
390 | ||
391 | trace_assign_type(field, entry); | |
392 | ||
24f1e32c FW |
393 | ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd, |
394 | entry->pid, iter->cpu, (char *)field->addr); | |
0722db01 P |
395 | if (!ret) |
396 | return TRACE_TYPE_PARTIAL_LINE; | |
397 | ||
db59504d | 398 | switch (field->type) { |
676c0dbe PM |
399 | case HW_BREAKPOINT_R: |
400 | ret = trace_seq_printf(s, " R "); | |
401 | break; | |
24f1e32c | 402 | case HW_BREAKPOINT_W: |
0722db01 P |
403 | ret = trace_seq_printf(s, " W "); |
404 | break; | |
24f1e32c | 405 | case HW_BREAKPOINT_R | HW_BREAKPOINT_W: |
0722db01 P |
406 | ret = trace_seq_printf(s, " RW "); |
407 | break; | |
408 | default: | |
409 | return TRACE_TYPE_PARTIAL_LINE; | |
410 | } | |
411 | ||
412 | if (!ret) | |
413 | return TRACE_TYPE_PARTIAL_LINE; | |
414 | ||
415 | sprint_symbol(str, field->ip); | |
d857ace1 | 416 | ret = trace_seq_printf(s, "%s\n", str); |
0722db01 P |
417 | if (!ret) |
418 | return TRACE_TYPE_PARTIAL_LINE; | |
419 | ||
420 | return TRACE_TYPE_HANDLED; | |
421 | } | |
422 | ||
423 | struct tracer ksym_tracer __read_mostly = | |
424 | { | |
425 | .name = "ksym_tracer", | |
426 | .init = ksym_trace_init, | |
427 | .reset = ksym_trace_reset, | |
428 | #ifdef CONFIG_FTRACE_SELFTEST | |
429 | .selftest = trace_selftest_startup_ksym, | |
430 | #endif | |
431 | .print_header = ksym_trace_print_header, | |
432 | .print_line = ksym_trace_output | |
433 | }; | |
434 | ||
0722db01 | 435 | #ifdef CONFIG_PROFILE_KSYM_TRACER |
53ab6680 | 436 | static int ksym_profile_show(struct seq_file *m, void *v) |
0722db01 | 437 | { |
53ab6680 LZ |
438 | struct hlist_node *node; |
439 | struct trace_ksym *entry; | |
440 | int access_type = 0; | |
441 | char fn_name[KSYM_NAME_LEN]; | |
442 | ||
9d7e9344 LZ |
443 | seq_puts(m, " Access Type "); |
444 | seq_puts(m, " Symbol Counter\n"); | |
445 | seq_puts(m, " ----------- "); | |
446 | seq_puts(m, " ------ -------\n"); | |
0722db01 | 447 | |
53ab6680 LZ |
448 | rcu_read_lock(); |
449 | hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) { | |
0722db01 | 450 | |
53ab6680 | 451 | access_type = entry->attr.bp_type; |
0722db01 | 452 | |
53ab6680 LZ |
453 | switch (access_type) { |
454 | case HW_BREAKPOINT_R: | |
455 | seq_puts(m, " R "); | |
456 | break; | |
457 | case HW_BREAKPOINT_W: | |
458 | seq_puts(m, " W "); | |
459 | break; | |
460 | case HW_BREAKPOINT_R | HW_BREAKPOINT_W: | |
461 | seq_puts(m, " RW "); | |
462 | break; | |
463 | default: | |
464 | seq_puts(m, " NA "); | |
465 | } | |
0722db01 | 466 | |
53ab6680 LZ |
467 | if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0) |
468 | seq_printf(m, " %-36s", fn_name); | |
469 | else | |
470 | seq_printf(m, " %-36s", "<NA>"); | |
471 | seq_printf(m, " %15llu\n", | |
472 | (unsigned long long)atomic64_read(&entry->counter)); | |
0722db01 | 473 | } |
53ab6680 | 474 | rcu_read_unlock(); |
0722db01 | 475 | |
0722db01 P |
476 | return 0; |
477 | } | |
478 | ||
53ab6680 | 479 | static int ksym_profile_open(struct inode *node, struct file *file) |
0722db01 | 480 | { |
53ab6680 | 481 | return single_open(file, ksym_profile_show, NULL); |
0722db01 P |
482 | } |
483 | ||
53ab6680 LZ |
484 | static const struct file_operations ksym_profile_fops = { |
485 | .open = ksym_profile_open, | |
486 | .read = seq_read, | |
487 | .llseek = seq_lseek, | |
488 | .release = single_release, | |
0722db01 | 489 | }; |
53ab6680 | 490 | #endif /* CONFIG_PROFILE_KSYM_TRACER */ |
0722db01 | 491 | |
53ab6680 | 492 | __init static int init_ksym_trace(void) |
0722db01 | 493 | { |
53ab6680 | 494 | struct dentry *d_tracer; |
0722db01 | 495 | |
53ab6680 | 496 | d_tracer = tracing_init_dentry(); |
0722db01 | 497 | |
53ab6680 LZ |
498 | trace_create_file("ksym_trace_filter", 0644, d_tracer, |
499 | NULL, &ksym_tracing_fops); | |
500 | ||
501 | #ifdef CONFIG_PROFILE_KSYM_TRACER | |
502 | trace_create_file("ksym_profile", 0444, d_tracer, | |
503 | NULL, &ksym_profile_fops); | |
504 | #endif | |
505 | ||
506 | return register_tracer(&ksym_tracer); | |
0722db01 | 507 | } |
53ab6680 | 508 | device_initcall(init_ksym_trace); |