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