Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +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> |
| 14 | #include <unistd.h> |
| 15 | #include <sys/ptrace.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/wait.h> |
| 19 | |
| 20 | // C++ Includes |
| 21 | // Other libraries and framework includes |
| 22 | #include "lldb/Core/Error.h" |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 23 | #include "lldb/Core/RegisterValue.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Scalar.h" |
| 25 | #include "lldb/Host/Host.h" |
| 26 | #include "lldb/Target/Thread.h" |
| 27 | #include "lldb/Target/RegisterContext.h" |
| 28 | #include "lldb/Utility/PseudoTerminal.h" |
| 29 | |
| 30 | #include "LinuxThread.h" |
| 31 | #include "ProcessLinux.h" |
| 32 | #include "ProcessMonitor.h" |
| 33 | |
| 34 | |
| 35 | using namespace lldb_private; |
| 36 | |
| 37 | //------------------------------------------------------------------------------ |
| 38 | // Static implementations of ProcessMonitor::ReadMemory and |
| 39 | // ProcessMonitor::WriteMemory. This enables mutual recursion between these |
| 40 | // functions without needed to go thru the thread funnel. |
| 41 | |
| 42 | static size_t |
| 43 | DoReadMemory(lldb::pid_t pid, unsigned word_size, |
| 44 | lldb::addr_t vm_addr, void *buf, size_t size, Error &error) |
| 45 | { |
| 46 | unsigned char *dst = static_cast<unsigned char*>(buf); |
| 47 | size_t bytes_read; |
| 48 | size_t remainder; |
| 49 | long data; |
| 50 | |
| 51 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) |
| 52 | { |
| 53 | errno = 0; |
| 54 | data = ptrace(PTRACE_PEEKDATA, pid, vm_addr, NULL); |
| 55 | |
| 56 | if (data == -1L && errno) |
| 57 | { |
| 58 | error.SetErrorToErrno(); |
| 59 | return bytes_read; |
| 60 | } |
| 61 | |
| 62 | remainder = size - bytes_read; |
| 63 | remainder = remainder > word_size ? word_size : remainder; |
| 64 | for (unsigned i = 0; i < remainder; ++i) |
| 65 | dst[i] = ((data >> i*8) & 0xFF); |
| 66 | vm_addr += word_size; |
| 67 | dst += word_size; |
| 68 | } |
| 69 | |
| 70 | return bytes_read; |
| 71 | } |
| 72 | |
| 73 | static size_t |
| 74 | DoWriteMemory(lldb::pid_t pid, unsigned word_size, |
| 75 | lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) |
| 76 | { |
| 77 | const unsigned char *src = static_cast<const unsigned char*>(buf); |
| 78 | size_t bytes_written = 0; |
| 79 | size_t remainder; |
| 80 | |
| 81 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) |
| 82 | { |
| 83 | remainder = size - bytes_written; |
| 84 | remainder = remainder > word_size ? word_size : remainder; |
| 85 | |
| 86 | if (remainder == word_size) |
| 87 | { |
| 88 | unsigned long data = 0; |
| 89 | for (unsigned i = 0; i < word_size; ++i) |
| 90 | data |= (unsigned long)src[i] << i*8; |
| 91 | |
| 92 | if (ptrace(PTRACE_POKEDATA, pid, vm_addr, data)) |
| 93 | { |
| 94 | error.SetErrorToErrno(); |
| 95 | return bytes_written; |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | unsigned char buff[8]; |
| 101 | if (DoReadMemory(pid, word_size, vm_addr, |
| 102 | buff, word_size, error) != word_size) |
| 103 | return bytes_written; |
| 104 | |
| 105 | memcpy(buff, src, remainder); |
| 106 | |
| 107 | if (DoWriteMemory(pid, word_size, vm_addr, |
| 108 | buff, word_size, error) != word_size) |
| 109 | return bytes_written; |
| 110 | } |
| 111 | |
| 112 | vm_addr += word_size; |
| 113 | src += word_size; |
| 114 | } |
| 115 | return bytes_written; |
| 116 | } |
| 117 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 118 | // Simple helper function to ensure flags are enabled on the given file |
| 119 | // descriptor. |
| 120 | static bool |
| 121 | EnsureFDFlags(int fd, int flags, Error &error) |
| 122 | { |
| 123 | int status; |
| 124 | |
| 125 | if ((status = fcntl(fd, F_GETFL)) == -1) |
| 126 | { |
| 127 | error.SetErrorToErrno(); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 132 | { |
| 133 | error.SetErrorToErrno(); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | return true; |
| 138 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 139 | |
| 140 | //------------------------------------------------------------------------------ |
| 141 | /// @class Operation |
| 142 | /// @brief Represents a ProcessMonitor operation. |
| 143 | /// |
| 144 | /// Under Linux, it is not possible to ptrace() from any other thread but the |
| 145 | /// one that spawned or attached to the process from the start. Therefore, when |
| 146 | /// a ProcessMonitor is asked to deliver or change the state of an inferior |
| 147 | /// process the operation must be "funneled" to a specific thread to perform the |
| 148 | /// task. The Operation class provides an abstract base for all services the |
| 149 | /// ProcessMonitor must perform via the single virtual function Execute, thus |
| 150 | /// encapsulating the code that needs to run in the privileged context. |
| 151 | class Operation |
| 152 | { |
| 153 | public: |
| 154 | virtual void Execute(ProcessMonitor *monitor) = 0; |
| 155 | }; |
| 156 | |
| 157 | //------------------------------------------------------------------------------ |
| 158 | /// @class ReadOperation |
| 159 | /// @brief Implements ProcessMonitor::ReadMemory. |
| 160 | class ReadOperation : public Operation |
| 161 | { |
| 162 | public: |
| 163 | ReadOperation(lldb::addr_t addr, void *buff, size_t size, |
| 164 | Error &error, size_t &result) |
| 165 | : m_addr(addr), m_buff(buff), m_size(size), |
| 166 | m_error(error), m_result(result) |
| 167 | { } |
| 168 | |
| 169 | void Execute(ProcessMonitor *monitor); |
| 170 | |
| 171 | private: |
| 172 | lldb::addr_t m_addr; |
| 173 | void *m_buff; |
| 174 | size_t m_size; |
| 175 | Error &m_error; |
| 176 | size_t &m_result; |
| 177 | }; |
| 178 | |
| 179 | void |
| 180 | ReadOperation::Execute(ProcessMonitor *monitor) |
| 181 | { |
| 182 | const unsigned word_size = monitor->GetProcess().GetAddressByteSize(); |
| 183 | lldb::pid_t pid = monitor->GetPID(); |
| 184 | |
| 185 | m_result = DoReadMemory(pid, word_size, m_addr, m_buff, m_size, m_error); |
| 186 | } |
| 187 | |
| 188 | //------------------------------------------------------------------------------ |
| 189 | /// @class ReadOperation |
| 190 | /// @brief Implements ProcessMonitor::WriteMemory. |
| 191 | class WriteOperation : public Operation |
| 192 | { |
| 193 | public: |
| 194 | WriteOperation(lldb::addr_t addr, const void *buff, size_t size, |
| 195 | Error &error, size_t &result) |
| 196 | : m_addr(addr), m_buff(buff), m_size(size), |
| 197 | m_error(error), m_result(result) |
| 198 | { } |
| 199 | |
| 200 | void Execute(ProcessMonitor *monitor); |
| 201 | |
| 202 | private: |
| 203 | lldb::addr_t m_addr; |
| 204 | const void *m_buff; |
| 205 | size_t m_size; |
| 206 | Error &m_error; |
| 207 | size_t &m_result; |
| 208 | }; |
| 209 | |
| 210 | void |
| 211 | WriteOperation::Execute(ProcessMonitor *monitor) |
| 212 | { |
| 213 | const unsigned word_size = monitor->GetProcess().GetAddressByteSize(); |
| 214 | lldb::pid_t pid = monitor->GetPID(); |
| 215 | |
| 216 | m_result = DoWriteMemory(pid, word_size, m_addr, m_buff, m_size, m_error); |
| 217 | } |
| 218 | |
| 219 | //------------------------------------------------------------------------------ |
| 220 | /// @class ReadRegOperation |
| 221 | /// @brief Implements ProcessMonitor::ReadRegisterValue. |
| 222 | class ReadRegOperation : public Operation |
| 223 | { |
| 224 | public: |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 225 | ReadRegOperation(unsigned offset, RegisterValue &value, bool &result) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 226 | : m_offset(offset), m_value(value), m_result(result) |
| 227 | { } |
| 228 | |
| 229 | void Execute(ProcessMonitor *monitor); |
| 230 | |
| 231 | private: |
| 232 | unsigned m_offset; |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 233 | RegisterValue &m_value; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 234 | bool &m_result; |
| 235 | }; |
| 236 | |
| 237 | void |
| 238 | ReadRegOperation::Execute(ProcessMonitor *monitor) |
| 239 | { |
| 240 | lldb::pid_t pid = monitor->GetPID(); |
| 241 | |
| 242 | // Set errno to zero so that we can detect a failed peek. |
| 243 | errno = 0; |
| 244 | unsigned long data = ptrace(PTRACE_PEEKUSER, pid, m_offset, NULL); |
| 245 | |
| 246 | if (data == -1UL && errno) |
| 247 | m_result = false; |
| 248 | else |
| 249 | { |
| 250 | m_value = data; |
| 251 | m_result = true; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | //------------------------------------------------------------------------------ |
| 256 | /// @class WriteRegOperation |
| 257 | /// @brief Implements ProcessMonitor::WriteRegisterValue. |
| 258 | class WriteRegOperation : public Operation |
| 259 | { |
| 260 | public: |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 261 | WriteRegOperation(unsigned offset, const RegisterValue &value, bool &result) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 262 | : m_offset(offset), m_value(value), m_result(result) |
| 263 | { } |
| 264 | |
| 265 | void Execute(ProcessMonitor *monitor); |
| 266 | |
| 267 | private: |
| 268 | unsigned m_offset; |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 269 | const RegisterValue &m_value; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 270 | bool &m_result; |
| 271 | }; |
| 272 | |
| 273 | void |
| 274 | WriteRegOperation::Execute(ProcessMonitor *monitor) |
| 275 | { |
| 276 | lldb::pid_t pid = monitor->GetPID(); |
| 277 | |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 278 | if (ptrace(PTRACE_POKEUSER, pid, m_offset, m_value.GetAsUInt64())) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 279 | m_result = false; |
| 280 | else |
| 281 | m_result = true; |
| 282 | } |
| 283 | |
| 284 | //------------------------------------------------------------------------------ |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 285 | /// @class ReadGPROperation |
| 286 | /// @brief Implements ProcessMonitor::ReadGPR. |
| 287 | class ReadGPROperation : public Operation |
| 288 | { |
| 289 | public: |
| 290 | ReadGPROperation(void *buf, bool &result) |
| 291 | : m_buf(buf), m_result(result) |
| 292 | { } |
| 293 | |
| 294 | void Execute(ProcessMonitor *monitor); |
| 295 | |
| 296 | private: |
| 297 | void *m_buf; |
| 298 | bool &m_result; |
| 299 | }; |
| 300 | |
| 301 | void |
| 302 | ReadGPROperation::Execute(ProcessMonitor *monitor) |
| 303 | { |
| 304 | if (ptrace(PTRACE_GETREGS, monitor->GetPID(), NULL, m_buf) < 0) |
| 305 | m_result = false; |
| 306 | else |
| 307 | m_result = true; |
| 308 | } |
| 309 | |
| 310 | //------------------------------------------------------------------------------ |
| 311 | /// @class ReadFPROperation |
| 312 | /// @brief Implements ProcessMonitor::ReadFPR. |
| 313 | class ReadFPROperation : public Operation |
| 314 | { |
| 315 | public: |
| 316 | ReadFPROperation(void *buf, bool &result) |
| 317 | : m_buf(buf), m_result(result) |
| 318 | { } |
| 319 | |
| 320 | void Execute(ProcessMonitor *monitor); |
| 321 | |
| 322 | private: |
| 323 | void *m_buf; |
| 324 | bool &m_result; |
| 325 | }; |
| 326 | |
| 327 | void |
| 328 | ReadFPROperation::Execute(ProcessMonitor *monitor) |
| 329 | { |
| 330 | if (ptrace(PTRACE_GETFPREGS, monitor->GetPID(), NULL, m_buf) < 0) |
| 331 | m_result = false; |
| 332 | else |
| 333 | m_result = true; |
| 334 | } |
| 335 | |
| 336 | //------------------------------------------------------------------------------ |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 337 | /// @class WriteGPROperation |
| 338 | /// @brief Implements ProcessMonitor::WriteGPR. |
| 339 | class WriteGPROperation : public Operation |
| 340 | { |
| 341 | public: |
| 342 | WriteGPROperation(void *buf, bool &result) |
| 343 | : m_buf(buf), m_result(result) |
| 344 | { } |
| 345 | |
| 346 | void Execute(ProcessMonitor *monitor); |
| 347 | |
| 348 | private: |
| 349 | void *m_buf; |
| 350 | bool &m_result; |
| 351 | }; |
| 352 | |
| 353 | void |
| 354 | WriteGPROperation::Execute(ProcessMonitor *monitor) |
| 355 | { |
| 356 | if (ptrace(PTRACE_SETREGS, monitor->GetPID(), NULL, m_buf) < 0) |
| 357 | m_result = false; |
| 358 | else |
| 359 | m_result = true; |
| 360 | } |
| 361 | |
| 362 | //------------------------------------------------------------------------------ |
| 363 | /// @class WriteFPROperation |
| 364 | /// @brief Implements ProcessMonitor::WriteFPR. |
| 365 | class WriteFPROperation : public Operation |
| 366 | { |
| 367 | public: |
| 368 | WriteFPROperation(void *buf, bool &result) |
| 369 | : m_buf(buf), m_result(result) |
| 370 | { } |
| 371 | |
| 372 | void Execute(ProcessMonitor *monitor); |
| 373 | |
| 374 | private: |
| 375 | void *m_buf; |
| 376 | bool &m_result; |
| 377 | }; |
| 378 | |
| 379 | void |
| 380 | WriteFPROperation::Execute(ProcessMonitor *monitor) |
| 381 | { |
| 382 | if (ptrace(PTRACE_SETFPREGS, monitor->GetPID(), NULL, m_buf) < 0) |
| 383 | m_result = false; |
| 384 | else |
| 385 | m_result = true; |
| 386 | } |
| 387 | |
| 388 | //------------------------------------------------------------------------------ |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 389 | /// @class ResumeOperation |
| 390 | /// @brief Implements ProcessMonitor::Resume. |
| 391 | class ResumeOperation : public Operation |
| 392 | { |
| 393 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 394 | ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) : |
| 395 | m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 396 | |
| 397 | void Execute(ProcessMonitor *monitor); |
| 398 | |
| 399 | private: |
| 400 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 401 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 402 | bool &m_result; |
| 403 | }; |
| 404 | |
| 405 | void |
| 406 | ResumeOperation::Execute(ProcessMonitor *monitor) |
| 407 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 408 | int data = 0; |
| 409 | |
| 410 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 411 | data = m_signo; |
| 412 | |
| 413 | if (ptrace(PTRACE_CONT, m_tid, NULL, data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 414 | m_result = false; |
| 415 | else |
| 416 | m_result = true; |
| 417 | } |
| 418 | |
| 419 | //------------------------------------------------------------------------------ |
| 420 | /// @class ResumeOperation |
| 421 | /// @brief Implements ProcessMonitor::SingleStep. |
| 422 | class SingleStepOperation : public Operation |
| 423 | { |
| 424 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 425 | SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result) |
| 426 | : m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 427 | |
| 428 | void Execute(ProcessMonitor *monitor); |
| 429 | |
| 430 | private: |
| 431 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 432 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 433 | bool &m_result; |
| 434 | }; |
| 435 | |
| 436 | void |
| 437 | SingleStepOperation::Execute(ProcessMonitor *monitor) |
| 438 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 439 | int data = 0; |
| 440 | |
| 441 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 442 | data = m_signo; |
| 443 | |
| 444 | if (ptrace(PTRACE_SINGLESTEP, m_tid, NULL, data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 445 | m_result = false; |
| 446 | else |
| 447 | m_result = true; |
| 448 | } |
| 449 | |
| 450 | //------------------------------------------------------------------------------ |
| 451 | /// @class SiginfoOperation |
| 452 | /// @brief Implements ProcessMonitor::GetSignalInfo. |
| 453 | class SiginfoOperation : public Operation |
| 454 | { |
| 455 | public: |
| 456 | SiginfoOperation(lldb::tid_t tid, void *info, bool &result) |
| 457 | : m_tid(tid), m_info(info), m_result(result) { } |
| 458 | |
| 459 | void Execute(ProcessMonitor *monitor); |
| 460 | |
| 461 | private: |
| 462 | lldb::tid_t m_tid; |
| 463 | void *m_info; |
| 464 | bool &m_result; |
| 465 | }; |
| 466 | |
| 467 | void |
| 468 | SiginfoOperation::Execute(ProcessMonitor *monitor) |
| 469 | { |
| 470 | if (ptrace(PTRACE_GETSIGINFO, m_tid, NULL, m_info)) |
| 471 | m_result = false; |
| 472 | else |
| 473 | m_result = true; |
| 474 | } |
| 475 | |
| 476 | //------------------------------------------------------------------------------ |
| 477 | /// @class EventMessageOperation |
| 478 | /// @brief Implements ProcessMonitor::GetEventMessage. |
| 479 | class EventMessageOperation : public Operation |
| 480 | { |
| 481 | public: |
| 482 | EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) |
| 483 | : m_tid(tid), m_message(message), m_result(result) { } |
| 484 | |
| 485 | void Execute(ProcessMonitor *monitor); |
| 486 | |
| 487 | private: |
| 488 | lldb::tid_t m_tid; |
| 489 | unsigned long *m_message; |
| 490 | bool &m_result; |
| 491 | }; |
| 492 | |
| 493 | void |
| 494 | EventMessageOperation::Execute(ProcessMonitor *monitor) |
| 495 | { |
| 496 | if (ptrace(PTRACE_GETEVENTMSG, m_tid, NULL, m_message)) |
| 497 | m_result = false; |
| 498 | else |
| 499 | m_result = true; |
| 500 | } |
| 501 | |
| 502 | //------------------------------------------------------------------------------ |
| 503 | /// @class KillOperation |
| 504 | /// @brief Implements ProcessMonitor::BringProcessIntoLimbo. |
| 505 | class KillOperation : public Operation |
| 506 | { |
| 507 | public: |
| 508 | KillOperation(bool &result) : m_result(result) { } |
| 509 | |
| 510 | void Execute(ProcessMonitor *monitor); |
| 511 | |
| 512 | private: |
| 513 | bool &m_result; |
| 514 | }; |
| 515 | |
| 516 | void |
| 517 | KillOperation::Execute(ProcessMonitor *monitor) |
| 518 | { |
| 519 | lldb::pid_t pid = monitor->GetPID(); |
| 520 | |
| 521 | if (ptrace(PTRACE_KILL, pid, NULL, NULL)) |
| 522 | m_result = false; |
| 523 | else |
| 524 | m_result = true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 527 | ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor) |
| 528 | : m_monitor(monitor) |
| 529 | { |
| 530 | sem_init(&m_semaphore, 0, 0); |
| 531 | } |
| 532 | |
| 533 | ProcessMonitor::OperationArgs::~OperationArgs() |
| 534 | { |
| 535 | sem_destroy(&m_semaphore); |
| 536 | } |
| 537 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 538 | ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, |
| 539 | lldb_private::Module *module, |
| 540 | char const **argv, |
| 541 | char const **envp, |
| 542 | const char *stdin_path, |
| 543 | const char *stdout_path, |
| 544 | const char *stderr_path) |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 545 | : OperationArgs(monitor), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 546 | m_module(module), |
| 547 | m_argv(argv), |
| 548 | m_envp(envp), |
| 549 | m_stdin_path(stdin_path), |
| 550 | m_stdout_path(stdout_path), |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 551 | m_stderr_path(stderr_path) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 552 | |
| 553 | ProcessMonitor::LaunchArgs::~LaunchArgs() |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 554 | { } |
| 555 | |
| 556 | ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, |
| 557 | lldb::pid_t pid) |
| 558 | : OperationArgs(monitor), m_pid(pid) { } |
| 559 | |
| 560 | ProcessMonitor::AttachArgs::~AttachArgs() |
| 561 | { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 562 | |
| 563 | //------------------------------------------------------------------------------ |
| 564 | /// The basic design of the ProcessMonitor is built around two threads. |
| 565 | /// |
| 566 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 567 | /// for changes in the debugee state. When a change is detected a |
| 568 | /// ProcessMessage is sent to the associated ProcessLinux instance. This thread |
| 569 | /// "drives" state changes in the debugger. |
| 570 | /// |
| 571 | /// The second thread (@see OperationThread) is responsible for two things 1) |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 572 | /// launching or attaching to the inferior process, and then 2) servicing |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 573 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 574 | /// on the Operation class for more info as to why this is needed. |
| 575 | ProcessMonitor::ProcessMonitor(ProcessLinux *process, |
| 576 | Module *module, |
| 577 | const char *argv[], |
| 578 | const char *envp[], |
| 579 | const char *stdin_path, |
| 580 | const char *stdout_path, |
| 581 | const char *stderr_path, |
| 582 | lldb_private::Error &error) |
| 583 | : m_process(process), |
| 584 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
| 585 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 586 | m_terminal_fd(-1), |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 587 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 588 | m_client_fd(-1), |
| 589 | m_server_fd(-1) |
| 590 | { |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 591 | std::auto_ptr<LaunchArgs> args; |
| 592 | |
| 593 | args.reset(new LaunchArgs(this, module, argv, envp, |
| 594 | stdin_path, stdout_path, stderr_path)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 595 | |
| 596 | // Server/client descriptors. |
| 597 | if (!EnableIPC()) |
| 598 | { |
| 599 | error.SetErrorToGenericError(); |
| 600 | error.SetErrorString("Monitor failed to initialize."); |
| 601 | } |
| 602 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 603 | StartLaunchOpThread(args.get(), error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 604 | if (!error.Success()) |
| 605 | return; |
| 606 | |
| 607 | WAIT_AGAIN: |
| 608 | // Wait for the operation thread to initialize. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 609 | if (sem_wait(&args->m_semaphore)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 610 | { |
| 611 | if (errno == EINTR) |
| 612 | goto WAIT_AGAIN; |
| 613 | else |
| 614 | { |
| 615 | error.SetErrorToErrno(); |
| 616 | return; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Check that the launch was a success. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 621 | if (!args->m_error.Success()) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 622 | { |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 623 | StopLaunchOpThread(); |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 624 | error = args->m_error; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
| 628 | // Finally, start monitoring the child process for change in state. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 629 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 630 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 631 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 632 | { |
| 633 | error.SetErrorToGenericError(); |
| 634 | error.SetErrorString("Process launch failed."); |
| 635 | return; |
| 636 | } |
| 637 | } |
| 638 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 639 | ProcessMonitor::ProcessMonitor(ProcessLinux *process, |
| 640 | lldb::pid_t pid, |
| 641 | lldb_private::Error &error) |
| 642 | : m_process(process), |
| 643 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
| 644 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 645 | m_terminal_fd(-1), |
| 646 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
| 647 | m_client_fd(-1), |
| 648 | m_server_fd(-1) |
| 649 | { |
| 650 | std::auto_ptr<AttachArgs> args; |
| 651 | |
| 652 | args.reset(new AttachArgs(this, pid)); |
| 653 | |
| 654 | // Server/client descriptors. |
| 655 | if (!EnableIPC()) |
| 656 | { |
| 657 | error.SetErrorToGenericError(); |
| 658 | error.SetErrorString("Monitor failed to initialize."); |
| 659 | } |
| 660 | |
| 661 | StartAttachOpThread(args.get(), error); |
| 662 | if (!error.Success()) |
| 663 | return; |
| 664 | |
| 665 | WAIT_AGAIN: |
| 666 | // Wait for the operation thread to initialize. |
| 667 | if (sem_wait(&args->m_semaphore)) |
| 668 | { |
| 669 | if (errno == EINTR) |
| 670 | goto WAIT_AGAIN; |
| 671 | else |
| 672 | { |
| 673 | error.SetErrorToErrno(); |
| 674 | return; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // Check that the launch was a success. |
| 679 | if (!args->m_error.Success()) |
| 680 | { |
| 681 | StopAttachOpThread(); |
| 682 | error = args->m_error; |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | // Finally, start monitoring the child process for change in state. |
| 687 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 688 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
| 689 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
| 690 | { |
| 691 | error.SetErrorToGenericError(); |
| 692 | error.SetErrorString("Process attach failed."); |
| 693 | return; |
| 694 | } |
| 695 | } |
| 696 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 697 | ProcessMonitor::~ProcessMonitor() |
| 698 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 699 | StopMonitor(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | //------------------------------------------------------------------------------ |
| 703 | // Thread setup and tear down. |
| 704 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 705 | ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 706 | { |
| 707 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 708 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 709 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 710 | return; |
| 711 | |
| 712 | m_operation_thread = |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 713 | Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 717 | ProcessMonitor::StopLaunchOpThread() |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 718 | { |
| 719 | lldb::thread_result_t result; |
| 720 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 721 | if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 722 | return; |
| 723 | |
| 724 | Host::ThreadCancel(m_operation_thread, NULL); |
| 725 | Host::ThreadJoin(m_operation_thread, &result, NULL); |
| 726 | } |
| 727 | |
| 728 | void * |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 729 | ProcessMonitor::LaunchOpThread(void *arg) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 730 | { |
| 731 | LaunchArgs *args = static_cast<LaunchArgs*>(arg); |
| 732 | |
Peter Collingbourne | 4aeb47e | 2011-06-14 03:55:49 +0000 | [diff] [blame] | 733 | if (!Launch(args)) { |
| 734 | sem_post(&args->m_semaphore); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 735 | return NULL; |
Peter Collingbourne | 4aeb47e | 2011-06-14 03:55:49 +0000 | [diff] [blame] | 736 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 737 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 738 | ServeOperation(args); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | bool |
| 743 | ProcessMonitor::Launch(LaunchArgs *args) |
| 744 | { |
| 745 | ProcessMonitor *monitor = args->m_monitor; |
| 746 | ProcessLinux &process = monitor->GetProcess(); |
| 747 | const char **argv = args->m_argv; |
| 748 | const char **envp = args->m_envp; |
| 749 | const char *stdin_path = args->m_stdin_path; |
| 750 | const char *stdout_path = args->m_stdout_path; |
| 751 | const char *stderr_path = args->m_stderr_path; |
| 752 | |
| 753 | lldb_utility::PseudoTerminal terminal; |
| 754 | const size_t err_len = 1024; |
| 755 | char err_str[err_len]; |
| 756 | lldb::pid_t pid; |
| 757 | |
| 758 | lldb::ThreadSP inferior; |
| 759 | |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 760 | // Propagate the environment if one is not supplied. |
| 761 | if (envp == NULL || envp[0] == NULL) |
| 762 | envp = const_cast<const char **>(environ); |
| 763 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 764 | // Pseudo terminal setup. |
| 765 | if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len)) |
| 766 | { |
| 767 | args->m_error.SetErrorToGenericError(); |
| 768 | args->m_error.SetErrorString("Could not open controlling TTY."); |
| 769 | goto FINISH; |
| 770 | } |
| 771 | |
| 772 | if ((pid = terminal.Fork(err_str, err_len)) < 0) |
| 773 | { |
| 774 | args->m_error.SetErrorToGenericError(); |
| 775 | args->m_error.SetErrorString("Process fork failed."); |
| 776 | goto FINISH; |
| 777 | } |
| 778 | |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 779 | // Recognized child exit status codes. |
| 780 | enum { |
| 781 | ePtraceFailed = 1, |
| 782 | eDupStdinFailed, |
| 783 | eDupStdoutFailed, |
| 784 | eDupStderrFailed, |
| 785 | eExecFailed |
| 786 | }; |
| 787 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 788 | // Child process. |
| 789 | if (pid == 0) |
| 790 | { |
| 791 | // Trace this process. |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 792 | if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0) |
| 793 | exit(ePtraceFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 794 | |
| 795 | // Do not inherit setgid powers. |
| 796 | setgid(getgid()); |
| 797 | |
| 798 | // Let us have our own process group. |
| 799 | setpgid(0, 0); |
| 800 | |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 801 | // Dup file descriptors if needed. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 802 | // |
| 803 | // FIXME: If two or more of the paths are the same we needlessly open |
| 804 | // the same file multiple times. |
| 805 | if (stdin_path != NULL && stdin_path[0]) |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 806 | if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 807 | exit(eDupStdinFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 808 | |
| 809 | if (stdout_path != NULL && stdout_path[0]) |
| 810 | if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 811 | exit(eDupStdoutFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 812 | |
| 813 | if (stderr_path != NULL && stderr_path[0]) |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 814 | if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 815 | exit(eDupStderrFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 816 | |
| 817 | // Execute. We should never return. |
| 818 | execve(argv[0], |
| 819 | const_cast<char *const *>(argv), |
| 820 | const_cast<char *const *>(envp)); |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 821 | exit(eExecFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | // Wait for the child process to to trap on its call to execve. |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 825 | pid_t wpid; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 826 | int status; |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 827 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 828 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 829 | args->m_error.SetErrorToErrno(); |
| 830 | goto FINISH; |
| 831 | } |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 832 | else if (WIFEXITED(status)) |
| 833 | { |
| 834 | // open, dup or execve likely failed for some reason. |
| 835 | args->m_error.SetErrorToGenericError(); |
| 836 | switch (WEXITSTATUS(status)) |
| 837 | { |
| 838 | case ePtraceFailed: |
| 839 | args->m_error.SetErrorString("Child ptrace failed."); |
| 840 | break; |
| 841 | case eDupStdinFailed: |
| 842 | args->m_error.SetErrorString("Child open stdin failed."); |
| 843 | break; |
| 844 | case eDupStdoutFailed: |
| 845 | args->m_error.SetErrorString("Child open stdout failed."); |
| 846 | break; |
| 847 | case eDupStderrFailed: |
| 848 | args->m_error.SetErrorString("Child open stderr failed."); |
| 849 | break; |
| 850 | case eExecFailed: |
| 851 | args->m_error.SetErrorString("Child exec failed."); |
| 852 | break; |
| 853 | default: |
| 854 | args->m_error.SetErrorString("Child returned unknown exit status."); |
| 855 | break; |
| 856 | } |
| 857 | goto FINISH; |
| 858 | } |
| 859 | assert(WIFSTOPPED(status) && wpid == pid && |
| 860 | "Could not sync with inferior process."); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 861 | |
| 862 | // Have the child raise an event on exit. This is used to keep the child in |
| 863 | // limbo until it is destroyed. |
| 864 | if (ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) |
| 865 | { |
| 866 | args->m_error.SetErrorToErrno(); |
| 867 | goto FINISH; |
| 868 | } |
| 869 | |
| 870 | // Release the master terminal descriptor and pass it off to the |
| 871 | // ProcessMonitor instance. Similarly stash the inferior pid. |
| 872 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 873 | monitor->m_pid = pid; |
| 874 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 875 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 876 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 877 | // descriptor to read from). |
| 878 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 879 | goto FINISH; |
| 880 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 881 | // Update the process thread list with this new thread and mark it as |
| 882 | // current. |
| 883 | inferior.reset(new LinuxThread(process, pid)); |
| 884 | process.GetThreadList().AddThread(inferior); |
Stephen Wilson | 5a8feea | 2011-01-04 21:39:27 +0000 | [diff] [blame] | 885 | process.GetThreadList().SetSelectedThreadByID(pid); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 886 | |
| 887 | // Let our process instance know the thread has stopped. |
| 888 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 889 | |
| 890 | FINISH: |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 891 | return args->m_error.Success(); |
| 892 | } |
| 893 | |
| 894 | bool |
| 895 | ProcessMonitor::EnableIPC() |
| 896 | { |
| 897 | int fd[2]; |
| 898 | |
| 899 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) |
| 900 | return false; |
| 901 | |
| 902 | m_client_fd = fd[0]; |
| 903 | m_server_fd = fd[1]; |
| 904 | return true; |
| 905 | } |
| 906 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 907 | void |
| 908 | ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) |
| 909 | { |
| 910 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 911 | |
| 912 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
| 913 | return; |
| 914 | |
| 915 | m_operation_thread = |
| 916 | Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error); |
| 917 | } |
| 918 | |
| 919 | void |
| 920 | ProcessMonitor::StopAttachOpThread() |
| 921 | { |
| 922 | assert(!"Not implemented yet!!!"); |
| 923 | } |
| 924 | |
| 925 | void * |
| 926 | ProcessMonitor::AttachOpThread(void *arg) |
| 927 | { |
| 928 | AttachArgs *args = static_cast<AttachArgs*>(arg); |
| 929 | |
| 930 | if (!Attach(args)) |
| 931 | return NULL; |
| 932 | |
| 933 | ServeOperation(args); |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | bool |
| 938 | ProcessMonitor::Attach(AttachArgs *args) |
| 939 | { |
| 940 | lldb::pid_t pid = args->m_pid; |
| 941 | |
| 942 | ProcessMonitor *monitor = args->m_monitor; |
| 943 | ProcessLinux &process = monitor->GetProcess(); |
| 944 | |
| 945 | lldb::ThreadSP inferior; |
| 946 | |
| 947 | if (pid <= 1) |
| 948 | { |
| 949 | args->m_error.SetErrorToGenericError(); |
| 950 | args->m_error.SetErrorString("Attaching to process 1 is not allowed."); |
| 951 | goto FINISH; |
| 952 | } |
| 953 | |
| 954 | // Attach to the requested process. |
| 955 | if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) |
| 956 | { |
| 957 | args->m_error.SetErrorToErrno(); |
| 958 | goto FINISH; |
| 959 | } |
| 960 | |
| 961 | int status; |
| 962 | if ((status = waitpid(pid, NULL, 0)) < 0) |
| 963 | { |
| 964 | args->m_error.SetErrorToErrno(); |
| 965 | goto FINISH; |
| 966 | } |
| 967 | |
| 968 | // Update the process thread list with the attached thread and |
| 969 | // mark it as current. |
| 970 | inferior.reset(new LinuxThread(process, pid)); |
| 971 | process.GetThreadList().AddThread(inferior); |
| 972 | process.GetThreadList().SetSelectedThreadByID(pid); |
| 973 | |
| 974 | // Let our process instance know the thread has stopped. |
| 975 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 976 | |
| 977 | FINISH: |
| 978 | return args->m_error.Success(); |
| 979 | } |
| 980 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 981 | bool |
| 982 | ProcessMonitor::MonitorCallback(void *callback_baton, |
| 983 | lldb::pid_t pid, |
| 984 | int signal, |
| 985 | int status) |
| 986 | { |
| 987 | ProcessMessage message; |
| 988 | ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton); |
| 989 | ProcessLinux *process = monitor->m_process; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 990 | bool stop_monitoring; |
| 991 | siginfo_t info; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 992 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 993 | if (!monitor->GetSignalInfo(pid, &info)) |
| 994 | stop_monitoring = true; // pid is gone. Bail. |
| 995 | else { |
| 996 | switch (info.si_signo) |
| 997 | { |
| 998 | case SIGTRAP: |
| 999 | message = MonitorSIGTRAP(monitor, &info, pid); |
| 1000 | break; |
| 1001 | |
| 1002 | default: |
| 1003 | message = MonitorSignal(monitor, &info, pid); |
| 1004 | break; |
| 1005 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1006 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1007 | process->SendMessage(message); |
| 1008 | stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1011 | return stop_monitoring; |
| 1012 | } |
| 1013 | |
| 1014 | ProcessMessage |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1015 | ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, |
| 1016 | const struct siginfo *info, lldb::pid_t pid) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1017 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1018 | ProcessMessage message; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1019 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1020 | assert(info->si_signo == SIGTRAP && "Unexpected child signal!"); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1021 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1022 | switch (info->si_code) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1023 | { |
| 1024 | default: |
| 1025 | assert(false && "Unexpected SIGTRAP code!"); |
| 1026 | break; |
| 1027 | |
| 1028 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 1029 | { |
| 1030 | // The inferior process is about to exit. Maintain the process in a |
| 1031 | // state of "limbo" until we are explicitly commanded to detach, |
| 1032 | // destroy, resume, etc. |
| 1033 | unsigned long data = 0; |
| 1034 | if (!monitor->GetEventMessage(pid, &data)) |
| 1035 | data = -1; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1036 | message = ProcessMessage::Limbo(pid, (data >> 8)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | case 0: |
| 1041 | case TRAP_TRACE: |
| 1042 | message = ProcessMessage::Trace(pid); |
| 1043 | break; |
| 1044 | |
| 1045 | case SI_KERNEL: |
| 1046 | case TRAP_BRKPT: |
| 1047 | message = ProcessMessage::Break(pid); |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | return message; |
| 1052 | } |
| 1053 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1054 | ProcessMessage |
| 1055 | ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, |
| 1056 | const struct siginfo *info, lldb::pid_t pid) |
| 1057 | { |
| 1058 | ProcessMessage message; |
| 1059 | int signo = info->si_signo; |
| 1060 | |
| 1061 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 1062 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 1063 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 1064 | // |
| 1065 | // IOW, user generated signals never generate what we consider to be a |
| 1066 | // "crash". |
| 1067 | // |
| 1068 | // Similarly, ACK signals generated by this monitor. |
| 1069 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 1070 | { |
| 1071 | if (info->si_pid == getpid()) |
| 1072 | return ProcessMessage::SignalDelivered(pid, signo); |
| 1073 | else |
| 1074 | return ProcessMessage::Signal(pid, signo); |
| 1075 | } |
| 1076 | |
| 1077 | if (signo == SIGSEGV) { |
| 1078 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1079 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info); |
| 1080 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1081 | } |
| 1082 | |
| 1083 | if (signo == SIGILL) { |
| 1084 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1085 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info); |
| 1086 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1087 | } |
| 1088 | |
| 1089 | if (signo == SIGFPE) { |
| 1090 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1091 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info); |
| 1092 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1093 | } |
| 1094 | |
| 1095 | if (signo == SIGBUS) { |
| 1096 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1097 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info); |
| 1098 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1099 | } |
| 1100 | |
| 1101 | // Everything else is "normal" and does not require any special action on |
| 1102 | // our part. |
| 1103 | return ProcessMessage::Signal(pid, signo); |
| 1104 | } |
| 1105 | |
| 1106 | ProcessMessage::CrashReason |
| 1107 | ProcessMonitor::GetCrashReasonForSIGSEGV(const struct siginfo *info) |
| 1108 | { |
| 1109 | ProcessMessage::CrashReason reason; |
| 1110 | assert(info->si_signo == SIGSEGV); |
| 1111 | |
| 1112 | reason = ProcessMessage::eInvalidCrashReason; |
| 1113 | |
| 1114 | switch (info->si_code) |
| 1115 | { |
| 1116 | default: |
| 1117 | assert(false && "unexpected si_code for SIGSEGV"); |
| 1118 | break; |
| 1119 | case SEGV_MAPERR: |
| 1120 | reason = ProcessMessage::eInvalidAddress; |
| 1121 | break; |
| 1122 | case SEGV_ACCERR: |
| 1123 | reason = ProcessMessage::ePrivilegedAddress; |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | return reason; |
| 1128 | } |
| 1129 | |
| 1130 | ProcessMessage::CrashReason |
| 1131 | ProcessMonitor::GetCrashReasonForSIGILL(const struct siginfo *info) |
| 1132 | { |
| 1133 | ProcessMessage::CrashReason reason; |
| 1134 | assert(info->si_signo == SIGILL); |
| 1135 | |
| 1136 | reason = ProcessMessage::eInvalidCrashReason; |
| 1137 | |
| 1138 | switch (info->si_code) |
| 1139 | { |
| 1140 | default: |
| 1141 | assert(false && "unexpected si_code for SIGILL"); |
| 1142 | break; |
| 1143 | case ILL_ILLOPC: |
| 1144 | reason = ProcessMessage::eIllegalOpcode; |
| 1145 | break; |
| 1146 | case ILL_ILLOPN: |
| 1147 | reason = ProcessMessage::eIllegalOperand; |
| 1148 | break; |
| 1149 | case ILL_ILLADR: |
| 1150 | reason = ProcessMessage::eIllegalAddressingMode; |
| 1151 | break; |
| 1152 | case ILL_ILLTRP: |
| 1153 | reason = ProcessMessage::eIllegalTrap; |
| 1154 | break; |
| 1155 | case ILL_PRVOPC: |
| 1156 | reason = ProcessMessage::ePrivilegedOpcode; |
| 1157 | break; |
| 1158 | case ILL_PRVREG: |
| 1159 | reason = ProcessMessage::ePrivilegedRegister; |
| 1160 | break; |
| 1161 | case ILL_COPROC: |
| 1162 | reason = ProcessMessage::eCoprocessorError; |
| 1163 | break; |
| 1164 | case ILL_BADSTK: |
| 1165 | reason = ProcessMessage::eInternalStackError; |
| 1166 | break; |
| 1167 | } |
| 1168 | |
| 1169 | return reason; |
| 1170 | } |
| 1171 | |
| 1172 | ProcessMessage::CrashReason |
| 1173 | ProcessMonitor::GetCrashReasonForSIGFPE(const struct siginfo *info) |
| 1174 | { |
| 1175 | ProcessMessage::CrashReason reason; |
| 1176 | assert(info->si_signo == SIGFPE); |
| 1177 | |
| 1178 | reason = ProcessMessage::eInvalidCrashReason; |
| 1179 | |
| 1180 | switch (info->si_code) |
| 1181 | { |
| 1182 | default: |
| 1183 | assert(false && "unexpected si_code for SIGFPE"); |
| 1184 | break; |
| 1185 | case FPE_INTDIV: |
| 1186 | reason = ProcessMessage::eIntegerDivideByZero; |
| 1187 | break; |
| 1188 | case FPE_INTOVF: |
| 1189 | reason = ProcessMessage::eIntegerOverflow; |
| 1190 | break; |
| 1191 | case FPE_FLTDIV: |
| 1192 | reason = ProcessMessage::eFloatDivideByZero; |
| 1193 | break; |
| 1194 | case FPE_FLTOVF: |
| 1195 | reason = ProcessMessage::eFloatOverflow; |
| 1196 | break; |
| 1197 | case FPE_FLTUND: |
| 1198 | reason = ProcessMessage::eFloatUnderflow; |
| 1199 | break; |
| 1200 | case FPE_FLTRES: |
| 1201 | reason = ProcessMessage::eFloatInexactResult; |
| 1202 | break; |
| 1203 | case FPE_FLTINV: |
| 1204 | reason = ProcessMessage::eFloatInvalidOperation; |
| 1205 | break; |
| 1206 | case FPE_FLTSUB: |
| 1207 | reason = ProcessMessage::eFloatSubscriptRange; |
| 1208 | break; |
| 1209 | } |
| 1210 | |
| 1211 | return reason; |
| 1212 | } |
| 1213 | |
| 1214 | ProcessMessage::CrashReason |
| 1215 | ProcessMonitor::GetCrashReasonForSIGBUS(const struct siginfo *info) |
| 1216 | { |
| 1217 | ProcessMessage::CrashReason reason; |
| 1218 | assert(info->si_signo == SIGBUS); |
| 1219 | |
| 1220 | reason = ProcessMessage::eInvalidCrashReason; |
| 1221 | |
| 1222 | switch (info->si_code) |
| 1223 | { |
| 1224 | default: |
| 1225 | assert(false && "unexpected si_code for SIGBUS"); |
| 1226 | break; |
| 1227 | case BUS_ADRALN: |
| 1228 | reason = ProcessMessage::eIllegalAlignment; |
| 1229 | break; |
| 1230 | case BUS_ADRERR: |
| 1231 | reason = ProcessMessage::eIllegalAddress; |
| 1232 | break; |
| 1233 | case BUS_OBJERR: |
| 1234 | reason = ProcessMessage::eHardwareError; |
| 1235 | break; |
| 1236 | } |
| 1237 | |
| 1238 | return reason; |
| 1239 | } |
| 1240 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1241 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 1242 | ProcessMonitor::ServeOperation(OperationArgs *args) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1243 | { |
| 1244 | int status; |
| 1245 | pollfd fdset; |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 1246 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1247 | ProcessMonitor *monitor = args->m_monitor; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1248 | |
| 1249 | fdset.fd = monitor->m_server_fd; |
| 1250 | fdset.events = POLLIN | POLLPRI; |
| 1251 | fdset.revents = 0; |
| 1252 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1253 | // We are finised with the arguments and are ready to go. Sync with the |
| 1254 | // parent thread and start serving operations on the inferior. |
| 1255 | sem_post(&args->m_semaphore); |
| 1256 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1257 | for (;;) |
| 1258 | { |
| 1259 | if ((status = poll(&fdset, 1, -1)) < 0) |
| 1260 | { |
| 1261 | switch (errno) |
| 1262 | { |
| 1263 | default: |
| 1264 | assert(false && "Unexpected poll() failure!"); |
| 1265 | continue; |
| 1266 | |
| 1267 | case EINTR: continue; // Just poll again. |
| 1268 | case EBADF: return; // Connection terminated. |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | assert(status == 1 && "Too many descriptors!"); |
| 1273 | |
| 1274 | if (fdset.revents & POLLIN) |
| 1275 | { |
| 1276 | Operation *op = NULL; |
| 1277 | |
| 1278 | READ_AGAIN: |
| 1279 | if ((status = read(fdset.fd, &op, sizeof(op))) < 0) |
| 1280 | { |
| 1281 | // There is only one acceptable failure. |
| 1282 | assert(errno == EINTR); |
| 1283 | goto READ_AGAIN; |
| 1284 | } |
| 1285 | |
| 1286 | assert(status == sizeof(op)); |
| 1287 | op->Execute(monitor); |
| 1288 | write(fdset.fd, &op, sizeof(op)); |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | void |
| 1294 | ProcessMonitor::DoOperation(Operation *op) |
| 1295 | { |
| 1296 | int status; |
| 1297 | Operation *ack = NULL; |
| 1298 | Mutex::Locker lock(m_server_mutex); |
| 1299 | |
| 1300 | // FIXME: Do proper error checking here. |
| 1301 | write(m_client_fd, &op, sizeof(op)); |
| 1302 | |
| 1303 | READ_AGAIN: |
| 1304 | if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0) |
| 1305 | { |
| 1306 | // If interrupted by a signal handler try again. Otherwise the monitor |
| 1307 | // thread probably died and we have a stale file descriptor -- abort the |
| 1308 | // operation. |
| 1309 | if (errno == EINTR) |
| 1310 | goto READ_AGAIN; |
| 1311 | return; |
| 1312 | } |
| 1313 | |
| 1314 | assert(status == sizeof(ack)); |
| 1315 | assert(ack == op && "Invalid monitor thread response!"); |
| 1316 | } |
| 1317 | |
| 1318 | size_t |
| 1319 | ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 1320 | Error &error) |
| 1321 | { |
| 1322 | size_t result; |
| 1323 | ReadOperation op(vm_addr, buf, size, error, result); |
| 1324 | DoOperation(&op); |
| 1325 | return result; |
| 1326 | } |
| 1327 | |
| 1328 | size_t |
| 1329 | ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 1330 | lldb_private::Error &error) |
| 1331 | { |
| 1332 | size_t result; |
| 1333 | WriteOperation op(vm_addr, buf, size, error, result); |
| 1334 | DoOperation(&op); |
| 1335 | return result; |
| 1336 | } |
| 1337 | |
| 1338 | bool |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 1339 | ProcessMonitor::ReadRegisterValue(unsigned offset, RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1340 | { |
| 1341 | bool result; |
| 1342 | ReadRegOperation op(offset, value, result); |
| 1343 | DoOperation(&op); |
| 1344 | return result; |
| 1345 | } |
| 1346 | |
| 1347 | bool |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 1348 | ProcessMonitor::WriteRegisterValue(unsigned offset, const RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1349 | { |
| 1350 | bool result; |
| 1351 | WriteRegOperation op(offset, value, result); |
| 1352 | DoOperation(&op); |
| 1353 | return result; |
| 1354 | } |
| 1355 | |
| 1356 | bool |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1357 | ProcessMonitor::ReadGPR(void *buf) |
| 1358 | { |
| 1359 | bool result; |
| 1360 | ReadGPROperation op(buf, result); |
| 1361 | DoOperation(&op); |
| 1362 | return result; |
| 1363 | } |
| 1364 | |
| 1365 | bool |
| 1366 | ProcessMonitor::ReadFPR(void *buf) |
| 1367 | { |
| 1368 | bool result; |
| 1369 | ReadFPROperation op(buf, result); |
| 1370 | DoOperation(&op); |
| 1371 | return result; |
| 1372 | } |
| 1373 | |
| 1374 | bool |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1375 | ProcessMonitor::WriteGPR(void *buf) |
| 1376 | { |
| 1377 | bool result; |
| 1378 | WriteGPROperation op(buf, result); |
| 1379 | DoOperation(&op); |
| 1380 | return result; |
| 1381 | } |
| 1382 | |
| 1383 | bool |
| 1384 | ProcessMonitor::WriteFPR(void *buf) |
| 1385 | { |
| 1386 | bool result; |
| 1387 | WriteFPROperation op(buf, result); |
| 1388 | DoOperation(&op); |
| 1389 | return result; |
| 1390 | } |
| 1391 | |
| 1392 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1393 | ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1394 | { |
| 1395 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1396 | ResumeOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1397 | DoOperation(&op); |
| 1398 | return result; |
| 1399 | } |
| 1400 | |
| 1401 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1402 | ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1403 | { |
| 1404 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1405 | SingleStepOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1406 | DoOperation(&op); |
| 1407 | return result; |
| 1408 | } |
| 1409 | |
| 1410 | bool |
| 1411 | ProcessMonitor::BringProcessIntoLimbo() |
| 1412 | { |
| 1413 | bool result; |
| 1414 | KillOperation op(result); |
| 1415 | DoOperation(&op); |
| 1416 | return result; |
| 1417 | } |
| 1418 | |
| 1419 | bool |
| 1420 | ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo) |
| 1421 | { |
| 1422 | bool result; |
| 1423 | SiginfoOperation op(tid, siginfo, result); |
| 1424 | DoOperation(&op); |
| 1425 | return result; |
| 1426 | } |
| 1427 | |
| 1428 | bool |
| 1429 | ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 1430 | { |
| 1431 | bool result; |
| 1432 | EventMessageOperation op(tid, message, result); |
| 1433 | DoOperation(&op); |
| 1434 | return result; |
| 1435 | } |
| 1436 | |
| 1437 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1438 | ProcessMonitor::Detach() |
| 1439 | { |
| 1440 | bool result; |
| 1441 | KillOperation op(result); |
| 1442 | DoOperation(&op); |
| 1443 | StopMonitor(); |
| 1444 | return result; |
| 1445 | } |
| 1446 | |
| 1447 | bool |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1448 | ProcessMonitor::DupDescriptor(const char *path, int fd, int flags) |
| 1449 | { |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1450 | int target_fd = open(path, flags, 0666); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1451 | |
| 1452 | if (target_fd == -1) |
| 1453 | return false; |
| 1454 | |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1455 | return (dup2(target_fd, fd) == -1) ? false : true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1456 | } |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1457 | |
| 1458 | void |
| 1459 | ProcessMonitor::StopMonitoringChildProcess() |
| 1460 | { |
| 1461 | lldb::thread_result_t thread_result; |
| 1462 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 1463 | if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1464 | { |
| 1465 | Host::ThreadCancel(m_monitor_thread, NULL); |
| 1466 | Host::ThreadJoin(m_monitor_thread, &thread_result, NULL); |
| 1467 | m_monitor_thread = LLDB_INVALID_HOST_THREAD; |
| 1468 | } |
| 1469 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1470 | |
| 1471 | void |
| 1472 | ProcessMonitor::StopMonitor() |
| 1473 | { |
| 1474 | StopMonitoringChildProcess(); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame^] | 1475 | StopLaunchOpThread(); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1476 | CloseFD(m_terminal_fd); |
| 1477 | CloseFD(m_client_fd); |
| 1478 | CloseFD(m_server_fd); |
| 1479 | } |
| 1480 | |
| 1481 | void |
| 1482 | ProcessMonitor::CloseFD(int &fd) |
| 1483 | { |
| 1484 | if (fd != -1) |
| 1485 | { |
| 1486 | close(fd); |
| 1487 | fd = -1; |
| 1488 | } |
| 1489 | } |