foo
[glibc.git] / assert / assert.c
blobda7356a824b7e1c459933184a226a421dd2893cc
1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <atomic.h>
20 #include <ldsodefs.h>
21 #include <libintl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sysdep.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
29 extern const char *__progname;
31 #include <wchar.h>
32 #include <libio/iolibio.h>
33 #define fflush(s) _IO_fflush (s)
35 /* This function, when passed a string containing an asserted
36 expression, a filename, and a line number, prints a message
37 on the standard error stream of the form:
38 a.c:10: foobar: Assertion `a == b' failed.
39 It then aborts program execution via a call to `abort'. */
41 #ifdef FATAL_PREPARE_INCLUDE
42 # include FATAL_PREPARE_INCLUDE
43 #endif
46 void
47 __assert_fail_base (const char *fmt, const char *assertion, const char *file,
48 unsigned int line, const char *function)
50 char *str;
52 #ifdef FATAL_PREPARE
53 FATAL_PREPARE;
54 #endif
56 int total;
57 if (__asprintf (&str, fmt,
58 __progname, __progname[0] ? ": " : "",
59 file, line,
60 function ? function : "", function ? ": " : "",
61 assertion, &total) >= 0)
63 /* Print the message. */
64 (void) __fxprintf (NULL, "%s", str);
65 (void) fflush (stderr);
67 total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
68 struct abort_msg_s *buf = __mmap (NULL, total, PROT_READ | PROT_WRITE,
69 MAP_ANON | MAP_PRIVATE, -1, 0);
70 if (__glibc_likely (buf != MAP_FAILED))
72 buf->size = total;
73 strcpy (buf->msg, str);
75 /* We have to free the old buffer since the application might
76 catch the SIGABRT signal. */
77 struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg, buf);
79 if (old != NULL)
80 __munmap (old, old->size);
83 free (str);
85 else
87 /* At least print a minimal message. */
88 static const char errstr[] = "Unexpected error.\n";
89 __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
92 abort ();
96 #undef __assert_fail
97 void
98 __assert_fail (const char *assertion, const char *file, unsigned int line,
99 const char *function)
101 __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
102 assertion, file, line, function);
104 hidden_def(__assert_fail)