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