err.cpp 13.3 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
31 32
#include "err.hpp"

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
const char *zmq::errno_to_string (int errno_)
{
    switch (errno_) {
#if defined ZMQ_HAVE_WINDOWS
    case ENOTSUP:
        return "Not supported";
    case EPROTONOSUPPORT:
        return "Protocol not supported";
    case ENOBUFS:
        return "No buffer space available";
    case ENETDOWN:
        return "Network is down";
    case EADDRINUSE:
        return "Address in use";
    case EADDRNOTAVAIL:
        return "Address not available";
    case ECONNREFUSED:
        return "Connection refused";
    case EINPROGRESS:
        return "Operation in progress";
#endif
    case EFSM:
        return "Operation cannot be accomplished in current state";
    case ENOCOMPATPROTO:
        return "The protocol is not compatible with the socket type";
    case ETERM:
        return "Context was terminated";
    case EMTHREAD:
        return "No thread available";
62 63
    case EHOSTUNREACH:
        return "Host unreachable";
64 65 66 67 68 69 70 71 72 73 74 75
    default:
#if defined _MSC_VER
#pragma warning (push)
#pragma warning (disable:4996)
#endif
        return strerror (errno_);
#if defined _MSC_VER
#pragma warning (pop)
#endif
    }
}

76 77 78 79 80 81 82 83 84
void zmq::zmq_abort(const char *errmsg_)
{
#if defined ZMQ_HAVE_WINDOWS

    //  Raise STATUS_FATAL_APP_EXIT.
    ULONG_PTR extra_info [1];
    extra_info [0] = (ULONG_PTR) errmsg_;
    RaiseException (0x40000015, EXCEPTION_NONCONTINUABLE, 1, extra_info);
#else
85
    (void)errmsg_;
86
    print_backtrace();
87 88 89 90
    abort ();
#endif
}

Martin Sustrik's avatar
Martin Sustrik committed
91
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
92

Martin Sustrik's avatar
Martin Sustrik committed
93
const char *zmq::wsa_error()
Martin Sustrik's avatar
Martin Sustrik committed
94
{
95
    const int last_error = WSAGetLastError();
Martin Sustrik's avatar
Martin Sustrik committed
96
    //  TODO: This is not a generic way to handle this...
97
    if (last_error == WSAEWOULDBLOCK)
Martin Sustrik's avatar
Martin Sustrik committed
98 99
        return NULL;

100
    return wsa_error_no (last_error);
101 102 103 104
}

const char *zmq::wsa_error_no (int no_)
{
105 106 107
    //  TODO:  It seems that list of Windows socket errors is longer than this.
    //         Investigate whether there's a way to convert it into the string
    //         automatically (wsaError->HRESULT->string?).
Martin Sustrik's avatar
Martin Sustrik committed
108
    return
109
        (no_ == WSABASEERR) ?
110
            "No Error" :
111
        (no_ == WSAEINTR) ?
112
            "Interrupted system call" :
113
        (no_ == WSAEBADF) ?
114
            "Bad file number" :
115
        (no_ == WSAEACCES) ?
116
            "Permission denied" :
117
        (no_ == WSAEFAULT) ?
118
            "Bad address" :
119
        (no_ == WSAEINVAL) ?
120
            "Invalid argument" :
121
        (no_ == WSAEMFILE) ?
122
            "Too many open files" :
123
        (no_ == WSAEWOULDBLOCK) ?
124
            "Operation would block" :
125
        (no_ == WSAEINPROGRESS) ?
126
            "Operation now in progress" :
127
        (no_ == WSAEALREADY) ?
128
            "Operation already in progress" :
129
        (no_ == WSAENOTSOCK) ?
130
            "Socket operation on non-socket" :
131
        (no_ == WSAEDESTADDRREQ) ?
132
            "Destination address required" :
133
        (no_ == WSAEMSGSIZE) ?
134
            "Message too long" :
135
        (no_ == WSAEPROTOTYPE) ?
136
            "Protocol wrong type for socket" :
137
        (no_ == WSAENOPROTOOPT) ?
138
            "Bad protocol option" :
139
        (no_ == WSAEPROTONOSUPPORT) ?
140
            "Protocol not supported" :
141
        (no_ == WSAESOCKTNOSUPPORT) ?
142
            "Socket type not supported" :
143
        (no_ == WSAEOPNOTSUPP) ?
144
            "Operation not supported on socket" :
145
        (no_ == WSAEPFNOSUPPORT) ?
146
            "Protocol family not supported" :
147
        (no_ == WSAEAFNOSUPPORT) ?
148
            "Address family not supported by protocol family" :
149
        (no_ == WSAEADDRINUSE) ?
150
            "Address already in use" :
151
        (no_ == WSAEADDRNOTAVAIL) ?
152
            "Can't assign requested address" :
153
        (no_ == WSAENETDOWN) ?
154
            "Network is down" :
155
        (no_ == WSAENETUNREACH) ?
156
            "Network is unreachable" :
157
        (no_ == WSAENETRESET) ?
158
            "Net dropped connection or reset" :
159
        (no_ == WSAECONNABORTED) ?
160
            "Software caused connection abort" :
161
        (no_ == WSAECONNRESET) ?
162
            "Connection reset by peer" :
163
        (no_ == WSAENOBUFS) ?
164
            "No buffer space available" :
165
        (no_ == WSAEISCONN) ?
166
            "Socket is already connected" :
167
        (no_ == WSAENOTCONN) ?
168
            "Socket is not connected" :
169
        (no_ == WSAESHUTDOWN) ?
170
            "Can't send after socket shutdown" :
171
        (no_ == WSAETOOMANYREFS) ?
172
            "Too many references can't splice" :
173
        (no_ == WSAETIMEDOUT) ?
174
            "Connection timed out" :
175
        (no_ == WSAECONNREFUSED) ?
176
            "Connection refused" :
177
        (no_ == WSAELOOP) ?
178
            "Too many levels of symbolic links" :
179
        (no_ == WSAENAMETOOLONG) ?
180
            "File name too long" :
181
        (no_ == WSAEHOSTDOWN) ?
182
            "Host is down" :
183
        (no_ == WSAEHOSTUNREACH) ?
184
            "No Route to Host" :
185
        (no_ == WSAENOTEMPTY) ?
186
            "Directory not empty" :
187
        (no_ == WSAEPROCLIM) ?
188
            "Too many processes" :
189
        (no_ == WSAEUSERS) ?
190
            "Too many users" :
191
        (no_ == WSAEDQUOT) ?
192
            "Disc Quota Exceeded" :
193
        (no_ == WSAESTALE) ?
194
            "Stale NFS file handle" :
195
        (no_ == WSAEREMOTE) ?
196
            "Too many levels of remote in path" :
197
        (no_ == WSASYSNOTREADY) ?
198
            "Network SubSystem is unavailable" :
199
        (no_ == WSAVERNOTSUPPORTED) ?
200
            "WINSOCK DLL Version out of range" :
201
        (no_ == WSANOTINITIALISED) ?
202
            "Successful WSASTARTUP not yet performed" :
203
        (no_ == WSAHOST_NOT_FOUND) ?
204
            "Host not found" :
205
        (no_ == WSATRY_AGAIN) ?
206
            "Non-Authoritative Host not found" :
207
        (no_ == WSANO_RECOVERY) ?
208
            "Non-Recoverable errors: FORMERR REFUSED NOTIMP" :
209
        (no_ == WSANO_DATA) ?
Martin Sustrik's avatar
Martin Sustrik committed
210
            "Valid name no data record of requested" :
211
        "error not defined";
Martin Sustrik's avatar
Martin Sustrik committed
212
}
213

Martin Sustrik's avatar
Martin Sustrik committed
214
void zmq::win_error (char *buffer_, size_t buffer_size_)
Martin Sustrik's avatar
Martin Sustrik committed
215 216
{
    DWORD errcode = GetLastError ();
217
#if defined _WIN32_WCE
evoskuil's avatar
evoskuil committed
218
    DWORD rc = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
boris@boressoft.ru's avatar
boris@boressoft.ru committed
219
        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
evoskuil's avatar
evoskuil committed
220
        SUBLANG_DEFAULT), (LPWSTR)buffer_, buffer_size_ / sizeof(wchar_t), NULL);
boris@boressoft.ru's avatar
boris@boressoft.ru committed
221
#else
Martin Sustrik's avatar
Martin Sustrik committed
222 223
    DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
evoskuil's avatar
evoskuil committed
224
        SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL);
boris@boressoft.ru's avatar
boris@boressoft.ru committed
225
#endif
Martin Sustrik's avatar
Martin Sustrik committed
226
    zmq_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
227 228
}

229
int zmq::wsa_error_to_errno (int errcode)
230 231
{
    switch (errcode) {
232 233 234
//  10004 - Interrupted system call.
    case WSAEINTR:
        return EINTR;
235
//  10009 - File handle is not valid.
236
    case WSAEBADF:
237
        return EBADF;
238 239 240 241 242 243 244
//  10013 - Permission denied.
    case WSAEACCES:
        return EACCES;
//  10014 - Bad address.
    case WSAEFAULT:
        return EFAULT;
//  10022 - Invalid argument.
245
    case WSAEINVAL:
246
        return EINVAL;
247
//  10024 - Too many open files.
248
    case WSAEMFILE:
249
        return EMFILE;
250 251 252
//  10035 - Operation would block.
    case WSAEWOULDBLOCK:
        return EBUSY;
253 254 255
//  10036 - Operation now in progress.
    case WSAEINPROGRESS:
        return EAGAIN;
256 257 258 259 260
//  10037 - Operation already in progress.
    case WSAEALREADY:
        return EAGAIN;
//  10038 - Socket operation on non-socket.
    case WSAENOTSOCK:
261
        return ENOTSOCK;
262 263 264
//  10039 - Destination address required.
    case WSAEDESTADDRREQ:
        return EFAULT;
265 266 267
//  10040 - Message too long.
    case WSAEMSGSIZE:
        return EMSGSIZE;
268 269 270 271 272 273
//  10041 - Protocol wrong type for socket.
    case WSAEPROTOTYPE:
        return EFAULT;
//  10042 - Bad protocol option.
    case WSAENOPROTOOPT:
        return EINVAL;
274
//  10043 - Protocol not supported.
275
    case WSAEPROTONOSUPPORT:
276
        return EPROTONOSUPPORT;
277 278 279 280 281 282 283 284 285
//  10044 - Socket type not supported.
    case WSAESOCKTNOSUPPORT:
        return EFAULT;
//  10045 - Operation not supported on socket.
    case WSAEOPNOTSUPP:
        return EFAULT;
//  10046 - Protocol family not supported.
    case WSAEPFNOSUPPORT:
        return EPROTONOSUPPORT;
286 287 288 289
//  10047 - Address family not supported by protocol family.
    case WSAEAFNOSUPPORT:
        return EAFNOSUPPORT;
//  10048 - Address already in use.
290
    case WSAEADDRINUSE:
291
        return EADDRINUSE;
292
//  10049 - Cannot assign requested address.
293
    case WSAEADDRNOTAVAIL:
294
        return EADDRNOTAVAIL;
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
//  10050 - Network is down.
    case WSAENETDOWN:
        return ENETDOWN;
//  10051 - Network is unreachable.
    case WSAENETUNREACH:
        return ENETUNREACH;
//  10052 - Network dropped connection on reset.
    case WSAENETRESET:
        return ENETRESET;
//  10053 - Software caused connection abort.
    case WSAECONNABORTED:
        return ECONNABORTED;
//  10054 - Connection reset by peer.
    case WSAECONNRESET:
        return ECONNRESET;
//  10055 - No buffer space available.
    case WSAENOBUFS:
        return ENOBUFS;
313 314 315
//  10056 - Socket is already connected.
    case WSAEISCONN:
        return EFAULT;
316 317 318
//  10057 - Socket is not connected.
    case WSAENOTCONN:
        return ENOTCONN;
319 320 321 322 323 324
//  10058 - Can't send after socket shutdown.
    case WSAESHUTDOWN:
        return EFAULT;
//  10059 - Too many references can't splice.
    case WSAETOOMANYREFS:
        return EFAULT;
325 326 327 328 329 330
//  10060 - Connection timed out.
    case WSAETIMEDOUT:
        return ETIMEDOUT;
//  10061 - Connection refused.
    case WSAECONNREFUSED:
        return ECONNREFUSED;
331 332 333 334 335 336 337 338 339
//  10062 - Too many levels of symbolic links.
    case WSAELOOP:
        return EFAULT;
//  10063 - File name too long.
    case WSAENAMETOOLONG:
        return EFAULT;
//  10064 - Host is down.
    case WSAEHOSTDOWN:
        return EAGAIN;
340 341 342
//  10065 - No route to host.
    case WSAEHOSTUNREACH:
        return EHOSTUNREACH;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
//  10066 - Directory not empty.
    case WSAENOTEMPTY:
        return EFAULT;
//  10067 - Too many processes.
    case WSAEPROCLIM:
        return EFAULT;
//  10068 - Too many users.
    case WSAEUSERS:
        return EFAULT;
//  10069 - Disc Quota Exceeded.
    case WSAEDQUOT:
        return EFAULT;
//  10070 - Stale NFS file handle.
    case WSAESTALE:
        return EFAULT;
//  10071 - Too many levels of remote in path.
    case WSAEREMOTE:
        return EFAULT;
//  10091 - Network SubSystem is unavailable.
    case WSASYSNOTREADY:
        return EFAULT;
//  10092 - WINSOCK DLL Version out of range.
    case WSAVERNOTSUPPORTED:
        return EFAULT;
//  10093 - Successful WSASTARTUP not yet performed.
    case WSANOTINITIALISED:
        return EFAULT;
//  11001 - Host not found.
    case WSAHOST_NOT_FOUND:
        return EFAULT;
//  11002 - Non-Authoritative Host not found.
    case WSATRY_AGAIN:
        return EFAULT;
//  11003 - Non-Recoverable errors: FORMERR REFUSED NOTIMP.
    case WSANO_RECOVERY:
        return EFAULT;
//  11004 - Valid name no data record of requested.
    case WSANO_DATA:
        return EFAULT;
382 383 384
    default:
        wsa_assert (false);
    }
385 386
    //  Not reachable
    return 0;
387 388
}

Martin Sustrik's avatar
Martin Sustrik committed
389
#endif
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

#ifdef HAVE_LIBUNWIND

#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <dlfcn.h>
#include <cxxabi.h>

void zmq::print_backtrace (void)
{
    Dl_info dl_info;
    unw_cursor_t cursor;
    unw_context_t ctx;
    unsigned frame_n = 0;

    unw_getcontext (&ctx);
    unw_init_local (&cursor, &ctx);

    while (unw_step (&cursor) > 0) {
        unw_word_t offset;
        unw_proc_info_t p_info;
        const char *file_name;
        char *demangled_name;
        char func_name[256] = "";
        void *addr;
        int rc;

        if (unw_get_proc_info (&cursor, &p_info))
            break;

        addr = (void *)(p_info.start_ip + offset);

        if (dladdr (addr, &dl_info) && dl_info.dli_fname)
            file_name = dl_info.dli_fname;
        else
            file_name = "?";

        rc = unw_get_proc_name (&cursor, func_name, 256, &offset);
        if (rc == -UNW_ENOINFO)
            strcpy(func_name, "?");

        demangled_name = abi::__cxa_demangle (func_name, NULL, NULL, &rc);

        printf ("#%u  %p in %s (%s+0x%lx)\n", frame_n++, addr, file_name,
                rc ? func_name : demangled_name, (unsigned long) offset);
        free (demangled_name);
    }

    fflush (stdout);
}

#else

void zmq::print_backtrace (void)
{
}

#endif