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