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