blob: 7963a1fbbe7b40bf70e689a6adf4a64ff5faa700 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SymbolContext.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/SymbolContext.h"
Greg Clayton12bec712010-06-28 21:30:43 +000011
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
Greg Clayton12bec712010-06-28 21:30:43 +000013#include "lldb/Symbol/CompileUnit.h"
Chris Lattner24943d22010-06-08 16:52:24 +000014#include "lldb/Symbol/ObjectFile.h"
15#include "lldb/Symbol/Symbol.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton12bec712010-06-28 21:30:43 +000017#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22SymbolContext::SymbolContext() :
23 target_sp (),
24 module_sp (),
25 comp_unit (NULL),
26 function (NULL),
27 block (NULL),
28 line_entry (),
29 symbol (NULL)
30{
31}
32
33SymbolContext::SymbolContext(const ModuleSP& m, CompileUnit *cu, Function *f, Block *b, LineEntry *le, Symbol *s) :
34 target_sp (),
35 module_sp (m),
36 comp_unit (cu),
37 function (f),
38 block (b),
39 line_entry (),
40 symbol (s)
41{
42 if (le)
43 line_entry = *le;
44}
45
46SymbolContext::SymbolContext(const TargetSP &t, const ModuleSP& m, CompileUnit *cu, Function *f, Block *b, LineEntry *le, Symbol *s) :
47 target_sp (t),
48 module_sp (m),
49 comp_unit (cu),
50 function (f),
51 block (b),
52 line_entry (),
53 symbol (s)
54{
55 if (le)
56 line_entry = *le;
57}
58
59SymbolContext::SymbolContext(const SymbolContext& rhs) :
60 target_sp (rhs.target_sp),
61 module_sp (rhs.module_sp),
62 comp_unit (rhs.comp_unit),
63 function (rhs.function),
64 block (rhs.block),
65 line_entry (rhs.line_entry),
66 symbol (rhs.symbol)
67{
68}
69
70
71SymbolContext::SymbolContext (SymbolContextScope *sc_scope) :
72 target_sp (),
73 module_sp (),
74 comp_unit (NULL),
75 function (NULL),
76 block (NULL),
77 line_entry (),
78 symbol (NULL)
79{
80 sc_scope->CalculateSymbolContext (this);
81}
82
83const SymbolContext&
84SymbolContext::operator= (const SymbolContext& rhs)
85{
86 if (this != &rhs)
87 {
88 target_sp = rhs.target_sp;
89 module_sp = rhs.module_sp;
90 comp_unit = rhs.comp_unit;
91 function = rhs.function;
92 block = rhs.block;
93 line_entry = rhs.line_entry;
94 symbol = rhs.symbol;
95 }
96 return *this;
97}
98
99void
100SymbolContext::Clear()
101{
102 target_sp.reset();
103 module_sp.reset();
104 comp_unit = NULL;
105 function = NULL;
106 block = NULL;
107 line_entry.Clear();
108 symbol = NULL;
109}
110
111void
112SymbolContext::DumpStopContext
113(
114 Stream *s,
115 ExecutionContextScope *exe_scope,
116 const Address &addr,
117 bool show_module
118) const
119{
Chris Lattner24943d22010-06-08 16:52:24 +0000120 if (show_module && module_sp)
121 {
122 *s << module_sp->GetFileSpec().GetFilename() << '`';
123 }
124
125 if (function != NULL)
126 {
127 if (function->GetMangled().GetName())
128 function->GetMangled().GetName().Dump(s);
129
Greg Clayton70436352010-06-30 23:03:03 +0000130 const addr_t function_offset = addr.GetOffset() - function->GetAddressRange().GetBaseAddress().GetOffset();
131 if (function_offset)
132 s->Printf(" + %llu", function_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000133
134 if (block != NULL)
135 {
136 s->IndentMore();
137 block->DumpStopContext(s, this);
138 s->IndentLess();
139 }
140 else
141 {
142 if (line_entry.IsValid())
143 {
144 s->PutCString(" at ");
145 if (line_entry.DumpStopContext(s))
146 return;
147 }
148 }
149 }
150 else if (symbol != NULL)
151 {
152 symbol->GetMangled().GetName().Dump(s);
153
154 if (symbol->GetAddressRangePtr())
155 {
Greg Clayton70436352010-06-30 23:03:03 +0000156 const addr_t symbol_offset = addr.GetOffset() - symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset();
157 if (symbol_offset)
158 s->Printf(" + %llu", symbol_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000159 }
160 }
161 else
162 {
163 addr.Dump(s, exe_scope, Address::DumpStyleModuleWithFileAddress);
164 }
165}
166
167void
Greg Clayton12bec712010-06-28 21:30:43 +0000168SymbolContext::GetDescription(Stream *s, lldb::DescriptionLevel level, Process *process) const
169{
170 if (module_sp)
171 {
172 s->Indent(" Module: \"");
173 module_sp->GetFileSpec().Dump(s);
174 s->PutChar('"');
175 s->EOL();
176 }
177
178 if (comp_unit != NULL)
179 {
180 s->Indent("CompileUnit: ");
181 comp_unit->GetDescription (s, level);
182 s->EOL();
183 }
184
185 if (function != NULL)
186 {
187 s->Indent(" Function: ");
188 function->GetDescription (s, level, process);
189 s->EOL();
190
191 Type *func_type = function->GetType();
192 if (func_type)
193 {
194 s->Indent(" FuncType: ");
195 func_type->GetDescription (s, level, false);
196 s->EOL();
197 }
198 }
199
200 if (block != NULL)
201 {
202 std::vector<Block *> blocks;
203 blocks.push_back (block);
204 Block *parent_block = block->GetParent();
205
206 while (parent_block)
207 {
208 blocks.push_back (parent_block);
209 parent_block = parent_block->GetParent();
210 }
211 std::vector<Block *>::reverse_iterator pos;
212 std::vector<Block *>::reverse_iterator begin = blocks.rbegin();
213 std::vector<Block *>::reverse_iterator end = blocks.rend();
214 for (pos = begin; pos != end; ++pos)
215 {
216 if (pos == begin)
217 s->Indent(" Blocks: ");
218 else
219 s->Indent(" ");
220 (*pos)->GetDescription(s, level, process);
221 s->EOL();
222 }
223 }
224
225 if (line_entry.IsValid())
226 {
227 s->Indent(" LineEntry: ");
228 line_entry.GetDescription (s, level, comp_unit, process, false);
229 s->EOL();
230 }
231
232 if (symbol != NULL)
233 {
234 s->Indent(" Symbol: ");
235 symbol->GetDescription(s, level, process);
236 s->EOL();
237 }
238}
239
240
241
242void
Chris Lattner24943d22010-06-08 16:52:24 +0000243SymbolContext::Dump(Stream *s, Process *process) const
244{
245 *s << (void *)this << ": ";
246 s->Indent();
247 s->PutCString("SymbolContext");
248 s->IndentMore();
249 s->EOL();
250 s->IndentMore();
251 s->Indent();
Greg Clayton12bec712010-06-28 21:30:43 +0000252 *s << "Module = " << (void *)module_sp.get() << ' ';
Chris Lattner24943d22010-06-08 16:52:24 +0000253 if (module_sp)
254 module_sp->GetFileSpec().Dump(s);
255 s->EOL();
256 s->Indent();
257 *s << "CompileUnit = " << (void *)comp_unit;
258 if (comp_unit != NULL)
Greg Clayton12bec712010-06-28 21:30:43 +0000259 *s << " {0x" << comp_unit->GetID() << "} " << *(dynamic_cast<FileSpec*> (comp_unit));
Chris Lattner24943d22010-06-08 16:52:24 +0000260 s->EOL();
261 s->Indent();
Greg Clayton12bec712010-06-28 21:30:43 +0000262 *s << "Function = " << (void *)function;
Chris Lattner24943d22010-06-08 16:52:24 +0000263 if (function != NULL)
264 {
Greg Clayton12bec712010-06-28 21:30:43 +0000265 *s << " {0x" << function->GetID() << "} " << function->GetType()->GetName() << ", address-range = ";
266 function->GetAddressRange().Dump(s, process, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
267 s->EOL();
268 s->Indent();
269 Type* func_type = function->GetType();
270 if (func_type)
271 {
272 *s << " Type = ";
273 func_type->Dump (s, false);
274 }
Chris Lattner24943d22010-06-08 16:52:24 +0000275 }
276 s->EOL();
277 s->Indent();
Greg Clayton12bec712010-06-28 21:30:43 +0000278 *s << "Block = " << (void *)block;
Chris Lattner24943d22010-06-08 16:52:24 +0000279 if (block != NULL)
Greg Clayton12bec712010-06-28 21:30:43 +0000280 *s << " {0x" << block->GetID() << '}';
Chris Lattner24943d22010-06-08 16:52:24 +0000281 // Dump the block and pass it a negative depth to we print all the parent blocks
282 //if (block != NULL)
283 // block->Dump(s, function->GetFileAddress(), INT_MIN);
284 s->EOL();
285 s->Indent();
Greg Clayton12bec712010-06-28 21:30:43 +0000286 *s << "LineEntry = ";
Chris Lattner24943d22010-06-08 16:52:24 +0000287 line_entry.Dump (s, process, true, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, true);
288 s->EOL();
289 s->Indent();
Greg Clayton12bec712010-06-28 21:30:43 +0000290 *s << "Symbol = " << (void *)symbol;
Chris Lattner24943d22010-06-08 16:52:24 +0000291 if (symbol != NULL && symbol->GetMangled())
292 *s << ' ' << symbol->GetMangled().GetName().AsCString();
293 s->EOL();
294 s->IndentLess();
295 s->IndentLess();
296}
297
298bool
299lldb_private::operator== (const SymbolContext& lhs, const SymbolContext& rhs)
300{
301 return lhs.target_sp.get() == rhs.target_sp.get() &&
302 lhs.module_sp.get() == rhs.module_sp.get() &&
303 lhs.comp_unit == rhs.comp_unit &&
304 lhs.function == rhs.function &&
305 LineEntry::Compare(lhs.line_entry, rhs.line_entry) == 0 &&
306 lhs.symbol == rhs.symbol;
307}
308
309bool
310lldb_private::operator!= (const SymbolContext& lhs, const SymbolContext& rhs)
311{
312 return lhs.target_sp.get() != rhs.target_sp.get() ||
313 lhs.module_sp.get() != rhs.module_sp.get() ||
314 lhs.comp_unit != rhs.comp_unit ||
315 lhs.function != rhs.function ||
316 LineEntry::Compare(lhs.line_entry, rhs.line_entry) != 0 ||
317 lhs.symbol != rhs.symbol;
318}
319
320bool
321SymbolContext::GetAddressRange (uint32_t scope, AddressRange &range) const
322{
323 if ((scope & eSymbolContextLineEntry) && line_entry.IsValid())
324 {
325 range = line_entry.range;
326 return true;
327 }
328 else if ((scope & eSymbolContextFunction) && function != NULL)
329 {
330 range = function->GetAddressRange();
331 return true;
332 }
333 else if ((scope & eSymbolContextSymbol) && symbol != NULL && symbol->GetAddressRangePtr())
334 {
335 range = *symbol->GetAddressRangePtr();
336
337 if (range.GetByteSize() == 0)
338 {
339 if (module_sp)
340 {
341 ObjectFile *objfile = module_sp->GetObjectFile();
342 if (objfile)
343 {
344 Symtab *symtab = objfile->GetSymtab();
345 if (symtab)
346 range.SetByteSize(symtab->CalculateSymbolSize (symbol));
347 }
348 }
349 }
350 return true;
351 }
352 range.Clear();
353 return false;
354}
355
356
357Function *
358SymbolContext::FindFunctionByName (const char *name) const
359{
360 ConstString name_const_str (name);
361 if (function != NULL)
362 {
363 // FIXME: Look in the class of the current function, if it exists,
364 // for methods matching name.
365 }
366
367 //
368 if (comp_unit != NULL)
369 {
370 // Make sure we've read in all the functions. We should be able to check and see
371 // if there's one by this name present before we do this...
372 module_sp->GetSymbolVendor()->ParseCompileUnitFunctions(*this);
373 uint32_t func_idx;
374 lldb::FunctionSP func_sp;
375 for (func_idx = 0; (func_sp = comp_unit->GetFunctionAtIndex(func_idx)) != NULL; ++func_idx)
376 {
377 if (func_sp->GetMangled().GetName() == name_const_str)
378 return func_sp.get();
379 }
380 }
381 if (module_sp != NULL)
382 {
383 SymbolContextList sc_matches;
Greg Clayton12bec712010-06-28 21:30:43 +0000384 if (module_sp->FindFunctions (name_const_str, eFunctionNameTypeBase | eFunctionNameTypeFull, false, sc_matches) > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000385 {
386 SymbolContext sc;
387 sc_matches.GetContextAtIndex (0, sc);
388 return sc.function;
389 }
390 }
391
392 if (target_sp)
393 {
394 SymbolContextList sc_matches;
Greg Clayton12bec712010-06-28 21:30:43 +0000395 if (target_sp->GetImages().FindFunctions (name_const_str, eFunctionNameTypeBase | eFunctionNameTypeFull, sc_matches) > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000396 {
397 SymbolContext sc;
398 sc_matches.GetContextAtIndex (0, sc);
399 return sc.function;
400 }
401 }
402
403 return NULL;
404}
405
406lldb::VariableSP
407SymbolContext::FindVariableByName (const char *name) const
408{
409 lldb::VariableSP return_value;
410 return return_value;
411}
412
413lldb::TypeSP
414SymbolContext::FindTypeByName (const char *name) const
415{
416 lldb::TypeSP return_value;
417 return return_value;
418}
419
420//----------------------------------------------------------------------
421//
422// SymbolContextList
423//
424//----------------------------------------------------------------------
425
426
427SymbolContextList::SymbolContextList() :
428 m_symbol_contexts()
429{
430}
431
432SymbolContextList::~SymbolContextList()
433{
434}
435
436void
437SymbolContextList::Append(const SymbolContext& sc)
438{
439 m_symbol_contexts.push_back(sc);
440}
441
442void
443SymbolContextList::Clear()
444{
445 m_symbol_contexts.clear();
446}
447
448void
449SymbolContextList::Dump(Stream *s, Process *process) const
450{
451
452 *s << (void *)this << ": ";
453 s->Indent();
454 s->PutCString("SymbolContextList");
455 s->EOL();
456 s->IndentMore();
457
458 collection::const_iterator pos, end = m_symbol_contexts.end();
459 for (pos = m_symbol_contexts.begin(); pos != end; ++pos)
460 {
461 pos->Dump(s, process);
462 }
463 s->IndentLess();
464}
465
466bool
467SymbolContextList::GetContextAtIndex(uint32_t idx, SymbolContext& sc) const
468{
469 if (idx < m_symbol_contexts.size())
470 {
471 sc = m_symbol_contexts[idx];
472 return true;
473 }
474 return false;
475}
476
477bool
478SymbolContextList::RemoveContextAtIndex (uint32_t idx)
479{
480 if (idx < m_symbol_contexts.size())
481 {
482 m_symbol_contexts.erase(m_symbol_contexts.begin() + idx);
483 return true;
484 }
485 return false;
486}
487
488uint32_t
489SymbolContextList::GetSize() const
490{
491 return m_symbol_contexts.size();
492}