Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the backtrace code out of the exception code
[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/backtrace.h"
51 #include "xbt/str.h"
52 #include "xbt/synchro_core.h"
53 #include "src/xbt_modinter.h"       /* backtrace initialization headers */
54
55 #include "src/xbt/ex_interface.h"
56 #include "simgrid/sg_config.h"  /* Configuration mechanism of SimGrid */
57
58 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
59
60 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
61
62 xbt_ex::~xbt_ex() {}
63
64 /* Change raw libc symbols to file names and line numbers */
65 void xbt_setup_backtrace(xbt_backtrace_location_t** loc, std::size_t count,
66   char** res);
67
68 void xbt_backtrace_display(xbt_backtrace_location_t* loc, std::size_t count)
69 {
70 #ifdef HAVE_BACKTRACE
71   std::vector<std::string> backtrace =
72     simgrid::xbt::resolveBacktrace(loc, count);
73   if (backtrace.empty()) {
74     fprintf(stderr, "(backtrace not set)\n");
75     return;
76   }
77   fprintf(stderr, "Backtrace (displayed in process %s):\n", SIMIX_process_self_get_name());
78   for (std::string const& s : backtrace)
79     fprintf(stderr, "---> %s\n", s.c_str());
80 #else
81   XBT_ERROR("No backtrace on this arch");
82 #endif
83 }
84
85 void xbt_throw(
86   char* message, xbt_errcat_t errcat, int value, 
87   const char* file, int line, const char* func)
88 {
89   xbt_ex e(message);
90   free(message);
91   e.category = errcat;
92   e.value = value;
93   e.file = file;
94   e.line = line;
95   e.func = func;
96   e.procname = xbt_procname();
97   e.pid = xbt_getpid();
98   throw e;
99 }
100
101 /** @brief shows an exception content and the associated stack if available */
102 void xbt_ex_display(xbt_ex_t * e)
103 {
104   char *thrower = NULL;
105   if (e->pid != xbt_getpid())
106     thrower = bprintf(" on process %d",e->pid);
107
108   std::fprintf(stderr, "** SimGrid: UNCAUGHT EXCEPTION received on %s(%d): category: %s; value: %d\n"
109           "** %s\n"
110           "** Thrown by %s()%s\n",
111           xbt_binary_name, xbt_getpid(), xbt_ex_catname(e->category), e->value, e->what(),
112           e->procname.c_str(), thrower ? thrower : " in this process");
113   XBT_CRITICAL("%s", e->what());
114   xbt_free(thrower);
115
116   if (xbt_initialized==0 || smx_cleaned) {
117     fprintf(stderr, "Ouch. SimGrid is not initialized yet, or already closing. No backtrace available.\n");
118     return; /* Not started yet or already closing. Trying to generate a backtrace would probably fail */
119   }
120
121   std::vector<std::string> backtrace = simgrid::xbt::resolveBacktrace(
122     e->bt.data(), e->bt.size());
123
124 #ifdef HAVE_BACKTRACE
125   if (!backtrace.empty()) {
126     /* We have everything to build neat backtraces */
127     int cutpath = 0;
128     try { // We don't want to have an exception while checking how to deal with the one we already have, do we?
129       cutpath = xbt_cfg_get_boolean("exception/cutpath");
130     }
131     catch(xbt_ex& e) {
132       // Nothing to do
133     }
134
135     std::fprintf(stderr, "\n");
136     for (std::string const& s : backtrace) {
137
138       // TODO, move this logic into solveBacktrace
139       if (cutpath) {
140         // TODO, simplify this
141         char* p = xbt_strdup(s.c_str());
142         xbt_str_rtrim(p, ":0123456789");
143         char* filename = strrchr(p, '/')+1;
144         char* end_of_message  = strrchr(p, ' ');
145         int length = strlen(p)-strlen(end_of_message);
146         char* dest = (char*) std::malloc(length);
147         std::memcpy(dest, &p[0], length);
148         dest[length] = 0;
149         std::fprintf(stderr, "%s %s\n", dest, filename);
150         std::free(dest);
151         std::free(p);
152       }
153       else {
154         std::fprintf(stderr, "%s\n", s.c_str());
155       }
156     }
157   } else
158 #endif
159     std::fprintf(stderr, "\n"
160         "**   In %s() at %s:%d\n"
161         "**   (no backtrace available)\n", e->func, e->file, e->line);
162 }
163
164 /** \brief returns a short name for the given exception category */
165 const char *xbt_ex_catname(xbt_errcat_t cat)
166 {
167   switch (cat) {
168   case unknown_error:
169     return "unknown error";
170   case arg_error:
171     return "invalid argument";
172   case bound_error:
173     return "out of bounds";
174   case mismatch_error:
175     return "mismatch";
176   case not_found_error:
177     return "not found";
178   case system_error:
179     return "system error";
180   case network_error:
181     return "network error";
182   case timeout_error:
183     return "timeout";
184   case cancel_error:
185     return "action canceled";
186   case thread_error:
187     return "thread error";
188   case host_error:
189     return "host failed";
190   case tracing_error:
191     return "tracing error";
192   case io_error:
193     return "io error";
194   case vm_error:
195     return "vm error";
196   }
197   return "INVALID ERROR";
198 }
199
200 #ifdef SIMGRID_TEST
201 #include <stdio.h>
202 #include "xbt/ex.h"
203
204 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
205
206 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
207 {
208   xbt_ex_t ex;
209   int n = 1;
210
211   xbt_test_add("basic nested control flow");
212
213   try {
214     if (n != 1)
215       xbt_test_fail("M1: n=%d (!= 1)", n);
216     n++;
217     try {
218       if (n != 2)
219         xbt_test_fail("M2: n=%d (!= 2)", n);
220       n++;
221       THROWF(unknown_error, 0, "something");
222     }
223     catch (xbt_ex& ex) {
224       if (n != 3)
225         xbt_test_fail("M3: n=%d (!= 3)", n);
226       n++;
227     }
228     n++;
229     try {
230       if (n != 5)
231         xbt_test_fail("M2: n=%d (!= 5)", n);
232       n++;
233       THROWF(unknown_error, 0, "something");
234     }
235     catch(xbt_ex& ex){
236       if (n != 6)
237         xbt_test_fail("M3: n=%d (!= 6)", n);
238       n++;
239       throw;
240       n++;
241     }
242     xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
243   }
244   catch(xbt_ex& e) {
245     if (n != 7)
246       xbt_test_fail("M4: n=%d (!= 7)", n);
247     n++;
248   }
249   if (n != 8)
250     xbt_test_fail("M5: n=%d (!= 8)", n);
251 }
252
253 XBT_TEST_UNIT("value", test_value, "exception value passing")
254 {
255   try {
256     THROWF(unknown_error, 2, "toto");
257   }
258   catch (xbt_ex& ex) {
259     xbt_test_add("exception value passing");
260     if (ex.category != unknown_error)
261       xbt_test_fail("category=%d (!= 1)", (int)ex.category);
262     if (ex.value != 2)
263       xbt_test_fail("value=%d (!= 2)", ex.value);
264     if (strcmp(ex.what(), "toto"))
265       xbt_test_fail("message=%s (!= toto)", ex.what());
266   }
267 }
268
269 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
270 {
271   xbt_ex_t ex;
272   int r1;
273   int XBT_ATTRIB_UNUSED r2;
274   int v1;
275   int v2;
276
277   r1 = r2 = v1 = v2 = 1234;
278   try {
279     r2 = 5678;
280     v2 = 5678;
281     THROWF(unknown_error, 0, "toto");
282   }
283   catch(xbt_ex& e) {
284     xbt_test_add("variable preservation");
285     if (r1 != 1234)
286       xbt_test_fail("r1=%d (!= 1234)", r1);
287     if (v1 != 1234)
288       xbt_test_fail("v1=%d (!= 1234)", v1);
289     /* r2 is allowed to be destroyed because not volatile */
290     if (v2 != 5678)
291       xbt_test_fail("v2=%d (!= 5678)", v2);
292   }
293 }
294
295 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
296 {
297   int v1;
298   int c;
299
300   xbt_test_add("cleanup handling");
301
302   v1 = 1234;
303   c = 0;
304   try {
305     v1 = 5678;
306     THROWF(1, 2, "blah");
307   }
308   catch (xbt_ex& ex) {
309     if (v1 != 5678)
310       xbt_test_fail("v1 = %d (!= 5678)", v1);
311     c = 1;
312     if (v1 != 5678)
313       xbt_test_fail("v1 = %d (!= 5678)", v1);
314     if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.what(), "blah")))
315       xbt_test_fail("unexpected exception contents");
316   }
317   if (!c)
318     xbt_test_fail("xbt_ex_free not executed");
319 }
320 #endif                          /* SIMGRID_TEST */