1 //===-- SBSymbol.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/SBSymbol.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Core/Disassembler.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Symbol/Symbol.h"
16 #include "lldb/Target/ExecutionContext.h"
17 #include "lldb/Target/Target.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 SBSymbol::SBSymbol () :
23     m_opaque_ptr (NULL)
24 {
25 }
26 
27 SBSymbol::SBSymbol (lldb_private::Symbol *lldb_object_ptr) :
28     m_opaque_ptr (lldb_object_ptr)
29 {
30 }
31 
32 SBSymbol::SBSymbol (const lldb::SBSymbol &rhs) :
33     m_opaque_ptr (rhs.m_opaque_ptr)
34 {
35 }
36 
37 const SBSymbol &
38 SBSymbol::operator = (const SBSymbol &rhs)
39 {
40     m_opaque_ptr = rhs.m_opaque_ptr;
41     return *this;
42 }
43 
44 SBSymbol::~SBSymbol ()
45 {
46     m_opaque_ptr = NULL;
47 }
48 
49 void
50 SBSymbol::SetSymbol (lldb_private::Symbol *lldb_object_ptr)
51 {
52     m_opaque_ptr = lldb_object_ptr;
53 }
54 
55 bool
56 SBSymbol::IsValid () const
57 {
58     return m_opaque_ptr != NULL;
59 }
60 
61 const char *
62 SBSymbol::GetName() const
63 {
64     const char *name = NULL;
65     if (m_opaque_ptr)
66         name = m_opaque_ptr->GetMangled().GetName().AsCString();
67 
68     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
69     if (log)
70         log->Printf ("SBSymbol(%p)::GetName () => \"%s\"",
71                      static_cast<void*>(m_opaque_ptr), name ? name : "");
72     return name;
73 }
74 
75 const char *
76 SBSymbol::GetMangledName () const
77 {
78     const char *name = NULL;
79     if (m_opaque_ptr)
80         name = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
81     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
82     if (log)
83         log->Printf ("SBSymbol(%p)::GetMangledName () => \"%s\"",
84                      static_cast<void*>(m_opaque_ptr), name ? name : "");
85 
86     return name;
87 }
88 
89 
90 bool
91 SBSymbol::operator == (const SBSymbol &rhs) const
92 {
93     return m_opaque_ptr == rhs.m_opaque_ptr;
94 }
95 
96 bool
97 SBSymbol::operator != (const SBSymbol &rhs) const
98 {
99     return m_opaque_ptr != rhs.m_opaque_ptr;
100 }
101 
102 bool
103 SBSymbol::GetDescription (SBStream &description)
104 {
105     Stream &strm = description.ref();
106 
107     if (m_opaque_ptr)
108     {
109         m_opaque_ptr->GetDescription (&strm,
110                                       lldb::eDescriptionLevelFull, NULL);
111     }
112     else
113         strm.PutCString ("No value");
114 
115     return true;
116 }
117 
118 SBInstructionList
119 SBSymbol::GetInstructions (SBTarget target)
120 {
121     return GetInstructions (target, NULL);
122 }
123 
124 SBInstructionList
125 SBSymbol::GetInstructions (SBTarget target, const char *flavor_string)
126 {
127     SBInstructionList sb_instructions;
128     if (m_opaque_ptr)
129     {
130         Mutex::Locker api_locker;
131         ExecutionContext exe_ctx;
132         TargetSP target_sp (target.GetSP());
133         if (target_sp)
134         {
135             api_locker.Lock (target_sp->GetAPIMutex());
136             target_sp->CalculateExecutionContext (exe_ctx);
137         }
138         if (m_opaque_ptr->ValueIsAddress())
139         {
140             const Address &symbol_addr = m_opaque_ptr->GetAddressRef();
141             ModuleSP module_sp = symbol_addr.GetModule();
142             if (module_sp)
143             {
144                 AddressRange symbol_range (symbol_addr, m_opaque_ptr->GetByteSize());
145                 const bool prefer_file_cache = false;
146                 sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture (),
147                                                                                  NULL,
148                                                                                  flavor_string,
149                                                                                  exe_ctx,
150                                                                                  symbol_range,
151                                                                                  prefer_file_cache));
152             }
153         }
154     }
155     return sb_instructions;
156 }
157 
158 lldb_private::Symbol *
159 SBSymbol::get ()
160 {
161     return m_opaque_ptr;
162 }
163 
164 void
165 SBSymbol::reset (lldb_private::Symbol *symbol)
166 {
167     m_opaque_ptr = symbol;
168 }
169 
170 SBAddress
171 SBSymbol::GetStartAddress ()
172 {
173     SBAddress addr;
174     if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress())
175     {
176         addr.SetAddress (&m_opaque_ptr->GetAddressRef());
177     }
178     return addr;
179 }
180 
181 SBAddress
182 SBSymbol::GetEndAddress ()
183 {
184     SBAddress addr;
185     if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress())
186     {
187         lldb::addr_t range_size = m_opaque_ptr->GetByteSize();
188         if (range_size > 0)
189         {
190             addr.SetAddress (&m_opaque_ptr->GetAddressRef());
191             addr->Slide (m_opaque_ptr->GetByteSize());
192         }
193     }
194     return addr;
195 }
196 
197 uint32_t
198 SBSymbol::GetPrologueByteSize ()
199 {
200     if (m_opaque_ptr)
201         return m_opaque_ptr->GetPrologueByteSize();
202     return 0;
203 }
204 
205 SymbolType
206 SBSymbol::GetType ()
207 {
208     if (m_opaque_ptr)
209         return m_opaque_ptr->GetType();
210     return eSymbolTypeInvalid;
211 }
212 
213 bool
214 SBSymbol::IsExternal()
215 {
216     if (m_opaque_ptr)
217         return m_opaque_ptr->IsExternal();
218     return false;
219 }
220 
221 bool
222 SBSymbol::IsSynthetic()
223 {
224     if (m_opaque_ptr)
225         return m_opaque_ptr->IsSynthetic();
226     return false;
227 }
228 
229