Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / xbt / ex.cpp
1 /* ex - Exception Handling                                                  */
2
3 /* Copyright (c) 2005-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /*  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>       */
7 /*  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>         */
8 /*  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>           */
9 /*  All rights reserved.                                                    */
10
11 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
12 /* It was heavily modified to fit the SimGrid framework.                    */
13
14 /* The OSSP version has the following copyright notice:
15 **  OSSP ex - Exception Handling
16 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
17 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
18 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
19 **
20 **  This file is part of OSSP ex, an exception handling library
21 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
22 **
23 **  Permission to use, copy, modify, and distribute this software for
24 **  any purpose with or without fee is hereby granted, provided that
25 **  the above copyright notice and this permission notice appear in all
26 **  copies.
27 **
28 **  THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESSED OR IMPLIED
29 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
32 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 **  SUCH DAMAGE.
40  */
41
42 /* The extensions made for the SimGrid project can either be distributed    */
43 /* under the same license, or under the LGPL v2.1                           */
44
45 #include <stdio.h>
46 #include <stdlib.h>
47
48 #include "src/internal_config.h"           /* execinfo when available */
49 #include "xbt/ex.h"
50 #include "xbt/str.h"
51 #include "xbt/synchro_core.h"
52 #include "src/xbt_modinter.h"       /* backtrace initialization headers */
53
54 #include "src/xbt/ex_interface.h"
55 #include "simgrid/sg_config.h"  /* Configuration mechanism of SimGrid */
56
57 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
58
59 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
60
61 xbt_ex::~xbt_ex() {}
62
63 /* Change raw libc symbols to file names and line numbers */
64 void xbt_ex_setup_backtrace(xbt_ex_t * e);
65
66 void xbt_backtrace_display(xbt_ex_t * e)
67 {
68   // TODO, backtrace
69 #if 0
70   xbt_ex_setup_backtrace(e);
71
72 #ifdef HAVE_BACKTRACE
73   if (e->bt_strings.empty()) {
74     fprintf(stderr, "(backtrace not set)\n");
75   } else {
76     fprintf(stderr, "Backtrace (displayed in process %s):\n", SIMIX_process_self_get_name());
77     for (std::string const& s : e->bt_strings) /* no need to display "xbt_backtrace_display" */
78       fprintf(stderr, "---> %s\n", s.c_str() + 4);
79   }
80   xbt_ex_free(*e);
81 #else
82   XBT_ERROR("No backtrace on this arch");
83 #endif
84   #endif
85 }
86
87 /** \brief show the backtrace of the current point (lovely while debuging) */
88 void xbt_backtrace_display_current(void)
89 {
90   xbt_ex_t e;
91   xbt_backtrace_current(&e);
92   xbt_backtrace_display(&e);
93 }
94
95 #if HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
96 # include "src/xbt/backtrace_linux.c"
97 #else
98 # include "src/xbt/backtrace_dummy.c"
99 #endif
100
101 void xbt_throw(
102   char* message, xbt_errcat_t errcat, int value, 
103   const char* file, int line, const char* func)
104 {
105   xbt_ex e(message);
106   free(message);
107   e.category = errcat;
108   e.value = value;
109   e.file = file;
110   e.line = line;
111   e.func = func;
112   e.procname = xbt_procname();
113   e.pid = xbt_getpid();
114   throw e;
115 }
116
117 /** @brief shows an exception content and the associated stack if available */
118 void xbt_ex_display(xbt_ex_t * e)
119 {
120   char *thrower = NULL;
121   if (e->pid != xbt_getpid())
122     thrower = bprintf(" on process %d",e->pid);
123
124   fprintf(stderr, "** SimGrid: UNCAUGHT EXCEPTION received on %s(%d): category: %s; value: %d\n"
125           "** %s\n"
126           "** Thrown by %s()%s\n",
127           xbt_binary_name, xbt_getpid(), xbt_ex_catname(e->category), e->value, e->what(),
128           e->procname.c_str(), thrower ? thrower : " in this process");
129   XBT_CRITICAL("%s", e->what());
130   xbt_free(thrower);
131
132   if (xbt_initialized==0 || smx_cleaned) {
133     fprintf(stderr, "Ouch. SimGrid is not initialized yet, or already closing. No backtrace available.\n");
134     return; /* Not started yet or already closing. Trying to generate a backtrace would probably fail */
135   }
136
137   if (e->bt_strings.empty())
138     xbt_ex_setup_backtrace(e);
139
140 #ifdef HAVE_BACKTRACE
141   if (!e->bt.empty() && !e->bt_strings.empty()) {
142     /* We have everything to build neat backtraces */
143     int cutpath = 0;
144     try { // We don't want to have an exception while checking how to deal with the one we already have, do we?
145       cutpath = xbt_cfg_get_boolean("exception/cutpath");
146     }
147     catch(xbt_ex& e) {
148       // Nothing to do
149     }
150
151     fprintf(stderr, "\n");
152     for (std::string const& s : e->bt_strings) {
153
154       if (cutpath) {
155         // TODO, backtrace
156         /*
157         char* p = e->bt_strings[i];
158         xbt_str_rtrim(p, ":0123456789");
159         char* filename = strrchr(p, '/')+1;
160         char* end_of_message  = strrchr(p, ' ');
161
162         int length = strlen(p)-strlen(end_of_message);
163         char* dest = malloc(length);
164
165         memcpy(dest, &p[0], length);
166         dest[length] = 0;
167
168         fprintf(stderr, "%s %s\n", dest, filename);
169
170         free(dest);
171         */
172       }
173       else {
174         fprintf(stderr, "%s\n", s.c_str());
175       }
176     }
177   } else
178 #endif
179     fprintf(stderr, "\n"
180         "**   In %s() at %s:%d\n"
181         "**   (no backtrace available)\n", e->func, e->file, e->line);
182 }
183
184 /** \brief returns a short name for the given exception category */
185 const char *xbt_ex_catname(xbt_errcat_t cat)
186 {
187   switch (cat) {
188   case unknown_error:
189     return "unknown error";
190   case arg_error:
191     return "invalid argument";
192   case bound_error:
193     return "out of bounds";
194   case mismatch_error:
195     return "mismatch";
196   case not_found_error:
197     return "not found";
198   case system_error:
199     return "system error";
200   case network_error:
201     return "network error";
202   case timeout_error:
203     return "timeout";
204   case cancel_error:
205     return "action canceled";
206   case thread_error:
207     return "thread error";
208   case host_error:
209     return "host failed";
210   case tracing_error:
211     return "tracing error";
212   case io_error:
213     return "io error";
214   case vm_error:
215     return "vm error";
216   }
217   return "INVALID ERROR";
218 }
219
220 #ifdef SIMGRID_TEST
221 #include <stdio.h>
222 #include "xbt/ex.h"
223
224 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
225
226 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
227 {
228   xbt_ex_t ex;
229   int n = 1;
230
231   xbt_test_add("basic nested control flow");
232
233   try {
234     if (n != 1)
235       xbt_test_fail("M1: n=%d (!= 1)", n);
236     n++;
237     try {
238       if (n != 2)
239         xbt_test_fail("M2: n=%d (!= 2)", n);
240       n++;
241       THROWF(unknown_error, 0, "something");
242     }
243     catch (xbt_ex& ex) {
244       if (n != 3)
245         xbt_test_fail("M3: n=%d (!= 3)", n);
246       n++;
247     }
248     n++;
249     try {
250       if (n != 5)
251         xbt_test_fail("M2: n=%d (!= 5)", n);
252       n++;
253       THROWF(unknown_error, 0, "something");
254     }
255     catch(xbt_ex& ex){
256       if (n != 6)
257         xbt_test_fail("M3: n=%d (!= 6)", n);
258       n++;
259       throw;
260       n++;
261     }
262     xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
263   }
264   catch(xbt_ex& e) {
265     if (n != 7)
266       xbt_test_fail("M4: n=%d (!= 7)", n);
267     n++;
268   }
269   if (n != 8)
270     xbt_test_fail("M5: n=%d (!= 8)", n);
271 }
272
273 XBT_TEST_UNIT("value", test_value, "exception value passing")
274 {
275   try {
276     THROWF(unknown_error, 2, "toto");
277   }
278   catch (xbt_ex& ex) {
279     xbt_test_add("exception value passing");
280     if (ex.category != unknown_error)
281       xbt_test_fail("category=%d (!= 1)", (int)ex.category);
282     if (ex.value != 2)
283       xbt_test_fail("value=%d (!= 2)", ex.value);
284     if (strcmp(ex.what(), "toto"))
285       xbt_test_fail("message=%s (!= toto)", ex.what());
286   }
287 }
288
289 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
290 {
291   xbt_ex_t ex;
292   int r1;
293   int XBT_ATTRIB_UNUSED r2;
294   int v1;
295   int v2;
296
297   r1 = r2 = v1 = v2 = 1234;
298   try {
299     r2 = 5678;
300     v2 = 5678;
301     THROWF(unknown_error, 0, "toto");
302   }
303   catch(xbt_ex& e) {
304     xbt_test_add("variable preservation");
305     if (r1 != 1234)
306       xbt_test_fail("r1=%d (!= 1234)", r1);
307     if (v1 != 1234)
308       xbt_test_fail("v1=%d (!= 1234)", v1);
309     /* r2 is allowed to be destroyed because not volatile */
310     if (v2 != 5678)
311       xbt_test_fail("v2=%d (!= 5678)", v2);
312   }
313 }
314
315 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
316 {
317   int v1;
318   int c;
319
320   xbt_test_add("cleanup handling");
321
322   v1 = 1234;
323   c = 0;
324   try {
325     v1 = 5678;
326     THROWF(1, 2, "blah");
327   }
328   catch (xbt_ex& ex) {
329     if (v1 != 5678)
330       xbt_test_fail("v1 = %d (!= 5678)", v1);
331     c = 1;
332     if (v1 != 5678)
333       xbt_test_fail("v1 = %d (!= 5678)", v1);
334     if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.what(), "blah")))
335       xbt_test_fail("unexpected exception contents");
336   }
337   if (!c)
338     xbt_test_fail("xbt_ex_free not executed");
339 }
340 #endif                          /* SIMGRID_TEST */