2013-11-22 02:39:32 +08:00
//===-- lldb-gdbserver.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
# include <errno.h>
# include <stdint.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
2014-07-13 01:34:24 +08:00
2014-07-12 09:12:44 +08:00
# ifndef _WIN32
2014-07-13 01:34:24 +08:00
# include <signal.h>
2014-07-12 09:12:44 +08:00
# include <unistd.h>
# endif
2013-11-22 02:39:32 +08:00
// C++ Includes
// Other libraries and framework includes
2015-05-10 23:22:09 +08:00
# include "llvm/ADT/StringRef.h"
2015-05-27 21:34:04 +08:00
# include "lldb/Core/Error.h"
2014-01-28 08:34:23 +08:00
# include "lldb/Core/PluginManager.h"
2014-10-07 05:22:36 +08:00
# include "lldb/Host/ConnectionFileDescriptor.h"
2015-10-01 15:45:58 +08:00
# include "lldb/Host/HostGetOpt.h"
2013-11-23 09:58:15 +08:00
# include "lldb/Host/OptionParser.h"
2015-05-27 21:34:04 +08:00
# include "lldb/Host/Pipe.h"
2014-08-07 02:16:26 +08:00
# include "lldb/Host/Socket.h"
2015-01-16 04:08:35 +08:00
# include "lldb/Host/StringConvert.h"
2015-05-27 21:34:04 +08:00
# include "lldb/Target/Platform.h"
2015-10-22 03:34:26 +08:00
# include "Acceptor.h"
2015-05-27 21:34:04 +08:00
# include "LLDBServerUtilities.h"
2015-02-11 18:29:30 +08:00
# include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h"
2013-11-22 02:39:32 +08:00
# include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
2014-01-28 08:34:23 +08:00
2014-07-12 06:50:13 +08:00
# ifndef LLGS_PROGRAM_NAME
2015-03-06 22:36:33 +08:00
# define LLGS_PROGRAM_NAME "lldb-server"
2014-07-12 06:50:13 +08:00
# endif
# ifndef LLGS_VERSION_STR
# define LLGS_VERSION_STR "local_build"
# endif
2015-05-10 23:22:09 +08:00
using namespace llvm ;
2013-11-22 02:39:32 +08:00
using namespace lldb ;
using namespace lldb_private ;
2015-05-27 21:34:04 +08:00
using namespace lldb_private : : lldb_server ;
2015-03-31 17:52:22 +08:00
using namespace lldb_private : : process_gdb_remote ;
2013-11-22 02:39:32 +08:00
//----------------------------------------------------------------------
// option descriptors for getopt_long_only()
//----------------------------------------------------------------------
2015-02-18 23:39:41 +08:00
static int g_debug = 0 ;
static int g_verbose = 0 ;
2013-11-22 02:39:32 +08:00
static struct option g_long_options [ ] =
{
{ " debug " , no_argument , & g_debug , 1 } ,
2014-01-28 08:34:23 +08:00
{ " platform " , required_argument , NULL , ' p ' } ,
2013-11-22 02:39:32 +08:00
{ " verbose " , no_argument , & g_verbose , 1 } ,
{ " log-file " , required_argument , NULL , ' l ' } ,
2015-05-10 23:22:09 +08:00
{ " log-channels " , required_argument , NULL , ' c ' } ,
2014-07-01 05:05:18 +08:00
{ " attach " , required_argument , NULL , ' a ' } ,
2015-04-28 07:20:30 +08:00
{ " named-pipe " , required_argument , NULL , ' N ' } ,
{ " pipe " , required_argument , NULL , ' U ' } ,
2014-07-13 01:34:24 +08:00
{ " native-regs " , no_argument , NULL , ' r ' } , // Specify to use the native registers instead of the gdb defaults for the architecture. NOTE: this is a do-nothing arg as it's behavior is default now. FIXME remove call from lldb-platform.
2014-07-27 04:39:17 +08:00
{ " reverse-connect " , no_argument , NULL , ' R ' } , // Specifies that llgs attaches to the client address:port rather than llgs listening for a connection from address on port.
2014-07-12 09:12:44 +08:00
{ " setsid " , no_argument , NULL , ' S ' } , // Call setsid() to make llgs run in its own session.
2013-11-22 02:39:32 +08:00
{ NULL , 0 , NULL , 0 }
} ;
//----------------------------------------------------------------------
// Watch for signals
//----------------------------------------------------------------------
2014-07-01 05:05:18 +08:00
static int g_sigpipe_received = 0 ;
static int g_sighup_received_count = 0 ;
2014-07-13 01:34:24 +08:00
# ifndef _WIN32
2015-02-18 23:39:41 +08:00
static void
2013-11-22 02:39:32 +08:00
signal_handler ( int signo )
{
2014-07-01 05:05:18 +08:00
Log * log ( GetLogIfAnyCategoriesSet ( LIBLLDB_LOG_PROCESS ) ) ;
2015-03-06 22:36:33 +08:00
fprintf ( stderr , " lldb-server:%s received signal %d \n " , __FUNCTION__ , signo ) ;
2014-07-01 05:05:18 +08:00
if ( log )
2015-03-06 22:36:33 +08:00
log - > Printf ( " lldb-server:%s received signal %d " , __FUNCTION__ , signo ) ;
2014-07-01 05:05:18 +08:00
2013-11-22 02:39:32 +08:00
switch ( signo )
{
case SIGPIPE :
g_sigpipe_received = 1 ;
break ;
}
}
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
static void
sighup_handler ( MainLoopBase & mainloop )
{
+ + g_sighup_received_count ;
Log * log ( GetLogIfAnyCategoriesSet ( LIBLLDB_LOG_PROCESS ) ) ;
if ( log )
log - > Printf ( " lldb-server:%s swallowing SIGHUP (receive count=%d) " , __FUNCTION__ , g_sighup_received_count ) ;
if ( g_sighup_received_count > = 2 )
mainloop . RequestTermination ( ) ;
}
2014-07-13 01:34:24 +08:00
# endif // #ifndef _WIN32
2013-11-22 02:39:32 +08:00
static void
2015-02-18 23:39:41 +08:00
display_usage ( const char * progname , const char * subcommand )
2013-11-22 02:39:32 +08:00
{
2015-04-28 07:20:30 +08:00
fprintf ( stderr , " Usage: \n %s %s "
2015-05-27 21:34:04 +08:00
" [--log-file log-file-name] "
" [--log-channels log-channel-list] "
2015-04-28 07:20:30 +08:00
" [--platform platform_name] "
" [--setsid] "
" [--named-pipe named-pipe-path] "
" [--native-regs] "
" [--attach pid] "
" [[HOST]:PORT] "
2015-02-18 23:39:41 +08:00
" [-- PROGRAM ARG1 ARG2 ...] \n " , progname , subcommand ) ;
2013-11-22 02:39:32 +08:00
exit ( 0 ) ;
}
2014-01-28 08:34:23 +08:00
static void
dump_available_platforms ( FILE * output_file )
{
fprintf ( output_file , " Available platform plugins: \n " ) ;
for ( int i = 0 ; ; + + i )
{
const char * plugin_name = PluginManager : : GetPlatformPluginNameAtIndex ( i ) ;
const char * plugin_desc = PluginManager : : GetPlatformPluginDescriptionAtIndex ( i ) ;
if ( ! plugin_name | | ! plugin_desc )
break ;
fprintf ( output_file , " %s \t %s \n " , plugin_name , plugin_desc ) ;
}
2014-09-20 04:11:50 +08:00
if ( Platform : : GetHostPlatform ( ) )
2014-01-28 08:34:23 +08:00
{
// add this since the default platform doesn't necessarily get registered by
// the plugin name (e.g. 'host' doesn't show up as a
// registered platform plugin even though it's the default).
2014-09-20 04:11:50 +08:00
fprintf ( output_file , " %s \t Default platform for this host. \n " , Platform : : GetHostPlatform ( ) - > GetPluginName ( ) . AsCString ( ) ) ;
2014-01-28 08:34:23 +08:00
}
}
2014-07-01 05:05:18 +08:00
static lldb : : PlatformSP
2014-09-21 01:34:48 +08:00
setup_platform ( const std : : string & platform_name )
2014-07-01 05:05:18 +08:00
{
lldb : : PlatformSP platform_sp ;
if ( platform_name . empty ( ) )
{
printf ( " using the default platform: " ) ;
2014-09-20 04:11:50 +08:00
platform_sp = Platform : : GetHostPlatform ( ) ;
2014-07-01 05:05:18 +08:00
printf ( " %s \n " , platform_sp - > GetPluginName ( ) . AsCString ( ) ) ;
return platform_sp ;
}
Error error ;
2014-09-21 01:34:48 +08:00
platform_sp = Platform : : Create ( lldb_private : : ConstString ( platform_name ) , error ) ;
2014-07-01 05:05:18 +08:00
if ( error . Fail ( ) )
{
// the host platform isn't registered with that name (at
// least, not always. Check if the given name matches
// the default platform name. If so, use it.
2014-09-20 04:11:50 +08:00
if ( Platform : : GetHostPlatform ( ) & & ( Platform : : GetHostPlatform ( ) - > GetPluginName ( ) = = ConstString ( platform_name . c_str ( ) ) ) )
2014-07-01 05:05:18 +08:00
{
2014-09-20 04:11:50 +08:00
platform_sp = Platform : : GetHostPlatform ( ) ;
2014-07-01 05:05:18 +08:00
}
else
{
fprintf ( stderr , " error: failed to create platform with name '%s' \n " , platform_name . c_str ( ) ) ;
dump_available_platforms ( stderr ) ;
exit ( 1 ) ;
}
}
printf ( " using platform: %s \n " , platform_name . c_str ( ) ) ;
return platform_sp ;
}
void
2015-02-11 18:29:30 +08:00
handle_attach_to_pid ( GDBRemoteCommunicationServerLLGS & gdb_server , lldb : : pid_t pid )
2014-07-01 05:05:18 +08:00
{
Error error = gdb_server . AttachToProcess ( pid ) ;
if ( error . Fail ( ) )
{
fprintf ( stderr , " error: failed to attach to pid % " PRIu64 " : %s \n " , pid , error . AsCString ( ) ) ;
exit ( 1 ) ;
}
}
void
2015-02-11 18:29:30 +08:00
handle_attach_to_process_name ( GDBRemoteCommunicationServerLLGS & gdb_server , const std : : string & process_name )
2014-07-01 05:05:18 +08:00
{
// FIXME implement.
}
void
2015-02-11 18:29:30 +08:00
handle_attach ( GDBRemoteCommunicationServerLLGS & gdb_server , const std : : string & attach_target )
2014-07-01 05:05:18 +08:00
{
assert ( ! attach_target . empty ( ) & & " attach_target cannot be empty " ) ;
2015-06-18 13:27:05 +08:00
// First check if the attach_target is convertible to a long. If so, we'll use it as a pid.
2014-07-01 05:05:18 +08:00
char * end_p = nullptr ;
const long int pid = strtol ( attach_target . c_str ( ) , & end_p , 10 ) ;
// We'll call it a match if the entire argument is consumed.
if ( end_p & & static_cast < size_t > ( end_p - attach_target . c_str ( ) ) = = attach_target . size ( ) )
handle_attach_to_pid ( gdb_server , static_cast < lldb : : pid_t > ( pid ) ) ;
else
handle_attach_to_process_name ( gdb_server , attach_target ) ;
}
void
2015-02-11 18:29:30 +08:00
handle_launch ( GDBRemoteCommunicationServerLLGS & gdb_server , int argc , const char * const argv [ ] )
2014-07-01 05:05:18 +08:00
{
Error error ;
error = gdb_server . SetLaunchArguments ( argv , argc ) ;
if ( error . Fail ( ) )
{
fprintf ( stderr , " error: failed to set launch args for '%s': %s \n " , argv [ 0 ] , error . AsCString ( ) ) ;
exit ( 1 ) ;
}
unsigned int launch_flags = eLaunchFlagStopAtEntry | eLaunchFlagDebug ;
error = gdb_server . SetLaunchFlags ( launch_flags ) ;
if ( error . Fail ( ) )
{
fprintf ( stderr , " error: failed to set launch flags for '%s': %s \n " , argv [ 0 ] , error . AsCString ( ) ) ;
exit ( 1 ) ;
}
error = gdb_server . LaunchProcess ( ) ;
if ( error . Fail ( ) )
{
fprintf ( stderr , " error: failed to launch '%s': %s \n " , argv [ 0 ] , error . AsCString ( ) ) ;
exit ( 1 ) ;
}
}
2014-11-28 04:51:24 +08:00
Error
2015-10-22 03:34:26 +08:00
writeSocketIdToPipe ( Pipe & port_pipe , const std : : string & socket_id )
2014-11-28 04:51:24 +08:00
{
2015-01-14 07:19:40 +08:00
size_t bytes_written = 0 ;
// Write the port number as a C string with the NULL terminator.
2015-10-22 05:58:22 +08:00
return port_pipe . Write ( socket_id . c_str ( ) , socket_id . size ( ) + 1 , bytes_written ) ;
2015-04-28 07:20:30 +08:00
}
Error
2015-10-22 03:34:26 +08:00
writeSocketIdToPipe ( const char * const named_pipe_path , const std : : string & socket_id )
2015-04-28 07:20:30 +08:00
{
Pipe port_name_pipe ;
// Wait for 10 seconds for pipe to be opened.
auto error = port_name_pipe . OpenAsWriterWithTimeout ( named_pipe_path , false ,
std : : chrono : : seconds { 10 } ) ;
if ( error . Fail ( ) )
return error ;
2015-10-22 03:34:26 +08:00
return writeSocketIdToPipe ( port_name_pipe , socket_id ) ;
2015-04-28 07:20:30 +08:00
}
Error
2015-10-22 03:34:26 +08:00
writeSocketIdToPipe ( int unnamed_pipe_fd , const std : : string & socket_id )
2015-04-28 07:20:30 +08:00
{
2015-04-30 03:01:43 +08:00
# if defined(_WIN32)
return Error ( " Unnamed pipes are not supported on Windows. " ) ;
# else
2015-04-30 01:36:58 +08:00
Pipe port_pipe { Pipe : : kInvalidDescriptor , unnamed_pipe_fd } ;
2015-10-22 03:34:26 +08:00
return writeSocketIdToPipe ( port_pipe , socket_id ) ;
2015-04-30 03:01:43 +08:00
# endif
2014-11-28 04:51:24 +08:00
}
2014-07-01 05:05:18 +08:00
void
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
ConnectToRemote ( MainLoop & mainloop , GDBRemoteCommunicationServerLLGS & gdb_server ,
2015-04-28 07:20:30 +08:00
bool reverse_connect , const char * const host_and_port ,
const char * const progname , const char * const subcommand ,
const char * const named_pipe_path , int unnamed_pipe_fd )
2014-07-01 05:05:18 +08:00
{
Error error ;
if ( host_and_port & & host_and_port [ 0 ] )
{
2014-07-27 04:39:17 +08:00
// Parse out host and port.
2014-07-12 06:50:13 +08:00
std : : string final_host_and_port ;
2014-07-27 04:39:17 +08:00
std : : string connection_host ;
std : : string connection_port ;
uint32_t connection_portno = 0 ;
2014-07-12 06:50:13 +08:00
// If host_and_port starts with ':', default the host to be "localhost" and expect the remainder to be the port.
if ( host_and_port [ 0 ] = = ' : ' )
final_host_and_port . append ( " localhost " ) ;
final_host_and_port . append ( host_and_port ) ;
const std : : string : : size_type colon_pos = final_host_and_port . find ( ' : ' ) ;
if ( colon_pos ! = std : : string : : npos )
{
2014-07-27 04:39:17 +08:00
connection_host = final_host_and_port . substr ( 0 , colon_pos ) ;
connection_port = final_host_and_port . substr ( colon_pos + 1 ) ;
2015-01-16 04:08:35 +08:00
connection_portno = StringConvert : : ToUInt32 ( connection_port . c_str ( ) , 0 ) ;
2014-07-12 06:50:13 +08:00
}
2014-07-01 05:05:18 +08:00
2015-10-22 03:34:26 +08:00
std : : unique_ptr < Connection > connection_up ;
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
2014-07-27 04:39:17 +08:00
if ( reverse_connect )
2014-07-12 06:50:13 +08:00
{
2014-07-27 04:39:17 +08:00
// llgs will connect to the gdb-remote client.
// Ensure we have a port number for the connection.
if ( connection_portno = = 0 )
2014-07-01 05:05:18 +08:00
{
2014-07-27 04:39:17 +08:00
fprintf ( stderr , " error: port number must be specified on when using reverse connect " ) ;
exit ( 1 ) ;
}
2014-07-01 05:05:18 +08:00
2014-07-27 04:39:17 +08:00
// Build the connection string.
char connection_url [ 512 ] ;
snprintf ( connection_url , sizeof ( connection_url ) , " connect://%s " , final_host_and_port . c_str ( ) ) ;
// Create the connection.
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
connection_up . reset ( new ConnectionFileDescriptor ) ;
2014-07-27 04:39:17 +08:00
auto connection_result = connection_up - > Connect ( connection_url , & error ) ;
if ( connection_result ! = eConnectionStatusSuccess )
{
fprintf ( stderr , " error: failed to connect to client at '%s' (connection status: %d) " , connection_url , static_cast < int > ( connection_result ) ) ;
exit ( - 1 ) ;
2014-07-01 05:05:18 +08:00
}
2014-07-27 04:39:17 +08:00
if ( error . Fail ( ) )
2014-07-01 05:05:18 +08:00
{
2014-07-27 04:39:17 +08:00
fprintf ( stderr , " error: failed to connect to client at '%s': %s " , connection_url , error . AsCString ( ) ) ;
exit ( - 1 ) ;
2014-07-01 05:05:18 +08:00
}
2014-07-12 06:50:13 +08:00
}
else
{
2015-10-22 03:34:26 +08:00
std : : unique_ptr < Acceptor > acceptor_up ( Acceptor : : Create ( final_host_and_port , false , error ) ) ;
if ( error . Fail ( ) )
{
fprintf ( stderr , " failed to create acceptor: %s " , error . AsCString ( ) ) ;
exit ( 1 ) ;
}
error = acceptor_up - > Listen ( 1 ) ;
if ( error . Fail ( ) )
2014-07-27 04:39:17 +08:00
{
2015-10-22 03:34:26 +08:00
fprintf ( stderr , " failed to listen: %s \n " , error . AsCString ( ) ) ;
exit ( 1 ) ;
}
const std : : string socket_id = acceptor_up - > GetLocalSocketId ( ) ;
if ( ! socket_id . empty ( ) )
{
// If we have a named pipe to write the socket id back to, do that now.
if ( named_pipe_path & & named_pipe_path [ 0 ] )
2014-07-27 04:39:17 +08:00
{
2015-10-22 03:34:26 +08:00
error = writeSocketIdToPipe ( named_pipe_path , socket_id ) ;
2014-11-28 04:51:24 +08:00
if ( error . Fail ( ) )
2015-10-22 03:34:26 +08:00
fprintf ( stderr , " failed to write to the named pipe \' %s \' : %s " ,
named_pipe_path , error . AsCString ( ) ) ;
2014-07-27 04:39:17 +08:00
}
2015-10-22 03:34:26 +08:00
// If we have an unnamed pipe to write the socket id back to, do that now.
else if ( unnamed_pipe_fd > = 0 )
2014-07-27 04:39:17 +08:00
{
2015-10-22 03:34:26 +08:00
error = writeSocketIdToPipe ( unnamed_pipe_fd , socket_id ) ;
2015-04-28 07:20:30 +08:00
if ( error . Fail ( ) )
fprintf ( stderr , " failed to write to the unnamed pipe: %s " ,
error . AsCString ( ) ) ;
}
}
2015-10-22 03:34:26 +08:00
else
2014-07-27 04:39:17 +08:00
{
2015-10-22 03:34:26 +08:00
fprintf ( stderr , " unable to get the socket id for the listening connection \n " ) ;
2014-07-27 04:39:17 +08:00
}
2015-10-22 03:34:26 +08:00
Connection * conn = nullptr ;
error = acceptor_up - > Accept ( false , conn ) ;
if ( error . Fail ( ) )
2014-07-27 04:39:17 +08:00
{
2015-10-22 03:34:26 +08:00
printf ( " failed to accept new connection: %s \n " , error . AsCString ( ) ) ;
exit ( 1 ) ;
2014-07-27 04:39:17 +08:00
}
2015-10-22 03:34:26 +08:00
connection_up . reset ( conn ) ;
2014-07-12 06:50:13 +08:00
}
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
error = gdb_server . InitializeConnection ( std : : move ( connection_up ) ) ;
if ( error . Fail ( ) )
2014-07-12 06:50:13 +08:00
{
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
fprintf ( stderr , " Failed to initialize connection: %s \n " , error . AsCString ( ) ) ;
exit ( - 1 ) ;
2014-07-12 06:50:13 +08:00
}
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
printf ( " Connection established. \n " ) ;
2014-07-01 05:05:18 +08:00
}
}
2015-03-06 23:47:23 +08:00
//----------------------------------------------------------------------
// main
//----------------------------------------------------------------------
int
main_gdbserver ( int argc , char * argv [ ] )
{
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
Error error ;
MainLoop mainloop ;
2015-03-12 05:14:22 +08:00
# ifndef _WIN32
// Setup signal handlers first thing.
signal ( SIGPIPE , signal_handler ) ;
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
MainLoop : : SignalHandleUP sighup_handle = mainloop . RegisterSignal ( SIGHUP , sighup_handler , error ) ;
2015-03-12 05:14:22 +08:00
# endif
2015-04-17 22:07:49 +08:00
# ifdef __linux__
// Block delivery of SIGCHLD on linux. NativeProcessLinux will read it using signalfd.
sigset_t set ;
sigemptyset ( & set ) ;
sigaddset ( & set , SIGCHLD ) ;
pthread_sigmask ( SIG_BLOCK , & set , NULL ) ;
# endif
2015-03-06 23:47:23 +08:00
2014-07-01 05:05:18 +08:00
const char * progname = argv [ 0 ] ;
2015-02-18 23:39:41 +08:00
const char * subcommand = argv [ 1 ] ;
argc - - ;
argv + + ;
2013-11-22 02:39:32 +08:00
int long_option_index = 0 ;
int ch ;
2014-01-28 08:34:23 +08:00
std : : string platform_name ;
2014-07-01 05:05:18 +08:00
std : : string attach_target ;
2014-07-12 06:50:13 +08:00
std : : string named_pipe_path ;
2015-05-10 23:22:09 +08:00
std : : string log_file ;
StringRef log_channels ; // e.g. "lldb process threads:gdb-remote default:linux all"
2015-04-28 07:20:30 +08:00
int unnamed_pipe_fd = - 1 ;
2014-07-27 04:39:17 +08:00
bool reverse_connect = false ;
2014-01-28 08:34:23 +08:00
2014-01-24 06:05:44 +08:00
// ProcessLaunchInfo launch_info;
2013-11-22 02:58:35 +08:00
ProcessAttachInfo attach_info ;
2013-11-22 02:39:32 +08:00
bool show_usage = false ;
int option_error = 0 ;
2013-11-23 02:55:04 +08:00
# if __GLIBC__
optind = 0 ;
# else
optreset = 1 ;
optind = 1 ;
# endif
2014-01-22 02:46:22 +08:00
2013-11-23 09:58:15 +08:00
std : : string short_options ( OptionParser : : GetShortOptionString ( g_long_options ) ) ;
while ( ( ch = getopt_long_only ( argc , argv , short_options . c_str ( ) , g_long_options , & long_option_index ) ) ! = - 1 )
2013-11-22 02:39:32 +08:00
{
switch ( ch )
{
case 0 : // Any optional that auto set themselves will return 0
break ;
case ' l ' : // Set Log File
if ( optarg & & optarg [ 0 ] )
2015-05-10 23:22:09 +08:00
log_file . assign ( optarg ) ;
2013-11-22 02:39:32 +08:00
break ;
2015-05-10 23:22:09 +08:00
case ' c ' : // Log Channels
2013-11-22 02:39:32 +08:00
if ( optarg & & optarg [ 0 ] )
2015-05-10 23:22:09 +08:00
log_channels = StringRef ( optarg ) ;
2014-01-22 02:46:22 +08:00
break ;
2014-01-28 08:34:23 +08:00
case ' p ' : // platform name
if ( optarg & & optarg [ 0 ] )
platform_name = optarg ;
break ;
2015-04-28 07:20:30 +08:00
case ' N ' : // named pipe
2014-07-12 06:50:13 +08:00
if ( optarg & & optarg [ 0 ] )
named_pipe_path = optarg ;
break ;
2015-04-28 07:20:30 +08:00
case ' U ' : // unnamed pipe
if ( optarg & & optarg [ 0 ] )
unnamed_pipe_fd = StringConvert : : ToUInt32 ( optarg , - 1 ) ;
2014-07-13 01:34:24 +08:00
case ' r ' :
// Do nothing, native regs is the default these days
break ;
2014-07-27 04:39:17 +08:00
case ' R ' :
reverse_connect = true ;
break ;
2014-07-12 09:12:44 +08:00
# ifndef _WIN32
case ' S ' :
// Put llgs into a new session. Terminals group processes
// into sessions and when a special terminal key sequences
// (like control+c) are typed they can cause signals to go out to
// all processes in a session. Using this --setsid (-S) option
// will cause debugserver to run in its own sessions and be free
// from such issues.
//
// This is useful when llgs is spawned from a command
// line application that uses llgs to do the debugging,
// yet that application doesn't want llgs receiving the
// signals sent to the session (i.e. dying when anyone hits ^C).
{
const : : pid_t new_sid = setsid ( ) ;
if ( new_sid = = - 1 )
{
const char * errno_str = strerror ( errno ) ;
fprintf ( stderr , " failed to set new session id for %s (%s) \n " , LLGS_PROGRAM_NAME , errno_str ? errno_str : " <no error string> " ) ;
}
}
break ;
# endif
2014-07-01 05:05:18 +08:00
case ' a ' : // attach {pid|process_name}
if ( optarg & & optarg [ 0 ] )
attach_target = optarg ;
break ;
2013-11-22 02:39:32 +08:00
case ' h ' : /* fall-through is intentional */
case ' ? ' :
show_usage = true ;
break ;
}
}
2014-01-22 02:46:22 +08:00
2013-11-22 02:39:32 +08:00
if ( show_usage | | option_error )
{
2015-02-18 23:39:41 +08:00
display_usage ( progname , subcommand ) ;
2013-11-22 02:39:32 +08:00
exit ( option_error ) ;
}
2014-01-22 02:46:22 +08:00
2015-05-27 21:34:04 +08:00
if ( ! LLDBServerUtilities : : SetupLogging ( log_file , log_channels , 0 ) )
return - 1 ;
2015-05-10 23:22:09 +08:00
2015-01-22 06:42:49 +08:00
Log * log ( lldb_private : : GetLogIfAnyCategoriesSet ( GDBR_LOG_VERBOSE ) ) ;
if ( log )
{
2015-03-06 22:36:33 +08:00
log - > Printf ( " lldb-server launch " ) ;
2015-01-22 06:42:49 +08:00
for ( int i = 0 ; i < argc ; i + + )
{
log - > Printf ( " argv[%i] = '%s' " , i , argv [ i ] ) ;
}
}
2013-11-22 02:39:32 +08:00
2014-07-01 05:05:18 +08:00
// Skip any options we consumed with getopt_long_only.
2013-11-22 02:39:32 +08:00
argc - = optind ;
argv + = optind ;
2014-01-22 02:46:22 +08:00
2013-11-22 02:39:32 +08:00
if ( argc = = 0 )
{
2015-02-18 23:39:41 +08:00
display_usage ( progname , subcommand ) ;
2013-11-22 02:39:32 +08:00
exit ( 255 ) ;
}
2014-01-18 04:18:59 +08:00
2015-02-11 18:29:30 +08:00
// Setup the platform that GDBRemoteCommunicationServerLLGS will use.
2014-07-01 05:05:18 +08:00
lldb : : PlatformSP platform_sp = setup_platform ( platform_name ) ;
2014-01-28 08:34:23 +08:00
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
GDBRemoteCommunicationServerLLGS gdb_server ( platform_sp , mainloop ) ;
2014-01-24 06:05:44 +08:00
2014-07-01 05:05:18 +08:00
const char * const host_and_port = argv [ 0 ] ;
2013-11-22 02:39:32 +08:00
argc - = 1 ;
argv + = 1 ;
2014-07-01 05:05:18 +08:00
2015-06-18 13:27:05 +08:00
// Any arguments left over are for the program that we need to launch. If there
2013-11-22 02:39:32 +08:00
// are no arguments, then the GDB server will start up and wait for an 'A' packet
2014-07-01 05:05:18 +08:00
// to launch a program, or a vAttach packet to attach to an existing process, unless
// explicitly asked to attach with the --attach={pid|program_name} form.
if ( ! attach_target . empty ( ) )
handle_attach ( gdb_server , attach_target ) ;
else if ( argc > 0 )
handle_launch ( gdb_server , argc , argv ) ;
2014-07-12 06:50:13 +08:00
// Print version info.
printf ( " %s-%s " , LLGS_PROGRAM_NAME , LLGS_VERSION_STR ) ;
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
ConnectToRemote ( mainloop , gdb_server , reverse_connect ,
2015-04-28 07:20:30 +08:00
host_and_port , progname , subcommand ,
named_pipe_path . c_str ( ) , unnamed_pipe_fd ) ;
2013-11-22 02:39:32 +08:00
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
if ( ! gdb_server . IsConnected ( ) )
{
fprintf ( stderr , " no connection information provided, unable to run \n " ) ;
display_usage ( progname , subcommand ) ;
return 1 ;
}
mainloop . Run ( ) ;
2015-03-06 22:36:33 +08:00
fprintf ( stderr , " lldb-server exiting... \n " ) ;
2013-11-22 02:39:32 +08:00
return 0 ;
}