blob: 14fcce3804c1e79f8241c4dfea3c33ca6b91f54f [file] [log] [blame]
Greg Clayton2bddd342010-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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytone3e3fee2013-02-17 20:46:30 +000012// C includes
13#include <dlfcn.h>
14#include <errno.h>
15#include <grp.h>
16#include <limits.h>
17#include <netdb.h>
18#include <pwd.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000019#include <sys/types.h>
Ed Mastef2b01622013-06-28 12:35:08 +000020#include <sys/sysctl.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000021#include <unistd.h>
22
23#if defined (__APPLE__)
24
25#include <dispatch/dispatch.h>
26#include <libproc.h>
27#include <mach-o/dyld.h>
28#include <mach/mach_port.h>
29
Ed Maste8b006c62013-06-25 18:58:11 +000030#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000031
Ed Maste8b006c62013-06-25 18:58:11 +000032#if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000033#include <sys/wait.h>
Ed Maste8b006c62013-06-25 18:58:11 +000034#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +000035
Ed Maste8b006c62013-06-25 18:58:11 +000036#if defined (__FreeBSD__)
Greg Claytone3e3fee2013-02-17 20:46:30 +000037#include <pthread_np.h>
Greg Claytone3e3fee2013-02-17 20:46:30 +000038#endif
39
Greg Clayton2bddd342010-09-07 20:11:56 +000040#include "lldb/Host/Host.h"
41#include "lldb/Core/ArchSpec.h"
42#include "lldb/Core/ConstString.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000043#include "lldb/Core/Debugger.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000044#include "lldb/Core/Error.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000045#include "lldb/Core/Log.h"
46#include "lldb/Core/StreamString.h"
Jim Inghamc075ecd2012-05-04 19:24:49 +000047#include "lldb/Core/ThreadSafeSTLMap.h"
Greg Clayton45319462011-02-08 00:35:34 +000048#include "lldb/Host/Config.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000049#include "lldb/Host/Endian.h"
Greg Claytone996fd32011-03-08 22:40:15 +000050#include "lldb/Host/FileSpec.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000051#include "lldb/Host/Mutex.h"
Greg Claytone996fd32011-03-08 22:40:15 +000052#include "lldb/Target/Process.h"
Sean Callananc0a6e062011-10-27 21:22:25 +000053#include "lldb/Target/TargetList.h"
Greg Clayton2bddd342010-09-07 20:11:56 +000054
Stephen Wilsonbd588712011-02-24 19:15:09 +000055#include "llvm/Support/Host.h"
Greg Claytone996fd32011-03-08 22:40:15 +000056#include "llvm/Support/MachO.h"
Daniel Malea53430eb2013-01-04 23:35:13 +000057#include "llvm/ADT/Twine.h"
Stephen Wilsonbd588712011-02-24 19:15:09 +000058
Greg Clayton32e0a752011-03-30 18:16:51 +000059
Greg Clayton2bddd342010-09-07 20:11:56 +000060
Greg Clayton45319462011-02-08 00:35:34 +000061
Greg Clayton2bddd342010-09-07 20:11:56 +000062
63using namespace lldb;
64using namespace lldb_private;
65
Greg Claytone4e45922011-11-16 05:37:56 +000066
Greg Clayton1c4cd072011-11-17 19:41:57 +000067#if !defined (__APPLE__)
Greg Clayton2bddd342010-09-07 20:11:56 +000068struct MonitorInfo
69{
70 lldb::pid_t pid; // The process ID to monitor
71 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
72 void *callback_baton; // The callback baton for the callback function
73 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
74};
75
76static void *
77MonitorChildProcessThreadFunction (void *arg);
78
79lldb::thread_t
80Host::StartMonitoringChildProcess
81(
82 Host::MonitorChildProcessCallback callback,
83 void *callback_baton,
84 lldb::pid_t pid,
85 bool monitor_signals
86)
87{
88 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
Greg Claytone4e45922011-11-16 05:37:56 +000089 MonitorInfo * info_ptr = new MonitorInfo();
Greg Clayton2bddd342010-09-07 20:11:56 +000090
Greg Claytone4e45922011-11-16 05:37:56 +000091 info_ptr->pid = pid;
92 info_ptr->callback = callback;
93 info_ptr->callback_baton = callback_baton;
94 info_ptr->monitor_signals = monitor_signals;
95
96 char thread_name[256];
Daniel Malead01b2952012-11-29 21:49:15 +000097 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
Greg Claytone4e45922011-11-16 05:37:56 +000098 thread = ThreadCreate (thread_name,
99 MonitorChildProcessThreadFunction,
100 info_ptr,
101 NULL);
102
Greg Clayton2bddd342010-09-07 20:11:56 +0000103 return thread;
104}
105
106//------------------------------------------------------------------
107// Scoped class that will disable thread canceling when it is
108// constructed, and exception safely restore the previous value it
109// when it goes out of scope.
110//------------------------------------------------------------------
111class ScopedPThreadCancelDisabler
112{
113public:
114 ScopedPThreadCancelDisabler()
115 {
116 // Disable the ability for this thread to be cancelled
117 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
118 if (err != 0)
119 m_old_state = -1;
120
121 }
122
123 ~ScopedPThreadCancelDisabler()
124 {
125 // Restore the ability for this thread to be cancelled to what it
126 // previously was.
127 if (m_old_state != -1)
128 ::pthread_setcancelstate (m_old_state, 0);
129 }
130private:
131 int m_old_state; // Save the old cancelability state.
132};
133
134static void *
135MonitorChildProcessThreadFunction (void *arg)
136{
Greg Clayton5160ce52013-03-27 23:08:40 +0000137 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton2bddd342010-09-07 20:11:56 +0000138 const char *function = __FUNCTION__;
139 if (log)
140 log->Printf ("%s (arg = %p) thread starting...", function, arg);
141
142 MonitorInfo *info = (MonitorInfo *)arg;
143
144 const Host::MonitorChildProcessCallback callback = info->callback;
145 void * const callback_baton = info->callback_baton;
146 const lldb::pid_t pid = info->pid;
147 const bool monitor_signals = info->monitor_signals;
148
149 delete info;
150
151 int status = -1;
Sylvestre Ledru59405832013-07-01 08:21:36 +0000152#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000153 #define __WALL 0
154#endif
Matt Kopec650648f2013-01-08 16:30:18 +0000155 const int options = __WALL;
156
Greg Clayton2bddd342010-09-07 20:11:56 +0000157 while (1)
158 {
Caroline Tice20ad3c42010-10-29 21:48:37 +0000159 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000160 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000161 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
Greg Clayton2bddd342010-09-07 20:11:56 +0000162
163 // Wait for all child processes
164 ::pthread_testcancel ();
Matt Kopec650648f2013-01-08 16:30:18 +0000165 // Get signals from all children with same process group of pid
166 const lldb::pid_t wait_pid = ::waitpid (-1*pid, &status, options);
Greg Clayton2bddd342010-09-07 20:11:56 +0000167 ::pthread_testcancel ();
168
169 if (wait_pid == -1)
170 {
171 if (errno == EINTR)
172 continue;
173 else
Andrew Kaylor93132f52013-05-28 23:04:25 +0000174 {
175 if (log)
176 log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno));
Greg Clayton2bddd342010-09-07 20:11:56 +0000177 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000178 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000179 }
Matt Kopec650648f2013-01-08 16:30:18 +0000180 else if (wait_pid > 0)
Greg Clayton2bddd342010-09-07 20:11:56 +0000181 {
182 bool exited = false;
183 int signal = 0;
184 int exit_status = 0;
185 const char *status_cstr = NULL;
186 if (WIFSTOPPED(status))
187 {
188 signal = WSTOPSIG(status);
189 status_cstr = "STOPPED";
190 }
191 else if (WIFEXITED(status))
192 {
193 exit_status = WEXITSTATUS(status);
194 status_cstr = "EXITED";
Andrew Kaylor93132f52013-05-28 23:04:25 +0000195 exited = true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000196 }
197 else if (WIFSIGNALED(status))
198 {
199 signal = WTERMSIG(status);
200 status_cstr = "SIGNALED";
Matt Kopec650648f2013-01-08 16:30:18 +0000201 if (wait_pid == pid) {
202 exited = true;
203 exit_status = -1;
204 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000205 }
206 else
207 {
Johnny Chen44805302011-07-19 19:48:13 +0000208 status_cstr = "(\?\?\?)";
Greg Clayton2bddd342010-09-07 20:11:56 +0000209 }
210
211 // Scope for pthread_cancel_disabler
212 {
213 ScopedPThreadCancelDisabler pthread_cancel_disabler;
214
Caroline Tice20ad3c42010-10-29 21:48:37 +0000215 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000216 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000217 log->Printf ("%s ::waitpid (pid = %" PRIu64 ", &status, options = %i) => pid = %" PRIu64 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
Greg Clayton2bddd342010-09-07 20:11:56 +0000218 function,
219 wait_pid,
220 options,
Greg Clayton2bddd342010-09-07 20:11:56 +0000221 pid,
222 status,
223 status_cstr,
224 signal,
225 exit_status);
226
227 if (exited || (signal != 0 && monitor_signals))
228 {
Greg Claytone4e45922011-11-16 05:37:56 +0000229 bool callback_return = false;
230 if (callback)
Matt Kopec650648f2013-01-08 16:30:18 +0000231 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
Greg Clayton2bddd342010-09-07 20:11:56 +0000232
233 // If our process exited, then this thread should exit
Andrew Kaylor93132f52013-05-28 23:04:25 +0000234 if (exited && wait_pid == pid)
235 {
236 if (log)
237 log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000238 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000239 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000240 // If the callback returns true, it means this process should
241 // exit
242 if (callback_return)
Andrew Kaylor93132f52013-05-28 23:04:25 +0000243 {
244 if (log)
245 log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg);
Greg Clayton2bddd342010-09-07 20:11:56 +0000246 break;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000247 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000248 }
249 }
250 }
251 }
252
Caroline Tice20ad3c42010-10-29 21:48:37 +0000253 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Greg Clayton2bddd342010-09-07 20:11:56 +0000254 if (log)
255 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
256
257 return NULL;
258}
259
Greg Claytone38a5ed2012-01-05 03:57:59 +0000260
261void
262Host::SystemLog (SystemLogType type, const char *format, va_list args)
263{
264 vfprintf (stderr, format, args);
265}
266
Greg Claytone4e45922011-11-16 05:37:56 +0000267#endif // #if !defined (__APPLE__)
268
Greg Claytone38a5ed2012-01-05 03:57:59 +0000269void
270Host::SystemLog (SystemLogType type, const char *format, ...)
271{
272 va_list args;
273 va_start (args, format);
274 SystemLog (type, format, args);
275 va_end (args);
276}
277
Greg Clayton2bddd342010-09-07 20:11:56 +0000278size_t
279Host::GetPageSize()
280{
281 return ::getpagesize();
282}
283
Greg Clayton2bddd342010-09-07 20:11:56 +0000284const ArchSpec &
Greg Clayton514487e2011-02-15 21:59:32 +0000285Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
Greg Clayton2bddd342010-09-07 20:11:56 +0000286{
Greg Clayton514487e2011-02-15 21:59:32 +0000287 static bool g_supports_32 = false;
288 static bool g_supports_64 = false;
289 static ArchSpec g_host_arch_32;
290 static ArchSpec g_host_arch_64;
291
Greg Clayton2bddd342010-09-07 20:11:56 +0000292#if defined (__APPLE__)
Greg Clayton514487e2011-02-15 21:59:32 +0000293
294 // Apple is different in that it can support both 32 and 64 bit executables
295 // in the same operating system running concurrently. Here we detect the
296 // correct host architectures for both 32 and 64 bit including if 64 bit
297 // executables are supported on the system.
298
299 if (g_supports_32 == false && g_supports_64 == false)
300 {
301 // All apple systems support 32 bit execution.
302 g_supports_32 = true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000303 uint32_t cputype, cpusubtype;
Greg Clayton514487e2011-02-15 21:59:32 +0000304 uint32_t is_64_bit_capable = false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000305 size_t len = sizeof(cputype);
Greg Clayton514487e2011-02-15 21:59:32 +0000306 ArchSpec host_arch;
307 // These will tell us about the kernel architecture, which even on a 64
308 // bit machine can be 32 bit...
Greg Clayton2bddd342010-09-07 20:11:56 +0000309 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
310 {
Greg Clayton514487e2011-02-15 21:59:32 +0000311 len = sizeof (cpusubtype);
312 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
313 cpusubtype = CPU_TYPE_ANY;
314
Greg Clayton2bddd342010-09-07 20:11:56 +0000315 len = sizeof (is_64_bit_capable);
316 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
317 {
318 if (is_64_bit_capable)
Greg Clayton514487e2011-02-15 21:59:32 +0000319 g_supports_64 = true;
320 }
321
322 if (is_64_bit_capable)
323 {
Greg Clayton93d3c8332011-02-16 04:46:07 +0000324#if defined (__i386__) || defined (__x86_64__)
325 if (cpusubtype == CPU_SUBTYPE_486)
326 cpusubtype = CPU_SUBTYPE_I386_ALL;
327#endif
Greg Clayton514487e2011-02-15 21:59:32 +0000328 if (cputype & CPU_ARCH_ABI64)
Greg Clayton2bddd342010-09-07 20:11:56 +0000329 {
Greg Clayton514487e2011-02-15 21:59:32 +0000330 // We have a 64 bit kernel on a 64 bit system
Greg Claytone0d378b2011-03-24 21:19:54 +0000331 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
332 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton514487e2011-02-15 21:59:32 +0000333 }
334 else
335 {
336 // We have a 32 bit kernel on a 64 bit system
Greg Claytone0d378b2011-03-24 21:19:54 +0000337 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton2bddd342010-09-07 20:11:56 +0000338 cputype |= CPU_ARCH_ABI64;
Greg Claytone0d378b2011-03-24 21:19:54 +0000339 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton2bddd342010-09-07 20:11:56 +0000340 }
341 }
Greg Clayton514487e2011-02-15 21:59:32 +0000342 else
343 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000344 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
Greg Clayton514487e2011-02-15 21:59:32 +0000345 g_host_arch_64.Clear();
346 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000347 }
Greg Clayton514487e2011-02-15 21:59:32 +0000348 }
349
350#else // #if defined (__APPLE__)
Stephen Wilsonbd588712011-02-24 19:15:09 +0000351
Greg Clayton514487e2011-02-15 21:59:32 +0000352 if (g_supports_32 == false && g_supports_64 == false)
353 {
Peter Collingbourne1f6198d2011-11-05 01:35:31 +0000354 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton514487e2011-02-15 21:59:32 +0000355
Stephen Wilsonbd588712011-02-24 19:15:09 +0000356 g_host_arch_32.Clear();
357 g_host_arch_64.Clear();
Greg Clayton514487e2011-02-15 21:59:32 +0000358
Greg Claytonb29e6c62012-10-11 17:38:58 +0000359 // If the OS is Linux, "unknown" in the vendor slot isn't what we want
360 // for the default triple. It's probably an artifact of config.guess.
361 if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor)
362 triple.setVendorName("");
363
Stephen Wilsonbd588712011-02-24 19:15:09 +0000364 switch (triple.getArch())
365 {
366 default:
367 g_host_arch_32.SetTriple(triple);
368 g_supports_32 = true;
369 break;
Greg Clayton514487e2011-02-15 21:59:32 +0000370
Stephen Wilsonbd588712011-02-24 19:15:09 +0000371 case llvm::Triple::x86_64:
Greg Clayton542e4072012-09-07 17:49:29 +0000372 g_host_arch_64.SetTriple(triple);
373 g_supports_64 = true;
374 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
375 g_supports_32 = true;
376 break;
377
Stephen Wilsonbd588712011-02-24 19:15:09 +0000378 case llvm::Triple::sparcv9:
379 case llvm::Triple::ppc64:
Stephen Wilsonbd588712011-02-24 19:15:09 +0000380 g_host_arch_64.SetTriple(triple);
381 g_supports_64 = true;
382 break;
383 }
Greg Clayton4796c4f2011-02-17 02:05:38 +0000384
385 g_supports_32 = g_host_arch_32.IsValid();
386 g_supports_64 = g_host_arch_64.IsValid();
Greg Clayton2bddd342010-09-07 20:11:56 +0000387 }
Greg Clayton514487e2011-02-15 21:59:32 +0000388
389#endif // #else for #if defined (__APPLE__)
390
391 if (arch_kind == eSystemDefaultArchitecture32)
392 return g_host_arch_32;
393 else if (arch_kind == eSystemDefaultArchitecture64)
394 return g_host_arch_64;
395
396 if (g_supports_64)
397 return g_host_arch_64;
398
399 return g_host_arch_32;
Greg Clayton2bddd342010-09-07 20:11:56 +0000400}
401
402const ConstString &
403Host::GetVendorString()
404{
405 static ConstString g_vendor;
406 if (!g_vendor)
407 {
Greg Clayton950971f2012-05-12 00:01:21 +0000408 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
409 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
410 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton2bddd342010-09-07 20:11:56 +0000411 }
412 return g_vendor;
413}
414
415const ConstString &
416Host::GetOSString()
417{
418 static ConstString g_os_string;
419 if (!g_os_string)
420 {
Greg Clayton950971f2012-05-12 00:01:21 +0000421 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
422 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
423 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
Greg Clayton2bddd342010-09-07 20:11:56 +0000424 }
425 return g_os_string;
426}
427
428const ConstString &
429Host::GetTargetTriple()
430{
431 static ConstString g_host_triple;
432 if (!(g_host_triple))
433 {
Greg Clayton950971f2012-05-12 00:01:21 +0000434 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
435 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
Greg Clayton2bddd342010-09-07 20:11:56 +0000436 }
437 return g_host_triple;
438}
439
440lldb::pid_t
441Host::GetCurrentProcessID()
442{
443 return ::getpid();
444}
445
446lldb::tid_t
447Host::GetCurrentThreadID()
448{
449#if defined (__APPLE__)
Greg Clayton813ddfc2012-09-18 18:19:49 +0000450 // Calling "mach_port_deallocate()" bumps the reference count on the thread
451 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
452 // count.
453 thread_port_t thread_self = mach_thread_self();
454 mach_port_deallocate(mach_task_self(), thread_self);
455 return thread_self;
Johnny Chen8f3d8382011-08-02 20:52:42 +0000456#elif defined(__FreeBSD__)
457 return lldb::tid_t(pthread_getthreadid_np());
Greg Clayton2bddd342010-09-07 20:11:56 +0000458#else
459 return lldb::tid_t(pthread_self());
460#endif
461}
462
Jim Ingham372787f2012-04-07 00:00:41 +0000463lldb::thread_t
464Host::GetCurrentThread ()
465{
466 return lldb::thread_t(pthread_self());
467}
468
Greg Clayton2bddd342010-09-07 20:11:56 +0000469const char *
470Host::GetSignalAsCString (int signo)
471{
472 switch (signo)
473 {
474 case SIGHUP: return "SIGHUP"; // 1 hangup
475 case SIGINT: return "SIGINT"; // 2 interrupt
476 case SIGQUIT: return "SIGQUIT"; // 3 quit
477 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
478 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
479 case SIGABRT: return "SIGABRT"; // 6 abort()
Greg Clayton0ddf6be2011-11-04 03:42:38 +0000480#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
Greg Clayton2bddd342010-09-07 20:11:56 +0000481 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
Benjamin Kramer44030f12011-11-04 16:06:40 +0000482#endif
483#if !defined(_POSIX_C_SOURCE)
Greg Clayton2bddd342010-09-07 20:11:56 +0000484 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
Benjamin Kramer44030f12011-11-04 16:06:40 +0000485#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000486 case SIGFPE: return "SIGFPE"; // 8 floating point exception
487 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
488 case SIGBUS: return "SIGBUS"; // 10 bus error
489 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
490 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
491 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
492 case SIGALRM: return "SIGALRM"; // 14 alarm clock
493 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
494 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
495 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
496 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
497 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
498 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
499 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
500 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local&LTOSTOP)
501#if !defined(_POSIX_C_SOURCE)
502 case SIGIO: return "SIGIO"; // 23 input/output possible signal
503#endif
504 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
505 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
506 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
507 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
508#if !defined(_POSIX_C_SOURCE)
509 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
510 case SIGINFO: return "SIGINFO"; // 29 information request
511#endif
512 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
513 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
514 default:
515 break;
516 }
517 return NULL;
518}
519
520void
521Host::WillTerminate ()
522{
523}
524
Sylvestre Ledru59405832013-07-01 08:21:36 +0000525#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
Matt Kopec62502c62013-05-13 19:33:58 +0000526
Greg Clayton2bddd342010-09-07 20:11:56 +0000527void
528Host::ThreadCreated (const char *thread_name)
529{
530}
Greg Claytone5219662010-12-03 06:02:24 +0000531
Peter Collingbourne2ced9132011-08-05 00:35:43 +0000532void
Greg Claytone5219662010-12-03 06:02:24 +0000533Host::Backtrace (Stream &strm, uint32_t max_frames)
534{
Greg Clayton4272cc72011-02-02 02:24:04 +0000535 // TODO: Is there a way to backtrace the current process on linux? Other systems?
Greg Claytone5219662010-12-03 06:02:24 +0000536}
537
Greg Clayton85851dd2010-12-04 00:10:17 +0000538size_t
539Host::GetEnvironment (StringList &env)
540{
Greg Clayton4272cc72011-02-02 02:24:04 +0000541 // TODO: Is there a way to the host environment for this process on linux? Other systems?
Greg Clayton85851dd2010-12-04 00:10:17 +0000542 return 0;
543}
544
Sylvestre Ledru59405832013-07-01 08:21:36 +0000545#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000546
547struct HostThreadCreateInfo
548{
549 std::string thread_name;
550 thread_func_t thread_fptr;
551 thread_arg_t thread_arg;
552
553 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
554 thread_name (name ? name : ""),
555 thread_fptr (fptr),
556 thread_arg (arg)
557 {
558 }
559};
560
561static thread_result_t
562ThreadCreateTrampoline (thread_arg_t arg)
563{
564 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
565 Host::ThreadCreated (info->thread_name.c_str());
566 thread_func_t thread_fptr = info->thread_fptr;
567 thread_arg_t thread_arg = info->thread_arg;
568
Greg Clayton5160ce52013-03-27 23:08:40 +0000569 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
Greg Clayton2bddd342010-09-07 20:11:56 +0000570 if (log)
571 log->Printf("thread created");
572
573 delete info;
574 return thread_fptr (thread_arg);
575}
576
577lldb::thread_t
578Host::ThreadCreate
579(
580 const char *thread_name,
581 thread_func_t thread_fptr,
582 thread_arg_t thread_arg,
583 Error *error
584)
585{
586 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
587
588 // Host::ThreadCreateTrampoline will delete this pointer for us.
589 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
590
591 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
592 if (err == 0)
593 {
594 if (error)
595 error->Clear();
596 return thread;
597 }
598
599 if (error)
600 error->SetError (err, eErrorTypePOSIX);
601
602 return LLDB_INVALID_HOST_THREAD;
603}
604
605bool
606Host::ThreadCancel (lldb::thread_t thread, Error *error)
607{
608 int err = ::pthread_cancel (thread);
609 if (error)
610 error->SetError(err, eErrorTypePOSIX);
611 return err == 0;
612}
613
614bool
615Host::ThreadDetach (lldb::thread_t thread, Error *error)
616{
617 int err = ::pthread_detach (thread);
618 if (error)
619 error->SetError(err, eErrorTypePOSIX);
620 return err == 0;
621}
622
623bool
624Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
625{
626 int err = ::pthread_join (thread, thread_result_ptr);
627 if (error)
628 error->SetError(err, eErrorTypePOSIX);
629 return err == 0;
630}
631
Jim Inghamc075ecd2012-05-04 19:24:49 +0000632
Greg Clayton85719632013-02-27 22:51:58 +0000633std::string
Greg Clayton2bddd342010-09-07 20:11:56 +0000634Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
635{
Greg Clayton85719632013-02-27 22:51:58 +0000636 std::string thread_name;
Greg Clayton2bddd342010-09-07 20:11:56 +0000637#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton85719632013-02-27 22:51:58 +0000638 // We currently can only get the name of a thread in the current process.
639 if (pid == Host::GetCurrentProcessID())
640 {
641 char pthread_name[1024];
642 if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
Greg Clayton2bddd342010-09-07 20:11:56 +0000643 {
Greg Clayton85719632013-02-27 22:51:58 +0000644 if (pthread_name[0])
Greg Clayton2bddd342010-09-07 20:11:56 +0000645 {
Greg Clayton85719632013-02-27 22:51:58 +0000646 thread_name = pthread_name;
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000647 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000648 }
Greg Clayton85719632013-02-27 22:51:58 +0000649 else
650 {
651 dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
652 if (current_queue != NULL)
653 {
654 const char *queue_name = dispatch_queue_get_label (current_queue);
655 if (queue_name && queue_name[0])
656 {
657 thread_name = queue_name;
658 }
659 }
660 }
Greg Clayton2bddd342010-09-07 20:11:56 +0000661 }
Greg Clayton85719632013-02-27 22:51:58 +0000662#endif
663 return thread_name;
Greg Clayton2bddd342010-09-07 20:11:56 +0000664}
665
Matt Kopec62502c62013-05-13 19:33:58 +0000666bool
Greg Clayton2bddd342010-09-07 20:11:56 +0000667Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
668{
Greg Clayton85719632013-02-27 22:51:58 +0000669#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Greg Clayton2bddd342010-09-07 20:11:56 +0000670 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
671 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
672 if (pid == LLDB_INVALID_PROCESS_ID)
673 pid = curr_pid;
674
675 if (tid == LLDB_INVALID_THREAD_ID)
676 tid = curr_tid;
677
Greg Clayton2bddd342010-09-07 20:11:56 +0000678 // Set the pthread name if possible
679 if (pid == curr_pid && tid == curr_tid)
680 {
Matt Kopec62502c62013-05-13 19:33:58 +0000681 if (::pthread_setname_np (name) == 0)
682 return true;
Greg Clayton2bddd342010-09-07 20:11:56 +0000683 }
Matt Kopec62502c62013-05-13 19:33:58 +0000684 return false;
Sylvestre Ledru59405832013-07-01 08:21:36 +0000685#elif defined (__linux__) || defined (__GLIBC__)
Matt Kopec62502c62013-05-13 19:33:58 +0000686 void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
687 if (fn)
688 {
689 int (*pthread_setname_np_func)(pthread_t thread, const char *name);
690 *reinterpret_cast<void **> (&pthread_setname_np_func) = fn;
691
692 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
693 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
694
695 if (pid == LLDB_INVALID_PROCESS_ID)
696 pid = curr_pid;
697
698 if (tid == LLDB_INVALID_THREAD_ID)
699 tid = curr_tid;
700
701 if (pid == curr_pid)
702 {
703 if (pthread_setname_np_func (tid, name) == 0)
704 return true;
705 }
706 }
707 return false;
Jim Ingham5c42d8a2013-05-15 18:27:08 +0000708#else
709 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000710#endif
Greg Clayton2bddd342010-09-07 20:11:56 +0000711}
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 Claytonb3326392011-01-13 01:23:43 +0000725 g_program_filespec.SetFile (program_fullpath, false);
Greg Clayton2bddd342010-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 Claytonb3326392011-01-13 01:23:43 +0000732 g_program_filespec.SetFile (large_program_fullpath, false);
Greg Clayton2bddd342010-09-07 20:11:56 +0000733
734 ::free (large_program_fullpath);
735 }
736#elif defined (__linux__)
737 char exe_path[PATH_MAX];
Stephen Wilsone5b94a92011-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 Claytonb3326392011-01-13 01:23:43 +0000741 g_program_filespec.SetFile(exe_path, false);
Stephen Wilsone5b94a92011-01-12 04:21:21 +0000742 }
Sylvestre Ledru59405832013-07-01 08:21:36 +0000743#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Greg Clayton2bddd342010-09-07 20:11:56 +0000744 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 Clayton87ff1ac2011-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 Clayton2bddd342010-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 Clayton274060b2010-10-20 20:54:39 +0000766 module_filespec.SetFile(info.dli_fname, true);
Greg Clayton2bddd342010-09-07 20:11:56 +0000767 }
768 return module_filespec;
769}
770
771#if !defined (__APPLE__) // see Host.mm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000772
773bool
774Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
775{
776 bundle.Clear();
777 return false;
778}
779
Greg Clayton2bddd342010-09-07 20:11:56 +0000780bool
Greg Claytondd36def2010-10-17 22:03:32 +0000781Host::ResolveExecutableInBundle (FileSpec &file)
Greg Clayton2bddd342010-09-07 20:11:56 +0000782{
Greg Claytondd36def2010-10-17 22:03:32 +0000783 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +0000784}
785#endif
786
Greg Clayton45319462011-02-08 00:35:34 +0000787// Opaque info that tracks a dynamic library that was loaded
788struct DynamicLibraryInfo
Greg Clayton4272cc72011-02-02 02:24:04 +0000789{
Greg Clayton45319462011-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 Clayton4272cc72011-02-02 02:24:04 +0000805 char path[PATH_MAX];
806 if (file_spec.GetPath(path, sizeof(path)))
807 {
Greg Clayton45319462011-02-08 00:35:34 +0000808 int mode = 0;
809
810 if (options & eDynamicLibraryOpenOptionLazy)
811 mode |= RTLD_LAZY;
Greg Claytonf9399452011-02-08 05:24:57 +0000812 else
813 mode |= RTLD_NOW;
814
Greg Clayton45319462011-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 Clayton75852f52011-02-07 17:43:47 +0000824#endif
Greg Clayton45319462011-02-08 00:35:34 +0000825
826 void * opaque = ::dlopen (path, mode);
827
828 if (opaque)
Greg Clayton4272cc72011-02-02 02:24:04 +0000829 {
830 error.Clear();
Greg Clayton45319462011-02-08 00:35:34 +0000831 return new DynamicLibraryInfo (file_spec, options, opaque);
Greg Clayton4272cc72011-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 Clayton45319462011-02-08 00:35:34 +0000842 return NULL;
Greg Clayton4272cc72011-02-02 02:24:04 +0000843}
844
845Error
Greg Clayton45319462011-02-08 00:35:34 +0000846Host::DynamicLibraryClose (void *opaque)
Greg Clayton4272cc72011-02-02 02:24:04 +0000847{
848 Error error;
Greg Clayton45319462011-02-08 00:35:34 +0000849 if (opaque == NULL)
Greg Clayton4272cc72011-02-02 02:24:04 +0000850 {
851 error.SetErrorString ("invalid dynamic library handle");
852 }
Greg Clayton45319462011-02-08 00:35:34 +0000853 else
Greg Clayton4272cc72011-02-02 02:24:04 +0000854 {
Greg Clayton45319462011-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 Clayton4272cc72011-02-02 02:24:04 +0000864 }
865 return error;
866}
867
868void *
Greg Clayton45319462011-02-08 00:35:34 +0000869Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
Greg Clayton4272cc72011-02-02 02:24:04 +0000870{
Greg Clayton45319462011-02-08 00:35:34 +0000871 if (opaque == NULL)
Greg Clayton4272cc72011-02-02 02:24:04 +0000872 {
873 error.SetErrorString ("invalid dynamic library handle");
Greg Clayton4272cc72011-02-02 02:24:04 +0000874 }
Greg Clayton4272cc72011-02-02 02:24:04 +0000875 else
Greg Clayton45319462011-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 Claytonf9399452011-02-08 05:24:57 +0000886 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
Greg Clayton45319462011-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 Clayton4272cc72011-02-02 02:24:04 +0000909}
Greg Claytondd36def2010-10-17 22:03:32 +0000910
911bool
912Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
913{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000914 // To get paths related to LLDB we get the path to the executable that
Greg Claytondd36def2010-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 Claytondce502e2011-11-04 03:34:56 +0000952#if !defined (__arm__)
Greg Claytondd36def2010-10-17 22:03:32 +0000953 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
Greg Claytondce502e2011-11-04 03:34:56 +0000954#endif
Greg Claytondd36def2010-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 Clayton4272cc72011-02-02 02:24:04 +0000989 // TODO: Anyone know how we can determine this for linux? Other systems??
Greg Claytondd36def2010-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
Ed Mastea85d3642013-07-02 19:30:52 +0000998#ifndef LLDB_DISABLE_PYTHON
Greg Claytondd36def2010-10-17 22:03:32 +0000999 case ePathTypePythonDir:
1000 {
Greg Claytondd36def2010-10-17 22:03:32 +00001001 static ConstString g_lldb_python_dir;
1002 if (!g_lldb_python_dir)
1003 {
1004 FileSpec lldb_file_spec;
1005 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1006 {
1007 char raw_path[PATH_MAX];
1008 char resolved_path[PATH_MAX];
1009 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1010
1011#if defined (__APPLE__)
1012 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1013 if (framework_pos)
1014 {
1015 framework_pos += strlen("LLDB.framework");
1016 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1017 }
Filipe Cabecinhas0b751162012-07-30 16:46:32 +00001018#else
Daniel Malea53430eb2013-01-04 23:35:13 +00001019 llvm::Twine python_version_dir;
1020 python_version_dir = "/python"
1021 + llvm::Twine(PY_MAJOR_VERSION)
1022 + "."
1023 + llvm::Twine(PY_MINOR_VERSION)
1024 + "/site-packages";
1025
Filipe Cabecinhascffbd092012-07-30 18:56:10 +00001026 // We may get our string truncated. Should we protect
1027 // this with an assert?
Daniel Malea53430eb2013-01-04 23:35:13 +00001028
1029 ::strncat(raw_path, python_version_dir.str().c_str(),
1030 sizeof(raw_path) - strlen(raw_path) - 1);
1031
Greg Claytondd36def2010-10-17 22:03:32 +00001032#endif
1033 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1034 g_lldb_python_dir.SetCString(resolved_path);
1035 }
1036 }
1037 file_spec.GetDirectory() = g_lldb_python_dir;
1038 return file_spec.GetDirectory();
1039 }
1040 break;
Ed Mastea85d3642013-07-02 19:30:52 +00001041#endif
1042
Greg Clayton4272cc72011-02-02 02:24:04 +00001043 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1044 {
1045#if defined (__APPLE__)
1046 static ConstString g_lldb_system_plugin_dir;
Greg Clayton1cb64962011-03-24 04:28:38 +00001047 static bool g_lldb_system_plugin_dir_located = false;
1048 if (!g_lldb_system_plugin_dir_located)
Greg Clayton4272cc72011-02-02 02:24:04 +00001049 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001050 g_lldb_system_plugin_dir_located = true;
Greg Clayton4272cc72011-02-02 02:24:04 +00001051 FileSpec lldb_file_spec;
1052 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1053 {
1054 char raw_path[PATH_MAX];
1055 char resolved_path[PATH_MAX];
1056 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1057
1058 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1059 if (framework_pos)
1060 {
1061 framework_pos += strlen("LLDB.framework");
1062 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton1cb64962011-03-24 04:28:38 +00001063 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1064 g_lldb_system_plugin_dir.SetCString(resolved_path);
Greg Clayton4272cc72011-02-02 02:24:04 +00001065 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001066 return false;
Greg Clayton4272cc72011-02-02 02:24:04 +00001067 }
1068 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001069
1070 if (g_lldb_system_plugin_dir)
1071 {
1072 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1073 return true;
1074 }
Greg Clayton4272cc72011-02-02 02:24:04 +00001075#endif
1076 // TODO: where would system LLDB plug-ins be located on linux? Other systems?
1077 return false;
1078 }
1079 break;
1080
1081 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1082 {
1083#if defined (__APPLE__)
1084 static ConstString g_lldb_user_plugin_dir;
1085 if (!g_lldb_user_plugin_dir)
1086 {
1087 char user_plugin_path[PATH_MAX];
1088 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1089 user_plugin_path,
1090 sizeof(user_plugin_path)))
1091 {
1092 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1093 }
1094 }
1095 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1096 return file_spec.GetDirectory();
1097#endif
1098 // TODO: where would user LLDB plug-ins be located on linux? Other systems?
1099 return false;
1100 }
Greg Claytondd36def2010-10-17 22:03:32 +00001101 }
1102
1103 return false;
1104}
1105
Greg Clayton1cb64962011-03-24 04:28:38 +00001106
1107bool
1108Host::GetHostname (std::string &s)
1109{
1110 char hostname[PATH_MAX];
1111 hostname[sizeof(hostname) - 1] = '\0';
1112 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1113 {
1114 struct hostent* h = ::gethostbyname (hostname);
1115 if (h)
1116 s.assign (h->h_name);
1117 else
1118 s.assign (hostname);
1119 return true;
1120 }
1121 return false;
1122}
1123
Greg Clayton32e0a752011-03-30 18:16:51 +00001124const char *
1125Host::GetUserName (uint32_t uid, std::string &user_name)
1126{
1127 struct passwd user_info;
1128 struct passwd *user_info_ptr = &user_info;
1129 char user_buffer[PATH_MAX];
1130 size_t user_buffer_size = sizeof(user_buffer);
1131 if (::getpwuid_r (uid,
1132 &user_info,
1133 user_buffer,
1134 user_buffer_size,
1135 &user_info_ptr) == 0)
1136 {
1137 if (user_info_ptr)
1138 {
1139 user_name.assign (user_info_ptr->pw_name);
1140 return user_name.c_str();
1141 }
1142 }
1143 user_name.clear();
1144 return NULL;
1145}
1146
1147const char *
1148Host::GetGroupName (uint32_t gid, std::string &group_name)
1149{
1150 char group_buffer[PATH_MAX];
1151 size_t group_buffer_size = sizeof(group_buffer);
1152 struct group group_info;
1153 struct group *group_info_ptr = &group_info;
1154 // Try the threadsafe version first
1155 if (::getgrgid_r (gid,
1156 &group_info,
1157 group_buffer,
1158 group_buffer_size,
1159 &group_info_ptr) == 0)
1160 {
1161 if (group_info_ptr)
1162 {
1163 group_name.assign (group_info_ptr->gr_name);
1164 return group_name.c_str();
1165 }
1166 }
1167 else
1168 {
1169 // The threadsafe version isn't currently working
1170 // for me on darwin, but the non-threadsafe version
1171 // is, so I am calling it below.
1172 group_info_ptr = ::getgrgid (gid);
1173 if (group_info_ptr)
1174 {
1175 group_name.assign (group_info_ptr->gr_name);
1176 return group_name.c_str();
1177 }
1178 }
1179 group_name.clear();
1180 return NULL;
1181}
1182
Sylvestre Ledru59405832013-07-01 08:21:36 +00001183#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) // see macosx/Host.mm
Greg Clayton1cb64962011-03-24 04:28:38 +00001184bool
1185Host::GetOSBuildString (std::string &s)
1186{
1187 s.clear();
1188 return false;
1189}
1190
1191bool
1192Host::GetOSKernelDescription (std::string &s)
1193{
1194 s.clear();
1195 return false;
1196}
Johnny Chen8f3d8382011-08-02 20:52:42 +00001197#endif
Greg Clayton1cb64962011-03-24 04:28:38 +00001198
Han Ming Ong84647042012-02-25 01:07:38 +00001199uint32_t
1200Host::GetUserID ()
1201{
1202 return getuid();
1203}
1204
1205uint32_t
1206Host::GetGroupID ()
1207{
1208 return getgid();
1209}
1210
1211uint32_t
1212Host::GetEffectiveUserID ()
1213{
1214 return geteuid();
1215}
1216
1217uint32_t
1218Host::GetEffectiveGroupID ()
1219{
1220 return getegid();
1221}
1222
Daniel Malea25d7eb02013-05-15 17:54:07 +00001223#if !defined (__APPLE__) && !defined(__linux__)
Greg Claytone996fd32011-03-08 22:40:15 +00001224uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001225Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
Greg Claytone996fd32011-03-08 22:40:15 +00001226{
1227 process_infos.Clear();
Greg Claytone996fd32011-03-08 22:40:15 +00001228 return process_infos.GetSize();
Greg Clayton2bddd342010-09-07 20:11:56 +00001229}
Daniel Malea25d7eb02013-05-15 17:54:07 +00001230#endif // #if !defined (__APPLE__) && !defined(__linux__)
Greg Clayton2bddd342010-09-07 20:11:56 +00001231
Sylvestre Ledru59405832013-07-01 08:21:36 +00001232#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined(__linux__)
Greg Claytone996fd32011-03-08 22:40:15 +00001233bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001234Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton2bddd342010-09-07 20:11:56 +00001235{
Greg Claytone996fd32011-03-08 22:40:15 +00001236 process_info.Clear();
1237 return false;
Greg Clayton2bddd342010-09-07 20:11:56 +00001238}
Johnny Chen8f3d8382011-08-02 20:52:42 +00001239#endif
Greg Clayton2bddd342010-09-07 20:11:56 +00001240
Matt Kopec085d6ce2013-05-31 22:00:07 +00001241#if !defined(__linux__)
1242bool
1243Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
1244{
1245 return false;
1246}
1247#endif
1248
Sean Callananc0a6e062011-10-27 21:22:25 +00001249lldb::TargetSP
1250Host::GetDummyTarget (lldb_private::Debugger &debugger)
1251{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001252 static TargetSP g_dummy_target_sp;
Filipe Cabecinhasb0183452012-05-17 15:48:02 +00001253
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001254 // FIXME: Maybe the dummy target should be per-Debugger
1255 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1256 {
1257 ArchSpec arch(Target::GetDefaultArchitecture());
1258 if (!arch.IsValid())
1259 arch = Host::GetArchitecture ();
1260 Error err = debugger.GetTargetList().CreateTarget(debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +00001261 NULL,
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001262 arch.GetTriple().getTriple().c_str(),
1263 false,
1264 NULL,
1265 g_dummy_target_sp);
1266 }
Filipe Cabecinhasb0183452012-05-17 15:48:02 +00001267
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +00001268 return g_dummy_target_sp;
Sean Callananc0a6e062011-10-27 21:22:25 +00001269}
1270
Greg Claytond1cf11a2012-04-14 01:42:46 +00001271struct ShellInfo
1272{
1273 ShellInfo () :
1274 process_reaped (false),
1275 can_delete (false),
1276 pid (LLDB_INVALID_PROCESS_ID),
1277 signo(-1),
1278 status(-1)
1279 {
1280 }
1281
1282 lldb_private::Predicate<bool> process_reaped;
1283 lldb_private::Predicate<bool> can_delete;
1284 lldb::pid_t pid;
1285 int signo;
1286 int status;
1287};
1288
1289static bool
1290MonitorShellCommand (void *callback_baton,
1291 lldb::pid_t pid,
1292 bool exited, // True if the process did exit
1293 int signo, // Zero for no signal
1294 int status) // Exit value of process if signal is zero
1295{
1296 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1297 shell_info->pid = pid;
1298 shell_info->signo = signo;
1299 shell_info->status = status;
1300 // Let the thread running Host::RunShellCommand() know that the process
1301 // exited and that ShellInfo has been filled in by broadcasting to it
1302 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1303 // Now wait for a handshake back from that thread running Host::RunShellCommand
1304 // so we know that we can delete shell_info_ptr
1305 shell_info->can_delete.WaitForValueEqualTo(true);
1306 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1307 usleep(1000);
1308 // Now delete the shell info that was passed into this function
1309 delete shell_info;
1310 return true;
1311}
1312
1313Error
1314Host::RunShellCommand (const char *command,
1315 const char *working_dir,
1316 int *status_ptr,
1317 int *signo_ptr,
1318 std::string *command_output_ptr,
Greg Claytonc8f814d2012-09-27 03:13:55 +00001319 uint32_t timeout_sec,
1320 const char *shell)
Greg Claytond1cf11a2012-04-14 01:42:46 +00001321{
1322 Error error;
1323 ProcessLaunchInfo launch_info;
Greg Claytonc8f814d2012-09-27 03:13:55 +00001324 if (shell && shell[0])
1325 {
1326 // Run the command in a shell
1327 launch_info.SetShell(shell);
1328 launch_info.GetArguments().AppendArgument(command);
1329 const bool localhost = true;
1330 const bool will_debug = false;
1331 const bool first_arg_is_full_shell_command = true;
1332 launch_info.ConvertArgumentsForLaunchingInShell (error,
1333 localhost,
1334 will_debug,
1335 first_arg_is_full_shell_command);
1336 }
1337 else
1338 {
1339 // No shell, just run it
1340 Args args (command);
1341 const bool first_arg_is_executable = true;
Greg Clayton45392552012-10-17 22:57:12 +00001342 launch_info.SetArguments(args, first_arg_is_executable);
Greg Claytonc8f814d2012-09-27 03:13:55 +00001343 }
Greg Claytond1cf11a2012-04-14 01:42:46 +00001344
1345 if (working_dir)
1346 launch_info.SetWorkingDirectory(working_dir);
1347 char output_file_path_buffer[L_tmpnam];
1348 const char *output_file_path = NULL;
1349 if (command_output_ptr)
1350 {
1351 // Create a temporary file to get the stdout/stderr and redirect the
1352 // output of the command into this file. We will later read this file
1353 // if all goes well and fill the data into "command_output_ptr"
1354 output_file_path = ::tmpnam(output_file_path_buffer);
1355 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1356 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
Greg Claytonc8f814d2012-09-27 03:13:55 +00001357 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
Greg Claytond1cf11a2012-04-14 01:42:46 +00001358 }
1359 else
1360 {
1361 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1362 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1363 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1364 }
1365
1366 // The process monitor callback will delete the 'shell_info_ptr' below...
Greg Clayton7b0992d2013-04-18 22:45:39 +00001367 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
Greg Claytond1cf11a2012-04-14 01:42:46 +00001368
1369 const bool monitor_signals = false;
1370 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1371
1372 error = LaunchProcess (launch_info);
1373 const lldb::pid_t pid = launch_info.GetProcessID();
1374 if (pid != LLDB_INVALID_PROCESS_ID)
1375 {
1376 // The process successfully launched, so we can defer ownership of
1377 // "shell_info" to the MonitorShellCommand callback function that will
Greg Claytone01e07b2013-04-18 18:10:51 +00001378 // get called when the process dies. We release the unique pointer as it
Greg Claytond1cf11a2012-04-14 01:42:46 +00001379 // doesn't need to delete the ShellInfo anymore.
1380 ShellInfo *shell_info = shell_info_ap.release();
1381 TimeValue timeout_time(TimeValue::Now());
1382 timeout_time.OffsetWithSeconds(timeout_sec);
1383 bool timed_out = false;
1384 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1385 if (timed_out)
1386 {
1387 error.SetErrorString("timed out waiting for shell command to complete");
1388
1389 // Kill the process since it didn't complete withint the timeout specified
1390 ::kill (pid, SIGKILL);
1391 // Wait for the monitor callback to get the message
1392 timeout_time = TimeValue::Now();
1393 timeout_time.OffsetWithSeconds(1);
1394 timed_out = false;
1395 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1396 }
1397 else
1398 {
1399 if (status_ptr)
1400 *status_ptr = shell_info->status;
1401
1402 if (signo_ptr)
1403 *signo_ptr = shell_info->signo;
1404
1405 if (command_output_ptr)
1406 {
1407 command_output_ptr->clear();
1408 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1409 uint64_t file_size = file_spec.GetByteSize();
1410 if (file_size > 0)
1411 {
1412 if (file_size > command_output_ptr->max_size())
1413 {
1414 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1415 }
1416 else
1417 {
1418 command_output_ptr->resize(file_size);
1419 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1420 }
1421 }
1422 }
1423 }
1424 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1425 }
1426 else
1427 {
1428 error.SetErrorString("failed to get process ID");
1429 }
1430
1431 if (output_file_path)
1432 ::unlink (output_file_path);
1433 // Handshake with the monitor thread, or just let it know in advance that
1434 // it can delete "shell_info" in case we timed out and were not able to kill
1435 // the process...
1436 return error;
1437}
1438
1439
Greg Claytone3e3fee2013-02-17 20:46:30 +00001440uint32_t
1441Host::GetNumberCPUS ()
1442{
1443 static uint32_t g_num_cores = UINT32_MAX;
1444 if (g_num_cores == UINT32_MAX)
1445 {
Sylvestre Ledru59405832013-07-01 08:21:36 +00001446#if defined(__APPLE__) or defined (__linux__) or defined (__FreeBSD__) or defined (__FreeBSD_kernel__)
Greg Claytone3e3fee2013-02-17 20:46:30 +00001447
1448 g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN);
1449
1450#elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
1451
1452 // Header file for this might need to be included at the top of this file
1453 SYSTEM_INFO system_info;
1454 ::GetSystemInfo (&system_info);
1455 g_num_cores = system_info.dwNumberOfProcessors;
1456
1457#else
1458
1459 // Assume POSIX support if a host specific case has not been supplied above
1460 g_num_cores = 0;
1461 int num_cores = 0;
1462 size_t num_cores_len = sizeof(num_cores);
Sylvestre Ledru59405832013-07-01 08:21:36 +00001463#ifdef HW_AVAILCPU
Greg Claytone3e3fee2013-02-17 20:46:30 +00001464 int mib[] = { CTL_HW, HW_AVAILCPU };
Sylvestre Ledru59405832013-07-01 08:21:36 +00001465#else
1466 int mib[] = { CTL_HW, HW_NCPU };
1467#endif
Greg Claytone3e3fee2013-02-17 20:46:30 +00001468
1469 /* get the number of CPUs from the system */
1470 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1471 {
1472 g_num_cores = num_cores;
1473 }
1474 else
1475 {
1476 mib[1] = HW_NCPU;
1477 num_cores_len = sizeof(num_cores);
1478 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1479 {
1480 if (num_cores > 0)
1481 g_num_cores = num_cores;
1482 }
1483 }
1484#endif
1485 }
1486 return g_num_cores;
1487}
1488
1489
Greg Claytond1cf11a2012-04-14 01:42:46 +00001490
Johnny Chen8f3d8382011-08-02 20:52:42 +00001491#if !defined (__APPLE__)
Greg Clayton2bddd342010-09-07 20:11:56 +00001492bool
Greg Clayton3b147632010-12-18 01:54:34 +00001493Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
Greg Clayton2bddd342010-09-07 20:11:56 +00001494{
1495 return false;
1496}
Greg Claytondd36def2010-10-17 22:03:32 +00001497
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001498void
1499Host::SetCrashDescriptionWithFormat (const char *format, ...)
1500{
1501}
1502
1503void
1504Host::SetCrashDescription (const char *description)
1505{
1506}
Greg Claytondd36def2010-10-17 22:03:32 +00001507
1508lldb::pid_t
1509LaunchApplication (const FileSpec &app_file_spec)
1510{
1511 return LLDB_INVALID_PROCESS_ID;
1512}
1513
Greg Clayton2bddd342010-09-07 20:11:56 +00001514#endif