blob: 1e3cb0a14da04f40f31d579f0949c6deb8151f04 [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__)
Greg Clayton5b0025f2012-05-12 00:01:21 +0000374 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
375 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
376 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000377#elif defined (__linux__)
378 g_vendor.SetCString("gnu");
Johnny Chen4b663292011-08-02 20:52:42 +0000379#elif defined (__FreeBSD__)
380 g_vendor.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000381#endif
382 }
383 return g_vendor;
384}
385
386const ConstString &
387Host::GetOSString()
388{
389 static ConstString g_os_string;
390 if (!g_os_string)
391 {
392#if defined (__APPLE__)
Greg Clayton5b0025f2012-05-12 00:01:21 +0000393 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
394 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
395 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000396#elif defined (__linux__)
397 g_os_string.SetCString("linux");
Johnny Chen4b663292011-08-02 20:52:42 +0000398#elif defined (__FreeBSD__)
399 g_os_string.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000400#endif
401 }
402 return g_os_string;
403}
404
405const ConstString &
406Host::GetTargetTriple()
407{
408 static ConstString g_host_triple;
409 if (!(g_host_triple))
410 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000411 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
412 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000413 }
414 return g_host_triple;
415}
416
417lldb::pid_t
418Host::GetCurrentProcessID()
419{
420 return ::getpid();
421}
422
423lldb::tid_t
424Host::GetCurrentThreadID()
425{
426#if defined (__APPLE__)
427 return ::mach_thread_self();
Johnny Chen4b663292011-08-02 20:52:42 +0000428#elif defined(__FreeBSD__)
429 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000430#else
431 return lldb::tid_t(pthread_self());
432#endif
433}
434
Jim Ingham1831e782012-04-07 00:00:41 +0000435lldb::thread_t
436Host::GetCurrentThread ()
437{
438 return lldb::thread_t(pthread_self());
439}
440
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000441const char *
442Host::GetSignalAsCString (int signo)
443{
444 switch (signo)
445 {
446 case SIGHUP: return "SIGHUP"; // 1 hangup
447 case SIGINT: return "SIGINT"; // 2 interrupt
448 case SIGQUIT: return "SIGQUIT"; // 3 quit
449 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
450 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
451 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000452#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000453 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000454#endif
455#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000456 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000457#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000458 case SIGFPE: return "SIGFPE"; // 8 floating point exception
459 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
460 case SIGBUS: return "SIGBUS"; // 10 bus error
461 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
462 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
463 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
464 case SIGALRM: return "SIGALRM"; // 14 alarm clock
465 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
466 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
467 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
468 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
469 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
470 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
471 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
472 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
473#if !defined(_POSIX_C_SOURCE)
474 case SIGIO: return "SIGIO"; // 23 input/output possible signal
475#endif
476 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
477 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
478 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
479 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
480#if !defined(_POSIX_C_SOURCE)
481 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
482 case SIGINFO: return "SIGINFO"; // 29 information request
483#endif
484 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
485 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
486 default:
487 break;
488 }
489 return NULL;
490}
491
492void
493Host::WillTerminate ()
494{
495}
496
Johnny Chen4b663292011-08-02 20:52:42 +0000497#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000498void
499Host::ThreadCreated (const char *thread_name)
500{
501}
Greg Claytonb749a262010-12-03 06:02:24 +0000502
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000503void
Greg Claytonb749a262010-12-03 06:02:24 +0000504Host::Backtrace (Stream &strm, uint32_t max_frames)
505{
Greg Clayton52fd9842011-02-02 02:24:04 +0000506 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000507}
508
Greg Clayton638351a2010-12-04 00:10:17 +0000509size_t
510Host::GetEnvironment (StringList &env)
511{
Greg Clayton52fd9842011-02-02 02:24:04 +0000512 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000513 return 0;
514}
515
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000516#endif
517
518struct HostThreadCreateInfo
519{
520 std::string thread_name;
521 thread_func_t thread_fptr;
522 thread_arg_t thread_arg;
523
524 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
525 thread_name (name ? name : ""),
526 thread_fptr (fptr),
527 thread_arg (arg)
528 {
529 }
530};
531
532static thread_result_t
533ThreadCreateTrampoline (thread_arg_t arg)
534{
535 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
536 Host::ThreadCreated (info->thread_name.c_str());
537 thread_func_t thread_fptr = info->thread_fptr;
538 thread_arg_t thread_arg = info->thread_arg;
539
Greg Claytone005f2c2010-11-06 01:53:30 +0000540 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000541 if (log)
542 log->Printf("thread created");
543
544 delete info;
545 return thread_fptr (thread_arg);
546}
547
548lldb::thread_t
549Host::ThreadCreate
550(
551 const char *thread_name,
552 thread_func_t thread_fptr,
553 thread_arg_t thread_arg,
554 Error *error
555)
556{
557 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
558
559 // Host::ThreadCreateTrampoline will delete this pointer for us.
560 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
561
562 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
563 if (err == 0)
564 {
565 if (error)
566 error->Clear();
567 return thread;
568 }
569
570 if (error)
571 error->SetError (err, eErrorTypePOSIX);
572
573 return LLDB_INVALID_HOST_THREAD;
574}
575
576bool
577Host::ThreadCancel (lldb::thread_t thread, Error *error)
578{
579 int err = ::pthread_cancel (thread);
580 if (error)
581 error->SetError(err, eErrorTypePOSIX);
582 return err == 0;
583}
584
585bool
586Host::ThreadDetach (lldb::thread_t thread, Error *error)
587{
588 int err = ::pthread_detach (thread);
589 if (error)
590 error->SetError(err, eErrorTypePOSIX);
591 return err == 0;
592}
593
594bool
595Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
596{
597 int err = ::pthread_join (thread, thread_result_ptr);
598 if (error)
599 error->SetError(err, eErrorTypePOSIX);
600 return err == 0;
601}
602
Jim Ingham35dd4962012-05-04 19:24:49 +0000603// rdar://problem/8153284
604// Fixed a crasher where during shutdown, loggings attempted to access the
605// thread name but the static map instance had already been destructed.
606// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
607// pthread_once action. That map will get leaked.
608//
609// Another approach is to introduce a static guard object which monitors its
610// own destruction and raises a flag, but this incurs more overhead.
611
612static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
613static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
614
615static void
616InitThreadNamesMap()
617{
618 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
619}
620
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000621//------------------------------------------------------------------
622// Control access to a static file thread name map using a single
623// static function to avoid a static constructor.
624//------------------------------------------------------------------
625static const char *
626ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
627{
Jim Ingham35dd4962012-05-04 19:24:49 +0000628 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
629 if (success != 0)
630 return NULL;
631
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000632 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
633
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000634 if (get)
635 {
636 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000637 std::string value;
638 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
639 if (found_it)
640 return value.c_str();
641 else
642 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000643 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000644 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000645 {
646 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000647 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000648 }
649 return NULL;
650}
651
652const char *
653Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
654{
655 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
656 if (name == NULL)
657 {
658#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
659 // We currently can only get the name of a thread in the current process.
660 if (pid == Host::GetCurrentProcessID())
661 {
662 char pthread_name[1024];
663 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
664 {
665 if (pthread_name[0])
666 {
667 // Set the thread in our string pool
668 ThreadNameAccessor (false, pid, tid, pthread_name);
669 // Get our copy of the thread name string
670 name = ThreadNameAccessor (true, pid, tid, NULL);
671 }
672 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000673
674 if (name == NULL)
675 {
676 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
677 if (current_queue != NULL)
678 name = dispatch_queue_get_label (current_queue);
679 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000680 }
681#endif
682 }
683 return name;
684}
685
686void
687Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
688{
689 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
690 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
691 if (pid == LLDB_INVALID_PROCESS_ID)
692 pid = curr_pid;
693
694 if (tid == LLDB_INVALID_THREAD_ID)
695 tid = curr_tid;
696
697#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
698 // Set the pthread name if possible
699 if (pid == curr_pid && tid == curr_tid)
700 {
701 ::pthread_setname_np (name);
702 }
703#endif
704 ThreadNameAccessor (false, pid, tid, name);
705}
706
707FileSpec
708Host::GetProgramFileSpec ()
709{
710 static FileSpec g_program_filespec;
711 if (!g_program_filespec)
712 {
713#if defined (__APPLE__)
714 char program_fullpath[PATH_MAX];
715 // If DST is NULL, then return the number of bytes needed.
716 uint32_t len = sizeof(program_fullpath);
717 int err = _NSGetExecutablePath (program_fullpath, &len);
718 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000719 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000720 else if (err == -1)
721 {
722 char *large_program_fullpath = (char *)::malloc (len + 1);
723
724 err = _NSGetExecutablePath (large_program_fullpath, &len);
725 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000726 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000727
728 ::free (large_program_fullpath);
729 }
730#elif defined (__linux__)
731 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000732 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
733 if (len > 0) {
734 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000735 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000736 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000737#elif defined (__FreeBSD__)
738 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
739 size_t exe_path_size;
740 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
741 {
Greg Clayton366795e2011-01-13 01:27:55 +0000742 char *exe_path = new char[exe_path_size];
743 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
744 g_program_filespec.SetFile(exe_path, false);
745 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000746 }
747#endif
748 }
749 return g_program_filespec;
750}
751
752FileSpec
753Host::GetModuleFileSpecForHostAddress (const void *host_addr)
754{
755 FileSpec module_filespec;
756 Dl_info info;
757 if (::dladdr (host_addr, &info))
758 {
759 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000760 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000761 }
762 return module_filespec;
763}
764
765#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000766
767bool
768Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
769{
770 bundle.Clear();
771 return false;
772}
773
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000774bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000775Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000776{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000777 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000778}
779#endif
780
Greg Clayton14ef59f2011-02-08 00:35:34 +0000781// Opaque info that tracks a dynamic library that was loaded
782struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000783{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000784 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
785 file_spec (fs),
786 open_options (o),
787 handle (h)
788 {
789 }
790
791 const FileSpec file_spec;
792 uint32_t open_options;
793 void * handle;
794};
795
796void *
797Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
798{
Greg Clayton52fd9842011-02-02 02:24:04 +0000799 char path[PATH_MAX];
800 if (file_spec.GetPath(path, sizeof(path)))
801 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000802 int mode = 0;
803
804 if (options & eDynamicLibraryOpenOptionLazy)
805 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000806 else
807 mode |= RTLD_NOW;
808
Greg Clayton14ef59f2011-02-08 00:35:34 +0000809
810 if (options & eDynamicLibraryOpenOptionLocal)
811 mode |= RTLD_LOCAL;
812 else
813 mode |= RTLD_GLOBAL;
814
815#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
816 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
817 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000818#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000819
820 void * opaque = ::dlopen (path, mode);
821
822 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000823 {
824 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000825 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000826 }
827 else
828 {
829 error.SetErrorString(::dlerror());
830 }
831 }
832 else
833 {
834 error.SetErrorString("failed to extract path");
835 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000836 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000837}
838
839Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000840Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000841{
842 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000843 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000844 {
845 error.SetErrorString ("invalid dynamic library handle");
846 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000847 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000848 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000849 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
850 if (::dlclose (dylib_info->handle) != 0)
851 {
852 error.SetErrorString(::dlerror());
853 }
854
855 dylib_info->open_options = 0;
856 dylib_info->handle = 0;
857 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000858 }
859 return error;
860}
861
862void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000863Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000864{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000865 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000866 {
867 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000868 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000869 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000870 {
871 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
872
873 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
874 if (symbol_addr)
875 {
876#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
877 // This host doesn't support limiting searches to this shared library
878 // so we need to verify that the match came from this shared library
879 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000880 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000881 {
882 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
883 if (match_dylib_spec != dylib_info->file_spec)
884 {
885 char dylib_path[PATH_MAX];
886 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
887 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
888 else
889 error.SetErrorString ("symbol not found");
890 return NULL;
891 }
892 }
893#endif
894 error.Clear();
895 return symbol_addr;
896 }
897 else
898 {
899 error.SetErrorString(::dlerror());
900 }
901 }
902 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000903}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000904
905bool
906Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
907{
Greg Clayton5d187e52011-01-08 20:28:42 +0000908 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000909 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
910 // on linux this is assumed to be the "lldb" main executable. If LLDB on
911 // linux is actually in a shared library (lldb.so??) then this function will
912 // need to be modified to "do the right thing".
913
914 switch (path_type)
915 {
916 case ePathTypeLLDBShlibDir:
917 {
918 static ConstString g_lldb_so_dir;
919 if (!g_lldb_so_dir)
920 {
921 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
922 g_lldb_so_dir = lldb_file_spec.GetDirectory();
923 }
924 file_spec.GetDirectory() = g_lldb_so_dir;
925 return file_spec.GetDirectory();
926 }
927 break;
928
929 case ePathTypeSupportExecutableDir:
930 {
931 static ConstString g_lldb_support_exe_dir;
932 if (!g_lldb_support_exe_dir)
933 {
934 FileSpec lldb_file_spec;
935 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
936 {
937 char raw_path[PATH_MAX];
938 char resolved_path[PATH_MAX];
939 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
940
941#if defined (__APPLE__)
942 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
943 if (framework_pos)
944 {
945 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000946#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000947 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000948#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000949 }
950#endif
951 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
952 g_lldb_support_exe_dir.SetCString(resolved_path);
953 }
954 }
955 file_spec.GetDirectory() = g_lldb_support_exe_dir;
956 return file_spec.GetDirectory();
957 }
958 break;
959
960 case ePathTypeHeaderDir:
961 {
962 static ConstString g_lldb_headers_dir;
963 if (!g_lldb_headers_dir)
964 {
965#if defined (__APPLE__)
966 FileSpec lldb_file_spec;
967 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
968 {
969 char raw_path[PATH_MAX];
970 char resolved_path[PATH_MAX];
971 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
972
973 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
974 if (framework_pos)
975 {
976 framework_pos += strlen("LLDB.framework");
977 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
978 }
979 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
980 g_lldb_headers_dir.SetCString(resolved_path);
981 }
982#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000983 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000984 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
985#endif
986 }
987 file_spec.GetDirectory() = g_lldb_headers_dir;
988 return file_spec.GetDirectory();
989 }
990 break;
991
992 case ePathTypePythonDir:
993 {
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 // For linux we are currently assuming the location of the lldb
996 // binary that contains this function is the directory that will
997 // contain lldb.so, lldb.py and embedded_interpreter.py...
998
999 static ConstString g_lldb_python_dir;
1000 if (!g_lldb_python_dir)
1001 {
1002 FileSpec lldb_file_spec;
1003 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1004 {
1005 char raw_path[PATH_MAX];
1006 char resolved_path[PATH_MAX];
1007 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1008
1009#if defined (__APPLE__)
1010 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1011 if (framework_pos)
1012 {
1013 framework_pos += strlen("LLDB.framework");
1014 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1015 }
1016#endif
1017 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1018 g_lldb_python_dir.SetCString(resolved_path);
1019 }
1020 }
1021 file_spec.GetDirectory() = g_lldb_python_dir;
1022 return file_spec.GetDirectory();
1023 }
1024 break;
1025
Greg Clayton52fd9842011-02-02 02:24:04 +00001026 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1027 {
1028#if defined (__APPLE__)
1029 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001030 static bool g_lldb_system_plugin_dir_located = false;
1031 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001032 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001033 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001034 FileSpec lldb_file_spec;
1035 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1036 {
1037 char raw_path[PATH_MAX];
1038 char resolved_path[PATH_MAX];
1039 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1040
1041 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1042 if (framework_pos)
1043 {
1044 framework_pos += strlen("LLDB.framework");
1045 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001046 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1047 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001048 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001049 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001050 }
1051 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001052
1053 if (g_lldb_system_plugin_dir)
1054 {
1055 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1056 return true;
1057 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001058#endif
1059 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1060 return false;
1061 }
1062 break;
1063
1064 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1065 {
1066#if defined (__APPLE__)
1067 static ConstString g_lldb_user_plugin_dir;
1068 if (!g_lldb_user_plugin_dir)
1069 {
1070 char user_plugin_path[PATH_MAX];
1071 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1072 user_plugin_path,
1073 sizeof(user_plugin_path)))
1074 {
1075 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1076 }
1077 }
1078 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1079 return file_spec.GetDirectory();
1080#endif
1081 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1082 return false;
1083 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001084 default:
1085 assert (!"Unhandled PathType");
1086 break;
1087 }
1088
1089 return false;
1090}
1091
Greg Clayton58e26e02011-03-24 04:28:38 +00001092
1093bool
1094Host::GetHostname (std::string &s)
1095{
1096 char hostname[PATH_MAX];
1097 hostname[sizeof(hostname) - 1] = '\0';
1098 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1099 {
1100 struct hostent* h = ::gethostbyname (hostname);
1101 if (h)
1102 s.assign (h->h_name);
1103 else
1104 s.assign (hostname);
1105 return true;
1106 }
1107 return false;
1108}
1109
Greg Clayton24bc5d92011-03-30 18:16:51 +00001110const char *
1111Host::GetUserName (uint32_t uid, std::string &user_name)
1112{
1113 struct passwd user_info;
1114 struct passwd *user_info_ptr = &user_info;
1115 char user_buffer[PATH_MAX];
1116 size_t user_buffer_size = sizeof(user_buffer);
1117 if (::getpwuid_r (uid,
1118 &user_info,
1119 user_buffer,
1120 user_buffer_size,
1121 &user_info_ptr) == 0)
1122 {
1123 if (user_info_ptr)
1124 {
1125 user_name.assign (user_info_ptr->pw_name);
1126 return user_name.c_str();
1127 }
1128 }
1129 user_name.clear();
1130 return NULL;
1131}
1132
1133const char *
1134Host::GetGroupName (uint32_t gid, std::string &group_name)
1135{
1136 char group_buffer[PATH_MAX];
1137 size_t group_buffer_size = sizeof(group_buffer);
1138 struct group group_info;
1139 struct group *group_info_ptr = &group_info;
1140 // Try the threadsafe version first
1141 if (::getgrgid_r (gid,
1142 &group_info,
1143 group_buffer,
1144 group_buffer_size,
1145 &group_info_ptr) == 0)
1146 {
1147 if (group_info_ptr)
1148 {
1149 group_name.assign (group_info_ptr->gr_name);
1150 return group_name.c_str();
1151 }
1152 }
1153 else
1154 {
1155 // The threadsafe version isn't currently working
1156 // for me on darwin, but the non-threadsafe version
1157 // is, so I am calling it below.
1158 group_info_ptr = ::getgrgid (gid);
1159 if (group_info_ptr)
1160 {
1161 group_name.assign (group_info_ptr->gr_name);
1162 return group_name.c_str();
1163 }
1164 }
1165 group_name.clear();
1166 return NULL;
1167}
1168
Johnny Chen4b663292011-08-02 20:52:42 +00001169#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001170bool
1171Host::GetOSBuildString (std::string &s)
1172{
1173 s.clear();
1174 return false;
1175}
1176
1177bool
1178Host::GetOSKernelDescription (std::string &s)
1179{
1180 s.clear();
1181 return false;
1182}
Johnny Chen4b663292011-08-02 20:52:42 +00001183#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001184
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001185uint32_t
1186Host::GetUserID ()
1187{
1188 return getuid();
1189}
1190
1191uint32_t
1192Host::GetGroupID ()
1193{
1194 return getgid();
1195}
1196
1197uint32_t
1198Host::GetEffectiveUserID ()
1199{
1200 return geteuid();
1201}
1202
1203uint32_t
1204Host::GetEffectiveGroupID ()
1205{
1206 return getegid();
1207}
1208
1209#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001210uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001211Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001212{
1213 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001214 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001215}
Johnny Chen4b663292011-08-02 20:52:42 +00001216#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001217
Johnny Chen4b663292011-08-02 20:52:42 +00001218#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001219bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001220Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001221{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001222 process_info.Clear();
1223 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001224}
Johnny Chen4b663292011-08-02 20:52:42 +00001225#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001226
Sean Callananf35a96c2011-10-27 21:22:25 +00001227lldb::TargetSP
1228Host::GetDummyTarget (lldb_private::Debugger &debugger)
1229{
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001230 lldb::TargetSP dummy_target_sp;
1231
1232 ArchSpec arch(Target::GetDefaultArchitecture());
1233 if (!arch.IsValid())
1234 arch = Host::GetArchitecture ();
1235 Error err = debugger.GetTargetList().CreateTarget(debugger,
1236 FileSpec(),
1237 arch.GetTriple().getTriple().c_str(),
1238 false,
1239 NULL,
1240 dummy_target_sp);
1241
1242 return dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001243}
1244
Greg Clayton97471182012-04-14 01:42:46 +00001245struct ShellInfo
1246{
1247 ShellInfo () :
1248 process_reaped (false),
1249 can_delete (false),
1250 pid (LLDB_INVALID_PROCESS_ID),
1251 signo(-1),
1252 status(-1)
1253 {
1254 }
1255
1256 lldb_private::Predicate<bool> process_reaped;
1257 lldb_private::Predicate<bool> can_delete;
1258 lldb::pid_t pid;
1259 int signo;
1260 int status;
1261};
1262
1263static bool
1264MonitorShellCommand (void *callback_baton,
1265 lldb::pid_t pid,
1266 bool exited, // True if the process did exit
1267 int signo, // Zero for no signal
1268 int status) // Exit value of process if signal is zero
1269{
1270 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1271 shell_info->pid = pid;
1272 shell_info->signo = signo;
1273 shell_info->status = status;
1274 // Let the thread running Host::RunShellCommand() know that the process
1275 // exited and that ShellInfo has been filled in by broadcasting to it
1276 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1277 // Now wait for a handshake back from that thread running Host::RunShellCommand
1278 // so we know that we can delete shell_info_ptr
1279 shell_info->can_delete.WaitForValueEqualTo(true);
1280 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1281 usleep(1000);
1282 // Now delete the shell info that was passed into this function
1283 delete shell_info;
1284 return true;
1285}
1286
1287Error
1288Host::RunShellCommand (const char *command,
1289 const char *working_dir,
1290 int *status_ptr,
1291 int *signo_ptr,
1292 std::string *command_output_ptr,
1293 uint32_t timeout_sec)
1294{
1295 Error error;
1296 ProcessLaunchInfo launch_info;
1297 launch_info.SetShell("/bin/bash");
1298 launch_info.GetArguments().AppendArgument(command);
1299 const bool localhost = true;
1300 const bool will_debug = false;
1301 const bool first_arg_is_full_shell_command = true;
1302 launch_info.ConvertArgumentsForLaunchingInShell (error,
1303 localhost,
1304 will_debug,
1305 first_arg_is_full_shell_command);
1306
1307 if (working_dir)
1308 launch_info.SetWorkingDirectory(working_dir);
1309 char output_file_path_buffer[L_tmpnam];
1310 const char *output_file_path = NULL;
1311 if (command_output_ptr)
1312 {
1313 // Create a temporary file to get the stdout/stderr and redirect the
1314 // output of the command into this file. We will later read this file
1315 // if all goes well and fill the data into "command_output_ptr"
1316 output_file_path = ::tmpnam(output_file_path_buffer);
1317 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1318 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
1319 launch_info.AppendDuplicateFileAction(STDERR_FILENO, STDOUT_FILENO);
1320 }
1321 else
1322 {
1323 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1324 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1325 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1326 }
1327
1328 // The process monitor callback will delete the 'shell_info_ptr' below...
1329 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1330
1331 const bool monitor_signals = false;
1332 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1333
1334 error = LaunchProcess (launch_info);
1335 const lldb::pid_t pid = launch_info.GetProcessID();
1336 if (pid != LLDB_INVALID_PROCESS_ID)
1337 {
1338 // The process successfully launched, so we can defer ownership of
1339 // "shell_info" to the MonitorShellCommand callback function that will
1340 // get called when the process dies. We release the std::auto_ptr as it
1341 // doesn't need to delete the ShellInfo anymore.
1342 ShellInfo *shell_info = shell_info_ap.release();
1343 TimeValue timeout_time(TimeValue::Now());
1344 timeout_time.OffsetWithSeconds(timeout_sec);
1345 bool timed_out = false;
1346 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1347 if (timed_out)
1348 {
1349 error.SetErrorString("timed out waiting for shell command to complete");
1350
1351 // Kill the process since it didn't complete withint the timeout specified
1352 ::kill (pid, SIGKILL);
1353 // Wait for the monitor callback to get the message
1354 timeout_time = TimeValue::Now();
1355 timeout_time.OffsetWithSeconds(1);
1356 timed_out = false;
1357 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1358 }
1359 else
1360 {
1361 if (status_ptr)
1362 *status_ptr = shell_info->status;
1363
1364 if (signo_ptr)
1365 *signo_ptr = shell_info->signo;
1366
1367 if (command_output_ptr)
1368 {
1369 command_output_ptr->clear();
1370 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1371 uint64_t file_size = file_spec.GetByteSize();
1372 if (file_size > 0)
1373 {
1374 if (file_size > command_output_ptr->max_size())
1375 {
1376 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1377 }
1378 else
1379 {
1380 command_output_ptr->resize(file_size);
1381 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1382 }
1383 }
1384 }
1385 }
1386 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1387 }
1388 else
1389 {
1390 error.SetErrorString("failed to get process ID");
1391 }
1392
1393 if (output_file_path)
1394 ::unlink (output_file_path);
1395 // Handshake with the monitor thread, or just let it know in advance that
1396 // it can delete "shell_info" in case we timed out and were not able to kill
1397 // the process...
1398 return error;
1399}
1400
1401
1402
Johnny Chen4b663292011-08-02 20:52:42 +00001403#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001404bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001405Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001406{
1407 return false;
1408}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001409
Greg Claytone98ac252010-11-10 04:57:04 +00001410void
1411Host::SetCrashDescriptionWithFormat (const char *format, ...)
1412{
1413}
1414
1415void
1416Host::SetCrashDescription (const char *description)
1417{
1418}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001419
1420lldb::pid_t
1421LaunchApplication (const FileSpec &app_file_spec)
1422{
1423 return LLDB_INVALID_PROCESS_ID;
1424}
1425
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001426#endif