Enrico Granata | caaf010 | 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 | |
Enrico Granata | f509c5e | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 10 | #include "lldb/DataFormatters/CXXFormatterFunctions.h" |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 11 | |
Dmitri Gribenko | 2a64f9a | 2013-01-30 15:05:59 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ConvertUTF.h" |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 13 | |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 14 | #include "lldb/Core/DataBufferHeap.h" |
| 15 | #include "lldb/Core/Error.h" |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Stream.h" |
| 17 | #include "lldb/Core/ValueObject.h" |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ValueObjectConstResult.h" |
| 19 | #include "lldb/Host/Endian.h" |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/ClangASTContext.h" |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 21 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 22 | #include "lldb/Target/Target.h" |
| 23 | |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 24 | #include <algorithm> |
| 25 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | using namespace lldb_private::formatters; |
| 29 | |
| 30 | bool |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 31 | lldb_private::formatters::ExtractValueFromObjCExpression (ValueObject &valobj, |
| 32 | const char* target_type, |
| 33 | const char* selector, |
| 34 | uint64_t &value) |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 35 | { |
| 36 | if (!target_type || !*target_type) |
| 37 | return false; |
| 38 | if (!selector || !*selector) |
| 39 | return false; |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 40 | StreamString expr; |
Daniel Malea | b9db9d5 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 41 | expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 42 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 43 | lldb::ValueObjectSP result_sp; |
| 44 | Target* target = exe_ctx.GetTargetPtr(); |
| 45 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
| 46 | if (!target || !stack_frame) |
| 47 | return false; |
Enrico Granata | d27026e | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 48 | |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 49 | EvaluateExpressionOptions options; |
Enrico Granata | d27026e | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 50 | options.SetCoerceToId(false) |
| 51 | .SetUnwindOnError(true) |
Sean Callanan | 99611fe | 2012-12-04 20:56:04 +0000 | [diff] [blame] | 52 | .SetKeepInMemory(true); |
Enrico Granata | d27026e | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 53 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 54 | target->EvaluateExpression(expr.GetData(), |
| 55 | stack_frame, |
Enrico Granata | d27026e | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 56 | result_sp, |
| 57 | options); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 58 | if (!result_sp) |
| 59 | return false; |
| 60 | value = result_sp->GetValueAsUnsigned(0); |
| 61 | return true; |
| 62 | } |
| 63 | |
Enrico Granata | ea68753 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 64 | bool |
| 65 | lldb_private::formatters::ExtractSummaryFromObjCExpression (ValueObject &valobj, |
| 66 | const char* target_type, |
| 67 | const char* selector, |
| 68 | Stream &stream) |
| 69 | { |
| 70 | if (!target_type || !*target_type) |
| 71 | return false; |
| 72 | if (!selector || !*selector) |
| 73 | return false; |
| 74 | StreamString expr; |
Enrico Granata | 04d3bb2 | 2013-02-19 01:14:06 +0000 | [diff] [blame] | 75 | expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector); |
Enrico Granata | ea68753 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 76 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 77 | lldb::ValueObjectSP result_sp; |
| 78 | Target* target = exe_ctx.GetTargetPtr(); |
| 79 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
| 80 | if (!target || !stack_frame) |
| 81 | return false; |
| 82 | |
| 83 | EvaluateExpressionOptions options; |
| 84 | options.SetCoerceToId(false) |
| 85 | .SetUnwindOnError(true) |
| 86 | .SetKeepInMemory(true) |
| 87 | .SetUseDynamic(lldb::eDynamicCanRunTarget); |
| 88 | |
| 89 | target->EvaluateExpression(expr.GetData(), |
| 90 | stack_frame, |
| 91 | result_sp, |
| 92 | options); |
| 93 | if (!result_sp) |
| 94 | return false; |
| 95 | stream.Printf("%s",result_sp->GetSummaryAsCString()); |
| 96 | return true; |
| 97 | } |
| 98 | |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 99 | lldb::ValueObjectSP |
| 100 | lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj, |
| 101 | const char* return_type, |
| 102 | const char* selector, |
| 103 | uint64_t index) |
| 104 | { |
| 105 | lldb::ValueObjectSP valobj_sp; |
| 106 | if (!return_type || !*return_type) |
| 107 | return valobj_sp; |
| 108 | if (!selector || !*selector) |
| 109 | return valobj_sp; |
| 110 | StreamString expr_path_stream; |
| 111 | valobj.GetExpressionPath(expr_path_stream, false); |
| 112 | StreamString expr; |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 113 | expr.Printf("(%s)[%s %s:%" PRId64 "]",return_type,expr_path_stream.GetData(),selector,index); |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 114 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 115 | lldb::ValueObjectSP result_sp; |
| 116 | Target* target = exe_ctx.GetTargetPtr(); |
| 117 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
| 118 | if (!target || !stack_frame) |
| 119 | return valobj_sp; |
| 120 | |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 121 | EvaluateExpressionOptions options; |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 122 | options.SetCoerceToId(false) |
| 123 | .SetUnwindOnError(true) |
| 124 | .SetKeepInMemory(true) |
| 125 | .SetUseDynamic(lldb::eDynamicCanRunTarget); |
| 126 | |
| 127 | target->EvaluateExpression(expr.GetData(), |
| 128 | stack_frame, |
| 129 | valobj_sp, |
| 130 | options); |
| 131 | return valobj_sp; |
| 132 | } |
| 133 | |
| 134 | lldb::ValueObjectSP |
| 135 | lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj, |
| 136 | const char* return_type, |
| 137 | const char* selector, |
| 138 | const char* key) |
| 139 | { |
| 140 | lldb::ValueObjectSP valobj_sp; |
| 141 | if (!return_type || !*return_type) |
| 142 | return valobj_sp; |
| 143 | if (!selector || !*selector) |
| 144 | return valobj_sp; |
| 145 | if (!key || !*key) |
| 146 | return valobj_sp; |
| 147 | StreamString expr_path_stream; |
| 148 | valobj.GetExpressionPath(expr_path_stream, false); |
| 149 | StreamString expr; |
| 150 | expr.Printf("(%s)[%s %s:%s]",return_type,expr_path_stream.GetData(),selector,key); |
| 151 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 152 | lldb::ValueObjectSP result_sp; |
| 153 | Target* target = exe_ctx.GetTargetPtr(); |
| 154 | StackFrame* stack_frame = exe_ctx.GetFramePtr(); |
| 155 | if (!target || !stack_frame) |
| 156 | return valobj_sp; |
| 157 | |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 158 | EvaluateExpressionOptions options; |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 159 | options.SetCoerceToId(false) |
| 160 | .SetUnwindOnError(true) |
| 161 | .SetKeepInMemory(true) |
| 162 | .SetUseDynamic(lldb::eDynamicCanRunTarget); |
| 163 | |
| 164 | target->EvaluateExpression(expr.GetData(), |
| 165 | stack_frame, |
| 166 | valobj_sp, |
| 167 | options); |
| 168 | return valobj_sp; |
| 169 | } |
| 170 | |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 171 | // use this call if you already have an LLDB-side buffer for the data |
| 172 | template<typename SourceDataType> |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 173 | static bool |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 174 | DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**, |
| 175 | const SourceDataType*, |
| 176 | UTF8**, |
| 177 | UTF8*, |
| 178 | ConversionFlags), |
| 179 | DataExtractor& data, |
| 180 | Stream& stream, |
| 181 | char prefix_token = '@', |
| 182 | char quote = '"', |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 183 | uint32_t sourceSize = 0) |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 184 | { |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 185 | if (prefix_token != 0) |
| 186 | stream.Printf("%c",prefix_token); |
| 187 | if (quote != 0) |
| 188 | stream.Printf("%c",quote); |
| 189 | if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 190 | { |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 191 | const int bufferSPSize = data.GetByteSize(); |
| 192 | if (sourceSize == 0) |
| 193 | { |
| 194 | const int origin_encoding = 8*sizeof(SourceDataType); |
Greg Clayton | 0cd3340 | 2013-02-08 21:59:34 +0000 | [diff] [blame] | 195 | sourceSize = bufferSPSize/(origin_encoding / 4); |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart(); |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 199 | SourceDataType *data_end_ptr = data_ptr + sourceSize; |
| 200 | |
| 201 | while (data_ptr < data_end_ptr) |
| 202 | { |
| 203 | if (!*data_ptr) |
| 204 | { |
| 205 | data_end_ptr = data_ptr; |
| 206 | break; |
| 207 | } |
| 208 | data_ptr++; |
| 209 | } |
| 210 | |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 211 | data_ptr = (SourceDataType*)data.GetDataStart(); |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 212 | |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 213 | lldb::DataBufferSP utf8_data_buffer_sp; |
| 214 | UTF8* utf8_data_ptr = nullptr; |
| 215 | UTF8* utf8_data_end_ptr = nullptr; |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 216 | |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 217 | if (ConvertFunction) |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 218 | { |
| 219 | utf8_data_buffer_sp.reset(new DataBufferHeap(bufferSPSize,0)); |
| 220 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); |
| 221 | utf8_data_end_ptr = utf8_data_ptr + bufferSPSize; |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 222 | ConvertFunction ( (const SourceDataType**)&data_ptr, data_end_ptr, &utf8_data_ptr, utf8_data_end_ptr, lenientConversion ); |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 223 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr |
| 224 | } |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 225 | else |
| 226 | { |
| 227 | // just copy the pointers - the cast is necessary to make the compiler happy |
| 228 | // but this should only happen if we are reading UTF8 data |
| 229 | utf8_data_ptr = (UTF8*)data_ptr; |
| 230 | utf8_data_end_ptr = (UTF8*)data_end_ptr; |
| 231 | } |
| 232 | |
Enrico Granata | b698579 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 233 | // since we tend to accept partial data (and even partially malformed data) |
| 234 | // we might end up with no NULL terminator before the end_ptr |
| 235 | // hence we need to take a slower route and ensure we stay within boundaries |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 236 | for (;utf8_data_ptr != utf8_data_end_ptr; utf8_data_ptr++) |
| 237 | { |
| 238 | if (!*utf8_data_ptr) |
| 239 | break; |
| 240 | stream.Printf("%c",*utf8_data_ptr); |
| 241 | } |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 242 | } |
| 243 | if (quote != 0) |
| 244 | stream.Printf("%c",quote); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | template<typename SourceDataType> |
| 249 | static bool |
| 250 | ReadUTFBufferAndDumpToStream (ConversionResult (*ConvertFunction) (const SourceDataType**, |
| 251 | const SourceDataType*, |
| 252 | UTF8**, |
| 253 | UTF8*, |
| 254 | ConversionFlags), |
| 255 | uint64_t location, |
| 256 | const ProcessSP& process_sp, |
| 257 | Stream& stream, |
| 258 | char prefix_token = '@', |
| 259 | char quote = '"', |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 260 | uint32_t sourceSize = 0) |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 261 | { |
| 262 | if (location == 0 || location == LLDB_INVALID_ADDRESS) |
| 263 | return false; |
| 264 | if (!process_sp) |
| 265 | return false; |
| 266 | |
| 267 | const int origin_encoding = 8*sizeof(SourceDataType); |
| 268 | if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32) |
| 269 | return false; |
| 270 | // if not UTF8, I need a conversion function to return proper UTF8 |
| 271 | if (origin_encoding != 8 && !ConvertFunction) |
| 272 | return false; |
| 273 | |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 274 | sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 275 | const int bufferSPSize = sourceSize * (origin_encoding >> 2); |
| 276 | |
| 277 | Error error; |
| 278 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0)); |
| 279 | |
| 280 | if (!buffer_sp->GetBytes()) |
| 281 | return false; |
| 282 | |
| 283 | size_t data_read = process_sp->ReadMemoryFromInferior(location, (char*)buffer_sp->GetBytes(), bufferSPSize, error); |
| 284 | if (error.Fail() || data_read == 0) |
| 285 | { |
| 286 | stream.Printf("unable to read data"); |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 287 | return true; |
| 288 | } |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 289 | |
| 290 | DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize()); |
| 291 | |
| 292 | return DumpUTFBufferToStream(ConvertFunction, data, stream, prefix_token, quote, sourceSize); |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | bool |
| 296 | lldb_private::formatters::Char16StringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 297 | { |
| 298 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 299 | if (!process_sp) |
| 300 | return false; |
| 301 | |
| 302 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 303 | |
| 304 | if (!valobj_addr) |
| 305 | return false; |
| 306 | |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 307 | if (!ReadUTFBufferAndDumpToStream<UTF16>(ConvertUTF16toUTF8,valobj_addr, |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 308 | process_sp, |
| 309 | stream, |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 310 | 'u')) |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 311 | { |
| 312 | stream.Printf("Summary Unavailable"); |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | bool |
| 320 | lldb_private::formatters::Char32StringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 321 | { |
| 322 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 323 | if (!process_sp) |
| 324 | return false; |
| 325 | |
| 326 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 327 | |
| 328 | if (!valobj_addr) |
| 329 | return false; |
| 330 | |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 331 | if (!ReadUTFBufferAndDumpToStream<UTF32>(ConvertUTF32toUTF8,valobj_addr, |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 332 | process_sp, |
| 333 | stream, |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 334 | 'U')) |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 335 | { |
| 336 | stream.Printf("Summary Unavailable"); |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | bool |
| 344 | lldb_private::formatters::WCharStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 345 | { |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 346 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 347 | if (!process_sp) |
| 348 | return false; |
| 349 | |
Enrico Granata | b698579 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 350 | lldb::addr_t data_addr = 0; |
| 351 | |
| 352 | if (valobj.IsPointerType()) |
| 353 | data_addr = valobj.GetValueAsUnsigned(0); |
| 354 | else if (valobj.IsArrayType()) |
| 355 | data_addr = valobj.GetAddressOf(); |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 356 | |
Enrico Granata | b698579 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 357 | if (data_addr == 0 || data_addr == LLDB_INVALID_ADDRESS) |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 358 | return false; |
| 359 | |
| 360 | clang::ASTContext* ast = valobj.GetClangAST(); |
| 361 | |
| 362 | if (!ast) |
| 363 | return false; |
| 364 | |
| 365 | uint32_t wchar_size = ClangASTType::GetClangTypeBitWidth(ast, ClangASTType::GetBasicType(ast, lldb::eBasicTypeWChar).GetOpaqueQualType()); |
| 366 | |
| 367 | switch (wchar_size) |
| 368 | { |
| 369 | case 8: |
| 370 | // utf 8 |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 371 | return ReadUTFBufferAndDumpToStream<UTF8>(nullptr, data_addr, |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 372 | process_sp, |
| 373 | stream, |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 374 | 'L'); |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 375 | case 16: |
| 376 | // utf 16 |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 377 | return ReadUTFBufferAndDumpToStream<UTF16>(ConvertUTF16toUTF8, data_addr, |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 378 | process_sp, |
| 379 | stream, |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 380 | 'L'); |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 381 | case 32: |
| 382 | // utf 32 |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 383 | return ReadUTFBufferAndDumpToStream<UTF32>(ConvertUTF32toUTF8, data_addr, |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 384 | process_sp, |
| 385 | stream, |
Enrico Granata | cd8cd61 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 386 | 'L'); |
| 387 | default: |
| 388 | stream.Printf("size for wchar_t is not valid"); |
| 389 | return true; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | bool |
| 395 | lldb_private::formatters::Char16SummaryProvider (ValueObject& valobj, Stream& stream) |
| 396 | { |
| 397 | DataExtractor data; |
| 398 | valobj.GetData(data); |
| 399 | |
| 400 | std::string value; |
| 401 | valobj.GetValueAsCString(lldb::eFormatUnicode16, value); |
| 402 | if (!value.empty()) |
| 403 | stream.Printf("%s ", value.c_str()); |
| 404 | |
| 405 | return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,data,stream, 'u','\'',1); |
| 406 | } |
| 407 | |
| 408 | bool |
| 409 | lldb_private::formatters::Char32SummaryProvider (ValueObject& valobj, Stream& stream) |
| 410 | { |
| 411 | DataExtractor data; |
| 412 | valobj.GetData(data); |
| 413 | |
| 414 | std::string value; |
| 415 | valobj.GetValueAsCString(lldb::eFormatUnicode32, value); |
| 416 | if (!value.empty()) |
| 417 | stream.Printf("%s ", value.c_str()); |
| 418 | |
| 419 | return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,data,stream, 'U','\'',1); |
| 420 | } |
| 421 | |
| 422 | bool |
| 423 | lldb_private::formatters::WCharSummaryProvider (ValueObject& valobj, Stream& stream) |
| 424 | { |
| 425 | DataExtractor data; |
| 426 | valobj.GetData(data); |
| 427 | |
| 428 | clang::ASTContext* ast = valobj.GetClangAST(); |
| 429 | |
| 430 | if (!ast) |
| 431 | return false; |
| 432 | |
| 433 | std::string value; |
| 434 | |
| 435 | uint32_t wchar_size = ClangASTType::GetClangTypeBitWidth(ast, ClangASTType::GetBasicType(ast, lldb::eBasicTypeWChar).GetOpaqueQualType()); |
| 436 | |
| 437 | switch (wchar_size) |
| 438 | { |
| 439 | case 8: |
| 440 | // utf 8 |
| 441 | valobj.GetValueAsCString(lldb::eFormatChar, value); |
| 442 | if (!value.empty()) |
| 443 | stream.Printf("%s ", value.c_str()); |
| 444 | return DumpUTFBufferToStream<UTF8>(nullptr, |
| 445 | data, |
| 446 | stream, |
| 447 | 'L', |
| 448 | '\'', |
| 449 | 1); |
| 450 | case 16: |
| 451 | // utf 16 |
| 452 | valobj.GetValueAsCString(lldb::eFormatUnicode16, value); |
| 453 | if (!value.empty()) |
| 454 | stream.Printf("%s ", value.c_str()); |
| 455 | return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8, |
| 456 | data, |
| 457 | stream, |
| 458 | 'L', |
| 459 | '\'', |
| 460 | 1); |
| 461 | case 32: |
| 462 | // utf 32 |
| 463 | valobj.GetValueAsCString(lldb::eFormatUnicode32, value); |
| 464 | if (!value.empty()) |
| 465 | stream.Printf("%s ", value.c_str()); |
| 466 | return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8, |
| 467 | data, |
| 468 | stream, |
| 469 | 'L', |
| 470 | '\'', |
| 471 | 1); |
Enrico Granata | 06d58b0 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 472 | default: |
| 473 | stream.Printf("size for wchar_t is not valid"); |
| 474 | return true; |
| 475 | } |
| 476 | return true; |
Enrico Granata | f5545f9 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Enrico Granata | b698579 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 479 | // this function extracts information from a libcxx std::basic_string<> |
| 480 | // irregardless of template arguments. it reports the size (in item count not bytes) |
| 481 | // and the location in memory where the string data can be found |
| 482 | static bool |
| 483 | ExtractLibcxxStringInfo (ValueObject& valobj, |
| 484 | ValueObjectSP &location_sp, |
| 485 | uint64_t& size) |
| 486 | { |
| 487 | ValueObjectSP D(valobj.GetChildAtIndexPath({0,0,0,0})); |
| 488 | if (!D) |
| 489 | return false; |
| 490 | |
| 491 | ValueObjectSP size_mode(D->GetChildAtIndexPath({1,0,0})); |
| 492 | if (!size_mode) |
| 493 | return false; |
| 494 | |
| 495 | uint64_t size_mode_value(size_mode->GetValueAsUnsigned(0)); |
| 496 | |
| 497 | if ((size_mode_value & 1) == 0) // this means the string is in short-mode and the data is stored inline |
| 498 | { |
| 499 | ValueObjectSP s(D->GetChildAtIndex(1, true)); |
| 500 | if (!s) |
| 501 | return false; |
| 502 | size = ((size_mode_value >> 1) % 256); |
| 503 | location_sp = s->GetChildAtIndex(1, true); |
| 504 | return (location_sp.get() != nullptr); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | ValueObjectSP l(D->GetChildAtIndex(0, true)); |
| 509 | if (!l) |
| 510 | return false; |
| 511 | location_sp = l->GetChildAtIndex(2, true); |
| 512 | ValueObjectSP size_vo(l->GetChildAtIndex(1, true)); |
| 513 | if (!size_vo || !location_sp) |
| 514 | return false; |
| 515 | size = size_vo->GetValueAsUnsigned(0); |
| 516 | return true; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | bool |
| 521 | lldb_private::formatters::LibcxxWStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 522 | { |
| 523 | uint64_t size = 0; |
| 524 | ValueObjectSP location_sp((ValueObject*)nullptr); |
| 525 | if (!ExtractLibcxxStringInfo(valobj, location_sp, size)) |
| 526 | return false; |
| 527 | if (size == 0) |
| 528 | { |
| 529 | stream.Printf("L\"\""); |
| 530 | return true; |
| 531 | } |
| 532 | if (!location_sp) |
| 533 | return false; |
| 534 | return WCharStringSummaryProvider(*location_sp.get(), stream); |
| 535 | } |
| 536 | |
| 537 | bool |
| 538 | lldb_private::formatters::LibcxxStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 539 | { |
| 540 | uint64_t size = 0; |
| 541 | ValueObjectSP location_sp((ValueObject*)nullptr); |
| 542 | if (!ExtractLibcxxStringInfo(valobj, location_sp, size)) |
| 543 | return false; |
| 544 | if (size == 0) |
| 545 | { |
| 546 | stream.Printf("\"\""); |
| 547 | return true; |
| 548 | } |
| 549 | if (!location_sp) |
| 550 | return false; |
| 551 | Error error; |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 552 | if (location_sp->ReadPointedString(stream, |
| 553 | error, |
| 554 | 0, // max length is decided by the settings |
| 555 | false) == 0) // do not honor array (terminates on first 0 byte even for a char[]) |
| 556 | stream.Printf("\"\""); // if nothing was read, print an empty string |
Enrico Granata | b698579 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 557 | return error.Success(); |
| 558 | } |
| 559 | |
Enrico Granata | 280b30f | 2013-03-15 18:55:30 +0000 | [diff] [blame] | 560 | bool |
| 561 | lldb_private::formatters::ObjCClassSummaryProvider (ValueObject& valobj, Stream& stream) |
| 562 | { |
| 563 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 564 | if (!process_sp) |
| 565 | return false; |
| 566 | |
| 567 | ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); |
| 568 | |
| 569 | if (!runtime) |
| 570 | return false; |
| 571 | |
| 572 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj.GetValueAsUnsigned(0))); |
| 573 | |
| 574 | if (!descriptor.get() || !descriptor->IsValid()) |
| 575 | return false; |
| 576 | |
| 577 | const char* class_name = descriptor->GetClassName().GetCString(); |
| 578 | |
| 579 | if (!class_name || !*class_name) |
| 580 | return false; |
| 581 | |
| 582 | stream.Printf("%s",class_name); |
| 583 | return true; |
| 584 | } |
| 585 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 586 | template<bool needs_at> |
| 587 | bool |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 588 | lldb_private::formatters::NSDataSummaryProvider (ValueObject& valobj, Stream& stream) |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 589 | { |
| 590 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 591 | if (!process_sp) |
| 592 | return false; |
| 593 | |
| 594 | ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); |
| 595 | |
| 596 | if (!runtime) |
| 597 | return false; |
| 598 | |
| 599 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj)); |
| 600 | |
| 601 | if (!descriptor.get() || !descriptor->IsValid()) |
| 602 | return false; |
| 603 | |
| 604 | bool is_64bit = (process_sp->GetAddressByteSize() == 8); |
| 605 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 606 | |
| 607 | if (!valobj_addr) |
| 608 | return false; |
| 609 | |
| 610 | uint64_t value = 0; |
| 611 | |
| 612 | const char* class_name = descriptor->GetClassName().GetCString(); |
Enrico Granata | 7685a56 | 2012-09-29 00:47:43 +0000 | [diff] [blame] | 613 | |
| 614 | if (!class_name || !*class_name) |
| 615 | return false; |
| 616 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 617 | if (!strcmp(class_name,"NSConcreteData") || |
| 618 | !strcmp(class_name,"NSConcreteMutableData") || |
| 619 | !strcmp(class_name,"__NSCFData")) |
| 620 | { |
| 621 | uint32_t offset = (is_64bit ? 16 : 8); |
| 622 | Error error; |
| 623 | value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, is_64bit ? 8 : 4, 0, error); |
| 624 | if (error.Fail()) |
| 625 | return false; |
| 626 | } |
| 627 | else |
| 628 | { |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 629 | if (!ExtractValueFromObjCExpression(valobj, "int", "length", value)) |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 633 | stream.Printf("%s%" PRIu64 " byte%s%s", |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 634 | (needs_at ? "@\"" : ""), |
| 635 | value, |
| 636 | (value > 1 ? "s" : ""), |
| 637 | (needs_at ? "\"" : "")); |
| 638 | |
| 639 | return true; |
| 640 | } |
| 641 | |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 642 | static bool |
| 643 | ReadAsciiBufferAndDumpToStream (lldb::addr_t location, |
| 644 | lldb::ProcessSP& process_sp, |
| 645 | Stream& dest, |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 646 | uint32_t size = 0, |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 647 | Error* error = NULL, |
| 648 | size_t *data_read = NULL, |
| 649 | char prefix_token = '@', |
| 650 | char quote = '"') |
| 651 | { |
| 652 | Error my_error; |
| 653 | size_t my_data_read; |
| 654 | if (!process_sp || location == 0) |
| 655 | return false; |
| 656 | |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 657 | size = std::min(size,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 658 | |
| 659 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0)); |
| 660 | |
| 661 | my_data_read = process_sp->ReadCStringFromMemory(location, (char*)buffer_sp->GetBytes(), size, my_error); |
| 662 | |
| 663 | if (error) |
| 664 | *error = my_error; |
| 665 | if (data_read) |
| 666 | *data_read = my_data_read; |
| 667 | |
| 668 | if (my_error.Fail()) |
| 669 | return false; |
| 670 | if (my_data_read) |
| 671 | dest.Printf("%c%c%s%c",prefix_token,quote,(char*)buffer_sp->GetBytes(),quote); |
| 672 | |
| 673 | return true; |
| 674 | } |
| 675 | |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 676 | #ifdef WANT_DEEP_PRINT |
| 677 | struct lldb__notInlineMutable { |
| 678 | void *buffer; |
| 679 | signed long length; |
| 680 | signed long capacity; |
| 681 | unsigned int hasGap:1; |
| 682 | unsigned int isFixedCapacity:1; |
| 683 | unsigned int isExternalMutable:1; |
| 684 | unsigned int capacityProvidedExternally:1; |
| 685 | #if __LP64__ |
| 686 | unsigned long desiredCapacity:60; |
| 687 | #else |
| 688 | unsigned long desiredCapacity:28; |
| 689 | #endif |
| 690 | void* contentsAllocator; |
| 691 | }; |
| 692 | |
| 693 | struct lldb__CFString { |
| 694 | uintptr_t _cfisa; |
| 695 | uint8_t _cfinfo[4]; |
| 696 | uint32_t _rc; |
| 697 | union { |
| 698 | struct __inline1 { |
| 699 | signed long length; |
| 700 | } inline1; |
| 701 | struct __notInlineImmutable1 { |
| 702 | void *buffer; |
| 703 | signed long length; |
| 704 | void* contentsDeallocator; |
| 705 | } notInlineImmutable1; |
| 706 | struct __notInlineImmutable2 { |
| 707 | void *buffer; |
| 708 | void* contentsDeallocator; |
| 709 | } notInlineImmutable2; |
| 710 | struct lldb__notInlineMutable notInlineMutable; |
| 711 | } variants; |
| 712 | }; |
| 713 | #endif |
| 714 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 715 | bool |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 716 | lldb_private::formatters::NSStringSummaryProvider (ValueObject& valobj, Stream& stream) |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 717 | { |
| 718 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 719 | if (!process_sp) |
| 720 | return false; |
| 721 | |
| 722 | ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); |
| 723 | |
| 724 | if (!runtime) |
| 725 | return false; |
| 726 | |
| 727 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj)); |
| 728 | |
| 729 | if (!descriptor.get() || !descriptor->IsValid()) |
| 730 | return false; |
| 731 | |
| 732 | uint32_t ptr_size = process_sp->GetAddressByteSize(); |
| 733 | |
| 734 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 735 | |
| 736 | if (!valobj_addr) |
| 737 | return false; |
| 738 | |
| 739 | const char* class_name = descriptor->GetClassName().GetCString(); |
| 740 | |
Enrico Granata | 25c9ade | 2012-09-29 00:45:53 +0000 | [diff] [blame] | 741 | if (!class_name || !*class_name) |
| 742 | return false; |
| 743 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 744 | uint64_t info_bits_location = valobj_addr + ptr_size; |
| 745 | if (process_sp->GetByteOrder() != lldb::eByteOrderLittle) |
| 746 | info_bits_location += 3; |
| 747 | |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 748 | Error error; |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 749 | |
| 750 | uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(info_bits_location, 1, 0, error); |
| 751 | if (error.Fail()) |
| 752 | return false; |
| 753 | |
| 754 | bool is_mutable = (info_bits & 1) == 1; |
| 755 | bool is_inline = (info_bits & 0x60) == 0; |
| 756 | bool has_explicit_length = (info_bits & (1 | 4)) != 4; |
| 757 | bool is_unicode = (info_bits & 0x10) == 0x10; |
| 758 | bool is_special = strcmp(class_name,"NSPathStore2") == 0; |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 759 | bool has_null = (info_bits & 8) == 8; |
| 760 | |
| 761 | size_t explicit_length = 0; |
| 762 | if (!has_null && has_explicit_length && !is_special) |
| 763 | { |
| 764 | lldb::addr_t explicit_length_offset = 2*ptr_size; |
| 765 | if (is_mutable and not is_inline) |
| 766 | explicit_length_offset = explicit_length_offset + ptr_size; // notInlineMutable.length; |
| 767 | else if (is_inline) |
| 768 | explicit_length = explicit_length + 0; // inline1.length; |
| 769 | else if (not is_inline and not is_mutable) |
| 770 | explicit_length_offset = explicit_length_offset + ptr_size; // notInlineImmutable1.length; |
| 771 | else |
| 772 | explicit_length_offset = 0; |
| 773 | |
| 774 | if (explicit_length_offset) |
| 775 | { |
| 776 | explicit_length_offset = valobj_addr + explicit_length_offset; |
| 777 | explicit_length = process_sp->ReadUnsignedIntegerFromMemory(explicit_length_offset, 4, 0, error); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | #ifdef WANT_DEEP_PRINT |
| 782 | lldb__CFString my_string_data; |
| 783 | process_sp->ReadMemory(valobj_addr, &my_string_data, sizeof(lldb__CFString),error); |
| 784 | |
| 785 | printf(R"( |
| 786 | __CFString my_string_data = { |
| 787 | uintptr_t _cfisa = %lu |
| 788 | uint8_t _cfinfo[4] = %c%c%c%c |
| 789 | uint32_t _rc = %d |
| 790 | union { |
| 791 | struct __inline1 { |
| 792 | signed long length = %ld |
| 793 | } inline1; |
| 794 | struct __notInlineImmutable1 { |
| 795 | void *buffer = %p |
| 796 | signed long length = %ld |
| 797 | void* contentsDeallocator = %p |
| 798 | } notInlineImmutable1; |
| 799 | struct __notInlineImmutable2 { |
| 800 | void *buffer = %p |
| 801 | void* contentsDeallocator = %p |
| 802 | } notInlineImmutable2; |
| 803 | struct __notInlineMutable notInlineMutable { |
| 804 | void *buffer = %p |
| 805 | signed long length = %ld |
| 806 | signed long capacity = %ld |
| 807 | unsigned int hasGap:1 = %d |
| 808 | unsigned int isFixedCapacity:1 = %d |
| 809 | unsigned int isExternalMutable:1 = %d |
| 810 | unsigned int capacityProvidedExternally:1 = %d |
| 811 | #if __LP64__ |
| 812 | unsigned long desiredCapacity:60 = %lu |
| 813 | #else |
| 814 | unsigned long desiredCapacity:28 = %lu |
| 815 | #endif |
| 816 | void* contentsAllocator = %p |
| 817 | } |
| 818 | } variants; |
| 819 | };\n)", |
| 820 | my_string_data._cfisa, |
| 821 | my_string_data._cfinfo[0],my_string_data._cfinfo[1],my_string_data._cfinfo[2],my_string_data._cfinfo[3], |
| 822 | my_string_data._rc, |
| 823 | my_string_data.variants.inline1.length, |
| 824 | my_string_data.variants.notInlineImmutable1.buffer, |
| 825 | my_string_data.variants.notInlineImmutable1.length, |
| 826 | my_string_data.variants.notInlineImmutable1.contentsDeallocator, |
| 827 | my_string_data.variants.notInlineImmutable2.buffer, |
| 828 | my_string_data.variants.notInlineImmutable2.contentsDeallocator, |
| 829 | my_string_data.variants.notInlineMutable.buffer, |
| 830 | my_string_data.variants.notInlineMutable.length, |
| 831 | my_string_data.variants.notInlineMutable.capacity, |
| 832 | my_string_data.variants.notInlineMutable.hasGap, |
| 833 | my_string_data.variants.notInlineMutable.isFixedCapacity, |
| 834 | my_string_data.variants.notInlineMutable.isExternalMutable, |
| 835 | my_string_data.variants.notInlineMutable.capacityProvidedExternally, |
| 836 | my_string_data.variants.notInlineMutable.desiredCapacity, |
| 837 | my_string_data.variants.notInlineMutable.desiredCapacity, |
| 838 | my_string_data.variants.notInlineMutable.contentsAllocator); |
| 839 | #endif |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 840 | |
| 841 | if (strcmp(class_name,"NSString") && |
| 842 | strcmp(class_name,"CFStringRef") && |
| 843 | strcmp(class_name,"CFMutableStringRef") && |
| 844 | strcmp(class_name,"__NSCFConstantString") && |
| 845 | strcmp(class_name,"__NSCFString") && |
| 846 | strcmp(class_name,"NSCFConstantString") && |
| 847 | strcmp(class_name,"NSCFString") && |
| 848 | strcmp(class_name,"NSPathStore2")) |
| 849 | { |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 850 | // not one of us - but tell me class name |
| 851 | stream.Printf("class name = %s",class_name); |
| 852 | return true; |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 855 | #ifdef WANT_DEEP_PRNT |
| 856 | stream.Printf("(M:%dI:%dL:%zuU:%dS:%dN:%d) ", |
| 857 | is_mutable, |
| 858 | is_inline, |
| 859 | explicit_length, |
| 860 | is_unicode, |
| 861 | is_special, |
| 862 | has_null); |
| 863 | #endif |
| 864 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 865 | if (is_mutable) |
| 866 | { |
| 867 | uint64_t location = 2 * ptr_size + valobj_addr; |
| 868 | location = process_sp->ReadPointerFromMemory(location, error); |
| 869 | if (error.Fail()) |
| 870 | return false; |
| 871 | if (has_explicit_length and is_unicode) |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 872 | return ReadUTFBufferAndDumpToStream<UTF16> (ConvertUTF16toUTF8,location, process_sp, stream, '@', '"', explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 873 | else |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 874 | return ReadAsciiBufferAndDumpToStream(location+1,process_sp,stream, explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 875 | } |
| 876 | else if (is_inline && has_explicit_length && !is_unicode && !is_special && !is_mutable) |
| 877 | { |
| 878 | uint64_t location = 3 * ptr_size + valobj_addr; |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 879 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 880 | } |
| 881 | else if (is_unicode) |
| 882 | { |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 883 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 884 | if (is_inline) |
| 885 | { |
| 886 | if (!has_explicit_length) |
| 887 | { |
| 888 | stream.Printf("found new combo"); |
| 889 | return true; |
| 890 | } |
| 891 | else |
| 892 | location += ptr_size; |
| 893 | } |
| 894 | else |
| 895 | { |
| 896 | location = process_sp->ReadPointerFromMemory(location, error); |
| 897 | if (error.Fail()) |
| 898 | return false; |
| 899 | } |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 900 | return ReadUTFBufferAndDumpToStream<UTF16> (ConvertUTF16toUTF8, location, process_sp, stream, '@', '"', explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 901 | } |
| 902 | else if (is_special) |
| 903 | { |
| 904 | uint64_t location = valobj_addr + (ptr_size == 8 ? 12 : 8); |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 905 | return ReadUTFBufferAndDumpToStream<UTF16> (ConvertUTF16toUTF8, location, process_sp, stream, '@', '"', explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 906 | } |
| 907 | else if (is_inline) |
| 908 | { |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 909 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 910 | if (!has_explicit_length) |
| 911 | location++; |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 912 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 913 | } |
| 914 | else |
| 915 | { |
Enrico Granata | f3a217e | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 916 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 917 | location = process_sp->ReadPointerFromMemory(location, error); |
| 918 | if (error.Fail()) |
| 919 | return false; |
Enrico Granata | fb1493d | 2013-04-02 21:25:34 +0000 | [diff] [blame^] | 920 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | stream.Printf("class name = %s",class_name); |
| 924 | return true; |
| 925 | |
| 926 | } |
| 927 | |
Enrico Granata | 9abbfba | 2012-10-03 23:53:45 +0000 | [diff] [blame] | 928 | bool |
Enrico Granata | dcffc1a | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 929 | lldb_private::formatters::NSAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 930 | { |
| 931 | TargetSP target_sp(valobj.GetTargetSP()); |
| 932 | if (!target_sp) |
| 933 | return false; |
| 934 | uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize(); |
| 935 | uint64_t pointee = valobj.GetValueAsUnsigned(0); |
| 936 | if (!pointee) |
| 937 | return false; |
| 938 | pointee += addr_size; |
| 939 | ClangASTType type(valobj.GetClangAST(),valobj.GetClangType()); |
| 940 | ExecutionContext exe_ctx(target_sp,false); |
| 941 | ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress("string_ptr", pointee, exe_ctx, type)); |
| 942 | if (!child_ptr_sp) |
| 943 | return false; |
| 944 | DataExtractor data; |
| 945 | child_ptr_sp->GetData(data); |
| 946 | ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData("string_data", data, exe_ctx, type)); |
| 947 | child_sp->GetValueAsUnsigned(0); |
| 948 | if (child_sp) |
| 949 | return NSStringSummaryProvider(*child_sp, stream); |
| 950 | return false; |
| 951 | } |
| 952 | |
| 953 | bool |
| 954 | lldb_private::formatters::NSMutableAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 955 | { |
| 956 | return NSAttributedStringSummaryProvider(valobj, stream); |
| 957 | } |
| 958 | |
| 959 | bool |
Enrico Granata | 9abbfba | 2012-10-03 23:53:45 +0000 | [diff] [blame] | 960 | lldb_private::formatters::RuntimeSpecificDescriptionSummaryProvider (ValueObject& valobj, Stream& stream) |
| 961 | { |
| 962 | stream.Printf("%s",valobj.GetObjectDescription()); |
| 963 | return true; |
| 964 | } |
| 965 | |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 966 | bool |
| 967 | lldb_private::formatters::ObjCBOOLSummaryProvider (ValueObject& valobj, Stream& stream) |
| 968 | { |
| 969 | const uint32_t type_info = ClangASTContext::GetTypeInfo(valobj.GetClangType(), |
| 970 | valobj.GetClangAST(), |
| 971 | NULL); |
| 972 | |
| 973 | ValueObjectSP real_guy_sp = valobj.GetSP(); |
| 974 | |
| 975 | if (type_info & ClangASTContext::eTypeIsPointer) |
| 976 | { |
| 977 | Error err; |
| 978 | real_guy_sp = valobj.Dereference(err); |
| 979 | if (err.Fail() || !real_guy_sp) |
| 980 | return false; |
| 981 | } |
| 982 | else if (type_info & ClangASTContext::eTypeIsReference) |
| 983 | { |
| 984 | real_guy_sp = valobj.GetChildAtIndex(0, true); |
| 985 | if (!real_guy_sp) |
| 986 | return false; |
| 987 | } |
| 988 | uint64_t value = real_guy_sp->GetValueAsUnsigned(0); |
| 989 | if (value == 0) |
| 990 | { |
| 991 | stream.Printf("NO"); |
| 992 | return true; |
| 993 | } |
| 994 | stream.Printf("YES"); |
| 995 | return true; |
| 996 | } |
| 997 | |
| 998 | template <bool is_sel_ptr> |
| 999 | bool |
| 1000 | lldb_private::formatters::ObjCSELSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1001 | { |
Enrico Granata | 1f9df78 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1002 | lldb::ValueObjectSP valobj_sp; |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1003 | |
Enrico Granata | 1f9df78 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1004 | if (!valobj.GetClangAST()) |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1005 | return false; |
Enrico Granata | 1f9df78 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1006 | void* char_opaque_type = valobj.GetClangAST()->CharTy.getAsOpaquePtr(); |
| 1007 | if (!char_opaque_type) |
| 1008 | return false; |
| 1009 | ClangASTType charstar(valobj.GetClangAST(),ClangASTType::GetPointerType(valobj.GetClangAST(), char_opaque_type)); |
| 1010 | |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1011 | ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); |
| 1012 | |
Enrico Granata | 1f9df78 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1013 | if (is_sel_ptr) |
| 1014 | { |
| 1015 | lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 1016 | if (data_address == LLDB_INVALID_ADDRESS) |
| 1017 | return false; |
| 1018 | valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address, exe_ctx, charstar); |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | DataExtractor data; |
| 1023 | valobj.GetData(data); |
| 1024 | valobj_sp = ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar); |
| 1025 | } |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1026 | |
Enrico Granata | 1f9df78 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1027 | if (!valobj_sp) |
| 1028 | return false; |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1029 | |
| 1030 | stream.Printf("%s",valobj_sp->GetSummaryAsCString()); |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
Enrico Granata | cba09f6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1034 | // POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001 |
| 1035 | // this call gives the POSIX equivalent of the Cocoa epoch |
| 1036 | time_t |
Enrico Granata | cc5d274 | 2013-03-19 22:58:48 +0000 | [diff] [blame] | 1037 | lldb_private::formatters::GetOSXEpoch () |
Enrico Granata | cba09f6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1038 | { |
| 1039 | static time_t epoch = 0; |
| 1040 | if (!epoch) |
| 1041 | { |
| 1042 | tzset(); |
| 1043 | tm tm_epoch; |
| 1044 | tm_epoch.tm_sec = 0; |
| 1045 | tm_epoch.tm_hour = 0; |
| 1046 | tm_epoch.tm_min = 0; |
| 1047 | tm_epoch.tm_mon = 0; |
| 1048 | tm_epoch.tm_mday = 1; |
| 1049 | tm_epoch.tm_year = 2001-1900; // for some reason, we need to subtract 1900 from this field. not sure why. |
| 1050 | tm_epoch.tm_isdst = -1; |
| 1051 | tm_epoch.tm_gmtoff = 0; |
| 1052 | tm_epoch.tm_zone = NULL; |
| 1053 | epoch = timegm(&tm_epoch); |
| 1054 | } |
| 1055 | return epoch; |
| 1056 | } |
| 1057 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1058 | size_t |
Enrico Granata | ea68753 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1059 | lldb_private::formatters::ExtractIndexFromString (const char* item_name) |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1060 | { |
| 1061 | if (!item_name || !*item_name) |
| 1062 | return UINT32_MAX; |
| 1063 | if (*item_name != '[') |
| 1064 | return UINT32_MAX; |
| 1065 | item_name++; |
Enrico Granata | ea68753 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1066 | char* endptr = NULL; |
| 1067 | unsigned long int idx = ::strtoul(item_name, &endptr, 0); |
| 1068 | if (idx == 0 && endptr == item_name) |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1069 | return UINT32_MAX; |
Enrico Granata | ea68753 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1070 | if (idx == ULONG_MAX) |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1071 | return UINT32_MAX; |
| 1072 | return idx; |
| 1073 | } |
| 1074 | |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1075 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::VectorIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp, |
| 1076 | ConstString item_name) : |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1077 | SyntheticChildrenFrontEnd(*valobj_sp.get()), |
| 1078 | m_exe_ctx_ref(), |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1079 | m_item_name(item_name), |
| 1080 | m_item_sp() |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1081 | { |
| 1082 | if (valobj_sp) |
| 1083 | Update(); |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1086 | bool |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1087 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::Update() |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1088 | { |
Enrico Granata | d32acdb | 2013-03-28 18:50:54 +0000 | [diff] [blame] | 1089 | m_item_sp.reset(); |
| 1090 | |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1091 | ValueObjectSP valobj_sp = m_backend.GetSP(); |
| 1092 | if (!valobj_sp) |
| 1093 | return false; |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1094 | |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1095 | if (!valobj_sp) |
| 1096 | return false; |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1097 | |
| 1098 | ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name,true)); |
| 1099 | if (!item_ptr) |
| 1100 | return false; |
| 1101 | if (item_ptr->GetValueAsUnsigned(0) == 0) |
| 1102 | return false; |
| 1103 | Error err; |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1104 | m_exe_ctx_ref = valobj_sp->GetExecutionContextRef(); |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1105 | m_item_sp = ValueObject::CreateValueObjectFromAddress("item", item_ptr->GetValueAsUnsigned(0), m_exe_ctx_ref, ClangASTType(item_ptr->GetClangAST(),ClangASTType::GetPointeeType(item_ptr->GetClangType()))); |
| 1106 | if (err.Fail()) |
| 1107 | m_item_sp.reset(); |
Enrico Granata | d32acdb | 2013-03-28 18:50:54 +0000 | [diff] [blame] | 1108 | return false; |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1109 | } |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1110 | |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1111 | size_t |
| 1112 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::CalculateNumChildren () |
| 1113 | { |
| 1114 | return 1; |
| 1115 | } |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1116 | |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1117 | lldb::ValueObjectSP |
| 1118 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx) |
| 1119 | { |
| 1120 | if (idx == 0) |
| 1121 | return m_item_sp; |
| 1122 | return lldb::ValueObjectSP(); |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | bool |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1126 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::MightHaveChildren () |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1127 | { |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
| 1131 | size_t |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1132 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name) |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1133 | { |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1134 | if (name == ConstString("item")) |
| 1135 | return 0; |
| 1136 | return UINT32_MAX; |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Enrico Granata | 32d7ee3 | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1139 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::~VectorIteratorSyntheticFrontEnd () |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1140 | { |
Enrico Granata | 689696c | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Enrico Granata | caaf010 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1143 | template bool |
Enrico Granata | f91e78f | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1144 | lldb_private::formatters::NSDataSummaryProvider<true> (ValueObject&, Stream&) ; |
| 1145 | |
| 1146 | template bool |
| 1147 | lldb_private::formatters::NSDataSummaryProvider<false> (ValueObject&, Stream&) ; |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1148 | |
| 1149 | template bool |
| 1150 | lldb_private::formatters::ObjCSELSummaryProvider<true> (ValueObject&, Stream&) ; |
| 1151 | |
| 1152 | template bool |
| 1153 | lldb_private::formatters::ObjCSELSummaryProvider<false> (ValueObject&, Stream&) ; |