diff mbox series

[v2,02/18] build: Only enable backtrace(3) in maintainer mode

Message ID 20240202225405.993792-2-denkenz@gmail.com (mailing list archive)
State Accepted
Commit 05eabee2524d7711c6fa413fdfc25a47728d4047
Headers show
Series [v2,01/18] umlrunner: Also mount /var/lib as tmpfs | expand

Commit Message

Denis Kenzior Feb. 2, 2024, 10:53 p.m. UTC
Using backtrace() is of no use when building with PIE (which most
distro compilers do by default) and prevents catching the coredump
for later retracing, which is needed since distros usually don't
install debug symbols by default either.

This patch thus only enables backtrace() when --enable-maintainer-mode
is passed and also tries to explicitly disable PIE.

This commit is based on the following commit from 'iwd':
b6910e121082 ("build: only enable backtrace(3) in maintainer mode")
---
 configure.ac | 11 +++++++++++
 src/log.c    |  8 ++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index c18cfa3a696f..551f863538ed 100644
--- a/configure.ac
+++ b/configure.ac
@@ -110,6 +110,17 @@  AC_ARG_ENABLE(ubsan, AS_HELP_STRING([--enable-ubsan],
 AC_CHECK_FUNCS(explicit_bzero)
 AC_CHECK_FUNCS(rawmemchr)
 
+# In maintainer mode: try to build with application backtrace and disable PIE.
+if (test "${USE_MAINTAINER_MODE}" = yes); then
+	AC_SEARCH_LIBS([backtrace], [execinfo],
+		[
+			AC_DEFINE([HAVE_BACKTRACE], [1],
+				[Define to 1 if you have backtrace(3).])
+			CFLAGS="$CFLAGS -fno-PIE"
+			LDFLAGS="$LDFLAGS -no-pie"
+		])
+fi
+
 AC_CHECK_FUNC(signalfd, dummy=yes,
 			AC_MSG_ERROR(signalfd support is required))
 
diff --git a/src/log.c b/src/log.c
index b33a7d262474..a8b4ef10eaae 100644
--- a/src/log.c
+++ b/src/log.c
@@ -30,7 +30,7 @@ 
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
-#ifdef __GLIBC__
+#ifdef HAVE_BACKTRACE
 #include <execinfo.h>
 #endif
 #include <dlfcn.h>
@@ -115,7 +115,7 @@  void ofono_debug(const char *format, ...)
 	va_end(ap);
 }
 
-#ifdef __GLIBC__
+#ifdef HAVE_BACKTRACE
 static void print_backtrace(unsigned int offset)
 {
 	void *frames[99];
@@ -303,7 +303,7 @@  int __ofono_log_init(const char *program, const char *debug,
 	if (detach == FALSE)
 		option |= LOG_PERROR;
 
-#ifdef __GLIBC__
+#ifdef HAVE_BACKTRACE
 	signal_setup(signal_handler);
 #endif
 
@@ -320,7 +320,7 @@  void __ofono_log_cleanup(void)
 
 	closelog();
 
-#ifdef __GLIBC__
+#ifdef HAVE_BACKTRACE
 	signal_setup(SIG_DFL);
 #endif