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