blob: 28180fce63a88cf7911797aca815015e45a4072a [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBModule.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/API/SBModule.h"
Greg Clayton466f6c42010-09-10 18:31:35 +000011#include "lldb/API/SBAddress.h"
12#include "lldb/API/SBFileSpec.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000013#include "lldb/API/SBStream.h"
Greg Clayton4ed315f2011-06-21 01:34:41 +000014#include "lldb/API/SBSymbolContextList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/Module.h"
Caroline Tice7826c882010-10-26 03:11:13 +000016#include "lldb/Core/Log.h"
Greg Clayton135adf22010-10-31 19:57:43 +000017#include "lldb/Core/StreamString.h"
Greg Clayton917c0002011-06-29 22:09:02 +000018#include "lldb/Core/ValueObjectList.h"
19#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata979e20d2011-07-29 19:53:35 +000020#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton917c0002011-06-29 22:09:02 +000021#include "lldb/Symbol/VariableList.h"
22#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
24using namespace lldb;
Caroline Tice7826c882010-10-26 03:11:13 +000025using namespace lldb_private;
Chris Lattner24943d22010-06-08 16:52:24 +000026
27
28SBModule::SBModule () :
Greg Clayton63094e02010-06-23 01:19:29 +000029 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000030{
31}
32
33SBModule::SBModule (const lldb::ModuleSP& module_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000034 m_opaque_sp (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000035{
36}
37
Greg Clayton538eb822010-11-05 23:17:00 +000038SBModule::SBModule(const SBModule &rhs) :
39 m_opaque_sp (rhs.m_opaque_sp)
40{
41}
42
43const SBModule &
44SBModule::operator = (const SBModule &rhs)
45{
46 if (this != &rhs)
47 m_opaque_sp = rhs.m_opaque_sp;
48 return *this;
49}
50
Chris Lattner24943d22010-06-08 16:52:24 +000051SBModule::~SBModule ()
52{
53}
54
55bool
56SBModule::IsValid () const
57{
Greg Clayton63094e02010-06-23 01:19:29 +000058 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000059}
60
Jim Inghame0bd5712011-12-19 20:39:44 +000061void
62SBModule::Clear()
63{
64 m_opaque_sp.reset();
65}
66
Chris Lattner24943d22010-06-08 16:52:24 +000067SBFileSpec
68SBModule::GetFileSpec () const
69{
Greg Claytone005f2c2010-11-06 01:53:30 +000070 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000071
Chris Lattner24943d22010-06-08 16:52:24 +000072 SBFileSpec file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +000073 if (m_opaque_sp)
74 file_spec.SetFileSpec(m_opaque_sp->GetFileSpec());
Caroline Tice7826c882010-10-26 03:11:13 +000075
76 if (log)
77 {
Greg Clayton49ce6822010-10-31 03:01:06 +000078 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
79 m_opaque_sp.get(), file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +000080 }
81
Chris Lattner24943d22010-06-08 16:52:24 +000082 return file_spec;
83}
84
Greg Clayton180546b2011-04-30 01:09:13 +000085lldb::SBFileSpec
86SBModule::GetPlatformFileSpec () const
87{
88 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
89
90 SBFileSpec file_spec;
91 if (m_opaque_sp)
92 file_spec.SetFileSpec(m_opaque_sp->GetPlatformFileSpec());
93
94 if (log)
95 {
96 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
97 m_opaque_sp.get(), file_spec.get());
98 }
99
100 return file_spec;
101
102}
103
104bool
105SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
106{
107 bool result = false;
108 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
109
110 if (m_opaque_sp)
111 {
112 m_opaque_sp->SetPlatformFileSpec(*platform_file);
113 result = true;
114 }
115
116 if (log)
117 {
118 log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s%s%s)) => %i",
119 m_opaque_sp.get(),
120 platform_file.get(),
121 platform_file->GetDirectory().GetCString(),
122 platform_file->GetDirectory() ? "/" : "",
123 platform_file->GetFilename().GetCString(),
124 result);
125 }
126 return result;
127}
128
129
130
Chris Lattner24943d22010-06-08 16:52:24 +0000131const uint8_t *
132SBModule::GetUUIDBytes () const
133{
Greg Claytone005f2c2010-11-06 01:53:30 +0000134 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000135
Greg Claytona66ba462010-10-30 04:51:46 +0000136 const uint8_t *uuid_bytes = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000137 if (m_opaque_sp)
Greg Claytona66ba462010-10-30 04:51:46 +0000138 uuid_bytes = (const uint8_t *)m_opaque_sp->GetUUID().GetBytes();
Caroline Tice7826c882010-10-26 03:11:13 +0000139
140 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000141 {
142 if (uuid_bytes)
143 {
144 StreamString s;
145 m_opaque_sp->GetUUID().Dump (&s);
146 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", m_opaque_sp.get(), s.GetData());
147 }
148 else
149 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", m_opaque_sp.get());
150 }
151 return uuid_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000152}
153
154
Johnny Chen919ee602011-04-01 00:35:55 +0000155const char *
156SBModule::GetUUIDString () const
157{
158 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
159
160 static char uuid_string[80];
161 const char * uuid_c_string = NULL;
162 if (m_opaque_sp)
163 uuid_c_string = (const char *)m_opaque_sp->GetUUID().GetAsCString(uuid_string, sizeof(uuid_string));
164
165 if (log)
166 {
167 if (uuid_c_string)
168 {
169 StreamString s;
170 m_opaque_sp->GetUUID().Dump (&s);
171 log->Printf ("SBModule(%p)::GetUUIDString () => %s", m_opaque_sp.get(), s.GetData());
172 }
173 else
174 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", m_opaque_sp.get());
175 }
176 return uuid_c_string;
177}
178
179
Chris Lattner24943d22010-06-08 16:52:24 +0000180bool
181SBModule::operator == (const SBModule &rhs) const
182{
Greg Clayton63094e02010-06-23 01:19:29 +0000183 if (m_opaque_sp)
184 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000185 return false;
186}
187
188bool
189SBModule::operator != (const SBModule &rhs) const
190{
Greg Clayton63094e02010-06-23 01:19:29 +0000191 if (m_opaque_sp)
192 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000193 return false;
194}
195
196lldb::ModuleSP &
197SBModule::operator *()
198{
Greg Clayton63094e02010-06-23 01:19:29 +0000199 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000200}
201
202lldb_private::Module *
203SBModule::operator ->()
204{
Greg Clayton63094e02010-06-23 01:19:29 +0000205 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000206}
207
208const lldb_private::Module *
209SBModule::operator ->() const
210{
Greg Clayton63094e02010-06-23 01:19:29 +0000211 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000212}
213
214lldb_private::Module *
215SBModule::get()
216{
Greg Clayton63094e02010-06-23 01:19:29 +0000217 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000218}
219
220const lldb_private::Module *
221SBModule::get() const
222{
Greg Clayton63094e02010-06-23 01:19:29 +0000223 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000224}
225
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000226const lldb::ModuleSP &
227SBModule::get_sp() const
228{
229 return m_opaque_sp;
230}
Chris Lattner24943d22010-06-08 16:52:24 +0000231
232void
233SBModule::SetModule (const lldb::ModuleSP& module_sp)
234{
Greg Clayton63094e02010-06-23 01:19:29 +0000235 m_opaque_sp = module_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000236}
237
Greg Clayton466f6c42010-09-10 18:31:35 +0000238
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000239SBAddress
240SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
Greg Clayton466f6c42010-09-10 18:31:35 +0000241{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000242 lldb::SBAddress sb_addr;
243 if (m_opaque_sp)
244 {
245 Address addr;
246 if (m_opaque_sp->ResolveFileAddress (vm_addr, addr))
247 sb_addr.ref() = addr;
248 }
249 return sb_addr;
Greg Clayton466f6c42010-09-10 18:31:35 +0000250}
251
252SBSymbolContext
253SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
254{
255 SBSymbolContext sb_sc;
256 if (m_opaque_sp && addr.IsValid())
Greg Claytona3955062011-07-22 16:46:35 +0000257 m_opaque_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
Greg Clayton466f6c42010-09-10 18:31:35 +0000258 return sb_sc;
259}
260
Caroline Tice98f930f2010-09-20 05:20:02 +0000261bool
262SBModule::GetDescription (SBStream &description)
263{
Greg Clayton96154be2011-11-13 06:57:31 +0000264 Stream &strm = description.ref();
265
Caroline Tice98f930f2010-09-20 05:20:02 +0000266 if (m_opaque_sp)
267 {
Greg Clayton96154be2011-11-13 06:57:31 +0000268 m_opaque_sp->GetDescription (&strm);
Caroline Tice98f930f2010-09-20 05:20:02 +0000269 }
270 else
Greg Clayton96154be2011-11-13 06:57:31 +0000271 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000272
273 return true;
274}
Greg Clayton43edca32010-12-07 05:40:31 +0000275
276size_t
277SBModule::GetNumSymbols ()
278{
279 if (m_opaque_sp)
280 {
281 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
282 if (obj_file)
283 {
284 Symtab *symtab = obj_file->GetSymtab();
285 if (symtab)
286 return symtab->GetNumSymbols();
287 }
288 }
289 return 0;
290}
291
292SBSymbol
293SBModule::GetSymbolAtIndex (size_t idx)
294{
295 SBSymbol sb_symbol;
296 if (m_opaque_sp)
297 {
298 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
299 if (obj_file)
300 {
301 Symtab *symtab = obj_file->GetSymtab();
302 if (symtab)
303 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
304 }
305 }
306 return sb_symbol;
307}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000308
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000309size_t
310SBModule::GetNumSections ()
311{
312 if (m_opaque_sp)
313 {
314 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
315 if (obj_file)
316 {
317 SectionList *section_list = obj_file->GetSectionList ();
318 if (section_list)
319 return section_list->GetSize();
320 }
321 }
322 return 0;
323}
324
325SBSection
326SBModule::GetSectionAtIndex (size_t idx)
327{
328 SBSection sb_section;
329 if (m_opaque_sp)
330 {
331 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
332 if (obj_file)
333 {
334 SectionList *section_list = obj_file->GetSectionList ();
335
336 if (section_list)
337 sb_section.SetSection(section_list->GetSectionAtIndex (idx).get());
338 }
339 }
340 return sb_section;
341}
342
Greg Clayton4ed315f2011-06-21 01:34:41 +0000343uint32_t
344SBModule::FindFunctions (const char *name,
345 uint32_t name_type_mask,
346 bool append,
347 lldb::SBSymbolContextList& sc_list)
348{
349 if (!append)
350 sc_list.Clear();
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000351 if (name && m_opaque_sp)
Greg Clayton4ed315f2011-06-21 01:34:41 +0000352 {
353 const bool symbols_ok = true;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000354 return m_opaque_sp->FindFunctions (ConstString(name),
355 NULL,
Greg Clayton4ed315f2011-06-21 01:34:41 +0000356 name_type_mask,
357 symbols_ok,
358 append,
359 *sc_list);
360 }
361 return 0;
362}
363
Greg Clayton917c0002011-06-29 22:09:02 +0000364
365SBValueList
366SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
367{
368 SBValueList sb_value_list;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000369 if (name && m_opaque_sp)
Greg Clayton917c0002011-06-29 22:09:02 +0000370 {
371 VariableList variable_list;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000372 const uint32_t match_count = m_opaque_sp->FindGlobalVariables (ConstString (name),
373 NULL,
Greg Clayton917c0002011-06-29 22:09:02 +0000374 false,
375 max_matches,
376 variable_list);
377
378 if (match_count > 0)
379 {
380 ValueObjectList &value_object_list = sb_value_list.ref();
381 for (uint32_t i=0; i<match_count; ++i)
382 {
383 lldb::ValueObjectSP valobj_sp;
Greg Clayton334d33a2012-01-30 07:41:31 +0000384 TargetSP target_sp (target.GetSP());
385 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
Greg Clayton917c0002011-06-29 22:09:02 +0000386 if (valobj_sp)
387 value_object_list.Append(valobj_sp);
388 }
389 }
390 }
391
392 return sb_value_list;
393}
Enrico Granata979e20d2011-07-29 19:53:35 +0000394
395lldb::SBType
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000396SBModule::FindFirstType (const char *name_cstr)
Enrico Granata979e20d2011-07-29 19:53:35 +0000397{
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000398 SBType sb_type;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000399 if (name_cstr && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000400 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000401 SymbolContext sc;
402 TypeList type_list;
403 uint32_t num_matches = 0;
404 ConstString name(name_cstr);
405
406 num_matches = m_opaque_sp->FindTypes(sc,
407 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000408 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000409 false,
410 1,
411 type_list);
412
413 if (num_matches)
414 sb_type = lldb::SBType(type_list.GetTypeAtIndex(0));
Enrico Granata979e20d2011-07-29 19:53:35 +0000415 }
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000416 return sb_type;
Enrico Granata979e20d2011-07-29 19:53:35 +0000417}
418
419lldb::SBTypeList
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000420SBModule::FindTypes (const char *type)
Enrico Granata979e20d2011-07-29 19:53:35 +0000421{
422
423 SBTypeList retval;
424
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000425 if (type && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000426 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000427 SymbolContext sc;
428 TypeList type_list;
429 uint32_t num_matches = 0;
430 ConstString name(type);
Enrico Granata979e20d2011-07-29 19:53:35 +0000431
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000432 num_matches = m_opaque_sp->FindTypes(sc,
433 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000434 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000435 false,
436 UINT32_MAX,
437 type_list);
438
439 for (size_t idx = 0; idx < num_matches; idx++)
440 {
441 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
442 if (type_sp)
443 retval.Append(SBType(type_sp));
444 }
Enrico Granata979e20d2011-07-29 19:53:35 +0000445 }
446
447 return retval;
Greg Clayton153ccd72011-08-10 02:10:13 +0000448}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000449
450
451SBSection
452SBModule::FindSection (const char *sect_name)
453{
454 SBSection sb_section;
455
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000456 if (sect_name && IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000457 {
458 ObjectFile *objfile = m_opaque_sp->GetObjectFile();
459 if (objfile)
460 {
461 SectionList *section_list = objfile->GetSectionList();
462 if (section_list)
463 {
464 ConstString const_sect_name(sect_name);
465 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
466 if (section_sp)
467 {
468 sb_section.SetSection(section_sp.get());
469 }
470 }
471 }
472 }
473 return sb_section;
474}
475
Greg Clayton1b925202012-01-29 06:07:39 +0000476lldb::ByteOrder
477SBModule::GetByteOrder ()
478{
479 if (m_opaque_sp)
480 return m_opaque_sp->GetArchitecture().GetByteOrder();
481 return eByteOrderInvalid;
482}
483
484const char *
485SBModule::GetTriple ()
486{
487 if (m_opaque_sp)
488 {
489 std::string triple (m_opaque_sp->GetArchitecture().GetTriple().str());
490 // Unique the string so we don't run into ownership issues since
491 // the const strings put the string into the string pool once and
492 // the strings never comes out
493 ConstString const_triple (triple.c_str());
494 return const_triple.GetCString();
495 }
496 return NULL;
497}
498
499uint32_t
500SBModule::GetAddressByteSize()
501{
502 if (m_opaque_sp)
503 return m_opaque_sp->GetArchitecture().GetAddressByteSize();
504 return sizeof(void*);
505}
506