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