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.h" /* Configuration mechanism of SimGrid */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
25 // Don't define ~xbt_ex() in ex.hpp. It is defined here to ensure that there is an unique definition of xt_ex in
26 // libsimgrid, but not in libsimgrid-java. Otherwise, sone tests are broken (seen with clang/libc++ on freebsd).
27 xbt_ex::~xbt_ex() = default;
29 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
31 xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message);
38 /** @brief shows an exception content and the associated stack if available */
39 void xbt_ex_display(xbt_ex_t * e)
41 simgrid::xbt::logException(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
44 /** \brief returns a short name for the given exception category */
45 const char *xbt_ex_catname(xbt_errcat_t cat)
49 return "unknown error";
51 return "invalid argument";
53 return "out of bounds";
59 return "system error";
61 return "network error";
65 return "action canceled";
67 return "thread error";
71 return "tracing error";
77 return "INVALID ERROR";
79 return "INVALID ERROR";
87 XBT_TEST_SUITE("xbt_ex", "Exception Handling");
89 XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow")
94 xbt_test_add("basic nested control flow");
98 xbt_test_fail("M1: n=%d (!= 1)", n);
102 xbt_test_fail("M2: n=%d (!= 2)", n);
104 THROWF(unknown_error, 0, "something");
108 xbt_test_fail("M3: n=%d (!= 3)", n);
114 xbt_test_fail("M2: n=%d (!= 5)", n);
116 THROWF(unknown_error, 0, "something");
120 xbt_test_fail("M3: n=%d (!= 6)", n);
125 xbt_test_fail("MX: n=%d (shouldn't reach this point)", n);
129 xbt_test_fail("M4: n=%d (!= 7)", n);
133 xbt_test_fail("M5: n=%d (!= 8)", n);
136 XBT_TEST_UNIT("value", test_value, "exception value passing")
139 THROWF(unknown_error, 2, "toto");
142 xbt_test_add("exception value passing");
143 if (ex.category != unknown_error)
144 xbt_test_fail("category=%d (!= 1)", (int)ex.category);
146 xbt_test_fail("value=%d (!= 2)", ex.value);
147 if (strcmp(ex.what(), "toto"))
148 xbt_test_fail("message=%s (!= toto)", ex.what());
152 XBT_TEST_UNIT("variables", test_variables, "variable value preservation")
156 XBT_ATTRIB_UNUSED int r2;
160 r1 = r2 = v1 = v2 = 1234;
164 THROWF(unknown_error, 0, "toto");
167 xbt_test_add("variable preservation");
169 xbt_test_fail("r1=%d (!= 1234)", r1);
171 xbt_test_fail("v1=%d (!= 1234)", v1);
172 /* r2 is allowed to be destroyed because not volatile */
174 xbt_test_fail("v2=%d (!= 5678)", v2);
178 XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling")
183 xbt_test_add("cleanup handling");
189 THROWF(1, 2, "blah");
193 xbt_test_fail("v1 = %d (!= 5678)", v1);
196 xbt_test_fail("v1 = %d (!= 5678)", v1);
197 if (not(ex.category == 1 && ex.value == 2 && not strcmp(ex.what(), "blah")))
198 xbt_test_fail("unexpected exception contents");
201 xbt_test_fail("xbt_ex_free not executed");
203 #endif /* SIMGRID_TEST */