blob: 09f354e27106becc3cb1a3680bae3259fdfdb540 [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)
Greg Clayton9ce95382012-02-13 23:10:39 +000049 {
50 const bool add_image_to_target = true;
51 const bool load_image_sections_in_target = true;
52 m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(),
53 header_addr,
54 add_image_to_target,
55 load_image_sections_in_target);
56 }
Greg Claytonb5a8f142012-02-05 02:38:54 +000057}
58
Greg Clayton538eb822010-11-05 23:17:00 +000059const SBModule &
60SBModule::operator = (const SBModule &rhs)
61{
62 if (this != &rhs)
63 m_opaque_sp = rhs.m_opaque_sp;
64 return *this;
65}
66
Chris Lattner24943d22010-06-08 16:52:24 +000067SBModule::~SBModule ()
68{
69}
70
71bool
72SBModule::IsValid () const
73{
Greg Clayton63094e02010-06-23 01:19:29 +000074 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000075}
76
Jim Inghame0bd5712011-12-19 20:39:44 +000077void
78SBModule::Clear()
79{
80 m_opaque_sp.reset();
81}
82
Chris Lattner24943d22010-06-08 16:52:24 +000083SBFileSpec
84SBModule::GetFileSpec () const
85{
Greg Claytone005f2c2010-11-06 01:53:30 +000086 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000087
Chris Lattner24943d22010-06-08 16:52:24 +000088 SBFileSpec file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +000089 if (m_opaque_sp)
90 file_spec.SetFileSpec(m_opaque_sp->GetFileSpec());
Caroline Tice7826c882010-10-26 03:11:13 +000091
92 if (log)
93 {
Greg Clayton49ce6822010-10-31 03:01:06 +000094 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
95 m_opaque_sp.get(), file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +000096 }
97
Chris Lattner24943d22010-06-08 16:52:24 +000098 return file_spec;
99}
100
Greg Clayton180546b2011-04-30 01:09:13 +0000101lldb::SBFileSpec
102SBModule::GetPlatformFileSpec () const
103{
104 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
105
106 SBFileSpec file_spec;
107 if (m_opaque_sp)
108 file_spec.SetFileSpec(m_opaque_sp->GetPlatformFileSpec());
109
110 if (log)
111 {
112 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
113 m_opaque_sp.get(), file_spec.get());
114 }
115
116 return file_spec;
117
118}
119
120bool
121SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
122{
123 bool result = false;
124 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
125
126 if (m_opaque_sp)
127 {
128 m_opaque_sp->SetPlatformFileSpec(*platform_file);
129 result = true;
130 }
131
132 if (log)
133 {
134 log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s%s%s)) => %i",
135 m_opaque_sp.get(),
136 platform_file.get(),
137 platform_file->GetDirectory().GetCString(),
138 platform_file->GetDirectory() ? "/" : "",
139 platform_file->GetFilename().GetCString(),
140 result);
141 }
142 return result;
143}
144
145
146
Chris Lattner24943d22010-06-08 16:52:24 +0000147const uint8_t *
148SBModule::GetUUIDBytes () const
149{
Greg Claytone005f2c2010-11-06 01:53:30 +0000150 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000151
Greg Claytona66ba462010-10-30 04:51:46 +0000152 const uint8_t *uuid_bytes = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000153 if (m_opaque_sp)
Greg Claytona66ba462010-10-30 04:51:46 +0000154 uuid_bytes = (const uint8_t *)m_opaque_sp->GetUUID().GetBytes();
Caroline Tice7826c882010-10-26 03:11:13 +0000155
156 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000157 {
158 if (uuid_bytes)
159 {
160 StreamString s;
161 m_opaque_sp->GetUUID().Dump (&s);
162 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", m_opaque_sp.get(), s.GetData());
163 }
164 else
165 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", m_opaque_sp.get());
166 }
167 return uuid_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000168}
169
170
Johnny Chen919ee602011-04-01 00:35:55 +0000171const char *
172SBModule::GetUUIDString () const
173{
174 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
175
176 static char uuid_string[80];
177 const char * uuid_c_string = NULL;
178 if (m_opaque_sp)
179 uuid_c_string = (const char *)m_opaque_sp->GetUUID().GetAsCString(uuid_string, sizeof(uuid_string));
180
181 if (log)
182 {
183 if (uuid_c_string)
184 {
185 StreamString s;
186 m_opaque_sp->GetUUID().Dump (&s);
187 log->Printf ("SBModule(%p)::GetUUIDString () => %s", m_opaque_sp.get(), s.GetData());
188 }
189 else
190 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", m_opaque_sp.get());
191 }
192 return uuid_c_string;
193}
194
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196bool
197SBModule::operator == (const SBModule &rhs) const
198{
Greg Clayton63094e02010-06-23 01:19:29 +0000199 if (m_opaque_sp)
200 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000201 return false;
202}
203
204bool
205SBModule::operator != (const SBModule &rhs) const
206{
Greg Clayton63094e02010-06-23 01:19:29 +0000207 if (m_opaque_sp)
208 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000209 return false;
210}
211
Greg Clayton0416bdf2012-01-30 09:04:36 +0000212ModuleSP
213SBModule::GetSP () const
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000214{
215 return m_opaque_sp;
216}
Chris Lattner24943d22010-06-08 16:52:24 +0000217
218void
Greg Clayton0416bdf2012-01-30 09:04:36 +0000219SBModule::SetSP (const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000220{
Greg Clayton63094e02010-06-23 01:19:29 +0000221 m_opaque_sp = module_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000222}
223
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000224SBAddress
225SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
Greg Clayton466f6c42010-09-10 18:31:35 +0000226{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000227 lldb::SBAddress sb_addr;
228 if (m_opaque_sp)
229 {
230 Address addr;
231 if (m_opaque_sp->ResolveFileAddress (vm_addr, addr))
232 sb_addr.ref() = addr;
233 }
234 return sb_addr;
Greg Clayton466f6c42010-09-10 18:31:35 +0000235}
236
237SBSymbolContext
238SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
239{
240 SBSymbolContext sb_sc;
241 if (m_opaque_sp && addr.IsValid())
Greg Claytona3955062011-07-22 16:46:35 +0000242 m_opaque_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
Greg Clayton466f6c42010-09-10 18:31:35 +0000243 return sb_sc;
244}
245
Caroline Tice98f930f2010-09-20 05:20:02 +0000246bool
247SBModule::GetDescription (SBStream &description)
248{
Greg Clayton96154be2011-11-13 06:57:31 +0000249 Stream &strm = description.ref();
250
Caroline Tice98f930f2010-09-20 05:20:02 +0000251 if (m_opaque_sp)
252 {
Greg Clayton96154be2011-11-13 06:57:31 +0000253 m_opaque_sp->GetDescription (&strm);
Caroline Tice98f930f2010-09-20 05:20:02 +0000254 }
255 else
Greg Clayton96154be2011-11-13 06:57:31 +0000256 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000257
258 return true;
259}
Greg Clayton43edca32010-12-07 05:40:31 +0000260
261size_t
262SBModule::GetNumSymbols ()
263{
264 if (m_opaque_sp)
265 {
266 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
267 if (obj_file)
268 {
269 Symtab *symtab = obj_file->GetSymtab();
270 if (symtab)
271 return symtab->GetNumSymbols();
272 }
273 }
274 return 0;
275}
276
277SBSymbol
278SBModule::GetSymbolAtIndex (size_t idx)
279{
280 SBSymbol sb_symbol;
281 if (m_opaque_sp)
282 {
283 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
284 if (obj_file)
285 {
286 Symtab *symtab = obj_file->GetSymtab();
287 if (symtab)
288 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
289 }
290 }
291 return sb_symbol;
292}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000293
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000294size_t
295SBModule::GetNumSections ()
296{
297 if (m_opaque_sp)
298 {
299 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
300 if (obj_file)
301 {
302 SectionList *section_list = obj_file->GetSectionList ();
303 if (section_list)
304 return section_list->GetSize();
305 }
306 }
307 return 0;
308}
309
310SBSection
311SBModule::GetSectionAtIndex (size_t idx)
312{
313 SBSection sb_section;
314 if (m_opaque_sp)
315 {
316 ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
317 if (obj_file)
318 {
319 SectionList *section_list = obj_file->GetSectionList ();
320
321 if (section_list)
322 sb_section.SetSection(section_list->GetSectionAtIndex (idx).get());
323 }
324 }
325 return sb_section;
326}
327
Greg Clayton7dd5c512012-02-06 01:44:54 +0000328lldb::SBSymbolContextList
Greg Clayton4ed315f2011-06-21 01:34:41 +0000329SBModule::FindFunctions (const char *name,
Greg Clayton7dd5c512012-02-06 01:44:54 +0000330 uint32_t name_type_mask)
Greg Clayton4ed315f2011-06-21 01:34:41 +0000331{
Greg Clayton7dd5c512012-02-06 01:44:54 +0000332 lldb::SBSymbolContextList sb_sc_list;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000333 if (name && m_opaque_sp)
Greg Clayton4ed315f2011-06-21 01:34:41 +0000334 {
Greg Clayton7dd5c512012-02-06 01:44:54 +0000335 const bool append = true;
Greg Clayton4ed315f2011-06-21 01:34:41 +0000336 const bool symbols_ok = true;
Sean Callanan302d78c2012-02-10 22:52:19 +0000337 const bool inlines_ok = true;
Greg Clayton7dd5c512012-02-06 01:44:54 +0000338 m_opaque_sp->FindFunctions (ConstString(name),
339 NULL,
340 name_type_mask,
Sean Callanan302d78c2012-02-10 22:52:19 +0000341 symbols_ok,
342 inlines_ok,
Greg Clayton7dd5c512012-02-06 01:44:54 +0000343 append,
344 *sb_sc_list);
Greg Clayton4ed315f2011-06-21 01:34:41 +0000345 }
Greg Clayton7dd5c512012-02-06 01:44:54 +0000346 return sb_sc_list;
Greg Clayton4ed315f2011-06-21 01:34:41 +0000347}
348
Greg Clayton917c0002011-06-29 22:09:02 +0000349
350SBValueList
351SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
352{
353 SBValueList sb_value_list;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000354 if (name && m_opaque_sp)
Greg Clayton917c0002011-06-29 22:09:02 +0000355 {
356 VariableList variable_list;
Sean Callanan3e80cd92011-10-12 02:08:07 +0000357 const uint32_t match_count = m_opaque_sp->FindGlobalVariables (ConstString (name),
358 NULL,
Greg Clayton917c0002011-06-29 22:09:02 +0000359 false,
360 max_matches,
361 variable_list);
362
363 if (match_count > 0)
364 {
365 ValueObjectList &value_object_list = sb_value_list.ref();
366 for (uint32_t i=0; i<match_count; ++i)
367 {
368 lldb::ValueObjectSP valobj_sp;
Greg Clayton334d33a2012-01-30 07:41:31 +0000369 TargetSP target_sp (target.GetSP());
370 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
Greg Clayton917c0002011-06-29 22:09:02 +0000371 if (valobj_sp)
372 value_object_list.Append(valobj_sp);
373 }
374 }
375 }
376
377 return sb_value_list;
378}
Enrico Granata979e20d2011-07-29 19:53:35 +0000379
380lldb::SBType
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000381SBModule::FindFirstType (const char *name_cstr)
Enrico Granata979e20d2011-07-29 19:53:35 +0000382{
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000383 SBType sb_type;
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000384 if (name_cstr && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000385 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000386 SymbolContext sc;
387 TypeList type_list;
388 uint32_t num_matches = 0;
389 ConstString name(name_cstr);
390
391 num_matches = m_opaque_sp->FindTypes(sc,
392 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000393 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000394 false,
395 1,
396 type_list);
397
398 if (num_matches)
399 sb_type = lldb::SBType(type_list.GetTypeAtIndex(0));
Enrico Granata979e20d2011-07-29 19:53:35 +0000400 }
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000401 return sb_type;
Enrico Granata979e20d2011-07-29 19:53:35 +0000402}
403
404lldb::SBTypeList
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000405SBModule::FindTypes (const char *type)
Enrico Granata979e20d2011-07-29 19:53:35 +0000406{
407
408 SBTypeList retval;
409
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000410 if (type && IsValid())
Enrico Granata979e20d2011-07-29 19:53:35 +0000411 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000412 SymbolContext sc;
413 TypeList type_list;
414 uint32_t num_matches = 0;
415 ConstString name(type);
Enrico Granata979e20d2011-07-29 19:53:35 +0000416
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000417 num_matches = m_opaque_sp->FindTypes(sc,
418 name,
Sean Callanan3e80cd92011-10-12 02:08:07 +0000419 NULL,
Greg Clayton0fb0bcc2011-08-03 22:57:10 +0000420 false,
421 UINT32_MAX,
422 type_list);
423
424 for (size_t idx = 0; idx < num_matches; idx++)
425 {
426 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
427 if (type_sp)
428 retval.Append(SBType(type_sp));
429 }
Enrico Granata979e20d2011-07-29 19:53:35 +0000430 }
431
432 return retval;
Greg Clayton153ccd72011-08-10 02:10:13 +0000433}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000434
435
436SBSection
437SBModule::FindSection (const char *sect_name)
438{
439 SBSection sb_section;
440
Johnny Chen87b8c4c2011-12-19 20:16:22 +0000441 if (sect_name && IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000442 {
443 ObjectFile *objfile = m_opaque_sp->GetObjectFile();
444 if (objfile)
445 {
446 SectionList *section_list = objfile->GetSectionList();
447 if (section_list)
448 {
449 ConstString const_sect_name(sect_name);
450 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
451 if (section_sp)
452 {
453 sb_section.SetSection(section_sp.get());
454 }
455 }
456 }
457 }
458 return sb_section;
459}
460
Greg Clayton1b925202012-01-29 06:07:39 +0000461lldb::ByteOrder
462SBModule::GetByteOrder ()
463{
464 if (m_opaque_sp)
465 return m_opaque_sp->GetArchitecture().GetByteOrder();
466 return eByteOrderInvalid;
467}
468
469const char *
470SBModule::GetTriple ()
471{
472 if (m_opaque_sp)
473 {
474 std::string triple (m_opaque_sp->GetArchitecture().GetTriple().str());
475 // Unique the string so we don't run into ownership issues since
476 // the const strings put the string into the string pool once and
477 // the strings never comes out
478 ConstString const_triple (triple.c_str());
479 return const_triple.GetCString();
480 }
481 return NULL;
482}
483
484uint32_t
485SBModule::GetAddressByteSize()
486{
487 if (m_opaque_sp)
488 return m_opaque_sp->GetArchitecture().GetAddressByteSize();
489 return sizeof(void*);
490}
491