1 //===-- CPPLanguageRuntime.cpp 2 //-------------------------------------------------*- C++ -*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #include "lldb/Target/CPPLanguageRuntime.h" 12 13 #include <string.h> 14 15 #include "llvm/ADT/StringRef.h" 16 17 #include "lldb/Symbol/Block.h" 18 #include "lldb/Symbol/VariableList.h" 19 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" 30 31 using namespace lldb; 32 using namespace lldb_private; 33 34 //---------------------------------------------------------------------- 35 // Destructor 36 //---------------------------------------------------------------------- 37 CPPLanguageRuntime::~CPPLanguageRuntime() {} 38 39 CPPLanguageRuntime::CPPLanguageRuntime(Process *process) 40 : LanguageRuntime(process) {} 41 42 bool CPPLanguageRuntime::GetObjectDescription(Stream &str, 43 ValueObject &object) { 44 // C++ has no generic way to do this. 45 return false; 46 } 47 48 bool CPPLanguageRuntime::GetObjectDescription( 49 Stream &str, Value &value, ExecutionContextScope *exe_scope) { 50 // C++ has no generic way to do this. 51 return false; 52 } 53 54 CPPLanguageRuntime::LibCppStdFunctionCallableInfo 55 CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo( 56 lldb::ValueObjectSP &valobj_sp) { 57 LibCppStdFunctionCallableInfo optional_info; 58 59 if (!valobj_sp) 60 return optional_info; 61 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 66 // e.g. 67 // 68 // (lldb) frame var -R f_display 69 // (std::__1::function<void (int)>) f_display = { 70 // __buf_ = { 71 // … 72 // } 73 // __f_ = 0x00007ffeefbffa00 74 // } 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 ... 78 // 79 // We will be handling five cases below, std::function is wrapping: 80 // 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)); 96 97 if (member__f_) { 98 ValueObjectSP sub_member__f_( 99 member__f_->GetChildMemberWithName(ConstString("__f_"), true)); 100 101 if (sub_member__f_) 102 member__f_ = sub_member__f_; 103 } 104 105 lldb::addr_t member__f_pointer_value = member__f_->GetValueAsUnsigned(0); 106 107 optional_info.member__f_pointer_value = member__f_pointer_value; 108 109 ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef()); 110 Process *process = exe_ctx.GetProcessPtr(); 111 112 if (process == nullptr) 113 return optional_info; 114 115 uint32_t address_size = process->GetAddressByteSize(); 116 Status status; 117 118 // First item pointed to by __f_ should be the pointer to the vtable for 119 // a __base object. 120 lldb::addr_t vtable_address = 121 process->ReadPointerFromMemory(member__f_pointer_value, status); 122 123 if (status.Fail()) 124 return optional_info; 125 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 128 // need it. 129 lldb::addr_t possible_function_address = 130 process->ReadPointerFromMemory(address_after_vtable, status); 131 132 if (status.Fail()) 133 return optional_info; 134 135 Target &target = process->GetTarget(); 136 137 if (target.GetSectionLoadList().IsEmpty()) 138 return optional_info; 139 140 Address vtable_addr_resolved; 141 SymbolContext sc; 142 Symbol *symbol; 143 144 if (!target.GetSectionLoadList().ResolveLoadAddress(vtable_address, 145 vtable_addr_resolved)) 146 return optional_info; 147 148 target.GetImages().ResolveSymbolContextForAddress( 149 vtable_addr_resolved, eSymbolContextEverything, sc); 150 symbol = sc.symbol; 151 152 if (symbol == nullptr) 153 return optional_info; 154 155 llvm::StringRef vtable_name(symbol->GetName().GetCString()); 156 bool found_expected_start_string = 157 vtable_name.startswith("vtable for std::__1::__function::__func<"); 158 159 if (!found_expected_start_string) 160 return optional_info; 161 162 // Given case 1 or 3 we have a vtable name, we are want to extract the first 163 // template parameter 164 // 165 // ... __func<main::$_0, std::__1::allocator<main::$_0> ... 166 // ^^^^^^^^^ 167 // 168 // We do this by find the first < and , and extracting in between. 169 // 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(','); 173 174 llvm::StringRef first_template_parameter = 175 vtable_name.slice(first_open_angle_bracket, first_comma); 176 177 Address function_address_resolved; 178 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); 186 symbol = sc.symbol; 187 } 188 189 auto get_name = [&first_template_parameter, &symbol]() { 190 // Given case 1: 191 // 192 // main::$_0 193 // 194 // we want to append ::operator()() 195 if (first_template_parameter.contains("$_")) 196 return llvm::Regex::escape(first_template_parameter.str()) + 197 R"(::operator\(\)\(.*\))"; 198 199 if (symbol != NULL && 200 symbol->GetName().GetStringRef().contains("__invoke")) { 201 202 llvm::StringRef symbol_name = symbol->GetName().GetStringRef(); 203 size_t pos2 = symbol_name.find_last_of(':'); 204 205 // Given case 2: 206 // 207 // main::$_1::__invoke(...) 208 // 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\(\)\(.*\))"; 213 214 return lambda_operator; 215 } 216 217 // Case 3 218 return first_template_parameter.str() + R"(::operator\(\)\(.*\))"; 219 ; 220 }; 221 222 std::string func_to_match = get_name(); 223 224 SymbolContextList scl; 225 226 target.GetImages().FindFunctions(RegularExpression{func_to_match}, true, true, 227 true, scl); 228 229 // Case 1,2 or 3 230 if (scl.GetSize() >= 1) { 231 SymbolContext sc2 = scl[0]; 232 233 AddressRange range; 234 sc2.GetAddressRange(eSymbolContextEverything, 0, false, range); 235 236 Address address = range.GetBaseAddress(); 237 238 Address addr; 239 if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target), 240 addr)) { 241 LineEntry line_entry; 242 addr.CalculateSymbolContextLineEntry(line_entry); 243 244 if (first_template_parameter.contains("$_") || 245 (symbol != nullptr && 246 symbol->GetName().GetStringRef().contains("__invoke"))) { 247 // Case 1 and 2 248 optional_info.callable_case = LibCppStdFunctionCallableCase::Lambda; 249 } else { 250 // Case 3 251 optional_info.callable_case = 252 LibCppStdFunctionCallableCase::CallableObject; 253 } 254 255 optional_info.callable_symbol = *symbol; 256 optional_info.callable_line_entry = line_entry; 257 optional_info.callable_address = addr; 258 return optional_info; 259 } 260 } 261 262 // Case 4 or 5 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; 268 269 return optional_info; 270 } 271 272 return optional_info; 273 } 274 275 lldb::ThreadPlanSP 276 CPPLanguageRuntime::GetStepThroughTrampolinePlan(Thread &thread, 277 bool stop_others) { 278 ThreadPlanSP ret_plan_sp; 279 280 lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); 281 282 TargetSP target_sp(thread.CalculateTarget()); 283 284 if (target_sp->GetSectionLoadList().IsEmpty()) 285 return ret_plan_sp; 286 287 Address pc_addr_resolved; 288 SymbolContext sc; 289 Symbol *symbol; 290 291 if (!target_sp->GetSectionLoadList().ResolveLoadAddress(curr_pc, 292 pc_addr_resolved)) 293 return ret_plan_sp; 294 295 target_sp->GetImages().ResolveSymbolContextForAddress( 296 pc_addr_resolved, eSymbolContextEverything, sc); 297 symbol = sc.symbol; 298 299 if (symbol == nullptr) 300 return ret_plan_sp; 301 302 llvm::StringRef function_name(symbol->GetName().GetCString()); 303 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. 309 // 310 bool found_expected_start_string = 311 function_name.startswith("std::__1::function<"); 312 313 if (!found_expected_start_string) 314 return ret_plan_sp; 315 316 AddressRange range_of_curr_func; 317 sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func); 318 319 StackFrameSP frame = thread.GetStackFrameAtIndex(0); 320 321 if (frame) { 322 ValueObjectSP value_sp = frame->FindVariable(ConstString("this")); 323 324 CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info = 325 FindLibCppStdFunctionCallableInfo(value_sp); 326 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)); 333 return ret_plan_sp; 334 } else { 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, 339 sc, eOnlyThisThread, 340 eLazyBoolYes, eLazyBoolYes)); 341 return ret_plan_sp; 342 } 343 } 344 345 return ret_plan_sp; 346 } 347