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