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