1b819cea2SGordon Ross /*
2b819cea2SGordon Ross  * CDDL HEADER START
3b819cea2SGordon Ross  *
4b819cea2SGordon Ross  * The contents of this file are subject to the terms of the
5b819cea2SGordon Ross  * Common Development and Distribution License (the "License").
6b819cea2SGordon Ross  * You may not use this file except in compliance with the License.
7b819cea2SGordon Ross  *
8b819cea2SGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b819cea2SGordon Ross  * or http://www.opensolaris.org/os/licensing.
10b819cea2SGordon Ross  * See the License for the specific language governing permissions
11b819cea2SGordon Ross  * and limitations under the License.
12b819cea2SGordon Ross  *
13b819cea2SGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14b819cea2SGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b819cea2SGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16b819cea2SGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17b819cea2SGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18b819cea2SGordon Ross  *
19b819cea2SGordon Ross  * CDDL HEADER END
20b819cea2SGordon Ross  */
21b819cea2SGordon Ross /*
22b819cea2SGordon Ross  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23b819cea2SGordon Ross  * Copyright (c) 2012 by Delphix. All rights reserved.
248e6f7903SYuri Pankov  * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
25*0a34963cSGordon Ross  * Copyright 2022 RackTop Systems, Inc.
26a923e7f4SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
27b819cea2SGordon Ross  */
28b819cea2SGordon Ross 
29b819cea2SGordon Ross #include <sys/param.h>
30b819cea2SGordon Ross #include <sys/types.h>
31b819cea2SGordon Ross #include <sys/varargs.h>
32b819cea2SGordon Ross #include <sys/systm.h>
33b819cea2SGordon Ross #include <sys/cmn_err.h>
34b819cea2SGordon Ross #include <sys/log.h>
35*0a34963cSGordon Ross #include <upanic.h>
36b819cea2SGordon Ross 
37b819cea2SGordon Ross #include <fakekernel.h>
38b819cea2SGordon Ross 
398329232eSGordon Ross void	debug_enter(char *);
40b819cea2SGordon Ross 
41b819cea2SGordon Ross char *volatile panicstr;
42b819cea2SGordon Ross va_list  panicargs;
43b819cea2SGordon Ross char panicbuf[512];
44b819cea2SGordon Ross 
458e6f7903SYuri Pankov int aok;
46b819cea2SGordon Ross 
47b819cea2SGordon Ross static const int
48b819cea2SGordon Ross ce_flags[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
49b819cea2SGordon Ross static const char
50b819cea2SGordon Ross ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
51b819cea2SGordon Ross static const char
52b819cea2SGordon Ross ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
53b819cea2SGordon Ross 
54b819cea2SGordon Ross 
55b819cea2SGordon Ross /*
56b819cea2SGordon Ross  * This function is just a stub, exported NODIRECT so that
57b819cea2SGordon Ross  * comsumers like fksmbd can provide their own.
58b819cea2SGordon Ross  * (One that actually prints the messages.)
59b819cea2SGordon Ross  *
60b819cea2SGordon Ross  * It's used by fakekernel_cprintf() below.
61b819cea2SGordon Ross  * The flags are SL_... from strlog.h
62b819cea2SGordon Ross  */
63b819cea2SGordon Ross /* ARGSUSED */
64b819cea2SGordon Ross void
fakekernel_putlog(char * msg,size_t len,int flags)65b819cea2SGordon Ross fakekernel_putlog(char *msg, size_t len, int flags)
66b819cea2SGordon Ross {
67b819cea2SGordon Ross }
68b819cea2SGordon Ross 
69b819cea2SGordon Ross /*
70b819cea2SGordon Ross  * fakekernel_cprintf() corresponds to os/printf.c:cprintf()
71b819cea2SGordon Ross  * This formats the message and calls fakekernel_putlog().
72b819cea2SGordon Ross  * It's exported NODIRECT to allow replacment.
73b819cea2SGordon Ross  * The flags are SL_... from strlog.h
74b819cea2SGordon Ross  */
75b819cea2SGordon Ross void
fakekernel_cprintf(const char * fmt,va_list adx,int flags,const char * prefix,const char * suffix)76b819cea2SGordon Ross fakekernel_cprintf(const char *fmt, va_list adx, int flags,
77f06dce2cSAndrew Stormont     const char *prefix, const char *suffix)
78b819cea2SGordon Ross {
79b819cea2SGordon Ross 	size_t bufsize = LOG_MSGSIZE;
80b819cea2SGordon Ross 	char buf[LOG_MSGSIZE];
81b819cea2SGordon Ross 	char *bufp = buf;
82b819cea2SGordon Ross 	char *msgp, *bufend;
83b819cea2SGordon Ross 	size_t len;
84b819cea2SGordon Ross 
85b819cea2SGordon Ross 	if (strchr("^!?", fmt[0]) != NULL) {
86b819cea2SGordon Ross 		if (fmt[0] == '^')
87b819cea2SGordon Ross 			flags |= SL_CONSONLY;
88b819cea2SGordon Ross 		else if (fmt[0] == '!')
89b819cea2SGordon Ross 			flags |= SL_LOGONLY;
90b819cea2SGordon Ross 		fmt++;
91b819cea2SGordon Ross 	}
92b819cea2SGordon Ross 
93b819cea2SGordon Ross 	bufend = bufp + bufsize;
94b819cea2SGordon Ross 	msgp = bufp;
95b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, "[fake_kernel] ");
96b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, prefix);
97b819cea2SGordon Ross 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
98b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, suffix);
99b819cea2SGordon Ross 	len = msgp - bufp;
100b819cea2SGordon Ross 
101b819cea2SGordon Ross 	fakekernel_putlog(bufp, len, flags);
102b819cea2SGordon Ross }
103b819cea2SGordon Ross 
1048329232eSGordon Ross /* ARGSUSED */
1058329232eSGordon Ross void
vzprintf(zoneid_t zoneid,const char * fmt,va_list adx)1068329232eSGordon Ross vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
1078329232eSGordon Ross {
1088329232eSGordon Ross 	fakekernel_cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "");
1098329232eSGordon Ross }
1108329232eSGordon Ross 
1118329232eSGordon Ross /*PRINTFLIKE2*/
1128329232eSGordon Ross void
zprintf(zoneid_t zoneid,const char * fmt,...)1138329232eSGordon Ross zprintf(zoneid_t zoneid, const char *fmt, ...)
1148329232eSGordon Ross {
1158329232eSGordon Ross 	va_list adx;
1168329232eSGordon Ross 
1178329232eSGordon Ross 	va_start(adx, fmt);
1188329232eSGordon Ross 	vzprintf(zoneid, fmt, adx);
1198329232eSGordon Ross 	va_end(adx);
1208329232eSGordon Ross }
1218329232eSGordon Ross 
122b819cea2SGordon Ross /*
123b819cea2SGordon Ross  * "User-level crash dump", if you will.
124b819cea2SGordon Ross  */
125b819cea2SGordon Ross void
vpanic(const char * fmt,va_list adx)126b819cea2SGordon Ross vpanic(const char *fmt, va_list adx)
127b819cea2SGordon Ross {
128b819cea2SGordon Ross 	va_list tmpargs;
129b819cea2SGordon Ross 
130b819cea2SGordon Ross 	panicstr = (char *)fmt;
131b819cea2SGordon Ross 	va_copy(panicargs, adx);
132b819cea2SGordon Ross 
133b819cea2SGordon Ross 	va_copy(tmpargs, adx);
134b819cea2SGordon Ross 	fakekernel_cprintf(fmt, tmpargs, SL_FATAL, "fatal: ", "\n");
135b819cea2SGordon Ross 
136b819cea2SGordon Ross 	(void) vsnprintf(panicbuf, sizeof (panicbuf), fmt, adx);
1378329232eSGordon Ross 	debug_enter(panicbuf);
138b819cea2SGordon Ross 
139*0a34963cSGordon Ross 	/* Call libc`upanic() so that mdb ::status works */
140*0a34963cSGordon Ross 	upanic(panicbuf, sizeof (panicbuf));
141b819cea2SGordon Ross }
142b819cea2SGordon Ross 
143b819cea2SGordon Ross void
panic(const char * fmt,...)144b819cea2SGordon Ross panic(const char *fmt, ...)
145b819cea2SGordon Ross {
146b819cea2SGordon Ross 	va_list adx;
147b819cea2SGordon Ross 
148b819cea2SGordon Ross 	va_start(adx, fmt);
149b819cea2SGordon Ross 	vpanic(fmt, adx);
150b819cea2SGordon Ross 	va_end(adx);
151b819cea2SGordon Ross }
152b819cea2SGordon Ross 
153f06dce2cSAndrew Stormont void
fm_panic(const char * fmt,...)154f06dce2cSAndrew Stormont fm_panic(const char *fmt, ...)
155f06dce2cSAndrew Stormont {
156f06dce2cSAndrew Stormont 	va_list adx;
157f06dce2cSAndrew Stormont 
158f06dce2cSAndrew Stormont 	va_start(adx, fmt);
159f06dce2cSAndrew Stormont 	vpanic(fmt, adx);
160f06dce2cSAndrew Stormont 	va_end(adx);
161f06dce2cSAndrew Stormont }
162f06dce2cSAndrew Stormont 
163b819cea2SGordon Ross void
vcmn_err(int ce,const char * fmt,va_list adx)164b819cea2SGordon Ross vcmn_err(int ce, const char *fmt, va_list adx)
165b819cea2SGordon Ross {
166b819cea2SGordon Ross 
167b819cea2SGordon Ross 	if (ce == CE_PANIC)
168b819cea2SGordon Ross 		vpanic(fmt, adx);
169b819cea2SGordon Ross 	if (ce >= CE_IGNORE)
170b819cea2SGordon Ross 		return;
171b819cea2SGordon Ross 
172b819cea2SGordon Ross 	fakekernel_cprintf(fmt, adx, ce_flags[ce] | SL_CONSOLE,
173b819cea2SGordon Ross 	    ce_prefix[ce], ce_suffix[ce]);
174b819cea2SGordon Ross }
175b819cea2SGordon Ross 
176b819cea2SGordon Ross /*PRINTFLIKE2*/
177b819cea2SGordon Ross void
cmn_err(int ce,const char * fmt,...)178b819cea2SGordon Ross cmn_err(int ce, const char *fmt, ...)
179b819cea2SGordon Ross {
180b819cea2SGordon Ross 	va_list adx;
181b819cea2SGordon Ross 
182b819cea2SGordon Ross 	va_start(adx, fmt);
183b819cea2SGordon Ross 	vcmn_err(ce, fmt, adx);
184b819cea2SGordon Ross 	va_end(adx);
185b819cea2SGordon Ross }
1868329232eSGordon Ross 
1878329232eSGordon Ross /* ARGSUSED */
1888329232eSGordon Ross void
debug_enter(char * str)1898329232eSGordon Ross debug_enter(char *str)
1908329232eSGordon Ross {
1918329232eSGordon Ross 	/* Just a place for a break point. */
1928329232eSGordon Ross }
1938e6f7903SYuri Pankov 
1948e6f7903SYuri Pankov int
assfail(const char * a,const char * f,int l)1958e6f7903SYuri Pankov assfail(const char *a, const char *f, int l)
1968e6f7903SYuri Pankov {
1978e6f7903SYuri Pankov 	if (!aok)
1988e6f7903SYuri Pankov 		panic("assertion failed: %s, file: %s, line: %d", a, f, l);
1998e6f7903SYuri Pankov 
2008e6f7903SYuri Pankov 	fprintf(stderr, "ASSERTION CAUGHT: %s, file: %s, line: %d\n", a, f, l);
2018e6f7903SYuri Pankov 
2028e6f7903SYuri Pankov 	return (0);
2038e6f7903SYuri Pankov }
2048e6f7903SYuri Pankov 
2058e6f7903SYuri Pankov void
assfail3(const char * a,uintmax_t lv,const char * op,uintmax_t rv,const char * f,int l)2068e6f7903SYuri Pankov assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
2078e6f7903SYuri Pankov     const char *f, int l)
2088e6f7903SYuri Pankov {
2098e6f7903SYuri Pankov 	if (!aok) {
2108e6f7903SYuri Pankov 		panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
2118e6f7903SYuri Pankov 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
2128e6f7903SYuri Pankov 		    f, l);
2138e6f7903SYuri Pankov 	}
2148e6f7903SYuri Pankov 
2158e6f7903SYuri Pankov 	fprintf(stderr, "ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
2168e6f7903SYuri Pankov 	    "line: %d\n", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
2178e6f7903SYuri Pankov 	    f, l);
2188e6f7903SYuri Pankov }
219