Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of 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 <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(simgrid::xbt::ThrowPoint(file, line, func), message);
96   free(message);
97   e.category = errcat;
98   e.value = value;
99   throw e;
100 }
101
102 /** @brief shows an exception content and the associated stack if available */
103 void xbt_ex_display(xbt_ex_t * e)
104 {
105   simgrid::xbt::logException(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
106 }
107
108 /** \brief returns a short name for the given exception category */
109 const char *xbt_ex_catname(xbt_errcat_t cat)
110 {
111   switch (cat) {
112   case unknown_error:
113     return "unknown error";
114   case arg_error:
115     return "invalid argument";
116   case bound_error:
117     return "out of bounds";
118   case mismatch_error:
119     return "mismatch";
120   case not_found_error:
121     return "not found";
122   case system_error:
123     return "system error";
124   case network_error:
125     return "network error";
126   case timeout_error:
127     return "timeout";
128   case cancel_error:
129     return "action canceled";
130   case thread_error:
131     return "thread error";
132   case host_error:
133     return "host failed";
134   case tracing_error:
135     return "tracing error";
136   case io_error:
137     return "io error";
138   case vm_error:
139     return "vm error";
140   }
141   return "INVALID ERROR";
142 }
143
144 #ifdef SIMGRID_TEST
145 #include <stdio.h>
146 #include "xbt/ex.h"
147 #include <xbt/ex.hpp>
148
149 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
150
151 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
152 {
153   xbt_ex_t ex;
154   int n = 1;
155
156   xbt_test_add("basic nested control flow");
157
158   try {
159     if (n != 1)
160       xbt_test_fail("M1: n=%d (!= 1)", n);
161     n++;
162     try {
163       if (n != 2)
164         xbt_test_fail("M2: n=%d (!= 2)", n);
165       n++;
166       THROWF(unknown_error, 0, "something");
167     }
168     catch (xbt_ex& ex) {
169       if (n != 3)
170         xbt_test_fail("M3: n=%d (!= 3)", n);
171       n++;
172     }
173     n++;
174     try {
175       if (n != 5)
176         xbt_test_fail("M2: n=%d (!= 5)", n);
177       n++;
178       THROWF(unknown_error, 0, "something");
179     }
180     catch(xbt_ex& ex){
181       if (n != 6)
182         xbt_test_fail("M3: n=%d (!= 6)", n);
183       n++;
184       throw;
185       n++;
186     }
187     xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
188   }
189   catch(xbt_ex& e) {
190     if (n != 7)
191       xbt_test_fail("M4: n=%d (!= 7)", n);
192     n++;
193   }
194   if (n != 8)
195     xbt_test_fail("M5: n=%d (!= 8)", n);
196 }
197
198 XBT_TEST_UNIT("value", test_value, "exception value passing")
199 {
200   try {
201     THROWF(unknown_error, 2, "toto");
202   }
203   catch (xbt_ex& ex) {
204     xbt_test_add("exception value passing");
205     if (ex.category != unknown_error)
206       xbt_test_fail("category=%d (!= 1)", (int)ex.category);
207     if (ex.value != 2)
208       xbt_test_fail("value=%d (!= 2)", ex.value);
209     if (strcmp(ex.what(), "toto"))
210       xbt_test_fail("message=%s (!= toto)", ex.what());
211   }
212 }
213
214 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
215 {
216   xbt_ex_t ex;
217   int r1;
218   int XBT_ATTRIB_UNUSED r2;
219   int v1;
220   int v2;
221
222   r1 = r2 = v1 = v2 = 1234;
223   try {
224     r2 = 5678;
225     v2 = 5678;
226     THROWF(unknown_error, 0, "toto");
227   }
228   catch(xbt_ex& e) {
229     xbt_test_add("variable preservation");
230     if (r1 != 1234)
231       xbt_test_fail("r1=%d (!= 1234)", r1);
232     if (v1 != 1234)
233       xbt_test_fail("v1=%d (!= 1234)", v1);
234     /* r2 is allowed to be destroyed because not volatile */
235     if (v2 != 5678)
236       xbt_test_fail("v2=%d (!= 5678)", v2);
237   }
238 }
239
240 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
241 {
242   int v1;
243   int c;
244
245   xbt_test_add("cleanup handling");
246
247   v1 = 1234;
248   c = 0;
249   try {
250     v1 = 5678;
251     THROWF(1, 2, "blah");
252   }
253   catch (xbt_ex& ex) {
254     if (v1 != 5678)
255       xbt_test_fail("v1 = %d (!= 5678)", v1);
256     c = 1;
257     if (v1 != 5678)
258       xbt_test_fail("v1 = %d (!= 5678)", v1);
259     if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.what(), "blah")))
260       xbt_test_fail("unexpected exception contents");
261   }
262   if (!c)
263     xbt_test_fail("xbt_ex_free not executed");
264 }
265 #endif                          /* SIMGRID_TEST */