From 1016acfe5cd1f0443fce214c5cdb997da65b8ed6 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Thu, 24 Oct 2024 13:10:11 -0700 Subject: [PATCH] Use getprogname()/program_invocation_name as fallback when no g_argv0 --- Util/BacktraceException.hpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Util/BacktraceException.hpp b/Util/BacktraceException.hpp index 2a6cd38dd..aa269c04a 100644 --- a/Util/BacktraceException.hpp +++ b/Util/BacktraceException.hpp @@ -10,6 +10,10 @@ #include #include +#ifdef __FreeBSD__ +#include +#endif + extern std::string g_argv0; namespace Util { @@ -77,13 +81,28 @@ class BacktraceException : public T { return oss.str(); } + const char* progname() const { + // This variable is set by the CLBOSS mainline. + if (g_argv0 != "unknown") + return g_argv0.c_str(); + + // When libclboss is linked with the unit tests the + // g_argv0 variable is not set and we need to use + // built-in versions +#ifdef __FreeBSD__ + return getprogname(); +#else + return program_invocation_name; +#endif + } + // Unfortunately there is no simple way to get a high quality // backtrace using in-process libraries. Instead for now we // popen an addr2line process and use it's output. std::string addr2line(void* addr) const { char cmd[512]; snprintf(cmd, sizeof(cmd), - "addr2line -C -f -p -e %s %p", g_argv0.c_str(), addr); + "addr2line -C -f -p -e %s %p", progname(), addr); std::array buffer; std::string result;