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>
28 #include "trace_output.h"
29 #include "trace_stat.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- */
38 #define KSYM_FILTER_ENTRY_LEN (KSYM_NAME_LEN + KSYM_TRACER_OP_LEN + 1)
40 static struct trace_array *ksym_trace_array;
42 static unsigned int ksym_filter_entry_count;
43 static unsigned int ksym_tracing_enabled;
45 static HLIST_HEAD(ksym_filter_head);
47 #ifdef CONFIG_PROFILE_KSYM_TRACER
49 #define MAX_UL_INT 0xffffffff
51 static DEFINE_MUTEX(ksym_tracer_mutex);
53 void ksym_collect_stats(unsigned long hbp_hit_addr)
55 struct hlist_node *node;
56 struct trace_ksym *entry;
59 hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
60 if ((entry->ksym_addr == hbp_hit_addr) &&
61 (entry->counter <= MAX_UL_INT)) {
68 #endif /* CONFIG_PROFILE_KSYM_TRACER */
70 void ksym_hbp_handler(struct hw_breakpoint *hbp, struct pt_regs *regs)
72 struct ring_buffer_event *event;
73 struct trace_array *tr;
74 struct trace_ksym *entry;
77 if (!ksym_tracing_enabled)
80 tr = ksym_trace_array;
83 event = trace_buffer_lock_reserve(tr, TRACE_KSYM,
84 sizeof(*entry), 0, pc);
88 entry = ring_buffer_event_data(event);
89 strlcpy(entry->ksym_name, hbp->info.name, KSYM_SYMBOL_LEN);
90 entry->ksym_hbp = hbp;
91 entry->ip = instruction_pointer(regs);
92 strlcpy(entry->p_name, current->comm, TASK_COMM_LEN);
93 #ifdef CONFIG_PROFILE_KSYM_TRACER
94 ksym_collect_stats(hbp->info.address);
95 #endif /* CONFIG_PROFILE_KSYM_TRACER */
97 trace_buffer_unlock_commit(tr, event, 0, pc);
100 /* Valid access types are represented as
102 * rw- : Set Read/Write Access Breakpoint
103 * -w- : Set Write Access Breakpoint
104 * --- : Clear Breakpoints
105 * --x : Set Execution Break points (Not available yet)
108 static int ksym_trace_get_access_type(char *access_str)
112 for (pos = 0; pos < KSYM_TRACER_OP_LEN; pos++) {
113 switch (access_str[pos]) {
115 access += (pos == 0) ? 4 : -1;
118 access += (pos == 1) ? 2 : -1;
129 access = HW_BREAKPOINT_RW;
132 access = HW_BREAKPOINT_WRITE;
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
153 static int parse_ksym_trace_str(char *input_string, char **ksymname,
156 char *delimiter = ":";
160 *ksymname = strsep(&input_string, delimiter);
161 *addr = kallsyms_lookup_name(*ksymname);
163 /* Check for malformed request: (2), (1) and (5) */
164 if ((!input_string) ||
165 (strlen(input_string) != (KSYM_TRACER_OP_LEN + 1)) ||
168 ret = ksym_trace_get_access_type(input_string);
174 int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
176 struct trace_ksym *entry;
179 if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
180 printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
181 " new requests for tracing can be accepted now.\n",
186 entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
190 entry->ksym_hbp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
191 if (!entry->ksym_hbp) {
196 entry->ksym_hbp->info.name = ksymname;
197 entry->ksym_hbp->info.type = op;
198 entry->ksym_addr = entry->ksym_hbp->info.address = addr;
200 entry->ksym_hbp->info.len = HW_BREAKPOINT_LEN_4;
202 entry->ksym_hbp->triggered = (void *)ksym_hbp_handler;
204 ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
206 printk(KERN_INFO "ksym_tracer request failed. Try again"
208 kfree(entry->ksym_hbp);
212 hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
213 ksym_filter_entry_count++;
218 static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
219 size_t count, loff_t *ppos)
221 struct trace_ksym *entry;
222 struct hlist_node *node;
223 char buf[KSYM_FILTER_ENTRY_LEN * KSYM_TRACER_MAX];
224 ssize_t ret, cnt = 0;
226 mutex_lock(&ksym_tracer_mutex);
228 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
229 cnt += snprintf(&buf[cnt], KSYM_FILTER_ENTRY_LEN - cnt, "%s:",
230 entry->ksym_hbp->info.name);
231 if (entry->ksym_hbp->info.type == HW_BREAKPOINT_WRITE)
232 cnt += snprintf(&buf[cnt], KSYM_FILTER_ENTRY_LEN - cnt,
234 else if (entry->ksym_hbp->info.type == HW_BREAKPOINT_RW)
235 cnt += snprintf(&buf[cnt], KSYM_FILTER_ENTRY_LEN - cnt,
238 ret = simple_read_from_buffer(ubuf, count, ppos, buf, strlen(buf));
239 mutex_unlock(&ksym_tracer_mutex);
244 static ssize_t ksym_trace_filter_write(struct file *file,
245 const char __user *buffer,
246 size_t count, loff_t *ppos)
248 struct trace_ksym *entry;
249 struct hlist_node *node;
250 char *input_string, *ksymname = NULL;
251 unsigned long ksym_addr = 0;
252 int ret, op, changed = 0;
254 /* Ignore echo "" > ksym_trace_filter */
258 input_string = kzalloc(count, GFP_KERNEL);
262 if (copy_from_user(input_string, buffer, count)) {
267 ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
273 mutex_lock(&ksym_tracer_mutex);
276 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
277 if (entry->ksym_addr == ksym_addr) {
278 /* Check for malformed request: (6) */
279 if (entry->ksym_hbp->info.type != op)
287 unregister_kernel_hw_breakpoint(entry->ksym_hbp);
288 entry->ksym_hbp->info.type = op;
290 ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
293 goto unlock_ret_path;
296 ksym_filter_entry_count--;
297 hlist_del_rcu(&(entry->ksym_hlist));
299 kfree(entry->ksym_hbp);
304 /* Check for malformed request: (4) */
307 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
312 goto unlock_ret_path;
318 mutex_unlock(&ksym_tracer_mutex);
322 static const struct file_operations ksym_tracing_fops = {
323 .open = tracing_open_generic,
324 .read = ksym_trace_filter_read,
325 .write = ksym_trace_filter_write,
328 static void ksym_trace_reset(struct trace_array *tr)
330 struct trace_ksym *entry;
331 struct hlist_node *node, *node1;
333 ksym_tracing_enabled = 0;
335 mutex_lock(&ksym_tracer_mutex);
336 hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
338 unregister_kernel_hw_breakpoint(entry->ksym_hbp);
339 ksym_filter_entry_count--;
340 hlist_del_rcu(&(entry->ksym_hlist));
342 /* Free the 'input_string' only if reset
343 * after startup self-test
345 #ifdef CONFIG_FTRACE_SELFTEST
346 if (strncmp(entry->ksym_hbp->info.name, KSYM_SELFTEST_ENTRY,
347 strlen(KSYM_SELFTEST_ENTRY)) != 0)
348 #endif /* CONFIG_FTRACE_SELFTEST*/
349 kfree(entry->ksym_hbp->info.name);
350 kfree(entry->ksym_hbp);
353 mutex_unlock(&ksym_tracer_mutex);
356 static int ksym_trace_init(struct trace_array *tr)
360 for_each_online_cpu(cpu)
361 tracing_reset(tr, cpu);
362 ksym_tracing_enabled = 1;
363 ksym_trace_array = tr;
368 static void ksym_trace_print_header(struct seq_file *m)
372 "# TASK-PID CPU# Symbol Type "
379 static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
381 struct trace_entry *entry = iter->ent;
382 struct trace_seq *s = &iter->seq;
383 struct trace_ksym *field;
384 char str[KSYM_SYMBOL_LEN];
387 if (entry->type != TRACE_KSYM)
388 return TRACE_TYPE_UNHANDLED;
390 trace_assign_type(field, entry);
392 ret = trace_seq_printf(s, "%-15s %-5d %-3d %-20s ", field->p_name,
393 entry->pid, iter->cpu, field->ksym_name);
395 return TRACE_TYPE_PARTIAL_LINE;
397 switch (field->ksym_hbp->info.type) {
398 case HW_BREAKPOINT_WRITE:
399 ret = trace_seq_printf(s, " W ");
401 case HW_BREAKPOINT_RW:
402 ret = trace_seq_printf(s, " RW ");
405 return TRACE_TYPE_PARTIAL_LINE;
409 return TRACE_TYPE_PARTIAL_LINE;
411 sprint_symbol(str, field->ip);
412 ret = trace_seq_printf(s, "%-20s\n", str);
414 return TRACE_TYPE_PARTIAL_LINE;
416 return TRACE_TYPE_HANDLED;
419 struct tracer ksym_tracer __read_mostly =
421 .name = "ksym_tracer",
422 .init = ksym_trace_init,
423 .reset = ksym_trace_reset,
424 #ifdef CONFIG_FTRACE_SELFTEST
425 .selftest = trace_selftest_startup_ksym,
427 .print_header = ksym_trace_print_header,
428 .print_line = ksym_trace_output
431 __init static int init_ksym_trace(void)
433 struct dentry *d_tracer;
434 struct dentry *entry;
436 d_tracer = tracing_init_dentry();
437 ksym_filter_entry_count = 0;
439 entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
440 NULL, &ksym_tracing_fops);
442 pr_warning("Could not create debugfs "
443 "'ksym_trace_filter' file\n");
445 return register_tracer(&ksym_tracer);
447 device_initcall(init_ksym_trace);
450 #ifdef CONFIG_PROFILE_KSYM_TRACER
451 static int ksym_tracer_stat_headers(struct seq_file *m)
453 seq_printf(m, " Access type ");
454 seq_printf(m, " Symbol Counter \n");
458 static int ksym_tracer_stat_show(struct seq_file *m, void *v)
460 struct hlist_node *stat = v;
461 struct trace_ksym *entry;
463 char fn_name[KSYM_NAME_LEN];
465 entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
468 access_type = entry->ksym_hbp->info.type;
470 switch (access_type) {
471 case HW_BREAKPOINT_WRITE:
472 seq_printf(m, " W ");
474 case HW_BREAKPOINT_RW:
475 seq_printf(m, " RW ");
478 seq_printf(m, " NA ");
481 if (lookup_symbol_name(entry->ksym_addr, fn_name) >= 0)
482 seq_printf(m, " %s ", fn_name);
484 seq_printf(m, " <NA> ");
486 seq_printf(m, "%15lu\n", entry->counter);
490 static void *ksym_tracer_stat_start(struct tracer_stat *trace)
492 return &(ksym_filter_head.first);
496 ksym_tracer_stat_next(void *v, int idx)
498 struct hlist_node *stat = v;
503 static struct tracer_stat ksym_tracer_stats = {
504 .name = "ksym_tracer",
505 .stat_start = ksym_tracer_stat_start,
506 .stat_next = ksym_tracer_stat_next,
507 .stat_headers = ksym_tracer_stat_headers,
508 .stat_show = ksym_tracer_stat_show
511 __init static int ksym_tracer_stat_init(void)
515 ret = register_stat_tracer(&ksym_tracer_stats);
517 printk(KERN_WARNING "Warning: could not register "
518 "ksym tracer stats\n");
524 fs_initcall(ksym_tracer_stat_init);
525 #endif /* CONFIG_PROFILE_KSYM_TRACER */