diff options
Diffstat (limited to 'source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp')
-rw-r--r-- | source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp | 206 |
1 files changed, 95 insertions, 111 deletions
diff --git a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp index 11052eff948f9..144ae103faa4b 100644 --- a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp +++ b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp @@ -14,6 +14,7 @@ #include <sys/stat.h> #include "lldb/Core/StreamFile.h" +#include "lldb/Host/Config.h" #include "lldb/Host/ConnectionFileDescriptor.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/Host.h" @@ -38,6 +39,8 @@ #if defined(__APPLE__) #define DEBUGSERVER_BASENAME "debugserver" +#elif defined(_WIN32) +#define DEBUGSERVER_BASENAME "lldb-server.exe" #else #define DEBUGSERVER_BASENAME "lldb-server" #endif @@ -99,10 +102,8 @@ size_t GDBRemoteCommunication::SendAck() { ConnectionStatus status = eConnectionStatusSuccess; char ch = '+'; const size_t bytes_written = Write(&ch, 1, status, nullptr); - if (log) - log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); - m_history.AddPacket(ch, GDBRemoteCommunicationHistory::ePacketTypeSend, - bytes_written); + LLDB_LOGF(log, "<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); + m_history.AddPacket(ch, GDBRemotePacket::ePacketTypeSend, bytes_written); return bytes_written; } @@ -111,10 +112,8 @@ size_t GDBRemoteCommunication::SendNack() { ConnectionStatus status = eConnectionStatusSuccess; char ch = '-'; const size_t bytes_written = Write(&ch, 1, status, nullptr); - if (log) - log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); - m_history.AddPacket(ch, GDBRemoteCommunicationHistory::ePacketTypeSend, - bytes_written); + LLDB_LOGF(log, "<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); + m_history.AddPacket(ch, GDBRemotePacket::ePacketTypeSend, bytes_written); return bytes_written; } @@ -172,13 +171,12 @@ GDBRemoteCommunication::SendRawPacketNoLock(llvm::StringRef packet, strm.Printf("%*s", (int)3, p); log->PutString(strm.GetString()); } else - log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, - (int)packet_length, packet_data); + LLDB_LOGF(log, "<%4" PRIu64 "> send packet: %.*s", + (uint64_t)bytes_written, (int)packet_length, packet_data); } m_history.AddPacket(packet.str(), packet_length, - GDBRemoteCommunicationHistory::ePacketTypeSend, - bytes_written); + GDBRemotePacket::ePacketTypeSend, bytes_written); if (bytes_written == packet_length) { if (!skip_ack && GetSendAcks()) @@ -186,9 +184,8 @@ GDBRemoteCommunication::SendRawPacketNoLock(llvm::StringRef packet, else return PacketResult::Success; } else { - if (log) - log->Printf("error: failed to send packet: %.*s", (int)packet_length, - packet_data); + LLDB_LOGF(log, "error: failed to send packet: %.*s", (int)packet_length, + packet_data); } } return PacketResult::ErrorSendFailed; @@ -332,11 +329,12 @@ GDBRemoteCommunication::WaitForPacketNoLock(StringExtractorGDBRemote &packet, std::string regex_str = "^"; regex_str += echo_packet; regex_str += "$"; - response_regex.Compile(regex_str); + response_regex = RegularExpression(regex_str); } else { echo_packet_len = ::snprintf(echo_packet, sizeof(echo_packet), "qC"); - response_regex.Compile(llvm::StringRef("^QC[0-9A-Fa-f]+$")); + response_regex = + RegularExpression(llvm::StringRef("^QC[0-9A-Fa-f]+$")); } PacketResult echo_packet_result = @@ -489,11 +487,10 @@ bool GDBRemoteCommunication::DecompressPacket() { llvm::StringRef(m_bytes).substr(1, hash_mark_idx - 1)); bool success = packet_checksum == actual_checksum; if (!success) { - if (log) - log->Printf( - "error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", - (int)(pkt_size), m_bytes.c_str(), (uint8_t)packet_checksum, - (uint8_t)actual_checksum); + LLDB_LOGF(log, + "error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", + (int)(pkt_size), m_bytes.c_str(), (uint8_t)packet_checksum, + (uint8_t)actual_checksum); } // Send the ack or nack if needed if (!success) { @@ -651,8 +648,8 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, 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); + LLDB_LOGF(log, "GDBRemoteCommunication::%s adding %u bytes: %.*s", + __FUNCTION__, (uint32_t)src_len, (uint32_t)src_len, src); } m_bytes.append((const char *)src, src_len); } @@ -733,9 +730,8 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, break; } } - if (log) - log->Printf("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", - __FUNCTION__, idx - 1, idx - 1, m_bytes.c_str()); + LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", + __FUNCTION__, idx - 1, idx - 1, m_bytes.c_str()); m_bytes.erase(0, idx - 1); } break; } @@ -752,7 +748,6 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, size_t content_end = content_start + content_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 @@ -800,25 +795,23 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, log->PutString(strm.GetString()); } else { if (CompressionIsEnabled()) - log->Printf("<%4" PRIu64 ":%" PRIu64 "> read packet: %.*s", - (uint64_t)original_packet_size, (uint64_t)total_length, - (int)(total_length), m_bytes.c_str()); + LLDB_LOGF(log, "<%4" PRIu64 ":%" PRIu64 "> read packet: %.*s", + (uint64_t)original_packet_size, (uint64_t)total_length, + (int)(total_length), m_bytes.c_str()); else - log->Printf("<%4" PRIu64 "> read packet: %.*s", - (uint64_t)total_length, (int)(total_length), - m_bytes.c_str()); + LLDB_LOGF(log, "<%4" PRIu64 "> read packet: %.*s", + (uint64_t)total_length, (int)(total_length), + m_bytes.c_str()); } } m_history.AddPacket(m_bytes, total_length, - GDBRemoteCommunicationHistory::ePacketTypeRecv, - total_length); + GDBRemotePacket::ePacketTypeRecv, total_length); - // Clear packet_str in case there is some existing data in it. - packet_str.clear(); // Copy the packet from m_bytes to packet_str expanding the run-length // encoding in the process. Reserve enough byte for the most common case // (no RLE used) + std ::string packet_str; packet_str.reserve(m_bytes.length()); for (std::string::const_iterator c = m_bytes.begin() + content_start; c != m_bytes.begin() + content_end; ++c) { @@ -841,6 +834,7 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, packet_str.push_back(*c); } } + packet = StringExtractorGDBRemote(packet_str); if (m_bytes[0] == '$' || m_bytes[0] == '%') { assert(checksum_idx < m_bytes.size()); @@ -853,11 +847,11 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, llvm::StringRef(m_bytes).slice(content_start, content_end)); 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); + LLDB_LOGF(log, + "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) @@ -867,9 +861,8 @@ GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len, } } else { success = false; - if (log) - log->Printf("error: invalid checksum in packet: '%s'\n", - m_bytes.c_str()); + LLDB_LOGF(log, "error: invalid checksum in packet: '%s'\n", + m_bytes.c_str()); } } @@ -933,10 +926,8 @@ Status GDBRemoteCommunication::StartDebugserverProcess( const char *url, Platform *platform, ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args, int pass_comm_fd) { Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); - if (log) - log->Printf("GDBRemoteCommunication::%s(url=%s, port=%" PRIu16 ")", - __FUNCTION__, url ? url : "<empty>", - port ? *port : uint16_t(0)); + LLDB_LOGF(log, "GDBRemoteCommunication::%s(url=%s, port=%" PRIu16 ")", + __FUNCTION__, url ? url : "<empty>", port ? *port : uint16_t(0)); Status error; // If we locate debugserver, keep that located version around @@ -953,10 +944,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( if (!env_debugserver_path.empty()) { debugserver_file_spec.SetFile(env_debugserver_path, FileSpec::Style::native); - if (log) - log->Printf("GDBRemoteCommunication::%s() gdb-remote stub exe path set " - "from environment variable: %s", - __FUNCTION__, env_debugserver_path.c_str()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() gdb-remote stub exe path set " + "from environment variable: %s", + __FUNCTION__, env_debugserver_path.c_str()); } else debugserver_file_spec = g_debugserver_file_spec; bool debugserver_exists = @@ -968,10 +959,9 @@ Status GDBRemoteCommunication::StartDebugserverProcess( debugserver_file_spec.AppendPathComponent(DEBUGSERVER_BASENAME); debugserver_exists = FileSystem::Instance().Exists(debugserver_file_spec); if (debugserver_exists) { - if (log) - log->Printf( - "GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", - __FUNCTION__, debugserver_file_spec.GetPath().c_str()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", + __FUNCTION__, debugserver_file_spec.GetPath().c_str()); g_debugserver_file_spec = debugserver_file_spec; } else { @@ -985,10 +975,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( // exist debugserver_exists = true; } else { - if (log) - log->Printf("GDBRemoteCommunication::%s() could not find " - "gdb-remote stub exe '%s'", - __FUNCTION__, debugserver_file_spec.GetPath().c_str()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() could not find " + "gdb-remote stub exe '%s'", + __FUNCTION__, debugserver_file_spec.GetPath().c_str()); } // Don't cache the platform specific GDB server binary as it could // change from platform to platform @@ -1052,10 +1042,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( error = socket_pipe.CreateWithUniqueName("debugserver-named-pipe", false, named_pipe_path); if (error.Fail()) { - if (log) - log->Printf("GDBRemoteCommunication::%s() " - "named pipe creation failed: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() " + "named pipe creation failed: %s", + __FUNCTION__, error.AsCString()); return error; } debugserver_args.AppendArgument(llvm::StringRef("--named-pipe")); @@ -1065,10 +1055,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( // using using an unnamed pipe... error = socket_pipe.CreateNew(true); if (error.Fail()) { - if (log) - log->Printf("GDBRemoteCommunication::%s() " - "unnamed pipe creation failed: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() " + "unnamed pipe creation failed: %s", + __FUNCTION__, error.AsCString()); return error; } pipe_t write = socket_pipe.GetWritePipe(); @@ -1081,10 +1071,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( // debugserver connect to us.. error = StartListenThread("127.0.0.1", 0); if (error.Fail()) { - if (log) - log->Printf("GDBRemoteCommunication::%s() unable to start listen " - "thread: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() unable to start listen " + "thread: %s", + __FUNCTION__, error.AsCString()); return error; } @@ -1104,9 +1094,8 @@ Status GDBRemoteCommunication::StartDebugserverProcess( *port = port_; } else { error.SetErrorString("failed to bind to port 0 on 127.0.0.1"); - if (log) - log->Printf("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__, - error.AsCString()); + LLDB_LOGF(log, "GDBRemoteCommunication::%s() failed: %s", + __FUNCTION__, error.AsCString()); return error; } } @@ -1148,10 +1137,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( if (has_env_var) { debugserver_args.AppendArgument(llvm::StringRef(extra_arg)); - if (log) - log->Printf("GDBRemoteCommunication::%s adding env var %s contents " - "to stub command line (%s)", - __FUNCTION__, env_var_name, extra_arg.c_str()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s adding env var %s contents " + "to stub command line (%s)", + __FUNCTION__, env_var_name, extra_arg.c_str()); } } while (has_env_var); @@ -1177,8 +1166,8 @@ Status GDBRemoteCommunication::StartDebugserverProcess( StreamString string_stream; Platform *const platform = nullptr; launch_info.Dump(string_stream, platform); - log->Printf("launch info for gdb-remote stub:\n%s", - string_stream.GetData()); + LLDB_LOGF(log, "launch info for gdb-remote stub:\n%s", + string_stream.GetData()); } error = Host::LaunchProcess(launch_info); @@ -1188,11 +1177,10 @@ Status GDBRemoteCommunication::StartDebugserverProcess( if (named_pipe_path.size() > 0) { error = socket_pipe.OpenAsReader(named_pipe_path, false); if (error.Fail()) - if (log) - log->Printf("GDBRemoteCommunication::%s() " - "failed to open named pipe %s for reading: %s", - __FUNCTION__, named_pipe_path.c_str(), - error.AsCString()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() " + "failed to open named pipe %s for reading: %s", + __FUNCTION__, named_pipe_path.c_str(), error.AsCString()); } if (socket_pipe.CanWrite()) @@ -1209,24 +1197,22 @@ Status GDBRemoteCommunication::StartDebugserverProcess( uint16_t child_port = StringConvert::ToUInt32(port_cstr, 0); if (*port == 0 || *port == child_port) { *port = child_port; - if (log) - log->Printf("GDBRemoteCommunication::%s() " - "debugserver listens %u port", - __FUNCTION__, *port); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() " + "debugserver listens %u port", + __FUNCTION__, *port); } else { - if (log) - log->Printf("GDBRemoteCommunication::%s() " - "debugserver listening on port " - "%d but requested port was %d", - __FUNCTION__, (uint32_t)child_port, - (uint32_t)(*port)); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() " + "debugserver listening on port " + "%d but requested port was %d", + __FUNCTION__, (uint32_t)child_port, (uint32_t)(*port)); } } else { - if (log) - log->Printf("GDBRemoteCommunication::%s() " - "failed to read a port value from pipe %s: %s", - __FUNCTION__, named_pipe_path.c_str(), - error.AsCString()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s() " + "failed to read a port value from pipe %s: %s", + __FUNCTION__, named_pipe_path.c_str(), error.AsCString()); } socket_pipe.Close(); } @@ -1234,10 +1220,9 @@ Status GDBRemoteCommunication::StartDebugserverProcess( if (named_pipe_path.size() > 0) { const auto err = socket_pipe.Delete(named_pipe_path); if (err.Fail()) { - if (log) - log->Printf( - "GDBRemoteCommunication::%s failed to delete pipe %s: %s", - __FUNCTION__, named_pipe_path.c_str(), err.AsCString()); + LLDB_LOGF(log, + "GDBRemoteCommunication::%s failed to delete pipe %s: %s", + __FUNCTION__, named_pipe_path.c_str(), err.AsCString()); } } @@ -1249,9 +1234,8 @@ Status GDBRemoteCommunication::StartDebugserverProcess( } if (error.Fail()) { - if (log) - log->Printf("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__, - error.AsCString()); + LLDB_LOGF(log, "GDBRemoteCommunication::%s() failed: %s", __FUNCTION__, + error.AsCString()); } return error; @@ -1351,7 +1335,7 @@ void GDBRemoteCommunication::AppendBytesToCache(const uint8_t *bytes, if (type == PacketType::Notify) { // put this packet into an event - const char *pdata = packet.GetStringRef().c_str(); + const char *pdata = packet.GetStringRef().data(); // as the communication class, we are a broadcaster and the async thread // is tuned to listen to us |