Fix stack alignment on x86_64
[glibc.git] / stdlib / abort.c
blobe9d0ab18fd7d05e3d7016d03794438ccd1e141d6
1 /* Copyright (C) 1991,1993,1995-1998,2001,2002,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <bits/libc-lock.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 /* Try to get a machine dependent instruction which will make the
28 program crash. This is used in case everything else fails. */
29 #include <abort-instr.h>
30 #ifndef ABORT_INSTRUCTION
31 /* No such instruction is available. */
32 # define ABORT_INSTRUCTION
33 #endif
35 #ifdef USE_IN_LIBIO
36 # include <libio/libioP.h>
37 # define fflush(s) _IO_flush_all_lockp (0)
38 #endif
40 /* Exported variable to locate abort message in core files etc. */
41 struct abort_msg_s *__abort_msg __attribute__ ((nocommon));
42 libc_hidden_def (__abort_msg)
44 /* We must avoid to run in circles. Therefore we remember how far we
45 already got. */
46 static int stage;
48 /* We should be prepared for multiple threads trying to run abort. */
49 __libc_lock_define_initialized_recursive (static, lock);
52 /* Cause an abnormal program termination with core-dump. */
53 void
54 abort (void)
56 struct sigaction act;
57 sigset_t sigs;
59 /* First acquire the lock. */
60 __libc_lock_lock_recursive (lock);
62 /* Now it's for sure we are alone. But recursive calls are possible. */
64 /* Unlock SIGABRT. */
65 if (stage == 0)
67 ++stage;
68 if (__sigemptyset (&sigs) == 0 &&
69 __sigaddset (&sigs, SIGABRT) == 0)
70 __sigprocmask (SIG_UNBLOCK, &sigs, (sigset_t *) NULL);
73 /* Flush all streams. We cannot close them now because the user
74 might have registered a handler for SIGABRT. */
75 if (stage == 1)
77 ++stage;
78 fflush (NULL);
81 /* Send signal which possibly calls a user handler. */
82 if (stage == 2)
84 /* This stage is special: we must allow repeated calls of
85 `abort' when a user defined handler for SIGABRT is installed.
86 This is risky since the `raise' implementation might also
87 fail but I don't see another possibility. */
88 int save_stage = stage;
90 stage = 0;
91 __libc_lock_unlock_recursive (lock);
93 raise (SIGABRT);
95 __libc_lock_lock_recursive (lock);
96 stage = save_stage + 1;
99 /* There was a handler installed. Now remove it. */
100 if (stage == 3)
102 ++stage;
103 memset (&act, '\0', sizeof (struct sigaction));
104 act.sa_handler = SIG_DFL;
105 __sigfillset (&act.sa_mask);
106 act.sa_flags = 0;
107 __sigaction (SIGABRT, &act, NULL);
110 /* Now close the streams which also flushes the output the user
111 defined handler might has produced. */
112 if (stage == 4)
114 ++stage;
115 __fcloseall ();
118 /* Try again. */
119 if (stage == 5)
121 ++stage;
122 raise (SIGABRT);
125 /* Now try to abort using the system specific command. */
126 if (stage == 6)
128 ++stage;
129 ABORT_INSTRUCTION;
132 /* If we can't signal ourselves and the abort instruction failed, exit. */
133 if (stage == 7)
135 ++stage;
136 _exit (127);
139 /* If even this fails try to use the provided instruction to crash
140 or otherwise make sure we never return. */
141 while (1)
142 /* Try for ever and ever. */
143 ABORT_INSTRUCTION;
145 libc_hidden_def (abort)