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