Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 *  Copyright (C) 1991, 1992  Linus Torvalds
  3 *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4 */
  5#include <linux/kallsyms.h>
  6#include <linux/kprobes.h>
  7#include <linux/uaccess.h>
  8#include <linux/utsname.h>
  9#include <linux/hardirq.h>
 10#include <linux/kdebug.h>
 11#include <linux/module.h>
 12#include <linux/ptrace.h>
 13#include <linux/ftrace.h>
 14#include <linux/kexec.h>
 15#include <linux/bug.h>
 16#include <linux/nmi.h>
 17#include <linux/sysfs.h>
 18
 19#include <asm/stacktrace.h>
 20
 21
 22int panic_on_unrecovered_nmi;
 23int panic_on_io_nmi;
 24unsigned int code_bytes = 64;
 25int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
 26static int die_counter;
 27
 28void printk_address(unsigned long address, int reliable)
 29{
 30	printk(" [<%p>] %s%pB\n", (void *) address,
 31			reliable ? "" : "? ", (void *) address);
 
 
 
 
 
 32}
 33
 34#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 35static void
 36print_ftrace_graph_addr(unsigned long addr, void *data,
 37			const struct stacktrace_ops *ops,
 38			struct thread_info *tinfo, int *graph)
 39{
 40	struct task_struct *task = tinfo->task;
 41	unsigned long ret_addr;
 42	int index = task->curr_ret_stack;
 43
 44	if (addr != (unsigned long)return_to_handler)
 45		return;
 46
 
 
 
 47	if (!task->ret_stack || index < *graph)
 48		return;
 49
 50	index -= *graph;
 51	ret_addr = task->ret_stack[index].ret;
 52
 53	ops->address(data, ret_addr, 1);
 54
 55	(*graph)++;
 56}
 57#else
 58static inline void
 59print_ftrace_graph_addr(unsigned long addr, void *data,
 60			const struct stacktrace_ops *ops,
 61			struct thread_info *tinfo, int *graph)
 62{ }
 63#endif
 64
 65/*
 66 * x86-64 can have up to three kernel stacks:
 67 * process stack
 68 * interrupt stack
 69 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
 70 */
 71
 72static inline int valid_stack_ptr(struct thread_info *tinfo,
 73			void *p, unsigned int size, void *end)
 74{
 75	void *t = tinfo;
 76	if (end) {
 77		if (p < end && p >= (end-THREAD_SIZE))
 78			return 1;
 79		else
 80			return 0;
 81	}
 82	return p > t && p < t + THREAD_SIZE - size;
 83}
 84
 85unsigned long
 86print_context_stack(struct thread_info *tinfo,
 87		unsigned long *stack, unsigned long bp,
 88		const struct stacktrace_ops *ops, void *data,
 89		unsigned long *end, int *graph)
 90{
 91	struct stack_frame *frame = (struct stack_frame *)bp;
 92
 93	while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
 94		unsigned long addr;
 95
 96		addr = *stack;
 97		if (__kernel_text_address(addr)) {
 98			if ((unsigned long) stack == bp + sizeof(long)) {
 99				ops->address(data, addr, 1);
100				frame = frame->next_frame;
101				bp = (unsigned long) frame;
102			} else {
103				ops->address(data, addr, 0);
104			}
105			print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
106		}
107		stack++;
108	}
109	return bp;
110}
111EXPORT_SYMBOL_GPL(print_context_stack);
112
113unsigned long
114print_context_stack_bp(struct thread_info *tinfo,
115		       unsigned long *stack, unsigned long bp,
116		       const struct stacktrace_ops *ops, void *data,
117		       unsigned long *end, int *graph)
118{
119	struct stack_frame *frame = (struct stack_frame *)bp;
120	unsigned long *ret_addr = &frame->return_address;
121
122	while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
123		unsigned long addr = *ret_addr;
124
125		if (!__kernel_text_address(addr))
126			break;
127
128		ops->address(data, addr, 1);
129		frame = frame->next_frame;
130		ret_addr = &frame->return_address;
131		print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
132	}
133
134	return (unsigned long)frame;
135}
136EXPORT_SYMBOL_GPL(print_context_stack_bp);
137
138static int print_trace_stack(void *data, char *name)
139{
140	printk("%s <%s> ", (char *)data, name);
141	return 0;
142}
143
144/*
145 * Print one address/symbol entries per line.
146 */
147static void print_trace_address(void *data, unsigned long addr, int reliable)
148{
149	touch_nmi_watchdog();
150	printk(data);
151	printk_address(addr, reliable);
152}
153
154static const struct stacktrace_ops print_trace_ops = {
155	.stack			= print_trace_stack,
156	.address		= print_trace_address,
157	.walk_stack		= print_context_stack,
158};
159
160void
161show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
162		unsigned long *stack, unsigned long bp, char *log_lvl)
163{
164	printk("%sCall Trace:\n", log_lvl);
165	dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
166}
167
168void show_trace(struct task_struct *task, struct pt_regs *regs,
169		unsigned long *stack, unsigned long bp)
170{
171	show_trace_log_lvl(task, regs, stack, bp, "");
172}
173
174void show_stack(struct task_struct *task, unsigned long *sp)
175{
176	show_stack_log_lvl(task, NULL, sp, 0, "");
177}
178
179/*
180 * The architecture-independent dump_stack generator
181 */
182void dump_stack(void)
183{
184	unsigned long bp;
185	unsigned long stack;
186
187	bp = stack_frame(current, NULL);
188	printk("Pid: %d, comm: %.20s %s %s %.*s\n",
189		current->pid, current->comm, print_tainted(),
190		init_utsname()->release,
191		(int)strcspn(init_utsname()->version, " "),
192		init_utsname()->version);
193	show_trace(NULL, NULL, &stack, bp);
 
 
 
194}
195EXPORT_SYMBOL(dump_stack);
196
197static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
198static int die_owner = -1;
199static unsigned int die_nest_count;
200
201unsigned __kprobes long oops_begin(void)
202{
203	int cpu;
204	unsigned long flags;
205
206	oops_enter();
207
208	/* racy, but better than risking deadlock. */
209	raw_local_irq_save(flags);
210	cpu = smp_processor_id();
211	if (!arch_spin_trylock(&die_lock)) {
212		if (cpu == die_owner)
213			/* nested oops. should stop eventually */;
214		else
215			arch_spin_lock(&die_lock);
216	}
217	die_nest_count++;
218	die_owner = cpu;
219	console_verbose();
220	bust_spinlocks(1);
221	return flags;
222}
223EXPORT_SYMBOL_GPL(oops_begin);
224
225void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
226{
227	if (regs && kexec_should_crash(current))
228		crash_kexec(regs);
229
230	bust_spinlocks(0);
231	die_owner = -1;
232	add_taint(TAINT_DIE);
233	die_nest_count--;
234	if (!die_nest_count)
235		/* Nest count reaches zero, release the lock. */
236		arch_spin_unlock(&die_lock);
237	raw_local_irq_restore(flags);
238	oops_exit();
239
240	if (!signr)
241		return;
242	if (in_interrupt())
243		panic("Fatal exception in interrupt");
244	if (panic_on_oops)
245		panic("Fatal exception");
246	do_exit(signr);
247}
248
249int __kprobes __die(const char *str, struct pt_regs *regs, long err)
250{
251#ifdef CONFIG_X86_32
252	unsigned short ss;
253	unsigned long sp;
254#endif
255	printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
 
256#ifdef CONFIG_PREEMPT
257	printk("PREEMPT ");
258#endif
259#ifdef CONFIG_SMP
260	printk("SMP ");
261#endif
262#ifdef CONFIG_DEBUG_PAGEALLOC
263	printk("DEBUG_PAGEALLOC");
264#endif
265	printk("\n");
266	if (notify_die(DIE_OOPS, str, regs, err,
267			current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
268		return 1;
269
270	show_registers(regs);
 
271#ifdef CONFIG_X86_32
272	if (user_mode_vm(regs)) {
273		sp = regs->sp;
274		ss = regs->ss & 0xffff;
275	} else {
276		sp = kernel_stack_pointer(regs);
277		savesegment(ss, ss);
278	}
279	printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
280	print_symbol("%s", regs->ip);
281	printk(" SS:ESP %04x:%08lx\n", ss, sp);
282#else
283	/* Executive summary in case the oops scrolled away */
284	printk(KERN_ALERT "RIP ");
285	printk_address(regs->ip, 1);
286	printk(" RSP <%016lx>\n", regs->sp);
287#endif
288	return 0;
289}
290
291/*
292 * This is gone through when something in the kernel has done something bad
293 * and is about to be terminated:
294 */
295void die(const char *str, struct pt_regs *regs, long err)
296{
297	unsigned long flags = oops_begin();
298	int sig = SIGSEGV;
299
300	if (!user_mode_vm(regs))
301		report_bug(regs->ip, regs);
302
303	if (__die(str, regs, err))
304		sig = 0;
305	oops_end(flags, regs, sig);
306}
307
308static int __init kstack_setup(char *s)
309{
 
 
 
310	if (!s)
311		return -EINVAL;
312	kstack_depth_to_print = simple_strtoul(s, NULL, 0);
 
 
 
 
313	return 0;
314}
315early_param("kstack", kstack_setup);
316
317static int __init code_bytes_setup(char *s)
318{
319	code_bytes = simple_strtoul(s, NULL, 0);
 
 
 
 
 
 
 
 
 
 
320	if (code_bytes > 8192)
321		code_bytes = 8192;
322
323	return 1;
324}
325__setup("code_bytes=", code_bytes_setup);
v3.15
  1/*
  2 *  Copyright (C) 1991, 1992  Linus Torvalds
  3 *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4 */
  5#include <linux/kallsyms.h>
  6#include <linux/kprobes.h>
  7#include <linux/uaccess.h>
  8#include <linux/utsname.h>
  9#include <linux/hardirq.h>
 10#include <linux/kdebug.h>
 11#include <linux/module.h>
 12#include <linux/ptrace.h>
 13#include <linux/ftrace.h>
 14#include <linux/kexec.h>
 15#include <linux/bug.h>
 16#include <linux/nmi.h>
 17#include <linux/sysfs.h>
 18
 19#include <asm/stacktrace.h>
 20
 21
 22int panic_on_unrecovered_nmi;
 23int panic_on_io_nmi;
 24unsigned int code_bytes = 64;
 25int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
 26static int die_counter;
 27
 28static void printk_stack_address(unsigned long address, int reliable)
 29{
 30	pr_cont(" [<%p>] %s%pB\n",
 31		(void *)address, reliable ? "" : "? ", (void *)address);
 32}
 33
 34void printk_address(unsigned long address)
 35{
 36	pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
 37}
 38
 39#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 40static void
 41print_ftrace_graph_addr(unsigned long addr, void *data,
 42			const struct stacktrace_ops *ops,
 43			struct thread_info *tinfo, int *graph)
 44{
 45	struct task_struct *task;
 46	unsigned long ret_addr;
 47	int index;
 48
 49	if (addr != (unsigned long)return_to_handler)
 50		return;
 51
 52	task = tinfo->task;
 53	index = task->curr_ret_stack;
 54
 55	if (!task->ret_stack || index < *graph)
 56		return;
 57
 58	index -= *graph;
 59	ret_addr = task->ret_stack[index].ret;
 60
 61	ops->address(data, ret_addr, 1);
 62
 63	(*graph)++;
 64}
 65#else
 66static inline void
 67print_ftrace_graph_addr(unsigned long addr, void *data,
 68			const struct stacktrace_ops *ops,
 69			struct thread_info *tinfo, int *graph)
 70{ }
 71#endif
 72
 73/*
 74 * x86-64 can have up to three kernel stacks:
 75 * process stack
 76 * interrupt stack
 77 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
 78 */
 79
 80static inline int valid_stack_ptr(struct thread_info *tinfo,
 81			void *p, unsigned int size, void *end)
 82{
 83	void *t = tinfo;
 84	if (end) {
 85		if (p < end && p >= (end-THREAD_SIZE))
 86			return 1;
 87		else
 88			return 0;
 89	}
 90	return p > t && p < t + THREAD_SIZE - size;
 91}
 92
 93unsigned long
 94print_context_stack(struct thread_info *tinfo,
 95		unsigned long *stack, unsigned long bp,
 96		const struct stacktrace_ops *ops, void *data,
 97		unsigned long *end, int *graph)
 98{
 99	struct stack_frame *frame = (struct stack_frame *)bp;
100
101	while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
102		unsigned long addr;
103
104		addr = *stack;
105		if (__kernel_text_address(addr)) {
106			if ((unsigned long) stack == bp + sizeof(long)) {
107				ops->address(data, addr, 1);
108				frame = frame->next_frame;
109				bp = (unsigned long) frame;
110			} else {
111				ops->address(data, addr, 0);
112			}
113			print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
114		}
115		stack++;
116	}
117	return bp;
118}
119EXPORT_SYMBOL_GPL(print_context_stack);
120
121unsigned long
122print_context_stack_bp(struct thread_info *tinfo,
123		       unsigned long *stack, unsigned long bp,
124		       const struct stacktrace_ops *ops, void *data,
125		       unsigned long *end, int *graph)
126{
127	struct stack_frame *frame = (struct stack_frame *)bp;
128	unsigned long *ret_addr = &frame->return_address;
129
130	while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
131		unsigned long addr = *ret_addr;
132
133		if (!__kernel_text_address(addr))
134			break;
135
136		ops->address(data, addr, 1);
137		frame = frame->next_frame;
138		ret_addr = &frame->return_address;
139		print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
140	}
141
142	return (unsigned long)frame;
143}
144EXPORT_SYMBOL_GPL(print_context_stack_bp);
145
146static int print_trace_stack(void *data, char *name)
147{
148	printk("%s <%s> ", (char *)data, name);
149	return 0;
150}
151
152/*
153 * Print one address/symbol entries per line.
154 */
155static void print_trace_address(void *data, unsigned long addr, int reliable)
156{
157	touch_nmi_watchdog();
158	printk(data);
159	printk_stack_address(addr, reliable);
160}
161
162static const struct stacktrace_ops print_trace_ops = {
163	.stack			= print_trace_stack,
164	.address		= print_trace_address,
165	.walk_stack		= print_context_stack,
166};
167
168void
169show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
170		unsigned long *stack, unsigned long bp, char *log_lvl)
171{
172	printk("%sCall Trace:\n", log_lvl);
173	dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
174}
175
176void show_trace(struct task_struct *task, struct pt_regs *regs,
177		unsigned long *stack, unsigned long bp)
178{
179	show_trace_log_lvl(task, regs, stack, bp, "");
180}
181
182void show_stack(struct task_struct *task, unsigned long *sp)
183{
184	unsigned long bp = 0;
 
 
 
 
 
 
 
 
185	unsigned long stack;
186
187	/*
188	 * Stack frames below this one aren't interesting.  Don't show them
189	 * if we're printing for %current.
190	 */
191	if (!sp && (!task || task == current)) {
192		sp = &stack;
193		bp = stack_frame(current, NULL);
194	}
195
196	show_stack_log_lvl(task, NULL, sp, bp, "");
197}
 
198
199static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
200static int die_owner = -1;
201static unsigned int die_nest_count;
202
203unsigned __kprobes long oops_begin(void)
204{
205	int cpu;
206	unsigned long flags;
207
208	oops_enter();
209
210	/* racy, but better than risking deadlock. */
211	raw_local_irq_save(flags);
212	cpu = smp_processor_id();
213	if (!arch_spin_trylock(&die_lock)) {
214		if (cpu == die_owner)
215			/* nested oops. should stop eventually */;
216		else
217			arch_spin_lock(&die_lock);
218	}
219	die_nest_count++;
220	die_owner = cpu;
221	console_verbose();
222	bust_spinlocks(1);
223	return flags;
224}
225EXPORT_SYMBOL_GPL(oops_begin);
226
227void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
228{
229	if (regs && kexec_should_crash(current))
230		crash_kexec(regs);
231
232	bust_spinlocks(0);
233	die_owner = -1;
234	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
235	die_nest_count--;
236	if (!die_nest_count)
237		/* Nest count reaches zero, release the lock. */
238		arch_spin_unlock(&die_lock);
239	raw_local_irq_restore(flags);
240	oops_exit();
241
242	if (!signr)
243		return;
244	if (in_interrupt())
245		panic("Fatal exception in interrupt");
246	if (panic_on_oops)
247		panic("Fatal exception");
248	do_exit(signr);
249}
250
251int __kprobes __die(const char *str, struct pt_regs *regs, long err)
252{
253#ifdef CONFIG_X86_32
254	unsigned short ss;
255	unsigned long sp;
256#endif
257	printk(KERN_DEFAULT
258	       "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
259#ifdef CONFIG_PREEMPT
260	printk("PREEMPT ");
261#endif
262#ifdef CONFIG_SMP
263	printk("SMP ");
264#endif
265#ifdef CONFIG_DEBUG_PAGEALLOC
266	printk("DEBUG_PAGEALLOC");
267#endif
268	printk("\n");
269	if (notify_die(DIE_OOPS, str, regs, err,
270			current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
271		return 1;
272
273	print_modules();
274	show_regs(regs);
275#ifdef CONFIG_X86_32
276	if (user_mode_vm(regs)) {
277		sp = regs->sp;
278		ss = regs->ss & 0xffff;
279	} else {
280		sp = kernel_stack_pointer(regs);
281		savesegment(ss, ss);
282	}
283	printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
284	print_symbol("%s", regs->ip);
285	printk(" SS:ESP %04x:%08lx\n", ss, sp);
286#else
287	/* Executive summary in case the oops scrolled away */
288	printk(KERN_ALERT "RIP ");
289	printk_address(regs->ip);
290	printk(" RSP <%016lx>\n", regs->sp);
291#endif
292	return 0;
293}
294
295/*
296 * This is gone through when something in the kernel has done something bad
297 * and is about to be terminated:
298 */
299void die(const char *str, struct pt_regs *regs, long err)
300{
301	unsigned long flags = oops_begin();
302	int sig = SIGSEGV;
303
304	if (!user_mode_vm(regs))
305		report_bug(regs->ip, regs);
306
307	if (__die(str, regs, err))
308		sig = 0;
309	oops_end(flags, regs, sig);
310}
311
312static int __init kstack_setup(char *s)
313{
314	ssize_t ret;
315	unsigned long val;
316
317	if (!s)
318		return -EINVAL;
319
320	ret = kstrtoul(s, 0, &val);
321	if (ret)
322		return ret;
323	kstack_depth_to_print = val;
324	return 0;
325}
326early_param("kstack", kstack_setup);
327
328static int __init code_bytes_setup(char *s)
329{
330	ssize_t ret;
331	unsigned long val;
332
333	if (!s)
334		return -EINVAL;
335
336	ret = kstrtoul(s, 0, &val);
337	if (ret)
338		return ret;
339
340	code_bytes = val;
341	if (code_bytes > 8192)
342		code_bytes = 8192;
343
344	return 1;
345}
346__setup("code_bytes=", code_bytes_setup);