Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ValueObject.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 | |
| 10 | #include "lldb/Core/ValueObject.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | |
| 17 | // Project includes |
| 18 | #include "lldb/Core/DataBufferHeap.h" |
| 19 | #include "lldb/Core/StreamString.h" |
| 20 | #include "lldb/Core/ValueObjectChild.h" |
| 21 | #include "lldb/Core/ValueObjectList.h" |
| 22 | |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ClangASTType.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/ClangASTContext.h" |
| 25 | #include "lldb/Symbol/Type.h" |
| 26 | |
Jim Ingham | 53c47f1 | 2010-09-10 23:12:17 +0000 | [diff] [blame] | 27 | #include "lldb/Target/ExecutionContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Process.h" |
| 29 | #include "lldb/Target/RegisterContext.h" |
| 30 | #include "lldb/Target/Thread.h" |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
| 36 | static lldb::user_id_t g_value_obj_uid = 0; |
| 37 | |
| 38 | //---------------------------------------------------------------------- |
| 39 | // ValueObject constructor |
| 40 | //---------------------------------------------------------------------- |
| 41 | ValueObject::ValueObject () : |
| 42 | UserID (++g_value_obj_uid), // Unique identifier for every value object |
| 43 | m_update_id (0), // Value object lists always start at 1, value objects start at zero |
| 44 | m_name (), |
| 45 | m_data (), |
| 46 | m_value (), |
| 47 | m_error (), |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 48 | m_value_str (), |
| 49 | m_old_value_str (), |
| 50 | m_location_str (), |
| 51 | m_summary_str (), |
Jim Ingham | 53c47f1 | 2010-09-10 23:12:17 +0000 | [diff] [blame] | 52 | m_object_desc_str (), |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 53 | m_children (), |
| 54 | m_synthetic_children (), |
| 55 | m_value_is_valid (false), |
| 56 | m_value_did_change (false), |
| 57 | m_children_count_valid (false), |
| 58 | m_old_value_valid (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | //---------------------------------------------------------------------- |
| 63 | // Destructor |
| 64 | //---------------------------------------------------------------------- |
| 65 | ValueObject::~ValueObject () |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | user_id_t |
| 70 | ValueObject::GetUpdateID() const |
| 71 | { |
| 72 | return m_update_id; |
| 73 | } |
| 74 | |
| 75 | bool |
| 76 | ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope) |
| 77 | { |
| 78 | if (exe_scope) |
| 79 | { |
| 80 | Process *process = exe_scope->CalculateProcess(); |
| 81 | if (process) |
| 82 | { |
| 83 | const user_id_t stop_id = process->GetStopID(); |
| 84 | if (m_update_id != stop_id) |
| 85 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 86 | bool first_update = m_update_id == 0; |
Greg Clayton | 73b953b | 2010-08-28 00:08:07 +0000 | [diff] [blame] | 87 | // Save the old value using swap to avoid a string copy which |
| 88 | // also will clear our m_value_str |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 89 | if (m_value_str.empty()) |
| 90 | { |
| 91 | m_old_value_valid = false; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | m_old_value_valid = true; |
| 96 | m_old_value_str.swap (m_value_str); |
| 97 | m_value_str.clear(); |
| 98 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | m_location_str.clear(); |
| 100 | m_summary_str.clear(); |
Jim Ingham | 53c47f1 | 2010-09-10 23:12:17 +0000 | [diff] [blame] | 101 | m_object_desc_str.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 73b953b | 2010-08-28 00:08:07 +0000 | [diff] [blame] | 103 | const bool value_was_valid = GetValueIsValid(); |
| 104 | SetValueDidChange (false); |
| 105 | |
| 106 | m_error.Clear(); |
| 107 | |
| 108 | // Call the pure virtual function to update the value |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | UpdateValue (exe_scope); |
Greg Clayton | 73b953b | 2010-08-28 00:08:07 +0000 | [diff] [blame] | 110 | |
| 111 | // Update the fact that we tried to update the value for this |
| 112 | // value object wether or not we succeed |
| 113 | m_update_id = stop_id; |
| 114 | bool success = m_error.Success(); |
| 115 | SetValueIsValid (success); |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 116 | |
| 117 | if (first_update) |
| 118 | SetValueDidChange (false); |
| 119 | else if (!m_value_did_change && success == false) |
Greg Clayton | 73b953b | 2010-08-28 00:08:07 +0000 | [diff] [blame] | 120 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 121 | // The value wasn't gotten successfully, so we mark this |
| 122 | // as changed if the value used to be valid and now isn't |
| 123 | SetValueDidChange (value_was_valid); |
Greg Clayton | 73b953b | 2010-08-28 00:08:07 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | return m_error.Success(); |
| 129 | } |
| 130 | |
| 131 | const DataExtractor & |
| 132 | ValueObject::GetDataExtractor () const |
| 133 | { |
| 134 | return m_data; |
| 135 | } |
| 136 | |
| 137 | DataExtractor & |
| 138 | ValueObject::GetDataExtractor () |
| 139 | { |
| 140 | return m_data; |
| 141 | } |
| 142 | |
| 143 | const Error & |
| 144 | ValueObject::GetError() const |
| 145 | { |
| 146 | return m_error; |
| 147 | } |
| 148 | |
| 149 | const ConstString & |
| 150 | ValueObject::GetName() const |
| 151 | { |
| 152 | return m_name; |
| 153 | } |
| 154 | |
| 155 | const char * |
| 156 | ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope) |
| 157 | { |
| 158 | if (UpdateValueIfNeeded(exe_scope)) |
| 159 | { |
| 160 | if (m_location_str.empty()) |
| 161 | { |
| 162 | StreamString sstr; |
| 163 | |
| 164 | switch (m_value.GetValueType()) |
| 165 | { |
| 166 | default: |
| 167 | break; |
| 168 | |
| 169 | case Value::eValueTypeScalar: |
| 170 | if (m_value.GetContextType() == Value::eContextTypeDCRegisterInfo) |
| 171 | { |
| 172 | RegisterInfo *reg_info = m_value.GetRegisterInfo(); |
| 173 | if (reg_info) |
| 174 | { |
| 175 | if (reg_info->name) |
| 176 | m_location_str = reg_info->name; |
| 177 | else if (reg_info->alt_name) |
| 178 | m_location_str = reg_info->alt_name; |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | m_location_str = "scalar"; |
| 183 | break; |
| 184 | |
| 185 | case Value::eValueTypeLoadAddress: |
| 186 | case Value::eValueTypeFileAddress: |
| 187 | case Value::eValueTypeHostAddress: |
| 188 | { |
| 189 | uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2; |
| 190 | sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS)); |
| 191 | m_location_str.swap(sstr.GetString()); |
| 192 | } |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return m_location_str.c_str(); |
| 198 | } |
| 199 | |
| 200 | Value & |
| 201 | ValueObject::GetValue() |
| 202 | { |
| 203 | return m_value; |
| 204 | } |
| 205 | |
| 206 | const Value & |
| 207 | ValueObject::GetValue() const |
| 208 | { |
| 209 | return m_value; |
| 210 | } |
| 211 | |
| 212 | bool |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 213 | ValueObject::GetValueIsValid () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 215 | return m_value_is_valid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | void |
| 220 | ValueObject::SetValueIsValid (bool b) |
| 221 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 222 | m_value_is_valid = b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 226 | ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 228 | GetValueAsCString (exe_scope); |
| 229 | return m_value_did_change; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void |
| 233 | ValueObject::SetValueDidChange (bool value_changed) |
| 234 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 235 | m_value_did_change = value_changed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | ValueObjectSP |
| 239 | ValueObject::GetChildAtIndex (uint32_t idx, bool can_create) |
| 240 | { |
| 241 | ValueObjectSP child_sp; |
| 242 | if (idx < GetNumChildren()) |
| 243 | { |
| 244 | // Check if we have already made the child value object? |
| 245 | if (can_create && m_children[idx].get() == NULL) |
| 246 | { |
| 247 | // No we haven't created the child at this index, so lets have our |
| 248 | // subclass do it and cache the result for quick future access. |
| 249 | m_children[idx] = CreateChildAtIndex (idx, false, 0); |
| 250 | } |
| 251 | |
| 252 | child_sp = m_children[idx]; |
| 253 | } |
| 254 | return child_sp; |
| 255 | } |
| 256 | |
| 257 | uint32_t |
| 258 | ValueObject::GetIndexOfChildWithName (const ConstString &name) |
| 259 | { |
| 260 | bool omit_empty_base_classes = true; |
| 261 | return ClangASTContext::GetIndexOfChildWithName (GetClangAST(), |
| 262 | GetOpaqueClangQualType(), |
| 263 | name.AsCString(), |
| 264 | omit_empty_base_classes); |
| 265 | } |
| 266 | |
| 267 | ValueObjectSP |
| 268 | ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create) |
| 269 | { |
| 270 | // when getting a child by name, it could be burried inside some base |
| 271 | // classes (which really aren't part of the expression path), so we |
| 272 | // need a vector of indexes that can get us down to the correct child |
| 273 | std::vector<uint32_t> child_indexes; |
| 274 | clang::ASTContext *clang_ast = GetClangAST(); |
| 275 | void *clang_type = GetOpaqueClangQualType(); |
| 276 | bool omit_empty_base_classes = true; |
| 277 | const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast, |
| 278 | clang_type, |
| 279 | name.AsCString(), |
| 280 | omit_empty_base_classes, |
| 281 | child_indexes); |
| 282 | ValueObjectSP child_sp; |
| 283 | if (num_child_indexes > 0) |
| 284 | { |
| 285 | std::vector<uint32_t>::const_iterator pos = child_indexes.begin (); |
| 286 | std::vector<uint32_t>::const_iterator end = child_indexes.end (); |
| 287 | |
| 288 | child_sp = GetChildAtIndex(*pos, can_create); |
| 289 | for (++pos; pos != end; ++pos) |
| 290 | { |
| 291 | if (child_sp) |
| 292 | { |
| 293 | ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create)); |
| 294 | child_sp = new_child_sp; |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | child_sp.reset(); |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | } |
| 303 | return child_sp; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | uint32_t |
| 308 | ValueObject::GetNumChildren () |
| 309 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 310 | if (!m_children_count_valid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | { |
| 312 | SetNumChildren (CalculateNumChildren()); |
| 313 | } |
| 314 | return m_children.size(); |
| 315 | } |
| 316 | void |
| 317 | ValueObject::SetNumChildren (uint32_t num_children) |
| 318 | { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 319 | m_children_count_valid = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 320 | m_children.resize(num_children); |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | ValueObject::SetName (const char *name) |
| 325 | { |
| 326 | m_name.SetCString(name); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | ValueObject::SetName (const ConstString &name) |
| 331 | { |
| 332 | m_name = name; |
| 333 | } |
| 334 | |
| 335 | ValueObjectSP |
| 336 | ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index) |
| 337 | { |
| 338 | ValueObjectSP valobj_sp; |
| 339 | bool omit_empty_base_classes = true; |
| 340 | |
| 341 | std::string child_name_str; |
| 342 | uint32_t child_byte_size = 0; |
| 343 | int32_t child_byte_offset = 0; |
| 344 | uint32_t child_bitfield_bit_size = 0; |
| 345 | uint32_t child_bitfield_bit_offset = 0; |
| 346 | const bool transparent_pointers = synthetic_array_member == false; |
| 347 | clang::ASTContext *clang_ast = GetClangAST(); |
| 348 | void *clang_type = GetOpaqueClangQualType(); |
| 349 | void *child_clang_type; |
| 350 | child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast, |
| 351 | GetName().AsCString(), |
| 352 | clang_type, |
| 353 | idx, |
| 354 | transparent_pointers, |
| 355 | omit_empty_base_classes, |
| 356 | child_name_str, |
| 357 | child_byte_size, |
| 358 | child_byte_offset, |
| 359 | child_bitfield_bit_size, |
| 360 | child_bitfield_bit_offset); |
| 361 | if (child_clang_type) |
| 362 | { |
| 363 | if (synthetic_index) |
| 364 | child_byte_offset += child_byte_size * synthetic_index; |
| 365 | |
| 366 | ConstString child_name; |
| 367 | if (!child_name_str.empty()) |
| 368 | child_name.SetCString (child_name_str.c_str()); |
| 369 | |
| 370 | valobj_sp.reset (new ValueObjectChild (this, |
| 371 | clang_ast, |
| 372 | child_clang_type, |
| 373 | child_name, |
| 374 | child_byte_size, |
| 375 | child_byte_offset, |
| 376 | child_bitfield_bit_size, |
| 377 | child_bitfield_bit_offset)); |
| 378 | } |
| 379 | return valobj_sp; |
| 380 | } |
| 381 | |
| 382 | const char * |
| 383 | ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope) |
| 384 | { |
| 385 | if (UpdateValueIfNeeded (exe_scope)) |
| 386 | { |
| 387 | if (m_summary_str.empty()) |
| 388 | { |
| 389 | void *clang_type = GetOpaqueClangQualType(); |
| 390 | |
| 391 | // See if this is a pointer to a C string? |
| 392 | uint32_t fixed_length = 0; |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 393 | if (clang_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | { |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 395 | StreamString sstr; |
| 396 | |
| 397 | if (ClangASTContext::IsCStringType (clang_type, fixed_length)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | { |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 399 | Process *process = exe_scope->CalculateProcess(); |
| 400 | if (process != NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | { |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 402 | lldb::AddressType cstr_address_type = eAddressTypeInvalid; |
| 403 | lldb::addr_t cstr_address = GetPointerValue (cstr_address_type, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 404 | |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 405 | if (cstr_address != LLDB_INVALID_ADDRESS) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 406 | { |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 407 | DataExtractor data; |
| 408 | size_t bytes_read = 0; |
| 409 | std::vector<char> data_buffer; |
| 410 | std::vector<char> cstr_buffer; |
| 411 | size_t cstr_length; |
| 412 | Error error; |
| 413 | if (fixed_length > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 414 | { |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 415 | data_buffer.resize(fixed_length); |
| 416 | // Resize the formatted buffer in case every character |
| 417 | // uses the "\xXX" format and one extra byte for a NULL |
| 418 | cstr_buffer.resize(data_buffer.size() * 4 + 1); |
| 419 | data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost); |
| 420 | bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), fixed_length, error); |
| 421 | if (bytes_read > 0) |
| 422 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 423 | sstr << '"'; |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 424 | cstr_length = data.Dump (&sstr, |
| 425 | 0, // Start offset in "data" |
| 426 | eFormatChar, // Print as characters |
| 427 | 1, // Size of item (1 byte for a char!) |
| 428 | bytes_read, // How many bytes to print? |
| 429 | UINT32_MAX, // num per line |
| 430 | LLDB_INVALID_ADDRESS,// base address |
| 431 | 0, // bitfield bit size |
| 432 | 0); // bitfield bit offset |
| 433 | sstr << '"'; |
| 434 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | } |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 436 | else |
| 437 | { |
| 438 | const size_t k_max_buf_size = 256; |
| 439 | data_buffer.resize (k_max_buf_size + 1); |
| 440 | // NULL terminate in case we don't get the entire C string |
| 441 | data_buffer.back() = '\0'; |
| 442 | // Make a formatted buffer that can contain take 4 |
| 443 | // bytes per character in case each byte uses the |
| 444 | // "\xXX" format and one extra byte for a NULL |
| 445 | cstr_buffer.resize (k_max_buf_size * 4 + 1); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 446 | |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 447 | data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost); |
| 448 | size_t total_cstr_len = 0; |
| 449 | while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0) |
| 450 | { |
| 451 | size_t len = strlen(&data_buffer.front()); |
| 452 | if (len == 0) |
| 453 | break; |
| 454 | if (len > bytes_read) |
| 455 | len = bytes_read; |
| 456 | if (sstr.GetSize() == 0) |
| 457 | sstr << '"'; |
| 458 | |
| 459 | cstr_length = data.Dump (&sstr, |
| 460 | 0, // Start offset in "data" |
| 461 | eFormatChar, // Print as characters |
| 462 | 1, // Size of item (1 byte for a char!) |
| 463 | len, // How many bytes to print? |
| 464 | UINT32_MAX, // num per line |
| 465 | LLDB_INVALID_ADDRESS,// base address |
| 466 | 0, // bitfield bit size |
| 467 | 0); // bitfield bit offset |
| 468 | |
| 469 | if (len < k_max_buf_size) |
| 470 | break; |
| 471 | cstr_address += total_cstr_len; |
| 472 | } |
| 473 | if (sstr.GetSize() > 0) |
| 474 | sstr << '"'; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (sstr.GetSize() > 0) |
| 480 | m_summary_str.assign (sstr.GetData(), sstr.GetSize()); |
| 481 | } |
| 482 | else if (ClangASTContext::IsFunctionPointerType (clang_type)) |
| 483 | { |
| 484 | lldb::AddressType func_ptr_address_type = eAddressTypeInvalid; |
| 485 | lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true); |
| 486 | |
| 487 | if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS) |
| 488 | { |
| 489 | switch (func_ptr_address_type) |
| 490 | { |
| 491 | case eAddressTypeInvalid: |
| 492 | case eAddressTypeFile: |
| 493 | break; |
| 494 | |
| 495 | case eAddressTypeLoad: |
| 496 | { |
| 497 | Address so_addr; |
| 498 | Process *process = exe_scope->CalculateProcess(); |
| 499 | if (process != NULL) |
| 500 | { |
| 501 | if (process->ResolveLoadAddress(func_ptr_address, so_addr)) |
| 502 | { |
| 503 | so_addr.Dump (&sstr, |
| 504 | exe_scope, |
| 505 | Address::DumpStyleResolvedDescription, |
| 506 | Address::DumpStyleSectionNameOffset); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | break; |
| 511 | |
| 512 | case eAddressTypeHost: |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | if (sstr.GetSize() > 0) |
| 517 | { |
| 518 | m_summary_str.assign (1, '('); |
| 519 | m_summary_str.append (sstr.GetData(), sstr.GetSize()); |
| 520 | m_summary_str.append (1, ')'); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | if (m_summary_str.empty()) |
| 527 | return NULL; |
| 528 | return m_summary_str.c_str(); |
| 529 | } |
| 530 | |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 531 | |
Jim Ingham | 53c47f1 | 2010-09-10 23:12:17 +0000 | [diff] [blame] | 532 | const char * |
| 533 | ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope) |
| 534 | { |
| 535 | if (!m_object_desc_str.empty()) |
| 536 | return m_object_desc_str.c_str(); |
| 537 | |
| 538 | if (!ClangASTContext::IsPointerType (GetOpaqueClangQualType())) |
| 539 | return NULL; |
| 540 | |
| 541 | if (!GetValueIsValid()) |
| 542 | return NULL; |
| 543 | |
| 544 | Process *process = exe_scope->CalculateProcess(); |
| 545 | |
| 546 | if (!process) |
| 547 | return NULL; |
| 548 | |
| 549 | Scalar scalar; |
| 550 | |
| 551 | if (!ClangASTType::GetValueAsScalar (GetClangAST(), |
| 552 | GetOpaqueClangQualType(), |
| 553 | GetDataExtractor(), |
| 554 | 0, |
| 555 | GetByteSize(), |
| 556 | scalar)) |
| 557 | return NULL; |
| 558 | |
| 559 | ExecutionContext exe_ctx; |
| 560 | exe_scope->Calculate(exe_ctx); |
| 561 | |
| 562 | Value val(scalar); |
| 563 | val.SetContext(Value::eContextTypeOpaqueClangQualType, |
| 564 | ClangASTContext::GetVoidPtrType(GetClangAST(), false)); |
| 565 | |
| 566 | StreamString s; |
| 567 | // FIXME: Check the runtime this object belongs to and get the appropriate object printer for the object kind. |
| 568 | if (process->GetObjCObjectPrinter().PrintObject(s, val, exe_ctx)) |
| 569 | { |
| 570 | m_object_desc_str.append (s.GetData()); |
| 571 | } |
| 572 | return m_object_desc_str.c_str(); |
| 573 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 574 | |
| 575 | const char * |
| 576 | ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope) |
| 577 | { |
| 578 | // If our byte size is zero this is an aggregate type that has children |
| 579 | if (ClangASTContext::IsAggregateType (GetOpaqueClangQualType()) == false) |
| 580 | { |
| 581 | if (UpdateValueIfNeeded(exe_scope)) |
| 582 | { |
| 583 | if (m_value_str.empty()) |
| 584 | { |
| 585 | const Value::ContextType context_type = m_value.GetContextType(); |
| 586 | |
| 587 | switch (context_type) |
| 588 | { |
| 589 | case Value::eContextTypeOpaqueClangQualType: |
| 590 | case Value::eContextTypeDCType: |
| 591 | case Value::eContextTypeDCVariable: |
| 592 | { |
| 593 | void *clang_type = GetOpaqueClangQualType (); |
| 594 | if (clang_type) |
| 595 | { |
| 596 | StreamString sstr; |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 597 | lldb::Format format = ClangASTType::GetFormat(clang_type); |
| 598 | if (ClangASTType::DumpTypeValue(GetClangAST(), // The clang AST |
| 599 | clang_type, // The clang type to display |
| 600 | &sstr, |
| 601 | format, // Format to display this type with |
| 602 | m_data, // Data to extract from |
| 603 | 0, // Byte offset into "m_data" |
| 604 | GetByteSize(), // Byte size of item in "m_data" |
| 605 | GetBitfieldBitSize(), // Bitfield bit size |
| 606 | GetBitfieldBitOffset())) // Bitfield bit offset |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 607 | m_value_str.swap(sstr.GetString()); |
| 608 | else |
| 609 | m_value_str.clear(); |
| 610 | } |
| 611 | } |
| 612 | break; |
| 613 | |
| 614 | case Value::eContextTypeDCRegisterInfo: |
| 615 | { |
| 616 | const RegisterInfo *reg_info = m_value.GetRegisterInfo(); |
| 617 | if (reg_info) |
| 618 | { |
| 619 | StreamString reg_sstr; |
| 620 | m_data.Dump(®_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0); |
| 621 | m_value_str.swap(reg_sstr.GetString()); |
| 622 | } |
| 623 | } |
| 624 | break; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 625 | |
| 626 | default: |
| 627 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 630 | |
| 631 | if (!m_value_did_change && m_old_value_valid) |
| 632 | { |
| 633 | // The value was gotten successfully, so we consider the |
| 634 | // value as changed if the value string differs |
| 635 | SetValueDidChange (m_old_value_str != m_value_str); |
| 636 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | if (m_value_str.empty()) |
| 640 | return NULL; |
| 641 | return m_value_str.c_str(); |
| 642 | } |
| 643 | |
Greg Clayton | 737b932 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 644 | addr_t |
| 645 | ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address) |
| 646 | { |
| 647 | lldb::addr_t address = LLDB_INVALID_ADDRESS; |
| 648 | address_type = eAddressTypeInvalid; |
| 649 | switch (GetValue().GetValueType()) |
| 650 | { |
| 651 | case Value::eValueTypeScalar: |
| 652 | if (scalar_is_load_address) |
| 653 | { |
| 654 | address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 655 | address_type = eAddressTypeLoad; |
| 656 | } |
| 657 | break; |
| 658 | |
| 659 | case Value::eValueTypeLoadAddress: |
| 660 | case Value::eValueTypeFileAddress: |
| 661 | case Value::eValueTypeHostAddress: |
| 662 | { |
| 663 | uint32_t data_offset = 0; |
| 664 | address = m_data.GetPointer(&data_offset); |
| 665 | address_type = m_value.GetValueAddressType(); |
| 666 | if (address_type == eAddressTypeInvalid) |
| 667 | address_type = eAddressTypeLoad; |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | return address; |
| 672 | } |
| 673 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 674 | bool |
| 675 | ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str) |
| 676 | { |
| 677 | // Make sure our value is up to date first so that our location and location |
| 678 | // type is valid. |
| 679 | if (!UpdateValueIfNeeded(exe_scope)) |
| 680 | return false; |
| 681 | |
| 682 | uint32_t count = 0; |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 683 | lldb::Encoding encoding = ClangASTType::GetEncoding (GetOpaqueClangQualType(), count); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 684 | |
| 685 | char *end = NULL; |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 686 | const size_t byte_size = GetByteSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 687 | switch (encoding) |
| 688 | { |
| 689 | case eEncodingInvalid: |
| 690 | return false; |
| 691 | |
| 692 | case eEncodingUint: |
| 693 | if (byte_size > sizeof(unsigned long long)) |
| 694 | { |
| 695 | return false; |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | unsigned long long ull_val = strtoull(value_str, &end, 0); |
| 700 | if (end && *end != '\0') |
| 701 | return false; |
| 702 | m_value = ull_val; |
| 703 | // Limit the bytes in our m_data appropriately. |
| 704 | m_value.GetScalar().GetData (m_data, byte_size); |
| 705 | } |
| 706 | break; |
| 707 | |
| 708 | case eEncodingSint: |
| 709 | if (byte_size > sizeof(long long)) |
| 710 | { |
| 711 | return false; |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | long long sll_val = strtoll(value_str, &end, 0); |
| 716 | if (end && *end != '\0') |
| 717 | return false; |
| 718 | m_value = sll_val; |
| 719 | // Limit the bytes in our m_data appropriately. |
| 720 | m_value.GetScalar().GetData (m_data, byte_size); |
| 721 | } |
| 722 | break; |
| 723 | |
| 724 | case eEncodingIEEE754: |
| 725 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 726 | const off_t byte_offset = GetByteOffset(); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 727 | uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 728 | if (dst != NULL) |
| 729 | { |
| 730 | // We are decoding a float into host byte order below, so make |
| 731 | // sure m_data knows what it contains. |
| 732 | m_data.SetByteOrder(eByteOrderHost); |
| 733 | const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue ( |
| 734 | GetClangAST(), |
| 735 | GetOpaqueClangQualType(), |
| 736 | value_str, |
| 737 | dst, |
| 738 | byte_size); |
| 739 | |
| 740 | if (converted_byte_size == byte_size) |
| 741 | { |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | break; |
| 746 | |
| 747 | case eEncodingVector: |
| 748 | return false; |
| 749 | |
| 750 | default: |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | // If we have made it here the value is in m_data and we should write it |
| 755 | // out to the target |
| 756 | return Write (); |
| 757 | } |
| 758 | |
| 759 | bool |
| 760 | ValueObject::Write () |
| 761 | { |
| 762 | // Clear the update ID so the next time we try and read the value |
| 763 | // we try and read it again. |
| 764 | m_update_id = 0; |
| 765 | |
| 766 | // TODO: when Value has a method to write a value back, call it from here. |
| 767 | return false; |
| 768 | |
| 769 | } |
| 770 | |
| 771 | void |
| 772 | ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp) |
| 773 | { |
| 774 | m_synthetic_children[key] = valobj_sp; |
| 775 | } |
| 776 | |
| 777 | ValueObjectSP |
| 778 | ValueObject::GetSyntheticChild (const ConstString &key) const |
| 779 | { |
| 780 | ValueObjectSP synthetic_child_sp; |
| 781 | std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key); |
| 782 | if (pos != m_synthetic_children.end()) |
| 783 | synthetic_child_sp = pos->second; |
| 784 | return synthetic_child_sp; |
| 785 | } |
| 786 | |
| 787 | bool |
| 788 | ValueObject::IsPointerType () |
| 789 | { |
| 790 | return ClangASTContext::IsPointerType (GetOpaqueClangQualType()); |
| 791 | } |
| 792 | |
| 793 | bool |
| 794 | ValueObject::IsPointerOrReferenceType () |
| 795 | { |
| 796 | return ClangASTContext::IsPointerOrReferenceType(GetOpaqueClangQualType()); |
| 797 | } |
| 798 | |
| 799 | ValueObjectSP |
| 800 | ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create) |
| 801 | { |
| 802 | ValueObjectSP synthetic_child_sp; |
| 803 | if (IsPointerType ()) |
| 804 | { |
| 805 | char index_str[64]; |
| 806 | snprintf(index_str, sizeof(index_str), "[%i]", index); |
| 807 | ConstString index_const_str(index_str); |
| 808 | // Check if we have already created a synthetic array member in this |
| 809 | // valid object. If we have we will re-use it. |
| 810 | synthetic_child_sp = GetSyntheticChild (index_const_str); |
| 811 | if (!synthetic_child_sp) |
| 812 | { |
| 813 | // We haven't made a synthetic array member for INDEX yet, so |
| 814 | // lets make one and cache it for any future reference. |
| 815 | synthetic_child_sp = CreateChildAtIndex(0, true, index); |
| 816 | |
| 817 | // Cache the value if we got one back... |
| 818 | if (synthetic_child_sp) |
| 819 | AddSyntheticChild(index_const_str, synthetic_child_sp); |
| 820 | } |
| 821 | } |
| 822 | return synthetic_child_sp; |
| 823 | } |