source: mainline/uspace/lib/c/generic/assert.c@ 13f2461

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 13f2461 was 13f2461, checked in by Jakub Jermar <jakub@…>, 14 years ago

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.

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32
33#include <assert.h>
34#include <stdio.h>
35#include <io/klog.h>
36#include <stdlib.h>
37#include <atomic.h>
38#include <stacktrace.h>
39
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)
48{
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,
72 cond, file, line);
73 stacktrace_print();
74
75 abort();
76}
77
78/** @}
79 */
Note: See TracBrowser for help on using the repository browser.