1ac7ddfbfSEd Maste //===-- SBModule.cpp --------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/API/SBModule.h"
11ac7ddfbfSEd Maste #include "lldb/API/SBAddress.h"
12ac7ddfbfSEd Maste #include "lldb/API/SBFileSpec.h"
13ac7ddfbfSEd Maste #include "lldb/API/SBModuleSpec.h"
14ac7ddfbfSEd Maste #include "lldb/API/SBProcess.h"
15ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
16ac7ddfbfSEd Maste #include "lldb/API/SBSymbolContextList.h"
17ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
18ac7ddfbfSEd Maste #include "lldb/Core/Log.h"
19ac7ddfbfSEd Maste #include "lldb/Core/Section.h"
20ac7ddfbfSEd Maste #include "lldb/Core/StreamString.h"
21ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectList.h"
22ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectVariable.h"
23ac7ddfbfSEd Maste #include "lldb/Symbol/ObjectFile.h"
24ac7ddfbfSEd Maste #include "lldb/Symbol/SymbolVendor.h"
25ac7ddfbfSEd Maste #include "lldb/Symbol/Symtab.h"
26ac7ddfbfSEd Maste #include "lldb/Symbol/VariableList.h"
27ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
28ac7ddfbfSEd Maste 
29ac7ddfbfSEd Maste using namespace lldb;
30ac7ddfbfSEd Maste using namespace lldb_private;
31ac7ddfbfSEd Maste 
32ac7ddfbfSEd Maste 
33ac7ddfbfSEd Maste SBModule::SBModule () :
34ac7ddfbfSEd Maste     m_opaque_sp ()
35ac7ddfbfSEd Maste {
36ac7ddfbfSEd Maste }
37ac7ddfbfSEd Maste 
38ac7ddfbfSEd Maste SBModule::SBModule (const lldb::ModuleSP& module_sp) :
39ac7ddfbfSEd Maste     m_opaque_sp (module_sp)
40ac7ddfbfSEd Maste {
41ac7ddfbfSEd Maste }
42ac7ddfbfSEd Maste 
43ac7ddfbfSEd Maste SBModule::SBModule(const SBModuleSpec &module_spec) :
44ac7ddfbfSEd Maste     m_opaque_sp ()
45ac7ddfbfSEd Maste {
46ac7ddfbfSEd Maste     ModuleSP module_sp;
47ac7ddfbfSEd Maste     Error error = ModuleList::GetSharedModule (*module_spec.m_opaque_ap,
48ac7ddfbfSEd Maste                                                module_sp,
49ac7ddfbfSEd Maste                                                NULL,
50ac7ddfbfSEd Maste                                                NULL,
51ac7ddfbfSEd Maste                                                NULL);
52ac7ddfbfSEd Maste     if (module_sp)
53ac7ddfbfSEd Maste         SetSP(module_sp);
54ac7ddfbfSEd Maste }
55ac7ddfbfSEd Maste 
56ac7ddfbfSEd Maste SBModule::SBModule(const SBModule &rhs) :
57ac7ddfbfSEd Maste     m_opaque_sp (rhs.m_opaque_sp)
58ac7ddfbfSEd Maste {
59ac7ddfbfSEd Maste }
60ac7ddfbfSEd Maste 
61ac7ddfbfSEd Maste SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) :
62ac7ddfbfSEd Maste     m_opaque_sp ()
63ac7ddfbfSEd Maste {
64ac7ddfbfSEd Maste     ProcessSP process_sp (process.GetSP());
65ac7ddfbfSEd Maste     if (process_sp)
66ac7ddfbfSEd Maste     {
67ac7ddfbfSEd Maste         m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr);
68ac7ddfbfSEd Maste         if (m_opaque_sp)
69ac7ddfbfSEd Maste         {
70ac7ddfbfSEd Maste             Target &target = process_sp->GetTarget();
71ac7ddfbfSEd Maste             bool changed = false;
72ac7ddfbfSEd Maste             m_opaque_sp->SetLoadAddress(target, 0, changed);
73ac7ddfbfSEd Maste             target.GetImages().Append(m_opaque_sp);
74ac7ddfbfSEd Maste         }
75ac7ddfbfSEd Maste     }
76ac7ddfbfSEd Maste }
77ac7ddfbfSEd Maste 
78ac7ddfbfSEd Maste const SBModule &
79ac7ddfbfSEd Maste SBModule::operator = (const SBModule &rhs)
80ac7ddfbfSEd Maste {
81ac7ddfbfSEd Maste     if (this != &rhs)
82ac7ddfbfSEd Maste         m_opaque_sp = rhs.m_opaque_sp;
83ac7ddfbfSEd Maste     return *this;
84ac7ddfbfSEd Maste }
85ac7ddfbfSEd Maste 
86ac7ddfbfSEd Maste SBModule::~SBModule ()
87ac7ddfbfSEd Maste {
88ac7ddfbfSEd Maste }
89ac7ddfbfSEd Maste 
90ac7ddfbfSEd Maste bool
91ac7ddfbfSEd Maste SBModule::IsValid () const
92ac7ddfbfSEd Maste {
93ac7ddfbfSEd Maste     return m_opaque_sp.get() != NULL;
94ac7ddfbfSEd Maste }
95ac7ddfbfSEd Maste 
96ac7ddfbfSEd Maste void
97ac7ddfbfSEd Maste SBModule::Clear()
98ac7ddfbfSEd Maste {
99ac7ddfbfSEd Maste     m_opaque_sp.reset();
100ac7ddfbfSEd Maste }
101ac7ddfbfSEd Maste 
102ac7ddfbfSEd Maste SBFileSpec
103ac7ddfbfSEd Maste SBModule::GetFileSpec () const
104ac7ddfbfSEd Maste {
105ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
106ac7ddfbfSEd Maste 
107ac7ddfbfSEd Maste     SBFileSpec file_spec;
108ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
109ac7ddfbfSEd Maste     if (module_sp)
110ac7ddfbfSEd Maste         file_spec.SetFileSpec(module_sp->GetFileSpec());
111ac7ddfbfSEd Maste 
112ac7ddfbfSEd Maste     if (log)
113ac7ddfbfSEd Maste     {
114ac7ddfbfSEd Maste         log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
115ac7ddfbfSEd Maste         module_sp.get(), file_spec.get());
116ac7ddfbfSEd Maste     }
117ac7ddfbfSEd Maste 
118ac7ddfbfSEd Maste     return file_spec;
119ac7ddfbfSEd Maste }
120ac7ddfbfSEd Maste 
121ac7ddfbfSEd Maste lldb::SBFileSpec
122ac7ddfbfSEd Maste SBModule::GetPlatformFileSpec () const
123ac7ddfbfSEd Maste {
124ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
125ac7ddfbfSEd Maste 
126ac7ddfbfSEd Maste     SBFileSpec file_spec;
127ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
128ac7ddfbfSEd Maste     if (module_sp)
129ac7ddfbfSEd Maste         file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
130ac7ddfbfSEd Maste 
131ac7ddfbfSEd Maste     if (log)
132ac7ddfbfSEd Maste     {
133ac7ddfbfSEd Maste         log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
134ac7ddfbfSEd Maste                      module_sp.get(), file_spec.get());
135ac7ddfbfSEd Maste     }
136ac7ddfbfSEd Maste 
137ac7ddfbfSEd Maste     return file_spec;
138ac7ddfbfSEd Maste 
139ac7ddfbfSEd Maste }
140ac7ddfbfSEd Maste 
141ac7ddfbfSEd Maste bool
142ac7ddfbfSEd Maste SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
143ac7ddfbfSEd Maste {
144ac7ddfbfSEd Maste     bool result = false;
145ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
146ac7ddfbfSEd Maste 
147ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
148ac7ddfbfSEd Maste     if (module_sp)
149ac7ddfbfSEd Maste     {
150ac7ddfbfSEd Maste         module_sp->SetPlatformFileSpec(*platform_file);
151ac7ddfbfSEd Maste         result = true;
152ac7ddfbfSEd Maste     }
153ac7ddfbfSEd Maste 
154ac7ddfbfSEd Maste     if (log)
155ac7ddfbfSEd Maste     {
156ac7ddfbfSEd Maste         log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i",
157ac7ddfbfSEd Maste                      module_sp.get(),
158ac7ddfbfSEd Maste                      platform_file.get(),
159ac7ddfbfSEd Maste                      platform_file->GetPath().c_str(),
160ac7ddfbfSEd Maste                      result);
161ac7ddfbfSEd Maste     }
162ac7ddfbfSEd Maste     return result;
163ac7ddfbfSEd Maste }
164ac7ddfbfSEd Maste 
165ac7ddfbfSEd Maste 
166ac7ddfbfSEd Maste 
167ac7ddfbfSEd Maste const uint8_t *
168ac7ddfbfSEd Maste SBModule::GetUUIDBytes () const
169ac7ddfbfSEd Maste {
170ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
171ac7ddfbfSEd Maste 
172ac7ddfbfSEd Maste     const uint8_t *uuid_bytes = NULL;
173ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
174ac7ddfbfSEd Maste     if (module_sp)
175ac7ddfbfSEd Maste         uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
176ac7ddfbfSEd Maste 
177ac7ddfbfSEd Maste     if (log)
178ac7ddfbfSEd Maste     {
179ac7ddfbfSEd Maste         if (uuid_bytes)
180ac7ddfbfSEd Maste         {
181ac7ddfbfSEd Maste             StreamString s;
182ac7ddfbfSEd Maste             module_sp->GetUUID().Dump (&s);
183ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", module_sp.get(), s.GetData());
184ac7ddfbfSEd Maste         }
185ac7ddfbfSEd Maste         else
186ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", module_sp.get());
187ac7ddfbfSEd Maste     }
188ac7ddfbfSEd Maste     return uuid_bytes;
189ac7ddfbfSEd Maste }
190ac7ddfbfSEd Maste 
191ac7ddfbfSEd Maste 
192ac7ddfbfSEd Maste const char *
193ac7ddfbfSEd Maste SBModule::GetUUIDString () const
194ac7ddfbfSEd Maste {
195ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
196ac7ddfbfSEd Maste 
197ac7ddfbfSEd Maste     static char uuid_string_buffer[80];
198ac7ddfbfSEd Maste     const char *uuid_c_string = NULL;
199ac7ddfbfSEd Maste     std::string uuid_string;
200ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
201ac7ddfbfSEd Maste     if (module_sp)
202ac7ddfbfSEd Maste         uuid_string = module_sp->GetUUID().GetAsString();
203ac7ddfbfSEd Maste 
204ac7ddfbfSEd Maste     if (!uuid_string.empty())
205ac7ddfbfSEd Maste     {
206ac7ddfbfSEd Maste         strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer));
207ac7ddfbfSEd Maste         uuid_c_string = uuid_string_buffer;
208ac7ddfbfSEd Maste     }
209ac7ddfbfSEd Maste 
210ac7ddfbfSEd Maste     if (log)
211ac7ddfbfSEd Maste     {
212ac7ddfbfSEd Maste         if (!uuid_string.empty())
213ac7ddfbfSEd Maste         {
214ac7ddfbfSEd Maste             StreamString s;
215ac7ddfbfSEd Maste             module_sp->GetUUID().Dump (&s);
216ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDString () => %s", module_sp.get(), s.GetData());
217ac7ddfbfSEd Maste         }
218ac7ddfbfSEd Maste         else
219ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDString () => NULL", module_sp.get());
220ac7ddfbfSEd Maste     }
221ac7ddfbfSEd Maste     return uuid_c_string;
222ac7ddfbfSEd Maste }
223ac7ddfbfSEd Maste 
224ac7ddfbfSEd Maste 
225ac7ddfbfSEd Maste bool
226ac7ddfbfSEd Maste SBModule::operator == (const SBModule &rhs) const
227ac7ddfbfSEd Maste {
228ac7ddfbfSEd Maste     if (m_opaque_sp)
229ac7ddfbfSEd Maste         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
230ac7ddfbfSEd Maste     return false;
231ac7ddfbfSEd Maste }
232ac7ddfbfSEd Maste 
233ac7ddfbfSEd Maste bool
234ac7ddfbfSEd Maste SBModule::operator != (const SBModule &rhs) const
235ac7ddfbfSEd Maste {
236ac7ddfbfSEd Maste     if (m_opaque_sp)
237ac7ddfbfSEd Maste         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
238ac7ddfbfSEd Maste     return false;
239ac7ddfbfSEd Maste }
240ac7ddfbfSEd Maste 
241ac7ddfbfSEd Maste ModuleSP
242ac7ddfbfSEd Maste SBModule::GetSP () const
243ac7ddfbfSEd Maste {
244ac7ddfbfSEd Maste     return m_opaque_sp;
245ac7ddfbfSEd Maste }
246ac7ddfbfSEd Maste 
247ac7ddfbfSEd Maste void
248ac7ddfbfSEd Maste SBModule::SetSP (const ModuleSP &module_sp)
249ac7ddfbfSEd Maste {
250ac7ddfbfSEd Maste     m_opaque_sp = module_sp;
251ac7ddfbfSEd Maste }
252ac7ddfbfSEd Maste 
253ac7ddfbfSEd Maste SBAddress
254ac7ddfbfSEd Maste SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
255ac7ddfbfSEd Maste {
256ac7ddfbfSEd Maste     lldb::SBAddress sb_addr;
257ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
258ac7ddfbfSEd Maste     if (module_sp)
259ac7ddfbfSEd Maste     {
260ac7ddfbfSEd Maste         Address addr;
261ac7ddfbfSEd Maste         if (module_sp->ResolveFileAddress (vm_addr, addr))
262ac7ddfbfSEd Maste             sb_addr.ref() = addr;
263ac7ddfbfSEd Maste     }
264ac7ddfbfSEd Maste     return sb_addr;
265ac7ddfbfSEd Maste }
266ac7ddfbfSEd Maste 
267ac7ddfbfSEd Maste SBSymbolContext
268ac7ddfbfSEd Maste SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
269ac7ddfbfSEd Maste {
270ac7ddfbfSEd Maste     SBSymbolContext sb_sc;
271ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
272ac7ddfbfSEd Maste     if (module_sp && addr.IsValid())
273ac7ddfbfSEd Maste         module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
274ac7ddfbfSEd Maste     return sb_sc;
275ac7ddfbfSEd Maste }
276ac7ddfbfSEd Maste 
277ac7ddfbfSEd Maste bool
278ac7ddfbfSEd Maste SBModule::GetDescription (SBStream &description)
279ac7ddfbfSEd Maste {
280ac7ddfbfSEd Maste     Stream &strm = description.ref();
281ac7ddfbfSEd Maste 
282ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
283ac7ddfbfSEd Maste     if (module_sp)
284ac7ddfbfSEd Maste     {
285ac7ddfbfSEd Maste         module_sp->GetDescription (&strm);
286ac7ddfbfSEd Maste     }
287ac7ddfbfSEd Maste     else
288ac7ddfbfSEd Maste         strm.PutCString ("No value");
289ac7ddfbfSEd Maste 
290ac7ddfbfSEd Maste     return true;
291ac7ddfbfSEd Maste }
292ac7ddfbfSEd Maste 
293ac7ddfbfSEd Maste uint32_t
294ac7ddfbfSEd Maste SBModule::GetNumCompileUnits()
295ac7ddfbfSEd Maste {
296ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
297ac7ddfbfSEd Maste     if (module_sp)
298ac7ddfbfSEd Maste     {
299ac7ddfbfSEd Maste         return module_sp->GetNumCompileUnits ();
300ac7ddfbfSEd Maste     }
301ac7ddfbfSEd Maste     return 0;
302ac7ddfbfSEd Maste }
303ac7ddfbfSEd Maste 
304ac7ddfbfSEd Maste SBCompileUnit
305ac7ddfbfSEd Maste SBModule::GetCompileUnitAtIndex (uint32_t index)
306ac7ddfbfSEd Maste {
307ac7ddfbfSEd Maste     SBCompileUnit sb_cu;
308ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
309ac7ddfbfSEd Maste     if (module_sp)
310ac7ddfbfSEd Maste     {
311ac7ddfbfSEd Maste         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
312ac7ddfbfSEd Maste         sb_cu.reset(cu_sp.get());
313ac7ddfbfSEd Maste     }
314ac7ddfbfSEd Maste     return sb_cu;
315ac7ddfbfSEd Maste }
316ac7ddfbfSEd Maste 
317ac7ddfbfSEd Maste static Symtab *
318ac7ddfbfSEd Maste GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
319ac7ddfbfSEd Maste {
320ac7ddfbfSEd Maste     if (module_sp)
321ac7ddfbfSEd Maste     {
322ac7ddfbfSEd Maste         SymbolVendor *symbols = module_sp->GetSymbolVendor();
323ac7ddfbfSEd Maste         if (symbols)
324ac7ddfbfSEd Maste             return symbols->GetSymtab();
325ac7ddfbfSEd Maste     }
326ac7ddfbfSEd Maste     return NULL;
327ac7ddfbfSEd Maste }
328ac7ddfbfSEd Maste 
329ac7ddfbfSEd Maste size_t
330ac7ddfbfSEd Maste SBModule::GetNumSymbols ()
331ac7ddfbfSEd Maste {
332ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
333ac7ddfbfSEd Maste     if (module_sp)
334ac7ddfbfSEd Maste     {
335ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
336ac7ddfbfSEd Maste         if (symtab)
337ac7ddfbfSEd Maste             return symtab->GetNumSymbols();
338ac7ddfbfSEd Maste     }
339ac7ddfbfSEd Maste     return 0;
340ac7ddfbfSEd Maste }
341ac7ddfbfSEd Maste 
342ac7ddfbfSEd Maste SBSymbol
343ac7ddfbfSEd Maste SBModule::GetSymbolAtIndex (size_t idx)
344ac7ddfbfSEd Maste {
345ac7ddfbfSEd Maste     SBSymbol sb_symbol;
346ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
347ac7ddfbfSEd Maste     Symtab *symtab = GetUnifiedSymbolTable (module_sp);
348ac7ddfbfSEd Maste     if (symtab)
349ac7ddfbfSEd Maste         sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
350ac7ddfbfSEd Maste     return sb_symbol;
351ac7ddfbfSEd Maste }
352ac7ddfbfSEd Maste 
353ac7ddfbfSEd Maste lldb::SBSymbol
354ac7ddfbfSEd Maste SBModule::FindSymbol (const char *name,
355ac7ddfbfSEd Maste                       lldb::SymbolType symbol_type)
356ac7ddfbfSEd Maste {
357ac7ddfbfSEd Maste     SBSymbol sb_symbol;
358ac7ddfbfSEd Maste     if (name && name[0])
359ac7ddfbfSEd Maste     {
360ac7ddfbfSEd Maste         ModuleSP module_sp (GetSP ());
361ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
362ac7ddfbfSEd Maste         if (symtab)
363ac7ddfbfSEd Maste             sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
364ac7ddfbfSEd Maste     }
365ac7ddfbfSEd Maste     return sb_symbol;
366ac7ddfbfSEd Maste }
367ac7ddfbfSEd Maste 
368ac7ddfbfSEd Maste 
369ac7ddfbfSEd Maste lldb::SBSymbolContextList
370ac7ddfbfSEd Maste SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
371ac7ddfbfSEd Maste {
372ac7ddfbfSEd Maste     SBSymbolContextList sb_sc_list;
373ac7ddfbfSEd Maste     if (name && name[0])
374ac7ddfbfSEd Maste     {
375ac7ddfbfSEd Maste         ModuleSP module_sp (GetSP ());
376ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
377ac7ddfbfSEd Maste         if (symtab)
378ac7ddfbfSEd Maste         {
379ac7ddfbfSEd Maste             std::vector<uint32_t> matching_symbol_indexes;
380ac7ddfbfSEd Maste             const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
381ac7ddfbfSEd Maste             if (num_matches)
382ac7ddfbfSEd Maste             {
383ac7ddfbfSEd Maste                 SymbolContext sc;
384ac7ddfbfSEd Maste                 sc.module_sp = module_sp;
385ac7ddfbfSEd Maste                 SymbolContextList &sc_list = *sb_sc_list;
386ac7ddfbfSEd Maste                 for (size_t i=0; i<num_matches; ++i)
387ac7ddfbfSEd Maste                 {
388ac7ddfbfSEd Maste                     sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
389ac7ddfbfSEd Maste                     if (sc.symbol)
390ac7ddfbfSEd Maste                         sc_list.Append(sc);
391ac7ddfbfSEd Maste                 }
392ac7ddfbfSEd Maste             }
393ac7ddfbfSEd Maste         }
394ac7ddfbfSEd Maste     }
395ac7ddfbfSEd Maste     return sb_sc_list;
396ac7ddfbfSEd Maste 
397ac7ddfbfSEd Maste }
398ac7ddfbfSEd Maste 
399ac7ddfbfSEd Maste 
400ac7ddfbfSEd Maste 
401ac7ddfbfSEd Maste size_t
402ac7ddfbfSEd Maste SBModule::GetNumSections ()
403ac7ddfbfSEd Maste {
404ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
405ac7ddfbfSEd Maste     if (module_sp)
406ac7ddfbfSEd Maste     {
407ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
408ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
409ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList();
410ac7ddfbfSEd Maste         if (section_list)
411ac7ddfbfSEd Maste             return section_list->GetSize();
412ac7ddfbfSEd Maste     }
413ac7ddfbfSEd Maste     return 0;
414ac7ddfbfSEd Maste }
415ac7ddfbfSEd Maste 
416ac7ddfbfSEd Maste SBSection
417ac7ddfbfSEd Maste SBModule::GetSectionAtIndex (size_t idx)
418ac7ddfbfSEd Maste {
419ac7ddfbfSEd Maste     SBSection sb_section;
420ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
421ac7ddfbfSEd Maste     if (module_sp)
422ac7ddfbfSEd Maste     {
423ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
424ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
425ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList ();
426ac7ddfbfSEd Maste 
427ac7ddfbfSEd Maste         if (section_list)
428ac7ddfbfSEd Maste             sb_section.SetSP(section_list->GetSectionAtIndex (idx));
429ac7ddfbfSEd Maste     }
430ac7ddfbfSEd Maste     return sb_section;
431ac7ddfbfSEd Maste }
432ac7ddfbfSEd Maste 
433ac7ddfbfSEd Maste lldb::SBSymbolContextList
434ac7ddfbfSEd Maste SBModule::FindFunctions (const char *name,
435ac7ddfbfSEd Maste                          uint32_t name_type_mask)
436ac7ddfbfSEd Maste {
437ac7ddfbfSEd Maste     lldb::SBSymbolContextList sb_sc_list;
438ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
439ac7ddfbfSEd Maste     if (name && module_sp)
440ac7ddfbfSEd Maste     {
441ac7ddfbfSEd Maste         const bool append = true;
442ac7ddfbfSEd Maste         const bool symbols_ok = true;
443ac7ddfbfSEd Maste         const bool inlines_ok = true;
444ac7ddfbfSEd Maste         module_sp->FindFunctions (ConstString(name),
445ac7ddfbfSEd Maste                                   NULL,
446ac7ddfbfSEd Maste                                   name_type_mask,
447ac7ddfbfSEd Maste                                   symbols_ok,
448ac7ddfbfSEd Maste                                   inlines_ok,
449ac7ddfbfSEd Maste                                   append,
450ac7ddfbfSEd Maste                                   *sb_sc_list);
451ac7ddfbfSEd Maste     }
452ac7ddfbfSEd Maste     return sb_sc_list;
453ac7ddfbfSEd Maste }
454ac7ddfbfSEd Maste 
455ac7ddfbfSEd Maste 
456ac7ddfbfSEd Maste SBValueList
457ac7ddfbfSEd Maste SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
458ac7ddfbfSEd Maste {
459ac7ddfbfSEd Maste     SBValueList sb_value_list;
460ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
461ac7ddfbfSEd Maste     if (name && module_sp)
462ac7ddfbfSEd Maste     {
463ac7ddfbfSEd Maste         VariableList variable_list;
464ac7ddfbfSEd Maste         const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
465ac7ddfbfSEd Maste                                                                      NULL,
466ac7ddfbfSEd Maste                                                                      false,
467ac7ddfbfSEd Maste                                                                      max_matches,
468ac7ddfbfSEd Maste                                                                      variable_list);
469ac7ddfbfSEd Maste 
470ac7ddfbfSEd Maste         if (match_count > 0)
471ac7ddfbfSEd Maste         {
472ac7ddfbfSEd Maste             for (uint32_t i=0; i<match_count; ++i)
473ac7ddfbfSEd Maste             {
474ac7ddfbfSEd Maste                 lldb::ValueObjectSP valobj_sp;
475ac7ddfbfSEd Maste                 TargetSP target_sp (target.GetSP());
476ac7ddfbfSEd Maste                 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
477ac7ddfbfSEd Maste                 if (valobj_sp)
478ac7ddfbfSEd Maste                     sb_value_list.Append(SBValue(valobj_sp));
479ac7ddfbfSEd Maste             }
480ac7ddfbfSEd Maste         }
481ac7ddfbfSEd Maste     }
482ac7ddfbfSEd Maste 
483ac7ddfbfSEd Maste     return sb_value_list;
484ac7ddfbfSEd Maste }
485ac7ddfbfSEd Maste 
486ac7ddfbfSEd Maste lldb::SBValue
487ac7ddfbfSEd Maste SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
488ac7ddfbfSEd Maste {
489ac7ddfbfSEd Maste     SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
490ac7ddfbfSEd Maste     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
491ac7ddfbfSEd Maste         return sb_value_list.GetValueAtIndex(0);
492ac7ddfbfSEd Maste     return SBValue();
493ac7ddfbfSEd Maste }
494ac7ddfbfSEd Maste 
495ac7ddfbfSEd Maste lldb::SBType
496ac7ddfbfSEd Maste SBModule::FindFirstType (const char *name_cstr)
497ac7ddfbfSEd Maste {
498ac7ddfbfSEd Maste     SBType sb_type;
499ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
500ac7ddfbfSEd Maste     if (name_cstr && module_sp)
501ac7ddfbfSEd Maste     {
502ac7ddfbfSEd Maste         SymbolContext sc;
503ac7ddfbfSEd Maste         const bool exact_match = false;
504ac7ddfbfSEd Maste         ConstString name(name_cstr);
505ac7ddfbfSEd Maste 
506ac7ddfbfSEd Maste         sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
507ac7ddfbfSEd Maste 
508ac7ddfbfSEd Maste         if (!sb_type.IsValid())
509ac7ddfbfSEd Maste             sb_type = SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
510ac7ddfbfSEd Maste     }
511ac7ddfbfSEd Maste     return sb_type;
512ac7ddfbfSEd Maste }
513ac7ddfbfSEd Maste 
514ac7ddfbfSEd Maste lldb::SBType
515ac7ddfbfSEd Maste SBModule::GetBasicType(lldb::BasicType type)
516ac7ddfbfSEd Maste {
517ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
518ac7ddfbfSEd Maste     if (module_sp)
519ac7ddfbfSEd Maste         return SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
520ac7ddfbfSEd Maste     return SBType();
521ac7ddfbfSEd Maste }
522ac7ddfbfSEd Maste 
523ac7ddfbfSEd Maste lldb::SBTypeList
524ac7ddfbfSEd Maste SBModule::FindTypes (const char *type)
525ac7ddfbfSEd Maste {
526ac7ddfbfSEd Maste     SBTypeList retval;
527ac7ddfbfSEd Maste 
528ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
529ac7ddfbfSEd Maste     if (type && module_sp)
530ac7ddfbfSEd Maste     {
531ac7ddfbfSEd Maste         SymbolContext sc;
532ac7ddfbfSEd Maste         TypeList type_list;
533ac7ddfbfSEd Maste         const bool exact_match = false;
534ac7ddfbfSEd Maste         ConstString name(type);
535ac7ddfbfSEd Maste         const uint32_t num_matches = module_sp->FindTypes (sc,
536ac7ddfbfSEd Maste                                                            name,
537ac7ddfbfSEd Maste                                                            exact_match,
538ac7ddfbfSEd Maste                                                            UINT32_MAX,
539ac7ddfbfSEd Maste                                                            type_list);
540ac7ddfbfSEd Maste 
541ac7ddfbfSEd Maste         if (num_matches > 0)
542ac7ddfbfSEd Maste         {
543ac7ddfbfSEd Maste             for (size_t idx = 0; idx < num_matches; idx++)
544ac7ddfbfSEd Maste             {
545ac7ddfbfSEd Maste                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
546ac7ddfbfSEd Maste                 if (type_sp)
547ac7ddfbfSEd Maste                     retval.Append(SBType(type_sp));
548ac7ddfbfSEd Maste             }
549ac7ddfbfSEd Maste         }
550ac7ddfbfSEd Maste         else
551ac7ddfbfSEd Maste         {
552ac7ddfbfSEd Maste             SBType sb_type(ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
553ac7ddfbfSEd Maste             if (sb_type.IsValid())
554ac7ddfbfSEd Maste                 retval.Append(sb_type);
555ac7ddfbfSEd Maste         }
556ac7ddfbfSEd Maste     }
557ac7ddfbfSEd Maste 
558ac7ddfbfSEd Maste     return retval;
559ac7ddfbfSEd Maste }
560ac7ddfbfSEd Maste 
561ac7ddfbfSEd Maste lldb::SBTypeList
562ac7ddfbfSEd Maste SBModule::GetTypes (uint32_t type_mask)
563ac7ddfbfSEd Maste {
564ac7ddfbfSEd Maste     SBTypeList sb_type_list;
565ac7ddfbfSEd Maste 
566ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
567ac7ddfbfSEd Maste     if (module_sp)
568ac7ddfbfSEd Maste     {
569ac7ddfbfSEd Maste         SymbolVendor* vendor = module_sp->GetSymbolVendor();
570ac7ddfbfSEd Maste         if (vendor)
571ac7ddfbfSEd Maste         {
572ac7ddfbfSEd Maste             TypeList type_list;
573ac7ddfbfSEd Maste             vendor->GetTypes (NULL, type_mask, type_list);
574ac7ddfbfSEd Maste             sb_type_list.m_opaque_ap->Append(type_list);
575ac7ddfbfSEd Maste         }
576ac7ddfbfSEd Maste     }
577ac7ddfbfSEd Maste     return sb_type_list;
578ac7ddfbfSEd Maste }
579ac7ddfbfSEd Maste 
580ac7ddfbfSEd Maste SBSection
581ac7ddfbfSEd Maste SBModule::FindSection (const char *sect_name)
582ac7ddfbfSEd Maste {
583ac7ddfbfSEd Maste     SBSection sb_section;
584ac7ddfbfSEd Maste 
585ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
586ac7ddfbfSEd Maste     if (sect_name && module_sp)
587ac7ddfbfSEd Maste     {
588ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
589ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
590ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList();
591ac7ddfbfSEd Maste         if (section_list)
592ac7ddfbfSEd Maste         {
593ac7ddfbfSEd Maste             ConstString const_sect_name(sect_name);
594ac7ddfbfSEd Maste             SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
595ac7ddfbfSEd Maste             if (section_sp)
596ac7ddfbfSEd Maste             {
597ac7ddfbfSEd Maste                 sb_section.SetSP (section_sp);
598ac7ddfbfSEd Maste             }
599ac7ddfbfSEd Maste         }
600ac7ddfbfSEd Maste     }
601ac7ddfbfSEd Maste     return sb_section;
602ac7ddfbfSEd Maste }
603ac7ddfbfSEd Maste 
604ac7ddfbfSEd Maste lldb::ByteOrder
605ac7ddfbfSEd Maste SBModule::GetByteOrder ()
606ac7ddfbfSEd Maste {
607ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
608ac7ddfbfSEd Maste     if (module_sp)
609ac7ddfbfSEd Maste         return module_sp->GetArchitecture().GetByteOrder();
610ac7ddfbfSEd Maste     return eByteOrderInvalid;
611ac7ddfbfSEd Maste }
612ac7ddfbfSEd Maste 
613ac7ddfbfSEd Maste const char *
614ac7ddfbfSEd Maste SBModule::GetTriple ()
615ac7ddfbfSEd Maste {
616ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
617ac7ddfbfSEd Maste     if (module_sp)
618ac7ddfbfSEd Maste     {
619ac7ddfbfSEd Maste         std::string triple (module_sp->GetArchitecture().GetTriple().str());
620ac7ddfbfSEd Maste         // Unique the string so we don't run into ownership issues since
621ac7ddfbfSEd Maste         // the const strings put the string into the string pool once and
622ac7ddfbfSEd Maste         // the strings never comes out
623ac7ddfbfSEd Maste         ConstString const_triple (triple.c_str());
624ac7ddfbfSEd Maste         return const_triple.GetCString();
625ac7ddfbfSEd Maste     }
626ac7ddfbfSEd Maste     return NULL;
627ac7ddfbfSEd Maste }
628ac7ddfbfSEd Maste 
629ac7ddfbfSEd Maste uint32_t
630ac7ddfbfSEd Maste SBModule::GetAddressByteSize()
631ac7ddfbfSEd Maste {
632ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
633ac7ddfbfSEd Maste     if (module_sp)
634ac7ddfbfSEd Maste         return module_sp->GetArchitecture().GetAddressByteSize();
635ac7ddfbfSEd Maste     return sizeof(void*);
636ac7ddfbfSEd Maste }
637ac7ddfbfSEd Maste 
638ac7ddfbfSEd Maste 
639ac7ddfbfSEd Maste uint32_t
640ac7ddfbfSEd Maste SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
641ac7ddfbfSEd Maste {
642ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
643ac7ddfbfSEd Maste     if (module_sp)
644ac7ddfbfSEd Maste         return module_sp->GetVersion(versions, num_versions);
645ac7ddfbfSEd Maste     else
646ac7ddfbfSEd Maste     {
647ac7ddfbfSEd Maste         if (versions && num_versions)
648ac7ddfbfSEd Maste         {
649ac7ddfbfSEd Maste             for (uint32_t i=0; i<num_versions; ++i)
650ac7ddfbfSEd Maste                 versions[i] = UINT32_MAX;
651ac7ddfbfSEd Maste         }
652ac7ddfbfSEd Maste         return 0;
653ac7ddfbfSEd Maste     }
654ac7ddfbfSEd Maste }
655ac7ddfbfSEd Maste 
656