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/slab.h>
29 #include "trace_output.h"
32 #include <linux/hw_breakpoint.h>
33 #include <asm/hw_breakpoint.h>
35 #include <asm/atomic.h>
37 #define KSYM_TRACER_OP_LEN 3 /* rw- */
40 struct perf_event
**ksym_hbp
;
41 struct perf_event_attr attr
;
42 #ifdef CONFIG_PROFILE_KSYM_TRACER
45 struct hlist_node ksym_hlist
;
48 static struct trace_array
*ksym_trace_array
;
50 static unsigned int ksym_tracing_enabled
;
52 static HLIST_HEAD(ksym_filter_head
);
54 static DEFINE_MUTEX(ksym_tracer_mutex
);
56 #ifdef CONFIG_PROFILE_KSYM_TRACER
58 #define MAX_UL_INT 0xffffffff
60 void ksym_collect_stats(unsigned long hbp_hit_addr
)
62 struct hlist_node
*node
;
63 struct trace_ksym
*entry
;
66 hlist_for_each_entry_rcu(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
67 if (entry
->attr
.bp_addr
== hbp_hit_addr
) {
68 atomic64_inc(&entry
->counter
);
74 #endif /* CONFIG_PROFILE_KSYM_TRACER */
76 void ksym_hbp_handler(struct perf_event
*hbp
, int nmi
,
77 struct perf_sample_data
*data
,
80 struct ring_buffer_event
*event
;
81 struct ksym_trace_entry
*entry
;
82 struct ring_buffer
*buffer
;
85 if (!ksym_tracing_enabled
)
88 buffer
= ksym_trace_array
->buffer
;
92 event
= trace_buffer_lock_reserve(buffer
, TRACE_KSYM
,
93 sizeof(*entry
), 0, pc
);
97 entry
= ring_buffer_event_data(event
);
98 entry
->ip
= instruction_pointer(regs
);
99 entry
->type
= hw_breakpoint_type(hbp
);
100 entry
->addr
= hw_breakpoint_addr(hbp
);
101 strlcpy(entry
->cmd
, current
->comm
, TASK_COMM_LEN
);
103 #ifdef CONFIG_PROFILE_KSYM_TRACER
104 ksym_collect_stats(hw_breakpoint_addr(hbp
));
105 #endif /* CONFIG_PROFILE_KSYM_TRACER */
107 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
110 /* Valid access types are represented as
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)
118 static int ksym_trace_get_access_type(char *str
)
123 access
|= HW_BREAKPOINT_R
;
126 access
|= HW_BREAKPOINT_W
;
129 access
|= HW_BREAKPOINT_X
;
132 case HW_BREAKPOINT_R
:
133 case HW_BREAKPOINT_W
:
134 case HW_BREAKPOINT_W
| HW_BREAKPOINT_R
:
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
,
158 *ksymname
= strsep(&input_string
, ":");
159 *addr
= kallsyms_lookup_name(*ksymname
);
161 /* Check for malformed request: (2), (1) and (5) */
162 if ((!input_string
) ||
163 (strlen(input_string
) != KSYM_TRACER_OP_LEN
) ||
167 ret
= ksym_trace_get_access_type(input_string
);
172 int process_new_ksym_entry(char *ksymname
, int op
, unsigned long addr
)
174 struct trace_ksym
*entry
;
177 entry
= kzalloc(sizeof(struct trace_ksym
), GFP_KERNEL
);
181 hw_breakpoint_init(&entry
->attr
);
183 entry
->attr
.bp_type
= op
;
184 entry
->attr
.bp_addr
= addr
;
185 entry
->attr
.bp_len
= HW_BREAKPOINT_LEN_4
;
187 entry
->ksym_hbp
= register_wide_hw_breakpoint(&entry
->attr
,
190 if (IS_ERR(entry
->ksym_hbp
)) {
191 ret
= PTR_ERR(entry
->ksym_hbp
);
192 if (ret
== -ENOSPC
) {
193 printk(KERN_ERR
"ksym_tracer: Maximum limit reached."
194 " No new requests for tracing can be accepted now.\n");
196 printk(KERN_INFO
"ksym_tracer request failed. Try again"
202 hlist_add_head_rcu(&(entry
->ksym_hlist
), &ksym_filter_head
);
212 static ssize_t
ksym_trace_filter_read(struct file
*filp
, char __user
*ubuf
,
213 size_t count
, loff_t
*ppos
)
215 struct trace_ksym
*entry
;
216 struct hlist_node
*node
;
221 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
226 mutex_lock(&ksym_tracer_mutex
);
228 hlist_for_each_entry(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
229 ret
= trace_seq_printf(s
, "%pS:",
230 (void *)(unsigned long)entry
->attr
.bp_addr
);
231 if (entry
->attr
.bp_type
== HW_BREAKPOINT_R
)
232 ret
= trace_seq_puts(s
, "r--\n");
233 else if (entry
->attr
.bp_type
== HW_BREAKPOINT_W
)
234 ret
= trace_seq_puts(s
, "-w-\n");
235 else if (entry
->attr
.bp_type
== (HW_BREAKPOINT_W
| HW_BREAKPOINT_R
))
236 ret
= trace_seq_puts(s
, "rw-\n");
240 cnt
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
242 mutex_unlock(&ksym_tracer_mutex
);
249 static void __ksym_trace_reset(void)
251 struct trace_ksym
*entry
;
252 struct hlist_node
*node
, *node1
;
254 mutex_lock(&ksym_tracer_mutex
);
255 hlist_for_each_entry_safe(entry
, node
, node1
, &ksym_filter_head
,
257 unregister_wide_hw_breakpoint(entry
->ksym_hbp
);
258 hlist_del_rcu(&(entry
->ksym_hlist
));
262 mutex_unlock(&ksym_tracer_mutex
);
265 static ssize_t
ksym_trace_filter_write(struct file
*file
,
266 const char __user
*buffer
,
267 size_t count
, loff_t
*ppos
)
269 struct trace_ksym
*entry
;
270 struct hlist_node
*node
;
271 char *buf
, *input_string
, *ksymname
= NULL
;
272 unsigned long ksym_addr
= 0;
273 int ret
, op
, changed
= 0;
275 buf
= kzalloc(count
+ 1, GFP_KERNEL
);
280 if (copy_from_user(buf
, buffer
, count
))
284 input_string
= strstrip(buf
);
287 * Clear all breakpoints if:
288 * 1: echo > ksym_trace_filter
289 * 2: echo 0 > ksym_trace_filter
290 * 3: echo "*:---" > ksym_trace_filter
292 if (!input_string
[0] || !strcmp(input_string
, "0") ||
293 !strcmp(input_string
, "*:---")) {
294 __ksym_trace_reset();
299 ret
= op
= parse_ksym_trace_str(input_string
, &ksymname
, &ksym_addr
);
303 mutex_lock(&ksym_tracer_mutex
);
306 hlist_for_each_entry(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
307 if (entry
->attr
.bp_addr
== ksym_addr
) {
308 /* Check for malformed request: (6) */
309 if (entry
->attr
.bp_type
!= op
)
317 unregister_wide_hw_breakpoint(entry
->ksym_hbp
);
318 entry
->attr
.bp_type
= op
;
322 register_wide_hw_breakpoint(&entry
->attr
,
324 if (IS_ERR(entry
->ksym_hbp
))
325 ret
= PTR_ERR(entry
->ksym_hbp
);
329 /* Error or "symbol:---" case: drop it */
330 hlist_del_rcu(&(entry
->ksym_hlist
));
335 /* Check for malformed request: (4) */
337 ret
= process_new_ksym_entry(ksymname
, op
, ksym_addr
);
340 mutex_unlock(&ksym_tracer_mutex
);
343 return !ret
? count
: ret
;
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
,
352 static void ksym_trace_reset(struct trace_array
*tr
)
354 ksym_tracing_enabled
= 0;
355 __ksym_trace_reset();
358 static int ksym_trace_init(struct trace_array
*tr
)
362 for_each_online_cpu(cpu
)
363 tracing_reset(tr
, cpu
);
364 ksym_tracing_enabled
= 1;
365 ksym_trace_array
= tr
;
370 static void ksym_trace_print_header(struct seq_file
*m
)
373 "# TASK-PID CPU# Symbol "
380 static enum print_line_t
ksym_trace_output(struct trace_iterator
*iter
)
382 struct trace_entry
*entry
= iter
->ent
;
383 struct trace_seq
*s
= &iter
->seq
;
384 struct ksym_trace_entry
*field
;
385 char str
[KSYM_SYMBOL_LEN
];
388 if (entry
->type
!= TRACE_KSYM
)
389 return TRACE_TYPE_UNHANDLED
;
391 trace_assign_type(field
, entry
);
393 ret
= trace_seq_printf(s
, "%11s-%-5d [%03d] %pS", field
->cmd
,
394 entry
->pid
, iter
->cpu
, (char *)field
->addr
);
396 return TRACE_TYPE_PARTIAL_LINE
;
398 switch (field
->type
) {
399 case HW_BREAKPOINT_R
:
400 ret
= trace_seq_printf(s
, " R ");
402 case HW_BREAKPOINT_W
:
403 ret
= trace_seq_printf(s
, " W ");
405 case HW_BREAKPOINT_R
| HW_BREAKPOINT_W
:
406 ret
= trace_seq_printf(s
, " RW ");
409 return TRACE_TYPE_PARTIAL_LINE
;
413 return TRACE_TYPE_PARTIAL_LINE
;
415 sprint_symbol(str
, field
->ip
);
416 ret
= trace_seq_printf(s
, "%s\n", str
);
418 return TRACE_TYPE_PARTIAL_LINE
;
420 return TRACE_TYPE_HANDLED
;
423 struct tracer ksym_tracer __read_mostly
=
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
,
431 .print_header
= ksym_trace_print_header
,
432 .print_line
= ksym_trace_output
435 #ifdef CONFIG_PROFILE_KSYM_TRACER
436 static int ksym_profile_show(struct seq_file
*m
, void *v
)
438 struct hlist_node
*node
;
439 struct trace_ksym
*entry
;
441 char fn_name
[KSYM_NAME_LEN
];
443 seq_puts(m
, " Access Type ");
444 seq_puts(m
, " Symbol Counter\n");
445 seq_puts(m
, " ----------- ");
446 seq_puts(m
, " ------ -------\n");
449 hlist_for_each_entry_rcu(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
451 access_type
= entry
->attr
.bp_type
;
453 switch (access_type
) {
454 case HW_BREAKPOINT_R
:
457 case HW_BREAKPOINT_W
:
460 case HW_BREAKPOINT_R
| HW_BREAKPOINT_W
:
467 if (lookup_symbol_name(entry
->attr
.bp_addr
, fn_name
) >= 0)
468 seq_printf(m
, " %-36s", fn_name
);
470 seq_printf(m
, " %-36s", "<NA>");
471 seq_printf(m
, " %15llu\n",
472 (unsigned long long)atomic64_read(&entry
->counter
));
479 static int ksym_profile_open(struct inode
*node
, struct file
*file
)
481 return single_open(file
, ksym_profile_show
, NULL
);
484 static const struct file_operations ksym_profile_fops
= {
485 .open
= ksym_profile_open
,
488 .release
= single_release
,
490 #endif /* CONFIG_PROFILE_KSYM_TRACER */
492 __init
static int init_ksym_trace(void)
494 struct dentry
*d_tracer
;
496 d_tracer
= tracing_init_dentry();
498 trace_create_file("ksym_trace_filter", 0644, d_tracer
,
499 NULL
, &ksym_tracing_fops
);
501 #ifdef CONFIG_PROFILE_KSYM_TRACER
502 trace_create_file("ksym_profile", 0444, d_tracer
,
503 NULL
, &ksym_profile_fops
);
506 return register_tracer(&ksym_tracer
);
508 device_initcall(init_ksym_trace
);