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