blob: 61386b5833b39d2091cc385960f213c27448e6c4 [file] [log] [blame]
Greg Clayton8f3b21d2010-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 Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton132c49a2013-02-17 20:46:30 +000012// C includes
13#include <dlfcn.h>
14#include <errno.h>
15#include <grp.h>
16#include <limits.h>
17#include <netdb.h>
18#include <pwd.h>
19#include <sys/sysctl.h>
20#include <sys/types.h>
21#include <unistd.h>
22
23#if defined (__APPLE__)
24
25#include <dispatch/dispatch.h>
26#include <libproc.h>
27#include <mach-o/dyld.h>
28#include <mach/mach_port.h>
29
30#elif defined (__linux__)
31
32#include <sys/wait.h>
33
34#elif defined (__FreeBSD__)
35
36#include <sys/wait.h>
37#include <pthread_np.h>
38
39#endif
40
Greg Clayton8f3b21d2010-09-07 20:11:56 +000041#include "lldb/Host/Host.h"
42#include "lldb/Core/ArchSpec.h"
43#include "lldb/Core/ConstString.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000044#include "lldb/Core/Debugger.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000045#include "lldb/Core/Error.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000046#include "lldb/Core/Log.h"
47#include "lldb/Core/StreamString.h"
Jim Ingham35dd4962012-05-04 19:24:49 +000048#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton14ef59f2011-02-08 00:35:34 +000049#include "lldb/Host/Config.h"
Greg Claytoncd548032011-02-01 01:31:41 +000050#include "lldb/Host/Endian.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000051#include "lldb/Host/FileSpec.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000052#include "lldb/Host/Mutex.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000053#include "lldb/Target/Process.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000054#include "lldb/Target/TargetList.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000055
Stephen Wilson7f513ba2011-02-24 19:15:09 +000056#include "llvm/Support/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000057#include "llvm/Support/MachO.h"
Daniel Malea21e32a62013-01-04 23:35:13 +000058#include "llvm/ADT/Twine.h"
Stephen Wilson7f513ba2011-02-24 19:15:09 +000059
Greg Clayton24bc5d92011-03-30 18:16:51 +000060
Greg Clayton8f3b21d2010-09-07 20:11:56 +000061
Greg Clayton14ef59f2011-02-08 00:35:34 +000062
Greg Clayton8f3b21d2010-09-07 20:11:56 +000063
64using namespace lldb;
65using namespace lldb_private;
66
Greg Clayton1c4642c2011-11-16 05:37:56 +000067
Greg Claytonc518fe72011-11-17 19:41:57 +000068#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +000069struct MonitorInfo
70{
71 lldb::pid_t pid; // The process ID to monitor
72 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
73 void *callback_baton; // The callback baton for the callback function
74 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
75};
76
77static void *
78MonitorChildProcessThreadFunction (void *arg);
79
80lldb::thread_t
81Host::StartMonitoringChildProcess
82(
83 Host::MonitorChildProcessCallback callback,
84 void *callback_baton,
85 lldb::pid_t pid,
86 bool monitor_signals
87)
88{
89 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton1c4642c2011-11-16 05:37:56 +000090 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton8f3b21d2010-09-07 20:11:56 +000091
Greg Clayton1c4642c2011-11-16 05:37:56 +000092 info_ptr->pid = pid;
93 info_ptr->callback = callback;
94 info_ptr->callback_baton = callback_baton;
95 info_ptr->monitor_signals = monitor_signals;
96
97 char thread_name[256];
Daniel Malea5f35a4b2012-11-29 21:49:15 +000098 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
Greg Clayton1c4642c2011-11-16 05:37:56 +000099 thread = ThreadCreate (thread_name,
100 MonitorChildProcessThreadFunction,
101 info_ptr,
102 NULL);
103
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000104 return thread;
105}
106
107//------------------------------------------------------------------
108// Scoped class that will disable thread canceling when it is
109// constructed, and exception safely restore the previous value it
110// when it goes out of scope.
111//------------------------------------------------------------------
112class ScopedPThreadCancelDisabler
113{
114public:
115 ScopedPThreadCancelDisabler()
116 {
117 // Disable the ability for this thread to be cancelled
118 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
119 if (err != 0)
120 m_old_state = -1;
121
122 }
123
124 ~ScopedPThreadCancelDisabler()
125 {
126 // Restore the ability for this thread to be cancelled to what it
127 // previously was.
128 if (m_old_state != -1)
129 ::pthread_setcancelstate (m_old_state, 0);
130 }
131private:
132 int m_old_state; // Save the old cancelability state.
133};
134
135static void *
136MonitorChildProcessThreadFunction (void *arg)
137{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000139 const char *function = __FUNCTION__;
140 if (log)
141 log->Printf ("%s (arg = %p) thread starting...", function, arg);
142
143 MonitorInfo *info = (MonitorInfo *)arg;
144
145 const Host::MonitorChildProcessCallback callback = info->callback;
146 void * const callback_baton = info->callback_baton;
147 const lldb::pid_t pid = info->pid;
148 const bool monitor_signals = info->monitor_signals;
149
150 delete info;
151
152 int status = -1;
Matt Kopecf1fda372013-01-08 16:30:18 +0000153 const int options = __WALL;
154
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000155 while (1)
156 {
Caroline Tice926060e2010-10-29 21:48:37 +0000157 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000158 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000159 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000160
161 // Wait for all child processes
162 ::pthread_testcancel ();
Matt Kopecf1fda372013-01-08 16:30:18 +0000163 // Get signals from all children with same process group of pid
164 const lldb::pid_t wait_pid = ::waitpid (-1*pid, &status, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000165 ::pthread_testcancel ();
166
167 if (wait_pid == -1)
168 {
169 if (errno == EINTR)
170 continue;
171 else
172 break;
173 }
Matt Kopecf1fda372013-01-08 16:30:18 +0000174 else if (wait_pid > 0)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000175 {
176 bool exited = false;
177 int signal = 0;
178 int exit_status = 0;
179 const char *status_cstr = NULL;
180 if (WIFSTOPPED(status))
181 {
182 signal = WSTOPSIG(status);
183 status_cstr = "STOPPED";
184 }
185 else if (WIFEXITED(status))
186 {
187 exit_status = WEXITSTATUS(status);
188 status_cstr = "EXITED";
Matt Kopecf1fda372013-01-08 16:30:18 +0000189 if (wait_pid == pid)
190 exited = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000191 }
192 else if (WIFSIGNALED(status))
193 {
194 signal = WTERMSIG(status);
195 status_cstr = "SIGNALED";
Matt Kopecf1fda372013-01-08 16:30:18 +0000196 if (wait_pid == pid) {
197 exited = true;
198 exit_status = -1;
199 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000200 }
201 else
202 {
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000203 status_cstr = "(\?\?\?)";
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000204 }
205
206 // Scope for pthread_cancel_disabler
207 {
208 ScopedPThreadCancelDisabler pthread_cancel_disabler;
209
Caroline Tice926060e2010-10-29 21:48:37 +0000210 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000211 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000212 log->Printf ("%s ::waitpid (pid = %" PRIu64 ", &status, options = %i) => pid = %" PRIu64 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000213 function,
214 wait_pid,
215 options,
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000216 pid,
217 status,
218 status_cstr,
219 signal,
220 exit_status);
221
222 if (exited || (signal != 0 && monitor_signals))
223 {
Greg Clayton1c4642c2011-11-16 05:37:56 +0000224 bool callback_return = false;
225 if (callback)
Matt Kopecf1fda372013-01-08 16:30:18 +0000226 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000227
228 // If our process exited, then this thread should exit
229 if (exited)
230 break;
231 // If the callback returns true, it means this process should
232 // exit
233 if (callback_return)
234 break;
235 }
236 }
237 }
238 }
239
Caroline Tice926060e2010-10-29 21:48:37 +0000240 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000241 if (log)
242 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
243
244 return NULL;
245}
246
Greg Claytondf6dc882012-01-05 03:57:59 +0000247
248void
249Host::SystemLog (SystemLogType type, const char *format, va_list args)
250{
251 vfprintf (stderr, format, args);
252}
253
Greg Clayton1c4642c2011-11-16 05:37:56 +0000254#endif // #if !defined (__APPLE__)
255
Greg Claytondf6dc882012-01-05 03:57:59 +0000256void
257Host::SystemLog (SystemLogType type, const char *format, ...)
258{
259 va_list args;
260 va_start (args, format);
261 SystemLog (type, format, args);
262 va_end (args);
263}
264
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000265size_t
266Host::GetPageSize()
267{
268 return ::getpagesize();
269}
270
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000271const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000272Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000273{
Greg Clayton395fc332011-02-15 21:59:32 +0000274 static bool g_supports_32 = false;
275 static bool g_supports_64 = false;
276 static ArchSpec g_host_arch_32;
277 static ArchSpec g_host_arch_64;
278
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000279#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000280
281 // Apple is different in that it can support both 32 and 64 bit executables
282 // in the same operating system running concurrently. Here we detect the
283 // correct host architectures for both 32 and 64 bit including if 64 bit
284 // executables are supported on the system.
285
286 if (g_supports_32 == false && g_supports_64 == false)
287 {
288 // All apple systems support 32 bit execution.
289 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000290 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000291 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000292 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000293 ArchSpec host_arch;
294 // These will tell us about the kernel architecture, which even on a 64
295 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000296 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
297 {
Greg Clayton395fc332011-02-15 21:59:32 +0000298 len = sizeof (cpusubtype);
299 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
300 cpusubtype = CPU_TYPE_ANY;
301
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000302 len = sizeof (is_64_bit_capable);
303 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
304 {
305 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000306 g_supports_64 = true;
307 }
308
309 if (is_64_bit_capable)
310 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000311#if defined (__i386__) || defined (__x86_64__)
312 if (cpusubtype == CPU_SUBTYPE_486)
313 cpusubtype = CPU_SUBTYPE_I386_ALL;
314#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000315 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000316 {
Greg Clayton395fc332011-02-15 21:59:32 +0000317 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000318 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
319 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000320 }
321 else
322 {
323 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000324 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000325 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000326 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000327 }
328 }
Greg Clayton395fc332011-02-15 21:59:32 +0000329 else
330 {
Greg Claytonb3448432011-03-24 21:19:54 +0000331 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000332 g_host_arch_64.Clear();
333 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000334 }
Greg Clayton395fc332011-02-15 21:59:32 +0000335 }
336
337#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000338
Greg Clayton395fc332011-02-15 21:59:32 +0000339 if (g_supports_32 == false && g_supports_64 == false)
340 {
Peter Collingbourneb47c9982011-11-05 01:35:31 +0000341 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000342
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000343 g_host_arch_32.Clear();
344 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000345
Greg Clayton7971a032012-10-11 17:38:58 +0000346 // If the OS is Linux, "unknown" in the vendor slot isn't what we want
347 // for the default triple. It's probably an artifact of config.guess.
348 if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor)
349 triple.setVendorName("");
350
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000351 switch (triple.getArch())
352 {
353 default:
354 g_host_arch_32.SetTriple(triple);
355 g_supports_32 = true;
356 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000357
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000358 case llvm::Triple::x86_64:
Greg Clayton1a450cd2012-09-07 17:49:29 +0000359 g_host_arch_64.SetTriple(triple);
360 g_supports_64 = true;
361 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
362 g_supports_32 = true;
363 break;
364
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000365 case llvm::Triple::sparcv9:
366 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000367 g_host_arch_64.SetTriple(triple);
368 g_supports_64 = true;
369 break;
370 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000371
372 g_supports_32 = g_host_arch_32.IsValid();
373 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000374 }
Greg Clayton395fc332011-02-15 21:59:32 +0000375
376#endif // #else for #if defined (__APPLE__)
377
378 if (arch_kind == eSystemDefaultArchitecture32)
379 return g_host_arch_32;
380 else if (arch_kind == eSystemDefaultArchitecture64)
381 return g_host_arch_64;
382
383 if (g_supports_64)
384 return g_host_arch_64;
385
386 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000387}
388
389const ConstString &
390Host::GetVendorString()
391{
392 static ConstString g_vendor;
393 if (!g_vendor)
394 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000395 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
396 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
397 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000398 }
399 return g_vendor;
400}
401
402const ConstString &
403Host::GetOSString()
404{
405 static ConstString g_os_string;
406 if (!g_os_string)
407 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000408 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
409 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
410 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000411 }
412 return g_os_string;
413}
414
415const ConstString &
416Host::GetTargetTriple()
417{
418 static ConstString g_host_triple;
419 if (!(g_host_triple))
420 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000421 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
422 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000423 }
424 return g_host_triple;
425}
426
427lldb::pid_t
428Host::GetCurrentProcessID()
429{
430 return ::getpid();
431}
432
433lldb::tid_t
434Host::GetCurrentThreadID()
435{
436#if defined (__APPLE__)
Greg Claytone93725b2012-09-18 18:19:49 +0000437 // Calling "mach_port_deallocate()" bumps the reference count on the thread
438 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
439 // count.
440 thread_port_t thread_self = mach_thread_self();
441 mach_port_deallocate(mach_task_self(), thread_self);
442 return thread_self;
Johnny Chen4b663292011-08-02 20:52:42 +0000443#elif defined(__FreeBSD__)
444 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000445#else
446 return lldb::tid_t(pthread_self());
447#endif
448}
449
Jim Ingham1831e782012-04-07 00:00:41 +0000450lldb::thread_t
451Host::GetCurrentThread ()
452{
453 return lldb::thread_t(pthread_self());
454}
455
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000456const char *
457Host::GetSignalAsCString (int signo)
458{
459 switch (signo)
460 {
461 case SIGHUP: return "SIGHUP"; // 1 hangup
462 case SIGINT: return "SIGINT"; // 2 interrupt
463 case SIGQUIT: return "SIGQUIT"; // 3 quit
464 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
465 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
466 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000467#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000468 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000469#endif
470#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000471 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000472#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000473 case SIGFPE: return "SIGFPE"; // 8 floating point exception
474 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
475 case SIGBUS: return "SIGBUS"; // 10 bus error
476 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
477 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
478 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
479 case SIGALRM: return "SIGALRM"; // 14 alarm clock
480 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
481 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
482 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
483 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
484 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
485 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
486 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
487 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
488#if !defined(_POSIX_C_SOURCE)
489 case SIGIO: return "SIGIO"; // 23 input/output possible signal
490#endif
491 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
492 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
493 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
494 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
495#if !defined(_POSIX_C_SOURCE)
496 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
497 case SIGINFO: return "SIGINFO"; // 29 information request
498#endif
499 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
500 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
501 default:
502 break;
503 }
504 return NULL;
505}
506
507void
508Host::WillTerminate ()
509{
510}
511
Johnny Chen4b663292011-08-02 20:52:42 +0000512#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000513void
514Host::ThreadCreated (const char *thread_name)
515{
516}
Greg Claytonb749a262010-12-03 06:02:24 +0000517
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000518void
Greg Claytonb749a262010-12-03 06:02:24 +0000519Host::Backtrace (Stream &strm, uint32_t max_frames)
520{
Greg Clayton52fd9842011-02-02 02:24:04 +0000521 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000522}
523
Greg Clayton638351a2010-12-04 00:10:17 +0000524size_t
525Host::GetEnvironment (StringList &env)
526{
Greg Clayton52fd9842011-02-02 02:24:04 +0000527 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000528 return 0;
529}
530
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000531#endif
532
533struct HostThreadCreateInfo
534{
535 std::string thread_name;
536 thread_func_t thread_fptr;
537 thread_arg_t thread_arg;
538
539 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
540 thread_name (name ? name : ""),
541 thread_fptr (fptr),
542 thread_arg (arg)
543 {
544 }
545};
546
547static thread_result_t
548ThreadCreateTrampoline (thread_arg_t arg)
549{
550 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
551 Host::ThreadCreated (info->thread_name.c_str());
552 thread_func_t thread_fptr = info->thread_fptr;
553 thread_arg_t thread_arg = info->thread_arg;
554
Greg Clayton952e9dc2013-03-27 23:08:40 +0000555 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000556 if (log)
557 log->Printf("thread created");
558
559 delete info;
560 return thread_fptr (thread_arg);
561}
562
563lldb::thread_t
564Host::ThreadCreate
565(
566 const char *thread_name,
567 thread_func_t thread_fptr,
568 thread_arg_t thread_arg,
569 Error *error
570)
571{
572 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
573
574 // Host::ThreadCreateTrampoline will delete this pointer for us.
575 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
576
577 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
578 if (err == 0)
579 {
580 if (error)
581 error->Clear();
582 return thread;
583 }
584
585 if (error)
586 error->SetError (err, eErrorTypePOSIX);
587
588 return LLDB_INVALID_HOST_THREAD;
589}
590
591bool
592Host::ThreadCancel (lldb::thread_t thread, Error *error)
593{
594 int err = ::pthread_cancel (thread);
595 if (error)
596 error->SetError(err, eErrorTypePOSIX);
597 return err == 0;
598}
599
600bool
601Host::ThreadDetach (lldb::thread_t thread, Error *error)
602{
603 int err = ::pthread_detach (thread);
604 if (error)
605 error->SetError(err, eErrorTypePOSIX);
606 return err == 0;
607}
608
609bool
610Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
611{
612 int err = ::pthread_join (thread, thread_result_ptr);
613 if (error)
614 error->SetError(err, eErrorTypePOSIX);
615 return err == 0;
616}
617
Jim Ingham35dd4962012-05-04 19:24:49 +0000618
Greg Clayton6f0165b2013-02-27 22:51:58 +0000619std::string
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000620Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
621{
Greg Clayton6f0165b2013-02-27 22:51:58 +0000622 std::string thread_name;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000623#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton6f0165b2013-02-27 22:51:58 +0000624 // We currently can only get the name of a thread in the current process.
625 if (pid == Host::GetCurrentProcessID())
626 {
627 char pthread_name[1024];
628 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000629 {
Greg Clayton6f0165b2013-02-27 22:51:58 +0000630 if (pthread_name[0])
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000631 {
Greg Clayton6f0165b2013-02-27 22:51:58 +0000632 thread_name = pthread_name;
Greg Clayton49ce6822010-10-31 03:01:06 +0000633 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000634 }
Greg Clayton6f0165b2013-02-27 22:51:58 +0000635 else
636 {
637 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
638 if (current_queue != NULL)
639 {
640 const char *queue_name = dispatch_queue_get_label (current_queue);
641 if (queue_name && queue_name[0])
642 {
643 thread_name = queue_name;
644 }
645 }
646 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000647 }
Greg Clayton6f0165b2013-02-27 22:51:58 +0000648#endif
649 return thread_name;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000650}
651
652void
653Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
654{
Greg Clayton6f0165b2013-02-27 22:51:58 +0000655#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000656 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
657 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
658 if (pid == LLDB_INVALID_PROCESS_ID)
659 pid = curr_pid;
660
661 if (tid == LLDB_INVALID_THREAD_ID)
662 tid = curr_tid;
663
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000664 // Set the pthread name if possible
665 if (pid == curr_pid && tid == curr_tid)
666 {
667 ::pthread_setname_np (name);
668 }
669#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000670}
671
672FileSpec
673Host::GetProgramFileSpec ()
674{
675 static FileSpec g_program_filespec;
676 if (!g_program_filespec)
677 {
678#if defined (__APPLE__)
679 char program_fullpath[PATH_MAX];
680 // If DST is NULL, then return the number of bytes needed.
681 uint32_t len = sizeof(program_fullpath);
682 int err = _NSGetExecutablePath (program_fullpath, &len);
683 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000684 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000685 else if (err == -1)
686 {
687 char *large_program_fullpath = (char *)::malloc (len + 1);
688
689 err = _NSGetExecutablePath (large_program_fullpath, &len);
690 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000691 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000692
693 ::free (large_program_fullpath);
694 }
695#elif defined (__linux__)
696 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000697 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
698 if (len > 0) {
699 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000700 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000701 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000702#elif defined (__FreeBSD__)
703 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
704 size_t exe_path_size;
705 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
706 {
Greg Clayton366795e2011-01-13 01:27:55 +0000707 char *exe_path = new char[exe_path_size];
708 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
709 g_program_filespec.SetFile(exe_path, false);
710 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000711 }
712#endif
713 }
714 return g_program_filespec;
715}
716
717FileSpec
718Host::GetModuleFileSpecForHostAddress (const void *host_addr)
719{
720 FileSpec module_filespec;
721 Dl_info info;
722 if (::dladdr (host_addr, &info))
723 {
724 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000725 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000726 }
727 return module_filespec;
728}
729
730#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000731
732bool
733Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
734{
735 bundle.Clear();
736 return false;
737}
738
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000739bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000740Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000741{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000742 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000743}
744#endif
745
Greg Clayton14ef59f2011-02-08 00:35:34 +0000746// Opaque info that tracks a dynamic library that was loaded
747struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000748{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000749 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
750 file_spec (fs),
751 open_options (o),
752 handle (h)
753 {
754 }
755
756 const FileSpec file_spec;
757 uint32_t open_options;
758 void * handle;
759};
760
761void *
762Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
763{
Greg Clayton52fd9842011-02-02 02:24:04 +0000764 char path[PATH_MAX];
765 if (file_spec.GetPath(path, sizeof(path)))
766 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000767 int mode = 0;
768
769 if (options & eDynamicLibraryOpenOptionLazy)
770 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000771 else
772 mode |= RTLD_NOW;
773
Greg Clayton14ef59f2011-02-08 00:35:34 +0000774
775 if (options & eDynamicLibraryOpenOptionLocal)
776 mode |= RTLD_LOCAL;
777 else
778 mode |= RTLD_GLOBAL;
779
780#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
781 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
782 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000783#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000784
785 void * opaque = ::dlopen (path, mode);
786
787 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000788 {
789 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000790 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000791 }
792 else
793 {
794 error.SetErrorString(::dlerror());
795 }
796 }
797 else
798 {
799 error.SetErrorString("failed to extract path");
800 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000801 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000802}
803
804Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000805Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000806{
807 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000808 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000809 {
810 error.SetErrorString ("invalid dynamic library handle");
811 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000812 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000813 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000814 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
815 if (::dlclose (dylib_info->handle) != 0)
816 {
817 error.SetErrorString(::dlerror());
818 }
819
820 dylib_info->open_options = 0;
821 dylib_info->handle = 0;
822 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000823 }
824 return error;
825}
826
827void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000828Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000829{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000830 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000831 {
832 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000833 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000834 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000835 {
836 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
837
838 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
839 if (symbol_addr)
840 {
841#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
842 // This host doesn't support limiting searches to this shared library
843 // so we need to verify that the match came from this shared library
844 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000845 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000846 {
847 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
848 if (match_dylib_spec != dylib_info->file_spec)
849 {
850 char dylib_path[PATH_MAX];
851 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
852 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
853 else
854 error.SetErrorString ("symbol not found");
855 return NULL;
856 }
857 }
858#endif
859 error.Clear();
860 return symbol_addr;
861 }
862 else
863 {
864 error.SetErrorString(::dlerror());
865 }
866 }
867 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000868}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000869
870bool
871Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
872{
Greg Clayton5d187e52011-01-08 20:28:42 +0000873 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000874 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
875 // on linux this is assumed to be the "lldb" main executable. If LLDB on
876 // linux is actually in a shared library (lldb.so??) then this function will
877 // need to be modified to "do the right thing".
878
879 switch (path_type)
880 {
881 case ePathTypeLLDBShlibDir:
882 {
883 static ConstString g_lldb_so_dir;
884 if (!g_lldb_so_dir)
885 {
886 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
887 g_lldb_so_dir = lldb_file_spec.GetDirectory();
888 }
889 file_spec.GetDirectory() = g_lldb_so_dir;
890 return file_spec.GetDirectory();
891 }
892 break;
893
894 case ePathTypeSupportExecutableDir:
895 {
896 static ConstString g_lldb_support_exe_dir;
897 if (!g_lldb_support_exe_dir)
898 {
899 FileSpec lldb_file_spec;
900 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
901 {
902 char raw_path[PATH_MAX];
903 char resolved_path[PATH_MAX];
904 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
905
906#if defined (__APPLE__)
907 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
908 if (framework_pos)
909 {
910 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000911#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000912 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000913#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000914 }
915#endif
916 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
917 g_lldb_support_exe_dir.SetCString(resolved_path);
918 }
919 }
920 file_spec.GetDirectory() = g_lldb_support_exe_dir;
921 return file_spec.GetDirectory();
922 }
923 break;
924
925 case ePathTypeHeaderDir:
926 {
927 static ConstString g_lldb_headers_dir;
928 if (!g_lldb_headers_dir)
929 {
930#if defined (__APPLE__)
931 FileSpec lldb_file_spec;
932 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
933 {
934 char raw_path[PATH_MAX];
935 char resolved_path[PATH_MAX];
936 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
937
938 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
939 if (framework_pos)
940 {
941 framework_pos += strlen("LLDB.framework");
942 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
943 }
944 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
945 g_lldb_headers_dir.SetCString(resolved_path);
946 }
947#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000948 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000949 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
950#endif
951 }
952 file_spec.GetDirectory() = g_lldb_headers_dir;
953 return file_spec.GetDirectory();
954 }
955 break;
956
957 case ePathTypePythonDir:
958 {
Greg Clayton24b48ff2010-10-17 22:03:32 +0000959 static ConstString g_lldb_python_dir;
960 if (!g_lldb_python_dir)
961 {
962 FileSpec lldb_file_spec;
963 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
964 {
965 char raw_path[PATH_MAX];
966 char resolved_path[PATH_MAX];
967 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
968
969#if defined (__APPLE__)
970 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
971 if (framework_pos)
972 {
973 framework_pos += strlen("LLDB.framework");
974 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
975 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +0000976#else
Daniel Malea21e32a62013-01-04 23:35:13 +0000977 llvm::Twine python_version_dir;
978 python_version_dir = "/python"
979 + llvm::Twine(PY_MAJOR_VERSION)
980 + "."
981 + llvm::Twine(PY_MINOR_VERSION)
982 + "/site-packages";
983
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +0000984 // We may get our string truncated. Should we protect
985 // this with an assert?
Daniel Malea21e32a62013-01-04 23:35:13 +0000986
987 ::strncat(raw_path, python_version_dir.str().c_str(),
988 sizeof(raw_path) - strlen(raw_path) - 1);
989
Greg Clayton24b48ff2010-10-17 22:03:32 +0000990#endif
991 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
992 g_lldb_python_dir.SetCString(resolved_path);
993 }
994 }
995 file_spec.GetDirectory() = g_lldb_python_dir;
996 return file_spec.GetDirectory();
997 }
998 break;
999
Greg Clayton52fd9842011-02-02 02:24:04 +00001000 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1001 {
1002#if defined (__APPLE__)
1003 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001004 static bool g_lldb_system_plugin_dir_located = false;
1005 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001006 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001007 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001008 FileSpec lldb_file_spec;
1009 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1010 {
1011 char raw_path[PATH_MAX];
1012 char resolved_path[PATH_MAX];
1013 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1014
1015 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1016 if (framework_pos)
1017 {
1018 framework_pos += strlen("LLDB.framework");
1019 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001020 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1021 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001022 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001023 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001024 }
1025 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001026
1027 if (g_lldb_system_plugin_dir)
1028 {
1029 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1030 return true;
1031 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001032#endif
1033 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1034 return false;
1035 }
1036 break;
1037
1038 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1039 {
1040#if defined (__APPLE__)
1041 static ConstString g_lldb_user_plugin_dir;
1042 if (!g_lldb_user_plugin_dir)
1043 {
1044 char user_plugin_path[PATH_MAX];
1045 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1046 user_plugin_path,
1047 sizeof(user_plugin_path)))
1048 {
1049 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1050 }
1051 }
1052 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1053 return file_spec.GetDirectory();
1054#endif
1055 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1056 return false;
1057 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001058 }
1059
1060 return false;
1061}
1062
Greg Clayton58e26e02011-03-24 04:28:38 +00001063
1064bool
1065Host::GetHostname (std::string &s)
1066{
1067 char hostname[PATH_MAX];
1068 hostname[sizeof(hostname) - 1] = '\0';
1069 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1070 {
1071 struct hostent* h = ::gethostbyname (hostname);
1072 if (h)
1073 s.assign (h->h_name);
1074 else
1075 s.assign (hostname);
1076 return true;
1077 }
1078 return false;
1079}
1080
Greg Clayton24bc5d92011-03-30 18:16:51 +00001081const char *
1082Host::GetUserName (uint32_t uid, std::string &user_name)
1083{
1084 struct passwd user_info;
1085 struct passwd *user_info_ptr = &user_info;
1086 char user_buffer[PATH_MAX];
1087 size_t user_buffer_size = sizeof(user_buffer);
1088 if (::getpwuid_r (uid,
1089 &user_info,
1090 user_buffer,
1091 user_buffer_size,
1092 &user_info_ptr) == 0)
1093 {
1094 if (user_info_ptr)
1095 {
1096 user_name.assign (user_info_ptr->pw_name);
1097 return user_name.c_str();
1098 }
1099 }
1100 user_name.clear();
1101 return NULL;
1102}
1103
1104const char *
1105Host::GetGroupName (uint32_t gid, std::string &group_name)
1106{
1107 char group_buffer[PATH_MAX];
1108 size_t group_buffer_size = sizeof(group_buffer);
1109 struct group group_info;
1110 struct group *group_info_ptr = &group_info;
1111 // Try the threadsafe version first
1112 if (::getgrgid_r (gid,
1113 &group_info,
1114 group_buffer,
1115 group_buffer_size,
1116 &group_info_ptr) == 0)
1117 {
1118 if (group_info_ptr)
1119 {
1120 group_name.assign (group_info_ptr->gr_name);
1121 return group_name.c_str();
1122 }
1123 }
1124 else
1125 {
1126 // The threadsafe version isn't currently working
1127 // for me on darwin, but the non-threadsafe version
1128 // is, so I am calling it below.
1129 group_info_ptr = ::getgrgid (gid);
1130 if (group_info_ptr)
1131 {
1132 group_name.assign (group_info_ptr->gr_name);
1133 return group_name.c_str();
1134 }
1135 }
1136 group_name.clear();
1137 return NULL;
1138}
1139
Johnny Chen4b663292011-08-02 20:52:42 +00001140#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001141bool
1142Host::GetOSBuildString (std::string &s)
1143{
1144 s.clear();
1145 return false;
1146}
1147
1148bool
1149Host::GetOSKernelDescription (std::string &s)
1150{
1151 s.clear();
1152 return false;
1153}
Johnny Chen4b663292011-08-02 20:52:42 +00001154#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001155
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001156uint32_t
1157Host::GetUserID ()
1158{
1159 return getuid();
1160}
1161
1162uint32_t
1163Host::GetGroupID ()
1164{
1165 return getgid();
1166}
1167
1168uint32_t
1169Host::GetEffectiveUserID ()
1170{
1171 return geteuid();
1172}
1173
1174uint32_t
1175Host::GetEffectiveGroupID ()
1176{
1177 return getegid();
1178}
1179
1180#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001181uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001182Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001183{
1184 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001185 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001186}
Johnny Chen4b663292011-08-02 20:52:42 +00001187#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001188
Johnny Chen4b663292011-08-02 20:52:42 +00001189#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001190bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001191Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001192{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001193 process_info.Clear();
1194 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001195}
Johnny Chen4b663292011-08-02 20:52:42 +00001196#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001197
Sean Callananf35a96c2011-10-27 21:22:25 +00001198lldb::TargetSP
1199Host::GetDummyTarget (lldb_private::Debugger &debugger)
1200{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001201 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001202
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001203 // FIXME: Maybe the dummy target should be per-Debugger
1204 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1205 {
1206 ArchSpec arch(Target::GetDefaultArchitecture());
1207 if (!arch.IsValid())
1208 arch = Host::GetArchitecture ();
1209 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001210 NULL,
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001211 arch.GetTriple().getTriple().c_str(),
1212 false,
1213 NULL,
1214 g_dummy_target_sp);
1215 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001216
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001217 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001218}
1219
Greg Clayton97471182012-04-14 01:42:46 +00001220struct ShellInfo
1221{
1222 ShellInfo () :
1223 process_reaped (false),
1224 can_delete (false),
1225 pid (LLDB_INVALID_PROCESS_ID),
1226 signo(-1),
1227 status(-1)
1228 {
1229 }
1230
1231 lldb_private::Predicate<bool> process_reaped;
1232 lldb_private::Predicate<bool> can_delete;
1233 lldb::pid_t pid;
1234 int signo;
1235 int status;
1236};
1237
1238static bool
1239MonitorShellCommand (void *callback_baton,
1240 lldb::pid_t pid,
1241 bool exited, // True if the process did exit
1242 int signo, // Zero for no signal
1243 int status) // Exit value of process if signal is zero
1244{
1245 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1246 shell_info->pid = pid;
1247 shell_info->signo = signo;
1248 shell_info->status = status;
1249 // Let the thread running Host::RunShellCommand() know that the process
1250 // exited and that ShellInfo has been filled in by broadcasting to it
1251 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1252 // Now wait for a handshake back from that thread running Host::RunShellCommand
1253 // so we know that we can delete shell_info_ptr
1254 shell_info->can_delete.WaitForValueEqualTo(true);
1255 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1256 usleep(1000);
1257 // Now delete the shell info that was passed into this function
1258 delete shell_info;
1259 return true;
1260}
1261
1262Error
1263Host::RunShellCommand (const char *command,
1264 const char *working_dir,
1265 int *status_ptr,
1266 int *signo_ptr,
1267 std::string *command_output_ptr,
Greg Claytonb924eb62012-09-27 03:13:55 +00001268 uint32_t timeout_sec,
1269 const char *shell)
Greg Clayton97471182012-04-14 01:42:46 +00001270{
1271 Error error;
1272 ProcessLaunchInfo launch_info;
Greg Claytonb924eb62012-09-27 03:13:55 +00001273 if (shell && shell[0])
1274 {
1275 // Run the command in a shell
1276 launch_info.SetShell(shell);
1277 launch_info.GetArguments().AppendArgument(command);
1278 const bool localhost = true;
1279 const bool will_debug = false;
1280 const bool first_arg_is_full_shell_command = true;
1281 launch_info.ConvertArgumentsForLaunchingInShell (error,
1282 localhost,
1283 will_debug,
1284 first_arg_is_full_shell_command);
1285 }
1286 else
1287 {
1288 // No shell, just run it
1289 Args args (command);
1290 const bool first_arg_is_executable = true;
Greg Clayton0c8446c2012-10-17 22:57:12 +00001291 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonb924eb62012-09-27 03:13:55 +00001292 }
Greg Clayton97471182012-04-14 01:42:46 +00001293
1294 if (working_dir)
1295 launch_info.SetWorkingDirectory(working_dir);
1296 char output_file_path_buffer[L_tmpnam];
1297 const char *output_file_path = NULL;
1298 if (command_output_ptr)
1299 {
1300 // Create a temporary file to get the stdout/stderr and redirect the
1301 // output of the command into this file. We will later read this file
1302 // if all goes well and fill the data into "command_output_ptr"
1303 output_file_path = ::tmpnam(output_file_path_buffer);
1304 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1305 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonb924eb62012-09-27 03:13:55 +00001306 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Clayton97471182012-04-14 01:42:46 +00001307 }
1308 else
1309 {
1310 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1311 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1312 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1313 }
1314
1315 // The process monitor callback will delete the 'shell_info_ptr' below...
1316 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1317
1318 const bool monitor_signals = false;
1319 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1320
1321 error = LaunchProcess (launch_info);
1322 const lldb::pid_t pid = launch_info.GetProcessID();
1323 if (pid != LLDB_INVALID_PROCESS_ID)
1324 {
1325 // The process successfully launched, so we can defer ownership of
1326 // "shell_info" to the MonitorShellCommand callback function that will
1327 // get called when the process dies. We release the std::auto_ptr as it
1328 // doesn't need to delete the ShellInfo anymore.
1329 ShellInfo *shell_info = shell_info_ap.release();
1330 TimeValue timeout_time(TimeValue::Now());
1331 timeout_time.OffsetWithSeconds(timeout_sec);
1332 bool timed_out = false;
1333 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1334 if (timed_out)
1335 {
1336 error.SetErrorString("timed out waiting for shell command to complete");
1337
1338 // Kill the process since it didn't complete withint the timeout specified
1339 ::kill (pid, SIGKILL);
1340 // Wait for the monitor callback to get the message
1341 timeout_time = TimeValue::Now();
1342 timeout_time.OffsetWithSeconds(1);
1343 timed_out = false;
1344 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1345 }
1346 else
1347 {
1348 if (status_ptr)
1349 *status_ptr = shell_info->status;
1350
1351 if (signo_ptr)
1352 *signo_ptr = shell_info->signo;
1353
1354 if (command_output_ptr)
1355 {
1356 command_output_ptr->clear();
1357 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1358 uint64_t file_size = file_spec.GetByteSize();
1359 if (file_size > 0)
1360 {
1361 if (file_size > command_output_ptr->max_size())
1362 {
1363 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1364 }
1365 else
1366 {
1367 command_output_ptr->resize(file_size);
1368 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1369 }
1370 }
1371 }
1372 }
1373 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1374 }
1375 else
1376 {
1377 error.SetErrorString("failed to get process ID");
1378 }
1379
1380 if (output_file_path)
1381 ::unlink (output_file_path);
1382 // Handshake with the monitor thread, or just let it know in advance that
1383 // it can delete "shell_info" in case we timed out and were not able to kill
1384 // the process...
1385 return error;
1386}
1387
1388
Greg Clayton132c49a2013-02-17 20:46:30 +00001389uint32_t
1390Host::GetNumberCPUS ()
1391{
1392 static uint32_t g_num_cores = UINT32_MAX;
1393 if (g_num_cores == UINT32_MAX)
1394 {
1395#if defined(__APPLE__) or defined (__linux__)
1396
1397 g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN);
1398
1399#elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
1400
1401 // Header file for this might need to be included at the top of this file
1402 SYSTEM_INFO system_info;
1403 ::GetSystemInfo (&system_info);
1404 g_num_cores = system_info.dwNumberOfProcessors;
1405
1406#else
1407
1408 // Assume POSIX support if a host specific case has not been supplied above
1409 g_num_cores = 0;
1410 int num_cores = 0;
1411 size_t num_cores_len = sizeof(num_cores);
1412 int mib[] = { CTL_HW, HW_AVAILCPU };
1413
1414 /* get the number of CPUs from the system */
1415 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1416 {
1417 g_num_cores = num_cores;
1418 }
1419 else
1420 {
1421 mib[1] = HW_NCPU;
1422 num_cores_len = sizeof(num_cores);
1423 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1424 {
1425 if (num_cores > 0)
1426 g_num_cores = num_cores;
1427 }
1428 }
1429#endif
1430 }
1431 return g_num_cores;
1432}
1433
1434
Greg Clayton97471182012-04-14 01:42:46 +00001435
Johnny Chen4b663292011-08-02 20:52:42 +00001436#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001437bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001438Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001439{
1440 return false;
1441}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001442
Greg Claytone98ac252010-11-10 04:57:04 +00001443void
1444Host::SetCrashDescriptionWithFormat (const char *format, ...)
1445{
1446}
1447
1448void
1449Host::SetCrashDescription (const char *description)
1450{
1451}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001452
1453lldb::pid_t
1454LaunchApplication (const FileSpec &app_file_spec)
1455{
1456 return LLDB_INVALID_PROCESS_ID;
1457}
1458
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001459#endif