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