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