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