[Lldb-commits] [lldb] r242496 - Add StringPrinter support for printing a std::string with embedded NUL bytes

Enrico Granata egranata at apple.com
Thu Jul 16 18:04:00 PDT 2015


Author: enrico
Date: Thu Jul 16 20:03:59 2015
New Revision: 242496

URL: http://llvm.org/viewvc/llvm-project?rev=242496&view=rev
Log:
Add StringPrinter support for printing a std::string with embedded NUL bytes


Modified:
    lldb/trunk/include/lldb/DataFormatters/StringPrinter.h
    lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp
    lldb/trunk/source/DataFormatters/StringPrinter.cpp
    lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/TestDataFormatterLibcxxString.py
    lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/main.cpp

Modified: lldb/trunk/include/lldb/DataFormatters/StringPrinter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/StringPrinter.h?rev=242496&r1=242495&r2=242496&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/StringPrinter.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/StringPrinter.h Thu Jul 16 20:03:59 2015
@@ -38,7 +38,8 @@ namespace lldb_private {
             m_source_size(0),
             m_needs_zero_termination(true),
             m_escape_non_printables(true),
-            m_ignore_max_length(false)
+            m_ignore_max_length(false),
+            m_zero_is_terminator(true)
             {
             }
             
@@ -136,6 +137,19 @@ namespace lldb_private {
             }
             
             ReadStringAndDumpToStreamOptions&
+            SetBinaryZeroIsTerminator (bool e)
+            {
+                m_zero_is_terminator = e;
+                return *this;
+            }
+            
+            bool
+            GetBinaryZeroIsTerminator () const
+            {
+                return m_zero_is_terminator;
+            }
+            
+            ReadStringAndDumpToStreamOptions&
             SetEscapeNonPrintables (bool e)
             {
                 m_escape_non_printables = e;
@@ -171,6 +185,7 @@ namespace lldb_private {
             bool m_needs_zero_termination;
             bool m_escape_non_printables;
             bool m_ignore_max_length;
+            bool m_zero_is_terminator;
         };
         
         class ReadBufferAndDumpToStreamOptions
@@ -183,12 +198,15 @@ namespace lldb_private {
             m_prefix_token(0),
             m_quote('"'),
             m_source_size(0),
-            m_escape_non_printables(true)
+            m_escape_non_printables(true),
+            m_zero_is_terminator(true)
             {
             }
             
             ReadBufferAndDumpToStreamOptions (ValueObject& valobj);
             
+            ReadBufferAndDumpToStreamOptions (const ReadStringAndDumpToStreamOptions& options);
+            
             ReadBufferAndDumpToStreamOptions&
             SetData (DataExtractor d)
             {
@@ -267,6 +285,19 @@ namespace lldb_private {
                 return m_escape_non_printables;
             }
             
+            ReadBufferAndDumpToStreamOptions&
+            SetBinaryZeroIsTerminator (bool e)
+            {
+                m_zero_is_terminator = e;
+                return *this;
+            }
+            
+            bool
+            GetBinaryZeroIsTerminator () const
+            {
+                return m_zero_is_terminator;
+            }
+            
         private:
             DataExtractor m_data;
             Stream* m_stream;
@@ -274,15 +305,16 @@ namespace lldb_private {
             char m_quote;
             uint32_t m_source_size;
             bool m_escape_non_printables;
+            bool m_zero_is_terminator;
         };
         
         template <StringElementType element_type>
         bool
-        ReadStringAndDumpToStream (ReadStringAndDumpToStreamOptions options);
+        ReadStringAndDumpToStream (const ReadStringAndDumpToStreamOptions& options);
         
         template <StringElementType element_type>
         bool
-        ReadBufferAndDumpToStream (ReadBufferAndDumpToStreamOptions options);
+        ReadBufferAndDumpToStream (const ReadBufferAndDumpToStreamOptions& options);
         
     } // namespace formatters
 } // namespace lldb_private

Modified: lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp?rev=242496&r1=242495&r2=242496&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp (original)
+++ lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp Thu Jul 16 20:03:59 2015
@@ -539,6 +539,7 @@ lldb_private::formatters::LibcxxStringSu
     options.SetPrefixToken(0);
     options.SetQuote('"');
     options.SetSourceSize(size);
+    options.SetBinaryZeroIsTerminator(false);
     lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
     
     return true;

Modified: lldb/trunk/source/DataFormatters/StringPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/StringPrinter.cpp?rev=242496&r1=242495&r2=242496&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/StringPrinter.cpp (original)
+++ lldb/trunk/source/DataFormatters/StringPrinter.cpp Thu Jul 16 20:03:59 2015
@@ -334,41 +334,44 @@ DumpUTFBufferToStream (ConversionResult
                                                             UTF8**,
                                                             UTF8*,
                                                             ConversionFlags),
-                       const DataExtractor& data,
-                       Stream& stream,
-                       char prefix_token,
-                       char quote,
-                       uint32_t sourceSize,
-                       bool escapeNonPrintables)
+                       const ReadBufferAndDumpToStreamOptions& dump_options)
 {
-    if (prefix_token != 0)
-        stream.Printf("%c",prefix_token);
-    if (quote != 0)
-        stream.Printf("%c",quote);
+    Stream &stream(*dump_options.GetStream());
+    if (dump_options.GetPrefixToken() != 0)
+        stream.Printf("%c",dump_options.GetPrefixToken());
+    if (dump_options.GetQuote() != 0)
+        stream.Printf("%c",dump_options.GetQuote());
+    auto data(dump_options.GetData());
+    auto source_size(dump_options.GetSourceSize());
     if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd())
     {
         const int bufferSPSize = data.GetByteSize();
-        if (sourceSize == 0)
+        if (dump_options.GetSourceSize() == 0)
         {
             const int origin_encoding = 8*sizeof(SourceDataType);
-            sourceSize = bufferSPSize/(origin_encoding / 4);
+            source_size = bufferSPSize/(origin_encoding / 4);
         }
         
         const SourceDataType *data_ptr = (const SourceDataType*)data.GetDataStart();
-        const SourceDataType *data_end_ptr = data_ptr + sourceSize;
+        const SourceDataType *data_end_ptr = data_ptr + source_size;
         
-        while (data_ptr < data_end_ptr)
+        const bool zero_is_terminator = dump_options.GetBinaryZeroIsTerminator();
+        
+        if (zero_is_terminator)
         {
-            if (!*data_ptr)
+            while (data_ptr < data_end_ptr)
             {
-                data_end_ptr = data_ptr;
-                break;
+                if (!*data_ptr)
+                {
+                    data_end_ptr = data_ptr;
+                    break;
+                }
+                data_ptr++;
             }
-            data_ptr++;
+            
+            data_ptr = (const SourceDataType*)data.GetDataStart();
         }
         
-        data_ptr = (const SourceDataType*)data.GetDataStart();
-        
         lldb::DataBufferSP utf8_data_buffer_sp;
         UTF8* utf8_data_ptr = nullptr;
         UTF8* utf8_data_end_ptr = nullptr;
@@ -389,15 +392,17 @@ DumpUTFBufferToStream (ConversionResult
             utf8_data_end_ptr = (UTF8*)data_end_ptr;
         }
         
+        const bool escape_non_printables = dump_options.GetEscapeNonPrintables();
+        
         // since we tend to accept partial data (and even partially malformed data)
         // we might end up with no NULL terminator before the end_ptr
         // hence we need to take a slower route and ensure we stay within boundaries
         for (;utf8_data_ptr < utf8_data_end_ptr;)
         {
-            if (!*utf8_data_ptr)
+            if (zero_is_terminator && !*utf8_data_ptr)
                 break;
             
-            if (escapeNonPrintables)
+            if (escape_non_printables)
             {
                 uint8_t* next_data = nullptr;
                 auto printable = GetPrintable(StringElementType::UTF8, utf8_data_ptr, utf8_data_end_ptr, next_data);
@@ -421,8 +426,8 @@ DumpUTFBufferToStream (ConversionResult
             }
         }
     }
-    if (quote != 0)
-        stream.Printf("%c",quote);
+    if (dump_options.GetQuote() != 0)
+        stream.Printf("%c",dump_options.GetQuote());
     return true;
 }
 
@@ -438,6 +443,16 @@ lldb_private::formatters::ReadBufferAndD
     SetEscapeNonPrintables(valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
 }
 
+lldb_private::formatters::ReadBufferAndDumpToStreamOptions::ReadBufferAndDumpToStreamOptions (const lldb_private::formatters::ReadStringAndDumpToStreamOptions& options) :
+    ReadBufferAndDumpToStreamOptions()
+{
+    SetStream(options.GetStream());
+    SetPrefixToken(options.GetPrefixToken());
+    SetQuote(options.GetQuote());
+    SetEscapeNonPrintables(options.GetEscapeNonPrintables());
+    SetBinaryZeroIsTerminator(options.GetBinaryZeroIsTerminator());
+}
+
 
 namespace lldb_private
 {
@@ -447,7 +462,7 @@ namespace formatters
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::ASCII> (ReadStringAndDumpToStreamOptions options)
+ReadStringAndDumpToStream<StringElementType::ASCII> (const ReadStringAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
     Error my_error;
@@ -581,13 +596,17 @@ ReadUTFBufferAndDumpToStream (const Read
     }
 
     DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize());
+    
+    ReadBufferAndDumpToStreamOptions dump_options(options);
+    dump_options.SetData(data);
+    dump_options.SetSourceSize(sourceSize);
 
-    return DumpUTFBufferToStream(ConvertFunction, data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize, options.GetEscapeNonPrintables());
+    return DumpUTFBufferToStream(ConvertFunction, dump_options);
 }
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::UTF8> (ReadStringAndDumpToStreamOptions options)
+ReadStringAndDumpToStream<StringElementType::UTF8> (const ReadStringAndDumpToStreamOptions& options)
 {
     return ReadUTFBufferAndDumpToStream<UTF8>(options,
                                               nullptr);
@@ -595,7 +614,7 @@ ReadStringAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::UTF16> (ReadStringAndDumpToStreamOptions options)
+ReadStringAndDumpToStream<StringElementType::UTF16> (const ReadStringAndDumpToStreamOptions& options)
 {
     return ReadUTFBufferAndDumpToStream<UTF16>(options,
                                                ConvertUTF16toUTF8);
@@ -603,7 +622,7 @@ ReadStringAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::UTF32> (ReadStringAndDumpToStreamOptions options)
+ReadStringAndDumpToStream<StringElementType::UTF32> (const ReadStringAndDumpToStreamOptions& options)
 {
     return ReadUTFBufferAndDumpToStream<UTF32>(options,
                                                ConvertUTF32toUTF8);
@@ -611,16 +630,16 @@ ReadStringAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::UTF8> (ReadBufferAndDumpToStreamOptions options)
+ReadBufferAndDumpToStream<StringElementType::UTF8> (const ReadBufferAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
 
-    return DumpUTFBufferToStream<UTF8>(nullptr, options.GetData(), *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), options.GetSourceSize(), options.GetEscapeNonPrintables());
+    return DumpUTFBufferToStream<UTF8>(nullptr, options);
 }
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::ASCII> (ReadBufferAndDumpToStreamOptions options)
+ReadBufferAndDumpToStream<StringElementType::ASCII> (const ReadBufferAndDumpToStreamOptions& options)
 {
     // treat ASCII the same as UTF8
     // FIXME: can we optimize ASCII some more?
@@ -629,20 +648,20 @@ ReadBufferAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::UTF16> (ReadBufferAndDumpToStreamOptions options)
+ReadBufferAndDumpToStream<StringElementType::UTF16> (const ReadBufferAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
 
-    return DumpUTFBufferToStream(ConvertUTF16toUTF8, options.GetData(), *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), options.GetSourceSize(), options.GetEscapeNonPrintables());
+    return DumpUTFBufferToStream(ConvertUTF16toUTF8, options);
 }
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::UTF32> (ReadBufferAndDumpToStreamOptions options)
+ReadBufferAndDumpToStream<StringElementType::UTF32> (const ReadBufferAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
 
-    return DumpUTFBufferToStream(ConvertUTF32toUTF8, options.GetData(), *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), options.GetSourceSize(), options.GetEscapeNonPrintables());
+    return DumpUTFBufferToStream(ConvertUTF32toUTF8, options);
 }
 
 } // namespace formatters

Modified: lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/TestDataFormatterLibcxxString.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/TestDataFormatterLibcxxString.py?rev=242496&r1=242495&r2=242496&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/TestDataFormatterLibcxxString.py (original)
+++ lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/TestDataFormatterLibcxxString.py Thu Jul 16 20:03:59 2015
@@ -64,7 +64,8 @@ class LibcxxStringDataFormatterTestCase(
                     '(std::__1::wstring) S = L"!!!!"',
                     '(const wchar_t *) mazeltov = 0x','L"מזל טוב"',
                     '(std::__1::string) q = "hello world"',
-                    '(std::__1::string) Q = "quite a long std::strin with lots of info inside it"'])
+                    '(std::__1::string) Q = "quite a long std::strin with lots of info inside it"',
+                    '(std::__1::string) IHaveEmbeddedZeros = "a\\0b\\0c\\0d"'])
 
         self.runCmd("n")
 
@@ -86,7 +87,8 @@ class LibcxxStringDataFormatterTestCase(
                     '(std::__1::wstring) S = L"!!!!!"',
                     '(const wchar_t *) mazeltov = 0x','L"מזל טוב"',
                     '(std::__1::string) q = "hello world"',
-                    '(std::__1::string) Q = "quite a long std::strin with lots of info inside it"'])
+                    '(std::__1::string) Q = "quite a long std::strin with lots of info inside it"',
+                    '(std::__1::string) IHaveEmbeddedZeros = "a\\0b\\0c\\0d"'])
 
 if __name__ == '__main__':
     import atexit

Modified: lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/main.cpp?rev=242496&r1=242495&r2=242496&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/main.cpp (original)
+++ lldb/trunk/test/functionalities/data-formatter/data-formatter-stl/libcxx/string/main.cpp Thu Jul 16 20:03:59 2015
@@ -8,6 +8,7 @@ int main()
     std::string q("hello world");
     std::string Q("quite a long std::strin with lots of info inside it");
     std::string TheVeryLongOne("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
 6789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
 3456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890someText1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
 2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
 9012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
 6789012345678901234567890123456789012345678901234567890");
+    std::string IHaveEmbeddedZeros("a\0b\0c\0d",7);
     S.assign(L"!!!!!"); // Set break point at this line.
     return 0;
 }






More information about the lldb-commits mailing list