10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*5891Sraf * Common Development and Distribution License (the "License").
6*5891Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*5891Sraf
220Sstevel@tonic-gate /*
23*5891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * Failure routines for libumem (not standalone)
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include "misc.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate static volatile int umem_exiting = 0;
390Sstevel@tonic-gate #define UMEM_EXIT_ABORT 1
400Sstevel@tonic-gate
410Sstevel@tonic-gate static mutex_t umem_exit_lock = DEFAULTMUTEX; /* protects umem_exiting */
420Sstevel@tonic-gate
430Sstevel@tonic-gate static int
firstexit(int type)440Sstevel@tonic-gate firstexit(int type)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate if (umem_exiting)
470Sstevel@tonic-gate return (0);
480Sstevel@tonic-gate
490Sstevel@tonic-gate (void) mutex_lock(&umem_exit_lock);
500Sstevel@tonic-gate if (umem_exiting) {
510Sstevel@tonic-gate (void) mutex_unlock(&umem_exit_lock);
520Sstevel@tonic-gate return (0);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate umem_exiting = type;
550Sstevel@tonic-gate (void) mutex_unlock(&umem_exit_lock);
560Sstevel@tonic-gate
570Sstevel@tonic-gate return (1);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * We can't use abort(3C), since it closes all of the standard library
620Sstevel@tonic-gate * FILEs, which can call free().
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * In addition, we can't just raise(SIGABRT), since the current handler
650Sstevel@tonic-gate * might do allocation. We give them once chance, though.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate static void __NORETURN
umem_do_abort(void)680Sstevel@tonic-gate umem_do_abort(void)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate if (firstexit(UMEM_EXIT_ABORT))
710Sstevel@tonic-gate (void) raise(SIGABRT);
720Sstevel@tonic-gate
730Sstevel@tonic-gate for (;;) {
740Sstevel@tonic-gate (void) signal(SIGABRT, SIG_DFL);
750Sstevel@tonic-gate (void) sigrelse(SIGABRT);
760Sstevel@tonic-gate (void) raise(SIGABRT);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate #define SKIP_FRAMES 1 /* skip the panic frame */
810Sstevel@tonic-gate #define ERR_STACK_FRAMES 128
820Sstevel@tonic-gate
830Sstevel@tonic-gate static void
print_stacktrace(void)840Sstevel@tonic-gate print_stacktrace(void)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate uintptr_t cur_stack[ERR_STACK_FRAMES];
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * if we are in a signal context, checking for it will recurse
900Sstevel@tonic-gate */
910Sstevel@tonic-gate uint_t nframes = getpcstack(cur_stack, ERR_STACK_FRAMES, 0);
920Sstevel@tonic-gate uint_t idx;
930Sstevel@tonic-gate
940Sstevel@tonic-gate if (nframes > SKIP_FRAMES) {
950Sstevel@tonic-gate umem_printf("stack trace:\n");
960Sstevel@tonic-gate
970Sstevel@tonic-gate for (idx = SKIP_FRAMES; idx < nframes; idx++) {
980Sstevel@tonic-gate (void) print_sym((void *)cur_stack[idx]);
990Sstevel@tonic-gate umem_printf("\n");
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate void
umem_panic(const char * format,...)1050Sstevel@tonic-gate umem_panic(const char *format, ...)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate va_list va;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate va_start(va, format);
1100Sstevel@tonic-gate umem_vprintf(format, va);
1110Sstevel@tonic-gate va_end(va);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (format[strlen(format)-1] != '\n')
1140Sstevel@tonic-gate umem_error_enter("\n");
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate print_stacktrace();
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate umem_do_abort();
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate void
umem_err_recoverable(const char * format,...)1220Sstevel@tonic-gate umem_err_recoverable(const char *format, ...)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate va_list va;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate va_start(va, format);
1270Sstevel@tonic-gate umem_vprintf(format, va);
1280Sstevel@tonic-gate va_end(va);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (format[strlen(format)-1] != '\n')
1310Sstevel@tonic-gate umem_error_enter("\n");
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate print_stacktrace();
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if (umem_abort > 0)
1360Sstevel@tonic-gate umem_do_abort();
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate int
__umem_assert_failed(const char * assertion,const char * file,int line)1400Sstevel@tonic-gate __umem_assert_failed(const char *assertion, const char *file, int line)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate umem_panic("Assertion failed: %s, file %s, line %d\n",
1430Sstevel@tonic-gate assertion, file, line);
1440Sstevel@tonic-gate /*NOTREACHED*/
1450Sstevel@tonic-gate return (0);
1460Sstevel@tonic-gate }
147