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