Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Variable.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/Symbol/Variable.h" |
| 11 | |
| 12 | #include "lldb/Core/Stream.h" |
| 13 | #include "lldb/Symbol/Block.h" |
| 14 | #include "lldb/Symbol/Function.h" |
| 15 | #include "lldb/Symbol/SymbolContext.h" |
| 16 | #include "lldb/Symbol/Type.h" |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame^] | 17 | #include "lldb/Target/Process.h" |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 18 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Target/StackFrame.h" |
| 20 | #include "lldb/Target/Thread.h" |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame^] | 21 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | //---------------------------------------------------------------------- |
| 27 | // Variable constructor |
| 28 | //---------------------------------------------------------------------- |
| 29 | Variable::Variable(lldb::user_id_t uid, |
| 30 | const ConstString& name, |
| 31 | Type *type, |
| 32 | ValueType scope, |
| 33 | SymbolContextScope *context, |
| 34 | Declaration* decl_ptr, |
| 35 | const DWARFExpression& location, |
| 36 | bool external, |
| 37 | bool artificial) : |
| 38 | UserID(uid), |
| 39 | m_name(name), |
| 40 | m_type(type), |
| 41 | m_scope(scope), |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 42 | m_owner_scope(context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | m_declaration(decl_ptr), |
| 44 | m_location(location), |
| 45 | m_external(external), |
| 46 | m_artificial(artificial) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | //---------------------------------------------------------------------- |
| 51 | // Destructor |
| 52 | //---------------------------------------------------------------------- |
| 53 | Variable::~Variable() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void |
| 59 | Variable::Dump(Stream *s, bool show_context) const |
| 60 | { |
| 61 | s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
| 62 | s->Indent(); |
| 63 | *s << "Variable" << (const UserID&)*this; |
| 64 | |
| 65 | if (m_name) |
| 66 | *s << ", name = \"" << m_name << "\""; |
| 67 | |
| 68 | if (m_type != NULL) |
| 69 | { |
| 70 | *s << ", type = " << (void*)m_type << " ("; |
| 71 | m_type->DumpTypeName(s); |
| 72 | s->PutChar(')'); |
| 73 | } |
| 74 | |
| 75 | if (m_scope != eValueTypeInvalid) |
| 76 | { |
| 77 | s->PutCString(", scope = "); |
| 78 | switch (m_scope) |
| 79 | { |
| 80 | case eValueTypeVariableGlobal: s->PutCString(m_external ? "global" : "static"); break; |
| 81 | case eValueTypeVariableArgument: s->PutCString("parameter"); break; |
| 82 | case eValueTypeVariableLocal: s->PutCString("local"); break; |
| 83 | default: *s << "??? (" << m_scope << ')'; |
| 84 | } |
| 85 | } |
| 86 | |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 87 | if (show_context && m_owner_scope != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | { |
| 89 | s->PutCString(", context = ( "); |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 90 | m_owner_scope->DumpSymbolContext(s); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | s->PutCString(" )"); |
| 92 | } |
| 93 | |
| 94 | m_declaration.Dump(s); |
| 95 | |
| 96 | if (m_location.IsValid()) |
| 97 | { |
| 98 | s->PutCString(", location = "); |
Greg Clayton | 178710c | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 99 | lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS; |
| 100 | if (m_location.IsLocationList()) |
| 101 | { |
| 102 | SymbolContext variable_sc; |
| 103 | m_owner_scope->CalculateSymbolContext(&variable_sc); |
| 104 | if (variable_sc.function) |
| 105 | loclist_base_addr = variable_sc.function->GetAddressRange().GetBaseAddress().GetFileAddress(); |
| 106 | } |
| 107 | m_location.GetDescription(s, lldb::eDescriptionLevelBrief, loclist_base_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (m_external) |
| 111 | s->PutCString(", external"); |
| 112 | |
| 113 | if (m_artificial) |
| 114 | s->PutCString(", artificial"); |
| 115 | |
| 116 | s->EOL(); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | size_t |
| 121 | Variable::MemorySize() const |
| 122 | { |
| 123 | return sizeof(Variable); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void |
| 128 | Variable::CalculateSymbolContext (SymbolContext *sc) |
| 129 | { |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 130 | if (m_owner_scope) |
| 131 | m_owner_scope->CalculateSymbolContext(sc); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | else |
| 133 | sc->Clear(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | bool |
| 138 | Variable::IsInScope (StackFrame *frame) |
| 139 | { |
| 140 | switch (m_scope) |
| 141 | { |
| 142 | case eValueTypeVariableGlobal: |
Greg Clayton | 178710c | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 143 | case eValueTypeVariableStatic: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | // Globals and statics are always in scope. |
| 145 | return true; |
| 146 | |
| 147 | case eValueTypeVariableArgument: |
| 148 | case eValueTypeVariableLocal: |
| 149 | // Check if the location has a location list that describes the value |
| 150 | // of the variable with address ranges and different locations for each |
| 151 | // address range? |
| 152 | if (m_location.IsLocationList()) |
| 153 | { |
Greg Clayton | 178710c | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 154 | SymbolContext sc; |
| 155 | CalculateSymbolContext(&sc); |
| 156 | |
| 157 | // Currently we only support functions that have things with |
| 158 | // locations lists. If this expands, we will need to add support |
| 159 | assert (sc.function); |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame^] | 160 | Target *target = &frame->GetThread().GetProcess().GetTarget(); |
| 161 | addr_t loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target); |
Greg Clayton | 178710c | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 162 | if (loclist_base_load_addr == LLDB_INVALID_ADDRESS) |
| 163 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | // It is a location list. We just need to tell if the location |
| 165 | // list contains the current address when converted to a load |
| 166 | // address |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame^] | 167 | return m_location.LocationListContainsAddress (loclist_base_load_addr, frame->GetFrameCodeAddress().GetLoadAddress (target)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | } |
| 169 | else |
| 170 | { |
| 171 | // We don't have a location list, we just need to see if the block |
| 172 | // that this variable was defined in is currently |
Greg Clayton | a357ecf | 2010-09-14 03:16:58 +0000 | [diff] [blame] | 173 | Block *deepest_frame_block = frame->GetSymbolContext(eSymbolContextBlock).block; |
| 174 | if (deepest_frame_block) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | { |
| 176 | SymbolContext variable_sc; |
| 177 | CalculateSymbolContext (&variable_sc); |
Greg Clayton | a357ecf | 2010-09-14 03:16:58 +0000 | [diff] [blame] | 178 | if (variable_sc.block == deepest_frame_block) |
Greg Clayton | 178710c | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 179 | return true; |
| 180 | |
Greg Clayton | a357ecf | 2010-09-14 03:16:58 +0000 | [diff] [blame] | 181 | return variable_sc.block->Contains (deepest_frame_block); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | default: |
Greg Clayton | 178710c | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 187 | assert (!"Unhandled case"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |