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"
48 #include "gmx_fatal.h"
53 #include "gromacs/fileio/gmxfio.h"
55 #include "gromacs/utility/gmxmpi.h"
57 #include "gromacs/legacyheaders/thread_mpi/threads.h"
59 static gmx_bool bDebug = FALSE;
60 static char *fatal_tmp_file = NULL;
61 static FILE *log_file = NULL;
63 static tMPI_Thread_mutex_t debug_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
64 static tMPI_Thread_mutex_t where_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
65 static tMPI_Thread_mutex_t fatal_tmp_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
68 gmx_bool bDebugMode(void)
72 tMPI_Thread_mutex_lock(&debug_mutex);
76 tMPI_Thread_mutex_unlock(&debug_mutex);
81 void gmx_fatal_set_log_file(FILE *fp)
86 void _where(const char *file, int line)
88 static gmx_bool bFirst = TRUE;
89 static int nskip = -1;
90 static int nwhere = 0;
96 tMPI_Thread_mutex_lock(&where_mutex);
97 if (bFirst) /* we repeat the check in the locked section because things
100 if ((temp = getenv("WHERE")) != NULL)
102 nskip = strtol(temp, NULL, 10);
106 tMPI_Thread_mutex_unlock(&where_mutex);
111 /* Skip the first n occasions, this allows to see where it goes wrong */
122 fprintf(fp, "WHERE %d, file %s - line %d\n", nwhere, file, line);
128 static void bputc(char *msg, int *len, char ch)
133 static void bputs(char *msg, int *len, const char *s, int fld)
135 for (fld -= (int)strlen(s); fld > 0; fld--)
137 bputc(msg, len, ' ');
141 bputc(msg, len, *(s++));
145 static void bputd(char *msg, int *len, int d)
149 bputc(msg, len, d+'0');
153 bputc(msg, len, d-10+'a');
157 static void bputi(char *msg, int *len, int val, int radix, int fld, gmx_bool bNeg)
168 for (fld--; fld > fmax; fld--)
170 bputc(msg, len, ' ');
174 bputc(msg, len, '-');
176 bputd(msg, len, val);
182 bputc(msg, len, '-');
184 bputi(msg, len, val/radix, radix, fld-1, FALSE);
185 bputd(msg, len, val%radix);
189 static int getfld(const char **p)
196 fld = (fld*10)+((*((*p)++))-'0');
201 /*static void _halt(char *file,int line,char *reason)
203 fprintf(stderr,"\nHALT in file %s line %d because:\n\t%s\n",
209 static int fatal_errno = 0;
211 static void quit_gmx(const char *msg)
213 tMPI_Thread_mutex_lock(&debug_mutex);
214 if (fatal_errno == 0)
218 fprintf(log_file, "%s\n", msg);
220 fprintf(stderr, "%s\n", msg);
221 /* we set it to no-zero because if this function is called, something
227 if (fatal_errno != -1)
235 if (gmx_mpi_initialized())
240 nnodes = gmx_node_num();
241 noderank = gmx_node_rank();
245 fprintf(stderr, "Error on node %d, will try to stop all the nodes\n",
248 gmx_abort(noderank, nnodes, -1);
258 fprintf(stderr, "dump core (y/n):");
260 if (toupper(getc(stdin)) != 'N')
267 tMPI_Thread_mutex_unlock(&debug_mutex);
270 /* The function below should be identical to quit_gmx,
271 * except that is does not actually quit and call gmx_abort.
273 static void quit_gmx_noquit(const char *msg)
275 tMPI_Thread_mutex_lock(&debug_mutex);
280 fprintf(log_file, "%s\n", msg);
282 fprintf(stderr, "%s\n", msg);
283 /* we set it to no-zero because if this function is called, something
289 if (fatal_errno != -1)
303 fprintf(stderr, "dump core (y/n):");
305 if (toupper(getc(stdin)) != 'N')
312 tMPI_Thread_mutex_unlock(&debug_mutex);
315 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
317 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
318 if (fatal_tmp_file == NULL)
320 fatal_tmp_file = strdup(fn);
324 fprintf(stderr, "BUGWARNING: fatal_tmp_file already set at %s:%d",
327 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
330 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
332 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
333 if (strcmp(fn, fatal_tmp_file) == 0)
335 sfree(fatal_tmp_file);
336 fatal_tmp_file = NULL;
340 fprintf(stderr, "BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
343 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
346 static void clean_fatal_tmp_file()
348 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
351 fprintf(stderr, "Cleaning up temporary file %s\n", fatal_tmp_file);
352 remove(fatal_tmp_file);
353 sfree(fatal_tmp_file);
354 fatal_tmp_file = NULL;
356 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
359 static void parse_printf_args(const char *fmt, va_list *ap, char *msg)
364 char ibuf[64], ifmt[64];
365 int index, ival, fld;
369 for (p = fmt; *p; p++)
373 bputc(msg, &len, *p);
382 ival = va_arg(*ap, int);
383 sprintf(ifmt, "0x%%%dx", fld);
384 sprintf(ibuf, ifmt, (unsigned int)ival);
385 for (index = 0; (index < (int)strlen(ibuf)); index++)
387 bputc(msg, &len, ibuf[index]);
391 ival = va_arg(*ap, int);
392 sprintf(ifmt, "%%%dd", fld);
393 sprintf(ibuf, ifmt, ival);
394 for (index = 0; (index < (int)strlen(ibuf)); index++)
396 bputc(msg, &len, ibuf[index]);
400 ival = va_arg(*ap, unsigned);
401 sprintf(ifmt, "%%%du", fld);
402 sprintf(ibuf, ifmt, ival);
403 for (index = 0; (index < (int)strlen(ibuf)); index++)
405 bputc(msg, &len, ibuf[index]);
409 dval = va_arg(*ap, double);
410 sprintf(ifmt, "%%%df", fld);
411 sprintf(ibuf, ifmt, dval);
412 for (index = 0; (index < (int)strlen(ibuf)); index++)
414 bputc(msg, &len, ibuf[index]);
418 dval = va_arg(*ap, double);
419 sprintf(ifmt, "%%%dg", fld);
420 sprintf(ibuf, ifmt, dval);
421 for (index = 0; (index < (int)strlen(ibuf)); index++)
423 bputc(msg, &len, ibuf[index]);
427 cval = (char) va_arg(*ap, int); /* char is promoted to int */
428 bputc(msg, &len, cval);
431 sval = va_arg(*ap, char *);
434 sval = strdup("(null)");
436 bputs(msg, &len, sval, fld);
439 bputc(msg, &len, *p);
447 bputc(msg, &len, '\0');
450 void gmx_fatal(int f_errno, const char *file, int line, const char *fmt, ...)
457 clean_fatal_tmp_file();
459 parse_printf_args(fmt, &ap, msg);
463 tMPI_Thread_mutex_lock(&debug_mutex);
464 fatal_errno = f_errno;
465 tMPI_Thread_mutex_unlock(&debug_mutex);
467 _gmx_error("fatal", msg, file, line);
470 void gmx_fatal_collective(int f_errno, const char *file, int line,
471 const t_commrec *cr, gmx_domdec_t *dd,
472 const char *fmt, ...)
484 /* Check if we are calling on all processes in MPI_COMM_WORLD */
487 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
491 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
493 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
494 bFinalize = (result != MPI_UNEQUAL);
497 if ((cr != NULL && MASTER(cr) ) ||
498 (dd != NULL && DDMASTER(dd)))
502 clean_fatal_tmp_file();
504 parse_printf_args(fmt, &ap, msg);
508 tMPI_Thread_mutex_lock(&debug_mutex);
509 fatal_errno = f_errno;
510 tMPI_Thread_mutex_unlock(&debug_mutex);
514 /* Use an error handler that does not quit */
515 set_gmx_error_handler(quit_gmx_noquit);
518 _gmx_error("fatal", msg, file, line);
524 /* Broadcast the fatal error number possibly modified
525 * on the master process, in case the user would like
526 * to use the return status on a non-master process.
527 * The master process in cr and dd always has global rank 0.
529 MPI_Bcast(&fatal_errno, sizeof(fatal_errno), MPI_BYTE,
532 /* Finalize nicely instead of aborting */
537 /* Let all other processes wait till the master has printed
538 * the error message and issued MPI_Abort.
540 MPI_Barrier(MPI_COMM_WORLD);
547 void _invalid_case(const char *fn, int line)
549 gmx_fatal(FARGS, "Invalid case in switch statement, file %s, line %d",
553 void _unexpected_eof(const char *fn, int line, const char *srcfn, int srcline)
555 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d\n"
556 "(Source file %s, line %d)", fn, line, srcfn, srcline);
560 * These files are global variables in the gromacs preprocessor
561 * Every routine in a file that includes gmx_fatal.h can write to these
562 * debug channels. Depending on the debuglevel used
563 * 0 to 3 of these filed are redirected to /dev/null
567 gmx_bool gmx_debug_at = FALSE;
569 void init_debug(const int dbglevel, const char *dbgfile)
571 tMPI_Thread_mutex_lock(&debug_mutex);
572 if (!bDebug) /* another thread hasn't already run this*/
575 debug = gmx_fio_fopen(dbgfile, "w+");
582 tMPI_Thread_mutex_unlock(&debug_mutex);
585 #if (defined __sgi && defined USE_SGI_FPE)
586 static void user_routine(unsigned us[5], int ii[2])
588 fprintf(stderr, "User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
589 us[0], us[1], us[2], us[3], us[4], ii[0], ii[1]);
590 fprintf(stderr, "Exception encountered! Dumping core\n");
594 static void abort_routine(unsigned int **ii)
596 fprintf(stderr, "Abort routine\n");
600 static void handle_signals(int n)
602 fprintf(stderr, "Handle signals: n = %d\n", n);
603 fprintf(stderr, "Dumping core\n");
607 void doexceptions(void)
611 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
613 int onoff, en_mask, abort_action, i;
615 tMPI_Thread_mutex_lock(&debug_mutex);
617 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
618 _EN_INVALID | _EN_INT_OVERFL;
619 abort_action = _ABORT_ON_ERROR;
620 handle_sigfpes(onoff, en_mask, user_routine, abort_action, abort_routine);
622 for (i = 0; (i < asize(hs)); i++)
624 signal(hs[i], handle_signals);
626 tMPI_Thread_mutex_unlock(&debug_mutex);
628 #endif /* __sgi and FPE */
630 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
632 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
634 void set_gmx_error_handler(void (*func)(const char *msg))
636 tMPI_Thread_mutex_lock(&debug_mutex);
637 gmx_error_handler = func;
638 tMPI_Thread_mutex_unlock(&debug_mutex);
641 char *gmx_strerror(const char *key)
644 const char *key, *msg;
646 error_msg_t msg[] = {
647 { "bug", "Possible bug" },
648 { "call", "Routine should not have been called" },
649 { "comm", "Communication (parallel processing) problem" },
650 { "fatal", "Fatal error" },
651 { "cmd", "Invalid command line argument" },
652 { "file", "File input/output error" },
653 { "impl", "Implementation restriction" },
654 { "incons", "Software inconsistency error" },
655 { "input", "Input error or input inconsistency" },
656 { "mem", "Memory allocation/freeing error" },
657 { "open", "Can not open file" },
658 { "range", "Range checking error" }
660 #define NMSG asize(msg)
666 return strdup("Empty message");
670 for (i = 0; (i < NMSG); i++)
672 if (strcmp(key, msg[i].key) == 0)
679 sprintf(buf, "No error message associated with key %s\n%s", key, gmxuser);
684 return strdup(msg[i].msg);
690 void _gmx_error(const char *key, const char *msg, const char *file, int line)
692 char buf[10240], tmpbuf[1024], errerrbuf[1024];
694 const char *llines = "-------------------------------------------------------";
697 /* protect the audience from suggestive discussions */
701 sprintf(errerrbuf, "Empty fatal_error message. %s", gmxuser);
704 cool_quote(tmpbuf, 1023, &cqnum);
705 strerr = gmx_strerror(key);
706 sprintf(buf, "\n%s\nProgram %s, %s\n"
707 "Source code file: %s, line: %d\n\n"
708 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
709 "website at http://www.gromacs.org/Documentation/Errors\n%s\n\n%s\n",
710 llines, ShortProgram(), GromacsVersion(), file, line,
711 strerr, msg ? msg : errerrbuf, llines, tmpbuf);
714 gmx_error_handler(buf);
717 void _range_check(int n, int n_min, int n_max, const char *warn_str,
718 const char *var, const char *file, int line)
722 if ((n < n_min) || (n >= n_max))
724 if (warn_str != NULL)
726 strcpy(buf, warn_str);
734 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
735 "within [ %d .. %d ]\n", var, n, n_min, n_max);
737 _gmx_error("range", buf, file, line);
741 void gmx_warning(const char *fmt, ...)
748 parse_printf_args(fmt, &ap, msg);
752 fprintf(stderr, "\nWARNING: %s\n\n", msg);