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