Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Provide a default dump_stack() function for architectures |
| 4 | * which don't implement their own. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 8 | #include <linux/export.h> |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 10 | #include <linux/sched/debug.h> |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 11 | #include <linux/smp.h> |
| 12 | #include <linux/atomic.h> |
Dave Young | e36df28 | 2018-02-13 15:28:34 +0800 | [diff] [blame] | 13 | #include <linux/kexec.h> |
| 14 | #include <linux/utsname.h> |
Peter Zijlstra | a8b62fd | 2020-09-21 12:58:17 +0200 | [diff] [blame] | 15 | #include <linux/stop_machine.h> |
Dave Young | e36df28 | 2018-02-13 15:28:34 +0800 | [diff] [blame] | 16 | |
| 17 | static char dump_stack_arch_desc_str[128]; |
| 18 | |
| 19 | /** |
| 20 | * dump_stack_set_arch_desc - set arch-specific str to show with task dumps |
| 21 | * @fmt: printf-style format string |
| 22 | * @...: arguments for the format string |
| 23 | * |
| 24 | * The configured string will be printed right after utsname during task |
| 25 | * dumps. Usually used to add arch-specific system identifiers. If an |
| 26 | * arch wants to make use of such an ID string, it should initialize this |
| 27 | * as soon as possible during boot. |
| 28 | */ |
| 29 | void __init dump_stack_set_arch_desc(const char *fmt, ...) |
| 30 | { |
| 31 | va_list args; |
| 32 | |
| 33 | va_start(args, fmt); |
| 34 | vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str), |
| 35 | fmt, args); |
| 36 | va_end(args); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * dump_stack_print_info - print generic debug info for dump_stack() |
| 41 | * @log_lvl: log level |
| 42 | * |
| 43 | * Arch-specific dump_stack() implementations can use this function to |
| 44 | * print out the same debug information as the generic dump_stack(). |
| 45 | */ |
| 46 | void dump_stack_print_info(const char *log_lvl) |
| 47 | { |
| 48 | printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s\n", |
| 49 | log_lvl, raw_smp_processor_id(), current->pid, current->comm, |
| 50 | kexec_crash_loaded() ? "Kdump: loaded " : "", |
| 51 | print_tainted(), |
| 52 | init_utsname()->release, |
| 53 | (int)strcspn(init_utsname()->version, " "), |
| 54 | init_utsname()->version); |
| 55 | |
| 56 | if (dump_stack_arch_desc_str[0] != '\0') |
| 57 | printk("%sHardware name: %s\n", |
| 58 | log_lvl, dump_stack_arch_desc_str); |
| 59 | |
| 60 | print_worker_info(log_lvl, current); |
Peter Zijlstra | a8b62fd | 2020-09-21 12:58:17 +0200 | [diff] [blame] | 61 | print_stop_info(log_lvl, current); |
Dave Young | e36df28 | 2018-02-13 15:28:34 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * show_regs_print_info - print generic debug info for show_regs() |
| 66 | * @log_lvl: log level |
| 67 | * |
| 68 | * show_regs() implementations can use this function to print out generic |
| 69 | * debug information. |
| 70 | */ |
| 71 | void show_regs_print_info(const char *log_lvl) |
| 72 | { |
| 73 | dump_stack_print_info(log_lvl); |
| 74 | } |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 75 | |
Alexander Potapenko | 4469c0f | 2021-06-28 19:40:30 -0700 | [diff] [blame] | 76 | static void __dump_stack(const char *log_lvl) |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 77 | { |
Alexander Potapenko | 4469c0f | 2021-06-28 19:40:30 -0700 | [diff] [blame] | 78 | dump_stack_print_info(log_lvl); |
| 79 | show_stack(NULL, NULL, log_lvl); |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 82 | /** |
| 83 | * dump_stack - dump the current task information and its stack trace |
| 84 | * |
| 85 | * Architectures can override this implementation by implementing its own. |
| 86 | */ |
Alexander Potapenko | 4469c0f | 2021-06-28 19:40:30 -0700 | [diff] [blame] | 87 | asmlinkage __visible void dump_stack_lvl(const char *log_lvl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Eric Dumazet | d7ce369 | 2016-02-05 15:36:16 -0800 | [diff] [blame] | 89 | unsigned long flags; |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Permit this cpu to perform nested stack dumps while serialising |
| 93 | * against other CPUs |
| 94 | */ |
John Ogness | 766c268 | 2021-06-17 11:56:50 +0206 | [diff] [blame] | 95 | printk_cpu_lock_irqsave(flags); |
Alexander Potapenko | 4469c0f | 2021-06-28 19:40:30 -0700 | [diff] [blame] | 96 | __dump_stack(log_lvl); |
John Ogness | 766c268 | 2021-06-17 11:56:50 +0206 | [diff] [blame] | 97 | printk_cpu_unlock_irqrestore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
Alexander Potapenko | 4469c0f | 2021-06-28 19:40:30 -0700 | [diff] [blame] | 99 | EXPORT_SYMBOL(dump_stack_lvl); |
| 100 | |
| 101 | asmlinkage __visible void dump_stack(void) |
| 102 | { |
| 103 | dump_stack_lvl(KERN_DEFAULT); |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | EXPORT_SYMBOL(dump_stack); |