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