| Greg Clayton | 59ec512 | 2011-07-15 18:02:58 +0000 | [diff] [blame] | 1 | //===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===// |
| Greg Clayton | f9765ac | 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 |
| Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame^] | 15 | #include <mutex> |
| 16 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 17 | // Other libraries and framework includes |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Debugger.h" |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 19 | #include "lldb/Core/PluginManager.h" |
| Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Module.h" |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ModuleSpec.h" |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 22 | #include "lldb/Core/State.h" |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 23 | #include "lldb/Core/UUID.h" |
| Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 24 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 25 | #include "lldb/Host/Host.h" |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Symbols.h" |
| Keno Fischer | 3d7162b | 2014-08-06 21:18:13 +0000 | [diff] [blame] | 27 | #include "lldb/Host/Socket.h" |
| Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 28 | #include "lldb/Host/ThreadLauncher.h" |
| Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 29 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 30 | #include "lldb/Interpreter/CommandObject.h" |
| 31 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
| 32 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 33 | #include "lldb/Interpreter/OptionGroupString.h" |
| 34 | #include "lldb/Interpreter/OptionGroupUInt64.h" |
| Ilia K | 41204d0 | 2015-03-04 12:05:24 +0000 | [diff] [blame] | 35 | #include "lldb/Interpreter/OptionValueProperties.h" |
| Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 36 | #include "lldb/Symbol/ObjectFile.h" |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 37 | #include "lldb/Target/RegisterContext.h" |
| Greg Clayton | 5750802 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 38 | #include "lldb/Target/Target.h" |
| Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 39 | #include "lldb/Target/Thread.h" |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 40 | |
| Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 41 | #define USEC_PER_SEC 1000000 |
| 42 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 43 | // Project includes |
| 44 | #include "ProcessKDP.h" |
| 45 | #include "ProcessKDPLog.h" |
| Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 46 | #include "ThreadKDP.h" |
| Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 47 | #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" |
| Jason Molenda | 840f12c | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 48 | #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h" |
| Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 49 | #include "Utility/StringExtractor.h" |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace lldb; |
| 52 | using namespace lldb_private; |
| 53 | |
| Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 54 | namespace { |
| 55 | |
| 56 | static PropertyDefinition |
| 57 | g_properties[] = |
| 58 | { |
| 59 | { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." }, |
| 60 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 61 | }; |
| 62 | |
| 63 | enum |
| 64 | { |
| 65 | ePropertyPacketTimeout |
| 66 | }; |
| 67 | |
| 68 | class PluginProperties : public Properties |
| 69 | { |
| 70 | public: |
| 71 | |
| 72 | static ConstString |
| 73 | GetSettingName () |
| 74 | { |
| 75 | return ProcessKDP::GetPluginNameStatic(); |
| 76 | } |
| 77 | |
| 78 | PluginProperties() : |
| 79 | Properties () |
| 80 | { |
| 81 | m_collection_sp.reset (new OptionValueProperties(GetSettingName())); |
| 82 | m_collection_sp->Initialize(g_properties); |
| 83 | } |
| 84 | |
| 85 | virtual |
| 86 | ~PluginProperties() |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | uint64_t |
| 91 | GetPacketTimeout() |
| 92 | { |
| 93 | const uint32_t idx = ePropertyPacketTimeout; |
| 94 | return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; |
| 99 | |
| 100 | static const ProcessKDPPropertiesSP & |
| 101 | GetGlobalPluginProperties() |
| 102 | { |
| 103 | static ProcessKDPPropertiesSP g_settings_sp; |
| 104 | if (!g_settings_sp) |
| 105 | g_settings_sp.reset (new PluginProperties ()); |
| 106 | return g_settings_sp; |
| 107 | } |
| 108 | |
| 109 | } // anonymous namespace end |
| 110 | |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 111 | static const lldb::tid_t g_kernel_tid = 1; |
| 112 | |
| Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 113 | ConstString |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 114 | ProcessKDP::GetPluginNameStatic() |
| 115 | { |
| Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 116 | static ConstString g_name("kdp-remote"); |
| 117 | return g_name; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | const char * |
| 121 | ProcessKDP::GetPluginDescriptionStatic() |
| 122 | { |
| 123 | return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | ProcessKDP::Terminate() |
| 128 | { |
| 129 | PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); |
| 130 | } |
| 131 | |
| 132 | |
| Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 133 | lldb::ProcessSP |
| 134 | ProcessKDP::CreateInstance (Target &target, |
| 135 | Listener &listener, |
| 136 | const FileSpec *crash_file_path) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 137 | { |
| Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 138 | lldb::ProcessSP process_sp; |
| 139 | if (crash_file_path == NULL) |
| 140 | process_sp.reset(new ProcessKDP (target, listener)); |
| 141 | return process_sp; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 145 | ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 146 | { |
| Greg Clayton | 596ed24 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 147 | if (plugin_specified_by_name) |
| 148 | return true; |
| 149 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 150 | // For now we are just making sure the file exists for a given module |
| Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 151 | Module *exe_module = target.GetExecutableModulePointer(); |
| 152 | if (exe_module) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 153 | { |
| 154 | const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple(); |
| Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 155 | switch (triple_ref.getOS()) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 156 | { |
| Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 157 | case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case |
| 158 | case llvm::Triple::MacOSX: // For desktop targets |
| 159 | case llvm::Triple::IOS: // For arm targets |
| 160 | if (triple_ref.getVendor() == llvm::Triple::Apple) |
| 161 | { |
| 162 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 163 | if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && |
| 164 | exe_objfile->GetStrata() == ObjectFile::eStrataKernel) |
| 165 | return true; |
| 166 | } |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | break; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| Greg Clayton | 596ed24 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 173 | return false; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | //---------------------------------------------------------------------- |
| 177 | // ProcessKDP constructor |
| 178 | //---------------------------------------------------------------------- |
| 179 | ProcessKDP::ProcessKDP(Target& target, Listener &listener) : |
| 180 | Process (target, listener), |
| 181 | m_comm("lldb.process.kdp-remote.communication"), |
| Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 182 | m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), |
| Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 183 | m_dyld_plugin_name (), |
| Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 184 | m_kernel_load_addr (LLDB_INVALID_ADDRESS), |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 185 | m_command_sp(), |
| 186 | m_kernel_thread_wp() |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 187 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 188 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 189 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
| Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 190 | const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); |
| 191 | if (timeout_seconds > 0) |
| 192 | m_comm.SetPacketTimeout(timeout_seconds); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | //---------------------------------------------------------------------- |
| 196 | // Destructor |
| 197 | //---------------------------------------------------------------------- |
| 198 | ProcessKDP::~ProcessKDP() |
| 199 | { |
| 200 | Clear(); |
| Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 201 | // We need to call finalize on the process before destroying ourselves |
| 202 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 203 | // destruct this class, then Process::~Process() might have problems |
| 204 | // trying to fully destroy the broadcaster. |
| 205 | Finalize(); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | //---------------------------------------------------------------------- |
| 209 | // PluginInterface |
| 210 | //---------------------------------------------------------------------- |
| Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 211 | lldb_private::ConstString |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 212 | ProcessKDP::GetPluginName() |
| 213 | { |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 214 | return GetPluginNameStatic(); |
| 215 | } |
| 216 | |
| 217 | uint32_t |
| 218 | ProcessKDP::GetPluginVersion() |
| 219 | { |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | Error |
| 224 | ProcessKDP::WillLaunch (Module* module) |
| 225 | { |
| 226 | Error error; |
| 227 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 228 | return error; |
| 229 | } |
| 230 | |
| 231 | Error |
| 232 | ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) |
| 233 | { |
| 234 | Error error; |
| 235 | error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); |
| 236 | return error; |
| 237 | } |
| 238 | |
| 239 | Error |
| 240 | ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
| 241 | { |
| 242 | Error error; |
| 243 | error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); |
| 244 | return error; |
| 245 | } |
| 246 | |
| 247 | Error |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 248 | ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 249 | { |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 250 | Error error; |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 251 | |
| 252 | // Don't let any JIT happen when doing KDP as we can't allocate |
| 253 | // memory and we don't want to be mucking with threads that might |
| 254 | // already be handling exceptions |
| 255 | SetCanJIT(false); |
| 256 | |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 257 | if (remote_url == NULL || remote_url[0] == '\0') |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 258 | { |
| 259 | error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url); |
| 260 | return error; |
| 261 | } |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 262 | |
| Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 263 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 264 | if (conn_ap.get()) |
| 265 | { |
| 266 | // Only try once for now. |
| 267 | // TODO: check if we should be retrying? |
| 268 | const uint32_t max_retry_count = 1; |
| 269 | for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) |
| 270 | { |
| 271 | if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) |
| 272 | break; |
| 273 | usleep (100000); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (conn_ap->IsConnected()) |
| 278 | { |
| Keno Fischer | 3d7162b | 2014-08-06 21:18:13 +0000 | [diff] [blame] | 279 | const Socket& socket = static_cast<const Socket&>(*conn_ap->GetReadObject()); |
| Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 280 | const uint16_t reply_port = socket.GetLocalPortNumber(); |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 281 | |
| 282 | if (reply_port != 0) |
| 283 | { |
| 284 | m_comm.SetConnection(conn_ap.release()); |
| 285 | |
| 286 | if (m_comm.SendRequestReattach(reply_port)) |
| 287 | { |
| 288 | if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) |
| 289 | { |
| 290 | m_comm.GetVersion(); |
| 291 | uint32_t cpu = m_comm.GetCPUType(); |
| 292 | uint32_t sub = m_comm.GetCPUSubtype(); |
| 293 | ArchSpec kernel_arch; |
| 294 | kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 295 | m_target.SetArchitecture(kernel_arch); |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 296 | |
| Jason Molenda | 840f12c | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 297 | /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */ |
| 298 | /* An EFI kdp session has neither UUID nor load address. */ |
| 299 | |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 300 | UUID kernel_uuid = m_comm.GetUUID (); |
| 301 | addr_t kernel_load_addr = m_comm.GetLoadAddress (); |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 302 | |
| Jason Molenda | 840f12c | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 303 | if (m_comm.RemoteIsEFI ()) |
| 304 | { |
| Greg Clayton | a1bce2e | 2014-07-16 21:16:27 +0000 | [diff] [blame] | 305 | // Select an invalid plugin name for the dynamic loader so one doesn't get used |
| 306 | // since EFI does its own manual loading via python scripting |
| 307 | static ConstString g_none_dynamic_loader("none"); |
| 308 | m_dyld_plugin_name = g_none_dynamic_loader; |
| 309 | |
| 310 | if (kernel_uuid.IsValid()) { |
| 311 | // If EFI passed in a UUID= try to lookup UUID |
| 312 | // The slide will not be provided. But the UUID |
| 313 | // lookup will be used to launch EFI debug scripts |
| 314 | // from the dSYM, that can load all of the symbols. |
| 315 | ModuleSpec module_spec; |
| 316 | module_spec.GetUUID() = kernel_uuid; |
| 317 | module_spec.GetArchitecture() = m_target.GetArchitecture(); |
| 318 | |
| 319 | // Lookup UUID locally, before attempting dsymForUUID like action |
| 320 | module_spec.GetSymbolFileSpec() = Symbols::LocateExecutableSymbolFile(module_spec); |
| 321 | if (module_spec.GetSymbolFileSpec()) |
| 322 | module_spec.GetFileSpec() = Symbols::LocateExecutableObjectFile (module_spec); |
| 323 | if (!module_spec.GetSymbolFileSpec() || !module_spec.GetSymbolFileSpec()) |
| 324 | Symbols::DownloadObjectAndSymbolFile (module_spec, true); |
| 325 | |
| 326 | if (module_spec.GetFileSpec().Exists()) |
| 327 | { |
| 328 | ModuleSP module_sp(new Module (module_spec.GetFileSpec(), m_target.GetArchitecture())); |
| 329 | if (module_sp.get() && module_sp->MatchesModuleSpec (module_spec)) |
| 330 | { |
| 331 | // Get the current target executable |
| 332 | ModuleSP exe_module_sp (m_target.GetExecutableModule ()); |
| 333 | |
| 334 | // Make sure you don't already have the right module loaded and they will be uniqued |
| 335 | if (exe_module_sp.get() != module_sp.get()) |
| 336 | m_target.SetExecutableModule (module_sp, false); |
| 337 | } |
| 338 | } |
| 339 | } |
| Jason Molenda | 840f12c | 2012-10-25 00:25:13 +0000 | [diff] [blame] | 340 | } |
| Jason Molenda | ca2ffa7 | 2013-05-09 23:52:21 +0000 | [diff] [blame] | 341 | else if (m_comm.RemoteIsDarwinKernel ()) |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 342 | { |
| Jason Molenda | ca2ffa7 | 2013-05-09 23:52:21 +0000 | [diff] [blame] | 343 | m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); |
| Jason Molenda | a8ea4ba | 2013-05-06 23:02:03 +0000 | [diff] [blame] | 344 | if (kernel_load_addr != LLDB_INVALID_ADDRESS) |
| 345 | { |
| 346 | m_kernel_load_addr = kernel_load_addr; |
| 347 | } |
| Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 350 | // Set the thread ID |
| 351 | UpdateThreadListIfNeeded (); |
| Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 352 | SetID (1); |
| Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 353 | GetThreadList (); |
| Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 354 | SetPrivateState (eStateStopped); |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 355 | StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream()); |
| 356 | if (async_strm_sp) |
| 357 | { |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 358 | const char *cstr; |
| 359 | if ((cstr = m_comm.GetKernelVersion ()) != NULL) |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 360 | { |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 361 | async_strm_sp->Printf ("Version: %s\n", cstr); |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 362 | async_strm_sp->Flush(); |
| 363 | } |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 364 | // if ((cstr = m_comm.GetImagePath ()) != NULL) |
| 365 | // { |
| 366 | // async_strm_sp->Printf ("Image Path: %s\n", cstr); |
| 367 | // async_strm_sp->Flush(); |
| 368 | // } |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 369 | } |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 370 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 371 | else |
| 372 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 373 | error.SetErrorString("KDP_REATTACH failed"); |
| 374 | } |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 375 | } |
| 376 | else |
| 377 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 378 | error.SetErrorString("KDP_REATTACH failed"); |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | error.SetErrorString("invalid reply port from UDP connection"); |
| 384 | } |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | if (error.Success()) |
| 389 | error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); |
| 390 | } |
| 391 | if (error.Fail()) |
| 392 | m_comm.Disconnect(); |
| 393 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 394 | return error; |
| 395 | } |
| 396 | |
| 397 | //---------------------------------------------------------------------- |
| 398 | // Process Control |
| 399 | //---------------------------------------------------------------------- |
| 400 | Error |
| Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 401 | ProcessKDP::DoLaunch (Module *exe_module, |
| Jean-Daniel Dupas | 7782de9 | 2013-12-09 22:52:50 +0000 | [diff] [blame] | 402 | ProcessLaunchInfo &launch_info) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 403 | { |
| 404 | Error error; |
| 405 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 406 | return error; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | Error |
| 411 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
| 412 | { |
| 413 | Error error; |
| 414 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 415 | return error; |
| 416 | } |
| 417 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 418 | Error |
| Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 419 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 420 | { |
| 421 | Error error; |
| 422 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 423 | return error; |
| 424 | } |
| 425 | |
| 426 | Error |
| Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 427 | ProcessKDP::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 428 | { |
| 429 | Error error; |
| 430 | error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); |
| 431 | return error; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | void |
| Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 436 | ProcessKDP::DidAttach (ArchSpec &process_arch) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 437 | { |
| Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 438 | Process::DidAttach(process_arch); |
| 439 | |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 440 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 441 | if (log) |
| Johnny Chen | 54cb8f8 | 2011-10-11 21:17:10 +0000 | [diff] [blame] | 442 | log->Printf ("ProcessKDP::DidAttach()"); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 443 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 444 | { |
| Greg Clayton | c3eefa3 | 2014-10-31 00:06:52 +0000 | [diff] [blame] | 445 | uint32_t cpu = m_comm.GetCPUType(); |
| 446 | if (cpu) |
| 447 | { |
| 448 | uint32_t sub = m_comm.GetCPUSubtype(); |
| 449 | process_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 450 | } |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 454 | addr_t |
| 455 | ProcessKDP::GetImageInfoAddress() |
| 456 | { |
| 457 | return m_kernel_load_addr; |
| 458 | } |
| 459 | |
| 460 | lldb_private::DynamicLoader * |
| 461 | ProcessKDP::GetDynamicLoader () |
| 462 | { |
| 463 | if (m_dyld_ap.get() == NULL) |
| Jason Molenda | 2e56a25 | 2013-05-11 03:09:05 +0000 | [diff] [blame] | 464 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); |
| Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 465 | return m_dyld_ap.get(); |
| 466 | } |
| 467 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 468 | Error |
| 469 | ProcessKDP::WillResume () |
| 470 | { |
| 471 | return Error(); |
| 472 | } |
| 473 | |
| 474 | Error |
| 475 | ProcessKDP::DoResume () |
| 476 | { |
| 477 | Error error; |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 478 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 479 | // Only start the async thread if we try to do any process control |
| Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 480 | if (!m_async_thread.IsJoinable()) |
| Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 481 | StartAsyncThread(); |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 482 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 483 | bool resume = false; |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 484 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 485 | // With KDP there is only one thread we can tell what to do |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 486 | ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid)); |
| 487 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 488 | if (kernel_thread_sp) |
| Greg Clayton | 4b1b8b3 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 489 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 490 | const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState(); |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 491 | |
| 492 | if (log) |
| 493 | log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state)); |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 494 | switch (thread_resume_state) |
| Greg Clayton | 4b1b8b3 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 495 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 496 | case eStateSuspended: |
| 497 | // Nothing to do here when a thread will stay suspended |
| 498 | // we just leave the CPU mask bit set to zero for the thread |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 499 | if (log) |
| 500 | log->Printf ("ProcessKDP::DoResume() = suspended???"); |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case eStateStepping: |
| Greg Clayton | 1afa68e | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 504 | { |
| 505 | lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); |
| 506 | |
| 507 | if (reg_ctx_sp) |
| 508 | { |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 509 | if (log) |
| 510 | log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);"); |
| Greg Clayton | 1afa68e | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 511 | reg_ctx_sp->HardwareSingleStep (true); |
| 512 | resume = true; |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); |
| 517 | } |
| 518 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 519 | break; |
| 520 | |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 521 | case eStateRunning: |
| Greg Clayton | 1afa68e | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 522 | { |
| 523 | lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); |
| 524 | |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 525 | if (reg_ctx_sp) |
| 526 | { |
| 527 | if (log) |
| 528 | log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);"); |
| 529 | reg_ctx_sp->HardwareSingleStep (false); |
| 530 | resume = true; |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); |
| 535 | } |
| Greg Clayton | 1afa68e | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 536 | } |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 537 | break; |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 538 | |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 539 | default: |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 540 | // The only valid thread resume states are listed above |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 541 | assert (!"invalid thread resume state"); |
| 542 | break; |
| Greg Clayton | 4b1b8b3 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 545 | |
| 546 | if (resume) |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 547 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 548 | if (log) |
| 549 | log->Printf ("ProcessKDP::DoResume () sending resume"); |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 550 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 551 | if (m_comm.SendRequestResume ()) |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 552 | { |
| 553 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue); |
| 554 | SetPrivateState(eStateRunning); |
| 555 | } |
| 556 | else |
| 557 | error.SetErrorString ("KDP resume failed"); |
| 558 | } |
| Greg Clayton | 4b1b8b3 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 559 | else |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 560 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 561 | error.SetErrorString ("kernel thread is suspended"); |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 564 | return error; |
| 565 | } |
| 566 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 567 | lldb::ThreadSP |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 568 | ProcessKDP::GetKernelThread() |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 569 | { |
| 570 | // KDP only tells us about one thread/core. Any other threads will usually |
| 571 | // be the ones that are read from memory by the OS plug-ins. |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 572 | |
| 573 | ThreadSP thread_sp (m_kernel_thread_wp.lock()); |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 574 | if (!thread_sp) |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 575 | { |
| 576 | thread_sp.reset(new ThreadKDP (*this, g_kernel_tid)); |
| 577 | m_kernel_thread_wp = thread_sp; |
| 578 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 579 | return thread_sp; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 585 | bool |
| Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 586 | ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 587 | { |
| 588 | // locker will keep a mutex locked until it goes out of scope |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 589 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 590 | if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 591 | log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 592 | |
| Greg Clayton | 39da3ef | 2013-04-11 22:23:34 +0000 | [diff] [blame] | 593 | // Even though there is a CPU mask, it doesn't mean we can see each CPU |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 594 | // indivudually, there is really only one. Lets call this thread 1. |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 595 | ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false)); |
| 596 | if (!thread_sp) |
| 597 | thread_sp = GetKernelThread (); |
| 598 | new_thread_list.AddThread(thread_sp); |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 599 | |
| Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 600 | return new_thread_list.GetSize(false) > 0; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 603 | void |
| 604 | ProcessKDP::RefreshStateAfterStop () |
| 605 | { |
| 606 | // Let all threads recover from stopping and do any clean up based |
| 607 | // on the previous thread state (if any). |
| 608 | m_thread_list.RefreshStateAfterStop(); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | Error |
| 612 | ProcessKDP::DoHalt (bool &caused_stop) |
| 613 | { |
| 614 | Error error; |
| 615 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 616 | if (m_comm.IsRunning()) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 617 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 618 | if (m_destroy_in_process) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 619 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 620 | // If we are attemping to destroy, we need to not return an error to |
| 621 | // Halt or DoDestroy won't get called. |
| 622 | // We are also currently running, so send a process stopped event |
| 623 | SetPrivateState (eStateStopped); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 624 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 625 | else |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 626 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 627 | error.SetErrorString ("KDP cannot interrupt a running kernel"); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | return error; |
| 631 | } |
| 632 | |
| 633 | Error |
| Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 634 | ProcessKDP::DoDetach(bool keep_stopped) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 635 | { |
| 636 | Error error; |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 637 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 638 | if (log) |
| Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 639 | log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 640 | |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 641 | if (m_comm.IsRunning()) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 642 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 643 | // We are running and we can't interrupt a running kernel, so we need |
| 644 | // to just close the connection to the kernel and hope for the best |
| 645 | } |
| 646 | else |
| 647 | { |
| Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 648 | // If we are going to keep the target stopped, then don't send the disconnect message. |
| 649 | if (!keep_stopped && m_comm.IsConnected()) |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 650 | { |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 651 | const bool success = m_comm.SendRequestDisconnect(); |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 652 | if (log) |
| 653 | { |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 654 | if (success) |
| 655 | log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 656 | else |
| Jim Ingham | 77e82d1 | 2013-05-09 00:05:35 +0000 | [diff] [blame] | 657 | log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed"); |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 658 | } |
| Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 659 | m_comm.Disconnect (); |
| Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 660 | } |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 661 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 662 | StopAsyncThread (); |
| Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 663 | m_comm.Clear(); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 664 | |
| 665 | SetPrivateState (eStateDetached); |
| 666 | ResumePrivateStateThread(); |
| 667 | |
| 668 | //KillDebugserverProcess (); |
| 669 | return error; |
| 670 | } |
| 671 | |
| 672 | Error |
| 673 | ProcessKDP::DoDestroy () |
| 674 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 675 | // For KDP there really is no difference between destroy and detach |
| Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 676 | bool keep_stopped = false; |
| 677 | return DoDetach(keep_stopped); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | //------------------------------------------------------------------ |
| 681 | // Process Queries |
| 682 | //------------------------------------------------------------------ |
| 683 | |
| 684 | bool |
| 685 | ProcessKDP::IsAlive () |
| 686 | { |
| 687 | return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
| 688 | } |
| 689 | |
| 690 | //------------------------------------------------------------------ |
| 691 | // Process Memory |
| 692 | //------------------------------------------------------------------ |
| 693 | size_t |
| 694 | ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 695 | { |
| Jason Molenda | 8eb3281 | 2014-05-21 23:44:02 +0000 | [diff] [blame] | 696 | uint8_t *data_buffer = (uint8_t *) buf; |
| Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 697 | if (m_comm.IsConnected()) |
| Jason Molenda | 8eb3281 | 2014-05-21 23:44:02 +0000 | [diff] [blame] | 698 | { |
| 699 | const size_t max_read_size = 512; |
| 700 | size_t total_bytes_read = 0; |
| 701 | |
| 702 | // Read the requested amount of memory in 512 byte chunks |
| 703 | while (total_bytes_read < size) |
| 704 | { |
| 705 | size_t bytes_to_read_this_request = size - total_bytes_read; |
| 706 | if (bytes_to_read_this_request > max_read_size) |
| 707 | { |
| 708 | bytes_to_read_this_request = max_read_size; |
| 709 | } |
| 710 | size_t bytes_read = m_comm.SendRequestReadMemory (addr + total_bytes_read, |
| 711 | data_buffer + total_bytes_read, |
| 712 | bytes_to_read_this_request, error); |
| 713 | total_bytes_read += bytes_read; |
| 714 | if (error.Fail() || bytes_read == 0) |
| 715 | { |
| 716 | return total_bytes_read; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | return total_bytes_read; |
| 721 | } |
| Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 722 | error.SetErrorString ("not connected"); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | size_t |
| 727 | ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 728 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 729 | if (m_comm.IsConnected()) |
| 730 | return m_comm.SendRequestWriteMemory (addr, buf, size, error); |
| 731 | error.SetErrorString ("not connected"); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | lldb::addr_t |
| 736 | ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 737 | { |
| 738 | error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); |
| 739 | return LLDB_INVALID_ADDRESS; |
| 740 | } |
| 741 | |
| 742 | Error |
| 743 | ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) |
| 744 | { |
| 745 | Error error; |
| 746 | error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); |
| 747 | return error; |
| 748 | } |
| 749 | |
| 750 | Error |
| Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 751 | ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 752 | { |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 753 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 754 | { |
| 755 | Error error; |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 756 | if (!bp_site->IsEnabled()) |
| 757 | { |
| 758 | if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) |
| 759 | { |
| 760 | bp_site->SetEnabled(true); |
| 761 | bp_site->SetType (BreakpointSite::eExternal); |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | error.SetErrorString ("KDP set breakpoint failed"); |
| 766 | } |
| 767 | } |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 768 | return error; |
| 769 | } |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 770 | return EnableSoftwareBreakpoint (bp_site); |
| 771 | } |
| 772 | |
| 773 | Error |
| Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 774 | ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 775 | { |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 776 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 777 | { |
| 778 | Error error; |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 779 | if (bp_site->IsEnabled()) |
| 780 | { |
| 781 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 782 | if (bp_type == BreakpointSite::eExternal) |
| 783 | { |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 784 | if (m_destroy_in_process && m_comm.IsRunning()) |
| 785 | { |
| 786 | // We are trying to destroy our connection and we are running |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 787 | bp_site->SetEnabled(false); |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 788 | } |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 789 | else |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 790 | { |
| 791 | if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) |
| 792 | bp_site->SetEnabled(false); |
| 793 | else |
| 794 | error.SetErrorString ("KDP remove breakpoint failed"); |
| 795 | } |
| Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 796 | } |
| 797 | else |
| 798 | { |
| 799 | error = DisableSoftwareBreakpoint (bp_site); |
| 800 | } |
| 801 | } |
| Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 802 | return error; |
| 803 | } |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 804 | return DisableSoftwareBreakpoint (bp_site); |
| 805 | } |
| 806 | |
| 807 | Error |
| Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 808 | ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 809 | { |
| 810 | Error error; |
| 811 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 812 | return error; |
| 813 | } |
| 814 | |
| 815 | Error |
| Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 816 | ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 817 | { |
| 818 | Error error; |
| 819 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 820 | return error; |
| 821 | } |
| 822 | |
| 823 | void |
| 824 | ProcessKDP::Clear() |
| 825 | { |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 826 | m_thread_list.Clear(); |
| 827 | } |
| 828 | |
| 829 | Error |
| 830 | ProcessKDP::DoSignal (int signo) |
| 831 | { |
| 832 | Error error; |
| 833 | error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); |
| 834 | return error; |
| 835 | } |
| 836 | |
| 837 | void |
| 838 | ProcessKDP::Initialize() |
| 839 | { |
| Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 840 | static std::once_flag g_once_flag; |
| 841 | |
| 842 | std::call_once(g_once_flag, []() |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 843 | { |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 844 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 845 | GetPluginDescriptionStatic(), |
| Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 846 | CreateInstance, |
| Bruce Mitchener | 488c89e | 2015-04-03 09:13:18 +0000 | [diff] [blame] | 847 | DebuggerInitialize); |
| Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 848 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 849 | Log::Callbacks log_callbacks = { |
| 850 | ProcessKDPLog::DisableLog, |
| 851 | ProcessKDPLog::EnableLog, |
| 852 | ProcessKDPLog::ListLogCategories |
| 853 | }; |
| Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 854 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 855 | Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); |
| Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 856 | }); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 859 | void |
| 860 | ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger) |
| 861 | { |
| 862 | if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) |
| 863 | { |
| 864 | const bool is_global_setting = true; |
| 865 | PluginManager::CreateSettingForProcessPlugin (debugger, |
| 866 | GetGlobalPluginProperties()->GetValueProperties(), |
| 867 | ConstString ("Properties for the kdp-remote process plug-in."), |
| 868 | is_global_setting); |
| 869 | } |
| 870 | } |
| 871 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 872 | bool |
| 873 | ProcessKDP::StartAsyncThread () |
| 874 | { |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 875 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 876 | |
| 877 | if (log) |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 878 | log->Printf ("ProcessKDP::StartAsyncThread ()"); |
| Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 879 | |
| Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 880 | if (m_async_thread.IsJoinable()) |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 881 | return true; |
| 882 | |
| Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 883 | m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); |
| Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 884 | return m_async_thread.IsJoinable(); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | void |
| 888 | ProcessKDP::StopAsyncThread () |
| 889 | { |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 890 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 891 | |
| 892 | if (log) |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 893 | log->Printf ("ProcessKDP::StopAsyncThread ()"); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 894 | |
| 895 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 896 | |
| 897 | // Stop the stdio thread |
| Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 898 | if (m_async_thread.IsJoinable()) |
| Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 899 | m_async_thread.Join(nullptr); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | |
| 903 | void * |
| 904 | ProcessKDP::AsyncThread (void *arg) |
| 905 | { |
| 906 | ProcessKDP *process = (ProcessKDP*) arg; |
| 907 | |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 908 | const lldb::pid_t pid = process->GetID(); |
| 909 | |
| Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 910 | Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 911 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 912 | log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 913 | |
| 914 | Listener listener ("ProcessKDP::AsyncThread"); |
| 915 | EventSP event_sp; |
| 916 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 917 | eBroadcastBitAsyncThreadShouldExit; |
| 918 | |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 919 | |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 920 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 921 | { |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 922 | bool done = false; |
| 923 | while (!done) |
| 924 | { |
| 925 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 926 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 927 | pid); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 928 | if (listener.WaitForEvent (NULL, event_sp)) |
| 929 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 930 | uint32_t event_type = event_sp->GetType(); |
| 931 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 932 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...", |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 933 | pid, |
| 934 | event_type); |
| 935 | |
| 936 | // When we are running, poll for 1 second to try and get an exception |
| 937 | // to indicate the process has stopped. If we don't get one, check to |
| 938 | // make sure no one asked us to exit |
| 939 | bool is_running = false; |
| 940 | DataExtractor exc_reply_packet; |
| 941 | do |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 942 | { |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 943 | switch (event_type) |
| 944 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 945 | case eBroadcastBitAsyncContinue: |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 946 | { |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 947 | is_running = true; |
| 948 | if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC)) |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 949 | { |
| Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 950 | ThreadSP thread_sp (process->GetKernelThread()); |
| Greg Clayton | 1afa68e | 2013-04-02 20:32:37 +0000 | [diff] [blame] | 951 | if (thread_sp) |
| 952 | { |
| 953 | lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext()); |
| 954 | if (reg_ctx_sp) |
| 955 | reg_ctx_sp->InvalidateAllRegisters(); |
| 956 | static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet); |
| 957 | } |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 958 | |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 959 | // TODO: parse the stop reply packet |
| Greg Clayton | 97d5cf0 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 960 | is_running = false; |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 961 | process->SetPrivateState(eStateStopped); |
| 962 | } |
| 963 | else |
| 964 | { |
| 965 | // Check to see if we are supposed to exit. There is no way to |
| 966 | // interrupt a running kernel, so all we can do is wait for an |
| 967 | // exception or detach... |
| 968 | if (listener.GetNextEvent(event_sp)) |
| 969 | { |
| 970 | // We got an event, go through the loop again |
| 971 | event_type = event_sp->GetType(); |
| 972 | } |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 975 | break; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 976 | |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 977 | case eBroadcastBitAsyncThreadShouldExit: |
| 978 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 979 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 980 | pid); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 981 | done = true; |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 982 | is_running = false; |
| 983 | break; |
| 984 | |
| 985 | default: |
| 986 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 987 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x", |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 988 | pid, |
| 989 | event_type); |
| 990 | done = true; |
| 991 | is_running = false; |
| 992 | break; |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 993 | } |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 994 | } while (is_running); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 995 | } |
| 996 | else |
| 997 | { |
| 998 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 999 | log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 1000 | pid); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 1001 | done = true; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (log) |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1007 | log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| Greg Clayton | 7925fbb | 2012-09-21 16:31:20 +0000 | [diff] [blame] | 1008 | arg, |
| 1009 | pid); |
| Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1010 | |
| 1011 | process->m_async_thread.Reset(); |
| Greg Clayton | f9765ac | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 1012 | return NULL; |
| 1013 | } |
| 1014 | |
| 1015 | |
| Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 1016 | class CommandObjectProcessKDPPacketSend : public CommandObjectParsed |
| 1017 | { |
| 1018 | private: |
| 1019 | |
| 1020 | OptionGroupOptions m_option_group; |
| 1021 | OptionGroupUInt64 m_command_byte; |
| 1022 | OptionGroupString m_packet_data; |
| 1023 | |
| 1024 | virtual Options * |
| 1025 | GetOptions () |
| 1026 | { |
| 1027 | return &m_option_group; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | public: |
| 1032 | CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) : |
| 1033 | CommandObjectParsed (interpreter, |
| 1034 | "process plugin packet send", |
| 1035 | "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. ", |
| 1036 | NULL), |
| 1037 | m_option_group (interpreter), |
| 1038 | 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), |
| 1039 | 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) |
| 1040 | { |
| 1041 | m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 1042 | m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 1043 | m_option_group.Finalize(); |
| 1044 | } |
| 1045 | |
| 1046 | ~CommandObjectProcessKDPPacketSend () |
| 1047 | { |
| 1048 | } |
| 1049 | |
| 1050 | bool |
| 1051 | DoExecute (Args& command, CommandReturnObject &result) |
| 1052 | { |
| 1053 | const size_t argc = command.GetArgumentCount(); |
| 1054 | if (argc == 0) |
| 1055 | { |
| 1056 | if (!m_command_byte.GetOptionValue().OptionWasSet()) |
| 1057 | { |
| 1058 | result.AppendError ("the --command option must be set to a valid command byte"); |
| 1059 | result.SetStatus (eReturnStatusFailed); |
| 1060 | } |
| 1061 | else |
| 1062 | { |
| 1063 | const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0); |
| 1064 | if (command_byte > 0 && command_byte <= UINT8_MAX) |
| 1065 | { |
| 1066 | ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 1067 | if (process) |
| 1068 | { |
| 1069 | const StateType state = process->GetState(); |
| 1070 | |
| 1071 | if (StateIsStoppedState (state, true)) |
| 1072 | { |
| 1073 | std::vector<uint8_t> payload_bytes; |
| 1074 | const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue(); |
| 1075 | if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0]) |
| 1076 | { |
| 1077 | StringExtractor extractor(ascii_hex_bytes_cstr); |
| 1078 | const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size(); |
| 1079 | if (ascii_hex_bytes_cstr_len & 1) |
| 1080 | { |
| 1081 | result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr); |
| 1082 | result.SetStatus (eReturnStatusFailed); |
| 1083 | return false; |
| 1084 | } |
| 1085 | payload_bytes.resize(ascii_hex_bytes_cstr_len/2); |
| 1086 | if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size()) |
| 1087 | { |
| 1088 | result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr); |
| 1089 | result.SetStatus (eReturnStatusFailed); |
| 1090 | return false; |
| 1091 | } |
| 1092 | } |
| 1093 | Error error; |
| 1094 | DataExtractor reply; |
| 1095 | process->GetCommunication().SendRawRequest (command_byte, |
| 1096 | payload_bytes.empty() ? NULL : payload_bytes.data(), |
| 1097 | payload_bytes.size(), |
| 1098 | reply, |
| 1099 | error); |
| 1100 | |
| 1101 | if (error.Success()) |
| 1102 | { |
| 1103 | // Copy the binary bytes into a hex ASCII string for the result |
| 1104 | StreamString packet; |
| 1105 | packet.PutBytesAsRawHex8(reply.GetDataStart(), |
| 1106 | reply.GetByteSize(), |
| 1107 | lldb::endian::InlHostByteOrder(), |
| 1108 | lldb::endian::InlHostByteOrder()); |
| 1109 | result.AppendMessage(packet.GetString().c_str()); |
| 1110 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 1111 | return true; |
| 1112 | } |
| 1113 | else |
| 1114 | { |
| 1115 | const char *error_cstr = error.AsCString(); |
| 1116 | if (error_cstr && error_cstr[0]) |
| 1117 | result.AppendError (error_cstr); |
| 1118 | else |
| 1119 | result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError()); |
| 1120 | result.SetStatus (eReturnStatusFailed); |
| 1121 | return false; |
| 1122 | } |
| 1123 | } |
| 1124 | else |
| 1125 | { |
| 1126 | result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state)); |
| 1127 | result.SetStatus (eReturnStatusFailed); |
| 1128 | } |
| 1129 | } |
| 1130 | else |
| 1131 | { |
| 1132 | result.AppendError ("invalid process"); |
| 1133 | result.SetStatus (eReturnStatusFailed); |
| 1134 | } |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1138 | result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte); |
| Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 1139 | result.SetStatus (eReturnStatusFailed); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str()); |
| 1146 | result.SetStatus (eReturnStatusFailed); |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | }; |
| 1151 | |
| 1152 | class CommandObjectProcessKDPPacket : public CommandObjectMultiword |
| 1153 | { |
| 1154 | private: |
| 1155 | |
| 1156 | public: |
| 1157 | CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) : |
| 1158 | CommandObjectMultiword (interpreter, |
| 1159 | "process plugin packet", |
| 1160 | "Commands that deal with KDP remote packets.", |
| 1161 | NULL) |
| 1162 | { |
| 1163 | LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter))); |
| 1164 | } |
| 1165 | |
| 1166 | ~CommandObjectProcessKDPPacket () |
| 1167 | { |
| 1168 | } |
| 1169 | }; |
| 1170 | |
| 1171 | class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword |
| 1172 | { |
| 1173 | public: |
| 1174 | CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) : |
| 1175 | CommandObjectMultiword (interpreter, |
| 1176 | "process plugin", |
| 1177 | "A set of commands for operating on a ProcessKDP process.", |
| 1178 | "process plugin <subcommand> [<subcommand-options>]") |
| 1179 | { |
| 1180 | LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter))); |
| 1181 | } |
| 1182 | |
| 1183 | ~CommandObjectMultiwordProcessKDP () |
| 1184 | { |
| 1185 | } |
| 1186 | }; |
| 1187 | |
| 1188 | CommandObject * |
| 1189 | ProcessKDP::GetPluginCommandObject() |
| 1190 | { |
| 1191 | if (!m_command_sp) |
| 1192 | m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter())); |
| 1193 | return m_command_sp.get(); |
| 1194 | } |
| 1195 | |