Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2008 ARM Limited
4 * Copyright (C) 2014 Regents of the University of California
5 */
6
7#include <linux/export.h>
8#include <linux/kallsyms.h>
9#include <linux/sched.h>
10#include <linux/sched/debug.h>
11#include <linux/sched/task_stack.h>
12#include <linux/stacktrace.h>
13#include <linux/ftrace.h>
14
15register unsigned long sp_in_global __asm__("sp");
16
17#ifdef CONFIG_FRAME_POINTER
18
19struct stackframe {
20 unsigned long fp;
21 unsigned long ra;
22};
23
24void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
25 bool (*fn)(unsigned long, void *), void *arg)
26{
27 unsigned long fp, sp, pc;
28
29 if (regs) {
30 fp = frame_pointer(regs);
31 sp = user_stack_pointer(regs);
32 pc = instruction_pointer(regs);
33 } else if (task == NULL || task == current) {
34 const register unsigned long current_sp = sp_in_global;
35 fp = (unsigned long)__builtin_frame_address(0);
36 sp = current_sp;
37 pc = (unsigned long)walk_stackframe;
38 } else {
39 /* task blocked in __switch_to */
40 fp = task->thread.s[0];
41 sp = task->thread.sp;
42 pc = task->thread.ra;
43 }
44
45 for (;;) {
46 unsigned long low, high;
47 struct stackframe *frame;
48
49 if (unlikely(!__kernel_text_address(pc) || fn(pc, arg)))
50 break;
51
52 /* Validate frame pointer */
53 low = sp + sizeof(struct stackframe);
54 high = ALIGN(sp, THREAD_SIZE);
55 if (unlikely(fp < low || fp > high || fp & 0x7))
56 break;
57 /* Unwind stack frame */
58 frame = (struct stackframe *)fp - 1;
59 sp = fp;
60 fp = frame->fp;
61 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
62 (unsigned long *)(fp - 8));
63 }
64}
65
66#else /* !CONFIG_FRAME_POINTER */
67
68void notrace walk_stackframe(struct task_struct *task,
69 struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
70{
71 unsigned long sp, pc;
72 unsigned long *ksp;
73
74 if (regs) {
75 sp = user_stack_pointer(regs);
76 pc = instruction_pointer(regs);
77 } else if (task == NULL || task == current) {
78 sp = sp_in_global;
79 pc = (unsigned long)walk_stackframe;
80 } else {
81 /* task blocked in __switch_to */
82 sp = task->thread.sp;
83 pc = task->thread.ra;
84 }
85
86 if (unlikely(sp & 0x7))
87 return;
88
89 ksp = (unsigned long *)sp;
90 while (!kstack_end(ksp)) {
91 if (__kernel_text_address(pc) && unlikely(fn(pc, arg)))
92 break;
93 pc = (*ksp++) - 0x4;
94 }
95}
96
97#endif /* CONFIG_FRAME_POINTER */
98
99
100static bool print_trace_address(unsigned long pc, void *arg)
101{
102 const char *loglvl = arg;
103
104 print_ip_sym(loglvl, pc);
105 return false;
106}
107
108void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
109{
110 pr_cont("Call Trace:\n");
111 walk_stackframe(task, NULL, print_trace_address, (void *)loglvl);
112}
113
114static bool save_wchan(unsigned long pc, void *arg)
115{
116 if (!in_sched_functions(pc)) {
117 unsigned long *p = arg;
118 *p = pc;
119 return true;
120 }
121 return false;
122}
123
124unsigned long get_wchan(struct task_struct *task)
125{
126 unsigned long pc = 0;
127
128 if (likely(task && task != current && task->state != TASK_RUNNING))
129 walk_stackframe(task, NULL, save_wchan, &pc);
130 return pc;
131}
132
133
134#ifdef CONFIG_STACKTRACE
135
136static bool __save_trace(unsigned long pc, void *arg, bool nosched)
137{
138 struct stack_trace *trace = arg;
139
140 if (unlikely(nosched && in_sched_functions(pc)))
141 return false;
142 if (unlikely(trace->skip > 0)) {
143 trace->skip--;
144 return false;
145 }
146
147 trace->entries[trace->nr_entries++] = pc;
148 return (trace->nr_entries >= trace->max_entries);
149}
150
151static bool save_trace(unsigned long pc, void *arg)
152{
153 return __save_trace(pc, arg, false);
154}
155
156/*
157 * Save stack-backtrace addresses into a stack_trace buffer.
158 */
159void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
160{
161 walk_stackframe(tsk, NULL, save_trace, trace);
162}
163EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
164
165void save_stack_trace(struct stack_trace *trace)
166{
167 save_stack_trace_tsk(NULL, trace);
168}
169EXPORT_SYMBOL_GPL(save_stack_trace);
170
171#endif /* CONFIG_STACKTRACE */
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2008 ARM Limited
4 * Copyright (C) 2014 Regents of the University of California
5 */
6
7#include <linux/export.h>
8#include <linux/kallsyms.h>
9#include <linux/sched.h>
10#include <linux/sched/debug.h>
11#include <linux/sched/task_stack.h>
12#include <linux/stacktrace.h>
13#include <linux/ftrace.h>
14
15#include <asm/stacktrace.h>
16
17register unsigned long sp_in_global __asm__("sp");
18
19#ifdef CONFIG_FRAME_POINTER
20
21void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
22 bool (*fn)(void *, unsigned long), void *arg)
23{
24 unsigned long fp, sp, pc;
25
26 if (regs) {
27 fp = frame_pointer(regs);
28 sp = user_stack_pointer(regs);
29 pc = instruction_pointer(regs);
30 } else if (task == NULL || task == current) {
31 fp = (unsigned long)__builtin_frame_address(1);
32 sp = (unsigned long)__builtin_frame_address(0);
33 pc = (unsigned long)__builtin_return_address(0);
34 } else {
35 /* task blocked in __switch_to */
36 fp = task->thread.s[0];
37 sp = task->thread.sp;
38 pc = task->thread.ra;
39 }
40
41 for (;;) {
42 unsigned long low, high;
43 struct stackframe *frame;
44
45 if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
46 break;
47
48 /* Validate frame pointer */
49 low = sp + sizeof(struct stackframe);
50 high = ALIGN(sp, THREAD_SIZE);
51 if (unlikely(fp < low || fp > high || fp & 0x7))
52 break;
53 /* Unwind stack frame */
54 frame = (struct stackframe *)fp - 1;
55 sp = fp;
56 if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
57 fp = frame->ra;
58 pc = regs->ra;
59 } else {
60 fp = frame->fp;
61 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
62 (unsigned long *)(fp - 8));
63 }
64
65 }
66}
67
68#else /* !CONFIG_FRAME_POINTER */
69
70void notrace walk_stackframe(struct task_struct *task,
71 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
72{
73 unsigned long sp, pc;
74 unsigned long *ksp;
75
76 if (regs) {
77 sp = user_stack_pointer(regs);
78 pc = instruction_pointer(regs);
79 } else if (task == NULL || task == current) {
80 sp = sp_in_global;
81 pc = (unsigned long)walk_stackframe;
82 } else {
83 /* task blocked in __switch_to */
84 sp = task->thread.sp;
85 pc = task->thread.ra;
86 }
87
88 if (unlikely(sp & 0x7))
89 return;
90
91 ksp = (unsigned long *)sp;
92 while (!kstack_end(ksp)) {
93 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
94 break;
95 pc = (*ksp++) - 0x4;
96 }
97}
98
99#endif /* CONFIG_FRAME_POINTER */
100
101static bool print_trace_address(void *arg, unsigned long pc)
102{
103 const char *loglvl = arg;
104
105 print_ip_sym(loglvl, pc);
106 return true;
107}
108
109noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
110 const char *loglvl)
111{
112 walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
113}
114
115void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
116{
117 pr_cont("%sCall Trace:\n", loglvl);
118 dump_backtrace(NULL, task, loglvl);
119}
120
121static bool save_wchan(void *arg, unsigned long pc)
122{
123 if (!in_sched_functions(pc)) {
124 unsigned long *p = arg;
125 *p = pc;
126 return false;
127 }
128 return true;
129}
130
131unsigned long get_wchan(struct task_struct *task)
132{
133 unsigned long pc = 0;
134
135 if (likely(task && task != current && !task_is_running(task))) {
136 if (!try_get_task_stack(task))
137 return 0;
138 walk_stackframe(task, NULL, save_wchan, &pc);
139 put_task_stack(task);
140 }
141 return pc;
142}
143
144#ifdef CONFIG_STACKTRACE
145
146noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
147 struct task_struct *task, struct pt_regs *regs)
148{
149 walk_stackframe(task, regs, consume_entry, cookie);
150}
151
152#endif /* CONFIG_STACKTRACE */