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 bool 38 ThreadPlanCallFunction::ConstructorSetup (Thread &thread, 39 bool discard_on_error, 40 ABI *& abi, 41 lldb::addr_t &start_load_addr, 42 lldb::addr_t &function_load_addr) 43 { 44 // Call function thread plans need to be master plans so that they can potentially stay on the stack when 45 // a breakpoint is hit during the function call. 46 SetIsMasterPlan (true); 47 SetOkayToDiscard (discard_on_error); 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%llx.", 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 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding... 117 thread.SetStopInfoToNothing(); 118 119 function_load_addr = m_function_addr.GetLoadAddress (target_sp.get()); 120 121 return true; 122 } 123 124 ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread, 125 Address &function, 126 const ClangASTType &return_type, 127 addr_t arg, 128 bool stop_other_threads, 129 bool discard_on_error, 130 addr_t *this_arg, 131 addr_t *cmd_arg) : 132 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion), 133 m_valid (false), 134 m_stop_other_threads (stop_other_threads), 135 m_function_addr (function), 136 m_function_sp (NULL), 137 m_return_type (return_type), 138 m_takedown_done (false), 139 m_stop_address (LLDB_INVALID_ADDRESS) 140 { 141 lldb::addr_t start_load_addr; 142 ABI *abi; 143 lldb::addr_t function_load_addr; 144 if (!ConstructorSetup (thread, discard_on_error, 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 discard_on_error, 189 addr_t *arg1_ptr, 190 addr_t *arg2_ptr, 191 addr_t *arg3_ptr, 192 addr_t *arg4_ptr, 193 addr_t *arg5_ptr, 194 addr_t *arg6_ptr) : 195 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion), 196 m_valid (false), 197 m_stop_other_threads (stop_other_threads), 198 m_function_addr (function), 199 m_function_sp(NULL), 200 m_return_type (return_type), 201 m_takedown_done (false), 202 m_stop_address (LLDB_INVALID_ADDRESS) 203 { 204 lldb::addr_t start_load_addr; 205 ABI *abi; 206 lldb::addr_t function_load_addr; 207 if (!ConstructorSetup (thread, discard_on_error, abi, start_load_addr, function_load_addr)) 208 return; 209 210 if (!abi->PrepareTrivialCall (thread, 211 m_function_sp, 212 function_load_addr, 213 start_load_addr, 214 arg1_ptr, 215 arg2_ptr, 216 arg3_ptr, 217 arg4_ptr, 218 arg5_ptr, 219 arg6_ptr)) 220 { 221 return; 222 } 223 224 ReportRegisterState ("Function call was set up. Register state was:"); 225 226 m_valid = true; 227 } 228 229 ThreadPlanCallFunction::~ThreadPlanCallFunction () 230 { 231 DoTakedown(); 232 } 233 234 void 235 ThreadPlanCallFunction::ReportRegisterState (const char *message) 236 { 237 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE)); 238 if (log) 239 { 240 StreamString strm; 241 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); 242 243 log->PutCString(message); 244 245 RegisterValue reg_value; 246 247 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount(); 248 reg_idx < num_registers; 249 ++reg_idx) 250 { 251 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx); 252 if (reg_ctx->ReadRegister(reg_info, reg_value)) 253 { 254 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault); 255 strm.EOL(); 256 } 257 } 258 log->PutCString(strm.GetData()); 259 } 260 } 261 262 void 263 ThreadPlanCallFunction::DoTakedown () 264 { 265 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP)); 266 267 if (!m_valid) 268 { 269 //Don't call DoTakedown if we were never valid to begin with. 270 if (log) 271 log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.", this); 272 return; 273 } 274 275 if (!m_takedown_done) 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.4llx, 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.RestoreThreadStateFromCheckpoint(m_stored_thread_state); 291 SetPlanComplete(); 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.4llx, m_valid: %d complete: %d.\n", this, m_thread.GetID(), m_valid, IsPlanComplete()); 301 } 302 } 303 304 void 305 ThreadPlanCallFunction::WillPop () 306 { 307 DoTakedown(); 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%llx", 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 if (BreakpointsExplainStop()) 346 return true; 347 348 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack. 349 if (!OkayToDiscard()) 350 return false; 351 352 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on. 353 // If it is not an internal breakpoint, consult OkayToDiscard. 354 355 if (m_real_stop_info_sp && m_real_stop_info_sp->GetStopReason() == eStopReasonBreakpoint) 356 { 357 ProcessSP process_sp (m_thread.CalculateProcess()); 358 uint64_t break_site_id = m_real_stop_info_sp->GetValue(); 359 BreakpointSiteSP bp_site_sp; 360 if (process_sp) 361 bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id); 362 if (bp_site_sp) 363 { 364 uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 365 bool is_internal = true; 366 for (uint32_t i = 0; i < num_owners; i++) 367 { 368 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 369 370 if (!bp.IsInternal()) 371 { 372 is_internal = false; 373 break; 374 } 375 } 376 if (is_internal) 377 return false; 378 } 379 380 return OkayToDiscard(); 381 } 382 else 383 { 384 // If the subplan is running, any crashes are attributable to us. 385 // If we want to discard the plan, then we say we explain the stop 386 // but if we are going to be discarded, let whoever is above us 387 // explain the stop. 388 return ((m_subplan_sp.get() != NULL) && !OkayToDiscard()); 389 } 390 } 391 392 bool 393 ThreadPlanCallFunction::ShouldStop (Event *event_ptr) 394 { 395 if (PlanExplainsStop()) 396 { 397 ReportRegisterState ("Function completed. Register state was:"); 398 399 DoTakedown(); 400 401 return true; 402 } 403 else 404 { 405 return false; 406 } 407 } 408 409 bool 410 ThreadPlanCallFunction::StopOthers () 411 { 412 return m_stop_other_threads; 413 } 414 415 void 416 ThreadPlanCallFunction::SetStopOthers (bool new_value) 417 { 418 if (m_subplan_sp) 419 { 420 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get()); 421 address_plan->SetStopOthers(new_value); 422 } 423 m_stop_other_threads = new_value; 424 } 425 426 StateType 427 ThreadPlanCallFunction::GetPlanRunState () 428 { 429 return eStateRunning; 430 } 431 432 void 433 ThreadPlanCallFunction::DidPush () 434 { 435 //#define SINGLE_STEP_EXPRESSIONS 436 437 #ifndef SINGLE_STEP_EXPRESSIONS 438 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads)); 439 440 m_thread.QueueThreadPlan(m_subplan_sp, false); 441 m_subplan_sp->SetPrivate (true); 442 #endif 443 } 444 445 bool 446 ThreadPlanCallFunction::WillStop () 447 { 448 return true; 449 } 450 451 bool 452 ThreadPlanCallFunction::MischiefManaged () 453 { 454 if (IsPlanComplete()) 455 { 456 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 457 458 if (log) 459 log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.", this); 460 461 ThreadPlan::MischiefManaged (); 462 return true; 463 } 464 else 465 { 466 return false; 467 } 468 } 469 470 void 471 ThreadPlanCallFunction::SetBreakpoints () 472 { 473 ProcessSP process_sp (m_thread.CalculateProcess()); 474 if (process_sp) 475 { 476 m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus); 477 m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC); 478 479 if (m_cxx_language_runtime) 480 m_cxx_language_runtime->SetExceptionBreakpoints(); 481 if (m_objc_language_runtime) 482 m_objc_language_runtime->SetExceptionBreakpoints(); 483 } 484 } 485 486 void 487 ThreadPlanCallFunction::ClearBreakpoints () 488 { 489 if (m_cxx_language_runtime) 490 m_cxx_language_runtime->ClearExceptionBreakpoints(); 491 if (m_objc_language_runtime) 492 m_objc_language_runtime->ClearExceptionBreakpoints(); 493 } 494 495 bool 496 ThreadPlanCallFunction::BreakpointsExplainStop() 497 { 498 StopInfoSP stop_info_sp = GetPrivateStopReason(); 499 500 if (m_cxx_language_runtime && 501 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)) 502 return true; 503 504 if (m_objc_language_runtime && 505 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)) 506 return true; 507 508 return false; 509 } 510