blob: d2ef05fde31d7145d7491c72ad02258168b8758c [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"
Greg Claytonb5a8f142012-02-05 02:38:54 +000013#include "lldb/API/SBProcess.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000014#include "lldb/API/SBStream.h"
Greg Clayton4ed315f2011-06-21 01:34:41 +000015#include "lldb/API/SBSymbolContextList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/Module.h"
Caroline Tice7826c882010-10-26 03:11:13 +000017#include "lldb/Core/Log.h"
Greg Clayton135adf22010-10-31 19:57:43 +000018#include "lldb/Core/StreamString.h"
Greg Clayton917c0002011-06-29 22:09:02 +000019#include "lldb/Core/ValueObjectList.h"
20#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata979e20d2011-07-29 19:53:35 +000021#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton917c0002011-06-29 22:09:02 +000022#include "lldb/Symbol/VariableList.h"
23#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024
25using namespace lldb;
Caroline Tice7826c882010-10-26 03:11:13 +000026using namespace lldb_private;
Chris Lattner24943d22010-06-08 16:52:24 +000027
28
29SBModule::SBModule () :
Greg Clayton63094e02010-06-23 01:19:29 +000030 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000031{
32}
33
34SBModule::SBModule (const lldb::ModuleSP& module_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000035 m_opaque_sp (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000036{
37}
38
Greg Clayton538eb822010-11-05 23:17:00 +000039SBModule::SBModule(const SBModule &rhs) :
40 m_opaque_sp (rhs.m_opaque_sp)
41{
42}
43
Greg Claytonb5a8f142012-02-05 02:38:54 +000044SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) :
45 m_opaque_sp ()
46{
47 ProcessSP process_sp (process.GetSP());
48 if (process_sp)
49 m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr);
50}
51
Greg Clayton538eb822010-11-05 23:17:00 +000052const SBModule &
53SBModule::operator = (const SBModule &rhs)
54{
55 if (this != &rhs)
56 m_opaque_sp = rhs.m_opaque_sp;
57 return *this;
58}
59
Chris Lattner24943d22010-06-08 16:52:24 +000060SBModule::~SBModule ()
61{
62}
63
64bool
65SBModule::IsValid () const
66{
Greg Clayton63094e02010-06-23 01:19:29 +000067 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
Jim Inghame0bd5712011-12-19 20:39:44 +000070void
71SBModule::Clear()
72{
73 m_opaque_sp.reset();
74}
75
Chris Lattner24943d22010-06-08 16:52:24 +000076SBFileSpec
77SBModule::GetFileSpec () const
78{
Greg Claytone005f2c2010-11-06 01:53:30 +000079 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000080
Chris Lattner24943d22010-06-08 16:52:24 +000081 SBFileSpec file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +000082 if (m_opaque_sp)
83 file_spec.SetFileSpec(m_opaque_sp->GetFileSpec());
Caroline Tice7826c882010-10-26 03:11:13 +000084
85 if (log)
86 {
Greg Clayton49ce6822010-10-31 03:01:06 +000087 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
88 m_opaque_sp.get(), file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +000089 }
90
Chris Lattner24943d22010-06-08 16:52:24 +000091 return file_spec;
92}
93
Greg Clayton180546b2011-04-30 01:09:13 +000094lldb::SBFileSpec
95SBModule::GetPlatformFileSpec () const
96{
97 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
98
99 SBFileSpec file_spec;
100 if (m_opaque_sp)
101 file_spec.SetFileSpec(m_opaque_sp->GetPlatformFileSpec());
102
103 if (log)
104 {
105 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
106 m_opaque_sp.get(), file_spec.get());
107 }
108
109 return file_spec;
110
111}
112
113bool
114SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
115{
116 bool result = false;
117 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
118
119 if (m_opaque_sp)
120 {
121 m_opaque_sp->SetPlatformFileSpec(*platform_file);
122 result = true;
123 }
124
125 if (log)
126 {
127 log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s%s%s)) => %i",
128 m_opaque_sp.get(),
129 platform_file.get(),
130 platform_file->GetDirectory().GetCString(),
131 platform_file->GetDirectory() ? "/" : "",
132 platform_file->GetFilename().GetCString(),
133 result);
134 }
135 return result;
136}
137
138
139
Chris Lattner24943d22010-06-08 16:52:24 +0000140const uint8_t *
141SBModule::GetUUIDBytes () const
142{
Greg Claytone005f2c2010-11-06 01:53:30 +0000143 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000144
Greg Claytona66ba462010-10-30 04:51:46 +0000145 const uint8_t *uuid_bytes = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000146 if (m_opaque_sp)
Greg Claytona66ba462010-10-30 04:51:46 +0000147 uuid_bytes = (const uint8_t *)m_opaque_sp->GetUUID().GetBytes();
Caroline Tice7826c882010-10-26 03:11:13 +0000148
149 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000150 {
151 if (uuid_bytes)
152 {
153 StreamString s;
154 m_opaque_sp->GetUUID().Dump (&s);
155 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", m_opaque_sp.get(), s.GetData());
156 }
157 else
158 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", m_opaque_sp.get());
159 }
160 return uuid_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000161}
162
163
Johnny Chen919ee602011-04-01 00:35:55 +0000164const char *
165SBModule::GetUUIDString () const
166{
167 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
168
169 static char uuid_string[80];
170 const char * uuid_c_string = NULL;
171 if (m_opaque_sp)
172 uuid_c_string = (const char *)m_opaque_sp->GetUUID().GetAsCString(uuid_string, sizeof(uuid_string));
173
174 if (log)
175 {
176 if (uuid_c_string)
177 {
178 StreamString s;
179 m_opaque_sp->GetUUID().Dump (&s);
180 log->Printf ("SBModule(%p)::GetUUIDString () => %s", m_opaque_sp.get(), s.GetData());
181 }
182 else
183 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", m_opaque_sp.get());
184 }
185 return uuid_c_string;
186}
187
188
Chris Lattner24943d22010-06-08 16:52:24 +0000189bool
190SBModule::operator == (const SBModule &rhs) const
191{
Greg Clayton63094e02010-06-23 01:19:29 +0000192 if (m_opaque_sp)
193 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000194 return false;
195}
196
197bool
198SBModule::operator != (const SBModule &rhs) const
199{
Greg Clayton63094e02010-06-23 01:19:29 +0000200 if (m_opaque_sp)
201 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000202 return false;
203}
204
Greg Clayton0416bdf2012-01-30 09:04:36 +0000205ModuleSP
206SBModule::GetSP () const
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000207{
208 return m_opaque_sp;
209}
Chris Lattner24943d22010-06-08 16:52:24 +0000210
211void
Greg Clayton0416bdf2012-01-30 09:04:36 +0000212SBModule::SetSP (const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000213{
Greg Clayton63094e02010-06-23 01:19:29 +0000214 m_opaque_sp = module_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000215}
216
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000217SBAddress
218SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
Greg Clayton466f6c42010-09-10 18:31:35 +0000219{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000220 lldb::SBAddress sb_addr;
221 if (m_opaque_sp)
222 {
223 Address addr;
224 if (m_opaque_sp->ResolveFileAddress (vm_addr, addr))
225 sb_addr.ref() = addr;
226 }
227 return sb_addr;
Greg Clayton466f6c42010-09-10 18:31:35 +0000228}
229
230SBSymbolContext
231SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
232{
233 SBSymbolContext sb_sc;
234 if (m_opaque_sp && addr.IsValid())
Greg Claytona3955062011-07-22 16:46:35 +0000235 m_opaque_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
Greg Clayton466f6c42010-09-10 18:31:35 +0000236 return sb_sc;
237}
238
Caroline Tice98f930f2010-09-20 05:20:02 +0000239bool
240SBModule::GetDescription (SBStream &description)
241{
Greg Clayton96154be2011-11-13 06:57:31 +0000242 Stream &strm = description.ref();
243
Caroline Tice98f930f2010-09-20 05:20:02 +0000244 if (m_opaque_sp)
245 {
Greg Clayton96154be2011-11-13 06:57:31 +0000246 m_opaque_sp->GetDescription (&strm);
Caroline Tice98f930f2010-09-20 05:20:02 +0000247 }
248 else
Greg Clayton96154be2011-11-13 06:57:31 +0000249 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000250
251 return true;
252}
Greg Clayton43edca32010-12-07 05:40:31 +0000253
254size_t
255SBModule::GetNumSymbols ()
256{
257 if (m_opaque_sp)
258 {
259 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
260 if (obj_file)
261 {
262 Symtab *symtab = obj_file->GetSymtab();
263 if (symtab)
264 return symtab->GetNumSymbols();
265 }
266 }
267 return 0;
268}
269
270SBSymbol
271SBModule::GetSymbolAtIndex (size_t idx)
272{
273 SBSymbol sb_symbol;
274 if (m_opaque_sp)
275 {
276 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
277 if (obj_file)
278 {
279 Symtab *symtab = obj_file->GetSymtab();
280 if (symtab)
281 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
282 }
283 }
284 return sb_symbol;
285}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000286
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000287size_t
288SBModule::GetNumSections ()
289{
290 if (m_opaque_sp)
291 {
292 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
293 if (obj_file)
294 {
295 SectionList *section_list = obj_file->GetSectionList ();
296 if (section_list)
297 return section_list->GetSize();
298 }
299 }
300 return 0;
301}
302
303SBSection
304SBModule::GetSectionAtIndex (size_t idx)
305{
306 SBSection sb_section;
307 if (m_opaque_sp)
308 {
309 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
310 if (obj_file)
311 {
312 SectionList *section_list = obj_file->GetSectionList ();
313
314 if (section_list)
315 sb_section.SetSection(section_list->GetSectionAtIndex (idx).get());
316 }
317 }
318 return sb_section;
319}
320
Greg Clayton4ed315f2011-06-21 01:34:41 +0000321uint32_t
322SBModule::FindFunctions (const char *name,
323 uint32_t name_type_mask,
324 bool append,
325 lldb::SBSymbolContextList& sc_list)
326{
327 if (!append)
328 sc_list.Clear();
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000329 if (name && m_opaque_sp)
Greg Clayton4ed315f2011-06-21 01:34:41 +0000330 {
331 const bool symbols_ok = true;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000332 return m_opaque_sp->FindFunctions (ConstString(name),
333 NULL,
Greg Clayton4ed315f2011-06-21 01:34:41 +0000334 name_type_mask,
335 symbols_ok,
336 append,
337 *sc_list);
338 }
339 return 0;
340}
341
Greg Clayton917c0002011-06-29 22:09:02 +0000342
343SBValueList
344SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
345{
346 SBValueList sb_value_list;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000347 if (name && m_opaque_sp)
Greg Clayton917c0002011-06-29 22:09:02 +0000348 {
349 VariableList variable_list;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000350 const uint32_t match_count = m_opaque_sp->FindGlobalVariables (ConstString (name),
351 NULL,
Greg Clayton917c0002011-06-29 22:09:02 +0000352 false,
353 max_matches,
354 variable_list);
355
356 if (match_count > 0)
357 {
358 ValueObjectList &value_object_list = sb_value_list.ref();
359 for (uint32_t i=0; i<match_count; ++i)
360 {
361 lldb::ValueObjectSP valobj_sp;
Greg Clayton334d33a2012-01-30 07:41:31 +0000362 TargetSP target_sp (target.GetSP());
363 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
Greg Clayton917c0002011-06-29 22:09:02 +0000364 if (valobj_sp)
365 value_object_list.Append(valobj_sp);
366 }
367 }
368 }
369
370 return sb_value_list;
371}
Enrico Granata979e20d2011-07-29 19:53:35 +0000372
373lldb::SBType
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000374SBModule::FindFirstType (const char *name_cstr)
Enrico Granata979e20d2011-07-29 19:53:35 +0000375{
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000376 SBType sb_type;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000377 if (name_cstr && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000378 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000379 SymbolContext sc;
380 TypeList type_list;
381 uint32_t num_matches = 0;
382 ConstString name(name_cstr);
383
384 num_matches = m_opaque_sp->FindTypes(sc,
385 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000386 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000387 false,
388 1,
389 type_list);
390
391 if (num_matches)
392 sb_type = lldb::SBType(type_list.GetTypeAtIndex(0));
Enrico Granata979e20d2011-07-29 19:53:35 +0000393 }
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000394 return sb_type;
Enrico Granata979e20d2011-07-29 19:53:35 +0000395}
396
397lldb::SBTypeList
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000398SBModule::FindTypes (const char *type)
Enrico Granata979e20d2011-07-29 19:53:35 +0000399{
400
401 SBTypeList retval;
402
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000403 if (type && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000404 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000405 SymbolContext sc;
406 TypeList type_list;
407 uint32_t num_matches = 0;
408 ConstString name(type);
Enrico Granata979e20d2011-07-29 19:53:35 +0000409
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000410 num_matches = m_opaque_sp->FindTypes(sc,
411 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000412 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000413 false,
414 UINT32_MAX,
415 type_list);
416
417 for (size_t idx = 0; idx < num_matches; idx++)
418 {
419 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
420 if (type_sp)
421 retval.Append(SBType(type_sp));
422 }
Enrico Granata979e20d2011-07-29 19:53:35 +0000423 }
424
425 return retval;
Greg Clayton153ccd72011-08-10 02:10:13 +0000426}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000427
428
429SBSection
430SBModule::FindSection (const char *sect_name)
431{
432 SBSection sb_section;
433
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000434 if (sect_name && IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000435 {
436 ObjectFile *objfile = m_opaque_sp->GetObjectFile();
437 if (objfile)
438 {
439 SectionList *section_list = objfile->GetSectionList();
440 if (section_list)
441 {
442 ConstString const_sect_name(sect_name);
443 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
444 if (section_sp)
445 {
446 sb_section.SetSection(section_sp.get());
447 }
448 }
449 }
450 }
451 return sb_section;
452}
453
Greg Clayton1b925202012-01-29 06:07:39 +0000454lldb::ByteOrder
455SBModule::GetByteOrder ()
456{
457 if (m_opaque_sp)
458 return m_opaque_sp->GetArchitecture().GetByteOrder();
459 return eByteOrderInvalid;
460}
461
462const char *
463SBModule::GetTriple ()
464{
465 if (m_opaque_sp)
466 {
467 std::string triple (m_opaque_sp->GetArchitecture().GetTriple().str());
468 // Unique the string so we don't run into ownership issues since
469 // the const strings put the string into the string pool once and
470 // the strings never comes out
471 ConstString const_triple (triple.c_str());
472 return const_triple.GetCString();
473 }
474 return NULL;
475}
476
477uint32_t
478SBModule::GetAddressByteSize()
479{
480 if (m_opaque_sp)
481 return m_opaque_sp->GetArchitecture().GetAddressByteSize();
482 return sizeof(void*);
483}
484