1dda28197Spatrick //===-- ItaniumABILanguageRuntime.cpp -------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "ItaniumABILanguageRuntime.h"
10061da546Spatrick
11dda28197Spatrick #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12061da546Spatrick #include "lldb/Breakpoint/BreakpointLocation.h"
13061da546Spatrick #include "lldb/Core/Mangled.h"
14061da546Spatrick #include "lldb/Core/Module.h"
15061da546Spatrick #include "lldb/Core/PluginManager.h"
16061da546Spatrick #include "lldb/Core/ValueObject.h"
17061da546Spatrick #include "lldb/Core/ValueObjectMemory.h"
18061da546Spatrick #include "lldb/DataFormatters/FormattersHelpers.h"
19061da546Spatrick #include "lldb/Expression/DiagnosticManager.h"
20061da546Spatrick #include "lldb/Expression/FunctionCaller.h"
21061da546Spatrick #include "lldb/Interpreter/CommandObject.h"
22061da546Spatrick #include "lldb/Interpreter/CommandObjectMultiword.h"
23061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
24061da546Spatrick #include "lldb/Symbol/Symbol.h"
25061da546Spatrick #include "lldb/Symbol/SymbolFile.h"
26061da546Spatrick #include "lldb/Symbol/TypeList.h"
27061da546Spatrick #include "lldb/Target/Process.h"
28061da546Spatrick #include "lldb/Target/RegisterContext.h"
29061da546Spatrick #include "lldb/Target/SectionLoadList.h"
30061da546Spatrick #include "lldb/Target/StopInfo.h"
31061da546Spatrick #include "lldb/Target/Target.h"
32061da546Spatrick #include "lldb/Target/Thread.h"
33061da546Spatrick #include "lldb/Utility/ConstString.h"
34*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
35061da546Spatrick #include "lldb/Utility/Log.h"
36061da546Spatrick #include "lldb/Utility/Scalar.h"
37061da546Spatrick #include "lldb/Utility/Status.h"
38061da546Spatrick
39061da546Spatrick #include <vector>
40061da546Spatrick
41061da546Spatrick using namespace lldb;
42061da546Spatrick using namespace lldb_private;
43061da546Spatrick
44dda28197Spatrick LLDB_PLUGIN_DEFINE_ADV(ItaniumABILanguageRuntime, CXXItaniumABI)
45dda28197Spatrick
46061da546Spatrick static const char *vtable_demangled_prefix = "vtable for ";
47061da546Spatrick
48061da546Spatrick char ItaniumABILanguageRuntime::ID = 0;
49061da546Spatrick
CouldHaveDynamicValue(ValueObject & in_value)50061da546Spatrick bool ItaniumABILanguageRuntime::CouldHaveDynamicValue(ValueObject &in_value) {
51061da546Spatrick const bool check_cxx = true;
52061da546Spatrick const bool check_objc = false;
53061da546Spatrick return in_value.GetCompilerType().IsPossibleDynamicType(nullptr, check_cxx,
54061da546Spatrick check_objc);
55061da546Spatrick }
56061da546Spatrick
GetTypeInfoFromVTableAddress(ValueObject & in_value,lldb::addr_t original_ptr,lldb::addr_t vtable_load_addr)57061da546Spatrick TypeAndOrName ItaniumABILanguageRuntime::GetTypeInfoFromVTableAddress(
58061da546Spatrick ValueObject &in_value, lldb::addr_t original_ptr,
59061da546Spatrick lldb::addr_t vtable_load_addr) {
60061da546Spatrick if (m_process && vtable_load_addr != LLDB_INVALID_ADDRESS) {
61061da546Spatrick // Find the symbol that contains the "vtable_load_addr" address
62061da546Spatrick Address vtable_addr;
63061da546Spatrick Target &target = m_process->GetTarget();
64061da546Spatrick if (!target.GetSectionLoadList().IsEmpty()) {
65061da546Spatrick if (target.GetSectionLoadList().ResolveLoadAddress(vtable_load_addr,
66061da546Spatrick vtable_addr)) {
67061da546Spatrick // See if we have cached info for this type already
68061da546Spatrick TypeAndOrName type_info = GetDynamicTypeInfo(vtable_addr);
69061da546Spatrick if (type_info)
70061da546Spatrick return type_info;
71061da546Spatrick
72061da546Spatrick SymbolContext sc;
73061da546Spatrick target.GetImages().ResolveSymbolContextForAddress(
74061da546Spatrick vtable_addr, eSymbolContextSymbol, sc);
75061da546Spatrick Symbol *symbol = sc.symbol;
76061da546Spatrick if (symbol != nullptr) {
77061da546Spatrick const char *name =
78dda28197Spatrick symbol->GetMangled().GetDemangledName().AsCString();
79061da546Spatrick if (name && strstr(name, vtable_demangled_prefix) == name) {
80*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Object);
81061da546Spatrick LLDB_LOGF(log,
82061da546Spatrick "0x%16.16" PRIx64
83061da546Spatrick ": static-type = '%s' has vtable symbol '%s'\n",
84061da546Spatrick original_ptr, in_value.GetTypeName().GetCString(), name);
85061da546Spatrick // We are a C++ class, that's good. Get the class name and look it
86061da546Spatrick // up:
87061da546Spatrick const char *class_name = name + strlen(vtable_demangled_prefix);
88061da546Spatrick // We know the class name is absolute, so tell FindTypes that by
89061da546Spatrick // prefixing it with the root namespace:
90061da546Spatrick std::string lookup_name("::");
91061da546Spatrick lookup_name.append(class_name);
92061da546Spatrick
93061da546Spatrick type_info.SetName(class_name);
94061da546Spatrick const bool exact_match = true;
95061da546Spatrick TypeList class_types;
96061da546Spatrick
97061da546Spatrick // First look in the module that the vtable symbol came from and
98061da546Spatrick // look for a single exact match.
99061da546Spatrick llvm::DenseSet<SymbolFile *> searched_symbol_files;
100061da546Spatrick if (sc.module_sp)
101061da546Spatrick sc.module_sp->FindTypes(ConstString(lookup_name), exact_match, 1,
102061da546Spatrick searched_symbol_files, class_types);
103061da546Spatrick
104061da546Spatrick // If we didn't find a symbol, then move on to the entire module
105061da546Spatrick // list in the target and get as many unique matches as possible
106061da546Spatrick if (class_types.Empty())
107061da546Spatrick target.GetImages().FindTypes(nullptr, ConstString(lookup_name),
108061da546Spatrick exact_match, UINT32_MAX,
109061da546Spatrick searched_symbol_files, class_types);
110061da546Spatrick
111061da546Spatrick lldb::TypeSP type_sp;
112061da546Spatrick if (class_types.Empty()) {
113061da546Spatrick LLDB_LOGF(log, "0x%16.16" PRIx64 ": is not dynamic\n",
114061da546Spatrick original_ptr);
115061da546Spatrick return TypeAndOrName();
116061da546Spatrick }
117061da546Spatrick if (class_types.GetSize() == 1) {
118061da546Spatrick type_sp = class_types.GetTypeAtIndex(0);
119061da546Spatrick if (type_sp) {
120dda28197Spatrick if (TypeSystemClang::IsCXXClassType(
121061da546Spatrick type_sp->GetForwardCompilerType())) {
122061da546Spatrick LLDB_LOGF(
123061da546Spatrick log,
124061da546Spatrick "0x%16.16" PRIx64
125061da546Spatrick ": static-type = '%s' has dynamic type: uid={0x%" PRIx64
126061da546Spatrick "}, type-name='%s'\n",
127061da546Spatrick original_ptr, in_value.GetTypeName().AsCString(),
128061da546Spatrick type_sp->GetID(), type_sp->GetName().GetCString());
129061da546Spatrick type_info.SetTypeSP(type_sp);
130061da546Spatrick }
131061da546Spatrick }
132061da546Spatrick } else {
133061da546Spatrick size_t i;
134061da546Spatrick if (log) {
135061da546Spatrick for (i = 0; i < class_types.GetSize(); i++) {
136061da546Spatrick type_sp = class_types.GetTypeAtIndex(i);
137061da546Spatrick if (type_sp) {
138061da546Spatrick LLDB_LOGF(
139061da546Spatrick log,
140061da546Spatrick "0x%16.16" PRIx64
141061da546Spatrick ": static-type = '%s' has multiple matching dynamic "
142061da546Spatrick "types: uid={0x%" PRIx64 "}, type-name='%s'\n",
143061da546Spatrick original_ptr, in_value.GetTypeName().AsCString(),
144061da546Spatrick type_sp->GetID(), type_sp->GetName().GetCString());
145061da546Spatrick }
146061da546Spatrick }
147061da546Spatrick }
148061da546Spatrick
149061da546Spatrick for (i = 0; i < class_types.GetSize(); i++) {
150061da546Spatrick type_sp = class_types.GetTypeAtIndex(i);
151061da546Spatrick if (type_sp) {
152dda28197Spatrick if (TypeSystemClang::IsCXXClassType(
153061da546Spatrick type_sp->GetForwardCompilerType())) {
154061da546Spatrick LLDB_LOGF(
155061da546Spatrick log,
156061da546Spatrick "0x%16.16" PRIx64 ": static-type = '%s' has multiple "
157061da546Spatrick "matching dynamic types, picking "
158061da546Spatrick "this one: uid={0x%" PRIx64 "}, type-name='%s'\n",
159061da546Spatrick original_ptr, in_value.GetTypeName().AsCString(),
160061da546Spatrick type_sp->GetID(), type_sp->GetName().GetCString());
161061da546Spatrick type_info.SetTypeSP(type_sp);
162061da546Spatrick }
163061da546Spatrick }
164061da546Spatrick }
165061da546Spatrick
166061da546Spatrick if (log) {
167061da546Spatrick LLDB_LOGF(log,
168061da546Spatrick "0x%16.16" PRIx64
169061da546Spatrick ": static-type = '%s' has multiple matching dynamic "
170061da546Spatrick "types, didn't find a C++ match\n",
171061da546Spatrick original_ptr, in_value.GetTypeName().AsCString());
172061da546Spatrick }
173061da546Spatrick }
174061da546Spatrick if (type_info)
175061da546Spatrick SetDynamicTypeInfo(vtable_addr, type_info);
176061da546Spatrick return type_info;
177061da546Spatrick }
178061da546Spatrick }
179061da546Spatrick }
180061da546Spatrick }
181061da546Spatrick }
182061da546Spatrick return TypeAndOrName();
183061da546Spatrick }
184061da546Spatrick
GetDynamicTypeAndAddress(ValueObject & in_value,lldb::DynamicValueType use_dynamic,TypeAndOrName & class_type_or_name,Address & dynamic_address,Value::ValueType & value_type)185061da546Spatrick bool ItaniumABILanguageRuntime::GetDynamicTypeAndAddress(
186061da546Spatrick ValueObject &in_value, lldb::DynamicValueType use_dynamic,
187061da546Spatrick TypeAndOrName &class_type_or_name, Address &dynamic_address,
188061da546Spatrick Value::ValueType &value_type) {
189061da546Spatrick // For Itanium, if the type has a vtable pointer in the object, it will be at
190061da546Spatrick // offset 0 in the object. That will point to the "address point" within the
191061da546Spatrick // vtable (not the beginning of the vtable.) We can then look up the symbol
192061da546Spatrick // containing this "address point" and that symbol's name demangled will
193061da546Spatrick // contain the full class name. The second pointer above the "address point"
194061da546Spatrick // is the "offset_to_top". We'll use that to get the start of the value
195061da546Spatrick // object which holds the dynamic type.
196061da546Spatrick //
197061da546Spatrick
198061da546Spatrick class_type_or_name.Clear();
199be691f3bSpatrick value_type = Value::ValueType::Scalar;
200061da546Spatrick
201061da546Spatrick // Only a pointer or reference type can have a different dynamic and static
202061da546Spatrick // type:
203061da546Spatrick if (!CouldHaveDynamicValue(in_value))
204061da546Spatrick return false;
205061da546Spatrick
206061da546Spatrick // First job, pull out the address at 0 offset from the object.
207061da546Spatrick AddressType address_type;
208061da546Spatrick lldb::addr_t original_ptr = in_value.GetPointerValue(&address_type);
209061da546Spatrick if (original_ptr == LLDB_INVALID_ADDRESS)
210061da546Spatrick return false;
211061da546Spatrick
212061da546Spatrick ExecutionContext exe_ctx(in_value.GetExecutionContextRef());
213061da546Spatrick
214061da546Spatrick Process *process = exe_ctx.GetProcessPtr();
215061da546Spatrick
216061da546Spatrick if (process == nullptr)
217061da546Spatrick return false;
218061da546Spatrick
219061da546Spatrick Status error;
220061da546Spatrick const lldb::addr_t vtable_address_point =
221061da546Spatrick process->ReadPointerFromMemory(original_ptr, error);
222061da546Spatrick
223061da546Spatrick if (!error.Success() || vtable_address_point == LLDB_INVALID_ADDRESS)
224061da546Spatrick return false;
225061da546Spatrick
226061da546Spatrick class_type_or_name = GetTypeInfoFromVTableAddress(in_value, original_ptr,
227061da546Spatrick vtable_address_point);
228061da546Spatrick
229061da546Spatrick if (!class_type_or_name)
230061da546Spatrick return false;
231061da546Spatrick
232061da546Spatrick CompilerType type = class_type_or_name.GetCompilerType();
233061da546Spatrick // There can only be one type with a given name, so we've just found
234061da546Spatrick // duplicate definitions, and this one will do as well as any other. We
235061da546Spatrick // don't consider something to have a dynamic type if it is the same as
236061da546Spatrick // the static type. So compare against the value we were handed.
237061da546Spatrick if (!type)
238061da546Spatrick return true;
239061da546Spatrick
240dda28197Spatrick if (TypeSystemClang::AreTypesSame(in_value.GetCompilerType(), type)) {
241061da546Spatrick // The dynamic type we found was the same type, so we don't have a
242061da546Spatrick // dynamic type here...
243061da546Spatrick return false;
244061da546Spatrick }
245061da546Spatrick
246061da546Spatrick // The offset_to_top is two pointers above the vtable pointer.
247061da546Spatrick const uint32_t addr_byte_size = process->GetAddressByteSize();
248061da546Spatrick const lldb::addr_t offset_to_top_location =
249061da546Spatrick vtable_address_point - 2 * addr_byte_size;
250061da546Spatrick // Watch for underflow, offset_to_top_location should be less than
251061da546Spatrick // vtable_address_point
252061da546Spatrick if (offset_to_top_location >= vtable_address_point)
253061da546Spatrick return false;
254061da546Spatrick const int64_t offset_to_top = process->ReadSignedIntegerFromMemory(
255061da546Spatrick offset_to_top_location, addr_byte_size, INT64_MIN, error);
256061da546Spatrick
257061da546Spatrick if (offset_to_top == INT64_MIN)
258061da546Spatrick return false;
259061da546Spatrick // So the dynamic type is a value that starts at offset_to_top above
260061da546Spatrick // the original address.
261061da546Spatrick lldb::addr_t dynamic_addr = original_ptr + offset_to_top;
262061da546Spatrick if (!process->GetTarget().GetSectionLoadList().ResolveLoadAddress(
263061da546Spatrick dynamic_addr, dynamic_address)) {
264061da546Spatrick dynamic_address.SetRawAddress(dynamic_addr);
265061da546Spatrick }
266061da546Spatrick return true;
267061da546Spatrick }
268061da546Spatrick
FixUpDynamicType(const TypeAndOrName & type_and_or_name,ValueObject & static_value)269061da546Spatrick TypeAndOrName ItaniumABILanguageRuntime::FixUpDynamicType(
270061da546Spatrick const TypeAndOrName &type_and_or_name, ValueObject &static_value) {
271061da546Spatrick CompilerType static_type(static_value.GetCompilerType());
272061da546Spatrick Flags static_type_flags(static_type.GetTypeInfo());
273061da546Spatrick
274061da546Spatrick TypeAndOrName ret(type_and_or_name);
275061da546Spatrick if (type_and_or_name.HasType()) {
276061da546Spatrick // The type will always be the type of the dynamic object. If our parent's
277061da546Spatrick // type was a pointer, then our type should be a pointer to the type of the
278061da546Spatrick // dynamic object. If a reference, then the original type should be
279061da546Spatrick // okay...
280061da546Spatrick CompilerType orig_type = type_and_or_name.GetCompilerType();
281061da546Spatrick CompilerType corrected_type = orig_type;
282061da546Spatrick if (static_type_flags.AllSet(eTypeIsPointer))
283061da546Spatrick corrected_type = orig_type.GetPointerType();
284061da546Spatrick else if (static_type_flags.AllSet(eTypeIsReference))
285061da546Spatrick corrected_type = orig_type.GetLValueReferenceType();
286061da546Spatrick ret.SetCompilerType(corrected_type);
287061da546Spatrick } else {
288061da546Spatrick // If we are here we need to adjust our dynamic type name to include the
289061da546Spatrick // correct & or * symbol
290061da546Spatrick std::string corrected_name(type_and_or_name.GetName().GetCString());
291061da546Spatrick if (static_type_flags.AllSet(eTypeIsPointer))
292061da546Spatrick corrected_name.append(" *");
293061da546Spatrick else if (static_type_flags.AllSet(eTypeIsReference))
294061da546Spatrick corrected_name.append(" &");
295061da546Spatrick // the parent type should be a correctly pointer'ed or referenc'ed type
296061da546Spatrick ret.SetCompilerType(static_type);
297061da546Spatrick ret.SetName(corrected_name.c_str());
298061da546Spatrick }
299061da546Spatrick return ret;
300061da546Spatrick }
301061da546Spatrick
302061da546Spatrick // Static Functions
303061da546Spatrick LanguageRuntime *
CreateInstance(Process * process,lldb::LanguageType language)304061da546Spatrick ItaniumABILanguageRuntime::CreateInstance(Process *process,
305061da546Spatrick lldb::LanguageType language) {
306061da546Spatrick // FIXME: We have to check the process and make sure we actually know that
307061da546Spatrick // this process supports
308061da546Spatrick // the Itanium ABI.
309061da546Spatrick if (language == eLanguageTypeC_plus_plus ||
310061da546Spatrick language == eLanguageTypeC_plus_plus_03 ||
311061da546Spatrick language == eLanguageTypeC_plus_plus_11 ||
312061da546Spatrick language == eLanguageTypeC_plus_plus_14)
313061da546Spatrick return new ItaniumABILanguageRuntime(process);
314061da546Spatrick else
315061da546Spatrick return nullptr;
316061da546Spatrick }
317061da546Spatrick
318061da546Spatrick class CommandObjectMultiwordItaniumABI_Demangle : public CommandObjectParsed {
319061da546Spatrick public:
CommandObjectMultiwordItaniumABI_Demangle(CommandInterpreter & interpreter)320061da546Spatrick CommandObjectMultiwordItaniumABI_Demangle(CommandInterpreter &interpreter)
321061da546Spatrick : CommandObjectParsed(interpreter, "demangle",
322061da546Spatrick "Demangle a C++ mangled name.",
323061da546Spatrick "language cplusplus demangle") {
324061da546Spatrick CommandArgumentEntry arg;
325061da546Spatrick CommandArgumentData index_arg;
326061da546Spatrick
327061da546Spatrick // Define the first (and only) variant of this arg.
328061da546Spatrick index_arg.arg_type = eArgTypeSymbol;
329061da546Spatrick index_arg.arg_repetition = eArgRepeatPlus;
330061da546Spatrick
331061da546Spatrick // There is only one variant this argument could be; put it into the
332061da546Spatrick // argument entry.
333061da546Spatrick arg.push_back(index_arg);
334061da546Spatrick
335061da546Spatrick // Push the data for the first argument into the m_arguments vector.
336061da546Spatrick m_arguments.push_back(arg);
337061da546Spatrick }
338061da546Spatrick
339061da546Spatrick ~CommandObjectMultiwordItaniumABI_Demangle() override = default;
340061da546Spatrick
341061da546Spatrick protected:
DoExecute(Args & command,CommandReturnObject & result)342061da546Spatrick bool DoExecute(Args &command, CommandReturnObject &result) override {
343061da546Spatrick bool demangled_any = false;
344061da546Spatrick bool error_any = false;
345061da546Spatrick for (auto &entry : command.entries()) {
346061da546Spatrick if (entry.ref().empty())
347061da546Spatrick continue;
348061da546Spatrick
349061da546Spatrick // the actual Mangled class should be strict about this, but on the
350061da546Spatrick // command line if you're copying mangled names out of 'nm' on Darwin,
351061da546Spatrick // they will come out with an extra underscore - be willing to strip this
352061da546Spatrick // on behalf of the user. This is the moral equivalent of the -_/-n
353061da546Spatrick // options to c++filt
354061da546Spatrick auto name = entry.ref();
355061da546Spatrick if (name.startswith("__Z"))
356061da546Spatrick name = name.drop_front();
357061da546Spatrick
358061da546Spatrick Mangled mangled(name);
359061da546Spatrick if (mangled.GuessLanguage() == lldb::eLanguageTypeC_plus_plus) {
360dda28197Spatrick ConstString demangled(mangled.GetDisplayDemangledName());
361061da546Spatrick demangled_any = true;
362061da546Spatrick result.AppendMessageWithFormat("%s ---> %s\n", entry.c_str(),
363061da546Spatrick demangled.GetCString());
364061da546Spatrick } else {
365061da546Spatrick error_any = true;
366061da546Spatrick result.AppendErrorWithFormat("%s is not a valid C++ mangled name\n",
367061da546Spatrick entry.ref().str().c_str());
368061da546Spatrick }
369061da546Spatrick }
370061da546Spatrick
371061da546Spatrick result.SetStatus(
372061da546Spatrick error_any ? lldb::eReturnStatusFailed
373061da546Spatrick : (demangled_any ? lldb::eReturnStatusSuccessFinishResult
374061da546Spatrick : lldb::eReturnStatusSuccessFinishNoResult));
375061da546Spatrick return result.Succeeded();
376061da546Spatrick }
377061da546Spatrick };
378061da546Spatrick
379061da546Spatrick class CommandObjectMultiwordItaniumABI : public CommandObjectMultiword {
380061da546Spatrick public:
CommandObjectMultiwordItaniumABI(CommandInterpreter & interpreter)381061da546Spatrick CommandObjectMultiwordItaniumABI(CommandInterpreter &interpreter)
382061da546Spatrick : CommandObjectMultiword(
383061da546Spatrick interpreter, "cplusplus",
384061da546Spatrick "Commands for operating on the C++ language runtime.",
385061da546Spatrick "cplusplus <subcommand> [<subcommand-options>]") {
386061da546Spatrick LoadSubCommand(
387061da546Spatrick "demangle",
388061da546Spatrick CommandObjectSP(
389061da546Spatrick new CommandObjectMultiwordItaniumABI_Demangle(interpreter)));
390061da546Spatrick }
391061da546Spatrick
392061da546Spatrick ~CommandObjectMultiwordItaniumABI() override = default;
393061da546Spatrick };
394061da546Spatrick
Initialize()395061da546Spatrick void ItaniumABILanguageRuntime::Initialize() {
396061da546Spatrick PluginManager::RegisterPlugin(
397061da546Spatrick GetPluginNameStatic(), "Itanium ABI for the C++ language", CreateInstance,
398061da546Spatrick [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
399061da546Spatrick return CommandObjectSP(
400061da546Spatrick new CommandObjectMultiwordItaniumABI(interpreter));
401061da546Spatrick });
402061da546Spatrick }
403061da546Spatrick
Terminate()404061da546Spatrick void ItaniumABILanguageRuntime::Terminate() {
405061da546Spatrick PluginManager::UnregisterPlugin(CreateInstance);
406061da546Spatrick }
407061da546Spatrick
CreateExceptionResolver(const BreakpointSP & bkpt,bool catch_bp,bool throw_bp)408061da546Spatrick BreakpointResolverSP ItaniumABILanguageRuntime::CreateExceptionResolver(
409dda28197Spatrick const BreakpointSP &bkpt, bool catch_bp, bool throw_bp) {
410061da546Spatrick return CreateExceptionResolver(bkpt, catch_bp, throw_bp, false);
411061da546Spatrick }
412061da546Spatrick
CreateExceptionResolver(const BreakpointSP & bkpt,bool catch_bp,bool throw_bp,bool for_expressions)413061da546Spatrick BreakpointResolverSP ItaniumABILanguageRuntime::CreateExceptionResolver(
414dda28197Spatrick const BreakpointSP &bkpt, bool catch_bp, bool throw_bp,
415dda28197Spatrick bool for_expressions) {
416061da546Spatrick // One complication here is that most users DON'T want to stop at
417061da546Spatrick // __cxa_allocate_expression, but until we can do anything better with
418061da546Spatrick // predicting unwinding the expression parser does. So we have two forms of
419061da546Spatrick // the exception breakpoints, one for expressions that leaves out
420061da546Spatrick // __cxa_allocate_exception, and one that includes it. The
421061da546Spatrick // SetExceptionBreakpoints does the latter, the CreateExceptionBreakpoint in
422061da546Spatrick // the runtime the former.
423061da546Spatrick static const char *g_catch_name = "__cxa_begin_catch";
424061da546Spatrick static const char *g_throw_name1 = "__cxa_throw";
425061da546Spatrick static const char *g_throw_name2 = "__cxa_rethrow";
426061da546Spatrick static const char *g_exception_throw_name = "__cxa_allocate_exception";
427061da546Spatrick std::vector<const char *> exception_names;
428061da546Spatrick exception_names.reserve(4);
429061da546Spatrick if (catch_bp)
430061da546Spatrick exception_names.push_back(g_catch_name);
431061da546Spatrick
432061da546Spatrick if (throw_bp) {
433061da546Spatrick exception_names.push_back(g_throw_name1);
434061da546Spatrick exception_names.push_back(g_throw_name2);
435061da546Spatrick }
436061da546Spatrick
437061da546Spatrick if (for_expressions)
438061da546Spatrick exception_names.push_back(g_exception_throw_name);
439061da546Spatrick
440061da546Spatrick BreakpointResolverSP resolver_sp(new BreakpointResolverName(
441061da546Spatrick bkpt, exception_names.data(), exception_names.size(),
442061da546Spatrick eFunctionNameTypeBase, eLanguageTypeUnknown, 0, eLazyBoolNo));
443061da546Spatrick
444061da546Spatrick return resolver_sp;
445061da546Spatrick }
446061da546Spatrick
CreateExceptionSearchFilter()447061da546Spatrick lldb::SearchFilterSP ItaniumABILanguageRuntime::CreateExceptionSearchFilter() {
448061da546Spatrick Target &target = m_process->GetTarget();
449061da546Spatrick
450061da546Spatrick FileSpecList filter_modules;
451061da546Spatrick if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple) {
452061da546Spatrick // Limit the number of modules that are searched for these breakpoints for
453061da546Spatrick // Apple binaries.
454061da546Spatrick filter_modules.EmplaceBack("libc++abi.dylib");
455061da546Spatrick filter_modules.EmplaceBack("libSystem.B.dylib");
456*f6aab3d8Srobert filter_modules.EmplaceBack("libc++abi.1.0.dylib");
457*f6aab3d8Srobert filter_modules.EmplaceBack("libc++abi.1.dylib");
458061da546Spatrick }
459061da546Spatrick return target.GetSearchFilterForModuleList(&filter_modules);
460061da546Spatrick }
461061da546Spatrick
CreateExceptionBreakpoint(bool catch_bp,bool throw_bp,bool for_expressions,bool is_internal)462061da546Spatrick lldb::BreakpointSP ItaniumABILanguageRuntime::CreateExceptionBreakpoint(
463061da546Spatrick bool catch_bp, bool throw_bp, bool for_expressions, bool is_internal) {
464061da546Spatrick Target &target = m_process->GetTarget();
465061da546Spatrick FileSpecList filter_modules;
466061da546Spatrick BreakpointResolverSP exception_resolver_sp =
467061da546Spatrick CreateExceptionResolver(nullptr, catch_bp, throw_bp, for_expressions);
468061da546Spatrick SearchFilterSP filter_sp(CreateExceptionSearchFilter());
469061da546Spatrick const bool hardware = false;
470061da546Spatrick const bool resolve_indirect_functions = false;
471061da546Spatrick return target.CreateBreakpoint(filter_sp, exception_resolver_sp, is_internal,
472061da546Spatrick hardware, resolve_indirect_functions);
473061da546Spatrick }
474061da546Spatrick
SetExceptionBreakpoints()475061da546Spatrick void ItaniumABILanguageRuntime::SetExceptionBreakpoints() {
476061da546Spatrick if (!m_process)
477061da546Spatrick return;
478061da546Spatrick
479061da546Spatrick const bool catch_bp = false;
480061da546Spatrick const bool throw_bp = true;
481061da546Spatrick const bool is_internal = true;
482061da546Spatrick const bool for_expressions = true;
483061da546Spatrick
484061da546Spatrick // For the exception breakpoints set by the Expression parser, we'll be a
485061da546Spatrick // little more aggressive and stop at exception allocation as well.
486061da546Spatrick
487061da546Spatrick if (m_cxx_exception_bp_sp) {
488061da546Spatrick m_cxx_exception_bp_sp->SetEnabled(true);
489061da546Spatrick } else {
490061da546Spatrick m_cxx_exception_bp_sp = CreateExceptionBreakpoint(
491061da546Spatrick catch_bp, throw_bp, for_expressions, is_internal);
492061da546Spatrick if (m_cxx_exception_bp_sp)
493061da546Spatrick m_cxx_exception_bp_sp->SetBreakpointKind("c++ exception");
494061da546Spatrick }
495061da546Spatrick }
496061da546Spatrick
ClearExceptionBreakpoints()497061da546Spatrick void ItaniumABILanguageRuntime::ClearExceptionBreakpoints() {
498061da546Spatrick if (!m_process)
499061da546Spatrick return;
500061da546Spatrick
501061da546Spatrick if (m_cxx_exception_bp_sp) {
502061da546Spatrick m_cxx_exception_bp_sp->SetEnabled(false);
503061da546Spatrick }
504061da546Spatrick }
505061da546Spatrick
ExceptionBreakpointsAreSet()506061da546Spatrick bool ItaniumABILanguageRuntime::ExceptionBreakpointsAreSet() {
507061da546Spatrick return m_cxx_exception_bp_sp && m_cxx_exception_bp_sp->IsEnabled();
508061da546Spatrick }
509061da546Spatrick
ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason)510061da546Spatrick bool ItaniumABILanguageRuntime::ExceptionBreakpointsExplainStop(
511061da546Spatrick lldb::StopInfoSP stop_reason) {
512061da546Spatrick if (!m_process)
513061da546Spatrick return false;
514061da546Spatrick
515061da546Spatrick if (!stop_reason || stop_reason->GetStopReason() != eStopReasonBreakpoint)
516061da546Spatrick return false;
517061da546Spatrick
518061da546Spatrick uint64_t break_site_id = stop_reason->GetValue();
519061da546Spatrick return m_process->GetBreakpointSiteList().BreakpointSiteContainsBreakpoint(
520061da546Spatrick break_site_id, m_cxx_exception_bp_sp->GetID());
521061da546Spatrick }
522061da546Spatrick
GetExceptionObjectForThread(ThreadSP thread_sp)523061da546Spatrick ValueObjectSP ItaniumABILanguageRuntime::GetExceptionObjectForThread(
524061da546Spatrick ThreadSP thread_sp) {
525061da546Spatrick if (!thread_sp->SafeToCallFunctions())
526061da546Spatrick return {};
527061da546Spatrick
528*f6aab3d8Srobert TypeSystemClangSP scratch_ts_sp =
529be691f3bSpatrick ScratchTypeSystemClang::GetForTarget(m_process->GetTarget());
530*f6aab3d8Srobert if (!scratch_ts_sp)
531061da546Spatrick return {};
532061da546Spatrick
533061da546Spatrick CompilerType voidstar =
534*f6aab3d8Srobert scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
535061da546Spatrick
536061da546Spatrick DiagnosticManager diagnostics;
537061da546Spatrick ExecutionContext exe_ctx;
538061da546Spatrick EvaluateExpressionOptions options;
539061da546Spatrick
540061da546Spatrick options.SetUnwindOnError(true);
541061da546Spatrick options.SetIgnoreBreakpoints(true);
542061da546Spatrick options.SetStopOthers(true);
543061da546Spatrick options.SetTimeout(m_process->GetUtilityExpressionTimeout());
544061da546Spatrick options.SetTryAllThreads(false);
545061da546Spatrick thread_sp->CalculateExecutionContext(exe_ctx);
546061da546Spatrick
547061da546Spatrick const ModuleList &modules = m_process->GetTarget().GetImages();
548061da546Spatrick SymbolContextList contexts;
549061da546Spatrick SymbolContext context;
550061da546Spatrick
551061da546Spatrick modules.FindSymbolsWithNameAndType(
552061da546Spatrick ConstString("__cxa_current_exception_type"), eSymbolTypeCode, contexts);
553061da546Spatrick contexts.GetContextAtIndex(0, context);
554061da546Spatrick if (!context.symbol) {
555061da546Spatrick return {};
556061da546Spatrick }
557061da546Spatrick Address addr = context.symbol->GetAddress();
558061da546Spatrick
559061da546Spatrick Status error;
560061da546Spatrick FunctionCaller *function_caller =
561061da546Spatrick m_process->GetTarget().GetFunctionCallerForLanguage(
562061da546Spatrick eLanguageTypeC, voidstar, addr, ValueList(), "caller", error);
563061da546Spatrick
564061da546Spatrick ExpressionResults func_call_ret;
565061da546Spatrick Value results;
566061da546Spatrick func_call_ret = function_caller->ExecuteFunction(exe_ctx, nullptr, options,
567061da546Spatrick diagnostics, results);
568061da546Spatrick if (func_call_ret != eExpressionCompleted || !error.Success()) {
569061da546Spatrick return ValueObjectSP();
570061da546Spatrick }
571061da546Spatrick
572061da546Spatrick size_t ptr_size = m_process->GetAddressByteSize();
573061da546Spatrick addr_t result_ptr = results.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
574061da546Spatrick addr_t exception_addr =
575061da546Spatrick m_process->ReadPointerFromMemory(result_ptr - ptr_size, error);
576061da546Spatrick
577061da546Spatrick if (!error.Success()) {
578061da546Spatrick return ValueObjectSP();
579061da546Spatrick }
580061da546Spatrick
581061da546Spatrick lldb_private::formatters::InferiorSizedWord exception_isw(exception_addr,
582061da546Spatrick *m_process);
583061da546Spatrick ValueObjectSP exception = ValueObject::CreateValueObjectFromData(
584061da546Spatrick "exception", exception_isw.GetAsData(m_process->GetByteOrder()), exe_ctx,
585061da546Spatrick voidstar);
586061da546Spatrick exception = exception->GetDynamicValue(eDynamicDontRunTarget);
587061da546Spatrick
588061da546Spatrick return exception;
589061da546Spatrick }
590061da546Spatrick
GetDynamicTypeInfo(const lldb_private::Address & vtable_addr)591061da546Spatrick TypeAndOrName ItaniumABILanguageRuntime::GetDynamicTypeInfo(
592061da546Spatrick const lldb_private::Address &vtable_addr) {
593061da546Spatrick std::lock_guard<std::mutex> locker(m_dynamic_type_map_mutex);
594061da546Spatrick DynamicTypeCache::const_iterator pos = m_dynamic_type_map.find(vtable_addr);
595061da546Spatrick if (pos == m_dynamic_type_map.end())
596061da546Spatrick return TypeAndOrName();
597061da546Spatrick else
598061da546Spatrick return pos->second;
599061da546Spatrick }
600061da546Spatrick
SetDynamicTypeInfo(const lldb_private::Address & vtable_addr,const TypeAndOrName & type_info)601061da546Spatrick void ItaniumABILanguageRuntime::SetDynamicTypeInfo(
602061da546Spatrick const lldb_private::Address &vtable_addr, const TypeAndOrName &type_info) {
603061da546Spatrick std::lock_guard<std::mutex> locker(m_dynamic_type_map_mutex);
604061da546Spatrick m_dynamic_type_map[vtable_addr] = type_info;
605061da546Spatrick }
606