trace_stack.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
 *
 */
#include <linux/stacktrace.h>
#include <linux/kallsyms.h>
#include <linux/seq_file.h>
#include <linux/spinlock.h>
#include <linux/uaccess.h>
#include <linux/ftrace.h>
#include <linux/module.h>
12
#include <linux/sysctl.h>
13
#include <linux/init.h>
14 15 16

#include <asm/setup.h>

17 18 19 20
#include "trace.h"

#define STACK_TRACE_ENTRIES 500

21 22 23 24
static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
	 { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
static unsigned stack_dump_index[STACK_TRACE_ENTRIES];

25 26 27 28 29
/*
 * Reserve one entry for the passed in ip. This will allow
 * us to remove most or all of the stack size overhead
 * added by the stack tracer itself.
 */
30
static struct stack_trace max_stack_trace = {
31
	.max_entries		= STACK_TRACE_ENTRIES - 1,
32
	.entries		= &stack_dump_trace[0],
33 34 35
};

static unsigned long max_stack_size;
36
static arch_spinlock_t max_stack_lock =
37
	(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
38 39

static DEFINE_PER_CPU(int, trace_active);
40 41 42 43
static DEFINE_MUTEX(stack_sysctl_mutex);

int stack_tracer_enabled;
static int last_stack_tracer_enabled;
44

45 46 47 48 49 50 51
static inline void print_max_stack(void)
{
	long i;
	int size;

	pr_emerg("        Depth    Size   Location    (%d entries)\n"
			   "        -----    ----   --------\n",
52
			   max_stack_trace.nr_entries);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

	for (i = 0; i < max_stack_trace.nr_entries; i++) {
		if (stack_dump_trace[i] == ULONG_MAX)
			break;
		if (i+1 == max_stack_trace.nr_entries ||
				stack_dump_trace[i+1] == ULONG_MAX)
			size = stack_dump_index[i];
		else
			size = stack_dump_index[i] - stack_dump_index[i+1];

		pr_emerg("%3ld) %8d   %5d   %pS\n", i, stack_dump_index[i],
				size, (void *)stack_dump_trace[i]);
	}
}

68
static inline void
69
check_stack(unsigned long ip, unsigned long *stack)
70
{
71
	unsigned long this_size, flags; unsigned long *p, *top, *start;
72 73
	static int tracer_frame;
	int frame_size = ACCESS_ONCE(tracer_frame);
74
	int i, x;
75

76
	this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
77
	this_size = THREAD_SIZE - this_size;
78 79
	/* Remove the frame of the tracer */
	this_size -= frame_size;
80 81 82 83

	if (this_size <= max_stack_size)
		return;

84
	/* we do not handle interrupt stacks yet */
85
	if (!object_is_on_stack(stack))
86 87
		return;

88 89 90 91
	/* Can't do this from NMI context (can cause deadlocks) */
	if (in_nmi())
		return;

92
	local_irq_save(flags);
93
	arch_spin_lock(&max_stack_lock);
94

95 96 97 98 99 100
	/*
	 * RCU may not be watching, make it see us.
	 * The stack trace code uses rcu_sched.
	 */
	rcu_irq_enter();

101 102 103 104
	/* In case another CPU set the tracer_frame on us */
	if (unlikely(!frame_size))
		this_size -= tracer_frame;

105 106 107 108 109 110
	/* a race could have already updated it */
	if (this_size <= max_stack_size)
		goto out;

	max_stack_size = this_size;

111
	max_stack_trace.nr_entries = 0;
112
	max_stack_trace.skip = 3;
113 114 115

	save_stack_trace(&max_stack_trace);

116 117 118 119 120
	/* Skip over the overhead of the stack tracer itself */
	for (i = 0; i < max_stack_trace.nr_entries; i++) {
		if (stack_dump_trace[i] == ip)
			break;
	}
121

122 123 124
	/*
	 * Now find where in the stack these are.
	 */
125
	x = 0;
126
	start = stack;
127 128 129 130 131 132 133 134 135 136 137
	top = (unsigned long *)
		(((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);

	/*
	 * Loop through all the entries. One of the entries may
	 * for some reason be missed on the stack, so we may
	 * have to account for them. If they are all there, this
	 * loop will only happen once. This code only takes place
	 * on a new max, so it is far from a fast path.
	 */
	while (i < max_stack_trace.nr_entries) {
138
		int found = 0;
139

140
		stack_dump_index[x] = this_size;
141 142 143
		p = start;

		for (; p < top && i < max_stack_trace.nr_entries; p++) {
144 145
			if (stack_dump_trace[i] == ULONG_MAX)
				break;
146
			if (*p == stack_dump_trace[i]) {
147 148
				stack_dump_trace[x] = stack_dump_trace[i++];
				this_size = stack_dump_index[x++] =
149
					(top - p) * sizeof(unsigned long);
150
				found = 1;
151 152
				/* Start the search from here */
				start = p + 1;
153 154 155 156 157 158 159
				/*
				 * We do not want to show the overhead
				 * of the stack tracer stack in the
				 * max stack. If we haven't figured
				 * out what that is, then figure it out
				 * now.
				 */
160
				if (unlikely(!tracer_frame)) {
161 162 163 164
					tracer_frame = (p - stack) *
						sizeof(unsigned long);
					max_stack_size -= tracer_frame;
				}
165 166 167
			}
		}

168 169
		if (!found)
			i++;
170 171
	}

172 173 174 175
	max_stack_trace.nr_entries = x;
	for (; x < i; x++)
		stack_dump_trace[x] = ULONG_MAX;

176
	if (task_stack_end_corrupted(current)) {
177 178 179 180
		print_max_stack();
		BUG();
	}

181
 out:
182
	rcu_irq_exit();
183
	arch_spin_unlock(&max_stack_lock);
184
	local_irq_restore(flags);
185 186 187
}

static void
188 189
stack_trace_call(unsigned long ip, unsigned long parent_ip,
		 struct ftrace_ops *op, struct pt_regs *pt_regs)
190
{
191
	unsigned long stack;
192
	int cpu;
193

194
	preempt_disable_notrace();
195 196 197 198 199 200

	cpu = raw_smp_processor_id();
	/* no atomic needed, we only modify this variable by this cpu */
	if (per_cpu(trace_active, cpu)++ != 0)
		goto out;

201
	ip += MCOUNT_INSN_SIZE;
202 203

	check_stack(ip, &stack);
204 205 206 207

 out:
	per_cpu(trace_active, cpu)--;
	/* prevent recursion in schedule */
208
	preempt_enable_notrace();
209 210 211 212 213
}

static struct ftrace_ops trace_ops __read_mostly =
{
	.func = stack_trace_call,
214
	.flags = FTRACE_OPS_FL_RECURSION_SAFE,
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
};

static ssize_t
stack_max_size_read(struct file *filp, char __user *ubuf,
		    size_t count, loff_t *ppos)
{
	unsigned long *ptr = filp->private_data;
	char buf[64];
	int r;

	r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
	if (r > sizeof(buf))
		r = sizeof(buf);
	return simple_read_from_buffer(ubuf, count, ppos, buf, r);
}

static ssize_t
stack_max_size_write(struct file *filp, const char __user *ubuf,
		     size_t count, loff_t *ppos)
{
	long *ptr = filp->private_data;
	unsigned long val, flags;
	int ret;
238
	int cpu;
239

240 241
	ret = kstrtoul_from_user(ubuf, count, 10, &val);
	if (ret)
242 243
		return ret;

244
	local_irq_save(flags);
245 246 247 248 249 250 251 252 253

	/*
	 * In case we trace inside arch_spin_lock() or after (NMI),
	 * we will cause circular lock, so we also need to increase
	 * the percpu trace_active here.
	 */
	cpu = smp_processor_id();
	per_cpu(trace_active, cpu)++;

254
	arch_spin_lock(&max_stack_lock);
255
	*ptr = val;
256
	arch_spin_unlock(&max_stack_lock);
257 258

	per_cpu(trace_active, cpu)--;
259
	local_irq_restore(flags);
260 261 262 263

	return count;
}

264
static const struct file_operations stack_max_size_fops = {
265 266 267
	.open		= tracing_open_generic,
	.read		= stack_max_size_read,
	.write		= stack_max_size_write,
268
	.llseek		= default_llseek,
269 270 271
};

static void *
272
__next(struct seq_file *m, loff_t *pos)
273
{
274
	long n = *pos - 1;
275

276
	if (n > max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
277 278
		return NULL;

279
	m->private = (void *)n;
280
	return &m->private;
281 282
}

283 284
static void *
t_next(struct seq_file *m, void *v, loff_t *pos)
285
{
286 287 288
	(*pos)++;
	return __next(m, pos);
}
289

290 291
static void *t_start(struct seq_file *m, loff_t *pos)
{
292 293
	int cpu;

294
	local_irq_disable();
295 296 297 298

	cpu = smp_processor_id();
	per_cpu(trace_active, cpu)++;

299
	arch_spin_lock(&max_stack_lock);
300

301 302 303
	if (*pos == 0)
		return SEQ_START_TOKEN;

304
	return __next(m, pos);
305 306 307 308
}

static void t_stop(struct seq_file *m, void *p)
{
309 310
	int cpu;

311
	arch_spin_unlock(&max_stack_lock);
312 313 314 315

	cpu = smp_processor_id();
	per_cpu(trace_active, cpu)--;

316 317 318
	local_irq_enable();
}

319
static void trace_lookup_stack(struct seq_file *m, long i)
320
{
321
	unsigned long addr = stack_dump_trace[i];
322

323
	seq_printf(m, "%pS\n", (void *)addr);
324 325
}

326 327 328 329 330 331 332 333 334 335 336
static void print_disabled(struct seq_file *m)
{
	seq_puts(m, "#\n"
		 "#  Stack tracer disabled\n"
		 "#\n"
		 "# To enable the stack tracer, either add 'stacktrace' to the\n"
		 "# kernel command line\n"
		 "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
		 "#\n");
}

337 338
static int t_show(struct seq_file *m, void *v)
{
339
	long i;
340 341
	int size;

342
	if (v == SEQ_START_TOKEN) {
343
		seq_printf(m, "        Depth    Size   Location"
344
			   "    (%d entries)\n"
345
			   "        -----    ----   --------\n",
346
			   max_stack_trace.nr_entries);
347 348 349 350

		if (!stack_tracer_enabled && !max_stack_size)
			print_disabled(m);

351 352
		return 0;
	}
353

354 355
	i = *(long *)v;

356 357
	if (i >= max_stack_trace.nr_entries ||
	    stack_dump_trace[i] == ULONG_MAX)
358 359
		return 0;

360 361 362 363 364 365 366 367 368
	if (i+1 == max_stack_trace.nr_entries ||
	    stack_dump_trace[i+1] == ULONG_MAX)
		size = stack_dump_index[i];
	else
		size = stack_dump_index[i] - stack_dump_index[i+1];

	seq_printf(m, "%3ld) %8d   %5d   ", i, stack_dump_index[i], size);

	trace_lookup_stack(m, i);
369 370 371 372

	return 0;
}

373
static const struct seq_operations stack_trace_seq_ops = {
374 375 376 377 378 379 380 381
	.start		= t_start,
	.next		= t_next,
	.stop		= t_stop,
	.show		= t_show,
};

static int stack_trace_open(struct inode *inode, struct file *file)
{
382
	return seq_open(file, &stack_trace_seq_ops);
383 384
}

385
static const struct file_operations stack_trace_fops = {
386 387 388
	.open		= stack_trace_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
389
	.release	= seq_release,
390 391
};

392 393 394 395 396 397 398 399 400 401 402
static int
stack_trace_filter_open(struct inode *inode, struct file *file)
{
	return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
				 inode, file);
}

static const struct file_operations stack_trace_filter_fops = {
	.open = stack_trace_filter_open,
	.read = seq_read,
	.write = ftrace_filter_write,
403
	.llseek = tracing_lseek,
404 405 406
	.release = ftrace_regex_release,
};

407 408
int
stack_trace_sysctl(struct ctl_table *table, int write,
409
		   void __user *buffer, size_t *lenp,
410 411 412 413 414 415
		   loff_t *ppos)
{
	int ret;

	mutex_lock(&stack_sysctl_mutex);

416
	ret = proc_dointvec(table, write, buffer, lenp, ppos);
417 418

	if (ret || !write ||
419
	    (last_stack_tracer_enabled == !!stack_tracer_enabled))
420 421
		goto out;

422
	last_stack_tracer_enabled = !!stack_tracer_enabled;
423 424 425 426 427 428 429 430 431 432 433

	if (stack_tracer_enabled)
		register_ftrace_function(&trace_ops);
	else
		unregister_ftrace_function(&trace_ops);

 out:
	mutex_unlock(&stack_sysctl_mutex);
	return ret;
}

434 435
static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;

436 437
static __init int enable_stacktrace(char *str)
{
438 439 440
	if (strncmp(str, "_filter=", 8) == 0)
		strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);

441 442
	stack_tracer_enabled = 1;
	last_stack_tracer_enabled = 1;
443 444 445 446
	return 1;
}
__setup("stacktrace", enable_stacktrace);

447 448 449 450 451
static __init int stack_trace_init(void)
{
	struct dentry *d_tracer;

	d_tracer = tracing_init_dentry();
452
	if (IS_ERR(d_tracer))
453
		return 0;
454

455 456
	trace_create_file("stack_max_size", 0644, d_tracer,
			&max_stack_size, &stack_max_size_fops);
457

458 459
	trace_create_file("stack_trace", 0444, d_tracer,
			NULL, &stack_trace_fops);
460

461 462 463
	trace_create_file("stack_trace_filter", 0444, d_tracer,
			NULL, &stack_trace_filter_fops);

464 465 466
	if (stack_trace_filter_buf[0])
		ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);

467
	if (stack_tracer_enabled)
468
		register_ftrace_function(&trace_ops);
469 470 471 472 473

	return 0;
}

device_initcall(stack_trace_init);