2 * stacktrace.c : stacktracing APIs needed by rest of kernel
3 * (wrappers over ARC dwarf based unwinder)
5 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 * -Implemented CONFIG_STACKTRACE APIs, primarily save_stack_trace_tsk( )
13 * for displaying task's kernel mode call stack in /proc/<pid>/stack
14 * -Iterator based approach to have single copy of unwinding core and APIs
15 * needing unwinding, implement the logic in iterator regarding:
16 * = which frame onwards to start capture
17 * = which frame to stop capturing (wchan)
18 * = specifics of data structs where trace is saved(CONFIG_STACKTRACE etc)
21 * -Implemented correct versions of thread_saved_pc() and get_wchan()
24 * -Initial implementation
27 #include <linux/ptrace.h>
28 #include <linux/export.h>
29 #include <linux/stacktrace.h>
30 #include <linux/kallsyms.h>
31 #include <asm/arcregs.h>
32 #include <asm/unwind.h>
33 #include <asm/switch_to.h>
35 /*-------------------------------------------------------------------------
37 *-------------------------------------------------------------------------
40 #ifdef CONFIG_ARC_DW2_UNWIND
42 static void seed_unwind_frame_info(struct task_struct
*tsk
,
44 struct unwind_frame_info
*frame_info
)
47 * synchronous unwinding (e.g. dump_stack)
48 * - uses current values of SP and friends
50 if (tsk
== NULL
&& regs
== NULL
) {
51 unsigned long fp
, sp
, blink
, ret
;
52 frame_info
->task
= current
;
59 : "=r"(fp
), "=r"(sp
), "=r"(blink
), "=r"(ret
)
62 frame_info
->regs
.r27
= fp
;
63 frame_info
->regs
.r28
= sp
;
64 frame_info
->regs
.r31
= blink
;
65 frame_info
->regs
.r63
= ret
;
66 frame_info
->call_frame
= 0;
67 } else if (regs
== NULL
) {
69 * Asynchronous unwinding of sleeping task
70 * - Gets SP etc from task's pt_regs (saved bottom of kernel
74 frame_info
->task
= tsk
;
76 frame_info
->regs
.r27
= TSK_K_FP(tsk
);
77 frame_info
->regs
.r28
= TSK_K_ESP(tsk
);
78 frame_info
->regs
.r31
= TSK_K_BLINK(tsk
);
79 frame_info
->regs
.r63
= (unsigned int)__switch_to
;
81 /* In the prologue of __switch_to, first FP is saved on stack
82 * and then SP is copied to FP. Dwarf assumes cfa as FP based
83 * but we didn't save FP. The value retrieved above is FP's
84 * state in previous frame.
85 * As a work around for this, we unwind from __switch_to start
86 * and adjust SP accordingly. The other limitation is that
87 * __switch_to macro is dwarf rules are not generated for inline
90 frame_info
->regs
.r27
= 0;
91 frame_info
->regs
.r28
+= 60;
92 frame_info
->call_frame
= 0;
96 * Asynchronous unwinding of intr/exception
97 * - Just uses the pt_regs passed
99 frame_info
->task
= tsk
;
101 frame_info
->regs
.r27
= regs
->fp
;
102 frame_info
->regs
.r28
= regs
->sp
;
103 frame_info
->regs
.r31
= regs
->blink
;
104 frame_info
->regs
.r63
= regs
->ret
;
105 frame_info
->call_frame
= 0;
111 notrace noinline
unsigned int
112 arc_unwind_core(struct task_struct
*tsk
, struct pt_regs
*regs
,
113 int (*consumer_fn
) (unsigned int, void *), void *arg
)
115 #ifdef CONFIG_ARC_DW2_UNWIND
117 unsigned int address
;
118 struct unwind_frame_info frame_info
;
120 seed_unwind_frame_info(tsk
, regs
, &frame_info
);
123 address
= UNW_PC(&frame_info
);
125 if (!address
|| !__kernel_text_address(address
))
128 if (consumer_fn(address
, arg
) == -1)
131 ret
= arc_unwind(&frame_info
);
135 frame_info
.regs
.r63
= frame_info
.regs
.r31
;
138 return address
; /* return the last address it saw */
140 /* On ARC, only Dward based unwinder works. fp based backtracing is
141 * not possible (-fno-omit-frame-pointer) because of the way function
142 * prelogue is setup (callee regs saved and then fp set and not other
145 pr_warn("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
151 /*-------------------------------------------------------------------------
152 * callbacks called by unwinder iterator to implement kernel APIs
154 * The callback can return -1 to force the iterator to stop, which by default
155 * keeps going till the bottom-most frame.
156 *-------------------------------------------------------------------------
159 /* Call-back which plugs into unwinding core to dump the stack in
160 * case of panic/OOPs/BUG etc
162 static int __print_sym(unsigned int address
, void *unused
)
164 __print_symbol(" %s\n", address
);
168 #ifdef CONFIG_STACKTRACE
170 /* Call-back which plugs into unwinding core to capture the
171 * traces needed by kernel on /proc/<pid>/stack
173 static int __collect_all(unsigned int address
, void *arg
)
175 struct stack_trace
*trace
= arg
;
180 trace
->entries
[trace
->nr_entries
++] = address
;
182 if (trace
->nr_entries
>= trace
->max_entries
)
188 static int __collect_all_but_sched(unsigned int address
, void *arg
)
190 struct stack_trace
*trace
= arg
;
192 if (in_sched_functions(address
))
198 trace
->entries
[trace
->nr_entries
++] = address
;
200 if (trace
->nr_entries
>= trace
->max_entries
)
208 static int __get_first_nonsched(unsigned int address
, void *unused
)
210 if (in_sched_functions(address
))
216 /*-------------------------------------------------------------------------
217 * APIs expected by various kernel sub-systems
218 *-------------------------------------------------------------------------
221 noinline
void show_stacktrace(struct task_struct
*tsk
, struct pt_regs
*regs
)
223 pr_info("\nStack Trace:\n");
224 arc_unwind_core(tsk
, regs
, __print_sym
, NULL
);
226 EXPORT_SYMBOL(show_stacktrace
);
228 /* Expected by sched Code */
229 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
231 show_stacktrace(tsk
, NULL
);
234 /* Another API expected by schedular, shows up in "ps" as Wait Channel
235 * Ofcourse just returning schedule( ) would be pointless so unwind until
236 * the function is not in schedular code
238 unsigned int get_wchan(struct task_struct
*tsk
)
240 return arc_unwind_core(tsk
, NULL
, __get_first_nonsched
, NULL
);
243 #ifdef CONFIG_STACKTRACE
246 * API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.
247 * A typical use is when /proc/<pid>/stack is queried by userland
249 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
251 /* Assumes @tsk is sleeping so unwinds from __switch_to */
252 arc_unwind_core(tsk
, NULL
, __collect_all_but_sched
, trace
);
255 void save_stack_trace(struct stack_trace
*trace
)
257 /* Pass NULL for task so it unwinds the current call frame */
258 arc_unwind_core(NULL
, NULL
, __collect_all
, trace
);
260 EXPORT_SYMBOL_GPL(save_stack_trace
);