config_host.mk.in                |    2 ++
 config_host/config_features.h.in |    4 ++++
 configure.ac                     |   14 ++++++++++++++
 sal/Library_sal.mk               |    2 ++
 sal/osl/unx/backtrace.c          |   20 +++++++++++++++-----
 sal/osl/unx/backtrace.h          |   13 +++----------
 sal/osl/unx/signal.cxx           |   32 +++-----------------------------
 7 files changed, 43 insertions(+), 44 deletions(-)

New commits:
commit c1be12396088563ed77ed61e54fd4e153469701d
Author:     Ismael Luceno <[email protected]>
AuthorDate: Tue Dec 28 12:12:13 2021 +0100
Commit:     Jan-Marek Glogowski <[email protected]>
CommitDate: Thu Dec 30 11:04:11 2021 +0100

    Add configure test for the backtrace implementation
    
    Improve robustness by testing explicitly; instead of assuming based on
    the OS, test first the libc and then test for libexecinfo.
    
    The assumption was wrong for FreeBSD (included since 2017) and for
    musl-based systems (not in libc).
    
    Also, remove INCLUDE_BACKTRACE and clean up a bit.
    
    Ref: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=220271
    Change-Id: Ib4b7fd4fe2cdf5f76babe47a21bd2990fcf42649
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127538
    Tested-by: Jenkins
    Reviewed-by: Jan-Marek Glogowski <[email protected]>

diff --git a/config_host.mk.in b/config_host.mk.in
index 7a4bf2cdcce0..1025ee4805f9 100644
--- a/config_host.mk.in
+++ b/config_host.mk.in
@@ -33,6 +33,8 @@ export AVAHI_CFLAGS=$(gb_SPACE)@AVAHI_CFLAGS@
 export AVAHI_LIBS=$(gb_SPACE)@AVAHI_LIBS@
 export LIBATOMIC_OPS_CFLAGS=$(gb_SPACE)@LIBATOMIC_OPS_CFLAGS@
 export LIBATOMIC_OPS_LIBS=$(gb_SPACE)@LIBATOMIC_OPS_LIBS@
+export BACKTRACE_CFLAGS=@BACKTRACE_CFLAGS@
+export BACKTRACE_LIBS=@BACKTRACE_LIBS@
 export BINDIR=@BINDIR@
 export BISON=@BISON@
 export BOOST_CPPFLAGS=@BOOST_CPPFLAGS@
diff --git a/config_host/config_features.h.in b/config_host/config_features.h.in
index e38e46386d83..0f610a5646bc 100644
--- a/config_host/config_features.h.in
+++ b/config_host/config_features.h.in
@@ -99,6 +99,10 @@
  */
 #define HAVE_FEATURE_BREAKPAD 0
 
+/* BACKTRACE - whether a GNU backtrace implementation is available.
+ */
+#define HAVE_FEATURE_BACKTRACE 0
+
 /*
  *  Whether OpenGL is enabled
  */
diff --git a/configure.ac b/configure.ac
index ee16840053ca..825482217b26 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1343,6 +1343,20 @@ if test $_os != "WINNT"; then
 fi
 AC_SUBST(DLOPEN_LIBS)
 
+# Check for a (GNU) backtrace implementation
+AC_ARG_VAR([BACKTRACE_CFLAGS], [Compiler flags needed to use backtrace(3)])
+AC_ARG_VAR([BACKTRACE_LIBS], [Linker flags needed to use backtrace(3)])
+AS_IF([test "x$BACKTRACE_LIBS$BACKTRACE_CFLAGS" = x], [
+    save_LIBS="$LIBS"
+    AC_SEARCH_LIBS([backtrace], [libexecinfo],
+        [case "$ac_cv_search_backtrace" in -l*) 
BACKTRACE_LIBS="$ac_cv_search_backtrace";; esac],
+        [PKG_CHECK_MODULES([BACKTRACE], [libexecinfo], 
[ac_cv_search_backtrace=], [:])])
+    LIBS="$save_LIBS"
+])
+AS_IF([test "x$ac_cv_search_backtrace" != xno ], [
+    AC_DEFINE([HAVE_FEATURE_BACKTRACE])
+])
+
 dnl ===================================================================
 dnl Sanity checks for Emscripten SDK setup
 dnl ===================================================================
diff --git a/sal/Library_sal.mk b/sal/Library_sal.mk
index ac121098273f..30af237cd546 100644
--- a/sal/Library_sal.mk
+++ b/sal/Library_sal.mk
@@ -19,6 +19,7 @@ $(eval $(call 
gb_Library_set_is_ure_library_or_dependency,sal))
 $(eval $(call gb_Library_set_include,sal,\
        $$(INCLUDE) \
        -I$(SRCDIR)/sal/inc \
+       $(BACKTRACE_CFLAGS) \
 ))
 
 $(eval $(call gb_Library_add_defs,sal,\
@@ -71,6 +72,7 @@ $(eval $(call gb_Library_add_libs,sal,\
        $(if $(filter HAIKU,$(OS)), \
                -lnetwork \
        ) \
+       $(BACKTRACE_LIBS) \
 ))
 
 ifeq ($(OS),MACOSX)
diff --git a/sal/osl/unx/backtrace.c b/sal/osl/unx/backtrace.c
index 26005f7715af..f7bd01c334aa 100644
--- a/sal/osl/unx/backtrace.c
+++ b/sal/osl/unx/backtrace.c
@@ -17,16 +17,19 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
+#include "backtrace.h"
+
+#if ! HAVE_FEATURE_BACKTRACE /* no GNU backtrace implementation available */
+
 #include <sal/types.h>
 
-#ifdef __sun
+#ifdef __sun /* Solaris */
 
 #include <dlfcn.h>
 #include <pthread.h>
 #include <setjmp.h>
 #include <stdio.h>
 #include <sys/frame.h>
-#include "backtrace.h"
 
 #if defined(SPARC)
 
@@ -140,7 +143,12 @@ void backtrace_symbols_fd( void **buffer, int size, int fd 
)
 #include <setjmp.h>
 #include <stddef.h>
 #include <stdio.h>
-#include "backtrace.h"
+
+/* no frame.h on FreeBSD */
+struct frame {
+    struct frame *fr_savfp;
+    long    fr_savpc;
+};
 
 #if defined(POWERPC) || defined(POWERPC64)
 
@@ -212,7 +220,7 @@ void backtrace_symbols_fd( void **buffer, int size, int fd )
     }
 }
 
-#elif !defined LINUX && !defined MACOSX && !defined IOS
+#else /* not GNU/BSD/Solaris */
 
 int backtrace( void **buffer, int max_frames )
 {
@@ -231,6 +239,8 @@ void backtrace_symbols_fd( void **buffer, int size, int fd )
     (void)buffer; (void)size; (void)fd;
 }
 
-#endif
+#endif /* not GNU/BSD/Solaris */
+
+#endif /* ! HAVE_FEATURE_BACKTRACE */
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/osl/unx/backtrace.h b/sal/osl/unx/backtrace.h
index 5fafc1c90622..11a9da52efc0 100644
--- a/sal/osl/unx/backtrace.h
+++ b/sal/osl/unx/backtrace.h
@@ -20,7 +20,9 @@
 #ifndef INCLUDED_SAL_OSL_UNX_BACKTRACE_H
 #define INCLUDED_SAL_OSL_UNX_BACKTRACE_H
 
-#if defined (LINUX)
+#include <config_features.h>
+
+#if HAVE_FEATURE_BACKTRACE /* GNU backtrace implementation available */
 
 #include <execinfo.h>
 
@@ -38,15 +40,6 @@ char ** backtrace_symbols(void * const * buffer, int size);
 
 void backtrace_symbols_fd( void **buffer, int size, int fd );
 
-/* no frame.h on FreeBSD */
-#if defined (FREEBSD) || defined (NETBSD) || defined (OPENBSD) || \
-    defined (DRAGONFLY)
-struct frame {
-    struct frame *fr_savfp;
-    long    fr_savpc;
-};
-#endif
-
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
diff --git a/sal/osl/unx/signal.cxx b/sal/osl/unx/signal.cxx
index 79721def6c5e..91cf59ff1db1 100644
--- a/sal/osl/unx/signal.cxx
+++ b/sal/osl/unx/signal.cxx
@@ -27,31 +27,9 @@
 /* system headers */
 #include "system.hxx"
 
-#if defined( MACOSX )
-
-#if defined( INTEL )
-#include "backtrace.h"
-#define INCLUDE_BACKTRACE
-#endif /* INTEL */
-
-#endif /* MACOSX */
-
-#ifdef LINUX
-#include <execinfo.h>
-#include <link.h>
-#define INCLUDE_BACKTRACE
-#endif
-
-#ifdef __sun
-
 #include "backtrace.h"
-#define INCLUDE_BACKTRACE
-
-#endif /* defined __sun */
 
-#if defined INCLUDE_BACKTRACE
 #define MAX_STACK_FRAMES 256
-#endif
 
 #include <osl/diagnose.h>
 #include <osl/signal.h>
@@ -304,24 +282,20 @@ namespace
 {
 void printStack(int sig)
 {
-#ifdef INCLUDE_BACKTRACE
     void *buffer[MAX_STACK_FRAMES];
     int size = backtrace( buffer, SAL_N_ELEMENTS(buffer) );
-#endif
 
     fprintf( stderr, "\n\nFatal exception: Signal %d\n", sig );
 
-#if defined( MACOSX ) && !defined( INCLUDE_BACKTRACE )
+#if ! HAVE_FEATURE_BACKTRACE && defined( MACOSX ) && !defined( INTEL )
     fprintf( stderr, "Please turn on Enable Crash Reporting and\nAutomatic 
Display of Crashlogs in the Console application\n" );
-#else
-#ifdef INCLUDE_BACKTRACE
+#endif
+
     if ( size > 0 )
     {
         fputs( "Stack:\n", stderr );
         backtrace_symbols_fd( buffer, size, fileno(stderr) );
     }
-#endif
-#endif
 }
 
 void callSystemHandler(int signal, siginfo_t * info, void * context)

Reply via email to