blob: 2a0ea4452717e7d06a0ccd5d9b0d246ef3745559 [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 Granataca6c8ee2014-10-30 01:45:39 +000013#include "lldb/DataFormatters/StringPrinter.h"
Enrico Granataf5196282012-09-04 18:48:21 +000014
Dmitri Gribenko024aa852013-01-30 15:05:59 +000015#include "llvm/Support/ConvertUTF.h"
Enrico Granataf5196282012-09-04 18:48:21 +000016
Enrico Granatab2698cd2012-09-13 18:27:09 +000017#include "lldb/Core/DataBufferHeap.h"
18#include "lldb/Core/Error.h"
Enrico Granataf5196282012-09-04 18:48:21 +000019#include "lldb/Core/Stream.h"
20#include "lldb/Core/ValueObject.h"
Enrico Granatab2698cd2012-09-13 18:27:09 +000021#include "lldb/Core/ValueObjectConstResult.h"
22#include "lldb/Host/Endian.h"
Enrico Granatab5887262012-10-29 21:18:03 +000023#include "lldb/Symbol/ClangASTContext.h"
Enrico Granataf5196282012-09-04 18:48:21 +000024#include "lldb/Target/Target.h"
Enrico Granataba8eb122014-07-30 21:07:50 +000025#include "lldb/Target/Thread.h"
Enrico Granataf5196282012-09-04 18:48:21 +000026
Enrico Granata76b08d52014-10-29 23:08:02 +000027#include "lldb/Utility/ProcessStructReader.h"
28
Enrico Granatad83bfce2013-04-02 21:25:34 +000029#include <algorithm>
30
Enrico Granataf5196282012-09-04 18:48:21 +000031using namespace lldb;
32using namespace lldb_private;
33using namespace lldb_private::formatters;
34
Enrico Granataba8eb122014-07-30 21:07:50 +000035StackFrame*
36lldb_private::formatters::GetViableFrame (ExecutionContext exe_ctx)
37{
38 StackFrame* frame = exe_ctx.GetFramePtr();
39 if (frame)
40 return frame;
41
42 Process* process = exe_ctx.GetProcessPtr();
43 if (!process)
44 return nullptr;
45
46 ThreadSP thread_sp(process->GetThreadList().GetSelectedThread());
47 if (thread_sp)
48 return thread_sp->GetSelectedFrame().get();
49 return nullptr;
50}
51
Enrico Granataf5196282012-09-04 18:48:21 +000052bool
Enrico Granatab2698cd2012-09-13 18:27:09 +000053lldb_private::formatters::ExtractValueFromObjCExpression (ValueObject &valobj,
54 const char* target_type,
55 const char* selector,
56 uint64_t &value)
Enrico Granataf5196282012-09-04 18:48:21 +000057{
58 if (!target_type || !*target_type)
59 return false;
60 if (!selector || !*selector)
61 return false;
Enrico Granataf5196282012-09-04 18:48:21 +000062 StreamString expr;
Daniel Maleaa85e6b62012-12-07 22:21:08 +000063 expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector);
Enrico Granataf5196282012-09-04 18:48:21 +000064 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
65 lldb::ValueObjectSP result_sp;
66 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +000067 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granataf5196282012-09-04 18:48:21 +000068 if (!target || !stack_frame)
69 return false;
Enrico Granatad4439aa2012-09-05 20:41:26 +000070
Jim Ingham35e1bda2012-10-16 21:41:58 +000071 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +000072 options.SetCoerceToId(false);
73 options.SetUnwindOnError(true);
74 options.SetKeepInMemory(true);
Enrico Granatad4439aa2012-09-05 20:41:26 +000075
Enrico Granataf5196282012-09-04 18:48:21 +000076 target->EvaluateExpression(expr.GetData(),
77 stack_frame,
Enrico Granatad4439aa2012-09-05 20:41:26 +000078 result_sp,
79 options);
Enrico Granataf5196282012-09-04 18:48:21 +000080 if (!result_sp)
81 return false;
82 value = result_sp->GetValueAsUnsigned(0);
83 return true;
84}
85
Enrico Granataf615b802013-02-15 23:38:37 +000086bool
87lldb_private::formatters::ExtractSummaryFromObjCExpression (ValueObject &valobj,
88 const char* target_type,
89 const char* selector,
90 Stream &stream)
91{
92 if (!target_type || !*target_type)
93 return false;
94 if (!selector || !*selector)
95 return false;
96 StreamString expr;
Enrico Granataeac4a482013-02-19 01:14:06 +000097 expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector);
Enrico Granataf615b802013-02-15 23:38:37 +000098 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
99 lldb::ValueObjectSP result_sp;
100 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +0000101 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granataf615b802013-02-15 23:38:37 +0000102 if (!target || !stack_frame)
103 return false;
104
105 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000106 options.SetCoerceToId(false);
107 options.SetUnwindOnError(true);
108 options.SetKeepInMemory(true);
109 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granataf615b802013-02-15 23:38:37 +0000110
111 target->EvaluateExpression(expr.GetData(),
112 stack_frame,
113 result_sp,
114 options);
115 if (!result_sp)
116 return false;
117 stream.Printf("%s",result_sp->GetSummaryAsCString());
118 return true;
119}
120
Enrico Granatab2698cd2012-09-13 18:27:09 +0000121lldb::ValueObjectSP
122lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj,
123 const char* return_type,
124 const char* selector,
125 uint64_t index)
126{
127 lldb::ValueObjectSP valobj_sp;
128 if (!return_type || !*return_type)
129 return valobj_sp;
130 if (!selector || !*selector)
131 return valobj_sp;
132 StreamString expr_path_stream;
133 valobj.GetExpressionPath(expr_path_stream, false);
134 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +0000135 expr.Printf("(%s)[%s %s:%" PRId64 "]",return_type,expr_path_stream.GetData(),selector,index);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000136 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
137 lldb::ValueObjectSP result_sp;
138 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +0000139 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000140 if (!target || !stack_frame)
141 return valobj_sp;
142
Jim Ingham35e1bda2012-10-16 21:41:58 +0000143 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000144 options.SetCoerceToId(false);
145 options.SetUnwindOnError(true);
146 options.SetKeepInMemory(true);
147 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000148
149 target->EvaluateExpression(expr.GetData(),
150 stack_frame,
151 valobj_sp,
152 options);
153 return valobj_sp;
154}
155
156lldb::ValueObjectSP
157lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj,
158 const char* return_type,
159 const char* selector,
160 const char* key)
161{
162 lldb::ValueObjectSP valobj_sp;
163 if (!return_type || !*return_type)
164 return valobj_sp;
165 if (!selector || !*selector)
166 return valobj_sp;
167 if (!key || !*key)
168 return valobj_sp;
169 StreamString expr_path_stream;
170 valobj.GetExpressionPath(expr_path_stream, false);
171 StreamString expr;
172 expr.Printf("(%s)[%s %s:%s]",return_type,expr_path_stream.GetData(),selector,key);
173 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
174 lldb::ValueObjectSP result_sp;
175 Target* target = exe_ctx.GetTargetPtr();
Enrico Granataba8eb122014-07-30 21:07:50 +0000176 StackFrame* stack_frame = GetViableFrame(exe_ctx);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000177 if (!target || !stack_frame)
178 return valobj_sp;
179
Jim Ingham35e1bda2012-10-16 21:41:58 +0000180 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000181 options.SetCoerceToId(false);
182 options.SetUnwindOnError(true);
183 options.SetKeepInMemory(true);
184 options.SetUseDynamic(lldb::eDynamicCanRunTarget);
Enrico Granatab2698cd2012-09-13 18:27:09 +0000185
186 target->EvaluateExpression(expr.GetData(),
187 stack_frame,
188 valobj_sp,
189 options);
190 return valobj_sp;
191}
192
Enrico Granataf68df122013-01-10 22:08:35 +0000193bool
194lldb_private::formatters::Char16StringSummaryProvider (ValueObject& valobj, Stream& stream)
195{
196 ProcessSP process_sp = valobj.GetProcessSP();
197 if (!process_sp)
198 return false;
199
200 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
201
202 if (!valobj_addr)
203 return false;
204
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000205 ReadStringAndDumpToStreamOptions options;
Enrico Granata56768392013-04-23 20:05:05 +0000206 options.SetLocation(valobj_addr);
Enrico Granata56768392013-04-23 20:05:05 +0000207 options.SetProcessSP(process_sp);
208 options.SetStream(&stream);
209 options.SetPrefixToken('u');
210
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000211 if (!ReadStringAndDumpToStream<StringElementType::UTF16>(options))
Enrico Granataf68df122013-01-10 22:08:35 +0000212 {
213 stream.Printf("Summary Unavailable");
214 return true;
215 }
216
217 return true;
218}
219
220bool
221lldb_private::formatters::Char32StringSummaryProvider (ValueObject& valobj, Stream& stream)
222{
223 ProcessSP process_sp = valobj.GetProcessSP();
224 if (!process_sp)
225 return false;
226
227 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
228
229 if (!valobj_addr)
230 return false;
231
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000232 ReadStringAndDumpToStreamOptions options;
Enrico Granata56768392013-04-23 20:05:05 +0000233 options.SetLocation(valobj_addr);
Enrico Granata56768392013-04-23 20:05:05 +0000234 options.SetProcessSP(process_sp);
235 options.SetStream(&stream);
Enrico Granatac03c5862013-04-23 21:37:33 +0000236 options.SetPrefixToken('U');
Enrico Granata56768392013-04-23 20:05:05 +0000237
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000238 if (!ReadStringAndDumpToStream<StringElementType::UTF32>(options))
Enrico Granataf68df122013-01-10 22:08:35 +0000239 {
240 stream.Printf("Summary Unavailable");
241 return true;
242 }
243
244 return true;
245}
246
247bool
248lldb_private::formatters::WCharStringSummaryProvider (ValueObject& valobj, Stream& stream)
249{
Enrico Granata38352042013-01-11 02:44:00 +0000250 ProcessSP process_sp = valobj.GetProcessSP();
251 if (!process_sp)
252 return false;
253
Enrico Granata3309d882013-01-12 01:00:22 +0000254 lldb::addr_t data_addr = 0;
255
256 if (valobj.IsPointerType())
257 data_addr = valobj.GetValueAsUnsigned(0);
258 else if (valobj.IsArrayType())
259 data_addr = valobj.GetAddressOf();
Enrico Granata38352042013-01-11 02:44:00 +0000260
Enrico Granata3309d882013-01-12 01:00:22 +0000261 if (data_addr == 0 || data_addr == LLDB_INVALID_ADDRESS)
Enrico Granata38352042013-01-11 02:44:00 +0000262 return false;
263
Greg Clayton57ee3062013-07-11 22:46:58 +0000264 clang::ASTContext* ast = valobj.GetClangType().GetASTContext();
265
Enrico Granata38352042013-01-11 02:44:00 +0000266 if (!ast)
267 return false;
268
Greg Clayton57ee3062013-07-11 22:46:58 +0000269 ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar);
270 const uint32_t wchar_size = wchar_clang_type.GetBitSize();
Enrico Granata38352042013-01-11 02:44:00 +0000271
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000272 ReadStringAndDumpToStreamOptions options;
273 options.SetLocation(data_addr);
274 options.SetProcessSP(process_sp);
275 options.SetStream(&stream);
276 options.SetPrefixToken('L');
277
Enrico Granata38352042013-01-11 02:44:00 +0000278 switch (wchar_size)
279 {
280 case 8:
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000281 return ReadStringAndDumpToStream<StringElementType::UTF8>(options);
Enrico Granata38352042013-01-11 02:44:00 +0000282 case 16:
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000283 return ReadStringAndDumpToStream<StringElementType::UTF16>(options);
Enrico Granata38352042013-01-11 02:44:00 +0000284 case 32:
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000285 return ReadStringAndDumpToStream<StringElementType::UTF32>(options);
Enrico Granata93d59662013-01-14 23:53:26 +0000286 default:
287 stream.Printf("size for wchar_t is not valid");
288 return true;
289 }
290 return true;
291}
292
293bool
294lldb_private::formatters::Char16SummaryProvider (ValueObject& valobj, Stream& stream)
295{
296 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000297 Error error;
298 valobj.GetData(data, error);
299
300 if (error.Fail())
301 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000302
303 std::string value;
304 valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
305 if (!value.empty())
306 stream.Printf("%s ", value.c_str());
307
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000308 ReadBufferAndDumpToStreamOptions options;
309 options.SetData(data);
310 options.SetStream(&stream);
311 options.SetPrefixToken('u');
312 options.SetQuote('\'');
313 options.SetSourceSize(1);
314
315 return ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
Enrico Granata93d59662013-01-14 23:53:26 +0000316}
317
318bool
319lldb_private::formatters::Char32SummaryProvider (ValueObject& valobj, Stream& stream)
320{
321 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000322 Error error;
323 valobj.GetData(data, error);
324
325 if (error.Fail())
326 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000327
328 std::string value;
329 valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
330 if (!value.empty())
331 stream.Printf("%s ", value.c_str());
332
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000333 ReadBufferAndDumpToStreamOptions options;
334 options.SetData(data);
335 options.SetStream(&stream);
336 options.SetPrefixToken('U');
337 options.SetQuote('\'');
338 options.SetSourceSize(1);
339
340 return ReadBufferAndDumpToStream<StringElementType::UTF32>(options);
Enrico Granata93d59662013-01-14 23:53:26 +0000341}
342
343bool
344lldb_private::formatters::WCharSummaryProvider (ValueObject& valobj, Stream& stream)
345{
346 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000347 Error error;
348 valobj.GetData(data, error);
349
350 if (error.Fail())
351 return false;
Enrico Granata93d59662013-01-14 23:53:26 +0000352
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000353 ReadBufferAndDumpToStreamOptions options;
354 options.SetData(data);
355 options.SetStream(&stream);
356 options.SetPrefixToken('L');
357 options.SetQuote('\'');
358 options.SetSourceSize(1);
Enrico Granata93d59662013-01-14 23:53:26 +0000359
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000360 return ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
Enrico Granataf68df122013-01-10 22:08:35 +0000361}
362
Enrico Granataa3962a72013-05-15 00:47:46 +0000363// the field layout in a libc++ string (cap, side, data or data, size, cap)
364enum LibcxxStringLayoutMode
365{
366 eLibcxxStringLayoutModeCSD = 0,
367 eLibcxxStringLayoutModeDSC = 1,
368 eLibcxxStringLayoutModeInvalid = 0xffff
369};
370
371// this function abstracts away the layout and mode details of a libc++ string
372// and returns the address of the data and the size ready for callers to consume
Enrico Granata3309d882013-01-12 01:00:22 +0000373static bool
374ExtractLibcxxStringInfo (ValueObject& valobj,
375 ValueObjectSP &location_sp,
376 uint64_t& size)
377{
378 ValueObjectSP D(valobj.GetChildAtIndexPath({0,0,0,0}));
379 if (!D)
380 return false;
381
Enrico Granataa3962a72013-05-15 00:47:46 +0000382 ValueObjectSP layout_decider(D->GetChildAtIndexPath({0,0}));
383
384 // this child should exist
385 if (!layout_decider)
Enrico Granata3309d882013-01-12 01:00:22 +0000386 return false;
387
Enrico Granataa3962a72013-05-15 00:47:46 +0000388 ConstString g_data_name("__data_");
389 ConstString g_size_name("__size_");
390 bool short_mode = false; // this means the string is in short-mode and the data is stored inline
391 LibcxxStringLayoutMode layout = (layout_decider->GetName() == g_data_name) ? eLibcxxStringLayoutModeDSC : eLibcxxStringLayoutModeCSD;
392 uint64_t size_mode_value = 0;
Enrico Granata3309d882013-01-12 01:00:22 +0000393
Enrico Granataa3962a72013-05-15 00:47:46 +0000394 if (layout == eLibcxxStringLayoutModeDSC)
395 {
396 ValueObjectSP size_mode(D->GetChildAtIndexPath({1,1,0}));
397 if (!size_mode)
398 return false;
399
400 if (size_mode->GetName() != g_size_name)
401 {
402 // we are hitting the padding structure, move along
403 size_mode = D->GetChildAtIndexPath({1,1,1});
404 if (!size_mode)
405 return false;
406 }
407
408 size_mode_value = (size_mode->GetValueAsUnsigned(0));
409 short_mode = ((size_mode_value & 0x80) == 0);
410 }
411 else
412 {
413 ValueObjectSP size_mode(D->GetChildAtIndexPath({1,0,0}));
414 if (!size_mode)
415 return false;
416
417 size_mode_value = (size_mode->GetValueAsUnsigned(0));
418 short_mode = ((size_mode_value & 1) == 0);
419 }
420
421 if (short_mode)
Enrico Granata3309d882013-01-12 01:00:22 +0000422 {
423 ValueObjectSP s(D->GetChildAtIndex(1, true));
424 if (!s)
425 return false;
Enrico Granataa3962a72013-05-15 00:47:46 +0000426 location_sp = s->GetChildAtIndex((layout == eLibcxxStringLayoutModeDSC) ? 0 : 1, true);
427 size = (layout == eLibcxxStringLayoutModeDSC) ? size_mode_value : ((size_mode_value >> 1) % 256);
Enrico Granata3309d882013-01-12 01:00:22 +0000428 return (location_sp.get() != nullptr);
429 }
430 else
431 {
432 ValueObjectSP l(D->GetChildAtIndex(0, true));
433 if (!l)
434 return false;
Enrico Granataa3962a72013-05-15 00:47:46 +0000435 // we can use the layout_decider object as the data pointer
436 location_sp = (layout == eLibcxxStringLayoutModeDSC) ? layout_decider : l->GetChildAtIndex(2, true);
Enrico Granata3309d882013-01-12 01:00:22 +0000437 ValueObjectSP size_vo(l->GetChildAtIndex(1, true));
438 if (!size_vo || !location_sp)
439 return false;
440 size = size_vo->GetValueAsUnsigned(0);
441 return true;
442 }
443}
444
445bool
446lldb_private::formatters::LibcxxWStringSummaryProvider (ValueObject& valobj, Stream& stream)
447{
448 uint64_t size = 0;
449 ValueObjectSP location_sp((ValueObject*)nullptr);
450 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
451 return false;
452 if (size == 0)
453 {
454 stream.Printf("L\"\"");
455 return true;
Enrico Granataa3962a72013-05-15 00:47:46 +0000456 }
Enrico Granata3309d882013-01-12 01:00:22 +0000457 if (!location_sp)
458 return false;
459 return WCharStringSummaryProvider(*location_sp.get(), stream);
460}
461
462bool
463lldb_private::formatters::LibcxxStringSummaryProvider (ValueObject& valobj, Stream& stream)
464{
465 uint64_t size = 0;
466 ValueObjectSP location_sp((ValueObject*)nullptr);
Enrico Granata2206b482014-10-30 18:27:31 +0000467
Enrico Granata3309d882013-01-12 01:00:22 +0000468 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
469 return false;
Enrico Granata2206b482014-10-30 18:27:31 +0000470
Enrico Granata3309d882013-01-12 01:00:22 +0000471 if (size == 0)
472 {
473 stream.Printf("\"\"");
474 return true;
475 }
Enrico Granata2206b482014-10-30 18:27:31 +0000476
Enrico Granata3309d882013-01-12 01:00:22 +0000477 if (!location_sp)
478 return false;
Enrico Granata2206b482014-10-30 18:27:31 +0000479
480 DataExtractor extractor;
481 size = std::min<decltype(size)>(size, valobj.GetTargetSP()->GetMaximumSizeOfStringSummary());
482 location_sp->GetPointeeData(extractor, 0, size);
483
484 lldb_private::formatters::ReadBufferAndDumpToStreamOptions options;
485 options.SetData(extractor); // none of this matters for a string - pass some defaults
486 options.SetStream(&stream);
487 options.SetPrefixToken(0);
488 options.SetQuote('"');
489 options.SetSourceSize(size);
490 options.SetEscapeNonPrintables(true);
491 lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
492
493 return true;
Enrico Granata3309d882013-01-12 01:00:22 +0000494}
495
Enrico Granata55900862013-03-15 18:55:30 +0000496bool
497lldb_private::formatters::ObjCClassSummaryProvider (ValueObject& valobj, Stream& stream)
498{
499 ProcessSP process_sp = valobj.GetProcessSP();
500 if (!process_sp)
501 return false;
502
503 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
504
505 if (!runtime)
506 return false;
507
Greg Clayton03da4cc2013-04-19 21:31:16 +0000508 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0)));
Enrico Granata55900862013-03-15 18:55:30 +0000509
510 if (!descriptor.get() || !descriptor->IsValid())
511 return false;
512
513 const char* class_name = descriptor->GetClassName().GetCString();
514
515 if (!class_name || !*class_name)
516 return false;
517
518 stream.Printf("%s",class_name);
519 return true;
520}
521
Enrico Granatac76b97b2013-04-26 00:59:02 +0000522class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd
523{
524public:
525 ObjCClassSyntheticChildrenFrontEnd (lldb::ValueObjectSP valobj_sp) :
526 SyntheticChildrenFrontEnd(*valobj_sp.get())
527 {
528 }
529
530 virtual size_t
531 CalculateNumChildren ()
532 {
533 return 0;
534 }
535
536 virtual lldb::ValueObjectSP
537 GetChildAtIndex (size_t idx)
538 {
539 return lldb::ValueObjectSP();
540 }
541
542 virtual bool
543 Update()
544 {
545 return false;
546 }
547
548 virtual bool
549 MightHaveChildren ()
550 {
551 return false;
552 }
553
554 virtual size_t
555 GetIndexOfChildWithName (const ConstString &name)
556 {
557 return UINT32_MAX;
558 }
559
560 virtual
561 ~ObjCClassSyntheticChildrenFrontEnd ()
562 {
563 }
564};
565
566SyntheticChildrenFrontEnd*
567lldb_private::formatters::ObjCClassSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
568{
569 return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp);
570}
571
Enrico Granataf5196282012-09-04 18:48:21 +0000572template<bool needs_at>
573bool
Enrico Granatab2698cd2012-09-13 18:27:09 +0000574lldb_private::formatters::NSDataSummaryProvider (ValueObject& valobj, Stream& stream)
Enrico Granataf5196282012-09-04 18:48:21 +0000575{
576 ProcessSP process_sp = valobj.GetProcessSP();
577 if (!process_sp)
578 return false;
579
580 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
581
582 if (!runtime)
583 return false;
584
585 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
586
587 if (!descriptor.get() || !descriptor->IsValid())
588 return false;
589
590 bool is_64bit = (process_sp->GetAddressByteSize() == 8);
591 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
592
593 if (!valobj_addr)
594 return false;
595
596 uint64_t value = 0;
597
598 const char* class_name = descriptor->GetClassName().GetCString();
Enrico Granata6d390772012-09-29 00:47:43 +0000599
600 if (!class_name || !*class_name)
601 return false;
602
Enrico Granataf5196282012-09-04 18:48:21 +0000603 if (!strcmp(class_name,"NSConcreteData") ||
604 !strcmp(class_name,"NSConcreteMutableData") ||
605 !strcmp(class_name,"__NSCFData"))
606 {
607 uint32_t offset = (is_64bit ? 16 : 8);
608 Error error;
609 value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, is_64bit ? 8 : 4, 0, error);
610 if (error.Fail())
611 return false;
612 }
613 else
614 {
Enrico Granatab2698cd2012-09-13 18:27:09 +0000615 if (!ExtractValueFromObjCExpression(valobj, "int", "length", value))
Enrico Granataf5196282012-09-04 18:48:21 +0000616 return false;
617 }
618
Daniel Malead01b2952012-11-29 21:49:15 +0000619 stream.Printf("%s%" PRIu64 " byte%s%s",
Enrico Granataf5196282012-09-04 18:48:21 +0000620 (needs_at ? "@\"" : ""),
621 value,
Enrico Granata1c333d02013-05-31 01:14:22 +0000622 (value != 1 ? "s" : ""),
Enrico Granataf5196282012-09-04 18:48:21 +0000623 (needs_at ? "\"" : ""));
624
625 return true;
626}
627
Enrico Granata87f00b42013-02-21 20:31:18 +0000628static bool
629ReadAsciiBufferAndDumpToStream (lldb::addr_t location,
630 lldb::ProcessSP& process_sp,
631 Stream& dest,
Enrico Granatad83bfce2013-04-02 21:25:34 +0000632 uint32_t size = 0,
Enrico Granata87f00b42013-02-21 20:31:18 +0000633 Error* error = NULL,
634 size_t *data_read = NULL,
635 char prefix_token = '@',
636 char quote = '"')
637{
638 Error my_error;
639 size_t my_data_read;
640 if (!process_sp || location == 0)
641 return false;
642
Enrico Granata23ab35a2013-04-02 23:07:55 +0000643 if (!size)
644 size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
645 else
646 size = std::min(size,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
Enrico Granata87f00b42013-02-21 20:31:18 +0000647
648 lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0));
649
650 my_data_read = process_sp->ReadCStringFromMemory(location, (char*)buffer_sp->GetBytes(), size, my_error);
651
652 if (error)
653 *error = my_error;
654 if (data_read)
655 *data_read = my_data_read;
656
657 if (my_error.Fail())
658 return false;
Enrico Granata5a9c4fe2013-05-17 23:28:13 +0000659
660 dest.Printf("%c%c",prefix_token,quote);
661
Enrico Granata87f00b42013-02-21 20:31:18 +0000662 if (my_data_read)
Enrico Granata5a9c4fe2013-05-17 23:28:13 +0000663 dest.Printf("%s",(char*)buffer_sp->GetBytes());
664
665 dest.Printf("%c",quote);
Enrico Granata87f00b42013-02-21 20:31:18 +0000666
667 return true;
668}
669
Enrico Granataf5196282012-09-04 18:48:21 +0000670bool
Jason Molenda705b1802014-06-13 02:37:02 +0000671lldb_private::formatters::NSTaggedString_SummaryProvider (ObjCLanguageRuntime::ClassDescriptorSP descriptor, Stream& stream)
672{
673 if (!descriptor)
674 return false;
675 uint64_t len_bits = 0, data_bits = 0;
676 if (!descriptor->GetTaggedPointerInfo(&len_bits,&data_bits,nullptr))
677 return false;
678
679 static const int g_MaxNonBitmaskedLen = 7; //TAGGED_STRING_UNPACKED_MAXLEN
680 static const int g_SixbitMaxLen = 9;
681 static const int g_fiveBitMaxLen = 11;
682
683 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013" "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
684
685 if (len_bits > g_fiveBitMaxLen)
686 return false;
687
688 // this is a fairly ugly trick - pretend that the numeric value is actually a char*
689 // this works under a few assumptions:
690 // little endian architecture
691 // sizeof(uint64_t) > g_MaxNonBitmaskedLen
692 if (len_bits <= g_MaxNonBitmaskedLen)
693 {
694 stream.Printf("@\"%s\"",(const char*)&data_bits);
695 return true;
696 }
697
698 // if the data is bitmasked, we need to actually process the bytes
699 uint8_t bitmask = 0;
700 uint8_t shift_offset = 0;
701
702 if (len_bits <= g_SixbitMaxLen)
703 {
704 bitmask = 0x03f;
705 shift_offset = 6;
706 }
707 else
708 {
709 bitmask = 0x01f;
710 shift_offset = 5;
711 }
712
713 std::vector<uint8_t> bytes;
714 bytes.resize(len_bits);
715 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits)
716 {
717 uint8_t packed = data_bits & bitmask;
718 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
719 }
720
721 stream.Printf("@\"%s\"",&bytes[0]);
722 return true;
723}
724
Enrico Granata76b08d52014-10-29 23:08:02 +0000725static ClangASTType
726GetNSPathStore2Type (Target &target)
727{
728 static ConstString g_type_name("__lldb_autogen_nspathstore2");
729
730 ClangASTContext *ast_ctx = target.GetScratchClangASTContext();
731
732 if (!ast_ctx)
733 return ClangASTType();
734
735 ClangASTType voidstar = ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
736 ClangASTType uint32 = ast_ctx->GetIntTypeFromBitSize(32, false);
737
738 return ast_ctx->GetOrCreateStructForIdentifier(g_type_name, {
739 {"isa",voidstar},
740 {"lengthAndRef",uint32},
741 {"buffer",voidstar}
742 });
743}
744
Jason Molenda705b1802014-06-13 02:37:02 +0000745bool
Enrico Granatab2698cd2012-09-13 18:27:09 +0000746lldb_private::formatters::NSStringSummaryProvider (ValueObject& valobj, Stream& stream)
Enrico Granataf5196282012-09-04 18:48:21 +0000747{
748 ProcessSP process_sp = valobj.GetProcessSP();
749 if (!process_sp)
750 return false;
751
752 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
753
754 if (!runtime)
755 return false;
756
757 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
758
759 if (!descriptor.get() || !descriptor->IsValid())
760 return false;
761
762 uint32_t ptr_size = process_sp->GetAddressByteSize();
763
764 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
765
766 if (!valobj_addr)
767 return false;
768
769 const char* class_name = descriptor->GetClassName().GetCString();
770
Enrico Granata60b81df2012-09-29 00:45:53 +0000771 if (!class_name || !*class_name)
772 return false;
773
Jason Molenda705b1802014-06-13 02:37:02 +0000774 bool is_tagged_ptr = (0 == strcmp(class_name,"NSTaggedPointerString")) && descriptor->GetTaggedPointerInfo();
775 // for a tagged pointer, the descriptor has everything we need
776 if (is_tagged_ptr)
777 return NSTaggedString_SummaryProvider(descriptor, stream);
778
779 // if not a tagged pointer that we know about, try the normal route
Enrico Granataf5196282012-09-04 18:48:21 +0000780 uint64_t info_bits_location = valobj_addr + ptr_size;
781 if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
782 info_bits_location += 3;
783
Enrico Granata87f00b42013-02-21 20:31:18 +0000784 Error error;
Enrico Granataf5196282012-09-04 18:48:21 +0000785
786 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(info_bits_location, 1, 0, error);
787 if (error.Fail())
788 return false;
789
790 bool is_mutable = (info_bits & 1) == 1;
791 bool is_inline = (info_bits & 0x60) == 0;
792 bool has_explicit_length = (info_bits & (1 | 4)) != 4;
793 bool is_unicode = (info_bits & 0x10) == 0x10;
794 bool is_special = strcmp(class_name,"NSPathStore2") == 0;
Enrico Granatad83bfce2013-04-02 21:25:34 +0000795 bool has_null = (info_bits & 8) == 8;
796
797 size_t explicit_length = 0;
798 if (!has_null && has_explicit_length && !is_special)
799 {
800 lldb::addr_t explicit_length_offset = 2*ptr_size;
Virgile Bellof02a3c52013-08-27 16:24:58 +0000801 if (is_mutable && !is_inline)
Enrico Granatad83bfce2013-04-02 21:25:34 +0000802 explicit_length_offset = explicit_length_offset + ptr_size; // notInlineMutable.length;
803 else if (is_inline)
804 explicit_length = explicit_length + 0; // inline1.length;
Virgile Bellof02a3c52013-08-27 16:24:58 +0000805 else if (!is_inline && !is_mutable)
Enrico Granatad83bfce2013-04-02 21:25:34 +0000806 explicit_length_offset = explicit_length_offset + ptr_size; // notInlineImmutable1.length;
807 else
808 explicit_length_offset = 0;
809
810 if (explicit_length_offset)
811 {
812 explicit_length_offset = valobj_addr + explicit_length_offset;
813 explicit_length = process_sp->ReadUnsignedIntegerFromMemory(explicit_length_offset, 4, 0, error);
814 }
815 }
816
Enrico Granataf5196282012-09-04 18:48:21 +0000817 if (strcmp(class_name,"NSString") &&
818 strcmp(class_name,"CFStringRef") &&
819 strcmp(class_name,"CFMutableStringRef") &&
820 strcmp(class_name,"__NSCFConstantString") &&
821 strcmp(class_name,"__NSCFString") &&
822 strcmp(class_name,"NSCFConstantString") &&
823 strcmp(class_name,"NSCFString") &&
824 strcmp(class_name,"NSPathStore2"))
825 {
Enrico Granata87f00b42013-02-21 20:31:18 +0000826 // not one of us - but tell me class name
827 stream.Printf("class name = %s",class_name);
828 return true;
Enrico Granataf5196282012-09-04 18:48:21 +0000829 }
830
831 if (is_mutable)
832 {
833 uint64_t location = 2 * ptr_size + valobj_addr;
834 location = process_sp->ReadPointerFromMemory(location, error);
835 if (error.Fail())
836 return false;
Virgile Bellof02a3c52013-08-27 16:24:58 +0000837 if (has_explicit_length && is_unicode)
Enrico Granata56768392013-04-23 20:05:05 +0000838 {
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000839 ReadStringAndDumpToStreamOptions options;
Enrico Granata56768392013-04-23 20:05:05 +0000840 options.SetLocation(location);
841 options.SetProcessSP(process_sp);
842 options.SetStream(&stream);
843 options.SetPrefixToken('@');
844 options.SetQuote('"');
845 options.SetSourceSize(explicit_length);
846 options.SetNeedsZeroTermination(false);
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000847 return ReadStringAndDumpToStream<StringElementType::UTF16>(options);
Enrico Granata56768392013-04-23 20:05:05 +0000848 }
Enrico Granataf5196282012-09-04 18:48:21 +0000849 else
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000850 {
851 ReadStringAndDumpToStreamOptions options;
852 options.SetLocation(location+1);
853 options.SetProcessSP(process_sp);
854 options.SetStream(&stream);
855 options.SetPrefixToken('@');
856 options.SetSourceSize(explicit_length);
857 options.SetNeedsZeroTermination(false);
858
859 return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
860 }
Enrico Granataf5196282012-09-04 18:48:21 +0000861 }
862 else if (is_inline && has_explicit_length && !is_unicode && !is_special && !is_mutable)
863 {
864 uint64_t location = 3 * ptr_size + valobj_addr;
Enrico Granatad83bfce2013-04-02 21:25:34 +0000865 return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length);
Enrico Granataf5196282012-09-04 18:48:21 +0000866 }
867 else if (is_unicode)
868 {
Enrico Granata87f00b42013-02-21 20:31:18 +0000869 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +0000870 if (is_inline)
871 {
872 if (!has_explicit_length)
873 {
874 stream.Printf("found new combo");
875 return true;
876 }
877 else
878 location += ptr_size;
Enrico Granata56768392013-04-23 20:05:05 +0000879 }
Enrico Granataf5196282012-09-04 18:48:21 +0000880 else
881 {
882 location = process_sp->ReadPointerFromMemory(location, error);
883 if (error.Fail())
884 return false;
885 }
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000886 ReadStringAndDumpToStreamOptions options;
Enrico Granata56768392013-04-23 20:05:05 +0000887 options.SetLocation(location);
888 options.SetProcessSP(process_sp);
889 options.SetStream(&stream);
890 options.SetPrefixToken('@');
891 options.SetQuote('"');
892 options.SetSourceSize(explicit_length);
893 options.SetNeedsZeroTermination(has_explicit_length == false);
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000894 return ReadStringAndDumpToStream<StringElementType::UTF16> (options);
Enrico Granataf5196282012-09-04 18:48:21 +0000895 }
896 else if (is_special)
897 {
Enrico Granata76b08d52014-10-29 23:08:02 +0000898 ProcessStructReader reader(valobj.GetProcessSP().get(), valobj.GetValueAsUnsigned(0), GetNSPathStore2Type(*valobj.GetTargetSP()));
899 explicit_length = reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
900 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
901
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000902 ReadStringAndDumpToStreamOptions options;
Enrico Granata56768392013-04-23 20:05:05 +0000903 options.SetLocation(location);
904 options.SetProcessSP(process_sp);
905 options.SetStream(&stream);
906 options.SetPrefixToken('@');
907 options.SetQuote('"');
908 options.SetSourceSize(explicit_length);
909 options.SetNeedsZeroTermination(has_explicit_length == false);
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000910 return ReadStringAndDumpToStream<StringElementType::UTF16> (options);
Enrico Granataf5196282012-09-04 18:48:21 +0000911 }
912 else if (is_inline)
913 {
Enrico Granata87f00b42013-02-21 20:31:18 +0000914 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +0000915 if (!has_explicit_length)
916 location++;
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000917 ReadStringAndDumpToStreamOptions options;
918 options.SetLocation(location);
919 options.SetProcessSP(process_sp);
920 options.SetStream(&stream);
921 options.SetPrefixToken('@');
922 options.SetSourceSize(explicit_length);
923 return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
Enrico Granataf5196282012-09-04 18:48:21 +0000924 }
925 else
926 {
Enrico Granata87f00b42013-02-21 20:31:18 +0000927 uint64_t location = valobj_addr + 2*ptr_size;
Enrico Granataf5196282012-09-04 18:48:21 +0000928 location = process_sp->ReadPointerFromMemory(location, error);
929 if (error.Fail())
930 return false;
Enrico Granatac71f3492013-06-19 19:15:29 +0000931 if (has_explicit_length && !has_null)
932 explicit_length++; // account for the fact that there is no NULL and we need to have one added
Enrico Granataca6c8ee2014-10-30 01:45:39 +0000933 ReadStringAndDumpToStreamOptions options;
934 options.SetLocation(location);
935 options.SetProcessSP(process_sp);
936 options.SetPrefixToken('@');
937 options.SetStream(&stream);
938 options.SetSourceSize(explicit_length);
939 return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
Enrico Granataf5196282012-09-04 18:48:21 +0000940 }
Enrico Granataf5196282012-09-04 18:48:21 +0000941}
942
Enrico Granataf175ad12012-10-03 23:53:45 +0000943bool
Enrico Granata5bfce362013-02-08 01:55:46 +0000944lldb_private::formatters::NSAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream)
945{
946 TargetSP target_sp(valobj.GetTargetSP());
947 if (!target_sp)
948 return false;
949 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton57ee3062013-07-11 22:46:58 +0000950 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
951 if (!pointer_value)
Enrico Granata5bfce362013-02-08 01:55:46 +0000952 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +0000953 pointer_value += addr_size;
954 ClangASTType type(valobj.GetClangType());
Enrico Granata5bfce362013-02-08 01:55:46 +0000955 ExecutionContext exe_ctx(target_sp,false);
Greg Clayton57ee3062013-07-11 22:46:58 +0000956 ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress("string_ptr", pointer_value, exe_ctx, type));
Enrico Granata5bfce362013-02-08 01:55:46 +0000957 if (!child_ptr_sp)
958 return false;
959 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000960 Error error;
961 child_ptr_sp->GetData(data, error);
962 if (error.Fail())
963 return false;
Enrico Granata5bfce362013-02-08 01:55:46 +0000964 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData("string_data", data, exe_ctx, type));
965 child_sp->GetValueAsUnsigned(0);
966 if (child_sp)
967 return NSStringSummaryProvider(*child_sp, stream);
968 return false;
969}
970
971bool
972lldb_private::formatters::NSMutableAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream)
973{
974 return NSAttributedStringSummaryProvider(valobj, stream);
975}
976
977bool
Enrico Granataf175ad12012-10-03 23:53:45 +0000978lldb_private::formatters::RuntimeSpecificDescriptionSummaryProvider (ValueObject& valobj, Stream& stream)
979{
980 stream.Printf("%s",valobj.GetObjectDescription());
981 return true;
982}
983
Enrico Granatab5887262012-10-29 21:18:03 +0000984bool
985lldb_private::formatters::ObjCBOOLSummaryProvider (ValueObject& valobj, Stream& stream)
986{
Greg Clayton57ee3062013-07-11 22:46:58 +0000987 const uint32_t type_info = valobj.GetClangType().GetTypeInfo();
Enrico Granatab5887262012-10-29 21:18:03 +0000988
989 ValueObjectSP real_guy_sp = valobj.GetSP();
990
Enrico Granata622be232014-10-21 20:52:14 +0000991 if (type_info & eTypeIsPointer)
Enrico Granatab5887262012-10-29 21:18:03 +0000992 {
993 Error err;
994 real_guy_sp = valobj.Dereference(err);
995 if (err.Fail() || !real_guy_sp)
996 return false;
997 }
Enrico Granata622be232014-10-21 20:52:14 +0000998 else if (type_info & eTypeIsReference)
Enrico Granatab5887262012-10-29 21:18:03 +0000999 {
1000 real_guy_sp = valobj.GetChildAtIndex(0, true);
1001 if (!real_guy_sp)
1002 return false;
1003 }
1004 uint64_t value = real_guy_sp->GetValueAsUnsigned(0);
1005 if (value == 0)
1006 {
1007 stream.Printf("NO");
1008 return true;
1009 }
1010 stream.Printf("YES");
1011 return true;
1012}
1013
1014template <bool is_sel_ptr>
1015bool
1016lldb_private::formatters::ObjCSELSummaryProvider (ValueObject& valobj, Stream& stream)
1017{
Enrico Granata75dfb432013-02-15 00:06:04 +00001018 lldb::ValueObjectSP valobj_sp;
Enrico Granatab5887262012-10-29 21:18:03 +00001019
Greg Clayton57ee3062013-07-11 22:46:58 +00001020 ClangASTType charstar (valobj.GetClangType().GetBasicTypeFromAST(eBasicTypeChar).GetPointerType());
1021
1022 if (!charstar)
Enrico Granatab5887262012-10-29 21:18:03 +00001023 return false;
Enrico Granata75dfb432013-02-15 00:06:04 +00001024
Enrico Granatab5887262012-10-29 21:18:03 +00001025 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1026
Enrico Granata75dfb432013-02-15 00:06:04 +00001027 if (is_sel_ptr)
1028 {
1029 lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1030 if (data_address == LLDB_INVALID_ADDRESS)
1031 return false;
1032 valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address, exe_ctx, charstar);
1033 }
1034 else
1035 {
1036 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +00001037 Error error;
1038 valobj.GetData(data, error);
1039 if (error.Fail())
1040 return false;
Enrico Granata75dfb432013-02-15 00:06:04 +00001041 valobj_sp = ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar);
1042 }
Enrico Granatab5887262012-10-29 21:18:03 +00001043
Enrico Granata75dfb432013-02-15 00:06:04 +00001044 if (!valobj_sp)
1045 return false;
Enrico Granatab5887262012-10-29 21:18:03 +00001046
1047 stream.Printf("%s",valobj_sp->GetSummaryAsCString());
1048 return true;
1049}
1050
Enrico Granata6d37cc62013-03-19 00:27:22 +00001051// POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001
1052// this call gives the POSIX equivalent of the Cocoa epoch
1053time_t
Enrico Granata92373532013-03-19 22:58:48 +00001054lldb_private::formatters::GetOSXEpoch ()
Enrico Granata6d37cc62013-03-19 00:27:22 +00001055{
1056 static time_t epoch = 0;
1057 if (!epoch)
1058 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001059#ifndef _WIN32
Enrico Granata6d37cc62013-03-19 00:27:22 +00001060 tzset();
1061 tm tm_epoch;
1062 tm_epoch.tm_sec = 0;
1063 tm_epoch.tm_hour = 0;
1064 tm_epoch.tm_min = 0;
1065 tm_epoch.tm_mon = 0;
1066 tm_epoch.tm_mday = 1;
1067 tm_epoch.tm_year = 2001-1900; // for some reason, we need to subtract 1900 from this field. not sure why.
1068 tm_epoch.tm_isdst = -1;
1069 tm_epoch.tm_gmtoff = 0;
1070 tm_epoch.tm_zone = NULL;
1071 epoch = timegm(&tm_epoch);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001072#endif
Enrico Granata6d37cc62013-03-19 00:27:22 +00001073 }
1074 return epoch;
1075}
1076
Greg Claytonc7bece562013-01-25 18:06:21 +00001077size_t
Enrico Granataf615b802013-02-15 23:38:37 +00001078lldb_private::formatters::ExtractIndexFromString (const char* item_name)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001079{
1080 if (!item_name || !*item_name)
1081 return UINT32_MAX;
1082 if (*item_name != '[')
1083 return UINT32_MAX;
1084 item_name++;
Enrico Granataf615b802013-02-15 23:38:37 +00001085 char* endptr = NULL;
1086 unsigned long int idx = ::strtoul(item_name, &endptr, 0);
1087 if (idx == 0 && endptr == item_name)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001088 return UINT32_MAX;
Enrico Granataf615b802013-02-15 23:38:37 +00001089 if (idx == ULONG_MAX)
Enrico Granatab2698cd2012-09-13 18:27:09 +00001090 return UINT32_MAX;
1091 return idx;
1092}
1093
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001094lldb_private::formatters::VectorIteratorSyntheticFrontEnd::VectorIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp,
1095 ConstString item_name) :
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001096SyntheticChildrenFrontEnd(*valobj_sp.get()),
1097m_exe_ctx_ref(),
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001098m_item_name(item_name),
1099m_item_sp()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001100{
1101 if (valobj_sp)
1102 Update();
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001103}
1104
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001105bool
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001106lldb_private::formatters::VectorIteratorSyntheticFrontEnd::Update()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001107{
Enrico Granata6eca3552013-03-28 18:50:54 +00001108 m_item_sp.reset();
1109
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001110 ValueObjectSP valobj_sp = m_backend.GetSP();
1111 if (!valobj_sp)
1112 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001113
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001114 if (!valobj_sp)
1115 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001116
1117 ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name,true));
1118 if (!item_ptr)
1119 return false;
1120 if (item_ptr->GetValueAsUnsigned(0) == 0)
1121 return false;
1122 Error err;
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001123 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
Greg Clayton57ee3062013-07-11 22:46:58 +00001124 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 +00001125 if (err.Fail())
1126 m_item_sp.reset();
Enrico Granata6eca3552013-03-28 18:50:54 +00001127 return false;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001128}
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001129
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001130size_t
1131lldb_private::formatters::VectorIteratorSyntheticFrontEnd::CalculateNumChildren ()
1132{
1133 return 1;
1134}
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001135
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001136lldb::ValueObjectSP
1137lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx)
1138{
1139 if (idx == 0)
1140 return m_item_sp;
1141 return lldb::ValueObjectSP();
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001142}
1143
1144bool
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001145lldb_private::formatters::VectorIteratorSyntheticFrontEnd::MightHaveChildren ()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001146{
1147 return true;
1148}
1149
1150size_t
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001151lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001152{
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001153 if (name == ConstString("item"))
1154 return 0;
1155 return UINT32_MAX;
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001156}
1157
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001158lldb_private::formatters::VectorIteratorSyntheticFrontEnd::~VectorIteratorSyntheticFrontEnd ()
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001159{
Enrico Granata3b1b2da2013-02-04 22:54:42 +00001160}
1161
Enrico Granataf5196282012-09-04 18:48:21 +00001162template bool
Enrico Granatab2698cd2012-09-13 18:27:09 +00001163lldb_private::formatters::NSDataSummaryProvider<true> (ValueObject&, Stream&) ;
1164
1165template bool
1166lldb_private::formatters::NSDataSummaryProvider<false> (ValueObject&, Stream&) ;
Enrico Granatab5887262012-10-29 21:18:03 +00001167
1168template bool
1169lldb_private::formatters::ObjCSELSummaryProvider<true> (ValueObject&, Stream&) ;
1170
1171template bool
1172lldb_private::formatters::ObjCSELSummaryProvider<false> (ValueObject&, Stream&) ;