xref: /illumos-gate/usr/src/lib/libumem/common/misc.h (revision bdb9230ac765cb7af3fc1f4119caf2c5720dceb3)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_MISC_H
28 #define	_MISC_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <thread.h>
35 #include <pthread.h>
36 #include <stdarg.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 extern uint_t umem_abort;		/* abort when errors occur */
43 extern uint_t umem_output;		/* output error messages to stderr */
44 extern caddr_t umem_min_stack;		/* max stack address for audit log */
45 extern caddr_t umem_max_stack;		/* min stack address for audit log */
46 
47 /*
48  * various utility functions
49  * These are globally implemented.
50  */
51 
52 #undef	offsetof
53 #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
54 
55 /*
56  * a safe printf  -- do not use for error messages.
57  */
58 void debug_printf(const char *format, ...);
59 
60 /*
61  * adds a message to the log without writing it out.
62  */
63 void log_message(const char *format, ...);
64 
65 /*
66  * returns the index of the (high/low) bit + 1
67  */
68 int highbit(ulong_t);
69 int lowbit(ulong_t);
70 #pragma no_side_effect(highbit, lowbit)
71 
72 /*
73  * Converts a hrtime_t to a timestruc_t
74  */
75 void hrt2ts(hrtime_t hrt, timestruc_t *tsp);
76 
77 /*
78  * tries to print out the symbol and offset of a pointer using umem_error_info
79  */
80 int print_sym(void *pointer);
81 
82 /*
83  * Information about the current error.  Can be called multiple times, should
84  * be followed eventually with a call to umem_err or umem_err_recoverable.
85  */
86 void umem_printf(const char *format, ...);
87 void umem_vprintf(const char *format, va_list);
88 
89 void umem_printf_warn(void *ignored, const char *format, ...);
90 
91 void umem_error_enter(const char *);
92 
93 /*
94  * prints error message and stack trace, then aborts.  Cannot return.
95  */
96 void umem_panic(const char *format, ...) __NORETURN;
97 #pragma does_not_return(umem_panic)
98 #pragma rarely_called(umem_panic)
99 
100 /*
101  * like umem_err, but only aborts if umem_abort > 0
102  */
103 void umem_err_recoverable(const char *format, ...);
104 
105 /*
106  * We define our own assertion handling since libc's assert() calls malloc()
107  */
108 #ifdef NDEBUG
109 #define	ASSERT(assertion) (void)0
110 #else
111 #define	ASSERT(assertion) (void)((assertion) || \
112     __umem_assert_failed(#assertion, __FILE__, __LINE__))
113 #endif
114 
115 int __umem_assert_failed(const char *assertion, const char *file, int line);
116 #pragma does_not_return(__umem_assert_failed)
117 #pragma rarely_called(__umem_assert_failed)
118 /*
119  * These have architecture-specific implementations.
120  */
121 
122 /*
123  * Returns the current function's frame pointer.
124  */
125 extern void *getfp(void);
126 
127 /*
128  * puts a pc-only stack trace of up to pcstack_limit frames into pcstack.
129  * Returns the number of stacks written.
130  *
131  * if check_sighandler != 0, and we are in a signal context, calls
132  * umem_err_recoverable.
133  */
134 extern int getpcstack(uintptr_t *pcstack, int pcstack_limit,
135     int check_sighandler);
136 
137 #ifdef	__cplusplus
138 }
139 #endif
140 
141 #endif /* _MISC_H */
142