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