1 //===-- SBFunction.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Module.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/Function.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Symbol/VariableList.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/Log.h"
24 using namespace lldb_private;
26 SBFunction::SBFunction() : m_opaque_ptr(NULL) {}
28 SBFunction::SBFunction(lldb_private::Function *lldb_object_ptr)
29 : m_opaque_ptr(lldb_object_ptr) {}
31 SBFunction::SBFunction(const lldb::SBFunction &rhs)
32 : m_opaque_ptr(rhs.m_opaque_ptr) {}
34 const SBFunction &SBFunction::operator=(const SBFunction &rhs) {
35 m_opaque_ptr = rhs.m_opaque_ptr;
39 SBFunction::~SBFunction() { m_opaque_ptr = NULL; }
41 bool SBFunction::IsValid() const { return m_opaque_ptr != NULL; }
43 const char *SBFunction::GetName() const {
44 const char *cstr = NULL;
46 cstr = m_opaque_ptr->GetName().AsCString();
48 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
51 log->Printf("SBFunction(%p)::GetName () => \"%s\"",
52 static_cast<void *>(m_opaque_ptr), cstr);
54 log->Printf("SBFunction(%p)::GetName () => NULL",
55 static_cast<void *>(m_opaque_ptr));
60 const char *SBFunction::GetDisplayName() const {
61 const char *cstr = NULL;
63 cstr = m_opaque_ptr->GetMangled()
64 .GetDisplayDemangledName(m_opaque_ptr->GetLanguage())
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
70 log->Printf("SBFunction(%p)::GetDisplayName () => \"%s\"",
71 static_cast<void *>(m_opaque_ptr), cstr);
73 log->Printf("SBFunction(%p)::GetDisplayName () => NULL",
74 static_cast<void *>(m_opaque_ptr));
79 const char *SBFunction::GetMangledName() const {
80 const char *cstr = NULL;
82 cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
83 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
86 log->Printf("SBFunction(%p)::GetMangledName () => \"%s\"",
87 static_cast<void *>(m_opaque_ptr), cstr);
89 log->Printf("SBFunction(%p)::GetMangledName () => NULL",
90 static_cast<void *>(m_opaque_ptr));
95 bool SBFunction::operator==(const SBFunction &rhs) const {
96 return m_opaque_ptr == rhs.m_opaque_ptr;
99 bool SBFunction::operator!=(const SBFunction &rhs) const {
100 return m_opaque_ptr != rhs.m_opaque_ptr;
103 bool SBFunction::GetDescription(SBStream &s) {
105 s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
106 m_opaque_ptr->GetID(), m_opaque_ptr->GetName().AsCString());
107 Type *func_type = m_opaque_ptr->GetType();
109 s.Printf(", type = %s", func_type->GetName().AsCString());
112 s.Printf("No value");
116 SBInstructionList SBFunction::GetInstructions(SBTarget target) {
117 return GetInstructions(target, NULL);
120 SBInstructionList SBFunction::GetInstructions(SBTarget target,
121 const char *flavor) {
122 SBInstructionList sb_instructions;
124 ExecutionContext exe_ctx;
125 TargetSP target_sp(target.GetSP());
126 std::unique_lock<std::recursive_mutex> lock;
128 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
129 target_sp->CalculateExecutionContext(exe_ctx);
130 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
133 m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());
135 const bool prefer_file_cache = false;
136 sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
137 module_sp->GetArchitecture(), NULL, flavor, exe_ctx,
138 m_opaque_ptr->GetAddressRange(), prefer_file_cache));
141 return sb_instructions;
144 lldb_private::Function *SBFunction::get() { return m_opaque_ptr; }
146 void SBFunction::reset(lldb_private::Function *lldb_object_ptr) {
147 m_opaque_ptr = lldb_object_ptr;
150 SBAddress SBFunction::GetStartAddress() {
153 addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
157 SBAddress SBFunction::GetEndAddress() {
160 addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
162 addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
163 addr->Slide(byte_size);
169 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
171 Block &block = m_opaque_ptr->GetBlock(true);
172 VariableListSP variable_list_sp = block.GetBlockVariableList(true);
173 if (variable_list_sp) {
174 VariableList arguments;
175 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
177 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
179 return variable_sp->GetName().GetCString();
185 uint32_t SBFunction::GetPrologueByteSize() {
187 return m_opaque_ptr->GetPrologueByteSize();
191 SBType SBFunction::GetType() {
194 Type *function_type = m_opaque_ptr->GetType();
196 sb_type.ref().SetType(function_type->shared_from_this());
201 SBBlock SBFunction::GetBlock() {
204 sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
208 lldb::LanguageType SBFunction::GetLanguage() {
210 if (m_opaque_ptr->GetCompileUnit())
211 return m_opaque_ptr->GetCompileUnit()->GetLanguage();
213 return lldb::eLanguageTypeUnknown;
216 bool SBFunction::GetIsOptimized() {
218 if (m_opaque_ptr->GetCompileUnit())
219 return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();