[Lldb-commits] [lldb] r231511 - Remove Host::Backtrace in favor of llvm::sys::PrintStackTrace()

Zachary Turner zturner at google.com
Fri Mar 6 12:45:43 PST 2015


Author: zturner
Date: Fri Mar  6 14:45:43 2015
New Revision: 231511

URL: http://llvm.org/viewvc/llvm-project?rev=231511&view=rev
Log:
Remove Host::Backtrace in favor of llvm::sys::PrintStackTrace()

This removes Host::Backtrace from the codebase, and changes all
call sites to use llvm::sys::PrintStackTrace().  This makes the
functionality available for all platforms, and even for platforms
which currently had a supported implementation of Host::Backtrace,
this patch should enable richer information in stack traces, such
as file and line number information, as well as giving it the
ability to unwind through inlined functions.

Modified:
    lldb/trunk/include/lldb/Host/Host.h
    lldb/trunk/source/Core/Log.cpp
    lldb/trunk/source/Core/Module.cpp
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/source/Host/freebsd/Host.cpp
    lldb/trunk/source/Host/linux/Host.cpp
    lldb/trunk/source/Host/macosx/Host.mm
    lldb/trunk/source/Symbol/ClangASTType.cpp
    lldb/trunk/source/Utility/LLDBAssert.cpp

Modified: lldb/trunk/include/lldb/Host/Host.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Host.h?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Host.h (original)
+++ lldb/trunk/include/lldb/Host/Host.h Fri Mar  6 14:45:43 2015
@@ -281,9 +281,6 @@ public:
     static bool
     OpenFileInExternalEditor (const FileSpec &file_spec, 
                               uint32_t line_no);
-
-    static void
-    Backtrace (Stream &strm, uint32_t max_frames);
     
     static size_t
     GetEnvironment (StringList &env);

Modified: lldb/trunk/source/Core/Log.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Log.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Core/Log.cpp (original)
+++ lldb/trunk/source/Core/Log.cpp Fri Mar  6 14:45:43 2015
@@ -29,6 +29,8 @@
 #include "lldb/Interpreter/Args.h"
 
 #include "llvm/ADT/SmallString.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Signals.h"
 using namespace lldb;
 using namespace lldb_private;
 
@@ -122,8 +124,13 @@ Log::PrintfWithFlagsVarArg (uint32_t fla
         header.PrintfVarArg (format, args);
         stream_sp->Printf("%s\n", header.GetData());
         
-        if (m_options.Test (LLDB_LOG_OPTION_BACKTRACE))
-            Host::Backtrace (*stream_sp, 1024);
+        if (m_options.Test(LLDB_LOG_OPTION_BACKTRACE)) 
+        {
+            std::string back_trace;
+            llvm::raw_string_ostream stream(back_trace);
+            llvm::sys::PrintStackTrace(stream);
+            stream_sp->PutCString(back_trace.c_str());
+        }
         stream_sp->Flush();
     }
 }

Modified: lldb/trunk/source/Core/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Module.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Core/Module.cpp (original)
+++ lldb/trunk/source/Core/Module.cpp Fri Mar  6 14:45:43 2015
@@ -40,6 +40,9 @@
 
 #include "Plugins/ObjectFile/JIT/ObjectFileJIT.h"
 
+#include "llvm/Support/raw_os_ostream.h"
+#include "llvm/Support/Signals.h"
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -1234,7 +1237,12 @@ Module::LogMessageVerboseBacktrace (Log
         log_message.PrintfVarArg (format, args);
         va_end (args);
         if (log->GetVerbose())
-            Host::Backtrace (log_message, 1024);
+        {
+            std::string back_trace;
+            llvm::raw_string_ostream stream(back_trace);
+            llvm::sys::PrintStackTrace(stream);
+            log_message.PutCString(back_trace.c_str());
+        }
         log->PutCString(log_message.GetString().c_str());
     }
 }

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Fri Mar  6 14:45:43 2015
@@ -397,12 +397,6 @@ Host::WillTerminate ()
 
 #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
 
-void
-Host::Backtrace (Stream &strm, uint32_t max_frames)
-{
-    // TODO: Is there a way to backtrace the current process on other systems?
-}
-
 size_t
 Host::GetEnvironment (StringList &env)
 {

Modified: lldb/trunk/source/Host/freebsd/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/freebsd/Host.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Host/freebsd/Host.cpp (original)
+++ lldb/trunk/source/Host/freebsd/Host.cpp Fri Mar  6 14:45:43 2015
@@ -50,35 +50,6 @@ extern "C" {
 using namespace lldb;
 using namespace lldb_private;
 
-void
-Host::Backtrace (Stream &strm, uint32_t max_frames)
-{
-    char backtrace_path[] = "/tmp/lldb-backtrace-tmp-XXXXXX";
-    int backtrace_fd = ::mkstemp (backtrace_path);
-    if (backtrace_fd != -1)
-    {
-        std::vector<void *> frame_buffer (max_frames, NULL);
-        int count = ::backtrace (&frame_buffer[0], frame_buffer.size());
-        ::backtrace_symbols_fd (&frame_buffer[0], count, backtrace_fd);
-
-        const off_t buffer_size = ::lseek(backtrace_fd, 0, SEEK_CUR);
-
-        if (::lseek(backtrace_fd, 0, SEEK_SET) == 0)
-        {
-            char *buffer = (char *)::malloc (buffer_size);
-            if (buffer)
-            {
-                ssize_t bytes_read = ::read (backtrace_fd, buffer, buffer_size);
-                if (bytes_read > 0)
-                    strm.Write(buffer, bytes_read);
-                ::free (buffer);
-            }
-        }
-        ::close (backtrace_fd);
-        ::unlink (backtrace_path);
-    }
-}
-
 size_t
 Host::GetEnvironment (StringList &env)
 {

Modified: lldb/trunk/source/Host/linux/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/linux/Host.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Host/linux/Host.cpp (original)
+++ lldb/trunk/source/Host/linux/Host.cpp Fri Mar  6 14:45:43 2015
@@ -377,28 +377,6 @@ Host::GetProcessInfo (lldb::pid_t pid, P
     return GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid);
 }
 
-void
-Host::Backtrace (Stream &strm, uint32_t max_frames)
-{
-#ifndef __ANDROID__
-    if (max_frames > 0)
-    {
-        std::vector<void *> frame_buffer (max_frames, NULL);
-        int num_frames = ::backtrace (&frame_buffer[0], frame_buffer.size());
-        char** strs = ::backtrace_symbols (&frame_buffer[0], num_frames);
-        if (strs)
-        {
-            // Start at 1 to skip the "Host::Backtrace" frame
-            for (int i = 1; i < num_frames; ++i)
-                strm.Printf("%s\n", strs[i]);
-            ::free (strs);
-        }
-    }
-#else
-    assert(false && "::backtrace() not supported on Android");
-#endif
-}
-
 size_t
 Host::GetEnvironment (StringList &env)
 {

Modified: lldb/trunk/source/Host/macosx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/Host.mm?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/Host.mm Fri Mar  6 14:45:43 2015
@@ -704,25 +704,6 @@ Host::OpenFileInExternalEditor (const Fi
 #endif // #if !defined(__arm__) && !defined(__arm64__) && !defined(__aarch64__)
 }
 
-
-void
-Host::Backtrace (Stream &strm, uint32_t max_frames)
-{
-    if (max_frames > 0)
-    {
-        std::vector<void *> frame_buffer (max_frames, NULL);
-        int num_frames = ::backtrace (&frame_buffer[0], frame_buffer.size());
-        char** strs = ::backtrace_symbols (&frame_buffer[0], num_frames);
-        if (strs)
-        {
-            // Start at 1 to skip the "Host::Backtrace" frame
-            for (int i = 1; i < num_frames; ++i)
-                strm.Printf("%s\n", strs[i]);
-            ::free (strs);
-        }
-    }
-}
-
 size_t
 Host::GetEnvironment (StringList &env)
 {

Modified: lldb/trunk/source/Symbol/ClangASTType.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTType.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/ClangASTType.cpp (original)
+++ lldb/trunk/source/Symbol/ClangASTType.cpp Fri Mar  6 14:45:43 2015
@@ -30,6 +30,8 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
 
+#include "llvm/Support/Format.h"
+#include "llvm/Support/Signals.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -2135,11 +2137,14 @@ ClangASTType::GetBitSize (ExecutionConte
                     if (!g_printed)
                     {
                         StreamString s;
-                        s.Printf("warning: trying to determine the size of type ");
                         DumpTypeDescription(&s);
-                        s.Printf("\n without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\nbacktrace:\n");
-                        Host::Backtrace(s, UINT32_MAX);
-                        printf("%s\n", s.GetData());
+
+                        llvm::outs() << "warning: trying to determine the size of type ";
+                        llvm::outs() << s.GetString() << "\n";
+                        llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
+                        llvm::outs() << "backtrace:\n";
+                        llvm::sys::PrintStackTrace(llvm::outs());
+                        llvm::outs() << "\n";
                         g_printed = true;
                     }
                 }
@@ -2166,6 +2171,7 @@ ClangASTType::GetByteSize (ExecutionCont
     return (GetBitSize (exe_scope) + 7) / 8;
 }
 
+
 size_t
 ClangASTType::GetTypeBitAlign () const
 {

Modified: lldb/trunk/source/Utility/LLDBAssert.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/LLDBAssert.cpp?rev=231511&r1=231510&r2=231511&view=diff
==============================================================================
--- lldb/trunk/source/Utility/LLDBAssert.cpp (original)
+++ lldb/trunk/source/Utility/LLDBAssert.cpp Fri Mar  6 14:45:43 2015
@@ -8,9 +8,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Utility/LLDBAssert.h"
-#include "lldb/Core/StreamString.h"
-#include "lldb/Host/Host.h"
 
+#include "llvm/Support/Format.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Signals.h"
+
+using namespace llvm;
 using namespace lldb_private;
 
 void
@@ -24,15 +27,10 @@ lldb_private::lldb_assert (int expressio
         ;
     else
     {
-        StreamString stream;
-        stream.Printf("Assertion failed: (%s), function %s, file %s, line %u\n",
-                      expr_text,
-                      func,
-                      file,
-                      line);
-        stream.Printf("backtrace leading to the failure:\n");
-        Host::Backtrace(stream, 1000);
-        stream.Printf("please file a bug report against lldb reporting this failure log, and as many details as possible\n");
-        printf("%s\n", stream.GetData());
+        errs() << format("Assertion failed: (%s), function %s, file %s, line %u\n",
+                   expr_text, func, file, line);
+        errs() << "backtrace leading to the failure:\n";
+        llvm::sys::PrintStackTrace(errs());
+        errs() << "please file a bug report against lldb reporting this failure log, and as many details as possible\n";
     }
 }





More information about the lldb-commits mailing list