1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
From ec94cd5a410c384c721ace20e010e50dbcf57142 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Fri, 20 Jun 2014 15:50:53 +0200
Subject: [PATCH hurd 2/3] libshouldbeinlibc: add assert(3) variant that prints
backtraces
* libshouldbeinlibc/Makefile: Add new files
* libshouldbeinlibc/assert-backtrace.{c,h}: New files.
---
libshouldbeinlibc/Makefile | 2 +
libshouldbeinlibc/assert-backtrace.c | 79 ++++++++++++++++++++++++++++++++++++
libshouldbeinlibc/assert-backtrace.h | 60 +++++++++++++++++++++++++++
3 files changed, 141 insertions(+)
create mode 100644 libshouldbeinlibc/assert-backtrace.c
create mode 100644 libshouldbeinlibc/assert-backtrace.h
diff --git a/libshouldbeinlibc/Makefile b/libshouldbeinlibc/Makefile
index 633d60e..04c085b 100644
--- a/libshouldbeinlibc/Makefile
+++ b/libshouldbeinlibc/Makefile
@@ -29,10 +29,12 @@ 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 \
refcount.c \
+ assert-backtrace.c \
installhdrs = idvec.h timefmt.h maptime.h \
wire.h portinfo.h portxlate.h cacheq.h ugids.h nullauth.h \
refcount.h \
+ assert-backtrace.h \
installhdrsubdir = .
diff --git a/libshouldbeinlibc/assert-backtrace.c b/libshouldbeinlibc/assert-backtrace.c
new file mode 100644
index 0000000..fd81e24
--- /dev/null
+++ b/libshouldbeinlibc/assert-backtrace.c
@@ -0,0 +1,79 @@
+/* Assert with backtraces
+
+ Copyright (C) 2016 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 NDEBUG
+
+#include <error.h>
+#include <errno.h>
+#include <execinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "assert-backtrace.h"
+
+static void __attribute__ ((noreturn))
+__assert_fail_base_backtrace (const char *fmt,
+ const char *assertion,
+ const char *file,
+ unsigned int line,
+ const char *function)
+{
+ const size_t size = 128;
+ const size_t skip = 2;
+ int nptrs;
+ void *buffer[size];
+
+ nptrs = backtrace(buffer, size);
+ if (nptrs == 0)
+ error (1, *__errno_location (), "backtrace");
+
+ fprintf (stderr,
+ fmt, program_invocation_name, file, line, function, assertion);
+ backtrace_symbols_fd (&buffer[skip], nptrs - skip, STDERR_FILENO);
+ fflush (stderr);
+
+ /* Die. */
+ abort ();
+}
+
+void
+__assert_fail_backtrace (const char *assertion, const char *file,
+ unsigned int line, const char *function)
+{
+ __assert_fail_base_backtrace ("%s: %s:%u: %s: Assertion '%s' failed.\n",
+ assertion, file, line, function);
+}
+
+void
+__assert_perror_fail_backtrace (int errnum,
+ const char *file,
+ unsigned int line,
+ const char *function)
+{
+ char errbuf[1024];
+
+ char *e = strerror_r (errnum, errbuf, sizeof errbuf);
+ __assert_fail_base_backtrace ("%s: %s:%u: %s: Unexpected error: %s.\n",
+ e, file, line, function);
+
+}
+
+#endif /* ! defined NDEBUG */
diff --git a/libshouldbeinlibc/assert-backtrace.h b/libshouldbeinlibc/assert-backtrace.h
new file mode 100644
index 0000000..c54b810
--- /dev/null
+++ b/libshouldbeinlibc/assert-backtrace.h
@@ -0,0 +1,60 @@
+/* Augment failing assertions with backtraces.
+
+ Copyright (C) 1994-2015 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__
+
+#ifdef NDEBUG
+
+#define assert_backtrace(expr) ((void) 0)
+#define assert_backtrace_perror(errnum) ((void) 0)
+
+#else /* NDEBUG */
+
+/* This prints an "Assertion failed" message, prints a stack trace,
+ and aborts. */
+void __assert_fail_backtrace (const char *assertion,
+ const char *file,
+ unsigned int line,
+ const char *function)
+ __attribute__ ((noreturn, unused));
+
+/* Likewise, but prints the error text for ERRNUM. */
+void __assert_perror_fail_backtrace (int errnum,
+ const char *file,
+ unsigned int line,
+ const char *function)
+ __attribute__ ((noreturn, unused));
+
+#define assert_backtrace(expr) \
+ ((expr) \
+ ? (void) 0 \
+ : __assert_fail_backtrace (__STRING(expr), \
+ __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__))
+
+#define assert_perror_backtrace(expr) \
+ ((expr == 0) \
+ ? (void) 0 \
+ : __assert_perror_fail_backtrace (expr, \
+ __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__))
+
+#endif /* NDEBUG */
+#endif /* __ASSERT_BACKTRACE__ */
--
2.1.4
|