err.cpp 13.2 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
    return wsa_error_no (WSAGetLastError(), NULL);
96 97
}

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

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

224
int zmq::wsa_error_to_errno (int errcode)
225 226
{
    switch (errcode) {
227 228 229
//  10004 - Interrupted system call.
    case WSAEINTR:
        return EINTR;
230
//  10009 - File handle is not valid.
231
    case WSAEBADF:
232
        return EBADF;
233 234 235 236 237 238 239
//  10013 - Permission denied.
    case WSAEACCES:
        return EACCES;
//  10014 - Bad address.
    case WSAEFAULT:
        return EFAULT;
//  10022 - Invalid argument.
240
    case WSAEINVAL:
241
        return EINVAL;
242
//  10024 - Too many open files.
243
    case WSAEMFILE:
244
        return EMFILE;
245 246 247
//  10035 - Operation would block.
    case WSAEWOULDBLOCK:
        return EBUSY;
248 249 250
//  10036 - Operation now in progress.
    case WSAEINPROGRESS:
        return EAGAIN;
251 252 253 254 255
//  10037 - Operation already in progress.
    case WSAEALREADY:
        return EAGAIN;
//  10038 - Socket operation on non-socket.
    case WSAENOTSOCK:
256
        return ENOTSOCK;
257 258 259
//  10039 - Destination address required.
    case WSAEDESTADDRREQ:
        return EFAULT;
260 261 262
//  10040 - Message too long.
    case WSAEMSGSIZE:
        return EMSGSIZE;
263 264 265 266 267 268
//  10041 - Protocol wrong type for socket.
    case WSAEPROTOTYPE:
        return EFAULT;
//  10042 - Bad protocol option.
    case WSAENOPROTOOPT:
        return EINVAL;
269
//  10043 - Protocol not supported.
270
    case WSAEPROTONOSUPPORT:
271
        return EPROTONOSUPPORT;
272 273 274 275 276 277 278 279 280
//  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;
281 282 283 284
//  10047 - Address family not supported by protocol family.
    case WSAEAFNOSUPPORT:
        return EAFNOSUPPORT;
//  10048 - Address already in use.
285
    case WSAEADDRINUSE:
286
        return EADDRINUSE;
287
//  10049 - Cannot assign requested address.
288
    case WSAEADDRNOTAVAIL:
289
        return EADDRNOTAVAIL;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
//  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;
308 309 310
//  10056 - Socket is already connected.
    case WSAEISCONN:
        return EFAULT;
311 312 313
//  10057 - Socket is not connected.
    case WSAENOTCONN:
        return ENOTCONN;
314 315 316 317 318 319
//  10058 - Can't send after socket shutdown.
    case WSAESHUTDOWN:
        return EFAULT;
//  10059 - Too many references can't splice.
    case WSAETOOMANYREFS:
        return EFAULT;
320 321 322 323 324 325
//  10060 - Connection timed out.
    case WSAETIMEDOUT:
        return ETIMEDOUT;
//  10061 - Connection refused.
    case WSAECONNREFUSED:
        return ECONNREFUSED;
326 327 328 329 330 331 332 333 334
//  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;
335 336 337
//  10065 - No route to host.
    case WSAEHOSTUNREACH:
        return EHOSTUNREACH;
338 339 340 341 342 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
//  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;
377 378 379
    default:
        wsa_assert (false);
    }
380 381
    //  Not reachable
    return 0;
382 383
}

Martin Sustrik's avatar
Martin Sustrik committed
384
#endif
385 386 387 388 389 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

#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