From: Arnaud Giersch Date: Tue, 2 Jul 2019 13:47:05 +0000 (+0200) Subject: Deprecate legacy xbt_ex; kill xbt_errcat_t. X-Git-Tag: v3.23.2~34 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/ad455a31635e6f71bb8dd5c06d85657812c783bd Deprecate legacy xbt_ex; kill xbt_errcat_t. --- diff --git a/include/simgrid/Exception.hpp b/include/simgrid/Exception.hpp index a6d40e8bae..200eb19cdc 100644 --- a/include/simgrid/Exception.hpp +++ b/include/simgrid/Exception.hpp @@ -78,124 +78,80 @@ public: : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint)) { } + ~Exception(); // DO NOT define it here -- see Exception.cpp for a rationale /** Return the information about where the exception was thrown */ xbt::ThrowPoint const& throw_point() const { return throwpoint_; } std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); } + /** Allow to carry a value (used by waitall/waitany) */ + int value = 0; + private: xbt::ThrowPoint throwpoint_; }; -} // namespace simgrid - -/** A legacy exception - * - * It is defined by a category and a value within that category (as well as - * an optional error message). - * - * This used to be a structure for C exceptions but it has been retrofitted - * as a C++ exception and some of its data has been moved in the - * @ref WithContextException base class. We should deprecate it and replace it - * with either C++ different exceptions or `std::system_error` which already - * provides this (category + error code) logic. - * TODO ^^ - * - * @ingroup XBT_ex_c - */ -class XBT_PUBLIC xbt_ex : public simgrid::Exception { -public: - /** - * - * @param throwpoint Throw point (use XBT_THROW_POINT) - * @param message Exception message - */ - xbt_ex(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : simgrid::Exception(std::move(throwpoint), std::move(message)) - { - } - - xbt_ex(const xbt_ex&) = default; - - ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale - - /** Category (what went wrong) */ - xbt_errcat_t category = unknown_error; - - /** Why did it went wrong */ - int value = 0; -}; - -namespace simgrid { - /** Exception raised when a timeout elapsed */ -class TimeoutError : public xbt_ex { +class TimeoutError : public Exception { public: TimeoutError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = timeout_error; } }; /** Exception raised when a host fails */ -class HostFailureException : public xbt_ex { +class HostFailureException : public Exception { public: HostFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = host_error; } }; /** Exception raised when a communication fails because of the network or because of the remote host */ -class NetworkFailureException : public xbt_ex { +class NetworkFailureException : public Exception { public: NetworkFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = network_error; } }; /** Exception raised when a storage fails */ -class StorageFailureException : public xbt_ex { +class StorageFailureException : public Exception { public: StorageFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = io_error; } }; /** Exception raised when a VM fails */ -class VmFailureException : public xbt_ex { +class VmFailureException : public Exception { public: VmFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = vm_error; } }; /** Exception raised when something got canceled before completion */ -class CancelException : public xbt_ex { +class CancelException : public Exception { public: CancelException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = cancel_error; } }; /** Exception raised when something is going wrong during the simulation tracing */ -class TracingError : public xbt_ex { +class TracingError : public Exception { public: TracingError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message) - : xbt_ex(std::move(throwpoint), std::move(message)) + : Exception(std::move(throwpoint), std::move(message)) { - category = tracing_error; } }; @@ -239,4 +195,6 @@ private: } // namespace simgrid +XBT_ATTRIB_DEPRECATED_v327("Please use simgrid::Exception") typedef simgrid::Exception xbt_ex; + #endif diff --git a/include/xbt/ex.h b/include/xbt/ex.h index c37ff4719a..0cbbb9da8e 100644 --- a/include/xbt/ex.h +++ b/include/xbt/ex.h @@ -21,52 +21,22 @@ * exactly play nicely together. */ -/** Categories of errors - * - * This very similar to std::error_catgory and should probably be replaced - * by this in the future. - * - * @ingroup XBT_ex_c - */ -typedef enum { - unknown_error = 0, /**< unknown error */ - arg_error, /**< Invalid argument */ - bound_error, /**< Out of bounds argument */ - mismatch_error, /**< The provided ID does not match */ - not_found_error, /**< The searched element was not found */ - system_error, /**< a syscall did fail */ - network_error, /**< error while sending/receiving data */ - timeout_error, /**< not quick enough, dude */ - cancel_error, /**< an action was canceled */ - thread_error, /**< error while [un]locking */ - host_error, /**< host failed */ - tracing_error, /**< error during the simulation tracing */ - io_error, /**< disk or file error */ - vm_error /**< vm error */ -} xbt_errcat_t; - SG_BEGIN_DECL() -/** Get the name of a category - * @ingroup XBT_ex_c - */ -XBT_PUBLIC const char* xbt_ex_catname(xbt_errcat_t cat); - /** Helper function used to throw exceptions in C */ -XBT_ATTRIB_NORETURN XBT_PUBLIC void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, - int line, const char* func); +XBT_ATTRIB_NORETURN XBT_PUBLIC void _xbt_throw(char* message, int value, const char* file, int line, const char* func); /** Builds and throws an exception * @ingroup XBT_ex_c * @hideinitializer */ -#define THROW(c, v) { _xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); } +#define THROW(v) _xbt_throw(NULL, v, __FILE__, __LINE__, __func__) /** Builds and throws an exception with a printf-like formatted message * @ingroup XBT_ex_c * @hideinitializer */ -#define THROWF(c, v, ...) _xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__) +#define THROWF(v, ...) _xbt_throw(bprintf(__VA_ARGS__), v, __FILE__, __LINE__, __func__) XBT_ATTRIB_NORETURN void xbt_throw_impossible(const char* file, int line, const char* func); /** Throw an exception because something impossible happened diff --git a/src/kernel/activity/CommImpl.cpp b/src/kernel/activity/CommImpl.cpp index bff989f0e9..9031e8312c 100644 --- a/src/kernel/activity/CommImpl.cpp +++ b/src/kernel/activity/CommImpl.cpp @@ -691,13 +691,7 @@ void CommImpl::finish() // In order to modify the exception we have to rethrow it: try { std::rethrow_exception(simcall->issuer->exception_); - } catch (simgrid::TimeoutError& e) { - e.value = rank; - simcall->issuer->exception_ = std::make_exception_ptr(e); - } catch (simgrid::NetworkFailureException& e) { - e.value = rank; - simcall->issuer->exception_ = std::make_exception_ptr(e); - } catch (simgrid::CancelException& e) { + } catch (simgrid::Exception& e) { e.value = rank; simcall->issuer->exception_ = std::make_exception_ptr(e); } diff --git a/src/plugins/vm/VmLiveMigration.cpp b/src/plugins/vm/VmLiveMigration.cpp index 08add03749..90b555321d 100644 --- a/src/plugins/vm/VmLiveMigration.cpp +++ b/src/plugins/vm/VmLiveMigration.cpp @@ -107,7 +107,7 @@ sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_r comm = mbox->put_init(msg, size)->set_rate(mig_speed)->wait_for(timeout); else comm = mbox->put_async(msg, size)->wait_for(timeout); - } catch (const xbt_ex&) { + } catch (const Exception&) { if (comm) { sg_size_t remaining = static_cast(comm->get_remaining()); XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size); @@ -182,7 +182,7 @@ void MigrationTx::operator()() } else if (sent > ramsize) XBT_CRITICAL("bug"); - } catch (const xbt_ex&) { + } catch (const Exception&) { // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code) // Stop the dirty page tracking an return (there is no memory space to release) sg_vm_stop_dirty_page_tracking(vm_); @@ -222,7 +222,7 @@ void MigrationTx::operator()() try { XBT_DEBUG("Stage 2, gonna send %llu", updated_size); sent = sendMigrationData(updated_size, 2, stage2_round, mig_speed, mig_timeout); - } catch (const xbt_ex&) { + } catch (const Exception&) { // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data // code) // Stop the dirty page tracking an return (there is no memory space to release) @@ -266,7 +266,7 @@ void MigrationTx::operator()() try { XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size); sendMigrationData(remaining_size, 3, 0, mig_speed, -1); - } catch (const xbt_ex&) { + } catch (const Exception&) { // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code) // Stop the dirty page tracking an return (there is no memory space to release) vm_->resume(); diff --git a/src/s4u/s4u_Engine.cpp b/src/s4u/s4u_Engine.cpp index 70e3043502..0c124e03f5 100644 --- a/src/s4u/s4u_Engine.cpp +++ b/src/s4u/s4u_Engine.cpp @@ -89,7 +89,7 @@ void Engine::load_platform(const std::string& platf) double start = xbt_os_time(); try { parse_platform_file(platf); - } catch (xbt_ex& e) { + } catch (const Exception& e) { xbt_die("Error while loading %s: %s", platf.c_str(), e.what()); } diff --git a/src/simgrid/Exception.cpp b/src/simgrid/Exception.cpp index d59d4874a1..ed0a7e9fa1 100644 --- a/src/simgrid/Exception.cpp +++ b/src/simgrid/Exception.cpp @@ -9,6 +9,11 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context); namespace simgrid { +// DO NOT define destructors for exceptions in Exception.hpp. +// Defining it here ensures that the exceptions are defined only in libsimgrid, but not in libsimgrid-java. +// Doing otherwise naturally breaks things (at least on freebsd with clang). + +Exception::~Exception() = default; ForcefulKillException::~ForcefulKillException() = default; void ForcefulKillException::do_throw() diff --git a/src/simix/smx_deployment.cpp b/src/simix/smx_deployment.cpp index 8468165933..422f0a86d4 100644 --- a/src/simix/smx_deployment.cpp +++ b/src/simix/smx_deployment.cpp @@ -48,7 +48,7 @@ void SIMIX_launch_application(const std::string& file) parse_status = surf_parse(); surf_parse_close(); xbt_assert(not parse_status, "Parse error at %s:%d", file.c_str(), surf_parse_lineno); - } catch (const xbt_ex&) { + } catch (const simgrid::Exception&) { XBT_ERROR( "Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.", file.c_str(), surf_parse_lineno); diff --git a/src/smpi/mpi/smpi_request.cpp b/src/smpi/mpi/smpi_request.cpp index f0a08cc644..692567af8e 100644 --- a/src/smpi/mpi/smpi_request.cpp +++ b/src/smpi/mpi/smpi_request.cpp @@ -548,7 +548,7 @@ int Request::test(MPI_Request * request, MPI_Status * status, int* flag) { if ((*request)->action_ != nullptr){ try{ *flag = simcall_comm_test((*request)->action_); - } catch (const xbt_ex&) { + } catch (const Exception&) { *flag = 0; return ret; } @@ -639,7 +639,7 @@ int Request::testany(int count, MPI_Request requests[], int *index, int* flag, M simcall_process_sleep(nsleeps*smpi_test_sleep); try{ i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches! - } catch (const xbt_ex&) { + } catch (const Exception&) { XBT_DEBUG("Exception in testany"); return 0; } @@ -905,7 +905,7 @@ int Request::wait(MPI_Request * request, MPI_Status * status) try{ // this is not a detached send simcall_comm_wait((*request)->action_, -1.0); - } catch (const xbt_ex&) { + } catch (const Exception&) { XBT_VERB("Request cancelled"); } } @@ -968,7 +968,7 @@ int Request::waitany(int count, MPI_Request requests[], MPI_Status * status) try{ // this is not a detached send i = simcall_comm_waitany(comms.data(), comms.size(), -1); - } catch (const xbt_ex&) { + } catch (const Exception&) { XBT_INFO("request %d cancelled ", i); return i; } diff --git a/src/xbt/exception.cpp b/src/xbt/exception.cpp index b3f4c51dd8..94661db438 100644 --- a/src/xbt/exception.cpp +++ b/src/xbt/exception.cpp @@ -14,59 +14,16 @@ XBT_LOG_EXTERNAL_CATEGORY(xbt); XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions"); -// DO NOT define ~xbt_ex() in exception.hpp. -// Defining it here ensures that xbt_ex is defined only in libsimgrid, but not in libsimgrid-java. -// Doing otherwise naturally breaks things (at least on freebsd with clang). - -xbt_ex::~xbt_ex() = default; - -void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func) +void _xbt_throw(char* message, int value, const char* file, int line, const char* func) { - xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()), - message); + simgrid::Exception e( + simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()), + message ? message : ""); xbt_free(message); - e.category = errcat; e.value = value; throw e; } -/** @brief returns a short name for the given exception category */ -const char* xbt_ex_catname(xbt_errcat_t cat) -{ - switch (cat) { - case unknown_error: - return "unknown error"; - case arg_error: - return "invalid argument"; - case bound_error: - return "out of bounds"; - case mismatch_error: - return "mismatch"; - case not_found_error: - return "not found"; - case system_error: - return "system error"; - case network_error: - return "network error"; - case timeout_error: - return "timeout"; - case cancel_error: - return "action canceled"; - case thread_error: - return "thread error"; - case host_error: - return "host failed"; - case tracing_error: - return "tracing error"; - case io_error: - return "io error"; - case vm_error: - return "vm error"; - default: - return "INVALID ERROR"; - } -} - namespace simgrid { namespace xbt { diff --git a/src/xbt/mmalloc/mfree.c b/src/xbt/mmalloc/mfree.c index e08aba34f4..411ca29273 100644 --- a/src/xbt/mmalloc/mfree.c +++ b/src/xbt/mmalloc/mfree.c @@ -39,12 +39,12 @@ void mfree(struct mdesc *mdp, void *ptr) switch (type) { case MMALLOC_TYPE_HEAPINFO: UNLOCK(mdp); - THROWF(system_error, 0, "Asked to free a fragment in a heapinfo block. I'm confused.\n"); + THROWF(0, "Asked to free a fragment in a heapinfo block. I'm confused.\n"); break; case MMALLOC_TYPE_FREE: /* Already free */ UNLOCK(mdp); - THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled.\n"); + THROWF(0, "Asked to free a fragment in a block that is already free. I'm puzzled.\n"); break; case MMALLOC_TYPE_UNFRAGMENTED: @@ -163,7 +163,7 @@ void mfree(struct mdesc *mdp, void *ptr) if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ UNLOCK(mdp); - THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n"); + THROWF(0, "Asked to free a fragment that is already free. I'm puzzled\n"); } if (MC_is_active() && mdp->heapinfo[block].busy_frag.ignore[frag_nb] > 0) diff --git a/src/xbt/xbt_replay.cpp b/src/xbt/xbt_replay.cpp index 7af9758b06..2c51d9d81a 100644 --- a/src/xbt/xbt_replay.cpp +++ b/src/xbt/xbt_replay.cpp @@ -102,7 +102,7 @@ static void handle_action(ReplayAction& action) action_fun function = action_funs.at(action.at(1)); try { function(action); - } catch (xbt_ex& e) { + } catch (const Exception& e) { action.clear(); xbt_die("Replay error:\n %s", e.what()); } diff --git a/teshsuite/xbt/mmalloc/mmalloc_test.cpp b/teshsuite/xbt/mmalloc/mmalloc_test.cpp index e5ddf72949..9b11df44f3 100644 --- a/teshsuite/xbt/mmalloc/mmalloc_test.cpp +++ b/teshsuite/xbt/mmalloc/mmalloc_test.cpp @@ -75,7 +75,7 @@ int main(int argc, char**argv) mfree(heapA, pointers[i]); try { mfree(heapA, pointers[i]); - } catch (const xbt_ex&) { + } catch (const simgrid::Exception&) { gotit = true; } if (not gotit) @@ -87,7 +87,7 @@ int main(int argc, char**argv) bool gotit = false; try { mfree(heapA, pointers[i]); - } catch (const xbt_ex&) { + } catch (const simgrid::Exception&) { gotit = true; } if (not gotit)