stacktrace.c 2.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7
/*
 * Stack trace management functions
 *
 *  Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
 */
#include <linux/sched.h>
8
#include <linux/sched/debug.h>
9
#include <linux/sched/task_stack.h>
10
#include <linux/stacktrace.h>
11
#include <linux/export.h>
12 13 14 15 16 17
#include <asm/stacktrace.h>

/*
 * Save stack-backtrace addresses into a stack_trace buffer:
 */
static void save_raw_context_stack(struct stack_trace *trace,
Aaro Koskinen's avatar
Aaro Koskinen committed
18
	unsigned long reg29, int savesched)
19 20 21 22 23 24
{
	unsigned long *sp = (unsigned long *)reg29;
	unsigned long addr;

	while (!kstack_end(sp)) {
		addr = *sp++;
Aaro Koskinen's avatar
Aaro Koskinen committed
25 26
		if (__kernel_text_address(addr) &&
		    (savesched || !in_sched_functions(addr))) {
27 28
			if (trace->skip > 0)
				trace->skip--;
29
			else
30
				trace->entries[trace->nr_entries++] = addr;
31 32 33 34 35 36
			if (trace->nr_entries >= trace->max_entries)
				break;
		}
	}
}

Ken Chen's avatar
Ken Chen committed
37
static void save_context_stack(struct stack_trace *trace,
Aaro Koskinen's avatar
Aaro Koskinen committed
38
	struct task_struct *tsk, struct pt_regs *regs, int savesched)
39 40 41 42 43 44 45
{
	unsigned long sp = regs->regs[29];
#ifdef CONFIG_KALLSYMS
	unsigned long ra = regs->regs[31];
	unsigned long pc = regs->cp0_epc;

	if (raw_show_trace || !__kernel_text_address(pc)) {
46
		unsigned long stack_page =
Ken Chen's avatar
Ken Chen committed
47
			(unsigned long)task_stack_page(tsk);
48 49
		if (stack_page && sp >= stack_page &&
		    sp <= stack_page + THREAD_SIZE - 32)
Aaro Koskinen's avatar
Aaro Koskinen committed
50
			save_raw_context_stack(trace, sp, savesched);
51
		return;
52 53
	}
	do {
Aaro Koskinen's avatar
Aaro Koskinen committed
54 55 56 57 58 59 60 61
		if (savesched || !in_sched_functions(pc)) {
			if (trace->skip > 0)
				trace->skip--;
			else
				trace->entries[trace->nr_entries++] = pc;
			if (trace->nr_entries >= trace->max_entries)
				break;
		}
Ken Chen's avatar
Ken Chen committed
62
		pc = unwind_stack(tsk, &sp, pc, &ra);
63 64
	} while (pc);
#else
Aaro Koskinen's avatar
Aaro Koskinen committed
65
	save_raw_context_stack(trace, sp, savesched);
66 67 68 69 70 71
#endif
}

/*
 * Save stack-backtrace addresses into a stack_trace buffer.
 */
72
void save_stack_trace(struct stack_trace *trace)
Ken Chen's avatar
Ken Chen committed
73 74 75 76 77 78
{
	save_stack_trace_tsk(current, trace);
}
EXPORT_SYMBOL_GPL(save_stack_trace);

void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
79 80 81 82 83 84
{
	struct pt_regs dummyregs;
	struct pt_regs *regs = &dummyregs;

	WARN_ON(trace->nr_entries || !trace->max_entries);

Ken Chen's avatar
Ken Chen committed
85 86 87 88 89 90
	if (tsk != current) {
		regs->regs[29] = tsk->thread.reg29;
		regs->regs[31] = 0;
		regs->cp0_epc = tsk->thread.reg31;
	} else
		prepare_frametrace(regs);
Aaro Koskinen's avatar
Aaro Koskinen committed
91
	save_context_stack(trace, tsk, regs, tsk == current);
92
}
Ken Chen's avatar
Ken Chen committed
93
EXPORT_SYMBOL_GPL(save_stack_trace_tsk);