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" |
Enrico Granata | ba8eb12 | 2014-07-30 21:07:50 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Thread.h" |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 25 | |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/ProcessStructReader.h" |
| 27 | |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | using namespace lldb_private::formatters; |
| 33 | |
Enrico Granata | ba8eb12 | 2014-07-30 21:07:50 +0000 | [diff] [blame] | 34 | StackFrame* |
| 35 | lldb_private::formatters::GetViableFrame (ExecutionContext exe_ctx) |
| 36 | { |
| 37 | StackFrame* frame = exe_ctx.GetFramePtr(); |
| 38 | if (frame) |
| 39 | return frame; |
| 40 | |
| 41 | Process* process = exe_ctx.GetProcessPtr(); |
| 42 | if (!process) |
| 43 | return nullptr; |
| 44 | |
| 45 | ThreadSP thread_sp(process->GetThreadList().GetSelectedThread()); |
| 46 | if (thread_sp) |
| 47 | return thread_sp->GetSelectedFrame().get(); |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 51 | bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 52 | lldb_private::formatters::ExtractValueFromObjCExpression (ValueObject &valobj, |
| 53 | const char* target_type, |
| 54 | const char* selector, |
| 55 | uint64_t &value) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 56 | { |
| 57 | if (!target_type || !*target_type) |
| 58 | return false; |
| 59 | if (!selector || !*selector) |
| 60 | return false; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 61 | StreamString expr; |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 62 | expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 63 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 64 | lldb::ValueObjectSP result_sp; |
| 65 | Target* target = exe_ctx.GetTargetPtr(); |
Enrico Granata | ba8eb12 | 2014-07-30 21:07:50 +0000 | [diff] [blame] | 66 | StackFrame* stack_frame = GetViableFrame(exe_ctx); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 67 | if (!target || !stack_frame) |
| 68 | return false; |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 69 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 70 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 71 | options.SetCoerceToId(false); |
| 72 | options.SetUnwindOnError(true); |
| 73 | options.SetKeepInMemory(true); |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 74 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 75 | target->EvaluateExpression(expr.GetData(), |
| 76 | stack_frame, |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 77 | result_sp, |
| 78 | options); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 79 | if (!result_sp) |
| 80 | return false; |
| 81 | value = result_sp->GetValueAsUnsigned(0); |
| 82 | return true; |
| 83 | } |
| 84 | |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 85 | bool |
| 86 | lldb_private::formatters::ExtractSummaryFromObjCExpression (ValueObject &valobj, |
| 87 | const char* target_type, |
| 88 | const char* selector, |
| 89 | Stream &stream) |
| 90 | { |
| 91 | if (!target_type || !*target_type) |
| 92 | return false; |
| 93 | if (!selector || !*selector) |
| 94 | return false; |
| 95 | StreamString expr; |
Enrico Granata | eac4a48 | 2013-02-19 01:14:06 +0000 | [diff] [blame] | 96 | expr.Printf("(%s)[(id)0x%" PRIx64 " %s]",target_type,valobj.GetPointerValue(),selector); |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 97 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 98 | lldb::ValueObjectSP result_sp; |
| 99 | Target* target = exe_ctx.GetTargetPtr(); |
Enrico Granata | ba8eb12 | 2014-07-30 21:07:50 +0000 | [diff] [blame] | 100 | StackFrame* stack_frame = GetViableFrame(exe_ctx); |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 101 | if (!target || !stack_frame) |
| 102 | return false; |
| 103 | |
| 104 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 105 | options.SetCoerceToId(false); |
| 106 | options.SetUnwindOnError(true); |
| 107 | options.SetKeepInMemory(true); |
| 108 | options.SetUseDynamic(lldb::eDynamicCanRunTarget); |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 109 | |
| 110 | target->EvaluateExpression(expr.GetData(), |
| 111 | stack_frame, |
| 112 | result_sp, |
| 113 | options); |
| 114 | if (!result_sp) |
| 115 | return false; |
| 116 | stream.Printf("%s",result_sp->GetSummaryAsCString()); |
| 117 | return true; |
| 118 | } |
| 119 | |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 120 | lldb::ValueObjectSP |
| 121 | lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj, |
| 122 | const char* return_type, |
| 123 | const char* selector, |
| 124 | uint64_t index) |
| 125 | { |
| 126 | lldb::ValueObjectSP valobj_sp; |
| 127 | if (!return_type || !*return_type) |
| 128 | return valobj_sp; |
| 129 | if (!selector || !*selector) |
| 130 | return valobj_sp; |
| 131 | StreamString expr_path_stream; |
| 132 | valobj.GetExpressionPath(expr_path_stream, false); |
| 133 | StreamString expr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 134 | 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] | 135 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 136 | lldb::ValueObjectSP result_sp; |
| 137 | Target* target = exe_ctx.GetTargetPtr(); |
Enrico Granata | ba8eb12 | 2014-07-30 21:07:50 +0000 | [diff] [blame] | 138 | StackFrame* stack_frame = GetViableFrame(exe_ctx); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 139 | if (!target || !stack_frame) |
| 140 | return valobj_sp; |
| 141 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 142 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 143 | options.SetCoerceToId(false); |
| 144 | options.SetUnwindOnError(true); |
| 145 | options.SetKeepInMemory(true); |
| 146 | options.SetUseDynamic(lldb::eDynamicCanRunTarget); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 147 | |
| 148 | target->EvaluateExpression(expr.GetData(), |
| 149 | stack_frame, |
| 150 | valobj_sp, |
| 151 | options); |
| 152 | return valobj_sp; |
| 153 | } |
| 154 | |
| 155 | lldb::ValueObjectSP |
| 156 | lldb_private::formatters::CallSelectorOnObject (ValueObject &valobj, |
| 157 | const char* return_type, |
| 158 | const char* selector, |
| 159 | const char* key) |
| 160 | { |
| 161 | lldb::ValueObjectSP valobj_sp; |
| 162 | if (!return_type || !*return_type) |
| 163 | return valobj_sp; |
| 164 | if (!selector || !*selector) |
| 165 | return valobj_sp; |
| 166 | if (!key || !*key) |
| 167 | return valobj_sp; |
| 168 | StreamString expr_path_stream; |
| 169 | valobj.GetExpressionPath(expr_path_stream, false); |
| 170 | StreamString expr; |
| 171 | expr.Printf("(%s)[%s %s:%s]",return_type,expr_path_stream.GetData(),selector,key); |
| 172 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 173 | lldb::ValueObjectSP result_sp; |
| 174 | Target* target = exe_ctx.GetTargetPtr(); |
Enrico Granata | ba8eb12 | 2014-07-30 21:07:50 +0000 | [diff] [blame] | 175 | StackFrame* stack_frame = GetViableFrame(exe_ctx); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 176 | if (!target || !stack_frame) |
| 177 | return valobj_sp; |
| 178 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 179 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 180 | options.SetCoerceToId(false); |
| 181 | options.SetUnwindOnError(true); |
| 182 | options.SetKeepInMemory(true); |
| 183 | options.SetUseDynamic(lldb::eDynamicCanRunTarget); |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 184 | |
| 185 | target->EvaluateExpression(expr.GetData(), |
| 186 | stack_frame, |
| 187 | valobj_sp, |
| 188 | options); |
| 189 | return valobj_sp; |
| 190 | } |
| 191 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 192 | // use this call if you already have an LLDB-side buffer for the data |
| 193 | template<typename SourceDataType> |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 194 | static bool |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 195 | DumpUTFBufferToStream (ConversionResult (*ConvertFunction) (const SourceDataType**, |
| 196 | const SourceDataType*, |
| 197 | UTF8**, |
| 198 | UTF8*, |
| 199 | ConversionFlags), |
| 200 | DataExtractor& data, |
| 201 | Stream& stream, |
| 202 | char prefix_token = '@', |
| 203 | char quote = '"', |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 204 | uint32_t sourceSize = 0) |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 205 | { |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 206 | if (prefix_token != 0) |
| 207 | stream.Printf("%c",prefix_token); |
| 208 | if (quote != 0) |
| 209 | stream.Printf("%c",quote); |
| 210 | if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 211 | { |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 212 | const int bufferSPSize = data.GetByteSize(); |
| 213 | if (sourceSize == 0) |
| 214 | { |
| 215 | const int origin_encoding = 8*sizeof(SourceDataType); |
Greg Clayton | aa4c47a | 2013-02-08 21:59:34 +0000 | [diff] [blame] | 216 | sourceSize = bufferSPSize/(origin_encoding / 4); |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | SourceDataType *data_ptr = (SourceDataType*)data.GetDataStart(); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 220 | SourceDataType *data_end_ptr = data_ptr + sourceSize; |
| 221 | |
| 222 | while (data_ptr < data_end_ptr) |
| 223 | { |
| 224 | if (!*data_ptr) |
| 225 | { |
| 226 | data_end_ptr = data_ptr; |
| 227 | break; |
| 228 | } |
| 229 | data_ptr++; |
| 230 | } |
| 231 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 232 | data_ptr = (SourceDataType*)data.GetDataStart(); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 233 | |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 234 | lldb::DataBufferSP utf8_data_buffer_sp; |
| 235 | UTF8* utf8_data_ptr = nullptr; |
| 236 | UTF8* utf8_data_end_ptr = nullptr; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 237 | |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 238 | if (ConvertFunction) |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 239 | { |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 240 | utf8_data_buffer_sp.reset(new DataBufferHeap(4*bufferSPSize,0)); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 241 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 242 | utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize(); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 243 | 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] | 244 | utf8_data_ptr = (UTF8*)utf8_data_buffer_sp->GetBytes(); // needed because the ConvertFunction will change the value of the data_ptr |
| 245 | } |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 246 | else |
| 247 | { |
| 248 | // just copy the pointers - the cast is necessary to make the compiler happy |
| 249 | // but this should only happen if we are reading UTF8 data |
| 250 | utf8_data_ptr = (UTF8*)data_ptr; |
| 251 | utf8_data_end_ptr = (UTF8*)data_end_ptr; |
| 252 | } |
| 253 | |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 254 | // since we tend to accept partial data (and even partially malformed data) |
| 255 | // we might end up with no NULL terminator before the end_ptr |
| 256 | // hence we need to take a slower route and ensure we stay within boundaries |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 257 | for (;utf8_data_ptr != utf8_data_end_ptr; utf8_data_ptr++) |
| 258 | { |
| 259 | if (!*utf8_data_ptr) |
| 260 | break; |
| 261 | stream.Printf("%c",*utf8_data_ptr); |
| 262 | } |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 263 | } |
| 264 | if (quote != 0) |
| 265 | stream.Printf("%c",quote); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | template<typename SourceDataType> |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 270 | class ReadUTFBufferAndDumpToStreamOptions |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 271 | { |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 272 | public: |
| 273 | typedef ConversionResult (*ConvertFunctionType) (const SourceDataType**, |
| 274 | const SourceDataType*, |
| 275 | UTF8**, |
| 276 | UTF8*, |
| 277 | ConversionFlags); |
| 278 | |
| 279 | ReadUTFBufferAndDumpToStreamOptions () : |
| 280 | m_conversion_function(NULL), |
| 281 | m_location(0), |
| 282 | m_process_sp(), |
| 283 | m_stream(NULL), |
| 284 | m_prefix_token('@'), |
| 285 | m_quote('"'), |
| 286 | m_source_size(0), |
| 287 | m_needs_zero_termination(true) |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | ReadUTFBufferAndDumpToStreamOptions& |
| 292 | SetConversionFunction (ConvertFunctionType f) |
| 293 | { |
| 294 | m_conversion_function = f; |
| 295 | return *this; |
| 296 | } |
| 297 | |
| 298 | ConvertFunctionType |
| 299 | GetConversionFunction () const |
| 300 | { |
| 301 | return m_conversion_function; |
| 302 | } |
| 303 | |
| 304 | ReadUTFBufferAndDumpToStreamOptions& |
| 305 | SetLocation (uint64_t l) |
| 306 | { |
| 307 | m_location = l; |
| 308 | return *this; |
| 309 | } |
| 310 | |
| 311 | uint64_t |
| 312 | GetLocation () const |
| 313 | { |
| 314 | return m_location; |
| 315 | } |
| 316 | |
| 317 | ReadUTFBufferAndDumpToStreamOptions& |
| 318 | SetProcessSP (ProcessSP p) |
| 319 | { |
| 320 | m_process_sp = p; |
| 321 | return *this; |
| 322 | } |
| 323 | |
| 324 | ProcessSP |
| 325 | GetProcessSP () const |
| 326 | { |
| 327 | return m_process_sp; |
| 328 | } |
| 329 | |
| 330 | ReadUTFBufferAndDumpToStreamOptions& |
| 331 | SetStream (Stream* s) |
| 332 | { |
| 333 | m_stream = s; |
| 334 | return *this; |
| 335 | } |
| 336 | |
| 337 | Stream* |
| 338 | GetStream () const |
| 339 | { |
| 340 | return m_stream; |
| 341 | } |
| 342 | |
| 343 | ReadUTFBufferAndDumpToStreamOptions& |
| 344 | SetPrefixToken (char p) |
| 345 | { |
| 346 | m_prefix_token = p; |
| 347 | return *this; |
| 348 | } |
| 349 | |
| 350 | char |
| 351 | GetPrefixToken () const |
| 352 | { |
| 353 | return m_prefix_token; |
| 354 | } |
| 355 | |
| 356 | ReadUTFBufferAndDumpToStreamOptions& |
| 357 | SetQuote (char q) |
| 358 | { |
| 359 | m_quote = q; |
| 360 | return *this; |
| 361 | } |
| 362 | |
| 363 | char |
| 364 | GetQuote () const |
| 365 | { |
| 366 | return m_quote; |
| 367 | } |
| 368 | |
| 369 | ReadUTFBufferAndDumpToStreamOptions& |
| 370 | SetSourceSize (uint32_t s) |
| 371 | { |
| 372 | m_source_size = s; |
| 373 | return *this; |
| 374 | } |
| 375 | |
| 376 | uint32_t |
| 377 | GetSourceSize () const |
| 378 | { |
| 379 | return m_source_size; |
| 380 | } |
| 381 | |
| 382 | ReadUTFBufferAndDumpToStreamOptions& |
| 383 | SetNeedsZeroTermination (bool z) |
| 384 | { |
| 385 | m_needs_zero_termination = z; |
| 386 | return *this; |
| 387 | } |
| 388 | |
| 389 | bool |
| 390 | GetNeedsZeroTermination () const |
| 391 | { |
| 392 | return m_needs_zero_termination; |
| 393 | } |
| 394 | |
| 395 | private: |
| 396 | ConvertFunctionType m_conversion_function; |
| 397 | uint64_t m_location; |
| 398 | ProcessSP m_process_sp; |
| 399 | Stream* m_stream; |
| 400 | char m_prefix_token; |
| 401 | char m_quote; |
| 402 | uint32_t m_source_size; |
| 403 | bool m_needs_zero_termination; |
| 404 | }; |
| 405 | |
| 406 | template<typename SourceDataType> |
| 407 | static bool |
| 408 | ReadUTFBufferAndDumpToStream (const ReadUTFBufferAndDumpToStreamOptions<SourceDataType>& options) |
| 409 | { |
| 410 | if (options.GetLocation() == 0 || options.GetLocation() == LLDB_INVALID_ADDRESS) |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 411 | return false; |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 412 | |
| 413 | ProcessSP process_sp(options.GetProcessSP()); |
| 414 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 415 | if (!process_sp) |
| 416 | return false; |
| 417 | |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 418 | const int type_width = sizeof(SourceDataType); |
| 419 | const int origin_encoding = 8 * type_width ; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 420 | if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32) |
| 421 | return false; |
| 422 | // if not UTF8, I need a conversion function to return proper UTF8 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 423 | if (origin_encoding != 8 && !options.GetConversionFunction()) |
| 424 | return false; |
| 425 | |
| 426 | if (!options.GetStream()) |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 427 | return false; |
| 428 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 429 | uint32_t sourceSize = options.GetSourceSize(); |
| 430 | bool needs_zero_terminator = options.GetNeedsZeroTermination(); |
| 431 | |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 432 | if (!sourceSize) |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 433 | { |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 434 | sourceSize = process_sp->GetTarget().GetMaximumSizeOfStringSummary(); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 435 | needs_zero_terminator = true; |
| 436 | } |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 437 | else |
| 438 | sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
| 439 | |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 440 | const int bufferSPSize = sourceSize * type_width; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 441 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 442 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0)); |
| 443 | |
| 444 | if (!buffer_sp->GetBytes()) |
| 445 | return false; |
| 446 | |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 447 | Error error; |
| 448 | char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes()); |
| 449 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 450 | size_t data_read = 0; |
| 451 | if (needs_zero_terminator) |
| 452 | data_read = process_sp->ReadStringFromMemory(options.GetLocation(), buffer, bufferSPSize, error, type_width); |
| 453 | else |
| 454 | data_read = process_sp->ReadMemoryFromInferior(options.GetLocation(), (char*)buffer_sp->GetBytes(), bufferSPSize, error); |
| 455 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 456 | if (error.Fail() || data_read == 0) |
| 457 | { |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 458 | options.GetStream()->Printf("unable to read data"); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 459 | return true; |
| 460 | } |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 461 | |
| 462 | DataExtractor data(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize()); |
| 463 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 464 | return DumpUTFBufferToStream(options.GetConversionFunction(), data, *options.GetStream(), options.GetPrefixToken(), options.GetQuote(), sourceSize); |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | bool |
| 468 | lldb_private::formatters::Char16StringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 469 | { |
| 470 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 471 | if (!process_sp) |
| 472 | return false; |
| 473 | |
| 474 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 475 | |
| 476 | if (!valobj_addr) |
| 477 | return false; |
| 478 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 479 | ReadUTFBufferAndDumpToStreamOptions<UTF16> options; |
| 480 | options.SetLocation(valobj_addr); |
| 481 | options.SetConversionFunction(ConvertUTF16toUTF8); |
| 482 | options.SetProcessSP(process_sp); |
| 483 | options.SetStream(&stream); |
| 484 | options.SetPrefixToken('u'); |
| 485 | |
| 486 | if (!ReadUTFBufferAndDumpToStream(options)) |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 487 | { |
| 488 | stream.Printf("Summary Unavailable"); |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | bool |
| 496 | lldb_private::formatters::Char32StringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 497 | { |
| 498 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 499 | if (!process_sp) |
| 500 | return false; |
| 501 | |
| 502 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 503 | |
| 504 | if (!valobj_addr) |
| 505 | return false; |
| 506 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 507 | ReadUTFBufferAndDumpToStreamOptions<UTF32> options; |
| 508 | options.SetLocation(valobj_addr); |
| 509 | options.SetConversionFunction(ConvertUTF32toUTF8); |
| 510 | options.SetProcessSP(process_sp); |
| 511 | options.SetStream(&stream); |
Enrico Granata | c03c586 | 2013-04-23 21:37:33 +0000 | [diff] [blame] | 512 | options.SetPrefixToken('U'); |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 513 | |
| 514 | if (!ReadUTFBufferAndDumpToStream(options)) |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 515 | { |
| 516 | stream.Printf("Summary Unavailable"); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | bool |
| 524 | lldb_private::formatters::WCharStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 525 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 526 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 527 | if (!process_sp) |
| 528 | return false; |
| 529 | |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 530 | lldb::addr_t data_addr = 0; |
| 531 | |
| 532 | if (valobj.IsPointerType()) |
| 533 | data_addr = valobj.GetValueAsUnsigned(0); |
| 534 | else if (valobj.IsArrayType()) |
| 535 | data_addr = valobj.GetAddressOf(); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 536 | |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 537 | if (data_addr == 0 || data_addr == LLDB_INVALID_ADDRESS) |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 538 | return false; |
| 539 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 540 | clang::ASTContext* ast = valobj.GetClangType().GetASTContext(); |
| 541 | |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 542 | if (!ast) |
| 543 | return false; |
| 544 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 545 | ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar); |
| 546 | const uint32_t wchar_size = wchar_clang_type.GetBitSize(); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 547 | |
| 548 | switch (wchar_size) |
| 549 | { |
| 550 | case 8: |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 551 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 552 | // utf 8 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 553 | |
| 554 | ReadUTFBufferAndDumpToStreamOptions<UTF8> options; |
| 555 | options.SetLocation(data_addr); |
| 556 | options.SetConversionFunction(nullptr); |
| 557 | options.SetProcessSP(process_sp); |
| 558 | options.SetStream(&stream); |
| 559 | options.SetPrefixToken('L'); |
| 560 | |
| 561 | return ReadUTFBufferAndDumpToStream(options); |
| 562 | } |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 563 | case 16: |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 564 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 565 | // utf 16 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 566 | ReadUTFBufferAndDumpToStreamOptions<UTF16> options; |
| 567 | options.SetLocation(data_addr); |
| 568 | options.SetConversionFunction(ConvertUTF16toUTF8); |
| 569 | options.SetProcessSP(process_sp); |
| 570 | options.SetStream(&stream); |
| 571 | options.SetPrefixToken('L'); |
| 572 | |
| 573 | return ReadUTFBufferAndDumpToStream(options); |
| 574 | } |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 575 | case 32: |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 576 | { |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 577 | // utf 32 |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 578 | ReadUTFBufferAndDumpToStreamOptions<UTF32> options; |
| 579 | options.SetLocation(data_addr); |
| 580 | options.SetConversionFunction(ConvertUTF32toUTF8); |
| 581 | options.SetProcessSP(process_sp); |
| 582 | options.SetStream(&stream); |
| 583 | options.SetPrefixToken('L'); |
| 584 | |
| 585 | return ReadUTFBufferAndDumpToStream(options); |
| 586 | } |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 587 | default: |
| 588 | stream.Printf("size for wchar_t is not valid"); |
| 589 | return true; |
| 590 | } |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | bool |
| 595 | lldb_private::formatters::Char16SummaryProvider (ValueObject& valobj, Stream& stream) |
| 596 | { |
| 597 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 598 | Error error; |
| 599 | valobj.GetData(data, error); |
| 600 | |
| 601 | if (error.Fail()) |
| 602 | return false; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 603 | |
| 604 | std::string value; |
| 605 | valobj.GetValueAsCString(lldb::eFormatUnicode16, value); |
| 606 | if (!value.empty()) |
| 607 | stream.Printf("%s ", value.c_str()); |
| 608 | |
| 609 | return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8,data,stream, 'u','\'',1); |
| 610 | } |
| 611 | |
| 612 | bool |
| 613 | lldb_private::formatters::Char32SummaryProvider (ValueObject& valobj, Stream& stream) |
| 614 | { |
| 615 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 616 | Error error; |
| 617 | valobj.GetData(data, error); |
| 618 | |
| 619 | if (error.Fail()) |
| 620 | return false; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 621 | |
| 622 | std::string value; |
| 623 | valobj.GetValueAsCString(lldb::eFormatUnicode32, value); |
| 624 | if (!value.empty()) |
| 625 | stream.Printf("%s ", value.c_str()); |
| 626 | |
| 627 | return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8,data,stream, 'U','\'',1); |
| 628 | } |
| 629 | |
| 630 | bool |
| 631 | lldb_private::formatters::WCharSummaryProvider (ValueObject& valobj, Stream& stream) |
| 632 | { |
| 633 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 634 | Error error; |
| 635 | valobj.GetData(data, error); |
| 636 | |
| 637 | if (error.Fail()) |
| 638 | return false; |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 639 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 640 | clang::ASTContext* ast = valobj.GetClangType().GetASTContext(); |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 641 | |
| 642 | if (!ast) |
| 643 | return false; |
| 644 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 645 | ClangASTType wchar_clang_type = ClangASTContext::GetBasicType(ast, lldb::eBasicTypeWChar); |
| 646 | const uint32_t wchar_size = wchar_clang_type.GetBitSize(); |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 647 | std::string value; |
| 648 | |
Enrico Granata | 93d5966 | 2013-01-14 23:53:26 +0000 | [diff] [blame] | 649 | switch (wchar_size) |
| 650 | { |
| 651 | case 8: |
| 652 | // utf 8 |
| 653 | valobj.GetValueAsCString(lldb::eFormatChar, value); |
| 654 | if (!value.empty()) |
| 655 | stream.Printf("%s ", value.c_str()); |
| 656 | return DumpUTFBufferToStream<UTF8>(nullptr, |
| 657 | data, |
| 658 | stream, |
| 659 | 'L', |
| 660 | '\'', |
| 661 | 1); |
| 662 | case 16: |
| 663 | // utf 16 |
| 664 | valobj.GetValueAsCString(lldb::eFormatUnicode16, value); |
| 665 | if (!value.empty()) |
| 666 | stream.Printf("%s ", value.c_str()); |
| 667 | return DumpUTFBufferToStream<UTF16>(ConvertUTF16toUTF8, |
| 668 | data, |
| 669 | stream, |
| 670 | 'L', |
| 671 | '\'', |
| 672 | 1); |
| 673 | case 32: |
| 674 | // utf 32 |
| 675 | valobj.GetValueAsCString(lldb::eFormatUnicode32, value); |
| 676 | if (!value.empty()) |
| 677 | stream.Printf("%s ", value.c_str()); |
| 678 | return DumpUTFBufferToStream<UTF32>(ConvertUTF32toUTF8, |
| 679 | data, |
| 680 | stream, |
| 681 | 'L', |
| 682 | '\'', |
| 683 | 1); |
Enrico Granata | 3835204 | 2013-01-11 02:44:00 +0000 | [diff] [blame] | 684 | default: |
| 685 | stream.Printf("size for wchar_t is not valid"); |
| 686 | return true; |
| 687 | } |
| 688 | return true; |
Enrico Granata | f68df12 | 2013-01-10 22:08:35 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 691 | // the field layout in a libc++ string (cap, side, data or data, size, cap) |
| 692 | enum LibcxxStringLayoutMode |
| 693 | { |
| 694 | eLibcxxStringLayoutModeCSD = 0, |
| 695 | eLibcxxStringLayoutModeDSC = 1, |
| 696 | eLibcxxStringLayoutModeInvalid = 0xffff |
| 697 | }; |
| 698 | |
| 699 | // this function abstracts away the layout and mode details of a libc++ string |
| 700 | // and returns the address of the data and the size ready for callers to consume |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 701 | static bool |
| 702 | ExtractLibcxxStringInfo (ValueObject& valobj, |
| 703 | ValueObjectSP &location_sp, |
| 704 | uint64_t& size) |
| 705 | { |
| 706 | ValueObjectSP D(valobj.GetChildAtIndexPath({0,0,0,0})); |
| 707 | if (!D) |
| 708 | return false; |
| 709 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 710 | ValueObjectSP layout_decider(D->GetChildAtIndexPath({0,0})); |
| 711 | |
| 712 | // this child should exist |
| 713 | if (!layout_decider) |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 714 | return false; |
| 715 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 716 | ConstString g_data_name("__data_"); |
| 717 | ConstString g_size_name("__size_"); |
| 718 | bool short_mode = false; // this means the string is in short-mode and the data is stored inline |
| 719 | LibcxxStringLayoutMode layout = (layout_decider->GetName() == g_data_name) ? eLibcxxStringLayoutModeDSC : eLibcxxStringLayoutModeCSD; |
| 720 | uint64_t size_mode_value = 0; |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 721 | |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 722 | if (layout == eLibcxxStringLayoutModeDSC) |
| 723 | { |
| 724 | ValueObjectSP size_mode(D->GetChildAtIndexPath({1,1,0})); |
| 725 | if (!size_mode) |
| 726 | return false; |
| 727 | |
| 728 | if (size_mode->GetName() != g_size_name) |
| 729 | { |
| 730 | // we are hitting the padding structure, move along |
| 731 | size_mode = D->GetChildAtIndexPath({1,1,1}); |
| 732 | if (!size_mode) |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | size_mode_value = (size_mode->GetValueAsUnsigned(0)); |
| 737 | short_mode = ((size_mode_value & 0x80) == 0); |
| 738 | } |
| 739 | else |
| 740 | { |
| 741 | ValueObjectSP size_mode(D->GetChildAtIndexPath({1,0,0})); |
| 742 | if (!size_mode) |
| 743 | return false; |
| 744 | |
| 745 | size_mode_value = (size_mode->GetValueAsUnsigned(0)); |
| 746 | short_mode = ((size_mode_value & 1) == 0); |
| 747 | } |
| 748 | |
| 749 | if (short_mode) |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 750 | { |
| 751 | ValueObjectSP s(D->GetChildAtIndex(1, true)); |
| 752 | if (!s) |
| 753 | return false; |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 754 | location_sp = s->GetChildAtIndex((layout == eLibcxxStringLayoutModeDSC) ? 0 : 1, true); |
| 755 | size = (layout == eLibcxxStringLayoutModeDSC) ? size_mode_value : ((size_mode_value >> 1) % 256); |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 756 | return (location_sp.get() != nullptr); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | ValueObjectSP l(D->GetChildAtIndex(0, true)); |
| 761 | if (!l) |
| 762 | return false; |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 763 | // we can use the layout_decider object as the data pointer |
| 764 | location_sp = (layout == eLibcxxStringLayoutModeDSC) ? layout_decider : l->GetChildAtIndex(2, true); |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 765 | ValueObjectSP size_vo(l->GetChildAtIndex(1, true)); |
| 766 | if (!size_vo || !location_sp) |
| 767 | return false; |
| 768 | size = size_vo->GetValueAsUnsigned(0); |
| 769 | return true; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | bool |
| 774 | lldb_private::formatters::LibcxxWStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 775 | { |
| 776 | uint64_t size = 0; |
| 777 | ValueObjectSP location_sp((ValueObject*)nullptr); |
| 778 | if (!ExtractLibcxxStringInfo(valobj, location_sp, size)) |
| 779 | return false; |
| 780 | if (size == 0) |
| 781 | { |
| 782 | stream.Printf("L\"\""); |
| 783 | return true; |
Enrico Granata | a3962a7 | 2013-05-15 00:47:46 +0000 | [diff] [blame] | 784 | } |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 785 | if (!location_sp) |
| 786 | return false; |
| 787 | return WCharStringSummaryProvider(*location_sp.get(), stream); |
| 788 | } |
| 789 | |
| 790 | bool |
| 791 | lldb_private::formatters::LibcxxStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 792 | { |
| 793 | uint64_t size = 0; |
| 794 | ValueObjectSP location_sp((ValueObject*)nullptr); |
| 795 | if (!ExtractLibcxxStringInfo(valobj, location_sp, size)) |
| 796 | return false; |
| 797 | if (size == 0) |
| 798 | { |
| 799 | stream.Printf("\"\""); |
| 800 | return true; |
| 801 | } |
| 802 | if (!location_sp) |
| 803 | return false; |
| 804 | Error error; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 805 | if (location_sp->ReadPointedString(stream, |
| 806 | error, |
| 807 | 0, // max length is decided by the settings |
| 808 | false) == 0) // do not honor array (terminates on first 0 byte even for a char[]) |
| 809 | stream.Printf("\"\""); // if nothing was read, print an empty string |
Enrico Granata | 3309d88 | 2013-01-12 01:00:22 +0000 | [diff] [blame] | 810 | return error.Success(); |
| 811 | } |
| 812 | |
Enrico Granata | 5590086 | 2013-03-15 18:55:30 +0000 | [diff] [blame] | 813 | bool |
| 814 | lldb_private::formatters::ObjCClassSummaryProvider (ValueObject& valobj, Stream& stream) |
| 815 | { |
| 816 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 817 | if (!process_sp) |
| 818 | return false; |
| 819 | |
| 820 | ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); |
| 821 | |
| 822 | if (!runtime) |
| 823 | return false; |
| 824 | |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 825 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0))); |
Enrico Granata | 5590086 | 2013-03-15 18:55:30 +0000 | [diff] [blame] | 826 | |
| 827 | if (!descriptor.get() || !descriptor->IsValid()) |
| 828 | return false; |
| 829 | |
| 830 | const char* class_name = descriptor->GetClassName().GetCString(); |
| 831 | |
| 832 | if (!class_name || !*class_name) |
| 833 | return false; |
| 834 | |
| 835 | stream.Printf("%s",class_name); |
| 836 | return true; |
| 837 | } |
| 838 | |
Enrico Granata | c76b97b | 2013-04-26 00:59:02 +0000 | [diff] [blame] | 839 | class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd |
| 840 | { |
| 841 | public: |
| 842 | ObjCClassSyntheticChildrenFrontEnd (lldb::ValueObjectSP valobj_sp) : |
| 843 | SyntheticChildrenFrontEnd(*valobj_sp.get()) |
| 844 | { |
| 845 | } |
| 846 | |
| 847 | virtual size_t |
| 848 | CalculateNumChildren () |
| 849 | { |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | virtual lldb::ValueObjectSP |
| 854 | GetChildAtIndex (size_t idx) |
| 855 | { |
| 856 | return lldb::ValueObjectSP(); |
| 857 | } |
| 858 | |
| 859 | virtual bool |
| 860 | Update() |
| 861 | { |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | virtual bool |
| 866 | MightHaveChildren () |
| 867 | { |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | virtual size_t |
| 872 | GetIndexOfChildWithName (const ConstString &name) |
| 873 | { |
| 874 | return UINT32_MAX; |
| 875 | } |
| 876 | |
| 877 | virtual |
| 878 | ~ObjCClassSyntheticChildrenFrontEnd () |
| 879 | { |
| 880 | } |
| 881 | }; |
| 882 | |
| 883 | SyntheticChildrenFrontEnd* |
| 884 | lldb_private::formatters::ObjCClassSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp) |
| 885 | { |
| 886 | return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp); |
| 887 | } |
| 888 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 889 | template<bool needs_at> |
| 890 | bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 891 | lldb_private::formatters::NSDataSummaryProvider (ValueObject& valobj, Stream& stream) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 892 | { |
| 893 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 894 | if (!process_sp) |
| 895 | return false; |
| 896 | |
| 897 | ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); |
| 898 | |
| 899 | if (!runtime) |
| 900 | return false; |
| 901 | |
| 902 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj)); |
| 903 | |
| 904 | if (!descriptor.get() || !descriptor->IsValid()) |
| 905 | return false; |
| 906 | |
| 907 | bool is_64bit = (process_sp->GetAddressByteSize() == 8); |
| 908 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 909 | |
| 910 | if (!valobj_addr) |
| 911 | return false; |
| 912 | |
| 913 | uint64_t value = 0; |
| 914 | |
| 915 | const char* class_name = descriptor->GetClassName().GetCString(); |
Enrico Granata | 6d39077 | 2012-09-29 00:47:43 +0000 | [diff] [blame] | 916 | |
| 917 | if (!class_name || !*class_name) |
| 918 | return false; |
| 919 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 920 | if (!strcmp(class_name,"NSConcreteData") || |
| 921 | !strcmp(class_name,"NSConcreteMutableData") || |
| 922 | !strcmp(class_name,"__NSCFData")) |
| 923 | { |
| 924 | uint32_t offset = (is_64bit ? 16 : 8); |
| 925 | Error error; |
| 926 | value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, is_64bit ? 8 : 4, 0, error); |
| 927 | if (error.Fail()) |
| 928 | return false; |
| 929 | } |
| 930 | else |
| 931 | { |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 932 | if (!ExtractValueFromObjCExpression(valobj, "int", "length", value)) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 933 | return false; |
| 934 | } |
| 935 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 936 | stream.Printf("%s%" PRIu64 " byte%s%s", |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 937 | (needs_at ? "@\"" : ""), |
| 938 | value, |
Enrico Granata | 1c333d0 | 2013-05-31 01:14:22 +0000 | [diff] [blame] | 939 | (value != 1 ? "s" : ""), |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 940 | (needs_at ? "\"" : "")); |
| 941 | |
| 942 | return true; |
| 943 | } |
| 944 | |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 945 | static bool |
| 946 | ReadAsciiBufferAndDumpToStream (lldb::addr_t location, |
| 947 | lldb::ProcessSP& process_sp, |
| 948 | Stream& dest, |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 949 | uint32_t size = 0, |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 950 | Error* error = NULL, |
| 951 | size_t *data_read = NULL, |
| 952 | char prefix_token = '@', |
| 953 | char quote = '"') |
| 954 | { |
| 955 | Error my_error; |
| 956 | size_t my_data_read; |
| 957 | if (!process_sp || location == 0) |
| 958 | return false; |
| 959 | |
Enrico Granata | 23ab35a | 2013-04-02 23:07:55 +0000 | [diff] [blame] | 960 | if (!size) |
| 961 | size = process_sp->GetTarget().GetMaximumSizeOfStringSummary(); |
| 962 | else |
| 963 | size = std::min(size,process_sp->GetTarget().GetMaximumSizeOfStringSummary()); |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 964 | |
| 965 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(size,0)); |
| 966 | |
| 967 | my_data_read = process_sp->ReadCStringFromMemory(location, (char*)buffer_sp->GetBytes(), size, my_error); |
| 968 | |
| 969 | if (error) |
| 970 | *error = my_error; |
| 971 | if (data_read) |
| 972 | *data_read = my_data_read; |
| 973 | |
| 974 | if (my_error.Fail()) |
| 975 | return false; |
Enrico Granata | 5a9c4fe | 2013-05-17 23:28:13 +0000 | [diff] [blame] | 976 | |
| 977 | dest.Printf("%c%c",prefix_token,quote); |
| 978 | |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 979 | if (my_data_read) |
Enrico Granata | 5a9c4fe | 2013-05-17 23:28:13 +0000 | [diff] [blame] | 980 | dest.Printf("%s",(char*)buffer_sp->GetBytes()); |
| 981 | |
| 982 | dest.Printf("%c",quote); |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 987 | bool |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 988 | lldb_private::formatters::NSTaggedString_SummaryProvider (ObjCLanguageRuntime::ClassDescriptorSP descriptor, Stream& stream) |
| 989 | { |
| 990 | if (!descriptor) |
| 991 | return false; |
| 992 | uint64_t len_bits = 0, data_bits = 0; |
| 993 | if (!descriptor->GetTaggedPointerInfo(&len_bits,&data_bits,nullptr)) |
| 994 | return false; |
| 995 | |
| 996 | static const int g_MaxNonBitmaskedLen = 7; //TAGGED_STRING_UNPACKED_MAXLEN |
| 997 | static const int g_SixbitMaxLen = 9; |
| 998 | static const int g_fiveBitMaxLen = 11; |
| 999 | |
| 1000 | static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013" "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX"; |
| 1001 | |
| 1002 | if (len_bits > g_fiveBitMaxLen) |
| 1003 | return false; |
| 1004 | |
| 1005 | // this is a fairly ugly trick - pretend that the numeric value is actually a char* |
| 1006 | // this works under a few assumptions: |
| 1007 | // little endian architecture |
| 1008 | // sizeof(uint64_t) > g_MaxNonBitmaskedLen |
| 1009 | if (len_bits <= g_MaxNonBitmaskedLen) |
| 1010 | { |
| 1011 | stream.Printf("@\"%s\"",(const char*)&data_bits); |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | // if the data is bitmasked, we need to actually process the bytes |
| 1016 | uint8_t bitmask = 0; |
| 1017 | uint8_t shift_offset = 0; |
| 1018 | |
| 1019 | if (len_bits <= g_SixbitMaxLen) |
| 1020 | { |
| 1021 | bitmask = 0x03f; |
| 1022 | shift_offset = 6; |
| 1023 | } |
| 1024 | else |
| 1025 | { |
| 1026 | bitmask = 0x01f; |
| 1027 | shift_offset = 5; |
| 1028 | } |
| 1029 | |
| 1030 | std::vector<uint8_t> bytes; |
| 1031 | bytes.resize(len_bits); |
| 1032 | for (; len_bits > 0; data_bits >>= shift_offset, --len_bits) |
| 1033 | { |
| 1034 | uint8_t packed = data_bits & bitmask; |
| 1035 | bytes.insert(bytes.begin(), sixBitToCharLookup[packed]); |
| 1036 | } |
| 1037 | |
| 1038 | stream.Printf("@\"%s\"",&bytes[0]); |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 1042 | static ClangASTType |
| 1043 | GetNSPathStore2Type (Target &target) |
| 1044 | { |
| 1045 | static ConstString g_type_name("__lldb_autogen_nspathstore2"); |
| 1046 | |
| 1047 | ClangASTContext *ast_ctx = target.GetScratchClangASTContext(); |
| 1048 | |
| 1049 | if (!ast_ctx) |
| 1050 | return ClangASTType(); |
| 1051 | |
| 1052 | ClangASTType voidstar = ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType(); |
| 1053 | ClangASTType uint32 = ast_ctx->GetIntTypeFromBitSize(32, false); |
| 1054 | |
| 1055 | return ast_ctx->GetOrCreateStructForIdentifier(g_type_name, { |
| 1056 | {"isa",voidstar}, |
| 1057 | {"lengthAndRef",uint32}, |
| 1058 | {"buffer",voidstar} |
| 1059 | }); |
| 1060 | } |
| 1061 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1062 | bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1063 | lldb_private::formatters::NSStringSummaryProvider (ValueObject& valobj, Stream& stream) |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1064 | { |
| 1065 | ProcessSP process_sp = valobj.GetProcessSP(); |
| 1066 | if (!process_sp) |
| 1067 | return false; |
| 1068 | |
| 1069 | ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); |
| 1070 | |
| 1071 | if (!runtime) |
| 1072 | return false; |
| 1073 | |
| 1074 | ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj)); |
| 1075 | |
| 1076 | if (!descriptor.get() || !descriptor->IsValid()) |
| 1077 | return false; |
| 1078 | |
| 1079 | uint32_t ptr_size = process_sp->GetAddressByteSize(); |
| 1080 | |
| 1081 | lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0); |
| 1082 | |
| 1083 | if (!valobj_addr) |
| 1084 | return false; |
| 1085 | |
| 1086 | const char* class_name = descriptor->GetClassName().GetCString(); |
| 1087 | |
Enrico Granata | 60b81df | 2012-09-29 00:45:53 +0000 | [diff] [blame] | 1088 | if (!class_name || !*class_name) |
| 1089 | return false; |
| 1090 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1091 | bool is_tagged_ptr = (0 == strcmp(class_name,"NSTaggedPointerString")) && descriptor->GetTaggedPointerInfo(); |
| 1092 | // for a tagged pointer, the descriptor has everything we need |
| 1093 | if (is_tagged_ptr) |
| 1094 | return NSTaggedString_SummaryProvider(descriptor, stream); |
| 1095 | |
| 1096 | // if not a tagged pointer that we know about, try the normal route |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1097 | uint64_t info_bits_location = valobj_addr + ptr_size; |
| 1098 | if (process_sp->GetByteOrder() != lldb::eByteOrderLittle) |
| 1099 | info_bits_location += 3; |
| 1100 | |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1101 | Error error; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1102 | |
| 1103 | uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(info_bits_location, 1, 0, error); |
| 1104 | if (error.Fail()) |
| 1105 | return false; |
| 1106 | |
| 1107 | bool is_mutable = (info_bits & 1) == 1; |
| 1108 | bool is_inline = (info_bits & 0x60) == 0; |
| 1109 | bool has_explicit_length = (info_bits & (1 | 4)) != 4; |
| 1110 | bool is_unicode = (info_bits & 0x10) == 0x10; |
| 1111 | bool is_special = strcmp(class_name,"NSPathStore2") == 0; |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1112 | bool has_null = (info_bits & 8) == 8; |
| 1113 | |
| 1114 | size_t explicit_length = 0; |
| 1115 | if (!has_null && has_explicit_length && !is_special) |
| 1116 | { |
| 1117 | lldb::addr_t explicit_length_offset = 2*ptr_size; |
Virgile Bello | f02a3c5 | 2013-08-27 16:24:58 +0000 | [diff] [blame] | 1118 | if (is_mutable && !is_inline) |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1119 | explicit_length_offset = explicit_length_offset + ptr_size; // notInlineMutable.length; |
| 1120 | else if (is_inline) |
| 1121 | explicit_length = explicit_length + 0; // inline1.length; |
Virgile Bello | f02a3c5 | 2013-08-27 16:24:58 +0000 | [diff] [blame] | 1122 | else if (!is_inline && !is_mutable) |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1123 | explicit_length_offset = explicit_length_offset + ptr_size; // notInlineImmutable1.length; |
| 1124 | else |
| 1125 | explicit_length_offset = 0; |
| 1126 | |
| 1127 | if (explicit_length_offset) |
| 1128 | { |
| 1129 | explicit_length_offset = valobj_addr + explicit_length_offset; |
| 1130 | explicit_length = process_sp->ReadUnsignedIntegerFromMemory(explicit_length_offset, 4, 0, error); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1134 | if (strcmp(class_name,"NSString") && |
| 1135 | strcmp(class_name,"CFStringRef") && |
| 1136 | strcmp(class_name,"CFMutableStringRef") && |
| 1137 | strcmp(class_name,"__NSCFConstantString") && |
| 1138 | strcmp(class_name,"__NSCFString") && |
| 1139 | strcmp(class_name,"NSCFConstantString") && |
| 1140 | strcmp(class_name,"NSCFString") && |
| 1141 | strcmp(class_name,"NSPathStore2")) |
| 1142 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1143 | // not one of us - but tell me class name |
| 1144 | stream.Printf("class name = %s",class_name); |
| 1145 | return true; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | if (is_mutable) |
| 1149 | { |
| 1150 | uint64_t location = 2 * ptr_size + valobj_addr; |
| 1151 | location = process_sp->ReadPointerFromMemory(location, error); |
| 1152 | if (error.Fail()) |
| 1153 | return false; |
Virgile Bello | f02a3c5 | 2013-08-27 16:24:58 +0000 | [diff] [blame] | 1154 | if (has_explicit_length && is_unicode) |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1155 | { |
| 1156 | ReadUTFBufferAndDumpToStreamOptions<UTF16> options; |
| 1157 | options.SetConversionFunction(ConvertUTF16toUTF8); |
| 1158 | options.SetLocation(location); |
| 1159 | options.SetProcessSP(process_sp); |
| 1160 | options.SetStream(&stream); |
| 1161 | options.SetPrefixToken('@'); |
| 1162 | options.SetQuote('"'); |
| 1163 | options.SetSourceSize(explicit_length); |
| 1164 | options.SetNeedsZeroTermination(false); |
| 1165 | return ReadUTFBufferAndDumpToStream (options); |
| 1166 | } |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1167 | else |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1168 | return ReadAsciiBufferAndDumpToStream(location+1,process_sp,stream, explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1169 | } |
| 1170 | else if (is_inline && has_explicit_length && !is_unicode && !is_special && !is_mutable) |
| 1171 | { |
| 1172 | uint64_t location = 3 * ptr_size + valobj_addr; |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1173 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1174 | } |
| 1175 | else if (is_unicode) |
| 1176 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1177 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1178 | if (is_inline) |
| 1179 | { |
| 1180 | if (!has_explicit_length) |
| 1181 | { |
| 1182 | stream.Printf("found new combo"); |
| 1183 | return true; |
| 1184 | } |
| 1185 | else |
| 1186 | location += ptr_size; |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1187 | } |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1188 | else |
| 1189 | { |
| 1190 | location = process_sp->ReadPointerFromMemory(location, error); |
| 1191 | if (error.Fail()) |
| 1192 | return false; |
| 1193 | } |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1194 | ReadUTFBufferAndDumpToStreamOptions<UTF16> options; |
| 1195 | options.SetConversionFunction(ConvertUTF16toUTF8); |
| 1196 | options.SetLocation(location); |
| 1197 | options.SetProcessSP(process_sp); |
| 1198 | options.SetStream(&stream); |
| 1199 | options.SetPrefixToken('@'); |
| 1200 | options.SetQuote('"'); |
| 1201 | options.SetSourceSize(explicit_length); |
| 1202 | options.SetNeedsZeroTermination(has_explicit_length == false); |
| 1203 | return ReadUTFBufferAndDumpToStream (options); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1204 | } |
| 1205 | else if (is_special) |
| 1206 | { |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 1207 | ProcessStructReader reader(valobj.GetProcessSP().get(), valobj.GetValueAsUnsigned(0), GetNSPathStore2Type(*valobj.GetTargetSP())); |
| 1208 | explicit_length = reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20; |
| 1209 | lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4; |
| 1210 | |
Enrico Granata | 5676839 | 2013-04-23 20:05:05 +0000 | [diff] [blame] | 1211 | ReadUTFBufferAndDumpToStreamOptions<UTF16> options; |
| 1212 | options.SetConversionFunction(ConvertUTF16toUTF8); |
| 1213 | options.SetLocation(location); |
| 1214 | options.SetProcessSP(process_sp); |
| 1215 | options.SetStream(&stream); |
| 1216 | options.SetPrefixToken('@'); |
| 1217 | options.SetQuote('"'); |
| 1218 | options.SetSourceSize(explicit_length); |
| 1219 | options.SetNeedsZeroTermination(has_explicit_length == false); |
| 1220 | return ReadUTFBufferAndDumpToStream (options); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1221 | } |
| 1222 | else if (is_inline) |
| 1223 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1224 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1225 | if (!has_explicit_length) |
| 1226 | location++; |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1227 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1228 | } |
| 1229 | else |
| 1230 | { |
Enrico Granata | 87f00b4 | 2013-02-21 20:31:18 +0000 | [diff] [blame] | 1231 | uint64_t location = valobj_addr + 2*ptr_size; |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1232 | location = process_sp->ReadPointerFromMemory(location, error); |
| 1233 | if (error.Fail()) |
| 1234 | return false; |
Enrico Granata | c71f349 | 2013-06-19 19:15:29 +0000 | [diff] [blame] | 1235 | if (has_explicit_length && !has_null) |
| 1236 | explicit_length++; // account for the fact that there is no NULL and we need to have one added |
Enrico Granata | d83bfce | 2013-04-02 21:25:34 +0000 | [diff] [blame] | 1237 | return ReadAsciiBufferAndDumpToStream(location,process_sp,stream,explicit_length); |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1238 | } |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Enrico Granata | f175ad1 | 2012-10-03 23:53:45 +0000 | [diff] [blame] | 1241 | bool |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1242 | lldb_private::formatters::NSAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1243 | { |
| 1244 | TargetSP target_sp(valobj.GetTargetSP()); |
| 1245 | if (!target_sp) |
| 1246 | return false; |
| 1247 | uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1248 | uint64_t pointer_value = valobj.GetValueAsUnsigned(0); |
| 1249 | if (!pointer_value) |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1250 | return false; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1251 | pointer_value += addr_size; |
| 1252 | ClangASTType type(valobj.GetClangType()); |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1253 | ExecutionContext exe_ctx(target_sp,false); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1254 | 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] | 1255 | if (!child_ptr_sp) |
| 1256 | return false; |
| 1257 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 1258 | Error error; |
| 1259 | child_ptr_sp->GetData(data, error); |
| 1260 | if (error.Fail()) |
| 1261 | return false; |
Enrico Granata | 5bfce36 | 2013-02-08 01:55:46 +0000 | [diff] [blame] | 1262 | ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData("string_data", data, exe_ctx, type)); |
| 1263 | child_sp->GetValueAsUnsigned(0); |
| 1264 | if (child_sp) |
| 1265 | return NSStringSummaryProvider(*child_sp, stream); |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | bool |
| 1270 | lldb_private::formatters::NSMutableAttributedStringSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1271 | { |
| 1272 | return NSAttributedStringSummaryProvider(valobj, stream); |
| 1273 | } |
| 1274 | |
| 1275 | bool |
Enrico Granata | f175ad1 | 2012-10-03 23:53:45 +0000 | [diff] [blame] | 1276 | lldb_private::formatters::RuntimeSpecificDescriptionSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1277 | { |
| 1278 | stream.Printf("%s",valobj.GetObjectDescription()); |
| 1279 | return true; |
| 1280 | } |
| 1281 | |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1282 | bool |
| 1283 | lldb_private::formatters::ObjCBOOLSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1284 | { |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1285 | const uint32_t type_info = valobj.GetClangType().GetTypeInfo(); |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1286 | |
| 1287 | ValueObjectSP real_guy_sp = valobj.GetSP(); |
| 1288 | |
Enrico Granata | 622be23 | 2014-10-21 20:52:14 +0000 | [diff] [blame] | 1289 | if (type_info & eTypeIsPointer) |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1290 | { |
| 1291 | Error err; |
| 1292 | real_guy_sp = valobj.Dereference(err); |
| 1293 | if (err.Fail() || !real_guy_sp) |
| 1294 | return false; |
| 1295 | } |
Enrico Granata | 622be23 | 2014-10-21 20:52:14 +0000 | [diff] [blame] | 1296 | else if (type_info & eTypeIsReference) |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1297 | { |
| 1298 | real_guy_sp = valobj.GetChildAtIndex(0, true); |
| 1299 | if (!real_guy_sp) |
| 1300 | return false; |
| 1301 | } |
| 1302 | uint64_t value = real_guy_sp->GetValueAsUnsigned(0); |
| 1303 | if (value == 0) |
| 1304 | { |
| 1305 | stream.Printf("NO"); |
| 1306 | return true; |
| 1307 | } |
| 1308 | stream.Printf("YES"); |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
| 1312 | template <bool is_sel_ptr> |
| 1313 | bool |
| 1314 | lldb_private::formatters::ObjCSELSummaryProvider (ValueObject& valobj, Stream& stream) |
| 1315 | { |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1316 | lldb::ValueObjectSP valobj_sp; |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1317 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1318 | ClangASTType charstar (valobj.GetClangType().GetBasicTypeFromAST(eBasicTypeChar).GetPointerType()); |
| 1319 | |
| 1320 | if (!charstar) |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1321 | return false; |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1322 | |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1323 | ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); |
| 1324 | |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1325 | if (is_sel_ptr) |
| 1326 | { |
| 1327 | lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 1328 | if (data_address == LLDB_INVALID_ADDRESS) |
| 1329 | return false; |
| 1330 | valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address, exe_ctx, charstar); |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | DataExtractor data; |
Sean Callanan | 866e91c | 2014-02-28 22:27:53 +0000 | [diff] [blame] | 1335 | Error error; |
| 1336 | valobj.GetData(data, error); |
| 1337 | if (error.Fail()) |
| 1338 | return false; |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1339 | valobj_sp = ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar); |
| 1340 | } |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1341 | |
Enrico Granata | 75dfb43 | 2013-02-15 00:06:04 +0000 | [diff] [blame] | 1342 | if (!valobj_sp) |
| 1343 | return false; |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1344 | |
| 1345 | stream.Printf("%s",valobj_sp->GetSummaryAsCString()); |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1349 | // POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001 |
| 1350 | // this call gives the POSIX equivalent of the Cocoa epoch |
| 1351 | time_t |
Enrico Granata | 9237353 | 2013-03-19 22:58:48 +0000 | [diff] [blame] | 1352 | lldb_private::formatters::GetOSXEpoch () |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1353 | { |
| 1354 | static time_t epoch = 0; |
| 1355 | if (!epoch) |
| 1356 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1357 | #ifndef _WIN32 |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1358 | tzset(); |
| 1359 | tm tm_epoch; |
| 1360 | tm_epoch.tm_sec = 0; |
| 1361 | tm_epoch.tm_hour = 0; |
| 1362 | tm_epoch.tm_min = 0; |
| 1363 | tm_epoch.tm_mon = 0; |
| 1364 | tm_epoch.tm_mday = 1; |
| 1365 | tm_epoch.tm_year = 2001-1900; // for some reason, we need to subtract 1900 from this field. not sure why. |
| 1366 | tm_epoch.tm_isdst = -1; |
| 1367 | tm_epoch.tm_gmtoff = 0; |
| 1368 | tm_epoch.tm_zone = NULL; |
| 1369 | epoch = timegm(&tm_epoch); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1370 | #endif |
Enrico Granata | 6d37cc6 | 2013-03-19 00:27:22 +0000 | [diff] [blame] | 1371 | } |
| 1372 | return epoch; |
| 1373 | } |
| 1374 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1375 | size_t |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1376 | lldb_private::formatters::ExtractIndexFromString (const char* item_name) |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1377 | { |
| 1378 | if (!item_name || !*item_name) |
| 1379 | return UINT32_MAX; |
| 1380 | if (*item_name != '[') |
| 1381 | return UINT32_MAX; |
| 1382 | item_name++; |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1383 | char* endptr = NULL; |
| 1384 | unsigned long int idx = ::strtoul(item_name, &endptr, 0); |
| 1385 | if (idx == 0 && endptr == item_name) |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1386 | return UINT32_MAX; |
Enrico Granata | f615b80 | 2013-02-15 23:38:37 +0000 | [diff] [blame] | 1387 | if (idx == ULONG_MAX) |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1388 | return UINT32_MAX; |
| 1389 | return idx; |
| 1390 | } |
| 1391 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1392 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::VectorIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp, |
| 1393 | ConstString item_name) : |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1394 | SyntheticChildrenFrontEnd(*valobj_sp.get()), |
| 1395 | m_exe_ctx_ref(), |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1396 | m_item_name(item_name), |
| 1397 | m_item_sp() |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1398 | { |
| 1399 | if (valobj_sp) |
| 1400 | Update(); |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1403 | bool |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1404 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::Update() |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1405 | { |
Enrico Granata | 6eca355 | 2013-03-28 18:50:54 +0000 | [diff] [blame] | 1406 | m_item_sp.reset(); |
| 1407 | |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1408 | ValueObjectSP valobj_sp = m_backend.GetSP(); |
| 1409 | if (!valobj_sp) |
| 1410 | return false; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1411 | |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1412 | if (!valobj_sp) |
| 1413 | return false; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1414 | |
| 1415 | ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name,true)); |
| 1416 | if (!item_ptr) |
| 1417 | return false; |
| 1418 | if (item_ptr->GetValueAsUnsigned(0) == 0) |
| 1419 | return false; |
| 1420 | Error err; |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1421 | m_exe_ctx_ref = valobj_sp->GetExecutionContextRef(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1422 | 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] | 1423 | if (err.Fail()) |
| 1424 | m_item_sp.reset(); |
Enrico Granata | 6eca355 | 2013-03-28 18:50:54 +0000 | [diff] [blame] | 1425 | return false; |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1426 | } |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1427 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1428 | size_t |
| 1429 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::CalculateNumChildren () |
| 1430 | { |
| 1431 | return 1; |
| 1432 | } |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1433 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1434 | lldb::ValueObjectSP |
| 1435 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx) |
| 1436 | { |
| 1437 | if (idx == 0) |
| 1438 | return m_item_sp; |
| 1439 | return lldb::ValueObjectSP(); |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | bool |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1443 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::MightHaveChildren () |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1444 | { |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
| 1448 | size_t |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1449 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name) |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1450 | { |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1451 | if (name == ConstString("item")) |
| 1452 | return 0; |
| 1453 | return UINT32_MAX; |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Enrico Granata | ea2bc0f | 2013-02-21 19:57:10 +0000 | [diff] [blame] | 1456 | lldb_private::formatters::VectorIteratorSyntheticFrontEnd::~VectorIteratorSyntheticFrontEnd () |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1457 | { |
Enrico Granata | 3b1b2da | 2013-02-04 22:54:42 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Enrico Granata | f519628 | 2012-09-04 18:48:21 +0000 | [diff] [blame] | 1460 | template bool |
Enrico Granata | b2698cd | 2012-09-13 18:27:09 +0000 | [diff] [blame] | 1461 | lldb_private::formatters::NSDataSummaryProvider<true> (ValueObject&, Stream&) ; |
| 1462 | |
| 1463 | template bool |
| 1464 | lldb_private::formatters::NSDataSummaryProvider<false> (ValueObject&, Stream&) ; |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 1465 | |
| 1466 | template bool |
| 1467 | lldb_private::formatters::ObjCSELSummaryProvider<true> (ValueObject&, Stream&) ; |
| 1468 | |
| 1469 | template bool |
| 1470 | lldb_private::formatters::ObjCSELSummaryProvider<false> (ValueObject&, Stream&) ; |