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