Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 13 | // Project includes |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 | [diff] [blame] | 14 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jim Ingham | 40d871f | 2010-10-26 00:27:45 | [diff] [blame] | 15 | #include "lldb/Breakpoint/Breakpoint.h" |
| 16 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 17 | #include "lldb/Core/Address.h" |
Pavel Labath | e03334c | 2018-07-24 15:48:13 | [diff] [blame^] | 18 | #include "lldb/Core/DumpRegisterValue.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 | [diff] [blame] | 20 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 32abc6e | 2015-03-03 19:23:09 | [diff] [blame] | 21 | #include "lldb/Target/ABI.h" |
Sean Callanan | f211510 | 2010-11-03 22:19:38 | [diff] [blame] | 22 | #include "lldb/Target/LanguageRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
| 24 | #include "lldb/Target/RegisterContext.h" |
Jim Ingham | 40d871f | 2010-10-26 00:27:45 | [diff] [blame] | 25 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
| 27 | #include "lldb/Target/Thread.h" |
| 28 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 | [diff] [blame] | 29 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 | [diff] [blame] | 30 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
| 35 | //---------------------------------------------------------------------- |
| 36 | // ThreadPlanCallFunction: Plan to call a single function |
| 37 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 38 | bool ThreadPlanCallFunction::ConstructorSetup( |
| 39 | Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr, |
| 40 | lldb::addr_t &function_load_addr) { |
| 41 | SetIsMasterPlan(true); |
| 42 | SetOkayToDiscard(false); |
| 43 | SetPrivate(true); |
Jim Ingham | 0092c8e | 2012-04-13 20:38:13 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 45 | ProcessSP process_sp(thread.GetProcess()); |
| 46 | if (!process_sp) |
| 47 | return false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 49 | abi = process_sp->GetABI().get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 51 | if (!abi) |
| 52 | return false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 54 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 56 | SetBreakpoints(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 58 | m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize(); |
| 59 | // If we can't read memory at the point of the process where we are planning |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 60 | // to put our function, we're not going to get any further... |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 61 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 62 | process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error); |
| 63 | if (!error.Success()) { |
| 64 | m_constructor_errors.Printf( |
| 65 | "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", |
| 66 | m_function_sp); |
| 67 | if (log) |
| 68 | log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this), |
| 69 | m_constructor_errors.GetData()); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | Module *exe_module = GetTarget().GetExecutableModulePointer(); |
| 74 | |
| 75 | if (exe_module == nullptr) { |
| 76 | m_constructor_errors.Printf( |
| 77 | "Can't execute code without an executable module."); |
| 78 | if (log) |
| 79 | log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this), |
| 80 | m_constructor_errors.GetData()); |
| 81 | return false; |
| 82 | } else { |
| 83 | ObjectFile *objectFile = exe_module->GetObjectFile(); |
| 84 | if (!objectFile) { |
| 85 | m_constructor_errors.Printf( |
| 86 | "Could not find object file for module \"%s\".", |
| 87 | exe_module->GetFileSpec().GetFilename().AsCString()); |
| 88 | |
| 89 | if (log) |
| 90 | log->Printf("ThreadPlanCallFunction(%p): %s.", |
| 91 | static_cast<void *>(this), m_constructor_errors.GetData()); |
| 92 | return false; |
Jim Ingham | 0092c8e | 2012-04-13 20:38:13 | [diff] [blame] | 93 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 95 | m_start_addr = objectFile->GetEntryPointAddress(); |
| 96 | if (!m_start_addr.IsValid()) { |
| 97 | m_constructor_errors.Printf( |
| 98 | "Could not find entry point address for executable module \"%s\".", |
| 99 | exe_module->GetFileSpec().GetFilename().AsCString()); |
| 100 | if (log) |
| 101 | log->Printf("ThreadPlanCallFunction(%p): %s.", |
| 102 | static_cast<void *>(this), m_constructor_errors.GetData()); |
| 103 | return false; |
Jim Ingham | 0092c8e | 2012-04-13 20:38:13 | [diff] [blame] | 104 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 105 | } |
Jim Ingham | ea06f3b | 2013-03-28 00:04:05 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 107 | start_load_addr = m_start_addr.GetLoadAddress(&GetTarget()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 109 | // Checkpoint the thread state so we can restore it later. |
| 110 | if (log && log->GetVerbose()) |
| 111 | ReportRegisterState("About to checkpoint thread before function call. " |
| 112 | "Original register state was:"); |
| 113 | |
| 114 | if (!thread.CheckpointThreadState(m_stored_thread_state)) { |
| 115 | m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to " |
| 116 | "checkpoint thread state."); |
| 117 | if (log) |
| 118 | log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this), |
| 119 | m_constructor_errors.GetData()); |
| 120 | return false; |
| 121 | } |
| 122 | function_load_addr = m_function_addr.GetLoadAddress(&GetTarget()); |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | ThreadPlanCallFunction::ThreadPlanCallFunction( |
| 128 | Thread &thread, const Address &function, const CompilerType &return_type, |
| 129 | llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options) |
| 130 | : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, |
| 131 | eVoteNoOpinion, eVoteNoOpinion), |
| 132 | m_valid(false), m_stop_other_threads(options.GetStopOthers()), |
| 133 | m_unwind_on_error(options.DoesUnwindOnError()), |
| 134 | m_ignore_breakpoints(options.DoesIgnoreBreakpoints()), |
| 135 | m_debug_execution(options.GetDebug()), |
| 136 | m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function), |
| 137 | m_function_sp(0), m_takedown_done(false), |
| 138 | m_should_clear_objc_exception_bp(false), |
| 139 | m_should_clear_cxx_exception_bp(false), |
| 140 | m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) { |
| 141 | lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS; |
| 142 | lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS; |
| 143 | ABI *abi = nullptr; |
| 144 | |
| 145 | if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr)) |
| 146 | return; |
| 147 | |
| 148 | if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr, |
| 149 | start_load_addr, args)) |
| 150 | return; |
| 151 | |
| 152 | ReportRegisterState("Function call was set up. Register state was:"); |
| 153 | |
| 154 | m_valid = true; |
| 155 | } |
| 156 | |
| 157 | ThreadPlanCallFunction::ThreadPlanCallFunction( |
| 158 | Thread &thread, const Address &function, |
| 159 | const EvaluateExpressionOptions &options) |
| 160 | : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, |
| 161 | eVoteNoOpinion, eVoteNoOpinion), |
| 162 | m_valid(false), m_stop_other_threads(options.GetStopOthers()), |
| 163 | m_unwind_on_error(options.DoesUnwindOnError()), |
| 164 | m_ignore_breakpoints(options.DoesIgnoreBreakpoints()), |
| 165 | m_debug_execution(options.GetDebug()), |
| 166 | m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function), |
| 167 | m_function_sp(0), m_takedown_done(false), |
| 168 | m_should_clear_objc_exception_bp(false), |
| 169 | m_should_clear_cxx_exception_bp(false), |
| 170 | m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {} |
| 171 | |
| 172 | ThreadPlanCallFunction::~ThreadPlanCallFunction() { |
| 173 | DoTakedown(PlanSucceeded()); |
| 174 | } |
| 175 | |
| 176 | void ThreadPlanCallFunction::ReportRegisterState(const char *message) { |
Pavel Labath | 3b7e198 | 2017-02-05 00:44:54 | [diff] [blame] | 177 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 178 | if (log && log->GetVerbose()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 179 | StreamString strm; |
| 180 | RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); |
| 181 | |
| 182 | log->PutCString(message); |
| 183 | |
| 184 | RegisterValue reg_value; |
| 185 | |
| 186 | for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount(); |
| 187 | reg_idx < num_registers; ++reg_idx) { |
| 188 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx); |
| 189 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
Pavel Labath | e03334c | 2018-07-24 15:48:13 | [diff] [blame^] | 190 | DumpRegisterValue(reg_value, &strm, reg_info, true, false, |
| 191 | eFormatDefault); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 192 | strm.EOL(); |
| 193 | } |
Jim Ingham | 0092c8e | 2012-04-13 20:38:13 | [diff] [blame] | 194 | } |
Zachary Turner | c156427 | 2016-11-16 21:15:24 | [diff] [blame] | 195 | log->PutString(strm.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 196 | } |
| 197 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 199 | void ThreadPlanCallFunction::DoTakedown(bool success) { |
| 200 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 202 | if (!m_valid) { |
| 203 | // Don't call DoTakedown if we were never valid to begin with. |
| 204 | if (log) |
| 205 | log->Printf("ThreadPlanCallFunction(%p): Log called on " |
| 206 | "ThreadPlanCallFunction that was never valid.", |
| 207 | static_cast<void *>(this)); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | if (!m_takedown_done) { |
| 212 | if (success) { |
| 213 | SetReturnValue(); |
| 214 | } |
| 215 | if (log) |
| 216 | log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread " |
| 217 | "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", |
| 218 | static_cast<void *>(this), m_thread.GetID(), m_valid, |
| 219 | IsPlanComplete()); |
| 220 | m_takedown_done = true; |
| 221 | m_stop_address = |
| 222 | m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC(); |
| 223 | m_real_stop_info_sp = GetPrivateStopInfo(); |
| 224 | if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) { |
| 225 | if (log) |
| 226 | log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore " |
| 227 | "register state", |
| 228 | static_cast<void *>(this)); |
| 229 | } |
| 230 | SetPlanComplete(success); |
| 231 | ClearBreakpoints(); |
Jim Ingham | 0092c8e | 2012-04-13 20:38:13 | [diff] [blame] | 232 | if (log && log->GetVerbose()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 233 | ReportRegisterState("Restoring thread state after function call. " |
| 234 | "Restored register state:"); |
| 235 | } else { |
Sean Callanan | ece9649 | 2010-11-08 03:49:50 | [diff] [blame] | 236 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 237 | log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for " |
| 238 | "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", |
| 239 | static_cast<void *>(this), m_thread.GetID(), m_valid, |
| 240 | IsPlanComplete()); |
| 241 | } |
Sean Callanan | 10af7c4 | 2010-11-04 01:51:38 | [diff] [blame] | 242 | } |
| 243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 244 | void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 | [diff] [blame] | 245 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 246 | void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) { |
| 247 | if (level == eDescriptionLevelBrief) { |
| 248 | s->Printf("Function call thread plan"); |
| 249 | } else { |
| 250 | TargetSP target_sp(m_thread.CalculateTarget()); |
| 251 | s->Printf("Thread plan to call 0x%" PRIx64, |
| 252 | m_function_addr.GetLoadAddress(target_sp.get())); |
| 253 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 254 | } |
| 255 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 256 | bool ThreadPlanCallFunction::ValidatePlan(Stream *error) { |
| 257 | if (!m_valid) { |
| 258 | if (error) { |
| 259 | if (m_constructor_errors.GetSize() > 0) |
Zachary Turner | c156427 | 2016-11-16 21:15:24 | [diff] [blame] | 260 | error->PutCString(m_constructor_errors.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 261 | else |
| 262 | error->PutCString("Unknown error"); |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | return true; |
Jim Ingham | bda4e5eb | 2011-01-18 01:58:06 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 270 | Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) { |
| 271 | if (m_takedown_done || IsPlanComplete()) |
| 272 | return eVoteYes; |
| 273 | else |
| 274 | return ThreadPlan::ShouldReportStop(event_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 275 | } |
| 276 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 277 | bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) { |
| 278 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 279 | LIBLLDB_LOG_PROCESS)); |
| 280 | m_real_stop_info_sp = GetPrivateStopInfo(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 281 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 282 | // If our subplan knows why we stopped, even if it's done (which would |
| 283 | // forward the question to us) we answer yes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 284 | if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) { |
| 285 | SetPlanComplete(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 286 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 287 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 288 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 289 | // Check if the breakpoint is one of ours. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 | [diff] [blame] | 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 291 | StopReason stop_reason; |
| 292 | if (!m_real_stop_info_sp) |
| 293 | stop_reason = eStopReasonNone; |
| 294 | else |
| 295 | stop_reason = m_real_stop_info_sp->GetStopReason(); |
| 296 | if (log) |
| 297 | log->Printf( |
| 298 | "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", |
| 299 | Thread::StopReasonAsCString(stop_reason)); |
| 300 | |
| 301 | if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop()) |
| 302 | return true; |
| 303 | |
| 304 | // One more quirk here. If this event was from Halt interrupting the target, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 305 | // then we should not consider ourselves complete. Return true to |
| 306 | // acknowledge the stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 307 | if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 | [diff] [blame] | 308 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 309 | log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an " |
| 310 | "Interrupt, returning true."); |
| 311 | return true; |
| 312 | } |
| 313 | // We control breakpoints separately from other "stop reasons." So first, |
| 314 | // check the case where we stopped for an internal breakpoint, in that case, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 315 | // continue on. If it is not an internal breakpoint, consult |
| 316 | // m_ignore_breakpoints. |
Jim Ingham | 18de2fd | 2012-05-10 01:35:39 | [diff] [blame] | 317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 318 | if (stop_reason == eStopReasonBreakpoint) { |
| 319 | ProcessSP process_sp(m_thread.CalculateProcess()); |
| 320 | uint64_t break_site_id = m_real_stop_info_sp->GetValue(); |
| 321 | BreakpointSiteSP bp_site_sp; |
| 322 | if (process_sp) |
| 323 | bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id); |
| 324 | if (bp_site_sp) { |
| 325 | uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); |
| 326 | bool is_internal = true; |
| 327 | for (uint32_t i = 0; i < num_owners; i++) { |
| 328 | Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 | [diff] [blame] | 329 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 330 | log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit " |
| 331 | "breakpoint %d while calling function", |
| 332 | bp.GetID()); |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 | [diff] [blame] | 333 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 334 | if (!bp.IsInternal()) { |
| 335 | is_internal = false; |
| 336 | break; |
Jim Ingham | 40d871f | 2010-10-26 00:27:45 | [diff] [blame] | 337 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 338 | } |
| 339 | if (is_internal) { |
| 340 | if (log) |
| 341 | log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an " |
| 342 | "internal breakpoint, not stopping."); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 | [diff] [blame] | 343 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 344 | } |
Jim Ingham | 40d871f | 2010-10-26 00:27:45 | [diff] [blame] | 345 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 346 | |
| 347 | if (m_ignore_breakpoints) { |
| 348 | if (log) |
| 349 | log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring " |
| 350 | "breakpoints, overriding breakpoint stop info ShouldStop, " |
| 351 | "returning true"); |
| 352 | m_real_stop_info_sp->OverrideShouldStop(false); |
| 353 | return true; |
| 354 | } else { |
| 355 | if (log) |
| 356 | log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not " |
| 357 | "ignoring breakpoints, overriding breakpoint stop info " |
| 358 | "ShouldStop, returning true"); |
| 359 | m_real_stop_info_sp->OverrideShouldStop(true); |
| 360 | return false; |
Jim Ingham | 40d871f | 2010-10-26 00:27:45 | [diff] [blame] | 361 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 362 | } else if (!m_unwind_on_error) { |
| 363 | // If we don't want to discard this plan, than any stop we don't understand |
| 364 | // should be propagated up the stack. |
| 365 | return false; |
| 366 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 367 | // If the subplan is running, any crashes are attributable to us. If we |
| 368 | // want to discard the plan, then we say we explain the stop but if we are |
| 369 | // going to be discarded, let whoever is above us explain the stop. But |
| 370 | // don't discard the plan if the stop would restart itself (for instance if |
| 371 | // it is a signal that is set not to stop. Check that here first. We just |
| 372 | // say we explain the stop but aren't done and everything will continue on |
| 373 | // from there. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 374 | |
| 375 | if (m_real_stop_info_sp && |
| 376 | m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) { |
| 377 | SetPlanComplete(false); |
| 378 | return m_subplan_sp ? m_unwind_on_error : false; |
| 379 | } else |
| 380 | return true; |
| 381 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 382 | } |
| 383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 384 | bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) { |
| 385 | // We do some computation in DoPlanExplainsStop that may or may not set the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 386 | // plan as complete. We need to do that here to make sure our state is |
| 387 | // correct. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 388 | DoPlanExplainsStop(event_ptr); |
| 389 | |
| 390 | if (IsPlanComplete()) { |
| 391 | ReportRegisterState("Function completed. Register state was:"); |
| 392 | return true; |
| 393 | } else { |
| 394 | return false; |
| 395 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 396 | } |
| 397 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 398 | bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 400 | StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 402 | void ThreadPlanCallFunction::DidPush() { |
| 403 | //#define SINGLE_STEP_EXPRESSIONS |
| 404 | |
| 405 | // Now set the thread state to "no reason" so we don't run with whatever |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 406 | // signal was outstanding... Wait till the plan is pushed so we aren't |
| 407 | // changing the stop info till we're about to run. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 408 | |
| 409 | GetThread().SetStopInfoToNothing(); |
| 410 | |
Sean Callanan | be3a1b1 | 2010-10-26 00:31:56 | [diff] [blame] | 411 | #ifndef SINGLE_STEP_EXPRESSIONS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 412 | m_subplan_sp.reset( |
| 413 | new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads)); |
| 414 | |
| 415 | m_thread.QueueThreadPlan(m_subplan_sp, false); |
| 416 | m_subplan_sp->SetPrivate(true); |
Sean Callanan | be3a1b1 | 2010-10-26 00:31:56 | [diff] [blame] | 417 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 418 | } |
| 419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 420 | bool ThreadPlanCallFunction::WillStop() { return true; } |
| 421 | |
| 422 | bool ThreadPlanCallFunction::MischiefManaged() { |
| 423 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 424 | |
| 425 | if (IsPlanComplete()) { |
| 426 | if (log) |
| 427 | log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.", |
| 428 | static_cast<void *>(this)); |
| 429 | |
| 430 | ThreadPlan::MischiefManaged(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 431 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 432 | } else { |
Sean Callanan | c98aca6 | 2010-11-03 19:36:28 | [diff] [blame] | 433 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 434 | } |
Sean Callanan | c98aca6 | 2010-11-03 19:36:28 | [diff] [blame] | 435 | } |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 | [diff] [blame] | 436 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 437 | void ThreadPlanCallFunction::SetBreakpoints() { |
| 438 | ProcessSP process_sp(m_thread.CalculateProcess()); |
| 439 | if (m_trap_exceptions && process_sp) { |
| 440 | m_cxx_language_runtime = |
| 441 | process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus); |
| 442 | m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC); |
Jim Ingham | 0ff099f | 2014-03-07 11:16:37 | [diff] [blame] | 443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 444 | if (m_cxx_language_runtime) { |
| 445 | m_should_clear_cxx_exception_bp = |
| 446 | !m_cxx_language_runtime->ExceptionBreakpointsAreSet(); |
| 447 | m_cxx_language_runtime->SetExceptionBreakpoints(); |
Ewan Crawford | 90ff791 | 2015-07-14 10:56:58 | [diff] [blame] | 448 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 449 | if (m_objc_language_runtime) { |
| 450 | m_should_clear_objc_exception_bp = |
| 451 | !m_objc_language_runtime->ExceptionBreakpointsAreSet(); |
| 452 | m_objc_language_runtime->SetExceptionBreakpoints(); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | void ThreadPlanCallFunction::ClearBreakpoints() { |
| 458 | if (m_trap_exceptions) { |
| 459 | if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp) |
| 460 | m_cxx_language_runtime->ClearExceptionBreakpoints(); |
| 461 | if (m_objc_language_runtime && m_should_clear_objc_exception_bp) |
| 462 | m_objc_language_runtime->ClearExceptionBreakpoints(); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | bool ThreadPlanCallFunction::BreakpointsExplainStop() { |
| 467 | StopInfoSP stop_info_sp = GetPrivateStopInfo(); |
| 468 | |
| 469 | if (m_trap_exceptions) { |
| 470 | if ((m_cxx_language_runtime && |
| 471 | m_cxx_language_runtime->ExceptionBreakpointsExplainStop( |
| 472 | stop_info_sp)) || |
| 473 | (m_objc_language_runtime && |
| 474 | m_objc_language_runtime->ExceptionBreakpointsExplainStop( |
| 475 | stop_info_sp))) { |
| 476 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP)); |
| 477 | if (log) |
| 478 | log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an " |
| 479 | "exception breakpoint, setting plan complete."); |
| 480 | |
| 481 | SetPlanComplete(false); |
| 482 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 483 | // If the user has set the ObjC language breakpoint, it would normally |
| 484 | // get priority over our internal catcher breakpoint, but in this case we |
| 485 | // can't let that happen, so force the ShouldStop here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 486 | stop_info_sp->OverrideShouldStop(true); |
| 487 | return true; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | void ThreadPlanCallFunction::SetStopOthers(bool new_value) { |
| 495 | m_subplan_sp->SetStopOthers(new_value); |
| 496 | } |
| 497 | |
| 498 | bool ThreadPlanCallFunction::RestoreThreadState() { |
| 499 | return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state); |
| 500 | } |
| 501 | |
| 502 | void ThreadPlanCallFunction::SetReturnValue() { |
| 503 | ProcessSP process_sp(m_thread.GetProcess()); |
| 504 | const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr; |
| 505 | if (abi && m_return_type.IsValid()) { |
| 506 | const bool persistent = false; |
| 507 | m_return_valobj_sp = |
| 508 | abi->GetReturnValueObject(m_thread, m_return_type, persistent); |
| 509 | } |
Ewan Crawford | 90ff791 | 2015-07-14 10:56:58 | [diff] [blame] | 510 | } |