4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
36 #include <sys/machelf.h>
38 #include <umem_impl.h>
41 #define UMEM_ERRFD 2 /* goes to standard error */
42 #define UMEM_MAX_ERROR_SIZE 4096 /* error messages are truncated to this */
45 * This is a circular buffer for holding error messages.
46 * umem_error_enter appends to the buffer, adding "..." to the beginning
47 * if data has been lost.
50 #define ERR_SIZE 8192 /* must be a power of 2 */
52 static mutex_t umem_error_lock = DEFAULTMUTEX;
54 static char umem_error_buffer[ERR_SIZE] = "";
55 static uint_t umem_error_begin = 0;
56 static uint_t umem_error_end = 0;
58 #define WRITE_AND_INC(var, value) { \
59 umem_error_buffer[(var)++] = (value); \
60 var = P2PHASE((var), ERR_SIZE); \
64 umem_log_enter(const char *error_str)
71 (void) mutex_lock(&umem_error_lock);
73 while ((c = *error_str++) != '\0') {
74 WRITE_AND_INC(umem_error_end, c);
75 if (umem_error_end == umem_error_begin)
79 umem_error_buffer[umem_error_end] = 0;
83 umem_error_begin = P2PHASE(umem_error_end + 1, ERR_SIZE);
85 idx = umem_error_begin;
86 WRITE_AND_INC(idx, '.');
87 WRITE_AND_INC(idx, '.');
88 WRITE_AND_INC(idx, '.');
91 (void) mutex_unlock(&umem_error_lock);
95 umem_error_enter(const char *error_str)
97 #ifndef UMEM_STANDALONE
98 if (umem_output && !issetugid())
99 (void) write(UMEM_ERRFD, error_str, strlen(error_str));
102 umem_log_enter(error_str);
113 if (i & 0xffffffff00000000ul) {
117 if (i & 0xffff0000) {
143 if (!(i & 0xffffffff)) {
166 hrt2ts(hrtime_t hrt, timestruc_t *tsp)
168 tsp->tv_sec = hrt / NANOSEC;
169 tsp->tv_nsec = hrt % NANOSEC;
173 log_message(const char *format, ...)
175 char buf[UMEM_MAX_ERROR_SIZE] = "";
179 va_start(va, format);
180 (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
183 #ifndef UMEM_STANDALONE
185 (void) write(UMEM_ERRFD, buf, strlen(buf));
191 #ifndef UMEM_STANDALONE
193 debug_printf(const char *format, ...)
195 char buf[UMEM_MAX_ERROR_SIZE] = "";
199 va_start(va, format);
200 (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
203 (void) write(UMEM_ERRFD, buf, strlen(buf));
208 umem_vprintf(const char *format, va_list va)
210 char buf[UMEM_MAX_ERROR_SIZE] = "";
212 (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
214 umem_error_enter(buf);
218 umem_printf(const char *format, ...)
222 va_start(va, format);
223 umem_vprintf(format, va);
229 umem_printf_warn(void *ignored, const char *format, ...)
233 va_start(va, format);
234 umem_vprintf(format, va);
239 * print_sym tries to print out the symbol and offset of a pointer
242 print_sym(void *pointer)
247 uintptr_t end = NULL;
249 Sym *ext_info = NULL;
251 result = dladdr1(pointer, &sym_info, (void **)&ext_info,
257 end = (uintptr_t)sym_info.dli_saddr + ext_info->st_size;
259 endpath = strrchr(sym_info.dli_fname, '/');
263 endpath = sym_info.dli_fname;
264 umem_printf("%s'", endpath);
267 if (result == 0 || (uintptr_t)pointer > end) {
268 umem_printf("?? (0x%p)", pointer);
271 umem_printf("%s+0x%p", sym_info.dli_sname,
272 (char *)pointer - (char *)sym_info.dli_saddr);