blob: aeff1fff6599c7e31e87578cc7b83b47abf692bc [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"
Greg Clayton14ef59f2011-02-08 00:35:34 +000017#include "lldb/Host/Config.h"
Greg Claytoncd548032011-02-01 01:31:41 +000018#include "lldb/Host/Endian.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000019#include "lldb/Host/FileSpec.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000020#include "lldb/Host/Mutex.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000021#include "lldb/Target/Process.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000022#include "lldb/Target/TargetList.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000023
Stephen Wilson7f513ba2011-02-24 19:15:09 +000024#include "llvm/Support/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000025#include "llvm/Support/MachO.h"
Stephen Wilson7f513ba2011-02-24 19:15:09 +000026
Greg Clayton8f3b21d2010-09-07 20:11:56 +000027#include <dlfcn.h>
28#include <errno.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000029#include <grp.h>
Stephen Wilsonec2d9782011-04-08 13:36:44 +000030#include <limits.h>
Greg Clayton58e26e02011-03-24 04:28:38 +000031#include <netdb.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000032#include <pwd.h>
33#include <sys/types.h>
34
Greg Clayton8f3b21d2010-09-07 20:11:56 +000035
36#if defined (__APPLE__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000037
Greg Clayton49ce6822010-10-31 03:01:06 +000038#include <dispatch/dispatch.h>
Greg Clayton8f3b21d2010-09-07 20:11:56 +000039#include <libproc.h>
40#include <mach-o/dyld.h>
Greg Claytonb5f67fb2011-02-05 06:36:35 +000041#include <sys/sysctl.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000042
Greg Clayton24bc5d92011-03-30 18:16:51 +000043
Greg Clayton0f577c22011-02-07 17:43:47 +000044#elif defined (__linux__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000045
Greg Clayton0f577c22011-02-07 17:43:47 +000046#include <sys/wait.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000047
Johnny Chen4b663292011-08-02 20:52:42 +000048#elif defined (__FreeBSD__)
49
50#include <sys/wait.h>
51#include <sys/sysctl.h>
52#include <pthread_np.h>
53
Greg Clayton8f3b21d2010-09-07 20:11:56 +000054#endif
55
56using namespace lldb;
57using namespace lldb_private;
58
Greg Clayton1c4642c2011-11-16 05:37:56 +000059
Greg Claytonc518fe72011-11-17 19:41:57 +000060#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +000061struct MonitorInfo
62{
63 lldb::pid_t pid; // The process ID to monitor
64 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
65 void *callback_baton; // The callback baton for the callback function
66 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
67};
68
69static void *
70MonitorChildProcessThreadFunction (void *arg);
71
72lldb::thread_t
73Host::StartMonitoringChildProcess
74(
75 Host::MonitorChildProcessCallback callback,
76 void *callback_baton,
77 lldb::pid_t pid,
78 bool monitor_signals
79)
80{
81 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton1c4642c2011-11-16 05:37:56 +000082 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton8f3b21d2010-09-07 20:11:56 +000083
Greg Clayton1c4642c2011-11-16 05:37:56 +000084 info_ptr->pid = pid;
85 info_ptr->callback = callback;
86 info_ptr->callback_baton = callback_baton;
87 info_ptr->monitor_signals = monitor_signals;
88
89 char thread_name[256];
90 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid);
91 thread = ThreadCreate (thread_name,
92 MonitorChildProcessThreadFunction,
93 info_ptr,
94 NULL);
95
Greg Clayton8f3b21d2010-09-07 20:11:56 +000096 return thread;
97}
98
99//------------------------------------------------------------------
100// Scoped class that will disable thread canceling when it is
101// constructed, and exception safely restore the previous value it
102// when it goes out of scope.
103//------------------------------------------------------------------
104class ScopedPThreadCancelDisabler
105{
106public:
107 ScopedPThreadCancelDisabler()
108 {
109 // Disable the ability for this thread to be cancelled
110 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
111 if (err != 0)
112 m_old_state = -1;
113
114 }
115
116 ~ScopedPThreadCancelDisabler()
117 {
118 // Restore the ability for this thread to be cancelled to what it
119 // previously was.
120 if (m_old_state != -1)
121 ::pthread_setcancelstate (m_old_state, 0);
122 }
123private:
124 int m_old_state; // Save the old cancelability state.
125};
126
127static void *
128MonitorChildProcessThreadFunction (void *arg)
129{
Greg Claytone005f2c2010-11-06 01:53:30 +0000130 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000131 const char *function = __FUNCTION__;
132 if (log)
133 log->Printf ("%s (arg = %p) thread starting...", function, arg);
134
135 MonitorInfo *info = (MonitorInfo *)arg;
136
137 const Host::MonitorChildProcessCallback callback = info->callback;
138 void * const callback_baton = info->callback_baton;
139 const lldb::pid_t pid = info->pid;
140 const bool monitor_signals = info->monitor_signals;
141
142 delete info;
143
144 int status = -1;
145 const int options = 0;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000146 while (1)
147 {
Caroline Tice926060e2010-10-29 21:48:37 +0000148 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000149 if (log)
Greg Clayton1c4642c2011-11-16 05:37:56 +0000150 log->Printf("%s ::wait_pid (pid = %i, &status, options = %i)...", function, pid, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000151
152 // Wait for all child processes
153 ::pthread_testcancel ();
Greg Clayton1c4642c2011-11-16 05:37:56 +0000154 const lldb::pid_t wait_pid = ::waitpid (pid, &status, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000155 ::pthread_testcancel ();
156
157 if (wait_pid == -1)
158 {
159 if (errno == EINTR)
160 continue;
161 else
162 break;
163 }
164 else if (wait_pid == pid)
165 {
166 bool exited = false;
167 int signal = 0;
168 int exit_status = 0;
169 const char *status_cstr = NULL;
170 if (WIFSTOPPED(status))
171 {
172 signal = WSTOPSIG(status);
173 status_cstr = "STOPPED";
174 }
175 else if (WIFEXITED(status))
176 {
177 exit_status = WEXITSTATUS(status);
178 status_cstr = "EXITED";
179 exited = true;
180 }
181 else if (WIFSIGNALED(status))
182 {
183 signal = WTERMSIG(status);
184 status_cstr = "SIGNALED";
185 exited = true;
186 exit_status = -1;
187 }
188 else
189 {
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000190 status_cstr = "(\?\?\?)";
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000191 }
192
193 // Scope for pthread_cancel_disabler
194 {
195 ScopedPThreadCancelDisabler pthread_cancel_disabler;
196
Caroline Tice926060e2010-10-29 21:48:37 +0000197 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000198 if (log)
Greg Clayton1c4642c2011-11-16 05:37:56 +0000199 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 +0000200 function,
201 wait_pid,
202 options,
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000203 pid,
204 status,
205 status_cstr,
206 signal,
207 exit_status);
208
209 if (exited || (signal != 0 && monitor_signals))
210 {
Greg Clayton1c4642c2011-11-16 05:37:56 +0000211 bool callback_return = false;
212 if (callback)
213 callback_return = callback (callback_baton, pid, exited, signal, exit_status);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000214
215 // If our process exited, then this thread should exit
216 if (exited)
217 break;
218 // If the callback returns true, it means this process should
219 // exit
220 if (callback_return)
221 break;
222 }
223 }
224 }
225 }
226
Caroline Tice926060e2010-10-29 21:48:37 +0000227 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000228 if (log)
229 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
230
231 return NULL;
232}
233
Greg Claytondf6dc882012-01-05 03:57:59 +0000234
235void
236Host::SystemLog (SystemLogType type, const char *format, va_list args)
237{
238 vfprintf (stderr, format, args);
239}
240
Greg Clayton1c4642c2011-11-16 05:37:56 +0000241#endif // #if !defined (__APPLE__)
242
Greg Claytondf6dc882012-01-05 03:57:59 +0000243void
244Host::SystemLog (SystemLogType type, const char *format, ...)
245{
246 va_list args;
247 va_start (args, format);
248 SystemLog (type, format, args);
249 va_end (args);
250}
251
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000252size_t
253Host::GetPageSize()
254{
255 return ::getpagesize();
256}
257
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000258const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000259Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000260{
Greg Clayton395fc332011-02-15 21:59:32 +0000261 static bool g_supports_32 = false;
262 static bool g_supports_64 = false;
263 static ArchSpec g_host_arch_32;
264 static ArchSpec g_host_arch_64;
265
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000266#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000267
268 // Apple is different in that it can support both 32 and 64 bit executables
269 // in the same operating system running concurrently. Here we detect the
270 // correct host architectures for both 32 and 64 bit including if 64 bit
271 // executables are supported on the system.
272
273 if (g_supports_32 == false && g_supports_64 == false)
274 {
275 // All apple systems support 32 bit execution.
276 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000277 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000278 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000279 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000280 ArchSpec host_arch;
281 // These will tell us about the kernel architecture, which even on a 64
282 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000283 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
284 {
Greg Clayton395fc332011-02-15 21:59:32 +0000285 len = sizeof (cpusubtype);
286 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
287 cpusubtype = CPU_TYPE_ANY;
288
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000289 len = sizeof (is_64_bit_capable);
290 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
291 {
292 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000293 g_supports_64 = true;
294 }
295
296 if (is_64_bit_capable)
297 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000298#if defined (__i386__) || defined (__x86_64__)
299 if (cpusubtype == CPU_SUBTYPE_486)
300 cpusubtype = CPU_SUBTYPE_I386_ALL;
301#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000302 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000303 {
Greg Clayton395fc332011-02-15 21:59:32 +0000304 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000305 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
306 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000307 }
308 else
309 {
310 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000311 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000312 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000313 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000314 }
315 }
Greg Clayton395fc332011-02-15 21:59:32 +0000316 else
317 {
Greg Claytonb3448432011-03-24 21:19:54 +0000318 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000319 g_host_arch_64.Clear();
320 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000321 }
Greg Clayton395fc332011-02-15 21:59:32 +0000322 }
323
324#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000325
Greg Clayton395fc332011-02-15 21:59:32 +0000326 if (g_supports_32 == false && g_supports_64 == false)
327 {
Peter Collingbourneb47c9982011-11-05 01:35:31 +0000328 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000329
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000330 g_host_arch_32.Clear();
331 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000332
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000333 switch (triple.getArch())
334 {
335 default:
336 g_host_arch_32.SetTriple(triple);
337 g_supports_32 = true;
338 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000339
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000340 case llvm::Triple::x86_64:
341 case llvm::Triple::sparcv9:
342 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000343 case llvm::Triple::cellspu:
344 g_host_arch_64.SetTriple(triple);
345 g_supports_64 = true;
346 break;
347 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000348
349 g_supports_32 = g_host_arch_32.IsValid();
350 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000351 }
Greg Clayton395fc332011-02-15 21:59:32 +0000352
353#endif // #else for #if defined (__APPLE__)
354
355 if (arch_kind == eSystemDefaultArchitecture32)
356 return g_host_arch_32;
357 else if (arch_kind == eSystemDefaultArchitecture64)
358 return g_host_arch_64;
359
360 if (g_supports_64)
361 return g_host_arch_64;
362
363 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000364}
365
366const ConstString &
367Host::GetVendorString()
368{
369 static ConstString g_vendor;
370 if (!g_vendor)
371 {
372#if defined (__APPLE__)
373 char ostype[64];
374 size_t len = sizeof(ostype);
375 if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0)
376 g_vendor.SetCString (ostype);
377 else
378 g_vendor.SetCString("apple");
379#elif defined (__linux__)
380 g_vendor.SetCString("gnu");
Johnny Chen4b663292011-08-02 20:52:42 +0000381#elif defined (__FreeBSD__)
382 g_vendor.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000383#endif
384 }
385 return g_vendor;
386}
387
388const ConstString &
389Host::GetOSString()
390{
391 static ConstString g_os_string;
392 if (!g_os_string)
393 {
394#if defined (__APPLE__)
395 g_os_string.SetCString("darwin");
396#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 {
411 StreamString triple;
412 triple.Printf("%s-%s-%s",
Greg Clayton940b1032011-02-23 00:35:02 +0000413 GetArchitecture().GetArchitectureName(),
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000414 GetVendorString().AsCString(),
415 GetOSString().AsCString());
416
417 std::transform (triple.GetString().begin(),
418 triple.GetString().end(),
419 triple.GetString().begin(),
420 ::tolower);
421
422 g_host_triple.SetCString(triple.GetString().c_str());
423 }
424 return g_host_triple;
425}
426
427lldb::pid_t
428Host::GetCurrentProcessID()
429{
430 return ::getpid();
431}
432
433lldb::tid_t
434Host::GetCurrentThreadID()
435{
436#if defined (__APPLE__)
437 return ::mach_thread_self();
Johnny Chen4b663292011-08-02 20:52:42 +0000438#elif defined(__FreeBSD__)
439 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000440#else
441 return lldb::tid_t(pthread_self());
442#endif
443}
444
Jim Ingham1831e782012-04-07 00:00:41 +0000445lldb::thread_t
446Host::GetCurrentThread ()
447{
448 return lldb::thread_t(pthread_self());
449}
450
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000451const char *
452Host::GetSignalAsCString (int signo)
453{
454 switch (signo)
455 {
456 case SIGHUP: return "SIGHUP"; // 1 hangup
457 case SIGINT: return "SIGINT"; // 2 interrupt
458 case SIGQUIT: return "SIGQUIT"; // 3 quit
459 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
460 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
461 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000462#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000463 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000464#endif
465#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000466 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000467#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000468 case SIGFPE: return "SIGFPE"; // 8 floating point exception
469 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
470 case SIGBUS: return "SIGBUS"; // 10 bus error
471 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
472 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
473 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
474 case SIGALRM: return "SIGALRM"; // 14 alarm clock
475 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
476 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
477 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
478 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
479 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
480 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
481 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
482 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
483#if !defined(_POSIX_C_SOURCE)
484 case SIGIO: return "SIGIO"; // 23 input/output possible signal
485#endif
486 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
487 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
488 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
489 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
490#if !defined(_POSIX_C_SOURCE)
491 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
492 case SIGINFO: return "SIGINFO"; // 29 information request
493#endif
494 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
495 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
496 default:
497 break;
498 }
499 return NULL;
500}
501
502void
503Host::WillTerminate ()
504{
505}
506
Johnny Chen4b663292011-08-02 20:52:42 +0000507#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000508void
509Host::ThreadCreated (const char *thread_name)
510{
511}
Greg Claytonb749a262010-12-03 06:02:24 +0000512
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000513void
Greg Claytonb749a262010-12-03 06:02:24 +0000514Host::Backtrace (Stream &strm, uint32_t max_frames)
515{
Greg Clayton52fd9842011-02-02 02:24:04 +0000516 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000517}
518
Greg Clayton638351a2010-12-04 00:10:17 +0000519size_t
520Host::GetEnvironment (StringList &env)
521{
Greg Clayton52fd9842011-02-02 02:24:04 +0000522 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000523 return 0;
524}
525
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000526#endif
527
528struct HostThreadCreateInfo
529{
530 std::string thread_name;
531 thread_func_t thread_fptr;
532 thread_arg_t thread_arg;
533
534 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
535 thread_name (name ? name : ""),
536 thread_fptr (fptr),
537 thread_arg (arg)
538 {
539 }
540};
541
542static thread_result_t
543ThreadCreateTrampoline (thread_arg_t arg)
544{
545 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
546 Host::ThreadCreated (info->thread_name.c_str());
547 thread_func_t thread_fptr = info->thread_fptr;
548 thread_arg_t thread_arg = info->thread_arg;
549
Greg Claytone005f2c2010-11-06 01:53:30 +0000550 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000551 if (log)
552 log->Printf("thread created");
553
554 delete info;
555 return thread_fptr (thread_arg);
556}
557
558lldb::thread_t
559Host::ThreadCreate
560(
561 const char *thread_name,
562 thread_func_t thread_fptr,
563 thread_arg_t thread_arg,
564 Error *error
565)
566{
567 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
568
569 // Host::ThreadCreateTrampoline will delete this pointer for us.
570 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
571
572 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
573 if (err == 0)
574 {
575 if (error)
576 error->Clear();
577 return thread;
578 }
579
580 if (error)
581 error->SetError (err, eErrorTypePOSIX);
582
583 return LLDB_INVALID_HOST_THREAD;
584}
585
586bool
587Host::ThreadCancel (lldb::thread_t thread, Error *error)
588{
589 int err = ::pthread_cancel (thread);
590 if (error)
591 error->SetError(err, eErrorTypePOSIX);
592 return err == 0;
593}
594
595bool
596Host::ThreadDetach (lldb::thread_t thread, Error *error)
597{
598 int err = ::pthread_detach (thread);
599 if (error)
600 error->SetError(err, eErrorTypePOSIX);
601 return err == 0;
602}
603
604bool
605Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
606{
607 int err = ::pthread_join (thread, thread_result_ptr);
608 if (error)
609 error->SetError(err, eErrorTypePOSIX);
610 return err == 0;
611}
612
613//------------------------------------------------------------------
614// Control access to a static file thread name map using a single
615// static function to avoid a static constructor.
616//------------------------------------------------------------------
617static const char *
618ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
619{
620 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
621
622 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
623 Mutex::Locker locker(&g_mutex);
624
625 typedef std::map<uint64_t, std::string> thread_name_map;
626 // rdar://problem/8153284
627 // Fixed a crasher where during shutdown, loggings attempted to access the
628 // thread name but the static map instance had already been destructed.
629 // Another approach is to introduce a static guard object which monitors its
630 // own destruction and raises a flag, but this incurs more overhead.
631 static thread_name_map *g_thread_names_ptr = new thread_name_map();
632 thread_name_map &g_thread_names = *g_thread_names_ptr;
633
634 if (get)
635 {
636 // See if the thread name exists in our thread name pool
637 thread_name_map::iterator pos = g_thread_names.find(pid_tid);
638 if (pos != g_thread_names.end())
639 return pos->second.c_str();
640 }
641 else
642 {
643 // Set the thread name
644 g_thread_names[pid_tid] = name;
645 }
646 return NULL;
647}
648
649const char *
650Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
651{
652 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
653 if (name == NULL)
654 {
655#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
656 // We currently can only get the name of a thread in the current process.
657 if (pid == Host::GetCurrentProcessID())
658 {
659 char pthread_name[1024];
660 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
661 {
662 if (pthread_name[0])
663 {
664 // Set the thread in our string pool
665 ThreadNameAccessor (false, pid, tid, pthread_name);
666 // Get our copy of the thread name string
667 name = ThreadNameAccessor (true, pid, tid, NULL);
668 }
669 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000670
671 if (name == NULL)
672 {
673 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
674 if (current_queue != NULL)
675 name = dispatch_queue_get_label (current_queue);
676 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000677 }
678#endif
679 }
680 return name;
681}
682
683void
684Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
685{
686 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
687 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
688 if (pid == LLDB_INVALID_PROCESS_ID)
689 pid = curr_pid;
690
691 if (tid == LLDB_INVALID_THREAD_ID)
692 tid = curr_tid;
693
694#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
695 // Set the pthread name if possible
696 if (pid == curr_pid && tid == curr_tid)
697 {
698 ::pthread_setname_np (name);
699 }
700#endif
701 ThreadNameAccessor (false, pid, tid, name);
702}
703
704FileSpec
705Host::GetProgramFileSpec ()
706{
707 static FileSpec g_program_filespec;
708 if (!g_program_filespec)
709 {
710#if defined (__APPLE__)
711 char program_fullpath[PATH_MAX];
712 // If DST is NULL, then return the number of bytes needed.
713 uint32_t len = sizeof(program_fullpath);
714 int err = _NSGetExecutablePath (program_fullpath, &len);
715 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000716 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000717 else if (err == -1)
718 {
719 char *large_program_fullpath = (char *)::malloc (len + 1);
720
721 err = _NSGetExecutablePath (large_program_fullpath, &len);
722 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000723 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000724
725 ::free (large_program_fullpath);
726 }
727#elif defined (__linux__)
728 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000729 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
730 if (len > 0) {
731 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000732 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000733 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000734#elif defined (__FreeBSD__)
735 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
736 size_t exe_path_size;
737 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
738 {
Greg Clayton366795e2011-01-13 01:27:55 +0000739 char *exe_path = new char[exe_path_size];
740 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
741 g_program_filespec.SetFile(exe_path, false);
742 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000743 }
744#endif
745 }
746 return g_program_filespec;
747}
748
749FileSpec
750Host::GetModuleFileSpecForHostAddress (const void *host_addr)
751{
752 FileSpec module_filespec;
753 Dl_info info;
754 if (::dladdr (host_addr, &info))
755 {
756 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000757 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000758 }
759 return module_filespec;
760}
761
762#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000763
764bool
765Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
766{
767 bundle.Clear();
768 return false;
769}
770
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000771bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000772Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000773{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000774 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000775}
776#endif
777
Greg Clayton14ef59f2011-02-08 00:35:34 +0000778// Opaque info that tracks a dynamic library that was loaded
779struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000780{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000781 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
782 file_spec (fs),
783 open_options (o),
784 handle (h)
785 {
786 }
787
788 const FileSpec file_spec;
789 uint32_t open_options;
790 void * handle;
791};
792
793void *
794Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
795{
Greg Clayton52fd9842011-02-02 02:24:04 +0000796 char path[PATH_MAX];
797 if (file_spec.GetPath(path, sizeof(path)))
798 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000799 int mode = 0;
800
801 if (options & eDynamicLibraryOpenOptionLazy)
802 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000803 else
804 mode |= RTLD_NOW;
805
Greg Clayton14ef59f2011-02-08 00:35:34 +0000806
807 if (options & eDynamicLibraryOpenOptionLocal)
808 mode |= RTLD_LOCAL;
809 else
810 mode |= RTLD_GLOBAL;
811
812#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
813 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
814 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000815#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000816
817 void * opaque = ::dlopen (path, mode);
818
819 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000820 {
821 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000822 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000823 }
824 else
825 {
826 error.SetErrorString(::dlerror());
827 }
828 }
829 else
830 {
831 error.SetErrorString("failed to extract path");
832 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000833 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000834}
835
836Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000837Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000838{
839 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000840 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000841 {
842 error.SetErrorString ("invalid dynamic library handle");
843 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000844 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000845 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000846 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
847 if (::dlclose (dylib_info->handle) != 0)
848 {
849 error.SetErrorString(::dlerror());
850 }
851
852 dylib_info->open_options = 0;
853 dylib_info->handle = 0;
854 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000855 }
856 return error;
857}
858
859void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000860Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000861{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000862 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000863 {
864 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000865 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000866 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000867 {
868 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
869
870 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
871 if (symbol_addr)
872 {
873#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
874 // This host doesn't support limiting searches to this shared library
875 // so we need to verify that the match came from this shared library
876 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000877 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000878 {
879 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
880 if (match_dylib_spec != dylib_info->file_spec)
881 {
882 char dylib_path[PATH_MAX];
883 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
884 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
885 else
886 error.SetErrorString ("symbol not found");
887 return NULL;
888 }
889 }
890#endif
891 error.Clear();
892 return symbol_addr;
893 }
894 else
895 {
896 error.SetErrorString(::dlerror());
897 }
898 }
899 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000900}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000901
902bool
903Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
904{
Greg Clayton5d187e52011-01-08 20:28:42 +0000905 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000906 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
907 // on linux this is assumed to be the "lldb" main executable. If LLDB on
908 // linux is actually in a shared library (lldb.so??) then this function will
909 // need to be modified to "do the right thing".
910
911 switch (path_type)
912 {
913 case ePathTypeLLDBShlibDir:
914 {
915 static ConstString g_lldb_so_dir;
916 if (!g_lldb_so_dir)
917 {
918 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
919 g_lldb_so_dir = lldb_file_spec.GetDirectory();
920 }
921 file_spec.GetDirectory() = g_lldb_so_dir;
922 return file_spec.GetDirectory();
923 }
924 break;
925
926 case ePathTypeSupportExecutableDir:
927 {
928 static ConstString g_lldb_support_exe_dir;
929 if (!g_lldb_support_exe_dir)
930 {
931 FileSpec lldb_file_spec;
932 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
933 {
934 char raw_path[PATH_MAX];
935 char resolved_path[PATH_MAX];
936 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
937
938#if defined (__APPLE__)
939 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
940 if (framework_pos)
941 {
942 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000943#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000944 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000945#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000946 }
947#endif
948 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
949 g_lldb_support_exe_dir.SetCString(resolved_path);
950 }
951 }
952 file_spec.GetDirectory() = g_lldb_support_exe_dir;
953 return file_spec.GetDirectory();
954 }
955 break;
956
957 case ePathTypeHeaderDir:
958 {
959 static ConstString g_lldb_headers_dir;
960 if (!g_lldb_headers_dir)
961 {
962#if defined (__APPLE__)
963 FileSpec lldb_file_spec;
964 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
965 {
966 char raw_path[PATH_MAX];
967 char resolved_path[PATH_MAX];
968 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
969
970 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
971 if (framework_pos)
972 {
973 framework_pos += strlen("LLDB.framework");
974 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
975 }
976 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
977 g_lldb_headers_dir.SetCString(resolved_path);
978 }
979#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000980 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000981 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
982#endif
983 }
984 file_spec.GetDirectory() = g_lldb_headers_dir;
985 return file_spec.GetDirectory();
986 }
987 break;
988
989 case ePathTypePythonDir:
990 {
Greg Clayton52fd9842011-02-02 02:24:04 +0000991 // TODO: Anyone know how we can determine this for linux? Other systems?
Greg Clayton24b48ff2010-10-17 22:03:32 +0000992 // For linux we are currently assuming the location of the lldb
993 // binary that contains this function is the directory that will
994 // contain lldb.so, lldb.py and embedded_interpreter.py...
995
996 static ConstString g_lldb_python_dir;
997 if (!g_lldb_python_dir)
998 {
999 FileSpec lldb_file_spec;
1000 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1001 {
1002 char raw_path[PATH_MAX];
1003 char resolved_path[PATH_MAX];
1004 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1005
1006#if defined (__APPLE__)
1007 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1008 if (framework_pos)
1009 {
1010 framework_pos += strlen("LLDB.framework");
1011 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1012 }
1013#endif
1014 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1015 g_lldb_python_dir.SetCString(resolved_path);
1016 }
1017 }
1018 file_spec.GetDirectory() = g_lldb_python_dir;
1019 return file_spec.GetDirectory();
1020 }
1021 break;
1022
Greg Clayton52fd9842011-02-02 02:24:04 +00001023 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1024 {
1025#if defined (__APPLE__)
1026 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001027 static bool g_lldb_system_plugin_dir_located = false;
1028 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001029 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001030 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001031 FileSpec lldb_file_spec;
1032 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1033 {
1034 char raw_path[PATH_MAX];
1035 char resolved_path[PATH_MAX];
1036 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1037
1038 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1039 if (framework_pos)
1040 {
1041 framework_pos += strlen("LLDB.framework");
1042 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001043 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1044 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001045 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001046 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001047 }
1048 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001049
1050 if (g_lldb_system_plugin_dir)
1051 {
1052 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1053 return true;
1054 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001055#endif
1056 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1057 return false;
1058 }
1059 break;
1060
1061 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1062 {
1063#if defined (__APPLE__)
1064 static ConstString g_lldb_user_plugin_dir;
1065 if (!g_lldb_user_plugin_dir)
1066 {
1067 char user_plugin_path[PATH_MAX];
1068 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1069 user_plugin_path,
1070 sizeof(user_plugin_path)))
1071 {
1072 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1073 }
1074 }
1075 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1076 return file_spec.GetDirectory();
1077#endif
1078 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1079 return false;
1080 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001081 default:
1082 assert (!"Unhandled PathType");
1083 break;
1084 }
1085
1086 return false;
1087}
1088
Greg Clayton58e26e02011-03-24 04:28:38 +00001089
1090bool
1091Host::GetHostname (std::string &s)
1092{
1093 char hostname[PATH_MAX];
1094 hostname[sizeof(hostname) - 1] = '\0';
1095 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1096 {
1097 struct hostent* h = ::gethostbyname (hostname);
1098 if (h)
1099 s.assign (h->h_name);
1100 else
1101 s.assign (hostname);
1102 return true;
1103 }
1104 return false;
1105}
1106
Greg Clayton24bc5d92011-03-30 18:16:51 +00001107const char *
1108Host::GetUserName (uint32_t uid, std::string &user_name)
1109{
1110 struct passwd user_info;
1111 struct passwd *user_info_ptr = &user_info;
1112 char user_buffer[PATH_MAX];
1113 size_t user_buffer_size = sizeof(user_buffer);
1114 if (::getpwuid_r (uid,
1115 &user_info,
1116 user_buffer,
1117 user_buffer_size,
1118 &user_info_ptr) == 0)
1119 {
1120 if (user_info_ptr)
1121 {
1122 user_name.assign (user_info_ptr->pw_name);
1123 return user_name.c_str();
1124 }
1125 }
1126 user_name.clear();
1127 return NULL;
1128}
1129
1130const char *
1131Host::GetGroupName (uint32_t gid, std::string &group_name)
1132{
1133 char group_buffer[PATH_MAX];
1134 size_t group_buffer_size = sizeof(group_buffer);
1135 struct group group_info;
1136 struct group *group_info_ptr = &group_info;
1137 // Try the threadsafe version first
1138 if (::getgrgid_r (gid,
1139 &group_info,
1140 group_buffer,
1141 group_buffer_size,
1142 &group_info_ptr) == 0)
1143 {
1144 if (group_info_ptr)
1145 {
1146 group_name.assign (group_info_ptr->gr_name);
1147 return group_name.c_str();
1148 }
1149 }
1150 else
1151 {
1152 // The threadsafe version isn't currently working
1153 // for me on darwin, but the non-threadsafe version
1154 // is, so I am calling it below.
1155 group_info_ptr = ::getgrgid (gid);
1156 if (group_info_ptr)
1157 {
1158 group_name.assign (group_info_ptr->gr_name);
1159 return group_name.c_str();
1160 }
1161 }
1162 group_name.clear();
1163 return NULL;
1164}
1165
Johnny Chen4b663292011-08-02 20:52:42 +00001166#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001167bool
1168Host::GetOSBuildString (std::string &s)
1169{
1170 s.clear();
1171 return false;
1172}
1173
1174bool
1175Host::GetOSKernelDescription (std::string &s)
1176{
1177 s.clear();
1178 return false;
1179}
Johnny Chen4b663292011-08-02 20:52:42 +00001180#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001181
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001182uint32_t
1183Host::GetUserID ()
1184{
1185 return getuid();
1186}
1187
1188uint32_t
1189Host::GetGroupID ()
1190{
1191 return getgid();
1192}
1193
1194uint32_t
1195Host::GetEffectiveUserID ()
1196{
1197 return geteuid();
1198}
1199
1200uint32_t
1201Host::GetEffectiveGroupID ()
1202{
1203 return getegid();
1204}
1205
1206#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001207uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001208Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001209{
1210 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001211 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001212}
Johnny Chen4b663292011-08-02 20:52:42 +00001213#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001214
Johnny Chen4b663292011-08-02 20:52:42 +00001215#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001216bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001217Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001218{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001219 process_info.Clear();
1220 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001221}
Johnny Chen4b663292011-08-02 20:52:42 +00001222#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001223
Sean Callananf35a96c2011-10-27 21:22:25 +00001224lldb::TargetSP
1225Host::GetDummyTarget (lldb_private::Debugger &debugger)
1226{
1227 static TargetSP dummy_target;
1228
1229 if (!dummy_target)
1230 {
1231 Error err = debugger.GetTargetList().CreateTarget(debugger,
1232 FileSpec(),
1233 Host::GetTargetTriple().AsCString(),
1234 false,
1235 NULL,
1236 dummy_target);
1237 }
1238
1239 return dummy_target;
1240}
1241
Greg Clayton97471182012-04-14 01:42:46 +00001242struct ShellInfo
1243{
1244 ShellInfo () :
1245 process_reaped (false),
1246 can_delete (false),
1247 pid (LLDB_INVALID_PROCESS_ID),
1248 signo(-1),
1249 status(-1)
1250 {
1251 }
1252
1253 lldb_private::Predicate<bool> process_reaped;
1254 lldb_private::Predicate<bool> can_delete;
1255 lldb::pid_t pid;
1256 int signo;
1257 int status;
1258};
1259
1260static bool
1261MonitorShellCommand (void *callback_baton,
1262 lldb::pid_t pid,
1263 bool exited, // True if the process did exit
1264 int signo, // Zero for no signal
1265 int status) // Exit value of process if signal is zero
1266{
1267 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1268 shell_info->pid = pid;
1269 shell_info->signo = signo;
1270 shell_info->status = status;
1271 // Let the thread running Host::RunShellCommand() know that the process
1272 // exited and that ShellInfo has been filled in by broadcasting to it
1273 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1274 // Now wait for a handshake back from that thread running Host::RunShellCommand
1275 // so we know that we can delete shell_info_ptr
1276 shell_info->can_delete.WaitForValueEqualTo(true);
1277 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1278 usleep(1000);
1279 // Now delete the shell info that was passed into this function
1280 delete shell_info;
1281 return true;
1282}
1283
1284Error
1285Host::RunShellCommand (const char *command,
1286 const char *working_dir,
1287 int *status_ptr,
1288 int *signo_ptr,
1289 std::string *command_output_ptr,
1290 uint32_t timeout_sec)
1291{
1292 Error error;
1293 ProcessLaunchInfo launch_info;
1294 launch_info.SetShell("/bin/bash");
1295 launch_info.GetArguments().AppendArgument(command);
1296 const bool localhost = true;
1297 const bool will_debug = false;
1298 const bool first_arg_is_full_shell_command = true;
1299 launch_info.ConvertArgumentsForLaunchingInShell (error,
1300 localhost,
1301 will_debug,
1302 first_arg_is_full_shell_command);
1303
1304 if (working_dir)
1305 launch_info.SetWorkingDirectory(working_dir);
1306 char output_file_path_buffer[L_tmpnam];
1307 const char *output_file_path = NULL;
1308 if (command_output_ptr)
1309 {
1310 // Create a temporary file to get the stdout/stderr and redirect the
1311 // output of the command into this file. We will later read this file
1312 // if all goes well and fill the data into "command_output_ptr"
1313 output_file_path = ::tmpnam(output_file_path_buffer);
1314 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1315 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
1316 launch_info.AppendDuplicateFileAction(STDERR_FILENO, STDOUT_FILENO);
1317 }
1318 else
1319 {
1320 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1321 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1322 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1323 }
1324
1325 // The process monitor callback will delete the 'shell_info_ptr' below...
1326 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1327
1328 const bool monitor_signals = false;
1329 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1330
1331 error = LaunchProcess (launch_info);
1332 const lldb::pid_t pid = launch_info.GetProcessID();
1333 if (pid != LLDB_INVALID_PROCESS_ID)
1334 {
1335 // The process successfully launched, so we can defer ownership of
1336 // "shell_info" to the MonitorShellCommand callback function that will
1337 // get called when the process dies. We release the std::auto_ptr as it
1338 // doesn't need to delete the ShellInfo anymore.
1339 ShellInfo *shell_info = shell_info_ap.release();
1340 TimeValue timeout_time(TimeValue::Now());
1341 timeout_time.OffsetWithSeconds(timeout_sec);
1342 bool timed_out = false;
1343 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1344 if (timed_out)
1345 {
1346 error.SetErrorString("timed out waiting for shell command to complete");
1347
1348 // Kill the process since it didn't complete withint the timeout specified
1349 ::kill (pid, SIGKILL);
1350 // Wait for the monitor callback to get the message
1351 timeout_time = TimeValue::Now();
1352 timeout_time.OffsetWithSeconds(1);
1353 timed_out = false;
1354 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1355 }
1356 else
1357 {
1358 if (status_ptr)
1359 *status_ptr = shell_info->status;
1360
1361 if (signo_ptr)
1362 *signo_ptr = shell_info->signo;
1363
1364 if (command_output_ptr)
1365 {
1366 command_output_ptr->clear();
1367 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1368 uint64_t file_size = file_spec.GetByteSize();
1369 if (file_size > 0)
1370 {
1371 if (file_size > command_output_ptr->max_size())
1372 {
1373 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1374 }
1375 else
1376 {
1377 command_output_ptr->resize(file_size);
1378 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1379 }
1380 }
1381 }
1382 }
1383 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1384 }
1385 else
1386 {
1387 error.SetErrorString("failed to get process ID");
1388 }
1389
1390 if (output_file_path)
1391 ::unlink (output_file_path);
1392 // Handshake with the monitor thread, or just let it know in advance that
1393 // it can delete "shell_info" in case we timed out and were not able to kill
1394 // the process...
1395 return error;
1396}
1397
1398
1399
Johnny Chen4b663292011-08-02 20:52:42 +00001400#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001401bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001402Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001403{
1404 return false;
1405}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001406
Greg Claytone98ac252010-11-10 04:57:04 +00001407void
1408Host::SetCrashDescriptionWithFormat (const char *format, ...)
1409{
1410}
1411
1412void
1413Host::SetCrashDescription (const char *description)
1414{
1415}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001416
1417lldb::pid_t
1418LaunchApplication (const FileSpec &app_file_spec)
1419{
1420 return LLDB_INVALID_PROCESS_ID;
1421}
1422
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001423#endif