Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / kernel / trace / trace_ksym.c
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/slab.h>
27 #include <linux/fs.h>
28
29 #include "trace_output.h"
30 #include "trace.h"
31
32 #include <linux/hw_breakpoint.h>
33 #include <asm/hw_breakpoint.h>
34
35 #include <asm/atomic.h>
36
37 #define KSYM_TRACER_OP_LEN 3 /* rw- */
38
39 struct trace_ksym {
40         struct perf_event       **ksym_hbp;
41         struct perf_event_attr  attr;
42 #ifdef CONFIG_PROFILE_KSYM_TRACER
43         atomic64_t              counter;
44 #endif
45         struct hlist_node       ksym_hlist;
46 };
47
48 static struct trace_array *ksym_trace_array;
49
50 static unsigned int ksym_tracing_enabled;
51
52 static HLIST_HEAD(ksym_filter_head);
53
54 static DEFINE_MUTEX(ksym_tracer_mutex);
55
56 #ifdef CONFIG_PROFILE_KSYM_TRACER
57
58 #define MAX_UL_INT 0xffffffff
59
60 void ksym_collect_stats(unsigned long hbp_hit_addr)
61 {
62         struct hlist_node *node;
63         struct trace_ksym *entry;
64
65         rcu_read_lock();
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);
69                         break;
70                 }
71         }
72         rcu_read_unlock();
73 }
74 #endif /* CONFIG_PROFILE_KSYM_TRACER */
75
76 void ksym_hbp_handler(struct perf_event *hbp, int nmi,
77                       struct perf_sample_data *data,
78                       struct pt_regs *regs)
79 {
80         struct ring_buffer_event *event;
81         struct ksym_trace_entry *entry;
82         struct ring_buffer *buffer;
83         int pc;
84
85         if (!ksym_tracing_enabled)
86                 return;
87
88         buffer = ksym_trace_array->buffer;
89
90         pc = preempt_count();
91
92         event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
93                                                         sizeof(*entry), 0, pc);
94         if (!event)
95                 return;
96
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);
102
103 #ifdef CONFIG_PROFILE_KSYM_TRACER
104         ksym_collect_stats(hw_breakpoint_addr(hbp));
105 #endif /* CONFIG_PROFILE_KSYM_TRACER */
106
107         trace_buffer_unlock_commit(buffer, event, 0, pc);
108 }
109
110 /* Valid access types are represented as
111  *
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)
116  *
117  */
118 static int ksym_trace_get_access_type(char *str)
119 {
120         int access = 0;
121
122         if (str[0] == 'r')
123                 access |= HW_BREAKPOINT_R;
124
125         if (str[1] == 'w')
126                 access |= HW_BREAKPOINT_W;
127
128         if (str[2] == 'x')
129                 access |= HW_BREAKPOINT_X;
130
131         switch (access) {
132         case HW_BREAKPOINT_R:
133         case HW_BREAKPOINT_W:
134         case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
135                 return access;
136         default:
137                 return -EINVAL;
138         }
139 }
140
141 /*
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
152  */
153 static int parse_ksym_trace_str(char *input_string, char **ksymname,
154                                                         unsigned long *addr)
155 {
156         int ret;
157
158         *ksymname = strsep(&input_string, ":");
159         *addr = kallsyms_lookup_name(*ksymname);
160
161         /* Check for malformed request: (2), (1) and (5) */
162         if ((!input_string) ||
163             (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
164             (*addr == 0))
165                 return -EINVAL;;
166
167         ret = ksym_trace_get_access_type(input_string);
168
169         return ret;
170 }
171
172 int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
173 {
174         struct trace_ksym *entry;
175         int ret = -ENOMEM;
176
177         entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
178         if (!entry)
179                 return -ENOMEM;
180
181         hw_breakpoint_init(&entry->attr);
182
183         entry->attr.bp_type = op;
184         entry->attr.bp_addr = addr;
185         entry->attr.bp_len = HW_BREAKPOINT_LEN_4;
186
187         entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr,
188                                         ksym_hbp_handler);
189
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");
195                 } else {
196                         printk(KERN_INFO "ksym_tracer request failed. Try again"
197                                          " later!!\n");
198                 }
199                 goto err;
200         }
201
202         hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
203
204         return 0;
205
206 err:
207         kfree(entry);
208
209         return ret;
210 }
211
212 static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
213                                                 size_t count, loff_t *ppos)
214 {
215         struct trace_ksym *entry;
216         struct hlist_node *node;
217         struct trace_seq *s;
218         ssize_t cnt = 0;
219         int ret;
220
221         s = kmalloc(sizeof(*s), GFP_KERNEL);
222         if (!s)
223                 return -ENOMEM;
224         trace_seq_init(s);
225
226         mutex_lock(&ksym_tracer_mutex);
227
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");
237                 WARN_ON_ONCE(!ret);
238         }
239
240         cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
241
242         mutex_unlock(&ksym_tracer_mutex);
243
244         kfree(s);
245
246         return cnt;
247 }
248
249 static void __ksym_trace_reset(void)
250 {
251         struct trace_ksym *entry;
252         struct hlist_node *node, *node1;
253
254         mutex_lock(&ksym_tracer_mutex);
255         hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
256                                                                 ksym_hlist) {
257                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
258                 hlist_del_rcu(&(entry->ksym_hlist));
259                 synchronize_rcu();
260                 kfree(entry);
261         }
262         mutex_unlock(&ksym_tracer_mutex);
263 }
264
265 static ssize_t ksym_trace_filter_write(struct file *file,
266                                         const char __user *buffer,
267                                                 size_t count, loff_t *ppos)
268 {
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;
274
275         buf = kzalloc(count + 1, GFP_KERNEL);
276         if (!buf)
277                 return -ENOMEM;
278
279         ret = -EFAULT;
280         if (copy_from_user(buf, buffer, count))
281                 goto out;
282
283         buf[count] = '\0';
284         input_string = strstrip(buf);
285
286         /*
287          * Clear all breakpoints if:
288          * 1: echo > ksym_trace_filter
289          * 2: echo 0 > ksym_trace_filter
290          * 3: echo "*:---" > ksym_trace_filter
291          */
292         if (!input_string[0] || !strcmp(input_string, "0") ||
293             !strcmp(input_string, "*:---")) {
294                 __ksym_trace_reset();
295                 ret = 0;
296                 goto out;
297         }
298
299         ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
300         if (ret < 0)
301                 goto out;
302
303         mutex_lock(&ksym_tracer_mutex);
304
305         ret = -EINVAL;
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)
310                                 changed = 1;
311                         else
312                                 goto out_unlock;
313                         break;
314                 }
315         }
316         if (changed) {
317                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
318                 entry->attr.bp_type = op;
319                 ret = 0;
320                 if (op > 0) {
321                         entry->ksym_hbp =
322                                 register_wide_hw_breakpoint(&entry->attr,
323                                         ksym_hbp_handler);
324                         if (IS_ERR(entry->ksym_hbp))
325                                 ret = PTR_ERR(entry->ksym_hbp);
326                         else
327                                 goto out_unlock;
328                 }
329                 /* Error or "symbol:---" case: drop it */
330                 hlist_del_rcu(&(entry->ksym_hlist));
331                 synchronize_rcu();
332                 kfree(entry);
333                 goto out_unlock;
334         } else {
335                 /* Check for malformed request: (4) */
336                 if (op)
337                         ret = process_new_ksym_entry(ksymname, op, ksym_addr);
338         }
339 out_unlock:
340         mutex_unlock(&ksym_tracer_mutex);
341 out:
342         kfree(buf);
343         return !ret ? count : ret;
344 }
345
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,
350 };
351
352 static void ksym_trace_reset(struct trace_array *tr)
353 {
354         ksym_tracing_enabled = 0;
355         __ksym_trace_reset();
356 }
357
358 static int ksym_trace_init(struct trace_array *tr)
359 {
360         int cpu, ret = 0;
361
362         for_each_online_cpu(cpu)
363                 tracing_reset(tr, cpu);
364         ksym_tracing_enabled = 1;
365         ksym_trace_array = tr;
366
367         return ret;
368 }
369
370 static void ksym_trace_print_header(struct seq_file *m)
371 {
372         seq_puts(m,
373                  "#       TASK-PID   CPU#      Symbol                    "
374                  "Type    Function\n");
375         seq_puts(m,
376                  "#          |        |          |                       "
377                  " |         |\n");
378 }
379
380 static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
381 {
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];
386         int ret;
387
388         if (entry->type != TRACE_KSYM)
389                 return TRACE_TYPE_UNHANDLED;
390
391         trace_assign_type(field, entry);
392
393         ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
394                                 entry->pid, iter->cpu, (char *)field->addr);
395         if (!ret)
396                 return TRACE_TYPE_PARTIAL_LINE;
397
398         switch (field->type) {
399         case HW_BREAKPOINT_R:
400                 ret = trace_seq_printf(s, " R  ");
401                 break;
402         case HW_BREAKPOINT_W:
403                 ret = trace_seq_printf(s, " W  ");
404                 break;
405         case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
406                 ret = trace_seq_printf(s, " RW ");
407                 break;
408         default:
409                 return TRACE_TYPE_PARTIAL_LINE;
410         }
411
412         if (!ret)
413                 return TRACE_TYPE_PARTIAL_LINE;
414
415         sprint_symbol(str, field->ip);
416         ret = trace_seq_printf(s, "%s\n", str);
417         if (!ret)
418                 return TRACE_TYPE_PARTIAL_LINE;
419
420         return TRACE_TYPE_HANDLED;
421 }
422
423 struct tracer ksym_tracer __read_mostly =
424 {
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,
430 #endif
431         .print_header   = ksym_trace_print_header,
432         .print_line     = ksym_trace_output
433 };
434
435 #ifdef CONFIG_PROFILE_KSYM_TRACER
436 static int ksym_profile_show(struct seq_file *m, void *v)
437 {
438         struct hlist_node *node;
439         struct trace_ksym *entry;
440         int access_type = 0;
441         char fn_name[KSYM_NAME_LEN];
442
443         seq_puts(m, "  Access Type ");
444         seq_puts(m, "  Symbol                                       Counter\n");
445         seq_puts(m, "  ----------- ");
446         seq_puts(m, "  ------                                       -------\n");
447
448         rcu_read_lock();
449         hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
450
451                 access_type = entry->attr.bp_type;
452
453                 switch (access_type) {
454                 case HW_BREAKPOINT_R:
455                         seq_puts(m, "  R           ");
456                         break;
457                 case HW_BREAKPOINT_W:
458                         seq_puts(m, "  W           ");
459                         break;
460                 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
461                         seq_puts(m, "  RW          ");
462                         break;
463                 default:
464                         seq_puts(m, "  NA          ");
465                 }
466
467                 if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
468                         seq_printf(m, "  %-36s", fn_name);
469                 else
470                         seq_printf(m, "  %-36s", "<NA>");
471                 seq_printf(m, " %15llu\n",
472                            (unsigned long long)atomic64_read(&entry->counter));
473         }
474         rcu_read_unlock();
475
476         return 0;
477 }
478
479 static int ksym_profile_open(struct inode *node, struct file *file)
480 {
481         return single_open(file, ksym_profile_show, NULL);
482 }
483
484 static const struct file_operations ksym_profile_fops = {
485         .open           = ksym_profile_open,
486         .read           = seq_read,
487         .llseek         = seq_lseek,
488         .release        = single_release,
489 };
490 #endif /* CONFIG_PROFILE_KSYM_TRACER */
491
492 __init static int init_ksym_trace(void)
493 {
494         struct dentry *d_tracer;
495
496         d_tracer = tracing_init_dentry();
497
498         trace_create_file("ksym_trace_filter", 0644, d_tracer,
499                           NULL, &ksym_tracing_fops);
500
501 #ifdef CONFIG_PROFILE_KSYM_TRACER
502         trace_create_file("ksym_profile", 0444, d_tracer,
503                           NULL, &ksym_profile_fops);
504 #endif
505
506         return register_tracer(&ksym_tracer);
507 }
508 device_initcall(init_ksym_trace);