1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "mtlib.h"
30*0Sstevel@tonic-gate #include <unistd.h>
31*0Sstevel@tonic-gate #include <dlfcn.h>
32*0Sstevel@tonic-gate #include <signal.h>
33*0Sstevel@tonic-gate #include <stdarg.h>
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <sys/machelf.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <umem_impl.h>
40*0Sstevel@tonic-gate #include "misc.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #define	UMEM_ERRFD	2	/* goes to standard error */
43*0Sstevel@tonic-gate #define	UMEM_MAX_ERROR_SIZE 4096 /* error messages are truncated to this */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * This is a circular buffer for holding error messages.
47*0Sstevel@tonic-gate  * umem_error_enter appends to the buffer, adding "..." to the beginning
48*0Sstevel@tonic-gate  * if data has been lost.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #define	ERR_SIZE 8192		/* must be a power of 2 */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static mutex_t umem_error_lock = DEFAULTMUTEX;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static char umem_error_buffer[ERR_SIZE] = "";
56*0Sstevel@tonic-gate static uint_t umem_error_begin = 0;
57*0Sstevel@tonic-gate static uint_t umem_error_end = 0;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #define	WRITE_AND_INC(var, value) { \
60*0Sstevel@tonic-gate 	umem_error_buffer[(var)++] = (value); \
61*0Sstevel@tonic-gate 	var = P2PHASE((var), ERR_SIZE); \
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static void
65*0Sstevel@tonic-gate umem_log_enter(const char *error_str)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	int looped;
68*0Sstevel@tonic-gate 	char c;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	looped = 0;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	(void) mutex_lock(&umem_error_lock);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	while ((c = *error_str++) != '\0') {
75*0Sstevel@tonic-gate 		WRITE_AND_INC(umem_error_end, c);
76*0Sstevel@tonic-gate 		if (umem_error_end == umem_error_begin)
77*0Sstevel@tonic-gate 			looped = 1;
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	umem_error_buffer[umem_error_end] = 0;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	if (looped) {
83*0Sstevel@tonic-gate 		uint_t idx;
84*0Sstevel@tonic-gate 		umem_error_begin = P2PHASE(umem_error_end + 1, ERR_SIZE);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 		idx = umem_error_begin;
87*0Sstevel@tonic-gate 		WRITE_AND_INC(idx, '.');
88*0Sstevel@tonic-gate 		WRITE_AND_INC(idx, '.');
89*0Sstevel@tonic-gate 		WRITE_AND_INC(idx, '.');
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	(void) mutex_unlock(&umem_error_lock);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate void
96*0Sstevel@tonic-gate umem_error_enter(const char *error_str)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate #ifndef UMEM_STANDALONE
99*0Sstevel@tonic-gate 	if (umem_output && !issetugid())
100*0Sstevel@tonic-gate 		(void) write(UMEM_ERRFD, error_str, strlen(error_str));
101*0Sstevel@tonic-gate #endif
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	umem_log_enter(error_str);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate int
107*0Sstevel@tonic-gate highbit(ulong_t i)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	register int h = 1;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (i == 0)
112*0Sstevel@tonic-gate 		return (0);
113*0Sstevel@tonic-gate #ifdef _LP64
114*0Sstevel@tonic-gate 	if (i & 0xffffffff00000000ul) {
115*0Sstevel@tonic-gate 		h += 32; i >>= 32;
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate 	if (i & 0xffff0000) {
119*0Sstevel@tonic-gate 		h += 16; i >>= 16;
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 	if (i & 0xff00) {
122*0Sstevel@tonic-gate 		h += 8; i >>= 8;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	if (i & 0xf0) {
125*0Sstevel@tonic-gate 		h += 4; i >>= 4;
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	if (i & 0xc) {
128*0Sstevel@tonic-gate 		h += 2; i >>= 2;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 	if (i & 0x2) {
131*0Sstevel@tonic-gate 		h += 1;
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 	return (h);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate int
137*0Sstevel@tonic-gate lowbit(ulong_t i)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	register int h = 1;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (i == 0)
142*0Sstevel@tonic-gate 		return (0);
143*0Sstevel@tonic-gate #ifdef _LP64
144*0Sstevel@tonic-gate 	if (!(i & 0xffffffff)) {
145*0Sstevel@tonic-gate 		h += 32; i >>= 32;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate #endif
148*0Sstevel@tonic-gate 	if (!(i & 0xffff)) {
149*0Sstevel@tonic-gate 		h += 16; i >>= 16;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	if (!(i & 0xff)) {
152*0Sstevel@tonic-gate 		h += 8; i >>= 8;
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 	if (!(i & 0xf)) {
155*0Sstevel@tonic-gate 		h += 4; i >>= 4;
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 	if (!(i & 0x3)) {
158*0Sstevel@tonic-gate 		h += 2; i >>= 2;
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	if (!(i & 0x1)) {
161*0Sstevel@tonic-gate 		h += 1;
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	return (h);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate void
167*0Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	tsp->tv_sec = hrt / NANOSEC;
170*0Sstevel@tonic-gate 	tsp->tv_nsec = hrt % NANOSEC;
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate void
174*0Sstevel@tonic-gate log_message(const char *format, ...)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	char buf[UMEM_MAX_ERROR_SIZE] = "";
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	va_list va;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	va_start(va, format);
181*0Sstevel@tonic-gate 	(void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
182*0Sstevel@tonic-gate 	va_end(va);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate #ifndef UMEM_STANDALONE
185*0Sstevel@tonic-gate 	if (umem_output > 1)
186*0Sstevel@tonic-gate 		(void) write(UMEM_ERRFD, buf, strlen(buf));
187*0Sstevel@tonic-gate #endif
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	umem_log_enter(buf);
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate #ifndef UMEM_STANDALONE
193*0Sstevel@tonic-gate void
194*0Sstevel@tonic-gate debug_printf(const char *format, ...)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	char buf[UMEM_MAX_ERROR_SIZE] = "";
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	va_list va;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	va_start(va, format);
201*0Sstevel@tonic-gate 	(void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
202*0Sstevel@tonic-gate 	va_end(va);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	(void) write(UMEM_ERRFD, buf, strlen(buf));
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate #endif
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate void
209*0Sstevel@tonic-gate umem_vprintf(const char *format, va_list va)
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate 	char buf[UMEM_MAX_ERROR_SIZE] = "";
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	(void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	umem_error_enter(buf);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate void
219*0Sstevel@tonic-gate umem_printf(const char *format, ...)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	va_list va;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	va_start(va, format);
224*0Sstevel@tonic-gate 	umem_vprintf(format, va);
225*0Sstevel@tonic-gate 	va_end(va);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate /*ARGSUSED*/
229*0Sstevel@tonic-gate void
230*0Sstevel@tonic-gate umem_printf_warn(void *ignored, const char *format, ...)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	va_list va;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	va_start(va, format);
235*0Sstevel@tonic-gate 	umem_vprintf(format, va);
236*0Sstevel@tonic-gate 	va_end(va);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate  * print_sym tries to print out the symbol and offset of a pointer
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate int
243*0Sstevel@tonic-gate print_sym(void *pointer)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	int result;
246*0Sstevel@tonic-gate 	Dl_info sym_info;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	uintptr_t end = NULL;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	Sym *ext_info = NULL;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	result = dladdr1(pointer, &sym_info, (void **)&ext_info,
253*0Sstevel@tonic-gate 	    RTLD_DL_SYMENT);
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	if (result != 0) {
256*0Sstevel@tonic-gate 		const char *endpath;
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 		end = (uintptr_t)sym_info.dli_saddr + ext_info->st_size;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		endpath = strrchr(sym_info.dli_fname, '/');
261*0Sstevel@tonic-gate 		if (endpath)
262*0Sstevel@tonic-gate 			endpath++;
263*0Sstevel@tonic-gate 		else
264*0Sstevel@tonic-gate 			endpath = sym_info.dli_fname;
265*0Sstevel@tonic-gate 		umem_printf("%s'", endpath);
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	if (result == 0 || (uintptr_t)pointer > end) {
269*0Sstevel@tonic-gate 		umem_printf("?? (0x%p)", pointer);
270*0Sstevel@tonic-gate 		return (0);
271*0Sstevel@tonic-gate 	} else {
272*0Sstevel@tonic-gate 		umem_printf("%s+0x%p", sym_info.dli_sname,
273*0Sstevel@tonic-gate 		    (char *)pointer - (char *)sym_info.dli_saddr);
274*0Sstevel@tonic-gate 		return (1);
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate }
277