2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
46 #include "gromacs/fileio/futil.h"
49 #include "gmx_fatal.h"
54 #include "gromacs/fileio/gmxfio.h"
56 #include "gromacs/utility/gmxmpi.h"
58 #include "gromacs/legacyheaders/thread_mpi/threads.h"
60 static gmx_bool bDebug = FALSE;
61 static char *fatal_tmp_file = NULL;
62 static FILE *log_file = NULL;
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;
69 gmx_bool bDebugMode(void)
73 tMPI_Thread_mutex_lock(&debug_mutex);
77 tMPI_Thread_mutex_unlock(&debug_mutex);
82 void gmx_fatal_set_log_file(FILE *fp)
87 void _where(const char *file, int line)
89 static gmx_bool bFirst = TRUE;
90 static int nskip = -1;
91 static int nwhere = 0;
97 tMPI_Thread_mutex_lock(&where_mutex);
98 if (bFirst) /* we repeat the check in the locked section because things
101 if ((temp = getenv("WHERE")) != NULL)
103 nskip = strtol(temp, NULL, 10);
107 tMPI_Thread_mutex_unlock(&where_mutex);
112 /* Skip the first n occasions, this allows to see where it goes wrong */
123 fprintf(fp, "WHERE %d, file %s - line %d\n", nwhere, file, line);
129 static void bputc(char *msg, int *len, char ch)
134 static void bputs(char *msg, int *len, const char *s, int fld)
136 for (fld -= (int)strlen(s); fld > 0; fld--)
138 bputc(msg, len, ' ');
142 bputc(msg, len, *(s++));
146 static void bputd(char *msg, int *len, int d)
150 bputc(msg, len, d+'0');
154 bputc(msg, len, d-10+'a');
158 static void bputi(char *msg, int *len, int val, int radix, int fld, gmx_bool bNeg)
169 for (fld--; fld > fmax; fld--)
171 bputc(msg, len, ' ');
175 bputc(msg, len, '-');
177 bputd(msg, len, val);
183 bputc(msg, len, '-');
185 bputi(msg, len, val/radix, radix, fld-1, FALSE);
186 bputd(msg, len, val%radix);
190 static int getfld(const char **p)
197 fld = (fld*10)+((*((*p)++))-'0');
202 /*static void _halt(char *file,int line,char *reason)
204 fprintf(stderr,"\nHALT in file %s line %d because:\n\t%s\n",
210 static int fatal_errno = 0;
212 static void quit_gmx(const char *msg)
214 tMPI_Thread_mutex_lock(&debug_mutex);
215 if (fatal_errno == 0)
219 fprintf(log_file, "%s\n", msg);
221 fprintf(stderr, "%s\n", msg);
222 /* we set it to no-zero because if this function is called, something
228 if (fatal_errno != -1)
236 if (gmx_mpi_initialized())
241 nnodes = gmx_node_num();
242 noderank = gmx_node_rank();
246 fprintf(stderr, "Error on node %d, will try to stop all the nodes\n",
249 gmx_abort(noderank, nnodes, -1);
259 fprintf(stderr, "dump core (y/n):");
261 if (toupper(getc(stdin)) != 'N')
268 tMPI_Thread_mutex_unlock(&debug_mutex);
271 /* The function below should be identical to quit_gmx,
272 * except that is does not actually quit and call gmx_abort.
274 static void quit_gmx_noquit(const char *msg)
276 tMPI_Thread_mutex_lock(&debug_mutex);
281 fprintf(log_file, "%s\n", msg);
283 fprintf(stderr, "%s\n", msg);
284 /* we set it to no-zero because if this function is called, something
290 if (fatal_errno != -1)
304 fprintf(stderr, "dump core (y/n):");
306 if (toupper(getc(stdin)) != 'N')
313 tMPI_Thread_mutex_unlock(&debug_mutex);
316 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
318 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
319 if (fatal_tmp_file == NULL)
321 fatal_tmp_file = strdup(fn);
325 fprintf(stderr, "BUGWARNING: fatal_tmp_file already set at %s:%d",
328 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
331 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
333 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
334 if (strcmp(fn, fatal_tmp_file) == 0)
336 sfree(fatal_tmp_file);
337 fatal_tmp_file = NULL;
341 fprintf(stderr, "BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
344 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
347 static void clean_fatal_tmp_file()
349 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
352 fprintf(stderr, "Cleaning up temporary file %s\n", fatal_tmp_file);
353 remove(fatal_tmp_file);
354 sfree(fatal_tmp_file);
355 fatal_tmp_file = NULL;
357 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
360 static void parse_printf_args(const char *fmt, va_list *ap, char *msg)
365 char ibuf[64], ifmt[64];
366 int index, ival, fld;
370 for (p = fmt; *p; p++)
374 bputc(msg, &len, *p);
383 ival = va_arg(*ap, int);
384 sprintf(ifmt, "0x%%%dx", fld);
385 sprintf(ibuf, ifmt, (unsigned int)ival);
386 for (index = 0; (index < (int)strlen(ibuf)); index++)
388 bputc(msg, &len, ibuf[index]);
392 ival = va_arg(*ap, int);
393 sprintf(ifmt, "%%%dd", fld);
394 sprintf(ibuf, ifmt, ival);
395 for (index = 0; (index < (int)strlen(ibuf)); index++)
397 bputc(msg, &len, ibuf[index]);
401 ival = va_arg(*ap, unsigned);
402 sprintf(ifmt, "%%%du", fld);
403 sprintf(ibuf, ifmt, ival);
404 for (index = 0; (index < (int)strlen(ibuf)); index++)
406 bputc(msg, &len, ibuf[index]);
410 dval = va_arg(*ap, double);
411 sprintf(ifmt, "%%%df", fld);
412 sprintf(ibuf, ifmt, dval);
413 for (index = 0; (index < (int)strlen(ibuf)); index++)
415 bputc(msg, &len, ibuf[index]);
419 dval = va_arg(*ap, double);
420 sprintf(ifmt, "%%%dg", fld);
421 sprintf(ibuf, ifmt, dval);
422 for (index = 0; (index < (int)strlen(ibuf)); index++)
424 bputc(msg, &len, ibuf[index]);
428 cval = (char) va_arg(*ap, int); /* char is promoted to int */
429 bputc(msg, &len, cval);
432 sval = va_arg(*ap, char *);
435 sval = strdup("(null)");
437 bputs(msg, &len, sval, fld);
440 bputc(msg, &len, *p);
448 bputc(msg, &len, '\0');
451 void gmx_fatal(int f_errno, const char *file, int line, const char *fmt, ...)
458 clean_fatal_tmp_file();
460 parse_printf_args(fmt, &ap, msg);
464 tMPI_Thread_mutex_lock(&debug_mutex);
465 fatal_errno = f_errno;
466 tMPI_Thread_mutex_unlock(&debug_mutex);
468 _gmx_error("fatal", msg, file, line);
471 void gmx_fatal_collective(int f_errno, const char *file, int line,
472 const t_commrec *cr, gmx_domdec_t *dd,
473 const char *fmt, ...)
485 /* Check if we are calling on all processes in MPI_COMM_WORLD */
488 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
492 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
494 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
495 bFinalize = (result != MPI_UNEQUAL);
498 if ((cr != NULL && MASTER(cr) ) ||
499 (dd != NULL && DDMASTER(dd)))
503 clean_fatal_tmp_file();
505 parse_printf_args(fmt, &ap, msg);
509 tMPI_Thread_mutex_lock(&debug_mutex);
510 fatal_errno = f_errno;
511 tMPI_Thread_mutex_unlock(&debug_mutex);
515 /* Use an error handler that does not quit */
516 set_gmx_error_handler(quit_gmx_noquit);
519 _gmx_error("fatal", msg, file, line);
525 /* Broadcast the fatal error number possibly modified
526 * on the master process, in case the user would like
527 * to use the return status on a non-master process.
528 * The master process in cr and dd always has global rank 0.
530 MPI_Bcast(&fatal_errno, sizeof(fatal_errno), MPI_BYTE,
533 /* Finalize nicely instead of aborting */
538 /* Let all other processes wait till the master has printed
539 * the error message and issued MPI_Abort.
541 MPI_Barrier(MPI_COMM_WORLD);
548 void _invalid_case(const char *fn, int line)
550 gmx_fatal(FARGS, "Invalid case in switch statement, file %s, line %d",
554 void _unexpected_eof(const char *fn, int line, const char *srcfn, int srcline)
556 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d\n"
557 "(Source file %s, line %d)", fn, line, srcfn, srcline);
561 * These files are global variables in the gromacs preprocessor
562 * Every routine in a file that includes gmx_fatal.h can write to these
563 * debug channels. Depending on the debuglevel used
564 * 0 to 3 of these filed are redirected to /dev/null
568 gmx_bool gmx_debug_at = FALSE;
570 void init_debug(const int dbglevel, const char *dbgfile)
572 tMPI_Thread_mutex_lock(&debug_mutex);
573 if (!bDebug) /* another thread hasn't already run this*/
576 debug = gmx_fio_fopen(dbgfile, "w+");
583 tMPI_Thread_mutex_unlock(&debug_mutex);
586 #if (defined __sgi && defined USE_SGI_FPE)
587 static void user_routine(unsigned us[5], int ii[2])
589 fprintf(stderr, "User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
590 us[0], us[1], us[2], us[3], us[4], ii[0], ii[1]);
591 fprintf(stderr, "Exception encountered! Dumping core\n");
595 static void abort_routine(unsigned int **ii)
597 fprintf(stderr, "Abort routine\n");
601 static void handle_signals(int n)
603 fprintf(stderr, "Handle signals: n = %d\n", n);
604 fprintf(stderr, "Dumping core\n");
608 void doexceptions(void)
612 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
614 int onoff, en_mask, abort_action, i;
616 tMPI_Thread_mutex_lock(&debug_mutex);
618 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
619 _EN_INVALID | _EN_INT_OVERFL;
620 abort_action = _ABORT_ON_ERROR;
621 handle_sigfpes(onoff, en_mask, user_routine, abort_action, abort_routine);
623 for (i = 0; (i < asize(hs)); i++)
625 signal(hs[i], handle_signals);
627 tMPI_Thread_mutex_unlock(&debug_mutex);
629 #endif /* __sgi and FPE */
631 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
633 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
635 void set_gmx_error_handler(void (*func)(const char *msg))
637 tMPI_Thread_mutex_lock(&debug_mutex);
638 gmx_error_handler = func;
639 tMPI_Thread_mutex_unlock(&debug_mutex);
642 char *gmx_strerror(const char *key)
645 const char *key, *msg;
647 error_msg_t msg[] = {
648 { "bug", "Possible bug" },
649 { "call", "Routine should not have been called" },
650 { "comm", "Communication (parallel processing) problem" },
651 { "fatal", "Fatal error" },
652 { "cmd", "Invalid command line argument" },
653 { "file", "File input/output error" },
654 { "impl", "Implementation restriction" },
655 { "incons", "Software inconsistency error" },
656 { "input", "Input error or input inconsistency" },
657 { "mem", "Memory allocation/freeing error" },
658 { "open", "Can not open file" },
659 { "range", "Range checking error" }
661 #define NMSG asize(msg)
667 return strdup("Empty message");
671 for (i = 0; (i < NMSG); i++)
673 if (strcmp(key, msg[i].key) == 0)
680 sprintf(buf, "No error message associated with key %s\n%s", key, gmxuser);
685 return strdup(msg[i].msg);
691 void _gmx_error(const char *key, const char *msg, const char *file, int line)
693 char buf[10240], tmpbuf[1024], errerrbuf[1024];
695 const char *llines = "-------------------------------------------------------";
698 /* protect the audience from suggestive discussions */
702 sprintf(errerrbuf, "Empty fatal_error message. %s", gmxuser);
705 cool_quote(tmpbuf, 1023, &cqnum);
706 strerr = gmx_strerror(key);
707 sprintf(buf, "\n%s\nProgram %s, %s\n"
708 "Source code file: %s, line: %d\n\n"
709 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
710 "website at http://www.gromacs.org/Documentation/Errors\n%s\n\n%s\n",
711 llines, ShortProgram(), GromacsVersion(), file, line,
712 strerr, msg ? msg : errerrbuf, llines, tmpbuf);
715 gmx_error_handler(buf);
718 void _range_check(int n, int n_min, int n_max, const char *warn_str,
719 const char *var, const char *file, int line)
723 if ((n < n_min) || (n >= n_max))
725 if (warn_str != NULL)
727 strcpy(buf, warn_str);
735 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
736 "within [ %d .. %d ]\n", var, n, n_min, n_max);
738 _gmx_error("range", buf, file, line);
742 void gmx_warning(const char *fmt, ...)
749 parse_printf_args(fmt, &ap, msg);
753 fprintf(stderr, "\nWARNING: %s\n\n", msg);