[Lldb-commits] [lldb] r220894 - Start adopting the StringPrinter API. The StringPrinter API is the new blessed way of printing strings that supports escaping non-printables, and has better handling of different UTF encodings
Enrico Granata
egranata at apple.com
Wed Oct 29 18:45:40 PDT 2014
Author: enrico
Date: Wed Oct 29 20:45:39 2014
New Revision: 220894
URL: http://llvm.org/viewvc/llvm-project?rev=220894&view=rev
Log:
Start adopting the StringPrinter API. The StringPrinter API is the new blessed way of printing strings that supports escaping non-printables, and has better handling of different UTF encodings
Added:
lldb/trunk/include/lldb/DataFormatters/StringPrinter.h
lldb/trunk/source/DataFormatters/StringPrinter.cpp
Modified:
lldb/trunk/lldb.xcodeproj/project.pbxproj
lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp
Added: lldb/trunk/include/lldb/DataFormatters/StringPrinter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/StringPrinter.h?rev=220894&view=auto
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/StringPrinter.h (added)
+++ lldb/trunk/include/lldb/DataFormatters/StringPrinter.h Wed Oct 29 20:45:39 2014
@@ -0,0 +1,271 @@
+//===-- StringPrinter.h -----------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_StringPrinter_h_
+#define liblldb_StringPrinter_h_
+
+#include "lldb/lldb-forward.h"
+
+#include "lldb/Core/DataExtractor.h"
+
+namespace lldb_private {
+ namespace formatters
+ {
+
+ enum class StringElementType {
+ ASCII,
+ UTF8,
+ UTF16,
+ UTF32
+ };
+
+ class ReadStringAndDumpToStreamOptions
+ {
+ public:
+
+ ReadStringAndDumpToStreamOptions () :
+ m_location(0),
+ m_process_sp(),
+ m_stream(NULL),
+ m_prefix_token(0),
+ m_quote('"'),
+ m_source_size(0),
+ m_needs_zero_termination(true),
+ m_escape_non_printables(true)
+ {
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetLocation (uint64_t l)
+ {
+ m_location = l;
+ return *this;
+ }
+
+ uint64_t
+ GetLocation () const
+ {
+ return m_location;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetProcessSP (lldb::ProcessSP p)
+ {
+ m_process_sp = p;
+ return *this;
+ }
+
+ lldb::ProcessSP
+ GetProcessSP () const
+ {
+ return m_process_sp;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetStream (Stream* s)
+ {
+ m_stream = s;
+ return *this;
+ }
+
+ Stream*
+ GetStream () const
+ {
+ return m_stream;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetPrefixToken (char p)
+ {
+ m_prefix_token = p;
+ return *this;
+ }
+
+ char
+ GetPrefixToken () const
+ {
+ return m_prefix_token;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetQuote (char q)
+ {
+ m_quote = q;
+ return *this;
+ }
+
+ char
+ GetQuote () const
+ {
+ return m_quote;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetSourceSize (uint32_t s)
+ {
+ m_source_size = s;
+ return *this;
+ }
+
+ uint32_t
+ GetSourceSize () const
+ {
+ return m_source_size;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetNeedsZeroTermination (bool z)
+ {
+ m_needs_zero_termination = z;
+ return *this;
+ }
+
+ bool
+ GetNeedsZeroTermination () const
+ {
+ return m_needs_zero_termination;
+ }
+
+ ReadStringAndDumpToStreamOptions&
+ SetEscapeNonPrintables (bool e)
+ {
+ m_escape_non_printables = e;
+ return *this;
+ }
+
+ bool
+ GetEscapeNonPrintables () const
+ {
+ return m_escape_non_printables;
+ }
+
+ private:
+ uint64_t m_location;
+ lldb::ProcessSP m_process_sp;
+ Stream* m_stream;
+ char m_prefix_token;
+ char m_quote;
+ uint32_t m_source_size;
+ bool m_needs_zero_termination;
+ bool m_escape_non_printables;
+ };
+
+ class ReadBufferAndDumpToStreamOptions
+ {
+ public:
+
+ ReadBufferAndDumpToStreamOptions () :
+ m_data(),
+ m_stream(NULL),
+ m_prefix_token(0),
+ m_quote('"'),
+ m_source_size(0),
+ m_escape_non_printables(true)
+ {
+ }
+
+ ReadBufferAndDumpToStreamOptions&
+ SetData (DataExtractor d)
+ {
+ m_data = d;
+ return *this;
+ }
+
+ lldb_private::DataExtractor
+ GetData () const
+ {
+ return m_data;
+ }
+
+ ReadBufferAndDumpToStreamOptions&
+ SetStream (Stream* s)
+ {
+ m_stream = s;
+ return *this;
+ }
+
+ Stream*
+ GetStream () const
+ {
+ return m_stream;
+ }
+
+ ReadBufferAndDumpToStreamOptions&
+ SetPrefixToken (char p)
+ {
+ m_prefix_token = p;
+ return *this;
+ }
+
+ char
+ GetPrefixToken () const
+ {
+ return m_prefix_token;
+ }
+
+ ReadBufferAndDumpToStreamOptions&
+ SetQuote (char q)
+ {
+ m_quote = q;
+ return *this;
+ }
+
+ char
+ GetQuote () const
+ {
+ return m_quote;
+ }
+
+ ReadBufferAndDumpToStreamOptions&
+ SetSourceSize (uint32_t s)
+ {
+ m_source_size = s;
+ return *this;
+ }
+
+ uint32_t
+ GetSourceSize () const
+ {
+ return m_source_size;
+ }
+
+ ReadBufferAndDumpToStreamOptions&
+ SetEscapeNonPrintables (bool e)
+ {
+ m_escape_non_printables = e;
+ return *this;
+ }
+
+ bool
+ GetEscapeNonPrintables () const
+ {
+ return m_escape_non_printables;
+ }
+
+ private:
+ DataExtractor m_data;
+ Stream* m_stream;
+ char m_prefix_token;
+ char m_quote;
+ uint32_t m_source_size;
+ bool m_escape_non_printables;
+ };
+
+ template <StringElementType element_type>
+ bool
+ ReadStringAndDumpToStream (ReadStringAndDumpToStreamOptions options);
+
+ template <StringElementType element_type>
+ bool
+ ReadBufferAndDumpToStream (ReadBufferAndDumpToStreamOptions options);
+
+ } // namespace formatters
+} // namespace lldb_private
+
+#endif // liblldb_StringPrinter_h_
Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=220894&r1=220893&r2=220894&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Wed Oct 29 20:45:39 2014
@@ -699,6 +699,7 @@
94E829CA152D33C1006F96A3 /* lldb-platform in Resources */ = {isa = PBXBuildFile; fileRef = 26DC6A101337FE6900FF7998 /* lldb-platform */; };
94EA1D5C15E6C9B400D4171A /* PythonDataObjects.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94EA1D5B15E6C9B400D4171A /* PythonDataObjects.cpp */; };
94EA27CE17DE91750070F505 /* LibCxxUnorderedMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94EA27CD17DE91750070F505 /* LibCxxUnorderedMap.cpp */; };
+ 94F48F251A01C687005C0EC6 /* StringPrinter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94F48F241A01C687005C0EC6 /* StringPrinter.cpp */; };
94FA3DE01405D50400833217 /* ValueObjectConstResultChild.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94FA3DDF1405D50300833217 /* ValueObjectConstResultChild.cpp */; };
966C6B7918E6A56A0093F5EC /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 966C6B7818E6A56A0093F5EC /* libz.dylib */; };
966C6B7A18E6A56A0093F5EC /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 966C6B7818E6A56A0093F5EC /* libz.dylib */; };
@@ -2077,6 +2078,8 @@
94EBAC8313D9EE26009BA64E /* PythonPointer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = PythonPointer.h; path = include/lldb/Utility/PythonPointer.h; sourceTree = "<group>"; };
94ED54A119C8A822007BE2EA /* ThreadSafeDenseMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ThreadSafeDenseMap.h; path = include/lldb/Core/ThreadSafeDenseMap.h; sourceTree = "<group>"; };
94EE33F218643C6900CD703B /* FormattersContainer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = FormattersContainer.h; path = include/lldb/DataFormatters/FormattersContainer.h; sourceTree = "<group>"; };
+ 94F48F231A01C679005C0EC6 /* StringPrinter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = StringPrinter.h; path = include/lldb/DataFormatters/StringPrinter.h; sourceTree = "<group>"; };
+ 94F48F241A01C687005C0EC6 /* StringPrinter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StringPrinter.cpp; path = source/DataFormatters/StringPrinter.cpp; sourceTree = "<group>"; };
94F6C4D119C264C70049D089 /* ProcessStructReader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ProcessStructReader.h; path = include/lldb/Utility/ProcessStructReader.h; sourceTree = "<group>"; };
94FA3DDD1405D4E500833217 /* ValueObjectConstResultChild.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ValueObjectConstResultChild.h; path = include/lldb/Core/ValueObjectConstResultChild.h; sourceTree = "<group>"; };
94FA3DDF1405D50300833217 /* ValueObjectConstResultChild.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ValueObjectConstResultChild.cpp; path = source/Core/ValueObjectConstResultChild.cpp; sourceTree = "<group>"; };
@@ -4336,6 +4339,8 @@
94D6A0A816CEB55F00833B6E /* NSDictionary.cpp */,
9439FB1919EF140C006FD6A4 /* NSIndexPath.cpp */,
94D6A0A916CEB55F00833B6E /* NSSet.cpp */,
+ 94F48F231A01C679005C0EC6 /* StringPrinter.h */,
+ 94F48F241A01C687005C0EC6 /* StringPrinter.cpp */,
94CB256816B096F90059775D /* TypeCategory.h */,
94CB256416B096F10059775D /* TypeCategory.cpp */,
94CB256916B096FA0059775D /* TypeCategoryMap.h */,
@@ -5442,6 +5447,7 @@
2698699B15E6CBD0002415FF /* OperatingSystemPython.cpp in Sources */,
947A1D641616476B0017C8D1 /* CommandObjectPlugin.cpp in Sources */,
262ED0081631FA3A00879631 /* OptionGroupString.cpp in Sources */,
+ 94F48F251A01C687005C0EC6 /* StringPrinter.cpp in Sources */,
94094C6B163B6F840083A547 /* ValueObjectCast.cpp in Sources */,
AF9107EF168570D200DBCD3C /* RegisterContextDarwin_arm64.cpp in Sources */,
94CB255B16B069770059775D /* CXXFormatterFunctions.cpp in Sources */,
Modified: lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp?rev=220894&r1=220893&r2=220894&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp (original)
+++ lldb/trunk/source/DataFormatters/CXXFormatterFunctions.cpp Wed Oct 29 20:45:39 2014
@@ -10,6 +10,7 @@
#include "lldb/lldb-python.h"
#include "lldb/DataFormatters/CXXFormatterFunctions.h"
+#include "lldb/DataFormatters/StringPrinter.h"
#include "llvm/Support/ConvertUTF.h"
@@ -189,281 +190,6 @@ lldb_private::formatters::CallSelectorOn
return valobj_sp;
}
-// use this call if you already have an LLDB-side buffer for the data
-template<typename SourceDataType>
-static bool
-DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**,
- const SourceDataType*,
- UTF8**,
- UTF8*,
- ConversionFlags),
- DataExtractor& data,
- Stream& stream,
- char prefix_token = '@',
- char quote = '"',
- uint32_t sourceSize = 0)
-{
- if (prefix_token != 0)
- stream.Printf("%c",prefix_token);
- if (quote != 0)
- stream.Printf("%c",quote);
- if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd())
- {
- const int bufferSPSize = data.GetByteSize();
- if (sourceSize == 0)
- {
- const int origin_encoding = 8*sizeof(SourceDataType);
- sourceSize = bufferSPSize/(origin_encoding / 4);
- }
-
- SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart();
- SourceDataType *data_end_ptr = data_ptr + sourceSize;
-
- while (data_ptr < data_end_ptr)
- {
- if (!*data_ptr)
- {
- data_end_ptr = data_ptr;
- break;
- }
- data_ptr++;
- }
-
- data_ptr = (SourceDataType*)data.GetDataStart();
-
- lldb::DataBufferSP utf8_data_buffer_sp;
- UTF8* utf8_data_ptr = nullptr;
- UTF8* utf8_data_end_ptr = nullptr;
-
- if (ConvertFunction)
- {
- utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0));
- utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes();
- utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
- ConvertFunction ( (const SourceDataType**)&data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion );
- utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr
- }
- else
- {
- // just copy the pointers - the cast is necessary to make the compiler happy
- // but this should only happen if we are reading UTF8 data
- utf8_data_ptr = (UTF8*)data_ptr;
- utf8_data_end_ptr = (UTF8*)data_end_ptr;
- }
-
- // 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; utf8_data_ptr++)
- {
- if (!*utf8_data_ptr)
- break;
- stream.Printf("%c",*utf8_data_ptr);
- }
- }
- if (quote != 0)
- stream.Printf("%c",quote);
- return true;
-}
-
-template<typename SourceDataType>
-class ReadUTFBufferAndDumpToStreamOptions
-{
-public:
- typedef ConversionResult (*ConvertFunctionType) (const SourceDataType**,
- const SourceDataType*,
- UTF8**,
- UTF8*,
- ConversionFlags);
-
- ReadUTFBufferAndDumpToStreamOptions () :
- m_conversion_function(NULL),
- m_location(0),
- m_process_sp(),
- m_stream(NULL),
- m_prefix_token('@'),
- m_quote('"'),
- m_source_size(0),
- m_needs_zero_termination(true)
- {
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetConversionFunction (ConvertFunctionType f)
- {
- m_conversion_function = f;
- return *this;
- }
-
- ConvertFunctionType
- GetConversionFunction () const
- {
- return m_conversion_function;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetLocation (uint64_t l)
- {
- m_location = l;
- return *this;
- }
-
- uint64_t
- GetLocation () const
- {
- return m_location;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetProcessSP (ProcessSP p)
- {
- m_process_sp = p;
- return *this;
- }
-
- ProcessSP
- GetProcessSP () const
- {
- return m_process_sp;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetStream (Stream* s)
- {
- m_stream = s;
- return *this;
- }
-
- Stream*
- GetStream () const
- {
- return m_stream;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetPrefixToken (char p)
- {
- m_prefix_token = p;
- return *this;
- }
-
- char
- GetPrefixToken () const
- {
- return m_prefix_token;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetQuote (char q)
- {
- m_quote = q;
- return *this;
- }
-
- char
- GetQuote () const
- {
- return m_quote;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetSourceSize (uint32_t s)
- {
- m_source_size = s;
- return *this;
- }
-
- uint32_t
- GetSourceSize () const
- {
- return m_source_size;
- }
-
- ReadUTFBufferAndDumpToStreamOptions&
- SetNeedsZeroTermination (bool z)
- {
- m_needs_zero_termination = z;
- return *this;
- }
-
- bool
- GetNeedsZeroTermination () const
- {
- return m_needs_zero_termination;
- }
-
-private:
- ConvertFunctionType m_conversion_function;
- uint64_t m_location;
- ProcessSP m_process_sp;
- Stream* m_stream;
- char m_prefix_token;
- char m_quote;
- uint32_t m_source_size;
- bool m_needs_zero_termination;
-};
-
-template<typename SourceDataType>
-static bool
-ReadUTFBufferAndDumpToStream (const ReadUTFBufferAndDumpToStreamOptions<SourceDataType>& options)
-{
- if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS)
- return false;
-
- ProcessSP process_sp(options.GetProcessSP());
-
- if (!process_sp)
- return false;
-
- const int type_width = sizeof(SourceDataType);
- const int origin_encoding = 8 * type_width ;
- if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
- return false;
- // if not UTF8, I need a conversion function to return proper UTF8
- if (origin_encoding != 8 && !options.GetConversionFunction())
- return false;
-
- if (!options.GetStream())
- return false;
-
- uint32_t sourceSize = options.GetSourceSize();
- bool needs_zero_terminator = options.GetNeedsZeroTermination();
-
- if (!sourceSize)
- {
- sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
- needs_zero_terminator = true;
- }
- else
- sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
-
- const int bufferSPSize = sourceSize * type_width;
-
- lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0));
-
- if (!buffer_sp->GetBytes())
- return false;
-
- Error error;
- char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
-
- size_t data_read = 0;
- if (needs_zero_terminator)
- data_read = process_sp->ReadStringFromMemory(options.GetLocation(), buffer, bufferSPSize, error, type_width);
- else
- data_read = process_sp->ReadMemoryFromInferior(options.GetLocation(), (char*)buffer_sp->GetBytes(), bufferSPSize, error);
-
- if (error.Fail() || data_read == 0)
- {
- options.GetStream()->Printf("unable to read data");
- return true;
- }
-
- DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize());
-
- return DumpUTFBufferToStream(options.GetConversionFunction(), data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize);
-}
-
bool
lldb_private::formatters::Char16StringSummaryProvider (ValueObject& valobj, Stream& stream)
{
@@ -476,14 +202,13 @@ lldb_private::formatters::Char16StringSu
if (!valobj_addr)
return false;
- ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
+ ReadStringAndDumpToStreamOptions options;
options.SetLocation(valobj_addr);
- options.SetConversionFunction(ConvertUTF16toUTF8);
options.SetProcessSP(process_sp);
options.SetStream(&stream);
options.SetPrefixToken('u');
- if (!ReadUTFBufferAndDumpToStream(options))
+ if (!ReadStringAndDumpToStream<StringElementType::UTF16>(options))
{
stream.Printf("Summary Unavailable");
return true;
@@ -504,14 +229,13 @@ lldb_private::formatters::Char32StringSu
if (!valobj_addr)
return false;
- ReadUTFBufferAndDumpToStreamOptions<UTF32> options;
+ ReadStringAndDumpToStreamOptions options;
options.SetLocation(valobj_addr);
- options.SetConversionFunction(ConvertUTF32toUTF8);
options.SetProcessSP(process_sp);
options.SetStream(&stream);
options.SetPrefixToken('U');
- if (!ReadUTFBufferAndDumpToStream(options))
+ if (!ReadStringAndDumpToStream<StringElementType::UTF32>(options))
{
stream.Printf("Summary Unavailable");
return true;
@@ -545,45 +269,20 @@ lldb_private::formatters::WCharStringSum
ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
const uint32_t wchar_size = wchar_clang_type.GetBitSize();
+ ReadStringAndDumpToStreamOptions options;
+ options.SetLocation(data_addr);
+ options.SetProcessSP(process_sp);
+ options.SetStream(&stream);
+ options.SetPrefixToken('L');
+
switch (wchar_size)
{
case 8:
- {
- // utf 8
-
- ReadUTFBufferAndDumpToStreamOptions<UTF8> options;
- options.SetLocation(data_addr);
- options.SetConversionFunction(nullptr);
- options.SetProcessSP(process_sp);
- options.SetStream(&stream);
- options.SetPrefixToken('L');
-
- return ReadUTFBufferAndDumpToStream(options);
- }
+ return ReadStringAndDumpToStream<StringElementType::UTF8>(options);
case 16:
- {
- // utf 16
- ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
- options.SetLocation(data_addr);
- options.SetConversionFunction(ConvertUTF16toUTF8);
- options.SetProcessSP(process_sp);
- options.SetStream(&stream);
- options.SetPrefixToken('L');
-
- return ReadUTFBufferAndDumpToStream(options);
- }
+ return ReadStringAndDumpToStream<StringElementType::UTF16>(options);
case 32:
- {
- // utf 32
- ReadUTFBufferAndDumpToStreamOptions<UTF32> options;
- options.SetLocation(data_addr);
- options.SetConversionFunction(ConvertUTF32toUTF8);
- options.SetProcessSP(process_sp);
- options.SetStream(&stream);
- options.SetPrefixToken('L');
-
- return ReadUTFBufferAndDumpToStream(options);
- }
+ return ReadStringAndDumpToStream<StringElementType::UTF32>(options);
default:
stream.Printf("size for wchar_t is not valid");
return true;
@@ -606,7 +305,14 @@ lldb_private::formatters::Char16SummaryP
if (!value.empty())
stream.Printf("%s ", value.c_str());
- return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,data,stream, 'u','\'',1);
+ ReadBufferAndDumpToStreamOptions options;
+ options.SetData(data);
+ options.SetStream(&stream);
+ options.SetPrefixToken('u');
+ options.SetQuote('\'');
+ options.SetSourceSize(1);
+
+ return ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
}
bool
@@ -624,7 +330,14 @@ lldb_private::formatters::Char32SummaryP
if (!value.empty())
stream.Printf("%s ", value.c_str());
- return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,data,stream, 'U','\'',1);
+ ReadBufferAndDumpToStreamOptions options;
+ options.SetData(data);
+ options.SetStream(&stream);
+ options.SetPrefixToken('U');
+ options.SetQuote('\'');
+ options.SetSourceSize(1);
+
+ return ReadBufferAndDumpToStream<StringElementType::UTF32>(options);
}
bool
@@ -637,55 +350,14 @@ lldb_private::formatters::WCharSummaryPr
if (error.Fail())
return false;
- clang::ASTContext* ast = valobj.GetClangType().GetASTContext();
-
- if (!ast)
- return false;
-
- ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
- const uint32_t wchar_size = wchar_clang_type.GetBitSize();
- std::string value;
+ ReadBufferAndDumpToStreamOptions options;
+ options.SetData(data);
+ options.SetStream(&stream);
+ options.SetPrefixToken('L');
+ options.SetQuote('\'');
+ options.SetSourceSize(1);
- switch (wchar_size)
- {
- case 8:
- // utf 8
- valobj.GetValueAsCString(lldb::eFormatChar, value);
- if (!value.empty())
- stream.Printf("%s ", value.c_str());
- return DumpUTFBufferToStream<UTF8>(nullptr,
- data,
- stream,
- 'L',
- '\'',
- 1);
- case 16:
- // utf 16
- valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
- if (!value.empty())
- stream.Printf("%s ", value.c_str());
- return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,
- data,
- stream,
- 'L',
- '\'',
- 1);
- case 32:
- // utf 32
- valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
- if (!value.empty())
- stream.Printf("%s ", value.c_str());
- return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,
- data,
- stream,
- 'L',
- '\'',
- 1);
- default:
- stream.Printf("size for wchar_t is not valid");
- return true;
- }
- return true;
+ return ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
}
// the field layout in a libc++ string (cap, side, data or data, size, cap)
@@ -1153,8 +825,7 @@ lldb_private::formatters::NSStringSummar
return false;
if (has_explicit_length && is_unicode)
{
- ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
- options.SetConversionFunction(ConvertUTF16toUTF8);
+ ReadStringAndDumpToStreamOptions options;
options.SetLocation(location);
options.SetProcessSP(process_sp);
options.SetStream(&stream);
@@ -1162,10 +833,20 @@ lldb_private::formatters::NSStringSummar
options.SetQuote('"');
options.SetSourceSize(explicit_length);
options.SetNeedsZeroTermination(false);
- return ReadUTFBufferAndDumpToStream (options);
+ return ReadStringAndDumpToStream<StringElementType::UTF16>(options);
}
else
- return ReadAsciiBufferAndDumpToStream(location+1,process_sp,stream, explicit_length);
+ {
+ ReadStringAndDumpToStreamOptions options;
+ options.SetLocation(location+1);
+ options.SetProcessSP(process_sp);
+ options.SetStream(&stream);
+ options.SetPrefixToken('@');
+ options.SetSourceSize(explicit_length);
+ options.SetNeedsZeroTermination(false);
+
+ return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
+ }
}
else if (is_inline && has_explicit_length && !is_unicode && !is_special && !is_mutable)
{
@@ -1191,8 +872,7 @@ lldb_private::formatters::NSStringSummar
if (error.Fail())
return false;
}
- ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
- options.SetConversionFunction(ConvertUTF16toUTF8);
+ ReadStringAndDumpToStreamOptions options;
options.SetLocation(location);
options.SetProcessSP(process_sp);
options.SetStream(&stream);
@@ -1200,7 +880,7 @@ lldb_private::formatters::NSStringSummar
options.SetQuote('"');
options.SetSourceSize(explicit_length);
options.SetNeedsZeroTermination(has_explicit_length == false);
- return ReadUTFBufferAndDumpToStream (options);
+ return ReadStringAndDumpToStream<StringElementType::UTF16> (options);
}
else if (is_special)
{
@@ -1208,8 +888,7 @@ lldb_private::formatters::NSStringSummar
explicit_length = reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
- ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
- options.SetConversionFunction(ConvertUTF16toUTF8);
+ ReadStringAndDumpToStreamOptions options;
options.SetLocation(location);
options.SetProcessSP(process_sp);
options.SetStream(&stream);
@@ -1217,14 +896,20 @@ lldb_private::formatters::NSStringSummar
options.SetQuote('"');
options.SetSourceSize(explicit_length);
options.SetNeedsZeroTermination(has_explicit_length == false);
- return ReadUTFBufferAndDumpToStream (options);
+ return ReadStringAndDumpToStream<StringElementType::UTF16> (options);
}
else if (is_inline)
{
uint64_t location = valobj_addr + 2*ptr_size;
if (!has_explicit_length)
location++;
- return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
+ ReadStringAndDumpToStreamOptions options;
+ options.SetLocation(location);
+ options.SetProcessSP(process_sp);
+ options.SetStream(&stream);
+ options.SetPrefixToken('@');
+ options.SetSourceSize(explicit_length);
+ return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
}
else
{
@@ -1234,7 +919,13 @@ lldb_private::formatters::NSStringSummar
return false;
if (has_explicit_length && !has_null)
explicit_length++; // account for the fact that there is no NULL and we need to have one added
- return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
+ ReadStringAndDumpToStreamOptions options;
+ options.SetLocation(location);
+ options.SetProcessSP(process_sp);
+ options.SetPrefixToken('@');
+ options.SetStream(&stream);
+ options.SetSourceSize(explicit_length);
+ return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
}
}
Added: lldb/trunk/source/DataFormatters/StringPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/StringPrinter.cpp?rev=220894&view=auto
==============================================================================
--- lldb/trunk/source/DataFormatters/StringPrinter.cpp (added)
+++ lldb/trunk/source/DataFormatters/StringPrinter.cpp Wed Oct 29 20:45:39 2014
@@ -0,0 +1,618 @@
+//===-- StringPrinter.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/DataFormatters/StringPrinter.h"
+
+#include "lldb/Core/DataExtractor.h"
+#include "lldb/Core/Error.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+
+#include "llvm/Support/ConvertUTF.h"
+
+#include <codecvt>
+#include <ctype.h>
+#include <functional>
+#include <locale>
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::formatters;
+
+// I can't use a std::unique_ptr for this because the Deleter is a template argument there
+// and I want the same type to represent both pointers I want to free and pointers I don't need
+// to free - which is what this class essentially is
+// It's very specialized to the needs of this file, and not suggested for general use
+template <typename T = uint8_t, typename U = char, typename S = size_t>
+struct StringPrinterBufferPointer
+{
+public:
+
+ typedef std::function<void(const T*)> Deleter;
+
+ StringPrinterBufferPointer (std::nullptr_t ptr) :
+ m_data(nullptr),
+ m_size(0),
+ m_deleter()
+ {}
+
+ StringPrinterBufferPointer(const T* bytes, S size, Deleter deleter = nullptr) :
+ m_data(bytes),
+ m_size(size),
+ m_deleter(deleter)
+ {}
+
+ StringPrinterBufferPointer(const U* bytes, S size, Deleter deleter = nullptr) :
+ m_data((T*)bytes),
+ m_size(size),
+ m_deleter(deleter)
+ {}
+
+ StringPrinterBufferPointer(StringPrinterBufferPointer&& rhs) :
+ m_data(rhs.m_data),
+ m_size(rhs.m_size),
+ m_deleter(rhs.m_deleter)
+ {
+ rhs.m_data = nullptr;
+ }
+
+ StringPrinterBufferPointer(const StringPrinterBufferPointer& rhs) :
+ m_data(rhs.m_data),
+ m_size(rhs.m_size),
+ m_deleter(rhs.m_deleter)
+ {
+ rhs.m_data = nullptr; // this is why m_data has to be mutable
+ }
+
+ const T*
+ GetBytes () const
+ {
+ return m_data;
+ }
+
+ const S
+ GetSize () const
+ {
+ return m_size;
+ }
+
+ ~StringPrinterBufferPointer ()
+ {
+ if (m_data && m_deleter)
+ m_deleter(m_data);
+ m_data = nullptr;
+ }
+
+ StringPrinterBufferPointer&
+ operator = (const StringPrinterBufferPointer& rhs)
+ {
+ if (m_data && m_deleter)
+ m_deleter(m_data);
+ m_data = rhs.m_data;
+ m_size = rhs.m_size;
+ m_deleter = rhs.m_deleter;
+ rhs.m_data = nullptr;
+ return *this;
+ }
+
+private:
+ mutable const T* m_data;
+ size_t m_size;
+ Deleter m_deleter;
+};
+
+// we define this for all values of type but only implement it for those we care about
+// that's good because we get linker errors for any unsupported type
+template <StringElementType type>
+static StringPrinterBufferPointer<>
+GetPrintableImpl(uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next);
+
+// mimic isprint() for Unicode codepoints
+static bool
+isprint(char32_t codepoint)
+{
+ if (codepoint <= 0x1F || codepoint == 0x7F) // C0
+ {
+ return false;
+ }
+ if (codepoint >= 0x80 && codepoint <= 0x9F) // C1
+ {
+ return false;
+ }
+ if (codepoint == 0x2028 || codepoint == 0x2029) // line/paragraph separators
+ {
+ return false;
+ }
+ if (codepoint == 0x200E || codepoint == 0x200F || (codepoint >= 0x202A && codepoint <= 0x202E)) // bidirectional text control
+ {
+ return false;
+ }
+ if (codepoint >= 0xFFF9 && codepoint <= 0xFFFF) // interlinears and generally specials
+ {
+ return false;
+ }
+ return true;
+}
+
+template <>
+StringPrinterBufferPointer<>
+GetPrintableImpl<StringElementType::ASCII> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
+{
+ StringPrinterBufferPointer<> retval = {nullptr};
+
+ switch (*buffer)
+ {
+ case '\a':
+ retval = {"\\a",2};
+ break;
+ case '\b':
+ retval = {"\\b",2};
+ break;
+ case '\f':
+ retval = {"\\f",2};
+ break;
+ case '\n':
+ retval = {"\\n",2};
+ break;
+ case '\r':
+ retval = {"\\r",2};
+ break;
+ case '\t':
+ retval = {"\\t",2};
+ break;
+ case '\v':
+ retval = {"\\v",2};
+ break;
+ case '\"':
+ retval = {"\\\"",2};
+ break;
+ case '\\':
+ retval = {"\\\\",2};
+ break;
+ default:
+ if (isprint(*buffer))
+ retval = {buffer,1};
+ else
+ {
+ retval = { new uint8_t[5],4,[] (const uint8_t* c) {delete[] c;} };
+ sprintf((char*)retval.GetBytes(),"\\x%02x",*buffer);
+ break;
+ }
+ }
+
+ next = buffer + 1;
+ return retval;
+}
+
+static char32_t
+ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1)
+{
+ return (c0-192)*64+(c1-128);
+}
+static char32_t
+ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1, unsigned char c2)
+{
+ return (c0-224)*4096+(c1-128)*64+(c2-128);
+}
+static char32_t
+ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1, unsigned char c2, unsigned char c3)
+{
+ return (c0-240)*262144+(c2-128)*4096+(c2-128)*64+(c3-128);
+}
+
+template <>
+StringPrinterBufferPointer<>
+GetPrintableImpl<StringElementType::UTF8> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
+{
+ StringPrinterBufferPointer<> retval {nullptr};
+
+ unsigned utf8_encoded_len = getNumBytesForUTF8(*buffer);
+
+ if (1+buffer_end-buffer < utf8_encoded_len)
+ {
+ // I don't have enough bytes - print whatever I have left
+ retval = {buffer,static_cast<size_t>(1+buffer_end-buffer)};
+ next = buffer_end+1;
+ return retval;
+ }
+
+ char32_t codepoint = 0;
+ switch (utf8_encoded_len)
+ {
+ case 1:
+ // this is just an ASCII byte - ask ASCII
+ return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next);
+ case 2:
+ codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer, (unsigned char)*(buffer+1));
+ break;
+ case 3:
+ codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer, (unsigned char)*(buffer+1), (unsigned char)*(buffer+2));
+ break;
+ case 4:
+ codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer, (unsigned char)*(buffer+1), (unsigned char)*(buffer+2), (unsigned char)*(buffer+3));
+ break;
+ default:
+ // this is probably some bogus non-character thing
+ // just print it as-is and hope to sync up again soon
+ retval = {buffer,1};
+ next = buffer+1;
+ return retval;
+ }
+
+ if (codepoint)
+ {
+ switch (codepoint)
+ {
+ case '\a':
+ retval = {"\\a",2};
+ break;
+ case '\b':
+ retval = {"\\b",2};
+ break;
+ case '\f':
+ retval = {"\\f",2};
+ break;
+ case '\n':
+ retval = {"\\n",2};
+ break;
+ case '\r':
+ retval = {"\\r",2};
+ break;
+ case '\t':
+ retval = {"\\t",2};
+ break;
+ case '\v':
+ retval = {"\\v",2};
+ break;
+ case '\"':
+ retval = {"\\\"",2};
+ break;
+ case '\\':
+ retval = {"\\\\",2};
+ break;
+ default:
+ if (isprint(codepoint))
+ retval = {buffer,utf8_encoded_len};
+ else
+ {
+ retval = { new uint8_t[11],10,[] (const uint8_t* c) {delete[] c;} };
+ sprintf((char*)retval.GetBytes(),"\\U%08x",codepoint);
+ break;
+ }
+ }
+
+ next = buffer + utf8_encoded_len;
+ return retval;
+ }
+
+ // this should not happen - but just in case.. try to resync at some point
+ retval = {buffer,1};
+ next = buffer+1;
+ return retval;
+}
+
+// Given a sequence of bytes, this function returns:
+// a sequence of bytes to actually print out + a length
+// the following unscanned position of the buffer is in next
+static StringPrinterBufferPointer<>
+GetPrintable(StringElementType type, uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
+{
+ if (!buffer)
+ return {nullptr};
+
+ switch (type)
+ {
+ case StringElementType::ASCII:
+ return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next);
+ case StringElementType::UTF8:
+ return GetPrintableImpl<StringElementType::UTF8>(buffer, buffer_end, next);
+ default:
+ return {nullptr};
+ }
+}
+
+template <>
+bool
+lldb_private::formatters::ReadStringAndDumpToStream<StringElementType::ASCII> (ReadStringAndDumpToStreamOptions options)
+{
+ assert(options.GetStream() && "need a Stream to print the string to");
+ Error my_error;
+ size_t my_data_read;
+
+ ProcessSP process_sp(options.GetProcessSP());
+
+ if (process_sp.get() == nullptr || options.GetLocation() == 0)
+ return false;
+
+ size_t size;
+
+ if (options.GetSourceSize() == 0)
+ size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
+ else
+ size = std::min(options.GetSourceSize(),process_sp->GetTarget().GetMaximumSizeOfStringSummary());
+
+ lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0));
+
+ my_data_read = process_sp->ReadCStringFromMemory(options.GetLocation(), (char*)buffer_sp->GetBytes(), size, my_error);
+
+ if (my_error.Fail())
+ return false;
+
+ char prefix_token = options.GetPrefixToken();
+ char quote = options.GetQuote();
+
+ if (prefix_token != 0)
+ options.GetStream()->Printf("%c%c",prefix_token,quote);
+ else if (quote != 0)
+ options.GetStream()->Printf("%c",quote);
+
+ uint8_t* data_end = buffer_sp->GetBytes()+buffer_sp->GetByteSize();
+
+ // 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 (uint8_t* data = buffer_sp->GetBytes(); *data && (data < data_end);)
+ {
+ if (options.GetEscapeNonPrintables())
+ {
+ uint8_t* next_data = nullptr;
+ auto printable = GetPrintable(StringElementType::ASCII, data, data_end, next_data);
+ auto printable_bytes = printable.GetBytes();
+ auto printable_size = printable.GetSize();
+ if (!printable_bytes || !next_data)
+ {
+ // GetPrintable() failed on us - print one byte in a desperate resync attempt
+ printable_bytes = data;
+ printable_size = 1;
+ next_data = data+1;
+ }
+ for (int c = 0; c < printable_size; c++)
+ options.GetStream()->Printf("%c", *(printable_bytes+c));
+ data = (uint8_t*)next_data;
+ }
+ else
+ {
+ options.GetStream()->Printf("%c",*data);
+ data++;
+ }
+ }
+
+ if (quote != 0)
+ options.GetStream()->Printf("%c",quote);
+
+ return true;
+}
+
+// use this call if you already have an LLDB-side buffer for the data
+template<typename SourceDataType>
+static bool
+DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**,
+ const SourceDataType*,
+ UTF8**,
+ UTF8*,
+ ConversionFlags),
+ const DataExtractor& data,
+ Stream& stream,
+ char prefix_token,
+ char quote,
+ uint32_t sourceSize,
+ bool escapeNonPrintables)
+{
+ if (prefix_token != 0)
+ stream.Printf("%c",prefix_token);
+ if (quote != 0)
+ stream.Printf("%c",quote);
+ if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd())
+ {
+ const int bufferSPSize = data.GetByteSize();
+ if (sourceSize == 0)
+ {
+ const int origin_encoding = 8*sizeof(SourceDataType);
+ sourceSize = bufferSPSize/(origin_encoding / 4);
+ }
+
+ SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart();
+ SourceDataType *data_end_ptr = data_ptr + sourceSize;
+
+ while (data_ptr < data_end_ptr)
+ {
+ if (!*data_ptr)
+ {
+ data_end_ptr = data_ptr;
+ break;
+ }
+ data_ptr++;
+ }
+
+ data_ptr = (SourceDataType*)data.GetDataStart();
+
+ lldb::DataBufferSP utf8_data_buffer_sp;
+ UTF8* utf8_data_ptr = nullptr;
+ UTF8* utf8_data_end_ptr = nullptr;
+
+ if (ConvertFunction)
+ {
+ utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0));
+ utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes();
+ utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
+ ConvertFunction ( (const SourceDataType**)&data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion );
+ utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr
+ }
+ else
+ {
+ // just copy the pointers - the cast is necessary to make the compiler happy
+ // but this should only happen if we are reading UTF8 data
+ utf8_data_ptr = (UTF8*)data_ptr;
+ utf8_data_end_ptr = (UTF8*)data_end_ptr;
+ }
+
+ // 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)
+ break;
+
+ if (escapeNonPrintables)
+ {
+ uint8_t* next_data = nullptr;
+ auto printable = GetPrintable(StringElementType::UTF8, utf8_data_ptr, utf8_data_end_ptr, next_data);
+ auto printable_bytes = printable.GetBytes();
+ auto printable_size = printable.GetSize();
+ if (!printable_bytes || !next_data)
+ {
+ // GetPrintable() failed on us - print one byte in a desperate resync attempt
+ printable_bytes = utf8_data_ptr;
+ printable_size = 1;
+ next_data = utf8_data_ptr+1;
+ }
+ for (int c = 0; c < printable_size; c++)
+ stream.Printf("%c", *(printable_bytes+c));
+ utf8_data_ptr = (uint8_t*)next_data;
+ }
+ else
+ {
+ stream.Printf("%c",*utf8_data_ptr);
+ utf8_data_ptr++;
+ }
+ }
+ }
+ if (quote != 0)
+ stream.Printf("%c",quote);
+ return true;
+}
+
+template<typename SourceDataType>
+static bool
+ReadUTFBufferAndDumpToStream (const ReadStringAndDumpToStreamOptions& options,
+ ConversionResult (*ConvertFunction) (const SourceDataType**,
+ const SourceDataType*,
+ UTF8**,
+ UTF8*,
+ ConversionFlags))
+{
+ assert(options.GetStream() && "need a Stream to print the string to");
+
+ if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS)
+ return false;
+
+ lldb::ProcessSP process_sp(options.GetProcessSP());
+
+ if (!process_sp)
+ return false;
+
+ const int type_width = sizeof(SourceDataType);
+ const int origin_encoding = 8 * type_width ;
+ if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
+ return false;
+ // if not UTF8, I need a conversion function to return proper UTF8
+ if (origin_encoding != 8 && !ConvertFunction)
+ return false;
+
+ if (!options.GetStream())
+ return false;
+
+ uint32_t sourceSize = options.GetSourceSize();
+ bool needs_zero_terminator = options.GetNeedsZeroTermination();
+
+ if (!sourceSize)
+ {
+ sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
+ needs_zero_terminator = true;
+ }
+ else
+ sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
+
+ const int bufferSPSize = sourceSize * type_width;
+
+ lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0));
+
+ if (!buffer_sp->GetBytes())
+ return false;
+
+ Error error;
+ char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
+
+ size_t data_read = 0;
+ if (needs_zero_terminator)
+ data_read = process_sp->ReadStringFromMemory(options.GetLocation(), buffer, bufferSPSize, error, type_width);
+ else
+ data_read = process_sp->ReadMemoryFromInferior(options.GetLocation(), (char*)buffer_sp->GetBytes(), bufferSPSize, error);
+
+ if (error.Fail() || data_read == 0)
+ {
+ options.GetStream()->Printf("unable to read data");
+ return true;
+ }
+
+ DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize());
+
+ return DumpUTFBufferToStream(ConvertFunction, data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize, options.GetEscapeNonPrintables());
+}
+
+template <>
+bool
+lldb_private::formatters::ReadStringAndDumpToStream<StringElementType::UTF8> (ReadStringAndDumpToStreamOptions options)
+{
+ return ReadUTFBufferAndDumpToStream<UTF8>(options,
+ nullptr);
+}
+
+template <>
+bool
+lldb_private::formatters::ReadStringAndDumpToStream<StringElementType::UTF16> (ReadStringAndDumpToStreamOptions options)
+{
+ return ReadUTFBufferAndDumpToStream<UTF16>(options,
+ ConvertUTF16toUTF8);
+}
+
+template <>
+bool
+lldb_private::formatters::ReadStringAndDumpToStream<StringElementType::UTF32> (ReadStringAndDumpToStreamOptions options)
+{
+ return ReadUTFBufferAndDumpToStream<UTF32>(options,
+ ConvertUTF32toUTF8);
+}
+
+template <>
+bool
+lldb_private::formatters::ReadBufferAndDumpToStream<StringElementType::UTF8> (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());
+}
+
+template <>
+bool
+lldb_private::formatters::ReadBufferAndDumpToStream<StringElementType::ASCII> (ReadBufferAndDumpToStreamOptions options)
+{
+ // treat ASCII the same as UTF8
+ // FIXME: can we optimize ASCII some more?
+ return ReadBufferAndDumpToStream<StringElementType::UTF8>(options);
+}
+
+template <>
+bool
+lldb_private::formatters::ReadBufferAndDumpToStream<StringElementType::UTF16> (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());
+}
+
+template <>
+bool
+lldb_private::formatters::ReadBufferAndDumpToStream<StringElementType::UTF32> (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());
+}
More information about the lldb-commits
mailing list