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