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