Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBStream.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Disassembler.h" |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Log.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Module.h" |
| 16 | #include "lldb/Symbol/CompileUnit.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/Function.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/Type.h" |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/VariableList.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ExecutionContext.h" |
| 21 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 24 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
| 26 | SBFunction::SBFunction () : |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 27 | m_opaque_ptr (NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | { |
| 29 | } |
| 30 | |
| 31 | SBFunction::SBFunction (lldb_private::Function *lldb_object_ptr) : |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 32 | m_opaque_ptr (lldb_object_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 36 | SBFunction::SBFunction (const lldb::SBFunction &rhs) : |
| 37 | m_opaque_ptr (rhs.m_opaque_ptr) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | const SBFunction & |
| 42 | SBFunction::operator = (const SBFunction &rhs) |
| 43 | { |
| 44 | m_opaque_ptr = rhs.m_opaque_ptr; |
| 45 | return *this; |
| 46 | } |
| 47 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | SBFunction::~SBFunction () |
| 49 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 50 | m_opaque_ptr = NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool |
| 54 | SBFunction::IsValid () const |
| 55 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 56 | return m_opaque_ptr != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | const char * |
| 60 | SBFunction::GetName() const |
| 61 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 62 | const char *cstr = NULL; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 63 | if (m_opaque_ptr) |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 64 | cstr = m_opaque_ptr->GetName().AsCString(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 65 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 66 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 67 | if (log) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 68 | { |
| 69 | if (cstr) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 70 | log->Printf ("SBFunction(%p)::GetName () => \"%s\"", |
| 71 | static_cast<void*>(m_opaque_ptr), cstr); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 72 | else |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 73 | log->Printf ("SBFunction(%p)::GetName () => NULL", |
| 74 | static_cast<void*>(m_opaque_ptr)); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 75 | } |
| 76 | return cstr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const char * |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 80 | SBFunction::GetDisplayName() const |
| 81 | { |
| 82 | const char *cstr = NULL; |
| 83 | if (m_opaque_ptr) |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 84 | cstr = m_opaque_ptr->GetMangled().GetDisplayDemangledName(m_opaque_ptr->GetLanguage()).AsCString(); |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 85 | |
| 86 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 87 | if (log) |
| 88 | { |
| 89 | if (cstr) |
| 90 | log->Printf ("SBFunction(%p)::GetDisplayName () => \"%s\"", |
| 91 | static_cast<void*>(m_opaque_ptr), cstr); |
| 92 | else |
| 93 | log->Printf ("SBFunction(%p)::GetDisplayName () => NULL", |
| 94 | static_cast<void*>(m_opaque_ptr)); |
| 95 | } |
| 96 | return cstr; |
| 97 | } |
| 98 | |
| 99 | const char * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | SBFunction::GetMangledName () const |
| 101 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 102 | const char *cstr = NULL; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 103 | if (m_opaque_ptr) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 104 | cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 105 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 106 | if (log) |
| 107 | { |
| 108 | if (cstr) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 109 | log->Printf ("SBFunction(%p)::GetMangledName () => \"%s\"", |
| 110 | static_cast<void*>(m_opaque_ptr), cstr); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 111 | else |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 112 | log->Printf ("SBFunction(%p)::GetMangledName () => NULL", |
| 113 | static_cast<void*>(m_opaque_ptr)); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 114 | } |
| 115 | return cstr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool |
| 119 | SBFunction::operator == (const SBFunction &rhs) const |
| 120 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 121 | return m_opaque_ptr == rhs.m_opaque_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool |
| 125 | SBFunction::operator != (const SBFunction &rhs) const |
| 126 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 127 | return m_opaque_ptr != rhs.m_opaque_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | } |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 129 | |
| 130 | bool |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 131 | SBFunction::GetDescription (SBStream &s) |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 132 | { |
| 133 | if (m_opaque_ptr) |
| 134 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 135 | s.Printf ("SBFunction: id = 0x%8.8" PRIx64 ", name = %s", |
Greg Clayton | 81c22f6 | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 136 | m_opaque_ptr->GetID(), |
| 137 | m_opaque_ptr->GetName().AsCString()); |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 138 | Type *func_type = m_opaque_ptr->GetType(); |
| 139 | if (func_type) |
| 140 | s.Printf(", type = %s", func_type->GetName().AsCString()); |
| 141 | return true; |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 142 | } |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 143 | s.Printf ("No value"); |
| 144 | return false; |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 145 | } |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 146 | |
| 147 | SBInstructionList |
| 148 | SBFunction::GetInstructions (SBTarget target) |
| 149 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 150 | return GetInstructions (target, NULL); |
| 151 | } |
| 152 | |
| 153 | SBInstructionList |
| 154 | SBFunction::GetInstructions (SBTarget target, const char *flavor) |
| 155 | { |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 156 | SBInstructionList sb_instructions; |
| 157 | if (m_opaque_ptr) |
| 158 | { |
| 159 | ExecutionContext exe_ctx; |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 160 | TargetSP target_sp (target.GetSP()); |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame^] | 161 | std::unique_lock<std::recursive_mutex> lock; |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 162 | if (target_sp) |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 163 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame^] | 164 | lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 165 | target_sp->CalculateExecutionContext (exe_ctx); |
| 166 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 167 | } |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 168 | ModuleSP module_sp (m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule()); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 169 | if (module_sp) |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 170 | { |
Jason Molenda | 6b3e6d5 | 2013-09-12 23:23:35 +0000 | [diff] [blame] | 171 | const bool prefer_file_cache = false; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 172 | sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture(), |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 173 | NULL, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 174 | flavor, |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 175 | exe_ctx, |
Jason Molenda | 6b3e6d5 | 2013-09-12 23:23:35 +0000 | [diff] [blame] | 176 | m_opaque_ptr->GetAddressRange(), |
| 177 | prefer_file_cache)); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | return sb_instructions; |
| 181 | } |
| 182 | |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 183 | lldb_private::Function * |
| 184 | SBFunction::get () |
| 185 | { |
| 186 | return m_opaque_ptr; |
| 187 | } |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 188 | |
Greg Clayton | 72eff18 | 2010-12-14 04:58:53 +0000 | [diff] [blame] | 189 | void |
| 190 | SBFunction::reset (lldb_private::Function *lldb_object_ptr) |
| 191 | { |
| 192 | m_opaque_ptr = lldb_object_ptr; |
| 193 | } |
| 194 | |
Greg Clayton | 93d00df | 2011-03-02 23:01:18 +0000 | [diff] [blame] | 195 | SBAddress |
| 196 | SBFunction::GetStartAddress () |
| 197 | { |
| 198 | SBAddress addr; |
| 199 | if (m_opaque_ptr) |
| 200 | addr.SetAddress (&m_opaque_ptr->GetAddressRange().GetBaseAddress()); |
| 201 | return addr; |
| 202 | } |
| 203 | |
| 204 | SBAddress |
| 205 | SBFunction::GetEndAddress () |
| 206 | { |
| 207 | SBAddress addr; |
| 208 | if (m_opaque_ptr) |
| 209 | { |
| 210 | addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize(); |
| 211 | if (byte_size > 0) |
| 212 | { |
| 213 | addr.SetAddress (&m_opaque_ptr->GetAddressRange().GetBaseAddress()); |
| 214 | addr->Slide (byte_size); |
| 215 | } |
| 216 | } |
| 217 | return addr; |
| 218 | } |
| 219 | |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 220 | const char * |
| 221 | SBFunction::GetArgumentName (uint32_t arg_idx) |
| 222 | { |
| 223 | if (m_opaque_ptr) |
| 224 | { |
| 225 | Block &block = m_opaque_ptr->GetBlock(true); |
| 226 | VariableListSP variable_list_sp = block.GetBlockVariableList(true); |
| 227 | if (variable_list_sp) |
| 228 | { |
| 229 | VariableList arguments; |
| 230 | variable_list_sp->AppendVariablesWithScope (eValueTypeVariableArgument, arguments, true); |
| 231 | lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx); |
| 232 | if (variable_sp) |
| 233 | return variable_sp->GetName().GetCString(); |
| 234 | } |
| 235 | } |
| 236 | return nullptr; |
| 237 | } |
Greg Clayton | 93d00df | 2011-03-02 23:01:18 +0000 | [diff] [blame] | 238 | |
| 239 | uint32_t |
| 240 | SBFunction::GetPrologueByteSize () |
| 241 | { |
| 242 | if (m_opaque_ptr) |
| 243 | return m_opaque_ptr->GetPrologueByteSize(); |
| 244 | return 0; |
| 245 | } |
| 246 | |
Greg Clayton | 5569e64 | 2012-02-06 01:44:54 +0000 | [diff] [blame] | 247 | SBType |
| 248 | SBFunction::GetType () |
| 249 | { |
| 250 | SBType sb_type; |
| 251 | if (m_opaque_ptr) |
| 252 | { |
| 253 | Type *function_type = m_opaque_ptr->GetType(); |
| 254 | if (function_type) |
| 255 | sb_type.ref().SetType (function_type->shared_from_this()); |
| 256 | } |
| 257 | return sb_type; |
| 258 | } |
| 259 | |
| 260 | SBBlock |
| 261 | SBFunction::GetBlock () |
| 262 | { |
| 263 | SBBlock sb_block; |
| 264 | if (m_opaque_ptr) |
| 265 | sb_block.SetPtr (&m_opaque_ptr->GetBlock (true)); |
| 266 | return sb_block; |
| 267 | } |
| 268 | |
Enrico Granata | 6cd8e0c | 2014-11-17 23:06:20 +0000 | [diff] [blame] | 269 | lldb::LanguageType |
| 270 | SBFunction::GetLanguage () |
| 271 | { |
| 272 | if (m_opaque_ptr) |
| 273 | { |
| 274 | if (m_opaque_ptr->GetCompileUnit()) |
| 275 | return m_opaque_ptr->GetCompileUnit()->GetLanguage(); |
| 276 | } |
| 277 | return lldb::eLanguageTypeUnknown; |
| 278 | } |
Greg Clayton | 5569e64 | 2012-02-06 01:44:54 +0000 | [diff] [blame] | 279 | |
Jason Molenda | 6ab659a | 2015-07-29 00:42:47 +0000 | [diff] [blame] | 280 | bool |
| 281 | SBFunction::GetIsOptimized () |
| 282 | { |
| 283 | if (m_opaque_ptr) |
| 284 | { |
| 285 | if (m_opaque_ptr->GetCompileUnit()) |
| 286 | return m_opaque_ptr->GetCompileUnit()->GetIsOptimized(); |
| 287 | } |
| 288 | return false; |
| 289 | } |