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