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