1 //===-- ThreadPlanCallFunction.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 #include "lldb/Target/ThreadPlanCallFunction.h" 11 #include "lldb/Breakpoint/Breakpoint.h" 12 #include "lldb/Breakpoint/BreakpointLocation.h" 13 #include "lldb/Core/Address.h" 14 #include "lldb/Core/DumpRegisterValue.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Symbol/ObjectFile.h" 17 #include "lldb/Target/ABI.h" 18 #include "lldb/Target/LanguageRuntime.h" 19 #include "lldb/Target/Process.h" 20 #include "lldb/Target/RegisterContext.h" 21 #include "lldb/Target/StopInfo.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Target/Thread.h" 24 #include "lldb/Target/ThreadPlanRunToAddress.h" 25 #include "lldb/Utility/Log.h" 26 #include "lldb/Utility/Stream.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 //---------------------------------------------------------------------- 32 // ThreadPlanCallFunction: Plan to call a single function 33 //---------------------------------------------------------------------- 34 bool ThreadPlanCallFunction::ConstructorSetup( 35 Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr, 36 lldb::addr_t &function_load_addr) { 37 SetIsMasterPlan(true); 38 SetOkayToDiscard(false); 39 SetPrivate(true); 40 41 ProcessSP process_sp(thread.GetProcess()); 42 if (!process_sp) 43 return false; 44 45 abi = process_sp->GetABI().get(); 46 47 if (!abi) 48 return false; 49 50 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP)); 51 52 SetBreakpoints(); 53 54 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize(); 55 // If we can't read memory at the point of the process where we are planning 56 // to put our function, we're not going to get any further... 57 Status error; 58 process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error); 59 if (!error.Success()) { 60 m_constructor_errors.Printf( 61 "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", 62 m_function_sp); 63 if (log) 64 log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this), 65 m_constructor_errors.GetData()); 66 return false; 67 } 68 69 Module *exe_module = GetTarget().GetExecutableModulePointer(); 70 71 if (exe_module == nullptr) { 72 m_constructor_errors.Printf( 73 "Can't execute code without an executable module."); 74 if (log) 75 log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this), 76 m_constructor_errors.GetData()); 77 return false; 78 } else { 79 ObjectFile *objectFile = exe_module->GetObjectFile(); 80 if (!objectFile) { 81 m_constructor_errors.Printf( 82 "Could not find object file for module \"%s\".", 83 exe_module->GetFileSpec().GetFilename().AsCString()); 84 85 if (log) 86 log->Printf("ThreadPlanCallFunction(%p): %s.", 87 static_cast<void *>(this), m_constructor_errors.GetData()); 88 return false; 89 } 90 91 m_start_addr = objectFile->GetEntryPointAddress(); 92 if (!m_start_addr.IsValid()) { 93 m_constructor_errors.Printf( 94 "Could not find entry point address for executable module \"%s\".", 95 exe_module->GetFileSpec().GetFilename().AsCString()); 96 if (log) 97 log->Printf("ThreadPlanCallFunction(%p): %s.", 98 static_cast<void *>(this), m_constructor_errors.GetData()); 99 return false; 100 } 101 } 102 103 start_load_addr = m_start_addr.GetLoadAddress(&GetTarget()); 104 105 // Checkpoint the thread state so we can restore it later. 106 if (log && log->GetVerbose()) 107 ReportRegisterState("About to checkpoint thread before function call. " 108 "Original register state was:"); 109 110 if (!thread.CheckpointThreadState(m_stored_thread_state)) { 111 m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to " 112 "checkpoint thread state."); 113 if (log) 114 log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this), 115 m_constructor_errors.GetData()); 116 return false; 117 } 118 function_load_addr = m_function_addr.GetLoadAddress(&GetTarget()); 119 120 return true; 121 } 122 123 ThreadPlanCallFunction::ThreadPlanCallFunction( 124 Thread &thread, const Address &function, const CompilerType &return_type, 125 llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options) 126 : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, 127 eVoteNoOpinion, eVoteNoOpinion), 128 m_valid(false), m_stop_other_threads(options.GetStopOthers()), 129 m_unwind_on_error(options.DoesUnwindOnError()), 130 m_ignore_breakpoints(options.DoesIgnoreBreakpoints()), 131 m_debug_execution(options.GetDebug()), 132 m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function), 133 m_function_sp(0), m_takedown_done(false), 134 m_should_clear_objc_exception_bp(false), 135 m_should_clear_cxx_exception_bp(false), 136 m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) { 137 lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS; 138 lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS; 139 ABI *abi = nullptr; 140 141 if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr)) 142 return; 143 144 if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr, 145 start_load_addr, args)) 146 return; 147 148 ReportRegisterState("Function call was set up. Register state was:"); 149 150 m_valid = true; 151 } 152 153 ThreadPlanCallFunction::ThreadPlanCallFunction( 154 Thread &thread, const Address &function, 155 const EvaluateExpressionOptions &options) 156 : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, 157 eVoteNoOpinion, eVoteNoOpinion), 158 m_valid(false), m_stop_other_threads(options.GetStopOthers()), 159 m_unwind_on_error(options.DoesUnwindOnError()), 160 m_ignore_breakpoints(options.DoesIgnoreBreakpoints()), 161 m_debug_execution(options.GetDebug()), 162 m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function), 163 m_function_sp(0), m_takedown_done(false), 164 m_should_clear_objc_exception_bp(false), 165 m_should_clear_cxx_exception_bp(false), 166 m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {} 167 168 ThreadPlanCallFunction::~ThreadPlanCallFunction() { 169 DoTakedown(PlanSucceeded()); 170 } 171 172 void ThreadPlanCallFunction::ReportRegisterState(const char *message) { 173 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 174 if (log && log->GetVerbose()) { 175 StreamString strm; 176 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); 177 178 log->PutCString(message); 179 180 RegisterValue reg_value; 181 182 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount(); 183 reg_idx < num_registers; ++reg_idx) { 184 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx); 185 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 186 DumpRegisterValue(reg_value, &strm, reg_info, true, false, 187 eFormatDefault); 188 strm.EOL(); 189 } 190 } 191 log->PutString(strm.GetString()); 192 } 193 } 194 195 void ThreadPlanCallFunction::DoTakedown(bool success) { 196 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP)); 197 198 if (!m_valid) { 199 // Don't call DoTakedown if we were never valid to begin with. 200 if (log) 201 log->Printf("ThreadPlanCallFunction(%p): Log called on " 202 "ThreadPlanCallFunction that was never valid.", 203 static_cast<void *>(this)); 204 return; 205 } 206 207 if (!m_takedown_done) { 208 if (success) { 209 SetReturnValue(); 210 } 211 if (log) 212 log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread " 213 "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", 214 static_cast<void *>(this), m_thread.GetID(), m_valid, 215 IsPlanComplete()); 216 m_takedown_done = true; 217 m_stop_address = 218 m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC(); 219 m_real_stop_info_sp = GetPrivateStopInfo(); 220 if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) { 221 if (log) 222 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore " 223 "register state", 224 static_cast<void *>(this)); 225 } 226 SetPlanComplete(success); 227 ClearBreakpoints(); 228 if (log && log->GetVerbose()) 229 ReportRegisterState("Restoring thread state after function call. " 230 "Restored register state:"); 231 } else { 232 if (log) 233 log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for " 234 "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", 235 static_cast<void *>(this), m_thread.GetID(), m_valid, 236 IsPlanComplete()); 237 } 238 } 239 240 void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); } 241 242 void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) { 243 if (level == eDescriptionLevelBrief) { 244 s->Printf("Function call thread plan"); 245 } else { 246 TargetSP target_sp(m_thread.CalculateTarget()); 247 s->Printf("Thread plan to call 0x%" PRIx64, 248 m_function_addr.GetLoadAddress(target_sp.get())); 249 } 250 } 251 252 bool ThreadPlanCallFunction::ValidatePlan(Stream *error) { 253 if (!m_valid) { 254 if (error) { 255 if (m_constructor_errors.GetSize() > 0) 256 error->PutCString(m_constructor_errors.GetString()); 257 else 258 error->PutCString("Unknown error"); 259 } 260 return false; 261 } 262 263 return true; 264 } 265 266 Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) { 267 if (m_takedown_done || IsPlanComplete()) 268 return eVoteYes; 269 else 270 return ThreadPlan::ShouldReportStop(event_ptr); 271 } 272 273 bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) { 274 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | 275 LIBLLDB_LOG_PROCESS)); 276 m_real_stop_info_sp = GetPrivateStopInfo(); 277 278 // If our subplan knows why we stopped, even if it's done (which would 279 // forward the question to us) we answer yes. 280 if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) { 281 SetPlanComplete(); 282 return true; 283 } 284 285 // Check if the breakpoint is one of ours. 286 287 StopReason stop_reason; 288 if (!m_real_stop_info_sp) 289 stop_reason = eStopReasonNone; 290 else 291 stop_reason = m_real_stop_info_sp->GetStopReason(); 292 if (log) 293 log->Printf( 294 "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", 295 Thread::StopReasonAsCString(stop_reason)); 296 297 if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop()) 298 return true; 299 300 // One more quirk here. If this event was from Halt interrupting the target, 301 // then we should not consider ourselves complete. Return true to 302 // acknowledge the stop. 303 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) { 304 if (log) 305 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an " 306 "Interrupt, returning true."); 307 return true; 308 } 309 // We control breakpoints separately from other "stop reasons." So first, 310 // check the case where we stopped for an internal breakpoint, in that case, 311 // continue on. If it is not an internal breakpoint, consult 312 // m_ignore_breakpoints. 313 314 if (stop_reason == eStopReasonBreakpoint) { 315 ProcessSP process_sp(m_thread.CalculateProcess()); 316 uint64_t break_site_id = m_real_stop_info_sp->GetValue(); 317 BreakpointSiteSP bp_site_sp; 318 if (process_sp) 319 bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id); 320 if (bp_site_sp) { 321 uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 322 bool is_internal = true; 323 for (uint32_t i = 0; i < num_owners; i++) { 324 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 325 if (log) 326 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit " 327 "breakpoint %d while calling function", 328 bp.GetID()); 329 330 if (!bp.IsInternal()) { 331 is_internal = false; 332 break; 333 } 334 } 335 if (is_internal) { 336 if (log) 337 log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an " 338 "internal breakpoint, not stopping."); 339 return false; 340 } 341 } 342 343 if (m_ignore_breakpoints) { 344 if (log) 345 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring " 346 "breakpoints, overriding breakpoint stop info ShouldStop, " 347 "returning true"); 348 m_real_stop_info_sp->OverrideShouldStop(false); 349 return true; 350 } else { 351 if (log) 352 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not " 353 "ignoring breakpoints, overriding breakpoint stop info " 354 "ShouldStop, returning true"); 355 m_real_stop_info_sp->OverrideShouldStop(true); 356 return false; 357 } 358 } else if (!m_unwind_on_error) { 359 // If we don't want to discard this plan, than any stop we don't understand 360 // should be propagated up the stack. 361 return false; 362 } else { 363 // If the subplan is running, any crashes are attributable to us. If we 364 // want to discard the plan, then we say we explain the stop but if we are 365 // going to be discarded, let whoever is above us explain the stop. But 366 // don't discard the plan if the stop would restart itself (for instance if 367 // it is a signal that is set not to stop. Check that here first. We just 368 // say we explain the stop but aren't done and everything will continue on 369 // from there. 370 371 if (m_real_stop_info_sp && 372 m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) { 373 SetPlanComplete(false); 374 return m_subplan_sp ? m_unwind_on_error : false; 375 } else 376 return true; 377 } 378 } 379 380 bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) { 381 // We do some computation in DoPlanExplainsStop that may or may not set the 382 // plan as complete. We need to do that here to make sure our state is 383 // correct. 384 DoPlanExplainsStop(event_ptr); 385 386 if (IsPlanComplete()) { 387 ReportRegisterState("Function completed. Register state was:"); 388 return true; 389 } else { 390 return false; 391 } 392 } 393 394 bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; } 395 396 StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; } 397 398 void ThreadPlanCallFunction::DidPush() { 399 //#define SINGLE_STEP_EXPRESSIONS 400 401 // Now set the thread state to "no reason" so we don't run with whatever 402 // signal was outstanding... Wait till the plan is pushed so we aren't 403 // changing the stop info till we're about to run. 404 405 GetThread().SetStopInfoToNothing(); 406 407 #ifndef SINGLE_STEP_EXPRESSIONS 408 m_subplan_sp.reset( 409 new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads)); 410 411 m_thread.QueueThreadPlan(m_subplan_sp, false); 412 m_subplan_sp->SetPrivate(true); 413 #endif 414 } 415 416 bool ThreadPlanCallFunction::WillStop() { return true; } 417 418 bool ThreadPlanCallFunction::MischiefManaged() { 419 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 420 421 if (IsPlanComplete()) { 422 if (log) 423 log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.", 424 static_cast<void *>(this)); 425 426 ThreadPlan::MischiefManaged(); 427 return true; 428 } else { 429 return false; 430 } 431 } 432 433 void ThreadPlanCallFunction::SetBreakpoints() { 434 ProcessSP process_sp(m_thread.CalculateProcess()); 435 if (m_trap_exceptions && process_sp) { 436 m_cxx_language_runtime = 437 process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus); 438 m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC); 439 440 if (m_cxx_language_runtime) { 441 m_should_clear_cxx_exception_bp = 442 !m_cxx_language_runtime->ExceptionBreakpointsAreSet(); 443 m_cxx_language_runtime->SetExceptionBreakpoints(); 444 } 445 if (m_objc_language_runtime) { 446 m_should_clear_objc_exception_bp = 447 !m_objc_language_runtime->ExceptionBreakpointsAreSet(); 448 m_objc_language_runtime->SetExceptionBreakpoints(); 449 } 450 } 451 } 452 453 void ThreadPlanCallFunction::ClearBreakpoints() { 454 if (m_trap_exceptions) { 455 if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp) 456 m_cxx_language_runtime->ClearExceptionBreakpoints(); 457 if (m_objc_language_runtime && m_should_clear_objc_exception_bp) 458 m_objc_language_runtime->ClearExceptionBreakpoints(); 459 } 460 } 461 462 bool ThreadPlanCallFunction::BreakpointsExplainStop() { 463 StopInfoSP stop_info_sp = GetPrivateStopInfo(); 464 465 if (m_trap_exceptions) { 466 if ((m_cxx_language_runtime && 467 m_cxx_language_runtime->ExceptionBreakpointsExplainStop( 468 stop_info_sp)) || 469 (m_objc_language_runtime && 470 m_objc_language_runtime->ExceptionBreakpointsExplainStop( 471 stop_info_sp))) { 472 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP)); 473 if (log) 474 log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an " 475 "exception breakpoint, setting plan complete."); 476 477 SetPlanComplete(false); 478 479 // If the user has set the ObjC language breakpoint, it would normally 480 // get priority over our internal catcher breakpoint, but in this case we 481 // can't let that happen, so force the ShouldStop here. 482 stop_info_sp->OverrideShouldStop(true); 483 return true; 484 } 485 } 486 487 return false; 488 } 489 490 void ThreadPlanCallFunction::SetStopOthers(bool new_value) { 491 m_subplan_sp->SetStopOthers(new_value); 492 } 493 494 bool ThreadPlanCallFunction::RestoreThreadState() { 495 return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state); 496 } 497 498 void ThreadPlanCallFunction::SetReturnValue() { 499 ProcessSP process_sp(m_thread.GetProcess()); 500 const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr; 501 if (abi && m_return_type.IsValid()) { 502 const bool persistent = false; 503 m_return_valobj_sp = 504 abi->GetReturnValueObject(m_thread, m_return_type, persistent); 505 } 506 } 507