LLDB mainline
CPPLanguageRuntime.cpp
Go to the documentation of this file.
1//===-- CPPLanguageRuntime.cpp---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <cstring>
10#include <iostream>
11
12#include <memory>
13
14#include "CPPLanguageRuntime.h"
17
18#include "llvm/ADT/StringRef.h"
19
20#include "lldb/Symbol/Block.h"
23
27#include "lldb/Target/ABI.h"
35#include "lldb/Utility/Timer.h"
36
37using namespace lldb;
38using namespace lldb_private;
39
41
43// Artificial coroutine-related variables emitted by clang.
44static ConstString g_promise = ConstString("__promise");
45static ConstString g_coro_frame = ConstString("__coro_frame");
46
47char CPPLanguageRuntime::ID = 0;
48
49/// A frame recognizer that is installed to hide libc++ implementation
50/// details from the backtrace.
52 std::array<RegularExpression, 2> m_hidden_regex;
54
56 bool ShouldHide() override { return true; }
57 };
58
59public:
62 // internal implementation details in the `std::` namespace
63 // std::__1::__function::__alloc_func<void (*)(), std::__1::allocator<void (*)()>, void ()>::operator()[abi:ne200000]
64 // std::__1::__function::__func<void (*)(), std::__1::allocator<void (*)()>, void ()>::operator()
65 // std::__1::__function::__value_func<void ()>::operator()[abi:ne200000]() const
66 // std::__2::__function::__policy_invoker<void (int, int)>::__call_impl[abi:ne200000]<std::__2::__function::__default_alloc_func<int (*)(int, int), int (int, int)>>
67 // std::__1::__invoke[abi:ne200000]<void (*&)()>
68 // std::__1::__invoke_void_return_wrapper<void, true>::__call[abi:ne200000]<void (*&)()>
69 RegularExpression{R"(^std::__[^:]*::__)"},
70 // internal implementation details in the `std::ranges` namespace
71 // std::__1::ranges::__sort::__sort_fn_impl[abi:ne200000]<std::__1::__wrap_iter<int*>, std::__1::__wrap_iter<int*>, bool (*)(int, int), std::__1::identity>
72 RegularExpression{R"(^std::__[^:]*::ranges::__)"},
73 },
75
76 std::string GetName() override { return "libc++ frame recognizer"; }
77
80 if (!frame_sp)
81 return {};
82 const auto &sc = frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
83 if (!sc.function)
84 return {};
85
86 // Check if we have a regex match
88 if (!r.Execute(sc.function->GetNameNoArguments()))
89 continue;
90
91 // Only hide this frame if the immediate caller is also within libc++.
92 lldb::ThreadSP thread_sp = frame_sp->GetThread();
93 if (!thread_sp)
94 return {};
95 lldb::StackFrameSP parent_frame_sp =
96 thread_sp->GetStackFrameAtIndex(frame_sp->GetFrameIndex() + 1);
97 if (!parent_frame_sp)
98 return {};
99 const auto &parent_sc =
100 parent_frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
101 if (!parent_sc.function)
102 return {};
103 if (parent_sc.function->GetNameNoArguments().GetStringRef().starts_with(
104 "std::"))
105 return m_hidden_frame;
106 }
107
108 return {};
109 }
110};
111
113 : LanguageRuntime(process), m_itanium_runtime(process) {
114 if (process) {
117 std::make_shared<RegularExpression>("^std::__[^:]*::"),
118 /*mangling_preference=*/Mangled::ePreferDemangledWithoutArguments,
119 /*first_instruction_only=*/false);
120
122 }
123}
124
126 return name == g_this || name == g_promise || name == g_coro_frame;
127}
128
130 ValueObject &object) {
131 // C++ has no generic way to do this.
132 return llvm::createStringError("C++ does not support object descriptions");
133}
134
135llvm::Error
137 ExecutionContextScope *exe_scope) {
138 // C++ has no generic way to do this.
139 return llvm::createStringError("C++ does not support object descriptions");
140}
141
142bool contains_lambda_identifier(llvm::StringRef &str_ref) {
143 return str_ref.contains("$_") || str_ref.contains("'lambda'");
144}
145
147line_entry_helper(Target &target, const SymbolContext &sc, Symbol *symbol,
148 llvm::StringRef first_template_param_sref, bool has_invoke) {
149
151
152 Address address = sc.GetFunctionOrSymbolAddress();
153
154 Address addr;
155 if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),
156 addr)) {
157 LineEntry line_entry;
158 addr.CalculateSymbolContextLineEntry(line_entry);
159
160 if (contains_lambda_identifier(first_template_param_sref) || has_invoke) {
161 // Case 1 and 2
162 optional_info.callable_case = lldb_private::CPPLanguageRuntime::
163 LibCppStdFunctionCallableCase::Lambda;
164 } else {
165 // Case 3
166 optional_info.callable_case = lldb_private::CPPLanguageRuntime::
167 LibCppStdFunctionCallableCase::CallableObject;
168 }
169
170 optional_info.callable_symbol = *symbol;
171 optional_info.callable_line_entry = line_entry;
172 optional_info.callable_address = addr;
173 }
174
175 return optional_info;
176}
177
180 lldb::ValueObjectSP &valobj_sp) {
182
183 LibCppStdFunctionCallableInfo optional_info;
184
185 if (!valobj_sp)
186 return optional_info;
187
188 // Member __f_ has type __base*, the contents of which will hold:
189 // 1) a vtable entry which may hold type information needed to discover the
190 // lambda being called
191 // 2) possibly hold a pointer to the callable object
192 // e.g.
193 //
194 // (lldb) frame var -R f_display
195 // (std::__1::function<void (int)>) f_display = {
196 // __buf_ = {
197 // …
198 // }
199 // __f_ = 0x00007ffeefbffa00
200 // }
201 // (lldb) memory read -fA 0x00007ffeefbffa00
202 // 0x7ffeefbffa00: ... `vtable for std::__1::__function::__func<void (*) ...
203 // 0x7ffeefbffa08: ... `print_num(int) at std_function_cppreference_exam ...
204 //
205 // We will be handling five cases below, std::function is wrapping:
206 //
207 // 1) a lambda we know at compile time. We will obtain the name of the lambda
208 // from the first template pameter from __func's vtable. We will look up
209 // the lambda's operator()() and obtain the line table entry.
210 // 2) a lambda we know at runtime. A pointer to the lambdas __invoke method
211 // will be stored after the vtable. We will obtain the lambdas name from
212 // this entry and lookup operator()() and obtain the line table entry.
213 // 3) a callable object via operator()(). We will obtain the name of the
214 // object from the first template parameter from __func's vtable. We will
215 // look up the objects operator()() and obtain the line table entry.
216 // 4) a member function. A pointer to the function will stored after the
217 // we will obtain the name from this pointer.
218 // 5) a free function. A pointer to the function will stored after the vtable
219 // we will obtain the name from this pointer.
220 ValueObjectSP member_f_(valobj_sp->GetChildMemberWithName("__f_"));
221
222 if (member_f_) {
223 ValueObjectSP sub_member_f_(member_f_->GetChildMemberWithName("__f_"));
224
225 if (sub_member_f_)
226 member_f_ = sub_member_f_;
227 }
228
229 if (!member_f_)
230 return optional_info;
231
232 lldb::addr_t member_f_pointer_value = member_f_->GetValueAsUnsigned(0);
233
234 optional_info.member_f_pointer_value = member_f_pointer_value;
235
236 if (!member_f_pointer_value)
237 return optional_info;
238
239 ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef());
240 Process *process = exe_ctx.GetProcessPtr();
241
242 if (process == nullptr)
243 return optional_info;
244
245 uint32_t address_size = process->GetAddressByteSize();
246 Status status;
247
248 // First item pointed to by __f_ should be the pointer to the vtable for
249 // a __base object.
250 lldb::addr_t vtable_address =
251 process->ReadPointerFromMemory(member_f_pointer_value, status);
252
253 ABISP abi_sp = process->GetABI();
254 if (abi_sp)
255 vtable_address = abi_sp->FixCodeAddress(vtable_address);
256
257 if (status.Fail())
258 return optional_info;
259
260 lldb::addr_t vtable_address_first_entry =
261 process->ReadPointerFromMemory(vtable_address + address_size, status);
262
263 if (abi_sp)
264 vtable_address_first_entry =
265 abi_sp->FixCodeAddress(vtable_address_first_entry);
266
267 if (status.Fail())
268 return optional_info;
269
270 lldb::addr_t address_after_vtable = member_f_pointer_value + address_size;
271 // As commented above we may not have a function pointer but if we do we will
272 // need it.
273 lldb::addr_t possible_function_address =
274 process->ReadPointerFromMemory(address_after_vtable, status);
275
276 if (abi_sp)
277 possible_function_address =
278 abi_sp->FixCodeAddress(possible_function_address);
279
280 if (status.Fail())
281 return optional_info;
282
283 Target &target = process->GetTarget();
284
285 if (!target.HasLoadedSections())
286 return optional_info;
287
288 Address vtable_first_entry_resolved;
289
290 if (!target.ResolveLoadAddress(vtable_address_first_entry,
291 vtable_first_entry_resolved))
292 return optional_info;
293
294 Address vtable_addr_resolved;
295 SymbolContext sc;
296 Symbol *symbol = nullptr;
297
298 if (!target.ResolveLoadAddress(vtable_address, vtable_addr_resolved))
299 return optional_info;
300
302 vtable_addr_resolved, eSymbolContextEverything, sc);
303 symbol = sc.symbol;
304
305 if (symbol == nullptr)
306 return optional_info;
307
308 llvm::StringRef vtable_name(symbol->GetName().GetStringRef());
309 bool found_expected_start_string =
310 vtable_name.starts_with("vtable for std::__1::__function::__func<");
311
312 if (!found_expected_start_string)
313 return optional_info;
314
315 // Given case 1 or 3 we have a vtable name, we are want to extract the first
316 // template parameter
317 //
318 // ... __func<main::$_0, std::__1::allocator<main::$_0> ...
319 // ^^^^^^^^^
320 //
321 // We could see names such as:
322 // main::$_0
323 // Bar::add_num2(int)::'lambda'(int)
324 // Bar
325 //
326 // We do this by find the first < and , and extracting in between.
327 //
328 // This covers the case of the lambda known at compile time.
329 size_t first_open_angle_bracket = vtable_name.find('<') + 1;
330 size_t first_comma = vtable_name.find(',');
331
332 llvm::StringRef first_template_parameter =
333 vtable_name.slice(first_open_angle_bracket, first_comma);
334
335 Address function_address_resolved;
336
337 // Setup for cases 2, 4 and 5 we have a pointer to a function after the
338 // vtable. We will use a process of elimination to drop through each case
339 // and obtain the data we need.
340 if (target.ResolveLoadAddress(possible_function_address,
341 function_address_resolved)) {
343 function_address_resolved, eSymbolContextEverything, sc);
344 symbol = sc.symbol;
345 }
346
347 // These conditions are used several times to simplify statements later on.
348 bool has_invoke =
349 (symbol ? symbol->GetName().GetStringRef().contains("__invoke") : false);
350 auto calculate_symbol_context_helper = [](auto &t,
351 SymbolContextList &sc_list) {
352 SymbolContext sc;
353 t->CalculateSymbolContext(&sc);
354 sc_list.Append(sc);
355 };
356
357 // Case 2
358 if (has_invoke) {
360 calculate_symbol_context_helper(symbol, scl);
361
362 return line_entry_helper(target, scl[0], symbol, first_template_parameter,
363 has_invoke);
364 }
365
366 // Case 4 or 5
367 if (symbol && !symbol->GetName().GetStringRef().starts_with("vtable for") &&
368 !contains_lambda_identifier(first_template_parameter) && !has_invoke) {
369 optional_info.callable_case =
371 optional_info.callable_address = function_address_resolved;
372 optional_info.callable_symbol = *symbol;
373
374 return optional_info;
375 }
376
377 std::string func_to_match = first_template_parameter.str();
378
379 auto it = CallableLookupCache.find(func_to_match);
380 if (it != CallableLookupCache.end())
381 return it->second;
382
384
385 CompileUnit *vtable_cu =
386 vtable_first_entry_resolved.CalculateSymbolContextCompileUnit();
387 llvm::StringRef name_to_use = func_to_match;
388
389 // Case 3, we have a callable object instead of a lambda
390 //
391 // TODO
392 // We currently don't support this case a callable object may have multiple
393 // operator()() varying on const/non-const and number of arguments and we
394 // don't have a way to currently distinguish them so we will bail out now.
395 if (!contains_lambda_identifier(name_to_use))
396 return optional_info;
397
398 if (vtable_cu && !has_invoke) {
399 lldb::FunctionSP func_sp =
400 vtable_cu->FindFunction([name_to_use](const FunctionSP &f) {
401 auto name = f->GetName().GetStringRef();
402 if (name.starts_with(name_to_use) && name.contains("operator"))
403 return true;
404
405 return false;
406 });
407
408 if (func_sp) {
409 calculate_symbol_context_helper(func_sp, scl);
410 }
411 }
412
413 if (symbol == nullptr)
414 return optional_info;
415
416 // Case 1 or 3
417 if (scl.GetSize() >= 1) {
418 optional_info = line_entry_helper(target, scl[0], symbol,
419 first_template_parameter, has_invoke);
420 }
421
422 CallableLookupCache[func_to_match] = optional_info;
423
424 return optional_info;
425}
426
429 bool stop_others) {
430 ThreadPlanSP ret_plan_sp;
431
432 lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC();
433
434 TargetSP target_sp(thread.CalculateTarget());
435
436 if (!target_sp->HasLoadedSections())
437 return ret_plan_sp;
438
439 Address pc_addr_resolved;
440 SymbolContext sc;
441 Symbol *symbol;
442
443 if (!target_sp->ResolveLoadAddress(curr_pc, pc_addr_resolved))
444 return ret_plan_sp;
445
446 target_sp->GetImages().ResolveSymbolContextForAddress(
447 pc_addr_resolved, eSymbolContextEverything, sc);
448 symbol = sc.symbol;
449
450 if (symbol == nullptr)
451 return ret_plan_sp;
452
453 llvm::StringRef function_name(symbol->GetName().GetCString());
454
455 // Handling the case where we are attempting to step into std::function.
456 // The behavior will be that we will attempt to obtain the wrapped
457 // callable via FindLibCppStdFunctionCallableInfo() and if we find it we
458 // will return a ThreadPlanRunToAddress to the callable. Therefore we will
459 // step into the wrapped callable.
460 //
461 bool found_expected_start_string =
462 function_name.starts_with("std::__1::function<");
463
464 if (!found_expected_start_string)
465 return ret_plan_sp;
466
467 AddressRange range_of_curr_func;
468 sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func);
469
470 StackFrameSP frame = thread.GetStackFrameAtIndex(0);
471
472 if (frame) {
473 Address func_start_address =
474 sc.function ? sc.function->GetAddress() : symbol->GetAddress();
475 lldb::addr_t func_start =
476 func_start_address.GetLoadAddress(target_sp.get());
477
478 if (func_start == LLDB_INVALID_ADDRESS)
479 return ret_plan_sp;
480
481 // Advance past the prologue if we stopped there.
482 uint32_t prologue_size = sc.function ? sc.function->GetPrologueByteSize()
483 : symbol->GetPrologueByteSize();
484 if (curr_pc < func_start + prologue_size) {
485 func_start_address.Slide(prologue_size);
486 return std::make_shared<ThreadPlanRunToAddress>(
487 thread, func_start_address, stop_others);
488 }
489
490 ValueObjectSP value_sp = frame->FindVariable(g_this);
491
494
496 value_sp->GetValueIsValid()) {
497 // We found the std::function wrapped callable and we have its address.
498 // We now create a ThreadPlan to run to the callable.
499 ret_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
500 thread, callable_info.callable_address, stop_others);
501 return ret_plan_sp;
502 } else {
503 // We are in std::function but we could not obtain the callable.
504 // We create a ThreadPlan to keep stepping through using the address range
505 // of the current function.
506 ret_plan_sp = std::make_shared<ThreadPlanStepInRange>(
507 thread, range_of_curr_func, sc, nullptr, eOnlyThisThread,
509 return ret_plan_sp;
510 }
511 }
512
513 return ret_plan_sp;
514}
515
517 llvm::StringRef mangled_name =
519 // Virtual function overriding from a non-virtual base use a "Th" prefix.
520 // Virtual function overriding from a virtual base must use a "Tv" prefix.
521 // Virtual function overriding thunks with covariant returns use a "Tc"
522 // prefix.
523 return mangled_name.starts_with("_ZTh") || mangled_name.starts_with("_ZTv") ||
524 mangled_name.starts_with("_ZTc");
525}
526
528 const bool check_cxx = true;
529 const bool check_objc = false;
530 return in_value.GetCompilerType().IsPossibleDynamicType(nullptr, check_cxx,
531 check_objc);
532}
533
535 ValueObject &in_value, lldb::DynamicValueType use_dynamic,
536 TypeAndOrName &class_type_or_name, Address &dynamic_address,
537 Value::ValueType &value_type, llvm::ArrayRef<uint8_t> &local_buffer) {
538 class_type_or_name.Clear();
539 value_type = Value::ValueType::Scalar;
540
541 if (!CouldHaveDynamicValue(in_value))
542 return false;
543
544 llvm::Expected<VTableInfoEntry> entry =
545 GetVTableInfoEntry(in_value, /*check_type=*/false);
546 if (!entry) {
547 llvm::consumeError(entry.takeError());
548 return false;
549 }
550
551 return m_itanium_runtime.GetDynamicTypeAndAddress(
552 in_value, use_dynamic, entry->info, class_type_or_name, dynamic_address,
553 value_type);
554}
555
558 ValueObject &static_value) {
559 CompilerType static_type(static_value.GetCompilerType());
560 Flags static_type_flags(static_type.GetTypeInfo());
561
562 TypeAndOrName ret(type_and_or_name);
563 if (type_and_or_name.HasType()) {
564 // The type will always be the type of the dynamic object. If our parent's
565 // type was a pointer, then our type should be a pointer to the type of the
566 // dynamic object. If a reference, then the original type should be
567 // okay...
568 CompilerType orig_type = type_and_or_name.GetCompilerType();
569 CompilerType corrected_type = orig_type;
570 if (static_type_flags.AllSet(eTypeIsPointer))
571 corrected_type = orig_type.GetPointerType();
572 else if (static_type_flags.AllSet(eTypeIsReference))
573 corrected_type = orig_type.GetLValueReferenceType();
574 ret.SetCompilerType(corrected_type);
575 } else {
576 // If we are here we need to adjust our dynamic type name to include the
577 // correct & or * symbol
578 std::string corrected_name(type_and_or_name.GetName().GetCString());
579 if (static_type_flags.AllSet(eTypeIsPointer))
580 corrected_name.append(" *");
581 else if (static_type_flags.AllSet(eTypeIsReference))
582 corrected_name.append(" &");
583 // the parent type should be a correctly pointer'ed or referenc'ed type
584 ret.SetCompilerType(static_type);
585 ret.SetName(corrected_name.c_str());
586 }
587 return ret;
588}
589
592 lldb::LanguageType language) {
593 if (language == eLanguageTypeC_plus_plus ||
594 language == eLanguageTypeC_plus_plus_03 ||
595 language == eLanguageTypeC_plus_plus_11 ||
596 language == eLanguageTypeC_plus_plus_14)
597 return new CPPLanguageRuntime(process);
598 else
599 return nullptr;
600}
601
604 GetPluginNameStatic(), "C++ language runtime", CreateInstance,
605 [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
606 return CommandObjectSP(new CommandObjectCPlusPlus(interpreter));
607 });
608}
609
613
614llvm::Expected<LanguageRuntime::VTableInfo>
616 llvm::Expected<VTableInfoEntry> entry =
617 GetVTableInfoEntry(in_value, check_type);
618 if (!entry)
619 return entry.takeError();
620 return entry->info;
621}
622
625 bool catch_bp, bool throw_bp) {
626 return CreateExceptionResolver(bkpt, catch_bp, throw_bp, false);
627}
628
631 bool catch_bp, bool throw_bp,
632 bool for_expressions) {
633 std::vector<const char *> exception_names;
634 m_itanium_runtime.AppendExceptionBreakpointFunctions(
635 exception_names, catch_bp, throw_bp, for_expressions);
636
638 bkpt, exception_names.data(), exception_names.size(),
639 eFunctionNameTypeBase, eLanguageTypeUnknown, 0, eLazyBoolNo));
640
641 return resolver_sp;
642}
643
645 Target &target = m_process->GetTarget();
646
647 FileSpecList filter_modules;
648 m_itanium_runtime.AppendExceptionBreakpointFilterModules(filter_modules,
649 target);
650 return target.GetSearchFilterForModuleList(&filter_modules);
651}
652
654 bool catch_bp, bool throw_bp, bool for_expressions, bool is_internal) {
655 Target &target = m_process->GetTarget();
656 FileSpecList filter_modules;
657 BreakpointResolverSP exception_resolver_sp =
658 CreateExceptionResolver(nullptr, catch_bp, throw_bp, for_expressions);
660 const bool hardware = false;
661 const bool resolve_indirect_functions = false;
662 return target.CreateBreakpoint(filter_sp, exception_resolver_sp, is_internal,
663 hardware, resolve_indirect_functions);
664}
665
667 if (!m_process)
668 return;
669
670 const bool catch_bp = false;
671 const bool throw_bp = true;
672 const bool is_internal = true;
673 const bool for_expressions = true;
674
675 // For the exception breakpoints set by the Expression parser, we'll be a
676 // little more aggressive and stop at exception allocation as well.
677
679 m_cxx_exception_bp_sp->SetEnabled(true);
680 } else {
682 catch_bp, throw_bp, for_expressions, is_internal);
684 m_cxx_exception_bp_sp->SetBreakpointKind("c++ exception");
685 }
686}
687
689 if (!m_process)
690 return;
691
693 m_cxx_exception_bp_sp->SetEnabled(false);
694 }
695}
696
700
702 lldb::StopInfoSP stop_reason) {
703 if (!m_process)
704 return false;
705
706 if (!stop_reason || stop_reason->GetStopReason() != eStopReasonBreakpoint)
707 return false;
708
709 uint64_t break_site_id = stop_reason->GetValue();
710 return m_process->GetBreakpointSiteList().StopPointSiteContainsBreakpoint(
711 break_site_id, m_cxx_exception_bp_sp->GetID());
712}
713
716 return m_itanium_runtime.GetExceptionObjectForThread(std::move(thread_sp));
717}
718
719static llvm::Error TypeHasVTable(CompilerType type) {
720 // Check to make sure the class has a vtable.
721 CompilerType original_type = type;
722 if (type.IsPointerOrReferenceType()) {
723 CompilerType pointee_type = type.GetPointeeType();
724 if (pointee_type)
725 type = pointee_type;
726 }
727
728 // Make sure this is a class or a struct first by checking the type class
729 // bitfield that gets returned.
730 if ((type.GetTypeClass() & (eTypeClassStruct | eTypeClassClass)) == 0) {
731 return llvm::createStringError(
732 std::errc::invalid_argument,
733 "type \"%s\" is not a class or struct or a pointer to one",
734 original_type.GetTypeName().AsCString("<invalid>"));
735 }
736
737 // Check if the type has virtual functions by asking it if it is polymorphic.
738 if (!type.IsPolymorphicClass()) {
739 return llvm::createStringError(std::errc::invalid_argument,
740 "type \"%s\" doesn't have a vtable",
741 type.GetTypeName().AsCString("<invalid>"));
742 }
743 return llvm::Error::success();
744}
745
746// This function can accept both pointers or references to classes as well as
747// instances of classes. If you are using this function during dynamic type
748// detection, only valid ValueObjects that return true to
749// CouldHaveDynamicValue(...) should call this function and \a check_type
750// should be set to false. This function is also used by ValueObjectVTable
751// and is can pass in instances of classes which is not suitable for dynamic
752// type detection, these cases should pass true for \a check_type.
753llvm::Expected<CPPLanguageRuntime::VTableInfoEntry>
755
756 CompilerType type = in_value.GetCompilerType();
757 if (check_type) {
758 if (llvm::Error err = TypeHasVTable(type))
759 return std::move(err);
760 }
761 ExecutionContext exe_ctx(in_value.GetExecutionContextRef());
762 Process *process = exe_ctx.GetProcessPtr();
763 if (process == nullptr)
764 return llvm::createStringError(std::errc::invalid_argument,
765 "invalid process");
766
767 auto [original_ptr, address_type] =
769 ? in_value.GetPointerValue()
770 : in_value.GetAddressOf(/*scalar_is_load_address=*/true);
771 if (original_ptr == LLDB_INVALID_ADDRESS || address_type != eAddressTypeLoad)
772 return llvm::createStringError(std::errc::invalid_argument,
773 "failed to get the address of the value");
774
776 lldb::addr_t vtable_load_addr =
777 process->ReadPointerFromMemory(original_ptr, error);
778
779 if (!error.Success() || vtable_load_addr == LLDB_INVALID_ADDRESS)
780 return llvm::createStringError(
781 std::errc::invalid_argument,
782 "failed to read vtable pointer from memory at 0x%" PRIx64,
783 original_ptr);
784
785 // The vtable load address can have authentication bits with
786 // AArch64 targets on Darwin.
787 vtable_load_addr = process->FixDataAddress(vtable_load_addr);
788
789 // Find the symbol that contains the "vtable_load_addr" address
790 Address vtable_addr;
791 if (!process->GetTarget().ResolveLoadAddress(vtable_load_addr, vtable_addr))
792 return llvm::createStringError(std::errc::invalid_argument,
793 "failed to resolve vtable pointer 0x%" PRIx64
794 "to a section",
795 vtable_load_addr);
796
797 // Check our cache first to see if we already have this info
798 {
799 std::lock_guard<std::mutex> locker(m_vtable_mutex);
800 auto pos = m_vtable_info_map.find(vtable_addr);
801 if (pos != m_vtable_info_map.end())
802 return pos->second;
803 }
804
805 Symbol *symbol = vtable_addr.CalculateSymbolContextSymbol();
806 if (symbol == nullptr)
807 return llvm::createStringError(std::errc::invalid_argument,
808 "no symbol found for 0x%" PRIx64,
809 vtable_load_addr);
810 if (m_itanium_runtime.IsVTableSymbol(symbol->GetMangled())) {
811 VTableInfoEntry entry{
812 /*info=*/VTableInfo{vtable_addr, symbol},
813 };
814 std::lock_guard<std::mutex> locker(m_vtable_mutex);
815 m_vtable_info_map[vtable_addr] = entry;
816 return entry;
817 }
818 return llvm::createStringError(std::errc::invalid_argument,
819 "symbol found that contains 0x%" PRIx64
820 " is not a vtable symbol",
821 vtable_load_addr);
822}
static ConstString g_promise
static ConstString g_coro_frame
bool contains_lambda_identifier(llvm::StringRef &str_ref)
static ConstString g_this
static llvm::Error TypeHasVTable(CompilerType type)
CPPLanguageRuntime::LibCppStdFunctionCallableInfo line_entry_helper(Target &target, const SymbolContext &sc, Symbol *symbol, llvm::StringRef first_template_param_sref, bool has_invoke)
static llvm::raw_ostream & error(Stream &strm)
static char ID
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName)
#define LLDB_SCOPED_TIMER()
Definition Timer.h:83
A frame recognizer that is installed to hide libc++ implementation details from the backtrace.
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame_sp) override
RecognizedStackFrameSP m_hidden_frame
std::array< RegularExpression, 2 > m_hidden_regex
std::string GetName() override
A section + offset based address range class.
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
lldb::addr_t GetCallableLoadAddress(Target *target, bool is_indirect=false) const
Get the load address as a callable code load address.
Definition Address.cpp:326
bool Slide(int64_t offset)
Definition Address.h:446
bool CalculateSymbolContextLineEntry(LineEntry &line_entry) const
Definition Address.cpp:901
CompileUnit * CalculateSymbolContextCompileUnit() const
Definition Address.cpp:845
Symbol * CalculateSymbolContextSymbol() const
Definition Address.cpp:887
"lldb/Breakpoint/BreakpointResolverName.h" This class sets breakpoints on a given function name,...
llvm::Expected< LanguageRuntime::VTableInfo > GetVTableInfo(ValueObject &in_value, bool check_type) override
Get the vtable information for a given value.
std::map< Address, VTableInfoEntry > m_vtable_info_map
TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value) override
bool CouldHaveDynamicValue(ValueObject &in_value) override
lldb::BreakpointSP CreateExceptionBreakpoint(bool catch_bp, bool throw_bp, bool for_expressions, bool is_internal)
static lldb_private::LanguageRuntime * CreateInstance(Process *process, lldb::LanguageType language)
llvm::Error GetObjectDescription(Stream &str, ValueObject &object) override
lldb::SearchFilterSP CreateExceptionSearchFilter() override
lldb::ValueObjectSP GetExceptionObjectForThread(lldb::ThreadSP thread_sp) override
llvm::Expected< VTableInfoEntry > GetVTableInfoEntry(ValueObject &in_value, bool check_type)
bool IsSymbolARuntimeThunk(const Symbol &symbol) override
bool IsAllowedRuntimeValue(ConstString name) override
Identify whether a name is a runtime value that should not be hidden by from the user interface.
LibCppStdFunctionCallableInfo FindLibCppStdFunctionCallableInfo(lldb::ValueObjectSP &valobj_sp)
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread, bool stop_others) override
Obtain a ThreadPlan to get us into C++ constructs such as std::function.
lldb::BreakpointResolverSP CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bool catch_bp, bool throw_bp) override
OperatorStringToCallableInfoMap CallableLookupCache
static llvm::StringRef GetPluginNameStatic()
bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name, Address &address, Value::ValueType &value_type, llvm::ArrayRef< uint8_t > &local_buffer) override
This call should return true if it could set the name and/or the type Sets address to the address of ...
bool ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason) override
A class that describes a compilation unit.
Definition CompileUnit.h:43
lldb::FunctionSP FindFunction(llvm::function_ref< bool(const lldb::FunctionSP &)> matching_lambda)
Find a function in the compile unit based on the predicate matching_lambda.
Generic representation of a type in a programming language.
bool IsPossibleDynamicType(CompilerType *target_type, bool check_cplusplus, bool check_objc) const
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
lldb::TypeClass GetTypeClass() const
CompilerType GetLValueReferenceType() const
Return a new CompilerType that is a L value reference to this type if this type is valid and the type...
ConstString GetTypeName(bool BaseOnly=false) const
CompilerType GetPointeeType() const
If this type is a pointer type, return the type that the pointer points to, else return an invalid ty...
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
bool IsPointerOrReferenceType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Process * GetProcessPtr() const
Returns a pointer to the process object.
A file collection class.
A class to manage flags.
Definition Flags.h:22
bool AllSet(ValueType mask) const
Test if all bits in mask are 1 in the current flags.
Definition Flags.h:83
const Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:430
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition Function.cpp:594
@ ePreferDemangledWithoutArguments
Definition Mangled.h:39
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:174
uint32_t ResolveSymbolContextForAddress(const Address &so_addr, lldb::SymbolContextItem resolve_scope, SymbolContext &sc) const
Resolve the symbol context for the given address. (const Address&,uint32_t,SymbolContext&)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
Definition Process.h:359
lldb::addr_t FixDataAddress(lldb::addr_t pc)
Definition Process.cpp:6233
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2517
uint32_t GetAddressByteSize() const
Definition Process.cpp:3930
const lldb::ABISP & GetABI()
Definition Process.cpp:1492
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1258
This class provides extra information about a stack frame that was provided by a specific stack frame...
Process * m_process
Definition Runtime.h:29
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, Mangled::NamePreference symbol_mangling, bool first_instruction_only=true)
Add a new recognizer that triggers on a given symbol name.
A base class for frame recognizers.
An error handling class.
Definition Status.h:118
bool Fail() const
Test for error condition.
Definition Status.cpp:293
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Defines a list of symbol context objects.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Symbol * symbol
The Symbol for a given query.
Address GetFunctionOrSymbolAddress() const
Get the address of the function or symbol represented by this symbol context.
Mangled & GetMangled()
Definition Symbol.h:147
ConstString GetName() const
Definition Symbol.cpp:511
Address GetAddress() const
Definition Symbol.h:89
uint32_t GetPrologueByteSize()
Definition Symbol.cpp:313
lldb::SearchFilterSP GetSearchFilterForModuleList(const FileSpecList *containingModuleList)
Definition Target.cpp:705
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3484
StackFrameRecognizerManager & GetFrameRecognizerManager()
Definition Target.h:1999
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition Target.cpp:504
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1247
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
Definition Type.h:780
void SetName(ConstString type_name)
Definition Type.cpp:911
CompilerType GetCompilerType() const
Definition Type.h:794
ConstString GetName() const
Definition Type.cpp:903
void SetCompilerType(CompilerType compiler_type)
Definition Type.cpp:931
bool HasType() const
Definition Type.h:812
CompilerType GetCompilerType()
const ExecutionContextRef & GetExecutionContextRef() const
virtual AddrAndType GetAddressOf(bool scalar_is_load_address=true)
ValueType
Type that describes Value::m_value.
Definition Value.h:42
@ Scalar
A raw scalar value.
Definition Value.h:46
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
void RegisterVerboseTrapFrameRecognizer(Process &process)
@ eAddressTypeLoad
Address is an address as in the current target inferior process.
std::shared_ptr< lldb_private::Function > FunctionSP
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::SearchFilter > SearchFilterSP
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
LanguageType
Programming language type.
@ eLanguageTypeC_plus_plus_14
ISO C++:2014.
@ eLanguageTypeC_plus_plus_03
ISO C++:2003.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eLanguageTypeC_plus_plus_11
ISO C++:2011.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::StackFrameRecognizer > StackFrameRecognizerSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
@ eStopReasonBreakpoint
std::shared_ptr< lldb_private::Target > TargetSP
bool ShouldHide() override
Controls whether this frame should be filtered out when displaying backtraces, for example.
A line table entry class.
Definition LineEntry.h:21