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"
57 #include "thread_mpi.h"
60 static bool bDebug
= FALSE
;
61 static char *fatal_tmp_file
= NULL
;
62 static FILE *log_file
= NULL
;
65 static tMPI_Thread_mutex_t debug_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
66 static tMPI_Thread_mutex_t where_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
67 static tMPI_Thread_mutex_t fatal_tmp_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
74 /*#ifdef GMX_THREADS*/
76 tMPI_Thread_mutex_lock(&debug_mutex
);
79 /*#ifdef GMX_THREADS*/
81 tMPI_Thread_mutex_unlock(&debug_mutex
);
86 void gmx_fatal_set_log_file(FILE *fp
)
91 void _where(const char *file
,int line
)
93 static bool bFirst
= TRUE
;
94 static int nskip
= -1;
95 static int nwhere
= 0;
101 tMPI_Thread_mutex_lock(&where_mutex
);
102 if (bFirst
) /* we repeat the check in the locked section because things
103 might have changed */
106 if ((temp
=getenv("WHERE")) != NULL
)
107 nskip
= strtol(temp
, NULL
, 10);
111 tMPI_Thread_mutex_unlock(&where_mutex
);
117 /* Skip the first n occasions, this allows to see where it goes wrong */
118 if (nwhere
>= nskip
) {
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
--)
139 bputc(msg
,len
,*(s
++));
142 static void bputd(char *msg
,int *len
,int d
)
144 if (d
<10) bputc(msg
,len
,d
+'0'); else bputc(msg
,len
,d
-10+'a');
147 static void bputi(char *msg
,int *len
,int val
,int radix
,int fld
,bool bNeg
)
156 for (fld
--; fld
>fmax
; fld
--)
166 bputi(msg
,len
,val
/radix
,radix
,fld
-1,FALSE
);
167 bputd(msg
,len
,val
%radix
);
171 static int getfld(const char **p
)
176 while (isdigit(**p
)) fld
=(fld
*10)+((*((*p
)++))-'0');
180 /*static void _halt(char *file,int line,char *reason)
182 fprintf(stderr,"\nHALT in file %s line %d because:\n\t%s\n",
188 static int fatal_errno
= 0;
190 static void quit_gmx(const char *msg
)
193 tMPI_Thread_mutex_lock(&debug_mutex
);
198 fprintf(log_file
,"%s\n",msg
);
199 fprintf(stderr
,"%s\n",msg
);
200 /* we set it to no-zero because if this function is called, something
206 if (fatal_errno
!= -1)
212 if (gmx_parallel_env_initialized()) {
216 nnodes
= gmx_node_num();
217 noderank
= gmx_node_rank();
220 fprintf(stderr
,"Error on node %d, will try to stop all the nodes\n",
222 gmx_abort(noderank
,nnodes
,-1);
230 fprintf(stderr
,"dump core (y/n):");
232 if (toupper(getc(stdin
))!='N')
239 tMPI_Thread_mutex_unlock(&debug_mutex
);
243 void _set_fatal_tmp_file(const char *fn
, const char *file
, int line
)
246 tMPI_Thread_mutex_lock(&fatal_tmp_mutex
);
248 if (fatal_tmp_file
== NULL
)
249 fatal_tmp_file
= strdup(fn
);
251 fprintf(stderr
,"BUGWARNING: fatal_tmp_file already set at %s:%d",
254 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex
);
258 void _unset_fatal_tmp_file(const char *fn
, const char *file
, int line
)
261 tMPI_Thread_mutex_lock(&fatal_tmp_mutex
);
263 if (strcmp(fn
,fatal_tmp_file
) == 0) {
264 sfree(fatal_tmp_file
);
265 fatal_tmp_file
= NULL
;
267 fprintf(stderr
,"BUGWARNING: file %s not set as fatal_tmp_file at %s:%d",
270 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex
);
274 static void clean_fatal_tmp_file()
277 tMPI_Thread_mutex_lock(&fatal_tmp_mutex
);
279 if (fatal_tmp_file
) {
280 fprintf(stderr
,"Cleaning up temporary file %s\n",fatal_tmp_file
);
281 remove(fatal_tmp_file
);
282 sfree(fatal_tmp_file
);
283 fatal_tmp_file
= NULL
;
286 tMPI_Thread_mutex_unlock(&fatal_tmp_mutex
);
290 static void parse_printf_args(const char *fmt
,va_list *ap
,char *msg
)
295 char ibuf
[64],ifmt
[64];
312 ival
= va_arg(*ap
,int);
313 sprintf(ifmt
,"0x%%%dx",fld
);
314 sprintf(ibuf
,ifmt
,(unsigned int)ival
);
315 for(index
=0; (index
<(int)strlen(ibuf
)); index
++)
316 bputc(msg
,&len
,ibuf
[index
]);
319 ival
= va_arg(*ap
,int);
320 sprintf(ifmt
,"%%%dd",fld
);
321 sprintf(ibuf
,ifmt
,ival
);
322 for(index
=0; (index
<(int)strlen(ibuf
)); index
++)
323 bputc(msg
,&len
,ibuf
[index
]);
326 ival
= va_arg(*ap
,unsigned);
327 sprintf(ifmt
,"%%%du",fld
);
328 sprintf(ibuf
,ifmt
,ival
);
329 for(index
=0; (index
<(int)strlen(ibuf
)); index
++)
330 bputc(msg
,&len
,ibuf
[index
]);
333 dval
= va_arg(*ap
,double);
334 sprintf(ifmt
,"%%%df",fld
);
335 sprintf(ibuf
,ifmt
,dval
);
336 for(index
=0; (index
<(int)strlen(ibuf
)); index
++)
337 bputc(msg
,&len
,ibuf
[index
]);
340 dval
= va_arg(*ap
,double);
341 sprintf(ifmt
,"%%%dg",fld
);
342 sprintf(ibuf
,ifmt
,dval
);
343 for(index
=0; (index
<(int)strlen(ibuf
)); index
++)
344 bputc(msg
,&len
,ibuf
[index
]);
347 cval
= (char) va_arg(*ap
,int); /* char is promoted to int */
348 bputc(msg
,&len
,cval
);
351 sval
= va_arg(*ap
,char *);
353 sval
= strdup("(null)");
354 bputs(msg
,&len
,sval
,fld
);
365 bputc(msg
,&len
,'\0');
368 void gmx_fatal(int f_errno
,const char *file
,int line
,const char *fmt
,...)
375 clean_fatal_tmp_file();
377 parse_printf_args(fmt
,&ap
,msg
);
382 tMPI_Thread_mutex_lock(&debug_mutex
);
385 fatal_errno
= f_errno
;
388 tMPI_Thread_mutex_unlock(&debug_mutex
);
391 _gmx_error("fatal",msg
,file
,line
);
394 void gmx_fatal_collective(int f_errno
,const char *file
,int line
,
405 clean_fatal_tmp_file();
407 parse_printf_args(fmt
,&ap
,msg
);
412 tMPI_Thread_mutex_lock(&debug_mutex
);
415 fatal_errno
= f_errno
;
418 tMPI_Thread_mutex_unlock(&debug_mutex
);
421 _gmx_error("fatal",msg
,file
,line
);
425 /* Let all other processes wait till the master has printed
426 * the error message and issued MPI_Abort.
428 MPI_Barrier(MPI_COMM_WORLD
);
432 void _invalid_case(const char *fn
,int line
)
434 gmx_fatal(FARGS
,"Invalid case in switch statement, file %s, line %d",
438 void _unexpected_eof(const char *fn
,int line
,const char *srcfn
,int srcline
)
440 gmx_fatal(FARGS
,"Unexpected end of file in file %s at line %d\n"
441 "(Source file %s, line %d)",fn
,line
,srcfn
,srcline
);
445 * These files are global variables in the gromacs preprocessor
446 * Every routine in a file that includes gmx_fatal.h can write to these
447 * debug channels. Depending on the debuglevel used
448 * 0 to 3 of these filed are redirected to /dev/null
452 bool gmx_debug_at
=FALSE
;
454 void init_debug (const int dbglevel
,const char *dbgfile
)
457 tMPI_Thread_mutex_lock(&debug_mutex
);
459 if (!bDebug
) /* another thread hasn't already run this*/
462 debug
=gmx_fio_fopen(dbgfile
,"w+");
468 tMPI_Thread_mutex_unlock(&debug_mutex
);
472 #if (defined __sgi && defined USE_SGI_FPE)
473 static void user_routine(unsigned us
[5], int ii
[2])
475 fprintf(stderr
,"User routine us=(%u,%u,%u,%u,%u) ii=(%d,%d)\n",
476 us
[0],us
[1],us
[2],us
[3],us
[4],ii
[0],ii
[1]);
477 fprintf(stderr
,"Exception encountered! Dumping core\n");
481 static void abort_routine(unsigned int **ii
)
483 fprintf(stderr
,"Abort routine\n");
487 static void handle_signals(int n
)
489 fprintf(stderr
,"Handle signals: n = %d\n",n
);
490 fprintf(stderr
,"Dumping core\n");
494 void doexceptions(void)
498 int hs
[] = { SIGILL
, SIGFPE
, SIGTRAP
, SIGEMT
, SIGSYS
};
500 int onoff
,en_mask
,abort_action
,i
;
503 tMPI_Thread_mutex_lock(&debug_mutex
);
506 en_mask
= _EN_UNDERFL
| _EN_OVERFL
| _EN_DIVZERO
|
507 _EN_INVALID
| _EN_INT_OVERFL
;
508 abort_action
= _ABORT_ON_ERROR
;
509 handle_sigfpes(onoff
,en_mask
,user_routine
,abort_action
,abort_routine
);
511 for(i
=0; (i
<asize(hs
)); i
++)
512 signal(hs
[i
],handle_signals
);
514 tMPI_Thread_mutex_unlock(&debug_mutex
);
517 #endif /* __sgi and FPE */
519 static const char *gmxuser
= "Please report this to the mailing list (gmx-users@gromacs.org)";
521 static void (*gmx_error_handler
)(const char *msg
) = quit_gmx
;
523 void set_gmx_error_handler(void (*func
)(const char *msg
))
526 tMPI_Thread_mutex_lock(&debug_mutex
);
528 gmx_error_handler
= func
;
530 tMPI_Thread_mutex_unlock(&debug_mutex
);
534 char *gmx_strerror(const char *key
)
537 const char *key
,*msg
;
539 error_msg_t msg
[] = {
540 { "bug", "Possible bug" },
541 { "call", "Routine should not have been called" },
542 { "comm", "Communication (parallel processing) problem" },
543 { "fatal", "Fatal error" },
544 { "cmd", "Invalid command line argument" },
545 { "file", "File input/output error" },
546 { "impl", "Implementation restriction" },
547 { "incons", "Software inconsistency error" },
548 { "input", "Input error or input inconsistency" },
549 { "mem", "Memory allocation/freeing error" },
550 { "open", "Can not open file" },
551 { "range", "Range checking error" }
553 #define NMSG asize(msg)
558 return strdup("Empty message");
560 for(i
=0; (i
<NMSG
); i
++)
561 if (strcmp(key
,msg
[i
].key
) == 0)
564 sprintf(buf
,"No error message associated with key %s\n%s",key
,gmxuser
);
568 return strdup(msg
[i
].msg
);
573 void _gmx_error(const char *key
,const char *msg
,const char *file
,int line
)
575 char buf
[10240],tmpbuf
[1024],errerrbuf
[1024];
577 const char *llines
= "-------------------------------------------------------";
580 /* protect the audience from suggestive discussions */
584 sprintf(errerrbuf
,"Empty fatal_error message. %s",gmxuser
);
587 cool_quote(tmpbuf
,1023,&cqnum
);
588 strerr
= gmx_strerror(key
);
589 sprintf(buf
,"\n%s\nProgram %s, %s\n"
590 "Source code file: %s, line: %d\n\n"
591 "%s:\n%s\nFor more information and tips for troubleshooting, please check the GROMACS\n"
592 "website at http://www.gromacs.org/Documentation/Errors\n%s\n\n%s\n",
593 llines
,ShortProgram(),GromacsVersion(),file
,line
,
594 strerr
,msg
? msg
: errerrbuf
,llines
,tmpbuf
);
597 gmx_error_handler(buf
);
600 void _range_check(int n
,int n_min
,int n_max
,const char *warn_str
,
601 const char *var
,const char *file
,int line
)
605 if ((n
< n_min
) || (n
>= n_max
)) {
606 if (warn_str
!= NULL
) {
607 strcpy(buf
,warn_str
);
613 sprintf(buf
+strlen(buf
),"Variable %s has value %d. It should have been "
614 "within [ %d .. %d ]\n",var
,n
,n_min
,n_max
);
616 _gmx_error("range",buf
,file
,line
);
620 void gmx_warning(const char *fmt
,...)
627 parse_printf_args(fmt
,&ap
,msg
);
631 fprintf(stderr
,"\nWARNING: %s\n\n",msg
);