diff --git a/config.lib b/config.lib index bf8231d376..97b4f523b9 100644 --- a/config.lib +++ b/config.lib @@ -97,6 +97,7 @@ set_default() { with_libbfd="1" with_bfd_extra_debug="1" with_self_gdb_debug="1" + with_self_lldb_debug="1" save_params_array=" build @@ -176,6 +177,7 @@ set_default() { with_libbfd with_bfd_extra_debug with_self_gdb_debug + with_self_lldb_debug CC CXX CFLAGS CXXFLAGS LDFLAGS CFLAGS_BUILD CXXFLAGS_BUILD LDFLAGS_BUILD PKG_CONFIG_PATH PKG_CONFIG_LIBDIR" } @@ -479,6 +481,10 @@ detect_params() { --with-self-gdb-debug) with_self_gdb_debug="1";; --with-self-gdb-debug=*) with_self_gdb_debug="$optarg";; + --without-self-lldb-debug) with_self_lldb_debug="0";; + --with-self-lldb-debug) with_self_lldb_debug="1";; + --with-self-lldb-debug=*) with_self_lldb_debug="$optarg";; + CC=* | --CC=*) CC="$optarg";; CXX=* | --CXX=*) CXX="$optarg";; CFLAGS=* | --CFLAGS=*) CFLAGS="$optarg";; @@ -1888,6 +1894,20 @@ EOL CFLAGS="$OSX_SYSROOT $CFLAGS" LDFLAGS="$OSX_LD_SYSROOT $LDFLAGS" fi + + if [ "$with_self_lldb_debug" = "0" ]; then + log 1 "using dbg lldb... no" + CFLAGS="$CFLAGS -DWITHOUT_DBG_LLDB" + else + log 1 "using dbg lldb... yes" + if [ $enable_debug -lt 1 ]; then + if [ -n "`"$cc_host" --version | head -n1 | grep "clang" 2>/dev/null`" ]; then + CFLAGS="$CFLAGS -gline-tables-only" + else + CFLAGS="$CFLAGS -g1" + fi + fi + fi fi if [ "$os" = "HAIKU" ]; then @@ -3826,6 +3846,7 @@ showhelp() { echo " --without-libbfd disable libbfd support, used for improved crash logs (MinGW and Unix/glibc only)" echo " --without-bfd-extra-debug disable extra debugging information when using libbfd (MinGW and Unix/glibc only)" echo " --without-self-gdb-debug disable improved crash logs using gdb (Linux only)" + echo " --without-self-lldb-debug disable improved crash logs using lldb (OSX only)" echo "" echo "Some influential environment variables:" echo " CC C compiler command" diff --git a/src/os/macosx/crashlog_osx.cpp b/src/os/macosx/crashlog_osx.cpp index 3300b818d7..14d5e83c3c 100644 --- a/src/os/macosx/crashlog_osx.cpp +++ b/src/os/macosx/crashlog_osx.cpp @@ -14,6 +14,7 @@ #include "../../string_func.h" #include "../../gamelog.h" #include "../../saveload/saveload.h" +#include "../../thread.h" #include "macos.h" #include @@ -41,6 +42,62 @@ #define MAX_STACK_FRAMES 64 +#if !defined(WITHOUT_DBG_LLDB) +static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, const char *last) +{ + int pipefd[2]; + if (pipe(pipefd) == -1) return false; + + int pid = fork(); + if (pid < 0) return false; + + if (pid == 0) { + /* child */ + + close(pipefd[0]); /* Close unused read end */ + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + int null_fd = open("/dev/null", O_RDWR); + if (null_fd != -1) { + dup2(null_fd, STDERR_FILENO); + dup2(null_fd, STDIN_FILENO); + } + + execvp(file, args); + exit(42); + } + + /* parent */ + + close(pipefd[1]); /* Close unused write end */ + + while (buffer < last) { + ssize_t res = read(pipefd[0], buffer, last - buffer); + if (res < 0) { + if (errno == EINTR) continue; + break; + } else if (res == 0) { + break; + } else { + buffer += res; + } + } + buffer += seprintf(buffer, last, "\n"); + + close(pipefd[0]); /* close read end */ + + int status; + int wait_ret = waitpid(pid, &status, 0); + if (wait_ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { + /* command did not appear to run successfully */ + return false; + } else { + /* command executed successfully */ + return true; + } +} +#endif /* !WITHOUT_DBG_LLDB */ + /** * OSX implementation for the crash logger. */ @@ -150,6 +207,57 @@ class CrashLogOSX : public CrashLog { return buffer + seprintf(buffer, last, "\n"); } + /** + * Get a stack backtrace of the current thread's stack and other info using the gdb debugger, if available. + * + * Using LLDB is useful as it knows about inlined functions and locals, and generally can + * do a more thorough job than in LogStacktrace. + * This is done in addition to LogStacktrace as lldb cannot be assumed to be present + * and there is some potentially useful information in the output from LogStacktrace + * which is not in lldb's output. + */ + char *LogLldbInfo(char *buffer, const char *last) const + { + +#if !defined(WITHOUT_DBG_LLDB) + pid_t pid = getpid(); + + char *buffer_orig = buffer; + buffer += seprintf(buffer, last, "LLDB info:\n"); + + char pid_buffer[16]; + + seprintf(pid_buffer, lastof(pid_buffer), "%d", pid); + + std::vector args; + args.push_back("lldb"); + args.push_back("-x"); + args.push_back("-p"); + args.push_back(pid_buffer); + args.push_back("--batch"); + + args.push_back("-o"); + args.push_back(IsNonMainThread() ? "bt all" : "bt"); + + args.push_back(nullptr); + if (!ExecReadStdout("lldb", const_cast(&(args[0])), buffer, last)) { + buffer = buffer_orig; + } +#endif /* !WITHOUT_DBG_LLDB */ + + return buffer; + } + + /** + * Log LLDB information if available + */ + char *LogRegisters(char *buffer, const char *last) const override + { + buffer = LogLldbInfo(buffer, last); + + return buffer; + } + public: /** * A crash log is always generated by signal.