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