blob: 27af65ff0bea84e5b7b8b299266eec6a37518e75 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Claytoneea26402010-09-14 23:36:40 +000017#include "lldb/Target/Process.h"
Greg Claytonb04e7a82010-08-24 21:05:24 +000018#include "lldb/Target/RegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Target/StackFrame.h"
20#include "lldb/Target/Thread.h"
Greg Claytoneea26402010-09-14 23:36:40 +000021#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------
27// Variable constructor
28//----------------------------------------------------------------------
29Variable::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 Clayton4fb08152010-08-30 18:11:35 +000042 m_owner_scope(context),
Chris Lattner24943d22010-06-08 16:52:24 +000043 m_declaration(decl_ptr),
44 m_location(location),
45 m_external(external),
46 m_artificial(artificial)
47{
48}
49
50//----------------------------------------------------------------------
51// Destructor
52//----------------------------------------------------------------------
53Variable::~Variable()
54{
55}
56
57
58void
59Variable::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 Clayton4fb08152010-08-30 18:11:35 +000087 if (show_context && m_owner_scope != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000088 {
89 s->PutCString(", context = ( ");
Greg Clayton4fb08152010-08-30 18:11:35 +000090 m_owner_scope->DumpSymbolContext(s);
Chris Lattner24943d22010-06-08 16:52:24 +000091 s->PutCString(" )");
92 }
93
94 m_declaration.Dump(s);
95
96 if (m_location.IsValid())
97 {
98 s->PutCString(", location = ");
Greg Clayton178710c2010-09-14 02:20:48 +000099 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 Lattner24943d22010-06-08 16:52:24 +0000108 }
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
120size_t
121Variable::MemorySize() const
122{
123 return sizeof(Variable);
124}
125
126
127void
128Variable::CalculateSymbolContext (SymbolContext *sc)
129{
Greg Clayton4fb08152010-08-30 18:11:35 +0000130 if (m_owner_scope)
131 m_owner_scope->CalculateSymbolContext(sc);
Chris Lattner24943d22010-06-08 16:52:24 +0000132 else
133 sc->Clear();
134}
135
136
137bool
138Variable::IsInScope (StackFrame *frame)
139{
140 switch (m_scope)
141 {
142 case eValueTypeVariableGlobal:
Greg Clayton178710c2010-09-14 02:20:48 +0000143 case eValueTypeVariableStatic:
Chris Lattner24943d22010-06-08 16:52:24 +0000144 // 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 Clayton178710c2010-09-14 02:20:48 +0000154 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 Claytoneea26402010-09-14 23:36:40 +0000160 Target *target = &frame->GetThread().GetProcess().GetTarget();
161 addr_t loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton178710c2010-09-14 02:20:48 +0000162 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
163 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000164 // 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 Claytoneea26402010-09-14 23:36:40 +0000167 return m_location.LocationListContainsAddress (loclist_base_load_addr, frame->GetFrameCodeAddress().GetLoadAddress (target));
Chris Lattner24943d22010-06-08 16:52:24 +0000168 }
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 Claytona357ecf2010-09-14 03:16:58 +0000173 Block *deepest_frame_block = frame->GetSymbolContext(eSymbolContextBlock).block;
174 if (deepest_frame_block)
Chris Lattner24943d22010-06-08 16:52:24 +0000175 {
176 SymbolContext variable_sc;
177 CalculateSymbolContext (&variable_sc);
Greg Claytona357ecf2010-09-14 03:16:58 +0000178 if (variable_sc.block == deepest_frame_block)
Greg Clayton178710c2010-09-14 02:20:48 +0000179 return true;
180
Greg Claytona357ecf2010-09-14 03:16:58 +0000181 return variable_sc.block->Contains (deepest_frame_block);
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
183 }
184 break;
185
186 default:
Greg Clayton178710c2010-09-14 02:20:48 +0000187 assert (!"Unhandled case");
Chris Lattner24943d22010-06-08 16:52:24 +0000188 break;
189 }
190 return false;
191}
192