[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new module 'nocrash'

From: Bruno Haible
Subject: new module 'nocrash'
Date: Sun, 4 Nov 2007 19:50:44 +0100
User-agent: KMail/1.5.4


Some autoconf tests require running a program and seeing whether it crashes
or not. Unfortunately, one some platforms, this causes a dialog box to appear.
If a configure script has 10 test programs of this kind, the installer can
mutate into a click-o-maniac; in other words, it's annoying.

One such platform is MacOS X (in some configurations), another one is BeOS.

Here's a macro that lets the autoconf test fail without a crash. So it
gets rid of the dialog box, without affecting the test's result.

2007-11-04  Bruno Haible  <address@hidden>

        * modules/nocrash: New file.
        * m4/nocrash.m4: New file, taken from GNU clisp. Code taken from
        GNU libsigsegv, with permission of GNU libsigsegv's copyright holders.

============================ modules/nocrash ==============================
Macro that avoids crashes in configure tests.







Bruno Haible, Paolo Bonzini

============================ m4/nocrash.m4 ================================
# nocrash.m4 serial 1
dnl Copyright (C) 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl Based on libsigsegv, from Bruno Haible and Paolo Bonzini.


dnl Expands to some code for use in .c programs that will cause the configure
dnl test to exit instead of crashing. This is useful to avoid triggering
dnl action from a background debugger and to avoid core dumps.
dnl Usage:   ...
dnl          ]GL_NOCRASH[
dnl          ...
dnl          int main() { nocrash_init(); ... }
#include <stdlib.h>
#if defined __MACH__ && defined __APPLE__
/* Avoid a crash on MacOS X.  */
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach/thread_status.h>
#include <mach/exception.h>
#include <mach/task.h>
#include <pthread.h>
/* The exception port on which our thread listens.  */
static mach_port_t our_exception_port;
/* The main function of the thread listening for exceptions of type
static void *
mach_exception_thread (void *arg)
  /* Buffer for a message to be received.  */
  struct {
    mach_msg_header_t head;
    mach_msg_body_t msgh_body;
    char data[1024];
  } msg;
  mach_msg_return_t retval;
  /* Wait for a message on the exception port.  */
  retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0, sizeof (msg),
                     our_exception_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  if (retval != MACH_MSG_SUCCESS)
    abort ();
  exit (1);
static void
nocrash_init (void)
  mach_port_t self = mach_task_self ();
  /* Allocate a port on which the thread shall listen for exceptions.  */
  if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
      == KERN_SUCCESS) {
    /* See 
    if (mach_port_insert_right (self, our_exception_port, our_exception_port,
        == KERN_SUCCESS) {
      /* The exceptions we want to catch.  Only EXC_BAD_ACCESS is interesting
         for us.  */
      exception_mask_t mask = EXC_MASK_BAD_ACCESS;
      /* Create the thread listening on the exception port.  */
      pthread_attr_t attr;
      pthread_t thread;
      if (pthread_attr_init (&attr) == 0
          && pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) == 0
          && pthread_create (&thread, &attr, mach_exception_thread, NULL) == 0) 
        pthread_attr_destroy (&attr);
        /* Replace the exception port info for these exceptions with our own.
           Note that we replace the exception port for the entire task, not only
           for a particular thread.  This has the effect that when our exception
           port gets the message, the thread specific exception port has already
           been asked, and we don't need to bother about it.
        task_set_exception_ports (self, mask, our_exception_port,
                                  EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
/* Avoid a crash on POSIX systems.  */
#include <signal.h>
/* A POSIX signal handler.  */
static void
exception_handler (int sig)
  exit (1);
static void
nocrash_init (void)
#ifdef SIGSEGV
  signal (SIGSEGV, exception_handler);
#ifdef SIGBUS
  signal (SIGBUS, exception_handler);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]