Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1 | //===-- 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 Kopec | ef14371 | 2013-06-03 18:00:07 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 12 | #include "lldb/DataFormatters/CXXFormatterFunctions.h" |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 13 | |
Dmitri Gribenko | 024aa85 | 2013-01-30 15:05:59 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ConvertUTF.h" |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 15 | |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 16 | #include "lldb/Core/DataBufferHeap.h" |
| 17 | #include "lldb/Core/Error.h" |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Stream.h" |
| 19 | #include "lldb/Core/ValueObject.h" |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ValueObjectConstResult.h" |
| 21 | #include "lldb/Host/Endian.h" |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/ClangASTContext.h" |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Target.h" |
| 24 | |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | using namespace lldb_private::formatters; |
| 30 | |
| 31 | bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 32 | lldb_private::formatters::ExtractValueFromObjCExpression (ValueObject &valobj, |
| 33 | const char* target_type, |
| 34 | const char* selector, |
| 35 | uint64_t &value) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 36 | { |
| 37 | if (!target_type || !*target_type) |
| 38 | return false; |
| 39 | if (!selector || !*selector) |
| 40 | return false; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 41 | StreamString expr; |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 42 | expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 43 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 44 | lldb::ValueObjectSP result_sp; |
| 45 | Target* target = exe_ctx.GetTargetPtr(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 46 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 47 | if (!target || !stack_frame) |
| 48 | return false; |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 49 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 50 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 51 | options.SetCoerceToId(false); |
| 52 | options.SetUnwindOnError(true); |
| 53 | options.SetKeepInMemory(true); |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 54 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 55 | target->EvaluateExpression(expr.GetData(), |
| 56 | stack_frame, |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 57 | result_sp, |
| 58 | options); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 59 | if (!result_sp) |
| 60 | return false; |
| 61 | value = result_sp->GetValueAsUnsigned(0); |
| 62 | return true; |
| 63 | } |
| 64 | |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 65 | bool |
| 66 | lldb_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 Granata | eac4a48 | 2013-02-19 01:14:06 +0000 | [diff] [blame] | 76 | expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector); |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 77 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 78 | lldb::ValueObjectSP result_sp; |
| 79 | Target* target = exe_ctx.GetTargetPtr(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 80 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 81 | if (!target || !stack_frame) |
| 82 | return false; |
| 83 | |
| 84 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 85 | options.SetCoerceToId(false); |
| 86 | options.SetUnwindOnError(true); |
| 87 | options.SetKeepInMemory(true); |
| 88 | options.SetUseDynamic(lldb::eDynamicCanRunTarget); |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 89 | |
| 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 Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 100 | lldb::ValueObjectSP |
| 101 | lldb_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 Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 114 | expr.Printf("(%s)[%s %s:%" PRId64 "]",return_type,expr_path_stream.GetData(),selector,index); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 115 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 116 | lldb::ValueObjectSP result_sp; |
| 117 | Target* target = exe_ctx.GetTargetPtr(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 118 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 119 | if (!target || !stack_frame) |
| 120 | return valobj_sp; |
| 121 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 122 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 123 | options.SetCoerceToId(false); |
| 124 | options.SetUnwindOnError(true); |
| 125 | options.SetKeepInMemory(true); |
| 126 | options.SetUseDynamic(lldb::eDynamicCanRunTarget); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 127 | |
| 128 | target->EvaluateExpression(expr.GetData(), |
| 129 | stack_frame, |
| 130 | valobj_sp, |
| 131 | options); |
| 132 | return valobj_sp; |
| 133 | } |
| 134 | |
| 135 | lldb::ValueObjectSP |
| 136 | lldb_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 Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 155 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 156 | if (!target || !stack_frame) |
| 157 | return valobj_sp; |
| 158 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 159 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 160 | options.SetCoerceToId(false); |
| 161 | options.SetUnwindOnError(true); |
| 162 | options.SetKeepInMemory(true); |
| 163 | options.SetUseDynamic(lldb::eDynamicCanRunTarget); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 164 | |
| 165 | target->EvaluateExpression(expr.GetData(), |
| 166 | stack_frame, |
| 167 | valobj_sp, |
| 168 | options); |
| 169 | return valobj_sp; |
| 170 | } |
| 171 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 172 | // use this call if you already have an LLDB-side buffer for the data |
| 173 | template<typename SourceDataType> |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 174 | static bool |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 175 | DumpUTFBufferToStream (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 Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 184 | uint32_t sourceSize = 0) |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 185 | { |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 186 | 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 Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 191 | { |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 192 | const int bufferSPSize = data.GetByteSize(); |
| 193 | if (sourceSize == 0) |
| 194 | { |
| 195 | const int origin_encoding = 8*sizeof(SourceDataType); |
Greg Clayton | aa4c47a | 2013-02-08 21:59:34 +0000 | [diff] [blame] | 196 | sourceSize = bufferSPSize/(origin_encoding / 4); |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart(); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 200 | 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 Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 212 | data_ptr = (SourceDataType*)data.GetDataStart(); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 213 | |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 214 | lldb::DataBufferSP utf8_data_buffer_sp; |
| 215 | UTF8* utf8_data_ptr = nullptr; |
| 216 | UTF8* utf8_data_end_ptr = nullptr; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 217 | |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 218 | if (ConvertFunction) |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 219 | { |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 220 | utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0)); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 221 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 222 | utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize(); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 223 | ConvertFunction ( (const SourceDataType**)&data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion ); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 224 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr |
| 225 | } |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 226 | 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 Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 234 | // 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 Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 237 | 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 Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 243 | } |
| 244 | if (quote != 0) |
| 245 | stream.Printf("%c",quote); |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | template<typename SourceDataType> |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 250 | class ReadUTFBufferAndDumpToStreamOptions |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 251 | { |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 252 | public: |
| 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 | |
| 375 | private: |
| 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 | |
| 386 | template<typename SourceDataType> |
| 387 | static bool |
| 388 | ReadUTFBufferAndDumpToStream (const ReadUTFBufferAndDumpToStreamOptions<SourceDataType>& options) |
| 389 | { |
| 390 | if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS) |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 391 | return false; |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 392 | |
| 393 | ProcessSP process_sp(options.GetProcessSP()); |
| 394 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 395 | if (!process_sp) |
| 396 | return false; |
| 397 | |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 398 | const int type_width = sizeof(SourceDataType); |
| 399 | const int origin_encoding = 8 * type_width ; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 400 | 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 Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 403 | if (origin_encoding != 8 && !options.GetConversionFunction()) |
| 404 | return false; |
| 405 | |
| 406 | if (!options.GetStream()) |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 407 | return false; |
| 408 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 409 | uint32_t sourceSize = options.GetSourceSize(); |
| 410 | bool needs_zero_terminator = options.GetNeedsZeroTermination(); |
| 411 | |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 412 | if (!sourceSize) |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 413 | { |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 414 | sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary(); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 415 | needs_zero_terminator = true; |
| 416 | } |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 417 | else |
| 418 | sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
| 419 | |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 420 | const int bufferSPSize = sourceSize * type_width; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 421 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 422 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0)); |
| 423 | |
| 424 | if (!buffer_sp->GetBytes()) |
| 425 | return false; |
| 426 | |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 427 | Error error; |
| 428 | char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes()); |
| 429 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 430 | 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 Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 436 | if (error.Fail() || data_read == 0) |
| 437 | { |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 438 | options.GetStream()->Printf("unable to read data"); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 439 | return true; |
| 440 | } |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 441 | |
| 442 | DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize()); |
| 443 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 444 | return DumpUTFBufferToStream(options.GetConversionFunction(), data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | bool |
| 448 | lldb_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 Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 459 | 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 Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 467 | { |
| 468 | stream.Printf("Summary Unavailable"); |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | bool |
| 476 | lldb_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 Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 487 | ReadUTFBufferAndDumpToStreamOptions<UTF32> options; |
| 488 | options.SetLocation(valobj_addr); |
| 489 | options.SetConversionFunction(ConvertUTF32toUTF8); |
| 490 | options.SetProcessSP(process_sp); |
| 491 | options.SetStream(&stream); |
Enrico Granata | c03c586 | 2013-04-23 21:37:33 +0000 | [diff] [blame] | 492 | options.SetPrefixToken('U'); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 493 | |
| 494 | if (!ReadUTFBufferAndDumpToStream(options)) |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 495 | { |
| 496 | stream.Printf("Summary Unavailable"); |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | bool |
| 504 | lldb_private::formatters::WCharStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 505 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 506 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 507 | if (!process_sp) |
| 508 | return false; |
| 509 | |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 510 | 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 Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 516 | |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 517 | if (data_addr == 0 || data_addr == LLDB_INVALID_ADDRESS) |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 518 | return false; |
| 519 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 520 | clang::ASTContext* ast = valobj.GetClangType().GetASTContext(); |
| 521 | |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 522 | if (!ast) |
| 523 | return false; |
| 524 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 525 | ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar); |
| 526 | const uint32_t wchar_size = wchar_clang_type.GetBitSize(); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 527 | |
| 528 | switch (wchar_size) |
| 529 | { |
| 530 | case 8: |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 531 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 532 | // utf 8 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 533 | |
| 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 Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 543 | case 16: |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 544 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 545 | // utf 16 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 546 | 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 Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 555 | case 32: |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 556 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 557 | // utf 32 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 558 | 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 Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 567 | default: |
| 568 | stream.Printf("size for wchar_t is not valid"); |
| 569 | return true; |
| 570 | } |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | bool |
| 575 | lldb_private::formatters::Char16SummaryProvider (ValueObject& valobj, Stream& stream) |
| 576 | { |
| 577 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 578 | Error error; |
| 579 | valobj.GetData(data, error); |
| 580 | |
| 581 | if (error.Fail()) |
| 582 | return false; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 583 | |
| 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 | |
| 592 | bool |
| 593 | lldb_private::formatters::Char32SummaryProvider (ValueObject& valobj, Stream& stream) |
| 594 | { |
| 595 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 596 | Error error; |
| 597 | valobj.GetData(data, error); |
| 598 | |
| 599 | if (error.Fail()) |
| 600 | return false; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 601 | |
| 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 | |
| 610 | bool |
| 611 | lldb_private::formatters::WCharSummaryProvider (ValueObject& valobj, Stream& stream) |
| 612 | { |
| 613 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 614 | Error error; |
| 615 | valobj.GetData(data, error); |
| 616 | |
| 617 | if (error.Fail()) |
| 618 | return false; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 619 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 620 | clang::ASTContext* ast = valobj.GetClangType().GetASTContext(); |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 621 | |
| 622 | if (!ast) |
| 623 | return false; |
| 624 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 625 | ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar); |
| 626 | const uint32_t wchar_size = wchar_clang_type.GetBitSize(); |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 627 | std::string value; |
| 628 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 629 | 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 Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 664 | default: |
| 665 | stream.Printf("size for wchar_t is not valid"); |
| 666 | return true; |
| 667 | } |
| 668 | return true; |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 671 | // the field layout in a libc++ string (cap, side, data or data, size, cap) |
| 672 | enum 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 Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 681 | static bool |
| 682 | ExtractLibcxxStringInfo (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 Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 690 | ValueObjectSP layout_decider(D->GetChildAtIndexPath({0,0})); |
| 691 | |
| 692 | // this child should exist |
| 693 | if (!layout_decider) |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 694 | return false; |
| 695 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 696 | 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 Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 701 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 702 | 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 Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 730 | { |
| 731 | ValueObjectSP s(D->GetChildAtIndex(1, true)); |
| 732 | if (!s) |
| 733 | return false; |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 734 | location_sp = s->GetChildAtIndex((layout == eLibcxxStringLayoutModeDSC) ? 0 : 1, true); |
| 735 | size = (layout == eLibcxxStringLayoutModeDSC) ? size_mode_value : ((size_mode_value >> 1) % 256); |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 736 | return (location_sp.get() != nullptr); |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | ValueObjectSP l(D->GetChildAtIndex(0, true)); |
| 741 | if (!l) |
| 742 | return false; |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 743 | // we can use the layout_decider object as the data pointer |
| 744 | location_sp = (layout == eLibcxxStringLayoutModeDSC) ? layout_decider : l->GetChildAtIndex(2, true); |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 745 | 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 | |
| 753 | bool |
| 754 | lldb_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 Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 764 | } |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 765 | if (!location_sp) |
| 766 | return false; |
| 767 | return WCharStringSummaryProvider(*location_sp.get(), stream); |
| 768 | } |
| 769 | |
| 770 | bool |
| 771 | lldb_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 Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 785 | 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 Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 790 | return error.Success(); |
| 791 | } |
| 792 | |
Enrico Granata | 5590086 | 2013-03-15 18:55:30 +0000 | [diff] [blame] | 793 | bool |
| 794 | lldb_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 Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 805 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0))); |
Enrico Granata | 5590086 | 2013-03-15 18:55:30 +0000 | [diff] [blame] | 806 | |
| 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 Granata | c76b97b | 2013-04-26 00:59:02 +0000 | [diff] [blame] | 819 | class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd |
| 820 | { |
| 821 | public: |
| 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 | |
| 863 | SyntheticChildrenFrontEnd* |
| 864 | lldb_private::formatters::ObjCClassSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp) |
| 865 | { |
| 866 | return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp); |
| 867 | } |
| 868 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 869 | template<bool needs_at> |
| 870 | bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 871 | lldb_private::formatters::NSDataSummaryProvider (ValueObject& valobj, Stream& stream) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 872 | { |
| 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 Granata | 6d39077 | 2012-09-29 00:47:43 +0000 | [diff] [blame] | 896 | |
| 897 | if (!class_name || !*class_name) |
| 898 | return false; |
| 899 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 900 | 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 Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 912 | if (!ExtractValueFromObjCExpression(valobj, "int", "length", value)) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 913 | return false; |
| 914 | } |
| 915 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 916 | stream.Printf("%s%" PRIu64 " byte%s%s", |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 917 | (needs_at ? "@\"" : ""), |
| 918 | value, |
Enrico Granata | 1c333d0 | 2013-05-31 01:14:22 +0000 | [diff] [blame] | 919 | (value != 1 ? "s" : ""), |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 920 | (needs_at ? "\"" : "")); |
| 921 | |
| 922 | return true; |
| 923 | } |
| 924 | |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 925 | static bool |
| 926 | ReadAsciiBufferAndDumpToStream (lldb::addr_t location, |
| 927 | lldb::ProcessSP& process_sp, |
| 928 | Stream& dest, |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 929 | uint32_t size = 0, |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 930 | 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 Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 940 | if (!size) |
| 941 | size = process_sp->GetTarget().GetMaximumSizeOfStringSummary(); |
| 942 | else |
| 943 | size = std::min(size,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 944 | |
| 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 Granata | 5a9c4fe | 2013-05-17 23:28:13 +0000 | [diff] [blame] | 956 | |
| 957 | dest.Printf("%c%c",prefix_token,quote); |
| 958 | |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 959 | if (my_data_read) |
Enrico Granata | 5a9c4fe | 2013-05-17 23:28:13 +0000 | [diff] [blame] | 960 | dest.Printf("%s",(char*)buffer_sp->GetBytes()); |
| 961 | |
| 962 | dest.Printf("%c",quote); |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 963 | |
| 964 | return true; |
| 965 | } |
| 966 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 967 | bool |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 968 | lldb_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 | |
| 1022 | bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1023 | lldb_private::formatters::NSStringSummaryProvider (ValueObject& valobj, Stream& stream) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1024 | { |
| 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 Granata | 60b81df | 2012-09-29 00:45:53 +0000 | [diff] [blame] | 1048 | if (!class_name || !*class_name) |
| 1049 | return false; |
| 1050 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1051 | 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 Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1057 | uint64_t info_bits_location = valobj_addr + ptr_size; |
| 1058 | if (process_sp->GetByteOrder() != lldb::eByteOrderLittle) |
| 1059 | info_bits_location += 3; |
| 1060 | |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1061 | Error error; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1062 | |
| 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 Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1072 | 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 Bello | f02a3c5 | 2013-08-27 16:24:58 +0000 | [diff] [blame] | 1078 | if (is_mutable && !is_inline) |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1079 | explicit_length_offset = explicit_length_offset + ptr_size; // notInlineMutable.length; |
| 1080 | else if (is_inline) |
| 1081 | explicit_length = explicit_length + 0; // inline1.length; |
Virgile Bello | f02a3c5 | 2013-08-27 16:24:58 +0000 | [diff] [blame] | 1082 | else if (!is_inline && !is_mutable) |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1083 | 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 Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1094 | 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 Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1103 | // not one of us - but tell me class name |
| 1104 | stream.Printf("class name = %s",class_name); |
| 1105 | return true; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1106 | } |
| 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 Bello | f02a3c5 | 2013-08-27 16:24:58 +0000 | [diff] [blame] | 1114 | if (has_explicit_length && is_unicode) |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1115 | { |
| 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 Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1127 | else |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1128 | return ReadAsciiBufferAndDumpToStream(location+1,process_sp,stream, explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1129 | } |
| 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 Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1133 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1134 | } |
| 1135 | else if (is_unicode) |
| 1136 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1137 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1138 | 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 Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1147 | } |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1148 | else |
| 1149 | { |
| 1150 | location = process_sp->ReadPointerFromMemory(location, error); |
| 1151 | if (error.Fail()) |
| 1152 | return false; |
| 1153 | } |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1154 | 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 Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1164 | } |
| 1165 | else if (is_special) |
| 1166 | { |
| 1167 | uint64_t location = valobj_addr + (ptr_size == 8 ? 12 : 8); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1168 | 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 Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1178 | } |
| 1179 | else if (is_inline) |
| 1180 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1181 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1182 | if (!has_explicit_length) |
| 1183 | location++; |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1184 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1185 | } |
| 1186 | else |
| 1187 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1188 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1189 | location = process_sp->ReadPointerFromMemory(location, error); |
| 1190 | if (error.Fail()) |
| 1191 | return false; |
Enrico Granata | c71f349 | 2013-06-19 19:15:29 +0000 | [diff] [blame] | 1192 | 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 Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1194 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | stream.Printf("class name = %s",class_name); |
| 1198 | return true; |
| 1199 | |
| 1200 | } |
| 1201 | |
Enrico Granata | f175ad1 | 2012-10-03 23:53:45 +0000 | [diff] [blame] | 1202 | bool |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1203 | lldb_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 Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1209 | uint64_t pointer_value = valobj.GetValueAsUnsigned(0); |
| 1210 | if (!pointer_value) |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1211 | return false; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1212 | pointer_value += addr_size; |
| 1213 | ClangASTType type(valobj.GetClangType()); |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1214 | ExecutionContext exe_ctx(target_sp,false); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1215 | ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress("string_ptr", pointer_value, exe_ctx, type)); |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1216 | if (!child_ptr_sp) |
| 1217 | return false; |
| 1218 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 1219 | Error error; |
| 1220 | child_ptr_sp->GetData(data, error); |
| 1221 | if (error.Fail()) |
| 1222 | return false; |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1223 | 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 | |
| 1230 | bool |
| 1231 | lldb_private::formatters::NSMutableAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1232 | { |
| 1233 | return NSAttributedStringSummaryProvider(valobj, stream); |
| 1234 | } |
| 1235 | |
| 1236 | bool |
Enrico Granata | f175ad1 | 2012-10-03 23:53:45 +0000 | [diff] [blame] | 1237 | lldb_private::formatters::RuntimeSpecificDescriptionSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1238 | { |
| 1239 | stream.Printf("%s",valobj.GetObjectDescription()); |
| 1240 | return true; |
| 1241 | } |
| 1242 | |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1243 | bool |
| 1244 | lldb_private::formatters::ObjCBOOLSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1245 | { |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1246 | const uint32_t type_info = valobj.GetClangType().GetTypeInfo(); |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1247 | |
| 1248 | ValueObjectSP real_guy_sp = valobj.GetSP(); |
| 1249 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1250 | if (type_info & ClangASTType::eTypeIsPointer) |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1251 | { |
| 1252 | Error err; |
| 1253 | real_guy_sp = valobj.Dereference(err); |
| 1254 | if (err.Fail() || !real_guy_sp) |
| 1255 | return false; |
| 1256 | } |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1257 | else if (type_info & ClangASTType::eTypeIsReference) |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1258 | { |
| 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 | |
| 1273 | template <bool is_sel_ptr> |
| 1274 | bool |
| 1275 | lldb_private::formatters::ObjCSELSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1276 | { |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1277 | lldb::ValueObjectSP valobj_sp; |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1278 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1279 | ClangASTType charstar (valobj.GetClangType().GetBasicTypeFromAST(eBasicTypeChar).GetPointerType()); |
| 1280 | |
| 1281 | if (!charstar) |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1282 | return false; |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1283 | |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1284 | ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); |
| 1285 | |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1286 | 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 Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 1296 | Error error; |
| 1297 | valobj.GetData(data, error); |
| 1298 | if (error.Fail()) |
| 1299 | return false; |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1300 | valobj_sp = ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar); |
| 1301 | } |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1302 | |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1303 | if (!valobj_sp) |
| 1304 | return false; |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1305 | |
| 1306 | stream.Printf("%s",valobj_sp->GetSummaryAsCString()); |
| 1307 | return true; |
| 1308 | } |
| 1309 | |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1310 | // 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 |
| 1312 | time_t |
Enrico Granata | 9237353 | 2013-03-19 22:58:48 +0000 | [diff] [blame] | 1313 | lldb_private::formatters::GetOSXEpoch () |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1314 | { |
| 1315 | static time_t epoch = 0; |
| 1316 | if (!epoch) |
| 1317 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1318 | #ifndef _WIN32 |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1319 | 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 Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1331 | #endif |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1332 | } |
| 1333 | return epoch; |
| 1334 | } |
| 1335 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1336 | size_t |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1337 | lldb_private::formatters::ExtractIndexFromString (const char* item_name) |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1338 | { |
| 1339 | if (!item_name || !*item_name) |
| 1340 | return UINT32_MAX; |
| 1341 | if (*item_name != '[') |
| 1342 | return UINT32_MAX; |
| 1343 | item_name++; |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1344 | char* endptr = NULL; |
| 1345 | unsigned long int idx = ::strtoul(item_name, &endptr, 0); |
| 1346 | if (idx == 0 && endptr == item_name) |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1347 | return UINT32_MAX; |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1348 | if (idx == ULONG_MAX) |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1349 | return UINT32_MAX; |
| 1350 | return idx; |
| 1351 | } |
| 1352 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1353 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::VectorIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp, |
| 1354 | ConstString item_name) : |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1355 | SyntheticChildrenFrontEnd(*valobj_sp.get()), |
| 1356 | m_exe_ctx_ref(), |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1357 | m_item_name(item_name), |
| 1358 | m_item_sp() |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1359 | { |
| 1360 | if (valobj_sp) |
| 1361 | Update(); |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1364 | bool |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1365 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::Update() |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1366 | { |
Enrico Granata | 6eca355 | 2013-03-28 18:50:54 +0000 | [diff] [blame] | 1367 | m_item_sp.reset(); |
| 1368 | |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1369 | ValueObjectSP valobj_sp = m_backend.GetSP(); |
| 1370 | if (!valobj_sp) |
| 1371 | return false; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1372 | |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1373 | if (!valobj_sp) |
| 1374 | return false; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1375 | |
| 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 Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1382 | m_exe_ctx_ref = valobj_sp->GetExecutionContextRef(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1383 | m_item_sp = ValueObject::CreateValueObjectFromAddress("item", item_ptr->GetValueAsUnsigned(0), m_exe_ctx_ref, item_ptr->GetClangType().GetPointeeType()); |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1384 | if (err.Fail()) |
| 1385 | m_item_sp.reset(); |
Enrico Granata | 6eca355 | 2013-03-28 18:50:54 +0000 | [diff] [blame] | 1386 | return false; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1387 | } |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1388 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1389 | size_t |
| 1390 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::CalculateNumChildren () |
| 1391 | { |
| 1392 | return 1; |
| 1393 | } |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1394 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1395 | lldb::ValueObjectSP |
| 1396 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx) |
| 1397 | { |
| 1398 | if (idx == 0) |
| 1399 | return m_item_sp; |
| 1400 | return lldb::ValueObjectSP(); |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | bool |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1404 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::MightHaveChildren () |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1405 | { |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
| 1409 | size_t |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1410 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name) |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1411 | { |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1412 | if (name == ConstString("item")) |
| 1413 | return 0; |
| 1414 | return UINT32_MAX; |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1417 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::~VectorIteratorSyntheticFrontEnd () |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1418 | { |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1421 | template bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1422 | lldb_private::formatters::NSDataSummaryProvider<true> (ValueObject&, Stream&) ; |
| 1423 | |
| 1424 | template bool |
| 1425 | lldb_private::formatters::NSDataSummaryProvider<false> (ValueObject&, Stream&) ; |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1426 | |
| 1427 | template bool |
| 1428 | lldb_private::formatters::ObjCSELSummaryProvider<true> (ValueObject&, Stream&) ; |
| 1429 | |
| 1430 | template bool |
| 1431 | lldb_private::formatters::ObjCSELSummaryProvider<false> (ValueObject&, Stream&) ; |