Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify the library initialization + deprecate 2 XBT functions
[simgrid.git] / include / simgrid / Exception.hpp
1 /* Copyright (c) 2018-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This file defines all possible exception that could occur in a SimGrid library. */
7
8 #ifndef SIMGRID_EXCEPTIONS_HPP
9 #define SIMGRID_EXCEPTIONS_HPP
10
11 #include <xbt/backtrace.hpp>
12 #include <xbt/ex.h>
13 #include <xbt/string.hpp>
14
15 #include <atomic>
16 #include <functional>
17 #include <stdexcept>
18 #include <string>
19
20 namespace simgrid {
21 namespace xbt {
22
23 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
24  *
25  *  Constitute the contextual information of where an exception was thrown
26  *
27  *  These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
28  *  are best created with the macro XBT_THROW_POINT.
29  */
30 class ThrowPoint {
31 public:
32   ThrowPoint() = default;
33   explicit ThrowPoint(const char* file, int line, const char* function, Backtrace&& bt, std::string&& actor_name,
34                       int pid)
35       : file_(file)
36       , line_(line)
37       , function_(function)
38       , backtrace_(std::move(bt))
39       , procname_(std::move(actor_name))
40       , pid_(pid)
41   {
42   }
43
44   const char* file_     = nullptr;
45   int line_             = 0;
46   const char* function_ = nullptr;
47   Backtrace backtrace_;
48   std::string procname_ = ""; /**< Name of the process who thrown this */
49   int pid_              = 0;  /**< PID of the process who thrown this */
50 };
51
52 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
53 #define XBT_THROW_POINT                                                                                                \
54   ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), sg_actor_self_get_name(),        \
55                              sg_actor_self_get_pid())
56
57 class XBT_PUBLIC ImpossibleError : public std::logic_error {
58 public:
59   using std::logic_error::logic_error;
60   ~ImpossibleError() override;
61 };
62
63 class XBT_PUBLIC InitializationError : public std::logic_error {
64 public:
65   using std::logic_error::logic_error;
66   ~InitializationError() override;
67 };
68
69 class XBT_PUBLIC UnimplementedError : public std::logic_error {
70 public:
71   using std::logic_error::logic_error;
72   ~UnimplementedError() override;
73 };
74
75 } // namespace xbt
76
77 /** Ancestor class of all SimGrid exception */
78 class Exception : public std::runtime_error {
79 public:
80   Exception(simgrid::xbt::ThrowPoint&& throwpoint, const std::string& message)
81       : std::runtime_error(message), throwpoint_(std::move(throwpoint))
82   {
83   }
84   Exception(const Exception&)     = default;
85   Exception(Exception&&) noexcept = default;
86   ~Exception() override; // DO NOT define it here -- see Exception.cpp for a rationale
87
88   /** Return the information about where the exception was thrown */
89   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
90
91   /** Allow to carry a value (used by testany/waitany) */
92   ssize_t get_value() const { return value_; }
93   void set_value(ssize_t value) { value_ = value; }
94
95   std::string resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
96
97   XBT_ATTRIB_NORETURN virtual void rethrow_nested(simgrid::xbt::ThrowPoint&& throwpoint,
98                                                   const std::string& message) const
99   {
100     std::throw_with_nested(Exception(std::move(throwpoint), message));
101   }
102
103 private:
104   xbt::ThrowPoint throwpoint_;
105   ssize_t value_ = 0;
106 };
107
108 #define DECLARE_SIMGRID_EXCEPTION(AnyException, ...)                                                                   \
109   class AnyException : public Exception {                                                                              \
110   public:                                                                                                              \
111     using Exception::Exception;                                                                                        \
112     __VA_ARGS__                                                                                                        \
113     ~AnyException() override;                                                                                          \
114     XBT_ATTRIB_NORETURN void rethrow_nested(simgrid::xbt::ThrowPoint&& throwpoint,                                     \
115                                             const std::string& message) const override                                 \
116     {                                                                                                                  \
117       std::throw_with_nested(AnyException(std::move(throwpoint), message));                                            \
118     }                                                                                                                  \
119   }
120
121 /** Exception raised when a timeout elapsed */
122 DECLARE_SIMGRID_EXCEPTION(TimeoutException);
123
124 /** Exception raised when a host fails */
125 DECLARE_SIMGRID_EXCEPTION(HostFailureException);
126
127 /** Exception raised when a communication fails because of the network or because of the remote host */
128 DECLARE_SIMGRID_EXCEPTION(NetworkFailureException);
129
130 /** Exception raised when a storage fails */
131 DECLARE_SIMGRID_EXCEPTION(StorageFailureException);
132
133 /** Exception raised when a VM fails */
134 DECLARE_SIMGRID_EXCEPTION(VmFailureException);
135
136 /** Exception raised when something got canceled before completion */
137 DECLARE_SIMGRID_EXCEPTION(CancelException);
138
139 /** Exception raised when something is going wrong during the simulation tracing */
140 DECLARE_SIMGRID_EXCEPTION(TracingError);
141
142 /** Exception raised when something is going wrong during the parsing of XML files */
143 #define PARSE_ERROR_CONSTRUCTOR                                                                                        \
144   ParseError(const std::string& file, int line, const std::string& msg)                                                \
145       : Exception(XBT_THROW_POINT, xbt::string_printf("Parse error at %s:%d: %s", file.c_str(), line, msg.c_str()))    \
146   {                                                                                                                    \
147   }
148
149 DECLARE_SIMGRID_EXCEPTION(ParseError, PARSE_ERROR_CONSTRUCTOR);
150 #undef PARSE_ERROR_CONSTRUCTOR
151
152 /** Exception raised by xbt_enforce, when an assertion is not satisfied */
153 DECLARE_SIMGRID_EXCEPTION(AssertionError);
154
155 #undef DECLARE_SIMGRID_EXCEPTION
156
157 class XBT_PUBLIC ForcefulKillException {
158   /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
159    *
160    * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
161    * all objects allocated on the stack (RAII powa).
162    *
163    * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
164    * SURVIVE a ForcefulKillException, or your simulation will segfault.
165    *
166    * @verbatim
167    * void* payload = malloc(512);
168    *
169    * try {
170    *   simgrid::s4u::this_actor::execute(100000);
171    * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
172    *   free(malloc);
173    *   throw; // I shall never survive on a host that was switched off
174    * }
175    * @endverbatim
176    */
177   /* Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
178    * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
179    */
180 public:
181   ForcefulKillException() = default;
182   explicit ForcefulKillException(const std::string& msg) : msg_("Actor killed (" + msg + ").") {}
183   ~ForcefulKillException();
184   const char* what() const noexcept { return msg_.c_str(); }
185
186   XBT_ATTRIB_NORETURN static void do_throw();
187   static bool try_n_catch(const std::function<void()>& try_block);
188
189 private:
190   std::string msg_ = "Actor killed.";
191 };
192
193 } // namespace simgrid
194 #endif