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
49 #include "gmx_fatal.h"
55 #include "gmx_fatal_collective.h"
64 static gmx_bool bDebug = FALSE;
65 static char *fatal_tmp_file = NULL;
66 static FILE *log_file = NULL;
69 static tMPI_Thread_mutex_t debug_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
70 static tMPI_Thread_mutex_t where_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
71 static tMPI_Thread_mutex_t fatal_tmp_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
75 gmx_bool bDebugMode(void)
78 /*#ifdef GMX_THREAD_MPI*/
80 tMPI_Thread_mutex_lock(&debug_mutex);
83 /*#ifdef GMX_THREAD_MPI*/
85 tMPI_Thread_mutex_unlock(&debug_mutex);
90 void gmx_fatal_set_log_file(FILE *fp)
95 void _where(const char *file,int line)
97 static gmx_bool bFirst = TRUE;
98 static int nskip = -1;
99 static int nwhere = 0;
104 #ifdef GMX_THREAD_MPI
105 tMPI_Thread_mutex_lock(&where_mutex);
106 if (bFirst) /* we repeat the check in the locked section because things
107 might have changed */
110 if ((temp=getenv("WHERE")) != NULL)
111 nskip = strtol(temp, NULL, 10);
113 #ifdef GMX_THREAD_MPI
115 tMPI_Thread_mutex_unlock(&where_mutex);
121 /* Skip the first n occasions, this allows to see where it goes wrong */
122 if (nwhere >= nskip) {
127 fprintf(fp,"WHERE %d, file %s - line %d\n",nwhere,file,line);
133 static void bputc(char *msg,int *len,char ch)
138 static void bputs(char *msg,int *len,const char *s,int fld)
140 for (fld-=(int)strlen(s); fld>0; fld--)
143 bputc(msg,len,*(s++));
146 static void bputd(char *msg,int *len,int d)
148 if (d<10) bputc(msg,len,d+'0'); else bputc(msg,len,d-10+'a');
151 static void bputi(char *msg,int *len,int val,int radix,int fld,gmx_bool bNeg)
160 for (fld--; fld>fmax; fld--)
170 bputi(msg,len,val/radix,radix,fld-1,FALSE);
171 bputd(msg,len,val%radix);
175 static int getfld(const char **p)
180 while (isdigit(**p)) fld=(fld*10)+((*((*p)++))-'0');
184 /*static void _halt(char *file,int line,char *reason)
186 fprintf(stderr,"\nHALT in file %s line %d because:\n\t%s\n",
192 static int fatal_errno = 0;
194 static void quit_gmx(const char *msg)
196 #ifdef GMX_THREAD_MPI
197 tMPI_Thread_mutex_lock(&debug_mutex);
199 if (fatal_errno == 0)
203 fprintf(log_file,"%s\n",msg);
205 fprintf(stderr,"%s\n",msg);
206 /* we set it to no-zero because if this function is called, something
212 if (fatal_errno != -1)
220 if (gmx_mpi_initialized())
225 nnodes = gmx_node_num();
226 noderank = gmx_node_rank();
230 fprintf(stderr,"Error on node %d, will try to stop all the nodes\n",
233 gmx_abort(noderank,nnodes,-1);
243 fprintf(stderr,"dump core (y/n):");
245 if (toupper(getc(stdin))!='N')
252 #ifdef GMX_THREAD_MPI
253 tMPI_Thread_mutex_unlock(&debug_mutex);
257 /* The function below should be identical to quit_gmx,
258 * except that is does not actually quit and call gmx_abort.
260 static void quit_gmx_noquit(const char *msg)
262 #ifdef GMX_THREAD_MPI
263 tMPI_Thread_mutex_lock(&debug_mutex);
268 fprintf(log_file,"%s\n",msg);
269 fprintf(stderr,"%s\n",msg);
270 /* we set it to no-zero because if this function is called, something
276 if (fatal_errno != -1)
288 fprintf(stderr,"dump core (y/n):");
290 if (toupper(getc(stdin))!='N')
297 #ifdef GMX_THREAD_MPI
298 tMPI_Thread_mutex_unlock(&debug_mutex);
302 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
304 #ifdef GMX_THREAD_MPI
305 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
307 if (fatal_tmp_file == NULL)
308 fatal_tmp_file = strdup(fn);
310 fprintf(stderr,"BUGWARNING: fatal_tmp_file already set at %s:%d",
312 #ifdef GMX_THREAD_MPI
313 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
317 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
319 #ifdef GMX_THREAD_MPI
320 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
322 if (strcmp(fn,fatal_tmp_file) == 0) {
323 sfree(fatal_tmp_file);
324 fatal_tmp_file = NULL;
326 fprintf(stderr,"BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
328 #ifdef GMX_THREAD_MPI
329 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
333 static void clean_fatal_tmp_file()
335 #ifdef GMX_THREAD_MPI
336 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
338 if (fatal_tmp_file) {
339 fprintf(stderr,"Cleaning up temporary file %s\n",fatal_tmp_file);
340 remove(fatal_tmp_file);
341 sfree(fatal_tmp_file);
342 fatal_tmp_file = NULL;
344 #ifdef GMX_THREAD_MPI
345 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
349 static void parse_printf_args(const char *fmt,va_list *ap,char *msg)
354 char ibuf[64],ifmt[64];
371 ival = va_arg(*ap,int);
372 sprintf(ifmt,"0x%%%dx",fld);
373 sprintf(ibuf,ifmt,(unsigned int)ival);
374 for(index=0; (index<(int)strlen(ibuf)); index++)
375 bputc(msg,&len,ibuf[index]);
378 ival = va_arg(*ap,int);
379 sprintf(ifmt,"%%%dd",fld);
380 sprintf(ibuf,ifmt,ival);
381 for(index=0; (index<(int)strlen(ibuf)); index++)
382 bputc(msg,&len,ibuf[index]);
385 ival = va_arg(*ap,unsigned);
386 sprintf(ifmt,"%%%du",fld);
387 sprintf(ibuf,ifmt,ival);
388 for(index=0; (index<(int)strlen(ibuf)); index++)
389 bputc(msg,&len,ibuf[index]);
392 dval = va_arg(*ap,double);
393 sprintf(ifmt,"%%%df",fld);
394 sprintf(ibuf,ifmt,dval);
395 for(index=0; (index<(int)strlen(ibuf)); index++)
396 bputc(msg,&len,ibuf[index]);
399 dval = va_arg(*ap,double);
400 sprintf(ifmt,"%%%dg",fld);
401 sprintf(ibuf,ifmt,dval);
402 for(index=0; (index<(int)strlen(ibuf)); index++)
403 bputc(msg,&len,ibuf[index]);
406 cval = (char) va_arg(*ap,int); /* char is promoted to int */
407 bputc(msg,&len,cval);
410 sval = va_arg(*ap,char *);
412 sval = strdup("(null)");
413 bputs(msg,&len,sval,fld);
424 bputc(msg,&len,'\0');
427 void gmx_fatal(int f_errno,const char *file,int line,const char *fmt,...)
434 clean_fatal_tmp_file();
436 parse_printf_args(fmt,&ap,msg);
440 #ifdef GMX_THREAD_MPI
441 tMPI_Thread_mutex_lock(&debug_mutex);
444 fatal_errno = f_errno;
446 #ifdef GMX_THREAD_MPI
447 tMPI_Thread_mutex_unlock(&debug_mutex);
450 _gmx_error("fatal",msg,file,line);
453 void gmx_fatal_collective(int f_errno,const char *file,int line,
454 const t_commrec *cr,gmx_domdec_t *dd,
467 /* Check if we are calling on all processes in MPI_COMM_WORLD */
470 MPI_Comm_compare(cr->mpi_comm_mysim,MPI_COMM_WORLD,&result);
474 MPI_Comm_compare(dd->mpi_comm_all,MPI_COMM_WORLD,&result);
476 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
477 bFinalize = (result != MPI_UNEQUAL);
480 if ((cr != NULL && MASTER(cr) ) ||
481 (dd != NULL && DDMASTER(dd)))
485 clean_fatal_tmp_file();
487 parse_printf_args(fmt,&ap,msg);
491 #ifdef GMX_THREAD_MPI
492 tMPI_Thread_mutex_lock(&debug_mutex);
495 fatal_errno = f_errno;
497 #ifdef GMX_THREAD_MPI
498 tMPI_Thread_mutex_unlock(&debug_mutex);
503 /* Use an error handler that does not quit */
504 set_gmx_error_handler(quit_gmx_noquit);
507 _gmx_error("fatal",msg,file,line);
513 /* Broadcast the fatal error number possibly modified
514 * on the master process, in case the user would like
515 * to use the return status on a non-master process.
516 * The master process in cr and dd always has global rank 0.
518 MPI_Bcast(&fatal_errno,sizeof(fatal_errno),MPI_BYTE,
521 /* Finalize nicely instead of aborting */
526 /* Let all other processes wait till the master has printed
527 * the error message and issued MPI_Abort.
529 MPI_Barrier(MPI_COMM_WORLD);
536 void _invalid_case(const char *fn,int line)
538 gmx_fatal(FARGS,"Invalid case in switch statement, file %s, line %d",
542 void _unexpected_eof(const char *fn,int line,const char *srcfn,int srcline)
544 gmx_fatal(FARGS,"Unexpected end of file in file %s at line %d\n"
545 "(Source file %s, line %d)",fn,line,srcfn,srcline);
549 * These files are global variables in the gromacs preprocessor
550 * Every routine in a file that includes gmx_fatal.h can write to these
551 * debug channels. Depending on the debuglevel used
552 * 0 to 3 of these filed are redirected to /dev/null
556 gmx_bool gmx_debug_at=FALSE;
558 void init_debug (const int dbglevel,const char *dbgfile)
560 #ifdef GMX_THREAD_MPI
561 tMPI_Thread_mutex_lock(&debug_mutex);
563 if (!bDebug) /* another thread hasn't already run this*/
566 debug=gmx_fio_fopen(dbgfile,"w+");
571 #ifdef GMX_THREAD_MPI
572 tMPI_Thread_mutex_unlock(&debug_mutex);
576 #if (defined __sgi && defined USE_SGI_FPE)
577 static void user_routine(unsigned us[5], int ii[2])
579 fprintf(stderr,"User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
580 us[0],us[1],us[2],us[3],us[4],ii[0],ii[1]);
581 fprintf(stderr,"Exception encountered! Dumping core\n");
585 static void abort_routine(unsigned int **ii)
587 fprintf(stderr,"Abort routine\n");
591 static void handle_signals(int n)
593 fprintf(stderr,"Handle signals: n = %d\n",n);
594 fprintf(stderr,"Dumping core\n");
598 void doexceptions(void)
602 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
604 int onoff,en_mask,abort_action,i;
606 #ifdef GMX_THREAD_MPI
607 tMPI_Thread_mutex_lock(&debug_mutex);
610 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
611 _EN_INVALID | _EN_INT_OVERFL;
612 abort_action = _ABORT_ON_ERROR;
613 handle_sigfpes(onoff,en_mask,user_routine,abort_action,abort_routine);
615 for(i=0; (i<asize(hs)); i++)
616 signal(hs[i],handle_signals);
617 #ifdef GMX_THREAD_MPI
618 tMPI_Thread_mutex_unlock(&debug_mutex);
621 #endif /* __sgi and FPE */
623 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
625 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
627 void set_gmx_error_handler(void (*func)(const char *msg))
629 #ifdef GMX_THREAD_MPI
630 tMPI_Thread_mutex_lock(&debug_mutex);
632 gmx_error_handler = func;
633 #ifdef GMX_THREAD_MPI
634 tMPI_Thread_mutex_unlock(&debug_mutex);
638 char *gmx_strerror(const char *key)
641 const char *key,*msg;
643 error_msg_t msg[] = {
644 { "bug", "Possible bug" },
645 { "call", "Routine should not have been called" },
646 { "comm", "Communication (parallel processing) problem" },
647 { "fatal", "Fatal error" },
648 { "cmd", "Invalid command line argument" },
649 { "file", "File input/output error" },
650 { "impl", "Implementation restriction" },
651 { "incons", "Software inconsistency error" },
652 { "input", "Input error or input inconsistency" },
653 { "mem", "Memory allocation/freeing error" },
654 { "open", "Can not open file" },
655 { "range", "Range checking error" }
657 #define NMSG asize(msg)
662 return strdup("Empty message");
664 for(i=0; (i<NMSG); i++)
665 if (strcmp(key,msg[i].key) == 0)
668 sprintf(buf,"No error message associated with key %s\n%s",key,gmxuser);
672 return strdup(msg[i].msg);
677 void _gmx_error(const char *key,const char *msg,const char *file,int line)
679 char buf[10240],tmpbuf[1024],errerrbuf[1024];
681 const char *llines = "-------------------------------------------------------";
684 /* protect the audience from suggestive discussions */
688 sprintf(errerrbuf,"Empty fatal_error message. %s",gmxuser);
691 cool_quote(tmpbuf,1023,&cqnum);
692 strerr = gmx_strerror(key);
693 sprintf(buf,"\n%s\nProgram %s, %s\n"
694 "Source code file: %s, line: %d\n\n"
695 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
696 "website at http://www.gromacs.org/Documentation/Errors\n%s\n\n%s\n",
697 llines,ShortProgram(),GromacsVersion(),file,line,
698 strerr,msg ? msg : errerrbuf,llines,tmpbuf);
701 gmx_error_handler(buf);
704 void _range_check(int n,int n_min,int n_max,const char *warn_str,
705 const char *var,const char *file,int line)
709 if ((n < n_min) || (n >= n_max)) {
710 if (warn_str != NULL) {
711 strcpy(buf,warn_str);
717 sprintf(buf+strlen(buf),"Variable %s has value %d. It should have been "
718 "within [ %d .. %d ]\n",var,n,n_min,n_max);
720 _gmx_error("range",buf,file,line);
724 void gmx_warning(const char *fmt,...)
731 parse_printf_args(fmt,&ap,msg);
735 fprintf(stderr,"\nWARNING: %s\n\n",msg);