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