Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 1 | //===-- StringPrinter.cpp ----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/DataFormatters/StringPrinter.h" |
| 11 | |
| 12 | #include "lldb/Core/DataExtractor.h" |
Enrico Granata | ebdc1ac | 2014-11-05 21:20:48 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Debugger.h" |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Error.h" |
Enrico Granata | ebdc1ac | 2014-11-05 21:20:48 +0000 | [diff] [blame] | 15 | #include "lldb/Core/ValueObject.h" |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Language.h" |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Target/Target.h" |
| 19 | |
| 20 | #include "llvm/Support/ConvertUTF.h" |
| 21 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 23 | #include <locale> |
| 24 | |
| 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | using namespace lldb_private::formatters; |
| 28 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 29 | // we define this for all values of type but only implement it for those we care about |
| 30 | // that's good because we get linker errors for any unsupported type |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 31 | template <lldb_private::formatters::StringPrinter::StringElementType type> |
Enrico Granata | ad650a1 | 2015-09-09 20:59:49 +0000 | [diff] [blame] | 32 | static StringPrinter::StringPrinterBufferPointer<> |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 33 | GetPrintableImpl(uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next); |
| 34 | |
| 35 | // mimic isprint() for Unicode codepoints |
| 36 | static bool |
| 37 | isprint(char32_t codepoint) |
| 38 | { |
| 39 | if (codepoint <= 0x1F || codepoint == 0x7F) // C0 |
| 40 | { |
| 41 | return false; |
| 42 | } |
| 43 | if (codepoint >= 0x80 && codepoint <= 0x9F) // C1 |
| 44 | { |
| 45 | return false; |
| 46 | } |
| 47 | if (codepoint == 0x2028 || codepoint == 0x2029) // line/paragraph separators |
| 48 | { |
| 49 | return false; |
| 50 | } |
| 51 | if (codepoint == 0x200E || codepoint == 0x200F || (codepoint >= 0x202A && codepoint <= 0x202E)) // bidirectional text control |
| 52 | { |
| 53 | return false; |
| 54 | } |
| 55 | if (codepoint >= 0xFFF9 && codepoint <= 0xFFFF) // interlinears and generally specials |
| 56 | { |
| 57 | return false; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | template <> |
Enrico Granata | ad650a1 | 2015-09-09 20:59:49 +0000 | [diff] [blame] | 63 | StringPrinter::StringPrinterBufferPointer<> |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 64 | GetPrintableImpl<StringPrinter::StringElementType::ASCII> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 65 | { |
Enrico Granata | ad650a1 | 2015-09-09 20:59:49 +0000 | [diff] [blame] | 66 | StringPrinter::StringPrinterBufferPointer<> retval = {nullptr}; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 67 | |
| 68 | switch (*buffer) |
| 69 | { |
Enrico Granata | da04fbb | 2014-12-18 19:43:29 +0000 | [diff] [blame] | 70 | case 0: |
| 71 | retval = {"\\0",2}; |
| 72 | break; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 73 | case '\a': |
| 74 | retval = {"\\a",2}; |
| 75 | break; |
| 76 | case '\b': |
| 77 | retval = {"\\b",2}; |
| 78 | break; |
| 79 | case '\f': |
| 80 | retval = {"\\f",2}; |
| 81 | break; |
| 82 | case '\n': |
| 83 | retval = {"\\n",2}; |
| 84 | break; |
| 85 | case '\r': |
| 86 | retval = {"\\r",2}; |
| 87 | break; |
| 88 | case '\t': |
| 89 | retval = {"\\t",2}; |
| 90 | break; |
| 91 | case '\v': |
| 92 | retval = {"\\v",2}; |
| 93 | break; |
| 94 | case '\"': |
| 95 | retval = {"\\\"",2}; |
| 96 | break; |
| 97 | case '\\': |
| 98 | retval = {"\\\\",2}; |
| 99 | break; |
| 100 | default: |
| 101 | if (isprint(*buffer)) |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 102 | retval = {buffer,1}; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 103 | else |
| 104 | { |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 105 | uint8_t* data = new uint8_t[5]; |
| 106 | sprintf((char*)data,"\\x%02x",*buffer); |
| 107 | retval = {data, 4, [] (const uint8_t* c) {delete[] c;} }; |
| 108 | break; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | next = buffer + 1; |
| 113 | return retval; |
| 114 | } |
| 115 | |
| 116 | static char32_t |
| 117 | ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1) |
| 118 | { |
| 119 | return (c0-192)*64+(c1-128); |
| 120 | } |
| 121 | static char32_t |
| 122 | ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1, unsigned char c2) |
| 123 | { |
| 124 | return (c0-224)*4096+(c1-128)*64+(c2-128); |
| 125 | } |
| 126 | static char32_t |
| 127 | ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1, unsigned char c2, unsigned char c3) |
| 128 | { |
| 129 | return (c0-240)*262144+(c2-128)*4096+(c2-128)*64+(c3-128); |
| 130 | } |
| 131 | |
| 132 | template <> |
Enrico Granata | ad650a1 | 2015-09-09 20:59:49 +0000 | [diff] [blame] | 133 | StringPrinter::StringPrinterBufferPointer<> |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 134 | GetPrintableImpl<StringPrinter::StringElementType::UTF8> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 135 | { |
Enrico Granata | ad650a1 | 2015-09-09 20:59:49 +0000 | [diff] [blame] | 136 | StringPrinter::StringPrinterBufferPointer<> retval {nullptr}; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 137 | |
| 138 | unsigned utf8_encoded_len = getNumBytesForUTF8(*buffer); |
| 139 | |
| 140 | if (1+buffer_end-buffer < utf8_encoded_len) |
| 141 | { |
| 142 | // I don't have enough bytes - print whatever I have left |
| 143 | retval = {buffer,static_cast<size_t>(1+buffer_end-buffer)}; |
| 144 | next = buffer_end+1; |
| 145 | return retval; |
| 146 | } |
| 147 | |
| 148 | char32_t codepoint = 0; |
| 149 | switch (utf8_encoded_len) |
| 150 | { |
| 151 | case 1: |
| 152 | // this is just an ASCII byte - ask ASCII |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 153 | return GetPrintableImpl<StringPrinter::StringElementType::ASCII>(buffer, buffer_end, next); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 154 | case 2: |
| 155 | codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer, (unsigned char)*(buffer+1)); |
| 156 | break; |
| 157 | case 3: |
| 158 | codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer, (unsigned char)*(buffer+1), (unsigned char)*(buffer+2)); |
| 159 | break; |
| 160 | case 4: |
| 161 | codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer, (unsigned char)*(buffer+1), (unsigned char)*(buffer+2), (unsigned char)*(buffer+3)); |
| 162 | break; |
| 163 | default: |
| 164 | // this is probably some bogus non-character thing |
| 165 | // just print it as-is and hope to sync up again soon |
| 166 | retval = {buffer,1}; |
| 167 | next = buffer+1; |
| 168 | return retval; |
| 169 | } |
| 170 | |
| 171 | if (codepoint) |
| 172 | { |
| 173 | switch (codepoint) |
| 174 | { |
Enrico Granata | da04fbb | 2014-12-18 19:43:29 +0000 | [diff] [blame] | 175 | case 0: |
| 176 | retval = {"\\0",2}; |
| 177 | break; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 178 | case '\a': |
| 179 | retval = {"\\a",2}; |
| 180 | break; |
| 181 | case '\b': |
| 182 | retval = {"\\b",2}; |
| 183 | break; |
| 184 | case '\f': |
| 185 | retval = {"\\f",2}; |
| 186 | break; |
| 187 | case '\n': |
| 188 | retval = {"\\n",2}; |
| 189 | break; |
| 190 | case '\r': |
| 191 | retval = {"\\r",2}; |
| 192 | break; |
| 193 | case '\t': |
| 194 | retval = {"\\t",2}; |
| 195 | break; |
| 196 | case '\v': |
| 197 | retval = {"\\v",2}; |
| 198 | break; |
| 199 | case '\"': |
| 200 | retval = {"\\\"",2}; |
| 201 | break; |
| 202 | case '\\': |
| 203 | retval = {"\\\\",2}; |
| 204 | break; |
| 205 | default: |
| 206 | if (isprint(codepoint)) |
| 207 | retval = {buffer,utf8_encoded_len}; |
| 208 | else |
| 209 | { |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 210 | uint8_t* data = new uint8_t[11]; |
| 211 | sprintf((char*)data,"\\U%08x",codepoint); |
| 212 | retval = { data,10,[] (const uint8_t* c) {delete[] c;} }; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | next = buffer + utf8_encoded_len; |
| 218 | return retval; |
| 219 | } |
| 220 | |
| 221 | // this should not happen - but just in case.. try to resync at some point |
| 222 | retval = {buffer,1}; |
| 223 | next = buffer+1; |
| 224 | return retval; |
| 225 | } |
| 226 | |
| 227 | // Given a sequence of bytes, this function returns: |
| 228 | // a sequence of bytes to actually print out + a length |
| 229 | // the following unscanned position of the buffer is in next |
Enrico Granata | ad650a1 | 2015-09-09 20:59:49 +0000 | [diff] [blame] | 230 | static StringPrinter::StringPrinterBufferPointer<> |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 231 | GetPrintable(StringPrinter::StringElementType type, uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 232 | { |
| 233 | if (!buffer) |
| 234 | return {nullptr}; |
| 235 | |
| 236 | switch (type) |
| 237 | { |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 238 | case StringPrinter::StringElementType::ASCII: |
| 239 | return GetPrintableImpl<StringPrinter::StringElementType::ASCII>(buffer, buffer_end, next); |
| 240 | case StringPrinter::StringElementType::UTF8: |
| 241 | return GetPrintableImpl<StringPrinter::StringElementType::UTF8>(buffer, buffer_end, next); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 242 | default: |
| 243 | return {nullptr}; |
| 244 | } |
| 245 | } |
| 246 | |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 247 | StringPrinter::EscapingHelper |
| 248 | StringPrinter::GetDefaultEscapingHelper (GetPrintableElementType elem_type) |
| 249 | { |
| 250 | switch (elem_type) |
| 251 | { |
| 252 | case GetPrintableElementType::UTF8: |
| 253 | return [] (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next) -> StringPrinter::StringPrinterBufferPointer<> { |
| 254 | return GetPrintable(StringPrinter::StringElementType::UTF8, buffer, buffer_end, next); |
| 255 | }; |
| 256 | case GetPrintableElementType::ASCII: |
| 257 | return [] (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next) -> StringPrinter::StringPrinterBufferPointer<> { |
| 258 | return GetPrintable(StringPrinter::StringElementType::ASCII, buffer, buffer_end, next); |
| 259 | }; |
| 260 | } |
| 261 | } |
| 262 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 263 | // use this call if you already have an LLDB-side buffer for the data |
| 264 | template<typename SourceDataType> |
| 265 | static bool |
| 266 | DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**, |
| 267 | const SourceDataType*, |
| 268 | UTF8**, |
| 269 | UTF8*, |
| 270 | ConversionFlags), |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 271 | const StringPrinter::ReadBufferAndDumpToStreamOptions& dump_options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 272 | { |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 273 | Stream &stream(*dump_options.GetStream()); |
| 274 | if (dump_options.GetPrefixToken() != 0) |
Enrico Granata | d54f7fb | 2015-10-07 02:06:48 +0000 | [diff] [blame] | 275 | stream.Printf("%s",dump_options.GetPrefixToken()); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 276 | if (dump_options.GetQuote() != 0) |
| 277 | stream.Printf("%c",dump_options.GetQuote()); |
| 278 | auto data(dump_options.GetData()); |
| 279 | auto source_size(dump_options.GetSourceSize()); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 280 | if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) |
| 281 | { |
| 282 | const int bufferSPSize = data.GetByteSize(); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 283 | if (dump_options.GetSourceSize() == 0) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 284 | { |
| 285 | const int origin_encoding = 8*sizeof(SourceDataType); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 286 | source_size = bufferSPSize/(origin_encoding / 4); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 289 | const SourceDataType *data_ptr = (const SourceDataType*)data.GetDataStart(); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 290 | const SourceDataType *data_end_ptr = data_ptr + source_size; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 291 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 292 | const bool zero_is_terminator = dump_options.GetBinaryZeroIsTerminator(); |
| 293 | |
| 294 | if (zero_is_terminator) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 295 | { |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 296 | while (data_ptr < data_end_ptr) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 297 | { |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 298 | if (!*data_ptr) |
| 299 | { |
| 300 | data_end_ptr = data_ptr; |
| 301 | break; |
| 302 | } |
| 303 | data_ptr++; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 304 | } |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 305 | |
| 306 | data_ptr = (const SourceDataType*)data.GetDataStart(); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 309 | lldb::DataBufferSP utf8_data_buffer_sp; |
| 310 | UTF8* utf8_data_ptr = nullptr; |
| 311 | UTF8* utf8_data_end_ptr = nullptr; |
| 312 | |
| 313 | if (ConvertFunction) |
| 314 | { |
| 315 | utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0)); |
| 316 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); |
| 317 | utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize(); |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 318 | ConvertFunction ( &data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion ); |
Enrico Granata | 8101f57 | 2015-07-17 01:56:25 +0000 | [diff] [blame] | 319 | if (false == zero_is_terminator) |
| 320 | utf8_data_end_ptr = utf8_data_ptr; |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 321 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | // just copy the pointers - the cast is necessary to make the compiler happy |
| 326 | // but this should only happen if we are reading UTF8 data |
Saleem Abdulrasool | ba507b0 | 2015-10-18 19:34:38 +0000 | [diff] [blame] | 327 | utf8_data_ptr = const_cast<UTF8 *>(reinterpret_cast<const UTF8*>(data_ptr)); |
| 328 | utf8_data_end_ptr = const_cast<UTF8 *>(reinterpret_cast<const UTF8*>(data_end_ptr)); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 331 | const bool escape_non_printables = dump_options.GetEscapeNonPrintables(); |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 332 | lldb_private::formatters::StringPrinter::EscapingHelper escaping_callback; |
| 333 | if (escape_non_printables) |
| 334 | { |
| 335 | if (Language *language = Language::FindPlugin(dump_options.GetLanguage())) |
| 336 | escaping_callback = language->GetStringPrinterEscapingHelper(lldb_private::formatters::StringPrinter::GetPrintableElementType::UTF8); |
| 337 | else |
| 338 | escaping_callback = lldb_private::formatters::StringPrinter::GetDefaultEscapingHelper(lldb_private::formatters::StringPrinter::GetPrintableElementType::UTF8); |
| 339 | } |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 340 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 341 | // since we tend to accept partial data (and even partially malformed data) |
| 342 | // we might end up with no NULL terminator before the end_ptr |
| 343 | // hence we need to take a slower route and ensure we stay within boundaries |
| 344 | for (;utf8_data_ptr < utf8_data_end_ptr;) |
| 345 | { |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 346 | if (zero_is_terminator && !*utf8_data_ptr) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 347 | break; |
| 348 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 349 | if (escape_non_printables) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 350 | { |
| 351 | uint8_t* next_data = nullptr; |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 352 | auto printable = escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 353 | auto printable_bytes = printable.GetBytes(); |
| 354 | auto printable_size = printable.GetSize(); |
| 355 | if (!printable_bytes || !next_data) |
| 356 | { |
| 357 | // GetPrintable() failed on us - print one byte in a desperate resync attempt |
| 358 | printable_bytes = utf8_data_ptr; |
| 359 | printable_size = 1; |
| 360 | next_data = utf8_data_ptr+1; |
| 361 | } |
Andy Gibbs | 3acfe1a | 2014-12-29 13:03:19 +0000 | [diff] [blame] | 362 | for (unsigned c = 0; c < printable_size; c++) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 363 | stream.Printf("%c", *(printable_bytes+c)); |
| 364 | utf8_data_ptr = (uint8_t*)next_data; |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | stream.Printf("%c",*utf8_data_ptr); |
| 369 | utf8_data_ptr++; |
| 370 | } |
| 371 | } |
| 372 | } |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 373 | if (dump_options.GetQuote() != 0) |
| 374 | stream.Printf("%c",dump_options.GetQuote()); |
Enrico Granata | d54f7fb | 2015-10-07 02:06:48 +0000 | [diff] [blame] | 375 | if (dump_options.GetSuffixToken() != 0) |
| 376 | stream.Printf("%s",dump_options.GetSuffixToken()); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 380 | lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::ReadStringAndDumpToStreamOptions (ValueObject& valobj) : |
Enrico Granata | ebdc1ac | 2014-11-05 21:20:48 +0000 | [diff] [blame] | 381 | ReadStringAndDumpToStreamOptions() |
| 382 | { |
| 383 | SetEscapeNonPrintables(valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables()); |
| 384 | } |
| 385 | |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 386 | lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::ReadBufferAndDumpToStreamOptions (ValueObject& valobj) : |
Enrico Granata | ebdc1ac | 2014-11-05 21:20:48 +0000 | [diff] [blame] | 387 | ReadBufferAndDumpToStreamOptions() |
| 388 | { |
| 389 | SetEscapeNonPrintables(valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables()); |
| 390 | } |
| 391 | |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 392 | lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::ReadBufferAndDumpToStreamOptions (const ReadStringAndDumpToStreamOptions& options) : |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 393 | ReadBufferAndDumpToStreamOptions() |
| 394 | { |
| 395 | SetStream(options.GetStream()); |
| 396 | SetPrefixToken(options.GetPrefixToken()); |
Enrico Granata | d54f7fb | 2015-10-07 02:06:48 +0000 | [diff] [blame] | 397 | SetSuffixToken(options.GetSuffixToken()); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 398 | SetQuote(options.GetQuote()); |
| 399 | SetEscapeNonPrintables(options.GetEscapeNonPrintables()); |
| 400 | SetBinaryZeroIsTerminator(options.GetBinaryZeroIsTerminator()); |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 401 | SetLanguage(options.GetLanguage()); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Enrico Granata | ebdc1ac | 2014-11-05 21:20:48 +0000 | [diff] [blame] | 404 | |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 405 | namespace lldb_private |
| 406 | { |
| 407 | |
| 408 | namespace formatters |
| 409 | { |
| 410 | |
| 411 | template <> |
| 412 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 413 | StringPrinter::ReadStringAndDumpToStream<StringPrinter::StringElementType::ASCII> (const ReadStringAndDumpToStreamOptions& options) |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 414 | { |
| 415 | assert(options.GetStream() && "need a Stream to print the string to"); |
| 416 | Error my_error; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 417 | |
| 418 | ProcessSP process_sp(options.GetProcessSP()); |
| 419 | |
| 420 | if (process_sp.get() == nullptr || options.GetLocation() == 0) |
| 421 | return false; |
| 422 | |
| 423 | size_t size; |
| 424 | |
| 425 | if (options.GetSourceSize() == 0) |
| 426 | size = process_sp->GetTarget().GetMaximumSizeOfStringSummary(); |
Enrico Granata | 3404221 | 2014-11-18 22:54:45 +0000 | [diff] [blame] | 427 | else if (!options.GetIgnoreMaxLength()) |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 428 | size = std::min(options.GetSourceSize(),process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
Enrico Granata | 3404221 | 2014-11-18 22:54:45 +0000 | [diff] [blame] | 429 | else |
| 430 | size = options.GetSourceSize(); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 431 | |
| 432 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0)); |
| 433 | |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 434 | process_sp->ReadCStringFromMemory(options.GetLocation(), (char*)buffer_sp->GetBytes(), size, my_error); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 435 | |
| 436 | if (my_error.Fail()) |
| 437 | return false; |
| 438 | |
Enrico Granata | d54f7fb | 2015-10-07 02:06:48 +0000 | [diff] [blame] | 439 | const char* prefix_token = options.GetPrefixToken(); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 440 | char quote = options.GetQuote(); |
| 441 | |
| 442 | if (prefix_token != 0) |
Enrico Granata | d54f7fb | 2015-10-07 02:06:48 +0000 | [diff] [blame] | 443 | options.GetStream()->Printf("%s%c",prefix_token,quote); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 444 | else if (quote != 0) |
| 445 | options.GetStream()->Printf("%c",quote); |
| 446 | |
| 447 | uint8_t* data_end = buffer_sp->GetBytes()+buffer_sp->GetByteSize(); |
| 448 | |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 449 | const bool escape_non_printables = options.GetEscapeNonPrintables(); |
| 450 | lldb_private::formatters::StringPrinter::EscapingHelper escaping_callback; |
| 451 | if (escape_non_printables) |
| 452 | { |
| 453 | if (Language *language = Language::FindPlugin(options.GetLanguage())) |
| 454 | escaping_callback = language->GetStringPrinterEscapingHelper(lldb_private::formatters::StringPrinter::GetPrintableElementType::ASCII); |
| 455 | else |
| 456 | escaping_callback = lldb_private::formatters::StringPrinter::GetDefaultEscapingHelper(lldb_private::formatters::StringPrinter::GetPrintableElementType::ASCII); |
| 457 | } |
| 458 | |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 459 | // since we tend to accept partial data (and even partially malformed data) |
| 460 | // we might end up with no NULL terminator before the end_ptr |
| 461 | // hence we need to take a slower route and ensure we stay within boundaries |
| 462 | for (uint8_t* data = buffer_sp->GetBytes(); *data && (data < data_end);) |
| 463 | { |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 464 | if (escape_non_printables) |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 465 | { |
| 466 | uint8_t* next_data = nullptr; |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 467 | auto printable = escaping_callback(data, data_end, next_data); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 468 | auto printable_bytes = printable.GetBytes(); |
| 469 | auto printable_size = printable.GetSize(); |
| 470 | if (!printable_bytes || !next_data) |
| 471 | { |
| 472 | // GetPrintable() failed on us - print one byte in a desperate resync attempt |
| 473 | printable_bytes = data; |
| 474 | printable_size = 1; |
| 475 | next_data = data+1; |
| 476 | } |
Andy Gibbs | 3acfe1a | 2014-12-29 13:03:19 +0000 | [diff] [blame] | 477 | for (unsigned c = 0; c < printable_size; c++) |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 478 | options.GetStream()->Printf("%c", *(printable_bytes+c)); |
| 479 | data = (uint8_t*)next_data; |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | options.GetStream()->Printf("%c",*data); |
| 484 | data++; |
| 485 | } |
| 486 | } |
Enrico Granata | d54f7fb | 2015-10-07 02:06:48 +0000 | [diff] [blame] | 487 | |
| 488 | const char* suffix_token = options.GetSuffixToken(); |
| 489 | |
| 490 | if (suffix_token != 0) |
| 491 | options.GetStream()->Printf("%c%s",quote, suffix_token); |
| 492 | else if (quote != 0) |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 493 | options.GetStream()->Printf("%c",quote); |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 498 | template<typename SourceDataType> |
| 499 | static bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 500 | ReadUTFBufferAndDumpToStream (const StringPrinter::ReadStringAndDumpToStreamOptions& options, |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 501 | ConversionResult (*ConvertFunction) (const SourceDataType**, |
| 502 | const SourceDataType*, |
| 503 | UTF8**, |
| 504 | UTF8*, |
| 505 | ConversionFlags)) |
| 506 | { |
| 507 | assert(options.GetStream() && "need a Stream to print the string to"); |
| 508 | |
| 509 | if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS) |
| 510 | return false; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 511 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 512 | lldb::ProcessSP process_sp(options.GetProcessSP()); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 513 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 514 | if (!process_sp) |
| 515 | return false; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 516 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 517 | const int type_width = sizeof(SourceDataType); |
| 518 | const int origin_encoding = 8 * type_width ; |
| 519 | if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32) |
| 520 | return false; |
| 521 | // if not UTF8, I need a conversion function to return proper UTF8 |
| 522 | if (origin_encoding != 8 && !ConvertFunction) |
| 523 | return false; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 524 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 525 | if (!options.GetStream()) |
| 526 | return false; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 527 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 528 | uint32_t sourceSize = options.GetSourceSize(); |
| 529 | bool needs_zero_terminator = options.GetNeedsZeroTermination(); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 530 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 531 | if (!sourceSize) |
| 532 | { |
| 533 | sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary(); |
| 534 | needs_zero_terminator = true; |
| 535 | } |
Enrico Granata | b0e8a55 | 2015-05-01 22:57:38 +0000 | [diff] [blame] | 536 | else if (!options.GetIgnoreMaxLength()) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 537 | sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 538 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 539 | const int bufferSPSize = sourceSize * type_width; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 540 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 541 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0)); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 542 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 543 | if (!buffer_sp->GetBytes()) |
| 544 | return false; |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 545 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 546 | Error error; |
| 547 | char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes()); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 548 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 549 | if (needs_zero_terminator) |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 550 | process_sp->ReadStringFromMemory(options.GetLocation(), buffer, bufferSPSize, error, type_width); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 551 | else |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 552 | process_sp->ReadMemoryFromInferior(options.GetLocation(), (char*)buffer_sp->GetBytes(), bufferSPSize, error); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 553 | |
Enrico Granata | 099263b | 2014-11-17 23:14:11 +0000 | [diff] [blame] | 554 | if (error.Fail()) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 555 | { |
| 556 | options.GetStream()->Printf("unable to read data"); |
| 557 | return true; |
| 558 | } |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 559 | |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 560 | DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize()); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 561 | |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 562 | StringPrinter::ReadBufferAndDumpToStreamOptions dump_options(options); |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 563 | dump_options.SetData(data); |
| 564 | dump_options.SetSourceSize(sourceSize); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 565 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 566 | return DumpUTFBufferToStream(ConvertFunction, dump_options); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | template <> |
| 570 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 571 | StringPrinter::ReadStringAndDumpToStream<StringPrinter::StringElementType::UTF8> (const ReadStringAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 572 | { |
| 573 | return ReadUTFBufferAndDumpToStream<UTF8>(options, |
| 574 | nullptr); |
| 575 | } |
| 576 | |
| 577 | template <> |
| 578 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 579 | StringPrinter::ReadStringAndDumpToStream<StringPrinter::StringElementType::UTF16> (const ReadStringAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 580 | { |
| 581 | return ReadUTFBufferAndDumpToStream<UTF16>(options, |
| 582 | ConvertUTF16toUTF8); |
| 583 | } |
| 584 | |
| 585 | template <> |
| 586 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 587 | StringPrinter::ReadStringAndDumpToStream<StringPrinter::StringElementType::UTF32> (const ReadStringAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 588 | { |
| 589 | return ReadUTFBufferAndDumpToStream<UTF32>(options, |
| 590 | ConvertUTF32toUTF8); |
| 591 | } |
| 592 | |
| 593 | template <> |
| 594 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 595 | StringPrinter::ReadBufferAndDumpToStream<StringPrinter::StringElementType::UTF8> (const ReadBufferAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 596 | { |
| 597 | assert(options.GetStream() && "need a Stream to print the string to"); |
| 598 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 599 | return DumpUTFBufferToStream<UTF8>(nullptr, options); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | template <> |
| 603 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 604 | StringPrinter::ReadBufferAndDumpToStream<StringPrinter::StringElementType::ASCII> (const ReadBufferAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 605 | { |
| 606 | // treat ASCII the same as UTF8 |
| 607 | // FIXME: can we optimize ASCII some more? |
| 608 | return ReadBufferAndDumpToStream<StringElementType::UTF8>(options); |
| 609 | } |
| 610 | |
| 611 | template <> |
| 612 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 613 | StringPrinter::ReadBufferAndDumpToStream<StringPrinter::StringElementType::UTF16> (const ReadBufferAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 614 | { |
| 615 | assert(options.GetStream() && "need a Stream to print the string to"); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 616 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 617 | return DumpUTFBufferToStream(ConvertUTF16toUTF8, options); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | template <> |
| 621 | bool |
Enrico Granata | ac49453 | 2015-09-09 22:30:24 +0000 | [diff] [blame] | 622 | StringPrinter::ReadBufferAndDumpToStream<StringPrinter::StringElementType::UTF32> (const ReadBufferAndDumpToStreamOptions& options) |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 623 | { |
| 624 | assert(options.GetStream() && "need a Stream to print the string to"); |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 625 | |
Enrico Granata | d07f755 | 2015-07-17 01:03:59 +0000 | [diff] [blame] | 626 | return DumpUTFBufferToStream(ConvertUTF32toUTF8, options); |
Enrico Granata | ca6c8ee | 2014-10-30 01:45:39 +0000 | [diff] [blame] | 627 | } |
Shawn Best | fd13743 | 2014-11-04 22:43:34 +0000 | [diff] [blame] | 628 | |
| 629 | } // namespace formatters |
| 630 | |
| 631 | } // namespace lldb_private |