Logo AND Algorithmique Numérique Distribuée

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