include: gcc 7's cpp has problems with the line continuations in .x files
[unleashed.git] / kernel / os / printf.c
blob1870adf4b40ec2b23a9edc819feb39f7c21349ca
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2016 Joyent, Inc.
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/sysmacros.h>
33 #include <sys/inline.h>
34 #include <sys/varargs.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/cmn_err.h>
38 #include <sys/syslog.h>
39 #include <sys/log.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/session.h>
43 #include <sys/stream.h>
44 #include <sys/kmem.h>
45 #include <sys/kobj.h>
46 #include <sys/atomic.h>
47 #include <sys/console.h>
48 #include <sys/cpuvar.h>
49 #include <sys/modctl.h>
50 #include <sys/reboot.h>
51 #include <sys/debug.h>
52 #include <sys/panic.h>
53 #include <sys/spl.h>
54 #include <sys/zone.h>
55 #include <sys/sunddi.h>
58 * In some debugging situations it's useful to log all messages to panicbuf,
59 * which is persistent across reboots (on most platforms). The range
60 * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
61 * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
62 * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
63 * this will reserve 1K for panic information and 7K for message logging.
65 uint32_t panicbuf_log = PANICBUFSIZE;
66 uint32_t panicbuf_index = PANICBUFSIZE;
68 static int aask, aok;
69 static int ce_to_sl[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
70 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
71 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
73 static void
74 cprintf(const char *fmt, va_list adx, int sl, const char *prefix,
75 const char *suffix, void *site, int mid, int sid, int level,
76 zoneid_t zoneid, dev_info_t *dip)
78 uint32_t msgid;
79 size_t bufsize = LOG_MSGSIZE;
80 char buf[LOG_MSGSIZE];
81 char *bufp = buf;
82 char *body, *msgp, *bufend;
83 mblk_t *mp;
84 int s, on_intr;
85 size_t len;
87 s = splhi();
88 on_intr = CPU_ON_INTR(CPU) ||
89 (interrupts_unleashed && (spltoipl(s) > LOCK_LEVEL));
90 splx(s);
92 ASSERT(zoneid == GLOBAL_ZONEID || !on_intr);
94 STRLOG_MAKE_MSGID(fmt, msgid);
96 if (strchr("^!?", fmt[0]) != NULL) {
97 if (fmt[0] == '^')
98 sl |= SL_CONSONLY;
99 else if (fmt[0] == '!' ||
100 (prefix[0] == '\0' && !(boothowto & RB_VERBOSE)))
101 sl = (sl & ~(SL_USER | SL_NOTE)) | SL_LOGONLY;
102 fmt++;
105 if ((sl & SL_USER) && (MUTEX_HELD(&pidlock) || on_intr)) {
106 zoneid = getzoneid();
107 sl = sl & ~(SL_USER | SL_LOGONLY) | SL_CONSOLE;
110 retry:
111 bufend = bufp + bufsize;
112 msgp = bufp;
113 body = msgp += snprintf(msgp, bufend - msgp,
114 "%s: [ID %u FACILITY_AND_PRIORITY] ",
115 mod_containing_pc(site), msgid);
116 msgp += snprintf(msgp, bufend - msgp, prefix);
117 if (dip != NULL)
118 msgp += snprintf(msgp, bufend - msgp, "%s%d: ",
119 ddi_driver_name(dip), ddi_get_instance(dip));
120 msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
121 msgp += snprintf(msgp, bufend - msgp, suffix);
122 len = strlen(body);
124 if (((sl & SL_CONSONLY) && panicstr) ||
125 (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
126 console_printf("%s", body);
127 goto out;
130 if (msgp - bufp >= bufsize && !on_intr) {
131 ASSERT(bufp == buf);
132 bufsize = msgp - bufp + 1;
133 bufp = kmem_alloc(bufsize, KM_NOSLEEP);
134 if (bufp != NULL)
135 goto retry;
136 bufsize = LOG_MSGSIZE;
137 bufp = buf;
140 mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
141 MIN(bufsize, msgp - bufp + 1), on_intr);
142 if (mp == NULL) {
143 if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
144 console_printf("%s", body);
145 goto out;
148 if (sl & SL_USER) {
149 ssize_t resid;
150 sess_t *sp;
152 if ((sp = tty_hold()) != NULL) {
153 if (sp->s_vp != NULL)
154 (void) vn_rdwr(UIO_WRITE, sp->s_vp, body,
155 len, 0LL, UIO_SYSSPACE, FAPPEND,
156 (rlim64_t)LOG_HIWAT, kcred, &resid);
157 tty_rele(sp);
161 if (on_intr && !panicstr) {
162 (void) putq(log_intrq, mp);
163 softcall((void (*)(void *))log_flushq, log_intrq);
164 } else {
165 log_sendmsg(mp, zoneid);
167 out:
168 if (panicbuf_log + len < PANICBUFSIZE) {
169 uint32_t old, new;
170 do {
171 old = panicbuf_index;
172 new = old + len;
173 if (new >= PANICBUFSIZE)
174 new = panicbuf_log + len;
175 } while (atomic_cas_32(&panicbuf_index, old, new) != old);
176 bcopy(body, &panicbuf[new - len], len);
178 if (bufp != buf)
179 kmem_free(bufp, bufsize);
182 void
183 vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
185 cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
186 zoneid, NULL);
189 void
190 vprintf(const char *fmt, va_list adx)
192 vzprintf(GLOBAL_ZONEID, fmt, adx);
195 /*PRINTFLIKE1*/
196 void
197 printf(const char *fmt, ...)
199 va_list adx;
201 va_start(adx, fmt);
202 cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
203 GLOBAL_ZONEID, NULL);
204 va_end(adx);
207 /*PRINTFLIKE2*/
208 void
209 zprintf(zoneid_t zoneid, const char *fmt, ...)
211 va_list adx;
213 va_start(adx, fmt);
214 cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
215 zoneid, NULL);
216 va_end(adx);
219 void
220 vuprintf(const char *fmt, va_list adx)
222 va_list adxcp;
223 va_copy(adxcp, adx);
225 /* Message the user tty, if any, and the global zone syslog */
226 cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
227 "", "", caller(), 0, 0, 0, GLOBAL_ZONEID, NULL);
229 /* Now message the local zone syslog */
230 if (!INGLOBALZONE(curproc))
231 cprintf(fmt, adxcp, SL_CONSOLE | SL_LOGONLY | SL_NOTE,
232 "", "", caller(), 0, 0, 0, getzoneid(), NULL);
234 va_end(adxcp);
237 /*PRINTFLIKE1*/
238 void
239 uprintf(const char *fmt, ...)
241 va_list adx;
243 va_start(adx, fmt);
245 vuprintf(fmt, adx);
247 va_end(adx);
250 static void
251 vzdcmn_err(zoneid_t zoneid, void *site, int ce, const char *fmt, va_list adx,
252 dev_info_t *dip)
254 if (ce == CE_PANIC)
255 vpanic(fmt, adx);
256 if ((uint_t)ce < CE_IGNORE) {
257 cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
258 ce_prefix[ce], ce_suffix[ce], site, 0, 0, 0,
259 zoneid, dip);
263 void
264 vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
266 vzdcmn_err(zoneid, caller(), ce, fmt, adx, NULL);
269 void
270 vcmn_err(int ce, const char *fmt, va_list adx)
272 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, NULL);
275 /*PRINTFLIKE3*/
276 void
277 vdev_err(dev_info_t *dip, int ce, const char *fmt, va_list adx)
279 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, dip);
282 /*PRINTFLIKE2*/
283 void
284 cmn_err(int ce, const char *fmt, ...)
286 va_list adx;
288 va_start(adx, fmt);
289 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, NULL);
290 va_end(adx);
293 /*PRINTFLIKE3*/
294 void
295 zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
297 va_list adx;
299 va_start(adx, fmt);
300 vzdcmn_err(zoneid, caller(), ce, fmt, adx, NULL);
301 va_end(adx);
304 /*PRINTFLIKE3*/
305 void
306 dev_err(dev_info_t *dip, int ce, char *fmt, ...)
308 va_list adx;
310 va_start(adx, fmt);
311 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, dip);
312 va_end(adx);
316 assfail(const char *a, const char *f, int l)
318 if (aask) {
319 printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
320 debug_enter(NULL);
323 if (!aok && !panicstr)
324 panic("assertion failed: %s, file: %s, line: %d", a, f, l);
326 return (0);
329 void
330 assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
331 const char *f, int l)
333 if (aask) {
334 printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
335 "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
336 f, l);
337 debug_enter(NULL);
340 if (!aok && !panicstr)
341 panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
342 "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
343 f, l);
347 strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
349 if (sl & log_global.lz_active) {
350 va_list adx;
351 va_start(adx, fmt);
352 cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
353 GLOBAL_ZONEID, NULL);
354 va_end(adx);
356 return (1);
360 vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
362 if (sl & log_global.lz_active)
363 cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
364 GLOBAL_ZONEID, NULL);
365 return (1);