trace_ksym.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * trace_ksym.c - Kernel Symbol Tracer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) IBM Corporation, 2009
 */

#include <linux/kallsyms.h>
#include <linux/uaccess.h>
#include <linux/debugfs.h>
#include <linux/ftrace.h>
#include <linux/module.h>
#include <linux/fs.h>

#include "trace_output.h"
#include "trace_stat.h"
#include "trace.h"

32 33 34 35 36
#include <linux/hw_breakpoint.h>
#include <asm/hw_breakpoint.h>

/*
 * For now, let us restrict the no. of symbols traced simultaneously to number
37 38 39 40 41 42
 * of available hardware breakpoint registers.
 */
#define KSYM_TRACER_MAX HBP_NUM

#define KSYM_TRACER_OP_LEN 3 /* rw- */

43
struct trace_ksym {
44
	struct perf_event	**ksym_hbp;
45
	struct perf_event_attr	attr;
46 47 48 49 50 51
#ifdef CONFIG_PROFILE_KSYM_TRACER
	unsigned long		counter;
#endif
	struct hlist_node	ksym_hlist;
};

52 53 54 55 56 57 58
static struct trace_array *ksym_trace_array;

static unsigned int ksym_filter_entry_count;
static unsigned int ksym_tracing_enabled;

static HLIST_HEAD(ksym_filter_head);

59 60
static DEFINE_MUTEX(ksym_tracer_mutex);

61 62 63 64 65 66 67 68 69 70 71
#ifdef CONFIG_PROFILE_KSYM_TRACER

#define MAX_UL_INT 0xffffffff

void ksym_collect_stats(unsigned long hbp_hit_addr)
{
	struct hlist_node *node;
	struct trace_ksym *entry;

	rcu_read_lock();
	hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
72
		if ((entry->attr.bp_addr == hbp_hit_addr) &&
73 74 75 76 77 78 79 80 81
		    (entry->counter <= MAX_UL_INT)) {
			entry->counter++;
			break;
		}
	}
	rcu_read_unlock();
}
#endif /* CONFIG_PROFILE_KSYM_TRACER */

82 83 84
void ksym_hbp_handler(struct perf_event *hbp, int nmi,
		      struct perf_sample_data *data,
		      struct pt_regs *regs)
85 86
{
	struct ring_buffer_event *event;
87
	struct ksym_trace_entry *entry;
88
	struct ring_buffer *buffer;
89 90 91 92 93
	int pc;

	if (!ksym_tracing_enabled)
		return;

94 95
	buffer = ksym_trace_array->buffer;

96 97
	pc = preempt_count();

98
	event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
99 100 101 102
							sizeof(*entry), 0, pc);
	if (!event)
		return;

103 104
	entry		= ring_buffer_event_data(event);
	entry->ip	= instruction_pointer(regs);
105 106
	entry->type	= hw_breakpoint_type(hbp);
	entry->addr	= hw_breakpoint_addr(hbp);
107 108
	strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);

109
#ifdef CONFIG_PROFILE_KSYM_TRACER
110
	ksym_collect_stats(hw_breakpoint_addr(hbp));
111 112
#endif /* CONFIG_PROFILE_KSYM_TRACER */

113
	trace_buffer_unlock_commit(buffer, event, 0, pc);
114 115 116 117 118 119 120 121 122 123
}

/* Valid access types are represented as
 *
 * rw- : Set Read/Write Access Breakpoint
 * -w- : Set Write Access Breakpoint
 * --- : Clear Breakpoints
 * --x : Set Execution Break points (Not available yet)
 *
 */
124
static int ksym_trace_get_access_type(char *str)
125
{
126
	int access = 0;
127

128
	if (str[0] == 'r')
129
		access |= HW_BREAKPOINT_R;
130 131

	if (str[1] == 'w')
132
		access |= HW_BREAKPOINT_W;
133

134 135
	if (str[2] == 'x')
		access |= HW_BREAKPOINT_X;
136 137

	switch (access) {
138
	case HW_BREAKPOINT_R:
139 140 141 142 143
	case HW_BREAKPOINT_W:
	case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
		return access;
	default:
		return -EINVAL;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	}
}

/*
 * There can be several possible malformed requests and we attempt to capture
 * all of them. We enumerate some of the rules
 * 1. We will not allow kernel symbols with ':' since it is used as a delimiter.
 *    i.e. multiple ':' symbols disallowed. Possible uses are of the form
 *    <module>:<ksym_name>:<op>.
 * 2. No delimiter symbol ':' in the input string
 * 3. Spurious operator symbols or symbols not in their respective positions
 * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file
 * 5. Kernel symbol not a part of /proc/kallsyms
 * 6. Duplicate requests
 */
static int parse_ksym_trace_str(char *input_string, char **ksymname,
							unsigned long *addr)
{
	int ret;

164
	*ksymname = strsep(&input_string, ":");
165 166 167 168
	*addr = kallsyms_lookup_name(*ksymname);

	/* Check for malformed request: (2), (1) and (5) */
	if ((!input_string) ||
169 170 171 172
	    (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
	    (*addr == 0))
		return -EINVAL;;

173 174 175 176 177 178 179 180
	ret = ksym_trace_get_access_type(input_string);

	return ret;
}

int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
{
	struct trace_ksym *entry;
L
Li Zefan 已提交
181
	int ret = -ENOMEM;
182 183 184 185 186 187 188 189 190 191 192 193

	if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
		printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
		" new requests for tracing can be accepted now.\n",
			KSYM_TRACER_MAX);
		return -ENOSPC;
	}

	entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;

194 195 196 197 198
	hw_breakpoint_init(&entry->attr);

	entry->attr.bp_type = op;
	entry->attr.bp_addr = addr;
	entry->attr.bp_len = HW_BREAKPOINT_LEN_4;
199 200

	ret = -EAGAIN;
201 202
	entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr,
					ksym_hbp_handler);
203

204 205
	if (IS_ERR(entry->ksym_hbp)) {
		ret = PTR_ERR(entry->ksym_hbp);
206 207
		printk(KERN_INFO "ksym_tracer request failed. Try again"
					" later!!\n");
L
Li Zefan 已提交
208
		goto err;
209
	}
210

211 212
	hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
	ksym_filter_entry_count++;
213

214
	return 0;
215

L
Li Zefan 已提交
216 217
err:
	kfree(entry);
218

L
Li Zefan 已提交
219
	return ret;
220 221 222 223 224 225 226
}

static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
						size_t count, loff_t *ppos)
{
	struct trace_ksym *entry;
	struct hlist_node *node;
227 228 229 230 231 232 233 234
	struct trace_seq *s;
	ssize_t cnt = 0;
	int ret;

	s = kmalloc(sizeof(*s), GFP_KERNEL);
	if (!s)
		return -ENOMEM;
	trace_seq_init(s);
235 236 237 238

	mutex_lock(&ksym_tracer_mutex);

	hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
239 240
		ret = trace_seq_printf(s, "%pS:", (void *)entry->attr.bp_addr);
		if (entry->attr.bp_type == HW_BREAKPOINT_R)
241
			ret = trace_seq_puts(s, "r--\n");
242
		else if (entry->attr.bp_type == HW_BREAKPOINT_W)
243
			ret = trace_seq_puts(s, "-w-\n");
244
		else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
245 246
			ret = trace_seq_puts(s, "rw-\n");
		WARN_ON_ONCE(!ret);
247
	}
248 249 250

	cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);

251 252
	mutex_unlock(&ksym_tracer_mutex);

253 254 255
	kfree(s);

	return cnt;
256 257
}

258 259 260 261 262 263 264 265
static void __ksym_trace_reset(void)
{
	struct trace_ksym *entry;
	struct hlist_node *node, *node1;

	mutex_lock(&ksym_tracer_mutex);
	hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
								ksym_hlist) {
266
		unregister_wide_hw_breakpoint(entry->ksym_hbp);
267 268 269 270 271 272 273 274
		ksym_filter_entry_count--;
		hlist_del_rcu(&(entry->ksym_hlist));
		synchronize_rcu();
		kfree(entry);
	}
	mutex_unlock(&ksym_tracer_mutex);
}

275 276 277 278 279 280 281 282 283 284
static ssize_t ksym_trace_filter_write(struct file *file,
					const char __user *buffer,
						size_t count, loff_t *ppos)
{
	struct trace_ksym *entry;
	struct hlist_node *node;
	char *input_string, *ksymname = NULL;
	unsigned long ksym_addr = 0;
	int ret, op, changed = 0;

285
	input_string = kzalloc(count + 1, GFP_KERNEL);
286 287 288 289 290 291 292
	if (!input_string)
		return -ENOMEM;

	if (copy_from_user(input_string, buffer, count)) {
		kfree(input_string);
		return -EFAULT;
	}
293
	input_string[count] = '\0';
294

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	strstrip(input_string);

	/*
	 * Clear all breakpoints if:
	 * 1: echo > ksym_trace_filter
	 * 2: echo 0 > ksym_trace_filter
	 * 3: echo "*:---" > ksym_trace_filter
	 */
	if (!input_string[0] || !strcmp(input_string, "0") ||
	    !strcmp(input_string, "*:---")) {
		__ksym_trace_reset();
		kfree(input_string);
		return count;
	}

310 311 312 313 314 315 316 317 318 319
	ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
	if (ret < 0) {
		kfree(input_string);
		return ret;
	}

	mutex_lock(&ksym_tracer_mutex);

	ret = -EINVAL;
	hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
320
		if (entry->attr.bp_addr == ksym_addr) {
321
			/* Check for malformed request: (6) */
322
			if (entry->attr.bp_type != op)
323 324
				changed = 1;
			else
L
Li Zefan 已提交
325
				goto out;
326 327 328 329
			break;
		}
	}
	if (changed) {
330
		unregister_wide_hw_breakpoint(entry->ksym_hbp);
331
		entry->attr.bp_type = op;
332
		ret = 0;
333
		if (op > 0) {
334
			entry->ksym_hbp =
335 336
				register_wide_hw_breakpoint(&entry->attr,
					ksym_hbp_handler);
337
			if (IS_ERR(entry->ksym_hbp))
338 339
				ret = PTR_ERR(entry->ksym_hbp);
			else
L
Li Zefan 已提交
340 341
				goto out;
		}
342
		/* Error or "symbol:---" case: drop it */
343 344 345 346
		ksym_filter_entry_count--;
		hlist_del_rcu(&(entry->ksym_hlist));
		synchronize_rcu();
		kfree(entry);
L
Li Zefan 已提交
347
		goto out;
348 349 350
	} else {
		/* Check for malformed request: (4) */
		if (op == 0)
L
Li Zefan 已提交
351
			goto out;
352 353
		ret = process_new_ksym_entry(ksymname, op, ksym_addr);
	}
L
Li Zefan 已提交
354 355
out:
	mutex_unlock(&ksym_tracer_mutex);
356 357 358

	kfree(input_string);

L
Li Zefan 已提交
359 360
	if (!ret)
		ret = count;
361 362 363 364 365 366 367 368 369 370 371 372
	return ret;
}

static const struct file_operations ksym_tracing_fops = {
	.open		= tracing_open_generic,
	.read		= ksym_trace_filter_read,
	.write		= ksym_trace_filter_write,
};

static void ksym_trace_reset(struct trace_array *tr)
{
	ksym_tracing_enabled = 0;
373
	__ksym_trace_reset();
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
}

static int ksym_trace_init(struct trace_array *tr)
{
	int cpu, ret = 0;

	for_each_online_cpu(cpu)
		tracing_reset(tr, cpu);
	ksym_tracing_enabled = 1;
	ksym_trace_array = tr;

	return ret;
}

static void ksym_trace_print_header(struct seq_file *m)
{
	seq_puts(m,
391 392
		 "#       TASK-PID   CPU#      Symbol                    "
		 "Type    Function\n");
393
	seq_puts(m,
394 395
		 "#          |        |          |                       "
		 " |         |\n");
396 397 398 399 400 401
}

static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
402
	struct ksym_trace_entry *field;
403 404 405 406 407 408 409 410
	char str[KSYM_SYMBOL_LEN];
	int ret;

	if (entry->type != TRACE_KSYM)
		return TRACE_TYPE_UNHANDLED;

	trace_assign_type(field, entry);

411 412
	ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
				entry->pid, iter->cpu, (char *)field->addr);
413 414 415
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

416
	switch (field->type) {
417 418 419
	case HW_BREAKPOINT_R:
		ret = trace_seq_printf(s, " R  ");
		break;
420
	case HW_BREAKPOINT_W:
421 422
		ret = trace_seq_printf(s, " W  ");
		break;
423
	case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
424 425 426 427 428 429 430 431 432 433
		ret = trace_seq_printf(s, " RW ");
		break;
	default:
		return TRACE_TYPE_PARTIAL_LINE;
	}

	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

	sprint_symbol(str, field->ip);
434
	ret = trace_seq_printf(s, "%s\n", str);
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

	return TRACE_TYPE_HANDLED;
}

struct tracer ksym_tracer __read_mostly =
{
	.name		= "ksym_tracer",
	.init		= ksym_trace_init,
	.reset		= ksym_trace_reset,
#ifdef CONFIG_FTRACE_SELFTEST
	.selftest	= trace_selftest_startup_ksym,
#endif
	.print_header   = ksym_trace_print_header,
	.print_line	= ksym_trace_output
};

__init static int init_ksym_trace(void)
{
	struct dentry *d_tracer;
	struct dentry *entry;

	d_tracer = tracing_init_dentry();
	ksym_filter_entry_count = 0;

	entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
				    NULL, &ksym_tracing_fops);
	if (!entry)
		pr_warning("Could not create debugfs "
			   "'ksym_trace_filter' file\n");

	return register_tracer(&ksym_tracer);
}
device_initcall(init_ksym_trace);


#ifdef CONFIG_PROFILE_KSYM_TRACER
static int ksym_tracer_stat_headers(struct seq_file *m)
{
475 476 477 478
	seq_puts(m, "  Access Type ");
	seq_puts(m, "  Symbol                                       Counter\n");
	seq_puts(m, "  ----------- ");
	seq_puts(m, "  ------                                       -------\n");
479 480 481 482 483 484 485 486 487 488 489 490
	return 0;
}

static int ksym_tracer_stat_show(struct seq_file *m, void *v)
{
	struct hlist_node *stat = v;
	struct trace_ksym *entry;
	int access_type = 0;
	char fn_name[KSYM_NAME_LEN];

	entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);

491
	access_type = entry->attr.bp_type;
492 493

	switch (access_type) {
494 495 496
	case HW_BREAKPOINT_R:
		seq_puts(m, "  R           ");
		break;
497
	case HW_BREAKPOINT_W:
498
		seq_puts(m, "  W           ");
499
		break;
500
	case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
501
		seq_puts(m, "  RW          ");
502 503
		break;
	default:
504
		seq_puts(m, "  NA          ");
505 506
	}

507
	if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
508
		seq_printf(m, "  %-36s", fn_name);
509
	else
510 511
		seq_printf(m, "  %-36s", "<NA>");
	seq_printf(m, " %15lu\n", entry->counter);
512 513 514 515 516 517

	return 0;
}

static void *ksym_tracer_stat_start(struct tracer_stat *trace)
{
518
	return ksym_filter_head.first;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
}

static void *
ksym_tracer_stat_next(void *v, int idx)
{
	struct hlist_node *stat = v;

	return stat->next;
}

static struct tracer_stat ksym_tracer_stats = {
	.name = "ksym_tracer",
	.stat_start = ksym_tracer_stat_start,
	.stat_next = ksym_tracer_stat_next,
	.stat_headers = ksym_tracer_stat_headers,
	.stat_show = ksym_tracer_stat_show
};

__init static int ksym_tracer_stat_init(void)
{
	int ret;

	ret = register_stat_tracer(&ksym_tracer_stats);
	if (ret) {
		printk(KERN_WARNING "Warning: could not register "
				    "ksym tracer stats\n");
		return 1;
	}

	return 0;
}
fs_initcall(ksym_tracer_stat_init);
#endif /* CONFIG_PROFILE_KSYM_TRACER */