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" 12 #include "lldb/API/SBStream.h" 13 #include "lldb/Core/Disassembler.h" 14 #include "lldb/Core/Log.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Symbol/CompileUnit.h" 17 #include "lldb/Symbol/Function.h" 18 #include "lldb/Symbol/Type.h" 19 #include "lldb/Symbol/VariableList.h" 20 #include "lldb/Target/ExecutionContext.h" 21 #include "lldb/Target/Target.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 SBFunction::SBFunction () : 27 m_opaque_ptr (NULL) 28 { 29 } 30 31 SBFunction::SBFunction (lldb_private::Function *lldb_object_ptr) : 32 m_opaque_ptr (lldb_object_ptr) 33 { 34 } 35 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 48 SBFunction::~SBFunction () 49 { 50 m_opaque_ptr = NULL; 51 } 52 53 bool 54 SBFunction::IsValid () const 55 { 56 return m_opaque_ptr != NULL; 57 } 58 59 const char * 60 SBFunction::GetName() const 61 { 62 const char *cstr = NULL; 63 if (m_opaque_ptr) 64 cstr = m_opaque_ptr->GetName().AsCString(); 65 66 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 67 if (log) 68 { 69 if (cstr) 70 log->Printf ("SBFunction(%p)::GetName () => \"%s\"", 71 static_cast<void*>(m_opaque_ptr), cstr); 72 else 73 log->Printf ("SBFunction(%p)::GetName () => NULL", 74 static_cast<void*>(m_opaque_ptr)); 75 } 76 return cstr; 77 } 78 79 const char * 80 SBFunction::GetDisplayName() const 81 { 82 const char *cstr = NULL; 83 if (m_opaque_ptr) 84 cstr = m_opaque_ptr->GetMangled().GetDisplayDemangledName(m_opaque_ptr->GetLanguage()).AsCString(); 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 * 100 SBFunction::GetMangledName () const 101 { 102 const char *cstr = NULL; 103 if (m_opaque_ptr) 104 cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString(); 105 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 106 if (log) 107 { 108 if (cstr) 109 log->Printf ("SBFunction(%p)::GetMangledName () => \"%s\"", 110 static_cast<void*>(m_opaque_ptr), cstr); 111 else 112 log->Printf ("SBFunction(%p)::GetMangledName () => NULL", 113 static_cast<void*>(m_opaque_ptr)); 114 } 115 return cstr; 116 } 117 118 bool 119 SBFunction::operator == (const SBFunction &rhs) const 120 { 121 return m_opaque_ptr == rhs.m_opaque_ptr; 122 } 123 124 bool 125 SBFunction::operator != (const SBFunction &rhs) const 126 { 127 return m_opaque_ptr != rhs.m_opaque_ptr; 128 } 129 130 bool 131 SBFunction::GetDescription (SBStream &s) 132 { 133 if (m_opaque_ptr) 134 { 135 s.Printf ("SBFunction: id = 0x%8.8" PRIx64 ", name = %s", 136 m_opaque_ptr->GetID(), 137 m_opaque_ptr->GetName().AsCString()); 138 Type *func_type = m_opaque_ptr->GetType(); 139 if (func_type) 140 s.Printf(", type = %s", func_type->GetName().AsCString()); 141 return true; 142 } 143 s.Printf ("No value"); 144 return false; 145 } 146 147 SBInstructionList 148 SBFunction::GetInstructions (SBTarget target) 149 { 150 return GetInstructions (target, NULL); 151 } 152 153 SBInstructionList 154 SBFunction::GetInstructions (SBTarget target, const char *flavor) 155 { 156 SBInstructionList sb_instructions; 157 if (m_opaque_ptr) 158 { 159 Mutex::Locker api_locker; 160 ExecutionContext exe_ctx; 161 TargetSP target_sp (target.GetSP()); 162 if (target_sp) 163 { 164 api_locker.Lock (target_sp->GetAPIMutex()); 165 target_sp->CalculateExecutionContext (exe_ctx); 166 exe_ctx.SetProcessSP(target_sp->GetProcessSP()); 167 } 168 ModuleSP module_sp (m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule()); 169 if (module_sp) 170 { 171 const bool prefer_file_cache = false; 172 sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture(), 173 NULL, 174 flavor, 175 exe_ctx, 176 m_opaque_ptr->GetAddressRange(), 177 prefer_file_cache)); 178 } 179 } 180 return sb_instructions; 181 } 182 183 lldb_private::Function * 184 SBFunction::get () 185 { 186 return m_opaque_ptr; 187 } 188 189 void 190 SBFunction::reset (lldb_private::Function *lldb_object_ptr) 191 { 192 m_opaque_ptr = lldb_object_ptr; 193 } 194 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 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 } 238 239 uint32_t 240 SBFunction::GetPrologueByteSize () 241 { 242 if (m_opaque_ptr) 243 return m_opaque_ptr->GetPrologueByteSize(); 244 return 0; 245 } 246 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 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 } 279 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 } 290