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