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