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"
244bb0738eSEd Maste #include "lldb/Symbol/SymbolFile.h"
25ac7ddfbfSEd Maste #include "lldb/Symbol/SymbolVendor.h"
26ac7ddfbfSEd Maste #include "lldb/Symbol/Symtab.h"
279f2f44ceSEd Maste #include "lldb/Symbol/TypeSystem.h"
28ac7ddfbfSEd Maste #include "lldb/Symbol/VariableList.h"
29ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
30ac7ddfbfSEd Maste 
31ac7ddfbfSEd Maste using namespace lldb;
32ac7ddfbfSEd Maste using namespace lldb_private;
33ac7ddfbfSEd Maste 
34ac7ddfbfSEd Maste 
35ac7ddfbfSEd Maste SBModule::SBModule () :
36ac7ddfbfSEd Maste     m_opaque_sp ()
37ac7ddfbfSEd Maste {
38ac7ddfbfSEd Maste }
39ac7ddfbfSEd Maste 
40ac7ddfbfSEd Maste SBModule::SBModule (const lldb::ModuleSP& module_sp) :
41ac7ddfbfSEd Maste     m_opaque_sp (module_sp)
42ac7ddfbfSEd Maste {
43ac7ddfbfSEd Maste }
44ac7ddfbfSEd Maste 
45ac7ddfbfSEd Maste SBModule::SBModule(const SBModuleSpec &module_spec) :
46ac7ddfbfSEd Maste     m_opaque_sp ()
47ac7ddfbfSEd Maste {
48ac7ddfbfSEd Maste     ModuleSP module_sp;
49ac7ddfbfSEd Maste     Error error = ModuleList::GetSharedModule (*module_spec.m_opaque_ap,
50ac7ddfbfSEd Maste                                                module_sp,
51ac7ddfbfSEd Maste                                                NULL,
52ac7ddfbfSEd Maste                                                NULL,
53ac7ddfbfSEd Maste                                                NULL);
54ac7ddfbfSEd Maste     if (module_sp)
55ac7ddfbfSEd Maste         SetSP(module_sp);
56ac7ddfbfSEd Maste }
57ac7ddfbfSEd Maste 
58ac7ddfbfSEd Maste SBModule::SBModule(const SBModule &rhs) :
59ac7ddfbfSEd Maste     m_opaque_sp (rhs.m_opaque_sp)
60ac7ddfbfSEd Maste {
61ac7ddfbfSEd Maste }
62ac7ddfbfSEd Maste 
63ac7ddfbfSEd Maste SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) :
64ac7ddfbfSEd Maste     m_opaque_sp ()
65ac7ddfbfSEd Maste {
66ac7ddfbfSEd Maste     ProcessSP process_sp (process.GetSP());
67ac7ddfbfSEd Maste     if (process_sp)
68ac7ddfbfSEd Maste     {
69ac7ddfbfSEd Maste         m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr);
70ac7ddfbfSEd Maste         if (m_opaque_sp)
71ac7ddfbfSEd Maste         {
72ac7ddfbfSEd Maste             Target &target = process_sp->GetTarget();
73ac7ddfbfSEd Maste             bool changed = false;
7412b93ac6SEd Maste             m_opaque_sp->SetLoadAddress(target, 0, true, changed);
75ac7ddfbfSEd Maste             target.GetImages().Append(m_opaque_sp);
76ac7ddfbfSEd Maste         }
77ac7ddfbfSEd Maste     }
78ac7ddfbfSEd Maste }
79ac7ddfbfSEd Maste 
80ac7ddfbfSEd Maste const SBModule &
81ac7ddfbfSEd Maste SBModule::operator = (const SBModule &rhs)
82ac7ddfbfSEd Maste {
83ac7ddfbfSEd Maste     if (this != &rhs)
84ac7ddfbfSEd Maste         m_opaque_sp = rhs.m_opaque_sp;
85ac7ddfbfSEd Maste     return *this;
86ac7ddfbfSEd Maste }
87ac7ddfbfSEd Maste 
88ac7ddfbfSEd Maste SBModule::~SBModule ()
89ac7ddfbfSEd Maste {
90ac7ddfbfSEd Maste }
91ac7ddfbfSEd Maste 
92ac7ddfbfSEd Maste bool
93ac7ddfbfSEd Maste SBModule::IsValid () const
94ac7ddfbfSEd Maste {
95ac7ddfbfSEd Maste     return m_opaque_sp.get() != NULL;
96ac7ddfbfSEd Maste }
97ac7ddfbfSEd Maste 
98ac7ddfbfSEd Maste void
99ac7ddfbfSEd Maste SBModule::Clear()
100ac7ddfbfSEd Maste {
101ac7ddfbfSEd Maste     m_opaque_sp.reset();
102ac7ddfbfSEd Maste }
103ac7ddfbfSEd Maste 
104ac7ddfbfSEd Maste SBFileSpec
105ac7ddfbfSEd Maste SBModule::GetFileSpec () const
106ac7ddfbfSEd Maste {
107ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
108ac7ddfbfSEd Maste 
109ac7ddfbfSEd Maste     SBFileSpec file_spec;
110ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
111ac7ddfbfSEd Maste     if (module_sp)
112ac7ddfbfSEd Maste         file_spec.SetFileSpec(module_sp->GetFileSpec());
113ac7ddfbfSEd Maste 
114ac7ddfbfSEd Maste     if (log)
115ac7ddfbfSEd Maste         log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
1160127ef0fSEd Maste                      static_cast<void*>(module_sp.get()),
1170127ef0fSEd Maste                      static_cast<const void*>(file_spec.get()));
118ac7ddfbfSEd Maste 
119ac7ddfbfSEd Maste     return file_spec;
120ac7ddfbfSEd Maste }
121ac7ddfbfSEd Maste 
122ac7ddfbfSEd Maste lldb::SBFileSpec
123ac7ddfbfSEd Maste SBModule::GetPlatformFileSpec () const
124ac7ddfbfSEd Maste {
125ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
126ac7ddfbfSEd Maste 
127ac7ddfbfSEd Maste     SBFileSpec file_spec;
128ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
129ac7ddfbfSEd Maste     if (module_sp)
130ac7ddfbfSEd Maste         file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
131ac7ddfbfSEd Maste 
132ac7ddfbfSEd Maste     if (log)
133ac7ddfbfSEd Maste         log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
1340127ef0fSEd Maste                      static_cast<void*>(module_sp.get()),
1350127ef0fSEd Maste                      static_cast<const void*>(file_spec.get()));
136ac7ddfbfSEd Maste 
137ac7ddfbfSEd Maste     return file_spec;
138ac7ddfbfSEd Maste }
139ac7ddfbfSEd Maste 
140ac7ddfbfSEd Maste bool
141ac7ddfbfSEd Maste SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
142ac7ddfbfSEd Maste {
143ac7ddfbfSEd Maste     bool result = false;
144ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
145ac7ddfbfSEd Maste 
146ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
147ac7ddfbfSEd Maste     if (module_sp)
148ac7ddfbfSEd Maste     {
149ac7ddfbfSEd Maste         module_sp->SetPlatformFileSpec(*platform_file);
150ac7ddfbfSEd Maste         result = true;
151ac7ddfbfSEd Maste     }
152ac7ddfbfSEd Maste 
153ac7ddfbfSEd Maste     if (log)
154ac7ddfbfSEd Maste         log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i",
1550127ef0fSEd Maste                      static_cast<void*>(module_sp.get()),
1560127ef0fSEd Maste                      static_cast<const void*>(platform_file.get()),
1570127ef0fSEd Maste                      platform_file->GetPath().c_str(), result);
158ac7ddfbfSEd Maste     return result;
159ac7ddfbfSEd Maste }
160ac7ddfbfSEd Maste 
161b952cd58SEd Maste lldb::SBFileSpec
162b952cd58SEd Maste SBModule::GetRemoteInstallFileSpec ()
163b952cd58SEd Maste {
164b952cd58SEd Maste     SBFileSpec sb_file_spec;
165b952cd58SEd Maste     ModuleSP module_sp (GetSP ());
166b952cd58SEd Maste     if (module_sp)
167b952cd58SEd Maste         sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec());
168b952cd58SEd Maste     return sb_file_spec;
169b952cd58SEd Maste }
170b952cd58SEd Maste 
171b952cd58SEd Maste bool
172b952cd58SEd Maste SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file)
173b952cd58SEd Maste {
174b952cd58SEd Maste     ModuleSP module_sp (GetSP ());
175b952cd58SEd Maste     if (module_sp)
176b952cd58SEd Maste     {
177b952cd58SEd Maste         module_sp->SetRemoteInstallFileSpec(file.ref());
178b952cd58SEd Maste         return true;
179b952cd58SEd Maste     }
180b952cd58SEd Maste     return false;
181b952cd58SEd Maste }
182ac7ddfbfSEd Maste 
183ac7ddfbfSEd Maste 
184ac7ddfbfSEd Maste const uint8_t *
185ac7ddfbfSEd Maste SBModule::GetUUIDBytes () const
186ac7ddfbfSEd Maste {
187ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
188ac7ddfbfSEd Maste 
189ac7ddfbfSEd Maste     const uint8_t *uuid_bytes = NULL;
190ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
191ac7ddfbfSEd Maste     if (module_sp)
192ac7ddfbfSEd Maste         uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
193ac7ddfbfSEd Maste 
194ac7ddfbfSEd Maste     if (log)
195ac7ddfbfSEd Maste     {
196ac7ddfbfSEd Maste         if (uuid_bytes)
197ac7ddfbfSEd Maste         {
198ac7ddfbfSEd Maste             StreamString s;
199ac7ddfbfSEd Maste             module_sp->GetUUID().Dump (&s);
2000127ef0fSEd Maste             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s",
2010127ef0fSEd Maste                          static_cast<void*>(module_sp.get()), s.GetData());
202ac7ddfbfSEd Maste         }
203ac7ddfbfSEd Maste         else
2040127ef0fSEd Maste             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL",
2050127ef0fSEd Maste                          static_cast<void*>(module_sp.get()));
206ac7ddfbfSEd Maste     }
207ac7ddfbfSEd Maste     return uuid_bytes;
208ac7ddfbfSEd Maste }
209ac7ddfbfSEd Maste 
210ac7ddfbfSEd Maste 
211ac7ddfbfSEd Maste const char *
212ac7ddfbfSEd Maste SBModule::GetUUIDString () const
213ac7ddfbfSEd Maste {
214ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
215ac7ddfbfSEd Maste 
2161c3bbb01SEd Maste     const char *uuid_cstr = NULL;
217ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
218ac7ddfbfSEd Maste     if (module_sp)
219ac7ddfbfSEd Maste     {
2201c3bbb01SEd Maste         // We are going to return a "const char *" value through the public
2211c3bbb01SEd Maste         // API, so we need to constify it so it gets added permanently the
2221c3bbb01SEd Maste         // string pool and then we don't need to worry about the lifetime of the
2231c3bbb01SEd Maste         // string as it will never go away once it has been put into the ConstString
2241c3bbb01SEd Maste         // string pool
2251c3bbb01SEd Maste         uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString();
2261c3bbb01SEd Maste     }
2271c3bbb01SEd Maste 
2281c3bbb01SEd Maste     if (uuid_cstr && uuid_cstr[0])
2291c3bbb01SEd Maste     {
2301c3bbb01SEd Maste         if (log)
2311c3bbb01SEd Maste             log->Printf ("SBModule(%p)::GetUUIDString () => %s", static_cast<void*>(module_sp.get()), uuid_cstr);
2321c3bbb01SEd Maste         return uuid_cstr;
233ac7ddfbfSEd Maste     }
234ac7ddfbfSEd Maste 
235ac7ddfbfSEd Maste     if (log)
2361c3bbb01SEd Maste         log->Printf ("SBModule(%p)::GetUUIDString () => NULL", static_cast<void*>(module_sp.get()));
2371c3bbb01SEd Maste     return NULL;
238ac7ddfbfSEd Maste }
239ac7ddfbfSEd Maste 
240ac7ddfbfSEd Maste 
241ac7ddfbfSEd Maste bool
242ac7ddfbfSEd Maste SBModule::operator == (const SBModule &rhs) const
243ac7ddfbfSEd Maste {
244ac7ddfbfSEd Maste     if (m_opaque_sp)
245ac7ddfbfSEd Maste         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
246ac7ddfbfSEd Maste     return false;
247ac7ddfbfSEd Maste }
248ac7ddfbfSEd Maste 
249ac7ddfbfSEd Maste bool
250ac7ddfbfSEd Maste SBModule::operator != (const SBModule &rhs) const
251ac7ddfbfSEd Maste {
252ac7ddfbfSEd Maste     if (m_opaque_sp)
253ac7ddfbfSEd Maste         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
254ac7ddfbfSEd Maste     return false;
255ac7ddfbfSEd Maste }
256ac7ddfbfSEd Maste 
257ac7ddfbfSEd Maste ModuleSP
258ac7ddfbfSEd Maste SBModule::GetSP () const
259ac7ddfbfSEd Maste {
260ac7ddfbfSEd Maste     return m_opaque_sp;
261ac7ddfbfSEd Maste }
262ac7ddfbfSEd Maste 
263ac7ddfbfSEd Maste void
264ac7ddfbfSEd Maste SBModule::SetSP (const ModuleSP &module_sp)
265ac7ddfbfSEd Maste {
266ac7ddfbfSEd Maste     m_opaque_sp = module_sp;
267ac7ddfbfSEd Maste }
268ac7ddfbfSEd Maste 
269ac7ddfbfSEd Maste SBAddress
270ac7ddfbfSEd Maste SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
271ac7ddfbfSEd Maste {
272ac7ddfbfSEd Maste     lldb::SBAddress sb_addr;
273ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
274ac7ddfbfSEd Maste     if (module_sp)
275ac7ddfbfSEd Maste     {
276ac7ddfbfSEd Maste         Address addr;
277ac7ddfbfSEd Maste         if (module_sp->ResolveFileAddress (vm_addr, addr))
278ac7ddfbfSEd Maste             sb_addr.ref() = addr;
279ac7ddfbfSEd Maste     }
280ac7ddfbfSEd Maste     return sb_addr;
281ac7ddfbfSEd Maste }
282ac7ddfbfSEd Maste 
283ac7ddfbfSEd Maste SBSymbolContext
284ac7ddfbfSEd Maste SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
285ac7ddfbfSEd Maste {
286ac7ddfbfSEd Maste     SBSymbolContext sb_sc;
287ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
288ac7ddfbfSEd Maste     if (module_sp && addr.IsValid())
289ac7ddfbfSEd Maste         module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
290ac7ddfbfSEd Maste     return sb_sc;
291ac7ddfbfSEd Maste }
292ac7ddfbfSEd Maste 
293ac7ddfbfSEd Maste bool
294ac7ddfbfSEd Maste SBModule::GetDescription (SBStream &description)
295ac7ddfbfSEd Maste {
296ac7ddfbfSEd Maste     Stream &strm = description.ref();
297ac7ddfbfSEd Maste 
298ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
299ac7ddfbfSEd Maste     if (module_sp)
300ac7ddfbfSEd Maste     {
301ac7ddfbfSEd Maste         module_sp->GetDescription (&strm);
302ac7ddfbfSEd Maste     }
303ac7ddfbfSEd Maste     else
304ac7ddfbfSEd Maste         strm.PutCString ("No value");
305ac7ddfbfSEd Maste 
306ac7ddfbfSEd Maste     return true;
307ac7ddfbfSEd Maste }
308ac7ddfbfSEd Maste 
309ac7ddfbfSEd Maste uint32_t
310ac7ddfbfSEd Maste SBModule::GetNumCompileUnits()
311ac7ddfbfSEd Maste {
312ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
313ac7ddfbfSEd Maste     if (module_sp)
314ac7ddfbfSEd Maste     {
315ac7ddfbfSEd Maste         return module_sp->GetNumCompileUnits ();
316ac7ddfbfSEd Maste     }
317ac7ddfbfSEd Maste     return 0;
318ac7ddfbfSEd Maste }
319ac7ddfbfSEd Maste 
320ac7ddfbfSEd Maste SBCompileUnit
321ac7ddfbfSEd Maste SBModule::GetCompileUnitAtIndex (uint32_t index)
322ac7ddfbfSEd Maste {
323ac7ddfbfSEd Maste     SBCompileUnit sb_cu;
324ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
325ac7ddfbfSEd Maste     if (module_sp)
326ac7ddfbfSEd Maste     {
327ac7ddfbfSEd Maste         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
328ac7ddfbfSEd Maste         sb_cu.reset(cu_sp.get());
329ac7ddfbfSEd Maste     }
330ac7ddfbfSEd Maste     return sb_cu;
331ac7ddfbfSEd Maste }
332ac7ddfbfSEd Maste 
333ac7ddfbfSEd Maste static Symtab *
334ac7ddfbfSEd Maste GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
335ac7ddfbfSEd Maste {
336ac7ddfbfSEd Maste     if (module_sp)
337ac7ddfbfSEd Maste     {
338ac7ddfbfSEd Maste         SymbolVendor *symbols = module_sp->GetSymbolVendor();
339ac7ddfbfSEd Maste         if (symbols)
340ac7ddfbfSEd Maste             return symbols->GetSymtab();
341ac7ddfbfSEd Maste     }
342ac7ddfbfSEd Maste     return NULL;
343ac7ddfbfSEd Maste }
344ac7ddfbfSEd Maste 
345ac7ddfbfSEd Maste size_t
346ac7ddfbfSEd Maste SBModule::GetNumSymbols ()
347ac7ddfbfSEd Maste {
348ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
349ac7ddfbfSEd Maste     if (module_sp)
350ac7ddfbfSEd Maste     {
351ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
352ac7ddfbfSEd Maste         if (symtab)
353ac7ddfbfSEd Maste             return symtab->GetNumSymbols();
354ac7ddfbfSEd Maste     }
355ac7ddfbfSEd Maste     return 0;
356ac7ddfbfSEd Maste }
357ac7ddfbfSEd Maste 
358ac7ddfbfSEd Maste SBSymbol
359ac7ddfbfSEd Maste SBModule::GetSymbolAtIndex (size_t idx)
360ac7ddfbfSEd Maste {
361ac7ddfbfSEd Maste     SBSymbol sb_symbol;
362ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
363ac7ddfbfSEd Maste     Symtab *symtab = GetUnifiedSymbolTable (module_sp);
364ac7ddfbfSEd Maste     if (symtab)
365ac7ddfbfSEd Maste         sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
366ac7ddfbfSEd Maste     return sb_symbol;
367ac7ddfbfSEd Maste }
368ac7ddfbfSEd Maste 
369ac7ddfbfSEd Maste lldb::SBSymbol
370ac7ddfbfSEd Maste SBModule::FindSymbol (const char *name,
371ac7ddfbfSEd Maste                       lldb::SymbolType symbol_type)
372ac7ddfbfSEd Maste {
373ac7ddfbfSEd Maste     SBSymbol sb_symbol;
374ac7ddfbfSEd Maste     if (name && name[0])
375ac7ddfbfSEd Maste     {
376ac7ddfbfSEd Maste         ModuleSP module_sp (GetSP ());
377ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
378ac7ddfbfSEd Maste         if (symtab)
379ac7ddfbfSEd Maste             sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
380ac7ddfbfSEd Maste     }
381ac7ddfbfSEd Maste     return sb_symbol;
382ac7ddfbfSEd Maste }
383ac7ddfbfSEd Maste 
384ac7ddfbfSEd Maste 
385ac7ddfbfSEd Maste lldb::SBSymbolContextList
386ac7ddfbfSEd Maste SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
387ac7ddfbfSEd Maste {
388ac7ddfbfSEd Maste     SBSymbolContextList sb_sc_list;
389ac7ddfbfSEd Maste     if (name && name[0])
390ac7ddfbfSEd Maste     {
391ac7ddfbfSEd Maste         ModuleSP module_sp (GetSP ());
392ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
393ac7ddfbfSEd Maste         if (symtab)
394ac7ddfbfSEd Maste         {
395ac7ddfbfSEd Maste             std::vector<uint32_t> matching_symbol_indexes;
396ac7ddfbfSEd Maste             const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
397ac7ddfbfSEd Maste             if (num_matches)
398ac7ddfbfSEd Maste             {
399ac7ddfbfSEd Maste                 SymbolContext sc;
400ac7ddfbfSEd Maste                 sc.module_sp = module_sp;
401ac7ddfbfSEd Maste                 SymbolContextList &sc_list = *sb_sc_list;
402ac7ddfbfSEd Maste                 for (size_t i=0; i<num_matches; ++i)
403ac7ddfbfSEd Maste                 {
404ac7ddfbfSEd Maste                     sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
405ac7ddfbfSEd Maste                     if (sc.symbol)
406ac7ddfbfSEd Maste                         sc_list.Append(sc);
407ac7ddfbfSEd Maste                 }
408ac7ddfbfSEd Maste             }
409ac7ddfbfSEd Maste         }
410ac7ddfbfSEd Maste     }
411ac7ddfbfSEd Maste     return sb_sc_list;
412ac7ddfbfSEd Maste 
413ac7ddfbfSEd Maste }
414ac7ddfbfSEd Maste 
415ac7ddfbfSEd Maste 
416ac7ddfbfSEd Maste 
417ac7ddfbfSEd Maste size_t
418ac7ddfbfSEd Maste SBModule::GetNumSections ()
419ac7ddfbfSEd Maste {
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         if (section_list)
427ac7ddfbfSEd Maste             return section_list->GetSize();
428ac7ddfbfSEd Maste     }
429ac7ddfbfSEd Maste     return 0;
430ac7ddfbfSEd Maste }
431ac7ddfbfSEd Maste 
432ac7ddfbfSEd Maste SBSection
433ac7ddfbfSEd Maste SBModule::GetSectionAtIndex (size_t idx)
434ac7ddfbfSEd Maste {
435ac7ddfbfSEd Maste     SBSection sb_section;
436ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
437ac7ddfbfSEd Maste     if (module_sp)
438ac7ddfbfSEd Maste     {
439ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
440ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
441ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList ();
442ac7ddfbfSEd Maste 
443ac7ddfbfSEd Maste         if (section_list)
444ac7ddfbfSEd Maste             sb_section.SetSP(section_list->GetSectionAtIndex (idx));
445ac7ddfbfSEd Maste     }
446ac7ddfbfSEd Maste     return sb_section;
447ac7ddfbfSEd Maste }
448ac7ddfbfSEd Maste 
449ac7ddfbfSEd Maste lldb::SBSymbolContextList
450ac7ddfbfSEd Maste SBModule::FindFunctions (const char *name,
451ac7ddfbfSEd Maste                          uint32_t name_type_mask)
452ac7ddfbfSEd Maste {
453ac7ddfbfSEd Maste     lldb::SBSymbolContextList sb_sc_list;
454ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
455ac7ddfbfSEd Maste     if (name && module_sp)
456ac7ddfbfSEd Maste     {
457ac7ddfbfSEd Maste         const bool append = true;
458ac7ddfbfSEd Maste         const bool symbols_ok = true;
459ac7ddfbfSEd Maste         const bool inlines_ok = true;
460ac7ddfbfSEd Maste         module_sp->FindFunctions (ConstString(name),
461ac7ddfbfSEd Maste                                   NULL,
462ac7ddfbfSEd Maste                                   name_type_mask,
463ac7ddfbfSEd Maste                                   symbols_ok,
464ac7ddfbfSEd Maste                                   inlines_ok,
465ac7ddfbfSEd Maste                                   append,
466ac7ddfbfSEd Maste                                   *sb_sc_list);
467ac7ddfbfSEd Maste     }
468ac7ddfbfSEd Maste     return sb_sc_list;
469ac7ddfbfSEd Maste }
470ac7ddfbfSEd Maste 
471ac7ddfbfSEd Maste 
472ac7ddfbfSEd Maste SBValueList
473ac7ddfbfSEd Maste SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
474ac7ddfbfSEd Maste {
475ac7ddfbfSEd Maste     SBValueList sb_value_list;
476ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
477ac7ddfbfSEd Maste     if (name && module_sp)
478ac7ddfbfSEd Maste     {
479ac7ddfbfSEd Maste         VariableList variable_list;
480ac7ddfbfSEd Maste         const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
481ac7ddfbfSEd Maste                                                                      NULL,
482ac7ddfbfSEd Maste                                                                      false,
483ac7ddfbfSEd Maste                                                                      max_matches,
484ac7ddfbfSEd Maste                                                                      variable_list);
485ac7ddfbfSEd Maste 
486ac7ddfbfSEd Maste         if (match_count > 0)
487ac7ddfbfSEd Maste         {
488ac7ddfbfSEd Maste             for (uint32_t i=0; i<match_count; ++i)
489ac7ddfbfSEd Maste             {
490ac7ddfbfSEd Maste                 lldb::ValueObjectSP valobj_sp;
491ac7ddfbfSEd Maste                 TargetSP target_sp (target.GetSP());
492ac7ddfbfSEd Maste                 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
493ac7ddfbfSEd Maste                 if (valobj_sp)
494ac7ddfbfSEd Maste                     sb_value_list.Append(SBValue(valobj_sp));
495ac7ddfbfSEd Maste             }
496ac7ddfbfSEd Maste         }
497ac7ddfbfSEd Maste     }
498ac7ddfbfSEd Maste 
499ac7ddfbfSEd Maste     return sb_value_list;
500ac7ddfbfSEd Maste }
501ac7ddfbfSEd Maste 
502ac7ddfbfSEd Maste lldb::SBValue
503ac7ddfbfSEd Maste SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
504ac7ddfbfSEd Maste {
505ac7ddfbfSEd Maste     SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
506ac7ddfbfSEd Maste     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
507ac7ddfbfSEd Maste         return sb_value_list.GetValueAtIndex(0);
508ac7ddfbfSEd Maste     return SBValue();
509ac7ddfbfSEd Maste }
510ac7ddfbfSEd Maste 
511ac7ddfbfSEd Maste lldb::SBType
512ac7ddfbfSEd Maste SBModule::FindFirstType (const char *name_cstr)
513ac7ddfbfSEd Maste {
514ac7ddfbfSEd Maste     SBType sb_type;
515ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
516ac7ddfbfSEd Maste     if (name_cstr && module_sp)
517ac7ddfbfSEd Maste     {
518ac7ddfbfSEd Maste         SymbolContext sc;
519ac7ddfbfSEd Maste         const bool exact_match = false;
520ac7ddfbfSEd Maste         ConstString name(name_cstr);
521ac7ddfbfSEd Maste 
522ac7ddfbfSEd Maste         sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
523ac7ddfbfSEd Maste 
524ac7ddfbfSEd Maste         if (!sb_type.IsValid())
5259f2f44ceSEd Maste         {
5269f2f44ceSEd Maste             TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
5279f2f44ceSEd Maste             if (type_system)
5289f2f44ceSEd Maste                 sb_type = SBType (type_system->GetBuiltinTypeByName(name));
5299f2f44ceSEd Maste         }
530ac7ddfbfSEd Maste     }
531ac7ddfbfSEd Maste     return sb_type;
532ac7ddfbfSEd Maste }
533ac7ddfbfSEd Maste 
534ac7ddfbfSEd Maste lldb::SBType
535ac7ddfbfSEd Maste SBModule::GetBasicType(lldb::BasicType type)
536ac7ddfbfSEd Maste {
537ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
538ac7ddfbfSEd Maste     if (module_sp)
5399f2f44ceSEd Maste     {
5409f2f44ceSEd Maste         TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
5419f2f44ceSEd Maste         if (type_system)
5429f2f44ceSEd Maste             return SBType (type_system->GetBasicTypeFromAST(type));
5439f2f44ceSEd Maste     }
544ac7ddfbfSEd Maste     return SBType();
545ac7ddfbfSEd Maste }
546ac7ddfbfSEd Maste 
547ac7ddfbfSEd Maste lldb::SBTypeList
548ac7ddfbfSEd Maste SBModule::FindTypes (const char *type)
549ac7ddfbfSEd Maste {
550ac7ddfbfSEd Maste     SBTypeList retval;
551ac7ddfbfSEd Maste 
552ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
553ac7ddfbfSEd Maste     if (type && module_sp)
554ac7ddfbfSEd Maste     {
555ac7ddfbfSEd Maste         SymbolContext sc;
556ac7ddfbfSEd Maste         TypeList type_list;
557ac7ddfbfSEd Maste         const bool exact_match = false;
558ac7ddfbfSEd Maste         ConstString name(type);
5594bb0738eSEd Maste         llvm::DenseSet<SymbolFile *> searched_symbol_files;
560ac7ddfbfSEd Maste         const uint32_t num_matches = module_sp->FindTypes (sc,
561ac7ddfbfSEd Maste                                                            name,
562ac7ddfbfSEd Maste                                                            exact_match,
563ac7ddfbfSEd Maste                                                            UINT32_MAX,
5644bb0738eSEd Maste                                                            searched_symbol_files,
565ac7ddfbfSEd Maste                                                            type_list);
566ac7ddfbfSEd Maste 
567ac7ddfbfSEd Maste         if (num_matches > 0)
568ac7ddfbfSEd Maste         {
569ac7ddfbfSEd Maste             for (size_t idx = 0; idx < num_matches; idx++)
570ac7ddfbfSEd Maste             {
571ac7ddfbfSEd Maste                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
572ac7ddfbfSEd Maste                 if (type_sp)
573ac7ddfbfSEd Maste                     retval.Append(SBType(type_sp));
574ac7ddfbfSEd Maste             }
575ac7ddfbfSEd Maste         }
576ac7ddfbfSEd Maste         else
577ac7ddfbfSEd Maste         {
5789f2f44ceSEd Maste             TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
5799f2f44ceSEd Maste             if (type_system)
5809f2f44ceSEd Maste             {
5819f2f44ceSEd Maste                 CompilerType compiler_type = type_system->GetBuiltinTypeByName(name);
5829f2f44ceSEd Maste                 if (compiler_type)
5839f2f44ceSEd Maste                     retval.Append(SBType(compiler_type));
5849f2f44ceSEd Maste             }
585ac7ddfbfSEd Maste         }
586ac7ddfbfSEd Maste     }
587ac7ddfbfSEd Maste 
588ac7ddfbfSEd Maste     return retval;
589ac7ddfbfSEd Maste }
590ac7ddfbfSEd Maste 
59112b93ac6SEd Maste lldb::SBType
59212b93ac6SEd Maste SBModule::GetTypeByID (lldb::user_id_t uid)
59312b93ac6SEd Maste {
59412b93ac6SEd Maste     ModuleSP module_sp (GetSP ());
59512b93ac6SEd Maste     if (module_sp)
59612b93ac6SEd Maste     {
59712b93ac6SEd Maste         SymbolVendor* vendor = module_sp->GetSymbolVendor();
59812b93ac6SEd Maste         if (vendor)
59912b93ac6SEd Maste         {
60012b93ac6SEd Maste             Type *type_ptr = vendor->ResolveTypeUID(uid);
60112b93ac6SEd Maste             if (type_ptr)
60212b93ac6SEd Maste                 return SBType(type_ptr->shared_from_this());
60312b93ac6SEd Maste         }
60412b93ac6SEd Maste     }
60512b93ac6SEd Maste     return SBType();
60612b93ac6SEd Maste }
60712b93ac6SEd Maste 
608ac7ddfbfSEd Maste lldb::SBTypeList
609ac7ddfbfSEd Maste SBModule::GetTypes (uint32_t type_mask)
610ac7ddfbfSEd Maste {
611ac7ddfbfSEd Maste     SBTypeList sb_type_list;
612ac7ddfbfSEd Maste 
613ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
614ac7ddfbfSEd Maste     if (module_sp)
615ac7ddfbfSEd Maste     {
616ac7ddfbfSEd Maste         SymbolVendor* vendor = module_sp->GetSymbolVendor();
617ac7ddfbfSEd Maste         if (vendor)
618ac7ddfbfSEd Maste         {
619ac7ddfbfSEd Maste             TypeList type_list;
620ac7ddfbfSEd Maste             vendor->GetTypes (NULL, type_mask, type_list);
621ac7ddfbfSEd Maste             sb_type_list.m_opaque_ap->Append(type_list);
622ac7ddfbfSEd Maste         }
623ac7ddfbfSEd Maste     }
624ac7ddfbfSEd Maste     return sb_type_list;
625ac7ddfbfSEd Maste }
626ac7ddfbfSEd Maste 
627ac7ddfbfSEd Maste SBSection
628ac7ddfbfSEd Maste SBModule::FindSection (const char *sect_name)
629ac7ddfbfSEd Maste {
630ac7ddfbfSEd Maste     SBSection sb_section;
631ac7ddfbfSEd Maste 
632ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
633ac7ddfbfSEd Maste     if (sect_name && module_sp)
634ac7ddfbfSEd Maste     {
635ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
636ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
637ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList();
638ac7ddfbfSEd Maste         if (section_list)
639ac7ddfbfSEd Maste         {
640ac7ddfbfSEd Maste             ConstString const_sect_name(sect_name);
641ac7ddfbfSEd Maste             SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
642ac7ddfbfSEd Maste             if (section_sp)
643ac7ddfbfSEd Maste             {
644ac7ddfbfSEd Maste                 sb_section.SetSP (section_sp);
645ac7ddfbfSEd Maste             }
646ac7ddfbfSEd Maste         }
647ac7ddfbfSEd Maste     }
648ac7ddfbfSEd Maste     return sb_section;
649ac7ddfbfSEd Maste }
650ac7ddfbfSEd Maste 
651ac7ddfbfSEd Maste lldb::ByteOrder
652ac7ddfbfSEd Maste SBModule::GetByteOrder ()
653ac7ddfbfSEd Maste {
654ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
655ac7ddfbfSEd Maste     if (module_sp)
656ac7ddfbfSEd Maste         return module_sp->GetArchitecture().GetByteOrder();
657ac7ddfbfSEd Maste     return eByteOrderInvalid;
658ac7ddfbfSEd Maste }
659ac7ddfbfSEd Maste 
660ac7ddfbfSEd Maste const char *
661ac7ddfbfSEd Maste SBModule::GetTriple ()
662ac7ddfbfSEd Maste {
663ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
664ac7ddfbfSEd Maste     if (module_sp)
665ac7ddfbfSEd Maste     {
666ac7ddfbfSEd Maste         std::string triple (module_sp->GetArchitecture().GetTriple().str());
667ac7ddfbfSEd Maste         // Unique the string so we don't run into ownership issues since
668ac7ddfbfSEd Maste         // the const strings put the string into the string pool once and
669ac7ddfbfSEd Maste         // the strings never comes out
670ac7ddfbfSEd Maste         ConstString const_triple (triple.c_str());
671ac7ddfbfSEd Maste         return const_triple.GetCString();
672ac7ddfbfSEd Maste     }
673ac7ddfbfSEd Maste     return NULL;
674ac7ddfbfSEd Maste }
675ac7ddfbfSEd Maste 
676ac7ddfbfSEd Maste uint32_t
677ac7ddfbfSEd Maste SBModule::GetAddressByteSize()
678ac7ddfbfSEd Maste {
679ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
680ac7ddfbfSEd Maste     if (module_sp)
681ac7ddfbfSEd Maste         return module_sp->GetArchitecture().GetAddressByteSize();
682ac7ddfbfSEd Maste     return sizeof(void*);
683ac7ddfbfSEd Maste }
684ac7ddfbfSEd Maste 
685ac7ddfbfSEd Maste 
686ac7ddfbfSEd Maste uint32_t
687ac7ddfbfSEd Maste SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
688ac7ddfbfSEd Maste {
689ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
690ac7ddfbfSEd Maste     if (module_sp)
691ac7ddfbfSEd Maste         return module_sp->GetVersion(versions, num_versions);
692ac7ddfbfSEd Maste     else
693ac7ddfbfSEd Maste     {
694ac7ddfbfSEd Maste         if (versions && num_versions)
695ac7ddfbfSEd Maste         {
696ac7ddfbfSEd Maste             for (uint32_t i=0; i<num_versions; ++i)
697ac7ddfbfSEd Maste                 versions[i] = UINT32_MAX;
698ac7ddfbfSEd Maste         }
699ac7ddfbfSEd Maste         return 0;
700ac7ddfbfSEd Maste     }
701ac7ddfbfSEd Maste }
702ac7ddfbfSEd Maste 
7031c3bbb01SEd Maste lldb::SBFileSpec
7041c3bbb01SEd Maste SBModule::GetSymbolFileSpec() const
7051c3bbb01SEd Maste {
7061c3bbb01SEd Maste     lldb::SBFileSpec sb_file_spec;
7071c3bbb01SEd Maste     ModuleSP module_sp(GetSP());
7081c3bbb01SEd Maste     if (module_sp)
7091c3bbb01SEd Maste     {
7101c3bbb01SEd Maste         SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor();
7111c3bbb01SEd Maste         if (symbol_vendor_ptr)
7121c3bbb01SEd Maste             sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec());
7131c3bbb01SEd Maste     }
7141c3bbb01SEd Maste     return sb_file_spec;
7151c3bbb01SEd Maste }
7161c3bbb01SEd Maste 
7171c3bbb01SEd Maste lldb::SBAddress
7181c3bbb01SEd Maste SBModule::GetObjectFileHeaderAddress() const
7191c3bbb01SEd Maste {
7201c3bbb01SEd Maste     lldb::SBAddress sb_addr;
7211c3bbb01SEd Maste     ModuleSP module_sp (GetSP ());
7221c3bbb01SEd Maste     if (module_sp)
7231c3bbb01SEd Maste     {
7241c3bbb01SEd Maste         ObjectFile *objfile_ptr = module_sp->GetObjectFile();
7251c3bbb01SEd Maste         if (objfile_ptr)
7261c3bbb01SEd Maste             sb_addr.ref() = objfile_ptr->GetHeaderAddress();
7271c3bbb01SEd Maste     }
7281c3bbb01SEd Maste     return sb_addr;
7291c3bbb01SEd Maste }
730