xref: /titanic_52/usr/src/lib/libumem/common/umem_fail.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Failure routines for libumem (not standalone)
31  */
32 
33 #include "c_synonyms.h"
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <string.h>
38 
39 #include "misc.h"
40 
41 static volatile int umem_exiting = 0;
42 #define	UMEM_EXIT_ABORT	1
43 
44 static mutex_t umem_exit_lock = DEFAULTMUTEX; /* protects umem_exiting */
45 
46 static int
47 firstexit(int type)
48 {
49 	if (umem_exiting)
50 		return (0);
51 
52 	(void) mutex_lock(&umem_exit_lock);
53 	if (umem_exiting) {
54 		(void) mutex_unlock(&umem_exit_lock);
55 		return (0);
56 	}
57 	umem_exiting = type;
58 	(void) mutex_unlock(&umem_exit_lock);
59 
60 	return (1);
61 }
62 
63 /*
64  * We can't use abort(3C), since it closes all of the standard library
65  * FILEs, which can call free().
66  *
67  * In addition, we can't just raise(SIGABRT), since the current handler
68  * might do allocation.  We give them once chance, though.
69  */
70 static void __NORETURN
71 umem_do_abort(void)
72 {
73 	if (firstexit(UMEM_EXIT_ABORT))
74 		(void) raise(SIGABRT);
75 
76 	for (;;) {
77 		(void) signal(SIGABRT, SIG_DFL);
78 		(void) sigrelse(SIGABRT);
79 		(void) raise(SIGABRT);
80 	}
81 }
82 
83 #define	SKIP_FRAMES		1	/* skip the panic frame */
84 #define	ERR_STACK_FRAMES	128
85 
86 static void
87 print_stacktrace(void)
88 {
89 	uintptr_t cur_stack[ERR_STACK_FRAMES];
90 
91 	/*
92 	 * if we are in a signal context, checking for it will recurse
93 	 */
94 	uint_t nframes = getpcstack(cur_stack, ERR_STACK_FRAMES, 0);
95 	uint_t idx;
96 
97 	if (nframes > SKIP_FRAMES) {
98 		umem_printf("stack trace:\n");
99 
100 		for (idx = SKIP_FRAMES; idx < nframes; idx++) {
101 			(void) print_sym((void *)cur_stack[idx]);
102 			umem_printf("\n");
103 		}
104 	}
105 }
106 
107 void
108 umem_panic(const char *format, ...)
109 {
110 	va_list va;
111 
112 	va_start(va, format);
113 	umem_vprintf(format, va);
114 	va_end(va);
115 
116 	if (format[strlen(format)-1] != '\n')
117 		umem_error_enter("\n");
118 
119 	print_stacktrace();
120 
121 	umem_do_abort();
122 }
123 
124 void
125 umem_err_recoverable(const char *format, ...)
126 {
127 	va_list va;
128 
129 	va_start(va, format);
130 	umem_vprintf(format, va);
131 	va_end(va);
132 
133 	if (format[strlen(format)-1] != '\n')
134 		umem_error_enter("\n");
135 
136 	print_stacktrace();
137 
138 	if (umem_abort > 0)
139 		umem_do_abort();
140 }
141 
142 int
143 __umem_assert_failed(const char *assertion, const char *file, int line)
144 {
145 	umem_panic("Assertion failed: %s, file %s, line %d\n",
146 	    assertion, file, line);
147 	umem_do_abort();
148 	/*NOTREACHED*/
149 	return (0);
150 }
151