1 //===-- ItaniumABILanguageRuntime.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ItaniumABILanguageRuntime.h"
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/SectionLoadList.h"
27 #include "lldb/Target/StopInfo.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
34 using namespace lldb_private;
36 static const char *vtable_demangled_prefix = "vtable for ";
39 ItaniumABILanguageRuntime::CouldHaveDynamicValue (ValueObject &in_value)
41 const bool check_cxx = true;
42 const bool check_objc = false;
43 return in_value.GetClangType().IsPossibleDynamicType (NULL, check_cxx, check_objc);
47 ItaniumABILanguageRuntime::GetDynamicTypeAndAddress (ValueObject &in_value,
48 lldb::DynamicValueType use_dynamic,
49 TypeAndOrName &class_type_or_name,
50 Address &dynamic_address)
52 // For Itanium, if the type has a vtable pointer in the object, it will be at offset 0
53 // in the object. That will point to the "address point" within the vtable (not the beginning of the
54 // vtable.) We can then look up the symbol containing this "address point" and that symbol's name
55 // demangled will contain the full class name.
56 // The second pointer above the "address point" is the "offset_to_top". We'll use that to get the
57 // start of the value object which holds the dynamic type.
60 class_type_or_name.Clear();
62 // Only a pointer or reference type can have a different dynamic and static type:
63 if (CouldHaveDynamicValue (in_value))
65 // First job, pull out the address at 0 offset from the object.
66 AddressType address_type;
67 lldb::addr_t original_ptr = in_value.GetPointerValue(&address_type);
68 if (original_ptr == LLDB_INVALID_ADDRESS)
71 ExecutionContext exe_ctx (in_value.GetExecutionContextRef());
73 Target *target = exe_ctx.GetTargetPtr();
74 Process *process = exe_ctx.GetProcessPtr();
76 char memory_buffer[16];
77 DataExtractor data(memory_buffer, sizeof(memory_buffer),
78 process->GetByteOrder(),
79 process->GetAddressByteSize());
80 size_t address_byte_size = process->GetAddressByteSize();
82 size_t bytes_read = process->ReadMemory (original_ptr,
86 if (!error.Success() || (bytes_read != address_byte_size))
91 lldb::offset_t offset = 0;
92 lldb::addr_t vtable_address_point = data.GetAddress (&offset);
97 // Now find the symbol that contains this address:
100 Address address_point_address;
101 if (target && !target->GetSectionLoadList().IsEmpty())
103 if (target->GetSectionLoadList().ResolveLoadAddress (vtable_address_point, address_point_address))
105 target->GetImages().ResolveSymbolContextForAddress (address_point_address, eSymbolContextSymbol, sc);
106 Symbol *symbol = sc.symbol;
109 const char *name = symbol->GetMangled().GetDemangledName().AsCString();
110 if (strstr(name, vtable_demangled_prefix) == name)
112 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
114 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has vtable symbol '%s'\n",
116 in_value.GetTypeName().GetCString(),
118 // We are a C++ class, that's good. Get the class name and look it up:
119 const char *class_name = name + strlen(vtable_demangled_prefix);
120 class_type_or_name.SetName (class_name);
121 const bool exact_match = true;
122 TypeList class_types;
124 uint32_t num_matches = 0;
125 // First look in the module that the vtable symbol came from
126 // and look for a single exact match.
129 num_matches = sc.module_sp->FindTypes (sc,
130 ConstString(class_name),
136 // If we didn't find a symbol, then move on to the entire
137 // module list in the target and get as many unique matches
139 if (num_matches == 0)
141 num_matches = target->GetImages().FindTypes (sc,
142 ConstString(class_name),
148 lldb::TypeSP type_sp;
149 if (num_matches == 0)
152 log->Printf("0x%16.16" PRIx64 ": is not dynamic\n", original_ptr);
155 if (num_matches == 1)
157 type_sp = class_types.GetTypeAtIndex(0);
159 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has dynamic type: uid={0x%" PRIx64 "}, type-name='%s'\n",
161 in_value.GetTypeName().AsCString(),
163 type_sp->GetName().GetCString());
165 class_type_or_name.SetTypeSP(class_types.GetTypeAtIndex(0));
167 else if (num_matches > 1)
172 for (i = 0; i < num_matches; i++)
174 type_sp = class_types.GetTypeAtIndex(i);
178 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has multiple matching dynamic types: uid={0x%" PRIx64 "}, type-name='%s'\n",
180 in_value.GetTypeName().AsCString(),
182 type_sp->GetName().GetCString());
187 for (i = 0; i < num_matches; i++)
189 type_sp = class_types.GetTypeAtIndex(i);
192 if (type_sp->GetClangFullType().IsCXXClassType())
195 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has multiple matching dynamic types, picking this one: uid={0x%" PRIx64 "}, type-name='%s'\n",
197 in_value.GetTypeName().AsCString(),
199 type_sp->GetName().GetCString());
200 class_type_or_name.SetTypeSP(type_sp);
206 if (i == num_matches)
209 log->Printf ("0x%16.16" PRIx64 ": static-type = '%s' has multiple matching dynamic types, didn't find a C++ match\n",
211 in_value.GetTypeName().AsCString());
216 // There can only be one type with a given name,
217 // so we've just found duplicate definitions, and this
218 // one will do as well as any other.
219 // We don't consider something to have a dynamic type if
220 // it is the same as the static type. So compare against
221 // the value we were handed.
224 if (ClangASTContext::AreTypesSame (in_value.GetClangType(),
225 type_sp->GetClangFullType()))
227 // The dynamic type we found was the same type,
228 // so we don't have a dynamic type here...
232 // The offset_to_top is two pointers above the address.
233 Address offset_to_top_address = address_point_address;
234 int64_t slide = -2 * ((int64_t) target->GetArchitecture().GetAddressByteSize());
235 offset_to_top_address.Slide (slide);
238 lldb::addr_t offset_to_top_location = offset_to_top_address.GetLoadAddress(target);
240 size_t bytes_read = process->ReadMemory (offset_to_top_location,
245 if (!error.Success() || (bytes_read != address_byte_size))
251 int64_t offset_to_top = data.GetMaxS64(&offset, process->GetAddressByteSize());
253 // So the dynamic type is a value that starts at offset_to_top
254 // above the original address.
255 lldb::addr_t dynamic_addr = original_ptr + offset_to_top;
256 if (!target->GetSectionLoadList().ResolveLoadAddress (dynamic_addr, dynamic_address))
258 dynamic_address.SetRawAddress(dynamic_addr);
268 return class_type_or_name.IsEmpty() == false;
272 ItaniumABILanguageRuntime::IsVTableName (const char *name)
277 // Can we maybe ask Clang about this?
278 if (strstr (name, "_vptr$") == name)
284 //------------------------------------------------------------------
286 //------------------------------------------------------------------
288 ItaniumABILanguageRuntime::CreateInstance (Process *process, lldb::LanguageType language)
290 // FIXME: We have to check the process and make sure we actually know that this process supports
292 if (language == eLanguageTypeC_plus_plus)
293 return new ItaniumABILanguageRuntime (process);
299 ItaniumABILanguageRuntime::Initialize()
301 PluginManager::RegisterPlugin (GetPluginNameStatic(),
302 "Itanium ABI for the C++ language",
307 ItaniumABILanguageRuntime::Terminate()
309 PluginManager::UnregisterPlugin (CreateInstance);
312 lldb_private::ConstString
313 ItaniumABILanguageRuntime::GetPluginNameStatic()
315 static ConstString g_name("itanium");
319 //------------------------------------------------------------------
320 // PluginInterface protocol
321 //------------------------------------------------------------------
322 lldb_private::ConstString
323 ItaniumABILanguageRuntime::GetPluginName()
325 return GetPluginNameStatic();
329 ItaniumABILanguageRuntime::GetPluginVersion()
335 ItaniumABILanguageRuntime::CreateExceptionResolver (Breakpoint *bkpt, bool catch_bp, bool throw_bp)
337 return CreateExceptionResolver (bkpt, catch_bp, throw_bp, false);
341 ItaniumABILanguageRuntime::CreateExceptionResolver (Breakpoint *bkpt, bool catch_bp, bool throw_bp, bool for_expressions)
343 // One complication here is that most users DON'T want to stop at __cxa_allocate_expression, but until we can do
344 // anything better with predicting unwinding the expression parser does. So we have two forms of the exception
345 // breakpoints, one for expressions that leaves out __cxa_allocate_exception, and one that includes it.
346 // The SetExceptionBreakpoints does the latter, the CreateExceptionBreakpoint in the runtime the former.
347 static const char *g_catch_name = "__cxa_begin_catch";
348 static const char *g_throw_name1 = "__cxa_throw";
349 static const char *g_throw_name2 = "__cxa_rethrow";
350 static const char *g_exception_throw_name = "__cxa_allocate_exception";
351 std::vector<const char *> exception_names;
352 exception_names.reserve(4);
354 exception_names.push_back(g_catch_name);
358 exception_names.push_back(g_throw_name1);
359 exception_names.push_back(g_throw_name2);
363 exception_names.push_back(g_exception_throw_name);
365 BreakpointResolverSP resolver_sp (new BreakpointResolverName (bkpt,
366 exception_names.data(),
367 exception_names.size(),
368 eFunctionNameTypeBase,
377 ItaniumABILanguageRuntime::CreateExceptionSearchFilter ()
379 Target &target = m_process->GetTarget();
381 if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple)
383 // Limit the number of modules that are searched for these breakpoints for
385 FileSpecList filter_modules;
386 filter_modules.Append(FileSpec("libc++abi.dylib", false));
387 filter_modules.Append(FileSpec("libSystem.B.dylib", false));
388 return target.GetSearchFilterForModuleList(&filter_modules);
392 return LanguageRuntime::CreateExceptionSearchFilter();
397 ItaniumABILanguageRuntime::CreateExceptionBreakpoint (bool catch_bp,
399 bool for_expressions,
402 Target &target = m_process->GetTarget();
403 FileSpecList filter_modules;
404 BreakpointResolverSP exception_resolver_sp = CreateExceptionResolver (NULL, catch_bp, throw_bp, for_expressions);
405 SearchFilterSP filter_sp (CreateExceptionSearchFilter ());
406 const bool hardware = false;
407 const bool resolve_indirect_functions = false;
408 return target.CreateBreakpoint (filter_sp, exception_resolver_sp, is_internal, hardware, resolve_indirect_functions);
412 ItaniumABILanguageRuntime::SetExceptionBreakpoints ()
417 const bool catch_bp = false;
418 const bool throw_bp = true;
419 const bool is_internal = true;
420 const bool for_expressions = true;
422 // For the exception breakpoints set by the Expression parser, we'll be a little more aggressive and
423 // stop at exception allocation as well.
425 if (m_cxx_exception_bp_sp)
427 m_cxx_exception_bp_sp->SetEnabled (true);
431 m_cxx_exception_bp_sp = CreateExceptionBreakpoint (catch_bp, throw_bp, for_expressions, is_internal);
432 if (m_cxx_exception_bp_sp)
433 m_cxx_exception_bp_sp->SetBreakpointKind("c++ exception");
439 ItaniumABILanguageRuntime::ClearExceptionBreakpoints ()
444 if (m_cxx_exception_bp_sp)
446 m_cxx_exception_bp_sp->SetEnabled (false);
451 ItaniumABILanguageRuntime::ExceptionBreakpointsAreSet ()
453 return m_cxx_exception_bp_sp && m_cxx_exception_bp_sp->IsEnabled();
457 ItaniumABILanguageRuntime::ExceptionBreakpointsExplainStop (lldb::StopInfoSP stop_reason)
463 stop_reason->GetStopReason() != eStopReasonBreakpoint)
466 uint64_t break_site_id = stop_reason->GetValue();
467 return m_process->GetBreakpointSiteList().BreakpointSiteContainsBreakpoint(break_site_id,
468 m_cxx_exception_bp_sp->GetID());