xref: /illumos-gate/usr/src/lib/libumem/common/misc.c (revision 9e4065d1d9bb5bf9273afe60b75b08315d99d542)
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
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * 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 /*
237257d1b4Sraf  * 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 #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <dlfcn.h>
297c478bd9Sstevel@tonic-gate #include <signal.h>
307c478bd9Sstevel@tonic-gate #include <stdarg.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <umem_impl.h>
377c478bd9Sstevel@tonic-gate #include "misc.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #define	UMEM_ERRFD	2	/* goes to standard error */
407c478bd9Sstevel@tonic-gate #define	UMEM_MAX_ERROR_SIZE 4096 /* error messages are truncated to this */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * This is a circular buffer for holding error messages.
447c478bd9Sstevel@tonic-gate  * umem_error_enter appends to the buffer, adding "..." to the beginning
457c478bd9Sstevel@tonic-gate  * if data has been lost.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	ERR_SIZE 8192		/* must be a power of 2 */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static mutex_t umem_error_lock = DEFAULTMUTEX;
517c478bd9Sstevel@tonic-gate 
52a73ed278SToomas Soome char umem_error_buffer[ERR_SIZE] = "";
53a73ed278SToomas Soome uint_t umem_error_begin = 0;
54a73ed278SToomas Soome uint_t umem_error_end = 0;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	WRITE_AND_INC(var, value) { \
577c478bd9Sstevel@tonic-gate 	umem_error_buffer[(var)++] = (value); \
587c478bd9Sstevel@tonic-gate 	var = P2PHASE((var), ERR_SIZE); \
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static void
log_enter(const char * error_str)62*9e4065d1SRichard Lowe log_enter(const char *error_str)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	int looped;
657c478bd9Sstevel@tonic-gate 	char c;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	looped = 0;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&umem_error_lock);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	while ((c = *error_str++) != '\0') {
727c478bd9Sstevel@tonic-gate 		WRITE_AND_INC(umem_error_end, c);
737c478bd9Sstevel@tonic-gate 		if (umem_error_end == umem_error_begin)
747c478bd9Sstevel@tonic-gate 			looped = 1;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	umem_error_buffer[umem_error_end] = 0;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (looped) {
807c478bd9Sstevel@tonic-gate 		uint_t idx;
817c478bd9Sstevel@tonic-gate 		umem_error_begin = P2PHASE(umem_error_end + 1, ERR_SIZE);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		idx = umem_error_begin;
847c478bd9Sstevel@tonic-gate 		WRITE_AND_INC(idx, '.');
857c478bd9Sstevel@tonic-gate 		WRITE_AND_INC(idx, '.');
867c478bd9Sstevel@tonic-gate 		WRITE_AND_INC(idx, '.');
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&umem_error_lock);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate void
umem_error_enter(const char * error_str)937c478bd9Sstevel@tonic-gate umem_error_enter(const char *error_str)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
967c478bd9Sstevel@tonic-gate 	if (umem_output && !issetugid())
977c478bd9Sstevel@tonic-gate 		(void) write(UMEM_ERRFD, error_str, strlen(error_str));
987c478bd9Sstevel@tonic-gate #endif
997c478bd9Sstevel@tonic-gate 
100*9e4065d1SRichard Lowe 	log_enter(error_str);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
highbit(ulong_t i)1047c478bd9Sstevel@tonic-gate highbit(ulong_t i)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	register int h = 1;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (i == 0)
1097c478bd9Sstevel@tonic-gate 		return (0);
1107c478bd9Sstevel@tonic-gate #ifdef _LP64
1117c478bd9Sstevel@tonic-gate 	if (i & 0xffffffff00000000ul) {
1127c478bd9Sstevel@tonic-gate 		h += 32; i >>= 32;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 	if (i & 0xffff0000) {
1167c478bd9Sstevel@tonic-gate 		h += 16; i >>= 16;
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	if (i & 0xff00) {
1197c478bd9Sstevel@tonic-gate 		h += 8; i >>= 8;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	if (i & 0xf0) {
1227c478bd9Sstevel@tonic-gate 		h += 4; i >>= 4;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	if (i & 0xc) {
1257c478bd9Sstevel@tonic-gate 		h += 2; i >>= 2;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	if (i & 0x2) {
1287c478bd9Sstevel@tonic-gate 		h += 1;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	return (h);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
lowbit(ulong_t i)1347c478bd9Sstevel@tonic-gate lowbit(ulong_t i)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	register int h = 1;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (i == 0)
1397c478bd9Sstevel@tonic-gate 		return (0);
1407c478bd9Sstevel@tonic-gate #ifdef _LP64
1417c478bd9Sstevel@tonic-gate 	if (!(i & 0xffffffff)) {
1427c478bd9Sstevel@tonic-gate 		h += 32; i >>= 32;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate 	if (!(i & 0xffff)) {
1467c478bd9Sstevel@tonic-gate 		h += 16; i >>= 16;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	if (!(i & 0xff)) {
1497c478bd9Sstevel@tonic-gate 		h += 8; i >>= 8;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	if (!(i & 0xf)) {
1527c478bd9Sstevel@tonic-gate 		h += 4; i >>= 4;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	if (!(i & 0x3)) {
1557c478bd9Sstevel@tonic-gate 		h += 2; i >>= 2;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	if (!(i & 0x1)) {
1587c478bd9Sstevel@tonic-gate 		h += 1;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return (h);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate void
hrt2ts(hrtime_t hrt,timestruc_t * tsp)1647c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	tsp->tv_sec = hrt / NANOSEC;
1677c478bd9Sstevel@tonic-gate 	tsp->tv_nsec = hrt % NANOSEC;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate void
log_message(const char * format,...)1717c478bd9Sstevel@tonic-gate log_message(const char *format, ...)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	char buf[UMEM_MAX_ERROR_SIZE] = "";
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	va_list va;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	va_start(va, format);
1787c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
1797c478bd9Sstevel@tonic-gate 	va_end(va);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1827c478bd9Sstevel@tonic-gate 	if (umem_output > 1)
1837c478bd9Sstevel@tonic-gate 		(void) write(UMEM_ERRFD, buf, strlen(buf));
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 
186*9e4065d1SRichard Lowe 	log_enter(buf);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1907c478bd9Sstevel@tonic-gate void
debug_printf(const char * format,...)1917c478bd9Sstevel@tonic-gate debug_printf(const char *format, ...)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	char buf[UMEM_MAX_ERROR_SIZE] = "";
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	va_list va;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	va_start(va, format);
1987c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
1997c478bd9Sstevel@tonic-gate 	va_end(va);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	(void) write(UMEM_ERRFD, buf, strlen(buf));
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate void
umem_vprintf(const char * format,va_list va)2067c478bd9Sstevel@tonic-gate umem_vprintf(const char *format, va_list va)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	char buf[UMEM_MAX_ERROR_SIZE] = "";
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	umem_error_enter(buf);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate void
umem_printf(const char * format,...)2167c478bd9Sstevel@tonic-gate umem_printf(const char *format, ...)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	va_list va;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	va_start(va, format);
2217c478bd9Sstevel@tonic-gate 	umem_vprintf(format, va);
2227c478bd9Sstevel@tonic-gate 	va_end(va);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2267c478bd9Sstevel@tonic-gate void
umem_printf_warn(void * ignored,const char * format,...)2277c478bd9Sstevel@tonic-gate umem_printf_warn(void *ignored, const char *format, ...)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	va_list va;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	va_start(va, format);
2327c478bd9Sstevel@tonic-gate 	umem_vprintf(format, va);
2337c478bd9Sstevel@tonic-gate 	va_end(va);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * print_sym tries to print out the symbol and offset of a pointer
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate int
print_sym(void * pointer)2407c478bd9Sstevel@tonic-gate print_sym(void *pointer)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	int result;
2437c478bd9Sstevel@tonic-gate 	Dl_info sym_info;
2447c478bd9Sstevel@tonic-gate 
245a6313a9dSToomas Soome 	uintptr_t end = (uintptr_t)NULL;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	Sym *ext_info = NULL;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	result = dladdr1(pointer, &sym_info, (void **)&ext_info,
2507c478bd9Sstevel@tonic-gate 	    RTLD_DL_SYMENT);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (result != 0) {
2537c478bd9Sstevel@tonic-gate 		const char *endpath;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		end = (uintptr_t)sym_info.dli_saddr + ext_info->st_size;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		endpath = strrchr(sym_info.dli_fname, '/');
2587c478bd9Sstevel@tonic-gate 		if (endpath)
2597c478bd9Sstevel@tonic-gate 			endpath++;
2607c478bd9Sstevel@tonic-gate 		else
2617c478bd9Sstevel@tonic-gate 			endpath = sym_info.dli_fname;
2627c478bd9Sstevel@tonic-gate 		umem_printf("%s'", endpath);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (result == 0 || (uintptr_t)pointer > end) {
2667c478bd9Sstevel@tonic-gate 		umem_printf("?? (0x%p)", pointer);
2677c478bd9Sstevel@tonic-gate 		return (0);
2687c478bd9Sstevel@tonic-gate 	} else {
2697c478bd9Sstevel@tonic-gate 		umem_printf("%s+0x%p", sym_info.dli_sname,
2707c478bd9Sstevel@tonic-gate 		    (char *)pointer - (char *)sym_info.dli_saddr);
2717c478bd9Sstevel@tonic-gate 		return (1);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate }
274