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