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