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