1 //===-- SBModule.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/API/SBModule.h"
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBSymbolContextList.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/StreamString.h"
18 #include "lldb/Core/ValueObjectList.h"
19 #include "lldb/Core/ValueObjectVariable.h"
20 #include "lldb/Symbol/VariableList.h"
21 #include "lldb/Target/Target.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 
27 SBModule::SBModule () :
28     m_opaque_sp ()
29 {
30 }
31 
32 SBModule::SBModule (const lldb::ModuleSP& module_sp) :
33     m_opaque_sp (module_sp)
34 {
35 }
36 
37 SBModule::SBModule(const SBModule &rhs) :
38     m_opaque_sp (rhs.m_opaque_sp)
39 {
40 }
41 
42 const SBModule &
43 SBModule::operator = (const SBModule &rhs)
44 {
45     if (this != &rhs)
46         m_opaque_sp = rhs.m_opaque_sp;
47     return *this;
48 }
49 
50 SBModule::~SBModule ()
51 {
52 }
53 
54 bool
55 SBModule::IsValid () const
56 {
57     return m_opaque_sp.get() != NULL;
58 }
59 
60 SBFileSpec
61 SBModule::GetFileSpec () const
62 {
63     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
64 
65     SBFileSpec file_spec;
66     if (m_opaque_sp)
67         file_spec.SetFileSpec(m_opaque_sp->GetFileSpec());
68 
69     if (log)
70     {
71         log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
72         m_opaque_sp.get(), file_spec.get());
73     }
74 
75     return file_spec;
76 }
77 
78 lldb::SBFileSpec
79 SBModule::GetPlatformFileSpec () const
80 {
81     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
82 
83     SBFileSpec file_spec;
84     if (m_opaque_sp)
85         file_spec.SetFileSpec(m_opaque_sp->GetPlatformFileSpec());
86 
87     if (log)
88     {
89         log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
90                      m_opaque_sp.get(), file_spec.get());
91     }
92 
93     return file_spec;
94 
95 }
96 
97 bool
98 SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
99 {
100     bool result = false;
101     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
102 
103     if (m_opaque_sp)
104     {
105         m_opaque_sp->SetPlatformFileSpec(*platform_file);
106         result = true;
107     }
108 
109     if (log)
110     {
111         log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s%s%s)) => %i",
112                      m_opaque_sp.get(),
113                      platform_file.get(),
114                      platform_file->GetDirectory().GetCString(),
115                      platform_file->GetDirectory() ? "/" : "",
116                      platform_file->GetFilename().GetCString(),
117                      result);
118     }
119     return result;
120 }
121 
122 
123 
124 const uint8_t *
125 SBModule::GetUUIDBytes () const
126 {
127     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
128 
129     const uint8_t *uuid_bytes = NULL;
130     if (m_opaque_sp)
131         uuid_bytes = (const uint8_t *)m_opaque_sp->GetUUID().GetBytes();
132 
133     if (log)
134     {
135         if (uuid_bytes)
136         {
137             StreamString s;
138             m_opaque_sp->GetUUID().Dump (&s);
139             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", m_opaque_sp.get(), s.GetData());
140         }
141         else
142             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", m_opaque_sp.get());
143     }
144     return uuid_bytes;
145 }
146 
147 
148 const char *
149 SBModule::GetUUIDString () const
150 {
151     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
152 
153     static char uuid_string[80];
154     const char * uuid_c_string = NULL;
155     if (m_opaque_sp)
156         uuid_c_string = (const char *)m_opaque_sp->GetUUID().GetAsCString(uuid_string, sizeof(uuid_string));
157 
158     if (log)
159     {
160         if (uuid_c_string)
161         {
162             StreamString s;
163             m_opaque_sp->GetUUID().Dump (&s);
164             log->Printf ("SBModule(%p)::GetUUIDString () => %s", m_opaque_sp.get(), s.GetData());
165         }
166         else
167             log->Printf ("SBModule(%p)::GetUUIDString () => NULL", m_opaque_sp.get());
168     }
169     return uuid_c_string;
170 }
171 
172 
173 bool
174 SBModule::operator == (const SBModule &rhs) const
175 {
176     if (m_opaque_sp)
177         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
178     return false;
179 }
180 
181 bool
182 SBModule::operator != (const SBModule &rhs) const
183 {
184     if (m_opaque_sp)
185         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
186     return false;
187 }
188 
189 lldb::ModuleSP &
190 SBModule::operator *()
191 {
192     return m_opaque_sp;
193 }
194 
195 lldb_private::Module *
196 SBModule::operator ->()
197 {
198     return m_opaque_sp.get();
199 }
200 
201 const lldb_private::Module *
202 SBModule::operator ->() const
203 {
204     return m_opaque_sp.get();
205 }
206 
207 lldb_private::Module *
208 SBModule::get()
209 {
210     return m_opaque_sp.get();
211 }
212 
213 const lldb_private::Module *
214 SBModule::get() const
215 {
216     return m_opaque_sp.get();
217 }
218 
219 
220 void
221 SBModule::SetModule (const lldb::ModuleSP& module_sp)
222 {
223     m_opaque_sp = module_sp;
224 }
225 
226 
227 bool
228 SBModule::ResolveFileAddress (lldb::addr_t vm_addr, SBAddress& addr)
229 {
230     if (m_opaque_sp && addr.IsValid())
231         return m_opaque_sp->ResolveFileAddress (vm_addr, *addr);
232 
233     if (addr.IsValid())
234         addr->Clear();
235     return false;
236 }
237 
238 SBSymbolContext
239 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
240 {
241     SBSymbolContext sb_sc;
242     if (m_opaque_sp && addr.IsValid())
243         m_opaque_sp->ResolveSymbolContextForAddress (*addr, resolve_scope, *sb_sc);
244     return sb_sc;
245 }
246 
247 bool
248 SBModule::GetDescription (SBStream &description)
249 {
250     if (m_opaque_sp)
251     {
252         description.ref();
253         m_opaque_sp->GetDescription (description.get());
254     }
255     else
256         description.Printf ("No value");
257 
258     return true;
259 }
260 
261 size_t
262 SBModule::GetNumSymbols ()
263 {
264     if (m_opaque_sp)
265     {
266         ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
267         if (obj_file)
268         {
269             Symtab *symtab = obj_file->GetSymtab();
270             if (symtab)
271                 return symtab->GetNumSymbols();
272         }
273     }
274     return 0;
275 }
276 
277 SBSymbol
278 SBModule::GetSymbolAtIndex (size_t idx)
279 {
280     SBSymbol sb_symbol;
281     if (m_opaque_sp)
282     {
283         ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
284         if (obj_file)
285         {
286             Symtab *symtab = obj_file->GetSymtab();
287             if (symtab)
288                 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
289         }
290     }
291     return sb_symbol;
292 }
293 
294 uint32_t
295 SBModule::FindFunctions (const char *name,
296                          uint32_t name_type_mask,
297                          bool append,
298                          lldb::SBSymbolContextList& sc_list)
299 {
300     if (!append)
301         sc_list.Clear();
302     if (m_opaque_sp)
303     {
304         const bool symbols_ok = true;
305         return m_opaque_sp->FindFunctions (ConstString(name),
306                                            name_type_mask,
307                                            symbols_ok,
308                                            append,
309                                            *sc_list);
310     }
311     return 0;
312 }
313 
314 
315 SBValueList
316 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
317 {
318     SBValueList sb_value_list;
319     if (m_opaque_sp)
320     {
321         VariableList variable_list;
322         const uint32_t match_count = m_opaque_sp->FindGlobalVariables (ConstString (name),
323                                                                        false,
324                                                                        max_matches,
325                                                                        variable_list);
326 
327         if (match_count > 0)
328         {
329             ValueObjectList &value_object_list = sb_value_list.ref();
330             for (uint32_t i=0; i<match_count; ++i)
331             {
332                 lldb::ValueObjectSP valobj_sp;
333                 if (target.IsValid())
334                     valobj_sp = ValueObjectVariable::Create (target.get(), variable_list.GetVariableAtIndex(i));
335                 else
336                     valobj_sp = ValueObjectVariable::Create (NULL, variable_list.GetVariableAtIndex(i));
337                 if (valobj_sp)
338                     value_object_list.Append(valobj_sp);
339             }
340         }
341     }
342 
343     return sb_value_list;
344 }
345