1ac7ddfbfSEd Maste //===-- SBFunction.cpp ------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/API/SBFunction.h"
11ac7ddfbfSEd Maste #include "lldb/API/SBProcess.h"
12ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
13ac7ddfbfSEd Maste #include "lldb/Core/Disassembler.h"
14ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
15ac7ddfbfSEd Maste #include "lldb/Symbol/CompileUnit.h"
16ac7ddfbfSEd Maste #include "lldb/Symbol/Function.h"
17ac7ddfbfSEd Maste #include "lldb/Symbol/Type.h"
189f2f44ceSEd Maste #include "lldb/Symbol/VariableList.h"
19ac7ddfbfSEd Maste #include "lldb/Target/ExecutionContext.h"
20ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
21*f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
22ac7ddfbfSEd Maste 
23ac7ddfbfSEd Maste using namespace lldb;
24ac7ddfbfSEd Maste using namespace lldb_private;
25ac7ddfbfSEd Maste 
SBFunction()26435933ddSDimitry Andric SBFunction::SBFunction() : m_opaque_ptr(NULL) {}
27ac7ddfbfSEd Maste 
SBFunction(lldb_private::Function * lldb_object_ptr)28435933ddSDimitry Andric SBFunction::SBFunction(lldb_private::Function *lldb_object_ptr)
29435933ddSDimitry Andric     : m_opaque_ptr(lldb_object_ptr) {}
30ac7ddfbfSEd Maste 
SBFunction(const lldb::SBFunction & rhs)31435933ddSDimitry Andric SBFunction::SBFunction(const lldb::SBFunction &rhs)
32435933ddSDimitry Andric     : m_opaque_ptr(rhs.m_opaque_ptr) {}
33ac7ddfbfSEd Maste 
operator =(const SBFunction & rhs)34435933ddSDimitry Andric const SBFunction &SBFunction::operator=(const SBFunction &rhs) {
35ac7ddfbfSEd Maste   m_opaque_ptr = rhs.m_opaque_ptr;
36ac7ddfbfSEd Maste   return *this;
37ac7ddfbfSEd Maste }
38ac7ddfbfSEd Maste 
~SBFunction()39435933ddSDimitry Andric SBFunction::~SBFunction() { m_opaque_ptr = NULL; }
40ac7ddfbfSEd Maste 
IsValid() const41435933ddSDimitry Andric bool SBFunction::IsValid() const { return m_opaque_ptr != NULL; }
42ac7ddfbfSEd Maste 
GetName() const43435933ddSDimitry Andric const char *SBFunction::GetName() const {
44ac7ddfbfSEd Maste   const char *cstr = NULL;
45ac7ddfbfSEd Maste   if (m_opaque_ptr)
46b91a7dfcSDimitry Andric     cstr = m_opaque_ptr->GetName().AsCString();
47ac7ddfbfSEd Maste 
48ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
49435933ddSDimitry Andric   if (log) {
50ac7ddfbfSEd Maste     if (cstr)
510127ef0fSEd Maste       log->Printf("SBFunction(%p)::GetName () => \"%s\"",
520127ef0fSEd Maste                   static_cast<void *>(m_opaque_ptr), cstr);
53ac7ddfbfSEd Maste     else
540127ef0fSEd Maste       log->Printf("SBFunction(%p)::GetName () => NULL",
550127ef0fSEd Maste                   static_cast<void *>(m_opaque_ptr));
56ac7ddfbfSEd Maste   }
57ac7ddfbfSEd Maste   return cstr;
58ac7ddfbfSEd Maste }
59ac7ddfbfSEd Maste 
GetDisplayName() const60435933ddSDimitry Andric const char *SBFunction::GetDisplayName() const {
61b91a7dfcSDimitry Andric   const char *cstr = NULL;
62b91a7dfcSDimitry Andric   if (m_opaque_ptr)
63435933ddSDimitry Andric     cstr = m_opaque_ptr->GetMangled()
64435933ddSDimitry Andric                .GetDisplayDemangledName(m_opaque_ptr->GetLanguage())
65435933ddSDimitry Andric                .AsCString();
66b91a7dfcSDimitry Andric 
67b91a7dfcSDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
68435933ddSDimitry Andric   if (log) {
69b91a7dfcSDimitry Andric     if (cstr)
70b91a7dfcSDimitry Andric       log->Printf("SBFunction(%p)::GetDisplayName () => \"%s\"",
71b91a7dfcSDimitry Andric                   static_cast<void *>(m_opaque_ptr), cstr);
72b91a7dfcSDimitry Andric     else
73b91a7dfcSDimitry Andric       log->Printf("SBFunction(%p)::GetDisplayName () => NULL",
74b91a7dfcSDimitry Andric                   static_cast<void *>(m_opaque_ptr));
75b91a7dfcSDimitry Andric   }
76b91a7dfcSDimitry Andric   return cstr;
77b91a7dfcSDimitry Andric }
78b91a7dfcSDimitry Andric 
GetMangledName() const79435933ddSDimitry Andric const char *SBFunction::GetMangledName() const {
80ac7ddfbfSEd Maste   const char *cstr = NULL;
81ac7ddfbfSEd Maste   if (m_opaque_ptr)
82ac7ddfbfSEd Maste     cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
83ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
84435933ddSDimitry Andric   if (log) {
85ac7ddfbfSEd Maste     if (cstr)
860127ef0fSEd Maste       log->Printf("SBFunction(%p)::GetMangledName () => \"%s\"",
870127ef0fSEd Maste                   static_cast<void *>(m_opaque_ptr), cstr);
88ac7ddfbfSEd Maste     else
890127ef0fSEd Maste       log->Printf("SBFunction(%p)::GetMangledName () => NULL",
900127ef0fSEd Maste                   static_cast<void *>(m_opaque_ptr));
91ac7ddfbfSEd Maste   }
92ac7ddfbfSEd Maste   return cstr;
93ac7ddfbfSEd Maste }
94ac7ddfbfSEd Maste 
operator ==(const SBFunction & rhs) const95435933ddSDimitry Andric bool SBFunction::operator==(const SBFunction &rhs) const {
96ac7ddfbfSEd Maste   return m_opaque_ptr == rhs.m_opaque_ptr;
97ac7ddfbfSEd Maste }
98ac7ddfbfSEd Maste 
operator !=(const SBFunction & rhs) const99435933ddSDimitry Andric bool SBFunction::operator!=(const SBFunction &rhs) const {
100ac7ddfbfSEd Maste   return m_opaque_ptr != rhs.m_opaque_ptr;
101ac7ddfbfSEd Maste }
102ac7ddfbfSEd Maste 
GetDescription(SBStream & s)103435933ddSDimitry Andric bool SBFunction::GetDescription(SBStream &s) {
104435933ddSDimitry Andric   if (m_opaque_ptr) {
105ac7ddfbfSEd Maste     s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
106435933ddSDimitry Andric              m_opaque_ptr->GetID(), m_opaque_ptr->GetName().AsCString());
107ac7ddfbfSEd Maste     Type *func_type = m_opaque_ptr->GetType();
108ac7ddfbfSEd Maste     if (func_type)
109ac7ddfbfSEd Maste       s.Printf(", type = %s", func_type->GetName().AsCString());
110ac7ddfbfSEd Maste     return true;
111ac7ddfbfSEd Maste   }
112ac7ddfbfSEd Maste   s.Printf("No value");
113ac7ddfbfSEd Maste   return false;
114ac7ddfbfSEd Maste }
115ac7ddfbfSEd Maste 
GetInstructions(SBTarget target)116435933ddSDimitry Andric SBInstructionList SBFunction::GetInstructions(SBTarget target) {
117ac7ddfbfSEd Maste   return GetInstructions(target, NULL);
118ac7ddfbfSEd Maste }
119ac7ddfbfSEd Maste 
GetInstructions(SBTarget target,const char * flavor)120435933ddSDimitry Andric SBInstructionList SBFunction::GetInstructions(SBTarget target,
121435933ddSDimitry Andric                                               const char *flavor) {
122ac7ddfbfSEd Maste   SBInstructionList sb_instructions;
123435933ddSDimitry Andric   if (m_opaque_ptr) {
124ac7ddfbfSEd Maste     ExecutionContext exe_ctx;
125ac7ddfbfSEd Maste     TargetSP target_sp(target.GetSP());
1264bb0738eSEd Maste     std::unique_lock<std::recursive_mutex> lock;
127435933ddSDimitry Andric     if (target_sp) {
1284bb0738eSEd Maste       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
129ac7ddfbfSEd Maste       target_sp->CalculateExecutionContext(exe_ctx);
130ac7ddfbfSEd Maste       exe_ctx.SetProcessSP(target_sp->GetProcessSP());
131ac7ddfbfSEd Maste     }
132435933ddSDimitry Andric     ModuleSP module_sp(
133435933ddSDimitry Andric         m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());
134435933ddSDimitry Andric     if (module_sp) {
13535617911SEd Maste       const bool prefer_file_cache = false;
136435933ddSDimitry Andric       sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
137435933ddSDimitry Andric           module_sp->GetArchitecture(), NULL, flavor, exe_ctx,
138435933ddSDimitry Andric           m_opaque_ptr->GetAddressRange(), prefer_file_cache));
139ac7ddfbfSEd Maste     }
140ac7ddfbfSEd Maste   }
141ac7ddfbfSEd Maste   return sb_instructions;
142ac7ddfbfSEd Maste }
143ac7ddfbfSEd Maste 
get()144435933ddSDimitry Andric lldb_private::Function *SBFunction::get() { return m_opaque_ptr; }
145ac7ddfbfSEd Maste 
reset(lldb_private::Function * lldb_object_ptr)146435933ddSDimitry Andric void SBFunction::reset(lldb_private::Function *lldb_object_ptr) {
147ac7ddfbfSEd Maste   m_opaque_ptr = lldb_object_ptr;
148ac7ddfbfSEd Maste }
149ac7ddfbfSEd Maste 
GetStartAddress()150435933ddSDimitry Andric SBAddress SBFunction::GetStartAddress() {
151ac7ddfbfSEd Maste   SBAddress addr;
152ac7ddfbfSEd Maste   if (m_opaque_ptr)
153ac7ddfbfSEd Maste     addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
154ac7ddfbfSEd Maste   return addr;
155ac7ddfbfSEd Maste }
156ac7ddfbfSEd Maste 
GetEndAddress()157435933ddSDimitry Andric SBAddress SBFunction::GetEndAddress() {
158ac7ddfbfSEd Maste   SBAddress addr;
159435933ddSDimitry Andric   if (m_opaque_ptr) {
160ac7ddfbfSEd Maste     addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
161435933ddSDimitry Andric     if (byte_size > 0) {
162ac7ddfbfSEd Maste       addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
163ac7ddfbfSEd Maste       addr->Slide(byte_size);
164ac7ddfbfSEd Maste     }
165ac7ddfbfSEd Maste   }
166ac7ddfbfSEd Maste   return addr;
167ac7ddfbfSEd Maste }
168ac7ddfbfSEd Maste 
GetArgumentName(uint32_t arg_idx)169435933ddSDimitry Andric const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
170435933ddSDimitry Andric   if (m_opaque_ptr) {
1719f2f44ceSEd Maste     Block &block = m_opaque_ptr->GetBlock(true);
1729f2f44ceSEd Maste     VariableListSP variable_list_sp = block.GetBlockVariableList(true);
173435933ddSDimitry Andric     if (variable_list_sp) {
1749f2f44ceSEd Maste       VariableList arguments;
175435933ddSDimitry Andric       variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
176435933ddSDimitry Andric                                                  arguments, true);
1779f2f44ceSEd Maste       lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
1789f2f44ceSEd Maste       if (variable_sp)
1799f2f44ceSEd Maste         return variable_sp->GetName().GetCString();
1809f2f44ceSEd Maste     }
1819f2f44ceSEd Maste   }
1829f2f44ceSEd Maste   return nullptr;
1839f2f44ceSEd Maste }
184ac7ddfbfSEd Maste 
GetPrologueByteSize()185435933ddSDimitry Andric uint32_t SBFunction::GetPrologueByteSize() {
186ac7ddfbfSEd Maste   if (m_opaque_ptr)
187ac7ddfbfSEd Maste     return m_opaque_ptr->GetPrologueByteSize();
188ac7ddfbfSEd Maste   return 0;
189ac7ddfbfSEd Maste }
190ac7ddfbfSEd Maste 
GetType()191435933ddSDimitry Andric SBType SBFunction::GetType() {
192ac7ddfbfSEd Maste   SBType sb_type;
193435933ddSDimitry Andric   if (m_opaque_ptr) {
194ac7ddfbfSEd Maste     Type *function_type = m_opaque_ptr->GetType();
195ac7ddfbfSEd Maste     if (function_type)
196ac7ddfbfSEd Maste       sb_type.ref().SetType(function_type->shared_from_this());
197ac7ddfbfSEd Maste   }
198ac7ddfbfSEd Maste   return sb_type;
199ac7ddfbfSEd Maste }
200ac7ddfbfSEd Maste 
GetBlock()201435933ddSDimitry Andric SBBlock SBFunction::GetBlock() {
202ac7ddfbfSEd Maste   SBBlock sb_block;
203ac7ddfbfSEd Maste   if (m_opaque_ptr)
204ac7ddfbfSEd Maste     sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
205ac7ddfbfSEd Maste   return sb_block;
206ac7ddfbfSEd Maste }
207ac7ddfbfSEd Maste 
GetLanguage()208435933ddSDimitry Andric lldb::LanguageType SBFunction::GetLanguage() {
209435933ddSDimitry Andric   if (m_opaque_ptr) {
2107aa51b79SEd Maste     if (m_opaque_ptr->GetCompileUnit())
2117aa51b79SEd Maste       return m_opaque_ptr->GetCompileUnit()->GetLanguage();
2127aa51b79SEd Maste   }
2137aa51b79SEd Maste   return lldb::eLanguageTypeUnknown;
2147aa51b79SEd Maste }
215ac7ddfbfSEd Maste 
GetIsOptimized()216435933ddSDimitry Andric bool SBFunction::GetIsOptimized() {
217435933ddSDimitry Andric   if (m_opaque_ptr) {
2189f2f44ceSEd Maste     if (m_opaque_ptr->GetCompileUnit())
2199f2f44ceSEd Maste       return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
2209f2f44ceSEd Maste   }
2219f2f44ceSEd Maste   return false;
2229f2f44ceSEd Maste }
223