xref: /linux-6.15/arch/sh/kernel/dumpstack.c (revision cec8d0e7)
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  *  Copyright (C) 2009  Matt Fleming
5  *  Copyright (C) 2002 - 2012  Paul Mundt
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file "COPYING" in the main directory of this archive
9  * for more details.
10  */
11 #include <linux/kallsyms.h>
12 #include <linux/ftrace.h>
13 #include <linux/debug_locks.h>
14 #include <linux/sched/debug.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/kdebug.h>
17 #include <linux/export.h>
18 #include <linux/uaccess.h>
19 #include <asm/unwinder.h>
20 #include <asm/stacktrace.h>
21 
22 void dump_mem(const char *str, unsigned long bottom, unsigned long top)
23 {
24 	unsigned long p;
25 	int i;
26 
27 	printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
28 
29 	for (p = bottom & ~31; p < top; ) {
30 		printk("%04lx: ", p & 0xffff);
31 
32 		for (i = 0; i < 8; i++, p += 4) {
33 			unsigned int val;
34 
35 			if (p < bottom || p >= top)
36 				printk("         ");
37 			else {
38 				if (__get_user(val, (unsigned int __user *)p)) {
39 					printk("\n");
40 					return;
41 				}
42 				printk("%08x ", val);
43 			}
44 		}
45 		printk("\n");
46 	}
47 }
48 
49 void printk_address(unsigned long address, int reliable)
50 {
51 	printk(" [<%p>] %s%pS\n", (void *) address,
52 			reliable ? "" : "? ", (void *) address);
53 }
54 
55 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
56 static void
57 print_ftrace_graph_addr(unsigned long addr, void *data,
58 			const struct stacktrace_ops *ops,
59 			struct thread_info *tinfo, int *graph)
60 {
61 	struct task_struct *task = tinfo->task;
62 	struct ftrace_ret_stack *ret_stack;
63 	unsigned long ret_addr;
64 
65 	if (addr != (unsigned long)return_to_handler)
66 		return;
67 
68 	if (!task->ret_stack)
69 		return;
70 
71 	ret_stack = ftrace_graph_get_ret_stack(task, *graph);
72 	if (!ret_stack)
73 		return;
74 
75 	ret_addr = ret_stack->ret;
76 
77 	ops->address(data, ret_addr, 1);
78 
79 	(*graph)++;
80 }
81 #else
82 static inline void
83 print_ftrace_graph_addr(unsigned long addr, void *data,
84 			const struct stacktrace_ops *ops,
85 			struct thread_info *tinfo, int *graph)
86 { }
87 #endif
88 
89 void
90 stack_reader_dump(struct task_struct *task, struct pt_regs *regs,
91 		  unsigned long *sp, const struct stacktrace_ops *ops,
92 		  void *data)
93 {
94 	struct thread_info *context;
95 	int graph = 0;
96 
97 	context = (struct thread_info *)
98 		((unsigned long)sp & (~(THREAD_SIZE - 1)));
99 
100 	while (!kstack_end(sp)) {
101 		unsigned long addr = *sp++;
102 
103 		if (__kernel_text_address(addr)) {
104 			ops->address(data, addr, 1);
105 
106 			print_ftrace_graph_addr(addr, data, ops,
107 						context, &graph);
108 		}
109 	}
110 }
111 
112 static int print_trace_stack(void *data, char *name)
113 {
114 	printk("%s <%s> ", (char *)data, name);
115 	return 0;
116 }
117 
118 /*
119  * Print one address/symbol entries per line.
120  */
121 static void print_trace_address(void *data, unsigned long addr, int reliable)
122 {
123 	printk("%s", (char *)data);
124 	printk_address(addr, reliable);
125 }
126 
127 static const struct stacktrace_ops print_trace_ops = {
128 	.stack = print_trace_stack,
129 	.address = print_trace_address,
130 };
131 
132 void show_trace(struct task_struct *tsk, unsigned long *sp,
133 		struct pt_regs *regs)
134 {
135 	if (regs && user_mode(regs))
136 		return;
137 
138 	printk("\nCall trace:\n");
139 
140 	unwind_stack(tsk, regs, sp, &print_trace_ops, "");
141 
142 	printk("\n");
143 
144 	if (!tsk)
145 		tsk = current;
146 
147 	debug_show_held_locks(tsk);
148 }
149 
150 void show_stack(struct task_struct *tsk, unsigned long *sp)
151 {
152 	unsigned long stack;
153 
154 	if (!tsk)
155 		tsk = current;
156 	if (tsk == current)
157 		sp = (unsigned long *)current_stack_pointer;
158 	else
159 		sp = (unsigned long *)tsk->thread.sp;
160 
161 	stack = (unsigned long)sp;
162 	dump_mem("Stack: ", stack, THREAD_SIZE +
163 		 (unsigned long)task_stack_page(tsk));
164 	show_trace(tsk, sp, NULL);
165 }
166