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