1 //===-- ItaniumABILanguageRuntime.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 "ItaniumABILanguageRuntime.h" 11 12 #include "lldb/Breakpoint/BreakpointLocation.h" 13 #include "lldb/Core/ConstString.h" 14 #include "lldb/Core/Error.h" 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/Scalar.h" 19 #include "lldb/Core/ValueObject.h" 20 #include "lldb/Core/ValueObjectMemory.h" 21 #include "lldb/Symbol/ClangASTContext.h" 22 #include "lldb/Symbol/Symbol.h" 23 #include "lldb/Symbol/TypeList.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Target/RegisterContext.h" 26 #include "lldb/Target/StopInfo.h" 27 #include "lldb/Target/Target.h" 28 #include "lldb/Target/Thread.h" 29 30 #include <vector> 31 32 using namespace lldb; 33 using namespace lldb_private; 34 35 static const char *vtable_demangled_prefix = "vtable for "; 36 37 bool 38 ItaniumABILanguageRuntime::CouldHaveDynamicValue (ValueObject &in_value) 39 { 40 return ClangASTContext::IsPossibleDynamicType(in_value.GetClangAST(), in_value.GetClangType(), NULL, 41 true, // check for C++ 42 false); // do not check for ObjC 43 } 44 45 bool 46 ItaniumABILanguageRuntime::GetDynamicTypeAndAddress (ValueObject &in_value, 47 lldb::DynamicValueType use_dynamic, 48 TypeAndOrName &class_type_or_name, 49 Address &dynamic_address) 50 { 51 // For Itanium, if the type has a vtable pointer in the object, it will be at offset 0 52 // in the object. That will point to the "address point" within the vtable (not the beginning of the 53 // vtable.) We can then look up the symbol containing this "address point" and that symbol's name 54 // demangled will contain the full class name. 55 // The second pointer above the "address point" is the "offset_to_top". We'll use that to get the 56 // start of the value object which holds the dynamic type. 57 // 58 59 class_type_or_name.Clear(); 60 61 // Only a pointer or reference type can have a different dynamic and static type: 62 if (CouldHaveDynamicValue (in_value)) 63 { 64 // First job, pull out the address at 0 offset from the object. 65 AddressType address_type; 66 lldb::addr_t original_ptr = in_value.GetPointerValue(&address_type); 67 if (original_ptr == LLDB_INVALID_ADDRESS) 68 return false; 69 70 ExecutionContext exe_ctx (in_value.GetExecutionContextRef()); 71 72 Target *target = exe_ctx.GetTargetPtr(); 73 Process *process = exe_ctx.GetProcessPtr(); 74 75 char memory_buffer[16]; 76 DataExtractor data(memory_buffer, sizeof(memory_buffer), 77 process->GetByteOrder(), 78 process->GetAddressByteSize()); 79 size_t address_byte_size = process->GetAddressByteSize(); 80 Error error; 81 size_t bytes_read = process->ReadMemory (original_ptr, 82 memory_buffer, 83 address_byte_size, 84 error); 85 if (!error.Success() || (bytes_read != address_byte_size)) 86 { 87 return false; 88 } 89 90 lldb::offset_t offset = 0; 91 lldb::addr_t vtable_address_point = data.GetAddress (&offset); 92 93 if (offset == 0) 94 return false; 95 96 // Now find the symbol that contains this address: 97 98 SymbolContext sc; 99 Address address_point_address; 100 if (target && !target->GetSectionLoadList().IsEmpty()) 101 { 102 if (target->GetSectionLoadList().ResolveLoadAddress (vtable_address_point, address_point_address)) 103 { 104 target->GetImages().ResolveSymbolContextForAddress (address_point_address, eSymbolContextSymbol, sc); 105 Symbol *symbol = sc.symbol; 106 if (symbol != NULL) 107 { 108 const char *name = symbol->GetMangled().GetDemangledName().AsCString(); 109 if (strstr(name, vtable_demangled_prefix) == name) 110 { 111 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 112 if (log) 113 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has vtable symbol '%s'\n", 114 original_ptr, 115 in_value.GetTypeName().GetCString(), 116 name); 117 // We are a C++ class, that's good. Get the class name and look it up: 118 const char *class_name = name + strlen(vtable_demangled_prefix); 119 class_type_or_name.SetName (class_name); 120 const bool exact_match = true; 121 TypeList class_types; 122 123 uint32_t num_matches = 0; 124 // First look in the module that the vtable symbol came from 125 // and look for a single exact match. 126 if (sc.module_sp) 127 { 128 num_matches = sc.module_sp->FindTypes (sc, 129 ConstString(class_name), 130 exact_match, 131 1, 132 class_types); 133 } 134 135 // If we didn't find a symbol, then move on to the entire 136 // module list in the target and get as many unique matches 137 // as possible 138 if (num_matches == 0) 139 { 140 num_matches = target->GetImages().FindTypes (sc, 141 ConstString(class_name), 142 exact_match, 143 UINT32_MAX, 144 class_types); 145 } 146 147 lldb::TypeSP type_sp; 148 if (num_matches == 0) 149 { 150 if (log) 151 log->Printf("0x%16.16" PRIx64 ": is not dynamic\n", original_ptr); 152 return false; 153 } 154 if (num_matches == 1) 155 { 156 type_sp = class_types.GetTypeAtIndex(0); 157 if (log) 158 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has dynamic type: uid={0x%" PRIx64 "}, type-name='%s'\n", 159 original_ptr, 160 in_value.GetTypeName().AsCString(), 161 type_sp->GetID(), 162 type_sp->GetName().GetCString()); 163 164 class_type_or_name.SetTypeSP(class_types.GetTypeAtIndex(0)); 165 } 166 else if (num_matches > 1) 167 { 168 size_t i; 169 if (log) 170 { 171 for (i = 0; i < num_matches; i++) 172 { 173 type_sp = class_types.GetTypeAtIndex(i); 174 if (type_sp) 175 { 176 if (log) 177 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has multiple matching dynamic types: uid={0x%" PRIx64 "}, type-name='%s'\n", 178 original_ptr, 179 in_value.GetTypeName().AsCString(), 180 type_sp->GetID(), 181 type_sp->GetName().GetCString()); 182 } 183 } 184 } 185 186 for (i = 0; i < num_matches; i++) 187 { 188 type_sp = class_types.GetTypeAtIndex(i); 189 if (type_sp) 190 { 191 if (ClangASTContext::IsCXXClassType(type_sp->GetClangFullType())) 192 { 193 if (log) 194 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has multiple matching dynamic types, picking this one: uid={0x%" PRIx64 "}, type-name='%s'\n", 195 original_ptr, 196 in_value.GetTypeName().AsCString(), 197 type_sp->GetID(), 198 type_sp->GetName().GetCString()); 199 class_type_or_name.SetTypeSP(type_sp); 200 break; 201 } 202 } 203 } 204 205 if (i == num_matches) 206 { 207 if (log) 208 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has multiple matching dynamic types, didn't find a C++ match\n", 209 original_ptr, 210 in_value.GetTypeName().AsCString()); 211 return false; 212 } 213 } 214 215 // There can only be one type with a given name, 216 // so we've just found duplicate definitions, and this 217 // one will do as well as any other. 218 // We don't consider something to have a dynamic type if 219 // it is the same as the static type. So compare against 220 // the value we were handed. 221 if (type_sp) 222 { 223 clang::ASTContext *in_ast_ctx = in_value.GetClangAST (); 224 clang::ASTContext *this_ast_ctx = type_sp->GetClangAST (); 225 if (in_ast_ctx == this_ast_ctx) 226 { 227 if (ClangASTContext::AreTypesSame (in_ast_ctx, 228 in_value.GetClangType(), 229 type_sp->GetClangFullType())) 230 { 231 // The dynamic type we found was the same type, 232 // so we don't have a dynamic type here... 233 return false; 234 } 235 } 236 237 // The offset_to_top is two pointers above the address. 238 Address offset_to_top_address = address_point_address; 239 int64_t slide = -2 * ((int64_t) target->GetArchitecture().GetAddressByteSize()); 240 offset_to_top_address.Slide (slide); 241 242 Error error; 243 lldb::addr_t offset_to_top_location = offset_to_top_address.GetLoadAddress(target); 244 245 size_t bytes_read = process->ReadMemory (offset_to_top_location, 246 memory_buffer, 247 address_byte_size, 248 error); 249 250 if (!error.Success() || (bytes_read != address_byte_size)) 251 { 252 return false; 253 } 254 255 offset = 0; 256 int64_t offset_to_top = data.GetMaxS64(&offset, process->GetAddressByteSize()); 257 258 // So the dynamic type is a value that starts at offset_to_top 259 // above the original address. 260 lldb::addr_t dynamic_addr = original_ptr + offset_to_top; 261 if (!target->GetSectionLoadList().ResolveLoadAddress (dynamic_addr, dynamic_address)) 262 { 263 dynamic_address.SetRawAddress(dynamic_addr); 264 } 265 return true; 266 } 267 } 268 } 269 } 270 } 271 } 272 273 return class_type_or_name.IsEmpty() == false; 274 } 275 276 bool 277 ItaniumABILanguageRuntime::IsVTableName (const char *name) 278 { 279 if (name == NULL) 280 return false; 281 282 // Can we maybe ask Clang about this? 283 if (strstr (name, "_vptr$") == name) 284 return true; 285 else 286 return false; 287 } 288 289 //------------------------------------------------------------------ 290 // Static Functions 291 //------------------------------------------------------------------ 292 LanguageRuntime * 293 ItaniumABILanguageRuntime::CreateInstance (Process *process, lldb::LanguageType language) 294 { 295 // FIXME: We have to check the process and make sure we actually know that this process supports 296 // the Itanium ABI. 297 if (language == eLanguageTypeC_plus_plus) 298 return new ItaniumABILanguageRuntime (process); 299 else 300 return NULL; 301 } 302 303 void 304 ItaniumABILanguageRuntime::Initialize() 305 { 306 PluginManager::RegisterPlugin (GetPluginNameStatic(), 307 "Itanium ABI for the C++ language", 308 CreateInstance); 309 } 310 311 void 312 ItaniumABILanguageRuntime::Terminate() 313 { 314 PluginManager::UnregisterPlugin (CreateInstance); 315 } 316 317 lldb_private::ConstString 318 ItaniumABILanguageRuntime::GetPluginNameStatic() 319 { 320 static ConstString g_name("itanium"); 321 return g_name; 322 } 323 324 //------------------------------------------------------------------ 325 // PluginInterface protocol 326 //------------------------------------------------------------------ 327 lldb_private::ConstString 328 ItaniumABILanguageRuntime::GetPluginName() 329 { 330 return GetPluginNameStatic(); 331 } 332 333 uint32_t 334 ItaniumABILanguageRuntime::GetPluginVersion() 335 { 336 return 1; 337 } 338 339 BreakpointResolverSP 340 ItaniumABILanguageRuntime::CreateExceptionResolver (Breakpoint *bkpt, bool catch_bp, bool throw_bp) 341 { 342 return CreateExceptionResolver (bkpt, catch_bp, throw_bp, false); 343 } 344 345 BreakpointResolverSP 346 ItaniumABILanguageRuntime::CreateExceptionResolver (Breakpoint *bkpt, bool catch_bp, bool throw_bp, bool for_expressions) 347 { 348 // One complication here is that most users DON'T want to stop at __cxa_allocate_expression, but until we can do 349 // anything better with predicting unwinding the expression parser does. So we have two forms of the exception 350 // breakpoints, one for expressions that leaves out __cxa_allocate_exception, and one that includes it. 351 // The SetExceptionBreakpoints does the latter, the CreateExceptionBreakpoint in the runtime the former. 352 static const char *g_catch_name = "__cxa_begin_catch"; 353 static const char *g_throw_name1 = "__cxa_throw"; 354 static const char *g_throw_name2 = "__cxa_rethrow"; 355 static const char *g_exception_throw_name = "__cxa_allocate_exception"; 356 std::vector<const char *> exception_names; 357 exception_names.reserve(4); 358 if (catch_bp) 359 exception_names.push_back(g_catch_name); 360 361 if (throw_bp) 362 { 363 exception_names.push_back(g_throw_name1); 364 exception_names.push_back(g_throw_name2); 365 } 366 367 if (for_expressions) 368 exception_names.push_back(g_exception_throw_name); 369 370 BreakpointResolverSP resolver_sp (new BreakpointResolverName (bkpt, 371 exception_names.data(), 372 exception_names.size(), 373 eFunctionNameTypeBase, 374 eLazyBoolNo)); 375 376 return resolver_sp; 377 } 378 379 380 381 lldb::SearchFilterSP 382 ItaniumABILanguageRuntime::CreateExceptionSearchFilter () 383 { 384 Target &target = m_process->GetTarget(); 385 386 if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple) 387 { 388 // Limit the number of modules that are searched for these breakpoints for 389 // Apple binaries. 390 FileSpecList filter_modules; 391 filter_modules.Append(FileSpec("libc++abi.dylib", false)); 392 filter_modules.Append(FileSpec("libSystem.B.dylib", false)); 393 return target.GetSearchFilterForModuleList(&filter_modules); 394 } 395 else 396 { 397 return LanguageRuntime::CreateExceptionSearchFilter(); 398 } 399 } 400 401 lldb::BreakpointSP 402 ItaniumABILanguageRuntime::CreateExceptionBreakpoint (bool catch_bp, 403 bool throw_bp, 404 bool for_expressions, 405 bool is_internal) 406 { 407 Target &target = m_process->GetTarget(); 408 FileSpecList filter_modules; 409 BreakpointResolverSP exception_resolver_sp = CreateExceptionResolver (NULL, catch_bp, throw_bp, for_expressions); 410 SearchFilterSP filter_sp (CreateExceptionSearchFilter ()); 411 return target.CreateBreakpoint (filter_sp, exception_resolver_sp, is_internal); 412 } 413 414 void 415 ItaniumABILanguageRuntime::SetExceptionBreakpoints () 416 { 417 if (!m_process) 418 return; 419 420 const bool catch_bp = false; 421 const bool throw_bp = true; 422 const bool is_internal = true; 423 const bool for_expressions = true; 424 425 // For the exception breakpoints set by the Expression parser, we'll be a little more aggressive and 426 // stop at exception allocation as well. 427 428 if (m_cxx_exception_bp_sp) 429 { 430 m_cxx_exception_bp_sp->SetEnabled (true); 431 } 432 else 433 { 434 m_cxx_exception_bp_sp = CreateExceptionBreakpoint (catch_bp, throw_bp, for_expressions, is_internal); 435 if (m_cxx_exception_bp_sp) 436 m_cxx_exception_bp_sp->SetBreakpointKind("c++ exception"); 437 } 438 439 } 440 441 void 442 ItaniumABILanguageRuntime::ClearExceptionBreakpoints () 443 { 444 if (!m_process) 445 return; 446 447 if (m_cxx_exception_bp_sp) 448 { 449 m_cxx_exception_bp_sp->SetEnabled (false); 450 } 451 } 452 453 bool 454 ItaniumABILanguageRuntime::ExceptionBreakpointsExplainStop (lldb::StopInfoSP stop_reason) 455 { 456 if (!m_process) 457 return false; 458 459 if (!stop_reason || 460 stop_reason->GetStopReason() != eStopReasonBreakpoint) 461 return false; 462 463 uint64_t break_site_id = stop_reason->GetValue(); 464 return m_process->GetBreakpointSiteList().BreakpointSiteContainsBreakpoint(break_site_id, 465 m_cxx_exception_bp_sp->GetID()); 466 467 } 468