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