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     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
69     if (log)
70         log->Printf ("SBSymbol(%p)::GetName () => \"%s\"", m_opaque_ptr, name ? name : "");
71     return name;
72 }
73 
74 const char *
75 SBSymbol::GetMangledName () const
76 {
77     const char *name = NULL;
78     if (m_opaque_ptr)
79         name = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
80     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
81     if (log)
82         log->Printf ("SBSymbol(%p)::GetMangledName () => \"%s\"", m_opaque_ptr, name ? name : "");
83 
84     return name;
85 }
86 
87 
88 bool
89 SBSymbol::operator == (const SBSymbol &rhs) const
90 {
91     return m_opaque_ptr == rhs.m_opaque_ptr;
92 }
93 
94 bool
95 SBSymbol::operator != (const SBSymbol &rhs) const
96 {
97     return m_opaque_ptr != rhs.m_opaque_ptr;
98 }
99 
100 bool
101 SBSymbol::GetDescription (SBStream &description)
102 {
103     if (m_opaque_ptr)
104     {
105         description.ref();
106         m_opaque_ptr->GetDescription (description.get(),
107                                       lldb::eDescriptionLevelFull, NULL);
108     }
109     else
110         description.Printf ("No value");
111 
112     return true;
113 }
114 
115 
116 
117 SBInstructionList
118 SBSymbol::GetInstructions (SBTarget target)
119 {
120     SBInstructionList sb_instructions;
121     if (m_opaque_ptr)
122     {
123         Mutex::Locker api_locker;
124         ExecutionContext exe_ctx;
125         if (target.IsValid())
126         {
127             api_locker.Reset (target->GetAPIMutex().GetMutex());
128             target->CalculateExecutionContext (exe_ctx);
129         }
130         const AddressRange *symbol_range = m_opaque_ptr->GetAddressRangePtr();
131         if (symbol_range)
132         {
133             Module *module = symbol_range->GetBaseAddress().GetModule();
134             if (module)
135             {
136                 sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module->GetArchitecture (),
137                                                                                  exe_ctx,
138                                                                                  *symbol_range));
139             }
140         }
141     }
142     return sb_instructions;
143 }
144 
145 lldb_private::Symbol *
146 SBSymbol::get ()
147 {
148     return m_opaque_ptr;
149 }
150 
151 void
152 SBSymbol::reset (lldb_private::Symbol *symbol)
153 {
154     m_opaque_ptr = symbol;
155 }
156