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/Module.h"
14 #include "lldb/Symbol/Symbol.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/Log.h"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
SBSymbol()22 SBSymbol::SBSymbol() : m_opaque_ptr(NULL) {}
23
SBSymbol(lldb_private::Symbol * lldb_object_ptr)24 SBSymbol::SBSymbol(lldb_private::Symbol *lldb_object_ptr)
25 : m_opaque_ptr(lldb_object_ptr) {}
26
SBSymbol(const lldb::SBSymbol & rhs)27 SBSymbol::SBSymbol(const lldb::SBSymbol &rhs)
28 : m_opaque_ptr(rhs.m_opaque_ptr) {}
29
operator =(const SBSymbol & rhs)30 const SBSymbol &SBSymbol::operator=(const SBSymbol &rhs) {
31 m_opaque_ptr = rhs.m_opaque_ptr;
32 return *this;
33 }
34
~SBSymbol()35 SBSymbol::~SBSymbol() { m_opaque_ptr = NULL; }
36
SetSymbol(lldb_private::Symbol * lldb_object_ptr)37 void SBSymbol::SetSymbol(lldb_private::Symbol *lldb_object_ptr) {
38 m_opaque_ptr = lldb_object_ptr;
39 }
40
IsValid() const41 bool SBSymbol::IsValid() const { return m_opaque_ptr != NULL; }
42
GetName() const43 const char *SBSymbol::GetName() const {
44 const char *name = NULL;
45 if (m_opaque_ptr)
46 name = m_opaque_ptr->GetName().AsCString();
47
48 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
49 if (log)
50 log->Printf("SBSymbol(%p)::GetName () => \"%s\"",
51 static_cast<void *>(m_opaque_ptr), name ? name : "");
52 return name;
53 }
54
GetDisplayName() const55 const char *SBSymbol::GetDisplayName() const {
56 const char *name = NULL;
57 if (m_opaque_ptr)
58 name = m_opaque_ptr->GetMangled()
59 .GetDisplayDemangledName(m_opaque_ptr->GetLanguage())
60 .AsCString();
61
62 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
63 if (log)
64 log->Printf("SBSymbol(%p)::GetDisplayName () => \"%s\"",
65 static_cast<void *>(m_opaque_ptr), name ? name : "");
66 return name;
67 }
68
GetMangledName() const69 const char *SBSymbol::GetMangledName() const {
70 const char *name = NULL;
71 if (m_opaque_ptr)
72 name = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
73 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
74 if (log)
75 log->Printf("SBSymbol(%p)::GetMangledName () => \"%s\"",
76 static_cast<void *>(m_opaque_ptr), name ? name : "");
77
78 return name;
79 }
80
operator ==(const SBSymbol & rhs) const81 bool SBSymbol::operator==(const SBSymbol &rhs) const {
82 return m_opaque_ptr == rhs.m_opaque_ptr;
83 }
84
operator !=(const SBSymbol & rhs) const85 bool SBSymbol::operator!=(const SBSymbol &rhs) const {
86 return m_opaque_ptr != rhs.m_opaque_ptr;
87 }
88
GetDescription(SBStream & description)89 bool SBSymbol::GetDescription(SBStream &description) {
90 Stream &strm = description.ref();
91
92 if (m_opaque_ptr) {
93 m_opaque_ptr->GetDescription(&strm, lldb::eDescriptionLevelFull, NULL);
94 } else
95 strm.PutCString("No value");
96
97 return true;
98 }
99
GetInstructions(SBTarget target)100 SBInstructionList SBSymbol::GetInstructions(SBTarget target) {
101 return GetInstructions(target, NULL);
102 }
103
GetInstructions(SBTarget target,const char * flavor_string)104 SBInstructionList SBSymbol::GetInstructions(SBTarget target,
105 const char *flavor_string) {
106 SBInstructionList sb_instructions;
107 if (m_opaque_ptr) {
108 ExecutionContext exe_ctx;
109 TargetSP target_sp(target.GetSP());
110 std::unique_lock<std::recursive_mutex> lock;
111 if (target_sp) {
112 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
113
114 target_sp->CalculateExecutionContext(exe_ctx);
115 }
116 if (m_opaque_ptr->ValueIsAddress()) {
117 const Address &symbol_addr = m_opaque_ptr->GetAddressRef();
118 ModuleSP module_sp = symbol_addr.GetModule();
119 if (module_sp) {
120 AddressRange symbol_range(symbol_addr, m_opaque_ptr->GetByteSize());
121 const bool prefer_file_cache = false;
122 sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
123 module_sp->GetArchitecture(), NULL, flavor_string, exe_ctx,
124 symbol_range, prefer_file_cache));
125 }
126 }
127 }
128 return sb_instructions;
129 }
130
get()131 lldb_private::Symbol *SBSymbol::get() { return m_opaque_ptr; }
132
reset(lldb_private::Symbol * symbol)133 void SBSymbol::reset(lldb_private::Symbol *symbol) { m_opaque_ptr = symbol; }
134
GetStartAddress()135 SBAddress SBSymbol::GetStartAddress() {
136 SBAddress addr;
137 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
138 addr.SetAddress(&m_opaque_ptr->GetAddressRef());
139 }
140 return addr;
141 }
142
GetEndAddress()143 SBAddress SBSymbol::GetEndAddress() {
144 SBAddress addr;
145 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
146 lldb::addr_t range_size = m_opaque_ptr->GetByteSize();
147 if (range_size > 0) {
148 addr.SetAddress(&m_opaque_ptr->GetAddressRef());
149 addr->Slide(m_opaque_ptr->GetByteSize());
150 }
151 }
152 return addr;
153 }
154
GetPrologueByteSize()155 uint32_t SBSymbol::GetPrologueByteSize() {
156 if (m_opaque_ptr)
157 return m_opaque_ptr->GetPrologueByteSize();
158 return 0;
159 }
160
GetType()161 SymbolType SBSymbol::GetType() {
162 if (m_opaque_ptr)
163 return m_opaque_ptr->GetType();
164 return eSymbolTypeInvalid;
165 }
166
IsExternal()167 bool SBSymbol::IsExternal() {
168 if (m_opaque_ptr)
169 return m_opaque_ptr->IsExternal();
170 return false;
171 }
172
IsSynthetic()173 bool SBSymbol::IsSynthetic() {
174 if (m_opaque_ptr)
175 return m_opaque_ptr->IsSynthetic();
176 return false;
177 }
178