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