Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2012 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "error.h"

#include <assert.h>
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/// Generic hook to format an error that does not have a format callback.
///
/// \param error Error for which to generate a message.
/// \param output_buffer Buffer to hold the generated message.
/// \param output_size Length of output_buffer.
static int
generic_format_callback(const kyua_error_t error, char* const output_buffer,
                        size_t output_size)
{
    assert(error != NULL);
    return snprintf(output_buffer, output_size, "Error '%s'", error->type_name);
}


/// Initializes an error object.
///
/// \param error Error for which to generate a message.
/// \param type_name Name of the error type.
/// \param data Opaque data that belongs to the error, for usage by
///     error-specific methods like format_callback.
/// \param data_size Size of the opaque data object.
/// \param format_callback Type-specific method to generate a user
///     representation of the error.
///
/// \return True if the initialization succeeds; false otherwise.  If
/// false, the error object passed in has not been modified.
static bool
error_init(kyua_error_t const error, const char* const type_name,
           void* const data, const size_t data_size,
           const kyua_error_format_callback format_callback)
{
    assert(data != NULL || data_size == 0);
    assert(data_size != 0 || data == NULL);

    bool ok;

    if (data == NULL) {
        error->data = NULL;
        error->needs_free = false;
        ok = true;
    } else {
        void* new_data = malloc(data_size);
        if (new_data == NULL) {
            ok = false;
        } else {
            memcpy(new_data, data, data_size);
            error->data = new_data;
            ok = true;
        }
    }

    if (ok) {
        error->type_name = type_name;
        error->format_callback = (format_callback == NULL) ?
            generic_format_callback : format_callback;
    }

    return ok;
}


/// Allocates and initializes a new error.
///
/// \param type_name Name of the error type.
/// \param data Opaque data that belongs to the error, for usage by
///     error-specific methods like format_callback.
/// \param data_size Size of the opaque data object.
/// \param format_callback Type-specific method to generate a user
///     representation of the error.
///
/// \return The newly initialized error, or an out of memory error.
kyua_error_t
kyua_error_new(const char* const type_name, void* const data,
               const size_t data_size,
               const kyua_error_format_callback format_callback)
{
    assert(data != NULL || data_size == 0);
    assert(data_size != 0 || data == NULL);

    kyua_error_t error = malloc(sizeof(struct kyua_error));
    if (error == NULL)
        error = kyua_oom_error_new();
    else {
        if (!error_init(error, type_name, data, data_size, format_callback)) {
            free(error);
            error = kyua_oom_error_new();
        } else {
            error->needs_free = true;
        }
    }

    assert(error != NULL);
    return error;
}


/// Releases an error.
///
/// \param error The error object to release.
void
kyua_error_free(kyua_error_t error)
{
    assert(error != NULL);

    const bool needs_free = error->needs_free;

    if (error->data != NULL)
        free(error->data);
    if (needs_free)
        free(error);
}


/// Returns the "most important" of two errors.
///
/// "Most important" is defined as: the primary error is returned if set,
/// otherwise the secondary error is returned.
///
/// It is the responsibility of the caller to free the *resulting* error of this
/// call.  The original errors passed in should not be consulted any longer,
/// because it is impossible to know which one was chosen.
///
/// \param primary The primary error to compare.
/// \param [in,out] secondary The secondary error to compare.  This is freed if
///     the primary error is set.
///
/// \return Either primary or secondary.
kyua_error_t
kyua_error_subsume(kyua_error_t primary, kyua_error_t secondary)
{
    if (kyua_error_is_set(primary)) {
        if (kyua_error_is_set(secondary))
            kyua_error_free(secondary);
        return primary;
    } else {
        return secondary;
    }
}


/// Constructor for a no-error condition.
///
/// \return Opaque representation of a no-error condition.
kyua_error_t
kyua_error_ok(void)
{
    return NULL;
}


/// Checks if the given error object represents an error or not.
///
/// \param error The error to check.
///
/// \return True if the error is set.
bool
kyua_error_is_set(const kyua_error_t error)
{
    return error != NULL;
}


/// Checks if the given error object is of a specific type.
///
/// \pre The error must be set.
///
/// \param error The error to check.
/// \param type_name The type of the expected error.
///
/// \return True if the error is of type type_name.
bool
kyua_error_is_type(const kyua_error_t error, const char* type_name)
{
    assert(error != NULL);

    return strcmp(error->type_name, type_name) == 0;
}


/// Returns a pointer to the error-specific data.
///
/// \pre The error must be set.
///
/// \param error The error to query.
///
/// \return An opaque pointer to the error data.  This should only be
/// dereferenced by the methods of the error class that created it.
const void*
kyua_error_data(const kyua_error_t error)
{
    assert(error != NULL);

    return error->data;
}


/// Generates a user-friendly representation of the error.
///
/// This cannot fail, but it is possible that the generated error does not
/// fit in the provided buffer.
///
/// \pre The error must be set.
///
/// \param error Error for which to generate a message.
/// \param output_buffer Buffer to hold the generated message.
/// \param output_size Length of output_buffer.
///
/// \return The number of bytes written to output_buffer, or a negative value if
/// there was an error.
int
kyua_error_format(const kyua_error_t error, char* const output_buffer,
                  const size_t output_size)
{
    assert(kyua_error_is_set(error));
    return error->format_callback(error, output_buffer, output_size);
}


/// Formats a string and appends an error code to it.
///
/// \param error Error to append to the formatted message.
/// \param format User-specified message, as a formatting string.
/// \param ap List of arguments to the format string.
/// \param [out] output_buffer Buffer into which to write the message.
/// \param output_size Length of the output_buffer.
///
/// \return The number of bytes written to output_buffer, or a negative value if
/// there was an error.
static int
format_user_message(const kyua_error_t error, const char* format, va_list ap,
                    char* const output_buffer, const size_t output_size)
{
    assert(kyua_error_is_set(error));

    va_list ap2;
    va_copy(ap2, ap);
    size_t written = vsnprintf(output_buffer, output_size, format, ap2);
    va_end(ap2);
    if (written >= output_size)
        return -1;

    written += snprintf(output_buffer + written, output_size - written, ": ");
    if (written >= output_size)
        return -1;

    return kyua_error_format(error, output_buffer + written,
                             output_size - written);
}


/// Version of err(3) that works with kyua_error_t objects.
///
/// \param exit_code Error code with which to terminate the execution.
/// \param error Error to append to the output.
/// \param format User-specified message, as a formatting string.
/// \param ... Positional arguments to the format string.
///
/// \post Execution terminates with exit_code.
void
kyua_error_err(const int exit_code, const kyua_error_t error,
               const char* format, ...)
{
    char buffer[2048];

    va_list ap;
    va_start(ap, format);
    (void)format_user_message(error, format, ap, buffer, sizeof(buffer));
    va_end(ap);
    kyua_error_free(error);

    errx(exit_code, "%s", buffer);
}


/// Writes an error to a file stream.
///
/// \param stream Stream to which to write the message.
/// \param error Error to append to the output.  This is not released.
/// \param format User-specified message, as a formatting string.
/// \param ... Positional arguments to the format string.
void
kyua_error_fprintf(FILE* stream, const kyua_error_t error,
                   const char* format, ...)
{
    char buffer[2048];

    va_list ap;
    va_start(ap, format);
    (void)format_user_message(error, format, ap, buffer, sizeof(buffer));
    va_end(ap);

    fprintf(stream, "%s", buffer);
}


/// Version of warn(3) that works with kyua_error_t objects.
///
/// \param error Error to append to the output.  This is not released.
/// \param format User-specified message, as a formatting string.
/// \param ... Positional arguments to the format string.
void
kyua_error_warn(const kyua_error_t error, const char* format, ...)
{
    char buffer[2048];

    va_list ap;
    va_start(ap, format);
    (void)format_user_message(error, format, ap, buffer, sizeof(buffer));
    va_end(ap);

    warnx("%s", buffer);
}


/// Name of an generic error type.
const char* const kyua_generic_error_type = "generic";


/// Generates a user-friendly representation of the error.
///
/// \pre The error must be set.
///
/// \param error Error for which to generate a message.
/// \param output_buffer Buffer to hold the generated message.
/// \param output_size Length of output_buffer.
///
/// \return The number of bytes written to output_buffer, or a negative value if
/// there was an error.
static int
generic_format(const kyua_error_t error, char* const output_buffer,
             const size_t output_size)
{
    assert(kyua_error_is_type(error, kyua_generic_error_type));

    const char* message = kyua_error_data(error);
    return snprintf(output_buffer, output_size, "%s", message);
}


/// Constructs a new generic error.
///
/// \param message Textual description of the problem.
/// \param ... Positional arguments for the description.
///
/// \return The generated error.
kyua_error_t
kyua_generic_error_new(const char* message, ...)
{
    char formatted[1024];
    va_list ap;

    va_start(ap, message);
    (void)vsnprintf(formatted, sizeof(formatted), message, ap);
    va_end(ap);

    return kyua_error_new(kyua_generic_error_type, formatted, sizeof(formatted),
                          generic_format);
}


/// Name of a libc type.
const char* const kyua_libc_error_type = "libc";


/// Representation of a libc error.
struct libc_error_data {
    /// Value of the errno captured during the error creation.
    int original_errno;

    /// Explanation of the problem that lead to the error.
    char description[4096];
};
/// Shorthand for a libc_error_data structure.
typedef struct libc_error_data libc_error_data_t;


/// Generates a user-friendly representation of the error.
///
/// \pre The error must be set.
///
/// \param error Error for which to generate a message.
/// \param output_buffer Buffer to hold the generated message.
/// \param output_size Length of output_buffer.
///
/// \return The number of bytes written to output_buffer, or a negative value if
/// there was an error.
static int
libc_format(const kyua_error_t error, char* const output_buffer,
            const size_t output_size)
{
    assert(kyua_error_is_type(error, kyua_libc_error_type));

    const libc_error_data_t* data = kyua_error_data(error);
    return snprintf(output_buffer, output_size, "%s: %s", data->description,
                    strerror(data->original_errno));
}


/// Constructs a new libc error.
///
/// \param original_errno libc error code for this error.
/// \param description Textual description of the problem.
/// \param ... Positional arguments for the description.
///
/// \return The generated error.
kyua_error_t
kyua_libc_error_new(const int original_errno, const char* description, ...)
{
    va_list ap;

    const size_t data_size = sizeof(libc_error_data_t);
    libc_error_data_t* data = (libc_error_data_t*)malloc(data_size);
    if (data == NULL)
        return kyua_oom_error_new();

    data->original_errno = original_errno;
    va_start(ap, description);
    (void)vsnprintf(data->description, sizeof(data->description),
                    description, ap);
    va_end(ap);

    return kyua_error_new(kyua_libc_error_type, data, data_size, libc_format);
}


/// Extracts the original errno of a libc error.
///
/// \pre error must have been constructed by kyua_libc_error_new.
///
/// \param error The error object to access.
///
/// \return The libc error code.
int
kyua_libc_error_errno(const kyua_error_t error)
{
    assert(kyua_error_is_type(error, kyua_libc_error_type));

    const struct libc_error_data* data = kyua_error_data(error);
    return data->original_errno;
}


/// Name of an OOM type.
const char* const kyua_oom_error_type = "oom";


/// Data of an out of memory error.
///
/// All error types are allocated in dynamic memory.  However, doing so for
/// an out of memory error is not possible because, when we are out of
/// memory, we probably cannot allocate more memory to generate an error.
/// Therefore, we just keep a single static instance of the out of memory
/// error around all the time.
static struct kyua_error oom_error;


/// Generates a user-friendly representation of the error.
///
/// \pre The error must be set.
///
/// \param error Error for which to generate a message.
/// \param output_buffer Buffer to hold the generated message.
/// \param output_size Length of output_buffer.
///
/// \return The number of bytes written to output_buffer, or a negative value if
/// there was an error.
static int
oom_format(const kyua_error_t error, char* const output_buffer,
           const size_t output_size)
{
    assert(kyua_error_is_type(error, kyua_oom_error_type));

    return snprintf(output_buffer, output_size, "Not enough memory");
}


/// Constructs a new out-of-memory error.
///
/// This will always succeed because we just return a reference to the
/// statically-allocated oom_error.
///
/// \return An error representing an out of memory condition.
kyua_error_t
kyua_oom_error_new(void)
{
    // This is idempotent; no need to ensure that we call it only once.
    const bool ok = error_init(&oom_error, kyua_oom_error_type, NULL, 0,
                               oom_format);
    assert(ok);

    return &oom_error;
}


/// Name of an usage error type.
const char* const kyua_usage_error_type = "usage";


/// Generates a user-friendly representation of the error.
///
/// \pre The error must be set.
///
/// \param error Error for which to generate a message.
/// \param output_buffer Buffer to hold the generated message.
/// \param output_size Length of output_buffer.
///
/// \return The number of bytes written to output_buffer, or a negative value if
/// there was an error.
static int
usage_format(const kyua_error_t error, char* const output_buffer,
             const size_t output_size)
{
    assert(kyua_error_is_type(error, kyua_usage_error_type));

    const char* message = kyua_error_data(error);
    return snprintf(output_buffer, output_size, "%s", message);
}


/// Constructs a new usage error.
///
/// \param message Textual description of the problem.
/// \param ... Positional arguments for the description.
///
/// \return The generated error.
kyua_error_t
kyua_usage_error_new(const char* message, ...)
{
    char formatted[1024];
    va_list ap;

    va_start(ap, message);
    (void)vsnprintf(formatted, sizeof(formatted), message, ap);
    va_end(ap);

    return kyua_error_new(kyua_usage_error_type, formatted, sizeof(formatted),
                          usage_format);
}