llvm-project/lldb/tools/lldb-platform/lldb-platform.cpp

225 lines
6.7 KiB
C++
Raw Normal View History

2011-03-22 09:34:44 +08:00
//===-- lldb-platform.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
2011-03-22 09:34:44 +08:00
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// C++ Includes
// Other libraries and framework includes
#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;
2011-03-22 09:34:44 +08:00
//----------------------------------------------------------------------
// option descriptors for getopt_long()
//----------------------------------------------------------------------
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' },
2011-03-22 09:34:44 +08:00
{ NULL, 0, NULL, 0 }
};
//----------------------------------------------------------------------
// Watch for signals
//----------------------------------------------------------------------
int g_sigpipe_received = 0;
void
signal_handler(int signo)
{
switch (signo)
{
case SIGPIPE:
g_sigpipe_received = 1;
break;
}
}
//----------------------------------------------------------------------
// main
//----------------------------------------------------------------------
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_post;
2011-03-22 09:34:44 +08:00
char ch;
Debugger::Initialize();
// ConnectionMachPort a;
// ConnectionMachPort b;
//
// lldb::ConnectionStatus status;
// const char *bootstrap_service_name = "HelloWorld";
// status = a.BootstrapCheckIn(bootstrap_service_name, &error);
//
// if (status != eConnectionStatusSuccess)
// {
// fprintf(stderr, "%s", error.AsCString());
// return 1;
// }
// status = b.BootstrapLookup (bootstrap_service_name, &error);
// if (status != eConnectionStatusSuccess)
// {
// fprintf(stderr, "%s", error.AsCString());
// return 2;
// }
//
// if (a.Write ("hello", 5, status, &error) == 5)
// {
// char buf[32];
// memset(buf, 0, sizeof(buf));
// if (b.Read (buf, 5, status, &error))
// {
// printf("read returned bytes: %s", buf);
// }
// else
// {
// fprintf(stderr, "%s", error.AsCString());
// return 4;
// }
// }
// else
// {
// fprintf(stderr, "%s", error.AsCString());
// return 3;
// }
while ((ch = getopt_long(argc, argv, "l:f:L:", g_long_options, &long_option_index)) != -1)
2011-03-22 09:34:44 +08:00
{
// DNBLogDebug("option: ch == %c (0x%2.2x) --%s%c%s\n",
// ch, (uint8_t)ch,
// g_long_options[long_option_index].name,
// g_long_options[long_option_index].has_arg ? '=' : ' ',
// optarg ? optarg : "");
switch (ch)
{
case 0: // Any optional that auto set themselves will return 0
break;
case 'l': // Set Log File
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));
}
2011-03-22 09:34:44 +08:00
else
{
FILE *log_file = fopen(optarg, "w");
if (log_file)
{
2011-03-22 09:34:44 +08:00
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");
}
2011-03-22 09:34:44 +08:00
}
}
break;
case 'f': // Log Flags
if (optarg && optarg[0])
log_args.AppendArgument(optarg);
2011-03-22 09:34:44 +08:00
break;
case 'L':
listen_host_post.append (optarg);
break;
2011-03-22 09:34:44 +08:00
}
}
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());
}
2011-03-22 09:34:44 +08:00
// Skip any options we consumed with getopt_long
argc -= optind;
argv += optind;
Moved the execution context that was in the Debugger into the CommandInterpreter where it was always being used. Make sure that Modules can track their object file offsets correctly to allow opening of sub object files (like the "__commpage" on darwin). Modified the Platforms to be able to launch processes. The first part of this move is the platform soon will become the entity that launches your program and when it does, it uses a new ProcessLaunchInfo class which encapsulates all process launching settings. This simplifies the internal APIs needed for launching. I want to slowly phase out process launching from the process classes, so for now we can still launch just as we used to, but eventually the platform is the object that should do the launching. Modified the Host::LaunchProcess in the MacOSX Host.mm to correctly be able to launch processes with all of the new eLaunchFlag settings. Modified any code that was manually launching processes to use the Host::LaunchProcess functions. Fixed an issue where lldb_private::Args had implicitly defined copy constructors that could do the wrong thing. This has now been fixed by adding an appropriate copy constructor and assignment operator. Make sure we don't add empty ModuleSP entries to a module list. Fixed the commpage module creation on MacOSX, but we still need to train the MacOSX dynamic loader to not get rid of it when it doesn't have an entry in the all image infos. Abstracted many more calls from in ProcessGDBRemote down into the GDBRemoteCommunicationClient subclass to make the classes cleaner and more efficient. Fixed the default iOS ARM register context to be correct and also added support for targets that don't support the qThreadStopInfo packet by selecting the current thread (only if needed) and then sending a stop reply packet. Debugserver can now start up with a --unix-socket (-u for short) and can then bind to port zero and send the port it bound to to a listening process on the other end. This allows the GDB remote platform to spawn new GDB server instances (debugserver) to allow platform debugging. llvm-svn: 129351
2011-04-12 13:54:46 +08:00
GDBRemoteCommunicationServer gdb_server (true);
if (!listen_host_post.empty())
{
std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
if (conn_ap.get())
{
std::string connect_url ("listen://");
connect_url.append(listen_host_post.c_str());
printf ("Listening for a connection on %s...\n", listen_host_post.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())
{
// After we connected, we need to get an initial ack from...
if (gdb_server.HandshakeWithClient(&error))
{
bool interrupt = false;
bool done = false;
while (!interrupt && !done)
{
if (!gdb_server.GetPacketAndSendResponse(NULL, error, interrupt, done))
break;
}
}
else
{
fprintf(stderr, "error: handshake with client failed\n");
}
}
2011-03-22 09:34:44 +08:00
Debugger::Terminate();
2011-03-22 09:34:44 +08:00
return 0;
}