ftrace: Call trace_parser_clear() properly
[safe/jmp/linux-2.6] / 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, void *data)
83 {
84         struct ring_buffer_event *event;
85         struct ksym_trace_entry *entry;
86         struct pt_regs *regs = data;
87         struct ring_buffer *buffer;
88         int pc;
89
90         if (!ksym_tracing_enabled)
91                 return;
92
93         buffer = ksym_trace_array->buffer;
94
95         pc = preempt_count();
96
97         event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
98                                                         sizeof(*entry), 0, pc);
99         if (!event)
100                 return;
101
102         entry           = ring_buffer_event_data(event);
103         entry->ip       = instruction_pointer(regs);
104         entry->type     = hw_breakpoint_type(hbp);
105         entry->addr     = hw_breakpoint_addr(hbp);
106         strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
107
108 #ifdef CONFIG_PROFILE_KSYM_TRACER
109         ksym_collect_stats(hw_breakpoint_addr(hbp));
110 #endif /* CONFIG_PROFILE_KSYM_TRACER */
111
112         trace_buffer_unlock_commit(buffer, event, 0, pc);
113 }
114
115 /* Valid access types are represented as
116  *
117  * rw- : Set Read/Write Access Breakpoint
118  * -w- : Set Write Access Breakpoint
119  * --- : Clear Breakpoints
120  * --x : Set Execution Break points (Not available yet)
121  *
122  */
123 static int ksym_trace_get_access_type(char *str)
124 {
125         int access = 0;
126
127         if (str[0] == 'r')
128                 access |= HW_BREAKPOINT_R;
129
130         if (str[1] == 'w')
131                 access |= HW_BREAKPOINT_W;
132
133         if (str[2] == 'x')
134                 access |= HW_BREAKPOINT_X;
135
136         switch (access) {
137         case HW_BREAKPOINT_R:
138         case HW_BREAKPOINT_W:
139         case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
140                 return access;
141         default:
142                 return -EINVAL;
143         }
144 }
145
146 /*
147  * There can be several possible malformed requests and we attempt to capture
148  * all of them. We enumerate some of the rules
149  * 1. We will not allow kernel symbols with ':' since it is used as a delimiter.
150  *    i.e. multiple ':' symbols disallowed. Possible uses are of the form
151  *    <module>:<ksym_name>:<op>.
152  * 2. No delimiter symbol ':' in the input string
153  * 3. Spurious operator symbols or symbols not in their respective positions
154  * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file
155  * 5. Kernel symbol not a part of /proc/kallsyms
156  * 6. Duplicate requests
157  */
158 static int parse_ksym_trace_str(char *input_string, char **ksymname,
159                                                         unsigned long *addr)
160 {
161         int ret;
162
163         *ksymname = strsep(&input_string, ":");
164         *addr = kallsyms_lookup_name(*ksymname);
165
166         /* Check for malformed request: (2), (1) and (5) */
167         if ((!input_string) ||
168             (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
169             (*addr == 0))
170                 return -EINVAL;;
171
172         ret = ksym_trace_get_access_type(input_string);
173
174         return ret;
175 }
176
177 int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
178 {
179         struct trace_ksym *entry;
180         int ret = -ENOMEM;
181
182         if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
183                 printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
184                 " new requests for tracing can be accepted now.\n",
185                         KSYM_TRACER_MAX);
186                 return -ENOSPC;
187         }
188
189         entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
190         if (!entry)
191                 return -ENOMEM;
192
193         hw_breakpoint_init(&entry->attr);
194
195         entry->attr.bp_type = op;
196         entry->attr.bp_addr = addr;
197         entry->attr.bp_len = HW_BREAKPOINT_LEN_4;
198
199         ret = -EAGAIN;
200         entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr,
201                                         ksym_hbp_handler);
202
203         if (IS_ERR(entry->ksym_hbp)) {
204                 ret = PTR_ERR(entry->ksym_hbp);
205                 printk(KERN_INFO "ksym_tracer request failed. Try again"
206                                         " later!!\n");
207                 goto err;
208         }
209
210         hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
211         ksym_filter_entry_count++;
212
213         return 0;
214
215 err:
216         kfree(entry);
217
218         return ret;
219 }
220
221 static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
222                                                 size_t count, loff_t *ppos)
223 {
224         struct trace_ksym *entry;
225         struct hlist_node *node;
226         struct trace_seq *s;
227         ssize_t cnt = 0;
228         int ret;
229
230         s = kmalloc(sizeof(*s), GFP_KERNEL);
231         if (!s)
232                 return -ENOMEM;
233         trace_seq_init(s);
234
235         mutex_lock(&ksym_tracer_mutex);
236
237         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
238                 ret = trace_seq_printf(s, "%pS:", (void *)entry->attr.bp_addr);
239                 if (entry->attr.bp_type == HW_BREAKPOINT_R)
240                         ret = trace_seq_puts(s, "r--\n");
241                 else if (entry->attr.bp_type == HW_BREAKPOINT_W)
242                         ret = trace_seq_puts(s, "-w-\n");
243                 else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
244                         ret = trace_seq_puts(s, "rw-\n");
245                 WARN_ON_ONCE(!ret);
246         }
247
248         cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
249
250         mutex_unlock(&ksym_tracer_mutex);
251
252         kfree(s);
253
254         return cnt;
255 }
256
257 static void __ksym_trace_reset(void)
258 {
259         struct trace_ksym *entry;
260         struct hlist_node *node, *node1;
261
262         mutex_lock(&ksym_tracer_mutex);
263         hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
264                                                                 ksym_hlist) {
265                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
266                 ksym_filter_entry_count--;
267                 hlist_del_rcu(&(entry->ksym_hlist));
268                 synchronize_rcu();
269                 kfree(entry);
270         }
271         mutex_unlock(&ksym_tracer_mutex);
272 }
273
274 static ssize_t ksym_trace_filter_write(struct file *file,
275                                         const char __user *buffer,
276                                                 size_t count, loff_t *ppos)
277 {
278         struct trace_ksym *entry;
279         struct hlist_node *node;
280         char *buf, *input_string, *ksymname = NULL;
281         unsigned long ksym_addr = 0;
282         int ret, op, changed = 0;
283
284         buf = kzalloc(count + 1, GFP_KERNEL);
285         if (!buf)
286                 return -ENOMEM;
287
288         ret = -EFAULT;
289         if (copy_from_user(buf, buffer, count))
290                 goto out;
291
292         buf[count] = '\0';
293         input_string = strstrip(buf);
294
295         /*
296          * Clear all breakpoints if:
297          * 1: echo > ksym_trace_filter
298          * 2: echo 0 > ksym_trace_filter
299          * 3: echo "*:---" > ksym_trace_filter
300          */
301         if (!input_string[0] || !strcmp(input_string, "0") ||
302             !strcmp(input_string, "*:---")) {
303                 __ksym_trace_reset();
304                 ret = 0;
305                 goto out;
306         }
307
308         ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
309         if (ret < 0)
310                 goto out;
311
312         mutex_lock(&ksym_tracer_mutex);
313
314         ret = -EINVAL;
315         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
316                 if (entry->attr.bp_addr == ksym_addr) {
317                         /* Check for malformed request: (6) */
318                         if (entry->attr.bp_type != op)
319                                 changed = 1;
320                         else
321                                 goto out_unlock;
322                         break;
323                 }
324         }
325         if (changed) {
326                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
327                 entry->attr.bp_type = op;
328                 ret = 0;
329                 if (op > 0) {
330                         entry->ksym_hbp =
331                                 register_wide_hw_breakpoint(&entry->attr,
332                                         ksym_hbp_handler);
333                         if (IS_ERR(entry->ksym_hbp))
334                                 ret = PTR_ERR(entry->ksym_hbp);
335                         else
336                                 goto out_unlock;
337                 }
338                 /* Error or "symbol:---" case: drop it */
339                 ksym_filter_entry_count--;
340                 hlist_del_rcu(&(entry->ksym_hlist));
341                 synchronize_rcu();
342                 kfree(entry);
343                 goto out_unlock;
344         } else {
345                 /* Check for malformed request: (4) */
346                 if (op)
347                         ret = process_new_ksym_entry(ksymname, op, ksym_addr);
348         }
349 out_unlock:
350         mutex_unlock(&ksym_tracer_mutex);
351 out:
352         kfree(buf);
353         return !ret ? count : ret;
354 }
355
356 static const struct file_operations ksym_tracing_fops = {
357         .open           = tracing_open_generic,
358         .read           = ksym_trace_filter_read,
359         .write          = ksym_trace_filter_write,
360 };
361
362 static void ksym_trace_reset(struct trace_array *tr)
363 {
364         ksym_tracing_enabled = 0;
365         __ksym_trace_reset();
366 }
367
368 static int ksym_trace_init(struct trace_array *tr)
369 {
370         int cpu, ret = 0;
371
372         for_each_online_cpu(cpu)
373                 tracing_reset(tr, cpu);
374         ksym_tracing_enabled = 1;
375         ksym_trace_array = tr;
376
377         return ret;
378 }
379
380 static void ksym_trace_print_header(struct seq_file *m)
381 {
382         seq_puts(m,
383                  "#       TASK-PID   CPU#      Symbol                    "
384                  "Type    Function\n");
385         seq_puts(m,
386                  "#          |        |          |                       "
387                  " |         |\n");
388 }
389
390 static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
391 {
392         struct trace_entry *entry = iter->ent;
393         struct trace_seq *s = &iter->seq;
394         struct ksym_trace_entry *field;
395         char str[KSYM_SYMBOL_LEN];
396         int ret;
397
398         if (entry->type != TRACE_KSYM)
399                 return TRACE_TYPE_UNHANDLED;
400
401         trace_assign_type(field, entry);
402
403         ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
404                                 entry->pid, iter->cpu, (char *)field->addr);
405         if (!ret)
406                 return TRACE_TYPE_PARTIAL_LINE;
407
408         switch (field->type) {
409         case HW_BREAKPOINT_R:
410                 ret = trace_seq_printf(s, " R  ");
411                 break;
412         case HW_BREAKPOINT_W:
413                 ret = trace_seq_printf(s, " W  ");
414                 break;
415         case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
416                 ret = trace_seq_printf(s, " RW ");
417                 break;
418         default:
419                 return TRACE_TYPE_PARTIAL_LINE;
420         }
421
422         if (!ret)
423                 return TRACE_TYPE_PARTIAL_LINE;
424
425         sprint_symbol(str, field->ip);
426         ret = trace_seq_printf(s, "%s\n", str);
427         if (!ret)
428                 return TRACE_TYPE_PARTIAL_LINE;
429
430         return TRACE_TYPE_HANDLED;
431 }
432
433 struct tracer ksym_tracer __read_mostly =
434 {
435         .name           = "ksym_tracer",
436         .init           = ksym_trace_init,
437         .reset          = ksym_trace_reset,
438 #ifdef CONFIG_FTRACE_SELFTEST
439         .selftest       = trace_selftest_startup_ksym,
440 #endif
441         .print_header   = ksym_trace_print_header,
442         .print_line     = ksym_trace_output
443 };
444
445 __init static int init_ksym_trace(void)
446 {
447         struct dentry *d_tracer;
448         struct dentry *entry;
449
450         d_tracer = tracing_init_dentry();
451         ksym_filter_entry_count = 0;
452
453         entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
454                                     NULL, &ksym_tracing_fops);
455         if (!entry)
456                 pr_warning("Could not create debugfs "
457                            "'ksym_trace_filter' file\n");
458
459         return register_tracer(&ksym_tracer);
460 }
461 device_initcall(init_ksym_trace);
462
463
464 #ifdef CONFIG_PROFILE_KSYM_TRACER
465 static int ksym_tracer_stat_headers(struct seq_file *m)
466 {
467         seq_puts(m, "  Access Type ");
468         seq_puts(m, "  Symbol                                       Counter\n");
469         seq_puts(m, "  ----------- ");
470         seq_puts(m, "  ------                                       -------\n");
471         return 0;
472 }
473
474 static int ksym_tracer_stat_show(struct seq_file *m, void *v)
475 {
476         struct hlist_node *stat = v;
477         struct trace_ksym *entry;
478         int access_type = 0;
479         char fn_name[KSYM_NAME_LEN];
480
481         entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
482
483         access_type = entry->attr.bp_type;
484
485         switch (access_type) {
486         case HW_BREAKPOINT_R:
487                 seq_puts(m, "  R           ");
488                 break;
489         case HW_BREAKPOINT_W:
490                 seq_puts(m, "  W           ");
491                 break;
492         case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
493                 seq_puts(m, "  RW          ");
494                 break;
495         default:
496                 seq_puts(m, "  NA          ");
497         }
498
499         if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
500                 seq_printf(m, "  %-36s", fn_name);
501         else
502                 seq_printf(m, "  %-36s", "<NA>");
503         seq_printf(m, " %15lu\n", entry->counter);
504
505         return 0;
506 }
507
508 static void *ksym_tracer_stat_start(struct tracer_stat *trace)
509 {
510         return ksym_filter_head.first;
511 }
512
513 static void *
514 ksym_tracer_stat_next(void *v, int idx)
515 {
516         struct hlist_node *stat = v;
517
518         return stat->next;
519 }
520
521 static struct tracer_stat ksym_tracer_stats = {
522         .name = "ksym_tracer",
523         .stat_start = ksym_tracer_stat_start,
524         .stat_next = ksym_tracer_stat_next,
525         .stat_headers = ksym_tracer_stat_headers,
526         .stat_show = ksym_tracer_stat_show
527 };
528
529 __init static int ksym_tracer_stat_init(void)
530 {
531         int ret;
532
533         ret = register_stat_tracer(&ksym_tracer_stats);
534         if (ret) {
535                 printk(KERN_WARNING "Warning: could not register "
536                                     "ksym tracer stats\n");
537                 return 1;
538         }
539
540         return 0;
541 }
542 fs_initcall(ksym_tracer_stat_init);
543 #endif /* CONFIG_PROFILE_KSYM_TRACER */