blob: 6200cc6c8393182db42a5942653c4522008fbb28 [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
Greg Clayton0416bdf2012-01-30 09:04:36 +0000196ModuleSP
197SBModule::GetSP () const
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000198{
199 return m_opaque_sp;
200}
Chris Lattner24943d22010-06-08 16:52:24 +0000201
202void
Greg Clayton0416bdf2012-01-30 09:04:36 +0000203SBModule::SetSP (const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000204{
Greg Clayton63094e02010-06-23 01:19:29 +0000205 m_opaque_sp = module_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000206}
207
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000208SBAddress
209SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
Greg Clayton466f6c42010-09-10 18:31:35 +0000210{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000211 lldb::SBAddress sb_addr;
212 if (m_opaque_sp)
213 {
214 Address addr;
215 if (m_opaque_sp->ResolveFileAddress (vm_addr, addr))
216 sb_addr.ref() = addr;
217 }
218 return sb_addr;
Greg Clayton466f6c42010-09-10 18:31:35 +0000219}
220
221SBSymbolContext
222SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
223{
224 SBSymbolContext sb_sc;
225 if (m_opaque_sp && addr.IsValid())
Greg Claytona3955062011-07-22 16:46:35 +0000226 m_opaque_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
Greg Clayton466f6c42010-09-10 18:31:35 +0000227 return sb_sc;
228}
229
Caroline Tice98f930f2010-09-20 05:20:02 +0000230bool
231SBModule::GetDescription (SBStream &description)
232{
Greg Clayton96154be2011-11-13 06:57:31 +0000233 Stream &strm = description.ref();
234
Caroline Tice98f930f2010-09-20 05:20:02 +0000235 if (m_opaque_sp)
236 {
Greg Clayton96154be2011-11-13 06:57:31 +0000237 m_opaque_sp->GetDescription (&strm);
Caroline Tice98f930f2010-09-20 05:20:02 +0000238 }
239 else
Greg Clayton96154be2011-11-13 06:57:31 +0000240 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000241
242 return true;
243}
Greg Clayton43edca32010-12-07 05:40:31 +0000244
245size_t
246SBModule::GetNumSymbols ()
247{
248 if (m_opaque_sp)
249 {
250 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
251 if (obj_file)
252 {
253 Symtab *symtab = obj_file->GetSymtab();
254 if (symtab)
255 return symtab->GetNumSymbols();
256 }
257 }
258 return 0;
259}
260
261SBSymbol
262SBModule::GetSymbolAtIndex (size_t idx)
263{
264 SBSymbol sb_symbol;
265 if (m_opaque_sp)
266 {
267 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
268 if (obj_file)
269 {
270 Symtab *symtab = obj_file->GetSymtab();
271 if (symtab)
272 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
273 }
274 }
275 return sb_symbol;
276}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000277
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000278size_t
279SBModule::GetNumSections ()
280{
281 if (m_opaque_sp)
282 {
283 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
284 if (obj_file)
285 {
286 SectionList *section_list = obj_file->GetSectionList ();
287 if (section_list)
288 return section_list->GetSize();
289 }
290 }
291 return 0;
292}
293
294SBSection
295SBModule::GetSectionAtIndex (size_t idx)
296{
297 SBSection sb_section;
298 if (m_opaque_sp)
299 {
300 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
301 if (obj_file)
302 {
303 SectionList *section_list = obj_file->GetSectionList ();
304
305 if (section_list)
306 sb_section.SetSection(section_list->GetSectionAtIndex (idx).get());
307 }
308 }
309 return sb_section;
310}
311
Greg Clayton4ed315f2011-06-21 01:34:41 +0000312uint32_t
313SBModule::FindFunctions (const char *name,
314 uint32_t name_type_mask,
315 bool append,
316 lldb::SBSymbolContextList& sc_list)
317{
318 if (!append)
319 sc_list.Clear();
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000320 if (name && m_opaque_sp)
Greg Clayton4ed315f2011-06-21 01:34:41 +0000321 {
322 const bool symbols_ok = true;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000323 return m_opaque_sp->FindFunctions (ConstString(name),
324 NULL,
Greg Clayton4ed315f2011-06-21 01:34:41 +0000325 name_type_mask,
326 symbols_ok,
327 append,
328 *sc_list);
329 }
330 return 0;
331}
332
Greg Clayton917c0002011-06-29 22:09:02 +0000333
334SBValueList
335SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
336{
337 SBValueList sb_value_list;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000338 if (name && m_opaque_sp)
Greg Clayton917c0002011-06-29 22:09:02 +0000339 {
340 VariableList variable_list;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000341 const uint32_t match_count = m_opaque_sp->FindGlobalVariables (ConstString (name),
342 NULL,
Greg Clayton917c0002011-06-29 22:09:02 +0000343 false,
344 max_matches,
345 variable_list);
346
347 if (match_count > 0)
348 {
349 ValueObjectList &value_object_list = sb_value_list.ref();
350 for (uint32_t i=0; i<match_count; ++i)
351 {
352 lldb::ValueObjectSP valobj_sp;
Greg Clayton334d33a2012-01-30 07:41:31 +0000353 TargetSP target_sp (target.GetSP());
354 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
Greg Clayton917c0002011-06-29 22:09:02 +0000355 if (valobj_sp)
356 value_object_list.Append(valobj_sp);
357 }
358 }
359 }
360
361 return sb_value_list;
362}
Enrico Granata979e20d2011-07-29 19:53:35 +0000363
364lldb::SBType
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000365SBModule::FindFirstType (const char *name_cstr)
Enrico Granata979e20d2011-07-29 19:53:35 +0000366{
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000367 SBType sb_type;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000368 if (name_cstr && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000369 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000370 SymbolContext sc;
371 TypeList type_list;
372 uint32_t num_matches = 0;
373 ConstString name(name_cstr);
374
375 num_matches = m_opaque_sp->FindTypes(sc,
376 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000377 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000378 false,
379 1,
380 type_list);
381
382 if (num_matches)
383 sb_type = lldb::SBType(type_list.GetTypeAtIndex(0));
Enrico Granata979e20d2011-07-29 19:53:35 +0000384 }
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000385 return sb_type;
Enrico Granata979e20d2011-07-29 19:53:35 +0000386}
387
388lldb::SBTypeList
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000389SBModule::FindTypes (const char *type)
Enrico Granata979e20d2011-07-29 19:53:35 +0000390{
391
392 SBTypeList retval;
393
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000394 if (type && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000395 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000396 SymbolContext sc;
397 TypeList type_list;
398 uint32_t num_matches = 0;
399 ConstString name(type);
Enrico Granata979e20d2011-07-29 19:53:35 +0000400
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000401 num_matches = m_opaque_sp->FindTypes(sc,
402 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000403 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000404 false,
405 UINT32_MAX,
406 type_list);
407
408 for (size_t idx = 0; idx < num_matches; idx++)
409 {
410 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
411 if (type_sp)
412 retval.Append(SBType(type_sp));
413 }
Enrico Granata979e20d2011-07-29 19:53:35 +0000414 }
415
416 return retval;
Greg Clayton153ccd72011-08-10 02:10:13 +0000417}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000418
419
420SBSection
421SBModule::FindSection (const char *sect_name)
422{
423 SBSection sb_section;
424
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000425 if (sect_name && IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000426 {
427 ObjectFile *objfile = m_opaque_sp->GetObjectFile();
428 if (objfile)
429 {
430 SectionList *section_list = objfile->GetSectionList();
431 if (section_list)
432 {
433 ConstString const_sect_name(sect_name);
434 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
435 if (section_sp)
436 {
437 sb_section.SetSection(section_sp.get());
438 }
439 }
440 }
441 }
442 return sb_section;
443}
444
Greg Clayton1b925202012-01-29 06:07:39 +0000445lldb::ByteOrder
446SBModule::GetByteOrder ()
447{
448 if (m_opaque_sp)
449 return m_opaque_sp->GetArchitecture().GetByteOrder();
450 return eByteOrderInvalid;
451}
452
453const char *
454SBModule::GetTriple ()
455{
456 if (m_opaque_sp)
457 {
458 std::string triple (m_opaque_sp->GetArchitecture().GetTriple().str());
459 // Unique the string so we don't run into ownership issues since
460 // the const strings put the string into the string pool once and
461 // the strings never comes out
462 ConstString const_triple (triple.c_str());
463 return const_triple.GetCString();
464 }
465 return NULL;
466}
467
468uint32_t
469SBModule::GetAddressByteSize()
470{
471 if (m_opaque_sp)
472 return m_opaque_sp->GetArchitecture().GetAddressByteSize();
473 return sizeof(void*);
474}
475