hw-breakpoints: Rewrite the hw-breakpoints layer on top of perf events
[linux-2.6-block.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         unsigned long           ksym_addr;
46         int                     type;
47         int                     len;
48 #ifdef CONFIG_PROFILE_KSYM_TRACER
49         unsigned long           counter;
50 #endif
51         struct hlist_node       ksym_hlist;
52 };
53
54 static struct trace_array *ksym_trace_array;
55
56 static unsigned int ksym_filter_entry_count;
57 static unsigned int ksym_tracing_enabled;
58
59 static HLIST_HEAD(ksym_filter_head);
60
61 static DEFINE_MUTEX(ksym_tracer_mutex);
62
63 #ifdef CONFIG_PROFILE_KSYM_TRACER
64
65 #define MAX_UL_INT 0xffffffff
66
67 void 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) {
74                 if ((entry->ksym_addr == hbp_hit_addr) &&
75                     (entry->counter <= MAX_UL_INT)) {
76                         entry->counter++;
77                         break;
78                 }
79         }
80         rcu_read_unlock();
81 }
82 #endif /* CONFIG_PROFILE_KSYM_TRACER */
83
84 void ksym_hbp_handler(struct perf_event *hbp, void *data)
85 {
86         struct ring_buffer_event *event;
87         struct ksym_trace_entry *entry;
88         struct pt_regs *regs = data;
89         struct ring_buffer *buffer;
90         int pc;
91
92         if (!ksym_tracing_enabled)
93                 return;
94
95         buffer = ksym_trace_array->buffer;
96
97         pc = preempt_count();
98
99         event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
100                                                         sizeof(*entry), 0, pc);
101         if (!event)
102                 return;
103
104         entry           = ring_buffer_event_data(event);
105         entry->ip       = instruction_pointer(regs);
106         entry->type     = hw_breakpoint_type(hbp);
107         entry->addr     = hw_breakpoint_addr(hbp);
108         strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
109
110 #ifdef CONFIG_PROFILE_KSYM_TRACER
111         ksym_collect_stats(hw_breakpoint_addr(hbp));
112 #endif /* CONFIG_PROFILE_KSYM_TRACER */
113
114         trace_buffer_unlock_commit(buffer, event, 0, pc);
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  */
125 static int ksym_trace_get_access_type(char *str)
126 {
127         int access = 0;
128
129         if (str[0] == 'r')
130                 access |= HW_BREAKPOINT_R;
131
132         if (str[1] == 'w')
133                 access |= HW_BREAKPOINT_W;
134
135         if (str[2] == 'x')
136                 access |= HW_BREAKPOINT_X;
137
138         switch (access) {
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         entry->type = op;
195         entry->ksym_addr = addr;
196         entry->len = HW_BREAKPOINT_LEN_4;
197
198         ret = -EAGAIN;
199         entry->ksym_hbp = register_wide_hw_breakpoint(entry->ksym_addr,
200                                         entry->len, entry->type,
201                                         ksym_hbp_handler, true);
202         if (IS_ERR(entry->ksym_hbp)) {
203                 entry->ksym_hbp = NULL;
204                 ret = PTR_ERR(entry->ksym_hbp);
205         }
206
207         if (!entry->ksym_hbp) {
208                 printk(KERN_INFO "ksym_tracer request failed. Try again"
209                                         " later!!\n");
210                 goto err;
211         }
212
213         hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
214         ksym_filter_entry_count++;
215
216         return 0;
217
218 err:
219         kfree(entry);
220
221         return ret;
222 }
223
224 static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
225                                                 size_t count, loff_t *ppos)
226 {
227         struct trace_ksym *entry;
228         struct hlist_node *node;
229         struct trace_seq *s;
230         ssize_t cnt = 0;
231         int ret;
232
233         s = kmalloc(sizeof(*s), GFP_KERNEL);
234         if (!s)
235                 return -ENOMEM;
236         trace_seq_init(s);
237
238         mutex_lock(&ksym_tracer_mutex);
239
240         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
241                 ret = trace_seq_printf(s, "%pS:", (void *)entry->ksym_addr);
242                 if (entry->type == HW_BREAKPOINT_W)
243                         ret = trace_seq_puts(s, "-w-\n");
244                 else if (entry->type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
245                         ret = trace_seq_puts(s, "rw-\n");
246                 WARN_ON_ONCE(!ret);
247         }
248
249         cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
250
251         mutex_unlock(&ksym_tracer_mutex);
252
253         kfree(s);
254
255         return cnt;
256 }
257
258 static 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) {
266                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
267                 ksym_filter_entry_count--;
268                 hlist_del_rcu(&(entry->ksym_hlist));
269                 synchronize_rcu();
270                 kfree(entry);
271         }
272         mutex_unlock(&ksym_tracer_mutex);
273 }
274
275 static 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
285         input_string = kzalloc(count + 1, GFP_KERNEL);
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         }
293         input_string[count] = '\0';
294
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
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) {
320                 if (entry->ksym_addr == ksym_addr) {
321                         /* Check for malformed request: (6) */
322                         if (entry->type != op)
323                                 changed = 1;
324                         else
325                                 goto out;
326                         break;
327                 }
328         }
329         if (changed) {
330                 unregister_wide_hw_breakpoint(entry->ksym_hbp);
331                 entry->type = op;
332                 if (op > 0) {
333                         entry->ksym_hbp =
334                                 register_wide_hw_breakpoint(entry->ksym_addr,
335                                         entry->len, entry->type,
336                                         ksym_hbp_handler, true);
337                         if (IS_ERR(entry->ksym_hbp))
338                                 entry->ksym_hbp = NULL;
339                         if (!entry->ksym_hbp)
340                                 goto out;
341                 }
342                 ksym_filter_entry_count--;
343                 hlist_del_rcu(&(entry->ksym_hlist));
344                 synchronize_rcu();
345                 kfree(entry);
346                 ret = 0;
347                 goto out;
348         } else {
349                 /* Check for malformed request: (4) */
350                 if (op == 0)
351                         goto out;
352                 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
353         }
354 out:
355         mutex_unlock(&ksym_tracer_mutex);
356
357         kfree(input_string);
358
359         if (!ret)
360                 ret = count;
361         return ret;
362 }
363
364 static 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
370 static void ksym_trace_reset(struct trace_array *tr)
371 {
372         ksym_tracing_enabled = 0;
373         __ksym_trace_reset();
374 }
375
376 static 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
388 static void ksym_trace_print_header(struct seq_file *m)
389 {
390         seq_puts(m,
391                  "#       TASK-PID   CPU#      Symbol                    "
392                  "Type    Function\n");
393         seq_puts(m,
394                  "#          |        |          |                       "
395                  " |         |\n");
396 }
397
398 static 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;
402         struct ksym_trace_entry *field;
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
411         ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
412                                 entry->pid, iter->cpu, (char *)field->addr);
413         if (!ret)
414                 return TRACE_TYPE_PARTIAL_LINE;
415
416         switch (field->type) {
417         case HW_BREAKPOINT_W:
418                 ret = trace_seq_printf(s, " W  ");
419                 break;
420         case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
421                 ret = trace_seq_printf(s, " RW ");
422                 break;
423         default:
424                 return TRACE_TYPE_PARTIAL_LINE;
425         }
426
427         if (!ret)
428                 return TRACE_TYPE_PARTIAL_LINE;
429
430         sprint_symbol(str, field->ip);
431         ret = trace_seq_printf(s, "%s\n", str);
432         if (!ret)
433                 return TRACE_TYPE_PARTIAL_LINE;
434
435         return TRACE_TYPE_HANDLED;
436 }
437
438 struct tracer ksym_tracer __read_mostly =
439 {
440         .name           = "ksym_tracer",
441         .init           = ksym_trace_init,
442         .reset          = ksym_trace_reset,
443 #ifdef CONFIG_FTRACE_SELFTEST
444         .selftest       = trace_selftest_startup_ksym,
445 #endif
446         .print_header   = ksym_trace_print_header,
447         .print_line     = ksym_trace_output
448 };
449
450 __init static int init_ksym_trace(void)
451 {
452         struct dentry *d_tracer;
453         struct dentry *entry;
454
455         d_tracer = tracing_init_dentry();
456         ksym_filter_entry_count = 0;
457
458         entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
459                                     NULL, &ksym_tracing_fops);
460         if (!entry)
461                 pr_warning("Could not create debugfs "
462                            "'ksym_trace_filter' file\n");
463
464         return register_tracer(&ksym_tracer);
465 }
466 device_initcall(init_ksym_trace);
467
468
469 #ifdef CONFIG_PROFILE_KSYM_TRACER
470 static int ksym_tracer_stat_headers(struct seq_file *m)
471 {
472         seq_puts(m, "  Access Type ");
473         seq_puts(m, "  Symbol                                       Counter\n");
474         seq_puts(m, "  ----------- ");
475         seq_puts(m, "  ------                                       -------\n");
476         return 0;
477 }
478
479 static int ksym_tracer_stat_show(struct seq_file *m, void *v)
480 {
481         struct hlist_node *stat = v;
482         struct trace_ksym *entry;
483         int access_type = 0;
484         char fn_name[KSYM_NAME_LEN];
485
486         entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
487
488         access_type = entry->type;
489
490         switch (access_type) {
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->ksym_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 */