Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Stack trace utility |
| 3 | * |
| 4 | * Copyright 2008 Christoph Hellwig, IBM Corp. |
| 5 | * |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 14 | #include <linux/sched.h> |
| 15 | #include <linux/stacktrace.h> |
| 16 | #include <asm/ptrace.h> |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 17 | #include <asm/processor.h> |
Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Save stack-backtrace addresses into a stack_trace buffer. |
| 21 | */ |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 22 | static void save_context_stack(struct stack_trace *trace, unsigned long sp, |
| 23 | struct task_struct *tsk, int savesched) |
Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 24 | { |
Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 25 | for (;;) { |
| 26 | unsigned long *stack = (unsigned long *) sp; |
| 27 | unsigned long newsp, ip; |
| 28 | |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 29 | if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD)) |
Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 30 | return; |
| 31 | |
| 32 | newsp = stack[0]; |
| 33 | ip = stack[STACK_FRAME_LR_SAVE]; |
| 34 | |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 35 | if (savesched || !in_sched_functions(ip)) { |
| 36 | if (!trace->skip) |
| 37 | trace->entries[trace->nr_entries++] = ip; |
| 38 | else |
| 39 | trace->skip--; |
| 40 | } |
Christoph Hellwig | fd3e0bb | 2008-04-17 14:35:00 +1000 | [diff] [blame] | 41 | |
| 42 | if (trace->nr_entries >= trace->max_entries) |
| 43 | return; |
| 44 | |
| 45 | sp = newsp; |
| 46 | } |
| 47 | } |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 48 | |
| 49 | void save_stack_trace(struct stack_trace *trace) |
| 50 | { |
| 51 | unsigned long sp; |
| 52 | |
Anton Blanchard | acf620e | 2014-10-13 19:41:39 +1100 | [diff] [blame] | 53 | sp = current_stack_pointer(); |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 54 | |
| 55 | save_context_stack(trace, sp, current, 1); |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(save_stack_trace); |
| 58 | |
| 59 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) |
| 60 | { |
Arnd Bergmann | 6fdc9f5 | 2008-07-17 08:12:25 +1000 | [diff] [blame] | 61 | save_context_stack(trace, tsk->thread.ksp, tsk, 0); |
Arnd Bergmann | 01f4b8b | 2008-07-11 00:08:18 +1000 | [diff] [blame] | 62 | } |
| 63 | EXPORT_SYMBOL_GPL(save_stack_trace_tsk); |
Steven Rostedt | 35de3b1 | 2015-12-08 13:50:56 -0500 | [diff] [blame] | 64 | |
| 65 | void |
| 66 | save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) |
| 67 | { |
| 68 | save_context_stack(trace, regs->gpr[1], current, 0); |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(save_stack_trace_regs); |