blob: 9653e25e04a76604e3b45a7f27cbcbf98271be01 [file] [log] [blame]
Enrico Granataf5196282012-09-04 18:48:21 +00001//===-- CXXFormatterFunctions.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
Matt Kopecef143712013-06-03 18:00:07 +000010#include "lldb/lldb-python.h"
11
Enrico Granata5548cb52013-01-28 23:47:25 +000012#include "lldb/DataFormatters/CXXFormatterFunctions.h"
Enrico Granataf5196282012-09-04 18:48:21 +000013
Dmitri Gribenko024aa852013-01-30 15:05:59 +000014#include "llvm/Support/ConvertUTF.h"
Enrico Granataf5196282012-09-04 18:48:21 +000015
Enrico Granatab2698cd2012-09-13 18:27:09 +000016#include "lldb/Core/DataBufferHeap.h"
17#include "lldb/Core/Error.h"
Enrico Granataf5196282012-09-04 18:48:21 +000018#include "lldb/Core/Stream.h"
19#include "lldb/Core/ValueObject.h"
Enrico Granatab2698cd2012-09-13 18:27:09 +000020#include "lldb/Core/ValueObjectConstResult.h"
21#include "lldb/Host/Endian.h"
Enrico Granatab5887262012-10-29 21:18:03 +000022#include "lldb/Symbol/ClangASTContext.h"
Enrico Granataf5196282012-09-04 18:48:21 +000023#include "lldb/Target/Target.h"
Enrico Granataba8eb122014-07-30 21:07:50 +000024#include "lldb/Target/Thread.h"
Enrico Granataf5196282012-09-04 18:48:21 +000025
Enrico Granata76b08d52014-10-29 23:08:02 +000026#include "lldb/Utility/ProcessStructReader.h"
27
Enrico Granatad83bfce2013-04-02 21:25:34 +000028#include <algorithm>
29
Enrico Granataf5196282012-09-04 18:48:21 +000030using namespace lldb;
31using namespace lldb_private;
32using namespace lldb_private::formatters;
33
Enrico Granataba8eb122014-07-30 21:07:50 +000034StackFrame*
35lldb_private::formatters::GetViableFrame (ExecutionContext exe_ctx)
36{
37 StackFrame* frame = exe_ctx.GetFramePtr();
38 if (frame)
39 return frame;
40
41 Process* process = exe_ctx.GetProcessPtr();
42 if (!process)
43 return nullptr;
44
45 ThreadSP thread_sp(process->GetThreadList().GetSelectedThread());
46 if (thread_sp)
47 return thread_sp->GetSelectedFrame().get();
48 return nullptr;
49}
50
Enrico Granataf5196282012-09-04 18:48:21 +000051bool
Enrico Granatab2698cd2012-09-13 18:27:09 +000052lldb_private::formatters::ExtractValueFromObjCExpression (ValueObject &valobj,
53 const char* target_type,
54 const char* selector,
55 uint64_t &value)
Enrico Granataf5196282012-09-04 18:48:21 +000056{
57 if (!target_type || !*target_type)
58 return false;
59 if (!selector || !*selector)
60 return false;
Enrico Granataf5196282012-09-04 18:48:21 +000061 StreamString expr;
Daniel Maleaa85e6b62012-12-07 22:21:08 +000062 expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector);
Enrico Granataf5196282012-09-04 18:48:21 +000063 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
64 lldb::ValueObjectSP result_sp;
65 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +000066 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granataf5196282012-09-04 18:48:21 +000067 if (!target || !stack_frame)
68 return false;
Enrico Granatad4439aa2012-09-05 20:41:26 +000069
Jim Ingham35e1bda2012-10-16 21:41:58 +000070 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +000071 options.SetCoerceToId(false);
72 options.SetUnwindOnError(true);
73 options.SetKeepInMemory(true);
Enrico Granatad4439aa2012-09-05 20:41:26 +000074
Enrico Granataf5196282012-09-04 18:48:21 +000075 target->EvaluateExpression(expr.GetData(),
76 stack_frame,
Enrico Granatad4439aa2012-09-05 20:41:26 +000077 result_sp,
78 options);
Enrico Granataf5196282012-09-04 18:48:21 +000079 if (!result_sp)
80 return false;
81 value = result_sp->GetValueAsUnsigned(0);
82 return true;
83}
84
Enrico Granataf615b802013-02-15 23:38:37 +000085bool
86lldb_private::formatters::ExtractSummaryFromObjCExpression (ValueObject &valobj,
87 const char* target_type,
88 const char* selector,
89 Stream &stream)
90{
91 if (!target_type || !*target_type)
92 return false;
93 if (!selector || !*selector)
94 return false;
95 StreamString expr;
Enrico Granataeac4a482013-02-19 01:14:06 +000096 expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector);
Enrico Granataf615b802013-02-15 23:38:37 +000097 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
98 lldb::ValueObjectSP result_sp;
99 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +0000100 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granataf615b802013-02-15 23:38:37 +0000101 if (!target || !stack_frame)
102 return false;
103
104 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000105 options.SetCoerceToId(false);
106 options.SetUnwindOnError(true);
107 options.SetKeepInMemory(true);
108 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granataf615b802013-02-15 23:38:37 +0000109
110 target->EvaluateExpression(expr.GetData(),
111 stack_frame,
112 result_sp,
113 options);
114 if (!result_sp)
115 return false;
116 stream.Printf("%s",result_sp->GetSummaryAsCString());
117 return true;
118}
119
Enrico Granatab2698cd2012-09-13 18:27:09 +0000120lldb::ValueObjectSP
121lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj,
122 const char* return_type,
123 const char* selector,
124 uint64_t index)
125{
126 lldb::ValueObjectSP valobj_sp;
127 if (!return_type || !*return_type)
128 return valobj_sp;
129 if (!selector || !*selector)
130 return valobj_sp;
131 StreamString expr_path_stream;
132 valobj.GetExpressionPath(expr_path_stream, false);
133 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +0000134 expr.Printf("(%s)[%s %s:%" PRId64 "]",return_type,expr_path_stream.GetData(),selector,index);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000135 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
136 lldb::ValueObjectSP result_sp;
137 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +0000138 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000139 if (!target || !stack_frame)
140 return valobj_sp;
141
Jim Ingham35e1bda2012-10-16 21:41:58 +0000142 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000143 options.SetCoerceToId(false);
144 options.SetUnwindOnError(true);
145 options.SetKeepInMemory(true);
146 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000147
148 target->EvaluateExpression(expr.GetData(),
149 stack_frame,
150 valobj_sp,
151 options);
152 return valobj_sp;
153}
154
155lldb::ValueObjectSP
156lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj,
157 const char* return_type,
158 const char* selector,
159 const char* key)
160{
161 lldb::ValueObjectSP valobj_sp;
162 if (!return_type || !*return_type)
163 return valobj_sp;
164 if (!selector || !*selector)
165 return valobj_sp;
166 if (!key || !*key)
167 return valobj_sp;
168 StreamString expr_path_stream;
169 valobj.GetExpressionPath(expr_path_stream, false);
170 StreamString expr;
171 expr.Printf("(%s)[%s %s:%s]",return_type,expr_path_stream.GetData(),selector,key);
172 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
173 lldb::ValueObjectSP result_sp;
174 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +0000175 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000176 if (!target || !stack_frame)
177 return valobj_sp;
178
Jim Ingham35e1bda2012-10-16 21:41:58 +0000179 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000180 options.SetCoerceToId(false);
181 options.SetUnwindOnError(true);
182 options.SetKeepInMemory(true);
183 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000184
185 target->EvaluateExpression(expr.GetData(),
186 stack_frame,
187 valobj_sp,
188 options);
189 return valobj_sp;
190}
191
Enrico Granata93d59662013-01-14 23:53:26 +0000192// use this call if you already have an LLDB-side buffer for the data
193template<typename SourceDataType>
Enrico Granataf68df122013-01-10 22:08:35 +0000194static bool
Enrico Granata93d59662013-01-14 23:53:26 +0000195DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**,
196 const SourceDataType*,
197 UTF8**,
198 UTF8*,
199 ConversionFlags),
200 DataExtractor& data,
201 Stream& stream,
202 char prefix_token = '@',
203 char quote = '"',
Enrico Granatad83bfce2013-04-02 21:25:34 +0000204 uint32_t sourceSize = 0)
Enrico Granataf68df122013-01-10 22:08:35 +0000205{
Enrico Granata93d59662013-01-14 23:53:26 +0000206 if (prefix_token != 0)
207 stream.Printf("%c",prefix_token);
208 if (quote != 0)
209 stream.Printf("%c",quote);
210 if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd())
Enrico Granataf68df122013-01-10 22:08:35 +0000211 {
Enrico Granata93d59662013-01-14 23:53:26 +0000212 const int bufferSPSize = data.GetByteSize();
213 if (sourceSize == 0)
214 {
215 const int origin_encoding = 8*sizeof(SourceDataType);
Greg Claytonaa4c47a2013-02-08 21:59:34 +0000216 sourceSize = bufferSPSize/(origin_encoding / 4);
Enrico Granata93d59662013-01-14 23:53:26 +0000217 }
218
219 SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart();
Enrico Granataf68df122013-01-10 22:08:35 +0000220 SourceDataType *data_end_ptr = data_ptr + sourceSize;
221
222 while (data_ptr < data_end_ptr)
223 {
224 if (!*data_ptr)
225 {
226 data_end_ptr = data_ptr;
227 break;
228 }
229 data_ptr++;
230 }
231
Enrico Granata93d59662013-01-14 23:53:26 +0000232 data_ptr = (SourceDataType*)data.GetDataStart();
Enrico Granataf68df122013-01-10 22:08:35 +0000233
Enrico Granata38352042013-01-11 02:44:00 +0000234 lldb::DataBufferSP utf8_data_buffer_sp;
235 UTF8* utf8_data_ptr = nullptr;
236 UTF8* utf8_data_end_ptr = nullptr;
Enrico Granata93d59662013-01-14 23:53:26 +0000237
Enrico Granataf68df122013-01-10 22:08:35 +0000238 if (ConvertFunction)
Enrico Granata38352042013-01-11 02:44:00 +0000239 {
Enrico Granata56768392013-04-23 20:05:05 +0000240 utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0));
Enrico Granata38352042013-01-11 02:44:00 +0000241 utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes();
Enrico Granata56768392013-04-23 20:05:05 +0000242 utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
Enrico Granataf68df122013-01-10 22:08:35 +0000243 ConvertFunction ( (const SourceDataType**)&data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion );
Enrico Granata38352042013-01-11 02:44:00 +0000244 utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr
245 }
Enrico Granataf68df122013-01-10 22:08:35 +0000246 else
247 {
248 // just copy the pointers - the cast is necessary to make the compiler happy
249 // but this should only happen if we are reading UTF8 data
250 utf8_data_ptr = (UTF8*)data_ptr;
251 utf8_data_end_ptr = (UTF8*)data_end_ptr;
252 }
253
Enrico Granata3309d882013-01-12 01:00:22 +0000254 // since we tend to accept partial data (and even partially malformed data)
255 // we might end up with no NULL terminator before the end_ptr
256 // hence we need to take a slower route and ensure we stay within boundaries
Enrico Granataf68df122013-01-10 22:08:35 +0000257 for (;utf8_data_ptr != utf8_data_end_ptr; utf8_data_ptr++)
258 {
259 if (!*utf8_data_ptr)
260 break;
261 stream.Printf("%c",*utf8_data_ptr);
262 }
Enrico Granata93d59662013-01-14 23:53:26 +0000263 }
264 if (quote != 0)
265 stream.Printf("%c",quote);
266 return true;
267}
268
269template<typename SourceDataType>
Enrico Granata56768392013-04-23 20:05:05 +0000270class ReadUTFBufferAndDumpToStreamOptions
Enrico Granata93d59662013-01-14 23:53:26 +0000271{
Enrico Granata56768392013-04-23 20:05:05 +0000272public:
273 typedef ConversionResult (*ConvertFunctionType) (const SourceDataType**,
274 const SourceDataType*,
275 UTF8**,
276 UTF8*,
277 ConversionFlags);
278
279 ReadUTFBufferAndDumpToStreamOptions () :
280 m_conversion_function(NULL),
281 m_location(0),
282 m_process_sp(),
283 m_stream(NULL),
284 m_prefix_token('@'),
285 m_quote('"'),
286 m_source_size(0),
287 m_needs_zero_termination(true)
288 {
289 }
290
291 ReadUTFBufferAndDumpToStreamOptions&
292 SetConversionFunction (ConvertFunctionType f)
293 {
294 m_conversion_function = f;
295 return *this;
296 }
297
298 ConvertFunctionType
299 GetConversionFunction () const
300 {
301 return m_conversion_function;
302 }
303
304 ReadUTFBufferAndDumpToStreamOptions&
305 SetLocation (uint64_t l)
306 {
307 m_location = l;
308 return *this;
309 }
310
311 uint64_t
312 GetLocation () const
313 {
314 return m_location;
315 }
316
317 ReadUTFBufferAndDumpToStreamOptions&
318 SetProcessSP (ProcessSP p)
319 {
320 m_process_sp = p;
321 return *this;
322 }
323
324 ProcessSP
325 GetProcessSP () const
326 {
327 return m_process_sp;
328 }
329
330 ReadUTFBufferAndDumpToStreamOptions&
331 SetStream (Stream* s)
332 {
333 m_stream = s;
334 return *this;
335 }
336
337 Stream*
338 GetStream () const
339 {
340 return m_stream;
341 }
342
343 ReadUTFBufferAndDumpToStreamOptions&
344 SetPrefixToken (char p)
345 {
346 m_prefix_token = p;
347 return *this;
348 }
349
350 char
351 GetPrefixToken () const
352 {
353 return m_prefix_token;
354 }
355
356 ReadUTFBufferAndDumpToStreamOptions&
357 SetQuote (char q)
358 {
359 m_quote = q;
360 return *this;
361 }
362
363 char
364 GetQuote () const
365 {
366 return m_quote;
367 }
368
369 ReadUTFBufferAndDumpToStreamOptions&
370 SetSourceSize (uint32_t s)
371 {
372 m_source_size = s;
373 return *this;
374 }
375
376 uint32_t
377 GetSourceSize () const
378 {
379 return m_source_size;
380 }
381
382 ReadUTFBufferAndDumpToStreamOptions&
383 SetNeedsZeroTermination (bool z)
384 {
385 m_needs_zero_termination = z;
386 return *this;
387 }
388
389 bool
390 GetNeedsZeroTermination () const
391 {
392 return m_needs_zero_termination;
393 }
394
395private:
396 ConvertFunctionType m_conversion_function;
397 uint64_t m_location;
398 ProcessSP m_process_sp;
399 Stream* m_stream;
400 char m_prefix_token;
401 char m_quote;
402 uint32_t m_source_size;
403 bool m_needs_zero_termination;
404};
405
406template<typename SourceDataType>
407static bool
408ReadUTFBufferAndDumpToStream (const ReadUTFBufferAndDumpToStreamOptions<SourceDataType>& options)
409{
410 if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS)
Enrico Granata93d59662013-01-14 23:53:26 +0000411 return false;
Enrico Granata56768392013-04-23 20:05:05 +0000412
413 ProcessSP process_sp(options.GetProcessSP());
414
Enrico Granata93d59662013-01-14 23:53:26 +0000415 if (!process_sp)
416 return false;
417
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +0000418 const int type_width = sizeof(SourceDataType);
419 const int origin_encoding = 8 * type_width ;
Enrico Granata93d59662013-01-14 23:53:26 +0000420 if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
421 return false;
422 // if not UTF8, I need a conversion function to return proper UTF8
Enrico Granata56768392013-04-23 20:05:05 +0000423 if (origin_encoding != 8 && !options.GetConversionFunction())
424 return false;
425
426 if (!options.GetStream())
Enrico Granata93d59662013-01-14 23:53:26 +0000427 return false;
428
Enrico Granata56768392013-04-23 20:05:05 +0000429 uint32_t sourceSize = options.GetSourceSize();
430 bool needs_zero_terminator = options.GetNeedsZeroTermination();
431
Enrico Granata23ab35a2013-04-02 23:07:55 +0000432 if (!sourceSize)
Enrico Granata56768392013-04-23 20:05:05 +0000433 {
Enrico Granata23ab35a2013-04-02 23:07:55 +0000434 sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
Enrico Granata56768392013-04-23 20:05:05 +0000435 needs_zero_terminator = true;
436 }
Enrico Granata23ab35a2013-04-02 23:07:55 +0000437 else
438 sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
439
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +0000440 const int bufferSPSize = sourceSize * type_width;
Enrico Granata93d59662013-01-14 23:53:26 +0000441
Enrico Granata93d59662013-01-14 23:53:26 +0000442 lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0));
443
444 if (!buffer_sp->GetBytes())
445 return false;
446
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +0000447 Error error;
448 char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
449
Enrico Granata56768392013-04-23 20:05:05 +0000450 size_t data_read = 0;
451 if (needs_zero_terminator)
452 data_read = process_sp->ReadStringFromMemory(options.GetLocation(), buffer, bufferSPSize, error, type_width);
453 else
454 data_read = process_sp->ReadMemoryFromInferior(options.GetLocation(), (char*)buffer_sp->GetBytes(), bufferSPSize, error);
455
Enrico Granata93d59662013-01-14 23:53:26 +0000456 if (error.Fail() || data_read == 0)
457 {
Enrico Granata56768392013-04-23 20:05:05 +0000458 options.GetStream()->Printf("unable to read data");
Enrico Granataf68df122013-01-10 22:08:35 +0000459 return true;
460 }
Enrico Granata93d59662013-01-14 23:53:26 +0000461
462 DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize());
463
Enrico Granata56768392013-04-23 20:05:05 +0000464 return DumpUTFBufferToStream(options.GetConversionFunction(), data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize);
Enrico Granataf68df122013-01-10 22:08:35 +0000465}
466
467bool
468lldb_private::formatters::Char16StringSummaryProvider (ValueObject& valobj, Stream& stream)
469{
470 ProcessSP process_sp = valobj.GetProcessSP();
471 if (!process_sp)
472 return false;
473
474 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
475
476 if (!valobj_addr)
477 return false;
478
Enrico Granata56768392013-04-23 20:05:05 +0000479 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
480 options.SetLocation(valobj_addr);
481 options.SetConversionFunction(ConvertUTF16toUTF8);
482 options.SetProcessSP(process_sp);
483 options.SetStream(&stream);
484 options.SetPrefixToken('u');
485
486 if (!ReadUTFBufferAndDumpToStream(options))
Enrico Granataf68df122013-01-10 22:08:35 +0000487 {
488 stream.Printf("Summary Unavailable");
489 return true;
490 }
491
492 return true;
493}
494
495bool
496lldb_private::formatters::Char32StringSummaryProvider (ValueObject& valobj, Stream& stream)
497{
498 ProcessSP process_sp = valobj.GetProcessSP();
499 if (!process_sp)
500 return false;
501
502 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
503
504 if (!valobj_addr)
505 return false;
506
Enrico Granata56768392013-04-23 20:05:05 +0000507 ReadUTFBufferAndDumpToStreamOptions<UTF32> options;
508 options.SetLocation(valobj_addr);
509 options.SetConversionFunction(ConvertUTF32toUTF8);
510 options.SetProcessSP(process_sp);
511 options.SetStream(&stream);
Enrico Granatac03c5862013-04-23 21:37:33 +0000512 options.SetPrefixToken('U');
Enrico Granata56768392013-04-23 20:05:05 +0000513
514 if (!ReadUTFBufferAndDumpToStream(options))
Enrico Granataf68df122013-01-10 22:08:35 +0000515 {
516 stream.Printf("Summary Unavailable");
517 return true;
518 }
519
520 return true;
521}
522
523bool
524lldb_private::formatters::WCharStringSummaryProvider (ValueObject& valobj, Stream& stream)
525{
Enrico Granata38352042013-01-11 02:44:00 +0000526 ProcessSP process_sp = valobj.GetProcessSP();
527 if (!process_sp)
528 return false;
529
Enrico Granata3309d882013-01-12 01:00:22 +0000530 lldb::addr_t data_addr = 0;
531
532 if (valobj.IsPointerType())
533 data_addr = valobj.GetValueAsUnsigned(0);
534 else if (valobj.IsArrayType())
535 data_addr = valobj.GetAddressOf();
Enrico Granata38352042013-01-11 02:44:00 +0000536
Enrico Granata3309d882013-01-12 01:00:22 +0000537 if (data_addr == 0 || data_addr == LLDB_INVALID_ADDRESS)
Enrico Granata38352042013-01-11 02:44:00 +0000538 return false;
539
Greg Clayton57ee3062013-07-11 22:46:58 +0000540 clang::ASTContext* ast = valobj.GetClangType().GetASTContext();
541
Enrico Granata38352042013-01-11 02:44:00 +0000542 if (!ast)
543 return false;
544
Greg Clayton57ee3062013-07-11 22:46:58 +0000545 ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
546 const uint32_t wchar_size = wchar_clang_type.GetBitSize();
Enrico Granata38352042013-01-11 02:44:00 +0000547
548 switch (wchar_size)
549 {
550 case 8:
Enrico Granata56768392013-04-23 20:05:05 +0000551 {
Enrico Granata38352042013-01-11 02:44:00 +0000552 // utf 8
Enrico Granata56768392013-04-23 20:05:05 +0000553
554 ReadUTFBufferAndDumpToStreamOptions<UTF8> options;
555 options.SetLocation(data_addr);
556 options.SetConversionFunction(nullptr);
557 options.SetProcessSP(process_sp);
558 options.SetStream(&stream);
559 options.SetPrefixToken('L');
560
561 return ReadUTFBufferAndDumpToStream(options);
562 }
Enrico Granata38352042013-01-11 02:44:00 +0000563 case 16:
Enrico Granata56768392013-04-23 20:05:05 +0000564 {
Enrico Granata38352042013-01-11 02:44:00 +0000565 // utf 16
Enrico Granata56768392013-04-23 20:05:05 +0000566 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
567 options.SetLocation(data_addr);
568 options.SetConversionFunction(ConvertUTF16toUTF8);
569 options.SetProcessSP(process_sp);
570 options.SetStream(&stream);
571 options.SetPrefixToken('L');
572
573 return ReadUTFBufferAndDumpToStream(options);
574 }
Enrico Granata38352042013-01-11 02:44:00 +0000575 case 32:
Enrico Granata56768392013-04-23 20:05:05 +0000576 {
Enrico Granata38352042013-01-11 02:44:00 +0000577 // utf 32
Enrico Granata56768392013-04-23 20:05:05 +0000578 ReadUTFBufferAndDumpToStreamOptions<UTF32> options;
579 options.SetLocation(data_addr);
580 options.SetConversionFunction(ConvertUTF32toUTF8);
581 options.SetProcessSP(process_sp);
582 options.SetStream(&stream);
583 options.SetPrefixToken('L');
584
585 return ReadUTFBufferAndDumpToStream(options);
586 }
Enrico Granata93d59662013-01-14 23:53:26 +0000587 default:
588 stream.Printf("size for wchar_t is not valid");
589 return true;
590 }
591 return true;
592}
593
594bool
595lldb_private::formatters::Char16SummaryProvider (ValueObject& valobj, Stream& stream)
596{
597 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000598 Error error;
599 valobj.GetData(data, error);
600
601 if (error.Fail())
602 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000603
604 std::string value;
605 valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
606 if (!value.empty())
607 stream.Printf("%s ", value.c_str());
608
609 return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,data,stream, 'u','\'',1);
610}
611
612bool
613lldb_private::formatters::Char32SummaryProvider (ValueObject& valobj, Stream& stream)
614{
615 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000616 Error error;
617 valobj.GetData(data, error);
618
619 if (error.Fail())
620 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000621
622 std::string value;
623 valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
624 if (!value.empty())
625 stream.Printf("%s ", value.c_str());
626
627 return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,data,stream, 'U','\'',1);
628}
629
630bool
631lldb_private::formatters::WCharSummaryProvider (ValueObject& valobj, Stream& stream)
632{
633 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000634 Error error;
635 valobj.GetData(data, error);
636
637 if (error.Fail())
638 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000639
Greg Clayton57ee3062013-07-11 22:46:58 +0000640 clang::ASTContext* ast = valobj.GetClangType().GetASTContext();
Enrico Granata93d59662013-01-14 23:53:26 +0000641
642 if (!ast)
643 return false;
644
Greg Clayton57ee3062013-07-11 22:46:58 +0000645 ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
646 const uint32_t wchar_size = wchar_clang_type.GetBitSize();
Enrico Granata93d59662013-01-14 23:53:26 +0000647 std::string value;
648
Enrico Granata93d59662013-01-14 23:53:26 +0000649 switch (wchar_size)
650 {
651 case 8:
652 // utf 8
653 valobj.GetValueAsCString(lldb::eFormatChar, value);
654 if (!value.empty())
655 stream.Printf("%s ", value.c_str());
656 return DumpUTFBufferToStream<UTF8>(nullptr,
657 data,
658 stream,
659 'L',
660 '\'',
661 1);
662 case 16:
663 // utf 16
664 valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
665 if (!value.empty())
666 stream.Printf("%s ", value.c_str());
667 return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,
668 data,
669 stream,
670 'L',
671 '\'',
672 1);
673 case 32:
674 // utf 32
675 valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
676 if (!value.empty())
677 stream.Printf("%s ", value.c_str());
678 return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,
679 data,
680 stream,
681 'L',
682 '\'',
683 1);
Enrico Granata38352042013-01-11 02:44:00 +0000684 default:
685 stream.Printf("size for wchar_t is not valid");
686 return true;
687 }
688 return true;
Enrico Granataf68df122013-01-10 22:08:35 +0000689}
690
Enrico Granataa3962a72013-05-15 00:47:46 +0000691// the field layout in a libc++ string (cap, side, data or data, size, cap)
692enum LibcxxStringLayoutMode
693{
694 eLibcxxStringLayoutModeCSD = 0,
695 eLibcxxStringLayoutModeDSC = 1,
696 eLibcxxStringLayoutModeInvalid = 0xffff
697};
698
699// this function abstracts away the layout and mode details of a libc++ string
700// and returns the address of the data and the size ready for callers to consume
Enrico Granata3309d882013-01-12 01:00:22 +0000701static bool
702ExtractLibcxxStringInfo (ValueObject& valobj,
703 ValueObjectSP &location_sp,
704 uint64_t& size)
705{
706 ValueObjectSP D(valobj.GetChildAtIndexPath({0,0,0,0}));
707 if (!D)
708 return false;
709
Enrico Granataa3962a72013-05-15 00:47:46 +0000710 ValueObjectSP layout_decider(D->GetChildAtIndexPath({0,0}));
711
712 // this child should exist
713 if (!layout_decider)
Enrico Granata3309d882013-01-12 01:00:22 +0000714 return false;
715
Enrico Granataa3962a72013-05-15 00:47:46 +0000716 ConstString g_data_name("__data_");
717 ConstString g_size_name("__size_");
718 bool short_mode = false; // this means the string is in short-mode and the data is stored inline
719 LibcxxStringLayoutMode layout = (layout_decider->GetName() == g_data_name) ? eLibcxxStringLayoutModeDSC : eLibcxxStringLayoutModeCSD;
720 uint64_t size_mode_value = 0;
Enrico Granata3309d882013-01-12 01:00:22 +0000721
Enrico Granataa3962a72013-05-15 00:47:46 +0000722 if (layout == eLibcxxStringLayoutModeDSC)
723 {
724 ValueObjectSP size_mode(D->GetChildAtIndexPath({1,1,0}));
725 if (!size_mode)
726 return false;
727
728 if (size_mode->GetName() != g_size_name)
729 {
730 // we are hitting the padding structure, move along
731 size_mode = D->GetChildAtIndexPath({1,1,1});
732 if (!size_mode)
733 return false;
734 }
735
736 size_mode_value = (size_mode->GetValueAsUnsigned(0));
737 short_mode = ((size_mode_value & 0x80) == 0);
738 }
739 else
740 {
741 ValueObjectSP size_mode(D->GetChildAtIndexPath({1,0,0}));
742 if (!size_mode)
743 return false;
744
745 size_mode_value = (size_mode->GetValueAsUnsigned(0));
746 short_mode = ((size_mode_value & 1) == 0);
747 }
748
749 if (short_mode)
Enrico Granata3309d882013-01-12 01:00:22 +0000750 {
751 ValueObjectSP s(D->GetChildAtIndex(1, true));
752 if (!s)
753 return false;
Enrico Granataa3962a72013-05-15 00:47:46 +0000754 location_sp = s->GetChildAtIndex((layout == eLibcxxStringLayoutModeDSC) ? 0 : 1, true);
755 size = (layout == eLibcxxStringLayoutModeDSC) ? size_mode_value : ((size_mode_value >> 1) % 256);
Enrico Granata3309d882013-01-12 01:00:22 +0000756 return (location_sp.get() != nullptr);
757 }
758 else
759 {
760 ValueObjectSP l(D->GetChildAtIndex(0, true));
761 if (!l)
762 return false;
Enrico Granataa3962a72013-05-15 00:47:46 +0000763 // we can use the layout_decider object as the data pointer
764 location_sp = (layout == eLibcxxStringLayoutModeDSC) ? layout_decider : l->GetChildAtIndex(2, true);
Enrico Granata3309d882013-01-12 01:00:22 +0000765 ValueObjectSP size_vo(l->GetChildAtIndex(1, true));
766 if (!size_vo || !location_sp)
767 return false;
768 size = size_vo->GetValueAsUnsigned(0);
769 return true;
770 }
771}
772
773bool
774lldb_private::formatters::LibcxxWStringSummaryProvider (ValueObject& valobj, Stream& stream)
775{
776 uint64_t size = 0;
777 ValueObjectSP location_sp((ValueObject*)nullptr);
778 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
779 return false;
780 if (size == 0)
781 {
782 stream.Printf("L\"\"");
783 return true;
Enrico Granataa3962a72013-05-15 00:47:46 +0000784 }
Enrico Granata3309d882013-01-12 01:00:22 +0000785 if (!location_sp)
786 return false;
787 return WCharStringSummaryProvider(*location_sp.get(), stream);
788}
789
790bool
791lldb_private::formatters::LibcxxStringSummaryProvider (ValueObject& valobj, Stream& stream)
792{
793 uint64_t size = 0;
794 ValueObjectSP location_sp((ValueObject*)nullptr);
795 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
796 return false;
797 if (size == 0)
798 {
799 stream.Printf("\"\"");
800 return true;
801 }
802 if (!location_sp)
803 return false;
804 Error error;
Enrico Granataea2bc0f2013-02-21 19:57:10 +0000805 if (location_sp->ReadPointedString(stream,
806 error,
807 0, // max length is decided by the settings
808 false) == 0) // do not honor array (terminates on first 0 byte even for a char[])
809 stream.Printf("\"\""); // if nothing was read, print an empty string
Enrico Granata3309d882013-01-12 01:00:22 +0000810 return error.Success();
811}
812
Enrico Granata55900862013-03-15 18:55:30 +0000813bool
814lldb_private::formatters::ObjCClassSummaryProvider (ValueObject& valobj, Stream& stream)
815{
816 ProcessSP process_sp = valobj.GetProcessSP();
817 if (!process_sp)
818 return false;
819
820 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
821
822 if (!runtime)
823 return false;
824
Greg Clayton03da4cc2013-04-19 21:31:16 +0000825 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0)));
Enrico Granata55900862013-03-15 18:55:30 +0000826
827 if (!descriptor.get() || !descriptor->IsValid())
828 return false;
829
830 const char* class_name = descriptor->GetClassName().GetCString();
831
832 if (!class_name || !*class_name)
833 return false;
834
835 stream.Printf("%s",class_name);
836 return true;
837}
838
Enrico Granatac76b97b2013-04-26 00:59:02 +0000839class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd
840{
841public:
842 ObjCClassSyntheticChildrenFrontEnd (lldb::ValueObjectSP valobj_sp) :
843 SyntheticChildrenFrontEnd(*valobj_sp.get())
844 {
845 }
846
847 virtual size_t
848 CalculateNumChildren ()
849 {
850 return 0;
851 }
852
853 virtual lldb::ValueObjectSP
854 GetChildAtIndex (size_t idx)
855 {
856 return lldb::ValueObjectSP();
857 }
858
859 virtual bool
860 Update()
861 {
862 return false;
863 }
864
865 virtual bool
866 MightHaveChildren ()
867 {
868 return false;
869 }
870
871 virtual size_t
872 GetIndexOfChildWithName (const ConstString &name)
873 {
874 return UINT32_MAX;
875 }
876
877 virtual
878 ~ObjCClassSyntheticChildrenFrontEnd ()
879 {
880 }
881};
882
883SyntheticChildrenFrontEnd*
884lldb_private::formatters::ObjCClassSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
885{
886 return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp);
887}
888
Enrico Granataf5196282012-09-04 18:48:21 +0000889template<bool needs_at>
890bool
Enrico Granatab2698cd2012-09-13 18:27:09 +0000891lldb_private::formatters::NSDataSummaryProvider (ValueObject& valobj, Stream& stream)
Enrico Granataf5196282012-09-04 18:48:21 +0000892{
893 ProcessSP process_sp = valobj.GetProcessSP();
894 if (!process_sp)
895 return false;
896
897 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
898
899 if (!runtime)
900 return false;
901
902 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
903
904 if (!descriptor.get() || !descriptor->IsValid())
905 return false;
906
907 bool is_64bit = (process_sp->GetAddressByteSize() == 8);
908 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
909
910 if (!valobj_addr)
911 return false;
912
913 uint64_t value = 0;
914
915 const char* class_name = descriptor->GetClassName().GetCString();
Enrico Granata6d390772012-09-29 00:47:43 +0000916
917 if (!class_name || !*class_name)
918 return false;
919
Enrico Granataf5196282012-09-04 18:48:21 +0000920 if (!strcmp(class_name,"NSConcreteData") ||
921 !strcmp(class_name,"NSConcreteMutableData") ||
922 !strcmp(class_name,"__NSCFData"))
923 {
924 uint32_t offset = (is_64bit ? 16 : 8);
925 Error error;
926 value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, is_64bit ? 8 : 4, 0, error);
927 if (error.Fail())
928 return false;
929 }
930 else
931 {
Enrico Granatab2698cd2012-09-13 18:27:09 +0000932 if (!ExtractValueFromObjCExpression(valobj, "int", "length", value))
Enrico Granataf5196282012-09-04 18:48:21 +0000933 return false;
934 }
935
Daniel Malead01b2952012-11-29 21:49:15 +0000936 stream.Printf("%s%" PRIu64 " byte%s%s",
Enrico Granataf5196282012-09-04 18:48:21 +0000937 (needs_at ? "@\"" : ""),
938 value,
Enrico Granata1c333d02013-05-31 01:14:22 +0000939 (value != 1 ? "s" : ""),
Enrico Granataf5196282012-09-04 18:48:21 +0000940 (needs_at ? "\"" : ""));
941
942 return true;
943}
944
Enrico Granata87f00b42013-02-21 20:31:18 +0000945static bool
946ReadAsciiBufferAndDumpToStream (lldb::addr_t location,
947 lldb::ProcessSP& process_sp,
948 Stream& dest,
Enrico Granatad83bfce2013-04-02 21:25:34 +0000949 uint32_t size = 0,
Enrico Granata87f00b42013-02-21 20:31:18 +0000950 Error* error = NULL,
951 size_t *data_read = NULL,
952 char prefix_token = '@',
953 char quote = '"')
954{
955 Error my_error;
956 size_t my_data_read;
957 if (!process_sp || location == 0)
958 return false;
959
Enrico Granata23ab35a2013-04-02 23:07:55 +0000960 if (!size)
961 size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
962 else
963 size = std::min(size,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
Enrico Granata87f00b42013-02-21 20:31:18 +0000964
965 lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0));
966
967 my_data_read = process_sp->ReadCStringFromMemory(location, (char*)buffer_sp->GetBytes(), size, my_error);
968
969 if (error)
970 *error = my_error;
971 if (data_read)
972 *data_read = my_data_read;
973
974 if (my_error.Fail())
975 return false;
Enrico Granata5a9c4fe2013-05-17 23:28:13 +0000976
977 dest.Printf("%c%c",prefix_token,quote);
978
Enrico Granata87f00b42013-02-21 20:31:18 +0000979 if (my_data_read)
Enrico Granata5a9c4fe2013-05-17 23:28:13 +0000980 dest.Printf("%s",(char*)buffer_sp->GetBytes());
981
982 dest.Printf("%c",quote);
Enrico Granata87f00b42013-02-21 20:31:18 +0000983
984 return true;
985}
986
Enrico Granataf5196282012-09-04 18:48:21 +0000987bool
Jason Molenda705b1802014-06-13 02:37:02 +0000988lldb_private::formatters::NSTaggedString_SummaryProvider (ObjCLanguageRuntime::ClassDescriptorSP descriptor, Stream& stream)
989{
990 if (!descriptor)
991 return false;
992 uint64_t len_bits = 0, data_bits = 0;
993 if (!descriptor->GetTaggedPointerInfo(&len_bits,&data_bits,nullptr))
994 return false;
995
996 static const int g_MaxNonBitmaskedLen = 7; //TAGGED_STRING_UNPACKED_MAXLEN
997 static const int g_SixbitMaxLen = 9;
998 static const int g_fiveBitMaxLen = 11;
999
1000 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013" "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
1001
1002 if (len_bits > g_fiveBitMaxLen)
1003 return false;
1004
1005 // this is a fairly ugly trick - pretend that the numeric value is actually a char*
1006 // this works under a few assumptions:
1007 // little endian architecture
1008 // sizeof(uint64_t) > g_MaxNonBitmaskedLen
1009 if (len_bits <= g_MaxNonBitmaskedLen)
1010 {
1011 stream.Printf("@\"%s\"",(const char*)&data_bits);
1012 return true;
1013 }
1014
1015 // if the data is bitmasked, we need to actually process the bytes
1016 uint8_t bitmask = 0;
1017 uint8_t shift_offset = 0;
1018
1019 if (len_bits <= g_SixbitMaxLen)
1020 {
1021 bitmask = 0x03f;
1022 shift_offset = 6;
1023 }
1024 else
1025 {
1026 bitmask = 0x01f;
1027 shift_offset = 5;
1028 }
1029
1030 std::vector<uint8_t> bytes;
1031 bytes.resize(len_bits);
1032 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits)
1033 {
1034 uint8_t packed = data_bits & bitmask;
1035 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
1036 }
1037
1038 stream.Printf("@\"%s\"",&bytes[0]);
1039 return true;
1040}
1041
Enrico Granata76b08d52014-10-29 23:08:02 +00001042static ClangASTType
1043GetNSPathStore2Type (Target &target)
1044{
1045 static ConstString g_type_name("__lldb_autogen_nspathstore2");
1046
1047 ClangASTContext *ast_ctx = target.GetScratchClangASTContext();
1048
1049 if (!ast_ctx)
1050 return ClangASTType();
1051
1052 ClangASTType voidstar = ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
1053 ClangASTType uint32 = ast_ctx->GetIntTypeFromBitSize(32, false);
1054
1055 return ast_ctx->GetOrCreateStructForIdentifier(g_type_name, {
1056 {"isa",voidstar},
1057 {"lengthAndRef",uint32},
1058 {"buffer",voidstar}
1059 });
1060}
1061
Jason Molenda705b1802014-06-13 02:37:02 +00001062bool
Enrico Granatab2698cd2012-09-13 18:27:09 +00001063lldb_private::formatters::NSStringSummaryProvider (ValueObject& valobj, Stream& stream)
Enrico Granataf5196282012-09-04 18:48:21 +00001064{
1065 ProcessSP process_sp = valobj.GetProcessSP();
1066 if (!process_sp)
1067 return false;
1068
1069 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1070
1071 if (!runtime)
1072 return false;
1073
1074 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
1075
1076 if (!descriptor.get() || !descriptor->IsValid())
1077 return false;
1078
1079 uint32_t ptr_size = process_sp->GetAddressByteSize();
1080
1081 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
1082
1083 if (!valobj_addr)
1084 return false;
1085
1086 const char* class_name = descriptor->GetClassName().GetCString();
1087
Enrico Granata60b81df2012-09-29 00:45:53 +00001088 if (!class_name || !*class_name)
1089 return false;
1090
Jason Molenda705b1802014-06-13 02:37:02 +00001091 bool is_tagged_ptr = (0 == strcmp(class_name,"NSTaggedPointerString")) && descriptor->GetTaggedPointerInfo();
1092 // for a tagged pointer, the descriptor has everything we need
1093 if (is_tagged_ptr)
1094 return NSTaggedString_SummaryProvider(descriptor, stream);
1095
1096 // if not a tagged pointer that we know about, try the normal route
Enrico Granataf5196282012-09-04 18:48:21 +00001097 uint64_t info_bits_location = valobj_addr + ptr_size;
1098 if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
1099 info_bits_location += 3;
1100
Enrico Granata87f00b42013-02-21 20:31:18 +00001101 Error error;
Enrico Granataf5196282012-09-04 18:48:21 +00001102
1103 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(info_bits_location, 1, 0, error);
1104 if (error.Fail())
1105 return false;
1106
1107 bool is_mutable = (info_bits & 1) == 1;
1108 bool is_inline = (info_bits & 0x60) == 0;
1109 bool has_explicit_length = (info_bits & (1 | 4)) != 4;
1110 bool is_unicode = (info_bits & 0x10) == 0x10;
1111 bool is_special = strcmp(class_name,"NSPathStore2") == 0;
Enrico Granatad83bfce2013-04-02 21:25:34 +00001112 bool has_null = (info_bits & 8) == 8;
1113
1114 size_t explicit_length = 0;
1115 if (!has_null && has_explicit_length && !is_special)
1116 {
1117 lldb::addr_t explicit_length_offset = 2*ptr_size;
Virgile Bellof02a3c52013-08-27 16:24:58 +00001118 if (is_mutable && !is_inline)
Enrico Granatad83bfce2013-04-02 21:25:34 +00001119 explicit_length_offset = explicit_length_offset + ptr_size; // notInlineMutable.length;
1120 else if (is_inline)
1121 explicit_length = explicit_length + 0; // inline1.length;
Virgile Bellof02a3c52013-08-27 16:24:58 +00001122 else if (!is_inline && !is_mutable)
Enrico Granatad83bfce2013-04-02 21:25:34 +00001123 explicit_length_offset = explicit_length_offset + ptr_size; // notInlineImmutable1.length;
1124 else
1125 explicit_length_offset = 0;
1126
1127 if (explicit_length_offset)
1128 {
1129 explicit_length_offset = valobj_addr + explicit_length_offset;
1130 explicit_length = process_sp->ReadUnsignedIntegerFromMemory(explicit_length_offset, 4, 0, error);
1131 }
1132 }
1133
Enrico Granataf5196282012-09-04 18:48:21 +00001134 if (strcmp(class_name,"NSString") &&
1135 strcmp(class_name,"CFStringRef") &&
1136 strcmp(class_name,"CFMutableStringRef") &&
1137 strcmp(class_name,"__NSCFConstantString") &&
1138 strcmp(class_name,"__NSCFString") &&
1139 strcmp(class_name,"NSCFConstantString") &&
1140 strcmp(class_name,"NSCFString") &&
1141 strcmp(class_name,"NSPathStore2"))
1142 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001143 // not one of us - but tell me class name
1144 stream.Printf("class name = %s",class_name);
1145 return true;
Enrico Granataf5196282012-09-04 18:48:21 +00001146 }
1147
1148 if (is_mutable)
1149 {
1150 uint64_t location = 2 * ptr_size + valobj_addr;
1151 location = process_sp->ReadPointerFromMemory(location, error);
1152 if (error.Fail())
1153 return false;
Virgile Bellof02a3c52013-08-27 16:24:58 +00001154 if (has_explicit_length && is_unicode)
Enrico Granata56768392013-04-23 20:05:05 +00001155 {
1156 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
1157 options.SetConversionFunction(ConvertUTF16toUTF8);
1158 options.SetLocation(location);
1159 options.SetProcessSP(process_sp);
1160 options.SetStream(&stream);
1161 options.SetPrefixToken('@');
1162 options.SetQuote('"');
1163 options.SetSourceSize(explicit_length);
1164 options.SetNeedsZeroTermination(false);
1165 return ReadUTFBufferAndDumpToStream (options);
1166 }
Enrico Granataf5196282012-09-04 18:48:21 +00001167 else
Enrico Granatad83bfce2013-04-02 21:25:34 +00001168 return ReadAsciiBufferAndDumpToStream(location+1,process_sp,stream, explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001169 }
1170 else if (is_inline && has_explicit_length && !is_unicode && !is_special && !is_mutable)
1171 {
1172 uint64_t location = 3 * ptr_size + valobj_addr;
Enrico Granatad83bfce2013-04-02 21:25:34 +00001173 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001174 }
1175 else if (is_unicode)
1176 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001177 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +00001178 if (is_inline)
1179 {
1180 if (!has_explicit_length)
1181 {
1182 stream.Printf("found new combo");
1183 return true;
1184 }
1185 else
1186 location += ptr_size;
Enrico Granata56768392013-04-23 20:05:05 +00001187 }
Enrico Granataf5196282012-09-04 18:48:21 +00001188 else
1189 {
1190 location = process_sp->ReadPointerFromMemory(location, error);
1191 if (error.Fail())
1192 return false;
1193 }
Enrico Granata56768392013-04-23 20:05:05 +00001194 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
1195 options.SetConversionFunction(ConvertUTF16toUTF8);
1196 options.SetLocation(location);
1197 options.SetProcessSP(process_sp);
1198 options.SetStream(&stream);
1199 options.SetPrefixToken('@');
1200 options.SetQuote('"');
1201 options.SetSourceSize(explicit_length);
1202 options.SetNeedsZeroTermination(has_explicit_length == false);
1203 return ReadUTFBufferAndDumpToStream (options);
Enrico Granataf5196282012-09-04 18:48:21 +00001204 }
1205 else if (is_special)
1206 {
Enrico Granata76b08d52014-10-29 23:08:02 +00001207 ProcessStructReader reader(valobj.GetProcessSP().get(), valobj.GetValueAsUnsigned(0), GetNSPathStore2Type(*valobj.GetTargetSP()));
1208 explicit_length = reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
1209 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
1210
Enrico Granata56768392013-04-23 20:05:05 +00001211 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
1212 options.SetConversionFunction(ConvertUTF16toUTF8);
1213 options.SetLocation(location);
1214 options.SetProcessSP(process_sp);
1215 options.SetStream(&stream);
1216 options.SetPrefixToken('@');
1217 options.SetQuote('"');
1218 options.SetSourceSize(explicit_length);
1219 options.SetNeedsZeroTermination(has_explicit_length == false);
1220 return ReadUTFBufferAndDumpToStream (options);
Enrico Granataf5196282012-09-04 18:48:21 +00001221 }
1222 else if (is_inline)
1223 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001224 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +00001225 if (!has_explicit_length)
1226 location++;
Enrico Granatad83bfce2013-04-02 21:25:34 +00001227 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001228 }
1229 else
1230 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001231 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +00001232 location = process_sp->ReadPointerFromMemory(location, error);
1233 if (error.Fail())
1234 return false;
Enrico Granatac71f3492013-06-19 19:15:29 +00001235 if (has_explicit_length && !has_null)
1236 explicit_length++; // account for the fact that there is no NULL and we need to have one added
Enrico Granatad83bfce2013-04-02 21:25:34 +00001237 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001238 }
Enrico Granataf5196282012-09-04 18:48:21 +00001239}
1240
Enrico Granataf175ad12012-10-03 23:53:45 +00001241bool
Enrico Granata5bfce362013-02-08 01:55:46 +00001242lldb_private::formatters::NSAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream)
1243{
1244 TargetSP target_sp(valobj.GetTargetSP());
1245 if (!target_sp)
1246 return false;
1247 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton57ee3062013-07-11 22:46:58 +00001248 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
1249 if (!pointer_value)
Enrico Granata5bfce362013-02-08 01:55:46 +00001250 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001251 pointer_value += addr_size;
1252 ClangASTType type(valobj.GetClangType());
Enrico Granata5bfce362013-02-08 01:55:46 +00001253 ExecutionContext exe_ctx(target_sp,false);
Greg Clayton57ee3062013-07-11 22:46:58 +00001254 ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress("string_ptr", pointer_value, exe_ctx, type));
Enrico Granata5bfce362013-02-08 01:55:46 +00001255 if (!child_ptr_sp)
1256 return false;
1257 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +00001258 Error error;
1259 child_ptr_sp->GetData(data, error);
1260 if (error.Fail())
1261 return false;
Enrico Granata5bfce362013-02-08 01:55:46 +00001262 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData("string_data", data, exe_ctx, type));
1263 child_sp->GetValueAsUnsigned(0);
1264 if (child_sp)
1265 return NSStringSummaryProvider(*child_sp, stream);
1266 return false;
1267}
1268
1269bool
1270lldb_private::formatters::NSMutableAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream)
1271{
1272 return NSAttributedStringSummaryProvider(valobj, stream);
1273}
1274
1275bool
Enrico Granataf175ad12012-10-03 23:53:45 +00001276lldb_private::formatters::RuntimeSpecificDescriptionSummaryProvider (ValueObject& valobj, Stream& stream)
1277{
1278 stream.Printf("%s",valobj.GetObjectDescription());
1279 return true;
1280}
1281
Enrico Granatab5887262012-10-29 21:18:03 +00001282bool
1283lldb_private::formatters::ObjCBOOLSummaryProvider (ValueObject& valobj, Stream& stream)
1284{
Greg Clayton57ee3062013-07-11 22:46:58 +00001285 const uint32_t type_info = valobj.GetClangType().GetTypeInfo();
Enrico Granatab5887262012-10-29 21:18:03 +00001286
1287 ValueObjectSP real_guy_sp = valobj.GetSP();
1288
Enrico Granata622be232014-10-21 20:52:14 +00001289 if (type_info & eTypeIsPointer)
Enrico Granatab5887262012-10-29 21:18:03 +00001290 {
1291 Error err;
1292 real_guy_sp = valobj.Dereference(err);
1293 if (err.Fail() || !real_guy_sp)
1294 return false;
1295 }
Enrico Granata622be232014-10-21 20:52:14 +00001296 else if (type_info & eTypeIsReference)
Enrico Granatab5887262012-10-29 21:18:03 +00001297 {
1298 real_guy_sp = valobj.GetChildAtIndex(0, true);
1299 if (!real_guy_sp)
1300 return false;
1301 }
1302 uint64_t value = real_guy_sp->GetValueAsUnsigned(0);
1303 if (value == 0)
1304 {
1305 stream.Printf("NO");
1306 return true;
1307 }
1308 stream.Printf("YES");
1309 return true;
1310}
1311
1312template <bool is_sel_ptr>
1313bool
1314lldb_private::formatters::ObjCSELSummaryProvider (ValueObject& valobj, Stream& stream)
1315{
Enrico Granata75dfb432013-02-15 00:06:04 +00001316 lldb::ValueObjectSP valobj_sp;
Enrico Granatab5887262012-10-29 21:18:03 +00001317
Greg Clayton57ee3062013-07-11 22:46:58 +00001318 ClangASTType charstar (valobj.GetClangType().GetBasicTypeFromAST(eBasicTypeChar).GetPointerType());
1319
1320 if (!charstar)
Enrico Granatab5887262012-10-29 21:18:03 +00001321 return false;
Enrico Granata75dfb432013-02-15 00:06:04 +00001322
Enrico Granatab5887262012-10-29 21:18:03 +00001323 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1324
Enrico Granata75dfb432013-02-15 00:06:04 +00001325 if (is_sel_ptr)
1326 {
1327 lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1328 if (data_address == LLDB_INVALID_ADDRESS)
1329 return false;
1330 valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address, exe_ctx, charstar);
1331 }
1332 else
1333 {
1334 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +00001335 Error error;
1336 valobj.GetData(data, error);
1337 if (error.Fail())
1338 return false;
Enrico Granata75dfb432013-02-15 00:06:04 +00001339 valobj_sp = ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar);
1340 }
Enrico Granatab5887262012-10-29 21:18:03 +00001341
Enrico Granata75dfb432013-02-15 00:06:04 +00001342 if (!valobj_sp)
1343 return false;
Enrico Granatab5887262012-10-29 21:18:03 +00001344
1345 stream.Printf("%s",valobj_sp->GetSummaryAsCString());
1346 return true;
1347}
1348
Enrico Granata6d37cc62013-03-19 00:27:22 +00001349// POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001
1350// this call gives the POSIX equivalent of the Cocoa epoch
1351time_t
Enrico Granata92373532013-03-19 22:58:48 +00001352lldb_private::formatters::GetOSXEpoch ()
Enrico Granata6d37cc62013-03-19 00:27:22 +00001353{
1354 static time_t epoch = 0;
1355 if (!epoch)
1356 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001357#ifndef _WIN32
Enrico Granata6d37cc62013-03-19 00:27:22 +00001358 tzset();
1359 tm tm_epoch;
1360 tm_epoch.tm_sec = 0;
1361 tm_epoch.tm_hour = 0;
1362 tm_epoch.tm_min = 0;
1363 tm_epoch.tm_mon = 0;
1364 tm_epoch.tm_mday = 1;
1365 tm_epoch.tm_year = 2001-1900; // for some reason, we need to subtract 1900 from this field. not sure why.
1366 tm_epoch.tm_isdst = -1;
1367 tm_epoch.tm_gmtoff = 0;
1368 tm_epoch.tm_zone = NULL;
1369 epoch = timegm(&tm_epoch);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001370#endif
Enrico Granata6d37cc62013-03-19 00:27:22 +00001371 }
1372 return epoch;
1373}
1374
Greg Claytonc7bece562013-01-25 18:06:21 +00001375size_t
Enrico Granataf615b802013-02-15 23:38:37 +00001376lldb_private::formatters::ExtractIndexFromString (const char* item_name)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001377{
1378 if (!item_name || !*item_name)
1379 return UINT32_MAX;
1380 if (*item_name != '[')
1381 return UINT32_MAX;
1382 item_name++;
Enrico Granataf615b802013-02-15 23:38:37 +00001383 char* endptr = NULL;
1384 unsigned long int idx = ::strtoul(item_name, &endptr, 0);
1385 if (idx == 0 && endptr == item_name)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001386 return UINT32_MAX;
Enrico Granataf615b802013-02-15 23:38:37 +00001387 if (idx == ULONG_MAX)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001388 return UINT32_MAX;
1389 return idx;
1390}
1391
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001392lldb_private::formatters::VectorIteratorSyntheticFrontEnd::VectorIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp,
1393 ConstString item_name) :
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001394SyntheticChildrenFrontEnd(*valobj_sp.get()),
1395m_exe_ctx_ref(),
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001396m_item_name(item_name),
1397m_item_sp()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001398{
1399 if (valobj_sp)
1400 Update();
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001401}
1402
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001403bool
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001404lldb_private::formatters::VectorIteratorSyntheticFrontEnd::Update()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001405{
Enrico Granata6eca3552013-03-28 18:50:54 +00001406 m_item_sp.reset();
1407
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001408 ValueObjectSP valobj_sp = m_backend.GetSP();
1409 if (!valobj_sp)
1410 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001411
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001412 if (!valobj_sp)
1413 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001414
1415 ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name,true));
1416 if (!item_ptr)
1417 return false;
1418 if (item_ptr->GetValueAsUnsigned(0) == 0)
1419 return false;
1420 Error err;
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001421 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
Greg Clayton57ee3062013-07-11 22:46:58 +00001422 m_item_sp = ValueObject::CreateValueObjectFromAddress("item", item_ptr->GetValueAsUnsigned(0), m_exe_ctx_ref, item_ptr->GetClangType().GetPointeeType());
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001423 if (err.Fail())
1424 m_item_sp.reset();
Enrico Granata6eca3552013-03-28 18:50:54 +00001425 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001426}
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001427
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001428size_t
1429lldb_private::formatters::VectorIteratorSyntheticFrontEnd::CalculateNumChildren ()
1430{
1431 return 1;
1432}
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001433
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001434lldb::ValueObjectSP
1435lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx)
1436{
1437 if (idx == 0)
1438 return m_item_sp;
1439 return lldb::ValueObjectSP();
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001440}
1441
1442bool
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001443lldb_private::formatters::VectorIteratorSyntheticFrontEnd::MightHaveChildren ()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001444{
1445 return true;
1446}
1447
1448size_t
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001449lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001450{
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001451 if (name == ConstString("item"))
1452 return 0;
1453 return UINT32_MAX;
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001454}
1455
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001456lldb_private::formatters::VectorIteratorSyntheticFrontEnd::~VectorIteratorSyntheticFrontEnd ()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001457{
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001458}
1459
Enrico Granataf5196282012-09-04 18:48:21 +00001460template bool
Enrico Granatab2698cd2012-09-13 18:27:09 +00001461lldb_private::formatters::NSDataSummaryProvider<true> (ValueObject&, Stream&) ;
1462
1463template bool
1464lldb_private::formatters::NSDataSummaryProvider<false> (ValueObject&, Stream&) ;
Enrico Granatab5887262012-10-29 21:18:03 +00001465
1466template bool
1467lldb_private::formatters::ObjCSELSummaryProvider<true> (ValueObject&, Stream&) ;
1468
1469template bool
1470lldb_private::formatters::ObjCSELSummaryProvider<false> (ValueObject&, Stream&) ;