Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
685f36deb963b8e5960e17c90c02e3d8202e893f
[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 } // namespace xbt
60
61 /** Ancestor class of all SimGrid exception */
62 class Exception : public std::runtime_error {
63 public:
64   Exception(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
65       : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
66   {
67   }
68
69   /** Return the information about where the exception was thrown */
70   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
71
72   std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
73
74 private:
75   xbt::ThrowPoint throwpoint_;
76 };
77
78 } // namespace simgrid
79
80 /** A legacy exception
81  *
82  *  It is defined by a category and a value within that category (as well as
83  *  an optional error message).
84  *
85  *  This used to be a structure for C exceptions but it has been retrofitted
86  *  as a C++ exception and some of its data has been moved in the
87  *  @ref WithContextException base class. We should deprecate it and replace it
88  *  with either C++ different exceptions or `std::system_error` which already
89  *  provides this (category + error code) logic.
90  *  TODO ^^
91  *
92  *  @ingroup XBT_ex_c
93  */
94 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
95 public:
96   /**
97    *
98    * @param throwpoint Throw point (use XBT_THROW_POINT)
99    * @param message    Exception message
100    */
101   xbt_ex(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
102       : simgrid::Exception(std::move(throwpoint), std::move(message))
103   {
104   }
105
106   xbt_ex(const xbt_ex&) = default;
107
108   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
109
110   /** Category (what went wrong) */
111   xbt_errcat_t category = unknown_error;
112
113   /** Why did it went wrong */
114   int value = 0;
115 };
116
117 namespace simgrid {
118
119 /** Exception raised when a timeout elapsed */
120 class TimeoutError : public xbt_ex {
121 public:
122   TimeoutError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
123       : xbt_ex(std::move(throwpoint), std::move(message))
124   {
125     category = timeout_error;
126   }
127 };
128
129 /** Exception raised when a host fails */
130 class HostFailureException : public xbt_ex {
131 public:
132   HostFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
133       : xbt_ex(std::move(throwpoint), std::move(message))
134   {
135     category = host_error;
136   }
137 };
138
139 /** Exception raised when a communication fails because of the network or because of the remote host */
140 class NetworkFailureException : public xbt_ex {
141 public:
142   NetworkFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
143       : xbt_ex(std::move(throwpoint), std::move(message))
144   {
145     category = network_error;
146   }
147 };
148
149 /** Exception raised when a storage fails */
150 class StorageFailureException : public xbt_ex {
151 public:
152   StorageFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
153       : xbt_ex(std::move(throwpoint), std::move(message))
154   {
155     category = io_error;
156   }
157 };
158
159 /** Exception raised when something got canceled before completion */
160 class CancelException : public xbt_ex {
161 public:
162   CancelException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
163       : xbt_ex(std::move(throwpoint), std::move(message))
164   {
165     category = cancel_error;
166   }
167 };
168
169 class XBT_PUBLIC ForcefulKillException {
170   /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
171    *
172    * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
173    * all objects allocated on the stack (RAII powa).
174    *
175    * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
176    * SURVIVE a ForcefulKillException, or your simulation will segfault.
177    *
178    * @verbatim
179    * void* payload = malloc(512);
180    *
181    * try {
182    *   simgrid::s4u::this_actor::execute(100000);
183    * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
184    *   free(malloc);
185    *   throw; // I shall never survive on an host that was switched off
186    * }
187    * @endverbatim
188    */
189   /* Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
190    * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
191    */
192 public:
193   ForcefulKillException() = default;
194   explicit ForcefulKillException(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(")."))
195   {
196   }
197   ~ForcefulKillException();
198   const char* what() const noexcept { return msg_.c_str(); }
199
200   XBT_ATTRIB_NORETURN static void do_throw();
201   static bool try_n_catch(const std::function<void()>& try_block);
202
203 private:
204   std::string msg_ = std::string("Actor killed.");
205 };
206
207 } // namespace simgrid
208
209 #endif