LLDB mainline
NSString.cpp
Go to the documentation of this file.
1//===-- NSString.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "NSString.h"
10
14#include "lldb/Target/Target.h"
17#include "lldb/Utility/Endian.h"
18#include "lldb/Utility/Status.h"
19#include "lldb/Utility/Stream.h"
22
23using namespace lldb;
24using namespace lldb_private;
25using namespace lldb_private::formatters;
26
27std::map<ConstString, CXXFunctionSummaryFormat::Callback> &
29 static std::map<ConstString, CXXFunctionSummaryFormat::Callback> g_map;
30 return g_map;
31}
32
34 ValueObject &valobj, Stream &stream,
35 const TypeSummaryOptions &summary_options) {
36 static constexpr llvm::StringLiteral g_TypeHint("NSString");
37
38 ProcessSP process_sp = valobj.GetProcessSP();
39 if (!process_sp)
40 return false;
41
42 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
43
44 if (!runtime)
45 return false;
46
48 runtime->GetClassDescriptor(valobj));
49
50 if (!descriptor.get() || !descriptor->IsValid())
51 return false;
52
53 uint32_t ptr_size = process_sp->GetAddressByteSize();
54
55 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
56
57 if (!valobj_addr)
58 return false;
59
60 ConstString class_name_cs = descriptor->GetClassName();
61 llvm::StringRef class_name = class_name_cs.GetStringRef();
62
63 if (class_name.empty())
64 return false;
65
66 // For tagged pointers, the descriptor has everything needed.
67 bool is_tagged = descriptor->GetTaggedPointerInfo();
68 if (is_tagged) {
69 if (class_name == "NSTaggedPointerString")
70 return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
71 summary_options);
72
73 if (class_name == "NSIndirectTaggedPointerString")
74 return NSIndirectTaggedString_SummaryProvider(valobj, descriptor, stream,
75 summary_options);
76 }
77
78 auto &additionals_map(NSString_Additionals::GetAdditionalSummaries());
79 auto iter = additionals_map.find(class_name_cs), end = additionals_map.end();
80 if (iter != end)
81 return iter->second(valobj, stream, summary_options);
82
83 // if not a tagged pointer that we know about, try the normal route
84 uint64_t info_bits_location = valobj_addr + ptr_size;
85 if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
86 info_bits_location += 3;
87
89
90 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(
91 info_bits_location, 1, 0, error);
92 if (error.Fail())
93 return false;
94
95 bool is_mutable = (info_bits & 1) == 1;
96 bool is_inline = (info_bits & 0x60) == 0;
97 bool has_explicit_length = (info_bits & (1 | 4)) != 4;
98 bool is_unicode = (info_bits & 0x10) == 0x10;
99 bool is_path_store = class_name == "NSPathStore2";
100 bool has_null = (info_bits & 8) == 8;
101
102 size_t explicit_length = 0;
103 if (!has_null && has_explicit_length && !is_path_store) {
104 lldb::addr_t explicit_length_offset = 2 * ptr_size;
105 if (is_mutable && !is_inline)
106 explicit_length_offset =
107 explicit_length_offset + ptr_size; // notInlineMutable.length;
108 else if (is_inline)
109 explicit_length = explicit_length + 0; // inline1.length;
110 else if (!is_inline && !is_mutable)
111 explicit_length_offset =
112 explicit_length_offset + ptr_size; // notInlineImmutable1.length;
113 else
114 explicit_length_offset = 0;
115
116 if (explicit_length_offset) {
117 explicit_length_offset = valobj_addr + explicit_length_offset;
118 explicit_length = process_sp->ReadUnsignedIntegerFromMemory(
119 explicit_length_offset, 4, 0, error);
120 }
121 }
122
123 const llvm::StringSet<> supported_string_classes = {
124 "NSString", "CFMutableStringRef",
125 "CFStringRef", "__NSCFConstantString",
126 "__NSCFString", "NSCFConstantString",
127 "NSCFString", "NSPathStore2"};
128 if (supported_string_classes.count(class_name) == 0) {
129 // not one of us - but tell me class name
130 stream.Printf("class name = %s", class_name_cs.GetCString());
131 return true;
132 }
133
134 llvm::StringRef prefix, suffix;
135 if (Language *language = Language::FindPlugin(summary_options.GetLanguage()))
136 std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
137
139 options.SetPrefixToken(prefix.str());
140 options.SetSuffixToken(suffix.str());
141
142 if (is_mutable) {
143 uint64_t location = 2 * ptr_size + valobj_addr;
144 location = process_sp->ReadPointerFromMemory(location, error);
145 if (error.Fail())
146 return false;
147 if (has_explicit_length && is_unicode) {
148 options.SetLocation(Address(location));
149 options.SetTargetSP(valobj.GetTargetSP());
150 options.SetStream(&stream);
151 options.SetQuote('"');
152 options.SetSourceSize(explicit_length);
153 options.SetHasSourceSize(has_explicit_length);
155 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
158 StringPrinter::StringElementType::UTF16>(options);
159 } else {
160 options.SetLocation(Address(location + 1));
161 options.SetTargetSP(valobj.GetTargetSP());
162 options.SetStream(&stream);
163 options.SetSourceSize(explicit_length);
164 options.SetHasSourceSize(has_explicit_length);
166 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
169 StringPrinter::StringElementType::ASCII>(options);
170 }
171 } else if (is_inline && has_explicit_length && !is_unicode &&
172 !is_path_store && !is_mutable) {
173 uint64_t location = 3 * ptr_size + valobj_addr;
174
175 options.SetLocation(Address(location));
176 options.SetTargetSP(valobj.GetTargetSP());
177 options.SetStream(&stream);
178 options.SetQuote('"');
179 options.SetSourceSize(explicit_length);
180 options.SetHasSourceSize(has_explicit_length);
181 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
184 StringPrinter::StringElementType::ASCII>(options);
185 } else if (is_unicode) {
186 uint64_t location = valobj_addr + 2 * ptr_size;
187 if (is_inline) {
188 if (!has_explicit_length) {
189 return false;
190 } else
191 location += ptr_size;
192 } else {
193 location = process_sp->ReadPointerFromMemory(location, error);
194 if (error.Fail())
195 return false;
196 }
197 options.SetLocation(Address(location));
198 options.SetTargetSP(valobj.GetTargetSP());
199 options.SetStream(&stream);
200 options.SetQuote('"');
201 options.SetSourceSize(explicit_length);
202 options.SetHasSourceSize(has_explicit_length);
203 if (has_explicit_length)
205 else
207 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
210 StringPrinter::StringElementType::UTF16>(options);
211 } else if (is_path_store) {
212 // _lengthAndRefCount is the first ivar of NSPathStore2 (after the isa).
213 uint64_t length_ivar_offset = 1 * ptr_size;
214 CompilerType length_type = valobj.GetCompilerType().GetBasicTypeFromAST(
216 ValueObjectSP length_valobj_sp =
217 valobj.GetSyntheticChildAtOffset(length_ivar_offset, length_type, true,
218 ConstString("_lengthAndRefCount"));
219 if (!length_valobj_sp)
220 return false;
221 // Get the length out of _lengthAndRefCount.
222 explicit_length = length_valobj_sp->GetValueAsUnsigned(0) >> 20;
223 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
224
225 options.SetLocation(Address(location));
226 options.SetTargetSP(valobj.GetTargetSP());
227 options.SetStream(&stream);
228 options.SetQuote('"');
229 options.SetSourceSize(explicit_length);
230 options.SetHasSourceSize(has_explicit_length);
231 if (has_explicit_length)
233 else
235 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
238 StringPrinter::StringElementType::UTF16>(options);
239 } else if (is_inline) {
240 uint64_t location = valobj_addr + 2 * ptr_size;
241 if (!has_explicit_length) {
242 // in this kind of string, the byte before the string content is a length
243 // byte so let's try and use it to handle the embedded NUL case
245 explicit_length =
246 process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error);
247 has_explicit_length = !(error.Fail() || explicit_length == 0);
248 location++;
249 }
250 options.SetLocation(Address(location));
251 options.SetTargetSP(valobj.GetTargetSP());
252 options.SetStream(&stream);
253 options.SetSourceSize(explicit_length);
254 options.SetHasSourceSize(has_explicit_length);
255 if (has_explicit_length)
257 else
259 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
261 if (has_explicit_length)
263 StringPrinter::StringElementType::UTF8>(options);
264 else
266 StringPrinter::StringElementType::ASCII>(options);
267 } else {
268 uint64_t location = valobj_addr + 2 * ptr_size;
269 location = process_sp->ReadPointerFromMemory(location, error);
270 if (error.Fail())
271 return false;
272 if (has_explicit_length && !has_null)
273 explicit_length++; // account for the fact that there is no NULL and we
274 // need to have one added
275 options.SetLocation(Address(location));
276 options.SetTargetSP(valobj.GetTargetSP());
277 options.SetStream(&stream);
278 options.SetSourceSize(explicit_length);
279 options.SetHasSourceSize(has_explicit_length);
280 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
283 StringPrinter::StringElementType::ASCII>(options);
284 }
285}
286
288 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
289 TargetSP target_sp(valobj.GetTargetSP());
290 if (!target_sp)
291 return false;
292 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
293 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
294 if (!pointer_value)
295 return false;
296 pointer_value += addr_size;
297 CompilerType type(valobj.GetCompilerType());
298 ExecutionContext exe_ctx(target_sp, false);
300 "string_ptr", pointer_value, exe_ctx, type));
301 if (!child_ptr_sp)
302 return false;
303 DataExtractor data;
305 child_ptr_sp->GetData(data, error);
306 if (error.Fail())
307 return false;
308 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData(
309 "string_data", data, exe_ctx, type));
310 child_sp->GetValueAsUnsigned(0);
311 if (child_sp)
312 return NSStringSummaryProvider(*child_sp, stream, options);
313 return false;
314}
315
317 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
318 return NSAttributedStringSummaryProvider(valobj, stream, options);
319}
320
323 Stream &stream, const TypeSummaryOptions &summary_options) {
324 static constexpr llvm::StringLiteral g_TypeHint("NSString");
325
326 if (!descriptor)
327 return false;
328 uint64_t len_bits = 0, data_bits = 0;
329 if (!descriptor->GetTaggedPointerInfo(&len_bits, &data_bits, nullptr))
330 return false;
331
332 static const int g_MaxNonBitmaskedLen = 7; // TAGGED_STRING_UNPACKED_MAXLEN
333 static const int g_SixbitMaxLen = 9;
334 static const int g_fiveBitMaxLen = 11;
335
336 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013"
337 "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
338
339 if (len_bits > g_fiveBitMaxLen)
340 return false;
341
342 llvm::StringRef prefix, suffix;
343 if (Language *language = Language::FindPlugin(summary_options.GetLanguage()))
344 std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
345
346 // this is a fairly ugly trick - pretend that the numeric value is actually a
347 // char* this works under a few assumptions: little endian architecture
348 // sizeof(uint64_t) > g_MaxNonBitmaskedLen
349 if (len_bits <= g_MaxNonBitmaskedLen) {
350 stream << prefix;
351 stream.Printf("\"%s\"", (const char *)&data_bits);
352 stream << suffix;
353 return true;
354 }
355
356 // if the data is bitmasked, we need to actually process the bytes
357 uint8_t bitmask = 0;
358 uint8_t shift_offset = 0;
359
360 if (len_bits <= g_SixbitMaxLen) {
361 bitmask = 0x03f;
362 shift_offset = 6;
363 } else {
364 bitmask = 0x01f;
365 shift_offset = 5;
366 }
367
368 std::vector<uint8_t> bytes;
369 bytes.resize(len_bits);
370 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits) {
371 uint8_t packed = data_bits & bitmask;
372 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
373 }
374
375 stream << prefix;
376 stream.Printf("\"%s\"", &bytes[0]);
377 stream << suffix;
378 return true;
379}
380
383 Stream &stream, const TypeSummaryOptions &summary_options) {
384 if (!descriptor)
385 return false;
386
387 uint64_t payload = 0;
388 if (!descriptor->GetTaggedPointerInfo(nullptr, nullptr, &payload))
389 return false;
390
391 // First 47 bits are the address of the contents.
392 addr_t ptr = payload & 0x7fffffffffffULL;
393 // Next 13 bits are the string's length.
394 size_t size = (payload >> 47) & 0x1fff;
395
396 Status status;
397 std::vector<char> buf(size);
398 if (auto process_sp = valobj.GetProcessSP())
399 if (process_sp->ReadMemory(ptr, buf.data(), size, status)) {
400 llvm::StringRef prefix, suffix;
401 if (auto *language = Language::FindPlugin(summary_options.GetLanguage()))
402 std::tie(prefix, suffix) =
403 language->GetFormatterPrefixSuffix("NSString");
404 stream << prefix << '"';
405 stream.PutCString({buf.data(), size});
406 stream << '"' << suffix;
407 return true;
408 }
409
410 if (status.Fail())
411 stream.Format("<{0}>", status);
412 return false;
413}
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition Address.h:62
Generic representation of a type in a programming language.
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
An data extractor class.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
static Language * FindPlugin(lldb::LanguageType language)
Definition Language.cpp:84
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
static ObjCLanguageRuntime * Get(Process &process)
virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value)
An error handling class.
Definition Status.h:118
bool Fail() const
Test for error condition.
Definition Status.cpp:293
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
lldb::LanguageType GetLanguage() const
lldb::TypeSummaryCapping GetCapping() const
lldb::ProcessSP GetProcessSP() const
lldb::ValueObjectSP CreateChildValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
Given an address either create a value object containing the value at that address,...
virtual lldb::ValueObjectSP GetSyntheticChildAtOffset(uint32_t offset, const CompilerType &type, bool can_create, ConstString name_const_str=ConstString())
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
lldb::TargetSP GetTargetSP() const
CompilerType GetCompilerType()
static std::map< ConstString, CXXFunctionSummaryFormat::Callback > & GetAdditionalSummaries()
Definition NSString.cpp:28
@ ZeroTerminate
Stop printing at the first zero terminator.
@ Ignore
Don't look for a terminator - print the whole buffer.
static bool ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions &options)
bool NSMutableAttributedStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSString.cpp:316
bool NSAttributedStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSString.cpp:287
bool NSIndirectTaggedString_SummaryProvider(ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor, Stream &stream, const TypeSummaryOptions &summary_options)
Definition NSString.cpp:381
bool NSTaggedString_SummaryProvider(ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor, Stream &stream, const TypeSummaryOptions &summary_options)
Definition NSString.cpp:321
bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition NSString.cpp:33
A class that represents a running process on the host machine.
@ eBasicTypeUnsignedInt
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP