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