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