blob: 9993a8493d1c8f3f7a6709cd5178836bf069a802 [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"
Sean Callananf35a96c2011-10-27 21:22:25 +000013#include "lldb/Core/Debugger.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000014#include "lldb/Core/Error.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000015#include "lldb/Core/Log.h"
16#include "lldb/Core/StreamString.h"
Greg Clayton14ef59f2011-02-08 00:35:34 +000017#include "lldb/Host/Config.h"
Greg Claytoncd548032011-02-01 01:31:41 +000018#include "lldb/Host/Endian.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000019#include "lldb/Host/FileSpec.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000020#include "lldb/Host/Mutex.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000021#include "lldb/Target/Process.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000022#include "lldb/Target/TargetList.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000023
Stephen Wilson7f513ba2011-02-24 19:15:09 +000024#include "llvm/Support/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000025#include "llvm/Support/MachO.h"
Stephen Wilson7f513ba2011-02-24 19:15:09 +000026
Greg Clayton8f3b21d2010-09-07 20:11:56 +000027#include <dlfcn.h>
28#include <errno.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000029#include <grp.h>
Stephen Wilsonec2d9782011-04-08 13:36:44 +000030#include <limits.h>
Greg Clayton58e26e02011-03-24 04:28:38 +000031#include <netdb.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000032#include <pwd.h>
33#include <sys/types.h>
34
Greg Clayton8f3b21d2010-09-07 20:11:56 +000035
36#if defined (__APPLE__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000037
Greg Clayton49ce6822010-10-31 03:01:06 +000038#include <dispatch/dispatch.h>
Greg Clayton8f3b21d2010-09-07 20:11:56 +000039#include <libproc.h>
40#include <mach-o/dyld.h>
Greg Claytonb5f67fb2011-02-05 06:36:35 +000041#include <sys/sysctl.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000042
Greg Clayton24bc5d92011-03-30 18:16:51 +000043
Greg Clayton0f577c22011-02-07 17:43:47 +000044#elif defined (__linux__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000045
Greg Clayton0f577c22011-02-07 17:43:47 +000046#include <sys/wait.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000047
Johnny Chen4b663292011-08-02 20:52:42 +000048#elif defined (__FreeBSD__)
49
50#include <sys/wait.h>
51#include <sys/sysctl.h>
52#include <pthread_np.h>
53
Greg Clayton8f3b21d2010-09-07 20:11:56 +000054#endif
55
56using namespace lldb;
57using namespace lldb_private;
58
59struct MonitorInfo
60{
61 lldb::pid_t pid; // The process ID to monitor
62 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
63 void *callback_baton; // The callback baton for the callback function
64 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
65};
66
67static void *
68MonitorChildProcessThreadFunction (void *arg);
69
70lldb::thread_t
71Host::StartMonitoringChildProcess
72(
73 Host::MonitorChildProcessCallback callback,
74 void *callback_baton,
75 lldb::pid_t pid,
76 bool monitor_signals
77)
78{
79 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
80 if (callback)
81 {
82 std::auto_ptr<MonitorInfo> info_ap(new MonitorInfo);
83
84 info_ap->pid = pid;
85 info_ap->callback = callback;
86 info_ap->callback_baton = callback_baton;
87 info_ap->monitor_signals = monitor_signals;
88
89 char thread_name[256];
90 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid);
91 thread = ThreadCreate (thread_name,
92 MonitorChildProcessThreadFunction,
93 info_ap.get(),
94 NULL);
95
Greg Clayton09c81ef2011-02-08 01:34:25 +000096 if (IS_VALID_LLDB_HOST_THREAD(thread))
Greg Clayton8f3b21d2010-09-07 20:11:56 +000097 info_ap.release();
98 }
99 return thread;
100}
101
102//------------------------------------------------------------------
103// Scoped class that will disable thread canceling when it is
104// constructed, and exception safely restore the previous value it
105// when it goes out of scope.
106//------------------------------------------------------------------
107class ScopedPThreadCancelDisabler
108{
109public:
110 ScopedPThreadCancelDisabler()
111 {
112 // Disable the ability for this thread to be cancelled
113 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
114 if (err != 0)
115 m_old_state = -1;
116
117 }
118
119 ~ScopedPThreadCancelDisabler()
120 {
121 // Restore the ability for this thread to be cancelled to what it
122 // previously was.
123 if (m_old_state != -1)
124 ::pthread_setcancelstate (m_old_state, 0);
125 }
126private:
127 int m_old_state; // Save the old cancelability state.
128};
129
130static void *
131MonitorChildProcessThreadFunction (void *arg)
132{
Greg Claytone005f2c2010-11-06 01:53:30 +0000133 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000134 const char *function = __FUNCTION__;
135 if (log)
136 log->Printf ("%s (arg = %p) thread starting...", function, arg);
137
138 MonitorInfo *info = (MonitorInfo *)arg;
139
140 const Host::MonitorChildProcessCallback callback = info->callback;
141 void * const callback_baton = info->callback_baton;
142 const lldb::pid_t pid = info->pid;
143 const bool monitor_signals = info->monitor_signals;
144
145 delete info;
146
147 int status = -1;
148 const int options = 0;
149 struct rusage *rusage = NULL;
150 while (1)
151 {
Caroline Tice926060e2010-10-29 21:48:37 +0000152 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000153 if (log)
154 log->Printf("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p)...", function, pid, options, rusage);
155
156 // Wait for all child processes
157 ::pthread_testcancel ();
158 const lldb::pid_t wait_pid = ::wait4 (pid, &status, options, rusage);
159 ::pthread_testcancel ();
160
161 if (wait_pid == -1)
162 {
163 if (errno == EINTR)
164 continue;
165 else
166 break;
167 }
168 else if (wait_pid == pid)
169 {
170 bool exited = false;
171 int signal = 0;
172 int exit_status = 0;
173 const char *status_cstr = NULL;
174 if (WIFSTOPPED(status))
175 {
176 signal = WSTOPSIG(status);
177 status_cstr = "STOPPED";
178 }
179 else if (WIFEXITED(status))
180 {
181 exit_status = WEXITSTATUS(status);
182 status_cstr = "EXITED";
183 exited = true;
184 }
185 else if (WIFSIGNALED(status))
186 {
187 signal = WTERMSIG(status);
188 status_cstr = "SIGNALED";
189 exited = true;
190 exit_status = -1;
191 }
192 else
193 {
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000194 status_cstr = "(\?\?\?)";
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000195 }
196
197 // Scope for pthread_cancel_disabler
198 {
199 ScopedPThreadCancelDisabler pthread_cancel_disabler;
200
Caroline Tice926060e2010-10-29 21:48:37 +0000201 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000202 if (log)
203 log->Printf ("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p) => pid = %i, status = 0x%8.8x (%s), signal = %i, exit_state = %i",
204 function,
205 wait_pid,
206 options,
207 rusage,
208 pid,
209 status,
210 status_cstr,
211 signal,
212 exit_status);
213
214 if (exited || (signal != 0 && monitor_signals))
215 {
216 bool callback_return = callback (callback_baton, pid, signal, exit_status);
217
218 // If our process exited, then this thread should exit
219 if (exited)
220 break;
221 // If the callback returns true, it means this process should
222 // exit
223 if (callback_return)
224 break;
225 }
226 }
227 }
228 }
229
Caroline Tice926060e2010-10-29 21:48:37 +0000230 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000231 if (log)
232 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
233
234 return NULL;
235}
236
237size_t
238Host::GetPageSize()
239{
240 return ::getpagesize();
241}
242
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000243const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000244Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000245{
Greg Clayton395fc332011-02-15 21:59:32 +0000246 static bool g_supports_32 = false;
247 static bool g_supports_64 = false;
248 static ArchSpec g_host_arch_32;
249 static ArchSpec g_host_arch_64;
250
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000251#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000252
253 // Apple is different in that it can support both 32 and 64 bit executables
254 // in the same operating system running concurrently. Here we detect the
255 // correct host architectures for both 32 and 64 bit including if 64 bit
256 // executables are supported on the system.
257
258 if (g_supports_32 == false && g_supports_64 == false)
259 {
260 // All apple systems support 32 bit execution.
261 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000262 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000263 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000264 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000265 ArchSpec host_arch;
266 // These will tell us about the kernel architecture, which even on a 64
267 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000268 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
269 {
Greg Clayton395fc332011-02-15 21:59:32 +0000270 len = sizeof (cpusubtype);
271 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
272 cpusubtype = CPU_TYPE_ANY;
273
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000274 len = sizeof (is_64_bit_capable);
275 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
276 {
277 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000278 g_supports_64 = true;
279 }
280
281 if (is_64_bit_capable)
282 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000283#if defined (__i386__) || defined (__x86_64__)
284 if (cpusubtype == CPU_SUBTYPE_486)
285 cpusubtype = CPU_SUBTYPE_I386_ALL;
286#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000287 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000288 {
Greg Clayton395fc332011-02-15 21:59:32 +0000289 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000290 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
291 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000292 }
293 else
294 {
295 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000296 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000297 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000298 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000299 }
300 }
Greg Clayton395fc332011-02-15 21:59:32 +0000301 else
302 {
Greg Claytonb3448432011-03-24 21:19:54 +0000303 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000304 g_host_arch_64.Clear();
305 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000306 }
Greg Clayton395fc332011-02-15 21:59:32 +0000307 }
308
309#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000310
Greg Clayton395fc332011-02-15 21:59:32 +0000311 if (g_supports_32 == false && g_supports_64 == false)
312 {
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000313 llvm::Triple triple(llvm::sys::getHostTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000314
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000315 g_host_arch_32.Clear();
316 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000317
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000318 switch (triple.getArch())
319 {
320 default:
321 g_host_arch_32.SetTriple(triple);
322 g_supports_32 = true;
323 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000324
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000325 case llvm::Triple::x86_64:
326 case llvm::Triple::sparcv9:
327 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000328 case llvm::Triple::cellspu:
329 g_host_arch_64.SetTriple(triple);
330 g_supports_64 = true;
331 break;
332 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000333
334 g_supports_32 = g_host_arch_32.IsValid();
335 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000336 }
Greg Clayton395fc332011-02-15 21:59:32 +0000337
338#endif // #else for #if defined (__APPLE__)
339
340 if (arch_kind == eSystemDefaultArchitecture32)
341 return g_host_arch_32;
342 else if (arch_kind == eSystemDefaultArchitecture64)
343 return g_host_arch_64;
344
345 if (g_supports_64)
346 return g_host_arch_64;
347
348 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000349}
350
351const ConstString &
352Host::GetVendorString()
353{
354 static ConstString g_vendor;
355 if (!g_vendor)
356 {
357#if defined (__APPLE__)
358 char ostype[64];
359 size_t len = sizeof(ostype);
360 if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0)
361 g_vendor.SetCString (ostype);
362 else
363 g_vendor.SetCString("apple");
364#elif defined (__linux__)
365 g_vendor.SetCString("gnu");
Johnny Chen4b663292011-08-02 20:52:42 +0000366#elif defined (__FreeBSD__)
367 g_vendor.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000368#endif
369 }
370 return g_vendor;
371}
372
373const ConstString &
374Host::GetOSString()
375{
376 static ConstString g_os_string;
377 if (!g_os_string)
378 {
379#if defined (__APPLE__)
380 g_os_string.SetCString("darwin");
381#elif defined (__linux__)
382 g_os_string.SetCString("linux");
Johnny Chen4b663292011-08-02 20:52:42 +0000383#elif defined (__FreeBSD__)
384 g_os_string.SetCString("freebsd");
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000385#endif
386 }
387 return g_os_string;
388}
389
390const ConstString &
391Host::GetTargetTriple()
392{
393 static ConstString g_host_triple;
394 if (!(g_host_triple))
395 {
396 StreamString triple;
397 triple.Printf("%s-%s-%s",
Greg Clayton940b1032011-02-23 00:35:02 +0000398 GetArchitecture().GetArchitectureName(),
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000399 GetVendorString().AsCString(),
400 GetOSString().AsCString());
401
402 std::transform (triple.GetString().begin(),
403 triple.GetString().end(),
404 triple.GetString().begin(),
405 ::tolower);
406
407 g_host_triple.SetCString(triple.GetString().c_str());
408 }
409 return g_host_triple;
410}
411
412lldb::pid_t
413Host::GetCurrentProcessID()
414{
415 return ::getpid();
416}
417
418lldb::tid_t
419Host::GetCurrentThreadID()
420{
421#if defined (__APPLE__)
422 return ::mach_thread_self();
Johnny Chen4b663292011-08-02 20:52:42 +0000423#elif defined(__FreeBSD__)
424 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000425#else
426 return lldb::tid_t(pthread_self());
427#endif
428}
429
430const char *
431Host::GetSignalAsCString (int signo)
432{
433 switch (signo)
434 {
435 case SIGHUP: return "SIGHUP"; // 1 hangup
436 case SIGINT: return "SIGINT"; // 2 interrupt
437 case SIGQUIT: return "SIGQUIT"; // 3 quit
438 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
439 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
440 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000441#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000442 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000443#endif
444#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000445 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000446#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000447 case SIGFPE: return "SIGFPE"; // 8 floating point exception
448 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
449 case SIGBUS: return "SIGBUS"; // 10 bus error
450 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
451 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
452 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
453 case SIGALRM: return "SIGALRM"; // 14 alarm clock
454 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
455 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
456 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
457 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
458 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
459 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
460 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
461 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
462#if !defined(_POSIX_C_SOURCE)
463 case SIGIO: return "SIGIO"; // 23 input/output possible signal
464#endif
465 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
466 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
467 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
468 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
469#if !defined(_POSIX_C_SOURCE)
470 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
471 case SIGINFO: return "SIGINFO"; // 29 information request
472#endif
473 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
474 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
475 default:
476 break;
477 }
478 return NULL;
479}
480
481void
482Host::WillTerminate ()
483{
484}
485
Johnny Chen4b663292011-08-02 20:52:42 +0000486#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000487void
488Host::ThreadCreated (const char *thread_name)
489{
490}
Greg Claytonb749a262010-12-03 06:02:24 +0000491
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000492void
Greg Claytonb749a262010-12-03 06:02:24 +0000493Host::Backtrace (Stream &strm, uint32_t max_frames)
494{
Greg Clayton52fd9842011-02-02 02:24:04 +0000495 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000496}
497
Greg Clayton638351a2010-12-04 00:10:17 +0000498size_t
499Host::GetEnvironment (StringList &env)
500{
Greg Clayton52fd9842011-02-02 02:24:04 +0000501 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000502 return 0;
503}
504
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000505#endif
506
507struct HostThreadCreateInfo
508{
509 std::string thread_name;
510 thread_func_t thread_fptr;
511 thread_arg_t thread_arg;
512
513 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
514 thread_name (name ? name : ""),
515 thread_fptr (fptr),
516 thread_arg (arg)
517 {
518 }
519};
520
521static thread_result_t
522ThreadCreateTrampoline (thread_arg_t arg)
523{
524 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
525 Host::ThreadCreated (info->thread_name.c_str());
526 thread_func_t thread_fptr = info->thread_fptr;
527 thread_arg_t thread_arg = info->thread_arg;
528
Greg Claytone005f2c2010-11-06 01:53:30 +0000529 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000530 if (log)
531 log->Printf("thread created");
532
533 delete info;
534 return thread_fptr (thread_arg);
535}
536
537lldb::thread_t
538Host::ThreadCreate
539(
540 const char *thread_name,
541 thread_func_t thread_fptr,
542 thread_arg_t thread_arg,
543 Error *error
544)
545{
546 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
547
548 // Host::ThreadCreateTrampoline will delete this pointer for us.
549 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
550
551 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
552 if (err == 0)
553 {
554 if (error)
555 error->Clear();
556 return thread;
557 }
558
559 if (error)
560 error->SetError (err, eErrorTypePOSIX);
561
562 return LLDB_INVALID_HOST_THREAD;
563}
564
565bool
566Host::ThreadCancel (lldb::thread_t thread, Error *error)
567{
568 int err = ::pthread_cancel (thread);
569 if (error)
570 error->SetError(err, eErrorTypePOSIX);
571 return err == 0;
572}
573
574bool
575Host::ThreadDetach (lldb::thread_t thread, Error *error)
576{
577 int err = ::pthread_detach (thread);
578 if (error)
579 error->SetError(err, eErrorTypePOSIX);
580 return err == 0;
581}
582
583bool
584Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
585{
586 int err = ::pthread_join (thread, thread_result_ptr);
587 if (error)
588 error->SetError(err, eErrorTypePOSIX);
589 return err == 0;
590}
591
592//------------------------------------------------------------------
593// Control access to a static file thread name map using a single
594// static function to avoid a static constructor.
595//------------------------------------------------------------------
596static const char *
597ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
598{
599 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
600
601 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
602 Mutex::Locker locker(&g_mutex);
603
604 typedef std::map<uint64_t, std::string> thread_name_map;
605 // rdar://problem/8153284
606 // Fixed a crasher where during shutdown, loggings attempted to access the
607 // thread name but the static map instance had already been destructed.
608 // Another approach is to introduce a static guard object which monitors its
609 // own destruction and raises a flag, but this incurs more overhead.
610 static thread_name_map *g_thread_names_ptr = new thread_name_map();
611 thread_name_map &g_thread_names = *g_thread_names_ptr;
612
613 if (get)
614 {
615 // See if the thread name exists in our thread name pool
616 thread_name_map::iterator pos = g_thread_names.find(pid_tid);
617 if (pos != g_thread_names.end())
618 return pos->second.c_str();
619 }
620 else
621 {
622 // Set the thread name
623 g_thread_names[pid_tid] = name;
624 }
625 return NULL;
626}
627
628const char *
629Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
630{
631 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
632 if (name == NULL)
633 {
634#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
635 // We currently can only get the name of a thread in the current process.
636 if (pid == Host::GetCurrentProcessID())
637 {
638 char pthread_name[1024];
639 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
640 {
641 if (pthread_name[0])
642 {
643 // Set the thread in our string pool
644 ThreadNameAccessor (false, pid, tid, pthread_name);
645 // Get our copy of the thread name string
646 name = ThreadNameAccessor (true, pid, tid, NULL);
647 }
648 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000649
650 if (name == NULL)
651 {
652 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
653 if (current_queue != NULL)
654 name = dispatch_queue_get_label (current_queue);
655 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000656 }
657#endif
658 }
659 return name;
660}
661
662void
663Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
664{
665 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
666 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
667 if (pid == LLDB_INVALID_PROCESS_ID)
668 pid = curr_pid;
669
670 if (tid == LLDB_INVALID_THREAD_ID)
671 tid = curr_tid;
672
673#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
674 // Set the pthread name if possible
675 if (pid == curr_pid && tid == curr_tid)
676 {
677 ::pthread_setname_np (name);
678 }
679#endif
680 ThreadNameAccessor (false, pid, tid, name);
681}
682
683FileSpec
684Host::GetProgramFileSpec ()
685{
686 static FileSpec g_program_filespec;
687 if (!g_program_filespec)
688 {
689#if defined (__APPLE__)
690 char program_fullpath[PATH_MAX];
691 // If DST is NULL, then return the number of bytes needed.
692 uint32_t len = sizeof(program_fullpath);
693 int err = _NSGetExecutablePath (program_fullpath, &len);
694 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000695 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000696 else if (err == -1)
697 {
698 char *large_program_fullpath = (char *)::malloc (len + 1);
699
700 err = _NSGetExecutablePath (large_program_fullpath, &len);
701 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000702 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000703
704 ::free (large_program_fullpath);
705 }
706#elif defined (__linux__)
707 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000708 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
709 if (len > 0) {
710 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000711 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000712 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000713#elif defined (__FreeBSD__)
714 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
715 size_t exe_path_size;
716 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
717 {
Greg Clayton366795e2011-01-13 01:27:55 +0000718 char *exe_path = new char[exe_path_size];
719 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
720 g_program_filespec.SetFile(exe_path, false);
721 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000722 }
723#endif
724 }
725 return g_program_filespec;
726}
727
728FileSpec
729Host::GetModuleFileSpecForHostAddress (const void *host_addr)
730{
731 FileSpec module_filespec;
732 Dl_info info;
733 if (::dladdr (host_addr, &info))
734 {
735 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000736 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000737 }
738 return module_filespec;
739}
740
741#if !defined (__APPLE__) // see Host.mm
742bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000743Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000744{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000745 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000746}
747#endif
748
Greg Clayton14ef59f2011-02-08 00:35:34 +0000749// Opaque info that tracks a dynamic library that was loaded
750struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000751{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000752 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
753 file_spec (fs),
754 open_options (o),
755 handle (h)
756 {
757 }
758
759 const FileSpec file_spec;
760 uint32_t open_options;
761 void * handle;
762};
763
764void *
765Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
766{
Greg Clayton52fd9842011-02-02 02:24:04 +0000767 char path[PATH_MAX];
768 if (file_spec.GetPath(path, sizeof(path)))
769 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000770 int mode = 0;
771
772 if (options & eDynamicLibraryOpenOptionLazy)
773 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000774 else
775 mode |= RTLD_NOW;
776
Greg Clayton14ef59f2011-02-08 00:35:34 +0000777
778 if (options & eDynamicLibraryOpenOptionLocal)
779 mode |= RTLD_LOCAL;
780 else
781 mode |= RTLD_GLOBAL;
782
783#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
784 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
785 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000786#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000787
788 void * opaque = ::dlopen (path, mode);
789
790 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000791 {
792 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000793 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000794 }
795 else
796 {
797 error.SetErrorString(::dlerror());
798 }
799 }
800 else
801 {
802 error.SetErrorString("failed to extract path");
803 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000804 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000805}
806
807Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000808Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000809{
810 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000811 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000812 {
813 error.SetErrorString ("invalid dynamic library handle");
814 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000815 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000816 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000817 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
818 if (::dlclose (dylib_info->handle) != 0)
819 {
820 error.SetErrorString(::dlerror());
821 }
822
823 dylib_info->open_options = 0;
824 dylib_info->handle = 0;
825 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000826 }
827 return error;
828}
829
830void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000831Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000832{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000833 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000834 {
835 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000836 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000837 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000838 {
839 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
840
841 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
842 if (symbol_addr)
843 {
844#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
845 // This host doesn't support limiting searches to this shared library
846 // so we need to verify that the match came from this shared library
847 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000848 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000849 {
850 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
851 if (match_dylib_spec != dylib_info->file_spec)
852 {
853 char dylib_path[PATH_MAX];
854 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
855 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
856 else
857 error.SetErrorString ("symbol not found");
858 return NULL;
859 }
860 }
861#endif
862 error.Clear();
863 return symbol_addr;
864 }
865 else
866 {
867 error.SetErrorString(::dlerror());
868 }
869 }
870 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000871}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000872
873bool
874Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
875{
Greg Clayton5d187e52011-01-08 20:28:42 +0000876 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000877 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
878 // on linux this is assumed to be the "lldb" main executable. If LLDB on
879 // linux is actually in a shared library (lldb.so??) then this function will
880 // need to be modified to "do the right thing".
881
882 switch (path_type)
883 {
884 case ePathTypeLLDBShlibDir:
885 {
886 static ConstString g_lldb_so_dir;
887 if (!g_lldb_so_dir)
888 {
889 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
890 g_lldb_so_dir = lldb_file_spec.GetDirectory();
891 }
892 file_spec.GetDirectory() = g_lldb_so_dir;
893 return file_spec.GetDirectory();
894 }
895 break;
896
897 case ePathTypeSupportExecutableDir:
898 {
899 static ConstString g_lldb_support_exe_dir;
900 if (!g_lldb_support_exe_dir)
901 {
902 FileSpec lldb_file_spec;
903 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
904 {
905 char raw_path[PATH_MAX];
906 char resolved_path[PATH_MAX];
907 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
908
909#if defined (__APPLE__)
910 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
911 if (framework_pos)
912 {
913 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000914#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000915 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000916#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000917 }
918#endif
919 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
920 g_lldb_support_exe_dir.SetCString(resolved_path);
921 }
922 }
923 file_spec.GetDirectory() = g_lldb_support_exe_dir;
924 return file_spec.GetDirectory();
925 }
926 break;
927
928 case ePathTypeHeaderDir:
929 {
930 static ConstString g_lldb_headers_dir;
931 if (!g_lldb_headers_dir)
932 {
933#if defined (__APPLE__)
934 FileSpec lldb_file_spec;
935 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
936 {
937 char raw_path[PATH_MAX];
938 char resolved_path[PATH_MAX];
939 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
940
941 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
942 if (framework_pos)
943 {
944 framework_pos += strlen("LLDB.framework");
945 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
946 }
947 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
948 g_lldb_headers_dir.SetCString(resolved_path);
949 }
950#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000951 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000952 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
953#endif
954 }
955 file_spec.GetDirectory() = g_lldb_headers_dir;
956 return file_spec.GetDirectory();
957 }
958 break;
959
960 case ePathTypePythonDir:
961 {
Greg Clayton52fd9842011-02-02 02:24:04 +0000962 // TODO: Anyone know how we can determine this for linux? Other systems?
Greg Clayton24b48ff2010-10-17 22:03:32 +0000963 // For linux we are currently assuming the location of the lldb
964 // binary that contains this function is the directory that will
965 // contain lldb.so, lldb.py and embedded_interpreter.py...
966
967 static ConstString g_lldb_python_dir;
968 if (!g_lldb_python_dir)
969 {
970 FileSpec lldb_file_spec;
971 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
972 {
973 char raw_path[PATH_MAX];
974 char resolved_path[PATH_MAX];
975 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
976
977#if defined (__APPLE__)
978 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
979 if (framework_pos)
980 {
981 framework_pos += strlen("LLDB.framework");
982 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
983 }
984#endif
985 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
986 g_lldb_python_dir.SetCString(resolved_path);
987 }
988 }
989 file_spec.GetDirectory() = g_lldb_python_dir;
990 return file_spec.GetDirectory();
991 }
992 break;
993
Greg Clayton52fd9842011-02-02 02:24:04 +0000994 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
995 {
996#if defined (__APPLE__)
997 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +0000998 static bool g_lldb_system_plugin_dir_located = false;
999 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001000 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001001 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001002 FileSpec lldb_file_spec;
1003 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1004 {
1005 char raw_path[PATH_MAX];
1006 char resolved_path[PATH_MAX];
1007 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1008
1009 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1010 if (framework_pos)
1011 {
1012 framework_pos += strlen("LLDB.framework");
1013 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001014 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1015 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001016 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001017 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001018 }
1019 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001020
1021 if (g_lldb_system_plugin_dir)
1022 {
1023 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1024 return true;
1025 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001026#endif
1027 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1028 return false;
1029 }
1030 break;
1031
1032 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1033 {
1034#if defined (__APPLE__)
1035 static ConstString g_lldb_user_plugin_dir;
1036 if (!g_lldb_user_plugin_dir)
1037 {
1038 char user_plugin_path[PATH_MAX];
1039 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1040 user_plugin_path,
1041 sizeof(user_plugin_path)))
1042 {
1043 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1044 }
1045 }
1046 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1047 return file_spec.GetDirectory();
1048#endif
1049 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1050 return false;
1051 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001052 default:
1053 assert (!"Unhandled PathType");
1054 break;
1055 }
1056
1057 return false;
1058}
1059
Greg Clayton58e26e02011-03-24 04:28:38 +00001060
1061bool
1062Host::GetHostname (std::string &s)
1063{
1064 char hostname[PATH_MAX];
1065 hostname[sizeof(hostname) - 1] = '\0';
1066 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1067 {
1068 struct hostent* h = ::gethostbyname (hostname);
1069 if (h)
1070 s.assign (h->h_name);
1071 else
1072 s.assign (hostname);
1073 return true;
1074 }
1075 return false;
1076}
1077
Greg Clayton24bc5d92011-03-30 18:16:51 +00001078const char *
1079Host::GetUserName (uint32_t uid, std::string &user_name)
1080{
1081 struct passwd user_info;
1082 struct passwd *user_info_ptr = &user_info;
1083 char user_buffer[PATH_MAX];
1084 size_t user_buffer_size = sizeof(user_buffer);
1085 if (::getpwuid_r (uid,
1086 &user_info,
1087 user_buffer,
1088 user_buffer_size,
1089 &user_info_ptr) == 0)
1090 {
1091 if (user_info_ptr)
1092 {
1093 user_name.assign (user_info_ptr->pw_name);
1094 return user_name.c_str();
1095 }
1096 }
1097 user_name.clear();
1098 return NULL;
1099}
1100
1101const char *
1102Host::GetGroupName (uint32_t gid, std::string &group_name)
1103{
1104 char group_buffer[PATH_MAX];
1105 size_t group_buffer_size = sizeof(group_buffer);
1106 struct group group_info;
1107 struct group *group_info_ptr = &group_info;
1108 // Try the threadsafe version first
1109 if (::getgrgid_r (gid,
1110 &group_info,
1111 group_buffer,
1112 group_buffer_size,
1113 &group_info_ptr) == 0)
1114 {
1115 if (group_info_ptr)
1116 {
1117 group_name.assign (group_info_ptr->gr_name);
1118 return group_name.c_str();
1119 }
1120 }
1121 else
1122 {
1123 // The threadsafe version isn't currently working
1124 // for me on darwin, but the non-threadsafe version
1125 // is, so I am calling it below.
1126 group_info_ptr = ::getgrgid (gid);
1127 if (group_info_ptr)
1128 {
1129 group_name.assign (group_info_ptr->gr_name);
1130 return group_name.c_str();
1131 }
1132 }
1133 group_name.clear();
1134 return NULL;
1135}
1136
Johnny Chen4b663292011-08-02 20:52:42 +00001137#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001138bool
1139Host::GetOSBuildString (std::string &s)
1140{
1141 s.clear();
1142 return false;
1143}
1144
1145bool
1146Host::GetOSKernelDescription (std::string &s)
1147{
1148 s.clear();
1149 return false;
1150}
Johnny Chen4b663292011-08-02 20:52:42 +00001151#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001152
Johnny Chen4b663292011-08-02 20:52:42 +00001153#if !defined(__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001154uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001155Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001156{
1157 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001158 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001159}
Johnny Chen4b663292011-08-02 20:52:42 +00001160#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001161
Johnny Chen4b663292011-08-02 20:52:42 +00001162#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001163bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001164Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001165{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001166 process_info.Clear();
1167 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001168}
Johnny Chen4b663292011-08-02 20:52:42 +00001169#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001170
Sean Callananf35a96c2011-10-27 21:22:25 +00001171lldb::TargetSP
1172Host::GetDummyTarget (lldb_private::Debugger &debugger)
1173{
1174 static TargetSP dummy_target;
1175
1176 if (!dummy_target)
1177 {
1178 Error err = debugger.GetTargetList().CreateTarget(debugger,
1179 FileSpec(),
1180 Host::GetTargetTriple().AsCString(),
1181 false,
1182 NULL,
1183 dummy_target);
1184 }
1185
1186 return dummy_target;
1187}
1188
Johnny Chen4b663292011-08-02 20:52:42 +00001189#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001190bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001191Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001192{
1193 return false;
1194}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001195
Greg Claytone98ac252010-11-10 04:57:04 +00001196void
1197Host::SetCrashDescriptionWithFormat (const char *format, ...)
1198{
1199}
1200
1201void
1202Host::SetCrashDescription (const char *description)
1203{
1204}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001205
1206lldb::pid_t
1207LaunchApplication (const FileSpec &app_file_spec)
1208{
1209 return LLDB_INVALID_PROCESS_ID;
1210}
1211
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001212#endif