Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef included_error_h |
| 39 | #define included_error_h |
| 40 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | #include <vppinfra/clib.h> /* for CLIB_LINUX_KERNEL */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #include <vppinfra/error_bootstrap.h> |
| 43 | |
| 44 | #ifdef CLIB_UNIX |
| 45 | #include <errno.h> |
| 46 | #endif |
| 47 | |
| 48 | #ifdef CLIB_LINUX_KERNEL |
| 49 | #include <linux/errno.h> |
| 50 | #endif |
| 51 | |
| 52 | #include <stdarg.h> |
| 53 | #include <vppinfra/vec.h> |
| 54 | |
| 55 | /* Callback functions for error reporting. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 56 | typedef void clib_error_handler_func_t (void *arg, u8 * msg, int msg_len); |
| 57 | void clib_error_register_handler (clib_error_handler_func_t func, void *arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | |
| 59 | #define clib_warning(format,args...) \ |
| 60 | _clib_error (CLIB_ERROR_WARNING, clib_error_function, __LINE__, format, ## args) |
| 61 | |
| 62 | #define clib_error(format,args...) \ |
| 63 | _clib_error (CLIB_ERROR_FATAL, clib_error_function, __LINE__, format, ## args) |
| 64 | |
| 65 | #define clib_unix_error(format,args...) \ |
| 66 | _clib_error (CLIB_ERROR_FATAL | CLIB_ERROR_ERRNO_VALID, clib_error_function, __LINE__, format, ## args) |
| 67 | |
| 68 | #define clib_unix_warning(format,args...) \ |
| 69 | _clib_error (CLIB_ERROR_WARNING | CLIB_ERROR_ERRNO_VALID, clib_error_function, __LINE__, format, ## args) |
| 70 | |
| 71 | /* For programming errors and assert. */ |
| 72 | #define clib_panic(format,args...) \ |
| 73 | _clib_error (CLIB_ERROR_ABORT, (char *) clib_error_function, __LINE__, format, ## args) |
| 74 | |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 75 | #include <vppinfra/clib_error.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | |
| 77 | #define clib_error_get_code(err) ((err) ? (err)->code : 0) |
| 78 | #define clib_error_set_code(err, c) \ |
| 79 | do { \ |
| 80 | if (err) \ |
| 81 | (err)->code = (c); \ |
| 82 | } while (0) |
| 83 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 84 | extern void *clib_error_free_vector (clib_error_t * errors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | #define clib_error_free(e) e = clib_error_free_vector(e) |
| 87 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | extern clib_error_t *_clib_error_return (clib_error_t * errors, |
| 89 | any code, |
| 90 | uword flags, |
| 91 | char *where, char *fmt, ...); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
| 93 | #define clib_error_return_code(e,code,flags,args...) \ |
| 94 | _clib_error_return((e),(code),(flags),(char *)clib_error_function,args) |
| 95 | |
| 96 | #define clib_error_create(args...) \ |
| 97 | clib_error_return_code(0,0,0,args) |
| 98 | |
| 99 | #define clib_error_return(e,args...) \ |
| 100 | clib_error_return_code(e,0,0,args) |
| 101 | |
| 102 | #define clib_error_return_unix(e,args...) \ |
| 103 | clib_error_return_code(e,errno,CLIB_ERROR_ERRNO_VALID,args) |
| 104 | |
| 105 | #define clib_error_return_fatal(e,args...) \ |
| 106 | clib_error_return_code(e,0,CLIB_ERROR_FATAL,args) |
| 107 | |
| 108 | #define clib_error_return_unix_fatal(e,args...) \ |
| 109 | clib_error_return_code(e,errno,CLIB_ERROR_ERRNO_VALID|CLIB_ERROR_FATAL,args) |
| 110 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 111 | extern clib_error_t *_clib_error_report (clib_error_t * errors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
| 113 | #define clib_error_report(e) do { (e) = _clib_error_report (e); } while (0) |
| 114 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 115 | u8 *format_clib_error (u8 * s, va_list * va); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 117 | always_inline word |
| 118 | unix_error_is_fatal (word error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | { |
| 120 | #ifdef CLIB_UNIX |
| 121 | switch (error) |
| 122 | { |
| 123 | case EWOULDBLOCK: |
| 124 | case EINTR: |
| 125 | return 0; |
| 126 | } |
| 127 | #endif |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | #define IF_ERROR_IS_FATAL_RETURN_ELSE_FREE(e) \ |
| 132 | do { \ |
| 133 | if (e) \ |
| 134 | { \ |
| 135 | if (unix_error_is_fatal (clib_error_get_code (e))) \ |
| 136 | return (e); \ |
| 137 | else \ |
| 138 | clib_error_free (e); \ |
| 139 | } \ |
| 140 | } while (0) |
| 141 | |
| 142 | #define ERROR_RETURN_IF(x) \ |
| 143 | do { \ |
| 144 | clib_error_t * _error_return_if = (x); \ |
| 145 | if (_error_return_if) \ |
| 146 | return clib_error_return (_error_return_if, 0); \ |
| 147 | } while (0) |
| 148 | |
| 149 | #define ERROR_ASSERT(truth) \ |
| 150 | ({ \ |
| 151 | clib_error_t * _error_assert = 0; \ |
| 152 | if (CLIB_DEBUG > 0 && ! (truth)) \ |
| 153 | { \ |
| 154 | _error_assert = clib_error_return_fatal \ |
| 155 | (0, "%s:%d (%s) assertion `%s' fails", \ |
| 156 | __FILE__, \ |
| 157 | (uword) __LINE__, \ |
| 158 | clib_error_function, \ |
| 159 | # truth); \ |
| 160 | } \ |
| 161 | _error_assert; \ |
| 162 | }) |
| 163 | |
| 164 | /* Assert to remain even if CLIB_DEBUG is set to 0. */ |
| 165 | #define CLIB_ERROR_ASSERT(truth) \ |
| 166 | ({ \ |
| 167 | clib_error_t * _error_assert = 0; \ |
| 168 | if (! (truth)) \ |
| 169 | { \ |
| 170 | _error_assert = \ |
| 171 | clib_error_return_fatal \ |
| 172 | (0, "%s:%d (%s) assertion `%s' fails", \ |
| 173 | __FILE__, \ |
| 174 | (uword) __LINE__, \ |
| 175 | clib_error_function, \ |
| 176 | # truth); \ |
| 177 | } \ |
| 178 | _error_assert; \ |
| 179 | }) |
| 180 | |
Dave Barach | 3ad7704 | 2017-01-04 17:24:32 -0500 | [diff] [blame] | 181 | /* |
| 182 | * If we're running under Coverity, don't die on |
| 183 | * failed static assertions. |
| 184 | */ |
| 185 | #ifdef __COVERITY__ |
| 186 | #ifndef _Static_assert |
| 187 | #define _Static_assert(x,y) |
| 188 | #endif |
| 189 | #endif |
| 190 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | #endif /* included_error_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * fd.io coding-style-patch-verification: ON |
| 195 | * |
| 196 | * Local Variables: |
| 197 | * eval: (c-set-style "gnu") |
| 198 | * End: |
| 199 | */ |