Changeset 13f2461 in mainline


Ignore:
Timestamp:
2011-05-21T15:45:48Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9d47440, e06e2716
Parents:
faeb7cc
Message:

Merge the functionality of assert_abort() and assert_static_abort() into
assert_abort().

Print the unexpanded message to klog first. Then try to print the
message and the stack trace to standard output using printf. Use the
same unabbreviated message wording for both klog and stdout. If a
nested or a parallel assert is detected, send the message only to klog
and then abort.

Re-introduce malloc_assert() to up the malloc_futex. This appears to be
inevitable if we want to print to stdout. To prevent undesired macro
expansion, call assert_abort() from malloc_assert() directly.

Location:
uspace/lib/c
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/assert.c

    rfaeb7cc r13f2461  
    3535#include <io/klog.h>
    3636#include <stdlib.h>
     37#include <atomic.h>
    3738#include <stacktrace.h>
    3839
    39 void assert_abort(const char *cond, const char *file, unsigned int line)
     40#define MSG_START       "Assertion failed ("
     41#define MSG_FILE        ") in file \""
     42#define MSG_LINE        "\", line "
     43#define MSG_END         ".\n"
     44
     45static atomic_t failed_asserts;
     46
     47void assert_abort(const char *cond, const char *file, const char *line)
    4048{
    41         printf("Assertion failed (%s) in file \"%s\", line %u.\n",
     49        /*
     50         * Send the message safely to klog. Nested asserts should not occur.
     51         */
     52        klog_write(MSG_START, str_size(MSG_START));
     53        klog_write(cond, str_size(cond));
     54        klog_write(MSG_FILE, str_size(MSG_FILE));
     55        klog_write(file, str_size(file));
     56        klog_write(MSG_LINE, str_size(MSG_LINE));
     57        klog_write(line, str_size(line));
     58        klog_write(MSG_END, str_size(MSG_END));
     59
     60        /*
     61         * Check if this is a nested or parallel assert.
     62         */
     63        if (atomic_postinc(&failed_asserts))
     64                abort();
     65       
     66        /*
     67         * Attempt to print the message to standard output and display
     68         * the stack trace. These operations can theoretically trigger nested
     69         * assertions.
     70         */
     71        printf(MSG_START "%s" MSG_FILE "%s" MSG_LINE "%s" MSG_END,
    4272            cond, file, line);
    4373        stacktrace_print();
    44         abort();
    45 }
    4674
    47 void assert_static_abort(const char *msg)
    48 {
    49         klog_write(msg, str_size(msg));
    5075        abort();
    5176}
  • uspace/lib/c/generic/malloc.c

    rfaeb7cc r13f2461  
    188188static futex_t malloc_futex = FUTEX_INITIALIZER;
    189189
     190#ifndef NDEBUG
     191
     192#define malloc_assert(expr) \
     193        do { \
     194                if (!(expr)) {\
     195                        futex_up(&malloc_futex); \
     196                        assert_abort(#expr, __FILE__, STR2(__LINE__)); \
     197                } \
     198        } while (0)
     199
     200#else /* NDEBUG */
     201
     202#define malloc_assert(expr)
     203
     204#endif /* NDEBUG */
     205
    190206/** Initialize a heap block
    191207 *
     
    228244        heap_block_head_t *head = (heap_block_head_t *) addr;
    229245       
    230         assert_static(head->magic == HEAP_BLOCK_HEAD_MAGIC);
     246        malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);
    231247       
    232248        heap_block_foot_t *foot = BLOCK_FOOT(head);
    233249       
    234         assert_static(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
    235         assert_static(head->size == foot->size);
     250        malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
     251        malloc_assert(head->size == foot->size);
    236252}
    237253
     
    247263        heap_area_t *area = (heap_area_t *) addr;
    248264       
    249         assert_static(area->magic == HEAP_AREA_MAGIC);
    250         assert_static(addr == area->start);
    251         assert_static(area->start < area->end);
    252         assert_static(((uintptr_t) area->start % PAGE_SIZE) == 0);
    253         assert_static(((uintptr_t) area->end % PAGE_SIZE) == 0);
     265        malloc_assert(area->magic == HEAP_AREA_MAGIC);
     266        malloc_assert(addr == area->start);
     267        malloc_assert(area->start < area->end);
     268        malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0);
     269        malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0);
    254270}
    255271
     
    382398       
    383399        block_check((void *) last_head);
    384         assert_static(last_head->area == area);
     400        malloc_assert(last_head->area == area);
    385401       
    386402        if (last_head->free) {
     
    395411               
    396412                block_check((void *) first_head);
    397                 assert_static(first_head->area == area);
     413                malloc_assert(first_head->area == area);
    398414               
    399415                size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE);
     
    497513static void split_mark(heap_block_head_t *cur, const size_t size)
    498514{
    499         assert_static(cur->size >= size);
     515        malloc_assert(cur->size >= size);
    500516       
    501517        /* See if we should split the block. */
     
    533549{
    534550        area_check((void *) area);
    535         assert_static((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    536         assert_static((void *) first_block < area->end);
     551        malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
     552        malloc_assert((void *) first_block < area->end);
    537553       
    538554        for (heap_block_head_t *cur = first_block; (void *) cur < area->end;
     
    661677static void *malloc_internal(const size_t size, const size_t align)
    662678{
    663         assert_static(first_heap_area != NULL);
     679        malloc_assert(first_heap_area != NULL);
    664680       
    665681        if (align == 0)
     
    786802       
    787803        block_check(head);
    788         assert_static(!head->free);
     804        malloc_assert(!head->free);
    789805       
    790806        heap_area_t *area = head->area;
    791807       
    792808        area_check(area);
    793         assert_static((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    794         assert_static((void *) head < area->end);
     809        malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
     810        malloc_assert((void *) head < area->end);
    795811       
    796812        void *ptr = NULL;
     
    863879       
    864880        block_check(head);
    865         assert_static(!head->free);
     881        malloc_assert(!head->free);
    866882       
    867883        heap_area_t *area = head->area;
    868884       
    869885        area_check(area);
    870         assert_static((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    871         assert_static((void *) head < area->end);
     886        malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
     887        malloc_assert((void *) head < area->end);
    872888       
    873889        /* Mark the block itself as free. */
  • uspace/lib/c/include/assert.h

    rfaeb7cc r13f2461  
    4747 */
    4848
     49#define STR(l)  #l
     50#define STR2(l) STR(l)
     51
    4952#ifndef NDEBUG
    5053
     
    5255        do { \
    5356                if (!(expr)) \
    54                         assert_abort(#expr, __FILE__, __LINE__); \
    55         } while (0)
    56 
    57 #define assert_static(expr) \
    58         do { \
    59                 if (!(expr)) \
    60                         assert_static_abort("Assertion failed (" #expr \
    61                             ") in file \"" __FILE__ "\".\n"); \
     57                        assert_abort(#expr, __FILE__, STR2(__LINE__)); \
    6258        } while (0)
    6359
     
    6561
    6662#define assert(expr)
    67 #define assert_static(expr)
    6863
    6964#endif /* NDEBUG */
    7065
    71 extern void assert_abort(const char *, const char *, unsigned int)
     66extern void assert_abort(const char *, const char *, const char *)
    7267    __attribute__((noreturn));
    73 extern void assert_static_abort(const char *);
    7468
    7569
Note: See TracChangeset for help on using the changeset viewer.