130fdc8d8SChris Lattner //===-- SBModule.cpp --------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/API/SBModule.h"
1109960031SGreg Clayton #include "lldb/API/SBAddress.h"
1209960031SGreg Clayton #include "lldb/API/SBFileSpec.h"
13c9660546SGreg Clayton #include "lldb/API/SBProcess.h"
14dde9cff3SCaroline Tice #include "lldb/API/SBStream.h"
15fe356d35SGreg Clayton #include "lldb/API/SBSymbolContextList.h"
1630fdc8d8SChris Lattner #include "lldb/Core/Module.h"
17ceb6b139SCaroline Tice #include "lldb/Core/Log.h"
181f746071SGreg Clayton #include "lldb/Core/Section.h"
1938adbbb8SGreg Clayton #include "lldb/Core/StreamString.h"
20dea8cb4fSGreg Clayton #include "lldb/Core/ValueObjectList.h"
21dea8cb4fSGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
221f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
236f3533fbSEnrico Granata #include "lldb/Symbol/SymbolVendor.h"
241f746071SGreg Clayton #include "lldb/Symbol/Symtab.h"
25dea8cb4fSGreg Clayton #include "lldb/Symbol/VariableList.h"
26dea8cb4fSGreg Clayton #include "lldb/Target/Target.h"
2730fdc8d8SChris Lattner 
2830fdc8d8SChris Lattner using namespace lldb;
29ceb6b139SCaroline Tice using namespace lldb_private;
3030fdc8d8SChris Lattner 
3130fdc8d8SChris Lattner 
3230fdc8d8SChris Lattner SBModule::SBModule () :
336611103cSGreg Clayton     m_opaque_sp ()
3430fdc8d8SChris Lattner {
3530fdc8d8SChris Lattner }
3630fdc8d8SChris Lattner 
3730fdc8d8SChris Lattner SBModule::SBModule (const lldb::ModuleSP& module_sp) :
386611103cSGreg Clayton     m_opaque_sp (module_sp)
3930fdc8d8SChris Lattner {
4030fdc8d8SChris Lattner }
4130fdc8d8SChris Lattner 
42efabb123SGreg Clayton SBModule::SBModule(const SBModule &rhs) :
43efabb123SGreg Clayton     m_opaque_sp (rhs.m_opaque_sp)
44efabb123SGreg Clayton {
45efabb123SGreg Clayton }
46efabb123SGreg Clayton 
47c9660546SGreg Clayton SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) :
48c9660546SGreg Clayton     m_opaque_sp ()
49c9660546SGreg Clayton {
50c9660546SGreg Clayton     ProcessSP process_sp (process.GetSP());
51c9660546SGreg Clayton     if (process_sp)
52c859e2d5SGreg Clayton     {
5339f7ee86SGreg Clayton         m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr);
5439f7ee86SGreg Clayton         if (m_opaque_sp)
5539f7ee86SGreg Clayton         {
5639f7ee86SGreg Clayton             Target &target = process_sp->GetTarget();
5739f7ee86SGreg Clayton             bool changed = false;
5839f7ee86SGreg Clayton             m_opaque_sp->SetLoadAddress(target, 0, changed);
5939f7ee86SGreg Clayton             target.GetImages().Append(m_opaque_sp);
6039f7ee86SGreg Clayton         }
61c859e2d5SGreg Clayton     }
62c9660546SGreg Clayton }
63c9660546SGreg Clayton 
64efabb123SGreg Clayton const SBModule &
65efabb123SGreg Clayton SBModule::operator = (const SBModule &rhs)
66efabb123SGreg Clayton {
67efabb123SGreg Clayton     if (this != &rhs)
68efabb123SGreg Clayton         m_opaque_sp = rhs.m_opaque_sp;
69efabb123SGreg Clayton     return *this;
70efabb123SGreg Clayton }
71efabb123SGreg Clayton 
7230fdc8d8SChris Lattner SBModule::~SBModule ()
7330fdc8d8SChris Lattner {
7430fdc8d8SChris Lattner }
7530fdc8d8SChris Lattner 
7630fdc8d8SChris Lattner bool
7730fdc8d8SChris Lattner SBModule::IsValid () const
7830fdc8d8SChris Lattner {
796611103cSGreg Clayton     return m_opaque_sp.get() != NULL;
8030fdc8d8SChris Lattner }
8130fdc8d8SChris Lattner 
825d3bca4eSJim Ingham void
835d3bca4eSJim Ingham SBModule::Clear()
845d3bca4eSJim Ingham {
855d3bca4eSJim Ingham     m_opaque_sp.reset();
865d3bca4eSJim Ingham }
875d3bca4eSJim Ingham 
8830fdc8d8SChris Lattner SBFileSpec
8930fdc8d8SChris Lattner SBModule::GetFileSpec () const
9030fdc8d8SChris Lattner {
915160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
92ceb6b139SCaroline Tice 
9330fdc8d8SChris Lattner     SBFileSpec file_spec;
94c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
95c2ff9318SGreg Clayton     if (module_sp)
96c2ff9318SGreg Clayton         file_spec.SetFileSpec(module_sp->GetFileSpec());
97ceb6b139SCaroline Tice 
98ceb6b139SCaroline Tice     if (log)
99ceb6b139SCaroline Tice     {
100cfd1acedSGreg Clayton         log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
101c2ff9318SGreg Clayton         module_sp.get(), file_spec.get());
102ceb6b139SCaroline Tice     }
103ceb6b139SCaroline Tice 
10430fdc8d8SChris Lattner     return file_spec;
10530fdc8d8SChris Lattner }
10630fdc8d8SChris Lattner 
1072289fa48SGreg Clayton lldb::SBFileSpec
1082289fa48SGreg Clayton SBModule::GetPlatformFileSpec () const
1092289fa48SGreg Clayton {
1105160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1112289fa48SGreg Clayton 
1122289fa48SGreg Clayton     SBFileSpec file_spec;
113c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
114c2ff9318SGreg Clayton     if (module_sp)
115c2ff9318SGreg Clayton         file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
1162289fa48SGreg Clayton 
1172289fa48SGreg Clayton     if (log)
1182289fa48SGreg Clayton     {
1192289fa48SGreg Clayton         log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
120c2ff9318SGreg Clayton                      module_sp.get(), file_spec.get());
1212289fa48SGreg Clayton     }
1222289fa48SGreg Clayton 
1232289fa48SGreg Clayton     return file_spec;
1242289fa48SGreg Clayton 
1252289fa48SGreg Clayton }
1262289fa48SGreg Clayton 
1272289fa48SGreg Clayton bool
1282289fa48SGreg Clayton SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
1292289fa48SGreg Clayton {
1302289fa48SGreg Clayton     bool result = false;
1315160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1322289fa48SGreg Clayton 
133c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
134c2ff9318SGreg Clayton     if (module_sp)
1352289fa48SGreg Clayton     {
136c2ff9318SGreg Clayton         module_sp->SetPlatformFileSpec(*platform_file);
1372289fa48SGreg Clayton         result = true;
1382289fa48SGreg Clayton     }
1392289fa48SGreg Clayton 
1402289fa48SGreg Clayton     if (log)
1412289fa48SGreg Clayton     {
142b5ad4ec7SGreg Clayton         log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i",
143c2ff9318SGreg Clayton                      module_sp.get(),
1442289fa48SGreg Clayton                      platform_file.get(),
145b5ad4ec7SGreg Clayton                      platform_file->GetPath().c_str(),
1462289fa48SGreg Clayton                      result);
1472289fa48SGreg Clayton     }
1482289fa48SGreg Clayton     return result;
1492289fa48SGreg Clayton }
1502289fa48SGreg Clayton 
1512289fa48SGreg Clayton 
1522289fa48SGreg Clayton 
15330fdc8d8SChris Lattner const uint8_t *
15430fdc8d8SChris Lattner SBModule::GetUUIDBytes () const
15530fdc8d8SChris Lattner {
1565160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
157ceb6b139SCaroline Tice 
1584838131bSGreg Clayton     const uint8_t *uuid_bytes = NULL;
159c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
160c2ff9318SGreg Clayton     if (module_sp)
161c2ff9318SGreg Clayton         uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
162ceb6b139SCaroline Tice 
163ceb6b139SCaroline Tice     if (log)
1644838131bSGreg Clayton     {
1654838131bSGreg Clayton         if (uuid_bytes)
1664838131bSGreg Clayton         {
1674838131bSGreg Clayton             StreamString s;
168c2ff9318SGreg Clayton             module_sp->GetUUID().Dump (&s);
169c2ff9318SGreg Clayton             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", module_sp.get(), s.GetData());
1704838131bSGreg Clayton         }
1714838131bSGreg Clayton         else
172c2ff9318SGreg Clayton             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", module_sp.get());
1734838131bSGreg Clayton     }
1744838131bSGreg Clayton     return uuid_bytes;
17530fdc8d8SChris Lattner }
17630fdc8d8SChris Lattner 
17730fdc8d8SChris Lattner 
178df2963edSJohnny Chen const char *
179df2963edSJohnny Chen SBModule::GetUUIDString () const
180df2963edSJohnny Chen {
1815160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
182df2963edSJohnny Chen 
183c16b4af0SJason Molenda     static char uuid_string_buffer[80];
184df2963edSJohnny Chen     const char *uuid_c_string = NULL;
185c16b4af0SJason Molenda     std::string uuid_string;
186c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
187c2ff9318SGreg Clayton     if (module_sp)
188c16b4af0SJason Molenda         uuid_string = module_sp->GetUUID().GetAsString();
189c16b4af0SJason Molenda 
190c16b4af0SJason Molenda     if (!uuid_string.empty())
191c16b4af0SJason Molenda     {
192c16b4af0SJason Molenda         strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer));
193c16b4af0SJason Molenda         uuid_c_string = uuid_string_buffer;
194c16b4af0SJason Molenda     }
195df2963edSJohnny Chen 
196df2963edSJohnny Chen     if (log)
197df2963edSJohnny Chen     {
198c16b4af0SJason Molenda         if (!uuid_string.empty())
199df2963edSJohnny Chen         {
200df2963edSJohnny Chen             StreamString s;
201c2ff9318SGreg Clayton             module_sp->GetUUID().Dump (&s);
202c2ff9318SGreg Clayton             log->Printf ("SBModule(%p)::GetUUIDString () => %s", module_sp.get(), s.GetData());
203df2963edSJohnny Chen         }
204df2963edSJohnny Chen         else
205c2ff9318SGreg Clayton             log->Printf ("SBModule(%p)::GetUUIDString () => NULL", module_sp.get());
206df2963edSJohnny Chen     }
207df2963edSJohnny Chen     return uuid_c_string;
208df2963edSJohnny Chen }
209df2963edSJohnny Chen 
210df2963edSJohnny Chen 
21130fdc8d8SChris Lattner bool
21230fdc8d8SChris Lattner SBModule::operator == (const SBModule &rhs) const
21330fdc8d8SChris Lattner {
2146611103cSGreg Clayton     if (m_opaque_sp)
2156611103cSGreg Clayton         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
21630fdc8d8SChris Lattner     return false;
21730fdc8d8SChris Lattner }
21830fdc8d8SChris Lattner 
21930fdc8d8SChris Lattner bool
22030fdc8d8SChris Lattner SBModule::operator != (const SBModule &rhs) const
22130fdc8d8SChris Lattner {
2226611103cSGreg Clayton     if (m_opaque_sp)
2236611103cSGreg Clayton         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
22430fdc8d8SChris Lattner     return false;
22530fdc8d8SChris Lattner }
22630fdc8d8SChris Lattner 
227acdbe816SGreg Clayton ModuleSP
228acdbe816SGreg Clayton SBModule::GetSP () const
229cac9c5f9SGreg Clayton {
230cac9c5f9SGreg Clayton     return m_opaque_sp;
231cac9c5f9SGreg Clayton }
23230fdc8d8SChris Lattner 
23330fdc8d8SChris Lattner void
234acdbe816SGreg Clayton SBModule::SetSP (const ModuleSP &module_sp)
23530fdc8d8SChris Lattner {
2366611103cSGreg Clayton     m_opaque_sp = module_sp;
23730fdc8d8SChris Lattner }
23830fdc8d8SChris Lattner 
239cac9c5f9SGreg Clayton SBAddress
240cac9c5f9SGreg Clayton SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
24109960031SGreg Clayton {
242cac9c5f9SGreg Clayton     lldb::SBAddress sb_addr;
243c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
244c2ff9318SGreg Clayton     if (module_sp)
245cac9c5f9SGreg Clayton     {
246cac9c5f9SGreg Clayton         Address addr;
247c2ff9318SGreg Clayton         if (module_sp->ResolveFileAddress (vm_addr, addr))
248cac9c5f9SGreg Clayton             sb_addr.ref() = addr;
249cac9c5f9SGreg Clayton     }
250cac9c5f9SGreg Clayton     return sb_addr;
25109960031SGreg Clayton }
25209960031SGreg Clayton 
25309960031SGreg Clayton SBSymbolContext
25409960031SGreg Clayton SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
25509960031SGreg Clayton {
25609960031SGreg Clayton     SBSymbolContext sb_sc;
257c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
258c2ff9318SGreg Clayton     if (module_sp && addr.IsValid())
259c2ff9318SGreg Clayton         module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
26009960031SGreg Clayton     return sb_sc;
26109960031SGreg Clayton }
26209960031SGreg Clayton 
263dde9cff3SCaroline Tice bool
264dde9cff3SCaroline Tice SBModule::GetDescription (SBStream &description)
265dde9cff3SCaroline Tice {
266da7bc7d0SGreg Clayton     Stream &strm = description.ref();
267da7bc7d0SGreg Clayton 
268c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
269c2ff9318SGreg Clayton     if (module_sp)
270dde9cff3SCaroline Tice     {
271c2ff9318SGreg Clayton         module_sp->GetDescription (&strm);
272dde9cff3SCaroline Tice     }
273dde9cff3SCaroline Tice     else
274da7bc7d0SGreg Clayton         strm.PutCString ("No value");
275dde9cff3SCaroline Tice 
276dde9cff3SCaroline Tice     return true;
277dde9cff3SCaroline Tice }
278bbdabce2SGreg Clayton 
279873a7a4bSJohnny Chen uint32_t
280873a7a4bSJohnny Chen SBModule::GetNumCompileUnits()
281873a7a4bSJohnny Chen {
282873a7a4bSJohnny Chen     ModuleSP module_sp (GetSP ());
283873a7a4bSJohnny Chen     if (module_sp)
284873a7a4bSJohnny Chen     {
285873a7a4bSJohnny Chen         return module_sp->GetNumCompileUnits ();
286873a7a4bSJohnny Chen     }
287873a7a4bSJohnny Chen     return 0;
288873a7a4bSJohnny Chen }
289873a7a4bSJohnny Chen 
290873a7a4bSJohnny Chen SBCompileUnit
291873a7a4bSJohnny Chen SBModule::GetCompileUnitAtIndex (uint32_t index)
292873a7a4bSJohnny Chen {
293873a7a4bSJohnny Chen     SBCompileUnit sb_cu;
294873a7a4bSJohnny Chen     ModuleSP module_sp (GetSP ());
295873a7a4bSJohnny Chen     if (module_sp)
296873a7a4bSJohnny Chen     {
297873a7a4bSJohnny Chen         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
298873a7a4bSJohnny Chen         sb_cu.reset(cu_sp.get());
299873a7a4bSJohnny Chen     }
300873a7a4bSJohnny Chen     return sb_cu;
301873a7a4bSJohnny Chen }
302873a7a4bSJohnny Chen 
303bbdabce2SGreg Clayton size_t
304bbdabce2SGreg Clayton SBModule::GetNumSymbols ()
305bbdabce2SGreg Clayton {
306c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
307c2ff9318SGreg Clayton     if (module_sp)
308bbdabce2SGreg Clayton     {
309c2ff9318SGreg Clayton         ObjectFile *obj_file = module_sp->GetObjectFile();
310bbdabce2SGreg Clayton         if (obj_file)
311bbdabce2SGreg Clayton         {
312bbdabce2SGreg Clayton             Symtab *symtab = obj_file->GetSymtab();
313bbdabce2SGreg Clayton             if (symtab)
314bbdabce2SGreg Clayton                 return symtab->GetNumSymbols();
315bbdabce2SGreg Clayton         }
316bbdabce2SGreg Clayton     }
317bbdabce2SGreg Clayton     return 0;
318bbdabce2SGreg Clayton }
319bbdabce2SGreg Clayton 
320bbdabce2SGreg Clayton SBSymbol
321bbdabce2SGreg Clayton SBModule::GetSymbolAtIndex (size_t idx)
322bbdabce2SGreg Clayton {
323bbdabce2SGreg Clayton     SBSymbol sb_symbol;
324c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
325c2ff9318SGreg Clayton     if (module_sp)
326bbdabce2SGreg Clayton     {
327c2ff9318SGreg Clayton         ObjectFile *obj_file = module_sp->GetObjectFile();
328bbdabce2SGreg Clayton         if (obj_file)
329bbdabce2SGreg Clayton         {
330bbdabce2SGreg Clayton             Symtab *symtab = obj_file->GetSymtab();
331bbdabce2SGreg Clayton             if (symtab)
332bbdabce2SGreg Clayton                 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
333bbdabce2SGreg Clayton         }
334bbdabce2SGreg Clayton     }
335bbdabce2SGreg Clayton     return sb_symbol;
336bbdabce2SGreg Clayton }
337fe356d35SGreg Clayton 
338e14e1925SGreg Clayton lldb::SBSymbol
339e14e1925SGreg Clayton SBModule::FindSymbol (const char *name,
340e14e1925SGreg Clayton                       lldb::SymbolType symbol_type)
341e14e1925SGreg Clayton {
342e14e1925SGreg Clayton     SBSymbol sb_symbol;
343e14e1925SGreg Clayton     if (name && name[0])
344e14e1925SGreg Clayton     {
345e14e1925SGreg Clayton         ModuleSP module_sp (GetSP ());
346e14e1925SGreg Clayton         if (module_sp)
347e14e1925SGreg Clayton         {
348e14e1925SGreg Clayton             ObjectFile *obj_file = module_sp->GetObjectFile();
349e14e1925SGreg Clayton             if (obj_file)
350e14e1925SGreg Clayton             {
351e14e1925SGreg Clayton                 Symtab *symtab = obj_file->GetSymtab();
352e14e1925SGreg Clayton                 if (symtab)
353e14e1925SGreg Clayton                     sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
354e14e1925SGreg Clayton             }
355e14e1925SGreg Clayton         }
356e14e1925SGreg Clayton     }
357e14e1925SGreg Clayton     return sb_symbol;
358e14e1925SGreg Clayton }
359e14e1925SGreg Clayton 
360e14e1925SGreg Clayton 
361e14e1925SGreg Clayton lldb::SBSymbolContextList
362e14e1925SGreg Clayton SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
363e14e1925SGreg Clayton {
364e14e1925SGreg Clayton     SBSymbolContextList sb_sc_list;
365e14e1925SGreg Clayton     if (name && name[0])
366e14e1925SGreg Clayton     {
367e14e1925SGreg Clayton         ModuleSP module_sp (GetSP ());
368e14e1925SGreg Clayton         if (module_sp)
369e14e1925SGreg Clayton         {
370e14e1925SGreg Clayton             ObjectFile *obj_file = module_sp->GetObjectFile();
371e14e1925SGreg Clayton             if (obj_file)
372e14e1925SGreg Clayton             {
373e14e1925SGreg Clayton                 Symtab *symtab = obj_file->GetSymtab();
374e14e1925SGreg Clayton                 if (symtab)
375e14e1925SGreg Clayton                 {
376e14e1925SGreg Clayton                     std::vector<uint32_t> matching_symbol_indexes;
377e14e1925SGreg Clayton                     const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
378e14e1925SGreg Clayton                     if (num_matches)
379e14e1925SGreg Clayton                     {
380e14e1925SGreg Clayton                         SymbolContext sc;
381e14e1925SGreg Clayton                         sc.module_sp = module_sp;
382e14e1925SGreg Clayton                         SymbolContextList &sc_list = *sb_sc_list;
383e14e1925SGreg Clayton                         for (size_t i=0; i<num_matches; ++i)
384e14e1925SGreg Clayton                         {
385e14e1925SGreg Clayton                             sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
386e14e1925SGreg Clayton                             if (sc.symbol)
387e14e1925SGreg Clayton                                 sc_list.Append(sc);
388e14e1925SGreg Clayton                         }
389e14e1925SGreg Clayton                     }
390e14e1925SGreg Clayton                 }
391e14e1925SGreg Clayton             }
392e14e1925SGreg Clayton         }
393e14e1925SGreg Clayton     }
394e14e1925SGreg Clayton     return sb_sc_list;
395e14e1925SGreg Clayton 
396e14e1925SGreg Clayton }
397e14e1925SGreg Clayton 
398e14e1925SGreg Clayton 
399e14e1925SGreg Clayton 
400cac9c5f9SGreg Clayton size_t
401cac9c5f9SGreg Clayton SBModule::GetNumSections ()
402cac9c5f9SGreg Clayton {
403c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
404c2ff9318SGreg Clayton     if (module_sp)
405cac9c5f9SGreg Clayton     {
406c2ff9318SGreg Clayton         ObjectFile *obj_file = module_sp->GetObjectFile();
407cac9c5f9SGreg Clayton         if (obj_file)
408cac9c5f9SGreg Clayton         {
409cac9c5f9SGreg Clayton             SectionList *section_list = obj_file->GetSectionList ();
410cac9c5f9SGreg Clayton             if (section_list)
411cac9c5f9SGreg Clayton                 return section_list->GetSize();
412cac9c5f9SGreg Clayton         }
413cac9c5f9SGreg Clayton     }
414cac9c5f9SGreg Clayton     return 0;
415cac9c5f9SGreg Clayton }
416cac9c5f9SGreg Clayton 
417cac9c5f9SGreg Clayton SBSection
418cac9c5f9SGreg Clayton SBModule::GetSectionAtIndex (size_t idx)
419cac9c5f9SGreg Clayton {
420cac9c5f9SGreg Clayton     SBSection sb_section;
421c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
422c2ff9318SGreg Clayton     if (module_sp)
423cac9c5f9SGreg Clayton     {
424c2ff9318SGreg Clayton         ObjectFile *obj_file = module_sp->GetObjectFile();
425cac9c5f9SGreg Clayton         if (obj_file)
426cac9c5f9SGreg Clayton         {
427cac9c5f9SGreg Clayton             SectionList *section_list = obj_file->GetSectionList ();
428cac9c5f9SGreg Clayton 
429cac9c5f9SGreg Clayton             if (section_list)
430e72dfb32SGreg Clayton                 sb_section.SetSP(section_list->GetSectionAtIndex (idx));
431cac9c5f9SGreg Clayton         }
432cac9c5f9SGreg Clayton     }
433cac9c5f9SGreg Clayton     return sb_section;
434cac9c5f9SGreg Clayton }
435cac9c5f9SGreg Clayton 
4365569e64eSGreg Clayton lldb::SBSymbolContextList
437fe356d35SGreg Clayton SBModule::FindFunctions (const char *name,
4385569e64eSGreg Clayton                          uint32_t name_type_mask)
439fe356d35SGreg Clayton {
4405569e64eSGreg Clayton     lldb::SBSymbolContextList sb_sc_list;
441c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
442c2ff9318SGreg Clayton     if (name && module_sp)
443fe356d35SGreg Clayton     {
4445569e64eSGreg Clayton         const bool append = true;
445fe356d35SGreg Clayton         const bool symbols_ok = true;
4469df05fbbSSean Callanan         const bool inlines_ok = true;
447c2ff9318SGreg Clayton         module_sp->FindFunctions (ConstString(name),
448b6d70ebcSSean Callanan                                   NULL,
449fe356d35SGreg Clayton                                   name_type_mask,
450fe356d35SGreg Clayton                                   symbols_ok,
4519df05fbbSSean Callanan                                   inlines_ok,
452fe356d35SGreg Clayton                                   append,
4535569e64eSGreg Clayton                                   *sb_sc_list);
454fe356d35SGreg Clayton     }
4555569e64eSGreg Clayton     return sb_sc_list;
456fe356d35SGreg Clayton }
457fe356d35SGreg Clayton 
458dea8cb4fSGreg Clayton 
459dea8cb4fSGreg Clayton SBValueList
460dea8cb4fSGreg Clayton SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
461dea8cb4fSGreg Clayton {
462dea8cb4fSGreg Clayton     SBValueList sb_value_list;
463c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
464c2ff9318SGreg Clayton     if (name && module_sp)
465dea8cb4fSGreg Clayton     {
466dea8cb4fSGreg Clayton         VariableList variable_list;
467c2ff9318SGreg Clayton         const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
468b6d70ebcSSean Callanan                                                                      NULL,
469dea8cb4fSGreg Clayton                                                                      false,
470dea8cb4fSGreg Clayton                                                                      max_matches,
471dea8cb4fSGreg Clayton                                                                      variable_list);
472dea8cb4fSGreg Clayton 
473dea8cb4fSGreg Clayton         if (match_count > 0)
474dea8cb4fSGreg Clayton         {
475dea8cb4fSGreg Clayton             for (uint32_t i=0; i<match_count; ++i)
476dea8cb4fSGreg Clayton             {
477dea8cb4fSGreg Clayton                 lldb::ValueObjectSP valobj_sp;
478b9556accSGreg Clayton                 TargetSP target_sp (target.GetSP());
479b9556accSGreg Clayton                 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
480dea8cb4fSGreg Clayton                 if (valobj_sp)
48185425d77SEnrico Granata                     sb_value_list.Append(SBValue(valobj_sp));
482dea8cb4fSGreg Clayton             }
483dea8cb4fSGreg Clayton         }
484dea8cb4fSGreg Clayton     }
485dea8cb4fSGreg Clayton 
486dea8cb4fSGreg Clayton     return sb_value_list;
487dea8cb4fSGreg Clayton }
4886f3533fbSEnrico Granata 
489bcd80b47SEnrico Granata lldb::SBValue
490bcd80b47SEnrico Granata SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
491bcd80b47SEnrico Granata {
492bcd80b47SEnrico Granata     SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
493bcd80b47SEnrico Granata     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
494bcd80b47SEnrico Granata         return sb_value_list.GetValueAtIndex(0);
495bcd80b47SEnrico Granata     return SBValue();
496bcd80b47SEnrico Granata }
497bcd80b47SEnrico Granata 
4986f3533fbSEnrico Granata lldb::SBType
4996f3533fbSEnrico Granata SBModule::FindFirstType (const char *name_cstr)
5006f3533fbSEnrico Granata {
501fe42ac4dSGreg Clayton     SBType sb_type;
502c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
503c2ff9318SGreg Clayton     if (name_cstr && module_sp)
504fe42ac4dSGreg Clayton     {
5056f3533fbSEnrico Granata         SymbolContext sc;
50684db9105SGreg Clayton         const bool exact_match = false;
5076f3533fbSEnrico Granata         ConstString name(name_cstr);
5086f3533fbSEnrico Granata 
509b43165b7SGreg Clayton         sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
5106f3533fbSEnrico Granata 
511b43165b7SGreg Clayton         if (!sb_type.IsValid())
512b43165b7SGreg Clayton             sb_type = SBType (ClangASTType::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
5136f3533fbSEnrico Granata     }
514fe42ac4dSGreg Clayton     return sb_type;
5156f3533fbSEnrico Granata }
5166f3533fbSEnrico Granata 
517b43165b7SGreg Clayton lldb::SBType
518b43165b7SGreg Clayton SBModule::GetBasicType(lldb::BasicType type)
519b43165b7SGreg Clayton {
520b43165b7SGreg Clayton     ModuleSP module_sp (GetSP ());
521b43165b7SGreg Clayton     if (module_sp)
522b43165b7SGreg Clayton         return SBType (ClangASTType::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
523b43165b7SGreg Clayton     return SBType();
524b43165b7SGreg Clayton }
525b43165b7SGreg Clayton 
5266f3533fbSEnrico Granata lldb::SBTypeList
5276f3533fbSEnrico Granata SBModule::FindTypes (const char *type)
5286f3533fbSEnrico Granata {
5296f3533fbSEnrico Granata     SBTypeList retval;
5306f3533fbSEnrico Granata 
531c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
532c2ff9318SGreg Clayton     if (type && module_sp)
533fe42ac4dSGreg Clayton     {
5346f3533fbSEnrico Granata         SymbolContext sc;
5356f3533fbSEnrico Granata         TypeList type_list;
53684db9105SGreg Clayton         const bool exact_match = false;
5376f3533fbSEnrico Granata         ConstString name(type);
538b43165b7SGreg Clayton         const uint32_t num_matches = module_sp->FindTypes (sc,
5396f3533fbSEnrico Granata                                                            name,
54084db9105SGreg Clayton                                                            exact_match,
5416f3533fbSEnrico Granata                                                            UINT32_MAX,
5426f3533fbSEnrico Granata                                                            type_list);
5436f3533fbSEnrico Granata 
544b43165b7SGreg Clayton         if (num_matches > 0)
545b43165b7SGreg Clayton         {
5466f3533fbSEnrico Granata             for (size_t idx = 0; idx < num_matches; idx++)
5476f3533fbSEnrico Granata             {
548fe42ac4dSGreg Clayton                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
549fe42ac4dSGreg Clayton                 if (type_sp)
550fe42ac4dSGreg Clayton                     retval.Append(SBType(type_sp));
551fe42ac4dSGreg Clayton             }
5526f3533fbSEnrico Granata         }
553b43165b7SGreg Clayton         else
554b43165b7SGreg Clayton         {
555b43165b7SGreg Clayton             SBType sb_type(ClangASTType::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
556b43165b7SGreg Clayton             if (sb_type.IsValid())
557b43165b7SGreg Clayton                 retval.Append(sb_type);
558b43165b7SGreg Clayton         }
559b43165b7SGreg Clayton     }
5606f3533fbSEnrico Granata 
5616f3533fbSEnrico Granata     return retval;
5626f3533fbSEnrico Granata }
563cac9c5f9SGreg Clayton 
564*f02500c7SGreg Clayton lldb::SBTypeList
565*f02500c7SGreg Clayton SBModule::GetTypes (uint32_t type_mask)
566*f02500c7SGreg Clayton {
567*f02500c7SGreg Clayton     SBTypeList sb_type_list;
568*f02500c7SGreg Clayton 
569*f02500c7SGreg Clayton     ModuleSP module_sp (GetSP ());
570*f02500c7SGreg Clayton     if (module_sp)
571*f02500c7SGreg Clayton     {
572*f02500c7SGreg Clayton         SymbolVendor* vendor = module_sp->GetSymbolVendor();
573*f02500c7SGreg Clayton         if (vendor)
574*f02500c7SGreg Clayton         {
575*f02500c7SGreg Clayton             TypeList type_list;
576*f02500c7SGreg Clayton             vendor->GetTypes (NULL, type_mask, type_list);
577*f02500c7SGreg Clayton             sb_type_list.m_opaque_ap->Append(type_list);
578*f02500c7SGreg Clayton         }
579*f02500c7SGreg Clayton     }
580*f02500c7SGreg Clayton     return sb_type_list;
581*f02500c7SGreg Clayton }
582cac9c5f9SGreg Clayton 
583cac9c5f9SGreg Clayton SBSection
584cac9c5f9SGreg Clayton SBModule::FindSection (const char *sect_name)
585cac9c5f9SGreg Clayton {
586cac9c5f9SGreg Clayton     SBSection sb_section;
587cac9c5f9SGreg Clayton 
588c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
589c2ff9318SGreg Clayton     if (sect_name && module_sp)
590cac9c5f9SGreg Clayton     {
591c2ff9318SGreg Clayton         ObjectFile *objfile = module_sp->GetObjectFile();
592cac9c5f9SGreg Clayton         if (objfile)
593cac9c5f9SGreg Clayton         {
594cac9c5f9SGreg Clayton             SectionList *section_list = objfile->GetSectionList();
595cac9c5f9SGreg Clayton             if (section_list)
596cac9c5f9SGreg Clayton             {
597cac9c5f9SGreg Clayton                 ConstString const_sect_name(sect_name);
598cac9c5f9SGreg Clayton                 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
599cac9c5f9SGreg Clayton                 if (section_sp)
600cac9c5f9SGreg Clayton                 {
601e72dfb32SGreg Clayton                     sb_section.SetSP (section_sp);
602cac9c5f9SGreg Clayton                 }
603cac9c5f9SGreg Clayton             }
604cac9c5f9SGreg Clayton         }
605cac9c5f9SGreg Clayton     }
606cac9c5f9SGreg Clayton     return sb_section;
607cac9c5f9SGreg Clayton }
608cac9c5f9SGreg Clayton 
60913d1950aSGreg Clayton lldb::ByteOrder
61013d1950aSGreg Clayton SBModule::GetByteOrder ()
61113d1950aSGreg Clayton {
612c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
613c2ff9318SGreg Clayton     if (module_sp)
614c2ff9318SGreg Clayton         return module_sp->GetArchitecture().GetByteOrder();
61513d1950aSGreg Clayton     return eByteOrderInvalid;
61613d1950aSGreg Clayton }
61713d1950aSGreg Clayton 
61813d1950aSGreg Clayton const char *
61913d1950aSGreg Clayton SBModule::GetTriple ()
62013d1950aSGreg Clayton {
621c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
622c2ff9318SGreg Clayton     if (module_sp)
62313d1950aSGreg Clayton     {
624c2ff9318SGreg Clayton         std::string triple (module_sp->GetArchitecture().GetTriple().str());
62513d1950aSGreg Clayton         // Unique the string so we don't run into ownership issues since
62613d1950aSGreg Clayton         // the const strings put the string into the string pool once and
62713d1950aSGreg Clayton         // the strings never comes out
62813d1950aSGreg Clayton         ConstString const_triple (triple.c_str());
62913d1950aSGreg Clayton         return const_triple.GetCString();
63013d1950aSGreg Clayton     }
63113d1950aSGreg Clayton     return NULL;
63213d1950aSGreg Clayton }
63313d1950aSGreg Clayton 
63413d1950aSGreg Clayton uint32_t
63513d1950aSGreg Clayton SBModule::GetAddressByteSize()
63613d1950aSGreg Clayton {
637c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
638c2ff9318SGreg Clayton     if (module_sp)
639c2ff9318SGreg Clayton         return module_sp->GetArchitecture().GetAddressByteSize();
64013d1950aSGreg Clayton     return sizeof(void*);
64113d1950aSGreg Clayton }
64213d1950aSGreg Clayton 
643c2ff9318SGreg Clayton 
644c2ff9318SGreg Clayton uint32_t
645c2ff9318SGreg Clayton SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
646c2ff9318SGreg Clayton {
647c2ff9318SGreg Clayton     ModuleSP module_sp (GetSP ());
648c2ff9318SGreg Clayton     if (module_sp)
6493467d80bSEnrico Granata         return module_sp->GetVersion(versions, num_versions);
6503467d80bSEnrico Granata     else
651c2ff9318SGreg Clayton     {
652c2ff9318SGreg Clayton         if (versions && num_versions)
653c2ff9318SGreg Clayton         {
654c2ff9318SGreg Clayton             for (uint32_t i=0; i<num_versions; ++i)
655c2ff9318SGreg Clayton                 versions[i] = UINT32_MAX;
656c2ff9318SGreg Clayton         }
657c2ff9318SGreg Clayton         return 0;
658c2ff9318SGreg Clayton     }
6593467d80bSEnrico Granata }
660c2ff9318SGreg Clayton 
661