blob: 51702d0e2511d4eca577c8d055ce70c88e301df8 [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"
14#include "lldb/Core/FileSpec.h"
15#include "lldb/Core/Log.h"
16#include "lldb/Core/StreamString.h"
17#include "lldb/Host/Mutex.h"
18
19#include <dlfcn.h>
20#include <errno.h>
21#include <sys/sysctl.h>
22#include <sys/wait.h>
23
24#if defined (__APPLE__)
25#include <libproc.h>
26#include <mach-o/dyld.h>
27#endif
28
29using namespace lldb;
30using namespace lldb_private;
31
32struct MonitorInfo
33{
34 lldb::pid_t pid; // The process ID to monitor
35 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
36 void *callback_baton; // The callback baton for the callback function
37 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
38};
39
40static void *
41MonitorChildProcessThreadFunction (void *arg);
42
43lldb::thread_t
44Host::StartMonitoringChildProcess
45(
46 Host::MonitorChildProcessCallback callback,
47 void *callback_baton,
48 lldb::pid_t pid,
49 bool monitor_signals
50)
51{
52 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
53 if (callback)
54 {
55 std::auto_ptr<MonitorInfo> info_ap(new MonitorInfo);
56
57 info_ap->pid = pid;
58 info_ap->callback = callback;
59 info_ap->callback_baton = callback_baton;
60 info_ap->monitor_signals = monitor_signals;
61
62 char thread_name[256];
63 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid);
64 thread = ThreadCreate (thread_name,
65 MonitorChildProcessThreadFunction,
66 info_ap.get(),
67 NULL);
68
69 if (thread != LLDB_INVALID_HOST_THREAD)
70 info_ap.release();
71 }
72 return thread;
73}
74
75//------------------------------------------------------------------
76// Scoped class that will disable thread canceling when it is
77// constructed, and exception safely restore the previous value it
78// when it goes out of scope.
79//------------------------------------------------------------------
80class ScopedPThreadCancelDisabler
81{
82public:
83 ScopedPThreadCancelDisabler()
84 {
85 // Disable the ability for this thread to be cancelled
86 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
87 if (err != 0)
88 m_old_state = -1;
89
90 }
91
92 ~ScopedPThreadCancelDisabler()
93 {
94 // Restore the ability for this thread to be cancelled to what it
95 // previously was.
96 if (m_old_state != -1)
97 ::pthread_setcancelstate (m_old_state, 0);
98 }
99private:
100 int m_old_state; // Save the old cancelability state.
101};
102
103static void *
104MonitorChildProcessThreadFunction (void *arg)
105{
106 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
107 const char *function = __FUNCTION__;
108 if (log)
109 log->Printf ("%s (arg = %p) thread starting...", function, arg);
110
111 MonitorInfo *info = (MonitorInfo *)arg;
112
113 const Host::MonitorChildProcessCallback callback = info->callback;
114 void * const callback_baton = info->callback_baton;
115 const lldb::pid_t pid = info->pid;
116 const bool monitor_signals = info->monitor_signals;
117
118 delete info;
119
120 int status = -1;
121 const int options = 0;
122 struct rusage *rusage = NULL;
123 while (1)
124 {
Caroline Tice926060e2010-10-29 21:48:37 +0000125 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000126 if (log)
127 log->Printf("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p)...", function, pid, options, rusage);
128
129 // Wait for all child processes
130 ::pthread_testcancel ();
131 const lldb::pid_t wait_pid = ::wait4 (pid, &status, options, rusage);
132 ::pthread_testcancel ();
133
134 if (wait_pid == -1)
135 {
136 if (errno == EINTR)
137 continue;
138 else
139 break;
140 }
141 else if (wait_pid == pid)
142 {
143 bool exited = false;
144 int signal = 0;
145 int exit_status = 0;
146 const char *status_cstr = NULL;
147 if (WIFSTOPPED(status))
148 {
149 signal = WSTOPSIG(status);
150 status_cstr = "STOPPED";
151 }
152 else if (WIFEXITED(status))
153 {
154 exit_status = WEXITSTATUS(status);
155 status_cstr = "EXITED";
156 exited = true;
157 }
158 else if (WIFSIGNALED(status))
159 {
160 signal = WTERMSIG(status);
161 status_cstr = "SIGNALED";
162 exited = true;
163 exit_status = -1;
164 }
165 else
166 {
167 status_cstr = "(???)";
168 }
169
170 // Scope for pthread_cancel_disabler
171 {
172 ScopedPThreadCancelDisabler pthread_cancel_disabler;
173
Caroline Tice926060e2010-10-29 21:48:37 +0000174 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000175 if (log)
176 log->Printf ("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p) => pid = %i, status = 0x%8.8x (%s), signal = %i, exit_state = %i",
177 function,
178 wait_pid,
179 options,
180 rusage,
181 pid,
182 status,
183 status_cstr,
184 signal,
185 exit_status);
186
187 if (exited || (signal != 0 && monitor_signals))
188 {
189 bool callback_return = callback (callback_baton, pid, signal, exit_status);
190
191 // If our process exited, then this thread should exit
192 if (exited)
193 break;
194 // If the callback returns true, it means this process should
195 // exit
196 if (callback_return)
197 break;
198 }
199 }
200 }
201 }
202
Caroline Tice926060e2010-10-29 21:48:37 +0000203 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000204 if (log)
205 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
206
207 return NULL;
208}
209
210size_t
211Host::GetPageSize()
212{
213 return ::getpagesize();
214}
215
216//------------------------------------------------------------------
217// Returns true if the host system is Big Endian.
218//------------------------------------------------------------------
219ByteOrder
220Host::GetByteOrder ()
221{
222 union EndianTest
223 {
224 uint32_t num;
225 uint8_t bytes[sizeof(uint32_t)];
226 } endian = { (uint16_t)0x11223344 };
227 switch (endian.bytes[0])
228 {
229 case 0x11: return eByteOrderLittle;
230 case 0x44: return eByteOrderBig;
231 case 0x33: return eByteOrderPDP;
232 }
233 return eByteOrderInvalid;
234}
235
236const ArchSpec &
237Host::GetArchitecture ()
238{
239 static ArchSpec g_host_arch;
240 if (!g_host_arch.IsValid())
241 {
242#if defined (__APPLE__)
243 uint32_t cputype, cpusubtype;
244 uint32_t is_64_bit_capable;
245 size_t len = sizeof(cputype);
246 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
247 {
248 len = sizeof(cpusubtype);
249 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) == 0)
250 g_host_arch.SetArch(cputype, cpusubtype);
251
252 len = sizeof (is_64_bit_capable);
253 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
254 {
255 if (is_64_bit_capable)
256 {
257 if (cputype == CPU_TYPE_I386 && cpusubtype == CPU_SUBTYPE_486)
258 cpusubtype = CPU_SUBTYPE_I386_ALL;
259
260 cputype |= CPU_ARCH_ABI64;
261 }
262 }
263 }
264#elif defined (__linux__)
265 g_host_arch.SetArch(7u, 144u);
266#endif
267 }
268 return g_host_arch;
269}
270
271const ConstString &
272Host::GetVendorString()
273{
274 static ConstString g_vendor;
275 if (!g_vendor)
276 {
277#if defined (__APPLE__)
278 char ostype[64];
279 size_t len = sizeof(ostype);
280 if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0)
281 g_vendor.SetCString (ostype);
282 else
283 g_vendor.SetCString("apple");
284#elif defined (__linux__)
285 g_vendor.SetCString("gnu");
286#endif
287 }
288 return g_vendor;
289}
290
291const ConstString &
292Host::GetOSString()
293{
294 static ConstString g_os_string;
295 if (!g_os_string)
296 {
297#if defined (__APPLE__)
298 g_os_string.SetCString("darwin");
299#elif defined (__linux__)
300 g_os_string.SetCString("linux");
301#endif
302 }
303 return g_os_string;
304}
305
306const ConstString &
307Host::GetTargetTriple()
308{
309 static ConstString g_host_triple;
310 if (!(g_host_triple))
311 {
312 StreamString triple;
313 triple.Printf("%s-%s-%s",
314 GetArchitecture().AsCString(),
315 GetVendorString().AsCString(),
316 GetOSString().AsCString());
317
318 std::transform (triple.GetString().begin(),
319 triple.GetString().end(),
320 triple.GetString().begin(),
321 ::tolower);
322
323 g_host_triple.SetCString(triple.GetString().c_str());
324 }
325 return g_host_triple;
326}
327
328lldb::pid_t
329Host::GetCurrentProcessID()
330{
331 return ::getpid();
332}
333
334lldb::tid_t
335Host::GetCurrentThreadID()
336{
337#if defined (__APPLE__)
338 return ::mach_thread_self();
339#else
340 return lldb::tid_t(pthread_self());
341#endif
342}
343
344const char *
345Host::GetSignalAsCString (int signo)
346{
347 switch (signo)
348 {
349 case SIGHUP: return "SIGHUP"; // 1 hangup
350 case SIGINT: return "SIGINT"; // 2 interrupt
351 case SIGQUIT: return "SIGQUIT"; // 3 quit
352 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
353 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
354 case SIGABRT: return "SIGABRT"; // 6 abort()
355#if defined(_POSIX_C_SOURCE)
356 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
357#else // !_POSIX_C_SOURCE
358 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
359#endif // !_POSIX_C_SOURCE
360 case SIGFPE: return "SIGFPE"; // 8 floating point exception
361 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
362 case SIGBUS: return "SIGBUS"; // 10 bus error
363 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
364 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
365 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
366 case SIGALRM: return "SIGALRM"; // 14 alarm clock
367 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
368 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
369 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
370 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
371 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
372 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
373 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
374 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
375#if !defined(_POSIX_C_SOURCE)
376 case SIGIO: return "SIGIO"; // 23 input/output possible signal
377#endif
378 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
379 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
380 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
381 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
382#if !defined(_POSIX_C_SOURCE)
383 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
384 case SIGINFO: return "SIGINFO"; // 29 information request
385#endif
386 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
387 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
388 default:
389 break;
390 }
391 return NULL;
392}
393
394void
395Host::WillTerminate ()
396{
397}
398
399#if !defined (__APPLE__) // see macosx/Host.mm
400void
401Host::ThreadCreated (const char *thread_name)
402{
403}
404#endif
405
406struct HostThreadCreateInfo
407{
408 std::string thread_name;
409 thread_func_t thread_fptr;
410 thread_arg_t thread_arg;
411
412 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
413 thread_name (name ? name : ""),
414 thread_fptr (fptr),
415 thread_arg (arg)
416 {
417 }
418};
419
420static thread_result_t
421ThreadCreateTrampoline (thread_arg_t arg)
422{
423 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
424 Host::ThreadCreated (info->thread_name.c_str());
425 thread_func_t thread_fptr = info->thread_fptr;
426 thread_arg_t thread_arg = info->thread_arg;
427
428 Log * log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
429 if (log)
430 log->Printf("thread created");
431
432 delete info;
433 return thread_fptr (thread_arg);
434}
435
436lldb::thread_t
437Host::ThreadCreate
438(
439 const char *thread_name,
440 thread_func_t thread_fptr,
441 thread_arg_t thread_arg,
442 Error *error
443)
444{
445 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
446
447 // Host::ThreadCreateTrampoline will delete this pointer for us.
448 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
449
450 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
451 if (err == 0)
452 {
453 if (error)
454 error->Clear();
455 return thread;
456 }
457
458 if (error)
459 error->SetError (err, eErrorTypePOSIX);
460
461 return LLDB_INVALID_HOST_THREAD;
462}
463
464bool
465Host::ThreadCancel (lldb::thread_t thread, Error *error)
466{
467 int err = ::pthread_cancel (thread);
468 if (error)
469 error->SetError(err, eErrorTypePOSIX);
470 return err == 0;
471}
472
473bool
474Host::ThreadDetach (lldb::thread_t thread, Error *error)
475{
476 int err = ::pthread_detach (thread);
477 if (error)
478 error->SetError(err, eErrorTypePOSIX);
479 return err == 0;
480}
481
482bool
483Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
484{
485 int err = ::pthread_join (thread, thread_result_ptr);
486 if (error)
487 error->SetError(err, eErrorTypePOSIX);
488 return err == 0;
489}
490
491//------------------------------------------------------------------
492// Control access to a static file thread name map using a single
493// static function to avoid a static constructor.
494//------------------------------------------------------------------
495static const char *
496ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
497{
498 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
499
500 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
501 Mutex::Locker locker(&g_mutex);
502
503 typedef std::map<uint64_t, std::string> thread_name_map;
504 // rdar://problem/8153284
505 // Fixed a crasher where during shutdown, loggings attempted to access the
506 // thread name but the static map instance had already been destructed.
507 // Another approach is to introduce a static guard object which monitors its
508 // own destruction and raises a flag, but this incurs more overhead.
509 static thread_name_map *g_thread_names_ptr = new thread_name_map();
510 thread_name_map &g_thread_names = *g_thread_names_ptr;
511
512 if (get)
513 {
514 // See if the thread name exists in our thread name pool
515 thread_name_map::iterator pos = g_thread_names.find(pid_tid);
516 if (pos != g_thread_names.end())
517 return pos->second.c_str();
518 }
519 else
520 {
521 // Set the thread name
522 g_thread_names[pid_tid] = name;
523 }
524 return NULL;
525}
526
527const char *
528Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
529{
530 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
531 if (name == NULL)
532 {
533#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
534 // We currently can only get the name of a thread in the current process.
535 if (pid == Host::GetCurrentProcessID())
536 {
537 char pthread_name[1024];
538 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
539 {
540 if (pthread_name[0])
541 {
542 // Set the thread in our string pool
543 ThreadNameAccessor (false, pid, tid, pthread_name);
544 // Get our copy of the thread name string
545 name = ThreadNameAccessor (true, pid, tid, NULL);
546 }
547 }
548 }
549#endif
550 }
551 return name;
552}
553
554void
555Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
556{
557 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
558 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
559 if (pid == LLDB_INVALID_PROCESS_ID)
560 pid = curr_pid;
561
562 if (tid == LLDB_INVALID_THREAD_ID)
563 tid = curr_tid;
564
565#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
566 // Set the pthread name if possible
567 if (pid == curr_pid && tid == curr_tid)
568 {
569 ::pthread_setname_np (name);
570 }
571#endif
572 ThreadNameAccessor (false, pid, tid, name);
573}
574
575FileSpec
576Host::GetProgramFileSpec ()
577{
578 static FileSpec g_program_filespec;
579 if (!g_program_filespec)
580 {
581#if defined (__APPLE__)
582 char program_fullpath[PATH_MAX];
583 // If DST is NULL, then return the number of bytes needed.
584 uint32_t len = sizeof(program_fullpath);
585 int err = _NSGetExecutablePath (program_fullpath, &len);
586 if (err == 0)
Greg Clayton537a7a82010-10-20 20:54:39 +0000587 g_program_filespec.SetFile (program_fullpath, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000588 else if (err == -1)
589 {
590 char *large_program_fullpath = (char *)::malloc (len + 1);
591
592 err = _NSGetExecutablePath (large_program_fullpath, &len);
593 if (err == 0)
Greg Clayton537a7a82010-10-20 20:54:39 +0000594 g_program_filespec.SetFile (large_program_fullpath, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000595
596 ::free (large_program_fullpath);
597 }
598#elif defined (__linux__)
599 char exe_path[PATH_MAX];
600 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
601 if (len >= 0)
602 g_program_filespec = FileSpec(exe_path);
603#elif defined (__FreeBSD__)
604 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
605 size_t exe_path_size;
606 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
607 {
608 char *exe_path = new char[exe_path_size];
609 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
610 g_program_filespec = FileSpec(exe_path);
611 }
612#endif
613 }
614 return g_program_filespec;
615}
616
617FileSpec
618Host::GetModuleFileSpecForHostAddress (const void *host_addr)
619{
620 FileSpec module_filespec;
621 Dl_info info;
622 if (::dladdr (host_addr, &info))
623 {
624 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000625 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000626 }
627 return module_filespec;
628}
629
630#if !defined (__APPLE__) // see Host.mm
631bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000632Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000633{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000634 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000635}
636#endif
637
Greg Clayton24b48ff2010-10-17 22:03:32 +0000638
639bool
640Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
641{
642 // To get paths related to LLDB we get the path to the exectuable that
643 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
644 // on linux this is assumed to be the "lldb" main executable. If LLDB on
645 // linux is actually in a shared library (lldb.so??) then this function will
646 // need to be modified to "do the right thing".
647
648 switch (path_type)
649 {
650 case ePathTypeLLDBShlibDir:
651 {
652 static ConstString g_lldb_so_dir;
653 if (!g_lldb_so_dir)
654 {
655 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
656 g_lldb_so_dir = lldb_file_spec.GetDirectory();
657 }
658 file_spec.GetDirectory() = g_lldb_so_dir;
659 return file_spec.GetDirectory();
660 }
661 break;
662
663 case ePathTypeSupportExecutableDir:
664 {
665 static ConstString g_lldb_support_exe_dir;
666 if (!g_lldb_support_exe_dir)
667 {
668 FileSpec lldb_file_spec;
669 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
670 {
671 char raw_path[PATH_MAX];
672 char resolved_path[PATH_MAX];
673 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
674
675#if defined (__APPLE__)
676 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
677 if (framework_pos)
678 {
679 framework_pos += strlen("LLDB.framework");
680 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
681 }
682#endif
683 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
684 g_lldb_support_exe_dir.SetCString(resolved_path);
685 }
686 }
687 file_spec.GetDirectory() = g_lldb_support_exe_dir;
688 return file_spec.GetDirectory();
689 }
690 break;
691
692 case ePathTypeHeaderDir:
693 {
694 static ConstString g_lldb_headers_dir;
695 if (!g_lldb_headers_dir)
696 {
697#if defined (__APPLE__)
698 FileSpec lldb_file_spec;
699 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
700 {
701 char raw_path[PATH_MAX];
702 char resolved_path[PATH_MAX];
703 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
704
705 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
706 if (framework_pos)
707 {
708 framework_pos += strlen("LLDB.framework");
709 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
710 }
711 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
712 g_lldb_headers_dir.SetCString(resolved_path);
713 }
714#else
715 // TODO: Anyone know how we can determine this for linux??
716 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
717#endif
718 }
719 file_spec.GetDirectory() = g_lldb_headers_dir;
720 return file_spec.GetDirectory();
721 }
722 break;
723
724 case ePathTypePythonDir:
725 {
726 // TODO: Anyone know how we can determine this for linux??
727 // For linux we are currently assuming the location of the lldb
728 // binary that contains this function is the directory that will
729 // contain lldb.so, lldb.py and embedded_interpreter.py...
730
731 static ConstString g_lldb_python_dir;
732 if (!g_lldb_python_dir)
733 {
734 FileSpec lldb_file_spec;
735 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
736 {
737 char raw_path[PATH_MAX];
738 char resolved_path[PATH_MAX];
739 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
740
741#if defined (__APPLE__)
742 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
743 if (framework_pos)
744 {
745 framework_pos += strlen("LLDB.framework");
746 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
747 }
748#endif
749 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
750 g_lldb_python_dir.SetCString(resolved_path);
751 }
752 }
753 file_spec.GetDirectory() = g_lldb_python_dir;
754 return file_spec.GetDirectory();
755 }
756 break;
757
758 default:
759 assert (!"Unhandled PathType");
760 break;
761 }
762
763 return false;
764}
765
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000766uint32_t
767Host::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
768{
769 uint32_t num_matches = 0;
770
771#if defined (__APPLE__)
772 int num_pids;
773 int size_of_pids;
Greg Claytonfb8876d2010-10-10 22:07:18 +0000774 std::vector<int> pid_list;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000775
776 size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
777 if (size_of_pids == -1)
778 return 0;
779
780 num_pids = size_of_pids/sizeof(int);
Greg Claytonfb8876d2010-10-10 22:07:18 +0000781
782 pid_list.resize (size_of_pids);
783 size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, &pid_list[0], size_of_pids);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000784 if (size_of_pids == -1)
785 return 0;
786
787 lldb::pid_t our_pid = getpid();
788
789 for (int i = 0; i < num_pids; i++)
790 {
791 struct proc_bsdinfo bsd_info;
792 int error = proc_pidinfo (pid_list[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
793 if (error == 0)
794 continue;
795
796 // Don't offer to attach to zombie processes, already traced or exiting
797 // processes, and of course, ourselves... It looks like passing the second arg of
798 // 0 to proc_listpids will exclude zombies anyway, but that's not documented so...
799 if (((bsd_info.pbi_flags & (PROC_FLAG_TRACED | PROC_FLAG_INEXIT)) != 0)
800 || (bsd_info.pbi_status == SZOMB)
801 || (bsd_info.pbi_pid == our_pid))
802 continue;
803 char pid_name[MAXCOMLEN * 2 + 1];
804 int name_len;
805 name_len = proc_name(bsd_info.pbi_pid, pid_name, MAXCOMLEN * 2);
806 if (name_len == 0)
807 continue;
808
809 if (strstr(pid_name, name) != pid_name)
810 continue;
811 matches.AppendString (pid_name);
812 pids.push_back (bsd_info.pbi_pid);
813 num_matches++;
814 }
815#endif
816
817 return num_matches;
818}
819
820ArchSpec
821Host::GetArchSpecForExistingProcess (lldb::pid_t pid)
822{
823 ArchSpec return_spec;
824
825#if defined (__APPLE__)
826 struct proc_bsdinfo bsd_info;
827 int error = proc_pidinfo (pid, PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
828 if (error == 0)
829 return return_spec;
830 if (bsd_info.pbi_flags & PROC_FLAG_LP64)
831 return_spec.SetArch(LLDB_ARCH_DEFAULT_64BIT);
832 else
833 return_spec.SetArch(LLDB_ARCH_DEFAULT_32BIT);
834#endif
835
836 return return_spec;
837}
838
839ArchSpec
840Host::GetArchSpecForExistingProcess (const char *process_name)
841{
842 ArchSpec returnSpec;
843 StringList matches;
844 std::vector<lldb::pid_t> pids;
845 if (ListProcessesMatchingName(process_name, matches, pids))
846 {
847 if (matches.GetSize() == 1)
848 {
849 return GetArchSpecForExistingProcess(pids[0]);
850 }
851 }
852 return returnSpec;
853}
854
855#if !defined (__APPLE__) // see macosx/Host.mm
856bool
857Host::OpenFileInExternalEditor (FileSpec &file_spec, uint32_t line_no)
858{
859 return false;
860}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000861
862
863lldb::pid_t
864LaunchApplication (const FileSpec &app_file_spec)
865{
866 return LLDB_INVALID_PROCESS_ID;
867}
868
869lldb::pid_t
870Host::LaunchInNewTerminal
871(
872 const char **argv,
873 const char **envp,
874 const ArchSpec *arch_spec,
875 bool stop_at_entry,
876 bool disable_aslr
877)
878{
879 return LLDB_INVALID_PROCESS_ID;
880}
881
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000882#endif