blob: ac535ef70f0ddc1d671ba4173110ca5393286a00 [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"
24
Enrico Granatad83bfce2013-04-02 21:25:34 +000025#include <algorithm>
26
Enrico Granataf5196282012-09-04 18:48:21 +000027using namespace lldb;
28using namespace lldb_private;
29using namespace lldb_private::formatters;
30
31bool
Enrico Granatab2698cd2012-09-13 18:27:09 +000032lldb_private::formatters::ExtractValueFromObjCExpression (ValueObject &valobj,
33 const char* target_type,
34 const char* selector,
35 uint64_t &value)
Enrico Granataf5196282012-09-04 18:48:21 +000036{
37 if (!target_type || !*target_type)
38 return false;
39 if (!selector || !*selector)
40 return false;
Enrico Granataf5196282012-09-04 18:48:21 +000041 StreamString expr;
Daniel Maleaa85e6b62012-12-07 22:21:08 +000042 expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector);
Enrico Granataf5196282012-09-04 18:48:21 +000043 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
44 lldb::ValueObjectSP result_sp;
45 Target* target = exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +000046 StackFrame* stack_frame = exe_ctx.GetFramePtr();
Enrico Granataf5196282012-09-04 18:48:21 +000047 if (!target || !stack_frame)
48 return false;
Enrico Granatad4439aa2012-09-05 20:41:26 +000049
Jim Ingham35e1bda2012-10-16 21:41:58 +000050 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +000051 options.SetCoerceToId(false);
52 options.SetUnwindOnError(true);
53 options.SetKeepInMemory(true);
Enrico Granatad4439aa2012-09-05 20:41:26 +000054
Enrico Granataf5196282012-09-04 18:48:21 +000055 target->EvaluateExpression(expr.GetData(),
56 stack_frame,
Enrico Granatad4439aa2012-09-05 20:41:26 +000057 result_sp,
58 options);
Enrico Granataf5196282012-09-04 18:48:21 +000059 if (!result_sp)
60 return false;
61 value = result_sp->GetValueAsUnsigned(0);
62 return true;
63}
64
Enrico Granataf615b802013-02-15 23:38:37 +000065bool
66lldb_private::formatters::ExtractSummaryFromObjCExpression (ValueObject &valobj,
67 const char* target_type,
68 const char* selector,
69 Stream &stream)
70{
71 if (!target_type || !*target_type)
72 return false;
73 if (!selector || !*selector)
74 return false;
75 StreamString expr;
Enrico Granataeac4a482013-02-19 01:14:06 +000076 expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector);
Enrico Granataf615b802013-02-15 23:38:37 +000077 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
78 lldb::ValueObjectSP result_sp;
79 Target* target = exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +000080 StackFrame* stack_frame = exe_ctx.GetFramePtr();
Enrico Granataf615b802013-02-15 23:38:37 +000081 if (!target || !stack_frame)
82 return false;
83
84 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +000085 options.SetCoerceToId(false);
86 options.SetUnwindOnError(true);
87 options.SetKeepInMemory(true);
88 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granataf615b802013-02-15 23:38:37 +000089
90 target->EvaluateExpression(expr.GetData(),
91 stack_frame,
92 result_sp,
93 options);
94 if (!result_sp)
95 return false;
96 stream.Printf("%s",result_sp->GetSummaryAsCString());
97 return true;
98}
99
Enrico Granatab2698cd2012-09-13 18:27:09 +0000100lldb::ValueObjectSP
101lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj,
102 const char* return_type,
103 const char* selector,
104 uint64_t index)
105{
106 lldb::ValueObjectSP valobj_sp;
107 if (!return_type || !*return_type)
108 return valobj_sp;
109 if (!selector || !*selector)
110 return valobj_sp;
111 StreamString expr_path_stream;
112 valobj.GetExpressionPath(expr_path_stream, false);
113 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +0000114 expr.Printf("(%s)[%s %s:%" PRId64 "]",return_type,expr_path_stream.GetData(),selector,index);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000115 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
116 lldb::ValueObjectSP result_sp;
117 Target* target = exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000118 StackFrame* stack_frame = exe_ctx.GetFramePtr();
Enrico Granatab2698cd2012-09-13 18:27:09 +0000119 if (!target || !stack_frame)
120 return valobj_sp;
121
Jim Ingham35e1bda2012-10-16 21:41:58 +0000122 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000123 options.SetCoerceToId(false);
124 options.SetUnwindOnError(true);
125 options.SetKeepInMemory(true);
126 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000127
128 target->EvaluateExpression(expr.GetData(),
129 stack_frame,
130 valobj_sp,
131 options);
132 return valobj_sp;
133}
134
135lldb::ValueObjectSP
136lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj,
137 const char* return_type,
138 const char* selector,
139 const char* key)
140{
141 lldb::ValueObjectSP valobj_sp;
142 if (!return_type || !*return_type)
143 return valobj_sp;
144 if (!selector || !*selector)
145 return valobj_sp;
146 if (!key || !*key)
147 return valobj_sp;
148 StreamString expr_path_stream;
149 valobj.GetExpressionPath(expr_path_stream, false);
150 StreamString expr;
151 expr.Printf("(%s)[%s %s:%s]",return_type,expr_path_stream.GetData(),selector,key);
152 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
153 lldb::ValueObjectSP result_sp;
154 Target* target = exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000155 StackFrame* stack_frame = exe_ctx.GetFramePtr();
Enrico Granatab2698cd2012-09-13 18:27:09 +0000156 if (!target || !stack_frame)
157 return valobj_sp;
158
Jim Ingham35e1bda2012-10-16 21:41:58 +0000159 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000160 options.SetCoerceToId(false);
161 options.SetUnwindOnError(true);
162 options.SetKeepInMemory(true);
163 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000164
165 target->EvaluateExpression(expr.GetData(),
166 stack_frame,
167 valobj_sp,
168 options);
169 return valobj_sp;
170}
171
Enrico Granata93d59662013-01-14 23:53:26 +0000172// use this call if you already have an LLDB-side buffer for the data
173template<typename SourceDataType>
Enrico Granataf68df122013-01-10 22:08:35 +0000174static bool
Enrico Granata93d59662013-01-14 23:53:26 +0000175DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**,
176 const SourceDataType*,
177 UTF8**,
178 UTF8*,
179 ConversionFlags),
180 DataExtractor& data,
181 Stream& stream,
182 char prefix_token = '@',
183 char quote = '"',
Enrico Granatad83bfce2013-04-02 21:25:34 +0000184 uint32_t sourceSize = 0)
Enrico Granataf68df122013-01-10 22:08:35 +0000185{
Enrico Granata93d59662013-01-14 23:53:26 +0000186 if (prefix_token != 0)
187 stream.Printf("%c",prefix_token);
188 if (quote != 0)
189 stream.Printf("%c",quote);
190 if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd())
Enrico Granataf68df122013-01-10 22:08:35 +0000191 {
Enrico Granata93d59662013-01-14 23:53:26 +0000192 const int bufferSPSize = data.GetByteSize();
193 if (sourceSize == 0)
194 {
195 const int origin_encoding = 8*sizeof(SourceDataType);
Greg Claytonaa4c47a2013-02-08 21:59:34 +0000196 sourceSize = bufferSPSize/(origin_encoding / 4);
Enrico Granata93d59662013-01-14 23:53:26 +0000197 }
198
199 SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart();
Enrico Granataf68df122013-01-10 22:08:35 +0000200 SourceDataType *data_end_ptr = data_ptr + sourceSize;
201
202 while (data_ptr < data_end_ptr)
203 {
204 if (!*data_ptr)
205 {
206 data_end_ptr = data_ptr;
207 break;
208 }
209 data_ptr++;
210 }
211
Enrico Granata93d59662013-01-14 23:53:26 +0000212 data_ptr = (SourceDataType*)data.GetDataStart();
Enrico Granataf68df122013-01-10 22:08:35 +0000213
Enrico Granata38352042013-01-11 02:44:00 +0000214 lldb::DataBufferSP utf8_data_buffer_sp;
215 UTF8* utf8_data_ptr = nullptr;
216 UTF8* utf8_data_end_ptr = nullptr;
Enrico Granata93d59662013-01-14 23:53:26 +0000217
Enrico Granataf68df122013-01-10 22:08:35 +0000218 if (ConvertFunction)
Enrico Granata38352042013-01-11 02:44:00 +0000219 {
Enrico Granata56768392013-04-23 20:05:05 +0000220 utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0));
Enrico Granata38352042013-01-11 02:44:00 +0000221 utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes();
Enrico Granata56768392013-04-23 20:05:05 +0000222 utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
Enrico Granataf68df122013-01-10 22:08:35 +0000223 ConvertFunction ( (const SourceDataType**)&data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion );
Enrico Granata38352042013-01-11 02:44:00 +0000224 utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr
225 }
Enrico Granataf68df122013-01-10 22:08:35 +0000226 else
227 {
228 // just copy the pointers - the cast is necessary to make the compiler happy
229 // but this should only happen if we are reading UTF8 data
230 utf8_data_ptr = (UTF8*)data_ptr;
231 utf8_data_end_ptr = (UTF8*)data_end_ptr;
232 }
233
Enrico Granata3309d882013-01-12 01:00:22 +0000234 // since we tend to accept partial data (and even partially malformed data)
235 // we might end up with no NULL terminator before the end_ptr
236 // hence we need to take a slower route and ensure we stay within boundaries
Enrico Granataf68df122013-01-10 22:08:35 +0000237 for (;utf8_data_ptr != utf8_data_end_ptr; utf8_data_ptr++)
238 {
239 if (!*utf8_data_ptr)
240 break;
241 stream.Printf("%c",*utf8_data_ptr);
242 }
Enrico Granata93d59662013-01-14 23:53:26 +0000243 }
244 if (quote != 0)
245 stream.Printf("%c",quote);
246 return true;
247}
248
249template<typename SourceDataType>
Enrico Granata56768392013-04-23 20:05:05 +0000250class ReadUTFBufferAndDumpToStreamOptions
Enrico Granata93d59662013-01-14 23:53:26 +0000251{
Enrico Granata56768392013-04-23 20:05:05 +0000252public:
253 typedef ConversionResult (*ConvertFunctionType) (const SourceDataType**,
254 const SourceDataType*,
255 UTF8**,
256 UTF8*,
257 ConversionFlags);
258
259 ReadUTFBufferAndDumpToStreamOptions () :
260 m_conversion_function(NULL),
261 m_location(0),
262 m_process_sp(),
263 m_stream(NULL),
264 m_prefix_token('@'),
265 m_quote('"'),
266 m_source_size(0),
267 m_needs_zero_termination(true)
268 {
269 }
270
271 ReadUTFBufferAndDumpToStreamOptions&
272 SetConversionFunction (ConvertFunctionType f)
273 {
274 m_conversion_function = f;
275 return *this;
276 }
277
278 ConvertFunctionType
279 GetConversionFunction () const
280 {
281 return m_conversion_function;
282 }
283
284 ReadUTFBufferAndDumpToStreamOptions&
285 SetLocation (uint64_t l)
286 {
287 m_location = l;
288 return *this;
289 }
290
291 uint64_t
292 GetLocation () const
293 {
294 return m_location;
295 }
296
297 ReadUTFBufferAndDumpToStreamOptions&
298 SetProcessSP (ProcessSP p)
299 {
300 m_process_sp = p;
301 return *this;
302 }
303
304 ProcessSP
305 GetProcessSP () const
306 {
307 return m_process_sp;
308 }
309
310 ReadUTFBufferAndDumpToStreamOptions&
311 SetStream (Stream* s)
312 {
313 m_stream = s;
314 return *this;
315 }
316
317 Stream*
318 GetStream () const
319 {
320 return m_stream;
321 }
322
323 ReadUTFBufferAndDumpToStreamOptions&
324 SetPrefixToken (char p)
325 {
326 m_prefix_token = p;
327 return *this;
328 }
329
330 char
331 GetPrefixToken () const
332 {
333 return m_prefix_token;
334 }
335
336 ReadUTFBufferAndDumpToStreamOptions&
337 SetQuote (char q)
338 {
339 m_quote = q;
340 return *this;
341 }
342
343 char
344 GetQuote () const
345 {
346 return m_quote;
347 }
348
349 ReadUTFBufferAndDumpToStreamOptions&
350 SetSourceSize (uint32_t s)
351 {
352 m_source_size = s;
353 return *this;
354 }
355
356 uint32_t
357 GetSourceSize () const
358 {
359 return m_source_size;
360 }
361
362 ReadUTFBufferAndDumpToStreamOptions&
363 SetNeedsZeroTermination (bool z)
364 {
365 m_needs_zero_termination = z;
366 return *this;
367 }
368
369 bool
370 GetNeedsZeroTermination () const
371 {
372 return m_needs_zero_termination;
373 }
374
375private:
376 ConvertFunctionType m_conversion_function;
377 uint64_t m_location;
378 ProcessSP m_process_sp;
379 Stream* m_stream;
380 char m_prefix_token;
381 char m_quote;
382 uint32_t m_source_size;
383 bool m_needs_zero_termination;
384};
385
386template<typename SourceDataType>
387static bool
388ReadUTFBufferAndDumpToStream (const ReadUTFBufferAndDumpToStreamOptions<SourceDataType>& options)
389{
390 if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS)
Enrico Granata93d59662013-01-14 23:53:26 +0000391 return false;
Enrico Granata56768392013-04-23 20:05:05 +0000392
393 ProcessSP process_sp(options.GetProcessSP());
394
Enrico Granata93d59662013-01-14 23:53:26 +0000395 if (!process_sp)
396 return false;
397
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +0000398 const int type_width = sizeof(SourceDataType);
399 const int origin_encoding = 8 * type_width ;
Enrico Granata93d59662013-01-14 23:53:26 +0000400 if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
401 return false;
402 // if not UTF8, I need a conversion function to return proper UTF8
Enrico Granata56768392013-04-23 20:05:05 +0000403 if (origin_encoding != 8 && !options.GetConversionFunction())
404 return false;
405
406 if (!options.GetStream())
Enrico Granata93d59662013-01-14 23:53:26 +0000407 return false;
408
Enrico Granata56768392013-04-23 20:05:05 +0000409 uint32_t sourceSize = options.GetSourceSize();
410 bool needs_zero_terminator = options.GetNeedsZeroTermination();
411
Enrico Granata23ab35a2013-04-02 23:07:55 +0000412 if (!sourceSize)
Enrico Granata56768392013-04-23 20:05:05 +0000413 {
Enrico Granata23ab35a2013-04-02 23:07:55 +0000414 sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
Enrico Granata56768392013-04-23 20:05:05 +0000415 needs_zero_terminator = true;
416 }
Enrico Granata23ab35a2013-04-02 23:07:55 +0000417 else
418 sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
419
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +0000420 const int bufferSPSize = sourceSize * type_width;
Enrico Granata93d59662013-01-14 23:53:26 +0000421
Enrico Granata93d59662013-01-14 23:53:26 +0000422 lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0));
423
424 if (!buffer_sp->GetBytes())
425 return false;
426
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +0000427 Error error;
428 char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
429
Enrico Granata56768392013-04-23 20:05:05 +0000430 size_t data_read = 0;
431 if (needs_zero_terminator)
432 data_read = process_sp->ReadStringFromMemory(options.GetLocation(), buffer, bufferSPSize, error, type_width);
433 else
434 data_read = process_sp->ReadMemoryFromInferior(options.GetLocation(), (char*)buffer_sp->GetBytes(), bufferSPSize, error);
435
Enrico Granata93d59662013-01-14 23:53:26 +0000436 if (error.Fail() || data_read == 0)
437 {
Enrico Granata56768392013-04-23 20:05:05 +0000438 options.GetStream()->Printf("unable to read data");
Enrico Granataf68df122013-01-10 22:08:35 +0000439 return true;
440 }
Enrico Granata93d59662013-01-14 23:53:26 +0000441
442 DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize());
443
Enrico Granata56768392013-04-23 20:05:05 +0000444 return DumpUTFBufferToStream(options.GetConversionFunction(), data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize);
Enrico Granataf68df122013-01-10 22:08:35 +0000445}
446
447bool
448lldb_private::formatters::Char16StringSummaryProvider (ValueObject& valobj, Stream& stream)
449{
450 ProcessSP process_sp = valobj.GetProcessSP();
451 if (!process_sp)
452 return false;
453
454 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
455
456 if (!valobj_addr)
457 return false;
458
Enrico Granata56768392013-04-23 20:05:05 +0000459 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
460 options.SetLocation(valobj_addr);
461 options.SetConversionFunction(ConvertUTF16toUTF8);
462 options.SetProcessSP(process_sp);
463 options.SetStream(&stream);
464 options.SetPrefixToken('u');
465
466 if (!ReadUTFBufferAndDumpToStream(options))
Enrico Granataf68df122013-01-10 22:08:35 +0000467 {
468 stream.Printf("Summary Unavailable");
469 return true;
470 }
471
472 return true;
473}
474
475bool
476lldb_private::formatters::Char32StringSummaryProvider (ValueObject& valobj, Stream& stream)
477{
478 ProcessSP process_sp = valobj.GetProcessSP();
479 if (!process_sp)
480 return false;
481
482 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
483
484 if (!valobj_addr)
485 return false;
486
Enrico Granata56768392013-04-23 20:05:05 +0000487 ReadUTFBufferAndDumpToStreamOptions<UTF32> options;
488 options.SetLocation(valobj_addr);
489 options.SetConversionFunction(ConvertUTF32toUTF8);
490 options.SetProcessSP(process_sp);
491 options.SetStream(&stream);
Enrico Granatac03c5862013-04-23 21:37:33 +0000492 options.SetPrefixToken('U');
Enrico Granata56768392013-04-23 20:05:05 +0000493
494 if (!ReadUTFBufferAndDumpToStream(options))
Enrico Granataf68df122013-01-10 22:08:35 +0000495 {
496 stream.Printf("Summary Unavailable");
497 return true;
498 }
499
500 return true;
501}
502
503bool
504lldb_private::formatters::WCharStringSummaryProvider (ValueObject& valobj, Stream& stream)
505{
Enrico Granata38352042013-01-11 02:44:00 +0000506 ProcessSP process_sp = valobj.GetProcessSP();
507 if (!process_sp)
508 return false;
509
Enrico Granata3309d882013-01-12 01:00:22 +0000510 lldb::addr_t data_addr = 0;
511
512 if (valobj.IsPointerType())
513 data_addr = valobj.GetValueAsUnsigned(0);
514 else if (valobj.IsArrayType())
515 data_addr = valobj.GetAddressOf();
Enrico Granata38352042013-01-11 02:44:00 +0000516
Enrico Granata3309d882013-01-12 01:00:22 +0000517 if (data_addr == 0 || data_addr == LLDB_INVALID_ADDRESS)
Enrico Granata38352042013-01-11 02:44:00 +0000518 return false;
519
Greg Clayton57ee3062013-07-11 22:46:58 +0000520 clang::ASTContext* ast = valobj.GetClangType().GetASTContext();
521
Enrico Granata38352042013-01-11 02:44:00 +0000522 if (!ast)
523 return false;
524
Greg Clayton57ee3062013-07-11 22:46:58 +0000525 ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
526 const uint32_t wchar_size = wchar_clang_type.GetBitSize();
Enrico Granata38352042013-01-11 02:44:00 +0000527
528 switch (wchar_size)
529 {
530 case 8:
Enrico Granata56768392013-04-23 20:05:05 +0000531 {
Enrico Granata38352042013-01-11 02:44:00 +0000532 // utf 8
Enrico Granata56768392013-04-23 20:05:05 +0000533
534 ReadUTFBufferAndDumpToStreamOptions<UTF8> options;
535 options.SetLocation(data_addr);
536 options.SetConversionFunction(nullptr);
537 options.SetProcessSP(process_sp);
538 options.SetStream(&stream);
539 options.SetPrefixToken('L');
540
541 return ReadUTFBufferAndDumpToStream(options);
542 }
Enrico Granata38352042013-01-11 02:44:00 +0000543 case 16:
Enrico Granata56768392013-04-23 20:05:05 +0000544 {
Enrico Granata38352042013-01-11 02:44:00 +0000545 // utf 16
Enrico Granata56768392013-04-23 20:05:05 +0000546 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
547 options.SetLocation(data_addr);
548 options.SetConversionFunction(ConvertUTF16toUTF8);
549 options.SetProcessSP(process_sp);
550 options.SetStream(&stream);
551 options.SetPrefixToken('L');
552
553 return ReadUTFBufferAndDumpToStream(options);
554 }
Enrico Granata38352042013-01-11 02:44:00 +0000555 case 32:
Enrico Granata56768392013-04-23 20:05:05 +0000556 {
Enrico Granata38352042013-01-11 02:44:00 +0000557 // utf 32
Enrico Granata56768392013-04-23 20:05:05 +0000558 ReadUTFBufferAndDumpToStreamOptions<UTF32> options;
559 options.SetLocation(data_addr);
560 options.SetConversionFunction(ConvertUTF32toUTF8);
561 options.SetProcessSP(process_sp);
562 options.SetStream(&stream);
563 options.SetPrefixToken('L');
564
565 return ReadUTFBufferAndDumpToStream(options);
566 }
Enrico Granata93d59662013-01-14 23:53:26 +0000567 default:
568 stream.Printf("size for wchar_t is not valid");
569 return true;
570 }
571 return true;
572}
573
574bool
575lldb_private::formatters::Char16SummaryProvider (ValueObject& valobj, Stream& stream)
576{
577 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000578 Error error;
579 valobj.GetData(data, error);
580
581 if (error.Fail())
582 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000583
584 std::string value;
585 valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
586 if (!value.empty())
587 stream.Printf("%s ", value.c_str());
588
589 return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,data,stream, 'u','\'',1);
590}
591
592bool
593lldb_private::formatters::Char32SummaryProvider (ValueObject& valobj, Stream& stream)
594{
595 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000596 Error error;
597 valobj.GetData(data, error);
598
599 if (error.Fail())
600 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000601
602 std::string value;
603 valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
604 if (!value.empty())
605 stream.Printf("%s ", value.c_str());
606
607 return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,data,stream, 'U','\'',1);
608}
609
610bool
611lldb_private::formatters::WCharSummaryProvider (ValueObject& valobj, Stream& stream)
612{
613 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000614 Error error;
615 valobj.GetData(data, error);
616
617 if (error.Fail())
618 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000619
Greg Clayton57ee3062013-07-11 22:46:58 +0000620 clang::ASTContext* ast = valobj.GetClangType().GetASTContext();
Enrico Granata93d59662013-01-14 23:53:26 +0000621
622 if (!ast)
623 return false;
624
Greg Clayton57ee3062013-07-11 22:46:58 +0000625 ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
626 const uint32_t wchar_size = wchar_clang_type.GetBitSize();
Enrico Granata93d59662013-01-14 23:53:26 +0000627 std::string value;
628
Enrico Granata93d59662013-01-14 23:53:26 +0000629 switch (wchar_size)
630 {
631 case 8:
632 // utf 8
633 valobj.GetValueAsCString(lldb::eFormatChar, value);
634 if (!value.empty())
635 stream.Printf("%s ", value.c_str());
636 return DumpUTFBufferToStream<UTF8>(nullptr,
637 data,
638 stream,
639 'L',
640 '\'',
641 1);
642 case 16:
643 // utf 16
644 valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
645 if (!value.empty())
646 stream.Printf("%s ", value.c_str());
647 return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,
648 data,
649 stream,
650 'L',
651 '\'',
652 1);
653 case 32:
654 // utf 32
655 valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
656 if (!value.empty())
657 stream.Printf("%s ", value.c_str());
658 return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,
659 data,
660 stream,
661 'L',
662 '\'',
663 1);
Enrico Granata38352042013-01-11 02:44:00 +0000664 default:
665 stream.Printf("size for wchar_t is not valid");
666 return true;
667 }
668 return true;
Enrico Granataf68df122013-01-10 22:08:35 +0000669}
670
Enrico Granataa3962a72013-05-15 00:47:46 +0000671// the field layout in a libc++ string (cap, side, data or data, size, cap)
672enum LibcxxStringLayoutMode
673{
674 eLibcxxStringLayoutModeCSD = 0,
675 eLibcxxStringLayoutModeDSC = 1,
676 eLibcxxStringLayoutModeInvalid = 0xffff
677};
678
679// this function abstracts away the layout and mode details of a libc++ string
680// and returns the address of the data and the size ready for callers to consume
Enrico Granata3309d882013-01-12 01:00:22 +0000681static bool
682ExtractLibcxxStringInfo (ValueObject& valobj,
683 ValueObjectSP &location_sp,
684 uint64_t& size)
685{
686 ValueObjectSP D(valobj.GetChildAtIndexPath({0,0,0,0}));
687 if (!D)
688 return false;
689
Enrico Granataa3962a72013-05-15 00:47:46 +0000690 ValueObjectSP layout_decider(D->GetChildAtIndexPath({0,0}));
691
692 // this child should exist
693 if (!layout_decider)
Enrico Granata3309d882013-01-12 01:00:22 +0000694 return false;
695
Enrico Granataa3962a72013-05-15 00:47:46 +0000696 ConstString g_data_name("__data_");
697 ConstString g_size_name("__size_");
698 bool short_mode = false; // this means the string is in short-mode and the data is stored inline
699 LibcxxStringLayoutMode layout = (layout_decider->GetName() == g_data_name) ? eLibcxxStringLayoutModeDSC : eLibcxxStringLayoutModeCSD;
700 uint64_t size_mode_value = 0;
Enrico Granata3309d882013-01-12 01:00:22 +0000701
Enrico Granataa3962a72013-05-15 00:47:46 +0000702 if (layout == eLibcxxStringLayoutModeDSC)
703 {
704 ValueObjectSP size_mode(D->GetChildAtIndexPath({1,1,0}));
705 if (!size_mode)
706 return false;
707
708 if (size_mode->GetName() != g_size_name)
709 {
710 // we are hitting the padding structure, move along
711 size_mode = D->GetChildAtIndexPath({1,1,1});
712 if (!size_mode)
713 return false;
714 }
715
716 size_mode_value = (size_mode->GetValueAsUnsigned(0));
717 short_mode = ((size_mode_value & 0x80) == 0);
718 }
719 else
720 {
721 ValueObjectSP size_mode(D->GetChildAtIndexPath({1,0,0}));
722 if (!size_mode)
723 return false;
724
725 size_mode_value = (size_mode->GetValueAsUnsigned(0));
726 short_mode = ((size_mode_value & 1) == 0);
727 }
728
729 if (short_mode)
Enrico Granata3309d882013-01-12 01:00:22 +0000730 {
731 ValueObjectSP s(D->GetChildAtIndex(1, true));
732 if (!s)
733 return false;
Enrico Granataa3962a72013-05-15 00:47:46 +0000734 location_sp = s->GetChildAtIndex((layout == eLibcxxStringLayoutModeDSC) ? 0 : 1, true);
735 size = (layout == eLibcxxStringLayoutModeDSC) ? size_mode_value : ((size_mode_value >> 1) % 256);
Enrico Granata3309d882013-01-12 01:00:22 +0000736 return (location_sp.get() != nullptr);
737 }
738 else
739 {
740 ValueObjectSP l(D->GetChildAtIndex(0, true));
741 if (!l)
742 return false;
Enrico Granataa3962a72013-05-15 00:47:46 +0000743 // we can use the layout_decider object as the data pointer
744 location_sp = (layout == eLibcxxStringLayoutModeDSC) ? layout_decider : l->GetChildAtIndex(2, true);
Enrico Granata3309d882013-01-12 01:00:22 +0000745 ValueObjectSP size_vo(l->GetChildAtIndex(1, true));
746 if (!size_vo || !location_sp)
747 return false;
748 size = size_vo->GetValueAsUnsigned(0);
749 return true;
750 }
751}
752
753bool
754lldb_private::formatters::LibcxxWStringSummaryProvider (ValueObject& valobj, Stream& stream)
755{
756 uint64_t size = 0;
757 ValueObjectSP location_sp((ValueObject*)nullptr);
758 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
759 return false;
760 if (size == 0)
761 {
762 stream.Printf("L\"\"");
763 return true;
Enrico Granataa3962a72013-05-15 00:47:46 +0000764 }
Enrico Granata3309d882013-01-12 01:00:22 +0000765 if (!location_sp)
766 return false;
767 return WCharStringSummaryProvider(*location_sp.get(), stream);
768}
769
770bool
771lldb_private::formatters::LibcxxStringSummaryProvider (ValueObject& valobj, Stream& stream)
772{
773 uint64_t size = 0;
774 ValueObjectSP location_sp((ValueObject*)nullptr);
775 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
776 return false;
777 if (size == 0)
778 {
779 stream.Printf("\"\"");
780 return true;
781 }
782 if (!location_sp)
783 return false;
784 Error error;
Enrico Granataea2bc0f2013-02-21 19:57:10 +0000785 if (location_sp->ReadPointedString(stream,
786 error,
787 0, // max length is decided by the settings
788 false) == 0) // do not honor array (terminates on first 0 byte even for a char[])
789 stream.Printf("\"\""); // if nothing was read, print an empty string
Enrico Granata3309d882013-01-12 01:00:22 +0000790 return error.Success();
791}
792
Enrico Granata55900862013-03-15 18:55:30 +0000793bool
794lldb_private::formatters::ObjCClassSummaryProvider (ValueObject& valobj, Stream& stream)
795{
796 ProcessSP process_sp = valobj.GetProcessSP();
797 if (!process_sp)
798 return false;
799
800 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
801
802 if (!runtime)
803 return false;
804
Greg Clayton03da4cc2013-04-19 21:31:16 +0000805 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0)));
Enrico Granata55900862013-03-15 18:55:30 +0000806
807 if (!descriptor.get() || !descriptor->IsValid())
808 return false;
809
810 const char* class_name = descriptor->GetClassName().GetCString();
811
812 if (!class_name || !*class_name)
813 return false;
814
815 stream.Printf("%s",class_name);
816 return true;
817}
818
Enrico Granatac76b97b2013-04-26 00:59:02 +0000819class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd
820{
821public:
822 ObjCClassSyntheticChildrenFrontEnd (lldb::ValueObjectSP valobj_sp) :
823 SyntheticChildrenFrontEnd(*valobj_sp.get())
824 {
825 }
826
827 virtual size_t
828 CalculateNumChildren ()
829 {
830 return 0;
831 }
832
833 virtual lldb::ValueObjectSP
834 GetChildAtIndex (size_t idx)
835 {
836 return lldb::ValueObjectSP();
837 }
838
839 virtual bool
840 Update()
841 {
842 return false;
843 }
844
845 virtual bool
846 MightHaveChildren ()
847 {
848 return false;
849 }
850
851 virtual size_t
852 GetIndexOfChildWithName (const ConstString &name)
853 {
854 return UINT32_MAX;
855 }
856
857 virtual
858 ~ObjCClassSyntheticChildrenFrontEnd ()
859 {
860 }
861};
862
863SyntheticChildrenFrontEnd*
864lldb_private::formatters::ObjCClassSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
865{
866 return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp);
867}
868
Enrico Granataf5196282012-09-04 18:48:21 +0000869template<bool needs_at>
870bool
Enrico Granatab2698cd2012-09-13 18:27:09 +0000871lldb_private::formatters::NSDataSummaryProvider (ValueObject& valobj, Stream& stream)
Enrico Granataf5196282012-09-04 18:48:21 +0000872{
873 ProcessSP process_sp = valobj.GetProcessSP();
874 if (!process_sp)
875 return false;
876
877 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
878
879 if (!runtime)
880 return false;
881
882 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
883
884 if (!descriptor.get() || !descriptor->IsValid())
885 return false;
886
887 bool is_64bit = (process_sp->GetAddressByteSize() == 8);
888 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
889
890 if (!valobj_addr)
891 return false;
892
893 uint64_t value = 0;
894
895 const char* class_name = descriptor->GetClassName().GetCString();
Enrico Granata6d390772012-09-29 00:47:43 +0000896
897 if (!class_name || !*class_name)
898 return false;
899
Enrico Granataf5196282012-09-04 18:48:21 +0000900 if (!strcmp(class_name,"NSConcreteData") ||
901 !strcmp(class_name,"NSConcreteMutableData") ||
902 !strcmp(class_name,"__NSCFData"))
903 {
904 uint32_t offset = (is_64bit ? 16 : 8);
905 Error error;
906 value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, is_64bit ? 8 : 4, 0, error);
907 if (error.Fail())
908 return false;
909 }
910 else
911 {
Enrico Granatab2698cd2012-09-13 18:27:09 +0000912 if (!ExtractValueFromObjCExpression(valobj, "int", "length", value))
Enrico Granataf5196282012-09-04 18:48:21 +0000913 return false;
914 }
915
Daniel Malead01b2952012-11-29 21:49:15 +0000916 stream.Printf("%s%" PRIu64 " byte%s%s",
Enrico Granataf5196282012-09-04 18:48:21 +0000917 (needs_at ? "@\"" : ""),
918 value,
Enrico Granata1c333d02013-05-31 01:14:22 +0000919 (value != 1 ? "s" : ""),
Enrico Granataf5196282012-09-04 18:48:21 +0000920 (needs_at ? "\"" : ""));
921
922 return true;
923}
924
Enrico Granata87f00b42013-02-21 20:31:18 +0000925static bool
926ReadAsciiBufferAndDumpToStream (lldb::addr_t location,
927 lldb::ProcessSP& process_sp,
928 Stream& dest,
Enrico Granatad83bfce2013-04-02 21:25:34 +0000929 uint32_t size = 0,
Enrico Granata87f00b42013-02-21 20:31:18 +0000930 Error* error = NULL,
931 size_t *data_read = NULL,
932 char prefix_token = '@',
933 char quote = '"')
934{
935 Error my_error;
936 size_t my_data_read;
937 if (!process_sp || location == 0)
938 return false;
939
Enrico Granata23ab35a2013-04-02 23:07:55 +0000940 if (!size)
941 size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
942 else
943 size = std::min(size,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
Enrico Granata87f00b42013-02-21 20:31:18 +0000944
945 lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0));
946
947 my_data_read = process_sp->ReadCStringFromMemory(location, (char*)buffer_sp->GetBytes(), size, my_error);
948
949 if (error)
950 *error = my_error;
951 if (data_read)
952 *data_read = my_data_read;
953
954 if (my_error.Fail())
955 return false;
Enrico Granata5a9c4fe2013-05-17 23:28:13 +0000956
957 dest.Printf("%c%c",prefix_token,quote);
958
Enrico Granata87f00b42013-02-21 20:31:18 +0000959 if (my_data_read)
Enrico Granata5a9c4fe2013-05-17 23:28:13 +0000960 dest.Printf("%s",(char*)buffer_sp->GetBytes());
961
962 dest.Printf("%c",quote);
Enrico Granata87f00b42013-02-21 20:31:18 +0000963
964 return true;
965}
966
Enrico Granataf5196282012-09-04 18:48:21 +0000967bool
Jason Molenda705b1802014-06-13 02:37:02 +0000968lldb_private::formatters::NSTaggedString_SummaryProvider (ObjCLanguageRuntime::ClassDescriptorSP descriptor, Stream& stream)
969{
970 if (!descriptor)
971 return false;
972 uint64_t len_bits = 0, data_bits = 0;
973 if (!descriptor->GetTaggedPointerInfo(&len_bits,&data_bits,nullptr))
974 return false;
975
976 static const int g_MaxNonBitmaskedLen = 7; //TAGGED_STRING_UNPACKED_MAXLEN
977 static const int g_SixbitMaxLen = 9;
978 static const int g_fiveBitMaxLen = 11;
979
980 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013" "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
981
982 if (len_bits > g_fiveBitMaxLen)
983 return false;
984
985 // this is a fairly ugly trick - pretend that the numeric value is actually a char*
986 // this works under a few assumptions:
987 // little endian architecture
988 // sizeof(uint64_t) > g_MaxNonBitmaskedLen
989 if (len_bits <= g_MaxNonBitmaskedLen)
990 {
991 stream.Printf("@\"%s\"",(const char*)&data_bits);
992 return true;
993 }
994
995 // if the data is bitmasked, we need to actually process the bytes
996 uint8_t bitmask = 0;
997 uint8_t shift_offset = 0;
998
999 if (len_bits <= g_SixbitMaxLen)
1000 {
1001 bitmask = 0x03f;
1002 shift_offset = 6;
1003 }
1004 else
1005 {
1006 bitmask = 0x01f;
1007 shift_offset = 5;
1008 }
1009
1010 std::vector<uint8_t> bytes;
1011 bytes.resize(len_bits);
1012 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits)
1013 {
1014 uint8_t packed = data_bits & bitmask;
1015 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
1016 }
1017
1018 stream.Printf("@\"%s\"",&bytes[0]);
1019 return true;
1020}
1021
1022bool
Enrico Granatab2698cd2012-09-13 18:27:09 +00001023lldb_private::formatters::NSStringSummaryProvider (ValueObject& valobj, Stream& stream)
Enrico Granataf5196282012-09-04 18:48:21 +00001024{
1025 ProcessSP process_sp = valobj.GetProcessSP();
1026 if (!process_sp)
1027 return false;
1028
1029 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1030
1031 if (!runtime)
1032 return false;
1033
1034 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
1035
1036 if (!descriptor.get() || !descriptor->IsValid())
1037 return false;
1038
1039 uint32_t ptr_size = process_sp->GetAddressByteSize();
1040
1041 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
1042
1043 if (!valobj_addr)
1044 return false;
1045
1046 const char* class_name = descriptor->GetClassName().GetCString();
1047
Enrico Granata60b81df2012-09-29 00:45:53 +00001048 if (!class_name || !*class_name)
1049 return false;
1050
Jason Molenda705b1802014-06-13 02:37:02 +00001051 bool is_tagged_ptr = (0 == strcmp(class_name,"NSTaggedPointerString")) && descriptor->GetTaggedPointerInfo();
1052 // for a tagged pointer, the descriptor has everything we need
1053 if (is_tagged_ptr)
1054 return NSTaggedString_SummaryProvider(descriptor, stream);
1055
1056 // if not a tagged pointer that we know about, try the normal route
Enrico Granataf5196282012-09-04 18:48:21 +00001057 uint64_t info_bits_location = valobj_addr + ptr_size;
1058 if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
1059 info_bits_location += 3;
1060
Enrico Granata87f00b42013-02-21 20:31:18 +00001061 Error error;
Enrico Granataf5196282012-09-04 18:48:21 +00001062
1063 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(info_bits_location, 1, 0, error);
1064 if (error.Fail())
1065 return false;
1066
1067 bool is_mutable = (info_bits & 1) == 1;
1068 bool is_inline = (info_bits & 0x60) == 0;
1069 bool has_explicit_length = (info_bits & (1 | 4)) != 4;
1070 bool is_unicode = (info_bits & 0x10) == 0x10;
1071 bool is_special = strcmp(class_name,"NSPathStore2") == 0;
Enrico Granatad83bfce2013-04-02 21:25:34 +00001072 bool has_null = (info_bits & 8) == 8;
1073
1074 size_t explicit_length = 0;
1075 if (!has_null && has_explicit_length && !is_special)
1076 {
1077 lldb::addr_t explicit_length_offset = 2*ptr_size;
Virgile Bellof02a3c52013-08-27 16:24:58 +00001078 if (is_mutable && !is_inline)
Enrico Granatad83bfce2013-04-02 21:25:34 +00001079 explicit_length_offset = explicit_length_offset + ptr_size; // notInlineMutable.length;
1080 else if (is_inline)
1081 explicit_length = explicit_length + 0; // inline1.length;
Virgile Bellof02a3c52013-08-27 16:24:58 +00001082 else if (!is_inline && !is_mutable)
Enrico Granatad83bfce2013-04-02 21:25:34 +00001083 explicit_length_offset = explicit_length_offset + ptr_size; // notInlineImmutable1.length;
1084 else
1085 explicit_length_offset = 0;
1086
1087 if (explicit_length_offset)
1088 {
1089 explicit_length_offset = valobj_addr + explicit_length_offset;
1090 explicit_length = process_sp->ReadUnsignedIntegerFromMemory(explicit_length_offset, 4, 0, error);
1091 }
1092 }
1093
Enrico Granataf5196282012-09-04 18:48:21 +00001094 if (strcmp(class_name,"NSString") &&
1095 strcmp(class_name,"CFStringRef") &&
1096 strcmp(class_name,"CFMutableStringRef") &&
1097 strcmp(class_name,"__NSCFConstantString") &&
1098 strcmp(class_name,"__NSCFString") &&
1099 strcmp(class_name,"NSCFConstantString") &&
1100 strcmp(class_name,"NSCFString") &&
1101 strcmp(class_name,"NSPathStore2"))
1102 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001103 // not one of us - but tell me class name
1104 stream.Printf("class name = %s",class_name);
1105 return true;
Enrico Granataf5196282012-09-04 18:48:21 +00001106 }
1107
1108 if (is_mutable)
1109 {
1110 uint64_t location = 2 * ptr_size + valobj_addr;
1111 location = process_sp->ReadPointerFromMemory(location, error);
1112 if (error.Fail())
1113 return false;
Virgile Bellof02a3c52013-08-27 16:24:58 +00001114 if (has_explicit_length && is_unicode)
Enrico Granata56768392013-04-23 20:05:05 +00001115 {
1116 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
1117 options.SetConversionFunction(ConvertUTF16toUTF8);
1118 options.SetLocation(location);
1119 options.SetProcessSP(process_sp);
1120 options.SetStream(&stream);
1121 options.SetPrefixToken('@');
1122 options.SetQuote('"');
1123 options.SetSourceSize(explicit_length);
1124 options.SetNeedsZeroTermination(false);
1125 return ReadUTFBufferAndDumpToStream (options);
1126 }
Enrico Granataf5196282012-09-04 18:48:21 +00001127 else
Enrico Granatad83bfce2013-04-02 21:25:34 +00001128 return ReadAsciiBufferAndDumpToStream(location+1,process_sp,stream, explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001129 }
1130 else if (is_inline && has_explicit_length && !is_unicode && !is_special && !is_mutable)
1131 {
1132 uint64_t location = 3 * ptr_size + valobj_addr;
Enrico Granatad83bfce2013-04-02 21:25:34 +00001133 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001134 }
1135 else if (is_unicode)
1136 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001137 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +00001138 if (is_inline)
1139 {
1140 if (!has_explicit_length)
1141 {
1142 stream.Printf("found new combo");
1143 return true;
1144 }
1145 else
1146 location += ptr_size;
Enrico Granata56768392013-04-23 20:05:05 +00001147 }
Enrico Granataf5196282012-09-04 18:48:21 +00001148 else
1149 {
1150 location = process_sp->ReadPointerFromMemory(location, error);
1151 if (error.Fail())
1152 return false;
1153 }
Enrico Granata56768392013-04-23 20:05:05 +00001154 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
1155 options.SetConversionFunction(ConvertUTF16toUTF8);
1156 options.SetLocation(location);
1157 options.SetProcessSP(process_sp);
1158 options.SetStream(&stream);
1159 options.SetPrefixToken('@');
1160 options.SetQuote('"');
1161 options.SetSourceSize(explicit_length);
1162 options.SetNeedsZeroTermination(has_explicit_length == false);
1163 return ReadUTFBufferAndDumpToStream (options);
Enrico Granataf5196282012-09-04 18:48:21 +00001164 }
1165 else if (is_special)
1166 {
1167 uint64_t location = valobj_addr + (ptr_size == 8 ? 12 : 8);
Enrico Granata56768392013-04-23 20:05:05 +00001168 ReadUTFBufferAndDumpToStreamOptions<UTF16> options;
1169 options.SetConversionFunction(ConvertUTF16toUTF8);
1170 options.SetLocation(location);
1171 options.SetProcessSP(process_sp);
1172 options.SetStream(&stream);
1173 options.SetPrefixToken('@');
1174 options.SetQuote('"');
1175 options.SetSourceSize(explicit_length);
1176 options.SetNeedsZeroTermination(has_explicit_length == false);
1177 return ReadUTFBufferAndDumpToStream (options);
Enrico Granataf5196282012-09-04 18:48:21 +00001178 }
1179 else if (is_inline)
1180 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001181 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +00001182 if (!has_explicit_length)
1183 location++;
Enrico Granatad83bfce2013-04-02 21:25:34 +00001184 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001185 }
1186 else
1187 {
Enrico Granata87f00b42013-02-21 20:31:18 +00001188 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +00001189 location = process_sp->ReadPointerFromMemory(location, error);
1190 if (error.Fail())
1191 return false;
Enrico Granatac71f3492013-06-19 19:15:29 +00001192 if (has_explicit_length && !has_null)
1193 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 +00001194 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +00001195 }
1196
1197 stream.Printf("class name = %s",class_name);
1198 return true;
1199
1200}
1201
Enrico Granataf175ad12012-10-03 23:53:45 +00001202bool
Enrico Granata5bfce362013-02-08 01:55:46 +00001203lldb_private::formatters::NSAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream)
1204{
1205 TargetSP target_sp(valobj.GetTargetSP());
1206 if (!target_sp)
1207 return false;
1208 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton57ee3062013-07-11 22:46:58 +00001209 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
1210 if (!pointer_value)
Enrico Granata5bfce362013-02-08 01:55:46 +00001211 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001212 pointer_value += addr_size;
1213 ClangASTType type(valobj.GetClangType());
Enrico Granata5bfce362013-02-08 01:55:46 +00001214 ExecutionContext exe_ctx(target_sp,false);
Greg Clayton57ee3062013-07-11 22:46:58 +00001215 ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress("string_ptr", pointer_value, exe_ctx, type));
Enrico Granata5bfce362013-02-08 01:55:46 +00001216 if (!child_ptr_sp)
1217 return false;
1218 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +00001219 Error error;
1220 child_ptr_sp->GetData(data, error);
1221 if (error.Fail())
1222 return false;
Enrico Granata5bfce362013-02-08 01:55:46 +00001223 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData("string_data", data, exe_ctx, type));
1224 child_sp->GetValueAsUnsigned(0);
1225 if (child_sp)
1226 return NSStringSummaryProvider(*child_sp, stream);
1227 return false;
1228}
1229
1230bool
1231lldb_private::formatters::NSMutableAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream)
1232{
1233 return NSAttributedStringSummaryProvider(valobj, stream);
1234}
1235
1236bool
Enrico Granataf175ad12012-10-03 23:53:45 +00001237lldb_private::formatters::RuntimeSpecificDescriptionSummaryProvider (ValueObject& valobj, Stream& stream)
1238{
1239 stream.Printf("%s",valobj.GetObjectDescription());
1240 return true;
1241}
1242
Enrico Granatab5887262012-10-29 21:18:03 +00001243bool
1244lldb_private::formatters::ObjCBOOLSummaryProvider (ValueObject& valobj, Stream& stream)
1245{
Greg Clayton57ee3062013-07-11 22:46:58 +00001246 const uint32_t type_info = valobj.GetClangType().GetTypeInfo();
Enrico Granatab5887262012-10-29 21:18:03 +00001247
1248 ValueObjectSP real_guy_sp = valobj.GetSP();
1249
Greg Clayton57ee3062013-07-11 22:46:58 +00001250 if (type_info & ClangASTType::eTypeIsPointer)
Enrico Granatab5887262012-10-29 21:18:03 +00001251 {
1252 Error err;
1253 real_guy_sp = valobj.Dereference(err);
1254 if (err.Fail() || !real_guy_sp)
1255 return false;
1256 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001257 else if (type_info & ClangASTType::eTypeIsReference)
Enrico Granatab5887262012-10-29 21:18:03 +00001258 {
1259 real_guy_sp = valobj.GetChildAtIndex(0, true);
1260 if (!real_guy_sp)
1261 return false;
1262 }
1263 uint64_t value = real_guy_sp->GetValueAsUnsigned(0);
1264 if (value == 0)
1265 {
1266 stream.Printf("NO");
1267 return true;
1268 }
1269 stream.Printf("YES");
1270 return true;
1271}
1272
1273template <bool is_sel_ptr>
1274bool
1275lldb_private::formatters::ObjCSELSummaryProvider (ValueObject& valobj, Stream& stream)
1276{
Enrico Granata75dfb432013-02-15 00:06:04 +00001277 lldb::ValueObjectSP valobj_sp;
Enrico Granatab5887262012-10-29 21:18:03 +00001278
Greg Clayton57ee3062013-07-11 22:46:58 +00001279 ClangASTType charstar (valobj.GetClangType().GetBasicTypeFromAST(eBasicTypeChar).GetPointerType());
1280
1281 if (!charstar)
Enrico Granatab5887262012-10-29 21:18:03 +00001282 return false;
Enrico Granata75dfb432013-02-15 00:06:04 +00001283
Enrico Granatab5887262012-10-29 21:18:03 +00001284 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1285
Enrico Granata75dfb432013-02-15 00:06:04 +00001286 if (is_sel_ptr)
1287 {
1288 lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1289 if (data_address == LLDB_INVALID_ADDRESS)
1290 return false;
1291 valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address, exe_ctx, charstar);
1292 }
1293 else
1294 {
1295 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +00001296 Error error;
1297 valobj.GetData(data, error);
1298 if (error.Fail())
1299 return false;
Enrico Granata75dfb432013-02-15 00:06:04 +00001300 valobj_sp = ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar);
1301 }
Enrico Granatab5887262012-10-29 21:18:03 +00001302
Enrico Granata75dfb432013-02-15 00:06:04 +00001303 if (!valobj_sp)
1304 return false;
Enrico Granatab5887262012-10-29 21:18:03 +00001305
1306 stream.Printf("%s",valobj_sp->GetSummaryAsCString());
1307 return true;
1308}
1309
Enrico Granata6d37cc62013-03-19 00:27:22 +00001310// POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001
1311// this call gives the POSIX equivalent of the Cocoa epoch
1312time_t
Enrico Granata92373532013-03-19 22:58:48 +00001313lldb_private::formatters::GetOSXEpoch ()
Enrico Granata6d37cc62013-03-19 00:27:22 +00001314{
1315 static time_t epoch = 0;
1316 if (!epoch)
1317 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001318#ifndef _WIN32
Enrico Granata6d37cc62013-03-19 00:27:22 +00001319 tzset();
1320 tm tm_epoch;
1321 tm_epoch.tm_sec = 0;
1322 tm_epoch.tm_hour = 0;
1323 tm_epoch.tm_min = 0;
1324 tm_epoch.tm_mon = 0;
1325 tm_epoch.tm_mday = 1;
1326 tm_epoch.tm_year = 2001-1900; // for some reason, we need to subtract 1900 from this field. not sure why.
1327 tm_epoch.tm_isdst = -1;
1328 tm_epoch.tm_gmtoff = 0;
1329 tm_epoch.tm_zone = NULL;
1330 epoch = timegm(&tm_epoch);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001331#endif
Enrico Granata6d37cc62013-03-19 00:27:22 +00001332 }
1333 return epoch;
1334}
1335
Greg Claytonc7bece562013-01-25 18:06:21 +00001336size_t
Enrico Granataf615b802013-02-15 23:38:37 +00001337lldb_private::formatters::ExtractIndexFromString (const char* item_name)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001338{
1339 if (!item_name || !*item_name)
1340 return UINT32_MAX;
1341 if (*item_name != '[')
1342 return UINT32_MAX;
1343 item_name++;
Enrico Granataf615b802013-02-15 23:38:37 +00001344 char* endptr = NULL;
1345 unsigned long int idx = ::strtoul(item_name, &endptr, 0);
1346 if (idx == 0 && endptr == item_name)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001347 return UINT32_MAX;
Enrico Granataf615b802013-02-15 23:38:37 +00001348 if (idx == ULONG_MAX)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001349 return UINT32_MAX;
1350 return idx;
1351}
1352
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001353lldb_private::formatters::VectorIteratorSyntheticFrontEnd::VectorIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp,
1354 ConstString item_name) :
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001355SyntheticChildrenFrontEnd(*valobj_sp.get()),
1356m_exe_ctx_ref(),
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001357m_item_name(item_name),
1358m_item_sp()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001359{
1360 if (valobj_sp)
1361 Update();
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001362}
1363
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001364bool
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001365lldb_private::formatters::VectorIteratorSyntheticFrontEnd::Update()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001366{
Enrico Granata6eca3552013-03-28 18:50:54 +00001367 m_item_sp.reset();
1368
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001369 ValueObjectSP valobj_sp = m_backend.GetSP();
1370 if (!valobj_sp)
1371 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001372
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001373 if (!valobj_sp)
1374 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001375
1376 ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name,true));
1377 if (!item_ptr)
1378 return false;
1379 if (item_ptr->GetValueAsUnsigned(0) == 0)
1380 return false;
1381 Error err;
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001382 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
Greg Clayton57ee3062013-07-11 22:46:58 +00001383 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 +00001384 if (err.Fail())
1385 m_item_sp.reset();
Enrico Granata6eca3552013-03-28 18:50:54 +00001386 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001387}
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001388
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001389size_t
1390lldb_private::formatters::VectorIteratorSyntheticFrontEnd::CalculateNumChildren ()
1391{
1392 return 1;
1393}
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001394
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001395lldb::ValueObjectSP
1396lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx)
1397{
1398 if (idx == 0)
1399 return m_item_sp;
1400 return lldb::ValueObjectSP();
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001401}
1402
1403bool
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001404lldb_private::formatters::VectorIteratorSyntheticFrontEnd::MightHaveChildren ()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001405{
1406 return true;
1407}
1408
1409size_t
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001410lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001411{
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001412 if (name == ConstString("item"))
1413 return 0;
1414 return UINT32_MAX;
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001415}
1416
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001417lldb_private::formatters::VectorIteratorSyntheticFrontEnd::~VectorIteratorSyntheticFrontEnd ()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001418{
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001419}
1420
Enrico Granataf5196282012-09-04 18:48:21 +00001421template bool
Enrico Granatab2698cd2012-09-13 18:27:09 +00001422lldb_private::formatters::NSDataSummaryProvider<true> (ValueObject&, Stream&) ;
1423
1424template bool
1425lldb_private::formatters::NSDataSummaryProvider<false> (ValueObject&, Stream&) ;
Enrico Granatab5887262012-10-29 21:18:03 +00001426
1427template bool
1428lldb_private::formatters::ObjCSELSummaryProvider<true> (ValueObject&, Stream&) ;
1429
1430template bool
1431lldb_private::formatters::ObjCSELSummaryProvider<false> (ValueObject&, Stream&) ;