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