Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.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/fs.h>
27
28 #include "trace_output.h"
29 #include "trace_stat.h"
30 #include "trace.h"
31
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
37  * of available hardware breakpoint registers.
38  */
39 #define KSYM_TRACER_MAX HBP_NUM
40
41 #define KSYM_TRACER_OP_LEN 3 /* rw- */
42
43 struct trace_ksym {
44         struct perf_event       **ksym_hbp;
45         struct perf_event_attr  attr;
46 #ifdef CONFIG_PROFILE_KSYM_TRACER
47         unsigned long           counter;
48 #endif
49         struct hlist_node       ksym_hlist;
50 };
51
52 static struct trace_array *ksym_trace_array;
53
54 static unsigned int ksym_filter_entry_count;
55 static unsigned int ksym_tracing_enabled;
56
57 static HLIST_HEAD(ksym_filter_head);
58
59 static DEFINE_MUTEX(ksym_tracer_mutex);
60
61 #ifdef CONFIG_PROFILE_KSYM_TRACER
62
63 #define MAX_UL_INT 0xffffffff
64
65 void 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) {
72                 if ((entry->attr.bp_addr == hbp_hit_addr) &&
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
82 void ksym_hbp_handler(struct perf_event *hbp, int nmi,
83                       struct perf_sample_data *data,
84                       struct pt_regs *regs)
85 {
86         struct ring_buffer_event *event;
87         struct ksym_trace_entry *entry;
88         struct ring_buffer *buffer;
89         int pc;
90
91         if (!ksym_tracing_enabled)
92                 return;
93
94         buffer = ksym_trace_array->buffer;
95
96         pc = preempt_count();
97
98         event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
99                                                         sizeof(*entry), 0, pc);
100         if (!event)
101                 return;
102
103         entry           = ring_buffer_event_data(event);
104         entry->ip       = instruction_pointer(regs);
105         entry->type     = hw_breakpoint_type(hbp);
106         entry->addr     = hw_breakpoint_addr(hbp);
107         strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
108
109 #ifdef CONFIG_PROFILE_KSYM_TRACER
110         ksym_collect_stats(hw_breakpoint_addr(hbp));
111 #endif /* CONFIG_PROFILE_KSYM_TRACER */
112
113         trace_buffer_unlock_commit(buffer, event, 0, pc);
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  */
124 static int ksym_trace_get_access_type(char *str)
125 {
126         int access = 0;
127
128         if (str[0] == 'r')
129                 access |= HW_BREAKPOINT_R;
130
131         if (str[1] == 'w')
132                 access |= HW_BREAKPOINT_W;
133
134         if (str[2] == 'x')
135                 access |= HW_BREAKPOINT_X;
136
137         switch (access) {
138         case HW_BREAKPOINT_R:
139         case HW_BREAKPOINT_W:
140         case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
141                 return access;
142         default:
143                 return -EINVAL;
144         }
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  */
159 static int parse_ksym_trace_str(char *input_string, char **ksymname,
160                                                         unsigned long *addr)
161 {
162         int ret;
163
164         *ksymname = strsep(&input_string, ":");
165         *addr = kallsyms_lookup_name(*ksymname);
166
167         /* Check for malformed request: (2), (1) and (5) */
168         if ((!input_string) ||
169             (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
170             (*addr == 0))
171                 return -EINVAL;;
172
173         ret = ksym_trace_get_access_type(input_string);
174
175         return ret;
176 }
177
178 int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
179 {
180         struct trace_ksym *entry;
181         int ret = -ENOMEM;
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
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;
199
200         ret = -EAGAIN;
201         entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr,
202                                         ksym_hbp_handler);
203
204         if (IS_ERR(entry->ksym_hbp)) {
205                 ret = PTR_ERR(entry->ksym_hbp);
206                 printk(KERN_INFO "ksym_tracer request failed. Try again"
207                                         " later!!\n");
208                 goto err;
209         }
210
211         hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
212         ksym_filter_entry_count++;
213
214         return 0;
215
216 err:
217         kfree(entry);
218
219         return ret;
220 }
221
222 static 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;
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);
235
236         mutex_lock(&ksym_tracer_mutex);
237
238         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
239                 ret = trace_seq_printf(s, "%pS:",
240                                 (void *)(unsigned long)entry->attr.bp_addr);
241                 if (entry->attr.bp_type == HW_BREAKPOINT_R)
242                         ret = trace_seq_puts(s, "r--\n");
243                 else if (entry->attr.bp_type == HW_BREAKPOINT_W)
244                         ret = trace_seq_puts(s, "-w-\n");
245                 else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
246                         ret = trace_seq_puts(s, "rw-\n");
247                 WARN_ON_ONCE(!ret);
248         }
249
250         cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
251
252         mutex_unlock(&ksym_tracer_mutex);
253
254         kfree(s);
255
256         return cnt;
257 }
258
259 static 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) {
267                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
268                 ksym_filter_entry_count--;
269                 hlist_del_rcu(&(entry->ksym_hlist));
270                 synchronize_rcu();
271                 kfree(entry);
272         }
273         mutex_unlock(&ksym_tracer_mutex);
274 }
275
276 static 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;
282         char *buf, *input_string, *ksymname = NULL;
283         unsigned long ksym_addr = 0;
284         int ret, op, changed = 0;
285
286         buf = kzalloc(count + 1, GFP_KERNEL);
287         if (!buf)
288                 return -ENOMEM;
289
290         ret = -EFAULT;
291         if (copy_from_user(buf, buffer, count))
292                 goto out;
293
294         buf[count] = '\0';
295         input_string = strstrip(buf);
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 (!buf[0] || !strcmp(buf, "0") ||
304             !strcmp(buf, "*:---")) {
305                 __ksym_trace_reset();
306                 ret = 0;
307                 goto out;
308         }
309
310         ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
311         if (ret < 0)
312                 goto out;
313
314         mutex_lock(&ksym_tracer_mutex);
315
316         ret = -EINVAL;
317         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
318                 if (entry->attr.bp_addr == ksym_addr) {
319                         /* Check for malformed request: (6) */
320                         if (entry->attr.bp_type != op)
321                                 changed = 1;
322                         else
323                                 goto out_unlock;
324                         break;
325                 }
326         }
327         if (changed) {
328                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
329                 entry->attr.bp_type = op;
330                 ret = 0;
331                 if (op > 0) {
332                         entry->ksym_hbp =
333                                 register_wide_hw_breakpoint(&entry->attr,
334                                         ksym_hbp_handler);
335                         if (IS_ERR(entry->ksym_hbp))
336                                 ret = PTR_ERR(entry->ksym_hbp);
337                         else
338                                 goto out_unlock;
339                 }
340                 /* Error or "symbol:---" case: drop it */
341                 ksym_filter_entry_count--;
342                 hlist_del_rcu(&(entry->ksym_hlist));
343                 synchronize_rcu();
344                 kfree(entry);
345                 goto out_unlock;
346         } else {
347                 /* Check for malformed request: (4) */
348                 if (op)
349                         ret = process_new_ksym_entry(ksymname, op, ksym_addr);
350         }
351 out_unlock:
352         mutex_unlock(&ksym_tracer_mutex);
353 out:
354         kfree(buf);
355         return !ret ? count : ret;
356 }
357
358 static 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
364 static void ksym_trace_reset(struct trace_array *tr)
365 {
366         ksym_tracing_enabled = 0;
367         __ksym_trace_reset();
368 }
369
370 static 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
382 static void ksym_trace_print_header(struct seq_file *m)
383 {
384         seq_puts(m,
385                  "#       TASK-PID   CPU#      Symbol                    "
386                  "Type    Function\n");
387         seq_puts(m,
388                  "#          |        |          |                       "
389                  " |         |\n");
390 }
391
392 static 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;
396         struct ksym_trace_entry *field;
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
405         ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
406                                 entry->pid, iter->cpu, (char *)field->addr);
407         if (!ret)
408                 return TRACE_TYPE_PARTIAL_LINE;
409
410         switch (field->type) {
411         case HW_BREAKPOINT_R:
412                 ret = trace_seq_printf(s, " R  ");
413                 break;
414         case HW_BREAKPOINT_W:
415                 ret = trace_seq_printf(s, " W  ");
416                 break;
417         case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
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);
428         ret = trace_seq_printf(s, "%s\n", str);
429         if (!ret)
430                 return TRACE_TYPE_PARTIAL_LINE;
431
432         return TRACE_TYPE_HANDLED;
433 }
434
435 struct 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
447 __init static int init_ksym_trace(void)
448 {
449         struct dentry *d_tracer;
450         struct dentry *entry;
451
452         d_tracer = tracing_init_dentry();
453         ksym_filter_entry_count = 0;
454
455         entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
456                                     NULL, &ksym_tracing_fops);
457         if (!entry)
458                 pr_warning("Could not create debugfs "
459                            "'ksym_trace_filter' file\n");
460
461         return register_tracer(&ksym_tracer);
462 }
463 device_initcall(init_ksym_trace);
464
465
466 #ifdef CONFIG_PROFILE_KSYM_TRACER
467 static int ksym_tracer_stat_headers(struct seq_file *m)
468 {
469         seq_puts(m, "  Access Type ");
470         seq_puts(m, "  Symbol                                       Counter\n");
471         seq_puts(m, "  ----------- ");
472         seq_puts(m, "  ------                                       -------\n");
473         return 0;
474 }
475
476 static int ksym_tracer_stat_show(struct seq_file *m, void *v)
477 {
478         struct hlist_node *stat = v;
479         struct trace_ksym *entry;
480         int access_type = 0;
481         char fn_name[KSYM_NAME_LEN];
482
483         entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
484
485         access_type = entry->attr.bp_type;
486
487         switch (access_type) {
488         case HW_BREAKPOINT_R:
489                 seq_puts(m, "  R           ");
490                 break;
491         case HW_BREAKPOINT_W:
492                 seq_puts(m, "  W           ");
493                 break;
494         case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
495                 seq_puts(m, "  RW          ");
496                 break;
497         default:
498                 seq_puts(m, "  NA          ");
499         }
500
501         if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
502                 seq_printf(m, "  %-36s", fn_name);
503         else
504                 seq_printf(m, "  %-36s", "<NA>");
505         seq_printf(m, " %15lu\n", entry->counter);
506
507         return 0;
508 }
509
510 static void *ksym_tracer_stat_start(struct tracer_stat *trace)
511 {
512         return ksym_filter_head.first;
513 }
514
515 static void *
516 ksym_tracer_stat_next(void *v, int idx)
517 {
518         struct hlist_node *stat = v;
519
520         return stat->next;
521 }
522
523 static struct tracer_stat ksym_tracer_stats = {
524         .name = "ksym_tracer",
525         .stat_start = ksym_tracer_stat_start,
526         .stat_next = ksym_tracer_stat_next,
527         .stat_headers = ksym_tracer_stat_headers,
528         .stat_show = ksym_tracer_stat_show
529 };
530
531 __init static int ksym_tracer_stat_init(void)
532 {
533         int ret;
534
535         ret = register_stat_tracer(&ksym_tracer_stats);
536         if (ret) {
537                 printk(KERN_WARNING "Warning: could not register "
538                                     "ksym tracer stats\n");
539                 return 1;
540         }
541
542         return 0;
543 }
544 fs_initcall(ksym_tracer_stat_init);
545 #endif /* CONFIG_PROFILE_KSYM_TRACER */