OSDN Git Service

Convert the rest of users to hidden
[uclinux-h8/uClibc.git] / libc / stdlib / abort.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB.  If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.  */
18
19 /* Hacked up for uClibc by Erik Andersen */
20
21 #define sigaction __sigaction
22
23 #define _GNU_SOURCE
24 #include <features.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <errno.h>
32
33
34 /* Our last ditch effort to commit suicide */
35 #if defined(__alpha__)
36 #define ABORT_INSTRUCTION asm ("call_pal 0")
37 #elif defined(__hppa__)
38 #define ABORT_INSTRUCTION asm ("iitlbp %r0,(%r0)")
39 #elif defined(__i386__)
40 #define ABORT_INSTRUCTION asm ("hlt")
41 #elif defined(__ia64__)
42 #define ABORT_INSTRUCTION asm ("break 0")
43 #elif defined(__m68k__)
44 #define ABORT_INSTRUCTION asm ("illegal")
45 #elif defined(__mc68000__)
46 #define ABORT_INSTRUCTION asm (".long 0xffffffff")
47 #elif defined(__mips__)
48 #define ABORT_INSTRUCTION asm ("break 255")
49 #elif defined(__powerpc__)
50 #define ABORT_INSTRUCTION asm (".long 0")
51 #elif defined(__s390__)
52 #define ABORT_INSTRUCTION asm (".word 0")
53 #elif defined(__sparc__)
54 #define ABORT_INSTRUCTION asm ("unimp 0xf00")
55 #elif defined(__SH5__)
56 #define ABORT_INSTRUCTION asm ("movi 0x10, r9; shori 0xff, r9; trapa r9")
57 #elif defined(__sh2__)
58 #define ABORT_INSTRUCTION asm ("trapa #32")
59 #elif defined(__sh__)
60 #define ABORT_INSTRUCTION asm ("trapa #0xff")
61 #elif defined(__x86_64__)
62 #define ABORT_INSTRUCTION asm ("hlt")
63 #else
64 #define ABORT_INSTRUCTION
65 #warning no abort instruction define for your arch
66 #endif
67
68 #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
69 extern void weak_function _stdio_term(void);
70 #endif
71 extern void _exit __P((int __status)) __attribute__ ((__noreturn__));
72 static int been_there_done_that = 0;
73
74 /* Be prepared in case multiple threads try to abort() */
75 #ifdef __UCLIBC_HAS_THREADS__
76 # include <pthread.h>
77 static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
78 #endif
79 #define LOCK    __pthread_mutex_lock(&mylock)
80 #define UNLOCK  __pthread_mutex_unlock(&mylock)
81
82
83 extern int __raise (int __sig) __THROW attribute_hidden;
84 /* Cause an abnormal program termination with core-dump */
85 void abort(void)
86 {
87         sigset_t sigset;
88
89         /* Make sure we acquire the lock before proceeding */
90         LOCK;
91
92         /* Unmask SIGABRT to be sure we can get it */
93         if (__sigemptyset(&sigset) == 0 && __sigaddset(&sigset, SIGABRT) == 0) {
94                 __sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *) NULL);
95         }
96
97         while (1) {
98                 /* Try to suicide with a SIGABRT */
99                 if (been_there_done_that == 0) {
100                         been_there_done_that++;
101
102 #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
103                         /* If we are using stdio, try to shut it down.  At the very least,
104                          * this will attemt to commit all buffered writes.  It may also
105                          * unboffer all writable files, or close them outright.
106                          * Check the stdio routines for details. */
107                         if (_stdio_term) {
108                                 _stdio_term();
109                         }
110 #endif
111
112 abort_it:
113                         UNLOCK;
114                         __raise(SIGABRT);
115                         LOCK;
116                 }
117
118                 /* Still here?  Try to remove any signal handlers */
119                 if (been_there_done_that == 1) {
120                         struct sigaction act;
121
122                         been_there_done_that++;
123                         __memset(&act, '\0', sizeof(struct sigaction));
124                         act.sa_handler = SIG_DFL;
125                         __sigfillset(&act.sa_mask);
126                         act.sa_flags = 0;
127                         sigaction(SIGABRT, &act, NULL);
128
129                         goto abort_it;
130                 }
131
132                 /* Still here?  Try to suicide with an illegal instruction */
133                 if (been_there_done_that == 2) {
134                         been_there_done_that++;
135                         ABORT_INSTRUCTION;
136                 }
137
138                 /* Still here?  Try to at least exit */
139                 if (been_there_done_that == 3) {
140                         been_there_done_that++;
141                         _exit(127);
142                 }
143
144                 /* Still here?  We're screwed.  Sleepy time.  Good night. */
145                 while (1)
146                         /* Try for ever and ever */
147                         ABORT_INSTRUCTION;
148         }
149 }