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