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