blob: c6a1b6e6fa7727f637473986752723780a606b66 [file] [log] [blame]
Greg Clayton2bddd342010-09-07 20:11:56 +00001//===-- Host.cpp ------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytone3e3fee2013-02-17 20:46:30 +000012// C includes
Greg Claytone3e3fee2013-02-17 20:46:30 +000013#include <errno.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000014#include <limits.h>
Greg Clayton23f8c952014-03-24 23:10:19 +000015#include <stdlib.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000016#include <sys/types.h>
Zachary Turnercb37ddd2014-10-15 17:27:11 +000017#ifndef _WIN32
Deepak Panickalb36da432014-01-13 14:55:15 +000018#include <unistd.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000019#include <dlfcn.h>
20#include <grp.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000021#include <netdb.h>
22#include <pwd.h>
Jason Molenda4a4b5dc2013-11-21 02:45:55 +000023#include <sys/stat.h>
Sylvestre Ledru785ee472013-09-05 15:39:09 +000024#endif
25
Greg Claytone3e3fee2013-02-17 20:46:30 +000026#if defined (__APPLE__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000027#include <mach/mach_port.h>
Charles Davis510938e2013-08-27 05:04:57 +000028#include <mach/mach_init.h>
29#include <mach-o/dyld.h>
Ed Maste8b006c62013-06-25 18:58:11 +000030#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000031
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +000032#if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) || defined(__NetBSD__)
Shawn Best8da0bf32014-11-08 01:41:49 +000033#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Daniel Malea4244cbd2013-08-27 20:58:59 +000034#include <spawn.h>
Todd Fialacacde7d2014-09-27 16:54:22 +000035#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000036#include <sys/wait.h>
Michael Sartainc2052432013-08-01 18:51:08 +000037#include <sys/syscall.h>
Ed Maste8b006c62013-06-25 18:58:11 +000038#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000039
Ed Maste8b006c62013-06-25 18:58:11 +000040#if defined (__FreeBSD__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000041#include <pthread_np.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000042#endif
43
Todd Fiala17096d72014-07-16 19:03:16 +000044// C++ includes
45#include <limits>
46
Greg Clayton2bddd342010-09-07 20:11:56 +000047#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000048#include "lldb/Host/HostInfo.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000049#include "lldb/Core/ArchSpec.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000050#include "lldb/Core/Debugger.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000051#include "lldb/Core/Error.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000052#include "lldb/Core/Log.h"
Daniel Malea4244cbd2013-08-27 20:58:59 +000053#include "lldb/Core/Module.h"
Greg Claytone996fd32011-03-08 22:40:15 +000054#include "lldb/Host/FileSpec.h"
Zachary Turner172d37d2014-10-14 21:55:08 +000055#include "lldb/Host/HostProcess.h"
56#include "lldb/Host/MonitoringProcessLauncher.h"
Zachary Turner172d37d2014-10-14 21:55:08 +000057#include "lldb/Host/ProcessLauncher.h"
Zachary Turner39de3112014-09-09 20:54:56 +000058#include "lldb/Host/ThreadLauncher.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000059#include "lldb/lldb-private-forward.h"
Zachary Turner696b5282014-08-14 16:01:25 +000060#include "lldb/Target/FileAction.h"
Zachary Turner696b5282014-08-14 16:01:25 +000061#include "lldb/Target/ProcessLaunchInfo.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000062#include "lldb/Target/TargetList.h"
Daniel Malea4244cbd2013-08-27 20:58:59 +000063#include "lldb/Utility/CleanUp.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000064
Zachary Turner172d37d2014-10-14 21:55:08 +000065#if defined(_WIN32)
66#include "lldb/Host/windows/ProcessLauncherWindows.h"
67#else
68#include "lldb/Host/posix/ProcessLauncherPosix.h"
69#endif
70
Todd Fiala76747122014-01-23 00:52:28 +000071#if defined (__APPLE__)
72#ifndef _POSIX_SPAWN_DISABLE_ASLR
73#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
74#endif
75
76extern "C"
77{
78 int __pthread_chdir(const char *path);
79 int __pthread_fchdir (int fildes);
80}
81
82#endif
Greg Clayton32e0a752011-03-30 18:16:51 +000083
Greg Clayton2bddd342010-09-07 20:11:56 +000084using namespace lldb;
85using namespace lldb_private;
86
Virgile Bellob2f1fb22013-08-23 12:44:05 +000087#if !defined (__APPLE__) && !defined (_WIN32)
Greg Clayton2bddd342010-09-07 20:11:56 +000088struct MonitorInfo
89{
90 lldb::pid_t pid; // The process ID to monitor
91 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
92 void *callback_baton; // The callback baton for the callback function
93 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
94};
95
Virgile Bellob2f1fb22013-08-23 12:44:05 +000096static thread_result_t
Greg Clayton2bddd342010-09-07 20:11:56 +000097MonitorChildProcessThreadFunction (void *arg);
98
Zachary Turner39de3112014-09-09 20:54:56 +000099HostThread
100Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
Greg Clayton2bddd342010-09-07 20:11:56 +0000101{
Greg Claytone4e45922011-11-16 05:37:56 +0000102 MonitorInfo * info_ptr = new MonitorInfo();
Daniel Malea4244cbd2013-08-27 20:58:59 +0000103
Greg Claytone4e45922011-11-16 05:37:56 +0000104 info_ptr->pid = pid;
105 info_ptr->callback = callback;
106 info_ptr->callback_baton = callback_baton;
107 info_ptr->monitor_signals = monitor_signals;
108
109 char thread_name[256];
Zachary Turner39de3112014-09-09 20:54:56 +0000110 ::snprintf(thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
111 return ThreadLauncher::LaunchThread(thread_name, MonitorChildProcessThreadFunction, info_ptr, NULL);
Greg Clayton2bddd342010-09-07 20:11:56 +0000112}
113
Shawn Best8da0bf32014-11-08 01:41:49 +0000114#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000115//------------------------------------------------------------------
116// Scoped class that will disable thread canceling when it is
117// constructed, and exception safely restore the previous value it
118// when it goes out of scope.
119//------------------------------------------------------------------
120class ScopedPThreadCancelDisabler
121{
122public:
123 ScopedPThreadCancelDisabler()
124 {
125 // Disable the ability for this thread to be cancelled
126 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
127 if (err != 0)
128 m_old_state = -1;
Greg Clayton2bddd342010-09-07 20:11:56 +0000129 }
130
131 ~ScopedPThreadCancelDisabler()
132 {
133 // Restore the ability for this thread to be cancelled to what it
134 // previously was.
135 if (m_old_state != -1)
136 ::pthread_setcancelstate (m_old_state, 0);
137 }
138private:
139 int m_old_state; // Save the old cancelability state.
140};
Shawn Best8da0bf32014-11-08 01:41:49 +0000141#endif // __ANDROID_NDK__
Greg Clayton2bddd342010-09-07 20:11:56 +0000142
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000143static thread_result_t
Greg Clayton2bddd342010-09-07 20:11:56 +0000144MonitorChildProcessThreadFunction (void *arg)
145{
Greg Clayton5160ce52013-03-27 23:08:40 +0000146 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton2bddd342010-09-07 20:11:56 +0000147 const char *function = __FUNCTION__;
148 if (log)
149 log->Printf ("%s (arg = %p) thread starting...", function, arg);
150
151 MonitorInfo *info = (MonitorInfo *)arg;
152
153 const Host::MonitorChildProcessCallback callback = info->callback;
154 void * const callback_baton = info->callback_baton;
Greg Clayton2bddd342010-09-07 20:11:56 +0000155 const bool monitor_signals = info->monitor_signals;
156
Daniel Malea4244cbd2013-08-27 20:58:59 +0000157 assert (info->pid <= UINT32_MAX);
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000158 const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000159
Greg Clayton2bddd342010-09-07 20:11:56 +0000160 delete info;
161
162 int status = -1;
Sylvestre Ledru59405832013-07-01 08:21:36 +0000163#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000164 #define __WALL 0
165#endif
Matt Kopec650648f2013-01-08 16:30:18 +0000166 const int options = __WALL;
167
Greg Clayton2bddd342010-09-07 20:11:56 +0000168 while (1)
169 {
Caroline Tice20ad3c42010-10-29 21:48:37 +0000170 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000171 if (log)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000172 log->Printf("%s ::wait_pid (pid = %" PRIi32 ", &status, options = %i)...", function, pid, options);
Greg Clayton2bddd342010-09-07 20:11:56 +0000173
174 // Wait for all child processes
Shawn Best8da0bf32014-11-08 01:41:49 +0000175#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000176 ::pthread_testcancel ();
Todd Fialacacde7d2014-09-27 16:54:22 +0000177#endif
Matt Kopec650648f2013-01-08 16:30:18 +0000178 // Get signals from all children with same process group of pid
Daniel Malea4244cbd2013-08-27 20:58:59 +0000179 const ::pid_t wait_pid = ::waitpid (pid, &status, options);
Shawn Best8da0bf32014-11-08 01:41:49 +0000180#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000181 ::pthread_testcancel ();
Todd Fialacacde7d2014-09-27 16:54:22 +0000182#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000183 if (wait_pid == -1)
184 {
185 if (errno == EINTR)
186 continue;
187 else
Andrew Kaylor93132f52013-05-28 23:04:25 +0000188 {
189 if (log)
190 log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno));
Greg Clayton2bddd342010-09-07 20:11:56 +0000191 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000192 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000193 }
Matt Kopec650648f2013-01-08 16:30:18 +0000194 else if (wait_pid > 0)
Greg Clayton2bddd342010-09-07 20:11:56 +0000195 {
196 bool exited = false;
197 int signal = 0;
198 int exit_status = 0;
199 const char *status_cstr = NULL;
200 if (WIFSTOPPED(status))
201 {
202 signal = WSTOPSIG(status);
203 status_cstr = "STOPPED";
204 }
205 else if (WIFEXITED(status))
206 {
207 exit_status = WEXITSTATUS(status);
208 status_cstr = "EXITED";
Andrew Kaylor93132f52013-05-28 23:04:25 +0000209 exited = true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000210 }
211 else if (WIFSIGNALED(status))
212 {
213 signal = WTERMSIG(status);
214 status_cstr = "SIGNALED";
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +0000215 if (wait_pid == abs(pid)) {
Matt Kopec650648f2013-01-08 16:30:18 +0000216 exited = true;
217 exit_status = -1;
218 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000219 }
220 else
221 {
Johnny Chen44805302011-07-19 19:48:13 +0000222 status_cstr = "(\?\?\?)";
Greg Clayton2bddd342010-09-07 20:11:56 +0000223 }
224
225 // Scope for pthread_cancel_disabler
226 {
Shawn Best8da0bf32014-11-08 01:41:49 +0000227#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000228 ScopedPThreadCancelDisabler pthread_cancel_disabler;
Todd Fialacacde7d2014-09-27 16:54:22 +0000229#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000230
Caroline Tice20ad3c42010-10-29 21:48:37 +0000231 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000232 if (log)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000233 log->Printf ("%s ::waitpid (pid = %" PRIi32 ", &status, options = %i) => pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
Greg Clayton2bddd342010-09-07 20:11:56 +0000234 function,
235 wait_pid,
236 options,
Greg Clayton2bddd342010-09-07 20:11:56 +0000237 pid,
238 status,
239 status_cstr,
240 signal,
241 exit_status);
242
243 if (exited || (signal != 0 && monitor_signals))
244 {
Greg Claytone4e45922011-11-16 05:37:56 +0000245 bool callback_return = false;
246 if (callback)
Matt Kopec650648f2013-01-08 16:30:18 +0000247 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton2bddd342010-09-07 20:11:56 +0000248
249 // If our process exited, then this thread should exit
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +0000250 if (exited && wait_pid == abs(pid))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000251 {
252 if (log)
253 log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000254 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000255 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000256 // If the callback returns true, it means this process should
257 // exit
258 if (callback_return)
Andrew Kaylor93132f52013-05-28 23:04:25 +0000259 {
260 if (log)
261 log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000262 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000263 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000264 }
265 }
266 }
267 }
268
Caroline Tice20ad3c42010-10-29 21:48:37 +0000269 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000270 if (log)
271 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
272
273 return NULL;
274}
275
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000276#endif // #if !defined (__APPLE__) && !defined (_WIN32)
277
278#if !defined (__APPLE__)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000279
280void
281Host::SystemLog (SystemLogType type, const char *format, va_list args)
282{
283 vfprintf (stderr, format, args);
284}
285
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000286#endif
Greg Claytone4e45922011-11-16 05:37:56 +0000287
Greg Claytone38a5ed2012-01-05 03:57:59 +0000288void
289Host::SystemLog (SystemLogType type, const char *format, ...)
290{
291 va_list args;
292 va_start (args, format);
293 SystemLog (type, format, args);
294 va_end (args);
295}
296
Greg Clayton2bddd342010-09-07 20:11:56 +0000297lldb::pid_t
298Host::GetCurrentProcessID()
299{
300 return ::getpid();
301}
302
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000303#ifndef _WIN32
304
Greg Clayton2bddd342010-09-07 20:11:56 +0000305lldb::tid_t
306Host::GetCurrentThreadID()
307{
308#if defined (__APPLE__)
Jean-Daniel Dupas3cfa8e22013-12-06 09:35:53 +0000309 // Calling "mach_thread_self()" bumps the reference count on the thread
Greg Clayton813ddfc2012-09-18 18:19:49 +0000310 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
311 // count.
312 thread_port_t thread_self = mach_thread_self();
313 mach_port_deallocate(mach_task_self(), thread_self);
314 return thread_self;
Johnny Chen8f3d8382011-08-02 20:52:42 +0000315#elif defined(__FreeBSD__)
316 return lldb::tid_t(pthread_getthreadid_np());
Shawn Best8da0bf32014-11-08 01:41:49 +0000317#elif defined(__ANDROID_NDK__)
318 return lldb::tid_t(gettid());
Michael Sartainc2052432013-08-01 18:51:08 +0000319#elif defined(__linux__)
320 return lldb::tid_t(syscall(SYS_gettid));
Greg Clayton2bddd342010-09-07 20:11:56 +0000321#else
322 return lldb::tid_t(pthread_self());
323#endif
324}
325
Jim Ingham372787f2012-04-07 00:00:41 +0000326lldb::thread_t
327Host::GetCurrentThread ()
328{
329 return lldb::thread_t(pthread_self());
330}
331
Greg Clayton2bddd342010-09-07 20:11:56 +0000332const char *
333Host::GetSignalAsCString (int signo)
334{
335 switch (signo)
336 {
337 case SIGHUP: return "SIGHUP"; // 1 hangup
338 case SIGINT: return "SIGINT"; // 2 interrupt
339 case SIGQUIT: return "SIGQUIT"; // 3 quit
340 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
341 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
342 case SIGABRT: return "SIGABRT"; // 6 abort()
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000343#if defined(SIGPOLL)
Sylvestre Ledrua4cc7de2013-12-13 09:51:39 +0000344#if !defined(SIGIO) || (SIGPOLL != SIGIO)
345// Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to
346// fail with 'multiple define cases with same value'
Greg Clayton2bddd342010-09-07 20:11:56 +0000347 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer44030f12011-11-04 16:06:40 +0000348#endif
Sylvestre Ledrua4cc7de2013-12-13 09:51:39 +0000349#endif
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000350#if defined(SIGEMT)
Greg Clayton2bddd342010-09-07 20:11:56 +0000351 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer44030f12011-11-04 16:06:40 +0000352#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000353 case SIGFPE: return "SIGFPE"; // 8 floating point exception
354 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
355 case SIGBUS: return "SIGBUS"; // 10 bus error
356 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
357 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
358 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
359 case SIGALRM: return "SIGALRM"; // 14 alarm clock
360 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
361 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
362 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
363 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
364 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
365 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
366 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
367 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000368#if defined(SIGIO)
Greg Clayton2bddd342010-09-07 20:11:56 +0000369 case SIGIO: return "SIGIO"; // 23 input/output possible signal
370#endif
371 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
372 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
373 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
374 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000375#if defined(SIGWINCH)
Greg Clayton2bddd342010-09-07 20:11:56 +0000376 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000377#endif
378#if defined(SIGINFO)
Greg Clayton2bddd342010-09-07 20:11:56 +0000379 case SIGINFO: return "SIGINFO"; // 29 information request
380#endif
381 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
382 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
383 default:
384 break;
385 }
386 return NULL;
387}
388
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000389#endif
390
Greg Clayton2bddd342010-09-07 20:11:56 +0000391void
392Host::WillTerminate ()
393{
394}
395
Sylvestre Ledru59405832013-07-01 08:21:36 +0000396#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
Matt Kopec62502c62013-05-13 19:33:58 +0000397
Greg Clayton2bddd342010-09-07 20:11:56 +0000398void
Greg Claytone5219662010-12-03 06:02:24 +0000399Host::Backtrace (Stream &strm, uint32_t max_frames)
400{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000401 // TODO: Is there a way to backtrace the current process on other systems?
Greg Claytone5219662010-12-03 06:02:24 +0000402}
403
Greg Clayton85851dd2010-12-04 00:10:17 +0000404size_t
405Host::GetEnvironment (StringList &env)
406{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000407 // TODO: Is there a way to the host environment for this process on other systems?
Greg Clayton85851dd2010-12-04 00:10:17 +0000408 return 0;
409}
410
Sylvestre Ledru59405832013-07-01 08:21:36 +0000411#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000412
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000413#ifndef _WIN32
414
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000415lldb::thread_key_t
416Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback)
417{
418 pthread_key_t key;
419 ::pthread_key_create (&key, callback);
420 return key;
421}
422
423void*
424Host::ThreadLocalStorageGet(lldb::thread_key_t key)
425{
426 return ::pthread_getspecific (key);
427}
428
429void
430Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value)
431{
432 ::pthread_setspecific (key, value);
433}
434
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000435#endif
436
Greg Clayton2bddd342010-09-07 20:11:56 +0000437#if !defined (__APPLE__) // see Host.mm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000438
439bool
440Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
441{
442 bundle.Clear();
443 return false;
444}
445
Greg Clayton2bddd342010-09-07 20:11:56 +0000446bool
Greg Claytondd36def2010-10-17 22:03:32 +0000447Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton2bddd342010-09-07 20:11:56 +0000448{
Greg Claytondd36def2010-10-17 22:03:32 +0000449 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000450}
451#endif
452
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000453#ifndef _WIN32
454
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000455FileSpec
456Host::GetModuleFileSpecForHostAddress (const void *host_addr)
457{
458 FileSpec module_filespec;
Shawn Best8da0bf32014-11-08 01:41:49 +0000459#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000460 Dl_info info;
461 if (::dladdr (host_addr, &info))
462 {
463 if (info.dli_fname)
464 module_filespec.SetFile(info.dli_fname, true);
465 }
Todd Fialacacde7d2014-09-27 16:54:22 +0000466#else
467 assert(false && "dladdr() not supported on Android");
468#endif
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000469 return module_filespec;
470}
471
472#endif
473
Matt Kopec085d6ce2013-05-31 22:00:07 +0000474#if !defined(__linux__)
475bool
476Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
477{
478 return false;
479}
480#endif
481
Sean Callananc0a6e062011-10-27 21:22:25 +0000482lldb::TargetSP
483Host::GetDummyTarget (lldb_private::Debugger &debugger)
484{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000485 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasb0183452012-05-17 15:48:02 +0000486
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000487 // FIXME: Maybe the dummy target should be per-Debugger
488 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
489 {
490 ArchSpec arch(Target::GetDefaultArchitecture());
491 if (!arch.IsValid())
Zachary Turner13b18262014-08-20 16:42:51 +0000492 arch = HostInfo::GetArchitecture();
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000493 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000494 NULL,
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000495 arch.GetTriple().getTriple().c_str(),
496 false,
497 NULL,
498 g_dummy_target_sp);
499 }
Filipe Cabecinhasb0183452012-05-17 15:48:02 +0000500
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000501 return g_dummy_target_sp;
Sean Callananc0a6e062011-10-27 21:22:25 +0000502}
503
Greg Claytond1cf11a2012-04-14 01:42:46 +0000504struct ShellInfo
505{
506 ShellInfo () :
507 process_reaped (false),
508 can_delete (false),
509 pid (LLDB_INVALID_PROCESS_ID),
510 signo(-1),
511 status(-1)
512 {
513 }
514
515 lldb_private::Predicate<bool> process_reaped;
516 lldb_private::Predicate<bool> can_delete;
517 lldb::pid_t pid;
518 int signo;
519 int status;
520};
521
522static bool
523MonitorShellCommand (void *callback_baton,
524 lldb::pid_t pid,
525 bool exited, // True if the process did exit
526 int signo, // Zero for no signal
527 int status) // Exit value of process if signal is zero
528{
529 ShellInfo *shell_info = (ShellInfo *)callback_baton;
530 shell_info->pid = pid;
531 shell_info->signo = signo;
532 shell_info->status = status;
533 // Let the thread running Host::RunShellCommand() know that the process
534 // exited and that ShellInfo has been filled in by broadcasting to it
535 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
536 // Now wait for a handshake back from that thread running Host::RunShellCommand
537 // so we know that we can delete shell_info_ptr
538 shell_info->can_delete.WaitForValueEqualTo(true);
539 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
540 usleep(1000);
541 // Now delete the shell info that was passed into this function
542 delete shell_info;
543 return true;
544}
545
546Error
547Host::RunShellCommand (const char *command,
548 const char *working_dir,
549 int *status_ptr,
550 int *signo_ptr,
551 std::string *command_output_ptr,
Greg Claytonc8f814d2012-09-27 03:13:55 +0000552 uint32_t timeout_sec,
Zachary Turner10687b02014-10-20 17:46:43 +0000553 bool run_in_default_shell)
Greg Claytond1cf11a2012-04-14 01:42:46 +0000554{
555 Error error;
556 ProcessLaunchInfo launch_info;
Zachary Turner10687b02014-10-20 17:46:43 +0000557 if (run_in_default_shell)
Greg Claytonc8f814d2012-09-27 03:13:55 +0000558 {
559 // Run the command in a shell
Zachary Turner10687b02014-10-20 17:46:43 +0000560 launch_info.SetShell(HostInfo::GetDefaultShell());
Greg Claytonc8f814d2012-09-27 03:13:55 +0000561 launch_info.GetArguments().AppendArgument(command);
562 const bool localhost = true;
563 const bool will_debug = false;
564 const bool first_arg_is_full_shell_command = true;
565 launch_info.ConvertArgumentsForLaunchingInShell (error,
566 localhost,
567 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +0000568 first_arg_is_full_shell_command,
569 0);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000570 }
571 else
572 {
573 // No shell, just run it
574 Args args (command);
575 const bool first_arg_is_executable = true;
Greg Clayton45392552012-10-17 22:57:12 +0000576 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000577 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000578
579 if (working_dir)
580 launch_info.SetWorkingDirectory(working_dir);
Greg Claytonc6931fc2013-12-04 18:53:50 +0000581 char output_file_path_buffer[PATH_MAX];
Greg Claytond1cf11a2012-04-14 01:42:46 +0000582 const char *output_file_path = NULL;
Greg Claytonc6931fc2013-12-04 18:53:50 +0000583
Greg Claytond1cf11a2012-04-14 01:42:46 +0000584 if (command_output_ptr)
585 {
586 // Create a temporary file to get the stdout/stderr and redirect the
587 // output of the command into this file. We will later read this file
588 // if all goes well and fill the data into "command_output_ptr"
Greg Claytonc6931fc2013-12-04 18:53:50 +0000589 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000590 if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Claytonc6931fc2013-12-04 18:53:50 +0000591 {
Zachary Turnerc25146b2014-08-21 23:56:55 +0000592 tmpdir_file_spec.AppendPathComponent("lldb-shell-output.XXXXXX");
Greg Claytonc6931fc2013-12-04 18:53:50 +0000593 strncpy(output_file_path_buffer, tmpdir_file_spec.GetPath().c_str(), sizeof(output_file_path_buffer));
594 }
595 else
596 {
597 strncpy(output_file_path_buffer, "/tmp/lldb-shell-output.XXXXXX", sizeof(output_file_path_buffer));
598 }
599
600 output_file_path = ::mktemp(output_file_path_buffer);
601 }
602
603 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
604 if (output_file_path)
605 {
Greg Claytond1cf11a2012-04-14 01:42:46 +0000606 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000607 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000608 }
609 else
610 {
Greg Claytond1cf11a2012-04-14 01:42:46 +0000611 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
612 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
613 }
614
615 // The process monitor callback will delete the 'shell_info_ptr' below...
Greg Clayton7b0992d2013-04-18 22:45:39 +0000616 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
Greg Claytond1cf11a2012-04-14 01:42:46 +0000617
618 const bool monitor_signals = false;
619 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
620
621 error = LaunchProcess (launch_info);
622 const lldb::pid_t pid = launch_info.GetProcessID();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000623
624 if (error.Success() && pid == LLDB_INVALID_PROCESS_ID)
625 error.SetErrorString("failed to get process ID");
626
627 if (error.Success())
Greg Claytond1cf11a2012-04-14 01:42:46 +0000628 {
629 // The process successfully launched, so we can defer ownership of
630 // "shell_info" to the MonitorShellCommand callback function that will
Greg Claytone01e07b2013-04-18 18:10:51 +0000631 // get called when the process dies. We release the unique pointer as it
Greg Claytond1cf11a2012-04-14 01:42:46 +0000632 // doesn't need to delete the ShellInfo anymore.
633 ShellInfo *shell_info = shell_info_ap.release();
Daniel Malea4244cbd2013-08-27 20:58:59 +0000634 TimeValue *timeout_ptr = nullptr;
Greg Claytond1cf11a2012-04-14 01:42:46 +0000635 TimeValue timeout_time(TimeValue::Now());
Daniel Malea4244cbd2013-08-27 20:58:59 +0000636 if (timeout_sec > 0) {
637 timeout_time.OffsetWithSeconds(timeout_sec);
638 timeout_ptr = &timeout_time;
639 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000640 bool timed_out = false;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000641 shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000642 if (timed_out)
643 {
644 error.SetErrorString("timed out waiting for shell command to complete");
Daniel Malea4244cbd2013-08-27 20:58:59 +0000645
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000646 // Kill the process since it didn't complete within the timeout specified
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000647 Kill (pid, SIGKILL);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000648 // Wait for the monitor callback to get the message
649 timeout_time = TimeValue::Now();
650 timeout_time.OffsetWithSeconds(1);
651 timed_out = false;
652 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
653 }
654 else
655 {
656 if (status_ptr)
657 *status_ptr = shell_info->status;
658
659 if (signo_ptr)
660 *signo_ptr = shell_info->signo;
661
662 if (command_output_ptr)
663 {
664 command_output_ptr->clear();
665 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
666 uint64_t file_size = file_spec.GetByteSize();
667 if (file_size > 0)
668 {
669 if (file_size > command_output_ptr->max_size())
670 {
671 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
672 }
673 else
674 {
675 command_output_ptr->resize(file_size);
676 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
677 }
678 }
679 }
680 }
681 shell_info->can_delete.SetValue(true, eBroadcastAlways);
682 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000683
684 if (output_file_path)
685 ::unlink (output_file_path);
686 // Handshake with the monitor thread, or just let it know in advance that
687 // it can delete "shell_info" in case we timed out and were not able to kill
688 // the process...
689 return error;
690}
691
Daniel Malea4244cbd2013-08-27 20:58:59 +0000692
Todd Fiala76747122014-01-23 00:52:28 +0000693// LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC
694// systems
695
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +0000696#if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__)
Todd Fiala76747122014-01-23 00:52:28 +0000697// this method needs to be visible to macosx/Host.cpp and
698// common/Host.cpp.
699
700short
Zachary Turner172d37d2014-10-14 21:55:08 +0000701Host::GetPosixspawnFlags(const ProcessLaunchInfo &launch_info)
Todd Fiala76747122014-01-23 00:52:28 +0000702{
Shawn Best8da0bf32014-11-08 01:41:49 +0000703#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Todd Fiala76747122014-01-23 00:52:28 +0000704 short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
705
706#if defined (__APPLE__)
707 if (launch_info.GetFlags().Test (eLaunchFlagExec))
708 flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag
709
710 if (launch_info.GetFlags().Test (eLaunchFlagDebug))
711 flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag
712
713 if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR))
714 flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag
715
716 if (launch_info.GetLaunchInSeparateProcessGroup())
717 flags |= POSIX_SPAWN_SETPGROUP;
718
719#ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
720#if defined (__APPLE__) && (defined (__x86_64__) || defined (__i386__))
721 static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate;
722 if (g_use_close_on_exec_flag == eLazyBoolCalculate)
723 {
724 g_use_close_on_exec_flag = eLazyBoolNo;
725
726 uint32_t major, minor, update;
Zachary Turner97a14e62014-08-19 17:18:29 +0000727 if (HostInfo::GetOSVersion(major, minor, update))
Todd Fiala76747122014-01-23 00:52:28 +0000728 {
729 // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier
730 if (major > 10 || (major == 10 && minor > 7))
731 {
732 // Only enable for 10.8 and later OS versions
733 g_use_close_on_exec_flag = eLazyBoolYes;
734 }
735 }
736 }
737#else
738 static LazyBool g_use_close_on_exec_flag = eLazyBoolYes;
739#endif
740 // Close all files exception those with file actions if this is supported.
741 if (g_use_close_on_exec_flag == eLazyBoolYes)
742 flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
743#endif
744#endif // #if defined (__APPLE__)
745 return flags;
Todd Fialacacde7d2014-09-27 16:54:22 +0000746#else
Shawn Best8da0bf32014-11-08 01:41:49 +0000747 assert(false && "Host::GetPosixspawnFlags() not supported on Android");
Todd Fialacacde7d2014-09-27 16:54:22 +0000748 return 0;
749#endif
Todd Fiala76747122014-01-23 00:52:28 +0000750}
751
752Error
Zachary Turner172d37d2014-10-14 21:55:08 +0000753Host::LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &launch_info, lldb::pid_t &pid)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000754{
755 Error error;
Shawn Best8da0bf32014-11-08 01:41:49 +0000756#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000757 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_PROCESS));
758
Daniel Malea4244cbd2013-08-27 20:58:59 +0000759 posix_spawnattr_t attr;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000760 error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000761
762 if (error.Fail() || log)
763 error.PutToLog(log, "::posix_spawnattr_init ( &attr )");
Daniel Malea4244cbd2013-08-27 20:58:59 +0000764 if (error.Fail())
765 return error;
766
767 // Make a quick class that will cleanup the posix spawn attributes in case
768 // we return in the middle of this function.
769 lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy);
770
771 sigset_t no_signals;
772 sigset_t all_signals;
773 sigemptyset (&no_signals);
774 sigfillset (&all_signals);
Todd Fiala76747122014-01-23 00:52:28 +0000775 ::posix_spawnattr_setsigmask(&attr, &no_signals);
Todd Fiala571768c2014-01-23 17:07:54 +0000776#if defined (__linux__) || defined (__FreeBSD__)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000777 ::posix_spawnattr_setsigdefault(&attr, &no_signals);
Todd Fiala76747122014-01-23 00:52:28 +0000778#else
779 ::posix_spawnattr_setsigdefault(&attr, &all_signals);
780#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000781
Todd Fiala76747122014-01-23 00:52:28 +0000782 short flags = GetPosixspawnFlags(launch_info);
Daniel Malea4244cbd2013-08-27 20:58:59 +0000783
784 error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000785 if (error.Fail() || log)
786 error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags);
Daniel Malea4244cbd2013-08-27 20:58:59 +0000787 if (error.Fail())
788 return error;
789
Todd Fiala76747122014-01-23 00:52:28 +0000790 // posix_spawnattr_setbinpref_np appears to be an Apple extension per:
791 // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/
792#if defined (__APPLE__) && !defined (__arm__)
Jim Ingham90331d52014-02-21 01:25:21 +0000793
794 // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell
795 // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation
796 // to do that job on OSX.
797
798 if (launch_info.GetShell() == nullptr)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000799 {
Jim Ingham90331d52014-02-21 01:25:21 +0000800 // We don't need to do this for ARM, and we really shouldn't now that we
801 // have multiple CPU subtypes and no posix_spawnattr call that allows us
802 // to set which CPU subtype to launch...
803 const ArchSpec &arch_spec = launch_info.GetArchitecture();
804 cpu_type_t cpu = arch_spec.GetMachOCPUType();
805 cpu_type_t sub = arch_spec.GetMachOCPUSubType();
806 if (cpu != 0 &&
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000807 cpu != static_cast<cpu_type_t>(UINT32_MAX) &&
808 cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) &&
Jim Ingham90331d52014-02-21 01:25:21 +0000809 !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail
810 {
811 size_t ocount = 0;
812 error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX);
813 if (error.Fail() || log)
814 error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %llu )", cpu, (uint64_t)ocount);
Todd Fiala76747122014-01-23 00:52:28 +0000815
Jim Ingham90331d52014-02-21 01:25:21 +0000816 if (error.Fail() || ocount != 1)
817 return error;
818 }
Daniel Malea4244cbd2013-08-27 20:58:59 +0000819 }
820
Todd Fiala76747122014-01-23 00:52:28 +0000821#endif
822
823 const char *tmp_argv[2];
824 char * const *argv = (char * const*)launch_info.GetArguments().GetConstArgumentVector();
825 char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector();
826 if (argv == NULL)
827 {
828 // posix_spawn gets very unhappy if it doesn't have at least the program
829 // name in argv[0]. One of the side affects I have noticed is the environment
830 // variables don't make it into the child process if "argv == NULL"!!!
831 tmp_argv[0] = exe_path;
832 tmp_argv[1] = NULL;
833 argv = (char * const*)tmp_argv;
834 }
835
836#if !defined (__APPLE__)
837 // manage the working directory
Daniel Malea4244cbd2013-08-27 20:58:59 +0000838 char current_dir[PATH_MAX];
839 current_dir[0] = '\0';
Todd Fiala76747122014-01-23 00:52:28 +0000840#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000841
842 const char *working_dir = launch_info.GetWorkingDirectory();
Todd Fiala76747122014-01-23 00:52:28 +0000843 if (working_dir)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000844 {
Todd Fiala76747122014-01-23 00:52:28 +0000845#if defined (__APPLE__)
846 // Set the working directory on this thread only
847 if (__pthread_chdir (working_dir) < 0) {
848 if (errno == ENOENT) {
849 error.SetErrorStringWithFormat("No such file or directory: %s", working_dir);
850 } else if (errno == ENOTDIR) {
851 error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir);
852 } else {
853 error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution.");
854 }
855 return error;
856 }
857#else
Daniel Malea4244cbd2013-08-27 20:58:59 +0000858 if (::getcwd(current_dir, sizeof(current_dir)) == NULL)
859 {
860 error.SetError(errno, eErrorTypePOSIX);
861 error.LogIfError(log, "unable to save the current directory");
862 return error;
863 }
864
865 if (::chdir(working_dir) == -1)
866 {
867 error.SetError(errno, eErrorTypePOSIX);
868 error.LogIfError(log, "unable to change working directory to %s", working_dir);
869 return error;
870 }
Todd Fiala76747122014-01-23 00:52:28 +0000871#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000872 }
873
Zachary Turner172d37d2014-10-14 21:55:08 +0000874 ::pid_t result_pid = LLDB_INVALID_PROCESS_ID;
Todd Fiala76747122014-01-23 00:52:28 +0000875 const size_t num_file_actions = launch_info.GetNumFileActions ();
876 if (num_file_actions > 0)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000877 {
Todd Fiala76747122014-01-23 00:52:28 +0000878 posix_spawn_file_actions_t file_actions;
879 error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX);
880 if (error.Fail() || log)
881 error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )");
882 if (error.Fail())
883 return error;
884
885 // Make a quick class that will cleanup the posix spawn attributes in case
886 // we return in the middle of this function.
887 lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy);
888
889 for (size_t i=0; i<num_file_actions; ++i)
890 {
Zachary Turner696b5282014-08-14 16:01:25 +0000891 const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i);
Todd Fiala76747122014-01-23 00:52:28 +0000892 if (launch_file_action)
893 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000894 if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log, error))
Todd Fiala76747122014-01-23 00:52:28 +0000895 return error;
896 }
897 }
898
Zachary Turner172d37d2014-10-14 21:55:08 +0000899 error.SetError(::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000900
901 if (error.Fail() || log)
902 {
Zachary Turner172d37d2014-10-14 21:55:08 +0000903 error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", result_pid,
904 exe_path, static_cast<void *>(&file_actions), static_cast<void *>(&attr), reinterpret_cast<const void *>(argv),
905 reinterpret_cast<const void *>(envp));
Todd Fiala76747122014-01-23 00:52:28 +0000906 if (log)
907 {
908 for (int ii=0; argv[ii]; ++ii)
909 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
910 }
911 }
912
913 }
914 else
915 {
Zachary Turner172d37d2014-10-14 21:55:08 +0000916 error.SetError(::posix_spawnp(&result_pid, exe_path, NULL, &attr, argv, envp), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000917
918 if (error.Fail() || log)
919 {
920 error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = NULL, attr = %p, argv = %p, envp = %p )",
Zachary Turner172d37d2014-10-14 21:55:08 +0000921 result_pid, exe_path, static_cast<void *>(&attr), reinterpret_cast<const void *>(argv),
922 reinterpret_cast<const void *>(envp));
Todd Fiala76747122014-01-23 00:52:28 +0000923 if (log)
924 {
925 for (int ii=0; argv[ii]; ++ii)
926 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
927 }
928 }
Daniel Malea4244cbd2013-08-27 20:58:59 +0000929 }
Zachary Turner172d37d2014-10-14 21:55:08 +0000930 pid = result_pid;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000931
Todd Fiala76747122014-01-23 00:52:28 +0000932 if (working_dir)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000933 {
Todd Fiala76747122014-01-23 00:52:28 +0000934#if defined (__APPLE__)
935 // No more thread specific current working directory
936 __pthread_fchdir (-1);
937#else
938 if (::chdir(current_dir) == -1 && error.Success())
939 {
940 error.SetError(errno, eErrorTypePOSIX);
941 error.LogIfError(log, "unable to change current directory back to %s",
942 current_dir);
943 }
944#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000945 }
Todd Fialacacde7d2014-09-27 16:54:22 +0000946#else
947 error.SetErrorString("Host::LaunchProcessPosixSpawn() not supported on Android");
948#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000949
950 return error;
951}
952
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000953bool
954Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error)
Zachary Turner696b5282014-08-14 16:01:25 +0000955{
Shawn Best8da0bf32014-11-08 01:41:49 +0000956#if !defined(__ANDROID__) && !defined(__ANDROID_NDK__)
Zachary Turner696b5282014-08-14 16:01:25 +0000957 if (info == NULL)
958 return false;
959
960 posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions);
961
962 switch (info->GetAction())
963 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000964 case FileAction::eFileActionNone:
965 error.Clear();
966 break;
Zachary Turner696b5282014-08-14 16:01:25 +0000967
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000968 case FileAction::eFileActionClose:
969 if (info->GetFD() == -1)
970 error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)");
971 else
972 {
973 error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX);
974 if (log && (error.Fail() || log))
975 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
976 static_cast<void *>(file_actions), info->GetFD());
977 }
978 break;
Zachary Turner696b5282014-08-14 16:01:25 +0000979
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000980 case FileAction::eFileActionDuplicate:
981 if (info->GetFD() == -1)
982 error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)");
983 else if (info->GetActionArgument() == -1)
984 error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
985 else
986 {
987 error.SetError(
988 ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()),
989 eErrorTypePOSIX);
990 if (log && (error.Fail() || log))
991 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
992 static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument());
993 }
994 break;
Zachary Turner696b5282014-08-14 16:01:25 +0000995
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000996 case FileAction::eFileActionOpen:
997 if (info->GetFD() == -1)
998 error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)");
999 else
1000 {
1001 int oflag = info->GetActionArgument();
Zachary Turner696b5282014-08-14 16:01:25 +00001002
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001003 mode_t mode = 0;
Zachary Turner696b5282014-08-14 16:01:25 +00001004
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001005 if (oflag & O_CREAT)
1006 mode = 0640;
Zachary Turner696b5282014-08-14 16:01:25 +00001007
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001008 error.SetError(
1009 ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode),
1010 eErrorTypePOSIX);
1011 if (error.Fail() || log)
1012 error.PutToLog(log,
1013 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
1014 static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode);
1015 }
1016 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001017 }
1018 return error.Success();
Todd Fialacacde7d2014-09-27 16:54:22 +00001019#else
1020 error.SetErrorString("Host::AddPosixSpawnFileAction() not supported on Android");
1021 return false;
1022#endif
Zachary Turner696b5282014-08-14 16:01:25 +00001023}
Todd Fiala76747122014-01-23 00:52:28 +00001024#endif // LaunchProcedssPosixSpawn: Apple, Linux, FreeBSD and other GLIBC systems
1025
Zachary Turner172d37d2014-10-14 21:55:08 +00001026#if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__NetBSD__) || defined(_WIN32)
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001027// The functions below implement process launching via posix_spawn() for Linux,
1028// FreeBSD and NetBSD.
Daniel Malea4244cbd2013-08-27 20:58:59 +00001029
1030Error
1031Host::LaunchProcess (ProcessLaunchInfo &launch_info)
1032{
Zachary Turner172d37d2014-10-14 21:55:08 +00001033 std::unique_ptr<ProcessLauncher> delegate_launcher;
1034#if defined(_WIN32)
1035 delegate_launcher.reset(new ProcessLauncherWindows());
1036#else
1037 delegate_launcher.reset(new ProcessLauncherPosix());
1038#endif
1039 MonitoringProcessLauncher launcher(std::move(delegate_launcher));
1040
Daniel Malea4244cbd2013-08-27 20:58:59 +00001041 Error error;
Zachary Turner172d37d2014-10-14 21:55:08 +00001042 HostProcess process = launcher.LaunchProcess(launch_info, error);
Daniel Malea4244cbd2013-08-27 20:58:59 +00001043
Zachary Turner172d37d2014-10-14 21:55:08 +00001044 // TODO(zturner): It would be better if the entire HostProcess were returned instead of writing
1045 // it into this structure.
1046 launch_info.SetProcessID(process.GetProcessId());
Daniel Malea4244cbd2013-08-27 20:58:59 +00001047
Daniel Malea4244cbd2013-08-27 20:58:59 +00001048 return error;
1049}
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001050#endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001051
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001052#ifndef _WIN32
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001053void
1054Host::Kill(lldb::pid_t pid, int signo)
1055{
1056 ::kill(pid, signo);
1057}
Greg Claytone3e3fee2013-02-17 20:46:30 +00001058
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001059#endif
Greg Claytond1cf11a2012-04-14 01:42:46 +00001060
Johnny Chen8f3d8382011-08-02 20:52:42 +00001061#if !defined (__APPLE__)
Greg Clayton2bddd342010-09-07 20:11:56 +00001062bool
Greg Clayton3b147632010-12-18 01:54:34 +00001063Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton2bddd342010-09-07 20:11:56 +00001064{
1065 return false;
1066}
Greg Claytondd36def2010-10-17 22:03:32 +00001067
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001068void
1069Host::SetCrashDescriptionWithFormat (const char *format, ...)
1070{
1071}
1072
1073void
1074Host::SetCrashDescription (const char *description)
1075{
1076}
Greg Claytondd36def2010-10-17 22:03:32 +00001077
1078lldb::pid_t
Daniel Maleae0f8f572013-08-26 23:57:52 +00001079Host::LaunchApplication (const FileSpec &app_file_spec)
Greg Claytondd36def2010-10-17 22:03:32 +00001080{
1081 return LLDB_INVALID_PROCESS_ID;
1082}
1083
Greg Claytonfbb76342013-11-20 21:07:01 +00001084#endif
Todd Fiala4ceced32014-08-29 17:35:57 +00001085
1086#if !defined (__linux__) && !defined (__FreeBSD__) && !defined (__NetBSD__)
1087
1088const lldb_private::UnixSignalsSP&
1089Host::GetUnixSignals ()
1090{
1091 static UnixSignalsSP s_unix_signals_sp (new UnixSignals ());
1092 return s_unix_signals_sp;
1093}
1094
1095#endif