blob: b25f8bcf1b104e217bd4bbf52473dc2899b22383 [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 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000385 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
386 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
387 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000388 }
389 return g_vendor;
390}
391
392const ConstString &
393Host::GetOSString()
394{
395 static ConstString g_os_string;
396 if (!g_os_string)
397 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000398 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
399 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
400 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000401 }
402 return g_os_string;
403}
404
405const ConstString &
406Host::GetTargetTriple()
407{
408 static ConstString g_host_triple;
409 if (!(g_host_triple))
410 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000411 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
412 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000413 }
414 return g_host_triple;
415}
416
417lldb::pid_t
418Host::GetCurrentProcessID()
419{
420 return ::getpid();
421}
422
423lldb::tid_t
424Host::GetCurrentThreadID()
425{
426#if defined (__APPLE__)
Greg Claytone93725b2012-09-18 18:19:49 +0000427 // Calling "mach_port_deallocate()" bumps the reference count on the thread
428 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
429 // count.
430 thread_port_t thread_self = mach_thread_self();
431 mach_port_deallocate(mach_task_self(), thread_self);
432 return thread_self;
Johnny Chen4b663292011-08-02 20:52:42 +0000433#elif defined(__FreeBSD__)
434 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000435#else
436 return lldb::tid_t(pthread_self());
437#endif
438}
439
Jim Ingham1831e782012-04-07 00:00:41 +0000440lldb::thread_t
441Host::GetCurrentThread ()
442{
443 return lldb::thread_t(pthread_self());
444}
445
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000446const char *
447Host::GetSignalAsCString (int signo)
448{
449 switch (signo)
450 {
451 case SIGHUP: return "SIGHUP"; // 1 hangup
452 case SIGINT: return "SIGINT"; // 2 interrupt
453 case SIGQUIT: return "SIGQUIT"; // 3 quit
454 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
455 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
456 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000457#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000458 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000459#endif
460#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000461 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000462#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000463 case SIGFPE: return "SIGFPE"; // 8 floating point exception
464 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
465 case SIGBUS: return "SIGBUS"; // 10 bus error
466 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
467 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
468 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
469 case SIGALRM: return "SIGALRM"; // 14 alarm clock
470 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
471 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
472 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
473 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
474 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
475 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
476 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
477 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
478#if !defined(_POSIX_C_SOURCE)
479 case SIGIO: return "SIGIO"; // 23 input/output possible signal
480#endif
481 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
482 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
483 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
484 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
485#if !defined(_POSIX_C_SOURCE)
486 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
487 case SIGINFO: return "SIGINFO"; // 29 information request
488#endif
489 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
490 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
491 default:
492 break;
493 }
494 return NULL;
495}
496
497void
498Host::WillTerminate ()
499{
500}
501
Johnny Chen4b663292011-08-02 20:52:42 +0000502#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000503void
504Host::ThreadCreated (const char *thread_name)
505{
506}
Greg Claytonb749a262010-12-03 06:02:24 +0000507
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000508void
Greg Claytonb749a262010-12-03 06:02:24 +0000509Host::Backtrace (Stream &strm, uint32_t max_frames)
510{
Greg Clayton52fd9842011-02-02 02:24:04 +0000511 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000512}
513
Greg Clayton638351a2010-12-04 00:10:17 +0000514size_t
515Host::GetEnvironment (StringList &env)
516{
Greg Clayton52fd9842011-02-02 02:24:04 +0000517 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000518 return 0;
519}
520
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000521#endif
522
523struct HostThreadCreateInfo
524{
525 std::string thread_name;
526 thread_func_t thread_fptr;
527 thread_arg_t thread_arg;
528
529 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
530 thread_name (name ? name : ""),
531 thread_fptr (fptr),
532 thread_arg (arg)
533 {
534 }
535};
536
537static thread_result_t
538ThreadCreateTrampoline (thread_arg_t arg)
539{
540 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
541 Host::ThreadCreated (info->thread_name.c_str());
542 thread_func_t thread_fptr = info->thread_fptr;
543 thread_arg_t thread_arg = info->thread_arg;
544
Greg Claytone005f2c2010-11-06 01:53:30 +0000545 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000546 if (log)
547 log->Printf("thread created");
548
549 delete info;
550 return thread_fptr (thread_arg);
551}
552
553lldb::thread_t
554Host::ThreadCreate
555(
556 const char *thread_name,
557 thread_func_t thread_fptr,
558 thread_arg_t thread_arg,
559 Error *error
560)
561{
562 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
563
564 // Host::ThreadCreateTrampoline will delete this pointer for us.
565 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
566
567 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
568 if (err == 0)
569 {
570 if (error)
571 error->Clear();
572 return thread;
573 }
574
575 if (error)
576 error->SetError (err, eErrorTypePOSIX);
577
578 return LLDB_INVALID_HOST_THREAD;
579}
580
581bool
582Host::ThreadCancel (lldb::thread_t thread, Error *error)
583{
584 int err = ::pthread_cancel (thread);
585 if (error)
586 error->SetError(err, eErrorTypePOSIX);
587 return err == 0;
588}
589
590bool
591Host::ThreadDetach (lldb::thread_t thread, Error *error)
592{
593 int err = ::pthread_detach (thread);
594 if (error)
595 error->SetError(err, eErrorTypePOSIX);
596 return err == 0;
597}
598
599bool
600Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
601{
602 int err = ::pthread_join (thread, thread_result_ptr);
603 if (error)
604 error->SetError(err, eErrorTypePOSIX);
605 return err == 0;
606}
607
Jim Ingham35dd4962012-05-04 19:24:49 +0000608// rdar://problem/8153284
609// Fixed a crasher where during shutdown, loggings attempted to access the
610// thread name but the static map instance had already been destructed.
611// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
612// pthread_once action. That map will get leaked.
613//
614// Another approach is to introduce a static guard object which monitors its
615// own destruction and raises a flag, but this incurs more overhead.
616
617static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
618static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
619
620static void
621InitThreadNamesMap()
622{
623 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
624}
625
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000626//------------------------------------------------------------------
627// Control access to a static file thread name map using a single
628// static function to avoid a static constructor.
629//------------------------------------------------------------------
630static const char *
631ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
632{
Jim Ingham35dd4962012-05-04 19:24:49 +0000633 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
634 if (success != 0)
635 return NULL;
636
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000637 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
638
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000639 if (get)
640 {
641 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000642 std::string value;
643 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
644 if (found_it)
645 return value.c_str();
646 else
647 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000648 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000649 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000650 {
651 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000652 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000653 }
654 return NULL;
655}
656
657const char *
658Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
659{
660 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
661 if (name == NULL)
662 {
663#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
664 // We currently can only get the name of a thread in the current process.
665 if (pid == Host::GetCurrentProcessID())
666 {
667 char pthread_name[1024];
668 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
669 {
670 if (pthread_name[0])
671 {
672 // Set the thread in our string pool
673 ThreadNameAccessor (false, pid, tid, pthread_name);
674 // Get our copy of the thread name string
675 name = ThreadNameAccessor (true, pid, tid, NULL);
676 }
677 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000678
679 if (name == NULL)
680 {
681 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
682 if (current_queue != NULL)
683 name = dispatch_queue_get_label (current_queue);
684 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000685 }
686#endif
687 }
688 return name;
689}
690
691void
692Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
693{
694 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
695 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
696 if (pid == LLDB_INVALID_PROCESS_ID)
697 pid = curr_pid;
698
699 if (tid == LLDB_INVALID_THREAD_ID)
700 tid = curr_tid;
701
702#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
703 // Set the pthread name if possible
704 if (pid == curr_pid && tid == curr_tid)
705 {
706 ::pthread_setname_np (name);
707 }
708#endif
709 ThreadNameAccessor (false, pid, tid, name);
710}
711
712FileSpec
713Host::GetProgramFileSpec ()
714{
715 static FileSpec g_program_filespec;
716 if (!g_program_filespec)
717 {
718#if defined (__APPLE__)
719 char program_fullpath[PATH_MAX];
720 // If DST is NULL, then return the number of bytes needed.
721 uint32_t len = sizeof(program_fullpath);
722 int err = _NSGetExecutablePath (program_fullpath, &len);
723 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000724 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000725 else if (err == -1)
726 {
727 char *large_program_fullpath = (char *)::malloc (len + 1);
728
729 err = _NSGetExecutablePath (large_program_fullpath, &len);
730 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000731 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000732
733 ::free (large_program_fullpath);
734 }
735#elif defined (__linux__)
736 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000737 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
738 if (len > 0) {
739 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000740 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000741 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000742#elif defined (__FreeBSD__)
743 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
744 size_t exe_path_size;
745 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
746 {
Greg Clayton366795e2011-01-13 01:27:55 +0000747 char *exe_path = new char[exe_path_size];
748 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
749 g_program_filespec.SetFile(exe_path, false);
750 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000751 }
752#endif
753 }
754 return g_program_filespec;
755}
756
757FileSpec
758Host::GetModuleFileSpecForHostAddress (const void *host_addr)
759{
760 FileSpec module_filespec;
761 Dl_info info;
762 if (::dladdr (host_addr, &info))
763 {
764 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000765 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000766 }
767 return module_filespec;
768}
769
770#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000771
772bool
773Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
774{
775 bundle.Clear();
776 return false;
777}
778
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000779bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000780Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000781{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000782 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000783}
784#endif
785
Greg Clayton14ef59f2011-02-08 00:35:34 +0000786// Opaque info that tracks a dynamic library that was loaded
787struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000788{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000789 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
790 file_spec (fs),
791 open_options (o),
792 handle (h)
793 {
794 }
795
796 const FileSpec file_spec;
797 uint32_t open_options;
798 void * handle;
799};
800
801void *
802Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
803{
Greg Clayton52fd9842011-02-02 02:24:04 +0000804 char path[PATH_MAX];
805 if (file_spec.GetPath(path, sizeof(path)))
806 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000807 int mode = 0;
808
809 if (options & eDynamicLibraryOpenOptionLazy)
810 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000811 else
812 mode |= RTLD_NOW;
813
Greg Clayton14ef59f2011-02-08 00:35:34 +0000814
815 if (options & eDynamicLibraryOpenOptionLocal)
816 mode |= RTLD_LOCAL;
817 else
818 mode |= RTLD_GLOBAL;
819
820#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
821 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
822 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000823#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000824
825 void * opaque = ::dlopen (path, mode);
826
827 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000828 {
829 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000830 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000831 }
832 else
833 {
834 error.SetErrorString(::dlerror());
835 }
836 }
837 else
838 {
839 error.SetErrorString("failed to extract path");
840 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000841 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000842}
843
844Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000845Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000846{
847 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000848 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000849 {
850 error.SetErrorString ("invalid dynamic library handle");
851 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000852 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000853 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000854 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
855 if (::dlclose (dylib_info->handle) != 0)
856 {
857 error.SetErrorString(::dlerror());
858 }
859
860 dylib_info->open_options = 0;
861 dylib_info->handle = 0;
862 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000863 }
864 return error;
865}
866
867void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000868Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000869{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000870 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000871 {
872 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000873 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000874 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000875 {
876 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
877
878 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
879 if (symbol_addr)
880 {
881#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
882 // This host doesn't support limiting searches to this shared library
883 // so we need to verify that the match came from this shared library
884 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000885 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000886 {
887 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
888 if (match_dylib_spec != dylib_info->file_spec)
889 {
890 char dylib_path[PATH_MAX];
891 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
892 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
893 else
894 error.SetErrorString ("symbol not found");
895 return NULL;
896 }
897 }
898#endif
899 error.Clear();
900 return symbol_addr;
901 }
902 else
903 {
904 error.SetErrorString(::dlerror());
905 }
906 }
907 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000908}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000909
910bool
911Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
912{
Greg Clayton5d187e52011-01-08 20:28:42 +0000913 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000914 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
915 // on linux this is assumed to be the "lldb" main executable. If LLDB on
916 // linux is actually in a shared library (lldb.so??) then this function will
917 // need to be modified to "do the right thing".
918
919 switch (path_type)
920 {
921 case ePathTypeLLDBShlibDir:
922 {
923 static ConstString g_lldb_so_dir;
924 if (!g_lldb_so_dir)
925 {
926 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
927 g_lldb_so_dir = lldb_file_spec.GetDirectory();
928 }
929 file_spec.GetDirectory() = g_lldb_so_dir;
930 return file_spec.GetDirectory();
931 }
932 break;
933
934 case ePathTypeSupportExecutableDir:
935 {
936 static ConstString g_lldb_support_exe_dir;
937 if (!g_lldb_support_exe_dir)
938 {
939 FileSpec lldb_file_spec;
940 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
941 {
942 char raw_path[PATH_MAX];
943 char resolved_path[PATH_MAX];
944 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
945
946#if defined (__APPLE__)
947 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
948 if (framework_pos)
949 {
950 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000951#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000952 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000953#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000954 }
955#endif
956 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
957 g_lldb_support_exe_dir.SetCString(resolved_path);
958 }
959 }
960 file_spec.GetDirectory() = g_lldb_support_exe_dir;
961 return file_spec.GetDirectory();
962 }
963 break;
964
965 case ePathTypeHeaderDir:
966 {
967 static ConstString g_lldb_headers_dir;
968 if (!g_lldb_headers_dir)
969 {
970#if defined (__APPLE__)
971 FileSpec lldb_file_spec;
972 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
973 {
974 char raw_path[PATH_MAX];
975 char resolved_path[PATH_MAX];
976 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
977
978 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
979 if (framework_pos)
980 {
981 framework_pos += strlen("LLDB.framework");
982 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
983 }
984 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
985 g_lldb_headers_dir.SetCString(resolved_path);
986 }
987#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000988 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000989 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
990#endif
991 }
992 file_spec.GetDirectory() = g_lldb_headers_dir;
993 return file_spec.GetDirectory();
994 }
995 break;
996
997 case ePathTypePythonDir:
998 {
Greg Clayton52fd9842011-02-02 02:24:04 +0000999 // TODO: Anyone know how we can determine this for linux? Other systems?
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001000 // For linux and FreeBSD we are currently assuming the
1001 // location of the lldb binary that contains this function is
1002 // the directory that will contain a python directory which
1003 // has our lldb module. This is how files get placed when
1004 // compiling with Makefiles.
Greg Clayton24b48ff2010-10-17 22:03:32 +00001005
1006 static ConstString g_lldb_python_dir;
1007 if (!g_lldb_python_dir)
1008 {
1009 FileSpec lldb_file_spec;
1010 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1011 {
1012 char raw_path[PATH_MAX];
1013 char resolved_path[PATH_MAX];
1014 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1015
1016#if defined (__APPLE__)
1017 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1018 if (framework_pos)
1019 {
1020 framework_pos += strlen("LLDB.framework");
1021 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1022 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001023#else
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +00001024 // We may get our string truncated. Should we protect
1025 // this with an assert?
1026 ::strncat(raw_path, "/python", sizeof(raw_path) - strlen(raw_path) - 1);
Greg Clayton24b48ff2010-10-17 22:03:32 +00001027#endif
1028 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1029 g_lldb_python_dir.SetCString(resolved_path);
1030 }
1031 }
1032 file_spec.GetDirectory() = g_lldb_python_dir;
1033 return file_spec.GetDirectory();
1034 }
1035 break;
1036
Greg Clayton52fd9842011-02-02 02:24:04 +00001037 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1038 {
1039#if defined (__APPLE__)
1040 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001041 static bool g_lldb_system_plugin_dir_located = false;
1042 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001043 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001044 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001045 FileSpec lldb_file_spec;
1046 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1047 {
1048 char raw_path[PATH_MAX];
1049 char resolved_path[PATH_MAX];
1050 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1051
1052 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1053 if (framework_pos)
1054 {
1055 framework_pos += strlen("LLDB.framework");
1056 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001057 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1058 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001059 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001060 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001061 }
1062 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001063
1064 if (g_lldb_system_plugin_dir)
1065 {
1066 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1067 return true;
1068 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001069#endif
1070 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1071 return false;
1072 }
1073 break;
1074
1075 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1076 {
1077#if defined (__APPLE__)
1078 static ConstString g_lldb_user_plugin_dir;
1079 if (!g_lldb_user_plugin_dir)
1080 {
1081 char user_plugin_path[PATH_MAX];
1082 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1083 user_plugin_path,
1084 sizeof(user_plugin_path)))
1085 {
1086 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1087 }
1088 }
1089 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1090 return file_spec.GetDirectory();
1091#endif
1092 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1093 return false;
1094 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001095 default:
1096 assert (!"Unhandled PathType");
1097 break;
1098 }
1099
1100 return false;
1101}
1102
Greg Clayton58e26e02011-03-24 04:28:38 +00001103
1104bool
1105Host::GetHostname (std::string &s)
1106{
1107 char hostname[PATH_MAX];
1108 hostname[sizeof(hostname) - 1] = '\0';
1109 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1110 {
1111 struct hostent* h = ::gethostbyname (hostname);
1112 if (h)
1113 s.assign (h->h_name);
1114 else
1115 s.assign (hostname);
1116 return true;
1117 }
1118 return false;
1119}
1120
Greg Clayton24bc5d92011-03-30 18:16:51 +00001121const char *
1122Host::GetUserName (uint32_t uid, std::string &user_name)
1123{
1124 struct passwd user_info;
1125 struct passwd *user_info_ptr = &user_info;
1126 char user_buffer[PATH_MAX];
1127 size_t user_buffer_size = sizeof(user_buffer);
1128 if (::getpwuid_r (uid,
1129 &user_info,
1130 user_buffer,
1131 user_buffer_size,
1132 &user_info_ptr) == 0)
1133 {
1134 if (user_info_ptr)
1135 {
1136 user_name.assign (user_info_ptr->pw_name);
1137 return user_name.c_str();
1138 }
1139 }
1140 user_name.clear();
1141 return NULL;
1142}
1143
1144const char *
1145Host::GetGroupName (uint32_t gid, std::string &group_name)
1146{
1147 char group_buffer[PATH_MAX];
1148 size_t group_buffer_size = sizeof(group_buffer);
1149 struct group group_info;
1150 struct group *group_info_ptr = &group_info;
1151 // Try the threadsafe version first
1152 if (::getgrgid_r (gid,
1153 &group_info,
1154 group_buffer,
1155 group_buffer_size,
1156 &group_info_ptr) == 0)
1157 {
1158 if (group_info_ptr)
1159 {
1160 group_name.assign (group_info_ptr->gr_name);
1161 return group_name.c_str();
1162 }
1163 }
1164 else
1165 {
1166 // The threadsafe version isn't currently working
1167 // for me on darwin, but the non-threadsafe version
1168 // is, so I am calling it below.
1169 group_info_ptr = ::getgrgid (gid);
1170 if (group_info_ptr)
1171 {
1172 group_name.assign (group_info_ptr->gr_name);
1173 return group_name.c_str();
1174 }
1175 }
1176 group_name.clear();
1177 return NULL;
1178}
1179
Johnny Chen4b663292011-08-02 20:52:42 +00001180#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001181bool
1182Host::GetOSBuildString (std::string &s)
1183{
1184 s.clear();
1185 return false;
1186}
1187
1188bool
1189Host::GetOSKernelDescription (std::string &s)
1190{
1191 s.clear();
1192 return false;
1193}
Johnny Chen4b663292011-08-02 20:52:42 +00001194#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001195
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001196uint32_t
1197Host::GetUserID ()
1198{
1199 return getuid();
1200}
1201
1202uint32_t
1203Host::GetGroupID ()
1204{
1205 return getgid();
1206}
1207
1208uint32_t
1209Host::GetEffectiveUserID ()
1210{
1211 return geteuid();
1212}
1213
1214uint32_t
1215Host::GetEffectiveGroupID ()
1216{
1217 return getegid();
1218}
1219
1220#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001221uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001222Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001223{
1224 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001225 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001226}
Johnny Chen4b663292011-08-02 20:52:42 +00001227#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001228
Johnny Chen4b663292011-08-02 20:52:42 +00001229#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001230bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001231Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001232{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001233 process_info.Clear();
1234 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001235}
Johnny Chen4b663292011-08-02 20:52:42 +00001236#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001237
Sean Callananf35a96c2011-10-27 21:22:25 +00001238lldb::TargetSP
1239Host::GetDummyTarget (lldb_private::Debugger &debugger)
1240{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001241 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001242
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001243 // FIXME: Maybe the dummy target should be per-Debugger
1244 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1245 {
1246 ArchSpec arch(Target::GetDefaultArchitecture());
1247 if (!arch.IsValid())
1248 arch = Host::GetArchitecture ();
1249 Error err = debugger.GetTargetList().CreateTarget(debugger,
1250 FileSpec(),
1251 arch.GetTriple().getTriple().c_str(),
1252 false,
1253 NULL,
1254 g_dummy_target_sp);
1255 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001256
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001257 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001258}
1259
Greg Clayton97471182012-04-14 01:42:46 +00001260struct ShellInfo
1261{
1262 ShellInfo () :
1263 process_reaped (false),
1264 can_delete (false),
1265 pid (LLDB_INVALID_PROCESS_ID),
1266 signo(-1),
1267 status(-1)
1268 {
1269 }
1270
1271 lldb_private::Predicate<bool> process_reaped;
1272 lldb_private::Predicate<bool> can_delete;
1273 lldb::pid_t pid;
1274 int signo;
1275 int status;
1276};
1277
1278static bool
1279MonitorShellCommand (void *callback_baton,
1280 lldb::pid_t pid,
1281 bool exited, // True if the process did exit
1282 int signo, // Zero for no signal
1283 int status) // Exit value of process if signal is zero
1284{
1285 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1286 shell_info->pid = pid;
1287 shell_info->signo = signo;
1288 shell_info->status = status;
1289 // Let the thread running Host::RunShellCommand() know that the process
1290 // exited and that ShellInfo has been filled in by broadcasting to it
1291 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1292 // Now wait for a handshake back from that thread running Host::RunShellCommand
1293 // so we know that we can delete shell_info_ptr
1294 shell_info->can_delete.WaitForValueEqualTo(true);
1295 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1296 usleep(1000);
1297 // Now delete the shell info that was passed into this function
1298 delete shell_info;
1299 return true;
1300}
1301
1302Error
1303Host::RunShellCommand (const char *command,
1304 const char *working_dir,
1305 int *status_ptr,
1306 int *signo_ptr,
1307 std::string *command_output_ptr,
Greg Claytonb924eb62012-09-27 03:13:55 +00001308 uint32_t timeout_sec,
1309 const char *shell)
Greg Clayton97471182012-04-14 01:42:46 +00001310{
1311 Error error;
1312 ProcessLaunchInfo launch_info;
Greg Claytonb924eb62012-09-27 03:13:55 +00001313 if (shell && shell[0])
1314 {
1315 // Run the command in a shell
1316 launch_info.SetShell(shell);
1317 launch_info.GetArguments().AppendArgument(command);
1318 const bool localhost = true;
1319 const bool will_debug = false;
1320 const bool first_arg_is_full_shell_command = true;
1321 launch_info.ConvertArgumentsForLaunchingInShell (error,
1322 localhost,
1323 will_debug,
1324 first_arg_is_full_shell_command);
1325 }
1326 else
1327 {
1328 // No shell, just run it
1329 Args args (command);
1330 const bool first_arg_is_executable = true;
Greg Clayton0c8446c2012-10-17 22:57:12 +00001331 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonb924eb62012-09-27 03:13:55 +00001332 }
Greg Clayton97471182012-04-14 01:42:46 +00001333
1334 if (working_dir)
1335 launch_info.SetWorkingDirectory(working_dir);
1336 char output_file_path_buffer[L_tmpnam];
1337 const char *output_file_path = NULL;
1338 if (command_output_ptr)
1339 {
1340 // Create a temporary file to get the stdout/stderr and redirect the
1341 // output of the command into this file. We will later read this file
1342 // if all goes well and fill the data into "command_output_ptr"
1343 output_file_path = ::tmpnam(output_file_path_buffer);
1344 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1345 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonb924eb62012-09-27 03:13:55 +00001346 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Clayton97471182012-04-14 01:42:46 +00001347 }
1348 else
1349 {
1350 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1351 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1352 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1353 }
1354
1355 // The process monitor callback will delete the 'shell_info_ptr' below...
1356 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1357
1358 const bool monitor_signals = false;
1359 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1360
1361 error = LaunchProcess (launch_info);
1362 const lldb::pid_t pid = launch_info.GetProcessID();
1363 if (pid != LLDB_INVALID_PROCESS_ID)
1364 {
1365 // The process successfully launched, so we can defer ownership of
1366 // "shell_info" to the MonitorShellCommand callback function that will
1367 // get called when the process dies. We release the std::auto_ptr as it
1368 // doesn't need to delete the ShellInfo anymore.
1369 ShellInfo *shell_info = shell_info_ap.release();
1370 TimeValue timeout_time(TimeValue::Now());
1371 timeout_time.OffsetWithSeconds(timeout_sec);
1372 bool timed_out = false;
1373 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1374 if (timed_out)
1375 {
1376 error.SetErrorString("timed out waiting for shell command to complete");
1377
1378 // Kill the process since it didn't complete withint the timeout specified
1379 ::kill (pid, SIGKILL);
1380 // Wait for the monitor callback to get the message
1381 timeout_time = TimeValue::Now();
1382 timeout_time.OffsetWithSeconds(1);
1383 timed_out = false;
1384 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1385 }
1386 else
1387 {
1388 if (status_ptr)
1389 *status_ptr = shell_info->status;
1390
1391 if (signo_ptr)
1392 *signo_ptr = shell_info->signo;
1393
1394 if (command_output_ptr)
1395 {
1396 command_output_ptr->clear();
1397 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1398 uint64_t file_size = file_spec.GetByteSize();
1399 if (file_size > 0)
1400 {
1401 if (file_size > command_output_ptr->max_size())
1402 {
1403 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1404 }
1405 else
1406 {
1407 command_output_ptr->resize(file_size);
1408 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1409 }
1410 }
1411 }
1412 }
1413 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1414 }
1415 else
1416 {
1417 error.SetErrorString("failed to get process ID");
1418 }
1419
1420 if (output_file_path)
1421 ::unlink (output_file_path);
1422 // Handshake with the monitor thread, or just let it know in advance that
1423 // it can delete "shell_info" in case we timed out and were not able to kill
1424 // the process...
1425 return error;
1426}
1427
1428
1429
Johnny Chen4b663292011-08-02 20:52:42 +00001430#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001431bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001432Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001433{
1434 return false;
1435}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001436
Greg Claytone98ac252010-11-10 04:57:04 +00001437void
1438Host::SetCrashDescriptionWithFormat (const char *format, ...)
1439{
1440}
1441
1442void
1443Host::SetCrashDescription (const char *description)
1444{
1445}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001446
1447lldb::pid_t
1448LaunchApplication (const FileSpec &app_file_spec)
1449{
1450 return LLDB_INVALID_PROCESS_ID;
1451}
1452
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001453#endif