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) 2012,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.
39 * Declares fatal error handling and debugging routines for C code.
42 * \ingroup module_utility
44 #ifndef GMX_UTILITY_FATALERROR_H
45 #define GMX_UTILITY_FATALERROR_H
50 #include "basedefinitions.h"
57 /** For compatibility with non-clang compilers. */
58 #define __has_feature(x) 0
61 /*! \def GMX_ATTRIBUTE_NORETURN
63 * Indicate that a function is not expected to return.
66 * There are functions outside this header that need the same attribute.
67 * This could be moved to a generic header and made it affect also compiler
70 #ifndef GMX_ATTRIBUTE_NORETURN
71 #if defined(__GNUC__) || __has_feature(attribute_analyzer_noreturn)
72 #define GMX_ATTRIBUTE_NORETURN __attribute__((noreturn))
74 #define GMX_ATTRIBUTE_NORETURN
81 * Functions can write to this file for debug info.
82 * Before writing to it, it should be checked whether the file is not NULL:
86 fprintf(debug, "%s", "Debug text");
91 /** Whether extra debugging is enabled. */
92 extern gmx_bool gmx_debug_at;
95 * Initializes debugging variables.
97 * This function is not threadsafe. It should be called as part of
98 * initializing \Gromacs, before any other thread accesses the library.
99 * For command line programs, gmx::CommandLineModuleManager takes care
100 * of this if the user requests debugging.
102 void gmx_init_debug(const int dbglevel, const char *dbgfile);
104 /** Returns TRUE when the program was started in debug mode */
105 gmx_bool bDebugMode(void);
107 /** Implementation for where(). */
109 _where(const char *file, int line);
110 /** Prints filename and line to stdlog. */
111 #define where() _where(__FILE__, __LINE__)
113 /** Sets the log file for printing error messages. */
115 gmx_fatal_set_log_file(FILE *fp);
118 * Sets an error handler for gmx_fatal() and other fatal error routines.
120 * The default handler prints the message.
121 * \Gromacs will terminate the program after the error handler returns.
122 * To make gmx_fatal_collective() work, the error handler should not terminate
123 * the program, as it cannot know what is the desired way of termination.
124 * The string passed to the handler may be a multi-line string.
129 set_gmx_error_handler(void (*func)(const char *msg));
132 * Low-level fatal error reporting routine for collective MPI errors.
134 * This function works as gmx_fatal(), but provides additional control for
135 * cases where it is known that the same error occurs on multiple MPI ranks.
136 * The error handler is called only if \p bMaster is `TRUE`, and MPI_Finalize()
137 * is called instead of MPI_Abort() in MPI-enabled \Gromacs if \p bFinalize is
140 * This is used to implement gmx_fatal_collective() (which cannot be declared
141 * here, since it would bring with it mdrun-specific dependencies).
144 gmx_fatal_mpi_va(int fatal_errno, const char *file, int line, gmx_bool bMaster,
145 gmx_bool bFinalize, const char *fmt, va_list ap) GMX_ATTRIBUTE_NORETURN;
148 * Fatal error reporting routine for \Gromacs.
150 * This function prints a fatal error message with a header that contains the
151 * source file and line number of the call, followed by the string specified by
152 * \p fmt and supplied parameters.
153 * If \p fatal_errno is 0, only the message and arguments are printed.
154 * If \p fatal_errno is a legal system errno or -1, a perror()-like message is
155 * printed after the first message; if fatal_errno is -1, the last system errno
157 * The format of \p fmt uses printf()-like formatting.
159 * In case all MPI processes want to stop with the same fatal error,
160 * use gmx_fatal_collective(), declared in network.h,
161 * to avoid having as many error messages as processes.
163 * The first three parameters can be provided through ::FARGS:
165 gmx_fatal(FARGS, fmt, ...);
169 gmx_fatal(int fatal_errno, const char *file, int line, const char *fmt, ...) GMX_ATTRIBUTE_NORETURN;
170 /** Helper macro to pass first three parameters to gmx_fatal(). */
171 #define FARGS 0, __FILE__, __LINE__
174 * Returns error message corresponding to a string key.
176 * This maps the strings used by gmx_error() to actual error messages.
177 * Caller is responsible of freeing the returned string.
179 char *gmx_strerror(const char *key);
181 /** Implementation for gmx_error(). */
182 void _gmx_error(const char *key, const char *msg, const char *file, int line) GMX_ATTRIBUTE_NORETURN;
184 * Alternative fatal error routine with canned messages.
186 * This works as gmx_fatal(), except that a generic error message is added
187 * based on a string key, and printf-style formatting is not supported.
188 * Should not typically be called directly, but through gmx_bug(), gmx_call()
191 #define gmx_error(key, msg) _gmx_error(key, msg, __FILE__, __LINE__)
193 /*! \name Fatal error routines for certain types of errors
195 * These wrap gmx_error() and provide the \p key parameter as one of the
196 * recognized strings.
199 #define gmx_bug(msg) gmx_error("bug", msg)
200 #define gmx_call(msg) gmx_error("call", msg)
201 #define gmx_comm(msg) gmx_error("comm", msg)
202 #define gmx_file(msg) gmx_error("file", msg)
203 #define gmx_cmd(msg) gmx_error("cmd", msg)
204 #define gmx_impl(msg) gmx_error("impl", msg)
205 #define gmx_incons(msg) gmx_error("incons", msg)
206 #define gmx_input(msg) gmx_error("input", msg)
207 #define gmx_mem(msg) gmx_error("mem", msg)
208 #define gmx_open(fn) gmx_error("open", fn)
212 * Implementation for range_check() and range_check_mesg().
214 * \p warn_str can be NULL.
216 void _range_check(int n, int n_min, int n_max, const char *warn_str,
218 const char *file, int line);
221 * Checks that a variable is within a range.
223 * If \p n is not in range [n_min, n_max), a fatal error is raised.
224 * \p n_min is inclusive, but \p n_max is not.
226 #define range_check_mesg(n, n_min, n_max, str) _range_check(n, n_min, n_max, str,#n, __FILE__, __LINE__)
229 * Checks that a variable is within a range.
231 * This works as range_check_mesg(), but with a default error message.
233 #define range_check(n, n_min, n_max) _range_check(n, n_min, n_max, NULL,#n, __FILE__, __LINE__)
236 * Prints a warning message to stderr.
238 * The format of \p fmt uses printf()-like formatting.
239 * The message string should NOT start with "WARNING"
240 * and should NOT end with a newline.
242 void gmx_warning(const char *fmt, ...);