17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7257d1b4Sraf * Common Development and Distribution License (the "License"). 6*7257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21e8031f0aSraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <unistd.h> 307c478bd9Sstevel@tonic-gate #include <dlfcn.h> 317c478bd9Sstevel@tonic-gate #include <signal.h> 327c478bd9Sstevel@tonic-gate #include <stdarg.h> 337c478bd9Sstevel@tonic-gate #include <stdio.h> 347c478bd9Sstevel@tonic-gate #include <string.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <sys/machelf.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <umem_impl.h> 397c478bd9Sstevel@tonic-gate #include "misc.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #define UMEM_ERRFD 2 /* goes to standard error */ 427c478bd9Sstevel@tonic-gate #define UMEM_MAX_ERROR_SIZE 4096 /* error messages are truncated to this */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * This is a circular buffer for holding error messages. 467c478bd9Sstevel@tonic-gate * umem_error_enter appends to the buffer, adding "..." to the beginning 477c478bd9Sstevel@tonic-gate * if data has been lost. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #define ERR_SIZE 8192 /* must be a power of 2 */ 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static mutex_t umem_error_lock = DEFAULTMUTEX; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate static char umem_error_buffer[ERR_SIZE] = ""; 557c478bd9Sstevel@tonic-gate static uint_t umem_error_begin = 0; 567c478bd9Sstevel@tonic-gate static uint_t umem_error_end = 0; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #define WRITE_AND_INC(var, value) { \ 597c478bd9Sstevel@tonic-gate umem_error_buffer[(var)++] = (value); \ 607c478bd9Sstevel@tonic-gate var = P2PHASE((var), ERR_SIZE); \ 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate static void 647c478bd9Sstevel@tonic-gate umem_log_enter(const char *error_str) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate int looped; 677c478bd9Sstevel@tonic-gate char c; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate looped = 0; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate (void) mutex_lock(&umem_error_lock); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate while ((c = *error_str++) != '\0') { 747c478bd9Sstevel@tonic-gate WRITE_AND_INC(umem_error_end, c); 757c478bd9Sstevel@tonic-gate if (umem_error_end == umem_error_begin) 767c478bd9Sstevel@tonic-gate looped = 1; 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate umem_error_buffer[umem_error_end] = 0; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (looped) { 827c478bd9Sstevel@tonic-gate uint_t idx; 837c478bd9Sstevel@tonic-gate umem_error_begin = P2PHASE(umem_error_end + 1, ERR_SIZE); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate idx = umem_error_begin; 867c478bd9Sstevel@tonic-gate WRITE_AND_INC(idx, '.'); 877c478bd9Sstevel@tonic-gate WRITE_AND_INC(idx, '.'); 887c478bd9Sstevel@tonic-gate WRITE_AND_INC(idx, '.'); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate (void) mutex_unlock(&umem_error_lock); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate void 957c478bd9Sstevel@tonic-gate umem_error_enter(const char *error_str) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 987c478bd9Sstevel@tonic-gate if (umem_output && !issetugid()) 997c478bd9Sstevel@tonic-gate (void) write(UMEM_ERRFD, error_str, strlen(error_str)); 1007c478bd9Sstevel@tonic-gate #endif 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate umem_log_enter(error_str); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate int 1067c478bd9Sstevel@tonic-gate highbit(ulong_t i) 1077c478bd9Sstevel@tonic-gate { 1087c478bd9Sstevel@tonic-gate register int h = 1; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (i == 0) 1117c478bd9Sstevel@tonic-gate return (0); 1127c478bd9Sstevel@tonic-gate #ifdef _LP64 1137c478bd9Sstevel@tonic-gate if (i & 0xffffffff00000000ul) { 1147c478bd9Sstevel@tonic-gate h += 32; i >>= 32; 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate #endif 1177c478bd9Sstevel@tonic-gate if (i & 0xffff0000) { 1187c478bd9Sstevel@tonic-gate h += 16; i >>= 16; 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate if (i & 0xff00) { 1217c478bd9Sstevel@tonic-gate h += 8; i >>= 8; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate if (i & 0xf0) { 1247c478bd9Sstevel@tonic-gate h += 4; i >>= 4; 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate if (i & 0xc) { 1277c478bd9Sstevel@tonic-gate h += 2; i >>= 2; 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate if (i & 0x2) { 1307c478bd9Sstevel@tonic-gate h += 1; 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate return (h); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate int 1367c478bd9Sstevel@tonic-gate lowbit(ulong_t i) 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate register int h = 1; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (i == 0) 1417c478bd9Sstevel@tonic-gate return (0); 1427c478bd9Sstevel@tonic-gate #ifdef _LP64 1437c478bd9Sstevel@tonic-gate if (!(i & 0xffffffff)) { 1447c478bd9Sstevel@tonic-gate h += 32; i >>= 32; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate #endif 1477c478bd9Sstevel@tonic-gate if (!(i & 0xffff)) { 1487c478bd9Sstevel@tonic-gate h += 16; i >>= 16; 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate if (!(i & 0xff)) { 1517c478bd9Sstevel@tonic-gate h += 8; i >>= 8; 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate if (!(i & 0xf)) { 1547c478bd9Sstevel@tonic-gate h += 4; i >>= 4; 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate if (!(i & 0x3)) { 1577c478bd9Sstevel@tonic-gate h += 2; i >>= 2; 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate if (!(i & 0x1)) { 1607c478bd9Sstevel@tonic-gate h += 1; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate return (h); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate void 1667c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp) 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate tsp->tv_sec = hrt / NANOSEC; 1697c478bd9Sstevel@tonic-gate tsp->tv_nsec = hrt % NANOSEC; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate void 1737c478bd9Sstevel@tonic-gate log_message(const char *format, ...) 1747c478bd9Sstevel@tonic-gate { 1757c478bd9Sstevel@tonic-gate char buf[UMEM_MAX_ERROR_SIZE] = ""; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate va_list va; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate va_start(va, format); 1807c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va); 1817c478bd9Sstevel@tonic-gate va_end(va); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 1847c478bd9Sstevel@tonic-gate if (umem_output > 1) 1857c478bd9Sstevel@tonic-gate (void) write(UMEM_ERRFD, buf, strlen(buf)); 1867c478bd9Sstevel@tonic-gate #endif 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate umem_log_enter(buf); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 1927c478bd9Sstevel@tonic-gate void 1937c478bd9Sstevel@tonic-gate debug_printf(const char *format, ...) 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate char buf[UMEM_MAX_ERROR_SIZE] = ""; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate va_list va; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate va_start(va, format); 2007c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va); 2017c478bd9Sstevel@tonic-gate va_end(va); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate (void) write(UMEM_ERRFD, buf, strlen(buf)); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate #endif 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate void 2087c478bd9Sstevel@tonic-gate umem_vprintf(const char *format, va_list va) 2097c478bd9Sstevel@tonic-gate { 2107c478bd9Sstevel@tonic-gate char buf[UMEM_MAX_ERROR_SIZE] = ""; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate umem_error_enter(buf); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate void 2187c478bd9Sstevel@tonic-gate umem_printf(const char *format, ...) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate va_list va; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate va_start(va, format); 2237c478bd9Sstevel@tonic-gate umem_vprintf(format, va); 2247c478bd9Sstevel@tonic-gate va_end(va); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2287c478bd9Sstevel@tonic-gate void 2297c478bd9Sstevel@tonic-gate umem_printf_warn(void *ignored, const char *format, ...) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate va_list va; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate va_start(va, format); 2347c478bd9Sstevel@tonic-gate umem_vprintf(format, va); 2357c478bd9Sstevel@tonic-gate va_end(va); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * print_sym tries to print out the symbol and offset of a pointer 2407c478bd9Sstevel@tonic-gate */ 2417c478bd9Sstevel@tonic-gate int 2427c478bd9Sstevel@tonic-gate print_sym(void *pointer) 2437c478bd9Sstevel@tonic-gate { 2447c478bd9Sstevel@tonic-gate int result; 2457c478bd9Sstevel@tonic-gate Dl_info sym_info; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate uintptr_t end = NULL; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate Sym *ext_info = NULL; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate result = dladdr1(pointer, &sym_info, (void **)&ext_info, 2527c478bd9Sstevel@tonic-gate RTLD_DL_SYMENT); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (result != 0) { 2557c478bd9Sstevel@tonic-gate const char *endpath; 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate end = (uintptr_t)sym_info.dli_saddr + ext_info->st_size; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate endpath = strrchr(sym_info.dli_fname, '/'); 2607c478bd9Sstevel@tonic-gate if (endpath) 2617c478bd9Sstevel@tonic-gate endpath++; 2627c478bd9Sstevel@tonic-gate else 2637c478bd9Sstevel@tonic-gate endpath = sym_info.dli_fname; 2647c478bd9Sstevel@tonic-gate umem_printf("%s'", endpath); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate if (result == 0 || (uintptr_t)pointer > end) { 2687c478bd9Sstevel@tonic-gate umem_printf("?? (0x%p)", pointer); 2697c478bd9Sstevel@tonic-gate return (0); 2707c478bd9Sstevel@tonic-gate } else { 2717c478bd9Sstevel@tonic-gate umem_printf("%s+0x%p", sym_info.dli_sname, 2727c478bd9Sstevel@tonic-gate (char *)pointer - (char *)sym_info.dli_saddr); 2737c478bd9Sstevel@tonic-gate return (1); 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate } 276