8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libumem / common / umem_fail.c
blob8e315b76c5429c69533d5e52c1f5d5cfe0278524
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.
28 * Failure routines for libumem (not standalone)
31 #include <sys/types.h>
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <string.h>
36 #include "misc.h"
38 static volatile int umem_exiting = 0;
39 #define UMEM_EXIT_ABORT 1
41 static mutex_t umem_exit_lock = DEFAULTMUTEX; /* protects umem_exiting */
43 static int
44 firstexit(int type)
46 if (umem_exiting)
47 return (0);
49 (void) mutex_lock(&umem_exit_lock);
50 if (umem_exiting) {
51 (void) mutex_unlock(&umem_exit_lock);
52 return (0);
54 umem_exiting = type;
55 (void) mutex_unlock(&umem_exit_lock);
57 return (1);
61 * We can't use abort(3C), since it closes all of the standard library
62 * FILEs, which can call free().
64 * In addition, we can't just raise(SIGABRT), since the current handler
65 * might do allocation. We give them once chance, though.
67 static void __NORETURN
68 umem_do_abort(void)
70 if (firstexit(UMEM_EXIT_ABORT))
71 (void) raise(SIGABRT);
73 for (;;) {
74 (void) signal(SIGABRT, SIG_DFL);
75 (void) sigrelse(SIGABRT);
76 (void) raise(SIGABRT);
80 #define SKIP_FRAMES 1 /* skip the panic frame */
81 #define ERR_STACK_FRAMES 128
83 static void
84 print_stacktrace(void)
86 uintptr_t cur_stack[ERR_STACK_FRAMES];
89 * if we are in a signal context, checking for it will recurse
91 uint_t nframes = getpcstack(cur_stack, ERR_STACK_FRAMES, 0);
92 uint_t idx;
94 if (nframes > SKIP_FRAMES) {
95 umem_printf("stack trace:\n");
97 for (idx = SKIP_FRAMES; idx < nframes; idx++) {
98 (void) print_sym((void *)cur_stack[idx]);
99 umem_printf("\n");
104 void
105 umem_panic(const char *format, ...)
107 va_list va;
109 va_start(va, format);
110 umem_vprintf(format, va);
111 va_end(va);
113 if (format[strlen(format)-1] != '\n')
114 umem_error_enter("\n");
116 print_stacktrace();
118 umem_do_abort();
121 void
122 umem_err_recoverable(const char *format, ...)
124 va_list va;
126 va_start(va, format);
127 umem_vprintf(format, va);
128 va_end(va);
130 if (format[strlen(format)-1] != '\n')
131 umem_error_enter("\n");
133 print_stacktrace();
135 if (umem_abort > 0)
136 umem_do_abort();
140 __umem_assert_failed(const char *assertion, const char *file, int line)
142 umem_panic("Assertion failed: %s, file %s, line %d\n",
143 assertion, file, line);
144 /*NOTREACHED*/
145 return (0);