1cfca06d7SDimitry Andric //===-- StringPrinter.cpp -------------------------------------------------===//
2205afe67SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6205afe67SEd Maste //
7205afe67SEd Maste //===----------------------------------------------------------------------===//
8205afe67SEd Maste
9205afe67SEd Maste #include "lldb/DataFormatters/StringPrinter.h"
10205afe67SEd Maste
11205afe67SEd Maste #include "lldb/Core/Debugger.h"
12205afe67SEd Maste #include "lldb/Core/ValueObject.h"
13e81d9d49SDimitry Andric #include "lldb/Target/Language.h"
14205afe67SEd Maste #include "lldb/Target/Process.h"
15205afe67SEd Maste #include "lldb/Target/Target.h"
16b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
17205afe67SEd Maste
18cfca06d7SDimitry Andric #include "llvm/ADT/StringExtras.h"
19205afe67SEd Maste #include "llvm/Support/ConvertUTF.h"
20205afe67SEd Maste
21344a3780SDimitry Andric #include <cctype>
22205afe67SEd Maste #include <locale>
235f29bb8aSDimitry Andric #include <memory>
24205afe67SEd Maste
25205afe67SEd Maste using namespace lldb;
26205afe67SEd Maste using namespace lldb_private;
27205afe67SEd Maste using namespace lldb_private::formatters;
28cfca06d7SDimitry Andric using GetPrintableElementType = StringPrinter::GetPrintableElementType;
29cfca06d7SDimitry Andric using StringElementType = StringPrinter::StringElementType;
30cfca06d7SDimitry Andric
31cfca06d7SDimitry Andric /// DecodedCharBuffer stores the decoded contents of a single character. It
32cfca06d7SDimitry Andric /// avoids managing memory on the heap by copying decoded bytes into an in-line
33cfca06d7SDimitry Andric /// buffer.
34cfca06d7SDimitry Andric class DecodedCharBuffer {
35cfca06d7SDimitry Andric public:
DecodedCharBuffer(std::nullptr_t)36cfca06d7SDimitry Andric DecodedCharBuffer(std::nullptr_t) {}
37cfca06d7SDimitry Andric
DecodedCharBuffer(const uint8_t * bytes,size_t size)38cfca06d7SDimitry Andric DecodedCharBuffer(const uint8_t *bytes, size_t size) : m_size(size) {
39cfca06d7SDimitry Andric if (size > MaxLength)
40cfca06d7SDimitry Andric llvm_unreachable("unsupported length");
41cfca06d7SDimitry Andric memcpy(m_data, bytes, size);
42cfca06d7SDimitry Andric }
43cfca06d7SDimitry Andric
DecodedCharBuffer(const char * bytes,size_t size)44cfca06d7SDimitry Andric DecodedCharBuffer(const char *bytes, size_t size)
45cfca06d7SDimitry Andric : DecodedCharBuffer(reinterpret_cast<const uint8_t *>(bytes), size) {}
46cfca06d7SDimitry Andric
GetBytes() const47cfca06d7SDimitry Andric const uint8_t *GetBytes() const { return m_data; }
48cfca06d7SDimitry Andric
GetSize() const49cfca06d7SDimitry Andric size_t GetSize() const { return m_size; }
50cfca06d7SDimitry Andric
51cfca06d7SDimitry Andric private:
52cfca06d7SDimitry Andric static constexpr unsigned MaxLength = 16;
53cfca06d7SDimitry Andric
54cfca06d7SDimitry Andric size_t m_size = 0;
55cfca06d7SDimitry Andric uint8_t m_data[MaxLength] = {0};
56cfca06d7SDimitry Andric };
57cfca06d7SDimitry Andric
58cfca06d7SDimitry Andric using EscapingHelper =
59cfca06d7SDimitry Andric std::function<DecodedCharBuffer(uint8_t *, uint8_t *, uint8_t *&)>;
60205afe67SEd Maste
61f73363f1SDimitry Andric // we define this for all values of type but only implement it for those we
62f73363f1SDimitry Andric // care about that's good because we get linker errors for any unsupported type
63cfca06d7SDimitry Andric template <StringElementType type>
64cfca06d7SDimitry Andric static DecodedCharBuffer
65cfca06d7SDimitry Andric GetPrintableImpl(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
66cfca06d7SDimitry Andric StringPrinter::EscapeStyle escape_style);
67205afe67SEd Maste
68cfca06d7SDimitry Andric // Mimic isprint() for Unicode codepoints.
isprint32(char32_t codepoint)69cfca06d7SDimitry Andric static bool isprint32(char32_t codepoint) {
70205afe67SEd Maste if (codepoint <= 0x1F || codepoint == 0x7F) // C0
71205afe67SEd Maste {
72205afe67SEd Maste return false;
73205afe67SEd Maste }
74205afe67SEd Maste if (codepoint >= 0x80 && codepoint <= 0x9F) // C1
75205afe67SEd Maste {
76205afe67SEd Maste return false;
77205afe67SEd Maste }
78205afe67SEd Maste if (codepoint == 0x2028 || codepoint == 0x2029) // line/paragraph separators
79205afe67SEd Maste {
80205afe67SEd Maste return false;
81205afe67SEd Maste }
8214f1b3e8SDimitry Andric if (codepoint == 0x200E || codepoint == 0x200F ||
8314f1b3e8SDimitry Andric (codepoint >= 0x202A &&
8414f1b3e8SDimitry Andric codepoint <= 0x202E)) // bidirectional text control
85205afe67SEd Maste {
86205afe67SEd Maste return false;
87205afe67SEd Maste }
8814f1b3e8SDimitry Andric if (codepoint >= 0xFFF9 &&
8914f1b3e8SDimitry Andric codepoint <= 0xFFFF) // interlinears and generally specials
90205afe67SEd Maste {
91205afe67SEd Maste return false;
92205afe67SEd Maste }
93205afe67SEd Maste return true;
94205afe67SEd Maste }
95205afe67SEd Maste
attemptASCIIEscape(llvm::UTF32 c,StringPrinter::EscapeStyle escape_style)96cfca06d7SDimitry Andric DecodedCharBuffer attemptASCIIEscape(llvm::UTF32 c,
97cfca06d7SDimitry Andric StringPrinter::EscapeStyle escape_style) {
98cfca06d7SDimitry Andric const bool is_swift_escape_style =
99cfca06d7SDimitry Andric escape_style == StringPrinter::EscapeStyle::Swift;
100cfca06d7SDimitry Andric switch (c) {
101205afe67SEd Maste case 0:
102cfca06d7SDimitry Andric return {"\\0", 2};
103205afe67SEd Maste case '\a':
104cfca06d7SDimitry Andric return {"\\a", 2};
105205afe67SEd Maste case '\b':
106cfca06d7SDimitry Andric if (is_swift_escape_style)
107cfca06d7SDimitry Andric return nullptr;
108cfca06d7SDimitry Andric return {"\\b", 2};
109205afe67SEd Maste case '\f':
110cfca06d7SDimitry Andric if (is_swift_escape_style)
111cfca06d7SDimitry Andric return nullptr;
112cfca06d7SDimitry Andric return {"\\f", 2};
113205afe67SEd Maste case '\n':
114cfca06d7SDimitry Andric return {"\\n", 2};
115205afe67SEd Maste case '\r':
116cfca06d7SDimitry Andric return {"\\r", 2};
117205afe67SEd Maste case '\t':
118cfca06d7SDimitry Andric return {"\\t", 2};
119205afe67SEd Maste case '\v':
120cfca06d7SDimitry Andric if (is_swift_escape_style)
121cfca06d7SDimitry Andric return nullptr;
122cfca06d7SDimitry Andric return {"\\v", 2};
123205afe67SEd Maste case '\"':
124cfca06d7SDimitry Andric return {"\\\"", 2};
125cfca06d7SDimitry Andric case '\'':
126cfca06d7SDimitry Andric if (is_swift_escape_style)
127cfca06d7SDimitry Andric return {"\\'", 2};
128cfca06d7SDimitry Andric return nullptr;
129205afe67SEd Maste case '\\':
130cfca06d7SDimitry Andric return {"\\\\", 2};
131205afe67SEd Maste }
132cfca06d7SDimitry Andric return nullptr;
133205afe67SEd Maste }
134205afe67SEd Maste
135205afe67SEd Maste template <>
GetPrintableImpl(uint8_t * buffer,uint8_t * buffer_end,uint8_t * & next,StringPrinter::EscapeStyle escape_style)136cfca06d7SDimitry Andric DecodedCharBuffer GetPrintableImpl<StringElementType::ASCII>(
137cfca06d7SDimitry Andric uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
138cfca06d7SDimitry Andric StringPrinter::EscapeStyle escape_style) {
139cfca06d7SDimitry Andric // The ASCII helper always advances 1 byte at a time.
140205afe67SEd Maste next = buffer + 1;
141cfca06d7SDimitry Andric
142cfca06d7SDimitry Andric DecodedCharBuffer retval = attemptASCIIEscape(*buffer, escape_style);
143cfca06d7SDimitry Andric if (retval.GetSize())
144205afe67SEd Maste return retval;
145cfca06d7SDimitry Andric
146cfca06d7SDimitry Andric // Use llvm's locale-independent isPrint(char), instead of the libc
147cfca06d7SDimitry Andric // implementation which may give different results on different platforms.
148cfca06d7SDimitry Andric if (llvm::isPrint(*buffer))
149cfca06d7SDimitry Andric return {buffer, 1};
150cfca06d7SDimitry Andric
151cfca06d7SDimitry Andric unsigned escaped_len;
152cfca06d7SDimitry Andric constexpr unsigned max_buffer_size = 7;
153cfca06d7SDimitry Andric uint8_t data[max_buffer_size];
154cfca06d7SDimitry Andric switch (escape_style) {
155cfca06d7SDimitry Andric case StringPrinter::EscapeStyle::CXX:
156cfca06d7SDimitry Andric // Prints 4 characters, then a \0 terminator.
1577fa27ce4SDimitry Andric escaped_len = snprintf((char *)data, max_buffer_size, "\\x%02x", *buffer);
158cfca06d7SDimitry Andric break;
159cfca06d7SDimitry Andric case StringPrinter::EscapeStyle::Swift:
160cfca06d7SDimitry Andric // Prints up to 6 characters, then a \0 terminator.
1617fa27ce4SDimitry Andric escaped_len = snprintf((char *)data, max_buffer_size, "\\u{%x}", *buffer);
162cfca06d7SDimitry Andric break;
163cfca06d7SDimitry Andric }
164cfca06d7SDimitry Andric lldbassert(escaped_len > 0 && "unknown string escape style");
165cfca06d7SDimitry Andric return {data, escaped_len};
166205afe67SEd Maste }
167205afe67SEd Maste
168cfca06d7SDimitry Andric template <>
GetPrintableImpl(uint8_t * buffer,uint8_t * buffer_end,uint8_t * & next,StringPrinter::EscapeStyle escape_style)169cfca06d7SDimitry Andric DecodedCharBuffer GetPrintableImpl<StringElementType::UTF8>(
170cfca06d7SDimitry Andric uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
171cfca06d7SDimitry Andric StringPrinter::EscapeStyle escape_style) {
172cfca06d7SDimitry Andric // If the utf8 encoded length is invalid (i.e., not in the closed interval
173cfca06d7SDimitry Andric // [1;4]), or if there aren't enough bytes to print, or if the subsequence
174cfca06d7SDimitry Andric // isn't valid utf8, fall back to printing an ASCII-escaped subsequence.
175cfca06d7SDimitry Andric if (!llvm::isLegalUTF8Sequence(buffer, buffer_end))
176cfca06d7SDimitry Andric return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next,
177cfca06d7SDimitry Andric escape_style);
178205afe67SEd Maste
179cfca06d7SDimitry Andric // Convert the valid utf8 sequence to a utf32 codepoint. This cannot fail.
180cfca06d7SDimitry Andric llvm::UTF32 codepoint = 0;
181cfca06d7SDimitry Andric const llvm::UTF8 *buffer_for_conversion = buffer;
182cfca06d7SDimitry Andric llvm::ConversionResult result = llvm::convertUTF8Sequence(
183cfca06d7SDimitry Andric &buffer_for_conversion, buffer_end, &codepoint, llvm::strictConversion);
184cfca06d7SDimitry Andric assert(result == llvm::conversionOK &&
185cfca06d7SDimitry Andric "Failed to convert legal utf8 sequence");
186b1c73532SDimitry Andric UNUSED_IF_ASSERT_DISABLED(result);
187cfca06d7SDimitry Andric
188cfca06d7SDimitry Andric // The UTF8 helper always advances by the utf8 encoded length.
189cfca06d7SDimitry Andric const unsigned utf8_encoded_len = buffer_for_conversion - buffer;
190205afe67SEd Maste next = buffer + utf8_encoded_len;
191205afe67SEd Maste
192cfca06d7SDimitry Andric DecodedCharBuffer retval = attemptASCIIEscape(codepoint, escape_style);
193cfca06d7SDimitry Andric if (retval.GetSize())
194205afe67SEd Maste return retval;
195cfca06d7SDimitry Andric if (isprint32(codepoint))
196cfca06d7SDimitry Andric return {buffer, utf8_encoded_len};
197cfca06d7SDimitry Andric
198cfca06d7SDimitry Andric unsigned escaped_len;
199cfca06d7SDimitry Andric constexpr unsigned max_buffer_size = 13;
200cfca06d7SDimitry Andric uint8_t data[max_buffer_size];
201cfca06d7SDimitry Andric switch (escape_style) {
202cfca06d7SDimitry Andric case StringPrinter::EscapeStyle::CXX:
203cfca06d7SDimitry Andric // Prints 10 characters, then a \0 terminator.
2047fa27ce4SDimitry Andric escaped_len = snprintf((char *)data, max_buffer_size, "\\U%08x", codepoint);
205cfca06d7SDimitry Andric break;
206cfca06d7SDimitry Andric case StringPrinter::EscapeStyle::Swift:
207cfca06d7SDimitry Andric // Prints up to 12 characters, then a \0 terminator.
2087fa27ce4SDimitry Andric escaped_len = snprintf((char *)data, max_buffer_size, "\\u{%x}", codepoint);
209cfca06d7SDimitry Andric break;
210cfca06d7SDimitry Andric }
211cfca06d7SDimitry Andric lldbassert(escaped_len > 0 && "unknown string escape style");
212cfca06d7SDimitry Andric return {data, escaped_len};
213205afe67SEd Maste }
214205afe67SEd Maste
215f73363f1SDimitry Andric // Given a sequence of bytes, this function returns: a sequence of bytes to
216f73363f1SDimitry Andric // actually print out + a length the following unscanned position of the buffer
217f73363f1SDimitry Andric // is in next
GetPrintable(StringElementType type,uint8_t * buffer,uint8_t * buffer_end,uint8_t * & next,StringPrinter::EscapeStyle escape_style)218cfca06d7SDimitry Andric static DecodedCharBuffer GetPrintable(StringElementType type, uint8_t *buffer,
219cfca06d7SDimitry Andric uint8_t *buffer_end, uint8_t *&next,
220cfca06d7SDimitry Andric StringPrinter::EscapeStyle escape_style) {
221cfca06d7SDimitry Andric if (!buffer || buffer >= buffer_end)
222205afe67SEd Maste return {nullptr};
223205afe67SEd Maste
22414f1b3e8SDimitry Andric switch (type) {
225cfca06d7SDimitry Andric case StringElementType::ASCII:
226cfca06d7SDimitry Andric return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next,
227cfca06d7SDimitry Andric escape_style);
228cfca06d7SDimitry Andric case StringElementType::UTF8:
229cfca06d7SDimitry Andric return GetPrintableImpl<StringElementType::UTF8>(buffer, buffer_end, next,
230cfca06d7SDimitry Andric escape_style);
231205afe67SEd Maste default:
232205afe67SEd Maste return {nullptr};
233205afe67SEd Maste }
234205afe67SEd Maste }
235205afe67SEd Maste
236cfca06d7SDimitry Andric static EscapingHelper
GetDefaultEscapingHelper(GetPrintableElementType elem_type,StringPrinter::EscapeStyle escape_style)237cfca06d7SDimitry Andric GetDefaultEscapingHelper(GetPrintableElementType elem_type,
238cfca06d7SDimitry Andric StringPrinter::EscapeStyle escape_style) {
23914f1b3e8SDimitry Andric switch (elem_type) {
240e81d9d49SDimitry Andric case GetPrintableElementType::UTF8:
241e81d9d49SDimitry Andric case GetPrintableElementType::ASCII:
242cfca06d7SDimitry Andric return [escape_style, elem_type](uint8_t *buffer, uint8_t *buffer_end,
243cfca06d7SDimitry Andric uint8_t *&next) -> DecodedCharBuffer {
244cfca06d7SDimitry Andric return GetPrintable(elem_type == GetPrintableElementType::UTF8
245cfca06d7SDimitry Andric ? StringElementType::UTF8
246cfca06d7SDimitry Andric : StringElementType::ASCII,
247cfca06d7SDimitry Andric buffer, buffer_end, next, escape_style);
248e81d9d49SDimitry Andric };
249e81d9d49SDimitry Andric }
250e81d9d49SDimitry Andric llvm_unreachable("bad element type");
251e81d9d49SDimitry Andric }
252e81d9d49SDimitry Andric
253cfca06d7SDimitry Andric /// Read a string encoded in accordance with \tparam SourceDataType from a
254cfca06d7SDimitry Andric /// host-side LLDB buffer, then pretty-print it to a stream using \p style.
255205afe67SEd Maste template <typename SourceDataType>
DumpEncodedBufferToStream(GetPrintableElementType style,llvm::ConversionResult (* ConvertFunction)(const SourceDataType **,const SourceDataType *,llvm::UTF8 **,llvm::UTF8 *,llvm::ConversionFlags),const StringPrinter::ReadBufferAndDumpToStreamOptions & dump_options)256cfca06d7SDimitry Andric static bool DumpEncodedBufferToStream(
257cfca06d7SDimitry Andric GetPrintableElementType style,
25814f1b3e8SDimitry Andric llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
259205afe67SEd Maste const SourceDataType *,
26014f1b3e8SDimitry Andric llvm::UTF8 **, llvm::UTF8 *,
26114f1b3e8SDimitry Andric llvm::ConversionFlags),
26214f1b3e8SDimitry Andric const StringPrinter::ReadBufferAndDumpToStreamOptions &dump_options) {
263cfca06d7SDimitry Andric assert(dump_options.GetStream() && "need a Stream to print the string to");
264e81d9d49SDimitry Andric Stream &stream(*dump_options.GetStream());
2655f29bb8aSDimitry Andric if (dump_options.GetPrefixToken() != nullptr)
266e81d9d49SDimitry Andric stream.Printf("%s", dump_options.GetPrefixToken());
267e81d9d49SDimitry Andric if (dump_options.GetQuote() != 0)
268e81d9d49SDimitry Andric stream.Printf("%c", dump_options.GetQuote());
269e81d9d49SDimitry Andric auto data(dump_options.GetData());
270e81d9d49SDimitry Andric auto source_size(dump_options.GetSourceSize());
27114f1b3e8SDimitry Andric if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) {
272205afe67SEd Maste const int bufferSPSize = data.GetByteSize();
27314f1b3e8SDimitry Andric if (dump_options.GetSourceSize() == 0) {
274205afe67SEd Maste const int origin_encoding = 8 * sizeof(SourceDataType);
275e81d9d49SDimitry Andric source_size = bufferSPSize / (origin_encoding / 4);
276205afe67SEd Maste }
277205afe67SEd Maste
27814f1b3e8SDimitry Andric const SourceDataType *data_ptr =
27914f1b3e8SDimitry Andric (const SourceDataType *)data.GetDataStart();
280e81d9d49SDimitry Andric const SourceDataType *data_end_ptr = data_ptr + source_size;
281205afe67SEd Maste
282e81d9d49SDimitry Andric const bool zero_is_terminator = dump_options.GetBinaryZeroIsTerminator();
283e81d9d49SDimitry Andric
28414f1b3e8SDimitry Andric if (zero_is_terminator) {
28514f1b3e8SDimitry Andric while (data_ptr < data_end_ptr) {
28614f1b3e8SDimitry Andric if (!*data_ptr) {
287205afe67SEd Maste data_end_ptr = data_ptr;
288205afe67SEd Maste break;
289205afe67SEd Maste }
290205afe67SEd Maste data_ptr++;
291205afe67SEd Maste }
292205afe67SEd Maste
2935e95aa85SEd Maste data_ptr = (const SourceDataType *)data.GetDataStart();
294e81d9d49SDimitry Andric }
295205afe67SEd Maste
296145449b1SDimitry Andric lldb::WritableDataBufferSP utf8_data_buffer_sp;
29714f1b3e8SDimitry Andric llvm::UTF8 *utf8_data_ptr = nullptr;
29814f1b3e8SDimitry Andric llvm::UTF8 *utf8_data_end_ptr = nullptr;
299205afe67SEd Maste
30014f1b3e8SDimitry Andric if (ConvertFunction) {
3015f29bb8aSDimitry Andric utf8_data_buffer_sp =
3025f29bb8aSDimitry Andric std::make_shared<DataBufferHeap>(4 * bufferSPSize, 0);
30314f1b3e8SDimitry Andric utf8_data_ptr = (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
304205afe67SEd Maste utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
30514f1b3e8SDimitry Andric ConvertFunction(&data_ptr, data_end_ptr, &utf8_data_ptr,
30614f1b3e8SDimitry Andric utf8_data_end_ptr, llvm::lenientConversion);
30794994d37SDimitry Andric if (!zero_is_terminator)
308e81d9d49SDimitry Andric utf8_data_end_ptr = utf8_data_ptr;
30914f1b3e8SDimitry Andric // needed because the ConvertFunction will change the value of the
31014f1b3e8SDimitry Andric // data_ptr.
31114f1b3e8SDimitry Andric utf8_data_ptr =
31214f1b3e8SDimitry Andric (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
31314f1b3e8SDimitry Andric } else {
31414f1b3e8SDimitry Andric // just copy the pointers - the cast is necessary to make the compiler
315f73363f1SDimitry Andric // happy but this should only happen if we are reading UTF8 data
31614f1b3e8SDimitry Andric utf8_data_ptr = const_cast<llvm::UTF8 *>(
31714f1b3e8SDimitry Andric reinterpret_cast<const llvm::UTF8 *>(data_ptr));
31814f1b3e8SDimitry Andric utf8_data_end_ptr = const_cast<llvm::UTF8 *>(
31914f1b3e8SDimitry Andric reinterpret_cast<const llvm::UTF8 *>(data_end_ptr));
320e81d9d49SDimitry Andric }
321e81d9d49SDimitry Andric
322e81d9d49SDimitry Andric const bool escape_non_printables = dump_options.GetEscapeNonPrintables();
323cfca06d7SDimitry Andric EscapingHelper escaping_callback;
324cfca06d7SDimitry Andric if (escape_non_printables)
32514f1b3e8SDimitry Andric escaping_callback =
326cfca06d7SDimitry Andric GetDefaultEscapingHelper(style, dump_options.GetEscapeStyle());
327205afe67SEd Maste
328205afe67SEd Maste // since we tend to accept partial data (and even partially malformed data)
329f73363f1SDimitry Andric // we might end up with no NULL terminator before the end_ptr hence we need
330f73363f1SDimitry Andric // to take a slower route and ensure we stay within boundaries
33114f1b3e8SDimitry Andric for (; utf8_data_ptr < utf8_data_end_ptr;) {
332e81d9d49SDimitry Andric if (zero_is_terminator && !*utf8_data_ptr)
333205afe67SEd Maste break;
334205afe67SEd Maste
33514f1b3e8SDimitry Andric if (escape_non_printables) {
336205afe67SEd Maste uint8_t *next_data = nullptr;
33714f1b3e8SDimitry Andric auto printable =
33814f1b3e8SDimitry Andric escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data);
339205afe67SEd Maste auto printable_bytes = printable.GetBytes();
340205afe67SEd Maste auto printable_size = printable.GetSize();
341cfca06d7SDimitry Andric
342cfca06d7SDimitry Andric // We failed to figure out how to print this string.
343cfca06d7SDimitry Andric if (!printable_bytes || !next_data)
344cfca06d7SDimitry Andric return false;
345cfca06d7SDimitry Andric
346205afe67SEd Maste for (unsigned c = 0; c < printable_size; c++)
347205afe67SEd Maste stream.Printf("%c", *(printable_bytes + c));
348205afe67SEd Maste utf8_data_ptr = (uint8_t *)next_data;
34914f1b3e8SDimitry Andric } else {
350205afe67SEd Maste stream.Printf("%c", *utf8_data_ptr);
351205afe67SEd Maste utf8_data_ptr++;
352205afe67SEd Maste }
353205afe67SEd Maste }
354205afe67SEd Maste }
355e81d9d49SDimitry Andric if (dump_options.GetQuote() != 0)
356e81d9d49SDimitry Andric stream.Printf("%c", dump_options.GetQuote());
3575f29bb8aSDimitry Andric if (dump_options.GetSuffixToken() != nullptr)
358e81d9d49SDimitry Andric stream.Printf("%s", dump_options.GetSuffixToken());
359e81d9d49SDimitry Andric if (dump_options.GetIsTruncated())
360e81d9d49SDimitry Andric stream.Printf("...");
361205afe67SEd Maste return true;
362205afe67SEd Maste }
363205afe67SEd Maste
36414f1b3e8SDimitry Andric lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::
ReadStringAndDumpToStreamOptions(ValueObject & valobj)36514f1b3e8SDimitry Andric ReadStringAndDumpToStreamOptions(ValueObject &valobj)
36614f1b3e8SDimitry Andric : ReadStringAndDumpToStreamOptions() {
36714f1b3e8SDimitry Andric SetEscapeNonPrintables(
36814f1b3e8SDimitry Andric valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
369205afe67SEd Maste }
370205afe67SEd Maste
37114f1b3e8SDimitry Andric lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
ReadBufferAndDumpToStreamOptions(ValueObject & valobj)37214f1b3e8SDimitry Andric ReadBufferAndDumpToStreamOptions(ValueObject &valobj)
37314f1b3e8SDimitry Andric : ReadBufferAndDumpToStreamOptions() {
37414f1b3e8SDimitry Andric SetEscapeNonPrintables(
37514f1b3e8SDimitry Andric valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
376205afe67SEd Maste }
377205afe67SEd Maste
37814f1b3e8SDimitry Andric lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
ReadBufferAndDumpToStreamOptions(const ReadStringAndDumpToStreamOptions & options)37914f1b3e8SDimitry Andric ReadBufferAndDumpToStreamOptions(
38014f1b3e8SDimitry Andric const ReadStringAndDumpToStreamOptions &options)
38114f1b3e8SDimitry Andric : ReadBufferAndDumpToStreamOptions() {
382e81d9d49SDimitry Andric SetStream(options.GetStream());
383e81d9d49SDimitry Andric SetPrefixToken(options.GetPrefixToken());
384e81d9d49SDimitry Andric SetSuffixToken(options.GetSuffixToken());
385e81d9d49SDimitry Andric SetQuote(options.GetQuote());
386e81d9d49SDimitry Andric SetEscapeNonPrintables(options.GetEscapeNonPrintables());
387e81d9d49SDimitry Andric SetBinaryZeroIsTerminator(options.GetBinaryZeroIsTerminator());
388cfca06d7SDimitry Andric SetEscapeStyle(options.GetEscapeStyle());
389e81d9d49SDimitry Andric }
390e81d9d49SDimitry Andric
39114f1b3e8SDimitry Andric namespace lldb_private {
392205afe67SEd Maste
39314f1b3e8SDimitry Andric namespace formatters {
394205afe67SEd Maste
395205afe67SEd Maste template <typename SourceDataType>
ReadEncodedBufferAndDumpToStream(StringElementType elem_type,const StringPrinter::ReadStringAndDumpToStreamOptions & options,llvm::ConversionResult (* ConvertFunction)(const SourceDataType **,const SourceDataType *,llvm::UTF8 **,llvm::UTF8 *,llvm::ConversionFlags))396cfca06d7SDimitry Andric static bool ReadEncodedBufferAndDumpToStream(
397cfca06d7SDimitry Andric StringElementType elem_type,
39814f1b3e8SDimitry Andric const StringPrinter::ReadStringAndDumpToStreamOptions &options,
39914f1b3e8SDimitry Andric llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
400205afe67SEd Maste const SourceDataType *,
40114f1b3e8SDimitry Andric llvm::UTF8 **, llvm::UTF8 *,
40214f1b3e8SDimitry Andric llvm::ConversionFlags)) {
403205afe67SEd Maste assert(options.GetStream() && "need a Stream to print the string to");
404cfca06d7SDimitry Andric if (!options.GetStream())
405cfca06d7SDimitry Andric return false;
406205afe67SEd Maste
40714f1b3e8SDimitry Andric if (options.GetLocation() == 0 ||
40814f1b3e8SDimitry Andric options.GetLocation() == LLDB_INVALID_ADDRESS)
409205afe67SEd Maste return false;
410205afe67SEd Maste
411c0981da4SDimitry Andric lldb::TargetSP target_sp = options.GetTargetSP();
412c0981da4SDimitry Andric if (!target_sp)
413205afe67SEd Maste return false;
414205afe67SEd Maste
415cfca06d7SDimitry Andric constexpr int type_width = sizeof(SourceDataType);
416cfca06d7SDimitry Andric constexpr int origin_encoding = 8 * type_width;
417205afe67SEd Maste if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
418205afe67SEd Maste return false;
419cfca06d7SDimitry Andric // If not UTF8 or ASCII, conversion to UTF8 is necessary.
420205afe67SEd Maste if (origin_encoding != 8 && !ConvertFunction)
421205afe67SEd Maste return false;
422205afe67SEd Maste
423205afe67SEd Maste bool needs_zero_terminator = options.GetNeedsZeroTermination();
424205afe67SEd Maste
425e81d9d49SDimitry Andric bool is_truncated = false;
426c0981da4SDimitry Andric const auto max_size = target_sp->GetMaximumSizeOfStringSummary();
427e81d9d49SDimitry Andric
428cfca06d7SDimitry Andric uint32_t sourceSize;
429cfca06d7SDimitry Andric if (elem_type == StringElementType::ASCII && !options.GetSourceSize()) {
430cfca06d7SDimitry Andric // FIXME: The NSString formatter sets HasSourceSize(true) when the size is
431cfca06d7SDimitry Andric // actually unknown, as well as SetBinaryZeroIsTerminator(false). IIUC the
432cfca06d7SDimitry Andric // C++ formatter also sets SetBinaryZeroIsTerminator(false) when it doesn't
433cfca06d7SDimitry Andric // mean to. I don't see how this makes sense: we should fix the formatters.
434cfca06d7SDimitry Andric //
435cfca06d7SDimitry Andric // Until then, the behavior that's expected for ASCII strings with unknown
436cfca06d7SDimitry Andric // lengths is to read up to the max size and then null-terminate. Do that.
437e81d9d49SDimitry Andric sourceSize = max_size;
438205afe67SEd Maste needs_zero_terminator = true;
439cfca06d7SDimitry Andric } else if (options.HasSourceSize()) {
440cfca06d7SDimitry Andric sourceSize = options.GetSourceSize();
441cfca06d7SDimitry Andric if (!options.GetIgnoreMaxLength()) {
44214f1b3e8SDimitry Andric if (sourceSize > max_size) {
443e81d9d49SDimitry Andric sourceSize = max_size;
444e81d9d49SDimitry Andric is_truncated = true;
445e81d9d49SDimitry Andric }
446e81d9d49SDimitry Andric }
447cfca06d7SDimitry Andric } else {
448cfca06d7SDimitry Andric sourceSize = max_size;
449cfca06d7SDimitry Andric needs_zero_terminator = true;
450cfca06d7SDimitry Andric }
451205afe67SEd Maste
452205afe67SEd Maste const int bufferSPSize = sourceSize * type_width;
453145449b1SDimitry Andric lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize, 0));
454205afe67SEd Maste
455cfca06d7SDimitry Andric // Check if we got bytes. We never get any bytes if we have an empty
456cfca06d7SDimitry Andric // string, but we still continue so that we end up actually printing
457cfca06d7SDimitry Andric // an empty string ("").
458cfca06d7SDimitry Andric if (sourceSize != 0 && !buffer_sp->GetBytes())
459205afe67SEd Maste return false;
460205afe67SEd Maste
461b76161e4SDimitry Andric Status error;
462205afe67SEd Maste char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
463205afe67SEd Maste
464cfca06d7SDimitry Andric if (elem_type == StringElementType::ASCII)
465c0981da4SDimitry Andric target_sp->ReadCStringFromMemory(options.GetLocation(), buffer,
466cfca06d7SDimitry Andric bufferSPSize, error);
467cfca06d7SDimitry Andric else if (needs_zero_terminator)
468c0981da4SDimitry Andric target_sp->ReadStringFromMemory(options.GetLocation(), buffer,
46914f1b3e8SDimitry Andric bufferSPSize, error, type_width);
470205afe67SEd Maste else
471c0981da4SDimitry Andric target_sp->ReadMemory(options.GetLocation(), buffer, bufferSPSize, error);
47214f1b3e8SDimitry Andric if (error.Fail()) {
473205afe67SEd Maste options.GetStream()->Printf("unable to read data");
474205afe67SEd Maste return true;
475205afe67SEd Maste }
476205afe67SEd Maste
477e81d9d49SDimitry Andric StringPrinter::ReadBufferAndDumpToStreamOptions dump_options(options);
478c0981da4SDimitry Andric dump_options.SetData(
479c0981da4SDimitry Andric DataExtractor(buffer_sp, target_sp->GetArchitecture().GetByteOrder(),
480c0981da4SDimitry Andric target_sp->GetArchitecture().GetAddressByteSize()));
481e81d9d49SDimitry Andric dump_options.SetSourceSize(sourceSize);
482e81d9d49SDimitry Andric dump_options.SetIsTruncated(is_truncated);
483cfca06d7SDimitry Andric dump_options.SetNeedsZeroTermination(needs_zero_terminator);
484cfca06d7SDimitry Andric if (needs_zero_terminator)
485cfca06d7SDimitry Andric dump_options.SetBinaryZeroIsTerminator(true);
486e81d9d49SDimitry Andric
487cfca06d7SDimitry Andric GetPrintableElementType print_style = (elem_type == StringElementType::ASCII)
488cfca06d7SDimitry Andric ? GetPrintableElementType::ASCII
489cfca06d7SDimitry Andric : GetPrintableElementType::UTF8;
490cfca06d7SDimitry Andric return DumpEncodedBufferToStream(print_style, ConvertFunction, dump_options);
491205afe67SEd Maste }
492205afe67SEd Maste
493205afe67SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)494cfca06d7SDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(
49514f1b3e8SDimitry Andric const ReadStringAndDumpToStreamOptions &options) {
496cfca06d7SDimitry Andric return ReadEncodedBufferAndDumpToStream<llvm::UTF8>(StringElementType::UTF8,
497cfca06d7SDimitry Andric options, nullptr);
498205afe67SEd Maste }
499205afe67SEd Maste
500205afe67SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)501cfca06d7SDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(
50214f1b3e8SDimitry Andric const ReadStringAndDumpToStreamOptions &options) {
503cfca06d7SDimitry Andric return ReadEncodedBufferAndDumpToStream<llvm::UTF16>(
504cfca06d7SDimitry Andric StringElementType::UTF16, options, llvm::ConvertUTF16toUTF8);
505205afe67SEd Maste }
506205afe67SEd Maste
507205afe67SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)508cfca06d7SDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(
50914f1b3e8SDimitry Andric const ReadStringAndDumpToStreamOptions &options) {
510cfca06d7SDimitry Andric return ReadEncodedBufferAndDumpToStream<llvm::UTF32>(
511cfca06d7SDimitry Andric StringElementType::UTF32, options, llvm::ConvertUTF32toUTF8);
512205afe67SEd Maste }
513205afe67SEd Maste
514205afe67SEd Maste template <>
ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions & options)515cfca06d7SDimitry Andric bool StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(
516cfca06d7SDimitry Andric const ReadStringAndDumpToStreamOptions &options) {
517cfca06d7SDimitry Andric return ReadEncodedBufferAndDumpToStream<char>(StringElementType::ASCII,
518cfca06d7SDimitry Andric options, nullptr);
519cfca06d7SDimitry Andric }
520cfca06d7SDimitry Andric
521cfca06d7SDimitry Andric template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)522cfca06d7SDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8>(
52314f1b3e8SDimitry Andric const ReadBufferAndDumpToStreamOptions &options) {
524cfca06d7SDimitry Andric return DumpEncodedBufferToStream<llvm::UTF8>(GetPrintableElementType::UTF8,
525cfca06d7SDimitry Andric nullptr, options);
526205afe67SEd Maste }
527205afe67SEd Maste
528205afe67SEd Maste template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)529cfca06d7SDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(
53014f1b3e8SDimitry Andric const ReadBufferAndDumpToStreamOptions &options) {
531cfca06d7SDimitry Andric return DumpEncodedBufferToStream(GetPrintableElementType::UTF8,
532cfca06d7SDimitry Andric llvm::ConvertUTF16toUTF8, options);
533cfca06d7SDimitry Andric }
534cfca06d7SDimitry Andric
535cfca06d7SDimitry Andric template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)536cfca06d7SDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>(
537cfca06d7SDimitry Andric const ReadBufferAndDumpToStreamOptions &options) {
538cfca06d7SDimitry Andric return DumpEncodedBufferToStream(GetPrintableElementType::UTF8,
539cfca06d7SDimitry Andric llvm::ConvertUTF32toUTF8, options);
540cfca06d7SDimitry Andric }
541cfca06d7SDimitry Andric
542cfca06d7SDimitry Andric template <>
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions & options)543cfca06d7SDimitry Andric bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::ASCII>(
544cfca06d7SDimitry Andric const ReadBufferAndDumpToStreamOptions &options) {
545cfca06d7SDimitry Andric // Treat ASCII the same as UTF8.
546cfca06d7SDimitry Andric //
547cfca06d7SDimitry Andric // FIXME: This is probably not the right thing to do (well, it's debatable).
548cfca06d7SDimitry Andric // If an ASCII-encoded string happens to contain a sequence of invalid bytes
549cfca06d7SDimitry Andric // that forms a valid UTF8 character, we'll print out that character. This is
550cfca06d7SDimitry Andric // good if you're playing fast and loose with encodings (probably good for
551cfca06d7SDimitry Andric // std::string users), but maybe not so good if you care about your string
552cfca06d7SDimitry Andric // formatter respecting the semantics of your selected string encoding. In
553cfca06d7SDimitry Andric // the latter case you'd want to see the character byte sequence ('\x..'), not
554cfca06d7SDimitry Andric // the UTF8 character itself.
555205afe67SEd Maste return ReadBufferAndDumpToStream<StringElementType::UTF8>(options);
556205afe67SEd Maste }
557205afe67SEd Maste
558205afe67SEd Maste } // namespace formatters
559205afe67SEd Maste
560205afe67SEd Maste } // namespace lldb_private
561