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