Logo AND Algorithmique Numérique Distribuée

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