blob: c00c87f5ef7b2bfe1960b65bb194031dd47af562 [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 Clayton8f3b21d2010-09-07 20:11:56 +0000412#endif
413
414struct HostThreadCreateInfo
415{
416 std::string thread_name;
417 thread_func_t thread_fptr;
418 thread_arg_t thread_arg;
419
420 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
421 thread_name (name ? name : ""),
422 thread_fptr (fptr),
423 thread_arg (arg)
424 {
425 }
426};
427
428static thread_result_t
429ThreadCreateTrampoline (thread_arg_t arg)
430{
431 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
432 Host::ThreadCreated (info->thread_name.c_str());
433 thread_func_t thread_fptr = info->thread_fptr;
434 thread_arg_t thread_arg = info->thread_arg;
435
Greg Claytone005f2c2010-11-06 01:53:30 +0000436 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000437 if (log)
438 log->Printf("thread created");
439
440 delete info;
441 return thread_fptr (thread_arg);
442}
443
444lldb::thread_t
445Host::ThreadCreate
446(
447 const char *thread_name,
448 thread_func_t thread_fptr,
449 thread_arg_t thread_arg,
450 Error *error
451)
452{
453 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
454
455 // Host::ThreadCreateTrampoline will delete this pointer for us.
456 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
457
458 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
459 if (err == 0)
460 {
461 if (error)
462 error->Clear();
463 return thread;
464 }
465
466 if (error)
467 error->SetError (err, eErrorTypePOSIX);
468
469 return LLDB_INVALID_HOST_THREAD;
470}
471
472bool
473Host::ThreadCancel (lldb::thread_t thread, Error *error)
474{
475 int err = ::pthread_cancel (thread);
476 if (error)
477 error->SetError(err, eErrorTypePOSIX);
478 return err == 0;
479}
480
481bool
482Host::ThreadDetach (lldb::thread_t thread, Error *error)
483{
484 int err = ::pthread_detach (thread);
485 if (error)
486 error->SetError(err, eErrorTypePOSIX);
487 return err == 0;
488}
489
490bool
491Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
492{
493 int err = ::pthread_join (thread, thread_result_ptr);
494 if (error)
495 error->SetError(err, eErrorTypePOSIX);
496 return err == 0;
497}
498
499//------------------------------------------------------------------
500// Control access to a static file thread name map using a single
501// static function to avoid a static constructor.
502//------------------------------------------------------------------
503static const char *
504ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
505{
506 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
507
508 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
509 Mutex::Locker locker(&g_mutex);
510
511 typedef std::map<uint64_t, std::string> thread_name_map;
512 // rdar://problem/8153284
513 // Fixed a crasher where during shutdown, loggings attempted to access the
514 // thread name but the static map instance had already been destructed.
515 // Another approach is to introduce a static guard object which monitors its
516 // own destruction and raises a flag, but this incurs more overhead.
517 static thread_name_map *g_thread_names_ptr = new thread_name_map();
518 thread_name_map &g_thread_names = *g_thread_names_ptr;
519
520 if (get)
521 {
522 // See if the thread name exists in our thread name pool
523 thread_name_map::iterator pos = g_thread_names.find(pid_tid);
524 if (pos != g_thread_names.end())
525 return pos->second.c_str();
526 }
527 else
528 {
529 // Set the thread name
530 g_thread_names[pid_tid] = name;
531 }
532 return NULL;
533}
534
535const char *
536Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
537{
538 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
539 if (name == NULL)
540 {
541#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
542 // We currently can only get the name of a thread in the current process.
543 if (pid == Host::GetCurrentProcessID())
544 {
545 char pthread_name[1024];
546 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
547 {
548 if (pthread_name[0])
549 {
550 // Set the thread in our string pool
551 ThreadNameAccessor (false, pid, tid, pthread_name);
552 // Get our copy of the thread name string
553 name = ThreadNameAccessor (true, pid, tid, NULL);
554 }
555 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000556
557 if (name == NULL)
558 {
559 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
560 if (current_queue != NULL)
561 name = dispatch_queue_get_label (current_queue);
562 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000563 }
564#endif
565 }
566 return name;
567}
568
569void
570Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
571{
572 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
573 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
574 if (pid == LLDB_INVALID_PROCESS_ID)
575 pid = curr_pid;
576
577 if (tid == LLDB_INVALID_THREAD_ID)
578 tid = curr_tid;
579
580#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
581 // Set the pthread name if possible
582 if (pid == curr_pid && tid == curr_tid)
583 {
584 ::pthread_setname_np (name);
585 }
586#endif
587 ThreadNameAccessor (false, pid, tid, name);
588}
589
590FileSpec
591Host::GetProgramFileSpec ()
592{
593 static FileSpec g_program_filespec;
594 if (!g_program_filespec)
595 {
596#if defined (__APPLE__)
597 char program_fullpath[PATH_MAX];
598 // If DST is NULL, then return the number of bytes needed.
599 uint32_t len = sizeof(program_fullpath);
600 int err = _NSGetExecutablePath (program_fullpath, &len);
601 if (err == 0)
Greg Clayton537a7a82010-10-20 20:54:39 +0000602 g_program_filespec.SetFile (program_fullpath, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000603 else if (err == -1)
604 {
605 char *large_program_fullpath = (char *)::malloc (len + 1);
606
607 err = _NSGetExecutablePath (large_program_fullpath, &len);
608 if (err == 0)
Greg Clayton537a7a82010-10-20 20:54:39 +0000609 g_program_filespec.SetFile (large_program_fullpath, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000610
611 ::free (large_program_fullpath);
612 }
613#elif defined (__linux__)
614 char exe_path[PATH_MAX];
615 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
616 if (len >= 0)
617 g_program_filespec = FileSpec(exe_path);
618#elif defined (__FreeBSD__)
619 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
620 size_t exe_path_size;
621 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
622 {
623 char *exe_path = new char[exe_path_size];
624 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
625 g_program_filespec = FileSpec(exe_path);
626 }
627#endif
628 }
629 return g_program_filespec;
630}
631
632FileSpec
633Host::GetModuleFileSpecForHostAddress (const void *host_addr)
634{
635 FileSpec module_filespec;
636 Dl_info info;
637 if (::dladdr (host_addr, &info))
638 {
639 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000640 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000641 }
642 return module_filespec;
643}
644
645#if !defined (__APPLE__) // see Host.mm
646bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000647Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000648{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000649 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000650}
651#endif
652
Greg Clayton24b48ff2010-10-17 22:03:32 +0000653
654bool
655Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
656{
657 // To get paths related to LLDB we get the path to the exectuable that
658 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
659 // on linux this is assumed to be the "lldb" main executable. If LLDB on
660 // linux is actually in a shared library (lldb.so??) then this function will
661 // need to be modified to "do the right thing".
662
663 switch (path_type)
664 {
665 case ePathTypeLLDBShlibDir:
666 {
667 static ConstString g_lldb_so_dir;
668 if (!g_lldb_so_dir)
669 {
670 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
671 g_lldb_so_dir = lldb_file_spec.GetDirectory();
672 }
673 file_spec.GetDirectory() = g_lldb_so_dir;
674 return file_spec.GetDirectory();
675 }
676 break;
677
678 case ePathTypeSupportExecutableDir:
679 {
680 static ConstString g_lldb_support_exe_dir;
681 if (!g_lldb_support_exe_dir)
682 {
683 FileSpec lldb_file_spec;
684 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
685 {
686 char raw_path[PATH_MAX];
687 char resolved_path[PATH_MAX];
688 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
689
690#if defined (__APPLE__)
691 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
692 if (framework_pos)
693 {
694 framework_pos += strlen("LLDB.framework");
695 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
696 }
697#endif
698 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
699 g_lldb_support_exe_dir.SetCString(resolved_path);
700 }
701 }
702 file_spec.GetDirectory() = g_lldb_support_exe_dir;
703 return file_spec.GetDirectory();
704 }
705 break;
706
707 case ePathTypeHeaderDir:
708 {
709 static ConstString g_lldb_headers_dir;
710 if (!g_lldb_headers_dir)
711 {
712#if defined (__APPLE__)
713 FileSpec lldb_file_spec;
714 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
715 {
716 char raw_path[PATH_MAX];
717 char resolved_path[PATH_MAX];
718 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
719
720 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
721 if (framework_pos)
722 {
723 framework_pos += strlen("LLDB.framework");
724 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
725 }
726 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
727 g_lldb_headers_dir.SetCString(resolved_path);
728 }
729#else
730 // TODO: Anyone know how we can determine this for linux??
731 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
732#endif
733 }
734 file_spec.GetDirectory() = g_lldb_headers_dir;
735 return file_spec.GetDirectory();
736 }
737 break;
738
739 case ePathTypePythonDir:
740 {
741 // TODO: Anyone know how we can determine this for linux??
742 // For linux we are currently assuming the location of the lldb
743 // binary that contains this function is the directory that will
744 // contain lldb.so, lldb.py and embedded_interpreter.py...
745
746 static ConstString g_lldb_python_dir;
747 if (!g_lldb_python_dir)
748 {
749 FileSpec lldb_file_spec;
750 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
751 {
752 char raw_path[PATH_MAX];
753 char resolved_path[PATH_MAX];
754 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
755
756#if defined (__APPLE__)
757 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
758 if (framework_pos)
759 {
760 framework_pos += strlen("LLDB.framework");
761 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
762 }
763#endif
764 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
765 g_lldb_python_dir.SetCString(resolved_path);
766 }
767 }
768 file_spec.GetDirectory() = g_lldb_python_dir;
769 return file_spec.GetDirectory();
770 }
771 break;
772
773 default:
774 assert (!"Unhandled PathType");
775 break;
776 }
777
778 return false;
779}
780
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000781uint32_t
782Host::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
783{
784 uint32_t num_matches = 0;
785
786#if defined (__APPLE__)
787 int num_pids;
788 int size_of_pids;
Greg Claytonfb8876d2010-10-10 22:07:18 +0000789 std::vector<int> pid_list;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000790
791 size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
792 if (size_of_pids == -1)
793 return 0;
794
795 num_pids = size_of_pids/sizeof(int);
Greg Claytonfb8876d2010-10-10 22:07:18 +0000796
797 pid_list.resize (size_of_pids);
798 size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, &pid_list[0], size_of_pids);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000799 if (size_of_pids == -1)
800 return 0;
801
802 lldb::pid_t our_pid = getpid();
803
804 for (int i = 0; i < num_pids; i++)
805 {
806 struct proc_bsdinfo bsd_info;
807 int error = proc_pidinfo (pid_list[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
808 if (error == 0)
809 continue;
810
811 // Don't offer to attach to zombie processes, already traced or exiting
812 // processes, and of course, ourselves... It looks like passing the second arg of
813 // 0 to proc_listpids will exclude zombies anyway, but that's not documented so...
814 if (((bsd_info.pbi_flags & (PROC_FLAG_TRACED | PROC_FLAG_INEXIT)) != 0)
815 || (bsd_info.pbi_status == SZOMB)
816 || (bsd_info.pbi_pid == our_pid))
817 continue;
818 char pid_name[MAXCOMLEN * 2 + 1];
819 int name_len;
820 name_len = proc_name(bsd_info.pbi_pid, pid_name, MAXCOMLEN * 2);
821 if (name_len == 0)
822 continue;
823
824 if (strstr(pid_name, name) != pid_name)
825 continue;
826 matches.AppendString (pid_name);
827 pids.push_back (bsd_info.pbi_pid);
828 num_matches++;
829 }
830#endif
831
832 return num_matches;
833}
834
835ArchSpec
836Host::GetArchSpecForExistingProcess (lldb::pid_t pid)
837{
838 ArchSpec return_spec;
839
840#if defined (__APPLE__)
841 struct proc_bsdinfo bsd_info;
842 int error = proc_pidinfo (pid, PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
843 if (error == 0)
844 return return_spec;
845 if (bsd_info.pbi_flags & PROC_FLAG_LP64)
846 return_spec.SetArch(LLDB_ARCH_DEFAULT_64BIT);
847 else
848 return_spec.SetArch(LLDB_ARCH_DEFAULT_32BIT);
849#endif
850
851 return return_spec;
852}
853
854ArchSpec
855Host::GetArchSpecForExistingProcess (const char *process_name)
856{
857 ArchSpec returnSpec;
858 StringList matches;
859 std::vector<lldb::pid_t> pids;
860 if (ListProcessesMatchingName(process_name, matches, pids))
861 {
862 if (matches.GetSize() == 1)
863 {
864 return GetArchSpecForExistingProcess(pids[0]);
865 }
866 }
867 return returnSpec;
868}
869
870#if !defined (__APPLE__) // see macosx/Host.mm
871bool
872Host::OpenFileInExternalEditor (FileSpec &file_spec, uint32_t line_no)
873{
874 return false;
875}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000876
Greg Claytone98ac252010-11-10 04:57:04 +0000877void
878Host::SetCrashDescriptionWithFormat (const char *format, ...)
879{
880}
881
882void
883Host::SetCrashDescription (const char *description)
884{
885}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000886
887lldb::pid_t
888LaunchApplication (const FileSpec &app_file_spec)
889{
890 return LLDB_INVALID_PROCESS_ID;
891}
892
893lldb::pid_t
894Host::LaunchInNewTerminal
895(
896 const char **argv,
897 const char **envp,
898 const ArchSpec *arch_spec,
899 bool stop_at_entry,
900 bool disable_aslr
901)
902{
903 return LLDB_INVALID_PROCESS_ID;
904}
905
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000906#endif