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