blob: 34a37b8fae12d5566c74565a606833050294a1ab [file] [log] [blame]
Greg Clayton2bddd342010-09-07 20:11:56 +00001//===-- Host.cpp ------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytone3e3fee2013-02-17 20:46:30 +000012// C includes
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 Clayton2bddd342010-09-07 20:11:56 +000041#include "lldb/Host/Host.h"
42#include "lldb/Core/ArchSpec.h"
43#include "lldb/Core/ConstString.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000044#include "lldb/Core/Debugger.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000045#include "lldb/Core/Error.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000046#include "lldb/Core/Log.h"
47#include "lldb/Core/StreamString.h"
Jim Inghamc075ecd2012-05-04 19:24:49 +000048#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton45319462011-02-08 00:35:34 +000049#include "lldb/Host/Config.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000050#include "lldb/Host/Endian.h"
Greg Claytone996fd32011-03-08 22:40:15 +000051#include "lldb/Host/FileSpec.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000052#include "lldb/Host/Mutex.h"
Greg Claytone996fd32011-03-08 22:40:15 +000053#include "lldb/Target/Process.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000054#include "lldb/Target/TargetList.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000055
Stephen Wilsonbd588712011-02-24 19:15:09 +000056#include "llvm/Support/Host.h"
Greg Claytone996fd32011-03-08 22:40:15 +000057#include "llvm/Support/MachO.h"
Daniel Malea53430eb2013-01-04 23:35:13 +000058#include "llvm/ADT/Twine.h"
Stephen Wilsonbd588712011-02-24 19:15:09 +000059
Greg Clayton32e0a752011-03-30 18:16:51 +000060
Greg Clayton2bddd342010-09-07 20:11:56 +000061
Greg Clayton45319462011-02-08 00:35:34 +000062
Greg Clayton2bddd342010-09-07 20:11:56 +000063
64using namespace lldb;
65using namespace lldb_private;
66
Greg Claytone4e45922011-11-16 05:37:56 +000067
Greg Clayton1c4cd072011-11-17 19:41:57 +000068#if !defined (__APPLE__)
Greg Clayton2bddd342010-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 Claytone4e45922011-11-16 05:37:56 +000090 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton2bddd342010-09-07 20:11:56 +000091
Greg Claytone4e45922011-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 Malead01b2952012-11-29 21:49:15 +000098 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
Greg Claytone4e45922011-11-16 05:37:56 +000099 thread = ThreadCreate (thread_name,
100 MonitorChildProcessThreadFunction,
101 info_ptr,
102 NULL);
103
Greg Clayton2bddd342010-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 Clayton5160ce52013-03-27 23:08:40 +0000138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton2bddd342010-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 Thirumurthi0f3b9b82013-05-01 20:38:19 +0000153#if defined (__FreeBSD__)
154 #define __WALL 0
155#endif
Matt Kopec650648f2013-01-08 16:30:18 +0000156 const int options = __WALL;
157
Greg Clayton2bddd342010-09-07 20:11:56 +0000158 while (1)
159 {
Caroline Tice20ad3c42010-10-29 21:48:37 +0000160 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000161 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000162 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
Greg Clayton2bddd342010-09-07 20:11:56 +0000163
164 // Wait for all child processes
165 ::pthread_testcancel ();
Matt Kopec650648f2013-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 Clayton2bddd342010-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 Kopec650648f2013-01-08 16:30:18 +0000177 else if (wait_pid > 0)
Greg Clayton2bddd342010-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 Kopec650648f2013-01-08 16:30:18 +0000192 if (wait_pid == pid)
193 exited = true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000194 }
195 else if (WIFSIGNALED(status))
196 {
197 signal = WTERMSIG(status);
198 status_cstr = "SIGNALED";
Matt Kopec650648f2013-01-08 16:30:18 +0000199 if (wait_pid == pid) {
200 exited = true;
201 exit_status = -1;
202 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000203 }
204 else
205 {
Johnny Chen44805302011-07-19 19:48:13 +0000206 status_cstr = "(\?\?\?)";
Greg Clayton2bddd342010-09-07 20:11:56 +0000207 }
208
209 // Scope for pthread_cancel_disabler
210 {
211 ScopedPThreadCancelDisabler pthread_cancel_disabler;
212
Caroline Tice20ad3c42010-10-29 21:48:37 +0000213 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000214 if (log)
Daniel Malead01b2952012-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 Clayton2bddd342010-09-07 20:11:56 +0000216 function,
217 wait_pid,
218 options,
Greg Clayton2bddd342010-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 Claytone4e45922011-11-16 05:37:56 +0000227 bool callback_return = false;
228 if (callback)
Matt Kopec650648f2013-01-08 16:30:18 +0000229 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton2bddd342010-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 Tice20ad3c42010-10-29 21:48:37 +0000243 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-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 Claytone38a5ed2012-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 Claytone4e45922011-11-16 05:37:56 +0000257#endif // #if !defined (__APPLE__)
258
Greg Claytone38a5ed2012-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 Clayton2bddd342010-09-07 20:11:56 +0000268size_t
269Host::GetPageSize()
270{
271 return ::getpagesize();
272}
273
Greg Clayton2bddd342010-09-07 20:11:56 +0000274const ArchSpec &
Greg Clayton514487e2011-02-15 21:59:32 +0000275Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton2bddd342010-09-07 20:11:56 +0000276{
Greg Clayton514487e2011-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 Clayton2bddd342010-09-07 20:11:56 +0000282#if defined (__APPLE__)
Greg Clayton514487e2011-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 Clayton2bddd342010-09-07 20:11:56 +0000293 uint32_t cputype, cpusubtype;
Greg Clayton514487e2011-02-15 21:59:32 +0000294 uint32_t is_64_bit_capable = false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000295 size_t len = sizeof(cputype);
Greg Clayton514487e2011-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 Clayton2bddd342010-09-07 20:11:56 +0000299 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
300 {
Greg Clayton514487e2011-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 Clayton2bddd342010-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 Clayton514487e2011-02-15 21:59:32 +0000309 g_supports_64 = true;
310 }
311
312 if (is_64_bit_capable)
313 {
Greg Clayton93d3c8332011-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 Clayton514487e2011-02-15 21:59:32 +0000318 if (cputype & CPU_ARCH_ABI64)
Greg Clayton2bddd342010-09-07 20:11:56 +0000319 {
Greg Clayton514487e2011-02-15 21:59:32 +0000320 // We have a 64 bit kernel on a 64 bit system
Greg Claytone0d378b2011-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 Clayton514487e2011-02-15 21:59:32 +0000323 }
324 else
325 {
326 // We have a 32 bit kernel on a 64 bit system
Greg Claytone0d378b2011-03-24 21:19:54 +0000327 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton2bddd342010-09-07 20:11:56 +0000328 cputype |= CPU_ARCH_ABI64;
Greg Claytone0d378b2011-03-24 21:19:54 +0000329 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton2bddd342010-09-07 20:11:56 +0000330 }
331 }
Greg Clayton514487e2011-02-15 21:59:32 +0000332 else
333 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000334 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton514487e2011-02-15 21:59:32 +0000335 g_host_arch_64.Clear();
336 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000337 }
Greg Clayton514487e2011-02-15 21:59:32 +0000338 }
339
340#else // #if defined (__APPLE__)
Stephen Wilsonbd588712011-02-24 19:15:09 +0000341
Greg Clayton514487e2011-02-15 21:59:32 +0000342 if (g_supports_32 == false && g_supports_64 == false)
343 {
Peter Collingbourne1f6198d2011-11-05 01:35:31 +0000344 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton514487e2011-02-15 21:59:32 +0000345
Stephen Wilsonbd588712011-02-24 19:15:09 +0000346 g_host_arch_32.Clear();
347 g_host_arch_64.Clear();
Greg Clayton514487e2011-02-15 21:59:32 +0000348
Greg Claytonb29e6c62012-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 Wilsonbd588712011-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 Clayton514487e2011-02-15 21:59:32 +0000360
Stephen Wilsonbd588712011-02-24 19:15:09 +0000361 case llvm::Triple::x86_64:
Greg Clayton542e4072012-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 Wilsonbd588712011-02-24 19:15:09 +0000368 case llvm::Triple::sparcv9:
369 case llvm::Triple::ppc64:
Stephen Wilsonbd588712011-02-24 19:15:09 +0000370 g_host_arch_64.SetTriple(triple);
371 g_supports_64 = true;
372 break;
373 }
Greg Clayton4796c4f2011-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 Clayton2bddd342010-09-07 20:11:56 +0000377 }
Greg Clayton514487e2011-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 Clayton2bddd342010-09-07 20:11:56 +0000390}
391
392const ConstString &
393Host::GetVendorString()
394{
395 static ConstString g_vendor;
396 if (!g_vendor)
397 {
Greg Clayton950971f2012-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 Clayton2bddd342010-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 Clayton950971f2012-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 Clayton2bddd342010-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 Clayton950971f2012-05-12 00:01:21 +0000424 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
425 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton2bddd342010-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 Clayton813ddfc2012-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 Chen8f3d8382011-08-02 20:52:42 +0000446#elif defined(__FreeBSD__)
447 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton2bddd342010-09-07 20:11:56 +0000448#else
449 return lldb::tid_t(pthread_self());
450#endif
451}
452
Jim Ingham372787f2012-04-07 00:00:41 +0000453lldb::thread_t
454Host::GetCurrentThread ()
455{
456 return lldb::thread_t(pthread_self());
457}
458
Greg Clayton2bddd342010-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 Clayton0ddf6be2011-11-04 03:42:38 +0000470#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton2bddd342010-09-07 20:11:56 +0000471 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer44030f12011-11-04 16:06:40 +0000472#endif
473#if !defined(_POSIX_C_SOURCE)
Greg Clayton2bddd342010-09-07 20:11:56 +0000474 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer44030f12011-11-04 16:06:40 +0000475#endif
Greg Clayton2bddd342010-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
Matt Kopec62502c62013-05-13 19:33:58 +0000515#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__linux__) // see macosx/Host.mm
516
Greg Clayton2bddd342010-09-07 20:11:56 +0000517void
518Host::ThreadCreated (const char *thread_name)
519{
520}
Greg Claytone5219662010-12-03 06:02:24 +0000521
Peter Collingbourne2ced9132011-08-05 00:35:43 +0000522void
Greg Claytone5219662010-12-03 06:02:24 +0000523Host::Backtrace (Stream &strm, uint32_t max_frames)
524{
Greg Clayton4272cc72011-02-02 02:24:04 +0000525 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytone5219662010-12-03 06:02:24 +0000526}
527
Greg Clayton85851dd2010-12-04 00:10:17 +0000528size_t
529Host::GetEnvironment (StringList &env)
530{
Greg Clayton4272cc72011-02-02 02:24:04 +0000531 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton85851dd2010-12-04 00:10:17 +0000532 return 0;
533}
534
Matt Kopec62502c62013-05-13 19:33:58 +0000535#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__linux__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000536
537struct HostThreadCreateInfo
538{
539 std::string thread_name;
540 thread_func_t thread_fptr;
541 thread_arg_t thread_arg;
542
543 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
544 thread_name (name ? name : ""),
545 thread_fptr (fptr),
546 thread_arg (arg)
547 {
548 }
549};
550
551static thread_result_t
552ThreadCreateTrampoline (thread_arg_t arg)
553{
554 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
555 Host::ThreadCreated (info->thread_name.c_str());
556 thread_func_t thread_fptr = info->thread_fptr;
557 thread_arg_t thread_arg = info->thread_arg;
558
Greg Clayton5160ce52013-03-27 23:08:40 +0000559 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton2bddd342010-09-07 20:11:56 +0000560 if (log)
561 log->Printf("thread created");
562
563 delete info;
564 return thread_fptr (thread_arg);
565}
566
567lldb::thread_t
568Host::ThreadCreate
569(
570 const char *thread_name,
571 thread_func_t thread_fptr,
572 thread_arg_t thread_arg,
573 Error *error
574)
575{
576 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
577
578 // Host::ThreadCreateTrampoline will delete this pointer for us.
579 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
580
581 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
582 if (err == 0)
583 {
584 if (error)
585 error->Clear();
586 return thread;
587 }
588
589 if (error)
590 error->SetError (err, eErrorTypePOSIX);
591
592 return LLDB_INVALID_HOST_THREAD;
593}
594
595bool
596Host::ThreadCancel (lldb::thread_t thread, Error *error)
597{
598 int err = ::pthread_cancel (thread);
599 if (error)
600 error->SetError(err, eErrorTypePOSIX);
601 return err == 0;
602}
603
604bool
605Host::ThreadDetach (lldb::thread_t thread, Error *error)
606{
607 int err = ::pthread_detach (thread);
608 if (error)
609 error->SetError(err, eErrorTypePOSIX);
610 return err == 0;
611}
612
613bool
614Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
615{
616 int err = ::pthread_join (thread, thread_result_ptr);
617 if (error)
618 error->SetError(err, eErrorTypePOSIX);
619 return err == 0;
620}
621
Jim Inghamc075ecd2012-05-04 19:24:49 +0000622
Greg Clayton85719632013-02-27 22:51:58 +0000623std::string
Greg Clayton2bddd342010-09-07 20:11:56 +0000624Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
625{
Greg Clayton85719632013-02-27 22:51:58 +0000626 std::string thread_name;
Greg Clayton2bddd342010-09-07 20:11:56 +0000627#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton85719632013-02-27 22:51:58 +0000628 // We currently can only get the name of a thread in the current process.
629 if (pid == Host::GetCurrentProcessID())
630 {
631 char pthread_name[1024];
632 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
Greg Clayton2bddd342010-09-07 20:11:56 +0000633 {
Greg Clayton85719632013-02-27 22:51:58 +0000634 if (pthread_name[0])
Greg Clayton2bddd342010-09-07 20:11:56 +0000635 {
Greg Clayton85719632013-02-27 22:51:58 +0000636 thread_name = pthread_name;
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000637 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000638 }
Greg Clayton85719632013-02-27 22:51:58 +0000639 else
640 {
641 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
642 if (current_queue != NULL)
643 {
644 const char *queue_name = dispatch_queue_get_label (current_queue);
645 if (queue_name && queue_name[0])
646 {
647 thread_name = queue_name;
648 }
649 }
650 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000651 }
Greg Clayton85719632013-02-27 22:51:58 +0000652#endif
653 return thread_name;
Greg Clayton2bddd342010-09-07 20:11:56 +0000654}
655
Matt Kopec62502c62013-05-13 19:33:58 +0000656bool
Greg Clayton2bddd342010-09-07 20:11:56 +0000657Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
658{
Greg Clayton85719632013-02-27 22:51:58 +0000659#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton2bddd342010-09-07 20:11:56 +0000660 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
661 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
662 if (pid == LLDB_INVALID_PROCESS_ID)
663 pid = curr_pid;
664
665 if (tid == LLDB_INVALID_THREAD_ID)
666 tid = curr_tid;
667
Greg Clayton2bddd342010-09-07 20:11:56 +0000668 // Set the pthread name if possible
669 if (pid == curr_pid && tid == curr_tid)
670 {
Matt Kopec62502c62013-05-13 19:33:58 +0000671 if (::pthread_setname_np (name) == 0)
672 return true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000673 }
Matt Kopec62502c62013-05-13 19:33:58 +0000674 return false;
675#elif defined (__linux__)
676 void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
677 if (fn)
678 {
679 int (*pthread_setname_np_func)(pthread_t thread, const char *name);
680 *reinterpret_cast<void **> (&pthread_setname_np_func) = fn;
681
682 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
683 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
684
685 if (pid == LLDB_INVALID_PROCESS_ID)
686 pid = curr_pid;
687
688 if (tid == LLDB_INVALID_THREAD_ID)
689 tid = curr_tid;
690
691 if (pid == curr_pid)
692 {
693 if (pthread_setname_np_func (tid, name) == 0)
694 return true;
695 }
696 }
697 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000698#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000699}
700
701FileSpec
702Host::GetProgramFileSpec ()
703{
704 static FileSpec g_program_filespec;
705 if (!g_program_filespec)
706 {
707#if defined (__APPLE__)
708 char program_fullpath[PATH_MAX];
709 // If DST is NULL, then return the number of bytes needed.
710 uint32_t len = sizeof(program_fullpath);
711 int err = _NSGetExecutablePath (program_fullpath, &len);
712 if (err == 0)
Greg Claytonb3326392011-01-13 01:23:43 +0000713 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton2bddd342010-09-07 20:11:56 +0000714 else if (err == -1)
715 {
716 char *large_program_fullpath = (char *)::malloc (len + 1);
717
718 err = _NSGetExecutablePath (large_program_fullpath, &len);
719 if (err == 0)
Greg Claytonb3326392011-01-13 01:23:43 +0000720 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton2bddd342010-09-07 20:11:56 +0000721
722 ::free (large_program_fullpath);
723 }
724#elif defined (__linux__)
725 char exe_path[PATH_MAX];
Stephen Wilsone5b94a92011-01-12 04:21:21 +0000726 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
727 if (len > 0) {
728 exe_path[len] = 0;
Greg Claytonb3326392011-01-13 01:23:43 +0000729 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsone5b94a92011-01-12 04:21:21 +0000730 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000731#elif defined (__FreeBSD__)
732 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
733 size_t exe_path_size;
734 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
735 {
Greg Clayton87ff1ac2011-01-13 01:27:55 +0000736 char *exe_path = new char[exe_path_size];
737 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
738 g_program_filespec.SetFile(exe_path, false);
739 delete[] exe_path;
Greg Clayton2bddd342010-09-07 20:11:56 +0000740 }
741#endif
742 }
743 return g_program_filespec;
744}
745
746FileSpec
747Host::GetModuleFileSpecForHostAddress (const void *host_addr)
748{
749 FileSpec module_filespec;
750 Dl_info info;
751 if (::dladdr (host_addr, &info))
752 {
753 if (info.dli_fname)
Greg Clayton274060b2010-10-20 20:54:39 +0000754 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton2bddd342010-09-07 20:11:56 +0000755 }
756 return module_filespec;
757}
758
759#if !defined (__APPLE__) // see Host.mm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000760
761bool
762Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
763{
764 bundle.Clear();
765 return false;
766}
767
Greg Clayton2bddd342010-09-07 20:11:56 +0000768bool
Greg Claytondd36def2010-10-17 22:03:32 +0000769Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton2bddd342010-09-07 20:11:56 +0000770{
Greg Claytondd36def2010-10-17 22:03:32 +0000771 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000772}
773#endif
774
Greg Clayton45319462011-02-08 00:35:34 +0000775// Opaque info that tracks a dynamic library that was loaded
776struct DynamicLibraryInfo
Greg Clayton4272cc72011-02-02 02:24:04 +0000777{
Greg Clayton45319462011-02-08 00:35:34 +0000778 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
779 file_spec (fs),
780 open_options (o),
781 handle (h)
782 {
783 }
784
785 const FileSpec file_spec;
786 uint32_t open_options;
787 void * handle;
788};
789
790void *
791Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
792{
Greg Clayton4272cc72011-02-02 02:24:04 +0000793 char path[PATH_MAX];
794 if (file_spec.GetPath(path, sizeof(path)))
795 {
Greg Clayton45319462011-02-08 00:35:34 +0000796 int mode = 0;
797
798 if (options & eDynamicLibraryOpenOptionLazy)
799 mode |= RTLD_LAZY;
Greg Claytonf9399452011-02-08 05:24:57 +0000800 else
801 mode |= RTLD_NOW;
802
Greg Clayton45319462011-02-08 00:35:34 +0000803
804 if (options & eDynamicLibraryOpenOptionLocal)
805 mode |= RTLD_LOCAL;
806 else
807 mode |= RTLD_GLOBAL;
808
809#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
810 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
811 mode |= RTLD_FIRST;
Greg Clayton75852f52011-02-07 17:43:47 +0000812#endif
Greg Clayton45319462011-02-08 00:35:34 +0000813
814 void * opaque = ::dlopen (path, mode);
815
816 if (opaque)
Greg Clayton4272cc72011-02-02 02:24:04 +0000817 {
818 error.Clear();
Greg Clayton45319462011-02-08 00:35:34 +0000819 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton4272cc72011-02-02 02:24:04 +0000820 }
821 else
822 {
823 error.SetErrorString(::dlerror());
824 }
825 }
826 else
827 {
828 error.SetErrorString("failed to extract path");
829 }
Greg Clayton45319462011-02-08 00:35:34 +0000830 return NULL;
Greg Clayton4272cc72011-02-02 02:24:04 +0000831}
832
833Error
Greg Clayton45319462011-02-08 00:35:34 +0000834Host::DynamicLibraryClose (void *opaque)
Greg Clayton4272cc72011-02-02 02:24:04 +0000835{
836 Error error;
Greg Clayton45319462011-02-08 00:35:34 +0000837 if (opaque == NULL)
Greg Clayton4272cc72011-02-02 02:24:04 +0000838 {
839 error.SetErrorString ("invalid dynamic library handle");
840 }
Greg Clayton45319462011-02-08 00:35:34 +0000841 else
Greg Clayton4272cc72011-02-02 02:24:04 +0000842 {
Greg Clayton45319462011-02-08 00:35:34 +0000843 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
844 if (::dlclose (dylib_info->handle) != 0)
845 {
846 error.SetErrorString(::dlerror());
847 }
848
849 dylib_info->open_options = 0;
850 dylib_info->handle = 0;
851 delete dylib_info;
Greg Clayton4272cc72011-02-02 02:24:04 +0000852 }
853 return error;
854}
855
856void *
Greg Clayton45319462011-02-08 00:35:34 +0000857Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton4272cc72011-02-02 02:24:04 +0000858{
Greg Clayton45319462011-02-08 00:35:34 +0000859 if (opaque == NULL)
Greg Clayton4272cc72011-02-02 02:24:04 +0000860 {
861 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton4272cc72011-02-02 02:24:04 +0000862 }
Greg Clayton4272cc72011-02-02 02:24:04 +0000863 else
Greg Clayton45319462011-02-08 00:35:34 +0000864 {
865 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
866
867 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
868 if (symbol_addr)
869 {
870#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
871 // This host doesn't support limiting searches to this shared library
872 // so we need to verify that the match came from this shared library
873 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonf9399452011-02-08 05:24:57 +0000874 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton45319462011-02-08 00:35:34 +0000875 {
876 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
877 if (match_dylib_spec != dylib_info->file_spec)
878 {
879 char dylib_path[PATH_MAX];
880 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
881 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
882 else
883 error.SetErrorString ("symbol not found");
884 return NULL;
885 }
886 }
887#endif
888 error.Clear();
889 return symbol_addr;
890 }
891 else
892 {
893 error.SetErrorString(::dlerror());
894 }
895 }
896 return NULL;
Greg Clayton4272cc72011-02-02 02:24:04 +0000897}
Greg Claytondd36def2010-10-17 22:03:32 +0000898
899bool
900Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
901{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000902 // To get paths related to LLDB we get the path to the executable that
Greg Claytondd36def2010-10-17 22:03:32 +0000903 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
904 // on linux this is assumed to be the "lldb" main executable. If LLDB on
905 // linux is actually in a shared library (lldb.so??) then this function will
906 // need to be modified to "do the right thing".
907
908 switch (path_type)
909 {
910 case ePathTypeLLDBShlibDir:
911 {
912 static ConstString g_lldb_so_dir;
913 if (!g_lldb_so_dir)
914 {
915 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
916 g_lldb_so_dir = lldb_file_spec.GetDirectory();
917 }
918 file_spec.GetDirectory() = g_lldb_so_dir;
919 return file_spec.GetDirectory();
920 }
921 break;
922
923 case ePathTypeSupportExecutableDir:
924 {
925 static ConstString g_lldb_support_exe_dir;
926 if (!g_lldb_support_exe_dir)
927 {
928 FileSpec lldb_file_spec;
929 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
930 {
931 char raw_path[PATH_MAX];
932 char resolved_path[PATH_MAX];
933 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
934
935#if defined (__APPLE__)
936 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
937 if (framework_pos)
938 {
939 framework_pos += strlen("LLDB.framework");
Greg Claytondce502e2011-11-04 03:34:56 +0000940#if !defined (__arm__)
Greg Claytondd36def2010-10-17 22:03:32 +0000941 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Claytondce502e2011-11-04 03:34:56 +0000942#endif
Greg Claytondd36def2010-10-17 22:03:32 +0000943 }
944#endif
945 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
946 g_lldb_support_exe_dir.SetCString(resolved_path);
947 }
948 }
949 file_spec.GetDirectory() = g_lldb_support_exe_dir;
950 return file_spec.GetDirectory();
951 }
952 break;
953
954 case ePathTypeHeaderDir:
955 {
956 static ConstString g_lldb_headers_dir;
957 if (!g_lldb_headers_dir)
958 {
959#if defined (__APPLE__)
960 FileSpec lldb_file_spec;
961 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
962 {
963 char raw_path[PATH_MAX];
964 char resolved_path[PATH_MAX];
965 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
966
967 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
968 if (framework_pos)
969 {
970 framework_pos += strlen("LLDB.framework");
971 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
972 }
973 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
974 g_lldb_headers_dir.SetCString(resolved_path);
975 }
976#else
Greg Clayton4272cc72011-02-02 02:24:04 +0000977 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Claytondd36def2010-10-17 22:03:32 +0000978 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
979#endif
980 }
981 file_spec.GetDirectory() = g_lldb_headers_dir;
982 return file_spec.GetDirectory();
983 }
984 break;
985
986 case ePathTypePythonDir:
987 {
Greg Claytondd36def2010-10-17 22:03:32 +0000988 static ConstString g_lldb_python_dir;
989 if (!g_lldb_python_dir)
990 {
991 FileSpec lldb_file_spec;
992 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
993 {
994 char raw_path[PATH_MAX];
995 char resolved_path[PATH_MAX];
996 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
997
998#if defined (__APPLE__)
999 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1000 if (framework_pos)
1001 {
1002 framework_pos += strlen("LLDB.framework");
1003 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1004 }
Filipe Cabecinhas0b751162012-07-30 16:46:32 +00001005#else
Daniel Malea53430eb2013-01-04 23:35:13 +00001006 llvm::Twine python_version_dir;
1007 python_version_dir = "/python"
1008 + llvm::Twine(PY_MAJOR_VERSION)
1009 + "."
1010 + llvm::Twine(PY_MINOR_VERSION)
1011 + "/site-packages";
1012
Filipe Cabecinhascffbd092012-07-30 18:56:10 +00001013 // We may get our string truncated. Should we protect
1014 // this with an assert?
Daniel Malea53430eb2013-01-04 23:35:13 +00001015
1016 ::strncat(raw_path, python_version_dir.str().c_str(),
1017 sizeof(raw_path) - strlen(raw_path) - 1);
1018
Greg Claytondd36def2010-10-17 22:03:32 +00001019#endif
1020 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1021 g_lldb_python_dir.SetCString(resolved_path);
1022 }
1023 }
1024 file_spec.GetDirectory() = g_lldb_python_dir;
1025 return file_spec.GetDirectory();
1026 }
1027 break;
1028
Greg Clayton4272cc72011-02-02 02:24:04 +00001029 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1030 {
1031#if defined (__APPLE__)
1032 static ConstString g_lldb_system_plugin_dir;
Greg Clayton1cb64962011-03-24 04:28:38 +00001033 static bool g_lldb_system_plugin_dir_located = false;
1034 if (!g_lldb_system_plugin_dir_located)
Greg Clayton4272cc72011-02-02 02:24:04 +00001035 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001036 g_lldb_system_plugin_dir_located = true;
Greg Clayton4272cc72011-02-02 02:24:04 +00001037 FileSpec lldb_file_spec;
1038 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1039 {
1040 char raw_path[PATH_MAX];
1041 char resolved_path[PATH_MAX];
1042 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1043
1044 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1045 if (framework_pos)
1046 {
1047 framework_pos += strlen("LLDB.framework");
1048 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton1cb64962011-03-24 04:28:38 +00001049 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1050 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton4272cc72011-02-02 02:24:04 +00001051 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001052 return false;
Greg Clayton4272cc72011-02-02 02:24:04 +00001053 }
1054 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001055
1056 if (g_lldb_system_plugin_dir)
1057 {
1058 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1059 return true;
1060 }
Greg Clayton4272cc72011-02-02 02:24:04 +00001061#endif
1062 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1063 return false;
1064 }
1065 break;
1066
1067 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1068 {
1069#if defined (__APPLE__)
1070 static ConstString g_lldb_user_plugin_dir;
1071 if (!g_lldb_user_plugin_dir)
1072 {
1073 char user_plugin_path[PATH_MAX];
1074 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1075 user_plugin_path,
1076 sizeof(user_plugin_path)))
1077 {
1078 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1079 }
1080 }
1081 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1082 return file_spec.GetDirectory();
1083#endif
1084 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1085 return false;
1086 }
Greg Claytondd36def2010-10-17 22:03:32 +00001087 }
1088
1089 return false;
1090}
1091
Greg Clayton1cb64962011-03-24 04:28:38 +00001092
1093bool
1094Host::GetHostname (std::string &s)
1095{
1096 char hostname[PATH_MAX];
1097 hostname[sizeof(hostname) - 1] = '\0';
1098 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1099 {
1100 struct hostent* h = ::gethostbyname (hostname);
1101 if (h)
1102 s.assign (h->h_name);
1103 else
1104 s.assign (hostname);
1105 return true;
1106 }
1107 return false;
1108}
1109
Greg Clayton32e0a752011-03-30 18:16:51 +00001110const char *
1111Host::GetUserName (uint32_t uid, std::string &user_name)
1112{
1113 struct passwd user_info;
1114 struct passwd *user_info_ptr = &user_info;
1115 char user_buffer[PATH_MAX];
1116 size_t user_buffer_size = sizeof(user_buffer);
1117 if (::getpwuid_r (uid,
1118 &user_info,
1119 user_buffer,
1120 user_buffer_size,
1121 &user_info_ptr) == 0)
1122 {
1123 if (user_info_ptr)
1124 {
1125 user_name.assign (user_info_ptr->pw_name);
1126 return user_name.c_str();
1127 }
1128 }
1129 user_name.clear();
1130 return NULL;
1131}
1132
1133const char *
1134Host::GetGroupName (uint32_t gid, std::string &group_name)
1135{
1136 char group_buffer[PATH_MAX];
1137 size_t group_buffer_size = sizeof(group_buffer);
1138 struct group group_info;
1139 struct group *group_info_ptr = &group_info;
1140 // Try the threadsafe version first
1141 if (::getgrgid_r (gid,
1142 &group_info,
1143 group_buffer,
1144 group_buffer_size,
1145 &group_info_ptr) == 0)
1146 {
1147 if (group_info_ptr)
1148 {
1149 group_name.assign (group_info_ptr->gr_name);
1150 return group_name.c_str();
1151 }
1152 }
1153 else
1154 {
1155 // The threadsafe version isn't currently working
1156 // for me on darwin, but the non-threadsafe version
1157 // is, so I am calling it below.
1158 group_info_ptr = ::getgrgid (gid);
1159 if (group_info_ptr)
1160 {
1161 group_name.assign (group_info_ptr->gr_name);
1162 return group_name.c_str();
1163 }
1164 }
1165 group_name.clear();
1166 return NULL;
1167}
1168
Johnny Chen8f3d8382011-08-02 20:52:42 +00001169#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton1cb64962011-03-24 04:28:38 +00001170bool
1171Host::GetOSBuildString (std::string &s)
1172{
1173 s.clear();
1174 return false;
1175}
1176
1177bool
1178Host::GetOSKernelDescription (std::string &s)
1179{
1180 s.clear();
1181 return false;
1182}
Johnny Chen8f3d8382011-08-02 20:52:42 +00001183#endif
Greg Clayton1cb64962011-03-24 04:28:38 +00001184
Han Ming Ong84647042012-02-25 01:07:38 +00001185uint32_t
1186Host::GetUserID ()
1187{
1188 return getuid();
1189}
1190
1191uint32_t
1192Host::GetGroupID ()
1193{
1194 return getgid();
1195}
1196
1197uint32_t
1198Host::GetEffectiveUserID ()
1199{
1200 return geteuid();
1201}
1202
1203uint32_t
1204Host::GetEffectiveGroupID ()
1205{
1206 return getegid();
1207}
1208
1209#if !defined (__APPLE__)
Greg Claytone996fd32011-03-08 22:40:15 +00001210uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001211Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone996fd32011-03-08 22:40:15 +00001212{
1213 process_infos.Clear();
Greg Claytone996fd32011-03-08 22:40:15 +00001214 return process_infos.GetSize();
Greg Clayton2bddd342010-09-07 20:11:56 +00001215}
Johnny Chen8f3d8382011-08-02 20:52:42 +00001216#endif
Greg Clayton2bddd342010-09-07 20:11:56 +00001217
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +00001218#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined(__linux__)
Greg Claytone996fd32011-03-08 22:40:15 +00001219bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001220Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton2bddd342010-09-07 20:11:56 +00001221{
Greg Claytone996fd32011-03-08 22:40:15 +00001222 process_info.Clear();
1223 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +00001224}
Johnny Chen8f3d8382011-08-02 20:52:42 +00001225#endif
Greg Clayton2bddd342010-09-07 20:11:56 +00001226
Sean Callananc0a6e062011-10-27 21:22:25 +00001227lldb::TargetSP
1228Host::GetDummyTarget (lldb_private::Debugger &debugger)
1229{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001230 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasb0183452012-05-17 15:48:02 +00001231
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001232 // FIXME: Maybe the dummy target should be per-Debugger
1233 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1234 {
1235 ArchSpec arch(Target::GetDefaultArchitecture());
1236 if (!arch.IsValid())
1237 arch = Host::GetArchitecture ();
1238 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +00001239 NULL,
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001240 arch.GetTriple().getTriple().c_str(),
1241 false,
1242 NULL,
1243 g_dummy_target_sp);
1244 }
Filipe Cabecinhasb0183452012-05-17 15:48:02 +00001245
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001246 return g_dummy_target_sp;
Sean Callananc0a6e062011-10-27 21:22:25 +00001247}
1248
Greg Claytond1cf11a2012-04-14 01:42:46 +00001249struct ShellInfo
1250{
1251 ShellInfo () :
1252 process_reaped (false),
1253 can_delete (false),
1254 pid (LLDB_INVALID_PROCESS_ID),
1255 signo(-1),
1256 status(-1)
1257 {
1258 }
1259
1260 lldb_private::Predicate<bool> process_reaped;
1261 lldb_private::Predicate<bool> can_delete;
1262 lldb::pid_t pid;
1263 int signo;
1264 int status;
1265};
1266
1267static bool
1268MonitorShellCommand (void *callback_baton,
1269 lldb::pid_t pid,
1270 bool exited, // True if the process did exit
1271 int signo, // Zero for no signal
1272 int status) // Exit value of process if signal is zero
1273{
1274 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1275 shell_info->pid = pid;
1276 shell_info->signo = signo;
1277 shell_info->status = status;
1278 // Let the thread running Host::RunShellCommand() know that the process
1279 // exited and that ShellInfo has been filled in by broadcasting to it
1280 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1281 // Now wait for a handshake back from that thread running Host::RunShellCommand
1282 // so we know that we can delete shell_info_ptr
1283 shell_info->can_delete.WaitForValueEqualTo(true);
1284 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1285 usleep(1000);
1286 // Now delete the shell info that was passed into this function
1287 delete shell_info;
1288 return true;
1289}
1290
1291Error
1292Host::RunShellCommand (const char *command,
1293 const char *working_dir,
1294 int *status_ptr,
1295 int *signo_ptr,
1296 std::string *command_output_ptr,
Greg Claytonc8f814d2012-09-27 03:13:55 +00001297 uint32_t timeout_sec,
1298 const char *shell)
Greg Claytond1cf11a2012-04-14 01:42:46 +00001299{
1300 Error error;
1301 ProcessLaunchInfo launch_info;
Greg Claytonc8f814d2012-09-27 03:13:55 +00001302 if (shell && shell[0])
1303 {
1304 // Run the command in a shell
1305 launch_info.SetShell(shell);
1306 launch_info.GetArguments().AppendArgument(command);
1307 const bool localhost = true;
1308 const bool will_debug = false;
1309 const bool first_arg_is_full_shell_command = true;
1310 launch_info.ConvertArgumentsForLaunchingInShell (error,
1311 localhost,
1312 will_debug,
1313 first_arg_is_full_shell_command);
1314 }
1315 else
1316 {
1317 // No shell, just run it
1318 Args args (command);
1319 const bool first_arg_is_executable = true;
Greg Clayton45392552012-10-17 22:57:12 +00001320 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonc8f814d2012-09-27 03:13:55 +00001321 }
Greg Claytond1cf11a2012-04-14 01:42:46 +00001322
1323 if (working_dir)
1324 launch_info.SetWorkingDirectory(working_dir);
1325 char output_file_path_buffer[L_tmpnam];
1326 const char *output_file_path = NULL;
1327 if (command_output_ptr)
1328 {
1329 // Create a temporary file to get the stdout/stderr and redirect the
1330 // output of the command into this file. We will later read this file
1331 // if all goes well and fill the data into "command_output_ptr"
1332 output_file_path = ::tmpnam(output_file_path_buffer);
1333 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1334 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonc8f814d2012-09-27 03:13:55 +00001335 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Claytond1cf11a2012-04-14 01:42:46 +00001336 }
1337 else
1338 {
1339 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1340 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1341 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1342 }
1343
1344 // The process monitor callback will delete the 'shell_info_ptr' below...
Greg Clayton7b0992d2013-04-18 22:45:39 +00001345 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
Greg Claytond1cf11a2012-04-14 01:42:46 +00001346
1347 const bool monitor_signals = false;
1348 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1349
1350 error = LaunchProcess (launch_info);
1351 const lldb::pid_t pid = launch_info.GetProcessID();
1352 if (pid != LLDB_INVALID_PROCESS_ID)
1353 {
1354 // The process successfully launched, so we can defer ownership of
1355 // "shell_info" to the MonitorShellCommand callback function that will
Greg Claytone01e07b2013-04-18 18:10:51 +00001356 // get called when the process dies. We release the unique pointer as it
Greg Claytond1cf11a2012-04-14 01:42:46 +00001357 // doesn't need to delete the ShellInfo anymore.
1358 ShellInfo *shell_info = shell_info_ap.release();
1359 TimeValue timeout_time(TimeValue::Now());
1360 timeout_time.OffsetWithSeconds(timeout_sec);
1361 bool timed_out = false;
1362 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1363 if (timed_out)
1364 {
1365 error.SetErrorString("timed out waiting for shell command to complete");
1366
1367 // Kill the process since it didn't complete withint the timeout specified
1368 ::kill (pid, SIGKILL);
1369 // Wait for the monitor callback to get the message
1370 timeout_time = TimeValue::Now();
1371 timeout_time.OffsetWithSeconds(1);
1372 timed_out = false;
1373 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1374 }
1375 else
1376 {
1377 if (status_ptr)
1378 *status_ptr = shell_info->status;
1379
1380 if (signo_ptr)
1381 *signo_ptr = shell_info->signo;
1382
1383 if (command_output_ptr)
1384 {
1385 command_output_ptr->clear();
1386 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1387 uint64_t file_size = file_spec.GetByteSize();
1388 if (file_size > 0)
1389 {
1390 if (file_size > command_output_ptr->max_size())
1391 {
1392 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1393 }
1394 else
1395 {
1396 command_output_ptr->resize(file_size);
1397 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1398 }
1399 }
1400 }
1401 }
1402 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1403 }
1404 else
1405 {
1406 error.SetErrorString("failed to get process ID");
1407 }
1408
1409 if (output_file_path)
1410 ::unlink (output_file_path);
1411 // Handshake with the monitor thread, or just let it know in advance that
1412 // it can delete "shell_info" in case we timed out and were not able to kill
1413 // the process...
1414 return error;
1415}
1416
1417
Greg Claytone3e3fee2013-02-17 20:46:30 +00001418uint32_t
1419Host::GetNumberCPUS ()
1420{
1421 static uint32_t g_num_cores = UINT32_MAX;
1422 if (g_num_cores == UINT32_MAX)
1423 {
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +00001424#if defined(__APPLE__) or defined (__linux__) or defined (__FreeBSD__)
Greg Claytone3e3fee2013-02-17 20:46:30 +00001425
1426 g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN);
1427
1428#elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
1429
1430 // Header file for this might need to be included at the top of this file
1431 SYSTEM_INFO system_info;
1432 ::GetSystemInfo (&system_info);
1433 g_num_cores = system_info.dwNumberOfProcessors;
1434
1435#else
1436
1437 // Assume POSIX support if a host specific case has not been supplied above
1438 g_num_cores = 0;
1439 int num_cores = 0;
1440 size_t num_cores_len = sizeof(num_cores);
1441 int mib[] = { CTL_HW, HW_AVAILCPU };
1442
1443 /* get the number of CPUs from the system */
1444 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1445 {
1446 g_num_cores = num_cores;
1447 }
1448 else
1449 {
1450 mib[1] = HW_NCPU;
1451 num_cores_len = sizeof(num_cores);
1452 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1453 {
1454 if (num_cores > 0)
1455 g_num_cores = num_cores;
1456 }
1457 }
1458#endif
1459 }
1460 return g_num_cores;
1461}
1462
1463
Greg Claytond1cf11a2012-04-14 01:42:46 +00001464
Johnny Chen8f3d8382011-08-02 20:52:42 +00001465#if !defined (__APPLE__)
Greg Clayton2bddd342010-09-07 20:11:56 +00001466bool
Greg Clayton3b147632010-12-18 01:54:34 +00001467Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton2bddd342010-09-07 20:11:56 +00001468{
1469 return false;
1470}
Greg Claytondd36def2010-10-17 22:03:32 +00001471
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001472void
1473Host::SetCrashDescriptionWithFormat (const char *format, ...)
1474{
1475}
1476
1477void
1478Host::SetCrashDescription (const char *description)
1479{
1480}
Greg Claytondd36def2010-10-17 22:03:32 +00001481
1482lldb::pid_t
1483LaunchApplication (const FileSpec &app_file_spec)
1484{
1485 return LLDB_INVALID_PROCESS_ID;
1486}
1487
Greg Clayton2bddd342010-09-07 20:11:56 +00001488#endif