1 //===-- lldb-gdbserver.cpp --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <cerrno>
10 #include <cstdint>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 
15 #ifndef _WIN32
16 #include <csignal>
17 #include <unistd.h>
18 #endif
19 
20 #include "LLDBServerUtilities.h"
21 #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h"
22 #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
23 #include "lldb/Host/Config.h"
24 #include "lldb/Host/ConnectionFileDescriptor.h"
25 #include "lldb/Host/FileSystem.h"
26 #include "lldb/Host/Pipe.h"
27 #include "lldb/Host/Socket.h"
28 #include "lldb/Host/common/NativeProcessProtocol.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Utility/LLDBLog.h"
31 #include "lldb/Utility/Status.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Option/ArgList.h"
34 #include "llvm/Option/OptTable.h"
35 #include "llvm/Option/Option.h"
36 #include "llvm/Support/Errno.h"
37 #include "llvm/Support/WithColor.h"
38 
39 #if defined(__linux__)
40 #include "Plugins/Process/Linux/NativeProcessLinux.h"
41 #elif defined(__FreeBSD__)
42 #include "Plugins/Process/FreeBSD/NativeProcessFreeBSD.h"
43 #elif defined(__NetBSD__)
44 #include "Plugins/Process/NetBSD/NativeProcessNetBSD.h"
45 #elif defined(_WIN32)
46 #include "Plugins/Process/Windows/Common/NativeProcessWindows.h"
47 #endif
48 
49 #ifndef LLGS_PROGRAM_NAME
50 #define LLGS_PROGRAM_NAME "lldb-server"
51 #endif
52 
53 #ifndef LLGS_VERSION_STR
54 #define LLGS_VERSION_STR "local_build"
55 #endif
56 
57 using namespace llvm;
58 using namespace lldb;
59 using namespace lldb_private;
60 using namespace lldb_private::lldb_server;
61 using namespace lldb_private::process_gdb_remote;
62 
63 namespace {
64 #if defined(__linux__)
65 typedef process_linux::NativeProcessLinux::Factory NativeProcessFactory;
66 #elif defined(__FreeBSD__)
67 typedef process_freebsd::NativeProcessFreeBSD::Factory NativeProcessFactory;
68 #elif defined(__NetBSD__)
69 typedef process_netbsd::NativeProcessNetBSD::Factory NativeProcessFactory;
70 #elif defined(_WIN32)
71 typedef NativeProcessWindows::Factory NativeProcessFactory;
72 #else
73 // Dummy implementation to make sure the code compiles
74 class NativeProcessFactory : public NativeProcessProtocol::Factory {
75 public:
76   llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
77   Launch(ProcessLaunchInfo &launch_info,
78          NativeProcessProtocol::NativeDelegate &delegate,
79          MainLoop &mainloop) const override {
80     llvm_unreachable("Not implemented");
81   }
82   llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
83   Attach(lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &delegate,
84          MainLoop &mainloop) const override {
85     llvm_unreachable("Not implemented");
86   }
87 };
88 #endif
89 }
90 
91 #ifndef _WIN32
92 // Watch for signals
93 static int g_sighup_received_count = 0;
94 
95 static void sighup_handler(MainLoopBase &mainloop) {
96   ++g_sighup_received_count;
97 
98   Log *log = GetLog(LLDBLog::Process);
99   LLDB_LOGF(log, "lldb-server:%s swallowing SIGHUP (receive count=%d)",
100             __FUNCTION__, g_sighup_received_count);
101 
102   if (g_sighup_received_count >= 2)
103     mainloop.RequestTermination();
104 }
105 #endif // #ifndef _WIN32
106 
107 void handle_attach_to_pid(GDBRemoteCommunicationServerLLGS &gdb_server,
108                           lldb::pid_t pid) {
109   Status error = gdb_server.AttachToProcess(pid);
110   if (error.Fail()) {
111     fprintf(stderr, "error: failed to attach to pid %" PRIu64 ": %s\n", pid,
112             error.AsCString());
113     exit(1);
114   }
115 }
116 
117 void handle_attach_to_process_name(GDBRemoteCommunicationServerLLGS &gdb_server,
118                                    const std::string &process_name) {
119   // FIXME implement.
120 }
121 
122 void handle_attach(GDBRemoteCommunicationServerLLGS &gdb_server,
123                    const std::string &attach_target) {
124   assert(!attach_target.empty() && "attach_target cannot be empty");
125 
126   // First check if the attach_target is convertible to a long. If so, we'll use
127   // it as a pid.
128   char *end_p = nullptr;
129   const long int pid = strtol(attach_target.c_str(), &end_p, 10);
130 
131   // We'll call it a match if the entire argument is consumed.
132   if (end_p &&
133       static_cast<size_t>(end_p - attach_target.c_str()) ==
134           attach_target.size())
135     handle_attach_to_pid(gdb_server, static_cast<lldb::pid_t>(pid));
136   else
137     handle_attach_to_process_name(gdb_server, attach_target);
138 }
139 
140 void handle_launch(GDBRemoteCommunicationServerLLGS &gdb_server,
141                    llvm::ArrayRef<llvm::StringRef> Arguments) {
142   ProcessLaunchInfo info;
143   info.GetFlags().Set(eLaunchFlagStopAtEntry | eLaunchFlagDebug |
144                       eLaunchFlagDisableASLR);
145   info.SetArguments(Args(Arguments), true);
146 
147   llvm::SmallString<64> cwd;
148   if (std::error_code ec = llvm::sys::fs::current_path(cwd)) {
149     llvm::errs() << "Error getting current directory: " << ec.message() << "\n";
150     exit(1);
151   }
152   FileSpec cwd_spec(cwd);
153   FileSystem::Instance().Resolve(cwd_spec);
154   info.SetWorkingDirectory(cwd_spec);
155   info.GetEnvironment() = Host::GetEnvironment();
156 
157   gdb_server.SetLaunchInfo(info);
158 
159   Status error = gdb_server.LaunchProcess();
160   if (error.Fail()) {
161     llvm::errs() << llvm::formatv("error: failed to launch '{0}': {1}\n",
162                                   Arguments[0], error);
163     exit(1);
164   }
165 }
166 
167 Status writeSocketIdToPipe(Pipe &port_pipe, llvm::StringRef socket_id) {
168   size_t bytes_written = 0;
169   // Write the port number as a C string with the NULL terminator.
170   return port_pipe.Write(socket_id.data(), socket_id.size() + 1, bytes_written);
171 }
172 
173 Status writeSocketIdToPipe(const char *const named_pipe_path,
174                            llvm::StringRef socket_id) {
175   Pipe port_name_pipe;
176   // Wait for 10 seconds for pipe to be opened.
177   auto error = port_name_pipe.OpenAsWriterWithTimeout(named_pipe_path, false,
178                                                       std::chrono::seconds{10});
179   if (error.Fail())
180     return error;
181   return writeSocketIdToPipe(port_name_pipe, socket_id);
182 }
183 
184 Status writeSocketIdToPipe(lldb::pipe_t unnamed_pipe,
185                            llvm::StringRef socket_id) {
186   Pipe port_pipe{LLDB_INVALID_PIPE, unnamed_pipe};
187   return writeSocketIdToPipe(port_pipe, socket_id);
188 }
189 
190 void ConnectToRemote(MainLoop &mainloop,
191                      GDBRemoteCommunicationServerLLGS &gdb_server,
192                      bool reverse_connect, llvm::StringRef host_and_port,
193                      const char *const progname, const char *const subcommand,
194                      const char *const named_pipe_path, pipe_t unnamed_pipe,
195                      int connection_fd) {
196   Status error;
197 
198   std::unique_ptr<Connection> connection_up;
199   std::string url;
200 
201   if (connection_fd != -1) {
202     url = llvm::formatv("fd://{0}", connection_fd).str();
203 
204     // Create the connection.
205 #if LLDB_ENABLE_POSIX && !defined _WIN32
206     ::fcntl(connection_fd, F_SETFD, FD_CLOEXEC);
207 #endif
208   } else if (!host_and_port.empty()) {
209     llvm::Expected<std::string> url_exp =
210         LLGSArgToURL(host_and_port, reverse_connect);
211     if (!url_exp) {
212       llvm::errs() << llvm::formatv("error: invalid host:port or URL '{0}': "
213                                     "{1}\n",
214                                     host_and_port,
215                                     llvm::toString(url_exp.takeError()));
216       exit(-1);
217     }
218 
219     url = std::move(url_exp.get());
220   }
221 
222   if (!url.empty()) {
223     // Create the connection or server.
224     std::unique_ptr<ConnectionFileDescriptor> conn_fd_up{
225         new ConnectionFileDescriptor};
226     auto connection_result = conn_fd_up->Connect(
227         url,
228         [named_pipe_path, unnamed_pipe](llvm::StringRef socket_id) {
229           // If we have a named pipe to write the socket id back to, do that
230           // now.
231           if (named_pipe_path && named_pipe_path[0]) {
232             Status error = writeSocketIdToPipe(named_pipe_path, socket_id);
233             if (error.Fail())
234               llvm::errs() << llvm::formatv(
235                   "failed to write to the named peipe '{0}': {1}\n",
236                   named_pipe_path, error.AsCString());
237           }
238           // If we have an unnamed pipe to write the socket id back to, do
239           // that now.
240           else if (unnamed_pipe != LLDB_INVALID_PIPE) {
241             Status error = writeSocketIdToPipe(unnamed_pipe, socket_id);
242             if (error.Fail())
243               llvm::errs() << llvm::formatv(
244                   "failed to write to the unnamed pipe: {0}\n", error);
245           }
246         },
247         &error);
248 
249     if (error.Fail()) {
250       llvm::errs() << llvm::formatv(
251           "error: failed to connect to client at '{0}': {1}\n", url, error);
252       exit(-1);
253     }
254     if (connection_result != eConnectionStatusSuccess) {
255       llvm::errs() << llvm::formatv(
256           "error: failed to connect to client at '{0}' "
257           "(connection status: {1})\n",
258           url, static_cast<int>(connection_result));
259       exit(-1);
260     }
261     connection_up = std::move(conn_fd_up);
262   }
263   error = gdb_server.InitializeConnection(std::move(connection_up));
264   if (error.Fail()) {
265     llvm::errs() << llvm::formatv("failed to initialize connection\n", error);
266     exit(-1);
267   }
268   llvm::outs() << "Connection established.\n";
269 }
270 
271 namespace {
272 enum ID {
273   OPT_INVALID = 0, // This is not an option ID.
274 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
275                HELPTEXT, METAVAR, VALUES)                                      \
276   OPT_##ID,
277 #include "LLGSOptions.inc"
278 #undef OPTION
279 };
280 
281 #define PREFIX(NAME, VALUE)                                                    \
282   constexpr llvm::StringLiteral NAME##_init[] = VALUE;                         \
283   constexpr llvm::ArrayRef<llvm::StringLiteral> NAME(                          \
284       NAME##_init, std::size(NAME##_init) - 1);
285 #include "LLGSOptions.inc"
286 #undef PREFIX
287 
288 static constexpr opt::OptTable::Info InfoTable[] = {
289 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
290                HELPTEXT, METAVAR, VALUES)                                      \
291   {                                                                            \
292       PREFIX,      NAME,      HELPTEXT,                                        \
293       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
294       PARAM,       FLAGS,     OPT_##GROUP,                                     \
295       OPT_##ALIAS, ALIASARGS, VALUES},
296 #include "LLGSOptions.inc"
297 #undef OPTION
298 };
299 
300 class LLGSOptTable : public opt::GenericOptTable {
301 public:
302   LLGSOptTable() : opt::GenericOptTable(InfoTable) {}
303 
304   void PrintHelp(llvm::StringRef Name) {
305     std::string Usage =
306         (Name + " [options] [[host]:port] [[--] program args...]").str();
307     OptTable::printHelp(llvm::outs(), Usage.c_str(), "lldb-server");
308     llvm::outs() << R"(
309 DESCRIPTION
310   lldb-server connects to the LLDB client, which drives the debugging session.
311   If no connection options are given, the [host]:port argument must be present
312   and will denote the address that lldb-server will listen on. [host] defaults
313   to "localhost" if empty. Port can be zero, in which case the port number will
314   be chosen dynamically and written to destinations given by --named-pipe and
315   --pipe arguments.
316 
317   If no target is selected at startup, lldb-server can be directed by the LLDB
318   client to launch or attach to a process.
319 
320 )";
321   }
322 };
323 } // namespace
324 
325 int main_gdbserver(int argc, char *argv[]) {
326   Status error;
327   MainLoop mainloop;
328 #ifndef _WIN32
329   // Setup signal handlers first thing.
330   signal(SIGPIPE, SIG_IGN);
331   MainLoop::SignalHandleUP sighup_handle =
332       mainloop.RegisterSignal(SIGHUP, sighup_handler, error);
333 #endif
334 
335   const char *progname = argv[0];
336   const char *subcommand = argv[1];
337   std::string attach_target;
338   std::string named_pipe_path;
339   std::string log_file;
340   StringRef
341       log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
342   lldb::pipe_t unnamed_pipe = LLDB_INVALID_PIPE;
343   bool reverse_connect = false;
344   int connection_fd = -1;
345 
346   // ProcessLaunchInfo launch_info;
347   ProcessAttachInfo attach_info;
348 
349   LLGSOptTable Opts;
350   llvm::BumpPtrAllocator Alloc;
351   llvm::StringSaver Saver(Alloc);
352   bool HasError = false;
353   opt::InputArgList Args = Opts.parseArgs(argc - 1, argv + 1, OPT_UNKNOWN,
354                                           Saver, [&](llvm::StringRef Msg) {
355                                             WithColor::error() << Msg << "\n";
356                                             HasError = true;
357                                           });
358   std::string Name =
359       (llvm::sys::path::filename(argv[0]) + " g[dbserver]").str();
360   std::string HelpText =
361       "Use '" + Name + " --help' for a complete list of options.\n";
362   if (HasError) {
363     llvm::errs() << HelpText;
364     return 1;
365   }
366 
367   if (Args.hasArg(OPT_help)) {
368     Opts.PrintHelp(Name);
369     return 0;
370   }
371 
372 #ifndef _WIN32
373   if (Args.hasArg(OPT_setsid)) {
374     // Put llgs into a new session. Terminals group processes
375     // into sessions and when a special terminal key sequences
376     // (like control+c) are typed they can cause signals to go out to
377     // all processes in a session. Using this --setsid (-S) option
378     // will cause debugserver to run in its own sessions and be free
379     // from such issues.
380     //
381     // This is useful when llgs is spawned from a command
382     // line application that uses llgs to do the debugging,
383     // yet that application doesn't want llgs receiving the
384     // signals sent to the session (i.e. dying when anyone hits ^C).
385     {
386       const ::pid_t new_sid = setsid();
387       if (new_sid == -1) {
388         WithColor::warning()
389             << llvm::formatv("failed to set new session id for {0} ({1})\n",
390                              LLGS_PROGRAM_NAME, llvm::sys::StrError());
391       }
392     }
393   }
394 #endif
395 
396   log_file = Args.getLastArgValue(OPT_log_file).str();
397   log_channels = Args.getLastArgValue(OPT_log_channels);
398   named_pipe_path = Args.getLastArgValue(OPT_named_pipe).str();
399   reverse_connect = Args.hasArg(OPT_reverse_connect);
400   attach_target = Args.getLastArgValue(OPT_attach).str();
401   if (Args.hasArg(OPT_pipe)) {
402     uint64_t Arg;
403     if (!llvm::to_integer(Args.getLastArgValue(OPT_pipe), Arg)) {
404       WithColor::error() << "invalid '--pipe' argument\n" << HelpText;
405       return 1;
406     }
407     unnamed_pipe = (pipe_t)Arg;
408   }
409   if (Args.hasArg(OPT_fd)) {
410     if (!llvm::to_integer(Args.getLastArgValue(OPT_fd), connection_fd)) {
411       WithColor::error() << "invalid '--fd' argument\n" << HelpText;
412       return 1;
413     }
414   }
415 
416   if (!LLDBServerUtilities::SetupLogging(
417           log_file, log_channels,
418           LLDB_LOG_OPTION_PREPEND_TIMESTAMP |
419               LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION))
420     return -1;
421 
422   std::vector<llvm::StringRef> Inputs;
423   for (opt::Arg *Arg : Args.filtered(OPT_INPUT))
424     Inputs.push_back(Arg->getValue());
425   if (opt::Arg *Arg = Args.getLastArg(OPT_REM)) {
426     for (const char *Val : Arg->getValues())
427       Inputs.push_back(Val);
428   }
429   if (Inputs.empty() && connection_fd == -1) {
430     WithColor::error() << "no connection arguments\n" << HelpText;
431     return 1;
432   }
433 
434   NativeProcessFactory factory;
435   GDBRemoteCommunicationServerLLGS gdb_server(mainloop, factory);
436 
437   llvm::StringRef host_and_port;
438   if (!Inputs.empty()) {
439     host_and_port = Inputs.front();
440     Inputs.erase(Inputs.begin());
441   }
442 
443   // Any arguments left over are for the program that we need to launch. If
444   // there
445   // are no arguments, then the GDB server will start up and wait for an 'A'
446   // packet
447   // to launch a program, or a vAttach packet to attach to an existing process,
448   // unless
449   // explicitly asked to attach with the --attach={pid|program_name} form.
450   if (!attach_target.empty())
451     handle_attach(gdb_server, attach_target);
452   else if (!Inputs.empty())
453     handle_launch(gdb_server, Inputs);
454 
455   // Print version info.
456   printf("%s-%s\n", LLGS_PROGRAM_NAME, LLGS_VERSION_STR);
457 
458   ConnectToRemote(mainloop, gdb_server, reverse_connect, host_and_port,
459                   progname, subcommand, named_pipe_path.c_str(),
460                   unnamed_pipe, connection_fd);
461 
462   if (!gdb_server.IsConnected()) {
463     fprintf(stderr, "no connection information provided, unable to run\n");
464     return 1;
465   }
466 
467   Status ret = mainloop.Run();
468   if (ret.Fail()) {
469     fprintf(stderr, "lldb-server terminating due to error: %s\n",
470             ret.AsCString());
471     return 1;
472   }
473   fprintf(stderr, "lldb-server exiting...\n");
474 
475   return 0;
476 }
477