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,2014, 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.
48 #include "gmx_fatal.h"
54 #include "gromacs/fileio/futil.h"
55 #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 int fatal_errno = 0;
131 static void quit_gmx(const char *msg)
133 tMPI_Thread_mutex_lock(&debug_mutex);
134 if (fatal_errno == 0)
138 fprintf(log_file, "%s\n", msg);
140 fprintf(stderr, "%s\n", msg);
141 /* we set it to no-zero because if this function is called, something
147 if (fatal_errno != -1)
155 if (gmx_mpi_initialized())
160 nnodes = gmx_node_num();
161 noderank = gmx_node_rank();
165 fprintf(stderr, "Error on node %d, will try to stop all the nodes\n",
168 gmx_abort(noderank, nnodes, -1);
178 fprintf(stderr, "dump core (y/n):");
180 if (toupper(getc(stdin)) != 'N')
187 tMPI_Thread_mutex_unlock(&debug_mutex);
190 /* The function below should be identical to quit_gmx,
191 * except that is does not actually quit and call gmx_abort.
193 static void quit_gmx_noquit(const char *msg)
195 tMPI_Thread_mutex_lock(&debug_mutex);
200 fprintf(log_file, "%s\n", msg);
202 fprintf(stderr, "%s\n", msg);
203 /* we set it to no-zero because if this function is called, something
209 if (fatal_errno != -1)
223 fprintf(stderr, "dump core (y/n):");
225 if (toupper(getc(stdin)) != 'N')
232 tMPI_Thread_mutex_unlock(&debug_mutex);
235 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
237 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
238 if (fatal_tmp_file == NULL)
240 fatal_tmp_file = strdup(fn);
244 fprintf(stderr, "BUGWARNING: fatal_tmp_file already set at %s:%d",
247 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
250 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
252 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
253 if (strcmp(fn, fatal_tmp_file) == 0)
255 sfree(fatal_tmp_file);
256 fatal_tmp_file = NULL;
260 fprintf(stderr, "BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
263 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
266 static void clean_fatal_tmp_file()
268 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
271 fprintf(stderr, "Cleaning up temporary file %s\n", fatal_tmp_file);
272 remove(fatal_tmp_file);
273 sfree(fatal_tmp_file);
274 fatal_tmp_file = NULL;
276 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
279 void gmx_fatal(int f_errno, const char *file, int line, const char *fmt, ...)
284 clean_fatal_tmp_file();
287 vsprintf(msg, fmt, ap);
290 tMPI_Thread_mutex_lock(&debug_mutex);
291 fatal_errno = f_errno;
292 tMPI_Thread_mutex_unlock(&debug_mutex);
294 _gmx_error("fatal", msg, file, line);
297 void gmx_fatal_collective(int f_errno, const char *file, int line,
298 const t_commrec *cr, gmx_domdec_t *dd,
299 const char *fmt, ...)
311 /* Check if we are calling on all processes in MPI_COMM_WORLD */
314 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
318 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
320 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
321 bFinalize = (result != MPI_UNEQUAL);
324 if ((cr != NULL && MASTER(cr) ) ||
325 (dd != NULL && DDMASTER(dd)))
327 clean_fatal_tmp_file();
330 vsprintf(msg, fmt, ap);
333 tMPI_Thread_mutex_lock(&debug_mutex);
334 fatal_errno = f_errno;
335 tMPI_Thread_mutex_unlock(&debug_mutex);
339 /* Use an error handler that does not quit */
340 set_gmx_error_handler(quit_gmx_noquit);
343 _gmx_error("fatal", msg, file, line);
349 /* Broadcast the fatal error number possibly modified
350 * on the master process, in case the user would like
351 * to use the return status on a non-master process.
352 * The master process in cr and dd always has global rank 0.
354 MPI_Bcast(&fatal_errno, sizeof(fatal_errno), MPI_BYTE,
357 /* Finalize nicely instead of aborting */
362 /* Let all other processes wait till the master has printed
363 * the error message and issued MPI_Abort.
365 MPI_Barrier(MPI_COMM_WORLD);
372 void _invalid_case(const char *fn, int line)
374 gmx_fatal(FARGS, "Invalid case in switch statement, file %s, line %d",
378 void _unexpected_eof(const char *fn, int line, const char *srcfn, int srcline)
380 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d\n"
381 "(Source file %s, line %d)", fn, line, srcfn, srcline);
385 * These files are global variables in the gromacs preprocessor
386 * Every routine in a file that includes gmx_fatal.h can write to these
387 * debug channels. Depending on the debuglevel used
388 * 0 to 3 of these filed are redirected to /dev/null
392 gmx_bool gmx_debug_at = FALSE;
394 void init_debug(const int dbglevel, const char *dbgfile)
396 tMPI_Thread_mutex_lock(&debug_mutex);
397 if (!bDebug) /* another thread hasn't already run this*/
400 debug = gmx_fio_fopen(dbgfile, "w+");
407 tMPI_Thread_mutex_unlock(&debug_mutex);
410 #if (defined __sgi && defined USE_SGI_FPE)
411 static void user_routine(unsigned us[5], int ii[2])
413 fprintf(stderr, "User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
414 us[0], us[1], us[2], us[3], us[4], ii[0], ii[1]);
415 fprintf(stderr, "Exception encountered! Dumping core\n");
419 static void abort_routine(unsigned int **ii)
421 fprintf(stderr, "Abort routine\n");
425 static void handle_signals(int n)
427 fprintf(stderr, "Handle signals: n = %d\n", n);
428 fprintf(stderr, "Dumping core\n");
432 void doexceptions(void)
436 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
438 int onoff, en_mask, abort_action, i;
440 tMPI_Thread_mutex_lock(&debug_mutex);
442 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
443 _EN_INVALID | _EN_INT_OVERFL;
444 abort_action = _ABORT_ON_ERROR;
445 handle_sigfpes(onoff, en_mask, user_routine, abort_action, abort_routine);
447 for (i = 0; (i < asize(hs)); i++)
449 signal(hs[i], handle_signals);
451 tMPI_Thread_mutex_unlock(&debug_mutex);
453 #endif /* __sgi and FPE */
455 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
457 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
459 void set_gmx_error_handler(void (*func)(const char *msg))
461 tMPI_Thread_mutex_lock(&debug_mutex);
462 gmx_error_handler = func;
463 tMPI_Thread_mutex_unlock(&debug_mutex);
466 char *gmx_strerror(const char *key)
469 const char *key, *msg;
471 error_msg_t msg[] = {
472 { "bug", "Possible bug" },
473 { "call", "Routine should not have been called" },
474 { "comm", "Communication (parallel processing) problem" },
475 { "fatal", "Fatal error" },
476 { "cmd", "Invalid command line argument" },
477 { "file", "File input/output error" },
478 { "impl", "Implementation restriction" },
479 { "incons", "Software inconsistency error" },
480 { "input", "Input error or input inconsistency" },
481 { "mem", "Memory allocation/freeing error" },
482 { "open", "Can not open file" },
483 { "range", "Range checking error" }
485 #define NMSG asize(msg)
491 return strdup("Empty message");
495 for (i = 0; (i < NMSG); i++)
497 if (strcmp(key, msg[i].key) == 0)
504 sprintf(buf, "No error message associated with key %s\n%s", key, gmxuser);
509 return strdup(msg[i].msg);
515 void _gmx_error(const char *key, const char *msg, const char *file, int line)
517 char buf[10240], errerrbuf[1024];
518 const char *llines = "-------------------------------------------------------";
521 /* protect the audience from suggestive discussions */
525 sprintf(errerrbuf, "Empty fatal_error message. %s", gmxuser);
528 strerr = gmx_strerror(key);
529 sprintf(buf, "\n%s\nProgram %s, %s\n"
530 "Source code file: %s, line: %d\n\n"
531 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
532 "website at http://www.gromacs.org/Documentation/Errors\n%s\n",
533 llines, ShortProgram(), GromacsVersion(), file, line,
534 strerr, msg ? msg : errerrbuf, llines);
537 gmx_error_handler(buf);
540 void _range_check(int n, int n_min, int n_max, const char *warn_str,
541 const char *var, const char *file, int line)
545 if ((n < n_min) || (n >= n_max))
547 if (warn_str != NULL)
549 strcpy(buf, warn_str);
557 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
558 "within [ %d .. %d ]\n", var, n, n_min, n_max);
560 _gmx_error("range", buf, file, line);
564 void gmx_warning(const char *fmt, ...)
570 vsprintf(msg, fmt, ap);
573 fprintf(stderr, "\nWARNING: %s\n\n", msg);