2010-06-09 00:52:24 +08:00
|
|
|
//===-- 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
|
2011-05-14 04:07:25 +08:00
|
|
|
#include <limits.h>
|
2011-03-26 02:16:28 +08:00
|
|
|
#include <string.h>
|
2013-12-05 03:19:12 +08:00
|
|
|
#include <sys/stat.h>
|
2011-03-26 02:16:28 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
2013-12-06 06:58:22 +08:00
|
|
|
#include "lldb/Core/ConnectionFileDescriptor.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2012-04-10 06:46:21 +08:00
|
|
|
#include "lldb/Core/StreamFile.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/StreamString.h"
|
2011-04-12 13:54:46 +08:00
|
|
|
#include "lldb/Host/FileSpec.h"
|
2014-08-16 06:04:21 +08:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2011-04-12 13:54:46 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
2014-08-07 02:16:26 +08:00
|
|
|
#include "lldb/Host/Socket.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Host/TimeValue.h"
|
2011-04-12 13:54:46 +08:00
|
|
|
#include "lldb/Target/Process.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// Project includes
|
|
|
|
#include "ProcessGDBRemoteLog.h"
|
|
|
|
|
2014-07-23 07:41:36 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
# define DEBUGSERVER_BASENAME "debugserver"
|
|
|
|
#else
|
|
|
|
# define DEBUGSERVER_BASENAME "lldb-gdbserver"
|
|
|
|
#endif
|
2011-04-12 13:54:46 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2012-04-10 06:46:21 +08:00
|
|
|
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 ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-14 05:24:18 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 06:46:21 +08:00
|
|
|
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;
|
2012-11-30 05:49:15 +08:00
|
|
|
strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n",
|
2012-04-10 06:46:21 +08:00
|
|
|
entry.packet_idx,
|
2012-04-14 05:24:18 +08:00
|
|
|
entry.tid,
|
2012-04-10 06:46:21 +08:00
|
|
|
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;
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
|
2012-04-10 06:46:21 +08:00
|
|
|
entry.packet_idx,
|
2012-04-14 05:24:18 +08:00
|
|
|
entry.tid,
|
2012-04-10 06:46:21 +08:00
|
|
|
entry.bytes_transmitted,
|
|
|
|
(entry.type == ePacketTypeSend) ? "send" : "read",
|
|
|
|
entry.packet.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// GDBRemoteCommunication constructor
|
|
|
|
//----------------------------------------------------------------------
|
2011-04-12 13:54:46 +08:00
|
|
|
GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
|
|
|
|
const char *listener_name,
|
|
|
|
bool is_platform) :
|
2011-03-22 12:00:09 +08:00
|
|
|
Communication(comm_name),
|
2013-08-27 07:57:52 +08:00
|
|
|
#ifdef LLDB_CONFIGURATION_DEBUG
|
|
|
|
m_packet_timeout (1000),
|
|
|
|
#else
|
2011-06-17 11:31:01 +08:00
|
|
|
m_packet_timeout (1),
|
2013-08-27 07:57:52 +08:00
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
m_sequence_mutex (Mutex::eMutexTypeRecursive),
|
2011-01-20 15:53:45 +08:00
|
|
|
m_public_is_running (false),
|
2011-03-24 12:28:38 +08:00
|
|
|
m_private_is_running (false),
|
2012-04-10 06:46:21 +08:00
|
|
|
m_history (512),
|
2011-04-12 13:54:46 +08:00
|
|
|
m_send_acks (true),
|
2013-12-06 06:58:22 +08:00
|
|
|
m_is_platform (is_platform),
|
|
|
|
m_listen_thread (LLDB_INVALID_HOST_THREAD),
|
|
|
|
m_listen_url ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Destructor
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
GDBRemoteCommunication::~GDBRemoteCommunication()
|
|
|
|
{
|
|
|
|
if (IsConnected())
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char
|
|
|
|
GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
|
|
|
|
{
|
|
|
|
int checksum = 0;
|
|
|
|
|
2013-08-20 22:12:58 +08:00
|
|
|
for (size_t i = 0; i < payload_length; ++i)
|
|
|
|
checksum += payload[i];
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
return checksum & 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2011-01-22 15:12:45 +08:00
|
|
|
GDBRemoteCommunication::SendAck ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
|
2010-06-09 00:52:24 +08:00
|
|
|
ConnectionStatus status = eConnectionStatusSuccess;
|
2012-04-10 06:46:21 +08:00
|
|
|
char ch = '+';
|
|
|
|
const size_t bytes_written = Write (&ch, 1, status, NULL);
|
|
|
|
if (log)
|
2013-10-24 02:24:30 +08:00
|
|
|
log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
|
2012-04-10 06:46:21 +08:00
|
|
|
m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
|
|
|
|
return bytes_written;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 15:12:45 +08:00
|
|
|
size_t
|
|
|
|
GDBRemoteCommunication::SendNack ()
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
|
2011-01-22 15:12:45 +08:00
|
|
|
ConnectionStatus status = eConnectionStatusSuccess;
|
2012-04-10 06:46:21 +08:00
|
|
|
char ch = '-';
|
|
|
|
const size_t bytes_written = Write (&ch, 1, status, NULL);
|
|
|
|
if (log)
|
2013-10-24 02:24:30 +08:00
|
|
|
log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
|
2012-04-10 06:46:21 +08:00
|
|
|
m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
|
|
|
|
return bytes_written;
|
Many improvements to the Platform base class and subclasses. The base Platform
class now implements the Host functionality for a lot of things that make
sense by default so that subclasses can check:
int
PlatformSubclass::Foo ()
{
if (IsHost())
return Platform::Foo (); // Let the platform base class do the host specific stuff
// Platform subclass specific code...
int result = ...
return result;
}
Added new functions to the platform:
virtual const char *Platform::GetUserName (uint32_t uid);
virtual const char *Platform::GetGroupName (uint32_t gid);
The user and group names are cached locally so that remote platforms can avoid
sending packets multiple times to resolve this information.
Added the parent process ID to the ProcessInfo class.
Added a new ProcessInfoMatch class which helps us to match processes up
and changed the Host layer over to using this new class. The new class allows
us to search for processs:
1 - by name (equal to, starts with, ends with, contains, and regex)
2 - by pid
3 - And further check for parent pid == value, uid == value, gid == value,
euid == value, egid == value, arch == value, parent == value.
This is all hookup up to the "platform process list" command which required
adding dumping routines to dump process information. If the Host class
implements the process lookup routines, you can now lists processes on
your local machine:
machine1.foo.com % lldb
(lldb) platform process list
PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99538 1 username usergroup username usergroup x86_64-apple-darwin FileMerge
94943 1 username usergroup username usergroup x86_64-apple-darwin mdworker
94852 244 username usergroup username usergroup x86_64-apple-darwin Safari
94727 244 username usergroup username usergroup x86_64-apple-darwin Xcode
92742 92710 username usergroup username usergroup i386-apple-darwin debugserver
This of course also works remotely with the lldb-platform:
machine1.foo.com % lldb-platform --listen 1234
machine2.foo.com % lldb
(lldb) platform create remote-macosx
Platform: remote-macosx
Connected: no
(lldb) platform connect connect://localhost:1444
Platform: remote-macosx
Triple: x86_64-apple-darwin
OS Version: 10.6.7 (10J869)
Kernel: Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386
Hostname: machine1.foo.com
Connected: yes
(lldb) platform process list
PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99556 244 username usergroup username usergroup x86_64-apple-darwin trustevaluation
99548 65539 username usergroup username usergroup x86_64-apple-darwin lldb
99538 1 username usergroup username usergroup x86_64-apple-darwin FileMerge
94943 1 username usergroup username usergroup x86_64-apple-darwin mdworker
94852 244 username usergroup username usergroup x86_64-apple-darwin Safari
The lldb-platform implements everything with the Host:: layer, so this should
"just work" for linux. I will probably be adding more stuff to the Host layer
for launching processes and attaching to processes so that this support should
eventually just work as well.
Modified the target to be able to be created with an architecture that differs
from the main executable. This is needed for iOS debugging since we can have
an "armv6" binary which can run on an "armv7" machine, so we want to be able
to do:
% lldb
(lldb) platform create remote-ios
(lldb) file --arch armv7 a.out
Where "a.out" is an armv6 executable. The platform then can correctly decide
to open all "armv7" images for all dependent shared libraries.
Modified the disassembly to show the current PC value. Example output:
(lldb) disassemble --frame
a.out`main:
0x1eb7: pushl %ebp
0x1eb8: movl %esp, %ebp
0x1eba: pushl %ebx
0x1ebb: subl $20, %esp
0x1ebe: calll 0x1ec3 ; main + 12 at test.c:18
0x1ec3: popl %ebx
-> 0x1ec4: calll 0x1f12 ; getpid
0x1ec9: movl %eax, 4(%esp)
0x1ecd: leal 199(%ebx), %eax
0x1ed3: movl %eax, (%esp)
0x1ed6: calll 0x1f18 ; printf
0x1edb: leal 213(%ebx), %eax
0x1ee1: movl %eax, (%esp)
0x1ee4: calll 0x1f1e ; puts
0x1ee9: calll 0x1f0c ; getchar
0x1eee: movl $20, (%esp)
0x1ef5: calll 0x1e6a ; sleep_loop at test.c:6
0x1efa: movl $12, %eax
0x1eff: addl $20, %esp
0x1f02: popl %ebx
0x1f03: leave
0x1f04: ret
This can be handy when dealing with the new --line options that was recently
added:
(lldb) disassemble --line
a.out`main + 13 at test.c:19
18 {
-> 19 printf("Process: %i\n\n", getpid());
20 puts("Press any key to continue..."); getchar();
-> 0x1ec4: calll 0x1f12 ; getpid
0x1ec9: movl %eax, 4(%esp)
0x1ecd: leal 199(%ebx), %eax
0x1ed3: movl %eax, (%esp)
0x1ed6: calll 0x1f18 ; printf
Modified the ModuleList to have a lookup based solely on a UUID. Since the
UUID is typically the MD5 checksum of a binary image, there is no need
to give the path and architecture when searching for a pre-existing
image in an image list.
Now that we support remote debugging a bit better, our lldb_private::Module
needs to be able to track what the original path for file was as the platform
knows it, as well as where the file is locally. The module has the two
following functions to retrieve both paths:
const FileSpec &Module::GetFileSpec () const;
const FileSpec &Module::GetPlatformFileSpec () const;
llvm-svn: 128563
2011-03-31 02:16:51 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 05:45:27 +08:00
|
|
|
GDBRemoteCommunication::PacketResult
|
2010-06-09 00:52:24 +08:00
|
|
|
GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
|
|
|
|
{
|
|
|
|
Mutex::Locker locker(m_sequence_mutex);
|
|
|
|
return SendPacketNoLock (payload, payload_length);
|
|
|
|
}
|
|
|
|
|
2013-12-07 05:45:27 +08:00
|
|
|
GDBRemoteCommunication::PacketResult
|
2010-06-09 00:52:24 +08:00
|
|
|
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));
|
|
|
|
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
|
2010-06-09 00:52:24 +08:00
|
|
|
ConnectionStatus status = eConnectionStatusSuccess;
|
|
|
|
size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL);
|
2012-04-10 06:46:21 +08:00
|
|
|
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 ())
|
2013-03-28 07:08:40 +08:00
|
|
|
m_history.Dump (log);
|
2012-04-10 06:46:21 +08:00
|
|
|
|
2013-10-24 02:24:30 +08:00
|
|
|
log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)packet.GetSize(), packet.GetData());
|
2012-04-10 06:46:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written);
|
|
|
|
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (bytes_written == packet.GetSize())
|
|
|
|
{
|
2011-02-12 14:28:37 +08:00
|
|
|
if (GetSendAcks ())
|
2013-12-07 05:45:27 +08:00
|
|
|
return GetAck ();
|
|
|
|
else
|
|
|
|
return PacketResult::Success;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2010-09-15 06:10:43 +08:00
|
|
|
else
|
|
|
|
{
|
2011-02-05 10:25:06 +08:00
|
|
|
if (log)
|
2011-05-20 11:15:54 +08:00
|
|
|
log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData());
|
2010-09-15 06:10:43 +08:00
|
|
|
}
|
2010-09-15 07:36:40 +08:00
|
|
|
}
|
2013-12-07 05:45:27 +08:00
|
|
|
return PacketResult::ErrorSendFailed;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 05:45:27 +08:00
|
|
|
GDBRemoteCommunication::PacketResult
|
2011-03-10 10:26:48 +08:00
|
|
|
GDBRemoteCommunication::GetAck ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-03-22 12:00:09 +08:00
|
|
|
StringExtractorGDBRemote packet;
|
2013-12-07 05:45:27 +08:00
|
|
|
PacketResult result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ());
|
|
|
|
if (result == PacketResult::Success)
|
|
|
|
{
|
|
|
|
if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck)
|
|
|
|
return PacketResult::Success;
|
|
|
|
else
|
|
|
|
return PacketResult::ErrorSendAck;
|
|
|
|
}
|
|
|
|
return result;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-06-09 06:50:40 +08:00
|
|
|
GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-06-01 00:54:51 +08:00
|
|
|
if (IsRunning())
|
2012-06-09 06:50:40 +08:00
|
|
|
return locker.TryLock (m_sequence_mutex, failure_message);
|
2012-06-01 00:54:51 +08:00
|
|
|
|
|
|
|
locker.Lock (m_sequence_mutex);
|
|
|
|
return true;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-23 07:43:18 +08:00
|
|
|
bool
|
|
|
|
GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
|
|
|
|
{
|
|
|
|
return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
|
|
|
|
}
|
|
|
|
|
2013-12-07 05:45:27 +08:00
|
|
|
GDBRemoteCommunication::PacketResult
|
2011-06-17 09:22:15 +08:00
|
|
|
GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
uint8_t buffer[8192];
|
|
|
|
Error error;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
|
2011-07-07 09:59:51 +08:00
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
// Check for a packet from our cache first without trying any reading...
|
|
|
|
if (CheckForPacket (NULL, 0, packet))
|
2013-12-07 05:45:27 +08:00
|
|
|
return PacketResult::Success;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2011-07-03 07:21:06 +08:00
|
|
|
bool timed_out = false;
|
2013-12-07 05:45:27 +08:00
|
|
|
bool disconnected = false;
|
2011-07-03 07:21:06 +08:00
|
|
|
while (IsConnected() && !timed_out)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-07-19 09:13:00 +08:00
|
|
|
lldb::ConnectionStatus status = eConnectionStatusNoConnection;
|
2011-06-17 09:22:15 +08:00
|
|
|
size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
|
2011-07-07 09:59:51 +08:00
|
|
|
|
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64,
|
2011-07-07 09:59:51 +08:00
|
|
|
__PRETTY_FUNCTION__,
|
|
|
|
timeout_usec,
|
|
|
|
Communication::ConnectionStatusAsCString (status),
|
|
|
|
error.AsCString(),
|
2012-09-19 02:04:04 +08:00
|
|
|
(uint64_t)bytes_read);
|
2011-07-07 09:59:51 +08:00
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
if (bytes_read > 0)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
if (CheckForPacket (buffer, bytes_read, packet))
|
2013-12-07 05:45:27 +08:00
|
|
|
return PacketResult::Success;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2011-06-17 09:22:15 +08:00
|
|
|
else
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
switch (status)
|
|
|
|
{
|
2011-07-03 05:07:54 +08:00
|
|
|
case eConnectionStatusTimedOut:
|
2014-05-02 08:45:31 +08:00
|
|
|
case eConnectionStatusInterrupted:
|
2011-07-03 07:21:06 +08:00
|
|
|
timed_out = true;
|
|
|
|
break;
|
|
|
|
case eConnectionStatusSuccess:
|
|
|
|
//printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
|
2011-06-17 09:22:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eConnectionStatusEndOfFile:
|
|
|
|
case eConnectionStatusNoConnection:
|
|
|
|
case eConnectionStatusLostConnection:
|
|
|
|
case eConnectionStatusError:
|
2013-12-07 05:45:27 +08:00
|
|
|
disconnected = true;
|
2011-06-17 09:22:15 +08:00
|
|
|
Disconnect();
|
|
|
|
break;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-07 05:45:27 +08:00
|
|
|
packet.Clear ();
|
|
|
|
if (disconnected)
|
|
|
|
return PacketResult::ErrorDisconnected;
|
|
|
|
if (timed_out)
|
|
|
|
return PacketResult::ErrorReplyTimeout;
|
|
|
|
else
|
|
|
|
return PacketResult::ErrorReplyFailed;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
bool
|
|
|
|
GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
// Put the packet data into the buffer in a thread safe fashion
|
|
|
|
Mutex::Locker locker(m_bytes_mutex);
|
2011-07-03 05:07:54 +08:00
|
|
|
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
|
2011-07-03 05:07:54 +08:00
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
if (src && src_len > 0)
|
2011-07-03 05:07:54 +08:00
|
|
|
{
|
2011-07-03 07:21:06 +08:00
|
|
|
if (log && log->GetVerbose())
|
2011-07-03 05:07:54 +08:00
|
|
|
{
|
|
|
|
StreamString s;
|
2011-07-03 07:21:06 +08:00
|
|
|
log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
|
|
|
|
__FUNCTION__,
|
|
|
|
(uint32_t)src_len,
|
|
|
|
(uint32_t)src_len,
|
|
|
|
src);
|
2011-07-03 05:07:54 +08:00
|
|
|
}
|
2011-06-17 09:22:15 +08:00
|
|
|
m_bytes.append ((const char *)src, src_len);
|
2011-07-03 05:07:54 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// Parse up the packets into gdb remote packets
|
2011-07-03 05:07:54 +08:00
|
|
|
if (!m_bytes.empty())
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
// 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.
|
2011-06-17 09:22:15 +08:00
|
|
|
size_t content_start = 0;
|
|
|
|
size_t content_length = 0;
|
|
|
|
size_t total_length = 0;
|
|
|
|
size_t checksum_idx = std::string::npos;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
switch (m_bytes[0])
|
|
|
|
{
|
|
|
|
case '+': // Look for ack
|
|
|
|
case '-': // Look for cancel
|
|
|
|
case '\x03': // ^C to halt target
|
2011-06-17 09:22:15 +08:00
|
|
|
content_length = total_length = 1; // The command is one byte long...
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
// Look for a standard gdb packet?
|
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
size_t hash_pos = m_bytes.find('#');
|
|
|
|
if (hash_pos != std::string::npos)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-06-17 11:31:01 +08:00
|
|
|
{
|
2011-07-03 05:07:54 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2011-06-17 11:31:01 +08:00
|
|
|
if (log)
|
2011-07-03 05:07:54 +08:00
|
|
|
log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
|
|
|
|
__FUNCTION__, idx, idx, m_bytes.c_str());
|
|
|
|
m_bytes.erase(0, idx);
|
2011-06-17 11:31:01 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
if (content_length == std::string::npos)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
packet.Clear();
|
|
|
|
return false;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2011-06-17 11:31:01 +08:00
|
|
|
else if (total_length > 0)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-06-17 09:22:15 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
// We have a valid packet...
|
2011-06-17 09:22:15 +08:00
|
|
|
assert (content_length <= m_bytes.size());
|
|
|
|
assert (total_length <= m_bytes.size());
|
|
|
|
assert (content_length <= total_length);
|
2014-06-21 04:41:07 +08:00
|
|
|
const size_t content_end = content_start + content_length;
|
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
bool success = true;
|
|
|
|
std::string &packet_str = packet.GetStringRef();
|
2012-04-10 06:46:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
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 ())
|
2013-03-28 07:08:40 +08:00
|
|
|
m_history.Dump (log);
|
2012-04-10 06:46:21 +08:00
|
|
|
|
2014-06-21 04:41:07 +08:00
|
|
|
bool binary = false;
|
|
|
|
// Only detect binary for packets that start with a '$' and have a '#CC' checksum
|
|
|
|
if (m_bytes[0] == '$' && total_length > 4)
|
|
|
|
{
|
|
|
|
for (size_t i=0; !binary && i<total_length; ++i)
|
|
|
|
{
|
|
|
|
if (isprint(m_bytes[i]) == 0)
|
|
|
|
binary = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (binary)
|
|
|
|
{
|
|
|
|
StreamString strm;
|
|
|
|
// Packet header...
|
|
|
|
strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]);
|
|
|
|
for (size_t i=content_start; i<content_end; ++i)
|
|
|
|
{
|
|
|
|
// Remove binary escaped bytes when displaying the packet...
|
|
|
|
const char ch = m_bytes[i];
|
|
|
|
if (ch == 0x7d)
|
|
|
|
{
|
|
|
|
// 0x7d is the escape character. The next character is to
|
|
|
|
// be XOR'd with 0x20.
|
|
|
|
const char escapee = m_bytes[++i] ^ 0x20;
|
|
|
|
strm.Printf("%2.2x", escapee);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strm.Printf("%2.2x", (uint8_t)ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Packet footer...
|
|
|
|
strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]);
|
|
|
|
log->PutCString(strm.GetString().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str());
|
|
|
|
}
|
2012-04-10 06:46:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
|
|
|
|
|
2013-08-28 23:10:37 +08:00
|
|
|
// Clear packet_str in case there is some existing data in it.
|
|
|
|
packet_str.clear();
|
2013-08-28 18:31:52 +08:00
|
|
|
// 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)
|
|
|
|
packet_str.reserve(m_bytes.length());
|
2014-06-21 04:41:07 +08:00
|
|
|
for (std::string::const_iterator c = m_bytes.begin() + content_start; c != m_bytes.begin() + content_end; ++c)
|
2013-08-28 18:31:52 +08:00
|
|
|
{
|
|
|
|
if (*c == '*')
|
|
|
|
{
|
|
|
|
// '*' indicates RLE. Next character will give us the
|
|
|
|
// repeat count and previous character is what is to be
|
|
|
|
// repeated.
|
|
|
|
char char_to_repeat = packet_str.back();
|
|
|
|
// Number of time the previous character is repeated
|
|
|
|
int repeat_count = *++c + 3 - ' ';
|
|
|
|
// We have the char_to_repeat and repeat_count. Now push
|
|
|
|
// it in the packet.
|
|
|
|
for (int i = 0; i < repeat_count; ++i)
|
|
|
|
packet_str.push_back(char_to_repeat);
|
|
|
|
}
|
2014-02-25 03:07:29 +08:00
|
|
|
else if (*c == 0x7d)
|
|
|
|
{
|
|
|
|
// 0x7d is the escape character. The next character is to
|
|
|
|
// be XOR'd with 0x20.
|
|
|
|
char escapee = *++c ^ 0x20;
|
|
|
|
packet_str.push_back(escapee);
|
|
|
|
}
|
2013-08-28 18:31:52 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
packet_str.push_back(*c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
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)
|
2011-09-21 05:44:10 +08:00
|
|
|
log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
|
2011-06-17 09:22:15 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 06:46:21 +08:00
|
|
|
|
2011-06-17 09:22:15 +08:00
|
|
|
m_bytes.erase(0, total_length);
|
|
|
|
packet.SetFilePos(0);
|
|
|
|
return success;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-17 09:22:15 +08:00
|
|
|
packet.Clear();
|
|
|
|
return false;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-04-12 13:54:46 +08:00
|
|
|
Error
|
2013-12-07 01:46:35 +08:00
|
|
|
GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
|
2013-12-06 06:58:22 +08:00
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread))
|
|
|
|
{
|
|
|
|
error.SetErrorString("listen thread already running");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char listen_url[512];
|
|
|
|
if (hostname && hostname[0])
|
2014-02-09 04:29:40 +08:00
|
|
|
snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port);
|
2013-12-06 06:58:22 +08:00
|
|
|
else
|
|
|
|
snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
|
|
|
|
m_listen_url = listen_url;
|
|
|
|
SetConnection(new ConnectionFileDescriptor());
|
|
|
|
m_listen_thread = Host::ThreadCreate (listen_url, GDBRemoteCommunication::ListenThread, this, &error);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GDBRemoteCommunication::JoinListenThread ()
|
|
|
|
{
|
|
|
|
if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread))
|
|
|
|
{
|
|
|
|
Host::ThreadJoin(m_listen_thread, NULL, NULL);
|
|
|
|
m_listen_thread = LLDB_INVALID_HOST_THREAD;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
lldb::thread_result_t
|
|
|
|
GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg)
|
|
|
|
{
|
|
|
|
GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg;
|
|
|
|
Error error;
|
|
|
|
ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection ();
|
|
|
|
|
|
|
|
if (connection)
|
|
|
|
{
|
|
|
|
// Do the listen on another thread so we can continue on...
|
|
|
|
if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess)
|
|
|
|
comm->SetConnection(NULL);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error
|
2014-01-11 06:24:11 +08:00
|
|
|
GDBRemoteCommunication::StartDebugserverProcess (const char *hostname,
|
|
|
|
uint16_t in_port,
|
2013-12-05 03:19:12 +08:00
|
|
|
lldb_private::ProcessLaunchInfo &launch_info,
|
2014-01-11 06:24:11 +08:00
|
|
|
uint16_t &out_port)
|
2011-04-12 13:54:46 +08:00
|
|
|
{
|
2014-07-23 07:41:36 +08:00
|
|
|
Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
|
|
|
|
if (log)
|
|
|
|
log->Printf ("GDBRemoteCommunication::%s(hostname=%s, in_port=%" PRIu16 ", out_port=%" PRIu16, __FUNCTION__, hostname ? hostname : "<empty>", in_port, out_port);
|
|
|
|
|
2014-01-11 06:24:11 +08:00
|
|
|
out_port = in_port;
|
2011-04-12 13:54:46 +08:00
|
|
|
Error error;
|
|
|
|
// If we locate debugserver, keep that located version around
|
|
|
|
static FileSpec g_debugserver_file_spec;
|
|
|
|
|
|
|
|
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)
|
2014-07-23 07:41:36 +08:00
|
|
|
{
|
2011-04-12 13:54:46 +08:00
|
|
|
debugserver_file_spec.SetFile (env_debugserver_path, false);
|
2014-07-23 07:41:36 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path);
|
|
|
|
}
|
2011-04-12 13:54:46 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-07-23 07:41:36 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
|
|
|
|
|
2011-04-12 13:54:46 +08:00
|
|
|
g_debugserver_file_spec = debugserver_file_spec;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-23 07:41:36 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
|
|
|
|
|
2011-04-12 13:54:46 +08:00
|
|
|
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);
|
2013-12-06 06:58:22 +08:00
|
|
|
|
|
|
|
// If a host and port is supplied then use it
|
2014-01-11 06:24:11 +08:00
|
|
|
char host_and_port[128];
|
|
|
|
if (hostname)
|
|
|
|
{
|
|
|
|
snprintf (host_and_port, sizeof(host_and_port), "%s:%u", hostname, in_port);
|
2013-12-06 06:58:22 +08:00
|
|
|
debugserver_args.AppendArgument(host_and_port);
|
2014-01-11 06:24:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
host_and_port[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2011-04-12 13:54:46 +08:00
|
|
|
// 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");
|
2013-12-06 06:58:22 +08:00
|
|
|
|
2013-12-05 03:19:12 +08:00
|
|
|
char named_pipe_path[PATH_MAX];
|
2014-01-25 11:57:13 +08:00
|
|
|
named_pipe_path[0] = '\0';
|
2013-12-05 03:19:12 +08:00
|
|
|
|
2014-01-11 06:24:11 +08:00
|
|
|
bool listen = false;
|
|
|
|
if (host_and_port[0])
|
2011-04-12 13:54:46 +08:00
|
|
|
{
|
2013-12-06 06:58:22 +08:00
|
|
|
// Create a temporary file to get the stdout/stderr and redirect the
|
|
|
|
// output of the command into this file. We will later read this file
|
|
|
|
// if all goes well and fill the data into "command_output_ptr"
|
|
|
|
|
2014-01-11 06:24:11 +08:00
|
|
|
if (in_port == 0)
|
2013-12-05 03:19:12 +08:00
|
|
|
{
|
2014-01-11 06:24:11 +08:00
|
|
|
// Binding to port zero, we need to figure out what port it ends up
|
|
|
|
// using using a named pipe...
|
|
|
|
FileSpec tmpdir_file_spec;
|
|
|
|
if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
|
2013-12-06 06:58:22 +08:00
|
|
|
{
|
2014-01-11 06:24:11 +08:00
|
|
|
tmpdir_file_spec.GetFilename().SetCString("debugserver-named-pipe.XXXXXX");
|
|
|
|
strncpy(named_pipe_path, tmpdir_file_spec.GetPath().c_str(), sizeof(named_pipe_path));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(named_pipe_path, "/tmp/debugserver-named-pipe.XXXXXX", sizeof(named_pipe_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::mktemp (named_pipe_path))
|
|
|
|
{
|
2014-03-12 18:45:23 +08:00
|
|
|
#if defined(_WIN32)
|
2014-01-13 22:55:15 +08:00
|
|
|
if ( false )
|
|
|
|
#else
|
2014-01-11 06:24:11 +08:00
|
|
|
if (::mkfifo(named_pipe_path, 0600) == 0)
|
2014-01-13 22:55:15 +08:00
|
|
|
#endif
|
2014-01-11 06:24:11 +08:00
|
|
|
{
|
|
|
|
debugserver_args.AppendArgument("--named-pipe");
|
|
|
|
debugserver_args.AppendArgument(named_pipe_path);
|
|
|
|
}
|
2013-12-06 06:58:22 +08:00
|
|
|
}
|
2013-12-05 03:19:12 +08:00
|
|
|
}
|
|
|
|
else
|
2014-01-11 06:24:11 +08:00
|
|
|
{
|
|
|
|
listen = true;
|
|
|
|
}
|
2011-04-12 13:54:46 +08:00
|
|
|
}
|
2013-12-05 03:19:12 +08:00
|
|
|
else
|
2013-12-06 06:58:22 +08:00
|
|
|
{
|
|
|
|
// No host and port given, so lets listen on our end and make the debugserver
|
|
|
|
// connect to us..
|
2014-02-28 03:38:18 +08:00
|
|
|
error = StartListenThread ("127.0.0.1", 0);
|
2013-12-06 06:58:22 +08:00
|
|
|
if (error.Fail())
|
|
|
|
return error;
|
|
|
|
|
|
|
|
ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection ();
|
2014-02-28 03:38:18 +08:00
|
|
|
// Wait for 10 seconds to resolve the bound port
|
2014-08-07 02:16:26 +08:00
|
|
|
out_port = connection->GetListeningPort(10);
|
2014-02-28 03:38:18 +08:00
|
|
|
if (out_port > 0)
|
|
|
|
{
|
|
|
|
char port_cstr[32];
|
|
|
|
snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", out_port);
|
|
|
|
// Send the host and port down that debugserver and specify an option
|
|
|
|
// so that it connects back to the port we are listening to in this process
|
|
|
|
debugserver_args.AppendArgument("--reverse-connect");
|
|
|
|
debugserver_args.AppendArgument(port_cstr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error.SetErrorString ("failed to bind to port 0 on 127.0.0.1");
|
|
|
|
return error;
|
|
|
|
}
|
2013-12-06 06:58:22 +08:00
|
|
|
}
|
|
|
|
|
2011-04-12 13:54:46 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close STDIN, STDOUT and STDERR. We might need to redirect them
|
|
|
|
// to "/dev/null" if we run into any problems.
|
2013-12-05 03:19:12 +08:00
|
|
|
launch_info.AppendCloseFileAction (STDIN_FILENO);
|
|
|
|
launch_info.AppendCloseFileAction (STDOUT_FILENO);
|
|
|
|
launch_info.AppendCloseFileAction (STDERR_FILENO);
|
2011-04-12 13:54:46 +08:00
|
|
|
|
|
|
|
error = Host::LaunchProcess(launch_info);
|
2013-12-05 03:19:12 +08:00
|
|
|
|
2014-03-01 04:47:08 +08:00
|
|
|
if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
|
2013-12-05 03:19:12 +08:00
|
|
|
{
|
2014-03-01 04:47:08 +08:00
|
|
|
if (named_pipe_path[0])
|
2013-12-05 03:19:12 +08:00
|
|
|
{
|
2014-03-01 04:47:08 +08:00
|
|
|
File name_pipe_file;
|
|
|
|
error = name_pipe_file.Open(named_pipe_path, File::eOpenOptionRead);
|
|
|
|
if (error.Success())
|
|
|
|
{
|
|
|
|
char port_cstr[256];
|
|
|
|
port_cstr[0] = '\0';
|
|
|
|
size_t num_bytes = sizeof(port_cstr);
|
|
|
|
error = name_pipe_file.Read(port_cstr, num_bytes);
|
|
|
|
assert (error.Success());
|
|
|
|
assert (num_bytes > 0 && port_cstr[num_bytes-1] == '\0');
|
|
|
|
out_port = Args::StringToUInt32(port_cstr, 0);
|
|
|
|
name_pipe_file.Close();
|
|
|
|
}
|
2014-08-16 06:04:21 +08:00
|
|
|
FileSystem::Unlink(named_pipe_path);
|
2014-03-01 04:47:08 +08:00
|
|
|
}
|
|
|
|
else if (listen)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make sure we actually connect with the debugserver...
|
|
|
|
JoinListenThread();
|
2013-12-05 03:19:12 +08:00
|
|
|
}
|
2013-12-06 06:58:22 +08:00
|
|
|
}
|
2011-04-12 13:54:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-26 08:56:27 +08:00
|
|
|
error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
|
2011-04-12 13:54:46 +08:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-04-10 06:46:21 +08:00
|
|
|
void
|
2012-04-14 05:24:18 +08:00
|
|
|
GDBRemoteCommunication::DumpHistory(Stream &strm)
|
2012-04-10 06:46:21 +08:00
|
|
|
{
|
2012-04-14 05:24:18 +08:00
|
|
|
m_history.Dump (strm);
|
2012-04-10 06:46:21 +08:00
|
|
|
}
|