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