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