Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't mix stderr and stdout, it breaks windows
[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 <xbt/backtrace.hpp>
49 #include "src/internal_config.h"           /* execinfo when available */
50 #include "xbt/ex.h"
51 #include <xbt/ex.hpp>
52 #include "xbt/log.h"
53 #include "xbt/log.hpp"
54 #include "xbt/backtrace.h"
55 #include "xbt/backtrace.hpp"
56 #include "xbt/str.h"
57 #include "xbt/synchro_core.h"
58 #include "src/xbt_modinter.h"       /* backtrace initialization headers */
59
60 #include "src/xbt/ex_interface.h"
61 #include "simgrid/sg_config.h"  /* Configuration mechanism of SimGrid */
62
63 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
64
65
66 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
67
68 xbt_ex::~xbt_ex() {}
69
70 /* Change raw libc symbols to file names and line numbers */
71 void xbt_setup_backtrace(xbt_backtrace_location_t** loc, std::size_t count,
72   char** res);
73
74 void xbt_backtrace_display(xbt_backtrace_location_t* loc, std::size_t count)
75 {
76 #ifdef HAVE_BACKTRACE
77   std::vector<std::string> backtrace =
78     simgrid::xbt::resolveBacktrace(loc, count);
79   if (backtrace.empty()) {
80     fprintf(stderr, "(backtrace not set)\n");
81     return;
82   }
83   fprintf(stderr, "Backtrace (displayed in process %s):\n", SIMIX_process_self_get_name());
84   for (std::string const& s : backtrace)
85     fprintf(stderr, "---> %s\n", s.c_str());
86 #else
87   XBT_ERROR("No backtrace on this arch");
88 #endif
89 }
90
91 void xbt_throw(
92   char* message, xbt_errcat_t errcat, int value, 
93   const char* file, int line, const char* func)
94 {
95   xbt_ex e(message);
96   free(message);
97   e.category = errcat;
98   e.value = value;
99   e.file = file;
100   e.line = line;
101   e.func = func;
102   throw e;
103 }
104
105 /** @brief shows an exception content and the associated stack if available */
106 void xbt_ex_display(xbt_ex_t * e)
107 {
108   simgrid::xbt::logException(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
109 }
110
111 /** \brief returns a short name for the given exception category */
112 const char *xbt_ex_catname(xbt_errcat_t cat)
113 {
114   switch (cat) {
115   case unknown_error:
116     return "unknown error";
117   case arg_error:
118     return "invalid argument";
119   case bound_error:
120     return "out of bounds";
121   case mismatch_error:
122     return "mismatch";
123   case not_found_error:
124     return "not found";
125   case system_error:
126     return "system error";
127   case network_error:
128     return "network error";
129   case timeout_error:
130     return "timeout";
131   case cancel_error:
132     return "action canceled";
133   case thread_error:
134     return "thread error";
135   case host_error:
136     return "host failed";
137   case tracing_error:
138     return "tracing error";
139   case io_error:
140     return "io error";
141   case vm_error:
142     return "vm error";
143   }
144   return "INVALID ERROR";
145 }
146
147 #ifdef SIMGRID_TEST
148 #include <stdio.h>
149 #include "xbt/ex.h"
150 #include <xbt/ex.hpp>
151
152 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
153
154 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
155 {
156   xbt_ex_t ex;
157   int n = 1;
158
159   xbt_test_add("basic nested control flow");
160
161   try {
162     if (n != 1)
163       xbt_test_fail("M1: n=%d (!= 1)", n);
164     n++;
165     try {
166       if (n != 2)
167         xbt_test_fail("M2: n=%d (!= 2)", n);
168       n++;
169       THROWF(unknown_error, 0, "something");
170     }
171     catch (xbt_ex& ex) {
172       if (n != 3)
173         xbt_test_fail("M3: n=%d (!= 3)", n);
174       n++;
175     }
176     n++;
177     try {
178       if (n != 5)
179         xbt_test_fail("M2: n=%d (!= 5)", n);
180       n++;
181       THROWF(unknown_error, 0, "something");
182     }
183     catch(xbt_ex& ex){
184       if (n != 6)
185         xbt_test_fail("M3: n=%d (!= 6)", n);
186       n++;
187       throw;
188       n++;
189     }
190     xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
191   }
192   catch(xbt_ex& e) {
193     if (n != 7)
194       xbt_test_fail("M4: n=%d (!= 7)", n);
195     n++;
196   }
197   if (n != 8)
198     xbt_test_fail("M5: n=%d (!= 8)", n);
199 }
200
201 XBT_TEST_UNIT("value", test_value, "exception value passing")
202 {
203   try {
204     THROWF(unknown_error, 2, "toto");
205   }
206   catch (xbt_ex& ex) {
207     xbt_test_add("exception value passing");
208     if (ex.category != unknown_error)
209       xbt_test_fail("category=%d (!= 1)", (int)ex.category);
210     if (ex.value != 2)
211       xbt_test_fail("value=%d (!= 2)", ex.value);
212     if (strcmp(ex.what(), "toto"))
213       xbt_test_fail("message=%s (!= toto)", ex.what());
214   }
215 }
216
217 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
218 {
219   xbt_ex_t ex;
220   int r1;
221   int XBT_ATTRIB_UNUSED r2;
222   int v1;
223   int v2;
224
225   r1 = r2 = v1 = v2 = 1234;
226   try {
227     r2 = 5678;
228     v2 = 5678;
229     THROWF(unknown_error, 0, "toto");
230   }
231   catch(xbt_ex& e) {
232     xbt_test_add("variable preservation");
233     if (r1 != 1234)
234       xbt_test_fail("r1=%d (!= 1234)", r1);
235     if (v1 != 1234)
236       xbt_test_fail("v1=%d (!= 1234)", v1);
237     /* r2 is allowed to be destroyed because not volatile */
238     if (v2 != 5678)
239       xbt_test_fail("v2=%d (!= 5678)", v2);
240   }
241 }
242
243 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
244 {
245   int v1;
246   int c;
247
248   xbt_test_add("cleanup handling");
249
250   v1 = 1234;
251   c = 0;
252   try {
253     v1 = 5678;
254     THROWF(1, 2, "blah");
255   }
256   catch (xbt_ex& ex) {
257     if (v1 != 5678)
258       xbt_test_fail("v1 = %d (!= 5678)", v1);
259     c = 1;
260     if (v1 != 5678)
261       xbt_test_fail("v1 = %d (!= 5678)", v1);
262     if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.what(), "blah")))
263       xbt_test_fail("unexpected exception contents");
264   }
265   if (!c)
266     xbt_test_fail("xbt_ex_free not executed");
267 }
268 #endif                          /* SIMGRID_TEST */