xref: /linux-6.15/arch/sh/kernel/dumpstack.c (revision b17b0153)
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/kdebug.h>
16 #include <linux/export.h>
17 #include <linux/uaccess.h>
18 #include <asm/unwinder.h>
19 #include <asm/stacktrace.h>
20 
21 void dump_mem(const char *str, unsigned long bottom, unsigned long top)
22 {
23 	unsigned long p;
24 	int i;
25 
26 	printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
27 
28 	for (p = bottom & ~31; p < top; ) {
29 		printk("%04lx: ", p & 0xffff);
30 
31 		for (i = 0; i < 8; i++, p += 4) {
32 			unsigned int val;
33 
34 			if (p < bottom || p >= top)
35 				printk("         ");
36 			else {
37 				if (__get_user(val, (unsigned int __user *)p)) {
38 					printk("\n");
39 					return;
40 				}
41 				printk("%08x ", val);
42 			}
43 		}
44 		printk("\n");
45 	}
46 }
47 
48 void printk_address(unsigned long address, int reliable)
49 {
50 	printk(" [<%p>] %s%pS\n", (void *) address,
51 			reliable ? "" : "? ", (void *) address);
52 }
53 
54 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
55 static void
56 print_ftrace_graph_addr(unsigned long addr, void *data,
57 			const struct stacktrace_ops *ops,
58 			struct thread_info *tinfo, int *graph)
59 {
60 	struct task_struct *task = tinfo->task;
61 	unsigned long ret_addr;
62 	int index = task->curr_ret_stack;
63 
64 	if (addr != (unsigned long)return_to_handler)
65 		return;
66 
67 	if (!task->ret_stack || index < *graph)
68 		return;
69 
70 	index -= *graph;
71 	ret_addr = task->ret_stack[index].ret;
72 
73 	ops->address(data, ret_addr, 1);
74 
75 	(*graph)++;
76 }
77 #else
78 static inline void
79 print_ftrace_graph_addr(unsigned long addr, void *data,
80 			const struct stacktrace_ops *ops,
81 			struct thread_info *tinfo, int *graph)
82 { }
83 #endif
84 
85 void
86 stack_reader_dump(struct task_struct *task, struct pt_regs *regs,
87 		  unsigned long *sp, const struct stacktrace_ops *ops,
88 		  void *data)
89 {
90 	struct thread_info *context;
91 	int graph = 0;
92 
93 	context = (struct thread_info *)
94 		((unsigned long)sp & (~(THREAD_SIZE - 1)));
95 
96 	while (!kstack_end(sp)) {
97 		unsigned long addr = *sp++;
98 
99 		if (__kernel_text_address(addr)) {
100 			ops->address(data, addr, 1);
101 
102 			print_ftrace_graph_addr(addr, data, ops,
103 						context, &graph);
104 		}
105 	}
106 }
107 
108 static int print_trace_stack(void *data, char *name)
109 {
110 	printk("%s <%s> ", (char *)data, name);
111 	return 0;
112 }
113 
114 /*
115  * Print one address/symbol entries per line.
116  */
117 static void print_trace_address(void *data, unsigned long addr, int reliable)
118 {
119 	printk("%s", (char *)data);
120 	printk_address(addr, reliable);
121 }
122 
123 static const struct stacktrace_ops print_trace_ops = {
124 	.stack = print_trace_stack,
125 	.address = print_trace_address,
126 };
127 
128 void show_trace(struct task_struct *tsk, unsigned long *sp,
129 		struct pt_regs *regs)
130 {
131 	if (regs && user_mode(regs))
132 		return;
133 
134 	printk("\nCall trace:\n");
135 
136 	unwind_stack(tsk, regs, sp, &print_trace_ops, "");
137 
138 	printk("\n");
139 
140 	if (!tsk)
141 		tsk = current;
142 
143 	debug_show_held_locks(tsk);
144 }
145 
146 void show_stack(struct task_struct *tsk, unsigned long *sp)
147 {
148 	unsigned long stack;
149 
150 	if (!tsk)
151 		tsk = current;
152 	if (tsk == current)
153 		sp = (unsigned long *)current_stack_pointer;
154 	else
155 		sp = (unsigned long *)tsk->thread.sp;
156 
157 	stack = (unsigned long)sp;
158 	dump_mem("Stack: ", stack, THREAD_SIZE +
159 		 (unsigned long)task_stack_page(tsk));
160 	show_trace(tsk, sp, NULL);
161 }
162