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