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