stacktrace.c 3.37 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
3
4
5
6
7
8
9
10
11
12
/*
 * Copyright (C) 2008 ARM Limited
 * Copyright (C) 2014 Regents of the University of California
 */

#include <linux/export.h>
#include <linux/kallsyms.h>
#include <linux/sched.h>
#include <linux/sched/debug.h>
#include <linux/sched/task_stack.h>
#include <linux/stacktrace.h>
13
#include <linux/ftrace.h>
14

Kefeng Wang's avatar
Kefeng Wang committed
15
16
#include <asm/stacktrace.h>

17
register unsigned long sp_in_global __asm__("sp");
18

19
20
#ifdef CONFIG_FRAME_POINTER

Mao Han's avatar
Mao Han committed
21
void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
Kefeng Wang's avatar
Kefeng Wang committed
22
			     bool (*fn)(void *, unsigned long), void *arg)
23
24
25
26
{
	unsigned long fp, sp, pc;

	if (regs) {
27
28
29
		fp = frame_pointer(regs);
		sp = user_stack_pointer(regs);
		pc = instruction_pointer(regs);
30
31
32
33
	} else if (task == current) {
		fp = (unsigned long)__builtin_frame_address(1);
		sp = (unsigned long)__builtin_frame_address(0);
		pc = (unsigned long)__builtin_return_address(0);
34
35
36
37
38
39
40
41
42
43
44
	} else {
		/* task blocked in __switch_to */
		fp = task->thread.s[0];
		sp = task->thread.sp;
		pc = task->thread.ra;
	}

	for (;;) {
		unsigned long low, high;
		struct stackframe *frame;

Kefeng Wang's avatar
Kefeng Wang committed
45
		if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
46
47
48
49
50
51
52
53
54
55
			break;

		/* Validate frame pointer */
		low = sp + sizeof(struct stackframe);
		high = ALIGN(sp, THREAD_SIZE);
		if (unlikely(fp < low || fp > high || fp & 0x7))
			break;
		/* Unwind stack frame */
		frame = (struct stackframe *)fp - 1;
		sp = fp;
56
57
58
59
60
61
62
63
64
		if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
			fp = frame->ra;
			pc = regs->ra;
		} else {
			fp = frame->fp;
			pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
						   (unsigned long *)(fp - 8));
		}

65
66
67
68
69
	}
}

#else /* !CONFIG_FRAME_POINTER */

70
void notrace walk_stackframe(struct task_struct *task,
71
	struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
72
73
74
75
76
{
	unsigned long sp, pc;
	unsigned long *ksp;

	if (regs) {
77
78
		sp = user_stack_pointer(regs);
		pc = instruction_pointer(regs);
79
	} else if (task == NULL || task == current) {
80
		sp = sp_in_global;
81
82
83
84
85
86
87
88
89
90
91
92
		pc = (unsigned long)walk_stackframe;
	} else {
		/* task blocked in __switch_to */
		sp = task->thread.sp;
		pc = task->thread.ra;
	}

	if (unlikely(sp & 0x7))
		return;

	ksp = (unsigned long *)sp;
	while (!kstack_end(ksp)) {
Kefeng Wang's avatar
Kefeng Wang committed
93
		if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
94
95
96
97
98
99
100
			break;
		pc = (*ksp++) - 0x4;
	}
}

#endif /* CONFIG_FRAME_POINTER */

Kefeng Wang's avatar
Kefeng Wang committed
101
static bool print_trace_address(void *arg, unsigned long pc)
102
{
103
104
105
	const char *loglvl = arg;

	print_ip_sym(loglvl, pc);
Kefeng Wang's avatar
Kefeng Wang committed
106
	return true;
107
108
}

109
noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
110
111
112
113
114
		    const char *loglvl)
{
	walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
}

115
void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
116
{
117
	pr_cont("%sCall Trace:\n", loglvl);
118
	dump_backtrace(NULL, task, loglvl);
119
120
}

121
static bool save_wchan(void *arg, unsigned long pc)
122
123
124
125
{
	if (!in_sched_functions(pc)) {
		unsigned long *p = arg;
		*p = pc;
Kefeng Wang's avatar
Kefeng Wang committed
126
		return false;
127
	}
Kefeng Wang's avatar
Kefeng Wang committed
128
	return true;
129
130
131
132
133
134
135
136
137
138
139
140
141
}

unsigned long get_wchan(struct task_struct *task)
{
	unsigned long pc = 0;

	if (likely(task && task != current && task->state != TASK_RUNNING))
		walk_stackframe(task, NULL, save_wchan, &pc);
	return pc;
}

#ifdef CONFIG_STACKTRACE

142
noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
Kefeng Wang's avatar
Kefeng Wang committed
143
		     struct task_struct *task, struct pt_regs *regs)
144
{
Kefeng Wang's avatar
Kefeng Wang committed
145
	walk_stackframe(task, regs, consume_entry, cookie);
146
147
148
}

#endif /* CONFIG_STACKTRACE */