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 | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 63 | cstr = m_opaque_ptr->GetMangled().GetName().AsCString(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 64 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 65 | LogSP 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) |
| 69 | log->Printf ("SBFunction(%p)::GetName () => \"%s\"", m_opaque_ptr, cstr); |
| 70 | else |
| 71 | log->Printf ("SBFunction(%p)::GetName () => NULL", m_opaque_ptr); |
| 72 | } |
| 73 | return cstr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | const char * |
| 77 | SBFunction::GetMangledName () const |
| 78 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 79 | const char *cstr = NULL; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 80 | if (m_opaque_ptr) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 81 | cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString(); |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 82 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 83 | if (log) |
| 84 | { |
| 85 | if (cstr) |
| 86 | log->Printf ("SBFunction(%p)::GetMangledName () => \"%s\"", m_opaque_ptr, cstr); |
| 87 | else |
| 88 | log->Printf ("SBFunction(%p)::GetMangledName () => NULL", m_opaque_ptr); |
| 89 | } |
| 90 | return cstr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool |
| 94 | SBFunction::operator == (const SBFunction &rhs) const |
| 95 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 96 | return m_opaque_ptr == rhs.m_opaque_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool |
| 100 | SBFunction::operator != (const SBFunction &rhs) const |
| 101 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 102 | return m_opaque_ptr != rhs.m_opaque_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | } |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 104 | |
| 105 | bool |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 106 | SBFunction::GetDescription (SBStream &s) |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 107 | { |
| 108 | if (m_opaque_ptr) |
| 109 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 110 | s.Printf ("SBFunction: id = 0x%8.8" PRIx64 ", name = %s", |
Greg Clayton | 81c22f6 | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 111 | m_opaque_ptr->GetID(), |
| 112 | m_opaque_ptr->GetName().AsCString()); |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 113 | Type *func_type = m_opaque_ptr->GetType(); |
| 114 | if (func_type) |
| 115 | s.Printf(", type = %s", func_type->GetName().AsCString()); |
| 116 | return true; |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 117 | } |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 118 | s.Printf ("No value"); |
| 119 | return false; |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 120 | } |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 121 | |
| 122 | SBInstructionList |
| 123 | SBFunction::GetInstructions (SBTarget target) |
| 124 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 125 | return GetInstructions (target, NULL); |
| 126 | } |
| 127 | |
| 128 | SBInstructionList |
| 129 | SBFunction::GetInstructions (SBTarget target, const char *flavor) |
| 130 | { |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 131 | SBInstructionList sb_instructions; |
| 132 | if (m_opaque_ptr) |
| 133 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 134 | Mutex::Locker api_locker; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 135 | ExecutionContext exe_ctx; |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 136 | TargetSP target_sp (target.GetSP()); |
| 137 | if (target_sp) |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 138 | { |
Jim Ingham | 10ebffa | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 139 | api_locker.Lock (target_sp->GetAPIMutex()); |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 140 | target_sp->CalculateExecutionContext (exe_ctx); |
| 141 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 142 | } |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 143 | ModuleSP module_sp (m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule()); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 144 | if (module_sp) |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 145 | { |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 146 | sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture(), |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 147 | NULL, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 148 | flavor, |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 149 | exe_ctx, |
| 150 | m_opaque_ptr->GetAddressRange())); |
| 151 | } |
| 152 | } |
| 153 | return sb_instructions; |
| 154 | } |
| 155 | |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 156 | lldb_private::Function * |
| 157 | SBFunction::get () |
| 158 | { |
| 159 | return m_opaque_ptr; |
| 160 | } |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 161 | |
Greg Clayton | 72eff18 | 2010-12-14 04:58:53 +0000 | [diff] [blame] | 162 | void |
| 163 | SBFunction::reset (lldb_private::Function *lldb_object_ptr) |
| 164 | { |
| 165 | m_opaque_ptr = lldb_object_ptr; |
| 166 | } |
| 167 | |
Greg Clayton | 93d00df | 2011-03-02 23:01:18 +0000 | [diff] [blame] | 168 | SBAddress |
| 169 | SBFunction::GetStartAddress () |
| 170 | { |
| 171 | SBAddress addr; |
| 172 | if (m_opaque_ptr) |
| 173 | addr.SetAddress (&m_opaque_ptr->GetAddressRange().GetBaseAddress()); |
| 174 | return addr; |
| 175 | } |
| 176 | |
| 177 | SBAddress |
| 178 | SBFunction::GetEndAddress () |
| 179 | { |
| 180 | SBAddress addr; |
| 181 | if (m_opaque_ptr) |
| 182 | { |
| 183 | addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize(); |
| 184 | if (byte_size > 0) |
| 185 | { |
| 186 | addr.SetAddress (&m_opaque_ptr->GetAddressRange().GetBaseAddress()); |
| 187 | addr->Slide (byte_size); |
| 188 | } |
| 189 | } |
| 190 | return addr; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | uint32_t |
| 195 | SBFunction::GetPrologueByteSize () |
| 196 | { |
| 197 | if (m_opaque_ptr) |
| 198 | return m_opaque_ptr->GetPrologueByteSize(); |
| 199 | return 0; |
| 200 | } |
| 201 | |
Greg Clayton | 5569e64 | 2012-02-06 01:44:54 +0000 | [diff] [blame] | 202 | SBType |
| 203 | SBFunction::GetType () |
| 204 | { |
| 205 | SBType sb_type; |
| 206 | if (m_opaque_ptr) |
| 207 | { |
| 208 | Type *function_type = m_opaque_ptr->GetType(); |
| 209 | if (function_type) |
| 210 | sb_type.ref().SetType (function_type->shared_from_this()); |
| 211 | } |
| 212 | return sb_type; |
| 213 | } |
| 214 | |
| 215 | SBBlock |
| 216 | SBFunction::GetBlock () |
| 217 | { |
| 218 | SBBlock sb_block; |
| 219 | if (m_opaque_ptr) |
| 220 | sb_block.SetPtr (&m_opaque_ptr->GetBlock (true)); |
| 221 | return sb_block; |
| 222 | } |
| 223 | |
| 224 | |
Greg Clayton | 93d00df | 2011-03-02 23:01:18 +0000 | [diff] [blame] | 225 | |