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