xref: /freebsd/sys/contrib/openzfs/module/os/linux/spl/spl-err.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: GPL-2.0-or-later
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
4eda14cbcSMatt Macy  *  Copyright (C) 2007 The Regents of the University of California.
5eda14cbcSMatt Macy  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6eda14cbcSMatt Macy  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
7eda14cbcSMatt Macy  *  UCRL-CODE-235197
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  *  This file is part of the SPL, Solaris Porting Layer.
10eda14cbcSMatt Macy  *
11eda14cbcSMatt Macy  *  The SPL is free software; you can redistribute it and/or modify it
12eda14cbcSMatt Macy  *  under the terms of the GNU General Public License as published by the
13eda14cbcSMatt Macy  *  Free Software Foundation; either version 2 of the License, or (at your
14eda14cbcSMatt Macy  *  option) any later version.
15eda14cbcSMatt Macy  *
16eda14cbcSMatt Macy  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
17eda14cbcSMatt Macy  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18eda14cbcSMatt Macy  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19eda14cbcSMatt Macy  *  for more details.
20eda14cbcSMatt Macy  *
21eda14cbcSMatt Macy  *  You should have received a copy of the GNU General Public License along
22eda14cbcSMatt Macy  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23eda14cbcSMatt Macy  *
24eda14cbcSMatt Macy  *  Solaris Porting Layer (SPL) Error Implementation.
25eda14cbcSMatt Macy  */
26eda14cbcSMatt Macy 
27eda14cbcSMatt Macy #include <sys/sysmacros.h>
28eda14cbcSMatt Macy #include <sys/cmn_err.h>
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy /*
31eda14cbcSMatt Macy  * It is often useful to actually have the panic crash the node so you
32eda14cbcSMatt Macy  * can then get notified of the event, get the crashdump for later
33eda14cbcSMatt Macy  * analysis and other such goodies.
34eda14cbcSMatt Macy  * But we would still default to the current default of not to do that.
35eda14cbcSMatt Macy  */
36dbd5678dSMartin Matuska static unsigned int spl_panic_halt;
37eda14cbcSMatt Macy module_param(spl_panic_halt, uint, 0644);
38eda14cbcSMatt Macy MODULE_PARM_DESC(spl_panic_halt, "Cause kernel panic on assertion failures");
39eda14cbcSMatt Macy 
40eda14cbcSMatt Macy void
spl_dumpstack(void)41eda14cbcSMatt Macy spl_dumpstack(void)
42eda14cbcSMatt Macy {
43eda14cbcSMatt Macy 	printk("Showing stack for process %d\n", current->pid);
44eda14cbcSMatt Macy 	dump_stack();
45eda14cbcSMatt Macy }
46eda14cbcSMatt Macy EXPORT_SYMBOL(spl_dumpstack);
47eda14cbcSMatt Macy 
48be181ee2SMartin Matuska void
spl_panic(const char * file,const char * func,int line,const char * fmt,...)49eda14cbcSMatt Macy spl_panic(const char *file, const char *func, int line, const char *fmt, ...)
50eda14cbcSMatt Macy {
51eda14cbcSMatt Macy 	const char *newfile;
52eda14cbcSMatt Macy 	char msg[MAXMSGLEN];
53eda14cbcSMatt Macy 	va_list ap;
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy 	newfile = strrchr(file, '/');
56eda14cbcSMatt Macy 	if (newfile != NULL)
57eda14cbcSMatt Macy 		newfile = newfile + 1;
58eda14cbcSMatt Macy 	else
59eda14cbcSMatt Macy 		newfile = file;
60eda14cbcSMatt Macy 
61eda14cbcSMatt Macy 	va_start(ap, fmt);
62eda14cbcSMatt Macy 	(void) vsnprintf(msg, sizeof (msg), fmt, ap);
63eda14cbcSMatt Macy 	va_end(ap);
64eda14cbcSMatt Macy 
65eda14cbcSMatt Macy 	printk(KERN_EMERG "%s", msg);
66eda14cbcSMatt Macy 	printk(KERN_EMERG "PANIC at %s:%d:%s()\n", newfile, line, func);
67eda14cbcSMatt Macy 	if (spl_panic_halt)
68eda14cbcSMatt Macy 		panic("%s", msg);
69eda14cbcSMatt Macy 
70eda14cbcSMatt Macy 	spl_dumpstack();
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy 	/* Halt the thread to facilitate further debugging */
73eda14cbcSMatt Macy 	set_current_state(TASK_UNINTERRUPTIBLE);
74eda14cbcSMatt Macy 	while (1)
75eda14cbcSMatt Macy 		schedule();
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy 	/* Unreachable */
78eda14cbcSMatt Macy }
79eda14cbcSMatt Macy EXPORT_SYMBOL(spl_panic);
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy void
vcmn_err(int ce,const char * fmt,va_list ap)82eda14cbcSMatt Macy vcmn_err(int ce, const char *fmt, va_list ap)
83eda14cbcSMatt Macy {
84eda14cbcSMatt Macy 	char msg[MAXMSGLEN];
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 	vsnprintf(msg, MAXMSGLEN, fmt, ap);
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy 	switch (ce) {
89eda14cbcSMatt Macy 	case CE_IGNORE:
90eda14cbcSMatt Macy 		break;
91eda14cbcSMatt Macy 	case CE_CONT:
92eda14cbcSMatt Macy 		printk("%s", msg);
93eda14cbcSMatt Macy 		break;
94eda14cbcSMatt Macy 	case CE_NOTE:
95eda14cbcSMatt Macy 		printk(KERN_NOTICE "NOTICE: %s\n", msg);
96eda14cbcSMatt Macy 		break;
97eda14cbcSMatt Macy 	case CE_WARN:
98eda14cbcSMatt Macy 		printk(KERN_WARNING "WARNING: %s\n", msg);
99eda14cbcSMatt Macy 		break;
100eda14cbcSMatt Macy 	case CE_PANIC:
101eda14cbcSMatt Macy 		printk(KERN_EMERG "PANIC: %s\n", msg);
102c03c5b1cSMartin Matuska 		if (spl_panic_halt)
103c03c5b1cSMartin Matuska 			panic("%s", msg);
104c03c5b1cSMartin Matuska 
105eda14cbcSMatt Macy 		spl_dumpstack();
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy 		/* Halt the thread to facilitate further debugging */
108eda14cbcSMatt Macy 		set_current_state(TASK_UNINTERRUPTIBLE);
109eda14cbcSMatt Macy 		while (1)
110eda14cbcSMatt Macy 			schedule();
111eda14cbcSMatt Macy 	}
112eda14cbcSMatt Macy } /* vcmn_err() */
113eda14cbcSMatt Macy EXPORT_SYMBOL(vcmn_err);
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy void
cmn_err(int ce,const char * fmt,...)116eda14cbcSMatt Macy cmn_err(int ce, const char *fmt, ...)
117eda14cbcSMatt Macy {
118eda14cbcSMatt Macy 	va_list ap;
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy 	va_start(ap, fmt);
121eda14cbcSMatt Macy 	vcmn_err(ce, fmt, ap);
122eda14cbcSMatt Macy 	va_end(ap);
123eda14cbcSMatt Macy } /* cmn_err() */
124eda14cbcSMatt Macy EXPORT_SYMBOL(cmn_err);
125