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