summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/printf.c
blob: 42669f096d80c47bd3e9d41b14b2ef786c6039b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
 * Copyright 2016 Joyent, Inc.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/inline.h>
#include <sys/varargs.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/syslog.h>
#include <sys/log.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/session.h>
#include <sys/stream.h>
#include <sys/kmem.h>
#include <sys/kobj.h>
#include <sys/atomic.h>
#include <sys/console.h>
#include <sys/cpuvar.h>
#include <sys/modctl.h>
#include <sys/reboot.h>
#include <sys/debug.h>
#include <sys/panic.h>
#include <sys/spl.h>
#include <sys/zone.h>
#include <sys/sunddi.h>

/*
 * In some debugging situations it's useful to log all messages to panicbuf,
 * which is persistent across reboots (on most platforms).  The range
 * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
 * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
 * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
 * this will reserve 1K for panic information and 7K for message logging.
 */
uint32_t panicbuf_log = PANICBUFSIZE;
uint32_t panicbuf_index = PANICBUFSIZE;

int aask, aok;
static int ce_to_sl[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };

static void
cprintf(const char *fmt, va_list adx, int sl, const char *prefix,
    const char *suffix, void *site, int mid, int sid, int level,
    zoneid_t zoneid, dev_info_t *dip)
{
	uint32_t msgid;
	size_t bufsize = LOG_MSGSIZE;
	char buf[LOG_MSGSIZE];
	char *bufp = buf;
	char *body, *msgp, *bufend;
	mblk_t *mp;
	int s, on_intr;
	size_t len;

	s = splhi();
	on_intr = CPU_ON_INTR(CPU) ||
	    (interrupts_unleashed && (spltoipl(s) > LOCK_LEVEL));
	splx(s);

	ASSERT(zoneid == GLOBAL_ZONEID || !on_intr);

	STRLOG_MAKE_MSGID(fmt, msgid);

	if (strchr("^!?", fmt[0]) != NULL) {
		if (fmt[0] == '^')
			sl |= SL_CONSONLY;
		else if (fmt[0] == '!' ||
		    (prefix[0] == '\0' && !(boothowto & RB_VERBOSE)))
			sl = (sl & ~(SL_USER | SL_NOTE)) | SL_LOGONLY;
		fmt++;
	}

	if ((sl & SL_USER) && (MUTEX_HELD(&pidlock) || on_intr)) {
		zoneid = getzoneid();
		sl = sl & ~(SL_USER | SL_LOGONLY) | SL_CONSOLE;
	}

retry:
	bufend = bufp + bufsize;
	msgp = bufp;
	body = msgp += snprintf(msgp, bufend - msgp,
	    "%s: [ID %u FACILITY_AND_PRIORITY] ",
	    mod_containing_pc(site), msgid);
	msgp += snprintf(msgp, bufend - msgp, prefix);
	if (dip != NULL)
		msgp += snprintf(msgp, bufend - msgp, "%s%d: ",
		    ddi_driver_name(dip), ddi_get_instance(dip));
	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
	msgp += snprintf(msgp, bufend - msgp, suffix);
	len = strlen(body);

	if (((sl & SL_CONSONLY) && panicstr) ||
	    (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
		console_printf("%s", body);
		goto out;
	}

	if (msgp - bufp >= bufsize && !on_intr) {
		ASSERT(bufp == buf);
		bufsize = msgp - bufp + 1;
		bufp = kmem_alloc(bufsize, KM_NOSLEEP);
		if (bufp != NULL)
			goto retry;
		bufsize = LOG_MSGSIZE;
		bufp = buf;
	}

	mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
	    MIN(bufsize, msgp - bufp + 1), on_intr);
	if (mp == NULL) {
		if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
			console_printf("%s", body);
		goto out;
	}

	if (sl & SL_USER) {
		ssize_t resid;
		sess_t *sp;

		if ((sp = tty_hold()) != NULL) {
			if (sp->s_vp != NULL)
				(void) vn_rdwr(UIO_WRITE, sp->s_vp, body,
				    len, 0LL, UIO_SYSSPACE, FAPPEND,
				    (rlim64_t)LOG_HIWAT, kcred, &resid);
			tty_rele(sp);
		}
	}

	if (on_intr && !panicstr) {
		(void) putq(log_intrq, mp);
		softcall((void (*)(void *))log_flushq, log_intrq);
	} else {
		log_sendmsg(mp, zoneid);
	}
out:
	if (panicbuf_log + len < PANICBUFSIZE) {
		uint32_t old, new;
		do {
			old = panicbuf_index;
			new = old + len;
			if (new >= PANICBUFSIZE)
				new = panicbuf_log + len;
		} while (atomic_cas_32(&panicbuf_index, old, new) != old);
		bcopy(body, &panicbuf[new - len], len);
	}
	if (bufp != buf)
		kmem_free(bufp, bufsize);
}

void
vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
{
	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
	    zoneid, NULL);
}

void
vprintf(const char *fmt, va_list adx)
{
	vzprintf(GLOBAL_ZONEID, fmt, adx);
}

/*PRINTFLIKE1*/
void
printf(const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
	    GLOBAL_ZONEID, NULL);
	va_end(adx);
}

/*PRINTFLIKE2*/
void
zprintf(zoneid_t zoneid, const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
	    zoneid, NULL);
	va_end(adx);
}

void
vuprintf(const char *fmt, va_list adx)
{
	va_list adxcp;
	va_copy(adxcp, adx);

	/* Message the user tty, if any, and the global zone syslog */
	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID, NULL);

	/* Now message the local zone syslog */
	if (!INGLOBALZONE(curproc))
		cprintf(fmt, adxcp, SL_CONSOLE | SL_LOGONLY | SL_NOTE,
		    "", "", caller(), 0, 0, 0, getzoneid(), NULL);

	va_end(adxcp);
}

/*PRINTFLIKE1*/
void
uprintf(const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);

	vuprintf(fmt, adx);

	va_end(adx);
}

static void
vzdcmn_err(zoneid_t zoneid, void *site, int ce, const char *fmt, va_list adx,
    dev_info_t *dip)
{
	if (ce == CE_PANIC)
		vpanic(fmt, adx);
	if ((uint_t)ce < CE_IGNORE) {
		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
		    ce_prefix[ce], ce_suffix[ce], site, 0, 0, 0,
		    zoneid, dip);
	}
}

void
vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
{
	vzdcmn_err(zoneid, caller(), ce, fmt, adx, NULL);
}

void
vcmn_err(int ce, const char *fmt, va_list adx)
{
	vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, NULL);
}

/*PRINTFLIKE3*/
void
vdev_err(dev_info_t *dip, int ce, const char *fmt, va_list adx)
{
	vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, dip);
}

/*PRINTFLIKE2*/
void
cmn_err(int ce, const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, NULL);
	va_end(adx);
}

/*PRINTFLIKE3*/
void
zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	vzdcmn_err(zoneid, caller(), ce, fmt, adx, NULL);
	va_end(adx);
}

/*PRINTFLIKE3*/
void
dev_err(dev_info_t *dip, int ce, char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, dip);
	va_end(adx);
}

int
assfail(const char *a, const char *f, int l)
{
	if (aask)  {
		printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
		debug_enter(NULL);
	}

	if (!aok && !panicstr)
		panic("assertion failed: %s, file: %s, line: %d", a, f, l);

	return (0);
}

void
assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
    const char *f, int l)
{
	if (aask)  {
		printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
		    f, l);
		debug_enter(NULL);
	}

	if (!aok && !panicstr)
		panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
		    f, l);
}

int
strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
{
	if (sl & log_global.lz_active) {
		va_list adx;
		va_start(adx, fmt);
		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
		    GLOBAL_ZONEID, NULL);
		va_end(adx);
	}
	return (1);
}

int
vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
{
	if (sl & log_global.lz_active)
		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
		    GLOBAL_ZONEID, NULL);
	return (1);
}