blob: 38cd75f0281e80619335ba0eb9b730107b549491 [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 Claytonb5f67fb2011-02-05 06:36:35 +000042#include <sys/sysctl.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000043
Greg Clayton24bc5d92011-03-30 18:16:51 +000044
Greg Clayton0f577c22011-02-07 17:43:47 +000045#elif defined (__linux__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000046
Greg Clayton0f577c22011-02-07 17:43:47 +000047#include <sys/wait.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000048
Johnny Chen4b663292011-08-02 20:52:42 +000049#elif defined (__FreeBSD__)
50
51#include <sys/wait.h>
52#include <sys/sysctl.h>
53#include <pthread_np.h>
54
Greg Clayton8f3b21d2010-09-07 20:11:56 +000055#endif
56
57using namespace lldb;
58using namespace lldb_private;
59
Greg Clayton1c4642c2011-11-16 05:37:56 +000060
Greg Claytonc518fe72011-11-17 19:41:57 +000061#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +000062struct MonitorInfo
63{
64 lldb::pid_t pid; // The process ID to monitor
65 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
66 void *callback_baton; // The callback baton for the callback function
67 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
68};
69
70static void *
71MonitorChildProcessThreadFunction (void *arg);
72
73lldb::thread_t
74Host::StartMonitoringChildProcess
75(
76 Host::MonitorChildProcessCallback callback,
77 void *callback_baton,
78 lldb::pid_t pid,
79 bool monitor_signals
80)
81{
82 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton1c4642c2011-11-16 05:37:56 +000083 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton8f3b21d2010-09-07 20:11:56 +000084
Greg Clayton1c4642c2011-11-16 05:37:56 +000085 info_ptr->pid = pid;
86 info_ptr->callback = callback;
87 info_ptr->callback_baton = callback_baton;
88 info_ptr->monitor_signals = monitor_signals;
89
90 char thread_name[256];
91 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid);
92 thread = ThreadCreate (thread_name,
93 MonitorChildProcessThreadFunction,
94 info_ptr,
95 NULL);
96
Greg Clayton8f3b21d2010-09-07 20:11:56 +000097 return thread;
98}
99
100//------------------------------------------------------------------
101// Scoped class that will disable thread canceling when it is
102// constructed, and exception safely restore the previous value it
103// when it goes out of scope.
104//------------------------------------------------------------------
105class ScopedPThreadCancelDisabler
106{
107public:
108 ScopedPThreadCancelDisabler()
109 {
110 // Disable the ability for this thread to be cancelled
111 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
112 if (err != 0)
113 m_old_state = -1;
114
115 }
116
117 ~ScopedPThreadCancelDisabler()
118 {
119 // Restore the ability for this thread to be cancelled to what it
120 // previously was.
121 if (m_old_state != -1)
122 ::pthread_setcancelstate (m_old_state, 0);
123 }
124private:
125 int m_old_state; // Save the old cancelability state.
126};
127
128static void *
129MonitorChildProcessThreadFunction (void *arg)
130{
Greg Claytone005f2c2010-11-06 01:53:30 +0000131 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000132 const char *function = __FUNCTION__;
133 if (log)
134 log->Printf ("%s (arg = %p) thread starting...", function, arg);
135
136 MonitorInfo *info = (MonitorInfo *)arg;
137
138 const Host::MonitorChildProcessCallback callback = info->callback;
139 void * const callback_baton = info->callback_baton;
140 const lldb::pid_t pid = info->pid;
141 const bool monitor_signals = info->monitor_signals;
142
143 delete info;
144
145 int status = -1;
146 const int options = 0;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000147 while (1)
148 {
Caroline Tice926060e2010-10-29 21:48:37 +0000149 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000150 if (log)
Greg Clayton1c4642c2011-11-16 05:37:56 +0000151 log->Printf("%s ::wait_pid (pid = %i, &status, options = %i)...", function, pid, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000152
153 // Wait for all child processes
154 ::pthread_testcancel ();
Greg Clayton1c4642c2011-11-16 05:37:56 +0000155 const lldb::pid_t wait_pid = ::waitpid (pid, &status, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000156 ::pthread_testcancel ();
157
158 if (wait_pid == -1)
159 {
160 if (errno == EINTR)
161 continue;
162 else
163 break;
164 }
165 else if (wait_pid == pid)
166 {
167 bool exited = false;
168 int signal = 0;
169 int exit_status = 0;
170 const char *status_cstr = NULL;
171 if (WIFSTOPPED(status))
172 {
173 signal = WSTOPSIG(status);
174 status_cstr = "STOPPED";
175 }
176 else if (WIFEXITED(status))
177 {
178 exit_status = WEXITSTATUS(status);
179 status_cstr = "EXITED";
180 exited = true;
181 }
182 else if (WIFSIGNALED(status))
183 {
184 signal = WTERMSIG(status);
185 status_cstr = "SIGNALED";
186 exited = true;
187 exit_status = -1;
188 }
189 else
190 {
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000191 status_cstr = "(\?\?\?)";
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000192 }
193
194 // Scope for pthread_cancel_disabler
195 {
196 ScopedPThreadCancelDisabler pthread_cancel_disabler;
197
Caroline Tice926060e2010-10-29 21:48:37 +0000198 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000199 if (log)
Greg Clayton1c4642c2011-11-16 05:37:56 +0000200 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 +0000201 function,
202 wait_pid,
203 options,
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000204 pid,
205 status,
206 status_cstr,
207 signal,
208 exit_status);
209
210 if (exited || (signal != 0 && monitor_signals))
211 {
Greg Clayton1c4642c2011-11-16 05:37:56 +0000212 bool callback_return = false;
213 if (callback)
214 callback_return = callback (callback_baton, pid, exited, signal, exit_status);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000215
216 // If our process exited, then this thread should exit
217 if (exited)
218 break;
219 // If the callback returns true, it means this process should
220 // exit
221 if (callback_return)
222 break;
223 }
224 }
225 }
226 }
227
Caroline Tice926060e2010-10-29 21:48:37 +0000228 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000229 if (log)
230 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
231
232 return NULL;
233}
234
Greg Claytondf6dc882012-01-05 03:57:59 +0000235
236void
237Host::SystemLog (SystemLogType type, const char *format, va_list args)
238{
239 vfprintf (stderr, format, args);
240}
241
Greg Clayton1c4642c2011-11-16 05:37:56 +0000242#endif // #if !defined (__APPLE__)
243
Greg Claytondf6dc882012-01-05 03:57:59 +0000244void
245Host::SystemLog (SystemLogType type, const char *format, ...)
246{
247 va_list args;
248 va_start (args, format);
249 SystemLog (type, format, args);
250 va_end (args);
251}
252
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000253size_t
254Host::GetPageSize()
255{
256 return ::getpagesize();
257}
258
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000259const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000260Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000261{
Greg Clayton395fc332011-02-15 21:59:32 +0000262 static bool g_supports_32 = false;
263 static bool g_supports_64 = false;
264 static ArchSpec g_host_arch_32;
265 static ArchSpec g_host_arch_64;
266
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000267#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000268
269 // Apple is different in that it can support both 32 and 64 bit executables
270 // in the same operating system running concurrently. Here we detect the
271 // correct host architectures for both 32 and 64 bit including if 64 bit
272 // executables are supported on the system.
273
274 if (g_supports_32 == false && g_supports_64 == false)
275 {
276 // All apple systems support 32 bit execution.
277 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000278 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000279 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000280 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000281 ArchSpec host_arch;
282 // These will tell us about the kernel architecture, which even on a 64
283 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000284 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
285 {
Greg Clayton395fc332011-02-15 21:59:32 +0000286 len = sizeof (cpusubtype);
287 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
288 cpusubtype = CPU_TYPE_ANY;
289
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000290 len = sizeof (is_64_bit_capable);
291 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
292 {
293 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000294 g_supports_64 = true;
295 }
296
297 if (is_64_bit_capable)
298 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000299#if defined (__i386__) || defined (__x86_64__)
300 if (cpusubtype == CPU_SUBTYPE_486)
301 cpusubtype = CPU_SUBTYPE_I386_ALL;
302#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000303 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000304 {
Greg Clayton395fc332011-02-15 21:59:32 +0000305 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000306 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
307 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000308 }
309 else
310 {
311 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000312 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000313 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000314 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000315 }
316 }
Greg Clayton395fc332011-02-15 21:59:32 +0000317 else
318 {
Greg Claytonb3448432011-03-24 21:19:54 +0000319 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000320 g_host_arch_64.Clear();
321 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000322 }
Greg Clayton395fc332011-02-15 21:59:32 +0000323 }
324
325#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000326
Greg Clayton395fc332011-02-15 21:59:32 +0000327 if (g_supports_32 == false && g_supports_64 == false)
328 {
Peter Collingbourneb47c9982011-11-05 01:35:31 +0000329 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000330
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000331 g_host_arch_32.Clear();
332 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000333
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000334 switch (triple.getArch())
335 {
336 default:
337 g_host_arch_32.SetTriple(triple);
338 g_supports_32 = true;
339 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000340
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000341 case llvm::Triple::x86_64:
342 case llvm::Triple::sparcv9:
343 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000344 case llvm::Triple::cellspu:
345 g_host_arch_64.SetTriple(triple);
346 g_supports_64 = true;
347 break;
348 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000349
350 g_supports_32 = g_host_arch_32.IsValid();
351 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000352 }
Greg Clayton395fc332011-02-15 21:59:32 +0000353
354#endif // #else for #if defined (__APPLE__)
355
356 if (arch_kind == eSystemDefaultArchitecture32)
357 return g_host_arch_32;
358 else if (arch_kind == eSystemDefaultArchitecture64)
359 return g_host_arch_64;
360
361 if (g_supports_64)
362 return g_host_arch_64;
363
364 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000365}
366
367const ConstString &
368Host::GetVendorString()
369{
370 static ConstString g_vendor;
371 if (!g_vendor)
372 {
373#if defined (__APPLE__)
374 char ostype[64];
375 size_t len = sizeof(ostype);
376 if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0)
377 g_vendor.SetCString (ostype);
378 else
379 g_vendor.SetCString("apple");
380#elif defined (__linux__)
381 g_vendor.SetCString("gnu");
Johnny Chen4b663292011-08-02 20:52:42 +0000382#elif defined (__FreeBSD__)
383 g_vendor.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000384#endif
385 }
386 return g_vendor;
387}
388
389const ConstString &
390Host::GetOSString()
391{
392 static ConstString g_os_string;
393 if (!g_os_string)
394 {
395#if defined (__APPLE__)
396 g_os_string.SetCString("darwin");
397#elif defined (__linux__)
398 g_os_string.SetCString("linux");
Johnny Chen4b663292011-08-02 20:52:42 +0000399#elif defined (__FreeBSD__)
400 g_os_string.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000401#endif
402 }
403 return g_os_string;
404}
405
406const ConstString &
407Host::GetTargetTriple()
408{
409 static ConstString g_host_triple;
410 if (!(g_host_triple))
411 {
412 StreamString triple;
413 triple.Printf("%s-%s-%s",
Greg Clayton940b1032011-02-23 00:35:02 +0000414 GetArchitecture().GetArchitectureName(),
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000415 GetVendorString().AsCString(),
416 GetOSString().AsCString());
417
418 std::transform (triple.GetString().begin(),
419 triple.GetString().end(),
420 triple.GetString().begin(),
421 ::tolower);
422
423 g_host_triple.SetCString(triple.GetString().c_str());
424 }
425 return g_host_triple;
426}
427
428lldb::pid_t
429Host::GetCurrentProcessID()
430{
431 return ::getpid();
432}
433
434lldb::tid_t
435Host::GetCurrentThreadID()
436{
437#if defined (__APPLE__)
438 return ::mach_thread_self();
Johnny Chen4b663292011-08-02 20:52:42 +0000439#elif defined(__FreeBSD__)
440 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000441#else
442 return lldb::tid_t(pthread_self());
443#endif
444}
445
Jim Ingham1831e782012-04-07 00:00:41 +0000446lldb::thread_t
447Host::GetCurrentThread ()
448{
449 return lldb::thread_t(pthread_self());
450}
451
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000452const char *
453Host::GetSignalAsCString (int signo)
454{
455 switch (signo)
456 {
457 case SIGHUP: return "SIGHUP"; // 1 hangup
458 case SIGINT: return "SIGINT"; // 2 interrupt
459 case SIGQUIT: return "SIGQUIT"; // 3 quit
460 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
461 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
462 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000463#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000464 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000465#endif
466#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000467 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000468#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000469 case SIGFPE: return "SIGFPE"; // 8 floating point exception
470 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
471 case SIGBUS: return "SIGBUS"; // 10 bus error
472 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
473 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
474 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
475 case SIGALRM: return "SIGALRM"; // 14 alarm clock
476 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
477 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
478 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
479 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
480 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
481 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
482 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
483 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
484#if !defined(_POSIX_C_SOURCE)
485 case SIGIO: return "SIGIO"; // 23 input/output possible signal
486#endif
487 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
488 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
489 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
490 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
491#if !defined(_POSIX_C_SOURCE)
492 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
493 case SIGINFO: return "SIGINFO"; // 29 information request
494#endif
495 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
496 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
497 default:
498 break;
499 }
500 return NULL;
501}
502
503void
504Host::WillTerminate ()
505{
506}
507
Johnny Chen4b663292011-08-02 20:52:42 +0000508#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000509void
510Host::ThreadCreated (const char *thread_name)
511{
512}
Greg Claytonb749a262010-12-03 06:02:24 +0000513
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000514void
Greg Claytonb749a262010-12-03 06:02:24 +0000515Host::Backtrace (Stream &strm, uint32_t max_frames)
516{
Greg Clayton52fd9842011-02-02 02:24:04 +0000517 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000518}
519
Greg Clayton638351a2010-12-04 00:10:17 +0000520size_t
521Host::GetEnvironment (StringList &env)
522{
Greg Clayton52fd9842011-02-02 02:24:04 +0000523 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000524 return 0;
525}
526
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000527#endif
528
529struct HostThreadCreateInfo
530{
531 std::string thread_name;
532 thread_func_t thread_fptr;
533 thread_arg_t thread_arg;
534
535 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
536 thread_name (name ? name : ""),
537 thread_fptr (fptr),
538 thread_arg (arg)
539 {
540 }
541};
542
543static thread_result_t
544ThreadCreateTrampoline (thread_arg_t arg)
545{
546 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
547 Host::ThreadCreated (info->thread_name.c_str());
548 thread_func_t thread_fptr = info->thread_fptr;
549 thread_arg_t thread_arg = info->thread_arg;
550
Greg Claytone005f2c2010-11-06 01:53:30 +0000551 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000552 if (log)
553 log->Printf("thread created");
554
555 delete info;
556 return thread_fptr (thread_arg);
557}
558
559lldb::thread_t
560Host::ThreadCreate
561(
562 const char *thread_name,
563 thread_func_t thread_fptr,
564 thread_arg_t thread_arg,
565 Error *error
566)
567{
568 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
569
570 // Host::ThreadCreateTrampoline will delete this pointer for us.
571 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
572
573 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
574 if (err == 0)
575 {
576 if (error)
577 error->Clear();
578 return thread;
579 }
580
581 if (error)
582 error->SetError (err, eErrorTypePOSIX);
583
584 return LLDB_INVALID_HOST_THREAD;
585}
586
587bool
588Host::ThreadCancel (lldb::thread_t thread, Error *error)
589{
590 int err = ::pthread_cancel (thread);
591 if (error)
592 error->SetError(err, eErrorTypePOSIX);
593 return err == 0;
594}
595
596bool
597Host::ThreadDetach (lldb::thread_t thread, Error *error)
598{
599 int err = ::pthread_detach (thread);
600 if (error)
601 error->SetError(err, eErrorTypePOSIX);
602 return err == 0;
603}
604
605bool
606Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
607{
608 int err = ::pthread_join (thread, thread_result_ptr);
609 if (error)
610 error->SetError(err, eErrorTypePOSIX);
611 return err == 0;
612}
613
Jim Ingham35dd4962012-05-04 19:24:49 +0000614// rdar://problem/8153284
615// Fixed a crasher where during shutdown, loggings attempted to access the
616// thread name but the static map instance had already been destructed.
617// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
618// pthread_once action. That map will get leaked.
619//
620// Another approach is to introduce a static guard object which monitors its
621// own destruction and raises a flag, but this incurs more overhead.
622
623static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
624static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
625
626static void
627InitThreadNamesMap()
628{
629 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
630}
631
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000632//------------------------------------------------------------------
633// Control access to a static file thread name map using a single
634// static function to avoid a static constructor.
635//------------------------------------------------------------------
636static const char *
637ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
638{
Jim Ingham35dd4962012-05-04 19:24:49 +0000639 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
640 if (success != 0)
641 return NULL;
642
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000643 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
644
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000645 if (get)
646 {
647 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000648 std::string value;
649 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
650 if (found_it)
651 return value.c_str();
652 else
653 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000654 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000655 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000656 {
657 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000658 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000659 }
660 return NULL;
661}
662
663const char *
664Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
665{
666 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
667 if (name == NULL)
668 {
669#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
670 // We currently can only get the name of a thread in the current process.
671 if (pid == Host::GetCurrentProcessID())
672 {
673 char pthread_name[1024];
674 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
675 {
676 if (pthread_name[0])
677 {
678 // Set the thread in our string pool
679 ThreadNameAccessor (false, pid, tid, pthread_name);
680 // Get our copy of the thread name string
681 name = ThreadNameAccessor (true, pid, tid, NULL);
682 }
683 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000684
685 if (name == NULL)
686 {
687 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
688 if (current_queue != NULL)
689 name = dispatch_queue_get_label (current_queue);
690 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000691 }
692#endif
693 }
694 return name;
695}
696
697void
698Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
699{
700 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
701 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
702 if (pid == LLDB_INVALID_PROCESS_ID)
703 pid = curr_pid;
704
705 if (tid == LLDB_INVALID_THREAD_ID)
706 tid = curr_tid;
707
708#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
709 // Set the pthread name if possible
710 if (pid == curr_pid && tid == curr_tid)
711 {
712 ::pthread_setname_np (name);
713 }
714#endif
715 ThreadNameAccessor (false, pid, tid, name);
716}
717
718FileSpec
719Host::GetProgramFileSpec ()
720{
721 static FileSpec g_program_filespec;
722 if (!g_program_filespec)
723 {
724#if defined (__APPLE__)
725 char program_fullpath[PATH_MAX];
726 // If DST is NULL, then return the number of bytes needed.
727 uint32_t len = sizeof(program_fullpath);
728 int err = _NSGetExecutablePath (program_fullpath, &len);
729 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000730 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000731 else if (err == -1)
732 {
733 char *large_program_fullpath = (char *)::malloc (len + 1);
734
735 err = _NSGetExecutablePath (large_program_fullpath, &len);
736 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000737 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000738
739 ::free (large_program_fullpath);
740 }
741#elif defined (__linux__)
742 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000743 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
744 if (len > 0) {
745 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000746 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000747 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000748#elif defined (__FreeBSD__)
749 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
750 size_t exe_path_size;
751 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
752 {
Greg Clayton366795e2011-01-13 01:27:55 +0000753 char *exe_path = new char[exe_path_size];
754 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
755 g_program_filespec.SetFile(exe_path, false);
756 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000757 }
758#endif
759 }
760 return g_program_filespec;
761}
762
763FileSpec
764Host::GetModuleFileSpecForHostAddress (const void *host_addr)
765{
766 FileSpec module_filespec;
767 Dl_info info;
768 if (::dladdr (host_addr, &info))
769 {
770 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000771 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000772 }
773 return module_filespec;
774}
775
776#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000777
778bool
779Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
780{
781 bundle.Clear();
782 return false;
783}
784
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000785bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000786Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000787{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000788 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000789}
790#endif
791
Greg Clayton14ef59f2011-02-08 00:35:34 +0000792// Opaque info that tracks a dynamic library that was loaded
793struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000794{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000795 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
796 file_spec (fs),
797 open_options (o),
798 handle (h)
799 {
800 }
801
802 const FileSpec file_spec;
803 uint32_t open_options;
804 void * handle;
805};
806
807void *
808Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
809{
Greg Clayton52fd9842011-02-02 02:24:04 +0000810 char path[PATH_MAX];
811 if (file_spec.GetPath(path, sizeof(path)))
812 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000813 int mode = 0;
814
815 if (options & eDynamicLibraryOpenOptionLazy)
816 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000817 else
818 mode |= RTLD_NOW;
819
Greg Clayton14ef59f2011-02-08 00:35:34 +0000820
821 if (options & eDynamicLibraryOpenOptionLocal)
822 mode |= RTLD_LOCAL;
823 else
824 mode |= RTLD_GLOBAL;
825
826#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
827 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
828 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000829#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000830
831 void * opaque = ::dlopen (path, mode);
832
833 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000834 {
835 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000836 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000837 }
838 else
839 {
840 error.SetErrorString(::dlerror());
841 }
842 }
843 else
844 {
845 error.SetErrorString("failed to extract path");
846 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000847 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000848}
849
850Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000851Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000852{
853 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000854 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000855 {
856 error.SetErrorString ("invalid dynamic library handle");
857 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000858 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000859 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000860 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
861 if (::dlclose (dylib_info->handle) != 0)
862 {
863 error.SetErrorString(::dlerror());
864 }
865
866 dylib_info->open_options = 0;
867 dylib_info->handle = 0;
868 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000869 }
870 return error;
871}
872
873void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000874Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000875{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000876 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000877 {
878 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000879 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000880 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000881 {
882 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
883
884 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
885 if (symbol_addr)
886 {
887#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
888 // This host doesn't support limiting searches to this shared library
889 // so we need to verify that the match came from this shared library
890 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000891 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000892 {
893 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
894 if (match_dylib_spec != dylib_info->file_spec)
895 {
896 char dylib_path[PATH_MAX];
897 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
898 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
899 else
900 error.SetErrorString ("symbol not found");
901 return NULL;
902 }
903 }
904#endif
905 error.Clear();
906 return symbol_addr;
907 }
908 else
909 {
910 error.SetErrorString(::dlerror());
911 }
912 }
913 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000914}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000915
916bool
917Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
918{
Greg Clayton5d187e52011-01-08 20:28:42 +0000919 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000920 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
921 // on linux this is assumed to be the "lldb" main executable. If LLDB on
922 // linux is actually in a shared library (lldb.so??) then this function will
923 // need to be modified to "do the right thing".
924
925 switch (path_type)
926 {
927 case ePathTypeLLDBShlibDir:
928 {
929 static ConstString g_lldb_so_dir;
930 if (!g_lldb_so_dir)
931 {
932 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
933 g_lldb_so_dir = lldb_file_spec.GetDirectory();
934 }
935 file_spec.GetDirectory() = g_lldb_so_dir;
936 return file_spec.GetDirectory();
937 }
938 break;
939
940 case ePathTypeSupportExecutableDir:
941 {
942 static ConstString g_lldb_support_exe_dir;
943 if (!g_lldb_support_exe_dir)
944 {
945 FileSpec lldb_file_spec;
946 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
947 {
948 char raw_path[PATH_MAX];
949 char resolved_path[PATH_MAX];
950 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
951
952#if defined (__APPLE__)
953 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
954 if (framework_pos)
955 {
956 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000957#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000958 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000959#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000960 }
961#endif
962 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
963 g_lldb_support_exe_dir.SetCString(resolved_path);
964 }
965 }
966 file_spec.GetDirectory() = g_lldb_support_exe_dir;
967 return file_spec.GetDirectory();
968 }
969 break;
970
971 case ePathTypeHeaderDir:
972 {
973 static ConstString g_lldb_headers_dir;
974 if (!g_lldb_headers_dir)
975 {
976#if defined (__APPLE__)
977 FileSpec lldb_file_spec;
978 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
979 {
980 char raw_path[PATH_MAX];
981 char resolved_path[PATH_MAX];
982 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
983
984 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
985 if (framework_pos)
986 {
987 framework_pos += strlen("LLDB.framework");
988 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
989 }
990 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
991 g_lldb_headers_dir.SetCString(resolved_path);
992 }
993#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000994 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000995 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
996#endif
997 }
998 file_spec.GetDirectory() = g_lldb_headers_dir;
999 return file_spec.GetDirectory();
1000 }
1001 break;
1002
1003 case ePathTypePythonDir:
1004 {
Greg Clayton52fd9842011-02-02 02:24:04 +00001005 // TODO: Anyone know how we can determine this for linux? Other systems?
Greg Clayton24b48ff2010-10-17 22:03:32 +00001006 // For linux we are currently assuming the location of the lldb
1007 // binary that contains this function is the directory that will
1008 // contain lldb.so, lldb.py and embedded_interpreter.py...
1009
1010 static ConstString g_lldb_python_dir;
1011 if (!g_lldb_python_dir)
1012 {
1013 FileSpec lldb_file_spec;
1014 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1015 {
1016 char raw_path[PATH_MAX];
1017 char resolved_path[PATH_MAX];
1018 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1019
1020#if defined (__APPLE__)
1021 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1022 if (framework_pos)
1023 {
1024 framework_pos += strlen("LLDB.framework");
1025 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1026 }
1027#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{
1241 static TargetSP dummy_target;
1242
1243 if (!dummy_target)
1244 {
1245 Error err = debugger.GetTargetList().CreateTarget(debugger,
1246 FileSpec(),
1247 Host::GetTargetTriple().AsCString(),
1248 false,
1249 NULL,
1250 dummy_target);
1251 }
1252
1253 return dummy_target;
1254}
1255
Greg Clayton97471182012-04-14 01:42:46 +00001256struct ShellInfo
1257{
1258 ShellInfo () :
1259 process_reaped (false),
1260 can_delete (false),
1261 pid (LLDB_INVALID_PROCESS_ID),
1262 signo(-1),
1263 status(-1)
1264 {
1265 }
1266
1267 lldb_private::Predicate<bool> process_reaped;
1268 lldb_private::Predicate<bool> can_delete;
1269 lldb::pid_t pid;
1270 int signo;
1271 int status;
1272};
1273
1274static bool
1275MonitorShellCommand (void *callback_baton,
1276 lldb::pid_t pid,
1277 bool exited, // True if the process did exit
1278 int signo, // Zero for no signal
1279 int status) // Exit value of process if signal is zero
1280{
1281 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1282 shell_info->pid = pid;
1283 shell_info->signo = signo;
1284 shell_info->status = status;
1285 // Let the thread running Host::RunShellCommand() know that the process
1286 // exited and that ShellInfo has been filled in by broadcasting to it
1287 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1288 // Now wait for a handshake back from that thread running Host::RunShellCommand
1289 // so we know that we can delete shell_info_ptr
1290 shell_info->can_delete.WaitForValueEqualTo(true);
1291 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1292 usleep(1000);
1293 // Now delete the shell info that was passed into this function
1294 delete shell_info;
1295 return true;
1296}
1297
1298Error
1299Host::RunShellCommand (const char *command,
1300 const char *working_dir,
1301 int *status_ptr,
1302 int *signo_ptr,
1303 std::string *command_output_ptr,
1304 uint32_t timeout_sec)
1305{
1306 Error error;
1307 ProcessLaunchInfo launch_info;
1308 launch_info.SetShell("/bin/bash");
1309 launch_info.GetArguments().AppendArgument(command);
1310 const bool localhost = true;
1311 const bool will_debug = false;
1312 const bool first_arg_is_full_shell_command = true;
1313 launch_info.ConvertArgumentsForLaunchingInShell (error,
1314 localhost,
1315 will_debug,
1316 first_arg_is_full_shell_command);
1317
1318 if (working_dir)
1319 launch_info.SetWorkingDirectory(working_dir);
1320 char output_file_path_buffer[L_tmpnam];
1321 const char *output_file_path = NULL;
1322 if (command_output_ptr)
1323 {
1324 // Create a temporary file to get the stdout/stderr and redirect the
1325 // output of the command into this file. We will later read this file
1326 // if all goes well and fill the data into "command_output_ptr"
1327 output_file_path = ::tmpnam(output_file_path_buffer);
1328 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1329 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
1330 launch_info.AppendDuplicateFileAction(STDERR_FILENO, STDOUT_FILENO);
1331 }
1332 else
1333 {
1334 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1335 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1336 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1337 }
1338
1339 // The process monitor callback will delete the 'shell_info_ptr' below...
1340 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1341
1342 const bool monitor_signals = false;
1343 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1344
1345 error = LaunchProcess (launch_info);
1346 const lldb::pid_t pid = launch_info.GetProcessID();
1347 if (pid != LLDB_INVALID_PROCESS_ID)
1348 {
1349 // The process successfully launched, so we can defer ownership of
1350 // "shell_info" to the MonitorShellCommand callback function that will
1351 // get called when the process dies. We release the std::auto_ptr as it
1352 // doesn't need to delete the ShellInfo anymore.
1353 ShellInfo *shell_info = shell_info_ap.release();
1354 TimeValue timeout_time(TimeValue::Now());
1355 timeout_time.OffsetWithSeconds(timeout_sec);
1356 bool timed_out = false;
1357 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1358 if (timed_out)
1359 {
1360 error.SetErrorString("timed out waiting for shell command to complete");
1361
1362 // Kill the process since it didn't complete withint the timeout specified
1363 ::kill (pid, SIGKILL);
1364 // Wait for the monitor callback to get the message
1365 timeout_time = TimeValue::Now();
1366 timeout_time.OffsetWithSeconds(1);
1367 timed_out = false;
1368 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1369 }
1370 else
1371 {
1372 if (status_ptr)
1373 *status_ptr = shell_info->status;
1374
1375 if (signo_ptr)
1376 *signo_ptr = shell_info->signo;
1377
1378 if (command_output_ptr)
1379 {
1380 command_output_ptr->clear();
1381 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1382 uint64_t file_size = file_spec.GetByteSize();
1383 if (file_size > 0)
1384 {
1385 if (file_size > command_output_ptr->max_size())
1386 {
1387 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1388 }
1389 else
1390 {
1391 command_output_ptr->resize(file_size);
1392 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1393 }
1394 }
1395 }
1396 }
1397 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1398 }
1399 else
1400 {
1401 error.SetErrorString("failed to get process ID");
1402 }
1403
1404 if (output_file_path)
1405 ::unlink (output_file_path);
1406 // Handshake with the monitor thread, or just let it know in advance that
1407 // it can delete "shell_info" in case we timed out and were not able to kill
1408 // the process...
1409 return error;
1410}
1411
1412
1413
Johnny Chen4b663292011-08-02 20:52:42 +00001414#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001415bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001416Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001417{
1418 return false;
1419}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001420
Greg Claytone98ac252010-11-10 04:57:04 +00001421void
1422Host::SetCrashDescriptionWithFormat (const char *format, ...)
1423{
1424}
1425
1426void
1427Host::SetCrashDescription (const char *description)
1428{
1429}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001430
1431lldb::pid_t
1432LaunchApplication (const FileSpec &app_file_spec)
1433{
1434 return LLDB_INVALID_PROCESS_ID;
1435}
1436
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001437#endif