1 //===-- CPPLanguageRuntime.cpp
2 //-------------------------------------------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "lldb/Target/CPPLanguageRuntime.h"
15 #include "llvm/ADT/StringRef.h"
17 #include "lldb/Symbol/Block.h"
18 #include "lldb/Symbol/VariableList.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/UniqueCStringMap.h"
22 #include "lldb/Symbol/ClangASTContext.h"
23 #include "lldb/Target/ABI.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/StackFrame.h"
28 #include "lldb/Target/ThreadPlanRunToAddress.h"
29 #include "lldb/Target/ThreadPlanStepInRange.h"
32 using namespace lldb_private;
34 //----------------------------------------------------------------------
36 //----------------------------------------------------------------------
37 CPPLanguageRuntime::~CPPLanguageRuntime() {}
39 CPPLanguageRuntime::CPPLanguageRuntime(Process *process)
40 : LanguageRuntime(process) {}
42 bool CPPLanguageRuntime::GetObjectDescription(Stream &str,
43 ValueObject &object) {
44 // C++ has no generic way to do this.
48 bool CPPLanguageRuntime::GetObjectDescription(
49 Stream &str, Value &value, ExecutionContextScope *exe_scope) {
50 // C++ has no generic way to do this.
54 CPPLanguageRuntime::LibCppStdFunctionCallableInfo
55 CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo(
56 lldb::ValueObjectSP &valobj_sp) {
57 LibCppStdFunctionCallableInfo optional_info;
62 // Member __f_ has type __base*, the contents of which will hold:
63 // 1) a vtable entry which may hold type information needed to discover the
64 // lambda being called
65 // 2) possibly hold a pointer to the callable object
68 // (lldb) frame var -R f_display
69 // (std::__1::function<void (int)>) f_display = {
73 // __f_ = 0x00007ffeefbffa00
75 // (lldb) memory read -fA 0x00007ffeefbffa00
76 // 0x7ffeefbffa00: ... `vtable for std::__1::__function::__func<void (*) ...
77 // 0x7ffeefbffa08: ... `print_num(int) at std_function_cppreference_exam ...
79 // We will be handling five cases below, std::function is wrapping:
81 // 1) a lambda we know at compile time. We will obtain the name of the lambda
82 // from the first template pameter from __func's vtable. We will look up
83 // the lambda's operator()() and obtain the line table entry.
84 // 2) a lambda we know at runtime. A pointer to the lambdas __invoke method
85 // will be stored after the vtable. We will obtain the lambdas name from
86 // this entry and lookup operator()() and obtain the line table entry.
87 // 3) a callable object via operator()(). We will obtain the name of the
88 // object from the first template parameter from __func's vtable. We will
89 // look up the objectc operator()() and obtain the line table entry.
90 // 4) a member function. A pointer to the function will stored after the
91 // we will obtain the name from this pointer.
92 // 5) a free function. A pointer to the function will stored after the vtable
93 // we will obtain the name from this pointer.
94 ValueObjectSP member__f_(
95 valobj_sp->GetChildMemberWithName(ConstString("__f_"), true));
98 ValueObjectSP sub_member__f_(
99 member__f_->GetChildMemberWithName(ConstString("__f_"), true));
102 member__f_ = sub_member__f_;
105 lldb::addr_t member__f_pointer_value = member__f_->GetValueAsUnsigned(0);
107 optional_info.member__f_pointer_value = member__f_pointer_value;
109 ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef());
110 Process *process = exe_ctx.GetProcessPtr();
112 if (process == nullptr)
113 return optional_info;
115 uint32_t address_size = process->GetAddressByteSize();
118 // First item pointed to by __f_ should be the pointer to the vtable for
120 lldb::addr_t vtable_address =
121 process->ReadPointerFromMemory(member__f_pointer_value, status);
124 return optional_info;
126 lldb::addr_t address_after_vtable = member__f_pointer_value + address_size;
127 // As commened above we may not have a function pointer but if we do we will
129 lldb::addr_t possible_function_address =
130 process->ReadPointerFromMemory(address_after_vtable, status);
133 return optional_info;
135 Target &target = process->GetTarget();
137 if (target.GetSectionLoadList().IsEmpty())
138 return optional_info;
140 Address vtable_addr_resolved;
144 if (!target.GetSectionLoadList().ResolveLoadAddress(vtable_address,
145 vtable_addr_resolved))
146 return optional_info;
148 target.GetImages().ResolveSymbolContextForAddress(
149 vtable_addr_resolved, eSymbolContextEverything, sc);
152 if (symbol == nullptr)
153 return optional_info;
155 llvm::StringRef vtable_name(symbol->GetName().GetCString());
156 bool found_expected_start_string =
157 vtable_name.startswith("vtable for std::__1::__function::__func<");
159 if (!found_expected_start_string)
160 return optional_info;
162 // Given case 1 or 3 we have a vtable name, we are want to extract the first
163 // template parameter
165 // ... __func<main::$_0, std::__1::allocator<main::$_0> ...
168 // We do this by find the first < and , and extracting in between.
170 // This covers the case of the lambda known at compile time.
171 size_t first_open_angle_bracket = vtable_name.find('<') + 1;
172 size_t first_comma = vtable_name.find_first_of(',');
174 llvm::StringRef first_template_parameter =
175 vtable_name.slice(first_open_angle_bracket, first_comma);
177 Address function_address_resolved;
179 // Setup for cases 2, 4 and 5 we have a pointer to a function after the
180 // vtable. We will use a process of elimination to drop through each case
181 // and obtain the data we need.
182 if (target.GetSectionLoadList().ResolveLoadAddress(
183 possible_function_address, function_address_resolved)) {
184 target.GetImages().ResolveSymbolContextForAddress(
185 function_address_resolved, eSymbolContextEverything, sc);
189 auto get_name = [&first_template_parameter, &symbol]() {
194 // we want to append ::operator()()
195 if (first_template_parameter.contains("$_"))
196 return llvm::Regex::escape(first_template_parameter.str()) +
197 R"(::operator\(\)\(.*\))";
199 if (symbol != NULL &&
200 symbol->GetName().GetStringRef().contains("__invoke")) {
202 llvm::StringRef symbol_name = symbol->GetName().GetStringRef();
203 size_t pos2 = symbol_name.find_last_of(':');
207 // main::$_1::__invoke(...)
209 // We want to slice off __invoke(...) and append operator()()
210 std::string lambda_operator =
211 llvm::Regex::escape(symbol_name.slice(0, pos2 + 1).str()) +
212 R"(operator\(\)\(.*\))";
214 return lambda_operator;
218 return first_template_parameter.str() + R"(::operator\(\)\(.*\))";
222 std::string func_to_match = get_name();
224 SymbolContextList scl;
226 target.GetImages().FindFunctions(RegularExpression{func_to_match}, true, true,
230 if (scl.GetSize() >= 1) {
231 SymbolContext sc2 = scl[0];
234 sc2.GetAddressRange(eSymbolContextEverything, 0, false, range);
236 Address address = range.GetBaseAddress();
239 if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),
241 LineEntry line_entry;
242 addr.CalculateSymbolContextLineEntry(line_entry);
244 if (first_template_parameter.contains("$_") ||
245 (symbol != nullptr &&
246 symbol->GetName().GetStringRef().contains("__invoke"))) {
248 optional_info.callable_case = LibCppStdFunctionCallableCase::Lambda;
251 optional_info.callable_case =
252 LibCppStdFunctionCallableCase::CallableObject;
255 optional_info.callable_symbol = *symbol;
256 optional_info.callable_line_entry = line_entry;
257 optional_info.callable_address = addr;
258 return optional_info;
263 if (!symbol->GetName().GetStringRef().startswith("vtable for")) {
264 optional_info.callable_case =
265 LibCppStdFunctionCallableCase::FreeOrMemberFunction;
266 optional_info.callable_address = function_address_resolved;
267 optional_info.callable_symbol = *symbol;
269 return optional_info;
272 return optional_info;
276 CPPLanguageRuntime::GetStepThroughTrampolinePlan(Thread &thread,
278 ThreadPlanSP ret_plan_sp;
280 lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC();
282 TargetSP target_sp(thread.CalculateTarget());
284 if (target_sp->GetSectionLoadList().IsEmpty())
287 Address pc_addr_resolved;
291 if (!target_sp->GetSectionLoadList().ResolveLoadAddress(curr_pc,
295 target_sp->GetImages().ResolveSymbolContextForAddress(
296 pc_addr_resolved, eSymbolContextEverything, sc);
299 if (symbol == nullptr)
302 llvm::StringRef function_name(symbol->GetName().GetCString());
304 // Handling the case where we are attempting to step into std::function.
305 // The behavior will be that we will attempt to obtain the wrapped
306 // callable via FindLibCppStdFunctionCallableInfo() and if we find it we
307 // will return a ThreadPlanRunToAddress to the callable. Therefore we will
308 // step into the wrapped callable.
310 bool found_expected_start_string =
311 function_name.startswith("std::__1::function<");
313 if (!found_expected_start_string)
316 AddressRange range_of_curr_func;
317 sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func);
319 StackFrameSP frame = thread.GetStackFrameAtIndex(0);
322 ValueObjectSP value_sp = frame->FindVariable(ConstString("this"));
324 CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info =
325 FindLibCppStdFunctionCallableInfo(value_sp);
327 if (callable_info.callable_case != LibCppStdFunctionCallableCase::Invalid &&
328 value_sp->GetValueIsValid()) {
329 // We found the std::function wrapped callable and we have its address.
330 // We now create a ThreadPlan to run to the callable.
331 ret_plan_sp.reset(new ThreadPlanRunToAddress(
332 thread, callable_info.callable_address, stop_others));
335 // We are in std::function but we could not obtain the callable.
336 // We create a ThreadPlan to keep stepping through using the address range
337 // of the current function.
338 ret_plan_sp.reset(new ThreadPlanStepInRange(thread, range_of_curr_func,
340 eLazyBoolYes, eLazyBoolYes));