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 lldb::addr_t arg, 41 bool stop_other_threads, 42 bool discard_on_error, 43 lldb::addr_t *this_arg) : 44 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion), 45 m_valid (false), 46 m_stop_other_threads (stop_other_threads), 47 m_arg_addr (arg), 48 m_args (NULL), 49 m_process (thread.GetProcess()), 50 m_thread (thread) 51 { 52 SetOkayToDiscard (discard_on_error); 53 54 Process& process = thread.GetProcess(); 55 Target& target = process.GetTarget(); 56 const ABI *abi = process.GetABI(); 57 58 if (!abi) 59 return; 60 61 SetBreakpoints(); 62 63 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize(); 64 65 SymbolContextList contexts; 66 SymbolContext context; 67 ModuleSP executableModuleSP (target.GetExecutableModule()); 68 69 if (!executableModuleSP || 70 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts)) 71 return; 72 73 contexts.GetContextAtIndex(0, context); 74 75 m_start_addr = context.symbol->GetValue(); 76 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target); 77 78 if (!thread.SaveFrameZeroState(m_register_backup)) 79 return; 80 81 m_function_addr = function; 82 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target); 83 84 if (!abi->PrepareTrivialCall(thread, 85 spBelowRedZone, 86 FunctionLoadAddr, 87 StartLoadAddr, 88 m_arg_addr, 89 this_arg)) 90 return; 91 92 m_valid = true; 93 } 94 95 ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread, 96 Address &function, 97 ValueList &args, 98 bool stop_other_threads, 99 bool discard_on_error) : 100 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion), 101 m_valid (false), 102 m_stop_other_threads (stop_other_threads), 103 m_arg_addr (0), 104 m_args (&args), 105 m_process (thread.GetProcess()), 106 m_thread (thread) 107 { 108 109 SetOkayToDiscard (discard_on_error); 110 111 Process& process = thread.GetProcess(); 112 Target& target = process.GetTarget(); 113 const ABI *abi = process.GetABI(); 114 115 if(!abi) 116 return; 117 118 SetBreakpoints(); 119 120 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize(); 121 122 SymbolContextList contexts; 123 SymbolContext context; 124 ModuleSP executableModuleSP (target.GetExecutableModule()); 125 126 if (!executableModuleSP || 127 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts)) 128 return; 129 130 contexts.GetContextAtIndex(0, context); 131 132 m_start_addr = context.symbol->GetValue(); 133 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target); 134 135 if(!thread.SaveFrameZeroState(m_register_backup)) 136 return; 137 138 m_function_addr = function; 139 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target); 140 141 if (!abi->PrepareNormalCall(thread, 142 spBelowRedZone, 143 FunctionLoadAddr, 144 StartLoadAddr, 145 *m_args)) 146 return; 147 148 m_valid = true; 149 } 150 151 ThreadPlanCallFunction::~ThreadPlanCallFunction () 152 { 153 } 154 155 void 156 ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level) 157 { 158 if (level == lldb::eDescriptionLevelBrief) 159 { 160 s->Printf("Function call thread plan"); 161 } 162 else 163 { 164 if (m_args) 165 s->Printf("Thread plan to call 0x%llx with parsed arguments", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr); 166 else 167 s->Printf("Thread plan to call 0x%llx void * argument at: 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr); 168 } 169 } 170 171 bool 172 ThreadPlanCallFunction::ValidatePlan (Stream *error) 173 { 174 if (!m_valid) 175 return false; 176 177 return true; 178 } 179 180 bool 181 ThreadPlanCallFunction::PlanExplainsStop () 182 { 183 // If our subplan knows why we stopped, even if it's done (which would forward the question to us) 184 // we answer yes. 185 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop()) 186 return true; 187 188 // Check if the breakpoint is one of ours. 189 190 if (BreakpointsExplainStop()) 191 return true; 192 193 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack. 194 if (!OkayToDiscard()) 195 return false; 196 197 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on. 198 // If it is not an internal breakpoint, consult OkayToDiscard. 199 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason(); 200 201 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) 202 { 203 uint64_t break_site_id = stop_info_sp->GetValue(); 204 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id); 205 if (bp_site_sp) 206 { 207 uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 208 bool is_internal = true; 209 for (uint32_t i = 0; i < num_owners; i++) 210 { 211 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 212 213 if (!bp.IsInternal()) 214 { 215 is_internal = false; 216 break; 217 } 218 } 219 if (is_internal) 220 return false; 221 } 222 223 return OkayToDiscard(); 224 } 225 else 226 { 227 // If the subplan is running, any crashes are attributable to us. 228 return (m_subplan_sp.get() != NULL); 229 } 230 } 231 232 bool 233 ThreadPlanCallFunction::ShouldStop (Event *event_ptr) 234 { 235 if (PlanExplainsStop()) 236 { 237 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); 238 239 if (log) 240 { 241 RegisterContext *reg_ctx = m_thread.GetRegisterContext(); 242 243 log->PutCString("Function completed. Register state was:"); 244 245 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount(); 246 register_index < num_registers; 247 ++register_index) 248 { 249 const char *register_name = reg_ctx->GetRegisterName(register_index); 250 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS); 251 252 log->Printf(" %s = 0x%llx", register_name, register_value); 253 } 254 } 255 256 m_thread.RestoreSaveFrameZero(m_register_backup); 257 m_thread.ClearStackFrames(); 258 SetPlanComplete(); 259 260 ClearBreakpoints(); 261 return true; 262 } 263 else 264 { 265 return false; 266 } 267 } 268 269 bool 270 ThreadPlanCallFunction::StopOthers () 271 { 272 return m_stop_other_threads; 273 } 274 275 void 276 ThreadPlanCallFunction::SetStopOthers (bool new_value) 277 { 278 if (m_subplan_sp) 279 { 280 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get()); 281 address_plan->SetStopOthers(new_value); 282 } 283 m_stop_other_threads = new_value; 284 } 285 286 StateType 287 ThreadPlanCallFunction::RunState () 288 { 289 return eStateRunning; 290 } 291 292 void 293 ThreadPlanCallFunction::DidPush () 294 { 295 //#define SINGLE_STEP_EXPRESSIONS 296 297 #ifndef SINGLE_STEP_EXPRESSIONS 298 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads)); 299 300 m_thread.QueueThreadPlan(m_subplan_sp, false); 301 #endif 302 } 303 304 bool 305 ThreadPlanCallFunction::WillStop () 306 { 307 return true; 308 } 309 310 bool 311 ThreadPlanCallFunction::MischiefManaged () 312 { 313 if (IsPlanComplete()) 314 { 315 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); 316 317 if (log) 318 log->Printf("Completed call function plan."); 319 320 ThreadPlan::MischiefManaged (); 321 return true; 322 } 323 else 324 { 325 return false; 326 } 327 } 328 329 void 330 ThreadPlanCallFunction::SetBreakpoints () 331 { 332 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus); 333 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC); 334 335 if (m_cxx_language_runtime) 336 m_cxx_language_runtime->SetExceptionBreakpoints(); 337 if (m_objc_language_runtime) 338 m_objc_language_runtime->SetExceptionBreakpoints(); 339 } 340 341 void 342 ThreadPlanCallFunction::ClearBreakpoints () 343 { 344 if (m_cxx_language_runtime) 345 m_cxx_language_runtime->ClearExceptionBreakpoints(); 346 if (m_objc_language_runtime) 347 m_objc_language_runtime->ClearExceptionBreakpoints(); 348 } 349 350 bool 351 ThreadPlanCallFunction::BreakpointsExplainStop() 352 { 353 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason(); 354 355 if (m_cxx_language_runtime && 356 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)) 357 return true; 358 359 if (m_objc_language_runtime && 360 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)) 361 return true; 362 363 return false; 364 } 365