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