blob: f5f8dd2284d543697f353df24de235725e98210e [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;
Ashok Thirumurthi32657de2013-05-01 20:38:19 +0000153#if defined (__FreeBSD__)
154 #define __WALL 0
155#endif
Matt Kopecf1fda372013-01-08 16:30:18 +0000156 const int options = __WALL;
157
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000158 while (1)
159 {
Caroline Tice926060e2010-10-29 21:48:37 +0000160 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000161 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000162 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000163
164 // Wait for all child processes
165 ::pthread_testcancel ();
Matt Kopecf1fda372013-01-08 16:30:18 +0000166 // Get signals from all children with same process group of pid
167 const lldb::pid_t wait_pid = ::waitpid (-1*pid, &status, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000168 ::pthread_testcancel ();
169
170 if (wait_pid == -1)
171 {
172 if (errno == EINTR)
173 continue;
174 else
175 break;
176 }
Matt Kopecf1fda372013-01-08 16:30:18 +0000177 else if (wait_pid > 0)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000178 {
179 bool exited = false;
180 int signal = 0;
181 int exit_status = 0;
182 const char *status_cstr = NULL;
183 if (WIFSTOPPED(status))
184 {
185 signal = WSTOPSIG(status);
186 status_cstr = "STOPPED";
187 }
188 else if (WIFEXITED(status))
189 {
190 exit_status = WEXITSTATUS(status);
191 status_cstr = "EXITED";
Matt Kopecf1fda372013-01-08 16:30:18 +0000192 if (wait_pid == pid)
193 exited = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000194 }
195 else if (WIFSIGNALED(status))
196 {
197 signal = WTERMSIG(status);
198 status_cstr = "SIGNALED";
Matt Kopecf1fda372013-01-08 16:30:18 +0000199 if (wait_pid == pid) {
200 exited = true;
201 exit_status = -1;
202 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000203 }
204 else
205 {
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000206 status_cstr = "(\?\?\?)";
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000207 }
208
209 // Scope for pthread_cancel_disabler
210 {
211 ScopedPThreadCancelDisabler pthread_cancel_disabler;
212
Caroline Tice926060e2010-10-29 21:48:37 +0000213 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000214 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000215 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 +0000216 function,
217 wait_pid,
218 options,
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000219 pid,
220 status,
221 status_cstr,
222 signal,
223 exit_status);
224
225 if (exited || (signal != 0 && monitor_signals))
226 {
Greg Clayton1c4642c2011-11-16 05:37:56 +0000227 bool callback_return = false;
228 if (callback)
Matt Kopecf1fda372013-01-08 16:30:18 +0000229 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000230
231 // If our process exited, then this thread should exit
232 if (exited)
233 break;
234 // If the callback returns true, it means this process should
235 // exit
236 if (callback_return)
237 break;
238 }
239 }
240 }
241 }
242
Caroline Tice926060e2010-10-29 21:48:37 +0000243 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000244 if (log)
245 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
246
247 return NULL;
248}
249
Greg Claytondf6dc882012-01-05 03:57:59 +0000250
251void
252Host::SystemLog (SystemLogType type, const char *format, va_list args)
253{
254 vfprintf (stderr, format, args);
255}
256
Greg Clayton1c4642c2011-11-16 05:37:56 +0000257#endif // #if !defined (__APPLE__)
258
Greg Claytondf6dc882012-01-05 03:57:59 +0000259void
260Host::SystemLog (SystemLogType type, const char *format, ...)
261{
262 va_list args;
263 va_start (args, format);
264 SystemLog (type, format, args);
265 va_end (args);
266}
267
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000268size_t
269Host::GetPageSize()
270{
271 return ::getpagesize();
272}
273
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000274const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000275Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000276{
Greg Clayton395fc332011-02-15 21:59:32 +0000277 static bool g_supports_32 = false;
278 static bool g_supports_64 = false;
279 static ArchSpec g_host_arch_32;
280 static ArchSpec g_host_arch_64;
281
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000282#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000283
284 // Apple is different in that it can support both 32 and 64 bit executables
285 // in the same operating system running concurrently. Here we detect the
286 // correct host architectures for both 32 and 64 bit including if 64 bit
287 // executables are supported on the system.
288
289 if (g_supports_32 == false && g_supports_64 == false)
290 {
291 // All apple systems support 32 bit execution.
292 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000293 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000294 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000295 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000296 ArchSpec host_arch;
297 // These will tell us about the kernel architecture, which even on a 64
298 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000299 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
300 {
Greg Clayton395fc332011-02-15 21:59:32 +0000301 len = sizeof (cpusubtype);
302 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
303 cpusubtype = CPU_TYPE_ANY;
304
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000305 len = sizeof (is_64_bit_capable);
306 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
307 {
308 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000309 g_supports_64 = true;
310 }
311
312 if (is_64_bit_capable)
313 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000314#if defined (__i386__) || defined (__x86_64__)
315 if (cpusubtype == CPU_SUBTYPE_486)
316 cpusubtype = CPU_SUBTYPE_I386_ALL;
317#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000318 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000319 {
Greg Clayton395fc332011-02-15 21:59:32 +0000320 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000321 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
322 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000323 }
324 else
325 {
326 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000327 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000328 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000329 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000330 }
331 }
Greg Clayton395fc332011-02-15 21:59:32 +0000332 else
333 {
Greg Claytonb3448432011-03-24 21:19:54 +0000334 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000335 g_host_arch_64.Clear();
336 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000337 }
Greg Clayton395fc332011-02-15 21:59:32 +0000338 }
339
340#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000341
Greg Clayton395fc332011-02-15 21:59:32 +0000342 if (g_supports_32 == false && g_supports_64 == false)
343 {
Peter Collingbourneb47c9982011-11-05 01:35:31 +0000344 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000345
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000346 g_host_arch_32.Clear();
347 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000348
Greg Clayton7971a032012-10-11 17:38:58 +0000349 // If the OS is Linux, "unknown" in the vendor slot isn't what we want
350 // for the default triple. It's probably an artifact of config.guess.
351 if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor)
352 triple.setVendorName("");
353
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000354 switch (triple.getArch())
355 {
356 default:
357 g_host_arch_32.SetTriple(triple);
358 g_supports_32 = true;
359 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000360
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000361 case llvm::Triple::x86_64:
Greg Clayton1a450cd2012-09-07 17:49:29 +0000362 g_host_arch_64.SetTriple(triple);
363 g_supports_64 = true;
364 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
365 g_supports_32 = true;
366 break;
367
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000368 case llvm::Triple::sparcv9:
369 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000370 g_host_arch_64.SetTriple(triple);
371 g_supports_64 = true;
372 break;
373 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000374
375 g_supports_32 = g_host_arch_32.IsValid();
376 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000377 }
Greg Clayton395fc332011-02-15 21:59:32 +0000378
379#endif // #else for #if defined (__APPLE__)
380
381 if (arch_kind == eSystemDefaultArchitecture32)
382 return g_host_arch_32;
383 else if (arch_kind == eSystemDefaultArchitecture64)
384 return g_host_arch_64;
385
386 if (g_supports_64)
387 return g_host_arch_64;
388
389 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000390}
391
392const ConstString &
393Host::GetVendorString()
394{
395 static ConstString g_vendor;
396 if (!g_vendor)
397 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000398 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
399 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
400 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000401 }
402 return g_vendor;
403}
404
405const ConstString &
406Host::GetOSString()
407{
408 static ConstString g_os_string;
409 if (!g_os_string)
410 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000411 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
412 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
413 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000414 }
415 return g_os_string;
416}
417
418const ConstString &
419Host::GetTargetTriple()
420{
421 static ConstString g_host_triple;
422 if (!(g_host_triple))
423 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000424 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
425 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000426 }
427 return g_host_triple;
428}
429
430lldb::pid_t
431Host::GetCurrentProcessID()
432{
433 return ::getpid();
434}
435
436lldb::tid_t
437Host::GetCurrentThreadID()
438{
439#if defined (__APPLE__)
Greg Claytone93725b2012-09-18 18:19:49 +0000440 // Calling "mach_port_deallocate()" bumps the reference count on the thread
441 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
442 // count.
443 thread_port_t thread_self = mach_thread_self();
444 mach_port_deallocate(mach_task_self(), thread_self);
445 return thread_self;
Johnny Chen4b663292011-08-02 20:52:42 +0000446#elif defined(__FreeBSD__)
447 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000448#else
449 return lldb::tid_t(pthread_self());
450#endif
451}
452
Jim Ingham1831e782012-04-07 00:00:41 +0000453lldb::thread_t
454Host::GetCurrentThread ()
455{
456 return lldb::thread_t(pthread_self());
457}
458
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000459const char *
460Host::GetSignalAsCString (int signo)
461{
462 switch (signo)
463 {
464 case SIGHUP: return "SIGHUP"; // 1 hangup
465 case SIGINT: return "SIGINT"; // 2 interrupt
466 case SIGQUIT: return "SIGQUIT"; // 3 quit
467 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
468 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
469 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000470#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000471 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000472#endif
473#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000474 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000475#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000476 case SIGFPE: return "SIGFPE"; // 8 floating point exception
477 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
478 case SIGBUS: return "SIGBUS"; // 10 bus error
479 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
480 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
481 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
482 case SIGALRM: return "SIGALRM"; // 14 alarm clock
483 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
484 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
485 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
486 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
487 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
488 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
489 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
490 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
491#if !defined(_POSIX_C_SOURCE)
492 case SIGIO: return "SIGIO"; // 23 input/output possible signal
493#endif
494 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
495 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
496 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
497 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
498#if !defined(_POSIX_C_SOURCE)
499 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
500 case SIGINFO: return "SIGINFO"; // 29 information request
501#endif
502 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
503 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
504 default:
505 break;
506 }
507 return NULL;
508}
509
510void
511Host::WillTerminate ()
512{
513}
514
Johnny Chen4b663292011-08-02 20:52:42 +0000515#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000516void
517Host::ThreadCreated (const char *thread_name)
518{
519}
Greg Claytonb749a262010-12-03 06:02:24 +0000520
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000521void
Greg Claytonb749a262010-12-03 06:02:24 +0000522Host::Backtrace (Stream &strm, uint32_t max_frames)
523{
Greg Clayton52fd9842011-02-02 02:24:04 +0000524 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000525}
526
Greg Clayton638351a2010-12-04 00:10:17 +0000527size_t
528Host::GetEnvironment (StringList &env)
529{
Greg Clayton52fd9842011-02-02 02:24:04 +0000530 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000531 return 0;
532}
533
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000534#endif
535
536struct HostThreadCreateInfo
537{
538 std::string thread_name;
539 thread_func_t thread_fptr;
540 thread_arg_t thread_arg;
541
542 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
543 thread_name (name ? name : ""),
544 thread_fptr (fptr),
545 thread_arg (arg)
546 {
547 }
548};
549
550static thread_result_t
551ThreadCreateTrampoline (thread_arg_t arg)
552{
553 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
554 Host::ThreadCreated (info->thread_name.c_str());
555 thread_func_t thread_fptr = info->thread_fptr;
556 thread_arg_t thread_arg = info->thread_arg;
557
Greg Clayton952e9dc2013-03-27 23:08:40 +0000558 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000559 if (log)
560 log->Printf("thread created");
561
562 delete info;
563 return thread_fptr (thread_arg);
564}
565
566lldb::thread_t
567Host::ThreadCreate
568(
569 const char *thread_name,
570 thread_func_t thread_fptr,
571 thread_arg_t thread_arg,
572 Error *error
573)
574{
575 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
576
577 // Host::ThreadCreateTrampoline will delete this pointer for us.
578 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
579
580 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
581 if (err == 0)
582 {
583 if (error)
584 error->Clear();
585 return thread;
586 }
587
588 if (error)
589 error->SetError (err, eErrorTypePOSIX);
590
591 return LLDB_INVALID_HOST_THREAD;
592}
593
594bool
595Host::ThreadCancel (lldb::thread_t thread, Error *error)
596{
597 int err = ::pthread_cancel (thread);
598 if (error)
599 error->SetError(err, eErrorTypePOSIX);
600 return err == 0;
601}
602
603bool
604Host::ThreadDetach (lldb::thread_t thread, Error *error)
605{
606 int err = ::pthread_detach (thread);
607 if (error)
608 error->SetError(err, eErrorTypePOSIX);
609 return err == 0;
610}
611
612bool
613Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
614{
615 int err = ::pthread_join (thread, thread_result_ptr);
616 if (error)
617 error->SetError(err, eErrorTypePOSIX);
618 return err == 0;
619}
620
Jim Ingham35dd4962012-05-04 19:24:49 +0000621
Greg Clayton6f0165b2013-02-27 22:51:58 +0000622std::string
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000623Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
624{
Greg Clayton6f0165b2013-02-27 22:51:58 +0000625 std::string thread_name;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000626#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton6f0165b2013-02-27 22:51:58 +0000627 // We currently can only get the name of a thread in the current process.
628 if (pid == Host::GetCurrentProcessID())
629 {
630 char pthread_name[1024];
631 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000632 {
Greg Clayton6f0165b2013-02-27 22:51:58 +0000633 if (pthread_name[0])
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000634 {
Greg Clayton6f0165b2013-02-27 22:51:58 +0000635 thread_name = pthread_name;
Greg Clayton49ce6822010-10-31 03:01:06 +0000636 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000637 }
Greg Clayton6f0165b2013-02-27 22:51:58 +0000638 else
639 {
640 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
641 if (current_queue != NULL)
642 {
643 const char *queue_name = dispatch_queue_get_label (current_queue);
644 if (queue_name && queue_name[0])
645 {
646 thread_name = queue_name;
647 }
648 }
649 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000650 }
Greg Clayton6f0165b2013-02-27 22:51:58 +0000651#endif
652 return thread_name;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000653}
654
655void
656Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
657{
Greg Clayton6f0165b2013-02-27 22:51:58 +0000658#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000659 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
660 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
661 if (pid == LLDB_INVALID_PROCESS_ID)
662 pid = curr_pid;
663
664 if (tid == LLDB_INVALID_THREAD_ID)
665 tid = curr_tid;
666
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000667 // Set the pthread name if possible
668 if (pid == curr_pid && tid == curr_tid)
669 {
670 ::pthread_setname_np (name);
671 }
672#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000673}
674
675FileSpec
676Host::GetProgramFileSpec ()
677{
678 static FileSpec g_program_filespec;
679 if (!g_program_filespec)
680 {
681#if defined (__APPLE__)
682 char program_fullpath[PATH_MAX];
683 // If DST is NULL, then return the number of bytes needed.
684 uint32_t len = sizeof(program_fullpath);
685 int err = _NSGetExecutablePath (program_fullpath, &len);
686 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000687 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000688 else if (err == -1)
689 {
690 char *large_program_fullpath = (char *)::malloc (len + 1);
691
692 err = _NSGetExecutablePath (large_program_fullpath, &len);
693 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000694 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000695
696 ::free (large_program_fullpath);
697 }
698#elif defined (__linux__)
699 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000700 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
701 if (len > 0) {
702 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000703 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000704 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000705#elif defined (__FreeBSD__)
706 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
707 size_t exe_path_size;
708 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
709 {
Greg Clayton366795e2011-01-13 01:27:55 +0000710 char *exe_path = new char[exe_path_size];
711 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
712 g_program_filespec.SetFile(exe_path, false);
713 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000714 }
715#endif
716 }
717 return g_program_filespec;
718}
719
720FileSpec
721Host::GetModuleFileSpecForHostAddress (const void *host_addr)
722{
723 FileSpec module_filespec;
724 Dl_info info;
725 if (::dladdr (host_addr, &info))
726 {
727 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000728 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000729 }
730 return module_filespec;
731}
732
733#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000734
735bool
736Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
737{
738 bundle.Clear();
739 return false;
740}
741
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000742bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000743Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000744{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000745 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000746}
747#endif
748
Greg Clayton14ef59f2011-02-08 00:35:34 +0000749// Opaque info that tracks a dynamic library that was loaded
750struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000751{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000752 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
753 file_spec (fs),
754 open_options (o),
755 handle (h)
756 {
757 }
758
759 const FileSpec file_spec;
760 uint32_t open_options;
761 void * handle;
762};
763
764void *
765Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
766{
Greg Clayton52fd9842011-02-02 02:24:04 +0000767 char path[PATH_MAX];
768 if (file_spec.GetPath(path, sizeof(path)))
769 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000770 int mode = 0;
771
772 if (options & eDynamicLibraryOpenOptionLazy)
773 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000774 else
775 mode |= RTLD_NOW;
776
Greg Clayton14ef59f2011-02-08 00:35:34 +0000777
778 if (options & eDynamicLibraryOpenOptionLocal)
779 mode |= RTLD_LOCAL;
780 else
781 mode |= RTLD_GLOBAL;
782
783#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
784 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
785 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000786#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000787
788 void * opaque = ::dlopen (path, mode);
789
790 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000791 {
792 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000793 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000794 }
795 else
796 {
797 error.SetErrorString(::dlerror());
798 }
799 }
800 else
801 {
802 error.SetErrorString("failed to extract path");
803 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000804 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000805}
806
807Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000808Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000809{
810 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000811 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000812 {
813 error.SetErrorString ("invalid dynamic library handle");
814 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000815 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000816 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000817 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
818 if (::dlclose (dylib_info->handle) != 0)
819 {
820 error.SetErrorString(::dlerror());
821 }
822
823 dylib_info->open_options = 0;
824 dylib_info->handle = 0;
825 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000826 }
827 return error;
828}
829
830void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000831Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000832{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000833 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000834 {
835 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000836 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000837 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000838 {
839 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
840
841 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
842 if (symbol_addr)
843 {
844#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
845 // This host doesn't support limiting searches to this shared library
846 // so we need to verify that the match came from this shared library
847 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000848 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000849 {
850 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
851 if (match_dylib_spec != dylib_info->file_spec)
852 {
853 char dylib_path[PATH_MAX];
854 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
855 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
856 else
857 error.SetErrorString ("symbol not found");
858 return NULL;
859 }
860 }
861#endif
862 error.Clear();
863 return symbol_addr;
864 }
865 else
866 {
867 error.SetErrorString(::dlerror());
868 }
869 }
870 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000871}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000872
873bool
874Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
875{
Greg Clayton5d187e52011-01-08 20:28:42 +0000876 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000877 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
878 // on linux this is assumed to be the "lldb" main executable. If LLDB on
879 // linux is actually in a shared library (lldb.so??) then this function will
880 // need to be modified to "do the right thing".
881
882 switch (path_type)
883 {
884 case ePathTypeLLDBShlibDir:
885 {
886 static ConstString g_lldb_so_dir;
887 if (!g_lldb_so_dir)
888 {
889 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
890 g_lldb_so_dir = lldb_file_spec.GetDirectory();
891 }
892 file_spec.GetDirectory() = g_lldb_so_dir;
893 return file_spec.GetDirectory();
894 }
895 break;
896
897 case ePathTypeSupportExecutableDir:
898 {
899 static ConstString g_lldb_support_exe_dir;
900 if (!g_lldb_support_exe_dir)
901 {
902 FileSpec lldb_file_spec;
903 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
904 {
905 char raw_path[PATH_MAX];
906 char resolved_path[PATH_MAX];
907 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
908
909#if defined (__APPLE__)
910 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
911 if (framework_pos)
912 {
913 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000914#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000915 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000916#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000917 }
918#endif
919 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
920 g_lldb_support_exe_dir.SetCString(resolved_path);
921 }
922 }
923 file_spec.GetDirectory() = g_lldb_support_exe_dir;
924 return file_spec.GetDirectory();
925 }
926 break;
927
928 case ePathTypeHeaderDir:
929 {
930 static ConstString g_lldb_headers_dir;
931 if (!g_lldb_headers_dir)
932 {
933#if defined (__APPLE__)
934 FileSpec lldb_file_spec;
935 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
936 {
937 char raw_path[PATH_MAX];
938 char resolved_path[PATH_MAX];
939 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
940
941 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
942 if (framework_pos)
943 {
944 framework_pos += strlen("LLDB.framework");
945 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
946 }
947 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
948 g_lldb_headers_dir.SetCString(resolved_path);
949 }
950#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000951 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000952 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
953#endif
954 }
955 file_spec.GetDirectory() = g_lldb_headers_dir;
956 return file_spec.GetDirectory();
957 }
958 break;
959
960 case ePathTypePythonDir:
961 {
Greg Clayton24b48ff2010-10-17 22:03:32 +0000962 static ConstString g_lldb_python_dir;
963 if (!g_lldb_python_dir)
964 {
965 FileSpec lldb_file_spec;
966 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
967 {
968 char raw_path[PATH_MAX];
969 char resolved_path[PATH_MAX];
970 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
971
972#if defined (__APPLE__)
973 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
974 if (framework_pos)
975 {
976 framework_pos += strlen("LLDB.framework");
977 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
978 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +0000979#else
Daniel Malea21e32a62013-01-04 23:35:13 +0000980 llvm::Twine python_version_dir;
981 python_version_dir = "/python"
982 + llvm::Twine(PY_MAJOR_VERSION)
983 + "."
984 + llvm::Twine(PY_MINOR_VERSION)
985 + "/site-packages";
986
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +0000987 // We may get our string truncated. Should we protect
988 // this with an assert?
Daniel Malea21e32a62013-01-04 23:35:13 +0000989
990 ::strncat(raw_path, python_version_dir.str().c_str(),
991 sizeof(raw_path) - strlen(raw_path) - 1);
992
Greg Clayton24b48ff2010-10-17 22:03:32 +0000993#endif
994 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
995 g_lldb_python_dir.SetCString(resolved_path);
996 }
997 }
998 file_spec.GetDirectory() = g_lldb_python_dir;
999 return file_spec.GetDirectory();
1000 }
1001 break;
1002
Greg Clayton52fd9842011-02-02 02:24:04 +00001003 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1004 {
1005#if defined (__APPLE__)
1006 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001007 static bool g_lldb_system_plugin_dir_located = false;
1008 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001009 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001010 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001011 FileSpec lldb_file_spec;
1012 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1013 {
1014 char raw_path[PATH_MAX];
1015 char resolved_path[PATH_MAX];
1016 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1017
1018 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1019 if (framework_pos)
1020 {
1021 framework_pos += strlen("LLDB.framework");
1022 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001023 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1024 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001025 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001026 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001027 }
1028 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001029
1030 if (g_lldb_system_plugin_dir)
1031 {
1032 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1033 return true;
1034 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001035#endif
1036 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1037 return false;
1038 }
1039 break;
1040
1041 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1042 {
1043#if defined (__APPLE__)
1044 static ConstString g_lldb_user_plugin_dir;
1045 if (!g_lldb_user_plugin_dir)
1046 {
1047 char user_plugin_path[PATH_MAX];
1048 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1049 user_plugin_path,
1050 sizeof(user_plugin_path)))
1051 {
1052 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1053 }
1054 }
1055 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1056 return file_spec.GetDirectory();
1057#endif
1058 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1059 return false;
1060 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001061 }
1062
1063 return false;
1064}
1065
Greg Clayton58e26e02011-03-24 04:28:38 +00001066
1067bool
1068Host::GetHostname (std::string &s)
1069{
1070 char hostname[PATH_MAX];
1071 hostname[sizeof(hostname) - 1] = '\0';
1072 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1073 {
1074 struct hostent* h = ::gethostbyname (hostname);
1075 if (h)
1076 s.assign (h->h_name);
1077 else
1078 s.assign (hostname);
1079 return true;
1080 }
1081 return false;
1082}
1083
Greg Clayton24bc5d92011-03-30 18:16:51 +00001084const char *
1085Host::GetUserName (uint32_t uid, std::string &user_name)
1086{
1087 struct passwd user_info;
1088 struct passwd *user_info_ptr = &user_info;
1089 char user_buffer[PATH_MAX];
1090 size_t user_buffer_size = sizeof(user_buffer);
1091 if (::getpwuid_r (uid,
1092 &user_info,
1093 user_buffer,
1094 user_buffer_size,
1095 &user_info_ptr) == 0)
1096 {
1097 if (user_info_ptr)
1098 {
1099 user_name.assign (user_info_ptr->pw_name);
1100 return user_name.c_str();
1101 }
1102 }
1103 user_name.clear();
1104 return NULL;
1105}
1106
1107const char *
1108Host::GetGroupName (uint32_t gid, std::string &group_name)
1109{
1110 char group_buffer[PATH_MAX];
1111 size_t group_buffer_size = sizeof(group_buffer);
1112 struct group group_info;
1113 struct group *group_info_ptr = &group_info;
1114 // Try the threadsafe version first
1115 if (::getgrgid_r (gid,
1116 &group_info,
1117 group_buffer,
1118 group_buffer_size,
1119 &group_info_ptr) == 0)
1120 {
1121 if (group_info_ptr)
1122 {
1123 group_name.assign (group_info_ptr->gr_name);
1124 return group_name.c_str();
1125 }
1126 }
1127 else
1128 {
1129 // The threadsafe version isn't currently working
1130 // for me on darwin, but the non-threadsafe version
1131 // is, so I am calling it below.
1132 group_info_ptr = ::getgrgid (gid);
1133 if (group_info_ptr)
1134 {
1135 group_name.assign (group_info_ptr->gr_name);
1136 return group_name.c_str();
1137 }
1138 }
1139 group_name.clear();
1140 return NULL;
1141}
1142
Johnny Chen4b663292011-08-02 20:52:42 +00001143#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001144bool
1145Host::GetOSBuildString (std::string &s)
1146{
1147 s.clear();
1148 return false;
1149}
1150
1151bool
1152Host::GetOSKernelDescription (std::string &s)
1153{
1154 s.clear();
1155 return false;
1156}
Johnny Chen4b663292011-08-02 20:52:42 +00001157#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001158
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001159uint32_t
1160Host::GetUserID ()
1161{
1162 return getuid();
1163}
1164
1165uint32_t
1166Host::GetGroupID ()
1167{
1168 return getgid();
1169}
1170
1171uint32_t
1172Host::GetEffectiveUserID ()
1173{
1174 return geteuid();
1175}
1176
1177uint32_t
1178Host::GetEffectiveGroupID ()
1179{
1180 return getegid();
1181}
1182
1183#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001184uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001185Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001186{
1187 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001188 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001189}
Johnny Chen4b663292011-08-02 20:52:42 +00001190#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001191
Johnny Chen4b663292011-08-02 20:52:42 +00001192#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001193bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001194Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001195{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001196 process_info.Clear();
1197 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001198}
Johnny Chen4b663292011-08-02 20:52:42 +00001199#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001200
Sean Callananf35a96c2011-10-27 21:22:25 +00001201lldb::TargetSP
1202Host::GetDummyTarget (lldb_private::Debugger &debugger)
1203{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001204 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001205
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001206 // FIXME: Maybe the dummy target should be per-Debugger
1207 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1208 {
1209 ArchSpec arch(Target::GetDefaultArchitecture());
1210 if (!arch.IsValid())
1211 arch = Host::GetArchitecture ();
1212 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001213 NULL,
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001214 arch.GetTriple().getTriple().c_str(),
1215 false,
1216 NULL,
1217 g_dummy_target_sp);
1218 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001219
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001220 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001221}
1222
Greg Clayton97471182012-04-14 01:42:46 +00001223struct ShellInfo
1224{
1225 ShellInfo () :
1226 process_reaped (false),
1227 can_delete (false),
1228 pid (LLDB_INVALID_PROCESS_ID),
1229 signo(-1),
1230 status(-1)
1231 {
1232 }
1233
1234 lldb_private::Predicate<bool> process_reaped;
1235 lldb_private::Predicate<bool> can_delete;
1236 lldb::pid_t pid;
1237 int signo;
1238 int status;
1239};
1240
1241static bool
1242MonitorShellCommand (void *callback_baton,
1243 lldb::pid_t pid,
1244 bool exited, // True if the process did exit
1245 int signo, // Zero for no signal
1246 int status) // Exit value of process if signal is zero
1247{
1248 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1249 shell_info->pid = pid;
1250 shell_info->signo = signo;
1251 shell_info->status = status;
1252 // Let the thread running Host::RunShellCommand() know that the process
1253 // exited and that ShellInfo has been filled in by broadcasting to it
1254 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1255 // Now wait for a handshake back from that thread running Host::RunShellCommand
1256 // so we know that we can delete shell_info_ptr
1257 shell_info->can_delete.WaitForValueEqualTo(true);
1258 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1259 usleep(1000);
1260 // Now delete the shell info that was passed into this function
1261 delete shell_info;
1262 return true;
1263}
1264
1265Error
1266Host::RunShellCommand (const char *command,
1267 const char *working_dir,
1268 int *status_ptr,
1269 int *signo_ptr,
1270 std::string *command_output_ptr,
Greg Claytonb924eb62012-09-27 03:13:55 +00001271 uint32_t timeout_sec,
1272 const char *shell)
Greg Clayton97471182012-04-14 01:42:46 +00001273{
1274 Error error;
1275 ProcessLaunchInfo launch_info;
Greg Claytonb924eb62012-09-27 03:13:55 +00001276 if (shell && shell[0])
1277 {
1278 // Run the command in a shell
1279 launch_info.SetShell(shell);
1280 launch_info.GetArguments().AppendArgument(command);
1281 const bool localhost = true;
1282 const bool will_debug = false;
1283 const bool first_arg_is_full_shell_command = true;
1284 launch_info.ConvertArgumentsForLaunchingInShell (error,
1285 localhost,
1286 will_debug,
1287 first_arg_is_full_shell_command);
1288 }
1289 else
1290 {
1291 // No shell, just run it
1292 Args args (command);
1293 const bool first_arg_is_executable = true;
Greg Clayton0c8446c2012-10-17 22:57:12 +00001294 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonb924eb62012-09-27 03:13:55 +00001295 }
Greg Clayton97471182012-04-14 01:42:46 +00001296
1297 if (working_dir)
1298 launch_info.SetWorkingDirectory(working_dir);
1299 char output_file_path_buffer[L_tmpnam];
1300 const char *output_file_path = NULL;
1301 if (command_output_ptr)
1302 {
1303 // Create a temporary file to get the stdout/stderr and redirect the
1304 // output of the command into this file. We will later read this file
1305 // if all goes well and fill the data into "command_output_ptr"
1306 output_file_path = ::tmpnam(output_file_path_buffer);
1307 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1308 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonb924eb62012-09-27 03:13:55 +00001309 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Clayton97471182012-04-14 01:42:46 +00001310 }
1311 else
1312 {
1313 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1314 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1315 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1316 }
1317
1318 // The process monitor callback will delete the 'shell_info_ptr' below...
Greg Clayton102b2c22013-04-18 22:45:39 +00001319 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
Greg Clayton97471182012-04-14 01:42:46 +00001320
1321 const bool monitor_signals = false;
1322 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1323
1324 error = LaunchProcess (launch_info);
1325 const lldb::pid_t pid = launch_info.GetProcessID();
1326 if (pid != LLDB_INVALID_PROCESS_ID)
1327 {
1328 // The process successfully launched, so we can defer ownership of
1329 // "shell_info" to the MonitorShellCommand callback function that will
Greg Clayton81a96aa2013-04-18 18:10:51 +00001330 // get called when the process dies. We release the unique pointer as it
Greg Clayton97471182012-04-14 01:42:46 +00001331 // doesn't need to delete the ShellInfo anymore.
1332 ShellInfo *shell_info = shell_info_ap.release();
1333 TimeValue timeout_time(TimeValue::Now());
1334 timeout_time.OffsetWithSeconds(timeout_sec);
1335 bool timed_out = false;
1336 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1337 if (timed_out)
1338 {
1339 error.SetErrorString("timed out waiting for shell command to complete");
1340
1341 // Kill the process since it didn't complete withint the timeout specified
1342 ::kill (pid, SIGKILL);
1343 // Wait for the monitor callback to get the message
1344 timeout_time = TimeValue::Now();
1345 timeout_time.OffsetWithSeconds(1);
1346 timed_out = false;
1347 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1348 }
1349 else
1350 {
1351 if (status_ptr)
1352 *status_ptr = shell_info->status;
1353
1354 if (signo_ptr)
1355 *signo_ptr = shell_info->signo;
1356
1357 if (command_output_ptr)
1358 {
1359 command_output_ptr->clear();
1360 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1361 uint64_t file_size = file_spec.GetByteSize();
1362 if (file_size > 0)
1363 {
1364 if (file_size > command_output_ptr->max_size())
1365 {
1366 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1367 }
1368 else
1369 {
1370 command_output_ptr->resize(file_size);
1371 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1372 }
1373 }
1374 }
1375 }
1376 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1377 }
1378 else
1379 {
1380 error.SetErrorString("failed to get process ID");
1381 }
1382
1383 if (output_file_path)
1384 ::unlink (output_file_path);
1385 // Handshake with the monitor thread, or just let it know in advance that
1386 // it can delete "shell_info" in case we timed out and were not able to kill
1387 // the process...
1388 return error;
1389}
1390
1391
Greg Clayton132c49a2013-02-17 20:46:30 +00001392uint32_t
1393Host::GetNumberCPUS ()
1394{
1395 static uint32_t g_num_cores = UINT32_MAX;
1396 if (g_num_cores == UINT32_MAX)
1397 {
Ashok Thirumurthi32657de2013-05-01 20:38:19 +00001398#if defined(__APPLE__) or defined (__linux__) or defined (__FreeBSD__)
Greg Clayton132c49a2013-02-17 20:46:30 +00001399
1400 g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN);
1401
1402#elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
1403
1404 // Header file for this might need to be included at the top of this file
1405 SYSTEM_INFO system_info;
1406 ::GetSystemInfo (&system_info);
1407 g_num_cores = system_info.dwNumberOfProcessors;
1408
1409#else
1410
1411 // Assume POSIX support if a host specific case has not been supplied above
1412 g_num_cores = 0;
1413 int num_cores = 0;
1414 size_t num_cores_len = sizeof(num_cores);
1415 int mib[] = { CTL_HW, HW_AVAILCPU };
1416
1417 /* get the number of CPUs from the system */
1418 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1419 {
1420 g_num_cores = num_cores;
1421 }
1422 else
1423 {
1424 mib[1] = HW_NCPU;
1425 num_cores_len = sizeof(num_cores);
1426 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1427 {
1428 if (num_cores > 0)
1429 g_num_cores = num_cores;
1430 }
1431 }
1432#endif
1433 }
1434 return g_num_cores;
1435}
1436
1437
Greg Clayton97471182012-04-14 01:42:46 +00001438
Johnny Chen4b663292011-08-02 20:52:42 +00001439#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001440bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001441Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001442{
1443 return false;
1444}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001445
Greg Claytone98ac252010-11-10 04:57:04 +00001446void
1447Host::SetCrashDescriptionWithFormat (const char *format, ...)
1448{
1449}
1450
1451void
1452Host::SetCrashDescription (const char *description)
1453{
1454}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001455
1456lldb::pid_t
1457LaunchApplication (const FileSpec &app_file_spec)
1458{
1459 return LLDB_INVALID_PROCESS_ID;
1460}
1461
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001462#endif