Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1 | //===-- 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 Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 12 | // C includes |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 14 | #include <limits.h> |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 17 | #ifdef _WIN32 |
| 18 | #include "lldb/Host/windows/windows.h" |
| 19 | #include <winsock2.h> |
Hafiz Abid Qadeer | a667875 | 2014-03-10 22:31:39 +0000 | [diff] [blame] | 20 | #include <ws2tcpip.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 21 | #else |
Deepak Panickal | b36da43 | 2014-01-13 14:55:15 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 23 | #include <dlfcn.h> |
| 24 | #include <grp.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 25 | #include <netdb.h> |
| 26 | #include <pwd.h> |
Jason Molenda | 4a4b5dc | 2013-11-21 02:45:55 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
Sylvestre Ledru | 785ee47 | 2013-09-05 15:39:09 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | #if !defined (__GNU__) && !defined (_WIN32) |
| 31 | // Does not exist under GNU/HURD or Windows |
Ed Maste | f2b0162 | 2013-06-28 12:35:08 +0000 | [diff] [blame] | 32 | #include <sys/sysctl.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 33 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 34 | |
| 35 | #if defined (__APPLE__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 36 | #include <mach/mach_port.h> |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 37 | #include <mach/mach_init.h> |
| 38 | #include <mach-o/dyld.h> |
Jim Ingham | 1a7ee70 | 2013-11-22 00:51:36 +0000 | [diff] [blame] | 39 | #include <AvailabilityMacros.h> |
Todd Fiala | e88aea3 | 2014-07-24 23:22:58 +0000 | [diff] [blame] | 40 | #ifndef CPU_SUBTYPE_X86_64_H |
| 41 | #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t)8) |
| 42 | #endif |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 43 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 44 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 45 | #if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) || defined(__NetBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 46 | #include <spawn.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 47 | #include <sys/wait.h> |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 48 | #include <sys/syscall.h> |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 49 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 50 | |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 51 | #if defined (__FreeBSD__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 52 | #include <pthread_np.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 55 | // C++ includes |
| 56 | #include <limits> |
| 57 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 58 | #include "lldb/Host/Host.h" |
| 59 | #include "lldb/Core/ArchSpec.h" |
| 60 | #include "lldb/Core/ConstString.h" |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 61 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 62 | #include "lldb/Core/Error.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 63 | #include "lldb/Core/Log.h" |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 64 | #include "lldb/Core/Module.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 65 | #include "lldb/Core/StreamString.h" |
Jim Ingham | c075ecd | 2012-05-04 19:24:49 +0000 | [diff] [blame] | 66 | #include "lldb/Core/ThreadSafeSTLMap.h" |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 67 | #include "lldb/Host/Config.h" |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 68 | #include "lldb/Host/Endian.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 69 | #include "lldb/Host/FileSpec.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 70 | #include "lldb/Host/Mutex.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 71 | #include "lldb/Target/FileAction.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 72 | #include "lldb/Target/Process.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 73 | #include "lldb/Target/ProcessLaunchInfo.h" |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 74 | #include "lldb/Target/TargetList.h" |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 75 | #include "lldb/Utility/CleanUp.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 76 | |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 77 | #include "llvm/ADT/STLExtras.h" |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame] | 78 | #include "llvm/ADT/SmallString.h" |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 79 | #include "llvm/Support/Host.h" |
Zachary Turner | 9e757b7 | 2014-07-28 16:45:05 +0000 | [diff] [blame] | 80 | #include "llvm/Support/Path.h" |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame] | 81 | #include "llvm/Support/raw_ostream.h" |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 82 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 83 | #if defined (__APPLE__) |
| 84 | #ifndef _POSIX_SPAWN_DISABLE_ASLR |
| 85 | #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 |
| 86 | #endif |
| 87 | |
| 88 | extern "C" |
| 89 | { |
| 90 | int __pthread_chdir(const char *path); |
| 91 | int __pthread_fchdir (int fildes); |
| 92 | } |
| 93 | |
| 94 | #endif |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 95 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 96 | using namespace lldb; |
| 97 | using namespace lldb_private; |
| 98 | |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 99 | // Define maximum thread name length |
| 100 | #if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__NetBSD__) |
| 101 | uint32_t const Host::MAX_THREAD_NAME_LENGTH = 16; |
| 102 | #else |
| 103 | uint32_t const Host::MAX_THREAD_NAME_LENGTH = std::numeric_limits<uint32_t>::max (); |
| 104 | #endif |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 105 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 106 | #if !defined (__APPLE__) && !defined (_WIN32) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 107 | struct MonitorInfo |
| 108 | { |
| 109 | lldb::pid_t pid; // The process ID to monitor |
| 110 | Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals |
| 111 | void *callback_baton; // The callback baton for the callback function |
| 112 | bool monitor_signals; // If true, call the callback when "pid" gets signaled. |
| 113 | }; |
| 114 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 115 | static thread_result_t |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 116 | MonitorChildProcessThreadFunction (void *arg); |
| 117 | |
| 118 | lldb::thread_t |
| 119 | Host::StartMonitoringChildProcess |
| 120 | ( |
| 121 | Host::MonitorChildProcessCallback callback, |
| 122 | void *callback_baton, |
| 123 | lldb::pid_t pid, |
| 124 | bool monitor_signals |
| 125 | ) |
| 126 | { |
| 127 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 128 | MonitorInfo * info_ptr = new MonitorInfo(); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 129 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 130 | info_ptr->pid = pid; |
| 131 | info_ptr->callback = callback; |
| 132 | info_ptr->callback_baton = callback_baton; |
| 133 | info_ptr->monitor_signals = monitor_signals; |
| 134 | |
| 135 | char thread_name[256]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 136 | ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 137 | thread = ThreadCreate (thread_name, |
| 138 | MonitorChildProcessThreadFunction, |
| 139 | info_ptr, |
| 140 | NULL); |
| 141 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 142 | return thread; |
| 143 | } |
| 144 | |
| 145 | //------------------------------------------------------------------ |
| 146 | // Scoped class that will disable thread canceling when it is |
| 147 | // constructed, and exception safely restore the previous value it |
| 148 | // when it goes out of scope. |
| 149 | //------------------------------------------------------------------ |
| 150 | class ScopedPThreadCancelDisabler |
| 151 | { |
| 152 | public: |
| 153 | ScopedPThreadCancelDisabler() |
| 154 | { |
| 155 | // Disable the ability for this thread to be cancelled |
| 156 | int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state); |
| 157 | if (err != 0) |
| 158 | m_old_state = -1; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | ~ScopedPThreadCancelDisabler() |
| 163 | { |
| 164 | // Restore the ability for this thread to be cancelled to what it |
| 165 | // previously was. |
| 166 | if (m_old_state != -1) |
| 167 | ::pthread_setcancelstate (m_old_state, 0); |
| 168 | } |
| 169 | private: |
| 170 | int m_old_state; // Save the old cancelability state. |
| 171 | }; |
| 172 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 173 | static thread_result_t |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 174 | MonitorChildProcessThreadFunction (void *arg) |
| 175 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 176 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 177 | const char *function = __FUNCTION__; |
| 178 | if (log) |
| 179 | log->Printf ("%s (arg = %p) thread starting...", function, arg); |
| 180 | |
| 181 | MonitorInfo *info = (MonitorInfo *)arg; |
| 182 | |
| 183 | const Host::MonitorChildProcessCallback callback = info->callback; |
| 184 | void * const callback_baton = info->callback_baton; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 185 | const bool monitor_signals = info->monitor_signals; |
| 186 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 187 | assert (info->pid <= UINT32_MAX); |
Andrew MacPherson | 82aae0d | 2014-04-02 06:57:45 +0000 | [diff] [blame] | 188 | const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 189 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 190 | delete info; |
| 191 | |
| 192 | int status = -1; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 193 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame] | 194 | #define __WALL 0 |
| 195 | #endif |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 196 | const int options = __WALL; |
| 197 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 198 | while (1) |
| 199 | { |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 200 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 201 | if (log) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 202 | log->Printf("%s ::wait_pid (pid = %" PRIi32 ", &status, options = %i)...", function, pid, options); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 203 | |
| 204 | // Wait for all child processes |
| 205 | ::pthread_testcancel (); |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 206 | // Get signals from all children with same process group of pid |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 207 | const ::pid_t wait_pid = ::waitpid (pid, &status, options); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 208 | ::pthread_testcancel (); |
| 209 | |
| 210 | if (wait_pid == -1) |
| 211 | { |
| 212 | if (errno == EINTR) |
| 213 | continue; |
| 214 | else |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 215 | { |
| 216 | if (log) |
| 217 | log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 218 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 219 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 220 | } |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 221 | else if (wait_pid > 0) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 222 | { |
| 223 | bool exited = false; |
| 224 | int signal = 0; |
| 225 | int exit_status = 0; |
| 226 | const char *status_cstr = NULL; |
| 227 | if (WIFSTOPPED(status)) |
| 228 | { |
| 229 | signal = WSTOPSIG(status); |
| 230 | status_cstr = "STOPPED"; |
| 231 | } |
| 232 | else if (WIFEXITED(status)) |
| 233 | { |
| 234 | exit_status = WEXITSTATUS(status); |
| 235 | status_cstr = "EXITED"; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 236 | exited = true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 237 | } |
| 238 | else if (WIFSIGNALED(status)) |
| 239 | { |
| 240 | signal = WTERMSIG(status); |
| 241 | status_cstr = "SIGNALED"; |
Andrew Kaylor | 7d2abdf | 2013-09-04 16:06:04 +0000 | [diff] [blame] | 242 | if (wait_pid == abs(pid)) { |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 243 | exited = true; |
| 244 | exit_status = -1; |
| 245 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
Johnny Chen | 4480530 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 249 | status_cstr = "(\?\?\?)"; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Scope for pthread_cancel_disabler |
| 253 | { |
| 254 | ScopedPThreadCancelDisabler pthread_cancel_disabler; |
| 255 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 256 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 257 | if (log) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 258 | log->Printf ("%s ::waitpid (pid = %" PRIi32 ", &status, options = %i) => pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 259 | function, |
| 260 | wait_pid, |
| 261 | options, |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 262 | pid, |
| 263 | status, |
| 264 | status_cstr, |
| 265 | signal, |
| 266 | exit_status); |
| 267 | |
| 268 | if (exited || (signal != 0 && monitor_signals)) |
| 269 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 270 | bool callback_return = false; |
| 271 | if (callback) |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 272 | callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 273 | |
| 274 | // If our process exited, then this thread should exit |
Andrew Kaylor | 7d2abdf | 2013-09-04 16:06:04 +0000 | [diff] [blame] | 275 | if (exited && wait_pid == abs(pid)) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 276 | { |
| 277 | if (log) |
| 278 | log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 279 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 280 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 281 | // If the callback returns true, it means this process should |
| 282 | // exit |
| 283 | if (callback_return) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 284 | { |
| 285 | if (log) |
| 286 | log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 287 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 288 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 294 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 295 | if (log) |
| 296 | log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg); |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 301 | #endif // #if !defined (__APPLE__) && !defined (_WIN32) |
| 302 | |
| 303 | #if !defined (__APPLE__) |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 304 | |
| 305 | void |
| 306 | Host::SystemLog (SystemLogType type, const char *format, va_list args) |
| 307 | { |
| 308 | vfprintf (stderr, format, args); |
| 309 | } |
| 310 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 311 | #endif |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 312 | |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 313 | void |
| 314 | Host::SystemLog (SystemLogType type, const char *format, ...) |
| 315 | { |
| 316 | va_list args; |
| 317 | va_start (args, format); |
| 318 | SystemLog (type, format, args); |
| 319 | va_end (args); |
| 320 | } |
| 321 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 322 | const ArchSpec & |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 323 | Host::GetArchitecture (SystemDefaultArchitecture arch_kind) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 324 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 325 | static bool g_supports_32 = false; |
| 326 | static bool g_supports_64 = false; |
| 327 | static ArchSpec g_host_arch_32; |
| 328 | static ArchSpec g_host_arch_64; |
| 329 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 330 | #if defined (__APPLE__) |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 331 | |
| 332 | // Apple is different in that it can support both 32 and 64 bit executables |
| 333 | // in the same operating system running concurrently. Here we detect the |
| 334 | // correct host architectures for both 32 and 64 bit including if 64 bit |
| 335 | // executables are supported on the system. |
| 336 | |
| 337 | if (g_supports_32 == false && g_supports_64 == false) |
| 338 | { |
| 339 | // All apple systems support 32 bit execution. |
| 340 | g_supports_32 = true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 341 | uint32_t cputype, cpusubtype; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 342 | uint32_t is_64_bit_capable = false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 343 | size_t len = sizeof(cputype); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 344 | ArchSpec host_arch; |
| 345 | // These will tell us about the kernel architecture, which even on a 64 |
| 346 | // bit machine can be 32 bit... |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 347 | if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0) |
| 348 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 349 | len = sizeof (cpusubtype); |
| 350 | if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0) |
| 351 | cpusubtype = CPU_TYPE_ANY; |
| 352 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 353 | len = sizeof (is_64_bit_capable); |
| 354 | if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0) |
| 355 | { |
| 356 | if (is_64_bit_capable) |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 357 | g_supports_64 = true; |
| 358 | } |
| 359 | |
| 360 | if (is_64_bit_capable) |
| 361 | { |
| 362 | if (cputype & CPU_ARCH_ABI64) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 363 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 364 | // We have a 64 bit kernel on a 64 bit system |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 365 | g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 366 | } |
| 367 | else |
| 368 | { |
Greg Clayton | 52edb36 | 2014-07-14 22:53:02 +0000 | [diff] [blame] | 369 | // We have a 64 bit kernel that is returning a 32 bit cputype, the |
| 370 | // cpusubtype will be correct as if it were for a 64 bit architecture |
| 371 | g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype | CPU_ARCH_ABI64, cpusubtype); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 372 | } |
Greg Clayton | 52edb36 | 2014-07-14 22:53:02 +0000 | [diff] [blame] | 373 | |
| 374 | // Now we need modify the cpusubtype for the 32 bit slices. |
| 375 | uint32_t cpusubtype32 = cpusubtype; |
| 376 | #if defined (__i386__) || defined (__x86_64__) |
| 377 | if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H) |
| 378 | cpusubtype32 = CPU_SUBTYPE_I386_ALL; |
| 379 | #elif defined (__arm__) || defined (__arm64__) || defined (__aarch64__) |
| 380 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 381 | cpusubtype32 = CPU_SUBTYPE_ARM_V7S; |
| 382 | #endif |
| 383 | g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), cpusubtype32); |
Greg Clayton | c76fa8a | 2014-07-29 21:27:21 +0000 | [diff] [blame] | 384 | |
| 385 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 386 | { |
| 387 | g_host_arch_32.GetTriple().setOS(llvm::Triple::IOS); |
| 388 | g_host_arch_64.GetTriple().setOS(llvm::Triple::IOS); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | g_host_arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
| 393 | g_host_arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
| 394 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 395 | } |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 396 | else |
| 397 | { |
Greg Clayton | 52edb36 | 2014-07-14 22:53:02 +0000 | [diff] [blame] | 398 | // We have a 32 bit kernel on a 32 bit system |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 399 | g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 400 | g_host_arch_64.Clear(); |
| 401 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 402 | } |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | #else // #if defined (__APPLE__) |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 406 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 407 | if (g_supports_32 == false && g_supports_64 == false) |
| 408 | { |
Peter Collingbourne | 1f6198d | 2011-11-05 01:35:31 +0000 | [diff] [blame] | 409 | llvm::Triple triple(llvm::sys::getDefaultTargetTriple()); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 410 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 411 | g_host_arch_32.Clear(); |
| 412 | g_host_arch_64.Clear(); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 413 | |
Greg Clayton | b29e6c6 | 2012-10-11 17:38:58 +0000 | [diff] [blame] | 414 | // If the OS is Linux, "unknown" in the vendor slot isn't what we want |
| 415 | // for the default triple. It's probably an artifact of config.guess. |
| 416 | if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor) |
Todd Fiala | e28f1d7 | 2014-01-17 22:21:22 +0000 | [diff] [blame] | 417 | triple.setVendorName (""); |
Greg Clayton | b29e6c6 | 2012-10-11 17:38:58 +0000 | [diff] [blame] | 418 | |
Todd Fiala | a9ddb0e | 2014-01-18 03:02:39 +0000 | [diff] [blame] | 419 | const char* distribution_id = GetDistributionId ().AsCString(); |
| 420 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 421 | switch (triple.getArch()) |
| 422 | { |
| 423 | default: |
| 424 | g_host_arch_32.SetTriple(triple); |
Todd Fiala | a9ddb0e | 2014-01-18 03:02:39 +0000 | [diff] [blame] | 425 | g_host_arch_32.SetDistributionId (distribution_id); |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 426 | g_supports_32 = true; |
| 427 | break; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 428 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 429 | case llvm::Triple::x86_64: |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 430 | g_host_arch_64.SetTriple(triple); |
Todd Fiala | a9ddb0e | 2014-01-18 03:02:39 +0000 | [diff] [blame] | 431 | g_host_arch_64.SetDistributionId (distribution_id); |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 432 | g_supports_64 = true; |
| 433 | g_host_arch_32.SetTriple(triple.get32BitArchVariant()); |
Todd Fiala | a9ddb0e | 2014-01-18 03:02:39 +0000 | [diff] [blame] | 434 | g_host_arch_32.SetDistributionId (distribution_id); |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 435 | g_supports_32 = true; |
| 436 | break; |
| 437 | |
Ed Maste | a837576 | 2014-04-01 18:06:45 +0000 | [diff] [blame] | 438 | case llvm::Triple::mips64: |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 439 | case llvm::Triple::sparcv9: |
| 440 | case llvm::Triple::ppc64: |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 441 | g_host_arch_64.SetTriple(triple); |
Todd Fiala | a9ddb0e | 2014-01-18 03:02:39 +0000 | [diff] [blame] | 442 | g_host_arch_64.SetDistributionId (distribution_id); |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 443 | g_supports_64 = true; |
| 444 | break; |
| 445 | } |
Greg Clayton | 4796c4f | 2011-02-17 02:05:38 +0000 | [diff] [blame] | 446 | |
| 447 | g_supports_32 = g_host_arch_32.IsValid(); |
| 448 | g_supports_64 = g_host_arch_64.IsValid(); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 449 | } |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 450 | |
| 451 | #endif // #else for #if defined (__APPLE__) |
| 452 | |
| 453 | if (arch_kind == eSystemDefaultArchitecture32) |
| 454 | return g_host_arch_32; |
| 455 | else if (arch_kind == eSystemDefaultArchitecture64) |
| 456 | return g_host_arch_64; |
| 457 | |
| 458 | if (g_supports_64) |
| 459 | return g_host_arch_64; |
| 460 | |
| 461 | return g_host_arch_32; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | const ConstString & |
| 465 | Host::GetVendorString() |
| 466 | { |
| 467 | static ConstString g_vendor; |
| 468 | if (!g_vendor) |
| 469 | { |
Greg Clayton | 950971f | 2012-05-12 00:01:21 +0000 | [diff] [blame] | 470 | const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture); |
| 471 | const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName(); |
| 472 | g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size()); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 473 | } |
| 474 | return g_vendor; |
| 475 | } |
| 476 | |
| 477 | const ConstString & |
| 478 | Host::GetOSString() |
| 479 | { |
| 480 | static ConstString g_os_string; |
| 481 | if (!g_os_string) |
| 482 | { |
Greg Clayton | 950971f | 2012-05-12 00:01:21 +0000 | [diff] [blame] | 483 | const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture); |
| 484 | const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName(); |
| 485 | g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size()); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 486 | } |
| 487 | return g_os_string; |
| 488 | } |
| 489 | |
| 490 | const ConstString & |
| 491 | Host::GetTargetTriple() |
| 492 | { |
| 493 | static ConstString g_host_triple; |
| 494 | if (!(g_host_triple)) |
| 495 | { |
Greg Clayton | 950971f | 2012-05-12 00:01:21 +0000 | [diff] [blame] | 496 | const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture); |
| 497 | g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str()); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 498 | } |
| 499 | return g_host_triple; |
| 500 | } |
| 501 | |
Todd Fiala | f3d61de | 2014-01-17 20:18:59 +0000 | [diff] [blame] | 502 | // See linux/Host.cpp for Linux-based implementations of this. |
| 503 | // Add your platform-specific implementation to the appropriate host file. |
| 504 | #if !defined(__linux__) |
| 505 | |
| 506 | const ConstString & |
| 507 | Host::GetDistributionId () |
| 508 | { |
| 509 | static ConstString s_distribution_id; |
| 510 | return s_distribution_id; |
| 511 | } |
| 512 | |
| 513 | #endif // #if !defined(__linux__) |
| 514 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 515 | lldb::pid_t |
| 516 | Host::GetCurrentProcessID() |
| 517 | { |
| 518 | return ::getpid(); |
| 519 | } |
| 520 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 521 | #ifndef _WIN32 |
| 522 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 523 | lldb::tid_t |
| 524 | Host::GetCurrentThreadID() |
| 525 | { |
| 526 | #if defined (__APPLE__) |
Jean-Daniel Dupas | 3cfa8e2 | 2013-12-06 09:35:53 +0000 | [diff] [blame] | 527 | // Calling "mach_thread_self()" bumps the reference count on the thread |
Greg Clayton | 813ddfc | 2012-09-18 18:19:49 +0000 | [diff] [blame] | 528 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
| 529 | // count. |
| 530 | thread_port_t thread_self = mach_thread_self(); |
| 531 | mach_port_deallocate(mach_task_self(), thread_self); |
| 532 | return thread_self; |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 533 | #elif defined(__FreeBSD__) |
| 534 | return lldb::tid_t(pthread_getthreadid_np()); |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 535 | #elif defined(__linux__) |
| 536 | return lldb::tid_t(syscall(SYS_gettid)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 537 | #else |
| 538 | return lldb::tid_t(pthread_self()); |
| 539 | #endif |
| 540 | } |
| 541 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 542 | lldb::thread_t |
| 543 | Host::GetCurrentThread () |
| 544 | { |
| 545 | return lldb::thread_t(pthread_self()); |
| 546 | } |
| 547 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 548 | const char * |
| 549 | Host::GetSignalAsCString (int signo) |
| 550 | { |
| 551 | switch (signo) |
| 552 | { |
| 553 | case SIGHUP: return "SIGHUP"; // 1 hangup |
| 554 | case SIGINT: return "SIGINT"; // 2 interrupt |
| 555 | case SIGQUIT: return "SIGQUIT"; // 3 quit |
| 556 | case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught) |
| 557 | case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught) |
| 558 | case SIGABRT: return "SIGABRT"; // 6 abort() |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 559 | #if defined(SIGPOLL) |
Sylvestre Ledru | a4cc7de | 2013-12-13 09:51:39 +0000 | [diff] [blame] | 560 | #if !defined(SIGIO) || (SIGPOLL != SIGIO) |
| 561 | // Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to |
| 562 | // fail with 'multiple define cases with same value' |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 563 | case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported) |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 564 | #endif |
Sylvestre Ledru | a4cc7de | 2013-12-13 09:51:39 +0000 | [diff] [blame] | 565 | #endif |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 566 | #if defined(SIGEMT) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 567 | case SIGEMT: return "SIGEMT"; // 7 EMT instruction |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 568 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 569 | case SIGFPE: return "SIGFPE"; // 8 floating point exception |
| 570 | case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored) |
| 571 | case SIGBUS: return "SIGBUS"; // 10 bus error |
| 572 | case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation |
| 573 | case SIGSYS: return "SIGSYS"; // 12 bad argument to system call |
| 574 | case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it |
| 575 | case SIGALRM: return "SIGALRM"; // 14 alarm clock |
| 576 | case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill |
| 577 | case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel |
| 578 | case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty |
| 579 | case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty |
| 580 | case SIGCONT: return "SIGCONT"; // 19 continue a stopped process |
| 581 | case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit |
| 582 | case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read |
| 583 | case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP) |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 584 | #if defined(SIGIO) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 585 | case SIGIO: return "SIGIO"; // 23 input/output possible signal |
| 586 | #endif |
| 587 | case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit |
| 588 | case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit |
| 589 | case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm |
| 590 | case SIGPROF: return "SIGPROF"; // 27 profiling time alarm |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 591 | #if defined(SIGWINCH) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 592 | case SIGWINCH: return "SIGWINCH"; // 28 window size changes |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 593 | #endif |
| 594 | #if defined(SIGINFO) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 595 | case SIGINFO: return "SIGINFO"; // 29 information request |
| 596 | #endif |
| 597 | case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1 |
| 598 | case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2 |
| 599 | default: |
| 600 | break; |
| 601 | } |
| 602 | return NULL; |
| 603 | } |
| 604 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 605 | #endif |
| 606 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 607 | void |
| 608 | Host::WillTerminate () |
| 609 | { |
| 610 | } |
| 611 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 612 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 613 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 614 | void |
| 615 | Host::ThreadCreated (const char *thread_name) |
| 616 | { |
| 617 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 618 | |
Peter Collingbourne | 2ced913 | 2011-08-05 00:35:43 +0000 | [diff] [blame] | 619 | void |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 620 | Host::Backtrace (Stream &strm, uint32_t max_frames) |
| 621 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 622 | // TODO: Is there a way to backtrace the current process on other systems? |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 625 | size_t |
| 626 | Host::GetEnvironment (StringList &env) |
| 627 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 628 | // TODO: Is there a way to the host environment for this process on other systems? |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 632 | #endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 633 | |
| 634 | struct HostThreadCreateInfo |
| 635 | { |
| 636 | std::string thread_name; |
| 637 | thread_func_t thread_fptr; |
| 638 | thread_arg_t thread_arg; |
| 639 | |
| 640 | HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) : |
| 641 | thread_name (name ? name : ""), |
| 642 | thread_fptr (fptr), |
| 643 | thread_arg (arg) |
| 644 | { |
| 645 | } |
| 646 | }; |
| 647 | |
| 648 | static thread_result_t |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 649 | #ifdef _WIN32 |
| 650 | __stdcall |
| 651 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 652 | ThreadCreateTrampoline (thread_arg_t arg) |
| 653 | { |
| 654 | HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg; |
| 655 | Host::ThreadCreated (info->thread_name.c_str()); |
| 656 | thread_func_t thread_fptr = info->thread_fptr; |
| 657 | thread_arg_t thread_arg = info->thread_arg; |
| 658 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 659 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 660 | if (log) |
| 661 | log->Printf("thread created"); |
| 662 | |
| 663 | delete info; |
| 664 | return thread_fptr (thread_arg); |
| 665 | } |
| 666 | |
| 667 | lldb::thread_t |
| 668 | Host::ThreadCreate |
| 669 | ( |
| 670 | const char *thread_name, |
| 671 | thread_func_t thread_fptr, |
| 672 | thread_arg_t thread_arg, |
| 673 | Error *error |
| 674 | ) |
| 675 | { |
| 676 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
| 677 | |
| 678 | // Host::ThreadCreateTrampoline will delete this pointer for us. |
| 679 | HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg); |
| 680 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 681 | #ifdef _WIN32 |
| 682 | thread = ::_beginthreadex(0, 0, ThreadCreateTrampoline, info_ptr, 0, NULL); |
| 683 | int err = thread <= 0 ? GetLastError() : 0; |
| 684 | #else |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 685 | int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 686 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 687 | if (err == 0) |
| 688 | { |
| 689 | if (error) |
| 690 | error->Clear(); |
| 691 | return thread; |
| 692 | } |
| 693 | |
| 694 | if (error) |
| 695 | error->SetError (err, eErrorTypePOSIX); |
| 696 | |
| 697 | return LLDB_INVALID_HOST_THREAD; |
| 698 | } |
| 699 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 700 | #ifndef _WIN32 |
| 701 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 702 | bool |
| 703 | Host::ThreadCancel (lldb::thread_t thread, Error *error) |
| 704 | { |
| 705 | int err = ::pthread_cancel (thread); |
| 706 | if (error) |
| 707 | error->SetError(err, eErrorTypePOSIX); |
| 708 | return err == 0; |
| 709 | } |
| 710 | |
| 711 | bool |
| 712 | Host::ThreadDetach (lldb::thread_t thread, Error *error) |
| 713 | { |
| 714 | int err = ::pthread_detach (thread); |
| 715 | if (error) |
| 716 | error->SetError(err, eErrorTypePOSIX); |
| 717 | return err == 0; |
| 718 | } |
| 719 | |
| 720 | bool |
| 721 | Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error) |
| 722 | { |
| 723 | int err = ::pthread_join (thread, thread_result_ptr); |
| 724 | if (error) |
| 725 | error->SetError(err, eErrorTypePOSIX); |
| 726 | return err == 0; |
| 727 | } |
| 728 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 729 | lldb::thread_key_t |
| 730 | Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) |
| 731 | { |
| 732 | pthread_key_t key; |
| 733 | ::pthread_key_create (&key, callback); |
| 734 | return key; |
| 735 | } |
| 736 | |
| 737 | void* |
| 738 | Host::ThreadLocalStorageGet(lldb::thread_key_t key) |
| 739 | { |
| 740 | return ::pthread_getspecific (key); |
| 741 | } |
| 742 | |
| 743 | void |
| 744 | Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) |
| 745 | { |
| 746 | ::pthread_setspecific (key, value); |
| 747 | } |
| 748 | |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 749 | bool |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 750 | Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name) |
| 751 | { |
Greg Clayton | 8571963 | 2013-02-27 22:51:58 +0000 | [diff] [blame] | 752 | #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 753 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 754 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 755 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 756 | pid = curr_pid; |
| 757 | |
| 758 | if (tid == LLDB_INVALID_THREAD_ID) |
| 759 | tid = curr_tid; |
| 760 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 761 | // Set the pthread name if possible |
| 762 | if (pid == curr_pid && tid == curr_tid) |
| 763 | { |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 764 | if (::pthread_setname_np (name) == 0) |
| 765 | return true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 766 | } |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 767 | return false; |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 768 | #elif defined (__FreeBSD__) |
| 769 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 770 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 771 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 772 | pid = curr_pid; |
| 773 | |
| 774 | if (tid == LLDB_INVALID_THREAD_ID) |
| 775 | tid = curr_tid; |
| 776 | |
| 777 | // Set the pthread name if possible |
| 778 | if (pid == curr_pid && tid == curr_tid) |
| 779 | { |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 780 | ::pthread_set_name_np (::pthread_self(), name); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 781 | return true; |
| 782 | } |
| 783 | return false; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 784 | #elif defined (__linux__) || defined (__GLIBC__) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 785 | void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np"); |
| 786 | if (fn) |
| 787 | { |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 788 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 789 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 790 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 791 | pid = curr_pid; |
| 792 | |
| 793 | if (tid == LLDB_INVALID_THREAD_ID) |
| 794 | tid = curr_tid; |
| 795 | |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 796 | if (pid == curr_pid && tid == curr_tid) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 797 | { |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 798 | int (*pthread_setname_np_func)(pthread_t thread, const char *name); |
| 799 | *reinterpret_cast<void **> (&pthread_setname_np_func) = fn; |
| 800 | |
| 801 | if (pthread_setname_np_func (::pthread_self(), name) == 0) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 802 | return true; |
| 803 | } |
| 804 | } |
| 805 | return false; |
Jim Ingham | 5c42d8a | 2013-05-15 18:27:08 +0000 | [diff] [blame] | 806 | #else |
| 807 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 808 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 811 | bool |
| 812 | Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, |
| 813 | const char *thread_name, size_t len) |
| 814 | { |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 815 | std::unique_ptr<char[]> namebuf(new char[len+1]); |
| 816 | |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 817 | // Thread names are coming in like '<lldb.comm.debugger.edit>' and |
| 818 | // '<lldb.comm.debugger.editline>'. So just chopping the end of the string |
| 819 | // off leads to a lot of similar named threads. Go through the thread name |
| 820 | // and search for the last dot and use that. |
| 821 | const char *lastdot = ::strrchr (thread_name, '.'); |
| 822 | |
| 823 | if (lastdot && lastdot != thread_name) |
| 824 | thread_name = lastdot + 1; |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 825 | ::strncpy (namebuf.get(), thread_name, len); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 826 | namebuf[len] = 0; |
| 827 | |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 828 | int namebuflen = strlen(namebuf.get()); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 829 | if (namebuflen > 0) |
| 830 | { |
| 831 | if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>') |
| 832 | { |
| 833 | // Trim off trailing '(' and '>' characters for a bit more cleanup. |
| 834 | namebuflen--; |
| 835 | namebuf[namebuflen] = 0; |
| 836 | } |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 837 | return Host::SetThreadName (pid, tid, namebuf.get()); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 838 | } |
| 839 | return false; |
| 840 | } |
| 841 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 842 | #endif |
| 843 | |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 844 | FileSpec::PathSyntax |
| 845 | Host::GetHostPathSyntax() |
| 846 | { |
| 847 | #if defined(_WIN32) |
| 848 | return FileSpec::ePathSyntaxWindows; |
| 849 | #else |
| 850 | return FileSpec::ePathSyntaxPosix; |
| 851 | #endif |
| 852 | } |
| 853 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 854 | FileSpec |
Zachary Turner | 9e757b7 | 2014-07-28 16:45:05 +0000 | [diff] [blame] | 855 | Host::GetUserProfileFileSpec () |
| 856 | { |
| 857 | static FileSpec g_profile_filespec; |
| 858 | if (!g_profile_filespec) |
| 859 | { |
| 860 | llvm::SmallString<64> path; |
| 861 | llvm::sys::path::home_directory(path); |
| 862 | return FileSpec(path.c_str(), false); |
| 863 | } |
| 864 | return g_profile_filespec; |
| 865 | } |
| 866 | |
| 867 | FileSpec |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 868 | Host::GetProgramFileSpec () |
| 869 | { |
| 870 | static FileSpec g_program_filespec; |
| 871 | if (!g_program_filespec) |
| 872 | { |
| 873 | #if defined (__APPLE__) |
| 874 | char program_fullpath[PATH_MAX]; |
| 875 | // If DST is NULL, then return the number of bytes needed. |
| 876 | uint32_t len = sizeof(program_fullpath); |
| 877 | int err = _NSGetExecutablePath (program_fullpath, &len); |
| 878 | if (err == 0) |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 879 | g_program_filespec.SetFile (program_fullpath, false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 880 | else if (err == -1) |
| 881 | { |
| 882 | char *large_program_fullpath = (char *)::malloc (len + 1); |
| 883 | |
| 884 | err = _NSGetExecutablePath (large_program_fullpath, &len); |
| 885 | if (err == 0) |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 886 | g_program_filespec.SetFile (large_program_fullpath, false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 887 | |
| 888 | ::free (large_program_fullpath); |
| 889 | } |
| 890 | #elif defined (__linux__) |
| 891 | char exe_path[PATH_MAX]; |
Stephen Wilson | e5b94a9 | 2011-01-12 04:21:21 +0000 | [diff] [blame] | 892 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); |
| 893 | if (len > 0) { |
| 894 | exe_path[len] = 0; |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 895 | g_program_filespec.SetFile(exe_path, false); |
Stephen Wilson | e5b94a9 | 2011-01-12 04:21:21 +0000 | [diff] [blame] | 896 | } |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 897 | #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 898 | int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() }; |
| 899 | size_t exe_path_size; |
| 900 | if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) |
| 901 | { |
Greg Clayton | 87ff1ac | 2011-01-13 01:27:55 +0000 | [diff] [blame] | 902 | char *exe_path = new char[exe_path_size]; |
| 903 | if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) |
| 904 | g_program_filespec.SetFile(exe_path, false); |
| 905 | delete[] exe_path; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 906 | } |
Zachary Turner | 9e757b7 | 2014-07-28 16:45:05 +0000 | [diff] [blame] | 907 | #elif defined(_WIN32) |
| 908 | std::vector<char> buffer(PATH_MAX); |
| 909 | ::GetModuleFileName(NULL, &buffer[0], buffer.size()); |
Zachary Turner | d8a5273 | 2014-07-29 05:39:21 +0000 | [diff] [blame] | 910 | g_program_filespec.SetFile(&buffer[0], false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 911 | #endif |
| 912 | } |
| 913 | return g_program_filespec; |
| 914 | } |
| 915 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 916 | #if !defined (__APPLE__) // see Host.mm |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 917 | |
| 918 | bool |
| 919 | Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle) |
| 920 | { |
| 921 | bundle.Clear(); |
| 922 | return false; |
| 923 | } |
| 924 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 925 | bool |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 926 | Host::ResolveExecutableInBundle (FileSpec &file) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 927 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 928 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 929 | } |
| 930 | #endif |
| 931 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 932 | #ifndef _WIN32 |
| 933 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 934 | // Opaque info that tracks a dynamic library that was loaded |
| 935 | struct DynamicLibraryInfo |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 936 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 937 | DynamicLibraryInfo (const FileSpec &fs, int o, void *h) : |
| 938 | file_spec (fs), |
| 939 | open_options (o), |
| 940 | handle (h) |
| 941 | { |
| 942 | } |
| 943 | |
| 944 | const FileSpec file_spec; |
| 945 | uint32_t open_options; |
| 946 | void * handle; |
| 947 | }; |
| 948 | |
| 949 | void * |
| 950 | Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error) |
| 951 | { |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 952 | char path[PATH_MAX]; |
| 953 | if (file_spec.GetPath(path, sizeof(path))) |
| 954 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 955 | int mode = 0; |
| 956 | |
| 957 | if (options & eDynamicLibraryOpenOptionLazy) |
| 958 | mode |= RTLD_LAZY; |
Greg Clayton | f939945 | 2011-02-08 05:24:57 +0000 | [diff] [blame] | 959 | else |
| 960 | mode |= RTLD_NOW; |
| 961 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 962 | |
| 963 | if (options & eDynamicLibraryOpenOptionLocal) |
| 964 | mode |= RTLD_LOCAL; |
| 965 | else |
| 966 | mode |= RTLD_GLOBAL; |
| 967 | |
| 968 | #ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED |
| 969 | if (options & eDynamicLibraryOpenOptionLimitGetSymbol) |
| 970 | mode |= RTLD_FIRST; |
Greg Clayton | 75852f5 | 2011-02-07 17:43:47 +0000 | [diff] [blame] | 971 | #endif |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 972 | |
| 973 | void * opaque = ::dlopen (path, mode); |
| 974 | |
| 975 | if (opaque) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 976 | { |
| 977 | error.Clear(); |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 978 | return new DynamicLibraryInfo (file_spec, options, opaque); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 979 | } |
| 980 | else |
| 981 | { |
| 982 | error.SetErrorString(::dlerror()); |
| 983 | } |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | error.SetErrorString("failed to extract path"); |
| 988 | } |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 989 | return NULL; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | Error |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 993 | Host::DynamicLibraryClose (void *opaque) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 994 | { |
| 995 | Error error; |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 996 | if (opaque == NULL) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 997 | { |
| 998 | error.SetErrorString ("invalid dynamic library handle"); |
| 999 | } |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 1000 | else |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1001 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 1002 | DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque; |
| 1003 | if (::dlclose (dylib_info->handle) != 0) |
| 1004 | { |
| 1005 | error.SetErrorString(::dlerror()); |
| 1006 | } |
| 1007 | |
| 1008 | dylib_info->open_options = 0; |
| 1009 | dylib_info->handle = 0; |
| 1010 | delete dylib_info; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1011 | } |
| 1012 | return error; |
| 1013 | } |
| 1014 | |
| 1015 | void * |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 1016 | Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1017 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 1018 | if (opaque == NULL) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1019 | { |
| 1020 | error.SetErrorString ("invalid dynamic library handle"); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1021 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1022 | else |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 1023 | { |
| 1024 | DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque; |
| 1025 | |
| 1026 | void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name); |
| 1027 | if (symbol_addr) |
| 1028 | { |
| 1029 | #ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED |
| 1030 | // This host doesn't support limiting searches to this shared library |
| 1031 | // so we need to verify that the match came from this shared library |
| 1032 | // if it was requested in the Host::DynamicLibraryOpen() function. |
Greg Clayton | f939945 | 2011-02-08 05:24:57 +0000 | [diff] [blame] | 1033 | if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol) |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 1034 | { |
| 1035 | FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr)); |
| 1036 | if (match_dylib_spec != dylib_info->file_spec) |
| 1037 | { |
| 1038 | char dylib_path[PATH_MAX]; |
| 1039 | if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path))) |
| 1040 | error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path); |
| 1041 | else |
| 1042 | error.SetErrorString ("symbol not found"); |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | } |
| 1046 | #endif |
| 1047 | error.Clear(); |
| 1048 | return symbol_addr; |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | error.SetErrorString(::dlerror()); |
| 1053 | } |
| 1054 | } |
| 1055 | return NULL; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1056 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1057 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1058 | FileSpec |
| 1059 | Host::GetModuleFileSpecForHostAddress (const void *host_addr) |
| 1060 | { |
| 1061 | FileSpec module_filespec; |
| 1062 | Dl_info info; |
| 1063 | if (::dladdr (host_addr, &info)) |
| 1064 | { |
| 1065 | if (info.dli_fname) |
| 1066 | module_filespec.SetFile(info.dli_fname, true); |
| 1067 | } |
| 1068 | return module_filespec; |
| 1069 | } |
| 1070 | |
| 1071 | #endif |
| 1072 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 1073 | |
| 1074 | static void CleanupProcessSpecificLLDBTempDir () |
| 1075 | { |
| 1076 | // Get the process specific LLDB temporary directory and delete it. |
| 1077 | FileSpec tmpdir_file_spec; |
| 1078 | if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
| 1079 | { |
| 1080 | // Remove the LLDB temporary directory if we have one. Set "recurse" to |
| 1081 | // true to all files that were created for the LLDB process can be cleaned up. |
| 1082 | const bool recurse = true; |
| 1083 | Host::RemoveDirectory(tmpdir_file_spec.GetDirectory().GetCString(), recurse); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1087 | bool |
| 1088 | Host::GetLLDBPath (PathType path_type, FileSpec &file_spec) |
| 1089 | { |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 1090 | // To get paths related to LLDB we get the path to the executable that |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1091 | // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB", |
| 1092 | // on linux this is assumed to be the "lldb" main executable. If LLDB on |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1093 | // linux is actually in a shared library (liblldb.so) then this function will |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1094 | // need to be modified to "do the right thing". |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1095 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1096 | |
| 1097 | switch (path_type) |
| 1098 | { |
| 1099 | case ePathTypeLLDBShlibDir: |
| 1100 | { |
| 1101 | static ConstString g_lldb_so_dir; |
| 1102 | if (!g_lldb_so_dir) |
| 1103 | { |
David Majnemer | 8490da1 | 2014-07-22 22:00:42 +0000 | [diff] [blame] | 1104 | FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress( |
| 1105 | reinterpret_cast<void *>(reinterpret_cast<intptr_t>(Host::GetLLDBPath)))); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1106 | g_lldb_so_dir = lldb_file_spec.GetDirectory(); |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1107 | if (log) |
| 1108 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBShlibDir) => '%s'", g_lldb_so_dir.GetCString()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1109 | } |
| 1110 | file_spec.GetDirectory() = g_lldb_so_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1111 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1112 | } |
| 1113 | break; |
| 1114 | |
| 1115 | case ePathTypeSupportExecutableDir: |
| 1116 | { |
| 1117 | static ConstString g_lldb_support_exe_dir; |
| 1118 | if (!g_lldb_support_exe_dir) |
| 1119 | { |
| 1120 | FileSpec lldb_file_spec; |
| 1121 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1122 | { |
| 1123 | char raw_path[PATH_MAX]; |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1124 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1125 | |
| 1126 | #if defined (__APPLE__) |
| 1127 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1128 | if (framework_pos) |
| 1129 | { |
| 1130 | framework_pos += strlen("LLDB.framework"); |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 1131 | #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__) |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1132 | // Shallow bundle |
| 1133 | *framework_pos = '\0'; |
| 1134 | #else |
| 1135 | // Normal bundle |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1136 | ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 1137 | #endif |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1138 | } |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 1139 | #elif defined (__linux__) || defined (__FreeBSD__) || defined (__NetBSD__) |
| 1140 | // Linux/*BSD will attempt to replace a */lib with */bin as the base directory for |
| 1141 | // helper exe programs. This will fail if the /lib and /bin directories are rooted in entirely |
| 1142 | // different trees. |
| 1143 | if (log) |
| 1144 | log->Printf ("Host::%s() attempting to derive the bin path (ePathTypeSupportExecutableDir) from this path: %s", __FUNCTION__, raw_path); |
| 1145 | char *lib_pos = ::strstr (raw_path, "/lib"); |
| 1146 | if (lib_pos != nullptr) |
| 1147 | { |
| 1148 | // First terminate the raw path at the start of lib. |
| 1149 | *lib_pos = '\0'; |
| 1150 | |
| 1151 | // Now write in bin in place of lib. |
| 1152 | ::strncpy (lib_pos, "/bin", PATH_MAX - (lib_pos - raw_path)); |
| 1153 | |
| 1154 | if (log) |
| 1155 | log->Printf ("Host::%s() derived the bin path as: %s", __FUNCTION__, raw_path); |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | if (log) |
| 1160 | log->Printf ("Host::%s() failed to find /lib/liblldb within the shared lib path, bailing on bin path construction", __FUNCTION__); |
| 1161 | } |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 1162 | #endif // #if defined (__APPLE__) |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 1163 | llvm::SmallString<64> resolved_path(raw_path); |
| 1164 | FileSpec::Resolve (resolved_path); |
| 1165 | g_lldb_support_exe_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1166 | } |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1167 | if (log) |
| 1168 | log->Printf("Host::GetLLDBPath(ePathTypeSupportExecutableDir) => '%s'", g_lldb_support_exe_dir.GetCString()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1169 | } |
| 1170 | file_spec.GetDirectory() = g_lldb_support_exe_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1171 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1172 | } |
| 1173 | break; |
| 1174 | |
| 1175 | case ePathTypeHeaderDir: |
| 1176 | { |
| 1177 | static ConstString g_lldb_headers_dir; |
| 1178 | if (!g_lldb_headers_dir) |
| 1179 | { |
| 1180 | #if defined (__APPLE__) |
| 1181 | FileSpec lldb_file_spec; |
| 1182 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1183 | { |
| 1184 | char raw_path[PATH_MAX]; |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1185 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1186 | |
| 1187 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1188 | if (framework_pos) |
| 1189 | { |
| 1190 | framework_pos += strlen("LLDB.framework"); |
| 1191 | ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); |
| 1192 | } |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1193 | llvm::SmallString<64> resolved_path(raw_path); |
| 1194 | FileSpec::Resolve (resolved_path); |
| 1195 | g_lldb_headers_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1196 | } |
| 1197 | #else |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1198 | // TODO: Anyone know how we can determine this for linux? Other systems?? |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1199 | g_lldb_headers_dir.SetCString ("/opt/local/include/lldb"); |
| 1200 | #endif |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1201 | if (log) |
| 1202 | log->Printf("Host::GetLLDBPath(ePathTypeHeaderDir) => '%s'", g_lldb_headers_dir.GetCString()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1203 | } |
| 1204 | file_spec.GetDirectory() = g_lldb_headers_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1205 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1206 | } |
| 1207 | break; |
| 1208 | |
Joerg Sonnenberger | c759876 | 2013-09-25 17:52:18 +0000 | [diff] [blame] | 1209 | #ifdef LLDB_DISABLE_PYTHON |
| 1210 | case ePathTypePythonDir: |
| 1211 | return false; |
| 1212 | #else |
| 1213 | case ePathTypePythonDir: |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1214 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1215 | static ConstString g_lldb_python_dir; |
| 1216 | if (!g_lldb_python_dir) |
| 1217 | { |
| 1218 | FileSpec lldb_file_spec; |
| 1219 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1220 | { |
| 1221 | char raw_path[PATH_MAX]; |
Zachary Turner | c9bf0c7 | 2014-07-18 20:36:08 +0000 | [diff] [blame] | 1222 | #if defined(_WIN32) |
| 1223 | lldb_file_spec.AppendPathComponent("../lib/site-packages"); |
| 1224 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1225 | #else |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1226 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1227 | |
| 1228 | #if defined (__APPLE__) |
| 1229 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1230 | if (framework_pos) |
| 1231 | { |
| 1232 | framework_pos += strlen("LLDB.framework"); |
| 1233 | ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); |
Todd Fiala | 5000e28 | 2014-01-18 08:05:32 +0000 | [diff] [blame] | 1234 | } |
| 1235 | else |
| 1236 | { |
| 1237 | #endif |
| 1238 | llvm::SmallString<256> python_version_dir; |
| 1239 | llvm::raw_svector_ostream os(python_version_dir); |
| 1240 | os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages"; |
| 1241 | os.flush(); |
| 1242 | |
| 1243 | // We may get our string truncated. Should we protect |
| 1244 | // this with an assert? |
| 1245 | |
| 1246 | ::strncat(raw_path, python_version_dir.c_str(), |
| 1247 | sizeof(raw_path) - strlen(raw_path) - 1); |
Zachary Turner | c9bf0c7 | 2014-07-18 20:36:08 +0000 | [diff] [blame] | 1248 | #endif |
Todd Fiala | 5000e28 | 2014-01-18 08:05:32 +0000 | [diff] [blame] | 1249 | #if defined (__APPLE__) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1250 | } |
| 1251 | #endif |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 1252 | llvm::SmallString<64> resolved_path(raw_path); |
| 1253 | FileSpec::Resolve (resolved_path); |
| 1254 | g_lldb_python_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1255 | } |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1256 | |
| 1257 | if (log) |
| 1258 | log->Printf("Host::GetLLDBPath(ePathTypePythonDir) => '%s'", g_lldb_python_dir.GetCString()); |
| 1259 | |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1260 | } |
| 1261 | file_spec.GetDirectory() = g_lldb_python_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1262 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1263 | } |
| 1264 | break; |
Ed Maste | a85d364 | 2013-07-02 19:30:52 +0000 | [diff] [blame] | 1265 | #endif |
| 1266 | |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1267 | case ePathTypeLLDBSystemPlugins: // System plug-ins directory |
| 1268 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1269 | #if defined (__APPLE__) || defined(__linux__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1270 | static ConstString g_lldb_system_plugin_dir; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1271 | static bool g_lldb_system_plugin_dir_located = false; |
| 1272 | if (!g_lldb_system_plugin_dir_located) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1273 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1274 | g_lldb_system_plugin_dir_located = true; |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1275 | #if defined (__APPLE__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1276 | FileSpec lldb_file_spec; |
| 1277 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1278 | { |
| 1279 | char raw_path[PATH_MAX]; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1280 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1281 | |
| 1282 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1283 | if (framework_pos) |
| 1284 | { |
| 1285 | framework_pos += strlen("LLDB.framework"); |
| 1286 | ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path)); |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1287 | llvm::SmallString<64> resolved_path(raw_path); |
| 1288 | FileSpec::Resolve (resolved_path); |
| 1289 | g_lldb_system_plugin_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1290 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1291 | return false; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1292 | } |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1293 | #elif defined (__linux__) |
| 1294 | FileSpec lldb_file_spec("/usr/lib/lldb", true); |
| 1295 | if (lldb_file_spec.Exists()) |
| 1296 | { |
| 1297 | g_lldb_system_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str()); |
| 1298 | } |
| 1299 | #endif // __APPLE__ || __linux__ |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (log) |
| 1302 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBSystemPlugins) => '%s'", g_lldb_system_plugin_dir.GetCString()); |
| 1303 | |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1304 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (g_lldb_system_plugin_dir) |
| 1307 | { |
| 1308 | file_spec.GetDirectory() = g_lldb_system_plugin_dir; |
| 1309 | return true; |
| 1310 | } |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1311 | #else |
| 1312 | // TODO: where would system LLDB plug-ins be located on other systems? |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1313 | return false; |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1314 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1315 | } |
| 1316 | break; |
| 1317 | |
| 1318 | case ePathTypeLLDBUserPlugins: // User plug-ins directory |
| 1319 | { |
| 1320 | #if defined (__APPLE__) |
| 1321 | static ConstString g_lldb_user_plugin_dir; |
| 1322 | if (!g_lldb_user_plugin_dir) |
| 1323 | { |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1324 | llvm::SmallString<64> user_plugin_path("~/Library/Application Support/LLDB/PlugIns"); |
| 1325 | FileSpec::Resolve (user_plugin_path); |
| 1326 | if (user_plugin_path.size()) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1327 | { |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1328 | g_lldb_user_plugin_dir.SetCString(user_plugin_path.c_str()); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | file_spec.GetDirectory() = g_lldb_user_plugin_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1332 | return (bool)file_spec.GetDirectory(); |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1333 | #elif defined (__linux__) |
| 1334 | static ConstString g_lldb_user_plugin_dir; |
| 1335 | if (!g_lldb_user_plugin_dir) |
| 1336 | { |
| 1337 | // XDG Base Directory Specification |
| 1338 | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 1339 | // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. |
| 1340 | FileSpec lldb_file_spec; |
| 1341 | const char *xdg_data_home = getenv("XDG_DATA_HOME"); |
| 1342 | if (xdg_data_home && xdg_data_home[0]) |
| 1343 | { |
| 1344 | std::string user_plugin_dir (xdg_data_home); |
| 1345 | user_plugin_dir += "/lldb"; |
| 1346 | lldb_file_spec.SetFile (user_plugin_dir.c_str(), true); |
| 1347 | } |
| 1348 | else |
| 1349 | { |
| 1350 | const char *home_dir = getenv("HOME"); |
| 1351 | if (home_dir && home_dir[0]) |
| 1352 | { |
| 1353 | std::string user_plugin_dir (home_dir); |
| 1354 | user_plugin_dir += "/.local/share/lldb"; |
| 1355 | lldb_file_spec.SetFile (user_plugin_dir.c_str(), true); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | if (lldb_file_spec.Exists()) |
| 1360 | g_lldb_user_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str()); |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1361 | if (log) |
| 1362 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBUserPlugins) => '%s'", g_lldb_user_plugin_dir.GetCString()); |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1363 | } |
| 1364 | file_spec.GetDirectory() = g_lldb_user_plugin_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1365 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1366 | #endif |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1367 | // TODO: where would user LLDB plug-ins be located on other systems? |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | } |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1370 | |
| 1371 | case ePathTypeLLDBTempSystemDir: |
| 1372 | { |
| 1373 | static ConstString g_lldb_tmp_dir; |
| 1374 | if (!g_lldb_tmp_dir) |
| 1375 | { |
| 1376 | const char *tmpdir_cstr = getenv("TMPDIR"); |
| 1377 | if (tmpdir_cstr == NULL) |
| 1378 | { |
| 1379 | tmpdir_cstr = getenv("TMP"); |
| 1380 | if (tmpdir_cstr == NULL) |
| 1381 | tmpdir_cstr = getenv("TEMP"); |
| 1382 | } |
| 1383 | if (tmpdir_cstr) |
| 1384 | { |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 1385 | StreamString pid_tmpdir; |
| 1386 | pid_tmpdir.Printf("%s/lldb", tmpdir_cstr); |
| 1387 | if (Host::MakeDirectory(pid_tmpdir.GetString().c_str(), eFilePermissionsDirectoryDefault).Success()) |
| 1388 | { |
| 1389 | pid_tmpdir.Printf("/%" PRIu64, Host::GetCurrentProcessID()); |
| 1390 | if (Host::MakeDirectory(pid_tmpdir.GetString().c_str(), eFilePermissionsDirectoryDefault).Success()) |
| 1391 | { |
| 1392 | // Make an atexit handler to clean up the process specify LLDB temp dir |
| 1393 | // and all of its contents. |
| 1394 | ::atexit (CleanupProcessSpecificLLDBTempDir); |
| 1395 | g_lldb_tmp_dir.SetCString(pid_tmpdir.GetString().c_str()); |
| 1396 | if (log) |
| 1397 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBTempSystemDir) => '%s'", g_lldb_tmp_dir.GetCString()); |
| 1398 | |
| 1399 | } |
| 1400 | } |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | file_spec.GetDirectory() = g_lldb_tmp_dir; |
| 1404 | return (bool)file_spec.GetDirectory(); |
| 1405 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1411 | |
| 1412 | bool |
| 1413 | Host::GetHostname (std::string &s) |
| 1414 | { |
| 1415 | char hostname[PATH_MAX]; |
| 1416 | hostname[sizeof(hostname) - 1] = '\0'; |
| 1417 | if (::gethostname (hostname, sizeof(hostname) - 1) == 0) |
| 1418 | { |
| 1419 | struct hostent* h = ::gethostbyname (hostname); |
| 1420 | if (h) |
| 1421 | s.assign (h->h_name); |
| 1422 | else |
| 1423 | s.assign (hostname); |
| 1424 | return true; |
| 1425 | } |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1429 | #ifndef _WIN32 |
| 1430 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1431 | const char * |
| 1432 | Host::GetUserName (uint32_t uid, std::string &user_name) |
| 1433 | { |
| 1434 | struct passwd user_info; |
| 1435 | struct passwd *user_info_ptr = &user_info; |
| 1436 | char user_buffer[PATH_MAX]; |
| 1437 | size_t user_buffer_size = sizeof(user_buffer); |
| 1438 | if (::getpwuid_r (uid, |
| 1439 | &user_info, |
| 1440 | user_buffer, |
| 1441 | user_buffer_size, |
| 1442 | &user_info_ptr) == 0) |
| 1443 | { |
| 1444 | if (user_info_ptr) |
| 1445 | { |
| 1446 | user_name.assign (user_info_ptr->pw_name); |
| 1447 | return user_name.c_str(); |
| 1448 | } |
| 1449 | } |
| 1450 | user_name.clear(); |
| 1451 | return NULL; |
| 1452 | } |
| 1453 | |
| 1454 | const char * |
| 1455 | Host::GetGroupName (uint32_t gid, std::string &group_name) |
| 1456 | { |
| 1457 | char group_buffer[PATH_MAX]; |
| 1458 | size_t group_buffer_size = sizeof(group_buffer); |
| 1459 | struct group group_info; |
| 1460 | struct group *group_info_ptr = &group_info; |
| 1461 | // Try the threadsafe version first |
| 1462 | if (::getgrgid_r (gid, |
| 1463 | &group_info, |
| 1464 | group_buffer, |
| 1465 | group_buffer_size, |
| 1466 | &group_info_ptr) == 0) |
| 1467 | { |
| 1468 | if (group_info_ptr) |
| 1469 | { |
| 1470 | group_name.assign (group_info_ptr->gr_name); |
| 1471 | return group_name.c_str(); |
| 1472 | } |
| 1473 | } |
| 1474 | else |
| 1475 | { |
| 1476 | // The threadsafe version isn't currently working |
| 1477 | // for me on darwin, but the non-threadsafe version |
| 1478 | // is, so I am calling it below. |
| 1479 | group_info_ptr = ::getgrgid (gid); |
| 1480 | if (group_info_ptr) |
| 1481 | { |
| 1482 | group_name.assign (group_info_ptr->gr_name); |
| 1483 | return group_name.c_str(); |
| 1484 | } |
| 1485 | } |
| 1486 | group_name.clear(); |
| 1487 | return NULL; |
| 1488 | } |
| 1489 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1490 | uint32_t |
| 1491 | Host::GetUserID () |
| 1492 | { |
| 1493 | return getuid(); |
| 1494 | } |
| 1495 | |
| 1496 | uint32_t |
| 1497 | Host::GetGroupID () |
| 1498 | { |
| 1499 | return getgid(); |
| 1500 | } |
| 1501 | |
| 1502 | uint32_t |
| 1503 | Host::GetEffectiveUserID () |
| 1504 | { |
| 1505 | return geteuid(); |
| 1506 | } |
| 1507 | |
| 1508 | uint32_t |
| 1509 | Host::GetEffectiveGroupID () |
| 1510 | { |
| 1511 | return getegid(); |
| 1512 | } |
| 1513 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1514 | #endif |
| 1515 | |
| 1516 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) // see macosx/Host.mm |
| 1517 | bool |
| 1518 | Host::GetOSBuildString (std::string &s) |
| 1519 | { |
| 1520 | s.clear(); |
| 1521 | return false; |
| 1522 | } |
| 1523 | |
| 1524 | bool |
| 1525 | Host::GetOSKernelDescription (std::string &s) |
| 1526 | { |
| 1527 | s.clear(); |
| 1528 | return false; |
| 1529 | } |
| 1530 | #endif |
| 1531 | |
Zachary Turner | 310035a | 2014-07-08 04:52:15 +0000 | [diff] [blame] | 1532 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) \ |
| 1533 | && !defined(__linux__) && !defined(_WIN32) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1534 | uint32_t |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1535 | Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1536 | { |
| 1537 | process_infos.Clear(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1538 | return process_infos.GetSize(); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1541 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1542 | Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1543 | { |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1544 | process_info.Clear(); |
| 1545 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1546 | } |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1547 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1548 | |
Matt Kopec | 085d6ce | 2013-05-31 22:00:07 +0000 | [diff] [blame] | 1549 | #if !defined(__linux__) |
| 1550 | bool |
| 1551 | Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) |
| 1552 | { |
| 1553 | return false; |
| 1554 | } |
| 1555 | #endif |
| 1556 | |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 1557 | lldb::TargetSP |
| 1558 | Host::GetDummyTarget (lldb_private::Debugger &debugger) |
| 1559 | { |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1560 | static TargetSP g_dummy_target_sp; |
Filipe Cabecinhas | b018345 | 2012-05-17 15:48:02 +0000 | [diff] [blame] | 1561 | |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1562 | // FIXME: Maybe the dummy target should be per-Debugger |
| 1563 | if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid()) |
| 1564 | { |
| 1565 | ArchSpec arch(Target::GetDefaultArchitecture()); |
| 1566 | if (!arch.IsValid()) |
| 1567 | arch = Host::GetArchitecture (); |
| 1568 | Error err = debugger.GetTargetList().CreateTarget(debugger, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1569 | NULL, |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1570 | arch.GetTriple().getTriple().c_str(), |
| 1571 | false, |
| 1572 | NULL, |
| 1573 | g_dummy_target_sp); |
| 1574 | } |
Filipe Cabecinhas | b018345 | 2012-05-17 15:48:02 +0000 | [diff] [blame] | 1575 | |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1576 | return g_dummy_target_sp; |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1579 | struct ShellInfo |
| 1580 | { |
| 1581 | ShellInfo () : |
| 1582 | process_reaped (false), |
| 1583 | can_delete (false), |
| 1584 | pid (LLDB_INVALID_PROCESS_ID), |
| 1585 | signo(-1), |
| 1586 | status(-1) |
| 1587 | { |
| 1588 | } |
| 1589 | |
| 1590 | lldb_private::Predicate<bool> process_reaped; |
| 1591 | lldb_private::Predicate<bool> can_delete; |
| 1592 | lldb::pid_t pid; |
| 1593 | int signo; |
| 1594 | int status; |
| 1595 | }; |
| 1596 | |
| 1597 | static bool |
| 1598 | MonitorShellCommand (void *callback_baton, |
| 1599 | lldb::pid_t pid, |
| 1600 | bool exited, // True if the process did exit |
| 1601 | int signo, // Zero for no signal |
| 1602 | int status) // Exit value of process if signal is zero |
| 1603 | { |
| 1604 | ShellInfo *shell_info = (ShellInfo *)callback_baton; |
| 1605 | shell_info->pid = pid; |
| 1606 | shell_info->signo = signo; |
| 1607 | shell_info->status = status; |
| 1608 | // Let the thread running Host::RunShellCommand() know that the process |
| 1609 | // exited and that ShellInfo has been filled in by broadcasting to it |
| 1610 | shell_info->process_reaped.SetValue(1, eBroadcastAlways); |
| 1611 | // Now wait for a handshake back from that thread running Host::RunShellCommand |
| 1612 | // so we know that we can delete shell_info_ptr |
| 1613 | shell_info->can_delete.WaitForValueEqualTo(true); |
| 1614 | // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete... |
| 1615 | usleep(1000); |
| 1616 | // Now delete the shell info that was passed into this function |
| 1617 | delete shell_info; |
| 1618 | return true; |
| 1619 | } |
| 1620 | |
| 1621 | Error |
| 1622 | Host::RunShellCommand (const char *command, |
| 1623 | const char *working_dir, |
| 1624 | int *status_ptr, |
| 1625 | int *signo_ptr, |
| 1626 | std::string *command_output_ptr, |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1627 | uint32_t timeout_sec, |
| 1628 | const char *shell) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1629 | { |
| 1630 | Error error; |
| 1631 | ProcessLaunchInfo launch_info; |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1632 | if (shell && shell[0]) |
| 1633 | { |
| 1634 | // Run the command in a shell |
| 1635 | launch_info.SetShell(shell); |
| 1636 | launch_info.GetArguments().AppendArgument(command); |
| 1637 | const bool localhost = true; |
| 1638 | const bool will_debug = false; |
| 1639 | const bool first_arg_is_full_shell_command = true; |
| 1640 | launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 1641 | localhost, |
| 1642 | will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 1643 | first_arg_is_full_shell_command, |
| 1644 | 0); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1645 | } |
| 1646 | else |
| 1647 | { |
| 1648 | // No shell, just run it |
| 1649 | Args args (command); |
| 1650 | const bool first_arg_is_executable = true; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 1651 | launch_info.SetArguments(args, first_arg_is_executable); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1652 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1653 | |
| 1654 | if (working_dir) |
| 1655 | launch_info.SetWorkingDirectory(working_dir); |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1656 | char output_file_path_buffer[PATH_MAX]; |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1657 | const char *output_file_path = NULL; |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1658 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1659 | if (command_output_ptr) |
| 1660 | { |
| 1661 | // Create a temporary file to get the stdout/stderr and redirect the |
| 1662 | // output of the command into this file. We will later read this file |
| 1663 | // if all goes well and fill the data into "command_output_ptr" |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1664 | FileSpec tmpdir_file_spec; |
| 1665 | if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
| 1666 | { |
| 1667 | tmpdir_file_spec.GetFilename().SetCString("lldb-shell-output.XXXXXX"); |
| 1668 | strncpy(output_file_path_buffer, tmpdir_file_spec.GetPath().c_str(), sizeof(output_file_path_buffer)); |
| 1669 | } |
| 1670 | else |
| 1671 | { |
| 1672 | strncpy(output_file_path_buffer, "/tmp/lldb-shell-output.XXXXXX", sizeof(output_file_path_buffer)); |
| 1673 | } |
| 1674 | |
| 1675 | output_file_path = ::mktemp(output_file_path_buffer); |
| 1676 | } |
| 1677 | |
| 1678 | launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); |
| 1679 | if (output_file_path) |
| 1680 | { |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1681 | launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1682 | launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1683 | } |
| 1684 | else |
| 1685 | { |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1686 | launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 1687 | launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true); |
| 1688 | } |
| 1689 | |
| 1690 | // The process monitor callback will delete the 'shell_info_ptr' below... |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 1691 | std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo()); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1692 | |
| 1693 | const bool monitor_signals = false; |
| 1694 | launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals); |
| 1695 | |
| 1696 | error = LaunchProcess (launch_info); |
| 1697 | const lldb::pid_t pid = launch_info.GetProcessID(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1698 | |
| 1699 | if (error.Success() && pid == LLDB_INVALID_PROCESS_ID) |
| 1700 | error.SetErrorString("failed to get process ID"); |
| 1701 | |
| 1702 | if (error.Success()) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1703 | { |
| 1704 | // The process successfully launched, so we can defer ownership of |
| 1705 | // "shell_info" to the MonitorShellCommand callback function that will |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 1706 | // get called when the process dies. We release the unique pointer as it |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1707 | // doesn't need to delete the ShellInfo anymore. |
| 1708 | ShellInfo *shell_info = shell_info_ap.release(); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1709 | TimeValue *timeout_ptr = nullptr; |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1710 | TimeValue timeout_time(TimeValue::Now()); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1711 | if (timeout_sec > 0) { |
| 1712 | timeout_time.OffsetWithSeconds(timeout_sec); |
| 1713 | timeout_ptr = &timeout_time; |
| 1714 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1715 | bool timed_out = false; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1716 | shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1717 | if (timed_out) |
| 1718 | { |
| 1719 | error.SetErrorString("timed out waiting for shell command to complete"); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1720 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 1721 | // Kill the process since it didn't complete within the timeout specified |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1722 | Kill (pid, SIGKILL); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1723 | // Wait for the monitor callback to get the message |
| 1724 | timeout_time = TimeValue::Now(); |
| 1725 | timeout_time.OffsetWithSeconds(1); |
| 1726 | timed_out = false; |
| 1727 | shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out); |
| 1728 | } |
| 1729 | else |
| 1730 | { |
| 1731 | if (status_ptr) |
| 1732 | *status_ptr = shell_info->status; |
| 1733 | |
| 1734 | if (signo_ptr) |
| 1735 | *signo_ptr = shell_info->signo; |
| 1736 | |
| 1737 | if (command_output_ptr) |
| 1738 | { |
| 1739 | command_output_ptr->clear(); |
| 1740 | FileSpec file_spec(output_file_path, File::eOpenOptionRead); |
| 1741 | uint64_t file_size = file_spec.GetByteSize(); |
| 1742 | if (file_size > 0) |
| 1743 | { |
| 1744 | if (file_size > command_output_ptr->max_size()) |
| 1745 | { |
| 1746 | error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string"); |
| 1747 | } |
| 1748 | else |
| 1749 | { |
| 1750 | command_output_ptr->resize(file_size); |
| 1751 | file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error); |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | shell_info->can_delete.SetValue(true, eBroadcastAlways); |
| 1757 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (output_file_path) |
| 1760 | ::unlink (output_file_path); |
| 1761 | // Handshake with the monitor thread, or just let it know in advance that |
| 1762 | // it can delete "shell_info" in case we timed out and were not able to kill |
| 1763 | // the process... |
| 1764 | return error; |
| 1765 | } |
| 1766 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1767 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1768 | // LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC |
| 1769 | // systems |
| 1770 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 1771 | #if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1772 | |
| 1773 | // this method needs to be visible to macosx/Host.cpp and |
| 1774 | // common/Host.cpp. |
| 1775 | |
| 1776 | short |
| 1777 | Host::GetPosixspawnFlags (ProcessLaunchInfo &launch_info) |
| 1778 | { |
| 1779 | short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK; |
| 1780 | |
| 1781 | #if defined (__APPLE__) |
| 1782 | if (launch_info.GetFlags().Test (eLaunchFlagExec)) |
| 1783 | flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag |
| 1784 | |
| 1785 | if (launch_info.GetFlags().Test (eLaunchFlagDebug)) |
| 1786 | flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag |
| 1787 | |
| 1788 | if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR)) |
| 1789 | flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag |
| 1790 | |
| 1791 | if (launch_info.GetLaunchInSeparateProcessGroup()) |
| 1792 | flags |= POSIX_SPAWN_SETPGROUP; |
| 1793 | |
| 1794 | #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT |
| 1795 | #if defined (__APPLE__) && (defined (__x86_64__) || defined (__i386__)) |
| 1796 | static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate; |
| 1797 | if (g_use_close_on_exec_flag == eLazyBoolCalculate) |
| 1798 | { |
| 1799 | g_use_close_on_exec_flag = eLazyBoolNo; |
| 1800 | |
| 1801 | uint32_t major, minor, update; |
| 1802 | if (Host::GetOSVersion(major, minor, update)) |
| 1803 | { |
| 1804 | // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier |
| 1805 | if (major > 10 || (major == 10 && minor > 7)) |
| 1806 | { |
| 1807 | // Only enable for 10.8 and later OS versions |
| 1808 | g_use_close_on_exec_flag = eLazyBoolYes; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | #else |
| 1813 | static LazyBool g_use_close_on_exec_flag = eLazyBoolYes; |
| 1814 | #endif |
| 1815 | // Close all files exception those with file actions if this is supported. |
| 1816 | if (g_use_close_on_exec_flag == eLazyBoolYes) |
| 1817 | flags |= POSIX_SPAWN_CLOEXEC_DEFAULT; |
| 1818 | #endif |
| 1819 | #endif // #if defined (__APPLE__) |
| 1820 | return flags; |
| 1821 | } |
| 1822 | |
| 1823 | Error |
| 1824 | Host::LaunchProcessPosixSpawn (const char *exe_path, ProcessLaunchInfo &launch_info, ::pid_t &pid) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1825 | { |
| 1826 | Error error; |
| 1827 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_PROCESS)); |
| 1828 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1829 | posix_spawnattr_t attr; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1830 | error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1831 | |
| 1832 | if (error.Fail() || log) |
| 1833 | error.PutToLog(log, "::posix_spawnattr_init ( &attr )"); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1834 | if (error.Fail()) |
| 1835 | return error; |
| 1836 | |
| 1837 | // Make a quick class that will cleanup the posix spawn attributes in case |
| 1838 | // we return in the middle of this function. |
| 1839 | lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy); |
| 1840 | |
| 1841 | sigset_t no_signals; |
| 1842 | sigset_t all_signals; |
| 1843 | sigemptyset (&no_signals); |
| 1844 | sigfillset (&all_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1845 | ::posix_spawnattr_setsigmask(&attr, &no_signals); |
Todd Fiala | 571768c | 2014-01-23 17:07:54 +0000 | [diff] [blame] | 1846 | #if defined (__linux__) || defined (__FreeBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1847 | ::posix_spawnattr_setsigdefault(&attr, &no_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1848 | #else |
| 1849 | ::posix_spawnattr_setsigdefault(&attr, &all_signals); |
| 1850 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1851 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1852 | short flags = GetPosixspawnFlags(launch_info); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1853 | |
| 1854 | error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1855 | if (error.Fail() || log) |
| 1856 | error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1857 | if (error.Fail()) |
| 1858 | return error; |
| 1859 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1860 | // posix_spawnattr_setbinpref_np appears to be an Apple extension per: |
| 1861 | // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/ |
| 1862 | #if defined (__APPLE__) && !defined (__arm__) |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1863 | |
| 1864 | // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell |
| 1865 | // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation |
| 1866 | // to do that job on OSX. |
| 1867 | |
| 1868 | if (launch_info.GetShell() == nullptr) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1869 | { |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1870 | // We don't need to do this for ARM, and we really shouldn't now that we |
| 1871 | // have multiple CPU subtypes and no posix_spawnattr call that allows us |
| 1872 | // to set which CPU subtype to launch... |
| 1873 | const ArchSpec &arch_spec = launch_info.GetArchitecture(); |
| 1874 | cpu_type_t cpu = arch_spec.GetMachOCPUType(); |
| 1875 | cpu_type_t sub = arch_spec.GetMachOCPUSubType(); |
| 1876 | if (cpu != 0 && |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 1877 | cpu != static_cast<cpu_type_t>(UINT32_MAX) && |
| 1878 | cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) && |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1879 | !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail |
| 1880 | { |
| 1881 | size_t ocount = 0; |
| 1882 | error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX); |
| 1883 | if (error.Fail() || log) |
| 1884 | error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %llu )", cpu, (uint64_t)ocount); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1885 | |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1886 | if (error.Fail() || ocount != 1) |
| 1887 | return error; |
| 1888 | } |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1891 | #endif |
| 1892 | |
| 1893 | const char *tmp_argv[2]; |
| 1894 | char * const *argv = (char * const*)launch_info.GetArguments().GetConstArgumentVector(); |
| 1895 | char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector(); |
| 1896 | if (argv == NULL) |
| 1897 | { |
| 1898 | // posix_spawn gets very unhappy if it doesn't have at least the program |
| 1899 | // name in argv[0]. One of the side affects I have noticed is the environment |
| 1900 | // variables don't make it into the child process if "argv == NULL"!!! |
| 1901 | tmp_argv[0] = exe_path; |
| 1902 | tmp_argv[1] = NULL; |
| 1903 | argv = (char * const*)tmp_argv; |
| 1904 | } |
| 1905 | |
| 1906 | #if !defined (__APPLE__) |
| 1907 | // manage the working directory |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1908 | char current_dir[PATH_MAX]; |
| 1909 | current_dir[0] = '\0'; |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1910 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1911 | |
| 1912 | const char *working_dir = launch_info.GetWorkingDirectory(); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1913 | if (working_dir) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1914 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1915 | #if defined (__APPLE__) |
| 1916 | // Set the working directory on this thread only |
| 1917 | if (__pthread_chdir (working_dir) < 0) { |
| 1918 | if (errno == ENOENT) { |
| 1919 | error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); |
| 1920 | } else if (errno == ENOTDIR) { |
| 1921 | error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir); |
| 1922 | } else { |
| 1923 | error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution."); |
| 1924 | } |
| 1925 | return error; |
| 1926 | } |
| 1927 | #else |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1928 | if (::getcwd(current_dir, sizeof(current_dir)) == NULL) |
| 1929 | { |
| 1930 | error.SetError(errno, eErrorTypePOSIX); |
| 1931 | error.LogIfError(log, "unable to save the current directory"); |
| 1932 | return error; |
| 1933 | } |
| 1934 | |
| 1935 | if (::chdir(working_dir) == -1) |
| 1936 | { |
| 1937 | error.SetError(errno, eErrorTypePOSIX); |
| 1938 | error.LogIfError(log, "unable to change working directory to %s", working_dir); |
| 1939 | return error; |
| 1940 | } |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1941 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1944 | const size_t num_file_actions = launch_info.GetNumFileActions (); |
| 1945 | if (num_file_actions > 0) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1946 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1947 | posix_spawn_file_actions_t file_actions; |
| 1948 | error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX); |
| 1949 | if (error.Fail() || log) |
| 1950 | error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )"); |
| 1951 | if (error.Fail()) |
| 1952 | return error; |
| 1953 | |
| 1954 | // Make a quick class that will cleanup the posix spawn attributes in case |
| 1955 | // we return in the middle of this function. |
| 1956 | lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy); |
| 1957 | |
| 1958 | for (size_t i=0; i<num_file_actions; ++i) |
| 1959 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1960 | const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1961 | if (launch_file_action) |
| 1962 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1963 | if (!AddPosixSpawnFileAction (&file_actions, |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1964 | launch_file_action, |
| 1965 | log, |
| 1966 | error)) |
| 1967 | return error; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | error.SetError (::posix_spawnp (&pid, |
| 1972 | exe_path, |
| 1973 | &file_actions, |
| 1974 | &attr, |
| 1975 | argv, |
| 1976 | envp), |
| 1977 | eErrorTypePOSIX); |
| 1978 | |
| 1979 | if (error.Fail() || log) |
| 1980 | { |
| 1981 | error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1982 | pid, exe_path, static_cast<void*>(&file_actions), |
| 1983 | static_cast<void*>(&attr), |
| 1984 | reinterpret_cast<const void*>(argv), |
| 1985 | reinterpret_cast<const void*>(envp)); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1986 | if (log) |
| 1987 | { |
| 1988 | for (int ii=0; argv[ii]; ++ii) |
| 1989 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | } |
| 1994 | else |
| 1995 | { |
| 1996 | error.SetError (::posix_spawnp (&pid, |
| 1997 | exe_path, |
| 1998 | NULL, |
| 1999 | &attr, |
| 2000 | argv, |
| 2001 | envp), |
| 2002 | eErrorTypePOSIX); |
| 2003 | |
| 2004 | if (error.Fail() || log) |
| 2005 | { |
| 2006 | error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = NULL, attr = %p, argv = %p, envp = %p )", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 2007 | pid, exe_path, static_cast<void*>(&attr), |
| 2008 | reinterpret_cast<const void*>(argv), |
| 2009 | reinterpret_cast<const void*>(envp)); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 2010 | if (log) |
| 2011 | { |
| 2012 | for (int ii=0; argv[ii]; ++ii) |
| 2013 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
| 2014 | } |
| 2015 | } |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 2018 | if (working_dir) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2019 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 2020 | #if defined (__APPLE__) |
| 2021 | // No more thread specific current working directory |
| 2022 | __pthread_fchdir (-1); |
| 2023 | #else |
| 2024 | if (::chdir(current_dir) == -1 && error.Success()) |
| 2025 | { |
| 2026 | error.SetError(errno, eErrorTypePOSIX); |
| 2027 | error.LogIfError(log, "unable to change current directory back to %s", |
| 2028 | current_dir); |
| 2029 | } |
| 2030 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | return error; |
| 2034 | } |
| 2035 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 2036 | bool Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error) |
| 2037 | { |
| 2038 | if (info == NULL) |
| 2039 | return false; |
| 2040 | |
| 2041 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
| 2042 | |
| 2043 | switch (info->GetAction()) |
| 2044 | { |
| 2045 | case FileAction::eFileActionNone: |
| 2046 | error.Clear(); |
| 2047 | break; |
| 2048 | |
| 2049 | case FileAction::eFileActionClose: |
| 2050 | if (info->GetFD() == -1) |
| 2051 | error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 2052 | else |
| 2053 | { |
| 2054 | error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX); |
| 2055 | if (log && (error.Fail() || log)) |
| 2056 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
| 2057 | static_cast<void *>(file_actions), info->GetFD()); |
| 2058 | } |
| 2059 | break; |
| 2060 | |
| 2061 | case FileAction::eFileActionDuplicate: |
| 2062 | if (info->GetFD() == -1) |
| 2063 | error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 2064 | else if (info->GetActionArgument() == -1) |
| 2065 | error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 2066 | else |
| 2067 | { |
| 2068 | error.SetError(::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()), |
| 2069 | eErrorTypePOSIX); |
| 2070 | if (log && (error.Fail() || log)) |
| 2071 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
| 2072 | static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument()); |
| 2073 | } |
| 2074 | break; |
| 2075 | |
| 2076 | case FileAction::eFileActionOpen: |
| 2077 | if (info->GetFD() == -1) |
| 2078 | error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 2079 | else |
| 2080 | { |
| 2081 | int oflag = info->GetActionArgument(); |
| 2082 | |
| 2083 | mode_t mode = 0; |
| 2084 | |
| 2085 | if (oflag & O_CREAT) |
| 2086 | mode = 0640; |
| 2087 | |
| 2088 | error.SetError( |
| 2089 | ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode), |
| 2090 | eErrorTypePOSIX); |
| 2091 | if (error.Fail() || log) |
| 2092 | error.PutToLog(log, "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 2093 | static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode); |
| 2094 | } |
| 2095 | break; |
| 2096 | } |
| 2097 | return error.Success(); |
| 2098 | } |
| 2099 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 2100 | #endif // LaunchProcedssPosixSpawn: Apple, Linux, FreeBSD and other GLIBC systems |
| 2101 | |
| 2102 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 2103 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__NetBSD__) |
| 2104 | // The functions below implement process launching via posix_spawn() for Linux, |
| 2105 | // FreeBSD and NetBSD. |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2106 | |
| 2107 | Error |
| 2108 | Host::LaunchProcess (ProcessLaunchInfo &launch_info) |
| 2109 | { |
| 2110 | Error error; |
| 2111 | char exe_path[PATH_MAX]; |
| 2112 | |
| 2113 | PlatformSP host_platform_sp (Platform::GetDefaultPlatform ()); |
| 2114 | |
| 2115 | const ArchSpec &arch_spec = launch_info.GetArchitecture(); |
| 2116 | |
| 2117 | FileSpec exe_spec(launch_info.GetExecutableFile()); |
| 2118 | |
| 2119 | FileSpec::FileType file_type = exe_spec.GetFileType(); |
| 2120 | if (file_type != FileSpec::eFileTypeRegular) |
| 2121 | { |
| 2122 | lldb::ModuleSP exe_module_sp; |
| 2123 | error = host_platform_sp->ResolveExecutable (exe_spec, |
| 2124 | arch_spec, |
| 2125 | exe_module_sp, |
| 2126 | NULL); |
| 2127 | |
| 2128 | if (error.Fail()) |
| 2129 | return error; |
| 2130 | |
| 2131 | if (exe_module_sp) |
| 2132 | exe_spec = exe_module_sp->GetFileSpec(); |
| 2133 | } |
| 2134 | |
| 2135 | if (exe_spec.Exists()) |
| 2136 | { |
| 2137 | exe_spec.GetPath (exe_path, sizeof(exe_path)); |
| 2138 | } |
| 2139 | else |
| 2140 | { |
| 2141 | launch_info.GetExecutableFile().GetPath (exe_path, sizeof(exe_path)); |
| 2142 | error.SetErrorStringWithFormat ("executable doesn't exist: '%s'", exe_path); |
| 2143 | return error; |
| 2144 | } |
| 2145 | |
| 2146 | assert(!launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY)); |
| 2147 | |
| 2148 | ::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 2149 | |
| 2150 | error = LaunchProcessPosixSpawn(exe_path, launch_info, pid); |
| 2151 | |
| 2152 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 2153 | { |
| 2154 | // If all went well, then set the process ID into the launch info |
| 2155 | launch_info.SetProcessID(pid); |
| 2156 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 2157 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2158 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2159 | // Make sure we reap any processes we spawn or we will have zombies. |
| 2160 | if (!launch_info.MonitorProcess()) |
| 2161 | { |
| 2162 | const bool monitor_signals = false; |
| 2163 | StartMonitoringChildProcess (Process::SetProcessExitStatus, |
| 2164 | NULL, |
| 2165 | pid, |
| 2166 | monitor_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 2167 | if (log) |
| 2168 | log->PutCString ("monitored child process with default Process::SetProcessExitStatus."); |
| 2169 | } |
| 2170 | else |
| 2171 | { |
| 2172 | if (log) |
| 2173 | log->PutCString ("monitored child process with user-specified process monitor."); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | else |
| 2177 | { |
| 2178 | // Invalid process ID, something didn't go well |
| 2179 | if (error.Success()) |
| 2180 | error.SetErrorString ("process launch failed for unknown reasons"); |
| 2181 | } |
| 2182 | return error; |
| 2183 | } |
| 2184 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 2185 | #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 2186 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2187 | #ifndef _WIN32 |
| 2188 | |
| 2189 | size_t |
| 2190 | Host::GetPageSize() |
| 2191 | { |
| 2192 | return ::getpagesize(); |
| 2193 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 2194 | |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2195 | uint32_t |
| 2196 | Host::GetNumberCPUS () |
| 2197 | { |
| 2198 | static uint32_t g_num_cores = UINT32_MAX; |
| 2199 | if (g_num_cores == UINT32_MAX) |
| 2200 | { |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 2201 | #if defined(__APPLE__) or defined (__linux__) or defined (__FreeBSD__) or defined (__FreeBSD_kernel__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2202 | |
| 2203 | g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2204 | |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2205 | #else |
| 2206 | |
| 2207 | // Assume POSIX support if a host specific case has not been supplied above |
| 2208 | g_num_cores = 0; |
| 2209 | int num_cores = 0; |
| 2210 | size_t num_cores_len = sizeof(num_cores); |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 2211 | #ifdef HW_AVAILCPU |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2212 | int mib[] = { CTL_HW, HW_AVAILCPU }; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 2213 | #else |
| 2214 | int mib[] = { CTL_HW, HW_NCPU }; |
| 2215 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2216 | |
| 2217 | /* get the number of CPUs from the system */ |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 2218 | if (sysctl(mib, llvm::array_lengthof(mib), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0)) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2219 | { |
| 2220 | g_num_cores = num_cores; |
| 2221 | } |
| 2222 | else |
| 2223 | { |
| 2224 | mib[1] = HW_NCPU; |
| 2225 | num_cores_len = sizeof(num_cores); |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 2226 | if (sysctl(mib, llvm::array_lengthof(mib), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0)) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2227 | { |
| 2228 | if (num_cores > 0) |
| 2229 | g_num_cores = num_cores; |
| 2230 | } |
| 2231 | } |
| 2232 | #endif |
| 2233 | } |
| 2234 | return g_num_cores; |
| 2235 | } |
| 2236 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2237 | void |
| 2238 | Host::Kill(lldb::pid_t pid, int signo) |
| 2239 | { |
| 2240 | ::kill(pid, signo); |
| 2241 | } |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 2242 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2243 | #endif |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 2244 | |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 2245 | #if !defined (__APPLE__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 2246 | bool |
Greg Clayton | 3b14763 | 2010-12-18 01:54:34 +0000 | [diff] [blame] | 2247 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 2248 | { |
| 2249 | return false; |
| 2250 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2251 | |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 2252 | void |
| 2253 | Host::SetCrashDescriptionWithFormat (const char *format, ...) |
| 2254 | { |
| 2255 | } |
| 2256 | |
| 2257 | void |
| 2258 | Host::SetCrashDescription (const char *description) |
| 2259 | { |
| 2260 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2261 | |
| 2262 | lldb::pid_t |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2263 | Host::LaunchApplication (const FileSpec &app_file_spec) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2264 | { |
| 2265 | return LLDB_INVALID_PROCESS_ID; |
| 2266 | } |
| 2267 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2268 | #endif |
| 2269 | |
| 2270 | |
Zachary Turner | e88be26 | 2014-08-09 01:29:07 +0000 | [diff] [blame] | 2271 | #if !defined(_WIN32) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2272 | Error |
| 2273 | Host::MakeDirectory (const char* path, uint32_t file_permissions) |
| 2274 | { |
| 2275 | Error error; |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2276 | if (path && path[0]) |
| 2277 | { |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2278 | if (::mkdir(path, file_permissions) != 0) |
| 2279 | { |
| 2280 | error.SetErrorToErrno(); |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2281 | switch (error.GetError()) |
| 2282 | { |
| 2283 | case ENOENT: |
| 2284 | { |
| 2285 | // Parent directory doesn't exist, so lets make it if we can |
| 2286 | FileSpec spec(path, false); |
| 2287 | if (spec.GetDirectory() && spec.GetFilename()) |
| 2288 | { |
| 2289 | // Make the parent directory and try again |
| 2290 | Error error2 = Host::MakeDirectory(spec.GetDirectory().GetCString(), file_permissions); |
| 2291 | if (error2.Success()) |
| 2292 | { |
| 2293 | // Try and make the directory again now that the parent directory was made successfully |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2294 | if (::mkdir(path, file_permissions) == 0) |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2295 | error.Clear(); |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2296 | else |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2297 | error.SetErrorToErrno(); |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | break; |
| 2302 | case EEXIST: |
| 2303 | { |
| 2304 | FileSpec path_spec(path, false); |
| 2305 | if (path_spec.IsDirectory()) |
| 2306 | error.Clear(); // It is a directory and it already exists |
| 2307 | } |
| 2308 | break; |
| 2309 | } |
| 2310 | } |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2311 | } |
| 2312 | else |
| 2313 | { |
| 2314 | error.SetErrorString("empty path"); |
| 2315 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2316 | return error; |
| 2317 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 2318 | |
| 2319 | Error |
| 2320 | Host::RemoveDirectory (const char* path, bool recurse) |
| 2321 | { |
| 2322 | Error error; |
| 2323 | if (path && path[0]) |
| 2324 | { |
| 2325 | if (recurse) |
| 2326 | { |
| 2327 | StreamString command; |
| 2328 | command.Printf("rm -rf \"%s\"", path); |
| 2329 | int status = ::system(command.GetString().c_str()); |
| 2330 | if (status != 0) |
| 2331 | error.SetError(status, eErrorTypeGeneric); |
| 2332 | } |
| 2333 | else |
| 2334 | { |
| 2335 | if (::rmdir(path) != 0) |
| 2336 | error.SetErrorToErrno(); |
| 2337 | } |
| 2338 | } |
| 2339 | else |
| 2340 | { |
| 2341 | error.SetErrorString("empty path"); |
| 2342 | } |
| 2343 | return error; |
| 2344 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2345 | |
| 2346 | Error |
| 2347 | Host::GetFilePermissions (const char* path, uint32_t &file_permissions) |
| 2348 | { |
| 2349 | Error error; |
| 2350 | struct stat file_stats; |
| 2351 | if (::stat (path, &file_stats) == 0) |
| 2352 | { |
| 2353 | // The bits in "st_mode" currently match the definitions |
| 2354 | // for the file mode bits in unix. |
| 2355 | file_permissions = file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 2356 | } |
| 2357 | else |
| 2358 | { |
| 2359 | error.SetErrorToErrno(); |
| 2360 | } |
| 2361 | return error; |
| 2362 | } |
| 2363 | |
| 2364 | Error |
| 2365 | Host::SetFilePermissions (const char* path, uint32_t file_permissions) |
| 2366 | { |
| 2367 | Error error; |
| 2368 | if (::chmod(path, file_permissions) != 0) |
| 2369 | error.SetErrorToErrno(); |
| 2370 | return error; |
| 2371 | } |
| 2372 | |
| 2373 | Error |
| 2374 | Host::Symlink (const char *src, const char *dst) |
| 2375 | { |
| 2376 | Error error; |
| 2377 | if (::symlink(dst, src) == -1) |
| 2378 | error.SetErrorToErrno(); |
| 2379 | return error; |
| 2380 | } |
| 2381 | |
| 2382 | Error |
| 2383 | Host::Unlink (const char *path) |
| 2384 | { |
| 2385 | Error error; |
| 2386 | if (::unlink(path) == -1) |
| 2387 | error.SetErrorToErrno(); |
| 2388 | return error; |
| 2389 | } |
| 2390 | |
| 2391 | Error |
| 2392 | Host::Readlink (const char *path, char *buf, size_t buf_len) |
| 2393 | { |
| 2394 | Error error; |
| 2395 | ssize_t count = ::readlink(path, buf, buf_len); |
| 2396 | if (count < 0) |
| 2397 | error.SetErrorToErrno(); |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 2398 | else if (static_cast<size_t>(count) < (buf_len-1)) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2399 | buf[count] = '\0'; // Success |
| 2400 | else |
| 2401 | error.SetErrorString("'buf' buffer is too small to contain link contents"); |
| 2402 | return error; |
| 2403 | } |
| 2404 | |
| 2405 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 2406 | #endif |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2407 | |
| 2408 | typedef std::map<lldb::user_id_t, lldb::FileSP> FDToFileMap; |
| 2409 | FDToFileMap& GetFDToFileMap() |
| 2410 | { |
| 2411 | static FDToFileMap g_fd2filemap; |
| 2412 | return g_fd2filemap; |
| 2413 | } |
| 2414 | |
| 2415 | lldb::user_id_t |
| 2416 | Host::OpenFile (const FileSpec& file_spec, |
| 2417 | uint32_t flags, |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2418 | uint32_t mode, |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2419 | Error &error) |
| 2420 | { |
| 2421 | std::string path (file_spec.GetPath()); |
| 2422 | if (path.empty()) |
| 2423 | { |
| 2424 | error.SetErrorString("empty path"); |
| 2425 | return UINT64_MAX; |
| 2426 | } |
| 2427 | FileSP file_sp(new File()); |
| 2428 | error = file_sp->Open(path.c_str(),flags,mode); |
| 2429 | if (file_sp->IsValid() == false) |
| 2430 | return UINT64_MAX; |
| 2431 | lldb::user_id_t fd = file_sp->GetDescriptor(); |
| 2432 | GetFDToFileMap()[fd] = file_sp; |
| 2433 | return fd; |
| 2434 | } |
| 2435 | |
| 2436 | bool |
| 2437 | Host::CloseFile (lldb::user_id_t fd, Error &error) |
| 2438 | { |
| 2439 | if (fd == UINT64_MAX) |
| 2440 | { |
| 2441 | error.SetErrorString ("invalid file descriptor"); |
| 2442 | return false; |
| 2443 | } |
| 2444 | FDToFileMap& file_map = GetFDToFileMap(); |
| 2445 | FDToFileMap::iterator pos = file_map.find(fd); |
| 2446 | if (pos == file_map.end()) |
| 2447 | { |
| 2448 | error.SetErrorStringWithFormat ("invalid host file descriptor %" PRIu64, fd); |
| 2449 | return false; |
| 2450 | } |
| 2451 | FileSP file_sp = pos->second; |
| 2452 | if (!file_sp) |
| 2453 | { |
| 2454 | error.SetErrorString ("invalid host backing file"); |
| 2455 | return false; |
| 2456 | } |
| 2457 | error = file_sp->Close(); |
| 2458 | file_map.erase(pos); |
| 2459 | return error.Success(); |
| 2460 | } |
| 2461 | |
| 2462 | uint64_t |
| 2463 | Host::WriteFile (lldb::user_id_t fd, uint64_t offset, const void* src, uint64_t src_len, Error &error) |
| 2464 | { |
| 2465 | if (fd == UINT64_MAX) |
| 2466 | { |
| 2467 | error.SetErrorString ("invalid file descriptor"); |
| 2468 | return UINT64_MAX; |
| 2469 | } |
| 2470 | FDToFileMap& file_map = GetFDToFileMap(); |
| 2471 | FDToFileMap::iterator pos = file_map.find(fd); |
| 2472 | if (pos == file_map.end()) |
| 2473 | { |
| 2474 | error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64 , fd); |
| 2475 | return false; |
| 2476 | } |
| 2477 | FileSP file_sp = pos->second; |
| 2478 | if (!file_sp) |
| 2479 | { |
| 2480 | error.SetErrorString ("invalid host backing file"); |
| 2481 | return UINT64_MAX; |
| 2482 | } |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 2483 | if (static_cast<uint64_t>(file_sp->SeekFromStart(offset, &error)) != offset || |
| 2484 | error.Fail()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2485 | return UINT64_MAX; |
| 2486 | size_t bytes_written = src_len; |
| 2487 | error = file_sp->Write(src, bytes_written); |
| 2488 | if (error.Fail()) |
| 2489 | return UINT64_MAX; |
| 2490 | return bytes_written; |
| 2491 | } |
| 2492 | |
| 2493 | uint64_t |
| 2494 | Host::ReadFile (lldb::user_id_t fd, uint64_t offset, void* dst, uint64_t dst_len, Error &error) |
| 2495 | { |
| 2496 | if (fd == UINT64_MAX) |
| 2497 | { |
| 2498 | error.SetErrorString ("invalid file descriptor"); |
| 2499 | return UINT64_MAX; |
| 2500 | } |
| 2501 | FDToFileMap& file_map = GetFDToFileMap(); |
| 2502 | FDToFileMap::iterator pos = file_map.find(fd); |
| 2503 | if (pos == file_map.end()) |
| 2504 | { |
| 2505 | error.SetErrorStringWithFormat ("invalid host file descriptor %" PRIu64, fd); |
| 2506 | return false; |
| 2507 | } |
| 2508 | FileSP file_sp = pos->second; |
| 2509 | if (!file_sp) |
| 2510 | { |
| 2511 | error.SetErrorString ("invalid host backing file"); |
| 2512 | return UINT64_MAX; |
| 2513 | } |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 2514 | if (static_cast<uint64_t>(file_sp->SeekFromStart(offset, &error)) != offset || |
| 2515 | error.Fail()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2516 | return UINT64_MAX; |
| 2517 | size_t bytes_read = dst_len; |
| 2518 | error = file_sp->Read(dst ,bytes_read); |
| 2519 | if (error.Fail()) |
| 2520 | return UINT64_MAX; |
| 2521 | return bytes_read; |
| 2522 | } |
| 2523 | |
| 2524 | lldb::user_id_t |
| 2525 | Host::GetFileSize (const FileSpec& file_spec) |
| 2526 | { |
| 2527 | return file_spec.GetByteSize(); |
| 2528 | } |
| 2529 | |
| 2530 | bool |
| 2531 | Host::GetFileExists (const FileSpec& file_spec) |
| 2532 | { |
| 2533 | return file_spec.Exists(); |
| 2534 | } |
| 2535 | |
| 2536 | bool |
| 2537 | Host::CalculateMD5 (const FileSpec& file_spec, |
| 2538 | uint64_t &low, |
| 2539 | uint64_t &high) |
| 2540 | { |
| 2541 | #if defined (__APPLE__) |
| 2542 | StreamString md5_cmd_line; |
| 2543 | md5_cmd_line.Printf("md5 -q '%s'", file_spec.GetPath().c_str()); |
| 2544 | std::string hash_string; |
| 2545 | Error err = Host::RunShellCommand(md5_cmd_line.GetData(), NULL, NULL, NULL, &hash_string, 60); |
| 2546 | if (err.Fail()) |
| 2547 | return false; |
| 2548 | // a correctly formed MD5 is 16-bytes, that is 32 hex digits |
| 2549 | // if the output is any other length it is probably wrong |
| 2550 | if (hash_string.size() != 32) |
| 2551 | return false; |
| 2552 | std::string part1(hash_string,0,16); |
| 2553 | std::string part2(hash_string,16); |
| 2554 | const char* part1_cstr = part1.c_str(); |
| 2555 | const char* part2_cstr = part2.c_str(); |
| 2556 | high = ::strtoull(part1_cstr, NULL, 16); |
| 2557 | low = ::strtoull(part2_cstr, NULL, 16); |
| 2558 | return true; |
| 2559 | #else |
| 2560 | // your own MD5 implementation here |
| 2561 | return false; |
| 2562 | #endif |
| 2563 | } |