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