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