1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
45 #include "gromacs/fileio/futil.h"
49 #include "gmx_fatal.h"
54 #include "gromacs/fileio/gmxfio.h"
56 #include "gromacs/utility/gmxmpi.h"
58 static gmx_bool bDebug = FALSE;
59 static char *fatal_tmp_file = NULL;
60 static FILE *log_file = NULL;
63 #include "thread_mpi/threads.h"
64 static tMPI_Thread_mutex_t debug_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
65 static tMPI_Thread_mutex_t where_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
66 static tMPI_Thread_mutex_t fatal_tmp_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
70 gmx_bool bDebugMode(void)
73 /*#ifdef GMX_THREAD_MPI*/
75 tMPI_Thread_mutex_lock(&debug_mutex);
78 /*#ifdef GMX_THREAD_MPI*/
80 tMPI_Thread_mutex_unlock(&debug_mutex);
85 void gmx_fatal_set_log_file(FILE *fp)
90 void _where(const char *file, int line)
92 static gmx_bool bFirst = TRUE;
93 static int nskip = -1;
94 static int nwhere = 0;
100 #ifdef GMX_THREAD_MPI
101 tMPI_Thread_mutex_lock(&where_mutex);
102 if (bFirst) /* we repeat the check in the locked section because things
103 might have changed */
106 if ((temp = getenv("WHERE")) != NULL)
108 nskip = strtol(temp, NULL, 10);
111 #ifdef GMX_THREAD_MPI
113 tMPI_Thread_mutex_unlock(&where_mutex);
120 /* Skip the first n occasions, this allows to see where it goes wrong */
131 fprintf(fp, "WHERE %d, file %s - line %d\n", nwhere, file, line);
137 static void bputc(char *msg, int *len, char ch)
142 static void bputs(char *msg, int *len, const char *s, int fld)
144 for (fld -= (int)strlen(s); fld > 0; fld--)
146 bputc(msg, len, ' ');
150 bputc(msg, len, *(s++));
154 static void bputd(char *msg, int *len, int d)
158 bputc(msg, len, d+'0');
162 bputc(msg, len, d-10+'a');
166 static void bputi(char *msg, int *len, int val, int radix, int fld, gmx_bool bNeg)
177 for (fld--; fld > fmax; fld--)
179 bputc(msg, len, ' ');
183 bputc(msg, len, '-');
185 bputd(msg, len, val);
191 bputc(msg, len, '-');
193 bputi(msg, len, val/radix, radix, fld-1, FALSE);
194 bputd(msg, len, val%radix);
198 static int getfld(const char **p)
205 fld = (fld*10)+((*((*p)++))-'0');
210 /*static void _halt(char *file,int line,char *reason)
212 fprintf(stderr,"\nHALT in file %s line %d because:\n\t%s\n",
218 static int fatal_errno = 0;
220 static void quit_gmx(const char *msg)
222 #ifdef GMX_THREAD_MPI
223 tMPI_Thread_mutex_lock(&debug_mutex);
225 if (fatal_errno == 0)
229 fprintf(log_file, "%s\n", msg);
231 fprintf(stderr, "%s\n", msg);
232 /* we set it to no-zero because if this function is called, something
238 if (fatal_errno != -1)
246 if (gmx_mpi_initialized())
251 nnodes = gmx_node_num();
252 noderank = gmx_node_rank();
256 fprintf(stderr, "Error on node %d, will try to stop all the nodes\n",
259 gmx_abort(noderank, nnodes, -1);
269 fprintf(stderr, "dump core (y/n):");
271 if (toupper(getc(stdin)) != 'N')
278 #ifdef GMX_THREAD_MPI
279 tMPI_Thread_mutex_unlock(&debug_mutex);
283 /* The function below should be identical to quit_gmx,
284 * except that is does not actually quit and call gmx_abort.
286 static void quit_gmx_noquit(const char *msg)
288 #ifdef GMX_THREAD_MPI
289 tMPI_Thread_mutex_lock(&debug_mutex);
295 fprintf(log_file, "%s\n", msg);
297 fprintf(stderr, "%s\n", msg);
298 /* we set it to no-zero because if this function is called, something
304 if (fatal_errno != -1)
318 fprintf(stderr, "dump core (y/n):");
320 if (toupper(getc(stdin)) != 'N')
327 #ifdef GMX_THREAD_MPI
328 tMPI_Thread_mutex_unlock(&debug_mutex);
332 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
334 #ifdef GMX_THREAD_MPI
335 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
337 if (fatal_tmp_file == NULL)
339 fatal_tmp_file = strdup(fn);
343 fprintf(stderr, "BUGWARNING: fatal_tmp_file already set at %s:%d",
346 #ifdef GMX_THREAD_MPI
347 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
351 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
353 #ifdef GMX_THREAD_MPI
354 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
356 if (strcmp(fn, fatal_tmp_file) == 0)
358 sfree(fatal_tmp_file);
359 fatal_tmp_file = NULL;
363 fprintf(stderr, "BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
366 #ifdef GMX_THREAD_MPI
367 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
371 static void clean_fatal_tmp_file()
373 #ifdef GMX_THREAD_MPI
374 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
378 fprintf(stderr, "Cleaning up temporary file %s\n", fatal_tmp_file);
379 remove(fatal_tmp_file);
380 sfree(fatal_tmp_file);
381 fatal_tmp_file = NULL;
383 #ifdef GMX_THREAD_MPI
384 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
388 static void parse_printf_args(const char *fmt, va_list *ap, char *msg)
393 char ibuf[64], ifmt[64];
394 int index, ival, fld;
398 for (p = fmt; *p; p++)
402 bputc(msg, &len, *p);
411 ival = va_arg(*ap, int);
412 sprintf(ifmt, "0x%%%dx", fld);
413 sprintf(ibuf, ifmt, (unsigned int)ival);
414 for (index = 0; (index < (int)strlen(ibuf)); index++)
416 bputc(msg, &len, ibuf[index]);
420 ival = va_arg(*ap, int);
421 sprintf(ifmt, "%%%dd", fld);
422 sprintf(ibuf, ifmt, ival);
423 for (index = 0; (index < (int)strlen(ibuf)); index++)
425 bputc(msg, &len, ibuf[index]);
429 ival = va_arg(*ap, unsigned);
430 sprintf(ifmt, "%%%du", fld);
431 sprintf(ibuf, ifmt, ival);
432 for (index = 0; (index < (int)strlen(ibuf)); index++)
434 bputc(msg, &len, ibuf[index]);
438 dval = va_arg(*ap, double);
439 sprintf(ifmt, "%%%df", fld);
440 sprintf(ibuf, ifmt, dval);
441 for (index = 0; (index < (int)strlen(ibuf)); index++)
443 bputc(msg, &len, ibuf[index]);
447 dval = va_arg(*ap, double);
448 sprintf(ifmt, "%%%dg", fld);
449 sprintf(ibuf, ifmt, dval);
450 for (index = 0; (index < (int)strlen(ibuf)); index++)
452 bputc(msg, &len, ibuf[index]);
456 cval = (char) va_arg(*ap, int); /* char is promoted to int */
457 bputc(msg, &len, cval);
460 sval = va_arg(*ap, char *);
463 sval = strdup("(null)");
465 bputs(msg, &len, sval, fld);
468 bputc(msg, &len, *p);
476 bputc(msg, &len, '\0');
479 void gmx_fatal(int f_errno, const char *file, int line, const char *fmt, ...)
486 clean_fatal_tmp_file();
488 parse_printf_args(fmt, &ap, msg);
492 #ifdef GMX_THREAD_MPI
493 tMPI_Thread_mutex_lock(&debug_mutex);
496 fatal_errno = f_errno;
498 #ifdef GMX_THREAD_MPI
499 tMPI_Thread_mutex_unlock(&debug_mutex);
502 _gmx_error("fatal", msg, file, line);
505 void gmx_fatal_collective(int f_errno, const char *file, int line,
506 const t_commrec *cr, gmx_domdec_t *dd,
507 const char *fmt, ...)
519 /* Check if we are calling on all processes in MPI_COMM_WORLD */
522 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
526 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
528 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
529 bFinalize = (result != MPI_UNEQUAL);
532 if ((cr != NULL && MASTER(cr) ) ||
533 (dd != NULL && DDMASTER(dd)))
537 clean_fatal_tmp_file();
539 parse_printf_args(fmt, &ap, msg);
543 #ifdef GMX_THREAD_MPI
544 tMPI_Thread_mutex_lock(&debug_mutex);
547 fatal_errno = f_errno;
549 #ifdef GMX_THREAD_MPI
550 tMPI_Thread_mutex_unlock(&debug_mutex);
555 /* Use an error handler that does not quit */
556 set_gmx_error_handler(quit_gmx_noquit);
559 _gmx_error("fatal", msg, file, line);
565 /* Broadcast the fatal error number possibly modified
566 * on the master process, in case the user would like
567 * to use the return status on a non-master process.
568 * The master process in cr and dd always has global rank 0.
570 MPI_Bcast(&fatal_errno, sizeof(fatal_errno), MPI_BYTE,
573 /* Finalize nicely instead of aborting */
578 /* Let all other processes wait till the master has printed
579 * the error message and issued MPI_Abort.
581 MPI_Barrier(MPI_COMM_WORLD);
588 void _invalid_case(const char *fn, int line)
590 gmx_fatal(FARGS, "Invalid case in switch statement, file %s, line %d",
594 void _unexpected_eof(const char *fn, int line, const char *srcfn, int srcline)
596 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d\n"
597 "(Source file %s, line %d)", fn, line, srcfn, srcline);
601 * These files are global variables in the gromacs preprocessor
602 * Every routine in a file that includes gmx_fatal.h can write to these
603 * debug channels. Depending on the debuglevel used
604 * 0 to 3 of these filed are redirected to /dev/null
608 gmx_bool gmx_debug_at = FALSE;
610 void init_debug (const int dbglevel, const char *dbgfile)
612 #ifdef GMX_THREAD_MPI
613 tMPI_Thread_mutex_lock(&debug_mutex);
615 if (!bDebug) /* another thread hasn't already run this*/
618 debug = gmx_fio_fopen(dbgfile, "w+");
625 #ifdef GMX_THREAD_MPI
626 tMPI_Thread_mutex_unlock(&debug_mutex);
630 #if (defined __sgi && defined USE_SGI_FPE)
631 static void user_routine(unsigned us[5], int ii[2])
633 fprintf(stderr, "User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
634 us[0], us[1], us[2], us[3], us[4], ii[0], ii[1]);
635 fprintf(stderr, "Exception encountered! Dumping core\n");
639 static void abort_routine(unsigned int **ii)
641 fprintf(stderr, "Abort routine\n");
645 static void handle_signals(int n)
647 fprintf(stderr, "Handle signals: n = %d\n", n);
648 fprintf(stderr, "Dumping core\n");
652 void doexceptions(void)
656 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
658 int onoff, en_mask, abort_action, i;
660 #ifdef GMX_THREAD_MPI
661 tMPI_Thread_mutex_lock(&debug_mutex);
664 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
665 _EN_INVALID | _EN_INT_OVERFL;
666 abort_action = _ABORT_ON_ERROR;
667 handle_sigfpes(onoff, en_mask, user_routine, abort_action, abort_routine);
669 for (i = 0; (i < asize(hs)); i++)
671 signal(hs[i], handle_signals);
673 #ifdef GMX_THREAD_MPI
674 tMPI_Thread_mutex_unlock(&debug_mutex);
677 #endif /* __sgi and FPE */
679 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
681 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
683 void set_gmx_error_handler(void (*func)(const char *msg))
685 #ifdef GMX_THREAD_MPI
686 tMPI_Thread_mutex_lock(&debug_mutex);
688 gmx_error_handler = func;
689 #ifdef GMX_THREAD_MPI
690 tMPI_Thread_mutex_unlock(&debug_mutex);
694 char *gmx_strerror(const char *key)
697 const char *key, *msg;
699 error_msg_t msg[] = {
700 { "bug", "Possible bug" },
701 { "call", "Routine should not have been called" },
702 { "comm", "Communication (parallel processing) problem" },
703 { "fatal", "Fatal error" },
704 { "cmd", "Invalid command line argument" },
705 { "file", "File input/output error" },
706 { "impl", "Implementation restriction" },
707 { "incons", "Software inconsistency error" },
708 { "input", "Input error or input inconsistency" },
709 { "mem", "Memory allocation/freeing error" },
710 { "open", "Can not open file" },
711 { "range", "Range checking error" }
713 #define NMSG asize(msg)
719 return strdup("Empty message");
723 for (i = 0; (i < NMSG); i++)
725 if (strcmp(key, msg[i].key) == 0)
732 sprintf(buf, "No error message associated with key %s\n%s", key, gmxuser);
737 return strdup(msg[i].msg);
743 void _gmx_error(const char *key, const char *msg, const char *file, int line)
745 char buf[10240], tmpbuf[1024], errerrbuf[1024];
747 const char *llines = "-------------------------------------------------------";
750 /* protect the audience from suggestive discussions */
754 sprintf(errerrbuf, "Empty fatal_error message. %s", gmxuser);
757 cool_quote(tmpbuf, 1023, &cqnum);
758 strerr = gmx_strerror(key);
759 sprintf(buf, "\n%s\nProgram %s, %s\n"
760 "Source code file: %s, line: %d\n\n"
761 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
762 "website at http://www.gromacs.org/Documentation/Errors\n%s\n\n%s\n",
763 llines, ShortProgram(), GromacsVersion(), file, line,
764 strerr, msg ? msg : errerrbuf, llines, tmpbuf);
767 gmx_error_handler(buf);
770 void _range_check(int n, int n_min, int n_max, const char *warn_str,
771 const char *var, const char *file, int line)
775 if ((n < n_min) || (n >= n_max))
777 if (warn_str != NULL)
779 strcpy(buf, warn_str);
787 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
788 "within [ %d .. %d ]\n", var, n, n_min, n_max);
790 _gmx_error("range", buf, file, line);
794 void gmx_warning(const char *fmt, ...)
801 parse_printf_args(fmt, &ap, msg);
805 fprintf(stderr, "\nWARNING: %s\n\n", msg);