]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBFunction.cpp
Merge clang trunk r238337 from ^/vendor/clang/dist, resolve conflicts,
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBFunction.cpp
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
25 SBFunction::SBFunction () :
26     m_opaque_ptr (NULL)
27 {
28 }
29
30 SBFunction::SBFunction (lldb_private::Function *lldb_object_ptr) :
31     m_opaque_ptr (lldb_object_ptr)
32 {
33 }
34
35 SBFunction::SBFunction (const lldb::SBFunction &rhs) :
36     m_opaque_ptr (rhs.m_opaque_ptr)
37 {
38 }
39
40 const SBFunction &
41 SBFunction::operator = (const SBFunction &rhs)
42 {
43     m_opaque_ptr = rhs.m_opaque_ptr;
44     return *this;
45 }
46
47 SBFunction::~SBFunction ()
48 {
49     m_opaque_ptr = NULL;
50 }
51
52 bool
53 SBFunction::IsValid () const
54 {
55     return m_opaque_ptr != NULL;
56 }
57
58 const char *
59 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\"",
70                          static_cast<void*>(m_opaque_ptr), cstr);
71         else
72             log->Printf ("SBFunction(%p)::GetName () => NULL",
73                          static_cast<void*>(m_opaque_ptr));
74     }
75     return cstr;
76 }
77
78 const char *
79 SBFunction::GetMangledName () const
80 {
81     const char *cstr = NULL;
82     if (m_opaque_ptr)
83         cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
84     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
85     if (log)
86     {
87         if (cstr)
88             log->Printf ("SBFunction(%p)::GetMangledName () => \"%s\"",
89                          static_cast<void*>(m_opaque_ptr), cstr);
90         else
91             log->Printf ("SBFunction(%p)::GetMangledName () => NULL",
92                          static_cast<void*>(m_opaque_ptr));
93     }
94     return cstr;
95 }
96
97 bool
98 SBFunction::operator == (const SBFunction &rhs) const
99 {
100     return m_opaque_ptr == rhs.m_opaque_ptr;
101 }
102
103 bool
104 SBFunction::operator != (const SBFunction &rhs) const
105 {
106     return m_opaque_ptr != rhs.m_opaque_ptr;
107 }
108
109 bool
110 SBFunction::GetDescription (SBStream &s)
111 {
112     if (m_opaque_ptr)
113     {
114         s.Printf ("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
115                   m_opaque_ptr->GetID(),
116                   m_opaque_ptr->GetName().AsCString());
117         Type *func_type = m_opaque_ptr->GetType();
118         if (func_type)
119             s.Printf(", type = %s", func_type->GetName().AsCString());
120         return true;
121     }
122     s.Printf ("No value");
123     return false;
124 }
125
126 SBInstructionList
127 SBFunction::GetInstructions (SBTarget target)
128 {
129     return GetInstructions (target, NULL);
130 }
131
132 SBInstructionList
133 SBFunction::GetInstructions (SBTarget target, const char *flavor)
134 {
135     SBInstructionList sb_instructions;
136     if (m_opaque_ptr)
137     {
138         Mutex::Locker api_locker;
139         ExecutionContext exe_ctx;
140         TargetSP target_sp (target.GetSP());
141         if (target_sp)
142         {
143             api_locker.Lock (target_sp->GetAPIMutex());
144             target_sp->CalculateExecutionContext (exe_ctx);
145             exe_ctx.SetProcessSP(target_sp->GetProcessSP());
146         }
147         ModuleSP module_sp (m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());
148         if (module_sp)
149         {
150             const bool prefer_file_cache = false;
151             sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture(),
152                                                                              NULL,
153                                                                              flavor,
154                                                                              exe_ctx,
155                                                                              m_opaque_ptr->GetAddressRange(),
156                                                                              prefer_file_cache));
157         }
158     }
159     return sb_instructions;
160 }
161
162 lldb_private::Function *
163 SBFunction::get ()
164 {
165     return m_opaque_ptr;
166 }
167
168 void
169 SBFunction::reset (lldb_private::Function *lldb_object_ptr)
170 {
171     m_opaque_ptr = lldb_object_ptr;
172 }
173
174 SBAddress
175 SBFunction::GetStartAddress ()
176 {
177     SBAddress addr;
178     if (m_opaque_ptr)
179         addr.SetAddress (&m_opaque_ptr->GetAddressRange().GetBaseAddress());
180     return addr;
181 }
182
183 SBAddress
184 SBFunction::GetEndAddress ()
185 {
186     SBAddress addr;
187     if (m_opaque_ptr)
188     {
189         addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
190         if (byte_size > 0)
191         {
192             addr.SetAddress (&m_opaque_ptr->GetAddressRange().GetBaseAddress());
193             addr->Slide (byte_size);
194         }
195     }
196     return addr;
197 }
198
199
200 uint32_t
201 SBFunction::GetPrologueByteSize ()
202 {
203     if (m_opaque_ptr)
204         return m_opaque_ptr->GetPrologueByteSize();
205     return 0;
206 }
207
208 SBType
209 SBFunction::GetType ()
210 {
211     SBType sb_type;
212     if (m_opaque_ptr)
213     {
214         Type *function_type = m_opaque_ptr->GetType();
215         if (function_type)
216             sb_type.ref().SetType (function_type->shared_from_this());
217     }
218     return sb_type;
219 }
220
221 SBBlock
222 SBFunction::GetBlock ()
223 {
224     SBBlock sb_block;
225     if (m_opaque_ptr)
226         sb_block.SetPtr (&m_opaque_ptr->GetBlock (true));
227     return sb_block;
228 }
229
230 lldb::LanguageType
231 SBFunction::GetLanguage ()
232 {
233     if (m_opaque_ptr)
234     {
235         if (m_opaque_ptr->GetCompileUnit())
236             return m_opaque_ptr->GetCompileUnit()->GetLanguage();
237     }
238     return lldb::eLanguageTypeUnknown;
239 }
240
241