1 /* ex - Exception Handling */
3 /* Copyright (c) 2005-2015. The SimGrid Team.
4 * All rights reserved. */
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. */
11 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
12 /* It was heavily modified to fit the SimGrid framework. */
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/>
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/.
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
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
42 /* The extensions made for the SimGrid project can either be distributed */
43 /* under the same license, or under the LGPL v2.1 */
48 #include <xbt/backtrace.hpp>
49 #include "src/internal_config.h" /* execinfo when available */
53 #include "xbt/log.hpp"
54 #include "xbt/backtrace.h"
55 #include "xbt/backtrace.hpp"
57 #include "xbt/synchro_core.h"
58 #include "src/xbt_modinter.h" /* backtrace initialization headers */
60 #include "src/xbt/ex_interface.h"
61 #include "simgrid/sg_config.h" /* Configuration mechanism of SimGrid */
63 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
66 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
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,
74 void xbt_backtrace_display(xbt_backtrace_location_t* loc, std::size_t count)
77 std::vector<std::string> backtrace =
78 simgrid::xbt::resolveBacktrace(loc, count);
79 if (backtrace.empty()) {
80 fprintf(stderr, "(backtrace not set)\n");
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());
87 XBT_ERROR("No backtrace on this arch");
92 char* message, xbt_errcat_t errcat, int value,
93 const char* file, int line, const char* func)
95 xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message);
102 /** @brief shows an exception content and the associated stack if available */
103 void xbt_ex_display(xbt_ex_t * e)
105 simgrid::xbt::logException(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
108 /** \brief returns a short name for the given exception category */
109 const char *xbt_ex_catname(xbt_errcat_t cat)
113 return "unknown error";
115 return "invalid argument";
117 return "out of bounds";
120 case not_found_error:
123 return "system error";
125 return "network error";
129 return "action canceled";
131 return "thread error";
133 return "host failed";
135 return "tracing error";
141 return "INVALID ERROR";
147 #include <xbt/ex.hpp>
149 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
151 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
156 xbt_test_add("basic nested control flow");
160 xbt_test_fail("M1: n=%d (!= 1)", n);
164 xbt_test_fail("M2: n=%d (!= 2)", n);
166 THROWF(unknown_error, 0, "something");
170 xbt_test_fail("M3: n=%d (!= 3)", n);
176 xbt_test_fail("M2: n=%d (!= 5)", n);
178 THROWF(unknown_error, 0, "something");
182 xbt_test_fail("M3: n=%d (!= 6)", n);
187 xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
191 xbt_test_fail("M4: n=%d (!= 7)", n);
195 xbt_test_fail("M5: n=%d (!= 8)", n);
198 XBT_TEST_UNIT("value", test_value, "exception value passing")
201 THROWF(unknown_error, 2, "toto");
204 xbt_test_add("exception value passing");
205 if (ex.category != unknown_error)
206 xbt_test_fail("category=%d (!= 1)", (int)ex.category);
208 xbt_test_fail("value=%d (!= 2)", ex.value);
209 if (strcmp(ex.what(), "toto"))
210 xbt_test_fail("message=%s (!= toto)", ex.what());
214 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
218 int XBT_ATTRIB_UNUSED r2;
222 r1 = r2 = v1 = v2 = 1234;
226 THROWF(unknown_error, 0, "toto");
229 xbt_test_add("variable preservation");
231 xbt_test_fail("r1=%d (!= 1234)", r1);
233 xbt_test_fail("v1=%d (!= 1234)", v1);
234 /* r2 is allowed to be destroyed because not volatile */
236 xbt_test_fail("v2=%d (!= 5678)", v2);
240 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
245 xbt_test_add("cleanup handling");
251 THROWF(1, 2, "blah");
255 xbt_test_fail("v1 = %d (!= 5678)", v1);
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");
263 xbt_test_fail("xbt_ex_free not executed");
265 #endif /* SIMGRID_TEST */