Make log options uniform betwwen lldb-platform and lldb-gdbserver

This change also get rid of an unused Debugger instance in
GDBRemoteCommunicationServerLLGS and the command interpreter from
lldb-platform what was used only for enabling logging.

Differential revision: http://reviews.llvm.org/D9876

llvm-svn: 238319
This commit is contained in:
Tamas Berghammer 2015-05-27 13:34:04 +00:00
parent f5c4dec750
commit 9c9ecce077
11 changed files with 170 additions and 114 deletions

View File

@ -74,6 +74,10 @@ public:
static bool
GetLogChannelCallbacks(const ConstString &channel, Log::Callbacks &log_callbacks);
static bool
EnableLogChannel(lldb::StreamSP &log_stream_sp, uint32_t log_options, const char *channel,
const char **categories, Stream &error_stream);
static void
EnableAllLogChannels(lldb::StreamSP &log_stream_sp, uint32_t log_options, const char **categories,
Stream *feedback_strm);

View File

@ -1275,8 +1275,6 @@ Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
bool
Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
{
Log::Callbacks log_callbacks;
StreamSP log_stream_sp;
if (m_log_callback_stream_sp)
{
@ -1309,33 +1307,7 @@ Debugger::EnableLog (const char *channel, const char **categories, const char *l
if (log_options == 0)
log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
{
log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
return true;
}
else
{
LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
if (log_channel_sp)
{
if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
{
return true;
}
else
{
error_stream.Printf ("Invalid log channel '%s'.\n", channel);
return false;
}
}
else
{
error_stream.Printf ("Invalid log channel '%s'.\n", channel);
return false;
}
}
return false;
return Log::EnableLogChannel(log_stream_sp, log_options, channel, categories, error_stream);
}
SourceManager &

View File

@ -365,6 +365,40 @@ Log::GetLogChannelCallbacks (const ConstString &channel, Log::Callbacks &log_cal
return false;
}
bool
Log::EnableLogChannel(lldb::StreamSP &log_stream_sp,
uint32_t log_options,
const char *channel,
const char **categories,
Stream &error_stream)
{
Log::Callbacks log_callbacks;
if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
{
log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
return true;
}
LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
if (log_channel_sp)
{
if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
{
return true;
}
else
{
error_stream.Printf ("Invalid log channel '%s'.\n", channel);
return false;
}
}
else
{
error_stream.Printf ("Invalid log channel '%s'.\n", channel);
return false;
}
}
void
Log::EnableAllLogChannels
(

View File

@ -24,8 +24,8 @@
#include "llvm/ADT/Triple.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/DataBuffer.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/RegisterValue.h"
#include "lldb/Core/State.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/ConnectionFileDescriptor.h"
@ -40,7 +40,6 @@
#include "lldb/Target/FileAction.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Host/common/NativeRegisterContext.h"
#include "lldb/Host/common/NativeProcessProtocol.h"
#include "lldb/Host/common/NativeThreadProtocol.h"
@ -75,8 +74,7 @@ namespace
// GDBRemoteCommunicationServerLLGS constructor
//----------------------------------------------------------------------
GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS(
const lldb::PlatformSP& platform_sp,
lldb::DebuggerSP &debugger_sp) :
const lldb::PlatformSP& platform_sp) :
GDBRemoteCommunicationServerCommon ("gdb-remote.server", "gdb-remote.server.rx_packet"),
m_platform_sp (platform_sp),
m_async_thread (LLDB_INVALID_HOST_THREAD),
@ -84,7 +82,6 @@ GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS(
m_continue_tid (LLDB_INVALID_THREAD_ID),
m_debugged_process_mutex (Mutex::eMutexTypeRecursive),
m_debugged_process_sp (),
m_debugger_sp (debugger_sp),
m_stdio_communication ("process.stdio"),
m_inferior_prev_state (StateType::eStateInvalid),
m_active_auxv_buffer_sp (),
@ -93,7 +90,6 @@ GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS(
m_next_saved_registers_id (1)
{
assert(platform_sp);
assert(debugger_sp && "must specify non-NULL debugger_sp for lldb-gdbserver");
RegisterPacketHandlers();
}

View File

@ -38,8 +38,7 @@ public:
//------------------------------------------------------------------
// Constructors and Destructors
//------------------------------------------------------------------
GDBRemoteCommunicationServerLLGS(const lldb::PlatformSP& platform_sp,
lldb::DebuggerSP& debugger_sp);
GDBRemoteCommunicationServerLLGS(const lldb::PlatformSP& platform_sp);
virtual
~GDBRemoteCommunicationServerLLGS();
@ -119,7 +118,6 @@ protected:
lldb::tid_t m_continue_tid;
Mutex m_debugged_process_mutex;
NativeProcessProtocolSP m_debugged_process_sp;
lldb::DebuggerSP m_debugger_sp;
Communication m_stdio_communication;
lldb::StateType m_inferior_prev_state;
lldb::DataBufferSP m_active_auxv_buffer_sp;

View File

@ -22,17 +22,19 @@ include(../../cmake/LLDBDependencies.cmake)
if (BUILD_SHARED_LIBS )
add_lldb_executable(lldb-server
lldb-server.cpp
lldb-gdbserver.cpp
lldb-platform.cpp
lldb-server.cpp
LLDBServerUtilities.cpp
)
target_link_libraries(lldb-server liblldb)
else()
add_lldb_executable(lldb-server
lldb-server.cpp
lldb-gdbserver.cpp
lldb-platform.cpp
lldb-server.cpp
LLDBServerUtilities.cpp
../../source/lldb.cpp
)

View File

@ -0,0 +1,67 @@
//===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "LLDBServerUtilities.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Interpreter/Args.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
using namespace lldb;
using namespace lldb_private::lldb_server;
using namespace llvm;
bool
LLDBServerUtilities::SetupLogging(const std::string& log_file,
const StringRef& log_channels,
uint32_t log_options)
{
lldb::StreamSP log_stream_sp;
if (log_file.empty())
{
log_stream_sp.reset(new StreamFile(stdout, false));
}
else
{
uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate |
File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
if (!(log_options & LLDB_LOG_OPTION_APPEND))
options |= File::eOpenOptionTruncate;
log_stream_sp.reset(new StreamFile(log_file.c_str(), options));
}
SmallVector<StringRef, 32> channel_array;
log_channels.split(channel_array, ":");
for (auto channel_with_categories : channel_array)
{
StreamString error_stream;
Args channel_then_categories(channel_with_categories);
std::string channel(channel_then_categories.GetArgumentAtIndex(0));
channel_then_categories.Shift (); // Shift off the channel
bool success = Log::EnableLogChannel(log_stream_sp,
log_options,
channel.c_str(),
channel_then_categories.GetConstArgumentVector(),
error_stream);
if (!success)
{
fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n",
log_file.c_str(),
channel_with_categories.str().c_str());
return false;
}
}
return true;
}

View File

@ -0,0 +1,25 @@
//===-- LLDBServerUtilities.h -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringRef.h"
#include <string>
namespace lldb_private {
namespace lldb_server {
class LLDBServerUtilities
{
public:
static bool
SetupLogging(const std::string& log_file, const llvm::StringRef& log_channels, uint32_t log_options);
};
}
}

View File

@ -25,20 +25,18 @@
// Other libraries and framework includes
#include "llvm/ADT/StringRef.h"
#include "lldb/Core/Error.h"
#include "lldb/Core/ConnectionMachPort.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Error.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/HostThread.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Host/OptionParser.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Host/Socket.h"
#include "lldb/Host/StringConvert.h"
#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Target/Platform.h"
#include "LLDBServerUtilities.h"
#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h"
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
@ -53,6 +51,7 @@
using namespace llvm;
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::lldb_server;
using namespace lldb_private::process_gdb_remote;
// lldb-gdbserver state
@ -126,9 +125,8 @@ static void
display_usage (const char *progname, const char* subcommand)
{
fprintf(stderr, "Usage:\n %s %s "
"[--log-file log-file-path] "
"[--log-flags flags] "
"[--lldb-command command]* "
"[--log-file log-file-name] "
"[--log-channels log-channel-list] "
"[--platform platform_name] "
"[--setsid] "
"[--named-pipe named-pipe-path] "
@ -541,7 +539,6 @@ main_gdbserver (int argc, char *argv[])
argc--;
argv++;
int long_option_index = 0;
Args log_args;
Error error;
int ch;
std::string platform_name;
@ -552,12 +549,6 @@ main_gdbserver (int argc, char *argv[])
int unnamed_pipe_fd = -1;
bool reverse_connect = false;
lldb::DebuggerSP debugger_sp = Debugger::CreateInstance ();
debugger_sp->SetInputFileHandle(stdin, false);
debugger_sp->SetOutputFileHandle(stdout, false);
debugger_sp->SetErrorFileHandle(stderr, false);
// ProcessLaunchInfo launch_info;
ProcessAttachInfo attach_info;
@ -572,8 +563,6 @@ main_gdbserver (int argc, char *argv[])
std::string short_options(OptionParser::GetShortOptionString(g_long_options));
std::vector<std::string> lldb_commands;
while ((ch = getopt_long_only(argc, argv, short_options.c_str(), g_long_options, &long_option_index)) != -1)
{
switch (ch)
@ -655,28 +644,8 @@ main_gdbserver (int argc, char *argv[])
exit(option_error);
}
SmallVector<StringRef, 32> channel_array;
log_channels.split(channel_array, ":");
uint32_t log_options = 0;
for (auto channel_with_categories : channel_array)
{
StreamString error_stream;
Args channel_then_categories(channel_with_categories);
std::string channel(channel_then_categories.GetArgumentAtIndex(0));
channel_then_categories.Shift (); // Shift off the channel
bool success = debugger_sp->EnableLog (channel.c_str(),
channel_then_categories.GetConstArgumentVector(),
log_file.c_str(),
log_options,
error_stream);
if (!success)
{
fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"",
log_file.c_str(),
channel_with_categories.str().c_str());
return -1;
}
}
if (!LLDBServerUtilities::SetupLogging(log_file, log_channels, 0))
return -1;
Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_VERBOSE));
if (log)
@ -701,7 +670,7 @@ main_gdbserver (int argc, char *argv[])
// Setup the platform that GDBRemoteCommunicationServerLLGS will use.
lldb::PlatformSP platform_sp = setup_platform (platform_name);
GDBRemoteCommunicationServerLLGS gdb_server (platform_sp, debugger_sp);
GDBRemoteCommunicationServerLLGS gdb_server (platform_sp);
const char *const host_and_port = argv[0];
argc -= 1;

View File

@ -25,21 +25,19 @@
// Other libraries and framework includes
#include "lldb/Core/Error.h"
#include "lldb/Core/ConnectionMachPort.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/HostGetOpt.h"
#include "lldb/Host/OptionParser.h"
#include "lldb/Host/Socket.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "LLDBServerUtilities.h"
#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h"
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::lldb_server;
using namespace lldb_private::process_gdb_remote;
using namespace llvm;
//----------------------------------------------------------------------
// option descriptors for getopt_long_only()
@ -53,12 +51,13 @@ 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-channels", required_argument, NULL, 'c' },
{ "listen", required_argument, NULL, 'L' },
{ "port-offset", required_argument, NULL, 'p' },
{ "gdbserver-port", required_argument, NULL, 'P' },
{ "min-gdbserver-port", required_argument, NULL, 'm' },
{ "max-gdbserver-port", required_argument, NULL, 'M' },
{ "lldb-command", required_argument, NULL, 'c' },
{ "server", no_argument, &g_server, 1 },
{ NULL, 0, NULL, 0 }
};
@ -71,7 +70,6 @@ static struct option g_long_options[] =
#define HIGH_PORT (49151u)
#endif
//----------------------------------------------------------------------
// Watch for signals
//----------------------------------------------------------------------
@ -93,8 +91,7 @@ signal_handler(int signo)
static void
display_usage (const char *progname, const char *subcommand)
{
fprintf(stderr, "Usage:\n %s %s [--server] --listen port\n",
progname, subcommand);
fprintf(stderr, "Usage:\n %s %s [--log-file log-file-name] [--log-channels log-channel-list] --server --listen port\n", progname, subcommand);
exit(0);
}
@ -115,18 +112,14 @@ main_platform (int argc, char *argv[])
std::string listen_host_port;
int ch;
lldb::DebuggerSP debugger_sp = Debugger::CreateInstance ();
debugger_sp->SetInputFileHandle(stdin, false);
debugger_sp->SetOutputFileHandle(stdout, false);
debugger_sp->SetErrorFileHandle(stderr, false);
std::string log_file;
StringRef log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap;
int min_gdbserver_port = 0;
int max_gdbserver_port = 0;
uint16_t port_offset = 0;
std::vector<std::string> lldb_commands;
bool show_usage = false;
int option_error = 0;
int socket_error = -1;
@ -151,6 +144,16 @@ main_platform (int argc, char *argv[])
listen_host_port.append (optarg);
break;
case 'l': // Set Log File
if (optarg && optarg[0])
log_file.assign(optarg);
break;
case 'c': // Log Channels
if (optarg && optarg[0])
log_channels = StringRef(optarg);
break;
case 'p':
{
char *end = NULL;
@ -205,10 +208,6 @@ main_platform (int argc, char *argv[])
}
}
break;
case 'c':
lldb_commands.push_back(optarg);
break;
case 'h': /* fall-through is intentional */
case '?':
@ -217,6 +216,9 @@ main_platform (int argc, char *argv[])
}
}
if (!LLDBServerUtilities::SetupLogging(log_file, log_channels, 0))
return -1;
// Make a port map for a port range that was specified.
if (min_gdbserver_port < max_gdbserver_port)
{
@ -227,7 +229,6 @@ main_platform (int argc, char *argv[])
{
fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port);
option_error = 3;
}
// Print usage and exit if no listening port is specified.
@ -240,17 +241,6 @@ main_platform (int argc, char *argv[])
exit(option_error);
}
// Execute any LLDB commands that we were asked to evaluate.
for (const auto &lldb_command : lldb_commands)
{
lldb_private::CommandReturnObject result;
printf("(lldb) %s\n", lldb_command.c_str());
debugger_sp->GetCommandInterpreter().HandleCommand(lldb_command.c_str(), eLazyBoolNo, result);
const char *output = result.GetOutputData();
if (output && output[0])
puts(output);
}
std::unique_ptr<Socket> listening_socket_up;
Socket *socket = nullptr;
const bool children_inherit_listen_socket = false;

View File

@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Debugger.h"
#include "lldb/Initialization/SystemLifetimeManager.h"
#include "lldb/Initialization/SystemInitializerCommon.h"