blob: 770ef0f3ceacb6c47f7d25962b34611613f3e98d [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
30#if !defined (__GNU__) && !defined (_WIN32)
31// Does not exist under GNU/HURD or Windows
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__)
Daniel Malea4244cbd2013-08-27 20:58:59 +000042#include <spawn.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000043#include <sys/wait.h>
Michael Sartainc2052432013-08-01 18:51:08 +000044#include <sys/syscall.h>
Ed Maste8b006c62013-06-25 18:58:11 +000045#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000046
Ed Maste8b006c62013-06-25 18:58:11 +000047#if defined (__FreeBSD__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000048#include <pthread_np.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000049#endif
50
Todd Fiala17096d72014-07-16 19:03:16 +000051// C++ includes
52#include <limits>
53
Greg Clayton2bddd342010-09-07 20:11:56 +000054#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000055#include "lldb/Host/HostInfo.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000056#include "lldb/Core/ArchSpec.h"
57#include "lldb/Core/ConstString.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000058#include "lldb/Core/Debugger.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000059#include "lldb/Core/Error.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000060#include "lldb/Core/Log.h"
Daniel Malea4244cbd2013-08-27 20:58:59 +000061#include "lldb/Core/Module.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000062#include "lldb/Core/StreamString.h"
Jim Inghamc075ecd2012-05-04 19:24:49 +000063#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton45319462011-02-08 00:35:34 +000064#include "lldb/Host/Config.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000065#include "lldb/Host/Endian.h"
Greg Claytone996fd32011-03-08 22:40:15 +000066#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000067#include "lldb/Host/FileSystem.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000068#include "lldb/Host/Mutex.h"
Zachary Turner696b5282014-08-14 16:01:25 +000069#include "lldb/Target/FileAction.h"
Greg Claytone996fd32011-03-08 22:40:15 +000070#include "lldb/Target/Process.h"
Zachary Turner696b5282014-08-14 16:01:25 +000071#include "lldb/Target/ProcessLaunchInfo.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000072#include "lldb/Target/TargetList.h"
Daniel Malea4244cbd2013-08-27 20:58:59 +000073#include "lldb/Utility/CleanUp.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000074
Saleem Abdulrasool28606952014-06-27 05:17:41 +000075#include "llvm/ADT/STLExtras.h"
Daniel Maleafa7425d2013-08-06 21:40:08 +000076#include "llvm/ADT/SmallString.h"
Stephen Wilsonbd588712011-02-24 19:15:09 +000077#include "llvm/Support/Host.h"
Zachary Turner9e757b72014-07-28 16:45:05 +000078#include "llvm/Support/Path.h"
Daniel Maleafa7425d2013-08-06 21:40:08 +000079#include "llvm/Support/raw_ostream.h"
Stephen Wilsonbd588712011-02-24 19:15:09 +000080
Todd Fiala76747122014-01-23 00:52:28 +000081#if defined (__APPLE__)
82#ifndef _POSIX_SPAWN_DISABLE_ASLR
83#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
84#endif
85
86extern "C"
87{
88 int __pthread_chdir(const char *path);
89 int __pthread_fchdir (int fildes);
90}
91
92#endif
Greg Clayton32e0a752011-03-30 18:16:51 +000093
Greg Clayton2bddd342010-09-07 20:11:56 +000094using namespace lldb;
95using namespace lldb_private;
96
Todd Fiala17096d72014-07-16 19:03:16 +000097// Define maximum thread name length
98#if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__NetBSD__)
99uint32_t const Host::MAX_THREAD_NAME_LENGTH = 16;
100#else
101uint32_t const Host::MAX_THREAD_NAME_LENGTH = std::numeric_limits<uint32_t>::max ();
102#endif
Greg Claytone4e45922011-11-16 05:37:56 +0000103
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000104#if !defined (__APPLE__) && !defined (_WIN32)
Greg Clayton2bddd342010-09-07 20:11:56 +0000105struct MonitorInfo
106{
107 lldb::pid_t pid; // The process ID to monitor
108 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
109 void *callback_baton; // The callback baton for the callback function
110 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
111};
112
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000113static thread_result_t
Greg Clayton2bddd342010-09-07 20:11:56 +0000114MonitorChildProcessThreadFunction (void *arg);
115
116lldb::thread_t
117Host::StartMonitoringChildProcess
118(
119 Host::MonitorChildProcessCallback callback,
120 void *callback_baton,
121 lldb::pid_t pid,
122 bool monitor_signals
123)
124{
125 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Claytone4e45922011-11-16 05:37:56 +0000126 MonitorInfo * info_ptr = new MonitorInfo();
Daniel Malea4244cbd2013-08-27 20:58:59 +0000127
Greg Claytone4e45922011-11-16 05:37:56 +0000128 info_ptr->pid = pid;
129 info_ptr->callback = callback;
130 info_ptr->callback_baton = callback_baton;
131 info_ptr->monitor_signals = monitor_signals;
132
133 char thread_name[256];
Daniel Malead01b2952012-11-29 21:49:15 +0000134 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
Greg Claytone4e45922011-11-16 05:37:56 +0000135 thread = ThreadCreate (thread_name,
136 MonitorChildProcessThreadFunction,
137 info_ptr,
138 NULL);
139
Greg Clayton2bddd342010-09-07 20:11:56 +0000140 return thread;
141}
142
143//------------------------------------------------------------------
144// Scoped class that will disable thread canceling when it is
145// constructed, and exception safely restore the previous value it
146// when it goes out of scope.
147//------------------------------------------------------------------
148class ScopedPThreadCancelDisabler
149{
150public:
151 ScopedPThreadCancelDisabler()
152 {
153 // Disable the ability for this thread to be cancelled
154 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
155 if (err != 0)
156 m_old_state = -1;
157
158 }
159
160 ~ScopedPThreadCancelDisabler()
161 {
162 // Restore the ability for this thread to be cancelled to what it
163 // previously was.
164 if (m_old_state != -1)
165 ::pthread_setcancelstate (m_old_state, 0);
166 }
167private:
168 int m_old_state; // Save the old cancelability state.
169};
170
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
203 ::pthread_testcancel ();
Matt Kopec650648f2013-01-08 16:30:18 +0000204 // Get signals from all children with same process group of pid
Daniel Malea4244cbd2013-08-27 20:58:59 +0000205 const ::pid_t wait_pid = ::waitpid (pid, &status, options);
Greg Clayton2bddd342010-09-07 20:11:56 +0000206 ::pthread_testcancel ();
207
208 if (wait_pid == -1)
209 {
210 if (errno == EINTR)
211 continue;
212 else
Andrew Kaylor93132f52013-05-28 23:04:25 +0000213 {
214 if (log)
215 log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno));
Greg Clayton2bddd342010-09-07 20:11:56 +0000216 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000217 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000218 }
Matt Kopec650648f2013-01-08 16:30:18 +0000219 else if (wait_pid > 0)
Greg Clayton2bddd342010-09-07 20:11:56 +0000220 {
221 bool exited = false;
222 int signal = 0;
223 int exit_status = 0;
224 const char *status_cstr = NULL;
225 if (WIFSTOPPED(status))
226 {
227 signal = WSTOPSIG(status);
228 status_cstr = "STOPPED";
229 }
230 else if (WIFEXITED(status))
231 {
232 exit_status = WEXITSTATUS(status);
233 status_cstr = "EXITED";
Andrew Kaylor93132f52013-05-28 23:04:25 +0000234 exited = true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000235 }
236 else if (WIFSIGNALED(status))
237 {
238 signal = WTERMSIG(status);
239 status_cstr = "SIGNALED";
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +0000240 if (wait_pid == abs(pid)) {
Matt Kopec650648f2013-01-08 16:30:18 +0000241 exited = true;
242 exit_status = -1;
243 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000244 }
245 else
246 {
Johnny Chen44805302011-07-19 19:48:13 +0000247 status_cstr = "(\?\?\?)";
Greg Clayton2bddd342010-09-07 20:11:56 +0000248 }
249
250 // Scope for pthread_cancel_disabler
251 {
252 ScopedPThreadCancelDisabler pthread_cancel_disabler;
253
Caroline Tice20ad3c42010-10-29 21:48:37 +0000254 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000255 if (log)
Daniel Malea4244cbd2013-08-27 20:58:59 +0000256 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 +0000257 function,
258 wait_pid,
259 options,
Greg Clayton2bddd342010-09-07 20:11:56 +0000260 pid,
261 status,
262 status_cstr,
263 signal,
264 exit_status);
265
266 if (exited || (signal != 0 && monitor_signals))
267 {
Greg Claytone4e45922011-11-16 05:37:56 +0000268 bool callback_return = false;
269 if (callback)
Matt Kopec650648f2013-01-08 16:30:18 +0000270 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton2bddd342010-09-07 20:11:56 +0000271
272 // If our process exited, then this thread should exit
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +0000273 if (exited && wait_pid == abs(pid))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000274 {
275 if (log)
276 log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000277 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000278 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000279 // If the callback returns true, it means this process should
280 // exit
281 if (callback_return)
Andrew Kaylor93132f52013-05-28 23:04:25 +0000282 {
283 if (log)
284 log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000285 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000286 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000287 }
288 }
289 }
290 }
291
Caroline Tice20ad3c42010-10-29 21:48:37 +0000292 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000293 if (log)
294 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
295
296 return NULL;
297}
298
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000299#endif // #if !defined (__APPLE__) && !defined (_WIN32)
300
301#if !defined (__APPLE__)
Greg Claytone38a5ed2012-01-05 03:57:59 +0000302
303void
304Host::SystemLog (SystemLogType type, const char *format, va_list args)
305{
306 vfprintf (stderr, format, args);
307}
308
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000309#endif
Greg Claytone4e45922011-11-16 05:37:56 +0000310
Greg Claytone38a5ed2012-01-05 03:57:59 +0000311void
312Host::SystemLog (SystemLogType type, const char *format, ...)
313{
314 va_list args;
315 va_start (args, format);
316 SystemLog (type, format, args);
317 va_end (args);
318}
319
Greg Clayton2bddd342010-09-07 20:11:56 +0000320lldb::pid_t
321Host::GetCurrentProcessID()
322{
323 return ::getpid();
324}
325
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000326#ifndef _WIN32
327
Greg Clayton2bddd342010-09-07 20:11:56 +0000328lldb::tid_t
329Host::GetCurrentThreadID()
330{
331#if defined (__APPLE__)
Jean-Daniel Dupas3cfa8e22013-12-06 09:35:53 +0000332 // Calling "mach_thread_self()" bumps the reference count on the thread
Greg Clayton813ddfc2012-09-18 18:19:49 +0000333 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
334 // count.
335 thread_port_t thread_self = mach_thread_self();
336 mach_port_deallocate(mach_task_self(), thread_self);
337 return thread_self;
Johnny Chen8f3d8382011-08-02 20:52:42 +0000338#elif defined(__FreeBSD__)
339 return lldb::tid_t(pthread_getthreadid_np());
Michael Sartainc2052432013-08-01 18:51:08 +0000340#elif defined(__linux__)
341 return lldb::tid_t(syscall(SYS_gettid));
Greg Clayton2bddd342010-09-07 20:11:56 +0000342#else
343 return lldb::tid_t(pthread_self());
344#endif
345}
346
Jim Ingham372787f2012-04-07 00:00:41 +0000347lldb::thread_t
348Host::GetCurrentThread ()
349{
350 return lldb::thread_t(pthread_self());
351}
352
Greg Clayton2bddd342010-09-07 20:11:56 +0000353const char *
354Host::GetSignalAsCString (int signo)
355{
356 switch (signo)
357 {
358 case SIGHUP: return "SIGHUP"; // 1 hangup
359 case SIGINT: return "SIGINT"; // 2 interrupt
360 case SIGQUIT: return "SIGQUIT"; // 3 quit
361 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
362 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
363 case SIGABRT: return "SIGABRT"; // 6 abort()
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000364#if defined(SIGPOLL)
Sylvestre Ledrua4cc7de2013-12-13 09:51:39 +0000365#if !defined(SIGIO) || (SIGPOLL != SIGIO)
366// Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to
367// fail with 'multiple define cases with same value'
Greg Clayton2bddd342010-09-07 20:11:56 +0000368 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer44030f12011-11-04 16:06:40 +0000369#endif
Sylvestre Ledrua4cc7de2013-12-13 09:51:39 +0000370#endif
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000371#if defined(SIGEMT)
Greg Clayton2bddd342010-09-07 20:11:56 +0000372 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer44030f12011-11-04 16:06:40 +0000373#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000374 case SIGFPE: return "SIGFPE"; // 8 floating point exception
375 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
376 case SIGBUS: return "SIGBUS"; // 10 bus error
377 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
378 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
379 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
380 case SIGALRM: return "SIGALRM"; // 14 alarm clock
381 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
382 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
383 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
384 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
385 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
386 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
387 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
388 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000389#if defined(SIGIO)
Greg Clayton2bddd342010-09-07 20:11:56 +0000390 case SIGIO: return "SIGIO"; // 23 input/output possible signal
391#endif
392 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
393 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
394 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
395 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000396#if defined(SIGWINCH)
Greg Clayton2bddd342010-09-07 20:11:56 +0000397 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
Jean-Daniel Dupas13698b22013-12-12 18:12:16 +0000398#endif
399#if defined(SIGINFO)
Greg Clayton2bddd342010-09-07 20:11:56 +0000400 case SIGINFO: return "SIGINFO"; // 29 information request
401#endif
402 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
403 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
404 default:
405 break;
406 }
407 return NULL;
408}
409
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000410#endif
411
Greg Clayton2bddd342010-09-07 20:11:56 +0000412void
413Host::WillTerminate ()
414{
415}
416
Sylvestre Ledru59405832013-07-01 08:21:36 +0000417#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
Matt Kopec62502c62013-05-13 19:33:58 +0000418
Greg Clayton2bddd342010-09-07 20:11:56 +0000419void
420Host::ThreadCreated (const char *thread_name)
421{
422}
Greg Claytone5219662010-12-03 06:02:24 +0000423
Peter Collingbourne2ced9132011-08-05 00:35:43 +0000424void
Greg Claytone5219662010-12-03 06:02:24 +0000425Host::Backtrace (Stream &strm, uint32_t max_frames)
426{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000427 // TODO: Is there a way to backtrace the current process on other systems?
Greg Claytone5219662010-12-03 06:02:24 +0000428}
429
Greg Clayton85851dd2010-12-04 00:10:17 +0000430size_t
431Host::GetEnvironment (StringList &env)
432{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000433 // TODO: Is there a way to the host environment for this process on other systems?
Greg Clayton85851dd2010-12-04 00:10:17 +0000434 return 0;
435}
436
Sylvestre Ledru59405832013-07-01 08:21:36 +0000437#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000438
439struct HostThreadCreateInfo
440{
441 std::string thread_name;
442 thread_func_t thread_fptr;
443 thread_arg_t thread_arg;
444
445 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
446 thread_name (name ? name : ""),
447 thread_fptr (fptr),
448 thread_arg (arg)
449 {
450 }
451};
452
453static thread_result_t
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000454#ifdef _WIN32
455__stdcall
456#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000457ThreadCreateTrampoline (thread_arg_t arg)
458{
459 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
460 Host::ThreadCreated (info->thread_name.c_str());
461 thread_func_t thread_fptr = info->thread_fptr;
462 thread_arg_t thread_arg = info->thread_arg;
463
Greg Clayton5160ce52013-03-27 23:08:40 +0000464 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton2bddd342010-09-07 20:11:56 +0000465 if (log)
466 log->Printf("thread created");
467
468 delete info;
469 return thread_fptr (thread_arg);
470}
471
472lldb::thread_t
473Host::ThreadCreate
474(
475 const char *thread_name,
476 thread_func_t thread_fptr,
477 thread_arg_t thread_arg,
478 Error *error
479)
480{
481 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
482
483 // Host::ThreadCreateTrampoline will delete this pointer for us.
484 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
485
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000486#ifdef _WIN32
487 thread = ::_beginthreadex(0, 0, ThreadCreateTrampoline, info_ptr, 0, NULL);
488 int err = thread <= 0 ? GetLastError() : 0;
489#else
Greg Clayton2bddd342010-09-07 20:11:56 +0000490 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000491#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000492 if (err == 0)
493 {
494 if (error)
495 error->Clear();
496 return thread;
497 }
498
499 if (error)
500 error->SetError (err, eErrorTypePOSIX);
501
502 return LLDB_INVALID_HOST_THREAD;
503}
504
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000505#ifndef _WIN32
506
Greg Clayton2bddd342010-09-07 20:11:56 +0000507bool
508Host::ThreadCancel (lldb::thread_t thread, Error *error)
509{
510 int err = ::pthread_cancel (thread);
511 if (error)
512 error->SetError(err, eErrorTypePOSIX);
513 return err == 0;
514}
515
516bool
517Host::ThreadDetach (lldb::thread_t thread, Error *error)
518{
519 int err = ::pthread_detach (thread);
520 if (error)
521 error->SetError(err, eErrorTypePOSIX);
522 return err == 0;
523}
524
525bool
526Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
527{
528 int err = ::pthread_join (thread, thread_result_ptr);
529 if (error)
530 error->SetError(err, eErrorTypePOSIX);
531 return err == 0;
532}
533
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000534lldb::thread_key_t
535Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback)
536{
537 pthread_key_t key;
538 ::pthread_key_create (&key, callback);
539 return key;
540}
541
542void*
543Host::ThreadLocalStorageGet(lldb::thread_key_t key)
544{
545 return ::pthread_getspecific (key);
546}
547
548void
549Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value)
550{
551 ::pthread_setspecific (key, value);
552}
553
Matt Kopec62502c62013-05-13 19:33:58 +0000554bool
Greg Clayton2bddd342010-09-07 20:11:56 +0000555Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
556{
Greg Clayton85719632013-02-27 22:51:58 +0000557#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton2bddd342010-09-07 20:11:56 +0000558 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
559 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
560 if (pid == LLDB_INVALID_PROCESS_ID)
561 pid = curr_pid;
562
563 if (tid == LLDB_INVALID_THREAD_ID)
564 tid = curr_tid;
565
Greg Clayton2bddd342010-09-07 20:11:56 +0000566 // Set the pthread name if possible
567 if (pid == curr_pid && tid == curr_tid)
568 {
Matt Kopec62502c62013-05-13 19:33:58 +0000569 if (::pthread_setname_np (name) == 0)
570 return true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000571 }
Matt Kopec62502c62013-05-13 19:33:58 +0000572 return false;
Ed Maste02983be2013-07-25 19:10:32 +0000573#elif defined (__FreeBSD__)
574 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
575 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
576 if (pid == LLDB_INVALID_PROCESS_ID)
577 pid = curr_pid;
578
579 if (tid == LLDB_INVALID_THREAD_ID)
580 tid = curr_tid;
581
582 // Set the pthread name if possible
583 if (pid == curr_pid && tid == curr_tid)
584 {
Michael Sartainc2052432013-08-01 18:51:08 +0000585 ::pthread_set_name_np (::pthread_self(), name);
Ed Maste02983be2013-07-25 19:10:32 +0000586 return true;
587 }
588 return false;
Sylvestre Ledru59405832013-07-01 08:21:36 +0000589#elif defined (__linux__) || defined (__GLIBC__)
Matt Kopec62502c62013-05-13 19:33:58 +0000590 void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
591 if (fn)
592 {
Matt Kopec62502c62013-05-13 19:33:58 +0000593 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
594 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
Matt Kopec62502c62013-05-13 19:33:58 +0000595 if (pid == LLDB_INVALID_PROCESS_ID)
596 pid = curr_pid;
597
598 if (tid == LLDB_INVALID_THREAD_ID)
599 tid = curr_tid;
600
Michael Sartainc2052432013-08-01 18:51:08 +0000601 if (pid == curr_pid && tid == curr_tid)
Matt Kopec62502c62013-05-13 19:33:58 +0000602 {
Michael Sartainc2052432013-08-01 18:51:08 +0000603 int (*pthread_setname_np_func)(pthread_t thread, const char *name);
604 *reinterpret_cast<void **> (&pthread_setname_np_func) = fn;
605
606 if (pthread_setname_np_func (::pthread_self(), name) == 0)
Matt Kopec62502c62013-05-13 19:33:58 +0000607 return true;
608 }
609 }
610 return false;
Jim Ingham5c42d8a2013-05-15 18:27:08 +0000611#else
612 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000613#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000614}
615
Ed Maste02983be2013-07-25 19:10:32 +0000616bool
617Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid,
618 const char *thread_name, size_t len)
619{
Enrico Granata67530b62014-02-12 03:37:33 +0000620 std::unique_ptr<char[]> namebuf(new char[len+1]);
621
Ed Maste02983be2013-07-25 19:10:32 +0000622 // Thread names are coming in like '<lldb.comm.debugger.edit>' and
623 // '<lldb.comm.debugger.editline>'. So just chopping the end of the string
624 // off leads to a lot of similar named threads. Go through the thread name
625 // and search for the last dot and use that.
626 const char *lastdot = ::strrchr (thread_name, '.');
627
628 if (lastdot && lastdot != thread_name)
629 thread_name = lastdot + 1;
Enrico Granata67530b62014-02-12 03:37:33 +0000630 ::strncpy (namebuf.get(), thread_name, len);
Ed Maste02983be2013-07-25 19:10:32 +0000631 namebuf[len] = 0;
632
Enrico Granata67530b62014-02-12 03:37:33 +0000633 int namebuflen = strlen(namebuf.get());
Ed Maste02983be2013-07-25 19:10:32 +0000634 if (namebuflen > 0)
635 {
636 if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>')
637 {
638 // Trim off trailing '(' and '>' characters for a bit more cleanup.
639 namebuflen--;
640 namebuf[namebuflen] = 0;
641 }
Enrico Granata67530b62014-02-12 03:37:33 +0000642 return Host::SetThreadName (pid, tid, namebuf.get());
Ed Maste02983be2013-07-25 19:10:32 +0000643 }
644 return false;
645}
646
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000647#endif
648
Greg Clayton2bddd342010-09-07 20:11:56 +0000649#if !defined (__APPLE__) // see Host.mm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000650
651bool
652Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
653{
654 bundle.Clear();
655 return false;
656}
657
Greg Clayton2bddd342010-09-07 20:11:56 +0000658bool
Greg Claytondd36def2010-10-17 22:03:32 +0000659Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton2bddd342010-09-07 20:11:56 +0000660{
Greg Claytondd36def2010-10-17 22:03:32 +0000661 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000662}
663#endif
664
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000665#ifndef _WIN32
666
Greg Clayton45319462011-02-08 00:35:34 +0000667// Opaque info that tracks a dynamic library that was loaded
668struct DynamicLibraryInfo
Greg Clayton4272cc72011-02-02 02:24:04 +0000669{
Greg Clayton45319462011-02-08 00:35:34 +0000670 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
671 file_spec (fs),
672 open_options (o),
673 handle (h)
674 {
675 }
676
677 const FileSpec file_spec;
678 uint32_t open_options;
679 void * handle;
680};
681
682void *
683Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
684{
Greg Clayton4272cc72011-02-02 02:24:04 +0000685 char path[PATH_MAX];
686 if (file_spec.GetPath(path, sizeof(path)))
687 {
Greg Clayton45319462011-02-08 00:35:34 +0000688 int mode = 0;
689
690 if (options & eDynamicLibraryOpenOptionLazy)
691 mode |= RTLD_LAZY;
Greg Claytonf9399452011-02-08 05:24:57 +0000692 else
693 mode |= RTLD_NOW;
694
Greg Clayton45319462011-02-08 00:35:34 +0000695
696 if (options & eDynamicLibraryOpenOptionLocal)
697 mode |= RTLD_LOCAL;
698 else
699 mode |= RTLD_GLOBAL;
700
701#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
702 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
703 mode |= RTLD_FIRST;
Greg Clayton75852f52011-02-07 17:43:47 +0000704#endif
Greg Clayton45319462011-02-08 00:35:34 +0000705
706 void * opaque = ::dlopen (path, mode);
707
708 if (opaque)
Greg Clayton4272cc72011-02-02 02:24:04 +0000709 {
710 error.Clear();
Greg Clayton45319462011-02-08 00:35:34 +0000711 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton4272cc72011-02-02 02:24:04 +0000712 }
713 else
714 {
715 error.SetErrorString(::dlerror());
716 }
717 }
718 else
719 {
720 error.SetErrorString("failed to extract path");
721 }
Greg Clayton45319462011-02-08 00:35:34 +0000722 return NULL;
Greg Clayton4272cc72011-02-02 02:24:04 +0000723}
724
725Error
Greg Clayton45319462011-02-08 00:35:34 +0000726Host::DynamicLibraryClose (void *opaque)
Greg Clayton4272cc72011-02-02 02:24:04 +0000727{
728 Error error;
Greg Clayton45319462011-02-08 00:35:34 +0000729 if (opaque == NULL)
Greg Clayton4272cc72011-02-02 02:24:04 +0000730 {
731 error.SetErrorString ("invalid dynamic library handle");
732 }
Greg Clayton45319462011-02-08 00:35:34 +0000733 else
Greg Clayton4272cc72011-02-02 02:24:04 +0000734 {
Greg Clayton45319462011-02-08 00:35:34 +0000735 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
736 if (::dlclose (dylib_info->handle) != 0)
737 {
738 error.SetErrorString(::dlerror());
739 }
740
741 dylib_info->open_options = 0;
742 dylib_info->handle = 0;
743 delete dylib_info;
Greg Clayton4272cc72011-02-02 02:24:04 +0000744 }
745 return error;
746}
747
748void *
Greg Clayton45319462011-02-08 00:35:34 +0000749Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton4272cc72011-02-02 02:24:04 +0000750{
Greg Clayton45319462011-02-08 00:35:34 +0000751 if (opaque == NULL)
Greg Clayton4272cc72011-02-02 02:24:04 +0000752 {
753 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton4272cc72011-02-02 02:24:04 +0000754 }
Greg Clayton4272cc72011-02-02 02:24:04 +0000755 else
Greg Clayton45319462011-02-08 00:35:34 +0000756 {
757 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
758
759 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
760 if (symbol_addr)
761 {
762#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
763 // This host doesn't support limiting searches to this shared library
764 // so we need to verify that the match came from this shared library
765 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonf9399452011-02-08 05:24:57 +0000766 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton45319462011-02-08 00:35:34 +0000767 {
768 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
769 if (match_dylib_spec != dylib_info->file_spec)
770 {
771 char dylib_path[PATH_MAX];
772 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
773 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
774 else
775 error.SetErrorString ("symbol not found");
776 return NULL;
777 }
778 }
779#endif
780 error.Clear();
781 return symbol_addr;
782 }
783 else
784 {
785 error.SetErrorString(::dlerror());
786 }
787 }
788 return NULL;
Greg Clayton4272cc72011-02-02 02:24:04 +0000789}
Greg Claytondd36def2010-10-17 22:03:32 +0000790
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000791FileSpec
792Host::GetModuleFileSpecForHostAddress (const void *host_addr)
793{
794 FileSpec module_filespec;
795 Dl_info info;
796 if (::dladdr (host_addr, &info))
797 {
798 if (info.dli_fname)
799 module_filespec.SetFile(info.dli_fname, true);
800 }
801 return module_filespec;
802}
803
804#endif
805
Matt Kopec085d6ce2013-05-31 22:00:07 +0000806#if !defined(__linux__)
807bool
808Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
809{
810 return false;
811}
812#endif
813
Sean Callananc0a6e062011-10-27 21:22:25 +0000814lldb::TargetSP
815Host::GetDummyTarget (lldb_private::Debugger &debugger)
816{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000817 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasb0183452012-05-17 15:48:02 +0000818
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000819 // FIXME: Maybe the dummy target should be per-Debugger
820 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
821 {
822 ArchSpec arch(Target::GetDefaultArchitecture());
823 if (!arch.IsValid())
Zachary Turner13b18262014-08-20 16:42:51 +0000824 arch = HostInfo::GetArchitecture();
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000825 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000826 NULL,
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000827 arch.GetTriple().getTriple().c_str(),
828 false,
829 NULL,
830 g_dummy_target_sp);
831 }
Filipe Cabecinhasb0183452012-05-17 15:48:02 +0000832
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000833 return g_dummy_target_sp;
Sean Callananc0a6e062011-10-27 21:22:25 +0000834}
835
Greg Claytond1cf11a2012-04-14 01:42:46 +0000836struct ShellInfo
837{
838 ShellInfo () :
839 process_reaped (false),
840 can_delete (false),
841 pid (LLDB_INVALID_PROCESS_ID),
842 signo(-1),
843 status(-1)
844 {
845 }
846
847 lldb_private::Predicate<bool> process_reaped;
848 lldb_private::Predicate<bool> can_delete;
849 lldb::pid_t pid;
850 int signo;
851 int status;
852};
853
854static bool
855MonitorShellCommand (void *callback_baton,
856 lldb::pid_t pid,
857 bool exited, // True if the process did exit
858 int signo, // Zero for no signal
859 int status) // Exit value of process if signal is zero
860{
861 ShellInfo *shell_info = (ShellInfo *)callback_baton;
862 shell_info->pid = pid;
863 shell_info->signo = signo;
864 shell_info->status = status;
865 // Let the thread running Host::RunShellCommand() know that the process
866 // exited and that ShellInfo has been filled in by broadcasting to it
867 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
868 // Now wait for a handshake back from that thread running Host::RunShellCommand
869 // so we know that we can delete shell_info_ptr
870 shell_info->can_delete.WaitForValueEqualTo(true);
871 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
872 usleep(1000);
873 // Now delete the shell info that was passed into this function
874 delete shell_info;
875 return true;
876}
877
878Error
879Host::RunShellCommand (const char *command,
880 const char *working_dir,
881 int *status_ptr,
882 int *signo_ptr,
883 std::string *command_output_ptr,
Greg Claytonc8f814d2012-09-27 03:13:55 +0000884 uint32_t timeout_sec,
885 const char *shell)
Greg Claytond1cf11a2012-04-14 01:42:46 +0000886{
887 Error error;
888 ProcessLaunchInfo launch_info;
Greg Claytonc8f814d2012-09-27 03:13:55 +0000889 if (shell && shell[0])
890 {
891 // Run the command in a shell
892 launch_info.SetShell(shell);
893 launch_info.GetArguments().AppendArgument(command);
894 const bool localhost = true;
895 const bool will_debug = false;
896 const bool first_arg_is_full_shell_command = true;
897 launch_info.ConvertArgumentsForLaunchingInShell (error,
898 localhost,
899 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +0000900 first_arg_is_full_shell_command,
901 0);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000902 }
903 else
904 {
905 // No shell, just run it
906 Args args (command);
907 const bool first_arg_is_executable = true;
Greg Clayton45392552012-10-17 22:57:12 +0000908 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000909 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000910
911 if (working_dir)
912 launch_info.SetWorkingDirectory(working_dir);
Greg Claytonc6931fc2013-12-04 18:53:50 +0000913 char output_file_path_buffer[PATH_MAX];
Greg Claytond1cf11a2012-04-14 01:42:46 +0000914 const char *output_file_path = NULL;
Greg Claytonc6931fc2013-12-04 18:53:50 +0000915
Greg Claytond1cf11a2012-04-14 01:42:46 +0000916 if (command_output_ptr)
917 {
918 // Create a temporary file to get the stdout/stderr and redirect the
919 // output of the command into this file. We will later read this file
920 // if all goes well and fill the data into "command_output_ptr"
Greg Claytonc6931fc2013-12-04 18:53:50 +0000921 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000922 if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Claytonc6931fc2013-12-04 18:53:50 +0000923 {
Zachary Turnerc25146b2014-08-21 23:56:55 +0000924 tmpdir_file_spec.AppendPathComponent("lldb-shell-output.XXXXXX");
Greg Claytonc6931fc2013-12-04 18:53:50 +0000925 strncpy(output_file_path_buffer, tmpdir_file_spec.GetPath().c_str(), sizeof(output_file_path_buffer));
926 }
927 else
928 {
929 strncpy(output_file_path_buffer, "/tmp/lldb-shell-output.XXXXXX", sizeof(output_file_path_buffer));
930 }
931
932 output_file_path = ::mktemp(output_file_path_buffer);
933 }
934
935 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
936 if (output_file_path)
937 {
Greg Claytond1cf11a2012-04-14 01:42:46 +0000938 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonc8f814d2012-09-27 03:13:55 +0000939 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000940 }
941 else
942 {
Greg Claytond1cf11a2012-04-14 01:42:46 +0000943 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
944 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
945 }
946
947 // The process monitor callback will delete the 'shell_info_ptr' below...
Greg Clayton7b0992d2013-04-18 22:45:39 +0000948 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
Greg Claytond1cf11a2012-04-14 01:42:46 +0000949
950 const bool monitor_signals = false;
951 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
952
953 error = LaunchProcess (launch_info);
954 const lldb::pid_t pid = launch_info.GetProcessID();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000955
956 if (error.Success() && pid == LLDB_INVALID_PROCESS_ID)
957 error.SetErrorString("failed to get process ID");
958
959 if (error.Success())
Greg Claytond1cf11a2012-04-14 01:42:46 +0000960 {
961 // The process successfully launched, so we can defer ownership of
962 // "shell_info" to the MonitorShellCommand callback function that will
Greg Claytone01e07b2013-04-18 18:10:51 +0000963 // get called when the process dies. We release the unique pointer as it
Greg Claytond1cf11a2012-04-14 01:42:46 +0000964 // doesn't need to delete the ShellInfo anymore.
965 ShellInfo *shell_info = shell_info_ap.release();
Daniel Malea4244cbd2013-08-27 20:58:59 +0000966 TimeValue *timeout_ptr = nullptr;
Greg Claytond1cf11a2012-04-14 01:42:46 +0000967 TimeValue timeout_time(TimeValue::Now());
Daniel Malea4244cbd2013-08-27 20:58:59 +0000968 if (timeout_sec > 0) {
969 timeout_time.OffsetWithSeconds(timeout_sec);
970 timeout_ptr = &timeout_time;
971 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000972 bool timed_out = false;
Daniel Malea4244cbd2013-08-27 20:58:59 +0000973 shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000974 if (timed_out)
975 {
976 error.SetErrorString("timed out waiting for shell command to complete");
Daniel Malea4244cbd2013-08-27 20:58:59 +0000977
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000978 // Kill the process since it didn't complete within the timeout specified
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000979 Kill (pid, SIGKILL);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000980 // Wait for the monitor callback to get the message
981 timeout_time = TimeValue::Now();
982 timeout_time.OffsetWithSeconds(1);
983 timed_out = false;
984 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
985 }
986 else
987 {
988 if (status_ptr)
989 *status_ptr = shell_info->status;
990
991 if (signo_ptr)
992 *signo_ptr = shell_info->signo;
993
994 if (command_output_ptr)
995 {
996 command_output_ptr->clear();
997 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
998 uint64_t file_size = file_spec.GetByteSize();
999 if (file_size > 0)
1000 {
1001 if (file_size > command_output_ptr->max_size())
1002 {
1003 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1004 }
1005 else
1006 {
1007 command_output_ptr->resize(file_size);
1008 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1009 }
1010 }
1011 }
1012 }
1013 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1014 }
Greg Claytond1cf11a2012-04-14 01:42:46 +00001015
1016 if (output_file_path)
1017 ::unlink (output_file_path);
1018 // Handshake with the monitor thread, or just let it know in advance that
1019 // it can delete "shell_info" in case we timed out and were not able to kill
1020 // the process...
1021 return error;
1022}
1023
Daniel Malea4244cbd2013-08-27 20:58:59 +00001024
Todd Fiala76747122014-01-23 00:52:28 +00001025// LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC
1026// systems
1027
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001028#if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__)
Todd Fiala76747122014-01-23 00:52:28 +00001029
1030// this method needs to be visible to macosx/Host.cpp and
1031// common/Host.cpp.
1032
1033short
1034Host::GetPosixspawnFlags (ProcessLaunchInfo &launch_info)
1035{
1036 short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
1037
1038#if defined (__APPLE__)
1039 if (launch_info.GetFlags().Test (eLaunchFlagExec))
1040 flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag
1041
1042 if (launch_info.GetFlags().Test (eLaunchFlagDebug))
1043 flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag
1044
1045 if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR))
1046 flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag
1047
1048 if (launch_info.GetLaunchInSeparateProcessGroup())
1049 flags |= POSIX_SPAWN_SETPGROUP;
1050
1051#ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
1052#if defined (__APPLE__) && (defined (__x86_64__) || defined (__i386__))
1053 static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate;
1054 if (g_use_close_on_exec_flag == eLazyBoolCalculate)
1055 {
1056 g_use_close_on_exec_flag = eLazyBoolNo;
1057
1058 uint32_t major, minor, update;
Zachary Turner97a14e62014-08-19 17:18:29 +00001059 if (HostInfo::GetOSVersion(major, minor, update))
Todd Fiala76747122014-01-23 00:52:28 +00001060 {
1061 // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier
1062 if (major > 10 || (major == 10 && minor > 7))
1063 {
1064 // Only enable for 10.8 and later OS versions
1065 g_use_close_on_exec_flag = eLazyBoolYes;
1066 }
1067 }
1068 }
1069#else
1070 static LazyBool g_use_close_on_exec_flag = eLazyBoolYes;
1071#endif
1072 // Close all files exception those with file actions if this is supported.
1073 if (g_use_close_on_exec_flag == eLazyBoolYes)
1074 flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
1075#endif
1076#endif // #if defined (__APPLE__)
1077 return flags;
1078}
1079
1080Error
1081Host::LaunchProcessPosixSpawn (const char *exe_path, ProcessLaunchInfo &launch_info, ::pid_t &pid)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001082{
1083 Error error;
1084 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_PROCESS));
1085
Daniel Malea4244cbd2013-08-27 20:58:59 +00001086 posix_spawnattr_t attr;
Daniel Malea4244cbd2013-08-27 20:58:59 +00001087 error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +00001088
1089 if (error.Fail() || log)
1090 error.PutToLog(log, "::posix_spawnattr_init ( &attr )");
Daniel Malea4244cbd2013-08-27 20:58:59 +00001091 if (error.Fail())
1092 return error;
1093
1094 // Make a quick class that will cleanup the posix spawn attributes in case
1095 // we return in the middle of this function.
1096 lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy);
1097
1098 sigset_t no_signals;
1099 sigset_t all_signals;
1100 sigemptyset (&no_signals);
1101 sigfillset (&all_signals);
Todd Fiala76747122014-01-23 00:52:28 +00001102 ::posix_spawnattr_setsigmask(&attr, &no_signals);
Todd Fiala571768c2014-01-23 17:07:54 +00001103#if defined (__linux__) || defined (__FreeBSD__)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001104 ::posix_spawnattr_setsigdefault(&attr, &no_signals);
Todd Fiala76747122014-01-23 00:52:28 +00001105#else
1106 ::posix_spawnattr_setsigdefault(&attr, &all_signals);
1107#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +00001108
Todd Fiala76747122014-01-23 00:52:28 +00001109 short flags = GetPosixspawnFlags(launch_info);
Daniel Malea4244cbd2013-08-27 20:58:59 +00001110
1111 error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX);
Todd Fiala76747122014-01-23 00:52:28 +00001112 if (error.Fail() || log)
1113 error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags);
Daniel Malea4244cbd2013-08-27 20:58:59 +00001114 if (error.Fail())
1115 return error;
1116
Todd Fiala76747122014-01-23 00:52:28 +00001117 // posix_spawnattr_setbinpref_np appears to be an Apple extension per:
1118 // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/
1119#if defined (__APPLE__) && !defined (__arm__)
Jim Ingham90331d52014-02-21 01:25:21 +00001120
1121 // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell
1122 // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation
1123 // to do that job on OSX.
1124
1125 if (launch_info.GetShell() == nullptr)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001126 {
Jim Ingham90331d52014-02-21 01:25:21 +00001127 // We don't need to do this for ARM, and we really shouldn't now that we
1128 // have multiple CPU subtypes and no posix_spawnattr call that allows us
1129 // to set which CPU subtype to launch...
1130 const ArchSpec &arch_spec = launch_info.GetArchitecture();
1131 cpu_type_t cpu = arch_spec.GetMachOCPUType();
1132 cpu_type_t sub = arch_spec.GetMachOCPUSubType();
1133 if (cpu != 0 &&
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001134 cpu != static_cast<cpu_type_t>(UINT32_MAX) &&
1135 cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) &&
Jim Ingham90331d52014-02-21 01:25:21 +00001136 !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail
1137 {
1138 size_t ocount = 0;
1139 error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX);
1140 if (error.Fail() || log)
1141 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 +00001142
Jim Ingham90331d52014-02-21 01:25:21 +00001143 if (error.Fail() || ocount != 1)
1144 return error;
1145 }
Daniel Malea4244cbd2013-08-27 20:58:59 +00001146 }
1147
Todd Fiala76747122014-01-23 00:52:28 +00001148#endif
1149
1150 const char *tmp_argv[2];
1151 char * const *argv = (char * const*)launch_info.GetArguments().GetConstArgumentVector();
1152 char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector();
1153 if (argv == NULL)
1154 {
1155 // posix_spawn gets very unhappy if it doesn't have at least the program
1156 // name in argv[0]. One of the side affects I have noticed is the environment
1157 // variables don't make it into the child process if "argv == NULL"!!!
1158 tmp_argv[0] = exe_path;
1159 tmp_argv[1] = NULL;
1160 argv = (char * const*)tmp_argv;
1161 }
1162
1163#if !defined (__APPLE__)
1164 // manage the working directory
Daniel Malea4244cbd2013-08-27 20:58:59 +00001165 char current_dir[PATH_MAX];
1166 current_dir[0] = '\0';
Todd Fiala76747122014-01-23 00:52:28 +00001167#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +00001168
1169 const char *working_dir = launch_info.GetWorkingDirectory();
Todd Fiala76747122014-01-23 00:52:28 +00001170 if (working_dir)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001171 {
Todd Fiala76747122014-01-23 00:52:28 +00001172#if defined (__APPLE__)
1173 // Set the working directory on this thread only
1174 if (__pthread_chdir (working_dir) < 0) {
1175 if (errno == ENOENT) {
1176 error.SetErrorStringWithFormat("No such file or directory: %s", working_dir);
1177 } else if (errno == ENOTDIR) {
1178 error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir);
1179 } else {
1180 error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution.");
1181 }
1182 return error;
1183 }
1184#else
Daniel Malea4244cbd2013-08-27 20:58:59 +00001185 if (::getcwd(current_dir, sizeof(current_dir)) == NULL)
1186 {
1187 error.SetError(errno, eErrorTypePOSIX);
1188 error.LogIfError(log, "unable to save the current directory");
1189 return error;
1190 }
1191
1192 if (::chdir(working_dir) == -1)
1193 {
1194 error.SetError(errno, eErrorTypePOSIX);
1195 error.LogIfError(log, "unable to change working directory to %s", working_dir);
1196 return error;
1197 }
Todd Fiala76747122014-01-23 00:52:28 +00001198#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +00001199 }
1200
Todd Fiala76747122014-01-23 00:52:28 +00001201 const size_t num_file_actions = launch_info.GetNumFileActions ();
1202 if (num_file_actions > 0)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001203 {
Todd Fiala76747122014-01-23 00:52:28 +00001204 posix_spawn_file_actions_t file_actions;
1205 error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX);
1206 if (error.Fail() || log)
1207 error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )");
1208 if (error.Fail())
1209 return error;
1210
1211 // Make a quick class that will cleanup the posix spawn attributes in case
1212 // we return in the middle of this function.
1213 lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy);
1214
1215 for (size_t i=0; i<num_file_actions; ++i)
1216 {
Zachary Turner696b5282014-08-14 16:01:25 +00001217 const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i);
Todd Fiala76747122014-01-23 00:52:28 +00001218 if (launch_file_action)
1219 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001220 if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log, error))
Todd Fiala76747122014-01-23 00:52:28 +00001221 return error;
1222 }
1223 }
1224
1225 error.SetError (::posix_spawnp (&pid,
1226 exe_path,
1227 &file_actions,
1228 &attr,
1229 argv,
1230 envp),
1231 eErrorTypePOSIX);
1232
1233 if (error.Fail() || log)
1234 {
1235 error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001236 pid, exe_path, static_cast<void*>(&file_actions),
1237 static_cast<void*>(&attr),
1238 reinterpret_cast<const void*>(argv),
1239 reinterpret_cast<const void*>(envp));
Todd Fiala76747122014-01-23 00:52:28 +00001240 if (log)
1241 {
1242 for (int ii=0; argv[ii]; ++ii)
1243 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
1244 }
1245 }
1246
1247 }
1248 else
1249 {
1250 error.SetError (::posix_spawnp (&pid,
1251 exe_path,
1252 NULL,
1253 &attr,
1254 argv,
1255 envp),
1256 eErrorTypePOSIX);
1257
1258 if (error.Fail() || log)
1259 {
1260 error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = NULL, attr = %p, argv = %p, envp = %p )",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001261 pid, exe_path, static_cast<void*>(&attr),
1262 reinterpret_cast<const void*>(argv),
1263 reinterpret_cast<const void*>(envp));
Todd Fiala76747122014-01-23 00:52:28 +00001264 if (log)
1265 {
1266 for (int ii=0; argv[ii]; ++ii)
1267 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
1268 }
1269 }
Daniel Malea4244cbd2013-08-27 20:58:59 +00001270 }
1271
Todd Fiala76747122014-01-23 00:52:28 +00001272 if (working_dir)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001273 {
Todd Fiala76747122014-01-23 00:52:28 +00001274#if defined (__APPLE__)
1275 // No more thread specific current working directory
1276 __pthread_fchdir (-1);
1277#else
1278 if (::chdir(current_dir) == -1 && error.Success())
1279 {
1280 error.SetError(errno, eErrorTypePOSIX);
1281 error.LogIfError(log, "unable to change current directory back to %s",
1282 current_dir);
1283 }
1284#endif
Daniel Malea4244cbd2013-08-27 20:58:59 +00001285 }
1286
1287 return error;
1288}
1289
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001290bool
1291Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error)
Zachary Turner696b5282014-08-14 16:01:25 +00001292{
1293 if (info == NULL)
1294 return false;
1295
1296 posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions);
1297
1298 switch (info->GetAction())
1299 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001300 case FileAction::eFileActionNone:
1301 error.Clear();
1302 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001303
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001304 case FileAction::eFileActionClose:
1305 if (info->GetFD() == -1)
1306 error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)");
1307 else
1308 {
1309 error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX);
1310 if (log && (error.Fail() || log))
1311 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
1312 static_cast<void *>(file_actions), info->GetFD());
1313 }
1314 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001315
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001316 case FileAction::eFileActionDuplicate:
1317 if (info->GetFD() == -1)
1318 error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)");
1319 else if (info->GetActionArgument() == -1)
1320 error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
1321 else
1322 {
1323 error.SetError(
1324 ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()),
1325 eErrorTypePOSIX);
1326 if (log && (error.Fail() || log))
1327 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
1328 static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument());
1329 }
1330 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001331
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001332 case FileAction::eFileActionOpen:
1333 if (info->GetFD() == -1)
1334 error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)");
1335 else
1336 {
1337 int oflag = info->GetActionArgument();
Zachary Turner696b5282014-08-14 16:01:25 +00001338
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001339 mode_t mode = 0;
Zachary Turner696b5282014-08-14 16:01:25 +00001340
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001341 if (oflag & O_CREAT)
1342 mode = 0640;
Zachary Turner696b5282014-08-14 16:01:25 +00001343
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001344 error.SetError(
1345 ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode),
1346 eErrorTypePOSIX);
1347 if (error.Fail() || log)
1348 error.PutToLog(log,
1349 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
1350 static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode);
1351 }
1352 break;
Zachary Turner696b5282014-08-14 16:01:25 +00001353 }
1354 return error.Success();
1355}
1356
Todd Fiala76747122014-01-23 00:52:28 +00001357#endif // LaunchProcedssPosixSpawn: Apple, Linux, FreeBSD and other GLIBC systems
1358
1359
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001360#if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__NetBSD__)
1361// The functions below implement process launching via posix_spawn() for Linux,
1362// FreeBSD and NetBSD.
Daniel Malea4244cbd2013-08-27 20:58:59 +00001363
1364Error
1365Host::LaunchProcess (ProcessLaunchInfo &launch_info)
1366{
1367 Error error;
1368 char exe_path[PATH_MAX];
1369
1370 PlatformSP host_platform_sp (Platform::GetDefaultPlatform ());
1371
1372 const ArchSpec &arch_spec = launch_info.GetArchitecture();
1373
1374 FileSpec exe_spec(launch_info.GetExecutableFile());
1375
1376 FileSpec::FileType file_type = exe_spec.GetFileType();
1377 if (file_type != FileSpec::eFileTypeRegular)
1378 {
1379 lldb::ModuleSP exe_module_sp;
1380 error = host_platform_sp->ResolveExecutable (exe_spec,
1381 arch_spec,
1382 exe_module_sp,
1383 NULL);
1384
1385 if (error.Fail())
1386 return error;
1387
1388 if (exe_module_sp)
1389 exe_spec = exe_module_sp->GetFileSpec();
1390 }
1391
1392 if (exe_spec.Exists())
1393 {
1394 exe_spec.GetPath (exe_path, sizeof(exe_path));
1395 }
1396 else
1397 {
1398 launch_info.GetExecutableFile().GetPath (exe_path, sizeof(exe_path));
1399 error.SetErrorStringWithFormat ("executable doesn't exist: '%s'", exe_path);
1400 return error;
1401 }
1402
1403 assert(!launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY));
1404
1405 ::pid_t pid = LLDB_INVALID_PROCESS_ID;
1406
1407 error = LaunchProcessPosixSpawn(exe_path, launch_info, pid);
1408
1409 if (pid != LLDB_INVALID_PROCESS_ID)
1410 {
1411 // If all went well, then set the process ID into the launch info
1412 launch_info.SetProcessID(pid);
1413
Todd Fiala76747122014-01-23 00:52:28 +00001414 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1415
Daniel Malea4244cbd2013-08-27 20:58:59 +00001416 // Make sure we reap any processes we spawn or we will have zombies.
1417 if (!launch_info.MonitorProcess())
1418 {
1419 const bool monitor_signals = false;
1420 StartMonitoringChildProcess (Process::SetProcessExitStatus,
1421 NULL,
1422 pid,
1423 monitor_signals);
Todd Fiala76747122014-01-23 00:52:28 +00001424 if (log)
1425 log->PutCString ("monitored child process with default Process::SetProcessExitStatus.");
1426 }
1427 else
1428 {
1429 if (log)
1430 log->PutCString ("monitored child process with user-specified process monitor.");
Daniel Malea4244cbd2013-08-27 20:58:59 +00001431 }
1432 }
1433 else
1434 {
1435 // Invalid process ID, something didn't go well
1436 if (error.Success())
1437 error.SetErrorString ("process launch failed for unknown reasons");
1438 }
1439 return error;
1440}
1441
Joerg Sonnenbergera53b3592014-05-02 19:09:40 +00001442#endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Daniel Malea4244cbd2013-08-27 20:58:59 +00001443
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001444#ifndef _WIN32
1445
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001446void
1447Host::Kill(lldb::pid_t pid, int signo)
1448{
1449 ::kill(pid, signo);
1450}
Greg Claytone3e3fee2013-02-17 20:46:30 +00001451
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001452#endif
Greg Claytond1cf11a2012-04-14 01:42:46 +00001453
Johnny Chen8f3d8382011-08-02 20:52:42 +00001454#if !defined (__APPLE__)
Greg Clayton2bddd342010-09-07 20:11:56 +00001455bool
Greg Clayton3b147632010-12-18 01:54:34 +00001456Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton2bddd342010-09-07 20:11:56 +00001457{
1458 return false;
1459}
Greg Claytondd36def2010-10-17 22:03:32 +00001460
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001461void
1462Host::SetCrashDescriptionWithFormat (const char *format, ...)
1463{
1464}
1465
1466void
1467Host::SetCrashDescription (const char *description)
1468{
1469}
Greg Claytondd36def2010-10-17 22:03:32 +00001470
1471lldb::pid_t
Daniel Maleae0f8f572013-08-26 23:57:52 +00001472Host::LaunchApplication (const FileSpec &app_file_spec)
Greg Claytondd36def2010-10-17 22:03:32 +00001473{
1474 return LLDB_INVALID_PROCESS_ID;
1475}
1476
Greg Claytonfbb76342013-11-20 21:07:01 +00001477#endif