diff options
author | emaste <emaste@FreeBSD.org> | 2013-08-23 18:06:42 +0000 |
---|---|---|
committer | emaste <emaste@FreeBSD.org> | 2013-08-23 18:06:42 +0000 |
commit | 424d4dadd208e2a1e9a43c3d55f47f03ba0c4509 (patch) | |
tree | 05d762b98a499804ce690e6ce04033f1ddf4dee6 /contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote | |
parent | cde487f27a84e02a560384f75178fddca68740f6 (diff) | |
parent | dcd15f81789e389c1cb27d264fcdddfd0a6002bd (diff) | |
download | FreeBSD-src-424d4dadd208e2a1e9a43c3d55f47f03ba0c4509.zip FreeBSD-src-424d4dadd208e2a1e9a43c3d55f47f03ba0c4509.tar.gz |
Merge lldb r188801 to contrib/llvm/tools/lldb/
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote')
14 files changed, 10218 insertions, 0 deletions
diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp new file mode 100644 index 0000000..d7efdf2 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp @@ -0,0 +1,643 @@ +//===-- GDBRemoteCommunication.cpp ------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "GDBRemoteCommunication.h" + +// C Includes +#include <limits.h> +#include <string.h> + +// C++ Includes +// Other libraries and framework includes +#include "lldb/Core/Log.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Host/FileSpec.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/TimeValue.h" +#include "lldb/Target/Process.h" + +// Project includes +#include "ProcessGDBRemoteLog.h" + +#define DEBUGSERVER_BASENAME "debugserver" + +using namespace lldb; +using namespace lldb_private; + +GDBRemoteCommunication::History::History (uint32_t size) : + m_packets(), + m_curr_idx (0), + m_total_packet_count (0), + m_dumped_to_log (false) +{ + m_packets.resize(size); +} + +GDBRemoteCommunication::History::~History () +{ +} + +void +GDBRemoteCommunication::History::AddPacket (char packet_char, + PacketType type, + uint32_t bytes_transmitted) +{ + const size_t size = m_packets.size(); + if (size > 0) + { + const uint32_t idx = GetNextIndex(); + m_packets[idx].packet.assign (1, packet_char); + m_packets[idx].type = type; + m_packets[idx].bytes_transmitted = bytes_transmitted; + m_packets[idx].packet_idx = m_total_packet_count; + m_packets[idx].tid = Host::GetCurrentThreadID(); + } +} + +void +GDBRemoteCommunication::History::AddPacket (const std::string &src, + uint32_t src_len, + PacketType type, + uint32_t bytes_transmitted) +{ + const size_t size = m_packets.size(); + if (size > 0) + { + const uint32_t idx = GetNextIndex(); + m_packets[idx].packet.assign (src, 0, src_len); + m_packets[idx].type = type; + m_packets[idx].bytes_transmitted = bytes_transmitted; + m_packets[idx].packet_idx = m_total_packet_count; + m_packets[idx].tid = Host::GetCurrentThreadID(); + } +} + +void +GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const +{ + const uint32_t size = GetNumPacketsInHistory (); + const uint32_t first_idx = GetFirstSavedPacketIndex (); + const uint32_t stop_idx = m_curr_idx + size; + for (uint32_t i = first_idx; i < stop_idx; ++i) + { + const uint32_t idx = NormalizeIndex (i); + const Entry &entry = m_packets[idx]; + if (entry.type == ePacketTypeInvalid || entry.packet.empty()) + break; + strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n", + entry.packet_idx, + entry.tid, + entry.bytes_transmitted, + (entry.type == ePacketTypeSend) ? "send" : "read", + entry.packet.c_str()); + } +} + +void +GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const +{ + if (log && !m_dumped_to_log) + { + m_dumped_to_log = true; + const uint32_t size = GetNumPacketsInHistory (); + const uint32_t first_idx = GetFirstSavedPacketIndex (); + const uint32_t stop_idx = m_curr_idx + size; + for (uint32_t i = first_idx; i < stop_idx; ++i) + { + const uint32_t idx = NormalizeIndex (i); + const Entry &entry = m_packets[idx]; + if (entry.type == ePacketTypeInvalid || entry.packet.empty()) + break; + log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s", + entry.packet_idx, + entry.tid, + entry.bytes_transmitted, + (entry.type == ePacketTypeSend) ? "send" : "read", + entry.packet.c_str()); + } + } +} + +//---------------------------------------------------------------------- +// GDBRemoteCommunication constructor +//---------------------------------------------------------------------- +GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name, + const char *listener_name, + bool is_platform) : + Communication(comm_name), + m_packet_timeout (1), + m_sequence_mutex (Mutex::eMutexTypeRecursive), + m_public_is_running (false), + m_private_is_running (false), + m_history (512), + m_send_acks (true), + m_is_platform (is_platform) +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +GDBRemoteCommunication::~GDBRemoteCommunication() +{ + if (IsConnected()) + { + Disconnect(); + } +} + +char +GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length) +{ + int checksum = 0; + + for (size_t i = 0; i < payload_length; ++i) + checksum += payload[i]; + + return checksum & 255; +} + +size_t +GDBRemoteCommunication::SendAck () +{ + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); + ConnectionStatus status = eConnectionStatusSuccess; + char ch = '+'; + const size_t bytes_written = Write (&ch, 1, status, NULL); + if (log) + log->Printf ("<%4zu> send packet: %c", bytes_written, ch); + m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); + return bytes_written; +} + +size_t +GDBRemoteCommunication::SendNack () +{ + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); + ConnectionStatus status = eConnectionStatusSuccess; + char ch = '-'; + const size_t bytes_written = Write (&ch, 1, status, NULL); + if (log) + log->Printf ("<%4zu> send packet: %c", bytes_written, ch); + m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); + return bytes_written; +} + +size_t +GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length) +{ + Mutex::Locker locker(m_sequence_mutex); + return SendPacketNoLock (payload, payload_length); +} + +size_t +GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length) +{ + if (IsConnected()) + { + StreamString packet(0, 4, eByteOrderBig); + + packet.PutChar('$'); + packet.Write (payload, payload_length); + packet.PutChar('#'); + packet.PutHex8(CalculcateChecksum (payload, payload_length)); + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); + ConnectionStatus status = eConnectionStatusSuccess; + size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL); + if (log) + { + // If logging was just enabled and we have history, then dump out what + // we have to the log so we get the historical context. The Dump() call that + // logs all of the packet will set a boolean so that we don't dump this more + // than once + if (!m_history.DidDumpToLog ()) + m_history.Dump (log); + + log->Printf ("<%4zu> send packet: %.*s", bytes_written, (int)packet.GetSize(), packet.GetData()); + } + + m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written); + + + if (bytes_written == packet.GetSize()) + { + if (GetSendAcks ()) + { + if (GetAck () != '+') + { + if (log) + log->Printf("get ack failed..."); + return 0; + } + } + } + else + { + if (log) + log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData()); + } + return bytes_written; + } + return 0; +} + +char +GDBRemoteCommunication::GetAck () +{ + StringExtractorGDBRemote packet; + if (WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ()) == 1) + return packet.GetChar(); + return 0; +} + +bool +GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message) +{ + if (IsRunning()) + return locker.TryLock (m_sequence_mutex, failure_message); + + locker.Lock (m_sequence_mutex); + return true; +} + + +bool +GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) +{ + return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); +} + +size_t +GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec) +{ + uint8_t buffer[8192]; + Error error; + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE)); + + // Check for a packet from our cache first without trying any reading... + if (CheckForPacket (NULL, 0, packet)) + return packet.GetStringRef().size(); + + bool timed_out = false; + while (IsConnected() && !timed_out) + { + lldb::ConnectionStatus status = eConnectionStatusNoConnection; + size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); + + if (log) + log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64, + __PRETTY_FUNCTION__, + timeout_usec, + Communication::ConnectionStatusAsCString (status), + error.AsCString(), + (uint64_t)bytes_read); + + if (bytes_read > 0) + { + if (CheckForPacket (buffer, bytes_read, packet)) + return packet.GetStringRef().size(); + } + else + { + switch (status) + { + case eConnectionStatusTimedOut: + timed_out = true; + break; + case eConnectionStatusSuccess: + //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); + break; + + case eConnectionStatusEndOfFile: + case eConnectionStatusNoConnection: + case eConnectionStatusLostConnection: + case eConnectionStatusError: + Disconnect(); + break; + } + } + } + packet.Clear (); + return 0; +} + +bool +GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet) +{ + // Put the packet data into the buffer in a thread safe fashion + Mutex::Locker locker(m_bytes_mutex); + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); + + if (src && src_len > 0) + { + if (log && log->GetVerbose()) + { + StreamString s; + log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s", + __FUNCTION__, + (uint32_t)src_len, + (uint32_t)src_len, + src); + } + m_bytes.append ((const char *)src, src_len); + } + + // Parse up the packets into gdb remote packets + if (!m_bytes.empty()) + { + // end_idx must be one past the last valid packet byte. Start + // it off with an invalid value that is the same as the current + // index. + size_t content_start = 0; + size_t content_length = 0; + size_t total_length = 0; + size_t checksum_idx = std::string::npos; + + switch (m_bytes[0]) + { + case '+': // Look for ack + case '-': // Look for cancel + case '\x03': // ^C to halt target + content_length = total_length = 1; // The command is one byte long... + break; + + case '$': + // Look for a standard gdb packet? + { + size_t hash_pos = m_bytes.find('#'); + if (hash_pos != std::string::npos) + { + if (hash_pos + 2 < m_bytes.size()) + { + checksum_idx = hash_pos + 1; + // Skip the dollar sign + content_start = 1; + // Don't include the # in the content or the $ in the content length + content_length = hash_pos - 1; + + total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes + } + else + { + // Checksum bytes aren't all here yet + content_length = std::string::npos; + } + } + } + break; + + default: + { + // We have an unexpected byte and we need to flush all bad + // data that is in m_bytes, so we need to find the first + // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt), + // or '$' character (start of packet header) or of course, + // the end of the data in m_bytes... + const size_t bytes_len = m_bytes.size(); + bool done = false; + uint32_t idx; + for (idx = 1; !done && idx < bytes_len; ++idx) + { + switch (m_bytes[idx]) + { + case '+': + case '-': + case '\x03': + case '$': + done = true; + break; + + default: + break; + } + } + if (log) + log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", + __FUNCTION__, idx, idx, m_bytes.c_str()); + m_bytes.erase(0, idx); + } + break; + } + + if (content_length == std::string::npos) + { + packet.Clear(); + return false; + } + else if (total_length > 0) + { + + // We have a valid packet... + assert (content_length <= m_bytes.size()); + assert (total_length <= m_bytes.size()); + assert (content_length <= total_length); + + bool success = true; + std::string &packet_str = packet.GetStringRef(); + + + if (log) + { + // If logging was just enabled and we have history, then dump out what + // we have to the log so we get the historical context. The Dump() call that + // logs all of the packet will set a boolean so that we don't dump this more + // than once + if (!m_history.DidDumpToLog ()) + m_history.Dump (log); + + log->Printf ("<%4zu> read packet: %.*s", total_length, (int)(total_length), m_bytes.c_str()); + } + + m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length); + + packet_str.assign (m_bytes, content_start, content_length); + + if (m_bytes[0] == '$') + { + assert (checksum_idx < m_bytes.size()); + if (::isxdigit (m_bytes[checksum_idx+0]) || + ::isxdigit (m_bytes[checksum_idx+1])) + { + if (GetSendAcks ()) + { + const char *packet_checksum_cstr = &m_bytes[checksum_idx]; + char packet_checksum = strtol (packet_checksum_cstr, NULL, 16); + char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size()); + success = packet_checksum == actual_checksum; + if (!success) + { + if (log) + log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", + (int)(total_length), + m_bytes.c_str(), + (uint8_t)packet_checksum, + (uint8_t)actual_checksum); + } + // Send the ack or nack if needed + if (!success) + SendNack(); + else + SendAck(); + } + } + else + { + success = false; + if (log) + log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str()); + } + } + + m_bytes.erase(0, total_length); + packet.SetFilePos(0); + return success; + } + } + packet.Clear(); + return false; +} + +Error +GDBRemoteCommunication::StartDebugserverProcess (const char *debugserver_url, + const char *unix_socket_name, // For handshaking + lldb_private::ProcessLaunchInfo &launch_info) +{ + Error error; + // If we locate debugserver, keep that located version around + static FileSpec g_debugserver_file_spec; + + // This function will fill in the launch information for the debugserver + // instance that gets launched. + launch_info.Clear(); + + char debugserver_path[PATH_MAX]; + FileSpec &debugserver_file_spec = launch_info.GetExecutableFile(); + + // Always check to see if we have an environment override for the path + // to the debugserver to use and use it if we do. + const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); + if (env_debugserver_path) + debugserver_file_spec.SetFile (env_debugserver_path, false); + else + debugserver_file_spec = g_debugserver_file_spec; + bool debugserver_exists = debugserver_file_spec.Exists(); + if (!debugserver_exists) + { + // The debugserver binary is in the LLDB.framework/Resources + // directory. + if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec)) + { + debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME); + debugserver_exists = debugserver_file_spec.Exists(); + if (debugserver_exists) + { + g_debugserver_file_spec = debugserver_file_spec; + } + else + { + g_debugserver_file_spec.Clear(); + debugserver_file_spec.Clear(); + } + } + } + + if (debugserver_exists) + { + debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); + + Args &debugserver_args = launch_info.GetArguments(); + debugserver_args.Clear(); + char arg_cstr[PATH_MAX]; + + // Start args with "debugserver /file/path -r --" + debugserver_args.AppendArgument(debugserver_path); + debugserver_args.AppendArgument(debugserver_url); + // use native registers, not the GDB registers + debugserver_args.AppendArgument("--native-regs"); + // make debugserver run in its own session so signals generated by + // special terminal key sequences (^C) don't affect debugserver + debugserver_args.AppendArgument("--setsid"); + + if (unix_socket_name && unix_socket_name[0]) + { + debugserver_args.AppendArgument("--unix-socket"); + debugserver_args.AppendArgument(unix_socket_name); + } + + const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); + if (env_debugserver_log_file) + { + ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); + debugserver_args.AppendArgument(arg_cstr); + } + + const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); + if (env_debugserver_log_flags) + { + ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); + debugserver_args.AppendArgument(arg_cstr); + } + // debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt"); + // debugserver_args.AppendArgument("--log-flags=0x802e0e"); + + // We currently send down all arguments, attach pids, or attach + // process names in dedicated GDB server packets, so we don't need + // to pass them as arguments. This is currently because of all the + // things we need to setup prior to launching: the environment, + // current working dir, file actions, etc. +#if 0 + // Now append the program arguments + if (inferior_argv) + { + // Terminate the debugserver args so we can now append the inferior args + debugserver_args.AppendArgument("--"); + + for (int i = 0; inferior_argv[i] != NULL; ++i) + debugserver_args.AppendArgument (inferior_argv[i]); + } + else if (attach_pid != LLDB_INVALID_PROCESS_ID) + { + ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid); + debugserver_args.AppendArgument (arg_cstr); + } + else if (attach_name && attach_name[0]) + { + if (wait_for_launch) + debugserver_args.AppendArgument ("--waitfor"); + else + debugserver_args.AppendArgument ("--attach"); + debugserver_args.AppendArgument (attach_name); + } +#endif + + // Close STDIN, STDOUT and STDERR. We might need to redirect them + // to "/dev/null" if we run into any problems. +// launch_info.AppendCloseFileAction (STDIN_FILENO); +// launch_info.AppendCloseFileAction (STDOUT_FILENO); +// launch_info.AppendCloseFileAction (STDERR_FILENO); + + error = Host::LaunchProcess(launch_info); + } + else + { + error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME ); + } + return error; +} + +void +GDBRemoteCommunication::DumpHistory(Stream &strm) +{ + m_history.Dump (strm); +} diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h new file mode 100644 index 0000000..a107795 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h @@ -0,0 +1,268 @@ +//===-- GDBRemoteCommunication.h --------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_GDBRemoteCommunication_h_ +#define liblldb_GDBRemoteCommunication_h_ + +// C Includes +// C++ Includes +#include <list> +#include <string> + +// Other libraries and framework includes +// Project includes +#include "lldb/lldb-public.h" +#include "lldb/Core/Communication.h" +#include "lldb/Core/Listener.h" +#include "lldb/Host/Mutex.h" +#include "lldb/Host/Predicate.h" +#include "lldb/Host/TimeValue.h" + +#include "Utility/StringExtractorGDBRemote.h" + +class ProcessGDBRemote; + +class GDBRemoteCommunication : public lldb_private::Communication +{ +public: + enum + { + eBroadcastBitRunPacketSent = kLoUserBroadcastBit + }; + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + GDBRemoteCommunication(const char *comm_name, + const char *listener_name, + bool is_platform); + + virtual + ~GDBRemoteCommunication(); + + char + GetAck (); + + size_t + SendAck (); + + size_t + SendNack (); + + char + CalculcateChecksum (const char *payload, + size_t payload_length); + + bool + GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL); + + bool + CheckForPacket (const uint8_t *src, + size_t src_len, + StringExtractorGDBRemote &packet); + bool + IsRunning() const + { + return m_public_is_running.GetValue(); + } + + bool + GetSendAcks () + { + return m_send_acks; + } + + //------------------------------------------------------------------ + // Client and server must implement these pure virtual functions + //------------------------------------------------------------------ + virtual bool + GetThreadSuffixSupported () = 0; + + //------------------------------------------------------------------ + // Set the global packet timeout. + // + // For clients, this is the timeout that gets used when sending + // packets and waiting for responses. For servers, this might not + // get used, and if it doesn't this should be moved to the + // GDBRemoteCommunicationClient. + //------------------------------------------------------------------ + uint32_t + SetPacketTimeout (uint32_t packet_timeout) + { + const uint32_t old_packet_timeout = m_packet_timeout; + m_packet_timeout = packet_timeout; + return old_packet_timeout; + } + + uint32_t + GetPacketTimeoutInMicroSeconds () const + { + return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec; + } + //------------------------------------------------------------------ + // Start a debugserver instance on the current host using the + // supplied connection URL. + //------------------------------------------------------------------ + lldb_private::Error + StartDebugserverProcess (const char *connect_url, + const char *unix_socket_name, + lldb_private::ProcessLaunchInfo &launch_info); + + void + DumpHistory(lldb_private::Stream &strm); + +protected: + + class History + { + public: + enum PacketType + { + ePacketTypeInvalid = 0, + ePacketTypeSend, + ePacketTypeRecv + }; + + struct Entry + { + Entry() : + packet(), + type (ePacketTypeInvalid), + bytes_transmitted (0), + packet_idx (0), + tid (LLDB_INVALID_THREAD_ID) + { + } + + void + Clear () + { + packet.clear(); + type = ePacketTypeInvalid; + bytes_transmitted = 0; + packet_idx = 0; + tid = LLDB_INVALID_THREAD_ID; + } + std::string packet; + PacketType type; + uint32_t bytes_transmitted; + uint32_t packet_idx; + lldb::tid_t tid; + }; + + History (uint32_t size); + + ~History (); + + // For single char packets for ack, nack and /x03 + void + AddPacket (char packet_char, + PacketType type, + uint32_t bytes_transmitted); + void + AddPacket (const std::string &src, + uint32_t src_len, + PacketType type, + uint32_t bytes_transmitted); + + void + Dump (lldb_private::Stream &strm) const; + + void + Dump (lldb_private::Log *log) const; + + bool + DidDumpToLog () const + { + return m_dumped_to_log; + } + +protected: + uint32_t + GetFirstSavedPacketIndex () const + { + if (m_total_packet_count < m_packets.size()) + return 0; + else + return m_curr_idx + 1; + } + + uint32_t + GetNumPacketsInHistory () const + { + if (m_total_packet_count < m_packets.size()) + return m_total_packet_count; + else + return (uint32_t)m_packets.size(); + } + + uint32_t + GetNextIndex() + { + ++m_total_packet_count; + const uint32_t idx = m_curr_idx; + m_curr_idx = NormalizeIndex(idx + 1); + return idx; + } + + uint32_t + NormalizeIndex (uint32_t i) const + { + return i % m_packets.size(); + } + + + std::vector<Entry> m_packets; + uint32_t m_curr_idx; + uint32_t m_total_packet_count; + mutable bool m_dumped_to_log; + }; + + size_t + SendPacket (const char *payload, + size_t payload_length); + + size_t + SendPacketNoLock (const char *payload, + size_t payload_length); + + size_t + WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response, + uint32_t timeout_usec); + + bool + WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); + + //------------------------------------------------------------------ + // Classes that inherit from GDBRemoteCommunication can see and modify these + //------------------------------------------------------------------ + uint32_t m_packet_timeout; +#ifdef LLDB_CONFIGURATION_DEBUG + lldb_private::TrackingMutex m_sequence_mutex; +#else + lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time +#endif + lldb_private::Predicate<bool> m_public_is_running; + lldb_private::Predicate<bool> m_private_is_running; + History m_history; + bool m_send_acks; + bool m_is_platform; // Set to true if this class represents a platform, + // false if this class represents a debug session for + // a single process + + + + +private: + //------------------------------------------------------------------ + // For GDBRemoteCommunication only + //------------------------------------------------------------------ + DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication); +}; + +#endif // liblldb_GDBRemoteCommunication_h_ diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp new file mode 100644 index 0000000..ca594a8 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp @@ -0,0 +1,2348 @@ +//===-- GDBRemoteCommunicationClient.cpp ------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "GDBRemoteCommunicationClient.h" + +// C Includes +// C++ Includes +#include <sstream> + +// Other libraries and framework includes +#include "llvm/ADT/Triple.h" +#include "lldb/Interpreter/Args.h" +#include "lldb/Core/ConnectionFileDescriptor.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/State.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Host/Endian.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/TimeValue.h" + +// Project includes +#include "Utility/StringExtractorGDBRemote.h" +#include "ProcessGDBRemote.h" +#include "ProcessGDBRemoteLog.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// GDBRemoteCommunicationClient constructor +//---------------------------------------------------------------------- +GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) : + GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform), + m_supports_not_sending_acks (eLazyBoolCalculate), + m_supports_thread_suffix (eLazyBoolCalculate), + m_supports_threads_in_stop_reply (eLazyBoolCalculate), + m_supports_vCont_all (eLazyBoolCalculate), + m_supports_vCont_any (eLazyBoolCalculate), + m_supports_vCont_c (eLazyBoolCalculate), + m_supports_vCont_C (eLazyBoolCalculate), + m_supports_vCont_s (eLazyBoolCalculate), + m_supports_vCont_S (eLazyBoolCalculate), + m_qHostInfo_is_valid (eLazyBoolCalculate), + m_qProcessInfo_is_valid (eLazyBoolCalculate), + m_supports_alloc_dealloc_memory (eLazyBoolCalculate), + m_supports_memory_region_info (eLazyBoolCalculate), + m_supports_watchpoint_support_info (eLazyBoolCalculate), + m_supports_detach_stay_stopped (eLazyBoolCalculate), + m_watchpoints_trigger_after_instruction(eLazyBoolCalculate), + m_attach_or_wait_reply(eLazyBoolCalculate), + m_prepare_for_reg_writing_reply (eLazyBoolCalculate), + m_supports_qProcessInfoPID (true), + m_supports_qfProcessInfo (true), + m_supports_qUserName (true), + m_supports_qGroupName (true), + m_supports_qThreadStopInfo (true), + m_supports_z0 (true), + m_supports_z1 (true), + m_supports_z2 (true), + m_supports_z3 (true), + m_supports_z4 (true), + m_curr_tid (LLDB_INVALID_THREAD_ID), + m_curr_tid_run (LLDB_INVALID_THREAD_ID), + m_num_supported_hardware_watchpoints (0), + m_async_mutex (Mutex::eMutexTypeRecursive), + m_async_packet_predicate (false), + m_async_packet (), + m_async_response (), + m_async_signal (-1), + m_thread_id_to_used_usec_map (), + m_host_arch(), + m_process_arch(), + m_os_version_major (UINT32_MAX), + m_os_version_minor (UINT32_MAX), + m_os_version_update (UINT32_MAX) +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() +{ + if (IsConnected()) + Disconnect(); +} + +bool +GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr) +{ + // Start the read thread after we send the handshake ack since if we + // fail to send the handshake ack, there is no reason to continue... + if (SendAck()) + return true; + + if (error_ptr) + error_ptr->SetErrorString("failed to send the handshake ack"); + return false; +} + +void +GDBRemoteCommunicationClient::QueryNoAckModeSupported () +{ + if (m_supports_not_sending_acks == eLazyBoolCalculate) + { + m_send_acks = true; + m_supports_not_sending_acks = eLazyBoolNo; + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false)) + { + if (response.IsOKResponse()) + { + m_send_acks = false; + m_supports_not_sending_acks = eLazyBoolYes; + } + } + } +} + +void +GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported () +{ + if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) + { + m_supports_threads_in_stop_reply = eLazyBoolNo; + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false)) + { + if (response.IsOKResponse()) + m_supports_threads_in_stop_reply = eLazyBoolYes; + } + } +} + +bool +GDBRemoteCommunicationClient::GetVAttachOrWaitSupported () +{ + if (m_attach_or_wait_reply == eLazyBoolCalculate) + { + m_attach_or_wait_reply = eLazyBoolNo; + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false)) + { + if (response.IsOKResponse()) + m_attach_or_wait_reply = eLazyBoolYes; + } + } + if (m_attach_or_wait_reply == eLazyBoolYes) + return true; + else + return false; +} + +bool +GDBRemoteCommunicationClient::GetSyncThreadStateSupported () +{ + if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate) + { + m_prepare_for_reg_writing_reply = eLazyBoolNo; + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false)) + { + if (response.IsOKResponse()) + m_prepare_for_reg_writing_reply = eLazyBoolYes; + } + } + if (m_prepare_for_reg_writing_reply == eLazyBoolYes) + return true; + else + return false; +} + + +void +GDBRemoteCommunicationClient::ResetDiscoverableSettings() +{ + m_supports_not_sending_acks = eLazyBoolCalculate; + m_supports_thread_suffix = eLazyBoolCalculate; + m_supports_threads_in_stop_reply = eLazyBoolCalculate; + m_supports_vCont_c = eLazyBoolCalculate; + m_supports_vCont_C = eLazyBoolCalculate; + m_supports_vCont_s = eLazyBoolCalculate; + m_supports_vCont_S = eLazyBoolCalculate; + m_qHostInfo_is_valid = eLazyBoolCalculate; + m_qProcessInfo_is_valid = eLazyBoolCalculate; + m_supports_alloc_dealloc_memory = eLazyBoolCalculate; + m_supports_memory_region_info = eLazyBoolCalculate; + m_prepare_for_reg_writing_reply = eLazyBoolCalculate; + m_attach_or_wait_reply = eLazyBoolCalculate; + + m_supports_qProcessInfoPID = true; + m_supports_qfProcessInfo = true; + m_supports_qUserName = true; + m_supports_qGroupName = true; + m_supports_qThreadStopInfo = true; + m_supports_z0 = true; + m_supports_z1 = true; + m_supports_z2 = true; + m_supports_z3 = true; + m_supports_z4 = true; + m_host_arch.Clear(); + m_process_arch.Clear(); +} + + +bool +GDBRemoteCommunicationClient::GetThreadSuffixSupported () +{ + if (m_supports_thread_suffix == eLazyBoolCalculate) + { + StringExtractorGDBRemote response; + m_supports_thread_suffix = eLazyBoolNo; + if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false)) + { + if (response.IsOKResponse()) + m_supports_thread_suffix = eLazyBoolYes; + } + } + return m_supports_thread_suffix; +} +bool +GDBRemoteCommunicationClient::GetVContSupported (char flavor) +{ + if (m_supports_vCont_c == eLazyBoolCalculate) + { + StringExtractorGDBRemote response; + m_supports_vCont_any = eLazyBoolNo; + m_supports_vCont_all = eLazyBoolNo; + m_supports_vCont_c = eLazyBoolNo; + m_supports_vCont_C = eLazyBoolNo; + m_supports_vCont_s = eLazyBoolNo; + m_supports_vCont_S = eLazyBoolNo; + if (SendPacketAndWaitForResponse("vCont?", response, false)) + { + const char *response_cstr = response.GetStringRef().c_str(); + if (::strstr (response_cstr, ";c")) + m_supports_vCont_c = eLazyBoolYes; + + if (::strstr (response_cstr, ";C")) + m_supports_vCont_C = eLazyBoolYes; + + if (::strstr (response_cstr, ";s")) + m_supports_vCont_s = eLazyBoolYes; + + if (::strstr (response_cstr, ";S")) + m_supports_vCont_S = eLazyBoolYes; + + if (m_supports_vCont_c == eLazyBoolYes && + m_supports_vCont_C == eLazyBoolYes && + m_supports_vCont_s == eLazyBoolYes && + m_supports_vCont_S == eLazyBoolYes) + { + m_supports_vCont_all = eLazyBoolYes; + } + + if (m_supports_vCont_c == eLazyBoolYes || + m_supports_vCont_C == eLazyBoolYes || + m_supports_vCont_s == eLazyBoolYes || + m_supports_vCont_S == eLazyBoolYes) + { + m_supports_vCont_any = eLazyBoolYes; + } + } + } + + switch (flavor) + { + case 'a': return m_supports_vCont_any; + case 'A': return m_supports_vCont_all; + case 'c': return m_supports_vCont_c; + case 'C': return m_supports_vCont_C; + case 's': return m_supports_vCont_s; + case 'S': return m_supports_vCont_S; + default: break; + } + return false; +} + + +size_t +GDBRemoteCommunicationClient::SendPacketAndWaitForResponse +( + const char *payload, + StringExtractorGDBRemote &response, + bool send_async +) +{ + return SendPacketAndWaitForResponse (payload, + ::strlen (payload), + response, + send_async); +} + +size_t +GDBRemoteCommunicationClient::SendPacketAndWaitForResponse +( + const char *payload, + size_t payload_length, + StringExtractorGDBRemote &response, + bool send_async +) +{ + Mutex::Locker locker; + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + size_t response_len = 0; + if (GetSequenceMutex (locker)) + { + if (SendPacketNoLock (payload, payload_length)) + response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); + else + { + if (log) + log->Printf("error: failed to send '%*s'", (int) payload_length, payload); + } + } + else + { + if (send_async) + { + if (IsRunning()) + { + Mutex::Locker async_locker (m_async_mutex); + m_async_packet.assign(payload, payload_length); + m_async_packet_predicate.SetValue (true, eBroadcastNever); + + if (log) + log->Printf ("async: async packet = %s", m_async_packet.c_str()); + + bool timed_out = false; + if (SendInterrupt(locker, 2, timed_out)) + { + if (m_interrupt_sent) + { + m_interrupt_sent = false; + TimeValue timeout_time; + timeout_time = TimeValue::Now(); + timeout_time.OffsetWithSeconds (m_packet_timeout); + + if (log) + log->Printf ("async: sent interrupt"); + + if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out)) + { + if (log) + log->Printf ("async: got response"); + + // Swap the response buffer to avoid malloc and string copy + response.GetStringRef().swap (m_async_response.GetStringRef()); + response_len = response.GetStringRef().size(); + } + else + { + if (log) + log->Printf ("async: timed out waiting for response"); + } + + // Make sure we wait until the continue packet has been sent again... + if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out)) + { + if (log) + { + if (timed_out) + log->Printf ("async: timed out waiting for process to resume, but process was resumed"); + else + log->Printf ("async: async packet sent"); + } + } + else + { + if (log) + log->Printf ("async: timed out waiting for process to resume"); + } + } + else + { + // We had a racy condition where we went to send the interrupt + // yet we were able to get the lock, so the process must have + // just stopped? + if (log) + log->Printf ("async: got lock without sending interrupt"); + // Send the packet normally since we got the lock + if (SendPacketNoLock (payload, payload_length)) + response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); + else + { + if (log) + log->Printf("error: failed to send '%*s'", (int) payload_length, payload); + } + } + } + else + { + if (log) + log->Printf ("async: failed to interrupt"); + } + } + else + { + if (log) + log->Printf ("async: not running, async is ignored"); + } + } + else + { + if (log) + log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload); + } + } + if (response_len == 0) + { + if (log) + log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload); + } + return response_len; +} + +static const char *end_delimiter = "--end--;"; +static const int end_delimiter_len = 8; + +std::string +GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData +( ProcessGDBRemote *process, + StringExtractorGDBRemote& profileDataExtractor +) +{ + std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map; + std::stringstream final_output; + std::string name, value; + + // Going to assuming thread_used_usec comes first, else bail out. + while (profileDataExtractor.GetNameColonValue(name, value)) + { + if (name.compare("thread_used_id") == 0) + { + StringExtractor threadIDHexExtractor(value.c_str()); + uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0); + + bool has_used_usec = false; + uint32_t curr_used_usec = 0; + std::string usec_name, usec_value; + uint32_t input_file_pos = profileDataExtractor.GetFilePos(); + if (profileDataExtractor.GetNameColonValue(usec_name, usec_value)) + { + if (usec_name.compare("thread_used_usec") == 0) + { + has_used_usec = true; + curr_used_usec = strtoull(usec_value.c_str(), NULL, 0); + } + else + { + // We didn't find what we want, it is probably + // an older version. Bail out. + profileDataExtractor.SetFilePos(input_file_pos); + } + } + + if (has_used_usec) + { + uint32_t prev_used_usec = 0; + std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id); + if (iterator != m_thread_id_to_used_usec_map.end()) + { + prev_used_usec = m_thread_id_to_used_usec_map[thread_id]; + } + + uint32_t real_used_usec = curr_used_usec - prev_used_usec; + // A good first time record is one that runs for at least 0.25 sec + bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000); + bool good_subsequent_time = (prev_used_usec > 0) && + ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id))); + + if (good_first_time || good_subsequent_time) + { + // We try to avoid doing too many index id reservation, + // resulting in fast increase of index ids. + + final_output << name << ":"; + int32_t index_id = process->AssignIndexIDToThread(thread_id); + final_output << index_id << ";"; + + final_output << usec_name << ":" << usec_value << ";"; + } + else + { + // Skip past 'thread_used_name'. + std::string local_name, local_value; + profileDataExtractor.GetNameColonValue(local_name, local_value); + } + + // Store current time as previous time so that they can be compared later. + new_thread_id_to_used_usec_map[thread_id] = curr_used_usec; + } + else + { + // Bail out and use old string. + final_output << name << ":" << value << ";"; + } + } + else + { + final_output << name << ":" << value << ";"; + } + } + final_output << end_delimiter; + m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map; + + return final_output.str(); +} + +StateType +GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse +( + ProcessGDBRemote *process, + const char *payload, + size_t packet_length, + StringExtractorGDBRemote &response +) +{ + m_curr_tid = LLDB_INVALID_THREAD_ID; + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__); + + Mutex::Locker locker(m_sequence_mutex); + StateType state = eStateRunning; + + BroadcastEvent(eBroadcastBitRunPacketSent, NULL); + m_public_is_running.SetValue (true, eBroadcastNever); + // Set the starting continue packet into "continue_packet". This packet + // may change if we are interrupted and we continue after an async packet... + std::string continue_packet(payload, packet_length); + + bool got_async_packet = false; + + while (state == eStateRunning) + { + if (!got_async_packet) + { + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str()); + if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0) + state = eStateInvalid; + + m_private_is_running.SetValue (true, eBroadcastAlways); + } + + got_async_packet = false; + + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str()); + + if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX)) + { + if (response.Empty()) + state = eStateInvalid; + else + { + const char stop_type = response.GetChar(); + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str()); + switch (stop_type) + { + case 'T': + case 'S': + { + if (process->GetStopID() == 0) + { + if (process->GetID() == LLDB_INVALID_PROCESS_ID) + { + lldb::pid_t pid = GetCurrentProcessID (); + if (pid != LLDB_INVALID_PROCESS_ID) + process->SetID (pid); + } + process->BuildDynamicRegisterInfo (true); + } + + // Privately notify any internal threads that we have stopped + // in case we wanted to interrupt our process, yet we might + // send a packet and continue without returning control to the + // user. + m_private_is_running.SetValue (false, eBroadcastAlways); + + const uint8_t signo = response.GetHexU8 (UINT8_MAX); + + bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue(); + if (continue_after_async || m_interrupt_sent) + { + // We sent an interrupt packet to stop the inferior process + // for an async signal or to send an async packet while running + // but we might have been single stepping and received the + // stop packet for the step instead of for the interrupt packet. + // Typically when an interrupt is sent a SIGINT or SIGSTOP + // is used, so if we get anything else, we need to try and + // get another stop reply packet that may have been sent + // due to sending the interrupt when the target is stopped + // which will just re-send a copy of the last stop reply + // packet. If we don't do this, then the reply for our + // async packet will be the repeat stop reply packet and cause + // a lot of trouble for us! + if (signo != SIGINT && signo != SIGSTOP) + { + continue_after_async = false; + + // We didn't get a a SIGINT or SIGSTOP, so try for a + // very brief time (1 ms) to get another stop reply + // packet to make sure it doesn't get in the way + StringExtractorGDBRemote extra_stop_reply_packet; + uint32_t timeout_usec = 1000; + if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec)) + { + switch (extra_stop_reply_packet.GetChar()) + { + case 'T': + case 'S': + // We did get an extra stop reply, which means + // our interrupt didn't stop the target so we + // shouldn't continue after the async signal + // or packet is sent... + continue_after_async = false; + break; + } + } + } + } + + if (m_async_signal != -1) + { + if (log) + log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal)); + + // Save off the async signal we are supposed to send + const int async_signal = m_async_signal; + // Clear the async signal member so we don't end up + // sending the signal multiple times... + m_async_signal = -1; + // Check which signal we stopped with + if (signo == async_signal) + { + if (log) + log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo)); + + // We already stopped with a signal that we wanted + // to stop with, so we are done + } + else + { + // We stopped with a different signal that the one + // we wanted to stop with, so now we must resume + // with the signal we want + char signal_packet[32]; + int signal_packet_len = 0; + signal_packet_len = ::snprintf (signal_packet, + sizeof (signal_packet), + "C%2.2x", + async_signal); + + if (log) + log->Printf ("async: stopped with signal %s, resume with %s", + Host::GetSignalAsCString (signo), + Host::GetSignalAsCString (async_signal)); + + // Set the continue packet to resume even if the + // interrupt didn't cause our stop (ignore continue_after_async) + continue_packet.assign(signal_packet, signal_packet_len); + continue; + } + } + else if (m_async_packet_predicate.GetValue()) + { + Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); + + // We are supposed to send an asynchronous packet while + // we are running. + m_async_response.Clear(); + if (m_async_packet.empty()) + { + if (packet_log) + packet_log->Printf ("async: error: empty async packet"); + + } + else + { + if (packet_log) + packet_log->Printf ("async: sending packet"); + + SendPacketAndWaitForResponse (&m_async_packet[0], + m_async_packet.size(), + m_async_response, + false); + } + // Let the other thread that was trying to send the async + // packet know that the packet has been sent and response is + // ready... + m_async_packet_predicate.SetValue(false, eBroadcastAlways); + + if (packet_log) + packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async); + + // Set the continue packet to resume if our interrupt + // for the async packet did cause the stop + if (continue_after_async) + { + // Reverting this for now as it is causing deadlocks + // in programs (<rdar://problem/11529853>). In the future + // we should check our thread list and "do the right thing" + // for new threads that show up while we stop and run async + // packets. Setting the packet to 'c' to continue all threads + // is the right thing to do 99.99% of the time because if a + // thread was single stepping, and we sent an interrupt, we + // will notice above that we didn't stop due to an interrupt + // but stopped due to stepping and we would _not_ continue. + continue_packet.assign (1, 'c'); + continue; + } + } + // Stop with signal and thread info + state = eStateStopped; + } + break; + + case 'W': + case 'X': + // process exited + state = eStateExited; + break; + + case 'O': + // STDOUT + { + got_async_packet = true; + std::string inferior_stdout; + inferior_stdout.reserve(response.GetBytesLeft () / 2); + char ch; + while ((ch = response.GetHexU8()) != '\0') + inferior_stdout.append(1, ch); + process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size()); + } + break; + + case 'A': + // Async miscellaneous reply. Right now, only profile data is coming through this channel. + { + got_async_packet = true; + std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A' + if (m_partial_profile_data.length() > 0) + { + m_partial_profile_data.append(input); + input = m_partial_profile_data; + m_partial_profile_data.clear(); + } + + size_t found, pos = 0, len = input.length(); + while ((found = input.find(end_delimiter, pos)) != std::string::npos) + { + StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str()); + std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor); + process->BroadcastAsyncProfileData (profile_data); + + pos = found + end_delimiter_len; + } + + if (pos < len) + { + // Last incomplete chunk. + m_partial_profile_data = input.substr(pos); + } + } + break; + + case 'E': + // ERROR + state = eStateInvalid; + break; + + default: + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__); + state = eStateInvalid; + break; + } + } + } + else + { + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__); + state = eStateInvalid; + } + } + if (log) + log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state)); + response.SetFilePos(0); + m_private_is_running.SetValue (false, eBroadcastAlways); + m_public_is_running.SetValue (false, eBroadcastAlways); + return state; +} + +bool +GDBRemoteCommunicationClient::SendAsyncSignal (int signo) +{ + Mutex::Locker async_locker (m_async_mutex); + m_async_signal = signo; + bool timed_out = false; + Mutex::Locker locker; + if (SendInterrupt (locker, 1, timed_out)) + return true; + m_async_signal = -1; + return false; +} + +// This function takes a mutex locker as a parameter in case the GetSequenceMutex +// actually succeeds. If it doesn't succeed in acquiring the sequence mutex +// (the expected result), then it will send the halt packet. If it does succeed +// then the caller that requested the interrupt will want to keep the sequence +// locked down so that no one else can send packets while the caller has control. +// This function usually gets called when we are running and need to stop the +// target. It can also be used when we are running and and we need to do something +// else (like read/write memory), so we need to interrupt the running process +// (gdb remote protocol requires this), and do what we need to do, then resume. + +bool +GDBRemoteCommunicationClient::SendInterrupt +( + Mutex::Locker& locker, + uint32_t seconds_to_wait_for_stop, + bool &timed_out +) +{ + timed_out = false; + Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); + + if (IsRunning()) + { + // Only send an interrupt if our debugserver is running... + if (GetSequenceMutex (locker)) + { + if (log) + log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt"); + } + else + { + // Someone has the mutex locked waiting for a response or for the + // inferior to stop, so send the interrupt on the down low... + char ctrl_c = '\x03'; + ConnectionStatus status = eConnectionStatusSuccess; + size_t bytes_written = Write (&ctrl_c, 1, status, NULL); + if (log) + log->PutCString("send packet: \\x03"); + if (bytes_written > 0) + { + m_interrupt_sent = true; + if (seconds_to_wait_for_stop) + { + TimeValue timeout; + if (seconds_to_wait_for_stop) + { + timeout = TimeValue::Now(); + timeout.OffsetWithSeconds (seconds_to_wait_for_stop); + } + if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out)) + { + if (log) + log->PutCString ("SendInterrupt () - sent interrupt, private state stopped"); + return true; + } + else + { + if (log) + log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume"); + } + } + else + { + if (log) + log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop..."); + return true; + } + } + else + { + if (log) + log->Printf ("SendInterrupt () - failed to write interrupt"); + } + return false; + } + } + else + { + if (log) + log->Printf ("SendInterrupt () - not running"); + } + return true; +} + +lldb::pid_t +GDBRemoteCommunicationClient::GetCurrentProcessID () +{ + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false)) + { + if (response.GetChar() == 'Q') + if (response.GetChar() == 'C') + return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID); + } + return LLDB_INVALID_PROCESS_ID; +} + +bool +GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str) +{ + error_str.clear(); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false)) + { + if (response.IsOKResponse()) + return true; + if (response.GetChar() == 'E') + { + // A string the describes what failed when launching... + error_str = response.GetStringRef().substr(1); + } + else + { + error_str.assign ("unknown error occurred launching process"); + } + } + else + { + error_str.assign ("timed out waiting for app to launch"); + } + return false; +} + +int +GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[]) +{ + if (argv && argv[0]) + { + StreamString packet; + packet.PutChar('A'); + const char *arg; + for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i) + { + const int arg_len = strlen(arg); + if (i > 0) + packet.PutChar(','); + packet.Printf("%i,%i,", arg_len * 2, i); + packet.PutBytesAsRawHex8 (arg, arg_len); + } + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +int +GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value) +{ + if (name_equal_value && name_equal_value[0]) + { + StreamString packet; + packet.Printf("QEnvironment:%s", name_equal_value); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +int +GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch) +{ + if (arch && arch[0]) + { + StreamString packet; + packet.Printf("QLaunchArch:%s", arch); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +bool +GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major, + uint32_t &minor, + uint32_t &update) +{ + if (GetHostInfo ()) + { + if (m_os_version_major != UINT32_MAX) + { + major = m_os_version_major; + minor = m_os_version_minor; + update = m_os_version_update; + return true; + } + } + return false; +} + +bool +GDBRemoteCommunicationClient::GetOSBuildString (std::string &s) +{ + if (GetHostInfo ()) + { + if (!m_os_build.empty()) + { + s = m_os_build; + return true; + } + } + s.clear(); + return false; +} + + +bool +GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s) +{ + if (GetHostInfo ()) + { + if (!m_os_kernel.empty()) + { + s = m_os_kernel; + return true; + } + } + s.clear(); + return false; +} + +bool +GDBRemoteCommunicationClient::GetHostname (std::string &s) +{ + if (GetHostInfo ()) + { + if (!m_hostname.empty()) + { + s = m_hostname; + return true; + } + } + s.clear(); + return false; +} + +ArchSpec +GDBRemoteCommunicationClient::GetSystemArchitecture () +{ + if (GetHostInfo ()) + return m_host_arch; + return ArchSpec(); +} + +const lldb_private::ArchSpec & +GDBRemoteCommunicationClient::GetProcessArchitecture () +{ + if (m_qProcessInfo_is_valid == eLazyBoolCalculate) + GetCurrentProcessInfo (); + return m_process_arch; +} + + +bool +GDBRemoteCommunicationClient::GetHostInfo (bool force) +{ + if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) + { + m_qHostInfo_is_valid = eLazyBoolNo; + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse ("qHostInfo", response, false)) + { + if (response.IsNormalResponse()) + { + std::string name; + std::string value; + uint32_t cpu = LLDB_INVALID_CPUTYPE; + uint32_t sub = 0; + std::string arch_name; + std::string os_name; + std::string vendor_name; + std::string triple; + uint32_t pointer_byte_size = 0; + StringExtractor extractor; + ByteOrder byte_order = eByteOrderInvalid; + uint32_t num_keys_decoded = 0; + while (response.GetNameColonValue(name, value)) + { + if (name.compare("cputype") == 0) + { + // exception type in big endian hex + cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0); + if (cpu != LLDB_INVALID_CPUTYPE) + ++num_keys_decoded; + } + else if (name.compare("cpusubtype") == 0) + { + // exception count in big endian hex + sub = Args::StringToUInt32 (value.c_str(), 0, 0); + if (sub != 0) + ++num_keys_decoded; + } + else if (name.compare("arch") == 0) + { + arch_name.swap (value); + ++num_keys_decoded; + } + else if (name.compare("triple") == 0) + { + // The triple comes as ASCII hex bytes since it contains '-' chars + extractor.GetStringRef().swap(value); + extractor.SetFilePos(0); + extractor.GetHexByteString (triple); + ++num_keys_decoded; + } + else if (name.compare("os_build") == 0) + { + extractor.GetStringRef().swap(value); + extractor.SetFilePos(0); + extractor.GetHexByteString (m_os_build); + ++num_keys_decoded; + } + else if (name.compare("hostname") == 0) + { + extractor.GetStringRef().swap(value); + extractor.SetFilePos(0); + extractor.GetHexByteString (m_hostname); + ++num_keys_decoded; + } + else if (name.compare("os_kernel") == 0) + { + extractor.GetStringRef().swap(value); + extractor.SetFilePos(0); + extractor.GetHexByteString (m_os_kernel); + ++num_keys_decoded; + } + else if (name.compare("ostype") == 0) + { + os_name.swap (value); + ++num_keys_decoded; + } + else if (name.compare("vendor") == 0) + { + vendor_name.swap(value); + ++num_keys_decoded; + } + else if (name.compare("endian") == 0) + { + ++num_keys_decoded; + if (value.compare("little") == 0) + byte_order = eByteOrderLittle; + else if (value.compare("big") == 0) + byte_order = eByteOrderBig; + else if (value.compare("pdp") == 0) + byte_order = eByteOrderPDP; + else + --num_keys_decoded; + } + else if (name.compare("ptrsize") == 0) + { + pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0); + if (pointer_byte_size != 0) + ++num_keys_decoded; + } + else if (name.compare("os_version") == 0) + { + Args::StringToVersion (value.c_str(), + m_os_version_major, + m_os_version_minor, + m_os_version_update); + if (m_os_version_major != UINT32_MAX) + ++num_keys_decoded; + } + else if (name.compare("watchpoint_exceptions_received") == 0) + { + ++num_keys_decoded; + if (strcmp(value.c_str(),"before") == 0) + m_watchpoints_trigger_after_instruction = eLazyBoolNo; + else if (strcmp(value.c_str(),"after") == 0) + m_watchpoints_trigger_after_instruction = eLazyBoolYes; + else + --num_keys_decoded; + } + + } + + if (num_keys_decoded > 0) + m_qHostInfo_is_valid = eLazyBoolYes; + + if (triple.empty()) + { + if (arch_name.empty()) + { + if (cpu != LLDB_INVALID_CPUTYPE) + { + m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub); + if (pointer_byte_size) + { + assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); + } + if (byte_order != eByteOrderInvalid) + { + assert (byte_order == m_host_arch.GetByteOrder()); + } + + if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0) + { + switch (m_host_arch.GetMachine()) + { + case llvm::Triple::arm: + case llvm::Triple::thumb: + os_name = "ios"; + break; + default: + os_name = "macosx"; + break; + } + } + if (!vendor_name.empty()) + m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); + if (!os_name.empty()) + m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); + + } + } + else + { + std::string triple; + triple += arch_name; + if (!vendor_name.empty() || !os_name.empty()) + { + triple += '-'; + if (vendor_name.empty()) + triple += "unknown"; + else + triple += vendor_name; + triple += '-'; + if (os_name.empty()) + triple += "unknown"; + else + triple += os_name; + } + m_host_arch.SetTriple (triple.c_str()); + + llvm::Triple &host_triple = m_host_arch.GetTriple(); + if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin) + { + switch (m_host_arch.GetMachine()) + { + case llvm::Triple::arm: + case llvm::Triple::thumb: + host_triple.setOS(llvm::Triple::IOS); + break; + default: + host_triple.setOS(llvm::Triple::MacOSX); + break; + } + } + if (pointer_byte_size) + { + assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); + } + if (byte_order != eByteOrderInvalid) + { + assert (byte_order == m_host_arch.GetByteOrder()); + } + + } + } + else + { + m_host_arch.SetTriple (triple.c_str()); + if (pointer_byte_size) + { + assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); + } + if (byte_order != eByteOrderInvalid) + { + assert (byte_order == m_host_arch.GetByteOrder()); + } + } + } + } + } + return m_qHostInfo_is_valid == eLazyBoolYes; +} + +int +GDBRemoteCommunicationClient::SendAttach +( + lldb::pid_t pid, + StringExtractorGDBRemote& response +) +{ + if (pid != LLDB_INVALID_PROCESS_ID) + { + char packet[64]; + const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid); + assert (packet_len < (int)sizeof(packet)); + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + if (response.IsErrorResponse()) + return response.GetError(); + return 0; + } + } + return -1; +} + +const lldb_private::ArchSpec & +GDBRemoteCommunicationClient::GetHostArchitecture () +{ + if (m_qHostInfo_is_valid == eLazyBoolCalculate) + GetHostInfo (); + return m_host_arch; +} + +addr_t +GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions) +{ + if (m_supports_alloc_dealloc_memory != eLazyBoolNo) + { + m_supports_alloc_dealloc_memory = eLazyBoolYes; + char packet[64]; + const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", + (uint64_t)size, + permissions & lldb::ePermissionsReadable ? "r" : "", + permissions & lldb::ePermissionsWritable ? "w" : "", + permissions & lldb::ePermissionsExecutable ? "x" : ""); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + if (!response.IsErrorResponse()) + return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); + } + else + { + m_supports_alloc_dealloc_memory = eLazyBoolNo; + } + } + return LLDB_INVALID_ADDRESS; +} + +bool +GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr) +{ + if (m_supports_alloc_dealloc_memory != eLazyBoolNo) + { + m_supports_alloc_dealloc_memory = eLazyBoolYes; + char packet[64]; + const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + if (response.IsOKResponse()) + return true; + } + else + { + m_supports_alloc_dealloc_memory = eLazyBoolNo; + } + } + return false; +} + +Error +GDBRemoteCommunicationClient::Detach (bool keep_stopped) +{ + Error error; + + if (keep_stopped) + { + if (m_supports_detach_stay_stopped == eLazyBoolCalculate) + { + char packet[64]; + const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:"); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + m_supports_detach_stay_stopped = eLazyBoolYes; + } + else + { + m_supports_detach_stay_stopped = eLazyBoolNo; + } + } + + if (m_supports_detach_stay_stopped == eLazyBoolNo) + { + error.SetErrorString("Stays stopped not supported by this target."); + return error; + } + else + { + size_t num_sent = SendPacket ("D1", 2); + if (num_sent == 0) + error.SetErrorString ("Sending extended disconnect packet failed."); + } + } + else + { + size_t num_sent = SendPacket ("D", 1); + if (num_sent == 0) + error.SetErrorString ("Sending disconnect packet failed."); + } + return error; +} + +Error +GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr, + lldb_private::MemoryRegionInfo ®ion_info) +{ + Error error; + region_info.Clear(); + + if (m_supports_memory_region_info != eLazyBoolNo) + { + m_supports_memory_region_info = eLazyBoolYes; + char packet[64]; + const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + std::string name; + std::string value; + addr_t addr_value; + bool success = true; + bool saw_permissions = false; + while (success && response.GetNameColonValue(name, value)) + { + if (name.compare ("start") == 0) + { + addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success); + if (success) + region_info.GetRange().SetRangeBase(addr_value); + } + else if (name.compare ("size") == 0) + { + addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success); + if (success) + region_info.GetRange().SetByteSize (addr_value); + } + else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid()) + { + saw_permissions = true; + if (region_info.GetRange().Contains (addr)) + { + if (value.find('r') != std::string::npos) + region_info.SetReadable (MemoryRegionInfo::eYes); + else + region_info.SetReadable (MemoryRegionInfo::eNo); + + if (value.find('w') != std::string::npos) + region_info.SetWritable (MemoryRegionInfo::eYes); + else + region_info.SetWritable (MemoryRegionInfo::eNo); + + if (value.find('x') != std::string::npos) + region_info.SetExecutable (MemoryRegionInfo::eYes); + else + region_info.SetExecutable (MemoryRegionInfo::eNo); + } + else + { + // The reported region does not contain this address -- we're looking at an unmapped page + region_info.SetReadable (MemoryRegionInfo::eNo); + region_info.SetWritable (MemoryRegionInfo::eNo); + region_info.SetExecutable (MemoryRegionInfo::eNo); + } + } + else if (name.compare ("error") == 0) + { + StringExtractorGDBRemote name_extractor; + // Swap "value" over into "name_extractor" + name_extractor.GetStringRef().swap(value); + // Now convert the HEX bytes into a string value + name_extractor.GetHexByteString (value); + error.SetErrorString(value.c_str()); + } + } + + // We got a valid address range back but no permissions -- which means this is an unmapped page + if (region_info.GetRange().IsValid() && saw_permissions == false) + { + region_info.SetReadable (MemoryRegionInfo::eNo); + region_info.SetWritable (MemoryRegionInfo::eNo); + region_info.SetExecutable (MemoryRegionInfo::eNo); + } + } + else + { + m_supports_memory_region_info = eLazyBoolNo; + } + } + + if (m_supports_memory_region_info == eLazyBoolNo) + { + error.SetErrorString("qMemoryRegionInfo is not supported"); + } + if (error.Fail()) + region_info.Clear(); + return error; + +} + +Error +GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num) +{ + Error error; + + if (m_supports_watchpoint_support_info == eLazyBoolYes) + { + num = m_num_supported_hardware_watchpoints; + return error; + } + + // Set num to 0 first. + num = 0; + if (m_supports_watchpoint_support_info != eLazyBoolNo) + { + char packet[64]; + const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:"); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + m_supports_watchpoint_support_info = eLazyBoolYes; + std::string name; + std::string value; + while (response.GetNameColonValue(name, value)) + { + if (name.compare ("num") == 0) + { + num = Args::StringToUInt32(value.c_str(), 0, 0); + m_num_supported_hardware_watchpoints = num; + } + } + } + else + { + m_supports_watchpoint_support_info = eLazyBoolNo; + } + } + + if (m_supports_watchpoint_support_info == eLazyBoolNo) + { + error.SetErrorString("qWatchpointSupportInfo is not supported"); + } + return error; + +} + +lldb_private::Error +GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after) +{ + Error error(GetWatchpointSupportInfo(num)); + if (error.Success()) + error = GetWatchpointsTriggerAfterInstruction(after); + return error; +} + +lldb_private::Error +GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after) +{ + Error error; + + // we assume watchpoints will happen after running the relevant opcode + // and we only want to override this behavior if we have explicitly + // received a qHostInfo telling us otherwise + if (m_qHostInfo_is_valid != eLazyBoolYes) + after = true; + else + after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo); + return error; +} + +int +GDBRemoteCommunicationClient::SetSTDIN (char const *path) +{ + if (path && path[0]) + { + StreamString packet; + packet.PutCString("QSetSTDIN:"); + packet.PutBytesAsRawHex8(path, strlen(path)); + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +int +GDBRemoteCommunicationClient::SetSTDOUT (char const *path) +{ + if (path && path[0]) + { + StreamString packet; + packet.PutCString("QSetSTDOUT:"); + packet.PutBytesAsRawHex8(path, strlen(path)); + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +int +GDBRemoteCommunicationClient::SetSTDERR (char const *path) +{ + if (path && path[0]) + { + StreamString packet; + packet.PutCString("QSetSTDERR:"); + packet.PutBytesAsRawHex8(path, strlen(path)); + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +int +GDBRemoteCommunicationClient::SetWorkingDir (char const *path) +{ + if (path && path[0]) + { + StreamString packet; + packet.PutCString("QSetWorkingDir:"); + packet.PutBytesAsRawHex8(path, strlen(path)); + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + } + return -1; +} + +int +GDBRemoteCommunicationClient::SetDisableASLR (bool enable) +{ + char packet[32]; + const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + if (response.IsOKResponse()) + return 0; + uint8_t error = response.GetError(); + if (error) + return error; + } + return -1; +} + +bool +GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) +{ + if (response.IsNormalResponse()) + { + std::string name; + std::string value; + StringExtractor extractor; + + while (response.GetNameColonValue(name, value)) + { + if (name.compare("pid") == 0) + { + process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); + } + else if (name.compare("ppid") == 0) + { + process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); + } + else if (name.compare("uid") == 0) + { + process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); + } + else if (name.compare("euid") == 0) + { + process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); + } + else if (name.compare("gid") == 0) + { + process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); + } + else if (name.compare("egid") == 0) + { + process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); + } + else if (name.compare("triple") == 0) + { + // The triple comes as ASCII hex bytes since it contains '-' chars + extractor.GetStringRef().swap(value); + extractor.SetFilePos(0); + extractor.GetHexByteString (value); + process_info.GetArchitecture ().SetTriple (value.c_str()); + } + else if (name.compare("name") == 0) + { + StringExtractor extractor; + // The process name from ASCII hex bytes since we can't + // control the characters in a process name + extractor.GetStringRef().swap(value); + extractor.SetFilePos(0); + extractor.GetHexByteString (value); + process_info.GetExecutableFile().SetFile (value.c_str(), false); + } + } + + if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) + return true; + } + return false; +} + +bool +GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) +{ + process_info.Clear(); + + if (m_supports_qProcessInfoPID) + { + char packet[32]; + const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + return DecodeProcessInfoResponse (response, process_info); + } + else + { + m_supports_qProcessInfoPID = false; + return false; + } + } + return false; +} + +bool +GDBRemoteCommunicationClient::GetCurrentProcessInfo () +{ + if (m_qProcessInfo_is_valid == eLazyBoolYes) + return true; + if (m_qProcessInfo_is_valid == eLazyBoolNo) + return false; + + GetHostInfo (); + + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse ("qProcessInfo", response, false)) + { + if (response.IsNormalResponse()) + { + std::string name; + std::string value; + uint32_t cpu = LLDB_INVALID_CPUTYPE; + uint32_t sub = 0; + std::string arch_name; + std::string os_name; + std::string vendor_name; + std::string triple; + uint32_t pointer_byte_size = 0; + StringExtractor extractor; + ByteOrder byte_order = eByteOrderInvalid; + uint32_t num_keys_decoded = 0; + while (response.GetNameColonValue(name, value)) + { + if (name.compare("cputype") == 0) + { + cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16); + if (cpu != LLDB_INVALID_CPUTYPE) + ++num_keys_decoded; + } + else if (name.compare("cpusubtype") == 0) + { + sub = Args::StringToUInt32 (value.c_str(), 0, 16); + if (sub != 0) + ++num_keys_decoded; + } + else if (name.compare("ostype") == 0) + { + os_name.swap (value); + ++num_keys_decoded; + } + else if (name.compare("vendor") == 0) + { + vendor_name.swap(value); + ++num_keys_decoded; + } + else if (name.compare("endian") == 0) + { + ++num_keys_decoded; + if (value.compare("little") == 0) + byte_order = eByteOrderLittle; + else if (value.compare("big") == 0) + byte_order = eByteOrderBig; + else if (value.compare("pdp") == 0) + byte_order = eByteOrderPDP; + else + --num_keys_decoded; + } + else if (name.compare("ptrsize") == 0) + { + pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16); + if (pointer_byte_size != 0) + ++num_keys_decoded; + } + } + if (num_keys_decoded > 0) + m_qProcessInfo_is_valid = eLazyBoolYes; + if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty()) + { + m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub); + if (pointer_byte_size) + { + assert (pointer_byte_size == m_process_arch.GetAddressByteSize()); + } + m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); + m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); + return true; + } + } + } + else + { + m_qProcessInfo_is_valid = eLazyBoolNo; + } + + return false; +} + + +uint32_t +GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info, + ProcessInstanceInfoList &process_infos) +{ + process_infos.Clear(); + + if (m_supports_qfProcessInfo) + { + StreamString packet; + packet.PutCString ("qfProcessInfo"); + if (!match_info.MatchAllProcesses()) + { + packet.PutChar (':'); + const char *name = match_info.GetProcessInfo().GetName(); + bool has_name_match = false; + if (name && name[0]) + { + has_name_match = true; + NameMatchType name_match_type = match_info.GetNameMatchType(); + switch (name_match_type) + { + case eNameMatchIgnore: + has_name_match = false; + break; + + case eNameMatchEquals: + packet.PutCString ("name_match:equals;"); + break; + + case eNameMatchContains: + packet.PutCString ("name_match:contains;"); + break; + + case eNameMatchStartsWith: + packet.PutCString ("name_match:starts_with;"); + break; + + case eNameMatchEndsWith: + packet.PutCString ("name_match:ends_with;"); + break; + + case eNameMatchRegularExpression: + packet.PutCString ("name_match:regex;"); + break; + } + if (has_name_match) + { + packet.PutCString ("name:"); + packet.PutBytesAsRawHex8(name, ::strlen(name)); + packet.PutChar (';'); + } + } + + if (match_info.GetProcessInfo().ProcessIDIsValid()) + packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID()); + if (match_info.GetProcessInfo().ParentProcessIDIsValid()) + packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID()); + if (match_info.GetProcessInfo().UserIDIsValid()) + packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID()); + if (match_info.GetProcessInfo().GroupIDIsValid()) + packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID()); + if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) + packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID()); + if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) + packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID()); + if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) + packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0); + if (match_info.GetProcessInfo().GetArchitecture().IsValid()) + { + const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture(); + const llvm::Triple &triple = match_arch.GetTriple(); + packet.PutCString("triple:"); + packet.PutCStringAsRawHex8(triple.getTriple().c_str()); + packet.PutChar (';'); + } + } + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) + { + do + { + ProcessInstanceInfo process_info; + if (!DecodeProcessInfoResponse (response, process_info)) + break; + process_infos.Append(process_info); + response.GetStringRef().clear(); + response.SetFilePos(0); + } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false)); + } + else + { + m_supports_qfProcessInfo = false; + return 0; + } + } + return process_infos.GetSize(); + +} + +bool +GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name) +{ + if (m_supports_qUserName) + { + char packet[32]; + const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + if (response.IsNormalResponse()) + { + // Make sure we parsed the right number of characters. The response is + // the hex encoded user name and should make up the entire packet. + // If there are any non-hex ASCII bytes, the length won't match below.. + if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) + return true; + } + } + else + { + m_supports_qUserName = false; + return false; + } + } + return false; + +} + +bool +GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name) +{ + if (m_supports_qGroupName) + { + char packet[32]; + const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) + { + if (response.IsNormalResponse()) + { + // Make sure we parsed the right number of characters. The response is + // the hex encoded group name and should make up the entire packet. + // If there are any non-hex ASCII bytes, the length won't match below.. + if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) + return true; + } + } + else + { + m_supports_qGroupName = false; + return false; + } + } + return false; +} + +void +GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets) +{ + uint32_t i; + TimeValue start_time, end_time; + uint64_t total_time_nsec; + float packets_per_second; + if (SendSpeedTestPacket (0, 0)) + { + for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2) + { + for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2) + { + start_time = TimeValue::Now(); + for (i=0; i<num_packets; ++i) + { + SendSpeedTestPacket (send_size, recv_size); + } + end_time = TimeValue::Now(); + total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); + packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; + printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n", + num_packets, + send_size, + recv_size, + total_time_nsec / TimeValue::NanoSecPerSec, + total_time_nsec % TimeValue::NanoSecPerSec, + packets_per_second); + if (recv_size == 0) + recv_size = 32; + } + if (send_size == 0) + send_size = 32; + } + } + else + { + start_time = TimeValue::Now(); + for (i=0; i<num_packets; ++i) + { + GetCurrentProcessID (); + } + end_time = TimeValue::Now(); + total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); + packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; + printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n", + num_packets, + total_time_nsec / TimeValue::NanoSecPerSec, + total_time_nsec % TimeValue::NanoSecPerSec, + packets_per_second); + } +} + +bool +GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size) +{ + StreamString packet; + packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size); + uint32_t bytes_left = send_size; + while (bytes_left > 0) + { + if (bytes_left >= 26) + { + packet.PutCString("abcdefghijklmnopqrstuvwxyz"); + bytes_left -= 26; + } + else + { + packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz"); + bytes_left = 0; + } + } + + StringExtractorGDBRemote response; + return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0; + return false; +} + +uint16_t +GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort () +{ + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false)) + { + std::string name; + std::string value; + uint16_t port = 0; + //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; + while (response.GetNameColonValue(name, value)) + { + if (name.size() == 4 && name.compare("port") == 0) + port = Args::StringToUInt32(value.c_str(), 0, 0); +// if (name.size() == 3 && name.compare("pid") == 0) +// pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0); + } + return port; + } + return 0; +} + +bool +GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid) +{ + if (m_curr_tid == tid) + return true; + + char packet[32]; + int packet_len; + if (tid == UINT64_MAX) + packet_len = ::snprintf (packet, sizeof(packet), "Hg-1"); + else + packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid); + assert (packet_len + 1 < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) + { + if (response.IsOKResponse()) + { + m_curr_tid = tid; + return true; + } + } + return false; +} + +bool +GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid) +{ + if (m_curr_tid_run == tid) + return true; + + char packet[32]; + int packet_len; + if (tid == UINT64_MAX) + packet_len = ::snprintf (packet, sizeof(packet), "Hc-1"); + else + packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid); + + assert (packet_len + 1 < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) + { + if (response.IsOKResponse()) + { + m_curr_tid_run = tid; + return true; + } + } + return false; +} + +bool +GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response) +{ + if (SendPacketAndWaitForResponse("?", 1, response, false)) + return response.IsNormalResponse(); + return false; +} + +bool +GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response) +{ + if (m_supports_qThreadStopInfo) + { + char packet[256]; + int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid); + assert (packet_len < (int)sizeof(packet)); + if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) + { + if (response.IsNormalResponse()) + return true; + else + return false; + } + else + { + m_supports_qThreadStopInfo = false; + } + } +// if (SetCurrentThread (tid)) +// return GetStopReply (response); + return false; +} + + +uint8_t +GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length) +{ + switch (type) + { + case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break; + case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break; + case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break; + case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break; + case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break; + } + + char packet[64]; + const int packet_len = ::snprintf (packet, + sizeof(packet), + "%c%i,%" PRIx64 ",%x", + insert ? 'Z' : 'z', + type, + addr, + length); + + assert (packet_len + 1 < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse(packet, packet_len, response, true)) + { + if (response.IsOKResponse()) + return 0; + else if (response.IsErrorResponse()) + return response.GetError(); + } + else + { + switch (type) + { + case eBreakpointSoftware: m_supports_z0 = false; break; + case eBreakpointHardware: m_supports_z1 = false; break; + case eWatchpointWrite: m_supports_z2 = false; break; + case eWatchpointRead: m_supports_z3 = false; break; + case eWatchpointReadWrite: m_supports_z4 = false; break; + } + } + + return UINT8_MAX; +} + +size_t +GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, + bool &sequence_mutex_unavailable) +{ + Mutex::Locker locker; + thread_ids.clear(); + + if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex")) + { + sequence_mutex_unavailable = false; + StringExtractorGDBRemote response; + + for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); + response.IsNormalResponse(); + SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ())) + { + char ch = response.GetChar(); + if (ch == 'l') + break; + if (ch == 'm') + { + do + { + tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID); + + if (tid != LLDB_INVALID_THREAD_ID) + { + thread_ids.push_back (tid); + } + ch = response.GetChar(); // Skip the command separator + } while (ch == ','); // Make sure we got a comma separator + } + } + } + else + { +#if defined (LLDB_CONFIGURATION_DEBUG) + // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"); +#else + Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); + if (log) + log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'"); +#endif + sequence_mutex_unavailable = true; + } + return thread_ids.size(); +} + +lldb::addr_t +GDBRemoteCommunicationClient::GetShlibInfoAddr() +{ + if (!IsRunning()) + { + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false)) + { + if (response.IsNormalResponse()) + return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); + } + } + return LLDB_INVALID_ADDRESS; +} + diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h new file mode 100644 index 0000000..5bb8387 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h @@ -0,0 +1,430 @@ +//===-- GDBRemoteCommunicationClient.h --------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_GDBRemoteCommunicationClient_h_ +#define liblldb_GDBRemoteCommunicationClient_h_ + +// C Includes +// C++ Includes +#include <vector> + +// Other libraries and framework includes +// Project includes +#include "lldb/Core/ArchSpec.h" +#include "lldb/Target/Process.h" + +#include "GDBRemoteCommunication.h" + +typedef enum +{ + eBreakpointSoftware = 0, + eBreakpointHardware, + eWatchpointWrite, + eWatchpointRead, + eWatchpointReadWrite +} GDBStoppointType; + +class GDBRemoteCommunicationClient : public GDBRemoteCommunication +{ +public: + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + GDBRemoteCommunicationClient(bool is_platform); + + virtual + ~GDBRemoteCommunicationClient(); + + //------------------------------------------------------------------ + // After connecting, send the handshake to the server to make sure + // we are communicating with it. + //------------------------------------------------------------------ + bool + HandshakeWithServer (lldb_private::Error *error_ptr); + + size_t + SendPacketAndWaitForResponse (const char *send_payload, + StringExtractorGDBRemote &response, + bool send_async); + + size_t + SendPacketAndWaitForResponse (const char *send_payload, + size_t send_length, + StringExtractorGDBRemote &response, + bool send_async); + + lldb::StateType + SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process, + const char *packet_payload, + size_t packet_length, + StringExtractorGDBRemote &response); + + virtual bool + GetThreadSuffixSupported (); + + void + QueryNoAckModeSupported (); + + void + GetListThreadsInStopReplySupported (); + + bool + SendAsyncSignal (int signo); + + bool + SendInterrupt (lldb_private::Mutex::Locker &locker, + uint32_t seconds_to_wait_for_stop, + bool &timed_out); + + lldb::pid_t + GetCurrentProcessID (); + + bool + GetLaunchSuccess (std::string &error_str); + + uint16_t + LaunchGDBserverAndGetPort (); + + //------------------------------------------------------------------ + /// Sends a GDB remote protocol 'A' packet that delivers program + /// arguments to the remote server. + /// + /// @param[in] argv + /// A NULL terminated array of const C strings to use as the + /// arguments. + /// + /// @return + /// Zero if the response was "OK", a positive value if the + /// the response was "Exx" where xx are two hex digits, or + /// -1 if the call is unsupported or any other unexpected + /// response was received. + //------------------------------------------------------------------ + int + SendArgumentsPacket (char const *argv[]); + + //------------------------------------------------------------------ + /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the + /// environment that will get used when launching an application + /// in conjunction with the 'A' packet. This function can be called + /// multiple times in a row in order to pass on the desired + /// environment that the inferior should be launched with. + /// + /// @param[in] name_equal_value + /// A NULL terminated C string that contains a single environment + /// in the format "NAME=VALUE". + /// + /// @return + /// Zero if the response was "OK", a positive value if the + /// the response was "Exx" where xx are two hex digits, or + /// -1 if the call is unsupported or any other unexpected + /// response was received. + //------------------------------------------------------------------ + int + SendEnvironmentPacket (char const *name_equal_value); + + int + SendLaunchArchPacket (const char *arch); + //------------------------------------------------------------------ + /// Sends a "vAttach:PID" where PID is in hex. + /// + /// @param[in] pid + /// A process ID for the remote gdb server to attach to. + /// + /// @param[out] response + /// The response received from the gdb server. If the return + /// value is zero, \a response will contain a stop reply + /// packet. + /// + /// @return + /// Zero if the attach was successful, or an error indicating + /// an error code. + //------------------------------------------------------------------ + int + SendAttach (lldb::pid_t pid, + StringExtractorGDBRemote& response); + + + //------------------------------------------------------------------ + /// Sets the path to use for stdin/out/err for a process + /// that will be launched with the 'A' packet. + /// + /// @param[in] path + /// The path to use for stdin/out/err + /// + /// @return + /// Zero if the for success, or an error code for failure. + //------------------------------------------------------------------ + int + SetSTDIN (char const *path); + int + SetSTDOUT (char const *path); + int + SetSTDERR (char const *path); + + //------------------------------------------------------------------ + /// Sets the disable ASLR flag to \a enable for a process that will + /// be launched with the 'A' packet. + /// + /// @param[in] enable + /// A boolean value indicating wether to disable ASLR or not. + /// + /// @return + /// Zero if the for success, or an error code for failure. + //------------------------------------------------------------------ + int + SetDisableASLR (bool enable); + + //------------------------------------------------------------------ + /// Sets the working directory to \a path for a process that will + /// be launched with the 'A' packet. + /// + /// @param[in] path + /// The path to a directory to use when launching our processs + /// + /// @return + /// Zero if the for success, or an error code for failure. + //------------------------------------------------------------------ + int + SetWorkingDir (char const *path); + + lldb::addr_t + AllocateMemory (size_t size, uint32_t permissions); + + bool + DeallocateMemory (lldb::addr_t addr); + + lldb_private::Error + Detach (bool keep_stopped); + + lldb_private::Error + GetMemoryRegionInfo (lldb::addr_t addr, + lldb_private::MemoryRegionInfo &range_info); + + lldb_private::Error + GetWatchpointSupportInfo (uint32_t &num); + + lldb_private::Error + GetWatchpointSupportInfo (uint32_t &num, bool& after); + + lldb_private::Error + GetWatchpointsTriggerAfterInstruction (bool &after); + + const lldb_private::ArchSpec & + GetHostArchitecture (); + + const lldb_private::ArchSpec & + GetProcessArchitecture (); + + bool + GetVContSupported (char flavor); + + bool + GetVAttachOrWaitSupported (); + + bool + GetSyncThreadStateSupported(); + + void + ResetDiscoverableSettings(); + + bool + GetHostInfo (bool force = false); + + bool + GetOSVersion (uint32_t &major, + uint32_t &minor, + uint32_t &update); + + bool + GetOSBuildString (std::string &s); + + bool + GetOSKernelDescription (std::string &s); + + lldb_private::ArchSpec + GetSystemArchitecture (); + + bool + GetHostname (std::string &s); + + lldb::addr_t + GetShlibInfoAddr(); + + bool + GetSupportsThreadSuffix (); + + bool + GetProcessInfo (lldb::pid_t pid, + lldb_private::ProcessInstanceInfo &process_info); + + uint32_t + FindProcesses (const lldb_private::ProcessInstanceInfoMatch &process_match_info, + lldb_private::ProcessInstanceInfoList &process_infos); + + bool + GetUserName (uint32_t uid, std::string &name); + + bool + GetGroupName (uint32_t gid, std::string &name); + + bool + HasFullVContSupport () + { + return GetVContSupported ('A'); + } + + bool + HasAnyVContSupport () + { + return GetVContSupported ('a'); + } + + bool + GetStopReply (StringExtractorGDBRemote &response); + + bool + GetThreadStopInfo (lldb::tid_t tid, + StringExtractorGDBRemote &response); + + bool + SupportsGDBStoppointPacket (GDBStoppointType type) + { + switch (type) + { + case eBreakpointSoftware: return m_supports_z0; + case eBreakpointHardware: return m_supports_z1; + case eWatchpointWrite: return m_supports_z2; + case eWatchpointRead: return m_supports_z3; + case eWatchpointReadWrite: return m_supports_z4; + } + return false; + } + uint8_t + SendGDBStoppointTypePacket (GDBStoppointType type, // Type of breakpoint or watchpoint + bool insert, // Insert or remove? + lldb::addr_t addr, // Address of breakpoint or watchpoint + uint32_t length); // Byte Size of breakpoint or watchpoint + + void + TestPacketSpeed (const uint32_t num_packets); + + // This packet is for testing the speed of the interface only. Both + // the client and server need to support it, but this allows us to + // measure the packet speed without any other work being done on the + // other end and avoids any of that work affecting the packet send + // and response times. + bool + SendSpeedTestPacket (uint32_t send_size, + uint32_t recv_size); + + bool + SetCurrentThread (uint64_t tid); + + bool + SetCurrentThreadForRun (uint64_t tid); + + lldb_private::LazyBool + SupportsAllocDeallocMemory () // const + { + // Uncomment this to have lldb pretend the debug server doesn't respond to alloc/dealloc memory packets. + // m_supports_alloc_dealloc_memory = lldb_private::eLazyBoolNo; + return m_supports_alloc_dealloc_memory; + } + + size_t + GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, + bool &sequence_mutex_unavailable); + + bool + GetInterruptWasSent () const + { + return m_interrupt_sent; + } + + std::string + HarmonizeThreadIdsForProfileData (ProcessGDBRemote *process, + StringExtractorGDBRemote &inputStringExtractor); + +protected: + + bool + GetCurrentProcessInfo (); + + //------------------------------------------------------------------ + // Classes that inherit from GDBRemoteCommunicationClient can see and modify these + //------------------------------------------------------------------ + lldb_private::LazyBool m_supports_not_sending_acks; + lldb_private::LazyBool m_supports_thread_suffix; + lldb_private::LazyBool m_supports_threads_in_stop_reply; + lldb_private::LazyBool m_supports_vCont_all; + lldb_private::LazyBool m_supports_vCont_any; + lldb_private::LazyBool m_supports_vCont_c; + lldb_private::LazyBool m_supports_vCont_C; + lldb_private::LazyBool m_supports_vCont_s; + lldb_private::LazyBool m_supports_vCont_S; + lldb_private::LazyBool m_qHostInfo_is_valid; + lldb_private::LazyBool m_qProcessInfo_is_valid; + lldb_private::LazyBool m_supports_alloc_dealloc_memory; + lldb_private::LazyBool m_supports_memory_region_info; + lldb_private::LazyBool m_supports_watchpoint_support_info; + lldb_private::LazyBool m_supports_detach_stay_stopped; + lldb_private::LazyBool m_watchpoints_trigger_after_instruction; + lldb_private::LazyBool m_attach_or_wait_reply; + lldb_private::LazyBool m_prepare_for_reg_writing_reply; + + bool + m_supports_qProcessInfoPID:1, + m_supports_qfProcessInfo:1, + m_supports_qUserName:1, + m_supports_qGroupName:1, + m_supports_qThreadStopInfo:1, + m_supports_z0:1, + m_supports_z1:1, + m_supports_z2:1, + m_supports_z3:1, + m_supports_z4:1; + + + lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations + lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc + + + uint32_t m_num_supported_hardware_watchpoints; + + // If we need to send a packet while the target is running, the m_async_XXX + // member variables take care of making this happen. + lldb_private::Mutex m_async_mutex; + lldb_private::Predicate<bool> m_async_packet_predicate; + std::string m_async_packet; + StringExtractorGDBRemote m_async_response; + int m_async_signal; // We were asked to deliver a signal to the inferior process. + bool m_interrupt_sent; + std::string m_partial_profile_data; + std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map; + + lldb_private::ArchSpec m_host_arch; + lldb_private::ArchSpec m_process_arch; + uint32_t m_os_version_major; + uint32_t m_os_version_minor; + uint32_t m_os_version_update; + std::string m_os_build; + std::string m_os_kernel; + std::string m_hostname; + + bool + DecodeProcessInfoResponse (StringExtractorGDBRemote &response, + lldb_private::ProcessInstanceInfo &process_info); +private: + //------------------------------------------------------------------ + // For GDBRemoteCommunicationClient only + //------------------------------------------------------------------ + DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationClient); +}; + +#endif // liblldb_GDBRemoteCommunicationClient_h_ diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp new file mode 100644 index 0000000..3a14e9f --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp @@ -0,0 +1,839 @@ +//===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "GDBRemoteCommunicationServer.h" + +// C Includes +// C++ Includes +// Other libraries and framework includes +#include "llvm/ADT/Triple.h" +#include "lldb/Interpreter/Args.h" +#include "lldb/Core/ConnectionFileDescriptor.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/State.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Host/Endian.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/TimeValue.h" +#include "lldb/Target/Process.h" + +// Project includes +#include "Utility/StringExtractorGDBRemote.h" +#include "ProcessGDBRemote.h" +#include "ProcessGDBRemoteLog.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// GDBRemoteCommunicationServer constructor +//---------------------------------------------------------------------- +GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(bool is_platform) : + GDBRemoteCommunication ("gdb-remote.server", "gdb-remote.server.rx_packet", is_platform), + m_async_thread (LLDB_INVALID_HOST_THREAD), + m_process_launch_info (), + m_process_launch_error (), + m_proc_infos (), + m_proc_infos_index (0), + m_lo_port_num (0), + m_hi_port_num (0) +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() +{ +} + + +//void * +//GDBRemoteCommunicationServer::AsyncThread (void *arg) +//{ +// GDBRemoteCommunicationServer *server = (GDBRemoteCommunicationServer*) arg; +// +// Log *log;// (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); +// if (log) +// log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, arg, process->GetID()); +// +// StringExtractorGDBRemote packet; +// +// while () +// { +// if (packet. +// } +// +// if (log) +// log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, arg, process->GetID()); +// +// process->m_async_thread = LLDB_INVALID_HOST_THREAD; +// return NULL; +//} +// +bool +GDBRemoteCommunicationServer::GetPacketAndSendResponse (uint32_t timeout_usec, + Error &error, + bool &interrupt, + bool &quit) +{ + StringExtractorGDBRemote packet; + if (WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec)) + { + const StringExtractorGDBRemote::ServerPacketType packet_type = packet.GetServerPacketType (); + switch (packet_type) + { + case StringExtractorGDBRemote::eServerPacketType_nack: + case StringExtractorGDBRemote::eServerPacketType_ack: + break; + + case StringExtractorGDBRemote::eServerPacketType_invalid: + error.SetErrorString("invalid packet"); + quit = true; + break; + + case StringExtractorGDBRemote::eServerPacketType_interrupt: + error.SetErrorString("interrupt received"); + interrupt = true; + break; + + case StringExtractorGDBRemote::eServerPacketType_unimplemented: + return SendUnimplementedResponse (packet.GetStringRef().c_str()) > 0; + + case StringExtractorGDBRemote::eServerPacketType_A: + return Handle_A (packet); + + case StringExtractorGDBRemote::eServerPacketType_qfProcessInfo: + return Handle_qfProcessInfo (packet); + + case StringExtractorGDBRemote::eServerPacketType_qsProcessInfo: + return Handle_qsProcessInfo (packet); + + case StringExtractorGDBRemote::eServerPacketType_qC: + return Handle_qC (packet); + + case StringExtractorGDBRemote::eServerPacketType_qHostInfo: + return Handle_qHostInfo (packet); + + case StringExtractorGDBRemote::eServerPacketType_qLaunchGDBServer: + return Handle_qLaunchGDBServer (packet); + + case StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess: + return Handle_qLaunchSuccess (packet); + + case StringExtractorGDBRemote::eServerPacketType_qGroupName: + return Handle_qGroupName (packet); + + case StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID: + return Handle_qProcessInfoPID (packet); + + case StringExtractorGDBRemote::eServerPacketType_qSpeedTest: + return Handle_qSpeedTest (packet); + + case StringExtractorGDBRemote::eServerPacketType_qUserName: + return Handle_qUserName (packet); + + case StringExtractorGDBRemote::eServerPacketType_QEnvironment: + return Handle_QEnvironment (packet); + + case StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR: + return Handle_QSetDisableASLR (packet); + + case StringExtractorGDBRemote::eServerPacketType_QSetSTDIN: + return Handle_QSetSTDIN (packet); + + case StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT: + return Handle_QSetSTDOUT (packet); + + case StringExtractorGDBRemote::eServerPacketType_QSetSTDERR: + return Handle_QSetSTDERR (packet); + + case StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir: + return Handle_QSetWorkingDir (packet); + + case StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode: + return Handle_QStartNoAckMode (packet); + } + return true; + } + else + { + if (!IsConnected()) + error.SetErrorString("lost connection"); + else + error.SetErrorString("timeout"); + } + + return false; +} + +size_t +GDBRemoteCommunicationServer::SendUnimplementedResponse (const char *) +{ + // TODO: Log the packet we aren't handling... + return SendPacketNoLock ("", 0); +} + +size_t +GDBRemoteCommunicationServer::SendErrorResponse (uint8_t err) +{ + char packet[16]; + int packet_len = ::snprintf (packet, sizeof(packet), "E%2.2x", err); + assert (packet_len < (int)sizeof(packet)); + return SendPacketNoLock (packet, packet_len); +} + + +size_t +GDBRemoteCommunicationServer::SendOKResponse () +{ + return SendPacketNoLock ("OK", 2); +} + +bool +GDBRemoteCommunicationServer::HandshakeWithClient(Error *error_ptr) +{ + return GetAck(); +} + +bool +GDBRemoteCommunicationServer::Handle_qHostInfo (StringExtractorGDBRemote &packet) +{ + StreamString response; + + // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00 + + ArchSpec host_arch (Host::GetArchitecture ()); + const llvm::Triple &host_triple = host_arch.GetTriple(); + response.PutCString("triple:"); + response.PutCStringAsRawHex8(host_triple.getTriple().c_str()); + response.Printf (";ptrsize:%u;",host_arch.GetAddressByteSize()); + + uint32_t cpu = host_arch.GetMachOCPUType(); + uint32_t sub = host_arch.GetMachOCPUSubType(); + if (cpu != LLDB_INVALID_CPUTYPE) + response.Printf ("cputype:%u;", cpu); + if (sub != LLDB_INVALID_CPUTYPE) + response.Printf ("cpusubtype:%u;", sub); + + if (cpu == ArchSpec::kCore_arm_any) + response.Printf("watchpoint_exceptions_received:before;"); // On armv7 we use "synchronous" watchpoints which means the exception is delivered before the instruction executes. + else + response.Printf("watchpoint_exceptions_received:after;"); + + switch (lldb::endian::InlHostByteOrder()) + { + case eByteOrderBig: response.PutCString ("endian:big;"); break; + case eByteOrderLittle: response.PutCString ("endian:little;"); break; + case eByteOrderPDP: response.PutCString ("endian:pdp;"); break; + default: response.PutCString ("endian:unknown;"); break; + } + + uint32_t major = UINT32_MAX; + uint32_t minor = UINT32_MAX; + uint32_t update = UINT32_MAX; + if (Host::GetOSVersion (major, minor, update)) + { + if (major != UINT32_MAX) + { + response.Printf("os_version:%u", major); + if (minor != UINT32_MAX) + { + response.Printf(".%u", minor); + if (update != UINT32_MAX) + response.Printf(".%u", update); + } + response.PutChar(';'); + } + } + + std::string s; + if (Host::GetOSBuildString (s)) + { + response.PutCString ("os_build:"); + response.PutCStringAsRawHex8(s.c_str()); + response.PutChar(';'); + } + if (Host::GetOSKernelDescription (s)) + { + response.PutCString ("os_kernel:"); + response.PutCStringAsRawHex8(s.c_str()); + response.PutChar(';'); + } + if (Host::GetHostname (s)) + { + response.PutCString ("hostname:"); + response.PutCStringAsRawHex8(s.c_str()); + response.PutChar(';'); + } + + return SendPacketNoLock (response.GetData(), response.GetSize()) > 0; +} + +static void +CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info, StreamString &response) +{ + response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;", + proc_info.GetProcessID(), + proc_info.GetParentProcessID(), + proc_info.GetUserID(), + proc_info.GetGroupID(), + proc_info.GetEffectiveUserID(), + proc_info.GetEffectiveGroupID()); + response.PutCString ("name:"); + response.PutCStringAsRawHex8(proc_info.GetName()); + response.PutChar(';'); + const ArchSpec &proc_arch = proc_info.GetArchitecture(); + if (proc_arch.IsValid()) + { + const llvm::Triple &proc_triple = proc_arch.GetTriple(); + response.PutCString("triple:"); + response.PutCStringAsRawHex8(proc_triple.getTriple().c_str()); + response.PutChar(';'); + } +} + +bool +GDBRemoteCommunicationServer::Handle_qProcessInfoPID (StringExtractorGDBRemote &packet) +{ + // Packet format: "qProcessInfoPID:%i" where %i is the pid + packet.SetFilePos(::strlen ("qProcessInfoPID:")); + lldb::pid_t pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID); + if (pid != LLDB_INVALID_PROCESS_ID) + { + ProcessInstanceInfo proc_info; + if (Host::GetProcessInfo(pid, proc_info)) + { + StreamString response; + CreateProcessInfoResponse (proc_info, response); + return SendPacketNoLock (response.GetData(), response.GetSize()); + } + } + return SendErrorResponse (1); +} + +bool +GDBRemoteCommunicationServer::Handle_qfProcessInfo (StringExtractorGDBRemote &packet) +{ + m_proc_infos_index = 0; + m_proc_infos.Clear(); + + ProcessInstanceInfoMatch match_info; + packet.SetFilePos(::strlen ("qfProcessInfo")); + if (packet.GetChar() == ':') + { + + std::string key; + std::string value; + while (packet.GetNameColonValue(key, value)) + { + bool success = true; + if (key.compare("name") == 0) + { + StringExtractor extractor; + extractor.GetStringRef().swap(value); + extractor.GetHexByteString (value); + match_info.GetProcessInfo().GetExecutableFile().SetFile(value.c_str(), false); + } + else if (key.compare("name_match") == 0) + { + if (value.compare("equals") == 0) + { + match_info.SetNameMatchType (eNameMatchEquals); + } + else if (value.compare("starts_with") == 0) + { + match_info.SetNameMatchType (eNameMatchStartsWith); + } + else if (value.compare("ends_with") == 0) + { + match_info.SetNameMatchType (eNameMatchEndsWith); + } + else if (value.compare("contains") == 0) + { + match_info.SetNameMatchType (eNameMatchContains); + } + else if (value.compare("regex") == 0) + { + match_info.SetNameMatchType (eNameMatchRegularExpression); + } + else + { + success = false; + } + } + else if (key.compare("pid") == 0) + { + match_info.GetProcessInfo().SetProcessID (Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success)); + } + else if (key.compare("parent_pid") == 0) + { + match_info.GetProcessInfo().SetParentProcessID (Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success)); + } + else if (key.compare("uid") == 0) + { + match_info.GetProcessInfo().SetUserID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); + } + else if (key.compare("gid") == 0) + { + match_info.GetProcessInfo().SetGroupID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); + } + else if (key.compare("euid") == 0) + { + match_info.GetProcessInfo().SetEffectiveUserID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); + } + else if (key.compare("egid") == 0) + { + match_info.GetProcessInfo().SetEffectiveGroupID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); + } + else if (key.compare("all_users") == 0) + { + match_info.SetMatchAllUsers(Args::StringToBoolean(value.c_str(), false, &success)); + } + else if (key.compare("triple") == 0) + { + match_info.GetProcessInfo().GetArchitecture().SetTriple (value.c_str(), NULL); + } + else + { + success = false; + } + + if (!success) + return SendErrorResponse (2); + } + } + + if (Host::FindProcesses (match_info, m_proc_infos)) + { + // We found something, return the first item by calling the get + // subsequent process info packet handler... + return Handle_qsProcessInfo (packet); + } + return SendErrorResponse (3); +} + +bool +GDBRemoteCommunicationServer::Handle_qsProcessInfo (StringExtractorGDBRemote &packet) +{ + if (m_proc_infos_index < m_proc_infos.GetSize()) + { + StreamString response; + CreateProcessInfoResponse (m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response); + ++m_proc_infos_index; + return SendPacketNoLock (response.GetData(), response.GetSize()); + } + return SendErrorResponse (4); +} + +bool +GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet) +{ + // Packet format: "qUserName:%i" where %i is the uid + packet.SetFilePos(::strlen ("qUserName:")); + uint32_t uid = packet.GetU32 (UINT32_MAX); + if (uid != UINT32_MAX) + { + std::string name; + if (Host::GetUserName (uid, name)) + { + StreamString response; + response.PutCStringAsRawHex8 (name.c_str()); + return SendPacketNoLock (response.GetData(), response.GetSize()); + } + } + return SendErrorResponse (5); + +} + +bool +GDBRemoteCommunicationServer::Handle_qGroupName (StringExtractorGDBRemote &packet) +{ + // Packet format: "qGroupName:%i" where %i is the gid + packet.SetFilePos(::strlen ("qGroupName:")); + uint32_t gid = packet.GetU32 (UINT32_MAX); + if (gid != UINT32_MAX) + { + std::string name; + if (Host::GetGroupName (gid, name)) + { + StreamString response; + response.PutCStringAsRawHex8 (name.c_str()); + return SendPacketNoLock (response.GetData(), response.GetSize()); + } + } + return SendErrorResponse (6); +} + +bool +GDBRemoteCommunicationServer::Handle_qSpeedTest (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("qSpeedTest:")); + + std::string key; + std::string value; + bool success = packet.GetNameColonValue(key, value); + if (success && key.compare("response_size") == 0) + { + uint32_t response_size = Args::StringToUInt32(value.c_str(), 0, 0, &success); + if (success) + { + if (response_size == 0) + return SendOKResponse(); + StreamString response; + uint32_t bytes_left = response_size; + response.PutCString("data:"); + while (bytes_left > 0) + { + if (bytes_left >= 26) + { + response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + bytes_left -= 26; + } + else + { + response.Printf ("%*.*s;", bytes_left, bytes_left, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + bytes_left = 0; + } + } + return SendPacketNoLock (response.GetData(), response.GetSize()); + } + } + return SendErrorResponse (7); +} + + +static void * +AcceptPortFromInferior (void *arg) +{ + const char *connect_url = (const char *)arg; + ConnectionFileDescriptor file_conn; + Error error; + if (file_conn.Connect (connect_url, &error) == eConnectionStatusSuccess) + { + char pid_str[256]; + ::memset (pid_str, 0, sizeof(pid_str)); + ConnectionStatus status; + const size_t pid_str_len = file_conn.Read (pid_str, sizeof(pid_str), 0, status, NULL); + if (pid_str_len > 0) + { + int pid = atoi (pid_str); + return (void *)(intptr_t)pid; + } + } + return NULL; +} +// +//static bool +//WaitForProcessToSIGSTOP (const lldb::pid_t pid, const int timeout_in_seconds) +//{ +// const int time_delta_usecs = 100000; +// const int num_retries = timeout_in_seconds/time_delta_usecs; +// for (int i=0; i<num_retries; i++) +// { +// struct proc_bsdinfo bsd_info; +// int error = ::proc_pidinfo (pid, PROC_PIDTBSDINFO, +// (uint64_t) 0, +// &bsd_info, +// PROC_PIDTBSDINFO_SIZE); +// +// switch (error) +// { +// case EINVAL: +// case ENOTSUP: +// case ESRCH: +// case EPERM: +// return false; +// +// default: +// break; +// +// case 0: +// if (bsd_info.pbi_status == SSTOP) +// return true; +// } +// ::usleep (time_delta_usecs); +// } +// return false; +//} + +bool +GDBRemoteCommunicationServer::Handle_A (StringExtractorGDBRemote &packet) +{ + // The 'A' packet is the most over designed packet ever here with + // redundant argument indexes, redundant argument lengths and needed hex + // encoded argument string values. Really all that is needed is a comma + // separated hex encoded argument value list, but we will stay true to the + // documented version of the 'A' packet here... + + packet.SetFilePos(1); // Skip the 'A' + bool success = true; + while (success && packet.GetBytesLeft() > 0) + { + // Decode the decimal argument string length. This length is the + // number of hex nibbles in the argument string value. + const uint32_t arg_len = packet.GetU32(UINT32_MAX); + if (arg_len == UINT32_MAX) + success = false; + else + { + // Make sure the argument hex string length is followed by a comma + if (packet.GetChar() != ',') + success = false; + else + { + // Decode the argument index. We ignore this really becuase + // who would really send down the arguments in a random order??? + const uint32_t arg_idx = packet.GetU32(UINT32_MAX); + if (arg_idx == UINT32_MAX) + success = false; + else + { + // Make sure the argument index is followed by a comma + if (packet.GetChar() != ',') + success = false; + else + { + // Decode the argument string value from hex bytes + // back into a UTF8 string and make sure the length + // matches the one supplied in the packet + std::string arg; + if (packet.GetHexByteString(arg) != (arg_len / 2)) + success = false; + else + { + // If there are any bytes lft + if (packet.GetBytesLeft()) + { + if (packet.GetChar() != ',') + success = false; + } + + if (success) + { + if (arg_idx == 0) + m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false); + m_process_launch_info.GetArguments().AppendArgument(arg.c_str()); + } + } + } + } + } + } + } + + if (success) + { + m_process_launch_info.GetFlags().Set (eLaunchFlagDebug); + m_process_launch_error = Host::LaunchProcess (m_process_launch_info); + if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) + { + return SendOKResponse (); + } + } + return SendErrorResponse (8); +} + +bool +GDBRemoteCommunicationServer::Handle_qC (StringExtractorGDBRemote &packet) +{ + lldb::pid_t pid = m_process_launch_info.GetProcessID(); + StreamString response; + response.Printf("QC%" PRIx64, pid); + if (m_is_platform) + { + // If we launch a process and this GDB server is acting as a platform, + // then we need to clear the process launch state so we can start + // launching another process. In order to launch a process a bunch or + // packets need to be sent: environment packets, working directory, + // disable ASLR, and many more settings. When we launch a process we + // then need to know when to clear this information. Currently we are + // selecting the 'qC' packet as that packet which seems to make the most + // sense. + if (pid != LLDB_INVALID_PROCESS_ID) + { + m_process_launch_info.Clear(); + } + } + return SendPacketNoLock (response.GetData(), response.GetSize()); +} + +bool +GDBRemoteCommunicationServer::Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet) +{ + // Spawn a local debugserver as a platform so we can then attach or launch + // a process... + + if (m_is_platform) + { + // Sleep and wait a bit for debugserver to start to listen... + ConnectionFileDescriptor file_conn; + char connect_url[PATH_MAX]; + Error error; + char unix_socket_name[PATH_MAX] = "/tmp/XXXXXX"; + if (::mktemp (unix_socket_name) == NULL) + { + error.SetErrorString ("failed to make temporary path for a unix socket"); + } + else + { + ::snprintf (connect_url, sizeof(connect_url), "unix-accept://%s", unix_socket_name); + // Spawn a new thread to accept the port that gets bound after + // binding to port 0 (zero). + lldb::thread_t accept_thread = Host::ThreadCreate (unix_socket_name, + AcceptPortFromInferior, + connect_url, + &error); + + if (IS_VALID_LLDB_HOST_THREAD(accept_thread)) + { + // Spawn a debugserver and try to get + ProcessLaunchInfo debugserver_launch_info; + error = StartDebugserverProcess ("localhost:0", + unix_socket_name, + debugserver_launch_info); + + lldb::pid_t debugserver_pid = debugserver_launch_info.GetProcessID(); + if (error.Success()) + { + bool success = false; + + thread_result_t accept_thread_result = NULL; + if (Host::ThreadJoin (accept_thread, &accept_thread_result, &error)) + { + if (accept_thread_result) + { + uint16_t port = (intptr_t)accept_thread_result; + char response[256]; + const int response_len = ::snprintf (response, sizeof(response), "pid:%" PRIu64 ";port:%u;", debugserver_pid, port); + assert (response_len < (int)sizeof(response)); + //m_port_to_pid_map[port] = debugserver_launch_info.GetProcessID(); + success = SendPacketNoLock (response, response_len) > 0; + } + } + ::unlink (unix_socket_name); + + if (!success) + { + if (debugserver_pid != LLDB_INVALID_PROCESS_ID) + ::kill (debugserver_pid, SIGINT); + } + return success; + } + } + } + } + return SendErrorResponse (13); +} + +bool +GDBRemoteCommunicationServer::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet) +{ + if (m_process_launch_error.Success()) + return SendOKResponse(); + StreamString response; + response.PutChar('E'); + response.PutCString(m_process_launch_error.AsCString("<unknown error>")); + return SendPacketNoLock (response.GetData(), response.GetSize()); +} + +bool +GDBRemoteCommunicationServer::Handle_QEnvironment (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("QEnvironment:")); + const uint32_t bytes_left = packet.GetBytesLeft(); + if (bytes_left > 0) + { + m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek()); + return SendOKResponse (); + } + return SendErrorResponse (9); +} + +bool +GDBRemoteCommunicationServer::Handle_QSetDisableASLR (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("QSetDisableASLR:")); + if (packet.GetU32(0)) + m_process_launch_info.GetFlags().Set (eLaunchFlagDisableASLR); + else + m_process_launch_info.GetFlags().Clear (eLaunchFlagDisableASLR); + return SendOKResponse (); +} + +bool +GDBRemoteCommunicationServer::Handle_QSetWorkingDir (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("QSetWorkingDir:")); + std::string path; + packet.GetHexByteString(path); + m_process_launch_info.SwapWorkingDirectory (path); + return SendOKResponse (); +} + +bool +GDBRemoteCommunicationServer::Handle_QSetSTDIN (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("QSetSTDIN:")); + ProcessLaunchInfo::FileAction file_action; + std::string path; + packet.GetHexByteString(path); + const bool read = false; + const bool write = true; + if (file_action.Open(STDIN_FILENO, path.c_str(), read, write)) + { + m_process_launch_info.AppendFileAction(file_action); + return SendOKResponse (); + } + return SendErrorResponse (10); +} + +bool +GDBRemoteCommunicationServer::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("QSetSTDOUT:")); + ProcessLaunchInfo::FileAction file_action; + std::string path; + packet.GetHexByteString(path); + const bool read = true; + const bool write = false; + if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write)) + { + m_process_launch_info.AppendFileAction(file_action); + return SendOKResponse (); + } + return SendErrorResponse (11); +} + +bool +GDBRemoteCommunicationServer::Handle_QSetSTDERR (StringExtractorGDBRemote &packet) +{ + packet.SetFilePos(::strlen ("QSetSTDERR:")); + ProcessLaunchInfo::FileAction file_action; + std::string path; + packet.GetHexByteString(path); + const bool read = true; + const bool write = false; + if (file_action.Open(STDERR_FILENO, path.c_str(), read, write)) + { + m_process_launch_info.AppendFileAction(file_action); + return SendOKResponse (); + } + return SendErrorResponse (12); +} + +bool +GDBRemoteCommunicationServer::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet) +{ + // Send response first before changing m_send_acks to we ack this packet + SendOKResponse (); + m_send_acks = false; + return true; +} diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h new file mode 100644 index 0000000..cce0e4e --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h @@ -0,0 +1,147 @@ +//===-- GDBRemoteCommunicationServer.h --------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_GDBRemoteCommunicationServer_h_ +#define liblldb_GDBRemoteCommunicationServer_h_ + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "lldb/Target/Process.h" + +#include "GDBRemoteCommunication.h" + +class ProcessGDBRemote; +class StringExtractorGDBRemote; + +class GDBRemoteCommunicationServer : public GDBRemoteCommunication +{ +public: + enum + { + eBroadcastBitRunPacketSent = kLoUserBroadcastBit + }; + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + GDBRemoteCommunicationServer(bool is_platform); + + virtual + ~GDBRemoteCommunicationServer(); + + bool + GetPacketAndSendResponse (uint32_t timeout_usec, + lldb_private::Error &error, + bool &interrupt, + bool &quit); + + virtual bool + GetThreadSuffixSupported () + { + return true; + } + + // After connecting, do a little handshake with the client to make sure + // we are at least communicating + bool + HandshakeWithClient (lldb_private::Error *error_ptr); + + // Set both ports to zero to let the platform automatically bind to + // a port chosen by the OS. + void + SetPortRange (uint16_t lo_port_num, uint16_t hi_port_num) + { + m_lo_port_num = lo_port_num; + m_hi_port_num = hi_port_num; + } + +protected: + //typedef std::map<uint16_t, lldb::pid_t> PortToPIDMap; + + lldb::thread_t m_async_thread; + lldb_private::ProcessLaunchInfo m_process_launch_info; + lldb_private::Error m_process_launch_error; + lldb_private::ProcessInstanceInfoList m_proc_infos; + uint32_t m_proc_infos_index; + uint16_t m_lo_port_num; + uint16_t m_hi_port_num; + //PortToPIDMap m_port_to_pid_map; + + size_t + SendUnimplementedResponse (const char *packet); + + size_t + SendErrorResponse (uint8_t error); + + size_t + SendOKResponse (); + + bool + Handle_A (StringExtractorGDBRemote &packet); + + bool + Handle_qLaunchSuccess (StringExtractorGDBRemote &packet); + + bool + Handle_qHostInfo (StringExtractorGDBRemote &packet); + + bool + Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet); + + bool + Handle_qProcessInfoPID (StringExtractorGDBRemote &packet); + + bool + Handle_qfProcessInfo (StringExtractorGDBRemote &packet); + + bool + Handle_qsProcessInfo (StringExtractorGDBRemote &packet); + + bool + Handle_qC (StringExtractorGDBRemote &packet); + + bool + Handle_qUserName (StringExtractorGDBRemote &packet); + + bool + Handle_qGroupName (StringExtractorGDBRemote &packet); + + bool + Handle_qSpeedTest (StringExtractorGDBRemote &packet); + + bool + Handle_QEnvironment (StringExtractorGDBRemote &packet); + + bool + Handle_QSetDisableASLR (StringExtractorGDBRemote &packet); + + bool + Handle_QSetWorkingDir (StringExtractorGDBRemote &packet); + + bool + Handle_QStartNoAckMode (StringExtractorGDBRemote &packet); + + bool + Handle_QSetSTDIN (StringExtractorGDBRemote &packet); + + bool + Handle_QSetSTDOUT (StringExtractorGDBRemote &packet); + + bool + Handle_QSetSTDERR (StringExtractorGDBRemote &packet); + +private: + //------------------------------------------------------------------ + // For GDBRemoteCommunicationServer only + //------------------------------------------------------------------ + DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer); +}; + +#endif // liblldb_GDBRemoteCommunicationServer_h_ diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp new file mode 100644 index 0000000..b1612a5 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp @@ -0,0 +1,971 @@ +//===-- GDBRemoteRegisterContext.cpp ----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "GDBRemoteRegisterContext.h" + +// C Includes +// C++ Includes +// Other libraries and framework includes +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Core/Scalar.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Utility/Utils.h" +// Project includes +#include "Utility/StringExtractorGDBRemote.h" +#include "ProcessGDBRemote.h" +#include "ProcessGDBRemoteLog.h" +#include "ThreadGDBRemote.h" +#include "Utility/ARM_GCC_Registers.h" +#include "Utility/ARM_DWARF_Registers.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// GDBRemoteRegisterContext constructor +//---------------------------------------------------------------------- +GDBRemoteRegisterContext::GDBRemoteRegisterContext +( + ThreadGDBRemote &thread, + uint32_t concrete_frame_idx, + GDBRemoteDynamicRegisterInfo ®_info, + bool read_all_at_once +) : + RegisterContext (thread, concrete_frame_idx), + m_reg_info (reg_info), + m_reg_valid (), + m_reg_data (), + m_read_all_at_once (read_all_at_once) +{ + // Resize our vector of bools to contain one bool for every register. + // We will use these boolean values to know when a register value + // is valid in m_reg_data. + m_reg_valid.resize (reg_info.GetNumRegisters()); + + // Make a heap based buffer that is big enough to store all registers + DataBufferSP reg_data_sp(new DataBufferHeap (reg_info.GetRegisterDataByteSize(), 0)); + m_reg_data.SetData (reg_data_sp); + +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +GDBRemoteRegisterContext::~GDBRemoteRegisterContext() +{ +} + +void +GDBRemoteRegisterContext::InvalidateAllRegisters () +{ + SetAllRegisterValid (false); +} + +void +GDBRemoteRegisterContext::SetAllRegisterValid (bool b) +{ + std::vector<bool>::iterator pos, end = m_reg_valid.end(); + for (pos = m_reg_valid.begin(); pos != end; ++pos) + *pos = b; +} + +size_t +GDBRemoteRegisterContext::GetRegisterCount () +{ + return m_reg_info.GetNumRegisters (); +} + +const RegisterInfo * +GDBRemoteRegisterContext::GetRegisterInfoAtIndex (size_t reg) +{ + return m_reg_info.GetRegisterInfoAtIndex (reg); +} + +size_t +GDBRemoteRegisterContext::GetRegisterSetCount () +{ + return m_reg_info.GetNumRegisterSets (); +} + + + +const RegisterSet * +GDBRemoteRegisterContext::GetRegisterSet (size_t reg_set) +{ + return m_reg_info.GetRegisterSet (reg_set); +} + + + +bool +GDBRemoteRegisterContext::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value) +{ + // Read the register + if (ReadRegisterBytes (reg_info, m_reg_data)) + { + const bool partial_data_ok = false; + Error error (value.SetValueFromData(reg_info, m_reg_data, reg_info->byte_offset, partial_data_ok)); + return error.Success(); + } + return false; +} + +bool +GDBRemoteRegisterContext::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) +{ + const RegisterInfo *reg_info = GetRegisterInfoAtIndex (reg); + if (reg_info == NULL) + return false; + + // Invalidate if needed + InvalidateIfNeeded(false); + + const uint32_t reg_byte_size = reg_info->byte_size; + const size_t bytes_copied = response.GetHexBytes (const_cast<uint8_t*>(m_reg_data.PeekData(reg_info->byte_offset, reg_byte_size)), reg_byte_size, '\xcc'); + bool success = bytes_copied == reg_byte_size; + if (success) + { + SetRegisterIsValid(reg, true); + } + else if (bytes_copied > 0) + { + // Only set register is valid to false if we copied some bytes, else + // leave it as it was. + SetRegisterIsValid(reg, false); + } + return success; +} + +// Helper function for GDBRemoteRegisterContext::ReadRegisterBytes(). +bool +GDBRemoteRegisterContext::GetPrimordialRegister(const lldb_private::RegisterInfo *reg_info, + GDBRemoteCommunicationClient &gdb_comm) +{ + char packet[64]; + StringExtractorGDBRemote response; + int packet_len = 0; + const uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; + if (gdb_comm.GetThreadSuffixSupported()) + packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, m_thread.GetProtocolID()); + else + packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg); + assert (packet_len < ((int)sizeof(packet) - 1)); + if (gdb_comm.SendPacketAndWaitForResponse(packet, response, false)) + return PrivateSetRegisterValue (reg, response); + + return false; +} +bool +GDBRemoteRegisterContext::ReadRegisterBytes (const RegisterInfo *reg_info, DataExtractor &data) +{ + ExecutionContext exe_ctx (CalculateThread()); + + Process *process = exe_ctx.GetProcessPtr(); + Thread *thread = exe_ctx.GetThreadPtr(); + if (process == NULL || thread == NULL) + return false; + + GDBRemoteCommunicationClient &gdb_comm (((ProcessGDBRemote *)process)->GetGDBRemote()); + + InvalidateIfNeeded(false); + + const uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; + + if (!GetRegisterIsValid(reg)) + { + Mutex::Locker locker; + if (gdb_comm.GetSequenceMutex (locker, "Didn't get sequence mutex for read register.")) + { + const bool thread_suffix_supported = gdb_comm.GetThreadSuffixSupported(); + ProcessSP process_sp (m_thread.GetProcess()); + if (thread_suffix_supported || static_cast<ProcessGDBRemote *>(process_sp.get())->GetGDBRemote().SetCurrentThread(m_thread.GetProtocolID())) + { + char packet[64]; + StringExtractorGDBRemote response; + int packet_len = 0; + if (m_read_all_at_once) + { + // Get all registers in one packet + if (thread_suffix_supported) + packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", m_thread.GetProtocolID()); + else + packet_len = ::snprintf (packet, sizeof(packet), "g"); + assert (packet_len < ((int)sizeof(packet) - 1)); + if (gdb_comm.SendPacketAndWaitForResponse(packet, response, false)) + { + if (response.IsNormalResponse()) + if (response.GetHexBytes ((void *)m_reg_data.GetDataStart(), m_reg_data.GetByteSize(), '\xcc') == m_reg_data.GetByteSize()) + SetAllRegisterValid (true); + } + } + else if (reg_info->value_regs) + { + // Process this composite register request by delegating to the constituent + // primordial registers. + + // Index of the primordial register. + bool success = true; + for (uint32_t idx = 0; success; ++idx) + { + const uint32_t prim_reg = reg_info->value_regs[idx]; + if (prim_reg == LLDB_INVALID_REGNUM) + break; + // We have a valid primordial regsiter as our constituent. + // Grab the corresponding register info. + const RegisterInfo *prim_reg_info = GetRegisterInfoAtIndex(prim_reg); + if (prim_reg_info == NULL) + success = false; + else + { + // Read the containing register if it hasn't already been read + if (!GetRegisterIsValid(prim_reg)) + success = GetPrimordialRegister(prim_reg_info, gdb_comm); + } + } + + if (success) + { + // If we reach this point, all primordial register requests have succeeded. + // Validate this composite register. + SetRegisterIsValid (reg_info, true); + } + } + else + { + // Get each register individually + GetPrimordialRegister(reg_info, gdb_comm); + } + } + } + else + { +#if LLDB_CONFIGURATION_DEBUG + StreamString strm; + gdb_comm.DumpHistory(strm); + Host::SetCrashDescription (strm.GetData()); + assert (!"Didn't get sequence mutex for read register."); +#else + Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS)); + if (log) + { + if (log->GetVerbose()) + { + StreamString strm; + gdb_comm.DumpHistory(strm); + log->Printf("error: failed to get packet sequence mutex, not sending read register for \"%s\":\n%s", reg_info->name, strm.GetData()); + } + else + { + log->Printf("error: failed to get packet sequence mutex, not sending read register for \"%s\"", reg_info->name); + } + } +#endif + } + + // Make sure we got a valid register value after reading it + if (!GetRegisterIsValid(reg)) + return false; + } + + if (&data != &m_reg_data) + { + // If we aren't extracting into our own buffer (which + // only happens when this function is called from + // ReadRegisterValue(uint32_t, Scalar&)) then + // we transfer bytes from our buffer into the data + // buffer that was passed in + data.SetByteOrder (m_reg_data.GetByteOrder()); + data.SetData (m_reg_data, reg_info->byte_offset, reg_info->byte_size); + } + return true; +} + +bool +GDBRemoteRegisterContext::WriteRegister (const RegisterInfo *reg_info, + const RegisterValue &value) +{ + DataExtractor data; + if (value.GetData (data)) + return WriteRegisterBytes (reg_info, data, 0); + return false; +} + +// Helper function for GDBRemoteRegisterContext::WriteRegisterBytes(). +bool +GDBRemoteRegisterContext::SetPrimordialRegister(const lldb_private::RegisterInfo *reg_info, + GDBRemoteCommunicationClient &gdb_comm) +{ + StreamString packet; + StringExtractorGDBRemote response; + const uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; + packet.Printf ("P%x=", reg); + packet.PutBytesAsRawHex8 (m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size), + reg_info->byte_size, + lldb::endian::InlHostByteOrder(), + lldb::endian::InlHostByteOrder()); + + if (gdb_comm.GetThreadSuffixSupported()) + packet.Printf (";thread:%4.4" PRIx64 ";", m_thread.GetProtocolID()); + + // Invalidate just this register + SetRegisterIsValid(reg, false); + if (gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), + packet.GetString().size(), + response, + false)) + { + if (response.IsOKResponse()) + return true; + } + return false; +} + +void +GDBRemoteRegisterContext::SyncThreadState(Process *process) +{ + // NB. We assume our caller has locked the sequence mutex. + + GDBRemoteCommunicationClient &gdb_comm (((ProcessGDBRemote *) process)->GetGDBRemote()); + if (!gdb_comm.GetSyncThreadStateSupported()) + return; + + StreamString packet; + StringExtractorGDBRemote response; + packet.Printf ("QSyncThreadState:%4.4" PRIx64 ";", m_thread.GetProtocolID()); + if (gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), + packet.GetString().size(), + response, + false)) + { + if (response.IsOKResponse()) + InvalidateAllRegisters(); + } +} + +bool +GDBRemoteRegisterContext::WriteRegisterBytes (const lldb_private::RegisterInfo *reg_info, DataExtractor &data, uint32_t data_offset) +{ + ExecutionContext exe_ctx (CalculateThread()); + + Process *process = exe_ctx.GetProcessPtr(); + Thread *thread = exe_ctx.GetThreadPtr(); + if (process == NULL || thread == NULL) + return false; + + GDBRemoteCommunicationClient &gdb_comm (((ProcessGDBRemote *)process)->GetGDBRemote()); +// FIXME: This check isn't right because IsRunning checks the Public state, but this +// is work you need to do - for instance in ShouldStop & friends - before the public +// state has been changed. +// if (gdb_comm.IsRunning()) +// return false; + + // Grab a pointer to where we are going to put this register + uint8_t *dst = const_cast<uint8_t*>(m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size)); + + if (dst == NULL) + return false; + + + if (data.CopyByteOrderedData (data_offset, // src offset + reg_info->byte_size, // src length + dst, // dst + reg_info->byte_size, // dst length + m_reg_data.GetByteOrder())) // dst byte order + { + Mutex::Locker locker; + if (gdb_comm.GetSequenceMutex (locker, "Didn't get sequence mutex for write register.")) + { + const bool thread_suffix_supported = gdb_comm.GetThreadSuffixSupported(); + ProcessSP process_sp (m_thread.GetProcess()); + if (thread_suffix_supported || static_cast<ProcessGDBRemote *>(process_sp.get())->GetGDBRemote().SetCurrentThread(m_thread.GetProtocolID())) + { + StreamString packet; + StringExtractorGDBRemote response; + + if (m_read_all_at_once) + { + // Set all registers in one packet + packet.PutChar ('G'); + packet.PutBytesAsRawHex8 (m_reg_data.GetDataStart(), + m_reg_data.GetByteSize(), + lldb::endian::InlHostByteOrder(), + lldb::endian::InlHostByteOrder()); + + if (thread_suffix_supported) + packet.Printf (";thread:%4.4" PRIx64 ";", m_thread.GetProtocolID()); + + // Invalidate all register values + InvalidateIfNeeded (true); + + if (gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), + packet.GetString().size(), + response, + false)) + { + SetAllRegisterValid (false); + if (response.IsOKResponse()) + { + return true; + } + } + } + else + { + bool success = true; + + if (reg_info->value_regs) + { + // This register is part of another register. In this case we read the actual + // register data for any "value_regs", and once all that data is read, we will + // have enough data in our register context bytes for the value of this register + + // Invalidate this composite register first. + + for (uint32_t idx = 0; success; ++idx) + { + const uint32_t reg = reg_info->value_regs[idx]; + if (reg == LLDB_INVALID_REGNUM) + break; + // We have a valid primordial regsiter as our constituent. + // Grab the corresponding register info. + const RegisterInfo *value_reg_info = GetRegisterInfoAtIndex(reg); + if (value_reg_info == NULL) + success = false; + else + success = SetPrimordialRegister(value_reg_info, gdb_comm); + } + } + else + { + // This is an actual register, write it + success = SetPrimordialRegister(reg_info, gdb_comm); + } + + // Check if writing this register will invalidate any other register values? + // If so, invalidate them + if (reg_info->invalidate_regs) + { + for (uint32_t idx = 0, reg = reg_info->invalidate_regs[0]; + reg != LLDB_INVALID_REGNUM; + reg = reg_info->invalidate_regs[++idx]) + { + SetRegisterIsValid(reg, false); + } + } + + return success; + } + } + } + else + { + Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS)); + if (log) + { + if (log->GetVerbose()) + { + StreamString strm; + gdb_comm.DumpHistory(strm); + log->Printf("error: failed to get packet sequence mutex, not sending write register for \"%s\":\n%s", reg_info->name, strm.GetData()); + } + else + log->Printf("error: failed to get packet sequence mutex, not sending write register for \"%s\"", reg_info->name); + } + } + } + return false; +} + + +bool +GDBRemoteRegisterContext::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) +{ + ExecutionContext exe_ctx (CalculateThread()); + + Process *process = exe_ctx.GetProcessPtr(); + Thread *thread = exe_ctx.GetThreadPtr(); + if (process == NULL || thread == NULL) + return false; + + GDBRemoteCommunicationClient &gdb_comm (((ProcessGDBRemote *)process)->GetGDBRemote()); + + StringExtractorGDBRemote response; + + Mutex::Locker locker; + if (gdb_comm.GetSequenceMutex (locker, "Didn't get sequence mutex for read all registers.")) + { + SyncThreadState(process); + + char packet[32]; + const bool thread_suffix_supported = gdb_comm.GetThreadSuffixSupported(); + ProcessSP process_sp (m_thread.GetProcess()); + if (thread_suffix_supported || static_cast<ProcessGDBRemote *>(process_sp.get())->GetGDBRemote().SetCurrentThread(m_thread.GetProtocolID())) + { + int packet_len = 0; + if (thread_suffix_supported) + packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64, m_thread.GetProtocolID()); + else + packet_len = ::snprintf (packet, sizeof(packet), "g"); + assert (packet_len < ((int)sizeof(packet) - 1)); + + if (gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false)) + { + if (response.IsErrorResponse()) + return false; + + std::string &response_str = response.GetStringRef(); + if (isxdigit(response_str[0])) + { + response_str.insert(0, 1, 'G'); + if (thread_suffix_supported) + { + char thread_id_cstr[64]; + ::snprintf (thread_id_cstr, sizeof(thread_id_cstr), ";thread:%4.4" PRIx64 ";", m_thread.GetProtocolID()); + response_str.append (thread_id_cstr); + } + data_sp.reset (new DataBufferHeap (response_str.c_str(), response_str.size())); + return true; + } + } + } + } + else + { + Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS)); + if (log) + { + if (log->GetVerbose()) + { + StreamString strm; + gdb_comm.DumpHistory(strm); + log->Printf("error: failed to get packet sequence mutex, not sending read all registers:\n%s", strm.GetData()); + } + else + log->Printf("error: failed to get packet sequence mutex, not sending read all registers"); + } + } + + data_sp.reset(); + return false; +} + +bool +GDBRemoteRegisterContext::WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) +{ + if (!data_sp || data_sp->GetBytes() == NULL || data_sp->GetByteSize() == 0) + return false; + + ExecutionContext exe_ctx (CalculateThread()); + + Process *process = exe_ctx.GetProcessPtr(); + Thread *thread = exe_ctx.GetThreadPtr(); + if (process == NULL || thread == NULL) + return false; + + GDBRemoteCommunicationClient &gdb_comm (((ProcessGDBRemote *)process)->GetGDBRemote()); + + StringExtractorGDBRemote response; + Mutex::Locker locker; + if (gdb_comm.GetSequenceMutex (locker, "Didn't get sequence mutex for write all registers.")) + { + const bool thread_suffix_supported = gdb_comm.GetThreadSuffixSupported(); + ProcessSP process_sp (m_thread.GetProcess()); + if (thread_suffix_supported || static_cast<ProcessGDBRemote *>(process_sp.get())->GetGDBRemote().SetCurrentThread(m_thread.GetProtocolID())) + { + // The data_sp contains the entire G response packet including the + // G, and if the thread suffix is supported, it has the thread suffix + // as well. + const char *G_packet = (const char *)data_sp->GetBytes(); + size_t G_packet_len = data_sp->GetByteSize(); + if (gdb_comm.SendPacketAndWaitForResponse (G_packet, + G_packet_len, + response, + false)) + { + if (response.IsOKResponse()) + return true; + else if (response.IsErrorResponse()) + { + uint32_t num_restored = 0; + // We need to manually go through all of the registers and + // restore them manually + + response.GetStringRef().assign (G_packet, G_packet_len); + response.SetFilePos(1); // Skip the leading 'G' + DataBufferHeap buffer (m_reg_data.GetByteSize(), 0); + DataExtractor restore_data (buffer.GetBytes(), + buffer.GetByteSize(), + m_reg_data.GetByteOrder(), + m_reg_data.GetAddressByteSize()); + + const uint32_t bytes_extracted = response.GetHexBytes ((void *)restore_data.GetDataStart(), + restore_data.GetByteSize(), + '\xcc'); + + if (bytes_extracted < restore_data.GetByteSize()) + restore_data.SetData(restore_data.GetDataStart(), bytes_extracted, m_reg_data.GetByteOrder()); + + //ReadRegisterBytes (const RegisterInfo *reg_info, RegisterValue &value, DataExtractor &data) + const RegisterInfo *reg_info; + // We have to march the offset of each register along in the + // buffer to make sure we get the right offset. + uint32_t reg_byte_offset = 0; + for (uint32_t reg_idx=0; (reg_info = GetRegisterInfoAtIndex (reg_idx)) != NULL; ++reg_idx, reg_byte_offset += reg_info->byte_size) + { + const uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; + + // Skip composite registers. + if (reg_info->value_regs) + continue; + + // Only write down the registers that need to be written + // if we are going to be doing registers individually. + bool write_reg = true; + const uint32_t reg_byte_size = reg_info->byte_size; + + const char *restore_src = (const char *)restore_data.PeekData(reg_byte_offset, reg_byte_size); + if (restore_src) + { + if (GetRegisterIsValid(reg)) + { + const char *current_src = (const char *)m_reg_data.PeekData(reg_byte_offset, reg_byte_size); + if (current_src) + write_reg = memcmp (current_src, restore_src, reg_byte_size) != 0; + } + + if (write_reg) + { + StreamString packet; + packet.Printf ("P%x=", reg); + packet.PutBytesAsRawHex8 (restore_src, + reg_byte_size, + lldb::endian::InlHostByteOrder(), + lldb::endian::InlHostByteOrder()); + + if (thread_suffix_supported) + packet.Printf (";thread:%4.4" PRIx64 ";", m_thread.GetProtocolID()); + + SetRegisterIsValid(reg, false); + if (gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), + packet.GetString().size(), + response, + false)) + { + if (response.IsOKResponse()) + ++num_restored; + } + } + } + } + return num_restored > 0; + } + } + } + } + else + { + Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS)); + if (log) + { + if (log->GetVerbose()) + { + StreamString strm; + gdb_comm.DumpHistory(strm); + log->Printf("error: failed to get packet sequence mutex, not sending write all registers:\n%s", strm.GetData()); + } + else + log->Printf("error: failed to get packet sequence mutex, not sending write all registers"); + } + } + return false; +} + + +uint32_t +GDBRemoteRegisterContext::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) +{ + return m_reg_info.ConvertRegisterKindToRegisterNumber (kind, num); +} + +void +GDBRemoteDynamicRegisterInfo::HardcodeARMRegisters(bool from_scratch) +{ + // For Advanced SIMD and VFP register mapping. + static uint32_t g_d0_regs[] = { 26, 27, LLDB_INVALID_REGNUM }; // (s0, s1) + static uint32_t g_d1_regs[] = { 28, 29, LLDB_INVALID_REGNUM }; // (s2, s3) + static uint32_t g_d2_regs[] = { 30, 31, LLDB_INVALID_REGNUM }; // (s4, s5) + static uint32_t g_d3_regs[] = { 32, 33, LLDB_INVALID_REGNUM }; // (s6, s7) + static uint32_t g_d4_regs[] = { 34, 35, LLDB_INVALID_REGNUM }; // (s8, s9) + static uint32_t g_d5_regs[] = { 36, 37, LLDB_INVALID_REGNUM }; // (s10, s11) + static uint32_t g_d6_regs[] = { 38, 39, LLDB_INVALID_REGNUM }; // (s12, s13) + static uint32_t g_d7_regs[] = { 40, 41, LLDB_INVALID_REGNUM }; // (s14, s15) + static uint32_t g_d8_regs[] = { 42, 43, LLDB_INVALID_REGNUM }; // (s16, s17) + static uint32_t g_d9_regs[] = { 44, 45, LLDB_INVALID_REGNUM }; // (s18, s19) + static uint32_t g_d10_regs[] = { 46, 47, LLDB_INVALID_REGNUM }; // (s20, s21) + static uint32_t g_d11_regs[] = { 48, 49, LLDB_INVALID_REGNUM }; // (s22, s23) + static uint32_t g_d12_regs[] = { 50, 51, LLDB_INVALID_REGNUM }; // (s24, s25) + static uint32_t g_d13_regs[] = { 52, 53, LLDB_INVALID_REGNUM }; // (s26, s27) + static uint32_t g_d14_regs[] = { 54, 55, LLDB_INVALID_REGNUM }; // (s28, s29) + static uint32_t g_d15_regs[] = { 56, 57, LLDB_INVALID_REGNUM }; // (s30, s31) + static uint32_t g_q0_regs[] = { 26, 27, 28, 29, LLDB_INVALID_REGNUM }; // (d0, d1) -> (s0, s1, s2, s3) + static uint32_t g_q1_regs[] = { 30, 31, 32, 33, LLDB_INVALID_REGNUM }; // (d2, d3) -> (s4, s5, s6, s7) + static uint32_t g_q2_regs[] = { 34, 35, 36, 37, LLDB_INVALID_REGNUM }; // (d4, d5) -> (s8, s9, s10, s11) + static uint32_t g_q3_regs[] = { 38, 39, 40, 41, LLDB_INVALID_REGNUM }; // (d6, d7) -> (s12, s13, s14, s15) + static uint32_t g_q4_regs[] = { 42, 43, 44, 45, LLDB_INVALID_REGNUM }; // (d8, d9) -> (s16, s17, s18, s19) + static uint32_t g_q5_regs[] = { 46, 47, 48, 49, LLDB_INVALID_REGNUM }; // (d10, d11) -> (s20, s21, s22, s23) + static uint32_t g_q6_regs[] = { 50, 51, 52, 53, LLDB_INVALID_REGNUM }; // (d12, d13) -> (s24, s25, s26, s27) + static uint32_t g_q7_regs[] = { 54, 55, 56, 57, LLDB_INVALID_REGNUM }; // (d14, d15) -> (s28, s29, s30, s31) + static uint32_t g_q8_regs[] = { 59, 60, LLDB_INVALID_REGNUM }; // (d16, d17) + static uint32_t g_q9_regs[] = { 61, 62, LLDB_INVALID_REGNUM }; // (d18, d19) + static uint32_t g_q10_regs[] = { 63, 64, LLDB_INVALID_REGNUM }; // (d20, d21) + static uint32_t g_q11_regs[] = { 65, 66, LLDB_INVALID_REGNUM }; // (d22, d23) + static uint32_t g_q12_regs[] = { 67, 68, LLDB_INVALID_REGNUM }; // (d24, d25) + static uint32_t g_q13_regs[] = { 69, 70, LLDB_INVALID_REGNUM }; // (d26, d27) + static uint32_t g_q14_regs[] = { 71, 72, LLDB_INVALID_REGNUM }; // (d28, d29) + static uint32_t g_q15_regs[] = { 73, 74, LLDB_INVALID_REGNUM }; // (d30, d31) + + // This is our array of composite registers, with each element coming from the above register mappings. + static uint32_t *g_composites[] = { + g_d0_regs, g_d1_regs, g_d2_regs, g_d3_regs, g_d4_regs, g_d5_regs, g_d6_regs, g_d7_regs, + g_d8_regs, g_d9_regs, g_d10_regs, g_d11_regs, g_d12_regs, g_d13_regs, g_d14_regs, g_d15_regs, + g_q0_regs, g_q1_regs, g_q2_regs, g_q3_regs, g_q4_regs, g_q5_regs, g_q6_regs, g_q7_regs, + g_q8_regs, g_q9_regs, g_q10_regs, g_q11_regs, g_q12_regs, g_q13_regs, g_q14_regs, g_q15_regs + }; + + static RegisterInfo g_register_infos[] = { +// NAME ALT SZ OFF ENCODING FORMAT COMPILER DWARF GENERIC GDB LLDB VALUE REGS INVALIDATE REGS +// ====== ====== === === ============= ============ =================== =================== ====================== === ==== ========== =============== + { "r0", "arg1", 4, 0, eEncodingUint, eFormatHex, { gcc_r0, dwarf_r0, LLDB_REGNUM_GENERIC_ARG1,0, 0 }, NULL, NULL}, + { "r1", "arg2", 4, 0, eEncodingUint, eFormatHex, { gcc_r1, dwarf_r1, LLDB_REGNUM_GENERIC_ARG2,1, 1 }, NULL, NULL}, + { "r2", "arg3", 4, 0, eEncodingUint, eFormatHex, { gcc_r2, dwarf_r2, LLDB_REGNUM_GENERIC_ARG3,2, 2 }, NULL, NULL}, + { "r3", "arg4", 4, 0, eEncodingUint, eFormatHex, { gcc_r3, dwarf_r3, LLDB_REGNUM_GENERIC_ARG4,3, 3 }, NULL, NULL}, + { "r4", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r4, dwarf_r4, LLDB_INVALID_REGNUM, 4, 4 }, NULL, NULL}, + { "r5", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r5, dwarf_r5, LLDB_INVALID_REGNUM, 5, 5 }, NULL, NULL}, + { "r6", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r6, dwarf_r6, LLDB_INVALID_REGNUM, 6, 6 }, NULL, NULL}, + { "r7", "fp", 4, 0, eEncodingUint, eFormatHex, { gcc_r7, dwarf_r7, LLDB_REGNUM_GENERIC_FP, 7, 7 }, NULL, NULL}, + { "r8", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r8, dwarf_r8, LLDB_INVALID_REGNUM, 8, 8 }, NULL, NULL}, + { "r9", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r9, dwarf_r9, LLDB_INVALID_REGNUM, 9, 9 }, NULL, NULL}, + { "r10", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r10, dwarf_r10, LLDB_INVALID_REGNUM, 10, 10 }, NULL, NULL}, + { "r11", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r11, dwarf_r11, LLDB_INVALID_REGNUM, 11, 11 }, NULL, NULL}, + { "r12", NULL, 4, 0, eEncodingUint, eFormatHex, { gcc_r12, dwarf_r12, LLDB_INVALID_REGNUM, 12, 12 }, NULL, NULL}, + { "sp", "r13", 4, 0, eEncodingUint, eFormatHex, { gcc_sp, dwarf_sp, LLDB_REGNUM_GENERIC_SP, 13, 13 }, NULL, NULL}, + { "lr", "r14", 4, 0, eEncodingUint, eFormatHex, { gcc_lr, dwarf_lr, LLDB_REGNUM_GENERIC_RA, 14, 14 }, NULL, NULL}, + { "pc", "r15", 4, 0, eEncodingUint, eFormatHex, { gcc_pc, dwarf_pc, LLDB_REGNUM_GENERIC_PC, 15, 15 }, NULL, NULL}, + { "f0", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 16, 16 }, NULL, NULL}, + { "f1", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 17, 17 }, NULL, NULL}, + { "f2", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 18, 18 }, NULL, NULL}, + { "f3", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 19, 19 }, NULL, NULL}, + { "f4", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 20, 20 }, NULL, NULL}, + { "f5", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 21, 21 }, NULL, NULL}, + { "f6", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 22, 22 }, NULL, NULL}, + { "f7", NULL, 12, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 23, 23 }, NULL, NULL}, + { "fps", NULL, 4, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 24, 24 }, NULL, NULL}, + { "cpsr","flags", 4, 0, eEncodingUint, eFormatHex, { gcc_cpsr, dwarf_cpsr, LLDB_INVALID_REGNUM, 25, 25 }, NULL, NULL}, + { "s0", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s0, LLDB_INVALID_REGNUM, 26, 26 }, NULL, NULL}, + { "s1", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s1, LLDB_INVALID_REGNUM, 27, 27 }, NULL, NULL}, + { "s2", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s2, LLDB_INVALID_REGNUM, 28, 28 }, NULL, NULL}, + { "s3", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s3, LLDB_INVALID_REGNUM, 29, 29 }, NULL, NULL}, + { "s4", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s4, LLDB_INVALID_REGNUM, 30, 30 }, NULL, NULL}, + { "s5", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s5, LLDB_INVALID_REGNUM, 31, 31 }, NULL, NULL}, + { "s6", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s6, LLDB_INVALID_REGNUM, 32, 32 }, NULL, NULL}, + { "s7", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s7, LLDB_INVALID_REGNUM, 33, 33 }, NULL, NULL}, + { "s8", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s8, LLDB_INVALID_REGNUM, 34, 34 }, NULL, NULL}, + { "s9", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s9, LLDB_INVALID_REGNUM, 35, 35 }, NULL, NULL}, + { "s10", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s10, LLDB_INVALID_REGNUM, 36, 36 }, NULL, NULL}, + { "s11", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s11, LLDB_INVALID_REGNUM, 37, 37 }, NULL, NULL}, + { "s12", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s12, LLDB_INVALID_REGNUM, 38, 38 }, NULL, NULL}, + { "s13", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s13, LLDB_INVALID_REGNUM, 39, 39 }, NULL, NULL}, + { "s14", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s14, LLDB_INVALID_REGNUM, 40, 40 }, NULL, NULL}, + { "s15", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s15, LLDB_INVALID_REGNUM, 41, 41 }, NULL, NULL}, + { "s16", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s16, LLDB_INVALID_REGNUM, 42, 42 }, NULL, NULL}, + { "s17", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s17, LLDB_INVALID_REGNUM, 43, 43 }, NULL, NULL}, + { "s18", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s18, LLDB_INVALID_REGNUM, 44, 44 }, NULL, NULL}, + { "s19", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s19, LLDB_INVALID_REGNUM, 45, 45 }, NULL, NULL}, + { "s20", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s20, LLDB_INVALID_REGNUM, 46, 46 }, NULL, NULL}, + { "s21", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s21, LLDB_INVALID_REGNUM, 47, 47 }, NULL, NULL}, + { "s22", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s22, LLDB_INVALID_REGNUM, 48, 48 }, NULL, NULL}, + { "s23", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s23, LLDB_INVALID_REGNUM, 49, 49 }, NULL, NULL}, + { "s24", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s24, LLDB_INVALID_REGNUM, 50, 50 }, NULL, NULL}, + { "s25", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s25, LLDB_INVALID_REGNUM, 51, 51 }, NULL, NULL}, + { "s26", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s26, LLDB_INVALID_REGNUM, 52, 52 }, NULL, NULL}, + { "s27", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s27, LLDB_INVALID_REGNUM, 53, 53 }, NULL, NULL}, + { "s28", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s28, LLDB_INVALID_REGNUM, 54, 54 }, NULL, NULL}, + { "s29", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s29, LLDB_INVALID_REGNUM, 55, 55 }, NULL, NULL}, + { "s30", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s30, LLDB_INVALID_REGNUM, 56, 56 }, NULL, NULL}, + { "s31", NULL, 4, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s31, LLDB_INVALID_REGNUM, 57, 57 }, NULL, NULL}, + { "fpscr",NULL, 4, 0, eEncodingUint, eFormatHex, { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 58, 58 }, NULL, NULL}, + { "d16", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d16, LLDB_INVALID_REGNUM, 59, 59 }, NULL, NULL}, + { "d17", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d17, LLDB_INVALID_REGNUM, 60, 60 }, NULL, NULL}, + { "d18", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d18, LLDB_INVALID_REGNUM, 61, 61 }, NULL, NULL}, + { "d19", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d19, LLDB_INVALID_REGNUM, 62, 62 }, NULL, NULL}, + { "d20", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d20, LLDB_INVALID_REGNUM, 63, 63 }, NULL, NULL}, + { "d21", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d21, LLDB_INVALID_REGNUM, 64, 64 }, NULL, NULL}, + { "d22", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d22, LLDB_INVALID_REGNUM, 65, 65 }, NULL, NULL}, + { "d23", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d23, LLDB_INVALID_REGNUM, 66, 66 }, NULL, NULL}, + { "d24", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d24, LLDB_INVALID_REGNUM, 67, 67 }, NULL, NULL}, + { "d25", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d25, LLDB_INVALID_REGNUM, 68, 68 }, NULL, NULL}, + { "d26", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d26, LLDB_INVALID_REGNUM, 69, 69 }, NULL, NULL}, + { "d27", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d27, LLDB_INVALID_REGNUM, 70, 70 }, NULL, NULL}, + { "d28", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d28, LLDB_INVALID_REGNUM, 71, 71 }, NULL, NULL}, + { "d29", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d29, LLDB_INVALID_REGNUM, 72, 72 }, NULL, NULL}, + { "d30", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d30, LLDB_INVALID_REGNUM, 73, 73 }, NULL, NULL}, + { "d31", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d31, LLDB_INVALID_REGNUM, 74, 74 }, NULL, NULL}, + { "d0", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d0, LLDB_INVALID_REGNUM, 75, 75 }, g_d0_regs, NULL}, + { "d1", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d1, LLDB_INVALID_REGNUM, 76, 76 }, g_d1_regs, NULL}, + { "d2", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d2, LLDB_INVALID_REGNUM, 77, 77 }, g_d2_regs, NULL}, + { "d3", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d3, LLDB_INVALID_REGNUM, 78, 78 }, g_d3_regs, NULL}, + { "d4", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d4, LLDB_INVALID_REGNUM, 79, 79 }, g_d4_regs, NULL}, + { "d5", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d5, LLDB_INVALID_REGNUM, 80, 80 }, g_d5_regs, NULL}, + { "d6", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d6, LLDB_INVALID_REGNUM, 81, 81 }, g_d6_regs, NULL}, + { "d7", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d7, LLDB_INVALID_REGNUM, 82, 82 }, g_d7_regs, NULL}, + { "d8", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d8, LLDB_INVALID_REGNUM, 83, 83 }, g_d8_regs, NULL}, + { "d9", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d9, LLDB_INVALID_REGNUM, 84, 84 }, g_d9_regs, NULL}, + { "d10", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d10, LLDB_INVALID_REGNUM, 85, 85 }, g_d10_regs, NULL}, + { "d11", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d11, LLDB_INVALID_REGNUM, 86, 86 }, g_d11_regs, NULL}, + { "d12", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d12, LLDB_INVALID_REGNUM, 87, 87 }, g_d12_regs, NULL}, + { "d13", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d13, LLDB_INVALID_REGNUM, 88, 88 }, g_d13_regs, NULL}, + { "d14", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d14, LLDB_INVALID_REGNUM, 89, 89 }, g_d14_regs, NULL}, + { "d15", NULL, 8, 0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d15, LLDB_INVALID_REGNUM, 90, 90 }, g_d15_regs, NULL}, + { "q0", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q0, LLDB_INVALID_REGNUM, 91, 91 }, g_q0_regs, NULL}, + { "q1", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q1, LLDB_INVALID_REGNUM, 92, 92 }, g_q1_regs, NULL}, + { "q2", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q2, LLDB_INVALID_REGNUM, 93, 93 }, g_q2_regs, NULL}, + { "q3", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q3, LLDB_INVALID_REGNUM, 94, 94 }, g_q3_regs, NULL}, + { "q4", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q4, LLDB_INVALID_REGNUM, 95, 95 }, g_q4_regs, NULL}, + { "q5", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q5, LLDB_INVALID_REGNUM, 96, 96 }, g_q5_regs, NULL}, + { "q6", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q6, LLDB_INVALID_REGNUM, 97, 97 }, g_q6_regs, NULL}, + { "q7", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q7, LLDB_INVALID_REGNUM, 98, 98 }, g_q7_regs, NULL}, + { "q8", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q8, LLDB_INVALID_REGNUM, 99, 99 }, g_q8_regs, NULL}, + { "q9", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q9, LLDB_INVALID_REGNUM, 100, 100 }, g_q9_regs, NULL}, + { "q10", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q10, LLDB_INVALID_REGNUM, 101, 101 }, g_q10_regs, NULL}, + { "q11", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q11, LLDB_INVALID_REGNUM, 102, 102 }, g_q11_regs, NULL}, + { "q12", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q12, LLDB_INVALID_REGNUM, 103, 103 }, g_q12_regs, NULL}, + { "q13", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q13, LLDB_INVALID_REGNUM, 104, 104 }, g_q13_regs, NULL}, + { "q14", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q14, LLDB_INVALID_REGNUM, 105, 105 }, g_q14_regs, NULL}, + { "q15", NULL, 16, 0, eEncodingVector, eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q15, LLDB_INVALID_REGNUM, 106, 106 }, g_q15_regs, NULL} + }; + + static const uint32_t num_registers = llvm::array_lengthof(g_register_infos); + static ConstString gpr_reg_set ("General Purpose Registers"); + static ConstString sfp_reg_set ("Software Floating Point Registers"); + static ConstString vfp_reg_set ("Floating Point Registers"); + size_t i; + if (from_scratch) + { + // Calculate the offsets of the registers + // Note that the layout of the "composite" registers (d0-d15 and q0-q15) which comes after the + // "primordial" registers is important. This enables us to calculate the offset of the composite + // register by using the offset of its first primordial register. For example, to calculate the + // offset of q0, use s0's offset. + if (g_register_infos[2].byte_offset == 0) + { + uint32_t byte_offset = 0; + for (i=0; i<num_registers; ++i) + { + // For primordial registers, increment the byte_offset by the byte_size to arrive at the + // byte_offset for the next register. Otherwise, we have a composite register whose + // offset can be calculated by consulting the offset of its first primordial register. + if (!g_register_infos[i].value_regs) + { + g_register_infos[i].byte_offset = byte_offset; + byte_offset += g_register_infos[i].byte_size; + } + else + { + const uint32_t first_primordial_reg = g_register_infos[i].value_regs[0]; + g_register_infos[i].byte_offset = g_register_infos[first_primordial_reg].byte_offset; + } + } + } + for (i=0; i<num_registers; ++i) + { + ConstString name; + ConstString alt_name; + if (g_register_infos[i].name && g_register_infos[i].name[0]) + name.SetCString(g_register_infos[i].name); + if (g_register_infos[i].alt_name && g_register_infos[i].alt_name[0]) + alt_name.SetCString(g_register_infos[i].alt_name); + + if (i <= 15 || i == 25) + AddRegister (g_register_infos[i], name, alt_name, gpr_reg_set); + else if (i <= 24) + AddRegister (g_register_infos[i], name, alt_name, sfp_reg_set); + else + AddRegister (g_register_infos[i], name, alt_name, vfp_reg_set); + } + } + else + { + // Add composite registers to our primordial registers, then. + const size_t num_composites = llvm::array_lengthof(g_composites); + const size_t num_dynamic_regs = GetNumRegisters(); + const size_t num_common_regs = num_registers - num_composites; + RegisterInfo *g_comp_register_infos = g_register_infos + num_common_regs; + + // First we need to validate that all registers that we already have match the non composite regs. + // If so, then we can add the registers, else we need to bail + bool match = true; + if (num_dynamic_regs == num_common_regs) + { + for (i=0; match && i<num_dynamic_regs; ++i) + { + // Make sure all register names match + if (m_regs[i].name && g_register_infos[i].name) + { + if (strcmp(m_regs[i].name, g_register_infos[i].name)) + { + match = false; + break; + } + } + + // Make sure all register byte sizes match + if (m_regs[i].byte_size != g_register_infos[i].byte_size) + { + match = false; + break; + } + } + } + else + { + // Wrong number of registers. + match = false; + } + // If "match" is true, then we can add extra registers. + if (match) + { + for (i=0; i<num_composites; ++i) + { + ConstString name; + ConstString alt_name; + const uint32_t first_primordial_reg = g_comp_register_infos[i].value_regs[0]; + const char *reg_name = g_register_infos[first_primordial_reg].name; + if (reg_name && reg_name[0]) + { + for (uint32_t j = 0; j < num_dynamic_regs; ++j) + { + const RegisterInfo *reg_info = GetRegisterInfoAtIndex(j); + // Find a matching primordial register info entry. + if (reg_info && reg_info->name && ::strcasecmp(reg_info->name, reg_name) == 0) + { + // The name matches the existing primordial entry. + // Find and assign the offset, and then add this composite register entry. + g_comp_register_infos[i].byte_offset = reg_info->byte_offset; + name.SetCString(g_comp_register_infos[i].name); + AddRegister(g_comp_register_infos[i], name, alt_name, vfp_reg_set); + } + } + } + } + } + } +} diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.h new file mode 100644 index 0000000..3110ddf --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.h @@ -0,0 +1,311 @@ +//===-- GDBRemoteRegisterContext.h ------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef lldb_GDBRemoteRegisterContext_h_ +#define lldb_GDBRemoteRegisterContext_h_ + +// C Includes +// C++ Includes +#include <vector> + +// Other libraries and framework includes +// Project includes +#include "lldb/lldb-private.h" +#include "lldb/lldb-enumerations.h" +#include "lldb/Core/ConstString.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Target/RegisterContext.h" +#include "GDBRemoteCommunicationClient.h" + +class ThreadGDBRemote; +class ProcessGDBRemote; +class StringExtractor; + +class GDBRemoteDynamicRegisterInfo +{ +public: + GDBRemoteDynamicRegisterInfo () : + m_regs (), + m_sets (), + m_set_reg_nums (), + m_reg_names (), + m_reg_alt_names (), + m_set_names (), + m_reg_data_byte_size (0) + { + } + + ~GDBRemoteDynamicRegisterInfo () + { + } + + void + AddRegister (lldb_private::RegisterInfo reg_info, + lldb_private::ConstString ®_name, + lldb_private::ConstString ®_alt_name, + lldb_private::ConstString &set_name) + { + const uint32_t reg_num = (uint32_t)m_regs.size(); + m_reg_names.push_back (reg_name); + m_reg_alt_names.push_back (reg_alt_name); + reg_info.name = reg_name.AsCString(); + assert (reg_info.name); + reg_info.alt_name = reg_alt_name.AsCString(NULL); + uint32_t i; + if (reg_info.value_regs) + { + for (i=0; reg_info.value_regs[i] != LLDB_INVALID_REGNUM; ++i) + m_value_regs_map[reg_num].push_back(reg_info.value_regs[i]); + m_value_regs_map[reg_num].push_back(LLDB_INVALID_REGNUM); + reg_info.value_regs = m_value_regs_map[reg_num].data(); + } + if (reg_info.invalidate_regs) + { + for (i=0; reg_info.invalidate_regs[i] != LLDB_INVALID_REGNUM; ++i) + m_invalidate_regs_map[reg_num].push_back(reg_info.invalidate_regs[i]); + m_invalidate_regs_map[reg_num].push_back(LLDB_INVALID_REGNUM); + reg_info.invalidate_regs = m_invalidate_regs_map[reg_num].data(); + } + m_regs.push_back (reg_info); + uint32_t set = GetRegisterSetIndexByName (set_name); + assert (set < m_sets.size()); + assert (set < m_set_reg_nums.size()); + assert (set < m_set_names.size()); + m_set_reg_nums[set].push_back(reg_num); + size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size; + if (m_reg_data_byte_size < end_reg_offset) + m_reg_data_byte_size = end_reg_offset; + } + + void + Finalize () + { + for (uint32_t set = 0; set < m_sets.size(); ++set) + { + assert (m_sets.size() == m_set_reg_nums.size()); + m_sets[set].num_registers = m_set_reg_nums[set].size(); + m_sets[set].registers = &m_set_reg_nums[set][0]; + } + } + + size_t + GetNumRegisters() const + { + return m_regs.size(); + } + + size_t + GetNumRegisterSets() const + { + return m_sets.size(); + } + + size_t + GetRegisterDataByteSize() const + { + return m_reg_data_byte_size; + } + + const lldb_private::RegisterInfo * + GetRegisterInfoAtIndex (uint32_t i) const + { + if (i < m_regs.size()) + return &m_regs[i]; + return NULL; + } + + const lldb_private::RegisterSet * + GetRegisterSet (uint32_t i) const + { + if (i < m_sets.size()) + return &m_sets[i]; + return NULL; + } + + uint32_t + GetRegisterSetIndexByName (lldb_private::ConstString &set_name) + { + name_collection::iterator pos, end = m_set_names.end(); + for (pos = m_set_names.begin(); pos != end; ++pos) + { + if (*pos == set_name) + return static_cast<uint32_t>(std::distance (m_set_names.begin(), pos)); + } + + m_set_names.push_back(set_name); + m_set_reg_nums.resize(m_set_reg_nums.size()+1); + lldb_private::RegisterSet new_set = { set_name.AsCString(), NULL, 0, NULL }; + m_sets.push_back (new_set); + return static_cast<uint32_t>(m_sets.size() - 1); + } + + uint32_t + ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) const + { + reg_collection::const_iterator pos, end = m_regs.end(); + for (pos = m_regs.begin(); pos != end; ++pos) + { + if (pos->kinds[kind] == num) + return static_cast<uint32_t>(std::distance (m_regs.begin(), pos)); + } + + return LLDB_INVALID_REGNUM; + } + void + Clear() + { + m_regs.clear(); + m_sets.clear(); + m_set_reg_nums.clear(); + m_reg_names.clear(); + m_reg_alt_names.clear(); + m_set_names.clear(); + } + + void + HardcodeARMRegisters(bool from_scratch); + +protected: + //------------------------------------------------------------------ + // Classes that inherit from GDBRemoteRegisterContext can see and modify these + //------------------------------------------------------------------ + typedef std::vector <lldb_private::RegisterInfo> reg_collection; + typedef std::vector <lldb_private::RegisterSet> set_collection; + typedef std::vector <uint32_t> reg_num_collection; + typedef std::vector <reg_num_collection> set_reg_num_collection; + typedef std::vector <lldb_private::ConstString> name_collection; + typedef std::map<uint32_t, reg_num_collection> reg_to_regs_map; + + reg_collection m_regs; + set_collection m_sets; + set_reg_num_collection m_set_reg_nums; + name_collection m_reg_names; + name_collection m_reg_alt_names; + name_collection m_set_names; + reg_to_regs_map m_value_regs_map; + reg_to_regs_map m_invalidate_regs_map; + size_t m_reg_data_byte_size; // The number of bytes required to store all registers +}; + +class GDBRemoteRegisterContext : public lldb_private::RegisterContext +{ +public: + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + GDBRemoteRegisterContext (ThreadGDBRemote &thread, + uint32_t concrete_frame_idx, + GDBRemoteDynamicRegisterInfo ®_info, + bool read_all_at_once); + + virtual + ~GDBRemoteRegisterContext (); + + //------------------------------------------------------------------ + // Subclasses must override these functions + //------------------------------------------------------------------ + virtual void + InvalidateAllRegisters (); + + virtual size_t + GetRegisterCount (); + + virtual const lldb_private::RegisterInfo * + GetRegisterInfoAtIndex (size_t reg); + + virtual size_t + GetRegisterSetCount (); + + virtual const lldb_private::RegisterSet * + GetRegisterSet (size_t reg_set); + + virtual bool + ReadRegister (const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value); + + virtual bool + WriteRegister (const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value); + + virtual bool + ReadAllRegisterValues (lldb::DataBufferSP &data_sp); + + virtual bool + WriteAllRegisterValues (const lldb::DataBufferSP &data_sp); + + virtual uint32_t + ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num); + +protected: + friend class ThreadGDBRemote; + + bool + ReadRegisterBytes (const lldb_private::RegisterInfo *reg_info, + lldb_private::DataExtractor &data); + + bool + WriteRegisterBytes (const lldb_private::RegisterInfo *reg_info, + lldb_private::DataExtractor &data, + uint32_t data_offset); + + bool + PrivateSetRegisterValue (uint32_t reg, StringExtractor &response); + + void + SetAllRegisterValid (bool b); + + bool + GetRegisterIsValid (uint32_t reg) const + { +#if defined (LLDB_CONFIGURATION_DEBUG) + assert (reg < m_reg_valid.size()); +#endif + if (reg < m_reg_valid.size()) + return m_reg_valid[reg]; + return false; + } + + void + SetRegisterIsValid (const lldb_private::RegisterInfo *reg_info, bool valid) + { + if (reg_info) + return SetRegisterIsValid (reg_info->kinds[lldb::eRegisterKindLLDB], valid); + } + + void + SetRegisterIsValid (uint32_t reg, bool valid) + { +#if defined (LLDB_CONFIGURATION_DEBUG) + assert (reg < m_reg_valid.size()); +#endif + if (reg < m_reg_valid.size()) + m_reg_valid[reg] = valid; + } + + void + SyncThreadState(lldb_private::Process *process); // Assumes the sequence mutex has already been acquired. + + GDBRemoteDynamicRegisterInfo &m_reg_info; + std::vector<bool> m_reg_valid; + lldb_private::DataExtractor m_reg_data; + bool m_read_all_at_once; + +private: + // Helper function for ReadRegisterBytes(). + bool GetPrimordialRegister(const lldb_private::RegisterInfo *reg_info, + GDBRemoteCommunicationClient &gdb_comm); + // Helper function for WriteRegisterBytes(). + bool SetPrimordialRegister(const lldb_private::RegisterInfo *reg_info, + GDBRemoteCommunicationClient &gdb_comm); + + //------------------------------------------------------------------ + // For GDBRemoteRegisterContext only + //------------------------------------------------------------------ + DISALLOW_COPY_AND_ASSIGN (GDBRemoteRegisterContext); +}; + +#endif // lldb_GDBRemoteRegisterContext_h_ diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp new file mode 100644 index 0000000..d27207f --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp @@ -0,0 +1,3291 @@ +//===-- ProcessGDBRemote.cpp ------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-python.h" + +// C Includes +#include <errno.h> +#include <spawn.h> +#include <stdlib.h> +#include <netinet/in.h> +#include <sys/mman.h> // for mmap +#include <sys/stat.h> +#include <sys/types.h> +#include <time.h> + +// C++ Includes +#include <algorithm> +#include <map> + +// Other libraries and framework includes + +#include "lldb/Breakpoint/Watchpoint.h" +#include "lldb/Interpreter/Args.h" +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/ConnectionFileDescriptor.h" +#include "lldb/Host/FileSpec.h" +#include "lldb/Core/InputReader.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/State.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/Timer.h" +#include "lldb/Core/Value.h" +#include "lldb/Host/Symbols.h" +#include "lldb/Host/TimeValue.h" +#include "lldb/Interpreter/CommandInterpreter.h" +#include "lldb/Interpreter/CommandObject.h" +#include "lldb/Interpreter/CommandObjectMultiword.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/DynamicLoader.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/TargetList.h" +#include "lldb/Target/ThreadPlanCallFunction.h" +#include "lldb/Utility/PseudoTerminal.h" + +// Project includes +#include "lldb/Host/Host.h" +#include "Plugins/Process/Utility/InferiorCallPOSIX.h" +#include "Plugins/Process/Utility/StopInfoMachException.h" +#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" +#include "Utility/StringExtractorGDBRemote.h" +#include "GDBRemoteRegisterContext.h" +#include "ProcessGDBRemote.h" +#include "ProcessGDBRemoteLog.h" +#include "ThreadGDBRemote.h" + + +namespace lldb +{ + // Provide a function that can easily dump the packet history if we know a + // ProcessGDBRemote * value (which we can get from logs or from debugging). + // We need the function in the lldb namespace so it makes it into the final + // executable since the LLDB shared library only exports stuff in the lldb + // namespace. This allows you to attach with a debugger and call this + // function and get the packet history dumped to a file. + void + DumpProcessGDBRemotePacketHistory (void *p, const char *path) + { + lldb_private::StreamFile strm; + lldb_private::Error error (strm.GetFile().Open(path, lldb_private::File::eOpenOptionWrite | lldb_private::File::eOpenOptionCanCreate)); + if (error.Success()) + ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm); + } +} + +#define DEBUGSERVER_BASENAME "debugserver" +using namespace lldb; +using namespace lldb_private; + + +namespace { + + static PropertyDefinition + g_properties[] = + { + { "packet-timeout" , OptionValue::eTypeUInt64 , true , 1, NULL, NULL, "Specify the default packet timeout in seconds." }, + { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } + }; + + enum + { + ePropertyPacketTimeout + }; + + class PluginProperties : public Properties + { + public: + + static ConstString + GetSettingName () + { + return ProcessGDBRemote::GetPluginNameStatic(); + } + + PluginProperties() : + Properties () + { + m_collection_sp.reset (new OptionValueProperties(GetSettingName())); + m_collection_sp->Initialize(g_properties); + } + + virtual + ~PluginProperties() + { + } + + uint64_t + GetPacketTimeout() + { + const uint32_t idx = ePropertyPacketTimeout; + return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); + } + }; + + typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; + + static const ProcessKDPPropertiesSP & + GetGlobalPluginProperties() + { + static ProcessKDPPropertiesSP g_settings_sp; + if (!g_settings_sp) + g_settings_sp.reset (new PluginProperties ()); + return g_settings_sp; + } + +} // anonymous namespace end + +static bool rand_initialized = false; + +// TODO Randomly assigning a port is unsafe. We should get an unused +// ephemeral port from the kernel and make sure we reserve it before passing +// it to debugserver. + +#if defined (__APPLE__) +#define LOW_PORT (IPPORT_RESERVED) +#define HIGH_PORT (IPPORT_HIFIRSTAUTO) +#else +#define LOW_PORT (1024u) +#define HIGH_PORT (49151u) +#endif + +static inline uint16_t +get_random_port () +{ + if (!rand_initialized) + { + time_t seed = time(NULL); + + rand_initialized = true; + srand(seed); + } + return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT; +} + + +lldb_private::ConstString +ProcessGDBRemote::GetPluginNameStatic() +{ + static ConstString g_name("gdb-remote"); + return g_name; +} + +const char * +ProcessGDBRemote::GetPluginDescriptionStatic() +{ + return "GDB Remote protocol based debugging plug-in."; +} + +void +ProcessGDBRemote::Terminate() +{ + PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance); +} + + +lldb::ProcessSP +ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path) +{ + lldb::ProcessSP process_sp; + if (crash_file_path == NULL) + process_sp.reset (new ProcessGDBRemote (target, listener)); + return process_sp; +} + +bool +ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name) +{ + if (plugin_specified_by_name) + return true; + + // For now we are just making sure the file exists for a given module + Module *exe_module = target.GetExecutableModulePointer(); + if (exe_module) + { + ObjectFile *exe_objfile = exe_module->GetObjectFile(); + // We can't debug core files... + switch (exe_objfile->GetType()) + { + case ObjectFile::eTypeInvalid: + case ObjectFile::eTypeCoreFile: + case ObjectFile::eTypeDebugInfo: + case ObjectFile::eTypeObjectFile: + case ObjectFile::eTypeSharedLibrary: + case ObjectFile::eTypeStubLibrary: + return false; + case ObjectFile::eTypeExecutable: + case ObjectFile::eTypeDynamicLinker: + case ObjectFile::eTypeUnknown: + break; + } + return exe_module->GetFileSpec().Exists(); + } + // However, if there is no executable module, we return true since we might be preparing to attach. + return true; +} + +//---------------------------------------------------------------------- +// ProcessGDBRemote constructor +//---------------------------------------------------------------------- +ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) : + Process (target, listener), + m_flags (0), + m_gdb_comm(false), + m_debugserver_pid (LLDB_INVALID_PROCESS_ID), + m_last_stop_packet (), + m_last_stop_packet_mutex (Mutex::eMutexTypeNormal), + m_register_info (), + m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"), + m_async_thread (LLDB_INVALID_HOST_THREAD), + m_async_thread_state(eAsyncThreadNotStarted), + m_async_thread_state_mutex(Mutex::eMutexTypeRecursive), + m_thread_ids (), + m_continue_c_tids (), + m_continue_C_tids (), + m_continue_s_tids (), + m_continue_S_tids (), + m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS), + m_max_memory_size (512), + m_addr_to_mmap_size (), + m_thread_create_bp_sp (), + m_waiting_for_attach (false), + m_destroy_tried_resuming (false), + m_command_sp () +{ + m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); + m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); + m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit"); + const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); + if (timeout_seconds > 0) + m_gdb_comm.SetPacketTimeout(timeout_seconds); +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +ProcessGDBRemote::~ProcessGDBRemote() +{ + // m_mach_process.UnregisterNotificationCallbacks (this); + Clear(); + // We need to call finalize on the process before destroying ourselves + // to make sure all of the broadcaster cleanup goes as planned. If we + // destruct this class, then Process::~Process() might have problems + // trying to fully destroy the broadcaster. + Finalize(); + + // The general Finalize is going to try to destroy the process and that SHOULD + // shut down the async thread. However, if we don't kill it it will get stranded and + // its connection will go away so when it wakes up it will crash. So kill it for sure here. + StopAsyncThread(); + KillDebugserverProcess(); +} + +//---------------------------------------------------------------------- +// PluginInterface +//---------------------------------------------------------------------- +ConstString +ProcessGDBRemote::GetPluginName() +{ + return GetPluginNameStatic(); +} + +uint32_t +ProcessGDBRemote::GetPluginVersion() +{ + return 1; +} + +void +ProcessGDBRemote::BuildDynamicRegisterInfo (bool force) +{ + if (!force && m_register_info.GetNumRegisters() > 0) + return; + + char packet[128]; + m_register_info.Clear(); + uint32_t reg_offset = 0; + uint32_t reg_num = 0; + for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse; + response_type == StringExtractorGDBRemote::eResponse; + ++reg_num) + { + const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); + assert (packet_len < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false)) + { + response_type = response.GetResponseType(); + if (response_type == StringExtractorGDBRemote::eResponse) + { + std::string name; + std::string value; + ConstString reg_name; + ConstString alt_name; + ConstString set_name; + std::vector<uint32_t> value_regs; + std::vector<uint32_t> invalidate_regs; + RegisterInfo reg_info = { NULL, // Name + NULL, // Alt name + 0, // byte size + reg_offset, // offset + eEncodingUint, // encoding + eFormatHex, // formate + { + LLDB_INVALID_REGNUM, // GCC reg num + LLDB_INVALID_REGNUM, // DWARF reg num + LLDB_INVALID_REGNUM, // generic reg num + reg_num, // GDB reg num + reg_num // native register number + }, + NULL, + NULL + }; + + while (response.GetNameColonValue(name, value)) + { + if (name.compare("name") == 0) + { + reg_name.SetCString(value.c_str()); + } + else if (name.compare("alt-name") == 0) + { + alt_name.SetCString(value.c_str()); + } + else if (name.compare("bitsize") == 0) + { + reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT; + } + else if (name.compare("offset") == 0) + { + uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0); + if (reg_offset != offset) + { + reg_offset = offset; + } + } + else if (name.compare("encoding") == 0) + { + const Encoding encoding = Args::StringToEncoding (value.c_str()); + if (encoding != eEncodingInvalid) + reg_info.encoding = encoding; + } + else if (name.compare("format") == 0) + { + Format format = eFormatInvalid; + if (Args::StringToFormat (value.c_str(), format, NULL).Success()) + reg_info.format = format; + else if (value.compare("binary") == 0) + reg_info.format = eFormatBinary; + else if (value.compare("decimal") == 0) + reg_info.format = eFormatDecimal; + else if (value.compare("hex") == 0) + reg_info.format = eFormatHex; + else if (value.compare("float") == 0) + reg_info.format = eFormatFloat; + else if (value.compare("vector-sint8") == 0) + reg_info.format = eFormatVectorOfSInt8; + else if (value.compare("vector-uint8") == 0) + reg_info.format = eFormatVectorOfUInt8; + else if (value.compare("vector-sint16") == 0) + reg_info.format = eFormatVectorOfSInt16; + else if (value.compare("vector-uint16") == 0) + reg_info.format = eFormatVectorOfUInt16; + else if (value.compare("vector-sint32") == 0) + reg_info.format = eFormatVectorOfSInt32; + else if (value.compare("vector-uint32") == 0) + reg_info.format = eFormatVectorOfUInt32; + else if (value.compare("vector-float32") == 0) + reg_info.format = eFormatVectorOfFloat32; + else if (value.compare("vector-uint128") == 0) + reg_info.format = eFormatVectorOfUInt128; + } + else if (name.compare("set") == 0) + { + set_name.SetCString(value.c_str()); + } + else if (name.compare("gcc") == 0) + { + reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); + } + else if (name.compare("dwarf") == 0) + { + reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); + } + else if (name.compare("generic") == 0) + { + reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str()); + } + else if (name.compare("container-regs") == 0) + { + std::pair<llvm::StringRef, llvm::StringRef> value_pair; + value_pair.second = value; + do + { + value_pair = value_pair.second.split(','); + if (!value_pair.first.empty()) + { + uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16); + if (reg != LLDB_INVALID_REGNUM) + value_regs.push_back (reg); + } + } while (!value_pair.second.empty()); + } + else if (name.compare("invalidate-regs") == 0) + { + std::pair<llvm::StringRef, llvm::StringRef> value_pair; + value_pair.second = value; + do + { + value_pair = value_pair.second.split(','); + if (!value_pair.first.empty()) + { + uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16); + if (reg != LLDB_INVALID_REGNUM) + invalidate_regs.push_back (reg); + } + } while (!value_pair.second.empty()); + } + } + + reg_info.byte_offset = reg_offset; + assert (reg_info.byte_size != 0); + reg_offset += reg_info.byte_size; + if (!value_regs.empty()) + { + value_regs.push_back(LLDB_INVALID_REGNUM); + reg_info.value_regs = value_regs.data(); + } + if (!invalidate_regs.empty()) + { + invalidate_regs.push_back(LLDB_INVALID_REGNUM); + reg_info.invalidate_regs = invalidate_regs.data(); + } + + m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); + } + } + else + { + break; + } + } + + // We didn't get anything if the accumulated reg_num is zero. See if we are + // debugging ARM and fill with a hard coded register set until we can get an + // updated debugserver down on the devices. + // On the other hand, if the accumulated reg_num is positive, see if we can + // add composite registers to the existing primordial ones. + bool from_scratch = (reg_num == 0); + + const ArchSpec &target_arch = GetTarget().GetArchitecture(); + const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); + const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); + + // Use the process' architecture instead of the host arch, if available + ArchSpec remote_arch; + if (remote_process_arch.IsValid ()) + remote_arch = remote_process_arch; + else + remote_arch = remote_host_arch; + + if (!target_arch.IsValid()) + { + if (remote_arch.IsValid() + && remote_arch.GetMachine() == llvm::Triple::arm + && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) + m_register_info.HardcodeARMRegisters(from_scratch); + } + else if (target_arch.GetMachine() == llvm::Triple::arm) + { + m_register_info.HardcodeARMRegisters(from_scratch); + } + + // At this point, we can finalize our register info. + m_register_info.Finalize (); +} + +Error +ProcessGDBRemote::WillLaunch (Module* module) +{ + return WillLaunchOrAttach (); +} + +Error +ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid) +{ + return WillLaunchOrAttach (); +} + +Error +ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) +{ + return WillLaunchOrAttach (); +} + +Error +ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url) +{ + Error error (WillLaunchOrAttach ()); + + if (error.Fail()) + return error; + + error = ConnectToDebugserver (remote_url); + + if (error.Fail()) + return error; + StartAsyncThread (); + + lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); + if (pid == LLDB_INVALID_PROCESS_ID) + { + // We don't have a valid process ID, so note that we are connected + // and could now request to launch or attach, or get remote process + // listings... + SetPrivateState (eStateConnected); + } + else + { + // We have a valid process + SetID (pid); + GetThreadList(); + if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false)) + { + const StateType state = SetThreadStopInfo (m_last_stop_packet); + if (state == eStateStopped) + { + SetPrivateState (state); + } + else + error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state)); + } + else + error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url); + } + + if (error.Success() + && !GetTarget().GetArchitecture().IsValid() + && m_gdb_comm.GetHostArchitecture().IsValid()) + { + // Prefer the *process'* architecture over that of the *host*, if available. + if (m_gdb_comm.GetProcessArchitecture().IsValid()) + GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); + else + GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); + } + + return error; +} + +Error +ProcessGDBRemote::WillLaunchOrAttach () +{ + Error error; + m_stdio_communication.Clear (); + return error; +} + +//---------------------------------------------------------------------- +// Process Control +//---------------------------------------------------------------------- +Error +ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info) +{ + Error error; + + uint32_t launch_flags = launch_info.GetFlags().Get(); + const char *stdin_path = NULL; + const char *stdout_path = NULL; + const char *stderr_path = NULL; + const char *working_dir = launch_info.GetWorkingDirectory(); + + const ProcessLaunchInfo::FileAction *file_action; + file_action = launch_info.GetFileActionForFD (STDIN_FILENO); + if (file_action) + { + if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) + stdin_path = file_action->GetPath(); + } + file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); + if (file_action) + { + if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) + stdout_path = file_action->GetPath(); + } + file_action = launch_info.GetFileActionForFD (STDERR_FILENO); + if (file_action) + { + if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) + stderr_path = file_action->GetPath(); + } + + // ::LogSetBitMask (GDBR_LOG_DEFAULT); + // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); + // ::LogSetLogFile ("/dev/stdout"); + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + + ObjectFile * object_file = exe_module->GetObjectFile(); + if (object_file) + { + char host_port[128]; + snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ()); + char connect_url[128]; + snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port); + + // Make sure we aren't already connected? + if (!m_gdb_comm.IsConnected()) + { + error = StartDebugserverProcess (host_port, launch_info); + if (error.Fail()) + { + if (log) + log->Printf("failed to start debugserver process: %s", error.AsCString()); + return error; + } + + error = ConnectToDebugserver (connect_url); + } + + if (error.Success()) + { + lldb_utility::PseudoTerminal pty; + const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; + + // If the debugserver is local and we aren't disabling STDIO, lets use + // a pseudo terminal to instead of relying on the 'O' packets for stdio + // since 'O' packets can really slow down debugging if the inferior + // does a lot of output. + PlatformSP platform_sp (m_target.GetPlatform()); + if (platform_sp && platform_sp->IsHost() && !disable_stdio) + { + const char *slave_name = NULL; + if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL) + { + if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0)) + slave_name = pty.GetSlaveName (NULL, 0); + } + if (stdin_path == NULL) + stdin_path = slave_name; + + if (stdout_path == NULL) + stdout_path = slave_name; + + if (stderr_path == NULL) + stderr_path = slave_name; + } + + // Set STDIN to /dev/null if we want STDIO disabled or if either + // STDOUT or STDERR have been set to something and STDIN hasn't + if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path))) + stdin_path = "/dev/null"; + + // Set STDOUT to /dev/null if we want STDIO disabled or if either + // STDIN or STDERR have been set to something and STDOUT hasn't + if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path))) + stdout_path = "/dev/null"; + + // Set STDERR to /dev/null if we want STDIO disabled or if either + // STDIN or STDOUT have been set to something and STDERR hasn't + if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path))) + stderr_path = "/dev/null"; + + if (stdin_path) + m_gdb_comm.SetSTDIN (stdin_path); + if (stdout_path) + m_gdb_comm.SetSTDOUT (stdout_path); + if (stderr_path) + m_gdb_comm.SetSTDERR (stderr_path); + + m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR); + + m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName()); + + if (working_dir && working_dir[0]) + { + m_gdb_comm.SetWorkingDir (working_dir); + } + + // Send the environment and the program + arguments after we connect + const Args &environment = launch_info.GetEnvironmentEntries(); + if (environment.GetArgumentCount()) + { + size_t num_environment_entries = environment.GetArgumentCount(); + for (size_t i=0; i<num_environment_entries; ++i) + { + const char *env_entry = environment.GetArgumentAtIndex(i); + if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) + break; + } + } + + const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10); + int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector()); + if (arg_packet_err == 0) + { + std::string error_str; + if (m_gdb_comm.GetLaunchSuccess (error_str)) + { + SetID (m_gdb_comm.GetCurrentProcessID ()); + } + else + { + error.SetErrorString (error_str.c_str()); + } + } + else + { + error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); + } + + m_gdb_comm.SetPacketTimeout (old_packet_timeout); + + if (GetID() == LLDB_INVALID_PROCESS_ID) + { + if (log) + log->Printf("failed to connect to debugserver: %s", error.AsCString()); + KillDebugserverProcess (); + return error; + } + + if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false)) + { + SetPrivateState (SetThreadStopInfo (m_last_stop_packet)); + + if (!disable_stdio) + { + if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) + SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor()); + } + } + } + else + { + if (log) + log->Printf("failed to connect to debugserver: %s", error.AsCString()); + } + } + else + { + // Set our user ID to an invalid process ID. + SetID(LLDB_INVALID_PROCESS_ID); + error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", + exe_module->GetFileSpec().GetFilename().AsCString(), + exe_module->GetArchitecture().GetArchitectureName()); + } + return error; + +} + + +Error +ProcessGDBRemote::ConnectToDebugserver (const char *connect_url) +{ + Error error; + // Sleep and wait a bit for debugserver to start to listen... + std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); + if (conn_ap.get()) + { + const uint32_t max_retry_count = 50; + uint32_t retry_count = 0; + while (!m_gdb_comm.IsConnected()) + { + if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) + { + m_gdb_comm.SetConnection (conn_ap.release()); + break; + } + else if (error.WasInterrupted()) + { + // If we were interrupted, don't keep retrying. + break; + } + + retry_count++; + + if (retry_count >= max_retry_count) + break; + + usleep (100000); + } + } + + if (!m_gdb_comm.IsConnected()) + { + if (error.Success()) + error.SetErrorString("not connected to remote gdb server"); + return error; + } + + // We always seem to be able to open a connection to a local port + // so we need to make sure we can then send data to it. If we can't + // then we aren't actually connected to anything, so try and do the + // handshake with the remote GDB server and make sure that goes + // alright. + if (!m_gdb_comm.HandshakeWithServer (NULL)) + { + m_gdb_comm.Disconnect(); + if (error.Success()) + error.SetErrorString("not connected to remote gdb server"); + return error; + } + m_gdb_comm.ResetDiscoverableSettings(); + m_gdb_comm.QueryNoAckModeSupported (); + m_gdb_comm.GetThreadSuffixSupported (); + m_gdb_comm.GetListThreadsInStopReplySupported (); + m_gdb_comm.GetHostInfo (); + m_gdb_comm.GetVContSupported ('c'); + m_gdb_comm.GetVAttachOrWaitSupported(); + + size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); + for (size_t idx = 0; idx < num_cmds; idx++) + { + StringExtractorGDBRemote response; + m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false); + } + return error; +} + +void +ProcessGDBRemote::DidLaunchOrAttach () +{ + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::DidLaunch()"); + if (GetID() != LLDB_INVALID_PROCESS_ID) + { + m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS; + + BuildDynamicRegisterInfo (false); + + // See if the GDB server supports the qHostInfo information + + ArchSpec gdb_remote_arch = m_gdb_comm.GetHostArchitecture(); + + // See if the GDB server supports the qProcessInfo packet, if so + // prefer that over the Host information as it will be more specific + // to our process. + + if (m_gdb_comm.GetProcessArchitecture().IsValid()) + gdb_remote_arch = m_gdb_comm.GetProcessArchitecture(); + + if (gdb_remote_arch.IsValid()) + { + ArchSpec &target_arch = GetTarget().GetArchitecture(); + + if (target_arch.IsValid()) + { + // If the remote host is ARM and we have apple as the vendor, then + // ARM executables and shared libraries can have mixed ARM architectures. + // You can have an armv6 executable, and if the host is armv7, then the + // system will load the best possible architecture for all shared libraries + // it has, so we really need to take the remote host architecture as our + // defacto architecture in this case. + + if (gdb_remote_arch.GetMachine() == llvm::Triple::arm && + gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) + { + target_arch = gdb_remote_arch; + } + else + { + // Fill in what is missing in the triple + const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple(); + llvm::Triple &target_triple = target_arch.GetTriple(); + if (target_triple.getVendorName().size() == 0) + { + target_triple.setVendor (remote_triple.getVendor()); + + if (target_triple.getOSName().size() == 0) + { + target_triple.setOS (remote_triple.getOS()); + + if (target_triple.getEnvironmentName().size() == 0) + target_triple.setEnvironment (remote_triple.getEnvironment()); + } + } + } + } + else + { + // The target doesn't have a valid architecture yet, set it from + // the architecture we got from the remote GDB server + target_arch = gdb_remote_arch; + } + } + } +} + +void +ProcessGDBRemote::DidLaunch () +{ + DidLaunchOrAttach (); +} + +Error +ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid) +{ + ProcessAttachInfo attach_info; + return DoAttachToProcessWithID(attach_pid, attach_info); +} + +Error +ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) +{ + Error error; + // Clear out and clean up from any current state + Clear(); + if (attach_pid != LLDB_INVALID_PROCESS_ID) + { + // Make sure we aren't already connected? + if (!m_gdb_comm.IsConnected()) + { + char host_port[128]; + snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ()); + char connect_url[128]; + snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port); + + error = StartDebugserverProcess (host_port, attach_info); + + if (error.Fail()) + { + const char *error_string = error.AsCString(); + if (error_string == NULL) + error_string = "unable to launch " DEBUGSERVER_BASENAME; + + SetExitStatus (-1, error_string); + } + else + { + error = ConnectToDebugserver (connect_url); + } + } + + if (error.Success()) + { + char packet[64]; + const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); + SetID (attach_pid); + m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len)); + } + } + return error; +} + +size_t +ProcessGDBRemote::AttachInputReaderCallback +( + void *baton, + InputReader *reader, + lldb::InputReaderAction notification, + const char *bytes, + size_t bytes_len +) +{ + if (notification == eInputReaderGotToken) + { + ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton; + if (gdb_process->m_waiting_for_attach) + gdb_process->m_waiting_for_attach = false; + reader->SetIsDone(true); + return 1; + } + return 0; +} + +Error +ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) +{ + Error error; + // Clear out and clean up from any current state + Clear(); + + if (process_name && process_name[0]) + { + // Make sure we aren't already connected? + if (!m_gdb_comm.IsConnected()) + { + char host_port[128]; + snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ()); + char connect_url[128]; + snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port); + + error = StartDebugserverProcess (host_port, attach_info); + if (error.Fail()) + { + const char *error_string = error.AsCString(); + if (error_string == NULL) + error_string = "unable to launch " DEBUGSERVER_BASENAME; + + SetExitStatus (-1, error_string); + } + else + { + error = ConnectToDebugserver (connect_url); + } + } + + if (error.Success()) + { + StreamString packet; + + if (wait_for_launch) + { + if (!m_gdb_comm.GetVAttachOrWaitSupported()) + { + packet.PutCString ("vAttachWait"); + } + else + { + if (attach_info.GetIgnoreExisting()) + packet.PutCString("vAttachWait"); + else + packet.PutCString ("vAttachOrWait"); + } + } + else + packet.PutCString("vAttachName"); + packet.PutChar(';'); + packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); + + m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize())); + + } + } + return error; +} + + +void +ProcessGDBRemote::DidAttach () +{ + DidLaunchOrAttach (); +} + + +Error +ProcessGDBRemote::WillResume () +{ + m_continue_c_tids.clear(); + m_continue_C_tids.clear(); + m_continue_s_tids.clear(); + m_continue_S_tids.clear(); + return Error(); +} + +Error +ProcessGDBRemote::DoResume () +{ + Error error; + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::Resume()"); + + Listener listener ("gdb-remote.resume-packet-sent"); + if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) + { + listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); + + const size_t num_threads = GetThreadList().GetSize(); + + StreamString continue_packet; + bool continue_packet_error = false; + if (m_gdb_comm.HasAnyVContSupport ()) + { + if (m_continue_c_tids.size() == num_threads) + { + // All threads are continuing, just send a "c" packet + continue_packet.PutCString ("c"); + } + else + { + continue_packet.PutCString ("vCont"); + + if (!m_continue_c_tids.empty()) + { + if (m_gdb_comm.GetVContSupported ('c')) + { + for (tid_collection::const_iterator t_pos = m_continue_c_tids.begin(), t_end = m_continue_c_tids.end(); t_pos != t_end; ++t_pos) + continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); + } + else + continue_packet_error = true; + } + + if (!continue_packet_error && !m_continue_C_tids.empty()) + { + if (m_gdb_comm.GetVContSupported ('C')) + { + for (tid_sig_collection::const_iterator s_pos = m_continue_C_tids.begin(), s_end = m_continue_C_tids.end(); s_pos != s_end; ++s_pos) + continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); + } + else + continue_packet_error = true; + } + + if (!continue_packet_error && !m_continue_s_tids.empty()) + { + if (m_gdb_comm.GetVContSupported ('s')) + { + for (tid_collection::const_iterator t_pos = m_continue_s_tids.begin(), t_end = m_continue_s_tids.end(); t_pos != t_end; ++t_pos) + continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); + } + else + continue_packet_error = true; + } + + if (!continue_packet_error && !m_continue_S_tids.empty()) + { + if (m_gdb_comm.GetVContSupported ('S')) + { + for (tid_sig_collection::const_iterator s_pos = m_continue_S_tids.begin(), s_end = m_continue_S_tids.end(); s_pos != s_end; ++s_pos) + continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); + } + else + continue_packet_error = true; + } + + if (continue_packet_error) + continue_packet.GetString().clear(); + } + } + else + continue_packet_error = true; + + if (continue_packet_error) + { + // Either no vCont support, or we tried to use part of the vCont + // packet that wasn't supported by the remote GDB server. + // We need to try and make a simple packet that can do our continue + const size_t num_continue_c_tids = m_continue_c_tids.size(); + const size_t num_continue_C_tids = m_continue_C_tids.size(); + const size_t num_continue_s_tids = m_continue_s_tids.size(); + const size_t num_continue_S_tids = m_continue_S_tids.size(); + if (num_continue_c_tids > 0) + { + if (num_continue_c_tids == num_threads) + { + // All threads are resuming... + m_gdb_comm.SetCurrentThreadForRun (-1); + continue_packet.PutChar ('c'); + continue_packet_error = false; + } + else if (num_continue_c_tids == 1 && + num_continue_C_tids == 0 && + num_continue_s_tids == 0 && + num_continue_S_tids == 0 ) + { + // Only one thread is continuing + m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); + continue_packet.PutChar ('c'); + continue_packet_error = false; + } + } + + if (continue_packet_error && num_continue_C_tids > 0) + { + if ((num_continue_C_tids + num_continue_c_tids) == num_threads && + num_continue_C_tids > 0 && + num_continue_s_tids == 0 && + num_continue_S_tids == 0 ) + { + const int continue_signo = m_continue_C_tids.front().second; + // Only one thread is continuing + if (num_continue_C_tids > 1) + { + // More that one thread with a signal, yet we don't have + // vCont support and we are being asked to resume each + // thread with a signal, we need to make sure they are + // all the same signal, or we can't issue the continue + // accurately with the current support... + if (num_continue_C_tids > 1) + { + continue_packet_error = false; + for (size_t i=1; i<m_continue_C_tids.size(); ++i) + { + if (m_continue_C_tids[i].second != continue_signo) + continue_packet_error = true; + } + } + if (!continue_packet_error) + m_gdb_comm.SetCurrentThreadForRun (-1); + } + else + { + // Set the continue thread ID + continue_packet_error = false; + m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first); + } + if (!continue_packet_error) + { + // Add threads continuing with the same signo... + continue_packet.Printf("C%2.2x", continue_signo); + } + } + } + + if (continue_packet_error && num_continue_s_tids > 0) + { + if (num_continue_s_tids == num_threads) + { + // All threads are resuming... + m_gdb_comm.SetCurrentThreadForRun (-1); + continue_packet.PutChar ('s'); + continue_packet_error = false; + } + else if (num_continue_c_tids == 0 && + num_continue_C_tids == 0 && + num_continue_s_tids == 1 && + num_continue_S_tids == 0 ) + { + // Only one thread is stepping + m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); + continue_packet.PutChar ('s'); + continue_packet_error = false; + } + } + + if (!continue_packet_error && num_continue_S_tids > 0) + { + if (num_continue_S_tids == num_threads) + { + const int step_signo = m_continue_S_tids.front().second; + // Are all threads trying to step with the same signal? + continue_packet_error = false; + if (num_continue_S_tids > 1) + { + for (size_t i=1; i<num_threads; ++i) + { + if (m_continue_S_tids[i].second != step_signo) + continue_packet_error = true; + } + } + if (!continue_packet_error) + { + // Add threads stepping with the same signo... + m_gdb_comm.SetCurrentThreadForRun (-1); + continue_packet.Printf("S%2.2x", step_signo); + } + } + else if (num_continue_c_tids == 0 && + num_continue_C_tids == 0 && + num_continue_s_tids == 0 && + num_continue_S_tids == 1 ) + { + // Only one thread is stepping with signal + m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first); + continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); + continue_packet_error = false; + } + } + } + + if (continue_packet_error) + { + error.SetErrorString ("can't make continue packet for this resume"); + } + else + { + EventSP event_sp; + TimeValue timeout; + timeout = TimeValue::Now(); + timeout.OffsetWithSeconds (5); + if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) + { + error.SetErrorString ("Trying to resume but the async thread is dead."); + if (log) + log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead."); + return error; + } + + m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize())); + + if (listener.WaitForEvent (&timeout, event_sp) == false) + { + error.SetErrorString("Resume timed out."); + if (log) + log->Printf ("ProcessGDBRemote::DoResume: Resume timed out."); + } + else if (event_sp->BroadcasterIs (&m_async_broadcaster)) + { + error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back."); + if (log) + log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back."); + return error; + } + } + } + + return error; +} + +void +ProcessGDBRemote::ClearThreadIDList () +{ + Mutex::Locker locker(m_thread_list_real.GetMutex()); + m_thread_ids.clear(); +} + +bool +ProcessGDBRemote::UpdateThreadIDList () +{ + Mutex::Locker locker(m_thread_list_real.GetMutex()); + bool sequence_mutex_unavailable = false; + m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable); + if (sequence_mutex_unavailable) + { + return false; // We just didn't get the list + } + return true; +} + +bool +ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) +{ + // locker will keep a mutex locked until it goes out of scope + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); + if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) + log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); + + size_t num_thread_ids = m_thread_ids.size(); + // The "m_thread_ids" thread ID list should always be updated after each stop + // reply packet, but in case it isn't, update it here. + if (num_thread_ids == 0) + { + if (!UpdateThreadIDList ()) + return false; + num_thread_ids = m_thread_ids.size(); + } + + ThreadList old_thread_list_copy(old_thread_list); + if (num_thread_ids > 0) + { + for (size_t i=0; i<num_thread_ids; ++i) + { + tid_t tid = m_thread_ids[i]; + ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); + if (!thread_sp) + { + thread_sp.reset (new ThreadGDBRemote (*this, tid)); + if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) + log->Printf( + "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n", + __FUNCTION__, + thread_sp.get(), + thread_sp->GetID()); + } + else + { + if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) + log->Printf( + "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n", + __FUNCTION__, + thread_sp.get(), + thread_sp->GetID()); + } + new_thread_list.AddThread(thread_sp); + } + } + + // Whatever that is left in old_thread_list_copy are not + // present in new_thread_list. Remove non-existent threads from internal id table. + size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); + for (size_t i=0; i<old_num_thread_ids; i++) + { + ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false)); + if (old_thread_sp) + { + lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); + m_thread_id_to_index_id_map.erase(old_thread_id); + } + } + + return true; +} + + +StateType +ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet) +{ + stop_packet.SetFilePos (0); + const char stop_type = stop_packet.GetChar(); + switch (stop_type) + { + case 'T': + case 'S': + { + // This is a bit of a hack, but is is required. If we did exec, we + // need to clear our thread lists and also know to rebuild our dynamic + // register info before we lookup and threads and populate the expedited + // register values so we need to know this right away so we can cleanup + // and update our registers. + const uint32_t stop_id = GetStopID(); + if (stop_id == 0) + { + // Our first stop, make sure we have a process ID, and also make + // sure we know about our registers + if (GetID() == LLDB_INVALID_PROCESS_ID) + { + lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); + if (pid != LLDB_INVALID_PROCESS_ID) + SetID (pid); + } + BuildDynamicRegisterInfo (true); + } + // Stop with signal and thread info + const uint8_t signo = stop_packet.GetHexU8(); + std::string name; + std::string value; + std::string thread_name; + std::string reason; + std::string description; + uint32_t exc_type = 0; + std::vector<addr_t> exc_data; + addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; + ThreadSP thread_sp; + ThreadGDBRemote *gdb_thread = NULL; + + while (stop_packet.GetNameColonValue(name, value)) + { + if (name.compare("metype") == 0) + { + // exception type in big endian hex + exc_type = Args::StringToUInt32 (value.c_str(), 0, 16); + } + else if (name.compare("medata") == 0) + { + // exception data in big endian hex + exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16)); + } + else if (name.compare("thread") == 0) + { + // thread in big endian hex + lldb::tid_t tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); + // m_thread_list_real does have its own mutex, but we need to + // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...) + // and the m_thread_list_real.AddThread(...) so it doesn't change on us + Mutex::Locker locker (m_thread_list_real.GetMutex ()); + thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); + + if (!thread_sp) + { + // Create the thread if we need to + thread_sp.reset (new ThreadGDBRemote (*this, tid)); + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); + if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) + log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n", + __FUNCTION__, + thread_sp.get(), + thread_sp->GetID()); + + m_thread_list_real.AddThread(thread_sp); + } + gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); + + } + else if (name.compare("threads") == 0) + { + Mutex::Locker locker(m_thread_list_real.GetMutex()); + m_thread_ids.clear(); + // A comma separated list of all threads in the current + // process that includes the thread for this stop reply + // packet + size_t comma_pos; + lldb::tid_t tid; + while ((comma_pos = value.find(',')) != std::string::npos) + { + value[comma_pos] = '\0'; + // thread in big endian hex + tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); + if (tid != LLDB_INVALID_THREAD_ID) + m_thread_ids.push_back (tid); + value.erase(0, comma_pos + 1); + + } + tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); + if (tid != LLDB_INVALID_THREAD_ID) + m_thread_ids.push_back (tid); + } + else if (name.compare("hexname") == 0) + { + StringExtractor name_extractor; + // Swap "value" over into "name_extractor" + name_extractor.GetStringRef().swap(value); + // Now convert the HEX bytes into a string value + name_extractor.GetHexByteString (value); + thread_name.swap (value); + } + else if (name.compare("name") == 0) + { + thread_name.swap (value); + } + else if (name.compare("qaddr") == 0) + { + thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16); + } + else if (name.compare("reason") == 0) + { + reason.swap(value); + } + else if (name.compare("description") == 0) + { + StringExtractor desc_extractor; + // Swap "value" over into "name_extractor" + desc_extractor.GetStringRef().swap(value); + // Now convert the HEX bytes into a string value + desc_extractor.GetHexByteString (thread_name); + } + else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1])) + { + // We have a register number that contains an expedited + // register value. Lets supply this register to our thread + // so it won't have to go and read it. + if (gdb_thread) + { + uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16); + + if (reg != UINT32_MAX) + { + StringExtractor reg_value_extractor; + // Swap "value" over into "reg_value_extractor" + reg_value_extractor.GetStringRef().swap(value); + if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor)) + { + Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'", + name.c_str(), + reg, + reg, + reg_value_extractor.GetStringRef().c_str(), + stop_packet.GetStringRef().c_str()); + } + } + } + } + } + + if (thread_sp) + { + // Clear the stop info just in case we don't set it to anything + thread_sp->SetStopInfo (StopInfoSP()); + + gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr); + gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str()); + if (exc_type != 0) + { + const size_t exc_data_size = exc_data.size(); + + thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp, + exc_type, + exc_data_size, + exc_data_size >= 1 ? exc_data[0] : 0, + exc_data_size >= 2 ? exc_data[1] : 0, + exc_data_size >= 3 ? exc_data[2] : 0)); + } + else + { + bool handled = false; + bool did_exec = false; + if (!reason.empty()) + { + if (reason.compare("trace") == 0) + { + thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); + handled = true; + } + else if (reason.compare("breakpoint") == 0) + { + addr_t pc = thread_sp->GetRegisterContext()->GetPC(); + lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); + if (bp_site_sp) + { + // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, + // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that + // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. + handled = true; + if (bp_site_sp->ValidForThisThread (thread_sp.get())) + { + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); + } + else + { + StopInfoSP invalid_stop_info_sp; + thread_sp->SetStopInfo (invalid_stop_info_sp); + } + } + + } + else if (reason.compare("trap") == 0) + { + // Let the trap just use the standard signal stop reason below... + } + else if (reason.compare("watchpoint") == 0) + { + break_id_t watch_id = LLDB_INVALID_WATCH_ID; + // TODO: locate the watchpoint somehow... + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id)); + handled = true; + } + else if (reason.compare("exception") == 0) + { + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str())); + handled = true; + } + else if (reason.compare("exec") == 0) + { + did_exec = true; + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp)); + handled = true; + } + } + + if (signo && did_exec == false) + { + if (signo == SIGTRAP) + { + // Currently we are going to assume SIGTRAP means we are either + // hitting a breakpoint or hardware single stepping. + handled = true; + addr_t pc = thread_sp->GetRegisterContext()->GetPC(); + lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); + + if (bp_site_sp) + { + // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, + // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that + // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. + if (bp_site_sp->ValidForThisThread (thread_sp.get())) + { + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); + } + else + { + StopInfoSP invalid_stop_info_sp; + thread_sp->SetStopInfo (invalid_stop_info_sp); + } + } + else + { + // If we were stepping then assume the stop was the result of the trace. If we were + // not stepping then report the SIGTRAP. + // FIXME: We are still missing the case where we single step over a trap instruction. + if (thread_sp->GetTemporaryResumeState() == eStateStepping) + thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); + else + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo)); + } + } + if (!handled) + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo)); + } + + if (!description.empty()) + { + lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); + if (stop_info_sp) + { + stop_info_sp->SetDescription (description.c_str()); + } + else + { + thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str())); + } + } + } + } + return eStateStopped; + } + break; + + case 'W': + // process exited + return eStateExited; + + default: + break; + } + return eStateInvalid; +} + +void +ProcessGDBRemote::RefreshStateAfterStop () +{ + Mutex::Locker locker(m_thread_list_real.GetMutex()); + m_thread_ids.clear(); + // Set the thread stop info. It might have a "threads" key whose value is + // a list of all thread IDs in the current process, so m_thread_ids might + // get set. + SetThreadStopInfo (m_last_stop_packet); + // Check to see if SetThreadStopInfo() filled in m_thread_ids? + if (m_thread_ids.empty()) + { + // No, we need to fetch the thread list manually + UpdateThreadIDList(); + } + + // Let all threads recover from stopping and do any clean up based + // on the previous thread state (if any). + m_thread_list_real.RefreshStateAfterStop(); + +} + +Error +ProcessGDBRemote::DoHalt (bool &caused_stop) +{ + Error error; + + bool timed_out = false; + Mutex::Locker locker; + + if (m_public_state.GetValue() == eStateAttaching) + { + // We are being asked to halt during an attach. We need to just close + // our file handle and debugserver will go away, and we can be done... + m_gdb_comm.Disconnect(); + } + else + { + if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out)) + { + if (timed_out) + error.SetErrorString("timed out sending interrupt packet"); + else + error.SetErrorString("unknown error sending interrupt packet"); + } + + caused_stop = m_gdb_comm.GetInterruptWasSent (); + } + return error; +} + +Error +ProcessGDBRemote::DoDetach(bool keep_stopped) +{ + Error error; + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); + + DisableAllBreakpointSites (); + + m_thread_list.DiscardThreadPlans(); + + error = m_gdb_comm.Detach (keep_stopped); + if (log) + { + if (error.Success()) + log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully"); + else + log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>"); + } + + if (!error.Success()) + return error; + + // Sleep for one second to let the process get all detached... + StopAsyncThread (); + + SetPrivateState (eStateDetached); + ResumePrivateStateThread(); + + //KillDebugserverProcess (); + return error; +} + + +Error +ProcessGDBRemote::DoDestroy () +{ + Error error; + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::DoDestroy()"); + + // There is a bug in older iOS debugservers where they don't shut down the process + // they are debugging properly. If the process is sitting at a breakpoint or an exception, + // this can cause problems with restarting. So we check to see if any of our threads are stopped + // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN + // destroy it again. + // + // Note, we don't have a good way to test the version of debugserver, but I happen to know that + // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of + // the debugservers with this bug are equal. There really should be a better way to test this! + // + // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and + // get called here to destroy again and we're still at a breakpoint or exception, then we should + // just do the straight-forward kill. + // + // And of course, if we weren't able to stop the process by the time we get here, it isn't + // necessary (or helpful) to do any of this. + + if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning) + { + PlatformSP platform_sp = GetTarget().GetPlatform(); + + // FIXME: These should be ConstStrings so we aren't doing strcmp'ing. + if (platform_sp + && platform_sp->GetName() + && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic()) + { + if (m_destroy_tried_resuming) + { + if (log) + log->PutCString ("ProcessGDBRemote::DoDestroy()Tried resuming to destroy once already, not doing it again."); + } + else + { + // At present, the plans are discarded and the breakpoints disabled Process::Destroy, + // but we really need it to happen here and it doesn't matter if we do it twice. + m_thread_list.DiscardThreadPlans(); + DisableAllBreakpointSites(); + + bool stop_looks_like_crash = false; + ThreadList &threads = GetThreadList(); + + { + Mutex::Locker locker(threads.GetMutex()); + + size_t num_threads = threads.GetSize(); + for (size_t i = 0; i < num_threads; i++) + { + ThreadSP thread_sp = threads.GetThreadAtIndex(i); + StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); + StopReason reason = eStopReasonInvalid; + if (stop_info_sp) + reason = stop_info_sp->GetStopReason(); + if (reason == eStopReasonBreakpoint + || reason == eStopReasonException) + { + if (log) + log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.", + thread_sp->GetProtocolID(), + stop_info_sp->GetDescription()); + stop_looks_like_crash = true; + break; + } + } + } + + if (stop_looks_like_crash) + { + if (log) + log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill."); + m_destroy_tried_resuming = true; + + // If we are going to run again before killing, it would be good to suspend all the threads + // before resuming so they won't get into more trouble. Sadly, for the threads stopped with + // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do + // have to run the risk of letting those threads proceed a bit. + + { + Mutex::Locker locker(threads.GetMutex()); + + size_t num_threads = threads.GetSize(); + for (size_t i = 0; i < num_threads; i++) + { + ThreadSP thread_sp = threads.GetThreadAtIndex(i); + StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); + StopReason reason = eStopReasonInvalid; + if (stop_info_sp) + reason = stop_info_sp->GetStopReason(); + if (reason != eStopReasonBreakpoint + && reason != eStopReasonException) + { + if (log) + log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.", + thread_sp->GetProtocolID()); + thread_sp->SetResumeState(eStateSuspended); + } + } + } + Resume (); + return Destroy(); + } + } + } + } + + // Interrupt if our inferior is running... + int exit_status = SIGABRT; + std::string exit_string; + + if (m_gdb_comm.IsConnected()) + { + if (m_public_state.GetValue() != eStateAttaching) + { + + StringExtractorGDBRemote response; + bool send_async = true; + const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (3); + + if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async)) + { + char packet_cmd = response.GetChar(0); + + if (packet_cmd == 'W' || packet_cmd == 'X') + { + SetLastStopPacket (response); + ClearThreadIDList (); + exit_status = response.GetHexU8(); + } + else + { + if (log) + log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str()); + exit_string.assign("got unexpected response to k packet: "); + exit_string.append(response.GetStringRef()); + } + } + else + { + if (log) + log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); + exit_string.assign("failed to send the k packet"); + } + + m_gdb_comm.SetPacketTimeout(old_packet_timeout); + } + else + { + if (log) + log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); + exit_string.assign ("killed or interrupted while attaching."); + } + } + else + { + // If we missed setting the exit status on the way out, do it here. + // NB set exit status can be called multiple times, the first one sets the status. + exit_string.assign("destroying when not connected to debugserver"); + } + + SetExitStatus(exit_status, exit_string.c_str()); + + StopAsyncThread (); + KillDebugserverProcess (); + return error; +} + +void +ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response) +{ + lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex); + const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos; + if (did_exec) + { + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec"); + + m_thread_list_real.Clear(); + m_thread_list.Clear(); + BuildDynamicRegisterInfo (true); + m_gdb_comm.ResetDiscoverableSettings(); + } + m_last_stop_packet = response; +} + + +//------------------------------------------------------------------ +// Process Queries +//------------------------------------------------------------------ + +bool +ProcessGDBRemote::IsAlive () +{ + return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited; +} + +addr_t +ProcessGDBRemote::GetImageInfoAddress() +{ + return m_gdb_comm.GetShlibInfoAddr(); +} + +//------------------------------------------------------------------ +// Process Memory +//------------------------------------------------------------------ +size_t +ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) +{ + if (size > m_max_memory_size) + { + // Keep memory read sizes down to a sane limit. This function will be + // called multiple times in order to complete the task by + // lldb_private::Process so it is ok to do this. + size = m_max_memory_size; + } + + char packet[64]; + const int packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size); + assert (packet_len + 1 < (int)sizeof(packet)); + StringExtractorGDBRemote response; + if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true)) + { + if (response.IsNormalResponse()) + { + error.Clear(); + return response.GetHexBytes(buf, size, '\xdd'); + } + else if (response.IsErrorResponse()) + error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); + else if (response.IsUnsupportedResponse()) + error.SetErrorStringWithFormat("GDB server does not support reading memory"); + else + error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str()); + } + else + { + error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); + } + return 0; +} + +size_t +ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) +{ + if (size > m_max_memory_size) + { + // Keep memory read sizes down to a sane limit. This function will be + // called multiple times in order to complete the task by + // lldb_private::Process so it is ok to do this. + size = m_max_memory_size; + } + + StreamString packet; + packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); + packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); + StringExtractorGDBRemote response; + if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true)) + { + if (response.IsOKResponse()) + { + error.Clear(); + return size; + } + else if (response.IsErrorResponse()) + error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr); + else if (response.IsUnsupportedResponse()) + error.SetErrorStringWithFormat("GDB server does not support writing memory"); + else + error.SetErrorStringWithFormat("unexpected response to GDB server memory write packet '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str()); + } + else + { + error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str()); + } + return 0; +} + +lldb::addr_t +ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) +{ + addr_t allocated_addr = LLDB_INVALID_ADDRESS; + + LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); + switch (supported) + { + case eLazyBoolCalculate: + case eLazyBoolYes: + allocated_addr = m_gdb_comm.AllocateMemory (size, permissions); + if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes) + return allocated_addr; + + case eLazyBoolNo: + // Call mmap() to create memory in the inferior.. + unsigned prot = 0; + if (permissions & lldb::ePermissionsReadable) + prot |= eMmapProtRead; + if (permissions & lldb::ePermissionsWritable) + prot |= eMmapProtWrite; + if (permissions & lldb::ePermissionsExecutable) + prot |= eMmapProtExec; + + if (InferiorCallMmap(this, allocated_addr, 0, size, prot, + eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) + m_addr_to_mmap_size[allocated_addr] = size; + else + allocated_addr = LLDB_INVALID_ADDRESS; + break; + } + + if (allocated_addr == LLDB_INVALID_ADDRESS) + error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions)); + else + error.Clear(); + return allocated_addr; +} + +Error +ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr, + MemoryRegionInfo ®ion_info) +{ + + Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info)); + return error; +} + +Error +ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num) +{ + + Error error (m_gdb_comm.GetWatchpointSupportInfo (num)); + return error; +} + +Error +ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after) +{ + Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after)); + return error; +} + +Error +ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr) +{ + Error error; + LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); + + switch (supported) + { + case eLazyBoolCalculate: + // We should never be deallocating memory without allocating memory + // first so we should never get eLazyBoolCalculate + error.SetErrorString ("tried to deallocate memory without ever allocating memory"); + break; + + case eLazyBoolYes: + if (!m_gdb_comm.DeallocateMemory (addr)) + error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); + break; + + case eLazyBoolNo: + // Call munmap() to deallocate memory in the inferior.. + { + MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); + if (pos != m_addr_to_mmap_size.end() && + InferiorCallMunmap(this, addr, pos->second)) + m_addr_to_mmap_size.erase (pos); + else + error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); + } + break; + } + + return error; +} + + +//------------------------------------------------------------------ +// Process STDIO +//------------------------------------------------------------------ +size_t +ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error) +{ + if (m_stdio_communication.IsConnected()) + { + ConnectionStatus status; + m_stdio_communication.Write(src, src_len, status, NULL); + } + return 0; +} + +Error +ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site) +{ + Error error; + assert (bp_site != NULL); + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); + user_id_t site_id = bp_site->GetID(); + const addr_t addr = bp_site->GetLoadAddress(); + if (log) + log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr); + + if (bp_site->IsEnabled()) + { + if (log) + log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64 " -- SUCCESS (already enabled)", site_id, (uint64_t)addr); + return error; + } + else + { + const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); + + if (bp_site->HardwarePreferred()) + { + // Try and set hardware breakpoint, and if that fails, fall through + // and set a software breakpoint? + if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware)) + { + if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0) + { + bp_site->SetEnabled(true); + bp_site->SetType (BreakpointSite::eHardware); + return error; + } + } + } + + if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware)) + { + if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0) + { + bp_site->SetEnabled(true); + bp_site->SetType (BreakpointSite::eExternal); + return error; + } + } + + return EnableSoftwareBreakpoint (bp_site); + } + + if (log) + { + const char *err_string = error.AsCString(); + log->Printf ("ProcessGDBRemote::EnableBreakpointSite () error for breakpoint at 0x%8.8" PRIx64 ": %s", + bp_site->GetLoadAddress(), + err_string ? err_string : "NULL"); + } + // We shouldn't reach here on a successful breakpoint enable... + if (error.Success()) + error.SetErrorToGenericError(); + return error; +} + +Error +ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site) +{ + Error error; + assert (bp_site != NULL); + addr_t addr = bp_site->GetLoadAddress(); + user_id_t site_id = bp_site->GetID(); + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); + if (log) + log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr); + + if (bp_site->IsEnabled()) + { + const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); + + BreakpointSite::Type bp_type = bp_site->GetType(); + switch (bp_type) + { + case BreakpointSite::eSoftware: + error = DisableSoftwareBreakpoint (bp_site); + break; + + case BreakpointSite::eHardware: + if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size)) + error.SetErrorToGenericError(); + break; + + case BreakpointSite::eExternal: + if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size)) + error.SetErrorToGenericError(); + break; + } + if (error.Success()) + bp_site->SetEnabled(false); + } + else + { + if (log) + log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", site_id, (uint64_t)addr); + return error; + } + + if (error.Success()) + error.SetErrorToGenericError(); + return error; +} + +// Pre-requisite: wp != NULL. +static GDBStoppointType +GetGDBStoppointType (Watchpoint *wp) +{ + assert(wp); + bool watch_read = wp->WatchpointRead(); + bool watch_write = wp->WatchpointWrite(); + + // watch_read and watch_write cannot both be false. + assert(watch_read || watch_write); + if (watch_read && watch_write) + return eWatchpointReadWrite; + else if (watch_read) + return eWatchpointRead; + else // Must be watch_write, then. + return eWatchpointWrite; +} + +Error +ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify) +{ + Error error; + if (wp) + { + user_id_t watchID = wp->GetID(); + addr_t addr = wp->GetLoadAddress(); + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); + if (log) + log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID); + if (wp->IsEnabled()) + { + if (log) + log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", watchID, (uint64_t)addr); + return error; + } + + GDBStoppointType type = GetGDBStoppointType(wp); + // Pass down an appropriate z/Z packet... + if (m_gdb_comm.SupportsGDBStoppointPacket (type)) + { + if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0) + { + wp->SetEnabled(true, notify); + return error; + } + else + error.SetErrorString("sending gdb watchpoint packet failed"); + } + else + error.SetErrorString("watchpoints not supported"); + } + else + { + error.SetErrorString("Watchpoint argument was NULL."); + } + if (error.Success()) + error.SetErrorToGenericError(); + return error; +} + +Error +ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify) +{ + Error error; + if (wp) + { + user_id_t watchID = wp->GetID(); + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); + + addr_t addr = wp->GetLoadAddress(); + + if (log) + log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr); + + if (!wp->IsEnabled()) + { + if (log) + log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", watchID, (uint64_t)addr); + // See also 'class WatchpointSentry' within StopInfo.cpp. + // This disabling attempt might come from the user-supplied actions, we'll route it in order for + // the watchpoint object to intelligently process this action. + wp->SetEnabled(false, notify); + return error; + } + + if (wp->IsHardware()) + { + GDBStoppointType type = GetGDBStoppointType(wp); + // Pass down an appropriate z/Z packet... + if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0) + { + wp->SetEnabled(false, notify); + return error; + } + else + error.SetErrorString("sending gdb watchpoint packet failed"); + } + // TODO: clear software watchpoints if we implement them + } + else + { + error.SetErrorString("Watchpoint argument was NULL."); + } + if (error.Success()) + error.SetErrorToGenericError(); + return error; +} + +void +ProcessGDBRemote::Clear() +{ + m_flags = 0; + m_thread_list_real.Clear(); + m_thread_list.Clear(); +} + +Error +ProcessGDBRemote::DoSignal (int signo) +{ + Error error; + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo); + + if (!m_gdb_comm.SendAsyncSignal (signo)) + error.SetErrorStringWithFormat("failed to send signal %i", signo); + return error; +} + +Error +ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url) +{ + ProcessLaunchInfo launch_info; + return StartDebugserverProcess(debugserver_url, launch_info); +} + +Error +ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url, const ProcessInfo &process_info) // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...") +{ + Error error; + if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) + { + // If we locate debugserver, keep that located version around + static FileSpec g_debugserver_file_spec; + + ProcessLaunchInfo debugserver_launch_info; + char debugserver_path[PATH_MAX]; + FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile(); + + // Always check to see if we have an environment override for the path + // to the debugserver to use and use it if we do. + const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); + if (env_debugserver_path) + debugserver_file_spec.SetFile (env_debugserver_path, false); + else + debugserver_file_spec = g_debugserver_file_spec; + bool debugserver_exists = debugserver_file_spec.Exists(); + if (!debugserver_exists) + { + // The debugserver binary is in the LLDB.framework/Resources + // directory. + if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec)) + { + debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME); + debugserver_exists = debugserver_file_spec.Exists(); + if (debugserver_exists) + { + g_debugserver_file_spec = debugserver_file_spec; + } + else + { + g_debugserver_file_spec.Clear(); + debugserver_file_spec.Clear(); + } + } + } + + if (debugserver_exists) + { + debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); + + m_stdio_communication.Clear(); + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + + Args &debugserver_args = debugserver_launch_info.GetArguments(); + char arg_cstr[PATH_MAX]; + + // Start args with "debugserver /file/path -r --" + debugserver_args.AppendArgument(debugserver_path); + debugserver_args.AppendArgument(debugserver_url); + // use native registers, not the GDB registers + debugserver_args.AppendArgument("--native-regs"); + // make debugserver run in its own session so signals generated by + // special terminal key sequences (^C) don't affect debugserver + debugserver_args.AppendArgument("--setsid"); + + const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); + if (env_debugserver_log_file) + { + ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); + debugserver_args.AppendArgument(arg_cstr); + } + + const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); + if (env_debugserver_log_flags) + { + ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); + debugserver_args.AppendArgument(arg_cstr); + } +// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt"); +// debugserver_args.AppendArgument("--log-flags=0x802e0e"); + + // We currently send down all arguments, attach pids, or attach + // process names in dedicated GDB server packets, so we don't need + // to pass them as arguments. This is currently because of all the + // things we need to setup prior to launching: the environment, + // current working dir, file actions, etc. +#if 0 + // Now append the program arguments + if (inferior_argv) + { + // Terminate the debugserver args so we can now append the inferior args + debugserver_args.AppendArgument("--"); + + for (int i = 0; inferior_argv[i] != NULL; ++i) + debugserver_args.AppendArgument (inferior_argv[i]); + } + else if (attach_pid != LLDB_INVALID_PROCESS_ID) + { + ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid); + debugserver_args.AppendArgument (arg_cstr); + } + else if (attach_name && attach_name[0]) + { + if (wait_for_launch) + debugserver_args.AppendArgument ("--waitfor"); + else + debugserver_args.AppendArgument ("--attach"); + debugserver_args.AppendArgument (attach_name); + } +#endif + + ProcessLaunchInfo::FileAction file_action; + + // Close STDIN, STDOUT and STDERR. We might need to redirect them + // to "/dev/null" if we run into any problems. + file_action.Close (STDIN_FILENO); + debugserver_launch_info.AppendFileAction (file_action); + file_action.Close (STDOUT_FILENO); + debugserver_launch_info.AppendFileAction (file_action); + file_action.Close (STDERR_FILENO); + debugserver_launch_info.AppendFileAction (file_action); + + if (log) + { + StreamString strm; + debugserver_args.Dump (&strm); + log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData()); + } + + debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false); + debugserver_launch_info.SetUserID(process_info.GetUserID()); + + error = Host::LaunchProcess(debugserver_launch_info); + + if (error.Success ()) + m_debugserver_pid = debugserver_launch_info.GetProcessID(); + else + m_debugserver_pid = LLDB_INVALID_PROCESS_ID; + + if (error.Fail() || log) + error.PutToLog(log, "Host::LaunchProcess (launch_info) => pid=%" PRIu64 ", path='%s'", m_debugserver_pid, debugserver_path); + } + else + { + error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME); + } + + if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) + StartAsyncThread (); + } + return error; +} + +bool +ProcessGDBRemote::MonitorDebugserverProcess +( + void *callback_baton, + lldb::pid_t debugserver_pid, + bool exited, // True if the process did exit + int signo, // Zero for no signal + int exit_status // Exit value of process if signal is zero +) +{ + // The baton is a "ProcessGDBRemote *". Now this class might be gone + // and might not exist anymore, so we need to carefully try to get the + // target for this process first since we have a race condition when + // we are done running between getting the notice that the inferior + // process has died and the debugserver that was debugging this process. + // In our test suite, we are also continually running process after + // process, so we must be very careful to make sure: + // 1 - process object hasn't been deleted already + // 2 - that a new process object hasn't been recreated in its place + + // "debugserver_pid" argument passed in is the process ID for + // debugserver that we are tracking... + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + + ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton; + + // Get a shared pointer to the target that has a matching process pointer. + // This target could be gone, or the target could already have a new process + // object inside of it + TargetSP target_sp (Debugger::FindTargetWithProcess(process)); + + if (log) + log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status); + + if (target_sp) + { + // We found a process in a target that matches, but another thread + // might be in the process of launching a new process that will + // soon replace it, so get a shared pointer to the process so we + // can keep it alive. + ProcessSP process_sp (target_sp->GetProcessSP()); + // Now we have a shared pointer to the process that can't go away on us + // so we now make sure it was the same as the one passed in, and also make + // sure that our previous "process *" didn't get deleted and have a new + // "process *" created in its place with the same pointer. To verify this + // we make sure the process has our debugserver process ID. If we pass all + // of these tests, then we are sure that this process is the one we were + // looking for. + if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid) + { + // Sleep for a half a second to make sure our inferior process has + // time to set its exit status before we set it incorrectly when + // both the debugserver and the inferior process shut down. + usleep (500000); + // If our process hasn't yet exited, debugserver might have died. + // If the process did exit, the we are reaping it. + const StateType state = process->GetState(); + + if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID && + state != eStateInvalid && + state != eStateUnloaded && + state != eStateExited && + state != eStateDetached) + { + char error_str[1024]; + if (signo) + { + const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo); + if (signal_cstr) + ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); + else + ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo); + } + else + { + ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status); + } + + process->SetExitStatus (-1, error_str); + } + // Debugserver has exited we need to let our ProcessGDBRemote + // know that it no longer has a debugserver instance + process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; + } + } + return true; +} + +void +ProcessGDBRemote::KillDebugserverProcess () +{ + if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) + { + ::kill (m_debugserver_pid, SIGINT); + m_debugserver_pid = LLDB_INVALID_PROCESS_ID; + } +} + +void +ProcessGDBRemote::Initialize() +{ + static bool g_initialized = false; + + if (g_initialized == false) + { + g_initialized = true; + PluginManager::RegisterPlugin (GetPluginNameStatic(), + GetPluginDescriptionStatic(), + CreateInstance, + DebuggerInitialize); + + Log::Callbacks log_callbacks = { + ProcessGDBRemoteLog::DisableLog, + ProcessGDBRemoteLog::EnableLog, + ProcessGDBRemoteLog::ListLogCategories + }; + + Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks); + } +} + +void +ProcessGDBRemote::DebuggerInitialize (lldb_private::Debugger &debugger) +{ + if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) + { + const bool is_global_setting = true; + PluginManager::CreateSettingForProcessPlugin (debugger, + GetGlobalPluginProperties()->GetValueProperties(), + ConstString ("Properties for the gdb-remote process plug-in."), + is_global_setting); + } +} + +bool +ProcessGDBRemote::StartAsyncThread () +{ + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + + if (log) + log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); + + Mutex::Locker start_locker(m_async_thread_state_mutex); + if (m_async_thread_state == eAsyncThreadNotStarted) + { + // Create a thread that watches our internal state and controls which + // events make it to clients (into the DCProcess event queue). + m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL); + if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) + { + m_async_thread_state = eAsyncThreadRunning; + return true; + } + else + return false; + } + else + { + // Somebody tried to start the async thread while it was either being started or stopped. If the former, and + // it started up successfully, then say all's well. Otherwise it is an error, since we aren't going to restart it. + if (log) + log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state); + if (m_async_thread_state == eAsyncThreadRunning) + return true; + else + return false; + } +} + +void +ProcessGDBRemote::StopAsyncThread () +{ + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); + + if (log) + log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); + + Mutex::Locker start_locker(m_async_thread_state_mutex); + if (m_async_thread_state == eAsyncThreadRunning) + { + m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); + + // This will shut down the async thread. + m_gdb_comm.Disconnect(); // Disconnect from the debug server. + + // Stop the stdio thread + if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) + { + Host::ThreadJoin (m_async_thread, NULL, NULL); + } + m_async_thread_state = eAsyncThreadDone; + } + else + { + if (log) + log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state); + } +} + + +void * +ProcessGDBRemote::AsyncThread (void *arg) +{ + ProcessGDBRemote *process = (ProcessGDBRemote*) arg; + + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID()); + + Listener listener ("ProcessGDBRemote::AsyncThread"); + EventSP event_sp; + const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | + eBroadcastBitAsyncThreadShouldExit; + + if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) + { + listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit); + + bool done = false; + while (!done) + { + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID()); + if (listener.WaitForEvent (NULL, event_sp)) + { + const uint32_t event_type = event_sp->GetType(); + if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) + { + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type); + + switch (event_type) + { + case eBroadcastBitAsyncContinue: + { + const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); + + if (continue_packet) + { + const char *continue_cstr = (const char *)continue_packet->GetBytes (); + const size_t continue_cstr_len = continue_packet->GetByteSize (); + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr); + + if (::strstr (continue_cstr, "vAttach") == NULL) + process->SetPrivateState(eStateRunning); + StringExtractorGDBRemote response; + StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response); + + // We need to immediately clear the thread ID list so we are sure to get a valid list of threads. + // The thread ID list might be contained within the "response", or the stop reply packet that + // caused the stop. So clear it now before we give the stop reply packet to the process + // using the process->SetLastStopPacket()... + process->ClearThreadIDList (); + + switch (stop_state) + { + case eStateStopped: + case eStateCrashed: + case eStateSuspended: + process->SetLastStopPacket (response); + process->SetPrivateState (stop_state); + break; + + case eStateExited: + process->SetLastStopPacket (response); + process->ClearThreadIDList(); + response.SetFilePos(1); + process->SetExitStatus(response.GetHexU8(), NULL); + done = true; + break; + + case eStateInvalid: + process->SetExitStatus(-1, "lost connection"); + break; + + default: + process->SetPrivateState (stop_state); + break; + } + } + } + break; + + case eBroadcastBitAsyncThreadShouldExit: + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID()); + done = true; + break; + + default: + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); + done = true; + break; + } + } + else if (event_sp->BroadcasterIs (&process->m_gdb_comm)) + { + if (event_type & Communication::eBroadcastBitReadThreadDidExit) + { + process->SetExitStatus (-1, "lost connection"); + done = true; + } + } + } + else + { + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID()); + done = true; + } + } + } + + if (log) + log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID()); + + process->m_async_thread = LLDB_INVALID_HOST_THREAD; + return NULL; +} + +const char * +ProcessGDBRemote::GetDispatchQueueNameForThread +( + addr_t thread_dispatch_qaddr, + std::string &dispatch_queue_name +) +{ + dispatch_queue_name.clear(); + if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) + { + // Cache the dispatch_queue_offsets_addr value so we don't always have + // to look it up + if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS) + { + static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets"); + const Symbol *dispatch_queue_offsets_symbol = NULL; + ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false)); + ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec)); + if (module_sp) + dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData); + + if (dispatch_queue_offsets_symbol == NULL) + { + ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false)); + module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec); + if (module_sp) + dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData); + } + if (dispatch_queue_offsets_symbol) + m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_target); + + if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS) + return NULL; + } + + uint8_t memory_buffer[8]; + DataExtractor data (memory_buffer, + sizeof(memory_buffer), + m_target.GetArchitecture().GetByteOrder(), + m_target.GetArchitecture().GetAddressByteSize()); + + // Excerpt from src/queue_private.h + struct dispatch_queue_offsets_s + { + uint16_t dqo_version; + uint16_t dqo_label; // in version 1-3, offset to string; in version 4+, offset to a pointer to a string + uint16_t dqo_label_size; // in version 1-3, length of string; in version 4+, size of a (void*) in this process + } dispatch_queue_offsets; + + + Error error; + if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets)) + { + lldb::offset_t data_offset = 0; + if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t))) + { + if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize()) + { + data_offset = 0; + lldb::addr_t queue_addr = data.GetAddress(&data_offset); + if (dispatch_queue_offsets.dqo_version >= 4) + { + // libdispatch versions 4+, pointer to dispatch name is in the + // queue structure. + lldb::addr_t pointer_to_label_address = queue_addr + dispatch_queue_offsets.dqo_label; + if (ReadMemory (pointer_to_label_address, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize()) + { + data_offset = 0; + lldb::addr_t label_addr = data.GetAddress(&data_offset); + ReadCStringFromMemory (label_addr, dispatch_queue_name, error); + } + } + else + { + // libdispatch versions 1-3, dispatch name is a fixed width char array + // in the queue structure. + lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label; + dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0'); + size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error); + if (bytes_read < dispatch_queue_offsets.dqo_label_size) + dispatch_queue_name.erase (bytes_read); + } + } + } + } + } + if (dispatch_queue_name.empty()) + return NULL; + return dispatch_queue_name.c_str(); +} + +//uint32_t +//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) +//{ +// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver +// // process and ask it for the list of processes. But if we are local, we can let the Host do it. +// if (m_local_debugserver) +// { +// return Host::ListProcessesMatchingName (name, matches, pids); +// } +// else +// { +// // FIXME: Implement talking to the remote debugserver. +// return 0; +// } +// +//} +// +bool +ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton, + lldb_private::StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id) +{ + // I don't think I have to do anything here, just make sure I notice the new thread when it starts to + // run so I can stop it if that's what I want to do. + Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); + if (log) + log->Printf("Hit New Thread Notification breakpoint."); + return false; +} + + +bool +ProcessGDBRemote::StartNoticingNewThreads() +{ + Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); + if (m_thread_create_bp_sp) + { + if (log && log->GetVerbose()) + log->Printf("Enabled noticing new thread breakpoint."); + m_thread_create_bp_sp->SetEnabled(true); + } + else + { + PlatformSP platform_sp (m_target.GetPlatform()); + if (platform_sp) + { + m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target); + if (m_thread_create_bp_sp) + { + if (log && log->GetVerbose()) + log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID()); + m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); + } + else + { + if (log) + log->Printf("Failed to create new thread notification breakpoint."); + } + } + } + return m_thread_create_bp_sp.get() != NULL; +} + +bool +ProcessGDBRemote::StopNoticingNewThreads() +{ + Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); + if (log && log->GetVerbose()) + log->Printf ("Disabling new thread notification breakpoint."); + + if (m_thread_create_bp_sp) + m_thread_create_bp_sp->SetEnabled(false); + + return true; +} + +lldb_private::DynamicLoader * +ProcessGDBRemote::GetDynamicLoader () +{ + if (m_dyld_ap.get() == NULL) + m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); + return m_dyld_ap.get(); +} + + +class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed +{ +private: + +public: + CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) : + CommandObjectParsed (interpreter, + "process plugin packet history", + "Dumps the packet history buffer. ", + NULL) + { + } + + ~CommandObjectProcessGDBRemotePacketHistory () + { + } + + bool + DoExecute (Args& command, CommandReturnObject &result) + { + const size_t argc = command.GetArgumentCount(); + if (argc == 0) + { + ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); + if (process) + { + process->GetGDBRemote().DumpHistory(result.GetOutputStream()); + result.SetStatus (eReturnStatusSuccessFinishResult); + return true; + } + } + else + { + result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); + } + result.SetStatus (eReturnStatusFailed); + return false; + } +}; + +class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed +{ +private: + +public: + CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) : + CommandObjectParsed (interpreter, + "process plugin packet send", + "Send a custom packet through the GDB remote protocol and print the answer. " + "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.", + NULL) + { + } + + ~CommandObjectProcessGDBRemotePacketSend () + { + } + + bool + DoExecute (Args& command, CommandReturnObject &result) + { + const size_t argc = command.GetArgumentCount(); + if (argc == 0) + { + result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str()); + result.SetStatus (eReturnStatusFailed); + return false; + } + + ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); + if (process) + { + for (size_t i=0; i<argc; ++ i) + { + const char *packet_cstr = command.GetArgumentAtIndex(0); + bool send_async = true; + StringExtractorGDBRemote response; + process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); + result.SetStatus (eReturnStatusSuccessFinishResult); + Stream &output_strm = result.GetOutputStream(); + output_strm.Printf (" packet: %s\n", packet_cstr); + std::string &response_str = response.GetStringRef(); + + if (strstr(packet_cstr, "qGetProfileData") != NULL) + { + response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response); + } + + if (response_str.empty()) + output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); + else + output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); + } + } + return true; + } +}; + +class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw +{ +private: + +public: + CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) : + CommandObjectRaw (interpreter, + "process plugin packet monitor", + "Send a qRcmd packet through the GDB remote protocol and print the response." + "The argument passed to this command will be hex encoded into a valid 'qRcmd' packet, sent and the response will be printed.", + NULL) + { + } + + ~CommandObjectProcessGDBRemotePacketMonitor () + { + } + + bool + DoExecute (const char *command, CommandReturnObject &result) + { + if (command == NULL || command[0] == '\0') + { + result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str()); + result.SetStatus (eReturnStatusFailed); + return false; + } + + ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); + if (process) + { + StreamString packet; + packet.PutCString("qRcmd,"); + packet.PutBytesAsRawHex8(command, strlen(command)); + const char *packet_cstr = packet.GetString().c_str(); + + bool send_async = true; + StringExtractorGDBRemote response; + process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); + result.SetStatus (eReturnStatusSuccessFinishResult); + Stream &output_strm = result.GetOutputStream(); + output_strm.Printf (" packet: %s\n", packet_cstr); + const std::string &response_str = response.GetStringRef(); + + if (response_str.empty()) + output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); + else + output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); + } + return true; + } +}; + +class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword +{ +private: + +public: + CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) : + CommandObjectMultiword (interpreter, + "process plugin packet", + "Commands that deal with GDB remote packets.", + NULL) + { + LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter))); + LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter))); + LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter))); + } + + ~CommandObjectProcessGDBRemotePacket () + { + } +}; + +class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword +{ +public: + CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) : + CommandObjectMultiword (interpreter, + "process plugin", + "A set of commands for operating on a ProcessGDBRemote process.", + "process plugin <subcommand> [<subcommand-options>]") + { + LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter))); + } + + ~CommandObjectMultiwordProcessGDBRemote () + { + } +}; + +CommandObject * +ProcessGDBRemote::GetPluginCommandObject() +{ + if (!m_command_sp) + m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter())); + return m_command_sp.get(); +} diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h new file mode 100644 index 0000000..e104b71 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h @@ -0,0 +1,396 @@ +//===-- ProcessGDBRemote.h --------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessGDBRemote_h_ +#define liblldb_ProcessGDBRemote_h_ + +// C Includes + +// C++ Includes +#include <list> +#include <vector> + +// Other libraries and framework includes +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/Broadcaster.h" +#include "lldb/Core/ConstString.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/InputReader.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/StringList.h" +#include "lldb/Core/ThreadSafeValue.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/Thread.h" + +#include "GDBRemoteCommunicationClient.h" +#include "Utility/StringExtractor.h" +#include "GDBRemoteRegisterContext.h" + +class ThreadGDBRemote; + +class ProcessGDBRemote : public lldb_private::Process +{ +public: + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + static lldb::ProcessSP + CreateInstance (lldb_private::Target& target, + lldb_private::Listener &listener, + const lldb_private::FileSpec *crash_file_path); + + static void + Initialize(); + + static void + DebuggerInitialize (lldb_private::Debugger &debugger); + + static void + Terminate(); + + static lldb_private::ConstString + GetPluginNameStatic(); + + static const char * + GetPluginDescriptionStatic(); + + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener); + + virtual + ~ProcessGDBRemote(); + + //------------------------------------------------------------------ + // Check if a given Process + //------------------------------------------------------------------ + virtual bool + CanDebug (lldb_private::Target &target, + bool plugin_specified_by_name); + + virtual lldb_private::CommandObject * + GetPluginCommandObject(); + + //------------------------------------------------------------------ + // Creating a new process, or attaching to an existing one + //------------------------------------------------------------------ + virtual lldb_private::Error + WillLaunch (lldb_private::Module* module); + + virtual lldb_private::Error + DoLaunch (lldb_private::Module *exe_module, + const lldb_private::ProcessLaunchInfo &launch_info); + + virtual void + DidLaunch (); + + virtual lldb_private::Error + WillAttachToProcessWithID (lldb::pid_t pid); + + virtual lldb_private::Error + WillAttachToProcessWithName (const char *process_name, bool wait_for_launch); + + virtual lldb_private::Error + DoConnectRemote (lldb_private::Stream *strm, const char *remote_url); + + lldb_private::Error + WillLaunchOrAttach (); + + virtual lldb_private::Error + DoAttachToProcessWithID (lldb::pid_t pid); + + virtual lldb_private::Error + DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info); + + virtual lldb_private::Error + DoAttachToProcessWithName (const char *process_name, + bool wait_for_launch, + const lldb_private::ProcessAttachInfo &attach_info); + + virtual void + DidAttach (); + + //------------------------------------------------------------------ + // PluginInterface protocol + //------------------------------------------------------------------ + virtual lldb_private::ConstString + GetPluginName(); + + virtual uint32_t + GetPluginVersion(); + + //------------------------------------------------------------------ + // Process Control + //------------------------------------------------------------------ + virtual lldb_private::Error + WillResume (); + + virtual lldb_private::Error + DoResume (); + + virtual lldb_private::Error + DoHalt (bool &caused_stop); + + virtual lldb_private::Error + DoDetach (bool keep_stopped); + + virtual bool + DetachRequiresHalt() { return true; } + + virtual lldb_private::Error + DoSignal (int signal); + + virtual lldb_private::Error + DoDestroy (); + + virtual void + RefreshStateAfterStop(); + + //------------------------------------------------------------------ + // Process Queries + //------------------------------------------------------------------ + virtual bool + IsAlive (); + + virtual lldb::addr_t + GetImageInfoAddress(); + + //------------------------------------------------------------------ + // Process Memory + //------------------------------------------------------------------ + virtual size_t + DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error); + + virtual size_t + DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error); + + virtual lldb::addr_t + DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error); + + virtual lldb_private::Error + GetMemoryRegionInfo (lldb::addr_t load_addr, + lldb_private::MemoryRegionInfo ®ion_info); + + virtual lldb_private::Error + DoDeallocateMemory (lldb::addr_t ptr); + + //------------------------------------------------------------------ + // Process STDIO + //------------------------------------------------------------------ + virtual size_t + PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error); + + //---------------------------------------------------------------------- + // Process Breakpoints + //---------------------------------------------------------------------- + virtual lldb_private::Error + EnableBreakpointSite (lldb_private::BreakpointSite *bp_site); + + virtual lldb_private::Error + DisableBreakpointSite (lldb_private::BreakpointSite *bp_site); + + //---------------------------------------------------------------------- + // Process Watchpoints + //---------------------------------------------------------------------- + virtual lldb_private::Error + EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true); + + virtual lldb_private::Error + DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true); + + virtual lldb_private::Error + GetWatchpointSupportInfo (uint32_t &num); + + virtual lldb_private::Error + GetWatchpointSupportInfo (uint32_t &num, bool& after); + + virtual bool + StartNoticingNewThreads(); + + virtual bool + StopNoticingNewThreads(); + + GDBRemoteCommunicationClient & + GetGDBRemote() + { + return m_gdb_comm; + } + +protected: + friend class ThreadGDBRemote; + friend class GDBRemoteCommunicationClient; + friend class GDBRemoteRegisterContext; + + //---------------------------------------------------------------------- + // Accessors + //---------------------------------------------------------------------- + bool + IsRunning ( lldb::StateType state ) + { + return state == lldb::eStateRunning || IsStepping(state); + } + + bool + IsStepping ( lldb::StateType state) + { + return state == lldb::eStateStepping; + } + bool + CanResume ( lldb::StateType state) + { + return state == lldb::eStateStopped; + } + + bool + HasExited (lldb::StateType state) + { + return state == lldb::eStateExited; + } + + bool + ProcessIDIsValid ( ) const; + + void + Clear ( ); + + lldb_private::Flags & + GetFlags () + { + return m_flags; + } + + const lldb_private::Flags & + GetFlags () const + { + return m_flags; + } + + virtual bool + UpdateThreadList (lldb_private::ThreadList &old_thread_list, + lldb_private::ThreadList &new_thread_list); + + lldb_private::Error + StartDebugserverProcess (const char *debugserver_url); + + lldb_private::Error + StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info); + + void + KillDebugserverProcess (); + + void + BuildDynamicRegisterInfo (bool force); + + void + SetLastStopPacket (const StringExtractorGDBRemote &response); + + //------------------------------------------------------------------ + /// Broadcaster event bits definitions. + //------------------------------------------------------------------ + enum + { + eBroadcastBitAsyncContinue = (1 << 0), + eBroadcastBitAsyncThreadShouldExit = (1 << 1), + eBroadcastBitAsyncThreadDidExit = (1 << 2) + }; + + typedef enum AsyncThreadState + { + eAsyncThreadNotStarted, + eAsyncThreadRunning, + eAsyncThreadDone + } AsyncThreadState; + + lldb_private::Flags m_flags; // Process specific flags (see eFlags enums) + GDBRemoteCommunicationClient m_gdb_comm; + lldb::pid_t m_debugserver_pid; + StringExtractorGDBRemote m_last_stop_packet; + lldb_private::Mutex m_last_stop_packet_mutex; + GDBRemoteDynamicRegisterInfo m_register_info; + lldb_private::Broadcaster m_async_broadcaster; + lldb::thread_t m_async_thread; + AsyncThreadState m_async_thread_state; + lldb_private::Mutex m_async_thread_state_mutex; + typedef std::vector<lldb::tid_t> tid_collection; + typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection; + typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap; + tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping + tid_collection m_continue_c_tids; // 'c' for continue + tid_sig_collection m_continue_C_tids; // 'C' for continue with signal + tid_collection m_continue_s_tids; // 's' for step + tid_sig_collection m_continue_S_tids; // 'S' for step with signal + lldb::addr_t m_dispatch_queue_offsets_addr; + size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory + MMapMap m_addr_to_mmap_size; + lldb::BreakpointSP m_thread_create_bp_sp; + bool m_waiting_for_attach; + bool m_destroy_tried_resuming; + lldb::CommandObjectSP m_command_sp; + + bool + StartAsyncThread (); + + void + StopAsyncThread (); + + static void * + AsyncThread (void *arg); + + static bool + MonitorDebugserverProcess (void *callback_baton, + lldb::pid_t pid, + bool exited, + int signo, + int exit_status); + + lldb::StateType + SetThreadStopInfo (StringExtractor& stop_packet); + + void + ClearThreadIDList (); + + bool + UpdateThreadIDList (); + + void + DidLaunchOrAttach (); + + lldb_private::Error + ConnectToDebugserver (const char *host_port); + + const char * + GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr, + std::string &dispatch_queue_name); + + static size_t + AttachInputReaderCallback (void *baton, + lldb_private::InputReader *reader, + lldb::InputReaderAction notification, + const char *bytes, + size_t bytes_len); + + lldb_private::DynamicLoader * + GetDynamicLoader (); + +private: + //------------------------------------------------------------------ + // For ProcessGDBRemote only + //------------------------------------------------------------------ + static bool + NewThreadNotifyBreakpointHit (void *baton, + lldb_private::StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + + DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote); + +}; + +#endif // liblldb_ProcessGDBRemote_h_ diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp new file mode 100644 index 0000000..15b861f --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp @@ -0,0 +1,196 @@ +//===-- ProcessGDBRemoteLog.cpp ---------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ProcessGDBRemoteLog.h" + +#include "lldb/Interpreter/Args.h" +#include "lldb/Core/StreamFile.h" + +#include "ProcessGDBRemote.h" + +using namespace lldb; +using namespace lldb_private; + + +// We want to avoid global constructors where code needs to be run so here we +// control access to our static g_log_sp by hiding it in a singleton function +// that will construct the static g_lob_sp the first time this function is +// called. +static bool g_log_enabled = false; +static Log * g_log = NULL; +static Log * +GetLog () +{ + if (!g_log_enabled) + return NULL; + return g_log; +} + + +Log * +ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (uint32_t mask) +{ + Log *log(GetLog ()); + if (log && mask) + { + uint32_t log_mask = log->GetMask().Get(); + if ((log_mask & mask) != mask) + return NULL; + } + return log; +} + +Log * +ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (uint32_t mask) +{ + Log *log(GetLog ()); + if (log && log->GetMask().Get() & mask) + return log; + return NULL; +} + +void +ProcessGDBRemoteLog::DisableLog (const char **categories, Stream *feedback_strm) +{ + Log *log (GetLog ()); + if (log) + { + uint32_t flag_bits = 0; + + if (categories[0] != NULL) + { + flag_bits = log->GetMask().Get(); + for (size_t i = 0; categories[i] != NULL; ++i) + { + const char *arg = categories[i]; + + + if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~GDBR_LOG_ALL; + else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~GDBR_LOG_ASYNC; + else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~GDBR_LOG_BREAKPOINTS; + else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~GDBR_LOG_COMM; + else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~GDBR_LOG_DEFAULT; + else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~GDBR_LOG_PACKETS; + else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY; + else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT; + else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG; + else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~GDBR_LOG_PROCESS; + else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~GDBR_LOG_STEP; + else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~GDBR_LOG_THREAD; + else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~GDBR_LOG_VERBOSE; + else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~GDBR_LOG_WATCHPOINTS; + else + { + feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); + ListLogCategories (feedback_strm); + } + + } + } + + if (flag_bits == 0) + g_log_enabled = false; + else + log->GetMask().Reset (flag_bits); + } + + return; +} + +Log * +ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm) +{ + // Try see if there already is a log - that way we can reuse its settings. + // We could reuse the log in toto, but we don't know that the stream is the same. + uint32_t flag_bits = 0; + if (g_log) + flag_bits = g_log->GetMask().Get(); + + // Now make a new log with this stream if one was provided + if (log_stream_sp) + { + if (g_log) + g_log->SetStream(log_stream_sp); + else + g_log = new Log(log_stream_sp); + } + + if (g_log) + { + bool got_unknown_category = false; + for (size_t i=0; categories[i] != NULL; ++i) + { + const char *arg = categories[i]; + + if (::strcasecmp (arg, "all") == 0 ) flag_bits |= GDBR_LOG_ALL; + else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= GDBR_LOG_ASYNC; + else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= GDBR_LOG_BREAKPOINTS; + else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= GDBR_LOG_COMM; + else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= GDBR_LOG_DEFAULT; + else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= GDBR_LOG_PACKETS; + else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= GDBR_LOG_MEMORY; + else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT; + else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG; + else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= GDBR_LOG_PROCESS; + else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= GDBR_LOG_STEP; + else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= GDBR_LOG_THREAD; + else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= GDBR_LOG_VERBOSE; + else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= GDBR_LOG_WATCHPOINTS; + else + { + feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); + if (got_unknown_category == false) + { + got_unknown_category = true; + ListLogCategories (feedback_strm); + } + } + } + if (flag_bits == 0) + flag_bits = GDBR_LOG_DEFAULT; + g_log->GetMask().Reset(flag_bits); + g_log->GetOptions().Reset(log_options); + } + g_log_enabled = true; + return g_log; +} + +void +ProcessGDBRemoteLog::ListLogCategories (Stream *strm) +{ + strm->Printf ("Logging categories for '%s':\n" + " all - turn on all available logging categories\n" + " async - log asynchronous activity\n" + " break - log breakpoints\n" + " communication - log communication activity\n" + " default - enable the default set of logging categories for liblldb\n" + " packets - log gdb remote packets\n" + " memory - log memory reads and writes\n" + " data-short - log memory bytes for memory reads and writes for short transactions only\n" + " data-long - log memory bytes for memory reads and writes for all transactions\n" + " process - log process events and activities\n" + " thread - log thread events and activities\n" + " step - log step related activities\n" + " verbose - enable verbose logging\n" + " watch - log watchpoint related activities\n", ProcessGDBRemote::GetPluginNameStatic().GetCString()); +} + + +void +ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...) +{ + Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask)); + if (log) + { + va_list args; + va_start (args, format); + log->VAPrintf (format, args); + va_end (args); + } +} diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h new file mode 100644 index 0000000..9373406 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h @@ -0,0 +1,57 @@ +//===-- ProcessGDBRemoteLog.h -----------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessGDBRemoteLog_h_ +#define liblldb_ProcessGDBRemoteLog_h_ + +// C Includes +// C++ Includes +// Other libraries and framework includes + +// Project includes +#include "lldb/Core/Log.h" + +#define GDBR_LOG_VERBOSE (1u << 0) +#define GDBR_LOG_PROCESS (1u << 1) +#define GDBR_LOG_THREAD (1u << 2) +#define GDBR_LOG_PACKETS (1u << 3) +#define GDBR_LOG_MEMORY (1u << 4) // Log memory reads/writes calls +#define GDBR_LOG_MEMORY_DATA_SHORT (1u << 5) // Log short memory reads/writes bytes +#define GDBR_LOG_MEMORY_DATA_LONG (1u << 6) // Log all memory reads/writes bytes +#define GDBR_LOG_BREAKPOINTS (1u << 7) +#define GDBR_LOG_WATCHPOINTS (1u << 8) +#define GDBR_LOG_STEP (1u << 9) +#define GDBR_LOG_COMM (1u << 10) +#define GDBR_LOG_ASYNC (1u << 11) +#define GDBR_LOG_ALL (UINT32_MAX) +#define GDBR_LOG_DEFAULT GDBR_LOG_PACKETS + +class ProcessGDBRemoteLog +{ +public: + static lldb_private::Log * + GetLogIfAllCategoriesSet(uint32_t mask = 0); + + static lldb_private::Log * + GetLogIfAnyCategoryIsSet (uint32_t mask); + + static void + DisableLog (const char **categories, lldb_private::Stream *feedback_strm); + + static lldb_private::Log * + EnableLog (lldb::StreamSP &log_stream_sp, uint32_t log_options, const char **categories, lldb_private::Stream *feedback_strm); + + static void + ListLogCategories (lldb_private::Stream *strm); + + static void + LogIf (uint32_t mask, const char *format, ...); +}; + +#endif // liblldb_ProcessGDBRemoteLog_h_ diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp new file mode 100644 index 0000000..38fb84d --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp @@ -0,0 +1,214 @@ +//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "ThreadGDBRemote.h" + +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/State.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Unwind.h" +#include "lldb/Breakpoint/Watchpoint.h" + +#include "ProcessGDBRemote.h" +#include "ProcessGDBRemoteLog.h" +#include "Utility/StringExtractorGDBRemote.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// Thread Registers +//---------------------------------------------------------------------- + +ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) : + Thread(process, tid), + m_thread_name (), + m_dispatch_queue_name (), + m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) +{ + ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", + this, + process.GetID(), + GetID()); +} + +ThreadGDBRemote::~ThreadGDBRemote () +{ + ProcessSP process_sp(GetProcess()); + ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", + this, + process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, + GetID()); + DestroyThread(); +} + +const char * +ThreadGDBRemote::GetName () +{ + if (m_thread_name.empty()) + return NULL; + return m_thread_name.c_str(); +} + + +const char * +ThreadGDBRemote::GetQueueName () +{ + // Always re-fetch the dispatch queue name since it can change + + if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) + { + ProcessSP process_sp (GetProcess()); + if (process_sp) + { + ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); + return gdb_process->GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name); + } + } + return NULL; +} + +void +ThreadGDBRemote::WillResume (StateType resume_state) +{ + int signo = GetResumeSignal(); + const lldb::user_id_t tid = GetProtocolID(); + Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); + if (log) + log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state)); + + ProcessSP process_sp (GetProcess()); + if (process_sp) + { + ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); + switch (resume_state) + { + case eStateSuspended: + case eStateStopped: + // Don't append anything for threads that should stay stopped. + break; + + case eStateRunning: + if (gdb_process->GetUnixSignals().SignalIsValid (signo)) + gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); + else + gdb_process->m_continue_c_tids.push_back(tid); + break; + + case eStateStepping: + if (gdb_process->GetUnixSignals().SignalIsValid (signo)) + gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); + else + gdb_process->m_continue_s_tids.push_back(tid); + break; + + default: + break; + } + } +} + +void +ThreadGDBRemote::RefreshStateAfterStop() +{ + // Invalidate all registers in our register context. We don't set "force" to + // true because the stop reply packet might have had some register values + // that were expedited and these will already be copied into the register + // context by the time this function gets called. The GDBRemoteRegisterContext + // class has been made smart enough to detect when it needs to invalidate + // which registers are valid by putting hooks in the register read and + // register supply functions where they check the process stop ID and do + // the right thing. + const bool force = false; + GetRegisterContext()->InvalidateIfNeeded (force); +} + +bool +ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) +{ + return thread != 0; +} + +void +ThreadGDBRemote::Dump(Log *log, uint32_t index) +{ +} + + +bool +ThreadGDBRemote::ShouldStop (bool &step_more) +{ + return true; +} +lldb::RegisterContextSP +ThreadGDBRemote::GetRegisterContext () +{ + if (m_reg_context_sp.get() == NULL) + m_reg_context_sp = CreateRegisterContextForFrame (NULL); + return m_reg_context_sp; +} + +lldb::RegisterContextSP +ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) +{ + lldb::RegisterContextSP reg_ctx_sp; + const bool read_all_registers_at_once = false; + uint32_t concrete_frame_idx = 0; + + if (frame) + concrete_frame_idx = frame->GetConcreteFrameIndex (); + + + if (concrete_frame_idx == 0) + { + ProcessSP process_sp (GetProcess()); + if (process_sp) + { + ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); + reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once)); + } + } + else + { + Unwind *unwinder = GetUnwinder (); + if (unwinder) + reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); + } + return reg_ctx_sp; +} + +bool +ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) +{ + GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); + assert (gdb_reg_ctx); + return gdb_reg_ctx->PrivateSetRegisterValue (reg, response); +} + +bool +ThreadGDBRemote::CalculateStopInfo () +{ + ProcessSP process_sp (GetProcess()); + if (process_sp) + { + StringExtractorGDBRemote stop_packet; + ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); + if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet)) + return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped; + } + return false; +} + + diff --git a/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.h b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.h new file mode 100644 index 0000000..50a3f19 --- /dev/null +++ b/contrib/llvm/tools/lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.h @@ -0,0 +1,107 @@ +//===-- ThreadGDBRemote.h ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ThreadGDBRemote_h_ +#define liblldb_ThreadGDBRemote_h_ + +#include <string> + +#include "lldb/Target/Process.h" +#include "lldb/Target/Thread.h" + +class StringExtractor; +class ProcessGDBRemote; + +class ThreadGDBRemote : public lldb_private::Thread +{ +public: + ThreadGDBRemote (lldb_private::Process &process, lldb::tid_t tid); + + virtual + ~ThreadGDBRemote (); + + virtual void + WillResume (lldb::StateType resume_state); + + virtual void + RefreshStateAfterStop(); + + virtual const char * + GetName (); + + virtual const char * + GetQueueName (); + + virtual lldb::RegisterContextSP + GetRegisterContext (); + + virtual lldb::RegisterContextSP + CreateRegisterContextForFrame (lldb_private::StackFrame *frame); + + void + Dump (lldb_private::Log *log, uint32_t index); + + static bool + ThreadIDIsValid (lldb::tid_t thread); + + bool + ShouldStop (bool &step_more); + + const char * + GetBasicInfoAsString (); + + void + SetName (const char *name) + { + if (name && name[0]) + m_thread_name.assign (name); + else + m_thread_name.clear(); + } + + lldb::addr_t + GetThreadDispatchQAddr () + { + return m_thread_dispatch_qaddr; + } + + void + SetThreadDispatchQAddr (lldb::addr_t thread_dispatch_qaddr) + { + m_thread_dispatch_qaddr = thread_dispatch_qaddr; + } + +protected: + + friend class ProcessGDBRemote; + + bool + PrivateSetRegisterValue (uint32_t reg, + StringExtractor &response); + + //------------------------------------------------------------------ + // Member variables. + //------------------------------------------------------------------ + std::string m_thread_name; + std::string m_dispatch_queue_name; + lldb::addr_t m_thread_dispatch_qaddr; + //------------------------------------------------------------------ + // Member variables. + //------------------------------------------------------------------ + + void + SetStopInfoFromPacket (StringExtractor &stop_packet, uint32_t stop_id); + + virtual bool + CalculateStopInfo (); + + +}; + +#endif // liblldb_ThreadGDBRemote_h_ |