bomb.c

Go to the documentation of this file.
00001 /*                          B O M B . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup bu_log */
00023 /*@{*/
00024 /**  @file ./libbu/bomb.c
00025  *
00026  *  This routine is called on a fatal
00027  *  error, where no recovery is possible.
00028  *
00029  *  @par Functions -
00030  *      bu_bomb         Called upon fatal error.
00031  *
00032  *  @author     Michael John Muuss
00033  *
00034  *  @par Source -
00035  *      The U. S. Army Research Laboratory                      @n
00036  *      Aberdeen Proving Ground, Maryland  21005-5068  USA
00037  *
00038  */
00039 
00040 #ifndef lint
00041 static const char RCSbomb[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/bomb.c,v 14.12 2006/08/31 05:50:24 lbutler Exp $ (ARL)";
00042 #endif
00043 
00044 #include "common.h"
00045 
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 #include <ctype.h>
00049 #ifdef HAVE_UNISTD_H
00050 #  include <unistd.h>
00051 #endif
00052 #ifdef HAVE_STRING_H
00053 #  include <string.h>
00054 #endif
00055 #ifdef HAVE_UNIX_IO
00056 #  include <fcntl.h>
00057 #endif
00058 
00059 #include "machine.h"
00060 #include "bu.h"
00061 
00062 #if 1
00063 struct bu_hook_list bu_bomb_hook_list = {
00064         {       BU_LIST_HEAD_MAGIC,
00065                 &bu_bomb_hook_list.l,
00066                 &bu_bomb_hook_list.l
00067         },
00068         BUHOOK_NULL,
00069         GENPTR_NULL
00070 };
00071 #else
00072 struct bu_hook_list bu_bomb_hook_list;
00073 #endif
00074 
00075 /*
00076  * These variables are global because BU_SETJUMP() *must* be a macro.
00077  * If you replace this version of bu_bomb() with one of your own,
00078  * you must also provide these variables, even if you don't use them.
00079  */
00080 int             bu_setjmp_valid = 0;    /**< @brief !0 = bu_jmpbuf is valid */
00081 jmp_buf         bu_jmpbuf;              /**< @brief for BU_SETJMP() */
00082 
00083 /**
00084  *                      B U _ B O M B
00085  *@brief
00086  *  Abort the program with a message.
00087  *
00088  *  Only produce a core-dump when that debugging bit is set.  Note
00089  *  that this function is meant to be a last resort graceful abort.
00090  *  It should not attempt to allocate anything on the stack or heap.
00091  */
00092 void
00093 bu_bomb(const char *str)
00094 {
00095 
00096         /* First thing, always always always try to print the string.
00097          * Avoid passing additional format arguments so as to avoid
00098          * buffer allocations inside fprintf().
00099          */
00100         fprintf(stderr, "\n");
00101         fprintf(stderr, str);
00102         fprintf(stderr, "\n");
00103         fflush(stderr);
00104 
00105         /* MGED would like to be able to additional logging, do callbacks. */
00106         if (BU_LIST_NON_EMPTY(&bu_bomb_hook_list.l)) {
00107                 bu_call_hook(&bu_bomb_hook_list, (genptr_t)str);
00108         }
00109 
00110         if( bu_setjmp_valid )  {
00111                 /* Application is catching fatal errors */
00112                 if( bu_is_parallel() )  {
00113                         fprintf(stderr,"bu_bomb(): in parallel mode, could not longjmp up to application handler\n");
00114                 } else {
00115                         /* Application is non-parallel, so this is safe */
00116                         fprintf(stderr,"bu_bomb(): taking longjmp up to application handler\n");
00117 #if __STDC__
00118                         longjmp( (void *)(bu_jmpbuf), 1 );
00119 #else
00120                         longjmp( (int *)(bu_jmpbuf), 1 );
00121 #endif
00122                         /* NOTREACHED */
00123                 }
00124         }
00125 
00126 #if defined(HAVE_UNIX_IO)
00127         /*
00128          * No application level error handling,
00129          * go to extra pains to ensure that user gets to see this message.
00130          * For example, mged hijacks output sent to stderr.
00131          */
00132         {
00133                 int     fd = open("/dev/tty", 1);
00134                 if( fd >= 0 )  {
00135                         write( fd, str, strlen(str) );
00136                         close(fd);
00137                 }
00138         }
00139 #endif
00140 
00141         /* If in parallel mode, try to signal the leader to die. */
00142         bu_kill_parallel();
00143 
00144         if( bu_debug & BU_DEBUG_COREDUMP )  {
00145                 fprintf(stderr,"bu_bomb causing intentional core dump due to debug flag\n");
00146                 fflush(stdout);
00147                 fflush(stderr);
00148                 abort();        /* should dump if ulimit is non-zero */
00149         }
00150 
00151         exit(12);
00152 }
00153 /*@}*/
00154 /*
00155  * Local Variables:
00156  * mode: C
00157  * tab-width: 8
00158  * c-basic-offset: 4
00159  * indent-tabs-mode: t
00160  * End:
00161  * ex: shiftwidth=4 tabstop=8
00162  */

Generated on Mon Sep 18 01:24:47 2006 for BRL-CAD by  doxygen 1.4.6