Linux Audio

Check our new training course

Loading...
v3.5.6
  1/*
  2 * Stack trace management functions
  3 *
  4 *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5 */
  6#include <linux/sched.h>
 
 
  7#include <linux/stacktrace.h>
  8#include <linux/module.h>
  9#include <linux/uaccess.h>
 10#include <asm/stacktrace.h>
 
 11
 12static int save_stack_stack(void *data, char *name)
 
 13{
 14	return 0;
 15}
 16
 17static void
 18__save_stack_address(void *data, unsigned long addr, bool reliable, bool nosched)
 19{
 20	struct stack_trace *trace = data;
 21#ifdef CONFIG_FRAME_POINTER
 22	if (!reliable)
 23		return;
 24#endif
 25	if (nosched && in_sched_functions(addr))
 26		return;
 27	if (trace->skip > 0) {
 28		trace->skip--;
 29		return;
 
 
 
 
 
 
 30	}
 31	if (trace->nr_entries < trace->max_entries)
 32		trace->entries[trace->nr_entries++] = addr;
 33}
 34
 35static void save_stack_address(void *data, unsigned long addr, int reliable)
 
 
 
 
 
 
 
 36{
 37	return __save_stack_address(data, addr, reliable, false);
 38}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 39
 40static void
 41save_stack_address_nosched(void *data, unsigned long addr, int reliable)
 42{
 43	return __save_stack_address(data, addr, reliable, true);
 44}
 45
 46static const struct stacktrace_ops save_stack_ops = {
 47	.stack		= save_stack_stack,
 48	.address	= save_stack_address,
 49	.walk_stack	= print_context_stack,
 50};
 51
 52static const struct stacktrace_ops save_stack_ops_nosched = {
 53	.stack		= save_stack_stack,
 54	.address	= save_stack_address_nosched,
 55	.walk_stack	= print_context_stack,
 56};
 
 
 57
 58/*
 59 * Save stack-backtrace addresses into a stack_trace buffer.
 60 */
 61void save_stack_trace(struct stack_trace *trace)
 62{
 63	dump_trace(current, NULL, NULL, 0, &save_stack_ops, trace);
 64	if (trace->nr_entries < trace->max_entries)
 65		trace->entries[trace->nr_entries++] = ULONG_MAX;
 66}
 67EXPORT_SYMBOL_GPL(save_stack_trace);
 68
 69void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
 70{
 71	dump_trace(current, regs, NULL, 0, &save_stack_ops, trace);
 72	if (trace->nr_entries < trace->max_entries)
 73		trace->entries[trace->nr_entries++] = ULONG_MAX;
 74}
 
 75
 76void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
 77{
 78	dump_trace(tsk, NULL, NULL, 0, &save_stack_ops_nosched, trace);
 79	if (trace->nr_entries < trace->max_entries)
 80		trace->entries[trace->nr_entries++] = ULONG_MAX;
 81}
 82EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
 83
 84/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
 85
 86struct stack_frame_user {
 87	const void __user	*next_fp;
 88	unsigned long		ret_addr;
 89};
 90
 91static int
 92copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
 93{
 94	int ret;
 95
 96	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
 97		return 0;
 98
 99	ret = 1;
100	pagefault_disable();
101	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
102		ret = 0;
103	pagefault_enable();
104
105	return ret;
106}
107
108static inline void __save_stack_trace_user(struct stack_trace *trace)
 
109{
110	const struct pt_regs *regs = task_pt_regs(current);
111	const void __user *fp = (const void __user *)regs->bp;
112
113	if (trace->nr_entries < trace->max_entries)
114		trace->entries[trace->nr_entries++] = regs->ip;
115
116	while (trace->nr_entries < trace->max_entries) {
117		struct stack_frame_user frame;
118
119		frame.next_fp = NULL;
120		frame.ret_addr = 0;
121		if (!copy_stack_frame(fp, &frame))
122			break;
123		if ((unsigned long)fp < regs->sp)
124			break;
125		if (frame.ret_addr) {
126			trace->entries[trace->nr_entries++] =
127				frame.ret_addr;
128		}
129		if (fp == frame.next_fp)
130			break;
131		fp = frame.next_fp;
132	}
133}
134
135void save_stack_trace_user(struct stack_trace *trace)
136{
137	/*
138	 * Trace user stack if we are not a kernel thread
139	 */
140	if (current->mm) {
141		__save_stack_trace_user(trace);
142	}
143	if (trace->nr_entries < trace->max_entries)
144		trace->entries[trace->nr_entries++] = ULONG_MAX;
145}
146
v5.4
  1/*
  2 * Stack trace management functions
  3 *
  4 *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5 */
  6#include <linux/sched.h>
  7#include <linux/sched/debug.h>
  8#include <linux/sched/task_stack.h>
  9#include <linux/stacktrace.h>
 10#include <linux/export.h>
 11#include <linux/uaccess.h>
 12#include <asm/stacktrace.h>
 13#include <asm/unwind.h>
 14
 15void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
 16		     struct task_struct *task, struct pt_regs *regs)
 17{
 18	struct unwind_state state;
 19	unsigned long addr;
 20
 21	if (regs && !consume_entry(cookie, regs->ip, false))
 
 
 
 
 
 
 
 
 
 
 
 22		return;
 23
 24	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
 25	     unwind_next_frame(&state)) {
 26		addr = unwind_get_return_address(&state);
 27		if (!addr || !consume_entry(cookie, addr, false))
 28			break;
 29	}
 
 
 30}
 31
 32/*
 33 * This function returns an error if it detects any unreliable features of the
 34 * stack.  Otherwise it guarantees that the stack trace is reliable.
 35 *
 36 * If the task is not 'current', the caller *must* ensure the task is inactive.
 37 */
 38int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
 39			     void *cookie, struct task_struct *task)
 40{
 41	struct unwind_state state;
 42	struct pt_regs *regs;
 43	unsigned long addr;
 44
 45	for (unwind_start(&state, task, NULL, NULL);
 46	     !unwind_done(&state) && !unwind_error(&state);
 47	     unwind_next_frame(&state)) {
 48
 49		regs = unwind_get_entry_regs(&state, NULL);
 50		if (regs) {
 51			/* Success path for user tasks */
 52			if (user_mode(regs))
 53				return 0;
 54
 55			/*
 56			 * Kernel mode registers on the stack indicate an
 57			 * in-kernel interrupt or exception (e.g., preemption
 58			 * or a page fault), which can make frame pointers
 59			 * unreliable.
 60			 */
 61
 62			if (IS_ENABLED(CONFIG_FRAME_POINTER))
 63				return -EINVAL;
 64		}
 
 
 65
 66		addr = unwind_get_return_address(&state);
 
 
 
 
 67
 68		/*
 69		 * A NULL or invalid return address probably means there's some
 70		 * generated code which __kernel_text_address() doesn't know
 71		 * about.
 72		 */
 73		if (!addr)
 74			return -EINVAL;
 75
 76		if (!consume_entry(cookie, addr, false))
 77			return -EINVAL;
 78	}
 
 
 
 
 
 
 
 79
 80	/* Check for stack corruption */
 81	if (unwind_error(&state))
 82		return -EINVAL;
 83
 84	/* Success path for non-user tasks, i.e. kthreads and idle tasks */
 85	if (!(task->flags & (PF_KTHREAD | PF_IDLE)))
 86		return -EINVAL;
 87
 88	return 0;
 
 
 
 
 89}
 
 90
 91/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
 92
 93struct stack_frame_user {
 94	const void __user	*next_fp;
 95	unsigned long		ret_addr;
 96};
 97
 98static int
 99copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
100{
101	int ret;
102
103	if (__range_not_ok(fp, sizeof(*frame), TASK_SIZE))
104		return 0;
105
106	ret = 1;
107	pagefault_disable();
108	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
109		ret = 0;
110	pagefault_enable();
111
112	return ret;
113}
114
115void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
116			  const struct pt_regs *regs)
117{
 
118	const void __user *fp = (const void __user *)regs->bp;
119
120	if (!consume_entry(cookie, regs->ip, false))
121		return;
122
123	while (1) {
124		struct stack_frame_user frame;
125
126		frame.next_fp = NULL;
127		frame.ret_addr = 0;
128		if (!copy_stack_frame(fp, &frame))
129			break;
130		if ((unsigned long)fp < regs->sp)
131			break;
132		if (!frame.ret_addr)
133			break;
134		if (!consume_entry(cookie, frame.ret_addr, false))
 
 
135			break;
136		fp = frame.next_fp;
137	}
 
 
 
 
 
 
 
 
 
 
 
 
138}
139