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