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