[Lldb-commits] [lldb] r114188 - in /lldb/trunk: include/lldb/API/SBStream.h include/lldb/API/SBStringList.h include/lldb/Core/StreamFile.h lldb.xcodeproj/project.pbxproj source/API/SBStream.cpp source/Core/StreamFile.cpp

Greg Clayton gclayton at apple.com
Fri Sep 17 10:42:16 PDT 2010


Author: gclayton
Date: Fri Sep 17 12:42:16 2010
New Revision: 114188

URL: http://llvm.org/viewvc/llvm-project?rev=114188&view=rev
Log:
We now have SBStream that mirrors the generic stream classes we 
use inside lldb (lldb_private::StreamFile, and lldb_private::StreamString).


Added:
    lldb/trunk/include/lldb/API/SBStream.h
    lldb/trunk/source/API/SBStream.cpp
Modified:
    lldb/trunk/include/lldb/API/SBStringList.h
    lldb/trunk/include/lldb/Core/StreamFile.h
    lldb/trunk/lldb.xcodeproj/project.pbxproj
    lldb/trunk/source/Core/StreamFile.cpp

Added: lldb/trunk/include/lldb/API/SBStream.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBStream.h?rev=114188&view=auto
==============================================================================
--- lldb/trunk/include/lldb/API/SBStream.h (added)
+++ lldb/trunk/include/lldb/API/SBStream.h Fri Sep 17 12:42:16 2010
@@ -0,0 +1,81 @@
+//===-- SBStream.h ----------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SBStream_h_
+#define LLDB_SBStream_h_
+
+#include "lldb/API/SBDefines.h"
+
+namespace lldb {
+
+class SBStream
+{
+public:
+
+    SBStream ();
+
+    ~SBStream ();
+
+    bool
+    IsValid() const;
+
+    // If this stream is not redirected to a file, it will maintain a local
+    // cache for the stream data which can be accessed using this accessor.
+    const char *
+    GetData ();
+
+    // If this stream is not redirected to a file, it will maintain a local
+    // cache for the stream output whose length can be accessed using this 
+    // accessor.
+    size_t
+    GetSize();
+
+    void
+    Printf (const char *format, ...);
+
+    void
+    RedirectToFile (const char *path, bool append);
+
+    void
+    RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership);
+
+    void
+    RedirectToFileDescriptor (int fd, bool transfer_fh_ownership);
+
+    // If the stream is redirected to a file, forget about the file and if
+    // ownership of the file was transfered to this object, close the file.
+    // If the stream is backed by a local cache, clear this cache.
+    void
+    Clear ();
+
+protected:
+
+#ifndef SWIG
+
+    lldb_private::Stream *
+    operator->();
+
+    lldb_private::Stream *
+    get();
+
+    lldb_private::Stream &
+    ref();
+
+#endif
+
+private:
+
+    DISALLOW_COPY_AND_ASSIGN (SBStream);
+    std::auto_ptr<lldb_private::Stream> m_opaque_ap;
+    bool m_is_file;
+};
+
+} // namespace lldb
+
+#endif // LLDB_SBStream_h_

Modified: lldb/trunk/include/lldb/API/SBStringList.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBStringList.h?rev=114188&r1=114187&r2=114188&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBStringList.h (original)
+++ lldb/trunk/include/lldb/API/SBStringList.h Fri Sep 17 12:42:16 2010
@@ -47,6 +47,8 @@
     void
     Clear ();
 
+protected:
+
 #ifndef SWIG
 
     const lldb_private::StringList *

Modified: lldb/trunk/include/lldb/Core/StreamFile.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/StreamFile.h?rev=114188&r1=114187&r2=114188&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/StreamFile.h (original)
+++ lldb/trunk/include/lldb/Core/StreamFile.h Fri Sep 17 12:42:16 2010
@@ -32,7 +32,7 @@
 
     StreamFile (uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order, FILE *f);
 
-    StreamFile (FILE *f);
+    StreamFile (FILE *f, bool tranfer_ownership = false);
 
     StreamFile (uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order, const char *path, const char *permissions = "w");
 

Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=114188&r1=114187&r2=114188&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Fri Sep 17 12:42:16 2010
@@ -69,6 +69,8 @@
 		268F9D55123AA16600B91E9B /* SBSymbolContextList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 268F9D54123AA16600B91E9B /* SBSymbolContextList.cpp */; };
 		26B42B1F1187A92B0079C8C8 /* lldb-include.h in Headers */ = {isa = PBXBuildFile; fileRef = 26B42B1E1187A92B0079C8C8 /* lldb-include.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		26B42C4D1187ABA50079C8C8 /* LLDB.h in Headers */ = {isa = PBXBuildFile; fileRef = 26B42C4C1187ABA50079C8C8 /* LLDB.h */; settings = {ATTRIBUTES = (Public, ); }; };
+		26C72C94124322890068DC16 /* SBStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C72C93124322890068DC16 /* SBStream.h */; };
+		26C72C961243229A0068DC16 /* SBStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26C72C951243229A0068DC16 /* SBStream.cpp */; };
 		26D27C9F11ED3A4E0024D721 /* ELFHeader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26D27C9D11ED3A4E0024D721 /* ELFHeader.cpp */; };
 		26D27CA011ED3A4E0024D721 /* ELFHeader.h in Headers */ = {isa = PBXBuildFile; fileRef = 26D27C9E11ED3A4E0024D721 /* ELFHeader.h */; };
 		26D5B06511B07550009A862E /* StoppointCallbackContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26BC7E0910F1B83100F91463 /* StoppointCallbackContext.cpp */; };
@@ -849,6 +851,8 @@
 		26BC7F3E10F1B90C00F91463 /* ThreadList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ThreadList.cpp; path = source/Target/ThreadList.cpp; sourceTree = "<group>"; };
 		26BC7F3F10F1B90C00F91463 /* ThreadPlan.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ThreadPlan.cpp; path = source/Target/ThreadPlan.cpp; sourceTree = "<group>"; };
 		26BC7F4C10F1BC1A00F91463 /* ObjectFile.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ObjectFile.cpp; path = source/Symbol/ObjectFile.cpp; sourceTree = "<group>"; };
+		26C72C93124322890068DC16 /* SBStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBStream.h; path = include/lldb/API/SBStream.h; sourceTree = "<group>"; };
+		26C72C951243229A0068DC16 /* SBStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBStream.cpp; path = source/API/SBStream.cpp; sourceTree = "<group>"; };
 		26C81CA411335651004BDC5A /* UUID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UUID.h; path = include/lldb/Core/UUID.h; sourceTree = "<group>"; };
 		26C81CA511335651004BDC5A /* UUID.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UUID.cpp; path = source/Core/UUID.cpp; sourceTree = "<group>"; };
 		26C9DF03113C5B93006B0F94 /* lldb */ = {isa = PBXFileReference; lastKnownFileType = folder; name = lldb; path = include/lldb; sourceTree = "<group>"; };
@@ -1481,6 +1485,8 @@
 				9A9831031125FC5800A56CB0 /* SBProcess.cpp */,
 				9A9831061125FC5800A56CB0 /* SBSourceManager.h */,
 				9A9831051125FC5800A56CB0 /* SBSourceManager.cpp */,
+				26C72C93124322890068DC16 /* SBStream.h */,
+				26C72C951243229A0068DC16 /* SBStream.cpp */,
 				9A357670116E7B5200E8ED2F /* SBStringList.h */,
 				9A357672116E7B6400E8ED2F /* SBStringList.cpp */,
 				26DE205A11618FF600A093E2 /* SBSymbol.h */,
@@ -2278,6 +2284,7 @@
 				268F9D53123AA15200B91E9B /* SBSymbolContextList.h in Headers */,
 				2618D7901240115500F2B8FE /* SectionLoadList.h in Headers */,
 				2618D959124056C700F2B8FE /* NameToDIE.h in Headers */,
+				26C72C94124322890068DC16 /* SBStream.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -2741,6 +2748,7 @@
 				268F9D55123AA16600B91E9B /* SBSymbolContextList.cpp in Sources */,
 				2618D7921240116900F2B8FE /* SectionLoadList.cpp in Sources */,
 				2618D9EB12406FE600F2B8FE /* NameToDIE.cpp in Sources */,
+				26C72C961243229A0068DC16 /* SBStream.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Added: lldb/trunk/source/API/SBStream.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBStream.cpp?rev=114188&view=auto
==============================================================================
--- lldb/trunk/source/API/SBStream.cpp (added)
+++ lldb/trunk/source/API/SBStream.cpp Fri Sep 17 12:42:16 2010
@@ -0,0 +1,178 @@
+//===-- SBStream.cpp ----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/API/SBStream.h"
+
+#include "lldb/Core/Stream.h"
+#include "lldb/Core/StreamFile.h"
+#include "lldb/Core/StreamString.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+SBStream::SBStream () :
+    m_opaque_ap (),
+    m_is_file (false)
+{
+}
+
+SBStream::~SBStream ()
+{
+}
+
+bool
+SBStream::IsValid() const
+{
+    return (m_opaque_ap.get() != NULL);
+}
+
+// If this stream is not redirected to a file, it will maintain a local
+// cache for the stream data which can be accessed using this accessor.
+const char *
+SBStream::GetData ()
+{
+    if (m_is_file || m_opaque_ap.get() == NULL)
+        return NULL;
+    
+    return static_cast<StreamString *>(m_opaque_ap.get())->GetData();
+}
+
+// If this stream is not redirected to a file, it will maintain a local
+// cache for the stream output whose length can be accessed using this 
+// accessor.
+size_t
+SBStream::GetSize()
+{
+    if (m_is_file || m_opaque_ap.get() == NULL)
+        return NULL;
+    
+    return static_cast<StreamString *>(m_opaque_ap.get())->GetSize();
+}
+
+void
+SBStream::Printf (const char *format, ...)
+{
+    va_list args;
+    va_start (args, format);
+    ref().PrintfVarArg (format, args);
+    va_end (args);
+}
+
+void
+SBStream::RedirectToFile (const char *path, bool append)
+{
+    std::string local_data;
+    if (m_opaque_ap.get())
+    {
+        // See if we have any locally backed data. If so, copy it so we can then
+        // redirect it to the file so we don't lose the data
+        if (!m_is_file)
+            local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
+    }
+    m_opaque_ap.reset (new StreamFile (path, append ? "a" : "w"));
+
+    if (m_opaque_ap.get())
+    {
+        m_is_file = true;
+
+        // If we had any data locally in our StreamString, then pass that along to 
+        // the to new file we are redirecting to.
+        if (!local_data.empty())
+            m_opaque_ap->Write (&local_data[0], local_data.size());
+    }
+    else
+        m_is_file = false;
+}
+
+void
+SBStream::RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership)
+{
+    std::string local_data;
+    if (m_opaque_ap.get())
+    {
+        // See if we have any locally backed data. If so, copy it so we can then
+        // redirect it to the file so we don't lose the data
+        if (!m_is_file)
+            local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
+    }
+    m_opaque_ap.reset (new StreamFile (fh, transfer_fh_ownership));
+
+    if (m_opaque_ap.get())
+    {
+        m_is_file = true;
+
+        // If we had any data locally in our StreamString, then pass that along to 
+        // the to new file we are redirecting to.
+        if (!local_data.empty())
+            m_opaque_ap->Write (&local_data[0], local_data.size());
+    }
+    else
+        m_is_file = false;
+}
+
+void
+SBStream::RedirectToFileDescriptor (int fd, bool transfer_fh_ownership)
+{
+    std::string local_data;
+    if (m_opaque_ap.get())
+    {
+        // See if we have any locally backed data. If so, copy it so we can then
+        // redirect it to the file so we don't lose the data
+        if (!m_is_file)
+            local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
+    }
+
+    m_opaque_ap.reset (new StreamFile (::fdopen (fd, "w"), transfer_fh_ownership));
+    if (m_opaque_ap.get())
+    {
+        m_is_file = true;
+
+        // If we had any data locally in our StreamString, then pass that along to 
+        // the to new file we are redirecting to.
+        if (!local_data.empty())
+            m_opaque_ap->Write (&local_data[0], local_data.size());
+    }
+    else
+        m_is_file = false;
+
+}
+
+lldb_private::Stream *
+SBStream::operator->()
+{
+    return m_opaque_ap.get();
+}
+
+lldb_private::Stream *
+SBStream::get()
+{
+    return m_opaque_ap.get();
+}
+
+lldb_private::Stream &
+SBStream::ref()
+{
+    if (m_opaque_ap.get() == NULL)
+        m_opaque_ap.reset (new StreamString());
+    return *m_opaque_ap.get();
+}
+
+void
+SBStream::Clear ()
+{
+    if (m_opaque_ap.get())
+    {
+        // See if we have any locally backed data. If so, copy it so we can then
+        // redirect it to the file so we don't lose the data
+        if (m_is_file)
+            m_opaque_ap.reset();
+        else
+            static_cast<StreamString *>(m_opaque_ap.get())->GetString().clear();
+    }
+}

Modified: lldb/trunk/source/Core/StreamFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/StreamFile.cpp?rev=114188&r1=114187&r2=114188&view=diff
==============================================================================
--- lldb/trunk/source/Core/StreamFile.cpp (original)
+++ lldb/trunk/source/Core/StreamFile.cpp Fri Sep 17 12:42:16 2010
@@ -31,16 +31,16 @@
 
 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, FILE *f) :
     Stream (flags, addr_size, byte_order),
-    m_file(f),
-    m_close_file(false),
+    m_file (f),
+    m_close_file (false),
     m_path_name ()
 {
 }
 
-StreamFile::StreamFile(FILE *f) :
+StreamFile::StreamFile(FILE *f, bool tranfer_ownership) :
     Stream (),
-    m_file(f),
-    m_close_file(false),
+    m_file (f),
+    m_close_file (tranfer_ownership),
     m_path_name ()
 {
 }





More information about the lldb-commits mailing list