blob: 58f5cd76c8c3fedfdb45d46300dc34d67b030e24 [file] [log] [blame]
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +09001/*
2 * arch/mips/kernel/stacktrace.c
3 *
4 * Stack trace management functions
5 *
6 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
7 */
8#include <linux/sched.h>
9#include <linux/stacktrace.h>
Andrew Morton36bd53d2008-07-23 00:58:13 -070010#include <linux/module.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090011#include <asm/stacktrace.h>
12
13/*
14 * Save stack-backtrace addresses into a stack_trace buffer:
15 */
16static void save_raw_context_stack(struct stack_trace *trace,
Atsushi Nemoto23126692006-09-28 19:15:33 +090017 unsigned long reg29)
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090018{
19 unsigned long *sp = (unsigned long *)reg29;
20 unsigned long addr;
21
22 while (!kstack_end(sp)) {
23 addr = *sp++;
24 if (__kernel_text_address(addr)) {
Atsushi Nemoto23126692006-09-28 19:15:33 +090025 if (trace->skip > 0)
26 trace->skip--;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090027 else
Atsushi Nemoto23126692006-09-28 19:15:33 +090028 trace->entries[trace->nr_entries++] = addr;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090029 if (trace->nr_entries >= trace->max_entries)
30 break;
31 }
32 }
33}
34
Ken Chen2ec220e2008-11-10 11:26:08 +030035static void save_context_stack(struct stack_trace *trace,
36 struct task_struct *tsk, struct pt_regs *regs)
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090037{
38 unsigned long sp = regs->regs[29];
39#ifdef CONFIG_KALLSYMS
40 unsigned long ra = regs->regs[31];
41 unsigned long pc = regs->cp0_epc;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090042
43 if (raw_show_trace || !__kernel_text_address(pc)) {
Atsushi Nemoto19246002006-09-29 18:02:51 +090044 unsigned long stack_page =
Ken Chen2ec220e2008-11-10 11:26:08 +030045 (unsigned long)task_stack_page(tsk);
Atsushi Nemoto23126692006-09-28 19:15:33 +090046 if (stack_page && sp >= stack_page &&
47 sp <= stack_page + THREAD_SIZE - 32)
48 save_raw_context_stack(trace, sp);
Atsushi Nemoto19246002006-09-29 18:02:51 +090049 return;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090050 }
51 do {
Atsushi Nemoto23126692006-09-28 19:15:33 +090052 if (trace->skip > 0)
53 trace->skip--;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090054 else
Atsushi Nemoto23126692006-09-28 19:15:33 +090055 trace->entries[trace->nr_entries++] = pc;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090056 if (trace->nr_entries >= trace->max_entries)
57 break;
Ken Chen2ec220e2008-11-10 11:26:08 +030058 pc = unwind_stack(tsk, &sp, pc, &ra);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090059 } while (pc);
60#else
Atsushi Nemotoeea32d42006-10-16 22:48:49 +090061 save_raw_context_stack(trace, sp);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090062#endif
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090063}
64
65/*
66 * Save stack-backtrace addresses into a stack_trace buffer.
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090067 */
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070068void save_stack_trace(struct stack_trace *trace)
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090069{
Ken Chen2ec220e2008-11-10 11:26:08 +030070 save_stack_trace_tsk(current, trace);
71}
72EXPORT_SYMBOL_GPL(save_stack_trace);
73
74void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
75{
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090076 struct pt_regs dummyregs;
77 struct pt_regs *regs = &dummyregs;
78
79 WARN_ON(trace->nr_entries || !trace->max_entries);
80
Ken Chen2ec220e2008-11-10 11:26:08 +030081 if (tsk != current) {
82 regs->regs[29] = tsk->thread.reg29;
83 regs->regs[31] = 0;
84 regs->cp0_epc = tsk->thread.reg31;
85 } else
86 prepare_frametrace(regs);
87 save_context_stack(trace, tsk, regs);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090088}
Ken Chen2ec220e2008-11-10 11:26:08 +030089EXPORT_SYMBOL_GPL(save_stack_trace_tsk);