xref: /openbsd-src/gnu/llvm/lldb/tools/lldb-server/lldb-gdbserver.cpp (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1*061da546Spatrick //===-- lldb-gdbserver.cpp --------------------------------------*- C++ -*-===//
2*061da546Spatrick //
3*061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*061da546Spatrick //
7*061da546Spatrick //===----------------------------------------------------------------------===//
8*061da546Spatrick 
9*061da546Spatrick #include <errno.h>
10*061da546Spatrick #include <stdint.h>
11*061da546Spatrick #include <stdio.h>
12*061da546Spatrick #include <stdlib.h>
13*061da546Spatrick #include <string.h>
14*061da546Spatrick 
15*061da546Spatrick #ifndef _WIN32
16*061da546Spatrick #include <signal.h>
17*061da546Spatrick #include <unistd.h>
18*061da546Spatrick #endif
19*061da546Spatrick 
20*061da546Spatrick 
21*061da546Spatrick #include "Acceptor.h"
22*061da546Spatrick #include "LLDBServerUtilities.h"
23*061da546Spatrick #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h"
24*061da546Spatrick #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
25*061da546Spatrick #include "lldb/Host/Config.h"
26*061da546Spatrick #include "lldb/Host/ConnectionFileDescriptor.h"
27*061da546Spatrick #include "lldb/Host/FileSystem.h"
28*061da546Spatrick #include "lldb/Host/HostGetOpt.h"
29*061da546Spatrick #include "lldb/Host/OptionParser.h"
30*061da546Spatrick #include "lldb/Host/Pipe.h"
31*061da546Spatrick #include "lldb/Host/Socket.h"
32*061da546Spatrick #include "lldb/Host/StringConvert.h"
33*061da546Spatrick #include "lldb/Host/common/NativeProcessProtocol.h"
34*061da546Spatrick #include "lldb/Target/Process.h"
35*061da546Spatrick #include "lldb/Utility/Status.h"
36*061da546Spatrick #include "llvm/ADT/StringRef.h"
37*061da546Spatrick #include "llvm/Support/Errno.h"
38*061da546Spatrick 
39*061da546Spatrick #if defined(__linux__)
40*061da546Spatrick #include "Plugins/Process/Linux/NativeProcessLinux.h"
41*061da546Spatrick #elif defined(__NetBSD__)
42*061da546Spatrick #include "Plugins/Process/NetBSD/NativeProcessNetBSD.h"
43*061da546Spatrick #elif defined(__OpenBSD__)
44*061da546Spatrick #include "Plugins/Process/OpenBSD/NativeProcessOpenBSD.h"
45*061da546Spatrick #elif defined(_WIN32)
46*061da546Spatrick #include "Plugins/Process/Windows/Common/NativeProcessWindows.h"
47*061da546Spatrick #endif
48*061da546Spatrick 
49*061da546Spatrick #ifndef LLGS_PROGRAM_NAME
50*061da546Spatrick #define LLGS_PROGRAM_NAME "lldb-server"
51*061da546Spatrick #endif
52*061da546Spatrick 
53*061da546Spatrick #ifndef LLGS_VERSION_STR
54*061da546Spatrick #define LLGS_VERSION_STR "local_build"
55*061da546Spatrick #endif
56*061da546Spatrick 
57*061da546Spatrick using namespace llvm;
58*061da546Spatrick using namespace lldb;
59*061da546Spatrick using namespace lldb_private;
60*061da546Spatrick using namespace lldb_private::lldb_server;
61*061da546Spatrick using namespace lldb_private::process_gdb_remote;
62*061da546Spatrick 
63*061da546Spatrick namespace {
64*061da546Spatrick #if defined(__linux__)
65*061da546Spatrick typedef process_linux::NativeProcessLinux::Factory NativeProcessFactory;
66*061da546Spatrick #elif defined(__NetBSD__)
67*061da546Spatrick typedef process_netbsd::NativeProcessNetBSD::Factory NativeProcessFactory;
68*061da546Spatrick #elif defined(__OpenBSD__)
69*061da546Spatrick typedef process_openbsd::NativeProcessOpenBSD::Factory NativeProcessFactory;
70*061da546Spatrick #elif defined(_WIN32)
71*061da546Spatrick typedef NativeProcessWindows::Factory NativeProcessFactory;
72*061da546Spatrick #else
73*061da546Spatrick // Dummy implementation to make sure the code compiles
74*061da546Spatrick class NativeProcessFactory : public NativeProcessProtocol::Factory {
75*061da546Spatrick public:
76*061da546Spatrick   llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
77*061da546Spatrick   Launch(ProcessLaunchInfo &launch_info,
78*061da546Spatrick          NativeProcessProtocol::NativeDelegate &delegate,
79*061da546Spatrick          MainLoop &mainloop) const override {
80*061da546Spatrick     llvm_unreachable("Not implemented");
81*061da546Spatrick   }
82*061da546Spatrick   llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
83*061da546Spatrick   Attach(lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &delegate,
84*061da546Spatrick          MainLoop &mainloop) const override {
85*061da546Spatrick     llvm_unreachable("Not implemented");
86*061da546Spatrick   }
87*061da546Spatrick };
88*061da546Spatrick #endif
89*061da546Spatrick }
90*061da546Spatrick 
91*061da546Spatrick // option descriptors for getopt_long_only()
92*061da546Spatrick 
93*061da546Spatrick static int g_debug = 0;
94*061da546Spatrick static int g_verbose = 0;
95*061da546Spatrick 
96*061da546Spatrick static struct option g_long_options[] = {
97*061da546Spatrick     {"debug", no_argument, &g_debug, 1},
98*061da546Spatrick     {"verbose", no_argument, &g_verbose, 1},
99*061da546Spatrick     {"log-file", required_argument, nullptr, 'l'},
100*061da546Spatrick     {"log-channels", required_argument, nullptr, 'c'},
101*061da546Spatrick     {"attach", required_argument, nullptr, 'a'},
102*061da546Spatrick     {"named-pipe", required_argument, nullptr, 'N'},
103*061da546Spatrick     {"pipe", required_argument, nullptr, 'U'},
104*061da546Spatrick     {"native-regs", no_argument, nullptr,
105*061da546Spatrick      'r'}, // Specify to use the native registers instead of the gdb defaults
106*061da546Spatrick            // for the architecture.  NOTE: this is a do-nothing arg as it's
107*061da546Spatrick            // behavior is default now.  FIXME remove call from lldb-platform.
108*061da546Spatrick     {"reverse-connect", no_argument, nullptr,
109*061da546Spatrick      'R'}, // Specifies that llgs attaches to the client address:port rather
110*061da546Spatrick            // than llgs listening for a connection from address on port.
111*061da546Spatrick     {"setsid", no_argument, nullptr,
112*061da546Spatrick      'S'}, // Call setsid() to make llgs run in its own session.
113*061da546Spatrick     {"fd", required_argument, nullptr, 'F'},
114*061da546Spatrick     {nullptr, 0, nullptr, 0}};
115*061da546Spatrick 
116*061da546Spatrick #ifndef _WIN32
117*061da546Spatrick // Watch for signals
118*061da546Spatrick static int g_sighup_received_count = 0;
119*061da546Spatrick 
120*061da546Spatrick static void sighup_handler(MainLoopBase &mainloop) {
121*061da546Spatrick   ++g_sighup_received_count;
122*061da546Spatrick 
123*061da546Spatrick   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
124*061da546Spatrick   LLDB_LOGF(log, "lldb-server:%s swallowing SIGHUP (receive count=%d)",
125*061da546Spatrick             __FUNCTION__, g_sighup_received_count);
126*061da546Spatrick 
127*061da546Spatrick   if (g_sighup_received_count >= 2)
128*061da546Spatrick     mainloop.RequestTermination();
129*061da546Spatrick }
130*061da546Spatrick #endif // #ifndef _WIN32
131*061da546Spatrick 
132*061da546Spatrick static void display_usage(const char *progname, const char *subcommand) {
133*061da546Spatrick   fprintf(stderr, "Usage:\n  %s %s "
134*061da546Spatrick                   "[--log-file log-file-name] "
135*061da546Spatrick                   "[--log-channels log-channel-list] "
136*061da546Spatrick                   "[--setsid] "
137*061da546Spatrick                   "[--fd file-descriptor]"
138*061da546Spatrick                   "[--named-pipe named-pipe-path] "
139*061da546Spatrick                   "[--native-regs] "
140*061da546Spatrick                   "[--attach pid] "
141*061da546Spatrick                   "[[HOST]:PORT] "
142*061da546Spatrick                   "[-- PROGRAM ARG1 ARG2 ...]\n",
143*061da546Spatrick           progname, subcommand);
144*061da546Spatrick }
145*061da546Spatrick 
146*061da546Spatrick void handle_attach_to_pid(GDBRemoteCommunicationServerLLGS &gdb_server,
147*061da546Spatrick                           lldb::pid_t pid) {
148*061da546Spatrick   Status error = gdb_server.AttachToProcess(pid);
149*061da546Spatrick   if (error.Fail()) {
150*061da546Spatrick     fprintf(stderr, "error: failed to attach to pid %" PRIu64 ": %s\n", pid,
151*061da546Spatrick             error.AsCString());
152*061da546Spatrick     exit(1);
153*061da546Spatrick   }
154*061da546Spatrick }
155*061da546Spatrick 
156*061da546Spatrick void handle_attach_to_process_name(GDBRemoteCommunicationServerLLGS &gdb_server,
157*061da546Spatrick                                    const std::string &process_name) {
158*061da546Spatrick   // FIXME implement.
159*061da546Spatrick }
160*061da546Spatrick 
161*061da546Spatrick void handle_attach(GDBRemoteCommunicationServerLLGS &gdb_server,
162*061da546Spatrick                    const std::string &attach_target) {
163*061da546Spatrick   assert(!attach_target.empty() && "attach_target cannot be empty");
164*061da546Spatrick 
165*061da546Spatrick   // First check if the attach_target is convertible to a long. If so, we'll use
166*061da546Spatrick   // it as a pid.
167*061da546Spatrick   char *end_p = nullptr;
168*061da546Spatrick   const long int pid = strtol(attach_target.c_str(), &end_p, 10);
169*061da546Spatrick 
170*061da546Spatrick   // We'll call it a match if the entire argument is consumed.
171*061da546Spatrick   if (end_p &&
172*061da546Spatrick       static_cast<size_t>(end_p - attach_target.c_str()) ==
173*061da546Spatrick           attach_target.size())
174*061da546Spatrick     handle_attach_to_pid(gdb_server, static_cast<lldb::pid_t>(pid));
175*061da546Spatrick   else
176*061da546Spatrick     handle_attach_to_process_name(gdb_server, attach_target);
177*061da546Spatrick }
178*061da546Spatrick 
179*061da546Spatrick void handle_launch(GDBRemoteCommunicationServerLLGS &gdb_server, int argc,
180*061da546Spatrick                    const char *const argv[]) {
181*061da546Spatrick   ProcessLaunchInfo info;
182*061da546Spatrick   info.GetFlags().Set(eLaunchFlagStopAtEntry | eLaunchFlagDebug |
183*061da546Spatrick                       eLaunchFlagDisableASLR);
184*061da546Spatrick   info.SetArguments(const_cast<const char **>(argv), true);
185*061da546Spatrick 
186*061da546Spatrick   llvm::SmallString<64> cwd;
187*061da546Spatrick   if (std::error_code ec = llvm::sys::fs::current_path(cwd)) {
188*061da546Spatrick     llvm::errs() << "Error getting current directory: " << ec.message() << "\n";
189*061da546Spatrick     exit(1);
190*061da546Spatrick   }
191*061da546Spatrick   FileSpec cwd_spec(cwd);
192*061da546Spatrick   FileSystem::Instance().Resolve(cwd_spec);
193*061da546Spatrick   info.SetWorkingDirectory(cwd_spec);
194*061da546Spatrick   info.GetEnvironment() = Host::GetEnvironment();
195*061da546Spatrick 
196*061da546Spatrick   gdb_server.SetLaunchInfo(info);
197*061da546Spatrick 
198*061da546Spatrick   Status error = gdb_server.LaunchProcess();
199*061da546Spatrick   if (error.Fail()) {
200*061da546Spatrick     llvm::errs() << llvm::formatv("error: failed to launch '{0}': {1}\n",
201*061da546Spatrick                                   argv[0], error);
202*061da546Spatrick     exit(1);
203*061da546Spatrick   }
204*061da546Spatrick }
205*061da546Spatrick 
206*061da546Spatrick Status writeSocketIdToPipe(Pipe &port_pipe, const std::string &socket_id) {
207*061da546Spatrick   size_t bytes_written = 0;
208*061da546Spatrick   // Write the port number as a C string with the NULL terminator.
209*061da546Spatrick   return port_pipe.Write(socket_id.c_str(), socket_id.size() + 1,
210*061da546Spatrick                          bytes_written);
211*061da546Spatrick }
212*061da546Spatrick 
213*061da546Spatrick Status writeSocketIdToPipe(const char *const named_pipe_path,
214*061da546Spatrick                            const std::string &socket_id) {
215*061da546Spatrick   Pipe port_name_pipe;
216*061da546Spatrick   // Wait for 10 seconds for pipe to be opened.
217*061da546Spatrick   auto error = port_name_pipe.OpenAsWriterWithTimeout(named_pipe_path, false,
218*061da546Spatrick                                                       std::chrono::seconds{10});
219*061da546Spatrick   if (error.Fail())
220*061da546Spatrick     return error;
221*061da546Spatrick   return writeSocketIdToPipe(port_name_pipe, socket_id);
222*061da546Spatrick }
223*061da546Spatrick 
224*061da546Spatrick Status writeSocketIdToPipe(lldb::pipe_t unnamed_pipe,
225*061da546Spatrick                            const std::string &socket_id) {
226*061da546Spatrick   Pipe port_pipe{LLDB_INVALID_PIPE, unnamed_pipe};
227*061da546Spatrick   return writeSocketIdToPipe(port_pipe, socket_id);
228*061da546Spatrick }
229*061da546Spatrick 
230*061da546Spatrick void ConnectToRemote(MainLoop &mainloop,
231*061da546Spatrick                      GDBRemoteCommunicationServerLLGS &gdb_server,
232*061da546Spatrick                      bool reverse_connect, const char *const host_and_port,
233*061da546Spatrick                      const char *const progname, const char *const subcommand,
234*061da546Spatrick                      const char *const named_pipe_path, pipe_t unnamed_pipe,
235*061da546Spatrick                      int connection_fd) {
236*061da546Spatrick   Status error;
237*061da546Spatrick 
238*061da546Spatrick   std::unique_ptr<Connection> connection_up;
239*061da546Spatrick   if (connection_fd != -1) {
240*061da546Spatrick     // Build the connection string.
241*061da546Spatrick     char connection_url[512];
242*061da546Spatrick     snprintf(connection_url, sizeof(connection_url), "fd://%d", connection_fd);
243*061da546Spatrick 
244*061da546Spatrick     // Create the connection.
245*061da546Spatrick #if LLDB_ENABLE_POSIX && !defined _WIN32
246*061da546Spatrick     ::fcntl(connection_fd, F_SETFD, FD_CLOEXEC);
247*061da546Spatrick #endif
248*061da546Spatrick     connection_up.reset(new ConnectionFileDescriptor);
249*061da546Spatrick     auto connection_result = connection_up->Connect(connection_url, &error);
250*061da546Spatrick     if (connection_result != eConnectionStatusSuccess) {
251*061da546Spatrick       fprintf(stderr, "error: failed to connect to client at '%s' "
252*061da546Spatrick                       "(connection status: %d)\n",
253*061da546Spatrick               connection_url, static_cast<int>(connection_result));
254*061da546Spatrick       exit(-1);
255*061da546Spatrick     }
256*061da546Spatrick     if (error.Fail()) {
257*061da546Spatrick       fprintf(stderr, "error: failed to connect to client at '%s': %s\n",
258*061da546Spatrick               connection_url, error.AsCString());
259*061da546Spatrick       exit(-1);
260*061da546Spatrick     }
261*061da546Spatrick   } else if (host_and_port && host_and_port[0]) {
262*061da546Spatrick     // Parse out host and port.
263*061da546Spatrick     std::string final_host_and_port;
264*061da546Spatrick     std::string connection_host;
265*061da546Spatrick     std::string connection_port;
266*061da546Spatrick     uint32_t connection_portno = 0;
267*061da546Spatrick 
268*061da546Spatrick     // If host_and_port starts with ':', default the host to be "localhost" and
269*061da546Spatrick     // expect the remainder to be the port.
270*061da546Spatrick     if (host_and_port[0] == ':')
271*061da546Spatrick       final_host_and_port.append("localhost");
272*061da546Spatrick     final_host_and_port.append(host_and_port);
273*061da546Spatrick 
274*061da546Spatrick     const std::string::size_type colon_pos = final_host_and_port.find(':');
275*061da546Spatrick     if (colon_pos != std::string::npos) {
276*061da546Spatrick       connection_host = final_host_and_port.substr(0, colon_pos);
277*061da546Spatrick       connection_port = final_host_and_port.substr(colon_pos + 1);
278*061da546Spatrick       connection_portno = StringConvert::ToUInt32(connection_port.c_str(), 0);
279*061da546Spatrick     }
280*061da546Spatrick 
281*061da546Spatrick 
282*061da546Spatrick     if (reverse_connect) {
283*061da546Spatrick       // llgs will connect to the gdb-remote client.
284*061da546Spatrick 
285*061da546Spatrick       // Ensure we have a port number for the connection.
286*061da546Spatrick       if (connection_portno == 0) {
287*061da546Spatrick         fprintf(stderr, "error: port number must be specified on when using "
288*061da546Spatrick                         "reverse connect\n");
289*061da546Spatrick         exit(1);
290*061da546Spatrick       }
291*061da546Spatrick 
292*061da546Spatrick       // Build the connection string.
293*061da546Spatrick       char connection_url[512];
294*061da546Spatrick       snprintf(connection_url, sizeof(connection_url), "connect://%s",
295*061da546Spatrick                final_host_and_port.c_str());
296*061da546Spatrick 
297*061da546Spatrick       // Create the connection.
298*061da546Spatrick       connection_up.reset(new ConnectionFileDescriptor);
299*061da546Spatrick       auto connection_result = connection_up->Connect(connection_url, &error);
300*061da546Spatrick       if (connection_result != eConnectionStatusSuccess) {
301*061da546Spatrick         fprintf(stderr, "error: failed to connect to client at '%s' "
302*061da546Spatrick                         "(connection status: %d)\n",
303*061da546Spatrick                 connection_url, static_cast<int>(connection_result));
304*061da546Spatrick         exit(-1);
305*061da546Spatrick       }
306*061da546Spatrick       if (error.Fail()) {
307*061da546Spatrick         fprintf(stderr, "error: failed to connect to client at '%s': %s\n",
308*061da546Spatrick                 connection_url, error.AsCString());
309*061da546Spatrick         exit(-1);
310*061da546Spatrick       }
311*061da546Spatrick     } else {
312*061da546Spatrick       std::unique_ptr<Acceptor> acceptor_up(
313*061da546Spatrick           Acceptor::Create(final_host_and_port, false, error));
314*061da546Spatrick       if (error.Fail()) {
315*061da546Spatrick         fprintf(stderr, "failed to create acceptor: %s\n", error.AsCString());
316*061da546Spatrick         exit(1);
317*061da546Spatrick       }
318*061da546Spatrick       error = acceptor_up->Listen(1);
319*061da546Spatrick       if (error.Fail()) {
320*061da546Spatrick         fprintf(stderr, "failed to listen: %s\n", error.AsCString());
321*061da546Spatrick         exit(1);
322*061da546Spatrick       }
323*061da546Spatrick       const std::string socket_id = acceptor_up->GetLocalSocketId();
324*061da546Spatrick       if (!socket_id.empty()) {
325*061da546Spatrick         // If we have a named pipe to write the socket id back to, do that now.
326*061da546Spatrick         if (named_pipe_path && named_pipe_path[0]) {
327*061da546Spatrick           error = writeSocketIdToPipe(named_pipe_path, socket_id);
328*061da546Spatrick           if (error.Fail())
329*061da546Spatrick             fprintf(stderr, "failed to write to the named pipe \'%s\': %s\n",
330*061da546Spatrick                     named_pipe_path, error.AsCString());
331*061da546Spatrick         }
332*061da546Spatrick         // If we have an unnamed pipe to write the socket id back to, do that
333*061da546Spatrick         // now.
334*061da546Spatrick         else if (unnamed_pipe != LLDB_INVALID_PIPE) {
335*061da546Spatrick           error = writeSocketIdToPipe(unnamed_pipe, socket_id);
336*061da546Spatrick           if (error.Fail())
337*061da546Spatrick             fprintf(stderr, "failed to write to the unnamed pipe: %s\n",
338*061da546Spatrick                     error.AsCString());
339*061da546Spatrick         }
340*061da546Spatrick       } else {
341*061da546Spatrick         fprintf(stderr,
342*061da546Spatrick                 "unable to get the socket id for the listening connection\n");
343*061da546Spatrick       }
344*061da546Spatrick 
345*061da546Spatrick       Connection *conn = nullptr;
346*061da546Spatrick       error = acceptor_up->Accept(false, conn);
347*061da546Spatrick       if (error.Fail()) {
348*061da546Spatrick         printf("failed to accept new connection: %s\n", error.AsCString());
349*061da546Spatrick         exit(1);
350*061da546Spatrick       }
351*061da546Spatrick       connection_up.reset(conn);
352*061da546Spatrick     }
353*061da546Spatrick   }
354*061da546Spatrick   error = gdb_server.InitializeConnection(std::move(connection_up));
355*061da546Spatrick   if (error.Fail()) {
356*061da546Spatrick     fprintf(stderr, "Failed to initialize connection: %s\n",
357*061da546Spatrick             error.AsCString());
358*061da546Spatrick     exit(-1);
359*061da546Spatrick   }
360*061da546Spatrick   printf("Connection established.\n");
361*061da546Spatrick }
362*061da546Spatrick 
363*061da546Spatrick // main
364*061da546Spatrick int main_gdbserver(int argc, char *argv[]) {
365*061da546Spatrick   Status error;
366*061da546Spatrick   MainLoop mainloop;
367*061da546Spatrick #ifndef _WIN32
368*061da546Spatrick   // Setup signal handlers first thing.
369*061da546Spatrick   signal(SIGPIPE, SIG_IGN);
370*061da546Spatrick   MainLoop::SignalHandleUP sighup_handle =
371*061da546Spatrick       mainloop.RegisterSignal(SIGHUP, sighup_handler, error);
372*061da546Spatrick #endif
373*061da546Spatrick 
374*061da546Spatrick   const char *progname = argv[0];
375*061da546Spatrick   const char *subcommand = argv[1];
376*061da546Spatrick   argc--;
377*061da546Spatrick   argv++;
378*061da546Spatrick   int long_option_index = 0;
379*061da546Spatrick   int ch;
380*061da546Spatrick   std::string attach_target;
381*061da546Spatrick   std::string named_pipe_path;
382*061da546Spatrick   std::string log_file;
383*061da546Spatrick   StringRef
384*061da546Spatrick       log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
385*061da546Spatrick   lldb::pipe_t unnamed_pipe = LLDB_INVALID_PIPE;
386*061da546Spatrick   bool reverse_connect = false;
387*061da546Spatrick   int connection_fd = -1;
388*061da546Spatrick 
389*061da546Spatrick   // ProcessLaunchInfo launch_info;
390*061da546Spatrick   ProcessAttachInfo attach_info;
391*061da546Spatrick 
392*061da546Spatrick   bool show_usage = false;
393*061da546Spatrick   int option_error = 0;
394*061da546Spatrick #if __GLIBC__
395*061da546Spatrick   optind = 0;
396*061da546Spatrick #else
397*061da546Spatrick   optreset = 1;
398*061da546Spatrick   optind = 1;
399*061da546Spatrick #endif
400*061da546Spatrick 
401*061da546Spatrick   std::string short_options(OptionParser::GetShortOptionString(g_long_options));
402*061da546Spatrick 
403*061da546Spatrick   while ((ch = getopt_long_only(argc, argv, short_options.c_str(),
404*061da546Spatrick                                 g_long_options, &long_option_index)) != -1) {
405*061da546Spatrick     switch (ch) {
406*061da546Spatrick     case 0: // Any optional that auto set themselves will return 0
407*061da546Spatrick       break;
408*061da546Spatrick 
409*061da546Spatrick     case 'l': // Set Log File
410*061da546Spatrick       if (optarg && optarg[0])
411*061da546Spatrick         log_file.assign(optarg);
412*061da546Spatrick       break;
413*061da546Spatrick 
414*061da546Spatrick     case 'c': // Log Channels
415*061da546Spatrick       if (optarg && optarg[0])
416*061da546Spatrick         log_channels = StringRef(optarg);
417*061da546Spatrick       break;
418*061da546Spatrick 
419*061da546Spatrick     case 'N': // named pipe
420*061da546Spatrick       if (optarg && optarg[0])
421*061da546Spatrick         named_pipe_path = optarg;
422*061da546Spatrick       break;
423*061da546Spatrick 
424*061da546Spatrick     case 'U': // unnamed pipe
425*061da546Spatrick       if (optarg && optarg[0])
426*061da546Spatrick         unnamed_pipe = (pipe_t)StringConvert::ToUInt64(optarg, -1);
427*061da546Spatrick       break;
428*061da546Spatrick 
429*061da546Spatrick     case 'r':
430*061da546Spatrick       // Do nothing, native regs is the default these days
431*061da546Spatrick       break;
432*061da546Spatrick 
433*061da546Spatrick     case 'R':
434*061da546Spatrick       reverse_connect = true;
435*061da546Spatrick       break;
436*061da546Spatrick 
437*061da546Spatrick     case 'F':
438*061da546Spatrick       connection_fd = StringConvert::ToUInt32(optarg, -1);
439*061da546Spatrick       break;
440*061da546Spatrick 
441*061da546Spatrick #ifndef _WIN32
442*061da546Spatrick     case 'S':
443*061da546Spatrick       // Put llgs into a new session. Terminals group processes
444*061da546Spatrick       // into sessions and when a special terminal key sequences
445*061da546Spatrick       // (like control+c) are typed they can cause signals to go out to
446*061da546Spatrick       // all processes in a session. Using this --setsid (-S) option
447*061da546Spatrick       // will cause debugserver to run in its own sessions and be free
448*061da546Spatrick       // from such issues.
449*061da546Spatrick       //
450*061da546Spatrick       // This is useful when llgs is spawned from a command
451*061da546Spatrick       // line application that uses llgs to do the debugging,
452*061da546Spatrick       // yet that application doesn't want llgs receiving the
453*061da546Spatrick       // signals sent to the session (i.e. dying when anyone hits ^C).
454*061da546Spatrick       {
455*061da546Spatrick         const ::pid_t new_sid = setsid();
456*061da546Spatrick         if (new_sid == -1) {
457*061da546Spatrick           llvm::errs() << llvm::formatv(
458*061da546Spatrick               "failed to set new session id for {0} ({1})\n", LLGS_PROGRAM_NAME,
459*061da546Spatrick               llvm::sys::StrError());
460*061da546Spatrick         }
461*061da546Spatrick       }
462*061da546Spatrick       break;
463*061da546Spatrick #endif
464*061da546Spatrick 
465*061da546Spatrick     case 'a': // attach {pid|process_name}
466*061da546Spatrick       if (optarg && optarg[0])
467*061da546Spatrick         attach_target = optarg;
468*061da546Spatrick       break;
469*061da546Spatrick 
470*061da546Spatrick     case 'h': /* fall-through is intentional */
471*061da546Spatrick     case '?':
472*061da546Spatrick       show_usage = true;
473*061da546Spatrick       break;
474*061da546Spatrick     }
475*061da546Spatrick   }
476*061da546Spatrick 
477*061da546Spatrick   if (show_usage || option_error) {
478*061da546Spatrick     display_usage(progname, subcommand);
479*061da546Spatrick     exit(option_error);
480*061da546Spatrick   }
481*061da546Spatrick 
482*061da546Spatrick   if (!LLDBServerUtilities::SetupLogging(
483*061da546Spatrick           log_file, log_channels,
484*061da546Spatrick           LLDB_LOG_OPTION_PREPEND_TIMESTAMP |
485*061da546Spatrick               LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION))
486*061da546Spatrick     return -1;
487*061da546Spatrick 
488*061da546Spatrick   Log *log(lldb_private::GetLogIfAnyCategoriesSet(GDBR_LOG_PROCESS));
489*061da546Spatrick   if (log) {
490*061da546Spatrick     LLDB_LOGF(log, "lldb-server launch");
491*061da546Spatrick     for (int i = 0; i < argc; i++) {
492*061da546Spatrick       LLDB_LOGF(log, "argv[%i] = '%s'", i, argv[i]);
493*061da546Spatrick     }
494*061da546Spatrick   }
495*061da546Spatrick 
496*061da546Spatrick   // Skip any options we consumed with getopt_long_only.
497*061da546Spatrick   argc -= optind;
498*061da546Spatrick   argv += optind;
499*061da546Spatrick 
500*061da546Spatrick   if (argc == 0 && connection_fd == -1) {
501*061da546Spatrick     fputs("No arguments\n", stderr);
502*061da546Spatrick     display_usage(progname, subcommand);
503*061da546Spatrick     exit(255);
504*061da546Spatrick   }
505*061da546Spatrick 
506*061da546Spatrick   NativeProcessFactory factory;
507*061da546Spatrick   GDBRemoteCommunicationServerLLGS gdb_server(mainloop, factory);
508*061da546Spatrick 
509*061da546Spatrick   const char *const host_and_port = argv[0];
510*061da546Spatrick   argc -= 1;
511*061da546Spatrick   argv += 1;
512*061da546Spatrick 
513*061da546Spatrick   // Any arguments left over are for the program that we need to launch. If
514*061da546Spatrick   // there
515*061da546Spatrick   // are no arguments, then the GDB server will start up and wait for an 'A'
516*061da546Spatrick   // packet
517*061da546Spatrick   // to launch a program, or a vAttach packet to attach to an existing process,
518*061da546Spatrick   // unless
519*061da546Spatrick   // explicitly asked to attach with the --attach={pid|program_name} form.
520*061da546Spatrick   if (!attach_target.empty())
521*061da546Spatrick     handle_attach(gdb_server, attach_target);
522*061da546Spatrick   else if (argc > 0)
523*061da546Spatrick     handle_launch(gdb_server, argc, argv);
524*061da546Spatrick 
525*061da546Spatrick   // Print version info.
526*061da546Spatrick   printf("%s-%s\n", LLGS_PROGRAM_NAME, LLGS_VERSION_STR);
527*061da546Spatrick 
528*061da546Spatrick   ConnectToRemote(mainloop, gdb_server, reverse_connect, host_and_port,
529*061da546Spatrick                   progname, subcommand, named_pipe_path.c_str(),
530*061da546Spatrick                   unnamed_pipe, connection_fd);
531*061da546Spatrick 
532*061da546Spatrick   if (!gdb_server.IsConnected()) {
533*061da546Spatrick     fprintf(stderr, "no connection information provided, unable to run\n");
534*061da546Spatrick     display_usage(progname, subcommand);
535*061da546Spatrick     return 1;
536*061da546Spatrick   }
537*061da546Spatrick 
538*061da546Spatrick   Status ret = mainloop.Run();
539*061da546Spatrick   if (ret.Fail()) {
540*061da546Spatrick     fprintf(stderr, "lldb-server terminating due to error: %s\n",
541*061da546Spatrick             ret.AsCString());
542*061da546Spatrick     return 1;
543*061da546Spatrick   }
544*061da546Spatrick   fprintf(stderr, "lldb-server exiting...\n");
545*061da546Spatrick 
546*061da546Spatrick   return 0;
547*061da546Spatrick }
548