blob: 515070e59304146c1b5ed564fe1b884477b3c397 [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"
13#include "lldb/Core/Error.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000014#include "lldb/Core/Log.h"
15#include "lldb/Core/StreamString.h"
Greg Clayton14ef59f2011-02-08 00:35:34 +000016#include "lldb/Host/Config.h"
Greg Claytoncd548032011-02-01 01:31:41 +000017#include "lldb/Host/Endian.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000018#include "lldb/Host/FileSpec.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000019#include "lldb/Host/Mutex.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000020#include "lldb/Target/Process.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000021
Stephen Wilson7f513ba2011-02-24 19:15:09 +000022#include "llvm/Support/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000023#include "llvm/Support/MachO.h"
Stephen Wilson7f513ba2011-02-24 19:15:09 +000024
Greg Clayton8f3b21d2010-09-07 20:11:56 +000025#include <dlfcn.h>
26#include <errno.h>
Greg Clayton58e26e02011-03-24 04:28:38 +000027#include <netdb.h>
Greg Clayton8f3b21d2010-09-07 20:11:56 +000028
29#if defined (__APPLE__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000030
Greg Clayton49ce6822010-10-31 03:01:06 +000031#include <dispatch/dispatch.h>
Greg Clayton8f3b21d2010-09-07 20:11:56 +000032#include <libproc.h>
33#include <mach-o/dyld.h>
Greg Claytonb5f67fb2011-02-05 06:36:35 +000034#include <sys/sysctl.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000035
Greg Clayton0f577c22011-02-07 17:43:47 +000036#elif defined (__linux__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000037
Greg Clayton0f577c22011-02-07 17:43:47 +000038#include <sys/wait.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000039
Greg Clayton8f3b21d2010-09-07 20:11:56 +000040#endif
41
42using namespace lldb;
43using namespace lldb_private;
44
45struct MonitorInfo
46{
47 lldb::pid_t pid; // The process ID to monitor
48 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
49 void *callback_baton; // The callback baton for the callback function
50 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
51};
52
53static void *
54MonitorChildProcessThreadFunction (void *arg);
55
56lldb::thread_t
57Host::StartMonitoringChildProcess
58(
59 Host::MonitorChildProcessCallback callback,
60 void *callback_baton,
61 lldb::pid_t pid,
62 bool monitor_signals
63)
64{
65 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
66 if (callback)
67 {
68 std::auto_ptr<MonitorInfo> info_ap(new MonitorInfo);
69
70 info_ap->pid = pid;
71 info_ap->callback = callback;
72 info_ap->callback_baton = callback_baton;
73 info_ap->monitor_signals = monitor_signals;
74
75 char thread_name[256];
76 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid);
77 thread = ThreadCreate (thread_name,
78 MonitorChildProcessThreadFunction,
79 info_ap.get(),
80 NULL);
81
Greg Clayton09c81ef2011-02-08 01:34:25 +000082 if (IS_VALID_LLDB_HOST_THREAD(thread))
Greg Clayton8f3b21d2010-09-07 20:11:56 +000083 info_ap.release();
84 }
85 return thread;
86}
87
88//------------------------------------------------------------------
89// Scoped class that will disable thread canceling when it is
90// constructed, and exception safely restore the previous value it
91// when it goes out of scope.
92//------------------------------------------------------------------
93class ScopedPThreadCancelDisabler
94{
95public:
96 ScopedPThreadCancelDisabler()
97 {
98 // Disable the ability for this thread to be cancelled
99 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
100 if (err != 0)
101 m_old_state = -1;
102
103 }
104
105 ~ScopedPThreadCancelDisabler()
106 {
107 // Restore the ability for this thread to be cancelled to what it
108 // previously was.
109 if (m_old_state != -1)
110 ::pthread_setcancelstate (m_old_state, 0);
111 }
112private:
113 int m_old_state; // Save the old cancelability state.
114};
115
116static void *
117MonitorChildProcessThreadFunction (void *arg)
118{
Greg Claytone005f2c2010-11-06 01:53:30 +0000119 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000120 const char *function = __FUNCTION__;
121 if (log)
122 log->Printf ("%s (arg = %p) thread starting...", function, arg);
123
124 MonitorInfo *info = (MonitorInfo *)arg;
125
126 const Host::MonitorChildProcessCallback callback = info->callback;
127 void * const callback_baton = info->callback_baton;
128 const lldb::pid_t pid = info->pid;
129 const bool monitor_signals = info->monitor_signals;
130
131 delete info;
132
133 int status = -1;
134 const int options = 0;
135 struct rusage *rusage = NULL;
136 while (1)
137 {
Caroline Tice926060e2010-10-29 21:48:37 +0000138 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000139 if (log)
140 log->Printf("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p)...", function, pid, options, rusage);
141
142 // Wait for all child processes
143 ::pthread_testcancel ();
144 const lldb::pid_t wait_pid = ::wait4 (pid, &status, options, rusage);
145 ::pthread_testcancel ();
146
147 if (wait_pid == -1)
148 {
149 if (errno == EINTR)
150 continue;
151 else
152 break;
153 }
154 else if (wait_pid == pid)
155 {
156 bool exited = false;
157 int signal = 0;
158 int exit_status = 0;
159 const char *status_cstr = NULL;
160 if (WIFSTOPPED(status))
161 {
162 signal = WSTOPSIG(status);
163 status_cstr = "STOPPED";
164 }
165 else if (WIFEXITED(status))
166 {
167 exit_status = WEXITSTATUS(status);
168 status_cstr = "EXITED";
169 exited = true;
170 }
171 else if (WIFSIGNALED(status))
172 {
173 signal = WTERMSIG(status);
174 status_cstr = "SIGNALED";
175 exited = true;
176 exit_status = -1;
177 }
178 else
179 {
180 status_cstr = "(???)";
181 }
182
183 // Scope for pthread_cancel_disabler
184 {
185 ScopedPThreadCancelDisabler pthread_cancel_disabler;
186
Caroline Tice926060e2010-10-29 21:48:37 +0000187 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000188 if (log)
189 log->Printf ("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p) => pid = %i, status = 0x%8.8x (%s), signal = %i, exit_state = %i",
190 function,
191 wait_pid,
192 options,
193 rusage,
194 pid,
195 status,
196 status_cstr,
197 signal,
198 exit_status);
199
200 if (exited || (signal != 0 && monitor_signals))
201 {
202 bool callback_return = callback (callback_baton, pid, signal, exit_status);
203
204 // If our process exited, then this thread should exit
205 if (exited)
206 break;
207 // If the callback returns true, it means this process should
208 // exit
209 if (callback_return)
210 break;
211 }
212 }
213 }
214 }
215
Caroline Tice926060e2010-10-29 21:48:37 +0000216 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000217 if (log)
218 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
219
220 return NULL;
221}
222
223size_t
224Host::GetPageSize()
225{
226 return ::getpagesize();
227}
228
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000229const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000230Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000231{
Greg Clayton395fc332011-02-15 21:59:32 +0000232 static bool g_supports_32 = false;
233 static bool g_supports_64 = false;
234 static ArchSpec g_host_arch_32;
235 static ArchSpec g_host_arch_64;
236
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000237#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000238
239 // Apple is different in that it can support both 32 and 64 bit executables
240 // in the same operating system running concurrently. Here we detect the
241 // correct host architectures for both 32 and 64 bit including if 64 bit
242 // executables are supported on the system.
243
244 if (g_supports_32 == false && g_supports_64 == false)
245 {
246 // All apple systems support 32 bit execution.
247 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000248 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000249 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000250 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000251 ArchSpec host_arch;
252 // These will tell us about the kernel architecture, which even on a 64
253 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000254 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
255 {
Greg Clayton395fc332011-02-15 21:59:32 +0000256 len = sizeof (cpusubtype);
257 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
258 cpusubtype = CPU_TYPE_ANY;
259
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000260 len = sizeof (is_64_bit_capable);
261 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
262 {
263 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000264 g_supports_64 = true;
265 }
266
267 if (is_64_bit_capable)
268 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000269#if defined (__i386__) || defined (__x86_64__)
270 if (cpusubtype == CPU_SUBTYPE_486)
271 cpusubtype = CPU_SUBTYPE_I386_ALL;
272#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000273 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000274 {
Greg Clayton395fc332011-02-15 21:59:32 +0000275 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000276 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
277 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000278 }
279 else
280 {
281 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000282 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000283 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000284 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000285 }
286 }
Greg Clayton395fc332011-02-15 21:59:32 +0000287 else
288 {
Greg Claytonb3448432011-03-24 21:19:54 +0000289 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000290 g_host_arch_64.Clear();
291 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000292 }
Greg Clayton395fc332011-02-15 21:59:32 +0000293 }
294
295#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000296
Greg Clayton395fc332011-02-15 21:59:32 +0000297 if (g_supports_32 == false && g_supports_64 == false)
298 {
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000299 llvm::Triple triple(llvm::sys::getHostTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000300
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000301 g_host_arch_32.Clear();
302 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000303
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000304 switch (triple.getArch())
305 {
306 default:
307 g_host_arch_32.SetTriple(triple);
308 g_supports_32 = true;
309 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000310
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000311 case llvm::Triple::alpha:
312 case llvm::Triple::x86_64:
313 case llvm::Triple::sparcv9:
314 case llvm::Triple::ppc64:
315 case llvm::Triple::systemz:
316 case llvm::Triple::cellspu:
317 g_host_arch_64.SetTriple(triple);
318 g_supports_64 = true;
319 break;
320 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000321
322 g_supports_32 = g_host_arch_32.IsValid();
323 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000324 }
Greg Clayton395fc332011-02-15 21:59:32 +0000325
326#endif // #else for #if defined (__APPLE__)
327
328 if (arch_kind == eSystemDefaultArchitecture32)
329 return g_host_arch_32;
330 else if (arch_kind == eSystemDefaultArchitecture64)
331 return g_host_arch_64;
332
333 if (g_supports_64)
334 return g_host_arch_64;
335
336 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000337}
338
339const ConstString &
340Host::GetVendorString()
341{
342 static ConstString g_vendor;
343 if (!g_vendor)
344 {
345#if defined (__APPLE__)
346 char ostype[64];
347 size_t len = sizeof(ostype);
348 if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0)
349 g_vendor.SetCString (ostype);
350 else
351 g_vendor.SetCString("apple");
352#elif defined (__linux__)
353 g_vendor.SetCString("gnu");
354#endif
355 }
356 return g_vendor;
357}
358
359const ConstString &
360Host::GetOSString()
361{
362 static ConstString g_os_string;
363 if (!g_os_string)
364 {
365#if defined (__APPLE__)
366 g_os_string.SetCString("darwin");
367#elif defined (__linux__)
368 g_os_string.SetCString("linux");
369#endif
370 }
371 return g_os_string;
372}
373
374const ConstString &
375Host::GetTargetTriple()
376{
377 static ConstString g_host_triple;
378 if (!(g_host_triple))
379 {
380 StreamString triple;
381 triple.Printf("%s-%s-%s",
Greg Clayton940b1032011-02-23 00:35:02 +0000382 GetArchitecture().GetArchitectureName(),
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000383 GetVendorString().AsCString(),
384 GetOSString().AsCString());
385
386 std::transform (triple.GetString().begin(),
387 triple.GetString().end(),
388 triple.GetString().begin(),
389 ::tolower);
390
391 g_host_triple.SetCString(triple.GetString().c_str());
392 }
393 return g_host_triple;
394}
395
396lldb::pid_t
397Host::GetCurrentProcessID()
398{
399 return ::getpid();
400}
401
402lldb::tid_t
403Host::GetCurrentThreadID()
404{
405#if defined (__APPLE__)
406 return ::mach_thread_self();
407#else
408 return lldb::tid_t(pthread_self());
409#endif
410}
411
412const char *
413Host::GetSignalAsCString (int signo)
414{
415 switch (signo)
416 {
417 case SIGHUP: return "SIGHUP"; // 1 hangup
418 case SIGINT: return "SIGINT"; // 2 interrupt
419 case SIGQUIT: return "SIGQUIT"; // 3 quit
420 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
421 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
422 case SIGABRT: return "SIGABRT"; // 6 abort()
423#if defined(_POSIX_C_SOURCE)
424 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
425#else // !_POSIX_C_SOURCE
426 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
427#endif // !_POSIX_C_SOURCE
428 case SIGFPE: return "SIGFPE"; // 8 floating point exception
429 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
430 case SIGBUS: return "SIGBUS"; // 10 bus error
431 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
432 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
433 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
434 case SIGALRM: return "SIGALRM"; // 14 alarm clock
435 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
436 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
437 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
438 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
439 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
440 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
441 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
442 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
443#if !defined(_POSIX_C_SOURCE)
444 case SIGIO: return "SIGIO"; // 23 input/output possible signal
445#endif
446 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
447 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
448 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
449 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
450#if !defined(_POSIX_C_SOURCE)
451 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
452 case SIGINFO: return "SIGINFO"; // 29 information request
453#endif
454 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
455 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
456 default:
457 break;
458 }
459 return NULL;
460}
461
462void
463Host::WillTerminate ()
464{
465}
466
467#if !defined (__APPLE__) // see macosx/Host.mm
468void
469Host::ThreadCreated (const char *thread_name)
470{
471}
Greg Claytonb749a262010-12-03 06:02:24 +0000472
473void
474Host::Backtrace (Stream &strm, uint32_t max_frames)
475{
Greg Clayton52fd9842011-02-02 02:24:04 +0000476 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000477}
478
Greg Clayton638351a2010-12-04 00:10:17 +0000479
480size_t
481Host::GetEnvironment (StringList &env)
482{
Greg Clayton52fd9842011-02-02 02:24:04 +0000483 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000484 return 0;
485}
486
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000487#endif
488
489struct HostThreadCreateInfo
490{
491 std::string thread_name;
492 thread_func_t thread_fptr;
493 thread_arg_t thread_arg;
494
495 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
496 thread_name (name ? name : ""),
497 thread_fptr (fptr),
498 thread_arg (arg)
499 {
500 }
501};
502
503static thread_result_t
504ThreadCreateTrampoline (thread_arg_t arg)
505{
506 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
507 Host::ThreadCreated (info->thread_name.c_str());
508 thread_func_t thread_fptr = info->thread_fptr;
509 thread_arg_t thread_arg = info->thread_arg;
510
Greg Claytone005f2c2010-11-06 01:53:30 +0000511 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000512 if (log)
513 log->Printf("thread created");
514
515 delete info;
516 return thread_fptr (thread_arg);
517}
518
519lldb::thread_t
520Host::ThreadCreate
521(
522 const char *thread_name,
523 thread_func_t thread_fptr,
524 thread_arg_t thread_arg,
525 Error *error
526)
527{
528 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
529
530 // Host::ThreadCreateTrampoline will delete this pointer for us.
531 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
532
533 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
534 if (err == 0)
535 {
536 if (error)
537 error->Clear();
538 return thread;
539 }
540
541 if (error)
542 error->SetError (err, eErrorTypePOSIX);
543
544 return LLDB_INVALID_HOST_THREAD;
545}
546
547bool
548Host::ThreadCancel (lldb::thread_t thread, Error *error)
549{
550 int err = ::pthread_cancel (thread);
551 if (error)
552 error->SetError(err, eErrorTypePOSIX);
553 return err == 0;
554}
555
556bool
557Host::ThreadDetach (lldb::thread_t thread, Error *error)
558{
559 int err = ::pthread_detach (thread);
560 if (error)
561 error->SetError(err, eErrorTypePOSIX);
562 return err == 0;
563}
564
565bool
566Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
567{
568 int err = ::pthread_join (thread, thread_result_ptr);
569 if (error)
570 error->SetError(err, eErrorTypePOSIX);
571 return err == 0;
572}
573
574//------------------------------------------------------------------
575// Control access to a static file thread name map using a single
576// static function to avoid a static constructor.
577//------------------------------------------------------------------
578static const char *
579ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
580{
581 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
582
583 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
584 Mutex::Locker locker(&g_mutex);
585
586 typedef std::map<uint64_t, std::string> thread_name_map;
587 // rdar://problem/8153284
588 // Fixed a crasher where during shutdown, loggings attempted to access the
589 // thread name but the static map instance had already been destructed.
590 // Another approach is to introduce a static guard object which monitors its
591 // own destruction and raises a flag, but this incurs more overhead.
592 static thread_name_map *g_thread_names_ptr = new thread_name_map();
593 thread_name_map &g_thread_names = *g_thread_names_ptr;
594
595 if (get)
596 {
597 // See if the thread name exists in our thread name pool
598 thread_name_map::iterator pos = g_thread_names.find(pid_tid);
599 if (pos != g_thread_names.end())
600 return pos->second.c_str();
601 }
602 else
603 {
604 // Set the thread name
605 g_thread_names[pid_tid] = name;
606 }
607 return NULL;
608}
609
610const char *
611Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
612{
613 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
614 if (name == NULL)
615 {
616#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
617 // We currently can only get the name of a thread in the current process.
618 if (pid == Host::GetCurrentProcessID())
619 {
620 char pthread_name[1024];
621 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
622 {
623 if (pthread_name[0])
624 {
625 // Set the thread in our string pool
626 ThreadNameAccessor (false, pid, tid, pthread_name);
627 // Get our copy of the thread name string
628 name = ThreadNameAccessor (true, pid, tid, NULL);
629 }
630 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000631
632 if (name == NULL)
633 {
634 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
635 if (current_queue != NULL)
636 name = dispatch_queue_get_label (current_queue);
637 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000638 }
639#endif
640 }
641 return name;
642}
643
644void
645Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
646{
647 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
648 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
649 if (pid == LLDB_INVALID_PROCESS_ID)
650 pid = curr_pid;
651
652 if (tid == LLDB_INVALID_THREAD_ID)
653 tid = curr_tid;
654
655#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
656 // Set the pthread name if possible
657 if (pid == curr_pid && tid == curr_tid)
658 {
659 ::pthread_setname_np (name);
660 }
661#endif
662 ThreadNameAccessor (false, pid, tid, name);
663}
664
665FileSpec
666Host::GetProgramFileSpec ()
667{
668 static FileSpec g_program_filespec;
669 if (!g_program_filespec)
670 {
671#if defined (__APPLE__)
672 char program_fullpath[PATH_MAX];
673 // If DST is NULL, then return the number of bytes needed.
674 uint32_t len = sizeof(program_fullpath);
675 int err = _NSGetExecutablePath (program_fullpath, &len);
676 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000677 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000678 else if (err == -1)
679 {
680 char *large_program_fullpath = (char *)::malloc (len + 1);
681
682 err = _NSGetExecutablePath (large_program_fullpath, &len);
683 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000684 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000685
686 ::free (large_program_fullpath);
687 }
688#elif defined (__linux__)
689 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000690 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
691 if (len > 0) {
692 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000693 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000694 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000695#elif defined (__FreeBSD__)
696 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
697 size_t exe_path_size;
698 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
699 {
Greg Clayton366795e2011-01-13 01:27:55 +0000700 char *exe_path = new char[exe_path_size];
701 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
702 g_program_filespec.SetFile(exe_path, false);
703 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000704 }
705#endif
706 }
707 return g_program_filespec;
708}
709
710FileSpec
711Host::GetModuleFileSpecForHostAddress (const void *host_addr)
712{
713 FileSpec module_filespec;
714 Dl_info info;
715 if (::dladdr (host_addr, &info))
716 {
717 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000718 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000719 }
720 return module_filespec;
721}
722
723#if !defined (__APPLE__) // see Host.mm
724bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000725Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000726{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000727 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000728}
729#endif
730
Greg Clayton14ef59f2011-02-08 00:35:34 +0000731// Opaque info that tracks a dynamic library that was loaded
732struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000733{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000734 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
735 file_spec (fs),
736 open_options (o),
737 handle (h)
738 {
739 }
740
741 const FileSpec file_spec;
742 uint32_t open_options;
743 void * handle;
744};
745
746void *
747Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
748{
Greg Clayton52fd9842011-02-02 02:24:04 +0000749 char path[PATH_MAX];
750 if (file_spec.GetPath(path, sizeof(path)))
751 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000752 int mode = 0;
753
754 if (options & eDynamicLibraryOpenOptionLazy)
755 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000756 else
757 mode |= RTLD_NOW;
758
Greg Clayton14ef59f2011-02-08 00:35:34 +0000759
760 if (options & eDynamicLibraryOpenOptionLocal)
761 mode |= RTLD_LOCAL;
762 else
763 mode |= RTLD_GLOBAL;
764
765#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
766 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
767 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000768#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000769
770 void * opaque = ::dlopen (path, mode);
771
772 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000773 {
774 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000775 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000776 }
777 else
778 {
779 error.SetErrorString(::dlerror());
780 }
781 }
782 else
783 {
784 error.SetErrorString("failed to extract path");
785 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000786 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000787}
788
789Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000790Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000791{
792 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000793 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000794 {
795 error.SetErrorString ("invalid dynamic library handle");
796 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000797 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000798 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000799 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
800 if (::dlclose (dylib_info->handle) != 0)
801 {
802 error.SetErrorString(::dlerror());
803 }
804
805 dylib_info->open_options = 0;
806 dylib_info->handle = 0;
807 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000808 }
809 return error;
810}
811
812void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000813Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000814{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000815 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000816 {
817 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000818 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000819 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000820 {
821 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
822
823 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
824 if (symbol_addr)
825 {
826#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
827 // This host doesn't support limiting searches to this shared library
828 // so we need to verify that the match came from this shared library
829 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000830 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000831 {
832 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
833 if (match_dylib_spec != dylib_info->file_spec)
834 {
835 char dylib_path[PATH_MAX];
836 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
837 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
838 else
839 error.SetErrorString ("symbol not found");
840 return NULL;
841 }
842 }
843#endif
844 error.Clear();
845 return symbol_addr;
846 }
847 else
848 {
849 error.SetErrorString(::dlerror());
850 }
851 }
852 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000853}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000854
855bool
856Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
857{
Greg Clayton5d187e52011-01-08 20:28:42 +0000858 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000859 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
860 // on linux this is assumed to be the "lldb" main executable. If LLDB on
861 // linux is actually in a shared library (lldb.so??) then this function will
862 // need to be modified to "do the right thing".
863
864 switch (path_type)
865 {
866 case ePathTypeLLDBShlibDir:
867 {
868 static ConstString g_lldb_so_dir;
869 if (!g_lldb_so_dir)
870 {
871 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
872 g_lldb_so_dir = lldb_file_spec.GetDirectory();
873 }
874 file_spec.GetDirectory() = g_lldb_so_dir;
875 return file_spec.GetDirectory();
876 }
877 break;
878
879 case ePathTypeSupportExecutableDir:
880 {
881 static ConstString g_lldb_support_exe_dir;
882 if (!g_lldb_support_exe_dir)
883 {
884 FileSpec lldb_file_spec;
885 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
886 {
887 char raw_path[PATH_MAX];
888 char resolved_path[PATH_MAX];
889 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
890
891#if defined (__APPLE__)
892 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
893 if (framework_pos)
894 {
895 framework_pos += strlen("LLDB.framework");
896 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
897 }
898#endif
899 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
900 g_lldb_support_exe_dir.SetCString(resolved_path);
901 }
902 }
903 file_spec.GetDirectory() = g_lldb_support_exe_dir;
904 return file_spec.GetDirectory();
905 }
906 break;
907
908 case ePathTypeHeaderDir:
909 {
910 static ConstString g_lldb_headers_dir;
911 if (!g_lldb_headers_dir)
912 {
913#if defined (__APPLE__)
914 FileSpec lldb_file_spec;
915 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
916 {
917 char raw_path[PATH_MAX];
918 char resolved_path[PATH_MAX];
919 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
920
921 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
922 if (framework_pos)
923 {
924 framework_pos += strlen("LLDB.framework");
925 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
926 }
927 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
928 g_lldb_headers_dir.SetCString(resolved_path);
929 }
930#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000931 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000932 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
933#endif
934 }
935 file_spec.GetDirectory() = g_lldb_headers_dir;
936 return file_spec.GetDirectory();
937 }
938 break;
939
940 case ePathTypePythonDir:
941 {
Greg Clayton52fd9842011-02-02 02:24:04 +0000942 // TODO: Anyone know how we can determine this for linux? Other systems?
Greg Clayton24b48ff2010-10-17 22:03:32 +0000943 // For linux we are currently assuming the location of the lldb
944 // binary that contains this function is the directory that will
945 // contain lldb.so, lldb.py and embedded_interpreter.py...
946
947 static ConstString g_lldb_python_dir;
948 if (!g_lldb_python_dir)
949 {
950 FileSpec lldb_file_spec;
951 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
952 {
953 char raw_path[PATH_MAX];
954 char resolved_path[PATH_MAX];
955 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
956
957#if defined (__APPLE__)
958 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
959 if (framework_pos)
960 {
961 framework_pos += strlen("LLDB.framework");
962 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
963 }
964#endif
965 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
966 g_lldb_python_dir.SetCString(resolved_path);
967 }
968 }
969 file_spec.GetDirectory() = g_lldb_python_dir;
970 return file_spec.GetDirectory();
971 }
972 break;
973
Greg Clayton52fd9842011-02-02 02:24:04 +0000974 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
975 {
976#if defined (__APPLE__)
977 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +0000978 static bool g_lldb_system_plugin_dir_located = false;
979 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +0000980 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000981 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +0000982 FileSpec lldb_file_spec;
983 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
984 {
985 char raw_path[PATH_MAX];
986 char resolved_path[PATH_MAX];
987 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
988
989 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
990 if (framework_pos)
991 {
992 framework_pos += strlen("LLDB.framework");
993 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +0000994 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
995 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +0000996 }
Greg Clayton58e26e02011-03-24 04:28:38 +0000997 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +0000998 }
999 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001000
1001 if (g_lldb_system_plugin_dir)
1002 {
1003 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1004 return true;
1005 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001006#endif
1007 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1008 return false;
1009 }
1010 break;
1011
1012 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1013 {
1014#if defined (__APPLE__)
1015 static ConstString g_lldb_user_plugin_dir;
1016 if (!g_lldb_user_plugin_dir)
1017 {
1018 char user_plugin_path[PATH_MAX];
1019 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1020 user_plugin_path,
1021 sizeof(user_plugin_path)))
1022 {
1023 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1024 }
1025 }
1026 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1027 return file_spec.GetDirectory();
1028#endif
1029 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1030 return false;
1031 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001032 default:
1033 assert (!"Unhandled PathType");
1034 break;
1035 }
1036
1037 return false;
1038}
1039
Greg Clayton58e26e02011-03-24 04:28:38 +00001040
1041bool
1042Host::GetHostname (std::string &s)
1043{
1044 char hostname[PATH_MAX];
1045 hostname[sizeof(hostname) - 1] = '\0';
1046 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1047 {
1048 struct hostent* h = ::gethostbyname (hostname);
1049 if (h)
1050 s.assign (h->h_name);
1051 else
1052 s.assign (hostname);
1053 return true;
1054 }
1055 return false;
1056}
1057
Greg Claytona733c042011-03-21 21:25:07 +00001058#if !defined (__APPLE__) // see macosx/Host.mm
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001059
Greg Clayton58e26e02011-03-24 04:28:38 +00001060bool
1061Host::GetOSBuildString (std::string &s)
1062{
1063 s.clear();
1064 return false;
1065}
1066
1067bool
1068Host::GetOSKernelDescription (std::string &s)
1069{
1070 s.clear();
1071 return false;
1072}
1073
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001074uint32_t
1075Host::FindProcessesByName (const char *name, NameMatchType name_match_type, ProcessInfoList &process_infos)
1076{
1077 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001078 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001079}
1080
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001081bool
1082Host::GetProcessInfo (lldb::pid_t pid, ProcessInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001083{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001084 process_info.Clear();
1085 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001086}
1087
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001088bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001089Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001090{
1091 return false;
1092}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001093
Greg Claytone98ac252010-11-10 04:57:04 +00001094void
1095Host::SetCrashDescriptionWithFormat (const char *format, ...)
1096{
1097}
1098
1099void
1100Host::SetCrashDescription (const char *description)
1101{
1102}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001103
1104lldb::pid_t
1105LaunchApplication (const FileSpec &app_file_spec)
1106{
1107 return LLDB_INVALID_PROCESS_ID;
1108}
1109
1110lldb::pid_t
1111Host::LaunchInNewTerminal
1112(
Greg Claytonb73620c2010-12-18 01:54:34 +00001113 const char *tty_name,
Greg Clayton24b48ff2010-10-17 22:03:32 +00001114 const char **argv,
1115 const char **envp,
Greg Claytonde915be2011-01-23 05:56:20 +00001116 const char *working_dir,
Greg Clayton24b48ff2010-10-17 22:03:32 +00001117 const ArchSpec *arch_spec,
1118 bool stop_at_entry,
1119 bool disable_aslr
1120)
1121{
1122 return LLDB_INVALID_PROCESS_ID;
1123}
1124
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001125#endif