Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ProcessGDBRemote.cpp ------------------------------------*- C++ -*-===// |
| 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 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 10 | #include "lldb/Host/Config.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | #include <errno.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 15 | #ifndef LLDB_DISABLE_POSIX |
Sean Callanan | 224f6f5 | 2012-07-19 18:07:36 +0000 | [diff] [blame] | 16 | #include <netinet/in.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include <sys/mman.h> // for mmap |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 18 | #include <sys/socket.h> |
Zachary Turner | bd22bf2 | 2016-08-12 16:52:31 +0000 | [diff] [blame] | 19 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
Stephen Wilson | dc91686 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 22 | #include <time.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | // C++ Includes |
| 25 | #include <algorithm> |
| 26 | #include <map> |
Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame] | 27 | #include <mutex> |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 28 | #include <sstream> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 30 | #include "lldb/Breakpoint/Watchpoint.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Core/ArchSpec.h" |
| 32 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 34 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | #include "lldb/Core/PluginManager.h" |
| 36 | #include "lldb/Core/State.h" |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 37 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | #include "lldb/Core/StreamString.h" |
| 39 | #include "lldb/Core/Timer.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 40 | #include "lldb/Core/Value.h" |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 41 | #include "lldb/DataFormatters/FormatManager.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 43 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 44 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 45 | #include "lldb/Host/HostThread.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 46 | #include "lldb/Host/StringConvert.h" |
Jason Molenda | d1fae14 | 2012-09-29 08:03:33 +0000 | [diff] [blame] | 47 | #include "lldb/Host/Symbols.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 48 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 50 | #include "lldb/Host/XML.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | #include "lldb/Interpreter/Args.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 52 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 53 | #include "lldb/Interpreter/CommandObject.h" |
| 54 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 55 | #include "lldb/Interpreter/CommandReturnObject.h" |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 56 | #include "lldb/Interpreter/OptionGroupBoolean.h" |
| 57 | #include "lldb/Interpreter/OptionGroupUInt64.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | #include "lldb/Interpreter/OptionValueProperties.h" |
| 59 | #include "lldb/Interpreter/Options.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 60 | #include "lldb/Interpreter/Property.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | #include "lldb/Symbol/ObjectFile.h" |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 62 | #include "lldb/Target/ABI.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | #include "lldb/Target/DynamicLoader.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | #include "lldb/Target/Target.h" |
| 66 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 67 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 68 | #include "lldb/Utility/CleanUp.h" |
Jason Molenda | a34a0c6 | 2010-06-09 21:28:42 +0000 | [diff] [blame] | 69 | #include "lldb/Utility/PseudoTerminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 71 | // Project includes |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | #include "GDBRemoteRegisterContext.h" |
| 73 | #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 74 | #include "Plugins/Process/Utility/GDBRemoteSignals.h" |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 75 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Jason Molenda | c42d243 | 2012-07-25 03:40:06 +0000 | [diff] [blame] | 76 | #include "Plugins/Process/Utility/StopInfoMachException.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | #include "ProcessGDBRemote.h" |
| 78 | #include "ProcessGDBRemoteLog.h" |
| 79 | #include "ThreadGDBRemote.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | #include "Utility/StringExtractorGDBRemote.h" |
| 81 | #include "lldb/Host/Host.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 82 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/StringSwitch.h" |
| 84 | #include "llvm/Support/raw_ostream.h" |
| 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | #define DEBUGSERVER_BASENAME "debugserver" |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 87 | using namespace lldb; |
| 88 | using namespace lldb_private; |
| 89 | using namespace lldb_private::process_gdb_remote; |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | namespace lldb { |
| 92 | // Provide a function that can easily dump the packet history if we know a |
| 93 | // ProcessGDBRemote * value (which we can get from logs or from debugging). |
| 94 | // We need the function in the lldb namespace so it makes it into the final |
| 95 | // executable since the LLDB shared library only exports stuff in the lldb |
| 96 | // namespace. This allows you to attach with a debugger and call this |
| 97 | // function and get the packet history dumped to a file. |
| 98 | void DumpProcessGDBRemotePacketHistory(void *p, const char *path) { |
| 99 | StreamFile strm; |
| 100 | Error error(strm.GetFile().Open(path, File::eOpenOptionWrite | |
| 101 | File::eOpenOptionCanCreate)); |
| 102 | if (error.Success()) |
| 103 | ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory(strm); |
| 104 | } |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 107 | namespace { |
Steve Pucci | 5ec012d | 2014-01-16 22:18:14 +0000 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | static PropertyDefinition g_properties[] = { |
| 110 | {"packet-timeout", OptionValue::eTypeUInt64, true, 1, NULL, NULL, |
| 111 | "Specify the default packet timeout in seconds."}, |
| 112 | {"target-definition-file", OptionValue::eTypeFileSpec, true, 0, NULL, NULL, |
| 113 | "The file that provides the description for remote target registers."}, |
| 114 | {NULL, OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL}}; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | enum { ePropertyPacketTimeout, ePropertyTargetDefinitionFile }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | class PluginProperties : public Properties { |
| 119 | public: |
| 120 | static ConstString GetSettingName() { |
| 121 | return ProcessGDBRemote::GetPluginNameStatic(); |
| 122 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | PluginProperties() : Properties() { |
| 125 | m_collection_sp.reset(new OptionValueProperties(GetSettingName())); |
| 126 | m_collection_sp->Initialize(g_properties); |
| 127 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | virtual ~PluginProperties() {} |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | uint64_t GetPacketTimeout() { |
| 132 | const uint32_t idx = ePropertyPacketTimeout; |
| 133 | return m_collection_sp->GetPropertyAtIndexAsUInt64( |
| 134 | NULL, idx, g_properties[idx].default_uint_value); |
| 135 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | bool SetPacketTimeout(uint64_t timeout) { |
| 138 | const uint32_t idx = ePropertyPacketTimeout; |
| 139 | return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout); |
| 140 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | FileSpec GetTargetDefinitionFile() const { |
| 143 | const uint32_t idx = ePropertyTargetDefinitionFile; |
| 144 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); |
| 145 | } |
| 146 | }; |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | static const ProcessKDPPropertiesSP &GetGlobalPluginProperties() { |
| 151 | static ProcessKDPPropertiesSP g_settings_sp; |
| 152 | if (!g_settings_sp) |
| 153 | g_settings_sp.reset(new PluginProperties()); |
| 154 | return g_settings_sp; |
| 155 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 156 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 157 | } // anonymous namespace end |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 158 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 159 | // TODO Randomly assigning a port is unsafe. We should get an unused |
| 160 | // ephemeral port from the kernel and make sure we reserve it before passing |
| 161 | // it to debugserver. |
| 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | #if defined(__APPLE__) |
| 164 | #define LOW_PORT (IPPORT_RESERVED) |
| 165 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 166 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | #define LOW_PORT (1024u) |
| 168 | #define HIGH_PORT (49151u) |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | #if defined(__APPLE__) && \ |
| 172 | (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 173 | static bool rand_initialized = false; |
| 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | static inline uint16_t get_random_port() { |
| 176 | if (!rand_initialized) { |
| 177 | time_t seed = time(NULL); |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 178 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | rand_initialized = true; |
| 180 | srand(seed); |
| 181 | } |
| 182 | return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT; |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 183 | } |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 184 | #endif |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | ConstString ProcessGDBRemote::GetPluginNameStatic() { |
| 187 | static ConstString g_name("gdb-remote"); |
| 188 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | const char *ProcessGDBRemote::GetPluginDescriptionStatic() { |
| 192 | return "GDB Remote protocol based debugging plug-in."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | void ProcessGDBRemote::Terminate() { |
| 196 | PluginManager::UnregisterPlugin(ProcessGDBRemote::CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 199 | lldb::ProcessSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | ProcessGDBRemote::CreateInstance(lldb::TargetSP target_sp, |
| 201 | ListenerSP listener_sp, |
| 202 | const FileSpec *crash_file_path) { |
| 203 | lldb::ProcessSP process_sp; |
| 204 | if (crash_file_path == NULL) |
| 205 | process_sp.reset(new ProcessGDBRemote(target_sp, listener_sp)); |
| 206 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | bool ProcessGDBRemote::CanDebug(lldb::TargetSP target_sp, |
| 210 | bool plugin_specified_by_name) { |
| 211 | if (plugin_specified_by_name) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 212 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | |
| 214 | // For now we are just making sure the file exists for a given module |
| 215 | Module *exe_module = target_sp->GetExecutableModulePointer(); |
| 216 | if (exe_module) { |
| 217 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 218 | // We can't debug core files... |
| 219 | switch (exe_objfile->GetType()) { |
| 220 | case ObjectFile::eTypeInvalid: |
| 221 | case ObjectFile::eTypeCoreFile: |
| 222 | case ObjectFile::eTypeDebugInfo: |
| 223 | case ObjectFile::eTypeObjectFile: |
| 224 | case ObjectFile::eTypeSharedLibrary: |
| 225 | case ObjectFile::eTypeStubLibrary: |
| 226 | case ObjectFile::eTypeJIT: |
| 227 | return false; |
| 228 | case ObjectFile::eTypeExecutable: |
| 229 | case ObjectFile::eTypeDynamicLinker: |
| 230 | case ObjectFile::eTypeUnknown: |
| 231 | break; |
| 232 | } |
| 233 | return exe_module->GetFileSpec().Exists(); |
| 234 | } |
| 235 | // However, if there is no executable module, we return true since we might be |
| 236 | // preparing to attach. |
| 237 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 240 | //---------------------------------------------------------------------- |
| 241 | // ProcessGDBRemote constructor |
| 242 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | ProcessGDBRemote::ProcessGDBRemote(lldb::TargetSP target_sp, |
| 244 | ListenerSP listener_sp) |
| 245 | : Process(target_sp, listener_sp), m_flags(0), m_gdb_comm(), |
| 246 | m_debugserver_pid(LLDB_INVALID_PROCESS_ID), m_last_stop_packet_mutex(), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 247 | m_register_info(), |
| 248 | m_async_broadcaster(NULL, "lldb.process.gdb-remote.async-broadcaster"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | m_async_listener_sp( |
| 250 | Listener::MakeListener("lldb.process.gdb-remote.async-listener")), |
| 251 | m_async_thread_state_mutex(), m_thread_ids(), m_thread_pcs(), |
| 252 | m_jstopinfo_sp(), m_jthreadsinfo_sp(), m_continue_c_tids(), |
| 253 | m_continue_C_tids(), m_continue_s_tids(), m_continue_S_tids(), |
| 254 | m_max_memory_size(0), m_remote_stub_max_memory_size(0), |
| 255 | m_addr_to_mmap_size(), m_thread_create_bp_sp(), |
| 256 | m_waiting_for_attach(false), m_destroy_tried_resuming(false), |
| 257 | m_command_sp(), m_breakpoint_pc_offset(0), |
| 258 | m_initial_tid(LLDB_INVALID_THREAD_ID) { |
| 259 | m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadShouldExit, |
| 260 | "async thread should exit"); |
| 261 | m_async_broadcaster.SetEventName(eBroadcastBitAsyncContinue, |
| 262 | "async thread continue"); |
| 263 | m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadDidExit, |
| 264 | "async thread did exit"); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 265 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_ASYNC)); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | const uint32_t async_event_mask = |
| 269 | eBroadcastBitAsyncContinue | eBroadcastBitAsyncThreadShouldExit; |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | if (m_async_listener_sp->StartListeningForEvents( |
| 272 | &m_async_broadcaster, async_event_mask) != async_event_mask) { |
| 273 | if (log) |
| 274 | log->Printf("ProcessGDBRemote::%s failed to listen for " |
| 275 | "m_async_broadcaster events", |
| 276 | __FUNCTION__); |
| 277 | } |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 278 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 279 | const uint32_t gdb_event_mask = |
| 280 | Communication::eBroadcastBitReadThreadDidExit | |
| 281 | GDBRemoteCommunication::eBroadcastBitGdbReadThreadGotNotify; |
| 282 | if (m_async_listener_sp->StartListeningForEvents( |
| 283 | &m_gdb_comm, gdb_event_mask) != gdb_event_mask) { |
| 284 | if (log) |
| 285 | log->Printf("ProcessGDBRemote::%s failed to listen for m_gdb_comm events", |
| 286 | __FUNCTION__); |
| 287 | } |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 288 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 289 | const uint64_t timeout_seconds = |
| 290 | GetGlobalPluginProperties()->GetPacketTimeout(); |
| 291 | if (timeout_seconds > 0) |
| 292 | m_gdb_comm.SetPacketTimeout(timeout_seconds); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 295 | //---------------------------------------------------------------------- |
| 296 | // Destructor |
| 297 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | ProcessGDBRemote::~ProcessGDBRemote() { |
| 299 | // m_mach_process.UnregisterNotificationCallbacks (this); |
| 300 | Clear(); |
| 301 | // We need to call finalize on the process before destroying ourselves |
| 302 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 303 | // destruct this class, then Process::~Process() might have problems |
| 304 | // trying to fully destroy the broadcaster. |
| 305 | Finalize(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 306 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | // The general Finalize is going to try to destroy the process and that SHOULD |
| 308 | // shut down the async thread. However, if we don't kill it it will get |
| 309 | // stranded and |
| 310 | // its connection will go away so when it wakes up it will crash. So kill it |
| 311 | // for sure here. |
| 312 | StopAsyncThread(); |
| 313 | KillDebugserverProcess(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | //---------------------------------------------------------------------- |
| 317 | // PluginInterface |
| 318 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 319 | ConstString ProcessGDBRemote::GetPluginName() { return GetPluginNameStatic(); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 320 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 321 | uint32_t ProcessGDBRemote::GetPluginVersion() { return 1; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | bool ProcessGDBRemote::ParsePythonTargetDefinition( |
| 324 | const FileSpec &target_definition_fspec) { |
| 325 | ScriptInterpreter *interpreter = |
| 326 | GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
| 327 | Error error; |
| 328 | StructuredData::ObjectSP module_object_sp( |
| 329 | interpreter->LoadPluginModule(target_definition_fspec, error)); |
| 330 | if (module_object_sp) { |
| 331 | StructuredData::DictionarySP target_definition_sp( |
| 332 | interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), |
| 333 | "gdb-server-target-definition", error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 334 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | if (target_definition_sp) { |
| 336 | StructuredData::ObjectSP target_object( |
| 337 | target_definition_sp->GetValueForKey("host-info")); |
| 338 | if (target_object) { |
| 339 | if (auto host_info_dict = target_object->GetAsDictionary()) { |
| 340 | StructuredData::ObjectSP triple_value = |
| 341 | host_info_dict->GetValueForKey("triple"); |
| 342 | if (auto triple_string_value = triple_value->GetAsString()) { |
| 343 | std::string triple_string = triple_string_value->GetValue(); |
| 344 | ArchSpec host_arch(triple_string.c_str()); |
| 345 | if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture())) { |
| 346 | GetTarget().SetArchitecture(host_arch); |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 347 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | } |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 349 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | } |
| 351 | m_breakpoint_pc_offset = 0; |
| 352 | StructuredData::ObjectSP breakpoint_pc_offset_value = |
| 353 | target_definition_sp->GetValueForKey("breakpoint-pc-offset"); |
| 354 | if (breakpoint_pc_offset_value) { |
| 355 | if (auto breakpoint_pc_int_value = |
| 356 | breakpoint_pc_offset_value->GetAsInteger()) |
| 357 | m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue(); |
| 358 | } |
| 359 | |
| 360 | if (m_register_info.SetRegisterInfo(*target_definition_sp, |
| 361 | GetTarget().GetArchitecture()) > 0) { |
| 362 | return true; |
| 363 | } |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 364 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | } |
| 366 | return false; |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | // If the remote stub didn't give us eh_frame or DWARF register numbers for a |
| 370 | // register, |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 371 | // see if the ABI can provide them. |
| 372 | // DWARF and eh_frame register numbers are defined as a part of the ABI. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 373 | static void AugmentRegisterInfoViaABI(RegisterInfo ®_info, |
| 374 | ConstString reg_name, ABISP abi_sp) { |
| 375 | if (reg_info.kinds[eRegisterKindEHFrame] == LLDB_INVALID_REGNUM || |
| 376 | reg_info.kinds[eRegisterKindDWARF] == LLDB_INVALID_REGNUM) { |
| 377 | if (abi_sp) { |
| 378 | RegisterInfo abi_reg_info; |
| 379 | if (abi_sp->GetRegisterInfoByName(reg_name, abi_reg_info)) { |
| 380 | if (reg_info.kinds[eRegisterKindEHFrame] == LLDB_INVALID_REGNUM && |
| 381 | abi_reg_info.kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM) { |
| 382 | reg_info.kinds[eRegisterKindEHFrame] = |
| 383 | abi_reg_info.kinds[eRegisterKindEHFrame]; |
| 384 | } |
| 385 | if (reg_info.kinds[eRegisterKindDWARF] == LLDB_INVALID_REGNUM && |
| 386 | abi_reg_info.kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM) { |
| 387 | reg_info.kinds[eRegisterKindDWARF] = |
| 388 | abi_reg_info.kinds[eRegisterKindDWARF]; |
| 389 | } |
| 390 | if (reg_info.kinds[eRegisterKindGeneric] == LLDB_INVALID_REGNUM && |
| 391 | abi_reg_info.kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) { |
| 392 | reg_info.kinds[eRegisterKindGeneric] = |
| 393 | abi_reg_info.kinds[eRegisterKindGeneric]; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static size_t SplitCommaSeparatedRegisterNumberString( |
| 401 | const llvm::StringRef &comma_separated_regiter_numbers, |
| 402 | std::vector<uint32_t> ®nums, int base) { |
| 403 | regnums.clear(); |
| 404 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 405 | value_pair.second = comma_separated_regiter_numbers; |
| 406 | do { |
| 407 | value_pair = value_pair.second.split(','); |
| 408 | if (!value_pair.first.empty()) { |
| 409 | uint32_t reg = StringConvert::ToUInt32(value_pair.first.str().c_str(), |
| 410 | LLDB_INVALID_REGNUM, base); |
| 411 | if (reg != LLDB_INVALID_REGNUM) |
| 412 | regnums.push_back(reg); |
| 413 | } |
| 414 | } while (!value_pair.second.empty()); |
| 415 | return regnums.size(); |
| 416 | } |
| 417 | |
| 418 | void ProcessGDBRemote::BuildDynamicRegisterInfo(bool force) { |
| 419 | if (!force && m_register_info.GetNumRegisters() > 0) |
| 420 | return; |
| 421 | |
| 422 | m_register_info.Clear(); |
| 423 | |
| 424 | // Check if qHostInfo specified a specific packet timeout for this connection. |
| 425 | // If so then lets update our setting so the user knows what the timeout is |
| 426 | // and can see it. |
| 427 | const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout(); |
| 428 | if (host_packet_timeout) { |
| 429 | GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout); |
| 430 | } |
| 431 | |
| 432 | // Register info search order: |
| 433 | // 1 - Use the target definition python file if one is specified. |
| 434 | // 2 - If the target definition doesn't have any of the info from the |
| 435 | // target.xml (registers) then proceed to read the target.xml. |
| 436 | // 3 - Fall back on the qRegisterInfo packets. |
| 437 | |
| 438 | FileSpec target_definition_fspec = |
| 439 | GetGlobalPluginProperties()->GetTargetDefinitionFile(); |
| 440 | if (!target_definition_fspec.Exists()) { |
| 441 | // If the filename doesn't exist, it may be a ~ not having been expanded - |
| 442 | // try to resolve it. |
| 443 | target_definition_fspec.ResolvePath(); |
| 444 | } |
| 445 | if (target_definition_fspec) { |
| 446 | // See if we can get register definitions from a python file |
| 447 | if (ParsePythonTargetDefinition(target_definition_fspec)) { |
| 448 | return; |
| 449 | } else { |
| 450 | StreamSP stream_sp = GetTarget().GetDebugger().GetAsyncOutputStream(); |
| 451 | stream_sp->Printf("ERROR: target description file %s failed to parse.\n", |
| 452 | target_definition_fspec.GetPath().c_str()); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 457 | const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); |
| 458 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 459 | |
| 460 | // Use the process' architecture instead of the host arch, if available |
| 461 | ArchSpec arch_to_use; |
| 462 | if (remote_process_arch.IsValid()) |
| 463 | arch_to_use = remote_process_arch; |
| 464 | else |
| 465 | arch_to_use = remote_host_arch; |
| 466 | |
| 467 | if (!arch_to_use.IsValid()) |
| 468 | arch_to_use = target_arch; |
| 469 | |
| 470 | if (GetGDBServerRegisterInfo(arch_to_use)) |
| 471 | return; |
| 472 | |
| 473 | char packet[128]; |
| 474 | uint32_t reg_offset = 0; |
| 475 | uint32_t reg_num = 0; |
| 476 | for (StringExtractorGDBRemote::ResponseType response_type = |
| 477 | StringExtractorGDBRemote::eResponse; |
| 478 | response_type == StringExtractorGDBRemote::eResponse; ++reg_num) { |
| 479 | const int packet_len = |
| 480 | ::snprintf(packet, sizeof(packet), "qRegisterInfo%x", reg_num); |
| 481 | assert(packet_len < (int)sizeof(packet)); |
| 482 | StringExtractorGDBRemote response; |
| 483 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, |
| 484 | false) == |
| 485 | GDBRemoteCommunication::PacketResult::Success) { |
| 486 | response_type = response.GetResponseType(); |
| 487 | if (response_type == StringExtractorGDBRemote::eResponse) { |
| 488 | llvm::StringRef name; |
| 489 | llvm::StringRef value; |
| 490 | ConstString reg_name; |
| 491 | ConstString alt_name; |
| 492 | ConstString set_name; |
| 493 | std::vector<uint32_t> value_regs; |
| 494 | std::vector<uint32_t> invalidate_regs; |
| 495 | std::vector<uint8_t> dwarf_opcode_bytes; |
| 496 | RegisterInfo reg_info = { |
| 497 | NULL, // Name |
| 498 | NULL, // Alt name |
| 499 | 0, // byte size |
| 500 | reg_offset, // offset |
| 501 | eEncodingUint, // encoding |
| 502 | eFormatHex, // format |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 503 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 504 | LLDB_INVALID_REGNUM, // eh_frame reg num |
| 505 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 506 | LLDB_INVALID_REGNUM, // generic reg num |
| 507 | reg_num, // process plugin reg num |
| 508 | reg_num // native register number |
| 509 | }, |
| 510 | NULL, |
| 511 | NULL, |
| 512 | NULL, // Dwarf expression opcode bytes pointer |
| 513 | 0 // Dwarf expression opcode bytes length |
| 514 | }; |
| 515 | |
| 516 | while (response.GetNameColonValue(name, value)) { |
| 517 | if (name.equals("name")) { |
| 518 | reg_name.SetString(value); |
| 519 | } else if (name.equals("alt-name")) { |
| 520 | alt_name.SetString(value); |
| 521 | } else if (name.equals("bitsize")) { |
| 522 | value.getAsInteger(0, reg_info.byte_size); |
| 523 | reg_info.byte_size /= CHAR_BIT; |
| 524 | } else if (name.equals("offset")) { |
| 525 | if (value.getAsInteger(0, reg_offset)) |
| 526 | reg_offset = UINT32_MAX; |
| 527 | } else if (name.equals("encoding")) { |
| 528 | const Encoding encoding = Args::StringToEncoding(value); |
| 529 | if (encoding != eEncodingInvalid) |
| 530 | reg_info.encoding = encoding; |
| 531 | } else if (name.equals("format")) { |
| 532 | Format format = eFormatInvalid; |
| 533 | if (Args::StringToFormat(value.str().c_str(), format, NULL) |
| 534 | .Success()) |
| 535 | reg_info.format = format; |
| 536 | else { |
| 537 | reg_info.format = |
| 538 | llvm::StringSwitch<Format>(value) |
| 539 | .Case("binary", eFormatBinary) |
| 540 | .Case("decimal", eFormatDecimal) |
| 541 | .Case("hex", eFormatHex) |
| 542 | .Case("float", eFormatFloat) |
| 543 | .Case("vector-sint8", eFormatVectorOfSInt8) |
| 544 | .Case("vector-uint8", eFormatVectorOfUInt8) |
| 545 | .Case("vector-sint16", eFormatVectorOfSInt16) |
| 546 | .Case("vector-uint16", eFormatVectorOfUInt16) |
| 547 | .Case("vector-sint32", eFormatVectorOfSInt32) |
| 548 | .Case("vector-uint32", eFormatVectorOfUInt32) |
| 549 | .Case("vector-float32", eFormatVectorOfFloat32) |
Valentina Giusti | cda0ae4 | 2016-09-08 14:16:45 +0000 | [diff] [blame] | 550 | .Case("vector-uint64", eFormatVectorOfUInt64) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 551 | .Case("vector-uint128", eFormatVectorOfUInt128) |
| 552 | .Default(eFormatInvalid); |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 553 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 554 | } else if (name.equals("set")) { |
| 555 | set_name.SetString(value); |
| 556 | } else if (name.equals("gcc") || name.equals("ehframe")) { |
| 557 | if (value.getAsInteger(0, reg_info.kinds[eRegisterKindEHFrame])) |
| 558 | reg_info.kinds[eRegisterKindEHFrame] = LLDB_INVALID_REGNUM; |
| 559 | } else if (name.equals("dwarf")) { |
| 560 | if (value.getAsInteger(0, reg_info.kinds[eRegisterKindDWARF])) |
| 561 | reg_info.kinds[eRegisterKindDWARF] = LLDB_INVALID_REGNUM; |
| 562 | } else if (name.equals("generic")) { |
| 563 | reg_info.kinds[eRegisterKindGeneric] = |
| 564 | Args::StringToGenericRegister(value); |
| 565 | } else if (name.equals("container-regs")) { |
| 566 | SplitCommaSeparatedRegisterNumberString(value, value_regs, 16); |
| 567 | } else if (name.equals("invalidate-regs")) { |
| 568 | SplitCommaSeparatedRegisterNumberString(value, invalidate_regs, 16); |
| 569 | } else if (name.equals("dynamic_size_dwarf_expr_bytes")) { |
| 570 | size_t dwarf_opcode_len = value.size() / 2; |
| 571 | assert(dwarf_opcode_len > 0); |
| 572 | |
| 573 | dwarf_opcode_bytes.resize(dwarf_opcode_len); |
| 574 | reg_info.dynamic_size_dwarf_len = dwarf_opcode_len; |
| 575 | |
| 576 | StringExtractor opcode_extractor(value); |
| 577 | uint32_t ret_val = |
| 578 | opcode_extractor.GetHexBytesAvail(dwarf_opcode_bytes); |
| 579 | assert(dwarf_opcode_len == ret_val); |
| 580 | |
| 581 | reg_info.dynamic_size_dwarf_expr_bytes = dwarf_opcode_bytes.data(); |
| 582 | } |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 583 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 584 | |
| 585 | reg_info.byte_offset = reg_offset; |
| 586 | assert(reg_info.byte_size != 0); |
| 587 | reg_offset += reg_info.byte_size; |
| 588 | if (!value_regs.empty()) { |
| 589 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 590 | reg_info.value_regs = value_regs.data(); |
| 591 | } |
| 592 | if (!invalidate_regs.empty()) { |
| 593 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 594 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 595 | } |
| 596 | |
| 597 | // We have to make a temporary ABI here, and not use the GetABI because |
| 598 | // this code |
| 599 | // gets called in DidAttach, when the target architecture (and |
| 600 | // consequently the ABI we'll get from |
| 601 | // the process) may be wrong. |
| 602 | ABISP abi_to_use = ABI::FindPlugin(arch_to_use); |
| 603 | |
| 604 | AugmentRegisterInfoViaABI(reg_info, reg_name, abi_to_use); |
| 605 | |
| 606 | m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 607 | } else { |
| 608 | break; // ensure exit before reg_num is incremented |
| 609 | } |
| 610 | } else { |
| 611 | break; |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 612 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (m_register_info.GetNumRegisters() > 0) { |
| 616 | m_register_info.Finalize(GetTarget().GetArchitecture()); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | // We didn't get anything if the accumulated reg_num is zero. See if we are |
| 621 | // debugging ARM and fill with a hard coded register set until we can get an |
| 622 | // updated debugserver down on the devices. |
| 623 | // On the other hand, if the accumulated reg_num is positive, see if we can |
| 624 | // add composite registers to the existing primordial ones. |
| 625 | bool from_scratch = (m_register_info.GetNumRegisters() == 0); |
| 626 | |
| 627 | if (!target_arch.IsValid()) { |
| 628 | if (arch_to_use.IsValid() && |
| 629 | (arch_to_use.GetMachine() == llvm::Triple::arm || |
| 630 | arch_to_use.GetMachine() == llvm::Triple::thumb) && |
| 631 | arch_to_use.GetTriple().getVendor() == llvm::Triple::Apple) |
| 632 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 633 | } else if (target_arch.GetMachine() == llvm::Triple::arm || |
| 634 | target_arch.GetMachine() == llvm::Triple::thumb) { |
| 635 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 636 | } |
| 637 | |
| 638 | // At this point, we can finalize our register info. |
| 639 | m_register_info.Finalize(GetTarget().GetArchitecture()); |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 642 | Error ProcessGDBRemote::WillLaunch(Module *module) { |
| 643 | return WillLaunchOrAttach(); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 646 | Error ProcessGDBRemote::WillAttachToProcessWithID(lldb::pid_t pid) { |
| 647 | return WillLaunchOrAttach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 650 | Error ProcessGDBRemote::WillAttachToProcessWithName(const char *process_name, |
| 651 | bool wait_for_launch) { |
| 652 | return WillLaunchOrAttach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 655 | Error ProcessGDBRemote::DoConnectRemote(Stream *strm, const char *remote_url) { |
| 656 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 657 | Error error(WillLaunchOrAttach()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 658 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 659 | if (error.Fail()) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 660 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | |
| 662 | error = ConnectToDebugserver(remote_url); |
| 663 | |
| 664 | if (error.Fail()) |
| 665 | return error; |
| 666 | StartAsyncThread(); |
| 667 | |
| 668 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); |
| 669 | if (pid == LLDB_INVALID_PROCESS_ID) { |
| 670 | // We don't have a valid process ID, so note that we are connected |
| 671 | // and could now request to launch or attach, or get remote process |
| 672 | // listings... |
| 673 | SetPrivateState(eStateConnected); |
| 674 | } else { |
| 675 | // We have a valid process |
| 676 | SetID(pid); |
| 677 | GetThreadList(); |
| 678 | StringExtractorGDBRemote response; |
| 679 | if (m_gdb_comm.GetStopReply(response)) { |
| 680 | SetLastStopPacket(response); |
| 681 | |
| 682 | // '?' Packets must be handled differently in non-stop mode |
| 683 | if (GetTarget().GetNonStopModeEnabled()) |
| 684 | HandleStopReplySequence(); |
| 685 | |
| 686 | Target &target = GetTarget(); |
| 687 | if (!target.GetArchitecture().IsValid()) { |
| 688 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) { |
| 689 | target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 690 | } else { |
| 691 | target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | const StateType state = SetThreadStopInfo(response); |
| 696 | if (state != eStateInvalid) { |
| 697 | SetPrivateState(state); |
| 698 | } else |
| 699 | error.SetErrorStringWithFormat("Process %" PRIu64 |
| 700 | " was reported after connecting to " |
| 701 | "'%s', but state was not stopped: %s", |
| 702 | pid, remote_url, StateAsCString(state)); |
| 703 | } else |
| 704 | error.SetErrorStringWithFormat("Process %" PRIu64 |
| 705 | " was reported after connecting to '%s', " |
| 706 | "but no stop reply packet was received", |
| 707 | pid, remote_url); |
| 708 | } |
| 709 | |
| 710 | if (log) |
| 711 | log->Printf("ProcessGDBRemote::%s pid %" PRIu64 |
| 712 | ": normalizing target architecture initial triple: %s " |
| 713 | "(GetTarget().GetArchitecture().IsValid() %s, " |
| 714 | "m_gdb_comm.GetHostArchitecture().IsValid(): %s)", |
| 715 | __FUNCTION__, GetID(), |
| 716 | GetTarget().GetArchitecture().GetTriple().getTriple().c_str(), |
| 717 | GetTarget().GetArchitecture().IsValid() ? "true" : "false", |
| 718 | m_gdb_comm.GetHostArchitecture().IsValid() ? "true" : "false"); |
| 719 | |
| 720 | if (error.Success() && !GetTarget().GetArchitecture().IsValid() && |
| 721 | m_gdb_comm.GetHostArchitecture().IsValid()) { |
| 722 | // Prefer the *process'* architecture over that of the *host*, if available. |
| 723 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 724 | GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 725 | else |
| 726 | GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 727 | } |
| 728 | |
| 729 | if (log) |
| 730 | log->Printf("ProcessGDBRemote::%s pid %" PRIu64 |
| 731 | ": normalized target architecture triple: %s", |
| 732 | __FUNCTION__, GetID(), |
| 733 | GetTarget().GetArchitecture().GetTriple().getTriple().c_str()); |
| 734 | |
| 735 | if (error.Success()) { |
| 736 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 737 | if (platform_sp && platform_sp->IsConnected()) |
| 738 | SetUnixSignals(platform_sp->GetUnixSignals()); |
| 739 | else |
| 740 | SetUnixSignals(UnixSignals::Create(GetTarget().GetArchitecture())); |
| 741 | } |
| 742 | |
| 743 | return error; |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | Error ProcessGDBRemote::WillLaunchOrAttach() { |
| 747 | Error error; |
| 748 | m_stdio_communication.Clear(); |
| 749 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | //---------------------------------------------------------------------- |
| 753 | // Process Control |
| 754 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 755 | Error ProcessGDBRemote::DoLaunch(Module *exe_module, |
| 756 | ProcessLaunchInfo &launch_info) { |
| 757 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 758 | Error error; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 759 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 760 | if (log) |
| 761 | log->Printf("ProcessGDBRemote::%s() entered", __FUNCTION__); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 763 | uint32_t launch_flags = launch_info.GetFlags().Get(); |
| 764 | FileSpec stdin_file_spec{}; |
| 765 | FileSpec stdout_file_spec{}; |
| 766 | FileSpec stderr_file_spec{}; |
| 767 | FileSpec working_dir = launch_info.GetWorkingDirectory(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 768 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 769 | const FileAction *file_action; |
| 770 | file_action = launch_info.GetFileActionForFD(STDIN_FILENO); |
| 771 | if (file_action) { |
| 772 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
| 773 | stdin_file_spec = file_action->GetFileSpec(); |
| 774 | } |
| 775 | file_action = launch_info.GetFileActionForFD(STDOUT_FILENO); |
| 776 | if (file_action) { |
| 777 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
| 778 | stdout_file_spec = file_action->GetFileSpec(); |
| 779 | } |
| 780 | file_action = launch_info.GetFileActionForFD(STDERR_FILENO); |
| 781 | if (file_action) { |
| 782 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
| 783 | stderr_file_spec = file_action->GetFileSpec(); |
| 784 | } |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 785 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 786 | if (log) { |
| 787 | if (stdin_file_spec || stdout_file_spec || stderr_file_spec) |
| 788 | log->Printf("ProcessGDBRemote::%s provided with STDIO paths via " |
| 789 | "launch_info: stdin=%s, stdout=%s, stderr=%s", |
| 790 | __FUNCTION__, |
| 791 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 792 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 793 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 794 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 795 | log->Printf("ProcessGDBRemote::%s no STDIO paths given via launch_info", |
| 796 | __FUNCTION__); |
| 797 | } |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 798 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 799 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
| 800 | if (stdin_file_spec || disable_stdio) { |
| 801 | // the inferior will be reading stdin from the specified file |
| 802 | // or stdio is completely disabled |
| 803 | m_stdin_forward = false; |
| 804 | } else { |
| 805 | m_stdin_forward = true; |
| 806 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 807 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 808 | // ::LogSetBitMask (GDBR_LOG_DEFAULT); |
| 809 | // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | |
| 810 | // LLDB_LOG_OPTION_PREPEND_TIMESTAMP | |
| 811 | // LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); |
| 812 | // ::LogSetLogFile ("/dev/stdout"); |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | ObjectFile *object_file = exe_module->GetObjectFile(); |
| 815 | if (object_file) { |
| 816 | error = EstablishConnectionIfNeeded(launch_info); |
| 817 | if (error.Success()) { |
| 818 | lldb_utility::PseudoTerminal pty; |
| 819 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 820 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 821 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 822 | if (disable_stdio) { |
| 823 | // set to /dev/null unless redirected to a file above |
| 824 | if (!stdin_file_spec) |
| 825 | stdin_file_spec.SetFile(FileSystem::DEV_NULL, false); |
| 826 | if (!stdout_file_spec) |
| 827 | stdout_file_spec.SetFile(FileSystem::DEV_NULL, false); |
| 828 | if (!stderr_file_spec) |
| 829 | stderr_file_spec.SetFile(FileSystem::DEV_NULL, false); |
| 830 | } else if (platform_sp && platform_sp->IsHost()) { |
| 831 | // If the debugserver is local and we aren't disabling STDIO, lets use |
| 832 | // a pseudo terminal to instead of relying on the 'O' packets for stdio |
| 833 | // since 'O' packets can really slow down debugging if the inferior |
| 834 | // does a lot of output. |
| 835 | if ((!stdin_file_spec || !stdout_file_spec || !stderr_file_spec) && |
| 836 | pty.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, NULL, 0)) { |
| 837 | FileSpec slave_name{pty.GetSlaveName(NULL, 0), false}; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 838 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | if (!stdin_file_spec) |
| 840 | stdin_file_spec = slave_name; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 841 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | if (!stdout_file_spec) |
| 843 | stdout_file_spec = slave_name; |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 844 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 845 | if (!stderr_file_spec) |
| 846 | stderr_file_spec = slave_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 847 | } |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 848 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 849 | log->Printf( |
| 850 | "ProcessGDBRemote::%s adjusted STDIO paths for local platform " |
| 851 | "(IsHost() is true) using slave: stdin=%s, stdout=%s, stderr=%s", |
| 852 | __FUNCTION__, |
| 853 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 854 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 855 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
| 856 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 858 | if (log) |
| 859 | log->Printf("ProcessGDBRemote::%s final STDIO paths after all " |
| 860 | "adjustments: stdin=%s, stdout=%s, stderr=%s", |
| 861 | __FUNCTION__, |
| 862 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 863 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 864 | stderr_file_spec ? stderr_file_spec.GetCString() |
| 865 | : "<null>"); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 866 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 867 | if (stdin_file_spec) |
| 868 | m_gdb_comm.SetSTDIN(stdin_file_spec); |
| 869 | if (stdout_file_spec) |
| 870 | m_gdb_comm.SetSTDOUT(stdout_file_spec); |
| 871 | if (stderr_file_spec) |
| 872 | m_gdb_comm.SetSTDERR(stderr_file_spec); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 873 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 874 | m_gdb_comm.SetDisableASLR(launch_flags & eLaunchFlagDisableASLR); |
| 875 | m_gdb_comm.SetDetachOnError(launch_flags & eLaunchFlagDetachOnError); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 876 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | m_gdb_comm.SendLaunchArchPacket( |
| 878 | GetTarget().GetArchitecture().GetArchitectureName()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 879 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 880 | const char *launch_event_data = launch_info.GetLaunchEventData(); |
| 881 | if (launch_event_data != NULL && *launch_event_data != '\0') |
| 882 | m_gdb_comm.SendLaunchEventDataPacket(launch_event_data); |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 884 | if (working_dir) { |
| 885 | m_gdb_comm.SetWorkingDir(working_dir); |
| 886 | } |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 887 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 888 | // Send the environment and the program + arguments after we connect |
| 889 | const Args &environment = launch_info.GetEnvironmentEntries(); |
| 890 | if (environment.GetArgumentCount()) { |
| 891 | size_t num_environment_entries = environment.GetArgumentCount(); |
| 892 | for (size_t i = 0; i < num_environment_entries; ++i) { |
| 893 | const char *env_entry = environment.GetArgumentAtIndex(i); |
| 894 | if (env_entry == NULL || |
| 895 | m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 896 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 899 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 900 | { |
| 901 | // Scope for the scoped timeout object |
| 902 | GDBRemoteCommunication::ScopedTimeout timeout(m_gdb_comm, 10); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 903 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 904 | int arg_packet_err = m_gdb_comm.SendArgumentsPacket(launch_info); |
| 905 | if (arg_packet_err == 0) { |
| 906 | std::string error_str; |
| 907 | if (m_gdb_comm.GetLaunchSuccess(error_str)) { |
| 908 | SetID(m_gdb_comm.GetCurrentProcessID()); |
| 909 | } else { |
| 910 | error.SetErrorString(error_str.c_str()); |
| 911 | } |
| 912 | } else { |
| 913 | error.SetErrorStringWithFormat("'A' packet returned an error: %i", |
| 914 | arg_packet_err); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | if (GetID() == LLDB_INVALID_PROCESS_ID) { |
| 919 | if (log) |
| 920 | log->Printf("failed to connect to debugserver: %s", |
| 921 | error.AsCString()); |
| 922 | KillDebugserverProcess(); |
| 923 | return error; |
| 924 | } |
| 925 | |
| 926 | StringExtractorGDBRemote response; |
| 927 | if (m_gdb_comm.GetStopReply(response)) { |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 928 | SetLastStopPacket(response); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 929 | // '?' Packets must be handled differently in non-stop mode |
| 930 | if (GetTarget().GetNonStopModeEnabled()) |
| 931 | HandleStopReplySequence(); |
| 932 | |
| 933 | const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 934 | |
| 935 | if (process_arch.IsValid()) { |
| 936 | GetTarget().MergeArchitecture(process_arch); |
| 937 | } else { |
| 938 | const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture(); |
| 939 | if (host_arch.IsValid()) |
| 940 | GetTarget().MergeArchitecture(host_arch); |
| 941 | } |
| 942 | |
| 943 | SetPrivateState(SetThreadStopInfo(response)); |
| 944 | |
| 945 | if (!disable_stdio) { |
| 946 | if (pty.GetMasterFileDescriptor() != |
| 947 | lldb_utility::PseudoTerminal::invalid_fd) |
| 948 | SetSTDIOFileDescriptor(pty.ReleaseMasterFileDescriptor()); |
| 949 | } |
| 950 | } |
| 951 | } else { |
| 952 | if (log) |
| 953 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 954 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 955 | } else { |
| 956 | // Set our user ID to an invalid process ID. |
| 957 | SetID(LLDB_INVALID_PROCESS_ID); |
| 958 | error.SetErrorStringWithFormat( |
| 959 | "failed to get object file from '%s' for arch %s", |
| 960 | exe_module->GetFileSpec().GetFilename().AsCString(), |
| 961 | exe_module->GetArchitecture().GetArchitectureName()); |
| 962 | } |
| 963 | return error; |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 966 | Error ProcessGDBRemote::ConnectToDebugserver(const char *connect_url) { |
| 967 | Error error; |
| 968 | // Only connect if we have a valid connect URL |
| 969 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 970 | |
| 971 | if (connect_url && connect_url[0]) { |
| 972 | if (log) |
| 973 | log->Printf("ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, |
| 974 | connect_url); |
| 975 | std::unique_ptr<ConnectionFileDescriptor> conn_ap( |
| 976 | new ConnectionFileDescriptor()); |
| 977 | if (conn_ap.get()) { |
| 978 | const uint32_t max_retry_count = 50; |
| 979 | uint32_t retry_count = 0; |
| 980 | while (!m_gdb_comm.IsConnected()) { |
| 981 | if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) { |
| 982 | m_gdb_comm.SetConnection(conn_ap.release()); |
| 983 | break; |
| 984 | } else if (error.WasInterrupted()) { |
| 985 | // If we were interrupted, don't keep retrying. |
| 986 | break; |
| 987 | } |
| 988 | |
| 989 | retry_count++; |
| 990 | |
| 991 | if (retry_count >= max_retry_count) |
| 992 | break; |
| 993 | |
| 994 | usleep(100000); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (!m_gdb_comm.IsConnected()) { |
| 1000 | if (error.Success()) |
| 1001 | error.SetErrorString("not connected to remote gdb server"); |
| 1002 | return error; |
| 1003 | } |
| 1004 | |
| 1005 | // Start the communications read thread so all incoming data can be |
| 1006 | // parsed into packets and queued as they arrive. |
| 1007 | if (GetTarget().GetNonStopModeEnabled()) |
| 1008 | m_gdb_comm.StartReadThread(); |
| 1009 | |
| 1010 | // We always seem to be able to open a connection to a local port |
| 1011 | // so we need to make sure we can then send data to it. If we can't |
| 1012 | // then we aren't actually connected to anything, so try and do the |
| 1013 | // handshake with the remote GDB server and make sure that goes |
| 1014 | // alright. |
| 1015 | if (!m_gdb_comm.HandshakeWithServer(&error)) { |
| 1016 | m_gdb_comm.Disconnect(); |
| 1017 | if (error.Success()) |
| 1018 | error.SetErrorString("not connected to remote gdb server"); |
| 1019 | return error; |
| 1020 | } |
| 1021 | |
| 1022 | // Send $QNonStop:1 packet on startup if required |
| 1023 | if (GetTarget().GetNonStopModeEnabled()) |
| 1024 | GetTarget().SetNonStopModeEnabled(m_gdb_comm.SetNonStopMode(true)); |
| 1025 | |
| 1026 | m_gdb_comm.GetEchoSupported(); |
| 1027 | m_gdb_comm.GetThreadSuffixSupported(); |
| 1028 | m_gdb_comm.GetListThreadsInStopReplySupported(); |
| 1029 | m_gdb_comm.GetHostInfo(); |
| 1030 | m_gdb_comm.GetVContSupported('c'); |
| 1031 | m_gdb_comm.GetVAttachOrWaitSupported(); |
| 1032 | |
| 1033 | // Ask the remote server for the default thread id |
| 1034 | if (GetTarget().GetNonStopModeEnabled()) |
| 1035 | m_gdb_comm.GetDefaultThreadId(m_initial_tid); |
| 1036 | |
| 1037 | size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); |
| 1038 | for (size_t idx = 0; idx < num_cmds; idx++) { |
| 1039 | StringExtractorGDBRemote response; |
| 1040 | m_gdb_comm.SendPacketAndWaitForResponse( |
| 1041 | GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false); |
| 1042 | } |
| 1043 | return error; |
| 1044 | } |
| 1045 | |
| 1046 | void ProcessGDBRemote::DidLaunchOrAttach(ArchSpec &process_arch) { |
| 1047 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 1048 | if (log) |
| 1049 | log->Printf("ProcessGDBRemote::%s()", __FUNCTION__); |
| 1050 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 1051 | BuildDynamicRegisterInfo(false); |
| 1052 | |
| 1053 | // See if the GDB server supports the qHostInfo information |
| 1054 | |
| 1055 | // See if the GDB server supports the qProcessInfo packet, if so |
| 1056 | // prefer that over the Host information as it will be more specific |
| 1057 | // to our process. |
| 1058 | |
| 1059 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 1060 | if (remote_process_arch.IsValid()) { |
| 1061 | process_arch = remote_process_arch; |
| 1062 | if (log) |
| 1063 | log->Printf("ProcessGDBRemote::%s gdb-remote had process architecture, " |
| 1064 | "using %s %s", |
| 1065 | __FUNCTION__, process_arch.GetArchitectureName() |
| 1066 | ? process_arch.GetArchitectureName() |
| 1067 | : "<null>", |
| 1068 | process_arch.GetTriple().getTriple().c_str() |
| 1069 | ? process_arch.GetTriple().getTriple().c_str() |
| 1070 | : "<null>"); |
| 1071 | } else { |
| 1072 | process_arch = m_gdb_comm.GetHostArchitecture(); |
| 1073 | if (log) |
| 1074 | log->Printf("ProcessGDBRemote::%s gdb-remote did not have process " |
| 1075 | "architecture, using gdb-remote host architecture %s %s", |
| 1076 | __FUNCTION__, process_arch.GetArchitectureName() |
| 1077 | ? process_arch.GetArchitectureName() |
| 1078 | : "<null>", |
| 1079 | process_arch.GetTriple().getTriple().c_str() |
| 1080 | ? process_arch.GetTriple().getTriple().c_str() |
| 1081 | : "<null>"); |
| 1082 | } |
| 1083 | |
| 1084 | if (process_arch.IsValid()) { |
| 1085 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 1086 | if (target_arch.IsValid()) { |
| 1087 | if (log) |
| 1088 | log->Printf( |
| 1089 | "ProcessGDBRemote::%s analyzing target arch, currently %s %s", |
| 1090 | __FUNCTION__, target_arch.GetArchitectureName() |
| 1091 | ? target_arch.GetArchitectureName() |
| 1092 | : "<null>", |
| 1093 | target_arch.GetTriple().getTriple().c_str() |
| 1094 | ? target_arch.GetTriple().getTriple().c_str() |
| 1095 | : "<null>"); |
| 1096 | |
| 1097 | // If the remote host is ARM and we have apple as the vendor, then |
| 1098 | // ARM executables and shared libraries can have mixed ARM |
| 1099 | // architectures. |
| 1100 | // You can have an armv6 executable, and if the host is armv7, then the |
| 1101 | // system will load the best possible architecture for all shared |
| 1102 | // libraries |
| 1103 | // it has, so we really need to take the remote host architecture as our |
| 1104 | // defacto architecture in this case. |
| 1105 | |
| 1106 | if ((process_arch.GetMachine() == llvm::Triple::arm || |
| 1107 | process_arch.GetMachine() == llvm::Triple::thumb) && |
| 1108 | process_arch.GetTriple().getVendor() == llvm::Triple::Apple) { |
| 1109 | GetTarget().SetArchitecture(process_arch); |
| 1110 | if (log) |
| 1111 | log->Printf("ProcessGDBRemote::%s remote process is ARM/Apple, " |
| 1112 | "setting target arch to %s %s", |
| 1113 | __FUNCTION__, process_arch.GetArchitectureName() |
| 1114 | ? process_arch.GetArchitectureName() |
| 1115 | : "<null>", |
| 1116 | process_arch.GetTriple().getTriple().c_str() |
| 1117 | ? process_arch.GetTriple().getTriple().c_str() |
| 1118 | : "<null>"); |
| 1119 | } else { |
| 1120 | // Fill in what is missing in the triple |
| 1121 | const llvm::Triple &remote_triple = process_arch.GetTriple(); |
| 1122 | llvm::Triple new_target_triple = target_arch.GetTriple(); |
| 1123 | if (new_target_triple.getVendorName().size() == 0) { |
| 1124 | new_target_triple.setVendor(remote_triple.getVendor()); |
| 1125 | |
| 1126 | if (new_target_triple.getOSName().size() == 0) { |
| 1127 | new_target_triple.setOS(remote_triple.getOS()); |
| 1128 | |
| 1129 | if (new_target_triple.getEnvironmentName().size() == 0) |
| 1130 | new_target_triple.setEnvironment( |
| 1131 | remote_triple.getEnvironment()); |
| 1132 | } |
| 1133 | |
| 1134 | ArchSpec new_target_arch = target_arch; |
| 1135 | new_target_arch.SetTriple(new_target_triple); |
| 1136 | GetTarget().SetArchitecture(new_target_arch); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | if (log) |
| 1141 | log->Printf("ProcessGDBRemote::%s final target arch after " |
| 1142 | "adjustments for remote architecture: %s %s", |
| 1143 | __FUNCTION__, target_arch.GetArchitectureName() |
| 1144 | ? target_arch.GetArchitectureName() |
| 1145 | : "<null>", |
| 1146 | target_arch.GetTriple().getTriple().c_str() |
| 1147 | ? target_arch.GetTriple().getTriple().c_str() |
| 1148 | : "<null>"); |
| 1149 | } else { |
| 1150 | // The target doesn't have a valid architecture yet, set it from |
| 1151 | // the architecture we got from the remote GDB server |
| 1152 | GetTarget().SetArchitecture(process_arch); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | // Find out which StructuredDataPlugins are supported by the |
| 1157 | // debug monitor. These plugins transmit data over async $J packets. |
| 1158 | auto supported_packets_array = |
| 1159 | m_gdb_comm.GetSupportedStructuredDataPlugins(); |
| 1160 | if (supported_packets_array) |
| 1161 | MapSupportedStructuredDataPlugins(*supported_packets_array); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | void ProcessGDBRemote::DidLaunch() { |
| 1166 | ArchSpec process_arch; |
| 1167 | DidLaunchOrAttach(process_arch); |
| 1168 | } |
| 1169 | |
| 1170 | Error ProcessGDBRemote::DoAttachToProcessWithID( |
| 1171 | lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) { |
| 1172 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 1173 | Error error; |
| 1174 | |
| 1175 | if (log) |
| 1176 | log->Printf("ProcessGDBRemote::%s()", __FUNCTION__); |
| 1177 | |
| 1178 | // Clear out and clean up from any current state |
| 1179 | Clear(); |
| 1180 | if (attach_pid != LLDB_INVALID_PROCESS_ID) { |
| 1181 | error = EstablishConnectionIfNeeded(attach_info); |
| 1182 | if (error.Success()) { |
| 1183 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
| 1184 | |
| 1185 | char packet[64]; |
| 1186 | const int packet_len = |
| 1187 | ::snprintf(packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); |
| 1188 | SetID(attach_pid); |
| 1189 | m_async_broadcaster.BroadcastEvent( |
| 1190 | eBroadcastBitAsyncContinue, new EventDataBytes(packet, packet_len)); |
| 1191 | } else |
| 1192 | SetExitStatus(-1, error.AsCString()); |
| 1193 | } |
| 1194 | |
| 1195 | return error; |
| 1196 | } |
| 1197 | |
| 1198 | Error ProcessGDBRemote::DoAttachToProcessWithName( |
| 1199 | const char *process_name, const ProcessAttachInfo &attach_info) { |
| 1200 | Error error; |
| 1201 | // Clear out and clean up from any current state |
| 1202 | Clear(); |
| 1203 | |
| 1204 | if (process_name && process_name[0]) { |
| 1205 | error = EstablishConnectionIfNeeded(attach_info); |
| 1206 | if (error.Success()) { |
| 1207 | StreamString packet; |
| 1208 | |
| 1209 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
| 1210 | |
| 1211 | if (attach_info.GetWaitForLaunch()) { |
| 1212 | if (!m_gdb_comm.GetVAttachOrWaitSupported()) { |
| 1213 | packet.PutCString("vAttachWait"); |
| 1214 | } else { |
| 1215 | if (attach_info.GetIgnoreExisting()) |
| 1216 | packet.PutCString("vAttachWait"); |
| 1217 | else |
| 1218 | packet.PutCString("vAttachOrWait"); |
| 1219 | } |
| 1220 | } else |
| 1221 | packet.PutCString("vAttachName"); |
| 1222 | packet.PutChar(';'); |
| 1223 | packet.PutBytesAsRawHex8(process_name, strlen(process_name), |
| 1224 | endian::InlHostByteOrder(), |
| 1225 | endian::InlHostByteOrder()); |
| 1226 | |
| 1227 | m_async_broadcaster.BroadcastEvent( |
| 1228 | eBroadcastBitAsyncContinue, |
| 1229 | new EventDataBytes(packet.GetData(), packet.GetSize())); |
| 1230 | |
| 1231 | } else |
| 1232 | SetExitStatus(-1, error.AsCString()); |
| 1233 | } |
| 1234 | return error; |
| 1235 | } |
| 1236 | |
| 1237 | void ProcessGDBRemote::DidExit() { |
| 1238 | // When we exit, disconnect from the GDB server communications |
| 1239 | m_gdb_comm.Disconnect(); |
| 1240 | } |
| 1241 | |
| 1242 | void ProcessGDBRemote::DidAttach(ArchSpec &process_arch) { |
| 1243 | // If you can figure out what the architecture is, fill it in here. |
| 1244 | process_arch.Clear(); |
| 1245 | DidLaunchOrAttach(process_arch); |
| 1246 | } |
| 1247 | |
| 1248 | Error ProcessGDBRemote::WillResume() { |
| 1249 | m_continue_c_tids.clear(); |
| 1250 | m_continue_C_tids.clear(); |
| 1251 | m_continue_s_tids.clear(); |
| 1252 | m_continue_S_tids.clear(); |
| 1253 | m_jstopinfo_sp.reset(); |
| 1254 | m_jthreadsinfo_sp.reset(); |
| 1255 | return Error(); |
| 1256 | } |
| 1257 | |
| 1258 | Error ProcessGDBRemote::DoResume() { |
| 1259 | Error error; |
| 1260 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 1261 | if (log) |
| 1262 | log->Printf("ProcessGDBRemote::Resume()"); |
| 1263 | |
| 1264 | ListenerSP listener_sp( |
| 1265 | Listener::MakeListener("gdb-remote.resume-packet-sent")); |
| 1266 | if (listener_sp->StartListeningForEvents( |
| 1267 | &m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) { |
| 1268 | listener_sp->StartListeningForEvents( |
| 1269 | &m_async_broadcaster, |
| 1270 | ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); |
| 1271 | |
| 1272 | const size_t num_threads = GetThreadList().GetSize(); |
| 1273 | |
| 1274 | StreamString continue_packet; |
| 1275 | bool continue_packet_error = false; |
| 1276 | if (m_gdb_comm.HasAnyVContSupport()) { |
| 1277 | if (!GetTarget().GetNonStopModeEnabled() && |
| 1278 | (m_continue_c_tids.size() == num_threads || |
| 1279 | (m_continue_c_tids.empty() && m_continue_C_tids.empty() && |
| 1280 | m_continue_s_tids.empty() && m_continue_S_tids.empty()))) { |
| 1281 | // All threads are continuing, just send a "c" packet |
| 1282 | continue_packet.PutCString("c"); |
| 1283 | } else { |
| 1284 | continue_packet.PutCString("vCont"); |
| 1285 | |
| 1286 | if (!m_continue_c_tids.empty()) { |
| 1287 | if (m_gdb_comm.GetVContSupported('c')) { |
| 1288 | for (tid_collection::const_iterator |
| 1289 | t_pos = m_continue_c_tids.begin(), |
| 1290 | t_end = m_continue_c_tids.end(); |
| 1291 | t_pos != t_end; ++t_pos) |
| 1292 | continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); |
| 1293 | } else |
| 1294 | continue_packet_error = true; |
| 1295 | } |
| 1296 | |
| 1297 | if (!continue_packet_error && !m_continue_C_tids.empty()) { |
| 1298 | if (m_gdb_comm.GetVContSupported('C')) { |
| 1299 | for (tid_sig_collection::const_iterator |
| 1300 | s_pos = m_continue_C_tids.begin(), |
| 1301 | s_end = m_continue_C_tids.end(); |
| 1302 | s_pos != s_end; ++s_pos) |
| 1303 | continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, |
| 1304 | s_pos->first); |
| 1305 | } else |
| 1306 | continue_packet_error = true; |
| 1307 | } |
| 1308 | |
| 1309 | if (!continue_packet_error && !m_continue_s_tids.empty()) { |
| 1310 | if (m_gdb_comm.GetVContSupported('s')) { |
| 1311 | for (tid_collection::const_iterator |
| 1312 | t_pos = m_continue_s_tids.begin(), |
| 1313 | t_end = m_continue_s_tids.end(); |
| 1314 | t_pos != t_end; ++t_pos) |
| 1315 | continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); |
| 1316 | } else |
| 1317 | continue_packet_error = true; |
| 1318 | } |
| 1319 | |
| 1320 | if (!continue_packet_error && !m_continue_S_tids.empty()) { |
| 1321 | if (m_gdb_comm.GetVContSupported('S')) { |
| 1322 | for (tid_sig_collection::const_iterator |
| 1323 | s_pos = m_continue_S_tids.begin(), |
| 1324 | s_end = m_continue_S_tids.end(); |
| 1325 | s_pos != s_end; ++s_pos) |
| 1326 | continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, |
| 1327 | s_pos->first); |
| 1328 | } else |
| 1329 | continue_packet_error = true; |
| 1330 | } |
| 1331 | |
| 1332 | if (continue_packet_error) |
| 1333 | continue_packet.GetString().clear(); |
| 1334 | } |
| 1335 | } else |
| 1336 | continue_packet_error = true; |
| 1337 | |
| 1338 | if (continue_packet_error) { |
| 1339 | // Either no vCont support, or we tried to use part of the vCont |
| 1340 | // packet that wasn't supported by the remote GDB server. |
| 1341 | // We need to try and make a simple packet that can do our continue |
| 1342 | const size_t num_continue_c_tids = m_continue_c_tids.size(); |
| 1343 | const size_t num_continue_C_tids = m_continue_C_tids.size(); |
| 1344 | const size_t num_continue_s_tids = m_continue_s_tids.size(); |
| 1345 | const size_t num_continue_S_tids = m_continue_S_tids.size(); |
| 1346 | if (num_continue_c_tids > 0) { |
| 1347 | if (num_continue_c_tids == num_threads) { |
| 1348 | // All threads are resuming... |
| 1349 | m_gdb_comm.SetCurrentThreadForRun(-1); |
| 1350 | continue_packet.PutChar('c'); |
| 1351 | continue_packet_error = false; |
| 1352 | } else if (num_continue_c_tids == 1 && num_continue_C_tids == 0 && |
| 1353 | num_continue_s_tids == 0 && num_continue_S_tids == 0) { |
| 1354 | // Only one thread is continuing |
| 1355 | m_gdb_comm.SetCurrentThreadForRun(m_continue_c_tids.front()); |
| 1356 | continue_packet.PutChar('c'); |
| 1357 | continue_packet_error = false; |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | if (continue_packet_error && num_continue_C_tids > 0) { |
| 1362 | if ((num_continue_C_tids + num_continue_c_tids) == num_threads && |
| 1363 | num_continue_C_tids > 0 && num_continue_s_tids == 0 && |
| 1364 | num_continue_S_tids == 0) { |
| 1365 | const int continue_signo = m_continue_C_tids.front().second; |
| 1366 | // Only one thread is continuing |
| 1367 | if (num_continue_C_tids > 1) { |
| 1368 | // More that one thread with a signal, yet we don't have |
| 1369 | // vCont support and we are being asked to resume each |
| 1370 | // thread with a signal, we need to make sure they are |
| 1371 | // all the same signal, or we can't issue the continue |
| 1372 | // accurately with the current support... |
| 1373 | if (num_continue_C_tids > 1) { |
| 1374 | continue_packet_error = false; |
| 1375 | for (size_t i = 1; i < m_continue_C_tids.size(); ++i) { |
| 1376 | if (m_continue_C_tids[i].second != continue_signo) |
| 1377 | continue_packet_error = true; |
| 1378 | } |
| 1379 | } |
| 1380 | if (!continue_packet_error) |
| 1381 | m_gdb_comm.SetCurrentThreadForRun(-1); |
| 1382 | } else { |
| 1383 | // Set the continue thread ID |
| 1384 | continue_packet_error = false; |
| 1385 | m_gdb_comm.SetCurrentThreadForRun(m_continue_C_tids.front().first); |
| 1386 | } |
| 1387 | if (!continue_packet_error) { |
| 1388 | // Add threads continuing with the same signo... |
| 1389 | continue_packet.Printf("C%2.2x", continue_signo); |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | if (continue_packet_error && num_continue_s_tids > 0) { |
| 1395 | if (num_continue_s_tids == num_threads) { |
| 1396 | // All threads are resuming... |
| 1397 | m_gdb_comm.SetCurrentThreadForRun(-1); |
| 1398 | |
| 1399 | // If in Non-Stop-Mode use vCont when stepping |
| 1400 | if (GetTarget().GetNonStopModeEnabled()) { |
| 1401 | if (m_gdb_comm.GetVContSupported('s')) |
| 1402 | continue_packet.PutCString("vCont;s"); |
| 1403 | else |
| 1404 | continue_packet.PutChar('s'); |
| 1405 | } else |
| 1406 | continue_packet.PutChar('s'); |
| 1407 | |
| 1408 | continue_packet_error = false; |
| 1409 | } else if (num_continue_c_tids == 0 && num_continue_C_tids == 0 && |
| 1410 | num_continue_s_tids == 1 && num_continue_S_tids == 0) { |
| 1411 | // Only one thread is stepping |
| 1412 | m_gdb_comm.SetCurrentThreadForRun(m_continue_s_tids.front()); |
| 1413 | continue_packet.PutChar('s'); |
| 1414 | continue_packet_error = false; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | if (!continue_packet_error && num_continue_S_tids > 0) { |
| 1419 | if (num_continue_S_tids == num_threads) { |
| 1420 | const int step_signo = m_continue_S_tids.front().second; |
| 1421 | // Are all threads trying to step with the same signal? |
| 1422 | continue_packet_error = false; |
| 1423 | if (num_continue_S_tids > 1) { |
| 1424 | for (size_t i = 1; i < num_threads; ++i) { |
| 1425 | if (m_continue_S_tids[i].second != step_signo) |
| 1426 | continue_packet_error = true; |
| 1427 | } |
| 1428 | } |
| 1429 | if (!continue_packet_error) { |
| 1430 | // Add threads stepping with the same signo... |
| 1431 | m_gdb_comm.SetCurrentThreadForRun(-1); |
| 1432 | continue_packet.Printf("S%2.2x", step_signo); |
| 1433 | } |
| 1434 | } else if (num_continue_c_tids == 0 && num_continue_C_tids == 0 && |
| 1435 | num_continue_s_tids == 0 && num_continue_S_tids == 1) { |
| 1436 | // Only one thread is stepping with signal |
| 1437 | m_gdb_comm.SetCurrentThreadForRun(m_continue_S_tids.front().first); |
| 1438 | continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); |
| 1439 | continue_packet_error = false; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | if (continue_packet_error) { |
| 1445 | error.SetErrorString("can't make continue packet for this resume"); |
| 1446 | } else { |
| 1447 | EventSP event_sp; |
| 1448 | if (!m_async_thread.IsJoinable()) { |
| 1449 | error.SetErrorString("Trying to resume but the async thread is dead."); |
| 1450 | if (log) |
| 1451 | log->Printf("ProcessGDBRemote::DoResume: Trying to resume but the " |
| 1452 | "async thread is dead."); |
| 1453 | return error; |
| 1454 | } |
| 1455 | |
| 1456 | m_async_broadcaster.BroadcastEvent( |
| 1457 | eBroadcastBitAsyncContinue, |
| 1458 | new EventDataBytes(continue_packet.GetData(), |
| 1459 | continue_packet.GetSize())); |
| 1460 | |
| 1461 | if (listener_sp->WaitForEvent(std::chrono::seconds(5), event_sp) == |
| 1462 | false) { |
| 1463 | error.SetErrorString("Resume timed out."); |
| 1464 | if (log) |
| 1465 | log->Printf("ProcessGDBRemote::DoResume: Resume timed out."); |
| 1466 | } else if (event_sp->BroadcasterIs(&m_async_broadcaster)) { |
| 1467 | error.SetErrorString("Broadcast continue, but the async thread was " |
| 1468 | "killed before we got an ack back."); |
| 1469 | if (log) |
| 1470 | log->Printf("ProcessGDBRemote::DoResume: Broadcast continue, but the " |
| 1471 | "async thread was killed before we got an ack back."); |
| 1472 | return error; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | return error; |
| 1478 | } |
| 1479 | |
| 1480 | void ProcessGDBRemote::HandleStopReplySequence() { |
| 1481 | while (true) { |
| 1482 | // Send vStopped |
| 1483 | StringExtractorGDBRemote response; |
| 1484 | m_gdb_comm.SendPacketAndWaitForResponse("vStopped", response, false); |
| 1485 | |
| 1486 | // OK represents end of signal list |
| 1487 | if (response.IsOKResponse()) |
| 1488 | break; |
| 1489 | |
| 1490 | // If not OK or a normal packet we have a problem |
| 1491 | if (!response.IsNormalResponse()) |
| 1492 | break; |
| 1493 | |
| 1494 | SetLastStopPacket(response); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | void ProcessGDBRemote::ClearThreadIDList() { |
| 1499 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
| 1500 | m_thread_ids.clear(); |
| 1501 | m_thread_pcs.clear(); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1504 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1505 | ProcessGDBRemote::UpdateThreadIDsFromStopReplyThreadsValue(std::string &value) { |
| 1506 | m_thread_ids.clear(); |
| 1507 | m_thread_pcs.clear(); |
| 1508 | size_t comma_pos; |
| 1509 | lldb::tid_t tid; |
| 1510 | while ((comma_pos = value.find(',')) != std::string::npos) { |
| 1511 | value[comma_pos] = '\0'; |
| 1512 | // thread in big endian hex |
| 1513 | tid = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1514 | if (tid != LLDB_INVALID_THREAD_ID) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1515 | m_thread_ids.push_back(tid); |
| 1516 | value.erase(0, comma_pos + 1); |
| 1517 | } |
| 1518 | tid = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1519 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1520 | m_thread_ids.push_back(tid); |
| 1521 | return m_thread_ids.size(); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1524 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1525 | ProcessGDBRemote::UpdateThreadPCsFromStopReplyThreadsValue(std::string &value) { |
| 1526 | m_thread_pcs.clear(); |
| 1527 | size_t comma_pos; |
| 1528 | lldb::addr_t pc; |
| 1529 | while ((comma_pos = value.find(',')) != std::string::npos) { |
| 1530 | value[comma_pos] = '\0'; |
| 1531 | pc = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16); |
| 1532 | if (pc != LLDB_INVALID_ADDRESS) |
| 1533 | m_thread_pcs.push_back(pc); |
| 1534 | value.erase(0, comma_pos + 1); |
| 1535 | } |
| 1536 | pc = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16); |
| 1537 | if (pc != LLDB_INVALID_THREAD_ID) |
| 1538 | m_thread_pcs.push_back(pc); |
| 1539 | return m_thread_pcs.size(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1542 | bool ProcessGDBRemote::UpdateThreadIDList() { |
| 1543 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1545 | if (m_jthreadsinfo_sp) { |
| 1546 | // If we have the JSON threads info, we can get the thread list from that |
| 1547 | StructuredData::Array *thread_infos = m_jthreadsinfo_sp->GetAsArray(); |
| 1548 | if (thread_infos && thread_infos->GetSize() > 0) { |
| 1549 | m_thread_ids.clear(); |
| 1550 | m_thread_pcs.clear(); |
| 1551 | thread_infos->ForEach([this](StructuredData::Object *object) -> bool { |
| 1552 | StructuredData::Dictionary *thread_dict = object->GetAsDictionary(); |
| 1553 | if (thread_dict) { |
| 1554 | // Set the thread stop info from the JSON dictionary |
| 1555 | SetThreadStopInfo(thread_dict); |
| 1556 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
| 1557 | if (thread_dict->GetValueForKeyAsInteger<lldb::tid_t>("tid", tid)) |
| 1558 | m_thread_ids.push_back(tid); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1559 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1560 | return true; // Keep iterating through all thread_info objects |
| 1561 | }); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1562 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1563 | if (!m_thread_ids.empty()) |
| 1564 | return true; |
| 1565 | } else { |
| 1566 | // See if we can get the thread IDs from the current stop reply packets |
| 1567 | // that might contain a "threads" key/value pair |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1568 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1569 | // Lock the thread stack while we access it |
| 1570 | // Mutex::Locker stop_stack_lock(m_last_stop_packet_mutex); |
| 1571 | std::unique_lock<std::recursive_mutex> stop_stack_lock( |
| 1572 | m_last_stop_packet_mutex, std::defer_lock); |
| 1573 | if (stop_stack_lock.try_lock()) { |
| 1574 | // Get the number of stop packets on the stack |
| 1575 | int nItems = m_stop_packet_stack.size(); |
| 1576 | // Iterate over them |
| 1577 | for (int i = 0; i < nItems; i++) { |
| 1578 | // Get the thread stop info |
| 1579 | StringExtractorGDBRemote &stop_info = m_stop_packet_stack[i]; |
| 1580 | const std::string &stop_info_str = stop_info.GetStringRef(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1581 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1582 | m_thread_pcs.clear(); |
| 1583 | const size_t thread_pcs_pos = stop_info_str.find(";thread-pcs:"); |
| 1584 | if (thread_pcs_pos != std::string::npos) { |
| 1585 | const size_t start = thread_pcs_pos + strlen(";thread-pcs:"); |
| 1586 | const size_t end = stop_info_str.find(';', start); |
| 1587 | if (end != std::string::npos) { |
| 1588 | std::string value = stop_info_str.substr(start, end - start); |
| 1589 | UpdateThreadPCsFromStopReplyThreadsValue(value); |
| 1590 | } |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1591 | } |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1592 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1593 | const size_t threads_pos = stop_info_str.find(";threads:"); |
| 1594 | if (threads_pos != std::string::npos) { |
| 1595 | const size_t start = threads_pos + strlen(";threads:"); |
| 1596 | const size_t end = stop_info_str.find(';', start); |
| 1597 | if (end != std::string::npos) { |
| 1598 | std::string value = stop_info_str.substr(start, end - start); |
| 1599 | if (UpdateThreadIDsFromStopReplyThreadsValue(value)) |
| 1600 | return true; |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | bool sequence_mutex_unavailable = false; |
| 1608 | m_gdb_comm.GetCurrentThreadIDs(m_thread_ids, sequence_mutex_unavailable); |
| 1609 | if (sequence_mutex_unavailable) { |
| 1610 | return false; // We just didn't get the list |
| 1611 | } |
| 1612 | return true; |
| 1613 | } |
| 1614 | |
| 1615 | bool ProcessGDBRemote::UpdateThreadList(ThreadList &old_thread_list, |
| 1616 | ThreadList &new_thread_list) { |
| 1617 | // locker will keep a mutex locked until it goes out of scope |
| 1618 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_THREAD)); |
| 1619 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1620 | log->Printf("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, |
| 1621 | GetID()); |
| 1622 | |
| 1623 | size_t num_thread_ids = m_thread_ids.size(); |
| 1624 | // The "m_thread_ids" thread ID list should always be updated after each stop |
| 1625 | // reply packet, but in case it isn't, update it here. |
| 1626 | if (num_thread_ids == 0) { |
| 1627 | if (!UpdateThreadIDList()) |
| 1628 | return false; |
| 1629 | num_thread_ids = m_thread_ids.size(); |
| 1630 | } |
| 1631 | |
| 1632 | ThreadList old_thread_list_copy(old_thread_list); |
| 1633 | if (num_thread_ids > 0) { |
| 1634 | for (size_t i = 0; i < num_thread_ids; ++i) { |
| 1635 | tid_t tid = m_thread_ids[i]; |
| 1636 | ThreadSP thread_sp( |
| 1637 | old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); |
| 1638 | if (!thread_sp) { |
| 1639 | thread_sp.reset(new ThreadGDBRemote(*this, tid)); |
| 1640 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1641 | log->Printf("ProcessGDBRemote::%s Making new thread: %p for thread " |
| 1642 | "ID: 0x%" PRIx64 ".\n", |
| 1643 | __FUNCTION__, static_cast<void *>(thread_sp.get()), |
| 1644 | thread_sp->GetID()); |
| 1645 | } else { |
| 1646 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1647 | log->Printf("ProcessGDBRemote::%s Found old thread: %p for thread " |
| 1648 | "ID: 0x%" PRIx64 ".\n", |
| 1649 | __FUNCTION__, static_cast<void *>(thread_sp.get()), |
| 1650 | thread_sp->GetID()); |
| 1651 | } |
| 1652 | // The m_thread_pcs vector has pc values in big-endian order, not |
| 1653 | // target-endian, unlike most |
| 1654 | // of the register read/write packets in gdb-remote protocol. |
| 1655 | // Early in the process startup, we may not yet have set the process |
| 1656 | // ByteOrder so we ignore these; |
| 1657 | // they are a performance improvement over fetching thread register values |
| 1658 | // individually, the |
| 1659 | // method we will fall back to if needed. |
| 1660 | if (m_thread_ids.size() == m_thread_pcs.size() && thread_sp.get() && |
| 1661 | GetByteOrder() != eByteOrderInvalid) { |
| 1662 | ThreadGDBRemote *gdb_thread = |
| 1663 | static_cast<ThreadGDBRemote *>(thread_sp.get()); |
| 1664 | RegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext()); |
| 1665 | if (reg_ctx_sp) { |
| 1666 | uint32_t pc_regnum = reg_ctx_sp->ConvertRegisterKindToRegisterNumber( |
| 1667 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 1668 | if (pc_regnum != LLDB_INVALID_REGNUM) { |
| 1669 | gdb_thread->PrivateSetRegisterValue(pc_regnum, m_thread_pcs[i]); |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | new_thread_list.AddThreadSortedByIndexID(thread_sp); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | // Whatever that is left in old_thread_list_copy are not |
| 1678 | // present in new_thread_list. Remove non-existent threads from internal id |
| 1679 | // table. |
| 1680 | size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); |
| 1681 | for (size_t i = 0; i < old_num_thread_ids; i++) { |
| 1682 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false)); |
| 1683 | if (old_thread_sp) { |
| 1684 | lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); |
| 1685 | m_thread_id_to_index_id_map.erase(old_thread_id); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | return true; |
| 1690 | } |
| 1691 | |
| 1692 | bool ProcessGDBRemote::GetThreadStopInfoFromJSON( |
| 1693 | ThreadGDBRemote *thread, const StructuredData::ObjectSP &thread_infos_sp) { |
| 1694 | // See if we got thread stop infos for all threads via the "jThreadsInfo" |
| 1695 | // packet |
| 1696 | if (thread_infos_sp) { |
| 1697 | StructuredData::Array *thread_infos = thread_infos_sp->GetAsArray(); |
| 1698 | if (thread_infos) { |
| 1699 | lldb::tid_t tid; |
| 1700 | const size_t n = thread_infos->GetSize(); |
| 1701 | for (size_t i = 0; i < n; ++i) { |
| 1702 | StructuredData::Dictionary *thread_dict = |
| 1703 | thread_infos->GetItemAtIndex(i)->GetAsDictionary(); |
| 1704 | if (thread_dict) { |
| 1705 | if (thread_dict->GetValueForKeyAsInteger<lldb::tid_t>( |
| 1706 | "tid", tid, LLDB_INVALID_THREAD_ID)) { |
| 1707 | if (tid == thread->GetID()) |
| 1708 | return (bool)SetThreadStopInfo(thread_dict); |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | } |
| 1714 | return false; |
| 1715 | } |
| 1716 | |
| 1717 | bool ProcessGDBRemote::CalculateThreadStopInfo(ThreadGDBRemote *thread) { |
| 1718 | // See if we got thread stop infos for all threads via the "jThreadsInfo" |
| 1719 | // packet |
| 1720 | if (GetThreadStopInfoFromJSON(thread, m_jthreadsinfo_sp)) |
| 1721 | return true; |
| 1722 | |
| 1723 | // See if we got thread stop info for any threads valid stop info reasons |
| 1724 | // threads |
| 1725 | // via the "jstopinfo" packet stop reply packet key/value pair? |
| 1726 | if (m_jstopinfo_sp) { |
| 1727 | // If we have "jstopinfo" then we have stop descriptions for all threads |
| 1728 | // that have stop reasons, and if there is no entry for a thread, then |
| 1729 | // it has no stop reason. |
| 1730 | thread->GetRegisterContext()->InvalidateIfNeeded(true); |
| 1731 | if (!GetThreadStopInfoFromJSON(thread, m_jstopinfo_sp)) { |
| 1732 | thread->SetStopInfo(StopInfoSP()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1733 | } |
| 1734 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | // Fall back to using the qThreadStopInfo packet |
| 1738 | StringExtractorGDBRemote stop_packet; |
| 1739 | if (GetGDBRemote().GetThreadStopInfo(thread->GetProtocolID(), stop_packet)) |
| 1740 | return SetThreadStopInfo(stop_packet) == eStateStopped; |
| 1741 | return false; |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1744 | ThreadSP ProcessGDBRemote::SetThreadStopInfo( |
| 1745 | lldb::tid_t tid, ExpeditedRegisterMap &expedited_register_map, |
| 1746 | uint8_t signo, const std::string &thread_name, const std::string &reason, |
| 1747 | const std::string &description, uint32_t exc_type, |
| 1748 | const std::vector<addr_t> &exc_data, addr_t thread_dispatch_qaddr, |
| 1749 | bool queue_vars_valid, // Set to true if queue_name, queue_kind and |
| 1750 | // queue_serial are valid |
| 1751 | LazyBool associated_with_dispatch_queue, addr_t dispatch_queue_t, |
| 1752 | std::string &queue_name, QueueKind queue_kind, uint64_t queue_serial) { |
| 1753 | ThreadSP thread_sp; |
| 1754 | if (tid != LLDB_INVALID_THREAD_ID) { |
| 1755 | // Scope for "locker" below |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1756 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1757 | // m_thread_list_real does have its own mutex, but we need to |
| 1758 | // hold onto the mutex between the call to |
| 1759 | // m_thread_list_real.FindThreadByID(...) |
| 1760 | // and the m_thread_list_real.AddThread(...) so it doesn't change on us |
| 1761 | std::lock_guard<std::recursive_mutex> guard( |
| 1762 | m_thread_list_real.GetMutex()); |
| 1763 | thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); |
| 1764 | |
| 1765 | if (!thread_sp) { |
| 1766 | // Create the thread if we need to |
| 1767 | thread_sp.reset(new ThreadGDBRemote(*this, tid)); |
| 1768 | m_thread_list_real.AddThread(thread_sp); |
| 1769 | } |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1770 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1771 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1772 | if (thread_sp) { |
| 1773 | ThreadGDBRemote *gdb_thread = |
| 1774 | static_cast<ThreadGDBRemote *>(thread_sp.get()); |
| 1775 | gdb_thread->GetRegisterContext()->InvalidateIfNeeded(true); |
| 1776 | |
| 1777 | for (const auto &pair : expedited_register_map) { |
| 1778 | StringExtractor reg_value_extractor; |
| 1779 | reg_value_extractor.GetStringRef() = pair.second; |
| 1780 | DataBufferSP buffer_sp(new DataBufferHeap( |
| 1781 | reg_value_extractor.GetStringRef().size() / 2, 0)); |
| 1782 | reg_value_extractor.GetHexBytes(buffer_sp->GetData(), '\xcc'); |
| 1783 | gdb_thread->PrivateSetRegisterValue(pair.first, buffer_sp->GetData()); |
| 1784 | } |
| 1785 | |
| 1786 | thread_sp->SetName(thread_name.empty() ? NULL : thread_name.c_str()); |
| 1787 | |
| 1788 | gdb_thread->SetThreadDispatchQAddr(thread_dispatch_qaddr); |
| 1789 | // Check if the GDB server was able to provide the queue name, kind and |
| 1790 | // serial number |
| 1791 | if (queue_vars_valid) |
| 1792 | gdb_thread->SetQueueInfo(std::move(queue_name), queue_kind, |
| 1793 | queue_serial, dispatch_queue_t, |
| 1794 | associated_with_dispatch_queue); |
| 1795 | else |
| 1796 | gdb_thread->ClearQueueInfo(); |
| 1797 | |
| 1798 | gdb_thread->SetAssociatedWithLibdispatchQueue( |
| 1799 | associated_with_dispatch_queue); |
| 1800 | |
| 1801 | if (dispatch_queue_t != LLDB_INVALID_ADDRESS) |
| 1802 | gdb_thread->SetQueueLibdispatchQueueAddress(dispatch_queue_t); |
| 1803 | |
| 1804 | // Make sure we update our thread stop reason just once |
| 1805 | if (!thread_sp->StopInfoIsUpToDate()) { |
| 1806 | thread_sp->SetStopInfo(StopInfoSP()); |
| 1807 | // If there's a memory thread backed by this thread, we need to use it |
| 1808 | // to calcualte StopInfo. |
| 1809 | ThreadSP memory_thread_sp = |
| 1810 | m_thread_list.FindThreadByProtocolID(thread_sp->GetProtocolID()); |
| 1811 | if (memory_thread_sp) |
| 1812 | thread_sp = memory_thread_sp; |
| 1813 | |
| 1814 | if (exc_type != 0) { |
| 1815 | const size_t exc_data_size = exc_data.size(); |
| 1816 | |
| 1817 | thread_sp->SetStopInfo( |
| 1818 | StopInfoMachException::CreateStopReasonWithMachException( |
| 1819 | *thread_sp, exc_type, exc_data_size, |
| 1820 | exc_data_size >= 1 ? exc_data[0] : 0, |
| 1821 | exc_data_size >= 2 ? exc_data[1] : 0, |
| 1822 | exc_data_size >= 3 ? exc_data[2] : 0)); |
| 1823 | } else { |
| 1824 | bool handled = false; |
| 1825 | bool did_exec = false; |
| 1826 | if (!reason.empty()) { |
| 1827 | if (reason.compare("trace") == 0) { |
| 1828 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 1829 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess() |
| 1830 | ->GetBreakpointSiteList() |
| 1831 | .FindByAddress(pc); |
| 1832 | |
| 1833 | // If the current pc is a breakpoint site then the StopInfo should |
| 1834 | // be set to Breakpoint |
| 1835 | // Otherwise, it will be set to Trace. |
| 1836 | if (bp_site_sp && |
| 1837 | bp_site_sp->ValidForThisThread(thread_sp.get())) { |
| 1838 | thread_sp->SetStopInfo( |
| 1839 | StopInfo::CreateStopReasonWithBreakpointSiteID( |
| 1840 | *thread_sp, bp_site_sp->GetID())); |
| 1841 | } else |
| 1842 | thread_sp->SetStopInfo( |
| 1843 | StopInfo::CreateStopReasonToTrace(*thread_sp)); |
| 1844 | handled = true; |
| 1845 | } else if (reason.compare("breakpoint") == 0) { |
| 1846 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 1847 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess() |
| 1848 | ->GetBreakpointSiteList() |
| 1849 | .FindByAddress(pc); |
| 1850 | if (bp_site_sp) { |
| 1851 | // If the breakpoint is for this thread, then we'll report the |
| 1852 | // hit, but if it is for another thread, |
| 1853 | // we can just report no reason. We don't need to worry about |
| 1854 | // stepping over the breakpoint here, that |
| 1855 | // will be taken care of when the thread resumes and notices |
| 1856 | // that there's a breakpoint under the pc. |
| 1857 | handled = true; |
| 1858 | if (bp_site_sp->ValidForThisThread(thread_sp.get())) { |
| 1859 | thread_sp->SetStopInfo( |
| 1860 | StopInfo::CreateStopReasonWithBreakpointSiteID( |
| 1861 | *thread_sp, bp_site_sp->GetID())); |
| 1862 | } else { |
| 1863 | StopInfoSP invalid_stop_info_sp; |
| 1864 | thread_sp->SetStopInfo(invalid_stop_info_sp); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1865 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1866 | } |
| 1867 | } else if (reason.compare("trap") == 0) { |
| 1868 | // Let the trap just use the standard signal stop reason below... |
| 1869 | } else if (reason.compare("watchpoint") == 0) { |
| 1870 | StringExtractor desc_extractor(description.c_str()); |
| 1871 | addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); |
| 1872 | uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32); |
| 1873 | addr_t wp_hit_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); |
| 1874 | watch_id_t watch_id = LLDB_INVALID_WATCH_ID; |
| 1875 | if (wp_addr != LLDB_INVALID_ADDRESS) { |
| 1876 | WatchpointSP wp_sp; |
| 1877 | ArchSpec::Core core = GetTarget().GetArchitecture().GetCore(); |
| 1878 | if ((core >= ArchSpec::kCore_mips_first && |
| 1879 | core <= ArchSpec::kCore_mips_last) || |
| 1880 | (core >= ArchSpec::eCore_arm_generic && |
| 1881 | core <= ArchSpec::eCore_arm_aarch64)) |
| 1882 | wp_sp = GetTarget().GetWatchpointList().FindByAddress( |
| 1883 | wp_hit_addr); |
| 1884 | if (!wp_sp) |
| 1885 | wp_sp = |
| 1886 | GetTarget().GetWatchpointList().FindByAddress(wp_addr); |
| 1887 | if (wp_sp) { |
| 1888 | wp_sp->SetHardwareIndex(wp_index); |
| 1889 | watch_id = wp_sp->GetID(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1890 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1891 | } |
| 1892 | if (watch_id == LLDB_INVALID_WATCH_ID) { |
| 1893 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet( |
| 1894 | GDBR_LOG_WATCHPOINTS)); |
| 1895 | if (log) |
| 1896 | log->Printf("failed to find watchpoint"); |
| 1897 | } |
| 1898 | thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithWatchpointID( |
| 1899 | *thread_sp, watch_id, wp_hit_addr)); |
| 1900 | handled = true; |
| 1901 | } else if (reason.compare("exception") == 0) { |
| 1902 | thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithException( |
| 1903 | *thread_sp, description.c_str())); |
| 1904 | handled = true; |
| 1905 | } else if (reason.compare("exec") == 0) { |
| 1906 | did_exec = true; |
| 1907 | thread_sp->SetStopInfo( |
| 1908 | StopInfo::CreateStopReasonWithExec(*thread_sp)); |
| 1909 | handled = true; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1910 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1911 | } else if (!signo) { |
| 1912 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 1913 | lldb::BreakpointSiteSP bp_site_sp = |
| 1914 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress( |
| 1915 | pc); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1916 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1917 | // If the current pc is a breakpoint site then the StopInfo should |
| 1918 | // be set to Breakpoint |
| 1919 | // even though the remote stub did not set it as such. This can |
| 1920 | // happen when |
| 1921 | // the thread is involuntarily interrupted (e.g. due to stops on |
| 1922 | // other |
| 1923 | // threads) just as it is about to execute the breakpoint |
| 1924 | // instruction. |
| 1925 | if (bp_site_sp && bp_site_sp->ValidForThisThread(thread_sp.get())) { |
| 1926 | thread_sp->SetStopInfo( |
| 1927 | StopInfo::CreateStopReasonWithBreakpointSiteID( |
| 1928 | *thread_sp, bp_site_sp->GetID())); |
| 1929 | handled = true; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1930 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1931 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1932 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1933 | if (!handled && signo && did_exec == false) { |
| 1934 | if (signo == SIGTRAP) { |
| 1935 | // Currently we are going to assume SIGTRAP means we are either |
| 1936 | // hitting a breakpoint or hardware single stepping. |
| 1937 | handled = true; |
| 1938 | addr_t pc = thread_sp->GetRegisterContext()->GetPC() + |
| 1939 | m_breakpoint_pc_offset; |
| 1940 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess() |
| 1941 | ->GetBreakpointSiteList() |
| 1942 | .FindByAddress(pc); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1943 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1944 | if (bp_site_sp) { |
| 1945 | // If the breakpoint is for this thread, then we'll report the |
| 1946 | // hit, but if it is for another thread, |
| 1947 | // we can just report no reason. We don't need to worry about |
| 1948 | // stepping over the breakpoint here, that |
| 1949 | // will be taken care of when the thread resumes and notices |
| 1950 | // that there's a breakpoint under the pc. |
| 1951 | if (bp_site_sp->ValidForThisThread(thread_sp.get())) { |
| 1952 | if (m_breakpoint_pc_offset != 0) |
| 1953 | thread_sp->GetRegisterContext()->SetPC(pc); |
| 1954 | thread_sp->SetStopInfo( |
| 1955 | StopInfo::CreateStopReasonWithBreakpointSiteID( |
| 1956 | *thread_sp, bp_site_sp->GetID())); |
| 1957 | } else { |
| 1958 | StopInfoSP invalid_stop_info_sp; |
| 1959 | thread_sp->SetStopInfo(invalid_stop_info_sp); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1960 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1961 | } else { |
| 1962 | // If we were stepping then assume the stop was the result of |
| 1963 | // the trace. If we were |
| 1964 | // not stepping then report the SIGTRAP. |
| 1965 | // FIXME: We are still missing the case where we single step |
| 1966 | // over a trap instruction. |
| 1967 | if (thread_sp->GetTemporaryResumeState() == eStateStepping) |
| 1968 | thread_sp->SetStopInfo( |
| 1969 | StopInfo::CreateStopReasonToTrace(*thread_sp)); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1970 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1971 | thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithSignal( |
| 1972 | *thread_sp, signo, description.c_str())); |
| 1973 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1974 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1975 | if (!handled) |
| 1976 | thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithSignal( |
| 1977 | *thread_sp, signo, description.c_str())); |
| 1978 | } |
| 1979 | |
| 1980 | if (!description.empty()) { |
| 1981 | lldb::StopInfoSP stop_info_sp(thread_sp->GetStopInfo()); |
| 1982 | if (stop_info_sp) { |
| 1983 | const char *stop_info_desc = stop_info_sp->GetDescription(); |
| 1984 | if (!stop_info_desc || !stop_info_desc[0]) |
| 1985 | stop_info_sp->SetDescription(description.c_str()); |
| 1986 | } else { |
| 1987 | thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithException( |
| 1988 | *thread_sp, description.c_str())); |
| 1989 | } |
| 1990 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1991 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1992 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1993 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1994 | } |
| 1995 | return thread_sp; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1998 | lldb::ThreadSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1999 | ProcessGDBRemote::SetThreadStopInfo(StructuredData::Dictionary *thread_dict) { |
| 2000 | static ConstString g_key_tid("tid"); |
| 2001 | static ConstString g_key_name("name"); |
| 2002 | static ConstString g_key_reason("reason"); |
| 2003 | static ConstString g_key_metype("metype"); |
| 2004 | static ConstString g_key_medata("medata"); |
| 2005 | static ConstString g_key_qaddr("qaddr"); |
| 2006 | static ConstString g_key_dispatch_queue_t("dispatch_queue_t"); |
| 2007 | static ConstString g_key_associated_with_dispatch_queue( |
| 2008 | "associated_with_dispatch_queue"); |
| 2009 | static ConstString g_key_queue_name("qname"); |
| 2010 | static ConstString g_key_queue_kind("qkind"); |
| 2011 | static ConstString g_key_queue_serial_number("qserialnum"); |
| 2012 | static ConstString g_key_registers("registers"); |
| 2013 | static ConstString g_key_memory("memory"); |
| 2014 | static ConstString g_key_address("address"); |
| 2015 | static ConstString g_key_bytes("bytes"); |
| 2016 | static ConstString g_key_description("description"); |
| 2017 | static ConstString g_key_signal("signal"); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2018 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2019 | // Stop with signal and thread info |
| 2020 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
| 2021 | uint8_t signo = 0; |
| 2022 | std::string value; |
| 2023 | std::string thread_name; |
| 2024 | std::string reason; |
| 2025 | std::string description; |
| 2026 | uint32_t exc_type = 0; |
| 2027 | std::vector<addr_t> exc_data; |
| 2028 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
| 2029 | ExpeditedRegisterMap expedited_register_map; |
| 2030 | bool queue_vars_valid = false; |
| 2031 | addr_t dispatch_queue_t = LLDB_INVALID_ADDRESS; |
| 2032 | LazyBool associated_with_dispatch_queue = eLazyBoolCalculate; |
| 2033 | std::string queue_name; |
| 2034 | QueueKind queue_kind = eQueueKindUnknown; |
| 2035 | uint64_t queue_serial_number = 0; |
| 2036 | // Iterate through all of the thread dictionary key/value pairs from the |
| 2037 | // structured data dictionary |
| 2038 | |
| 2039 | thread_dict->ForEach([this, &tid, &expedited_register_map, &thread_name, |
| 2040 | &signo, &reason, &description, &exc_type, &exc_data, |
| 2041 | &thread_dispatch_qaddr, &queue_vars_valid, |
| 2042 | &associated_with_dispatch_queue, &dispatch_queue_t, |
| 2043 | &queue_name, &queue_kind, &queue_serial_number]( |
| 2044 | ConstString key, |
| 2045 | StructuredData::Object *object) -> bool { |
| 2046 | if (key == g_key_tid) { |
| 2047 | // thread in big endian hex |
| 2048 | tid = object->GetIntegerValue(LLDB_INVALID_THREAD_ID); |
| 2049 | } else if (key == g_key_metype) { |
| 2050 | // exception type in big endian hex |
| 2051 | exc_type = object->GetIntegerValue(0); |
| 2052 | } else if (key == g_key_medata) { |
| 2053 | // exception data in big endian hex |
| 2054 | StructuredData::Array *array = object->GetAsArray(); |
| 2055 | if (array) { |
| 2056 | array->ForEach([&exc_data](StructuredData::Object *object) -> bool { |
| 2057 | exc_data.push_back(object->GetIntegerValue()); |
| 2058 | return true; // Keep iterating through all array items |
| 2059 | }); |
| 2060 | } |
| 2061 | } else if (key == g_key_name) { |
| 2062 | thread_name = object->GetStringValue(); |
| 2063 | } else if (key == g_key_qaddr) { |
| 2064 | thread_dispatch_qaddr = object->GetIntegerValue(LLDB_INVALID_ADDRESS); |
| 2065 | } else if (key == g_key_queue_name) { |
| 2066 | queue_vars_valid = true; |
| 2067 | queue_name = object->GetStringValue(); |
| 2068 | } else if (key == g_key_queue_kind) { |
| 2069 | std::string queue_kind_str = object->GetStringValue(); |
| 2070 | if (queue_kind_str == "serial") { |
| 2071 | queue_vars_valid = true; |
| 2072 | queue_kind = eQueueKindSerial; |
| 2073 | } else if (queue_kind_str == "concurrent") { |
| 2074 | queue_vars_valid = true; |
| 2075 | queue_kind = eQueueKindConcurrent; |
| 2076 | } |
| 2077 | } else if (key == g_key_queue_serial_number) { |
| 2078 | queue_serial_number = object->GetIntegerValue(0); |
| 2079 | if (queue_serial_number != 0) |
| 2080 | queue_vars_valid = true; |
| 2081 | } else if (key == g_key_dispatch_queue_t) { |
| 2082 | dispatch_queue_t = object->GetIntegerValue(0); |
| 2083 | if (dispatch_queue_t != 0 && dispatch_queue_t != LLDB_INVALID_ADDRESS) |
| 2084 | queue_vars_valid = true; |
| 2085 | } else if (key == g_key_associated_with_dispatch_queue) { |
| 2086 | queue_vars_valid = true; |
| 2087 | bool associated = object->GetBooleanValue(); |
| 2088 | if (associated) |
| 2089 | associated_with_dispatch_queue = eLazyBoolYes; |
| 2090 | else |
| 2091 | associated_with_dispatch_queue = eLazyBoolNo; |
| 2092 | } else if (key == g_key_reason) { |
| 2093 | reason = object->GetStringValue(); |
| 2094 | } else if (key == g_key_description) { |
| 2095 | description = object->GetStringValue(); |
| 2096 | } else if (key == g_key_registers) { |
| 2097 | StructuredData::Dictionary *registers_dict = object->GetAsDictionary(); |
| 2098 | |
| 2099 | if (registers_dict) { |
| 2100 | registers_dict->ForEach( |
| 2101 | [&expedited_register_map](ConstString key, |
| 2102 | StructuredData::Object *object) -> bool { |
| 2103 | const uint32_t reg = |
| 2104 | StringConvert::ToUInt32(key.GetCString(), UINT32_MAX, 10); |
| 2105 | if (reg != UINT32_MAX) |
| 2106 | expedited_register_map[reg] = object->GetStringValue(); |
| 2107 | return true; // Keep iterating through all array items |
| 2108 | }); |
| 2109 | } |
| 2110 | } else if (key == g_key_memory) { |
| 2111 | StructuredData::Array *array = object->GetAsArray(); |
| 2112 | if (array) { |
| 2113 | array->ForEach([this](StructuredData::Object *object) -> bool { |
| 2114 | StructuredData::Dictionary *mem_cache_dict = |
| 2115 | object->GetAsDictionary(); |
| 2116 | if (mem_cache_dict) { |
| 2117 | lldb::addr_t mem_cache_addr = LLDB_INVALID_ADDRESS; |
| 2118 | if (mem_cache_dict->GetValueForKeyAsInteger<lldb::addr_t>( |
| 2119 | "address", mem_cache_addr)) { |
| 2120 | if (mem_cache_addr != LLDB_INVALID_ADDRESS) { |
| 2121 | StringExtractor bytes; |
| 2122 | if (mem_cache_dict->GetValueForKeyAsString( |
| 2123 | "bytes", bytes.GetStringRef())) { |
| 2124 | bytes.SetFilePos(0); |
| 2125 | |
| 2126 | const size_t byte_size = bytes.GetStringRef().size() / 2; |
| 2127 | DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0)); |
| 2128 | const size_t bytes_copied = |
| 2129 | bytes.GetHexBytes(data_buffer_sp->GetData(), 0); |
| 2130 | if (bytes_copied == byte_size) |
| 2131 | m_memory_cache.AddL1CacheData(mem_cache_addr, |
| 2132 | data_buffer_sp); |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | return true; // Keep iterating through all array items |
| 2138 | }); |
| 2139 | } |
| 2140 | |
| 2141 | } else if (key == g_key_signal) |
| 2142 | signo = object->GetIntegerValue(LLDB_INVALID_SIGNAL_NUMBER); |
| 2143 | return true; // Keep iterating through all dictionary key/value pairs |
| 2144 | }); |
| 2145 | |
| 2146 | return SetThreadStopInfo(tid, expedited_register_map, signo, thread_name, |
| 2147 | reason, description, exc_type, exc_data, |
| 2148 | thread_dispatch_qaddr, queue_vars_valid, |
| 2149 | associated_with_dispatch_queue, dispatch_queue_t, |
| 2150 | queue_name, queue_kind, queue_serial_number); |
| 2151 | } |
| 2152 | |
| 2153 | StateType ProcessGDBRemote::SetThreadStopInfo(StringExtractor &stop_packet) { |
| 2154 | stop_packet.SetFilePos(0); |
| 2155 | const char stop_type = stop_packet.GetChar(); |
| 2156 | switch (stop_type) { |
| 2157 | case 'T': |
| 2158 | case 'S': { |
| 2159 | // This is a bit of a hack, but is is required. If we did exec, we |
| 2160 | // need to clear our thread lists and also know to rebuild our dynamic |
| 2161 | // register info before we lookup and threads and populate the expedited |
| 2162 | // register values so we need to know this right away so we can cleanup |
| 2163 | // and update our registers. |
| 2164 | const uint32_t stop_id = GetStopID(); |
| 2165 | if (stop_id == 0) { |
| 2166 | // Our first stop, make sure we have a process ID, and also make |
| 2167 | // sure we know about our registers |
| 2168 | if (GetID() == LLDB_INVALID_PROCESS_ID) { |
| 2169 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); |
| 2170 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 2171 | SetID(pid); |
| 2172 | } |
| 2173 | BuildDynamicRegisterInfo(true); |
| 2174 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2175 | // Stop with signal and thread info |
| 2176 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2177 | const uint8_t signo = stop_packet.GetHexU8(); |
| 2178 | llvm::StringRef key; |
| 2179 | llvm::StringRef value; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2180 | std::string thread_name; |
| 2181 | std::string reason; |
| 2182 | std::string description; |
| 2183 | uint32_t exc_type = 0; |
| 2184 | std::vector<addr_t> exc_data; |
| 2185 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2186 | bool queue_vars_valid = |
| 2187 | false; // says if locals below that start with "queue_" are valid |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2188 | addr_t dispatch_queue_t = LLDB_INVALID_ADDRESS; |
| 2189 | LazyBool associated_with_dispatch_queue = eLazyBoolCalculate; |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2190 | std::string queue_name; |
| 2191 | QueueKind queue_kind = eQueueKindUnknown; |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2192 | uint64_t queue_serial_number = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2193 | ExpeditedRegisterMap expedited_register_map; |
| 2194 | while (stop_packet.GetNameColonValue(key, value)) { |
| 2195 | if (key.compare("metype") == 0) { |
| 2196 | // exception type in big endian hex |
| 2197 | value.getAsInteger(16, exc_type); |
| 2198 | } else if (key.compare("medata") == 0) { |
| 2199 | // exception data in big endian hex |
| 2200 | uint64_t x; |
| 2201 | value.getAsInteger(16, x); |
| 2202 | exc_data.push_back(x); |
| 2203 | } else if (key.compare("thread") == 0) { |
| 2204 | // thread in big endian hex |
| 2205 | if (value.getAsInteger(16, tid)) |
| 2206 | tid = LLDB_INVALID_THREAD_ID; |
| 2207 | } else if (key.compare("threads") == 0) { |
| 2208 | std::lock_guard<std::recursive_mutex> guard( |
| 2209 | m_thread_list_real.GetMutex()); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2210 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2211 | m_thread_ids.clear(); |
| 2212 | // A comma separated list of all threads in the current |
| 2213 | // process that includes the thread for this stop reply |
| 2214 | // packet |
| 2215 | lldb::tid_t tid; |
| 2216 | while (!value.empty()) { |
| 2217 | llvm::StringRef tid_str; |
| 2218 | std::tie(tid_str, value) = value.split(','); |
| 2219 | if (tid_str.getAsInteger(16, tid)) |
| 2220 | tid = LLDB_INVALID_THREAD_ID; |
| 2221 | m_thread_ids.push_back(tid); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2222 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2223 | } else if (key.compare("thread-pcs") == 0) { |
| 2224 | m_thread_pcs.clear(); |
| 2225 | // A comma separated list of all threads in the current |
| 2226 | // process that includes the thread for this stop reply |
| 2227 | // packet |
| 2228 | lldb::addr_t pc; |
| 2229 | while (!value.empty()) { |
| 2230 | llvm::StringRef pc_str; |
| 2231 | std::tie(pc_str, value) = value.split(','); |
| 2232 | if (pc_str.getAsInteger(16, pc)) |
| 2233 | pc = LLDB_INVALID_ADDRESS; |
| 2234 | m_thread_pcs.push_back(pc); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2235 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2236 | } else if (key.compare("jstopinfo") == 0) { |
| 2237 | StringExtractor json_extractor(value); |
| 2238 | std::string json; |
| 2239 | // Now convert the HEX bytes into a string value |
| 2240 | json_extractor.GetHexByteString(json); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2242 | // This JSON contains thread IDs and thread stop info for all threads. |
| 2243 | // It doesn't contain expedited registers, memory or queue info. |
| 2244 | m_jstopinfo_sp = StructuredData::ParseJSON(json); |
| 2245 | } else if (key.compare("hexname") == 0) { |
| 2246 | StringExtractor name_extractor(value); |
| 2247 | std::string name; |
| 2248 | // Now convert the HEX bytes into a string value |
| 2249 | name_extractor.GetHexByteString(thread_name); |
| 2250 | } else if (key.compare("name") == 0) { |
| 2251 | thread_name = value; |
| 2252 | } else if (key.compare("qaddr") == 0) { |
| 2253 | value.getAsInteger(16, thread_dispatch_qaddr); |
| 2254 | } else if (key.compare("dispatch_queue_t") == 0) { |
| 2255 | queue_vars_valid = true; |
| 2256 | value.getAsInteger(16, dispatch_queue_t); |
| 2257 | } else if (key.compare("qname") == 0) { |
| 2258 | queue_vars_valid = true; |
| 2259 | StringExtractor name_extractor(value); |
| 2260 | // Now convert the HEX bytes into a string value |
| 2261 | name_extractor.GetHexByteString(queue_name); |
| 2262 | } else if (key.compare("qkind") == 0) { |
| 2263 | queue_kind = llvm::StringSwitch<QueueKind>(value) |
| 2264 | .Case("serial", eQueueKindSerial) |
| 2265 | .Case("concurrent", eQueueKindConcurrent) |
| 2266 | .Default(eQueueKindUnknown); |
| 2267 | queue_vars_valid = queue_kind != eQueueKindUnknown; |
| 2268 | } else if (key.compare("qserialnum") == 0) { |
| 2269 | if (!value.getAsInteger(0, queue_serial_number)) |
| 2270 | queue_vars_valid = true; |
| 2271 | } else if (key.compare("reason") == 0) { |
| 2272 | reason = value; |
| 2273 | } else if (key.compare("description") == 0) { |
| 2274 | StringExtractor desc_extractor(value); |
| 2275 | // Now convert the HEX bytes into a string value |
| 2276 | desc_extractor.GetHexByteString(description); |
| 2277 | } else if (key.compare("memory") == 0) { |
| 2278 | // Expedited memory. GDB servers can choose to send back expedited |
| 2279 | // memory |
| 2280 | // that can populate the L1 memory cache in the process so that things |
| 2281 | // like |
| 2282 | // the frame pointer backchain can be expedited. This will help stack |
| 2283 | // backtracing be more efficient by not having to send as many memory |
| 2284 | // read |
| 2285 | // requests down the remote GDB server. |
| 2286 | |
| 2287 | // Key/value pair format: memory:<addr>=<bytes>; |
| 2288 | // <addr> is a number whose base will be interpreted by the prefix: |
| 2289 | // "0x[0-9a-fA-F]+" for hex |
| 2290 | // "0[0-7]+" for octal |
| 2291 | // "[1-9]+" for decimal |
| 2292 | // <bytes> is native endian ASCII hex bytes just like the register |
| 2293 | // values |
| 2294 | llvm::StringRef addr_str, bytes_str; |
| 2295 | std::tie(addr_str, bytes_str) = value.split('='); |
| 2296 | if (!addr_str.empty() && !bytes_str.empty()) { |
| 2297 | lldb::addr_t mem_cache_addr = LLDB_INVALID_ADDRESS; |
| 2298 | if (!addr_str.getAsInteger(0, mem_cache_addr)) { |
| 2299 | StringExtractor bytes(bytes_str); |
| 2300 | const size_t byte_size = bytes.GetBytesLeft() / 2; |
| 2301 | DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0)); |
| 2302 | const size_t bytes_copied = |
| 2303 | bytes.GetHexBytes(data_buffer_sp->GetData(), 0); |
| 2304 | if (bytes_copied == byte_size) |
| 2305 | m_memory_cache.AddL1CacheData(mem_cache_addr, data_buffer_sp); |
| 2306 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2307 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2308 | } else if (key.compare("watch") == 0 || key.compare("rwatch") == 0 || |
| 2309 | key.compare("awatch") == 0) { |
| 2310 | // Support standard GDB remote stop reply packet 'TAAwatch:addr' |
| 2311 | lldb::addr_t wp_addr = LLDB_INVALID_ADDRESS; |
| 2312 | value.getAsInteger(16, wp_addr); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2313 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2314 | WatchpointSP wp_sp = |
| 2315 | GetTarget().GetWatchpointList().FindByAddress(wp_addr); |
| 2316 | uint32_t wp_index = LLDB_INVALID_INDEX32; |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 2317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2318 | if (wp_sp) |
| 2319 | wp_index = wp_sp->GetHardwareIndex(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2320 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2321 | reason = "watchpoint"; |
| 2322 | StreamString ostr; |
| 2323 | ostr.Printf("%" PRIu64 " %" PRIu32, wp_addr, wp_index); |
| 2324 | description = ostr.GetString().c_str(); |
| 2325 | } else if (key.compare("library") == 0) { |
| 2326 | LoadModules(); |
| 2327 | } else if (key.size() == 2 && ::isxdigit(key[0]) && ::isxdigit(key[1])) { |
| 2328 | uint32_t reg = UINT32_MAX; |
| 2329 | if (!key.getAsInteger(16, reg)) |
| 2330 | expedited_register_map[reg] = std::move(value); |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | if (tid == LLDB_INVALID_THREAD_ID) { |
| 2335 | // A thread id may be invalid if the response is old style 'S' packet |
| 2336 | // which does not provide the |
| 2337 | // thread information. So update the thread list and choose the first one. |
| 2338 | UpdateThreadIDList(); |
| 2339 | |
| 2340 | if (!m_thread_ids.empty()) { |
| 2341 | tid = m_thread_ids.front(); |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | ThreadSP thread_sp = SetThreadStopInfo( |
| 2346 | tid, expedited_register_map, signo, thread_name, reason, description, |
| 2347 | exc_type, exc_data, thread_dispatch_qaddr, queue_vars_valid, |
| 2348 | associated_with_dispatch_queue, dispatch_queue_t, queue_name, |
| 2349 | queue_kind, queue_serial_number); |
| 2350 | |
| 2351 | return eStateStopped; |
| 2352 | } break; |
| 2353 | |
| 2354 | case 'W': |
| 2355 | case 'X': |
| 2356 | // process exited |
| 2357 | return eStateExited; |
| 2358 | |
| 2359 | default: |
| 2360 | break; |
| 2361 | } |
| 2362 | return eStateInvalid; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2363 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2364 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2365 | void ProcessGDBRemote::RefreshStateAfterStop() { |
| 2366 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 2367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2368 | m_thread_ids.clear(); |
| 2369 | m_thread_pcs.clear(); |
| 2370 | // Set the thread stop info. It might have a "threads" key whose value is |
| 2371 | // a list of all thread IDs in the current process, so m_thread_ids might |
| 2372 | // get set. |
Greg Clayton | a5801ad | 2015-07-15 22:59:03 +0000 | [diff] [blame] | 2373 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2374 | // Scope for the lock |
| 2375 | { |
| 2376 | // Lock the thread stack while we access it |
| 2377 | std::lock_guard<std::recursive_mutex> guard(m_last_stop_packet_mutex); |
| 2378 | // Get the number of stop packets on the stack |
| 2379 | int nItems = m_stop_packet_stack.size(); |
| 2380 | // Iterate over them |
| 2381 | for (int i = 0; i < nItems; i++) { |
| 2382 | // Get the thread stop info |
| 2383 | StringExtractorGDBRemote stop_info = m_stop_packet_stack[i]; |
| 2384 | // Process thread stop info |
| 2385 | SetThreadStopInfo(stop_info); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2386 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2387 | // Clear the thread stop stack |
| 2388 | m_stop_packet_stack.clear(); |
| 2389 | } |
| 2390 | |
| 2391 | // Check to see if SetThreadStopInfo() filled in m_thread_ids? |
| 2392 | if (m_thread_ids.empty()) { |
| 2393 | // No, we need to fetch the thread list manually |
| 2394 | UpdateThreadIDList(); |
| 2395 | } |
| 2396 | |
| 2397 | // If we have queried for a default thread id |
| 2398 | if (m_initial_tid != LLDB_INVALID_THREAD_ID) { |
| 2399 | m_thread_list.SetSelectedThreadByID(m_initial_tid); |
| 2400 | m_initial_tid = LLDB_INVALID_THREAD_ID; |
| 2401 | } |
| 2402 | |
| 2403 | // Let all threads recover from stopping and do any clean up based |
| 2404 | // on the previous thread state (if any). |
| 2405 | m_thread_list_real.RefreshStateAfterStop(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2408 | Error ProcessGDBRemote::DoHalt(bool &caused_stop) { |
| 2409 | Error error; |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 2410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2411 | if (m_public_state.GetValue() == eStateAttaching) { |
| 2412 | // We are being asked to halt during an attach. We need to just close |
| 2413 | // our file handle and debugserver will go away, and we can be done... |
| 2414 | m_gdb_comm.Disconnect(); |
| 2415 | } else |
| 2416 | caused_stop = m_gdb_comm.Interrupt(); |
| 2417 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2420 | Error ProcessGDBRemote::DoDetach(bool keep_stopped) { |
| 2421 | Error error; |
| 2422 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2423 | if (log) |
| 2424 | log->Printf("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2425 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2426 | error = m_gdb_comm.Detach(keep_stopped); |
| 2427 | if (log) { |
| 2428 | if (error.Success()) |
| 2429 | log->PutCString( |
| 2430 | "ProcessGDBRemote::DoDetach() detach packet sent successfully"); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2431 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2432 | log->Printf("ProcessGDBRemote::DoDetach() detach packet send failed: %s", |
| 2433 | error.AsCString() ? error.AsCString() : "<unknown error>"); |
| 2434 | } |
| 2435 | |
| 2436 | if (!error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2437 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Sleep for one second to let the process get all detached... |
| 2440 | StopAsyncThread(); |
| 2441 | |
| 2442 | SetPrivateState(eStateDetached); |
| 2443 | ResumePrivateStateThread(); |
| 2444 | |
| 2445 | // KillDebugserverProcess (); |
| 2446 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2449 | Error ProcessGDBRemote::DoDestroy() { |
| 2450 | Error error; |
| 2451 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2452 | if (log) |
| 2453 | log->Printf("ProcessGDBRemote::DoDestroy()"); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2454 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2455 | // There is a bug in older iOS debugservers where they don't shut down the |
| 2456 | // process |
| 2457 | // they are debugging properly. If the process is sitting at a breakpoint or |
| 2458 | // an exception, |
| 2459 | // this can cause problems with restarting. So we check to see if any of our |
| 2460 | // threads are stopped |
| 2461 | // at a breakpoint, and if so we remove all the breakpoints, resume the |
| 2462 | // process, and THEN |
| 2463 | // destroy it again. |
| 2464 | // |
| 2465 | // Note, we don't have a good way to test the version of debugserver, but I |
| 2466 | // happen to know that |
| 2467 | // the set of all the iOS debugservers which don't support |
| 2468 | // GetThreadSuffixSupported() and that of |
| 2469 | // the debugservers with this bug are equal. There really should be a better |
| 2470 | // way to test this! |
| 2471 | // |
| 2472 | // We also use m_destroy_tried_resuming to make sure we only do this once, if |
| 2473 | // we resume and then halt and |
| 2474 | // get called here to destroy again and we're still at a breakpoint or |
| 2475 | // exception, then we should |
| 2476 | // just do the straight-forward kill. |
| 2477 | // |
| 2478 | // And of course, if we weren't able to stop the process by the time we get |
| 2479 | // here, it isn't |
| 2480 | // necessary (or helpful) to do any of this. |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2481 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2482 | if (!m_gdb_comm.GetThreadSuffixSupported() && |
| 2483 | m_public_state.GetValue() != eStateRunning) { |
| 2484 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2485 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2486 | // FIXME: These should be ConstStrings so we aren't doing strcmp'ing. |
| 2487 | if (platform_sp && platform_sp->GetName() && |
| 2488 | platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic()) { |
| 2489 | if (m_destroy_tried_resuming) { |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2490 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2491 | log->PutCString("ProcessGDBRemote::DoDestroy() - Tried resuming to " |
| 2492 | "destroy once already, not doing it again."); |
| 2493 | } else { |
| 2494 | // At present, the plans are discarded and the breakpoints disabled |
| 2495 | // Process::Destroy, |
| 2496 | // but we really need it to happen here and it doesn't matter if we do |
| 2497 | // it twice. |
| 2498 | m_thread_list.DiscardThreadPlans(); |
| 2499 | DisableAllBreakpointSites(); |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2500 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2501 | bool stop_looks_like_crash = false; |
| 2502 | ThreadList &threads = GetThreadList(); |
| 2503 | |
| 2504 | { |
| 2505 | std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); |
| 2506 | |
| 2507 | size_t num_threads = threads.GetSize(); |
| 2508 | for (size_t i = 0; i < num_threads; i++) { |
| 2509 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
| 2510 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
| 2511 | StopReason reason = eStopReasonInvalid; |
| 2512 | if (stop_info_sp) |
| 2513 | reason = stop_info_sp->GetStopReason(); |
| 2514 | if (reason == eStopReasonBreakpoint || |
| 2515 | reason == eStopReasonException) { |
| 2516 | if (log) |
| 2517 | log->Printf( |
| 2518 | "ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 |
| 2519 | " stopped with reason: %s.", |
| 2520 | thread_sp->GetProtocolID(), stop_info_sp->GetDescription()); |
| 2521 | stop_looks_like_crash = true; |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | if (stop_looks_like_crash) { |
| 2528 | if (log) |
| 2529 | log->PutCString("ProcessGDBRemote::DoDestroy() - Stopped at a " |
| 2530 | "breakpoint, continue and then kill."); |
| 2531 | m_destroy_tried_resuming = true; |
| 2532 | |
| 2533 | // If we are going to run again before killing, it would be good to |
| 2534 | // suspend all the threads |
| 2535 | // before resuming so they won't get into more trouble. Sadly, for |
| 2536 | // the threads stopped with |
| 2537 | // the breakpoint or exception, the exception doesn't get cleared if |
| 2538 | // it is suspended, so we do |
| 2539 | // have to run the risk of letting those threads proceed a bit. |
| 2540 | |
| 2541 | { |
| 2542 | std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); |
| 2543 | |
| 2544 | size_t num_threads = threads.GetSize(); |
| 2545 | for (size_t i = 0; i < num_threads; i++) { |
| 2546 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
| 2547 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
| 2548 | StopReason reason = eStopReasonInvalid; |
| 2549 | if (stop_info_sp) |
| 2550 | reason = stop_info_sp->GetStopReason(); |
| 2551 | if (reason != eStopReasonBreakpoint && |
| 2552 | reason != eStopReasonException) { |
| 2553 | if (log) |
| 2554 | log->Printf("ProcessGDBRemote::DoDestroy() - Suspending " |
| 2555 | "thread: 0x%4.4" PRIx64 " before running.", |
| 2556 | thread_sp->GetProtocolID()); |
| 2557 | thread_sp->SetResumeState(eStateSuspended); |
| 2558 | } |
| 2559 | } |
| 2560 | } |
| 2561 | Resume(); |
| 2562 | return Destroy(false); |
| 2563 | } |
| 2564 | } |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2565 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2566 | } |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2567 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2568 | // Interrupt if our inferior is running... |
| 2569 | int exit_status = SIGABRT; |
| 2570 | std::string exit_string; |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2571 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2572 | if (m_gdb_comm.IsConnected()) { |
| 2573 | if (m_public_state.GetValue() != eStateAttaching) { |
| 2574 | StringExtractorGDBRemote response; |
| 2575 | bool send_async = true; |
| 2576 | GDBRemoteCommunication::ScopedTimeout(m_gdb_comm, 3); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2577 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2578 | if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, |
| 2579 | send_async) == |
| 2580 | GDBRemoteCommunication::PacketResult::Success) { |
| 2581 | char packet_cmd = response.GetChar(0); |
| 2582 | |
| 2583 | if (packet_cmd == 'W' || packet_cmd == 'X') { |
| 2584 | #if defined(__APPLE__) |
| 2585 | // For Native processes on Mac OS X, we launch through the Host |
| 2586 | // Platform, then hand the process off |
| 2587 | // to debugserver, which becomes the parent process through |
| 2588 | // "PT_ATTACH". Then when we go to kill |
| 2589 | // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then we |
| 2590 | // call waitpid which returns |
| 2591 | // with no error and the correct status. But amusingly enough that |
| 2592 | // doesn't seem to actually reap |
| 2593 | // the process, but instead it is left around as a Zombie. Probably |
| 2594 | // the kernel is in the process of |
| 2595 | // switching ownership back to lldb which was the original parent, and |
| 2596 | // gets confused in the handoff. |
| 2597 | // Anyway, so call waitpid here to finally reap it. |
| 2598 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2599 | if (platform_sp && platform_sp->IsHost()) { |
| 2600 | int status; |
| 2601 | ::pid_t reap_pid; |
| 2602 | reap_pid = waitpid(GetID(), &status, WNOHANG); |
| 2603 | if (log) |
| 2604 | log->Printf("Reaped pid: %d, status: %d.\n", reap_pid, status); |
| 2605 | } |
| 2606 | #endif |
| 2607 | SetLastStopPacket(response); |
| 2608 | ClearThreadIDList(); |
| 2609 | exit_status = response.GetHexU8(); |
| 2610 | } else { |
| 2611 | if (log) |
| 2612 | log->Printf("ProcessGDBRemote::DoDestroy - got unexpected response " |
| 2613 | "to k packet: %s", |
| 2614 | response.GetStringRef().c_str()); |
| 2615 | exit_string.assign("got unexpected response to k packet: "); |
| 2616 | exit_string.append(response.GetStringRef()); |
| 2617 | } |
| 2618 | } else { |
| 2619 | if (log) |
| 2620 | log->Printf("ProcessGDBRemote::DoDestroy - failed to send k packet"); |
| 2621 | exit_string.assign("failed to send the k packet"); |
| 2622 | } |
| 2623 | } else { |
| 2624 | if (log) |
| 2625 | log->Printf("ProcessGDBRemote::DoDestroy - killed or interrupted while " |
| 2626 | "attaching"); |
| 2627 | exit_string.assign("killed or interrupted while attaching."); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2628 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2629 | } else { |
| 2630 | // If we missed setting the exit status on the way out, do it here. |
| 2631 | // NB set exit status can be called multiple times, the first one sets the |
| 2632 | // status. |
| 2633 | exit_string.assign("destroying when not connected to debugserver"); |
| 2634 | } |
| 2635 | |
| 2636 | SetExitStatus(exit_status, exit_string.c_str()); |
| 2637 | |
| 2638 | StopAsyncThread(); |
| 2639 | KillDebugserverProcess(); |
| 2640 | return error; |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2643 | void ProcessGDBRemote::SetLastStopPacket( |
| 2644 | const StringExtractorGDBRemote &response) { |
| 2645 | const bool did_exec = |
| 2646 | response.GetStringRef().find(";reason:exec;") != std::string::npos; |
| 2647 | if (did_exec) { |
| 2648 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2649 | if (log) |
| 2650 | log->Printf("ProcessGDBRemote::SetLastStopPacket () - detected exec"); |
| 2651 | |
| 2652 | m_thread_list_real.Clear(); |
| 2653 | m_thread_list.Clear(); |
| 2654 | BuildDynamicRegisterInfo(true); |
| 2655 | m_gdb_comm.ResetDiscoverableSettings(did_exec); |
| 2656 | } |
| 2657 | |
| 2658 | // Scope the lock |
| 2659 | { |
| 2660 | // Lock the thread stack while we access it |
| 2661 | std::lock_guard<std::recursive_mutex> guard(m_last_stop_packet_mutex); |
| 2662 | |
| 2663 | // We are are not using non-stop mode, there can only be one last stop |
| 2664 | // reply packet, so clear the list. |
| 2665 | if (GetTarget().GetNonStopModeEnabled() == false) |
| 2666 | m_stop_packet_stack.clear(); |
| 2667 | |
| 2668 | // Add this stop packet to the stop packet stack |
| 2669 | // This stack will get popped and examined when we switch to the |
| 2670 | // Stopped state |
| 2671 | m_stop_packet_stack.push_back(response); |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | void ProcessGDBRemote::SetUnixSignals(const UnixSignalsSP &signals_sp) { |
| 2676 | Process::SetUnixSignals(std::make_shared<GDBRemoteSignals>(signals_sp)); |
Chaoren Lin | c963a22 | 2015-09-01 16:58:45 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2679 | //------------------------------------------------------------------ |
| 2680 | // Process Queries |
| 2681 | //------------------------------------------------------------------ |
| 2682 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2683 | bool ProcessGDBRemote::IsAlive() { |
| 2684 | return m_gdb_comm.IsConnected() && Process::IsAlive(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2687 | addr_t ProcessGDBRemote::GetImageInfoAddress() { |
| 2688 | // request the link map address via the $qShlibInfoAddr packet |
| 2689 | lldb::addr_t addr = m_gdb_comm.GetShlibInfoAddr(); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 2690 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2691 | // the loaded module list can also provides a link map address |
| 2692 | if (addr == LLDB_INVALID_ADDRESS) { |
| 2693 | LoadedModuleInfoList list; |
| 2694 | if (GetLoadedModuleList(list).Success()) |
| 2695 | addr = list.m_link_map; |
| 2696 | } |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 2697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2698 | return addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2701 | void ProcessGDBRemote::WillPublicStop() { |
| 2702 | // See if the GDB remote client supports the JSON threads info. |
| 2703 | // If so, we gather stop info for all threads, expedited registers, |
| 2704 | // expedited memory, runtime queue information (iOS and MacOSX only), |
| 2705 | // and more. Expediting memory will help stack backtracing be much |
| 2706 | // faster. Expediting registers will make sure we don't have to read |
| 2707 | // the thread registers for GPRs. |
| 2708 | m_jthreadsinfo_sp = m_gdb_comm.GetThreadsInfo(); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2709 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2710 | if (m_jthreadsinfo_sp) { |
| 2711 | // Now set the stop info for each thread and also expedite any registers |
| 2712 | // and memory that was in the jThreadsInfo response. |
| 2713 | StructuredData::Array *thread_infos = m_jthreadsinfo_sp->GetAsArray(); |
| 2714 | if (thread_infos) { |
| 2715 | const size_t n = thread_infos->GetSize(); |
| 2716 | for (size_t i = 0; i < n; ++i) { |
| 2717 | StructuredData::Dictionary *thread_dict = |
| 2718 | thread_infos->GetItemAtIndex(i)->GetAsDictionary(); |
| 2719 | if (thread_dict) |
| 2720 | SetThreadStopInfo(thread_dict); |
| 2721 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2722 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2723 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2726 | //------------------------------------------------------------------ |
| 2727 | // Process Memory |
| 2728 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2729 | size_t ProcessGDBRemote::DoReadMemory(addr_t addr, void *buf, size_t size, |
| 2730 | Error &error) { |
| 2731 | GetMaxMemorySize(); |
| 2732 | if (size > m_max_memory_size) { |
| 2733 | // Keep memory read sizes down to a sane limit. This function will be |
| 2734 | // called multiple times in order to complete the task by |
| 2735 | // lldb_private::Process so it is ok to do this. |
| 2736 | size = m_max_memory_size; |
| 2737 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2738 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2739 | char packet[64]; |
| 2740 | int packet_len; |
| 2741 | bool binary_memory_read = m_gdb_comm.GetxPacketSupported(); |
| 2742 | packet_len = ::snprintf(packet, sizeof(packet), "%c%" PRIx64 ",%" PRIx64, |
| 2743 | binary_memory_read ? 'x' : 'm', (uint64_t)addr, |
| 2744 | (uint64_t)size); |
| 2745 | assert(packet_len + 1 < (int)sizeof(packet)); |
| 2746 | StringExtractorGDBRemote response; |
| 2747 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, |
| 2748 | true) == |
| 2749 | GDBRemoteCommunication::PacketResult::Success) { |
| 2750 | if (response.IsNormalResponse()) { |
| 2751 | error.Clear(); |
| 2752 | if (binary_memory_read) { |
| 2753 | // The lower level GDBRemoteCommunication packet receive layer has |
| 2754 | // already de-quoted any |
| 2755 | // 0x7d character escaping that was present in the packet |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2756 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2757 | size_t data_received_size = response.GetBytesLeft(); |
| 2758 | if (data_received_size > size) { |
| 2759 | // Don't write past the end of BUF if the remote debug server gave us |
| 2760 | // too |
| 2761 | // much data for some reason. |
| 2762 | data_received_size = size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2763 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2764 | memcpy(buf, response.GetStringRef().data(), data_received_size); |
| 2765 | return data_received_size; |
| 2766 | } else { |
| 2767 | return response.GetHexBytes( |
| 2768 | llvm::MutableArrayRef<uint8_t>((uint8_t *)buf, size), '\xdd'); |
| 2769 | } |
| 2770 | } else if (response.IsErrorResponse()) |
| 2771 | error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); |
| 2772 | else if (response.IsUnsupportedResponse()) |
| 2773 | error.SetErrorStringWithFormat( |
| 2774 | "GDB server does not support reading memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2775 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2776 | error.SetErrorStringWithFormat( |
| 2777 | "unexpected response to GDB server memory read packet '%s': '%s'", |
| 2778 | packet, response.GetStringRef().c_str()); |
| 2779 | } else { |
| 2780 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); |
| 2781 | } |
| 2782 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2785 | size_t ProcessGDBRemote::DoWriteMemory(addr_t addr, const void *buf, |
| 2786 | size_t size, Error &error) { |
| 2787 | GetMaxMemorySize(); |
| 2788 | if (size > m_max_memory_size) { |
| 2789 | // Keep memory read sizes down to a sane limit. This function will be |
| 2790 | // called multiple times in order to complete the task by |
| 2791 | // lldb_private::Process so it is ok to do this. |
| 2792 | size = m_max_memory_size; |
| 2793 | } |
Greg Clayton | b4aaf2e | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 2794 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2795 | StreamString packet; |
| 2796 | packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); |
| 2797 | packet.PutBytesAsRawHex8(buf, size, endian::InlHostByteOrder(), |
| 2798 | endian::InlHostByteOrder()); |
| 2799 | StringExtractorGDBRemote response; |
| 2800 | if (m_gdb_comm.SendPacketAndWaitForResponse( |
| 2801 | packet.GetData(), packet.GetSize(), response, true) == |
| 2802 | GDBRemoteCommunication::PacketResult::Success) { |
| 2803 | if (response.IsOKResponse()) { |
| 2804 | error.Clear(); |
| 2805 | return size; |
| 2806 | } else if (response.IsErrorResponse()) |
| 2807 | error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, |
| 2808 | addr); |
| 2809 | else if (response.IsUnsupportedResponse()) |
| 2810 | error.SetErrorStringWithFormat( |
| 2811 | "GDB server does not support writing memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2812 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2813 | error.SetErrorStringWithFormat( |
| 2814 | "unexpected response to GDB server memory write packet '%s': '%s'", |
| 2815 | packet.GetString().c_str(), response.GetStringRef().c_str()); |
| 2816 | } else { |
| 2817 | error.SetErrorStringWithFormat("failed to send packet: '%s'", |
| 2818 | packet.GetString().c_str()); |
| 2819 | } |
| 2820 | return 0; |
| 2821 | } |
| 2822 | |
| 2823 | lldb::addr_t ProcessGDBRemote::DoAllocateMemory(size_t size, |
| 2824 | uint32_t permissions, |
| 2825 | Error &error) { |
| 2826 | Log *log( |
| 2827 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_EXPRESSIONS)); |
| 2828 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 2829 | |
| 2830 | if (m_gdb_comm.SupportsAllocDeallocMemory() != eLazyBoolNo) { |
| 2831 | allocated_addr = m_gdb_comm.AllocateMemory(size, permissions); |
| 2832 | if (allocated_addr != LLDB_INVALID_ADDRESS || |
| 2833 | m_gdb_comm.SupportsAllocDeallocMemory() == eLazyBoolYes) |
| 2834 | return allocated_addr; |
| 2835 | } |
| 2836 | |
| 2837 | if (m_gdb_comm.SupportsAllocDeallocMemory() == eLazyBoolNo) { |
| 2838 | // Call mmap() to create memory in the inferior.. |
| 2839 | unsigned prot = 0; |
| 2840 | if (permissions & lldb::ePermissionsReadable) |
| 2841 | prot |= eMmapProtRead; |
| 2842 | if (permissions & lldb::ePermissionsWritable) |
| 2843 | prot |= eMmapProtWrite; |
| 2844 | if (permissions & lldb::ePermissionsExecutable) |
| 2845 | prot |= eMmapProtExec; |
| 2846 | |
| 2847 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 2848 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) |
| 2849 | m_addr_to_mmap_size[allocated_addr] = size; |
| 2850 | else { |
| 2851 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 2852 | if (log) |
| 2853 | log->Printf("ProcessGDBRemote::%s no direct stub support for memory " |
| 2854 | "allocation, and InferiorCallMmap also failed - is stub " |
| 2855 | "missing register context save/restore capability?", |
| 2856 | __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2857 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | if (allocated_addr == LLDB_INVALID_ADDRESS) |
| 2861 | error.SetErrorStringWithFormat( |
| 2862 | "unable to allocate %" PRIu64 " bytes of memory with permissions %s", |
| 2863 | (uint64_t)size, GetPermissionsAsCString(permissions)); |
| 2864 | else |
| 2865 | error.Clear(); |
| 2866 | return allocated_addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2869 | Error ProcessGDBRemote::GetMemoryRegionInfo(addr_t load_addr, |
| 2870 | MemoryRegionInfo ®ion_info) { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2871 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2872 | Error error(m_gdb_comm.GetMemoryRegionInfo(load_addr, region_info)); |
| 2873 | return error; |
| 2874 | } |
| 2875 | |
| 2876 | Error ProcessGDBRemote::GetWatchpointSupportInfo(uint32_t &num) { |
| 2877 | |
| 2878 | Error error(m_gdb_comm.GetWatchpointSupportInfo(num)); |
| 2879 | return error; |
| 2880 | } |
| 2881 | |
| 2882 | Error ProcessGDBRemote::GetWatchpointSupportInfo(uint32_t &num, bool &after) { |
| 2883 | Error error(m_gdb_comm.GetWatchpointSupportInfo( |
| 2884 | num, after, GetTarget().GetArchitecture())); |
| 2885 | return error; |
| 2886 | } |
| 2887 | |
| 2888 | Error ProcessGDBRemote::DoDeallocateMemory(lldb::addr_t addr) { |
| 2889 | Error error; |
| 2890 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
| 2891 | |
| 2892 | switch (supported) { |
| 2893 | case eLazyBoolCalculate: |
| 2894 | // We should never be deallocating memory without allocating memory |
| 2895 | // first so we should never get eLazyBoolCalculate |
| 2896 | error.SetErrorString( |
| 2897 | "tried to deallocate memory without ever allocating memory"); |
| 2898 | break; |
| 2899 | |
| 2900 | case eLazyBoolYes: |
| 2901 | if (!m_gdb_comm.DeallocateMemory(addr)) |
| 2902 | error.SetErrorStringWithFormat( |
| 2903 | "unable to deallocate memory at 0x%" PRIx64, addr); |
| 2904 | break; |
| 2905 | |
| 2906 | case eLazyBoolNo: |
| 2907 | // Call munmap() to deallocate memory in the inferior.. |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2908 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2909 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 2910 | if (pos != m_addr_to_mmap_size.end() && |
| 2911 | InferiorCallMunmap(this, addr, pos->second)) |
| 2912 | m_addr_to_mmap_size.erase(pos); |
| 2913 | else |
| 2914 | error.SetErrorStringWithFormat( |
| 2915 | "unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | cec91ef | 2016-02-26 01:20:20 +0000 | [diff] [blame] | 2916 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2917 | break; |
| 2918 | } |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2919 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2920 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2923 | //------------------------------------------------------------------ |
| 2924 | // Process STDIO |
| 2925 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2926 | size_t ProcessGDBRemote::PutSTDIN(const char *src, size_t src_len, |
| 2927 | Error &error) { |
| 2928 | if (m_stdio_communication.IsConnected()) { |
| 2929 | ConnectionStatus status; |
| 2930 | m_stdio_communication.Write(src, src_len, status, NULL); |
| 2931 | } else if (m_stdin_forward) { |
| 2932 | m_gdb_comm.SendStdinNotification(src, src_len); |
| 2933 | } |
| 2934 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2937 | Error ProcessGDBRemote::EnableBreakpointSite(BreakpointSite *bp_site) { |
| 2938 | Error error; |
| 2939 | assert(bp_site != NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2940 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2941 | // Get logging info |
| 2942 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
| 2943 | user_id_t site_id = bp_site->GetID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2945 | // Get the breakpoint address |
| 2946 | const addr_t addr = bp_site->GetLoadAddress(); |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2947 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2948 | // Log that a breakpoint was requested |
| 2949 | if (log) |
| 2950 | log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 |
| 2951 | ") address = 0x%" PRIx64, |
| 2952 | site_id, (uint64_t)addr); |
| 2953 | |
| 2954 | // Breakpoint already exists and is enabled |
| 2955 | if (bp_site->IsEnabled()) { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2956 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2957 | log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 |
| 2958 | ") address = 0x%" PRIx64 " -- SUCCESS (already enabled)", |
| 2959 | site_id, (uint64_t)addr); |
| 2960 | return error; |
| 2961 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2962 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2963 | // Get the software breakpoint trap opcode size |
| 2964 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2965 | |
| 2966 | // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this |
| 2967 | // breakpoint type |
| 2968 | // is supported by the remote stub. These are set to true by default, and |
| 2969 | // later set to false |
| 2970 | // only after we receive an unimplemented response when sending a breakpoint |
| 2971 | // packet. This means |
| 2972 | // initially that unless we were specifically instructed to use a hardware |
| 2973 | // breakpoint, LLDB will |
| 2974 | // attempt to set a software breakpoint. HardwareRequired() also queries a |
| 2975 | // boolean variable which |
| 2976 | // indicates if the user specifically asked for hardware breakpoints. If true |
| 2977 | // then we will |
| 2978 | // skip over software breakpoints. |
| 2979 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && |
| 2980 | (!bp_site->HardwareRequired())) { |
| 2981 | // Try to send off a software breakpoint packet ($Z0) |
| 2982 | uint8_t error_no = m_gdb_comm.SendGDBStoppointTypePacket( |
| 2983 | eBreakpointSoftware, true, addr, bp_op_size); |
| 2984 | if (error_no == 0) { |
| 2985 | // The breakpoint was placed successfully |
| 2986 | bp_site->SetEnabled(true); |
| 2987 | bp_site->SetType(BreakpointSite::eExternal); |
| 2988 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2989 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2990 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2991 | // SendGDBStoppointTypePacket() will return an error if it was unable to set |
| 2992 | // this |
| 2993 | // breakpoint. We need to differentiate between a error specific to placing |
| 2994 | // this breakpoint |
| 2995 | // or if we have learned that this breakpoint type is unsupported. To do |
| 2996 | // this, we |
| 2997 | // must test the support boolean for this breakpoint type to see if it now |
| 2998 | // indicates that |
| 2999 | // this breakpoint type is unsupported. If they are still supported then we |
| 3000 | // should return |
| 3001 | // with the error code. If they are now unsupported, then we would like to |
| 3002 | // fall through |
| 3003 | // and try another form of breakpoint. |
| 3004 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) { |
| 3005 | if (error_no != UINT8_MAX) |
| 3006 | error.SetErrorStringWithFormat( |
| 3007 | "error: %d sending the breakpoint request", errno); |
| 3008 | else |
| 3009 | error.SetErrorString("error sending the breakpoint request"); |
| 3010 | return error; |
| 3011 | } |
| 3012 | |
| 3013 | // We reach here when software breakpoints have been found to be |
| 3014 | // unsupported. For future |
| 3015 | // calls to set a breakpoint, we will not attempt to set a breakpoint with a |
| 3016 | // type that is |
| 3017 | // known not to be supported. |
| 3018 | if (log) |
| 3019 | log->Printf("Software breakpoints are unsupported"); |
| 3020 | |
| 3021 | // So we will fall through and try a hardware breakpoint |
| 3022 | } |
| 3023 | |
| 3024 | // The process of setting a hardware breakpoint is much the same as above. We |
| 3025 | // check the |
| 3026 | // supported boolean for this breakpoint type, and if it is thought to be |
| 3027 | // supported then we |
| 3028 | // will try to set this breakpoint with a hardware breakpoint. |
| 3029 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) { |
| 3030 | // Try to send off a hardware breakpoint packet ($Z1) |
| 3031 | uint8_t error_no = m_gdb_comm.SendGDBStoppointTypePacket( |
| 3032 | eBreakpointHardware, true, addr, bp_op_size); |
| 3033 | if (error_no == 0) { |
| 3034 | // The breakpoint was placed successfully |
| 3035 | bp_site->SetEnabled(true); |
| 3036 | bp_site->SetType(BreakpointSite::eHardware); |
| 3037 | return error; |
| 3038 | } |
| 3039 | |
| 3040 | // Check if the error was something other then an unsupported breakpoint |
| 3041 | // type |
| 3042 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) { |
| 3043 | // Unable to set this hardware breakpoint |
| 3044 | if (error_no != UINT8_MAX) |
| 3045 | error.SetErrorStringWithFormat( |
| 3046 | "error: %d sending the hardware breakpoint request " |
| 3047 | "(hardware breakpoint resources might be exhausted or unavailable)", |
| 3048 | error_no); |
| 3049 | else |
| 3050 | error.SetErrorString("error sending the hardware breakpoint request " |
| 3051 | "(hardware breakpoint resources " |
| 3052 | "might be exhausted or unavailable)"); |
| 3053 | return error; |
| 3054 | } |
| 3055 | |
| 3056 | // We will reach here when the stub gives an unsupported response to a |
| 3057 | // hardware breakpoint |
| 3058 | if (log) |
| 3059 | log->Printf("Hardware breakpoints are unsupported"); |
| 3060 | |
| 3061 | // Finally we will falling through to a #trap style breakpoint |
| 3062 | } |
| 3063 | |
| 3064 | // Don't fall through when hardware breakpoints were specifically requested |
| 3065 | if (bp_site->HardwareRequired()) { |
| 3066 | error.SetErrorString("hardware breakpoints are not supported"); |
| 3067 | return error; |
| 3068 | } |
| 3069 | |
| 3070 | // As a last resort we want to place a manual breakpoint. An instruction |
| 3071 | // is placed into the process memory using memory write packets. |
| 3072 | return EnableSoftwareBreakpoint(bp_site); |
| 3073 | } |
| 3074 | |
| 3075 | Error ProcessGDBRemote::DisableBreakpointSite(BreakpointSite *bp_site) { |
| 3076 | Error error; |
| 3077 | assert(bp_site != NULL); |
| 3078 | addr_t addr = bp_site->GetLoadAddress(); |
| 3079 | user_id_t site_id = bp_site->GetID(); |
| 3080 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
| 3081 | if (log) |
| 3082 | log->Printf("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 |
| 3083 | ") addr = 0x%8.8" PRIx64, |
| 3084 | site_id, (uint64_t)addr); |
| 3085 | |
| 3086 | if (bp_site->IsEnabled()) { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3087 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 3088 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3089 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 3090 | switch (bp_type) { |
| 3091 | case BreakpointSite::eSoftware: |
| 3092 | error = DisableSoftwareBreakpoint(bp_site); |
| 3093 | break; |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3094 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3095 | case BreakpointSite::eHardware: |
| 3096 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, |
| 3097 | addr, bp_op_size)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3098 | error.SetErrorToGenericError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3099 | break; |
| 3100 | |
| 3101 | case BreakpointSite::eExternal: { |
| 3102 | GDBStoppointType stoppoint_type; |
| 3103 | if (bp_site->IsHardware()) |
| 3104 | stoppoint_type = eBreakpointHardware; |
| 3105 | else |
| 3106 | stoppoint_type = eBreakpointSoftware; |
| 3107 | |
| 3108 | if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, |
| 3109 | bp_op_size)) |
| 3110 | error.SetErrorToGenericError(); |
| 3111 | } break; |
| 3112 | } |
| 3113 | if (error.Success()) |
| 3114 | bp_site->SetEnabled(false); |
| 3115 | } else { |
| 3116 | if (log) |
| 3117 | log->Printf("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 |
| 3118 | ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", |
| 3119 | site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3120 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | if (error.Success()) |
| 3124 | error.SetErrorToGenericError(); |
| 3125 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3128 | // Pre-requisite: wp != NULL. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3129 | static GDBStoppointType GetGDBStoppointType(Watchpoint *wp) { |
| 3130 | assert(wp); |
| 3131 | bool watch_read = wp->WatchpointRead(); |
| 3132 | bool watch_write = wp->WatchpointWrite(); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3134 | // watch_read and watch_write cannot both be false. |
| 3135 | assert(watch_read || watch_write); |
| 3136 | if (watch_read && watch_write) |
| 3137 | return eWatchpointReadWrite; |
| 3138 | else if (watch_read) |
| 3139 | return eWatchpointRead; |
| 3140 | else // Must be watch_write, then. |
| 3141 | return eWatchpointWrite; |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3144 | Error ProcessGDBRemote::EnableWatchpoint(Watchpoint *wp, bool notify) { |
| 3145 | Error error; |
| 3146 | if (wp) { |
| 3147 | user_id_t watchID = wp->GetID(); |
| 3148 | addr_t addr = wp->GetLoadAddress(); |
| 3149 | Log *log( |
| 3150 | ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3151 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3152 | log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", |
| 3153 | watchID); |
| 3154 | if (wp->IsEnabled()) { |
| 3155 | if (log) |
| 3156 | log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 |
| 3157 | ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", |
| 3158 | watchID, (uint64_t)addr); |
| 3159 | return error; |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 3160 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3161 | |
| 3162 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 3163 | // Pass down an appropriate z/Z packet... |
| 3164 | if (m_gdb_comm.SupportsGDBStoppointPacket(type)) { |
| 3165 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, |
| 3166 | wp->GetByteSize()) == 0) { |
| 3167 | wp->SetEnabled(true, notify); |
| 3168 | return error; |
| 3169 | } else |
| 3170 | error.SetErrorString("sending gdb watchpoint packet failed"); |
| 3171 | } else |
| 3172 | error.SetErrorString("watchpoints not supported"); |
| 3173 | } else { |
| 3174 | error.SetErrorString("Watchpoint argument was NULL."); |
| 3175 | } |
| 3176 | if (error.Success()) |
| 3177 | error.SetErrorToGenericError(); |
| 3178 | return error; |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 3179 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3180 | |
| 3181 | Error ProcessGDBRemote::DisableWatchpoint(Watchpoint *wp, bool notify) { |
| 3182 | Error error; |
| 3183 | if (wp) { |
| 3184 | user_id_t watchID = wp->GetID(); |
| 3185 | |
| 3186 | Log *log( |
| 3187 | ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
| 3188 | |
| 3189 | addr_t addr = wp->GetLoadAddress(); |
| 3190 | |
| 3191 | if (log) |
| 3192 | log->Printf("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 |
| 3193 | ") addr = 0x%8.8" PRIx64, |
| 3194 | watchID, (uint64_t)addr); |
| 3195 | |
| 3196 | if (!wp->IsEnabled()) { |
| 3197 | if (log) |
| 3198 | log->Printf("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 |
| 3199 | ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", |
| 3200 | watchID, (uint64_t)addr); |
| 3201 | // See also 'class WatchpointSentry' within StopInfo.cpp. |
| 3202 | // This disabling attempt might come from the user-supplied actions, we'll |
| 3203 | // route it in order for |
| 3204 | // the watchpoint object to intelligently process this action. |
| 3205 | wp->SetEnabled(false, notify); |
| 3206 | return error; |
| 3207 | } |
| 3208 | |
| 3209 | if (wp->IsHardware()) { |
| 3210 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 3211 | // Pass down an appropriate z/Z packet... |
| 3212 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, |
| 3213 | wp->GetByteSize()) == 0) { |
| 3214 | wp->SetEnabled(false, notify); |
| 3215 | return error; |
| 3216 | } else |
| 3217 | error.SetErrorString("sending gdb watchpoint packet failed"); |
| 3218 | } |
| 3219 | // TODO: clear software watchpoints if we implement them |
| 3220 | } else { |
| 3221 | error.SetErrorString("Watchpoint argument was NULL."); |
| 3222 | } |
| 3223 | if (error.Success()) |
| 3224 | error.SetErrorToGenericError(); |
| 3225 | return error; |
| 3226 | } |
| 3227 | |
| 3228 | void ProcessGDBRemote::Clear() { |
| 3229 | m_flags = 0; |
| 3230 | m_thread_list_real.Clear(); |
| 3231 | m_thread_list.Clear(); |
| 3232 | } |
| 3233 | |
| 3234 | Error ProcessGDBRemote::DoSignal(int signo) { |
| 3235 | Error error; |
| 3236 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3237 | if (log) |
| 3238 | log->Printf("ProcessGDBRemote::DoSignal (signal = %d)", signo); |
| 3239 | |
| 3240 | if (!m_gdb_comm.SendAsyncSignal(signo)) |
| 3241 | error.SetErrorStringWithFormat("failed to send signal %i", signo); |
| 3242 | return error; |
| 3243 | } |
| 3244 | |
| 3245 | Error ProcessGDBRemote::EstablishConnectionIfNeeded( |
| 3246 | const ProcessInfo &process_info) { |
| 3247 | // Make sure we aren't already connected? |
| 3248 | if (m_gdb_comm.IsConnected()) |
| 3249 | return Error(); |
| 3250 | |
| 3251 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 3252 | if (platform_sp && !platform_sp->IsHost()) |
| 3253 | return Error("Lost debug server connection"); |
| 3254 | |
| 3255 | auto error = LaunchAndConnectToDebugserver(process_info); |
| 3256 | if (error.Fail()) { |
| 3257 | const char *error_string = error.AsCString(); |
| 3258 | if (error_string == nullptr) |
| 3259 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
| 3260 | } |
| 3261 | return error; |
| 3262 | } |
| 3263 | #if defined(__APPLE__) |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3264 | #define USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 1 |
| 3265 | #endif |
| 3266 | |
| 3267 | #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3268 | static bool SetCloexecFlag(int fd) { |
| 3269 | #if defined(FD_CLOEXEC) |
| 3270 | int flags = ::fcntl(fd, F_GETFD); |
| 3271 | if (flags == -1) |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3272 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3273 | return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0); |
| 3274 | #else |
| 3275 | return false; |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3276 | #endif |
| 3277 | } |
| 3278 | #endif |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 3279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3280 | Error ProcessGDBRemote::LaunchAndConnectToDebugserver( |
| 3281 | const ProcessInfo &process_info) { |
| 3282 | using namespace std::placeholders; // For _1, _2, etc. |
Pavel Labath | 998bdc5 | 2016-05-11 16:59:04 +0000 | [diff] [blame] | 3283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3284 | Error error; |
| 3285 | if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) { |
| 3286 | // If we locate debugserver, keep that located version around |
| 3287 | static FileSpec g_debugserver_file_spec; |
| 3288 | |
| 3289 | ProcessLaunchInfo debugserver_launch_info; |
| 3290 | // Make debugserver run in its own session so signals generated by |
| 3291 | // special terminal key sequences (^C) don't affect debugserver. |
| 3292 | debugserver_launch_info.SetLaunchInSeparateProcessGroup(true); |
| 3293 | |
| 3294 | const std::weak_ptr<ProcessGDBRemote> this_wp = |
| 3295 | std::static_pointer_cast<ProcessGDBRemote>(shared_from_this()); |
| 3296 | debugserver_launch_info.SetMonitorProcessCallback( |
| 3297 | std::bind(MonitorDebugserverProcess, this_wp, _1, _2, _3, _4), false); |
| 3298 | debugserver_launch_info.SetUserID(process_info.GetUserID()); |
| 3299 | |
| 3300 | int communication_fd = -1; |
| 3301 | #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION |
| 3302 | // Auto close the sockets we might open up unless everything goes OK. This |
| 3303 | // helps us not leak file descriptors when things go wrong. |
| 3304 | lldb_utility::CleanUp<int, int> our_socket(-1, -1, close); |
| 3305 | lldb_utility::CleanUp<int, int> gdb_socket(-1, -1, close); |
| 3306 | |
| 3307 | // Use a socketpair on Apple for now until other platforms can verify it |
| 3308 | // works and is fast enough |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3309 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3310 | int sockets[2]; /* the pair of socket descriptors */ |
| 3311 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) { |
| 3312 | error.SetErrorToErrno(); |
| 3313 | return error; |
| 3314 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3315 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3316 | our_socket.set(sockets[0]); |
| 3317 | gdb_socket.set(sockets[1]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3318 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3319 | |
| 3320 | // Don't let any child processes inherit our communication socket |
| 3321 | SetCloexecFlag(our_socket.get()); |
| 3322 | communication_fd = gdb_socket.get(); |
| 3323 | #endif |
| 3324 | |
| 3325 | error = m_gdb_comm.StartDebugserverProcess( |
| 3326 | nullptr, GetTarget().GetPlatform().get(), debugserver_launch_info, |
| 3327 | nullptr, nullptr, communication_fd); |
| 3328 | |
| 3329 | if (error.Success()) |
| 3330 | m_debugserver_pid = debugserver_launch_info.GetProcessID(); |
| 3331 | else |
| 3332 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 3333 | |
| 3334 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) { |
| 3335 | #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION |
| 3336 | // Our process spawned correctly, we can now set our connection to use our |
| 3337 | // end of the socket pair |
| 3338 | m_gdb_comm.SetConnection( |
| 3339 | new ConnectionFileDescriptor(our_socket.release(), true)); |
| 3340 | #endif |
| 3341 | StartAsyncThread(); |
| 3342 | } |
| 3343 | |
| 3344 | if (error.Fail()) { |
| 3345 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3346 | |
| 3347 | if (log) |
| 3348 | log->Printf("failed to start debugserver process: %s", |
| 3349 | error.AsCString()); |
| 3350 | return error; |
| 3351 | } |
| 3352 | |
| 3353 | if (m_gdb_comm.IsConnected()) { |
| 3354 | // Finish the connection process by doing the handshake without connecting |
| 3355 | // (send NULL URL) |
| 3356 | ConnectToDebugserver(NULL); |
| 3357 | } else { |
| 3358 | error.SetErrorString("connection failed"); |
| 3359 | } |
| 3360 | } |
| 3361 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3364 | bool ProcessGDBRemote::MonitorDebugserverProcess( |
| 3365 | std::weak_ptr<ProcessGDBRemote> process_wp, lldb::pid_t debugserver_pid, |
| 3366 | bool exited, // True if the process did exit |
| 3367 | int signo, // Zero for no signal |
| 3368 | int exit_status // Exit value of process if signal is zero |
| 3369 | ) { |
| 3370 | // "debugserver_pid" argument passed in is the process ID for |
| 3371 | // debugserver that we are tracking... |
| 3372 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3373 | const bool handled = true; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3374 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3375 | if (log) |
| 3376 | log->Printf("ProcessGDBRemote::%s(process_wp, pid=%" PRIu64 |
| 3377 | ", signo=%i (0x%x), exit_status=%i)", |
| 3378 | __FUNCTION__, debugserver_pid, signo, signo, exit_status); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3379 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3380 | std::shared_ptr<ProcessGDBRemote> process_sp = process_wp.lock(); |
| 3381 | if (log) |
| 3382 | log->Printf("ProcessGDBRemote::%s(process = %p)", __FUNCTION__, |
| 3383 | static_cast<void *>(process_sp.get())); |
| 3384 | if (!process_sp || process_sp->m_debugserver_pid != debugserver_pid) |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3385 | return handled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3386 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3387 | // Sleep for a half a second to make sure our inferior process has |
| 3388 | // time to set its exit status before we set it incorrectly when |
| 3389 | // both the debugserver and the inferior process shut down. |
| 3390 | usleep(500000); |
| 3391 | // If our process hasn't yet exited, debugserver might have died. |
| 3392 | // If the process did exit, then we are reaping it. |
| 3393 | const StateType state = process_sp->GetState(); |
| 3394 | |
| 3395 | if (state != eStateInvalid && state != eStateUnloaded && |
| 3396 | state != eStateExited && state != eStateDetached) { |
| 3397 | char error_str[1024]; |
| 3398 | if (signo) { |
| 3399 | const char *signal_cstr = |
| 3400 | process_sp->GetUnixSignals()->GetSignalAsCString(signo); |
| 3401 | if (signal_cstr) |
| 3402 | ::snprintf(error_str, sizeof(error_str), |
| 3403 | DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); |
| 3404 | else |
| 3405 | ::snprintf(error_str, sizeof(error_str), |
| 3406 | DEBUGSERVER_BASENAME " died with signal %i", signo); |
| 3407 | } else { |
| 3408 | ::snprintf(error_str, sizeof(error_str), |
| 3409 | DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", |
| 3410 | exit_status); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3411 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3412 | |
| 3413 | process_sp->SetExitStatus(-1, error_str); |
| 3414 | } |
| 3415 | // Debugserver has exited we need to let our ProcessGDBRemote |
| 3416 | // know that it no longer has a debugserver instance |
| 3417 | process_sp->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 3418 | return handled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3421 | void ProcessGDBRemote::KillDebugserverProcess() { |
| 3422 | m_gdb_comm.Disconnect(); |
| 3423 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) { |
| 3424 | Host::Kill(m_debugserver_pid, SIGINT); |
| 3425 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 3426 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3429 | void ProcessGDBRemote::Initialize() { |
| 3430 | static std::once_flag g_once_flag; |
| 3431 | |
| 3432 | std::call_once(g_once_flag, []() { |
| 3433 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 3434 | GetPluginDescriptionStatic(), CreateInstance, |
| 3435 | DebuggerInitialize); |
| 3436 | }); |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3439 | void ProcessGDBRemote::DebuggerInitialize(Debugger &debugger) { |
| 3440 | if (!PluginManager::GetSettingForProcessPlugin( |
| 3441 | debugger, PluginProperties::GetSettingName())) { |
| 3442 | const bool is_global_setting = true; |
| 3443 | PluginManager::CreateSettingForProcessPlugin( |
| 3444 | debugger, GetGlobalPluginProperties()->GetValueProperties(), |
| 3445 | ConstString("Properties for the gdb-remote process plug-in."), |
| 3446 | is_global_setting); |
| 3447 | } |
| 3448 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3449 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3450 | bool ProcessGDBRemote::StartAsyncThread() { |
| 3451 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3452 | |
| 3453 | if (log) |
| 3454 | log->Printf("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 3455 | |
| 3456 | std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex); |
| 3457 | if (!m_async_thread.IsJoinable()) { |
| 3458 | // Create a thread that watches our internal state and controls which |
| 3459 | // events make it to clients (into the DCProcess event queue). |
| 3460 | |
| 3461 | m_async_thread = |
| 3462 | ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", |
| 3463 | ProcessGDBRemote::AsyncThread, this, NULL); |
| 3464 | } else if (log) |
| 3465 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was " |
| 3466 | "already running.", |
| 3467 | __FUNCTION__); |
| 3468 | |
| 3469 | return m_async_thread.IsJoinable(); |
| 3470 | } |
| 3471 | |
| 3472 | void ProcessGDBRemote::StopAsyncThread() { |
| 3473 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3474 | |
| 3475 | if (log) |
| 3476 | log->Printf("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 3477 | |
| 3478 | std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex); |
| 3479 | if (m_async_thread.IsJoinable()) { |
| 3480 | m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncThreadShouldExit); |
| 3481 | |
| 3482 | // This will shut down the async thread. |
| 3483 | m_gdb_comm.Disconnect(); // Disconnect from the debug server. |
| 3484 | |
| 3485 | // Stop the stdio thread |
| 3486 | m_async_thread.Join(nullptr); |
| 3487 | m_async_thread.Reset(); |
| 3488 | } else if (log) |
| 3489 | log->Printf( |
| 3490 | "ProcessGDBRemote::%s () - Called when Async thread was not running.", |
| 3491 | __FUNCTION__); |
| 3492 | } |
| 3493 | |
| 3494 | bool ProcessGDBRemote::HandleNotifyPacket(StringExtractorGDBRemote &packet) { |
| 3495 | // get the packet at a string |
| 3496 | const std::string &pkt = packet.GetStringRef(); |
| 3497 | // skip %stop: |
| 3498 | StringExtractorGDBRemote stop_info(pkt.c_str() + 5); |
| 3499 | |
| 3500 | // pass as a thread stop info packet |
| 3501 | SetLastStopPacket(stop_info); |
| 3502 | |
| 3503 | // check for more stop reasons |
| 3504 | HandleStopReplySequence(); |
| 3505 | |
| 3506 | // if the process is stopped then we need to fake a resume |
| 3507 | // so that we can stop properly with the new break. This |
| 3508 | // is possible due to SetPrivateState() broadcasting the |
| 3509 | // state change as a side effect. |
| 3510 | if (GetPrivateState() == lldb::StateType::eStateStopped) { |
| 3511 | SetPrivateState(lldb::StateType::eStateRunning); |
| 3512 | } |
| 3513 | |
| 3514 | // since we have some stopped packets we can halt the process |
| 3515 | SetPrivateState(lldb::StateType::eStateStopped); |
| 3516 | |
| 3517 | return true; |
| 3518 | } |
| 3519 | |
| 3520 | thread_result_t ProcessGDBRemote::AsyncThread(void *arg) { |
| 3521 | ProcessGDBRemote *process = (ProcessGDBRemote *)arg; |
| 3522 | |
| 3523 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3524 | if (log) |
| 3525 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3526 | ") thread starting...", |
| 3527 | __FUNCTION__, arg, process->GetID()); |
| 3528 | |
| 3529 | EventSP event_sp; |
| 3530 | bool done = false; |
| 3531 | while (!done) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3532 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3533 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3534 | ") listener.WaitForEvent (NULL, event_sp)...", |
| 3535 | __FUNCTION__, arg, process->GetID()); |
| 3536 | if (process->m_async_listener_sp->WaitForEvent(std::chrono::microseconds(0), |
| 3537 | event_sp)) { |
| 3538 | const uint32_t event_type = event_sp->GetType(); |
| 3539 | if (event_sp->BroadcasterIs(&process->m_async_broadcaster)) { |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3540 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3541 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3542 | ") Got an event of type: %d...", |
| 3543 | __FUNCTION__, arg, process->GetID(), event_type); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3545 | switch (event_type) { |
| 3546 | case eBroadcastBitAsyncContinue: { |
| 3547 | const EventDataBytes *continue_packet = |
| 3548 | EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3549 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3550 | if (continue_packet) { |
| 3551 | const char *continue_cstr = |
| 3552 | (const char *)continue_packet->GetBytes(); |
| 3553 | const size_t continue_cstr_len = continue_packet->GetByteSize(); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3554 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3555 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3556 | ") got eBroadcastBitAsyncContinue: %s", |
| 3557 | __FUNCTION__, arg, process->GetID(), continue_cstr); |
| 3558 | |
| 3559 | if (::strstr(continue_cstr, "vAttach") == NULL) |
| 3560 | process->SetPrivateState(eStateRunning); |
| 3561 | StringExtractorGDBRemote response; |
| 3562 | |
| 3563 | // If in Non-Stop-Mode |
| 3564 | if (process->GetTarget().GetNonStopModeEnabled()) { |
| 3565 | // send the vCont packet |
| 3566 | if (!process->GetGDBRemote().SendvContPacket( |
| 3567 | llvm::StringRef(continue_cstr, continue_cstr_len), |
| 3568 | response)) { |
| 3569 | // Something went wrong |
| 3570 | done = true; |
| 3571 | break; |
| 3572 | } |
| 3573 | } |
| 3574 | // If in All-Stop-Mode |
| 3575 | else { |
| 3576 | StateType stop_state = |
| 3577 | process->GetGDBRemote().SendContinuePacketAndWaitForResponse( |
| 3578 | *process, *process->GetUnixSignals(), |
| 3579 | llvm::StringRef(continue_cstr, continue_cstr_len), |
| 3580 | response); |
| 3581 | |
| 3582 | // We need to immediately clear the thread ID list so we are sure |
| 3583 | // to get a valid list of threads. |
| 3584 | // The thread ID list might be contained within the "response", or |
| 3585 | // the stop reply packet that |
| 3586 | // caused the stop. So clear it now before we give the stop reply |
| 3587 | // packet to the process |
| 3588 | // using the process->SetLastStopPacket()... |
| 3589 | process->ClearThreadIDList(); |
| 3590 | |
| 3591 | switch (stop_state) { |
| 3592 | case eStateStopped: |
| 3593 | case eStateCrashed: |
| 3594 | case eStateSuspended: |
| 3595 | process->SetLastStopPacket(response); |
| 3596 | process->SetPrivateState(stop_state); |
| 3597 | break; |
| 3598 | |
| 3599 | case eStateExited: { |
| 3600 | process->SetLastStopPacket(response); |
| 3601 | process->ClearThreadIDList(); |
| 3602 | response.SetFilePos(1); |
| 3603 | |
| 3604 | int exit_status = response.GetHexU8(); |
| 3605 | std::string desc_string; |
| 3606 | if (response.GetBytesLeft() > 0 && |
| 3607 | response.GetChar('-') == ';') { |
| 3608 | llvm::StringRef desc_str; |
| 3609 | llvm::StringRef desc_token; |
| 3610 | while (response.GetNameColonValue(desc_token, desc_str)) { |
| 3611 | if (desc_token != "description") |
| 3612 | continue; |
| 3613 | StringExtractor extractor(desc_str); |
| 3614 | extractor.GetHexByteString(desc_string); |
| 3615 | } |
| 3616 | } |
| 3617 | process->SetExitStatus(exit_status, desc_string.c_str()); |
| 3618 | done = true; |
| 3619 | break; |
| 3620 | } |
| 3621 | case eStateInvalid: { |
| 3622 | // Check to see if we were trying to attach and if we got back |
| 3623 | // the "E87" error code from debugserver -- this indicates that |
| 3624 | // the process is not debuggable. Return a slightly more |
| 3625 | // helpful |
| 3626 | // error message about why the attach failed. |
| 3627 | if (::strstr(continue_cstr, "vAttach") != NULL && |
| 3628 | response.GetError() == 0x87) { |
| 3629 | process->SetExitStatus(-1, "cannot attach to process due to " |
| 3630 | "System Integrity Protection"); |
| 3631 | } |
| 3632 | // E01 code from vAttach means that the attach failed |
| 3633 | if (::strstr(continue_cstr, "vAttach") != NULL && |
| 3634 | response.GetError() == 0x1) { |
| 3635 | process->SetExitStatus(-1, "unable to attach"); |
| 3636 | } else { |
| 3637 | process->SetExitStatus(-1, "lost connection"); |
| 3638 | } |
| 3639 | break; |
| 3640 | } |
| 3641 | |
| 3642 | default: |
| 3643 | process->SetPrivateState(stop_state); |
| 3644 | break; |
| 3645 | } // switch(stop_state) |
| 3646 | } // else // if in All-stop-mode |
| 3647 | } // if (continue_packet) |
| 3648 | } // case eBroadcastBitAysncContinue |
| 3649 | break; |
| 3650 | |
| 3651 | case eBroadcastBitAsyncThreadShouldExit: |
| 3652 | if (log) |
| 3653 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3654 | ") got eBroadcastBitAsyncThreadShouldExit...", |
| 3655 | __FUNCTION__, arg, process->GetID()); |
| 3656 | done = true; |
| 3657 | break; |
| 3658 | |
| 3659 | default: |
| 3660 | if (log) |
| 3661 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3662 | ") got unknown event 0x%8.8x", |
| 3663 | __FUNCTION__, arg, process->GetID(), event_type); |
| 3664 | done = true; |
| 3665 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3666 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3667 | } else if (event_sp->BroadcasterIs(&process->m_gdb_comm)) { |
| 3668 | switch (event_type) { |
| 3669 | case Communication::eBroadcastBitReadThreadDidExit: |
| 3670 | process->SetExitStatus(-1, "lost connection"); |
| 3671 | done = true; |
| 3672 | break; |
| 3673 | |
| 3674 | case GDBRemoteCommunication::eBroadcastBitGdbReadThreadGotNotify: { |
| 3675 | lldb_private::Event *event = event_sp.get(); |
| 3676 | const EventDataBytes *continue_packet = |
| 3677 | EventDataBytes::GetEventDataFromEvent(event); |
| 3678 | StringExtractorGDBRemote notify( |
| 3679 | (const char *)continue_packet->GetBytes()); |
| 3680 | // Hand this over to the process to handle |
| 3681 | process->HandleNotifyPacket(notify); |
| 3682 | break; |
| 3683 | } |
| 3684 | |
| 3685 | default: |
| 3686 | if (log) |
| 3687 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3688 | ") got unknown event 0x%8.8x", |
| 3689 | __FUNCTION__, arg, process->GetID(), event_type); |
| 3690 | done = true; |
| 3691 | break; |
| 3692 | } |
| 3693 | } |
| 3694 | } else { |
| 3695 | if (log) |
| 3696 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3697 | ") listener.WaitForEvent (NULL, event_sp) => false", |
| 3698 | __FUNCTION__, arg, process->GetID()); |
| 3699 | done = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3700 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3701 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3702 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3703 | if (log) |
| 3704 | log->Printf("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 |
| 3705 | ") thread exiting...", |
| 3706 | __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3707 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3708 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3711 | // uint32_t |
| 3712 | // ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList |
| 3713 | // &matches, std::vector<lldb::pid_t> &pids) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3714 | //{ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3715 | // // If we are planning to launch the debugserver remotely, then we need to |
| 3716 | // fire up a debugserver |
| 3717 | // // process and ask it for the list of processes. But if we are local, we |
| 3718 | // can let the Host do it. |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3719 | // if (m_local_debugserver) |
| 3720 | // { |
| 3721 | // return Host::ListProcessesMatchingName (name, matches, pids); |
| 3722 | // } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3723 | // else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3724 | // { |
| 3725 | // // FIXME: Implement talking to the remote debugserver. |
| 3726 | // return 0; |
| 3727 | // } |
| 3728 | // |
| 3729 | //} |
| 3730 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3731 | bool ProcessGDBRemote::NewThreadNotifyBreakpointHit( |
| 3732 | void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, |
| 3733 | lldb::user_id_t break_loc_id) { |
| 3734 | // I don't think I have to do anything here, just make sure I notice the new |
| 3735 | // thread when it starts to |
| 3736 | // run so I can stop it if that's what I want to do. |
| 3737 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 3738 | if (log) |
| 3739 | log->Printf("Hit New Thread Notification breakpoint."); |
| 3740 | return false; |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3743 | bool ProcessGDBRemote::StartNoticingNewThreads() { |
| 3744 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 3745 | if (m_thread_create_bp_sp) { |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 3746 | if (log && log->GetVerbose()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3747 | log->Printf("Enabled noticing new thread breakpoint."); |
| 3748 | m_thread_create_bp_sp->SetEnabled(true); |
| 3749 | } else { |
| 3750 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 3751 | if (platform_sp) { |
| 3752 | m_thread_create_bp_sp = |
| 3753 | platform_sp->SetThreadCreationBreakpoint(GetTarget()); |
| 3754 | if (m_thread_create_bp_sp) { |
| 3755 | if (log && log->GetVerbose()) |
| 3756 | log->Printf( |
| 3757 | "Successfully created new thread notification breakpoint %i", |
| 3758 | m_thread_create_bp_sp->GetID()); |
| 3759 | m_thread_create_bp_sp->SetCallback( |
| 3760 | ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); |
| 3761 | } else { |
| 3762 | if (log) |
| 3763 | log->Printf("Failed to create new thread notification breakpoint."); |
| 3764 | } |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3765 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3766 | } |
| 3767 | return m_thread_create_bp_sp.get() != NULL; |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3770 | bool ProcessGDBRemote::StopNoticingNewThreads() { |
| 3771 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 3772 | if (log && log->GetVerbose()) |
| 3773 | log->Printf("Disabling new thread notification breakpoint."); |
| 3774 | |
| 3775 | if (m_thread_create_bp_sp) |
| 3776 | m_thread_create_bp_sp->SetEnabled(false); |
| 3777 | |
| 3778 | return true; |
| 3779 | } |
| 3780 | |
| 3781 | DynamicLoader *ProcessGDBRemote::GetDynamicLoader() { |
| 3782 | if (m_dyld_ap.get() == NULL) |
| 3783 | m_dyld_ap.reset(DynamicLoader::FindPlugin(this, NULL)); |
| 3784 | return m_dyld_ap.get(); |
| 3785 | } |
| 3786 | |
| 3787 | Error ProcessGDBRemote::SendEventData(const char *data) { |
| 3788 | int return_value; |
| 3789 | bool was_supported; |
| 3790 | |
| 3791 | Error error; |
| 3792 | |
| 3793 | return_value = m_gdb_comm.SendLaunchEventDataPacket(data, &was_supported); |
| 3794 | if (return_value != 0) { |
| 3795 | if (!was_supported) |
| 3796 | error.SetErrorString("Sending events is not supported for this process."); |
| 3797 | else |
| 3798 | error.SetErrorStringWithFormat("Error sending event data: %d.", |
| 3799 | return_value); |
| 3800 | } |
| 3801 | return error; |
| 3802 | } |
| 3803 | |
| 3804 | const DataBufferSP ProcessGDBRemote::GetAuxvData() { |
| 3805 | DataBufferSP buf; |
| 3806 | if (m_gdb_comm.GetQXferAuxvReadSupported()) { |
| 3807 | std::string response_string; |
| 3808 | if (m_gdb_comm.SendPacketsAndConcatenateResponses("qXfer:auxv:read::", |
| 3809 | response_string) == |
| 3810 | GDBRemoteCommunication::PacketResult::Success) |
| 3811 | buf.reset(new DataBufferHeap(response_string.c_str(), |
| 3812 | response_string.length())); |
| 3813 | } |
| 3814 | return buf; |
Steve Pucci | 03904ac | 2014-03-04 23:18:46 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3817 | StructuredData::ObjectSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3818 | ProcessGDBRemote::GetExtendedInfoForThread(lldb::tid_t tid) { |
| 3819 | StructuredData::ObjectSP object_sp; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3820 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3821 | if (m_gdb_comm.GetThreadExtendedInfoSupported()) { |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3822 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3823 | SystemRuntime *runtime = GetSystemRuntime(); |
| 3824 | if (runtime) { |
| 3825 | runtime->AddThreadExtendedInfoPacketHints(args_dict); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3826 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3827 | args_dict->GetAsDictionary()->AddIntegerItem("thread", tid); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3828 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3829 | StreamString packet; |
| 3830 | packet << "jThreadExtendedInfo:"; |
| 3831 | args_dict->Dump(packet, false); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3833 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 3834 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 3835 | // these characters in its packet output -- so we add the quoted version |
| 3836 | // of the } character here manually in case we talk to a debugserver which |
| 3837 | // un-escapes the characters at packet read time. |
| 3838 | packet << (char)(0x7d ^ 0x20); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3839 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3840 | StringExtractorGDBRemote response; |
| 3841 | response.SetResponseValidatorToJSON(); |
| 3842 | if (m_gdb_comm.SendPacketAndWaitForResponse( |
| 3843 | packet.GetData(), packet.GetSize(), response, false) == |
| 3844 | GDBRemoteCommunication::PacketResult::Success) { |
| 3845 | StringExtractorGDBRemote::ResponseType response_type = |
| 3846 | response.GetResponseType(); |
| 3847 | if (response_type == StringExtractorGDBRemote::eResponse) { |
| 3848 | if (!response.Empty()) { |
| 3849 | object_sp = StructuredData::ParseJSON(response.GetStringRef()); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3850 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3851 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3852 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3853 | } |
| 3854 | return object_sp; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3857 | StructuredData::ObjectSP ProcessGDBRemote::GetLoadedDynamicLibrariesInfos( |
| 3858 | lldb::addr_t image_list_address, lldb::addr_t image_count) { |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3859 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3860 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 3861 | args_dict->GetAsDictionary()->AddIntegerItem("image_list_address", |
| 3862 | image_list_address); |
| 3863 | args_dict->GetAsDictionary()->AddIntegerItem("image_count", image_count); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3864 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3865 | return GetLoadedDynamicLibrariesInfos_sender(args_dict); |
| 3866 | } |
| 3867 | |
| 3868 | StructuredData::ObjectSP ProcessGDBRemote::GetLoadedDynamicLibrariesInfos() { |
| 3869 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 3870 | |
| 3871 | args_dict->GetAsDictionary()->AddBooleanItem("fetch_all_solibs", true); |
| 3872 | |
| 3873 | return GetLoadedDynamicLibrariesInfos_sender(args_dict); |
| 3874 | } |
| 3875 | |
| 3876 | StructuredData::ObjectSP ProcessGDBRemote::GetLoadedDynamicLibrariesInfos( |
| 3877 | const std::vector<lldb::addr_t> &load_addresses) { |
| 3878 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 3879 | StructuredData::ArraySP addresses(new StructuredData::Array); |
| 3880 | |
| 3881 | for (auto addr : load_addresses) { |
| 3882 | StructuredData::ObjectSP addr_sp(new StructuredData::Integer(addr)); |
| 3883 | addresses->AddItem(addr_sp); |
| 3884 | } |
| 3885 | |
| 3886 | args_dict->GetAsDictionary()->AddItem("solib_addresses", addresses); |
| 3887 | |
| 3888 | return GetLoadedDynamicLibrariesInfos_sender(args_dict); |
| 3889 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 3890 | |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3891 | StructuredData::ObjectSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3892 | ProcessGDBRemote::GetLoadedDynamicLibrariesInfos_sender( |
| 3893 | StructuredData::ObjectSP args_dict) { |
| 3894 | StructuredData::ObjectSP object_sp; |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3895 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3896 | if (m_gdb_comm.GetLoadedDynamicLibrariesInfosSupported()) { |
| 3897 | // Scope for the scoped timeout object |
| 3898 | GDBRemoteCommunication::ScopedTimeout timeout(m_gdb_comm, 10); |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3899 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3900 | StreamString packet; |
| 3901 | packet << "jGetLoadedDynamicLibrariesInfos:"; |
| 3902 | args_dict->Dump(packet, false); |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3903 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3904 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 3905 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 3906 | // these characters in its packet output -- so we add the quoted version |
| 3907 | // of the } character here manually in case we talk to a debugserver which |
| 3908 | // un-escapes the characters at packet read time. |
| 3909 | packet << (char)(0x7d ^ 0x20); |
| 3910 | |
| 3911 | StringExtractorGDBRemote response; |
| 3912 | response.SetResponseValidatorToJSON(); |
| 3913 | if (m_gdb_comm.SendPacketAndWaitForResponse( |
| 3914 | packet.GetData(), packet.GetSize(), response, false) == |
| 3915 | GDBRemoteCommunication::PacketResult::Success) { |
| 3916 | StringExtractorGDBRemote::ResponseType response_type = |
| 3917 | response.GetResponseType(); |
| 3918 | if (response_type == StringExtractorGDBRemote::eResponse) { |
| 3919 | if (!response.Empty()) { |
| 3920 | object_sp = StructuredData::ParseJSON(response.GetStringRef()); |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3921 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3922 | } |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3923 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3924 | } |
| 3925 | return object_sp; |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3928 | StructuredData::ObjectSP ProcessGDBRemote::GetSharedCacheInfo() { |
| 3929 | StructuredData::ObjectSP object_sp; |
| 3930 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 3931 | |
| 3932 | if (m_gdb_comm.GetSharedCacheInfoSupported()) { |
| 3933 | StreamString packet; |
| 3934 | packet << "jGetSharedCacheInfo:"; |
| 3935 | args_dict->Dump(packet, false); |
| 3936 | |
| 3937 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 3938 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 3939 | // these characters in its packet output -- so we add the quoted version |
| 3940 | // of the } character here manually in case we talk to a debugserver which |
| 3941 | // un-escapes the characters at packet read time. |
| 3942 | packet << (char)(0x7d ^ 0x20); |
| 3943 | |
| 3944 | StringExtractorGDBRemote response; |
| 3945 | response.SetResponseValidatorToJSON(); |
| 3946 | if (m_gdb_comm.SendPacketAndWaitForResponse( |
| 3947 | packet.GetData(), packet.GetSize(), response, false) == |
| 3948 | GDBRemoteCommunication::PacketResult::Success) { |
| 3949 | StringExtractorGDBRemote::ResponseType response_type = |
| 3950 | response.GetResponseType(); |
| 3951 | if (response_type == StringExtractorGDBRemote::eResponse) { |
| 3952 | if (!response.Empty()) { |
| 3953 | object_sp = StructuredData::ParseJSON(response.GetStringRef()); |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | return object_sp; |
| 3959 | } |
| 3960 | |
| 3961 | Error ProcessGDBRemote::ConfigureStructuredData( |
| 3962 | const ConstString &type_name, const StructuredData::ObjectSP &config_sp) { |
| 3963 | return m_gdb_comm.ConfigureRemoteStructuredData(type_name, config_sp); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3964 | } |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 3965 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3966 | // Establish the largest memory read/write payloads we should use. |
| 3967 | // If the remote stub has a max packet size, stay under that size. |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3968 | // |
| 3969 | // If the remote stub's max packet size is crazy large, use a |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3970 | // reasonable largeish default. |
| 3971 | // |
| 3972 | // If the remote stub doesn't advertise a max packet size, use a |
| 3973 | // conservative default. |
| 3974 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3975 | void ProcessGDBRemote::GetMaxMemorySize() { |
| 3976 | const uint64_t reasonable_largeish_default = 128 * 1024; |
| 3977 | const uint64_t conservative_default = 512; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3978 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3979 | if (m_max_memory_size == 0) { |
| 3980 | uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize(); |
| 3981 | if (stub_max_size != UINT64_MAX && stub_max_size != 0) { |
| 3982 | // Save the stub's claimed maximum packet size |
| 3983 | m_remote_stub_max_memory_size = stub_max_size; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3984 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3985 | // Even if the stub says it can support ginormous packets, |
| 3986 | // don't exceed our reasonable largeish default packet size. |
| 3987 | if (stub_max_size > reasonable_largeish_default) { |
| 3988 | stub_max_size = reasonable_largeish_default; |
| 3989 | } |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3990 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3991 | m_max_memory_size = stub_max_size; |
| 3992 | } else { |
| 3993 | m_max_memory_size = conservative_default; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3994 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3995 | } |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3998 | void ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize( |
| 3999 | uint64_t user_specified_max) { |
| 4000 | if (user_specified_max != 0) { |
| 4001 | GetMaxMemorySize(); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4002 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4003 | if (m_remote_stub_max_memory_size != 0) { |
| 4004 | if (m_remote_stub_max_memory_size < user_specified_max) { |
| 4005 | m_max_memory_size = m_remote_stub_max_memory_size; // user specified a |
| 4006 | // packet size too |
| 4007 | // big, go as big |
| 4008 | // as the remote stub says we can go. |
| 4009 | } else { |
| 4010 | m_max_memory_size = user_specified_max; // user's packet size is good |
| 4011 | } |
| 4012 | } else { |
| 4013 | m_max_memory_size = |
| 4014 | user_specified_max; // user's packet size is probably fine |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4015 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4016 | } |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4019 | bool ProcessGDBRemote::GetModuleSpec(const FileSpec &module_file_spec, |
| 4020 | const ArchSpec &arch, |
| 4021 | ModuleSpec &module_spec) { |
| 4022 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4023 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 4024 | const ModuleCacheKey key(module_file_spec.GetPath(), |
| 4025 | arch.GetTriple().getTriple()); |
| 4026 | auto cached = m_cached_module_specs.find(key); |
| 4027 | if (cached != m_cached_module_specs.end()) { |
| 4028 | module_spec = cached->second; |
| 4029 | return bool(module_spec); |
| 4030 | } |
| 4031 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4032 | if (!m_gdb_comm.GetModuleInfo(module_file_spec, arch, module_spec)) { |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4033 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4034 | log->Printf("ProcessGDBRemote::%s - failed to get module info for %s:%s", |
| 4035 | __FUNCTION__, module_file_spec.GetPath().c_str(), |
| 4036 | arch.GetTriple().getTriple().c_str()); |
| 4037 | return false; |
| 4038 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4039 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4040 | if (log) { |
| 4041 | StreamString stream; |
| 4042 | module_spec.Dump(stream); |
| 4043 | log->Printf("ProcessGDBRemote::%s - got module info for (%s:%s) : %s", |
| 4044 | __FUNCTION__, module_file_spec.GetPath().c_str(), |
| 4045 | arch.GetTriple().getTriple().c_str(), |
| 4046 | stream.GetString().c_str()); |
| 4047 | } |
| 4048 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 4049 | m_cached_module_specs[key] = module_spec; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4050 | return true; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 4053 | void ProcessGDBRemote::PrefetchModuleSpecs( |
| 4054 | llvm::ArrayRef<FileSpec> module_file_specs, const llvm::Triple &triple) { |
| 4055 | auto module_specs = m_gdb_comm.GetModulesInfo(module_file_specs, triple); |
| 4056 | if (module_specs) { |
| 4057 | for (const FileSpec &spec : module_file_specs) |
Pavel Labath | cfc7ae6 | 2016-09-08 16:58:30 +0000 | [diff] [blame] | 4058 | m_cached_module_specs[ModuleCacheKey(spec.GetPath(), |
| 4059 | triple.getTriple())] = ModuleSpec(); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 4060 | for (const ModuleSpec &spec : *module_specs) |
Pavel Labath | cfc7ae6 | 2016-09-08 16:58:30 +0000 | [diff] [blame] | 4061 | m_cached_module_specs[ModuleCacheKey(spec.GetFileSpec().GetPath(), |
| 4062 | triple.getTriple())] = spec; |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4066 | bool ProcessGDBRemote::GetHostOSVersion(uint32_t &major, uint32_t &minor, |
| 4067 | uint32_t &update) { |
| 4068 | if (m_gdb_comm.GetOSVersion(major, minor, update)) |
| 4069 | return true; |
| 4070 | // We failed to get the host OS version, defer to the base |
| 4071 | // implementation to correctly invalidate the arguments. |
| 4072 | return Process::GetHostOSVersion(major, minor, update); |
Jim Ingham | 13c30d2 | 2015-11-05 22:33:17 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4075 | namespace { |
| 4076 | |
| 4077 | typedef std::vector<std::string> stringVec; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4078 | |
| 4079 | typedef std::vector<struct GdbServerRegisterInfo> GDBServerRegisterVec; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4080 | struct RegisterSetInfo { |
| 4081 | ConstString name; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4082 | }; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4083 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4084 | typedef std::map<uint32_t, RegisterSetInfo> RegisterSetMap; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4085 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4086 | struct GdbServerTargetInfo { |
| 4087 | std::string arch; |
| 4088 | std::string osabi; |
| 4089 | stringVec includes; |
| 4090 | RegisterSetMap reg_set_map; |
| 4091 | XMLNode feature_node; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4092 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4093 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4094 | bool ParseRegisters(XMLNode feature_node, GdbServerTargetInfo &target_info, |
| 4095 | GDBRemoteDynamicRegisterInfo &dyn_reg_info, ABISP abi_sp, |
| 4096 | uint32_t &cur_reg_num, uint32_t ®_offset) { |
| 4097 | if (!feature_node) |
| 4098 | return false; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4099 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4100 | feature_node.ForEachChildElementWithName( |
| 4101 | "reg", [&target_info, &dyn_reg_info, &cur_reg_num, ®_offset, |
| 4102 | &abi_sp](const XMLNode ®_node) -> bool { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4103 | std::string gdb_group; |
| 4104 | std::string gdb_type; |
| 4105 | ConstString reg_name; |
| 4106 | ConstString alt_name; |
| 4107 | ConstString set_name; |
| 4108 | std::vector<uint32_t> value_regs; |
| 4109 | std::vector<uint32_t> invalidate_regs; |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4110 | std::vector<uint8_t> dwarf_opcode_bytes; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4111 | bool encoding_set = false; |
| 4112 | bool format_set = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4113 | RegisterInfo reg_info = { |
| 4114 | NULL, // Name |
| 4115 | NULL, // Alt name |
| 4116 | 0, // byte size |
| 4117 | reg_offset, // offset |
| 4118 | eEncodingUint, // encoding |
| 4119 | eFormatHex, // format |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4120 | { |
Jason Molenda | bf67a30 | 2015-09-01 05:17:01 +0000 | [diff] [blame] | 4121 | LLDB_INVALID_REGNUM, // eh_frame reg num |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4122 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 4123 | LLDB_INVALID_REGNUM, // generic reg num |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4124 | cur_reg_num, // process plugin reg num |
| 4125 | cur_reg_num // native register number |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4126 | }, |
| 4127 | NULL, |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4128 | NULL, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4129 | NULL, // Dwarf Expression opcode bytes pointer |
| 4130 | 0 // Dwarf Expression opcode bytes length |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4131 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4133 | reg_node.ForEachAttribute([&target_info, &gdb_group, &gdb_type, |
| 4134 | ®_name, &alt_name, &set_name, &value_regs, |
| 4135 | &invalidate_regs, &encoding_set, &format_set, |
| 4136 | ®_info, &cur_reg_num, ®_offset, |
| 4137 | &dwarf_opcode_bytes]( |
| 4138 | const llvm::StringRef &name, |
| 4139 | const llvm::StringRef &value) -> bool { |
| 4140 | if (name == "name") { |
| 4141 | reg_name.SetString(value); |
| 4142 | } else if (name == "bitsize") { |
| 4143 | reg_info.byte_size = |
| 4144 | StringConvert::ToUInt32(value.data(), 0, 0) / CHAR_BIT; |
| 4145 | } else if (name == "type") { |
| 4146 | gdb_type = value.str(); |
| 4147 | } else if (name == "group") { |
| 4148 | gdb_group = value.str(); |
| 4149 | } else if (name == "regnum") { |
| 4150 | const uint32_t regnum = |
| 4151 | StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 4152 | if (regnum != LLDB_INVALID_REGNUM) { |
| 4153 | reg_info.kinds[eRegisterKindProcessPlugin] = regnum; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4154 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4155 | } else if (name == "offset") { |
| 4156 | reg_offset = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 4157 | } else if (name == "altname") { |
| 4158 | alt_name.SetString(value); |
| 4159 | } else if (name == "encoding") { |
| 4160 | encoding_set = true; |
| 4161 | reg_info.encoding = |
| 4162 | Args::StringToEncoding(value.data(), eEncodingUint); |
| 4163 | } else if (name == "format") { |
| 4164 | format_set = true; |
| 4165 | Format format = eFormatInvalid; |
| 4166 | if (Args::StringToFormat(value.data(), format, NULL).Success()) |
| 4167 | reg_info.format = format; |
| 4168 | else if (value == "vector-sint8") |
| 4169 | reg_info.format = eFormatVectorOfSInt8; |
| 4170 | else if (value == "vector-uint8") |
| 4171 | reg_info.format = eFormatVectorOfUInt8; |
| 4172 | else if (value == "vector-sint16") |
| 4173 | reg_info.format = eFormatVectorOfSInt16; |
| 4174 | else if (value == "vector-uint16") |
| 4175 | reg_info.format = eFormatVectorOfUInt16; |
| 4176 | else if (value == "vector-sint32") |
| 4177 | reg_info.format = eFormatVectorOfSInt32; |
| 4178 | else if (value == "vector-uint32") |
| 4179 | reg_info.format = eFormatVectorOfUInt32; |
| 4180 | else if (value == "vector-float32") |
| 4181 | reg_info.format = eFormatVectorOfFloat32; |
Valentina Giusti | cda0ae4 | 2016-09-08 14:16:45 +0000 | [diff] [blame] | 4182 | else if (value == "vector-uint64") |
| 4183 | reg_info.format = eFormatVectorOfUInt64; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4184 | else if (value == "vector-uint128") |
| 4185 | reg_info.format = eFormatVectorOfUInt128; |
| 4186 | } else if (name == "group_id") { |
| 4187 | const uint32_t set_id = |
| 4188 | StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 4189 | RegisterSetMap::const_iterator pos = |
| 4190 | target_info.reg_set_map.find(set_id); |
| 4191 | if (pos != target_info.reg_set_map.end()) |
| 4192 | set_name = pos->second.name; |
| 4193 | } else if (name == "gcc_regnum" || name == "ehframe_regnum") { |
| 4194 | reg_info.kinds[eRegisterKindEHFrame] = |
| 4195 | StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 4196 | } else if (name == "dwarf_regnum") { |
| 4197 | reg_info.kinds[eRegisterKindDWARF] = |
| 4198 | StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 4199 | } else if (name == "generic") { |
| 4200 | reg_info.kinds[eRegisterKindGeneric] = |
| 4201 | Args::StringToGenericRegister(value.data()); |
| 4202 | } else if (name == "value_regnums") { |
| 4203 | SplitCommaSeparatedRegisterNumberString(value, value_regs, 0); |
| 4204 | } else if (name == "invalidate_regnums") { |
| 4205 | SplitCommaSeparatedRegisterNumberString(value, invalidate_regs, 0); |
| 4206 | } else if (name == "dynamic_size_dwarf_expr_bytes") { |
| 4207 | StringExtractor opcode_extractor; |
| 4208 | std::string opcode_string = value.str(); |
| 4209 | size_t dwarf_opcode_len = opcode_string.length() / 2; |
| 4210 | assert(dwarf_opcode_len > 0); |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4212 | dwarf_opcode_bytes.resize(dwarf_opcode_len); |
| 4213 | reg_info.dynamic_size_dwarf_len = dwarf_opcode_len; |
| 4214 | opcode_extractor.GetStringRef().swap(opcode_string); |
| 4215 | uint32_t ret_val = |
| 4216 | opcode_extractor.GetHexBytesAvail(dwarf_opcode_bytes); |
| 4217 | assert(dwarf_opcode_len == ret_val); |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4219 | reg_info.dynamic_size_dwarf_expr_bytes = dwarf_opcode_bytes.data(); |
| 4220 | } else { |
| 4221 | printf("unhandled attribute %s = %s\n", name.data(), value.data()); |
| 4222 | } |
| 4223 | return true; // Keep iterating through all attributes |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4224 | }); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4226 | if (!gdb_type.empty() && !(encoding_set || format_set)) { |
| 4227 | if (gdb_type.find("int") == 0) { |
| 4228 | reg_info.format = eFormatHex; |
| 4229 | reg_info.encoding = eEncodingUint; |
| 4230 | } else if (gdb_type == "data_ptr" || gdb_type == "code_ptr") { |
| 4231 | reg_info.format = eFormatAddressInfo; |
| 4232 | reg_info.encoding = eEncodingUint; |
| 4233 | } else if (gdb_type == "i387_ext" || gdb_type == "float") { |
| 4234 | reg_info.format = eFormatFloat; |
| 4235 | reg_info.encoding = eEncodingIEEE754; |
| 4236 | } |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4237 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4238 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4239 | // Only update the register set name if we didn't get a "reg_set" |
| 4240 | // attribute. |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4241 | // "set_name" will be empty if we didn't have a "reg_set" attribute. |
| 4242 | if (!set_name && !gdb_group.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4243 | set_name.SetCString(gdb_group.c_str()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4244 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4245 | reg_info.byte_offset = reg_offset; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4246 | assert(reg_info.byte_size != 0); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4247 | reg_offset += reg_info.byte_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4248 | if (!value_regs.empty()) { |
| 4249 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 4250 | reg_info.value_regs = value_regs.data(); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4251 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4252 | if (!invalidate_regs.empty()) { |
| 4253 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 4254 | reg_info.invalidate_regs = invalidate_regs.data(); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4255 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4256 | |
Jason Molenda | 63bd0db | 2015-09-15 23:20:34 +0000 | [diff] [blame] | 4257 | ++cur_reg_num; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4258 | AugmentRegisterInfoViaABI(reg_info, reg_name, abi_sp); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4259 | dyn_reg_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4260 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4261 | return true; // Keep iterating through all "reg" elements |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4262 | }); |
| 4263 | return true; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4264 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4265 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 4266 | } // namespace {} |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4267 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4268 | // query the target of gdb-remote for extended target information |
| 4269 | // return: 'true' on success |
| 4270 | // 'false' on failure |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4271 | bool ProcessGDBRemote::GetGDBServerRegisterInfo(ArchSpec &arch_to_use) { |
| 4272 | // Make sure LLDB has an XML parser it can use first |
| 4273 | if (!XMLDocument::XMLEnabled()) |
| 4274 | return false; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4276 | // redirect libxml2's error handler since the default prints to stdout |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4278 | GDBRemoteCommunicationClient &comm = m_gdb_comm; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4280 | // check that we have extended feature read support |
| 4281 | if (!comm.GetQXferFeaturesReadSupported()) |
| 4282 | return false; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4284 | // request the target xml file |
| 4285 | std::string raw; |
| 4286 | lldb_private::Error lldberr; |
| 4287 | if (!comm.ReadExtFeature(ConstString("features"), ConstString("target.xml"), |
| 4288 | raw, lldberr)) { |
| 4289 | return false; |
| 4290 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4292 | XMLDocument xml_document; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4293 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4294 | if (xml_document.ParseMemory(raw.c_str(), raw.size(), "target.xml")) { |
| 4295 | GdbServerTargetInfo target_info; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4297 | XMLNode target_node = xml_document.GetRootElement("target"); |
| 4298 | if (target_node) { |
| 4299 | XMLNode feature_node; |
| 4300 | target_node.ForEachChildElement([&target_info, this, &feature_node]( |
| 4301 | const XMLNode &node) -> bool { |
| 4302 | llvm::StringRef name = node.GetName(); |
| 4303 | if (name == "architecture") { |
| 4304 | node.GetElementText(target_info.arch); |
| 4305 | } else if (name == "osabi") { |
| 4306 | node.GetElementText(target_info.osabi); |
| 4307 | } else if (name == "xi:include" || name == "include") { |
| 4308 | llvm::StringRef href = node.GetAttributeValue("href"); |
| 4309 | if (!href.empty()) |
| 4310 | target_info.includes.push_back(href.str()); |
| 4311 | } else if (name == "feature") { |
| 4312 | feature_node = node; |
| 4313 | } else if (name == "groups") { |
| 4314 | node.ForEachChildElementWithName( |
| 4315 | "group", [&target_info](const XMLNode &node) -> bool { |
| 4316 | uint32_t set_id = UINT32_MAX; |
| 4317 | RegisterSetInfo set_info; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4318 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4319 | node.ForEachAttribute( |
| 4320 | [&set_id, &set_info](const llvm::StringRef &name, |
| 4321 | const llvm::StringRef &value) -> bool { |
| 4322 | if (name == "id") |
| 4323 | set_id = StringConvert::ToUInt32(value.data(), |
| 4324 | UINT32_MAX, 0); |
| 4325 | if (name == "name") |
| 4326 | set_info.name = ConstString(value); |
| 4327 | return true; // Keep iterating through all attributes |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4328 | }); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4329 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4330 | if (set_id != UINT32_MAX) |
| 4331 | target_info.reg_set_map[set_id] = set_info; |
| 4332 | return true; // Keep iterating through all "group" elements |
| 4333 | }); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4334 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4335 | return true; // Keep iterating through all children of the target_node |
| 4336 | }); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4337 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4338 | // Initialize these outside of ParseRegisters, since they should not be |
| 4339 | // reset inside each include feature |
| 4340 | uint32_t cur_reg_num = 0; |
| 4341 | uint32_t reg_offset = 0; |
| 4342 | |
| 4343 | // Don't use Process::GetABI, this code gets called from DidAttach, and in |
| 4344 | // that context we haven't |
| 4345 | // set the Target's architecture yet, so the ABI is also potentially |
| 4346 | // incorrect. |
| 4347 | ABISP abi_to_use_sp = ABI::FindPlugin(arch_to_use); |
| 4348 | if (feature_node) { |
| 4349 | ParseRegisters(feature_node, target_info, this->m_register_info, |
| 4350 | abi_to_use_sp, cur_reg_num, reg_offset); |
| 4351 | } |
| 4352 | |
| 4353 | for (const auto &include : target_info.includes) { |
| 4354 | // request register file |
| 4355 | std::string xml_data; |
| 4356 | if (!comm.ReadExtFeature(ConstString("features"), ConstString(include), |
| 4357 | xml_data, lldberr)) |
| 4358 | continue; |
| 4359 | |
| 4360 | XMLDocument include_xml_document; |
| 4361 | include_xml_document.ParseMemory(xml_data.data(), xml_data.size(), |
| 4362 | include.c_str()); |
| 4363 | XMLNode include_feature_node = |
| 4364 | include_xml_document.GetRootElement("feature"); |
| 4365 | if (include_feature_node) { |
| 4366 | ParseRegisters(include_feature_node, target_info, |
| 4367 | this->m_register_info, abi_to_use_sp, cur_reg_num, |
| 4368 | reg_offset); |
| 4369 | } |
| 4370 | } |
| 4371 | this->m_register_info.Finalize(arch_to_use); |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | return m_register_info.GetNumRegisters() > 0; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4378 | Error ProcessGDBRemote::GetLoadedModuleList(LoadedModuleInfoList &list) { |
| 4379 | // Make sure LLDB has an XML parser it can use first |
| 4380 | if (!XMLDocument::XMLEnabled()) |
| 4381 | return Error(0, ErrorType::eErrorTypeGeneric); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4383 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS); |
| 4384 | if (log) |
| 4385 | log->Printf("ProcessGDBRemote::%s", __FUNCTION__); |
| 4386 | |
| 4387 | GDBRemoteCommunicationClient &comm = m_gdb_comm; |
| 4388 | |
| 4389 | // check that we have extended feature read support |
| 4390 | if (comm.GetQXferLibrariesSVR4ReadSupported()) { |
| 4391 | list.clear(); |
| 4392 | |
| 4393 | // request the loaded library list |
| 4394 | std::string raw; |
| 4395 | lldb_private::Error lldberr; |
| 4396 | |
| 4397 | if (!comm.ReadExtFeature(ConstString("libraries-svr4"), ConstString(""), |
| 4398 | raw, lldberr)) |
| 4399 | return Error(0, ErrorType::eErrorTypeGeneric); |
| 4400 | |
| 4401 | // parse the xml file in memory |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4402 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4403 | log->Printf("parsing: %s", raw.c_str()); |
| 4404 | XMLDocument doc; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4405 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4406 | if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) |
| 4407 | return Error(0, ErrorType::eErrorTypeGeneric); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4408 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4409 | XMLNode root_element = doc.GetRootElement("library-list-svr4"); |
| 4410 | if (!root_element) |
| 4411 | return Error(); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4413 | // main link map structure |
| 4414 | llvm::StringRef main_lm = root_element.GetAttributeValue("main-lm"); |
| 4415 | if (!main_lm.empty()) { |
| 4416 | list.m_link_map = |
| 4417 | StringConvert::ToUInt64(main_lm.data(), LLDB_INVALID_ADDRESS, 0); |
| 4418 | } |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4420 | root_element.ForEachChildElementWithName( |
| 4421 | "library", [log, &list](const XMLNode &library) -> bool { |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4423 | LoadedModuleInfoList::LoadedModuleInfo module; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4425 | library.ForEachAttribute( |
| 4426 | [log, &module](const llvm::StringRef &name, |
| 4427 | const llvm::StringRef &value) -> bool { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4428 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4429 | if (name == "name") |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4430 | module.set_name(value.str()); |
| 4431 | else if (name == "lm") { |
| 4432 | // the address of the link_map struct. |
| 4433 | module.set_link_map(StringConvert::ToUInt64( |
| 4434 | value.data(), LLDB_INVALID_ADDRESS, 0)); |
| 4435 | } else if (name == "l_addr") { |
| 4436 | // the displacement as read from the field 'l_addr' of the |
| 4437 | // link_map struct. |
| 4438 | module.set_base(StringConvert::ToUInt64( |
| 4439 | value.data(), LLDB_INVALID_ADDRESS, 0)); |
| 4440 | // base address is always a displacement, not an absolute |
| 4441 | // value. |
| 4442 | module.set_base_is_offset(true); |
| 4443 | } else if (name == "l_ld") { |
| 4444 | // the memory address of the libraries PT_DYAMIC section. |
| 4445 | module.set_dynamic(StringConvert::ToUInt64( |
| 4446 | value.data(), LLDB_INVALID_ADDRESS, 0)); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4447 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4448 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4449 | return true; // Keep iterating over all properties of "library" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4450 | }); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4451 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4452 | if (log) { |
| 4453 | std::string name; |
| 4454 | lldb::addr_t lm = 0, base = 0, ld = 0; |
| 4455 | bool base_is_offset; |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4456 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4457 | module.get_name(name); |
| 4458 | module.get_link_map(lm); |
| 4459 | module.get_base(base); |
| 4460 | module.get_base_is_offset(base_is_offset); |
| 4461 | module.get_dynamic(ld); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4462 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4463 | log->Printf("found (link_map:0x%08" PRIx64 ", base:0x%08" PRIx64 |
| 4464 | "[%s], ld:0x%08" PRIx64 ", name:'%s')", |
| 4465 | lm, base, (base_is_offset ? "offset" : "absolute"), ld, |
| 4466 | name.c_str()); |
| 4467 | } |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4468 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4469 | list.add(module); |
| 4470 | return true; // Keep iterating over all "library" elements in the root |
| 4471 | // node |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4472 | }); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4474 | if (log) |
| 4475 | log->Printf("found %" PRId32 " modules in total", |
| 4476 | (int)list.m_list.size()); |
| 4477 | } else if (comm.GetQXferLibrariesReadSupported()) { |
| 4478 | list.clear(); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4479 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4480 | // request the loaded library list |
| 4481 | std::string raw; |
| 4482 | lldb_private::Error lldberr; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4483 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4484 | if (!comm.ReadExtFeature(ConstString("libraries"), ConstString(""), raw, |
| 4485 | lldberr)) |
| 4486 | return Error(0, ErrorType::eErrorTypeGeneric); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4487 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4488 | if (log) |
| 4489 | log->Printf("parsing: %s", raw.c_str()); |
| 4490 | XMLDocument doc; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4491 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4492 | if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) |
| 4493 | return Error(0, ErrorType::eErrorTypeGeneric); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4495 | XMLNode root_element = doc.GetRootElement("library-list"); |
| 4496 | if (!root_element) |
| 4497 | return Error(); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4499 | root_element.ForEachChildElementWithName( |
| 4500 | "library", [log, &list](const XMLNode &library) -> bool { |
| 4501 | LoadedModuleInfoList::LoadedModuleInfo module; |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4503 | llvm::StringRef name = library.GetAttributeValue("name"); |
| 4504 | module.set_name(name.str()); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4505 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4506 | // The base address of a given library will be the address of its |
| 4507 | // first section. Most remotes send only one section for Windows |
| 4508 | // targets for example. |
| 4509 | const XMLNode §ion = |
| 4510 | library.FindFirstChildElementWithName("section"); |
| 4511 | llvm::StringRef address = section.GetAttributeValue("address"); |
| 4512 | module.set_base( |
| 4513 | StringConvert::ToUInt64(address.data(), LLDB_INVALID_ADDRESS, 0)); |
| 4514 | // These addresses are absolute values. |
| 4515 | module.set_base_is_offset(false); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4516 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4517 | if (log) { |
| 4518 | std::string name; |
| 4519 | lldb::addr_t base = 0; |
| 4520 | bool base_is_offset; |
| 4521 | module.get_name(name); |
| 4522 | module.get_base(base); |
| 4523 | module.get_base_is_offset(base_is_offset); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4524 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4525 | log->Printf("found (base:0x%08" PRIx64 "[%s], name:'%s')", base, |
| 4526 | (base_is_offset ? "offset" : "absolute"), name.c_str()); |
| 4527 | } |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4529 | list.add(module); |
| 4530 | return true; // Keep iterating over all "library" elements in the root |
| 4531 | // node |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4532 | }); |
| 4533 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4534 | if (log) |
| 4535 | log->Printf("found %" PRId32 " modules in total", |
| 4536 | (int)list.m_list.size()); |
| 4537 | } else { |
| 4538 | return Error(0, ErrorType::eErrorTypeGeneric); |
| 4539 | } |
| 4540 | |
| 4541 | return Error(); |
| 4542 | } |
| 4543 | |
| 4544 | lldb::ModuleSP ProcessGDBRemote::LoadModuleAtAddress(const FileSpec &file, |
| 4545 | lldb::addr_t link_map, |
| 4546 | lldb::addr_t base_addr, |
| 4547 | bool value_is_offset) { |
| 4548 | DynamicLoader *loader = GetDynamicLoader(); |
| 4549 | if (!loader) |
| 4550 | return nullptr; |
| 4551 | |
| 4552 | return loader->LoadModuleAtAddress(file, link_map, base_addr, |
| 4553 | value_is_offset); |
| 4554 | } |
| 4555 | |
| 4556 | size_t ProcessGDBRemote::LoadModules(LoadedModuleInfoList &module_list) { |
| 4557 | using lldb_private::process_gdb_remote::ProcessGDBRemote; |
| 4558 | |
| 4559 | // request a list of loaded libraries from GDBServer |
| 4560 | if (GetLoadedModuleList(module_list).Fail()) |
| 4561 | return 0; |
| 4562 | |
| 4563 | // get a list of all the modules |
| 4564 | ModuleList new_modules; |
| 4565 | |
| 4566 | for (LoadedModuleInfoList::LoadedModuleInfo &modInfo : module_list.m_list) { |
| 4567 | std::string mod_name; |
| 4568 | lldb::addr_t mod_base; |
| 4569 | lldb::addr_t link_map; |
| 4570 | bool mod_base_is_offset; |
| 4571 | |
| 4572 | bool valid = true; |
| 4573 | valid &= modInfo.get_name(mod_name); |
| 4574 | valid &= modInfo.get_base(mod_base); |
| 4575 | valid &= modInfo.get_base_is_offset(mod_base_is_offset); |
| 4576 | if (!valid) |
| 4577 | continue; |
| 4578 | |
| 4579 | if (!modInfo.get_link_map(link_map)) |
| 4580 | link_map = LLDB_INVALID_ADDRESS; |
| 4581 | |
| 4582 | FileSpec file(mod_name.c_str(), true); |
| 4583 | lldb::ModuleSP module_sp = |
| 4584 | LoadModuleAtAddress(file, link_map, mod_base, mod_base_is_offset); |
| 4585 | |
| 4586 | if (module_sp.get()) |
| 4587 | new_modules.Append(module_sp); |
| 4588 | } |
| 4589 | |
| 4590 | if (new_modules.GetSize() > 0) { |
| 4591 | ModuleList removed_modules; |
| 4592 | Target &target = GetTarget(); |
| 4593 | ModuleList &loaded_modules = m_process->GetTarget().GetImages(); |
| 4594 | |
| 4595 | for (size_t i = 0; i < loaded_modules.GetSize(); ++i) { |
| 4596 | const lldb::ModuleSP loaded_module = loaded_modules.GetModuleAtIndex(i); |
| 4597 | |
| 4598 | bool found = false; |
| 4599 | for (size_t j = 0; j < new_modules.GetSize(); ++j) { |
| 4600 | if (new_modules.GetModuleAtIndex(j).get() == loaded_module.get()) |
| 4601 | found = true; |
| 4602 | } |
| 4603 | |
| 4604 | // The main executable will never be included in libraries-svr4, don't |
| 4605 | // remove it |
| 4606 | if (!found && |
| 4607 | loaded_module.get() != target.GetExecutableModulePointer()) { |
| 4608 | removed_modules.Append(loaded_module); |
| 4609 | } |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4610 | } |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4611 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4612 | loaded_modules.Remove(removed_modules); |
| 4613 | m_process->GetTarget().ModulesDidUnload(removed_modules, false); |
| 4614 | |
| 4615 | new_modules.ForEach([&target](const lldb::ModuleSP module_sp) -> bool { |
| 4616 | lldb_private::ObjectFile *obj = module_sp->GetObjectFile(); |
| 4617 | if (!obj) |
| 4618 | return true; |
| 4619 | |
| 4620 | if (obj->GetType() != ObjectFile::Type::eTypeExecutable) |
| 4621 | return true; |
| 4622 | |
| 4623 | lldb::ModuleSP module_copy_sp = module_sp; |
| 4624 | target.SetExecutableModule(module_copy_sp, false); |
| 4625 | return false; |
| 4626 | }); |
| 4627 | |
| 4628 | loaded_modules.AppendIfNeeded(new_modules); |
| 4629 | m_process->GetTarget().ModulesDidLoad(new_modules); |
| 4630 | } |
| 4631 | |
| 4632 | return new_modules.GetSize(); |
| 4633 | } |
| 4634 | |
| 4635 | size_t ProcessGDBRemote::LoadModules() { |
| 4636 | LoadedModuleInfoList module_list; |
| 4637 | return LoadModules(module_list); |
| 4638 | } |
| 4639 | |
| 4640 | Error ProcessGDBRemote::GetFileLoadAddress(const FileSpec &file, |
| 4641 | bool &is_loaded, |
| 4642 | lldb::addr_t &load_addr) { |
| 4643 | is_loaded = false; |
| 4644 | load_addr = LLDB_INVALID_ADDRESS; |
| 4645 | |
| 4646 | std::string file_path = file.GetPath(false); |
| 4647 | if (file_path.empty()) |
| 4648 | return Error("Empty file name specified"); |
| 4649 | |
| 4650 | StreamString packet; |
| 4651 | packet.PutCString("qFileLoadAddress:"); |
| 4652 | packet.PutCStringAsRawHex8(file_path.c_str()); |
| 4653 | |
| 4654 | StringExtractorGDBRemote response; |
| 4655 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), |
| 4656 | response, false) != |
| 4657 | GDBRemoteCommunication::PacketResult::Success) |
| 4658 | return Error("Sending qFileLoadAddress packet failed"); |
| 4659 | |
| 4660 | if (response.IsErrorResponse()) { |
| 4661 | if (response.GetError() == 1) { |
| 4662 | // The file is not loaded into the inferior |
| 4663 | is_loaded = false; |
| 4664 | load_addr = LLDB_INVALID_ADDRESS; |
| 4665 | return Error(); |
| 4666 | } |
| 4667 | |
| 4668 | return Error( |
| 4669 | "Fetching file load address from remote server returned an error"); |
| 4670 | } |
| 4671 | |
| 4672 | if (response.IsNormalResponse()) { |
| 4673 | is_loaded = true; |
| 4674 | load_addr = response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4675 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | return Error("Unknown error happened during sending the load address packet"); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4681 | void ProcessGDBRemote::ModulesDidLoad(ModuleList &module_list) { |
| 4682 | // We must call the lldb_private::Process::ModulesDidLoad () first before we |
| 4683 | // do anything |
| 4684 | Process::ModulesDidLoad(module_list); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4685 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4686 | // After loading shared libraries, we can ask our remote GDB server if |
| 4687 | // it needs any symbols. |
| 4688 | m_gdb_comm.ServeSymbolLookups(this); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4691 | void ProcessGDBRemote::HandleAsyncStdout(llvm::StringRef out) { |
| 4692 | AppendSTDOUT(out.data(), out.size()); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | static const char *end_delimiter = "--end--;"; |
| 4696 | static const int end_delimiter_len = 8; |
| 4697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4698 | void ProcessGDBRemote::HandleAsyncMisc(llvm::StringRef data) { |
| 4699 | std::string input = data.str(); // '1' to move beyond 'A' |
| 4700 | if (m_partial_profile_data.length() > 0) { |
| 4701 | m_partial_profile_data.append(input); |
| 4702 | input = m_partial_profile_data; |
| 4703 | m_partial_profile_data.clear(); |
| 4704 | } |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4705 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4706 | size_t found, pos = 0, len = input.length(); |
| 4707 | while ((found = input.find(end_delimiter, pos)) != std::string::npos) { |
| 4708 | StringExtractorGDBRemote profileDataExtractor( |
| 4709 | input.substr(pos, found).c_str()); |
| 4710 | std::string profile_data = |
| 4711 | HarmonizeThreadIdsForProfileData(profileDataExtractor); |
| 4712 | BroadcastAsyncProfileData(profile_data); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4713 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4714 | pos = found + end_delimiter_len; |
| 4715 | } |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4717 | if (pos < len) { |
| 4718 | // Last incomplete chunk. |
| 4719 | m_partial_profile_data = input.substr(pos); |
| 4720 | } |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4723 | std::string ProcessGDBRemote::HarmonizeThreadIdsForProfileData( |
| 4724 | StringExtractorGDBRemote &profileDataExtractor) { |
| 4725 | std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map; |
| 4726 | std::string output; |
| 4727 | llvm::raw_string_ostream output_stream(output); |
| 4728 | llvm::StringRef name, value; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4730 | // Going to assuming thread_used_usec comes first, else bail out. |
| 4731 | while (profileDataExtractor.GetNameColonValue(name, value)) { |
| 4732 | if (name.compare("thread_used_id") == 0) { |
| 4733 | StringExtractor threadIDHexExtractor(value); |
| 4734 | uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4735 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4736 | bool has_used_usec = false; |
| 4737 | uint32_t curr_used_usec = 0; |
| 4738 | llvm::StringRef usec_name, usec_value; |
| 4739 | uint32_t input_file_pos = profileDataExtractor.GetFilePos(); |
| 4740 | if (profileDataExtractor.GetNameColonValue(usec_name, usec_value)) { |
| 4741 | if (usec_name.equals("thread_used_usec")) { |
| 4742 | has_used_usec = true; |
| 4743 | usec_value.getAsInteger(0, curr_used_usec); |
| 4744 | } else { |
| 4745 | // We didn't find what we want, it is probably |
| 4746 | // an older version. Bail out. |
| 4747 | profileDataExtractor.SetFilePos(input_file_pos); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4748 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | if (has_used_usec) { |
| 4752 | uint32_t prev_used_usec = 0; |
| 4753 | std::map<uint64_t, uint32_t>::iterator iterator = |
| 4754 | m_thread_id_to_used_usec_map.find(thread_id); |
| 4755 | if (iterator != m_thread_id_to_used_usec_map.end()) { |
| 4756 | prev_used_usec = m_thread_id_to_used_usec_map[thread_id]; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4757 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4758 | |
| 4759 | uint32_t real_used_usec = curr_used_usec - prev_used_usec; |
| 4760 | // A good first time record is one that runs for at least 0.25 sec |
| 4761 | bool good_first_time = |
| 4762 | (prev_used_usec == 0) && (real_used_usec > 250000); |
| 4763 | bool good_subsequent_time = |
| 4764 | (prev_used_usec > 0) && |
| 4765 | ((real_used_usec > 0) || (HasAssignedIndexIDToThread(thread_id))); |
| 4766 | |
| 4767 | if (good_first_time || good_subsequent_time) { |
| 4768 | // We try to avoid doing too many index id reservation, |
| 4769 | // resulting in fast increase of index ids. |
| 4770 | |
| 4771 | output_stream << name << ":"; |
| 4772 | int32_t index_id = AssignIndexIDToThread(thread_id); |
| 4773 | output_stream << index_id << ";"; |
| 4774 | |
| 4775 | output_stream << usec_name << ":" << usec_value << ";"; |
| 4776 | } else { |
| 4777 | // Skip past 'thread_used_name'. |
| 4778 | llvm::StringRef local_name, local_value; |
| 4779 | profileDataExtractor.GetNameColonValue(local_name, local_value); |
| 4780 | } |
| 4781 | |
| 4782 | // Store current time as previous time so that they can be compared |
| 4783 | // later. |
| 4784 | new_thread_id_to_used_usec_map[thread_id] = curr_used_usec; |
| 4785 | } else { |
| 4786 | // Bail out and use old string. |
| 4787 | output_stream << name << ":" << value << ";"; |
| 4788 | } |
| 4789 | } else { |
| 4790 | output_stream << name << ":" << value << ";"; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4791 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4792 | } |
| 4793 | output_stream << end_delimiter; |
| 4794 | m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4795 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4796 | return output_stream.str(); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4799 | void ProcessGDBRemote::HandleStopReply() { |
| 4800 | if (GetStopID() != 0) |
| 4801 | return; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4802 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4803 | if (GetID() == LLDB_INVALID_PROCESS_ID) { |
| 4804 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); |
| 4805 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 4806 | SetID(pid); |
| 4807 | } |
| 4808 | BuildDynamicRegisterInfo(true); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4809 | } |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 4810 | |
Todd Fiala | fcdb1af | 2016-09-10 00:06:29 +0000 | [diff] [blame] | 4811 | static const char *const s_async_json_packet_prefix = "JSON-async:"; |
| 4812 | |
| 4813 | static StructuredData::ObjectSP |
| 4814 | ParseStructuredDataPacket(llvm::StringRef packet) { |
| 4815 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 4816 | |
| 4817 | if (!packet.consume_front(s_async_json_packet_prefix)) { |
| 4818 | if (log) { |
| 4819 | log->Printf( |
| 4820 | "GDBRemoteCommmunicationClientBase::%s() received $J packet " |
| 4821 | "but was not a StructuredData packet: packet starts with " |
| 4822 | "%s", |
| 4823 | __FUNCTION__, |
| 4824 | packet.slice(0, strlen(s_async_json_packet_prefix)).str().c_str()); |
| 4825 | } |
| 4826 | return StructuredData::ObjectSP(); |
| 4827 | } |
| 4828 | |
| 4829 | // This is an asynchronous JSON packet, destined for a |
| 4830 | // StructuredDataPlugin. |
| 4831 | StructuredData::ObjectSP json_sp = StructuredData::ParseJSON(packet); |
| 4832 | if (log) { |
| 4833 | if (json_sp) { |
| 4834 | StreamString json_str; |
| 4835 | json_sp->Dump(json_str); |
| 4836 | json_str.Flush(); |
| 4837 | log->Printf("ProcessGDBRemote::%s() " |
| 4838 | "received Async StructuredData packet: %s", |
| 4839 | __FUNCTION__, json_str.GetString().c_str()); |
| 4840 | } else { |
| 4841 | log->Printf("ProcessGDBRemote::%s" |
| 4842 | "() received StructuredData packet:" |
| 4843 | " parse failure", |
| 4844 | __FUNCTION__); |
| 4845 | } |
| 4846 | } |
| 4847 | return json_sp; |
| 4848 | } |
| 4849 | |
| 4850 | void ProcessGDBRemote::HandleAsyncStructuredDataPacket(llvm::StringRef data) { |
| 4851 | auto structured_data_sp = ParseStructuredDataPacket(data); |
| 4852 | if (structured_data_sp) |
| 4853 | RouteAsyncStructuredData(structured_data_sp); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4856 | class CommandObjectProcessGDBRemoteSpeedTest : public CommandObjectParsed { |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4857 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4858 | CommandObjectProcessGDBRemoteSpeedTest(CommandInterpreter &interpreter) |
| 4859 | : CommandObjectParsed(interpreter, "process plugin packet speed-test", |
| 4860 | "Tests packet speeds of various sizes to determine " |
| 4861 | "the performance characteristics of the GDB remote " |
| 4862 | "connection. ", |
| 4863 | NULL), |
| 4864 | m_option_group(), |
| 4865 | m_num_packets(LLDB_OPT_SET_1, false, "count", 'c', 0, eArgTypeCount, |
| 4866 | "The number of packets to send of each varying size " |
| 4867 | "(default is 1000).", |
| 4868 | 1000), |
| 4869 | m_max_send(LLDB_OPT_SET_1, false, "max-send", 's', 0, eArgTypeCount, |
| 4870 | "The maximum number of bytes to send in a packet. Sizes " |
| 4871 | "increase in powers of 2 while the size is less than or " |
| 4872 | "equal to this option value. (default 1024).", |
| 4873 | 1024), |
| 4874 | m_max_recv(LLDB_OPT_SET_1, false, "max-receive", 'r', 0, eArgTypeCount, |
| 4875 | "The maximum number of bytes to receive in a packet. Sizes " |
| 4876 | "increase in powers of 2 while the size is less than or " |
| 4877 | "equal to this option value. (default 1024).", |
| 4878 | 1024), |
| 4879 | m_json(LLDB_OPT_SET_1, false, "json", 'j', |
| 4880 | "Print the output as JSON data for easy parsing.", false, true) { |
| 4881 | m_option_group.Append(&m_num_packets, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4882 | m_option_group.Append(&m_max_send, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4883 | m_option_group.Append(&m_max_recv, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4884 | m_option_group.Append(&m_json, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4885 | m_option_group.Finalize(); |
| 4886 | } |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4887 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4888 | ~CommandObjectProcessGDBRemoteSpeedTest() {} |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 4889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4890 | Options *GetOptions() override { return &m_option_group; } |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4891 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4892 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 4893 | const size_t argc = command.GetArgumentCount(); |
| 4894 | if (argc == 0) { |
| 4895 | ProcessGDBRemote *process = |
| 4896 | (ProcessGDBRemote *)m_interpreter.GetExecutionContext() |
| 4897 | .GetProcessPtr(); |
| 4898 | if (process) { |
| 4899 | StreamSP output_stream_sp( |
| 4900 | m_interpreter.GetDebugger().GetAsyncOutputStream()); |
| 4901 | result.SetImmediateOutputStream(output_stream_sp); |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4903 | const uint32_t num_packets = |
| 4904 | (uint32_t)m_num_packets.GetOptionValue().GetCurrentValue(); |
| 4905 | const uint64_t max_send = m_max_send.GetOptionValue().GetCurrentValue(); |
| 4906 | const uint64_t max_recv = m_max_recv.GetOptionValue().GetCurrentValue(); |
| 4907 | const bool json = m_json.GetOptionValue().GetCurrentValue(); |
| 4908 | if (output_stream_sp) |
| 4909 | process->GetGDBRemote().TestPacketSpeed( |
| 4910 | num_packets, max_send, max_recv, json, *output_stream_sp); |
| 4911 | else { |
| 4912 | process->GetGDBRemote().TestPacketSpeed( |
| 4913 | num_packets, max_send, max_recv, json, result.GetOutputStream()); |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4914 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4915 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 4916 | return true; |
| 4917 | } |
| 4918 | } else { |
| 4919 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 4920 | m_cmd_name.c_str()); |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4921 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4922 | result.SetStatus(eReturnStatusFailed); |
| 4923 | return false; |
| 4924 | } |
| 4925 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4926 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4927 | OptionGroupOptions m_option_group; |
| 4928 | OptionGroupUInt64 m_num_packets; |
| 4929 | OptionGroupUInt64 m_max_send; |
| 4930 | OptionGroupUInt64 m_max_recv; |
| 4931 | OptionGroupBoolean m_json; |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4932 | }; |
| 4933 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4934 | class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 4935 | private: |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4936 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4937 | CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) |
| 4938 | : CommandObjectParsed(interpreter, "process plugin packet history", |
| 4939 | "Dumps the packet history buffer. ", NULL) {} |
| 4940 | |
| 4941 | ~CommandObjectProcessGDBRemotePacketHistory() {} |
| 4942 | |
| 4943 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 4944 | const size_t argc = command.GetArgumentCount(); |
| 4945 | if (argc == 0) { |
| 4946 | ProcessGDBRemote *process = |
| 4947 | (ProcessGDBRemote *)m_interpreter.GetExecutionContext() |
| 4948 | .GetProcessPtr(); |
| 4949 | if (process) { |
| 4950 | process->GetGDBRemote().DumpHistory(result.GetOutputStream()); |
| 4951 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 4952 | return true; |
| 4953 | } |
| 4954 | } else { |
| 4955 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 4956 | m_cmd_name.c_str()); |
| 4957 | } |
| 4958 | result.SetStatus(eReturnStatusFailed); |
| 4959 | return false; |
| 4960 | } |
| 4961 | }; |
| 4962 | |
| 4963 | class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed { |
| 4964 | private: |
| 4965 | public: |
| 4966 | CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) |
| 4967 | : CommandObjectParsed( |
| 4968 | interpreter, "process plugin packet xfer-size", |
| 4969 | "Maximum size that lldb will try to read/write one one chunk.", |
| 4970 | NULL) {} |
| 4971 | |
| 4972 | ~CommandObjectProcessGDBRemotePacketXferSize() {} |
| 4973 | |
| 4974 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 4975 | const size_t argc = command.GetArgumentCount(); |
| 4976 | if (argc == 0) { |
| 4977 | result.AppendErrorWithFormat("'%s' takes an argument to specify the max " |
| 4978 | "amount to be transferred when " |
| 4979 | "reading/writing", |
| 4980 | m_cmd_name.c_str()); |
| 4981 | result.SetStatus(eReturnStatusFailed); |
| 4982 | return false; |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4983 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4984 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4985 | ProcessGDBRemote *process = |
| 4986 | (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 4987 | if (process) { |
| 4988 | const char *packet_size = command.GetArgumentAtIndex(0); |
| 4989 | errno = 0; |
| 4990 | uint64_t user_specified_max = strtoul(packet_size, NULL, 10); |
| 4991 | if (errno == 0 && user_specified_max != 0) { |
| 4992 | process->SetUserSpecifiedMaxMemoryTransferSize(user_specified_max); |
| 4993 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 4994 | return true; |
| 4995 | } |
| 4996 | } |
| 4997 | result.SetStatus(eReturnStatusFailed); |
| 4998 | return false; |
| 4999 | } |
| 5000 | }; |
| 5001 | |
| 5002 | class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed { |
| 5003 | private: |
| 5004 | public: |
| 5005 | CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) |
| 5006 | : CommandObjectParsed(interpreter, "process plugin packet send", |
| 5007 | "Send a custom packet through the GDB remote " |
| 5008 | "protocol and print the answer. " |
| 5009 | "The packet header and footer will automatically " |
| 5010 | "be added to the packet prior to sending and " |
| 5011 | "stripped from the result.", |
| 5012 | NULL) {} |
| 5013 | |
| 5014 | ~CommandObjectProcessGDBRemotePacketSend() {} |
| 5015 | |
| 5016 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 5017 | const size_t argc = command.GetArgumentCount(); |
| 5018 | if (argc == 0) { |
| 5019 | result.AppendErrorWithFormat( |
| 5020 | "'%s' takes a one or more packet content arguments", |
| 5021 | m_cmd_name.c_str()); |
| 5022 | result.SetStatus(eReturnStatusFailed); |
| 5023 | return false; |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5024 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5025 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5026 | ProcessGDBRemote *process = |
| 5027 | (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5028 | if (process) { |
| 5029 | for (size_t i = 0; i < argc; ++i) { |
| 5030 | const char *packet_cstr = command.GetArgumentAtIndex(0); |
| 5031 | bool send_async = true; |
| 5032 | StringExtractorGDBRemote response; |
| 5033 | process->GetGDBRemote().SendPacketAndWaitForResponse( |
| 5034 | packet_cstr, response, send_async); |
| 5035 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 5036 | Stream &output_strm = result.GetOutputStream(); |
| 5037 | output_strm.Printf(" packet: %s\n", packet_cstr); |
| 5038 | std::string &response_str = response.GetStringRef(); |
| 5039 | |
| 5040 | if (strstr(packet_cstr, "qGetProfileData") != NULL) { |
| 5041 | response_str = process->HarmonizeThreadIdsForProfileData(response); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5042 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5043 | |
| 5044 | if (response_str.empty()) |
| 5045 | output_strm.PutCString("response: \nerror: UNIMPLEMENTED\n"); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5046 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5047 | output_strm.Printf("response: %s\n", response.GetStringRef().c_str()); |
| 5048 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5049 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5050 | return true; |
| 5051 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5052 | }; |
| 5053 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5054 | class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5055 | private: |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5056 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5057 | CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) |
| 5058 | : CommandObjectRaw(interpreter, "process plugin packet monitor", |
| 5059 | "Send a qRcmd packet through the GDB remote protocol " |
| 5060 | "and print the response." |
| 5061 | "The argument passed to this command will be hex " |
| 5062 | "encoded into a valid 'qRcmd' packet, sent and the " |
| 5063 | "response will be printed.", |
| 5064 | NULL) {} |
| 5065 | |
| 5066 | ~CommandObjectProcessGDBRemotePacketMonitor() {} |
| 5067 | |
| 5068 | bool DoExecute(const char *command, CommandReturnObject &result) override { |
| 5069 | if (command == NULL || command[0] == '\0') { |
| 5070 | result.AppendErrorWithFormat("'%s' takes a command string argument", |
| 5071 | m_cmd_name.c_str()); |
| 5072 | result.SetStatus(eReturnStatusFailed); |
| 5073 | return false; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5074 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5075 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5076 | ProcessGDBRemote *process = |
| 5077 | (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5078 | if (process) { |
| 5079 | StreamString packet; |
| 5080 | packet.PutCString("qRcmd,"); |
| 5081 | packet.PutBytesAsRawHex8(command, strlen(command)); |
| 5082 | const char *packet_cstr = packet.GetString().c_str(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5083 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5084 | bool send_async = true; |
| 5085 | StringExtractorGDBRemote response; |
| 5086 | process->GetGDBRemote().SendPacketAndWaitForResponse( |
| 5087 | packet_cstr, response, send_async); |
| 5088 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 5089 | Stream &output_strm = result.GetOutputStream(); |
| 5090 | output_strm.Printf(" packet: %s\n", packet_cstr); |
| 5091 | const std::string &response_str = response.GetStringRef(); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5092 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5093 | if (response_str.empty()) |
| 5094 | output_strm.PutCString("response: \nerror: UNIMPLEMENTED\n"); |
| 5095 | else |
| 5096 | output_strm.Printf("response: %s\n", response.GetStringRef().c_str()); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5097 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5098 | return true; |
| 5099 | } |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5100 | }; |
| 5101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5102 | class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5103 | private: |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5104 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5105 | CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) |
| 5106 | : CommandObjectMultiword(interpreter, "process plugin packet", |
| 5107 | "Commands that deal with GDB remote packets.", |
| 5108 | NULL) { |
| 5109 | LoadSubCommand( |
| 5110 | "history", |
| 5111 | CommandObjectSP( |
| 5112 | new CommandObjectProcessGDBRemotePacketHistory(interpreter))); |
| 5113 | LoadSubCommand( |
| 5114 | "send", CommandObjectSP( |
| 5115 | new CommandObjectProcessGDBRemotePacketSend(interpreter))); |
| 5116 | LoadSubCommand( |
| 5117 | "monitor", |
| 5118 | CommandObjectSP( |
| 5119 | new CommandObjectProcessGDBRemotePacketMonitor(interpreter))); |
| 5120 | LoadSubCommand( |
| 5121 | "xfer-size", |
| 5122 | CommandObjectSP( |
| 5123 | new CommandObjectProcessGDBRemotePacketXferSize(interpreter))); |
| 5124 | LoadSubCommand("speed-test", |
| 5125 | CommandObjectSP(new CommandObjectProcessGDBRemoteSpeedTest( |
| 5126 | interpreter))); |
| 5127 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5129 | ~CommandObjectProcessGDBRemotePacket() {} |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5130 | }; |
| 5131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5132 | class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword { |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5133 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5134 | CommandObjectMultiwordProcessGDBRemote(CommandInterpreter &interpreter) |
| 5135 | : CommandObjectMultiword( |
| 5136 | interpreter, "process plugin", |
| 5137 | "Commands for operating on a ProcessGDBRemote process.", |
| 5138 | "process plugin <subcommand> [<subcommand-options>]") { |
| 5139 | LoadSubCommand( |
| 5140 | "packet", |
| 5141 | CommandObjectSP(new CommandObjectProcessGDBRemotePacket(interpreter))); |
| 5142 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5144 | ~CommandObjectMultiwordProcessGDBRemote() {} |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5145 | }; |
| 5146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5147 | CommandObject *ProcessGDBRemote::GetPluginCommandObject() { |
| 5148 | if (!m_command_sp) |
| 5149 | m_command_sp.reset(new CommandObjectMultiwordProcessGDBRemote( |
| 5150 | GetTarget().GetDebugger().GetCommandInterpreter())); |
| 5151 | return m_command_sp.get(); |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5152 | } |