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> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 17 | #include <sys/mman.h> // for mmap |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 18 | #include <sys/socket.h> |
Zachary Turner | bd22bf2 | 2016-08-12 16:52:31 +0000 | [diff] [blame] | 19 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
Stephen Wilson | dc91686 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 22 | #include <time.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | // C++ Includes |
| 25 | #include <algorithm> |
| 26 | #include <map> |
Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame] | 27 | #include <mutex> |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 28 | #include <sstream> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 30 | #include "lldb/Breakpoint/Watchpoint.h" |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 31 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Core/ArchSpec.h" |
| 33 | #include "lldb/Core/Debugger.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 34 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Greg Clayton | 53239f0 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 35 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 37 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | #include "lldb/Core/PluginManager.h" |
| 39 | #include "lldb/Core/State.h" |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 40 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | #include "lldb/Core/StreamString.h" |
| 42 | #include "lldb/Core/Timer.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 43 | #include "lldb/Core/Value.h" |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 44 | #include "lldb/DataFormatters/FormatManager.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 45 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 46 | #include "lldb/Host/HostThread.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 47 | #include "lldb/Host/StringConvert.h" |
Jason Molenda | d1fae14 | 2012-09-29 08:03:33 +0000 | [diff] [blame] | 48 | #include "lldb/Host/Symbols.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 49 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 51 | #include "lldb/Host/XML.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 52 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 53 | #include "lldb/Interpreter/CommandObject.h" |
| 54 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 55 | #include "lldb/Interpreter/CommandReturnObject.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 56 | #include "lldb/Interpreter/OptionValueProperties.h" |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 57 | #include "lldb/Interpreter/Options.h" |
| 58 | #include "lldb/Interpreter/OptionGroupBoolean.h" |
| 59 | #include "lldb/Interpreter/OptionGroupUInt64.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 60 | #include "lldb/Interpreter/Property.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | #include "lldb/Symbol/ObjectFile.h" |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 62 | #include "lldb/Target/ABI.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | #include "lldb/Target/DynamicLoader.h" |
| 64 | #include "lldb/Target/Target.h" |
| 65 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 66 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 67 | #include "lldb/Target/SystemRuntime.h" |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 68 | #include "lldb/Utility/CleanUp.h" |
Jason Molenda | a34a0c6 | 2010-06-09 21:28:42 +0000 | [diff] [blame] | 69 | #include "lldb/Utility/PseudoTerminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 71 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | #include "lldb/Host/Host.h" |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 73 | #include "Plugins/Process/Utility/GDBRemoteSignals.h" |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 74 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Jason Molenda | c42d243 | 2012-07-25 03:40:06 +0000 | [diff] [blame] | 75 | #include "Plugins/Process/Utility/StopInfoMachException.h" |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 76 | #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 77 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | #include "GDBRemoteRegisterContext.h" |
| 79 | #include "ProcessGDBRemote.h" |
| 80 | #include "ProcessGDBRemoteLog.h" |
| 81 | #include "ThreadGDBRemote.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 82 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/StringSwitch.h" |
| 84 | #include "llvm/Support/raw_ostream.h" |
| 85 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 86 | #define DEBUGSERVER_BASENAME "debugserver" |
| 87 | using namespace lldb; |
| 88 | using namespace lldb_private; |
| 89 | using namespace lldb_private::process_gdb_remote; |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 90 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 91 | namespace lldb |
| 92 | { |
| 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 |
| 100 | DumpProcessGDBRemotePacketHistory (void *p, const char *path) |
| 101 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 102 | StreamFile strm; |
| 103 | Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate)); |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 104 | if (error.Success()) |
| 105 | ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 106 | } |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 109 | namespace { |
Steve Pucci | 5ec012d | 2014-01-16 22:18:14 +0000 | [diff] [blame] | 110 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 111 | static PropertyDefinition |
| 112 | g_properties[] = |
| 113 | { |
| 114 | { "packet-timeout" , OptionValue::eTypeUInt64 , true , 1, NULL, NULL, "Specify the default packet timeout in seconds." }, |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 115 | { "target-definition-file" , OptionValue::eTypeFileSpec , true, 0 , NULL, NULL, "The file that provides the description for remote target registers." }, |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 116 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 117 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 118 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 119 | enum |
| 120 | { |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 121 | ePropertyPacketTimeout, |
| 122 | ePropertyTargetDefinitionFile |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 123 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 124 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 125 | class PluginProperties : public Properties |
| 126 | { |
| 127 | public: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 128 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 129 | static ConstString |
| 130 | GetSettingName () |
| 131 | { |
| 132 | return ProcessGDBRemote::GetPluginNameStatic(); |
| 133 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 134 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 135 | PluginProperties() : |
| 136 | Properties () |
| 137 | { |
| 138 | m_collection_sp.reset (new OptionValueProperties(GetSettingName())); |
| 139 | m_collection_sp->Initialize(g_properties); |
| 140 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 141 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 142 | virtual |
| 143 | ~PluginProperties() |
| 144 | { |
| 145 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 146 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 147 | uint64_t |
| 148 | GetPacketTimeout() |
| 149 | { |
| 150 | const uint32_t idx = ePropertyPacketTimeout; |
| 151 | return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); |
| 152 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 153 | |
| 154 | bool |
| 155 | SetPacketTimeout(uint64_t timeout) |
| 156 | { |
| 157 | const uint32_t idx = ePropertyPacketTimeout; |
| 158 | return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout); |
| 159 | } |
| 160 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 161 | FileSpec |
| 162 | GetTargetDefinitionFile () const |
| 163 | { |
| 164 | const uint32_t idx = ePropertyTargetDefinitionFile; |
| 165 | return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx); |
| 166 | } |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 167 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 168 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 169 | typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 170 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 171 | static const ProcessKDPPropertiesSP & |
| 172 | GetGlobalPluginProperties() |
| 173 | { |
| 174 | static ProcessKDPPropertiesSP g_settings_sp; |
| 175 | if (!g_settings_sp) |
| 176 | g_settings_sp.reset (new PluginProperties ()); |
| 177 | return g_settings_sp; |
| 178 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 179 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 180 | } // anonymous namespace end |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 181 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 182 | // TODO Randomly assigning a port is unsafe. We should get an unused |
| 183 | // ephemeral port from the kernel and make sure we reserve it before passing |
| 184 | // it to debugserver. |
| 185 | |
| 186 | #if defined (__APPLE__) |
| 187 | #define LOW_PORT (IPPORT_RESERVED) |
| 188 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
| 189 | #else |
| 190 | #define LOW_PORT (1024u) |
| 191 | #define HIGH_PORT (49151u) |
| 192 | #endif |
| 193 | |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 194 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 195 | static bool rand_initialized = false; |
| 196 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 197 | static inline uint16_t |
| 198 | get_random_port () |
| 199 | { |
| 200 | if (!rand_initialized) |
| 201 | { |
| 202 | time_t seed = time(NULL); |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 203 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 204 | rand_initialized = true; |
| 205 | srand(seed); |
| 206 | } |
| 207 | return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT; |
| 208 | } |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 209 | #endif |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 210 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 211 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | ProcessGDBRemote::GetPluginNameStatic() |
| 213 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 214 | static ConstString g_name("gdb-remote"); |
| 215 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | const char * |
| 219 | ProcessGDBRemote::GetPluginDescriptionStatic() |
| 220 | { |
| 221 | return "GDB Remote protocol based debugging plug-in."; |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | ProcessGDBRemote::Terminate() |
| 226 | { |
| 227 | PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance); |
| 228 | } |
| 229 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 230 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 231 | lldb::ProcessSP |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 232 | ProcessGDBRemote::CreateInstance (lldb::TargetSP target_sp, ListenerSP listener_sp, const FileSpec *crash_file_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 234 | lldb::ProcessSP process_sp; |
| 235 | if (crash_file_path == NULL) |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 236 | process_sp.reset (new ProcessGDBRemote (target_sp, listener_sp)); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 237 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 241 | ProcessGDBRemote::CanDebug (lldb::TargetSP target_sp, bool plugin_specified_by_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | { |
Greg Clayton | 596ed24 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 243 | if (plugin_specified_by_name) |
| 244 | return true; |
| 245 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | // For now we are just making sure the file exists for a given module |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 247 | Module *exe_module = target_sp->GetExecutableModulePointer(); |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 248 | if (exe_module) |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 249 | { |
| 250 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 251 | // We can't debug core files... |
| 252 | switch (exe_objfile->GetType()) |
| 253 | { |
| 254 | case ObjectFile::eTypeInvalid: |
| 255 | case ObjectFile::eTypeCoreFile: |
| 256 | case ObjectFile::eTypeDebugInfo: |
| 257 | case ObjectFile::eTypeObjectFile: |
| 258 | case ObjectFile::eTypeSharedLibrary: |
| 259 | case ObjectFile::eTypeStubLibrary: |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 260 | case ObjectFile::eTypeJIT: |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 261 | return false; |
| 262 | case ObjectFile::eTypeExecutable: |
| 263 | case ObjectFile::eTypeDynamicLinker: |
| 264 | case ObjectFile::eTypeUnknown: |
| 265 | break; |
| 266 | } |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 267 | return exe_module->GetFileSpec().Exists(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 268 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 269 | // However, if there is no executable module, we return true since we might be preparing to attach. |
| 270 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 273 | //---------------------------------------------------------------------- |
| 274 | // ProcessGDBRemote constructor |
| 275 | //---------------------------------------------------------------------- |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 276 | ProcessGDBRemote::ProcessGDBRemote(lldb::TargetSP target_sp, ListenerSP listener_sp) |
| 277 | : Process(target_sp, listener_sp), |
| 278 | m_flags(0), |
| 279 | m_gdb_comm(), |
| 280 | m_debugserver_pid(LLDB_INVALID_PROCESS_ID), |
| 281 | m_last_stop_packet_mutex(), |
| 282 | m_register_info(), |
| 283 | m_async_broadcaster(NULL, "lldb.process.gdb-remote.async-broadcaster"), |
| 284 | m_async_listener_sp(Listener::MakeListener("lldb.process.gdb-remote.async-listener")), |
| 285 | m_async_thread_state_mutex(), |
| 286 | m_thread_ids(), |
| 287 | m_thread_pcs(), |
| 288 | m_jstopinfo_sp(), |
| 289 | m_jthreadsinfo_sp(), |
| 290 | m_continue_c_tids(), |
| 291 | m_continue_C_tids(), |
| 292 | m_continue_s_tids(), |
| 293 | m_continue_S_tids(), |
| 294 | m_max_memory_size(0), |
| 295 | m_remote_stub_max_memory_size(0), |
| 296 | m_addr_to_mmap_size(), |
| 297 | m_thread_create_bp_sp(), |
| 298 | m_waiting_for_attach(false), |
| 299 | m_destroy_tried_resuming(false), |
| 300 | m_command_sp(), |
| 301 | m_breakpoint_pc_offset(0), |
| 302 | m_initial_tid(LLDB_INVALID_THREAD_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 304 | m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 305 | m_async_broadcaster.SetEventName(eBroadcastBitAsyncContinue, "async thread continue"); |
| 306 | m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadDidExit, "async thread did exit"); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 307 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 308 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_ASYNC)); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 309 | |
| 310 | const uint32_t async_event_mask = eBroadcastBitAsyncContinue | eBroadcastBitAsyncThreadShouldExit; |
| 311 | |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 312 | if (m_async_listener_sp->StartListeningForEvents(&m_async_broadcaster, async_event_mask) != async_event_mask) |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 313 | { |
| 314 | if (log) |
| 315 | log->Printf("ProcessGDBRemote::%s failed to listen for m_async_broadcaster events", __FUNCTION__); |
| 316 | } |
| 317 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 318 | const uint32_t gdb_event_mask = |
| 319 | Communication::eBroadcastBitReadThreadDidExit | GDBRemoteCommunication::eBroadcastBitGdbReadThreadGotNotify; |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 320 | if (m_async_listener_sp->StartListeningForEvents(&m_gdb_comm, gdb_event_mask) != gdb_event_mask) |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 321 | { |
| 322 | if (log) |
| 323 | log->Printf("ProcessGDBRemote::%s failed to listen for m_gdb_comm events", __FUNCTION__); |
| 324 | } |
| 325 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 326 | const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); |
| 327 | if (timeout_seconds > 0) |
| 328 | m_gdb_comm.SetPacketTimeout(timeout_seconds); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 331 | //---------------------------------------------------------------------- |
| 332 | // Destructor |
| 333 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | ProcessGDBRemote::~ProcessGDBRemote() |
| 335 | { |
| 336 | // m_mach_process.UnregisterNotificationCallbacks (this); |
| 337 | Clear(); |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 338 | // We need to call finalize on the process before destroying ourselves |
| 339 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 340 | // destruct this class, then Process::~Process() might have problems |
| 341 | // trying to fully destroy the broadcaster. |
| 342 | Finalize(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 343 | |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 344 | // The general Finalize is going to try to destroy the process and that SHOULD |
| 345 | // shut down the async thread. However, if we don't kill it it will get stranded and |
| 346 | // its connection will go away so when it wakes up it will crash. So kill it for sure here. |
| 347 | StopAsyncThread(); |
| 348 | KillDebugserverProcess(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | //---------------------------------------------------------------------- |
| 352 | // PluginInterface |
| 353 | //---------------------------------------------------------------------- |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 354 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | ProcessGDBRemote::GetPluginName() |
| 356 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | return GetPluginNameStatic(); |
| 358 | } |
| 359 | |
| 360 | uint32_t |
| 361 | ProcessGDBRemote::GetPluginVersion() |
| 362 | { |
| 363 | return 1; |
| 364 | } |
| 365 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 366 | bool |
| 367 | ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec) |
| 368 | { |
| 369 | ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
| 370 | Error error; |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 371 | StructuredData::ObjectSP module_object_sp(interpreter->LoadPluginModule(target_definition_fspec, error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 372 | if (module_object_sp) |
| 373 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 374 | StructuredData::DictionarySP target_definition_sp( |
| 375 | interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), "gdb-server-target-definition", error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 376 | |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 377 | if (target_definition_sp) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 378 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 379 | StructuredData::ObjectSP target_object(target_definition_sp->GetValueForKey("host-info")); |
| 380 | if (target_object) |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 381 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 382 | if (auto host_info_dict = target_object->GetAsDictionary()) |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 383 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 384 | StructuredData::ObjectSP triple_value = host_info_dict->GetValueForKey("triple"); |
| 385 | if (auto triple_string_value = triple_value->GetAsString()) |
| 386 | { |
| 387 | std::string triple_string = triple_string_value->GetValue(); |
| 388 | ArchSpec host_arch(triple_string.c_str()); |
| 389 | if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture())) |
| 390 | { |
| 391 | GetTarget().SetArchitecture(host_arch); |
| 392 | } |
| 393 | } |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 394 | } |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 395 | } |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 396 | m_breakpoint_pc_offset = 0; |
| 397 | StructuredData::ObjectSP breakpoint_pc_offset_value = target_definition_sp->GetValueForKey("breakpoint-pc-offset"); |
| 398 | if (breakpoint_pc_offset_value) |
| 399 | { |
| 400 | if (auto breakpoint_pc_int_value = breakpoint_pc_offset_value->GetAsInteger()) |
| 401 | m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue(); |
| 402 | } |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 403 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 404 | if (m_register_info.SetRegisterInfo(*target_definition_sp, GetTarget().GetArchitecture()) > 0) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 405 | { |
| 406 | return true; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | return false; |
| 411 | } |
| 412 | |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 413 | // If the remote stub didn't give us eh_frame or DWARF register numbers for a register, |
| 414 | // see if the ABI can provide them. |
| 415 | // DWARF and eh_frame register numbers are defined as a part of the ABI. |
| 416 | static void |
| 417 | AugmentRegisterInfoViaABI (RegisterInfo ®_info, ConstString reg_name, ABISP abi_sp) |
| 418 | { |
| 419 | if (reg_info.kinds[eRegisterKindEHFrame] == LLDB_INVALID_REGNUM |
| 420 | || reg_info.kinds[eRegisterKindDWARF] == LLDB_INVALID_REGNUM) |
| 421 | { |
| 422 | if (abi_sp) |
| 423 | { |
| 424 | RegisterInfo abi_reg_info; |
| 425 | if (abi_sp->GetRegisterInfoByName (reg_name, abi_reg_info)) |
| 426 | { |
Greg Clayton | b1583dc | 2015-12-04 18:37:48 +0000 | [diff] [blame] | 427 | if (reg_info.kinds[eRegisterKindEHFrame] == LLDB_INVALID_REGNUM && |
| 428 | abi_reg_info.kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM) |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 429 | { |
| 430 | reg_info.kinds[eRegisterKindEHFrame] = abi_reg_info.kinds[eRegisterKindEHFrame]; |
| 431 | } |
Greg Clayton | b1583dc | 2015-12-04 18:37:48 +0000 | [diff] [blame] | 432 | if (reg_info.kinds[eRegisterKindDWARF] == LLDB_INVALID_REGNUM && |
| 433 | abi_reg_info.kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM) |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 434 | { |
| 435 | reg_info.kinds[eRegisterKindDWARF] = abi_reg_info.kinds[eRegisterKindDWARF]; |
| 436 | } |
Greg Clayton | b1583dc | 2015-12-04 18:37:48 +0000 | [diff] [blame] | 437 | if (reg_info.kinds[eRegisterKindGeneric] == LLDB_INVALID_REGNUM && |
| 438 | abi_reg_info.kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) |
| 439 | { |
| 440 | reg_info.kinds[eRegisterKindGeneric] = abi_reg_info.kinds[eRegisterKindGeneric]; |
| 441 | } |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 447 | static size_t |
| 448 | SplitCommaSeparatedRegisterNumberString(const llvm::StringRef &comma_separated_regiter_numbers, std::vector<uint32_t> ®nums, int base) |
| 449 | { |
| 450 | regnums.clear(); |
| 451 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 452 | value_pair.second = comma_separated_regiter_numbers; |
| 453 | do |
| 454 | { |
| 455 | value_pair = value_pair.second.split(','); |
| 456 | if (!value_pair.first.empty()) |
| 457 | { |
| 458 | uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, base); |
| 459 | if (reg != LLDB_INVALID_REGNUM) |
| 460 | regnums.push_back (reg); |
| 461 | } |
| 462 | } while (!value_pair.second.empty()); |
| 463 | return regnums.size(); |
| 464 | } |
| 465 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 467 | void |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 468 | ProcessGDBRemote::BuildDynamicRegisterInfo (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 469 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 470 | if (!force && m_register_info.GetNumRegisters() > 0) |
| 471 | return; |
| 472 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | m_register_info.Clear(); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 474 | |
| 475 | // Check if qHostInfo specified a specific packet timeout for this connection. |
| 476 | // If so then lets update our setting so the user knows what the timeout is |
| 477 | // and can see it. |
| 478 | const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout(); |
| 479 | if (host_packet_timeout) |
| 480 | { |
| 481 | GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout); |
| 482 | } |
| 483 | |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 484 | // Register info search order: |
Ewan Crawford | 0c996a9 | 2015-06-30 13:08:44 +0000 | [diff] [blame] | 485 | // 1 - Use the target definition python file if one is specified. |
| 486 | // 2 - If the target definition doesn't have any of the info from the target.xml (registers) then proceed to read the target.xml. |
| 487 | // 3 - Fall back on the qRegisterInfo packets. |
| 488 | |
| 489 | FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile (); |
Jason Molenda | 91e468c | 2015-08-20 04:29:46 +0000 | [diff] [blame] | 490 | if (!target_definition_fspec.Exists()) |
| 491 | { |
| 492 | // If the filename doesn't exist, it may be a ~ not having been expanded - try to resolve it. |
| 493 | target_definition_fspec.ResolvePath(); |
| 494 | } |
Ewan Crawford | 0c996a9 | 2015-06-30 13:08:44 +0000 | [diff] [blame] | 495 | if (target_definition_fspec) |
| 496 | { |
| 497 | // See if we can get register definitions from a python file |
| 498 | if (ParsePythonTargetDefinition (target_definition_fspec)) |
Jason Molenda | 5543abb | 2015-08-20 03:05:09 +0000 | [diff] [blame] | 499 | { |
Ewan Crawford | 0c996a9 | 2015-06-30 13:08:44 +0000 | [diff] [blame] | 500 | return; |
Jason Molenda | 5543abb | 2015-08-20 03:05:09 +0000 | [diff] [blame] | 501 | } |
| 502 | else |
| 503 | { |
| 504 | StreamSP stream_sp = GetTarget().GetDebugger().GetAsyncOutputStream(); |
| 505 | stream_sp->Printf ("ERROR: target description file %s failed to parse.\n", target_definition_fspec.GetPath().c_str()); |
| 506 | } |
Ewan Crawford | 0c996a9 | 2015-06-30 13:08:44 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Jim Ingham | 7b71c0b | 2016-02-18 23:58:45 +0000 | [diff] [blame] | 509 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 510 | const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); |
| 511 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 512 | |
| 513 | // Use the process' architecture instead of the host arch, if available |
| 514 | ArchSpec arch_to_use; |
| 515 | if (remote_process_arch.IsValid ()) |
| 516 | arch_to_use = remote_process_arch; |
| 517 | else |
| 518 | arch_to_use = remote_host_arch; |
| 519 | |
| 520 | if (!arch_to_use.IsValid()) |
| 521 | arch_to_use = target_arch; |
| 522 | |
| 523 | if (GetGDBServerRegisterInfo (arch_to_use)) |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 524 | return; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 525 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 526 | char packet[128]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | uint32_t reg_offset = 0; |
| 528 | uint32_t reg_num = 0; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 529 | for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 530 | response_type == StringExtractorGDBRemote::eResponse; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 531 | ++reg_num) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 533 | const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 534 | assert (packet_len < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 536 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false) == GDBRemoteCommunication::PacketResult::Success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 537 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 538 | response_type = response.GetResponseType(); |
| 539 | if (response_type == StringExtractorGDBRemote::eResponse) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 541 | llvm::StringRef name; |
| 542 | llvm::StringRef value; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | ConstString reg_name; |
| 544 | ConstString alt_name; |
| 545 | ConstString set_name; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 546 | std::vector<uint32_t> value_regs; |
| 547 | std::vector<uint32_t> invalidate_regs; |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 548 | std::vector<uint8_t> dwarf_opcode_bytes; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | RegisterInfo reg_info = { NULL, // Name |
| 550 | NULL, // Alt name |
| 551 | 0, // byte size |
| 552 | reg_offset, // offset |
| 553 | eEncodingUint, // encoding |
Jason Molenda | bf67a30 | 2015-09-01 05:17:01 +0000 | [diff] [blame] | 554 | eFormatHex, // format |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | { |
Jason Molenda | bf67a30 | 2015-09-01 05:17:01 +0000 | [diff] [blame] | 556 | LLDB_INVALID_REGNUM, // eh_frame reg num |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 557 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 558 | LLDB_INVALID_REGNUM, // generic reg num |
Jason Molenda | 63bd0db | 2015-09-15 23:20:34 +0000 | [diff] [blame] | 559 | reg_num, // process plugin reg num |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 560 | reg_num // native register number |
Greg Clayton | 435d85a | 2012-02-29 19:27:27 +0000 | [diff] [blame] | 561 | }, |
| 562 | NULL, |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 563 | NULL, |
| 564 | NULL, // Dwarf expression opcode bytes pointer |
| 565 | 0 // Dwarf expression opcode bytes length |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | while (response.GetNameColonValue(name, value)) |
| 569 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 570 | if (name.equals("name")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 571 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 572 | reg_name.SetString(value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 573 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 574 | else if (name.equals("alt-name")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 575 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 576 | alt_name.SetString(value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 577 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 578 | else if (name.equals("bitsize")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 579 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 580 | value.getAsInteger(0, reg_info.byte_size); |
| 581 | reg_info.byte_size /= CHAR_BIT; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 583 | else if (name.equals("offset")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 585 | if (value.getAsInteger(0, reg_offset)) |
| 586 | reg_offset = UINT32_MAX; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 587 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 588 | else if (name.equals("encoding")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 589 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 590 | const Encoding encoding = Args::StringToEncoding(value); |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 591 | if (encoding != eEncodingInvalid) |
| 592 | reg_info.encoding = encoding; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 593 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 594 | else if (name.equals("format")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 595 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 596 | Format format = eFormatInvalid; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 597 | if (Args::StringToFormat(value.str().c_str(), format, NULL).Success()) |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 598 | reg_info.format = format; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 599 | else |
| 600 | { |
| 601 | reg_info.format = llvm::StringSwitch<Format>(value) |
| 602 | .Case("binary", eFormatBinary) |
| 603 | .Case("decimal", eFormatDecimal) |
| 604 | .Case("hex", eFormatHex) |
| 605 | .Case("float", eFormatFloat) |
| 606 | .Case("vector-sint8", eFormatVectorOfSInt8) |
| 607 | .Case("vector-uint8", eFormatVectorOfUInt8) |
| 608 | .Case("vector-sint16", eFormatVectorOfSInt16) |
| 609 | .Case("vector-uint16", eFormatVectorOfUInt16) |
| 610 | .Case("vector-sint32", eFormatVectorOfSInt32) |
| 611 | .Case("vector-uint32", eFormatVectorOfUInt32) |
| 612 | .Case("vector-float32", eFormatVectorOfFloat32) |
| 613 | .Case("vector-uint128", eFormatVectorOfUInt128) |
| 614 | .Default(eFormatInvalid); |
| 615 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 616 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 617 | else if (name.equals("set")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 618 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 619 | set_name.SetString(value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 620 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 621 | else if (name.equals("gcc") || name.equals("ehframe")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 622 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 623 | if (value.getAsInteger(0, reg_info.kinds[eRegisterKindEHFrame])) |
| 624 | reg_info.kinds[eRegisterKindEHFrame] = LLDB_INVALID_REGNUM; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 625 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 626 | else if (name.equals("dwarf")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 628 | if (value.getAsInteger(0, reg_info.kinds[eRegisterKindDWARF])) |
| 629 | reg_info.kinds[eRegisterKindDWARF] = LLDB_INVALID_REGNUM; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 630 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 631 | else if (name.equals("generic")) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 632 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 633 | reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister(value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 634 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 635 | else if (name.equals("container-regs")) |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 636 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 637 | SplitCommaSeparatedRegisterNumberString(value, value_regs, 16); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 638 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 639 | else if (name.equals("invalidate-regs")) |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 640 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 641 | SplitCommaSeparatedRegisterNumberString(value, invalidate_regs, 16); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 642 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 643 | else if (name.equals("dynamic_size_dwarf_expr_bytes")) |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 644 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 645 | size_t dwarf_opcode_len = value.size() / 2; |
| 646 | assert(dwarf_opcode_len > 0); |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 647 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 648 | dwarf_opcode_bytes.resize(dwarf_opcode_len); |
| 649 | reg_info.dynamic_size_dwarf_len = dwarf_opcode_len; |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 650 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 651 | StringExtractor opcode_extractor(value); |
Zachary Turner | d08f09c | 2016-08-30 18:12:11 +0000 | [diff] [blame] | 652 | uint32_t ret_val = opcode_extractor.GetHexBytesAvail(dwarf_opcode_bytes); |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 653 | assert(dwarf_opcode_len == ret_val); |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 654 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 655 | reg_info.dynamic_size_dwarf_expr_bytes = dwarf_opcode_bytes.data(); |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 659 | reg_info.byte_offset = reg_offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 660 | assert (reg_info.byte_size != 0); |
| 661 | reg_offset += reg_info.byte_size; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 662 | if (!value_regs.empty()) |
| 663 | { |
| 664 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 665 | reg_info.value_regs = value_regs.data(); |
| 666 | } |
| 667 | if (!invalidate_regs.empty()) |
| 668 | { |
| 669 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 670 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 671 | } |
| 672 | |
Jim Ingham | 7b71c0b | 2016-02-18 23:58:45 +0000 | [diff] [blame] | 673 | // We have to make a temporary ABI here, and not use the GetABI because this code |
| 674 | // gets called in DidAttach, when the target architecture (and consequently the ABI we'll get from |
| 675 | // the process) may be wrong. |
| 676 | ABISP abi_to_use = ABI::FindPlugin(arch_to_use); |
| 677 | |
| 678 | AugmentRegisterInfoViaABI (reg_info, reg_name, abi_to_use); |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 681 | } |
Todd Fiala | 1a63440 | 2014-01-08 07:52:40 +0000 | [diff] [blame] | 682 | else |
| 683 | { |
| 684 | break; // ensure exit before reg_num is incremented |
| 685 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | } |
| 687 | else |
| 688 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 689 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 693 | if (m_register_info.GetNumRegisters() > 0) |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 694 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 695 | m_register_info.Finalize(GetTarget().GetArchitecture()); |
| 696 | return; |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 697 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 698 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 699 | // We didn't get anything if the accumulated reg_num is zero. See if we are |
| 700 | // debugging ARM and fill with a hard coded register set until we can get an |
| 701 | // updated debugserver down on the devices. |
| 702 | // On the other hand, if the accumulated reg_num is positive, see if we can |
| 703 | // add composite registers to the existing primordial ones. |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 704 | bool from_scratch = (m_register_info.GetNumRegisters() == 0); |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 705 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 706 | if (!target_arch.IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 707 | { |
Jim Ingham | 7b71c0b | 2016-02-18 23:58:45 +0000 | [diff] [blame] | 708 | if (arch_to_use.IsValid() |
| 709 | && (arch_to_use.GetMachine() == llvm::Triple::arm || arch_to_use.GetMachine() == llvm::Triple::thumb) |
| 710 | && arch_to_use.GetTriple().getVendor() == llvm::Triple::Apple) |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 711 | m_register_info.HardcodeARMRegisters(from_scratch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 712 | } |
Jason Molenda | 6d9fe8c | 2015-08-21 00:13:37 +0000 | [diff] [blame] | 713 | else if (target_arch.GetMachine() == llvm::Triple::arm |
| 714 | || target_arch.GetMachine() == llvm::Triple::thumb) |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 715 | { |
| 716 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 717 | } |
| 718 | |
| 719 | // At this point, we can finalize our register info. |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 720 | m_register_info.Finalize (GetTarget().GetArchitecture()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | Error |
| 724 | ProcessGDBRemote::WillLaunch (Module* module) |
| 725 | { |
| 726 | return WillLaunchOrAttach (); |
| 727 | } |
| 728 | |
| 729 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 730 | ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 731 | { |
| 732 | return WillLaunchOrAttach (); |
| 733 | } |
| 734 | |
| 735 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 736 | ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 737 | { |
| 738 | return WillLaunchOrAttach (); |
| 739 | } |
| 740 | |
| 741 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 742 | ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 743 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 744 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 745 | Error error (WillLaunchOrAttach ()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 746 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 747 | if (error.Fail()) |
| 748 | return error; |
| 749 | |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 750 | error = ConnectToDebugserver (remote_url); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 751 | |
| 752 | if (error.Fail()) |
| 753 | return error; |
| 754 | StartAsyncThread (); |
| 755 | |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 756 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 757 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 758 | { |
| 759 | // We don't have a valid process ID, so note that we are connected |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 760 | // and could now request to launch or attach, or get remote process |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 761 | // listings... |
| 762 | SetPrivateState (eStateConnected); |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | // We have a valid process |
| 767 | SetID (pid); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 768 | GetThreadList(); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 769 | StringExtractorGDBRemote response; |
| 770 | if (m_gdb_comm.GetStopReply(response)) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 771 | { |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 772 | SetLastStopPacket(response); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 773 | |
| 774 | // '?' Packets must be handled differently in non-stop mode |
| 775 | if (GetTarget().GetNonStopModeEnabled()) |
| 776 | HandleStopReplySequence(); |
| 777 | |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 778 | Target &target = GetTarget(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 779 | if (!target.GetArchitecture().IsValid()) |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 780 | { |
| 781 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 782 | { |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 783 | target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 784 | } |
| 785 | else |
| 786 | { |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 787 | target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 788 | } |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 791 | const StateType state = SetThreadStopInfo (response); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 792 | if (state != eStateInvalid) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 793 | { |
| 794 | SetPrivateState (state); |
| 795 | } |
| 796 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 797 | error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 798 | } |
| 799 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 800 | error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 801 | } |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 802 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 803 | if (log) |
| 804 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalizing target architecture initial triple: %s (GetTarget().GetArchitecture().IsValid() %s, m_gdb_comm.GetHostArchitecture().IsValid(): %s)", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str (), GetTarget ().GetArchitecture ().IsValid () ? "true" : "false", m_gdb_comm.GetHostArchitecture ().IsValid () ? "true" : "false"); |
| 805 | |
| 806 | |
| 807 | if (error.Success() |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 808 | && !GetTarget().GetArchitecture().IsValid() |
| 809 | && m_gdb_comm.GetHostArchitecture().IsValid()) |
| 810 | { |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 811 | // Prefer the *process'* architecture over that of the *host*, if available. |
| 812 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 813 | GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 814 | else |
| 815 | GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 818 | if (log) |
| 819 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalized target architecture triple: %s", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str ()); |
| 820 | |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 821 | if (error.Success()) |
Jaydeep Patil | 6fc590d | 2015-07-30 05:06:51 +0000 | [diff] [blame] | 822 | { |
| 823 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 824 | if (platform_sp && platform_sp->IsConnected()) |
| 825 | SetUnixSignals(platform_sp->GetUnixSignals()); |
| 826 | else |
| 827 | SetUnixSignals(UnixSignals::Create(GetTarget().GetArchitecture())); |
| 828 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 829 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 830 | return error; |
| 831 | } |
| 832 | |
| 833 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 834 | ProcessGDBRemote::WillLaunchOrAttach () |
| 835 | { |
| 836 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 837 | m_stdio_communication.Clear (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | return error; |
| 839 | } |
| 840 | |
| 841 | //---------------------------------------------------------------------- |
| 842 | // Process Control |
| 843 | //---------------------------------------------------------------------- |
| 844 | Error |
Jean-Daniel Dupas | 7782de9 | 2013-12-09 22:52:50 +0000 | [diff] [blame] | 845 | ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 846 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 847 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 848 | Error error; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 849 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 850 | if (log) |
| 851 | log->Printf ("ProcessGDBRemote::%s() entered", __FUNCTION__); |
| 852 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 853 | uint32_t launch_flags = launch_info.GetFlags().Get(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 854 | FileSpec stdin_file_spec{}; |
| 855 | FileSpec stdout_file_spec{}; |
| 856 | FileSpec stderr_file_spec{}; |
| 857 | FileSpec working_dir = launch_info.GetWorkingDirectory(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 858 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 859 | const FileAction *file_action; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 860 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 861 | if (file_action) |
| 862 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 863 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 864 | stdin_file_spec = file_action->GetFileSpec(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 865 | } |
| 866 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 867 | if (file_action) |
| 868 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 869 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 870 | stdout_file_spec = file_action->GetFileSpec(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 871 | } |
| 872 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 873 | if (file_action) |
| 874 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 875 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 876 | stderr_file_spec = file_action->GetFileSpec(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 879 | if (log) |
| 880 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 881 | if (stdin_file_spec || stdout_file_spec || stderr_file_spec) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 882 | log->Printf ("ProcessGDBRemote::%s provided with STDIO paths via launch_info: stdin=%s, stdout=%s, stderr=%s", |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 883 | __FUNCTION__, |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 884 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 885 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 886 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 887 | else |
| 888 | log->Printf ("ProcessGDBRemote::%s no STDIO paths given via launch_info", __FUNCTION__); |
| 889 | } |
| 890 | |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 891 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 892 | if (stdin_file_spec || disable_stdio) |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 893 | { |
| 894 | // the inferior will be reading stdin from the specified file |
| 895 | // or stdio is completely disabled |
| 896 | m_stdin_forward = false; |
| 897 | } |
| 898 | else |
| 899 | { |
| 900 | m_stdin_forward = true; |
| 901 | } |
| 902 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 903 | // ::LogSetBitMask (GDBR_LOG_DEFAULT); |
| 904 | // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); |
| 905 | // ::LogSetLogFile ("/dev/stdout"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 906 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 907 | ObjectFile * object_file = exe_module->GetObjectFile(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 908 | if (object_file) |
| 909 | { |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 910 | error = EstablishConnectionIfNeeded (launch_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 911 | if (error.Success()) |
| 912 | { |
| 913 | lldb_utility::PseudoTerminal pty; |
| 914 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 915 | |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 916 | PlatformSP platform_sp (GetTarget().GetPlatform()); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 917 | if (disable_stdio) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 918 | { |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 919 | // set to /dev/null unless redirected to a file above |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 920 | if (!stdin_file_spec) |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 921 | stdin_file_spec.SetFile(FileSystem::DEV_NULL, false); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 922 | if (!stdout_file_spec) |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 923 | stdout_file_spec.SetFile(FileSystem::DEV_NULL, false); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 924 | if (!stderr_file_spec) |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 925 | stderr_file_spec.SetFile(FileSystem::DEV_NULL, false); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 926 | } |
| 927 | else if (platform_sp && platform_sp->IsHost()) |
| 928 | { |
| 929 | // If the debugserver is local and we aren't disabling STDIO, lets use |
| 930 | // a pseudo terminal to instead of relying on the 'O' packets for stdio |
| 931 | // since 'O' packets can really slow down debugging if the inferior |
| 932 | // does a lot of output. |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 933 | if ((!stdin_file_spec || !stdout_file_spec || !stderr_file_spec) && |
| 934 | pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 935 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 936 | FileSpec slave_name{pty.GetSlaveName(NULL, 0), false}; |
| 937 | |
| 938 | if (!stdin_file_spec) |
| 939 | stdin_file_spec = slave_name; |
| 940 | |
| 941 | if (!stdout_file_spec) |
| 942 | stdout_file_spec = slave_name; |
| 943 | |
| 944 | if (!stderr_file_spec) |
| 945 | stderr_file_spec = slave_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 946 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 947 | if (log) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 948 | log->Printf ("ProcessGDBRemote::%s adjusted STDIO paths for local platform (IsHost() is true) using slave: stdin=%s, stdout=%s, stderr=%s", |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 949 | __FUNCTION__, |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 950 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 951 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 952 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 955 | if (log) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 956 | log->Printf ("ProcessGDBRemote::%s final STDIO paths after all adjustments: stdin=%s, stdout=%s, stderr=%s", |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 957 | __FUNCTION__, |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 958 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 959 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 960 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 961 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 962 | if (stdin_file_spec) |
| 963 | m_gdb_comm.SetSTDIN(stdin_file_spec); |
| 964 | if (stdout_file_spec) |
| 965 | m_gdb_comm.SetSTDOUT(stdout_file_spec); |
| 966 | if (stderr_file_spec) |
| 967 | m_gdb_comm.SetSTDERR(stderr_file_spec); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 968 | |
| 969 | m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR); |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 970 | m_gdb_comm.SetDetachOnError (launch_flags & eLaunchFlagDetachOnError); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 971 | |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 972 | m_gdb_comm.SendLaunchArchPacket (GetTarget().GetArchitecture().GetArchitectureName()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 973 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 974 | const char * launch_event_data = launch_info.GetLaunchEventData(); |
| 975 | if (launch_event_data != NULL && *launch_event_data != '\0') |
| 976 | m_gdb_comm.SendLaunchEventDataPacket (launch_event_data); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 977 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 978 | if (working_dir) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 979 | { |
| 980 | m_gdb_comm.SetWorkingDir (working_dir); |
| 981 | } |
| 982 | |
| 983 | // Send the environment and the program + arguments after we connect |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 984 | const Args &environment = launch_info.GetEnvironmentEntries(); |
| 985 | if (environment.GetArgumentCount()) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 986 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 987 | size_t num_environment_entries = environment.GetArgumentCount(); |
| 988 | for (size_t i=0; i<num_environment_entries; ++i) |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 989 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 990 | const char *env_entry = environment.GetArgumentAtIndex(i); |
| 991 | if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 992 | break; |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 993 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 994 | } |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 995 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 996 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 997 | // Scope for the scoped timeout object |
| 998 | GDBRemoteCommunication::ScopedTimeout timeout (m_gdb_comm, 10); |
| 999 | |
| 1000 | int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info); |
| 1001 | if (arg_packet_err == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1002 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1003 | std::string error_str; |
| 1004 | if (m_gdb_comm.GetLaunchSuccess (error_str)) |
| 1005 | { |
| 1006 | SetID (m_gdb_comm.GetCurrentProcessID ()); |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | error.SetErrorString (error_str.c_str()); |
| 1011 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1012 | } |
| 1013 | else |
| 1014 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1015 | error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1016 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1017 | } |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1018 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1019 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1020 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 1021 | if (log) |
| 1022 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1023 | KillDebugserverProcess (); |
| 1024 | return error; |
| 1025 | } |
| 1026 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1027 | StringExtractorGDBRemote response; |
| 1028 | if (m_gdb_comm.GetStopReply(response)) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1029 | { |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1030 | SetLastStopPacket(response); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1031 | // '?' Packets must be handled differently in non-stop mode |
| 1032 | if (GetTarget().GetNonStopModeEnabled()) |
| 1033 | HandleStopReplySequence(); |
| 1034 | |
Tamas Berghammer | e9f4dfe | 2015-03-13 10:32:42 +0000 | [diff] [blame] | 1035 | const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 1036 | |
| 1037 | if (process_arch.IsValid()) |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 1038 | { |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 1039 | GetTarget().MergeArchitecture(process_arch); |
Tamas Berghammer | e9f4dfe | 2015-03-13 10:32:42 +0000 | [diff] [blame] | 1040 | } |
| 1041 | else |
| 1042 | { |
| 1043 | const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture(); |
| 1044 | if (host_arch.IsValid()) |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 1045 | GetTarget().MergeArchitecture(host_arch); |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1048 | SetPrivateState (SetThreadStopInfo (response)); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1049 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1050 | if (!disable_stdio) |
| 1051 | { |
| 1052 | if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 1053 | SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1054 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 1057 | else |
| 1058 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 1059 | if (log) |
| 1060 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 1061 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | // Set our user ID to an invalid process ID. |
| 1066 | SetID(LLDB_INVALID_PROCESS_ID); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1067 | error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", |
| 1068 | exe_module->GetFileSpec().GetFilename().AsCString(), |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 1069 | exe_module->GetArchitecture().GetArchitectureName()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1071 | return error; |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | Error |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 1077 | ProcessGDBRemote::ConnectToDebugserver (const char *connect_url) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1078 | { |
| 1079 | Error error; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1080 | // Only connect if we have a valid connect URL |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1081 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1082 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1083 | if (connect_url && connect_url[0]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1084 | { |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1085 | if (log) |
| 1086 | log->Printf("ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, connect_url); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1087 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 1088 | if (conn_ap.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1089 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1090 | const uint32_t max_retry_count = 50; |
| 1091 | uint32_t retry_count = 0; |
| 1092 | while (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1093 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1094 | if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) |
| 1095 | { |
| 1096 | m_gdb_comm.SetConnection (conn_ap.release()); |
| 1097 | break; |
| 1098 | } |
| 1099 | else if (error.WasInterrupted()) |
| 1100 | { |
| 1101 | // If we were interrupted, don't keep retrying. |
| 1102 | break; |
| 1103 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1104 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1105 | retry_count++; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1106 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1107 | if (retry_count >= max_retry_count) |
| 1108 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1109 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1110 | usleep (100000); |
| 1111 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (!m_gdb_comm.IsConnected()) |
| 1116 | { |
| 1117 | if (error.Success()) |
| 1118 | error.SetErrorString("not connected to remote gdb server"); |
| 1119 | return error; |
| 1120 | } |
| 1121 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 1122 | |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 1123 | // Start the communications read thread so all incoming data can be |
| 1124 | // parsed into packets and queued as they arrive. |
| 1125 | if (GetTarget().GetNonStopModeEnabled()) |
| 1126 | m_gdb_comm.StartReadThread(); |
| 1127 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1128 | // We always seem to be able to open a connection to a local port |
| 1129 | // so we need to make sure we can then send data to it. If we can't |
| 1130 | // then we aren't actually connected to anything, so try and do the |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1131 | // handshake with the remote GDB server and make sure that goes |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1132 | // alright. |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 1133 | if (!m_gdb_comm.HandshakeWithServer (&error)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1134 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1135 | m_gdb_comm.Disconnect(); |
| 1136 | if (error.Success()) |
| 1137 | error.SetErrorString("not connected to remote gdb server"); |
| 1138 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1139 | } |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Send $QNonStop:1 packet on startup if required |
| 1142 | if (GetTarget().GetNonStopModeEnabled()) |
Ewan Crawford | aa7eda7 | 2015-06-24 15:14:26 +0000 | [diff] [blame] | 1143 | GetTarget().SetNonStopModeEnabled (m_gdb_comm.SetNonStopMode(true)); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1144 | |
Greg Clayton | b30c50c | 2015-05-29 00:01:55 +0000 | [diff] [blame] | 1145 | m_gdb_comm.GetEchoSupported (); |
Pavel Labath | 5c95ee4 | 2016-08-30 13:56:11 +0000 | [diff] [blame] | 1146 | m_gdb_comm.GetThreadSuffixSupported (); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1147 | m_gdb_comm.GetListThreadsInStopReplySupported (); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1148 | m_gdb_comm.GetHostInfo (); |
| 1149 | m_gdb_comm.GetVContSupported ('c'); |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1150 | m_gdb_comm.GetVAttachOrWaitSupported(); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Ask the remote server for the default thread id |
| 1153 | if (GetTarget().GetNonStopModeEnabled()) |
| 1154 | m_gdb_comm.GetDefaultThreadId(m_initial_tid); |
| 1155 | |
| 1156 | |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 1157 | size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); |
| 1158 | for (size_t idx = 0; idx < num_cmds; idx++) |
| 1159 | { |
| 1160 | StringExtractorGDBRemote response; |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 1161 | m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false); |
| 1162 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1163 | return error; |
| 1164 | } |
| 1165 | |
| 1166 | void |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1167 | ProcessGDBRemote::DidLaunchOrAttach (ArchSpec& process_arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1168 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1169 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1170 | if (log) |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1171 | log->Printf ("ProcessGDBRemote::%s()", __FUNCTION__); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 1172 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1173 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1174 | BuildDynamicRegisterInfo (false); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1176 | // See if the GDB server supports the qHostInfo information |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1177 | |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1178 | |
| 1179 | // See if the GDB server supports the qProcessInfo packet, if so |
| 1180 | // prefer that over the Host information as it will be more specific |
| 1181 | // to our process. |
| 1182 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1183 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 1184 | if (remote_process_arch.IsValid()) |
| 1185 | { |
| 1186 | process_arch = remote_process_arch; |
| 1187 | if (log) |
| 1188 | log->Printf ("ProcessGDBRemote::%s gdb-remote had process architecture, using %s %s", |
| 1189 | __FUNCTION__, |
| 1190 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1191 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1192 | } |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1193 | else |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1194 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1195 | process_arch = m_gdb_comm.GetHostArchitecture(); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1196 | if (log) |
| 1197 | log->Printf ("ProcessGDBRemote::%s gdb-remote did not have process architecture, using gdb-remote host architecture %s %s", |
| 1198 | __FUNCTION__, |
| 1199 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1200 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1201 | } |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1202 | |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1203 | if (process_arch.IsValid()) |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1204 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1205 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1206 | if (target_arch.IsValid()) |
| 1207 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1208 | if (log) |
| 1209 | log->Printf ("ProcessGDBRemote::%s analyzing target arch, currently %s %s", |
| 1210 | __FUNCTION__, |
| 1211 | target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>", |
| 1212 | target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1213 | |
| 1214 | // If the remote host is ARM and we have apple as the vendor, then |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1215 | // ARM executables and shared libraries can have mixed ARM architectures. |
| 1216 | // You can have an armv6 executable, and if the host is armv7, then the |
| 1217 | // system will load the best possible architecture for all shared libraries |
| 1218 | // it has, so we really need to take the remote host architecture as our |
| 1219 | // defacto architecture in this case. |
| 1220 | |
Jason Molenda | 6d9fe8c | 2015-08-21 00:13:37 +0000 | [diff] [blame] | 1221 | if ((process_arch.GetMachine() == llvm::Triple::arm || process_arch.GetMachine() == llvm::Triple::thumb) |
| 1222 | && process_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1223 | { |
Jason Molenda | 921c01b | 2014-08-03 21:42:52 +0000 | [diff] [blame] | 1224 | GetTarget().SetArchitecture (process_arch); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1225 | if (log) |
| 1226 | log->Printf ("ProcessGDBRemote::%s remote process is ARM/Apple, setting target arch to %s %s", |
| 1227 | __FUNCTION__, |
| 1228 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1229 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | // Fill in what is missing in the triple |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1234 | const llvm::Triple &remote_triple = process_arch.GetTriple(); |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1235 | llvm::Triple new_target_triple = target_arch.GetTriple(); |
| 1236 | if (new_target_triple.getVendorName().size() == 0) |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1237 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1238 | new_target_triple.setVendor (remote_triple.getVendor()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1239 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1240 | if (new_target_triple.getOSName().size() == 0) |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1241 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1242 | new_target_triple.setOS (remote_triple.getOS()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1243 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1244 | if (new_target_triple.getEnvironmentName().size() == 0) |
| 1245 | new_target_triple.setEnvironment (remote_triple.getEnvironment()); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1246 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1247 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1248 | ArchSpec new_target_arch = target_arch; |
| 1249 | new_target_arch.SetTriple(new_target_triple); |
| 1250 | GetTarget().SetArchitecture(new_target_arch); |
| 1251 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1252 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1253 | |
| 1254 | if (log) |
| 1255 | log->Printf ("ProcessGDBRemote::%s final target arch after adjustments for remote architecture: %s %s", |
| 1256 | __FUNCTION__, |
| 1257 | target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>", |
| 1258 | target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>"); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | // The target doesn't have a valid architecture yet, set it from |
| 1263 | // the architecture we got from the remote GDB server |
Jason Molenda | 921c01b | 2014-08-03 21:42:52 +0000 | [diff] [blame] | 1264 | GetTarget().SetArchitecture (process_arch); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1265 | } |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1266 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1267 | |
| 1268 | // Find out which StructuredDataPlugins are supported by the |
| 1269 | // debug monitor. These plugins transmit data over async $J packets. |
| 1270 | auto supported_packets_array = |
| 1271 | m_gdb_comm.GetSupportedStructuredDataPlugins(); |
| 1272 | if (supported_packets_array) |
| 1273 | MapSupportedStructuredDataPlugins(*supported_packets_array); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | void |
| 1278 | ProcessGDBRemote::DidLaunch () |
| 1279 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1280 | ArchSpec process_arch; |
| 1281 | DidLaunchOrAttach (process_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | Error |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1285 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 1286 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1287 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1288 | Error error; |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (log) |
| 1291 | log->Printf ("ProcessGDBRemote::%s()", __FUNCTION__); |
| 1292 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1293 | // Clear out and clean up from any current state |
| 1294 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1295 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 1296 | { |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 1297 | error = EstablishConnectionIfNeeded (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1298 | if (error.Success()) |
| 1299 | { |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1300 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1301 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1302 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1303 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1304 | SetID (attach_pid); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1305 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1306 | } |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 1307 | else |
| 1308 | SetExitStatus (-1, error.AsCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1309 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1311 | return error; |
| 1312 | } |
| 1313 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1314 | Error |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 1315 | ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1316 | { |
| 1317 | Error error; |
| 1318 | // Clear out and clean up from any current state |
| 1319 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1321 | if (process_name && process_name[0]) |
| 1322 | { |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 1323 | error = EstablishConnectionIfNeeded (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1324 | if (error.Success()) |
| 1325 | { |
| 1326 | StreamString packet; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1327 | |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1328 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1329 | |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 1330 | if (attach_info.GetWaitForLaunch()) |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1331 | { |
| 1332 | if (!m_gdb_comm.GetVAttachOrWaitSupported()) |
| 1333 | { |
| 1334 | packet.PutCString ("vAttachWait"); |
| 1335 | } |
| 1336 | else |
| 1337 | { |
| 1338 | if (attach_info.GetIgnoreExisting()) |
| 1339 | packet.PutCString("vAttachWait"); |
| 1340 | else |
| 1341 | packet.PutCString ("vAttachOrWait"); |
| 1342 | } |
| 1343 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1344 | else |
| 1345 | packet.PutCString("vAttachName"); |
| 1346 | packet.PutChar(';'); |
Bruce Mitchener | 9ccb970 | 2015-11-07 04:40:13 +0000 | [diff] [blame] | 1347 | packet.PutBytesAsRawHex8(process_name, strlen(process_name), endian::InlHostByteOrder(), endian::InlHostByteOrder()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1348 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1349 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize())); |
| 1350 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1351 | } |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 1352 | else |
| 1353 | SetExitStatus (-1, error.AsCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1354 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1355 | return error; |
| 1356 | } |
| 1357 | |
Greg Clayton | 5df78fa | 2015-05-23 03:54:53 +0000 | [diff] [blame] | 1358 | void |
| 1359 | ProcessGDBRemote::DidExit () |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1360 | { |
Greg Clayton | 5df78fa | 2015-05-23 03:54:53 +0000 | [diff] [blame] | 1361 | // When we exit, disconnect from the GDB server communications |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1362 | m_gdb_comm.Disconnect(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1365 | void |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1366 | ProcessGDBRemote::DidAttach (ArchSpec &process_arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1367 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1368 | // If you can figure out what the architecture is, fill it in here. |
| 1369 | process_arch.Clear(); |
| 1370 | DidLaunchOrAttach (process_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1374 | Error |
| 1375 | ProcessGDBRemote::WillResume () |
| 1376 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1377 | m_continue_c_tids.clear(); |
| 1378 | m_continue_C_tids.clear(); |
| 1379 | m_continue_s_tids.clear(); |
| 1380 | m_continue_S_tids.clear(); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1381 | m_jstopinfo_sp.reset(); |
| 1382 | m_jthreadsinfo_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1383 | return Error(); |
| 1384 | } |
| 1385 | |
| 1386 | Error |
| 1387 | ProcessGDBRemote::DoResume () |
| 1388 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1389 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1390 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1391 | if (log) |
| 1392 | log->Printf ("ProcessGDBRemote::Resume()"); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1393 | |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 1394 | ListenerSP listener_sp (Listener::MakeListener("gdb-remote.resume-packet-sent")); |
| 1395 | if (listener_sp->StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1396 | { |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 1397 | listener_sp->StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1398 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1399 | const size_t num_threads = GetThreadList().GetSize(); |
| 1400 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1401 | StreamString continue_packet; |
| 1402 | bool continue_packet_error = false; |
| 1403 | if (m_gdb_comm.HasAnyVContSupport ()) |
| 1404 | { |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1405 | if (!GetTarget().GetNonStopModeEnabled() && |
| 1406 | (m_continue_c_tids.size() == num_threads || |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 1407 | (m_continue_c_tids.empty() && |
| 1408 | m_continue_C_tids.empty() && |
| 1409 | m_continue_s_tids.empty() && |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1410 | m_continue_S_tids.empty()))) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1411 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1412 | // All threads are continuing, just send a "c" packet |
| 1413 | continue_packet.PutCString ("c"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1414 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1415 | else |
| 1416 | { |
| 1417 | continue_packet.PutCString ("vCont"); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1418 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1419 | if (!m_continue_c_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1420 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1421 | if (m_gdb_comm.GetVContSupported ('c')) |
| 1422 | { |
| 1423 | for (tid_collection::const_iterator t_pos = m_continue_c_tids.begin(), t_end = m_continue_c_tids.end(); t_pos != t_end; ++t_pos) |
| 1424 | continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); |
| 1425 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1426 | else |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1427 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1428 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1429 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1430 | if (!continue_packet_error && !m_continue_C_tids.empty()) |
| 1431 | { |
| 1432 | if (m_gdb_comm.GetVContSupported ('C')) |
| 1433 | { |
| 1434 | for (tid_sig_collection::const_iterator s_pos = m_continue_C_tids.begin(), s_end = m_continue_C_tids.end(); s_pos != s_end; ++s_pos) |
| 1435 | continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1436 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1437 | else |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1438 | continue_packet_error = true; |
| 1439 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1440 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1441 | if (!continue_packet_error && !m_continue_s_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1442 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1443 | if (m_gdb_comm.GetVContSupported ('s')) |
| 1444 | { |
| 1445 | for (tid_collection::const_iterator t_pos = m_continue_s_tids.begin(), t_end = m_continue_s_tids.end(); t_pos != t_end; ++t_pos) |
| 1446 | continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); |
| 1447 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1448 | else |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1449 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1450 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1451 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1452 | if (!continue_packet_error && !m_continue_S_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1453 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1454 | if (m_gdb_comm.GetVContSupported ('S')) |
| 1455 | { |
| 1456 | for (tid_sig_collection::const_iterator s_pos = m_continue_S_tids.begin(), s_end = m_continue_S_tids.end(); s_pos != s_end; ++s_pos) |
| 1457 | continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1458 | } |
| 1459 | else |
| 1460 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1461 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1462 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1463 | if (continue_packet_error) |
| 1464 | continue_packet.GetString().clear(); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1465 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1466 | } |
| 1467 | else |
| 1468 | continue_packet_error = true; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1469 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1470 | if (continue_packet_error) |
| 1471 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1472 | // Either no vCont support, or we tried to use part of the vCont |
| 1473 | // packet that wasn't supported by the remote GDB server. |
| 1474 | // We need to try and make a simple packet that can do our continue |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1475 | const size_t num_continue_c_tids = m_continue_c_tids.size(); |
| 1476 | const size_t num_continue_C_tids = m_continue_C_tids.size(); |
| 1477 | const size_t num_continue_s_tids = m_continue_s_tids.size(); |
| 1478 | const size_t num_continue_S_tids = m_continue_S_tids.size(); |
| 1479 | if (num_continue_c_tids > 0) |
| 1480 | { |
| 1481 | if (num_continue_c_tids == num_threads) |
| 1482 | { |
| 1483 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1484 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1485 | continue_packet.PutChar ('c'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1486 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1487 | } |
| 1488 | else if (num_continue_c_tids == 1 && |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1489 | num_continue_C_tids == 0 && |
| 1490 | num_continue_s_tids == 0 && |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1491 | num_continue_S_tids == 0 ) |
| 1492 | { |
| 1493 | // Only one thread is continuing |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1494 | m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1495 | continue_packet.PutChar ('c'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1496 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1500 | if (continue_packet_error && num_continue_C_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1501 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1502 | if ((num_continue_C_tids + num_continue_c_tids) == num_threads && |
| 1503 | num_continue_C_tids > 0 && |
| 1504 | num_continue_s_tids == 0 && |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1505 | num_continue_S_tids == 0 ) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1506 | { |
| 1507 | const int continue_signo = m_continue_C_tids.front().second; |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1508 | // Only one thread is continuing |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1509 | if (num_continue_C_tids > 1) |
| 1510 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1511 | // More that one thread with a signal, yet we don't have |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1512 | // vCont support and we are being asked to resume each |
| 1513 | // thread with a signal, we need to make sure they are |
| 1514 | // all the same signal, or we can't issue the continue |
| 1515 | // accurately with the current support... |
| 1516 | if (num_continue_C_tids > 1) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1517 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1518 | continue_packet_error = false; |
| 1519 | for (size_t i=1; i<m_continue_C_tids.size(); ++i) |
| 1520 | { |
| 1521 | if (m_continue_C_tids[i].second != continue_signo) |
| 1522 | continue_packet_error = true; |
| 1523 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1524 | } |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1525 | if (!continue_packet_error) |
| 1526 | m_gdb_comm.SetCurrentThreadForRun (-1); |
| 1527 | } |
| 1528 | else |
| 1529 | { |
| 1530 | // Set the continue thread ID |
| 1531 | continue_packet_error = false; |
| 1532 | m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1533 | } |
| 1534 | if (!continue_packet_error) |
| 1535 | { |
| 1536 | // Add threads continuing with the same signo... |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1537 | continue_packet.Printf("C%2.2x", continue_signo); |
| 1538 | } |
| 1539 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1542 | if (continue_packet_error && num_continue_s_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1543 | { |
| 1544 | if (num_continue_s_tids == num_threads) |
| 1545 | { |
| 1546 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1547 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Ewan Crawford | 76df288 | 2015-06-23 12:32:06 +0000 | [diff] [blame] | 1548 | |
| 1549 | // If in Non-Stop-Mode use vCont when stepping |
| 1550 | if (GetTarget().GetNonStopModeEnabled()) |
| 1551 | { |
| 1552 | if (m_gdb_comm.GetVContSupported('s')) |
| 1553 | continue_packet.PutCString("vCont;s"); |
| 1554 | else |
| 1555 | continue_packet.PutChar('s'); |
| 1556 | } |
| 1557 | else |
| 1558 | continue_packet.PutChar('s'); |
| 1559 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1560 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1561 | } |
| 1562 | else if (num_continue_c_tids == 0 && |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1563 | num_continue_C_tids == 0 && |
| 1564 | num_continue_s_tids == 1 && |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1565 | num_continue_S_tids == 0 ) |
| 1566 | { |
| 1567 | // Only one thread is stepping |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1568 | m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1569 | continue_packet.PutChar ('s'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1570 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | if (!continue_packet_error && num_continue_S_tids > 0) |
| 1575 | { |
| 1576 | if (num_continue_S_tids == num_threads) |
| 1577 | { |
| 1578 | const int step_signo = m_continue_S_tids.front().second; |
| 1579 | // Are all threads trying to step with the same signal? |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1580 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1581 | if (num_continue_S_tids > 1) |
| 1582 | { |
| 1583 | for (size_t i=1; i<num_threads; ++i) |
| 1584 | { |
| 1585 | if (m_continue_S_tids[i].second != step_signo) |
| 1586 | continue_packet_error = true; |
| 1587 | } |
| 1588 | } |
| 1589 | if (!continue_packet_error) |
| 1590 | { |
| 1591 | // Add threads stepping with the same signo... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1592 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1593 | continue_packet.Printf("S%2.2x", step_signo); |
| 1594 | } |
| 1595 | } |
| 1596 | else if (num_continue_c_tids == 0 && |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1597 | num_continue_C_tids == 0 && |
| 1598 | num_continue_s_tids == 0 && |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1599 | num_continue_S_tids == 1 ) |
| 1600 | { |
| 1601 | // Only one thread is stepping with signal |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1602 | m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1603 | continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1604 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (continue_packet_error) |
| 1610 | { |
| 1611 | error.SetErrorString ("can't make continue packet for this resume"); |
| 1612 | } |
| 1613 | else |
| 1614 | { |
| 1615 | EventSP event_sp; |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1616 | if (!m_async_thread.IsJoinable()) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1617 | { |
| 1618 | error.SetErrorString ("Trying to resume but the async thread is dead."); |
| 1619 | if (log) |
| 1620 | log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead."); |
| 1621 | return error; |
| 1622 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1623 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1624 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize())); |
| 1625 | |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 1626 | if (listener_sp->WaitForEvent(std::chrono::seconds(5), event_sp) == false) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1627 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1628 | error.SetErrorString("Resume timed out."); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1629 | if (log) |
| 1630 | log->Printf ("ProcessGDBRemote::DoResume: Resume timed out."); |
| 1631 | } |
| 1632 | else if (event_sp->BroadcasterIs (&m_async_broadcaster)) |
| 1633 | { |
| 1634 | error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1635 | if (log) |
| 1636 | log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1637 | return error; |
| 1638 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1639 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1642 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1645 | void |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1646 | ProcessGDBRemote::HandleStopReplySequence () |
| 1647 | { |
| 1648 | while(true) |
| 1649 | { |
| 1650 | // Send vStopped |
| 1651 | StringExtractorGDBRemote response; |
| 1652 | m_gdb_comm.SendPacketAndWaitForResponse("vStopped", response, false); |
| 1653 | |
| 1654 | // OK represents end of signal list |
| 1655 | if (response.IsOKResponse()) |
| 1656 | break; |
| 1657 | |
| 1658 | // If not OK or a normal packet we have a problem |
| 1659 | if (!response.IsNormalResponse()) |
| 1660 | break; |
| 1661 | |
| 1662 | SetLastStopPacket(response); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | void |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1667 | ProcessGDBRemote::ClearThreadIDList () |
| 1668 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 1669 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1670 | m_thread_ids.clear(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1671 | m_thread_pcs.clear(); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1674 | size_t |
| 1675 | ProcessGDBRemote::UpdateThreadIDsFromStopReplyThreadsValue (std::string &value) |
| 1676 | { |
| 1677 | m_thread_ids.clear(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1678 | m_thread_pcs.clear(); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1679 | size_t comma_pos; |
| 1680 | lldb::tid_t tid; |
| 1681 | while ((comma_pos = value.find(',')) != std::string::npos) |
| 1682 | { |
| 1683 | value[comma_pos] = '\0'; |
| 1684 | // thread in big endian hex |
| 1685 | tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1686 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1687 | m_thread_ids.push_back (tid); |
| 1688 | value.erase(0, comma_pos + 1); |
| 1689 | } |
| 1690 | tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1691 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1692 | m_thread_ids.push_back (tid); |
| 1693 | return m_thread_ids.size(); |
| 1694 | } |
| 1695 | |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1696 | size_t |
| 1697 | ProcessGDBRemote::UpdateThreadPCsFromStopReplyThreadsValue (std::string &value) |
| 1698 | { |
| 1699 | m_thread_pcs.clear(); |
| 1700 | size_t comma_pos; |
| 1701 | lldb::addr_t pc; |
| 1702 | while ((comma_pos = value.find(',')) != std::string::npos) |
| 1703 | { |
| 1704 | value[comma_pos] = '\0'; |
| 1705 | pc = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_ADDRESS, 16); |
| 1706 | if (pc != LLDB_INVALID_ADDRESS) |
| 1707 | m_thread_pcs.push_back (pc); |
| 1708 | value.erase(0, comma_pos + 1); |
| 1709 | } |
| 1710 | pc = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_ADDRESS, 16); |
| 1711 | if (pc != LLDB_INVALID_THREAD_ID) |
| 1712 | m_thread_pcs.push_back (pc); |
| 1713 | return m_thread_pcs.size(); |
| 1714 | } |
| 1715 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1716 | bool |
| 1717 | ProcessGDBRemote::UpdateThreadIDList () |
| 1718 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 1719 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1720 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1721 | if (m_jthreadsinfo_sp) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1722 | { |
| 1723 | // If we have the JSON threads info, we can get the thread list from that |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1724 | StructuredData::Array *thread_infos = m_jthreadsinfo_sp->GetAsArray(); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1725 | if (thread_infos && thread_infos->GetSize() > 0) |
| 1726 | { |
| 1727 | m_thread_ids.clear(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1728 | m_thread_pcs.clear(); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1729 | thread_infos->ForEach([this](StructuredData::Object* object) -> bool { |
| 1730 | StructuredData::Dictionary *thread_dict = object->GetAsDictionary(); |
| 1731 | if (thread_dict) |
| 1732 | { |
| 1733 | // Set the thread stop info from the JSON dictionary |
| 1734 | SetThreadStopInfo (thread_dict); |
| 1735 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
| 1736 | if (thread_dict->GetValueForKeyAsInteger<lldb::tid_t>("tid", tid)) |
| 1737 | m_thread_ids.push_back(tid); |
| 1738 | } |
| 1739 | return true; // Keep iterating through all thread_info objects |
| 1740 | }); |
| 1741 | } |
| 1742 | if (!m_thread_ids.empty()) |
| 1743 | return true; |
| 1744 | } |
| 1745 | else |
| 1746 | { |
| 1747 | // See if we can get the thread IDs from the current stop reply packets |
| 1748 | // that might contain a "threads" key/value pair |
| 1749 | |
| 1750 | // Lock the thread stack while we access it |
Greg Clayton | fc1e77a98 | 2015-11-03 22:42:04 +0000 | [diff] [blame] | 1751 | //Mutex::Locker stop_stack_lock(m_last_stop_packet_mutex); |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 1752 | std::unique_lock<std::recursive_mutex> stop_stack_lock(m_last_stop_packet_mutex, std::defer_lock); |
| 1753 | if (stop_stack_lock.try_lock()) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1754 | { |
Greg Clayton | fc1e77a98 | 2015-11-03 22:42:04 +0000 | [diff] [blame] | 1755 | // Get the number of stop packets on the stack |
| 1756 | int nItems = m_stop_packet_stack.size(); |
| 1757 | // Iterate over them |
| 1758 | for (int i = 0; i < nItems; i++) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1759 | { |
Greg Clayton | fc1e77a98 | 2015-11-03 22:42:04 +0000 | [diff] [blame] | 1760 | // Get the thread stop info |
| 1761 | StringExtractorGDBRemote &stop_info = m_stop_packet_stack[i]; |
| 1762 | const std::string &stop_info_str = stop_info.GetStringRef(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1763 | |
| 1764 | m_thread_pcs.clear(); |
| 1765 | const size_t thread_pcs_pos = stop_info_str.find(";thread-pcs:"); |
| 1766 | if (thread_pcs_pos != std::string::npos) |
| 1767 | { |
| 1768 | const size_t start = thread_pcs_pos + strlen(";thread-pcs:"); |
| 1769 | const size_t end = stop_info_str.find(';', start); |
| 1770 | if (end != std::string::npos) |
| 1771 | { |
| 1772 | std::string value = stop_info_str.substr(start, end - start); |
| 1773 | UpdateThreadPCsFromStopReplyThreadsValue(value); |
| 1774 | } |
| 1775 | } |
| 1776 | |
Greg Clayton | fc1e77a98 | 2015-11-03 22:42:04 +0000 | [diff] [blame] | 1777 | const size_t threads_pos = stop_info_str.find(";threads:"); |
| 1778 | if (threads_pos != std::string::npos) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1779 | { |
Greg Clayton | fc1e77a98 | 2015-11-03 22:42:04 +0000 | [diff] [blame] | 1780 | const size_t start = threads_pos + strlen(";threads:"); |
| 1781 | const size_t end = stop_info_str.find(';', start); |
| 1782 | if (end != std::string::npos) |
| 1783 | { |
| 1784 | std::string value = stop_info_str.substr(start, end - start); |
| 1785 | if (UpdateThreadIDsFromStopReplyThreadsValue(value)) |
| 1786 | return true; |
| 1787 | } |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | } |
| 1792 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1793 | bool sequence_mutex_unavailable = false; |
| 1794 | m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable); |
| 1795 | if (sequence_mutex_unavailable) |
| 1796 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1797 | return false; // We just didn't get the list |
| 1798 | } |
| 1799 | return true; |
| 1800 | } |
| 1801 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1802 | bool |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1803 | ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1804 | { |
| 1805 | // locker will keep a mutex locked until it goes out of scope |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1806 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 1807 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1808 | log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1809 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1810 | size_t num_thread_ids = m_thread_ids.size(); |
| 1811 | // The "m_thread_ids" thread ID list should always be updated after each stop |
| 1812 | // reply packet, but in case it isn't, update it here. |
| 1813 | if (num_thread_ids == 0) |
| 1814 | { |
| 1815 | if (!UpdateThreadIDList ()) |
| 1816 | return false; |
| 1817 | num_thread_ids = m_thread_ids.size(); |
| 1818 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1819 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1820 | ThreadList old_thread_list_copy(old_thread_list); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1821 | if (num_thread_ids > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1822 | { |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1823 | for (size_t i=0; i<num_thread_ids; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1824 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1825 | tid_t tid = m_thread_ids[i]; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1826 | ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1827 | if (!thread_sp) |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1828 | { |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1829 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1830 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1831 | log->Printf( |
| 1832 | "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1833 | __FUNCTION__, static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1834 | thread_sp->GetID()); |
| 1835 | } |
| 1836 | else |
| 1837 | { |
| 1838 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1839 | log->Printf( |
| 1840 | "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1841 | __FUNCTION__, static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1842 | thread_sp->GetID()); |
| 1843 | } |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1844 | // The m_thread_pcs vector has pc values in big-endian order, not target-endian, unlike most |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1845 | // of the register read/write packets in gdb-remote protocol. |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1846 | // Early in the process startup, we may not yet have set the process ByteOrder so we ignore these; |
| 1847 | // they are a performance improvement over fetching thread register values individually, the |
| 1848 | // method we will fall back to if needed. |
| 1849 | if (m_thread_ids.size() == m_thread_pcs.size() && thread_sp.get() && GetByteOrder() != eByteOrderInvalid) |
| 1850 | { |
| 1851 | ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); |
| 1852 | RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext()); |
| 1853 | if (reg_ctx_sp) |
| 1854 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1855 | uint32_t pc_regnum = reg_ctx_sp->ConvertRegisterKindToRegisterNumber |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 1856 | (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 1857 | if (pc_regnum != LLDB_INVALID_REGNUM) |
| 1858 | { |
| 1859 | gdb_thread->PrivateSetRegisterValue (pc_regnum, m_thread_pcs[i]); |
| 1860 | } |
| 1861 | } |
| 1862 | } |
Greg Clayton | 88f86b6 | 2016-06-10 23:23:34 +0000 | [diff] [blame] | 1863 | new_thread_list.AddThreadSortedByIndexID (thread_sp); |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1864 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1865 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1866 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1867 | // Whatever that is left in old_thread_list_copy are not |
| 1868 | // present in new_thread_list. Remove non-existent threads from internal id table. |
| 1869 | size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); |
| 1870 | for (size_t i=0; i<old_num_thread_ids; i++) |
| 1871 | { |
| 1872 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false)); |
| 1873 | if (old_thread_sp) |
| 1874 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1875 | lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1876 | m_thread_id_to_index_id_map.erase(old_thread_id); |
| 1877 | } |
| 1878 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 1879 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1880 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 1883 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1884 | bool |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1885 | ProcessGDBRemote::GetThreadStopInfoFromJSON (ThreadGDBRemote *thread, const StructuredData::ObjectSP &thread_infos_sp) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1886 | { |
| 1887 | // See if we got thread stop infos for all threads via the "jThreadsInfo" packet |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1888 | if (thread_infos_sp) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1889 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1890 | StructuredData::Array *thread_infos = thread_infos_sp->GetAsArray(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1891 | if (thread_infos) |
| 1892 | { |
| 1893 | lldb::tid_t tid; |
| 1894 | const size_t n = thread_infos->GetSize(); |
| 1895 | for (size_t i=0; i<n; ++i) |
| 1896 | { |
| 1897 | StructuredData::Dictionary *thread_dict = thread_infos->GetItemAtIndex(i)->GetAsDictionary(); |
| 1898 | if (thread_dict) |
| 1899 | { |
| 1900 | if (thread_dict->GetValueForKeyAsInteger<lldb::tid_t>("tid", tid, LLDB_INVALID_THREAD_ID)) |
| 1901 | { |
| 1902 | if (tid == thread->GetID()) |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1903 | return (bool)SetThreadStopInfo(thread_dict); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 1909 | return false; |
| 1910 | } |
| 1911 | |
| 1912 | bool |
| 1913 | ProcessGDBRemote::CalculateThreadStopInfo (ThreadGDBRemote *thread) |
| 1914 | { |
| 1915 | // See if we got thread stop infos for all threads via the "jThreadsInfo" packet |
| 1916 | if (GetThreadStopInfoFromJSON (thread, m_jthreadsinfo_sp)) |
| 1917 | return true; |
| 1918 | |
| 1919 | // See if we got thread stop info for any threads valid stop info reasons threads |
| 1920 | // via the "jstopinfo" packet stop reply packet key/value pair? |
| 1921 | if (m_jstopinfo_sp) |
| 1922 | { |
| 1923 | // If we have "jstopinfo" then we have stop descriptions for all threads |
| 1924 | // that have stop reasons, and if there is no entry for a thread, then |
| 1925 | // it has no stop reason. |
| 1926 | thread->GetRegisterContext()->InvalidateIfNeeded(true); |
| 1927 | if (!GetThreadStopInfoFromJSON (thread, m_jstopinfo_sp)) |
| 1928 | { |
| 1929 | thread->SetStopInfo (StopInfoSP()); |
| 1930 | } |
| 1931 | return true; |
| 1932 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Fall back to using the qThreadStopInfo packet |
| 1935 | StringExtractorGDBRemote stop_packet; |
| 1936 | if (GetGDBRemote().GetThreadStopInfo(thread->GetProtocolID(), stop_packet)) |
| 1937 | return SetThreadStopInfo (stop_packet) == eStateStopped; |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 1941 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1942 | ThreadSP |
| 1943 | ProcessGDBRemote::SetThreadStopInfo (lldb::tid_t tid, |
| 1944 | ExpeditedRegisterMap &expedited_register_map, |
| 1945 | uint8_t signo, |
| 1946 | const std::string &thread_name, |
| 1947 | const std::string &reason, |
| 1948 | const std::string &description, |
| 1949 | uint32_t exc_type, |
| 1950 | const std::vector<addr_t> &exc_data, |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1951 | addr_t thread_dispatch_qaddr, |
| 1952 | bool queue_vars_valid, // Set to true if queue_name, queue_kind and queue_serial are valid |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 1953 | LazyBool associated_with_dispatch_queue, |
| 1954 | addr_t dispatch_queue_t, |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1955 | std::string &queue_name, |
| 1956 | QueueKind queue_kind, |
| 1957 | uint64_t queue_serial) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1958 | { |
| 1959 | ThreadSP thread_sp; |
| 1960 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1961 | { |
| 1962 | // Scope for "locker" below |
| 1963 | { |
| 1964 | // m_thread_list_real does have its own mutex, but we need to |
| 1965 | // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...) |
| 1966 | // and the m_thread_list_real.AddThread(...) so it doesn't change on us |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 1967 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1968 | thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); |
| 1969 | |
| 1970 | if (!thread_sp) |
| 1971 | { |
| 1972 | // Create the thread if we need to |
| 1973 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
| 1974 | m_thread_list_real.AddThread(thread_sp); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | if (thread_sp) |
| 1979 | { |
| 1980 | ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); |
| 1981 | gdb_thread->GetRegisterContext()->InvalidateIfNeeded(true); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1982 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1983 | for (const auto &pair : expedited_register_map) |
| 1984 | { |
| 1985 | StringExtractor reg_value_extractor; |
| 1986 | reg_value_extractor.GetStringRef() = pair.second; |
Pavel Labath | b42b48e | 2016-08-19 12:31:49 +0000 | [diff] [blame] | 1987 | DataBufferSP buffer_sp(new DataBufferHeap(reg_value_extractor.GetStringRef().size() / 2, 0)); |
Zachary Turner | d08f09c | 2016-08-30 18:12:11 +0000 | [diff] [blame] | 1988 | reg_value_extractor.GetHexBytes(buffer_sp->GetData(), '\xcc'); |
| 1989 | gdb_thread->PrivateSetRegisterValue(pair.first, buffer_sp->GetData()); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 1992 | thread_sp->SetName (thread_name.empty() ? NULL : thread_name.c_str()); |
| 1993 | |
| 1994 | gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1995 | // Check if the GDB server was able to provide the queue name, kind and serial number |
| 1996 | if (queue_vars_valid) |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 1997 | gdb_thread->SetQueueInfo(std::move(queue_name), queue_kind, queue_serial, dispatch_queue_t, associated_with_dispatch_queue); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 1998 | else |
| 1999 | gdb_thread->ClearQueueInfo(); |
| 2000 | |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2001 | gdb_thread->SetAssociatedWithLibdispatchQueue (associated_with_dispatch_queue); |
| 2002 | |
| 2003 | if (dispatch_queue_t != LLDB_INVALID_ADDRESS) |
| 2004 | gdb_thread->SetQueueLibdispatchQueueAddress (dispatch_queue_t); |
| 2005 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2006 | // Make sure we update our thread stop reason just once |
| 2007 | if (!thread_sp->StopInfoIsUpToDate()) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2008 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2009 | thread_sp->SetStopInfo (StopInfoSP()); |
Ryan Brown | 65d4d5c | 2015-09-16 21:20:44 +0000 | [diff] [blame] | 2010 | // If there's a memory thread backed by this thread, we need to use it to calcualte StopInfo. |
| 2011 | ThreadSP memory_thread_sp = m_thread_list.FindThreadByProtocolID(thread_sp->GetProtocolID()); |
| 2012 | if (memory_thread_sp) |
| 2013 | thread_sp = memory_thread_sp; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2014 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2015 | if (exc_type != 0) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2016 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2017 | const size_t exc_data_size = exc_data.size(); |
| 2018 | |
| 2019 | thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp, |
| 2020 | exc_type, |
| 2021 | exc_data_size, |
| 2022 | exc_data_size >= 1 ? exc_data[0] : 0, |
| 2023 | exc_data_size >= 2 ? exc_data[1] : 0, |
| 2024 | exc_data_size >= 3 ? exc_data[2] : 0)); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2025 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2026 | else |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2027 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2028 | bool handled = false; |
| 2029 | bool did_exec = false; |
| 2030 | if (!reason.empty()) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2031 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2032 | if (reason.compare("trace") == 0) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2033 | { |
Pavel Labath | ef40912 | 2016-04-07 08:16:10 +0000 | [diff] [blame] | 2034 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 2035 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
| 2036 | |
| 2037 | // If the current pc is a breakpoint site then the StopInfo should be set to Breakpoint |
| 2038 | // Otherwise, it will be set to Trace. |
| 2039 | if (bp_site_sp && bp_site_sp->ValidForThisThread(thread_sp.get())) |
| 2040 | { |
| 2041 | thread_sp->SetStopInfo( |
| 2042 | StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp, bp_site_sp->GetID())); |
| 2043 | } |
| 2044 | else |
| 2045 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2046 | handled = true; |
| 2047 | } |
| 2048 | else if (reason.compare("breakpoint") == 0) |
| 2049 | { |
Pavel Labath | ef40912 | 2016-04-07 08:16:10 +0000 | [diff] [blame] | 2050 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 2051 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
| 2052 | if (bp_site_sp) |
| 2053 | { |
| 2054 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 2055 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 2056 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
| 2057 | handled = true; |
| 2058 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
| 2059 | { |
| 2060 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
| 2061 | } |
| 2062 | else |
| 2063 | { |
| 2064 | StopInfoSP invalid_stop_info_sp; |
| 2065 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
| 2066 | } |
| 2067 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2068 | } |
| 2069 | else if (reason.compare("trap") == 0) |
| 2070 | { |
| 2071 | // Let the trap just use the standard signal stop reason below... |
| 2072 | } |
| 2073 | else if (reason.compare("watchpoint") == 0) |
| 2074 | { |
| 2075 | StringExtractor desc_extractor(description.c_str()); |
| 2076 | addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); |
| 2077 | uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32); |
Jaydeep Patil | 8314350 | 2015-08-13 03:44:09 +0000 | [diff] [blame] | 2078 | addr_t wp_hit_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2079 | watch_id_t watch_id = LLDB_INVALID_WATCH_ID; |
| 2080 | if (wp_addr != LLDB_INVALID_ADDRESS) |
| 2081 | { |
Mohit K. Bhakkad | 6846bc8 | 2015-12-02 17:45:02 +0000 | [diff] [blame] | 2082 | WatchpointSP wp_sp; |
| 2083 | ArchSpec::Core core = GetTarget().GetArchitecture().GetCore(); |
Omair Javaid | 43507f5 | 2016-06-16 16:41:22 +0000 | [diff] [blame] | 2084 | if ((core >= ArchSpec::kCore_mips_first && core <= ArchSpec::kCore_mips_last) || |
Omair Javaid | c6dc90e | 2016-06-27 11:18:23 +0000 | [diff] [blame] | 2085 | (core >= ArchSpec::eCore_arm_generic && core <= ArchSpec::eCore_arm_aarch64)) |
Mohit K. Bhakkad | 6846bc8 | 2015-12-02 17:45:02 +0000 | [diff] [blame] | 2086 | wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_hit_addr); |
| 2087 | if (!wp_sp) |
| 2088 | wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_addr); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2089 | if (wp_sp) |
| 2090 | { |
| 2091 | wp_sp->SetHardwareIndex(wp_index); |
| 2092 | watch_id = wp_sp->GetID(); |
| 2093 | } |
| 2094 | } |
| 2095 | if (watch_id == LLDB_INVALID_WATCH_ID) |
| 2096 | { |
| 2097 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_WATCHPOINTS)); |
| 2098 | if (log) log->Printf ("failed to find watchpoint"); |
| 2099 | } |
Jaydeep Patil | 8314350 | 2015-08-13 03:44:09 +0000 | [diff] [blame] | 2100 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id, wp_hit_addr)); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2101 | handled = true; |
| 2102 | } |
| 2103 | else if (reason.compare("exception") == 0) |
| 2104 | { |
| 2105 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str())); |
| 2106 | handled = true; |
| 2107 | } |
| 2108 | else if (reason.compare("exec") == 0) |
| 2109 | { |
| 2110 | did_exec = true; |
| 2111 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp)); |
| 2112 | handled = true; |
| 2113 | } |
| 2114 | } |
Pavel Labath | 3ce324a | 2016-04-06 09:10:29 +0000 | [diff] [blame] | 2115 | else if (!signo) |
| 2116 | { |
Pavel Labath | ef40912 | 2016-04-07 08:16:10 +0000 | [diff] [blame] | 2117 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 2118 | lldb::BreakpointSiteSP bp_site_sp = |
| 2119 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
| 2120 | |
Pavel Labath | 3ce324a | 2016-04-06 09:10:29 +0000 | [diff] [blame] | 2121 | // If the current pc is a breakpoint site then the StopInfo should be set to Breakpoint |
| 2122 | // even though the remote stub did not set it as such. This can happen when |
| 2123 | // the thread is involuntarily interrupted (e.g. due to stops on other |
| 2124 | // threads) just as it is about to execute the breakpoint instruction. |
Pavel Labath | ef40912 | 2016-04-07 08:16:10 +0000 | [diff] [blame] | 2125 | if (bp_site_sp && bp_site_sp->ValidForThisThread(thread_sp.get())) |
| 2126 | { |
| 2127 | thread_sp->SetStopInfo( |
| 2128 | StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp, bp_site_sp->GetID())); |
Pavel Labath | 3ce324a | 2016-04-06 09:10:29 +0000 | [diff] [blame] | 2129 | handled = true; |
Pavel Labath | ef40912 | 2016-04-07 08:16:10 +0000 | [diff] [blame] | 2130 | } |
Pavel Labath | 3ce324a | 2016-04-06 09:10:29 +0000 | [diff] [blame] | 2131 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2132 | |
| 2133 | if (!handled && signo && did_exec == false) |
| 2134 | { |
| 2135 | if (signo == SIGTRAP) |
| 2136 | { |
| 2137 | // Currently we are going to assume SIGTRAP means we are either |
| 2138 | // hitting a breakpoint or hardware single stepping. |
| 2139 | handled = true; |
| 2140 | addr_t pc = thread_sp->GetRegisterContext()->GetPC() + m_breakpoint_pc_offset; |
| 2141 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
| 2142 | |
| 2143 | if (bp_site_sp) |
| 2144 | { |
| 2145 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 2146 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 2147 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
| 2148 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
| 2149 | { |
| 2150 | if(m_breakpoint_pc_offset != 0) |
| 2151 | thread_sp->GetRegisterContext()->SetPC(pc); |
| 2152 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
| 2153 | } |
| 2154 | else |
| 2155 | { |
| 2156 | StopInfoSP invalid_stop_info_sp; |
| 2157 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
| 2158 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2159 | } |
| 2160 | else |
| 2161 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2162 | // If we were stepping then assume the stop was the result of the trace. If we were |
| 2163 | // not stepping then report the SIGTRAP. |
| 2164 | // FIXME: We are still missing the case where we single step over a trap instruction. |
| 2165 | if (thread_sp->GetTemporaryResumeState() == eStateStepping) |
| 2166 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
| 2167 | else |
| 2168 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo, description.c_str())); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2171 | if (!handled) |
| 2172 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo, description.c_str())); |
| 2173 | } |
| 2174 | |
| 2175 | if (!description.empty()) |
| 2176 | { |
| 2177 | lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 2178 | if (stop_info_sp) |
| 2179 | { |
| 2180 | const char *stop_info_desc = stop_info_sp->GetDescription(); |
| 2181 | if (!stop_info_desc || !stop_info_desc[0]) |
| 2182 | stop_info_sp->SetDescription (description.c_str()); |
| 2183 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2184 | else |
| 2185 | { |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2186 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str())); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2187 | } |
| 2188 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | } |
| 2192 | } |
| 2193 | return thread_sp; |
| 2194 | } |
| 2195 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2196 | lldb::ThreadSP |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2197 | ProcessGDBRemote::SetThreadStopInfo (StructuredData::Dictionary *thread_dict) |
| 2198 | { |
| 2199 | static ConstString g_key_tid("tid"); |
| 2200 | static ConstString g_key_name("name"); |
| 2201 | static ConstString g_key_reason("reason"); |
| 2202 | static ConstString g_key_metype("metype"); |
| 2203 | static ConstString g_key_medata("medata"); |
| 2204 | static ConstString g_key_qaddr("qaddr"); |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2205 | static ConstString g_key_dispatch_queue_t("dispatch_queue_t"); |
| 2206 | static ConstString g_key_associated_with_dispatch_queue("associated_with_dispatch_queue"); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2207 | static ConstString g_key_queue_name("qname"); |
| 2208 | static ConstString g_key_queue_kind("qkind"); |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2209 | static ConstString g_key_queue_serial_number("qserialnum"); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2210 | static ConstString g_key_registers("registers"); |
| 2211 | static ConstString g_key_memory("memory"); |
| 2212 | static ConstString g_key_address("address"); |
| 2213 | static ConstString g_key_bytes("bytes"); |
| 2214 | static ConstString g_key_description("description"); |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 2215 | static ConstString g_key_signal("signal"); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2216 | |
| 2217 | // Stop with signal and thread info |
| 2218 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
| 2219 | uint8_t signo = 0; |
| 2220 | std::string value; |
| 2221 | std::string thread_name; |
| 2222 | std::string reason; |
| 2223 | std::string description; |
| 2224 | uint32_t exc_type = 0; |
| 2225 | std::vector<addr_t> exc_data; |
| 2226 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
| 2227 | ExpeditedRegisterMap expedited_register_map; |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2228 | bool queue_vars_valid = false; |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2229 | addr_t dispatch_queue_t = LLDB_INVALID_ADDRESS; |
| 2230 | LazyBool associated_with_dispatch_queue = eLazyBoolCalculate; |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2231 | std::string queue_name; |
| 2232 | QueueKind queue_kind = eQueueKindUnknown; |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2233 | uint64_t queue_serial_number = 0; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2234 | // Iterate through all of the thread dictionary key/value pairs from the structured data dictionary |
| 2235 | |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2236 | thread_dict->ForEach([this, |
| 2237 | &tid, |
| 2238 | &expedited_register_map, |
| 2239 | &thread_name, |
| 2240 | &signo, |
| 2241 | &reason, |
| 2242 | &description, |
| 2243 | &exc_type, |
| 2244 | &exc_data, |
| 2245 | &thread_dispatch_qaddr, |
| 2246 | &queue_vars_valid, |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2247 | &associated_with_dispatch_queue, |
| 2248 | &dispatch_queue_t, |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2249 | &queue_name, |
| 2250 | &queue_kind, |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2251 | &queue_serial_number] |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2252 | (ConstString key, StructuredData::Object* object) -> bool |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2253 | { |
| 2254 | if (key == g_key_tid) |
| 2255 | { |
| 2256 | // thread in big endian hex |
| 2257 | tid = object->GetIntegerValue(LLDB_INVALID_THREAD_ID); |
| 2258 | } |
| 2259 | else if (key == g_key_metype) |
| 2260 | { |
| 2261 | // exception type in big endian hex |
| 2262 | exc_type = object->GetIntegerValue(0); |
| 2263 | } |
| 2264 | else if (key == g_key_medata) |
| 2265 | { |
| 2266 | // exception data in big endian hex |
| 2267 | StructuredData::Array *array = object->GetAsArray(); |
| 2268 | if (array) |
| 2269 | { |
| 2270 | array->ForEach([&exc_data](StructuredData::Object* object) -> bool { |
| 2271 | exc_data.push_back(object->GetIntegerValue()); |
| 2272 | return true; // Keep iterating through all array items |
| 2273 | }); |
| 2274 | } |
| 2275 | } |
| 2276 | else if (key == g_key_name) |
| 2277 | { |
Pavel Labath | 3a29f8b | 2015-07-28 09:18:32 +0000 | [diff] [blame] | 2278 | thread_name = object->GetStringValue(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2279 | } |
| 2280 | else if (key == g_key_qaddr) |
| 2281 | { |
| 2282 | thread_dispatch_qaddr = object->GetIntegerValue(LLDB_INVALID_ADDRESS); |
| 2283 | } |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2284 | else if (key == g_key_queue_name) |
| 2285 | { |
| 2286 | queue_vars_valid = true; |
Pavel Labath | 3a29f8b | 2015-07-28 09:18:32 +0000 | [diff] [blame] | 2287 | queue_name = object->GetStringValue(); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2288 | } |
| 2289 | else if (key == g_key_queue_kind) |
| 2290 | { |
| 2291 | std::string queue_kind_str = object->GetStringValue(); |
| 2292 | if (queue_kind_str == "serial") |
| 2293 | { |
| 2294 | queue_vars_valid = true; |
| 2295 | queue_kind = eQueueKindSerial; |
| 2296 | } |
| 2297 | else if (queue_kind_str == "concurrent") |
| 2298 | { |
| 2299 | queue_vars_valid = true; |
| 2300 | queue_kind = eQueueKindConcurrent; |
| 2301 | } |
| 2302 | } |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2303 | else if (key == g_key_queue_serial_number) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2304 | { |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2305 | queue_serial_number = object->GetIntegerValue(0); |
| 2306 | if (queue_serial_number != 0) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2307 | queue_vars_valid = true; |
| 2308 | } |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2309 | else if (key == g_key_dispatch_queue_t) |
| 2310 | { |
| 2311 | dispatch_queue_t = object->GetIntegerValue(0); |
| 2312 | if (dispatch_queue_t != 0 && dispatch_queue_t != LLDB_INVALID_ADDRESS) |
| 2313 | queue_vars_valid = true; |
| 2314 | } |
| 2315 | else if (key == g_key_associated_with_dispatch_queue) |
| 2316 | { |
| 2317 | queue_vars_valid = true; |
| 2318 | bool associated = object->GetBooleanValue (); |
| 2319 | if (associated) |
| 2320 | associated_with_dispatch_queue = eLazyBoolYes; |
| 2321 | else |
| 2322 | associated_with_dispatch_queue = eLazyBoolNo; |
| 2323 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2324 | else if (key == g_key_reason) |
| 2325 | { |
Pavel Labath | 3a29f8b | 2015-07-28 09:18:32 +0000 | [diff] [blame] | 2326 | reason = object->GetStringValue(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2327 | } |
| 2328 | else if (key == g_key_description) |
| 2329 | { |
Pavel Labath | 3a29f8b | 2015-07-28 09:18:32 +0000 | [diff] [blame] | 2330 | description = object->GetStringValue(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2331 | } |
| 2332 | else if (key == g_key_registers) |
| 2333 | { |
| 2334 | StructuredData::Dictionary *registers_dict = object->GetAsDictionary(); |
| 2335 | |
| 2336 | if (registers_dict) |
| 2337 | { |
| 2338 | registers_dict->ForEach([&expedited_register_map](ConstString key, StructuredData::Object* object) -> bool { |
| 2339 | const uint32_t reg = StringConvert::ToUInt32 (key.GetCString(), UINT32_MAX, 10); |
| 2340 | if (reg != UINT32_MAX) |
Pavel Labath | 3a29f8b | 2015-07-28 09:18:32 +0000 | [diff] [blame] | 2341 | expedited_register_map[reg] = object->GetStringValue(); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2342 | return true; // Keep iterating through all array items |
| 2343 | }); |
| 2344 | } |
| 2345 | } |
| 2346 | else if (key == g_key_memory) |
| 2347 | { |
| 2348 | StructuredData::Array *array = object->GetAsArray(); |
| 2349 | if (array) |
| 2350 | { |
| 2351 | array->ForEach([this](StructuredData::Object* object) -> bool { |
| 2352 | StructuredData::Dictionary *mem_cache_dict = object->GetAsDictionary(); |
| 2353 | if (mem_cache_dict) |
| 2354 | { |
| 2355 | lldb::addr_t mem_cache_addr = LLDB_INVALID_ADDRESS; |
| 2356 | if (mem_cache_dict->GetValueForKeyAsInteger<lldb::addr_t>("address", mem_cache_addr)) |
| 2357 | { |
| 2358 | if (mem_cache_addr != LLDB_INVALID_ADDRESS) |
| 2359 | { |
| 2360 | StringExtractor bytes; |
| 2361 | if (mem_cache_dict->GetValueForKeyAsString("bytes", bytes.GetStringRef())) |
| 2362 | { |
| 2363 | bytes.SetFilePos(0); |
| 2364 | |
| 2365 | const size_t byte_size = bytes.GetStringRef().size()/2; |
| 2366 | DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0)); |
Zachary Turner | d08f09c | 2016-08-30 18:12:11 +0000 | [diff] [blame] | 2367 | const size_t bytes_copied = bytes.GetHexBytes (data_buffer_sp->GetData(), 0); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2368 | if (bytes_copied == byte_size) |
| 2369 | m_memory_cache.AddL1CacheData(mem_cache_addr, data_buffer_sp); |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | return true; // Keep iterating through all array items |
| 2375 | }); |
| 2376 | } |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 2377 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2378 | } |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 2379 | else if (key == g_key_signal) |
| 2380 | signo = object->GetIntegerValue(LLDB_INVALID_SIGNAL_NUMBER); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2381 | return true; // Keep iterating through all dictionary key/value pairs |
| 2382 | }); |
| 2383 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2384 | return SetThreadStopInfo (tid, |
| 2385 | expedited_register_map, |
| 2386 | signo, |
| 2387 | thread_name, |
| 2388 | reason, |
| 2389 | description, |
| 2390 | exc_type, |
| 2391 | exc_data, |
| 2392 | thread_dispatch_qaddr, |
| 2393 | queue_vars_valid, |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2394 | associated_with_dispatch_queue, |
| 2395 | dispatch_queue_t, |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2396 | queue_name, |
| 2397 | queue_kind, |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2398 | queue_serial_number); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2399 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2400 | |
| 2401 | StateType |
| 2402 | ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet) |
| 2403 | { |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 2404 | stop_packet.SetFilePos (0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2405 | const char stop_type = stop_packet.GetChar(); |
| 2406 | switch (stop_type) |
| 2407 | { |
| 2408 | case 'T': |
| 2409 | case 'S': |
| 2410 | { |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 2411 | // This is a bit of a hack, but is is required. If we did exec, we |
| 2412 | // need to clear our thread lists and also know to rebuild our dynamic |
| 2413 | // register info before we lookup and threads and populate the expedited |
| 2414 | // register values so we need to know this right away so we can cleanup |
| 2415 | // and update our registers. |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2416 | const uint32_t stop_id = GetStopID(); |
| 2417 | if (stop_id == 0) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 2418 | { |
| 2419 | // Our first stop, make sure we have a process ID, and also make |
| 2420 | // sure we know about our registers |
| 2421 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 2422 | { |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 2423 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 2424 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 2425 | SetID (pid); |
| 2426 | } |
| 2427 | BuildDynamicRegisterInfo (true); |
| 2428 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2429 | // Stop with signal and thread info |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2430 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2431 | const uint8_t signo = stop_packet.GetHexU8(); |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2432 | llvm::StringRef key; |
| 2433 | llvm::StringRef value; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2434 | std::string thread_name; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2435 | std::string reason; |
| 2436 | std::string description; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2437 | uint32_t exc_type = 0; |
Greg Clayton | 896dff6 | 2010-07-23 16:45:51 +0000 | [diff] [blame] | 2438 | std::vector<addr_t> exc_data; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2439 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2440 | bool queue_vars_valid = false; // says if locals below that start with "queue_" are valid |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2441 | addr_t dispatch_queue_t = LLDB_INVALID_ADDRESS; |
| 2442 | LazyBool associated_with_dispatch_queue = eLazyBoolCalculate; |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2443 | std::string queue_name; |
| 2444 | QueueKind queue_kind = eQueueKindUnknown; |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2445 | uint64_t queue_serial_number = 0; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2446 | ExpeditedRegisterMap expedited_register_map; |
| 2447 | while (stop_packet.GetNameColonValue(key, value)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2448 | { |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2449 | if (key.compare("metype") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2450 | { |
| 2451 | // exception type in big endian hex |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2452 | value.getAsInteger(16, exc_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2453 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2454 | else if (key.compare("medata") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2455 | { |
| 2456 | // exception data in big endian hex |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2457 | uint64_t x; |
| 2458 | value.getAsInteger(16, x); |
| 2459 | exc_data.push_back(x); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2460 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2461 | else if (key.compare("thread") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2462 | { |
| 2463 | // thread in big endian hex |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2464 | if (value.getAsInteger(16, tid)) |
| 2465 | tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2466 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2467 | else if (key.compare("threads") == 0) |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2468 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 2469 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
| 2470 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2471 | m_thread_ids.clear(); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 2472 | // A comma separated list of all threads in the current |
| 2473 | // process that includes the thread for this stop reply |
| 2474 | // packet |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2475 | lldb::tid_t tid; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2476 | while (!value.empty()) |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2477 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2478 | llvm::StringRef tid_str; |
| 2479 | std::tie(tid_str, value) = value.split(','); |
| 2480 | if (tid_str.getAsInteger(16, tid)) |
| 2481 | tid = LLDB_INVALID_THREAD_ID; |
| 2482 | m_thread_ids.push_back(tid); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2483 | } |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2484 | } |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 2485 | else if (key.compare("thread-pcs") == 0) |
| 2486 | { |
| 2487 | m_thread_pcs.clear(); |
| 2488 | // A comma separated list of all threads in the current |
| 2489 | // process that includes the thread for this stop reply |
| 2490 | // packet |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 2491 | lldb::addr_t pc; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2492 | while (!value.empty()) |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 2493 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2494 | llvm::StringRef pc_str; |
| 2495 | std::tie(pc_str, value) = value.split(','); |
| 2496 | if (pc_str.getAsInteger(16, pc)) |
| 2497 | pc = LLDB_INVALID_ADDRESS; |
| 2498 | m_thread_pcs.push_back(pc); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 2499 | } |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 2500 | } |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2501 | else if (key.compare("jstopinfo") == 0) |
Greg Clayton | a5801ad | 2015-07-15 22:59:03 +0000 | [diff] [blame] | 2502 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2503 | StringExtractor json_extractor(value); |
| 2504 | std::string json; |
Greg Clayton | a5801ad | 2015-07-15 22:59:03 +0000 | [diff] [blame] | 2505 | // Now convert the HEX bytes into a string value |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2506 | json_extractor.GetHexByteString(json); |
Greg Clayton | a5801ad | 2015-07-15 22:59:03 +0000 | [diff] [blame] | 2507 | |
| 2508 | // This JSON contains thread IDs and thread stop info for all threads. |
| 2509 | // It doesn't contain expedited registers, memory or queue info. |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2510 | m_jstopinfo_sp = StructuredData::ParseJSON(json); |
Greg Clayton | a5801ad | 2015-07-15 22:59:03 +0000 | [diff] [blame] | 2511 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2512 | else if (key.compare("hexname") == 0) |
Greg Clayton | de9d049 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 2513 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2514 | StringExtractor name_extractor(value); |
| 2515 | std::string name; |
Greg Clayton | de9d049 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 2516 | // Now convert the HEX bytes into a string value |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2517 | name_extractor.GetHexByteString(thread_name); |
Greg Clayton | de9d049 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 2518 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2519 | else if (key.compare("name") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2520 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2521 | thread_name = value; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2522 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2523 | else if (key.compare("qaddr") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2524 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2525 | value.getAsInteger(16, thread_dispatch_qaddr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2526 | } |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2527 | else if (key.compare("dispatch_queue_t") == 0) |
| 2528 | { |
| 2529 | queue_vars_valid = true; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2530 | value.getAsInteger(16, dispatch_queue_t); |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2531 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2532 | else if (key.compare("qname") == 0) |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2533 | { |
| 2534 | queue_vars_valid = true; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2535 | StringExtractor name_extractor(value); |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2536 | // Now convert the HEX bytes into a string value |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2537 | name_extractor.GetHexByteString(queue_name); |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2538 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2539 | else if (key.compare("qkind") == 0) |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2540 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2541 | queue_kind = llvm::StringSwitch<QueueKind>(value) |
| 2542 | .Case("serial", eQueueKindSerial) |
| 2543 | .Case("concurrent", eQueueKindConcurrent) |
| 2544 | .Default(eQueueKindUnknown); |
| 2545 | queue_vars_valid = queue_kind != eQueueKindUnknown; |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2546 | } |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2547 | else if (key.compare("qserialnum") == 0) |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2548 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2549 | if (!value.getAsInteger(0, queue_serial_number)) |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2550 | queue_vars_valid = true; |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 2551 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2552 | else if (key.compare("reason") == 0) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2553 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2554 | reason = value; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2555 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2556 | else if (key.compare("description") == 0) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2557 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2558 | StringExtractor desc_extractor(value); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2559 | // Now convert the HEX bytes into a string value |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2560 | desc_extractor.GetHexByteString(description); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2561 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2562 | else if (key.compare("memory") == 0) |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 2563 | { |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2564 | // Expedited memory. GDB servers can choose to send back expedited memory |
| 2565 | // that can populate the L1 memory cache in the process so that things like |
| 2566 | // the frame pointer backchain can be expedited. This will help stack |
| 2567 | // backtracing be more efficient by not having to send as many memory read |
| 2568 | // requests down the remote GDB server. |
Chaoren Lin | 7a30608 | 2015-06-23 03:17:01 +0000 | [diff] [blame] | 2569 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2570 | // Key/value pair format: memory:<addr>=<bytes>; |
| 2571 | // <addr> is a number whose base will be interpreted by the prefix: |
| 2572 | // "0x[0-9a-fA-F]+" for hex |
| 2573 | // "0[0-7]+" for octal |
| 2574 | // "[1-9]+" for decimal |
| 2575 | // <bytes> is native endian ASCII hex bytes just like the register values |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2576 | llvm::StringRef addr_str, bytes_str; |
| 2577 | std::tie(addr_str, bytes_str) = value.split('='); |
| 2578 | if (!addr_str.empty() && !bytes_str.empty()) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2579 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2580 | lldb::addr_t mem_cache_addr = LLDB_INVALID_ADDRESS; |
| 2581 | if (!addr_str.getAsInteger(0, mem_cache_addr)) |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 2582 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2583 | StringExtractor bytes(bytes_str); |
| 2584 | const size_t byte_size = bytes.GetBytesLeft() / 2; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2585 | DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0)); |
Zachary Turner | d08f09c | 2016-08-30 18:12:11 +0000 | [diff] [blame] | 2586 | const size_t bytes_copied = bytes.GetHexBytes (data_buffer_sp->GetData(), 0); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2587 | if (bytes_copied == byte_size) |
| 2588 | m_memory_cache.AddL1CacheData(mem_cache_addr, data_buffer_sp); |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | } |
Jaydeep Patil | 725666c | 2015-08-13 03:46:01 +0000 | [diff] [blame] | 2592 | else if (key.compare("watch") == 0 || key.compare("rwatch") == 0 || key.compare("awatch") == 0) |
| 2593 | { |
| 2594 | // Support standard GDB remote stop reply packet 'TAAwatch:addr' |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2595 | lldb::addr_t wp_addr = LLDB_INVALID_ADDRESS; |
| 2596 | value.getAsInteger(16, wp_addr); |
| 2597 | |
Jaydeep Patil | 725666c | 2015-08-13 03:46:01 +0000 | [diff] [blame] | 2598 | WatchpointSP wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_addr); |
| 2599 | uint32_t wp_index = LLDB_INVALID_INDEX32; |
| 2600 | |
| 2601 | if (wp_sp) |
| 2602 | wp_index = wp_sp->GetHardwareIndex(); |
| 2603 | |
| 2604 | reason = "watchpoint"; |
| 2605 | StreamString ostr; |
| 2606 | ostr.Printf("%" PRIu64 " %" PRIu32, wp_addr, wp_index); |
| 2607 | description = ostr.GetString().c_str(); |
| 2608 | } |
Stephane Sezer | 2618e91 | 2015-08-21 16:51:56 +0000 | [diff] [blame] | 2609 | else if (key.compare("library") == 0) |
| 2610 | { |
| 2611 | LoadModules(); |
| 2612 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2613 | else if (key.size() == 2 && ::isxdigit(key[0]) && ::isxdigit(key[1])) |
| 2614 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 2615 | uint32_t reg = UINT32_MAX; |
| 2616 | if (!key.getAsInteger(16, reg)) |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2617 | expedited_register_map[reg] = std::move(value); |
| 2618 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2619 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2620 | |
Bhushan D. Attarde | 1e271df | 2015-07-24 04:06:20 +0000 | [diff] [blame] | 2621 | if (tid == LLDB_INVALID_THREAD_ID) |
| 2622 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2623 | // A thread id may be invalid if the response is old style 'S' packet which does not provide the |
Bhushan D. Attarde | 1e271df | 2015-07-24 04:06:20 +0000 | [diff] [blame] | 2624 | // thread information. So update the thread list and choose the first one. |
| 2625 | UpdateThreadIDList (); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2626 | |
Bhushan D. Attarde | 1e271df | 2015-07-24 04:06:20 +0000 | [diff] [blame] | 2627 | if (!m_thread_ids.empty ()) |
| 2628 | { |
| 2629 | tid = m_thread_ids.front (); |
| 2630 | } |
| 2631 | } |
| 2632 | |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2633 | ThreadSP thread_sp = SetThreadStopInfo (tid, |
| 2634 | expedited_register_map, |
| 2635 | signo, |
| 2636 | thread_name, |
| 2637 | reason, |
| 2638 | description, |
| 2639 | exc_type, |
| 2640 | exc_data, |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2641 | thread_dispatch_qaddr, |
| 2642 | queue_vars_valid, |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 2643 | associated_with_dispatch_queue, |
| 2644 | dispatch_queue_t, |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2645 | queue_name, |
| 2646 | queue_kind, |
Jason Molenda | 26d84e8 | 2016-01-08 00:20:48 +0000 | [diff] [blame] | 2647 | queue_serial_number); |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 2648 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2649 | return eStateStopped; |
| 2650 | } |
| 2651 | break; |
| 2652 | |
| 2653 | case 'W': |
Todd Fiala | ff6131a | 2014-05-19 04:57:23 +0000 | [diff] [blame] | 2654 | case 'X': |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2655 | // process exited |
| 2656 | return eStateExited; |
| 2657 | |
| 2658 | default: |
| 2659 | break; |
| 2660 | } |
| 2661 | return eStateInvalid; |
| 2662 | } |
| 2663 | |
| 2664 | void |
| 2665 | ProcessGDBRemote::RefreshStateAfterStop () |
| 2666 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 2667 | std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); |
| 2668 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2669 | m_thread_ids.clear(); |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 2670 | m_thread_pcs.clear(); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2671 | // Set the thread stop info. It might have a "threads" key whose value is |
| 2672 | // a list of all thread IDs in the current process, so m_thread_ids might |
| 2673 | // get set. |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2674 | |
| 2675 | // Scope for the lock |
| 2676 | { |
| 2677 | // Lock the thread stack while we access it |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 2678 | std::lock_guard<std::recursive_mutex> guard(m_last_stop_packet_mutex); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2679 | // Get the number of stop packets on the stack |
| 2680 | int nItems = m_stop_packet_stack.size(); |
| 2681 | // Iterate over them |
| 2682 | for (int i = 0; i < nItems; i++) |
| 2683 | { |
| 2684 | // Get the thread stop info |
| 2685 | StringExtractorGDBRemote stop_info = m_stop_packet_stack[i]; |
| 2686 | // Process thread stop info |
| 2687 | SetThreadStopInfo(stop_info); |
| 2688 | } |
| 2689 | // Clear the thread stop stack |
| 2690 | m_stop_packet_stack.clear(); |
| 2691 | } |
| 2692 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2693 | // Check to see if SetThreadStopInfo() filled in m_thread_ids? |
| 2694 | if (m_thread_ids.empty()) |
| 2695 | { |
| 2696 | // No, we need to fetch the thread list manually |
| 2697 | UpdateThreadIDList(); |
| 2698 | } |
| 2699 | |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 2700 | // If we have queried for a default thread id |
| 2701 | if (m_initial_tid != LLDB_INVALID_THREAD_ID) |
| 2702 | { |
| 2703 | m_thread_list.SetSelectedThreadByID(m_initial_tid); |
| 2704 | m_initial_tid = LLDB_INVALID_THREAD_ID; |
| 2705 | } |
| 2706 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2707 | // Let all threads recover from stopping and do any clean up based |
| 2708 | // on the previous thread state (if any). |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 2709 | m_thread_list_real.RefreshStateAfterStop(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | Error |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2714 | ProcessGDBRemote::DoHalt (bool &caused_stop) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2715 | { |
| 2716 | Error error; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2717 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2718 | if (m_public_state.GetValue() == eStateAttaching) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2719 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2720 | // We are being asked to halt during an attach. We need to just close |
| 2721 | // our file handle and debugserver will go away, and we can be done... |
| 2722 | m_gdb_comm.Disconnect(); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2723 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2724 | else |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 2725 | caused_stop = m_gdb_comm.Interrupt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2726 | return error; |
| 2727 | } |
| 2728 | |
| 2729 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2730 | ProcessGDBRemote::DoDetach(bool keep_stopped) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2731 | { |
| 2732 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2733 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2734 | if (log) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2735 | log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2736 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2737 | error = m_gdb_comm.Detach (keep_stopped); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2738 | if (log) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2739 | { |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2740 | if (error.Success()) |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2741 | log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully"); |
| 2742 | else |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2743 | log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>"); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2744 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2745 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2746 | if (!error.Success()) |
| 2747 | return error; |
| 2748 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2749 | // Sleep for one second to let the process get all detached... |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2750 | StopAsyncThread (); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2751 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2752 | SetPrivateState (eStateDetached); |
| 2753 | ResumePrivateStateThread(); |
| 2754 | |
| 2755 | //KillDebugserverProcess (); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2756 | return error; |
| 2757 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2758 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2760 | Error |
| 2761 | ProcessGDBRemote::DoDestroy () |
| 2762 | { |
| 2763 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2764 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2765 | if (log) |
| 2766 | log->Printf ("ProcessGDBRemote::DoDestroy()"); |
| 2767 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2768 | // There is a bug in older iOS debugservers where they don't shut down the process |
| 2769 | // they are debugging properly. If the process is sitting at a breakpoint or an exception, |
| 2770 | // this can cause problems with restarting. So we check to see if any of our threads are stopped |
| 2771 | // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN |
| 2772 | // destroy it again. |
| 2773 | // |
| 2774 | // Note, we don't have a good way to test the version of debugserver, but I happen to know that |
| 2775 | // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of |
| 2776 | // the debugservers with this bug are equal. There really should be a better way to test this! |
| 2777 | // |
| 2778 | // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and |
| 2779 | // get called here to destroy again and we're still at a breakpoint or exception, then we should |
| 2780 | // just do the straight-forward kill. |
| 2781 | // |
| 2782 | // And of course, if we weren't able to stop the process by the time we get here, it isn't |
| 2783 | // necessary (or helpful) to do any of this. |
| 2784 | |
| 2785 | if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning) |
| 2786 | { |
| 2787 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2788 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2789 | // FIXME: These should be ConstStrings so we aren't doing strcmp'ing. |
| 2790 | if (platform_sp |
| 2791 | && platform_sp->GetName() |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 2792 | && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic()) |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2793 | { |
| 2794 | if (m_destroy_tried_resuming) |
| 2795 | { |
| 2796 | if (log) |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2797 | log->PutCString ("ProcessGDBRemote::DoDestroy() - Tried resuming to destroy once already, not doing it again."); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2798 | } |
| 2799 | else |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2800 | { |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2801 | // At present, the plans are discarded and the breakpoints disabled Process::Destroy, |
| 2802 | // but we really need it to happen here and it doesn't matter if we do it twice. |
| 2803 | m_thread_list.DiscardThreadPlans(); |
| 2804 | DisableAllBreakpointSites(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2805 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2806 | bool stop_looks_like_crash = false; |
| 2807 | ThreadList &threads = GetThreadList(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2808 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2809 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 2810 | std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2811 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2812 | size_t num_threads = threads.GetSize(); |
| 2813 | for (size_t i = 0; i < num_threads; i++) |
| 2814 | { |
| 2815 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 2816 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2817 | StopReason reason = eStopReasonInvalid; |
| 2818 | if (stop_info_sp) |
| 2819 | reason = stop_info_sp->GetStopReason(); |
| 2820 | if (reason == eStopReasonBreakpoint |
| 2821 | || reason == eStopReasonException) |
| 2822 | { |
| 2823 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2824 | log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.", |
| 2825 | thread_sp->GetProtocolID(), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2826 | stop_info_sp->GetDescription()); |
| 2827 | stop_looks_like_crash = true; |
| 2828 | break; |
| 2829 | } |
| 2830 | } |
| 2831 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2832 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2833 | if (stop_looks_like_crash) |
| 2834 | { |
| 2835 | if (log) |
| 2836 | log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill."); |
| 2837 | m_destroy_tried_resuming = true; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2838 | |
| 2839 | // If we are going to run again before killing, it would be good to suspend all the threads |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2840 | // before resuming so they won't get into more trouble. Sadly, for the threads stopped with |
| 2841 | // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do |
| 2842 | // have to run the risk of letting those threads proceed a bit. |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2843 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2844 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 2845 | std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2846 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2847 | size_t num_threads = threads.GetSize(); |
| 2848 | for (size_t i = 0; i < num_threads; i++) |
| 2849 | { |
| 2850 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 2851 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2852 | StopReason reason = eStopReasonInvalid; |
| 2853 | if (stop_info_sp) |
| 2854 | reason = stop_info_sp->GetStopReason(); |
| 2855 | if (reason != eStopReasonBreakpoint |
| 2856 | && reason != eStopReasonException) |
| 2857 | { |
| 2858 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2859 | log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.", |
| 2860 | thread_sp->GetProtocolID()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2861 | thread_sp->SetResumeState(eStateSuspended); |
| 2862 | } |
| 2863 | } |
| 2864 | } |
| 2865 | Resume (); |
Jason Molenda | ede3193 | 2015-04-17 05:01:58 +0000 | [diff] [blame] | 2866 | return Destroy(false); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 2871 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2872 | // Interrupt if our inferior is running... |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2873 | int exit_status = SIGABRT; |
| 2874 | std::string exit_string; |
| 2875 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 2876 | if (m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2877 | { |
Jim Ingham | aab7837 | 2011-10-28 01:11:35 +0000 | [diff] [blame] | 2878 | if (m_public_state.GetValue() != eStateAttaching) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2879 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2880 | StringExtractorGDBRemote response; |
| 2881 | bool send_async = true; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 2882 | GDBRemoteCommunication::ScopedTimeout (m_gdb_comm, 3); |
Filipe Cabecinhas | 9e10605 | 2012-08-22 13:25:58 +0000 | [diff] [blame] | 2883 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2884 | if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async) == GDBRemoteCommunication::PacketResult::Success) |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2885 | { |
| 2886 | char packet_cmd = response.GetChar(0); |
| 2887 | |
| 2888 | if (packet_cmd == 'W' || packet_cmd == 'X') |
| 2889 | { |
Jim Ingham | eac0aa4 | 2014-02-21 22:36:11 +0000 | [diff] [blame] | 2890 | #if defined(__APPLE__) |
Jason Molenda | 17b4539 | 2016-06-23 04:24:16 +0000 | [diff] [blame] | 2891 | // For Native processes on Mac OS X, we launch through the Host Platform, then hand the process off |
Jim Ingham | 9d67cc5 | 2014-02-21 22:35:29 +0000 | [diff] [blame] | 2892 | // to debugserver, which becomes the parent process through "PT_ATTACH". Then when we go to kill |
Jason Molenda | 17b4539 | 2016-06-23 04:24:16 +0000 | [diff] [blame] | 2893 | // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then we call waitpid which returns |
Jim Ingham | 9d67cc5 | 2014-02-21 22:35:29 +0000 | [diff] [blame] | 2894 | // with no error and the correct status. But amusingly enough that doesn't seem to actually reap |
| 2895 | // the process, but instead it is left around as a Zombie. Probably the kernel is in the process of |
| 2896 | // switching ownership back to lldb which was the original parent, and gets confused in the handoff. |
| 2897 | // Anyway, so call waitpid here to finally reap it. |
| 2898 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2899 | if (platform_sp && platform_sp->IsHost()) |
| 2900 | { |
| 2901 | int status; |
| 2902 | ::pid_t reap_pid; |
| 2903 | reap_pid = waitpid (GetID(), &status, WNOHANG); |
| 2904 | if (log) |
| 2905 | log->Printf ("Reaped pid: %d, status: %d.\n", reap_pid, status); |
| 2906 | } |
| 2907 | #endif |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2908 | SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2909 | ClearThreadIDList (); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2910 | exit_status = response.GetHexU8(); |
| 2911 | } |
| 2912 | else |
| 2913 | { |
| 2914 | if (log) |
| 2915 | log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str()); |
| 2916 | exit_string.assign("got unexpected response to k packet: "); |
| 2917 | exit_string.append(response.GetStringRef()); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2918 | } |
| 2919 | } |
| 2920 | else |
| 2921 | { |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2922 | if (log) |
| 2923 | log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); |
| 2924 | exit_string.assign("failed to send the k packet"); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2925 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2926 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2927 | else |
| 2928 | { |
| 2929 | if (log) |
Ed Maste | b3a5333 | 2014-03-17 17:05:22 +0000 | [diff] [blame] | 2930 | log->Printf ("ProcessGDBRemote::DoDestroy - killed or interrupted while attaching"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 2931 | exit_string.assign ("killed or interrupted while attaching."); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2932 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2933 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2934 | else |
| 2935 | { |
| 2936 | // If we missed setting the exit status on the way out, do it here. |
| 2937 | // NB set exit status can be called multiple times, the first one sets the status. |
| 2938 | exit_string.assign("destroying when not connected to debugserver"); |
| 2939 | } |
| 2940 | |
| 2941 | SetExitStatus(exit_status, exit_string.c_str()); |
| 2942 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2943 | StopAsyncThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2944 | KillDebugserverProcess (); |
| 2945 | return error; |
| 2946 | } |
| 2947 | |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2948 | void |
| 2949 | ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response) |
| 2950 | { |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2951 | const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos; |
| 2952 | if (did_exec) |
| 2953 | { |
| 2954 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2955 | if (log) |
| 2956 | log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec"); |
| 2957 | |
| 2958 | m_thread_list_real.Clear(); |
| 2959 | m_thread_list.Clear(); |
| 2960 | BuildDynamicRegisterInfo (true); |
Greg Clayton | 2e59d4f | 2015-06-29 20:08:51 +0000 | [diff] [blame] | 2961 | m_gdb_comm.ResetDiscoverableSettings (did_exec); |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2962 | } |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2963 | |
| 2964 | // Scope the lock |
| 2965 | { |
| 2966 | // Lock the thread stack while we access it |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 2967 | std::lock_guard<std::recursive_mutex> guard(m_last_stop_packet_mutex); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 2968 | |
| 2969 | // We are are not using non-stop mode, there can only be one last stop |
| 2970 | // reply packet, so clear the list. |
| 2971 | if (GetTarget().GetNonStopModeEnabled() == false) |
| 2972 | m_stop_packet_stack.clear(); |
| 2973 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2974 | // Add this stop packet to the stop packet stack |
| 2975 | // This stack will get popped and examined when we switch to the |
| 2976 | // Stopped state |
| 2977 | m_stop_packet_stack.push_back(response); |
| 2978 | } |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Chaoren Lin | c963a22 | 2015-09-01 16:58:45 +0000 | [diff] [blame] | 2981 | void |
| 2982 | ProcessGDBRemote::SetUnixSignals(const UnixSignalsSP &signals_sp) |
| 2983 | { |
| 2984 | Process::SetUnixSignals(std::make_shared<GDBRemoteSignals>(signals_sp)); |
| 2985 | } |
| 2986 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2987 | //------------------------------------------------------------------ |
| 2988 | // Process Queries |
| 2989 | //------------------------------------------------------------------ |
| 2990 | |
| 2991 | bool |
| 2992 | ProcessGDBRemote::IsAlive () |
| 2993 | { |
Jason Molenda | 1d3b356 | 2015-11-05 23:54:29 +0000 | [diff] [blame] | 2994 | return m_gdb_comm.IsConnected() && Process::IsAlive(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | addr_t |
| 2998 | ProcessGDBRemote::GetImageInfoAddress() |
| 2999 | { |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3000 | // request the link map address via the $qShlibInfoAddr packet |
| 3001 | lldb::addr_t addr = m_gdb_comm.GetShlibInfoAddr(); |
| 3002 | |
| 3003 | // the loaded module list can also provides a link map address |
| 3004 | if (addr == LLDB_INVALID_ADDRESS) |
| 3005 | { |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 3006 | LoadedModuleInfoList list; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3007 | if (GetLoadedModuleList (list).Success()) |
| 3008 | addr = list.m_link_map; |
| 3009 | } |
| 3010 | |
| 3011 | return addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 3014 | void |
| 3015 | ProcessGDBRemote::WillPublicStop () |
| 3016 | { |
| 3017 | // See if the GDB remote client supports the JSON threads info. |
| 3018 | // If so, we gather stop info for all threads, expedited registers, |
| 3019 | // expedited memory, runtime queue information (iOS and MacOSX only), |
| 3020 | // and more. Expediting memory will help stack backtracing be much |
| 3021 | // faster. Expediting registers will make sure we don't have to read |
| 3022 | // the thread registers for GPRs. |
| 3023 | m_jthreadsinfo_sp = m_gdb_comm.GetThreadsInfo(); |
| 3024 | |
| 3025 | if (m_jthreadsinfo_sp) |
| 3026 | { |
| 3027 | // Now set the stop info for each thread and also expedite any registers |
| 3028 | // and memory that was in the jThreadsInfo response. |
| 3029 | StructuredData::Array *thread_infos = m_jthreadsinfo_sp->GetAsArray(); |
| 3030 | if (thread_infos) |
| 3031 | { |
| 3032 | const size_t n = thread_infos->GetSize(); |
| 3033 | for (size_t i=0; i<n; ++i) |
| 3034 | { |
| 3035 | StructuredData::Dictionary *thread_dict = thread_infos->GetItemAtIndex(i)->GetAsDictionary(); |
| 3036 | if (thread_dict) |
| 3037 | SetThreadStopInfo(thread_dict); |
| 3038 | } |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3043 | //------------------------------------------------------------------ |
| 3044 | // Process Memory |
| 3045 | //------------------------------------------------------------------ |
| 3046 | size_t |
| 3047 | ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 3048 | { |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3049 | GetMaxMemorySize (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3050 | if (size > m_max_memory_size) |
| 3051 | { |
| 3052 | // Keep memory read sizes down to a sane limit. This function will be |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3053 | // called multiple times in order to complete the task by |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3054 | // lldb_private::Process so it is ok to do this. |
| 3055 | size = m_max_memory_size; |
| 3056 | } |
| 3057 | |
| 3058 | char packet[64]; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3059 | int packet_len; |
| 3060 | bool binary_memory_read = m_gdb_comm.GetxPacketSupported(); |
Pavel Labath | 3bf1125 | 2015-10-14 12:59:37 +0000 | [diff] [blame] | 3061 | packet_len = ::snprintf(packet, sizeof(packet), "%c%" PRIx64 ",%" PRIx64, |
| 3062 | binary_memory_read ? 'x' : 'm', (uint64_t)addr, (uint64_t)size); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 3063 | assert (packet_len + 1 < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3064 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 3065 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true) == GDBRemoteCommunication::PacketResult::Success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3066 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 3067 | if (response.IsNormalResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3068 | { |
| 3069 | error.Clear(); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3070 | if (binary_memory_read) |
| 3071 | { |
| 3072 | // The lower level GDBRemoteCommunication packet receive layer has already de-quoted any |
| 3073 | // 0x7d character escaping that was present in the packet |
| 3074 | |
| 3075 | size_t data_received_size = response.GetBytesLeft(); |
| 3076 | if (data_received_size > size) |
| 3077 | { |
| 3078 | // Don't write past the end of BUF if the remote debug server gave us too |
| 3079 | // much data for some reason. |
| 3080 | data_received_size = size; |
| 3081 | } |
| 3082 | memcpy (buf, response.GetStringRef().data(), data_received_size); |
| 3083 | return data_received_size; |
| 3084 | } |
| 3085 | else |
| 3086 | { |
Zachary Turner | d08f09c | 2016-08-30 18:12:11 +0000 | [diff] [blame] | 3087 | return response.GetHexBytes(llvm::MutableArrayRef<uint8_t>((uint8_t*)buf, size), '\xdd'); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3088 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3089 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 3090 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 3091 | error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 3092 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 3093 | error.SetErrorStringWithFormat("GDB server does not support reading memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3094 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 3095 | error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3096 | } |
| 3097 | else |
| 3098 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 3099 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3100 | } |
| 3101 | return 0; |
| 3102 | } |
| 3103 | |
| 3104 | size_t |
| 3105 | ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 3106 | { |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3107 | GetMaxMemorySize (); |
Greg Clayton | b4aaf2e | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 3108 | if (size > m_max_memory_size) |
| 3109 | { |
| 3110 | // Keep memory read sizes down to a sane limit. This function will be |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3111 | // called multiple times in order to complete the task by |
Greg Clayton | b4aaf2e | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 3112 | // lldb_private::Process so it is ok to do this. |
| 3113 | size = m_max_memory_size; |
| 3114 | } |
| 3115 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3116 | StreamString packet; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3117 | packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); |
Bruce Mitchener | 9ccb970 | 2015-11-07 04:40:13 +0000 | [diff] [blame] | 3118 | packet.PutBytesAsRawHex8(buf, size, endian::InlHostByteOrder(), endian::InlHostByteOrder()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3119 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 3120 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true) == GDBRemoteCommunication::PacketResult::Success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3121 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 3122 | if (response.IsOKResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3123 | { |
| 3124 | error.Clear(); |
| 3125 | return size; |
| 3126 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 3127 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 3128 | error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 3129 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 3130 | error.SetErrorStringWithFormat("GDB server does not support writing memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3131 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 3132 | error.SetErrorStringWithFormat("unexpected response to GDB server memory write packet '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3133 | } |
| 3134 | else |
| 3135 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 3136 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3137 | } |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
| 3141 | lldb::addr_t |
| 3142 | ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 3143 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3144 | Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_EXPRESSIONS)); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 3145 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3146 | |
Greg Clayton | cec91ef | 2016-02-26 01:20:20 +0000 | [diff] [blame] | 3147 | if (m_gdb_comm.SupportsAllocDeallocMemory() != eLazyBoolNo) |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 3148 | { |
Greg Clayton | cec91ef | 2016-02-26 01:20:20 +0000 | [diff] [blame] | 3149 | allocated_addr = m_gdb_comm.AllocateMemory (size, permissions); |
| 3150 | if (allocated_addr != LLDB_INVALID_ADDRESS || m_gdb_comm.SupportsAllocDeallocMemory() == eLazyBoolYes) |
| 3151 | return allocated_addr; |
| 3152 | } |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 3153 | |
Greg Clayton | cec91ef | 2016-02-26 01:20:20 +0000 | [diff] [blame] | 3154 | if (m_gdb_comm.SupportsAllocDeallocMemory() == eLazyBoolNo) |
| 3155 | { |
| 3156 | // Call mmap() to create memory in the inferior.. |
| 3157 | unsigned prot = 0; |
| 3158 | if (permissions & lldb::ePermissionsReadable) |
| 3159 | prot |= eMmapProtRead; |
| 3160 | if (permissions & lldb::ePermissionsWritable) |
| 3161 | prot |= eMmapProtWrite; |
| 3162 | if (permissions & lldb::ePermissionsExecutable) |
| 3163 | prot |= eMmapProtExec; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 3164 | |
Greg Clayton | cec91ef | 2016-02-26 01:20:20 +0000 | [diff] [blame] | 3165 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 3166 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) |
| 3167 | m_addr_to_mmap_size[allocated_addr] = size; |
| 3168 | else |
| 3169 | { |
| 3170 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 3171 | if (log) |
| 3172 | log->Printf ("ProcessGDBRemote::%s no direct stub support for memory allocation, and InferiorCallMmap also failed - is stub missing register context save/restore capability?", __FUNCTION__); |
| 3173 | } |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 3174 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3175 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3176 | if (allocated_addr == LLDB_INVALID_ADDRESS) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3177 | error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3178 | else |
| 3179 | error.Clear(); |
| 3180 | return allocated_addr; |
| 3181 | } |
| 3182 | |
| 3183 | Error |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3184 | ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr, |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 3185 | MemoryRegionInfo ®ion_info) |
| 3186 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3187 | |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 3188 | Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info)); |
| 3189 | return error; |
| 3190 | } |
| 3191 | |
| 3192 | Error |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 3193 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num) |
| 3194 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3195 | |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 3196 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num)); |
| 3197 | return error; |
| 3198 | } |
| 3199 | |
| 3200 | Error |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 3201 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after) |
| 3202 | { |
Jaydeep Patil | 725666c | 2015-08-13 03:46:01 +0000 | [diff] [blame] | 3203 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after, GetTarget().GetArchitecture())); |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 3204 | return error; |
| 3205 | } |
| 3206 | |
| 3207 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3208 | ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr) |
| 3209 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3210 | Error error; |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 3211 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
| 3212 | |
| 3213 | switch (supported) |
| 3214 | { |
| 3215 | case eLazyBoolCalculate: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3216 | // We should never be deallocating memory without allocating memory |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 3217 | // first so we should never get eLazyBoolCalculate |
| 3218 | error.SetErrorString ("tried to deallocate memory without ever allocating memory"); |
| 3219 | break; |
| 3220 | |
| 3221 | case eLazyBoolYes: |
| 3222 | if (!m_gdb_comm.DeallocateMemory (addr)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3223 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 3224 | break; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3225 | |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 3226 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 3227 | // Call munmap() to deallocate memory in the inferior.. |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 3228 | { |
| 3229 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 3230 | if (pos != m_addr_to_mmap_size.end() && |
| 3231 | InferiorCallMunmap(this, addr, pos->second)) |
| 3232 | m_addr_to_mmap_size.erase (pos); |
| 3233 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3234 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 3235 | } |
| 3236 | break; |
| 3237 | } |
| 3238 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3239 | return error; |
| 3240 | } |
| 3241 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3243 | //------------------------------------------------------------------ |
| 3244 | // Process STDIO |
| 3245 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3246 | size_t |
| 3247 | ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error) |
| 3248 | { |
| 3249 | if (m_stdio_communication.IsConnected()) |
| 3250 | { |
| 3251 | ConnectionStatus status; |
| 3252 | m_stdio_communication.Write(src, src_len, status, NULL); |
| 3253 | } |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 3254 | else if (m_stdin_forward) |
Vince Harron | e0be425 | 2015-02-06 18:32:57 +0000 | [diff] [blame] | 3255 | { |
| 3256 | m_gdb_comm.SendStdinNotification(src, src_len); |
| 3257 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3258 | return 0; |
| 3259 | } |
| 3260 | |
| 3261 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 3262 | ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3263 | { |
| 3264 | Error error; |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3265 | assert(bp_site != NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3266 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3267 | // Get logging info |
| 3268 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3269 | user_id_t site_id = bp_site->GetID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3270 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3271 | // Get the breakpoint address |
| 3272 | const addr_t addr = bp_site->GetLoadAddress(); |
| 3273 | |
| 3274 | // Log that a breakpoint was requested |
| 3275 | if (log) |
| 3276 | log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr); |
| 3277 | |
| 3278 | // Breakpoint already exists and is enabled |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3279 | if (bp_site->IsEnabled()) |
| 3280 | { |
| 3281 | if (log) |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3282 | log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64 " -- SUCCESS (already enabled)", site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3283 | return error; |
| 3284 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3285 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3286 | // Get the software breakpoint trap opcode size |
| 3287 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 3288 | |
| 3289 | // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this breakpoint type |
| 3290 | // is supported by the remote stub. These are set to true by default, and later set to false |
| 3291 | // only after we receive an unimplemented response when sending a breakpoint packet. This means |
| 3292 | // initially that unless we were specifically instructed to use a hardware breakpoint, LLDB will |
| 3293 | // attempt to set a software breakpoint. HardwareRequired() also queries a boolean variable which |
| 3294 | // indicates if the user specifically asked for hardware breakpoints. If true then we will |
| 3295 | // skip over software breakpoints. |
| 3296 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && (!bp_site->HardwareRequired())) |
| 3297 | { |
| 3298 | // Try to send off a software breakpoint packet ($Z0) |
Jim Ingham | 906d91e | 2016-05-19 02:13:44 +0000 | [diff] [blame] | 3299 | uint8_t error_no = m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size); |
| 3300 | if (error_no == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3301 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3302 | // The breakpoint was placed successfully |
| 3303 | bp_site->SetEnabled(true); |
| 3304 | bp_site->SetType(BreakpointSite::eExternal); |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 3305 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3306 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3307 | |
| 3308 | // SendGDBStoppointTypePacket() will return an error if it was unable to set this |
| 3309 | // breakpoint. We need to differentiate between a error specific to placing this breakpoint |
| 3310 | // or if we have learned that this breakpoint type is unsupported. To do this, we |
| 3311 | // must test the support boolean for this breakpoint type to see if it now indicates that |
| 3312 | // this breakpoint type is unsupported. If they are still supported then we should return |
| 3313 | // with the error code. If they are now unsupported, then we would like to fall through |
| 3314 | // and try another form of breakpoint. |
| 3315 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) |
Jim Ingham | 906d91e | 2016-05-19 02:13:44 +0000 | [diff] [blame] | 3316 | { |
| 3317 | if (error_no != UINT8_MAX) |
| 3318 | error.SetErrorStringWithFormat("error: %d sending the breakpoint request", errno); |
| 3319 | else |
| 3320 | error.SetErrorString("error sending the breakpoint request"); |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3321 | return error; |
Jim Ingham | 906d91e | 2016-05-19 02:13:44 +0000 | [diff] [blame] | 3322 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3323 | |
| 3324 | // We reach here when software breakpoints have been found to be unsupported. For future |
| 3325 | // calls to set a breakpoint, we will not attempt to set a breakpoint with a type that is |
| 3326 | // known not to be supported. |
| 3327 | if (log) |
| 3328 | log->Printf("Software breakpoints are unsupported"); |
| 3329 | |
| 3330 | // So we will fall through and try a hardware breakpoint |
| 3331 | } |
| 3332 | |
| 3333 | // The process of setting a hardware breakpoint is much the same as above. We check the |
| 3334 | // supported boolean for this breakpoint type, and if it is thought to be supported then we |
| 3335 | // will try to set this breakpoint with a hardware breakpoint. |
| 3336 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) |
| 3337 | { |
| 3338 | // Try to send off a hardware breakpoint packet ($Z1) |
Jim Ingham | 906d91e | 2016-05-19 02:13:44 +0000 | [diff] [blame] | 3339 | uint8_t error_no = m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size); |
| 3340 | if (error_no == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3341 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3342 | // The breakpoint was placed successfully |
| 3343 | bp_site->SetEnabled(true); |
| 3344 | bp_site->SetType(BreakpointSite::eHardware); |
| 3345 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3346 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3347 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3348 | // Check if the error was something other then an unsupported breakpoint type |
| 3349 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) |
| 3350 | { |
| 3351 | // Unable to set this hardware breakpoint |
Jim Ingham | 906d91e | 2016-05-19 02:13:44 +0000 | [diff] [blame] | 3352 | if (error_no != UINT8_MAX) |
| 3353 | error.SetErrorStringWithFormat("error: %d sending the hardware breakpoint request " |
| 3354 | "(hardware breakpoint resources might be exhausted or unavailable)", |
| 3355 | error_no); |
| 3356 | else |
| 3357 | error.SetErrorString("error sending the hardware breakpoint request (hardware breakpoint resources " |
| 3358 | "might be exhausted or unavailable)"); |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3359 | return error; |
| 3360 | } |
| 3361 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 3362 | // We will reach here when the stub gives an unsupported response to a hardware breakpoint |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3363 | if (log) |
| 3364 | log->Printf("Hardware breakpoints are unsupported"); |
| 3365 | |
| 3366 | // Finally we will falling through to a #trap style breakpoint |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3369 | // Don't fall through when hardware breakpoints were specifically requested |
| 3370 | if (bp_site->HardwareRequired()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3371 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3372 | error.SetErrorString("hardware breakpoints are not supported"); |
| 3373 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3374 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3375 | |
| 3376 | // As a last resort we want to place a manual breakpoint. An instruction |
| 3377 | // is placed into the process memory using memory write packets. |
| 3378 | return EnableSoftwareBreakpoint(bp_site); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 3382 | ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3383 | { |
| 3384 | Error error; |
| 3385 | assert (bp_site != NULL); |
| 3386 | addr_t addr = bp_site->GetLoadAddress(); |
| 3387 | user_id_t site_id = bp_site->GetID(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3388 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3389 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 3390 | log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3391 | |
| 3392 | if (bp_site->IsEnabled()) |
| 3393 | { |
| 3394 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 3395 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3396 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 3397 | switch (bp_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3398 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3399 | case BreakpointSite::eSoftware: |
| 3400 | error = DisableSoftwareBreakpoint (bp_site); |
| 3401 | break; |
| 3402 | |
| 3403 | case BreakpointSite::eHardware: |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 3404 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, addr, bp_op_size)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3405 | error.SetErrorToGenericError(); |
| 3406 | break; |
| 3407 | |
| 3408 | case BreakpointSite::eExternal: |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 3409 | { |
| 3410 | GDBStoppointType stoppoint_type; |
| 3411 | if (bp_site->IsHardware()) |
| 3412 | stoppoint_type = eBreakpointHardware; |
| 3413 | else |
| 3414 | stoppoint_type = eBreakpointSoftware; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3415 | |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 3416 | if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, bp_op_size)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3417 | error.SetErrorToGenericError(); |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 3418 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3419 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3420 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3421 | if (error.Success()) |
| 3422 | bp_site->SetEnabled(false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3423 | } |
| 3424 | else |
| 3425 | { |
| 3426 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 3427 | log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3428 | return error; |
| 3429 | } |
| 3430 | |
| 3431 | if (error.Success()) |
| 3432 | error.SetErrorToGenericError(); |
| 3433 | return error; |
| 3434 | } |
| 3435 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3436 | // Pre-requisite: wp != NULL. |
| 3437 | static GDBStoppointType |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 3438 | GetGDBStoppointType (Watchpoint *wp) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3439 | { |
| 3440 | assert(wp); |
| 3441 | bool watch_read = wp->WatchpointRead(); |
| 3442 | bool watch_write = wp->WatchpointWrite(); |
| 3443 | |
| 3444 | // watch_read and watch_write cannot both be false. |
| 3445 | assert(watch_read || watch_write); |
| 3446 | if (watch_read && watch_write) |
| 3447 | return eWatchpointReadWrite; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 3448 | else if (watch_read) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3449 | return eWatchpointRead; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 3450 | else // Must be watch_write, then. |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3451 | return eWatchpointWrite; |
| 3452 | } |
| 3453 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3454 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 3455 | ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3456 | { |
| 3457 | Error error; |
| 3458 | if (wp) |
| 3459 | { |
| 3460 | user_id_t watchID = wp->GetID(); |
| 3461 | addr_t addr = wp->GetLoadAddress(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3462 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3463 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3464 | log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3465 | if (wp->IsEnabled()) |
| 3466 | { |
| 3467 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3468 | log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", watchID, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3469 | return error; |
| 3470 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3471 | |
| 3472 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 3473 | // Pass down an appropriate z/Z packet... |
| 3474 | if (m_gdb_comm.SupportsGDBStoppointPacket (type)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3475 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3476 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0) |
| 3477 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 3478 | wp->SetEnabled(true, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3479 | return error; |
| 3480 | } |
| 3481 | else |
| 3482 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3483 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3484 | else |
| 3485 | error.SetErrorString("watchpoints not supported"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3486 | } |
| 3487 | else |
| 3488 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 3489 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3490 | } |
| 3491 | if (error.Success()) |
| 3492 | error.SetErrorToGenericError(); |
| 3493 | return error; |
| 3494 | } |
| 3495 | |
| 3496 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 3497 | ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3498 | { |
| 3499 | Error error; |
| 3500 | if (wp) |
| 3501 | { |
| 3502 | user_id_t watchID = wp->GetID(); |
| 3503 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3504 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3505 | |
| 3506 | addr_t addr = wp->GetLoadAddress(); |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 3507 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3508 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3509 | log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3510 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3511 | if (!wp->IsEnabled()) |
| 3512 | { |
| 3513 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3514 | log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", watchID, (uint64_t)addr); |
Johnny Chen | 892943f | 2012-08-23 22:28:26 +0000 | [diff] [blame] | 3515 | // See also 'class WatchpointSentry' within StopInfo.cpp. |
| 3516 | // This disabling attempt might come from the user-supplied actions, we'll route it in order for |
| 3517 | // the watchpoint object to intelligently process this action. |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 3518 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3519 | return error; |
| 3520 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3521 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3522 | if (wp->IsHardware()) |
| 3523 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3524 | GDBStoppointType type = GetGDBStoppointType(wp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3525 | // Pass down an appropriate z/Z packet... |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3526 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0) |
| 3527 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 3528 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 3529 | return error; |
| 3530 | } |
| 3531 | else |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3532 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3533 | } |
| 3534 | // TODO: clear software watchpoints if we implement them |
| 3535 | } |
| 3536 | else |
| 3537 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 3538 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3539 | } |
| 3540 | if (error.Success()) |
| 3541 | error.SetErrorToGenericError(); |
| 3542 | return error; |
| 3543 | } |
| 3544 | |
| 3545 | void |
| 3546 | ProcessGDBRemote::Clear() |
| 3547 | { |
| 3548 | m_flags = 0; |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 3549 | m_thread_list_real.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3550 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | Error |
| 3554 | ProcessGDBRemote::DoSignal (int signo) |
| 3555 | { |
| 3556 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3557 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3558 | if (log) |
| 3559 | log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo); |
| 3560 | |
| 3561 | if (!m_gdb_comm.SendAsyncSignal (signo)) |
| 3562 | error.SetErrorStringWithFormat("failed to send signal %i", signo); |
| 3563 | return error; |
| 3564 | } |
| 3565 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3566 | Error |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 3567 | ProcessGDBRemote::EstablishConnectionIfNeeded (const ProcessInfo &process_info) |
| 3568 | { |
| 3569 | // Make sure we aren't already connected? |
| 3570 | if (m_gdb_comm.IsConnected()) |
| 3571 | return Error(); |
| 3572 | |
| 3573 | PlatformSP platform_sp (GetTarget ().GetPlatform ()); |
| 3574 | if (platform_sp && !platform_sp->IsHost ()) |
| 3575 | return Error("Lost debug server connection"); |
| 3576 | |
| 3577 | auto error = LaunchAndConnectToDebugserver (process_info); |
| 3578 | if (error.Fail()) |
| 3579 | { |
| 3580 | const char *error_string = error.AsCString(); |
| 3581 | if (error_string == nullptr) |
| 3582 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
| 3583 | } |
| 3584 | return error; |
| 3585 | } |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3586 | #if defined (__APPLE__) |
| 3587 | #define USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 1 |
| 3588 | #endif |
| 3589 | |
| 3590 | #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION |
| 3591 | static bool SetCloexecFlag(int fd) |
| 3592 | { |
| 3593 | #if defined(FD_CLOEXEC) |
| 3594 | int flags = ::fcntl(fd, F_GETFD); |
| 3595 | if (flags == -1) |
| 3596 | return false; |
| 3597 | return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0); |
| 3598 | #else |
| 3599 | return false; |
| 3600 | #endif |
| 3601 | } |
| 3602 | #endif |
Oleksiy Vyalov | afd6ce4 | 2015-11-23 19:32:24 +0000 | [diff] [blame] | 3603 | |
| 3604 | Error |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3605 | ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3606 | { |
Pavel Labath | 998bdc5 | 2016-05-11 16:59:04 +0000 | [diff] [blame] | 3607 | using namespace std::placeholders; // For _1, _2, etc. |
| 3608 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3609 | Error error; |
| 3610 | if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) |
| 3611 | { |
| 3612 | // If we locate debugserver, keep that located version around |
| 3613 | static FileSpec g_debugserver_file_spec; |
| 3614 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 3615 | ProcessLaunchInfo debugserver_launch_info; |
Oleksiy Vyalov | f8ce61c | 2015-01-28 17:36:59 +0000 | [diff] [blame] | 3616 | // Make debugserver run in its own session so signals generated by |
| 3617 | // special terminal key sequences (^C) don't affect debugserver. |
| 3618 | debugserver_launch_info.SetLaunchInSeparateProcessGroup(true); |
| 3619 | |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3620 | const std::weak_ptr<ProcessGDBRemote> this_wp = std::static_pointer_cast<ProcessGDBRemote>(shared_from_this()); |
| 3621 | debugserver_launch_info.SetMonitorProcessCallback(std::bind(MonitorDebugserverProcess, this_wp, _1, _2, _3, _4), |
Pavel Labath | 998bdc5 | 2016-05-11 16:59:04 +0000 | [diff] [blame] | 3622 | false); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3623 | debugserver_launch_info.SetUserID(process_info.GetUserID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3624 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 3625 | |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3626 | int communication_fd = -1; |
| 3627 | #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION |
| 3628 | // Auto close the sockets we might open up unless everything goes OK. This |
| 3629 | // helps us not leak file descriptors when things go wrong. |
| 3630 | lldb_utility::CleanUp <int, int> our_socket(-1, -1, close); |
| 3631 | lldb_utility::CleanUp <int, int> gdb_socket(-1, -1, close); |
| 3632 | |
| 3633 | // Use a socketpair on Apple for now until other platforms can verify it |
| 3634 | // works and is fast enough |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 3635 | { |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3636 | int sockets[2]; /* the pair of socket descriptors */ |
| 3637 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) |
| 3638 | { |
| 3639 | error.SetErrorToErrno(); |
| 3640 | return error; |
| 3641 | } |
| 3642 | |
| 3643 | our_socket.set(sockets[0]); |
| 3644 | gdb_socket.set(sockets[1]); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3647 | // Don't let any child processes inherit our communication socket |
| 3648 | SetCloexecFlag(our_socket.get()); |
| 3649 | communication_fd = gdb_socket.get(); |
| 3650 | #endif |
| 3651 | |
| 3652 | error = m_gdb_comm.StartDebugserverProcess(nullptr, GetTarget().GetPlatform().get(), debugserver_launch_info, nullptr, nullptr, communication_fd); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3653 | |
| 3654 | if (error.Success ()) |
| 3655 | m_debugserver_pid = debugserver_launch_info.GetProcessID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3656 | else |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3657 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3658 | |
| 3659 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3660 | { |
| 3661 | #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION |
| 3662 | // Our process spawned correctly, we can now set our connection to use our |
| 3663 | // end of the socket pair |
| 3664 | m_gdb_comm.SetConnection(new ConnectionFileDescriptor(our_socket.release(), true)); |
| 3665 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3666 | StartAsyncThread (); |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3667 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3668 | |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3669 | if (error.Fail()) |
| 3670 | { |
| 3671 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 3672 | |
| 3673 | if (log) |
| 3674 | log->Printf("failed to start debugserver process: %s", error.AsCString()); |
| 3675 | return error; |
| 3676 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3677 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 3678 | if (m_gdb_comm.IsConnected()) |
| 3679 | { |
| 3680 | // Finish the connection process by doing the handshake without connecting (send NULL URL) |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3681 | ConnectToDebugserver(NULL); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 3682 | } |
| 3683 | else |
| 3684 | { |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame] | 3685 | error.SetErrorString("connection failed"); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 3686 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3687 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3688 | } |
| 3689 | return error; |
| 3690 | } |
| 3691 | |
| 3692 | bool |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3693 | ProcessGDBRemote::MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp, lldb::pid_t debugserver_pid, |
Pavel Labath | 998bdc5 | 2016-05-11 16:59:04 +0000 | [diff] [blame] | 3694 | bool exited, // True if the process did exit |
| 3695 | int signo, // Zero for no signal |
| 3696 | int exit_status // Exit value of process if signal is zero |
| 3697 | ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3698 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3699 | // "debugserver_pid" argument passed in is the process ID for |
| 3700 | // debugserver that we are tracking... |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3701 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3702 | const bool handled = true; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3703 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3704 | if (log) |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3705 | log->Printf("ProcessGDBRemote::%s(process_wp, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", __FUNCTION__, |
| 3706 | debugserver_pid, signo, signo, exit_status); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3707 | |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3708 | std::shared_ptr<ProcessGDBRemote> process_sp = process_wp.lock(); |
| 3709 | if (log) |
Saleem Abdulrasool | abdfc21 | 2016-05-15 18:18:13 +0000 | [diff] [blame] | 3710 | log->Printf("ProcessGDBRemote::%s(process = %p)", __FUNCTION__, static_cast<void *>(process_sp.get())); |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3711 | if (!process_sp || process_sp->m_debugserver_pid != debugserver_pid) |
| 3712 | return handled; |
| 3713 | |
| 3714 | // Sleep for a half a second to make sure our inferior process has |
| 3715 | // time to set its exit status before we set it incorrectly when |
| 3716 | // both the debugserver and the inferior process shut down. |
| 3717 | usleep(500000); |
| 3718 | // If our process hasn't yet exited, debugserver might have died. |
| 3719 | // If the process did exit, then we are reaping it. |
| 3720 | const StateType state = process_sp->GetState(); |
| 3721 | |
| 3722 | if (state != eStateInvalid && state != eStateUnloaded && state != eStateExited && state != eStateDetached) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3723 | { |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3724 | char error_str[1024]; |
| 3725 | if (signo) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3726 | { |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3727 | const char *signal_cstr = process_sp->GetUnixSignals()->GetSignalAsCString(signo); |
| 3728 | if (signal_cstr) |
| 3729 | ::snprintf(error_str, sizeof(error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); |
| 3730 | else |
| 3731 | ::snprintf(error_str, sizeof(error_str), DEBUGSERVER_BASENAME " died with signal %i", signo); |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 3732 | } |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3733 | else |
| 3734 | { |
| 3735 | ::snprintf(error_str, sizeof(error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", |
| 3736 | exit_status); |
| 3737 | } |
| 3738 | |
| 3739 | process_sp->SetExitStatus(-1, error_str); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3740 | } |
Pavel Labath | 194357c | 2016-05-12 11:10:01 +0000 | [diff] [blame] | 3741 | // Debugserver has exited we need to let our ProcessGDBRemote |
| 3742 | // know that it no longer has a debugserver instance |
| 3743 | process_sp->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 3744 | return handled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
| 3747 | void |
| 3748 | ProcessGDBRemote::KillDebugserverProcess () |
| 3749 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3750 | m_gdb_comm.Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3751 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 3752 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 3753 | Host::Kill (m_debugserver_pid, SIGINT); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3754 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | void |
| 3759 | ProcessGDBRemote::Initialize() |
| 3760 | { |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 3761 | static std::once_flag g_once_flag; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3762 | |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 3763 | std::call_once(g_once_flag, []() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3764 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3765 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 3766 | GetPluginDescriptionStatic(), |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3767 | CreateInstance, |
| 3768 | DebuggerInitialize); |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 3769 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3772 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3773 | ProcessGDBRemote::DebuggerInitialize (Debugger &debugger) |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3774 | { |
| 3775 | if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) |
| 3776 | { |
| 3777 | const bool is_global_setting = true; |
| 3778 | PluginManager::CreateSettingForProcessPlugin (debugger, |
| 3779 | GetGlobalPluginProperties()->GetValueProperties(), |
| 3780 | ConstString ("Properties for the gdb-remote process plug-in."), |
| 3781 | is_global_setting); |
| 3782 | } |
| 3783 | } |
| 3784 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3785 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3786 | ProcessGDBRemote::StartAsyncThread () |
| 3787 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3788 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3789 | |
| 3790 | if (log) |
| 3791 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3792 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 3793 | std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3794 | if (!m_async_thread.IsJoinable()) |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3795 | { |
| 3796 | // Create a thread that watches our internal state and controls which |
| 3797 | // events make it to clients (into the DCProcess event queue). |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3798 | |
| 3799 | m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3800 | } |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3801 | else if (log) |
| 3802 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was already running.", __FUNCTION__); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3803 | |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3804 | return m_async_thread.IsJoinable(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | void |
| 3808 | ProcessGDBRemote::StopAsyncThread () |
| 3809 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3810 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3811 | |
| 3812 | if (log) |
| 3813 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 3814 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 3815 | std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3816 | if (m_async_thread.IsJoinable()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3817 | { |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3818 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3819 | |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3820 | // This will shut down the async thread. |
| 3821 | m_gdb_comm.Disconnect(); // Disconnect from the debug server. |
| 3822 | |
| 3823 | // Stop the stdio thread |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3824 | m_async_thread.Join(nullptr); |
Pavel Labath | a55a953 | 2015-03-11 09:53:42 +0000 | [diff] [blame] | 3825 | m_async_thread.Reset(); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3826 | } |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3827 | else if (log) |
| 3828 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was not running.", __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Ewan Crawford | 76df288 | 2015-06-23 12:32:06 +0000 | [diff] [blame] | 3831 | bool |
| 3832 | ProcessGDBRemote::HandleNotifyPacket (StringExtractorGDBRemote &packet) |
| 3833 | { |
| 3834 | // get the packet at a string |
| 3835 | const std::string &pkt = packet.GetStringRef(); |
| 3836 | // skip %stop: |
| 3837 | StringExtractorGDBRemote stop_info(pkt.c_str() + 5); |
| 3838 | |
| 3839 | // pass as a thread stop info packet |
| 3840 | SetLastStopPacket(stop_info); |
| 3841 | |
| 3842 | // check for more stop reasons |
| 3843 | HandleStopReplySequence(); |
| 3844 | |
| 3845 | // if the process is stopped then we need to fake a resume |
| 3846 | // so that we can stop properly with the new break. This |
| 3847 | // is possible due to SetPrivateState() broadcasting the |
| 3848 | // state change as a side effect. |
| 3849 | if (GetPrivateState() == lldb::StateType::eStateStopped) |
| 3850 | { |
| 3851 | SetPrivateState(lldb::StateType::eStateRunning); |
| 3852 | } |
| 3853 | |
| 3854 | // since we have some stopped packets we can halt the process |
| 3855 | SetPrivateState(lldb::StateType::eStateStopped); |
| 3856 | |
| 3857 | return true; |
| 3858 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3859 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 3860 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3861 | ProcessGDBRemote::AsyncThread (void *arg) |
| 3862 | { |
| 3863 | ProcessGDBRemote *process = (ProcessGDBRemote*) arg; |
| 3864 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3865 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3866 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3867 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3868 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3869 | EventSP event_sp; |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3870 | bool done = false; |
| 3871 | while (!done) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3872 | { |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3873 | if (log) |
| 3874 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID()); |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 3875 | if (process->m_async_listener_sp->WaitForEvent(std::chrono::microseconds(0), event_sp)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3876 | { |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3877 | const uint32_t event_type = event_sp->GetType(); |
| 3878 | if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3879 | { |
| 3880 | if (log) |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3881 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type); |
| 3882 | |
| 3883 | switch (event_type) |
| 3884 | { |
| 3885 | case eBroadcastBitAsyncContinue: |
| 3886 | { |
| 3887 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
| 3888 | |
| 3889 | if (continue_packet) |
| 3890 | { |
| 3891 | const char *continue_cstr = (const char *)continue_packet->GetBytes (); |
| 3892 | const size_t continue_cstr_len = continue_packet->GetByteSize (); |
| 3893 | if (log) |
| 3894 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr); |
| 3895 | |
| 3896 | if (::strstr (continue_cstr, "vAttach") == NULL) |
| 3897 | process->SetPrivateState(eStateRunning); |
| 3898 | StringExtractorGDBRemote response; |
| 3899 | |
| 3900 | // If in Non-Stop-Mode |
| 3901 | if (process->GetTarget().GetNonStopModeEnabled()) |
| 3902 | { |
| 3903 | // send the vCont packet |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 3904 | if (!process->GetGDBRemote().SendvContPacket( |
| 3905 | llvm::StringRef(continue_cstr, continue_cstr_len), response)) |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3906 | { |
| 3907 | // Something went wrong |
| 3908 | done = true; |
| 3909 | break; |
| 3910 | } |
| 3911 | } |
| 3912 | // If in All-Stop-Mode |
| 3913 | else |
| 3914 | { |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 3915 | StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse( |
| 3916 | *process, *process->GetUnixSignals(), |
| 3917 | llvm::StringRef(continue_cstr, continue_cstr_len), response); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3918 | |
| 3919 | // We need to immediately clear the thread ID list so we are sure to get a valid list of threads. |
| 3920 | // The thread ID list might be contained within the "response", or the stop reply packet that |
| 3921 | // caused the stop. So clear it now before we give the stop reply packet to the process |
| 3922 | // using the process->SetLastStopPacket()... |
| 3923 | process->ClearThreadIDList (); |
| 3924 | |
| 3925 | switch (stop_state) |
| 3926 | { |
| 3927 | case eStateStopped: |
| 3928 | case eStateCrashed: |
| 3929 | case eStateSuspended: |
| 3930 | process->SetLastStopPacket (response); |
| 3931 | process->SetPrivateState (stop_state); |
| 3932 | break; |
| 3933 | |
| 3934 | case eStateExited: |
| 3935 | { |
| 3936 | process->SetLastStopPacket (response); |
| 3937 | process->ClearThreadIDList(); |
| 3938 | response.SetFilePos(1); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 3939 | |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3940 | int exit_status = response.GetHexU8(); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3941 | std::string desc_string; |
| 3942 | if (response.GetBytesLeft() > 0 && response.GetChar('-') == ';') |
| 3943 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 3944 | llvm::StringRef desc_str; |
| 3945 | llvm::StringRef desc_token; |
| 3946 | while (response.GetNameColonValue(desc_token, desc_str)) |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3947 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 3948 | if (desc_token != "description") |
| 3949 | continue; |
| 3950 | StringExtractor extractor(desc_str); |
| 3951 | extractor.GetHexByteString(desc_string); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3952 | } |
| 3953 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 3954 | process->SetExitStatus(exit_status, desc_string.c_str()); |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 3955 | done = true; |
| 3956 | break; |
| 3957 | } |
| 3958 | case eStateInvalid: |
| 3959 | { |
| 3960 | // Check to see if we were trying to attach and if we got back |
| 3961 | // the "E87" error code from debugserver -- this indicates that |
| 3962 | // the process is not debuggable. Return a slightly more helpful |
| 3963 | // error message about why the attach failed. |
| 3964 | if (::strstr (continue_cstr, "vAttach") != NULL |
| 3965 | && response.GetError() == 0x87) |
| 3966 | { |
| 3967 | process->SetExitStatus(-1, "cannot attach to process due to System Integrity Protection"); |
| 3968 | } |
| 3969 | // E01 code from vAttach means that the attach failed |
| 3970 | if (::strstr (continue_cstr, "vAttach") != NULL |
| 3971 | && response.GetError() == 0x1) |
| 3972 | { |
| 3973 | process->SetExitStatus(-1, "unable to attach"); |
| 3974 | } |
| 3975 | else |
| 3976 | { |
| 3977 | process->SetExitStatus(-1, "lost connection"); |
| 3978 | } |
| 3979 | break; |
| 3980 | } |
| 3981 | |
| 3982 | default: |
| 3983 | process->SetPrivateState (stop_state); |
| 3984 | break; |
| 3985 | } // switch(stop_state) |
| 3986 | } // else // if in All-stop-mode |
| 3987 | } // if (continue_packet) |
| 3988 | } // case eBroadcastBitAysncContinue |
| 3989 | break; |
| 3990 | |
| 3991 | case eBroadcastBitAsyncThreadShouldExit: |
| 3992 | if (log) |
| 3993 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID()); |
| 3994 | done = true; |
| 3995 | break; |
| 3996 | |
| 3997 | default: |
| 3998 | if (log) |
| 3999 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); |
| 4000 | done = true; |
| 4001 | break; |
| 4002 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4003 | } |
Pavel Labath | 5055685 | 2015-09-03 09:36:22 +0000 | [diff] [blame] | 4004 | else if (event_sp->BroadcasterIs (&process->m_gdb_comm)) |
| 4005 | { |
| 4006 | switch (event_type) |
| 4007 | { |
| 4008 | case Communication::eBroadcastBitReadThreadDidExit: |
| 4009 | process->SetExitStatus (-1, "lost connection"); |
| 4010 | done = true; |
| 4011 | break; |
| 4012 | |
| 4013 | case GDBRemoteCommunication::eBroadcastBitGdbReadThreadGotNotify: |
| 4014 | { |
| 4015 | lldb_private::Event *event = event_sp.get(); |
| 4016 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event); |
| 4017 | StringExtractorGDBRemote notify((const char*)continue_packet->GetBytes()); |
| 4018 | // Hand this over to the process to handle |
| 4019 | process->HandleNotifyPacket(notify); |
| 4020 | break; |
| 4021 | } |
| 4022 | |
| 4023 | default: |
| 4024 | if (log) |
| 4025 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); |
| 4026 | done = true; |
| 4027 | break; |
| 4028 | } |
| 4029 | } |
| 4030 | } |
| 4031 | else |
| 4032 | { |
| 4033 | if (log) |
| 4034 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID()); |
| 4035 | done = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4040 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4041 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4042 | return NULL; |
| 4043 | } |
| 4044 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4045 | //uint32_t |
| 4046 | //ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 4047 | //{ |
| 4048 | // // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver |
| 4049 | // // process and ask it for the list of processes. But if we are local, we can let the Host do it. |
| 4050 | // if (m_local_debugserver) |
| 4051 | // { |
| 4052 | // return Host::ListProcessesMatchingName (name, matches, pids); |
| 4053 | // } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4054 | // else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4055 | // { |
| 4056 | // // FIXME: Implement talking to the remote debugserver. |
| 4057 | // return 0; |
| 4058 | // } |
| 4059 | // |
| 4060 | //} |
| 4061 | // |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4062 | bool |
| 4063 | ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4064 | StoppointCallbackContext *context, |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4065 | lldb::user_id_t break_id, |
| 4066 | lldb::user_id_t break_loc_id) |
| 4067 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4068 | // I don't think I have to do anything here, just make sure I notice the new thread when it starts to |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4069 | // run so I can stop it if that's what I want to do. |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4070 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4071 | if (log) |
| 4072 | log->Printf("Hit New Thread Notification breakpoint."); |
| 4073 | return false; |
| 4074 | } |
| 4075 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 4076 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4077 | bool |
| 4078 | ProcessGDBRemote::StartNoticingNewThreads() |
| 4079 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4080 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4081 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4082 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4083 | if (log && log->GetVerbose()) |
| 4084 | log->Printf("Enabled noticing new thread breakpoint."); |
| 4085 | m_thread_create_bp_sp->SetEnabled(true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4086 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4087 | else |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4088 | { |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 4089 | PlatformSP platform_sp (GetTarget().GetPlatform()); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4090 | if (platform_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4091 | { |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 4092 | m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(GetTarget()); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4093 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4094 | { |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 4095 | if (log && log->GetVerbose()) |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4096 | log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID()); |
| 4097 | m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4098 | } |
| 4099 | else |
| 4100 | { |
| 4101 | if (log) |
| 4102 | log->Printf("Failed to create new thread notification breakpoint."); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4106 | return m_thread_create_bp_sp.get() != NULL; |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | bool |
| 4110 | ProcessGDBRemote::StopNoticingNewThreads() |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4111 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4112 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 4113 | if (log && log->GetVerbose()) |
Jim Ingham | 0e97cbc | 2011-02-08 05:19:01 +0000 | [diff] [blame] | 4114 | log->Printf ("Disabling new thread notification breakpoint."); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 4115 | |
| 4116 | if (m_thread_create_bp_sp) |
| 4117 | m_thread_create_bp_sp->SetEnabled(false); |
| 4118 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4119 | return true; |
| 4120 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4121 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4122 | DynamicLoader * |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 4123 | ProcessGDBRemote::GetDynamicLoader () |
| 4124 | { |
| 4125 | if (m_dyld_ap.get() == NULL) |
Jason Molenda | 2e56a25 | 2013-05-11 03:09:05 +0000 | [diff] [blame] | 4126 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 4127 | return m_dyld_ap.get(); |
| 4128 | } |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 4129 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 4130 | Error |
| 4131 | ProcessGDBRemote::SendEventData(const char *data) |
| 4132 | { |
| 4133 | int return_value; |
| 4134 | bool was_supported; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4135 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 4136 | Error error; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4137 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 4138 | return_value = m_gdb_comm.SendLaunchEventDataPacket (data, &was_supported); |
| 4139 | if (return_value != 0) |
| 4140 | { |
| 4141 | if (!was_supported) |
| 4142 | error.SetErrorString("Sending events is not supported for this process."); |
| 4143 | else |
| 4144 | error.SetErrorStringWithFormat("Error sending event data: %d.", return_value); |
| 4145 | } |
| 4146 | return error; |
| 4147 | } |
| 4148 | |
Steve Pucci | 03904ac | 2014-03-04 23:18:46 +0000 | [diff] [blame] | 4149 | const DataBufferSP |
| 4150 | ProcessGDBRemote::GetAuxvData() |
| 4151 | { |
| 4152 | DataBufferSP buf; |
| 4153 | if (m_gdb_comm.GetQXferAuxvReadSupported()) |
| 4154 | { |
| 4155 | std::string response_string; |
| 4156 | if (m_gdb_comm.SendPacketsAndConcatenateResponses("qXfer:auxv:read::", response_string) == GDBRemoteCommunication::PacketResult::Success) |
| 4157 | buf.reset(new DataBufferHeap(response_string.c_str(), response_string.length())); |
| 4158 | } |
| 4159 | return buf; |
| 4160 | } |
| 4161 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 4162 | StructuredData::ObjectSP |
| 4163 | ProcessGDBRemote::GetExtendedInfoForThread (lldb::tid_t tid) |
| 4164 | { |
| 4165 | StructuredData::ObjectSP object_sp; |
| 4166 | |
| 4167 | if (m_gdb_comm.GetThreadExtendedInfoSupported()) |
| 4168 | { |
| 4169 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 4170 | SystemRuntime *runtime = GetSystemRuntime(); |
| 4171 | if (runtime) |
| 4172 | { |
| 4173 | runtime->AddThreadExtendedInfoPacketHints (args_dict); |
| 4174 | } |
| 4175 | args_dict->GetAsDictionary()->AddIntegerItem ("thread", tid); |
| 4176 | |
| 4177 | StreamString packet; |
| 4178 | packet << "jThreadExtendedInfo:"; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 4179 | args_dict->Dump (packet, false); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 4180 | |
| 4181 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 4182 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 4183 | // these characters in its packet output -- so we add the quoted version |
| 4184 | // of the } character here manually in case we talk to a debugserver which |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 4185 | // un-escapes the characters at packet read time. |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 4186 | packet << (char) (0x7d ^ 0x20); |
| 4187 | |
| 4188 | StringExtractorGDBRemote response; |
Greg Clayton | 830c81d | 2016-04-01 00:41:29 +0000 | [diff] [blame] | 4189 | response.SetResponseValidatorToJSON(); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 4190 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success) |
| 4191 | { |
| 4192 | StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType(); |
| 4193 | if (response_type == StringExtractorGDBRemote::eResponse) |
| 4194 | { |
| 4195 | if (!response.Empty()) |
| 4196 | { |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 4197 | object_sp = StructuredData::ParseJSON (response.GetStringRef()); |
| 4198 | } |
| 4199 | } |
| 4200 | } |
| 4201 | } |
| 4202 | return object_sp; |
| 4203 | } |
| 4204 | |
| 4205 | StructuredData::ObjectSP |
| 4206 | ProcessGDBRemote::GetLoadedDynamicLibrariesInfos (lldb::addr_t image_list_address, lldb::addr_t image_count) |
| 4207 | { |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 4208 | |
| 4209 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 4210 | args_dict->GetAsDictionary()->AddIntegerItem ("image_list_address", image_list_address); |
| 4211 | args_dict->GetAsDictionary()->AddIntegerItem ("image_count", image_count); |
| 4212 | |
| 4213 | return GetLoadedDynamicLibrariesInfos_sender (args_dict); |
| 4214 | } |
| 4215 | |
| 4216 | StructuredData::ObjectSP |
| 4217 | ProcessGDBRemote::GetLoadedDynamicLibrariesInfos () |
| 4218 | { |
| 4219 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 4220 | |
| 4221 | args_dict->GetAsDictionary()->AddBooleanItem ("fetch_all_solibs", true); |
| 4222 | |
| 4223 | return GetLoadedDynamicLibrariesInfos_sender (args_dict); |
| 4224 | } |
| 4225 | |
| 4226 | StructuredData::ObjectSP |
| 4227 | ProcessGDBRemote::GetLoadedDynamicLibrariesInfos (const std::vector<lldb::addr_t> &load_addresses) |
| 4228 | { |
| 4229 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 4230 | StructuredData::ArraySP addresses(new StructuredData::Array); |
| 4231 | |
| 4232 | for (auto addr : load_addresses) |
| 4233 | { |
| 4234 | StructuredData::ObjectSP addr_sp (new StructuredData::Integer (addr)); |
| 4235 | addresses->AddItem (addr_sp); |
| 4236 | } |
| 4237 | |
| 4238 | args_dict->GetAsDictionary()->AddItem ("solib_addresses", addresses); |
| 4239 | |
| 4240 | return GetLoadedDynamicLibrariesInfos_sender (args_dict); |
| 4241 | } |
| 4242 | |
| 4243 | StructuredData::ObjectSP |
| 4244 | ProcessGDBRemote::GetLoadedDynamicLibrariesInfos_sender (StructuredData::ObjectSP args_dict) |
| 4245 | { |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 4246 | StructuredData::ObjectSP object_sp; |
| 4247 | |
| 4248 | if (m_gdb_comm.GetLoadedDynamicLibrariesInfosSupported()) |
| 4249 | { |
Jason Molenda | 1d3b356 | 2015-11-05 23:54:29 +0000 | [diff] [blame] | 4250 | // Scope for the scoped timeout object |
| 4251 | GDBRemoteCommunication::ScopedTimeout timeout (m_gdb_comm, 10); |
| 4252 | |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 4253 | StreamString packet; |
| 4254 | packet << "jGetLoadedDynamicLibrariesInfos:"; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 4255 | args_dict->Dump (packet, false); |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 4256 | |
| 4257 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 4258 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 4259 | // these characters in its packet output -- so we add the quoted version |
| 4260 | // of the } character here manually in case we talk to a debugserver which |
| 4261 | // un-escapes the characters at packet read time. |
| 4262 | packet << (char) (0x7d ^ 0x20); |
| 4263 | |
| 4264 | StringExtractorGDBRemote response; |
Greg Clayton | 830c81d | 2016-04-01 00:41:29 +0000 | [diff] [blame] | 4265 | response.SetResponseValidatorToJSON(); |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 4266 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success) |
| 4267 | { |
| 4268 | StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType(); |
| 4269 | if (response_type == StringExtractorGDBRemote::eResponse) |
| 4270 | { |
| 4271 | if (!response.Empty()) |
| 4272 | { |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 4273 | object_sp = StructuredData::ParseJSON (response.GetStringRef()); |
| 4274 | } |
| 4275 | } |
| 4276 | } |
| 4277 | } |
| 4278 | return object_sp; |
| 4279 | } |
| 4280 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 4281 | |
| 4282 | |
| 4283 | |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 4284 | StructuredData::ObjectSP |
| 4285 | ProcessGDBRemote::GetSharedCacheInfo () |
| 4286 | { |
| 4287 | StructuredData::ObjectSP object_sp; |
| 4288 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 4289 | |
| 4290 | if (m_gdb_comm.GetSharedCacheInfoSupported()) |
| 4291 | { |
| 4292 | StreamString packet; |
| 4293 | packet << "jGetSharedCacheInfo:"; |
| 4294 | args_dict->Dump (packet, false); |
| 4295 | |
| 4296 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 4297 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 4298 | // these characters in its packet output -- so we add the quoted version |
| 4299 | // of the } character here manually in case we talk to a debugserver which |
| 4300 | // un-escapes the characters at packet read time. |
| 4301 | packet << (char) (0x7d ^ 0x20); |
| 4302 | |
| 4303 | StringExtractorGDBRemote response; |
| 4304 | response.SetResponseValidatorToJSON(); |
| 4305 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success) |
| 4306 | { |
| 4307 | StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType(); |
| 4308 | if (response_type == StringExtractorGDBRemote::eResponse) |
| 4309 | { |
| 4310 | if (!response.Empty()) |
| 4311 | { |
| 4312 | object_sp = StructuredData::ParseJSON (response.GetStringRef()); |
| 4313 | } |
| 4314 | } |
| 4315 | } |
| 4316 | } |
| 4317 | return object_sp; |
| 4318 | } |
| 4319 | |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4320 | Error |
| 4321 | ProcessGDBRemote::ConfigureStructuredData(const ConstString &type_name, |
| 4322 | const StructuredData::ObjectSP |
| 4323 | &config_sp) |
| 4324 | { |
| 4325 | return m_gdb_comm.ConfigureRemoteStructuredData(type_name, config_sp); |
| 4326 | } |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 4327 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4328 | // Establish the largest memory read/write payloads we should use. |
| 4329 | // 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] | 4330 | // |
| 4331 | // 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] | 4332 | // reasonable largeish default. |
| 4333 | // |
| 4334 | // If the remote stub doesn't advertise a max packet size, use a |
| 4335 | // conservative default. |
| 4336 | |
| 4337 | void |
| 4338 | ProcessGDBRemote::GetMaxMemorySize() |
| 4339 | { |
| 4340 | const uint64_t reasonable_largeish_default = 128 * 1024; |
| 4341 | const uint64_t conservative_default = 512; |
| 4342 | |
| 4343 | if (m_max_memory_size == 0) |
| 4344 | { |
| 4345 | uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize(); |
| 4346 | if (stub_max_size != UINT64_MAX && stub_max_size != 0) |
| 4347 | { |
| 4348 | // Save the stub's claimed maximum packet size |
| 4349 | m_remote_stub_max_memory_size = stub_max_size; |
| 4350 | |
| 4351 | // Even if the stub says it can support ginormous packets, |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 4352 | // don't exceed our reasonable largeish default packet size. |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4353 | if (stub_max_size > reasonable_largeish_default) |
| 4354 | { |
| 4355 | stub_max_size = reasonable_largeish_default; |
| 4356 | } |
| 4357 | |
| 4358 | m_max_memory_size = stub_max_size; |
| 4359 | } |
| 4360 | else |
| 4361 | { |
| 4362 | m_max_memory_size = conservative_default; |
| 4363 | } |
| 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | void |
| 4368 | ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max) |
| 4369 | { |
| 4370 | if (user_specified_max != 0) |
| 4371 | { |
| 4372 | GetMaxMemorySize (); |
| 4373 | |
| 4374 | if (m_remote_stub_max_memory_size != 0) |
| 4375 | { |
| 4376 | if (m_remote_stub_max_memory_size < user_specified_max) |
| 4377 | { |
| 4378 | m_max_memory_size = m_remote_stub_max_memory_size; // user specified a packet size too big, go as big |
| 4379 | // as the remote stub says we can go. |
| 4380 | } |
| 4381 | else |
| 4382 | { |
| 4383 | m_max_memory_size = user_specified_max; // user's packet size is good |
| 4384 | } |
| 4385 | } |
| 4386 | else |
| 4387 | { |
| 4388 | m_max_memory_size = user_specified_max; // user's packet size is probably fine |
| 4389 | } |
| 4390 | } |
| 4391 | } |
| 4392 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4393 | bool |
| 4394 | ProcessGDBRemote::GetModuleSpec(const FileSpec& module_file_spec, |
| 4395 | const ArchSpec& arch, |
| 4396 | ModuleSpec &module_spec) |
| 4397 | { |
| 4398 | Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM); |
| 4399 | |
| 4400 | if (!m_gdb_comm.GetModuleInfo (module_file_spec, arch, module_spec)) |
| 4401 | { |
| 4402 | if (log) |
| 4403 | log->Printf ("ProcessGDBRemote::%s - failed to get module info for %s:%s", |
| 4404 | __FUNCTION__, module_file_spec.GetPath ().c_str (), |
| 4405 | arch.GetTriple ().getTriple ().c_str ()); |
| 4406 | return false; |
| 4407 | } |
| 4408 | |
| 4409 | if (log) |
| 4410 | { |
| 4411 | StreamString stream; |
| 4412 | module_spec.Dump (stream); |
| 4413 | log->Printf ("ProcessGDBRemote::%s - got module info for (%s:%s) : %s", |
| 4414 | __FUNCTION__, module_file_spec.GetPath ().c_str (), |
| 4415 | arch.GetTriple ().getTriple ().c_str (), stream.GetString ().c_str ()); |
| 4416 | } |
| 4417 | |
| 4418 | return true; |
| 4419 | } |
| 4420 | |
Jim Ingham | 13c30d2 | 2015-11-05 22:33:17 +0000 | [diff] [blame] | 4421 | bool |
| 4422 | ProcessGDBRemote::GetHostOSVersion(uint32_t &major, |
| 4423 | uint32_t &minor, |
| 4424 | uint32_t &update) |
| 4425 | { |
| 4426 | if (m_gdb_comm.GetOSVersion(major, minor, update)) |
| 4427 | return true; |
| 4428 | // We failed to get the host OS version, defer to the base |
| 4429 | // implementation to correctly invalidate the arguments. |
| 4430 | return Process::GetHostOSVersion(major, minor, update); |
| 4431 | } |
| 4432 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4433 | namespace { |
| 4434 | |
| 4435 | typedef std::vector<std::string> stringVec; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4436 | |
| 4437 | typedef std::vector<struct GdbServerRegisterInfo> GDBServerRegisterVec; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4438 | struct RegisterSetInfo |
| 4439 | { |
| 4440 | ConstString name; |
| 4441 | }; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4442 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4443 | typedef std::map<uint32_t, RegisterSetInfo> RegisterSetMap; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4444 | |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4445 | struct GdbServerTargetInfo |
| 4446 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4447 | std::string arch; |
| 4448 | std::string osabi; |
| 4449 | stringVec includes; |
| 4450 | RegisterSetMap reg_set_map; |
| 4451 | XMLNode feature_node; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4452 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4453 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4454 | bool |
Francis Ricci | 55954ae | 2016-04-25 21:03:55 +0000 | [diff] [blame] | 4455 | ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemoteDynamicRegisterInfo &dyn_reg_info, ABISP abi_sp, uint32_t &cur_reg_num, uint32_t ®_offset) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4456 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4457 | if (!feature_node) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4458 | return false; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4459 | |
Jason Molenda | 63bd0db | 2015-09-15 23:20:34 +0000 | [diff] [blame] | 4460 | feature_node.ForEachChildElementWithName("reg", [&target_info, &dyn_reg_info, &cur_reg_num, ®_offset, &abi_sp](const XMLNode ®_node) -> bool { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4461 | std::string gdb_group; |
| 4462 | std::string gdb_type; |
| 4463 | ConstString reg_name; |
| 4464 | ConstString alt_name; |
| 4465 | ConstString set_name; |
| 4466 | std::vector<uint32_t> value_regs; |
| 4467 | std::vector<uint32_t> invalidate_regs; |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4468 | std::vector<uint8_t> dwarf_opcode_bytes; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4469 | bool encoding_set = false; |
| 4470 | bool format_set = false; |
| 4471 | RegisterInfo reg_info = { NULL, // Name |
| 4472 | NULL, // Alt name |
| 4473 | 0, // byte size |
| 4474 | reg_offset, // offset |
| 4475 | eEncodingUint, // encoding |
Jason Molenda | bf67a30 | 2015-09-01 05:17:01 +0000 | [diff] [blame] | 4476 | eFormatHex, // format |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4477 | { |
Jason Molenda | bf67a30 | 2015-09-01 05:17:01 +0000 | [diff] [blame] | 4478 | LLDB_INVALID_REGNUM, // eh_frame reg num |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4479 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 4480 | LLDB_INVALID_REGNUM, // generic reg num |
Jason Molenda | 63bd0db | 2015-09-15 23:20:34 +0000 | [diff] [blame] | 4481 | cur_reg_num, // process plugin reg num |
| 4482 | cur_reg_num // native register number |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4483 | }, |
| 4484 | NULL, |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4485 | NULL, |
| 4486 | NULL, // Dwarf Expression opcode bytes pointer |
| 4487 | 0 // Dwarf Expression opcode bytes length |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4488 | }; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4489 | |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4490 | reg_node.ForEachAttribute([&target_info, &gdb_group, &gdb_type, ®_name, &alt_name, &set_name, &value_regs, &invalidate_regs, &encoding_set, &format_set, ®_info, &cur_reg_num, ®_offset, &dwarf_opcode_bytes](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4491 | if (name == "name") |
| 4492 | { |
| 4493 | reg_name.SetString(value); |
| 4494 | } |
| 4495 | else if (name == "bitsize") |
| 4496 | { |
| 4497 | reg_info.byte_size = StringConvert::ToUInt32(value.data(), 0, 0) / CHAR_BIT; |
| 4498 | } |
| 4499 | else if (name == "type") |
| 4500 | { |
| 4501 | gdb_type = value.str(); |
| 4502 | } |
| 4503 | else if (name == "group") |
| 4504 | { |
| 4505 | gdb_group = value.str(); |
| 4506 | } |
| 4507 | else if (name == "regnum") |
| 4508 | { |
| 4509 | const uint32_t regnum = StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 4510 | if (regnum != LLDB_INVALID_REGNUM) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4511 | { |
Jason Molenda | 63bd0db | 2015-09-15 23:20:34 +0000 | [diff] [blame] | 4512 | reg_info.kinds[eRegisterKindProcessPlugin] = regnum; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4513 | } |
| 4514 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4515 | else if (name == "offset") |
| 4516 | { |
| 4517 | reg_offset = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 4518 | } |
| 4519 | else if (name == "altname") |
| 4520 | { |
| 4521 | alt_name.SetString(value); |
| 4522 | } |
| 4523 | else if (name == "encoding") |
| 4524 | { |
| 4525 | encoding_set = true; |
| 4526 | reg_info.encoding = Args::StringToEncoding (value.data(), eEncodingUint); |
| 4527 | } |
| 4528 | else if (name == "format") |
| 4529 | { |
| 4530 | format_set = true; |
| 4531 | Format format = eFormatInvalid; |
| 4532 | if (Args::StringToFormat (value.data(), format, NULL).Success()) |
| 4533 | reg_info.format = format; |
| 4534 | else if (value == "vector-sint8") |
| 4535 | reg_info.format = eFormatVectorOfSInt8; |
| 4536 | else if (value == "vector-uint8") |
| 4537 | reg_info.format = eFormatVectorOfUInt8; |
| 4538 | else if (value == "vector-sint16") |
| 4539 | reg_info.format = eFormatVectorOfSInt16; |
| 4540 | else if (value == "vector-uint16") |
| 4541 | reg_info.format = eFormatVectorOfUInt16; |
| 4542 | else if (value == "vector-sint32") |
| 4543 | reg_info.format = eFormatVectorOfSInt32; |
| 4544 | else if (value == "vector-uint32") |
| 4545 | reg_info.format = eFormatVectorOfUInt32; |
| 4546 | else if (value == "vector-float32") |
| 4547 | reg_info.format = eFormatVectorOfFloat32; |
| 4548 | else if (value == "vector-uint128") |
| 4549 | reg_info.format = eFormatVectorOfUInt128; |
| 4550 | } |
| 4551 | else if (name == "group_id") |
| 4552 | { |
| 4553 | const uint32_t set_id = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 4554 | RegisterSetMap::const_iterator pos = target_info.reg_set_map.find(set_id); |
| 4555 | if (pos != target_info.reg_set_map.end()) |
| 4556 | set_name = pos->second.name; |
| 4557 | } |
Jason Molenda | a18f707 | 2015-08-15 01:21:01 +0000 | [diff] [blame] | 4558 | else if (name == "gcc_regnum" || name == "ehframe_regnum") |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4559 | { |
Jason Molenda | a18f707 | 2015-08-15 01:21:01 +0000 | [diff] [blame] | 4560 | reg_info.kinds[eRegisterKindEHFrame] = StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4561 | } |
| 4562 | else if (name == "dwarf_regnum") |
| 4563 | { |
| 4564 | reg_info.kinds[eRegisterKindDWARF] = StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 4565 | } |
| 4566 | else if (name == "generic") |
| 4567 | { |
| 4568 | reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister(value.data()); |
| 4569 | } |
| 4570 | else if (name == "value_regnums") |
| 4571 | { |
| 4572 | SplitCommaSeparatedRegisterNumberString(value, value_regs, 0); |
| 4573 | } |
| 4574 | else if (name == "invalidate_regnums") |
| 4575 | { |
| 4576 | SplitCommaSeparatedRegisterNumberString(value, invalidate_regs, 0); |
| 4577 | } |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4578 | else if (name == "dynamic_size_dwarf_expr_bytes") |
| 4579 | { |
| 4580 | StringExtractor opcode_extractor; |
| 4581 | std::string opcode_string = value.str (); |
| 4582 | size_t dwarf_opcode_len = opcode_string.length () / 2; |
| 4583 | assert (dwarf_opcode_len > 0); |
| 4584 | |
| 4585 | dwarf_opcode_bytes.resize (dwarf_opcode_len); |
| 4586 | reg_info.dynamic_size_dwarf_len = dwarf_opcode_len; |
| 4587 | opcode_extractor.GetStringRef ().swap (opcode_string); |
Zachary Turner | d08f09c | 2016-08-30 18:12:11 +0000 | [diff] [blame] | 4588 | uint32_t ret_val = opcode_extractor.GetHexBytesAvail (dwarf_opcode_bytes); |
Nitesh Jain | 52b6cc5 | 2016-08-01 13:45:51 +0000 | [diff] [blame] | 4589 | assert (dwarf_opcode_len == ret_val); |
| 4590 | |
| 4591 | reg_info.dynamic_size_dwarf_expr_bytes = dwarf_opcode_bytes.data (); |
| 4592 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4593 | else |
| 4594 | { |
| 4595 | printf("unhandled attribute %s = %s\n", name.data(), value.data()); |
| 4596 | } |
| 4597 | return true; // Keep iterating through all attributes |
| 4598 | }); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4599 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4600 | if (!gdb_type.empty() && !(encoding_set || format_set)) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4601 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4602 | if (gdb_type.find("int") == 0) |
| 4603 | { |
| 4604 | reg_info.format = eFormatHex; |
| 4605 | reg_info.encoding = eEncodingUint; |
| 4606 | } |
| 4607 | else if (gdb_type == "data_ptr" || gdb_type == "code_ptr") |
| 4608 | { |
| 4609 | reg_info.format = eFormatAddressInfo; |
| 4610 | reg_info.encoding = eEncodingUint; |
| 4611 | } |
| 4612 | else if (gdb_type == "i387_ext" || gdb_type == "float") |
| 4613 | { |
| 4614 | reg_info.format = eFormatFloat; |
| 4615 | reg_info.encoding = eEncodingIEEE754; |
| 4616 | } |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4617 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4618 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4619 | // Only update the register set name if we didn't get a "reg_set" attribute. |
| 4620 | // "set_name" will be empty if we didn't have a "reg_set" attribute. |
| 4621 | if (!set_name && !gdb_group.empty()) |
| 4622 | set_name.SetCString(gdb_group.c_str()); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4623 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4624 | reg_info.byte_offset = reg_offset; |
| 4625 | assert (reg_info.byte_size != 0); |
| 4626 | reg_offset += reg_info.byte_size; |
| 4627 | if (!value_regs.empty()) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4628 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4629 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 4630 | reg_info.value_regs = value_regs.data(); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4631 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4632 | if (!invalidate_regs.empty()) |
| 4633 | { |
| 4634 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 4635 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 4636 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4637 | |
Jason Molenda | 63bd0db | 2015-09-15 23:20:34 +0000 | [diff] [blame] | 4638 | ++cur_reg_num; |
Jason Molenda | 21586c8 | 2015-09-09 03:36:24 +0000 | [diff] [blame] | 4639 | AugmentRegisterInfoViaABI (reg_info, reg_name, abi_sp); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4640 | dyn_reg_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4641 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4642 | return true; // Keep iterating through all "reg" elements |
| 4643 | }); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4644 | return true; |
| 4645 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4646 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 4647 | } // namespace {} |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4648 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4649 | |
| 4650 | // query the target of gdb-remote for extended target information |
| 4651 | // return: 'true' on success |
| 4652 | // 'false' on failure |
| 4653 | bool |
Jim Ingham | 7b71c0b | 2016-02-18 23:58:45 +0000 | [diff] [blame] | 4654 | ProcessGDBRemote::GetGDBServerRegisterInfo (ArchSpec &arch_to_use) |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4655 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4656 | // Make sure LLDB has an XML parser it can use first |
| 4657 | if (!XMLDocument::XMLEnabled()) |
| 4658 | return false; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4659 | |
| 4660 | // redirect libxml2's error handler since the default prints to stdout |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4661 | |
| 4662 | GDBRemoteCommunicationClient & comm = m_gdb_comm; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4663 | |
| 4664 | // check that we have extended feature read support |
| 4665 | if ( !comm.GetQXferFeaturesReadSupported( ) ) |
| 4666 | return false; |
| 4667 | |
| 4668 | // request the target xml file |
| 4669 | std::string raw; |
| 4670 | lldb_private::Error lldberr; |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4671 | if (!comm.ReadExtFeature(ConstString("features"), |
| 4672 | ConstString("target.xml"), |
| 4673 | raw, |
| 4674 | lldberr)) |
| 4675 | { |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4676 | return false; |
| 4677 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4678 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4679 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4680 | XMLDocument xml_document; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4681 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4682 | if (xml_document.ParseMemory(raw.c_str(), raw.size(), "target.xml")) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4683 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4684 | GdbServerTargetInfo target_info; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4685 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4686 | XMLNode target_node = xml_document.GetRootElement("target"); |
| 4687 | if (target_node) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 4688 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4689 | XMLNode feature_node; |
| 4690 | target_node.ForEachChildElement([&target_info, this, &feature_node](const XMLNode &node) -> bool |
| 4691 | { |
| 4692 | llvm::StringRef name = node.GetName(); |
| 4693 | if (name == "architecture") |
| 4694 | { |
| 4695 | node.GetElementText(target_info.arch); |
| 4696 | } |
| 4697 | else if (name == "osabi") |
| 4698 | { |
| 4699 | node.GetElementText(target_info.osabi); |
| 4700 | } |
Ewan Crawford | 682e842 | 2015-06-26 09:38:27 +0000 | [diff] [blame] | 4701 | else if (name == "xi:include" || name == "include") |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4702 | { |
| 4703 | llvm::StringRef href = node.GetAttributeValue("href"); |
| 4704 | if (!href.empty()) |
| 4705 | target_info.includes.push_back(href.str()); |
| 4706 | } |
| 4707 | else if (name == "feature") |
| 4708 | { |
| 4709 | feature_node = node; |
| 4710 | } |
| 4711 | else if (name == "groups") |
| 4712 | { |
| 4713 | node.ForEachChildElementWithName("group", [&target_info](const XMLNode &node) -> bool { |
| 4714 | uint32_t set_id = UINT32_MAX; |
| 4715 | RegisterSetInfo set_info; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4716 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4717 | node.ForEachAttribute([&set_id, &set_info](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { |
| 4718 | if (name == "id") |
| 4719 | set_id = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 4720 | if (name == "name") |
| 4721 | set_info.name = ConstString(value); |
| 4722 | return true; // Keep iterating through all attributes |
| 4723 | }); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4724 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4725 | if (set_id != UINT32_MAX) |
| 4726 | target_info.reg_set_map[set_id] = set_info; |
| 4727 | return true; // Keep iterating through all "group" elements |
| 4728 | }); |
| 4729 | } |
| 4730 | return true; // Keep iterating through all children of the target_node |
| 4731 | }); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4732 | |
Francis Ricci | 55954ae | 2016-04-25 21:03:55 +0000 | [diff] [blame] | 4733 | // Initialize these outside of ParseRegisters, since they should not be reset inside each include feature |
| 4734 | uint32_t cur_reg_num = 0; |
| 4735 | uint32_t reg_offset = 0; |
| 4736 | |
Jim Ingham | 7b71c0b | 2016-02-18 23:58:45 +0000 | [diff] [blame] | 4737 | // Don't use Process::GetABI, this code gets called from DidAttach, and in that context we haven't |
| 4738 | // set the Target's architecture yet, so the ABI is also potentially incorrect. |
| 4739 | ABISP abi_to_use_sp = ABI::FindPlugin(arch_to_use); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4740 | if (feature_node) |
| 4741 | { |
Francis Ricci | 55954ae | 2016-04-25 21:03:55 +0000 | [diff] [blame] | 4742 | ParseRegisters(feature_node, target_info, this->m_register_info, abi_to_use_sp, cur_reg_num, reg_offset); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4743 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4744 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4745 | for (const auto &include : target_info.includes) |
| 4746 | { |
| 4747 | // request register file |
| 4748 | std::string xml_data; |
| 4749 | if (!comm.ReadExtFeature(ConstString("features"), |
| 4750 | ConstString(include), |
| 4751 | xml_data, |
| 4752 | lldberr)) |
| 4753 | continue; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4754 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4755 | XMLDocument include_xml_document; |
| 4756 | include_xml_document.ParseMemory(xml_data.data(), xml_data.size(), include.c_str()); |
| 4757 | XMLNode include_feature_node = include_xml_document.GetRootElement("feature"); |
| 4758 | if (include_feature_node) |
| 4759 | { |
Francis Ricci | 55954ae | 2016-04-25 21:03:55 +0000 | [diff] [blame] | 4760 | ParseRegisters(include_feature_node, target_info, this->m_register_info, abi_to_use_sp, cur_reg_num, reg_offset); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4761 | } |
| 4762 | } |
Jim Ingham | 7b71c0b | 2016-02-18 23:58:45 +0000 | [diff] [blame] | 4763 | this->m_register_info.Finalize(arch_to_use); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4767 | return m_register_info.GetNumRegisters() > 0; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4770 | Error |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 4771 | ProcessGDBRemote::GetLoadedModuleList (LoadedModuleInfoList & list) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4772 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4773 | // Make sure LLDB has an XML parser it can use first |
| 4774 | if (!XMLDocument::XMLEnabled()) |
| 4775 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 4776 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4777 | Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 4778 | if (log) |
| 4779 | log->Printf ("ProcessGDBRemote::%s", __FUNCTION__); |
| 4780 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4781 | GDBRemoteCommunicationClient & comm = m_gdb_comm; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4782 | |
| 4783 | // check that we have extended feature read support |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4784 | if (comm.GetQXferLibrariesSVR4ReadSupported ()) { |
| 4785 | list.clear (); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4786 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4787 | // request the loaded library list |
| 4788 | std::string raw; |
| 4789 | lldb_private::Error lldberr; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4790 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4791 | if (!comm.ReadExtFeature (ConstString ("libraries-svr4"), ConstString (""), raw, lldberr)) |
| 4792 | return Error (0, ErrorType::eErrorTypeGeneric); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4793 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4794 | // parse the xml file in memory |
| 4795 | if (log) |
| 4796 | log->Printf ("parsing: %s", raw.c_str()); |
| 4797 | XMLDocument doc; |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4798 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4799 | if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) |
| 4800 | return Error (0, ErrorType::eErrorTypeGeneric); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4801 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4802 | XMLNode root_element = doc.GetRootElement("library-list-svr4"); |
| 4803 | if (!root_element) |
| 4804 | return Error(); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4805 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4806 | // main link map structure |
| 4807 | llvm::StringRef main_lm = root_element.GetAttributeValue("main-lm"); |
| 4808 | if (!main_lm.empty()) |
| 4809 | { |
| 4810 | list.m_link_map = StringConvert::ToUInt64(main_lm.data(), LLDB_INVALID_ADDRESS, 0); |
| 4811 | } |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4812 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4813 | root_element.ForEachChildElementWithName("library", [log, &list](const XMLNode &library) -> bool { |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4814 | |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 4815 | LoadedModuleInfoList::LoadedModuleInfo module; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4816 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4817 | library.ForEachAttribute([log, &module](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4818 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4819 | if (name == "name") |
| 4820 | module.set_name (value.str()); |
| 4821 | else if (name == "lm") |
| 4822 | { |
| 4823 | // the address of the link_map struct. |
| 4824 | module.set_link_map(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0)); |
| 4825 | } |
| 4826 | else if (name == "l_addr") |
| 4827 | { |
| 4828 | // the displacement as read from the field 'l_addr' of the link_map struct. |
| 4829 | module.set_base(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0)); |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4830 | // base address is always a displacement, not an absolute value. |
| 4831 | module.set_base_is_offset(true); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4832 | } |
| 4833 | else if (name == "l_ld") |
| 4834 | { |
| 4835 | // the memory address of the libraries PT_DYAMIC section. |
| 4836 | module.set_dynamic(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0)); |
| 4837 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 4838 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4839 | return true; // Keep iterating over all properties of "library" |
| 4840 | }); |
| 4841 | |
| 4842 | if (log) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4843 | { |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4844 | std::string name; |
| 4845 | lldb::addr_t lm=0, base=0, ld=0; |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4846 | bool base_is_offset; |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4847 | |
| 4848 | module.get_name (name); |
| 4849 | module.get_link_map (lm); |
| 4850 | module.get_base (base); |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4851 | module.get_base_is_offset (base_is_offset); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4852 | module.get_dynamic (ld); |
| 4853 | |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4854 | log->Printf ("found (link_map:0x%08" PRIx64 ", base:0x%08" PRIx64 "[%s], ld:0x%08" PRIx64 ", name:'%s')", lm, base, (base_is_offset ? "offset" : "absolute"), ld, name.c_str()); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4855 | } |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4856 | |
| 4857 | list.add (module); |
| 4858 | return true; // Keep iterating over all "library" elements in the root node |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4859 | }); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4860 | |
| 4861 | if (log) |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4862 | log->Printf ("found %" PRId32 " modules in total", (int) list.m_list.size()); |
| 4863 | } else if (comm.GetQXferLibrariesReadSupported ()) { |
| 4864 | list.clear (); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4865 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4866 | // request the loaded library list |
| 4867 | std::string raw; |
| 4868 | lldb_private::Error lldberr; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4869 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4870 | if (!comm.ReadExtFeature (ConstString ("libraries"), ConstString (""), raw, lldberr)) |
| 4871 | return Error (0, ErrorType::eErrorTypeGeneric); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4872 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4873 | if (log) |
| 4874 | log->Printf ("parsing: %s", raw.c_str()); |
| 4875 | XMLDocument doc; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4876 | |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4877 | if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) |
| 4878 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 4879 | |
| 4880 | XMLNode root_element = doc.GetRootElement("library-list"); |
| 4881 | if (!root_element) |
| 4882 | return Error(); |
| 4883 | |
| 4884 | root_element.ForEachChildElementWithName("library", [log, &list](const XMLNode &library) -> bool { |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 4885 | LoadedModuleInfoList::LoadedModuleInfo module; |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4886 | |
| 4887 | llvm::StringRef name = library.GetAttributeValue("name"); |
| 4888 | module.set_name(name.str()); |
| 4889 | |
| 4890 | // The base address of a given library will be the address of its |
| 4891 | // first section. Most remotes send only one section for Windows |
| 4892 | // targets for example. |
| 4893 | const XMLNode §ion = library.FindFirstChildElementWithName("section"); |
| 4894 | llvm::StringRef address = section.GetAttributeValue("address"); |
| 4895 | module.set_base(StringConvert::ToUInt64(address.data(), LLDB_INVALID_ADDRESS, 0)); |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4896 | // These addresses are absolute values. |
| 4897 | module.set_base_is_offset(false); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4898 | |
| 4899 | if (log) |
| 4900 | { |
| 4901 | std::string name; |
| 4902 | lldb::addr_t base = 0; |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4903 | bool base_is_offset; |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4904 | module.get_name (name); |
| 4905 | module.get_base (base); |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4906 | module.get_base_is_offset (base_is_offset); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4907 | |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4908 | log->Printf ("found (base:0x%08" PRIx64 "[%s], name:'%s')", base, (base_is_offset ? "offset" : "absolute"), name.c_str()); |
Stephane Sezer | 9a7cacb | 2015-07-08 19:14:03 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | list.add (module); |
| 4912 | return true; // Keep iterating over all "library" elements in the root node |
| 4913 | }); |
| 4914 | |
| 4915 | if (log) |
| 4916 | log->Printf ("found %" PRId32 " modules in total", (int) list.m_list.size()); |
| 4917 | } else { |
| 4918 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 4919 | } |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4920 | |
| 4921 | return Error(); |
| 4922 | } |
| 4923 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4924 | lldb::ModuleSP |
Stephane Sezer | 3553c0e | 2016-04-05 17:25:32 +0000 | [diff] [blame] | 4925 | ProcessGDBRemote::LoadModuleAtAddress (const FileSpec &file, lldb::addr_t link_map, |
| 4926 | lldb::addr_t base_addr, bool value_is_offset) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4927 | { |
Stephane Sezer | 3553c0e | 2016-04-05 17:25:32 +0000 | [diff] [blame] | 4928 | DynamicLoader *loader = GetDynamicLoader(); |
| 4929 | if (!loader) |
| 4930 | return nullptr; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4931 | |
Stephane Sezer | 3553c0e | 2016-04-05 17:25:32 +0000 | [diff] [blame] | 4932 | return loader->LoadModuleAtAddress(file, link_map, base_addr, value_is_offset); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | size_t |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 4936 | ProcessGDBRemote::LoadModules (LoadedModuleInfoList &module_list) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4937 | { |
| 4938 | using lldb_private::process_gdb_remote::ProcessGDBRemote; |
| 4939 | |
| 4940 | // request a list of loaded libraries from GDBServer |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4941 | if (GetLoadedModuleList (module_list).Fail()) |
| 4942 | return 0; |
| 4943 | |
| 4944 | // get a list of all the modules |
| 4945 | ModuleList new_modules; |
| 4946 | |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 4947 | for (LoadedModuleInfoList::LoadedModuleInfo & modInfo : module_list.m_list) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4948 | { |
| 4949 | std::string mod_name; |
| 4950 | lldb::addr_t mod_base; |
Stephane Sezer | 3553c0e | 2016-04-05 17:25:32 +0000 | [diff] [blame] | 4951 | lldb::addr_t link_map; |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4952 | bool mod_base_is_offset; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4953 | |
| 4954 | bool valid = true; |
| 4955 | valid &= modInfo.get_name (mod_name); |
| 4956 | valid &= modInfo.get_base (mod_base); |
Stephane Sezer | c6845a0 | 2015-08-20 22:07:48 +0000 | [diff] [blame] | 4957 | valid &= modInfo.get_base_is_offset (mod_base_is_offset); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4958 | if (!valid) |
| 4959 | continue; |
| 4960 | |
Stephane Sezer | 3553c0e | 2016-04-05 17:25:32 +0000 | [diff] [blame] | 4961 | if (!modInfo.get_link_map (link_map)) |
| 4962 | link_map = LLDB_INVALID_ADDRESS; |
| 4963 | |
Francis Ricci | a030061 | 2016-04-27 17:10:15 +0000 | [diff] [blame] | 4964 | FileSpec file (mod_name.c_str(), true); |
Stephane Sezer | 3553c0e | 2016-04-05 17:25:32 +0000 | [diff] [blame] | 4965 | lldb::ModuleSP module_sp = LoadModuleAtAddress (file, link_map, mod_base, |
| 4966 | mod_base_is_offset); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4967 | |
| 4968 | if (module_sp.get()) |
| 4969 | new_modules.Append (module_sp); |
| 4970 | } |
| 4971 | |
| 4972 | if (new_modules.GetSize() > 0) |
| 4973 | { |
Francis Ricci | be8cab7 | 2016-04-25 21:02:24 +0000 | [diff] [blame] | 4974 | ModuleList removed_modules; |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 4975 | Target &target = GetTarget(); |
Francis Ricci | be8cab7 | 2016-04-25 21:02:24 +0000 | [diff] [blame] | 4976 | ModuleList &loaded_modules = m_process->GetTarget().GetImages(); |
| 4977 | |
| 4978 | for (size_t i = 0; i < loaded_modules.GetSize(); ++i) |
| 4979 | { |
| 4980 | const lldb::ModuleSP loaded_module = loaded_modules.GetModuleAtIndex(i); |
| 4981 | |
| 4982 | bool found = false; |
| 4983 | for (size_t j = 0; j < new_modules.GetSize(); ++j) |
| 4984 | { |
| 4985 | if (new_modules.GetModuleAtIndex(j).get() == loaded_module.get()) |
| 4986 | found = true; |
| 4987 | } |
| 4988 | |
Francis Ricci | 80dbd15 | 2016-06-06 15:00:50 +0000 | [diff] [blame] | 4989 | // The main executable will never be included in libraries-svr4, don't remove it |
| 4990 | if (!found && loaded_module.get() != target.GetExecutableModulePointer()) |
Francis Ricci | be8cab7 | 2016-04-25 21:02:24 +0000 | [diff] [blame] | 4991 | { |
Francis Ricci | 80dbd15 | 2016-06-06 15:00:50 +0000 | [diff] [blame] | 4992 | removed_modules.Append (loaded_module); |
Francis Ricci | be8cab7 | 2016-04-25 21:02:24 +0000 | [diff] [blame] | 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | loaded_modules.Remove (removed_modules); |
| 4997 | m_process->GetTarget().ModulesDidUnload (removed_modules, false); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4998 | |
| 4999 | new_modules.ForEach ([&target](const lldb::ModuleSP module_sp) -> bool |
| 5000 | { |
| 5001 | lldb_private::ObjectFile * obj = module_sp->GetObjectFile (); |
| 5002 | if (!obj) |
| 5003 | return true; |
| 5004 | |
| 5005 | if (obj->GetType () != ObjectFile::Type::eTypeExecutable) |
| 5006 | return true; |
| 5007 | |
| 5008 | lldb::ModuleSP module_copy_sp = module_sp; |
| 5009 | target.SetExecutableModule (module_copy_sp, false); |
| 5010 | return false; |
| 5011 | }); |
| 5012 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 5013 | loaded_modules.AppendIfNeeded (new_modules); |
| 5014 | m_process->GetTarget().ModulesDidLoad (new_modules); |
| 5015 | } |
| 5016 | |
| 5017 | return new_modules.GetSize(); |
Stephane Sezer | 87b0fe0 | 2016-01-12 19:02:41 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | size_t |
| 5021 | ProcessGDBRemote::LoadModules () |
| 5022 | { |
| 5023 | LoadedModuleInfoList module_list; |
| 5024 | return LoadModules (module_list); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 5025 | } |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 5026 | |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 5027 | Error |
| 5028 | ProcessGDBRemote::GetFileLoadAddress(const FileSpec& file, bool& is_loaded, lldb::addr_t& load_addr) |
| 5029 | { |
| 5030 | is_loaded = false; |
| 5031 | load_addr = LLDB_INVALID_ADDRESS; |
| 5032 | |
| 5033 | std::string file_path = file.GetPath(false); |
| 5034 | if (file_path.empty ()) |
| 5035 | return Error("Empty file name specified"); |
| 5036 | |
| 5037 | StreamString packet; |
| 5038 | packet.PutCString("qFileLoadAddress:"); |
| 5039 | packet.PutCStringAsRawHex8(file_path.c_str()); |
| 5040 | |
| 5041 | StringExtractorGDBRemote response; |
| 5042 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), response, false) != GDBRemoteCommunication::PacketResult::Success) |
| 5043 | return Error("Sending qFileLoadAddress packet failed"); |
| 5044 | |
| 5045 | if (response.IsErrorResponse()) |
| 5046 | { |
| 5047 | if (response.GetError() == 1) |
| 5048 | { |
| 5049 | // The file is not loaded into the inferior |
| 5050 | is_loaded = false; |
| 5051 | load_addr = LLDB_INVALID_ADDRESS; |
| 5052 | return Error(); |
| 5053 | } |
| 5054 | |
| 5055 | return Error("Fetching file load address from remote server returned an error"); |
| 5056 | } |
| 5057 | |
| 5058 | if (response.IsNormalResponse()) |
| 5059 | { |
| 5060 | is_loaded = true; |
| 5061 | load_addr = response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 5062 | return Error(); |
| 5063 | } |
| 5064 | |
| 5065 | return Error("Unknown error happened during sending the load address packet"); |
| 5066 | } |
| 5067 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5068 | |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 5069 | void |
| 5070 | ProcessGDBRemote::ModulesDidLoad (ModuleList &module_list) |
| 5071 | { |
| 5072 | // We must call the lldb_private::Process::ModulesDidLoad () first before we do anything |
| 5073 | Process::ModulesDidLoad (module_list); |
| 5074 | |
| 5075 | // After loading shared libraries, we can ask our remote GDB server if |
| 5076 | // it needs any symbols. |
| 5077 | m_gdb_comm.ServeSymbolLookups(this); |
| 5078 | } |
| 5079 | |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5080 | void |
| 5081 | ProcessGDBRemote::HandleAsyncStdout(llvm::StringRef out) |
| 5082 | { |
| 5083 | AppendSTDOUT(out.data(), out.size()); |
| 5084 | } |
| 5085 | |
| 5086 | static const char *end_delimiter = "--end--;"; |
| 5087 | static const int end_delimiter_len = 8; |
| 5088 | |
| 5089 | void |
| 5090 | ProcessGDBRemote::HandleAsyncMisc(llvm::StringRef data) |
| 5091 | { |
| 5092 | std::string input = data.str(); // '1' to move beyond 'A' |
| 5093 | if (m_partial_profile_data.length() > 0) |
| 5094 | { |
| 5095 | m_partial_profile_data.append(input); |
| 5096 | input = m_partial_profile_data; |
| 5097 | m_partial_profile_data.clear(); |
| 5098 | } |
| 5099 | |
| 5100 | size_t found, pos = 0, len = input.length(); |
| 5101 | while ((found = input.find(end_delimiter, pos)) != std::string::npos) |
| 5102 | { |
| 5103 | StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str()); |
| 5104 | std::string profile_data = HarmonizeThreadIdsForProfileData(profileDataExtractor); |
| 5105 | BroadcastAsyncProfileData(profile_data); |
| 5106 | |
| 5107 | pos = found + end_delimiter_len; |
| 5108 | } |
| 5109 | |
| 5110 | if (pos < len) |
| 5111 | { |
| 5112 | // Last incomplete chunk. |
| 5113 | m_partial_profile_data = input.substr(pos); |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | std::string |
| 5118 | ProcessGDBRemote::HarmonizeThreadIdsForProfileData(StringExtractorGDBRemote &profileDataExtractor) |
| 5119 | { |
| 5120 | std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5121 | std::string output; |
| 5122 | llvm::raw_string_ostream output_stream(output); |
| 5123 | llvm::StringRef name, value; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5124 | |
| 5125 | // Going to assuming thread_used_usec comes first, else bail out. |
| 5126 | while (profileDataExtractor.GetNameColonValue(name, value)) |
| 5127 | { |
| 5128 | if (name.compare("thread_used_id") == 0) |
| 5129 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5130 | StringExtractor threadIDHexExtractor(value); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5131 | uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0); |
| 5132 | |
| 5133 | bool has_used_usec = false; |
| 5134 | uint32_t curr_used_usec = 0; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5135 | llvm::StringRef usec_name, usec_value; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5136 | uint32_t input_file_pos = profileDataExtractor.GetFilePos(); |
| 5137 | if (profileDataExtractor.GetNameColonValue(usec_name, usec_value)) |
| 5138 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5139 | if (usec_name.equals("thread_used_usec")) |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5140 | { |
| 5141 | has_used_usec = true; |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5142 | usec_value.getAsInteger(0, curr_used_usec); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5143 | } |
| 5144 | else |
| 5145 | { |
| 5146 | // We didn't find what we want, it is probably |
| 5147 | // an older version. Bail out. |
| 5148 | profileDataExtractor.SetFilePos(input_file_pos); |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | if (has_used_usec) |
| 5153 | { |
| 5154 | uint32_t prev_used_usec = 0; |
| 5155 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id); |
| 5156 | if (iterator != m_thread_id_to_used_usec_map.end()) |
| 5157 | { |
| 5158 | prev_used_usec = m_thread_id_to_used_usec_map[thread_id]; |
| 5159 | } |
| 5160 | |
| 5161 | uint32_t real_used_usec = curr_used_usec - prev_used_usec; |
| 5162 | // A good first time record is one that runs for at least 0.25 sec |
| 5163 | bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000); |
| 5164 | bool good_subsequent_time = |
| 5165 | (prev_used_usec > 0) && ((real_used_usec > 0) || (HasAssignedIndexIDToThread(thread_id))); |
| 5166 | |
| 5167 | if (good_first_time || good_subsequent_time) |
| 5168 | { |
| 5169 | // We try to avoid doing too many index id reservation, |
| 5170 | // resulting in fast increase of index ids. |
| 5171 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5172 | output_stream << name << ":"; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5173 | int32_t index_id = AssignIndexIDToThread(thread_id); |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5174 | output_stream << index_id << ";"; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5175 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5176 | output_stream << usec_name << ":" << usec_value << ";"; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5177 | } |
| 5178 | else |
| 5179 | { |
| 5180 | // Skip past 'thread_used_name'. |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5181 | llvm::StringRef local_name, local_value; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5182 | profileDataExtractor.GetNameColonValue(local_name, local_value); |
| 5183 | } |
| 5184 | |
| 5185 | // Store current time as previous time so that they can be compared later. |
| 5186 | new_thread_id_to_used_usec_map[thread_id] = curr_used_usec; |
| 5187 | } |
| 5188 | else |
| 5189 | { |
| 5190 | // Bail out and use old string. |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5191 | output_stream << name << ":" << value << ";"; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5192 | } |
| 5193 | } |
| 5194 | else |
| 5195 | { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5196 | output_stream << name << ":" << value << ";"; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5197 | } |
| 5198 | } |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5199 | output_stream << end_delimiter; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5200 | m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map; |
| 5201 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 5202 | return output_stream.str(); |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | void |
| 5206 | ProcessGDBRemote::HandleStopReply() |
| 5207 | { |
| 5208 | if (GetStopID() != 0) |
| 5209 | return; |
| 5210 | |
| 5211 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 5212 | { |
| 5213 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); |
| 5214 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 5215 | SetID(pid); |
| 5216 | } |
| 5217 | BuildDynamicRegisterInfo(true); |
| 5218 | } |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5219 | |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5220 | bool |
| 5221 | ProcessGDBRemote::HandleAsyncStructuredData(const StructuredData::ObjectSP |
| 5222 | &object_sp) |
| 5223 | { |
| 5224 | return RouteAsyncStructuredData(object_sp); |
| 5225 | } |
| 5226 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 5227 | class CommandObjectProcessGDBRemoteSpeedTest: public CommandObjectParsed |
| 5228 | { |
| 5229 | public: |
| 5230 | CommandObjectProcessGDBRemoteSpeedTest(CommandInterpreter &interpreter) : |
| 5231 | CommandObjectParsed (interpreter, |
| 5232 | "process plugin packet speed-test", |
| 5233 | "Tests packet speeds of various sizes to determine the performance characteristics of the GDB remote connection. ", |
| 5234 | NULL), |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 5235 | m_option_group (), |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 5236 | m_num_packets (LLDB_OPT_SET_1, false, "count", 'c', 0, eArgTypeCount, "The number of packets to send of each varying size (default is 1000).", 1000), |
| 5237 | m_max_send (LLDB_OPT_SET_1, false, "max-send", 's', 0, eArgTypeCount, "The maximum number of bytes to send in a packet. Sizes increase in powers of 2 while the size is less than or equal to this option value. (default 1024).", 1024), |
| 5238 | m_max_recv (LLDB_OPT_SET_1, false, "max-receive", 'r', 0, eArgTypeCount, "The maximum number of bytes to receive in a packet. Sizes increase in powers of 2 while the size is less than or equal to this option value. (default 1024).", 1024), |
| 5239 | m_json (LLDB_OPT_SET_1, false, "json", 'j', "Print the output as JSON data for easy parsing.", false, true) |
| 5240 | { |
| 5241 | m_option_group.Append (&m_num_packets, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 5242 | m_option_group.Append (&m_max_send, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 5243 | m_option_group.Append (&m_max_recv, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 5244 | m_option_group.Append (&m_json, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 5245 | m_option_group.Finalize(); |
| 5246 | } |
| 5247 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5248 | ~CommandObjectProcessGDBRemoteSpeedTest () |
| 5249 | { |
| 5250 | } |
| 5251 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 5252 | |
| 5253 | Options * |
| 5254 | GetOptions () override |
| 5255 | { |
| 5256 | return &m_option_group; |
| 5257 | } |
| 5258 | |
| 5259 | bool |
| 5260 | DoExecute (Args& command, CommandReturnObject &result) override |
| 5261 | { |
| 5262 | const size_t argc = command.GetArgumentCount(); |
| 5263 | if (argc == 0) |
| 5264 | { |
| 5265 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5266 | if (process) |
| 5267 | { |
| 5268 | StreamSP output_stream_sp (m_interpreter.GetDebugger().GetAsyncOutputStream()); |
| 5269 | result.SetImmediateOutputStream (output_stream_sp); |
| 5270 | |
| 5271 | const uint32_t num_packets = (uint32_t)m_num_packets.GetOptionValue().GetCurrentValue(); |
| 5272 | const uint64_t max_send = m_max_send.GetOptionValue().GetCurrentValue(); |
| 5273 | const uint64_t max_recv = m_max_recv.GetOptionValue().GetCurrentValue(); |
| 5274 | const bool json = m_json.GetOptionValue().GetCurrentValue(); |
| 5275 | if (output_stream_sp) |
| 5276 | process->GetGDBRemote().TestPacketSpeed (num_packets, max_send, max_recv, json, *output_stream_sp); |
| 5277 | else |
| 5278 | { |
| 5279 | process->GetGDBRemote().TestPacketSpeed (num_packets, max_send, max_recv, json, result.GetOutputStream()); |
| 5280 | } |
| 5281 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 5282 | return true; |
| 5283 | } |
| 5284 | } |
| 5285 | else |
| 5286 | { |
| 5287 | result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); |
| 5288 | } |
| 5289 | result.SetStatus (eReturnStatusFailed); |
| 5290 | return false; |
| 5291 | } |
| 5292 | protected: |
| 5293 | OptionGroupOptions m_option_group; |
| 5294 | OptionGroupUInt64 m_num_packets; |
| 5295 | OptionGroupUInt64 m_max_send; |
| 5296 | OptionGroupUInt64 m_max_recv; |
| 5297 | OptionGroupBoolean m_json; |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5298 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 5299 | }; |
| 5300 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5301 | class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5302 | { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5303 | private: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5304 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5305 | public: |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5306 | CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) : |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5307 | CommandObjectParsed (interpreter, |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5308 | "process plugin packet history", |
| 5309 | "Dumps the packet history buffer. ", |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5310 | NULL) |
| 5311 | { |
| 5312 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5313 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5314 | ~CommandObjectProcessGDBRemotePacketHistory () |
| 5315 | { |
| 5316 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5317 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5318 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 5319 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5320 | { |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5321 | const size_t argc = command.GetArgumentCount(); |
| 5322 | if (argc == 0) |
| 5323 | { |
| 5324 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5325 | if (process) |
| 5326 | { |
| 5327 | process->GetGDBRemote().DumpHistory(result.GetOutputStream()); |
| 5328 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 5329 | return true; |
| 5330 | } |
| 5331 | } |
| 5332 | else |
| 5333 | { |
| 5334 | result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); |
| 5335 | } |
| 5336 | result.SetStatus (eReturnStatusFailed); |
| 5337 | return false; |
| 5338 | } |
| 5339 | }; |
| 5340 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5341 | class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed |
| 5342 | { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5343 | private: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5344 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5345 | public: |
| 5346 | CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) : |
| 5347 | CommandObjectParsed (interpreter, |
| 5348 | "process plugin packet xfer-size", |
| 5349 | "Maximum size that lldb will try to read/write one one chunk.", |
| 5350 | NULL) |
| 5351 | { |
| 5352 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5353 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5354 | ~CommandObjectProcessGDBRemotePacketXferSize () |
| 5355 | { |
| 5356 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5357 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5358 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 5359 | DoExecute (Args& command, CommandReturnObject &result) override |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5360 | { |
| 5361 | const size_t argc = command.GetArgumentCount(); |
| 5362 | if (argc == 0) |
| 5363 | { |
| 5364 | result.AppendErrorWithFormat ("'%s' takes an argument to specify the max amount to be transferred when reading/writing", m_cmd_name.c_str()); |
| 5365 | result.SetStatus (eReturnStatusFailed); |
| 5366 | return false; |
| 5367 | } |
| 5368 | |
| 5369 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5370 | if (process) |
| 5371 | { |
| 5372 | const char *packet_size = command.GetArgumentAtIndex(0); |
| 5373 | errno = 0; |
| 5374 | uint64_t user_specified_max = strtoul (packet_size, NULL, 10); |
| 5375 | if (errno == 0 && user_specified_max != 0) |
| 5376 | { |
| 5377 | process->SetUserSpecifiedMaxMemoryTransferSize (user_specified_max); |
| 5378 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 5379 | return true; |
| 5380 | } |
| 5381 | } |
| 5382 | result.SetStatus (eReturnStatusFailed); |
| 5383 | return false; |
| 5384 | } |
| 5385 | }; |
| 5386 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5387 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5388 | class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed |
| 5389 | { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5390 | private: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5391 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5392 | public: |
| 5393 | CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) : |
| 5394 | CommandObjectParsed (interpreter, |
| 5395 | "process plugin packet send", |
| 5396 | "Send a custom packet through the GDB remote protocol and print the answer. " |
| 5397 | "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.", |
| 5398 | NULL) |
| 5399 | { |
| 5400 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5401 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5402 | ~CommandObjectProcessGDBRemotePacketSend () |
| 5403 | { |
| 5404 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5405 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5406 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 5407 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5408 | { |
| 5409 | const size_t argc = command.GetArgumentCount(); |
| 5410 | if (argc == 0) |
| 5411 | { |
| 5412 | result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str()); |
| 5413 | result.SetStatus (eReturnStatusFailed); |
| 5414 | return false; |
| 5415 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5416 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5417 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5418 | if (process) |
| 5419 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 5420 | for (size_t i=0; i<argc; ++ i) |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5421 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 5422 | const char *packet_cstr = command.GetArgumentAtIndex(0); |
| 5423 | bool send_async = true; |
| 5424 | StringExtractorGDBRemote response; |
| 5425 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 5426 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 5427 | Stream &output_strm = result.GetOutputStream(); |
| 5428 | output_strm.Printf (" packet: %s\n", packet_cstr); |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 5429 | std::string &response_str = response.GetStringRef(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5430 | |
Han Ming Ong | 399289e | 2013-06-21 19:56:59 +0000 | [diff] [blame] | 5431 | if (strstr(packet_cstr, "qGetProfileData") != NULL) |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 5432 | { |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 5433 | response_str = process->HarmonizeThreadIdsForProfileData(response); |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 5436 | if (response_str.empty()) |
| 5437 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 5438 | else |
| 5439 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5440 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5441 | } |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5442 | return true; |
| 5443 | } |
| 5444 | }; |
| 5445 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5446 | class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw |
| 5447 | { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5448 | private: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5449 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5450 | public: |
| 5451 | CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) : |
| 5452 | CommandObjectRaw (interpreter, |
| 5453 | "process plugin packet monitor", |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 5454 | "Send a qRcmd packet through the GDB remote protocol and print the response." |
| 5455 | "The argument passed to this command will be hex encoded into a valid 'qRcmd' packet, sent and the response will be printed.", |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5456 | NULL) |
| 5457 | { |
| 5458 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5459 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5460 | ~CommandObjectProcessGDBRemotePacketMonitor () |
| 5461 | { |
| 5462 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5463 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5464 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 5465 | DoExecute (const char *command, CommandReturnObject &result) override |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5466 | { |
| 5467 | if (command == NULL || command[0] == '\0') |
| 5468 | { |
| 5469 | result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str()); |
| 5470 | result.SetStatus (eReturnStatusFailed); |
| 5471 | return false; |
| 5472 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5473 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5474 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 5475 | if (process) |
| 5476 | { |
| 5477 | StreamString packet; |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 5478 | packet.PutCString("qRcmd,"); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5479 | packet.PutBytesAsRawHex8(command, strlen(command)); |
| 5480 | const char *packet_cstr = packet.GetString().c_str(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5481 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5482 | bool send_async = true; |
| 5483 | StringExtractorGDBRemote response; |
| 5484 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 5485 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 5486 | Stream &output_strm = result.GetOutputStream(); |
| 5487 | output_strm.Printf (" packet: %s\n", packet_cstr); |
| 5488 | const std::string &response_str = response.GetStringRef(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5489 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5490 | if (response_str.empty()) |
| 5491 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 5492 | else |
| 5493 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
| 5494 | } |
| 5495 | return true; |
| 5496 | } |
| 5497 | }; |
| 5498 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5499 | class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword |
| 5500 | { |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5501 | private: |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5502 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5503 | public: |
| 5504 | CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) : |
| 5505 | CommandObjectMultiword (interpreter, |
| 5506 | "process plugin packet", |
| 5507 | "Commands that deal with GDB remote packets.", |
| 5508 | NULL) |
| 5509 | { |
| 5510 | LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter))); |
| 5511 | LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter))); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 5512 | LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter))); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 5513 | LoadSubCommand ("xfer-size", CommandObjectSP (new CommandObjectProcessGDBRemotePacketXferSize (interpreter))); |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 5514 | LoadSubCommand ("speed-test", CommandObjectSP (new CommandObjectProcessGDBRemoteSpeedTest (interpreter))); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5515 | } |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5516 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5517 | ~CommandObjectProcessGDBRemotePacket () |
| 5518 | { |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 5519 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 5520 | }; |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5521 | |
| 5522 | class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword |
| 5523 | { |
| 5524 | public: |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 5525 | CommandObjectMultiwordProcessGDBRemote(CommandInterpreter &interpreter) |
| 5526 | : CommandObjectMultiword(interpreter, "process plugin", "Commands for operating on a ProcessGDBRemote process.", |
| 5527 | "process plugin <subcommand> [<subcommand-options>]") |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5528 | { |
| 5529 | LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter))); |
| 5530 | } |
| 5531 | |
Eugene Zelenko | 0722f08 | 2015-10-24 01:28:05 +0000 | [diff] [blame] | 5532 | ~CommandObjectMultiwordProcessGDBRemote () |
| 5533 | { |
| 5534 | } |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5535 | }; |
| 5536 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 5537 | CommandObject * |
| 5538 | ProcessGDBRemote::GetPluginCommandObject() |
| 5539 | { |
| 5540 | if (!m_command_sp) |
| 5541 | m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter())); |
| 5542 | return m_command_sp.get(); |
| 5543 | } |