#include <cstring>
#include <memory>
#include "CPPLanguageRuntime.h"
#include "llvm/ADT/StringRef.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/UniqueCStringMap.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/ThreadPlanRunToAddress.h"
#include "lldb/Target/ThreadPlanStepInRange.h"
#include "lldb/Utility/Timer.h"
using namespace lldb;
using namespace lldb_private;
static ConstString g_this = ConstString("this");
static ConstString g_promise = ConstString("__promise");
static ConstString g_coro_frame = ConstString("__coro_frame");
char CPPLanguageRuntime::ID = 0;
CPPLanguageRuntime::CPPLanguageRuntime(Process *process)
: LanguageRuntime(process) {}
bool CPPLanguageRuntime::IsAllowedRuntimeValue(ConstString name) {
return name == g_this || name == g_promise || name == g_coro_frame;
}
llvm::Error CPPLanguageRuntime::GetObjectDescription(Stream &str,
ValueObject &object) {
return llvm::createStringError("C++ does not support object descriptions");
}
llvm::Error
CPPLanguageRuntime::GetObjectDescription(Stream &str, Value &value,
ExecutionContextScope *exe_scope) {
return llvm::createStringError("C++ does not support object descriptions");
}
bool contains_lambda_identifier(llvm::StringRef &str_ref) {
return str_ref.contains("$_") || str_ref.contains("'lambda'");
}
CPPLanguageRuntime::LibCppStdFunctionCallableInfo
line_entry_helper(Target &target, const SymbolContext &sc, Symbol *symbol,
llvm::StringRef first_template_param_sref,
bool has_invoke) {
CPPLanguageRuntime::LibCppStdFunctionCallableInfo optional_info;
AddressRange range;
sc.GetAddressRange(eSymbolContextEverything, 0, false, range);
Address address = range.GetBaseAddress();
Address addr;
if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),
addr)) {
LineEntry line_entry;
addr.CalculateSymbolContextLineEntry(line_entry);
if (contains_lambda_identifier(first_template_param_sref) || has_invoke) {
optional_info.callable_case = lldb_private::CPPLanguageRuntime::
LibCppStdFunctionCallableCase::Lambda;
} else {
optional_info.callable_case = lldb_private::CPPLanguageRuntime::
LibCppStdFunctionCallableCase::CallableObject;
}
optional_info.callable_symbol = *symbol;
optional_info.callable_line_entry = line_entry;
optional_info.callable_address = addr;
}
return optional_info;
}
CPPLanguageRuntime::LibCppStdFunctionCallableInfo
CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo(
lldb::ValueObjectSP &valobj_sp) {
LLDB_SCOPED_TIMER();
LibCppStdFunctionCallableInfo optional_info;
if (!valobj_sp)
return optional_info;
ValueObjectSP member_f_(valobj_sp->GetChildMemberWithName("__f_"));
if (member_f_) {
ValueObjectSP sub_member_f_(member_f_->GetChildMemberWithName("__f_"));
if (sub_member_f_)
member_f_ = sub_member_f_;
}
if (!member_f_)
return optional_info;
lldb::addr_t member_f_pointer_value = member_f_->GetValueAsUnsigned(0);
optional_info.member_f_pointer_value = member_f_pointer_value;
if (!member_f_pointer_value)
return optional_info;
ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef());
Process *process = exe_ctx.GetProcessPtr();
if (process == nullptr)
return optional_info;
uint32_t address_size = process->GetAddressByteSize();
Status status;
lldb::addr_t vtable_address =
process->ReadPointerFromMemory(member_f_pointer_value, status);
if (status.Fail())
return optional_info;
lldb::addr_t vtable_address_first_entry =
process->ReadPointerFromMemory(vtable_address + address_size, status);
if (status.Fail())
return optional_info;
lldb::addr_t address_after_vtable = member_f_pointer_value + address_size;
lldb::addr_t possible_function_address =
process->ReadPointerFromMemory(address_after_vtable, status);
if (status.Fail())
return optional_info;
Target &target = process->GetTarget();
if (target.GetSectionLoadList().IsEmpty())
return optional_info;
Address vtable_first_entry_resolved;
if (!target.GetSectionLoadList().ResolveLoadAddress(
vtable_address_first_entry, vtable_first_entry_resolved))
return optional_info;
Address vtable_addr_resolved;
SymbolContext sc;
Symbol *symbol = nullptr;
if (!target.GetSectionLoadList().ResolveLoadAddress(vtable_address,
vtable_addr_resolved))
return optional_info;
target.GetImages().ResolveSymbolContextForAddress(
vtable_addr_resolved, eSymbolContextEverything, sc);
symbol = sc.symbol;
if (symbol == nullptr)
return optional_info;
llvm::StringRef vtable_name(symbol->GetName().GetStringRef());
bool found_expected_start_string =
vtable_name.starts_with("vtable for std::__1::__function::__func<");
if (!found_expected_start_string)
return optional_info;
size_t first_open_angle_bracket = vtable_name.find('<') + 1;
size_t first_comma = vtable_name.find(',');
llvm::StringRef first_template_parameter =
vtable_name.slice(first_open_angle_bracket, first_comma);
Address function_address_resolved;
if (target.GetSectionLoadList().ResolveLoadAddress(
possible_function_address, function_address_resolved)) {
target.GetImages().ResolveSymbolContextForAddress(
function_address_resolved, eSymbolContextEverything, sc);
symbol = sc.symbol;
}
bool has_invoke =
(symbol ? symbol->GetName().GetStringRef().contains("__invoke") : false);
auto calculate_symbol_context_helper = [](auto &t,
SymbolContextList &sc_list) {
SymbolContext sc;
t->CalculateSymbolContext(&sc);
sc_list.Append(sc);
};
if (has_invoke) {
SymbolContextList scl;
calculate_symbol_context_helper(symbol, scl);
return line_entry_helper(target, scl[0], symbol, first_template_parameter,
has_invoke);
}
if (symbol && !symbol->GetName().GetStringRef().starts_with("vtable for") &&
!contains_lambda_identifier(first_template_parameter) && !has_invoke) {
optional_info.callable_case =
LibCppStdFunctionCallableCase::FreeOrMemberFunction;
optional_info.callable_address = function_address_resolved;
optional_info.callable_symbol = *symbol;
return optional_info;
}
std::string func_to_match = first_template_parameter.str();
auto it = CallableLookupCache.find(func_to_match);
if (it != CallableLookupCache.end())
return it->second;
SymbolContextList scl;
CompileUnit *vtable_cu =
vtable_first_entry_resolved.CalculateSymbolContextCompileUnit();
llvm::StringRef name_to_use = func_to_match;
if (!contains_lambda_identifier(name_to_use))
return optional_info;
if (vtable_cu && !has_invoke) {
lldb::FunctionSP func_sp =
vtable_cu->FindFunction([name_to_use](const FunctionSP &f) {
auto name = f->GetName().GetStringRef();
if (name.starts_with(name_to_use) && name.contains("operator"))
return true;
return false;
});
if (func_sp) {
calculate_symbol_context_helper(func_sp, scl);
}
}
if (symbol == nullptr)
return optional_info;
if (scl.GetSize() >= 1) {
optional_info = line_entry_helper(target, scl[0], symbol,
first_template_parameter, has_invoke);
}
CallableLookupCache[func_to_match] = optional_info;
return optional_info;
}
lldb::ThreadPlanSP
CPPLanguageRuntime::GetStepThroughTrampolinePlan(Thread &thread,
bool stop_others) {
ThreadPlanSP ret_plan_sp;
lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC();
TargetSP target_sp(thread.CalculateTarget());
if (target_sp->GetSectionLoadList().IsEmpty())
return ret_plan_sp;
Address pc_addr_resolved;
SymbolContext sc;
Symbol *symbol;
if (!target_sp->GetSectionLoadList().ResolveLoadAddress(curr_pc,
pc_addr_resolved))
return ret_plan_sp;
target_sp->GetImages().ResolveSymbolContextForAddress(
pc_addr_resolved, eSymbolContextEverything, sc);
symbol = sc.symbol;
if (symbol == nullptr)
return ret_plan_sp;
llvm::StringRef function_name(symbol->GetName().GetCString());
bool found_expected_start_string =
function_name.starts_with("std::__1::function<");
if (!found_expected_start_string)
return ret_plan_sp;
AddressRange range_of_curr_func;
sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func);
StackFrameSP frame = thread.GetStackFrameAtIndex(0);
if (frame) {
ValueObjectSP value_sp = frame->FindVariable(g_this);
CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info =
FindLibCppStdFunctionCallableInfo(value_sp);
if (callable_info.callable_case != LibCppStdFunctionCallableCase::Invalid &&
value_sp->GetValueIsValid()) {
ret_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
thread, callable_info.callable_address, stop_others);
return ret_plan_sp;
} else {
ret_plan_sp = std::make_shared<ThreadPlanStepInRange>(
thread, range_of_curr_func, sc, nullptr, eOnlyThisThread,
eLazyBoolYes, eLazyBoolYes);
return ret_plan_sp;
}
}
return ret_plan_sp;
}