1435933ddSDimitry Andric //===-- StringPrinter.cpp ----------------------------------------*- C++
2435933ddSDimitry Andric //-*-===//
37aa51b79SEd Maste //
47aa51b79SEd Maste //                     The LLVM Compiler Infrastructure
57aa51b79SEd Maste //
67aa51b79SEd Maste // This file is distributed under the University of Illinois Open Source
77aa51b79SEd Maste // License. See LICENSE.TXT for details.
87aa51b79SEd Maste //
97aa51b79SEd Maste //===----------------------------------------------------------------------===//
107aa51b79SEd Maste 
117aa51b79SEd Maste #include "lldb/DataFormatters/StringPrinter.h"
127aa51b79SEd Maste 
137aa51b79SEd Maste #include "lldb/Core/Debugger.h"
147aa51b79SEd Maste #include "lldb/Core/ValueObject.h"
159f2f44ceSEd Maste #include "lldb/Target/Language.h"
167aa51b79SEd Maste #include "lldb/Target/Process.h"
177aa51b79SEd Maste #include "lldb/Target/Target.h"
185517e702SDimitry Andric #include "lldb/Utility/Status.h"
197aa51b79SEd Maste 
207aa51b79SEd Maste #include "llvm/Support/ConvertUTF.h"
217aa51b79SEd Maste 
227aa51b79SEd Maste #include <ctype.h>
237aa51b79SEd Maste #include <locale>
247aa51b79SEd Maste 
257aa51b79SEd Maste using namespace lldb;
267aa51b79SEd Maste using namespace lldb_private;
277aa51b79SEd Maste using namespace lldb_private::formatters;
287aa51b79SEd Maste 
294ba319b5SDimitry Andric // we define this for all values of type but only implement it for those we
304ba319b5SDimitry Andric // care about that's good because we get linker errors for any unsupported type
319f2f44ceSEd Maste template <lldb_private::formatters::StringPrinter::StringElementType type>
329f2f44ceSEd Maste static StringPrinter::StringPrinterBufferPointer<>
337aa51b79SEd Maste GetPrintableImpl(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next);
347aa51b79SEd Maste 
357aa51b79SEd Maste // mimic isprint() for Unicode codepoints
isprint(char32_t codepoint)36435933ddSDimitry Andric static bool isprint(char32_t codepoint) {
377aa51b79SEd Maste   if (codepoint <= 0x1F || codepoint == 0x7F) // C0
387aa51b79SEd Maste   {
397aa51b79SEd Maste     return false;
407aa51b79SEd Maste   }
417aa51b79SEd Maste   if (codepoint >= 0x80 && codepoint <= 0x9F) // C1
427aa51b79SEd Maste   {
437aa51b79SEd Maste     return false;
447aa51b79SEd Maste   }
457aa51b79SEd Maste   if (codepoint == 0x2028 || codepoint == 0x2029) // line/paragraph separators
467aa51b79SEd Maste   {
477aa51b79SEd Maste     return false;
487aa51b79SEd Maste   }
49435933ddSDimitry Andric   if (codepoint == 0x200E || codepoint == 0x200F ||
50435933ddSDimitry Andric       (codepoint >= 0x202A &&
51435933ddSDimitry Andric        codepoint <= 0x202E)) // bidirectional text control
527aa51b79SEd Maste   {
537aa51b79SEd Maste     return false;
547aa51b79SEd Maste   }
55435933ddSDimitry Andric   if (codepoint >= 0xFFF9 &&
56435933ddSDimitry Andric       codepoint <= 0xFFFF) // interlinears and generally specials
577aa51b79SEd Maste   {
587aa51b79SEd Maste     return false;
597aa51b79SEd Maste   }
607aa51b79SEd Maste   return true;
617aa51b79SEd Maste }
627aa51b79SEd Maste 
637aa51b79SEd Maste template <>
649f2f44ceSEd Maste StringPrinter::StringPrinterBufferPointer<>
GetPrintableImpl(uint8_t * buffer,uint8_t * buffer_end,uint8_t * & next)65435933ddSDimitry Andric GetPrintableImpl<StringPrinter::StringElementType::ASCII>(uint8_t *buffer,
66435933ddSDimitry Andric                                                           uint8_t *buffer_end,
67435933ddSDimitry Andric                                                           uint8_t *&next) {
689f2f44ceSEd Maste   StringPrinter::StringPrinterBufferPointer<> retval = {nullptr};
697aa51b79SEd Maste 
70435933ddSDimitry Andric   switch (*buffer) {
717aa51b79SEd Maste   case 0:
727aa51b79SEd Maste     retval = {"\\0", 2};
737aa51b79SEd Maste     break;
747aa51b79SEd Maste   case '\a':
757aa51b79SEd Maste     retval = {"\\a", 2};
767aa51b79SEd Maste     break;
777aa51b79SEd Maste   case '\b':
787aa51b79SEd Maste     retval = {"\\b", 2};
797aa51b79SEd Maste     break;
807aa51b79SEd Maste   case '\f':
817aa51b79SEd Maste     retval = {"\\f", 2};
827aa51b79SEd Maste     break;
837aa51b79SEd Maste   case '\n':
847aa51b79SEd Maste     retval = {"\\n", 2};
857aa51b79SEd Maste     break;
867aa51b79SEd Maste   case '\r':
877aa51b79SEd Maste     retval = {"\\r", 2};
887aa51b79SEd Maste     break;
897aa51b79SEd Maste   case '\t':
907aa51b79SEd Maste     retval = {"\\t", 2};
917aa51b79SEd Maste     break;
927aa51b79SEd Maste   case '\v':
937aa51b79SEd Maste     retval = {"\\v", 2};
947aa51b79SEd Maste     break;
957aa51b79SEd Maste   case '\"':
967aa51b79SEd Maste     retval = {"\\\"", 2};
977aa51b79SEd Maste     break;
987aa51b79SEd Maste   case '\\':
997aa51b79SEd Maste     retval = {"\\\\", 2};
1007aa51b79SEd Maste     break;
1017aa51b79SEd Maste   default:
1027aa51b79SEd Maste     if (isprint(*buffer))
1037aa51b79SEd Maste       retval = {buffer, 1};
104435933ddSDimitry Andric     else {
1051c3bbb01SEd Maste       uint8_t *data = new uint8_t[5];
1061c3bbb01SEd Maste       sprintf((char *)data, "\\x%02x", *buffer);
1071c3bbb01SEd Maste       retval = {data, 4, [](const uint8_t *c) { delete[] c; }};
1087aa51b79SEd Maste       break;
1097aa51b79SEd Maste     }
1107aa51b79SEd Maste   }
1117aa51b79SEd Maste 
1127aa51b79SEd Maste   next = buffer + 1;
1137aa51b79SEd Maste   return retval;
1147aa51b79SEd Maste }
1157aa51b79SEd Maste 
ConvertUTF8ToCodePoint(unsigned char c0,unsigned char c1)116435933ddSDimitry Andric static char32_t ConvertUTF8ToCodePoint(unsigned char c0, unsigned char c1) {
1177aa51b79SEd Maste   return (c0 - 192) * 64 + (c1 - 128);
1187aa51b79SEd Maste }
ConvertUTF8ToCodePoint(unsigned char c0,unsigned char c1,unsigned char c2)119435933ddSDimitry Andric static char32_t ConvertUTF8ToCodePoint(unsigned char c0, unsigned char c1,
120435933ddSDimitry Andric                                        unsigned char c2) {
1217aa51b79SEd Maste   return (c0 - 224) * 4096 + (c1 - 128) * 64 + (c2 - 128);
1227aa51b79SEd Maste }
ConvertUTF8ToCodePoint(unsigned char c0,unsigned char c1,unsigned char c2,unsigned char c3)123435933ddSDimitry Andric static char32_t ConvertUTF8ToCodePoint(unsigned char c0, unsigned char c1,
124435933ddSDimitry Andric                                        unsigned char c2, unsigned char c3) {
1257aa51b79SEd Maste   return (c0 - 240) * 262144 + (c2 - 128) * 4096 + (c2 - 128) * 64 + (c3 - 128);
1267aa51b79SEd Maste }
1277aa51b79SEd Maste 
1287aa51b79SEd Maste template <>
1299f2f44ceSEd Maste StringPrinter::StringPrinterBufferPointer<>
GetPrintableImpl(uint8_t * buffer,uint8_t * buffer_end,uint8_t * & next)130435933ddSDimitry Andric GetPrintableImpl<StringPrinter::StringElementType::UTF8>(uint8_t *buffer,
131435933ddSDimitry Andric                                                          uint8_t *buffer_end,
132435933ddSDimitry Andric                                                          uint8_t *&next) {
1339f2f44ceSEd Maste   StringPrinter::StringPrinterBufferPointer<> retval{nullptr};
1347aa51b79SEd Maste 
135435933ddSDimitry Andric   unsigned utf8_encoded_len = llvm::getNumBytesForUTF8(*buffer);
1367aa51b79SEd Maste 
137435933ddSDimitry Andric   if (1u + std::distance(buffer, buffer_end) < utf8_encoded_len) {
1387aa51b79SEd Maste     // I don't have enough bytes - print whatever I have left
1397aa51b79SEd Maste     retval = {buffer, static_cast<size_t>(1 + buffer_end - buffer)};
1407aa51b79SEd Maste     next = buffer_end + 1;
1417aa51b79SEd Maste     return retval;
1427aa51b79SEd Maste   }
1437aa51b79SEd Maste 
1447aa51b79SEd Maste   char32_t codepoint = 0;
145435933ddSDimitry Andric   switch (utf8_encoded_len) {
1467aa51b79SEd Maste   case 1:
1477aa51b79SEd Maste     // this is just an ASCII byte - ask ASCII
148435933ddSDimitry Andric     return GetPrintableImpl<StringPrinter::StringElementType::ASCII>(
149435933ddSDimitry Andric         buffer, buffer_end, next);
1507aa51b79SEd Maste   case 2:
151435933ddSDimitry Andric     codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer,
152435933ddSDimitry Andric                                        (unsigned char)*(buffer + 1));
1537aa51b79SEd Maste     break;
1547aa51b79SEd Maste   case 3:
155435933ddSDimitry Andric     codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer,
156435933ddSDimitry Andric                                        (unsigned char)*(buffer + 1),
157435933ddSDimitry Andric                                        (unsigned char)*(buffer + 2));
1587aa51b79SEd Maste     break;
1597aa51b79SEd Maste   case 4:
160435933ddSDimitry Andric     codepoint = ConvertUTF8ToCodePoint(
161435933ddSDimitry Andric         (unsigned char)*buffer, (unsigned char)*(buffer + 1),
162435933ddSDimitry Andric         (unsigned char)*(buffer + 2), (unsigned char)*(buffer + 3));
1637aa51b79SEd Maste     break;
1647aa51b79SEd Maste   default:
1654ba319b5SDimitry Andric     // this is probably some bogus non-character thing just print it as-is and
1664ba319b5SDimitry Andric     // hope to sync up again soon
1677aa51b79SEd Maste     retval = {buffer, 1};
1687aa51b79SEd Maste     next = buffer + 1;
1697aa51b79SEd Maste     return retval;
1707aa51b79SEd Maste   }
1717aa51b79SEd Maste 
172435933ddSDimitry Andric   if (codepoint) {
173435933ddSDimitry Andric     switch (codepoint) {
1747aa51b79SEd Maste     case 0:
1757aa51b79SEd Maste       retval = {"\\0", 2};
1767aa51b79SEd Maste       break;
1777aa51b79SEd Maste     case '\a':
1787aa51b79SEd Maste       retval = {"\\a", 2};
1797aa51b79SEd Maste       break;
1807aa51b79SEd Maste     case '\b':
1817aa51b79SEd Maste       retval = {"\\b", 2};
1827aa51b79SEd Maste       break;
1837aa51b79SEd Maste     case '\f':
1847aa51b79SEd Maste       retval = {"\\f", 2};
1857aa51b79SEd Maste       break;
1867aa51b79SEd Maste     case '\n':
1877aa51b79SEd Maste       retval = {"\\n", 2};
1887aa51b79SEd Maste       break;
1897aa51b79SEd Maste     case '\r':
1907aa51b79SEd Maste       retval = {"\\r", 2};
1917aa51b79SEd Maste       break;
1927aa51b79SEd Maste     case '\t':
1937aa51b79SEd Maste       retval = {"\\t", 2};
1947aa51b79SEd Maste       break;
1957aa51b79SEd Maste     case '\v':
1967aa51b79SEd Maste       retval = {"\\v", 2};
1977aa51b79SEd Maste       break;
1987aa51b79SEd Maste     case '\"':
1997aa51b79SEd Maste       retval = {"\\\"", 2};
2007aa51b79SEd Maste       break;
2017aa51b79SEd Maste     case '\\':
2027aa51b79SEd Maste       retval = {"\\\\", 2};
2037aa51b79SEd Maste       break;
2047aa51b79SEd Maste     default:
2057aa51b79SEd Maste       if (isprint(codepoint))
2067aa51b79SEd Maste         retval = {buffer, utf8_encoded_len};
207435933ddSDimitry Andric       else {
2081c3bbb01SEd Maste         uint8_t *data = new uint8_t[11];
2094bb0738eSEd Maste         sprintf((char *)data, "\\U%08x", (unsigned)codepoint);
2101c3bbb01SEd Maste         retval = {data, 10, [](const uint8_t *c) { delete[] c; }};
2117aa51b79SEd Maste         break;
2127aa51b79SEd Maste       }
2137aa51b79SEd Maste     }
2147aa51b79SEd Maste 
2157aa51b79SEd Maste     next = buffer + utf8_encoded_len;
2167aa51b79SEd Maste     return retval;
2177aa51b79SEd Maste   }
2187aa51b79SEd Maste 
2197aa51b79SEd Maste   // this should not happen - but just in case.. try to resync at some point
2207aa51b79SEd Maste   retval = {buffer, 1};
2217aa51b79SEd Maste   next = buffer + 1;
2227aa51b79SEd Maste   return retval;
2237aa51b79SEd Maste }
2247aa51b79SEd Maste 
2254ba319b5SDimitry Andric // Given a sequence of bytes, this function returns: a sequence of bytes to
2264ba319b5SDimitry Andric // actually print out + a length the following unscanned position of the buffer
2274ba319b5SDimitry Andric // is in next
2289f2f44ceSEd Maste static StringPrinter::StringPrinterBufferPointer<>
GetPrintable(StringPrinter::StringElementType type,uint8_t * buffer,uint8_t * buffer_end,uint8_t * & next)229435933ddSDimitry Andric GetPrintable(StringPrinter::StringElementType type, uint8_t *buffer,
230435933ddSDimitry Andric              uint8_t *buffer_end, uint8_t *&next) {
2317aa51b79SEd Maste   if (!buffer)
2327aa51b79SEd Maste     return {nullptr};
2337aa51b79SEd Maste 
234435933ddSDimitry Andric   switch (type) {
2359f2f44ceSEd Maste   case StringPrinter::StringElementType::ASCII:
236435933ddSDimitry Andric     return GetPrintableImpl<StringPrinter::StringElementType::ASCII>(
237435933ddSDimitry Andric         buffer, buffer_end, next);
2389f2f44ceSEd Maste   case StringPrinter::StringElementType::UTF8:
239435933ddSDimitry Andric     return GetPrintableImpl<StringPrinter::StringElementType::UTF8>(
240435933ddSDimitry Andric         buffer, buffer_end, next);
2417aa51b79SEd Maste   default:
2427aa51b79SEd Maste     return {nullptr};
2437aa51b79SEd Maste   }
2447aa51b79SEd Maste }
2457aa51b79SEd Maste 
2469f2f44ceSEd Maste StringPrinter::EscapingHelper
GetDefaultEscapingHelper(GetPrintableElementType elem_type)247435933ddSDimitry Andric StringPrinter::GetDefaultEscapingHelper(GetPrintableElementType elem_type) {
248435933ddSDimitry Andric   switch (elem_type) {
2499f2f44ceSEd Maste   case GetPrintableElementType::UTF8:
250435933ddSDimitry Andric     return [](uint8_t *buffer, uint8_t *buffer_end,
251435933ddSDimitry Andric               uint8_t *&next) -> StringPrinter::StringPrinterBufferPointer<> {
252435933ddSDimitry Andric       return GetPrintable(StringPrinter::StringElementType::UTF8, buffer,
253435933ddSDimitry Andric                           buffer_end, next);
2549f2f44ceSEd Maste     };
2559f2f44ceSEd Maste   case GetPrintableElementType::ASCII:
256435933ddSDimitry Andric     return [](uint8_t *buffer, uint8_t *buffer_end,
257435933ddSDimitry Andric               uint8_t *&next) -> StringPrinter::StringPrinterBufferPointer<> {
258435933ddSDimitry Andric       return GetPrintable(StringPrinter::StringElementType::ASCII, buffer,
259435933ddSDimitry Andric                           buffer_end, next);
2609f2f44ceSEd Maste     };
2619f2f44ceSEd Maste   }
2629f2f44ceSEd Maste   llvm_unreachable("bad element type");
2639f2f44ceSEd Maste }
2649f2f44ceSEd Maste 
2657aa51b79SEd Maste // use this call if you already have an LLDB-side buffer for the data
2667aa51b79SEd Maste template <typename SourceDataType>
DumpUTFBufferToStream(llvm::ConversionResult (* ConvertFunction)(const SourceDataType **,const SourceDataType *,llvm::UTF8 **,llvm::UTF8 *,llvm::ConversionFlags),const StringPrinter::ReadBufferAndDumpToStreamOptions & dump_options)267435933ddSDimitry Andric static bool DumpUTFBufferToStream(
268435933ddSDimitry Andric     llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
2697aa51b79SEd Maste                                               const SourceDataType *,
270435933ddSDimitry Andric                                               llvm::UTF8 **, llvm::UTF8 *,
271435933ddSDimitry Andric                                               llvm::ConversionFlags),
272435933ddSDimitry Andric     const StringPrinter::ReadBufferAndDumpToStreamOptions &dump_options) {
2739f2f44ceSEd Maste   Stream &stream(*dump_options.GetStream());
2749f2f44ceSEd Maste   if (dump_options.GetPrefixToken() != 0)
2759f2f44ceSEd Maste     stream.Printf("%s", dump_options.GetPrefixToken());
2769f2f44ceSEd Maste   if (dump_options.GetQuote() != 0)
2779f2f44ceSEd Maste     stream.Printf("%c", dump_options.GetQuote());
2789f2f44ceSEd Maste   auto data(dump_options.GetData());
2799f2f44ceSEd Maste   auto source_size(dump_options.GetSourceSize());
280435933ddSDimitry Andric   if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) {
2817aa51b79SEd Maste     const int bufferSPSize = data.GetByteSize();
282435933ddSDimitry Andric     if (dump_options.GetSourceSize() == 0) {
2837aa51b79SEd Maste       const int origin_encoding = 8 * sizeof(SourceDataType);
2849f2f44ceSEd Maste       source_size = bufferSPSize / (origin_encoding / 4);
2857aa51b79SEd Maste     }
2867aa51b79SEd Maste 
287435933ddSDimitry Andric     const SourceDataType *data_ptr =
288435933ddSDimitry Andric         (const SourceDataType *)data.GetDataStart();
2899f2f44ceSEd Maste     const SourceDataType *data_end_ptr = data_ptr + source_size;
2907aa51b79SEd Maste 
2919f2f44ceSEd Maste     const bool zero_is_terminator = dump_options.GetBinaryZeroIsTerminator();
2929f2f44ceSEd Maste 
293435933ddSDimitry Andric     if (zero_is_terminator) {
294435933ddSDimitry Andric       while (data_ptr < data_end_ptr) {
295435933ddSDimitry Andric         if (!*data_ptr) {
2967aa51b79SEd Maste           data_end_ptr = data_ptr;
2977aa51b79SEd Maste           break;
2987aa51b79SEd Maste         }
2997aa51b79SEd Maste         data_ptr++;
3007aa51b79SEd Maste       }
3017aa51b79SEd Maste 
3021c3bbb01SEd Maste       data_ptr = (const SourceDataType *)data.GetDataStart();
3039f2f44ceSEd Maste     }
3047aa51b79SEd Maste 
3057aa51b79SEd Maste     lldb::DataBufferSP utf8_data_buffer_sp;
306435933ddSDimitry Andric     llvm::UTF8 *utf8_data_ptr = nullptr;
307435933ddSDimitry Andric     llvm::UTF8 *utf8_data_end_ptr = nullptr;
3087aa51b79SEd Maste 
309435933ddSDimitry Andric     if (ConvertFunction) {
3107aa51b79SEd Maste       utf8_data_buffer_sp.reset(new DataBufferHeap(4 * bufferSPSize, 0));
311435933ddSDimitry Andric       utf8_data_ptr = (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
3127aa51b79SEd Maste       utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
313435933ddSDimitry Andric       ConvertFunction(&data_ptr, data_end_ptr, &utf8_data_ptr,
314435933ddSDimitry Andric                       utf8_data_end_ptr, llvm::lenientConversion);
315*b5893f02SDimitry Andric       if (!zero_is_terminator)
3169f2f44ceSEd Maste         utf8_data_end_ptr = utf8_data_ptr;
317435933ddSDimitry Andric       // needed because the ConvertFunction will change the value of the
318435933ddSDimitry Andric       // data_ptr.
319435933ddSDimitry Andric       utf8_data_ptr =
320435933ddSDimitry Andric           (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
321435933ddSDimitry Andric     } else {
322435933ddSDimitry Andric       // just copy the pointers - the cast is necessary to make the compiler
3234ba319b5SDimitry Andric       // happy but this should only happen if we are reading UTF8 data
324435933ddSDimitry Andric       utf8_data_ptr = const_cast<llvm::UTF8 *>(
325435933ddSDimitry Andric           reinterpret_cast<const llvm::UTF8 *>(data_ptr));
326435933ddSDimitry Andric       utf8_data_end_ptr = const_cast<llvm::UTF8 *>(
327435933ddSDimitry Andric           reinterpret_cast<const llvm::UTF8 *>(data_end_ptr));
3289f2f44ceSEd Maste     }
3299f2f44ceSEd Maste 
3309f2f44ceSEd Maste     const bool escape_non_printables = dump_options.GetEscapeNonPrintables();
3319f2f44ceSEd Maste     lldb_private::formatters::StringPrinter::EscapingHelper escaping_callback;
332435933ddSDimitry Andric     if (escape_non_printables) {
3339f2f44ceSEd Maste       if (Language *language = Language::FindPlugin(dump_options.GetLanguage()))
334435933ddSDimitry Andric         escaping_callback = language->GetStringPrinterEscapingHelper(
335435933ddSDimitry Andric             lldb_private::formatters::StringPrinter::GetPrintableElementType::
336435933ddSDimitry Andric                 UTF8);
3379f2f44ceSEd Maste       else
338435933ddSDimitry Andric         escaping_callback =
339435933ddSDimitry Andric             lldb_private::formatters::StringPrinter::GetDefaultEscapingHelper(
340435933ddSDimitry Andric                 lldb_private::formatters::StringPrinter::
341435933ddSDimitry Andric                     GetPrintableElementType::UTF8);
3427aa51b79SEd Maste     }
3437aa51b79SEd Maste 
3447aa51b79SEd Maste     // since we tend to accept partial data (and even partially malformed data)
3454ba319b5SDimitry Andric     // we might end up with no NULL terminator before the end_ptr hence we need
3464ba319b5SDimitry Andric     // to take a slower route and ensure we stay within boundaries
347435933ddSDimitry Andric     for (; utf8_data_ptr < utf8_data_end_ptr;) {
3489f2f44ceSEd Maste       if (zero_is_terminator && !*utf8_data_ptr)
3497aa51b79SEd Maste         break;
3507aa51b79SEd Maste 
351435933ddSDimitry Andric       if (escape_non_printables) {
3527aa51b79SEd Maste         uint8_t *next_data = nullptr;
353435933ddSDimitry Andric         auto printable =
354435933ddSDimitry Andric             escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data);
3557aa51b79SEd Maste         auto printable_bytes = printable.GetBytes();
3567aa51b79SEd Maste         auto printable_size = printable.GetSize();
357435933ddSDimitry Andric         if (!printable_bytes || !next_data) {
358435933ddSDimitry Andric           // GetPrintable() failed on us - print one byte in a desperate resync
359435933ddSDimitry Andric           // attempt
3607aa51b79SEd Maste           printable_bytes = utf8_data_ptr;
3617aa51b79SEd Maste           printable_size = 1;
3627aa51b79SEd Maste           next_data = utf8_data_ptr + 1;
3637aa51b79SEd Maste         }
3647aa51b79SEd Maste         for (unsigned c = 0; c < printable_size; c++)
3657aa51b79SEd Maste           stream.Printf("%c", *(printable_bytes + c));
3667aa51b79SEd Maste         utf8_data_ptr = (uint8_t *)next_data;
367435933ddSDimitry Andric       } else {
3687aa51b79SEd Maste         stream.Printf("%c", *utf8_data_ptr);
3697aa51b79SEd Maste         utf8_data_ptr++;
3707aa51b79SEd Maste       }
3717aa51b79SEd Maste     }
3727aa51b79SEd Maste   }
3739f2f44ceSEd Maste   if (dump_options.GetQuote() != 0)
3749f2f44ceSEd Maste     stream.Printf("%c", dump_options.GetQuote());
3759f2f44ceSEd Maste   if (dump_options.GetSuffixToken() != 0)
3769f2f44ceSEd Maste     stream.Printf("%s", dump_options.GetSuffixToken());
3779f2f44ceSEd Maste   if (dump_options.GetIsTruncated())
3789f2f44ceSEd Maste     stream.Printf("...");
3797aa51b79SEd Maste   return true;
3807aa51b79SEd Maste }
3817aa51b79SEd Maste 
382435933ddSDimitry Andric lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::
ReadStringAndDumpToStreamOptions(ValueObject & valobj)383435933ddSDimitry Andric     ReadStringAndDumpToStreamOptions(ValueObject &valobj)
384435933ddSDimitry Andric     : ReadStringAndDumpToStreamOptions() {
385435933ddSDimitry Andric   SetEscapeNonPrintables(
386435933ddSDimitry Andric       valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
3877aa51b79SEd Maste }
3887aa51b79SEd Maste 
389435933ddSDimitry Andric lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
ReadBufferAndDumpToStreamOptions(ValueObject & valobj)390435933ddSDimitry Andric     ReadBufferAndDumpToStreamOptions(ValueObject &valobj)
391435933ddSDimitry Andric     : ReadBufferAndDumpToStreamOptions() {
392435933ddSDimitry Andric   SetEscapeNonPrintables(
393435933ddSDimitry Andric       valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
3947aa51b79SEd Maste }
3957aa51b79SEd Maste 
396435933ddSDimitry Andric lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
ReadBufferAndDumpToStreamOptions(const ReadStringAndDumpToStreamOptions & options)397435933ddSDimitry Andric     ReadBufferAndDumpToStreamOptions(
398435933ddSDimitry Andric         const ReadStringAndDumpToStreamOptions &options)
399435933ddSDimitry Andric     : ReadBufferAndDumpToStreamOptions() {
4009f2f44ceSEd Maste   SetStream(options.GetStream());
4019f2f44ceSEd Maste   SetPrefixToken(options.GetPrefixToken());
4029f2f44ceSEd Maste   SetSuffixToken(options.GetSuffixToken());
4039f2f44ceSEd Maste   SetQuote(options.GetQuote());
4049f2f44ceSEd Maste   SetEscapeNonPrintables(options.GetEscapeNonPrintables());
4059f2f44ceSEd Maste   SetBinaryZeroIsTerminator(options.GetBinaryZeroIsTerminator());
4069f2f44ceSEd Maste   SetLanguage(options.GetLanguage());
4079f2f44ceSEd Maste }
4089f2f44ceSEd Maste 
409435933ddSDimitry Andric namespace lldb_private {
4107aa51b79SEd Maste 
411435933ddSDimitry Andric namespace formatters {
4127aa51b79SEd Maste 
4137aa51b79SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)414435933ddSDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<
415435933ddSDimitry Andric     StringPrinter::StringElementType::ASCII>(
416435933ddSDimitry Andric     const ReadStringAndDumpToStreamOptions &options) {
4177aa51b79SEd Maste   assert(options.GetStream() && "need a Stream to print the string to");
4185517e702SDimitry Andric   Status my_error;
4197aa51b79SEd Maste 
4207aa51b79SEd Maste   ProcessSP process_sp(options.GetProcessSP());
4217aa51b79SEd Maste 
4227aa51b79SEd Maste   if (process_sp.get() == nullptr || options.GetLocation() == 0)
4237aa51b79SEd Maste     return false;
4247aa51b79SEd Maste 
4257aa51b79SEd Maste   size_t size;
4269f2f44ceSEd Maste   const auto max_size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
4279f2f44ceSEd Maste   bool is_truncated = false;
4287aa51b79SEd Maste 
4297aa51b79SEd Maste   if (options.GetSourceSize() == 0)
4309f2f44ceSEd Maste     size = max_size;
431435933ddSDimitry Andric   else if (!options.GetIgnoreMaxLength()) {
4329f2f44ceSEd Maste     size = options.GetSourceSize();
433435933ddSDimitry Andric     if (size > max_size) {
4349f2f44ceSEd Maste       size = max_size;
4359f2f44ceSEd Maste       is_truncated = true;
4369f2f44ceSEd Maste     }
437435933ddSDimitry Andric   } else
4387aa51b79SEd Maste     size = options.GetSourceSize();
4397aa51b79SEd Maste 
4407aa51b79SEd Maste   lldb::DataBufferSP buffer_sp(new DataBufferHeap(size, 0));
4417aa51b79SEd Maste 
442435933ddSDimitry Andric   process_sp->ReadCStringFromMemory(
443435933ddSDimitry Andric       options.GetLocation(), (char *)buffer_sp->GetBytes(), size, my_error);
4447aa51b79SEd Maste 
4457aa51b79SEd Maste   if (my_error.Fail())
4467aa51b79SEd Maste     return false;
4477aa51b79SEd Maste 
4489f2f44ceSEd Maste   const char *prefix_token = options.GetPrefixToken();
4497aa51b79SEd Maste   char quote = options.GetQuote();
4507aa51b79SEd Maste 
4517aa51b79SEd Maste   if (prefix_token != 0)
4529f2f44ceSEd Maste     options.GetStream()->Printf("%s%c", prefix_token, quote);
4537aa51b79SEd Maste   else if (quote != 0)
4547aa51b79SEd Maste     options.GetStream()->Printf("%c", quote);
4557aa51b79SEd Maste 
4567aa51b79SEd Maste   uint8_t *data_end = buffer_sp->GetBytes() + buffer_sp->GetByteSize();
4577aa51b79SEd Maste 
4589f2f44ceSEd Maste   const bool escape_non_printables = options.GetEscapeNonPrintables();
4599f2f44ceSEd Maste   lldb_private::formatters::StringPrinter::EscapingHelper escaping_callback;
460435933ddSDimitry Andric   if (escape_non_printables) {
4619f2f44ceSEd Maste     if (Language *language = Language::FindPlugin(options.GetLanguage()))
462435933ddSDimitry Andric       escaping_callback = language->GetStringPrinterEscapingHelper(
463435933ddSDimitry Andric           lldb_private::formatters::StringPrinter::GetPrintableElementType::
464435933ddSDimitry Andric               ASCII);
4659f2f44ceSEd Maste     else
466435933ddSDimitry Andric       escaping_callback =
467435933ddSDimitry Andric           lldb_private::formatters::StringPrinter::GetDefaultEscapingHelper(
468435933ddSDimitry Andric               lldb_private::formatters::StringPrinter::GetPrintableElementType::
469435933ddSDimitry Andric                   ASCII);
4709f2f44ceSEd Maste   }
4719f2f44ceSEd Maste 
4727aa51b79SEd Maste   // since we tend to accept partial data (and even partially malformed data)
4734ba319b5SDimitry Andric   // we might end up with no NULL terminator before the end_ptr hence we need
4744ba319b5SDimitry Andric   // to take a slower route and ensure we stay within boundaries
475435933ddSDimitry Andric   for (uint8_t *data = buffer_sp->GetBytes(); *data && (data < data_end);) {
476435933ddSDimitry Andric     if (escape_non_printables) {
4777aa51b79SEd Maste       uint8_t *next_data = nullptr;
4789f2f44ceSEd Maste       auto printable = escaping_callback(data, data_end, next_data);
4797aa51b79SEd Maste       auto printable_bytes = printable.GetBytes();
4807aa51b79SEd Maste       auto printable_size = printable.GetSize();
481435933ddSDimitry Andric       if (!printable_bytes || !next_data) {
482435933ddSDimitry Andric         // GetPrintable() failed on us - print one byte in a desperate resync
483435933ddSDimitry Andric         // attempt
4847aa51b79SEd Maste         printable_bytes = data;
4857aa51b79SEd Maste         printable_size = 1;
4867aa51b79SEd Maste         next_data = data + 1;
4877aa51b79SEd Maste       }
4887aa51b79SEd Maste       for (unsigned c = 0; c < printable_size; c++)
4897aa51b79SEd Maste         options.GetStream()->Printf("%c", *(printable_bytes + c));
4907aa51b79SEd Maste       data = (uint8_t *)next_data;
491435933ddSDimitry Andric     } else {
4927aa51b79SEd Maste       options.GetStream()->Printf("%c", *data);
4937aa51b79SEd Maste       data++;
4947aa51b79SEd Maste     }
4957aa51b79SEd Maste   }
4967aa51b79SEd Maste 
4979f2f44ceSEd Maste   const char *suffix_token = options.GetSuffixToken();
4989f2f44ceSEd Maste 
4999f2f44ceSEd Maste   if (suffix_token != 0)
5009f2f44ceSEd Maste     options.GetStream()->Printf("%c%s", quote, suffix_token);
5019f2f44ceSEd Maste   else if (quote != 0)
5027aa51b79SEd Maste     options.GetStream()->Printf("%c", quote);
5037aa51b79SEd Maste 
5049f2f44ceSEd Maste   if (is_truncated)
5059f2f44ceSEd Maste     options.GetStream()->Printf("...");
5069f2f44ceSEd Maste 
5077aa51b79SEd Maste   return true;
5087aa51b79SEd Maste }
5097aa51b79SEd Maste 
5107aa51b79SEd Maste template <typename SourceDataType>
ReadUTFBufferAndDumpToStream(const StringPrinter::ReadStringAndDumpToStreamOptions & options,llvm::ConversionResult (* ConvertFunction)(const SourceDataType **,const SourceDataType *,llvm::UTF8 **,llvm::UTF8 *,llvm::ConversionFlags))511435933ddSDimitry Andric static bool ReadUTFBufferAndDumpToStream(
512435933ddSDimitry Andric     const StringPrinter::ReadStringAndDumpToStreamOptions &options,
513435933ddSDimitry Andric     llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
5147aa51b79SEd Maste                                               const SourceDataType *,
515435933ddSDimitry Andric                                               llvm::UTF8 **, llvm::UTF8 *,
516435933ddSDimitry Andric                                               llvm::ConversionFlags)) {
5177aa51b79SEd Maste   assert(options.GetStream() && "need a Stream to print the string to");
5187aa51b79SEd Maste 
519435933ddSDimitry Andric   if (options.GetLocation() == 0 ||
520435933ddSDimitry Andric       options.GetLocation() == LLDB_INVALID_ADDRESS)
5217aa51b79SEd Maste     return false;
5227aa51b79SEd Maste 
5237aa51b79SEd Maste   lldb::ProcessSP process_sp(options.GetProcessSP());
5247aa51b79SEd Maste 
5257aa51b79SEd Maste   if (!process_sp)
5267aa51b79SEd Maste     return false;
5277aa51b79SEd Maste 
5287aa51b79SEd Maste   const int type_width = sizeof(SourceDataType);
5297aa51b79SEd Maste   const int origin_encoding = 8 * type_width;
5307aa51b79SEd Maste   if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
5317aa51b79SEd Maste     return false;
5327aa51b79SEd Maste   // if not UTF8, I need a conversion function to return proper UTF8
5337aa51b79SEd Maste   if (origin_encoding != 8 && !ConvertFunction)
5347aa51b79SEd Maste     return false;
5357aa51b79SEd Maste 
5367aa51b79SEd Maste   if (!options.GetStream())
5377aa51b79SEd Maste     return false;
5387aa51b79SEd Maste 
5397aa51b79SEd Maste   uint32_t sourceSize = options.GetSourceSize();
5407aa51b79SEd Maste   bool needs_zero_terminator = options.GetNeedsZeroTermination();
5417aa51b79SEd Maste 
5429f2f44ceSEd Maste   bool is_truncated = false;
5439f2f44ceSEd Maste   const auto max_size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
5449f2f44ceSEd Maste 
545435933ddSDimitry Andric   if (!sourceSize) {
5469f2f44ceSEd Maste     sourceSize = max_size;
5477aa51b79SEd Maste     needs_zero_terminator = true;
548435933ddSDimitry Andric   } else if (!options.GetIgnoreMaxLength()) {
549435933ddSDimitry Andric     if (sourceSize > max_size) {
5509f2f44ceSEd Maste       sourceSize = max_size;
5519f2f44ceSEd Maste       is_truncated = true;
5529f2f44ceSEd Maste     }
5539f2f44ceSEd Maste   }
5547aa51b79SEd Maste 
5557aa51b79SEd Maste   const int bufferSPSize = sourceSize * type_width;
5567aa51b79SEd Maste 
5577aa51b79SEd Maste   lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize, 0));
5587aa51b79SEd Maste 
5597aa51b79SEd Maste   if (!buffer_sp->GetBytes())
5607aa51b79SEd Maste     return false;
5617aa51b79SEd Maste 
5625517e702SDimitry Andric   Status error;
5637aa51b79SEd Maste   char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
5647aa51b79SEd Maste 
5657aa51b79SEd Maste   if (needs_zero_terminator)
566435933ddSDimitry Andric     process_sp->ReadStringFromMemory(options.GetLocation(), buffer,
567435933ddSDimitry Andric                                      bufferSPSize, error, type_width);
5687aa51b79SEd Maste   else
569435933ddSDimitry Andric     process_sp->ReadMemoryFromInferior(options.GetLocation(),
570435933ddSDimitry Andric                                        (char *)buffer_sp->GetBytes(),
571435933ddSDimitry Andric                                        bufferSPSize, error);
5727aa51b79SEd Maste 
573435933ddSDimitry Andric   if (error.Fail()) {
5747aa51b79SEd Maste     options.GetStream()->Printf("unable to read data");
5757aa51b79SEd Maste     return true;
5767aa51b79SEd Maste   }
5777aa51b79SEd Maste 
578435933ddSDimitry Andric   DataExtractor data(buffer_sp, process_sp->GetByteOrder(),
579435933ddSDimitry Andric                      process_sp->GetAddressByteSize());
5807aa51b79SEd Maste 
5819f2f44ceSEd Maste   StringPrinter::ReadBufferAndDumpToStreamOptions dump_options(options);
5829f2f44ceSEd Maste   dump_options.SetData(data);
5839f2f44ceSEd Maste   dump_options.SetSourceSize(sourceSize);
5849f2f44ceSEd Maste   dump_options.SetIsTruncated(is_truncated);
5859f2f44ceSEd Maste 
5869f2f44ceSEd Maste   return DumpUTFBufferToStream(ConvertFunction, dump_options);
5877aa51b79SEd Maste }
5887aa51b79SEd Maste 
5897aa51b79SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)590435933ddSDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<
591435933ddSDimitry Andric     StringPrinter::StringElementType::UTF8>(
592435933ddSDimitry Andric     const ReadStringAndDumpToStreamOptions &options) {
593435933ddSDimitry Andric   return ReadUTFBufferAndDumpToStream<llvm::UTF8>(options, nullptr);
5947aa51b79SEd Maste }
5957aa51b79SEd Maste 
5967aa51b79SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)597435933ddSDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<
598435933ddSDimitry Andric     StringPrinter::StringElementType::UTF16>(
599435933ddSDimitry Andric     const ReadStringAndDumpToStreamOptions &options) {
600435933ddSDimitry Andric   return ReadUTFBufferAndDumpToStream<llvm::UTF16>(options,
601435933ddSDimitry Andric                                                    llvm::ConvertUTF16toUTF8);
6027aa51b79SEd Maste }
6037aa51b79SEd Maste 
6047aa51b79SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)605435933ddSDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<
606435933ddSDimitry Andric     StringPrinter::StringElementType::UTF32>(
607435933ddSDimitry Andric     const ReadStringAndDumpToStreamOptions &options) {
608435933ddSDimitry Andric   return ReadUTFBufferAndDumpToStream<llvm::UTF32>(options,
609435933ddSDimitry Andric                                                    llvm::ConvertUTF32toUTF8);
6107aa51b79SEd Maste }
6117aa51b79SEd Maste 
6127aa51b79SEd Maste template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)613435933ddSDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<
614435933ddSDimitry Andric     StringPrinter::StringElementType::UTF8>(
615435933ddSDimitry Andric     const ReadBufferAndDumpToStreamOptions &options) {
6167aa51b79SEd Maste   assert(options.GetStream() && "need a Stream to print the string to");
6177aa51b79SEd Maste 
618435933ddSDimitry Andric   return DumpUTFBufferToStream<llvm::UTF8>(nullptr, options);
6197aa51b79SEd Maste }
6207aa51b79SEd Maste 
6217aa51b79SEd Maste template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)622435933ddSDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<
623435933ddSDimitry Andric     StringPrinter::StringElementType::ASCII>(
624435933ddSDimitry Andric     const ReadBufferAndDumpToStreamOptions &options) {
6257aa51b79SEd Maste   // treat ASCII the same as UTF8
6267aa51b79SEd Maste   // FIXME: can we optimize ASCII some more?
6277aa51b79SEd Maste   return ReadBufferAndDumpToStream<StringElementType::UTF8>(options);
6287aa51b79SEd Maste }
6297aa51b79SEd Maste 
6307aa51b79SEd Maste template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)631435933ddSDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<
632435933ddSDimitry Andric     StringPrinter::StringElementType::UTF16>(
633435933ddSDimitry Andric     const ReadBufferAndDumpToStreamOptions &options) {
6347aa51b79SEd Maste   assert(options.GetStream() && "need a Stream to print the string to");
6357aa51b79SEd Maste 
636435933ddSDimitry Andric   return DumpUTFBufferToStream(llvm::ConvertUTF16toUTF8, options);
6377aa51b79SEd Maste }
6387aa51b79SEd Maste 
6397aa51b79SEd Maste template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)640435933ddSDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<
641435933ddSDimitry Andric     StringPrinter::StringElementType::UTF32>(
642435933ddSDimitry Andric     const ReadBufferAndDumpToStreamOptions &options) {
6437aa51b79SEd Maste   assert(options.GetStream() && "need a Stream to print the string to");
6447aa51b79SEd Maste 
645435933ddSDimitry Andric   return DumpUTFBufferToStream(llvm::ConvertUTF32toUTF8, options);
6467aa51b79SEd Maste }
6477aa51b79SEd Maste 
6487aa51b79SEd Maste } // namespace formatters
6497aa51b79SEd Maste 
6507aa51b79SEd Maste } // namespace lldb_private
651