Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compare file prefix only.
[simgrid.git] / src / xbt / ex.cpp
1 /* ex - Exception Handling                                                  */
2
3 /* Copyright (c) 2005-2017. The SimGrid Team. All rights reserved.          */
4
5 /*  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>       */
6 /*  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>         */
7 /*  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>           */
8 /*  All rights reserved.                                                    */
9
10 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
11 /* It was heavily modified to fit the SimGrid framework.                    */
12
13 /* The OSSP version has the following copyright notice:
14 **  OSSP ex - Exception Handling
15 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
16 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
17 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
18 **
19 **  This file is part of OSSP ex, an exception handling library
20 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
21 **
22 **  Permission to use, copy, modify, and distribute this software for
23 **  any purpose with or without fee is hereby granted, provided that
24 **  the above copyright notice and this permission notice appear in all
25 **  copies.
26 **
27 **  THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESSED OR IMPLIED
28 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
31 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
34 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 **  SUCH DAMAGE.
39  */
40
41 /* The extensions made for the SimGrid project can either be distributed    */
42 /* under the same license, or under the LGPL v2.1                           */
43
44 #include <cstdio>
45 #include <cstdlib>
46
47 #include <xbt/backtrace.hpp>
48 #include "src/internal_config.h"           /* execinfo when available */
49 #include "xbt/ex.h"
50 #include <xbt/ex.hpp>
51 #include "xbt/log.h"
52 #include "xbt/log.hpp"
53 #include "xbt/backtrace.h"
54 #include "xbt/backtrace.hpp"
55 #include "xbt/str.h"
56 #include "src/xbt_modinter.h"       /* backtrace initialization headers */
57
58 #include "simgrid/sg_config.h"  /* Configuration mechanism of SimGrid */
59
60 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
61
62 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
63 {
64   xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message);
65   free(message);
66   e.category = errcat;
67   e.value = value;
68   throw e;
69 }
70
71 /** @brief shows an exception content and the associated stack if available */
72 void xbt_ex_display(xbt_ex_t * e)
73 {
74   simgrid::xbt::logException(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
75 }
76
77 /** \brief returns a short name for the given exception category */
78 const char *xbt_ex_catname(xbt_errcat_t cat)
79 {
80   switch (cat) {
81   case unknown_error:
82     return "unknown error";
83   case arg_error:
84     return "invalid argument";
85   case bound_error:
86     return "out of bounds";
87   case mismatch_error:
88     return "mismatch";
89   case not_found_error:
90     return "not found";
91   case system_error:
92     return "system error";
93   case network_error:
94     return "network error";
95   case timeout_error:
96     return "timeout";
97   case cancel_error:
98     return "action canceled";
99   case thread_error:
100     return "thread error";
101   case host_error:
102     return "host failed";
103   case tracing_error:
104     return "tracing error";
105   case io_error:
106     return "io error";
107   case vm_error:
108     return "vm error";
109   default:
110     return "INVALID ERROR";
111   }
112   return "INVALID ERROR";
113 }
114
115 #ifdef SIMGRID_TEST
116 #include "xbt/ex.h"
117 #include <cstdio>
118 #include <xbt/ex.hpp>
119
120 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
121
122 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
123 {
124   xbt_ex_t ex;
125   int n = 1;
126
127   xbt_test_add("basic nested control flow");
128
129   try {
130     if (n != 1)
131       xbt_test_fail("M1: n=%d (!= 1)", n);
132     n++;
133     try {
134       if (n != 2)
135         xbt_test_fail("M2: n=%d (!= 2)", n);
136       n++;
137       THROWF(unknown_error, 0, "something");
138     }
139     catch (xbt_ex& ex) {
140       if (n != 3)
141         xbt_test_fail("M3: n=%d (!= 3)", n);
142       n++;
143     }
144     n++;
145     try {
146       if (n != 5)
147         xbt_test_fail("M2: n=%d (!= 5)", n);
148       n++;
149       THROWF(unknown_error, 0, "something");
150     }
151     catch(xbt_ex& ex){
152       if (n != 6)
153         xbt_test_fail("M3: n=%d (!= 6)", n);
154       n++;
155       throw;
156       n++;
157     }
158     xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
159   }
160   catch(xbt_ex& e) {
161     if (n != 7)
162       xbt_test_fail("M4: n=%d (!= 7)", n);
163     n++;
164   }
165   if (n != 8)
166     xbt_test_fail("M5: n=%d (!= 8)", n);
167 }
168
169 XBT_TEST_UNIT("value", test_value, "exception value passing")
170 {
171   try {
172     THROWF(unknown_error, 2, "toto");
173   }
174   catch (xbt_ex& ex) {
175     xbt_test_add("exception value passing");
176     if (ex.category != unknown_error)
177       xbt_test_fail("category=%d (!= 1)", (int)ex.category);
178     if (ex.value != 2)
179       xbt_test_fail("value=%d (!= 2)", ex.value);
180     if (strcmp(ex.what(), "toto"))
181       xbt_test_fail("message=%s (!= toto)", ex.what());
182   }
183 }
184
185 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
186 {
187   xbt_ex_t ex;
188   int r1;
189   int XBT_ATTRIB_UNUSED r2;
190   int v1;
191   int v2;
192
193   r1 = r2 = v1 = v2 = 1234;
194   try {
195     r2 = 5678;
196     v2 = 5678;
197     THROWF(unknown_error, 0, "toto");
198   }
199   catch(xbt_ex& e) {
200     xbt_test_add("variable preservation");
201     if (r1 != 1234)
202       xbt_test_fail("r1=%d (!= 1234)", r1);
203     if (v1 != 1234)
204       xbt_test_fail("v1=%d (!= 1234)", v1);
205     /* r2 is allowed to be destroyed because not volatile */
206     if (v2 != 5678)
207       xbt_test_fail("v2=%d (!= 5678)", v2);
208   }
209 }
210
211 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
212 {
213   int v1;
214   int c;
215
216   xbt_test_add("cleanup handling");
217
218   v1 = 1234;
219   c = 0;
220   try {
221     v1 = 5678;
222     THROWF(1, 2, "blah");
223   }
224   catch (xbt_ex& ex) {
225     if (v1 != 5678)
226       xbt_test_fail("v1 = %d (!= 5678)", v1);
227     c = 1;
228     if (v1 != 5678)
229       xbt_test_fail("v1 = %d (!= 5678)", v1);
230     if (not(ex.category == 1 && ex.value == 2 && not strcmp(ex.what(), "blah")))
231       xbt_test_fail("unexpected exception contents");
232   }
233   if (not c)
234     xbt_test_fail("xbt_ex_free not executed");
235 }
236 #endif                          /* SIMGRID_TEST */