net: fix for utsrelease.h moving to generated
[linux-2.6-block.git] / kernel / trace / trace_ksym.c
CommitLineData
0722db01
P
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_stat.h"
30#include "trace.h"
31
24f1e32c
FW
32#include <linux/hw_breakpoint.h>
33#include <asm/hw_breakpoint.h>
34
35/*
36 * For now, let us restrict the no. of symbols traced simultaneously to number
0722db01
P
37 * of available hardware breakpoint registers.
38 */
39#define KSYM_TRACER_MAX HBP_NUM
40
41#define KSYM_TRACER_OP_LEN 3 /* rw- */
0722db01 42
db59504d 43struct trace_ksym {
24f1e32c 44 struct perf_event **ksym_hbp;
dd1853c3 45 struct perf_event_attr attr;
db59504d
LZ
46#ifdef CONFIG_PROFILE_KSYM_TRACER
47 unsigned long counter;
48#endif
49 struct hlist_node ksym_hlist;
50};
51
0722db01
P
52static struct trace_array *ksym_trace_array;
53
54static unsigned int ksym_filter_entry_count;
55static unsigned int ksym_tracing_enabled;
56
57static HLIST_HEAD(ksym_filter_head);
58
73874005
FW
59static DEFINE_MUTEX(ksym_tracer_mutex);
60
0722db01
P
61#ifdef CONFIG_PROFILE_KSYM_TRACER
62
63#define MAX_UL_INT 0xffffffff
64
0722db01
P
65void ksym_collect_stats(unsigned long hbp_hit_addr)
66{
67 struct hlist_node *node;
68 struct trace_ksym *entry;
69
70 rcu_read_lock();
71 hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
dd1853c3 72 if ((entry->attr.bp_addr == hbp_hit_addr) &&
0722db01
P
73 (entry->counter <= MAX_UL_INT)) {
74 entry->counter++;
75 break;
76 }
77 }
78 rcu_read_unlock();
79}
80#endif /* CONFIG_PROFILE_KSYM_TRACER */
81
b326e956
FW
82void ksym_hbp_handler(struct perf_event *hbp, int nmi,
83 struct perf_sample_data *data,
84 struct pt_regs *regs)
0722db01
P
85{
86 struct ring_buffer_event *event;
db59504d 87 struct ksym_trace_entry *entry;
d6a65dff 88 struct ring_buffer *buffer;
0722db01
P
89 int pc;
90
91 if (!ksym_tracing_enabled)
92 return;
93
d6a65dff
FW
94 buffer = ksym_trace_array->buffer;
95
0722db01
P
96 pc = preempt_count();
97
d6a65dff 98 event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
0722db01
P
99 sizeof(*entry), 0, pc);
100 if (!event)
101 return;
102
db59504d
LZ
103 entry = ring_buffer_event_data(event);
104 entry->ip = instruction_pointer(regs);
24f1e32c
FW
105 entry->type = hw_breakpoint_type(hbp);
106 entry->addr = hw_breakpoint_addr(hbp);
db59504d
LZ
107 strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
108
0722db01 109#ifdef CONFIG_PROFILE_KSYM_TRACER
24f1e32c 110 ksym_collect_stats(hw_breakpoint_addr(hbp));
0722db01
P
111#endif /* CONFIG_PROFILE_KSYM_TRACER */
112
d6a65dff 113 trace_buffer_unlock_commit(buffer, event, 0, pc);
0722db01
P
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 */
f088e547 124static int ksym_trace_get_access_type(char *str)
0722db01 125{
f088e547 126 int access = 0;
0722db01 127
f088e547 128 if (str[0] == 'r')
24f1e32c 129 access |= HW_BREAKPOINT_R;
f088e547
LZ
130
131 if (str[1] == 'w')
24f1e32c 132 access |= HW_BREAKPOINT_W;
f088e547 133
24f1e32c
FW
134 if (str[2] == 'x')
135 access |= HW_BREAKPOINT_X;
0722db01
P
136
137 switch (access) {
676c0dbe 138 case HW_BREAKPOINT_R:
24f1e32c
FW
139 case HW_BREAKPOINT_W:
140 case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
141 return access;
142 default:
143 return -EINVAL;
0722db01 144 }
0722db01
P
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 */
159static int parse_ksym_trace_str(char *input_string, char **ksymname,
160 unsigned long *addr)
161{
0722db01
P
162 int ret;
163
92cf9f8f 164 *ksymname = strsep(&input_string, ":");
0722db01
P
165 *addr = kallsyms_lookup_name(*ksymname);
166
167 /* Check for malformed request: (2), (1) and (5) */
168 if ((!input_string) ||
92cf9f8f
LZ
169 (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
170 (*addr == 0))
171 return -EINVAL;;
172
0722db01
P
173 ret = ksym_trace_get_access_type(input_string);
174
0722db01
P
175 return ret;
176}
177
178int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
179{
180 struct trace_ksym *entry;
558df6c8 181 int ret = -ENOMEM;
0722db01
P
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
dd1853c3
FW
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;
24f1e32c
FW
199
200 ret = -EAGAIN;
dd1853c3
FW
201 entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr,
202 ksym_hbp_handler);
605bfaee 203
24f1e32c 204 if (IS_ERR(entry->ksym_hbp)) {
24f1e32c 205 ret = PTR_ERR(entry->ksym_hbp);
0722db01
P
206 printk(KERN_INFO "ksym_tracer request failed. Try again"
207 " later!!\n");
558df6c8 208 goto err;
0722db01 209 }
24f1e32c 210
0722db01
P
211 hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
212 ksym_filter_entry_count++;
24f1e32c 213
0722db01 214 return 0;
24f1e32c 215
558df6c8 216err:
558df6c8 217 kfree(entry);
24f1e32c 218
558df6c8 219 return ret;
0722db01
P
220}
221
222static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
223 size_t count, loff_t *ppos)
224{
225 struct trace_ksym *entry;
226 struct hlist_node *node;
be9742e6
LZ
227 struct trace_seq *s;
228 ssize_t cnt = 0;
229 int ret;
230
231 s = kmalloc(sizeof(*s), GFP_KERNEL);
232 if (!s)
233 return -ENOMEM;
234 trace_seq_init(s);
0722db01
P
235
236 mutex_lock(&ksym_tracer_mutex);
237
238 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
dd1853c3
FW
239 ret = trace_seq_printf(s, "%pS:", (void *)entry->attr.bp_addr);
240 if (entry->attr.bp_type == HW_BREAKPOINT_R)
676c0dbe 241 ret = trace_seq_puts(s, "r--\n");
dd1853c3 242 else if (entry->attr.bp_type == HW_BREAKPOINT_W)
be9742e6 243 ret = trace_seq_puts(s, "-w-\n");
dd1853c3 244 else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
be9742e6
LZ
245 ret = trace_seq_puts(s, "rw-\n");
246 WARN_ON_ONCE(!ret);
0722db01 247 }
be9742e6
LZ
248
249 cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
250
0722db01
P
251 mutex_unlock(&ksym_tracer_mutex);
252
be9742e6
LZ
253 kfree(s);
254
255 return cnt;
0722db01
P
256}
257
75e33751
XG
258static 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) {
24f1e32c 266 unregister_wide_hw_breakpoint(entry->ksym_hbp);
75e33751
XG
267 ksym_filter_entry_count--;
268 hlist_del_rcu(&(entry->ksym_hlist));
269 synchronize_rcu();
75e33751
XG
270 kfree(entry);
271 }
272 mutex_unlock(&ksym_tracer_mutex);
273}
274
0722db01
P
275static 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 *input_string, *ksymname = NULL;
282 unsigned long ksym_addr = 0;
283 int ret, op, changed = 0;
284
011ed568 285 input_string = kzalloc(count + 1, GFP_KERNEL);
0722db01
P
286 if (!input_string)
287 return -ENOMEM;
288
289 if (copy_from_user(input_string, buffer, count)) {
290 kfree(input_string);
291 return -EFAULT;
292 }
011ed568 293 input_string[count] = '\0';
0722db01 294
75e33751
XG
295 strstrip(input_string);
296
297 /*
298 * Clear all breakpoints if:
299 * 1: echo > ksym_trace_filter
300 * 2: echo 0 > ksym_trace_filter
301 * 3: echo "*:---" > ksym_trace_filter
302 */
303 if (!input_string[0] || !strcmp(input_string, "0") ||
304 !strcmp(input_string, "*:---")) {
305 __ksym_trace_reset();
306 kfree(input_string);
307 return count;
308 }
309
0722db01
P
310 ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
311 if (ret < 0) {
312 kfree(input_string);
313 return ret;
314 }
315
316 mutex_lock(&ksym_tracer_mutex);
317
318 ret = -EINVAL;
319 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
dd1853c3 320 if (entry->attr.bp_addr == ksym_addr) {
0722db01 321 /* Check for malformed request: (6) */
dd1853c3 322 if (entry->attr.bp_type != op)
0722db01
P
323 changed = 1;
324 else
558df6c8 325 goto out;
0722db01
P
326 break;
327 }
328 }
329 if (changed) {
24f1e32c 330 unregister_wide_hw_breakpoint(entry->ksym_hbp);
dd1853c3 331 entry->attr.bp_type = op;
605bfaee 332 ret = 0;
0722db01 333 if (op > 0) {
24f1e32c 334 entry->ksym_hbp =
dd1853c3
FW
335 register_wide_hw_breakpoint(&entry->attr,
336 ksym_hbp_handler);
24f1e32c 337 if (IS_ERR(entry->ksym_hbp))
605bfaee
FW
338 ret = PTR_ERR(entry->ksym_hbp);
339 else
558df6c8
LZ
340 goto out;
341 }
d99be40a 342 /* Error or "symbol:---" case: drop it */
0722db01
P
343 ksym_filter_entry_count--;
344 hlist_del_rcu(&(entry->ksym_hlist));
345 synchronize_rcu();
0722db01 346 kfree(entry);
558df6c8 347 goto out;
0722db01
P
348 } else {
349 /* Check for malformed request: (4) */
350 if (op == 0)
558df6c8 351 goto out;
0722db01 352 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
0722db01 353 }
558df6c8
LZ
354out:
355 mutex_unlock(&ksym_tracer_mutex);
0722db01 356
0722db01
P
357 kfree(input_string);
358
558df6c8
LZ
359 if (!ret)
360 ret = count;
0722db01
P
361 return ret;
362}
363
364static const struct file_operations ksym_tracing_fops = {
365 .open = tracing_open_generic,
366 .read = ksym_trace_filter_read,
367 .write = ksym_trace_filter_write,
368};
369
370static void ksym_trace_reset(struct trace_array *tr)
371{
0722db01 372 ksym_tracing_enabled = 0;
75e33751 373 __ksym_trace_reset();
0722db01
P
374}
375
376static int ksym_trace_init(struct trace_array *tr)
377{
378 int cpu, ret = 0;
379
380 for_each_online_cpu(cpu)
381 tracing_reset(tr, cpu);
382 ksym_tracing_enabled = 1;
383 ksym_trace_array = tr;
384
385 return ret;
386}
387
388static void ksym_trace_print_header(struct seq_file *m)
389{
0722db01 390 seq_puts(m,
d857ace1
XG
391 "# TASK-PID CPU# Symbol "
392 "Type Function\n");
0722db01 393 seq_puts(m,
d857ace1
XG
394 "# | | | "
395 " | |\n");
0722db01
P
396}
397
398static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
399{
400 struct trace_entry *entry = iter->ent;
401 struct trace_seq *s = &iter->seq;
db59504d 402 struct ksym_trace_entry *field;
0722db01
P
403 char str[KSYM_SYMBOL_LEN];
404 int ret;
405
406 if (entry->type != TRACE_KSYM)
407 return TRACE_TYPE_UNHANDLED;
408
409 trace_assign_type(field, entry);
410
24f1e32c
FW
411 ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
412 entry->pid, iter->cpu, (char *)field->addr);
0722db01
P
413 if (!ret)
414 return TRACE_TYPE_PARTIAL_LINE;
415
db59504d 416 switch (field->type) {
676c0dbe
PM
417 case HW_BREAKPOINT_R:
418 ret = trace_seq_printf(s, " R ");
419 break;
24f1e32c 420 case HW_BREAKPOINT_W:
0722db01
P
421 ret = trace_seq_printf(s, " W ");
422 break;
24f1e32c 423 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
0722db01
P
424 ret = trace_seq_printf(s, " RW ");
425 break;
426 default:
427 return TRACE_TYPE_PARTIAL_LINE;
428 }
429
430 if (!ret)
431 return TRACE_TYPE_PARTIAL_LINE;
432
433 sprint_symbol(str, field->ip);
d857ace1 434 ret = trace_seq_printf(s, "%s\n", str);
0722db01
P
435 if (!ret)
436 return TRACE_TYPE_PARTIAL_LINE;
437
438 return TRACE_TYPE_HANDLED;
439}
440
441struct tracer ksym_tracer __read_mostly =
442{
443 .name = "ksym_tracer",
444 .init = ksym_trace_init,
445 .reset = ksym_trace_reset,
446#ifdef CONFIG_FTRACE_SELFTEST
447 .selftest = trace_selftest_startup_ksym,
448#endif
449 .print_header = ksym_trace_print_header,
450 .print_line = ksym_trace_output
451};
452
453__init static int init_ksym_trace(void)
454{
455 struct dentry *d_tracer;
456 struct dentry *entry;
457
458 d_tracer = tracing_init_dentry();
459 ksym_filter_entry_count = 0;
460
461 entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
462 NULL, &ksym_tracing_fops);
463 if (!entry)
464 pr_warning("Could not create debugfs "
465 "'ksym_trace_filter' file\n");
466
467 return register_tracer(&ksym_tracer);
468}
469device_initcall(init_ksym_trace);
470
471
472#ifdef CONFIG_PROFILE_KSYM_TRACER
473static int ksym_tracer_stat_headers(struct seq_file *m)
474{
9d7e9344
LZ
475 seq_puts(m, " Access Type ");
476 seq_puts(m, " Symbol Counter\n");
477 seq_puts(m, " ----------- ");
478 seq_puts(m, " ------ -------\n");
0722db01
P
479 return 0;
480}
481
482static int ksym_tracer_stat_show(struct seq_file *m, void *v)
483{
484 struct hlist_node *stat = v;
485 struct trace_ksym *entry;
486 int access_type = 0;
487 char fn_name[KSYM_NAME_LEN];
488
489 entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
490
dd1853c3 491 access_type = entry->attr.bp_type;
0722db01
P
492
493 switch (access_type) {
676c0dbe
PM
494 case HW_BREAKPOINT_R:
495 seq_puts(m, " R ");
496 break;
24f1e32c 497 case HW_BREAKPOINT_W:
9d7e9344 498 seq_puts(m, " W ");
0722db01 499 break;
24f1e32c 500 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
9d7e9344 501 seq_puts(m, " RW ");
0722db01
P
502 break;
503 default:
9d7e9344 504 seq_puts(m, " NA ");
0722db01
P
505 }
506
dd1853c3 507 if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
9d7e9344 508 seq_printf(m, " %-36s", fn_name);
0722db01 509 else
9d7e9344
LZ
510 seq_printf(m, " %-36s", "<NA>");
511 seq_printf(m, " %15lu\n", entry->counter);
0722db01 512
0722db01
P
513 return 0;
514}
515
516static void *ksym_tracer_stat_start(struct tracer_stat *trace)
517{
9d7e9344 518 return ksym_filter_head.first;
0722db01
P
519}
520
521static void *
522ksym_tracer_stat_next(void *v, int idx)
523{
524 struct hlist_node *stat = v;
525
526 return stat->next;
527}
528
529static struct tracer_stat ksym_tracer_stats = {
530 .name = "ksym_tracer",
531 .stat_start = ksym_tracer_stat_start,
532 .stat_next = ksym_tracer_stat_next,
533 .stat_headers = ksym_tracer_stat_headers,
534 .stat_show = ksym_tracer_stat_show
535};
536
537__init static int ksym_tracer_stat_init(void)
538{
539 int ret;
540
541 ret = register_stat_tracer(&ksym_tracer_stats);
542 if (ret) {
543 printk(KERN_WARNING "Warning: could not register "
544 "ksym tracer stats\n");
545 return 1;
546 }
547
548 return 0;
549}
550fs_initcall(ksym_tracer_stat_init);
551#endif /* CONFIG_PROFILE_KSYM_TRACER */