stacktrace.c 3.1 KB
Newer Older
1 2 3
/*
 * Stack trace management functions
 *
I
Ingo Molnar 已提交
4
 *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 6
 */
#include <linux/sched.h>
7
#include <linux/sched/debug.h>
8
#include <linux/sched/task_stack.h>
9
#include <linux/stacktrace.h>
10
#include <linux/export.h>
11
#include <linux/uaccess.h>
12
#include <asm/stacktrace.h>
13
#include <asm/unwind.h>
14

15 16
static int save_stack_address(struct stack_trace *trace, unsigned long addr,
			      bool nosched)
17
{
18
	if (nosched && in_sched_functions(addr))
19
		return 0;
20

21 22
	if (trace->skip > 0) {
		trace->skip--;
23
		return 0;
24 25
	}

26 27 28 29 30
	if (trace->nr_entries >= trace->max_entries)
		return -1;

	trace->entries[trace->nr_entries++] = addr;
	return 0;
31 32
}

33 34 35
static void __save_stack_trace(struct stack_trace *trace,
			       struct task_struct *task, struct pt_regs *regs,
			       bool nosched)
A
Arjan van de Ven 已提交
36
{
37 38
	struct unwind_state state;
	unsigned long addr;
A
Arjan van de Ven 已提交
39

40 41
	if (regs)
		save_stack_address(trace, regs->ip, nosched);
42

43 44 45 46 47 48 49 50 51 52
	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
	     unwind_next_frame(&state)) {
		addr = unwind_get_return_address(&state);
		if (!addr || save_stack_address(trace, addr, nosched))
			break;
	}

	if (trace->nr_entries < trace->max_entries)
		trace->entries[trace->nr_entries++] = ULONG_MAX;
}
A
Arjan van de Ven 已提交
53

54 55 56
/*
 * Save stack-backtrace addresses into a stack_trace buffer.
 */
C
Christoph Hellwig 已提交
57
void save_stack_trace(struct stack_trace *trace)
58
{
59
	__save_stack_trace(trace, current, NULL, false);
60
}
61
EXPORT_SYMBOL_GPL(save_stack_trace);
A
Arjan van de Ven 已提交
62

63
void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
64
{
65
	__save_stack_trace(trace, current, regs, false);
66 67
}

A
Arjan van de Ven 已提交
68 69
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
{
70 71 72
	if (!try_get_task_stack(tsk))
		return;

73
	__save_stack_trace(trace, tsk, NULL, true);
74 75

	put_task_stack(tsk);
A
Arjan van de Ven 已提交
76
}
77
EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
78 79 80

/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */

81
struct stack_frame_user {
82
	const void __user	*next_fp;
83
	unsigned long		ret_addr;
84 85
};

86 87
static int
copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	int ret;

	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
		return 0;

	ret = 1;
	pagefault_disable();
	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
		ret = 0;
	pagefault_enable();

	return ret;
}

103 104 105 106 107 108 109 110 111
static inline void __save_stack_trace_user(struct stack_trace *trace)
{
	const struct pt_regs *regs = task_pt_regs(current);
	const void __user *fp = (const void __user *)regs->bp;

	if (trace->nr_entries < trace->max_entries)
		trace->entries[trace->nr_entries++] = regs->ip;

	while (trace->nr_entries < trace->max_entries) {
112
		struct stack_frame_user frame;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

		frame.next_fp = NULL;
		frame.ret_addr = 0;
		if (!copy_stack_frame(fp, &frame))
			break;
		if ((unsigned long)fp < regs->sp)
			break;
		if (frame.ret_addr) {
			trace->entries[trace->nr_entries++] =
				frame.ret_addr;
		}
		if (fp == frame.next_fp)
			break;
		fp = frame.next_fp;
	}
}

130 131 132 133 134 135
void save_stack_trace_user(struct stack_trace *trace)
{
	/*
	 * Trace user stack if we are not a kernel thread
	 */
	if (current->mm) {
136
		__save_stack_trace_user(trace);
137 138 139 140 141
	}
	if (trace->nr_entries < trace->max_entries)
		trace->entries[trace->nr_entries++] = ULONG_MAX;
}