From bf778e29890b4288ef92640a19b41a3b25e7c2e2 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Thu, 21 Nov 2019 19:48:13 +0100 Subject: [PATCH] Add support for BSD in libbacktrace. https://gcc.gnu.org/ml/gcc-patches/2018-03/msg00068.html --- libbacktrace/fileline.cpp | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/libbacktrace/fileline.cpp b/libbacktrace/fileline.cpp index baabbe2a..bca2c47b 100644 --- a/libbacktrace/fileline.cpp +++ b/libbacktrace/fileline.cpp @@ -34,16 +34,49 @@ POSSIBILITY OF SUCH DAMAGE. */ #include #include +#include #include #include #include #include +#ifdef BSD +# include +# include +#endif + #include "backtrace.hpp" #include "internal.hpp" -#ifndef HAVE_GETEXECNAME -#define getexecname() NULL +#ifdef BSD +# if !defined HAVE_GETEXECNAME && defined KERN_PROC_PATHNAME +# define HAVE_GETEXECNAME +static char execname[PATH_MAX + 1]; +static const char * +getexecname(void) +{ + size_t path_len = sizeof(execname); + int mib[] = { + CTL_KERN, +#if defined(__NetBSD__) + KERN_PROC_ARGS, + -1, + KERN_PROC_PATHNAME, +#else + KERN_PROC, + KERN_PROC_PATHNAME, + -1, +#endif + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + int rc = sysctl(mib, miblen, execname, &path_len, NULL, 0); + return rc ? NULL : execname; +} +# endif +#else +# ifndef HAVE_GETEXECNAME +# define getexecname() NULL +# endif #endif namespace tracy