1 //===-- StringPrinter.cpp ----------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "lldb/DataFormatters/StringPrinter.h"
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/ValueObject.h"
15 #include "lldb/Target/Language.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/Status.h"
20 #include "llvm/Support/ConvertUTF.h"
26 using namespace lldb_private;
27 using namespace lldb_private::formatters;
29 // we define this for all values of type but only implement it for those we
30 // care about that's good because we get linker errors for any unsupported type
31 template <lldb_private::formatters::StringPrinter::StringElementType type>
32 static StringPrinter::StringPrinterBufferPointer<>
33 GetPrintableImpl(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next);
35 // mimic isprint() for Unicode codepoints
36 static bool isprint(char32_t codepoint) {
37 if (codepoint <= 0x1F || codepoint == 0x7F) // C0
41 if (codepoint >= 0x80 && codepoint <= 0x9F) // C1
45 if (codepoint == 0x2028 || codepoint == 0x2029) // line/paragraph separators
49 if (codepoint == 0x200E || codepoint == 0x200F ||
50 (codepoint >= 0x202A &&
51 codepoint <= 0x202E)) // bidirectional text control
55 if (codepoint >= 0xFFF9 &&
56 codepoint <= 0xFFFF) // interlinears and generally specials
64 StringPrinter::StringPrinterBufferPointer<>
65 GetPrintableImpl<StringPrinter::StringElementType::ASCII>(uint8_t *buffer,
68 StringPrinter::StringPrinterBufferPointer<> retval = {nullptr};
102 if (isprint(*buffer))
103 retval = {buffer, 1};
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; }};
116 static char32_t ConvertUTF8ToCodePoint(unsigned char c0, unsigned char c1) {
117 return (c0 - 192) * 64 + (c1 - 128);
119 static char32_t ConvertUTF8ToCodePoint(unsigned char c0, unsigned char c1,
121 return (c0 - 224) * 4096 + (c1 - 128) * 64 + (c2 - 128);
123 static char32_t ConvertUTF8ToCodePoint(unsigned char c0, unsigned char c1,
124 unsigned char c2, unsigned char c3) {
125 return (c0 - 240) * 262144 + (c2 - 128) * 4096 + (c2 - 128) * 64 + (c3 - 128);
129 StringPrinter::StringPrinterBufferPointer<>
130 GetPrintableImpl<StringPrinter::StringElementType::UTF8>(uint8_t *buffer,
133 StringPrinter::StringPrinterBufferPointer<> retval{nullptr};
135 unsigned utf8_encoded_len = llvm::getNumBytesForUTF8(*buffer);
137 if (1u + std::distance(buffer, buffer_end) < utf8_encoded_len) {
138 // I don't have enough bytes - print whatever I have left
139 retval = {buffer, static_cast<size_t>(1 + buffer_end - buffer)};
140 next = buffer_end + 1;
144 char32_t codepoint = 0;
145 switch (utf8_encoded_len) {
147 // this is just an ASCII byte - ask ASCII
148 return GetPrintableImpl<StringPrinter::StringElementType::ASCII>(
149 buffer, buffer_end, next);
151 codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer,
152 (unsigned char)*(buffer + 1));
155 codepoint = ConvertUTF8ToCodePoint((unsigned char)*buffer,
156 (unsigned char)*(buffer + 1),
157 (unsigned char)*(buffer + 2));
160 codepoint = ConvertUTF8ToCodePoint(
161 (unsigned char)*buffer, (unsigned char)*(buffer + 1),
162 (unsigned char)*(buffer + 2), (unsigned char)*(buffer + 3));
165 // this is probably some bogus non-character thing just print it as-is and
166 // hope to sync up again soon
167 retval = {buffer, 1};
199 retval = {"\\\"", 2};
202 retval = {"\\\\", 2};
205 if (isprint(codepoint))
206 retval = {buffer, utf8_encoded_len};
208 uint8_t *data = new uint8_t[11];
209 sprintf((char *)data, "\\U%08x", (unsigned)codepoint);
210 retval = {data, 10, [](const uint8_t *c) { delete[] c; }};
215 next = buffer + utf8_encoded_len;
219 // this should not happen - but just in case.. try to resync at some point
220 retval = {buffer, 1};
225 // Given a sequence of bytes, this function returns: a sequence of bytes to
226 // actually print out + a length the following unscanned position of the buffer
228 static StringPrinter::StringPrinterBufferPointer<>
229 GetPrintable(StringPrinter::StringElementType type, uint8_t *buffer,
230 uint8_t *buffer_end, uint8_t *&next) {
235 case StringPrinter::StringElementType::ASCII:
236 return GetPrintableImpl<StringPrinter::StringElementType::ASCII>(
237 buffer, buffer_end, next);
238 case StringPrinter::StringElementType::UTF8:
239 return GetPrintableImpl<StringPrinter::StringElementType::UTF8>(
240 buffer, buffer_end, next);
246 StringPrinter::EscapingHelper
247 StringPrinter::GetDefaultEscapingHelper(GetPrintableElementType elem_type) {
249 case GetPrintableElementType::UTF8:
250 return [](uint8_t *buffer, uint8_t *buffer_end,
251 uint8_t *&next) -> StringPrinter::StringPrinterBufferPointer<> {
252 return GetPrintable(StringPrinter::StringElementType::UTF8, buffer,
255 case GetPrintableElementType::ASCII:
256 return [](uint8_t *buffer, uint8_t *buffer_end,
257 uint8_t *&next) -> StringPrinter::StringPrinterBufferPointer<> {
258 return GetPrintable(StringPrinter::StringElementType::ASCII, buffer,
262 llvm_unreachable("bad element type");
265 // use this call if you already have an LLDB-side buffer for the data
266 template <typename SourceDataType>
267 static bool DumpUTFBufferToStream(
268 llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
269 const SourceDataType *,
270 llvm::UTF8 **, llvm::UTF8 *,
271 llvm::ConversionFlags),
272 const StringPrinter::ReadBufferAndDumpToStreamOptions &dump_options) {
273 Stream &stream(*dump_options.GetStream());
274 if (dump_options.GetPrefixToken() != 0)
275 stream.Printf("%s", dump_options.GetPrefixToken());
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());
280 if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) {
281 const int bufferSPSize = data.GetByteSize();
282 if (dump_options.GetSourceSize() == 0) {
283 const int origin_encoding = 8 * sizeof(SourceDataType);
284 source_size = bufferSPSize / (origin_encoding / 4);
287 const SourceDataType *data_ptr =
288 (const SourceDataType *)data.GetDataStart();
289 const SourceDataType *data_end_ptr = data_ptr + source_size;
291 const bool zero_is_terminator = dump_options.GetBinaryZeroIsTerminator();
293 if (zero_is_terminator) {
294 while (data_ptr < data_end_ptr) {
296 data_end_ptr = data_ptr;
302 data_ptr = (const SourceDataType *)data.GetDataStart();
305 lldb::DataBufferSP utf8_data_buffer_sp;
306 llvm::UTF8 *utf8_data_ptr = nullptr;
307 llvm::UTF8 *utf8_data_end_ptr = nullptr;
309 if (ConvertFunction) {
310 utf8_data_buffer_sp.reset(new DataBufferHeap(4 * bufferSPSize, 0));
311 utf8_data_ptr = (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
312 utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
313 ConvertFunction(&data_ptr, data_end_ptr, &utf8_data_ptr,
314 utf8_data_end_ptr, llvm::lenientConversion);
315 if (!zero_is_terminator)
316 utf8_data_end_ptr = utf8_data_ptr;
317 // needed because the ConvertFunction will change the value of the
320 (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
322 // just copy the pointers - the cast is necessary to make the compiler
323 // happy but this should only happen if we are reading UTF8 data
324 utf8_data_ptr = const_cast<llvm::UTF8 *>(
325 reinterpret_cast<const llvm::UTF8 *>(data_ptr));
326 utf8_data_end_ptr = const_cast<llvm::UTF8 *>(
327 reinterpret_cast<const llvm::UTF8 *>(data_end_ptr));
330 const bool escape_non_printables = dump_options.GetEscapeNonPrintables();
331 lldb_private::formatters::StringPrinter::EscapingHelper escaping_callback;
332 if (escape_non_printables) {
333 if (Language *language = Language::FindPlugin(dump_options.GetLanguage()))
334 escaping_callback = language->GetStringPrinterEscapingHelper(
335 lldb_private::formatters::StringPrinter::GetPrintableElementType::
339 lldb_private::formatters::StringPrinter::GetDefaultEscapingHelper(
340 lldb_private::formatters::StringPrinter::
341 GetPrintableElementType::UTF8);
344 // since we tend to accept partial data (and even partially malformed data)
345 // we might end up with no NULL terminator before the end_ptr hence we need
346 // to take a slower route and ensure we stay within boundaries
347 for (; utf8_data_ptr < utf8_data_end_ptr;) {
348 if (zero_is_terminator && !*utf8_data_ptr)
351 if (escape_non_printables) {
352 uint8_t *next_data = nullptr;
354 escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data);
355 auto printable_bytes = printable.GetBytes();
356 auto printable_size = printable.GetSize();
357 if (!printable_bytes || !next_data) {
358 // GetPrintable() failed on us - print one byte in a desperate resync
360 printable_bytes = utf8_data_ptr;
362 next_data = utf8_data_ptr + 1;
364 for (unsigned c = 0; c < printable_size; c++)
365 stream.Printf("%c", *(printable_bytes + c));
366 utf8_data_ptr = (uint8_t *)next_data;
368 stream.Printf("%c", *utf8_data_ptr);
373 if (dump_options.GetQuote() != 0)
374 stream.Printf("%c", dump_options.GetQuote());
375 if (dump_options.GetSuffixToken() != 0)
376 stream.Printf("%s", dump_options.GetSuffixToken());
377 if (dump_options.GetIsTruncated())
378 stream.Printf("...");
382 lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::
383 ReadStringAndDumpToStreamOptions(ValueObject &valobj)
384 : ReadStringAndDumpToStreamOptions() {
385 SetEscapeNonPrintables(
386 valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
389 lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
390 ReadBufferAndDumpToStreamOptions(ValueObject &valobj)
391 : ReadBufferAndDumpToStreamOptions() {
392 SetEscapeNonPrintables(
393 valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
396 lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
397 ReadBufferAndDumpToStreamOptions(
398 const ReadStringAndDumpToStreamOptions &options)
399 : ReadBufferAndDumpToStreamOptions() {
400 SetStream(options.GetStream());
401 SetPrefixToken(options.GetPrefixToken());
402 SetSuffixToken(options.GetSuffixToken());
403 SetQuote(options.GetQuote());
404 SetEscapeNonPrintables(options.GetEscapeNonPrintables());
405 SetBinaryZeroIsTerminator(options.GetBinaryZeroIsTerminator());
406 SetLanguage(options.GetLanguage());
409 namespace lldb_private {
411 namespace formatters {
414 bool StringPrinter::ReadStringAndDumpToStream<
415 StringPrinter::StringElementType::ASCII>(
416 const ReadStringAndDumpToStreamOptions &options) {
417 assert(options.GetStream() && "need a Stream to print the string to");
420 ProcessSP process_sp(options.GetProcessSP());
422 if (process_sp.get() == nullptr || options.GetLocation() == 0)
426 const auto max_size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
427 bool is_truncated = false;
429 if (options.GetSourceSize() == 0)
431 else if (!options.GetIgnoreMaxLength()) {
432 size = options.GetSourceSize();
433 if (size > max_size) {
438 size = options.GetSourceSize();
440 lldb::DataBufferSP buffer_sp(new DataBufferHeap(size, 0));
442 process_sp->ReadCStringFromMemory(
443 options.GetLocation(), (char *)buffer_sp->GetBytes(), size, my_error);
448 const char *prefix_token = options.GetPrefixToken();
449 char quote = options.GetQuote();
451 if (prefix_token != 0)
452 options.GetStream()->Printf("%s%c", prefix_token, quote);
454 options.GetStream()->Printf("%c", quote);
456 uint8_t *data_end = buffer_sp->GetBytes() + buffer_sp->GetByteSize();
458 const bool escape_non_printables = options.GetEscapeNonPrintables();
459 lldb_private::formatters::StringPrinter::EscapingHelper escaping_callback;
460 if (escape_non_printables) {
461 if (Language *language = Language::FindPlugin(options.GetLanguage()))
462 escaping_callback = language->GetStringPrinterEscapingHelper(
463 lldb_private::formatters::StringPrinter::GetPrintableElementType::
467 lldb_private::formatters::StringPrinter::GetDefaultEscapingHelper(
468 lldb_private::formatters::StringPrinter::GetPrintableElementType::
472 // since we tend to accept partial data (and even partially malformed data)
473 // we might end up with no NULL terminator before the end_ptr hence we need
474 // to take a slower route and ensure we stay within boundaries
475 for (uint8_t *data = buffer_sp->GetBytes(); *data && (data < data_end);) {
476 if (escape_non_printables) {
477 uint8_t *next_data = nullptr;
478 auto printable = escaping_callback(data, data_end, next_data);
479 auto printable_bytes = printable.GetBytes();
480 auto printable_size = printable.GetSize();
481 if (!printable_bytes || !next_data) {
482 // GetPrintable() failed on us - print one byte in a desperate resync
484 printable_bytes = data;
486 next_data = data + 1;
488 for (unsigned c = 0; c < printable_size; c++)
489 options.GetStream()->Printf("%c", *(printable_bytes + c));
490 data = (uint8_t *)next_data;
492 options.GetStream()->Printf("%c", *data);
497 const char *suffix_token = options.GetSuffixToken();
499 if (suffix_token != 0)
500 options.GetStream()->Printf("%c%s", quote, suffix_token);
502 options.GetStream()->Printf("%c", quote);
505 options.GetStream()->Printf("...");
510 template <typename SourceDataType>
511 static bool ReadUTFBufferAndDumpToStream(
512 const StringPrinter::ReadStringAndDumpToStreamOptions &options,
513 llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
514 const SourceDataType *,
515 llvm::UTF8 **, llvm::UTF8 *,
516 llvm::ConversionFlags)) {
517 assert(options.GetStream() && "need a Stream to print the string to");
519 if (options.GetLocation() == 0 ||
520 options.GetLocation() == LLDB_INVALID_ADDRESS)
523 lldb::ProcessSP process_sp(options.GetProcessSP());
528 const int type_width = sizeof(SourceDataType);
529 const int origin_encoding = 8 * type_width;
530 if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
532 // if not UTF8, I need a conversion function to return proper UTF8
533 if (origin_encoding != 8 && !ConvertFunction)
536 if (!options.GetStream())
539 uint32_t sourceSize = options.GetSourceSize();
540 bool needs_zero_terminator = options.GetNeedsZeroTermination();
542 bool is_truncated = false;
543 const auto max_size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
546 sourceSize = max_size;
547 needs_zero_terminator = true;
548 } else if (!options.GetIgnoreMaxLength()) {
549 if (sourceSize > max_size) {
550 sourceSize = max_size;
555 const int bufferSPSize = sourceSize * type_width;
557 lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize, 0));
559 if (!buffer_sp->GetBytes())
563 char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
565 if (needs_zero_terminator)
566 process_sp->ReadStringFromMemory(options.GetLocation(), buffer,
567 bufferSPSize, error, type_width);
569 process_sp->ReadMemoryFromInferior(options.GetLocation(),
570 (char *)buffer_sp->GetBytes(),
571 bufferSPSize, error);
574 options.GetStream()->Printf("unable to read data");
578 DataExtractor data(buffer_sp, process_sp->GetByteOrder(),
579 process_sp->GetAddressByteSize());
581 StringPrinter::ReadBufferAndDumpToStreamOptions dump_options(options);
582 dump_options.SetData(data);
583 dump_options.SetSourceSize(sourceSize);
584 dump_options.SetIsTruncated(is_truncated);
586 return DumpUTFBufferToStream(ConvertFunction, dump_options);
590 bool StringPrinter::ReadStringAndDumpToStream<
591 StringPrinter::StringElementType::UTF8>(
592 const ReadStringAndDumpToStreamOptions &options) {
593 return ReadUTFBufferAndDumpToStream<llvm::UTF8>(options, nullptr);
597 bool StringPrinter::ReadStringAndDumpToStream<
598 StringPrinter::StringElementType::UTF16>(
599 const ReadStringAndDumpToStreamOptions &options) {
600 return ReadUTFBufferAndDumpToStream<llvm::UTF16>(options,
601 llvm::ConvertUTF16toUTF8);
605 bool StringPrinter::ReadStringAndDumpToStream<
606 StringPrinter::StringElementType::UTF32>(
607 const ReadStringAndDumpToStreamOptions &options) {
608 return ReadUTFBufferAndDumpToStream<llvm::UTF32>(options,
609 llvm::ConvertUTF32toUTF8);
613 bool StringPrinter::ReadBufferAndDumpToStream<
614 StringPrinter::StringElementType::UTF8>(
615 const ReadBufferAndDumpToStreamOptions &options) {
616 assert(options.GetStream() && "need a Stream to print the string to");
618 return DumpUTFBufferToStream<llvm::UTF8>(nullptr, options);
622 bool StringPrinter::ReadBufferAndDumpToStream<
623 StringPrinter::StringElementType::ASCII>(
624 const ReadBufferAndDumpToStreamOptions &options) {
625 // treat ASCII the same as UTF8
626 // FIXME: can we optimize ASCII some more?
627 return ReadBufferAndDumpToStream<StringElementType::UTF8>(options);
631 bool StringPrinter::ReadBufferAndDumpToStream<
632 StringPrinter::StringElementType::UTF16>(
633 const ReadBufferAndDumpToStreamOptions &options) {
634 assert(options.GetStream() && "need a Stream to print the string to");
636 return DumpUTFBufferToStream(llvm::ConvertUTF16toUTF8, options);
640 bool StringPrinter::ReadBufferAndDumpToStream<
641 StringPrinter::StringElementType::UTF32>(
642 const ReadBufferAndDumpToStreamOptions &options) {
643 assert(options.GetStream() && "need a Stream to print the string to");
645 return DumpUTFBufferToStream(llvm::ConvertUTF32toUTF8, options);
648 } // namespace formatters
650 } // namespace lldb_private