diff --git a/lldb/tools/lldb-server/lldb-platform.cpp b/lldb/tools/lldb-server/lldb-platform.cpp index 1a969eec8db85ec512fd1dd592a5d36b8f22cd58..bd5f854a3f22613ff80191f070e502876fd2131e 100644 --- a/lldb/tools/lldb-server/lldb-platform.cpp +++ b/lldb/tools/lldb-server/lldb-platform.cpp @@ -18,6 +18,7 @@ #if !defined(_WIN32) #include #endif + #include #include "llvm/Support/FileSystem.h" @@ -47,6 +48,7 @@ using namespace llvm; static int g_debug = 0; static int g_verbose = 0; static int g_server = 0; +static int nonservermodetimeout = 300; static struct option g_long_options[] = { {"debug", no_argument, &g_debug, 1}, @@ -60,6 +62,7 @@ static struct option g_long_options[] = { {"max-gdbserver-port", required_argument, nullptr, 'M'}, {"socket-file", required_argument, nullptr, 'f'}, {"server", no_argument, &g_server, 1}, + {"timeout", required_argument, nullptr, 'T'}, {nullptr, 0, nullptr, 0}}; #if defined(__APPLE__) @@ -70,6 +73,7 @@ static struct option g_long_options[] = { #define HIGH_PORT (49151u) #endif + #if !defined(_WIN32) // Watch for signals static void signal_handler(int signo) { @@ -86,10 +90,15 @@ static void signal_handler(int signo) { } #endif +static void sigalrm(int sig){ + llvm::errs() << "SIGALRM received because of timeout, exiting lldb-server...\n"; + exit(0); +} + static void display_usage(const char *progname, const char *subcommand) { fprintf(stderr, "Usage:\n %s %s [--log-file log-file-name] [--log-channels " "log-channel-list] [--port-file port-file-path] --server " - "--listen port\n", + "--listen port --timeout time\n", progname, subcommand); exit(0); } @@ -181,6 +190,16 @@ int main_platform(int argc, char *argv[]) { case 0: // Any optional that auto set themselves will return 0 break; + case 'T': + { + if(!llvm::to_integer(optarg, nonservermodetimeout)||nonservermodetimeout <= 0){ + WithColor::error() << "invalid timeout parameter " << optarg << "\n"; + display_usage(progname, subcommand); + exit(0); + } + break; + } + case 'L': listen_host_port.append(optarg); break; @@ -318,11 +337,26 @@ int main_platform(int argc, char *argv[]) { const bool children_inherit_accept_socket = true; Connection *conn = nullptr; + + // create an alarm for timeout in non-server mode +#if !defined(_WIN32) + struct sigaction act; + act.sa_handler = sigalrm; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + sigaction(SIGALRM,&act,NULL); + if(!g_server && getenv("LISTIMEOUT")!=nullptr) { + alarm(nonservermodetimeout); + } +#endif error = acceptor_up->Accept(children_inherit_accept_socket, conn); if (error.Fail()) { WithColor::error() << error.AsCString() << '\n'; exit(socket_error); } +#if !defined(_WIN32) + alarm(0); +#endif printf("Connection established.\n"); if (g_server) { // Collect child zombie processes.