abort.c 3.9 KB

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