1 /*
2 Genome-wide Efficient Mixed Model Association (GEMMA)
3 Copyright © 2011-2017, Xiang Zhou
4 Copyright © 2017, Peter Carbonetto
5 Copyright © 2017, Pjotr Prins
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef __DEBUG_H__
22 #define __DEBUG_H__
23
24 #include <assert.h>
25 #include <iostream>
26 #include <csignal>
27
28 #include "gsl/gsl_matrix.h"
29
30 void gemma_gsl_error_handler (const char * reason,
31 const char * file,
32 int line, int gsl_errno);
33
34 void debug_set_debug_mode(bool setting);
35 void debug_set_debug_data_mode(bool setting);
36 void debug_set_check_mode(bool setting);
37 void debug_set_no_check_mode(bool setting);
38 void debug_set_no_fpe_check_mode(bool setting);
39 void debug_set_strict_mode(bool setting);
40 void debug_set_quiet_mode(bool setting);
41 void debug_set_issue(uint issue);
42 void debug_set_legacy_mode(bool setting);
43
44 bool is_debug_mode();
45 bool is_debug_data_mode();
46 bool is_no_check_mode();
47 bool is_check_mode();
48 bool is_fpe_check_mode();
49 bool is_strict_mode();
50 bool is_quiet_mode();
51 bool is_issue(uint issue);
52 bool is_legacy_mode();
53
54 void enable_segfpe();
55 void disable_segfpe();
56
57 #define check_int_mult_overflow(m,n) \
58 { auto x = m * n; \
59 enforce_msg(x / m == n, "multiply integer overflow"); }
60
61 void write(const double d, const char *msg = "");
62 void write(const char *s, const char *msg = "");
63 void write(const gsl_vector *v, const char *msg = "");
64 void write(const gsl_matrix *m, const char *msg = "");
65
66 gsl_matrix *gsl_matrix_safe_alloc(size_t rows,size_t cols);
67 int gsl_matrix_safe_memcpy (gsl_matrix *dest, const gsl_matrix *src);
68 void gsl_matrix_safe_free (gsl_matrix *v);
69 void gsl_matrix_warn_free (gsl_matrix *v);
70 void do_gsl_matrix_safe_free (gsl_matrix *m, const char *__pretty_function, const char *__file, int __line, bool warn_only);
71
72 double do_gsl_matrix_safe_get (const gsl_matrix * m, const size_t i, const size_t j, const char *__pretty_function, const char *__file, int __line);
73 #define gsl_matrix_safe_get(m,i,j) do_gsl_matrix_safe_get(m, i, j,__SHOW_FUNC,__FILE__,__LINE__);
74
75 gsl_vector *gsl_vector_safe_alloc(size_t n);
76 int gsl_vector_safe_memcpy (gsl_vector *dest, const gsl_vector *src);
77 void gsl_vector_safe_free (gsl_vector *v);
78 void do_gsl_vector_safe_free (gsl_vector *v, const char *__pretty_function, const char *__file, int __line);
79
80 char *do_strtok_safe(char *tokenize, const char *delimiters, const char *__pretty_function, const char *__file, int __line, const char *infile = NULL);
81 #define strtok_safe2(string,delimiters,infile) do_strtok_safe(string,delimiters,__SHOW_FUNC,__FILE__,__LINE__,infile)
82 #define strtok_safe(string,delimiters) do_strtok_safe(string,delimiters,__SHOW_FUNC,__FILE__,__LINE__)
83
84 // Validation routines
85 void do_validate_K(const gsl_matrix *K, const char*__pretty_func, const char *__file, int __line);
86
87 #define ROUND(f) round(f * 10000.)/10000
88 #define validate_K(K) do_validate_K(K,__SHOW_FUNC,__FILE__,__LINE__)
89
90 #define warning_at_msg(__file,__line,msg) cerr << "**** WARNING: " << msg << " in " << __file << " at line " << __line << endl;
91
warnfail_at_msg(bool strict,const char * __function,const char * __file,int __line,const char * msg)92 inline void warnfail_at_msg(bool strict, const char *__function, const char *__file, int __line, const char *msg) {
93 if (strict)
94 std::cerr << "**** STRICT FAIL: ";
95 else
96 std::cerr << "**** WARNING: ";
97 std::cerr << msg << " in " << __file << " at line " << __line << " in " << __function << std::endl;
98 if (strict)
99 std::raise(SIGINT); // keep stack trace for gdb
100 }
101
fail_at_msg(const char * __file,int __line,std::string msg)102 inline void fail_at_msg(const char *__file, int __line, std::string msg) {
103 std::cerr << "**** FAILED: " << msg << " in " << __file << " at line " << __line << std::endl;
104 std::raise(SIGINT); // keep stack trace for gdb
105 }
106
107 # ifndef __ASSERT_VOID_CAST
108 # define __ASSERT_VOID_CAST (void)
109 # endif
110
fail_msg(const char * msg)111 inline void fail_msg(const char *msg) {
112 std::cerr << "**** FAILED: " << msg << std::endl;
113 std::raise(SIGINT); // keep stack trace for gdb
114 }
115
fail_msg(std::string msg)116 inline void fail_msg(std::string msg) {
117 std::cerr << "**** FAILED: " << msg << std::endl;
118 std::raise(SIGINT); // keep stack trace for gdb
119 }
120
121 #define info_msg(msg) if (!is_quiet_mode()) cerr << "**** INFO: " << msg << "." << endl;
122 #define msg(msg) info_msg(msg);
123
124 #if defined NDEBUG
125 #define __SHOW_FUNC __func__
126
127 #define warning_msg(msg) cerr << "**** WARNING: " << msg << endl;
128 #define debug_msg(msg)
129 #define assert_issue(is_issue, expr)
130
131 #else // DEBUG
132
133 #define __SHOW_FUNC __func__
134
135 #define warning_msg(msg) cerr << "**** WARNING: " << msg << " in " << __FILE__ << " at line " << __LINE__ << " in " << __func__ << endl;
136 #define debug_msg(msg) (is_debug_mode() && cerr << "**** DEBUG: " << msg << " in " << __FILE__ << " at line " << __LINE__ << " in " << __func__ << endl);
137 #define assert_issue(is_issue, expr) \
138 ((is_issue) ? enforce_msg(expr,"FAIL: ISSUE assert") : __ASSERT_VOID_CAST(0))
139
140 #endif // NDEBUG
141
142 // enforce works like assert but also when NDEBUG is set (i.e., it
143 // always works). enforce_msg prints message instead of expr
144
145 /* This prints an "Assertion failed" message and aborts. */
__enforce_fail(const char * __assertion,const char * __file,unsigned int __line,const char * __function)146 inline void __enforce_fail(const char *__assertion, const char *__file,
147 unsigned int __line,
148 const char *__function)
149 {
150 std::cout << "ERROR: Enforce failed for " << __assertion << " in " << __file << " at line " << __line << " in " << __function << std::endl;
151 std::raise(SIGINT); // keep stack trace for gdb
152 // exit(1);
153 }
154
155 #define enforce(expr) \
156 ((expr) \
157 ? __ASSERT_VOID_CAST(0) \
158 : __enforce_fail(__STRING(expr), __FILE__, __LINE__, __SHOW_FUNC))
159
160 #define enforce_msg(expr, msg) \
161 ((expr) ? __ASSERT_VOID_CAST(0) \
162 : __enforce_fail(msg, __FILE__, __LINE__, __SHOW_FUNC))
163
164 #define enforce_str(expr, msg) \
165 ((expr) \
166 ? __ASSERT_VOID_CAST(0) \
167 : __enforce_fail((msg).c_str(), __FILE__, __LINE__, __SHOW_FUNC))
168
169 #define enforce_is_int(s) \
170 enforce_str(std::regex_match(s, std::regex("^[-+]?[0-9]+$")),s + " not an integer")
171
172 #define enforce_is_float(s) \
173 enforce_str(std::regex_match(s, std::regex("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$")),s + " not a float")
174
175 // Helpers to create a unique varname per MACRO
176 #define COMBINE1(X, Y) X##Y
177 #define COMBINE(X, Y) COMBINE1(X, Y)
178
179 #define enforce_gsl(expr) \
180 auto COMBINE(res, __LINE__) = (expr); \
181 (COMBINE(res, __LINE__) == 0 \
182 ? __ASSERT_VOID_CAST(0) \
183 : __enforce_fail(gsl_strerror(COMBINE(res, __LINE__)), __FILE__, \
184 __LINE__, __SHOW_FUNC))
185
186 #define enforce_fexists(fn, msg) \
187 if (!fn.empty()) \
188 enforce_msg(stat(fn.c_str(), &fileInfo) == 0, \
189 ((std::string(__STRING(fn)) + " " + fn + ": " + msg).c_str()));
190
191 #define gsl_matrix_safe_free(m) \
192 do_gsl_matrix_safe_free(m,__SHOW_FUNC,__FILE__,__LINE__,false);
193 #define gsl_matrix_warn_free(m) \
194 do_gsl_matrix_safe_free(m,__SHOW_FUNC,__FILE__,__LINE__,true);
195 #define gsl_vector_safe_free(v) \
196 do_gsl_vector_safe_free(v,__SHOW_FUNC,__FILE__,__LINE__);
197
198 #endif
199