xref: /openbmc/linux/arch/powerpc/kernel/stacktrace.c (revision e2c75e76)
1 /*
2  * Stack trace utility
3  *
4  * Copyright 2008 Christoph Hellwig, IBM Corp.
5  *
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12 
13 #include <linux/export.h>
14 #include <linux/sched.h>
15 #include <linux/sched/debug.h>
16 #include <linux/stacktrace.h>
17 #include <asm/ptrace.h>
18 #include <asm/processor.h>
19 
20 /*
21  * Save stack-backtrace addresses into a stack_trace buffer.
22  */
23 static void save_context_stack(struct stack_trace *trace, unsigned long sp,
24 			struct task_struct *tsk, int savesched)
25 {
26 	for (;;) {
27 		unsigned long *stack = (unsigned long *) sp;
28 		unsigned long newsp, ip;
29 
30 		if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
31 			return;
32 
33 		newsp = stack[0];
34 		ip = stack[STACK_FRAME_LR_SAVE];
35 
36 		if (savesched || !in_sched_functions(ip)) {
37 			if (!trace->skip)
38 				trace->entries[trace->nr_entries++] = ip;
39 			else
40 				trace->skip--;
41 		}
42 
43 		if (trace->nr_entries >= trace->max_entries)
44 			return;
45 
46 		sp = newsp;
47 	}
48 }
49 
50 void save_stack_trace(struct stack_trace *trace)
51 {
52 	unsigned long sp;
53 
54 	sp = current_stack_pointer();
55 
56 	save_context_stack(trace, sp, current, 1);
57 }
58 EXPORT_SYMBOL_GPL(save_stack_trace);
59 
60 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
61 {
62 	unsigned long sp;
63 
64 	if (tsk == current)
65 		sp = current_stack_pointer();
66 	else
67 		sp = tsk->thread.ksp;
68 
69 	save_context_stack(trace, sp, tsk, 0);
70 }
71 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
72 
73 void
74 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
75 {
76 	save_context_stack(trace, regs->gpr[1], current, 0);
77 }
78 EXPORT_SYMBOL_GPL(save_stack_trace_regs);
79