blob: 53e3ddd154357e0b9b1d484ef07578aabaabdeca [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton8f3b21d2010-09-07 20:11:56 +000012#include "lldb/Host/Host.h"
13#include "lldb/Core/ArchSpec.h"
14#include "lldb/Core/ConstString.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000015#include "lldb/Core/Debugger.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000016#include "lldb/Core/Error.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/StreamString.h"
Jim Ingham35dd4962012-05-04 19:24:49 +000019#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton14ef59f2011-02-08 00:35:34 +000020#include "lldb/Host/Config.h"
Greg Claytoncd548032011-02-01 01:31:41 +000021#include "lldb/Host/Endian.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000022#include "lldb/Host/FileSpec.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000023#include "lldb/Host/Mutex.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000024#include "lldb/Target/Process.h"
Sean Callananf35a96c2011-10-27 21:22:25 +000025#include "lldb/Target/TargetList.h"
Greg Clayton8f3b21d2010-09-07 20:11:56 +000026
Stephen Wilson7f513ba2011-02-24 19:15:09 +000027#include "llvm/Support/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000028#include "llvm/Support/MachO.h"
Stephen Wilson7f513ba2011-02-24 19:15:09 +000029
Greg Clayton8f3b21d2010-09-07 20:11:56 +000030#include <dlfcn.h>
31#include <errno.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000032#include <grp.h>
Stephen Wilsonec2d9782011-04-08 13:36:44 +000033#include <limits.h>
Greg Clayton58e26e02011-03-24 04:28:38 +000034#include <netdb.h>
Greg Clayton24bc5d92011-03-30 18:16:51 +000035#include <pwd.h>
36#include <sys/types.h>
37
Greg Clayton8f3b21d2010-09-07 20:11:56 +000038
39#if defined (__APPLE__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000040
Greg Clayton49ce6822010-10-31 03:01:06 +000041#include <dispatch/dispatch.h>
Greg Clayton8f3b21d2010-09-07 20:11:56 +000042#include <libproc.h>
43#include <mach-o/dyld.h>
Greg Claytone93725b2012-09-18 18:19:49 +000044#include <mach/mach_port.h>
Greg Claytonb5f67fb2011-02-05 06:36:35 +000045#include <sys/sysctl.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000046
Greg Clayton24bc5d92011-03-30 18:16:51 +000047
Greg Clayton0f577c22011-02-07 17:43:47 +000048#elif defined (__linux__)
Greg Clayton14ef59f2011-02-08 00:35:34 +000049
Greg Clayton0f577c22011-02-07 17:43:47 +000050#include <sys/wait.h>
Greg Clayton14ef59f2011-02-08 00:35:34 +000051
Johnny Chen4b663292011-08-02 20:52:42 +000052#elif defined (__FreeBSD__)
53
54#include <sys/wait.h>
55#include <sys/sysctl.h>
56#include <pthread_np.h>
57
Greg Clayton8f3b21d2010-09-07 20:11:56 +000058#endif
59
60using namespace lldb;
61using namespace lldb_private;
62
Greg Clayton1c4642c2011-11-16 05:37:56 +000063
Greg Claytonc518fe72011-11-17 19:41:57 +000064#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +000065struct MonitorInfo
66{
67 lldb::pid_t pid; // The process ID to monitor
68 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
69 void *callback_baton; // The callback baton for the callback function
70 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
71};
72
73static void *
74MonitorChildProcessThreadFunction (void *arg);
75
76lldb::thread_t
77Host::StartMonitoringChildProcess
78(
79 Host::MonitorChildProcessCallback callback,
80 void *callback_baton,
81 lldb::pid_t pid,
82 bool monitor_signals
83)
84{
85 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton1c4642c2011-11-16 05:37:56 +000086 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton8f3b21d2010-09-07 20:11:56 +000087
Greg Clayton1c4642c2011-11-16 05:37:56 +000088 info_ptr->pid = pid;
89 info_ptr->callback = callback;
90 info_ptr->callback_baton = callback_baton;
91 info_ptr->monitor_signals = monitor_signals;
92
93 char thread_name[256];
Daniel Malea5f35a4b2012-11-29 21:49:15 +000094 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
Greg Clayton1c4642c2011-11-16 05:37:56 +000095 thread = ThreadCreate (thread_name,
96 MonitorChildProcessThreadFunction,
97 info_ptr,
98 NULL);
99
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000100 return thread;
101}
102
103//------------------------------------------------------------------
104// Scoped class that will disable thread canceling when it is
105// constructed, and exception safely restore the previous value it
106// when it goes out of scope.
107//------------------------------------------------------------------
108class ScopedPThreadCancelDisabler
109{
110public:
111 ScopedPThreadCancelDisabler()
112 {
113 // Disable the ability for this thread to be cancelled
114 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
115 if (err != 0)
116 m_old_state = -1;
117
118 }
119
120 ~ScopedPThreadCancelDisabler()
121 {
122 // Restore the ability for this thread to be cancelled to what it
123 // previously was.
124 if (m_old_state != -1)
125 ::pthread_setcancelstate (m_old_state, 0);
126 }
127private:
128 int m_old_state; // Save the old cancelability state.
129};
130
131static void *
132MonitorChildProcessThreadFunction (void *arg)
133{
Greg Claytone005f2c2010-11-06 01:53:30 +0000134 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000135 const char *function = __FUNCTION__;
136 if (log)
137 log->Printf ("%s (arg = %p) thread starting...", function, arg);
138
139 MonitorInfo *info = (MonitorInfo *)arg;
140
141 const Host::MonitorChildProcessCallback callback = info->callback;
142 void * const callback_baton = info->callback_baton;
143 const lldb::pid_t pid = info->pid;
144 const bool monitor_signals = info->monitor_signals;
145
146 delete info;
147
148 int status = -1;
149 const int options = 0;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000150 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)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000154 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000155
156 // Wait for all child processes
157 ::pthread_testcancel ();
Greg Clayton1c4642c2011-11-16 05:37:56 +0000158 const lldb::pid_t wait_pid = ::waitpid (pid, &status, options);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000159 ::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)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000203 log->Printf ("%s ::waitpid (pid = %" PRIu64 ", &status, options = %i) => pid = %" PRIu64 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000204 function,
205 wait_pid,
206 options,
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000207 pid,
208 status,
209 status_cstr,
210 signal,
211 exit_status);
212
213 if (exited || (signal != 0 && monitor_signals))
214 {
Greg Clayton1c4642c2011-11-16 05:37:56 +0000215 bool callback_return = false;
216 if (callback)
217 callback_return = callback (callback_baton, pid, exited, signal, exit_status);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000218
219 // If our process exited, then this thread should exit
220 if (exited)
221 break;
222 // If the callback returns true, it means this process should
223 // exit
224 if (callback_return)
225 break;
226 }
227 }
228 }
229 }
230
Caroline Tice926060e2010-10-29 21:48:37 +0000231 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000232 if (log)
233 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
234
235 return NULL;
236}
237
Greg Claytondf6dc882012-01-05 03:57:59 +0000238
239void
240Host::SystemLog (SystemLogType type, const char *format, va_list args)
241{
242 vfprintf (stderr, format, args);
243}
244
Greg Clayton1c4642c2011-11-16 05:37:56 +0000245#endif // #if !defined (__APPLE__)
246
Greg Claytondf6dc882012-01-05 03:57:59 +0000247void
248Host::SystemLog (SystemLogType type, const char *format, ...)
249{
250 va_list args;
251 va_start (args, format);
252 SystemLog (type, format, args);
253 va_end (args);
254}
255
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000256size_t
257Host::GetPageSize()
258{
259 return ::getpagesize();
260}
261
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000262const ArchSpec &
Greg Clayton395fc332011-02-15 21:59:32 +0000263Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000264{
Greg Clayton395fc332011-02-15 21:59:32 +0000265 static bool g_supports_32 = false;
266 static bool g_supports_64 = false;
267 static ArchSpec g_host_arch_32;
268 static ArchSpec g_host_arch_64;
269
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000270#if defined (__APPLE__)
Greg Clayton395fc332011-02-15 21:59:32 +0000271
272 // Apple is different in that it can support both 32 and 64 bit executables
273 // in the same operating system running concurrently. Here we detect the
274 // correct host architectures for both 32 and 64 bit including if 64 bit
275 // executables are supported on the system.
276
277 if (g_supports_32 == false && g_supports_64 == false)
278 {
279 // All apple systems support 32 bit execution.
280 g_supports_32 = true;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000281 uint32_t cputype, cpusubtype;
Greg Clayton395fc332011-02-15 21:59:32 +0000282 uint32_t is_64_bit_capable = false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000283 size_t len = sizeof(cputype);
Greg Clayton395fc332011-02-15 21:59:32 +0000284 ArchSpec host_arch;
285 // These will tell us about the kernel architecture, which even on a 64
286 // bit machine can be 32 bit...
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000287 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
288 {
Greg Clayton395fc332011-02-15 21:59:32 +0000289 len = sizeof (cpusubtype);
290 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
291 cpusubtype = CPU_TYPE_ANY;
292
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000293 len = sizeof (is_64_bit_capable);
294 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
295 {
296 if (is_64_bit_capable)
Greg Clayton395fc332011-02-15 21:59:32 +0000297 g_supports_64 = true;
298 }
299
300 if (is_64_bit_capable)
301 {
Greg Clayton75c703d2011-02-16 04:46:07 +0000302#if defined (__i386__) || defined (__x86_64__)
303 if (cpusubtype == CPU_SUBTYPE_486)
304 cpusubtype = CPU_SUBTYPE_I386_ALL;
305#endif
Greg Clayton395fc332011-02-15 21:59:32 +0000306 if (cputype & CPU_ARCH_ABI64)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000307 {
Greg Clayton395fc332011-02-15 21:59:32 +0000308 // We have a 64 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000309 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
310 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000311 }
312 else
313 {
314 // We have a 32 bit kernel on a 64 bit system
Greg Claytonb3448432011-03-24 21:19:54 +0000315 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000316 cputype |= CPU_ARCH_ABI64;
Greg Claytonb3448432011-03-24 21:19:54 +0000317 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000318 }
319 }
Greg Clayton395fc332011-02-15 21:59:32 +0000320 else
321 {
Greg Claytonb3448432011-03-24 21:19:54 +0000322 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +0000323 g_host_arch_64.Clear();
324 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000325 }
Greg Clayton395fc332011-02-15 21:59:32 +0000326 }
327
328#else // #if defined (__APPLE__)
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000329
Greg Clayton395fc332011-02-15 21:59:32 +0000330 if (g_supports_32 == false && g_supports_64 == false)
331 {
Peter Collingbourneb47c9982011-11-05 01:35:31 +0000332 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton395fc332011-02-15 21:59:32 +0000333
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000334 g_host_arch_32.Clear();
335 g_host_arch_64.Clear();
Greg Clayton395fc332011-02-15 21:59:32 +0000336
Greg Clayton7971a032012-10-11 17:38:58 +0000337 // If the OS is Linux, "unknown" in the vendor slot isn't what we want
338 // for the default triple. It's probably an artifact of config.guess.
339 if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor)
340 triple.setVendorName("");
341
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000342 switch (triple.getArch())
343 {
344 default:
345 g_host_arch_32.SetTriple(triple);
346 g_supports_32 = true;
347 break;
Greg Clayton395fc332011-02-15 21:59:32 +0000348
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000349 case llvm::Triple::x86_64:
Greg Clayton1a450cd2012-09-07 17:49:29 +0000350 g_host_arch_64.SetTriple(triple);
351 g_supports_64 = true;
352 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
353 g_supports_32 = true;
354 break;
355
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000356 case llvm::Triple::sparcv9:
357 case llvm::Triple::ppc64:
Stephen Wilson7f513ba2011-02-24 19:15:09 +0000358 g_host_arch_64.SetTriple(triple);
359 g_supports_64 = true;
360 break;
361 }
Greg Clayton4fefe322011-02-17 02:05:38 +0000362
363 g_supports_32 = g_host_arch_32.IsValid();
364 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000365 }
Greg Clayton395fc332011-02-15 21:59:32 +0000366
367#endif // #else for #if defined (__APPLE__)
368
369 if (arch_kind == eSystemDefaultArchitecture32)
370 return g_host_arch_32;
371 else if (arch_kind == eSystemDefaultArchitecture64)
372 return g_host_arch_64;
373
374 if (g_supports_64)
375 return g_host_arch_64;
376
377 return g_host_arch_32;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000378}
379
380const ConstString &
381Host::GetVendorString()
382{
383 static ConstString g_vendor;
384 if (!g_vendor)
385 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000386 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
387 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
388 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000389 }
390 return g_vendor;
391}
392
393const ConstString &
394Host::GetOSString()
395{
396 static ConstString g_os_string;
397 if (!g_os_string)
398 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000399 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
400 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
401 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000402 }
403 return g_os_string;
404}
405
406const ConstString &
407Host::GetTargetTriple()
408{
409 static ConstString g_host_triple;
410 if (!(g_host_triple))
411 {
Greg Clayton5b0025f2012-05-12 00:01:21 +0000412 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
413 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000414 }
415 return g_host_triple;
416}
417
418lldb::pid_t
419Host::GetCurrentProcessID()
420{
421 return ::getpid();
422}
423
424lldb::tid_t
425Host::GetCurrentThreadID()
426{
427#if defined (__APPLE__)
Greg Claytone93725b2012-09-18 18:19:49 +0000428 // Calling "mach_port_deallocate()" bumps the reference count on the thread
429 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
430 // count.
431 thread_port_t thread_self = mach_thread_self();
432 mach_port_deallocate(mach_task_self(), thread_self);
433 return thread_self;
Johnny Chen4b663292011-08-02 20:52:42 +0000434#elif defined(__FreeBSD__)
435 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000436#else
437 return lldb::tid_t(pthread_self());
438#endif
439}
440
Jim Ingham1831e782012-04-07 00:00:41 +0000441lldb::thread_t
442Host::GetCurrentThread ()
443{
444 return lldb::thread_t(pthread_self());
445}
446
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000447const char *
448Host::GetSignalAsCString (int signo)
449{
450 switch (signo)
451 {
452 case SIGHUP: return "SIGHUP"; // 1 hangup
453 case SIGINT: return "SIGINT"; // 2 interrupt
454 case SIGQUIT: return "SIGQUIT"; // 3 quit
455 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
456 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
457 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton193cc832011-11-04 03:42:38 +0000458#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000459 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000460#endif
461#if !defined(_POSIX_C_SOURCE)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000462 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer06c306c2011-11-04 16:06:40 +0000463#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000464 case SIGFPE: return "SIGFPE"; // 8 floating point exception
465 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
466 case SIGBUS: return "SIGBUS"; // 10 bus error
467 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
468 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
469 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
470 case SIGALRM: return "SIGALRM"; // 14 alarm clock
471 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
472 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
473 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
474 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
475 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
476 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
477 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
478 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
479#if !defined(_POSIX_C_SOURCE)
480 case SIGIO: return "SIGIO"; // 23 input/output possible signal
481#endif
482 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
483 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
484 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
485 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
486#if !defined(_POSIX_C_SOURCE)
487 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
488 case SIGINFO: return "SIGINFO"; // 29 information request
489#endif
490 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
491 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
492 default:
493 break;
494 }
495 return NULL;
496}
497
498void
499Host::WillTerminate ()
500{
501}
502
Johnny Chen4b663292011-08-02 20:52:42 +0000503#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000504void
505Host::ThreadCreated (const char *thread_name)
506{
507}
Greg Claytonb749a262010-12-03 06:02:24 +0000508
Peter Collingbourne5f0559d2011-08-05 00:35:43 +0000509void
Greg Claytonb749a262010-12-03 06:02:24 +0000510Host::Backtrace (Stream &strm, uint32_t max_frames)
511{
Greg Clayton52fd9842011-02-02 02:24:04 +0000512 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytonb749a262010-12-03 06:02:24 +0000513}
514
Greg Clayton638351a2010-12-04 00:10:17 +0000515size_t
516Host::GetEnvironment (StringList &env)
517{
Greg Clayton52fd9842011-02-02 02:24:04 +0000518 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton638351a2010-12-04 00:10:17 +0000519 return 0;
520}
521
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000522#endif
523
524struct HostThreadCreateInfo
525{
526 std::string thread_name;
527 thread_func_t thread_fptr;
528 thread_arg_t thread_arg;
529
530 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
531 thread_name (name ? name : ""),
532 thread_fptr (fptr),
533 thread_arg (arg)
534 {
535 }
536};
537
538static thread_result_t
539ThreadCreateTrampoline (thread_arg_t arg)
540{
541 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
542 Host::ThreadCreated (info->thread_name.c_str());
543 thread_func_t thread_fptr = info->thread_fptr;
544 thread_arg_t thread_arg = info->thread_arg;
545
Greg Claytone005f2c2010-11-06 01:53:30 +0000546 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000547 if (log)
548 log->Printf("thread created");
549
550 delete info;
551 return thread_fptr (thread_arg);
552}
553
554lldb::thread_t
555Host::ThreadCreate
556(
557 const char *thread_name,
558 thread_func_t thread_fptr,
559 thread_arg_t thread_arg,
560 Error *error
561)
562{
563 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
564
565 // Host::ThreadCreateTrampoline will delete this pointer for us.
566 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
567
568 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
569 if (err == 0)
570 {
571 if (error)
572 error->Clear();
573 return thread;
574 }
575
576 if (error)
577 error->SetError (err, eErrorTypePOSIX);
578
579 return LLDB_INVALID_HOST_THREAD;
580}
581
582bool
583Host::ThreadCancel (lldb::thread_t thread, Error *error)
584{
585 int err = ::pthread_cancel (thread);
586 if (error)
587 error->SetError(err, eErrorTypePOSIX);
588 return err == 0;
589}
590
591bool
592Host::ThreadDetach (lldb::thread_t thread, Error *error)
593{
594 int err = ::pthread_detach (thread);
595 if (error)
596 error->SetError(err, eErrorTypePOSIX);
597 return err == 0;
598}
599
600bool
601Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
602{
603 int err = ::pthread_join (thread, thread_result_ptr);
604 if (error)
605 error->SetError(err, eErrorTypePOSIX);
606 return err == 0;
607}
608
Jim Ingham35dd4962012-05-04 19:24:49 +0000609// rdar://problem/8153284
610// Fixed a crasher where during shutdown, loggings attempted to access the
611// thread name but the static map instance had already been destructed.
612// So we are using a ThreadSafeSTLMap POINTER, initializing it with a
613// pthread_once action. That map will get leaked.
614//
615// Another approach is to introduce a static guard object which monitors its
616// own destruction and raises a flag, but this incurs more overhead.
617
618static pthread_once_t g_thread_map_once = PTHREAD_ONCE_INIT;
619static ThreadSafeSTLMap<uint64_t, std::string> *g_thread_names_map_ptr;
620
621static void
622InitThreadNamesMap()
623{
624 g_thread_names_map_ptr = new ThreadSafeSTLMap<uint64_t, std::string>();
625}
626
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000627//------------------------------------------------------------------
628// Control access to a static file thread name map using a single
629// static function to avoid a static constructor.
630//------------------------------------------------------------------
631static const char *
632ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name)
633{
Jim Ingham35dd4962012-05-04 19:24:49 +0000634 int success = ::pthread_once (&g_thread_map_once, InitThreadNamesMap);
635 if (success != 0)
636 return NULL;
637
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000638 uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid;
639
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000640 if (get)
641 {
642 // See if the thread name exists in our thread name pool
Jim Ingham35dd4962012-05-04 19:24:49 +0000643 std::string value;
644 bool found_it = g_thread_names_map_ptr->GetValueForKey (pid_tid, value);
645 if (found_it)
646 return value.c_str();
647 else
648 return NULL;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000649 }
Jim Ingham35dd4962012-05-04 19:24:49 +0000650 else if (name)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000651 {
652 // Set the thread name
Jim Ingham35dd4962012-05-04 19:24:49 +0000653 g_thread_names_map_ptr->SetValueForKey (pid_tid, std::string(name));
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000654 }
655 return NULL;
656}
657
658const char *
659Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
660{
661 const char *name = ThreadNameAccessor (true, pid, tid, NULL);
662 if (name == NULL)
663 {
664#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
665 // We currently can only get the name of a thread in the current process.
666 if (pid == Host::GetCurrentProcessID())
667 {
668 char pthread_name[1024];
669 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
670 {
671 if (pthread_name[0])
672 {
673 // Set the thread in our string pool
674 ThreadNameAccessor (false, pid, tid, pthread_name);
675 // Get our copy of the thread name string
676 name = ThreadNameAccessor (true, pid, tid, NULL);
677 }
678 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000679
680 if (name == NULL)
681 {
682 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
683 if (current_queue != NULL)
684 name = dispatch_queue_get_label (current_queue);
685 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000686 }
687#endif
688 }
689 return name;
690}
691
692void
693Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
694{
695 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
696 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
697 if (pid == LLDB_INVALID_PROCESS_ID)
698 pid = curr_pid;
699
700 if (tid == LLDB_INVALID_THREAD_ID)
701 tid = curr_tid;
702
703#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
704 // Set the pthread name if possible
705 if (pid == curr_pid && tid == curr_tid)
706 {
707 ::pthread_setname_np (name);
708 }
709#endif
710 ThreadNameAccessor (false, pid, tid, name);
711}
712
713FileSpec
714Host::GetProgramFileSpec ()
715{
716 static FileSpec g_program_filespec;
717 if (!g_program_filespec)
718 {
719#if defined (__APPLE__)
720 char program_fullpath[PATH_MAX];
721 // If DST is NULL, then return the number of bytes needed.
722 uint32_t len = sizeof(program_fullpath);
723 int err = _NSGetExecutablePath (program_fullpath, &len);
724 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000725 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000726 else if (err == -1)
727 {
728 char *large_program_fullpath = (char *)::malloc (len + 1);
729
730 err = _NSGetExecutablePath (large_program_fullpath, &len);
731 if (err == 0)
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000732 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000733
734 ::free (large_program_fullpath);
735 }
736#elif defined (__linux__)
737 char exe_path[PATH_MAX];
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000738 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
739 if (len > 0) {
740 exe_path[len] = 0;
Greg Clayton20fbf8d2011-01-13 01:23:43 +0000741 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsonf302a9e2011-01-12 04:21:21 +0000742 }
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000743#elif defined (__FreeBSD__)
744 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
745 size_t exe_path_size;
746 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
747 {
Greg Clayton366795e2011-01-13 01:27:55 +0000748 char *exe_path = new char[exe_path_size];
749 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
750 g_program_filespec.SetFile(exe_path, false);
751 delete[] exe_path;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000752 }
753#endif
754 }
755 return g_program_filespec;
756}
757
758FileSpec
759Host::GetModuleFileSpecForHostAddress (const void *host_addr)
760{
761 FileSpec module_filespec;
762 Dl_info info;
763 if (::dladdr (host_addr, &info))
764 {
765 if (info.dli_fname)
Greg Clayton537a7a82010-10-20 20:54:39 +0000766 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000767 }
768 return module_filespec;
769}
770
771#if !defined (__APPLE__) // see Host.mm
Greg Clayton9ce95382012-02-13 23:10:39 +0000772
773bool
774Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
775{
776 bundle.Clear();
777 return false;
778}
779
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000780bool
Greg Clayton24b48ff2010-10-17 22:03:32 +0000781Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000782{
Greg Clayton24b48ff2010-10-17 22:03:32 +0000783 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +0000784}
785#endif
786
Greg Clayton14ef59f2011-02-08 00:35:34 +0000787// Opaque info that tracks a dynamic library that was loaded
788struct DynamicLibraryInfo
Greg Clayton52fd9842011-02-02 02:24:04 +0000789{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000790 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
791 file_spec (fs),
792 open_options (o),
793 handle (h)
794 {
795 }
796
797 const FileSpec file_spec;
798 uint32_t open_options;
799 void * handle;
800};
801
802void *
803Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
804{
Greg Clayton52fd9842011-02-02 02:24:04 +0000805 char path[PATH_MAX];
806 if (file_spec.GetPath(path, sizeof(path)))
807 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000808 int mode = 0;
809
810 if (options & eDynamicLibraryOpenOptionLazy)
811 mode |= RTLD_LAZY;
Greg Claytonbf467b02011-02-08 05:24:57 +0000812 else
813 mode |= RTLD_NOW;
814
Greg Clayton14ef59f2011-02-08 00:35:34 +0000815
816 if (options & eDynamicLibraryOpenOptionLocal)
817 mode |= RTLD_LOCAL;
818 else
819 mode |= RTLD_GLOBAL;
820
821#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
822 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
823 mode |= RTLD_FIRST;
Greg Clayton0f577c22011-02-07 17:43:47 +0000824#endif
Greg Clayton14ef59f2011-02-08 00:35:34 +0000825
826 void * opaque = ::dlopen (path, mode);
827
828 if (opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000829 {
830 error.Clear();
Greg Clayton14ef59f2011-02-08 00:35:34 +0000831 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton52fd9842011-02-02 02:24:04 +0000832 }
833 else
834 {
835 error.SetErrorString(::dlerror());
836 }
837 }
838 else
839 {
840 error.SetErrorString("failed to extract path");
841 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000842 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000843}
844
845Error
Greg Clayton14ef59f2011-02-08 00:35:34 +0000846Host::DynamicLibraryClose (void *opaque)
Greg Clayton52fd9842011-02-02 02:24:04 +0000847{
848 Error error;
Greg Clayton14ef59f2011-02-08 00:35:34 +0000849 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000850 {
851 error.SetErrorString ("invalid dynamic library handle");
852 }
Greg Clayton14ef59f2011-02-08 00:35:34 +0000853 else
Greg Clayton52fd9842011-02-02 02:24:04 +0000854 {
Greg Clayton14ef59f2011-02-08 00:35:34 +0000855 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
856 if (::dlclose (dylib_info->handle) != 0)
857 {
858 error.SetErrorString(::dlerror());
859 }
860
861 dylib_info->open_options = 0;
862 dylib_info->handle = 0;
863 delete dylib_info;
Greg Clayton52fd9842011-02-02 02:24:04 +0000864 }
865 return error;
866}
867
868void *
Greg Clayton14ef59f2011-02-08 00:35:34 +0000869Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton52fd9842011-02-02 02:24:04 +0000870{
Greg Clayton14ef59f2011-02-08 00:35:34 +0000871 if (opaque == NULL)
Greg Clayton52fd9842011-02-02 02:24:04 +0000872 {
873 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton52fd9842011-02-02 02:24:04 +0000874 }
Greg Clayton52fd9842011-02-02 02:24:04 +0000875 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000876 {
877 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
878
879 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
880 if (symbol_addr)
881 {
882#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
883 // This host doesn't support limiting searches to this shared library
884 // so we need to verify that the match came from this shared library
885 // if it was requested in the Host::DynamicLibraryOpen() function.
Greg Claytonbf467b02011-02-08 05:24:57 +0000886 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton14ef59f2011-02-08 00:35:34 +0000887 {
888 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
889 if (match_dylib_spec != dylib_info->file_spec)
890 {
891 char dylib_path[PATH_MAX];
892 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
893 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
894 else
895 error.SetErrorString ("symbol not found");
896 return NULL;
897 }
898 }
899#endif
900 error.Clear();
901 return symbol_addr;
902 }
903 else
904 {
905 error.SetErrorString(::dlerror());
906 }
907 }
908 return NULL;
Greg Clayton52fd9842011-02-02 02:24:04 +0000909}
Greg Clayton24b48ff2010-10-17 22:03:32 +0000910
911bool
912Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
913{
Greg Clayton5d187e52011-01-08 20:28:42 +0000914 // To get paths related to LLDB we get the path to the executable that
Greg Clayton24b48ff2010-10-17 22:03:32 +0000915 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
916 // on linux this is assumed to be the "lldb" main executable. If LLDB on
917 // linux is actually in a shared library (lldb.so??) then this function will
918 // need to be modified to "do the right thing".
919
920 switch (path_type)
921 {
922 case ePathTypeLLDBShlibDir:
923 {
924 static ConstString g_lldb_so_dir;
925 if (!g_lldb_so_dir)
926 {
927 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
928 g_lldb_so_dir = lldb_file_spec.GetDirectory();
929 }
930 file_spec.GetDirectory() = g_lldb_so_dir;
931 return file_spec.GetDirectory();
932 }
933 break;
934
935 case ePathTypeSupportExecutableDir:
936 {
937 static ConstString g_lldb_support_exe_dir;
938 if (!g_lldb_support_exe_dir)
939 {
940 FileSpec lldb_file_spec;
941 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
942 {
943 char raw_path[PATH_MAX];
944 char resolved_path[PATH_MAX];
945 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
946
947#if defined (__APPLE__)
948 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
949 if (framework_pos)
950 {
951 framework_pos += strlen("LLDB.framework");
Greg Clayton3e4238d2011-11-04 03:34:56 +0000952#if !defined (__arm__)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000953 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Clayton3e4238d2011-11-04 03:34:56 +0000954#endif
Greg Clayton24b48ff2010-10-17 22:03:32 +0000955 }
956#endif
957 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
958 g_lldb_support_exe_dir.SetCString(resolved_path);
959 }
960 }
961 file_spec.GetDirectory() = g_lldb_support_exe_dir;
962 return file_spec.GetDirectory();
963 }
964 break;
965
966 case ePathTypeHeaderDir:
967 {
968 static ConstString g_lldb_headers_dir;
969 if (!g_lldb_headers_dir)
970 {
971#if defined (__APPLE__)
972 FileSpec lldb_file_spec;
973 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
974 {
975 char raw_path[PATH_MAX];
976 char resolved_path[PATH_MAX];
977 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
978
979 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
980 if (framework_pos)
981 {
982 framework_pos += strlen("LLDB.framework");
983 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
984 }
985 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
986 g_lldb_headers_dir.SetCString(resolved_path);
987 }
988#else
Greg Clayton52fd9842011-02-02 02:24:04 +0000989 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Clayton24b48ff2010-10-17 22:03:32 +0000990 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
991#endif
992 }
993 file_spec.GetDirectory() = g_lldb_headers_dir;
994 return file_spec.GetDirectory();
995 }
996 break;
997
998 case ePathTypePythonDir:
999 {
Greg Clayton52fd9842011-02-02 02:24:04 +00001000 // TODO: Anyone know how we can determine this for linux? Other systems?
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001001 // For linux and FreeBSD we are currently assuming the
1002 // location of the lldb binary that contains this function is
1003 // the directory that will contain a python directory which
1004 // has our lldb module. This is how files get placed when
1005 // compiling with Makefiles.
Greg Clayton24b48ff2010-10-17 22:03:32 +00001006
1007 static ConstString g_lldb_python_dir;
1008 if (!g_lldb_python_dir)
1009 {
1010 FileSpec lldb_file_spec;
1011 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1012 {
1013 char raw_path[PATH_MAX];
1014 char resolved_path[PATH_MAX];
1015 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1016
1017#if defined (__APPLE__)
1018 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1019 if (framework_pos)
1020 {
1021 framework_pos += strlen("LLDB.framework");
1022 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1023 }
Filipe Cabecinhasdf802722012-07-30 16:46:32 +00001024#else
Filipe Cabecinhas67aa5b62012-07-30 18:56:10 +00001025 // We may get our string truncated. Should we protect
1026 // this with an assert?
1027 ::strncat(raw_path, "/python", sizeof(raw_path) - strlen(raw_path) - 1);
Greg Clayton24b48ff2010-10-17 22:03:32 +00001028#endif
1029 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1030 g_lldb_python_dir.SetCString(resolved_path);
1031 }
1032 }
1033 file_spec.GetDirectory() = g_lldb_python_dir;
1034 return file_spec.GetDirectory();
1035 }
1036 break;
1037
Greg Clayton52fd9842011-02-02 02:24:04 +00001038 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1039 {
1040#if defined (__APPLE__)
1041 static ConstString g_lldb_system_plugin_dir;
Greg Clayton58e26e02011-03-24 04:28:38 +00001042 static bool g_lldb_system_plugin_dir_located = false;
1043 if (!g_lldb_system_plugin_dir_located)
Greg Clayton52fd9842011-02-02 02:24:04 +00001044 {
Greg Clayton58e26e02011-03-24 04:28:38 +00001045 g_lldb_system_plugin_dir_located = true;
Greg Clayton52fd9842011-02-02 02:24:04 +00001046 FileSpec lldb_file_spec;
1047 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1048 {
1049 char raw_path[PATH_MAX];
1050 char resolved_path[PATH_MAX];
1051 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1052
1053 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1054 if (framework_pos)
1055 {
1056 framework_pos += strlen("LLDB.framework");
1057 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton58e26e02011-03-24 04:28:38 +00001058 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1059 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton52fd9842011-02-02 02:24:04 +00001060 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001061 return false;
Greg Clayton52fd9842011-02-02 02:24:04 +00001062 }
1063 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001064
1065 if (g_lldb_system_plugin_dir)
1066 {
1067 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1068 return true;
1069 }
Greg Clayton52fd9842011-02-02 02:24:04 +00001070#endif
1071 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1072 return false;
1073 }
1074 break;
1075
1076 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1077 {
1078#if defined (__APPLE__)
1079 static ConstString g_lldb_user_plugin_dir;
1080 if (!g_lldb_user_plugin_dir)
1081 {
1082 char user_plugin_path[PATH_MAX];
1083 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1084 user_plugin_path,
1085 sizeof(user_plugin_path)))
1086 {
1087 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1088 }
1089 }
1090 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1091 return file_spec.GetDirectory();
1092#endif
1093 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1094 return false;
1095 }
Greg Clayton24b48ff2010-10-17 22:03:32 +00001096 }
1097
1098 return false;
1099}
1100
Greg Clayton58e26e02011-03-24 04:28:38 +00001101
1102bool
1103Host::GetHostname (std::string &s)
1104{
1105 char hostname[PATH_MAX];
1106 hostname[sizeof(hostname) - 1] = '\0';
1107 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1108 {
1109 struct hostent* h = ::gethostbyname (hostname);
1110 if (h)
1111 s.assign (h->h_name);
1112 else
1113 s.assign (hostname);
1114 return true;
1115 }
1116 return false;
1117}
1118
Greg Clayton24bc5d92011-03-30 18:16:51 +00001119const char *
1120Host::GetUserName (uint32_t uid, std::string &user_name)
1121{
1122 struct passwd user_info;
1123 struct passwd *user_info_ptr = &user_info;
1124 char user_buffer[PATH_MAX];
1125 size_t user_buffer_size = sizeof(user_buffer);
1126 if (::getpwuid_r (uid,
1127 &user_info,
1128 user_buffer,
1129 user_buffer_size,
1130 &user_info_ptr) == 0)
1131 {
1132 if (user_info_ptr)
1133 {
1134 user_name.assign (user_info_ptr->pw_name);
1135 return user_name.c_str();
1136 }
1137 }
1138 user_name.clear();
1139 return NULL;
1140}
1141
1142const char *
1143Host::GetGroupName (uint32_t gid, std::string &group_name)
1144{
1145 char group_buffer[PATH_MAX];
1146 size_t group_buffer_size = sizeof(group_buffer);
1147 struct group group_info;
1148 struct group *group_info_ptr = &group_info;
1149 // Try the threadsafe version first
1150 if (::getgrgid_r (gid,
1151 &group_info,
1152 group_buffer,
1153 group_buffer_size,
1154 &group_info_ptr) == 0)
1155 {
1156 if (group_info_ptr)
1157 {
1158 group_name.assign (group_info_ptr->gr_name);
1159 return group_name.c_str();
1160 }
1161 }
1162 else
1163 {
1164 // The threadsafe version isn't currently working
1165 // for me on darwin, but the non-threadsafe version
1166 // is, so I am calling it below.
1167 group_info_ptr = ::getgrgid (gid);
1168 if (group_info_ptr)
1169 {
1170 group_name.assign (group_info_ptr->gr_name);
1171 return group_name.c_str();
1172 }
1173 }
1174 group_name.clear();
1175 return NULL;
1176}
1177
Johnny Chen4b663292011-08-02 20:52:42 +00001178#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
Greg Clayton58e26e02011-03-24 04:28:38 +00001179bool
1180Host::GetOSBuildString (std::string &s)
1181{
1182 s.clear();
1183 return false;
1184}
1185
1186bool
1187Host::GetOSKernelDescription (std::string &s)
1188{
1189 s.clear();
1190 return false;
1191}
Johnny Chen4b663292011-08-02 20:52:42 +00001192#endif
Greg Clayton58e26e02011-03-24 04:28:38 +00001193
Han Ming Ongd1040dd2012-02-25 01:07:38 +00001194uint32_t
1195Host::GetUserID ()
1196{
1197 return getuid();
1198}
1199
1200uint32_t
1201Host::GetGroupID ()
1202{
1203 return getgid();
1204}
1205
1206uint32_t
1207Host::GetEffectiveUserID ()
1208{
1209 return geteuid();
1210}
1211
1212uint32_t
1213Host::GetEffectiveGroupID ()
1214{
1215 return getegid();
1216}
1217
1218#if !defined (__APPLE__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001219uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001220Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001221{
1222 process_infos.Clear();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001223 return process_infos.GetSize();
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001224}
Johnny Chen4b663292011-08-02 20:52:42 +00001225#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001226
Johnny Chen4b663292011-08-02 20:52:42 +00001227#if !defined (__APPLE__) && !defined (__FreeBSD__)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001228bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001229Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001230{
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001231 process_info.Clear();
1232 return false;
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001233}
Johnny Chen4b663292011-08-02 20:52:42 +00001234#endif
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001235
Sean Callananf35a96c2011-10-27 21:22:25 +00001236lldb::TargetSP
1237Host::GetDummyTarget (lldb_private::Debugger &debugger)
1238{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001239 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001240
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001241 // FIXME: Maybe the dummy target should be per-Debugger
1242 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1243 {
1244 ArchSpec arch(Target::GetDefaultArchitecture());
1245 if (!arch.IsValid())
1246 arch = Host::GetArchitecture ();
1247 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001248 NULL,
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001249 arch.GetTriple().getTriple().c_str(),
1250 false,
1251 NULL,
1252 g_dummy_target_sp);
1253 }
Filipe Cabecinhasf42d3f62012-05-17 15:48:02 +00001254
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +00001255 return g_dummy_target_sp;
Sean Callananf35a96c2011-10-27 21:22:25 +00001256}
1257
Greg Clayton97471182012-04-14 01:42:46 +00001258struct ShellInfo
1259{
1260 ShellInfo () :
1261 process_reaped (false),
1262 can_delete (false),
1263 pid (LLDB_INVALID_PROCESS_ID),
1264 signo(-1),
1265 status(-1)
1266 {
1267 }
1268
1269 lldb_private::Predicate<bool> process_reaped;
1270 lldb_private::Predicate<bool> can_delete;
1271 lldb::pid_t pid;
1272 int signo;
1273 int status;
1274};
1275
1276static bool
1277MonitorShellCommand (void *callback_baton,
1278 lldb::pid_t pid,
1279 bool exited, // True if the process did exit
1280 int signo, // Zero for no signal
1281 int status) // Exit value of process if signal is zero
1282{
1283 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1284 shell_info->pid = pid;
1285 shell_info->signo = signo;
1286 shell_info->status = status;
1287 // Let the thread running Host::RunShellCommand() know that the process
1288 // exited and that ShellInfo has been filled in by broadcasting to it
1289 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1290 // Now wait for a handshake back from that thread running Host::RunShellCommand
1291 // so we know that we can delete shell_info_ptr
1292 shell_info->can_delete.WaitForValueEqualTo(true);
1293 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1294 usleep(1000);
1295 // Now delete the shell info that was passed into this function
1296 delete shell_info;
1297 return true;
1298}
1299
1300Error
1301Host::RunShellCommand (const char *command,
1302 const char *working_dir,
1303 int *status_ptr,
1304 int *signo_ptr,
1305 std::string *command_output_ptr,
Greg Claytonb924eb62012-09-27 03:13:55 +00001306 uint32_t timeout_sec,
1307 const char *shell)
Greg Clayton97471182012-04-14 01:42:46 +00001308{
1309 Error error;
1310 ProcessLaunchInfo launch_info;
Greg Claytonb924eb62012-09-27 03:13:55 +00001311 if (shell && shell[0])
1312 {
1313 // Run the command in a shell
1314 launch_info.SetShell(shell);
1315 launch_info.GetArguments().AppendArgument(command);
1316 const bool localhost = true;
1317 const bool will_debug = false;
1318 const bool first_arg_is_full_shell_command = true;
1319 launch_info.ConvertArgumentsForLaunchingInShell (error,
1320 localhost,
1321 will_debug,
1322 first_arg_is_full_shell_command);
1323 }
1324 else
1325 {
1326 // No shell, just run it
1327 Args args (command);
1328 const bool first_arg_is_executable = true;
Greg Clayton0c8446c2012-10-17 22:57:12 +00001329 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonb924eb62012-09-27 03:13:55 +00001330 }
Greg Clayton97471182012-04-14 01:42:46 +00001331
1332 if (working_dir)
1333 launch_info.SetWorkingDirectory(working_dir);
1334 char output_file_path_buffer[L_tmpnam];
1335 const char *output_file_path = NULL;
1336 if (command_output_ptr)
1337 {
1338 // Create a temporary file to get the stdout/stderr and redirect the
1339 // output of the command into this file. We will later read this file
1340 // if all goes well and fill the data into "command_output_ptr"
1341 output_file_path = ::tmpnam(output_file_path_buffer);
1342 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1343 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonb924eb62012-09-27 03:13:55 +00001344 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Clayton97471182012-04-14 01:42:46 +00001345 }
1346 else
1347 {
1348 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1349 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1350 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1351 }
1352
1353 // The process monitor callback will delete the 'shell_info_ptr' below...
1354 std::auto_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1355
1356 const bool monitor_signals = false;
1357 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1358
1359 error = LaunchProcess (launch_info);
1360 const lldb::pid_t pid = launch_info.GetProcessID();
1361 if (pid != LLDB_INVALID_PROCESS_ID)
1362 {
1363 // The process successfully launched, so we can defer ownership of
1364 // "shell_info" to the MonitorShellCommand callback function that will
1365 // get called when the process dies. We release the std::auto_ptr as it
1366 // doesn't need to delete the ShellInfo anymore.
1367 ShellInfo *shell_info = shell_info_ap.release();
1368 TimeValue timeout_time(TimeValue::Now());
1369 timeout_time.OffsetWithSeconds(timeout_sec);
1370 bool timed_out = false;
1371 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1372 if (timed_out)
1373 {
1374 error.SetErrorString("timed out waiting for shell command to complete");
1375
1376 // Kill the process since it didn't complete withint the timeout specified
1377 ::kill (pid, SIGKILL);
1378 // Wait for the monitor callback to get the message
1379 timeout_time = TimeValue::Now();
1380 timeout_time.OffsetWithSeconds(1);
1381 timed_out = false;
1382 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1383 }
1384 else
1385 {
1386 if (status_ptr)
1387 *status_ptr = shell_info->status;
1388
1389 if (signo_ptr)
1390 *signo_ptr = shell_info->signo;
1391
1392 if (command_output_ptr)
1393 {
1394 command_output_ptr->clear();
1395 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1396 uint64_t file_size = file_spec.GetByteSize();
1397 if (file_size > 0)
1398 {
1399 if (file_size > command_output_ptr->max_size())
1400 {
1401 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1402 }
1403 else
1404 {
1405 command_output_ptr->resize(file_size);
1406 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1407 }
1408 }
1409 }
1410 }
1411 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1412 }
1413 else
1414 {
1415 error.SetErrorString("failed to get process ID");
1416 }
1417
1418 if (output_file_path)
1419 ::unlink (output_file_path);
1420 // Handshake with the monitor thread, or just let it know in advance that
1421 // it can delete "shell_info" in case we timed out and were not able to kill
1422 // the process...
1423 return error;
1424}
1425
1426
1427
Johnny Chen4b663292011-08-02 20:52:42 +00001428#if !defined (__APPLE__)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001429bool
Greg Claytonb73620c2010-12-18 01:54:34 +00001430Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001431{
1432 return false;
1433}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001434
Greg Claytone98ac252010-11-10 04:57:04 +00001435void
1436Host::SetCrashDescriptionWithFormat (const char *format, ...)
1437{
1438}
1439
1440void
1441Host::SetCrashDescription (const char *description)
1442{
1443}
Greg Clayton24b48ff2010-10-17 22:03:32 +00001444
1445lldb::pid_t
1446LaunchApplication (const FileSpec &app_file_spec)
1447{
1448 return LLDB_INVALID_PROCESS_ID;
1449}
1450
Greg Clayton8f3b21d2010-09-07 20:11:56 +00001451#endif