Greg Clayton | 269f91e | 2011-07-15 18:02:58 +0000 | [diff] [blame] | 1 | //===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===// |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 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 | // C Includes |
| 11 | #include <errno.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | // C++ Includes |
| 15 | // Other libraries and framework includes |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 18 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ModuleSpec.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 21 | #include "lldb/Core/State.h" |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 22 | #include "lldb/Core/UUID.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 23 | #include "lldb/Host/Host.h" |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Symbols.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 26 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 27 | #include "lldb/Target/Target.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Thread.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 29 | |
| 30 | // Project includes |
| 31 | #include "ProcessKDP.h" |
| 32 | #include "ProcessKDPLog.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 33 | #include "ThreadKDP.h" |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 34 | #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| 38 | |
| 39 | const char * |
| 40 | ProcessKDP::GetPluginNameStatic() |
| 41 | { |
| 42 | return "kdp-remote"; |
| 43 | } |
| 44 | |
| 45 | const char * |
| 46 | ProcessKDP::GetPluginDescriptionStatic() |
| 47 | { |
| 48 | return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | ProcessKDP::Terminate() |
| 53 | { |
| 54 | PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); |
| 55 | } |
| 56 | |
| 57 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 58 | lldb::ProcessSP |
| 59 | ProcessKDP::CreateInstance (Target &target, |
| 60 | Listener &listener, |
| 61 | const FileSpec *crash_file_path) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 62 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 63 | lldb::ProcessSP process_sp; |
| 64 | if (crash_file_path == NULL) |
| 65 | process_sp.reset(new ProcessKDP (target, listener)); |
| 66 | return process_sp; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 70 | ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 71 | { |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 72 | if (plugin_specified_by_name) |
| 73 | return true; |
| 74 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 75 | // For now we are just making sure the file exists for a given module |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 76 | Module *exe_module = target.GetExecutableModulePointer(); |
| 77 | if (exe_module) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 78 | { |
| 79 | const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple(); |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 80 | switch (triple_ref.getOS()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 81 | { |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 82 | case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case |
| 83 | case llvm::Triple::MacOSX: // For desktop targets |
| 84 | case llvm::Triple::IOS: // For arm targets |
| 85 | if (triple_ref.getVendor() == llvm::Triple::Apple) |
| 86 | { |
| 87 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 88 | if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && |
| 89 | exe_objfile->GetStrata() == ObjectFile::eStrataKernel) |
| 90 | return true; |
| 91 | } |
| 92 | break; |
| 93 | |
| 94 | default: |
| 95 | break; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 98 | return false; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | //---------------------------------------------------------------------- |
| 102 | // ProcessKDP constructor |
| 103 | //---------------------------------------------------------------------- |
| 104 | ProcessKDP::ProcessKDP(Target& target, Listener &listener) : |
| 105 | Process (target, listener), |
| 106 | m_comm("lldb.process.kdp-remote.communication"), |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 107 | m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 108 | m_async_thread (LLDB_INVALID_HOST_THREAD), |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 109 | m_destroy_in_process (false), |
| 110 | m_dyld_plugin_name (), |
| 111 | m_kernel_load_addr (LLDB_INVALID_ADDRESS) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 112 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 113 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 114 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | //---------------------------------------------------------------------- |
| 118 | // Destructor |
| 119 | //---------------------------------------------------------------------- |
| 120 | ProcessKDP::~ProcessKDP() |
| 121 | { |
| 122 | Clear(); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 123 | // We need to call finalize on the process before destroying ourselves |
| 124 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 125 | // destruct this class, then Process::~Process() might have problems |
| 126 | // trying to fully destroy the broadcaster. |
| 127 | Finalize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | //---------------------------------------------------------------------- |
| 131 | // PluginInterface |
| 132 | //---------------------------------------------------------------------- |
| 133 | const char * |
| 134 | ProcessKDP::GetPluginName() |
| 135 | { |
| 136 | return "Process debugging plug-in that uses the Darwin KDP remote protocol"; |
| 137 | } |
| 138 | |
| 139 | const char * |
| 140 | ProcessKDP::GetShortPluginName() |
| 141 | { |
| 142 | return GetPluginNameStatic(); |
| 143 | } |
| 144 | |
| 145 | uint32_t |
| 146 | ProcessKDP::GetPluginVersion() |
| 147 | { |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | Error |
| 152 | ProcessKDP::WillLaunch (Module* module) |
| 153 | { |
| 154 | Error error; |
| 155 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 156 | return error; |
| 157 | } |
| 158 | |
| 159 | Error |
| 160 | ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) |
| 161 | { |
| 162 | Error error; |
| 163 | error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); |
| 164 | return error; |
| 165 | } |
| 166 | |
| 167 | Error |
| 168 | ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
| 169 | { |
| 170 | Error error; |
| 171 | error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); |
| 172 | return error; |
| 173 | } |
| 174 | |
| 175 | Error |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 176 | ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 177 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 178 | Error error; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 179 | |
| 180 | // Don't let any JIT happen when doing KDP as we can't allocate |
| 181 | // memory and we don't want to be mucking with threads that might |
| 182 | // already be handling exceptions |
| 183 | SetCanJIT(false); |
| 184 | |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 185 | if (remote_url == NULL || remote_url[0] == '\0') |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 186 | { |
| 187 | error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url); |
| 188 | return error; |
| 189 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 190 | |
| 191 | std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 192 | if (conn_ap.get()) |
| 193 | { |
| 194 | // Only try once for now. |
| 195 | // TODO: check if we should be retrying? |
| 196 | const uint32_t max_retry_count = 1; |
| 197 | for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) |
| 198 | { |
| 199 | if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) |
| 200 | break; |
| 201 | usleep (100000); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (conn_ap->IsConnected()) |
| 206 | { |
| 207 | const uint16_t reply_port = conn_ap->GetReadPort (); |
| 208 | |
| 209 | if (reply_port != 0) |
| 210 | { |
| 211 | m_comm.SetConnection(conn_ap.release()); |
| 212 | |
| 213 | if (m_comm.SendRequestReattach(reply_port)) |
| 214 | { |
| 215 | if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) |
| 216 | { |
| 217 | m_comm.GetVersion(); |
| 218 | uint32_t cpu = m_comm.GetCPUType(); |
| 219 | uint32_t sub = m_comm.GetCPUSubtype(); |
| 220 | ArchSpec kernel_arch; |
| 221 | kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 222 | m_target.SetArchitecture(kernel_arch); |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 223 | |
| 224 | /* Get the kernel's UUID and load address via kdp-kernelversion packet. */ |
| 225 | |
| 226 | UUID kernel_uuid = m_comm.GetUUID (); |
| 227 | addr_t kernel_load_addr = m_comm.GetLoadAddress (); |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 228 | |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 229 | if (kernel_load_addr != LLDB_INVALID_ADDRESS) |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 230 | { |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 231 | m_kernel_load_addr = kernel_load_addr; |
| 232 | m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 235 | // Set the thread ID |
| 236 | UpdateThreadListIfNeeded (); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 237 | SetID (1); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 238 | GetThreadList (); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 239 | SetPrivateState (eStateStopped); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 240 | StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream()); |
| 241 | if (async_strm_sp) |
| 242 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 243 | const char *cstr; |
| 244 | if ((cstr = m_comm.GetKernelVersion ()) != NULL) |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 245 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 246 | async_strm_sp->Printf ("Version: %s\n", cstr); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 247 | async_strm_sp->Flush(); |
| 248 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 249 | // if ((cstr = m_comm.GetImagePath ()) != NULL) |
| 250 | // { |
| 251 | // async_strm_sp->Printf ("Image Path: %s\n", cstr); |
| 252 | // async_strm_sp->Flush(); |
| 253 | // } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 254 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 255 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 256 | else |
| 257 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 258 | error.SetErrorString("KDP_REATTACH failed"); |
| 259 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 260 | } |
| 261 | else |
| 262 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 263 | error.SetErrorString("KDP_REATTACH failed"); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | error.SetErrorString("invalid reply port from UDP connection"); |
| 269 | } |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | if (error.Success()) |
| 274 | error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); |
| 275 | } |
| 276 | if (error.Fail()) |
| 277 | m_comm.Disconnect(); |
| 278 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 279 | return error; |
| 280 | } |
| 281 | |
| 282 | //---------------------------------------------------------------------- |
| 283 | // Process Control |
| 284 | //---------------------------------------------------------------------- |
| 285 | Error |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 286 | ProcessKDP::DoLaunch (Module *exe_module, |
| 287 | const ProcessLaunchInfo &launch_info) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 288 | { |
| 289 | Error error; |
| 290 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 291 | return error; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | Error |
| 296 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
| 297 | { |
| 298 | Error error; |
| 299 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 300 | return error; |
| 301 | } |
| 302 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 303 | Error |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 304 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 305 | { |
| 306 | Error error; |
| 307 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 308 | return error; |
| 309 | } |
| 310 | |
| 311 | Error |
| 312 | ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 313 | { |
| 314 | Error error; |
| 315 | error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); |
| 316 | return error; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void |
| 321 | ProcessKDP::DidAttach () |
| 322 | { |
| 323 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| 324 | if (log) |
Johnny Chen | 01df057 | 2011-10-11 21:17:10 +0000 | [diff] [blame] | 325 | log->Printf ("ProcessKDP::DidAttach()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 326 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 327 | { |
| 328 | // TODO: figure out the register context that we will use |
| 329 | } |
| 330 | } |
| 331 | |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 332 | addr_t |
| 333 | ProcessKDP::GetImageInfoAddress() |
| 334 | { |
| 335 | return m_kernel_load_addr; |
| 336 | } |
| 337 | |
| 338 | lldb_private::DynamicLoader * |
| 339 | ProcessKDP::GetDynamicLoader () |
| 340 | { |
| 341 | if (m_dyld_ap.get() == NULL) |
| 342 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str())); |
| 343 | return m_dyld_ap.get(); |
| 344 | } |
| 345 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 346 | Error |
| 347 | ProcessKDP::WillResume () |
| 348 | { |
| 349 | return Error(); |
| 350 | } |
| 351 | |
| 352 | Error |
| 353 | ProcessKDP::DoResume () |
| 354 | { |
| 355 | Error error; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 356 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| 357 | // Only start the async thread if we try to do any process control |
| 358 | if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 359 | StartAsyncThread (); |
| 360 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 361 | bool resume = false; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 362 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 363 | // With KDP there is only one thread we can tell what to do |
| 364 | ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list)); |
| 365 | if (kernel_thread_sp) |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 366 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 367 | const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState(); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 368 | switch (thread_resume_state) |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 369 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 370 | case eStateSuspended: |
| 371 | // Nothing to do here when a thread will stay suspended |
| 372 | // we just leave the CPU mask bit set to zero for the thread |
| 373 | break; |
| 374 | |
| 375 | case eStateStepping: |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 376 | kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (true); |
| 377 | resume = true; |
| 378 | break; |
| 379 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 380 | case eStateRunning: |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 381 | kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (false); |
| 382 | resume = true; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 383 | break; |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 384 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 385 | default: |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 386 | // The only valid thread resume states are listed above |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 387 | assert (!"invalid thread resume state"); |
| 388 | break; |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 391 | |
| 392 | if (resume) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 393 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 394 | if (log) |
| 395 | log->Printf ("ProcessKDP::DoResume () sending resume"); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 396 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 397 | if (m_comm.SendRequestResume ()) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 398 | { |
| 399 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue); |
| 400 | SetPrivateState(eStateRunning); |
| 401 | } |
| 402 | else |
| 403 | error.SetErrorString ("KDP resume failed"); |
| 404 | } |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 405 | else |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 406 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 407 | error.SetErrorString ("kernel thread is suspended"); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 410 | return error; |
| 411 | } |
| 412 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 413 | lldb::ThreadSP |
| 414 | ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 415 | { |
| 416 | // KDP only tells us about one thread/core. Any other threads will usually |
| 417 | // be the ones that are read from memory by the OS plug-ins. |
| 418 | const lldb::tid_t kernel_tid = 1; |
| 419 | ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false)); |
| 420 | if (!thread_sp) |
| 421 | { |
Jim Ingham | 94a5d0d | 2012-10-10 18:32:14 +0000 | [diff] [blame^] | 422 | thread_sp.reset(new ThreadKDP (*this, kernel_tid)); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 423 | new_thread_list.AddThread(thread_sp); |
| 424 | } |
| 425 | return thread_sp; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 431 | bool |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 432 | ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 433 | { |
| 434 | // locker will keep a mutex locked until it goes out of scope |
| 435 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); |
| 436 | if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 437 | log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 438 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 439 | // Even though there is a CPU mask, it doesn't mean to can see each CPU |
| 440 | // indivudually, there is really only one. Lets call this thread 1. |
| 441 | GetKernelThread (old_thread_list, new_thread_list); |
| 442 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 443 | return new_thread_list.GetSize(false) > 0; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 446 | void |
| 447 | ProcessKDP::RefreshStateAfterStop () |
| 448 | { |
| 449 | // Let all threads recover from stopping and do any clean up based |
| 450 | // on the previous thread state (if any). |
| 451 | m_thread_list.RefreshStateAfterStop(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | Error |
| 455 | ProcessKDP::DoHalt (bool &caused_stop) |
| 456 | { |
| 457 | Error error; |
| 458 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 459 | if (m_comm.IsRunning()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 460 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 461 | if (m_destroy_in_process) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 462 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 463 | // If we are attemping to destroy, we need to not return an error to |
| 464 | // Halt or DoDestroy won't get called. |
| 465 | // We are also currently running, so send a process stopped event |
| 466 | SetPrivateState (eStateStopped); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 467 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 468 | else |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 469 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 470 | error.SetErrorString ("KDP cannot interrupt a running kernel"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | return error; |
| 474 | } |
| 475 | |
| 476 | Error |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 477 | ProcessKDP::DoDetach() |
| 478 | { |
| 479 | Error error; |
| 480 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 481 | if (log) |
| 482 | log->Printf ("ProcessKDP::DoDetach()"); |
| 483 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 484 | if (m_comm.IsRunning()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 485 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 486 | // We are running and we can't interrupt a running kernel, so we need |
| 487 | // to just close the connection to the kernel and hope for the best |
| 488 | } |
| 489 | else |
| 490 | { |
| 491 | DisableAllBreakpointSites (); |
| 492 | |
| 493 | m_thread_list.DiscardThreadPlans(); |
| 494 | |
| 495 | if (m_comm.IsConnected()) |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 496 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 497 | |
| 498 | m_comm.SendRequestDisconnect(); |
| 499 | |
| 500 | size_t response_size = m_comm.Disconnect (); |
| 501 | if (log) |
| 502 | { |
| 503 | if (response_size) |
| 504 | log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); |
| 505 | else |
| 506 | log->PutCString ("ProcessKDP::DoDetach() detach packet send failed"); |
| 507 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 508 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 509 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 510 | StopAsyncThread (); |
Greg Clayton | db9d6f4 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 511 | m_comm.Clear(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 512 | |
| 513 | SetPrivateState (eStateDetached); |
| 514 | ResumePrivateStateThread(); |
| 515 | |
| 516 | //KillDebugserverProcess (); |
| 517 | return error; |
| 518 | } |
| 519 | |
| 520 | Error |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 521 | ProcessKDP::WillDestroy () |
| 522 | { |
| 523 | Error error; |
| 524 | m_destroy_in_process = true; |
| 525 | return error; |
| 526 | } |
| 527 | |
| 528 | Error |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 529 | ProcessKDP::DoDestroy () |
| 530 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 531 | // For KDP there really is no difference between destroy and detach |
| 532 | return DoDetach(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | //------------------------------------------------------------------ |
| 536 | // Process Queries |
| 537 | //------------------------------------------------------------------ |
| 538 | |
| 539 | bool |
| 540 | ProcessKDP::IsAlive () |
| 541 | { |
| 542 | return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
| 543 | } |
| 544 | |
| 545 | //------------------------------------------------------------------ |
| 546 | // Process Memory |
| 547 | //------------------------------------------------------------------ |
| 548 | size_t |
| 549 | ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 550 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 551 | if (m_comm.IsConnected()) |
| 552 | return m_comm.SendRequestReadMemory (addr, buf, size, error); |
| 553 | error.SetErrorString ("not connected"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | size_t |
| 558 | ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 559 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 560 | if (m_comm.IsConnected()) |
| 561 | return m_comm.SendRequestWriteMemory (addr, buf, size, error); |
| 562 | error.SetErrorString ("not connected"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | lldb::addr_t |
| 567 | ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 568 | { |
| 569 | error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); |
| 570 | return LLDB_INVALID_ADDRESS; |
| 571 | } |
| 572 | |
| 573 | Error |
| 574 | ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) |
| 575 | { |
| 576 | Error error; |
| 577 | error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); |
| 578 | return error; |
| 579 | } |
| 580 | |
| 581 | Error |
| 582 | ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site) |
| 583 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 584 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 585 | { |
| 586 | Error error; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 587 | if (!bp_site->IsEnabled()) |
| 588 | { |
| 589 | if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) |
| 590 | { |
| 591 | bp_site->SetEnabled(true); |
| 592 | bp_site->SetType (BreakpointSite::eExternal); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | error.SetErrorString ("KDP set breakpoint failed"); |
| 597 | } |
| 598 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 599 | return error; |
| 600 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 601 | return EnableSoftwareBreakpoint (bp_site); |
| 602 | } |
| 603 | |
| 604 | Error |
| 605 | ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site) |
| 606 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 607 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 608 | { |
| 609 | Error error; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 610 | if (bp_site->IsEnabled()) |
| 611 | { |
| 612 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 613 | if (bp_type == BreakpointSite::eExternal) |
| 614 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 615 | if (m_destroy_in_process && m_comm.IsRunning()) |
| 616 | { |
| 617 | // We are trying to destroy our connection and we are running |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 618 | bp_site->SetEnabled(false); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 619 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 620 | else |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 621 | { |
| 622 | if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) |
| 623 | bp_site->SetEnabled(false); |
| 624 | else |
| 625 | error.SetErrorString ("KDP remove breakpoint failed"); |
| 626 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 627 | } |
| 628 | else |
| 629 | { |
| 630 | error = DisableSoftwareBreakpoint (bp_site); |
| 631 | } |
| 632 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 633 | return error; |
| 634 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 635 | return DisableSoftwareBreakpoint (bp_site); |
| 636 | } |
| 637 | |
| 638 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 639 | ProcessKDP::EnableWatchpoint (Watchpoint *wp) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 640 | { |
| 641 | Error error; |
| 642 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 643 | return error; |
| 644 | } |
| 645 | |
| 646 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 647 | ProcessKDP::DisableWatchpoint (Watchpoint *wp) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 648 | { |
| 649 | Error error; |
| 650 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 651 | return error; |
| 652 | } |
| 653 | |
| 654 | void |
| 655 | ProcessKDP::Clear() |
| 656 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 657 | m_thread_list.Clear(); |
| 658 | } |
| 659 | |
| 660 | Error |
| 661 | ProcessKDP::DoSignal (int signo) |
| 662 | { |
| 663 | Error error; |
| 664 | error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); |
| 665 | return error; |
| 666 | } |
| 667 | |
| 668 | void |
| 669 | ProcessKDP::Initialize() |
| 670 | { |
| 671 | static bool g_initialized = false; |
| 672 | |
| 673 | if (g_initialized == false) |
| 674 | { |
| 675 | g_initialized = true; |
| 676 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 677 | GetPluginDescriptionStatic(), |
| 678 | CreateInstance); |
| 679 | |
| 680 | Log::Callbacks log_callbacks = { |
| 681 | ProcessKDPLog::DisableLog, |
| 682 | ProcessKDPLog::EnableLog, |
| 683 | ProcessKDPLog::ListLogCategories |
| 684 | }; |
| 685 | |
| 686 | Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | bool |
| 691 | ProcessKDP::StartAsyncThread () |
| 692 | { |
| 693 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 694 | |
| 695 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 696 | log->Printf ("ProcessKDP::StartAsyncThread ()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 697 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 698 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 699 | return true; |
| 700 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 701 | m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); |
| 702 | return IS_VALID_LLDB_HOST_THREAD(m_async_thread); |
| 703 | } |
| 704 | |
| 705 | void |
| 706 | ProcessKDP::StopAsyncThread () |
| 707 | { |
| 708 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 709 | |
| 710 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 711 | log->Printf ("ProcessKDP::StopAsyncThread ()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 712 | |
| 713 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 714 | |
| 715 | // Stop the stdio thread |
| 716 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 717 | { |
| 718 | Host::ThreadJoin (m_async_thread, NULL, NULL); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 719 | m_async_thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | |
| 724 | void * |
| 725 | ProcessKDP::AsyncThread (void *arg) |
| 726 | { |
| 727 | ProcessKDP *process = (ProcessKDP*) arg; |
| 728 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 729 | const lldb::pid_t pid = process->GetID(); |
| 730 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 731 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| 732 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 733 | log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 734 | |
| 735 | Listener listener ("ProcessKDP::AsyncThread"); |
| 736 | EventSP event_sp; |
| 737 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 738 | eBroadcastBitAsyncThreadShouldExit; |
| 739 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 740 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 741 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 742 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 743 | bool done = false; |
| 744 | while (!done) |
| 745 | { |
| 746 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 747 | log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...", |
| 748 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 749 | if (listener.WaitForEvent (NULL, event_sp)) |
| 750 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 751 | uint32_t event_type = event_sp->GetType(); |
| 752 | if (log) |
| 753 | log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...", |
| 754 | pid, |
| 755 | event_type); |
| 756 | |
| 757 | // When we are running, poll for 1 second to try and get an exception |
| 758 | // to indicate the process has stopped. If we don't get one, check to |
| 759 | // make sure no one asked us to exit |
| 760 | bool is_running = false; |
| 761 | DataExtractor exc_reply_packet; |
| 762 | do |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 763 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 764 | switch (event_type) |
| 765 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 766 | case eBroadcastBitAsyncContinue: |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 767 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 768 | is_running = true; |
| 769 | if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC)) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 770 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 771 | ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList())); |
| 772 | thread_sp->GetRegisterContext()->InvalidateAllRegisters(); |
| 773 | static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet); |
| 774 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 775 | // TODO: parse the stop reply packet |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 776 | is_running = false; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 777 | process->SetPrivateState(eStateStopped); |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | // Check to see if we are supposed to exit. There is no way to |
| 782 | // interrupt a running kernel, so all we can do is wait for an |
| 783 | // exception or detach... |
| 784 | if (listener.GetNextEvent(event_sp)) |
| 785 | { |
| 786 | // We got an event, go through the loop again |
| 787 | event_type = event_sp->GetType(); |
| 788 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 791 | break; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 792 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 793 | case eBroadcastBitAsyncThreadShouldExit: |
| 794 | if (log) |
| 795 | log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", |
| 796 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 797 | done = true; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 798 | is_running = false; |
| 799 | break; |
| 800 | |
| 801 | default: |
| 802 | if (log) |
| 803 | log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x", |
| 804 | pid, |
| 805 | event_type); |
| 806 | done = true; |
| 807 | is_running = false; |
| 808 | break; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 809 | } |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 810 | } while (is_running); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 811 | } |
| 812 | else |
| 813 | { |
| 814 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 815 | log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false", |
| 816 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 817 | done = true; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 823 | log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...", |
| 824 | arg, |
| 825 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 826 | |
| 827 | process->m_async_thread = LLDB_INVALID_HOST_THREAD; |
| 828 | return NULL; |
| 829 | } |
| 830 | |
| 831 | |