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