Skip to content

Commit 8d24508

Browse files
authored
Improve assert() failure output (#956)
Changes: - _ast_assertfail(): Use glibc's backtrace() and backtrace_symbols() to produce a backtrace after a failed assertion. - Compile with -rdynamic or -Wl,-export-dynamic when available. Glibc's backtrace_symbols() function will fail to print readable function names if this is not included in the linker's flags, resulting in significantly worse backtraces. (This flag should've been part of #357, but it appears to have been forgotten about/omitted accidentally.) - nv_endsubscript(): Add an error message prior to calling assert to hopefully make the intermittent crashing bug easier to trace (I can't trigger it manually at all, so hopefully the next CI crash produces more useful output.) - ci.yml: Compile CI builds with CCFLAGS=-Og and LDFLAGS=-g (required alongside -rdynamic for useful backtraces).
1 parent 356b19f commit 8d24508

9 files changed

Lines changed: 31 additions & 10 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Checkout sources
1616
uses: actions/checkout@main
1717
- name: Build
18-
run: bin/package make -j5
18+
run: bin/package make -j5 CCFLAGS=-Og
1919
- name: Regression tests
2020
run: |
2121
PS4="$PS4[ci.yml] "
@@ -30,13 +30,13 @@ jobs:
3030
sed --regexp-extended --in-place=.orig \
3131
'/^SHOPT (AUDIT|BGX|BRACEPAT|DEVFD|DYNAMIC|EDPREDICT|ESH|FIXEDARRAY|HISTEXPAND|MULTIBYTE|NAMESPACE|OPTIMIZE|SPAWN|STATS|SUID_EXEC|VSH)=/ s/=1?/=0/' \
3232
src/cmd/ksh93/SHOPT.sh &&
33-
bin/package make -j5 &&
33+
bin/package make -j5 CCFLAGS=-Og &&
3434
: default regression tests with SHOPTs disabled &&
3535
script -q -e -c "bin/shtests" &&
3636
: enable SHOPT_SCRIPTONLY, rebuild ksh &&
3737
sed --regexp-extended --in-place=.orig \
3838
'/^SHOPT SCRIPTONLY=/ s/=0?/=1/' \
3939
src/cmd/ksh93/SHOPT.sh &&
40-
bin/package make -j5 &&
40+
bin/package make -j5 CCFLAGS=-Og &&
4141
: default regression tests with SHOPT_SCRIPTONLY enabled &&
4242
script -q -e -c "bin/shtests"

src/cmd/builtin/Mamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setv INSTALLROOT ../../..
99
setv CC cc
1010
setv mam_cc_FLAGS %{mam_cc_TARGET} %{mam_cc_DLL} %{-debug-symbols?1?%{mam_cc_DEBUG} -D_BLD_DEBUG?%{mam_cc_OPTIMIZE}?}
1111
setv CCFLAGS
12-
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??}
12+
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??} %{mam_cc_EXPORT_DYNAMIC}
1313
setv IFFEFLAGS
1414
setv LDFLAGS
1515

src/cmd/ksh93/Mamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ setv INSTALLROOT ../../..
88
setv CC cc
99
setv mam_cc_FLAGS %{mam_cc_TARGET} %{mam_cc_DLL} -D_BLD_ksh %{-debug-symbols?1?%{mam_cc_DEBUG} -D_BLD_DEBUG?%{mam_cc_OPTIMIZE}?}
1010
setv CCFLAGS
11-
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??}
11+
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??} %{mam_cc_EXPORT_DYNAMIC}
1212
setv IFFEFLAGS
1313
setv LDFLAGS
1414
setv SH_DICT \"libshell\"

src/cmd/ksh93/sh/array.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,8 @@ char *nv_endsubscript(Namval_t *np, char *cp, int mode)
14831483
{
14841484
int count=1, quoted=0, c;
14851485
char *sp = cp+1;
1486+
if(*cp!='[')
1487+
errormsg(SH_DICT, ERROR_exit(0), "Failure: *cp == %c, cp == '%s'", *cp, cp);
14861488
assert(*cp=='[');
14871489
/* first find matching ']' */
14881490
while(count>0 && (c= *++cp))

src/lib/libast/Mamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ setv INCLUDE_AST %{INSTALLROOT}/include/ast
1010
setv CC cc
1111
setv mam_cc_FLAGS %{mam_cc_TARGET} %{mam_cc_DLL} -D_BLD_ast %{-debug-symbols?1?%{mam_cc_DEBUG} -D_BLD_DEBUG?%{mam_cc_OPTIMIZE}?}
1212
setv CCFLAGS
13-
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??}
13+
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??} %{mam_cc_EXPORT_DYNAMIC}
1414
setv IFFEFLAGS
1515
setv LDFLAGS
1616
setv DYLIB_PRE %{mam_cc_SUFFIX_DYNAMIC?*?%{mam_cc_PREFIX_DYNAMIC}?%{mam_cc_PREFIX_SHARED}?}

src/lib/libast/comp/assert.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,32 @@
1616

1717
#include <ast.h>
1818

19+
#if _hdr_execinfo
20+
#include <execinfo.h>
21+
#endif
22+
1923
noreturn void _ast_assertfail(const char *a, const char *fun, const char *file, int line)
2024
{
2125
#if _has___func__ || _has___FUNCTION__
2226
sfprintf(sfstderr,"\n*** assertion %s failed in %s(), %s:%d\n", a, fun, file, line);
2327
#else
2428
NOT_USED(fun);
2529
sfprintf(sfstderr,"\n*** assertion %s failed in %s:%d\n", a, file, line);
30+
#endif
31+
#if _lib_backtrace && _lib_backtrace_symbols
32+
{
33+
void* callstack[200];
34+
int i, frames = backtrace(callstack, 200);
35+
char** strs = backtrace_symbols(callstack, frames);
36+
if(strs)
37+
{
38+
for (i = 0; i < frames; ++i)
39+
sfprintf(sfstderr, "%s\n", strs[i]);
40+
free(strs);
41+
}
42+
else
43+
sfprintf(sfstderr, "Could not obtain a backtrace (%s)\n", strerror(errno));
44+
}
2645
#endif
2746
sfsync(NULL);
2847
abort();

src/lib/libast/features/lib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmd universe
44

55
sys mman
66
hdr fcntl,dirent,direntry,filio,fnmatch,jioctl,libgen,limits
7-
hdr locale,ndir,nl_types,process,spawn,utime
7+
hdr execinfo,locale,ndir,nl_types,process,spawn,utime
88
hdr linux/fs,linux/msdos_fs
99
hdr wctype
1010
hdr wchar note{ <wchar.h> and isw*() really work }end execute{
@@ -24,7 +24,7 @@ dat _tzname,tzname
2424

2525
lib BSDsetpgrp
2626
lib _cleanup
27-
lib bcopy,bzero,confstr,dirread
27+
lib backtrace,backtrace_symbols,bcopy,bzero,confstr,dirread
2828
lib fchmod,fcntl,fnmatch,fork,fsync
2929
lib getconf,getdents,getdirentries,getdtablesize
3030
lib gethostname,getpagesize,getrlimit,getuniverse

src/lib/libcmd/Mamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ setv mam_cc_FLAGS %{mam_cc_TARGET} %{mam_cc_DLL} %{-debug-symbols?1?%{mam_cc_DEB
1010
setv CCFLAGS
1111
setv IFFEFLAGS
1212
setv LDFLAGS
13-
setv CCLDFLAGS
13+
setv CCLDFLAGS %{mam_cc_EXPORT_DYNAMIC}
1414
setv DYLIB_PRE %{mam_cc_SUFFIX_DYNAMIC?*?%{mam_cc_PREFIX_DYNAMIC}?%{mam_cc_PREFIX_SHARED}?}
1515
setv DYLIB_SUF %{mam_cc_SUFFIX_DYNAMIC-%{mam_cc_SUFFIX_SHARED}}
1616
setv DYLIB_VERSION 4.0

src/lib/libdll/Mamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ setv mam_cc_FLAGS %{mam_cc_TARGET} %{mam_cc_DLL} %{-debug-symbols?1?%{mam_cc_DEB
1010
setv CCFLAGS
1111
setv IFFEFLAGS
1212
setv LDFLAGS
13-
setv CCLDFLAGS
13+
setv CCLDFLAGS %{mam_cc_EXPORT_DYNAMIC}
1414
setv DYLIB_PRE %{mam_cc_SUFFIX_DYNAMIC?*?%{mam_cc_PREFIX_DYNAMIC}?%{mam_cc_PREFIX_SHARED}?}
1515
setv DYLIB_SUF %{mam_cc_SUFFIX_DYNAMIC-%{mam_cc_SUFFIX_SHARED}}
1616
setv DYLIB_VERSION 4.0

0 commit comments

Comments
 (0)