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