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