bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH hurd 1/2] libshouldbeinlibc: add assert.h variant that prints bac


From: Justus Winter
Subject: [PATCH hurd 1/2] libshouldbeinlibc: add assert.h variant that prints backtraces
Date: Tue, 28 Oct 2014 23:18:37 +0100

* libshouldbeinlibc/assert-backtrace.h: New file.
* libshouldbeinlibc/Makefile (installhdrs): Add assert-backtrace.h.
---
 libshouldbeinlibc/Makefile           |  4 ++-
 libshouldbeinlibc/assert-backtrace.h | 68 ++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 libshouldbeinlibc/assert-backtrace.h

diff --git a/libshouldbeinlibc/Makefile b/libshouldbeinlibc/Makefile
index 14a7939..b32e1bd 100644
--- a/libshouldbeinlibc/Makefile
+++ b/libshouldbeinlibc/Makefile
@@ -29,7 +29,9 @@ SRCS = termsize.c timefmt.c exec-reauth.c maptime-funcs.c \
        ugids-auth.c ugids-xinl.c ugids-merge.c ugids-imply.c ugids-posix.c \
        ugids-verify-auth.c nullauth.c
 installhdrs = idvec.h timefmt.h maptime.h \
-             wire.h portinfo.h portxlate.h cacheq.h ugids.h nullauth.h
+             wire.h portinfo.h portxlate.h cacheq.h ugids.h nullauth.h \
+             assert-backtrace.h \
+
 installhdrsubdir = .
 
 OBJS = $(SRCS:.c=.o)
diff --git a/libshouldbeinlibc/assert-backtrace.h 
b/libshouldbeinlibc/assert-backtrace.h
new file mode 100644
index 0000000..7baf4b3
--- /dev/null
+++ b/libshouldbeinlibc/assert-backtrace.h
@@ -0,0 +1,68 @@
+/* Augment failing assertions with backtraces.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of the GNU Hurd.
+
+   The GNU Hurd is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
+
+   The GNU Hurd is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef __ASSERT_BACKTRACE__
+#define __ASSERT_BACKTRACE__
+
+#ifndef NDEBUG
+
+#include <assert.h>
+#include <errno.h>
+#include <execinfo.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/* This prints an "Assertion failed" message and aborts.  */
+static void __assert_fail_backtrace (const char *__assertion,
+                                     const char *__file,
+                                     unsigned int __line,
+                                     const char *__function)
+  __attribute__ ((noreturn, unused));
+
+#undef assert
+#define assert(expr)                                                   \
+  ((expr)                                                              \
+   ? __ASSERT_VOID_CAST (0)                                            \
+   : __assert_fail_backtrace (__STRING(expr), __FILE__, __LINE__, 
__ASSERT_FUNCTION))
+
+static inline void
+__assert_fail_backtrace (const char *__assertion, const char *__file,
+                         unsigned int __line, const char *__function)
+{
+#define SIZE   128
+#define SKIP   1
+  int nptrs;
+  void *buffer[SIZE];
+
+  error (0, 0, "%s:%d: %s: Assertion `%s' failed.",
+         __file, __line, __function, __assertion);
+
+  nptrs = backtrace(buffer, SIZE);
+  if (nptrs == 0)
+    error (1, errno, "backtrace");
+
+  backtrace_symbols_fd (&buffer[SKIP], nptrs - SKIP, STDERR_FILENO);
+  fflush (stderr);
+  exit (EXIT_FAILURE);
+#undef SKIP
+#undef SIZE
+}
+
+#endif /* NDEBUG */
+#endif /* __ASSERT_BACKTRACE__ */
-- 
2.1.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]