Logo AND Algorithmique Numérique Distribuée

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