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             ModuleSP module_sp (m_opaque_ptr->GetAddress().GetModule());
141             if (module_sp)
142             {
143                 AddressRange symbol_range (m_opaque_ptr->GetAddress(), m_opaque_ptr->GetByteSize());
144                 const bool prefer_file_cache = false;
145                 sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture (),
146                                                                                  NULL,
147                                                                                  flavor_string,
148                                                                                  exe_ctx,
149                                                                                  symbol_range,
150                                                                                  prefer_file_cache));
151             }
152         }
153     }
154     return sb_instructions;
155 }
156 
157 lldb_private::Symbol *
158 SBSymbol::get ()
159 {
160     return m_opaque_ptr;
161 }
162 
163 void
164 SBSymbol::reset (lldb_private::Symbol *symbol)
165 {
166     m_opaque_ptr = symbol;
167 }
168 
169 SBAddress
170 SBSymbol::GetStartAddress ()
171 {
172     SBAddress addr;
173     if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress())
174     {
175         addr.SetAddress (&m_opaque_ptr->GetAddress());
176     }
177     return addr;
178 }
179 
180 SBAddress
181 SBSymbol::GetEndAddress ()
182 {
183     SBAddress addr;
184     if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress())
185     {
186         lldb::addr_t range_size = m_opaque_ptr->GetByteSize();
187         if (range_size > 0)
188         {
189             addr.SetAddress (&m_opaque_ptr->GetAddress());
190             addr->Slide (m_opaque_ptr->GetByteSize());
191         }
192     }
193     return addr;
194 }
195 
196 uint32_t
197 SBSymbol::GetPrologueByteSize ()
198 {
199     if (m_opaque_ptr)
200         return m_opaque_ptr->GetPrologueByteSize();
201     return 0;
202 }
203 
204 SymbolType
205 SBSymbol::GetType ()
206 {
207     if (m_opaque_ptr)
208         return m_opaque_ptr->GetType();
209     return eSymbolTypeInvalid;
210 }
211 
212 bool
213 SBSymbol::IsExternal()
214 {
215     if (m_opaque_ptr)
216         return m_opaque_ptr->IsExternal();
217     return false;
218 }
219 
220 bool
221 SBSymbol::IsSynthetic()
222 {
223     if (m_opaque_ptr)
224         return m_opaque_ptr->IsSynthetic();
225     return false;
226 }
227 
228