1 //===-- LLVMUserExpression.cpp ----------------------------------*- C++ -*-===// 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 10 #include "lldb/Expression/LLVMUserExpression.h" 11 #include "lldb/Core/Module.h" 12 #include "lldb/Core/StreamFile.h" 13 #include "lldb/Core/ValueObjectConstResult.h" 14 #include "lldb/Expression/DiagnosticManager.h" 15 #include "lldb/Expression/ExpressionSourceCode.h" 16 #include "lldb/Expression/IRExecutionUnit.h" 17 #include "lldb/Expression/IRInterpreter.h" 18 #include "lldb/Expression/Materializer.h" 19 #include "lldb/Host/HostInfo.h" 20 #include "lldb/Symbol/Block.h" 21 #include "lldb/Symbol/ClangASTContext.h" 22 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 23 #include "lldb/Symbol/Function.h" 24 #include "lldb/Symbol/ObjectFile.h" 25 #include "lldb/Symbol/SymbolVendor.h" 26 #include "lldb/Symbol/Type.h" 27 #include "lldb/Symbol/VariableList.h" 28 #include "lldb/Target/ExecutionContext.h" 29 #include "lldb/Target/Process.h" 30 #include "lldb/Target/StackFrame.h" 31 #include "lldb/Target/Target.h" 32 #include "lldb/Target/ThreadPlan.h" 33 #include "lldb/Target/ThreadPlanCallUserExpression.h" 34 #include "lldb/Utility/ConstString.h" 35 #include "lldb/Utility/Log.h" 36 #include "lldb/Utility/StreamString.h" 37 38 using namespace lldb_private; 39 40 LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope, 41 llvm::StringRef expr, 42 llvm::StringRef prefix, 43 lldb::LanguageType language, 44 ResultType desired_type, 45 const EvaluateExpressionOptions &options) 46 : UserExpression(exe_scope, expr, prefix, language, desired_type, options), 47 m_stack_frame_bottom(LLDB_INVALID_ADDRESS), 48 m_stack_frame_top(LLDB_INVALID_ADDRESS), m_allow_cxx(false), 49 m_allow_objc(false), m_transformed_text(), m_execution_unit_sp(), 50 m_materializer_up(), m_jit_module_wp(), m_enforce_valid_object(true), 51 m_in_cplusplus_method(false), m_in_objectivec_method(false), 52 m_in_static_method(false), m_needs_object_ptr(false), m_target(NULL), 53 m_can_interpret(false), m_materialized_address(LLDB_INVALID_ADDRESS) {} 54 55 LLVMUserExpression::~LLVMUserExpression() { 56 if (m_target) { 57 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock()); 58 if (jit_module_sp) 59 m_target->GetImages().Remove(jit_module_sp); 60 } 61 } 62 63 lldb::ExpressionResults 64 LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager, 65 ExecutionContext &exe_ctx, 66 const EvaluateExpressionOptions &options, 67 lldb::UserExpressionSP &shared_ptr_to_me, 68 lldb::ExpressionVariableSP &result) { 69 // The expression log is quite verbose, and if you're just tracking the 70 // execution of the expression, it's quite convenient to have these logs come 71 // out with the STEP log as well. 72 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS | 73 LIBLLDB_LOG_STEP)); 74 75 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) { 76 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS; 77 78 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx, 79 struct_address)) { 80 diagnostic_manager.Printf( 81 eDiagnosticSeverityError, 82 "errored out in %s, couldn't PrepareToExecuteJITExpression", 83 __FUNCTION__); 84 return lldb::eExpressionSetupError; 85 } 86 87 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS; 88 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS; 89 90 if (m_can_interpret) { 91 llvm::Module *module = m_execution_unit_sp->GetModule(); 92 llvm::Function *function = m_execution_unit_sp->GetFunction(); 93 94 if (!module || !function) { 95 diagnostic_manager.PutString( 96 eDiagnosticSeverityError, 97 "supposed to interpret, but nothing is there"); 98 return lldb::eExpressionSetupError; 99 } 100 101 Status interpreter_error; 102 103 std::vector<lldb::addr_t> args; 104 105 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) { 106 diagnostic_manager.Printf(eDiagnosticSeverityError, 107 "errored out in %s, couldn't AddArguments", 108 __FUNCTION__); 109 return lldb::eExpressionSetupError; 110 } 111 112 function_stack_bottom = m_stack_frame_bottom; 113 function_stack_top = m_stack_frame_top; 114 115 IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp, 116 interpreter_error, function_stack_bottom, 117 function_stack_top, exe_ctx); 118 119 if (!interpreter_error.Success()) { 120 diagnostic_manager.Printf(eDiagnosticSeverityError, 121 "supposed to interpret, but failed: %s", 122 interpreter_error.AsCString()); 123 return lldb::eExpressionDiscarded; 124 } 125 } else { 126 if (!exe_ctx.HasThreadScope()) { 127 diagnostic_manager.Printf(eDiagnosticSeverityError, 128 "%s called with no thread selected", 129 __FUNCTION__); 130 return lldb::eExpressionSetupError; 131 } 132 133 Address wrapper_address(m_jit_start_addr); 134 135 std::vector<lldb::addr_t> args; 136 137 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) { 138 diagnostic_manager.Printf(eDiagnosticSeverityError, 139 "errored out in %s, couldn't AddArguments", 140 __FUNCTION__); 141 return lldb::eExpressionSetupError; 142 } 143 144 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression( 145 exe_ctx.GetThreadRef(), wrapper_address, args, options, 146 shared_ptr_to_me)); 147 148 StreamString ss; 149 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) { 150 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString()); 151 return lldb::eExpressionSetupError; 152 } 153 154 ThreadPlanCallUserExpression *user_expression_plan = 155 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get()); 156 157 lldb::addr_t function_stack_pointer = 158 user_expression_plan->GetFunctionStackPointer(); 159 160 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize(); 161 function_stack_top = function_stack_pointer; 162 163 if (log) 164 log->Printf( 165 "-- [UserExpression::Execute] Execution of expression begins --"); 166 167 if (exe_ctx.GetProcessPtr()) 168 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true); 169 170 lldb::ExpressionResults execution_result = 171 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, 172 diagnostic_manager); 173 174 if (exe_ctx.GetProcessPtr()) 175 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false); 176 177 if (log) 178 log->Printf("-- [UserExpression::Execute] Execution of expression " 179 "completed --"); 180 181 if (execution_result == lldb::eExpressionInterrupted || 182 execution_result == lldb::eExpressionHitBreakpoint) { 183 const char *error_desc = NULL; 184 185 if (call_plan_sp) { 186 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo(); 187 if (real_stop_info_sp) 188 error_desc = real_stop_info_sp->GetDescription(); 189 } 190 if (error_desc) 191 diagnostic_manager.Printf(eDiagnosticSeverityError, 192 "Execution was interrupted, reason: %s.", 193 error_desc); 194 else 195 diagnostic_manager.PutString(eDiagnosticSeverityError, 196 "Execution was interrupted."); 197 198 if ((execution_result == lldb::eExpressionInterrupted && 199 options.DoesUnwindOnError()) || 200 (execution_result == lldb::eExpressionHitBreakpoint && 201 options.DoesIgnoreBreakpoints())) 202 diagnostic_manager.AppendMessageToDiagnostic( 203 "The process has been returned to the state before expression " 204 "evaluation."); 205 else { 206 if (execution_result == lldb::eExpressionHitBreakpoint) 207 user_expression_plan->TransferExpressionOwnership(); 208 diagnostic_manager.AppendMessageToDiagnostic( 209 "The process has been left at the point where it was " 210 "interrupted, " 211 "use \"thread return -x\" to return to the state before " 212 "expression evaluation."); 213 } 214 215 return execution_result; 216 } else if (execution_result == lldb::eExpressionStoppedForDebug) { 217 diagnostic_manager.PutString( 218 eDiagnosticSeverityRemark, 219 "Execution was halted at the first instruction of the expression " 220 "function because \"debug\" was requested.\n" 221 "Use \"thread return -x\" to return to the state before expression " 222 "evaluation."); 223 return execution_result; 224 } else if (execution_result != lldb::eExpressionCompleted) { 225 diagnostic_manager.Printf( 226 eDiagnosticSeverityError, 227 "Couldn't execute function; result was %s", 228 Process::ExecutionResultAsCString(execution_result)); 229 return execution_result; 230 } 231 } 232 233 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result, 234 function_stack_bottom, function_stack_top)) { 235 return lldb::eExpressionCompleted; 236 } else { 237 return lldb::eExpressionResultUnavailable; 238 } 239 } else { 240 diagnostic_manager.PutString( 241 eDiagnosticSeverityError, 242 "Expression can't be run, because there is no JIT compiled function"); 243 return lldb::eExpressionSetupError; 244 } 245 } 246 247 bool LLVMUserExpression::FinalizeJITExecution( 248 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, 249 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom, 250 lldb::addr_t function_stack_top) { 251 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 252 253 if (log) 254 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing " 255 "after execution --"); 256 257 if (!m_dematerializer_sp) { 258 diagnostic_manager.Printf(eDiagnosticSeverityError, 259 "Couldn't apply expression side effects : no " 260 "dematerializer is present"); 261 return false; 262 } 263 264 Status dematerialize_error; 265 266 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom, 267 function_stack_top); 268 269 if (!dematerialize_error.Success()) { 270 diagnostic_manager.Printf(eDiagnosticSeverityError, 271 "Couldn't apply expression side effects : %s", 272 dematerialize_error.AsCString("unknown error")); 273 return false; 274 } 275 276 result = 277 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope()); 278 279 if (result) 280 result->TransferAddress(); 281 282 m_dematerializer_sp.reset(); 283 284 return true; 285 } 286 287 bool LLVMUserExpression::PrepareToExecuteJITExpression( 288 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, 289 lldb::addr_t &struct_address) { 290 lldb::TargetSP target; 291 lldb::ProcessSP process; 292 lldb::StackFrameSP frame; 293 294 if (!LockAndCheckContext(exe_ctx, target, process, frame)) { 295 diagnostic_manager.PutString( 296 eDiagnosticSeverityError, 297 "The context has changed before we could JIT the expression!"); 298 return false; 299 } 300 301 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) { 302 if (m_materialized_address == LLDB_INVALID_ADDRESS) { 303 Status alloc_error; 304 305 IRMemoryMap::AllocationPolicy policy = 306 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly 307 : IRMemoryMap::eAllocationPolicyMirror; 308 309 const bool zero_memory = false; 310 311 m_materialized_address = m_execution_unit_sp->Malloc( 312 m_materializer_up->GetStructByteSize(), 313 m_materializer_up->GetStructAlignment(), 314 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy, 315 zero_memory, alloc_error); 316 317 if (!alloc_error.Success()) { 318 diagnostic_manager.Printf( 319 eDiagnosticSeverityError, 320 "Couldn't allocate space for materialized struct: %s", 321 alloc_error.AsCString()); 322 return false; 323 } 324 } 325 326 struct_address = m_materialized_address; 327 328 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) { 329 Status alloc_error; 330 331 const size_t stack_frame_size = 512 * 1024; 332 333 const bool zero_memory = false; 334 335 m_stack_frame_bottom = m_execution_unit_sp->Malloc( 336 stack_frame_size, 8, 337 lldb::ePermissionsReadable | lldb::ePermissionsWritable, 338 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error); 339 340 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size; 341 342 if (!alloc_error.Success()) { 343 diagnostic_manager.Printf( 344 eDiagnosticSeverityError, 345 "Couldn't allocate space for the stack frame: %s", 346 alloc_error.AsCString()); 347 return false; 348 } 349 } 350 351 Status materialize_error; 352 353 m_dematerializer_sp = m_materializer_up->Materialize( 354 frame, *m_execution_unit_sp, struct_address, materialize_error); 355 356 if (!materialize_error.Success()) { 357 diagnostic_manager.Printf(eDiagnosticSeverityError, 358 "Couldn't materialize: %s", 359 materialize_error.AsCString()); 360 return false; 361 } 362 } 363 return true; 364 } 365 366 lldb::ModuleSP LLVMUserExpression::GetJITModule() { 367 if (m_execution_unit_sp) 368 return m_execution_unit_sp->GetJITModule(); 369 return lldb::ModuleSP(); 370 } 371