ARM: Merge for-2635/samsung-hwmon
[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>
5a0e3ad6 26#include <linux/slab.h>
0722db01
P
27#include <linux/fs.h>
28
29#include "trace_output.h"
0722db01
P
30#include "trace.h"
31
24f1e32c
FW
32#include <linux/hw_breakpoint.h>
33#include <asm/hw_breakpoint.h>
34
e6d9491b
LZ
35#include <asm/atomic.h>
36
24f1e32c
FW
37/*
38 * For now, let us restrict the no. of symbols traced simultaneously to number
0722db01
P
39 * of available hardware breakpoint registers.
40 */
41#define KSYM_TRACER_MAX HBP_NUM
42
43#define KSYM_TRACER_OP_LEN 3 /* rw- */
0722db01 44
db59504d 45struct trace_ksym {
24f1e32c 46 struct perf_event **ksym_hbp;
dd1853c3 47 struct perf_event_attr attr;
db59504d 48#ifdef CONFIG_PROFILE_KSYM_TRACER
e6d9491b 49 atomic64_t counter;
db59504d
LZ
50#endif
51 struct hlist_node ksym_hlist;
52};
53
0722db01
P
54static struct trace_array *ksym_trace_array;
55
56static unsigned int ksym_filter_entry_count;
57static unsigned int ksym_tracing_enabled;
58
59static HLIST_HEAD(ksym_filter_head);
60
73874005
FW
61static DEFINE_MUTEX(ksym_tracer_mutex);
62
0722db01
P
63#ifdef CONFIG_PROFILE_KSYM_TRACER
64
65#define MAX_UL_INT 0xffffffff
66
0722db01
P
67void ksym_collect_stats(unsigned long hbp_hit_addr)
68{
69 struct hlist_node *node;
70 struct trace_ksym *entry;
71
72 rcu_read_lock();
73 hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
e6d9491b
LZ
74 if (entry->attr.bp_addr == hbp_hit_addr) {
75 atomic64_inc(&entry->counter);
0722db01
P
76 break;
77 }
78 }
79 rcu_read_unlock();
80}
81#endif /* CONFIG_PROFILE_KSYM_TRACER */
82
b326e956
FW
83void ksym_hbp_handler(struct perf_event *hbp, int nmi,
84 struct perf_sample_data *data,
85 struct pt_regs *regs)
0722db01
P
86{
87 struct ring_buffer_event *event;
db59504d 88 struct ksym_trace_entry *entry;
d6a65dff 89 struct ring_buffer *buffer;
0722db01
P
90 int pc;
91
92 if (!ksym_tracing_enabled)
93 return;
94
d6a65dff
FW
95 buffer = ksym_trace_array->buffer;
96
0722db01
P
97 pc = preempt_count();
98
d6a65dff 99 event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
0722db01
P
100 sizeof(*entry), 0, pc);
101 if (!event)
102 return;
103
db59504d
LZ
104 entry = ring_buffer_event_data(event);
105 entry->ip = instruction_pointer(regs);
24f1e32c
FW
106 entry->type = hw_breakpoint_type(hbp);
107 entry->addr = hw_breakpoint_addr(hbp);
db59504d
LZ
108 strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
109
0722db01 110#ifdef CONFIG_PROFILE_KSYM_TRACER
24f1e32c 111 ksym_collect_stats(hw_breakpoint_addr(hbp));
0722db01
P
112#endif /* CONFIG_PROFILE_KSYM_TRACER */
113
d6a65dff 114 trace_buffer_unlock_commit(buffer, event, 0, pc);
0722db01
P
115}
116
117/* Valid access types are represented as
118 *
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)
123 *
124 */
f088e547 125static int ksym_trace_get_access_type(char *str)
0722db01 126{
f088e547 127 int access = 0;
0722db01 128
f088e547 129 if (str[0] == 'r')
24f1e32c 130 access |= HW_BREAKPOINT_R;
f088e547
LZ
131
132 if (str[1] == 'w')
24f1e32c 133 access |= HW_BREAKPOINT_W;
f088e547 134
24f1e32c
FW
135 if (str[2] == 'x')
136 access |= HW_BREAKPOINT_X;
0722db01
P
137
138 switch (access) {
676c0dbe 139 case HW_BREAKPOINT_R:
24f1e32c
FW
140 case HW_BREAKPOINT_W:
141 case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
142 return access;
143 default:
144 return -EINVAL;
0722db01 145 }
0722db01
P
146}
147
148/*
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
159 */
160static int parse_ksym_trace_str(char *input_string, char **ksymname,
161 unsigned long *addr)
162{
0722db01
P
163 int ret;
164
92cf9f8f 165 *ksymname = strsep(&input_string, ":");
0722db01
P
166 *addr = kallsyms_lookup_name(*ksymname);
167
168 /* Check for malformed request: (2), (1) and (5) */
169 if ((!input_string) ||
92cf9f8f
LZ
170 (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
171 (*addr == 0))
172 return -EINVAL;;
173
0722db01
P
174 ret = ksym_trace_get_access_type(input_string);
175
0722db01
P
176 return ret;
177}
178
179int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
180{
181 struct trace_ksym *entry;
558df6c8 182 int ret = -ENOMEM;
0722db01
P
183
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",
187 KSYM_TRACER_MAX);
188 return -ENOSPC;
189 }
190
191 entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
192 if (!entry)
193 return -ENOMEM;
194
dd1853c3
FW
195 hw_breakpoint_init(&entry->attr);
196
197 entry->attr.bp_type = op;
198 entry->attr.bp_addr = addr;
199 entry->attr.bp_len = HW_BREAKPOINT_LEN_4;
24f1e32c 200
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) {
16620e0f
LZ
239 ret = trace_seq_printf(s, "%pS:",
240 (void *)(unsigned long)entry->attr.bp_addr);
dd1853c3 241 if (entry->attr.bp_type == HW_BREAKPOINT_R)
676c0dbe 242 ret = trace_seq_puts(s, "r--\n");
dd1853c3 243 else if (entry->attr.bp_type == HW_BREAKPOINT_W)
be9742e6 244 ret = trace_seq_puts(s, "-w-\n");
dd1853c3 245 else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
be9742e6
LZ
246 ret = trace_seq_puts(s, "rw-\n");
247 WARN_ON_ONCE(!ret);
0722db01 248 }
be9742e6
LZ
249
250 cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
251
0722db01
P
252 mutex_unlock(&ksym_tracer_mutex);
253
be9742e6
LZ
254 kfree(s);
255
256 return cnt;
0722db01
P
257}
258
75e33751
XG
259static void __ksym_trace_reset(void)
260{
261 struct trace_ksym *entry;
262 struct hlist_node *node, *node1;
263
264 mutex_lock(&ksym_tracer_mutex);
265 hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
266 ksym_hlist) {
24f1e32c 267 unregister_wide_hw_breakpoint(entry->ksym_hbp);
75e33751
XG
268 ksym_filter_entry_count--;
269 hlist_del_rcu(&(entry->ksym_hlist));
270 synchronize_rcu();
75e33751
XG
271 kfree(entry);
272 }
273 mutex_unlock(&ksym_tracer_mutex);
274}
275
0722db01
P
276static ssize_t ksym_trace_filter_write(struct file *file,
277 const char __user *buffer,
278 size_t count, loff_t *ppos)
279{
280 struct trace_ksym *entry;
281 struct hlist_node *node;
d954fbf0 282 char *buf, *input_string, *ksymname = NULL;
0722db01
P
283 unsigned long ksym_addr = 0;
284 int ret, op, changed = 0;
285
d954fbf0
TG
286 buf = kzalloc(count + 1, GFP_KERNEL);
287 if (!buf)
0722db01
P
288 return -ENOMEM;
289
d954fbf0
TG
290 ret = -EFAULT;
291 if (copy_from_user(buf, buffer, count))
292 goto out;
0722db01 293
d954fbf0
TG
294 buf[count] = '\0';
295 input_string = strstrip(buf);
75e33751
XG
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 */
3d13ec2e
LZ
303 if (!input_string[0] || !strcmp(input_string, "0") ||
304 !strcmp(input_string, "*:---")) {
75e33751 305 __ksym_trace_reset();
d954fbf0
TG
306 ret = 0;
307 goto out;
75e33751
XG
308 }
309
0722db01 310 ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
d954fbf0
TG
311 if (ret < 0)
312 goto out;
0722db01
P
313
314 mutex_lock(&ksym_tracer_mutex);
315
316 ret = -EINVAL;
317 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
dd1853c3 318 if (entry->attr.bp_addr == ksym_addr) {
0722db01 319 /* Check for malformed request: (6) */
dd1853c3 320 if (entry->attr.bp_type != op)
0722db01
P
321 changed = 1;
322 else
d954fbf0 323 goto out_unlock;
0722db01
P
324 break;
325 }
326 }
327 if (changed) {
24f1e32c 328 unregister_wide_hw_breakpoint(entry->ksym_hbp);
dd1853c3 329 entry->attr.bp_type = op;
605bfaee 330 ret = 0;
0722db01 331 if (op > 0) {
24f1e32c 332 entry->ksym_hbp =
dd1853c3
FW
333 register_wide_hw_breakpoint(&entry->attr,
334 ksym_hbp_handler);
24f1e32c 335 if (IS_ERR(entry->ksym_hbp))
605bfaee
FW
336 ret = PTR_ERR(entry->ksym_hbp);
337 else
d954fbf0 338 goto out_unlock;
558df6c8 339 }
d99be40a 340 /* Error or "symbol:---" case: drop it */
0722db01
P
341 ksym_filter_entry_count--;
342 hlist_del_rcu(&(entry->ksym_hlist));
343 synchronize_rcu();
0722db01 344 kfree(entry);
d954fbf0 345 goto out_unlock;
0722db01
P
346 } else {
347 /* Check for malformed request: (4) */
d954fbf0
TG
348 if (op)
349 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
0722db01 350 }
d954fbf0 351out_unlock:
558df6c8 352 mutex_unlock(&ksym_tracer_mutex);
d954fbf0
TG
353out:
354 kfree(buf);
355 return !ret ? count : ret;
0722db01
P
356}
357
358static const struct file_operations ksym_tracing_fops = {
359 .open = tracing_open_generic,
360 .read = ksym_trace_filter_read,
361 .write = ksym_trace_filter_write,
362};
363
364static void ksym_trace_reset(struct trace_array *tr)
365{
0722db01 366 ksym_tracing_enabled = 0;
75e33751 367 __ksym_trace_reset();
0722db01
P
368}
369
370static int ksym_trace_init(struct trace_array *tr)
371{
372 int cpu, ret = 0;
373
374 for_each_online_cpu(cpu)
375 tracing_reset(tr, cpu);
376 ksym_tracing_enabled = 1;
377 ksym_trace_array = tr;
378
379 return ret;
380}
381
382static void ksym_trace_print_header(struct seq_file *m)
383{
0722db01 384 seq_puts(m,
d857ace1
XG
385 "# TASK-PID CPU# Symbol "
386 "Type Function\n");
0722db01 387 seq_puts(m,
d857ace1
XG
388 "# | | | "
389 " | |\n");
0722db01
P
390}
391
392static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
393{
394 struct trace_entry *entry = iter->ent;
395 struct trace_seq *s = &iter->seq;
db59504d 396 struct ksym_trace_entry *field;
0722db01
P
397 char str[KSYM_SYMBOL_LEN];
398 int ret;
399
400 if (entry->type != TRACE_KSYM)
401 return TRACE_TYPE_UNHANDLED;
402
403 trace_assign_type(field, entry);
404
24f1e32c
FW
405 ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
406 entry->pid, iter->cpu, (char *)field->addr);
0722db01
P
407 if (!ret)
408 return TRACE_TYPE_PARTIAL_LINE;
409
db59504d 410 switch (field->type) {
676c0dbe
PM
411 case HW_BREAKPOINT_R:
412 ret = trace_seq_printf(s, " R ");
413 break;
24f1e32c 414 case HW_BREAKPOINT_W:
0722db01
P
415 ret = trace_seq_printf(s, " W ");
416 break;
24f1e32c 417 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
0722db01
P
418 ret = trace_seq_printf(s, " RW ");
419 break;
420 default:
421 return TRACE_TYPE_PARTIAL_LINE;
422 }
423
424 if (!ret)
425 return TRACE_TYPE_PARTIAL_LINE;
426
427 sprint_symbol(str, field->ip);
d857ace1 428 ret = trace_seq_printf(s, "%s\n", str);
0722db01
P
429 if (!ret)
430 return TRACE_TYPE_PARTIAL_LINE;
431
432 return TRACE_TYPE_HANDLED;
433}
434
435struct tracer ksym_tracer __read_mostly =
436{
437 .name = "ksym_tracer",
438 .init = ksym_trace_init,
439 .reset = ksym_trace_reset,
440#ifdef CONFIG_FTRACE_SELFTEST
441 .selftest = trace_selftest_startup_ksym,
442#endif
443 .print_header = ksym_trace_print_header,
444 .print_line = ksym_trace_output
445};
446
0722db01 447#ifdef CONFIG_PROFILE_KSYM_TRACER
53ab6680 448static int ksym_profile_show(struct seq_file *m, void *v)
0722db01 449{
53ab6680
LZ
450 struct hlist_node *node;
451 struct trace_ksym *entry;
452 int access_type = 0;
453 char fn_name[KSYM_NAME_LEN];
454
9d7e9344
LZ
455 seq_puts(m, " Access Type ");
456 seq_puts(m, " Symbol Counter\n");
457 seq_puts(m, " ----------- ");
458 seq_puts(m, " ------ -------\n");
0722db01 459
53ab6680
LZ
460 rcu_read_lock();
461 hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
0722db01 462
53ab6680 463 access_type = entry->attr.bp_type;
0722db01 464
53ab6680
LZ
465 switch (access_type) {
466 case HW_BREAKPOINT_R:
467 seq_puts(m, " R ");
468 break;
469 case HW_BREAKPOINT_W:
470 seq_puts(m, " W ");
471 break;
472 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
473 seq_puts(m, " RW ");
474 break;
475 default:
476 seq_puts(m, " NA ");
477 }
0722db01 478
53ab6680
LZ
479 if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
480 seq_printf(m, " %-36s", fn_name);
481 else
482 seq_printf(m, " %-36s", "<NA>");
483 seq_printf(m, " %15llu\n",
484 (unsigned long long)atomic64_read(&entry->counter));
0722db01 485 }
53ab6680 486 rcu_read_unlock();
0722db01 487
0722db01
P
488 return 0;
489}
490
53ab6680 491static int ksym_profile_open(struct inode *node, struct file *file)
0722db01 492{
53ab6680 493 return single_open(file, ksym_profile_show, NULL);
0722db01
P
494}
495
53ab6680
LZ
496static const struct file_operations ksym_profile_fops = {
497 .open = ksym_profile_open,
498 .read = seq_read,
499 .llseek = seq_lseek,
500 .release = single_release,
0722db01 501};
53ab6680 502#endif /* CONFIG_PROFILE_KSYM_TRACER */
0722db01 503
53ab6680 504__init static int init_ksym_trace(void)
0722db01 505{
53ab6680 506 struct dentry *d_tracer;
0722db01 507
53ab6680 508 d_tracer = tracing_init_dentry();
0722db01 509
53ab6680
LZ
510 trace_create_file("ksym_trace_filter", 0644, d_tracer,
511 NULL, &ksym_tracing_fops);
512
513#ifdef CONFIG_PROFILE_KSYM_TRACER
514 trace_create_file("ksym_profile", 0444, d_tracer,
515 NULL, &ksym_profile_fops);
516#endif
517
518 return register_tracer(&ksym_tracer);
0722db01 519}
53ab6680 520device_initcall(init_ksym_trace);