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"
63 static gmx_bool bDebug = FALSE;
64 static char *fatal_tmp_file = NULL;
65 static FILE *log_file = NULL;
68 static tMPI_Thread_mutex_t debug_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
69 static tMPI_Thread_mutex_t where_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
70 static tMPI_Thread_mutex_t fatal_tmp_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
74 gmx_bool bDebugMode(void)
77 /*#ifdef GMX_THREAD_MPI*/
79 tMPI_Thread_mutex_lock(&debug_mutex);
82 /*#ifdef GMX_THREAD_MPI*/
84 tMPI_Thread_mutex_unlock(&debug_mutex);
89 void gmx_fatal_set_log_file(FILE *fp)
94 void _where(const char *file,int line)
96 static gmx_bool bFirst = TRUE;
97 static int nskip = -1;
98 static int nwhere = 0;
103 #ifdef GMX_THREAD_MPI
104 tMPI_Thread_mutex_lock(&where_mutex);
105 if (bFirst) /* we repeat the check in the locked section because things
106 might have changed */
109 if ((temp=getenv("WHERE")) != NULL)
110 nskip = strtol(temp, NULL, 10);
112 #ifdef GMX_THREAD_MPI
114 tMPI_Thread_mutex_unlock(&where_mutex);
120 /* Skip the first n occasions, this allows to see where it goes wrong */
121 if (nwhere >= nskip) {
126 fprintf(fp,"WHERE %d, file %s - line %d\n",nwhere,file,line);
132 static void bputc(char *msg,int *len,char ch)
137 static void bputs(char *msg,int *len,const char *s,int fld)
139 for (fld-=(int)strlen(s); fld>0; fld--)
142 bputc(msg,len,*(s++));
145 static void bputd(char *msg,int *len,int d)
147 if (d<10) bputc(msg,len,d+'0'); else bputc(msg,len,d-10+'a');
150 static void bputi(char *msg,int *len,int val,int radix,int fld,gmx_bool bNeg)
159 for (fld--; fld>fmax; fld--)
169 bputi(msg,len,val/radix,radix,fld-1,FALSE);
170 bputd(msg,len,val%radix);
174 static int getfld(const char **p)
179 while (isdigit(**p)) fld=(fld*10)+((*((*p)++))-'0');
183 /*static void _halt(char *file,int line,char *reason)
185 fprintf(stderr,"\nHALT in file %s line %d because:\n\t%s\n",
191 static int fatal_errno = 0;
193 static void quit_gmx(const char *msg)
195 #ifdef GMX_THREAD_MPI
196 tMPI_Thread_mutex_lock(&debug_mutex);
198 if (fatal_errno == 0)
202 fprintf(log_file,"%s\n",msg);
204 fprintf(stderr,"%s\n",msg);
205 /* we set it to no-zero because if this function is called, something
211 if (fatal_errno != -1)
223 nnodes = gmx_node_num();
224 noderank = gmx_node_rank();
228 fprintf(stderr,"Error on node %d, will try to stop all the nodes\n",
231 gmx_abort(noderank,nnodes,-1);
241 fprintf(stderr,"dump core (y/n):");
243 if (toupper(getc(stdin))!='N')
250 #ifdef GMX_THREAD_MPI
251 tMPI_Thread_mutex_unlock(&debug_mutex);
255 /* The function below should be identical to quit_gmx,
256 * except that is does not actually quit and call gmx_abort.
258 static void quit_gmx_noquit(const char *msg)
260 #ifdef GMX_THREAD_MPI
261 tMPI_Thread_mutex_lock(&debug_mutex);
266 fprintf(log_file,"%s\n",msg);
267 fprintf(stderr,"%s\n",msg);
268 /* we set it to no-zero because if this function is called, something
274 if (fatal_errno != -1)
286 fprintf(stderr,"dump core (y/n):");
288 if (toupper(getc(stdin))!='N')
295 #ifdef GMX_THREAD_MPI
296 tMPI_Thread_mutex_unlock(&debug_mutex);
300 void _set_fatal_tmp_file(const char *fn, const char *file, int line)
302 #ifdef GMX_THREAD_MPI
303 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
305 if (fatal_tmp_file == NULL)
306 fatal_tmp_file = strdup(fn);
308 fprintf(stderr,"BUGWARNING: fatal_tmp_file already set at %s:%d",
310 #ifdef GMX_THREAD_MPI
311 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
315 void _unset_fatal_tmp_file(const char *fn, const char *file, int line)
317 #ifdef GMX_THREAD_MPI
318 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
320 if (strcmp(fn,fatal_tmp_file) == 0) {
321 sfree(fatal_tmp_file);
322 fatal_tmp_file = NULL;
324 fprintf(stderr,"BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
326 #ifdef GMX_THREAD_MPI
327 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
331 static void clean_fatal_tmp_file()
333 #ifdef GMX_THREAD_MPI
334 tMPI_Thread_mutex_lock(&fatal_tmp_mutex);
336 if (fatal_tmp_file) {
337 fprintf(stderr,"Cleaning up temporary file %s\n",fatal_tmp_file);
338 remove(fatal_tmp_file);
339 sfree(fatal_tmp_file);
340 fatal_tmp_file = NULL;
342 #ifdef GMX_THREAD_MPI
343 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex);
347 static void parse_printf_args(const char *fmt,va_list *ap,char *msg)
352 char ibuf[64],ifmt[64];
369 ival = va_arg(*ap,int);
370 sprintf(ifmt,"0x%%%dx",fld);
371 sprintf(ibuf,ifmt,(unsigned int)ival);
372 for(index=0; (index<(int)strlen(ibuf)); index++)
373 bputc(msg,&len,ibuf[index]);
376 ival = va_arg(*ap,int);
377 sprintf(ifmt,"%%%dd",fld);
378 sprintf(ibuf,ifmt,ival);
379 for(index=0; (index<(int)strlen(ibuf)); index++)
380 bputc(msg,&len,ibuf[index]);
383 ival = va_arg(*ap,unsigned);
384 sprintf(ifmt,"%%%du",fld);
385 sprintf(ibuf,ifmt,ival);
386 for(index=0; (index<(int)strlen(ibuf)); index++)
387 bputc(msg,&len,ibuf[index]);
390 dval = va_arg(*ap,double);
391 sprintf(ifmt,"%%%df",fld);
392 sprintf(ibuf,ifmt,dval);
393 for(index=0; (index<(int)strlen(ibuf)); index++)
394 bputc(msg,&len,ibuf[index]);
397 dval = va_arg(*ap,double);
398 sprintf(ifmt,"%%%dg",fld);
399 sprintf(ibuf,ifmt,dval);
400 for(index=0; (index<(int)strlen(ibuf)); index++)
401 bputc(msg,&len,ibuf[index]);
404 cval = (char) va_arg(*ap,int); /* char is promoted to int */
405 bputc(msg,&len,cval);
408 sval = va_arg(*ap,char *);
410 sval = strdup("(null)");
411 bputs(msg,&len,sval,fld);
422 bputc(msg,&len,'\0');
425 void gmx_fatal(int f_errno,const char *file,int line,const char *fmt,...)
432 clean_fatal_tmp_file();
434 parse_printf_args(fmt,&ap,msg);
438 #ifdef GMX_THREAD_MPI
439 tMPI_Thread_mutex_lock(&debug_mutex);
442 fatal_errno = f_errno;
444 #ifdef GMX_THREAD_MPI
445 tMPI_Thread_mutex_unlock(&debug_mutex);
448 _gmx_error("fatal",msg,file,line);
451 void gmx_fatal_collective(int f_errno,const char *file,int line,
452 t_commrec *cr,gmx_domdec_t *dd,
465 /* Check if we are calling on all processes in MPI_COMM_WORLD */
468 MPI_Comm_compare(cr->mpi_comm_mysim,MPI_COMM_WORLD,&result);
472 MPI_Comm_compare(dd->mpi_comm_all,MPI_COMM_WORLD,&result);
474 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
475 bFinalize = (result != MPI_UNEQUAL);
478 if ((cr != NULL && MASTER(cr) ) ||
479 (dd != NULL && DDMASTER(dd)))
483 clean_fatal_tmp_file();
485 parse_printf_args(fmt,&ap,msg);
489 #ifdef GMX_THREAD_MPI
490 tMPI_Thread_mutex_lock(&debug_mutex);
493 fatal_errno = f_errno;
495 #ifdef GMX_THREAD_MPI
496 tMPI_Thread_mutex_unlock(&debug_mutex);
501 /* Use an error handler that does not quit */
502 set_gmx_error_handler(quit_gmx_noquit);
505 _gmx_error("fatal",msg,file,line);
511 /* Broadcast the fatal error number possibly modified
512 * on the master process, in case the user would like
513 * to use the return status on a non-master process.
514 * The master process in cr and dd always has global rank 0.
516 MPI_Bcast(&fatal_errno,sizeof(fatal_errno),MPI_BYTE,
519 /* Finalize nicely instead of aborting */
524 /* Let all other processes wait till the master has printed
525 * the error message and issued MPI_Abort.
527 MPI_Barrier(MPI_COMM_WORLD);
534 void _invalid_case(const char *fn,int line)
536 gmx_fatal(FARGS,"Invalid case in switch statement, file %s, line %d",
540 void _unexpected_eof(const char *fn,int line,const char *srcfn,int srcline)
542 gmx_fatal(FARGS,"Unexpected end of file in file %s at line %d\n"
543 "(Source file %s, line %d)",fn,line,srcfn,srcline);
547 * These files are global variables in the gromacs preprocessor
548 * Every routine in a file that includes gmx_fatal.h can write to these
549 * debug channels. Depending on the debuglevel used
550 * 0 to 3 of these filed are redirected to /dev/null
554 gmx_bool gmx_debug_at=FALSE;
556 void init_debug (const int dbglevel,const char *dbgfile)
558 #ifdef GMX_THREAD_MPI
559 tMPI_Thread_mutex_lock(&debug_mutex);
561 if (!bDebug) /* another thread hasn't already run this*/
564 debug=gmx_fio_fopen(dbgfile,"w+");
569 #ifdef GMX_THREAD_MPI
570 tMPI_Thread_mutex_unlock(&debug_mutex);
574 #if (defined __sgi && defined USE_SGI_FPE)
575 static void user_routine(unsigned us[5], int ii[2])
577 fprintf(stderr,"User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
578 us[0],us[1],us[2],us[3],us[4],ii[0],ii[1]);
579 fprintf(stderr,"Exception encountered! Dumping core\n");
583 static void abort_routine(unsigned int **ii)
585 fprintf(stderr,"Abort routine\n");
589 static void handle_signals(int n)
591 fprintf(stderr,"Handle signals: n = %d\n",n);
592 fprintf(stderr,"Dumping core\n");
596 void doexceptions(void)
600 int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS };
602 int onoff,en_mask,abort_action,i;
604 #ifdef GMX_THREAD_MPI
605 tMPI_Thread_mutex_lock(&debug_mutex);
608 en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO |
609 _EN_INVALID | _EN_INT_OVERFL;
610 abort_action = _ABORT_ON_ERROR;
611 handle_sigfpes(onoff,en_mask,user_routine,abort_action,abort_routine);
613 for(i=0; (i<asize(hs)); i++)
614 signal(hs[i],handle_signals);
615 #ifdef GMX_THREAD_MPI
616 tMPI_Thread_mutex_unlock(&debug_mutex);
619 #endif /* __sgi and FPE */
621 static const char *gmxuser = "Please report this to the mailing list (gmx-users@gromacs.org)";
623 static void (*gmx_error_handler)(const char *msg) = quit_gmx;
625 void set_gmx_error_handler(void (*func)(const char *msg))
627 #ifdef GMX_THREAD_MPI
628 tMPI_Thread_mutex_lock(&debug_mutex);
630 gmx_error_handler = func;
631 #ifdef GMX_THREAD_MPI
632 tMPI_Thread_mutex_unlock(&debug_mutex);
636 char *gmx_strerror(const char *key)
639 const char *key,*msg;
641 error_msg_t msg[] = {
642 { "bug", "Possible bug" },
643 { "call", "Routine should not have been called" },
644 { "comm", "Communication (parallel processing) problem" },
645 { "fatal", "Fatal error" },
646 { "cmd", "Invalid command line argument" },
647 { "file", "File input/output error" },
648 { "impl", "Implementation restriction" },
649 { "incons", "Software inconsistency error" },
650 { "input", "Input error or input inconsistency" },
651 { "mem", "Memory allocation/freeing error" },
652 { "open", "Can not open file" },
653 { "range", "Range checking error" }
655 #define NMSG asize(msg)
660 return strdup("Empty message");
662 for(i=0; (i<NMSG); i++)
663 if (strcmp(key,msg[i].key) == 0)
666 sprintf(buf,"No error message associated with key %s\n%s",key,gmxuser);
670 return strdup(msg[i].msg);
675 void _gmx_error(const char *key,const char *msg,const char *file,int line)
677 char buf[10240],tmpbuf[1024],errerrbuf[1024];
679 const char *llines = "-------------------------------------------------------";
682 /* protect the audience from suggestive discussions */
686 sprintf(errerrbuf,"Empty fatal_error message. %s",gmxuser);
689 cool_quote(tmpbuf,1023,&cqnum);
690 strerr = gmx_strerror(key);
691 sprintf(buf,"\n%s\nProgram %s, %s\n"
692 "Source code file: %s, line: %d\n\n"
693 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
694 "website at http://www.gromacs.org/Documentation/Errors\n%s\n\n%s\n",
695 llines,ShortProgram(),GromacsVersion(),file,line,
696 strerr,msg ? msg : errerrbuf,llines,tmpbuf);
699 gmx_error_handler(buf);
702 void _range_check(int n,int n_min,int n_max,const char *warn_str,
703 const char *var,const char *file,int line)
707 if ((n < n_min) || (n >= n_max)) {
708 if (warn_str != NULL) {
709 strcpy(buf,warn_str);
715 sprintf(buf+strlen(buf),"Variable %s has value %d. It should have been "
716 "within [ %d .. %d ]\n",var,n,n_min,n_max);
718 _gmx_error("range",buf,file,line);
722 void gmx_warning(const char *fmt,...)
729 parse_printf_args(fmt,&ap,msg);
733 fprintf(stderr,"\nWARNING: %s\n\n",msg);