1 /* ex - Exception Handling */
3 /* Copyright (c) 2005-2018. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <xbt/backtrace.hpp>
12 #include "src/internal_config.h" /* execinfo when available */
16 #include "xbt/log.hpp"
17 #include "xbt/backtrace.h"
18 #include "xbt/backtrace.hpp"
19 #include "src/xbt_modinter.h" /* backtrace initialization headers */
21 #include "simgrid/sg_config.hpp" /* Configuration mechanism of SimGrid */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
25 // DO NOT define ~xbt_ex() in ex.hpp.
26 // Defining it here ensures that xbt_ex is defined only in libsimgrid, but not in libsimgrid-java.
27 // Doing otherwise naturally breaks things (at least on freebsd with clang).
29 xbt_ex::~xbt_ex() = default;
31 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
33 xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message);
40 /** @brief shows an exception content and the associated stack if available */
41 void xbt_ex_display(xbt_ex_t * e)
43 simgrid::xbt::logException(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
46 /** \brief returns a short name for the given exception category */
47 const char *xbt_ex_catname(xbt_errcat_t cat)
51 return "unknown error";
53 return "invalid argument";
55 return "out of bounds";
61 return "system error";
63 return "network error";
67 return "action canceled";
69 return "thread error";
73 return "tracing error";
79 return "INVALID ERROR";
81 return "INVALID ERROR";
89 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
91 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
96 xbt_test_add("basic nested control flow");
100 xbt_test_fail("M1: n=%d (!= 1)", n);
104 xbt_test_fail("M2: n=%d (!= 2)", n);
106 THROWF(unknown_error, 0, "something");
110 xbt_test_fail("M3: n=%d (!= 3)", n);
116 xbt_test_fail("M2: n=%d (!= 5)", n);
118 THROWF(unknown_error, 0, "something");
122 xbt_test_fail("M3: n=%d (!= 6)", n);
127 xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
131 xbt_test_fail("M4: n=%d (!= 7)", n);
135 xbt_test_fail("M5: n=%d (!= 8)", n);
138 XBT_TEST_UNIT("value", test_value, "exception value passing")
141 THROWF(unknown_error, 2, "toto");
144 xbt_test_add("exception value passing");
145 if (ex.category != unknown_error)
146 xbt_test_fail("category=%d (!= 1)", (int)ex.category);
148 xbt_test_fail("value=%d (!= 2)", ex.value);
149 if (strcmp(ex.what(), "toto"))
150 xbt_test_fail("message=%s (!= toto)", ex.what());
154 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
158 XBT_ATTRIB_UNUSED int r2;
162 r1 = r2 = v1 = v2 = 1234;
166 THROWF(unknown_error, 0, "toto");
169 xbt_test_add("variable preservation");
171 xbt_test_fail("r1=%d (!= 1234)", r1);
173 xbt_test_fail("v1=%d (!= 1234)", v1);
174 /* r2 is allowed to be destroyed because not volatile */
176 xbt_test_fail("v2=%d (!= 5678)", v2);
180 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
185 xbt_test_add("cleanup handling");
191 THROWF(1, 2, "blah");
195 xbt_test_fail("v1 = %d (!= 5678)", v1);
198 xbt_test_fail("v1 = %d (!= 5678)", v1);
199 if (not(ex.category == 1 && ex.value == 2 && not strcmp(ex.what(), "blah")))
200 xbt_test_fail("unexpected exception contents");
203 xbt_test_fail("xbt_ex_free not executed");
205 #endif /* SIMGRID_TEST */