1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_IOMGR_ERROR_H
20 #define GRPC_CORE_LIB_IOMGR_ERROR_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <inttypes.h>
25 #include <stdbool.h>
26 
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 
32 #include "src/core/lib/debug/trace.h"
33 
34 /// Opaque representation of an error.
35 /// See https://github.com/grpc/grpc/blob/master/doc/core/grpc-error.md for a
36 /// full write up of this object.
37 
38 typedef struct grpc_error grpc_error;
39 
40 extern grpc_core::DebugOnlyTraceFlag grpc_trace_error_refcount;
41 
42 typedef enum {
43   /// 'errno' from the operating system
44   GRPC_ERROR_INT_ERRNO,
45   /// __LINE__ from the call site creating the error
46   GRPC_ERROR_INT_FILE_LINE,
47   /// stream identifier: for errors that are associated with an individual
48   /// wire stream
49   GRPC_ERROR_INT_STREAM_ID,
50   /// grpc status code representing this error
51   GRPC_ERROR_INT_GRPC_STATUS,
52   /// offset into some binary blob (usually represented by
53   /// GRPC_ERROR_STR_RAW_BYTES) where the error occurred
54   GRPC_ERROR_INT_OFFSET,
55   /// context sensitive index associated with the error
56   GRPC_ERROR_INT_INDEX,
57   /// context sensitive size associated with the error
58   GRPC_ERROR_INT_SIZE,
59   /// http2 error code associated with the error (see the HTTP2 RFC)
60   GRPC_ERROR_INT_HTTP2_ERROR,
61   /// TSI status code associated with the error
62   GRPC_ERROR_INT_TSI_CODE,
63   /// grpc_security_status associated with the error
64   GRPC_ERROR_INT_SECURITY_STATUS,
65   /// WSAGetLastError() reported when this error occurred
66   GRPC_ERROR_INT_WSA_ERROR,
67   /// File descriptor associated with this error
68   GRPC_ERROR_INT_FD,
69   /// HTTP status (i.e. 404)
70   GRPC_ERROR_INT_HTTP_STATUS,
71   /// context sensitive limit associated with the error
72   GRPC_ERROR_INT_LIMIT,
73   /// chttp2: did the error occur while a write was in progress
74   GRPC_ERROR_INT_OCCURRED_DURING_WRITE,
75   /// channel connectivity state associated with the error
76   GRPC_ERROR_INT_CHANNEL_CONNECTIVITY_STATE,
77 
78   /// Must always be last
79   GRPC_ERROR_INT_MAX,
80 } grpc_error_ints;
81 
82 typedef enum {
83   /// top-level textual description of this error
84   GRPC_ERROR_STR_DESCRIPTION,
85   /// source file in which this error occurred
86   GRPC_ERROR_STR_FILE,
87   /// operating system description of this error
88   GRPC_ERROR_STR_OS_ERROR,
89   /// syscall that generated this error
90   GRPC_ERROR_STR_SYSCALL,
91   /// peer that we were trying to communicate when this error occurred
92   GRPC_ERROR_STR_TARGET_ADDRESS,
93   /// grpc status message associated with this error
94   GRPC_ERROR_STR_GRPC_MESSAGE,
95   /// hex dump (or similar) with the data that generated this error
96   GRPC_ERROR_STR_RAW_BYTES,
97   /// tsi error string associated with this error
98   GRPC_ERROR_STR_TSI_ERROR,
99   /// filename that we were trying to read/write when this error occurred
100   GRPC_ERROR_STR_FILENAME,
101   /// which data was queued for writing when the error occurred
102   GRPC_ERROR_STR_QUEUED_BUFFERS,
103   /// key associated with the error
104   GRPC_ERROR_STR_KEY,
105   /// value associated with the error
106   GRPC_ERROR_STR_VALUE,
107 
108   /// Must always be last
109   GRPC_ERROR_STR_MAX,
110 } grpc_error_strs;
111 
112 typedef enum {
113   /// timestamp of error creation
114   GRPC_ERROR_TIME_CREATED,
115 
116   /// Must always be last
117   GRPC_ERROR_TIME_MAX,
118 } grpc_error_times;
119 
120 /// The following "special" errors can be propagated without allocating memory.
121 /// They are always even so that other code (particularly combiner locks,
122 /// polling engines) can safely use the lower bit for themselves.
123 
124 #define GRPC_ERROR_NONE ((grpc_error*)NULL)
125 #define GRPC_ERROR_RESERVED_1 ((grpc_error*)1)
126 #define GRPC_ERROR_OOM ((grpc_error*)2)
127 #define GRPC_ERROR_RESERVED_2 ((grpc_error*)3)
128 #define GRPC_ERROR_CANCELLED ((grpc_error*)4)
129 #define GRPC_ERROR_SPECIAL_MAX GRPC_ERROR_CANCELLED
130 
grpc_error_is_special(struct grpc_error * err)131 inline bool grpc_error_is_special(struct grpc_error* err) {
132   return err <= GRPC_ERROR_SPECIAL_MAX;
133 }
134 
135 // debug only toggles that allow for a sanity to check that ensures we will
136 // never create any errors in the per-RPC hotpath.
137 void grpc_disable_error_creation();
138 void grpc_enable_error_creation();
139 
140 const char* grpc_error_string(grpc_error* error);
141 
142 /// Create an error - but use GRPC_ERROR_CREATE instead
143 grpc_error* grpc_error_create(const char* file, int line,
144                               const grpc_slice& desc, grpc_error** referencing,
145                               size_t num_referencing);
146 /// Create an error (this is the preferred way of generating an error that is
147 ///   not due to a system call - for system calls, use GRPC_OS_ERROR or
148 ///   GRPC_WSA_ERROR as appropriate)
149 /// \a referencing is an array of num_referencing elements indicating one or
150 /// more errors that are believed to have contributed to this one
151 /// err = grpc_error_create(x, y, z, r, nr) is equivalent to:
152 ///   err = grpc_error_create(x, y, z, NULL, 0);
153 ///   for (i=0; i<nr; i++) err = grpc_error_add_child(err, r[i]);
154 #define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)                           \
155   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
156                     NULL, 0)
157 #define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc)                           \
158   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
159                     NULL, 0)
160 
161 // Create an error that references some other errors. This function adds a
162 // reference to each error in errs - it does not consume an existing reference
163 #define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)  \
164   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
165                     errs, count)
166 #define GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(desc, errs, count)  \
167   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
168                     errs, count)
169 
170 #define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list) \
171   grpc_error_create_from_vector(__FILE__, __LINE__, desc, error_list)
172 
173 #ifndef NDEBUG
174 grpc_error* grpc_error_do_ref(grpc_error* err, const char* file, int line);
175 void grpc_error_do_unref(grpc_error* err, const char* file, int line);
grpc_error_ref(grpc_error * err,const char * file,int line)176 inline grpc_error* grpc_error_ref(grpc_error* err, const char* file, int line) {
177   if (grpc_error_is_special(err)) return err;
178   return grpc_error_do_ref(err, file, line);
179 }
grpc_error_unref(grpc_error * err,const char * file,int line)180 inline void grpc_error_unref(grpc_error* err, const char* file, int line) {
181   if (grpc_error_is_special(err)) return;
182   grpc_error_do_unref(err, file, line);
183 }
184 #define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__)
185 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err, __FILE__, __LINE__)
186 #else
187 grpc_error* grpc_error_do_ref(grpc_error* err);
188 void grpc_error_do_unref(grpc_error* err);
grpc_error_ref(grpc_error * err)189 inline grpc_error* grpc_error_ref(grpc_error* err) {
190   if (grpc_error_is_special(err)) return err;
191   return grpc_error_do_ref(err);
192 }
grpc_error_unref(grpc_error * err)193 inline void grpc_error_unref(grpc_error* err) {
194   if (grpc_error_is_special(err)) return;
195   grpc_error_do_unref(err);
196 }
197 #define GRPC_ERROR_REF(err) grpc_error_ref(err)
198 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
199 #endif
200 
201 // Consumes all the errors in the vector and forms a referencing error from
202 // them. If the vector is empty, return GRPC_ERROR_NONE.
203 template <typename VectorType>
grpc_error_create_from_vector(const char * file,int line,const char * desc,VectorType * error_list)204 static grpc_error* grpc_error_create_from_vector(const char* file, int line,
205                                                  const char* desc,
206                                                  VectorType* error_list) {
207   grpc_error* error = GRPC_ERROR_NONE;
208   if (error_list->size() != 0) {
209     error = grpc_error_create(file, line, grpc_slice_from_static_string(desc),
210                               error_list->data(), error_list->size());
211     // Remove refs to all errors in error_list.
212     for (size_t i = 0; i < error_list->size(); i++) {
213       GRPC_ERROR_UNREF((*error_list)[i]);
214     }
215     error_list->clear();
216   }
217   return error;
218 }
219 
220 grpc_error* grpc_error_set_int(grpc_error* src, grpc_error_ints which,
221                                intptr_t value) GRPC_MUST_USE_RESULT;
222 /// It is an error to pass nullptr as `p`. Caller should allocate a dummy
223 /// intptr_t for `p`, even if the value of `p` is not used.
224 bool grpc_error_get_int(grpc_error* error, grpc_error_ints which, intptr_t* p);
225 /// This call takes ownership of the slice; the error is responsible for
226 /// eventually unref-ing it.
227 grpc_error* grpc_error_set_str(grpc_error* src, grpc_error_strs which,
228                                const grpc_slice& str) GRPC_MUST_USE_RESULT;
229 /// Returns false if the specified string is not set.
230 /// Caller does NOT own the slice.
231 bool grpc_error_get_str(grpc_error* error, grpc_error_strs which,
232                         grpc_slice* s);
233 
234 /// Add a child error: an error that is believed to have contributed to this
235 /// error occurring. Allows root causing high level errors from lower level
236 /// errors that contributed to them. The src error takes ownership of the
237 /// child error.
238 ///
239 /// Edge Conditions -
240 /// 1) If either of \a src or \a child is GRPC_ERROR_NONE, returns a reference
241 /// to the other argument. 2) If both \a src and \a child are GRPC_ERROR_NONE,
242 /// returns GRPC_ERROR_NONE. 3) If \a src and \a child point to the same error,
243 /// returns a single reference. (Note that, 2 references should have been
244 /// received to the error in this case.)
245 grpc_error* grpc_error_add_child(grpc_error* src,
246                                  grpc_error* child) GRPC_MUST_USE_RESULT;
247 
248 grpc_error* grpc_os_error(const char* file, int line, int err,
249                           const char* call_name) GRPC_MUST_USE_RESULT;
250 
grpc_assert_never_ok(grpc_error * error)251 inline grpc_error* grpc_assert_never_ok(grpc_error* error) {
252   GPR_ASSERT(error != GRPC_ERROR_NONE);
253   return error;
254 }
255 
256 /// create an error associated with errno!=0 (an 'operating system' error)
257 #define GRPC_OS_ERROR(err, call_name) \
258   grpc_assert_never_ok(grpc_os_error(__FILE__, __LINE__, err, call_name))
259 grpc_error* grpc_wsa_error(const char* file, int line, int err,
260                            const char* call_name) GRPC_MUST_USE_RESULT;
261 /// windows only: create an error associated with WSAGetLastError()!=0
262 #define GRPC_WSA_ERROR(err, call_name) \
263   grpc_wsa_error(__FILE__, __LINE__, err, call_name)
264 
265 bool grpc_log_error(const char* what, grpc_error* error, const char* file,
266                     int line);
grpc_log_if_error(const char * what,grpc_error * error,const char * file,int line)267 inline bool grpc_log_if_error(const char* what, grpc_error* error,
268                               const char* file, int line) {
269   return error == GRPC_ERROR_NONE ? true
270                                   : grpc_log_error(what, error, file, line);
271 }
272 
273 #define GRPC_LOG_IF_ERROR(what, error) \
274   (grpc_log_if_error((what), (error), __FILE__, __LINE__))
275 
276 #endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */
277