• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- SBSymbolContext.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBSymbolContext.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/Symbol.h"
16 #include "lldb/Symbol/SymbolContext.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
SBSymbolContext()21 SBSymbolContext::SBSymbolContext() : m_opaque_up() {
22   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSymbolContext);
23 }
24 
SBSymbolContext(const SymbolContext * sc_ptr)25 SBSymbolContext::SBSymbolContext(const SymbolContext *sc_ptr) : m_opaque_up() {
26   LLDB_RECORD_CONSTRUCTOR(SBSymbolContext,
27                           (const lldb_private::SymbolContext *), sc_ptr);
28 
29   if (sc_ptr)
30     m_opaque_up = std::make_unique<SymbolContext>(*sc_ptr);
31 }
32 
SBSymbolContext(const SBSymbolContext & rhs)33 SBSymbolContext::SBSymbolContext(const SBSymbolContext &rhs) : m_opaque_up() {
34   LLDB_RECORD_CONSTRUCTOR(SBSymbolContext, (const lldb::SBSymbolContext &),
35                           rhs);
36 
37   m_opaque_up = clone(rhs.m_opaque_up);
38 }
39 
40 SBSymbolContext::~SBSymbolContext() = default;
41 
operator =(const SBSymbolContext & rhs)42 const SBSymbolContext &SBSymbolContext::operator=(const SBSymbolContext &rhs) {
43   LLDB_RECORD_METHOD(const lldb::SBSymbolContext &,
44                      SBSymbolContext, operator=,(const lldb::SBSymbolContext &),
45                      rhs);
46 
47   if (this != &rhs)
48     m_opaque_up = clone(rhs.m_opaque_up);
49   return LLDB_RECORD_RESULT(*this);
50 }
51 
SetSymbolContext(const SymbolContext * sc_ptr)52 void SBSymbolContext::SetSymbolContext(const SymbolContext *sc_ptr) {
53   if (sc_ptr)
54     m_opaque_up = std::make_unique<SymbolContext>(*sc_ptr);
55   else
56     m_opaque_up->Clear(true);
57 }
58 
IsValid() const59 bool SBSymbolContext::IsValid() const {
60   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSymbolContext, IsValid);
61   return this->operator bool();
62 }
operator bool() const63 SBSymbolContext::operator bool() const {
64   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSymbolContext, operator bool);
65 
66   return m_opaque_up != nullptr;
67 }
68 
GetModule()69 SBModule SBSymbolContext::GetModule() {
70   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBModule, SBSymbolContext, GetModule);
71 
72   SBModule sb_module;
73   ModuleSP module_sp;
74   if (m_opaque_up) {
75     module_sp = m_opaque_up->module_sp;
76     sb_module.SetSP(module_sp);
77   }
78 
79   return LLDB_RECORD_RESULT(sb_module);
80 }
81 
GetCompileUnit()82 SBCompileUnit SBSymbolContext::GetCompileUnit() {
83   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBCompileUnit, SBSymbolContext,
84                              GetCompileUnit);
85 
86   return LLDB_RECORD_RESULT(
87       SBCompileUnit(m_opaque_up ? m_opaque_up->comp_unit : nullptr));
88 }
89 
GetFunction()90 SBFunction SBSymbolContext::GetFunction() {
91   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFunction, SBSymbolContext, GetFunction);
92 
93   Function *function = nullptr;
94 
95   if (m_opaque_up)
96     function = m_opaque_up->function;
97 
98   SBFunction sb_function(function);
99 
100   return LLDB_RECORD_RESULT(sb_function);
101 }
102 
GetBlock()103 SBBlock SBSymbolContext::GetBlock() {
104   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBSymbolContext, GetBlock);
105 
106   return LLDB_RECORD_RESULT(
107       SBBlock(m_opaque_up ? m_opaque_up->block : nullptr));
108 }
109 
GetLineEntry()110 SBLineEntry SBSymbolContext::GetLineEntry() {
111   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBLineEntry, SBSymbolContext, GetLineEntry);
112 
113   SBLineEntry sb_line_entry;
114   if (m_opaque_up)
115     sb_line_entry.SetLineEntry(m_opaque_up->line_entry);
116 
117   return LLDB_RECORD_RESULT(sb_line_entry);
118 }
119 
GetSymbol()120 SBSymbol SBSymbolContext::GetSymbol() {
121   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSymbol, SBSymbolContext, GetSymbol);
122 
123   Symbol *symbol = nullptr;
124 
125   if (m_opaque_up)
126     symbol = m_opaque_up->symbol;
127 
128   SBSymbol sb_symbol(symbol);
129 
130   return LLDB_RECORD_RESULT(sb_symbol);
131 }
132 
SetModule(lldb::SBModule module)133 void SBSymbolContext::SetModule(lldb::SBModule module) {
134   LLDB_RECORD_METHOD(void, SBSymbolContext, SetModule, (lldb::SBModule),
135                      module);
136 
137   ref().module_sp = module.GetSP();
138 }
139 
SetCompileUnit(lldb::SBCompileUnit compile_unit)140 void SBSymbolContext::SetCompileUnit(lldb::SBCompileUnit compile_unit) {
141   LLDB_RECORD_METHOD(void, SBSymbolContext, SetCompileUnit,
142                      (lldb::SBCompileUnit), compile_unit);
143 
144   ref().comp_unit = compile_unit.get();
145 }
146 
SetFunction(lldb::SBFunction function)147 void SBSymbolContext::SetFunction(lldb::SBFunction function) {
148   LLDB_RECORD_METHOD(void, SBSymbolContext, SetFunction, (lldb::SBFunction),
149                      function);
150 
151   ref().function = function.get();
152 }
153 
SetBlock(lldb::SBBlock block)154 void SBSymbolContext::SetBlock(lldb::SBBlock block) {
155   LLDB_RECORD_METHOD(void, SBSymbolContext, SetBlock, (lldb::SBBlock), block);
156 
157   ref().block = block.GetPtr();
158 }
159 
SetLineEntry(lldb::SBLineEntry line_entry)160 void SBSymbolContext::SetLineEntry(lldb::SBLineEntry line_entry) {
161   LLDB_RECORD_METHOD(void, SBSymbolContext, SetLineEntry, (lldb::SBLineEntry),
162                      line_entry);
163 
164   if (line_entry.IsValid())
165     ref().line_entry = line_entry.ref();
166   else
167     ref().line_entry.Clear();
168 }
169 
SetSymbol(lldb::SBSymbol symbol)170 void SBSymbolContext::SetSymbol(lldb::SBSymbol symbol) {
171   LLDB_RECORD_METHOD(void, SBSymbolContext, SetSymbol, (lldb::SBSymbol),
172                      symbol);
173 
174   ref().symbol = symbol.get();
175 }
176 
operator ->() const177 lldb_private::SymbolContext *SBSymbolContext::operator->() const {
178   return m_opaque_up.get();
179 }
180 
operator *() const181 const lldb_private::SymbolContext &SBSymbolContext::operator*() const {
182   assert(m_opaque_up.get());
183   return *m_opaque_up;
184 }
185 
operator *()186 lldb_private::SymbolContext &SBSymbolContext::operator*() {
187   if (m_opaque_up == nullptr)
188     m_opaque_up = std::make_unique<SymbolContext>();
189   return *m_opaque_up;
190 }
191 
ref()192 lldb_private::SymbolContext &SBSymbolContext::ref() {
193   if (m_opaque_up == nullptr)
194     m_opaque_up = std::make_unique<SymbolContext>();
195   return *m_opaque_up;
196 }
197 
get() const198 lldb_private::SymbolContext *SBSymbolContext::get() const {
199   return m_opaque_up.get();
200 }
201 
GetDescription(SBStream & description)202 bool SBSymbolContext::GetDescription(SBStream &description) {
203   LLDB_RECORD_METHOD(bool, SBSymbolContext, GetDescription, (lldb::SBStream &),
204                      description);
205 
206   Stream &strm = description.ref();
207 
208   if (m_opaque_up) {
209     m_opaque_up->GetDescription(&strm, lldb::eDescriptionLevelFull, nullptr);
210   } else
211     strm.PutCString("No value");
212 
213   return true;
214 }
215 
216 SBSymbolContext
GetParentOfInlinedScope(const SBAddress & curr_frame_pc,SBAddress & parent_frame_addr) const217 SBSymbolContext::GetParentOfInlinedScope(const SBAddress &curr_frame_pc,
218                                          SBAddress &parent_frame_addr) const {
219   LLDB_RECORD_METHOD_CONST(lldb::SBSymbolContext, SBSymbolContext,
220                            GetParentOfInlinedScope,
221                            (const lldb::SBAddress &, lldb::SBAddress &),
222                            curr_frame_pc, parent_frame_addr);
223 
224   SBSymbolContext sb_sc;
225   if (m_opaque_up.get() && curr_frame_pc.IsValid()) {
226     if (m_opaque_up->GetParentOfInlinedScope(curr_frame_pc.ref(), sb_sc.ref(),
227                                              parent_frame_addr.ref()))
228       return LLDB_RECORD_RESULT(sb_sc);
229   }
230   return LLDB_RECORD_RESULT(SBSymbolContext());
231 }
232 
233 namespace lldb_private {
234 namespace repro {
235 
236 template <>
RegisterMethods(Registry & R)237 void RegisterMethods<SBSymbolContext>(Registry &R) {
238   LLDB_REGISTER_CONSTRUCTOR(SBSymbolContext, ());
239   LLDB_REGISTER_CONSTRUCTOR(SBSymbolContext,
240                             (const lldb_private::SymbolContext *));
241   LLDB_REGISTER_CONSTRUCTOR(SBSymbolContext, (const lldb::SBSymbolContext &));
242   LLDB_REGISTER_METHOD(
243       const lldb::SBSymbolContext &,
244       SBSymbolContext, operator=,(const lldb::SBSymbolContext &));
245   LLDB_REGISTER_METHOD_CONST(bool, SBSymbolContext, IsValid, ());
246   LLDB_REGISTER_METHOD_CONST(bool, SBSymbolContext, operator bool, ());
247   LLDB_REGISTER_METHOD(lldb::SBModule, SBSymbolContext, GetModule, ());
248   LLDB_REGISTER_METHOD(lldb::SBCompileUnit, SBSymbolContext, GetCompileUnit,
249                        ());
250   LLDB_REGISTER_METHOD(lldb::SBFunction, SBSymbolContext, GetFunction, ());
251   LLDB_REGISTER_METHOD(lldb::SBBlock, SBSymbolContext, GetBlock, ());
252   LLDB_REGISTER_METHOD(lldb::SBLineEntry, SBSymbolContext, GetLineEntry, ());
253   LLDB_REGISTER_METHOD(lldb::SBSymbol, SBSymbolContext, GetSymbol, ());
254   LLDB_REGISTER_METHOD(void, SBSymbolContext, SetModule, (lldb::SBModule));
255   LLDB_REGISTER_METHOD(void, SBSymbolContext, SetCompileUnit,
256                        (lldb::SBCompileUnit));
257   LLDB_REGISTER_METHOD(void, SBSymbolContext, SetFunction,
258                        (lldb::SBFunction));
259   LLDB_REGISTER_METHOD(void, SBSymbolContext, SetBlock, (lldb::SBBlock));
260   LLDB_REGISTER_METHOD(void, SBSymbolContext, SetLineEntry,
261                        (lldb::SBLineEntry));
262   LLDB_REGISTER_METHOD(void, SBSymbolContext, SetSymbol, (lldb::SBSymbol));
263   LLDB_REGISTER_METHOD(bool, SBSymbolContext, GetDescription,
264                        (lldb::SBStream &));
265   LLDB_REGISTER_METHOD_CONST(lldb::SBSymbolContext, SBSymbolContext,
266                              GetParentOfInlinedScope,
267                              (const lldb::SBAddress &, lldb::SBAddress &));
268 }
269 
270 }
271 }
272