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