blob: e5866687c54d0da6e51d96e33682b7de2ad64875 [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];
92 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid);
93 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)
Greg Clayton1c4642c2011-11-16 05:37:56 +0000152 log->Printf("%s ::wait_pid (pid = %i, &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)
Greg Clayton1c4642c2011-11-16 05:37:56 +0000201 log->Printf ("%s ::waitpid (pid = %i, &status, options = %i) => pid = %i, 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 case llvm::Triple::cellspu:
357 g_host_arch_64.SetTriple(triple);
358 g_supports_64 = true;
359 break;
360 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000361
362 g_supports_32 = g_host_arch_32.IsValid();
363 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000364 }
Greg Clayton395fc332011-02-15 21:59:32 +0000365
366#endif // #else for #if defined (__APPLE__)
367
368 if (arch_kind == eSystemDefaultArchitecture32)
369 return g_host_arch_32;
370 else if (arch_kind == eSystemDefaultArchitecture64)
371 return g_host_arch_64;
372
373 if (g_supports_64)
374 return g_host_arch_64;
375
376 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000377}
378
379const ConstString &
380Host::GetVendorString()
381{
382 static ConstString g_vendor;
383 if (!g_vendor)
384 {
385#if defined (__APPLE__)
Greg Clayton5b0025f2012-05-12 00:01:21 +0000386 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
387 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
388 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000389#elif defined (__linux__)
390 g_vendor.SetCString("gnu");
Johnny Chen4b663292011-08-02 20:52:42 +0000391#elif defined (__FreeBSD__)
392 g_vendor.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000393#endif
394 }
395 return g_vendor;
396}
397
398const ConstString &
399Host::GetOSString()
400{
401 static ConstString g_os_string;
402 if (!g_os_string)
403 {
404#if defined (__APPLE__)
Greg Clayton5b0025f2012-05-12 00:01:21 +0000405 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
406 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
407 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000408#elif defined (__linux__)
409 g_os_string.SetCString("linux");
Johnny Chen4b663292011-08-02 20:52:42 +0000410#elif defined (__FreeBSD__)
411 g_os_string.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000412#endif
413 }
414 return g_os_string;
415}
416
417const ConstString &
418Host::GetTargetTriple()
419{
420 static ConstString g_host_triple;
421 if (!(g_host_triple))
422 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000423 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
424 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000425 }
426 return g_host_triple;
427}
428
429lldb::pid_t
430Host::GetCurrentProcessID()
431{
432 return ::getpid();
433}
434
435lldb::tid_t
436Host::GetCurrentThreadID()
437{
438#if defined (__APPLE__)
Greg Claytone93725b2012-09-18 18:19:49 +0000439 // Calling "mach_port_deallocate()" bumps the reference count on the thread
440 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
441 // count.
442 thread_port_t thread_self = mach_thread_self();
443 mach_port_deallocate(mach_task_self(), thread_self);
444 return thread_self;
Johnny Chen4b663292011-08-02 20:52:42 +0000445#elif defined(__FreeBSD__)
446 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000447#else
448 return lldb::tid_t(pthread_self());
449#endif
450}
451
Jim Ingham1831e782012-04-07 00:00:41 +0000452lldb::thread_t
453Host::GetCurrentThread ()
454{
455 return lldb::thread_t(pthread_self());
456}
457
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000458const char *
459Host::GetSignalAsCString (int signo)
460{
461 switch (signo)
462 {
463 case SIGHUP: return "SIGHUP"; // 1 hangup
464 case SIGINT: return "SIGINT"; // 2 interrupt
465 case SIGQUIT: return "SIGQUIT"; // 3 quit
466 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
467 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
468 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000469#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000470 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000471#endif
472#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000473 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000474#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000475 case SIGFPE: return "SIGFPE"; // 8 floating point exception
476 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
477 case SIGBUS: return "SIGBUS"; // 10 bus error
478 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
479 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
480 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
481 case SIGALRM: return "SIGALRM"; // 14 alarm clock
482 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
483 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
484 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
485 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
486 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
487 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
488 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
489 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
490#if !defined(_POSIX_C_SOURCE)
491 case SIGIO: return "SIGIO"; // 23 input/output possible signal
492#endif
493 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
494 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
495 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
496 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
497#if !defined(_POSIX_C_SOURCE)
498 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
499 case SIGINFO: return "SIGINFO"; // 29 information request
500#endif
501 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
502 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
503 default:
504 break;
505 }
506 return NULL;
507}
508
509void
510Host::WillTerminate ()
511{
512}
513
Johnny Chen4b663292011-08-02 20:52:42 +0000514#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000515void
516Host::ThreadCreated (const char *thread_name)
517{
518}
Greg Claytonb749a262010-12-03 06:02:24 +0000519
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000520void
Greg Claytonb749a262010-12-03 06:02:24 +0000521Host::Backtrace (Stream &strm, uint32_t max_frames)
522{
Greg Clayton52fd9842011-02-02 02:24:04 +0000523 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000524}
525
Greg Clayton638351a2010-12-04 00:10:17 +0000526size_t
527Host::GetEnvironment (StringList &env)
528{
Greg Clayton52fd9842011-02-02 02:24:04 +0000529 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000530 return 0;
531}
532
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000533#endif
534
535struct HostThreadCreateInfo
536{
537 std::string thread_name;
538 thread_func_t thread_fptr;
539 thread_arg_t thread_arg;
540
541 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
542 thread_name (name ? name : ""),
543 thread_fptr (fptr),
544 thread_arg (arg)
545 {
546 }
547};
548
549static thread_result_t
550ThreadCreateTrampoline (thread_arg_t arg)
551{
552 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
553 Host::ThreadCreated (info->thread_name.c_str());
554 thread_func_t thread_fptr = info->thread_fptr;
555 thread_arg_t thread_arg = info->thread_arg;
556
Greg Claytone005f2c2010-11-06 01:53:30 +0000557 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000558 if (log)
559 log->Printf("thread created");
560
561 delete info;
562 return thread_fptr (thread_arg);
563}
564
565lldb::thread_t
566Host::ThreadCreate
567(
568 const char *thread_name,
569 thread_func_t thread_fptr,
570 thread_arg_t thread_arg,
571 Error *error
572)
573{
574 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
575
576 // Host::ThreadCreateTrampoline will delete this pointer for us.
577 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
578
579 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
580 if (err == 0)
581 {
582 if (error)
583 error->Clear();
584 return thread;
585 }
586
587 if (error)
588 error->SetError (err, eErrorTypePOSIX);
589
590 return LLDB_INVALID_HOST_THREAD;
591}
592
593bool
594Host::ThreadCancel (lldb::thread_t thread, Error *error)
595{
596 int err = ::pthread_cancel (thread);
597 if (error)
598 error->SetError(err, eErrorTypePOSIX);
599 return err == 0;
600}
601
602bool
603Host::ThreadDetach (lldb::thread_t thread, Error *error)
604{
605 int err = ::pthread_detach (thread);
606 if (error)
607 error->SetError(err, eErrorTypePOSIX);
608 return err == 0;
609}
610
611bool
612Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
613{
614 int err = ::pthread_join (thread, thread_result_ptr);
615 if (error)
616 error->SetError(err, eErrorTypePOSIX);
617 return err == 0;
618}
619
Jim Ingham35dd4962012-05-04 19:24:49 +0000620// rdar://problem/8153284
621// Fixed a crasher where during shutdown, loggings attempted to access the
622// thread name but the static map instance had already been destructed.
623// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
624// pthread_once action. That map will get leaked.
625//
626// Another approach is to introduce a static guard object which monitors its
627// own destruction and raises a flag, but this incurs more overhead.
628
629static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
630static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
631
632static void
633InitThreadNamesMap()
634{
635 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
636}
637
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000638//------------------------------------------------------------------
639// Control access to a static file thread name map using a single
640// static function to avoid a static constructor.
641//------------------------------------------------------------------
642static const char *
643ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
644{
Jim Ingham35dd4962012-05-04 19:24:49 +0000645 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
646 if (success != 0)
647 return NULL;
648
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000649 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
650
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000651 if (get)
652 {
653 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000654 std::string value;
655 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
656 if (found_it)
657 return value.c_str();
658 else
659 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000660 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000661 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000662 {
663 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000664 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000665 }
666 return NULL;
667}
668
669const char *
670Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
671{
672 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
673 if (name == NULL)
674 {
675#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
676 // We currently can only get the name of a thread in the current process.
677 if (pid == Host::GetCurrentProcessID())
678 {
679 char pthread_name[1024];
680 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
681 {
682 if (pthread_name[0])
683 {
684 // Set the thread in our string pool
685 ThreadNameAccessor (false, pid, tid, pthread_name);
686 // Get our copy of the thread name string
687 name = ThreadNameAccessor (true, pid, tid, NULL);
688 }
689 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000690
691 if (name == NULL)
692 {
693 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
694 if (current_queue != NULL)
695 name = dispatch_queue_get_label (current_queue);
696 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000697 }
698#endif
699 }
700 return name;
701}
702
703void
704Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
705{
706 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
707 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
708 if (pid == LLDB_INVALID_PROCESS_ID)
709 pid = curr_pid;
710
711 if (tid == LLDB_INVALID_THREAD_ID)
712 tid = curr_tid;
713
714#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
715 // Set the pthread name if possible
716 if (pid == curr_pid && tid == curr_tid)
717 {
718 ::pthread_setname_np (name);
719 }
720#endif
721 ThreadNameAccessor (false, pid, tid, name);
722}
723
724FileSpec
725Host::GetProgramFileSpec ()
726{
727 static FileSpec g_program_filespec;
728 if (!g_program_filespec)
729 {
730#if defined (__APPLE__)
731 char program_fullpath[PATH_MAX];
732 // If DST is NULL, then return the number of bytes needed.
733 uint32_t len = sizeof(program_fullpath);
734 int err = _NSGetExecutablePath (program_fullpath, &len);
735 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000736 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000737 else if (err == -1)
738 {
739 char *large_program_fullpath = (char *)::malloc (len + 1);
740
741 err = _NSGetExecutablePath (large_program_fullpath, &len);
742 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000743 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000744
745 ::free (large_program_fullpath);
746 }
747#elif defined (__linux__)
748 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000749 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
750 if (len > 0) {
751 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000752 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000753 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000754#elif defined (__FreeBSD__)
755 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
756 size_t exe_path_size;
757 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
758 {
Greg Clayton366795e2011-01-13 01:27:55 +0000759 char *exe_path = new char[exe_path_size];
760 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
761 g_program_filespec.SetFile(exe_path, false);
762 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000763 }
764#endif
765 }
766 return g_program_filespec;
767}
768
769FileSpec
770Host::GetModuleFileSpecForHostAddress (const void *host_addr)
771{
772 FileSpec module_filespec;
773 Dl_info info;
774 if (::dladdr (host_addr, &info))
775 {
776 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000777 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000778 }
779 return module_filespec;
780}
781
782#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000783
784bool
785Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
786{
787 bundle.Clear();
788 return false;
789}
790
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000791bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000792Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000793{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000794 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000795}
796#endif
797
Greg Clayton14ef59f2011-02-08 00:35:34 +0000798// Opaque info that tracks a dynamic library that was loaded
799struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000800{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000801 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
802 file_spec (fs),
803 open_options (o),
804 handle (h)
805 {
806 }
807
808 const FileSpec file_spec;
809 uint32_t open_options;
810 void * handle;
811};
812
813void *
814Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
815{
Greg Clayton52fd9842011-02-02 02:24:04 +0000816 char path[PATH_MAX];
817 if (file_spec.GetPath(path, sizeof(path)))
818 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000819 int mode = 0;
820
821 if (options & eDynamicLibraryOpenOptionLazy)
822 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000823 else
824 mode |= RTLD_NOW;
825
Greg Clayton14ef59f2011-02-08 00:35:34 +0000826
827 if (options & eDynamicLibraryOpenOptionLocal)
828 mode |= RTLD_LOCAL;
829 else
830 mode |= RTLD_GLOBAL;
831
832#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
833 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
834 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000835#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000836
837 void * opaque = ::dlopen (path, mode);
838
839 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000840 {
841 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000842 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000843 }
844 else
845 {
846 error.SetErrorString(::dlerror());
847 }
848 }
849 else
850 {
851 error.SetErrorString("failed to extract path");
852 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000853 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000854}
855
856Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000857Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000858{
859 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000860 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000861 {
862 error.SetErrorString ("invalid dynamic library handle");
863 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000864 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000865 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000866 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
867 if (::dlclose (dylib_info->handle) != 0)
868 {
869 error.SetErrorString(::dlerror());
870 }
871
872 dylib_info->open_options = 0;
873 dylib_info->handle = 0;
874 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000875 }
876 return error;
877}
878
879void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000880Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000881{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000882 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000883 {
884 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000885 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000886 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000887 {
888 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
889
890 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
891 if (symbol_addr)
892 {
893#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
894 // This host doesn't support limiting searches to this shared library
895 // so we need to verify that the match came from this shared library
896 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000897 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000898 {
899 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
900 if (match_dylib_spec != dylib_info->file_spec)
901 {
902 char dylib_path[PATH_MAX];
903 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
904 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
905 else
906 error.SetErrorString ("symbol not found");
907 return NULL;
908 }
909 }
910#endif
911 error.Clear();
912 return symbol_addr;
913 }
914 else
915 {
916 error.SetErrorString(::dlerror());
917 }
918 }
919 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000920}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000921
922bool
923Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
924{
Greg Clayton5d187e52011-01-08 20:28:42 +0000925 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000926 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
927 // on linux this is assumed to be the "lldb" main executable. If LLDB on
928 // linux is actually in a shared library (lldb.so??) then this function will
929 // need to be modified to "do the right thing".
930
931 switch (path_type)
932 {
933 case ePathTypeLLDBShlibDir:
934 {
935 static ConstString g_lldb_so_dir;
936 if (!g_lldb_so_dir)
937 {
938 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
939 g_lldb_so_dir = lldb_file_spec.GetDirectory();
940 }
941 file_spec.GetDirectory() = g_lldb_so_dir;
942 return file_spec.GetDirectory();
943 }
944 break;
945
946 case ePathTypeSupportExecutableDir:
947 {
948 static ConstString g_lldb_support_exe_dir;
949 if (!g_lldb_support_exe_dir)
950 {
951 FileSpec lldb_file_spec;
952 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
953 {
954 char raw_path[PATH_MAX];
955 char resolved_path[PATH_MAX];
956 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
957
958#if defined (__APPLE__)
959 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
960 if (framework_pos)
961 {
962 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000963#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000964 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000965#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000966 }
967#endif
968 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
969 g_lldb_support_exe_dir.SetCString(resolved_path);
970 }
971 }
972 file_spec.GetDirectory() = g_lldb_support_exe_dir;
973 return file_spec.GetDirectory();
974 }
975 break;
976
977 case ePathTypeHeaderDir:
978 {
979 static ConstString g_lldb_headers_dir;
980 if (!g_lldb_headers_dir)
981 {
982#if defined (__APPLE__)
983 FileSpec lldb_file_spec;
984 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
985 {
986 char raw_path[PATH_MAX];
987 char resolved_path[PATH_MAX];
988 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
989
990 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
991 if (framework_pos)
992 {
993 framework_pos += strlen("LLDB.framework");
994 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
995 }
996 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
997 g_lldb_headers_dir.SetCString(resolved_path);
998 }
999#else
Greg Clayton52fd9842011-02-02 02:24:04 +00001000 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +00001001 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
1002#endif
1003 }
1004 file_spec.GetDirectory() = g_lldb_headers_dir;
1005 return file_spec.GetDirectory();
1006 }
1007 break;
1008
1009 case ePathTypePythonDir:
1010 {
Greg Clayton52fd9842011-02-02 02:24:04 +00001011 // TODO: Anyone know how we can determine this for linux? Other systems?
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001012 // For linux and FreeBSD we are currently assuming the
1013 // location of the lldb binary that contains this function is
1014 // the directory that will contain a python directory which
1015 // has our lldb module. This is how files get placed when
1016 // compiling with Makefiles.
Greg Clayton24b48ff2010-10-17 22:03:32 +00001017
1018 static ConstString g_lldb_python_dir;
1019 if (!g_lldb_python_dir)
1020 {
1021 FileSpec lldb_file_spec;
1022 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1023 {
1024 char raw_path[PATH_MAX];
1025 char resolved_path[PATH_MAX];
1026 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1027
1028#if defined (__APPLE__)
1029 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1030 if (framework_pos)
1031 {
1032 framework_pos += strlen("LLDB.framework");
1033 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1034 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001035#else
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +00001036 // We may get our string truncated. Should we protect
1037 // this with an assert?
1038 ::strncat(raw_path, "/python", sizeof(raw_path) - strlen(raw_path) - 1);
Greg Clayton24b48ff2010-10-17 22:03:32 +00001039#endif
1040 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1041 g_lldb_python_dir.SetCString(resolved_path);
1042 }
1043 }
1044 file_spec.GetDirectory() = g_lldb_python_dir;
1045 return file_spec.GetDirectory();
1046 }
1047 break;
1048
Greg Clayton52fd9842011-02-02 02:24:04 +00001049 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1050 {
1051#if defined (__APPLE__)
1052 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001053 static bool g_lldb_system_plugin_dir_located = false;
1054 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001055 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001056 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001057 FileSpec lldb_file_spec;
1058 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1059 {
1060 char raw_path[PATH_MAX];
1061 char resolved_path[PATH_MAX];
1062 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1063
1064 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1065 if (framework_pos)
1066 {
1067 framework_pos += strlen("LLDB.framework");
1068 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001069 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1070 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001071 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001072 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001073 }
1074 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001075
1076 if (g_lldb_system_plugin_dir)
1077 {
1078 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1079 return true;
1080 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001081#endif
1082 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1083 return false;
1084 }
1085 break;
1086
1087 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1088 {
1089#if defined (__APPLE__)
1090 static ConstString g_lldb_user_plugin_dir;
1091 if (!g_lldb_user_plugin_dir)
1092 {
1093 char user_plugin_path[PATH_MAX];
1094 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1095 user_plugin_path,
1096 sizeof(user_plugin_path)))
1097 {
1098 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1099 }
1100 }
1101 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1102 return file_spec.GetDirectory();
1103#endif
1104 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1105 return false;
1106 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001107 default:
1108 assert (!"Unhandled PathType");
1109 break;
1110 }
1111
1112 return false;
1113}
1114
Greg Clayton58e26e02011-03-24 04:28:38 +00001115
1116bool
1117Host::GetHostname (std::string &s)
1118{
1119 char hostname[PATH_MAX];
1120 hostname[sizeof(hostname) - 1] = '\0';
1121 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1122 {
1123 struct hostent* h = ::gethostbyname (hostname);
1124 if (h)
1125 s.assign (h->h_name);
1126 else
1127 s.assign (hostname);
1128 return true;
1129 }
1130 return false;
1131}
1132
Greg Clayton24bc5d92011-03-30 18:16:51 +00001133const char *
1134Host::GetUserName (uint32_t uid, std::string &user_name)
1135{
1136 struct passwd user_info;
1137 struct passwd *user_info_ptr = &user_info;
1138 char user_buffer[PATH_MAX];
1139 size_t user_buffer_size = sizeof(user_buffer);
1140 if (::getpwuid_r (uid,
1141 &user_info,
1142 user_buffer,
1143 user_buffer_size,
1144 &user_info_ptr) == 0)
1145 {
1146 if (user_info_ptr)
1147 {
1148 user_name.assign (user_info_ptr->pw_name);
1149 return user_name.c_str();
1150 }
1151 }
1152 user_name.clear();
1153 return NULL;
1154}
1155
1156const char *
1157Host::GetGroupName (uint32_t gid, std::string &group_name)
1158{
1159 char group_buffer[PATH_MAX];
1160 size_t group_buffer_size = sizeof(group_buffer);
1161 struct group group_info;
1162 struct group *group_info_ptr = &group_info;
1163 // Try the threadsafe version first
1164 if (::getgrgid_r (gid,
1165 &group_info,
1166 group_buffer,
1167 group_buffer_size,
1168 &group_info_ptr) == 0)
1169 {
1170 if (group_info_ptr)
1171 {
1172 group_name.assign (group_info_ptr->gr_name);
1173 return group_name.c_str();
1174 }
1175 }
1176 else
1177 {
1178 // The threadsafe version isn't currently working
1179 // for me on darwin, but the non-threadsafe version
1180 // is, so I am calling it below.
1181 group_info_ptr = ::getgrgid (gid);
1182 if (group_info_ptr)
1183 {
1184 group_name.assign (group_info_ptr->gr_name);
1185 return group_name.c_str();
1186 }
1187 }
1188 group_name.clear();
1189 return NULL;
1190}
1191
Johnny Chen4b663292011-08-02 20:52:42 +00001192#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001193bool
1194Host::GetOSBuildString (std::string &s)
1195{
1196 s.clear();
1197 return false;
1198}
1199
1200bool
1201Host::GetOSKernelDescription (std::string &s)
1202{
1203 s.clear();
1204 return false;
1205}
Johnny Chen4b663292011-08-02 20:52:42 +00001206#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001207
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001208uint32_t
1209Host::GetUserID ()
1210{
1211 return getuid();
1212}
1213
1214uint32_t
1215Host::GetGroupID ()
1216{
1217 return getgid();
1218}
1219
1220uint32_t
1221Host::GetEffectiveUserID ()
1222{
1223 return geteuid();
1224}
1225
1226uint32_t
1227Host::GetEffectiveGroupID ()
1228{
1229 return getegid();
1230}
1231
1232#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001233uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001234Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001235{
1236 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001237 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001238}
Johnny Chen4b663292011-08-02 20:52:42 +00001239#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001240
Johnny Chen4b663292011-08-02 20:52:42 +00001241#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001242bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001243Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001244{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001245 process_info.Clear();
1246 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001247}
Johnny Chen4b663292011-08-02 20:52:42 +00001248#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001249
Sean Callananf35a96c2011-10-27 21:22:25 +00001250lldb::TargetSP
1251Host::GetDummyTarget (lldb_private::Debugger &debugger)
1252{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001253 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001254
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001255 // FIXME: Maybe the dummy target should be per-Debugger
1256 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1257 {
1258 ArchSpec arch(Target::GetDefaultArchitecture());
1259 if (!arch.IsValid())
1260 arch = Host::GetArchitecture ();
1261 Error err = debugger.GetTargetList().CreateTarget(debugger,
1262 FileSpec(),
1263 arch.GetTriple().getTriple().c_str(),
1264 false,
1265 NULL,
1266 g_dummy_target_sp);
1267 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001268
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001269 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001270}
1271
Greg Clayton97471182012-04-14 01:42:46 +00001272struct ShellInfo
1273{
1274 ShellInfo () :
1275 process_reaped (false),
1276 can_delete (false),
1277 pid (LLDB_INVALID_PROCESS_ID),
1278 signo(-1),
1279 status(-1)
1280 {
1281 }
1282
1283 lldb_private::Predicate<bool> process_reaped;
1284 lldb_private::Predicate<bool> can_delete;
1285 lldb::pid_t pid;
1286 int signo;
1287 int status;
1288};
1289
1290static bool
1291MonitorShellCommand (void *callback_baton,
1292 lldb::pid_t pid,
1293 bool exited, // True if the process did exit
1294 int signo, // Zero for no signal
1295 int status) // Exit value of process if signal is zero
1296{
1297 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1298 shell_info->pid = pid;
1299 shell_info->signo = signo;
1300 shell_info->status = status;
1301 // Let the thread running Host::RunShellCommand() know that the process
1302 // exited and that ShellInfo has been filled in by broadcasting to it
1303 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1304 // Now wait for a handshake back from that thread running Host::RunShellCommand
1305 // so we know that we can delete shell_info_ptr
1306 shell_info->can_delete.WaitForValueEqualTo(true);
1307 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1308 usleep(1000);
1309 // Now delete the shell info that was passed into this function
1310 delete shell_info;
1311 return true;
1312}
1313
1314Error
1315Host::RunShellCommand (const char *command,
1316 const char *working_dir,
1317 int *status_ptr,
1318 int *signo_ptr,
1319 std::string *command_output_ptr,
Greg Claytonb924eb62012-09-27 03:13:55 +00001320 uint32_t timeout_sec,
1321 const char *shell)
Greg Clayton97471182012-04-14 01:42:46 +00001322{
1323 Error error;
1324 ProcessLaunchInfo launch_info;
Greg Claytonb924eb62012-09-27 03:13:55 +00001325 if (shell && shell[0])
1326 {
1327 // Run the command in a shell
1328 launch_info.SetShell(shell);
1329 launch_info.GetArguments().AppendArgument(command);
1330 const bool localhost = true;
1331 const bool will_debug = false;
1332 const bool first_arg_is_full_shell_command = true;
1333 launch_info.ConvertArgumentsForLaunchingInShell (error,
1334 localhost,
1335 will_debug,
1336 first_arg_is_full_shell_command);
1337 }
1338 else
1339 {
1340 // No shell, just run it
1341 Args args (command);
1342 const bool first_arg_is_executable = true;
1343 const bool first_arg_is_executable_and_argument = true;
1344 launch_info.SetArguments(args, first_arg_is_executable, first_arg_is_executable_and_argument);
1345 }
Greg Clayton97471182012-04-14 01:42:46 +00001346
1347 if (working_dir)
1348 launch_info.SetWorkingDirectory(working_dir);
1349 char output_file_path_buffer[L_tmpnam];
1350 const char *output_file_path = NULL;
1351 if (command_output_ptr)
1352 {
1353 // Create a temporary file to get the stdout/stderr and redirect the
1354 // output of the command into this file. We will later read this file
1355 // if all goes well and fill the data into "command_output_ptr"
1356 output_file_path = ::tmpnam(output_file_path_buffer);
1357 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1358 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonb924eb62012-09-27 03:13:55 +00001359 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Clayton97471182012-04-14 01:42:46 +00001360 }
1361 else
1362 {
1363 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1364 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1365 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1366 }
1367
1368 // The process monitor callback will delete the 'shell_info_ptr' below...
1369 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1370
1371 const bool monitor_signals = false;
1372 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1373
1374 error = LaunchProcess (launch_info);
1375 const lldb::pid_t pid = launch_info.GetProcessID();
1376 if (pid != LLDB_INVALID_PROCESS_ID)
1377 {
1378 // The process successfully launched, so we can defer ownership of
1379 // "shell_info" to the MonitorShellCommand callback function that will
1380 // get called when the process dies. We release the std::auto_ptr as it
1381 // doesn't need to delete the ShellInfo anymore.
1382 ShellInfo *shell_info = shell_info_ap.release();
1383 TimeValue timeout_time(TimeValue::Now());
1384 timeout_time.OffsetWithSeconds(timeout_sec);
1385 bool timed_out = false;
1386 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1387 if (timed_out)
1388 {
1389 error.SetErrorString("timed out waiting for shell command to complete");
1390
1391 // Kill the process since it didn't complete withint the timeout specified
1392 ::kill (pid, SIGKILL);
1393 // Wait for the monitor callback to get the message
1394 timeout_time = TimeValue::Now();
1395 timeout_time.OffsetWithSeconds(1);
1396 timed_out = false;
1397 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1398 }
1399 else
1400 {
1401 if (status_ptr)
1402 *status_ptr = shell_info->status;
1403
1404 if (signo_ptr)
1405 *signo_ptr = shell_info->signo;
1406
1407 if (command_output_ptr)
1408 {
1409 command_output_ptr->clear();
1410 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1411 uint64_t file_size = file_spec.GetByteSize();
1412 if (file_size > 0)
1413 {
1414 if (file_size > command_output_ptr->max_size())
1415 {
1416 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1417 }
1418 else
1419 {
1420 command_output_ptr->resize(file_size);
1421 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1422 }
1423 }
1424 }
1425 }
1426 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1427 }
1428 else
1429 {
1430 error.SetErrorString("failed to get process ID");
1431 }
1432
1433 if (output_file_path)
1434 ::unlink (output_file_path);
1435 // Handshake with the monitor thread, or just let it know in advance that
1436 // it can delete "shell_info" in case we timed out and were not able to kill
1437 // the process...
1438 return error;
1439}
1440
1441
1442
Johnny Chen4b663292011-08-02 20:52:42 +00001443#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001444bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001445Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001446{
1447 return false;
1448}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001449
Greg Claytone98ac252010-11-10 04:57:04 +00001450void
1451Host::SetCrashDescriptionWithFormat (const char *format, ...)
1452{
1453}
1454
1455void
1456Host::SetCrashDescription (const char *description)
1457{
1458}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001459
1460lldb::pid_t
1461LaunchApplication (const FileSpec &app_file_spec)
1462{
1463 return LLDB_INVALID_PROCESS_ID;
1464}
1465
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001466#endif