Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBValue.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 | |
Eli Friedman | 7a62c8b | 2010-06-09 07:44:37 +0000 | [diff] [blame] | 10 | #include "lldb/API/SBValue.h" |
Caroline Tice | 98f930f | 2010-09-20 05:20:02 +0000 | [diff] [blame^] | 11 | #include "lldb/API/SBStream.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include "lldb/Core/DataExtractor.h" |
| 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/Stream.h" |
| 16 | #include "lldb/Core/StreamFile.h" |
| 17 | #include "lldb/Core/Value.h" |
| 18 | #include "lldb/Core/ValueObject.h" |
| 19 | #include "lldb/Symbol/Block.h" |
| 20 | #include "lldb/Symbol/ObjectFile.h" |
| 21 | #include "lldb/Symbol/Variable.h" |
| 22 | #include "lldb/Target/ExecutionContext.h" |
| 23 | #include "lldb/Target/Process.h" |
| 24 | #include "lldb/Target/StackFrame.h" |
| 25 | #include "lldb/Target/Thread.h" |
| 26 | |
Eli Friedman | 7a62c8b | 2010-06-09 07:44:37 +0000 | [diff] [blame] | 27 | #include "lldb/API/SBProcess.h" |
| 28 | #include "lldb/API/SBTarget.h" |
| 29 | #include "lldb/API/SBThread.h" |
| 30 | #include "lldb/API/SBFrame.h" |
| 31 | #include "lldb/API/SBDebugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
| 36 | SBValue::SBValue () : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 37 | m_opaque_sp () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | SBValue::SBValue (const lldb::ValueObjectSP &value_sp) : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 42 | m_opaque_sp (value_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | SBValue::~SBValue() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | SBValue::IsValid () const |
| 52 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 53 | return (m_opaque_sp.get() != NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | const char * |
| 57 | SBValue::GetName() |
| 58 | { |
| 59 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 60 | return m_opaque_sp->GetName().AsCString(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | else |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | const char * |
| 66 | SBValue::GetTypeName () |
| 67 | { |
| 68 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 69 | return m_opaque_sp->GetTypeName().AsCString(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | else |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | size_t |
| 75 | SBValue::GetByteSize () |
| 76 | { |
| 77 | size_t result = 0; |
| 78 | |
| 79 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 80 | result = m_opaque_sp->GetByteSize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | SBValue::IsInScope (const SBFrame &frame) |
| 87 | { |
| 88 | bool result = false; |
| 89 | |
| 90 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 91 | result = m_opaque_sp->IsInScope (frame.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | const char * |
| 97 | SBValue::GetValue (const SBFrame &frame) |
| 98 | { |
| 99 | const char *value_string = NULL; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 100 | if ( m_opaque_sp) |
Greg Clayton | 17dae08 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 101 | value_string = m_opaque_sp->GetValueAsCString (frame.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | return value_string; |
| 103 | } |
| 104 | |
Jim Ingham | 4ae5196 | 2010-09-10 23:12:17 +0000 | [diff] [blame] | 105 | const char * |
| 106 | SBValue::GetObjectDescription (const SBFrame &frame) |
| 107 | { |
| 108 | const char *value_string = NULL; |
| 109 | if ( m_opaque_sp) |
| 110 | value_string = m_opaque_sp->GetObjectDescription (frame.get()); |
| 111 | return value_string; |
| 112 | } |
| 113 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | bool |
Greg Clayton | 17dae08 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 115 | SBValue::GetValueDidChange (const SBFrame &frame) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | { |
| 117 | if (IsValid()) |
Greg Clayton | 17dae08 | 2010-09-02 02:59:18 +0000 | [diff] [blame] | 118 | return m_opaque_sp->GetValueDidChange (frame.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | const char * |
| 123 | SBValue::GetSummary (const SBFrame &frame) |
| 124 | { |
| 125 | const char *value_string = NULL; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 126 | if ( m_opaque_sp) |
| 127 | value_string = m_opaque_sp->GetSummaryAsCString(frame.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | return value_string; |
| 129 | } |
| 130 | |
| 131 | const char * |
| 132 | SBValue::GetLocation (const SBFrame &frame) |
| 133 | { |
| 134 | const char *value_string = NULL; |
| 135 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 136 | value_string = m_opaque_sp->GetLocationAsCString(frame.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | return value_string; |
| 138 | } |
| 139 | |
| 140 | bool |
| 141 | SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str) |
| 142 | { |
| 143 | bool success = false; |
| 144 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 145 | success = m_opaque_sp->SetValueFromCString (frame.get(), value_str); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | return success; |
| 147 | } |
| 148 | |
| 149 | SBValue |
| 150 | SBValue::GetChildAtIndex (uint32_t idx) |
| 151 | { |
| 152 | lldb::ValueObjectSP child_sp; |
| 153 | |
| 154 | if (IsValid()) |
| 155 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 156 | child_sp = m_opaque_sp->GetChildAtIndex (idx, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | SBValue sb_value (child_sp); |
| 160 | return sb_value; |
| 161 | } |
| 162 | |
| 163 | uint32_t |
| 164 | SBValue::GetIndexOfChildWithName (const char *name) |
| 165 | { |
| 166 | if (IsValid()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 167 | return m_opaque_sp->GetIndexOfChildWithName (ConstString(name)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | return UINT32_MAX; |
| 169 | } |
| 170 | |
| 171 | SBValue |
| 172 | SBValue::GetChildMemberWithName (const char *name) |
| 173 | { |
| 174 | lldb::ValueObjectSP child_sp; |
| 175 | const ConstString str_name (name); |
| 176 | |
| 177 | if (IsValid()) |
| 178 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 179 | child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | SBValue sb_value (child_sp); |
| 183 | return sb_value; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | uint32_t |
| 188 | SBValue::GetNumChildren () |
| 189 | { |
| 190 | uint32_t num_children = 0; |
| 191 | |
| 192 | if (IsValid()) |
| 193 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 194 | num_children = m_opaque_sp->GetNumChildren(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return num_children; |
| 198 | } |
| 199 | |
| 200 | bool |
| 201 | SBValue::ValueIsStale () |
| 202 | { |
| 203 | bool result = true; |
| 204 | |
| 205 | if (IsValid()) |
| 206 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 207 | result = m_opaque_sp->GetValueIsValid(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | SBValue |
| 215 | SBValue::Dereference () |
| 216 | { |
| 217 | if (IsValid()) |
| 218 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 219 | if (m_opaque_sp->IsPointerType()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | { |
| 221 | return GetChildAtIndex(0); |
| 222 | } |
| 223 | } |
| 224 | return *this; |
| 225 | } |
| 226 | |
| 227 | bool |
| 228 | SBValue::TypeIsPtrType () |
| 229 | { |
| 230 | bool is_ptr_type = false; |
| 231 | |
| 232 | if (IsValid()) |
| 233 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 234 | is_ptr_type = m_opaque_sp->IsPointerType(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return is_ptr_type; |
| 238 | } |
| 239 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | void * |
| 241 | SBValue::GetOpaqueType() |
| 242 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 243 | if (m_opaque_sp) |
| 244 | return m_opaque_sp->GetOpaqueClangQualType(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | // Mimic shared pointer... |
| 249 | lldb_private::ValueObject * |
| 250 | SBValue::get() const |
| 251 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 252 | return m_opaque_sp.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | lldb_private::ValueObject * |
| 256 | SBValue::operator->() const |
| 257 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 258 | return m_opaque_sp.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | lldb::ValueObjectSP & |
| 262 | SBValue::operator*() |
| 263 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 264 | return m_opaque_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | const lldb::ValueObjectSP & |
| 268 | SBValue::operator*() const |
| 269 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 270 | return m_opaque_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | } |
Caroline Tice | 98f930f | 2010-09-20 05:20:02 +0000 | [diff] [blame^] | 272 | |
| 273 | bool |
| 274 | SBValue::GetDescription (SBStream &description) |
| 275 | { |
| 276 | if (m_opaque_sp) |
| 277 | { |
| 278 | const char *name = GetName(); |
| 279 | const char *type_name = GetTypeName (); |
| 280 | size_t byte_size = GetByteSize (); |
| 281 | uint32_t num_children = GetNumChildren (); |
| 282 | bool is_stale = ValueIsStale (); |
| 283 | description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"), |
| 284 | (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size); |
| 285 | if (num_children > 0) |
| 286 | description.Printf (", num_children: %d", num_children); |
| 287 | |
| 288 | if (is_stale) |
| 289 | description.Printf (" [value is stale]"); |
| 290 | } |
| 291 | else |
| 292 | description.Printf ("No value"); |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | PyObject * |
| 298 | SBValue::__repr__ () |
| 299 | { |
| 300 | SBStream description; |
| 301 | GetDescription (description); |
| 302 | return PyString_FromString (description.GetData()); |
| 303 | } |