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 | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 26 | #include "lldb/Interpreter/CommandObject.h" |
| 27 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
| 28 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 29 | #include "lldb/Interpreter/OptionGroupString.h" |
| 30 | #include "lldb/Interpreter/OptionGroupUInt64.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 31 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 32 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Target.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 34 | #include "lldb/Target/Thread.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 35 | |
| 36 | // Project includes |
| 37 | #include "ProcessKDP.h" |
| 38 | #include "ProcessKDPLog.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 39 | #include "ThreadKDP.h" |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 40 | #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" |
Jason Molenda | d06bb50 | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 41 | #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h" |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 42 | #include "Utility/StringExtractor.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace lldb; |
| 45 | using namespace lldb_private; |
| 46 | |
| 47 | const char * |
| 48 | ProcessKDP::GetPluginNameStatic() |
| 49 | { |
| 50 | return "kdp-remote"; |
| 51 | } |
| 52 | |
| 53 | const char * |
| 54 | ProcessKDP::GetPluginDescriptionStatic() |
| 55 | { |
| 56 | return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | ProcessKDP::Terminate() |
| 61 | { |
| 62 | PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); |
| 63 | } |
| 64 | |
| 65 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 66 | lldb::ProcessSP |
| 67 | ProcessKDP::CreateInstance (Target &target, |
| 68 | Listener &listener, |
| 69 | const FileSpec *crash_file_path) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 70 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 71 | lldb::ProcessSP process_sp; |
| 72 | if (crash_file_path == NULL) |
| 73 | process_sp.reset(new ProcessKDP (target, listener)); |
| 74 | return process_sp; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 78 | ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 79 | { |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 80 | if (plugin_specified_by_name) |
| 81 | return true; |
| 82 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 83 | // 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] | 84 | Module *exe_module = target.GetExecutableModulePointer(); |
| 85 | if (exe_module) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 86 | { |
| 87 | const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple(); |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 88 | switch (triple_ref.getOS()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 89 | { |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 90 | case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case |
| 91 | case llvm::Triple::MacOSX: // For desktop targets |
| 92 | case llvm::Triple::IOS: // For arm targets |
| 93 | if (triple_ref.getVendor() == llvm::Triple::Apple) |
| 94 | { |
| 95 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 96 | if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && |
| 97 | exe_objfile->GetStrata() == ObjectFile::eStrataKernel) |
| 98 | return true; |
| 99 | } |
| 100 | break; |
| 101 | |
| 102 | default: |
| 103 | break; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 106 | return false; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | //---------------------------------------------------------------------- |
| 110 | // ProcessKDP constructor |
| 111 | //---------------------------------------------------------------------- |
| 112 | ProcessKDP::ProcessKDP(Target& target, Listener &listener) : |
| 113 | Process (target, listener), |
| 114 | m_comm("lldb.process.kdp-remote.communication"), |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 115 | m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 116 | m_async_thread (LLDB_INVALID_HOST_THREAD), |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 117 | m_dyld_plugin_name (), |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 118 | m_kernel_load_addr (LLDB_INVALID_ADDRESS), |
| 119 | m_command_sp() |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 120 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 121 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 122 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | //---------------------------------------------------------------------- |
| 126 | // Destructor |
| 127 | //---------------------------------------------------------------------- |
| 128 | ProcessKDP::~ProcessKDP() |
| 129 | { |
| 130 | Clear(); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 131 | // We need to call finalize on the process before destroying ourselves |
| 132 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 133 | // destruct this class, then Process::~Process() might have problems |
| 134 | // trying to fully destroy the broadcaster. |
| 135 | Finalize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | //---------------------------------------------------------------------- |
| 139 | // PluginInterface |
| 140 | //---------------------------------------------------------------------- |
| 141 | const char * |
| 142 | ProcessKDP::GetPluginName() |
| 143 | { |
| 144 | return "Process debugging plug-in that uses the Darwin KDP remote protocol"; |
| 145 | } |
| 146 | |
| 147 | const char * |
| 148 | ProcessKDP::GetShortPluginName() |
| 149 | { |
| 150 | return GetPluginNameStatic(); |
| 151 | } |
| 152 | |
| 153 | uint32_t |
| 154 | ProcessKDP::GetPluginVersion() |
| 155 | { |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | Error |
| 160 | ProcessKDP::WillLaunch (Module* module) |
| 161 | { |
| 162 | Error error; |
| 163 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 164 | return error; |
| 165 | } |
| 166 | |
| 167 | Error |
| 168 | ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) |
| 169 | { |
| 170 | Error error; |
| 171 | error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); |
| 172 | return error; |
| 173 | } |
| 174 | |
| 175 | Error |
| 176 | ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
| 177 | { |
| 178 | Error error; |
| 179 | error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); |
| 180 | return error; |
| 181 | } |
| 182 | |
| 183 | Error |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 184 | ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 185 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 186 | Error error; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 187 | |
| 188 | // Don't let any JIT happen when doing KDP as we can't allocate |
| 189 | // memory and we don't want to be mucking with threads that might |
| 190 | // already be handling exceptions |
| 191 | SetCanJIT(false); |
| 192 | |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 193 | if (remote_url == NULL || remote_url[0] == '\0') |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 194 | { |
| 195 | error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url); |
| 196 | return error; |
| 197 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 198 | |
Greg Clayton | 102b2c2 | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 199 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 200 | if (conn_ap.get()) |
| 201 | { |
| 202 | // Only try once for now. |
| 203 | // TODO: check if we should be retrying? |
| 204 | const uint32_t max_retry_count = 1; |
| 205 | for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) |
| 206 | { |
| 207 | if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) |
| 208 | break; |
| 209 | usleep (100000); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (conn_ap->IsConnected()) |
| 214 | { |
| 215 | const uint16_t reply_port = conn_ap->GetReadPort (); |
| 216 | |
| 217 | if (reply_port != 0) |
| 218 | { |
| 219 | m_comm.SetConnection(conn_ap.release()); |
| 220 | |
| 221 | if (m_comm.SendRequestReattach(reply_port)) |
| 222 | { |
| 223 | if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) |
| 224 | { |
| 225 | m_comm.GetVersion(); |
| 226 | uint32_t cpu = m_comm.GetCPUType(); |
| 227 | uint32_t sub = m_comm.GetCPUSubtype(); |
| 228 | ArchSpec kernel_arch; |
| 229 | kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 230 | m_target.SetArchitecture(kernel_arch); |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 231 | |
Jason Molenda | d06bb50 | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 232 | /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */ |
| 233 | /* An EFI kdp session has neither UUID nor load address. */ |
| 234 | |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 235 | UUID kernel_uuid = m_comm.GetUUID (); |
| 236 | addr_t kernel_load_addr = m_comm.GetLoadAddress (); |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 237 | |
Jason Molenda | d06bb50 | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 238 | if (m_comm.RemoteIsEFI ()) |
| 239 | { |
| 240 | m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic(); |
| 241 | } |
| 242 | else if (kernel_load_addr != LLDB_INVALID_ADDRESS) |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 243 | { |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 244 | m_kernel_load_addr = kernel_load_addr; |
| 245 | m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 248 | // Set the thread ID |
| 249 | UpdateThreadListIfNeeded (); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 250 | SetID (1); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 251 | GetThreadList (); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 252 | SetPrivateState (eStateStopped); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 253 | StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream()); |
| 254 | if (async_strm_sp) |
| 255 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 256 | const char *cstr; |
| 257 | if ((cstr = m_comm.GetKernelVersion ()) != NULL) |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 258 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 259 | async_strm_sp->Printf ("Version: %s\n", cstr); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 260 | async_strm_sp->Flush(); |
| 261 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 262 | // if ((cstr = m_comm.GetImagePath ()) != NULL) |
| 263 | // { |
| 264 | // async_strm_sp->Printf ("Image Path: %s\n", cstr); |
| 265 | // async_strm_sp->Flush(); |
| 266 | // } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 267 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 268 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 269 | else |
| 270 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 271 | error.SetErrorString("KDP_REATTACH failed"); |
| 272 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 273 | } |
| 274 | else |
| 275 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 276 | error.SetErrorString("KDP_REATTACH failed"); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | error.SetErrorString("invalid reply port from UDP connection"); |
| 282 | } |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | if (error.Success()) |
| 287 | error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); |
| 288 | } |
| 289 | if (error.Fail()) |
| 290 | m_comm.Disconnect(); |
| 291 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 292 | return error; |
| 293 | } |
| 294 | |
| 295 | //---------------------------------------------------------------------- |
| 296 | // Process Control |
| 297 | //---------------------------------------------------------------------- |
| 298 | Error |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 299 | ProcessKDP::DoLaunch (Module *exe_module, |
| 300 | const ProcessLaunchInfo &launch_info) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 301 | { |
| 302 | Error error; |
| 303 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 304 | return error; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | Error |
| 309 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
| 310 | { |
| 311 | Error error; |
| 312 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 313 | return error; |
| 314 | } |
| 315 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 316 | Error |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 317 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 318 | { |
| 319 | Error error; |
| 320 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 321 | return error; |
| 322 | } |
| 323 | |
| 324 | Error |
| 325 | 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] | 326 | { |
| 327 | Error error; |
| 328 | error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); |
| 329 | return error; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | void |
| 334 | ProcessKDP::DidAttach () |
| 335 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 336 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 337 | if (log) |
Johnny Chen | 01df057 | 2011-10-11 21:17:10 +0000 | [diff] [blame] | 338 | log->Printf ("ProcessKDP::DidAttach()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 339 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 340 | { |
| 341 | // TODO: figure out the register context that we will use |
| 342 | } |
| 343 | } |
| 344 | |
Jason Molenda | b46937c | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 345 | addr_t |
| 346 | ProcessKDP::GetImageInfoAddress() |
| 347 | { |
| 348 | return m_kernel_load_addr; |
| 349 | } |
| 350 | |
| 351 | lldb_private::DynamicLoader * |
| 352 | ProcessKDP::GetDynamicLoader () |
| 353 | { |
| 354 | if (m_dyld_ap.get() == NULL) |
| 355 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str())); |
| 356 | return m_dyld_ap.get(); |
| 357 | } |
| 358 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 359 | Error |
| 360 | ProcessKDP::WillResume () |
| 361 | { |
| 362 | return Error(); |
| 363 | } |
| 364 | |
| 365 | Error |
| 366 | ProcessKDP::DoResume () |
| 367 | { |
| 368 | Error error; |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 369 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 370 | // Only start the async thread if we try to do any process control |
| 371 | if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 372 | StartAsyncThread (); |
| 373 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 374 | bool resume = false; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 375 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 376 | // With KDP there is only one thread we can tell what to do |
| 377 | ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list)); |
| 378 | if (kernel_thread_sp) |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 379 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 380 | const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState(); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 381 | switch (thread_resume_state) |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 382 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 383 | case eStateSuspended: |
| 384 | // Nothing to do here when a thread will stay suspended |
| 385 | // we just leave the CPU mask bit set to zero for the thread |
| 386 | break; |
| 387 | |
| 388 | case eStateStepping: |
Greg Clayton | 4ee040c | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 389 | { |
| 390 | lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); |
| 391 | |
| 392 | if (reg_ctx_sp) |
| 393 | { |
| 394 | reg_ctx_sp->HardwareSingleStep (true); |
| 395 | resume = true; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); |
| 400 | } |
| 401 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 402 | break; |
| 403 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 404 | case eStateRunning: |
Greg Clayton | 4ee040c | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 405 | { |
| 406 | lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); |
| 407 | |
| 408 | if (reg_ctx_sp) |
| 409 | { |
| 410 | reg_ctx_sp->HardwareSingleStep (false); |
| 411 | resume = true; |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); |
| 416 | } |
| 417 | } |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 418 | break; |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 419 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 420 | default: |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 421 | // The only valid thread resume states are listed above |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 422 | assert (!"invalid thread resume state"); |
| 423 | break; |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 426 | |
| 427 | if (resume) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 428 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 429 | if (log) |
| 430 | log->Printf ("ProcessKDP::DoResume () sending resume"); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 431 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 432 | if (m_comm.SendRequestResume ()) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 433 | { |
| 434 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue); |
| 435 | SetPrivateState(eStateRunning); |
| 436 | } |
| 437 | else |
| 438 | error.SetErrorString ("KDP resume failed"); |
| 439 | } |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 440 | else |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 441 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 442 | error.SetErrorString ("kernel thread is suspended"); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 445 | return error; |
| 446 | } |
| 447 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 448 | lldb::ThreadSP |
| 449 | ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 450 | { |
| 451 | // KDP only tells us about one thread/core. Any other threads will usually |
| 452 | // be the ones that are read from memory by the OS plug-ins. |
| 453 | const lldb::tid_t kernel_tid = 1; |
| 454 | ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false)); |
| 455 | if (!thread_sp) |
Jim Ingham | 94a5d0d | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 456 | thread_sp.reset(new ThreadKDP (*this, kernel_tid)); |
Greg Clayton | a365dc5 | 2013-04-11 22:23:34 +0000 | [diff] [blame] | 457 | new_thread_list.AddThread(thread_sp); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 458 | return thread_sp; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 464 | bool |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 465 | ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 466 | { |
| 467 | // locker will keep a mutex locked until it goes out of scope |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 468 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 469 | if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 470 | log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 471 | |
Greg Clayton | a365dc5 | 2013-04-11 22:23:34 +0000 | [diff] [blame] | 472 | // Even though there is a CPU mask, it doesn't mean we can see each CPU |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 473 | // indivudually, there is really only one. Lets call this thread 1. |
| 474 | GetKernelThread (old_thread_list, new_thread_list); |
| 475 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 476 | return new_thread_list.GetSize(false) > 0; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 479 | void |
| 480 | ProcessKDP::RefreshStateAfterStop () |
| 481 | { |
| 482 | // Let all threads recover from stopping and do any clean up based |
| 483 | // on the previous thread state (if any). |
| 484 | m_thread_list.RefreshStateAfterStop(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | Error |
| 488 | ProcessKDP::DoHalt (bool &caused_stop) |
| 489 | { |
| 490 | Error error; |
| 491 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 492 | if (m_comm.IsRunning()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 493 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 494 | if (m_destroy_in_process) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 495 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 496 | // If we are attemping to destroy, we need to not return an error to |
| 497 | // Halt or DoDestroy won't get called. |
| 498 | // We are also currently running, so send a process stopped event |
| 499 | SetPrivateState (eStateStopped); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 500 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 501 | else |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 502 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 503 | error.SetErrorString ("KDP cannot interrupt a running kernel"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | return error; |
| 507 | } |
| 508 | |
| 509 | Error |
Daniel Malea | 411ab47 | 2013-05-01 19:11:56 +0000 | [diff] [blame] | 510 | ProcessKDP::DoDetach() |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 511 | { |
| 512 | Error error; |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 513 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 514 | if (log) |
Daniel Malea | 411ab47 | 2013-05-01 19:11:56 +0000 | [diff] [blame] | 515 | log->Printf ("ProcessKDP::DoDetach()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 516 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 517 | if (m_comm.IsRunning()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 518 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 519 | // We are running and we can't interrupt a running kernel, so we need |
| 520 | // to just close the connection to the kernel and hope for the best |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | DisableAllBreakpointSites (); |
| 525 | |
| 526 | m_thread_list.DiscardThreadPlans(); |
| 527 | |
Daniel Malea | 411ab47 | 2013-05-01 19:11:56 +0000 | [diff] [blame] | 528 | if (m_comm.IsConnected()) |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 529 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 530 | |
| 531 | m_comm.SendRequestDisconnect(); |
| 532 | |
| 533 | size_t response_size = m_comm.Disconnect (); |
| 534 | if (log) |
| 535 | { |
| 536 | if (response_size) |
| 537 | log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); |
| 538 | else |
| 539 | log->PutCString ("ProcessKDP::DoDetach() detach packet send failed"); |
| 540 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 541 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 542 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 543 | StopAsyncThread (); |
Greg Clayton | db9d6f4 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 544 | m_comm.Clear(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 545 | |
| 546 | SetPrivateState (eStateDetached); |
| 547 | ResumePrivateStateThread(); |
| 548 | |
| 549 | //KillDebugserverProcess (); |
| 550 | return error; |
| 551 | } |
| 552 | |
| 553 | Error |
| 554 | ProcessKDP::DoDestroy () |
| 555 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 556 | // For KDP there really is no difference between destroy and detach |
Daniel Malea | 411ab47 | 2013-05-01 19:11:56 +0000 | [diff] [blame] | 557 | return DoDetach(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | //------------------------------------------------------------------ |
| 561 | // Process Queries |
| 562 | //------------------------------------------------------------------ |
| 563 | |
| 564 | bool |
| 565 | ProcessKDP::IsAlive () |
| 566 | { |
| 567 | return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
| 568 | } |
| 569 | |
| 570 | //------------------------------------------------------------------ |
| 571 | // Process Memory |
| 572 | //------------------------------------------------------------------ |
| 573 | size_t |
| 574 | ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 575 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 576 | if (m_comm.IsConnected()) |
| 577 | return m_comm.SendRequestReadMemory (addr, buf, size, error); |
| 578 | error.SetErrorString ("not connected"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | size_t |
| 583 | ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 584 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 585 | if (m_comm.IsConnected()) |
| 586 | return m_comm.SendRequestWriteMemory (addr, buf, size, error); |
| 587 | error.SetErrorString ("not connected"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | lldb::addr_t |
| 592 | ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 593 | { |
| 594 | error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); |
| 595 | return LLDB_INVALID_ADDRESS; |
| 596 | } |
| 597 | |
| 598 | Error |
| 599 | ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) |
| 600 | { |
| 601 | Error error; |
| 602 | error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); |
| 603 | return error; |
| 604 | } |
| 605 | |
| 606 | Error |
Jim Ingham | efb4aeb | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 607 | ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 608 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 609 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 610 | { |
| 611 | Error error; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 612 | if (!bp_site->IsEnabled()) |
| 613 | { |
| 614 | if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) |
| 615 | { |
| 616 | bp_site->SetEnabled(true); |
| 617 | bp_site->SetType (BreakpointSite::eExternal); |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | error.SetErrorString ("KDP set breakpoint failed"); |
| 622 | } |
| 623 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 624 | return error; |
| 625 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 626 | return EnableSoftwareBreakpoint (bp_site); |
| 627 | } |
| 628 | |
| 629 | Error |
Jim Ingham | efb4aeb | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 630 | ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 631 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 632 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 633 | { |
| 634 | Error error; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 635 | if (bp_site->IsEnabled()) |
| 636 | { |
| 637 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 638 | if (bp_type == BreakpointSite::eExternal) |
| 639 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 640 | if (m_destroy_in_process && m_comm.IsRunning()) |
| 641 | { |
| 642 | // We are trying to destroy our connection and we are running |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 643 | bp_site->SetEnabled(false); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 644 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 645 | else |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 646 | { |
| 647 | if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) |
| 648 | bp_site->SetEnabled(false); |
| 649 | else |
| 650 | error.SetErrorString ("KDP remove breakpoint failed"); |
| 651 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 652 | } |
| 653 | else |
| 654 | { |
| 655 | error = DisableSoftwareBreakpoint (bp_site); |
| 656 | } |
| 657 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 658 | return error; |
| 659 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 660 | return DisableSoftwareBreakpoint (bp_site); |
| 661 | } |
| 662 | |
| 663 | Error |
Jim Ingham | 9c970a3 | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 664 | ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 665 | { |
| 666 | Error error; |
| 667 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 668 | return error; |
| 669 | } |
| 670 | |
| 671 | Error |
Jim Ingham | 9c970a3 | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 672 | ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 673 | { |
| 674 | Error error; |
| 675 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 676 | return error; |
| 677 | } |
| 678 | |
| 679 | void |
| 680 | ProcessKDP::Clear() |
| 681 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 682 | m_thread_list.Clear(); |
| 683 | } |
| 684 | |
| 685 | Error |
| 686 | ProcessKDP::DoSignal (int signo) |
| 687 | { |
| 688 | Error error; |
| 689 | error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); |
| 690 | return error; |
| 691 | } |
| 692 | |
| 693 | void |
| 694 | ProcessKDP::Initialize() |
| 695 | { |
| 696 | static bool g_initialized = false; |
| 697 | |
| 698 | if (g_initialized == false) |
| 699 | { |
| 700 | g_initialized = true; |
| 701 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 702 | GetPluginDescriptionStatic(), |
| 703 | CreateInstance); |
| 704 | |
| 705 | Log::Callbacks log_callbacks = { |
| 706 | ProcessKDPLog::DisableLog, |
| 707 | ProcessKDPLog::EnableLog, |
| 708 | ProcessKDPLog::ListLogCategories |
| 709 | }; |
| 710 | |
| 711 | Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | bool |
| 716 | ProcessKDP::StartAsyncThread () |
| 717 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 718 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 719 | |
| 720 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 721 | log->Printf ("ProcessKDP::StartAsyncThread ()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 722 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 723 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 724 | return true; |
| 725 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 726 | m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); |
| 727 | return IS_VALID_LLDB_HOST_THREAD(m_async_thread); |
| 728 | } |
| 729 | |
| 730 | void |
| 731 | ProcessKDP::StopAsyncThread () |
| 732 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 733 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 734 | |
| 735 | if (log) |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 736 | log->Printf ("ProcessKDP::StopAsyncThread ()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 737 | |
| 738 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 739 | |
| 740 | // Stop the stdio thread |
| 741 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 742 | { |
| 743 | Host::ThreadJoin (m_async_thread, NULL, NULL); |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 744 | m_async_thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | void * |
| 750 | ProcessKDP::AsyncThread (void *arg) |
| 751 | { |
| 752 | ProcessKDP *process = (ProcessKDP*) arg; |
| 753 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 754 | const lldb::pid_t pid = process->GetID(); |
| 755 | |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 756 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 757 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 758 | log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 759 | |
| 760 | Listener listener ("ProcessKDP::AsyncThread"); |
| 761 | EventSP event_sp; |
| 762 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 763 | eBroadcastBitAsyncThreadShouldExit; |
| 764 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 765 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 766 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 767 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 768 | bool done = false; |
| 769 | while (!done) |
| 770 | { |
| 771 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 772 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 773 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 774 | if (listener.WaitForEvent (NULL, event_sp)) |
| 775 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 776 | uint32_t event_type = event_sp->GetType(); |
| 777 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 778 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...", |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 779 | pid, |
| 780 | event_type); |
| 781 | |
| 782 | // When we are running, poll for 1 second to try and get an exception |
| 783 | // to indicate the process has stopped. If we don't get one, check to |
| 784 | // make sure no one asked us to exit |
| 785 | bool is_running = false; |
| 786 | DataExtractor exc_reply_packet; |
| 787 | do |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 788 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 789 | switch (event_type) |
| 790 | { |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 791 | case eBroadcastBitAsyncContinue: |
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 | is_running = true; |
| 794 | if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC)) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 795 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 796 | ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList())); |
Greg Clayton | 4ee040c | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 797 | if (thread_sp) |
| 798 | { |
| 799 | lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext()); |
| 800 | if (reg_ctx_sp) |
| 801 | reg_ctx_sp->InvalidateAllRegisters(); |
| 802 | static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet); |
| 803 | } |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 804 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 805 | // TODO: parse the stop reply packet |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 806 | is_running = false; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 807 | process->SetPrivateState(eStateStopped); |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | // Check to see if we are supposed to exit. There is no way to |
| 812 | // interrupt a running kernel, so all we can do is wait for an |
| 813 | // exception or detach... |
| 814 | if (listener.GetNextEvent(event_sp)) |
| 815 | { |
| 816 | // We got an event, go through the loop again |
| 817 | event_type = event_sp->GetType(); |
| 818 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 821 | break; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 822 | |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 823 | case eBroadcastBitAsyncThreadShouldExit: |
| 824 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 825 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 826 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 827 | done = true; |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 828 | is_running = false; |
| 829 | break; |
| 830 | |
| 831 | default: |
| 832 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 833 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x", |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 834 | pid, |
| 835 | event_type); |
| 836 | done = true; |
| 837 | is_running = false; |
| 838 | break; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 839 | } |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 840 | } while (is_running); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 841 | } |
| 842 | else |
| 843 | { |
| 844 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 845 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 846 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 847 | done = true; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 853 | log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...", |
Greg Clayton | e76f8c4 | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 854 | arg, |
| 855 | pid); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 856 | |
| 857 | process->m_async_thread = LLDB_INVALID_HOST_THREAD; |
| 858 | return NULL; |
| 859 | } |
| 860 | |
| 861 | |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 862 | class CommandObjectProcessKDPPacketSend : public CommandObjectParsed |
| 863 | { |
| 864 | private: |
| 865 | |
| 866 | OptionGroupOptions m_option_group; |
| 867 | OptionGroupUInt64 m_command_byte; |
| 868 | OptionGroupString m_packet_data; |
| 869 | |
| 870 | virtual Options * |
| 871 | GetOptions () |
| 872 | { |
| 873 | return &m_option_group; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | public: |
| 878 | CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) : |
| 879 | CommandObjectParsed (interpreter, |
| 880 | "process plugin packet send", |
| 881 | "Send a custom packet through the KDP protocol by specifying the command byte and the packet payload data. A packet will be sent with a correct header and payload, and the raw result bytes will be displayed as a string value. ", |
| 882 | NULL), |
| 883 | m_option_group (interpreter), |
| 884 | m_command_byte(LLDB_OPT_SET_1, true , "command", 'c', 0, eArgTypeNone, "Specify the command byte to use when sending the KDP request packet.", 0), |
| 885 | m_packet_data (LLDB_OPT_SET_1, false, "payload", 'p', 0, eArgTypeNone, "Specify packet payload bytes as a hex ASCII string with no spaces or hex prefixes.", NULL) |
| 886 | { |
| 887 | m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 888 | m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 889 | m_option_group.Finalize(); |
| 890 | } |
| 891 | |
| 892 | ~CommandObjectProcessKDPPacketSend () |
| 893 | { |
| 894 | } |
| 895 | |
| 896 | bool |
| 897 | DoExecute (Args& command, CommandReturnObject &result) |
| 898 | { |
| 899 | const size_t argc = command.GetArgumentCount(); |
| 900 | if (argc == 0) |
| 901 | { |
| 902 | if (!m_command_byte.GetOptionValue().OptionWasSet()) |
| 903 | { |
| 904 | result.AppendError ("the --command option must be set to a valid command byte"); |
| 905 | result.SetStatus (eReturnStatusFailed); |
| 906 | } |
| 907 | else |
| 908 | { |
| 909 | const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0); |
| 910 | if (command_byte > 0 && command_byte <= UINT8_MAX) |
| 911 | { |
| 912 | ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 913 | if (process) |
| 914 | { |
| 915 | const StateType state = process->GetState(); |
| 916 | |
| 917 | if (StateIsStoppedState (state, true)) |
| 918 | { |
| 919 | std::vector<uint8_t> payload_bytes; |
| 920 | const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue(); |
| 921 | if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0]) |
| 922 | { |
| 923 | StringExtractor extractor(ascii_hex_bytes_cstr); |
| 924 | const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size(); |
| 925 | if (ascii_hex_bytes_cstr_len & 1) |
| 926 | { |
| 927 | result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr); |
| 928 | result.SetStatus (eReturnStatusFailed); |
| 929 | return false; |
| 930 | } |
| 931 | payload_bytes.resize(ascii_hex_bytes_cstr_len/2); |
| 932 | if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size()) |
| 933 | { |
| 934 | result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr); |
| 935 | result.SetStatus (eReturnStatusFailed); |
| 936 | return false; |
| 937 | } |
| 938 | } |
| 939 | Error error; |
| 940 | DataExtractor reply; |
| 941 | process->GetCommunication().SendRawRequest (command_byte, |
| 942 | payload_bytes.empty() ? NULL : payload_bytes.data(), |
| 943 | payload_bytes.size(), |
| 944 | reply, |
| 945 | error); |
| 946 | |
| 947 | if (error.Success()) |
| 948 | { |
| 949 | // Copy the binary bytes into a hex ASCII string for the result |
| 950 | StreamString packet; |
| 951 | packet.PutBytesAsRawHex8(reply.GetDataStart(), |
| 952 | reply.GetByteSize(), |
| 953 | lldb::endian::InlHostByteOrder(), |
| 954 | lldb::endian::InlHostByteOrder()); |
| 955 | result.AppendMessage(packet.GetString().c_str()); |
| 956 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 957 | return true; |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | const char *error_cstr = error.AsCString(); |
| 962 | if (error_cstr && error_cstr[0]) |
| 963 | result.AppendError (error_cstr); |
| 964 | else |
| 965 | result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError()); |
| 966 | result.SetStatus (eReturnStatusFailed); |
| 967 | return false; |
| 968 | } |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state)); |
| 973 | result.SetStatus (eReturnStatusFailed); |
| 974 | } |
| 975 | } |
| 976 | else |
| 977 | { |
| 978 | result.AppendError ("invalid process"); |
| 979 | result.SetStatus (eReturnStatusFailed); |
| 980 | } |
| 981 | } |
| 982 | else |
| 983 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 984 | result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte); |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 985 | result.SetStatus (eReturnStatusFailed); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str()); |
| 992 | result.SetStatus (eReturnStatusFailed); |
| 993 | } |
| 994 | return false; |
| 995 | } |
| 996 | }; |
| 997 | |
| 998 | class CommandObjectProcessKDPPacket : public CommandObjectMultiword |
| 999 | { |
| 1000 | private: |
| 1001 | |
| 1002 | public: |
| 1003 | CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) : |
| 1004 | CommandObjectMultiword (interpreter, |
| 1005 | "process plugin packet", |
| 1006 | "Commands that deal with KDP remote packets.", |
| 1007 | NULL) |
| 1008 | { |
| 1009 | LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter))); |
| 1010 | } |
| 1011 | |
| 1012 | ~CommandObjectProcessKDPPacket () |
| 1013 | { |
| 1014 | } |
| 1015 | }; |
| 1016 | |
| 1017 | class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword |
| 1018 | { |
| 1019 | public: |
| 1020 | CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) : |
| 1021 | CommandObjectMultiword (interpreter, |
| 1022 | "process plugin", |
| 1023 | "A set of commands for operating on a ProcessKDP process.", |
| 1024 | "process plugin <subcommand> [<subcommand-options>]") |
| 1025 | { |
| 1026 | LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter))); |
| 1027 | } |
| 1028 | |
| 1029 | ~CommandObjectMultiwordProcessKDP () |
| 1030 | { |
| 1031 | } |
| 1032 | }; |
| 1033 | |
| 1034 | CommandObject * |
| 1035 | ProcessKDP::GetPluginCommandObject() |
| 1036 | { |
| 1037 | if (!m_command_sp) |
| 1038 | m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter())); |
| 1039 | return m_command_sp.get(); |
| 1040 | } |
| 1041 | |