blob: 875b5796ffc7242183a875fc52e4965e4eb59faa [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
10#include "lldb/Host/Host.h"
11#include "lldb/Core/ArchSpec.h"
12#include "lldb/Core/ConstString.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000013#include "lldb/Core/Debugger.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000014#include "lldb/Core/Error.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000015#include "lldb/Core/Log.h"
16#include "lldb/Core/StreamString.h"
Jim Ingham35dd4962012-05-04 19:24:49 +000017#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton14ef59f2011-02-08 00:35:34 +000018#include "lldb/Host/Config.h"
Greg Claytoncd548032011-02-01 01:31:41 +000019#include "lldb/Host/Endian.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000020#include "lldb/Host/FileSpec.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000021#include "lldb/Host/Mutex.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000022#include "lldb/Target/Process.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000023#include "lldb/Target/TargetList.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000024
Stephen Wilson7f513ba2011-02-24 19:15:09 +000025#include "llvm/Support/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000026#include "llvm/Support/MachO.h"
Stephen Wilson7f513ba2011-02-24 19:15:09 +000027
Greg Clayton8f3b21d2010-09-07 20:11:56 +000028#include <dlfcn.h>
29#include <errno.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000030#include <grp.h>
Stephen Wilsonec2d9782011-04-08 13:36:44 +000031#include <limits.h>
Greg Clayton58e26e02011-03-24 04:28:38 +000032#include <netdb.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000033#include <pwd.h>
34#include <sys/types.h>
35
Greg Clayton8f3b21d2010-09-07 20:11:56 +000036
37#if defined (__APPLE__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000038
Greg Clayton49ce6822010-10-31 03:01:06 +000039#include <dispatch/dispatch.h>
Greg Clayton8f3b21d2010-09-07 20:11:56 +000040#include <libproc.h>
41#include <mach-o/dyld.h>
Greg Claytone93725b2012-09-18 18:19:49 +000042#include <mach/mach_port.h>
Greg Claytonb5f67fb2011-02-05 06:36:35 +000043#include <sys/sysctl.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000044
Greg Clayton24bc5d92011-03-30 18:16:51 +000045
Greg Clayton0f577c22011-02-07 17:43:47 +000046#elif defined (__linux__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000047
Greg Clayton0f577c22011-02-07 17:43:47 +000048#include <sys/wait.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000049
Johnny Chen4b663292011-08-02 20:52:42 +000050#elif defined (__FreeBSD__)
51
52#include <sys/wait.h>
53#include <sys/sysctl.h>
54#include <pthread_np.h>
55
Greg Clayton8f3b21d2010-09-07 20:11:56 +000056#endif
57
58using namespace lldb;
59using namespace lldb_private;
60
Greg Clayton1c4642c2011-11-16 05:37:56 +000061
Greg Claytonc518fe72011-11-17 19:41:57 +000062#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +000063struct MonitorInfo
64{
65 lldb::pid_t pid; // The process ID to monitor
66 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
67 void *callback_baton; // The callback baton for the callback function
68 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
69};
70
71static void *
72MonitorChildProcessThreadFunction (void *arg);
73
74lldb::thread_t
75Host::StartMonitoringChildProcess
76(
77 Host::MonitorChildProcessCallback callback,
78 void *callback_baton,
79 lldb::pid_t pid,
80 bool monitor_signals
81)
82{
83 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton1c4642c2011-11-16 05:37:56 +000084 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton8f3b21d2010-09-07 20:11:56 +000085
Greg Clayton1c4642c2011-11-16 05:37:56 +000086 info_ptr->pid = pid;
87 info_ptr->callback = callback;
88 info_ptr->callback_baton = callback_baton;
89 info_ptr->monitor_signals = monitor_signals;
90
91 char thread_name[256];
Daniel Malea5f35a4b2012-11-29 21:49:15 +000092 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
Greg Clayton1c4642c2011-11-16 05:37:56 +000093 thread = ThreadCreate (thread_name,
94 MonitorChildProcessThreadFunction,
95 info_ptr,
96 NULL);
97
Greg Clayton8f3b21d2010-09-07 20:11:56 +000098 return thread;
99}
100
101//------------------------------------------------------------------
102// Scoped class that will disable thread canceling when it is
103// constructed, and exception safely restore the previous value it
104// when it goes out of scope.
105//------------------------------------------------------------------
106class ScopedPThreadCancelDisabler
107{
108public:
109 ScopedPThreadCancelDisabler()
110 {
111 // Disable the ability for this thread to be cancelled
112 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
113 if (err != 0)
114 m_old_state = -1;
115
116 }
117
118 ~ScopedPThreadCancelDisabler()
119 {
120 // Restore the ability for this thread to be cancelled to what it
121 // previously was.
122 if (m_old_state != -1)
123 ::pthread_setcancelstate (m_old_state, 0);
124 }
125private:
126 int m_old_state; // Save the old cancelability state.
127};
128
129static void *
130MonitorChildProcessThreadFunction (void *arg)
131{
Greg Claytone005f2c2010-11-06 01:53:30 +0000132 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000133 const char *function = __FUNCTION__;
134 if (log)
135 log->Printf ("%s (arg = %p) thread starting...", function, arg);
136
137 MonitorInfo *info = (MonitorInfo *)arg;
138
139 const Host::MonitorChildProcessCallback callback = info->callback;
140 void * const callback_baton = info->callback_baton;
141 const lldb::pid_t pid = info->pid;
142 const bool monitor_signals = info->monitor_signals;
143
144 delete info;
145
146 int status = -1;
147 const int options = 0;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000148 while (1)
149 {
Caroline Tice926060e2010-10-29 21:48:37 +0000150 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000151 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000152 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000153
154 // Wait for all child processes
155 ::pthread_testcancel ();
Greg Clayton1c4642c2011-11-16 05:37:56 +0000156 const lldb::pid_t wait_pid = ::waitpid (pid, &status, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000157 ::pthread_testcancel ();
158
159 if (wait_pid == -1)
160 {
161 if (errno == EINTR)
162 continue;
163 else
164 break;
165 }
166 else if (wait_pid == pid)
167 {
168 bool exited = false;
169 int signal = 0;
170 int exit_status = 0;
171 const char *status_cstr = NULL;
172 if (WIFSTOPPED(status))
173 {
174 signal = WSTOPSIG(status);
175 status_cstr = "STOPPED";
176 }
177 else if (WIFEXITED(status))
178 {
179 exit_status = WEXITSTATUS(status);
180 status_cstr = "EXITED";
181 exited = true;
182 }
183 else if (WIFSIGNALED(status))
184 {
185 signal = WTERMSIG(status);
186 status_cstr = "SIGNALED";
187 exited = true;
188 exit_status = -1;
189 }
190 else
191 {
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000192 status_cstr = "(\?\?\?)";
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000193 }
194
195 // Scope for pthread_cancel_disabler
196 {
197 ScopedPThreadCancelDisabler pthread_cancel_disabler;
198
Caroline Tice926060e2010-10-29 21:48:37 +0000199 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000200 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000201 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 +0000202 function,
203 wait_pid,
204 options,
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000205 pid,
206 status,
207 status_cstr,
208 signal,
209 exit_status);
210
211 if (exited || (signal != 0 && monitor_signals))
212 {
Greg Clayton1c4642c2011-11-16 05:37:56 +0000213 bool callback_return = false;
214 if (callback)
215 callback_return = callback (callback_baton, pid, exited, signal, exit_status);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000216
217 // If our process exited, then this thread should exit
218 if (exited)
219 break;
220 // If the callback returns true, it means this process should
221 // exit
222 if (callback_return)
223 break;
224 }
225 }
226 }
227 }
228
Caroline Tice926060e2010-10-29 21:48:37 +0000229 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000230 if (log)
231 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
232
233 return NULL;
234}
235
Greg Claytondf6dc882012-01-05 03:57:59 +0000236
237void
238Host::SystemLog (SystemLogType type, const char *format, va_list args)
239{
240 vfprintf (stderr, format, args);
241}
242
Greg Clayton1c4642c2011-11-16 05:37:56 +0000243#endif // #if !defined (__APPLE__)
244
Greg Claytondf6dc882012-01-05 03:57:59 +0000245void
246Host::SystemLog (SystemLogType type, const char *format, ...)
247{
248 va_list args;
249 va_start (args, format);
250 SystemLog (type, format, args);
251 va_end (args);
252}
253
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000254size_t
255Host::GetPageSize()
256{
257 return ::getpagesize();
258}
259
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000260const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000261Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000262{
Greg Clayton395fc332011-02-15 21:59:32 +0000263 static bool g_supports_32 = false;
264 static bool g_supports_64 = false;
265 static ArchSpec g_host_arch_32;
266 static ArchSpec g_host_arch_64;
267
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000268#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000269
270 // Apple is different in that it can support both 32 and 64 bit executables
271 // in the same operating system running concurrently. Here we detect the
272 // correct host architectures for both 32 and 64 bit including if 64 bit
273 // executables are supported on the system.
274
275 if (g_supports_32 == false && g_supports_64 == false)
276 {
277 // All apple systems support 32 bit execution.
278 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000279 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000280 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000281 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000282 ArchSpec host_arch;
283 // These will tell us about the kernel architecture, which even on a 64
284 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000285 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
286 {
Greg Clayton395fc332011-02-15 21:59:32 +0000287 len = sizeof (cpusubtype);
288 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
289 cpusubtype = CPU_TYPE_ANY;
290
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000291 len = sizeof (is_64_bit_capable);
292 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
293 {
294 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000295 g_supports_64 = true;
296 }
297
298 if (is_64_bit_capable)
299 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000300#if defined (__i386__) || defined (__x86_64__)
301 if (cpusubtype == CPU_SUBTYPE_486)
302 cpusubtype = CPU_SUBTYPE_I386_ALL;
303#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000304 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000305 {
Greg Clayton395fc332011-02-15 21:59:32 +0000306 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000307 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
308 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000309 }
310 else
311 {
312 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000313 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000314 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000315 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000316 }
317 }
Greg Clayton395fc332011-02-15 21:59:32 +0000318 else
319 {
Greg Claytonb3448432011-03-24 21:19:54 +0000320 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000321 g_host_arch_64.Clear();
322 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000323 }
Greg Clayton395fc332011-02-15 21:59:32 +0000324 }
325
326#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000327
Greg Clayton395fc332011-02-15 21:59:32 +0000328 if (g_supports_32 == false && g_supports_64 == false)
329 {
Peter Collingbourneb47c9982011-11-05 01:35:31 +0000330 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000331
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000332 g_host_arch_32.Clear();
333 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000334
Greg Clayton7971a032012-10-11 17:38:58 +0000335 // If the OS is Linux, "unknown" in the vendor slot isn't what we want
336 // for the default triple. It's probably an artifact of config.guess.
337 if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor)
338 triple.setVendorName("");
339
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000340 switch (triple.getArch())
341 {
342 default:
343 g_host_arch_32.SetTriple(triple);
344 g_supports_32 = true;
345 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000346
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000347 case llvm::Triple::x86_64:
Greg Clayton1a450cd2012-09-07 17:49:29 +0000348 g_host_arch_64.SetTriple(triple);
349 g_supports_64 = true;
350 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
351 g_supports_32 = true;
352 break;
353
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000354 case llvm::Triple::sparcv9:
355 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000356 g_host_arch_64.SetTriple(triple);
357 g_supports_64 = true;
358 break;
359 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000360
361 g_supports_32 = g_host_arch_32.IsValid();
362 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000363 }
Greg Clayton395fc332011-02-15 21:59:32 +0000364
365#endif // #else for #if defined (__APPLE__)
366
367 if (arch_kind == eSystemDefaultArchitecture32)
368 return g_host_arch_32;
369 else if (arch_kind == eSystemDefaultArchitecture64)
370 return g_host_arch_64;
371
372 if (g_supports_64)
373 return g_host_arch_64;
374
375 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000376}
377
378const ConstString &
379Host::GetVendorString()
380{
381 static ConstString g_vendor;
382 if (!g_vendor)
383 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000384 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
385 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
386 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000387 }
388 return g_vendor;
389}
390
391const ConstString &
392Host::GetOSString()
393{
394 static ConstString g_os_string;
395 if (!g_os_string)
396 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000397 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
398 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
399 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000400 }
401 return g_os_string;
402}
403
404const ConstString &
405Host::GetTargetTriple()
406{
407 static ConstString g_host_triple;
408 if (!(g_host_triple))
409 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000410 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
411 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000412 }
413 return g_host_triple;
414}
415
416lldb::pid_t
417Host::GetCurrentProcessID()
418{
419 return ::getpid();
420}
421
422lldb::tid_t
423Host::GetCurrentThreadID()
424{
425#if defined (__APPLE__)
Greg Claytone93725b2012-09-18 18:19:49 +0000426 // Calling "mach_port_deallocate()" bumps the reference count on the thread
427 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
428 // count.
429 thread_port_t thread_self = mach_thread_self();
430 mach_port_deallocate(mach_task_self(), thread_self);
431 return thread_self;
Johnny Chen4b663292011-08-02 20:52:42 +0000432#elif defined(__FreeBSD__)
433 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000434#else
435 return lldb::tid_t(pthread_self());
436#endif
437}
438
Jim Ingham1831e782012-04-07 00:00:41 +0000439lldb::thread_t
440Host::GetCurrentThread ()
441{
442 return lldb::thread_t(pthread_self());
443}
444
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000445const char *
446Host::GetSignalAsCString (int signo)
447{
448 switch (signo)
449 {
450 case SIGHUP: return "SIGHUP"; // 1 hangup
451 case SIGINT: return "SIGINT"; // 2 interrupt
452 case SIGQUIT: return "SIGQUIT"; // 3 quit
453 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
454 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
455 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000456#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000457 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000458#endif
459#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000460 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000461#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000462 case SIGFPE: return "SIGFPE"; // 8 floating point exception
463 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
464 case SIGBUS: return "SIGBUS"; // 10 bus error
465 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
466 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
467 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
468 case SIGALRM: return "SIGALRM"; // 14 alarm clock
469 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
470 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
471 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
472 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
473 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
474 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
475 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
476 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
477#if !defined(_POSIX_C_SOURCE)
478 case SIGIO: return "SIGIO"; // 23 input/output possible signal
479#endif
480 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
481 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
482 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
483 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
484#if !defined(_POSIX_C_SOURCE)
485 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
486 case SIGINFO: return "SIGINFO"; // 29 information request
487#endif
488 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
489 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
490 default:
491 break;
492 }
493 return NULL;
494}
495
496void
497Host::WillTerminate ()
498{
499}
500
Johnny Chen4b663292011-08-02 20:52:42 +0000501#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000502void
503Host::ThreadCreated (const char *thread_name)
504{
505}
Greg Claytonb749a262010-12-03 06:02:24 +0000506
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000507void
Greg Claytonb749a262010-12-03 06:02:24 +0000508Host::Backtrace (Stream &strm, uint32_t max_frames)
509{
Greg Clayton52fd9842011-02-02 02:24:04 +0000510 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000511}
512
Greg Clayton638351a2010-12-04 00:10:17 +0000513size_t
514Host::GetEnvironment (StringList &env)
515{
Greg Clayton52fd9842011-02-02 02:24:04 +0000516 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000517 return 0;
518}
519
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000520#endif
521
522struct HostThreadCreateInfo
523{
524 std::string thread_name;
525 thread_func_t thread_fptr;
526 thread_arg_t thread_arg;
527
528 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
529 thread_name (name ? name : ""),
530 thread_fptr (fptr),
531 thread_arg (arg)
532 {
533 }
534};
535
536static thread_result_t
537ThreadCreateTrampoline (thread_arg_t arg)
538{
539 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
540 Host::ThreadCreated (info->thread_name.c_str());
541 thread_func_t thread_fptr = info->thread_fptr;
542 thread_arg_t thread_arg = info->thread_arg;
543
Greg Claytone005f2c2010-11-06 01:53:30 +0000544 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000545 if (log)
546 log->Printf("thread created");
547
548 delete info;
549 return thread_fptr (thread_arg);
550}
551
552lldb::thread_t
553Host::ThreadCreate
554(
555 const char *thread_name,
556 thread_func_t thread_fptr,
557 thread_arg_t thread_arg,
558 Error *error
559)
560{
561 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
562
563 // Host::ThreadCreateTrampoline will delete this pointer for us.
564 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
565
566 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
567 if (err == 0)
568 {
569 if (error)
570 error->Clear();
571 return thread;
572 }
573
574 if (error)
575 error->SetError (err, eErrorTypePOSIX);
576
577 return LLDB_INVALID_HOST_THREAD;
578}
579
580bool
581Host::ThreadCancel (lldb::thread_t thread, Error *error)
582{
583 int err = ::pthread_cancel (thread);
584 if (error)
585 error->SetError(err, eErrorTypePOSIX);
586 return err == 0;
587}
588
589bool
590Host::ThreadDetach (lldb::thread_t thread, Error *error)
591{
592 int err = ::pthread_detach (thread);
593 if (error)
594 error->SetError(err, eErrorTypePOSIX);
595 return err == 0;
596}
597
598bool
599Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
600{
601 int err = ::pthread_join (thread, thread_result_ptr);
602 if (error)
603 error->SetError(err, eErrorTypePOSIX);
604 return err == 0;
605}
606
Jim Ingham35dd4962012-05-04 19:24:49 +0000607// rdar://problem/8153284
608// Fixed a crasher where during shutdown, loggings attempted to access the
609// thread name but the static map instance had already been destructed.
610// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
611// pthread_once action. That map will get leaked.
612//
613// Another approach is to introduce a static guard object which monitors its
614// own destruction and raises a flag, but this incurs more overhead.
615
616static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
617static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
618
619static void
620InitThreadNamesMap()
621{
622 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
623}
624
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000625//------------------------------------------------------------------
626// Control access to a static file thread name map using a single
627// static function to avoid a static constructor.
628//------------------------------------------------------------------
629static const char *
630ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
631{
Jim Ingham35dd4962012-05-04 19:24:49 +0000632 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
633 if (success != 0)
634 return NULL;
635
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000636 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
637
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000638 if (get)
639 {
640 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000641 std::string value;
642 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
643 if (found_it)
644 return value.c_str();
645 else
646 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000647 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000648 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000649 {
650 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000651 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000652 }
653 return NULL;
654}
655
656const char *
657Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
658{
659 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
660 if (name == NULL)
661 {
662#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
663 // We currently can only get the name of a thread in the current process.
664 if (pid == Host::GetCurrentProcessID())
665 {
666 char pthread_name[1024];
667 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
668 {
669 if (pthread_name[0])
670 {
671 // Set the thread in our string pool
672 ThreadNameAccessor (false, pid, tid, pthread_name);
673 // Get our copy of the thread name string
674 name = ThreadNameAccessor (true, pid, tid, NULL);
675 }
676 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000677
678 if (name == NULL)
679 {
680 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
681 if (current_queue != NULL)
682 name = dispatch_queue_get_label (current_queue);
683 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000684 }
685#endif
686 }
687 return name;
688}
689
690void
691Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
692{
693 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
694 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
695 if (pid == LLDB_INVALID_PROCESS_ID)
696 pid = curr_pid;
697
698 if (tid == LLDB_INVALID_THREAD_ID)
699 tid = curr_tid;
700
701#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
702 // Set the pthread name if possible
703 if (pid == curr_pid && tid == curr_tid)
704 {
705 ::pthread_setname_np (name);
706 }
707#endif
708 ThreadNameAccessor (false, pid, tid, name);
709}
710
711FileSpec
712Host::GetProgramFileSpec ()
713{
714 static FileSpec g_program_filespec;
715 if (!g_program_filespec)
716 {
717#if defined (__APPLE__)
718 char program_fullpath[PATH_MAX];
719 // If DST is NULL, then return the number of bytes needed.
720 uint32_t len = sizeof(program_fullpath);
721 int err = _NSGetExecutablePath (program_fullpath, &len);
722 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000723 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000724 else if (err == -1)
725 {
726 char *large_program_fullpath = (char *)::malloc (len + 1);
727
728 err = _NSGetExecutablePath (large_program_fullpath, &len);
729 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000730 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000731
732 ::free (large_program_fullpath);
733 }
734#elif defined (__linux__)
735 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000736 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
737 if (len > 0) {
738 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000739 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000740 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000741#elif defined (__FreeBSD__)
742 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
743 size_t exe_path_size;
744 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
745 {
Greg Clayton366795e2011-01-13 01:27:55 +0000746 char *exe_path = new char[exe_path_size];
747 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
748 g_program_filespec.SetFile(exe_path, false);
749 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000750 }
751#endif
752 }
753 return g_program_filespec;
754}
755
756FileSpec
757Host::GetModuleFileSpecForHostAddress (const void *host_addr)
758{
759 FileSpec module_filespec;
760 Dl_info info;
761 if (::dladdr (host_addr, &info))
762 {
763 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000764 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000765 }
766 return module_filespec;
767}
768
769#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000770
771bool
772Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
773{
774 bundle.Clear();
775 return false;
776}
777
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000778bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000779Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000780{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000781 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000782}
783#endif
784
Greg Clayton14ef59f2011-02-08 00:35:34 +0000785// Opaque info that tracks a dynamic library that was loaded
786struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000787{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000788 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
789 file_spec (fs),
790 open_options (o),
791 handle (h)
792 {
793 }
794
795 const FileSpec file_spec;
796 uint32_t open_options;
797 void * handle;
798};
799
800void *
801Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
802{
Greg Clayton52fd9842011-02-02 02:24:04 +0000803 char path[PATH_MAX];
804 if (file_spec.GetPath(path, sizeof(path)))
805 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000806 int mode = 0;
807
808 if (options & eDynamicLibraryOpenOptionLazy)
809 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000810 else
811 mode |= RTLD_NOW;
812
Greg Clayton14ef59f2011-02-08 00:35:34 +0000813
814 if (options & eDynamicLibraryOpenOptionLocal)
815 mode |= RTLD_LOCAL;
816 else
817 mode |= RTLD_GLOBAL;
818
819#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
820 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
821 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000822#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000823
824 void * opaque = ::dlopen (path, mode);
825
826 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000827 {
828 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000829 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000830 }
831 else
832 {
833 error.SetErrorString(::dlerror());
834 }
835 }
836 else
837 {
838 error.SetErrorString("failed to extract path");
839 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000840 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000841}
842
843Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000844Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000845{
846 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000847 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000848 {
849 error.SetErrorString ("invalid dynamic library handle");
850 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000851 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000852 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000853 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
854 if (::dlclose (dylib_info->handle) != 0)
855 {
856 error.SetErrorString(::dlerror());
857 }
858
859 dylib_info->open_options = 0;
860 dylib_info->handle = 0;
861 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000862 }
863 return error;
864}
865
866void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000867Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000868{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000869 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000870 {
871 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000872 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000873 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000874 {
875 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
876
877 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
878 if (symbol_addr)
879 {
880#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
881 // This host doesn't support limiting searches to this shared library
882 // so we need to verify that the match came from this shared library
883 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000884 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000885 {
886 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
887 if (match_dylib_spec != dylib_info->file_spec)
888 {
889 char dylib_path[PATH_MAX];
890 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
891 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
892 else
893 error.SetErrorString ("symbol not found");
894 return NULL;
895 }
896 }
897#endif
898 error.Clear();
899 return symbol_addr;
900 }
901 else
902 {
903 error.SetErrorString(::dlerror());
904 }
905 }
906 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000907}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000908
909bool
910Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
911{
Greg Clayton5d187e52011-01-08 20:28:42 +0000912 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000913 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
914 // on linux this is assumed to be the "lldb" main executable. If LLDB on
915 // linux is actually in a shared library (lldb.so??) then this function will
916 // need to be modified to "do the right thing".
917
918 switch (path_type)
919 {
920 case ePathTypeLLDBShlibDir:
921 {
922 static ConstString g_lldb_so_dir;
923 if (!g_lldb_so_dir)
924 {
925 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
926 g_lldb_so_dir = lldb_file_spec.GetDirectory();
927 }
928 file_spec.GetDirectory() = g_lldb_so_dir;
929 return file_spec.GetDirectory();
930 }
931 break;
932
933 case ePathTypeSupportExecutableDir:
934 {
935 static ConstString g_lldb_support_exe_dir;
936 if (!g_lldb_support_exe_dir)
937 {
938 FileSpec lldb_file_spec;
939 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
940 {
941 char raw_path[PATH_MAX];
942 char resolved_path[PATH_MAX];
943 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
944
945#if defined (__APPLE__)
946 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
947 if (framework_pos)
948 {
949 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000950#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000951 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000952#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000953 }
954#endif
955 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
956 g_lldb_support_exe_dir.SetCString(resolved_path);
957 }
958 }
959 file_spec.GetDirectory() = g_lldb_support_exe_dir;
960 return file_spec.GetDirectory();
961 }
962 break;
963
964 case ePathTypeHeaderDir:
965 {
966 static ConstString g_lldb_headers_dir;
967 if (!g_lldb_headers_dir)
968 {
969#if defined (__APPLE__)
970 FileSpec lldb_file_spec;
971 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
972 {
973 char raw_path[PATH_MAX];
974 char resolved_path[PATH_MAX];
975 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
976
977 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
978 if (framework_pos)
979 {
980 framework_pos += strlen("LLDB.framework");
981 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
982 }
983 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
984 g_lldb_headers_dir.SetCString(resolved_path);
985 }
986#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000987 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000988 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
989#endif
990 }
991 file_spec.GetDirectory() = g_lldb_headers_dir;
992 return file_spec.GetDirectory();
993 }
994 break;
995
996 case ePathTypePythonDir:
997 {
Greg Clayton52fd9842011-02-02 02:24:04 +0000998 // TODO: Anyone know how we can determine this for linux? Other systems?
Filipe Cabecinhasdf802722012-07-30 16:46:32 +0000999 // For linux and FreeBSD we are currently assuming the
1000 // location of the lldb binary that contains this function is
1001 // the directory that will contain a python directory which
1002 // has our lldb module. This is how files get placed when
1003 // compiling with Makefiles.
Greg Clayton24b48ff2010-10-17 22:03:32 +00001004
1005 static ConstString g_lldb_python_dir;
1006 if (!g_lldb_python_dir)
1007 {
1008 FileSpec lldb_file_spec;
1009 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1010 {
1011 char raw_path[PATH_MAX];
1012 char resolved_path[PATH_MAX];
1013 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1014
1015#if defined (__APPLE__)
1016 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1017 if (framework_pos)
1018 {
1019 framework_pos += strlen("LLDB.framework");
1020 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1021 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001022#else
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +00001023 // We may get our string truncated. Should we protect
1024 // this with an assert?
1025 ::strncat(raw_path, "/python", sizeof(raw_path) - strlen(raw_path) - 1);
Greg Clayton24b48ff2010-10-17 22:03:32 +00001026#endif
1027 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1028 g_lldb_python_dir.SetCString(resolved_path);
1029 }
1030 }
1031 file_spec.GetDirectory() = g_lldb_python_dir;
1032 return file_spec.GetDirectory();
1033 }
1034 break;
1035
Greg Clayton52fd9842011-02-02 02:24:04 +00001036 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1037 {
1038#if defined (__APPLE__)
1039 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001040 static bool g_lldb_system_plugin_dir_located = false;
1041 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001042 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001043 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001044 FileSpec lldb_file_spec;
1045 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1046 {
1047 char raw_path[PATH_MAX];
1048 char resolved_path[PATH_MAX];
1049 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1050
1051 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1052 if (framework_pos)
1053 {
1054 framework_pos += strlen("LLDB.framework");
1055 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001056 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1057 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001058 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001059 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001060 }
1061 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001062
1063 if (g_lldb_system_plugin_dir)
1064 {
1065 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1066 return true;
1067 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001068#endif
1069 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1070 return false;
1071 }
1072 break;
1073
1074 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1075 {
1076#if defined (__APPLE__)
1077 static ConstString g_lldb_user_plugin_dir;
1078 if (!g_lldb_user_plugin_dir)
1079 {
1080 char user_plugin_path[PATH_MAX];
1081 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1082 user_plugin_path,
1083 sizeof(user_plugin_path)))
1084 {
1085 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1086 }
1087 }
1088 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1089 return file_spec.GetDirectory();
1090#endif
1091 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1092 return false;
1093 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001094 default:
1095 assert (!"Unhandled PathType");
1096 break;
1097 }
1098
1099 return false;
1100}
1101
Greg Clayton58e26e02011-03-24 04:28:38 +00001102
1103bool
1104Host::GetHostname (std::string &s)
1105{
1106 char hostname[PATH_MAX];
1107 hostname[sizeof(hostname) - 1] = '\0';
1108 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1109 {
1110 struct hostent* h = ::gethostbyname (hostname);
1111 if (h)
1112 s.assign (h->h_name);
1113 else
1114 s.assign (hostname);
1115 return true;
1116 }
1117 return false;
1118}
1119
Greg Clayton24bc5d92011-03-30 18:16:51 +00001120const char *
1121Host::GetUserName (uint32_t uid, std::string &user_name)
1122{
1123 struct passwd user_info;
1124 struct passwd *user_info_ptr = &user_info;
1125 char user_buffer[PATH_MAX];
1126 size_t user_buffer_size = sizeof(user_buffer);
1127 if (::getpwuid_r (uid,
1128 &user_info,
1129 user_buffer,
1130 user_buffer_size,
1131 &user_info_ptr) == 0)
1132 {
1133 if (user_info_ptr)
1134 {
1135 user_name.assign (user_info_ptr->pw_name);
1136 return user_name.c_str();
1137 }
1138 }
1139 user_name.clear();
1140 return NULL;
1141}
1142
1143const char *
1144Host::GetGroupName (uint32_t gid, std::string &group_name)
1145{
1146 char group_buffer[PATH_MAX];
1147 size_t group_buffer_size = sizeof(group_buffer);
1148 struct group group_info;
1149 struct group *group_info_ptr = &group_info;
1150 // Try the threadsafe version first
1151 if (::getgrgid_r (gid,
1152 &group_info,
1153 group_buffer,
1154 group_buffer_size,
1155 &group_info_ptr) == 0)
1156 {
1157 if (group_info_ptr)
1158 {
1159 group_name.assign (group_info_ptr->gr_name);
1160 return group_name.c_str();
1161 }
1162 }
1163 else
1164 {
1165 // The threadsafe version isn't currently working
1166 // for me on darwin, but the non-threadsafe version
1167 // is, so I am calling it below.
1168 group_info_ptr = ::getgrgid (gid);
1169 if (group_info_ptr)
1170 {
1171 group_name.assign (group_info_ptr->gr_name);
1172 return group_name.c_str();
1173 }
1174 }
1175 group_name.clear();
1176 return NULL;
1177}
1178
Johnny Chen4b663292011-08-02 20:52:42 +00001179#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001180bool
1181Host::GetOSBuildString (std::string &s)
1182{
1183 s.clear();
1184 return false;
1185}
1186
1187bool
1188Host::GetOSKernelDescription (std::string &s)
1189{
1190 s.clear();
1191 return false;
1192}
Johnny Chen4b663292011-08-02 20:52:42 +00001193#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001194
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001195uint32_t
1196Host::GetUserID ()
1197{
1198 return getuid();
1199}
1200
1201uint32_t
1202Host::GetGroupID ()
1203{
1204 return getgid();
1205}
1206
1207uint32_t
1208Host::GetEffectiveUserID ()
1209{
1210 return geteuid();
1211}
1212
1213uint32_t
1214Host::GetEffectiveGroupID ()
1215{
1216 return getegid();
1217}
1218
1219#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001220uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001221Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001222{
1223 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001224 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001225}
Johnny Chen4b663292011-08-02 20:52:42 +00001226#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001227
Johnny Chen4b663292011-08-02 20:52:42 +00001228#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001229bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001230Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001231{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001232 process_info.Clear();
1233 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001234}
Johnny Chen4b663292011-08-02 20:52:42 +00001235#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001236
Sean Callananf35a96c2011-10-27 21:22:25 +00001237lldb::TargetSP
1238Host::GetDummyTarget (lldb_private::Debugger &debugger)
1239{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001240 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001241
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001242 // FIXME: Maybe the dummy target should be per-Debugger
1243 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1244 {
1245 ArchSpec arch(Target::GetDefaultArchitecture());
1246 if (!arch.IsValid())
1247 arch = Host::GetArchitecture ();
1248 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001249 NULL,
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001250 arch.GetTriple().getTriple().c_str(),
1251 false,
1252 NULL,
1253 g_dummy_target_sp);
1254 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001255
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001256 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001257}
1258
Greg Clayton97471182012-04-14 01:42:46 +00001259struct ShellInfo
1260{
1261 ShellInfo () :
1262 process_reaped (false),
1263 can_delete (false),
1264 pid (LLDB_INVALID_PROCESS_ID),
1265 signo(-1),
1266 status(-1)
1267 {
1268 }
1269
1270 lldb_private::Predicate<bool> process_reaped;
1271 lldb_private::Predicate<bool> can_delete;
1272 lldb::pid_t pid;
1273 int signo;
1274 int status;
1275};
1276
1277static bool
1278MonitorShellCommand (void *callback_baton,
1279 lldb::pid_t pid,
1280 bool exited, // True if the process did exit
1281 int signo, // Zero for no signal
1282 int status) // Exit value of process if signal is zero
1283{
1284 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1285 shell_info->pid = pid;
1286 shell_info->signo = signo;
1287 shell_info->status = status;
1288 // Let the thread running Host::RunShellCommand() know that the process
1289 // exited and that ShellInfo has been filled in by broadcasting to it
1290 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1291 // Now wait for a handshake back from that thread running Host::RunShellCommand
1292 // so we know that we can delete shell_info_ptr
1293 shell_info->can_delete.WaitForValueEqualTo(true);
1294 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1295 usleep(1000);
1296 // Now delete the shell info that was passed into this function
1297 delete shell_info;
1298 return true;
1299}
1300
1301Error
1302Host::RunShellCommand (const char *command,
1303 const char *working_dir,
1304 int *status_ptr,
1305 int *signo_ptr,
1306 std::string *command_output_ptr,
Greg Claytonb924eb62012-09-27 03:13:55 +00001307 uint32_t timeout_sec,
1308 const char *shell)
Greg Clayton97471182012-04-14 01:42:46 +00001309{
1310 Error error;
1311 ProcessLaunchInfo launch_info;
Greg Claytonb924eb62012-09-27 03:13:55 +00001312 if (shell && shell[0])
1313 {
1314 // Run the command in a shell
1315 launch_info.SetShell(shell);
1316 launch_info.GetArguments().AppendArgument(command);
1317 const bool localhost = true;
1318 const bool will_debug = false;
1319 const bool first_arg_is_full_shell_command = true;
1320 launch_info.ConvertArgumentsForLaunchingInShell (error,
1321 localhost,
1322 will_debug,
1323 first_arg_is_full_shell_command);
1324 }
1325 else
1326 {
1327 // No shell, just run it
1328 Args args (command);
1329 const bool first_arg_is_executable = true;
Greg Clayton0c8446c2012-10-17 22:57:12 +00001330 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonb924eb62012-09-27 03:13:55 +00001331 }
Greg Clayton97471182012-04-14 01:42:46 +00001332
1333 if (working_dir)
1334 launch_info.SetWorkingDirectory(working_dir);
1335 char output_file_path_buffer[L_tmpnam];
1336 const char *output_file_path = NULL;
1337 if (command_output_ptr)
1338 {
1339 // Create a temporary file to get the stdout/stderr and redirect the
1340 // output of the command into this file. We will later read this file
1341 // if all goes well and fill the data into "command_output_ptr"
1342 output_file_path = ::tmpnam(output_file_path_buffer);
1343 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1344 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonb924eb62012-09-27 03:13:55 +00001345 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Clayton97471182012-04-14 01:42:46 +00001346 }
1347 else
1348 {
1349 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1350 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1351 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1352 }
1353
1354 // The process monitor callback will delete the 'shell_info_ptr' below...
1355 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1356
1357 const bool monitor_signals = false;
1358 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1359
1360 error = LaunchProcess (launch_info);
1361 const lldb::pid_t pid = launch_info.GetProcessID();
1362 if (pid != LLDB_INVALID_PROCESS_ID)
1363 {
1364 // The process successfully launched, so we can defer ownership of
1365 // "shell_info" to the MonitorShellCommand callback function that will
1366 // get called when the process dies. We release the std::auto_ptr as it
1367 // doesn't need to delete the ShellInfo anymore.
1368 ShellInfo *shell_info = shell_info_ap.release();
1369 TimeValue timeout_time(TimeValue::Now());
1370 timeout_time.OffsetWithSeconds(timeout_sec);
1371 bool timed_out = false;
1372 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1373 if (timed_out)
1374 {
1375 error.SetErrorString("timed out waiting for shell command to complete");
1376
1377 // Kill the process since it didn't complete withint the timeout specified
1378 ::kill (pid, SIGKILL);
1379 // Wait for the monitor callback to get the message
1380 timeout_time = TimeValue::Now();
1381 timeout_time.OffsetWithSeconds(1);
1382 timed_out = false;
1383 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1384 }
1385 else
1386 {
1387 if (status_ptr)
1388 *status_ptr = shell_info->status;
1389
1390 if (signo_ptr)
1391 *signo_ptr = shell_info->signo;
1392
1393 if (command_output_ptr)
1394 {
1395 command_output_ptr->clear();
1396 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1397 uint64_t file_size = file_spec.GetByteSize();
1398 if (file_size > 0)
1399 {
1400 if (file_size > command_output_ptr->max_size())
1401 {
1402 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1403 }
1404 else
1405 {
1406 command_output_ptr->resize(file_size);
1407 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1408 }
1409 }
1410 }
1411 }
1412 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1413 }
1414 else
1415 {
1416 error.SetErrorString("failed to get process ID");
1417 }
1418
1419 if (output_file_path)
1420 ::unlink (output_file_path);
1421 // Handshake with the monitor thread, or just let it know in advance that
1422 // it can delete "shell_info" in case we timed out and were not able to kill
1423 // the process...
1424 return error;
1425}
1426
1427
1428
Johnny Chen4b663292011-08-02 20:52:42 +00001429#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001430bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001431Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001432{
1433 return false;
1434}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001435
Greg Claytone98ac252010-11-10 04:57:04 +00001436void
1437Host::SetCrashDescriptionWithFormat (const char *format, ...)
1438{
1439}
1440
1441void
1442Host::SetCrashDescription (const char *description)
1443{
1444}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001445
1446lldb::pid_t
1447LaunchApplication (const FileSpec &app_file_spec)
1448{
1449 return LLDB_INVALID_PROCESS_ID;
1450}
1451
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001452#endif