Linux Audio

Check our new training course

Loading...
v4.10.11
  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/export.h>
  9#include <linux/uaccess.h>
 10#include <asm/stacktrace.h>
 11#include <asm/unwind.h>
 12
 13static int save_stack_address(struct stack_trace *trace, unsigned long addr,
 14			      bool nosched)
 15{
 
 
 
 
 
 
 
 
 
 
 
 16	if (nosched && in_sched_functions(addr))
 17		return 0;
 18
 19	if (trace->skip > 0) {
 20		trace->skip--;
 21		return 0;
 22	}
 23
 24	if (trace->nr_entries >= trace->max_entries)
 25		return -1;
 26
 27	trace->entries[trace->nr_entries++] = addr;
 28	return 0;
 29}
 30
 31static void __save_stack_trace(struct stack_trace *trace,
 32			       struct task_struct *task, struct pt_regs *regs,
 33			       bool nosched)
 34{
 35	struct unwind_state state;
 36	unsigned long addr;
 37
 38	if (regs)
 39		save_stack_address(trace, regs->ip, nosched);
 40
 41	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
 42	     unwind_next_frame(&state)) {
 43		addr = unwind_get_return_address(&state);
 44		if (!addr || save_stack_address(trace, addr, nosched))
 45			break;
 46	}
 47
 48	if (trace->nr_entries < trace->max_entries)
 49		trace->entries[trace->nr_entries++] = ULONG_MAX;
 
 
 50}
 51
 
 
 
 
 
 
 
 
 
 
 
 
 52/*
 53 * Save stack-backtrace addresses into a stack_trace buffer.
 54 */
 55void save_stack_trace(struct stack_trace *trace)
 56{
 57	__save_stack_trace(trace, current, NULL, false);
 
 
 58}
 59EXPORT_SYMBOL_GPL(save_stack_trace);
 60
 61void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
 62{
 63	__save_stack_trace(trace, current, regs, false);
 
 
 64}
 65
 66void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
 67{
 68	if (!try_get_task_stack(tsk))
 69		return;
 70
 71	__save_stack_trace(trace, tsk, NULL, true);
 72
 73	put_task_stack(tsk);
 74}
 75EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
 76
 77/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
 78
 79struct stack_frame_user {
 80	const void __user	*next_fp;
 81	unsigned long		ret_addr;
 82};
 83
 84static int
 85copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
 86{
 87	int ret;
 88
 89	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
 90		return 0;
 91
 92	ret = 1;
 93	pagefault_disable();
 94	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
 95		ret = 0;
 96	pagefault_enable();
 97
 98	return ret;
 99}
100
101static inline void __save_stack_trace_user(struct stack_trace *trace)
102{
103	const struct pt_regs *regs = task_pt_regs(current);
104	const void __user *fp = (const void __user *)regs->bp;
105
106	if (trace->nr_entries < trace->max_entries)
107		trace->entries[trace->nr_entries++] = regs->ip;
108
109	while (trace->nr_entries < trace->max_entries) {
110		struct stack_frame_user frame;
111
112		frame.next_fp = NULL;
113		frame.ret_addr = 0;
114		if (!copy_stack_frame(fp, &frame))
115			break;
116		if ((unsigned long)fp < regs->sp)
117			break;
118		if (frame.ret_addr) {
119			trace->entries[trace->nr_entries++] =
120				frame.ret_addr;
121		}
122		if (fp == frame.next_fp)
123			break;
124		fp = frame.next_fp;
125	}
126}
127
128void save_stack_trace_user(struct stack_trace *trace)
129{
130	/*
131	 * Trace user stack if we are not a kernel thread
132	 */
133	if (current->mm) {
134		__save_stack_trace_user(trace);
135	}
136	if (trace->nr_entries < trace->max_entries)
137		trace->entries[trace->nr_entries++] = ULONG_MAX;
138}
139
v4.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 int
 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 0;
 24#endif
 25	if (nosched && in_sched_functions(addr))
 26		return 0;
 
 27	if (trace->skip > 0) {
 28		trace->skip--;
 29		return 0;
 30	}
 31	if (trace->nr_entries < trace->max_entries) {
 32		trace->entries[trace->nr_entries++] = addr;
 33		return 0;
 34	} else {
 35		return -1; /* no more room, stop walking the stack */
 36	}
 37}
 38
 39static int save_stack_address(void *data, unsigned long addr, int reliable)
 40{
 41	return __save_stack_address(data, addr, reliable, false);
 42}
 
 
 
 
 
 
 
 
 
 
 
 
 43
 44static int
 45save_stack_address_nosched(void *data, unsigned long addr, int reliable)
 46{
 47	return __save_stack_address(data, addr, reliable, true);
 48}
 49
 50static const struct stacktrace_ops save_stack_ops = {
 51	.stack		= save_stack_stack,
 52	.address	= save_stack_address,
 53	.walk_stack	= print_context_stack,
 54};
 55
 56static const struct stacktrace_ops save_stack_ops_nosched = {
 57	.stack		= save_stack_stack,
 58	.address	= save_stack_address_nosched,
 59	.walk_stack	= print_context_stack,
 60};
 61
 62/*
 63 * Save stack-backtrace addresses into a stack_trace buffer.
 64 */
 65void save_stack_trace(struct stack_trace *trace)
 66{
 67	dump_trace(current, NULL, NULL, 0, &save_stack_ops, trace);
 68	if (trace->nr_entries < trace->max_entries)
 69		trace->entries[trace->nr_entries++] = ULONG_MAX;
 70}
 71EXPORT_SYMBOL_GPL(save_stack_trace);
 72
 73void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
 74{
 75	dump_trace(current, regs, NULL, 0, &save_stack_ops, trace);
 76	if (trace->nr_entries < trace->max_entries)
 77		trace->entries[trace->nr_entries++] = ULONG_MAX;
 78}
 79
 80void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
 81{
 82	dump_trace(tsk, NULL, NULL, 0, &save_stack_ops_nosched, trace);
 83	if (trace->nr_entries < trace->max_entries)
 84		trace->entries[trace->nr_entries++] = ULONG_MAX;
 
 
 
 85}
 86EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
 87
 88/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
 89
 90struct stack_frame_user {
 91	const void __user	*next_fp;
 92	unsigned long		ret_addr;
 93};
 94
 95static int
 96copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
 97{
 98	int ret;
 99
100	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
101		return 0;
102
103	ret = 1;
104	pagefault_disable();
105	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
106		ret = 0;
107	pagefault_enable();
108
109	return ret;
110}
111
112static inline void __save_stack_trace_user(struct stack_trace *trace)
113{
114	const struct pt_regs *regs = task_pt_regs(current);
115	const void __user *fp = (const void __user *)regs->bp;
116
117	if (trace->nr_entries < trace->max_entries)
118		trace->entries[trace->nr_entries++] = regs->ip;
119
120	while (trace->nr_entries < trace->max_entries) {
121		struct stack_frame_user frame;
122
123		frame.next_fp = NULL;
124		frame.ret_addr = 0;
125		if (!copy_stack_frame(fp, &frame))
126			break;
127		if ((unsigned long)fp < regs->sp)
128			break;
129		if (frame.ret_addr) {
130			trace->entries[trace->nr_entries++] =
131				frame.ret_addr;
132		}
133		if (fp == frame.next_fp)
134			break;
135		fp = frame.next_fp;
136	}
137}
138
139void save_stack_trace_user(struct stack_trace *trace)
140{
141	/*
142	 * Trace user stack if we are not a kernel thread
143	 */
144	if (current->mm) {
145		__save_stack_trace_user(trace);
146	}
147	if (trace->nr_entries < trace->max_entries)
148		trace->entries[trace->nr_entries++] = ULONG_MAX;
149}
150