blob: 5755aa72c6d6d570ca9c8362baa1277ae6ddf916 [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 Clayton20fbf8d2011-01-13 01:23:43 +0000610 g_program_filespec.SetFile (program_fullpath, false);
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 Clayton20fbf8d2011-01-13 01:23:43 +0000617 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000618
619 ::free (large_program_fullpath);
620 }
621#elif defined (__linux__)
622 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000623 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
624 if (len > 0) {
625 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000626 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000627 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000628#elif defined (__FreeBSD__)
629 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
630 size_t exe_path_size;
631 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
632 {
Greg Clayton366795e2011-01-13 01:27:55 +0000633 char *exe_path = new char[exe_path_size];
634 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
635 g_program_filespec.SetFile(exe_path, false);
636 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000637 }
638#endif
639 }
640 return g_program_filespec;
641}
642
643FileSpec
644Host::GetModuleFileSpecForHostAddress (const void *host_addr)
645{
646 FileSpec module_filespec;
647 Dl_info info;
648 if (::dladdr (host_addr, &info))
649 {
650 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000651 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000652 }
653 return module_filespec;
654}
655
656#if !defined (__APPLE__) // see Host.mm
657bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000658Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000659{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000660 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000661}
662#endif
663
Greg Clayton24b48ff2010-10-17 22:03:32 +0000664
665bool
666Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
667{
Greg Clayton5d187e52011-01-08 20:28:42 +0000668 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000669 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
670 // on linux this is assumed to be the "lldb" main executable. If LLDB on
671 // linux is actually in a shared library (lldb.so??) then this function will
672 // need to be modified to "do the right thing".
673
674 switch (path_type)
675 {
676 case ePathTypeLLDBShlibDir:
677 {
678 static ConstString g_lldb_so_dir;
679 if (!g_lldb_so_dir)
680 {
681 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
682 g_lldb_so_dir = lldb_file_spec.GetDirectory();
683 }
684 file_spec.GetDirectory() = g_lldb_so_dir;
685 return file_spec.GetDirectory();
686 }
687 break;
688
689 case ePathTypeSupportExecutableDir:
690 {
691 static ConstString g_lldb_support_exe_dir;
692 if (!g_lldb_support_exe_dir)
693 {
694 FileSpec lldb_file_spec;
695 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
696 {
697 char raw_path[PATH_MAX];
698 char resolved_path[PATH_MAX];
699 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
700
701#if defined (__APPLE__)
702 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
703 if (framework_pos)
704 {
705 framework_pos += strlen("LLDB.framework");
706 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
707 }
708#endif
709 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
710 g_lldb_support_exe_dir.SetCString(resolved_path);
711 }
712 }
713 file_spec.GetDirectory() = g_lldb_support_exe_dir;
714 return file_spec.GetDirectory();
715 }
716 break;
717
718 case ePathTypeHeaderDir:
719 {
720 static ConstString g_lldb_headers_dir;
721 if (!g_lldb_headers_dir)
722 {
723#if defined (__APPLE__)
724 FileSpec lldb_file_spec;
725 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
726 {
727 char raw_path[PATH_MAX];
728 char resolved_path[PATH_MAX];
729 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
730
731 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
732 if (framework_pos)
733 {
734 framework_pos += strlen("LLDB.framework");
735 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
736 }
737 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
738 g_lldb_headers_dir.SetCString(resolved_path);
739 }
740#else
741 // TODO: Anyone know how we can determine this for linux??
742 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
743#endif
744 }
745 file_spec.GetDirectory() = g_lldb_headers_dir;
746 return file_spec.GetDirectory();
747 }
748 break;
749
750 case ePathTypePythonDir:
751 {
752 // TODO: Anyone know how we can determine this for linux??
753 // For linux we are currently assuming the location of the lldb
754 // binary that contains this function is the directory that will
755 // contain lldb.so, lldb.py and embedded_interpreter.py...
756
757 static ConstString g_lldb_python_dir;
758 if (!g_lldb_python_dir)
759 {
760 FileSpec lldb_file_spec;
761 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
762 {
763 char raw_path[PATH_MAX];
764 char resolved_path[PATH_MAX];
765 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
766
767#if defined (__APPLE__)
768 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
769 if (framework_pos)
770 {
771 framework_pos += strlen("LLDB.framework");
772 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
773 }
774#endif
775 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
776 g_lldb_python_dir.SetCString(resolved_path);
777 }
778 }
779 file_spec.GetDirectory() = g_lldb_python_dir;
780 return file_spec.GetDirectory();
781 }
782 break;
783
784 default:
785 assert (!"Unhandled PathType");
786 break;
787 }
788
789 return false;
790}
791
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000792uint32_t
793Host::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
794{
795 uint32_t num_matches = 0;
796
797#if defined (__APPLE__)
798 int num_pids;
799 int size_of_pids;
Greg Claytonfb8876d2010-10-10 22:07:18 +0000800 std::vector<int> pid_list;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000801
802 size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
803 if (size_of_pids == -1)
804 return 0;
805
806 num_pids = size_of_pids/sizeof(int);
Greg Claytonfb8876d2010-10-10 22:07:18 +0000807
808 pid_list.resize (size_of_pids);
809 size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, &pid_list[0], size_of_pids);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000810 if (size_of_pids == -1)
811 return 0;
812
813 lldb::pid_t our_pid = getpid();
814
815 for (int i = 0; i < num_pids; i++)
816 {
817 struct proc_bsdinfo bsd_info;
818 int error = proc_pidinfo (pid_list[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
819 if (error == 0)
820 continue;
821
822 // Don't offer to attach to zombie processes, already traced or exiting
823 // processes, and of course, ourselves... It looks like passing the second arg of
824 // 0 to proc_listpids will exclude zombies anyway, but that's not documented so...
825 if (((bsd_info.pbi_flags & (PROC_FLAG_TRACED | PROC_FLAG_INEXIT)) != 0)
826 || (bsd_info.pbi_status == SZOMB)
827 || (bsd_info.pbi_pid == our_pid))
828 continue;
829 char pid_name[MAXCOMLEN * 2 + 1];
830 int name_len;
831 name_len = proc_name(bsd_info.pbi_pid, pid_name, MAXCOMLEN * 2);
832 if (name_len == 0)
833 continue;
834
835 if (strstr(pid_name, name) != pid_name)
836 continue;
837 matches.AppendString (pid_name);
838 pids.push_back (bsd_info.pbi_pid);
839 num_matches++;
840 }
841#endif
842
843 return num_matches;
844}
845
846ArchSpec
847Host::GetArchSpecForExistingProcess (lldb::pid_t pid)
848{
849 ArchSpec return_spec;
850
851#if defined (__APPLE__)
852 struct proc_bsdinfo bsd_info;
853 int error = proc_pidinfo (pid, PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
854 if (error == 0)
855 return return_spec;
856 if (bsd_info.pbi_flags & PROC_FLAG_LP64)
857 return_spec.SetArch(LLDB_ARCH_DEFAULT_64BIT);
858 else
859 return_spec.SetArch(LLDB_ARCH_DEFAULT_32BIT);
860#endif
861
862 return return_spec;
863}
864
865ArchSpec
866Host::GetArchSpecForExistingProcess (const char *process_name)
867{
868 ArchSpec returnSpec;
869 StringList matches;
870 std::vector<lldb::pid_t> pids;
871 if (ListProcessesMatchingName(process_name, matches, pids))
872 {
873 if (matches.GetSize() == 1)
874 {
875 return GetArchSpecForExistingProcess(pids[0]);
876 }
877 }
878 return returnSpec;
879}
880
881#if !defined (__APPLE__) // see macosx/Host.mm
882bool
Greg Claytonb73620c2010-12-18 01:54:34 +0000883Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000884{
885 return false;
886}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000887
Greg Claytone98ac252010-11-10 04:57:04 +0000888void
889Host::SetCrashDescriptionWithFormat (const char *format, ...)
890{
891}
892
893void
894Host::SetCrashDescription (const char *description)
895{
896}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000897
898lldb::pid_t
899LaunchApplication (const FileSpec &app_file_spec)
900{
901 return LLDB_INVALID_PROCESS_ID;
902}
903
904lldb::pid_t
905Host::LaunchInNewTerminal
906(
Greg Claytonb73620c2010-12-18 01:54:34 +0000907 const char *tty_name,
Greg Clayton24b48ff2010-10-17 22:03:32 +0000908 const char **argv,
909 const char **envp,
910 const ArchSpec *arch_spec,
911 bool stop_at_entry,
912 bool disable_aslr
913)
914{
915 return LLDB_INVALID_PROCESS_ID;
916}
917
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000918#endif