blob: 6a24f64b43e4e873e98e9549829d7c742c9f267d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Caroline Ticedde9cff2010-09-20 05:20:02 +000012#include "lldb/API/SBStream.h"
Greg Clayton1d273162010-10-06 03:09:58 +000013#include "lldb/Core/Disassembler.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Symbol/Function.h"
Greg Clayton05faeb72010-10-07 04:19:01 +000017#include "lldb/Symbol/Type.h"
Greg Claytonfe689042015-11-10 17:47:04 +000018#include "lldb/Symbol/VariableList.h"
Greg Clayton1d273162010-10-06 03:09:58 +000019#include "lldb/Target/ExecutionContext.h"
20#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000021#include "lldb/Utility/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
Greg Clayton1d273162010-10-06 03:09:58 +000024using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026SBFunction::SBFunction() : m_opaque_ptr(NULL) {}
27
28SBFunction::SBFunction(lldb_private::Function *lldb_object_ptr)
29 : m_opaque_ptr(lldb_object_ptr) {}
30
31SBFunction::SBFunction(const lldb::SBFunction &rhs)
32 : m_opaque_ptr(rhs.m_opaque_ptr) {}
33
34const SBFunction &SBFunction::operator=(const SBFunction &rhs) {
35 m_opaque_ptr = rhs.m_opaque_ptr;
36 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039SBFunction::~SBFunction() { m_opaque_ptr = NULL; }
40
41bool SBFunction::IsValid() const { return m_opaque_ptr != NULL; }
42
43const char *SBFunction::GetName() const {
44 const char *cstr = NULL;
45 if (m_opaque_ptr)
46 cstr = m_opaque_ptr->GetName().AsCString();
47
48 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
49 if (log) {
50 if (cstr)
51 log->Printf("SBFunction(%p)::GetName () => \"%s\"",
52 static_cast<void *>(m_opaque_ptr), cstr);
53 else
54 log->Printf("SBFunction(%p)::GetName () => NULL",
55 static_cast<void *>(m_opaque_ptr));
56 }
57 return cstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060const char *SBFunction::GetDisplayName() const {
61 const char *cstr = NULL;
62 if (m_opaque_ptr)
63 cstr = m_opaque_ptr->GetMangled()
64 .GetDisplayDemangledName(m_opaque_ptr->GetLanguage())
65 .AsCString();
66
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
68 if (log) {
69 if (cstr)
70 log->Printf("SBFunction(%p)::GetDisplayName () => \"%s\"",
71 static_cast<void *>(m_opaque_ptr), cstr);
72 else
73 log->Printf("SBFunction(%p)::GetDisplayName () => NULL",
74 static_cast<void *>(m_opaque_ptr));
75 }
76 return cstr;
Greg Claytonefabb122010-11-05 23:17:00 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079const char *SBFunction::GetMangledName() const {
80 const char *cstr = NULL;
81 if (m_opaque_ptr)
82 cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
83 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
84 if (log) {
85 if (cstr)
86 log->Printf("SBFunction(%p)::GetMangledName () => \"%s\"",
87 static_cast<void *>(m_opaque_ptr), cstr);
88 else
89 log->Printf("SBFunction(%p)::GetMangledName () => NULL",
90 static_cast<void *>(m_opaque_ptr));
91 }
92 return cstr;
Greg Claytonefabb122010-11-05 23:17:00 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095bool SBFunction::operator==(const SBFunction &rhs) const {
96 return m_opaque_ptr == rhs.m_opaque_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099bool SBFunction::operator!=(const SBFunction &rhs) const {
100 return m_opaque_ptr != rhs.m_opaque_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103bool SBFunction::GetDescription(SBStream &s) {
104 if (m_opaque_ptr) {
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();
108 if (func_type)
109 s.Printf(", type = %s", func_type->GetName().AsCString());
110 return true;
111 }
112 s.Printf("No value");
113 return false;
114}
Caroline Ticeceb6b132010-10-26 03:11:13 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116SBInstructionList SBFunction::GetInstructions(SBTarget target) {
117 return GetInstructions(target, NULL);
118}
119
120SBInstructionList SBFunction::GetInstructions(SBTarget target,
121 const char *flavor) {
122 SBInstructionList sb_instructions;
123 if (m_opaque_ptr) {
124 ExecutionContext exe_ctx;
125 TargetSP target_sp(target.GetSP());
126 std::unique_lock<std::recursive_mutex> lock;
127 if (target_sp) {
128 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
129 target_sp->CalculateExecutionContext(exe_ctx);
130 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 ModuleSP module_sp(
133 m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());
134 if (module_sp) {
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));
Enrico Granatac1f705c2015-07-06 18:28:46 +0000139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 }
141 return sb_instructions;
Enrico Granatac1f705c2015-07-06 18:28:46 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144lldb_private::Function *SBFunction::get() { return m_opaque_ptr; }
145
146void SBFunction::reset(lldb_private::Function *lldb_object_ptr) {
147 m_opaque_ptr = lldb_object_ptr;
148}
149
150SBAddress SBFunction::GetStartAddress() {
151 SBAddress addr;
152 if (m_opaque_ptr)
153 addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
154 return addr;
155}
156
157SBAddress SBFunction::GetEndAddress() {
158 SBAddress addr;
159 if (m_opaque_ptr) {
160 addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
161 if (byte_size > 0) {
162 addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
163 addr->Slide(byte_size);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000164 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 }
166 return addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
170 if (m_opaque_ptr) {
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,
176 arguments, true);
177 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
178 if (variable_sp)
179 return variable_sp->GetName().GetCString();
Caroline Ticedde9cff2010-09-20 05:20:02 +0000180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 }
182 return nullptr;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000183}
Greg Clayton1d273162010-10-06 03:09:58 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185uint32_t SBFunction::GetPrologueByteSize() {
186 if (m_opaque_ptr)
187 return m_opaque_ptr->GetPrologueByteSize();
188 return 0;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000189}
190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191SBType SBFunction::GetType() {
192 SBType sb_type;
193 if (m_opaque_ptr) {
194 Type *function_type = m_opaque_ptr->GetType();
195 if (function_type)
196 sb_type.ref().SetType(function_type->shared_from_this());
197 }
198 return sb_type;
Greg Clayton1d273162010-10-06 03:09:58 +0000199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201SBBlock SBFunction::GetBlock() {
202 SBBlock sb_block;
203 if (m_opaque_ptr)
204 sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
205 return sb_block;
Caroline Tice750cd172010-10-26 23:49:36 +0000206}
Greg Clayton1d273162010-10-06 03:09:58 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208lldb::LanguageType SBFunction::GetLanguage() {
209 if (m_opaque_ptr) {
210 if (m_opaque_ptr->GetCompileUnit())
211 return m_opaque_ptr->GetCompileUnit()->GetLanguage();
212 }
213 return lldb::eLanguageTypeUnknown;
Greg Clayton72eff182010-12-14 04:58:53 +0000214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216bool SBFunction::GetIsOptimized() {
217 if (m_opaque_ptr) {
218 if (m_opaque_ptr->GetCompileUnit())
219 return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
220 }
221 return false;
Jason Molenda6ab659a2015-07-29 00:42:47 +0000222}