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