1 /* 2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file 3 4 This file is part of libzmq, the ZeroMQ core engine in C++. 5 6 libzmq is free software; you can redistribute it and/or modify it under 7 the terms of the GNU Lesser General Public License (LGPL) as published 8 by the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 As a special exception, the Contributors give you permission to link 12 this library with independent modules to produce an executable, 13 regardless of the license terms of these independent modules, and to 14 copy and distribute the resulting executable under terms of your choice, 15 provided that you also meet, for each linked independent module, the 16 terms and conditions of the license of that module. An independent 17 module is a module which is not derived from or based on this library. 18 If you modify this library, you must extend this exception to your 19 version of the library. 20 21 libzmq is distributed in the hope that it will be useful, but WITHOUT 22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 24 License for more details. 25 26 You should have received a copy of the GNU Lesser General Public License 27 along with this program. If not, see <http://www.gnu.org/licenses/>. 28 */ 29 30 #ifndef __ZMQ_ERR_HPP_INCLUDED__ 31 #define __ZMQ_ERR_HPP_INCLUDED__ 32 33 #include <assert.h> 34 #if defined _WIN32_WCE 35 #include "..\builds\msvc\errno.hpp" 36 #else 37 #include <errno.h> 38 #endif 39 #include <string.h> 40 #include <stdlib.h> 41 #include <stdio.h> 42 43 #ifndef ZMQ_HAVE_WINDOWS 44 #include <netdb.h> 45 #endif 46 47 #include "likely.hpp" 48 49 // 0MQ-specific error codes are defined in zmq.h 50 51 // EPROTO is not used by OpenBSD and maybe other platforms. 52 #ifndef EPROTO 53 #define EPROTO 0 54 #endif 55 56 namespace zmq 57 { 58 const char *errno_to_string (int errno_); 59 #if defined __clang__ 60 #if __has_feature(attribute_analyzer_noreturn) 61 void zmq_abort (const char *errmsg_) __attribute__ ((analyzer_noreturn)); 62 #else 63 void zmq_abort (const char *errmsg_); 64 #endif 65 #elif defined __MSCVER__ 66 __declspec(noreturn) void zmq_abort (const char *errmsg_); 67 #else 68 void zmq_abort (const char *errmsg_); 69 #endif 70 void print_backtrace (); 71 } 72 73 #ifdef ZMQ_HAVE_WINDOWS 74 75 namespace zmq 76 { 77 const char *wsa_error (); 78 const char * 79 wsa_error_no (int no_, 80 const char *wsae_wouldblock_string_ = "Operation would block"); 81 void win_error (char *buffer_, size_t buffer_size_); 82 int wsa_error_to_errno (int errcode_); 83 } 84 85 // Provides convenient way to check WSA-style errors on Windows. 86 #define wsa_assert(x) \ 87 do { \ 88 if (unlikely (!(x))) { \ 89 const char *errstr = zmq::wsa_error (); \ 90 if (errstr != NULL) { \ 91 fprintf (stderr, "Assertion failed: %s [%i] (%s:%d)\n", \ 92 errstr, WSAGetLastError (), __FILE__, __LINE__); \ 93 fflush (stderr); \ 94 zmq::zmq_abort (errstr); \ 95 } \ 96 } \ 97 } while (false) 98 99 // Provides convenient way to assert on WSA-style errors on Windows. 100 #define wsa_assert_no(no) \ 101 do { \ 102 const char *errstr = zmq::wsa_error_no (no); \ 103 if (errstr != NULL) { \ 104 fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \ 105 __FILE__, __LINE__); \ 106 fflush (stderr); \ 107 zmq::zmq_abort (errstr); \ 108 } \ 109 } while (false) 110 111 // Provides convenient way to check GetLastError-style errors on Windows. 112 #define win_assert(x) \ 113 do { \ 114 if (unlikely (!(x))) { \ 115 char errstr[256]; \ 116 zmq::win_error (errstr, 256); \ 117 fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \ 118 __FILE__, __LINE__); \ 119 fflush (stderr); \ 120 zmq::zmq_abort (errstr); \ 121 } \ 122 } while (false) 123 124 #endif 125 126 // This macro works in exactly the same way as the normal assert. It is used 127 // in its stead because standard assert on Win32 in broken - it prints nothing 128 // when used within the scope of JNI library. 129 #define zmq_assert(x) \ 130 do { \ 131 if (unlikely (!(x))) { \ 132 fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, __FILE__, \ 133 __LINE__); \ 134 fflush (stderr); \ 135 zmq::zmq_abort (#x); \ 136 } \ 137 } while (false) 138 139 // Provides convenient way to check for errno-style errors. 140 #define errno_assert(x) \ 141 do { \ 142 if (unlikely (!(x))) { \ 143 const char *errstr = strerror (errno); \ 144 fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__); \ 145 fflush (stderr); \ 146 zmq::zmq_abort (errstr); \ 147 } \ 148 } while (false) 149 150 // Provides convenient way to check for POSIX errors. 151 #define posix_assert(x) \ 152 do { \ 153 if (unlikely (x)) { \ 154 const char *errstr = strerror (x); \ 155 fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__); \ 156 fflush (stderr); \ 157 zmq::zmq_abort (errstr); \ 158 } \ 159 } while (false) 160 161 // Provides convenient way to check for errors from getaddrinfo. 162 #define gai_assert(x) \ 163 do { \ 164 if (unlikely (x)) { \ 165 const char *errstr = gai_strerror (x); \ 166 fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__); \ 167 fflush (stderr); \ 168 zmq::zmq_abort (errstr); \ 169 } \ 170 } while (false) 171 172 // Provides convenient way to check whether memory allocation have succeeded. 173 #define alloc_assert(x) \ 174 do { \ 175 if (unlikely (!x)) { \ 176 fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n", __FILE__, \ 177 __LINE__); \ 178 fflush (stderr); \ 179 zmq::zmq_abort ("FATAL ERROR: OUT OF MEMORY"); \ 180 } \ 181 } while (false) 182 183 #endif 184