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