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