2008-04-17 11:35:00 +07:00
|
|
|
/*
|
|
|
|
* Stack trace utility
|
|
|
|
*
|
|
|
|
* Copyright 2008 Christoph Hellwig, IBM Corp.
|
2018-05-04 19:38:34 +07:00
|
|
|
* Copyright 2018 SUSE Linux GmbH
|
2008-04-17 11:35:00 +07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
2011-07-23 05:24:23 +07:00
|
|
|
#include <linux/export.h>
|
2018-05-04 19:38:34 +07:00
|
|
|
#include <linux/kallsyms.h>
|
|
|
|
#include <linux/module.h>
|
2008-04-17 11:35:00 +07:00
|
|
|
#include <linux/sched.h>
|
2017-02-09 00:51:35 +07:00
|
|
|
#include <linux/sched/debug.h>
|
2018-05-04 19:38:34 +07:00
|
|
|
#include <linux/sched/task_stack.h>
|
2008-04-17 11:35:00 +07:00
|
|
|
#include <linux/stacktrace.h>
|
|
|
|
#include <asm/ptrace.h>
|
2008-07-10 21:08:18 +07:00
|
|
|
#include <asm/processor.h>
|
2018-05-04 19:38:34 +07:00
|
|
|
#include <linux/ftrace.h>
|
|
|
|
#include <asm/kprobes.h>
|
2008-04-17 11:35:00 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save stack-backtrace addresses into a stack_trace buffer.
|
|
|
|
*/
|
2008-07-10 21:08:18 +07:00
|
|
|
static void save_context_stack(struct stack_trace *trace, unsigned long sp,
|
|
|
|
struct task_struct *tsk, int savesched)
|
2008-04-17 11:35:00 +07:00
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
unsigned long *stack = (unsigned long *) sp;
|
|
|
|
unsigned long newsp, ip;
|
|
|
|
|
2008-07-10 21:08:18 +07:00
|
|
|
if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
|
2008-04-17 11:35:00 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
newsp = stack[0];
|
|
|
|
ip = stack[STACK_FRAME_LR_SAVE];
|
|
|
|
|
2008-07-10 21:08:18 +07:00
|
|
|
if (savesched || !in_sched_functions(ip)) {
|
|
|
|
if (!trace->skip)
|
|
|
|
trace->entries[trace->nr_entries++] = ip;
|
|
|
|
else
|
|
|
|
trace->skip--;
|
|
|
|
}
|
2008-04-17 11:35:00 +07:00
|
|
|
|
|
|
|
if (trace->nr_entries >= trace->max_entries)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sp = newsp;
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 21:08:18 +07:00
|
|
|
|
|
|
|
void save_stack_trace(struct stack_trace *trace)
|
|
|
|
{
|
|
|
|
unsigned long sp;
|
|
|
|
|
2014-10-13 15:41:39 +07:00
|
|
|
sp = current_stack_pointer();
|
2008-07-10 21:08:18 +07:00
|
|
|
|
|
|
|
save_context_stack(trace, sp, current, 1);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(save_stack_trace);
|
|
|
|
|
|
|
|
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
|
|
|
|
{
|
2017-03-28 02:32:33 +07:00
|
|
|
unsigned long sp;
|
|
|
|
|
|
|
|
if (tsk == current)
|
|
|
|
sp = current_stack_pointer();
|
|
|
|
else
|
|
|
|
sp = tsk->thread.ksp;
|
|
|
|
|
|
|
|
save_context_stack(trace, sp, tsk, 0);
|
2008-07-10 21:08:18 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
|
2015-12-09 01:50:56 +07:00
|
|
|
|
|
|
|
void
|
|
|
|
save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
|
|
|
|
{
|
|
|
|
save_context_stack(trace, regs->gpr[1], current, 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(save_stack_trace_regs);
|
2018-05-04 19:38:34 +07:00
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
|
|
|
|
int
|
|
|
|
save_stack_trace_tsk_reliable(struct task_struct *tsk,
|
|
|
|
struct stack_trace *trace)
|
|
|
|
{
|
|
|
|
unsigned long sp;
|
|
|
|
unsigned long stack_page = (unsigned long)task_stack_page(tsk);
|
|
|
|
unsigned long stack_end;
|
|
|
|
int graph_idx = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The last frame (unwinding first) may not yet have saved
|
|
|
|
* its LR onto the stack.
|
|
|
|
*/
|
|
|
|
int firstframe = 1;
|
|
|
|
|
|
|
|
if (tsk == current)
|
|
|
|
sp = current_stack_pointer();
|
|
|
|
else
|
|
|
|
sp = tsk->thread.ksp;
|
|
|
|
|
|
|
|
stack_end = stack_page + THREAD_SIZE;
|
|
|
|
if (!is_idle_task(tsk)) {
|
|
|
|
/*
|
|
|
|
* For user tasks, this is the SP value loaded on
|
|
|
|
* kernel entry, see "PACAKSAVE(r13)" in _switch() and
|
|
|
|
* system_call_common()/EXCEPTION_PROLOG_COMMON().
|
|
|
|
*
|
|
|
|
* Likewise for non-swapper kernel threads,
|
|
|
|
* this also happens to be the top of the stack
|
|
|
|
* as setup by copy_thread().
|
|
|
|
*
|
|
|
|
* Note that stack backlinks are not properly setup by
|
|
|
|
* copy_thread() and thus, a forked task() will have
|
|
|
|
* an unreliable stack trace until it's been
|
|
|
|
* _switch()'ed to for the first time.
|
|
|
|
*/
|
|
|
|
stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* idle tasks have a custom stack layout,
|
|
|
|
* c.f. cpu_idle_thread_init().
|
|
|
|
*/
|
|
|
|
stack_end -= STACK_FRAME_OVERHEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sp < stack_page + sizeof(struct thread_struct) ||
|
|
|
|
sp > stack_end - STACK_FRAME_MIN_SIZE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
unsigned long *stack = (unsigned long *) sp;
|
|
|
|
unsigned long newsp, ip;
|
|
|
|
|
|
|
|
/* sanity check: ABI requires SP to be aligned 16 bytes. */
|
|
|
|
if (sp & 0xF)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Mark stacktraces with exception frames as unreliable. */
|
|
|
|
if (sp <= stack_end - STACK_INT_FRAME_SIZE &&
|
|
|
|
stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
newsp = stack[0];
|
|
|
|
/* Stack grows downwards; unwinder may only go up. */
|
|
|
|
if (newsp <= sp)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (newsp != stack_end &&
|
|
|
|
newsp > stack_end - STACK_FRAME_MIN_SIZE) {
|
|
|
|
return 1; /* invalid backlink, too far up. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Examine the saved LR: it must point into kernel code. */
|
|
|
|
ip = stack[STACK_FRAME_LR_SAVE];
|
|
|
|
if (!firstframe && !__kernel_text_address(ip))
|
|
|
|
return 1;
|
|
|
|
firstframe = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: IMHO these tests do not belong in
|
|
|
|
* arch-dependent code, they are generic.
|
|
|
|
*/
|
|
|
|
ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark stacktraces with kretprobed functions on them
|
|
|
|
* as unreliable.
|
|
|
|
*/
|
|
|
|
if (ip == (unsigned long)kretprobe_trampoline)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!trace->skip)
|
|
|
|
trace->entries[trace->nr_entries++] = ip;
|
|
|
|
else
|
|
|
|
trace->skip--;
|
|
|
|
|
|
|
|
if (newsp == stack_end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (trace->nr_entries >= trace->max_entries)
|
|
|
|
return -E2BIG;
|
|
|
|
|
|
|
|
sp = newsp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(save_stack_trace_tsk_reliable);
|
|
|
|
#endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
|