blob: 64348840ccb8bbb94c1567c832b116d9a811f142 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- CPPLanguageRuntime.cpp---------------------------------------------===//
Jim Ingham22777012010-09-23 02:01:19 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jim Ingham22777012010-09-23 02:01:19 +00006//
7//===----------------------------------------------------------------------===//
8
Greg Clayton6ecb2322013-05-18 00:11:21 +00009#include <string.h>
10
Jonas Devlieghere796ac802019-02-11 23:13:08 +000011#include <memory>
12
Alex Langforde0678ca2019-07-12 20:09:32 +000013#include "CPPLanguageRuntime.h"
14
Jim Inghamfa39bb42014-10-25 00:33:55 +000015#include "llvm/ADT/StringRef.h"
16
Shafik Yaghmour443e20b2018-09-11 20:58:28 +000017#include "lldb/Symbol/Block.h"
Adrian Prantl1db0f0c2019-05-02 23:07:23 +000018#include "lldb/Symbol/Variable.h"
Shafik Yaghmour443e20b2018-09-11 20:58:28 +000019#include "lldb/Symbol/VariableList.h"
20
Alex Langford8be30212020-01-29 11:59:28 -080021#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
Jim Ingham22777012010-09-23 02:01:19 +000022#include "lldb/Core/PluginManager.h"
Enrico Granata1d261d12012-02-03 01:41:25 +000023#include "lldb/Core/UniqueCStringMap.h"
shafik91e94a72019-11-12 11:23:38 -080024#include "lldb/Symbol/CompileUnit.h"
Shafik Yaghmour443e20b2018-09-11 20:58:28 +000025#include "lldb/Target/ABI.h"
Jim Ingham5a369122010-09-28 01:25:32 +000026#include "lldb/Target/ExecutionContext.h"
Shafik Yaghmour443e20b2018-09-11 20:58:28 +000027#include "lldb/Target/RegisterContext.h"
28#include "lldb/Target/SectionLoadList.h"
29#include "lldb/Target/StackFrame.h"
30#include "lldb/Target/ThreadPlanRunToAddress.h"
Shafik Yaghmouraa302682018-10-12 17:20:39 +000031#include "lldb/Target/ThreadPlanStepInRange.h"
shafik91e94a72019-11-12 11:23:38 -080032#include "lldb/Utility/Timer.h"
Jim Ingham22777012010-09-23 02:01:19 +000033
34using namespace lldb;
35using namespace lldb_private;
36
Adrian Prantl1db0f0c2019-05-02 23:07:23 +000037static ConstString g_this = ConstString("this");
38
Alex Langford056f6f12019-06-08 18:45:00 +000039char CPPLanguageRuntime::ID = 0;
40
Jim Ingham22777012010-09-23 02:01:19 +000041// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000042CPPLanguageRuntime::~CPPLanguageRuntime() {}
43
44CPPLanguageRuntime::CPPLanguageRuntime(Process *process)
45 : LanguageRuntime(process) {}
46
Alex Langfordd7fcee62019-07-01 20:36:33 +000047bool CPPLanguageRuntime::IsWhitelistedRuntimeValue(ConstString name) {
48 return name == g_this;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051bool CPPLanguageRuntime::GetObjectDescription(Stream &str,
52 ValueObject &object) {
53 // C++ has no generic way to do this.
54 return false;
Jim Ingham22777012010-09-23 02:01:19 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057bool CPPLanguageRuntime::GetObjectDescription(
58 Stream &str, Value &value, ExecutionContextScope *exe_scope) {
59 // C++ has no generic way to do this.
60 return false;
Jim Ingham6c68fb42010-09-30 00:54:27 +000061}
Shafik Yaghmour443e20b2018-09-11 20:58:28 +000062
shafik91e94a72019-11-12 11:23:38 -080063bool contains_lambda_identifier(llvm::StringRef &str_ref) {
64 return str_ref.contains("$_") || str_ref.contains("'lambda'");
Pavel Labath6612fab2019-11-26 14:47:28 +010065}
shafik91e94a72019-11-12 11:23:38 -080066
67CPPLanguageRuntime::LibCppStdFunctionCallableInfo
68line_entry_helper(Target &target, const SymbolContext &sc, Symbol *symbol,
69 llvm::StringRef first_template_param_sref,
70 bool has___invoke) {
71
72 CPPLanguageRuntime::LibCppStdFunctionCallableInfo optional_info;
73
74 AddressRange range;
75 sc.GetAddressRange(eSymbolContextEverything, 0, false, range);
76
77 Address address = range.GetBaseAddress();
78
79 Address addr;
80 if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),
81 addr)) {
82 LineEntry line_entry;
83 addr.CalculateSymbolContextLineEntry(line_entry);
84
85 if (contains_lambda_identifier(first_template_param_sref) || has___invoke) {
86 // Case 1 and 2
87 optional_info.callable_case = lldb_private::CPPLanguageRuntime::
88 LibCppStdFunctionCallableCase::Lambda;
89 } else {
90 // Case 3
91 optional_info.callable_case = lldb_private::CPPLanguageRuntime::
92 LibCppStdFunctionCallableCase::CallableObject;
93 }
94
95 optional_info.callable_symbol = *symbol;
96 optional_info.callable_line_entry = line_entry;
97 optional_info.callable_address = addr;
98 }
99
100 return optional_info;
101}
102
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000103CPPLanguageRuntime::LibCppStdFunctionCallableInfo
104CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo(
105 lldb::ValueObjectSP &valobj_sp) {
shafik91e94a72019-11-12 11:23:38 -0800106 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
107 Timer scoped_timer(func_cat,
108 "CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo");
109
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000110 LibCppStdFunctionCallableInfo optional_info;
111
112 if (!valobj_sp)
113 return optional_info;
114
115 // Member __f_ has type __base*, the contents of which will hold:
116 // 1) a vtable entry which may hold type information needed to discover the
117 // lambda being called
118 // 2) possibly hold a pointer to the callable object
119 // e.g.
120 //
121 // (lldb) frame var -R f_display
122 // (std::__1::function<void (int)>) f_display = {
123 // __buf_ = {
124 // …
125 // }
126 // __f_ = 0x00007ffeefbffa00
127 // }
128 // (lldb) memory read -fA 0x00007ffeefbffa00
129 // 0x7ffeefbffa00: ... `vtable for std::__1::__function::__func<void (*) ...
130 // 0x7ffeefbffa08: ... `print_num(int) at std_function_cppreference_exam ...
131 //
132 // We will be handling five cases below, std::function is wrapping:
133 //
134 // 1) a lambda we know at compile time. We will obtain the name of the lambda
135 // from the first template pameter from __func's vtable. We will look up
136 // the lambda's operator()() and obtain the line table entry.
137 // 2) a lambda we know at runtime. A pointer to the lambdas __invoke method
138 // will be stored after the vtable. We will obtain the lambdas name from
139 // this entry and lookup operator()() and obtain the line table entry.
140 // 3) a callable object via operator()(). We will obtain the name of the
141 // object from the first template parameter from __func's vtable. We will
shafik91e94a72019-11-12 11:23:38 -0800142 // look up the objects operator()() and obtain the line table entry.
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000143 // 4) a member function. A pointer to the function will stored after the
144 // we will obtain the name from this pointer.
145 // 5) a free function. A pointer to the function will stored after the vtable
146 // we will obtain the name from this pointer.
147 ValueObjectSP member__f_(
148 valobj_sp->GetChildMemberWithName(ConstString("__f_"), true));
Shafik Yaghmour99bc2b22018-12-10 23:26:38 +0000149
150 if (member__f_) {
151 ValueObjectSP sub_member__f_(
152 member__f_->GetChildMemberWithName(ConstString("__f_"), true));
153
154 if (sub_member__f_)
155 member__f_ = sub_member__f_;
156 }
157
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000158 lldb::addr_t member__f_pointer_value = member__f_->GetValueAsUnsigned(0);
159
160 optional_info.member__f_pointer_value = member__f_pointer_value;
161
shafik91e94a72019-11-12 11:23:38 -0800162 if (!member__f_pointer_value)
163 return optional_info;
164
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000165 ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef());
166 Process *process = exe_ctx.GetProcessPtr();
167
168 if (process == nullptr)
169 return optional_info;
170
171 uint32_t address_size = process->GetAddressByteSize();
172 Status status;
173
174 // First item pointed to by __f_ should be the pointer to the vtable for
175 // a __base object.
176 lldb::addr_t vtable_address =
177 process->ReadPointerFromMemory(member__f_pointer_value, status);
178
179 if (status.Fail())
180 return optional_info;
181
shafik91e94a72019-11-12 11:23:38 -0800182 lldb::addr_t vtable_address_first_entry =
183 process->ReadPointerFromMemory(vtable_address + address_size, status);
184
185 if (status.Fail())
186 return optional_info;
187
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000188 lldb::addr_t address_after_vtable = member__f_pointer_value + address_size;
shafik91e94a72019-11-12 11:23:38 -0800189 // As commented above we may not have a function pointer but if we do we will
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000190 // need it.
191 lldb::addr_t possible_function_address =
192 process->ReadPointerFromMemory(address_after_vtable, status);
193
194 if (status.Fail())
195 return optional_info;
196
197 Target &target = process->GetTarget();
198
199 if (target.GetSectionLoadList().IsEmpty())
200 return optional_info;
201
shafik91e94a72019-11-12 11:23:38 -0800202 Address vtable_first_entry_resolved;
203
204 if (!target.GetSectionLoadList().ResolveLoadAddress(
205 vtable_address_first_entry, vtable_first_entry_resolved))
206 return optional_info;
207
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000208 Address vtable_addr_resolved;
209 SymbolContext sc;
shafik91e94a72019-11-12 11:23:38 -0800210 Symbol *symbol = nullptr;
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000211
212 if (!target.GetSectionLoadList().ResolveLoadAddress(vtable_address,
213 vtable_addr_resolved))
214 return optional_info;
215
216 target.GetImages().ResolveSymbolContextForAddress(
217 vtable_addr_resolved, eSymbolContextEverything, sc);
218 symbol = sc.symbol;
219
220 if (symbol == nullptr)
221 return optional_info;
222
shafik91e94a72019-11-12 11:23:38 -0800223 llvm::StringRef vtable_name(symbol->GetName().GetStringRef());
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000224 bool found_expected_start_string =
225 vtable_name.startswith("vtable for std::__1::__function::__func<");
226
227 if (!found_expected_start_string)
228 return optional_info;
229
230 // Given case 1 or 3 we have a vtable name, we are want to extract the first
231 // template parameter
232 //
233 // ... __func<main::$_0, std::__1::allocator<main::$_0> ...
234 // ^^^^^^^^^
235 //
shafik91e94a72019-11-12 11:23:38 -0800236 // We could see names such as:
237 // main::$_0
238 // Bar::add_num2(int)::'lambda'(int)
239 // Bar
240 //
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000241 // We do this by find the first < and , and extracting in between.
242 //
243 // This covers the case of the lambda known at compile time.
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000244 size_t first_open_angle_bracket = vtable_name.find('<') + 1;
Jonas Devliegherec712bac2019-03-28 18:10:14 +0000245 size_t first_comma = vtable_name.find(',');
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000246
247 llvm::StringRef first_template_parameter =
248 vtable_name.slice(first_open_angle_bracket, first_comma);
249
250 Address function_address_resolved;
251
252 // Setup for cases 2, 4 and 5 we have a pointer to a function after the
253 // vtable. We will use a process of elimination to drop through each case
254 // and obtain the data we need.
255 if (target.GetSectionLoadList().ResolveLoadAddress(
256 possible_function_address, function_address_resolved)) {
257 target.GetImages().ResolveSymbolContextForAddress(
258 function_address_resolved, eSymbolContextEverything, sc);
259 symbol = sc.symbol;
260 }
shafik91e94a72019-11-12 11:23:38 -0800261
262 // These conditions are used several times to simplify statements later on.
263 bool has___invoke =
264 (symbol ? symbol->GetName().GetStringRef().contains("__invoke") : false);
265 auto calculate_symbol_context_helper = [](auto &t,
266 SymbolContextList &sc_list) {
267 SymbolContext sc;
268 t->CalculateSymbolContext(&sc);
269 sc_list.Append(sc);
270 };
271
272 // Case 2
273 if (has___invoke) {
274 SymbolContextList scl;
275 calculate_symbol_context_helper(symbol, scl);
276
277 return line_entry_helper(target, scl[0], symbol, first_template_parameter,
278 has___invoke);
279 }
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000280
shafike18f4db2019-11-06 15:57:52 -0800281 // Case 4 or 5
shafike18f4db2019-11-06 15:57:52 -0800282 if (symbol && !symbol->GetName().GetStringRef().startswith("vtable for") &&
shafik91e94a72019-11-12 11:23:38 -0800283 !contains_lambda_identifier(first_template_parameter) && !has___invoke) {
shafike18f4db2019-11-06 15:57:52 -0800284 optional_info.callable_case =
285 LibCppStdFunctionCallableCase::FreeOrMemberFunction;
286 optional_info.callable_address = function_address_resolved;
287 optional_info.callable_symbol = *symbol;
288
289 return optional_info;
290 }
291
shafik91e94a72019-11-12 11:23:38 -0800292 std::string func_to_match = first_template_parameter.str();
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000293
shafike18f4db2019-11-06 15:57:52 -0800294 auto it = CallableLookupCache.find(func_to_match);
295 if (it != CallableLookupCache.end())
296 return it->second;
297
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000298 SymbolContextList scl;
299
shafik91e94a72019-11-12 11:23:38 -0800300 CompileUnit *vtable_cu =
301 vtable_first_entry_resolved.CalculateSymbolContextCompileUnit();
302 llvm::StringRef name_to_use = func_to_match;
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000303
shafik91e94a72019-11-12 11:23:38 -0800304 // Case 3, we have a callable object instead of a lambda
305 //
306 // TODO
307 // We currently don't support this case a callable object may have multiple
308 // operator()() varying on const/non-const and number of arguments and we
309 // don't have a way to currently distinguish them so we will bail out now.
310 if (!contains_lambda_identifier(name_to_use))
311 return optional_info;
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000312
shafik91e94a72019-11-12 11:23:38 -0800313 if (vtable_cu && !has___invoke) {
314 lldb::FunctionSP func_sp =
315 vtable_cu->FindFunction([name_to_use](const FunctionSP &f) {
316 auto name = f->GetName().GetStringRef();
317 if (name.startswith(name_to_use) && name.contains("operator"))
318 return true;
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000319
shafik91e94a72019-11-12 11:23:38 -0800320 return false;
321 });
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000322
shafik91e94a72019-11-12 11:23:38 -0800323 if (func_sp) {
324 calculate_symbol_context_helper(func_sp, scl);
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000325 }
326 }
327
shafik91e94a72019-11-12 11:23:38 -0800328 // Case 1 or 3
329 if (scl.GetSize() >= 1) {
330 optional_info = line_entry_helper(target, scl[0], symbol,
331 first_template_parameter, has___invoke);
332 }
333
shafike18f4db2019-11-06 15:57:52 -0800334 CallableLookupCache[func_to_match] = optional_info;
Shafik Yaghmour443e20b2018-09-11 20:58:28 +0000335
336 return optional_info;
337}
Shafik Yaghmouraa302682018-10-12 17:20:39 +0000338
339lldb::ThreadPlanSP
340CPPLanguageRuntime::GetStepThroughTrampolinePlan(Thread &thread,
341 bool stop_others) {
342 ThreadPlanSP ret_plan_sp;
343
344 lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC();
345
346 TargetSP target_sp(thread.CalculateTarget());
347
348 if (target_sp->GetSectionLoadList().IsEmpty())
349 return ret_plan_sp;
350
351 Address pc_addr_resolved;
352 SymbolContext sc;
353 Symbol *symbol;
354
355 if (!target_sp->GetSectionLoadList().ResolveLoadAddress(curr_pc,
356 pc_addr_resolved))
357 return ret_plan_sp;
358
359 target_sp->GetImages().ResolveSymbolContextForAddress(
360 pc_addr_resolved, eSymbolContextEverything, sc);
361 symbol = sc.symbol;
362
363 if (symbol == nullptr)
364 return ret_plan_sp;
365
366 llvm::StringRef function_name(symbol->GetName().GetCString());
367
368 // Handling the case where we are attempting to step into std::function.
369 // The behavior will be that we will attempt to obtain the wrapped
370 // callable via FindLibCppStdFunctionCallableInfo() and if we find it we
371 // will return a ThreadPlanRunToAddress to the callable. Therefore we will
372 // step into the wrapped callable.
373 //
374 bool found_expected_start_string =
375 function_name.startswith("std::__1::function<");
376
377 if (!found_expected_start_string)
378 return ret_plan_sp;
379
380 AddressRange range_of_curr_func;
381 sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func);
382
383 StackFrameSP frame = thread.GetStackFrameAtIndex(0);
384
385 if (frame) {
Adrian Prantl1db0f0c2019-05-02 23:07:23 +0000386 ValueObjectSP value_sp = frame->FindVariable(g_this);
Shafik Yaghmouraa302682018-10-12 17:20:39 +0000387
388 CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info =
389 FindLibCppStdFunctionCallableInfo(value_sp);
390
391 if (callable_info.callable_case != LibCppStdFunctionCallableCase::Invalid &&
392 value_sp->GetValueIsValid()) {
393 // We found the std::function wrapped callable and we have its address.
394 // We now create a ThreadPlan to run to the callable.
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000395 ret_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
396 thread, callable_info.callable_address, stop_others);
Shafik Yaghmouraa302682018-10-12 17:20:39 +0000397 return ret_plan_sp;
398 } else {
399 // We are in std::function but we could not obtain the callable.
400 // We create a ThreadPlan to keep stepping through using the address range
401 // of the current function.
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000402 ret_plan_sp = std::make_shared<ThreadPlanStepInRange>(
403 thread, range_of_curr_func, sc, eOnlyThisThread, eLazyBoolYes,
404 eLazyBoolYes);
Shafik Yaghmouraa302682018-10-12 17:20:39 +0000405 return ret_plan_sp;
406 }
407 }
408
409 return ret_plan_sp;
410}