blob: ee0cb197105c1ee947c9a2a7004d6c0b05b58100 [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>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000017#ifdef _WIN32
18#include "lldb/Host/windows/windows.h"
19#include <winsock2.h>
Hafiz Abid Qadeera6678752014-03-10 22:31:39 +000020#include <ws2tcpip.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000021#else
Deepak Panickalb36da432014-01-13 14:55:15 +000022#include <unistd.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000023#include <dlfcn.h>
24#include <grp.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000025#include <netdb.h>
26#include <pwd.h>
Jason Molenda4a4b5dc2013-11-21 02:45:55 +000027#include <sys/stat.h>
Sylvestre Ledru785ee472013-09-05 15:39:09 +000028#endif
29
Todd Fialacacde7d2014-09-27 16:54:22 +000030#if !defined (__GNU__) && !defined (_WIN32) && !defined (__ANDROID__)
31// Does not exist under GNU/HURD, or Windows, or Android
Ed Mastef2b01622013-06-28 12:35:08 +000032#include <sys/sysctl.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000033#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000034
35#if defined (__APPLE__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000036#include <mach/mach_port.h>
Charles Davis510938e2013-08-27 05:04:57 +000037#include <mach/mach_init.h>
38#include <mach-o/dyld.h>
Ed Maste8b006c62013-06-25 18:58:11 +000039#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000040
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +000041#if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) || defined(__NetBSD__)
Todd Fialacacde7d2014-09-27 16:54:22 +000042#ifndef __ANDROID__
Daniel Malea4244cbd2013-08-27 20:58:59 +000043#include <spawn.h>
Todd Fialacacde7d2014-09-27 16:54:22 +000044#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000045#include <sys/wait.h>
Michael Sartainc2052432013-08-01 18:51:08 +000046#include <sys/syscall.h>
Ed Maste8b006c62013-06-25 18:58:11 +000047#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000048
Ed Maste8b006c62013-06-25 18:58:11 +000049#if defined (__FreeBSD__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000050#include <pthread_np.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000051#endif
52
Zachary Turner39de3112014-09-09 20:54:56 +000053#if defined(__linux__)
54#include "Plugins/Process/Linux/ProcFileReader.h"
55#endif
56
Todd Fiala17096d72014-07-16 19:03:16 +000057// C++ includes
58#include <limits>
59
Greg Clayton2bddd342010-09-07 20:11:56 +000060#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000061#include "lldb/Host/HostInfo.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000062#include "lldb/Core/ArchSpec.h"
63#include "lldb/Core/ConstString.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000064#include "lldb/Core/Debugger.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000065#include "lldb/Core/Error.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000066#include "lldb/Core/Log.h"
Daniel Malea4244cbd2013-08-27 20:58:59 +000067#include "lldb/Core/Module.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000068#include "lldb/Core/StreamString.h"
Jim Inghamc075ecd2012-05-04 19:24:49 +000069#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton45319462011-02-08 00:35:34 +000070#include "lldb/Host/Config.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000071#include "lldb/Host/Endian.h"
Greg Claytone996fd32011-03-08 22:40:15 +000072#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000073#include "lldb/Host/FileSystem.h"
Zachary Turner172d37d2014-10-14 21:55:08 +000074#include "lldb/Host/HostProcess.h"
75#include "lldb/Host/MonitoringProcessLauncher.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000076#include "lldb/Host/Mutex.h"
Zachary Turner172d37d2014-10-14 21:55:08 +000077#include "lldb/Host/ProcessLauncher.h"
Zachary Turner39de3112014-09-09 20:54:56 +000078#include "lldb/Host/ThreadLauncher.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000079#include "lldb/lldb-private-forward.h"
Zachary Turner696b5282014-08-14 16:01:25 +000080#include "lldb/Target/FileAction.h"
Greg Claytone996fd32011-03-08 22:40:15 +000081#include "lldb/Target/Process.h"
Zachary Turner696b5282014-08-14 16:01:25 +000082#include "lldb/Target/ProcessLaunchInfo.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000083#include "lldb/Target/TargetList.h"
Daniel Malea4244cbd2013-08-27 20:58:59 +000084#include "lldb/Utility/CleanUp.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000085
Saleem Abdulrasool28606952014-06-27 05:17:41 +000086#include "llvm/ADT/STLExtras.h"
Daniel Maleafa7425d2013-08-06 21:40:08 +000087#include "llvm/ADT/SmallString.h"
Stephen Wilsonbd588712011-02-24 19:15:09 +000088#include "llvm/Support/Host.h"
Zachary Turner9e757b72014-07-28 16:45:05 +000089#include "llvm/Support/Path.h"
Daniel Maleafa7425d2013-08-06 21:40:08 +000090#include "llvm/Support/raw_ostream.h"
Stephen Wilsonbd588712011-02-24 19:15:09 +000091
Zachary Turner172d37d2014-10-14 21:55:08 +000092#if defined(_WIN32)
93#include "lldb/Host/windows/ProcessLauncherWindows.h"
94#else
95#include "lldb/Host/posix/ProcessLauncherPosix.h"
96#endif
97
Todd Fiala76747122014-01-23 00:52:28 +000098#if defined (__APPLE__)
99#ifndef _POSIX_SPAWN_DISABLE_ASLR
100#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
101#endif
102
103extern "C"
104{
105 int __pthread_chdir(const char *path);
106 int __pthread_fchdir (int fildes);
107}
108
109#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000110
Greg Clayton2bddd342010-09-07 20:11:56 +0000111using namespace lldb;
112using namespace lldb_private;
113
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000114#if !defined (__APPLE__) && !defined (_WIN32)
Greg Clayton2bddd342010-09-07 20:11:56 +0000115struct MonitorInfo
116{
117 lldb::pid_t pid; // The process ID to monitor
118 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
119 void *callback_baton; // The callback baton for the callback function
120 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
121};
122
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000123static thread_result_t
Greg Clayton2bddd342010-09-07 20:11:56 +0000124MonitorChildProcessThreadFunction (void *arg);
125
Zachary Turner39de3112014-09-09 20:54:56 +0000126HostThread
127Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
Greg Clayton2bddd342010-09-07 20:11:56 +0000128{
Greg Claytone4e45922011-11-16 05:37:56 +0000129 MonitorInfo * info_ptr = new MonitorInfo();
Daniel Malea4244cbd2013-08-27 20:58:59 +0000130
Greg Claytone4e45922011-11-16 05:37:56 +0000131 info_ptr->pid = pid;
132 info_ptr->callback = callback;
133 info_ptr->callback_baton = callback_baton;
134 info_ptr->monitor_signals = monitor_signals;
135
136 char thread_name[256];
Zachary Turner39de3112014-09-09 20:54:56 +0000137 ::snprintf(thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
138 return ThreadLauncher::LaunchThread(thread_name, MonitorChildProcessThreadFunction, info_ptr, NULL);
Greg Clayton2bddd342010-09-07 20:11:56 +0000139}
140
Todd Fialacacde7d2014-09-27 16:54:22 +0000141#ifndef __ANDROID__
Greg Clayton2bddd342010-09-07 20:11:56 +0000142//------------------------------------------------------------------
143// Scoped class that will disable thread canceling when it is
144// constructed, and exception safely restore the previous value it
145// when it goes out of scope.
146//------------------------------------------------------------------
147class ScopedPThreadCancelDisabler
148{
149public:
150 ScopedPThreadCancelDisabler()
151 {
152 // Disable the ability for this thread to be cancelled
153 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
154 if (err != 0)
155 m_old_state = -1;
156
157 }
158
159 ~ScopedPThreadCancelDisabler()
160 {
161 // Restore the ability for this thread to be cancelled to what it
162 // previously was.
163 if (m_old_state != -1)
164 ::pthread_setcancelstate (m_old_state, 0);
165 }
166private:
167 int m_old_state; // Save the old cancelability state.
168};
Todd Fialacacde7d2014-09-27 16:54:22 +0000169#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000170
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000171static thread_result_t
Greg Clayton2bddd342010-09-07 20:11:56 +0000172MonitorChildProcessThreadFunction (void *arg)
173{
Greg Clayton5160ce52013-03-27 23:08:40 +0000174 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton2bddd342010-09-07 20:11:56 +0000175 const char *function = __FUNCTION__;
176 if (log)
177 log->Printf ("%s (arg = %p) thread starting...", function, arg);
178
179 MonitorInfo *info = (MonitorInfo *)arg;
180
181 const Host::MonitorChildProcessCallback callback = info->callback;
182 void * const callback_baton = info->callback_baton;
Greg Clayton2bddd342010-09-07 20:11:56 +0000183 const bool monitor_signals = info->monitor_signals;
184
Daniel Malea4244cbd2013-08-27 20:58:59 +0000185 assert (info->pid <= UINT32_MAX);
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000186 const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000187
Greg Clayton2bddd342010-09-07 20:11:56 +0000188 delete info;
189
190 int status = -1;
Sylvestre Ledru59405832013-07-01 08:21:36 +0000191#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000192 #define __WALL 0
193#endif
Matt Kopec650648f2013-01-08 16:30:18 +0000194 const int options = __WALL;
195
Greg Clayton2bddd342010-09-07 20:11:56 +0000196 while (1)
197 {
Caroline Tice20ad3c42010-10-29 21:48:37 +0000198 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000199 if (log)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000200 log->Printf("%s ::wait_pid (pid = %" PRIi32 ", &status, options = %i)...", function, pid, options);
Greg Clayton2bddd342010-09-07 20:11:56 +0000201
202 // Wait for all child processes
Todd Fialacacde7d2014-09-27 16:54:22 +0000203#ifndef __ANDROID__
Greg Clayton2bddd342010-09-07 20:11:56 +0000204 ::pthread_testcancel ();
Todd Fialacacde7d2014-09-27 16:54:22 +0000205#endif
Matt Kopec650648f2013-01-08 16:30:18 +0000206 // Get signals from all children with same process group of pid
Daniel Malea4244cbd2013-08-27 20:58:59 +0000207 const ::pid_t wait_pid = ::waitpid (pid, &status, options);
Todd Fialacacde7d2014-09-27 16:54:22 +0000208#ifndef __ANDROID__
Greg Clayton2bddd342010-09-07 20:11:56 +0000209 ::pthread_testcancel ();
Todd Fialacacde7d2014-09-27 16:54:22 +0000210#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000211
212 if (wait_pid == -1)
213 {
214 if (errno == EINTR)
215 continue;
216 else
Andrew Kaylor93132f52013-05-28 23:04:25 +0000217 {
218 if (log)
219 log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno));
Greg Clayton2bddd342010-09-07 20:11:56 +0000220 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000221 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000222 }
Matt Kopec650648f2013-01-08 16:30:18 +0000223 else if (wait_pid > 0)
Greg Clayton2bddd342010-09-07 20:11:56 +0000224 {
225 bool exited = false;
226 int signal = 0;
227 int exit_status = 0;
228 const char *status_cstr = NULL;
229 if (WIFSTOPPED(status))
230 {
231 signal = WSTOPSIG(status);
232 status_cstr = "STOPPED";
233 }
234 else if (WIFEXITED(status))
235 {
236 exit_status = WEXITSTATUS(status);
237 status_cstr = "EXITED";
Andrew Kaylor93132f52013-05-28 23:04:25 +0000238 exited = true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000239 }
240 else if (WIFSIGNALED(status))
241 {
242 signal = WTERMSIG(status);
243 status_cstr = "SIGNALED";
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +0000244 if (wait_pid == abs(pid)) {
Matt Kopec650648f2013-01-08 16:30:18 +0000245 exited = true;
246 exit_status = -1;
247 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000248 }
249 else
250 {
Johnny Chen44805302011-07-19 19:48:13 +0000251 status_cstr = "(\?\?\?)";
Greg Clayton2bddd342010-09-07 20:11:56 +0000252 }
253
254 // Scope for pthread_cancel_disabler
255 {
Todd Fialacacde7d2014-09-27 16:54:22 +0000256#ifndef __ANDROID__
Greg Clayton2bddd342010-09-07 20:11:56 +0000257 ScopedPThreadCancelDisabler pthread_cancel_disabler;
Todd Fialacacde7d2014-09-27 16:54:22 +0000258#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000259
Caroline Tice20ad3c42010-10-29 21:48:37 +0000260 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000261 if (log)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000262 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 +0000263 function,
264 wait_pid,
265 options,
Greg Clayton2bddd342010-09-07 20:11:56 +0000266 pid,
267 status,
268 status_cstr,
269 signal,
270 exit_status);
271
272 if (exited || (signal != 0 && monitor_signals))
273 {
Greg Claytone4e45922011-11-16 05:37:56 +0000274 bool callback_return = false;
275 if (callback)
Matt Kopec650648f2013-01-08 16:30:18 +0000276 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton2bddd342010-09-07 20:11:56 +0000277
278 // If our process exited, then this thread should exit
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +0000279 if (exited && wait_pid == abs(pid))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000280 {
281 if (log)
282 log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000283 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000284 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000285 // If the callback returns true, it means this process should
286 // exit
287 if (callback_return)
Andrew Kaylor93132f52013-05-28 23:04:25 +0000288 {
289 if (log)
290 log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000291 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000292 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000293 }
294 }
295 }
296 }
297
Caroline Tice20ad3c42010-10-29 21:48:37 +0000298 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000299 if (log)
300 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
301
302 return NULL;
303}
304
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000305#endif // #if !defined (__APPLE__) && !defined (_WIN32)
306
307#if !defined (__APPLE__)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000308
309void
310Host::SystemLog (SystemLogType type, const char *format, va_list args)
311{
312 vfprintf (stderr, format, args);
313}
314
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000315#endif
Greg Claytone4e45922011-11-16 05:37:56 +0000316
Greg Claytone38a5ed2012-01-05 03:57:59 +0000317void
318Host::SystemLog (SystemLogType type, const char *format, ...)
319{
320 va_list args;
321 va_start (args, format);
322 SystemLog (type, format, args);
323 va_end (args);
324}
325
Greg Clayton2bddd342010-09-07 20:11:56 +0000326lldb::pid_t
327Host::GetCurrentProcessID()
328{
329 return ::getpid();
330}
331
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000332#ifndef _WIN32
333
Greg Clayton2bddd342010-09-07 20:11:56 +0000334lldb::tid_t
335Host::GetCurrentThreadID()
336{
337#if defined (__APPLE__)
Jean-Daniel Dupas3cfa8e22013-12-06 09:35:53 +0000338 // Calling "mach_thread_self()" bumps the reference count on the thread
Greg Clayton813ddfc2012-09-18 18:19:49 +0000339 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
340 // count.
341 thread_port_t thread_self = mach_thread_self();
342 mach_port_deallocate(mach_task_self(), thread_self);
343 return thread_self;
Johnny Chen8f3d8382011-08-02 20:52:42 +0000344#elif defined(__FreeBSD__)
345 return lldb::tid_t(pthread_getthreadid_np());
Michael Sartainc2052432013-08-01 18:51:08 +0000346#elif defined(__linux__)
347 return lldb::tid_t(syscall(SYS_gettid));
Greg Clayton2bddd342010-09-07 20:11:56 +0000348#else
349 return lldb::tid_t(pthread_self());
350#endif
351}
352
Jim Ingham372787f2012-04-07 00:00:41 +0000353lldb::thread_t
354Host::GetCurrentThread ()
355{
356 return lldb::thread_t(pthread_self());
357}
358
Greg Clayton2bddd342010-09-07 20:11:56 +0000359const char *
360Host::GetSignalAsCString (int signo)
361{
362 switch (signo)
363 {
364 case SIGHUP: return "SIGHUP"; // 1 hangup
365 case SIGINT: return "SIGINT"; // 2 interrupt
366 case SIGQUIT: return "SIGQUIT"; // 3 quit
367 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
368 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
369 case SIGABRT: return "SIGABRT"; // 6 abort()
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000370#if defined(SIGPOLL)
Sylvestre Ledrua4cc7de2013-12-13 09:51:39 +0000371#if !defined(SIGIO) || (SIGPOLL != SIGIO)
372// Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to
373// fail with 'multiple define cases with same value'
Greg Clayton2bddd342010-09-07 20:11:56 +0000374 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer44030f12011-11-04 16:06:40 +0000375#endif
Sylvestre Ledrua4cc7de2013-12-13 09:51:39 +0000376#endif
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000377#if defined(SIGEMT)
Greg Clayton2bddd342010-09-07 20:11:56 +0000378 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer44030f12011-11-04 16:06:40 +0000379#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000380 case SIGFPE: return "SIGFPE"; // 8 floating point exception
381 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
382 case SIGBUS: return "SIGBUS"; // 10 bus error
383 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
384 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
385 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
386 case SIGALRM: return "SIGALRM"; // 14 alarm clock
387 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
388 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
389 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
390 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
391 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
392 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
393 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
394 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000395#if defined(SIGIO)
Greg Clayton2bddd342010-09-07 20:11:56 +0000396 case SIGIO: return "SIGIO"; // 23 input/output possible signal
397#endif
398 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
399 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
400 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
401 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000402#if defined(SIGWINCH)
Greg Clayton2bddd342010-09-07 20:11:56 +0000403 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000404#endif
405#if defined(SIGINFO)
Greg Clayton2bddd342010-09-07 20:11:56 +0000406 case SIGINFO: return "SIGINFO"; // 29 information request
407#endif
408 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
409 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
410 default:
411 break;
412 }
413 return NULL;
414}
415
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000416#endif
417
Greg Clayton2bddd342010-09-07 20:11:56 +0000418void
419Host::WillTerminate ()
420{
421}
422
Sylvestre Ledru59405832013-07-01 08:21:36 +0000423#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
Matt Kopec62502c62013-05-13 19:33:58 +0000424
Greg Clayton2bddd342010-09-07 20:11:56 +0000425void
Greg Claytone5219662010-12-03 06:02:24 +0000426Host::Backtrace (Stream &strm, uint32_t max_frames)
427{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000428 // TODO: Is there a way to backtrace the current process on other systems?
Greg Claytone5219662010-12-03 06:02:24 +0000429}
430
Greg Clayton85851dd2010-12-04 00:10:17 +0000431size_t
432Host::GetEnvironment (StringList &env)
433{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000434 // TODO: Is there a way to the host environment for this process on other systems?
Greg Clayton85851dd2010-12-04 00:10:17 +0000435 return 0;
436}
437
Sylvestre Ledru59405832013-07-01 08:21:36 +0000438#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000439
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000440#ifndef _WIN32
441
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000442lldb::thread_key_t
443Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback)
444{
445 pthread_key_t key;
446 ::pthread_key_create (&key, callback);
447 return key;
448}
449
450void*
451Host::ThreadLocalStorageGet(lldb::thread_key_t key)
452{
453 return ::pthread_getspecific (key);
454}
455
456void
457Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value)
458{
459 ::pthread_setspecific (key, value);
460}
461
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000462#endif
463
Greg Clayton2bddd342010-09-07 20:11:56 +0000464#if !defined (__APPLE__) // see Host.mm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000465
466bool
467Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
468{
469 bundle.Clear();
470 return false;
471}
472
Greg Clayton2bddd342010-09-07 20:11:56 +0000473bool
Greg Claytondd36def2010-10-17 22:03:32 +0000474Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton2bddd342010-09-07 20:11:56 +0000475{
Greg Claytondd36def2010-10-17 22:03:32 +0000476 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000477}
478#endif
479
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000480#ifndef _WIN32
481
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000482FileSpec
483Host::GetModuleFileSpecForHostAddress (const void *host_addr)
484{
485 FileSpec module_filespec;
Todd Fialacacde7d2014-09-27 16:54:22 +0000486#ifndef __ANDROID__
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000487 Dl_info info;
488 if (::dladdr (host_addr, &info))
489 {
490 if (info.dli_fname)
491 module_filespec.SetFile(info.dli_fname, true);
492 }
Todd Fialacacde7d2014-09-27 16:54:22 +0000493#else
494 assert(false && "dladdr() not supported on Android");
495#endif
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000496 return module_filespec;
497}
498
499#endif
500
Matt Kopec085d6ce2013-05-31 22:00:07 +0000501#if !defined(__linux__)
502bool
503Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
504{
505 return false;
506}
507#endif
508
Sean Callananc0a6e062011-10-27 21:22:25 +0000509lldb::TargetSP
510Host::GetDummyTarget (lldb_private::Debugger &debugger)
511{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000512 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasb0183452012-05-17 15:48:02 +0000513
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000514 // FIXME: Maybe the dummy target should be per-Debugger
515 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
516 {
517 ArchSpec arch(Target::GetDefaultArchitecture());
518 if (!arch.IsValid())
Zachary Turner13b18262014-08-20 16:42:51 +0000519 arch = HostInfo::GetArchitecture();
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000520 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000521 NULL,
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000522 arch.GetTriple().getTriple().c_str(),
523 false,
524 NULL,
525 g_dummy_target_sp);
526 }
Filipe Cabecinhasb0183452012-05-17 15:48:02 +0000527
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000528 return g_dummy_target_sp;
Sean Callananc0a6e062011-10-27 21:22:25 +0000529}
530
Greg Claytond1cf11a2012-04-14 01:42:46 +0000531struct ShellInfo
532{
533 ShellInfo () :
534 process_reaped (false),
535 can_delete (false),
536 pid (LLDB_INVALID_PROCESS_ID),
537 signo(-1),
538 status(-1)
539 {
540 }
541
542 lldb_private::Predicate<bool> process_reaped;
543 lldb_private::Predicate<bool> can_delete;
544 lldb::pid_t pid;
545 int signo;
546 int status;
547};
548
549static bool
550MonitorShellCommand (void *callback_baton,
551 lldb::pid_t pid,
552 bool exited, // True if the process did exit
553 int signo, // Zero for no signal
554 int status) // Exit value of process if signal is zero
555{
556 ShellInfo *shell_info = (ShellInfo *)callback_baton;
557 shell_info->pid = pid;
558 shell_info->signo = signo;
559 shell_info->status = status;
560 // Let the thread running Host::RunShellCommand() know that the process
561 // exited and that ShellInfo has been filled in by broadcasting to it
562 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
563 // Now wait for a handshake back from that thread running Host::RunShellCommand
564 // so we know that we can delete shell_info_ptr
565 shell_info->can_delete.WaitForValueEqualTo(true);
566 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
567 usleep(1000);
568 // Now delete the shell info that was passed into this function
569 delete shell_info;
570 return true;
571}
572
573Error
574Host::RunShellCommand (const char *command,
575 const char *working_dir,
576 int *status_ptr,
577 int *signo_ptr,
578 std::string *command_output_ptr,
Greg Claytonc8f814d2012-09-27 03:13:55 +0000579 uint32_t timeout_sec,
580 const char *shell)
Greg Claytond1cf11a2012-04-14 01:42:46 +0000581{
582 Error error;
583 ProcessLaunchInfo launch_info;
Greg Claytonc8f814d2012-09-27 03:13:55 +0000584 if (shell && shell[0])
585 {
586 // Run the command in a shell
587 launch_info.SetShell(shell);
588 launch_info.GetArguments().AppendArgument(command);
589 const bool localhost = true;
590 const bool will_debug = false;
591 const bool first_arg_is_full_shell_command = true;
592 launch_info.ConvertArgumentsForLaunchingInShell (error,
593 localhost,
594 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +0000595 first_arg_is_full_shell_command,
596 0);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000597 }
598 else
599 {
600 // No shell, just run it
601 Args args (command);
602 const bool first_arg_is_executable = true;
Greg Clayton45392552012-10-17 22:57:12 +0000603 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000604 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000605
606 if (working_dir)
607 launch_info.SetWorkingDirectory(working_dir);
Greg Claytonc6931fc2013-12-04 18:53:50 +0000608 char output_file_path_buffer[PATH_MAX];
Greg Claytond1cf11a2012-04-14 01:42:46 +0000609 const char *output_file_path = NULL;
Greg Claytonc6931fc2013-12-04 18:53:50 +0000610
Greg Claytond1cf11a2012-04-14 01:42:46 +0000611 if (command_output_ptr)
612 {
613 // Create a temporary file to get the stdout/stderr and redirect the
614 // output of the command into this file. We will later read this file
615 // if all goes well and fill the data into "command_output_ptr"
Greg Claytonc6931fc2013-12-04 18:53:50 +0000616 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000617 if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Claytonc6931fc2013-12-04 18:53:50 +0000618 {
Zachary Turnerc25146b2014-08-21 23:56:55 +0000619 tmpdir_file_spec.AppendPathComponent("lldb-shell-output.XXXXXX");
Greg Claytonc6931fc2013-12-04 18:53:50 +0000620 strncpy(output_file_path_buffer, tmpdir_file_spec.GetPath().c_str(), sizeof(output_file_path_buffer));
621 }
622 else
623 {
624 strncpy(output_file_path_buffer, "/tmp/lldb-shell-output.XXXXXX", sizeof(output_file_path_buffer));
625 }
626
627 output_file_path = ::mktemp(output_file_path_buffer);
628 }
629
630 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
631 if (output_file_path)
632 {
Greg Claytond1cf11a2012-04-14 01:42:46 +0000633 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000634 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000635 }
636 else
637 {
Greg Claytond1cf11a2012-04-14 01:42:46 +0000638 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
639 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
640 }
641
642 // The process monitor callback will delete the 'shell_info_ptr' below...
Greg Clayton7b0992d2013-04-18 22:45:39 +0000643 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
Greg Claytond1cf11a2012-04-14 01:42:46 +0000644
645 const bool monitor_signals = false;
646 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
647
648 error = LaunchProcess (launch_info);
649 const lldb::pid_t pid = launch_info.GetProcessID();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000650
651 if (error.Success() && pid == LLDB_INVALID_PROCESS_ID)
652 error.SetErrorString("failed to get process ID");
653
654 if (error.Success())
Greg Claytond1cf11a2012-04-14 01:42:46 +0000655 {
656 // The process successfully launched, so we can defer ownership of
657 // "shell_info" to the MonitorShellCommand callback function that will
Greg Claytone01e07b2013-04-18 18:10:51 +0000658 // get called when the process dies. We release the unique pointer as it
Greg Claytond1cf11a2012-04-14 01:42:46 +0000659 // doesn't need to delete the ShellInfo anymore.
660 ShellInfo *shell_info = shell_info_ap.release();
Daniel Malea4244cbd2013-08-27 20:58:59 +0000661 TimeValue *timeout_ptr = nullptr;
Greg Claytond1cf11a2012-04-14 01:42:46 +0000662 TimeValue timeout_time(TimeValue::Now());
Daniel Malea4244cbd2013-08-27 20:58:59 +0000663 if (timeout_sec > 0) {
664 timeout_time.OffsetWithSeconds(timeout_sec);
665 timeout_ptr = &timeout_time;
666 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000667 bool timed_out = false;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000668 shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000669 if (timed_out)
670 {
671 error.SetErrorString("timed out waiting for shell command to complete");
Daniel Malea4244cbd2013-08-27 20:58:59 +0000672
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000673 // Kill the process since it didn't complete within the timeout specified
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000674 Kill (pid, SIGKILL);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000675 // Wait for the monitor callback to get the message
676 timeout_time = TimeValue::Now();
677 timeout_time.OffsetWithSeconds(1);
678 timed_out = false;
679 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
680 }
681 else
682 {
683 if (status_ptr)
684 *status_ptr = shell_info->status;
685
686 if (signo_ptr)
687 *signo_ptr = shell_info->signo;
688
689 if (command_output_ptr)
690 {
691 command_output_ptr->clear();
692 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
693 uint64_t file_size = file_spec.GetByteSize();
694 if (file_size > 0)
695 {
696 if (file_size > command_output_ptr->max_size())
697 {
698 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
699 }
700 else
701 {
702 command_output_ptr->resize(file_size);
703 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
704 }
705 }
706 }
707 }
708 shell_info->can_delete.SetValue(true, eBroadcastAlways);
709 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000710
711 if (output_file_path)
712 ::unlink (output_file_path);
713 // Handshake with the monitor thread, or just let it know in advance that
714 // it can delete "shell_info" in case we timed out and were not able to kill
715 // the process...
716 return error;
717}
718
Daniel Malea4244cbd2013-08-27 20:58:59 +0000719
Todd Fiala76747122014-01-23 00:52:28 +0000720// LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC
721// systems
722
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +0000723#if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__)
Todd Fiala76747122014-01-23 00:52:28 +0000724
725// this method needs to be visible to macosx/Host.cpp and
726// common/Host.cpp.
727
728short
Zachary Turner172d37d2014-10-14 21:55:08 +0000729Host::GetPosixspawnFlags(const ProcessLaunchInfo &launch_info)
Todd Fiala76747122014-01-23 00:52:28 +0000730{
Todd Fialacacde7d2014-09-27 16:54:22 +0000731#ifndef __ANDROID__
Todd Fiala76747122014-01-23 00:52:28 +0000732 short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
733
734#if defined (__APPLE__)
735 if (launch_info.GetFlags().Test (eLaunchFlagExec))
736 flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag
737
738 if (launch_info.GetFlags().Test (eLaunchFlagDebug))
739 flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag
740
741 if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR))
742 flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag
743
744 if (launch_info.GetLaunchInSeparateProcessGroup())
745 flags |= POSIX_SPAWN_SETPGROUP;
746
747#ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
748#if defined (__APPLE__) && (defined (__x86_64__) || defined (__i386__))
749 static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate;
750 if (g_use_close_on_exec_flag == eLazyBoolCalculate)
751 {
752 g_use_close_on_exec_flag = eLazyBoolNo;
753
754 uint32_t major, minor, update;
Zachary Turner97a14e62014-08-19 17:18:29 +0000755 if (HostInfo::GetOSVersion(major, minor, update))
Todd Fiala76747122014-01-23 00:52:28 +0000756 {
757 // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier
758 if (major > 10 || (major == 10 && minor > 7))
759 {
760 // Only enable for 10.8 and later OS versions
761 g_use_close_on_exec_flag = eLazyBoolYes;
762 }
763 }
764 }
765#else
766 static LazyBool g_use_close_on_exec_flag = eLazyBoolYes;
767#endif
768 // Close all files exception those with file actions if this is supported.
769 if (g_use_close_on_exec_flag == eLazyBoolYes)
770 flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
771#endif
772#endif // #if defined (__APPLE__)
773 return flags;
Todd Fialacacde7d2014-09-27 16:54:22 +0000774#else
775 assert(false *&& "Host::GetPosixspawnFlags() not supported on Android");
776 return 0;
777#endif
Todd Fiala76747122014-01-23 00:52:28 +0000778}
779
780Error
Zachary Turner172d37d2014-10-14 21:55:08 +0000781Host::LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &launch_info, lldb::pid_t &pid)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000782{
783 Error error;
Todd Fialacacde7d2014-09-27 16:54:22 +0000784#ifndef __ANDROID__
Daniel Malea4244cbd2013-08-27 20:58:59 +0000785 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_PROCESS));
786
Daniel Malea4244cbd2013-08-27 20:58:59 +0000787 posix_spawnattr_t attr;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000788 error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000789
790 if (error.Fail() || log)
791 error.PutToLog(log, "::posix_spawnattr_init ( &attr )");
Daniel Malea4244cbd2013-08-27 20:58:59 +0000792 if (error.Fail())
793 return error;
794
795 // Make a quick class that will cleanup the posix spawn attributes in case
796 // we return in the middle of this function.
797 lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy);
798
799 sigset_t no_signals;
800 sigset_t all_signals;
801 sigemptyset (&no_signals);
802 sigfillset (&all_signals);
Todd Fiala76747122014-01-23 00:52:28 +0000803 ::posix_spawnattr_setsigmask(&attr, &no_signals);
Todd Fiala571768c2014-01-23 17:07:54 +0000804#if defined (__linux__) || defined (__FreeBSD__)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000805 ::posix_spawnattr_setsigdefault(&attr, &no_signals);
Todd Fiala76747122014-01-23 00:52:28 +0000806#else
807 ::posix_spawnattr_setsigdefault(&attr, &all_signals);
808#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000809
Todd Fiala76747122014-01-23 00:52:28 +0000810 short flags = GetPosixspawnFlags(launch_info);
Daniel Malea4244cbd2013-08-27 20:58:59 +0000811
812 error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000813 if (error.Fail() || log)
814 error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags);
Daniel Malea4244cbd2013-08-27 20:58:59 +0000815 if (error.Fail())
816 return error;
817
Todd Fiala76747122014-01-23 00:52:28 +0000818 // posix_spawnattr_setbinpref_np appears to be an Apple extension per:
819 // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/
820#if defined (__APPLE__) && !defined (__arm__)
Jim Ingham90331d52014-02-21 01:25:21 +0000821
822 // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell
823 // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation
824 // to do that job on OSX.
825
826 if (launch_info.GetShell() == nullptr)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000827 {
Jim Ingham90331d52014-02-21 01:25:21 +0000828 // We don't need to do this for ARM, and we really shouldn't now that we
829 // have multiple CPU subtypes and no posix_spawnattr call that allows us
830 // to set which CPU subtype to launch...
831 const ArchSpec &arch_spec = launch_info.GetArchitecture();
832 cpu_type_t cpu = arch_spec.GetMachOCPUType();
833 cpu_type_t sub = arch_spec.GetMachOCPUSubType();
834 if (cpu != 0 &&
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000835 cpu != static_cast<cpu_type_t>(UINT32_MAX) &&
836 cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) &&
Jim Ingham90331d52014-02-21 01:25:21 +0000837 !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail
838 {
839 size_t ocount = 0;
840 error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX);
841 if (error.Fail() || log)
842 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 +0000843
Jim Ingham90331d52014-02-21 01:25:21 +0000844 if (error.Fail() || ocount != 1)
845 return error;
846 }
Daniel Malea4244cbd2013-08-27 20:58:59 +0000847 }
848
Todd Fiala76747122014-01-23 00:52:28 +0000849#endif
850
851 const char *tmp_argv[2];
852 char * const *argv = (char * const*)launch_info.GetArguments().GetConstArgumentVector();
853 char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector();
854 if (argv == NULL)
855 {
856 // posix_spawn gets very unhappy if it doesn't have at least the program
857 // name in argv[0]. One of the side affects I have noticed is the environment
858 // variables don't make it into the child process if "argv == NULL"!!!
859 tmp_argv[0] = exe_path;
860 tmp_argv[1] = NULL;
861 argv = (char * const*)tmp_argv;
862 }
863
864#if !defined (__APPLE__)
865 // manage the working directory
Daniel Malea4244cbd2013-08-27 20:58:59 +0000866 char current_dir[PATH_MAX];
867 current_dir[0] = '\0';
Todd Fiala76747122014-01-23 00:52:28 +0000868#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000869
870 const char *working_dir = launch_info.GetWorkingDirectory();
Todd Fiala76747122014-01-23 00:52:28 +0000871 if (working_dir)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000872 {
Todd Fiala76747122014-01-23 00:52:28 +0000873#if defined (__APPLE__)
874 // Set the working directory on this thread only
875 if (__pthread_chdir (working_dir) < 0) {
876 if (errno == ENOENT) {
877 error.SetErrorStringWithFormat("No such file or directory: %s", working_dir);
878 } else if (errno == ENOTDIR) {
879 error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir);
880 } else {
881 error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution.");
882 }
883 return error;
884 }
885#else
Daniel Malea4244cbd2013-08-27 20:58:59 +0000886 if (::getcwd(current_dir, sizeof(current_dir)) == NULL)
887 {
888 error.SetError(errno, eErrorTypePOSIX);
889 error.LogIfError(log, "unable to save the current directory");
890 return error;
891 }
892
893 if (::chdir(working_dir) == -1)
894 {
895 error.SetError(errno, eErrorTypePOSIX);
896 error.LogIfError(log, "unable to change working directory to %s", working_dir);
897 return error;
898 }
Todd Fiala76747122014-01-23 00:52:28 +0000899#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000900 }
901
Zachary Turner172d37d2014-10-14 21:55:08 +0000902 ::pid_t result_pid = LLDB_INVALID_PROCESS_ID;
Todd Fiala76747122014-01-23 00:52:28 +0000903 const size_t num_file_actions = launch_info.GetNumFileActions ();
904 if (num_file_actions > 0)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000905 {
Todd Fiala76747122014-01-23 00:52:28 +0000906 posix_spawn_file_actions_t file_actions;
907 error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX);
908 if (error.Fail() || log)
909 error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )");
910 if (error.Fail())
911 return error;
912
913 // Make a quick class that will cleanup the posix spawn attributes in case
914 // we return in the middle of this function.
915 lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy);
916
917 for (size_t i=0; i<num_file_actions; ++i)
918 {
Zachary Turner696b5282014-08-14 16:01:25 +0000919 const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i);
Todd Fiala76747122014-01-23 00:52:28 +0000920 if (launch_file_action)
921 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000922 if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log, error))
Todd Fiala76747122014-01-23 00:52:28 +0000923 return error;
924 }
925 }
926
Zachary Turner172d37d2014-10-14 21:55:08 +0000927 error.SetError(::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000928
929 if (error.Fail() || log)
930 {
Zachary Turner172d37d2014-10-14 21:55:08 +0000931 error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", result_pid,
932 exe_path, static_cast<void *>(&file_actions), static_cast<void *>(&attr), reinterpret_cast<const void *>(argv),
933 reinterpret_cast<const void *>(envp));
Todd Fiala76747122014-01-23 00:52:28 +0000934 if (log)
935 {
936 for (int ii=0; argv[ii]; ++ii)
937 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
938 }
939 }
940
941 }
942 else
943 {
Zachary Turner172d37d2014-10-14 21:55:08 +0000944 error.SetError(::posix_spawnp(&result_pid, exe_path, NULL, &attr, argv, envp), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +0000945
946 if (error.Fail() || log)
947 {
948 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 +0000949 result_pid, exe_path, static_cast<void *>(&attr), reinterpret_cast<const void *>(argv),
950 reinterpret_cast<const void *>(envp));
Todd Fiala76747122014-01-23 00:52:28 +0000951 if (log)
952 {
953 for (int ii=0; argv[ii]; ++ii)
954 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
955 }
956 }
Daniel Malea4244cbd2013-08-27 20:58:59 +0000957 }
Zachary Turner172d37d2014-10-14 21:55:08 +0000958 pid = result_pid;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000959
Todd Fiala76747122014-01-23 00:52:28 +0000960 if (working_dir)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000961 {
Todd Fiala76747122014-01-23 00:52:28 +0000962#if defined (__APPLE__)
963 // No more thread specific current working directory
964 __pthread_fchdir (-1);
965#else
966 if (::chdir(current_dir) == -1 && error.Success())
967 {
968 error.SetError(errno, eErrorTypePOSIX);
969 error.LogIfError(log, "unable to change current directory back to %s",
970 current_dir);
971 }
972#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000973 }
Todd Fialacacde7d2014-09-27 16:54:22 +0000974#else
975 error.SetErrorString("Host::LaunchProcessPosixSpawn() not supported on Android");
976#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +0000977
978 return error;
979}
980
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000981bool
982Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error)
Zachary Turner696b5282014-08-14 16:01:25 +0000983{
Todd Fialacacde7d2014-09-27 16:54:22 +0000984#ifndef __ANDROID__
Zachary Turner696b5282014-08-14 16:01:25 +0000985 if (info == NULL)
986 return false;
987
988 posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions);
989
990 switch (info->GetAction())
991 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000992 case FileAction::eFileActionNone:
993 error.Clear();
994 break;
Zachary Turner696b5282014-08-14 16:01:25 +0000995
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000996 case FileAction::eFileActionClose:
997 if (info->GetFD() == -1)
998 error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)");
999 else
1000 {
1001 error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX);
1002 if (log && (error.Fail() || log))
1003 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
1004 static_cast<void *>(file_actions), info->GetFD());
1005 }
1006 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001007
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001008 case FileAction::eFileActionDuplicate:
1009 if (info->GetFD() == -1)
1010 error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)");
1011 else if (info->GetActionArgument() == -1)
1012 error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
1013 else
1014 {
1015 error.SetError(
1016 ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()),
1017 eErrorTypePOSIX);
1018 if (log && (error.Fail() || log))
1019 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
1020 static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument());
1021 }
1022 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001023
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001024 case FileAction::eFileActionOpen:
1025 if (info->GetFD() == -1)
1026 error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)");
1027 else
1028 {
1029 int oflag = info->GetActionArgument();
Zachary Turner696b5282014-08-14 16:01:25 +00001030
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001031 mode_t mode = 0;
Zachary Turner696b5282014-08-14 16:01:25 +00001032
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001033 if (oflag & O_CREAT)
1034 mode = 0640;
Zachary Turner696b5282014-08-14 16:01:25 +00001035
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001036 error.SetError(
1037 ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode),
1038 eErrorTypePOSIX);
1039 if (error.Fail() || log)
1040 error.PutToLog(log,
1041 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
1042 static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode);
1043 }
1044 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001045 }
1046 return error.Success();
Todd Fialacacde7d2014-09-27 16:54:22 +00001047#else
1048 error.SetErrorString("Host::AddPosixSpawnFileAction() not supported on Android");
1049 return false;
1050#endif
Zachary Turner696b5282014-08-14 16:01:25 +00001051}
1052
Todd Fiala76747122014-01-23 00:52:28 +00001053#endif // LaunchProcedssPosixSpawn: Apple, Linux, FreeBSD and other GLIBC systems
1054
Zachary Turner172d37d2014-10-14 21:55:08 +00001055#if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__NetBSD__) || defined(_WIN32)
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001056// The functions below implement process launching via posix_spawn() for Linux,
1057// FreeBSD and NetBSD.
Daniel Malea4244cbd2013-08-27 20:58:59 +00001058
1059Error
1060Host::LaunchProcess (ProcessLaunchInfo &launch_info)
1061{
Zachary Turner172d37d2014-10-14 21:55:08 +00001062 std::unique_ptr<ProcessLauncher> delegate_launcher;
1063#if defined(_WIN32)
1064 delegate_launcher.reset(new ProcessLauncherWindows());
1065#else
1066 delegate_launcher.reset(new ProcessLauncherPosix());
1067#endif
1068 MonitoringProcessLauncher launcher(std::move(delegate_launcher));
1069
Daniel Malea4244cbd2013-08-27 20:58:59 +00001070 Error error;
Zachary Turner172d37d2014-10-14 21:55:08 +00001071 HostProcess process = launcher.LaunchProcess(launch_info, error);
Daniel Malea4244cbd2013-08-27 20:58:59 +00001072
Zachary Turner172d37d2014-10-14 21:55:08 +00001073 // TODO(zturner): It would be better if the entire HostProcess were returned instead of writing
1074 // it into this structure.
1075 launch_info.SetProcessID(process.GetProcessId());
Daniel Malea4244cbd2013-08-27 20:58:59 +00001076
Daniel Malea4244cbd2013-08-27 20:58:59 +00001077 return error;
1078}
1079
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001080#endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001081
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001082#ifndef _WIN32
1083
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001084void
1085Host::Kill(lldb::pid_t pid, int signo)
1086{
1087 ::kill(pid, signo);
1088}
Greg Claytone3e3fee2013-02-17 20:46:30 +00001089
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001090#endif
Greg Claytond1cf11a2012-04-14 01:42:46 +00001091
Johnny Chen8f3d8382011-08-02 20:52:42 +00001092#if !defined (__APPLE__)
Greg Clayton2bddd342010-09-07 20:11:56 +00001093bool
Greg Clayton3b147632010-12-18 01:54:34 +00001094Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton2bddd342010-09-07 20:11:56 +00001095{
1096 return false;
1097}
Greg Claytondd36def2010-10-17 22:03:32 +00001098
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001099void
1100Host::SetCrashDescriptionWithFormat (const char *format, ...)
1101{
1102}
1103
1104void
1105Host::SetCrashDescription (const char *description)
1106{
1107}
Greg Claytondd36def2010-10-17 22:03:32 +00001108
1109lldb::pid_t
Daniel Maleae0f8f572013-08-26 23:57:52 +00001110Host::LaunchApplication (const FileSpec &app_file_spec)
Greg Claytondd36def2010-10-17 22:03:32 +00001111{
1112 return LLDB_INVALID_PROCESS_ID;
1113}
1114
Greg Claytonfbb76342013-11-20 21:07:01 +00001115#endif
Todd Fiala4ceced32014-08-29 17:35:57 +00001116
1117#if !defined (__linux__) && !defined (__FreeBSD__) && !defined (__NetBSD__)
1118
1119const lldb_private::UnixSignalsSP&
1120Host::GetUnixSignals ()
1121{
1122 static UnixSignalsSP s_unix_signals_sp (new UnixSignals ());
1123 return s_unix_signals_sp;
1124}
1125
1126#endif