blob: fd3e4afb23edde43ae6a3e1ef9a0f1491c8d2d54 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Symbol.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/Symbol.h"
11
12#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Target/Process.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20
21Symbol::Symbol() :
22 UserID (),
23 m_mangled (),
24 m_type (eSymbolTypeInvalid),
25 m_type_data (0),
26 m_type_data_resolved (false),
27 m_is_synthetic (false),
28 m_is_debug (false),
29 m_is_external (false),
30 m_size_is_sibling (false),
31 m_size_is_synthesized (false),
32 m_searched_for_function (false),
33 m_addr_range (),
34 m_flags (),
35 m_function (NULL)
36{
37}
38
39Symbol::Symbol
40(
41 user_id_t symID,
42 const char *name,
43 bool name_is_mangled,
44 SymbolType type,
45 bool external,
46 bool is_debug,
47 bool is_trampoline,
48 bool is_artificial,
49 const Section* section,
50 addr_t offset,
51 uint32_t size,
52 uint32_t flags
53) :
54 UserID (symID),
55 m_mangled (name, name_is_mangled),
56 m_type (type),
57 m_type_data (0),
58 m_type_data_resolved (false),
59 m_is_synthetic (is_artificial),
60 m_is_debug (is_debug),
61 m_is_external (external),
62 m_size_is_sibling (false),
63 m_size_is_synthesized (false),
64 m_searched_for_function (false),
65 m_addr_range (section, offset, size),
66 m_flags (flags),
67 m_function (NULL)
68{
69}
70
71Symbol::Symbol
72(
73 user_id_t symID,
74 const char *name,
75 bool name_is_mangled,
76 SymbolType type,
77 bool external,
78 bool is_debug,
79 bool is_trampoline,
80 bool is_artificial,
81 const AddressRange &range,
82 uint32_t flags
83) :
84 UserID (symID),
85 m_mangled (name, name_is_mangled),
86 m_type (type),
87 m_type_data (0),
88 m_type_data_resolved (false),
89 m_is_synthetic (is_artificial),
90 m_is_debug (is_debug),
91 m_is_external (external),
92 m_size_is_sibling (false),
93 m_size_is_synthesized (false),
94 m_searched_for_function (false),
95 m_addr_range (range),
96 m_flags (flags),
97 m_function (NULL)
98{
99}
100
101Symbol::Symbol(const Symbol& rhs):
102 UserID (rhs),
103 m_mangled (rhs.m_mangled),
104 m_type (rhs.m_type),
105 m_type_data (rhs.m_type_data),
106 m_type_data_resolved (rhs.m_type_data_resolved),
107 m_is_synthetic (rhs.m_is_synthetic),
108 m_is_debug (rhs.m_is_debug),
109 m_is_external (rhs.m_is_external),
110 m_size_is_sibling (rhs.m_size_is_sibling),
111 m_size_is_synthesized (false),
112 m_searched_for_function (false),
113 m_addr_range (rhs.m_addr_range),
114 m_flags (rhs.m_flags),
115 m_function (NULL)
116{
117}
118
119const Symbol&
120Symbol::operator= (const Symbol& rhs)
121{
122 if (this != &rhs)
123 {
124 UserID::operator= (rhs);
125 m_mangled = rhs.m_mangled;
126 m_type = rhs.m_type;
127 m_type_data = rhs.m_type_data;
128 m_type_data_resolved = rhs.m_type_data_resolved;
129 m_is_synthetic = rhs.m_is_synthetic;
130 m_is_debug = rhs.m_is_debug;
131 m_is_external = rhs.m_is_external;
132 m_size_is_sibling = rhs.m_size_is_sibling;
133 m_size_is_synthesized = rhs.m_size_is_sibling;
134 m_searched_for_function = rhs.m_searched_for_function;
135 m_addr_range = rhs.m_addr_range;
136 m_flags = rhs.m_flags;
137 m_function = rhs.m_function;
138 }
139 return *this;
140}
141
Chris Lattner24943d22010-06-08 16:52:24 +0000142AddressRange *
143Symbol::GetAddressRangePtr()
144{
145 if (m_addr_range.GetBaseAddress().GetSection())
146 return &m_addr_range;
147 return NULL;
148}
149
150const AddressRange *
151Symbol::GetAddressRangePtr() const
152{
153 if (m_addr_range.GetBaseAddress().GetSection())
154 return &m_addr_range;
155 return NULL;
156}
157
Chris Lattner24943d22010-06-08 16:52:24 +0000158uint32_t
159Symbol::GetSiblingIndex() const
160{
161 return m_size_is_sibling ? m_addr_range.GetByteSize() : 0;
162}
163
Chris Lattner24943d22010-06-08 16:52:24 +0000164bool
165Symbol::IsTrampoline () const
166{
167 return m_type == eSymbolTypeTrampoline;
168}
169
Chris Lattner24943d22010-06-08 16:52:24 +0000170void
171Symbol::Dump(Stream *s, Process *process, uint32_t index) const
172{
173// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
174// s->Indent();
175// s->Printf("Symbol[%5u] %6u %c%c %-12s ",
176 s->Printf("[%5u] %6u %c%c%c %-12s ",
177 index,
178 GetID(),
179 m_is_debug ? 'D' : ' ',
180 m_is_synthetic ? 'S' : ' ',
181 m_is_external ? 'X' : ' ',
182 GetTypeAsString());
183
184 const Section *section = m_addr_range.GetBaseAddress().GetSection();
185 if (section != NULL)
186 {
187 if (!m_addr_range.GetBaseAddress().Dump(s, NULL, Address::DumpStyleFileAddress))
188 s->Printf("%*s", 18, "");
189
190 s->PutChar(' ');
191
192 if (!m_addr_range.GetBaseAddress().Dump(s, process, Address::DumpStyleLoadAddress))
193 s->Printf("%*s", 18, "");
194
195 const char *format = m_size_is_sibling ?
196 " Sibling -> [%5llu] 0x%8.8x %s\n":
197 " 0x%16.16llx 0x%8.8x %s\n";
198 s->Printf( format,
199 m_addr_range.GetByteSize(),
200 m_flags,
201 m_mangled.GetName().AsCString(""));
202 }
203 else
204 {
205 const char *format = m_size_is_sibling ?
206 "0x%16.16llx Sibling -> [%5llu] 0x%8.8x %s\n":
207 "0x%16.16llx 0x%16.16llx 0x%8.8x %s\n";
208 s->Printf( format,
209 m_addr_range.GetBaseAddress().GetOffset(),
210 m_addr_range.GetByteSize(),
211 m_flags,
212 m_mangled.GetName().AsCString(""));
213 }
214}
215
Chris Lattner24943d22010-06-08 16:52:24 +0000216Function *
217Symbol::GetFunction ()
218{
219 if (m_function == NULL && !m_searched_for_function)
220 {
221 m_searched_for_function = true;
222 Module *module = m_addr_range.GetBaseAddress().GetModule();
223 if (module)
224 {
225 SymbolContext sc;
226 if (module->ResolveSymbolContextForAddress(m_addr_range.GetBaseAddress(), eSymbolContextFunction, sc))
227 m_function = sc.function;
228 }
229 }
230 return m_function;
231}
232
233uint32_t
234Symbol::GetPrologueByteSize ()
235{
236 if (m_type == eSymbolTypeCode || m_type == eSymbolTypeFunction)
237 {
238 if (!m_type_data_resolved)
239 {
240 m_type_data_resolved = true;
241 Module *module = m_addr_range.GetBaseAddress().GetModule();
242 SymbolContext sc;
243 if (module && module->ResolveSymbolContextForAddress (m_addr_range.GetBaseAddress(),
244 eSymbolContextLineEntry,
245 sc))
246 {
247 m_type_data = sc.line_entry.range.GetByteSize();
248 }
249 else
250 {
251 // TODO: expose something in Process to figure out the
252 // size of a function prologue.
253 }
254 }
255 return m_type_data;
256 }
257 return 0;
258}
259
260void
Chris Lattner24943d22010-06-08 16:52:24 +0000261Symbol::SetValue(addr_t value)
262{
263 m_addr_range.GetBaseAddress().SetSection(NULL);
264 m_addr_range.GetBaseAddress().SetOffset(value);
265}
266
267
268bool
269Symbol::Compare(const ConstString& name, SymbolType type) const
270{
271 if (m_type == eSymbolTypeAny || m_type == type)
272 return m_mangled.GetMangledName() == name || m_mangled.GetDemangledName() == name;
273 return false;
274}
275
276#define ENUM_TO_CSTRING(x) case eSymbolType##x: return #x;
277
278const char *
279Symbol::GetTypeAsString() const
280{
281 switch (m_type)
282 {
283 ENUM_TO_CSTRING(Invalid);
284 ENUM_TO_CSTRING(Absolute);
285 ENUM_TO_CSTRING(Extern);
286 ENUM_TO_CSTRING(Code);
287 ENUM_TO_CSTRING(Data);
288 ENUM_TO_CSTRING(Trampoline);
289 ENUM_TO_CSTRING(Runtime);
290 ENUM_TO_CSTRING(Exception);
291 ENUM_TO_CSTRING(SourceFile);
292 ENUM_TO_CSTRING(HeaderFile);
293 ENUM_TO_CSTRING(ObjectFile);
294 ENUM_TO_CSTRING(Function);
295 ENUM_TO_CSTRING(FunctionEnd);
296 ENUM_TO_CSTRING(CommonBlock);
297 ENUM_TO_CSTRING(Block);
298 ENUM_TO_CSTRING(Static);
299 ENUM_TO_CSTRING(Global);
300 ENUM_TO_CSTRING(Local);
301 ENUM_TO_CSTRING(Param);
302 ENUM_TO_CSTRING(Variable);
303 ENUM_TO_CSTRING(VariableType);
304 ENUM_TO_CSTRING(LineEntry);
305 ENUM_TO_CSTRING(LineHeader);
306 ENUM_TO_CSTRING(ScopeBegin);
307 ENUM_TO_CSTRING(ScopeEnd);
308 ENUM_TO_CSTRING(Additional);
309 ENUM_TO_CSTRING(Compiler);
310 ENUM_TO_CSTRING(Instrumentation);
311 ENUM_TO_CSTRING(Undefined);
312 default:
313 break;
314 }
315 return "<unknown SymbolType>";
316}
317