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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 11 | #include "lldb/Host/Config.h" |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 12 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | // C Includes |
| 14 | #include <errno.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 16 | #ifndef LLDB_DISABLE_POSIX |
Sean Callanan | 224f6f5 | 2012-07-19 18:07:36 +0000 | [diff] [blame] | 17 | #include <netinet/in.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 18 | #include <sys/mman.h> // for mmap |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +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> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
| 29 | // Other libraries and framework includes |
| 30 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 31 | #include "lldb/Breakpoint/Watchpoint.h" |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 32 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Core/ArchSpec.h" |
| 34 | #include "lldb/Core/Debugger.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 35 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Greg Clayton | 53239f0 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 36 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 38 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Core/PluginManager.h" |
| 40 | #include "lldb/Core/State.h" |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 41 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | #include "lldb/Core/StreamString.h" |
| 43 | #include "lldb/Core/Timer.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 44 | #include "lldb/Core/Value.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 45 | #include "lldb/Host/HostThread.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 46 | #include "lldb/Host/StringConvert.h" |
Jason Molenda | d1fae14 | 2012-09-29 08:03:33 +0000 | [diff] [blame] | 47 | #include "lldb/Host/Symbols.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 48 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 50 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 51 | #include "lldb/Interpreter/CommandObject.h" |
| 52 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 53 | #include "lldb/Interpreter/CommandReturnObject.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 54 | #include "lldb/Interpreter/OptionValueProperties.h" |
| 55 | #include "lldb/Interpreter/Property.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | #include "lldb/Symbol/ObjectFile.h" |
| 57 | #include "lldb/Target/DynamicLoader.h" |
| 58 | #include "lldb/Target/Target.h" |
| 59 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 60 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 61 | #include "lldb/Target/SystemRuntime.h" |
Jason Molenda | a34a0c6 | 2010-06-09 21:28:42 +0000 | [diff] [blame] | 62 | #include "lldb/Utility/PseudoTerminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
| 64 | // Project includes |
| 65 | #include "lldb/Host/Host.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 66 | #include "Plugins/Process/Utility/FreeBSDSignals.h" |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 67 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 68 | #include "Plugins/Process/Utility/LinuxSignals.h" |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame^] | 69 | #include "Plugins/Process/Utility/MipsLinuxSignals.h" |
Jason Molenda | c42d243 | 2012-07-25 03:40:06 +0000 | [diff] [blame] | 70 | #include "Plugins/Process/Utility/StopInfoMachException.h" |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 71 | #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 72 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | #include "GDBRemoteRegisterContext.h" |
| 74 | #include "ProcessGDBRemote.h" |
| 75 | #include "ProcessGDBRemoteLog.h" |
| 76 | #include "ThreadGDBRemote.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 77 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 78 | #define DEBUGSERVER_BASENAME "debugserver" |
| 79 | using namespace lldb; |
| 80 | using namespace lldb_private; |
| 81 | using namespace lldb_private::process_gdb_remote; |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 82 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 83 | namespace lldb |
| 84 | { |
| 85 | // Provide a function that can easily dump the packet history if we know a |
| 86 | // ProcessGDBRemote * value (which we can get from logs or from debugging). |
| 87 | // We need the function in the lldb namespace so it makes it into the final |
| 88 | // executable since the LLDB shared library only exports stuff in the lldb |
| 89 | // namespace. This allows you to attach with a debugger and call this |
| 90 | // function and get the packet history dumped to a file. |
| 91 | void |
| 92 | DumpProcessGDBRemotePacketHistory (void *p, const char *path) |
| 93 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 94 | StreamFile strm; |
| 95 | Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate)); |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 96 | if (error.Success()) |
| 97 | ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 98 | } |
Filipe Cabecinhas | c34f776 | 2012-05-23 16:27:09 +0000 | [diff] [blame] | 99 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 101 | namespace { |
Steve Pucci | 5ec012d | 2014-01-16 22:18:14 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 103 | static PropertyDefinition |
| 104 | g_properties[] = |
| 105 | { |
| 106 | { "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] | 107 | { "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] | 108 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 109 | }; |
| 110 | |
| 111 | enum |
| 112 | { |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 113 | ePropertyPacketTimeout, |
| 114 | ePropertyTargetDefinitionFile |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | class PluginProperties : public Properties |
| 118 | { |
| 119 | public: |
| 120 | |
| 121 | static ConstString |
| 122 | GetSettingName () |
| 123 | { |
| 124 | return ProcessGDBRemote::GetPluginNameStatic(); |
| 125 | } |
| 126 | |
| 127 | PluginProperties() : |
| 128 | Properties () |
| 129 | { |
| 130 | m_collection_sp.reset (new OptionValueProperties(GetSettingName())); |
| 131 | m_collection_sp->Initialize(g_properties); |
| 132 | } |
| 133 | |
| 134 | virtual |
| 135 | ~PluginProperties() |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | uint64_t |
| 140 | GetPacketTimeout() |
| 141 | { |
| 142 | const uint32_t idx = ePropertyPacketTimeout; |
| 143 | return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); |
| 144 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 145 | |
| 146 | bool |
| 147 | SetPacketTimeout(uint64_t timeout) |
| 148 | { |
| 149 | const uint32_t idx = ePropertyPacketTimeout; |
| 150 | return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout); |
| 151 | } |
| 152 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 153 | FileSpec |
| 154 | GetTargetDefinitionFile () const |
| 155 | { |
| 156 | const uint32_t idx = ePropertyTargetDefinitionFile; |
| 157 | return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx); |
| 158 | } |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; |
| 162 | |
| 163 | static const ProcessKDPPropertiesSP & |
| 164 | GetGlobalPluginProperties() |
| 165 | { |
| 166 | static ProcessKDPPropertiesSP g_settings_sp; |
| 167 | if (!g_settings_sp) |
| 168 | g_settings_sp.reset (new PluginProperties ()); |
| 169 | return g_settings_sp; |
| 170 | } |
| 171 | |
| 172 | } // anonymous namespace end |
| 173 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 174 | // TODO Randomly assigning a port is unsafe. We should get an unused |
| 175 | // ephemeral port from the kernel and make sure we reserve it before passing |
| 176 | // it to debugserver. |
| 177 | |
| 178 | #if defined (__APPLE__) |
| 179 | #define LOW_PORT (IPPORT_RESERVED) |
| 180 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
| 181 | #else |
| 182 | #define LOW_PORT (1024u) |
| 183 | #define HIGH_PORT (49151u) |
| 184 | #endif |
| 185 | |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 186 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 187 | static bool rand_initialized = false; |
| 188 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 189 | static inline uint16_t |
| 190 | get_random_port () |
| 191 | { |
| 192 | if (!rand_initialized) |
| 193 | { |
| 194 | time_t seed = time(NULL); |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 195 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 196 | rand_initialized = true; |
| 197 | srand(seed); |
| 198 | } |
| 199 | return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT; |
| 200 | } |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 201 | #endif |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 202 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 203 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | ProcessGDBRemote::GetPluginNameStatic() |
| 205 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 206 | static ConstString g_name("gdb-remote"); |
| 207 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | const char * |
| 211 | ProcessGDBRemote::GetPluginDescriptionStatic() |
| 212 | { |
| 213 | return "GDB Remote protocol based debugging plug-in."; |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | ProcessGDBRemote::Terminate() |
| 218 | { |
| 219 | PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance); |
| 220 | } |
| 221 | |
| 222 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 223 | lldb::ProcessSP |
| 224 | ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 226 | lldb::ProcessSP process_sp; |
| 227 | if (crash_file_path == NULL) |
| 228 | process_sp.reset (new ProcessGDBRemote (target, listener)); |
| 229 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool |
Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 233 | ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 234 | { |
Greg Clayton | 596ed24 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 235 | if (plugin_specified_by_name) |
| 236 | return true; |
| 237 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | // For now we are just making sure the file exists for a given module |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 239 | Module *exe_module = target.GetExecutableModulePointer(); |
| 240 | if (exe_module) |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 241 | { |
| 242 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 243 | // We can't debug core files... |
| 244 | switch (exe_objfile->GetType()) |
| 245 | { |
| 246 | case ObjectFile::eTypeInvalid: |
| 247 | case ObjectFile::eTypeCoreFile: |
| 248 | case ObjectFile::eTypeDebugInfo: |
| 249 | case ObjectFile::eTypeObjectFile: |
| 250 | case ObjectFile::eTypeSharedLibrary: |
| 251 | case ObjectFile::eTypeStubLibrary: |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 252 | case ObjectFile::eTypeJIT: |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 253 | return false; |
| 254 | case ObjectFile::eTypeExecutable: |
| 255 | case ObjectFile::eTypeDynamicLinker: |
| 256 | case ObjectFile::eTypeUnknown: |
| 257 | break; |
| 258 | } |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 259 | return exe_module->GetFileSpec().Exists(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 260 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 261 | // However, if there is no executable module, we return true since we might be preparing to attach. |
| 262 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | //---------------------------------------------------------------------- |
| 266 | // ProcessGDBRemote constructor |
| 267 | //---------------------------------------------------------------------- |
| 268 | ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) : |
| 269 | Process (target, listener), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | m_flags (0), |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 271 | m_gdb_comm (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | m_debugserver_pid (LLDB_INVALID_PROCESS_ID), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 273 | m_last_stop_packet (), |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 274 | m_last_stop_packet_mutex (Mutex::eMutexTypeNormal), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | m_register_info (), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 276 | m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"), |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 277 | m_async_thread_state_mutex(Mutex::eMutexTypeRecursive), |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 278 | m_thread_ids (), |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 279 | m_continue_c_tids (), |
| 280 | m_continue_C_tids (), |
| 281 | m_continue_s_tids (), |
| 282 | m_continue_S_tids (), |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 283 | m_max_memory_size (0), |
| 284 | m_remote_stub_max_memory_size (0), |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 285 | m_addr_to_mmap_size (), |
| 286 | m_thread_create_bp_sp (), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 287 | m_waiting_for_attach (false), |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 288 | m_destroy_tried_resuming (false), |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 289 | m_command_sp (), |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 290 | m_breakpoint_pc_offset (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | { |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 292 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 293 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 294 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit"); |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 295 | const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); |
| 296 | if (timeout_seconds > 0) |
| 297 | m_gdb_comm.SetPacketTimeout(timeout_seconds); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | //---------------------------------------------------------------------- |
| 301 | // Destructor |
| 302 | //---------------------------------------------------------------------- |
| 303 | ProcessGDBRemote::~ProcessGDBRemote() |
| 304 | { |
| 305 | // m_mach_process.UnregisterNotificationCallbacks (this); |
| 306 | Clear(); |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 307 | // We need to call finalize on the process before destroying ourselves |
| 308 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 309 | // destruct this class, then Process::~Process() might have problems |
| 310 | // trying to fully destroy the broadcaster. |
| 311 | Finalize(); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 312 | |
| 313 | // The general Finalize is going to try to destroy the process and that SHOULD |
| 314 | // shut down the async thread. However, if we don't kill it it will get stranded and |
| 315 | // its connection will go away so when it wakes up it will crash. So kill it for sure here. |
| 316 | StopAsyncThread(); |
| 317 | KillDebugserverProcess(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | //---------------------------------------------------------------------- |
| 321 | // PluginInterface |
| 322 | //---------------------------------------------------------------------- |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 323 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | ProcessGDBRemote::GetPluginName() |
| 325 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | return GetPluginNameStatic(); |
| 327 | } |
| 328 | |
| 329 | uint32_t |
| 330 | ProcessGDBRemote::GetPluginVersion() |
| 331 | { |
| 332 | return 1; |
| 333 | } |
| 334 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 335 | bool |
| 336 | ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec) |
| 337 | { |
| 338 | ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
| 339 | Error error; |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 340 | StructuredData::ObjectSP module_object_sp(interpreter->LoadPluginModule(target_definition_fspec, error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 341 | if (module_object_sp) |
| 342 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 343 | StructuredData::DictionarySP target_definition_sp( |
| 344 | interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), "gdb-server-target-definition", error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 345 | |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 346 | if (target_definition_sp) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 347 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 348 | StructuredData::ObjectSP target_object(target_definition_sp->GetValueForKey("host-info")); |
| 349 | if (target_object) |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 350 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 351 | if (auto host_info_dict = target_object->GetAsDictionary()) |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 352 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 353 | StructuredData::ObjectSP triple_value = host_info_dict->GetValueForKey("triple"); |
| 354 | if (auto triple_string_value = triple_value->GetAsString()) |
| 355 | { |
| 356 | std::string triple_string = triple_string_value->GetValue(); |
| 357 | ArchSpec host_arch(triple_string.c_str()); |
| 358 | if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture())) |
| 359 | { |
| 360 | GetTarget().SetArchitecture(host_arch); |
| 361 | } |
| 362 | } |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 363 | } |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 364 | } |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 365 | m_breakpoint_pc_offset = 0; |
| 366 | StructuredData::ObjectSP breakpoint_pc_offset_value = target_definition_sp->GetValueForKey("breakpoint-pc-offset"); |
| 367 | if (breakpoint_pc_offset_value) |
| 368 | { |
| 369 | if (auto breakpoint_pc_int_value = breakpoint_pc_offset_value->GetAsInteger()) |
| 370 | m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue(); |
| 371 | } |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 372 | |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 373 | if (m_register_info.SetRegisterInfo(*target_definition_sp, GetTarget().GetArchitecture().GetByteOrder()) > 0) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 374 | { |
| 375 | return true; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | void |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 384 | ProcessGDBRemote::BuildDynamicRegisterInfo (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 386 | if (!force && m_register_info.GetNumRegisters() > 0) |
| 387 | return; |
| 388 | |
| 389 | char packet[128]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | m_register_info.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 391 | uint32_t reg_offset = 0; |
| 392 | uint32_t reg_num = 0; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 393 | for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 394 | response_type == StringExtractorGDBRemote::eResponse; |
| 395 | ++reg_num) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 397 | const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 398 | assert (packet_len < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 399 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 400 | 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] | 401 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 402 | response_type = response.GetResponseType(); |
| 403 | if (response_type == StringExtractorGDBRemote::eResponse) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 404 | { |
| 405 | std::string name; |
| 406 | std::string value; |
| 407 | ConstString reg_name; |
| 408 | ConstString alt_name; |
| 409 | ConstString set_name; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 410 | std::vector<uint32_t> value_regs; |
| 411 | std::vector<uint32_t> invalidate_regs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 412 | RegisterInfo reg_info = { NULL, // Name |
| 413 | NULL, // Alt name |
| 414 | 0, // byte size |
| 415 | reg_offset, // offset |
| 416 | eEncodingUint, // encoding |
| 417 | eFormatHex, // formate |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 418 | { |
| 419 | LLDB_INVALID_REGNUM, // GCC reg num |
| 420 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 421 | LLDB_INVALID_REGNUM, // generic reg num |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 422 | reg_num, // GDB reg num |
| 423 | reg_num // native register number |
Greg Clayton | 435d85a | 2012-02-29 19:27:27 +0000 | [diff] [blame] | 424 | }, |
| 425 | NULL, |
| 426 | NULL |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | while (response.GetNameColonValue(name, value)) |
| 430 | { |
| 431 | if (name.compare("name") == 0) |
| 432 | { |
| 433 | reg_name.SetCString(value.c_str()); |
| 434 | } |
| 435 | else if (name.compare("alt-name") == 0) |
| 436 | { |
| 437 | alt_name.SetCString(value.c_str()); |
| 438 | } |
| 439 | else if (name.compare("bitsize") == 0) |
| 440 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 441 | reg_info.byte_size = StringConvert::ToUInt32(value.c_str(), 0, 0) / CHAR_BIT; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | } |
| 443 | else if (name.compare("offset") == 0) |
| 444 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 445 | uint32_t offset = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0); |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 446 | if (reg_offset != offset) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | { |
| 448 | reg_offset = offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | else if (name.compare("encoding") == 0) |
| 452 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 453 | const Encoding encoding = Args::StringToEncoding (value.c_str()); |
| 454 | if (encoding != eEncodingInvalid) |
| 455 | reg_info.encoding = encoding; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 456 | } |
| 457 | else if (name.compare("format") == 0) |
| 458 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 459 | Format format = eFormatInvalid; |
| 460 | if (Args::StringToFormat (value.c_str(), format, NULL).Success()) |
| 461 | reg_info.format = format; |
| 462 | else if (value.compare("binary") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | reg_info.format = eFormatBinary; |
| 464 | else if (value.compare("decimal") == 0) |
| 465 | reg_info.format = eFormatDecimal; |
| 466 | else if (value.compare("hex") == 0) |
| 467 | reg_info.format = eFormatHex; |
| 468 | else if (value.compare("float") == 0) |
| 469 | reg_info.format = eFormatFloat; |
| 470 | else if (value.compare("vector-sint8") == 0) |
| 471 | reg_info.format = eFormatVectorOfSInt8; |
| 472 | else if (value.compare("vector-uint8") == 0) |
| 473 | reg_info.format = eFormatVectorOfUInt8; |
| 474 | else if (value.compare("vector-sint16") == 0) |
| 475 | reg_info.format = eFormatVectorOfSInt16; |
| 476 | else if (value.compare("vector-uint16") == 0) |
| 477 | reg_info.format = eFormatVectorOfUInt16; |
| 478 | else if (value.compare("vector-sint32") == 0) |
| 479 | reg_info.format = eFormatVectorOfSInt32; |
| 480 | else if (value.compare("vector-uint32") == 0) |
| 481 | reg_info.format = eFormatVectorOfUInt32; |
| 482 | else if (value.compare("vector-float32") == 0) |
| 483 | reg_info.format = eFormatVectorOfFloat32; |
| 484 | else if (value.compare("vector-uint128") == 0) |
| 485 | reg_info.format = eFormatVectorOfUInt128; |
| 486 | } |
| 487 | else if (name.compare("set") == 0) |
| 488 | { |
| 489 | set_name.SetCString(value.c_str()); |
| 490 | } |
| 491 | else if (name.compare("gcc") == 0) |
| 492 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 493 | reg_info.kinds[eRegisterKindGCC] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 494 | } |
| 495 | else if (name.compare("dwarf") == 0) |
| 496 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 497 | reg_info.kinds[eRegisterKindDWARF] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 498 | } |
| 499 | else if (name.compare("generic") == 0) |
| 500 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 501 | reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 502 | } |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 503 | else if (name.compare("container-regs") == 0) |
| 504 | { |
| 505 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 506 | value_pair.second = value; |
| 507 | do |
| 508 | { |
| 509 | value_pair = value_pair.second.split(','); |
| 510 | if (!value_pair.first.empty()) |
| 511 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 512 | uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16); |
Greg Clayton | 0ba2024 | 2013-01-21 23:32:42 +0000 | [diff] [blame] | 513 | if (reg != LLDB_INVALID_REGNUM) |
| 514 | value_regs.push_back (reg); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 515 | } |
| 516 | } while (!value_pair.second.empty()); |
| 517 | } |
| 518 | else if (name.compare("invalidate-regs") == 0) |
| 519 | { |
| 520 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 521 | value_pair.second = value; |
| 522 | do |
| 523 | { |
| 524 | value_pair = value_pair.second.split(','); |
| 525 | if (!value_pair.first.empty()) |
| 526 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 527 | uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16); |
Greg Clayton | 0ba2024 | 2013-01-21 23:32:42 +0000 | [diff] [blame] | 528 | if (reg != LLDB_INVALID_REGNUM) |
| 529 | invalidate_regs.push_back (reg); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 530 | } |
| 531 | } while (!value_pair.second.empty()); |
| 532 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 535 | reg_info.byte_offset = reg_offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 536 | assert (reg_info.byte_size != 0); |
| 537 | reg_offset += reg_info.byte_size; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 538 | if (!value_regs.empty()) |
| 539 | { |
| 540 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 541 | reg_info.value_regs = value_regs.data(); |
| 542 | } |
| 543 | if (!invalidate_regs.empty()) |
| 544 | { |
| 545 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 546 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 547 | } |
| 548 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 550 | } |
Todd Fiala | 1a63440 | 2014-01-08 07:52:40 +0000 | [diff] [blame] | 551 | else |
| 552 | { |
| 553 | break; // ensure exit before reg_num is incremented |
| 554 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | } |
| 556 | else |
| 557 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 558 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 562 | // Check if qHostInfo specified a specific packet timeout for this connection. |
| 563 | // If so then lets update our setting so the user knows what the timeout is |
| 564 | // and can see it. |
| 565 | const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout(); |
| 566 | if (host_packet_timeout) |
| 567 | { |
| 568 | GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout); |
| 569 | } |
| 570 | |
| 571 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 572 | if (reg_num == 0) |
| 573 | { |
| 574 | FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile (); |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 575 | |
| 576 | if (target_definition_fspec) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 577 | { |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 578 | // See if we can get register definitions from a python file |
| 579 | if (ParsePythonTargetDefinition (target_definition_fspec)) |
| 580 | return; |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 584 | // We didn't get anything if the accumulated reg_num is zero. See if we are |
| 585 | // debugging ARM and fill with a hard coded register set until we can get an |
| 586 | // updated debugserver down on the devices. |
| 587 | // On the other hand, if the accumulated reg_num is positive, see if we can |
| 588 | // add composite registers to the existing primordial ones. |
| 589 | bool from_scratch = (reg_num == 0); |
| 590 | |
| 591 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 592 | const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); |
| 593 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 594 | |
| 595 | // Use the process' architecture instead of the host arch, if available |
| 596 | ArchSpec remote_arch; |
| 597 | if (remote_process_arch.IsValid ()) |
| 598 | remote_arch = remote_process_arch; |
| 599 | else |
| 600 | remote_arch = remote_host_arch; |
| 601 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 602 | if (!target_arch.IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | { |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 604 | if (remote_arch.IsValid() |
| 605 | && remote_arch.GetMachine() == llvm::Triple::arm |
| 606 | && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
| 607 | m_register_info.HardcodeARMRegisters(from_scratch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 608 | } |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 609 | else if (target_arch.GetMachine() == llvm::Triple::arm) |
| 610 | { |
| 611 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 612 | } |
| 613 | |
| 614 | // At this point, we can finalize our register info. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 615 | m_register_info.Finalize (); |
| 616 | } |
| 617 | |
| 618 | Error |
| 619 | ProcessGDBRemote::WillLaunch (Module* module) |
| 620 | { |
| 621 | return WillLaunchOrAttach (); |
| 622 | } |
| 623 | |
| 624 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 625 | ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | { |
| 627 | return WillLaunchOrAttach (); |
| 628 | } |
| 629 | |
| 630 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 631 | ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 632 | { |
| 633 | return WillLaunchOrAttach (); |
| 634 | } |
| 635 | |
| 636 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 637 | ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 638 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 639 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 640 | Error error (WillLaunchOrAttach ()); |
| 641 | |
| 642 | if (error.Fail()) |
| 643 | return error; |
| 644 | |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 645 | error = ConnectToDebugserver (remote_url); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 646 | |
| 647 | if (error.Fail()) |
| 648 | return error; |
| 649 | StartAsyncThread (); |
| 650 | |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 651 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 652 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 653 | { |
| 654 | // We don't have a valid process ID, so note that we are connected |
| 655 | // and could now request to launch or attach, or get remote process |
| 656 | // listings... |
| 657 | SetPrivateState (eStateConnected); |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | // We have a valid process |
| 662 | SetID (pid); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 663 | GetThreadList(); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 664 | if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 665 | { |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 666 | if (!m_target.GetArchitecture().IsValid()) |
| 667 | { |
| 668 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 669 | { |
| 670 | m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | m_target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 675 | } |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 678 | const StateType state = SetThreadStopInfo (m_last_stop_packet); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 679 | if (state == eStateStopped) |
| 680 | { |
| 681 | SetPrivateState (state); |
| 682 | } |
| 683 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 684 | 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] | 685 | } |
| 686 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 687 | 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] | 688 | } |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 689 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 690 | if (log) |
| 691 | 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"); |
| 692 | |
| 693 | |
| 694 | if (error.Success() |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 695 | && !GetTarget().GetArchitecture().IsValid() |
| 696 | && m_gdb_comm.GetHostArchitecture().IsValid()) |
| 697 | { |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 698 | // Prefer the *process'* architecture over that of the *host*, if available. |
| 699 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 700 | GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 701 | else |
| 702 | GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 705 | if (log) |
| 706 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalized target architecture triple: %s", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str ()); |
| 707 | |
| 708 | // Set the Unix signals properly for the target. |
| 709 | // FIXME Add a gdb-remote packet to discover dynamically. |
| 710 | if (error.Success ()) |
| 711 | { |
Tamas Berghammer | ac83982 | 2015-03-04 11:34:10 +0000 | [diff] [blame] | 712 | const ArchSpec arch_spec = m_gdb_comm.GetHostArchitecture(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 713 | if (arch_spec.IsValid ()) |
| 714 | { |
| 715 | if (log) |
| 716 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": determining unix signals type based on architecture %s, triple %s", __FUNCTION__, GetID (), arch_spec.GetArchitectureName () ? arch_spec.GetArchitectureName () : "<null>", arch_spec.GetTriple ().getTriple ().c_str ()); |
| 717 | |
| 718 | switch (arch_spec.GetTriple ().getOS ()) |
| 719 | { |
| 720 | case llvm::Triple::Linux: |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame^] | 721 | if (arch_spec.GetTriple ().getArch () == llvm::Triple::mips64 || arch_spec.GetTriple ().getArch () == llvm::Triple::mips64el) |
| 722 | SetUnixSignals (UnixSignalsSP (new process_linux::MipsLinuxSignals ())); |
| 723 | else |
| 724 | SetUnixSignals (UnixSignalsSP (new process_linux::LinuxSignals ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 725 | if (log) |
| 726 | log->Printf ("ProcessGDBRemote::%s using Linux unix signals type for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 727 | break; |
| 728 | case llvm::Triple::OpenBSD: |
| 729 | case llvm::Triple::FreeBSD: |
| 730 | case llvm::Triple::NetBSD: |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 731 | SetUnixSignals (UnixSignalsSP (new FreeBSDSignals ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 732 | if (log) |
| 733 | log->Printf ("ProcessGDBRemote::%s using *BSD unix signals type for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 734 | break; |
| 735 | default: |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 736 | SetUnixSignals (UnixSignalsSP (new UnixSignals ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 737 | if (log) |
| 738 | log->Printf ("ProcessGDBRemote::%s using generic unix signals type for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 744 | return error; |
| 745 | } |
| 746 | |
| 747 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 748 | ProcessGDBRemote::WillLaunchOrAttach () |
| 749 | { |
| 750 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | m_stdio_communication.Clear (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 752 | return error; |
| 753 | } |
| 754 | |
| 755 | //---------------------------------------------------------------------- |
| 756 | // Process Control |
| 757 | //---------------------------------------------------------------------- |
| 758 | Error |
Jean-Daniel Dupas | 7782de9 | 2013-12-09 22:52:50 +0000 | [diff] [blame] | 759 | ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 761 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 762 | Error error; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 763 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 764 | if (log) |
| 765 | log->Printf ("ProcessGDBRemote::%s() entered", __FUNCTION__); |
| 766 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 767 | uint32_t launch_flags = launch_info.GetFlags().Get(); |
| 768 | const char *stdin_path = NULL; |
| 769 | const char *stdout_path = NULL; |
| 770 | const char *stderr_path = NULL; |
| 771 | const char *working_dir = launch_info.GetWorkingDirectory(); |
| 772 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 773 | const FileAction *file_action; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 774 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 775 | if (file_action) |
| 776 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 777 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 778 | stdin_path = file_action->GetPath(); |
| 779 | } |
| 780 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 781 | if (file_action) |
| 782 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 783 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 784 | stdout_path = file_action->GetPath(); |
| 785 | } |
| 786 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 787 | if (file_action) |
| 788 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 789 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 790 | stderr_path = file_action->GetPath(); |
| 791 | } |
| 792 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 793 | if (log) |
| 794 | { |
| 795 | if (stdin_path || stdout_path || stderr_path) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 796 | 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] | 797 | __FUNCTION__, |
| 798 | stdin_path ? stdin_path : "<null>", |
| 799 | stdout_path ? stdout_path : "<null>", |
| 800 | stderr_path ? stderr_path : "<null>"); |
| 801 | else |
| 802 | log->Printf ("ProcessGDBRemote::%s no STDIO paths given via launch_info", __FUNCTION__); |
| 803 | } |
| 804 | |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 805 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
| 806 | if (stdin_path || disable_stdio) |
| 807 | { |
| 808 | // the inferior will be reading stdin from the specified file |
| 809 | // or stdio is completely disabled |
| 810 | m_stdin_forward = false; |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | m_stdin_forward = true; |
| 815 | } |
| 816 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 817 | // ::LogSetBitMask (GDBR_LOG_DEFAULT); |
| 818 | // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); |
| 819 | // ::LogSetLogFile ("/dev/stdout"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 820 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 821 | ObjectFile * object_file = exe_module->GetObjectFile(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 822 | if (object_file) |
| 823 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 824 | // Make sure we aren't already connected? |
| 825 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 826 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 827 | error = LaunchAndConnectToDebugserver (launch_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (error.Success()) |
| 831 | { |
| 832 | lldb_utility::PseudoTerminal pty; |
| 833 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 834 | |
Greg Clayton | f58c269 | 2011-06-24 22:32:10 +0000 | [diff] [blame] | 835 | PlatformSP platform_sp (m_target.GetPlatform()); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 836 | if (disable_stdio) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 837 | { |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 838 | // set to /dev/null unless redirected to a file above |
| 839 | if (!stdin_path) |
| 840 | stdin_path = "/dev/null"; |
| 841 | if (!stdout_path) |
| 842 | stdout_path = "/dev/null"; |
| 843 | if (!stderr_path) |
| 844 | stderr_path = "/dev/null"; |
| 845 | } |
| 846 | else if (platform_sp && platform_sp->IsHost()) |
| 847 | { |
| 848 | // If the debugserver is local and we aren't disabling STDIO, lets use |
| 849 | // a pseudo terminal to instead of relying on the 'O' packets for stdio |
| 850 | // since 'O' packets can really slow down debugging if the inferior |
| 851 | // does a lot of output. |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 852 | const char *slave_name = NULL; |
| 853 | if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 854 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 855 | if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0)) |
| 856 | slave_name = pty.GetSlaveName (NULL, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 857 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 858 | if (stdin_path == NULL) |
| 859 | stdin_path = slave_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 860 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 861 | if (stdout_path == NULL) |
| 862 | stdout_path = slave_name; |
| 863 | |
| 864 | if (stderr_path == NULL) |
| 865 | stderr_path = slave_name; |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 866 | |
| 867 | if (log) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 868 | 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] | 869 | __FUNCTION__, |
| 870 | stdin_path ? stdin_path : "<null>", |
| 871 | stdout_path ? stdout_path : "<null>", |
| 872 | stderr_path ? stderr_path : "<null>"); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 875 | if (log) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 876 | 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] | 877 | __FUNCTION__, |
| 878 | stdin_path ? stdin_path : "<null>", |
| 879 | stdout_path ? stdout_path : "<null>", |
| 880 | stderr_path ? stderr_path : "<null>"); |
| 881 | |
| 882 | if (stdin_path) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 883 | m_gdb_comm.SetSTDIN (stdin_path); |
| 884 | if (stdout_path) |
| 885 | m_gdb_comm.SetSTDOUT (stdout_path); |
| 886 | if (stderr_path) |
| 887 | m_gdb_comm.SetSTDERR (stderr_path); |
| 888 | |
| 889 | m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR); |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 890 | m_gdb_comm.SetDetachOnError (launch_flags & eLaunchFlagDetachOnError); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 891 | |
Greg Clayton | c4103b3 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 892 | m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 893 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 894 | const char * launch_event_data = launch_info.GetLaunchEventData(); |
| 895 | if (launch_event_data != NULL && *launch_event_data != '\0') |
| 896 | m_gdb_comm.SendLaunchEventDataPacket (launch_event_data); |
| 897 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 898 | if (working_dir && working_dir[0]) |
| 899 | { |
| 900 | m_gdb_comm.SetWorkingDir (working_dir); |
| 901 | } |
| 902 | |
| 903 | // Send the environment and the program + arguments after we connect |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 904 | const Args &environment = launch_info.GetEnvironmentEntries(); |
| 905 | if (environment.GetArgumentCount()) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 906 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 907 | size_t num_environment_entries = environment.GetArgumentCount(); |
| 908 | for (size_t i=0; i<num_environment_entries; ++i) |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 909 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 910 | const char *env_entry = environment.GetArgumentAtIndex(i); |
| 911 | if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 912 | break; |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 913 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 914 | } |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 915 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 916 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 917 | // Scope for the scoped timeout object |
| 918 | GDBRemoteCommunication::ScopedTimeout timeout (m_gdb_comm, 10); |
| 919 | |
| 920 | int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info); |
| 921 | if (arg_packet_err == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 922 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 923 | std::string error_str; |
| 924 | if (m_gdb_comm.GetLaunchSuccess (error_str)) |
| 925 | { |
| 926 | SetID (m_gdb_comm.GetCurrentProcessID ()); |
| 927 | } |
| 928 | else |
| 929 | { |
| 930 | error.SetErrorString (error_str.c_str()); |
| 931 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 932 | } |
| 933 | else |
| 934 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 935 | error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 936 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 937 | } |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 938 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 939 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 940 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 941 | if (log) |
| 942 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 943 | KillDebugserverProcess (); |
| 944 | return error; |
| 945 | } |
| 946 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 947 | if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 948 | { |
Tamas Berghammer | e9f4dfe | 2015-03-13 10:32:42 +0000 | [diff] [blame] | 949 | const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 950 | |
| 951 | if (process_arch.IsValid()) |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 952 | { |
Tamas Berghammer | e9f4dfe | 2015-03-13 10:32:42 +0000 | [diff] [blame] | 953 | m_target.MergeArchitecture(process_arch); |
| 954 | } |
| 955 | else |
| 956 | { |
| 957 | const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture(); |
| 958 | if (host_arch.IsValid()) |
| 959 | m_target.MergeArchitecture(host_arch); |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 962 | SetPrivateState (SetThreadStopInfo (m_last_stop_packet)); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 963 | |
| 964 | if (!disable_stdio) |
| 965 | { |
| 966 | if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 967 | SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 971 | else |
| 972 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 973 | if (log) |
| 974 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 975 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 976 | } |
| 977 | else |
| 978 | { |
| 979 | // Set our user ID to an invalid process ID. |
| 980 | SetID(LLDB_INVALID_PROCESS_ID); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 981 | error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", |
| 982 | exe_module->GetFileSpec().GetFilename().AsCString(), |
| 983 | exe_module->GetArchitecture().GetArchitectureName()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 984 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 985 | return error; |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 986 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | |
| 990 | Error |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 991 | ProcessGDBRemote::ConnectToDebugserver (const char *connect_url) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 992 | { |
| 993 | Error error; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 994 | // Only connect if we have a valid connect URL |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 995 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 996 | |
| 997 | if (connect_url && connect_url[0]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 998 | { |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 999 | if (log) |
| 1000 | log->Printf("ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, connect_url); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1001 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 1002 | if (conn_ap.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1003 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1004 | const uint32_t max_retry_count = 50; |
| 1005 | uint32_t retry_count = 0; |
| 1006 | while (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1007 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1008 | if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) |
| 1009 | { |
| 1010 | m_gdb_comm.SetConnection (conn_ap.release()); |
| 1011 | break; |
| 1012 | } |
| 1013 | else if (error.WasInterrupted()) |
| 1014 | { |
| 1015 | // If we were interrupted, don't keep retrying. |
| 1016 | break; |
| 1017 | } |
| 1018 | |
| 1019 | retry_count++; |
| 1020 | |
| 1021 | if (retry_count >= max_retry_count) |
| 1022 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1023 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1024 | usleep (100000); |
| 1025 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (!m_gdb_comm.IsConnected()) |
| 1030 | { |
| 1031 | if (error.Success()) |
| 1032 | error.SetErrorString("not connected to remote gdb server"); |
| 1033 | return error; |
| 1034 | } |
| 1035 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1036 | // We always seem to be able to open a connection to a local port |
| 1037 | // so we need to make sure we can then send data to it. If we can't |
| 1038 | // then we aren't actually connected to anything, so try and do the |
| 1039 | // handshake with the remote GDB server and make sure that goes |
| 1040 | // alright. |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 1041 | if (!m_gdb_comm.HandshakeWithServer (&error)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1042 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1043 | m_gdb_comm.Disconnect(); |
| 1044 | if (error.Success()) |
| 1045 | error.SetErrorString("not connected to remote gdb server"); |
| 1046 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1047 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1048 | m_gdb_comm.GetThreadSuffixSupported (); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1049 | m_gdb_comm.GetListThreadsInStopReplySupported (); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1050 | m_gdb_comm.GetHostInfo (); |
| 1051 | m_gdb_comm.GetVContSupported ('c'); |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1052 | m_gdb_comm.GetVAttachOrWaitSupported(); |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 1053 | |
| 1054 | size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); |
| 1055 | for (size_t idx = 0; idx < num_cmds; idx++) |
| 1056 | { |
| 1057 | StringExtractorGDBRemote response; |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 1058 | m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false); |
| 1059 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1060 | return error; |
| 1061 | } |
| 1062 | |
| 1063 | void |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1064 | ProcessGDBRemote::DidLaunchOrAttach (ArchSpec& process_arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1065 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1066 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1067 | if (log) |
| 1068 | log->Printf ("ProcessGDBRemote::DidLaunch()"); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 1069 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1071 | BuildDynamicRegisterInfo (false); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1073 | // See if the GDB server supports the qHostInfo information |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1074 | |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1075 | |
| 1076 | // See if the GDB server supports the qProcessInfo packet, if so |
| 1077 | // prefer that over the Host information as it will be more specific |
| 1078 | // to our process. |
| 1079 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1080 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 1081 | if (remote_process_arch.IsValid()) |
| 1082 | { |
| 1083 | process_arch = remote_process_arch; |
| 1084 | if (log) |
| 1085 | log->Printf ("ProcessGDBRemote::%s gdb-remote had process architecture, using %s %s", |
| 1086 | __FUNCTION__, |
| 1087 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1088 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1089 | } |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1090 | else |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1091 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1092 | process_arch = m_gdb_comm.GetHostArchitecture(); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1093 | if (log) |
| 1094 | log->Printf ("ProcessGDBRemote::%s gdb-remote did not have process architecture, using gdb-remote host architecture %s %s", |
| 1095 | __FUNCTION__, |
| 1096 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1097 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1098 | } |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1099 | |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1100 | if (process_arch.IsValid()) |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1101 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1102 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1103 | if (target_arch.IsValid()) |
| 1104 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1105 | if (log) |
| 1106 | log->Printf ("ProcessGDBRemote::%s analyzing target arch, currently %s %s", |
| 1107 | __FUNCTION__, |
| 1108 | target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>", |
| 1109 | target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1110 | |
| 1111 | // 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] | 1112 | // ARM executables and shared libraries can have mixed ARM architectures. |
| 1113 | // You can have an armv6 executable, and if the host is armv7, then the |
| 1114 | // system will load the best possible architecture for all shared libraries |
| 1115 | // it has, so we really need to take the remote host architecture as our |
| 1116 | // defacto architecture in this case. |
| 1117 | |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1118 | if (process_arch.GetMachine() == llvm::Triple::arm && |
| 1119 | process_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1120 | { |
Jason Molenda | 921c01b | 2014-08-03 21:42:52 +0000 | [diff] [blame] | 1121 | GetTarget().SetArchitecture (process_arch); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1122 | if (log) |
| 1123 | log->Printf ("ProcessGDBRemote::%s remote process is ARM/Apple, setting target arch to %s %s", |
| 1124 | __FUNCTION__, |
| 1125 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1126 | 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] | 1127 | } |
| 1128 | else |
| 1129 | { |
| 1130 | // Fill in what is missing in the triple |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1131 | const llvm::Triple &remote_triple = process_arch.GetTriple(); |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1132 | llvm::Triple new_target_triple = target_arch.GetTriple(); |
| 1133 | if (new_target_triple.getVendorName().size() == 0) |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1134 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1135 | new_target_triple.setVendor (remote_triple.getVendor()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1136 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1137 | if (new_target_triple.getOSName().size() == 0) |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1138 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1139 | new_target_triple.setOS (remote_triple.getOS()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1140 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1141 | if (new_target_triple.getEnvironmentName().size() == 0) |
| 1142 | new_target_triple.setEnvironment (remote_triple.getEnvironment()); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1143 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1144 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1145 | ArchSpec new_target_arch = target_arch; |
| 1146 | new_target_arch.SetTriple(new_target_triple); |
| 1147 | GetTarget().SetArchitecture(new_target_arch); |
| 1148 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1149 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1150 | |
| 1151 | if (log) |
| 1152 | log->Printf ("ProcessGDBRemote::%s final target arch after adjustments for remote architecture: %s %s", |
| 1153 | __FUNCTION__, |
| 1154 | target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>", |
| 1155 | 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] | 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | // The target doesn't have a valid architecture yet, set it from |
| 1160 | // the architecture we got from the remote GDB server |
Jason Molenda | 921c01b | 2014-08-03 21:42:52 +0000 | [diff] [blame] | 1161 | GetTarget().SetArchitecture (process_arch); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1162 | } |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | void |
| 1168 | ProcessGDBRemote::DidLaunch () |
| 1169 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1170 | ArchSpec process_arch; |
| 1171 | DidLaunchOrAttach (process_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | Error |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1175 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1176 | { |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1177 | ProcessAttachInfo attach_info; |
| 1178 | return DoAttachToProcessWithID(attach_pid, attach_info); |
| 1179 | } |
| 1180 | |
| 1181 | Error |
| 1182 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 1183 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1184 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1185 | Error error; |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (log) |
| 1188 | log->Printf ("ProcessGDBRemote::%s()", __FUNCTION__); |
| 1189 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1190 | // Clear out and clean up from any current state |
| 1191 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1192 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 1193 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1194 | // Make sure we aren't already connected? |
| 1195 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1196 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1197 | error = LaunchAndConnectToDebugserver (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1200 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1201 | const char *error_string = error.AsCString(); |
| 1202 | if (error_string == NULL) |
| 1203 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
| 1204 | |
| 1205 | SetExitStatus (-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1206 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | if (error.Success()) |
| 1210 | { |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1211 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1212 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1213 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1214 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); |
Greg Clayton | 3b60842 | 2011-11-19 02:11:30 +0000 | [diff] [blame] | 1215 | SetID (attach_pid); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1216 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1220 | return error; |
| 1221 | } |
| 1222 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1223 | Error |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 1224 | ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1225 | { |
| 1226 | Error error; |
| 1227 | // Clear out and clean up from any current state |
| 1228 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1229 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1230 | if (process_name && process_name[0]) |
| 1231 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1232 | // Make sure we aren't already connected? |
| 1233 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1234 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1235 | error = LaunchAndConnectToDebugserver (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1236 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1237 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1238 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1239 | const char *error_string = error.AsCString(); |
| 1240 | if (error_string == NULL) |
| 1241 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1242 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1243 | SetExitStatus (-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1244 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | if (error.Success()) |
| 1248 | { |
| 1249 | StreamString packet; |
| 1250 | |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1251 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
| 1252 | |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 1253 | if (attach_info.GetWaitForLaunch()) |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1254 | { |
| 1255 | if (!m_gdb_comm.GetVAttachOrWaitSupported()) |
| 1256 | { |
| 1257 | packet.PutCString ("vAttachWait"); |
| 1258 | } |
| 1259 | else |
| 1260 | { |
| 1261 | if (attach_info.GetIgnoreExisting()) |
| 1262 | packet.PutCString("vAttachWait"); |
| 1263 | else |
| 1264 | packet.PutCString ("vAttachOrWait"); |
| 1265 | } |
| 1266 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1267 | else |
| 1268 | packet.PutCString("vAttachName"); |
| 1269 | packet.PutChar(';'); |
| 1270 | packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
| 1271 | |
| 1272 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize())); |
| 1273 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1276 | return error; |
| 1277 | } |
| 1278 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1279 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1280 | bool |
| 1281 | ProcessGDBRemote::SetExitStatus (int exit_status, const char *cstr) |
| 1282 | { |
| 1283 | m_gdb_comm.Disconnect(); |
| 1284 | return Process::SetExitStatus (exit_status, cstr); |
| 1285 | } |
| 1286 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1287 | void |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1288 | ProcessGDBRemote::DidAttach (ArchSpec &process_arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1289 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1290 | // If you can figure out what the architecture is, fill it in here. |
| 1291 | process_arch.Clear(); |
| 1292 | DidLaunchOrAttach (process_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1296 | Error |
| 1297 | ProcessGDBRemote::WillResume () |
| 1298 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1299 | m_continue_c_tids.clear(); |
| 1300 | m_continue_C_tids.clear(); |
| 1301 | m_continue_s_tids.clear(); |
| 1302 | m_continue_S_tids.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1303 | return Error(); |
| 1304 | } |
| 1305 | |
| 1306 | Error |
| 1307 | ProcessGDBRemote::DoResume () |
| 1308 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1309 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1310 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1311 | if (log) |
| 1312 | log->Printf ("ProcessGDBRemote::Resume()"); |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1313 | |
| 1314 | Listener listener ("gdb-remote.resume-packet-sent"); |
| 1315 | if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) |
| 1316 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1317 | listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); |
| 1318 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1319 | const size_t num_threads = GetThreadList().GetSize(); |
| 1320 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1321 | StreamString continue_packet; |
| 1322 | bool continue_packet_error = false; |
| 1323 | if (m_gdb_comm.HasAnyVContSupport ()) |
| 1324 | { |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 1325 | if (m_continue_c_tids.size() == num_threads || |
| 1326 | (m_continue_c_tids.empty() && |
| 1327 | m_continue_C_tids.empty() && |
| 1328 | m_continue_s_tids.empty() && |
| 1329 | m_continue_S_tids.empty())) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1330 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1331 | // All threads are continuing, just send a "c" packet |
| 1332 | continue_packet.PutCString ("c"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1333 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1334 | else |
| 1335 | { |
| 1336 | continue_packet.PutCString ("vCont"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1337 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1338 | if (!m_continue_c_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1339 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1340 | if (m_gdb_comm.GetVContSupported ('c')) |
| 1341 | { |
| 1342 | 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) |
| 1343 | continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); |
| 1344 | } |
| 1345 | else |
| 1346 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1347 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1348 | |
| 1349 | if (!continue_packet_error && !m_continue_C_tids.empty()) |
| 1350 | { |
| 1351 | if (m_gdb_comm.GetVContSupported ('C')) |
| 1352 | { |
| 1353 | 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) |
| 1354 | continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1355 | } |
| 1356 | else |
| 1357 | continue_packet_error = true; |
| 1358 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1359 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1360 | if (!continue_packet_error && !m_continue_s_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1361 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1362 | if (m_gdb_comm.GetVContSupported ('s')) |
| 1363 | { |
| 1364 | 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) |
| 1365 | continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); |
| 1366 | } |
| 1367 | else |
| 1368 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1369 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1370 | |
| 1371 | if (!continue_packet_error && !m_continue_S_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1372 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1373 | if (m_gdb_comm.GetVContSupported ('S')) |
| 1374 | { |
| 1375 | 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) |
| 1376 | continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1377 | } |
| 1378 | else |
| 1379 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1380 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (continue_packet_error) |
| 1383 | continue_packet.GetString().clear(); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1384 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1385 | } |
| 1386 | else |
| 1387 | continue_packet_error = true; |
| 1388 | |
| 1389 | if (continue_packet_error) |
| 1390 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1391 | // Either no vCont support, or we tried to use part of the vCont |
| 1392 | // packet that wasn't supported by the remote GDB server. |
| 1393 | // 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] | 1394 | const size_t num_continue_c_tids = m_continue_c_tids.size(); |
| 1395 | const size_t num_continue_C_tids = m_continue_C_tids.size(); |
| 1396 | const size_t num_continue_s_tids = m_continue_s_tids.size(); |
| 1397 | const size_t num_continue_S_tids = m_continue_S_tids.size(); |
| 1398 | if (num_continue_c_tids > 0) |
| 1399 | { |
| 1400 | if (num_continue_c_tids == num_threads) |
| 1401 | { |
| 1402 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1403 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1404 | continue_packet.PutChar ('c'); |
| 1405 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1406 | } |
| 1407 | else if (num_continue_c_tids == 1 && |
| 1408 | num_continue_C_tids == 0 && |
| 1409 | num_continue_s_tids == 0 && |
| 1410 | num_continue_S_tids == 0 ) |
| 1411 | { |
| 1412 | // Only one thread is continuing |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1413 | m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1414 | continue_packet.PutChar ('c'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1415 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1419 | if (continue_packet_error && num_continue_C_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1420 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1421 | if ((num_continue_C_tids + num_continue_c_tids) == num_threads && |
| 1422 | num_continue_C_tids > 0 && |
| 1423 | num_continue_s_tids == 0 && |
| 1424 | num_continue_S_tids == 0 ) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1425 | { |
| 1426 | const int continue_signo = m_continue_C_tids.front().second; |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1427 | // Only one thread is continuing |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1428 | if (num_continue_C_tids > 1) |
| 1429 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1430 | // More that one thread with a signal, yet we don't have |
| 1431 | // vCont support and we are being asked to resume each |
| 1432 | // thread with a signal, we need to make sure they are |
| 1433 | // all the same signal, or we can't issue the continue |
| 1434 | // accurately with the current support... |
| 1435 | if (num_continue_C_tids > 1) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1436 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1437 | continue_packet_error = false; |
| 1438 | for (size_t i=1; i<m_continue_C_tids.size(); ++i) |
| 1439 | { |
| 1440 | if (m_continue_C_tids[i].second != continue_signo) |
| 1441 | continue_packet_error = true; |
| 1442 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1443 | } |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1444 | if (!continue_packet_error) |
| 1445 | m_gdb_comm.SetCurrentThreadForRun (-1); |
| 1446 | } |
| 1447 | else |
| 1448 | { |
| 1449 | // Set the continue thread ID |
| 1450 | continue_packet_error = false; |
| 1451 | m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1452 | } |
| 1453 | if (!continue_packet_error) |
| 1454 | { |
| 1455 | // Add threads continuing with the same signo... |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1456 | continue_packet.Printf("C%2.2x", continue_signo); |
| 1457 | } |
| 1458 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1461 | if (continue_packet_error && num_continue_s_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1462 | { |
| 1463 | if (num_continue_s_tids == num_threads) |
| 1464 | { |
| 1465 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1466 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1467 | continue_packet.PutChar ('s'); |
| 1468 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1469 | } |
| 1470 | else if (num_continue_c_tids == 0 && |
| 1471 | num_continue_C_tids == 0 && |
| 1472 | num_continue_s_tids == 1 && |
| 1473 | num_continue_S_tids == 0 ) |
| 1474 | { |
| 1475 | // Only one thread is stepping |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1476 | m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1477 | continue_packet.PutChar ('s'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1478 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | if (!continue_packet_error && num_continue_S_tids > 0) |
| 1483 | { |
| 1484 | if (num_continue_S_tids == num_threads) |
| 1485 | { |
| 1486 | const int step_signo = m_continue_S_tids.front().second; |
| 1487 | // Are all threads trying to step with the same signal? |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1488 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1489 | if (num_continue_S_tids > 1) |
| 1490 | { |
| 1491 | for (size_t i=1; i<num_threads; ++i) |
| 1492 | { |
| 1493 | if (m_continue_S_tids[i].second != step_signo) |
| 1494 | continue_packet_error = true; |
| 1495 | } |
| 1496 | } |
| 1497 | if (!continue_packet_error) |
| 1498 | { |
| 1499 | // Add threads stepping with the same signo... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1500 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1501 | continue_packet.Printf("S%2.2x", step_signo); |
| 1502 | } |
| 1503 | } |
| 1504 | else if (num_continue_c_tids == 0 && |
| 1505 | num_continue_C_tids == 0 && |
| 1506 | num_continue_s_tids == 0 && |
| 1507 | num_continue_S_tids == 1 ) |
| 1508 | { |
| 1509 | // Only one thread is stepping with signal |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1510 | m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1511 | continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1512 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | if (continue_packet_error) |
| 1518 | { |
| 1519 | error.SetErrorString ("can't make continue packet for this resume"); |
| 1520 | } |
| 1521 | else |
| 1522 | { |
| 1523 | EventSP event_sp; |
| 1524 | TimeValue timeout; |
| 1525 | timeout = TimeValue::Now(); |
| 1526 | timeout.OffsetWithSeconds (5); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1527 | if (!m_async_thread.IsJoinable()) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1528 | { |
| 1529 | error.SetErrorString ("Trying to resume but the async thread is dead."); |
| 1530 | if (log) |
| 1531 | log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead."); |
| 1532 | return error; |
| 1533 | } |
| 1534 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1535 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize())); |
| 1536 | |
| 1537 | if (listener.WaitForEvent (&timeout, event_sp) == false) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1538 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1539 | error.SetErrorString("Resume timed out."); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1540 | if (log) |
| 1541 | log->Printf ("ProcessGDBRemote::DoResume: Resume timed out."); |
| 1542 | } |
| 1543 | else if (event_sp->BroadcasterIs (&m_async_broadcaster)) |
| 1544 | { |
| 1545 | error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1546 | if (log) |
| 1547 | log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1548 | return error; |
| 1549 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1550 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1553 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1556 | void |
| 1557 | ProcessGDBRemote::ClearThreadIDList () |
| 1558 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1559 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1560 | m_thread_ids.clear(); |
| 1561 | } |
| 1562 | |
| 1563 | bool |
| 1564 | ProcessGDBRemote::UpdateThreadIDList () |
| 1565 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1566 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1567 | bool sequence_mutex_unavailable = false; |
| 1568 | m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable); |
| 1569 | if (sequence_mutex_unavailable) |
| 1570 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1571 | return false; // We just didn't get the list |
| 1572 | } |
| 1573 | return true; |
| 1574 | } |
| 1575 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1576 | bool |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1577 | ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1578 | { |
| 1579 | // locker will keep a mutex locked until it goes out of scope |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1580 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 1581 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1582 | log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1583 | |
| 1584 | size_t num_thread_ids = m_thread_ids.size(); |
| 1585 | // The "m_thread_ids" thread ID list should always be updated after each stop |
| 1586 | // reply packet, but in case it isn't, update it here. |
| 1587 | if (num_thread_ids == 0) |
| 1588 | { |
| 1589 | if (!UpdateThreadIDList ()) |
| 1590 | return false; |
| 1591 | num_thread_ids = m_thread_ids.size(); |
| 1592 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1593 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1594 | ThreadList old_thread_list_copy(old_thread_list); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1595 | if (num_thread_ids > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1596 | { |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1597 | for (size_t i=0; i<num_thread_ids; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1598 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1599 | tid_t tid = m_thread_ids[i]; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1600 | ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1601 | if (!thread_sp) |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1602 | { |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1603 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1604 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1605 | log->Printf( |
| 1606 | "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1607 | __FUNCTION__, static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1608 | thread_sp->GetID()); |
| 1609 | } |
| 1610 | else |
| 1611 | { |
| 1612 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1613 | log->Printf( |
| 1614 | "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1615 | __FUNCTION__, static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1616 | thread_sp->GetID()); |
| 1617 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1618 | new_thread_list.AddThread(thread_sp); |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1619 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1620 | } |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Whatever that is left in old_thread_list_copy are not |
| 1623 | // present in new_thread_list. Remove non-existent threads from internal id table. |
| 1624 | size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); |
| 1625 | for (size_t i=0; i<old_num_thread_ids; i++) |
| 1626 | { |
| 1627 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false)); |
| 1628 | if (old_thread_sp) |
| 1629 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1630 | lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1631 | m_thread_id_to_index_id_map.erase(old_thread_id); |
| 1632 | } |
| 1633 | } |
| 1634 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1635 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | StateType |
| 1640 | ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet) |
| 1641 | { |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 1642 | stop_packet.SetFilePos (0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1643 | const char stop_type = stop_packet.GetChar(); |
| 1644 | switch (stop_type) |
| 1645 | { |
| 1646 | case 'T': |
| 1647 | case 'S': |
| 1648 | { |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1649 | // This is a bit of a hack, but is is required. If we did exec, we |
| 1650 | // need to clear our thread lists and also know to rebuild our dynamic |
| 1651 | // register info before we lookup and threads and populate the expedited |
| 1652 | // register values so we need to know this right away so we can cleanup |
| 1653 | // and update our registers. |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1654 | const uint32_t stop_id = GetStopID(); |
| 1655 | if (stop_id == 0) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1656 | { |
| 1657 | // Our first stop, make sure we have a process ID, and also make |
| 1658 | // sure we know about our registers |
| 1659 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1660 | { |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1661 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1662 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1663 | SetID (pid); |
| 1664 | } |
| 1665 | BuildDynamicRegisterInfo (true); |
| 1666 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1667 | // Stop with signal and thread info |
| 1668 | const uint8_t signo = stop_packet.GetHexU8(); |
| 1669 | std::string name; |
| 1670 | std::string value; |
| 1671 | std::string thread_name; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1672 | std::string reason; |
| 1673 | std::string description; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1674 | uint32_t exc_type = 0; |
Greg Clayton | 896dff6 | 2010-07-23 16:45:51 +0000 | [diff] [blame] | 1675 | std::vector<addr_t> exc_data; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1676 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1677 | ThreadSP thread_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1678 | ThreadGDBRemote *gdb_thread = NULL; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1679 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1680 | while (stop_packet.GetNameColonValue(name, value)) |
| 1681 | { |
| 1682 | if (name.compare("metype") == 0) |
| 1683 | { |
| 1684 | // exception type in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1685 | exc_type = StringConvert::ToUInt32 (value.c_str(), 0, 16); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1686 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1687 | else if (name.compare("medata") == 0) |
| 1688 | { |
| 1689 | // exception data in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1690 | exc_data.push_back(StringConvert::ToUInt64 (value.c_str(), 0, 16)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1691 | } |
| 1692 | else if (name.compare("thread") == 0) |
| 1693 | { |
| 1694 | // thread in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1695 | lldb::tid_t tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1696 | // m_thread_list_real does have its own mutex, but we need to |
| 1697 | // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...) |
| 1698 | // and the m_thread_list_real.AddThread(...) so it doesn't change on us |
| 1699 | Mutex::Locker locker (m_thread_list_real.GetMutex ()); |
| 1700 | thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1701 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1702 | if (!thread_sp) |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1703 | { |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1704 | // Create the thread if we need to |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1705 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1706 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
| 1707 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1708 | log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
| 1709 | __FUNCTION__, |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1710 | static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1711 | thread_sp->GetID()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1712 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1713 | m_thread_list_real.AddThread(thread_sp); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1714 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1715 | gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); |
| 1716 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1717 | } |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1718 | else if (name.compare("threads") == 0) |
| 1719 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1720 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1721 | m_thread_ids.clear(); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1722 | // A comma separated list of all threads in the current |
| 1723 | // process that includes the thread for this stop reply |
| 1724 | // packet |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1725 | size_t comma_pos; |
| 1726 | lldb::tid_t tid; |
| 1727 | while ((comma_pos = value.find(',')) != std::string::npos) |
| 1728 | { |
| 1729 | value[comma_pos] = '\0'; |
| 1730 | // thread in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1731 | tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1732 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1733 | m_thread_ids.push_back (tid); |
| 1734 | value.erase(0, comma_pos + 1); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1735 | } |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1736 | tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1737 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1738 | m_thread_ids.push_back (tid); |
| 1739 | } |
Greg Clayton | de9d049 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 1740 | else if (name.compare("hexname") == 0) |
| 1741 | { |
| 1742 | StringExtractor name_extractor; |
| 1743 | // Swap "value" over into "name_extractor" |
| 1744 | name_extractor.GetStringRef().swap(value); |
| 1745 | // Now convert the HEX bytes into a string value |
| 1746 | name_extractor.GetHexByteString (value); |
| 1747 | thread_name.swap (value); |
| 1748 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1749 | else if (name.compare("name") == 0) |
| 1750 | { |
| 1751 | thread_name.swap (value); |
| 1752 | } |
Greg Clayton | 6f35f5c | 2010-09-09 06:32:46 +0000 | [diff] [blame] | 1753 | else if (name.compare("qaddr") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1754 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1755 | thread_dispatch_qaddr = StringConvert::ToUInt64 (value.c_str(), 0, 16); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1756 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1757 | else if (name.compare("reason") == 0) |
| 1758 | { |
| 1759 | reason.swap(value); |
| 1760 | } |
| 1761 | else if (name.compare("description") == 0) |
| 1762 | { |
| 1763 | StringExtractor desc_extractor; |
| 1764 | // Swap "value" over into "name_extractor" |
| 1765 | desc_extractor.GetStringRef().swap(value); |
| 1766 | // Now convert the HEX bytes into a string value |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 1767 | desc_extractor.GetHexByteString (value); |
| 1768 | description.swap(value); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1769 | } |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1770 | else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1])) |
| 1771 | { |
| 1772 | // We have a register number that contains an expedited |
| 1773 | // register value. Lets supply this register to our thread |
| 1774 | // so it won't have to go and read it. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1775 | if (gdb_thread) |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1776 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1777 | uint32_t reg = StringConvert::ToUInt32 (name.c_str(), UINT32_MAX, 16); |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1778 | |
| 1779 | if (reg != UINT32_MAX) |
| 1780 | { |
| 1781 | StringExtractor reg_value_extractor; |
| 1782 | // Swap "value" over into "reg_value_extractor" |
| 1783 | reg_value_extractor.GetStringRef().swap(value); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1784 | if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor)) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1785 | { |
| 1786 | Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'", |
| 1787 | name.c_str(), |
| 1788 | reg, |
| 1789 | reg, |
| 1790 | reg_value_extractor.GetStringRef().c_str(), |
| 1791 | stop_packet.GetStringRef().c_str()); |
| 1792 | } |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1796 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1797 | |
Hafiz Abid Qadeer | 673b4a3 | 2014-02-20 10:23:20 +0000 | [diff] [blame] | 1798 | // If the response is old style 'S' packet which does not provide us with thread information |
| 1799 | // then update the thread list and choose the first one. |
| 1800 | if (!thread_sp) |
| 1801 | { |
| 1802 | UpdateThreadIDList (); |
| 1803 | |
| 1804 | if (!m_thread_ids.empty ()) |
| 1805 | { |
| 1806 | Mutex::Locker locker (m_thread_list_real.GetMutex ()); |
| 1807 | thread_sp = m_thread_list_real.FindThreadByProtocolID (m_thread_ids.front (), false); |
| 1808 | if (thread_sp) |
| 1809 | gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get ()); |
| 1810 | } |
| 1811 | } |
| 1812 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1813 | if (thread_sp) |
| 1814 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1815 | // Clear the stop info just in case we don't set it to anything |
| 1816 | thread_sp->SetStopInfo (StopInfoSP()); |
| 1817 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1818 | gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr); |
Jim Ingham | dd2fe7a | 2011-01-28 02:23:12 +0000 | [diff] [blame] | 1819 | gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1820 | if (exc_type != 0) |
| 1821 | { |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1822 | const size_t exc_data_size = exc_data.size(); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1823 | |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1824 | thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp, |
| 1825 | exc_type, |
| 1826 | exc_data_size, |
| 1827 | exc_data_size >= 1 ? exc_data[0] : 0, |
| 1828 | exc_data_size >= 2 ? exc_data[1] : 0, |
| 1829 | exc_data_size >= 3 ? exc_data[2] : 0)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1830 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1831 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1832 | { |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1833 | bool handled = false; |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1834 | bool did_exec = false; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1835 | if (!reason.empty()) |
| 1836 | { |
| 1837 | if (reason.compare("trace") == 0) |
| 1838 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1839 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1840 | handled = true; |
| 1841 | } |
| 1842 | else if (reason.compare("breakpoint") == 0) |
| 1843 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1844 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 1845 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1846 | if (bp_site_sp) |
| 1847 | { |
| 1848 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1849 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1850 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1851 | handled = true; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1852 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1853 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1854 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1855 | } |
| 1856 | else |
| 1857 | { |
| 1858 | StopInfoSP invalid_stop_info_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1859 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1860 | } |
| 1861 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1862 | } |
| 1863 | else if (reason.compare("trap") == 0) |
| 1864 | { |
| 1865 | // Let the trap just use the standard signal stop reason below... |
| 1866 | } |
| 1867 | else if (reason.compare("watchpoint") == 0) |
| 1868 | { |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 1869 | StringExtractor desc_extractor(description.c_str()); |
| 1870 | addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); |
| 1871 | uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32); |
| 1872 | watch_id_t watch_id = LLDB_INVALID_WATCH_ID; |
| 1873 | if (wp_addr != LLDB_INVALID_ADDRESS) |
| 1874 | { |
| 1875 | WatchpointSP wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_addr); |
| 1876 | if (wp_sp) |
| 1877 | { |
| 1878 | wp_sp->SetHardwareIndex(wp_index); |
| 1879 | watch_id = wp_sp->GetID(); |
| 1880 | } |
| 1881 | } |
| 1882 | if (watch_id == LLDB_INVALID_WATCH_ID) |
| 1883 | { |
| 1884 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_WATCHPOINTS)); |
| 1885 | if (log) log->Printf ("failed to find watchpoint"); |
| 1886 | } |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1887 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1888 | handled = true; |
| 1889 | } |
| 1890 | else if (reason.compare("exception") == 0) |
| 1891 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1892 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1893 | handled = true; |
| 1894 | } |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1895 | else if (reason.compare("exec") == 0) |
| 1896 | { |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1897 | did_exec = true; |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1898 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp)); |
| 1899 | handled = true; |
| 1900 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1901 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1902 | |
Jim Ingham | 40083a4 | 2014-02-24 19:49:46 +0000 | [diff] [blame] | 1903 | if (!handled && signo && did_exec == false) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1904 | { |
| 1905 | if (signo == SIGTRAP) |
| 1906 | { |
| 1907 | // Currently we are going to assume SIGTRAP means we are either |
| 1908 | // hitting a breakpoint or hardware single stepping. |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1909 | handled = true; |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 1910 | addr_t pc = thread_sp->GetRegisterContext()->GetPC() + m_breakpoint_pc_offset; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1911 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1912 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1913 | if (bp_site_sp) |
| 1914 | { |
| 1915 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1916 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1917 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1918 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1919 | { |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 1920 | if(m_breakpoint_pc_offset != 0) |
| 1921 | thread_sp->GetRegisterContext()->SetPC(pc); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1922 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1923 | } |
| 1924 | else |
| 1925 | { |
| 1926 | StopInfoSP invalid_stop_info_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1927 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1928 | } |
| 1929 | } |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1930 | else |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1931 | { |
Jim Ingham | 4dc613b | 2012-10-27 02:52:04 +0000 | [diff] [blame] | 1932 | // If we were stepping then assume the stop was the result of the trace. If we were |
| 1933 | // not stepping then report the SIGTRAP. |
| 1934 | // FIXME: We are still missing the case where we single step over a trap instruction. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1935 | if (thread_sp->GetTemporaryResumeState() == eStateStepping) |
| 1936 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Jim Ingham | 4dc613b | 2012-10-27 02:52:04 +0000 | [diff] [blame] | 1937 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1938 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | if (!handled) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1942 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo)); |
Jason Molenda | 8214b01 | 2013-04-25 01:33:46 +0000 | [diff] [blame] | 1943 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1944 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1945 | if (!description.empty()) |
| 1946 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1947 | lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1948 | if (stop_info_sp) |
| 1949 | { |
| 1950 | stop_info_sp->SetDescription (description.c_str()); |
Greg Clayton | 3418c85 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 1951 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1952 | else |
| 1953 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1954 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1958 | } |
| 1959 | return eStateStopped; |
| 1960 | } |
| 1961 | break; |
| 1962 | |
| 1963 | case 'W': |
Todd Fiala | ff6131a | 2014-05-19 04:57:23 +0000 | [diff] [blame] | 1964 | case 'X': |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1965 | // process exited |
| 1966 | return eStateExited; |
| 1967 | |
| 1968 | default: |
| 1969 | break; |
| 1970 | } |
| 1971 | return eStateInvalid; |
| 1972 | } |
| 1973 | |
| 1974 | void |
| 1975 | ProcessGDBRemote::RefreshStateAfterStop () |
| 1976 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1977 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1978 | m_thread_ids.clear(); |
| 1979 | // Set the thread stop info. It might have a "threads" key whose value is |
| 1980 | // a list of all thread IDs in the current process, so m_thread_ids might |
| 1981 | // get set. |
| 1982 | SetThreadStopInfo (m_last_stop_packet); |
| 1983 | // Check to see if SetThreadStopInfo() filled in m_thread_ids? |
| 1984 | if (m_thread_ids.empty()) |
| 1985 | { |
| 1986 | // No, we need to fetch the thread list manually |
| 1987 | UpdateThreadIDList(); |
| 1988 | } |
| 1989 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1990 | // Let all threads recover from stopping and do any clean up based |
| 1991 | // on the previous thread state (if any). |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1992 | m_thread_list_real.RefreshStateAfterStop(); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | Error |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1997 | ProcessGDBRemote::DoHalt (bool &caused_stop) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1998 | { |
| 1999 | Error error; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2000 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 2001 | bool timed_out = false; |
| 2002 | Mutex::Locker locker; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2003 | |
| 2004 | if (m_public_state.GetValue() == eStateAttaching) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2005 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2006 | // We are being asked to halt during an attach. We need to just close |
| 2007 | // our file handle and debugserver will go away, and we can be done... |
| 2008 | m_gdb_comm.Disconnect(); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2009 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2010 | else |
| 2011 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 2012 | if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out)) |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2013 | { |
| 2014 | if (timed_out) |
| 2015 | error.SetErrorString("timed out sending interrupt packet"); |
| 2016 | else |
| 2017 | error.SetErrorString("unknown error sending interrupt packet"); |
| 2018 | } |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 2019 | |
| 2020 | caused_stop = m_gdb_comm.GetInterruptWasSent (); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2021 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2022 | return error; |
| 2023 | } |
| 2024 | |
| 2025 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2026 | ProcessGDBRemote::DoDetach(bool keep_stopped) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2027 | { |
| 2028 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2029 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2030 | if (log) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2031 | log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); |
| 2032 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2033 | error = m_gdb_comm.Detach (keep_stopped); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2034 | if (log) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2035 | { |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2036 | if (error.Success()) |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2037 | log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully"); |
| 2038 | else |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2039 | 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] | 2040 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2041 | |
| 2042 | if (!error.Success()) |
| 2043 | return error; |
| 2044 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2045 | // Sleep for one second to let the process get all detached... |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2046 | StopAsyncThread (); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2047 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2048 | SetPrivateState (eStateDetached); |
| 2049 | ResumePrivateStateThread(); |
| 2050 | |
| 2051 | //KillDebugserverProcess (); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2052 | return error; |
| 2053 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2054 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2056 | Error |
| 2057 | ProcessGDBRemote::DoDestroy () |
| 2058 | { |
| 2059 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2060 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2061 | if (log) |
| 2062 | log->Printf ("ProcessGDBRemote::DoDestroy()"); |
| 2063 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2064 | // There is a bug in older iOS debugservers where they don't shut down the process |
| 2065 | // they are debugging properly. If the process is sitting at a breakpoint or an exception, |
| 2066 | // this can cause problems with restarting. So we check to see if any of our threads are stopped |
| 2067 | // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN |
| 2068 | // destroy it again. |
| 2069 | // |
| 2070 | // Note, we don't have a good way to test the version of debugserver, but I happen to know that |
| 2071 | // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of |
| 2072 | // the debugservers with this bug are equal. There really should be a better way to test this! |
| 2073 | // |
| 2074 | // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and |
| 2075 | // get called here to destroy again and we're still at a breakpoint or exception, then we should |
| 2076 | // just do the straight-forward kill. |
| 2077 | // |
| 2078 | // And of course, if we weren't able to stop the process by the time we get here, it isn't |
| 2079 | // necessary (or helpful) to do any of this. |
| 2080 | |
| 2081 | if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning) |
| 2082 | { |
| 2083 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 2084 | |
| 2085 | // FIXME: These should be ConstStrings so we aren't doing strcmp'ing. |
| 2086 | if (platform_sp |
| 2087 | && platform_sp->GetName() |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 2088 | && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic()) |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2089 | { |
| 2090 | if (m_destroy_tried_resuming) |
| 2091 | { |
| 2092 | if (log) |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2093 | 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] | 2094 | } |
| 2095 | else |
| 2096 | { |
| 2097 | // At present, the plans are discarded and the breakpoints disabled Process::Destroy, |
| 2098 | // but we really need it to happen here and it doesn't matter if we do it twice. |
| 2099 | m_thread_list.DiscardThreadPlans(); |
| 2100 | DisableAllBreakpointSites(); |
| 2101 | |
| 2102 | bool stop_looks_like_crash = false; |
| 2103 | ThreadList &threads = GetThreadList(); |
| 2104 | |
| 2105 | { |
Jim Ingham | 4535037 | 2012-09-11 00:08:52 +0000 | [diff] [blame] | 2106 | Mutex::Locker locker(threads.GetMutex()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2107 | |
| 2108 | size_t num_threads = threads.GetSize(); |
| 2109 | for (size_t i = 0; i < num_threads; i++) |
| 2110 | { |
| 2111 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 2112 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2113 | StopReason reason = eStopReasonInvalid; |
| 2114 | if (stop_info_sp) |
| 2115 | reason = stop_info_sp->GetStopReason(); |
| 2116 | if (reason == eStopReasonBreakpoint |
| 2117 | || reason == eStopReasonException) |
| 2118 | { |
| 2119 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2120 | log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.", |
| 2121 | thread_sp->GetProtocolID(), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2122 | stop_info_sp->GetDescription()); |
| 2123 | stop_looks_like_crash = true; |
| 2124 | break; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | if (stop_looks_like_crash) |
| 2130 | { |
| 2131 | if (log) |
| 2132 | log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill."); |
| 2133 | m_destroy_tried_resuming = true; |
| 2134 | |
| 2135 | // If we are going to run again before killing, it would be good to suspend all the threads |
| 2136 | // before resuming so they won't get into more trouble. Sadly, for the threads stopped with |
| 2137 | // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do |
| 2138 | // have to run the risk of letting those threads proceed a bit. |
| 2139 | |
| 2140 | { |
Jim Ingham | 4535037 | 2012-09-11 00:08:52 +0000 | [diff] [blame] | 2141 | Mutex::Locker locker(threads.GetMutex()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2142 | |
| 2143 | size_t num_threads = threads.GetSize(); |
| 2144 | for (size_t i = 0; i < num_threads; i++) |
| 2145 | { |
| 2146 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 2147 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2148 | StopReason reason = eStopReasonInvalid; |
| 2149 | if (stop_info_sp) |
| 2150 | reason = stop_info_sp->GetStopReason(); |
| 2151 | if (reason != eStopReasonBreakpoint |
| 2152 | && reason != eStopReasonException) |
| 2153 | { |
| 2154 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2155 | log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.", |
| 2156 | thread_sp->GetProtocolID()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2157 | thread_sp->SetResumeState(eStateSuspended); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | Resume (); |
| 2162 | return Destroy(); |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2168 | // Interrupt if our inferior is running... |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2169 | int exit_status = SIGABRT; |
| 2170 | std::string exit_string; |
| 2171 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 2172 | if (m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2173 | { |
Jim Ingham | aab7837 | 2011-10-28 01:11:35 +0000 | [diff] [blame] | 2174 | if (m_public_state.GetValue() != eStateAttaching) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2175 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2176 | StringExtractorGDBRemote response; |
| 2177 | bool send_async = true; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 2178 | GDBRemoteCommunication::ScopedTimeout (m_gdb_comm, 3); |
Filipe Cabecinhas | 9e10605 | 2012-08-22 13:25:58 +0000 | [diff] [blame] | 2179 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2180 | 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] | 2181 | { |
| 2182 | char packet_cmd = response.GetChar(0); |
| 2183 | |
| 2184 | if (packet_cmd == 'W' || packet_cmd == 'X') |
| 2185 | { |
Jim Ingham | eac0aa4 | 2014-02-21 22:36:11 +0000 | [diff] [blame] | 2186 | #if defined(__APPLE__) |
Jim Ingham | 9d67cc5 | 2014-02-21 22:35:29 +0000 | [diff] [blame] | 2187 | // For Native processes on Mac OS X, we launch through the Host Platform, then hand the process off |
| 2188 | // to debugserver, which becomes the parent process through "PT_ATTACH". Then when we go to kill |
| 2189 | // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then we call waitpid which returns |
| 2190 | // with no error and the correct status. But amusingly enough that doesn't seem to actually reap |
| 2191 | // the process, but instead it is left around as a Zombie. Probably the kernel is in the process of |
| 2192 | // switching ownership back to lldb which was the original parent, and gets confused in the handoff. |
| 2193 | // Anyway, so call waitpid here to finally reap it. |
| 2194 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2195 | if (platform_sp && platform_sp->IsHost()) |
| 2196 | { |
| 2197 | int status; |
| 2198 | ::pid_t reap_pid; |
| 2199 | reap_pid = waitpid (GetID(), &status, WNOHANG); |
| 2200 | if (log) |
| 2201 | log->Printf ("Reaped pid: %d, status: %d.\n", reap_pid, status); |
| 2202 | } |
| 2203 | #endif |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2204 | SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2205 | ClearThreadIDList (); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2206 | exit_status = response.GetHexU8(); |
| 2207 | } |
| 2208 | else |
| 2209 | { |
| 2210 | if (log) |
| 2211 | log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str()); |
| 2212 | exit_string.assign("got unexpected response to k packet: "); |
| 2213 | exit_string.append(response.GetStringRef()); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | else |
| 2217 | { |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2218 | if (log) |
| 2219 | log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); |
| 2220 | exit_string.assign("failed to send the k packet"); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2221 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2222 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2223 | else |
| 2224 | { |
| 2225 | if (log) |
Ed Maste | b3a5333 | 2014-03-17 17:05:22 +0000 | [diff] [blame] | 2226 | log->Printf ("ProcessGDBRemote::DoDestroy - killed or interrupted while attaching"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 2227 | exit_string.assign ("killed or interrupted while attaching."); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2228 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2229 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2230 | else |
| 2231 | { |
| 2232 | // If we missed setting the exit status on the way out, do it here. |
| 2233 | // NB set exit status can be called multiple times, the first one sets the status. |
| 2234 | exit_string.assign("destroying when not connected to debugserver"); |
| 2235 | } |
| 2236 | |
| 2237 | SetExitStatus(exit_status, exit_string.c_str()); |
| 2238 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2239 | StopAsyncThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2240 | KillDebugserverProcess (); |
| 2241 | return error; |
| 2242 | } |
| 2243 | |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2244 | void |
| 2245 | ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response) |
| 2246 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 2247 | Mutex::Locker locker (m_last_stop_packet_mutex); |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2248 | const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos; |
| 2249 | if (did_exec) |
| 2250 | { |
| 2251 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2252 | if (log) |
| 2253 | log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec"); |
| 2254 | |
| 2255 | m_thread_list_real.Clear(); |
| 2256 | m_thread_list.Clear(); |
| 2257 | BuildDynamicRegisterInfo (true); |
| 2258 | m_gdb_comm.ResetDiscoverableSettings(); |
| 2259 | } |
| 2260 | m_last_stop_packet = response; |
| 2261 | } |
| 2262 | |
| 2263 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2264 | //------------------------------------------------------------------ |
| 2265 | // Process Queries |
| 2266 | //------------------------------------------------------------------ |
| 2267 | |
| 2268 | bool |
| 2269 | ProcessGDBRemote::IsAlive () |
| 2270 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 2271 | return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | addr_t |
| 2275 | ProcessGDBRemote::GetImageInfoAddress() |
| 2276 | { |
Jason Molenda | 6ba6d3d | 2013-01-30 04:39:32 +0000 | [diff] [blame] | 2277 | return m_gdb_comm.GetShlibInfoAddr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2280 | //------------------------------------------------------------------ |
| 2281 | // Process Memory |
| 2282 | //------------------------------------------------------------------ |
| 2283 | size_t |
| 2284 | ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2285 | { |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2286 | GetMaxMemorySize (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2287 | if (size > m_max_memory_size) |
| 2288 | { |
| 2289 | // Keep memory read sizes down to a sane limit. This function will be |
| 2290 | // called multiple times in order to complete the task by |
| 2291 | // lldb_private::Process so it is ok to do this. |
| 2292 | size = m_max_memory_size; |
| 2293 | } |
| 2294 | |
| 2295 | char packet[64]; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2296 | int packet_len; |
| 2297 | bool binary_memory_read = m_gdb_comm.GetxPacketSupported(); |
| 2298 | if (binary_memory_read) |
| 2299 | { |
| 2300 | packet_len = ::snprintf (packet, sizeof(packet), "x0x%" PRIx64 ",0x%" PRIx64, (uint64_t)addr, (uint64_t)size); |
| 2301 | } |
| 2302 | else |
| 2303 | { |
| 2304 | packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size); |
| 2305 | } |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2306 | assert (packet_len + 1 < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2307 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2308 | 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] | 2309 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2310 | if (response.IsNormalResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2311 | { |
| 2312 | error.Clear(); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2313 | if (binary_memory_read) |
| 2314 | { |
| 2315 | // The lower level GDBRemoteCommunication packet receive layer has already de-quoted any |
| 2316 | // 0x7d character escaping that was present in the packet |
| 2317 | |
| 2318 | size_t data_received_size = response.GetBytesLeft(); |
| 2319 | if (data_received_size > size) |
| 2320 | { |
| 2321 | // Don't write past the end of BUF if the remote debug server gave us too |
| 2322 | // much data for some reason. |
| 2323 | data_received_size = size; |
| 2324 | } |
| 2325 | memcpy (buf, response.GetStringRef().data(), data_received_size); |
| 2326 | return data_received_size; |
| 2327 | } |
| 2328 | else |
| 2329 | { |
| 2330 | return response.GetHexBytes(buf, size, '\xdd'); |
| 2331 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2332 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2333 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 2334 | error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2335 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2336 | error.SetErrorStringWithFormat("GDB server does not support reading memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2337 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2338 | 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] | 2339 | } |
| 2340 | else |
| 2341 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 2342 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2343 | } |
| 2344 | return 0; |
| 2345 | } |
| 2346 | |
| 2347 | size_t |
| 2348 | ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2349 | { |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2350 | GetMaxMemorySize (); |
Greg Clayton | b4aaf2e | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 2351 | if (size > m_max_memory_size) |
| 2352 | { |
| 2353 | // Keep memory read sizes down to a sane limit. This function will be |
| 2354 | // called multiple times in order to complete the task by |
| 2355 | // lldb_private::Process so it is ok to do this. |
| 2356 | size = m_max_memory_size; |
| 2357 | } |
| 2358 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2359 | StreamString packet; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2360 | packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 2361 | packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2362 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2363 | 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] | 2364 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2365 | if (response.IsOKResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2366 | { |
| 2367 | error.Clear(); |
| 2368 | return size; |
| 2369 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2370 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 2371 | error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2372 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2373 | error.SetErrorStringWithFormat("GDB server does not support writing memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2374 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2375 | 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] | 2376 | } |
| 2377 | else |
| 2378 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 2379 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2380 | } |
| 2381 | return 0; |
| 2382 | } |
| 2383 | |
| 2384 | lldb::addr_t |
| 2385 | ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 2386 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 2387 | Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_EXPRESSIONS)); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2388 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 2389 | |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2390 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2391 | switch (supported) |
| 2392 | { |
| 2393 | case eLazyBoolCalculate: |
| 2394 | case eLazyBoolYes: |
| 2395 | allocated_addr = m_gdb_comm.AllocateMemory (size, permissions); |
| 2396 | if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes) |
| 2397 | return allocated_addr; |
| 2398 | |
| 2399 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2400 | // Call mmap() to create memory in the inferior.. |
| 2401 | unsigned prot = 0; |
| 2402 | if (permissions & lldb::ePermissionsReadable) |
| 2403 | prot |= eMmapProtRead; |
| 2404 | if (permissions & lldb::ePermissionsWritable) |
| 2405 | prot |= eMmapProtWrite; |
| 2406 | if (permissions & lldb::ePermissionsExecutable) |
| 2407 | prot |= eMmapProtExec; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2408 | |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2409 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 2410 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) |
| 2411 | m_addr_to_mmap_size[allocated_addr] = size; |
| 2412 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2413 | { |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2414 | allocated_addr = LLDB_INVALID_ADDRESS; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2415 | if (log) |
| 2416 | log->Printf ("ProcessGDBRemote::%s no direct stub support for memory allocation, and InferiorCallMmap also failed - is stub missing register context save/restore capability?", __FUNCTION__); |
| 2417 | } |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | } |
| 2420 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2421 | if (allocated_addr == LLDB_INVALID_ADDRESS) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2422 | 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] | 2423 | else |
| 2424 | error.Clear(); |
| 2425 | return allocated_addr; |
| 2426 | } |
| 2427 | |
| 2428 | Error |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 2429 | ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr, |
| 2430 | MemoryRegionInfo ®ion_info) |
| 2431 | { |
| 2432 | |
| 2433 | Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info)); |
| 2434 | return error; |
| 2435 | } |
| 2436 | |
| 2437 | Error |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 2438 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num) |
| 2439 | { |
| 2440 | |
| 2441 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num)); |
| 2442 | return error; |
| 2443 | } |
| 2444 | |
| 2445 | Error |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 2446 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after) |
| 2447 | { |
| 2448 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after)); |
| 2449 | return error; |
| 2450 | } |
| 2451 | |
| 2452 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2453 | ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr) |
| 2454 | { |
| 2455 | Error error; |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2456 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
| 2457 | |
| 2458 | switch (supported) |
| 2459 | { |
| 2460 | case eLazyBoolCalculate: |
| 2461 | // We should never be deallocating memory without allocating memory |
| 2462 | // first so we should never get eLazyBoolCalculate |
| 2463 | error.SetErrorString ("tried to deallocate memory without ever allocating memory"); |
| 2464 | break; |
| 2465 | |
| 2466 | case eLazyBoolYes: |
| 2467 | if (!m_gdb_comm.DeallocateMemory (addr)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2468 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2469 | break; |
| 2470 | |
| 2471 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2472 | // Call munmap() to deallocate memory in the inferior.. |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2473 | { |
| 2474 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2475 | if (pos != m_addr_to_mmap_size.end() && |
| 2476 | InferiorCallMunmap(this, addr, pos->second)) |
| 2477 | m_addr_to_mmap_size.erase (pos); |
| 2478 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2479 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2480 | } |
| 2481 | break; |
| 2482 | } |
| 2483 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2484 | return error; |
| 2485 | } |
| 2486 | |
| 2487 | |
| 2488 | //------------------------------------------------------------------ |
| 2489 | // Process STDIO |
| 2490 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2491 | size_t |
| 2492 | ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error) |
| 2493 | { |
| 2494 | if (m_stdio_communication.IsConnected()) |
| 2495 | { |
| 2496 | ConnectionStatus status; |
| 2497 | m_stdio_communication.Write(src, src_len, status, NULL); |
| 2498 | } |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 2499 | else if (m_stdin_forward) |
Vince Harron | e0be425 | 2015-02-06 18:32:57 +0000 | [diff] [blame] | 2500 | { |
| 2501 | m_gdb_comm.SendStdinNotification(src, src_len); |
| 2502 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2507 | ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2508 | { |
| 2509 | Error error; |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2510 | assert(bp_site != NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2511 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2512 | // Get logging info |
| 2513 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2514 | user_id_t site_id = bp_site->GetID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2515 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2516 | // Get the breakpoint address |
| 2517 | const addr_t addr = bp_site->GetLoadAddress(); |
| 2518 | |
| 2519 | // Log that a breakpoint was requested |
| 2520 | if (log) |
| 2521 | log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr); |
| 2522 | |
| 2523 | // Breakpoint already exists and is enabled |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2524 | if (bp_site->IsEnabled()) |
| 2525 | { |
| 2526 | if (log) |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2527 | 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] | 2528 | return error; |
| 2529 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2530 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2531 | // Get the software breakpoint trap opcode size |
| 2532 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2533 | |
| 2534 | // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this breakpoint type |
| 2535 | // is supported by the remote stub. These are set to true by default, and later set to false |
| 2536 | // only after we receive an unimplemented response when sending a breakpoint packet. This means |
| 2537 | // initially that unless we were specifically instructed to use a hardware breakpoint, LLDB will |
| 2538 | // attempt to set a software breakpoint. HardwareRequired() also queries a boolean variable which |
| 2539 | // indicates if the user specifically asked for hardware breakpoints. If true then we will |
| 2540 | // skip over software breakpoints. |
| 2541 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && (!bp_site->HardwareRequired())) |
| 2542 | { |
| 2543 | // Try to send off a software breakpoint packet ($Z0) |
| 2544 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2545 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2546 | // The breakpoint was placed successfully |
| 2547 | bp_site->SetEnabled(true); |
| 2548 | bp_site->SetType(BreakpointSite::eExternal); |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2549 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2550 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2551 | |
| 2552 | // SendGDBStoppointTypePacket() will return an error if it was unable to set this |
| 2553 | // breakpoint. We need to differentiate between a error specific to placing this breakpoint |
| 2554 | // or if we have learned that this breakpoint type is unsupported. To do this, we |
| 2555 | // must test the support boolean for this breakpoint type to see if it now indicates that |
| 2556 | // this breakpoint type is unsupported. If they are still supported then we should return |
| 2557 | // with the error code. If they are now unsupported, then we would like to fall through |
| 2558 | // and try another form of breakpoint. |
| 2559 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) |
| 2560 | return error; |
| 2561 | |
| 2562 | // We reach here when software breakpoints have been found to be unsupported. For future |
| 2563 | // calls to set a breakpoint, we will not attempt to set a breakpoint with a type that is |
| 2564 | // known not to be supported. |
| 2565 | if (log) |
| 2566 | log->Printf("Software breakpoints are unsupported"); |
| 2567 | |
| 2568 | // So we will fall through and try a hardware breakpoint |
| 2569 | } |
| 2570 | |
| 2571 | // The process of setting a hardware breakpoint is much the same as above. We check the |
| 2572 | // supported boolean for this breakpoint type, and if it is thought to be supported then we |
| 2573 | // will try to set this breakpoint with a hardware breakpoint. |
| 2574 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) |
| 2575 | { |
| 2576 | // Try to send off a hardware breakpoint packet ($Z1) |
| 2577 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2578 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2579 | // The breakpoint was placed successfully |
| 2580 | bp_site->SetEnabled(true); |
| 2581 | bp_site->SetType(BreakpointSite::eHardware); |
| 2582 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2583 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2584 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2585 | // Check if the error was something other then an unsupported breakpoint type |
| 2586 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) |
| 2587 | { |
| 2588 | // Unable to set this hardware breakpoint |
| 2589 | error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)"); |
| 2590 | return error; |
| 2591 | } |
| 2592 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2593 | // 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] | 2594 | if (log) |
| 2595 | log->Printf("Hardware breakpoints are unsupported"); |
| 2596 | |
| 2597 | // Finally we will falling through to a #trap style breakpoint |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2600 | // Don't fall through when hardware breakpoints were specifically requested |
| 2601 | if (bp_site->HardwareRequired()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2602 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2603 | error.SetErrorString("hardware breakpoints are not supported"); |
| 2604 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2605 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2606 | |
| 2607 | // As a last resort we want to place a manual breakpoint. An instruction |
| 2608 | // is placed into the process memory using memory write packets. |
| 2609 | return EnableSoftwareBreakpoint(bp_site); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2613 | ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2614 | { |
| 2615 | Error error; |
| 2616 | assert (bp_site != NULL); |
| 2617 | addr_t addr = bp_site->GetLoadAddress(); |
| 2618 | user_id_t site_id = bp_site->GetID(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2619 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2620 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2621 | 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] | 2622 | |
| 2623 | if (bp_site->IsEnabled()) |
| 2624 | { |
| 2625 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 2626 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2627 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 2628 | switch (bp_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2629 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2630 | case BreakpointSite::eSoftware: |
| 2631 | error = DisableSoftwareBreakpoint (bp_site); |
| 2632 | break; |
| 2633 | |
| 2634 | case BreakpointSite::eHardware: |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2635 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, addr, bp_op_size)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2636 | error.SetErrorToGenericError(); |
| 2637 | break; |
| 2638 | |
| 2639 | case BreakpointSite::eExternal: |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 2640 | { |
| 2641 | GDBStoppointType stoppoint_type; |
| 2642 | if (bp_site->IsHardware()) |
| 2643 | stoppoint_type = eBreakpointHardware; |
| 2644 | else |
| 2645 | stoppoint_type = eBreakpointSoftware; |
| 2646 | |
| 2647 | if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, bp_op_size)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2648 | error.SetErrorToGenericError(); |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 2649 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2650 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2651 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2652 | if (error.Success()) |
| 2653 | bp_site->SetEnabled(false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2654 | } |
| 2655 | else |
| 2656 | { |
| 2657 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2658 | 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] | 2659 | return error; |
| 2660 | } |
| 2661 | |
| 2662 | if (error.Success()) |
| 2663 | error.SetErrorToGenericError(); |
| 2664 | return error; |
| 2665 | } |
| 2666 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2667 | // Pre-requisite: wp != NULL. |
| 2668 | static GDBStoppointType |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2669 | GetGDBStoppointType (Watchpoint *wp) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2670 | { |
| 2671 | assert(wp); |
| 2672 | bool watch_read = wp->WatchpointRead(); |
| 2673 | bool watch_write = wp->WatchpointWrite(); |
| 2674 | |
| 2675 | // watch_read and watch_write cannot both be false. |
| 2676 | assert(watch_read || watch_write); |
| 2677 | if (watch_read && watch_write) |
| 2678 | return eWatchpointReadWrite; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2679 | else if (watch_read) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2680 | return eWatchpointRead; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2681 | else // Must be watch_write, then. |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2682 | return eWatchpointWrite; |
| 2683 | } |
| 2684 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2685 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2686 | ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2687 | { |
| 2688 | Error error; |
| 2689 | if (wp) |
| 2690 | { |
| 2691 | user_id_t watchID = wp->GetID(); |
| 2692 | addr_t addr = wp->GetLoadAddress(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2693 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2694 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2695 | log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2696 | if (wp->IsEnabled()) |
| 2697 | { |
| 2698 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2699 | 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] | 2700 | return error; |
| 2701 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2702 | |
| 2703 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 2704 | // Pass down an appropriate z/Z packet... |
| 2705 | if (m_gdb_comm.SupportsGDBStoppointPacket (type)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2706 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2707 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0) |
| 2708 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2709 | wp->SetEnabled(true, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2710 | return error; |
| 2711 | } |
| 2712 | else |
| 2713 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2714 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2715 | else |
| 2716 | error.SetErrorString("watchpoints not supported"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2717 | } |
| 2718 | else |
| 2719 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2720 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2721 | } |
| 2722 | if (error.Success()) |
| 2723 | error.SetErrorToGenericError(); |
| 2724 | return error; |
| 2725 | } |
| 2726 | |
| 2727 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2728 | ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2729 | { |
| 2730 | Error error; |
| 2731 | if (wp) |
| 2732 | { |
| 2733 | user_id_t watchID = wp->GetID(); |
| 2734 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2735 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2736 | |
| 2737 | addr_t addr = wp->GetLoadAddress(); |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2738 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2739 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2740 | 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] | 2741 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2742 | if (!wp->IsEnabled()) |
| 2743 | { |
| 2744 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2745 | 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] | 2746 | // See also 'class WatchpointSentry' within StopInfo.cpp. |
| 2747 | // This disabling attempt might come from the user-supplied actions, we'll route it in order for |
| 2748 | // the watchpoint object to intelligently process this action. |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2749 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2750 | return error; |
| 2751 | } |
| 2752 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2753 | if (wp->IsHardware()) |
| 2754 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2755 | GDBStoppointType type = GetGDBStoppointType(wp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2756 | // Pass down an appropriate z/Z packet... |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2757 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0) |
| 2758 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2759 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2760 | return error; |
| 2761 | } |
| 2762 | else |
| 2763 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2764 | } |
| 2765 | // TODO: clear software watchpoints if we implement them |
| 2766 | } |
| 2767 | else |
| 2768 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2769 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2770 | } |
| 2771 | if (error.Success()) |
| 2772 | error.SetErrorToGenericError(); |
| 2773 | return error; |
| 2774 | } |
| 2775 | |
| 2776 | void |
| 2777 | ProcessGDBRemote::Clear() |
| 2778 | { |
| 2779 | m_flags = 0; |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 2780 | m_thread_list_real.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2781 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | Error |
| 2785 | ProcessGDBRemote::DoSignal (int signo) |
| 2786 | { |
| 2787 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2788 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2789 | if (log) |
| 2790 | log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo); |
| 2791 | |
| 2792 | if (!m_gdb_comm.SendAsyncSignal (signo)) |
| 2793 | error.SetErrorStringWithFormat("failed to send signal %i", signo); |
| 2794 | return error; |
| 2795 | } |
| 2796 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2797 | Error |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2798 | ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2799 | { |
| 2800 | Error error; |
| 2801 | if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) |
| 2802 | { |
| 2803 | // If we locate debugserver, keep that located version around |
| 2804 | static FileSpec g_debugserver_file_spec; |
| 2805 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2806 | ProcessLaunchInfo debugserver_launch_info; |
Oleksiy Vyalov | f8ce61c | 2015-01-28 17:36:59 +0000 | [diff] [blame] | 2807 | // Make debugserver run in its own session so signals generated by |
| 2808 | // special terminal key sequences (^C) don't affect debugserver. |
| 2809 | debugserver_launch_info.SetLaunchInSeparateProcessGroup(true); |
| 2810 | |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2811 | debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false); |
| 2812 | debugserver_launch_info.SetUserID(process_info.GetUserID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2813 | |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 2814 | #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__)) |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 2815 | // On iOS, still do a local connection using a random port |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 2816 | const char *hostname = "127.0.0.1"; |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 2817 | uint16_t port = get_random_port (); |
| 2818 | #else |
| 2819 | // Set hostname being NULL to do the reverse connect where debugserver |
| 2820 | // will bind to port zero and it will communicate back to us the port |
| 2821 | // that we will connect to |
| 2822 | const char *hostname = NULL; |
| 2823 | uint16_t port = 0; |
| 2824 | #endif |
| 2825 | |
| 2826 | error = m_gdb_comm.StartDebugserverProcess (hostname, |
| 2827 | port, |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 2828 | debugserver_launch_info, |
| 2829 | port); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2830 | |
| 2831 | if (error.Success ()) |
| 2832 | m_debugserver_pid = debugserver_launch_info.GetProcessID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2833 | else |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2834 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2835 | |
| 2836 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 2837 | StartAsyncThread (); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2838 | |
| 2839 | if (error.Fail()) |
| 2840 | { |
| 2841 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 2842 | |
| 2843 | if (log) |
| 2844 | log->Printf("failed to start debugserver process: %s", error.AsCString()); |
| 2845 | return error; |
| 2846 | } |
| 2847 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 2848 | if (m_gdb_comm.IsConnected()) |
| 2849 | { |
| 2850 | // Finish the connection process by doing the handshake without connecting (send NULL URL) |
| 2851 | ConnectToDebugserver (NULL); |
| 2852 | } |
| 2853 | else |
| 2854 | { |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 2855 | StreamString connect_url; |
| 2856 | connect_url.Printf("connect://%s:%u", hostname, port); |
| 2857 | error = ConnectToDebugserver (connect_url.GetString().c_str()); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 2858 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2860 | } |
| 2861 | return error; |
| 2862 | } |
| 2863 | |
| 2864 | bool |
| 2865 | ProcessGDBRemote::MonitorDebugserverProcess |
| 2866 | ( |
| 2867 | void *callback_baton, |
| 2868 | lldb::pid_t debugserver_pid, |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2869 | bool exited, // True if the process did exit |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2870 | int signo, // Zero for no signal |
| 2871 | int exit_status // Exit value of process if signal is zero |
| 2872 | ) |
| 2873 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2874 | // The baton is a "ProcessGDBRemote *". Now this class might be gone |
| 2875 | // and might not exist anymore, so we need to carefully try to get the |
| 2876 | // target for this process first since we have a race condition when |
| 2877 | // we are done running between getting the notice that the inferior |
| 2878 | // process has died and the debugserver that was debugging this process. |
| 2879 | // In our test suite, we are also continually running process after |
| 2880 | // process, so we must be very careful to make sure: |
| 2881 | // 1 - process object hasn't been deleted already |
| 2882 | // 2 - that a new process object hasn't been recreated in its place |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2883 | |
| 2884 | // "debugserver_pid" argument passed in is the process ID for |
| 2885 | // debugserver that we are tracking... |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2886 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2887 | |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2888 | ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2889 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2890 | // Get a shared pointer to the target that has a matching process pointer. |
| 2891 | // This target could be gone, or the target could already have a new process |
| 2892 | // object inside of it |
| 2893 | TargetSP target_sp (Debugger::FindTargetWithProcess(process)); |
| 2894 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2895 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2896 | log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2897 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2898 | if (target_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2899 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2900 | // We found a process in a target that matches, but another thread |
| 2901 | // might be in the process of launching a new process that will |
| 2902 | // soon replace it, so get a shared pointer to the process so we |
| 2903 | // can keep it alive. |
| 2904 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 2905 | // Now we have a shared pointer to the process that can't go away on us |
| 2906 | // so we now make sure it was the same as the one passed in, and also make |
| 2907 | // sure that our previous "process *" didn't get deleted and have a new |
| 2908 | // "process *" created in its place with the same pointer. To verify this |
| 2909 | // we make sure the process has our debugserver process ID. If we pass all |
| 2910 | // of these tests, then we are sure that this process is the one we were |
| 2911 | // looking for. |
| 2912 | if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2913 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2914 | // Sleep for a half a second to make sure our inferior process has |
| 2915 | // time to set its exit status before we set it incorrectly when |
| 2916 | // both the debugserver and the inferior process shut down. |
| 2917 | usleep (500000); |
| 2918 | // If our process hasn't yet exited, debugserver might have died. |
| 2919 | // If the process did exit, the we are reaping it. |
| 2920 | const StateType state = process->GetState(); |
| 2921 | |
| 2922 | if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID && |
| 2923 | state != eStateInvalid && |
| 2924 | state != eStateUnloaded && |
| 2925 | state != eStateExited && |
| 2926 | state != eStateDetached) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2927 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2928 | char error_str[1024]; |
| 2929 | if (signo) |
| 2930 | { |
| 2931 | const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo); |
| 2932 | if (signal_cstr) |
| 2933 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); |
| 2934 | else |
| 2935 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo); |
| 2936 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2937 | else |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2938 | { |
| 2939 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status); |
| 2940 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2941 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2942 | process->SetExitStatus (-1, error_str); |
| 2943 | } |
| 2944 | // Debugserver has exited we need to let our ProcessGDBRemote |
| 2945 | // know that it no longer has a debugserver instance |
| 2946 | process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2947 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2948 | } |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
| 2952 | void |
| 2953 | ProcessGDBRemote::KillDebugserverProcess () |
| 2954 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2955 | m_gdb_comm.Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2956 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 2957 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2958 | Host::Kill (m_debugserver_pid, SIGINT); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2959 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | void |
| 2964 | ProcessGDBRemote::Initialize() |
| 2965 | { |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 2966 | static std::once_flag g_once_flag; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2967 | |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 2968 | std::call_once(g_once_flag, []() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2969 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2970 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 2971 | GetPluginDescriptionStatic(), |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 2972 | CreateInstance, |
| 2973 | DebuggerInitialize); |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 2974 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 2977 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 2978 | ProcessGDBRemote::DebuggerInitialize (Debugger &debugger) |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 2979 | { |
| 2980 | if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) |
| 2981 | { |
| 2982 | const bool is_global_setting = true; |
| 2983 | PluginManager::CreateSettingForProcessPlugin (debugger, |
| 2984 | GetGlobalPluginProperties()->GetValueProperties(), |
| 2985 | ConstString ("Properties for the gdb-remote process plug-in."), |
| 2986 | is_global_setting); |
| 2987 | } |
| 2988 | } |
| 2989 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2990 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2991 | ProcessGDBRemote::StartAsyncThread () |
| 2992 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2993 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2994 | |
| 2995 | if (log) |
| 2996 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 2997 | |
| 2998 | Mutex::Locker start_locker(m_async_thread_state_mutex); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 2999 | if (!m_async_thread.IsJoinable()) |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3000 | { |
| 3001 | // Create a thread that watches our internal state and controls which |
| 3002 | // events make it to clients (into the DCProcess event queue). |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3003 | |
| 3004 | 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] | 3005 | } |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3006 | else if (log) |
| 3007 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was already running.", __FUNCTION__); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3008 | |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3009 | return m_async_thread.IsJoinable(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | void |
| 3013 | ProcessGDBRemote::StopAsyncThread () |
| 3014 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3015 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3016 | |
| 3017 | if (log) |
| 3018 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 3019 | |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3020 | Mutex::Locker start_locker(m_async_thread_state_mutex); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3021 | if (m_async_thread.IsJoinable()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3022 | { |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3023 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 3024 | |
| 3025 | // This will shut down the async thread. |
| 3026 | m_gdb_comm.Disconnect(); // Disconnect from the debug server. |
| 3027 | |
| 3028 | // Stop the stdio thread |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3029 | m_async_thread.Join(nullptr); |
Pavel Labath | a55a953 | 2015-03-11 09:53:42 +0000 | [diff] [blame] | 3030 | m_async_thread.Reset(); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3031 | } |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3032 | else if (log) |
| 3033 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was not running.", __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 3037 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3038 | ProcessGDBRemote::AsyncThread (void *arg) |
| 3039 | { |
| 3040 | ProcessGDBRemote *process = (ProcessGDBRemote*) arg; |
| 3041 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3042 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3043 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3044 | 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] | 3045 | |
| 3046 | Listener listener ("ProcessGDBRemote::AsyncThread"); |
| 3047 | EventSP event_sp; |
| 3048 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 3049 | eBroadcastBitAsyncThreadShouldExit; |
| 3050 | |
| 3051 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 3052 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3053 | listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit); |
| 3054 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3055 | bool done = false; |
| 3056 | while (!done) |
| 3057 | { |
| 3058 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3059 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3060 | if (listener.WaitForEvent (NULL, event_sp)) |
| 3061 | { |
| 3062 | const uint32_t event_type = event_sp->GetType(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3063 | if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3064 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3065 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3066 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3067 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3068 | switch (event_type) |
| 3069 | { |
| 3070 | case eBroadcastBitAsyncContinue: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3071 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3072 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3073 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3074 | if (continue_packet) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3075 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3076 | const char *continue_cstr = (const char *)continue_packet->GetBytes (); |
| 3077 | const size_t continue_cstr_len = continue_packet->GetByteSize (); |
| 3078 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3079 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3080 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3081 | if (::strstr (continue_cstr, "vAttach") == NULL) |
| 3082 | process->SetPrivateState(eStateRunning); |
| 3083 | StringExtractorGDBRemote response; |
| 3084 | StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3085 | |
Greg Clayton | 0772ded | 2012-05-16 02:48:06 +0000 | [diff] [blame] | 3086 | // We need to immediately clear the thread ID list so we are sure to get a valid list of threads. |
| 3087 | // The thread ID list might be contained within the "response", or the stop reply packet that |
| 3088 | // caused the stop. So clear it now before we give the stop reply packet to the process |
| 3089 | // using the process->SetLastStopPacket()... |
| 3090 | process->ClearThreadIDList (); |
| 3091 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3092 | switch (stop_state) |
| 3093 | { |
| 3094 | case eStateStopped: |
| 3095 | case eStateCrashed: |
| 3096 | case eStateSuspended: |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 3097 | process->SetLastStopPacket (response); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3098 | process->SetPrivateState (stop_state); |
| 3099 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3100 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3101 | case eStateExited: |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3102 | { |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 3103 | process->SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 3104 | process->ClearThreadIDList(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3105 | response.SetFilePos(1); |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3106 | |
| 3107 | int exit_status = response.GetHexU8(); |
| 3108 | const char *desc_cstr = NULL; |
| 3109 | StringExtractor extractor; |
| 3110 | std::string desc_string; |
| 3111 | if (response.GetBytesLeft() > 0 && response.GetChar('-') == ';') |
| 3112 | { |
| 3113 | std::string desc_token; |
| 3114 | while (response.GetNameColonValue (desc_token, desc_string)) |
| 3115 | { |
| 3116 | if (desc_token == "description") |
| 3117 | { |
| 3118 | extractor.GetStringRef().swap(desc_string); |
| 3119 | extractor.SetFilePos(0); |
| 3120 | extractor.GetHexByteString (desc_string); |
| 3121 | desc_cstr = desc_string.c_str(); |
| 3122 | } |
| 3123 | } |
| 3124 | } |
| 3125 | process->SetExitStatus(exit_status, desc_cstr); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3126 | done = true; |
| 3127 | break; |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3128 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3129 | case eStateInvalid: |
| 3130 | process->SetExitStatus(-1, "lost connection"); |
| 3131 | break; |
| 3132 | |
| 3133 | default: |
| 3134 | process->SetPrivateState (stop_state); |
| 3135 | break; |
| 3136 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3137 | } |
| 3138 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3139 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3140 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3141 | case eBroadcastBitAsyncThreadShouldExit: |
| 3142 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3143 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3144 | done = true; |
| 3145 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3146 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3147 | default: |
| 3148 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3149 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3150 | done = true; |
| 3151 | break; |
| 3152 | } |
| 3153 | } |
| 3154 | else if (event_sp->BroadcasterIs (&process->m_gdb_comm)) |
| 3155 | { |
| 3156 | if (event_type & Communication::eBroadcastBitReadThreadDidExit) |
| 3157 | { |
| 3158 | process->SetExitStatus (-1, "lost connection"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3159 | done = true; |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3160 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | else |
| 3164 | { |
| 3165 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3166 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3167 | done = true; |
| 3168 | } |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3173 | 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] | 3174 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3175 | return NULL; |
| 3176 | } |
| 3177 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3178 | //uint32_t |
| 3179 | //ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 3180 | //{ |
| 3181 | // // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver |
| 3182 | // // process and ask it for the list of processes. But if we are local, we can let the Host do it. |
| 3183 | // if (m_local_debugserver) |
| 3184 | // { |
| 3185 | // return Host::ListProcessesMatchingName (name, matches, pids); |
| 3186 | // } |
| 3187 | // else |
| 3188 | // { |
| 3189 | // // FIXME: Implement talking to the remote debugserver. |
| 3190 | // return 0; |
| 3191 | // } |
| 3192 | // |
| 3193 | //} |
| 3194 | // |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3195 | bool |
| 3196 | ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3197 | StoppointCallbackContext *context, |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3198 | lldb::user_id_t break_id, |
| 3199 | lldb::user_id_t break_loc_id) |
| 3200 | { |
| 3201 | // I don't think I have to do anything here, just make sure I notice the new thread when it starts to |
| 3202 | // 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] | 3203 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3204 | if (log) |
| 3205 | log->Printf("Hit New Thread Notification breakpoint."); |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | bool |
| 3211 | ProcessGDBRemote::StartNoticingNewThreads() |
| 3212 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3213 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3214 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3215 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3216 | if (log && log->GetVerbose()) |
| 3217 | log->Printf("Enabled noticing new thread breakpoint."); |
| 3218 | m_thread_create_bp_sp->SetEnabled(true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3219 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3220 | else |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3221 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3222 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 3223 | if (platform_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3224 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3225 | m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target); |
| 3226 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3227 | { |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 3228 | if (log && log->GetVerbose()) |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3229 | log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID()); |
| 3230 | m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3231 | } |
| 3232 | else |
| 3233 | { |
| 3234 | if (log) |
| 3235 | log->Printf("Failed to create new thread notification breakpoint."); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3236 | } |
| 3237 | } |
| 3238 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3239 | return m_thread_create_bp_sp.get() != NULL; |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | bool |
| 3243 | ProcessGDBRemote::StopNoticingNewThreads() |
| 3244 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3245 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 3246 | if (log && log->GetVerbose()) |
Jim Ingham | 0e97cbc | 2011-02-08 05:19:01 +0000 | [diff] [blame] | 3247 | log->Printf ("Disabling new thread notification breakpoint."); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3248 | |
| 3249 | if (m_thread_create_bp_sp) |
| 3250 | m_thread_create_bp_sp->SetEnabled(false); |
| 3251 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3252 | return true; |
| 3253 | } |
| 3254 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3255 | DynamicLoader * |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 3256 | ProcessGDBRemote::GetDynamicLoader () |
| 3257 | { |
| 3258 | if (m_dyld_ap.get() == NULL) |
Jason Molenda | 2e56a25 | 2013-05-11 03:09:05 +0000 | [diff] [blame] | 3259 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 3260 | return m_dyld_ap.get(); |
| 3261 | } |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3262 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3263 | Error |
| 3264 | ProcessGDBRemote::SendEventData(const char *data) |
| 3265 | { |
| 3266 | int return_value; |
| 3267 | bool was_supported; |
| 3268 | |
| 3269 | Error error; |
| 3270 | |
| 3271 | return_value = m_gdb_comm.SendLaunchEventDataPacket (data, &was_supported); |
| 3272 | if (return_value != 0) |
| 3273 | { |
| 3274 | if (!was_supported) |
| 3275 | error.SetErrorString("Sending events is not supported for this process."); |
| 3276 | else |
| 3277 | error.SetErrorStringWithFormat("Error sending event data: %d.", return_value); |
| 3278 | } |
| 3279 | return error; |
| 3280 | } |
| 3281 | |
Steve Pucci | 03904ac | 2014-03-04 23:18:46 +0000 | [diff] [blame] | 3282 | const DataBufferSP |
| 3283 | ProcessGDBRemote::GetAuxvData() |
| 3284 | { |
| 3285 | DataBufferSP buf; |
| 3286 | if (m_gdb_comm.GetQXferAuxvReadSupported()) |
| 3287 | { |
| 3288 | std::string response_string; |
| 3289 | if (m_gdb_comm.SendPacketsAndConcatenateResponses("qXfer:auxv:read::", response_string) == GDBRemoteCommunication::PacketResult::Success) |
| 3290 | buf.reset(new DataBufferHeap(response_string.c_str(), response_string.length())); |
| 3291 | } |
| 3292 | return buf; |
| 3293 | } |
| 3294 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3295 | StructuredData::ObjectSP |
| 3296 | ProcessGDBRemote::GetExtendedInfoForThread (lldb::tid_t tid) |
| 3297 | { |
| 3298 | StructuredData::ObjectSP object_sp; |
| 3299 | |
| 3300 | if (m_gdb_comm.GetThreadExtendedInfoSupported()) |
| 3301 | { |
| 3302 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 3303 | SystemRuntime *runtime = GetSystemRuntime(); |
| 3304 | if (runtime) |
| 3305 | { |
| 3306 | runtime->AddThreadExtendedInfoPacketHints (args_dict); |
| 3307 | } |
| 3308 | args_dict->GetAsDictionary()->AddIntegerItem ("thread", tid); |
| 3309 | |
| 3310 | StreamString packet; |
| 3311 | packet << "jThreadExtendedInfo:"; |
| 3312 | args_dict->Dump (packet); |
| 3313 | |
| 3314 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 3315 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 3316 | // these characters in its packet output -- so we add the quoted version |
| 3317 | // 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] | 3318 | // un-escapes the characters at packet read time. |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3319 | packet << (char) (0x7d ^ 0x20); |
| 3320 | |
| 3321 | StringExtractorGDBRemote response; |
| 3322 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success) |
| 3323 | { |
| 3324 | StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType(); |
| 3325 | if (response_type == StringExtractorGDBRemote::eResponse) |
| 3326 | { |
| 3327 | if (!response.Empty()) |
| 3328 | { |
| 3329 | // The packet has already had the 0x7d xor quoting stripped out at the |
| 3330 | // GDBRemoteCommunication packet receive level. |
| 3331 | object_sp = StructuredData::ParseJSON (response.GetStringRef()); |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | return object_sp; |
| 3337 | } |
| 3338 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3339 | // Establish the largest memory read/write payloads we should use. |
| 3340 | // If the remote stub has a max packet size, stay under that size. |
| 3341 | // |
| 3342 | // If the remote stub's max packet size is crazy large, use a |
| 3343 | // reasonable largeish default. |
| 3344 | // |
| 3345 | // If the remote stub doesn't advertise a max packet size, use a |
| 3346 | // conservative default. |
| 3347 | |
| 3348 | void |
| 3349 | ProcessGDBRemote::GetMaxMemorySize() |
| 3350 | { |
| 3351 | const uint64_t reasonable_largeish_default = 128 * 1024; |
| 3352 | const uint64_t conservative_default = 512; |
| 3353 | |
| 3354 | if (m_max_memory_size == 0) |
| 3355 | { |
| 3356 | uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize(); |
| 3357 | if (stub_max_size != UINT64_MAX && stub_max_size != 0) |
| 3358 | { |
| 3359 | // Save the stub's claimed maximum packet size |
| 3360 | m_remote_stub_max_memory_size = stub_max_size; |
| 3361 | |
| 3362 | // Even if the stub says it can support ginormous packets, |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 3363 | // don't exceed our reasonable largeish default packet size. |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3364 | if (stub_max_size > reasonable_largeish_default) |
| 3365 | { |
| 3366 | stub_max_size = reasonable_largeish_default; |
| 3367 | } |
| 3368 | |
| 3369 | m_max_memory_size = stub_max_size; |
| 3370 | } |
| 3371 | else |
| 3372 | { |
| 3373 | m_max_memory_size = conservative_default; |
| 3374 | } |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | void |
| 3379 | ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max) |
| 3380 | { |
| 3381 | if (user_specified_max != 0) |
| 3382 | { |
| 3383 | GetMaxMemorySize (); |
| 3384 | |
| 3385 | if (m_remote_stub_max_memory_size != 0) |
| 3386 | { |
| 3387 | if (m_remote_stub_max_memory_size < user_specified_max) |
| 3388 | { |
| 3389 | m_max_memory_size = m_remote_stub_max_memory_size; // user specified a packet size too big, go as big |
| 3390 | // as the remote stub says we can go. |
| 3391 | } |
| 3392 | else |
| 3393 | { |
| 3394 | m_max_memory_size = user_specified_max; // user's packet size is good |
| 3395 | } |
| 3396 | } |
| 3397 | else |
| 3398 | { |
| 3399 | m_max_memory_size = user_specified_max; // user's packet size is probably fine |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3404 | bool |
| 3405 | ProcessGDBRemote::GetModuleSpec(const FileSpec& module_file_spec, |
| 3406 | const ArchSpec& arch, |
| 3407 | ModuleSpec &module_spec) |
| 3408 | { |
| 3409 | Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM); |
| 3410 | |
| 3411 | if (!m_gdb_comm.GetModuleInfo (module_file_spec, arch, module_spec)) |
| 3412 | { |
| 3413 | if (log) |
| 3414 | log->Printf ("ProcessGDBRemote::%s - failed to get module info for %s:%s", |
| 3415 | __FUNCTION__, module_file_spec.GetPath ().c_str (), |
| 3416 | arch.GetTriple ().getTriple ().c_str ()); |
| 3417 | return false; |
| 3418 | } |
| 3419 | |
| 3420 | if (log) |
| 3421 | { |
| 3422 | StreamString stream; |
| 3423 | module_spec.Dump (stream); |
| 3424 | log->Printf ("ProcessGDBRemote::%s - got module info for (%s:%s) : %s", |
| 3425 | __FUNCTION__, module_file_spec.GetPath ().c_str (), |
| 3426 | arch.GetTriple ().getTriple ().c_str (), stream.GetString ().c_str ()); |
| 3427 | } |
| 3428 | |
| 3429 | return true; |
| 3430 | } |
| 3431 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3432 | class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3433 | { |
| 3434 | private: |
| 3435 | |
| 3436 | public: |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3437 | CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) : |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3438 | CommandObjectParsed (interpreter, |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3439 | "process plugin packet history", |
| 3440 | "Dumps the packet history buffer. ", |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3441 | NULL) |
| 3442 | { |
| 3443 | } |
| 3444 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3445 | ~CommandObjectProcessGDBRemotePacketHistory () |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3446 | { |
| 3447 | } |
| 3448 | |
| 3449 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 3450 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3451 | { |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3452 | const size_t argc = command.GetArgumentCount(); |
| 3453 | if (argc == 0) |
| 3454 | { |
| 3455 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 3456 | if (process) |
| 3457 | { |
| 3458 | process->GetGDBRemote().DumpHistory(result.GetOutputStream()); |
| 3459 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 3460 | return true; |
| 3461 | } |
| 3462 | } |
| 3463 | else |
| 3464 | { |
| 3465 | result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); |
| 3466 | } |
| 3467 | result.SetStatus (eReturnStatusFailed); |
| 3468 | return false; |
| 3469 | } |
| 3470 | }; |
| 3471 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3472 | class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed |
| 3473 | { |
| 3474 | private: |
| 3475 | |
| 3476 | public: |
| 3477 | CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) : |
| 3478 | CommandObjectParsed (interpreter, |
| 3479 | "process plugin packet xfer-size", |
| 3480 | "Maximum size that lldb will try to read/write one one chunk.", |
| 3481 | NULL) |
| 3482 | { |
| 3483 | } |
| 3484 | |
| 3485 | ~CommandObjectProcessGDBRemotePacketXferSize () |
| 3486 | { |
| 3487 | } |
| 3488 | |
| 3489 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 3490 | DoExecute (Args& command, CommandReturnObject &result) override |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3491 | { |
| 3492 | const size_t argc = command.GetArgumentCount(); |
| 3493 | if (argc == 0) |
| 3494 | { |
| 3495 | result.AppendErrorWithFormat ("'%s' takes an argument to specify the max amount to be transferred when reading/writing", m_cmd_name.c_str()); |
| 3496 | result.SetStatus (eReturnStatusFailed); |
| 3497 | return false; |
| 3498 | } |
| 3499 | |
| 3500 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 3501 | if (process) |
| 3502 | { |
| 3503 | const char *packet_size = command.GetArgumentAtIndex(0); |
| 3504 | errno = 0; |
| 3505 | uint64_t user_specified_max = strtoul (packet_size, NULL, 10); |
| 3506 | if (errno == 0 && user_specified_max != 0) |
| 3507 | { |
| 3508 | process->SetUserSpecifiedMaxMemoryTransferSize (user_specified_max); |
| 3509 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 3510 | return true; |
| 3511 | } |
| 3512 | } |
| 3513 | result.SetStatus (eReturnStatusFailed); |
| 3514 | return false; |
| 3515 | } |
| 3516 | }; |
| 3517 | |
| 3518 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3519 | class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed |
| 3520 | { |
| 3521 | private: |
| 3522 | |
| 3523 | public: |
| 3524 | CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) : |
| 3525 | CommandObjectParsed (interpreter, |
| 3526 | "process plugin packet send", |
| 3527 | "Send a custom packet through the GDB remote protocol and print the answer. " |
| 3528 | "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.", |
| 3529 | NULL) |
| 3530 | { |
| 3531 | } |
| 3532 | |
| 3533 | ~CommandObjectProcessGDBRemotePacketSend () |
| 3534 | { |
| 3535 | } |
| 3536 | |
| 3537 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 3538 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3539 | { |
| 3540 | const size_t argc = command.GetArgumentCount(); |
| 3541 | if (argc == 0) |
| 3542 | { |
| 3543 | result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str()); |
| 3544 | result.SetStatus (eReturnStatusFailed); |
| 3545 | return false; |
| 3546 | } |
| 3547 | |
| 3548 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 3549 | if (process) |
| 3550 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 3551 | for (size_t i=0; i<argc; ++ i) |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3552 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 3553 | const char *packet_cstr = command.GetArgumentAtIndex(0); |
| 3554 | bool send_async = true; |
| 3555 | StringExtractorGDBRemote response; |
| 3556 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 3557 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 3558 | Stream &output_strm = result.GetOutputStream(); |
| 3559 | output_strm.Printf (" packet: %s\n", packet_cstr); |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 3560 | std::string &response_str = response.GetStringRef(); |
| 3561 | |
Han Ming Ong | 399289e | 2013-06-21 19:56:59 +0000 | [diff] [blame] | 3562 | if (strstr(packet_cstr, "qGetProfileData") != NULL) |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 3563 | { |
| 3564 | response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response); |
| 3565 | } |
| 3566 | |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 3567 | if (response_str.empty()) |
| 3568 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 3569 | else |
| 3570 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3571 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3572 | } |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3573 | return true; |
| 3574 | } |
| 3575 | }; |
| 3576 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3577 | class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw |
| 3578 | { |
| 3579 | private: |
| 3580 | |
| 3581 | public: |
| 3582 | CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) : |
| 3583 | CommandObjectRaw (interpreter, |
| 3584 | "process plugin packet monitor", |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 3585 | "Send a qRcmd packet through the GDB remote protocol and print the response." |
| 3586 | "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] | 3587 | NULL) |
| 3588 | { |
| 3589 | } |
| 3590 | |
| 3591 | ~CommandObjectProcessGDBRemotePacketMonitor () |
| 3592 | { |
| 3593 | } |
| 3594 | |
| 3595 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 3596 | DoExecute (const char *command, CommandReturnObject &result) override |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3597 | { |
| 3598 | if (command == NULL || command[0] == '\0') |
| 3599 | { |
| 3600 | result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str()); |
| 3601 | result.SetStatus (eReturnStatusFailed); |
| 3602 | return false; |
| 3603 | } |
| 3604 | |
| 3605 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 3606 | if (process) |
| 3607 | { |
| 3608 | StreamString packet; |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 3609 | packet.PutCString("qRcmd,"); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3610 | packet.PutBytesAsRawHex8(command, strlen(command)); |
| 3611 | const char *packet_cstr = packet.GetString().c_str(); |
| 3612 | |
| 3613 | bool send_async = true; |
| 3614 | StringExtractorGDBRemote response; |
| 3615 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 3616 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 3617 | Stream &output_strm = result.GetOutputStream(); |
| 3618 | output_strm.Printf (" packet: %s\n", packet_cstr); |
| 3619 | const std::string &response_str = response.GetStringRef(); |
| 3620 | |
| 3621 | if (response_str.empty()) |
| 3622 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 3623 | else |
| 3624 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
| 3625 | } |
| 3626 | return true; |
| 3627 | } |
| 3628 | }; |
| 3629 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3630 | class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword |
| 3631 | { |
| 3632 | private: |
| 3633 | |
| 3634 | public: |
| 3635 | CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) : |
| 3636 | CommandObjectMultiword (interpreter, |
| 3637 | "process plugin packet", |
| 3638 | "Commands that deal with GDB remote packets.", |
| 3639 | NULL) |
| 3640 | { |
| 3641 | LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter))); |
| 3642 | LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter))); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3643 | LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter))); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3644 | LoadSubCommand ("xfer-size", CommandObjectSP (new CommandObjectProcessGDBRemotePacketXferSize (interpreter))); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | ~CommandObjectProcessGDBRemotePacket () |
| 3648 | { |
| 3649 | } |
| 3650 | }; |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3651 | |
| 3652 | class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword |
| 3653 | { |
| 3654 | public: |
| 3655 | CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) : |
| 3656 | CommandObjectMultiword (interpreter, |
| 3657 | "process plugin", |
| 3658 | "A set of commands for operating on a ProcessGDBRemote process.", |
| 3659 | "process plugin <subcommand> [<subcommand-options>]") |
| 3660 | { |
| 3661 | LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter))); |
| 3662 | } |
| 3663 | |
| 3664 | ~CommandObjectMultiwordProcessGDBRemote () |
| 3665 | { |
| 3666 | } |
| 3667 | }; |
| 3668 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3669 | CommandObject * |
| 3670 | ProcessGDBRemote::GetPluginCommandObject() |
| 3671 | { |
| 3672 | if (!m_command_sp) |
| 3673 | m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter())); |
| 3674 | return m_command_sp.get(); |
| 3675 | } |