Logo AND Algorithmique Numérique Distribuée

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