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
)
46 if (tsk
== NULL
&& regs
== NULL
) {
47 unsigned long fp
, sp
, blink
, ret
;
48 frame_info
->task
= current
;
55 : "=r"(fp
), "=r"(sp
), "=r"(blink
), "=r"(ret
)
58 frame_info
->regs
.r27
= fp
;
59 frame_info
->regs
.r28
= sp
;
60 frame_info
->regs
.r31
= blink
;
61 frame_info
->regs
.r63
= ret
;
62 frame_info
->call_frame
= 0;
63 } else if (regs
== NULL
) {
65 frame_info
->task
= tsk
;
67 frame_info
->regs
.r27
= KSTK_FP(tsk
);
68 frame_info
->regs
.r28
= KSTK_ESP(tsk
);
69 frame_info
->regs
.r31
= KSTK_BLINK(tsk
);
70 frame_info
->regs
.r63
= (unsigned int)__switch_to
;
72 /* In the prologue of __switch_to, first FP is saved on stack
73 * and then SP is copied to FP. Dwarf assumes cfa as FP based
74 * but we didn't save FP. The value retrieved above is FP's
75 * state in previous frame.
76 * As a work around for this, we unwind from __switch_to start
77 * and adjust SP accordingly. The other limitation is that
78 * __switch_to macro is dwarf rules are not generated for inline
81 frame_info
->regs
.r27
= 0;
82 frame_info
->regs
.r28
+= 60;
83 frame_info
->call_frame
= 0;
86 frame_info
->task
= tsk
;
88 frame_info
->regs
.r27
= regs
->fp
;
89 frame_info
->regs
.r28
= regs
->sp
;
90 frame_info
->regs
.r31
= regs
->blink
;
91 frame_info
->regs
.r63
= regs
->ret
;
92 frame_info
->call_frame
= 0;
98 static noinline
unsigned int
99 arc_unwind_core(struct task_struct
*tsk
, struct pt_regs
*regs
,
100 int (*consumer_fn
) (unsigned int, void *), void *arg
)
102 #ifdef CONFIG_ARC_DW2_UNWIND
104 unsigned int address
;
105 struct unwind_frame_info frame_info
;
107 seed_unwind_frame_info(tsk
, regs
, &frame_info
);
110 address
= UNW_PC(&frame_info
);
112 if (address
&& __kernel_text_address(address
)) {
113 if (consumer_fn(address
, arg
) == -1)
117 ret
= arc_unwind(&frame_info
);
120 frame_info
.regs
.r63
= frame_info
.regs
.r31
;
127 return address
; /* return the last address it saw */
129 /* On ARC, only Dward based unwinder works. fp based backtracing is
130 * not possible (-fno-omit-frame-pointer) because of the way function
131 * prelogue is setup (callee regs saved and then fp set and not other
134 pr_warn("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
140 /*-------------------------------------------------------------------------
141 * callbacks called by unwinder iterator to implement kernel APIs
143 * The callback can return -1 to force the iterator to stop, which by default
144 * keeps going till the bottom-most frame.
145 *-------------------------------------------------------------------------
148 /* Call-back which plugs into unwinding core to dump the stack in
149 * case of panic/OOPs/BUG etc
151 static int __print_sym(unsigned int address
, void *unused
)
153 __print_symbol(" %s\n", address
);
157 #ifdef CONFIG_STACKTRACE
159 /* Call-back which plugs into unwinding core to capture the
160 * traces needed by kernel on /proc/<pid>/stack
162 static int __collect_all(unsigned int address
, void *arg
)
164 struct stack_trace
*trace
= arg
;
169 trace
->entries
[trace
->nr_entries
++] = address
;
171 if (trace
->nr_entries
>= trace
->max_entries
)
177 static int __collect_all_but_sched(unsigned int address
, void *arg
)
179 struct stack_trace
*trace
= arg
;
181 if (in_sched_functions(address
))
187 trace
->entries
[trace
->nr_entries
++] = address
;
189 if (trace
->nr_entries
>= trace
->max_entries
)
197 static int __get_first_nonsched(unsigned int address
, void *unused
)
199 if (in_sched_functions(address
))
205 /*-------------------------------------------------------------------------
206 * APIs expected by various kernel sub-systems
207 *-------------------------------------------------------------------------
210 noinline
void show_stacktrace(struct task_struct
*tsk
, struct pt_regs
*regs
)
212 pr_info("\nStack Trace:\n");
213 arc_unwind_core(tsk
, regs
, __print_sym
, NULL
);
215 EXPORT_SYMBOL(show_stacktrace
);
217 /* Expected by sched Code */
218 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
220 show_stacktrace(tsk
, NULL
);
223 /* Another API expected by schedular, shows up in "ps" as Wait Channel
224 * Ofcourse just returning schedule( ) would be pointless so unwind until
225 * the function is not in schedular code
227 unsigned int get_wchan(struct task_struct
*tsk
)
229 return arc_unwind_core(tsk
, NULL
, __get_first_nonsched
, NULL
);
232 #ifdef CONFIG_STACKTRACE
235 * API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.
236 * A typical use is when /proc/<pid>/stack is queried by userland
238 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
240 /* Assumes @tsk is sleeping so unwinds from __switch_to */
241 arc_unwind_core(tsk
, NULL
, __collect_all_but_sched
, trace
);
244 void save_stack_trace(struct stack_trace
*trace
)
246 /* Pass NULL for task so it unwinds the current call frame */
247 arc_unwind_core(NULL
, NULL
, __collect_all
, trace
);
249 EXPORT_SYMBOL_GPL(save_stack_trace
);