elf: Remove LD_PROFILE for static binaries
[glibc.git] / assert / assert.c
blobbf0f4a69f58fc424750cbfe9122a9ec50ddb10a1
1 /* Copyright (C) 1991-2023 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 <https://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>
27 #include <setvmaname.h>
30 extern const char *__progname;
32 #include <wchar.h>
33 #include <libio/iolibio.h>
34 #define fflush(s) _IO_fflush (s)
36 /* This function, when passed a string containing an asserted
37 expression, a filename, and a line number, prints a message
38 on the standard error stream of the form:
39 a.c:10: foobar: Assertion `a == b' failed.
40 It then aborts program execution via a call to `abort'. */
42 #ifdef FATAL_PREPARE_INCLUDE
43 # include FATAL_PREPARE_INCLUDE
44 #endif
47 void
48 __assert_fail_base (const char *fmt, const char *assertion, const char *file,
49 unsigned int line, const char *function)
51 char *str;
53 #ifdef FATAL_PREPARE
54 FATAL_PREPARE;
55 #endif
57 int total;
58 if (__asprintf (&str, fmt,
59 __progname, __progname[0] ? ": " : "",
60 file, line,
61 function ? function : "", function ? ": " : "",
62 assertion, &total) >= 0)
64 /* Print the message. */
65 (void) __fxprintf (NULL, "%s", str);
66 (void) fflush (stderr);
68 total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
69 struct abort_msg_s *buf = __mmap (NULL, total, PROT_READ | PROT_WRITE,
70 MAP_ANON | MAP_PRIVATE, -1, 0);
71 if (__glibc_likely (buf != MAP_FAILED))
73 buf->size = total;
74 strcpy (buf->msg, str);
75 __set_vma_name (buf, total, " glibc: assert");
77 /* We have to free the old buffer since the application might
78 catch the SIGABRT signal. */
79 struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg, buf);
81 if (old != NULL)
82 __munmap (old, old->size);
85 free (str);
87 else
89 /* At least print a minimal message. */
90 static const char errstr[] = "Unexpected error.\n";
91 __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
94 abort ();
98 #undef __assert_fail
99 void
100 __assert_fail (const char *assertion, const char *file, unsigned int line,
101 const char *function)
103 __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
104 assertion, file, line, function);