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