10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211219Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <dlfcn.h>
310Sstevel@tonic-gate #include <signal.h>
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/machelf.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <umem_impl.h>
390Sstevel@tonic-gate #include "misc.h"
400Sstevel@tonic-gate
410Sstevel@tonic-gate #define UMEM_ERRFD 2 /* goes to standard error */
420Sstevel@tonic-gate #define UMEM_MAX_ERROR_SIZE 4096 /* error messages are truncated to this */
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * This is a circular buffer for holding error messages.
460Sstevel@tonic-gate * umem_error_enter appends to the buffer, adding "..." to the beginning
470Sstevel@tonic-gate * if data has been lost.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate #define ERR_SIZE 8192 /* must be a power of 2 */
510Sstevel@tonic-gate
520Sstevel@tonic-gate static mutex_t umem_error_lock = DEFAULTMUTEX;
530Sstevel@tonic-gate
540Sstevel@tonic-gate static char umem_error_buffer[ERR_SIZE] = "";
550Sstevel@tonic-gate static uint_t umem_error_begin = 0;
560Sstevel@tonic-gate static uint_t umem_error_end = 0;
570Sstevel@tonic-gate
580Sstevel@tonic-gate #define WRITE_AND_INC(var, value) { \
590Sstevel@tonic-gate umem_error_buffer[(var)++] = (value); \
600Sstevel@tonic-gate var = P2PHASE((var), ERR_SIZE); \
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate static void
umem_log_enter(const char * error_str)640Sstevel@tonic-gate umem_log_enter(const char *error_str)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate int looped;
670Sstevel@tonic-gate char c;
680Sstevel@tonic-gate
690Sstevel@tonic-gate looped = 0;
700Sstevel@tonic-gate
710Sstevel@tonic-gate (void) mutex_lock(&umem_error_lock);
720Sstevel@tonic-gate
730Sstevel@tonic-gate while ((c = *error_str++) != '\0') {
740Sstevel@tonic-gate WRITE_AND_INC(umem_error_end, c);
750Sstevel@tonic-gate if (umem_error_end == umem_error_begin)
760Sstevel@tonic-gate looped = 1;
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate umem_error_buffer[umem_error_end] = 0;
800Sstevel@tonic-gate
810Sstevel@tonic-gate if (looped) {
820Sstevel@tonic-gate uint_t idx;
830Sstevel@tonic-gate umem_error_begin = P2PHASE(umem_error_end + 1, ERR_SIZE);
840Sstevel@tonic-gate
850Sstevel@tonic-gate idx = umem_error_begin;
860Sstevel@tonic-gate WRITE_AND_INC(idx, '.');
870Sstevel@tonic-gate WRITE_AND_INC(idx, '.');
880Sstevel@tonic-gate WRITE_AND_INC(idx, '.');
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate (void) mutex_unlock(&umem_error_lock);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate void
umem_error_enter(const char * error_str)950Sstevel@tonic-gate umem_error_enter(const char *error_str)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate #ifndef UMEM_STANDALONE
980Sstevel@tonic-gate if (umem_output && !issetugid())
990Sstevel@tonic-gate (void) write(UMEM_ERRFD, error_str, strlen(error_str));
1000Sstevel@tonic-gate #endif
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate umem_log_enter(error_str);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate int
highbit(ulong_t i)1060Sstevel@tonic-gate highbit(ulong_t i)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate register int h = 1;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (i == 0)
1110Sstevel@tonic-gate return (0);
1120Sstevel@tonic-gate #ifdef _LP64
1130Sstevel@tonic-gate if (i & 0xffffffff00000000ul) {
1140Sstevel@tonic-gate h += 32; i >>= 32;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate if (i & 0xffff0000) {
1180Sstevel@tonic-gate h += 16; i >>= 16;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate if (i & 0xff00) {
1210Sstevel@tonic-gate h += 8; i >>= 8;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate if (i & 0xf0) {
1240Sstevel@tonic-gate h += 4; i >>= 4;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate if (i & 0xc) {
1270Sstevel@tonic-gate h += 2; i >>= 2;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate if (i & 0x2) {
1300Sstevel@tonic-gate h += 1;
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate return (h);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate int
lowbit(ulong_t i)1360Sstevel@tonic-gate lowbit(ulong_t i)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate register int h = 1;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate if (i == 0)
1410Sstevel@tonic-gate return (0);
1420Sstevel@tonic-gate #ifdef _LP64
1430Sstevel@tonic-gate if (!(i & 0xffffffff)) {
1440Sstevel@tonic-gate h += 32; i >>= 32;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate #endif
1470Sstevel@tonic-gate if (!(i & 0xffff)) {
1480Sstevel@tonic-gate h += 16; i >>= 16;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate if (!(i & 0xff)) {
1510Sstevel@tonic-gate h += 8; i >>= 8;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate if (!(i & 0xf)) {
1540Sstevel@tonic-gate h += 4; i >>= 4;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate if (!(i & 0x3)) {
1570Sstevel@tonic-gate h += 2; i >>= 2;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate if (!(i & 0x1)) {
1600Sstevel@tonic-gate h += 1;
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate return (h);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate void
hrt2ts(hrtime_t hrt,timestruc_t * tsp)1660Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate tsp->tv_sec = hrt / NANOSEC;
1690Sstevel@tonic-gate tsp->tv_nsec = hrt % NANOSEC;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate void
log_message(const char * format,...)1730Sstevel@tonic-gate log_message(const char *format, ...)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate char buf[UMEM_MAX_ERROR_SIZE] = "";
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate va_list va;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate va_start(va, format);
1800Sstevel@tonic-gate (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
1810Sstevel@tonic-gate va_end(va);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1840Sstevel@tonic-gate if (umem_output > 1)
1850Sstevel@tonic-gate (void) write(UMEM_ERRFD, buf, strlen(buf));
1860Sstevel@tonic-gate #endif
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate umem_log_enter(buf);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1920Sstevel@tonic-gate void
debug_printf(const char * format,...)1930Sstevel@tonic-gate debug_printf(const char *format, ...)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate char buf[UMEM_MAX_ERROR_SIZE] = "";
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate va_list va;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate va_start(va, format);
2000Sstevel@tonic-gate (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
2010Sstevel@tonic-gate va_end(va);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate (void) write(UMEM_ERRFD, buf, strlen(buf));
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate void
umem_vprintf(const char * format,va_list va)2080Sstevel@tonic-gate umem_vprintf(const char *format, va_list va)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate char buf[UMEM_MAX_ERROR_SIZE] = "";
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate umem_error_enter(buf);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate void
umem_printf(const char * format,...)2180Sstevel@tonic-gate umem_printf(const char *format, ...)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate va_list va;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate va_start(va, format);
2230Sstevel@tonic-gate umem_vprintf(format, va);
2240Sstevel@tonic-gate va_end(va);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /*ARGSUSED*/
2280Sstevel@tonic-gate void
umem_printf_warn(void * ignored,const char * format,...)2290Sstevel@tonic-gate umem_printf_warn(void *ignored, const char *format, ...)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate va_list va;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate va_start(va, format);
2340Sstevel@tonic-gate umem_vprintf(format, va);
2350Sstevel@tonic-gate va_end(va);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate * print_sym tries to print out the symbol and offset of a pointer
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate int
print_sym(void * pointer)2420Sstevel@tonic-gate print_sym(void *pointer)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate int result;
2450Sstevel@tonic-gate Dl_info sym_info;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate uintptr_t end = NULL;
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate Sym *ext_info = NULL;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate result = dladdr1(pointer, &sym_info, (void **)&ext_info,
2520Sstevel@tonic-gate RTLD_DL_SYMENT);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (result != 0) {
2550Sstevel@tonic-gate const char *endpath;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate end = (uintptr_t)sym_info.dli_saddr + ext_info->st_size;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate endpath = strrchr(sym_info.dli_fname, '/');
2600Sstevel@tonic-gate if (endpath)
2610Sstevel@tonic-gate endpath++;
2620Sstevel@tonic-gate else
2630Sstevel@tonic-gate endpath = sym_info.dli_fname;
2640Sstevel@tonic-gate umem_printf("%s'", endpath);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (result == 0 || (uintptr_t)pointer > end) {
2680Sstevel@tonic-gate umem_printf("?? (0x%p)", pointer);
2690Sstevel@tonic-gate return (0);
2700Sstevel@tonic-gate } else {
2710Sstevel@tonic-gate umem_printf("%s+0x%p", sym_info.dli_sname,
2720Sstevel@tonic-gate (char *)pointer - (char *)sym_info.dli_saddr);
2730Sstevel@tonic-gate return (1);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate }
276