blob: 4b23ff412ab37f810b907303508d4705e56a2565 [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:
Greg Clayton1a450cd2012-09-07 17:49:29 +0000342 g_host_arch_64.SetTriple(triple);
343 g_supports_64 = true;
344 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
345 g_supports_32 = true;
346 break;
347
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000348 case llvm::Triple::sparcv9:
349 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000350 case llvm::Triple::cellspu:
351 g_host_arch_64.SetTriple(triple);
352 g_supports_64 = true;
353 break;
354 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000355
356 g_supports_32 = g_host_arch_32.IsValid();
357 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000358 }
Greg Clayton395fc332011-02-15 21:59:32 +0000359
360#endif // #else for #if defined (__APPLE__)
361
362 if (arch_kind == eSystemDefaultArchitecture32)
363 return g_host_arch_32;
364 else if (arch_kind == eSystemDefaultArchitecture64)
365 return g_host_arch_64;
366
367 if (g_supports_64)
368 return g_host_arch_64;
369
370 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000371}
372
373const ConstString &
374Host::GetVendorString()
375{
376 static ConstString g_vendor;
377 if (!g_vendor)
378 {
379#if defined (__APPLE__)
Greg Clayton5b0025f2012-05-12 00:01:21 +0000380 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
381 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
382 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000383#elif defined (__linux__)
384 g_vendor.SetCString("gnu");
Johnny Chen4b663292011-08-02 20:52:42 +0000385#elif defined (__FreeBSD__)
386 g_vendor.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000387#endif
388 }
389 return g_vendor;
390}
391
392const ConstString &
393Host::GetOSString()
394{
395 static ConstString g_os_string;
396 if (!g_os_string)
397 {
398#if defined (__APPLE__)
Greg Clayton5b0025f2012-05-12 00:01:21 +0000399 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
400 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
401 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000402#elif defined (__linux__)
403 g_os_string.SetCString("linux");
Johnny Chen4b663292011-08-02 20:52:42 +0000404#elif defined (__FreeBSD__)
405 g_os_string.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000406#endif
407 }
408 return g_os_string;
409}
410
411const ConstString &
412Host::GetTargetTriple()
413{
414 static ConstString g_host_triple;
415 if (!(g_host_triple))
416 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000417 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
418 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000419 }
420 return g_host_triple;
421}
422
423lldb::pid_t
424Host::GetCurrentProcessID()
425{
426 return ::getpid();
427}
428
429lldb::tid_t
430Host::GetCurrentThreadID()
431{
432#if defined (__APPLE__)
433 return ::mach_thread_self();
Johnny Chen4b663292011-08-02 20:52:42 +0000434#elif defined(__FreeBSD__)
435 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000436#else
437 return lldb::tid_t(pthread_self());
438#endif
439}
440
Jim Ingham1831e782012-04-07 00:00:41 +0000441lldb::thread_t
442Host::GetCurrentThread ()
443{
444 return lldb::thread_t(pthread_self());
445}
446
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000447const char *
448Host::GetSignalAsCString (int signo)
449{
450 switch (signo)
451 {
452 case SIGHUP: return "SIGHUP"; // 1 hangup
453 case SIGINT: return "SIGINT"; // 2 interrupt
454 case SIGQUIT: return "SIGQUIT"; // 3 quit
455 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
456 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
457 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000458#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000459 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000460#endif
461#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000462 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000463#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000464 case SIGFPE: return "SIGFPE"; // 8 floating point exception
465 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
466 case SIGBUS: return "SIGBUS"; // 10 bus error
467 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
468 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
469 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
470 case SIGALRM: return "SIGALRM"; // 14 alarm clock
471 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
472 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
473 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
474 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
475 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
476 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
477 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
478 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
479#if !defined(_POSIX_C_SOURCE)
480 case SIGIO: return "SIGIO"; // 23 input/output possible signal
481#endif
482 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
483 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
484 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
485 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
486#if !defined(_POSIX_C_SOURCE)
487 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
488 case SIGINFO: return "SIGINFO"; // 29 information request
489#endif
490 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
491 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
492 default:
493 break;
494 }
495 return NULL;
496}
497
498void
499Host::WillTerminate ()
500{
501}
502
Johnny Chen4b663292011-08-02 20:52:42 +0000503#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000504void
505Host::ThreadCreated (const char *thread_name)
506{
507}
Greg Claytonb749a262010-12-03 06:02:24 +0000508
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000509void
Greg Claytonb749a262010-12-03 06:02:24 +0000510Host::Backtrace (Stream &strm, uint32_t max_frames)
511{
Greg Clayton52fd9842011-02-02 02:24:04 +0000512 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000513}
514
Greg Clayton638351a2010-12-04 00:10:17 +0000515size_t
516Host::GetEnvironment (StringList &env)
517{
Greg Clayton52fd9842011-02-02 02:24:04 +0000518 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000519 return 0;
520}
521
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000522#endif
523
524struct HostThreadCreateInfo
525{
526 std::string thread_name;
527 thread_func_t thread_fptr;
528 thread_arg_t thread_arg;
529
530 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
531 thread_name (name ? name : ""),
532 thread_fptr (fptr),
533 thread_arg (arg)
534 {
535 }
536};
537
538static thread_result_t
539ThreadCreateTrampoline (thread_arg_t arg)
540{
541 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
542 Host::ThreadCreated (info->thread_name.c_str());
543 thread_func_t thread_fptr = info->thread_fptr;
544 thread_arg_t thread_arg = info->thread_arg;
545
Greg Claytone005f2c2010-11-06 01:53:30 +0000546 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000547 if (log)
548 log->Printf("thread created");
549
550 delete info;
551 return thread_fptr (thread_arg);
552}
553
554lldb::thread_t
555Host::ThreadCreate
556(
557 const char *thread_name,
558 thread_func_t thread_fptr,
559 thread_arg_t thread_arg,
560 Error *error
561)
562{
563 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
564
565 // Host::ThreadCreateTrampoline will delete this pointer for us.
566 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
567
568 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
569 if (err == 0)
570 {
571 if (error)
572 error->Clear();
573 return thread;
574 }
575
576 if (error)
577 error->SetError (err, eErrorTypePOSIX);
578
579 return LLDB_INVALID_HOST_THREAD;
580}
581
582bool
583Host::ThreadCancel (lldb::thread_t thread, Error *error)
584{
585 int err = ::pthread_cancel (thread);
586 if (error)
587 error->SetError(err, eErrorTypePOSIX);
588 return err == 0;
589}
590
591bool
592Host::ThreadDetach (lldb::thread_t thread, Error *error)
593{
594 int err = ::pthread_detach (thread);
595 if (error)
596 error->SetError(err, eErrorTypePOSIX);
597 return err == 0;
598}
599
600bool
601Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
602{
603 int err = ::pthread_join (thread, thread_result_ptr);
604 if (error)
605 error->SetError(err, eErrorTypePOSIX);
606 return err == 0;
607}
608
Jim Ingham35dd4962012-05-04 19:24:49 +0000609// rdar://problem/8153284
610// Fixed a crasher where during shutdown, loggings attempted to access the
611// thread name but the static map instance had already been destructed.
612// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
613// pthread_once action. That map will get leaked.
614//
615// Another approach is to introduce a static guard object which monitors its
616// own destruction and raises a flag, but this incurs more overhead.
617
618static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
619static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
620
621static void
622InitThreadNamesMap()
623{
624 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
625}
626
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000627//------------------------------------------------------------------
628// Control access to a static file thread name map using a single
629// static function to avoid a static constructor.
630//------------------------------------------------------------------
631static const char *
632ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
633{
Jim Ingham35dd4962012-05-04 19:24:49 +0000634 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
635 if (success != 0)
636 return NULL;
637
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000638 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
639
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000640 if (get)
641 {
642 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000643 std::string value;
644 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
645 if (found_it)
646 return value.c_str();
647 else
648 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000649 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000650 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000651 {
652 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000653 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000654 }
655 return NULL;
656}
657
658const char *
659Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
660{
661 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
662 if (name == NULL)
663 {
664#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
665 // We currently can only get the name of a thread in the current process.
666 if (pid == Host::GetCurrentProcessID())
667 {
668 char pthread_name[1024];
669 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
670 {
671 if (pthread_name[0])
672 {
673 // Set the thread in our string pool
674 ThreadNameAccessor (false, pid, tid, pthread_name);
675 // Get our copy of the thread name string
676 name = ThreadNameAccessor (true, pid, tid, NULL);
677 }
678 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000679
680 if (name == NULL)
681 {
682 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
683 if (current_queue != NULL)
684 name = dispatch_queue_get_label (current_queue);
685 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000686 }
687#endif
688 }
689 return name;
690}
691
692void
693Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
694{
695 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
696 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
697 if (pid == LLDB_INVALID_PROCESS_ID)
698 pid = curr_pid;
699
700 if (tid == LLDB_INVALID_THREAD_ID)
701 tid = curr_tid;
702
703#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
704 // Set the pthread name if possible
705 if (pid == curr_pid && tid == curr_tid)
706 {
707 ::pthread_setname_np (name);
708 }
709#endif
710 ThreadNameAccessor (false, pid, tid, name);
711}
712
713FileSpec
714Host::GetProgramFileSpec ()
715{
716 static FileSpec g_program_filespec;
717 if (!g_program_filespec)
718 {
719#if defined (__APPLE__)
720 char program_fullpath[PATH_MAX];
721 // If DST is NULL, then return the number of bytes needed.
722 uint32_t len = sizeof(program_fullpath);
723 int err = _NSGetExecutablePath (program_fullpath, &len);
724 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000725 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000726 else if (err == -1)
727 {
728 char *large_program_fullpath = (char *)::malloc (len + 1);
729
730 err = _NSGetExecutablePath (large_program_fullpath, &len);
731 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000732 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000733
734 ::free (large_program_fullpath);
735 }
736#elif defined (__linux__)
737 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000738 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
739 if (len > 0) {
740 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000741 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000742 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000743#elif defined (__FreeBSD__)
744 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
745 size_t exe_path_size;
746 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
747 {
Greg Clayton366795e2011-01-13 01:27:55 +0000748 char *exe_path = new char[exe_path_size];
749 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
750 g_program_filespec.SetFile(exe_path, false);
751 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000752 }
753#endif
754 }
755 return g_program_filespec;
756}
757
758FileSpec
759Host::GetModuleFileSpecForHostAddress (const void *host_addr)
760{
761 FileSpec module_filespec;
762 Dl_info info;
763 if (::dladdr (host_addr, &info))
764 {
765 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000766 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000767 }
768 return module_filespec;
769}
770
771#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000772
773bool
774Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
775{
776 bundle.Clear();
777 return false;
778}
779
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000780bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000781Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000782{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000783 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000784}
785#endif
786
Greg Clayton14ef59f2011-02-08 00:35:34 +0000787// Opaque info that tracks a dynamic library that was loaded
788struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000789{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000790 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
791 file_spec (fs),
792 open_options (o),
793 handle (h)
794 {
795 }
796
797 const FileSpec file_spec;
798 uint32_t open_options;
799 void * handle;
800};
801
802void *
803Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
804{
Greg Clayton52fd9842011-02-02 02:24:04 +0000805 char path[PATH_MAX];
806 if (file_spec.GetPath(path, sizeof(path)))
807 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000808 int mode = 0;
809
810 if (options & eDynamicLibraryOpenOptionLazy)
811 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000812 else
813 mode |= RTLD_NOW;
814
Greg Clayton14ef59f2011-02-08 00:35:34 +0000815
816 if (options & eDynamicLibraryOpenOptionLocal)
817 mode |= RTLD_LOCAL;
818 else
819 mode |= RTLD_GLOBAL;
820
821#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
822 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
823 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000824#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000825
826 void * opaque = ::dlopen (path, mode);
827
828 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000829 {
830 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000831 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000832 }
833 else
834 {
835 error.SetErrorString(::dlerror());
836 }
837 }
838 else
839 {
840 error.SetErrorString("failed to extract path");
841 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000842 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000843}
844
845Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000846Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000847{
848 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000849 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000850 {
851 error.SetErrorString ("invalid dynamic library handle");
852 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000853 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000854 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000855 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
856 if (::dlclose (dylib_info->handle) != 0)
857 {
858 error.SetErrorString(::dlerror());
859 }
860
861 dylib_info->open_options = 0;
862 dylib_info->handle = 0;
863 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000864 }
865 return error;
866}
867
868void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000869Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000870{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000871 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000872 {
873 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000874 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000875 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000876 {
877 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
878
879 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
880 if (symbol_addr)
881 {
882#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
883 // This host doesn't support limiting searches to this shared library
884 // so we need to verify that the match came from this shared library
885 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000886 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000887 {
888 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
889 if (match_dylib_spec != dylib_info->file_spec)
890 {
891 char dylib_path[PATH_MAX];
892 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
893 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
894 else
895 error.SetErrorString ("symbol not found");
896 return NULL;
897 }
898 }
899#endif
900 error.Clear();
901 return symbol_addr;
902 }
903 else
904 {
905 error.SetErrorString(::dlerror());
906 }
907 }
908 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000909}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000910
911bool
912Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
913{
Greg Clayton5d187e52011-01-08 20:28:42 +0000914 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000915 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
916 // on linux this is assumed to be the "lldb" main executable. If LLDB on
917 // linux is actually in a shared library (lldb.so??) then this function will
918 // need to be modified to "do the right thing".
919
920 switch (path_type)
921 {
922 case ePathTypeLLDBShlibDir:
923 {
924 static ConstString g_lldb_so_dir;
925 if (!g_lldb_so_dir)
926 {
927 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
928 g_lldb_so_dir = lldb_file_spec.GetDirectory();
929 }
930 file_spec.GetDirectory() = g_lldb_so_dir;
931 return file_spec.GetDirectory();
932 }
933 break;
934
935 case ePathTypeSupportExecutableDir:
936 {
937 static ConstString g_lldb_support_exe_dir;
938 if (!g_lldb_support_exe_dir)
939 {
940 FileSpec lldb_file_spec;
941 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
942 {
943 char raw_path[PATH_MAX];
944 char resolved_path[PATH_MAX];
945 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
946
947#if defined (__APPLE__)
948 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
949 if (framework_pos)
950 {
951 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000952#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000953 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000954#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000955 }
956#endif
957 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
958 g_lldb_support_exe_dir.SetCString(resolved_path);
959 }
960 }
961 file_spec.GetDirectory() = g_lldb_support_exe_dir;
962 return file_spec.GetDirectory();
963 }
964 break;
965
966 case ePathTypeHeaderDir:
967 {
968 static ConstString g_lldb_headers_dir;
969 if (!g_lldb_headers_dir)
970 {
971#if defined (__APPLE__)
972 FileSpec lldb_file_spec;
973 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
974 {
975 char raw_path[PATH_MAX];
976 char resolved_path[PATH_MAX];
977 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
978
979 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
980 if (framework_pos)
981 {
982 framework_pos += strlen("LLDB.framework");
983 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
984 }
985 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
986 g_lldb_headers_dir.SetCString(resolved_path);
987 }
988#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000989 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000990 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
991#endif
992 }
993 file_spec.GetDirectory() = g_lldb_headers_dir;
994 return file_spec.GetDirectory();
995 }
996 break;
997
998 case ePathTypePythonDir:
999 {
Greg Clayton52fd9842011-02-02 02:24:04 +00001000 // TODO: Anyone know how we can determine this for linux? Other systems?
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001001 // For linux and FreeBSD we are currently assuming the
1002 // location of the lldb binary that contains this function is
1003 // the directory that will contain a python directory which
1004 // has our lldb module. This is how files get placed when
1005 // compiling with Makefiles.
Greg Clayton24b48ff2010-10-17 22:03:32 +00001006
1007 static ConstString g_lldb_python_dir;
1008 if (!g_lldb_python_dir)
1009 {
1010 FileSpec lldb_file_spec;
1011 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1012 {
1013 char raw_path[PATH_MAX];
1014 char resolved_path[PATH_MAX];
1015 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1016
1017#if defined (__APPLE__)
1018 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1019 if (framework_pos)
1020 {
1021 framework_pos += strlen("LLDB.framework");
1022 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1023 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001024#else
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +00001025 // We may get our string truncated. Should we protect
1026 // this with an assert?
1027 ::strncat(raw_path, "/python", sizeof(raw_path) - strlen(raw_path) - 1);
Greg Clayton24b48ff2010-10-17 22:03:32 +00001028#endif
1029 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1030 g_lldb_python_dir.SetCString(resolved_path);
1031 }
1032 }
1033 file_spec.GetDirectory() = g_lldb_python_dir;
1034 return file_spec.GetDirectory();
1035 }
1036 break;
1037
Greg Clayton52fd9842011-02-02 02:24:04 +00001038 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1039 {
1040#if defined (__APPLE__)
1041 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001042 static bool g_lldb_system_plugin_dir_located = false;
1043 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001044 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001045 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001046 FileSpec lldb_file_spec;
1047 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1048 {
1049 char raw_path[PATH_MAX];
1050 char resolved_path[PATH_MAX];
1051 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1052
1053 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1054 if (framework_pos)
1055 {
1056 framework_pos += strlen("LLDB.framework");
1057 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001058 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1059 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001060 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001061 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001062 }
1063 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001064
1065 if (g_lldb_system_plugin_dir)
1066 {
1067 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1068 return true;
1069 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001070#endif
1071 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1072 return false;
1073 }
1074 break;
1075
1076 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1077 {
1078#if defined (__APPLE__)
1079 static ConstString g_lldb_user_plugin_dir;
1080 if (!g_lldb_user_plugin_dir)
1081 {
1082 char user_plugin_path[PATH_MAX];
1083 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1084 user_plugin_path,
1085 sizeof(user_plugin_path)))
1086 {
1087 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1088 }
1089 }
1090 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1091 return file_spec.GetDirectory();
1092#endif
1093 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1094 return false;
1095 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001096 default:
1097 assert (!"Unhandled PathType");
1098 break;
1099 }
1100
1101 return false;
1102}
1103
Greg Clayton58e26e02011-03-24 04:28:38 +00001104
1105bool
1106Host::GetHostname (std::string &s)
1107{
1108 char hostname[PATH_MAX];
1109 hostname[sizeof(hostname) - 1] = '\0';
1110 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1111 {
1112 struct hostent* h = ::gethostbyname (hostname);
1113 if (h)
1114 s.assign (h->h_name);
1115 else
1116 s.assign (hostname);
1117 return true;
1118 }
1119 return false;
1120}
1121
Greg Clayton24bc5d92011-03-30 18:16:51 +00001122const char *
1123Host::GetUserName (uint32_t uid, std::string &user_name)
1124{
1125 struct passwd user_info;
1126 struct passwd *user_info_ptr = &user_info;
1127 char user_buffer[PATH_MAX];
1128 size_t user_buffer_size = sizeof(user_buffer);
1129 if (::getpwuid_r (uid,
1130 &user_info,
1131 user_buffer,
1132 user_buffer_size,
1133 &user_info_ptr) == 0)
1134 {
1135 if (user_info_ptr)
1136 {
1137 user_name.assign (user_info_ptr->pw_name);
1138 return user_name.c_str();
1139 }
1140 }
1141 user_name.clear();
1142 return NULL;
1143}
1144
1145const char *
1146Host::GetGroupName (uint32_t gid, std::string &group_name)
1147{
1148 char group_buffer[PATH_MAX];
1149 size_t group_buffer_size = sizeof(group_buffer);
1150 struct group group_info;
1151 struct group *group_info_ptr = &group_info;
1152 // Try the threadsafe version first
1153 if (::getgrgid_r (gid,
1154 &group_info,
1155 group_buffer,
1156 group_buffer_size,
1157 &group_info_ptr) == 0)
1158 {
1159 if (group_info_ptr)
1160 {
1161 group_name.assign (group_info_ptr->gr_name);
1162 return group_name.c_str();
1163 }
1164 }
1165 else
1166 {
1167 // The threadsafe version isn't currently working
1168 // for me on darwin, but the non-threadsafe version
1169 // is, so I am calling it below.
1170 group_info_ptr = ::getgrgid (gid);
1171 if (group_info_ptr)
1172 {
1173 group_name.assign (group_info_ptr->gr_name);
1174 return group_name.c_str();
1175 }
1176 }
1177 group_name.clear();
1178 return NULL;
1179}
1180
Johnny Chen4b663292011-08-02 20:52:42 +00001181#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001182bool
1183Host::GetOSBuildString (std::string &s)
1184{
1185 s.clear();
1186 return false;
1187}
1188
1189bool
1190Host::GetOSKernelDescription (std::string &s)
1191{
1192 s.clear();
1193 return false;
1194}
Johnny Chen4b663292011-08-02 20:52:42 +00001195#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001196
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001197uint32_t
1198Host::GetUserID ()
1199{
1200 return getuid();
1201}
1202
1203uint32_t
1204Host::GetGroupID ()
1205{
1206 return getgid();
1207}
1208
1209uint32_t
1210Host::GetEffectiveUserID ()
1211{
1212 return geteuid();
1213}
1214
1215uint32_t
1216Host::GetEffectiveGroupID ()
1217{
1218 return getegid();
1219}
1220
1221#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001222uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001223Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001224{
1225 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001226 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001227}
Johnny Chen4b663292011-08-02 20:52:42 +00001228#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001229
Johnny Chen4b663292011-08-02 20:52:42 +00001230#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001231bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001232Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001233{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001234 process_info.Clear();
1235 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001236}
Johnny Chen4b663292011-08-02 20:52:42 +00001237#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001238
Sean Callananf35a96c2011-10-27 21:22:25 +00001239lldb::TargetSP
1240Host::GetDummyTarget (lldb_private::Debugger &debugger)
1241{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001242 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001243
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001244 // FIXME: Maybe the dummy target should be per-Debugger
1245 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1246 {
1247 ArchSpec arch(Target::GetDefaultArchitecture());
1248 if (!arch.IsValid())
1249 arch = Host::GetArchitecture ();
1250 Error err = debugger.GetTargetList().CreateTarget(debugger,
1251 FileSpec(),
1252 arch.GetTriple().getTriple().c_str(),
1253 false,
1254 NULL,
1255 g_dummy_target_sp);
1256 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001257
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001258 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001259}
1260
Greg Clayton97471182012-04-14 01:42:46 +00001261struct ShellInfo
1262{
1263 ShellInfo () :
1264 process_reaped (false),
1265 can_delete (false),
1266 pid (LLDB_INVALID_PROCESS_ID),
1267 signo(-1),
1268 status(-1)
1269 {
1270 }
1271
1272 lldb_private::Predicate<bool> process_reaped;
1273 lldb_private::Predicate<bool> can_delete;
1274 lldb::pid_t pid;
1275 int signo;
1276 int status;
1277};
1278
1279static bool
1280MonitorShellCommand (void *callback_baton,
1281 lldb::pid_t pid,
1282 bool exited, // True if the process did exit
1283 int signo, // Zero for no signal
1284 int status) // Exit value of process if signal is zero
1285{
1286 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1287 shell_info->pid = pid;
1288 shell_info->signo = signo;
1289 shell_info->status = status;
1290 // Let the thread running Host::RunShellCommand() know that the process
1291 // exited and that ShellInfo has been filled in by broadcasting to it
1292 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1293 // Now wait for a handshake back from that thread running Host::RunShellCommand
1294 // so we know that we can delete shell_info_ptr
1295 shell_info->can_delete.WaitForValueEqualTo(true);
1296 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1297 usleep(1000);
1298 // Now delete the shell info that was passed into this function
1299 delete shell_info;
1300 return true;
1301}
1302
1303Error
1304Host::RunShellCommand (const char *command,
1305 const char *working_dir,
1306 int *status_ptr,
1307 int *signo_ptr,
1308 std::string *command_output_ptr,
1309 uint32_t timeout_sec)
1310{
1311 Error error;
1312 ProcessLaunchInfo launch_info;
1313 launch_info.SetShell("/bin/bash");
1314 launch_info.GetArguments().AppendArgument(command);
1315 const bool localhost = true;
1316 const bool will_debug = false;
1317 const bool first_arg_is_full_shell_command = true;
1318 launch_info.ConvertArgumentsForLaunchingInShell (error,
1319 localhost,
1320 will_debug,
1321 first_arg_is_full_shell_command);
1322
1323 if (working_dir)
1324 launch_info.SetWorkingDirectory(working_dir);
1325 char output_file_path_buffer[L_tmpnam];
1326 const char *output_file_path = NULL;
1327 if (command_output_ptr)
1328 {
1329 // Create a temporary file to get the stdout/stderr and redirect the
1330 // output of the command into this file. We will later read this file
1331 // if all goes well and fill the data into "command_output_ptr"
1332 output_file_path = ::tmpnam(output_file_path_buffer);
1333 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1334 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
1335 launch_info.AppendDuplicateFileAction(STDERR_FILENO, STDOUT_FILENO);
1336 }
1337 else
1338 {
1339 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1340 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1341 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1342 }
1343
1344 // The process monitor callback will delete the 'shell_info_ptr' below...
1345 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1346
1347 const bool monitor_signals = false;
1348 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1349
1350 error = LaunchProcess (launch_info);
1351 const lldb::pid_t pid = launch_info.GetProcessID();
1352 if (pid != LLDB_INVALID_PROCESS_ID)
1353 {
1354 // The process successfully launched, so we can defer ownership of
1355 // "shell_info" to the MonitorShellCommand callback function that will
1356 // get called when the process dies. We release the std::auto_ptr as it
1357 // doesn't need to delete the ShellInfo anymore.
1358 ShellInfo *shell_info = shell_info_ap.release();
1359 TimeValue timeout_time(TimeValue::Now());
1360 timeout_time.OffsetWithSeconds(timeout_sec);
1361 bool timed_out = false;
1362 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1363 if (timed_out)
1364 {
1365 error.SetErrorString("timed out waiting for shell command to complete");
1366
1367 // Kill the process since it didn't complete withint the timeout specified
1368 ::kill (pid, SIGKILL);
1369 // Wait for the monitor callback to get the message
1370 timeout_time = TimeValue::Now();
1371 timeout_time.OffsetWithSeconds(1);
1372 timed_out = false;
1373 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1374 }
1375 else
1376 {
1377 if (status_ptr)
1378 *status_ptr = shell_info->status;
1379
1380 if (signo_ptr)
1381 *signo_ptr = shell_info->signo;
1382
1383 if (command_output_ptr)
1384 {
1385 command_output_ptr->clear();
1386 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1387 uint64_t file_size = file_spec.GetByteSize();
1388 if (file_size > 0)
1389 {
1390 if (file_size > command_output_ptr->max_size())
1391 {
1392 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1393 }
1394 else
1395 {
1396 command_output_ptr->resize(file_size);
1397 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1398 }
1399 }
1400 }
1401 }
1402 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1403 }
1404 else
1405 {
1406 error.SetErrorString("failed to get process ID");
1407 }
1408
1409 if (output_file_path)
1410 ::unlink (output_file_path);
1411 // Handshake with the monitor thread, or just let it know in advance that
1412 // it can delete "shell_info" in case we timed out and were not able to kill
1413 // the process...
1414 return error;
1415}
1416
1417
1418
Johnny Chen4b663292011-08-02 20:52:42 +00001419#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001420bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001421Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001422{
1423 return false;
1424}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001425
Greg Claytone98ac252010-11-10 04:57:04 +00001426void
1427Host::SetCrashDescriptionWithFormat (const char *format, ...)
1428{
1429}
1430
1431void
1432Host::SetCrashDescription (const char *description)
1433{
1434}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001435
1436lldb::pid_t
1437LaunchApplication (const FileSpec &app_file_spec)
1438{
1439 return LLDB_INVALID_PROCESS_ID;
1440}
1441
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001442#endif