~ubuntu-branches/debian/squeeze/ffcall/squeeze

« back to all changes in this revision

Viewing changes to ffcall/m4/nocrash.m4

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2010-06-26 15:29:30 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100626152930-c09y01gk3szcnykn
Tags: 1.10+cvs20100619-2
Ship to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
dnl Copyright (C) 2005 Free Software Foundation, Inc.
2
 
dnl This file is free software, distributed under the terms of the GNU
3
 
dnl General Public License.  As a special exception to the GNU General
4
 
dnl Public License, this file may be distributed as part of a program
5
 
dnl that contains a configuration script generated by Autoconf, under
6
 
dnl the same distribution terms as the rest of that program.
7
 
 
8
 
dnl Based on libsigsegv, from Bruno Haible and Paolo Bonzini.
9
 
 
10
 
AC_PREREQ(2.13)
11
 
 
12
 
dnl Expands to some code for use in .c programs that will cause the configure
13
 
dnl test to exit instead of crashing. This is useful to avoid triggering
14
 
dnl action from a background debugger and to avoid core dumps.
15
 
dnl Usage:   ...
16
 
dnl          ]GL_NOCRASH[
17
 
dnl          ...
18
 
dnl          int main() { nocrash_init(); ... }
19
 
AC_DEFUN([GL_NOCRASH],[[
20
 
#include <stdlib.h>
21
 
#if defined __MACH__ && defined __APPLE__
22
 
/* Avoid a crash on MacOS X.  */
23
 
#include <mach/mach.h>
24
 
#include <mach/mach_error.h>
25
 
#include <mach/thread_status.h>
26
 
#include <mach/exception.h>
27
 
#include <mach/task.h>
28
 
#include <pthread.h>
29
 
/* The exception port on which our thread listens.  */
30
 
static mach_port_t our_exception_port;
31
 
/* The main function of the thread listening for exceptions of type
32
 
   EXC_BAD_ACCESS.  */
33
 
static void *
34
 
mach_exception_thread (void *arg)
35
 
{
36
 
  /* Buffer for a message to be received.  */
37
 
  struct {
38
 
    mach_msg_header_t head;
39
 
    mach_msg_body_t msgh_body;
40
 
    char data[1024];
41
 
  } msg;
42
 
  mach_msg_return_t retval;
43
 
  /* Wait for a message on the exception port.  */
44
 
  retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0, sizeof (msg),
45
 
                     our_exception_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
46
 
  if (retval != MACH_MSG_SUCCESS)
47
 
    abort ();
48
 
  exit (1);
49
 
}
50
 
static void
51
 
nocrash_init (void)
52
 
{
53
 
  mach_port_t self = mach_task_self ();
54
 
  /* Allocate a port on which the thread shall listen for exceptions.  */
55
 
  if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
56
 
      == KERN_SUCCESS) {
57
 
    /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html.  */
58
 
    if (mach_port_insert_right (self, our_exception_port, our_exception_port,
59
 
                                MACH_MSG_TYPE_MAKE_SEND)
60
 
        == KERN_SUCCESS) {
61
 
      /* The exceptions we want to catch.  Only EXC_BAD_ACCESS is interesting
62
 
         for us.  */
63
 
      exception_mask_t mask = EXC_MASK_BAD_ACCESS;
64
 
      /* Create the thread listening on the exception port.  */
65
 
      pthread_attr_t attr;
66
 
      pthread_t thread;
67
 
      if (pthread_attr_init (&attr) == 0
68
 
          && pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) == 0
69
 
          && pthread_create (&thread, &attr, mach_exception_thread, NULL) == 0) {
70
 
        pthread_attr_destroy (&attr);
71
 
        /* Replace the exception port info for these exceptions with our own.
72
 
           Note that we replace the exception port for the entire task, not only
73
 
           for a particular thread.  This has the effect that when our exception
74
 
           port gets the message, the thread specific exception port has already
75
 
           been asked, and we don't need to bother about it.
76
 
           See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html.  */
77
 
        task_set_exception_ports (self, mask, our_exception_port,
78
 
                                  EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
79
 
      }
80
 
    }
81
 
  }
82
 
}
83
 
#else
84
 
/* Avoid a crash on POSIX systems.  */
85
 
#include <signal.h>
86
 
/* A POSIX signal handler.  */
87
 
static void
88
 
exception_handler (int sig)
89
 
{
90
 
  exit (1);
91
 
}
92
 
static void
93
 
nocrash_init (void)
94
 
{
95
 
#ifdef SIGSEGV
96
 
  signal (SIGSEGV, exception_handler);
97
 
#endif
98
 
#ifdef SIGBUS
99
 
  signal (SIGBUS, exception_handler);
100
 
#endif
101
 
}
102
 
#endif
103
 
]])