hw-breakpoints: Improve in-kernel event creation error granularity
[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>
26#include <linux/fs.h>
27
28#include "trace_output.h"
29#include "trace_stat.h"
30#include "trace.h"
31
24f1e32c
FW
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
0722db01
P
37 * of available hardware breakpoint registers.
38 */
39#define KSYM_TRACER_MAX HBP_NUM
40
41#define KSYM_TRACER_OP_LEN 3 /* rw- */
0722db01 42
db59504d 43struct trace_ksym {
24f1e32c 44 struct perf_event **ksym_hbp;
db59504d 45 unsigned long ksym_addr;
24f1e32c
FW
46 int type;
47 int len;
db59504d
LZ
48#ifdef CONFIG_PROFILE_KSYM_TRACER
49 unsigned long counter;
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) {
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
24f1e32c 84void ksym_hbp_handler(struct perf_event *hbp, void *data)
0722db01
P
85{
86 struct ring_buffer_event *event;
db59504d 87 struct ksym_trace_entry *entry;
24f1e32c 88 struct pt_regs *regs = data;
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
24f1e32c
FW
195 entry->type = op;
196 entry->ksym_addr = addr;
197 entry->len = HW_BREAKPOINT_LEN_4;
198
199 ret = -EAGAIN;
200 entry->ksym_hbp = register_wide_hw_breakpoint(entry->ksym_addr,
201 entry->len, entry->type,
202 ksym_hbp_handler, true);
203 if (IS_ERR(entry->ksym_hbp)) {
204 entry->ksym_hbp = NULL;
205 ret = PTR_ERR(entry->ksym_hbp);
206 }
0722db01 207
24f1e32c 208 if (!entry->ksym_hbp) {
0722db01
P
209 printk(KERN_INFO "ksym_tracer request failed. Try again"
210 " later!!\n");
558df6c8 211 goto err;
0722db01 212 }
24f1e32c 213
0722db01
P
214 hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
215 ksym_filter_entry_count++;
24f1e32c 216
0722db01 217 return 0;
24f1e32c 218
558df6c8 219err:
558df6c8 220 kfree(entry);
24f1e32c 221
558df6c8 222 return ret;
0722db01
P
223}
224
225static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
226 size_t count, loff_t *ppos)
227{
228 struct trace_ksym *entry;
229 struct hlist_node *node;
be9742e6
LZ
230 struct trace_seq *s;
231 ssize_t cnt = 0;
232 int ret;
233
234 s = kmalloc(sizeof(*s), GFP_KERNEL);
235 if (!s)
236 return -ENOMEM;
237 trace_seq_init(s);
0722db01
P
238
239 mutex_lock(&ksym_tracer_mutex);
240
241 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
24f1e32c 242 ret = trace_seq_printf(s, "%pS:", (void *)entry->ksym_addr);
676c0dbe
PM
243 if (entry->type == HW_BREAKPOINT_R)
244 ret = trace_seq_puts(s, "r--\n");
245 else if (entry->type == HW_BREAKPOINT_W)
be9742e6 246 ret = trace_seq_puts(s, "-w-\n");
24f1e32c 247 else if (entry->type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
be9742e6
LZ
248 ret = trace_seq_puts(s, "rw-\n");
249 WARN_ON_ONCE(!ret);
0722db01 250 }
be9742e6
LZ
251
252 cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
253
0722db01
P
254 mutex_unlock(&ksym_tracer_mutex);
255
be9742e6
LZ
256 kfree(s);
257
258 return cnt;
0722db01
P
259}
260
75e33751
XG
261static void __ksym_trace_reset(void)
262{
263 struct trace_ksym *entry;
264 struct hlist_node *node, *node1;
265
266 mutex_lock(&ksym_tracer_mutex);
267 hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
268 ksym_hlist) {
24f1e32c 269 unregister_wide_hw_breakpoint(entry->ksym_hbp);
75e33751
XG
270 ksym_filter_entry_count--;
271 hlist_del_rcu(&(entry->ksym_hlist));
272 synchronize_rcu();
75e33751
XG
273 kfree(entry);
274 }
275 mutex_unlock(&ksym_tracer_mutex);
276}
277
0722db01
P
278static ssize_t ksym_trace_filter_write(struct file *file,
279 const char __user *buffer,
280 size_t count, loff_t *ppos)
281{
282 struct trace_ksym *entry;
283 struct hlist_node *node;
284 char *input_string, *ksymname = NULL;
285 unsigned long ksym_addr = 0;
286 int ret, op, changed = 0;
287
011ed568 288 input_string = kzalloc(count + 1, GFP_KERNEL);
0722db01
P
289 if (!input_string)
290 return -ENOMEM;
291
292 if (copy_from_user(input_string, buffer, count)) {
293 kfree(input_string);
294 return -EFAULT;
295 }
011ed568 296 input_string[count] = '\0';
0722db01 297
75e33751
XG
298 strstrip(input_string);
299
300 /*
301 * Clear all breakpoints if:
302 * 1: echo > ksym_trace_filter
303 * 2: echo 0 > ksym_trace_filter
304 * 3: echo "*:---" > ksym_trace_filter
305 */
306 if (!input_string[0] || !strcmp(input_string, "0") ||
307 !strcmp(input_string, "*:---")) {
308 __ksym_trace_reset();
309 kfree(input_string);
310 return count;
311 }
312
0722db01
P
313 ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
314 if (ret < 0) {
315 kfree(input_string);
316 return ret;
317 }
318
319 mutex_lock(&ksym_tracer_mutex);
320
321 ret = -EINVAL;
322 hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
323 if (entry->ksym_addr == ksym_addr) {
324 /* Check for malformed request: (6) */
24f1e32c 325 if (entry->type != op)
0722db01
P
326 changed = 1;
327 else
558df6c8 328 goto out;
0722db01
P
329 break;
330 }
331 }
332 if (changed) {
24f1e32c
FW
333 unregister_wide_hw_breakpoint(entry->ksym_hbp);
334 entry->type = op;
0722db01 335 if (op > 0) {
24f1e32c
FW
336 entry->ksym_hbp =
337 register_wide_hw_breakpoint(entry->ksym_addr,
338 entry->len, entry->type,
339 ksym_hbp_handler, true);
340 if (IS_ERR(entry->ksym_hbp))
341 entry->ksym_hbp = NULL;
d99be40a
FW
342
343 /* modified without problem */
344 if (entry->ksym_hbp) {
345 ret = 0;
558df6c8 346 goto out;
d99be40a
FW
347 }
348 } else {
349 ret = 0;
558df6c8 350 }
d99be40a 351 /* Error or "symbol:---" case: drop it */
0722db01
P
352 ksym_filter_entry_count--;
353 hlist_del_rcu(&(entry->ksym_hlist));
354 synchronize_rcu();
0722db01 355 kfree(entry);
558df6c8 356 goto out;
0722db01
P
357 } else {
358 /* Check for malformed request: (4) */
359 if (op == 0)
558df6c8 360 goto out;
0722db01 361 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
0722db01 362 }
558df6c8
LZ
363out:
364 mutex_unlock(&ksym_tracer_mutex);
0722db01 365
0722db01
P
366 kfree(input_string);
367
558df6c8
LZ
368 if (!ret)
369 ret = count;
0722db01
P
370 return ret;
371}
372
373static const struct file_operations ksym_tracing_fops = {
374 .open = tracing_open_generic,
375 .read = ksym_trace_filter_read,
376 .write = ksym_trace_filter_write,
377};
378
379static void ksym_trace_reset(struct trace_array *tr)
380{
0722db01 381 ksym_tracing_enabled = 0;
75e33751 382 __ksym_trace_reset();
0722db01
P
383}
384
385static int ksym_trace_init(struct trace_array *tr)
386{
387 int cpu, ret = 0;
388
389 for_each_online_cpu(cpu)
390 tracing_reset(tr, cpu);
391 ksym_tracing_enabled = 1;
392 ksym_trace_array = tr;
393
394 return ret;
395}
396
397static void ksym_trace_print_header(struct seq_file *m)
398{
0722db01 399 seq_puts(m,
d857ace1
XG
400 "# TASK-PID CPU# Symbol "
401 "Type Function\n");
0722db01 402 seq_puts(m,
d857ace1
XG
403 "# | | | "
404 " | |\n");
0722db01
P
405}
406
407static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
408{
409 struct trace_entry *entry = iter->ent;
410 struct trace_seq *s = &iter->seq;
db59504d 411 struct ksym_trace_entry *field;
0722db01
P
412 char str[KSYM_SYMBOL_LEN];
413 int ret;
414
415 if (entry->type != TRACE_KSYM)
416 return TRACE_TYPE_UNHANDLED;
417
418 trace_assign_type(field, entry);
419
24f1e32c
FW
420 ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
421 entry->pid, iter->cpu, (char *)field->addr);
0722db01
P
422 if (!ret)
423 return TRACE_TYPE_PARTIAL_LINE;
424
db59504d 425 switch (field->type) {
676c0dbe
PM
426 case HW_BREAKPOINT_R:
427 ret = trace_seq_printf(s, " R ");
428 break;
24f1e32c 429 case HW_BREAKPOINT_W:
0722db01
P
430 ret = trace_seq_printf(s, " W ");
431 break;
24f1e32c 432 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
0722db01
P
433 ret = trace_seq_printf(s, " RW ");
434 break;
435 default:
436 return TRACE_TYPE_PARTIAL_LINE;
437 }
438
439 if (!ret)
440 return TRACE_TYPE_PARTIAL_LINE;
441
442 sprint_symbol(str, field->ip);
d857ace1 443 ret = trace_seq_printf(s, "%s\n", str);
0722db01
P
444 if (!ret)
445 return TRACE_TYPE_PARTIAL_LINE;
446
447 return TRACE_TYPE_HANDLED;
448}
449
450struct tracer ksym_tracer __read_mostly =
451{
452 .name = "ksym_tracer",
453 .init = ksym_trace_init,
454 .reset = ksym_trace_reset,
455#ifdef CONFIG_FTRACE_SELFTEST
456 .selftest = trace_selftest_startup_ksym,
457#endif
458 .print_header = ksym_trace_print_header,
459 .print_line = ksym_trace_output
460};
461
462__init static int init_ksym_trace(void)
463{
464 struct dentry *d_tracer;
465 struct dentry *entry;
466
467 d_tracer = tracing_init_dentry();
468 ksym_filter_entry_count = 0;
469
470 entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
471 NULL, &ksym_tracing_fops);
472 if (!entry)
473 pr_warning("Could not create debugfs "
474 "'ksym_trace_filter' file\n");
475
476 return register_tracer(&ksym_tracer);
477}
478device_initcall(init_ksym_trace);
479
480
481#ifdef CONFIG_PROFILE_KSYM_TRACER
482static int ksym_tracer_stat_headers(struct seq_file *m)
483{
9d7e9344
LZ
484 seq_puts(m, " Access Type ");
485 seq_puts(m, " Symbol Counter\n");
486 seq_puts(m, " ----------- ");
487 seq_puts(m, " ------ -------\n");
0722db01
P
488 return 0;
489}
490
491static int ksym_tracer_stat_show(struct seq_file *m, void *v)
492{
493 struct hlist_node *stat = v;
494 struct trace_ksym *entry;
495 int access_type = 0;
496 char fn_name[KSYM_NAME_LEN];
497
498 entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
499
24f1e32c 500 access_type = entry->type;
0722db01
P
501
502 switch (access_type) {
676c0dbe
PM
503 case HW_BREAKPOINT_R:
504 seq_puts(m, " R ");
505 break;
24f1e32c 506 case HW_BREAKPOINT_W:
9d7e9344 507 seq_puts(m, " W ");
0722db01 508 break;
24f1e32c 509 case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
9d7e9344 510 seq_puts(m, " RW ");
0722db01
P
511 break;
512 default:
9d7e9344 513 seq_puts(m, " NA ");
0722db01
P
514 }
515
516 if (lookup_symbol_name(entry->ksym_addr, fn_name) >= 0)
9d7e9344 517 seq_printf(m, " %-36s", fn_name);
0722db01 518 else
9d7e9344
LZ
519 seq_printf(m, " %-36s", "<NA>");
520 seq_printf(m, " %15lu\n", entry->counter);
0722db01 521
0722db01
P
522 return 0;
523}
524
525static void *ksym_tracer_stat_start(struct tracer_stat *trace)
526{
9d7e9344 527 return ksym_filter_head.first;
0722db01
P
528}
529
530static void *
531ksym_tracer_stat_next(void *v, int idx)
532{
533 struct hlist_node *stat = v;
534
535 return stat->next;
536}
537
538static struct tracer_stat ksym_tracer_stats = {
539 .name = "ksym_tracer",
540 .stat_start = ksym_tracer_stat_start,
541 .stat_next = ksym_tracer_stat_next,
542 .stat_headers = ksym_tracer_stat_headers,
543 .stat_show = ksym_tracer_stat_show
544};
545
546__init static int ksym_tracer_stat_init(void)
547{
548 int ret;
549
550 ret = register_stat_tracer(&ksym_tracer_stats);
551 if (ret) {
552 printk(KERN_WARNING "Warning: could not register "
553 "ksym tracer stats\n");
554 return 1;
555 }
556
557 return 0;
558}
559fs_initcall(ksym_tracer_stat_init);
560#endif /* CONFIG_PROFILE_KSYM_TRACER */