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