Adopt PrettyStackTrace in LLDB
authorSean Callanan <scallanan@apple.com>
Wed, 14 Dec 2016 21:31:31 +0000 (21:31 +0000)
committerSean Callanan <scallanan@apple.com>
Wed, 14 Dec 2016 21:31:31 +0000 (21:31 +0000)
LLDB needs some minor changes to adopt PrettyStackTrace after https://reviews.llvm.org/D27683.
We remove our own SetCrashDescription() function and use LLVM-provided RAII objects instead.
We also make sure LLDB doesn't define __crashtracer_info__ which would collide with LLVM's definition.

Differential Revision: https://reviews.llvm.org/D27735

llvm-svn: 289711

lldb/include/lldb/Host/Host.h
lldb/scripts/Xcode/build-llvm.py
lldb/source/API/SBFrame.cpp
lldb/source/API/SBTarget.cpp
lldb/source/Host/common/Host.cpp
lldb/source/Host/macosx/Host.mm
lldb/source/Initialization/SystemInitializerCommon.cpp
lldb/source/Interpreter/CommandInterpreter.cpp
lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

index 0666b67..64c0dd6 100644 (file)
@@ -183,23 +183,6 @@ public:
   //------------------------------------------------------------------
   static bool ResolveExecutableInBundle(FileSpec &file);
 
-  //------------------------------------------------------------------
-  /// Set a string that can be displayed if host application crashes.
-  ///
-  /// Some operating systems have the ability to print a description
-  /// for shared libraries when a program crashes. If the host OS
-  /// supports such a mechanism, it should be implemented to help
-  /// with crash triage.
-  ///
-  /// @param[in] format
-  ///     A printf format that will be used to form a new crash
-  ///     description string.
-  //------------------------------------------------------------------
-  static void SetCrashDescriptionWithFormat(const char *format, ...)
-      __attribute__((format(printf, 1, 2)));
-
-  static void SetCrashDescription(const char *description);
-
   static uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
                                 ProcessInstanceInfoList &proc_infos);
 
index ff474c2..792f925 100755 (executable)
@@ -346,7 +346,8 @@ def cmake_flags():
                     "-DCMAKE_C_FLAGS={}".format(get_c_flags()),
                     "-DCMAKE_CXX_FLAGS={}".format(get_cxx_flags()),
                     "-DCMAKE_EXE_LINKER_FLAGS={}".format(get_exe_linker_flags()),
-                    "-DCMAKE_SHARED_LINKER_FLAGS={}".format(get_shared_linker_flags())]
+                    "-DCMAKE_SHARED_LINKER_FLAGS={}".format(get_shared_linker_flags()),
+                    "-DHAVE_CRASHREPORTER_INFO=1"]
     deployment_target = get_deployment_target()
     if deployment_target:
         cmake_flags.append(
index 1845b12..5500c33 100644 (file)
@@ -52,6 +52,8 @@
 #include "lldb/API/SBValue.h"
 #include "lldb/API/SBVariablesOptions.h"
 
+#include "llvm/Support/PrettyStackTrace.h"
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -1288,10 +1290,11 @@ lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
     if (stop_locker.TryLock(&process->GetRunLock())) {
       frame = exe_ctx.GetFramePtr();
       if (frame) {
+        std::unique_ptr<llvm::PrettyStackTraceFormat> PST;
         if (target->GetDisplayExpressionsInCrashlogs()) {
           StreamString frame_description;
           frame->DumpUsingSettingsFormat(&frame_description);
-          Host::SetCrashDescriptionWithFormat(
+          PST = llvm::make_unique<llvm::PrettyStackTraceFormat>(
               "SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value "
               "= %u) %s",
               expr, options.GetFetchDynamicValue(),
@@ -1301,9 +1304,6 @@ lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
         exe_results = target->EvaluateExpression(expr, frame, expr_value_sp,
                                                  options.ref());
         expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
-
-        if (target->GetDisplayExpressionsInCrashlogs())
-          Host::SetCrashDescription(nullptr);
       } else {
         if (log)
           log->Printf("SBFrame::EvaluateExpression () => error: could not "
index a620f6f..211f77a 100644 (file)
@@ -64,6 +64,7 @@
 
 #include "../source/Commands/CommandObjectBreakpoint.h"
 #include "lldb/Interpreter/CommandReturnObject.h"
+#include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Regex.h"
 
 using namespace lldb;
@@ -2129,7 +2130,7 @@ lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
       StreamString frame_description;
       if (frame)
         frame->DumpUsingSettingsFormat(&frame_description);
-      Host::SetCrashDescriptionWithFormat(
+      llvm::PrettyStackTraceFormat PST(
           "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
           "%u) %s",
           expr, options.GetFetchDynamicValue(),
@@ -2139,9 +2140,6 @@ lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
           target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
 
       expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
-#ifdef LLDB_CONFIGURATION_DEBUG
-      Host::SetCrashDescription(NULL);
-#endif
     } else {
       if (log)
         log->Printf("SBTarget::EvaluateExpression () => error: could not "
index f0d7e2e..6d0ad01 100644 (file)
@@ -999,10 +999,6 @@ bool Host::OpenFileInExternalEditor(const FileSpec &file_spec,
   return false;
 }
 
-void Host::SetCrashDescriptionWithFormat(const char *format, ...) {}
-
-void Host::SetCrashDescription(const char *description) {}
-
 #endif
 
 const UnixSignalsSP &Host::GetUnixSignals() {
index 6c0a44b..99bc7ec 100644 (file)
@@ -537,47 +537,6 @@ LaunchInNewTerminalWithAppleScript(const char *exe_path,
 
 #endif // #if !defined(__arm__) && !defined(__arm64__) && !defined(__aarch64__)
 
-// On MacOSX CrashReporter will display a string for each shared library if
-// the shared library has an exported symbol named "__crashreporter_info__".
-
-static std::mutex &GetCrashReporterMutex() {
-  static std::mutex g_mutex;
-  return g_mutex;
-}
-
-extern "C" {
-const char *__crashreporter_info__ = NULL;
-}
-
-asm(".desc ___crashreporter_info__, 0x10");
-
-void Host::SetCrashDescriptionWithFormat(const char *format, ...) {
-  static StreamString g_crash_description;
-  std::lock_guard<std::mutex> guard(GetCrashReporterMutex());
-
-  if (format) {
-    va_list args;
-    va_start(args, format);
-    g_crash_description.GetString() = llvm::StringRef("");
-    g_crash_description.PrintfVarArg(format, args);
-    va_end(args);
-    __crashreporter_info__ = g_crash_description.GetData();
-  } else {
-    __crashreporter_info__ = NULL;
-  }
-}
-
-void Host::SetCrashDescription(const char *cstr) {
-  std::lock_guard<std::mutex> guard(GetCrashReporterMutex());
-  static std::string g_crash_description;
-  if (cstr) {
-    g_crash_description.assign(cstr);
-    __crashreporter_info__ = g_crash_description.c_str();
-  } else {
-    __crashreporter_info__ = NULL;
-  }
-}
-
 bool Host::OpenFileInExternalEditor(const FileSpec &file_spec,
                                     uint32_t line_no) {
 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
index 5bf4cea..1139955 100644 (file)
 #include "lldb/Host/windows/windows.h"
 #endif
 
+#include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/TargetSelect.h"
 
 #include <string>
 
 using namespace lldb_private;
 
-static void fatal_error_handler(void *user_data, const std::string &reason,
-                                bool gen_crash_diag) {
-  Host::SetCrashDescription(reason.c_str());
-  ::abort();
-}
-
 SystemInitializerCommon::SystemInitializerCommon() {}
 
 SystemInitializerCommon::~SystemInitializerCommon() {}
@@ -74,12 +69,11 @@ void SystemInitializerCommon::Initialize() {
   }
 #endif
 
+  llvm::EnablePrettyStackTrace();
   Log::Initialize();
   HostInfo::Initialize();
   Timer scoped_timer(LLVM_PRETTY_FUNCTION, LLVM_PRETTY_FUNCTION);
 
-  llvm::install_fatal_error_handler(fatal_error_handler, 0);
-
   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
 
   // Initialize plug-ins
index 305d6a9..a805572 100644 (file)
@@ -72,6 +72,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/PrettyStackTrace.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -1526,13 +1527,8 @@ bool CommandInterpreter::HandleCommand(const char *command_line,
   std::string original_command_string(command_line);
 
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMANDS));
-  Host::SetCrashDescriptionWithFormat("HandleCommand(command = \"%s\")",
-                                      command_line);
-
-  // Make a scoped cleanup object that will clear the crash description string
-  // on exit of this function.
-  lldb_utility::CleanUp<const char *> crash_description_cleanup(
-      nullptr, Host::SetCrashDescription);
+  llvm::PrettyStackTraceFormat PST("HandleCommand(command = \"%s\")",
+                                   command_line);
 
   if (log)
     log->Printf("Processing command: %s", command_line);
index c5a62d5..5289808 100644 (file)
@@ -1473,8 +1473,7 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
                         }
 
                         if (add_method) {
-                          // REMOVE THE CRASH DESCRIPTION BELOW
-                          Host::SetCrashDescriptionWithFormat(
+                          llvm::PrettyStackTraceFormat PST(
                               "SymbolFileDWARF::ParseType() is adding a method "
                               "%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
                               type_name_cstr,
@@ -1492,12 +1491,12 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
                           if (accessibility == eAccessNone)
                             accessibility = eAccessPublic;
 
-                          clang::CXXMethodDecl *cxx_method_decl;
-                          cxx_method_decl = m_ast.AddMethodToCXXRecordType(
-                              class_opaque_type.GetOpaqueQualType(),
-                              type_name_cstr, clang_type, accessibility,
-                              is_virtual, is_static, is_inline, is_explicit,
-                              is_attr_used, is_artificial);
+                          clang::CXXMethodDecl *cxx_method_decl =
+                              m_ast.AddMethodToCXXRecordType(
+                                  class_opaque_type.GetOpaqueQualType(),
+                                  type_name_cstr, clang_type, accessibility,
+                                  is_virtual, is_static, is_inline, is_explicit,
+                                  is_attr_used, is_artificial);
 
                           type_handled = cxx_method_decl != NULL;
 
@@ -1507,8 +1506,6 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
                                     cxx_method_decl),
                                 die);
 
-                            Host::SetCrashDescription(NULL);
-
                             ClangASTMetadata metadata;
                             metadata.SetUserID(die.GetID());