8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libumem / common / misc.c
bloba3da9e5b05f9f9bbe284d21a169238e602466314
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <unistd.h>
30 #include <dlfcn.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
36 #include <sys/machelf.h>
38 #include <umem_impl.h>
39 #include "misc.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); \
63 static void
64 umem_log_enter(const char *error_str)
66 int looped;
67 char c;
69 looped = 0;
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)
76 looped = 1;
79 umem_error_buffer[umem_error_end] = 0;
81 if (looped) {
82 uint_t idx;
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);
94 void
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));
100 #endif
102 umem_log_enter(error_str);
106 highbit(ulong_t i)
108 register int h = 1;
110 if (i == 0)
111 return (0);
112 #ifdef _LP64
113 if (i & 0xffffffff00000000ul) {
114 h += 32; i >>= 32;
116 #endif
117 if (i & 0xffff0000) {
118 h += 16; i >>= 16;
120 if (i & 0xff00) {
121 h += 8; i >>= 8;
123 if (i & 0xf0) {
124 h += 4; i >>= 4;
126 if (i & 0xc) {
127 h += 2; i >>= 2;
129 if (i & 0x2) {
130 h += 1;
132 return (h);
136 lowbit(ulong_t i)
138 register int h = 1;
140 if (i == 0)
141 return (0);
142 #ifdef _LP64
143 if (!(i & 0xffffffff)) {
144 h += 32; i >>= 32;
146 #endif
147 if (!(i & 0xffff)) {
148 h += 16; i >>= 16;
150 if (!(i & 0xff)) {
151 h += 8; i >>= 8;
153 if (!(i & 0xf)) {
154 h += 4; i >>= 4;
156 if (!(i & 0x3)) {
157 h += 2; i >>= 2;
159 if (!(i & 0x1)) {
160 h += 1;
162 return (h);
165 void
166 hrt2ts(hrtime_t hrt, timestruc_t *tsp)
168 tsp->tv_sec = hrt / NANOSEC;
169 tsp->tv_nsec = hrt % NANOSEC;
172 void
173 log_message(const char *format, ...)
175 char buf[UMEM_MAX_ERROR_SIZE] = "";
177 va_list va;
179 va_start(va, format);
180 (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
181 va_end(va);
183 #ifndef UMEM_STANDALONE
184 if (umem_output > 1)
185 (void) write(UMEM_ERRFD, buf, strlen(buf));
186 #endif
188 umem_log_enter(buf);
191 #ifndef UMEM_STANDALONE
192 void
193 debug_printf(const char *format, ...)
195 char buf[UMEM_MAX_ERROR_SIZE] = "";
197 va_list va;
199 va_start(va, format);
200 (void) vsnprintf(buf, UMEM_MAX_ERROR_SIZE-1, format, va);
201 va_end(va);
203 (void) write(UMEM_ERRFD, buf, strlen(buf));
205 #endif
207 void
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);
217 void
218 umem_printf(const char *format, ...)
220 va_list va;
222 va_start(va, format);
223 umem_vprintf(format, va);
224 va_end(va);
227 /*ARGSUSED*/
228 void
229 umem_printf_warn(void *ignored, const char *format, ...)
231 va_list va;
233 va_start(va, format);
234 umem_vprintf(format, va);
235 va_end(va);
239 * print_sym tries to print out the symbol and offset of a pointer
242 print_sym(void *pointer)
244 int result;
245 Dl_info sym_info;
247 uintptr_t end = NULL;
249 Sym *ext_info = NULL;
251 result = dladdr1(pointer, &sym_info, (void **)&ext_info,
252 RTLD_DL_SYMENT);
254 if (result != 0) {
255 const char *endpath;
257 end = (uintptr_t)sym_info.dli_saddr + ext_info->st_size;
259 endpath = strrchr(sym_info.dli_fname, '/');
260 if (endpath)
261 endpath++;
262 else
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);
269 return (0);
270 } else {
271 umem_printf("%s+0x%p", sym_info.dli_sname,
272 (char *)pointer - (char *)sym_info.dli_saddr);
273 return (1);