tracing/ksym_tracer: support quick clear for ksym_trace_filter -- v2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_ksym.c
blob2fde875ead4cff51ad09fec0d18fc53d0554bcdf
1 /*
2 * trace_ksym.c - Kernel Symbol Tracer
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2009
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>
28 #include "trace_output.h"
29 #include "trace_stat.h"
30 #include "trace.h"
32 /* For now, let us restrict the no. of symbols traced simultaneously to number
33 * of available hardware breakpoint registers.
35 #define KSYM_TRACER_MAX HBP_NUM
37 #define KSYM_TRACER_OP_LEN 3 /* rw- */
39 struct trace_ksym {
40 struct hw_breakpoint *ksym_hbp;
41 unsigned long ksym_addr;
42 #ifdef CONFIG_PROFILE_KSYM_TRACER
43 unsigned long counter;
44 #endif
45 struct hlist_node ksym_hlist;
48 static struct trace_array *ksym_trace_array;
50 static unsigned int ksym_filter_entry_count;
51 static unsigned int ksym_tracing_enabled;
53 static HLIST_HEAD(ksym_filter_head);
55 static DEFINE_MUTEX(ksym_tracer_mutex);
57 #ifdef CONFIG_PROFILE_KSYM_TRACER
59 #define MAX_UL_INT 0xffffffff
61 void ksym_collect_stats(unsigned long hbp_hit_addr)
63 struct hlist_node *node;
64 struct trace_ksym *entry;
66 rcu_read_lock();
67 hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
68 if ((entry->ksym_addr == hbp_hit_addr) &&
69 (entry->counter <= MAX_UL_INT)) {
70 entry->counter++;
71 break;
74 rcu_read_unlock();
76 #endif /* CONFIG_PROFILE_KSYM_TRACER */
78 void ksym_hbp_handler(struct hw_breakpoint *hbp, struct pt_regs *regs)
80 struct ring_buffer_event *event;
81 struct trace_array *tr;
82 struct ksym_trace_entry *entry;
83 int pc;
85 if (!ksym_tracing_enabled)
86 return;
88 tr = ksym_trace_array;
89 pc = preempt_count();
91 event = trace_buffer_lock_reserve(tr, TRACE_KSYM,
92 sizeof(*entry), 0, pc);
93 if (!event)
94 return;
96 entry = ring_buffer_event_data(event);
97 entry->ip = instruction_pointer(regs);
98 entry->type = hbp->info.type;
99 strlcpy(entry->ksym_name, hbp->info.name, KSYM_SYMBOL_LEN);
100 strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
102 #ifdef CONFIG_PROFILE_KSYM_TRACER
103 ksym_collect_stats(hbp->info.address);
104 #endif /* CONFIG_PROFILE_KSYM_TRACER */
106 trace_buffer_unlock_commit(tr, event, 0, pc);
109 /* Valid access types are represented as
111 * rw- : Set Read/Write Access Breakpoint
112 * -w- : Set Write Access Breakpoint
113 * --- : Clear Breakpoints
114 * --x : Set Execution Break points (Not available yet)
117 static int ksym_trace_get_access_type(char *str)
119 int access = 0;
121 if (str[0] == 'r')
122 access += 4;
123 else if (str[0] != '-')
124 return -EINVAL;
126 if (str[1] == 'w')
127 access += 2;
128 else if (str[1] != '-')
129 return -EINVAL;
131 if (str[2] != '-')
132 return -EINVAL;
134 switch (access) {
135 case 6:
136 access = HW_BREAKPOINT_RW;
137 break;
138 case 4:
139 access = -EINVAL;
140 break;
141 case 2:
142 access = HW_BREAKPOINT_WRITE;
143 break;
146 return access;
150 * There can be several possible malformed requests and we attempt to capture
151 * all of them. We enumerate some of the rules
152 * 1. We will not allow kernel symbols with ':' since it is used as a delimiter.
153 * i.e. multiple ':' symbols disallowed. Possible uses are of the form
154 * <module>:<ksym_name>:<op>.
155 * 2. No delimiter symbol ':' in the input string
156 * 3. Spurious operator symbols or symbols not in their respective positions
157 * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file
158 * 5. Kernel symbol not a part of /proc/kallsyms
159 * 6. Duplicate requests
161 static int parse_ksym_trace_str(char *input_string, char **ksymname,
162 unsigned long *addr)
164 int ret;
166 *ksymname = strsep(&input_string, ":");
167 *addr = kallsyms_lookup_name(*ksymname);
169 /* Check for malformed request: (2), (1) and (5) */
170 if ((!input_string) ||
171 (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
172 (*addr == 0))
173 return -EINVAL;;
175 ret = ksym_trace_get_access_type(input_string);
177 return ret;
180 int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
182 struct trace_ksym *entry;
183 int ret = -ENOMEM;
185 if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
186 printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
187 " new requests for tracing can be accepted now.\n",
188 KSYM_TRACER_MAX);
189 return -ENOSPC;
192 entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
193 if (!entry)
194 return -ENOMEM;
196 entry->ksym_hbp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
197 if (!entry->ksym_hbp)
198 goto err;
200 entry->ksym_hbp->info.name = kstrdup(ksymname, GFP_KERNEL);
201 if (!entry->ksym_hbp->info.name)
202 goto err;
204 entry->ksym_hbp->info.type = op;
205 entry->ksym_addr = entry->ksym_hbp->info.address = addr;
206 #ifdef CONFIG_X86
207 entry->ksym_hbp->info.len = HW_BREAKPOINT_LEN_4;
208 #endif
209 entry->ksym_hbp->triggered = (void *)ksym_hbp_handler;
211 ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
212 if (ret < 0) {
213 printk(KERN_INFO "ksym_tracer request failed. Try again"
214 " later!!\n");
215 ret = -EAGAIN;
216 goto err;
218 hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
219 ksym_filter_entry_count++;
220 return 0;
221 err:
222 if (entry->ksym_hbp)
223 kfree(entry->ksym_hbp->info.name);
224 kfree(entry->ksym_hbp);
225 kfree(entry);
226 return ret;
229 static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
230 size_t count, loff_t *ppos)
232 struct trace_ksym *entry;
233 struct hlist_node *node;
234 struct trace_seq *s;
235 ssize_t cnt = 0;
236 int ret;
238 s = kmalloc(sizeof(*s), GFP_KERNEL);
239 if (!s)
240 return -ENOMEM;
241 trace_seq_init(s);
243 mutex_lock(&ksym_tracer_mutex);
245 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
246 ret = trace_seq_printf(s, "%s:", entry->ksym_hbp->info.name);
247 if (entry->ksym_hbp->info.type == HW_BREAKPOINT_WRITE)
248 ret = trace_seq_puts(s, "-w-\n");
249 else if (entry->ksym_hbp->info.type == HW_BREAKPOINT_RW)
250 ret = trace_seq_puts(s, "rw-\n");
251 WARN_ON_ONCE(!ret);
254 cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
256 mutex_unlock(&ksym_tracer_mutex);
258 kfree(s);
260 return cnt;
263 static void __ksym_trace_reset(void)
265 struct trace_ksym *entry;
266 struct hlist_node *node, *node1;
268 mutex_lock(&ksym_tracer_mutex);
269 hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
270 ksym_hlist) {
271 unregister_kernel_hw_breakpoint(entry->ksym_hbp);
272 ksym_filter_entry_count--;
273 hlist_del_rcu(&(entry->ksym_hlist));
274 synchronize_rcu();
275 kfree(entry->ksym_hbp->info.name);
276 kfree(entry->ksym_hbp);
277 kfree(entry);
279 mutex_unlock(&ksym_tracer_mutex);
282 static ssize_t ksym_trace_filter_write(struct file *file,
283 const char __user *buffer,
284 size_t count, loff_t *ppos)
286 struct trace_ksym *entry;
287 struct hlist_node *node;
288 char *input_string, *ksymname = NULL;
289 unsigned long ksym_addr = 0;
290 int ret, op, changed = 0;
292 input_string = kzalloc(count + 1, GFP_KERNEL);
293 if (!input_string)
294 return -ENOMEM;
296 if (copy_from_user(input_string, buffer, count)) {
297 kfree(input_string);
298 return -EFAULT;
300 input_string[count] = '\0';
302 strstrip(input_string);
305 * Clear all breakpoints if:
306 * 1: echo > ksym_trace_filter
307 * 2: echo 0 > ksym_trace_filter
308 * 3: echo "*:---" > ksym_trace_filter
310 if (!input_string[0] || !strcmp(input_string, "0") ||
311 !strcmp(input_string, "*:---")) {
312 __ksym_trace_reset();
313 kfree(input_string);
314 return count;
317 ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
318 if (ret < 0) {
319 kfree(input_string);
320 return ret;
323 mutex_lock(&ksym_tracer_mutex);
325 ret = -EINVAL;
326 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
327 if (entry->ksym_addr == ksym_addr) {
328 /* Check for malformed request: (6) */
329 if (entry->ksym_hbp->info.type != op)
330 changed = 1;
331 else
332 goto out;
333 break;
336 if (changed) {
337 unregister_kernel_hw_breakpoint(entry->ksym_hbp);
338 entry->ksym_hbp->info.type = op;
339 if (op > 0) {
340 ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
341 if (ret == 0)
342 goto out;
344 ksym_filter_entry_count--;
345 hlist_del_rcu(&(entry->ksym_hlist));
346 synchronize_rcu();
347 kfree(entry->ksym_hbp->info.name);
348 kfree(entry->ksym_hbp);
349 kfree(entry);
350 ret = 0;
351 goto out;
352 } else {
353 /* Check for malformed request: (4) */
354 if (op == 0)
355 goto out;
356 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
358 out:
359 mutex_unlock(&ksym_tracer_mutex);
361 kfree(input_string);
363 if (!ret)
364 ret = count;
365 return ret;
368 static const struct file_operations ksym_tracing_fops = {
369 .open = tracing_open_generic,
370 .read = ksym_trace_filter_read,
371 .write = ksym_trace_filter_write,
374 static void ksym_trace_reset(struct trace_array *tr)
376 ksym_tracing_enabled = 0;
377 __ksym_trace_reset();
380 static int ksym_trace_init(struct trace_array *tr)
382 int cpu, ret = 0;
384 for_each_online_cpu(cpu)
385 tracing_reset(tr, cpu);
386 ksym_tracing_enabled = 1;
387 ksym_trace_array = tr;
389 return ret;
392 static void ksym_trace_print_header(struct seq_file *m)
394 seq_puts(m,
395 "# TASK-PID CPU# Symbol "
396 "Type Function\n");
397 seq_puts(m,
398 "# | | | "
399 " | |\n");
402 static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
404 struct trace_entry *entry = iter->ent;
405 struct trace_seq *s = &iter->seq;
406 struct ksym_trace_entry *field;
407 char str[KSYM_SYMBOL_LEN];
408 int ret;
410 if (entry->type != TRACE_KSYM)
411 return TRACE_TYPE_UNHANDLED;
413 trace_assign_type(field, entry);
415 ret = trace_seq_printf(s, "%11s-%-5d [%03d] %-30s ", field->cmd,
416 entry->pid, iter->cpu, field->ksym_name);
417 if (!ret)
418 return TRACE_TYPE_PARTIAL_LINE;
420 switch (field->type) {
421 case HW_BREAKPOINT_WRITE:
422 ret = trace_seq_printf(s, " W ");
423 break;
424 case HW_BREAKPOINT_RW:
425 ret = trace_seq_printf(s, " RW ");
426 break;
427 default:
428 return TRACE_TYPE_PARTIAL_LINE;
431 if (!ret)
432 return TRACE_TYPE_PARTIAL_LINE;
434 sprint_symbol(str, field->ip);
435 ret = trace_seq_printf(s, "%s\n", str);
436 if (!ret)
437 return TRACE_TYPE_PARTIAL_LINE;
439 return TRACE_TYPE_HANDLED;
442 struct tracer ksym_tracer __read_mostly =
444 .name = "ksym_tracer",
445 .init = ksym_trace_init,
446 .reset = ksym_trace_reset,
447 #ifdef CONFIG_FTRACE_SELFTEST
448 .selftest = trace_selftest_startup_ksym,
449 #endif
450 .print_header = ksym_trace_print_header,
451 .print_line = ksym_trace_output
454 __init static int init_ksym_trace(void)
456 struct dentry *d_tracer;
457 struct dentry *entry;
459 d_tracer = tracing_init_dentry();
460 ksym_filter_entry_count = 0;
462 entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
463 NULL, &ksym_tracing_fops);
464 if (!entry)
465 pr_warning("Could not create debugfs "
466 "'ksym_trace_filter' file\n");
468 return register_tracer(&ksym_tracer);
470 device_initcall(init_ksym_trace);
473 #ifdef CONFIG_PROFILE_KSYM_TRACER
474 static int ksym_tracer_stat_headers(struct seq_file *m)
476 seq_puts(m, " Access Type ");
477 seq_puts(m, " Symbol Counter\n");
478 seq_puts(m, " ----------- ");
479 seq_puts(m, " ------ -------\n");
480 return 0;
483 static int ksym_tracer_stat_show(struct seq_file *m, void *v)
485 struct hlist_node *stat = v;
486 struct trace_ksym *entry;
487 int access_type = 0;
488 char fn_name[KSYM_NAME_LEN];
490 entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
492 if (entry->ksym_hbp)
493 access_type = entry->ksym_hbp->info.type;
495 switch (access_type) {
496 case HW_BREAKPOINT_WRITE:
497 seq_puts(m, " W ");
498 break;
499 case HW_BREAKPOINT_RW:
500 seq_puts(m, " RW ");
501 break;
502 default:
503 seq_puts(m, " NA ");
506 if (lookup_symbol_name(entry->ksym_addr, fn_name) >= 0)
507 seq_printf(m, " %-36s", fn_name);
508 else
509 seq_printf(m, " %-36s", "<NA>");
510 seq_printf(m, " %15lu\n", entry->counter);
512 return 0;
515 static void *ksym_tracer_stat_start(struct tracer_stat *trace)
517 return ksym_filter_head.first;
520 static void *
521 ksym_tracer_stat_next(void *v, int idx)
523 struct hlist_node *stat = v;
525 return stat->next;
528 static struct tracer_stat ksym_tracer_stats = {
529 .name = "ksym_tracer",
530 .stat_start = ksym_tracer_stat_start,
531 .stat_next = ksym_tracer_stat_next,
532 .stat_headers = ksym_tracer_stat_headers,
533 .stat_show = ksym_tracer_stat_show
536 __init static int ksym_tracer_stat_init(void)
538 int ret;
540 ret = register_stat_tracer(&ksym_tracer_stats);
541 if (ret) {
542 printk(KERN_WARNING "Warning: could not register "
543 "ksym tracer stats\n");
544 return 1;
547 return 0;
549 fs_initcall(ksym_tracer_stat_init);
550 #endif /* CONFIG_PROFILE_KSYM_TRACER */