blob: 3290f225a192fae92ecd047e816e2b89fe66039b [file] [log] [blame]
Eli Friedman7c7c19d2010-07-02 19:28:44 +00001//===-- Host.mm -------------------------------------------------*- 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 <dlfcn.h>
11#include <libgen.h>
12#include <signal.h>
13#include <stddef.h>
14#include <sys/sysctl.h>
15#include <unistd.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18
19#include <map>
20#include <string>
21
22#include "lldb/Host/Host.h"
23#include "lldb/Core/ArchSpec.h"
24#include "lldb/Core/ConstString.h"
25#include "lldb/Core/Error.h"
26#include "lldb/Core/FileSpec.h"
27#include "lldb/Core/Log.h"
28#include "lldb/Core/StreamString.h"
29#include "lldb/Host/Mutex.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/TargetList.h"
33#include "lldb/lldb-private-log.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38//------------------------------------------------------------------
39// Return the size in bytes of a page on the host system
40//------------------------------------------------------------------
41size_t
42Host::GetPageSize()
43{
44 return ::getpagesize();
45}
46
47
48//------------------------------------------------------------------
49// Returns true if the host system is Big Endian.
50//------------------------------------------------------------------
51ByteOrder
52Host::GetByteOrder()
53{
54 union EndianTest
55 {
56 uint32_t num;
57 uint8_t bytes[sizeof(uint32_t)];
58 } endian = { (uint16_t)0x11223344 };
59 switch (endian.bytes[0])
60 {
61 case 0x11: return eByteOrderLittle;
62 case 0x44: return eByteOrderBig;
63 case 0x33: return eByteOrderPDP;
64 }
65 return eByteOrderInvalid;
66}
67
68lldb::pid_t
69Host::GetCurrentProcessID()
70{
71 return ::getpid();
72}
73
74lldb::pid_t
75Host::GetCurrentThreadID()
76{
77 return pthread_self();
78}
79
80
81const ArchSpec &
82Host::GetArchitecture ()
83{
84 static ArchSpec g_host_arch;
85#if 0
86 if (!g_host_arch.IsValid())
87 {
88 uint32_t cputype, cpusubtype;
89 uint32_t is_64_bit_capable;
90 size_t len = sizeof(cputype);
91 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
92 {
93 len = sizeof(cpusubtype);
94 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) == 0)
95 g_host_arch.SetArch(cputype, cpusubtype);
96
97 len = sizeof (is_64_bit_capable);
98 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
99 {
100 if (is_64_bit_capable)
101 {
102 if (cputype == CPU_TYPE_I386 && cpusubtype == CPU_SUBTYPE_486)
103 cpusubtype = CPU_SUBTYPE_I386_ALL;
104
105 cputype |= CPU_ARCH_ABI64;
106 }
107 }
108 }
109 }
110#else
111 g_host_arch.SetArch(7u, 144u);
112#endif
113 return g_host_arch;
114}
115
116const ConstString &
117Host::GetVendorString()
118{
119 static ConstString g_vendor;
120 if (!g_vendor)
121 {
122#if 0
123 char ostype[64];
124 size_t len = sizeof(ostype);
125 if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0)
126 g_vendor.SetCString (ostype);
127#else
128 g_vendor.SetCString("gnu");
129#endif
130 }
131 return g_vendor;
132}
133
134const ConstString &
135Host::GetOSString()
136{
137 static ConstString g_os_string("linux");
138 return g_os_string;
139}
140
141const ConstString &
142Host::GetTargetTriple()
143{
144 static ConstString g_host_triple;
145 if (!(g_host_triple))
146 {
147 StreamString triple;
148 triple.Printf("%s-%s-%s",
149 GetArchitecture ().AsCString(),
150 GetVendorString().AsCString("apple"),
151 GetOSString().AsCString("darwin"));
152
153 std::transform (triple.GetString().begin(),
154 triple.GetString().end(),
155 triple.GetString().begin(),
156 ::tolower);
157
158 g_host_triple.SetCString(triple.GetString().c_str());
159 }
160 return g_host_triple;
161}
162
163static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
164static pthread_key_t g_thread_create_key = 0;
165
166static void
167InitThreadCreated()
168{
169 ::pthread_key_create (&g_thread_create_key, 0);
170}
171
172struct HostThreadCreateInfo
173{
174 std::string thread_name;
175 thread_func_t thread_fptr;
176 thread_arg_t thread_arg;
177
178 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
179 thread_name (name ? name : ""),
180 thread_fptr (fptr),
181 thread_arg (arg)
182 {
183 }
184};
185
186static thread_result_t
187ThreadCreateTrampoline (thread_arg_t arg)
188{
189 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
190 Host::ThreadCreated (info->thread_name.c_str());
191 thread_func_t thread_fptr = info->thread_fptr;
192 thread_arg_t thread_arg = info->thread_arg;
193
194 Log * log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
195 if (log)
196 log->Printf("thread created");
197
198 delete info;
199 return thread_fptr (thread_arg);
200}
201
202lldb::thread_t
203Host::ThreadCreate
204(
205 const char *thread_name,
206 thread_func_t thread_fptr,
207 thread_arg_t thread_arg,
208 Error *error
209)
210{
211 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
212
213 // Host::ThreadCreateTrampoline will delete this pointer for us.
214 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
215
216 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
217 if (err == 0)
218 {
219 if (error)
220 error->Clear();
221 return thread;
222 }
223
224 if (error)
225 error->SetError (err, eErrorTypePOSIX);
226
227 return LLDB_INVALID_HOST_THREAD;
228}
229
230bool
231Host::ThreadCancel (lldb::thread_t thread, Error *error)
232{
233
234 int err = ::pthread_cancel (thread);
235 if (error)
236 error->SetError(err, eErrorTypePOSIX);
237 return err == 0;
238}
239
240bool
241Host::ThreadDetach (lldb::thread_t thread, Error *error)
242{
243 int err = ::pthread_detach (thread);
244 if (error)
245 error->SetError(err, eErrorTypePOSIX);
246 return err == 0;
247}
248
249bool
250Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
251{
252 int err = ::pthread_join (thread, thread_result_ptr);
253 if (error)
254 error->SetError(err, eErrorTypePOSIX);
255 return err == 0;
256}
257
258void
259Host::ThreadCreated (const char *thread_name)
260{
261 ::pthread_once (&g_thread_create_once, InitThreadCreated);
262 if (g_thread_create_key)
263 {
264 //::pthread_setspecific (g_thread_create_key, new MacOSXDarwinThread(thread_name));
265 }
266}
267
268//------------------------------------------------------------------
269// Control access to a static file thread name map using a single
270// static function to avoid a static constructor.
271//------------------------------------------------------------------
272static const char *
273ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
274{
275
276 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
277
278 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
279 Mutex::Locker locker(&g_mutex);
280
281 typedef std::map<uint64_t, std::string> thread_name_map;
282 static thread_name_map g_thread_names;
283
284 if (get)
285 {
286 // See if the thread name exists in our thread name pool
287 thread_name_map::iterator pos = g_thread_names.find(pid_tid);
288 if (pos != g_thread_names.end())
289 return pos->second.c_str();
290 }
291 else
292 {
293 // Set the thread name
294 g_thread_names[pid_tid] = name;
295 }
296 return NULL;
297}
298
299
300
301const char *
302Host::GetSignalAsCString (int signo)
303{
304 switch (signo)
305 {
306 case SIGHUP: return "SIGHUP"; // 1 hangup
307 case SIGINT: return "SIGINT"; // 2 interrupt
308 case SIGQUIT: return "SIGQUIT"; // 3 quit
309 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
310 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
311 case SIGABRT: return "SIGABRT"; // 6 abort()
312#if defined(_POSIX_C_SOURCE)
313 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
314#else // !_POSIX_C_SOURCE
315 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
316#endif // !_POSIX_C_SOURCE
317 case SIGFPE: return "SIGFPE"; // 8 floating point exception
318 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
319 case SIGBUS: return "SIGBUS"; // 10 bus error
320 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
321 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
322 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
323 case SIGALRM: return "SIGALRM"; // 14 alarm clock
324 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
325 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
326 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
327 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
328 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
329 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
330 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
331 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
332#if !defined(_POSIX_C_SOURCE)
333 case SIGIO: return "SIGIO"; // 23 input/output possible signal
334#endif
335 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
336 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
337 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
338 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
339#if !defined(_POSIX_C_SOURCE)
340 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
341 case SIGINFO: return "SIGINFO"; // 29 information request
342#endif
343 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
344 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
345 default:
346 break;
347 }
348 return NULL;
349}
350
351const char *
352Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
353{
354 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
355 if (name == NULL)
356 {
357 // We currently can only get the name of a thread in the current process.
358#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
359 if (pid == Host::GetCurrentProcessID())
360 {
361 char pthread_name[1024];
362 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
363 {
364 if (pthread_name[0])
365 {
366 // Set the thread in our string pool
367 ThreadNameAccessor (false, pid, tid, pthread_name);
368 // Get our copy of the thread name string
369 name = ThreadNameAccessor (true, pid, tid, NULL);
370 }
371 }
372 }
373#endif
374 }
375 return name;
376}
377
378void
379Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
380{
381 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
382 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
383 if (pid == LLDB_INVALID_PROCESS_ID)
384 pid = curr_pid;
385
386 if (tid == LLDB_INVALID_THREAD_ID)
387 tid = curr_tid;
388
389#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
390 // Set the pthread name if possible
391 if (pid == curr_pid && tid == curr_tid)
392 {
393 ::pthread_setname_np (name) == 0;
394 }
395#endif
396 ThreadNameAccessor (false, pid, tid, name);
397}
398
399FileSpec
400Host::GetProgramFileSpec ()
401{
402 static FileSpec g_program_filepsec;
403 if (!g_program_filepsec)
404 {
Eli Friedman5bc37a42010-07-02 19:38:10 +0000405 char exe_path[PATH_MAX];
406 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
407 if (len >= 0)
408 g_program_filepsec = FileSpec(exe_path);
Eli Friedman7c7c19d2010-07-02 19:28:44 +0000409 }
410 return g_program_filepsec;
411}
412
413
414FileSpec
415Host::GetModuleFileSpecForHostAddress (const void *host_addr)
416{
417 FileSpec module_filespec;
418 Dl_info info;
419 if (::dladdr (host_addr, &info))
420 {
421 if (info.dli_fname)
422 module_filespec.SetFile(info.dli_fname);
423 }
424 return module_filespec;
425}
426
427
428bool
429Host::ResolveExecutableInBundle (FileSpec *file)
430{
431#if 0
432 if (file->GetFileType () == FileSpec::eFileTypeDirectory)
433 {
434 char path[PATH_MAX];
435 if (file->GetPath(path, sizeof(path)))
436 {
437 CFCBundle bundle (path);
438 CFCReleaser<CFURLRef> url(bundle.CopyExecutableURL ());
439 if (url.get())
440 {
441 if (::CFURLGetFileSystemRepresentation (url.get(), YES, (UInt8*)path, sizeof(path)))
442 {
443 file->SetFile(path);
444 return true;
445 }
446 }
447 }
448 }
449#endif
450 return false;
451}
452
453struct MonitorInfo
454{
455 int handle;
456 pthread_t thread;
457 Host::MonitorChildProcessCallback callback;
458 void *callback_baton;
459 bool monitor_signals;
460};
461
462typedef std::multimap<lldb::pid_t, MonitorInfo> MonitorInfoMap;
463static pthread_mutex_t g_monitor_map_mutex = PTHREAD_MUTEX_INITIALIZER;
464typedef lldb::SharedPtr<MonitorInfoMap>::Type MonitorInfoMapSP;
465
466static MonitorInfoMapSP&
467GetMonitorMap (bool can_create)
468{
469 static MonitorInfoMapSP g_monitor_map_sp;
470 if (can_create && g_monitor_map_sp.get() == NULL)
471 {
472 g_monitor_map_sp.reset (new MonitorInfoMap);
473 }
474 return g_monitor_map_sp;
475}
476
477static Predicate<bool>&
478GetChildProcessPredicate ()
479{
480 static Predicate<bool> g_has_child_processes;
481 return g_has_child_processes;
482}
483
484static void *
485MonitorChildProcessThreadFunction (void *arg);
486
487static pthread_t g_monitor_thread;
488
489uint32_t
490Host::StartMonitoringChildProcess
491(
492 MonitorChildProcessCallback callback,
493 void *callback_baton,
494 lldb::pid_t pid,
495 bool monitor_signals
496)
497{
498 static uint32_t g_handle = 0;
499 if (callback)
500 {
501 Mutex::Locker locker(&g_monitor_map_mutex);
502 if (!g_monitor_thread)
503 {
504 ::pid_t wait_pid = -1;
505 g_monitor_thread = ThreadCreate ("<lldb.host.wait4>",
506 MonitorChildProcessThreadFunction,
507 &wait_pid,
508 NULL);
509 if (g_monitor_thread)
510 {
511 //Host::ThreadDetach (g_monitor_thread, NULL);
512 }
513 }
514
515 if (g_monitor_thread)
516 {
517 MonitorInfo info = { ++g_handle, 0, callback, callback_baton, monitor_signals };
518 MonitorInfoMapSP monitor_map_sp (GetMonitorMap (true));
519 if (monitor_map_sp)
520 {
521 monitor_map_sp->insert(std::make_pair(pid, info));
522 GetChildProcessPredicate ().SetValue (true, eBroadcastOnChange);
523 return info.handle;
524 }
525 }
526 }
527 return 0;
528}
529
530bool
531Host::StopMonitoringChildProcess (uint32_t handle)
532{
533 Mutex::Locker locker(&g_monitor_map_mutex);
534 MonitorInfoMapSP monitor_map_sp (GetMonitorMap (false));
535 if (monitor_map_sp)
536 {
537 MonitorInfoMap::iterator pos, end = monitor_map_sp->end();
538 for (pos = monitor_map_sp->end(); pos != end; ++pos)
539 {
540 if (pos->second.handle == handle)
541 {
542 monitor_map_sp->erase(pos);
543 return true;
544 }
545 }
546 }
547 return false;
548}
549
550
551//------------------------------------------------------------------
552// Scoped class that will disable thread canceling when it is
553// constructed, and exception safely restore the previous value it
554// when it goes out of scope.
555//------------------------------------------------------------------
556class ScopedPThreadCancelDisabler
557{
558public:
559
560 ScopedPThreadCancelDisabler()
561 {
562 // Disable the ability for this thread to be cancelled
563 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
564 if (err != 0)
565 m_old_state = -1;
566
567 }
568
569 ~ScopedPThreadCancelDisabler()
570 {
571 // Restore the ability for this thread to be cancelled to what it
572 // previously was.
573 if (m_old_state != -1)
574 ::pthread_setcancelstate (m_old_state, 0);
575 }
576private:
577 int m_old_state; // Save the old cancelability state.
578};
579
580
581
582static void *
583MonitorChildProcessThreadFunction (void *arg)
584{
585 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
586 const char *function = __FUNCTION__;
587 if (log)
588 log->Printf ("%s (arg = %p) thread starting...", function, arg);
589
590 const ::pid_t wait_pid = -1;//*((pid_t*)arg);
591 int status = -1;
592 const int options = 0;
593 struct rusage *rusage = NULL;
594 while (1)
595 {
596 if (log)
597 log->Printf("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p)...", function, wait_pid, options, rusage);
598
599 // Wait for all child processes
600 ::pthread_testcancel ();
601 lldb::pid_t pid = ::wait4 (wait_pid, &status, options, rusage);
602 ::pthread_testcancel ();
603
604 if (pid < 0)
605 {
606 // No child processes to watch wait for the mutex to be cleared
607
608 // Scope for "locker"
609 {
610 ScopedPThreadCancelDisabler pthread_cancel_disabler;
611
612 // First clear out all monitor entries since we have no processes
613 // to watch.
614 Mutex::Locker locker(&g_monitor_map_mutex);
615 // Since we don't have any child processes, we can safely clear
616 // anyone with a valid pid.
617 MonitorInfoMapSP monitor_map_sp(GetMonitorMap (false));
618 if (monitor_map_sp)
619 {
620 MonitorInfoMap::iterator pos = monitor_map_sp->begin();
621 while (pos != monitor_map_sp->end())
622 {
623 // pid value of 0 and -1 are special (see man page on wait4...)
624 if (pos->first > 0)
625 {
626 MonitorInfoMap::iterator next_pos = pos; ++next_pos;
627 monitor_map_sp->erase (pos, next_pos);
628 pos = next_pos;
629 }
630 else
631 ++pos;
632 }
633 }
634 }
635
636 if (log)
637 log->Printf("%s no child processes, wait for some...", function);
638 GetChildProcessPredicate ().SetValue (false, eBroadcastNever);
639 ::pthread_testcancel();
640 GetChildProcessPredicate ().WaitForValueEqualTo (true);
641 if (log)
642 log->Printf("%s resuming monitoring of child processes.", function);
643
644 }
645 else
646 {
647 ScopedPThreadCancelDisabler pthread_cancel_disabler;
648 bool exited = false;
649 int signal = 0;
650 int exit_status = 0;
651 const char *status_cstr = NULL;
652 if (WIFSTOPPED(status))
653 {
654 signal = WSTOPSIG(status);
655 status_cstr = "STOPPED";
656 }
657 else if (WIFEXITED(status))
658 {
659 exit_status = WEXITSTATUS(status);
660 status_cstr = "EXITED";
661 exited = true;
662 }
663 else if (WIFSIGNALED(status))
664 {
665 signal = WTERMSIG(status);
666 status_cstr = "SIGNALED";
667 exited = true;
668 exit_status = -1;
669 }
670 else
671 {
672 status_cstr = "(???"")";
673 }
674
675 if (log)
676 log->Printf ("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p) => pid = %i, status = 0x%8.8x (%s), signal = %i, exit_state = %i",
677 function,
678 wait_pid,
679 options,
680 rusage,
681 pid,
682 status,
683 status_cstr,
684 signal,
685 exit_status);
686
687 // Scope for mutex locker
688 {
689 // Notify anyone listening to this process
690 Mutex::Locker locker(&g_monitor_map_mutex);
691 MonitorInfoMapSP monitor_map_sp(GetMonitorMap (false));
692 if (monitor_map_sp)
693 {
694 std::pair<MonitorInfoMap::iterator, MonitorInfoMap::iterator> range;
695 range = monitor_map_sp->equal_range(pid);
696 MonitorInfoMap::iterator pos;
697 for (pos = range.first; pos != range.second; ++pos)
698 {
699 if (exited || (signal != 0 && pos->second.monitor_signals))
700 {
701 bool callback_return = pos->second.callback (pos->second.callback_baton, pid, signal, exit_status);
702
703 if (exited || callback_return)
704 {
705 // Make this entry as needing to be removed by
706 // setting its handle to zero
707 pos->second.handle = 0;
708 }
709 }
710 }
711
712 // Remove any entries that requested to be removed or any
713 // entries for child processes that did exit. We know this
714 // because we changed the handles to an invalid value.
715 pos = monitor_map_sp->begin();
716 while (pos != monitor_map_sp->end())
717 {
718 if (pos->second.handle == 0)
719 {
720 MonitorInfoMap::iterator next_pos = pos; ++next_pos;
721 monitor_map_sp->erase (pos, next_pos);
722 pos = next_pos;
723 }
724 else
725 ++pos;
726 }
727 }
728 }
729 }
730 }
731
732 if (log)
733 log->Printf ("ProcessMacOSX::%s (arg = %p) thread exiting...", __FUNCTION__, arg);
734
735 g_monitor_thread = NULL;
736 return NULL;
737}
738
739void
740Host::WillTerminate ()
741{
742 if (g_monitor_thread != NULL)
743 {
744 ThreadCancel (g_monitor_thread, NULL);
Eli Friedman7c7c19d2010-07-02 19:28:44 +0000745 ThreadJoin(g_monitor_thread, NULL, NULL);
746 g_monitor_thread = NULL;
747 }
748}
749