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