Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1 | //===-- ProcessMonitor.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 | |
| 10 | // C Includes |
| 11 | #include <errno.h> |
| 12 | #include <poll.h> |
| 13 | #include <string.h> |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #include <signal.h> |
| 17 | #include <sys/ptrace.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | |
| 22 | // C++ Includes |
| 23 | // Other libraries and framework includes |
| 24 | #include "lldb/Core/Error.h" |
| 25 | #include "lldb/Core/RegisterValue.h" |
| 26 | #include "lldb/Core/Scalar.h" |
| 27 | #include "lldb/Host/Host.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 28 | #include "lldb/Host/ThreadLauncher.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Thread.h" |
| 30 | #include "lldb/Target/RegisterContext.h" |
| 31 | #include "lldb/Utility/PseudoTerminal.h" |
| 32 | |
| 33 | |
| 34 | #include "POSIXThread.h" |
| 35 | #include "ProcessFreeBSD.h" |
| 36 | #include "ProcessPOSIXLog.h" |
| 37 | #include "ProcessMonitor.h" |
| 38 | |
| 39 | extern "C" { |
| 40 | extern char ** environ; |
| 41 | } |
| 42 | |
| 43 | using namespace lldb; |
| 44 | using namespace lldb_private; |
| 45 | |
| 46 | // We disable the tracing of ptrace calls for integration builds to |
| 47 | // avoid the additional indirection and checks. |
| 48 | #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
| 49 | // Wrapper for ptrace to catch errors and log calls. |
| 50 | |
| 51 | const char * |
| 52 | Get_PT_IO_OP(int op) |
| 53 | { |
| 54 | switch (op) { |
| 55 | case PIOD_READ_D: return "READ_D"; |
| 56 | case PIOD_WRITE_D: return "WRITE_D"; |
| 57 | case PIOD_READ_I: return "READ_I"; |
| 58 | case PIOD_WRITE_I: return "WRITE_I"; |
| 59 | default: return "Unknown op"; |
| 60 | } |
| 61 | } |
| 62 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 63 | // Wrapper for ptrace to catch errors and log calls. |
| 64 | // Note that ptrace sets errno on error because -1 is reserved as a valid result. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 65 | extern long |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame] | 66 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 67 | const char* reqName, const char* file, int line) |
| 68 | { |
| 69 | long int result; |
| 70 | |
Ashok Thirumurthi | 0118635 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 71 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 72 | |
| 73 | if (log) { |
Sylvestre Ledru | 779f921 | 2013-10-31 23:55:19 +0000 | [diff] [blame] | 74 | log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d", |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 75 | reqName, pid, addr, data, file, line); |
| 76 | if (req == PT_IO) { |
| 77 | struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr; |
| 78 | |
Sylvestre Ledru | 779f921 | 2013-10-31 23:55:19 +0000 | [diff] [blame] | 79 | log->Printf("PT_IO: op=%s offs=%zx size=%zu", |
Ed Maste | a708a36 | 2013-06-25 14:47:45 +0000 | [diff] [blame] | 80 | Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | //PtraceDisplayBytes(req, data); |
| 85 | |
| 86 | errno = 0; |
Matt Kopec | c6672c8 | 2013-03-15 20:00:39 +0000 | [diff] [blame] | 87 | result = ptrace(req, pid, (caddr_t) addr, data); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 88 | |
| 89 | //PtraceDisplayBytes(req, data); |
| 90 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 91 | if (log && errno != 0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 92 | { |
| 93 | const char* str; |
| 94 | switch (errno) |
| 95 | { |
| 96 | case ESRCH: str = "ESRCH"; break; |
| 97 | case EINVAL: str = "EINVAL"; break; |
| 98 | case EBUSY: str = "EBUSY"; break; |
| 99 | case EPERM: str = "EPERM"; break; |
| 100 | default: str = "<unknown>"; |
| 101 | } |
| 102 | log->Printf("ptrace() failed; errno=%d (%s)", errno, str); |
| 103 | } |
| 104 | |
| 105 | if (log) { |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 106 | #ifdef __amd64__ |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 107 | if (req == PT_GETREGS) { |
| 108 | struct reg *r = (struct reg *) addr; |
| 109 | |
| 110 | log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip); |
| 111 | log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp); |
| 112 | log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp); |
| 113 | log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax); |
| 114 | } |
Ed Maste | 7d4c0d5b | 2013-07-22 20:51:08 +0000 | [diff] [blame] | 115 | #endif |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 116 | if (req == PT_GETDBREGS || req == PT_SETDBREGS) { |
| 117 | struct dbreg *r = (struct dbreg *) addr; |
| 118 | char setget = (req == PT_GETDBREGS) ? 'G' : 'S'; |
| 119 | |
| 120 | for (int i = 0; i <= 7; i++) |
| 121 | log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]); |
| 122 | } |
| 123 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 124 | |
| 125 | return result; |
| 126 | } |
| 127 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 128 | // Wrapper for ptrace when logging is not required. |
| 129 | // Sets errno to 0 prior to calling ptrace. |
| 130 | extern long |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame] | 131 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 132 | { |
| 133 | long result = 0; |
| 134 | errno = 0; |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame] | 135 | result = ptrace(req, pid, (caddr_t)addr, data); |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 136 | return result; |
| 137 | } |
| 138 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 139 | #define PTRACE(req, pid, addr, data) \ |
| 140 | PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__) |
| 141 | #else |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 142 | PtraceWrapper((req), (pid), (addr), (data)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
| 145 | //------------------------------------------------------------------------------ |
| 146 | // Static implementations of ProcessMonitor::ReadMemory and |
| 147 | // ProcessMonitor::WriteMemory. This enables mutual recursion between these |
| 148 | // functions without needed to go thru the thread funnel. |
| 149 | |
| 150 | static size_t |
| 151 | DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size, |
| 152 | Error &error) |
| 153 | { |
| 154 | struct ptrace_io_desc pi_desc; |
| 155 | |
| 156 | pi_desc.piod_op = PIOD_READ_D; |
| 157 | pi_desc.piod_offs = (void *)vm_addr; |
| 158 | pi_desc.piod_addr = buf; |
| 159 | pi_desc.piod_len = size; |
| 160 | |
| 161 | if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) |
| 162 | error.SetErrorToErrno(); |
| 163 | return pi_desc.piod_len; |
| 164 | } |
| 165 | |
| 166 | static size_t |
| 167 | DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf, |
| 168 | size_t size, Error &error) |
| 169 | { |
| 170 | struct ptrace_io_desc pi_desc; |
| 171 | |
| 172 | pi_desc.piod_op = PIOD_WRITE_D; |
| 173 | pi_desc.piod_offs = (void *)vm_addr; |
| 174 | pi_desc.piod_addr = (void *)buf; |
| 175 | pi_desc.piod_len = size; |
| 176 | |
| 177 | if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) |
| 178 | error.SetErrorToErrno(); |
| 179 | return pi_desc.piod_len; |
| 180 | } |
| 181 | |
| 182 | // Simple helper function to ensure flags are enabled on the given file |
| 183 | // descriptor. |
| 184 | static bool |
| 185 | EnsureFDFlags(int fd, int flags, Error &error) |
| 186 | { |
| 187 | int status; |
| 188 | |
| 189 | if ((status = fcntl(fd, F_GETFL)) == -1) |
| 190 | { |
| 191 | error.SetErrorToErrno(); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 196 | { |
| 197 | error.SetErrorToErrno(); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | //------------------------------------------------------------------------------ |
| 205 | /// @class Operation |
| 206 | /// @brief Represents a ProcessMonitor operation. |
| 207 | /// |
| 208 | /// Under FreeBSD, it is not possible to ptrace() from any other thread but the |
| 209 | /// one that spawned or attached to the process from the start. Therefore, when |
| 210 | /// a ProcessMonitor is asked to deliver or change the state of an inferior |
| 211 | /// process the operation must be "funneled" to a specific thread to perform the |
| 212 | /// task. The Operation class provides an abstract base for all services the |
| 213 | /// ProcessMonitor must perform via the single virtual function Execute, thus |
| 214 | /// encapsulating the code that needs to run in the privileged context. |
| 215 | class Operation |
| 216 | { |
| 217 | public: |
Ed Maste | 5a9a626 | 2013-06-24 14:55:03 +0000 | [diff] [blame] | 218 | virtual ~Operation() {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 219 | virtual void Execute(ProcessMonitor *monitor) = 0; |
| 220 | }; |
| 221 | |
| 222 | //------------------------------------------------------------------------------ |
| 223 | /// @class ReadOperation |
| 224 | /// @brief Implements ProcessMonitor::ReadMemory. |
| 225 | class ReadOperation : public Operation |
| 226 | { |
| 227 | public: |
| 228 | ReadOperation(lldb::addr_t addr, void *buff, size_t size, |
| 229 | Error &error, size_t &result) |
| 230 | : m_addr(addr), m_buff(buff), m_size(size), |
| 231 | m_error(error), m_result(result) |
| 232 | { } |
| 233 | |
| 234 | void Execute(ProcessMonitor *monitor); |
| 235 | |
| 236 | private: |
| 237 | lldb::addr_t m_addr; |
| 238 | void *m_buff; |
| 239 | size_t m_size; |
| 240 | Error &m_error; |
| 241 | size_t &m_result; |
| 242 | }; |
| 243 | |
| 244 | void |
| 245 | ReadOperation::Execute(ProcessMonitor *monitor) |
| 246 | { |
| 247 | lldb::pid_t pid = monitor->GetPID(); |
| 248 | |
| 249 | m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error); |
| 250 | } |
| 251 | |
| 252 | //------------------------------------------------------------------------------ |
Ed Maste | a56115f | 2013-07-17 14:30:26 +0000 | [diff] [blame] | 253 | /// @class WriteOperation |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 254 | /// @brief Implements ProcessMonitor::WriteMemory. |
| 255 | class WriteOperation : public Operation |
| 256 | { |
| 257 | public: |
| 258 | WriteOperation(lldb::addr_t addr, const void *buff, size_t size, |
| 259 | Error &error, size_t &result) |
| 260 | : m_addr(addr), m_buff(buff), m_size(size), |
| 261 | m_error(error), m_result(result) |
| 262 | { } |
| 263 | |
| 264 | void Execute(ProcessMonitor *monitor); |
| 265 | |
| 266 | private: |
| 267 | lldb::addr_t m_addr; |
| 268 | const void *m_buff; |
| 269 | size_t m_size; |
| 270 | Error &m_error; |
| 271 | size_t &m_result; |
| 272 | }; |
| 273 | |
| 274 | void |
| 275 | WriteOperation::Execute(ProcessMonitor *monitor) |
| 276 | { |
| 277 | lldb::pid_t pid = monitor->GetPID(); |
| 278 | |
| 279 | m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error); |
| 280 | } |
| 281 | |
| 282 | //------------------------------------------------------------------------------ |
| 283 | /// @class ReadRegOperation |
| 284 | /// @brief Implements ProcessMonitor::ReadRegisterValue. |
| 285 | class ReadRegOperation : public Operation |
| 286 | { |
| 287 | public: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 288 | ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size, |
| 289 | RegisterValue &value, bool &result) |
| 290 | : m_tid(tid), m_offset(offset), m_size(size), |
| 291 | m_value(value), m_result(result) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 292 | { } |
| 293 | |
| 294 | void Execute(ProcessMonitor *monitor); |
| 295 | |
| 296 | private: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 297 | lldb::tid_t m_tid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 298 | unsigned m_offset; |
| 299 | unsigned m_size; |
| 300 | RegisterValue &m_value; |
| 301 | bool &m_result; |
| 302 | }; |
| 303 | |
| 304 | void |
| 305 | ReadRegOperation::Execute(ProcessMonitor *monitor) |
| 306 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 307 | struct reg regs; |
| 308 | int rc; |
| 309 | |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 310 | if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)®s, 0)) < 0) { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 311 | m_result = false; |
| 312 | } else { |
| 313 | if (m_size == sizeof(uintptr_t)) |
| 314 | m_value = *(uintptr_t *)(((caddr_t)®s) + m_offset); |
| 315 | else |
| 316 | memcpy(&m_value, (((caddr_t)®s) + m_offset), m_size); |
| 317 | m_result = true; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | //------------------------------------------------------------------------------ |
| 322 | /// @class WriteRegOperation |
| 323 | /// @brief Implements ProcessMonitor::WriteRegisterValue. |
| 324 | class WriteRegOperation : public Operation |
| 325 | { |
| 326 | public: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 327 | WriteRegOperation(lldb::tid_t tid, unsigned offset, |
| 328 | const RegisterValue &value, bool &result) |
| 329 | : m_tid(tid), m_offset(offset), |
| 330 | m_value(value), m_result(result) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 331 | { } |
| 332 | |
| 333 | void Execute(ProcessMonitor *monitor); |
| 334 | |
| 335 | private: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 336 | lldb::tid_t m_tid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 337 | unsigned m_offset; |
| 338 | const RegisterValue &m_value; |
| 339 | bool &m_result; |
| 340 | }; |
| 341 | |
| 342 | void |
| 343 | WriteRegOperation::Execute(ProcessMonitor *monitor) |
| 344 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 345 | struct reg regs; |
| 346 | |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 347 | if (PTRACE(PT_GETREGS, m_tid, (caddr_t)®s, 0) < 0) { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 348 | m_result = false; |
| 349 | return; |
| 350 | } |
| 351 | *(uintptr_t *)(((caddr_t)®s) + m_offset) = (uintptr_t)m_value.GetAsUInt64(); |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 352 | if (PTRACE(PT_SETREGS, m_tid, (caddr_t)®s, 0) < 0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 353 | m_result = false; |
| 354 | else |
| 355 | m_result = true; |
| 356 | } |
| 357 | |
| 358 | //------------------------------------------------------------------------------ |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 359 | /// @class ReadDebugRegOperation |
| 360 | /// @brief Implements ProcessMonitor::ReadDebugRegisterValue. |
| 361 | class ReadDebugRegOperation : public Operation |
| 362 | { |
| 363 | public: |
| 364 | ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size, |
| 365 | RegisterValue &value, bool &result) |
| 366 | : m_tid(tid), m_offset(offset), m_size(size), |
| 367 | m_value(value), m_result(result) |
| 368 | { } |
| 369 | |
| 370 | void Execute(ProcessMonitor *monitor); |
| 371 | |
| 372 | private: |
| 373 | lldb::tid_t m_tid; |
| 374 | unsigned m_offset; |
| 375 | unsigned m_size; |
| 376 | RegisterValue &m_value; |
| 377 | bool &m_result; |
| 378 | }; |
| 379 | |
| 380 | void |
| 381 | ReadDebugRegOperation::Execute(ProcessMonitor *monitor) |
| 382 | { |
| 383 | struct dbreg regs; |
| 384 | int rc; |
| 385 | |
| 386 | if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)®s, 0)) < 0) { |
| 387 | m_result = false; |
| 388 | } else { |
| 389 | if (m_size == sizeof(uintptr_t)) |
| 390 | m_value = *(uintptr_t *)(((caddr_t)®s) + m_offset); |
| 391 | else |
| 392 | memcpy(&m_value, (((caddr_t)®s) + m_offset), m_size); |
| 393 | m_result = true; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | //------------------------------------------------------------------------------ |
| 398 | /// @class WriteDebugRegOperation |
| 399 | /// @brief Implements ProcessMonitor::WriteDebugRegisterValue. |
| 400 | class WriteDebugRegOperation : public Operation |
| 401 | { |
| 402 | public: |
| 403 | WriteDebugRegOperation(lldb::tid_t tid, unsigned offset, |
| 404 | const RegisterValue &value, bool &result) |
| 405 | : m_tid(tid), m_offset(offset), |
| 406 | m_value(value), m_result(result) |
| 407 | { } |
| 408 | |
| 409 | void Execute(ProcessMonitor *monitor); |
| 410 | |
| 411 | private: |
| 412 | lldb::tid_t m_tid; |
| 413 | unsigned m_offset; |
| 414 | const RegisterValue &m_value; |
| 415 | bool &m_result; |
| 416 | }; |
| 417 | |
| 418 | void |
| 419 | WriteDebugRegOperation::Execute(ProcessMonitor *monitor) |
| 420 | { |
| 421 | struct dbreg regs; |
| 422 | |
| 423 | if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)®s, 0) < 0) { |
| 424 | m_result = false; |
| 425 | return; |
| 426 | } |
| 427 | *(uintptr_t *)(((caddr_t)®s) + m_offset) = (uintptr_t)m_value.GetAsUInt64(); |
| 428 | if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)®s, 0) < 0) |
| 429 | m_result = false; |
| 430 | else |
| 431 | m_result = true; |
| 432 | } |
| 433 | |
| 434 | //------------------------------------------------------------------------------ |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 435 | /// @class ReadGPROperation |
| 436 | /// @brief Implements ProcessMonitor::ReadGPR. |
| 437 | class ReadGPROperation : public Operation |
| 438 | { |
| 439 | public: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 440 | ReadGPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 441 | : m_tid(tid), m_buf(buf), m_result(result) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 442 | { } |
| 443 | |
| 444 | void Execute(ProcessMonitor *monitor); |
| 445 | |
| 446 | private: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 447 | lldb::tid_t m_tid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 448 | void *m_buf; |
| 449 | bool &m_result; |
| 450 | }; |
| 451 | |
| 452 | void |
| 453 | ReadGPROperation::Execute(ProcessMonitor *monitor) |
| 454 | { |
| 455 | int rc; |
| 456 | |
| 457 | errno = 0; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 458 | rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 459 | if (errno != 0) |
| 460 | m_result = false; |
| 461 | else |
| 462 | m_result = true; |
| 463 | } |
| 464 | |
| 465 | //------------------------------------------------------------------------------ |
| 466 | /// @class ReadFPROperation |
| 467 | /// @brief Implements ProcessMonitor::ReadFPR. |
| 468 | class ReadFPROperation : public Operation |
| 469 | { |
| 470 | public: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 471 | ReadFPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 472 | : m_tid(tid), m_buf(buf), m_result(result) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 473 | { } |
| 474 | |
| 475 | void Execute(ProcessMonitor *monitor); |
| 476 | |
| 477 | private: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 478 | lldb::tid_t m_tid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 479 | void *m_buf; |
| 480 | bool &m_result; |
| 481 | }; |
| 482 | |
| 483 | void |
| 484 | ReadFPROperation::Execute(ProcessMonitor *monitor) |
| 485 | { |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 486 | if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 487 | m_result = false; |
| 488 | else |
| 489 | m_result = true; |
| 490 | } |
| 491 | |
| 492 | //------------------------------------------------------------------------------ |
| 493 | /// @class WriteGPROperation |
| 494 | /// @brief Implements ProcessMonitor::WriteGPR. |
| 495 | class WriteGPROperation : public Operation |
| 496 | { |
| 497 | public: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 498 | WriteGPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 499 | : m_tid(tid), m_buf(buf), m_result(result) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 500 | { } |
| 501 | |
| 502 | void Execute(ProcessMonitor *monitor); |
| 503 | |
| 504 | private: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 505 | lldb::tid_t m_tid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 506 | void *m_buf; |
| 507 | bool &m_result; |
| 508 | }; |
| 509 | |
| 510 | void |
| 511 | WriteGPROperation::Execute(ProcessMonitor *monitor) |
| 512 | { |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 513 | if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 514 | m_result = false; |
| 515 | else |
| 516 | m_result = true; |
| 517 | } |
| 518 | |
| 519 | //------------------------------------------------------------------------------ |
| 520 | /// @class WriteFPROperation |
| 521 | /// @brief Implements ProcessMonitor::WriteFPR. |
| 522 | class WriteFPROperation : public Operation |
| 523 | { |
| 524 | public: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 525 | WriteFPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 526 | : m_tid(tid), m_buf(buf), m_result(result) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 527 | { } |
| 528 | |
| 529 | void Execute(ProcessMonitor *monitor); |
| 530 | |
| 531 | private: |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 532 | lldb::tid_t m_tid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 533 | void *m_buf; |
| 534 | bool &m_result; |
| 535 | }; |
| 536 | |
| 537 | void |
| 538 | WriteFPROperation::Execute(ProcessMonitor *monitor) |
| 539 | { |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 540 | if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 541 | m_result = false; |
| 542 | else |
| 543 | m_result = true; |
| 544 | } |
| 545 | |
| 546 | //------------------------------------------------------------------------------ |
| 547 | /// @class ResumeOperation |
| 548 | /// @brief Implements ProcessMonitor::Resume. |
| 549 | class ResumeOperation : public Operation |
| 550 | { |
| 551 | public: |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 552 | ResumeOperation(uint32_t signo, bool &result) : |
| 553 | m_signo(signo), m_result(result) { } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 554 | |
| 555 | void Execute(ProcessMonitor *monitor); |
| 556 | |
| 557 | private: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 558 | uint32_t m_signo; |
| 559 | bool &m_result; |
| 560 | }; |
| 561 | |
| 562 | void |
| 563 | ResumeOperation::Execute(ProcessMonitor *monitor) |
| 564 | { |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 565 | lldb::pid_t pid = monitor->GetPID(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 566 | int data = 0; |
| 567 | |
| 568 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 569 | data = m_signo; |
| 570 | |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 571 | if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 572 | { |
| 573 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 574 | |
| 575 | if (log) |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 576 | log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 577 | m_result = false; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 578 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 579 | else |
| 580 | m_result = true; |
| 581 | } |
| 582 | |
| 583 | //------------------------------------------------------------------------------ |
Ed Maste | 428a678 | 2013-06-24 15:04:47 +0000 | [diff] [blame] | 584 | /// @class SingleStepOperation |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 585 | /// @brief Implements ProcessMonitor::SingleStep. |
| 586 | class SingleStepOperation : public Operation |
| 587 | { |
| 588 | public: |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 589 | SingleStepOperation(uint32_t signo, bool &result) |
| 590 | : m_signo(signo), m_result(result) { } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 591 | |
| 592 | void Execute(ProcessMonitor *monitor); |
| 593 | |
| 594 | private: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 595 | uint32_t m_signo; |
| 596 | bool &m_result; |
| 597 | }; |
| 598 | |
| 599 | void |
| 600 | SingleStepOperation::Execute(ProcessMonitor *monitor) |
| 601 | { |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 602 | lldb::pid_t pid = monitor->GetPID(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 603 | int data = 0; |
| 604 | |
| 605 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 606 | data = m_signo; |
| 607 | |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 608 | if (PTRACE(PT_STEP, pid, NULL, data)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 609 | m_result = false; |
| 610 | else |
| 611 | m_result = true; |
| 612 | } |
| 613 | |
| 614 | //------------------------------------------------------------------------------ |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 615 | /// @class LwpInfoOperation |
| 616 | /// @brief Implements ProcessMonitor::GetLwpInfo. |
| 617 | class LwpInfoOperation : public Operation |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 618 | { |
| 619 | public: |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 620 | LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err) |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 621 | : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 622 | |
| 623 | void Execute(ProcessMonitor *monitor); |
| 624 | |
| 625 | private: |
| 626 | lldb::tid_t m_tid; |
| 627 | void *m_info; |
| 628 | bool &m_result; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 629 | int &m_err; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 630 | }; |
| 631 | |
| 632 | void |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 633 | LwpInfoOperation::Execute(ProcessMonitor *monitor) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 634 | { |
| 635 | struct ptrace_lwpinfo plwp; |
| 636 | |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 637 | if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 638 | m_result = false; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 639 | m_err = errno; |
| 640 | } else { |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 641 | memcpy(m_info, &plwp, sizeof(plwp)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 642 | m_result = true; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | //------------------------------------------------------------------------------ |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 647 | /// @class ThreadSuspendOperation |
| 648 | /// @brief Implements ProcessMonitor::ThreadSuspend. |
| 649 | class ThreadSuspendOperation : public Operation |
| 650 | { |
| 651 | public: |
| 652 | ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result) |
| 653 | : m_tid(tid), m_suspend(suspend), m_result(result) { } |
| 654 | |
| 655 | void Execute(ProcessMonitor *monitor); |
| 656 | |
| 657 | private: |
| 658 | lldb::tid_t m_tid; |
| 659 | bool m_suspend; |
| 660 | bool &m_result; |
| 661 | } ; |
| 662 | |
| 663 | void |
| 664 | ThreadSuspendOperation::Execute(ProcessMonitor *monitor) |
| 665 | { |
| 666 | m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0); |
| 667 | } |
| 668 | |
| 669 | |
| 670 | |
| 671 | //------------------------------------------------------------------------------ |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 672 | /// @class EventMessageOperation |
| 673 | /// @brief Implements ProcessMonitor::GetEventMessage. |
| 674 | class EventMessageOperation : public Operation |
| 675 | { |
| 676 | public: |
| 677 | EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) |
| 678 | : m_tid(tid), m_message(message), m_result(result) { } |
| 679 | |
| 680 | void Execute(ProcessMonitor *monitor); |
| 681 | |
| 682 | private: |
| 683 | lldb::tid_t m_tid; |
| 684 | unsigned long *m_message; |
| 685 | bool &m_result; |
| 686 | }; |
| 687 | |
| 688 | void |
| 689 | EventMessageOperation::Execute(ProcessMonitor *monitor) |
| 690 | { |
| 691 | struct ptrace_lwpinfo plwp; |
| 692 | |
| 693 | if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) |
| 694 | m_result = false; |
| 695 | else { |
| 696 | if (plwp.pl_flags & PL_FLAG_FORKED) { |
Ed Maste | 82a0005 | 2013-11-25 21:15:53 +0000 | [diff] [blame] | 697 | *m_message = plwp.pl_child_pid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 698 | m_result = true; |
| 699 | } else |
| 700 | m_result = false; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | //------------------------------------------------------------------------------ |
| 705 | /// @class KillOperation |
Ed Maste | 7088293 | 2014-04-01 14:30:56 +0000 | [diff] [blame] | 706 | /// @brief Implements ProcessMonitor::Kill. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 707 | class KillOperation : public Operation |
| 708 | { |
| 709 | public: |
| 710 | KillOperation(bool &result) : m_result(result) { } |
| 711 | |
| 712 | void Execute(ProcessMonitor *monitor); |
| 713 | |
| 714 | private: |
| 715 | bool &m_result; |
| 716 | }; |
| 717 | |
| 718 | void |
| 719 | KillOperation::Execute(ProcessMonitor *monitor) |
| 720 | { |
| 721 | lldb::pid_t pid = monitor->GetPID(); |
| 722 | |
| 723 | if (PTRACE(PT_KILL, pid, NULL, 0)) |
| 724 | m_result = false; |
| 725 | else |
| 726 | m_result = true; |
| 727 | } |
| 728 | |
| 729 | //------------------------------------------------------------------------------ |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 730 | /// @class DetachOperation |
Ed Maste | 263c928 | 2014-03-17 17:45:53 +0000 | [diff] [blame] | 731 | /// @brief Implements ProcessMonitor::Detach. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 732 | class DetachOperation : public Operation |
| 733 | { |
| 734 | public: |
| 735 | DetachOperation(Error &result) : m_error(result) { } |
| 736 | |
| 737 | void Execute(ProcessMonitor *monitor); |
| 738 | |
| 739 | private: |
| 740 | Error &m_error; |
| 741 | }; |
| 742 | |
| 743 | void |
| 744 | DetachOperation::Execute(ProcessMonitor *monitor) |
| 745 | { |
| 746 | lldb::pid_t pid = monitor->GetPID(); |
| 747 | |
| 748 | if (PTRACE(PT_DETACH, pid, NULL, 0) < 0) |
| 749 | m_error.SetErrorToErrno(); |
| 750 | |
| 751 | } |
| 752 | |
| 753 | ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor) |
| 754 | : m_monitor(monitor) |
| 755 | { |
| 756 | sem_init(&m_semaphore, 0, 0); |
| 757 | } |
| 758 | |
| 759 | ProcessMonitor::OperationArgs::~OperationArgs() |
| 760 | { |
| 761 | sem_destroy(&m_semaphore); |
| 762 | } |
| 763 | |
| 764 | ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, |
| 765 | lldb_private::Module *module, |
| 766 | char const **argv, |
| 767 | char const **envp, |
| 768 | const char *stdin_path, |
| 769 | const char *stdout_path, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 770 | const char *stderr_path, |
| 771 | const char *working_dir) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 772 | : OperationArgs(monitor), |
| 773 | m_module(module), |
| 774 | m_argv(argv), |
| 775 | m_envp(envp), |
| 776 | m_stdin_path(stdin_path), |
| 777 | m_stdout_path(stdout_path), |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 778 | m_stderr_path(stderr_path), |
| 779 | m_working_dir(working_dir) { } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 780 | |
| 781 | ProcessMonitor::LaunchArgs::~LaunchArgs() |
| 782 | { } |
| 783 | |
| 784 | ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, |
| 785 | lldb::pid_t pid) |
| 786 | : OperationArgs(monitor), m_pid(pid) { } |
| 787 | |
| 788 | ProcessMonitor::AttachArgs::~AttachArgs() |
| 789 | { } |
| 790 | |
| 791 | //------------------------------------------------------------------------------ |
| 792 | /// The basic design of the ProcessMonitor is built around two threads. |
| 793 | /// |
| 794 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 795 | /// for changes in the debugee state. When a change is detected a |
| 796 | /// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread |
| 797 | /// "drives" state changes in the debugger. |
| 798 | /// |
| 799 | /// The second thread (@see OperationThread) is responsible for two things 1) |
| 800 | /// launching or attaching to the inferior process, and then 2) servicing |
| 801 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 802 | /// on the Operation class for more info as to why this is needed. |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 803 | ProcessMonitor::ProcessMonitor(ProcessPOSIX *process, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 804 | Module *module, |
| 805 | const char *argv[], |
| 806 | const char *envp[], |
| 807 | const char *stdin_path, |
| 808 | const char *stdout_path, |
| 809 | const char *stderr_path, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 810 | const char *working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 811 | const lldb_private::ProcessLaunchInfo & /* launch_info */, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 812 | lldb_private::Error &error) |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 813 | : m_process(static_cast<ProcessFreeBSD *>(process)), |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 814 | m_pid(LLDB_INVALID_PROCESS_ID), |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 815 | m_terminal_fd(-1), |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 816 | m_operation(0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 817 | { |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 818 | std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp, |
| 819 | stdin_path, stdout_path, stderr_path, |
| 820 | working_dir)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 821 | |
| 822 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 823 | sem_init(&m_operation_pending, 0, 0); |
| 824 | sem_init(&m_operation_done, 0, 0); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 825 | |
| 826 | StartLaunchOpThread(args.get(), error); |
| 827 | if (!error.Success()) |
| 828 | return; |
| 829 | |
| 830 | WAIT_AGAIN: |
| 831 | // Wait for the operation thread to initialize. |
| 832 | if (sem_wait(&args->m_semaphore)) |
| 833 | { |
| 834 | if (errno == EINTR) |
| 835 | goto WAIT_AGAIN; |
| 836 | else |
| 837 | { |
| 838 | error.SetErrorToErrno(); |
| 839 | return; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | // Check that the launch was a success. |
| 844 | if (!args->m_error.Success()) |
| 845 | { |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 846 | StopOpThread(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 847 | error = args->m_error; |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | // Finally, start monitoring the child process for change in state. |
| 852 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 853 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 854 | if (m_monitor_thread.GetState() != eThreadStateRunning) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 855 | { |
| 856 | error.SetErrorToGenericError(); |
| 857 | error.SetErrorString("Process launch failed."); |
| 858 | return; |
| 859 | } |
| 860 | } |
| 861 | |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 862 | ProcessMonitor::ProcessMonitor(ProcessPOSIX *process, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 863 | lldb::pid_t pid, |
| 864 | lldb_private::Error &error) |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 865 | : m_process(static_cast<ProcessFreeBSD *>(process)), |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 866 | m_pid(pid), |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 867 | m_terminal_fd(-1), |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 868 | m_operation(0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 869 | { |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 870 | sem_init(&m_operation_pending, 0, 0); |
| 871 | sem_init(&m_operation_done, 0, 0); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 872 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 873 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 874 | std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 875 | |
| 876 | StartAttachOpThread(args.get(), error); |
| 877 | if (!error.Success()) |
| 878 | return; |
| 879 | |
| 880 | WAIT_AGAIN: |
| 881 | // Wait for the operation thread to initialize. |
| 882 | if (sem_wait(&args->m_semaphore)) |
| 883 | { |
| 884 | if (errno == EINTR) |
| 885 | goto WAIT_AGAIN; |
| 886 | else |
| 887 | { |
| 888 | error.SetErrorToErrno(); |
| 889 | return; |
| 890 | } |
| 891 | } |
| 892 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 893 | // Check that the attach was a success. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 894 | if (!args->m_error.Success()) |
| 895 | { |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 896 | StopOpThread(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 897 | error = args->m_error; |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | // Finally, start monitoring the child process for change in state. |
| 902 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 903 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 904 | if (m_monitor_thread.GetState() != eThreadStateRunning) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 905 | { |
| 906 | error.SetErrorToGenericError(); |
| 907 | error.SetErrorString("Process attach failed."); |
| 908 | return; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | ProcessMonitor::~ProcessMonitor() |
| 913 | { |
| 914 | StopMonitor(); |
| 915 | } |
| 916 | |
| 917 | //------------------------------------------------------------------------------ |
| 918 | // Thread setup and tear down. |
| 919 | void |
| 920 | ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) |
| 921 | { |
| 922 | static const char *g_thread_name = "lldb.process.freebsd.operation"; |
| 923 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 924 | if (m_operation_thread.GetState() == eThreadStateRunning) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 925 | return; |
| 926 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 927 | m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 930 | void * |
| 931 | ProcessMonitor::LaunchOpThread(void *arg) |
| 932 | { |
| 933 | LaunchArgs *args = static_cast<LaunchArgs*>(arg); |
| 934 | |
| 935 | if (!Launch(args)) { |
| 936 | sem_post(&args->m_semaphore); |
| 937 | return NULL; |
| 938 | } |
| 939 | |
| 940 | ServeOperation(args); |
| 941 | return NULL; |
| 942 | } |
| 943 | |
| 944 | bool |
| 945 | ProcessMonitor::Launch(LaunchArgs *args) |
| 946 | { |
| 947 | ProcessMonitor *monitor = args->m_monitor; |
| 948 | ProcessFreeBSD &process = monitor->GetProcess(); |
| 949 | const char **argv = args->m_argv; |
| 950 | const char **envp = args->m_envp; |
| 951 | const char *stdin_path = args->m_stdin_path; |
| 952 | const char *stdout_path = args->m_stdout_path; |
| 953 | const char *stderr_path = args->m_stderr_path; |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 954 | const char *working_dir = args->m_working_dir; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 955 | |
| 956 | lldb_utility::PseudoTerminal terminal; |
| 957 | const size_t err_len = 1024; |
| 958 | char err_str[err_len]; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 959 | lldb::pid_t pid; |
| 960 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 961 | // Propagate the environment if one is not supplied. |
| 962 | if (envp == NULL || envp[0] == NULL) |
| 963 | envp = const_cast<const char **>(environ); |
| 964 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 965 | if ((pid = terminal.Fork(err_str, err_len)) == -1) |
| 966 | { |
| 967 | args->m_error.SetErrorToGenericError(); |
| 968 | args->m_error.SetErrorString("Process fork failed."); |
| 969 | goto FINISH; |
| 970 | } |
| 971 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 972 | // Recognized child exit status codes. |
| 973 | enum { |
| 974 | ePtraceFailed = 1, |
| 975 | eDupStdinFailed, |
| 976 | eDupStdoutFailed, |
| 977 | eDupStderrFailed, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 978 | eChdirFailed, |
Ed Maste | 441a1be | 2014-02-04 19:37:15 +0000 | [diff] [blame] | 979 | eExecFailed, |
| 980 | eSetGidFailed |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 981 | }; |
| 982 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 983 | // Child process. |
| 984 | if (pid == 0) |
| 985 | { |
| 986 | // Trace this process. |
| 987 | if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0) |
| 988 | exit(ePtraceFailed); |
| 989 | |
| 990 | // Do not inherit setgid powers. |
Ed Maste | 441a1be | 2014-02-04 19:37:15 +0000 | [diff] [blame] | 991 | if (setgid(getgid()) != 0) |
| 992 | exit(eSetGidFailed); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 993 | |
| 994 | // Let us have our own process group. |
| 995 | setpgid(0, 0); |
| 996 | |
| 997 | // Dup file descriptors if needed. |
| 998 | // |
| 999 | // FIXME: If two or more of the paths are the same we needlessly open |
| 1000 | // the same file multiple times. |
| 1001 | if (stdin_path != NULL && stdin_path[0]) |
| 1002 | if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY)) |
| 1003 | exit(eDupStdinFailed); |
| 1004 | |
| 1005 | if (stdout_path != NULL && stdout_path[0]) |
| 1006 | if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
| 1007 | exit(eDupStdoutFailed); |
| 1008 | |
| 1009 | if (stderr_path != NULL && stderr_path[0]) |
| 1010 | if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT)) |
| 1011 | exit(eDupStderrFailed); |
| 1012 | |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 1013 | // Change working directory |
| 1014 | if (working_dir != NULL && working_dir[0]) |
| 1015 | if (0 != ::chdir(working_dir)) |
| 1016 | exit(eChdirFailed); |
| 1017 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1018 | // Execute. We should never return. |
| 1019 | execve(argv[0], |
| 1020 | const_cast<char *const *>(argv), |
| 1021 | const_cast<char *const *>(envp)); |
| 1022 | exit(eExecFailed); |
| 1023 | } |
| 1024 | |
| 1025 | // Wait for the child process to to trap on its call to execve. |
| 1026 | ::pid_t wpid; |
| 1027 | int status; |
| 1028 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
| 1029 | { |
| 1030 | args->m_error.SetErrorToErrno(); |
| 1031 | goto FINISH; |
| 1032 | } |
| 1033 | else if (WIFEXITED(status)) |
| 1034 | { |
| 1035 | // open, dup or execve likely failed for some reason. |
| 1036 | args->m_error.SetErrorToGenericError(); |
| 1037 | switch (WEXITSTATUS(status)) |
| 1038 | { |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1039 | case ePtraceFailed: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1040 | args->m_error.SetErrorString("Child ptrace failed."); |
| 1041 | break; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1042 | case eDupStdinFailed: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1043 | args->m_error.SetErrorString("Child open stdin failed."); |
| 1044 | break; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1045 | case eDupStdoutFailed: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1046 | args->m_error.SetErrorString("Child open stdout failed."); |
| 1047 | break; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1048 | case eDupStderrFailed: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1049 | args->m_error.SetErrorString("Child open stderr failed."); |
| 1050 | break; |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 1051 | case eChdirFailed: |
| 1052 | args->m_error.SetErrorString("Child failed to set working directory."); |
| 1053 | break; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1054 | case eExecFailed: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1055 | args->m_error.SetErrorString("Child exec failed."); |
| 1056 | break; |
Ed Maste | 441a1be | 2014-02-04 19:37:15 +0000 | [diff] [blame] | 1057 | case eSetGidFailed: |
| 1058 | args->m_error.SetErrorString("Child setgid failed."); |
| 1059 | break; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1060 | default: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1061 | args->m_error.SetErrorString("Child returned unknown exit status."); |
| 1062 | break; |
| 1063 | } |
| 1064 | goto FINISH; |
| 1065 | } |
Ed Maste | 82a0005 | 2013-11-25 21:15:53 +0000 | [diff] [blame] | 1066 | assert(WIFSTOPPED(status) && wpid == (::pid_t)pid && |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1067 | "Could not sync with inferior process."); |
| 1068 | |
| 1069 | #ifdef notyet |
| 1070 | // Have the child raise an event on exit. This is used to keep the child in |
| 1071 | // limbo until it is destroyed. |
| 1072 | if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) |
| 1073 | { |
| 1074 | args->m_error.SetErrorToErrno(); |
| 1075 | goto FINISH; |
| 1076 | } |
| 1077 | #endif |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1078 | // Release the master terminal descriptor and pass it off to the |
| 1079 | // ProcessMonitor instance. Similarly stash the inferior pid. |
| 1080 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1081 | monitor->m_pid = pid; |
| 1082 | |
| 1083 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 1084 | // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking |
| 1085 | // descriptor to read from). |
| 1086 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 1087 | goto FINISH; |
| 1088 | |
Ed Maste | e544143 | 2013-09-03 23:55:30 +0000 | [diff] [blame] | 1089 | process.SendMessage(ProcessMessage::Attach(pid)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1090 | |
| 1091 | FINISH: |
| 1092 | return args->m_error.Success(); |
| 1093 | } |
| 1094 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1095 | void |
| 1096 | ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) |
| 1097 | { |
| 1098 | static const char *g_thread_name = "lldb.process.freebsd.operation"; |
| 1099 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1100 | if (m_operation_thread.GetState() == eThreadStateRunning) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1101 | return; |
| 1102 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1103 | m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1106 | void * |
| 1107 | ProcessMonitor::AttachOpThread(void *arg) |
| 1108 | { |
| 1109 | AttachArgs *args = static_cast<AttachArgs*>(arg); |
| 1110 | |
| 1111 | if (!Attach(args)) |
| 1112 | return NULL; |
| 1113 | |
| 1114 | ServeOperation(args); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
| 1118 | bool |
| 1119 | ProcessMonitor::Attach(AttachArgs *args) |
| 1120 | { |
| 1121 | lldb::pid_t pid = args->m_pid; |
| 1122 | |
| 1123 | ProcessMonitor *monitor = args->m_monitor; |
| 1124 | ProcessFreeBSD &process = monitor->GetProcess(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1125 | |
| 1126 | if (pid <= 1) |
| 1127 | { |
| 1128 | args->m_error.SetErrorToGenericError(); |
| 1129 | args->m_error.SetErrorString("Attaching to process 1 is not allowed."); |
| 1130 | goto FINISH; |
| 1131 | } |
| 1132 | |
| 1133 | // Attach to the requested process. |
| 1134 | if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) |
| 1135 | { |
| 1136 | args->m_error.SetErrorToErrno(); |
| 1137 | goto FINISH; |
| 1138 | } |
| 1139 | |
| 1140 | int status; |
| 1141 | if ((status = waitpid(pid, NULL, 0)) < 0) |
| 1142 | { |
| 1143 | args->m_error.SetErrorToErrno(); |
| 1144 | goto FINISH; |
| 1145 | } |
| 1146 | |
Ed Maste | e544143 | 2013-09-03 23:55:30 +0000 | [diff] [blame] | 1147 | process.SendMessage(ProcessMessage::Attach(pid)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1148 | |
Ed Maste | e544143 | 2013-09-03 23:55:30 +0000 | [diff] [blame] | 1149 | FINISH: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1150 | return args->m_error.Success(); |
| 1151 | } |
| 1152 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1153 | size_t |
| 1154 | ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids) |
| 1155 | { |
| 1156 | lwpid_t *tids; |
| 1157 | int tdcnt; |
| 1158 | |
| 1159 | thread_ids.clear(); |
| 1160 | |
| 1161 | tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0); |
| 1162 | if (tdcnt <= 0) |
| 1163 | return 0; |
| 1164 | tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids)); |
| 1165 | if (tids == NULL) |
| 1166 | return 0; |
| 1167 | if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) { |
| 1168 | free(tids); |
| 1169 | return 0; |
| 1170 | } |
| 1171 | thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt); |
| 1172 | free(tids); |
| 1173 | return thread_ids.size(); |
| 1174 | } |
| 1175 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1176 | bool |
| 1177 | ProcessMonitor::MonitorCallback(void *callback_baton, |
| 1178 | lldb::pid_t pid, |
| 1179 | bool exited, |
| 1180 | int signal, |
| 1181 | int status) |
| 1182 | { |
| 1183 | ProcessMessage message; |
| 1184 | ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton); |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 1185 | ProcessFreeBSD *process = monitor->m_process; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1186 | assert(process); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1187 | bool stop_monitoring; |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 1188 | struct ptrace_lwpinfo plwp; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1189 | int ptrace_err; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1190 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1191 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 1192 | |
| 1193 | if (exited) |
| 1194 | { |
| 1195 | if (log) |
| 1196 | log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid); |
| 1197 | message = ProcessMessage::Exit(pid, status); |
| 1198 | process->SendMessage(message); |
| 1199 | return pid == process->GetID(); |
| 1200 | } |
| 1201 | |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 1202 | if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1203 | stop_monitoring = true; // pid is gone. Bail. |
| 1204 | else { |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 1205 | switch (plwp.pl_siginfo.si_signo) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1206 | { |
| 1207 | case SIGTRAP: |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1208 | message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1209 | break; |
| 1210 | |
| 1211 | default: |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1212 | message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | process->SendMessage(message); |
| 1217 | stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage; |
| 1218 | } |
| 1219 | |
| 1220 | return stop_monitoring; |
| 1221 | } |
| 1222 | |
| 1223 | ProcessMessage |
| 1224 | ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1225 | const siginfo_t *info, lldb::tid_t tid) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1226 | { |
| 1227 | ProcessMessage message; |
| 1228 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1229 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 1230 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1231 | assert(monitor); |
| 1232 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1233 | |
| 1234 | switch (info->si_code) |
| 1235 | { |
| 1236 | default: |
| 1237 | assert(false && "Unexpected SIGTRAP code!"); |
| 1238 | break; |
| 1239 | |
| 1240 | case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): |
| 1241 | { |
| 1242 | // The inferior process is about to exit. Maintain the process in a |
| 1243 | // state of "limbo" until we are explicitly commanded to detach, |
| 1244 | // destroy, resume, etc. |
| 1245 | unsigned long data = 0; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1246 | if (!monitor->GetEventMessage(tid, &data)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1247 | data = -1; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1248 | if (log) |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1249 | log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid); |
| 1250 | message = ProcessMessage::Limbo(tid, (data >> 8)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | case 0: |
| 1255 | case TRAP_TRACE: |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1256 | if (log) |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 1257 | log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1258 | message = ProcessMessage::Trace(tid); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1259 | break; |
| 1260 | |
| 1261 | case SI_KERNEL: |
| 1262 | case TRAP_BRKPT: |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1263 | if (log) |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1264 | log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid); |
| 1265 | message = ProcessMessage::Break(tid); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | return message; |
| 1270 | } |
| 1271 | |
| 1272 | ProcessMessage |
| 1273 | ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1274 | const siginfo_t *info, lldb::tid_t tid) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1275 | { |
| 1276 | ProcessMessage message; |
| 1277 | int signo = info->si_signo; |
| 1278 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1279 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 1280 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1281 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 1282 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 1283 | // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD. |
| 1284 | // |
| 1285 | // IOW, user generated signals never generate what we consider to be a |
| 1286 | // "crash". |
| 1287 | // |
| 1288 | // Similarly, ACK signals generated by this monitor. |
| 1289 | if (info->si_code == SI_USER) |
| 1290 | { |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1291 | if (log) |
| 1292 | log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d", |
| 1293 | __FUNCTION__, |
| 1294 | monitor->m_process->GetUnixSignals().GetSignalAsCString (signo), |
| 1295 | "SI_USER", |
| 1296 | info->si_pid); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1297 | if (info->si_pid == getpid()) |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1298 | return ProcessMessage::SignalDelivered(tid, signo); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1299 | else |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1300 | return ProcessMessage::Signal(tid, signo); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1303 | if (log) |
| 1304 | log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo)); |
| 1305 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1306 | if (signo == SIGSEGV) { |
| 1307 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1308 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1309 | return ProcessMessage::Crash(tid, reason, signo, fault_addr); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | if (signo == SIGILL) { |
| 1313 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1314 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1315 | return ProcessMessage::Crash(tid, reason, signo, fault_addr); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | if (signo == SIGFPE) { |
| 1319 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1320 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1321 | return ProcessMessage::Crash(tid, reason, signo, fault_addr); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | if (signo == SIGBUS) { |
| 1325 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1326 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1327 | return ProcessMessage::Crash(tid, reason, signo, fault_addr); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | // Everything else is "normal" and does not require any special action on |
| 1331 | // our part. |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1332 | return ProcessMessage::Signal(tid, signo); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | ProcessMessage::CrashReason |
| 1336 | ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
| 1337 | { |
| 1338 | ProcessMessage::CrashReason reason; |
| 1339 | assert(info->si_signo == SIGSEGV); |
| 1340 | |
| 1341 | reason = ProcessMessage::eInvalidCrashReason; |
| 1342 | |
| 1343 | switch (info->si_code) |
| 1344 | { |
| 1345 | default: |
| 1346 | assert(false && "unexpected si_code for SIGSEGV"); |
| 1347 | break; |
| 1348 | case SEGV_MAPERR: |
| 1349 | reason = ProcessMessage::eInvalidAddress; |
| 1350 | break; |
| 1351 | case SEGV_ACCERR: |
| 1352 | reason = ProcessMessage::ePrivilegedAddress; |
| 1353 | break; |
| 1354 | } |
| 1355 | |
| 1356 | return reason; |
| 1357 | } |
| 1358 | |
| 1359 | ProcessMessage::CrashReason |
| 1360 | ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info) |
| 1361 | { |
| 1362 | ProcessMessage::CrashReason reason; |
| 1363 | assert(info->si_signo == SIGILL); |
| 1364 | |
| 1365 | reason = ProcessMessage::eInvalidCrashReason; |
| 1366 | |
| 1367 | switch (info->si_code) |
| 1368 | { |
| 1369 | default: |
| 1370 | assert(false && "unexpected si_code for SIGILL"); |
| 1371 | break; |
| 1372 | case ILL_ILLOPC: |
| 1373 | reason = ProcessMessage::eIllegalOpcode; |
| 1374 | break; |
| 1375 | case ILL_ILLOPN: |
| 1376 | reason = ProcessMessage::eIllegalOperand; |
| 1377 | break; |
| 1378 | case ILL_ILLADR: |
| 1379 | reason = ProcessMessage::eIllegalAddressingMode; |
| 1380 | break; |
| 1381 | case ILL_ILLTRP: |
| 1382 | reason = ProcessMessage::eIllegalTrap; |
| 1383 | break; |
| 1384 | case ILL_PRVOPC: |
| 1385 | reason = ProcessMessage::ePrivilegedOpcode; |
| 1386 | break; |
| 1387 | case ILL_PRVREG: |
| 1388 | reason = ProcessMessage::ePrivilegedRegister; |
| 1389 | break; |
| 1390 | case ILL_COPROC: |
| 1391 | reason = ProcessMessage::eCoprocessorError; |
| 1392 | break; |
| 1393 | case ILL_BADSTK: |
| 1394 | reason = ProcessMessage::eInternalStackError; |
| 1395 | break; |
| 1396 | } |
| 1397 | |
| 1398 | return reason; |
| 1399 | } |
| 1400 | |
| 1401 | ProcessMessage::CrashReason |
| 1402 | ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info) |
| 1403 | { |
| 1404 | ProcessMessage::CrashReason reason; |
| 1405 | assert(info->si_signo == SIGFPE); |
| 1406 | |
| 1407 | reason = ProcessMessage::eInvalidCrashReason; |
| 1408 | |
| 1409 | switch (info->si_code) |
| 1410 | { |
| 1411 | default: |
| 1412 | assert(false && "unexpected si_code for SIGFPE"); |
| 1413 | break; |
| 1414 | case FPE_INTDIV: |
| 1415 | reason = ProcessMessage::eIntegerDivideByZero; |
| 1416 | break; |
| 1417 | case FPE_INTOVF: |
| 1418 | reason = ProcessMessage::eIntegerOverflow; |
| 1419 | break; |
| 1420 | case FPE_FLTDIV: |
| 1421 | reason = ProcessMessage::eFloatDivideByZero; |
| 1422 | break; |
| 1423 | case FPE_FLTOVF: |
| 1424 | reason = ProcessMessage::eFloatOverflow; |
| 1425 | break; |
| 1426 | case FPE_FLTUND: |
| 1427 | reason = ProcessMessage::eFloatUnderflow; |
| 1428 | break; |
| 1429 | case FPE_FLTRES: |
| 1430 | reason = ProcessMessage::eFloatInexactResult; |
| 1431 | break; |
| 1432 | case FPE_FLTINV: |
| 1433 | reason = ProcessMessage::eFloatInvalidOperation; |
| 1434 | break; |
| 1435 | case FPE_FLTSUB: |
| 1436 | reason = ProcessMessage::eFloatSubscriptRange; |
| 1437 | break; |
| 1438 | } |
| 1439 | |
| 1440 | return reason; |
| 1441 | } |
| 1442 | |
| 1443 | ProcessMessage::CrashReason |
| 1444 | ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info) |
| 1445 | { |
| 1446 | ProcessMessage::CrashReason reason; |
| 1447 | assert(info->si_signo == SIGBUS); |
| 1448 | |
| 1449 | reason = ProcessMessage::eInvalidCrashReason; |
| 1450 | |
| 1451 | switch (info->si_code) |
| 1452 | { |
| 1453 | default: |
| 1454 | assert(false && "unexpected si_code for SIGBUS"); |
| 1455 | break; |
| 1456 | case BUS_ADRALN: |
| 1457 | reason = ProcessMessage::eIllegalAlignment; |
| 1458 | break; |
| 1459 | case BUS_ADRERR: |
| 1460 | reason = ProcessMessage::eIllegalAddress; |
| 1461 | break; |
| 1462 | case BUS_OBJERR: |
| 1463 | reason = ProcessMessage::eHardwareError; |
| 1464 | break; |
| 1465 | } |
| 1466 | |
| 1467 | return reason; |
| 1468 | } |
| 1469 | |
| 1470 | void |
| 1471 | ProcessMonitor::ServeOperation(OperationArgs *args) |
| 1472 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1473 | ProcessMonitor *monitor = args->m_monitor; |
| 1474 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1475 | // We are finised with the arguments and are ready to go. Sync with the |
| 1476 | // parent thread and start serving operations on the inferior. |
| 1477 | sem_post(&args->m_semaphore); |
| 1478 | |
| 1479 | for (;;) |
| 1480 | { |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1481 | // wait for next pending operation |
| 1482 | sem_wait(&monitor->m_operation_pending); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1483 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1484 | monitor->m_operation->Execute(monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1485 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1486 | // notify calling thread that operation is complete |
| 1487 | sem_post(&monitor->m_operation_done); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | void |
| 1492 | ProcessMonitor::DoOperation(Operation *op) |
| 1493 | { |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1494 | Mutex::Locker lock(m_operation_mutex); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1495 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1496 | m_operation = op; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1497 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1498 | // notify operation thread that an operation is ready to be processed |
| 1499 | sem_post(&m_operation_pending); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1500 | |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1501 | // wait for operation to complete |
| 1502 | sem_wait(&m_operation_done); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | size_t |
| 1506 | ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 1507 | Error &error) |
| 1508 | { |
| 1509 | size_t result; |
| 1510 | ReadOperation op(vm_addr, buf, size, error, result); |
| 1511 | DoOperation(&op); |
| 1512 | return result; |
| 1513 | } |
| 1514 | |
| 1515 | size_t |
| 1516 | ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 1517 | lldb_private::Error &error) |
| 1518 | { |
| 1519 | size_t result; |
| 1520 | WriteOperation op(vm_addr, buf, size, error, result); |
| 1521 | DoOperation(&op); |
| 1522 | return result; |
| 1523 | } |
| 1524 | |
| 1525 | bool |
Ashok Thirumurthi | acbb1a5 | 2013-05-09 19:59:47 +0000 | [diff] [blame] | 1526 | ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name, |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1527 | unsigned size, RegisterValue &value) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1528 | { |
| 1529 | bool result; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 1530 | ReadRegOperation op(tid, offset, size, value, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1531 | DoOperation(&op); |
| 1532 | return result; |
| 1533 | } |
| 1534 | |
| 1535 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1536 | ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
Ashok Thirumurthi | acbb1a5 | 2013-05-09 19:59:47 +0000 | [diff] [blame] | 1537 | const char* reg_name, const RegisterValue &value) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1538 | { |
| 1539 | bool result; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 1540 | WriteRegOperation op(tid, offset, value, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1541 | DoOperation(&op); |
| 1542 | return result; |
| 1543 | } |
| 1544 | |
| 1545 | bool |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 1546 | ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset, |
| 1547 | const char *reg_name, unsigned size, |
| 1548 | lldb_private::RegisterValue &value) |
| 1549 | { |
| 1550 | bool result; |
| 1551 | ReadDebugRegOperation op(tid, offset, size, value, result); |
| 1552 | DoOperation(&op); |
| 1553 | return result; |
| 1554 | } |
| 1555 | |
| 1556 | bool |
| 1557 | ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset, |
| 1558 | const char *reg_name, |
| 1559 | const lldb_private::RegisterValue &value) |
| 1560 | { |
| 1561 | bool result; |
| 1562 | WriteDebugRegOperation op(tid, offset, value, result); |
| 1563 | DoOperation(&op); |
| 1564 | return result; |
| 1565 | } |
| 1566 | |
| 1567 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1568 | ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1569 | { |
| 1570 | bool result; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 1571 | ReadGPROperation op(tid, buf, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1572 | DoOperation(&op); |
| 1573 | return result; |
| 1574 | } |
| 1575 | |
| 1576 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1577 | ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1578 | { |
| 1579 | bool result; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 1580 | ReadFPROperation op(tid, buf, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1581 | DoOperation(&op); |
| 1582 | return result; |
| 1583 | } |
| 1584 | |
| 1585 | bool |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1586 | ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 1587 | { |
| 1588 | return false; |
| 1589 | } |
| 1590 | |
| 1591 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1592 | ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1593 | { |
| 1594 | bool result; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 1595 | WriteGPROperation op(tid, buf, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1596 | DoOperation(&op); |
| 1597 | return result; |
| 1598 | } |
| 1599 | |
| 1600 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1601 | ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1602 | { |
| 1603 | bool result; |
Ed Maste | 6f06641 | 2013-07-04 21:47:32 +0000 | [diff] [blame] | 1604 | WriteFPROperation op(tid, buf, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1605 | DoOperation(&op); |
| 1606 | return result; |
| 1607 | } |
| 1608 | |
| 1609 | bool |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1610 | ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 1611 | { |
| 1612 | return false; |
| 1613 | } |
| 1614 | |
| 1615 | bool |
Ed Maste | 68f5179 | 2013-10-18 19:16:44 +0000 | [diff] [blame] | 1616 | ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) |
| 1617 | { |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
| 1621 | bool |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 1622 | ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1623 | { |
| 1624 | bool result; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1625 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 1626 | |
Ed Maste | 4aeb3c0 | 2014-05-28 14:11:20 +0000 | [diff] [blame] | 1627 | if (log) { |
| 1628 | const char *signame = m_process->GetUnixSignals().GetSignalAsCString (signo); |
| 1629 | if (signame == nullptr) |
| 1630 | signame = "<none>"; |
| 1631 | log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s", |
| 1632 | __FUNCTION__, GetPID(), signame); |
| 1633 | } |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 1634 | ResumeOperation op(signo, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1635 | DoOperation(&op); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1636 | if (log) |
| 1637 | log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false"); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1638 | return result; |
| 1639 | } |
| 1640 | |
| 1641 | bool |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 1642 | ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1643 | { |
| 1644 | bool result; |
Ed Maste | 502f902 | 2013-11-25 16:31:23 +0000 | [diff] [blame] | 1645 | SingleStepOperation op(signo, result); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1646 | DoOperation(&op); |
| 1647 | return result; |
| 1648 | } |
| 1649 | |
| 1650 | bool |
Ed Maste | 7088293 | 2014-04-01 14:30:56 +0000 | [diff] [blame] | 1651 | ProcessMonitor::Kill() |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1652 | { |
| 1653 | bool result; |
| 1654 | KillOperation op(result); |
| 1655 | DoOperation(&op); |
| 1656 | return result; |
| 1657 | } |
| 1658 | |
| 1659 | bool |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 1660 | ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1661 | { |
| 1662 | bool result; |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 1663 | LwpInfoOperation op(tid, lwpinfo, result, ptrace_err); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1664 | DoOperation(&op); |
| 1665 | return result; |
| 1666 | } |
| 1667 | |
| 1668 | bool |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1669 | ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) |
| 1670 | { |
| 1671 | bool result; |
| 1672 | ThreadSuspendOperation op(tid, suspend, result); |
| 1673 | DoOperation(&op); |
| 1674 | return result; |
| 1675 | } |
| 1676 | |
| 1677 | bool |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1678 | ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 1679 | { |
| 1680 | bool result; |
| 1681 | EventMessageOperation op(tid, message, result); |
| 1682 | DoOperation(&op); |
| 1683 | return result; |
| 1684 | } |
| 1685 | |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1686 | lldb_private::Error |
Matt Kopec | edee182 | 2013-06-03 19:48:53 +0000 | [diff] [blame] | 1687 | ProcessMonitor::Detach(lldb::tid_t tid) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1688 | { |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1689 | lldb_private::Error error; |
| 1690 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1691 | { |
| 1692 | DetachOperation op(error); |
| 1693 | DoOperation(&op); |
| 1694 | } |
| 1695 | return error; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | bool |
| 1699 | ProcessMonitor::DupDescriptor(const char *path, int fd, int flags) |
| 1700 | { |
| 1701 | int target_fd = open(path, flags, 0666); |
| 1702 | |
| 1703 | if (target_fd == -1) |
| 1704 | return false; |
| 1705 | |
| 1706 | return (dup2(target_fd, fd) == -1) ? false : true; |
| 1707 | } |
| 1708 | |
| 1709 | void |
| 1710 | ProcessMonitor::StopMonitoringChildProcess() |
| 1711 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1712 | if (m_monitor_thread.GetState() == eThreadStateRunning) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1713 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1714 | m_monitor_thread.Cancel(); |
| 1715 | m_monitor_thread.Join(nullptr); |
| 1716 | m_monitor_thread.Reset(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | void |
| 1721 | ProcessMonitor::StopMonitor() |
| 1722 | { |
| 1723 | StopMonitoringChildProcess(); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1724 | StopOpThread(); |
Ed Maste | 756e1ff | 2013-09-18 19:34:08 +0000 | [diff] [blame] | 1725 | sem_destroy(&m_operation_pending); |
| 1726 | sem_destroy(&m_operation_done); |
| 1727 | |
Andrew Kaylor | 5e26899 | 2013-09-14 00:17:31 +0000 | [diff] [blame] | 1728 | // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to |
| 1729 | // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of |
| 1730 | // the descriptor to a ConnectionFileDescriptor object. Consequently |
| 1731 | // even though still has the file descriptor, we shouldn't close it here. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 1734 | // FIXME: On Linux, when a new thread is created, we receive to notifications, |
| 1735 | // (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the |
| 1736 | // child thread id as additional information, and (2) a SIGSTOP|SI_USER from |
| 1737 | // the new child thread indicating that it has is stopped because we attached. |
| 1738 | // We have no guarantee of the order in which these arrive, but we need both |
| 1739 | // before we are ready to proceed. We currently keep a list of threads which |
| 1740 | // have sent the initial SIGSTOP|SI_USER event. Then when we receive the |
| 1741 | // SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred |
| 1742 | // we call ProcessMonitor::WaitForInitialTIDStop() to wait for it. |
| 1743 | // |
| 1744 | // Right now, the above logic is in ProcessPOSIX, so we need a definition of |
| 1745 | // this function in the FreeBSD ProcessMonitor implementation even if it isn't |
| 1746 | // logically needed. |
| 1747 | // |
| 1748 | // We really should figure out what actually happens on FreeBSD and move the |
| 1749 | // Linux-specific logic out of ProcessPOSIX as needed. |
| 1750 | |
| 1751 | bool |
| 1752 | ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) |
| 1753 | { |
| 1754 | return true; |
| 1755 | } |
| 1756 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1757 | void |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1758 | ProcessMonitor::StopOpThread() |
| 1759 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1760 | if (m_operation_thread.GetState() != eThreadStateRunning) |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1761 | return; |
| 1762 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1763 | m_operation_thread.Cancel(); |
| 1764 | m_operation_thread.Join(nullptr); |
| 1765 | m_operation_thread.Reset(); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1766 | } |