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