#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lldb/Core/Error.h"
#include "lldb/Core/ConnectionFileDescriptor.h"
#include "lldb/Core/ConnectionMachPort.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/StreamFile.h"
#include "GDBRemoteCommunicationServer.h"
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
using namespace lldb;
using namespace lldb_private;
int g_debug = 0;
int g_verbose = 0;
static struct option g_long_options[] =
{
{ "debug", no_argument, &g_debug, 1 },
{ "verbose", no_argument, &g_verbose, 1 },
{ "log-file", required_argument, NULL, 'l' },
{ "log-flags", required_argument, NULL, 'f' },
{ "listen", required_argument, NULL, 'L' },
{ NULL, 0, NULL, 0 }
};
int g_sigpipe_received = 0;
void
signal_handler(int signo)
{
switch (signo)
{
case SIGPIPE:
g_sigpipe_received = 1;
break;
}
}
int
main (int argc, char *argv[])
{
signal (SIGPIPE, signal_handler);
int long_option_index = 0;
StreamSP log_stream_sp;
Args log_args;
Error error;
std::string listen_host_port;
char ch;
Debugger::Initialize();
while ((ch = getopt_long(argc, argv, "l:f:L:", g_long_options, &long_option_index)) != -1)
{
switch (ch)
{
case 0: break;
case 'l': if (optarg && optarg[0])
{
if ((strcasecmp(optarg, "stdout") == 0) || (strcmp(optarg, "/dev/stdout") == 0))
{
log_stream_sp.reset (new StreamFile (stdout, false));
}
else if ((strcasecmp(optarg, "stderr") == 0) || (strcmp(optarg, "/dev/stderr") == 0))
{
log_stream_sp.reset (new StreamFile (stderr, false));
}
else
{
FILE *log_file = fopen(optarg, "w");
if (log_file)
{
setlinebuf(log_file);
log_stream_sp.reset (new StreamFile (log_file, true));
}
else
{
const char *errno_str = strerror(errno);
fprintf (stderr, "Failed to open log file '%s' for writing: errno = %i (%s)", optarg, errno, errno_str ? errno_str : "unknown error");
}
}
}
break;
case 'f': if (optarg && optarg[0])
log_args.AppendArgument(optarg);
break;
case 'L':
listen_host_port.append (optarg);
break;
}
}
if (log_stream_sp)
{
if (log_args.GetArgumentCount() == 0)
log_args.AppendArgument("default");
ProcessGDBRemoteLog::EnableLog (log_stream_sp, 0,log_args, log_stream_sp.get());
}
argc -= optind;
argv += optind;
GDBRemoteCommunicationServer gdb_server (true);
if (!listen_host_port.empty())
{
std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
if (conn_ap.get())
{
std::string connect_url ("listen://");
connect_url.append(listen_host_port.c_str());
printf ("Listening for a connection on %s...\n", listen_host_port.c_str());
if (conn_ap->Connect(connect_url.c_str(), &error) == eConnectionStatusSuccess)
{
printf ("Connection established.\n");
gdb_server.SetConnection (conn_ap.release());
}
}
}
if (gdb_server.IsConnected())
{
if (gdb_server.HandshakeWithClient(&error))
{
bool interrupt = false;
bool done = false;
while (!interrupt && !done)
{
if (!gdb_server.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done))
break;
}
if (error.Fail())
{
fprintf(stderr, "error: %s\n", error.AsCString());
}
}
else
{
fprintf(stderr, "error: handshake with client failed\n");
}
}
Debugger::Terminate();
return 0;
}