blob: a42989757e1837c22f02f05ffc6321c1bd0d4348 [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
61SBFileSpec
62SBModule::GetFileSpec () const
63{
Greg Claytone005f2c2010-11-06 01:53:30 +000064 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000065
Chris Lattner24943d22010-06-08 16:52:24 +000066 SBFileSpec file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +000067 if (m_opaque_sp)
68 file_spec.SetFileSpec(m_opaque_sp->GetFileSpec());
Caroline Tice7826c882010-10-26 03:11:13 +000069
70 if (log)
71 {
Greg Clayton49ce6822010-10-31 03:01:06 +000072 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
73 m_opaque_sp.get(), file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +000074 }
75
Chris Lattner24943d22010-06-08 16:52:24 +000076 return file_spec;
77}
78
Greg Clayton180546b2011-04-30 01:09:13 +000079lldb::SBFileSpec
80SBModule::GetPlatformFileSpec () const
81{
82 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
83
84 SBFileSpec file_spec;
85 if (m_opaque_sp)
86 file_spec.SetFileSpec(m_opaque_sp->GetPlatformFileSpec());
87
88 if (log)
89 {
90 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
91 m_opaque_sp.get(), file_spec.get());
92 }
93
94 return file_spec;
95
96}
97
98bool
99SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
100{
101 bool result = false;
102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
103
104 if (m_opaque_sp)
105 {
106 m_opaque_sp->SetPlatformFileSpec(*platform_file);
107 result = true;
108 }
109
110 if (log)
111 {
112 log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s%s%s)) => %i",
113 m_opaque_sp.get(),
114 platform_file.get(),
115 platform_file->GetDirectory().GetCString(),
116 platform_file->GetDirectory() ? "/" : "",
117 platform_file->GetFilename().GetCString(),
118 result);
119 }
120 return result;
121}
122
123
124
Chris Lattner24943d22010-06-08 16:52:24 +0000125const uint8_t *
126SBModule::GetUUIDBytes () const
127{
Greg Claytone005f2c2010-11-06 01:53:30 +0000128 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000129
Greg Claytona66ba462010-10-30 04:51:46 +0000130 const uint8_t *uuid_bytes = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000131 if (m_opaque_sp)
Greg Claytona66ba462010-10-30 04:51:46 +0000132 uuid_bytes = (const uint8_t *)m_opaque_sp->GetUUID().GetBytes();
Caroline Tice7826c882010-10-26 03:11:13 +0000133
134 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000135 {
136 if (uuid_bytes)
137 {
138 StreamString s;
139 m_opaque_sp->GetUUID().Dump (&s);
140 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", m_opaque_sp.get(), s.GetData());
141 }
142 else
143 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", m_opaque_sp.get());
144 }
145 return uuid_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000146}
147
148
Johnny Chen919ee602011-04-01 00:35:55 +0000149const char *
150SBModule::GetUUIDString () const
151{
152 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
153
154 static char uuid_string[80];
155 const char * uuid_c_string = NULL;
156 if (m_opaque_sp)
157 uuid_c_string = (const char *)m_opaque_sp->GetUUID().GetAsCString(uuid_string, sizeof(uuid_string));
158
159 if (log)
160 {
161 if (uuid_c_string)
162 {
163 StreamString s;
164 m_opaque_sp->GetUUID().Dump (&s);
165 log->Printf ("SBModule(%p)::GetUUIDString () => %s", m_opaque_sp.get(), s.GetData());
166 }
167 else
168 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", m_opaque_sp.get());
169 }
170 return uuid_c_string;
171}
172
173
Chris Lattner24943d22010-06-08 16:52:24 +0000174bool
175SBModule::operator == (const SBModule &rhs) const
176{
Greg Clayton63094e02010-06-23 01:19:29 +0000177 if (m_opaque_sp)
178 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000179 return false;
180}
181
182bool
183SBModule::operator != (const SBModule &rhs) const
184{
Greg Clayton63094e02010-06-23 01:19:29 +0000185 if (m_opaque_sp)
186 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000187 return false;
188}
189
190lldb::ModuleSP &
191SBModule::operator *()
192{
Greg Clayton63094e02010-06-23 01:19:29 +0000193 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000194}
195
196lldb_private::Module *
197SBModule::operator ->()
198{
Greg Clayton63094e02010-06-23 01:19:29 +0000199 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000200}
201
202const lldb_private::Module *
203SBModule::operator ->() const
204{
Greg Clayton63094e02010-06-23 01:19:29 +0000205 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000206}
207
208lldb_private::Module *
209SBModule::get()
210{
Greg Clayton63094e02010-06-23 01:19:29 +0000211 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000212}
213
214const lldb_private::Module *
215SBModule::get() const
216{
Greg Clayton63094e02010-06-23 01:19:29 +0000217 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000218}
219
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000220const lldb::ModuleSP &
221SBModule::get_sp() const
222{
223 return m_opaque_sp;
224}
Chris Lattner24943d22010-06-08 16:52:24 +0000225
226void
227SBModule::SetModule (const lldb::ModuleSP& module_sp)
228{
Greg Clayton63094e02010-06-23 01:19:29 +0000229 m_opaque_sp = module_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000230}
231
Greg Clayton466f6c42010-09-10 18:31:35 +0000232
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000233SBAddress
234SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
Greg Clayton466f6c42010-09-10 18:31:35 +0000235{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000236 lldb::SBAddress sb_addr;
237 if (m_opaque_sp)
238 {
239 Address addr;
240 if (m_opaque_sp->ResolveFileAddress (vm_addr, addr))
241 sb_addr.ref() = addr;
242 }
243 return sb_addr;
Greg Clayton466f6c42010-09-10 18:31:35 +0000244}
245
246SBSymbolContext
247SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
248{
249 SBSymbolContext sb_sc;
250 if (m_opaque_sp && addr.IsValid())
Greg Claytona3955062011-07-22 16:46:35 +0000251 m_opaque_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
Greg Clayton466f6c42010-09-10 18:31:35 +0000252 return sb_sc;
253}
254
Caroline Tice98f930f2010-09-20 05:20:02 +0000255bool
256SBModule::GetDescription (SBStream &description)
257{
Greg Clayton96154be2011-11-13 06:57:31 +0000258 Stream &strm = description.ref();
259
Caroline Tice98f930f2010-09-20 05:20:02 +0000260 if (m_opaque_sp)
261 {
Greg Clayton96154be2011-11-13 06:57:31 +0000262 m_opaque_sp->GetDescription (&strm);
Caroline Tice98f930f2010-09-20 05:20:02 +0000263 }
264 else
Greg Clayton96154be2011-11-13 06:57:31 +0000265 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000266
267 return true;
268}
Greg Clayton43edca32010-12-07 05:40:31 +0000269
270size_t
271SBModule::GetNumSymbols ()
272{
273 if (m_opaque_sp)
274 {
275 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
276 if (obj_file)
277 {
278 Symtab *symtab = obj_file->GetSymtab();
279 if (symtab)
280 return symtab->GetNumSymbols();
281 }
282 }
283 return 0;
284}
285
286SBSymbol
287SBModule::GetSymbolAtIndex (size_t idx)
288{
289 SBSymbol sb_symbol;
290 if (m_opaque_sp)
291 {
292 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
293 if (obj_file)
294 {
295 Symtab *symtab = obj_file->GetSymtab();
296 if (symtab)
297 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
298 }
299 }
300 return sb_symbol;
301}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000302
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000303size_t
304SBModule::GetNumSections ()
305{
306 if (m_opaque_sp)
307 {
308 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
309 if (obj_file)
310 {
311 SectionList *section_list = obj_file->GetSectionList ();
312 if (section_list)
313 return section_list->GetSize();
314 }
315 }
316 return 0;
317}
318
319SBSection
320SBModule::GetSectionAtIndex (size_t idx)
321{
322 SBSection sb_section;
323 if (m_opaque_sp)
324 {
325 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
326 if (obj_file)
327 {
328 SectionList *section_list = obj_file->GetSectionList ();
329
330 if (section_list)
331 sb_section.SetSection(section_list->GetSectionAtIndex (idx).get());
332 }
333 }
334 return sb_section;
335}
336
Greg Clayton4ed315f2011-06-21 01:34:41 +0000337uint32_t
338SBModule::FindFunctions (const char *name,
339 uint32_t name_type_mask,
340 bool append,
341 lldb::SBSymbolContextList& sc_list)
342{
343 if (!append)
344 sc_list.Clear();
345 if (m_opaque_sp)
346 {
347 const bool symbols_ok = true;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000348 return m_opaque_sp->FindFunctions (ConstString(name),
349 NULL,
Greg Clayton4ed315f2011-06-21 01:34:41 +0000350 name_type_mask,
351 symbols_ok,
352 append,
353 *sc_list);
354 }
355 return 0;
356}
357
Greg Clayton917c0002011-06-29 22:09:02 +0000358
359SBValueList
360SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
361{
362 SBValueList sb_value_list;
363 if (m_opaque_sp)
364 {
365 VariableList variable_list;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000366 const uint32_t match_count = m_opaque_sp->FindGlobalVariables (ConstString (name),
367 NULL,
Greg Clayton917c0002011-06-29 22:09:02 +0000368 false,
369 max_matches,
370 variable_list);
371
372 if (match_count > 0)
373 {
374 ValueObjectList &value_object_list = sb_value_list.ref();
375 for (uint32_t i=0; i<match_count; ++i)
376 {
377 lldb::ValueObjectSP valobj_sp;
378 if (target.IsValid())
379 valobj_sp = ValueObjectVariable::Create (target.get(), variable_list.GetVariableAtIndex(i));
380 else
381 valobj_sp = ValueObjectVariable::Create (NULL, variable_list.GetVariableAtIndex(i));
382 if (valobj_sp)
383 value_object_list.Append(valobj_sp);
384 }
385 }
386 }
387
388 return sb_value_list;
389}
Enrico Granata979e20d2011-07-29 19:53:35 +0000390
391lldb::SBType
392SBModule::FindFirstType (const char* name_cstr)
393{
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000394 SBType sb_type;
395 if (IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000396 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000397 SymbolContext sc;
398 TypeList type_list;
399 uint32_t num_matches = 0;
400 ConstString name(name_cstr);
401
402 num_matches = m_opaque_sp->FindTypes(sc,
403 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000404 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000405 false,
406 1,
407 type_list);
408
409 if (num_matches)
410 sb_type = lldb::SBType(type_list.GetTypeAtIndex(0));
Enrico Granata979e20d2011-07-29 19:53:35 +0000411 }
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000412 return sb_type;
Enrico Granata979e20d2011-07-29 19:53:35 +0000413}
414
415lldb::SBTypeList
416SBModule::FindTypes (const char* type)
417{
418
419 SBTypeList retval;
420
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000421 if (IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000422 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000423 SymbolContext sc;
424 TypeList type_list;
425 uint32_t num_matches = 0;
426 ConstString name(type);
Enrico Granata979e20d2011-07-29 19:53:35 +0000427
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000428 num_matches = m_opaque_sp->FindTypes(sc,
429 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000430 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000431 false,
432 UINT32_MAX,
433 type_list);
434
435 for (size_t idx = 0; idx < num_matches; idx++)
436 {
437 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
438 if (type_sp)
439 retval.Append(SBType(type_sp));
440 }
Enrico Granata979e20d2011-07-29 19:53:35 +0000441 }
442
443 return retval;
Greg Clayton153ccd72011-08-10 02:10:13 +0000444}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000445
446
447SBSection
448SBModule::FindSection (const char *sect_name)
449{
450 SBSection sb_section;
451
452 if (IsValid())
453 {
454 ObjectFile *objfile = m_opaque_sp->GetObjectFile();
455 if (objfile)
456 {
457 SectionList *section_list = objfile->GetSectionList();
458 if (section_list)
459 {
460 ConstString const_sect_name(sect_name);
461 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
462 if (section_sp)
463 {
464 sb_section.SetSection(section_sp.get());
465 }
466 }
467 }
468 }
469 return sb_section;
470}
471