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 #include <linux/hw_breakpoint.h>
33 #include <asm/hw_breakpoint.h>
36 * For now, let us restrict the no. of symbols traced simultaneously to number
37 * of available hardware breakpoint registers.
39 #define KSYM_TRACER_MAX HBP_NUM
41 #define KSYM_TRACER_OP_LEN 3 /* rw- */
44 struct perf_event
**ksym_hbp
;
45 unsigned long ksym_addr
;
48 #ifdef CONFIG_PROFILE_KSYM_TRACER
49 unsigned long counter
;
51 struct hlist_node ksym_hlist
;
54 static struct trace_array
*ksym_trace_array
;
56 static unsigned int ksym_filter_entry_count
;
57 static unsigned int ksym_tracing_enabled
;
59 static HLIST_HEAD(ksym_filter_head
);
61 static DEFINE_MUTEX(ksym_tracer_mutex
);
63 #ifdef CONFIG_PROFILE_KSYM_TRACER
65 #define MAX_UL_INT 0xffffffff
67 void ksym_collect_stats(unsigned long hbp_hit_addr
)
69 struct hlist_node
*node
;
70 struct trace_ksym
*entry
;
73 hlist_for_each_entry_rcu(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
74 if ((entry
->ksym_addr
== hbp_hit_addr
) &&
75 (entry
->counter
<= MAX_UL_INT
)) {
82 #endif /* CONFIG_PROFILE_KSYM_TRACER */
84 void ksym_hbp_handler(struct perf_event
*hbp
, void *data
)
86 struct ring_buffer_event
*event
;
87 struct ksym_trace_entry
*entry
;
88 struct pt_regs
*regs
= data
;
89 struct ring_buffer
*buffer
;
92 if (!ksym_tracing_enabled
)
95 buffer
= ksym_trace_array
->buffer
;
99 event
= trace_buffer_lock_reserve(buffer
, TRACE_KSYM
,
100 sizeof(*entry
), 0, pc
);
104 entry
= ring_buffer_event_data(event
);
105 entry
->ip
= instruction_pointer(regs
);
106 entry
->type
= hw_breakpoint_type(hbp
);
107 entry
->addr
= hw_breakpoint_addr(hbp
);
108 strlcpy(entry
->cmd
, current
->comm
, TASK_COMM_LEN
);
110 #ifdef CONFIG_PROFILE_KSYM_TRACER
111 ksym_collect_stats(hw_breakpoint_addr(hbp
));
112 #endif /* CONFIG_PROFILE_KSYM_TRACER */
114 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
117 /* Valid access types are represented as
119 * rw- : Set Read/Write Access Breakpoint
120 * -w- : Set Write Access Breakpoint
121 * --- : Clear Breakpoints
122 * --x : Set Execution Break points (Not available yet)
125 static int ksym_trace_get_access_type(char *str
)
130 access
|= HW_BREAKPOINT_R
;
133 access
|= HW_BREAKPOINT_W
;
136 access
|= HW_BREAKPOINT_X
;
139 case HW_BREAKPOINT_R
:
140 case HW_BREAKPOINT_W
:
141 case HW_BREAKPOINT_W
| HW_BREAKPOINT_R
:
149 * There can be several possible malformed requests and we attempt to capture
150 * all of them. We enumerate some of the rules
151 * 1. We will not allow kernel symbols with ':' since it is used as a delimiter.
152 * i.e. multiple ':' symbols disallowed. Possible uses are of the form
153 * <module>:<ksym_name>:<op>.
154 * 2. No delimiter symbol ':' in the input string
155 * 3. Spurious operator symbols or symbols not in their respective positions
156 * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file
157 * 5. Kernel symbol not a part of /proc/kallsyms
158 * 6. Duplicate requests
160 static int parse_ksym_trace_str(char *input_string
, char **ksymname
,
165 *ksymname
= strsep(&input_string
, ":");
166 *addr
= kallsyms_lookup_name(*ksymname
);
168 /* Check for malformed request: (2), (1) and (5) */
169 if ((!input_string
) ||
170 (strlen(input_string
) != KSYM_TRACER_OP_LEN
) ||
174 ret
= ksym_trace_get_access_type(input_string
);
179 int process_new_ksym_entry(char *ksymname
, int op
, unsigned long addr
)
181 struct trace_ksym
*entry
;
184 if (ksym_filter_entry_count
>= KSYM_TRACER_MAX
) {
185 printk(KERN_ERR
"ksym_tracer: Maximum limit:(%d) reached. No"
186 " new requests for tracing can be accepted now.\n",
191 entry
= kzalloc(sizeof(struct trace_ksym
), GFP_KERNEL
);
196 entry
->ksym_addr
= addr
;
197 entry
->len
= HW_BREAKPOINT_LEN_4
;
200 entry
->ksym_hbp
= register_wide_hw_breakpoint(entry
->ksym_addr
,
201 entry
->len
, entry
->type
,
202 ksym_hbp_handler
, true);
204 if (IS_ERR(entry
->ksym_hbp
)) {
205 ret
= PTR_ERR(entry
->ksym_hbp
);
206 printk(KERN_INFO
"ksym_tracer request failed. Try again"
211 hlist_add_head_rcu(&(entry
->ksym_hlist
), &ksym_filter_head
);
212 ksym_filter_entry_count
++;
222 static ssize_t
ksym_trace_filter_read(struct file
*filp
, char __user
*ubuf
,
223 size_t count
, loff_t
*ppos
)
225 struct trace_ksym
*entry
;
226 struct hlist_node
*node
;
231 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
236 mutex_lock(&ksym_tracer_mutex
);
238 hlist_for_each_entry(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
239 ret
= trace_seq_printf(s
, "%pS:", (void *)entry
->ksym_addr
);
240 if (entry
->type
== HW_BREAKPOINT_R
)
241 ret
= trace_seq_puts(s
, "r--\n");
242 else if (entry
->type
== HW_BREAKPOINT_W
)
243 ret
= trace_seq_puts(s
, "-w-\n");
244 else if (entry
->type
== (HW_BREAKPOINT_W
| HW_BREAKPOINT_R
))
245 ret
= trace_seq_puts(s
, "rw-\n");
249 cnt
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
251 mutex_unlock(&ksym_tracer_mutex
);
258 static void __ksym_trace_reset(void)
260 struct trace_ksym
*entry
;
261 struct hlist_node
*node
, *node1
;
263 mutex_lock(&ksym_tracer_mutex
);
264 hlist_for_each_entry_safe(entry
, node
, node1
, &ksym_filter_head
,
266 unregister_wide_hw_breakpoint(entry
->ksym_hbp
);
267 ksym_filter_entry_count
--;
268 hlist_del_rcu(&(entry
->ksym_hlist
));
272 mutex_unlock(&ksym_tracer_mutex
);
275 static ssize_t
ksym_trace_filter_write(struct file
*file
,
276 const char __user
*buffer
,
277 size_t count
, loff_t
*ppos
)
279 struct trace_ksym
*entry
;
280 struct hlist_node
*node
;
281 char *input_string
, *ksymname
= NULL
;
282 unsigned long ksym_addr
= 0;
283 int ret
, op
, changed
= 0;
285 input_string
= kzalloc(count
+ 1, GFP_KERNEL
);
289 if (copy_from_user(input_string
, buffer
, count
)) {
293 input_string
[count
] = '\0';
295 strstrip(input_string
);
298 * Clear all breakpoints if:
299 * 1: echo > ksym_trace_filter
300 * 2: echo 0 > ksym_trace_filter
301 * 3: echo "*:---" > ksym_trace_filter
303 if (!input_string
[0] || !strcmp(input_string
, "0") ||
304 !strcmp(input_string
, "*:---")) {
305 __ksym_trace_reset();
310 ret
= op
= parse_ksym_trace_str(input_string
, &ksymname
, &ksym_addr
);
316 mutex_lock(&ksym_tracer_mutex
);
319 hlist_for_each_entry(entry
, node
, &ksym_filter_head
, ksym_hlist
) {
320 if (entry
->ksym_addr
== ksym_addr
) {
321 /* Check for malformed request: (6) */
322 if (entry
->type
!= op
)
330 unregister_wide_hw_breakpoint(entry
->ksym_hbp
);
335 register_wide_hw_breakpoint(entry
->ksym_addr
,
336 entry
->len
, entry
->type
,
337 ksym_hbp_handler
, true);
338 if (IS_ERR(entry
->ksym_hbp
))
339 ret
= PTR_ERR(entry
->ksym_hbp
);
343 /* Error or "symbol:---" case: drop it */
344 ksym_filter_entry_count
--;
345 hlist_del_rcu(&(entry
->ksym_hlist
));
350 /* Check for malformed request: (4) */
353 ret
= process_new_ksym_entry(ksymname
, op
, ksym_addr
);
356 mutex_unlock(&ksym_tracer_mutex
);
365 static const struct file_operations ksym_tracing_fops
= {
366 .open
= tracing_open_generic
,
367 .read
= ksym_trace_filter_read
,
368 .write
= ksym_trace_filter_write
,
371 static void ksym_trace_reset(struct trace_array
*tr
)
373 ksym_tracing_enabled
= 0;
374 __ksym_trace_reset();
377 static int ksym_trace_init(struct trace_array
*tr
)
381 for_each_online_cpu(cpu
)
382 tracing_reset(tr
, cpu
);
383 ksym_tracing_enabled
= 1;
384 ksym_trace_array
= tr
;
389 static void ksym_trace_print_header(struct seq_file
*m
)
392 "# TASK-PID CPU# Symbol "
399 static enum print_line_t
ksym_trace_output(struct trace_iterator
*iter
)
401 struct trace_entry
*entry
= iter
->ent
;
402 struct trace_seq
*s
= &iter
->seq
;
403 struct ksym_trace_entry
*field
;
404 char str
[KSYM_SYMBOL_LEN
];
407 if (entry
->type
!= TRACE_KSYM
)
408 return TRACE_TYPE_UNHANDLED
;
410 trace_assign_type(field
, entry
);
412 ret
= trace_seq_printf(s
, "%11s-%-5d [%03d] %pS", field
->cmd
,
413 entry
->pid
, iter
->cpu
, (char *)field
->addr
);
415 return TRACE_TYPE_PARTIAL_LINE
;
417 switch (field
->type
) {
418 case HW_BREAKPOINT_R
:
419 ret
= trace_seq_printf(s
, " R ");
421 case HW_BREAKPOINT_W
:
422 ret
= trace_seq_printf(s
, " W ");
424 case HW_BREAKPOINT_R
| HW_BREAKPOINT_W
:
425 ret
= trace_seq_printf(s
, " RW ");
428 return TRACE_TYPE_PARTIAL_LINE
;
432 return TRACE_TYPE_PARTIAL_LINE
;
434 sprint_symbol(str
, field
->ip
);
435 ret
= trace_seq_printf(s
, "%s\n", str
);
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
,
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
);
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");
483 static int ksym_tracer_stat_show(struct seq_file
*m
, void *v
)
485 struct hlist_node
*stat
= v
;
486 struct trace_ksym
*entry
;
488 char fn_name
[KSYM_NAME_LEN
];
490 entry
= hlist_entry(stat
, struct trace_ksym
, ksym_hlist
);
492 access_type
= entry
->type
;
494 switch (access_type
) {
495 case HW_BREAKPOINT_R
:
498 case HW_BREAKPOINT_W
:
501 case HW_BREAKPOINT_R
| HW_BREAKPOINT_W
:
508 if (lookup_symbol_name(entry
->ksym_addr
, fn_name
) >= 0)
509 seq_printf(m
, " %-36s", fn_name
);
511 seq_printf(m
, " %-36s", "<NA>");
512 seq_printf(m
, " %15lu\n", entry
->counter
);
517 static void *ksym_tracer_stat_start(struct tracer_stat
*trace
)
519 return ksym_filter_head
.first
;
523 ksym_tracer_stat_next(void *v
, int idx
)
525 struct hlist_node
*stat
= v
;
530 static struct tracer_stat ksym_tracer_stats
= {
531 .name
= "ksym_tracer",
532 .stat_start
= ksym_tracer_stat_start
,
533 .stat_next
= ksym_tracer_stat_next
,
534 .stat_headers
= ksym_tracer_stat_headers
,
535 .stat_show
= ksym_tracer_stat_show
538 __init
static int ksym_tracer_stat_init(void)
542 ret
= register_stat_tracer(&ksym_tracer_stats
);
544 printk(KERN_WARNING
"Warning: could not register "
545 "ksym tracer stats\n");
551 fs_initcall(ksym_tracer_stat_init
);
552 #endif /* CONFIG_PROFILE_KSYM_TRACER */