Index: lldb/trunk/include/lldb/Core/Log.h =================================================================== --- lldb/trunk/include/lldb/Core/Log.h +++ lldb/trunk/include/lldb/Core/Log.h @@ -74,6 +74,10 @@ 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); Index: lldb/trunk/source/Core/Debugger.cpp =================================================================== --- lldb/trunk/source/Core/Debugger.cpp +++ lldb/trunk/source/Core/Debugger.cpp @@ -1275,8 +1275,6 @@ 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 @@ 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 & Index: lldb/trunk/source/Core/Log.cpp =================================================================== --- lldb/trunk/source/Core/Log.cpp +++ lldb/trunk/source/Core/Log.cpp @@ -365,6 +365,40 @@ 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 ( Index: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h =================================================================== --- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h +++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h @@ -38,8 +38,7 @@ //------------------------------------------------------------------ // 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 @@ 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; Index: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp =================================================================== --- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp +++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp @@ -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 @@ // 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 @@ 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 @@ m_next_saved_registers_id (1) { assert(platform_sp); - assert(debugger_sp && "must specify non-NULL debugger_sp for lldb-gdbserver"); RegisterPacketHandlers(); } Index: lldb/trunk/tools/lldb-server/CMakeLists.txt =================================================================== --- lldb/trunk/tools/lldb-server/CMakeLists.txt +++ lldb/trunk/tools/lldb-server/CMakeLists.txt @@ -22,17 +22,19 @@ 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 ) Index: lldb/trunk/tools/lldb-server/LLDBServerUtilities.h =================================================================== --- lldb/trunk/tools/lldb-server/LLDBServerUtilities.h +++ lldb/trunk/tools/lldb-server/LLDBServerUtilities.h @@ -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 + +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); +}; + +} +} Index: lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp =================================================================== --- lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp +++ lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp @@ -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 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; +} Index: lldb/trunk/tools/lldb-server/lldb-gdbserver.cpp =================================================================== --- lldb/trunk/tools/lldb-server/lldb-gdbserver.cpp +++ lldb/trunk/tools/lldb-server/lldb-gdbserver.cpp @@ -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 @@ 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 @@ argc--; argv++; int long_option_index = 0; - Args log_args; Error error; int ch; std::string platform_name; @@ -552,12 +549,6 @@ 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 @@ std::string short_options(OptionParser::GetShortOptionString(g_long_options)); - std::vector 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 @@ exit(option_error); } - SmallVector 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 @@ // 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; Index: lldb/trunk/tools/lldb-server/lldb-platform.cpp =================================================================== --- lldb/trunk/tools/lldb-server/lldb-platform.cpp +++ lldb/trunk/tools/lldb-server/lldb-platform.cpp @@ -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 @@ { { "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 @@ #define HIGH_PORT (49151u) #endif - //---------------------------------------------------------------------- // Watch for signals //---------------------------------------------------------------------- @@ -93,8 +91,7 @@ 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 @@ 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 lldb_commands; bool show_usage = false; int option_error = 0; int socket_error = -1; @@ -151,6 +144,16 @@ 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 @@ } } break; - - case 'c': - lldb_commands.push_back(optarg); - break; case 'h': /* fall-through is intentional */ case '?': @@ -217,6 +216,9 @@ } } + 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 @@ { 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 @@ 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 listening_socket_up; Socket *socket = nullptr; const bool children_inherit_listen_socket = false; Index: lldb/trunk/tools/lldb-server/lldb-server.cpp =================================================================== --- lldb/trunk/tools/lldb-server/lldb-server.cpp +++ lldb/trunk/tools/lldb-server/lldb-server.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "lldb/Core/Debugger.h" #include "lldb/Initialization/SystemLifetimeManager.h" #include "lldb/Initialization/SystemInitializerCommon.h"