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 
165b952cd58SEd Maste lldb::SBFileSpec
166b952cd58SEd Maste SBModule::GetRemoteInstallFileSpec ()
167b952cd58SEd Maste {
168b952cd58SEd Maste     SBFileSpec sb_file_spec;
169b952cd58SEd Maste     ModuleSP module_sp (GetSP ());
170b952cd58SEd Maste     if (module_sp)
171b952cd58SEd Maste         sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec());
172b952cd58SEd Maste     return sb_file_spec;
173b952cd58SEd Maste }
174b952cd58SEd Maste 
175b952cd58SEd Maste bool
176b952cd58SEd Maste SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file)
177b952cd58SEd Maste {
178b952cd58SEd Maste     ModuleSP module_sp (GetSP ());
179b952cd58SEd Maste     if (module_sp)
180b952cd58SEd Maste     {
181b952cd58SEd Maste         module_sp->SetRemoteInstallFileSpec(file.ref());
182b952cd58SEd Maste         return true;
183b952cd58SEd Maste     }
184b952cd58SEd Maste     return false;
185b952cd58SEd Maste }
186ac7ddfbfSEd Maste 
187ac7ddfbfSEd Maste 
188ac7ddfbfSEd Maste const uint8_t *
189ac7ddfbfSEd Maste SBModule::GetUUIDBytes () const
190ac7ddfbfSEd Maste {
191ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
192ac7ddfbfSEd Maste 
193ac7ddfbfSEd Maste     const uint8_t *uuid_bytes = NULL;
194ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
195ac7ddfbfSEd Maste     if (module_sp)
196ac7ddfbfSEd Maste         uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
197ac7ddfbfSEd Maste 
198ac7ddfbfSEd Maste     if (log)
199ac7ddfbfSEd Maste     {
200ac7ddfbfSEd Maste         if (uuid_bytes)
201ac7ddfbfSEd Maste         {
202ac7ddfbfSEd Maste             StreamString s;
203ac7ddfbfSEd Maste             module_sp->GetUUID().Dump (&s);
204ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", module_sp.get(), s.GetData());
205ac7ddfbfSEd Maste         }
206ac7ddfbfSEd Maste         else
207ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", module_sp.get());
208ac7ddfbfSEd Maste     }
209ac7ddfbfSEd Maste     return uuid_bytes;
210ac7ddfbfSEd Maste }
211ac7ddfbfSEd Maste 
212ac7ddfbfSEd Maste 
213ac7ddfbfSEd Maste const char *
214ac7ddfbfSEd Maste SBModule::GetUUIDString () const
215ac7ddfbfSEd Maste {
216ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
217ac7ddfbfSEd Maste 
218ac7ddfbfSEd Maste     static char uuid_string_buffer[80];
219ac7ddfbfSEd Maste     const char *uuid_c_string = NULL;
220ac7ddfbfSEd Maste     std::string uuid_string;
221ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
222ac7ddfbfSEd Maste     if (module_sp)
223ac7ddfbfSEd Maste         uuid_string = module_sp->GetUUID().GetAsString();
224ac7ddfbfSEd Maste 
225ac7ddfbfSEd Maste     if (!uuid_string.empty())
226ac7ddfbfSEd Maste     {
227ac7ddfbfSEd Maste         strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer));
228ac7ddfbfSEd Maste         uuid_c_string = uuid_string_buffer;
229ac7ddfbfSEd Maste     }
230ac7ddfbfSEd Maste 
231ac7ddfbfSEd Maste     if (log)
232ac7ddfbfSEd Maste     {
233ac7ddfbfSEd Maste         if (!uuid_string.empty())
234ac7ddfbfSEd Maste         {
235ac7ddfbfSEd Maste             StreamString s;
236ac7ddfbfSEd Maste             module_sp->GetUUID().Dump (&s);
237ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDString () => %s", module_sp.get(), s.GetData());
238ac7ddfbfSEd Maste         }
239ac7ddfbfSEd Maste         else
240ac7ddfbfSEd Maste             log->Printf ("SBModule(%p)::GetUUIDString () => NULL", module_sp.get());
241ac7ddfbfSEd Maste     }
242ac7ddfbfSEd Maste     return uuid_c_string;
243ac7ddfbfSEd Maste }
244ac7ddfbfSEd Maste 
245ac7ddfbfSEd Maste 
246ac7ddfbfSEd Maste bool
247ac7ddfbfSEd Maste SBModule::operator == (const SBModule &rhs) const
248ac7ddfbfSEd Maste {
249ac7ddfbfSEd Maste     if (m_opaque_sp)
250ac7ddfbfSEd Maste         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
251ac7ddfbfSEd Maste     return false;
252ac7ddfbfSEd Maste }
253ac7ddfbfSEd Maste 
254ac7ddfbfSEd Maste bool
255ac7ddfbfSEd Maste SBModule::operator != (const SBModule &rhs) const
256ac7ddfbfSEd Maste {
257ac7ddfbfSEd Maste     if (m_opaque_sp)
258ac7ddfbfSEd Maste         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
259ac7ddfbfSEd Maste     return false;
260ac7ddfbfSEd Maste }
261ac7ddfbfSEd Maste 
262ac7ddfbfSEd Maste ModuleSP
263ac7ddfbfSEd Maste SBModule::GetSP () const
264ac7ddfbfSEd Maste {
265ac7ddfbfSEd Maste     return m_opaque_sp;
266ac7ddfbfSEd Maste }
267ac7ddfbfSEd Maste 
268ac7ddfbfSEd Maste void
269ac7ddfbfSEd Maste SBModule::SetSP (const ModuleSP &module_sp)
270ac7ddfbfSEd Maste {
271ac7ddfbfSEd Maste     m_opaque_sp = module_sp;
272ac7ddfbfSEd Maste }
273ac7ddfbfSEd Maste 
274ac7ddfbfSEd Maste SBAddress
275ac7ddfbfSEd Maste SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
276ac7ddfbfSEd Maste {
277ac7ddfbfSEd Maste     lldb::SBAddress sb_addr;
278ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
279ac7ddfbfSEd Maste     if (module_sp)
280ac7ddfbfSEd Maste     {
281ac7ddfbfSEd Maste         Address addr;
282ac7ddfbfSEd Maste         if (module_sp->ResolveFileAddress (vm_addr, addr))
283ac7ddfbfSEd Maste             sb_addr.ref() = addr;
284ac7ddfbfSEd Maste     }
285ac7ddfbfSEd Maste     return sb_addr;
286ac7ddfbfSEd Maste }
287ac7ddfbfSEd Maste 
288ac7ddfbfSEd Maste SBSymbolContext
289ac7ddfbfSEd Maste SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
290ac7ddfbfSEd Maste {
291ac7ddfbfSEd Maste     SBSymbolContext sb_sc;
292ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
293ac7ddfbfSEd Maste     if (module_sp && addr.IsValid())
294ac7ddfbfSEd Maste         module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
295ac7ddfbfSEd Maste     return sb_sc;
296ac7ddfbfSEd Maste }
297ac7ddfbfSEd Maste 
298ac7ddfbfSEd Maste bool
299ac7ddfbfSEd Maste SBModule::GetDescription (SBStream &description)
300ac7ddfbfSEd Maste {
301ac7ddfbfSEd Maste     Stream &strm = description.ref();
302ac7ddfbfSEd Maste 
303ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
304ac7ddfbfSEd Maste     if (module_sp)
305ac7ddfbfSEd Maste     {
306ac7ddfbfSEd Maste         module_sp->GetDescription (&strm);
307ac7ddfbfSEd Maste     }
308ac7ddfbfSEd Maste     else
309ac7ddfbfSEd Maste         strm.PutCString ("No value");
310ac7ddfbfSEd Maste 
311ac7ddfbfSEd Maste     return true;
312ac7ddfbfSEd Maste }
313ac7ddfbfSEd Maste 
314ac7ddfbfSEd Maste uint32_t
315ac7ddfbfSEd Maste SBModule::GetNumCompileUnits()
316ac7ddfbfSEd Maste {
317ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
318ac7ddfbfSEd Maste     if (module_sp)
319ac7ddfbfSEd Maste     {
320ac7ddfbfSEd Maste         return module_sp->GetNumCompileUnits ();
321ac7ddfbfSEd Maste     }
322ac7ddfbfSEd Maste     return 0;
323ac7ddfbfSEd Maste }
324ac7ddfbfSEd Maste 
325ac7ddfbfSEd Maste SBCompileUnit
326ac7ddfbfSEd Maste SBModule::GetCompileUnitAtIndex (uint32_t index)
327ac7ddfbfSEd Maste {
328ac7ddfbfSEd Maste     SBCompileUnit sb_cu;
329ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
330ac7ddfbfSEd Maste     if (module_sp)
331ac7ddfbfSEd Maste     {
332ac7ddfbfSEd Maste         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
333ac7ddfbfSEd Maste         sb_cu.reset(cu_sp.get());
334ac7ddfbfSEd Maste     }
335ac7ddfbfSEd Maste     return sb_cu;
336ac7ddfbfSEd Maste }
337ac7ddfbfSEd Maste 
338ac7ddfbfSEd Maste static Symtab *
339ac7ddfbfSEd Maste GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
340ac7ddfbfSEd Maste {
341ac7ddfbfSEd Maste     if (module_sp)
342ac7ddfbfSEd Maste     {
343ac7ddfbfSEd Maste         SymbolVendor *symbols = module_sp->GetSymbolVendor();
344ac7ddfbfSEd Maste         if (symbols)
345ac7ddfbfSEd Maste             return symbols->GetSymtab();
346ac7ddfbfSEd Maste     }
347ac7ddfbfSEd Maste     return NULL;
348ac7ddfbfSEd Maste }
349ac7ddfbfSEd Maste 
350ac7ddfbfSEd Maste size_t
351ac7ddfbfSEd Maste SBModule::GetNumSymbols ()
352ac7ddfbfSEd Maste {
353ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
354ac7ddfbfSEd Maste     if (module_sp)
355ac7ddfbfSEd Maste     {
356ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
357ac7ddfbfSEd Maste         if (symtab)
358ac7ddfbfSEd Maste             return symtab->GetNumSymbols();
359ac7ddfbfSEd Maste     }
360ac7ddfbfSEd Maste     return 0;
361ac7ddfbfSEd Maste }
362ac7ddfbfSEd Maste 
363ac7ddfbfSEd Maste SBSymbol
364ac7ddfbfSEd Maste SBModule::GetSymbolAtIndex (size_t idx)
365ac7ddfbfSEd Maste {
366ac7ddfbfSEd Maste     SBSymbol sb_symbol;
367ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
368ac7ddfbfSEd Maste     Symtab *symtab = GetUnifiedSymbolTable (module_sp);
369ac7ddfbfSEd Maste     if (symtab)
370ac7ddfbfSEd Maste         sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
371ac7ddfbfSEd Maste     return sb_symbol;
372ac7ddfbfSEd Maste }
373ac7ddfbfSEd Maste 
374ac7ddfbfSEd Maste lldb::SBSymbol
375ac7ddfbfSEd Maste SBModule::FindSymbol (const char *name,
376ac7ddfbfSEd Maste                       lldb::SymbolType symbol_type)
377ac7ddfbfSEd Maste {
378ac7ddfbfSEd Maste     SBSymbol sb_symbol;
379ac7ddfbfSEd Maste     if (name && name[0])
380ac7ddfbfSEd Maste     {
381ac7ddfbfSEd Maste         ModuleSP module_sp (GetSP ());
382ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
383ac7ddfbfSEd Maste         if (symtab)
384ac7ddfbfSEd Maste             sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
385ac7ddfbfSEd Maste     }
386ac7ddfbfSEd Maste     return sb_symbol;
387ac7ddfbfSEd Maste }
388ac7ddfbfSEd Maste 
389ac7ddfbfSEd Maste 
390ac7ddfbfSEd Maste lldb::SBSymbolContextList
391ac7ddfbfSEd Maste SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
392ac7ddfbfSEd Maste {
393ac7ddfbfSEd Maste     SBSymbolContextList sb_sc_list;
394ac7ddfbfSEd Maste     if (name && name[0])
395ac7ddfbfSEd Maste     {
396ac7ddfbfSEd Maste         ModuleSP module_sp (GetSP ());
397ac7ddfbfSEd Maste         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
398ac7ddfbfSEd Maste         if (symtab)
399ac7ddfbfSEd Maste         {
400ac7ddfbfSEd Maste             std::vector<uint32_t> matching_symbol_indexes;
401ac7ddfbfSEd Maste             const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
402ac7ddfbfSEd Maste             if (num_matches)
403ac7ddfbfSEd Maste             {
404ac7ddfbfSEd Maste                 SymbolContext sc;
405ac7ddfbfSEd Maste                 sc.module_sp = module_sp;
406ac7ddfbfSEd Maste                 SymbolContextList &sc_list = *sb_sc_list;
407ac7ddfbfSEd Maste                 for (size_t i=0; i<num_matches; ++i)
408ac7ddfbfSEd Maste                 {
409ac7ddfbfSEd Maste                     sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
410ac7ddfbfSEd Maste                     if (sc.symbol)
411ac7ddfbfSEd Maste                         sc_list.Append(sc);
412ac7ddfbfSEd Maste                 }
413ac7ddfbfSEd Maste             }
414ac7ddfbfSEd Maste         }
415ac7ddfbfSEd Maste     }
416ac7ddfbfSEd Maste     return sb_sc_list;
417ac7ddfbfSEd Maste 
418ac7ddfbfSEd Maste }
419ac7ddfbfSEd Maste 
420ac7ddfbfSEd Maste 
421ac7ddfbfSEd Maste 
422ac7ddfbfSEd Maste size_t
423ac7ddfbfSEd Maste SBModule::GetNumSections ()
424ac7ddfbfSEd Maste {
425ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
426ac7ddfbfSEd Maste     if (module_sp)
427ac7ddfbfSEd Maste     {
428ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
429ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
430ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList();
431ac7ddfbfSEd Maste         if (section_list)
432ac7ddfbfSEd Maste             return section_list->GetSize();
433ac7ddfbfSEd Maste     }
434ac7ddfbfSEd Maste     return 0;
435ac7ddfbfSEd Maste }
436ac7ddfbfSEd Maste 
437ac7ddfbfSEd Maste SBSection
438ac7ddfbfSEd Maste SBModule::GetSectionAtIndex (size_t idx)
439ac7ddfbfSEd Maste {
440ac7ddfbfSEd Maste     SBSection sb_section;
441ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
442ac7ddfbfSEd Maste     if (module_sp)
443ac7ddfbfSEd Maste     {
444ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
445ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
446ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList ();
447ac7ddfbfSEd Maste 
448ac7ddfbfSEd Maste         if (section_list)
449ac7ddfbfSEd Maste             sb_section.SetSP(section_list->GetSectionAtIndex (idx));
450ac7ddfbfSEd Maste     }
451ac7ddfbfSEd Maste     return sb_section;
452ac7ddfbfSEd Maste }
453ac7ddfbfSEd Maste 
454ac7ddfbfSEd Maste lldb::SBSymbolContextList
455ac7ddfbfSEd Maste SBModule::FindFunctions (const char *name,
456ac7ddfbfSEd Maste                          uint32_t name_type_mask)
457ac7ddfbfSEd Maste {
458ac7ddfbfSEd Maste     lldb::SBSymbolContextList sb_sc_list;
459ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
460ac7ddfbfSEd Maste     if (name && module_sp)
461ac7ddfbfSEd Maste     {
462ac7ddfbfSEd Maste         const bool append = true;
463ac7ddfbfSEd Maste         const bool symbols_ok = true;
464ac7ddfbfSEd Maste         const bool inlines_ok = true;
465ac7ddfbfSEd Maste         module_sp->FindFunctions (ConstString(name),
466ac7ddfbfSEd Maste                                   NULL,
467ac7ddfbfSEd Maste                                   name_type_mask,
468ac7ddfbfSEd Maste                                   symbols_ok,
469ac7ddfbfSEd Maste                                   inlines_ok,
470ac7ddfbfSEd Maste                                   append,
471ac7ddfbfSEd Maste                                   *sb_sc_list);
472ac7ddfbfSEd Maste     }
473ac7ddfbfSEd Maste     return sb_sc_list;
474ac7ddfbfSEd Maste }
475ac7ddfbfSEd Maste 
476ac7ddfbfSEd Maste 
477ac7ddfbfSEd Maste SBValueList
478ac7ddfbfSEd Maste SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
479ac7ddfbfSEd Maste {
480ac7ddfbfSEd Maste     SBValueList sb_value_list;
481ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
482ac7ddfbfSEd Maste     if (name && module_sp)
483ac7ddfbfSEd Maste     {
484ac7ddfbfSEd Maste         VariableList variable_list;
485ac7ddfbfSEd Maste         const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
486ac7ddfbfSEd Maste                                                                      NULL,
487ac7ddfbfSEd Maste                                                                      false,
488ac7ddfbfSEd Maste                                                                      max_matches,
489ac7ddfbfSEd Maste                                                                      variable_list);
490ac7ddfbfSEd Maste 
491ac7ddfbfSEd Maste         if (match_count > 0)
492ac7ddfbfSEd Maste         {
493ac7ddfbfSEd Maste             for (uint32_t i=0; i<match_count; ++i)
494ac7ddfbfSEd Maste             {
495ac7ddfbfSEd Maste                 lldb::ValueObjectSP valobj_sp;
496ac7ddfbfSEd Maste                 TargetSP target_sp (target.GetSP());
497ac7ddfbfSEd Maste                 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
498ac7ddfbfSEd Maste                 if (valobj_sp)
499ac7ddfbfSEd Maste                     sb_value_list.Append(SBValue(valobj_sp));
500ac7ddfbfSEd Maste             }
501ac7ddfbfSEd Maste         }
502ac7ddfbfSEd Maste     }
503ac7ddfbfSEd Maste 
504ac7ddfbfSEd Maste     return sb_value_list;
505ac7ddfbfSEd Maste }
506ac7ddfbfSEd Maste 
507ac7ddfbfSEd Maste lldb::SBValue
508ac7ddfbfSEd Maste SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
509ac7ddfbfSEd Maste {
510ac7ddfbfSEd Maste     SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
511ac7ddfbfSEd Maste     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
512ac7ddfbfSEd Maste         return sb_value_list.GetValueAtIndex(0);
513ac7ddfbfSEd Maste     return SBValue();
514ac7ddfbfSEd Maste }
515ac7ddfbfSEd Maste 
516ac7ddfbfSEd Maste lldb::SBType
517ac7ddfbfSEd Maste SBModule::FindFirstType (const char *name_cstr)
518ac7ddfbfSEd Maste {
519ac7ddfbfSEd Maste     SBType sb_type;
520ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
521ac7ddfbfSEd Maste     if (name_cstr && module_sp)
522ac7ddfbfSEd Maste     {
523ac7ddfbfSEd Maste         SymbolContext sc;
524ac7ddfbfSEd Maste         const bool exact_match = false;
525ac7ddfbfSEd Maste         ConstString name(name_cstr);
526ac7ddfbfSEd Maste 
527ac7ddfbfSEd Maste         sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
528ac7ddfbfSEd Maste 
529ac7ddfbfSEd Maste         if (!sb_type.IsValid())
530ac7ddfbfSEd Maste             sb_type = SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
531ac7ddfbfSEd Maste     }
532ac7ddfbfSEd Maste     return sb_type;
533ac7ddfbfSEd Maste }
534ac7ddfbfSEd Maste 
535ac7ddfbfSEd Maste lldb::SBType
536ac7ddfbfSEd Maste SBModule::GetBasicType(lldb::BasicType type)
537ac7ddfbfSEd Maste {
538ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
539ac7ddfbfSEd Maste     if (module_sp)
540ac7ddfbfSEd Maste         return SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
541ac7ddfbfSEd Maste     return SBType();
542ac7ddfbfSEd Maste }
543ac7ddfbfSEd Maste 
544ac7ddfbfSEd Maste lldb::SBTypeList
545ac7ddfbfSEd Maste SBModule::FindTypes (const char *type)
546ac7ddfbfSEd Maste {
547ac7ddfbfSEd Maste     SBTypeList retval;
548ac7ddfbfSEd Maste 
549ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
550ac7ddfbfSEd Maste     if (type && module_sp)
551ac7ddfbfSEd Maste     {
552ac7ddfbfSEd Maste         SymbolContext sc;
553ac7ddfbfSEd Maste         TypeList type_list;
554ac7ddfbfSEd Maste         const bool exact_match = false;
555ac7ddfbfSEd Maste         ConstString name(type);
556ac7ddfbfSEd Maste         const uint32_t num_matches = module_sp->FindTypes (sc,
557ac7ddfbfSEd Maste                                                            name,
558ac7ddfbfSEd Maste                                                            exact_match,
559ac7ddfbfSEd Maste                                                            UINT32_MAX,
560ac7ddfbfSEd Maste                                                            type_list);
561ac7ddfbfSEd Maste 
562ac7ddfbfSEd Maste         if (num_matches > 0)
563ac7ddfbfSEd Maste         {
564ac7ddfbfSEd Maste             for (size_t idx = 0; idx < num_matches; idx++)
565ac7ddfbfSEd Maste             {
566ac7ddfbfSEd Maste                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
567ac7ddfbfSEd Maste                 if (type_sp)
568ac7ddfbfSEd Maste                     retval.Append(SBType(type_sp));
569ac7ddfbfSEd Maste             }
570ac7ddfbfSEd Maste         }
571ac7ddfbfSEd Maste         else
572ac7ddfbfSEd Maste         {
573ac7ddfbfSEd Maste             SBType sb_type(ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
574ac7ddfbfSEd Maste             if (sb_type.IsValid())
575ac7ddfbfSEd Maste                 retval.Append(sb_type);
576ac7ddfbfSEd Maste         }
577ac7ddfbfSEd Maste     }
578ac7ddfbfSEd Maste 
579ac7ddfbfSEd Maste     return retval;
580ac7ddfbfSEd Maste }
581ac7ddfbfSEd Maste 
582ac7ddfbfSEd Maste lldb::SBTypeList
583ac7ddfbfSEd Maste SBModule::GetTypes (uint32_t type_mask)
584ac7ddfbfSEd Maste {
585ac7ddfbfSEd Maste     SBTypeList sb_type_list;
586ac7ddfbfSEd Maste 
587ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
588ac7ddfbfSEd Maste     if (module_sp)
589ac7ddfbfSEd Maste     {
590ac7ddfbfSEd Maste         SymbolVendor* vendor = module_sp->GetSymbolVendor();
591ac7ddfbfSEd Maste         if (vendor)
592ac7ddfbfSEd Maste         {
593ac7ddfbfSEd Maste             TypeList type_list;
594ac7ddfbfSEd Maste             vendor->GetTypes (NULL, type_mask, type_list);
595ac7ddfbfSEd Maste             sb_type_list.m_opaque_ap->Append(type_list);
596ac7ddfbfSEd Maste         }
597ac7ddfbfSEd Maste     }
598ac7ddfbfSEd Maste     return sb_type_list;
599ac7ddfbfSEd Maste }
600ac7ddfbfSEd Maste 
601ac7ddfbfSEd Maste SBSection
602ac7ddfbfSEd Maste SBModule::FindSection (const char *sect_name)
603ac7ddfbfSEd Maste {
604ac7ddfbfSEd Maste     SBSection sb_section;
605ac7ddfbfSEd Maste 
606ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
607ac7ddfbfSEd Maste     if (sect_name && module_sp)
608ac7ddfbfSEd Maste     {
609ac7ddfbfSEd Maste         // Give the symbol vendor a chance to add to the unified section list.
610ac7ddfbfSEd Maste         module_sp->GetSymbolVendor();
611ac7ddfbfSEd Maste         SectionList *section_list = module_sp->GetSectionList();
612ac7ddfbfSEd Maste         if (section_list)
613ac7ddfbfSEd Maste         {
614ac7ddfbfSEd Maste             ConstString const_sect_name(sect_name);
615ac7ddfbfSEd Maste             SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
616ac7ddfbfSEd Maste             if (section_sp)
617ac7ddfbfSEd Maste             {
618ac7ddfbfSEd Maste                 sb_section.SetSP (section_sp);
619ac7ddfbfSEd Maste             }
620ac7ddfbfSEd Maste         }
621ac7ddfbfSEd Maste     }
622ac7ddfbfSEd Maste     return sb_section;
623ac7ddfbfSEd Maste }
624ac7ddfbfSEd Maste 
625ac7ddfbfSEd Maste lldb::ByteOrder
626ac7ddfbfSEd Maste SBModule::GetByteOrder ()
627ac7ddfbfSEd Maste {
628ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
629ac7ddfbfSEd Maste     if (module_sp)
630ac7ddfbfSEd Maste         return module_sp->GetArchitecture().GetByteOrder();
631ac7ddfbfSEd Maste     return eByteOrderInvalid;
632ac7ddfbfSEd Maste }
633ac7ddfbfSEd Maste 
634ac7ddfbfSEd Maste const char *
635ac7ddfbfSEd Maste SBModule::GetTriple ()
636ac7ddfbfSEd Maste {
637ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
638ac7ddfbfSEd Maste     if (module_sp)
639ac7ddfbfSEd Maste     {
640ac7ddfbfSEd Maste         std::string triple (module_sp->GetArchitecture().GetTriple().str());
641ac7ddfbfSEd Maste         // Unique the string so we don't run into ownership issues since
642ac7ddfbfSEd Maste         // the const strings put the string into the string pool once and
643ac7ddfbfSEd Maste         // the strings never comes out
644ac7ddfbfSEd Maste         ConstString const_triple (triple.c_str());
645ac7ddfbfSEd Maste         return const_triple.GetCString();
646ac7ddfbfSEd Maste     }
647ac7ddfbfSEd Maste     return NULL;
648ac7ddfbfSEd Maste }
649ac7ddfbfSEd Maste 
650ac7ddfbfSEd Maste uint32_t
651ac7ddfbfSEd Maste SBModule::GetAddressByteSize()
652ac7ddfbfSEd Maste {
653ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
654ac7ddfbfSEd Maste     if (module_sp)
655ac7ddfbfSEd Maste         return module_sp->GetArchitecture().GetAddressByteSize();
656ac7ddfbfSEd Maste     return sizeof(void*);
657ac7ddfbfSEd Maste }
658ac7ddfbfSEd Maste 
659ac7ddfbfSEd Maste 
660ac7ddfbfSEd Maste uint32_t
661ac7ddfbfSEd Maste SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
662ac7ddfbfSEd Maste {
663ac7ddfbfSEd Maste     ModuleSP module_sp (GetSP ());
664ac7ddfbfSEd Maste     if (module_sp)
665ac7ddfbfSEd Maste         return module_sp->GetVersion(versions, num_versions);
666ac7ddfbfSEd Maste     else
667ac7ddfbfSEd Maste     {
668ac7ddfbfSEd Maste         if (versions && num_versions)
669ac7ddfbfSEd Maste         {
670ac7ddfbfSEd Maste             for (uint32_t i=0; i<num_versions; ++i)
671ac7ddfbfSEd Maste                 versions[i] = UINT32_MAX;
672ac7ddfbfSEd Maste         }
673ac7ddfbfSEd Maste         return 0;
674ac7ddfbfSEd Maste     }
675ac7ddfbfSEd Maste }
676ac7ddfbfSEd Maste 
677