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