Currently, the log-level of show_stack() depends on a platform realization. It creates situations where the headers are printed with lower log level or higher than the stacktrace (depending on a platform or user). Furthermore, it forces the logic decision from user to an architecture side. In result, some users as sysrq/kdb/etc are doing tricks with temporary rising console_loglevel while printing their messages. And in result it not only may print unwanted messages from other CPUs, but also omit printing at all in the unlucky case where the printk() was deferred. Introducing log-level parameter and KERN_UNSUPPRESSED [1] seems an easier approach than introducing more printk buffers. Also, it will consolidate printings with headers. Add log level parameter to show_trace() as a preparation to introduce show_stack_loglvl(). [1]: https://lore.kernel.org/lkml/20190528002412.1625-1-dima@arista.com/T/#u Signed-off-by: Dmitry Safonov <dima@arista.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Rich Felker <dalias@libc.org> Link: http://lkml.kernel.org/r/20200418201944.482088-33-dima@arista.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
163 lines
3.5 KiB
C
163 lines
3.5 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
* Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
|
|
* Copyright (C) 2009 Matt Fleming
|
|
* Copyright (C) 2002 - 2012 Paul Mundt
|
|
*/
|
|
#include <linux/kallsyms.h>
|
|
#include <linux/ftrace.h>
|
|
#include <linux/debug_locks.h>
|
|
#include <linux/sched/debug.h>
|
|
#include <linux/sched/task_stack.h>
|
|
#include <linux/kdebug.h>
|
|
#include <linux/export.h>
|
|
#include <linux/uaccess.h>
|
|
#include <asm/unwinder.h>
|
|
#include <asm/stacktrace.h>
|
|
|
|
void dump_mem(const char *str, const char *loglvl,
|
|
unsigned long bottom, unsigned long top)
|
|
{
|
|
unsigned long p;
|
|
int i;
|
|
|
|
printk("%s%s(0x%08lx to 0x%08lx)\n", loglvl, str, bottom, top);
|
|
|
|
for (p = bottom & ~31; p < top; ) {
|
|
printk("%s%04lx: ", loglvl, p & 0xffff);
|
|
|
|
for (i = 0; i < 8; i++, p += 4) {
|
|
unsigned int val;
|
|
|
|
if (p < bottom || p >= top)
|
|
printk("%s ", loglvl);
|
|
else {
|
|
if (__get_user(val, (unsigned int __user *)p)) {
|
|
printk("%s\n", loglvl);
|
|
return;
|
|
}
|
|
printk("%s%08x ", loglvl, val);
|
|
}
|
|
}
|
|
printk("%s\n", loglvl);
|
|
}
|
|
}
|
|
|
|
void printk_address(unsigned long address, int reliable, const char *loglvl)
|
|
{
|
|
printk("%s [<%p>] %s%pS\n", loglvl, (void *) address,
|
|
reliable ? "" : "? ", (void *) address);
|
|
}
|
|
|
|
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
|
|
static void
|
|
print_ftrace_graph_addr(unsigned long addr, void *data,
|
|
const struct stacktrace_ops *ops,
|
|
struct thread_info *tinfo, int *graph)
|
|
{
|
|
struct task_struct *task = tinfo->task;
|
|
struct ftrace_ret_stack *ret_stack;
|
|
unsigned long ret_addr;
|
|
|
|
if (addr != (unsigned long)return_to_handler)
|
|
return;
|
|
|
|
if (!task->ret_stack)
|
|
return;
|
|
|
|
ret_stack = ftrace_graph_get_ret_stack(task, *graph);
|
|
if (!ret_stack)
|
|
return;
|
|
|
|
ret_addr = ret_stack->ret;
|
|
|
|
ops->address(data, ret_addr, 1);
|
|
|
|
(*graph)++;
|
|
}
|
|
#else
|
|
static inline void
|
|
print_ftrace_graph_addr(unsigned long addr, void *data,
|
|
const struct stacktrace_ops *ops,
|
|
struct thread_info *tinfo, int *graph)
|
|
{ }
|
|
#endif
|
|
|
|
void
|
|
stack_reader_dump(struct task_struct *task, struct pt_regs *regs,
|
|
unsigned long *sp, const struct stacktrace_ops *ops,
|
|
void *data)
|
|
{
|
|
struct thread_info *context;
|
|
int graph = 0;
|
|
|
|
context = (struct thread_info *)
|
|
((unsigned long)sp & (~(THREAD_SIZE - 1)));
|
|
|
|
while (!kstack_end(sp)) {
|
|
unsigned long addr = *sp++;
|
|
|
|
if (__kernel_text_address(addr)) {
|
|
ops->address(data, addr, 1);
|
|
|
|
print_ftrace_graph_addr(addr, data, ops,
|
|
context, &graph);
|
|
}
|
|
}
|
|
}
|
|
|
|
static int print_trace_stack(void *data, char *name)
|
|
{
|
|
printk("%s <%s> ", (char *)data, name);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Print one address/symbol entries per line.
|
|
*/
|
|
static void print_trace_address(void *data, unsigned long addr, int reliable)
|
|
{
|
|
printk_address(addr, reliable, (char *)data);
|
|
}
|
|
|
|
static const struct stacktrace_ops print_trace_ops = {
|
|
.stack = print_trace_stack,
|
|
.address = print_trace_address,
|
|
};
|
|
|
|
void show_trace(struct task_struct *tsk, unsigned long *sp,
|
|
struct pt_regs *regs, const char *loglvl)
|
|
{
|
|
if (regs && user_mode(regs))
|
|
return;
|
|
|
|
printk("%s\nCall trace:\n", loglvl);
|
|
|
|
unwind_stack(tsk, regs, sp, &print_trace_ops, (void *)loglvl);
|
|
|
|
printk("%s\n", loglvl);
|
|
|
|
if (!tsk)
|
|
tsk = current;
|
|
|
|
debug_show_held_locks(tsk);
|
|
}
|
|
|
|
void show_stack(struct task_struct *tsk, unsigned long *sp)
|
|
{
|
|
unsigned long stack;
|
|
|
|
if (!tsk)
|
|
tsk = current;
|
|
if (tsk == current)
|
|
sp = (unsigned long *)current_stack_pointer;
|
|
else
|
|
sp = (unsigned long *)tsk->thread.sp;
|
|
|
|
stack = (unsigned long)sp;
|
|
dump_mem("Stack: ", KERN_DEFAULT, stack, THREAD_SIZE +
|
|
(unsigned long)task_stack_page(tsk));
|
|
show_trace(tsk, sp, NULL, KERN_DEFAULT);
|
|
}
|