Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7bd9d4c673fddb8e4cac94ba41f4011803b4245c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************
 * Copyright (c) 2007, 2009 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * This module defines agent error codes in addition to system codes defined in errno.h
 */

#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "errors.h"
#include "events.h"
#include "streams.h"
#include "myalloc.h"
#include "json.h"
#include "trace.h"

#define ERR_MESSAGE_MIN         (STD_ERR_BASE + 100)
#define ERR_MESSAGE_MAX         (STD_ERR_BASE + 199)

#define MESSAGE_CNT             (ERR_MESSAGE_MAX - ERR_MESSAGE_MIN + 1)

#define SRC_SYSTEM  1
#define SRC_GAI     2
#define SRC_MESSAGE 3
#define SRC_REPORT  4

typedef struct ReportBuffer {
    ErrorReport pub; /* public part of error report */
    int refs;
    int gets;
} ReportBuffer;

typedef struct ErrorMessage {
    int source;
    int error;
    char * text;
    ReportBuffer * report;
} ErrorMessage;

static ErrorMessage msgs[MESSAGE_CNT];
static int msgs_pos = 0;

static void release_report(ReportBuffer * report) {
    if (report == NULL) return;
    assert(report->refs > report->gets);
    report->refs--;
    if (report->refs == 0) {
        while (report->pub.props != NULL) {
            ErrorReportItem * i = report->pub.props;
            report->pub.props = i->next;
            loc_free(i->name);
            loc_free(i->value);
            loc_free(i);
        }
        loc_free(report->pub.format);
        loc_free(report);
    }
}

static ErrorMessage * alloc_msg(int source) {
    ErrorMessage * m = msgs + msgs_pos;
    assert(is_dispatch_thread());
    errno = ERR_MESSAGE_MIN + msgs_pos++;
    if (msgs_pos >= MESSAGE_CNT) msgs_pos = 0;
    release_report(m->report);
    loc_free(m->text);
    m->source = source;
    m->error = 0;
    m->report = NULL;
    m->text = NULL;
    return m;
}

#ifdef WIN32

static char * system_strerror(DWORD errno_win32) {
    static char msg[512];
    WCHAR * buf = NULL;
    assert(is_dispatch_thread());
    if (!FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_MAX_WIDTH_MASK,
        NULL,
        errno_win32,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
        (LPWSTR)&buf, 0, NULL) ||
        !WideCharToMultiByte(CP_UTF8, 0, buf, -1, msg, sizeof(msg), NULL, NULL))
    {
        snprintf(msg, sizeof(msg), "System Error Code %lu", (unsigned long)errno_win32);
    }
    if (buf != NULL) LocalFree(buf);
    return msg;
}

typedef struct EventArgs {
    HANDLE done;
    int win32_code;
    int error_code;
} EventArgs;

static void set_win32_errno_event(void * args) {
    ErrorMessage * m = NULL;
    EventArgs * e = (EventArgs *)args;

    m = alloc_msg(SRC_SYSTEM);
    m->error = e->win32_code;
    e->error_code = errno;
    SetEvent(e->done);
}

int set_win32_errno(DWORD win32_error_code) {
    if (win32_error_code) {
        if (is_dispatch_thread()) {
            ErrorMessage * m = alloc_msg(SRC_SYSTEM);
            m->error = win32_error_code;
        }
        else {
            /* Called on background thread */
            int error = 0;
            EventArgs * e = (EventArgs *)loc_alloc_zero(sizeof(EventArgs));
            e->done = CreateEvent(NULL, TRUE, FALSE, NULL);
            e->win32_code = win32_error_code;
            post_event(set_win32_errno_event, e);
            WaitForSingleObject(e->done, INFINITE);
            CloseHandle(e->done);
            error = e->error_code;
            loc_free(e);
            errno = error;
        }
    }
    else {
        errno = 0;
    }
    return errno;
}

#endif

const char * errno_to_str(int err) {
    switch (err) {
    case ERR_ALREADY_STOPPED:   return "Already stopped";
    case ERR_ALREADY_EXITED:    return "Already exited";
    case ERR_ALREADY_RUNNING:   return "Already running";
    case ERR_JSON_SYNTAX:       return "JSON syntax error";
    case ERR_PROTOCOL:          return "Protocol format error";
    case ERR_INV_CONTEXT:       return "Invalid context ID";
    case ERR_INV_ADDRESS:       return "Invalid address";
    case ERR_EOF:               return "End of file";
    case ERR_BASE64:            return "Invalid BASE64 string";
    case ERR_INV_EXPRESSION:    return "Invalid expression";
    case ERR_SYM_NOT_FOUND:     return "Symbol not found";
    case ERR_ALREADY_ATTACHED:  return "Already attached";
    case ERR_BUFFER_OVERFLOW:   return "Buffer overflow";
    case ERR_INV_FORMAT:        return "Format is not supported";
    case ERR_INV_NUMBER:        return "Invalid number";
    case ERR_IS_RUNNING:        return "Execution context is running";
    case ERR_INV_DWARF:         return "Error reading DWARF data";
    case ERR_UNSUPPORTED:       return "Unsupported command";
    case ERR_CHANNEL_CLOSED:    return "Channel closed";
    case ERR_COMMAND_CANCELLED: return "Command cancelled";
    case ERR_UNKNOWN_PEER:      return "Unknown peer ID";
    case ERR_INV_DATA_SIZE:     return "Invalid data size";
    case ERR_INV_DATA_TYPE:     return "Invalid data type";
    case ERR_INV_COMMAND:       return "Command is not recognized";
    case ERR_INV_TRANSPORT:     return "Invalid transport name";
    case ERR_CACHE_MISS:        return "Invalid data cache state";
    default:
        if (err >= ERR_MESSAGE_MIN && err <= ERR_MESSAGE_MAX) {
            ErrorMessage * m = msgs + (err - ERR_MESSAGE_MIN);
            if (m->report != NULL && m->report->pub.format != NULL) {
                /* TODO: error report args */
                return m->report->pub.format;
            }
            switch (m->source) {
#ifdef WIN32
            case SRC_SYSTEM:
                return system_strerror(m->error);
#endif
            case SRC_GAI:
                return loc_gai_strerror(m->error);
            case SRC_MESSAGE:
                return m->text;
            case SRC_REPORT:
                return errno_to_str(m->error);
            }
        }
        return strerror(err);
    }
}

int set_errno(int no, const char * msg) {
    errno = no;
    if (no != 0 && msg != NULL) {
        const char * text0 = errno_to_str(no);
        int len = strlen(msg) + strlen(text0) + 4;
        char * text1 = (char *)loc_alloc(len);
        ErrorMessage * m = NULL;
        snprintf(text1, len, "%s. %s", msg, text0);
        m = alloc_msg(SRC_MESSAGE);
        m->error = get_error_code(no);
        m->text = text1;
    }
    return errno;
}

int set_gai_errno(int no) {
    errno = no;
    if (no != 0) {
        ErrorMessage * m = alloc_msg(SRC_GAI);
        m->error = no;
    }
    return errno;
}

int set_error_report_errno(ErrorReport * r) {
    errno = 0;
    if (r != NULL) {
        ReportBuffer * report = (ReportBuffer *)((char *)r - offsetof(ReportBuffer, pub));
        ErrorMessage * m = alloc_msg(SRC_REPORT);
        m->error = report->pub.code + STD_ERR_BASE;
        m->report = report;
        report->refs++;
    }
    return errno;
}

int get_error_code(int no) {
    while (no >= ERR_MESSAGE_MIN && no <= ERR_MESSAGE_MAX) {
        ErrorMessage * m = msgs + (no - ERR_MESSAGE_MIN);
        switch (m->source) {
        case SRC_REPORT:
        case SRC_MESSAGE:
            no = m->error;
            continue;
        }
        return ERR_OTHER;
    }
    return no;
}

static void add_report_prop(ReportBuffer * report, const char * name, ByteArrayOutputStream * buf) {
    ErrorReportItem * i = (ErrorReportItem *)loc_alloc(sizeof(ErrorReportItem));
    i->name = loc_strdup(name);
    get_byte_array_output_stream_data(buf, &i->value, NULL);
    i->next = report->pub.props;
    report->pub.props = i;
}

static void add_report_prop_int(ReportBuffer * report, const char * name, unsigned long n) {
    ByteArrayOutputStream buf;
    OutputStream * out = create_byte_array_output_stream(&buf);
    json_write_ulong(out, n);
    write_stream(out, 0);
    add_report_prop(report, name, &buf);
}

static void add_report_prop_str(ReportBuffer * report, const char * name, const char * str) {
    ByteArrayOutputStream buf;
    OutputStream * out = create_byte_array_output_stream(&buf);
    json_write_string(out, str);
    write_stream(out, 0);
    add_report_prop(report, name, &buf);
}

ErrorReport * get_error_report(int err) {
    ErrorMessage * m = NULL;
    if (err >= ERR_MESSAGE_MIN && err <= ERR_MESSAGE_MAX) {
        m = msgs + (err - ERR_MESSAGE_MIN);
        if (m->report != NULL) {
            m->report->refs++;
            m->report->gets++;
            return &m->report->pub;
        }
    }
    if (err != 0) {
        ReportBuffer * report = (ReportBuffer *)loc_alloc_zero(sizeof(ReportBuffer));
        struct timespec timenow;

        if (clock_gettime(CLOCK_REALTIME, &timenow) == 0) {
            report->pub.time_stamp = (uint64_t)timenow.tv_sec * 1000 + timenow.tv_nsec / 1000000;
        }

        report->pub.format = loc_strdup(errno_to_str(err));

        if (m != NULL) {
            if (m->source == SRC_MESSAGE) {
                err = m->error;
            }
#ifdef WIN32
            else if (m->source == SRC_SYSTEM) {
                add_report_prop_int(report, "AltCode", m->error);
                add_report_prop_str(report, "AltOrg", "WIN32");
                err = ERR_OTHER;
            }
#endif
            else {
                err = ERR_OTHER;
            }
        }

        if (err < STD_ERR_BASE || err > ERR_MESSAGE_MAX) {
            add_report_prop_int(report, "AltCode", err);
#if defined(_MSC_VER)
            add_report_prop_str(report, "AltOrg", "MSC");
#elif defined(_WRS_KERNEL)
            add_report_prop_str(report, "AltOrg", "VxWorks");
#elif defined(__CYGWIN__)
            add_report_prop_str(report, "AltOrg", "CygWin");
#elif defined(__linux__)
            add_report_prop_str(report, "AltOrg", "Linux");
#else
            add_report_prop_str(report, "AltOrg", "POSIX");
#endif
            err = ERR_OTHER;
        }

        assert(err >= STD_ERR_BASE);
        assert(err < ERR_MESSAGE_MIN);

        report->pub.code = err - STD_ERR_BASE;
        report->refs = 1;
        report->gets = 1;
        if (m != NULL) {
            assert(m->report == NULL);
            m->report = report;
            report->refs++;
        }
        return &report->pub;
    }
    return NULL;
}

ErrorReport * create_error_report(void) {
    ReportBuffer * report = (ReportBuffer *)loc_alloc_zero(sizeof(ReportBuffer));
    report->refs = 1;
    report->gets = 1;
    return &report->pub;
}

void release_error_report(ErrorReport * r) {
    if (r != NULL) {
        ReportBuffer * report = (ReportBuffer *)((char *)r - offsetof(ReportBuffer, pub));
        assert(is_dispatch_thread());
        assert(report->gets > 0);
        report->gets--;
        release_report(report);
    }
}

#ifdef NDEBUG

void check_error(int error) {
    if (error == 0) return;
#if ENABLE_Trace
    trace(LOG_ALWAYS, "Fatal error %d: %s", error, errno_to_str(error));
    trace(LOG_ALWAYS, "  Exiting agent...");
    if (log_file == stderr) exit(1);
#endif
    fprintf(stderr, "Fatal error %d: %s", error, errno_to_str(error));
    fprintf(stderr, "  Exiting agent...");
    exit(1);
}

#else /* NDEBUG */

void check_error_debug(const char * file, int line, int error) {
    if (error == 0) return;
#if ENABLE_Trace
    trace(LOG_ALWAYS, "Fatal error %d: %s", error, errno_to_str(error));
    trace(LOG_ALWAYS, "  At %s:%d", file, line);
    trace(LOG_ALWAYS, "  Exiting agent...");
    if (log_file == stderr) exit(1);
#endif
    fprintf(stderr, "Fatal error %d: %s", error, errno_to_str(error));
    fprintf(stderr, "  At %s:%d", file, line);
    fprintf(stderr, "  Exiting agent...");
    exit(1);
}

#endif /* NDEBUG */

Back to the top