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/SBFileSpec.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/StreamString.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 
23 SBModule::SBModule () :
24     m_opaque_sp ()
25 {
26 }
27 
28 SBModule::SBModule (const lldb::ModuleSP& module_sp) :
29     m_opaque_sp (module_sp)
30 {
31 }
32 
33 SBModule::SBModule(const SBModule &rhs) :
34     m_opaque_sp (rhs.m_opaque_sp)
35 {
36 }
37 
38 const SBModule &
39 SBModule::operator = (const SBModule &rhs)
40 {
41     if (this != &rhs)
42         m_opaque_sp = rhs.m_opaque_sp;
43     return *this;
44 }
45 
46 SBModule::~SBModule ()
47 {
48 }
49 
50 bool
51 SBModule::IsValid () const
52 {
53     return m_opaque_sp.get() != NULL;
54 }
55 
56 SBFileSpec
57 SBModule::GetFileSpec () const
58 {
59     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
60 
61     SBFileSpec file_spec;
62     if (m_opaque_sp)
63         file_spec.SetFileSpec(m_opaque_sp->GetFileSpec());
64 
65     if (log)
66     {
67         log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
68         m_opaque_sp.get(), file_spec.get());
69     }
70 
71     return file_spec;
72 }
73 
74 const uint8_t *
75 SBModule::GetUUIDBytes () const
76 {
77     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
78 
79     const uint8_t *uuid_bytes = NULL;
80     if (m_opaque_sp)
81         uuid_bytes = (const uint8_t *)m_opaque_sp->GetUUID().GetBytes();
82 
83     if (log)
84     {
85         if (uuid_bytes)
86         {
87             StreamString s;
88             m_opaque_sp->GetUUID().Dump (&s);
89             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", m_opaque_sp.get(), s.GetData());
90         }
91         else
92             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", m_opaque_sp.get());
93     }
94     return uuid_bytes;
95 }
96 
97 
98 const char *
99 SBModule::GetUUIDString () const
100 {
101     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
102 
103     static char uuid_string[80];
104     const char * uuid_c_string = NULL;
105     if (m_opaque_sp)
106         uuid_c_string = (const char *)m_opaque_sp->GetUUID().GetAsCString(uuid_string, sizeof(uuid_string));
107 
108     if (log)
109     {
110         if (uuid_c_string)
111         {
112             StreamString s;
113             m_opaque_sp->GetUUID().Dump (&s);
114             log->Printf ("SBModule(%p)::GetUUIDString () => %s", m_opaque_sp.get(), s.GetData());
115         }
116         else
117             log->Printf ("SBModule(%p)::GetUUIDString () => NULL", m_opaque_sp.get());
118     }
119     return uuid_c_string;
120 }
121 
122 
123 bool
124 SBModule::operator == (const SBModule &rhs) const
125 {
126     if (m_opaque_sp)
127         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
128     return false;
129 }
130 
131 bool
132 SBModule::operator != (const SBModule &rhs) const
133 {
134     if (m_opaque_sp)
135         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
136     return false;
137 }
138 
139 lldb::ModuleSP &
140 SBModule::operator *()
141 {
142     return m_opaque_sp;
143 }
144 
145 lldb_private::Module *
146 SBModule::operator ->()
147 {
148     return m_opaque_sp.get();
149 }
150 
151 const lldb_private::Module *
152 SBModule::operator ->() const
153 {
154     return m_opaque_sp.get();
155 }
156 
157 lldb_private::Module *
158 SBModule::get()
159 {
160     return m_opaque_sp.get();
161 }
162 
163 const lldb_private::Module *
164 SBModule::get() const
165 {
166     return m_opaque_sp.get();
167 }
168 
169 
170 void
171 SBModule::SetModule (const lldb::ModuleSP& module_sp)
172 {
173     m_opaque_sp = module_sp;
174 }
175 
176 
177 bool
178 SBModule::ResolveFileAddress (lldb::addr_t vm_addr, SBAddress& addr)
179 {
180     if (m_opaque_sp)
181         return m_opaque_sp->ResolveFileAddress (vm_addr, *addr);
182 
183     addr->Clear();
184     return false;
185 }
186 
187 SBSymbolContext
188 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
189 {
190     SBSymbolContext sb_sc;
191     if (m_opaque_sp && addr.IsValid())
192         m_opaque_sp->ResolveSymbolContextForAddress (*addr, resolve_scope, *sb_sc);
193     return sb_sc;
194 }
195 
196 bool
197 SBModule::GetDescription (SBStream &description)
198 {
199     if (m_opaque_sp)
200     {
201         description.ref();
202         m_opaque_sp->GetDescription (description.get());
203     }
204     else
205         description.Printf ("No value");
206 
207     return true;
208 }
209 
210 size_t
211 SBModule::GetNumSymbols ()
212 {
213     if (m_opaque_sp)
214     {
215         ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
216         if (obj_file)
217         {
218             Symtab *symtab = obj_file->GetSymtab();
219             if (symtab)
220                 return symtab->GetNumSymbols();
221         }
222     }
223     return 0;
224 }
225 
226 SBSymbol
227 SBModule::GetSymbolAtIndex (size_t idx)
228 {
229     SBSymbol sb_symbol;
230     if (m_opaque_sp)
231     {
232         ObjectFile *obj_file = m_opaque_sp->GetObjectFile();
233         if (obj_file)
234         {
235             Symtab *symtab = obj_file->GetSymtab();
236             if (symtab)
237                 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
238         }
239     }
240     return sb_symbol;
241 }
242