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