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 | //------------------------------------------------------------------------------ |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 337 | /// @class ResumeOperation |
| 338 | /// @brief Implements ProcessMonitor::Resume. |
| 339 | class ResumeOperation : public Operation |
| 340 | { |
| 341 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 342 | ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) : |
| 343 | m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 344 | |
| 345 | void Execute(ProcessMonitor *monitor); |
| 346 | |
| 347 | private: |
| 348 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 349 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 350 | bool &m_result; |
| 351 | }; |
| 352 | |
| 353 | void |
| 354 | ResumeOperation::Execute(ProcessMonitor *monitor) |
| 355 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 356 | int data = 0; |
| 357 | |
| 358 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 359 | data = m_signo; |
| 360 | |
| 361 | if (ptrace(PTRACE_CONT, m_tid, NULL, data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 362 | m_result = false; |
| 363 | else |
| 364 | m_result = true; |
| 365 | } |
| 366 | |
| 367 | //------------------------------------------------------------------------------ |
| 368 | /// @class ResumeOperation |
| 369 | /// @brief Implements ProcessMonitor::SingleStep. |
| 370 | class SingleStepOperation : public Operation |
| 371 | { |
| 372 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 373 | SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result) |
| 374 | : m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 375 | |
| 376 | void Execute(ProcessMonitor *monitor); |
| 377 | |
| 378 | private: |
| 379 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 380 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 381 | bool &m_result; |
| 382 | }; |
| 383 | |
| 384 | void |
| 385 | SingleStepOperation::Execute(ProcessMonitor *monitor) |
| 386 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 387 | int data = 0; |
| 388 | |
| 389 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 390 | data = m_signo; |
| 391 | |
| 392 | if (ptrace(PTRACE_SINGLESTEP, m_tid, NULL, data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 393 | m_result = false; |
| 394 | else |
| 395 | m_result = true; |
| 396 | } |
| 397 | |
| 398 | //------------------------------------------------------------------------------ |
| 399 | /// @class SiginfoOperation |
| 400 | /// @brief Implements ProcessMonitor::GetSignalInfo. |
| 401 | class SiginfoOperation : public Operation |
| 402 | { |
| 403 | public: |
| 404 | SiginfoOperation(lldb::tid_t tid, void *info, bool &result) |
| 405 | : m_tid(tid), m_info(info), m_result(result) { } |
| 406 | |
| 407 | void Execute(ProcessMonitor *monitor); |
| 408 | |
| 409 | private: |
| 410 | lldb::tid_t m_tid; |
| 411 | void *m_info; |
| 412 | bool &m_result; |
| 413 | }; |
| 414 | |
| 415 | void |
| 416 | SiginfoOperation::Execute(ProcessMonitor *monitor) |
| 417 | { |
| 418 | if (ptrace(PTRACE_GETSIGINFO, m_tid, NULL, m_info)) |
| 419 | m_result = false; |
| 420 | else |
| 421 | m_result = true; |
| 422 | } |
| 423 | |
| 424 | //------------------------------------------------------------------------------ |
| 425 | /// @class EventMessageOperation |
| 426 | /// @brief Implements ProcessMonitor::GetEventMessage. |
| 427 | class EventMessageOperation : public Operation |
| 428 | { |
| 429 | public: |
| 430 | EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) |
| 431 | : m_tid(tid), m_message(message), m_result(result) { } |
| 432 | |
| 433 | void Execute(ProcessMonitor *monitor); |
| 434 | |
| 435 | private: |
| 436 | lldb::tid_t m_tid; |
| 437 | unsigned long *m_message; |
| 438 | bool &m_result; |
| 439 | }; |
| 440 | |
| 441 | void |
| 442 | EventMessageOperation::Execute(ProcessMonitor *monitor) |
| 443 | { |
| 444 | if (ptrace(PTRACE_GETEVENTMSG, m_tid, NULL, m_message)) |
| 445 | m_result = false; |
| 446 | else |
| 447 | m_result = true; |
| 448 | } |
| 449 | |
| 450 | //------------------------------------------------------------------------------ |
| 451 | /// @class KillOperation |
| 452 | /// @brief Implements ProcessMonitor::BringProcessIntoLimbo. |
| 453 | class KillOperation : public Operation |
| 454 | { |
| 455 | public: |
| 456 | KillOperation(bool &result) : m_result(result) { } |
| 457 | |
| 458 | void Execute(ProcessMonitor *monitor); |
| 459 | |
| 460 | private: |
| 461 | bool &m_result; |
| 462 | }; |
| 463 | |
| 464 | void |
| 465 | KillOperation::Execute(ProcessMonitor *monitor) |
| 466 | { |
| 467 | lldb::pid_t pid = monitor->GetPID(); |
| 468 | |
| 469 | if (ptrace(PTRACE_KILL, pid, NULL, NULL)) |
| 470 | m_result = false; |
| 471 | else |
| 472 | m_result = true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, |
| 476 | lldb_private::Module *module, |
| 477 | char const **argv, |
| 478 | char const **envp, |
| 479 | const char *stdin_path, |
| 480 | const char *stdout_path, |
| 481 | const char *stderr_path) |
| 482 | : m_monitor(monitor), |
| 483 | m_module(module), |
| 484 | m_argv(argv), |
| 485 | m_envp(envp), |
| 486 | m_stdin_path(stdin_path), |
| 487 | m_stdout_path(stdout_path), |
| 488 | m_stderr_path(stderr_path) |
| 489 | { |
| 490 | sem_init(&m_semaphore, 0, 0); |
| 491 | } |
| 492 | |
| 493 | ProcessMonitor::LaunchArgs::~LaunchArgs() |
| 494 | { |
| 495 | sem_destroy(&m_semaphore); |
| 496 | } |
| 497 | |
| 498 | //------------------------------------------------------------------------------ |
| 499 | /// The basic design of the ProcessMonitor is built around two threads. |
| 500 | /// |
| 501 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 502 | /// for changes in the debugee state. When a change is detected a |
| 503 | /// ProcessMessage is sent to the associated ProcessLinux instance. This thread |
| 504 | /// "drives" state changes in the debugger. |
| 505 | /// |
| 506 | /// The second thread (@see OperationThread) is responsible for two things 1) |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 507 | /// launching or attaching to the inferior process, and then 2) servicing |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 508 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 509 | /// on the Operation class for more info as to why this is needed. |
| 510 | ProcessMonitor::ProcessMonitor(ProcessLinux *process, |
| 511 | Module *module, |
| 512 | const char *argv[], |
| 513 | const char *envp[], |
| 514 | const char *stdin_path, |
| 515 | const char *stdout_path, |
| 516 | const char *stderr_path, |
| 517 | lldb_private::Error &error) |
| 518 | : m_process(process), |
| 519 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
| 520 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 521 | m_terminal_fd(-1), |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 522 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 523 | m_client_fd(-1), |
| 524 | m_server_fd(-1) |
| 525 | { |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 526 | std::auto_ptr<LaunchArgs> args; |
| 527 | |
| 528 | args.reset(new LaunchArgs(this, module, argv, envp, |
| 529 | stdin_path, stdout_path, stderr_path)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 530 | |
| 531 | // Server/client descriptors. |
| 532 | if (!EnableIPC()) |
| 533 | { |
| 534 | error.SetErrorToGenericError(); |
| 535 | error.SetErrorString("Monitor failed to initialize."); |
| 536 | } |
| 537 | |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 538 | StartOperationThread(args.get(), error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 539 | if (!error.Success()) |
| 540 | return; |
| 541 | |
| 542 | WAIT_AGAIN: |
| 543 | // Wait for the operation thread to initialize. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 544 | if (sem_wait(&args->m_semaphore)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 545 | { |
| 546 | if (errno == EINTR) |
| 547 | goto WAIT_AGAIN; |
| 548 | else |
| 549 | { |
| 550 | error.SetErrorToErrno(); |
| 551 | return; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // Check that the launch was a success. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 556 | if (!args->m_error.Success()) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 557 | { |
| 558 | StopOperationThread(); |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 559 | error = args->m_error; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
| 563 | // Finally, start monitoring the child process for change in state. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 564 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 565 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 566 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 567 | { |
| 568 | error.SetErrorToGenericError(); |
| 569 | error.SetErrorString("Process launch failed."); |
| 570 | return; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | ProcessMonitor::~ProcessMonitor() |
| 575 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 576 | StopMonitor(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | //------------------------------------------------------------------------------ |
| 580 | // Thread setup and tear down. |
| 581 | void |
| 582 | ProcessMonitor::StartOperationThread(LaunchArgs *args, Error &error) |
| 583 | { |
| 584 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 585 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 586 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 587 | return; |
| 588 | |
| 589 | m_operation_thread = |
| 590 | Host::ThreadCreate(g_thread_name, OperationThread, args, &error); |
| 591 | } |
| 592 | |
| 593 | void |
| 594 | ProcessMonitor::StopOperationThread() |
| 595 | { |
| 596 | lldb::thread_result_t result; |
| 597 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 598 | if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 599 | return; |
| 600 | |
| 601 | Host::ThreadCancel(m_operation_thread, NULL); |
| 602 | Host::ThreadJoin(m_operation_thread, &result, NULL); |
| 603 | } |
| 604 | |
| 605 | void * |
| 606 | ProcessMonitor::OperationThread(void *arg) |
| 607 | { |
| 608 | LaunchArgs *args = static_cast<LaunchArgs*>(arg); |
| 609 | |
| 610 | if (!Launch(args)) |
| 611 | return NULL; |
| 612 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 613 | ServeOperation(args); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | bool |
| 618 | ProcessMonitor::Launch(LaunchArgs *args) |
| 619 | { |
| 620 | ProcessMonitor *monitor = args->m_monitor; |
| 621 | ProcessLinux &process = monitor->GetProcess(); |
| 622 | const char **argv = args->m_argv; |
| 623 | const char **envp = args->m_envp; |
| 624 | const char *stdin_path = args->m_stdin_path; |
| 625 | const char *stdout_path = args->m_stdout_path; |
| 626 | const char *stderr_path = args->m_stderr_path; |
| 627 | |
| 628 | lldb_utility::PseudoTerminal terminal; |
| 629 | const size_t err_len = 1024; |
| 630 | char err_str[err_len]; |
| 631 | lldb::pid_t pid; |
| 632 | |
| 633 | lldb::ThreadSP inferior; |
| 634 | |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 635 | // Propagate the environment if one is not supplied. |
| 636 | if (envp == NULL || envp[0] == NULL) |
| 637 | envp = const_cast<const char **>(environ); |
| 638 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 639 | // Pseudo terminal setup. |
| 640 | if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len)) |
| 641 | { |
| 642 | args->m_error.SetErrorToGenericError(); |
| 643 | args->m_error.SetErrorString("Could not open controlling TTY."); |
| 644 | goto FINISH; |
| 645 | } |
| 646 | |
| 647 | if ((pid = terminal.Fork(err_str, err_len)) < 0) |
| 648 | { |
| 649 | args->m_error.SetErrorToGenericError(); |
| 650 | args->m_error.SetErrorString("Process fork failed."); |
| 651 | goto FINISH; |
| 652 | } |
| 653 | |
| 654 | // Child process. |
| 655 | if (pid == 0) |
| 656 | { |
| 657 | // Trace this process. |
| 658 | ptrace(PTRACE_TRACEME, 0, NULL, NULL); |
| 659 | |
| 660 | // Do not inherit setgid powers. |
| 661 | setgid(getgid()); |
| 662 | |
| 663 | // Let us have our own process group. |
| 664 | setpgid(0, 0); |
| 665 | |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 666 | // Dup file descriptors if needed. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 667 | // |
| 668 | // FIXME: If two or more of the paths are the same we needlessly open |
| 669 | // the same file multiple times. |
| 670 | if (stdin_path != NULL && stdin_path[0]) |
| 671 | if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY | O_CREAT)) |
| 672 | exit(1); |
| 673 | |
| 674 | if (stdout_path != NULL && stdout_path[0]) |
| 675 | if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
| 676 | exit(1); |
| 677 | |
| 678 | if (stderr_path != NULL && stderr_path[0]) |
| 679 | if (!DupDescriptor(stderr_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
| 680 | exit(1); |
| 681 | |
| 682 | // Execute. We should never return. |
| 683 | execve(argv[0], |
| 684 | const_cast<char *const *>(argv), |
| 685 | const_cast<char *const *>(envp)); |
| 686 | exit(-1); |
| 687 | } |
| 688 | |
| 689 | // Wait for the child process to to trap on its call to execve. |
| 690 | int status; |
| 691 | if ((status = waitpid(pid, NULL, 0)) < 0) |
| 692 | { |
| 693 | // execve likely failed for some reason. |
| 694 | args->m_error.SetErrorToErrno(); |
| 695 | goto FINISH; |
| 696 | } |
| 697 | assert(status == pid && "Could not sync with inferior process."); |
| 698 | |
| 699 | // Have the child raise an event on exit. This is used to keep the child in |
| 700 | // limbo until it is destroyed. |
| 701 | if (ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) |
| 702 | { |
| 703 | args->m_error.SetErrorToErrno(); |
| 704 | goto FINISH; |
| 705 | } |
| 706 | |
| 707 | // Release the master terminal descriptor and pass it off to the |
| 708 | // ProcessMonitor instance. Similarly stash the inferior pid. |
| 709 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 710 | monitor->m_pid = pid; |
| 711 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 712 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 713 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 714 | // descriptor to read from). |
| 715 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 716 | goto FINISH; |
| 717 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 718 | // Update the process thread list with this new thread and mark it as |
| 719 | // current. |
| 720 | inferior.reset(new LinuxThread(process, pid)); |
| 721 | process.GetThreadList().AddThread(inferior); |
Stephen Wilson | 5a8feea | 2011-01-04 21:39:27 +0000 | [diff] [blame] | 722 | process.GetThreadList().SetSelectedThreadByID(pid); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 723 | |
| 724 | // Let our process instance know the thread has stopped. |
| 725 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 726 | |
| 727 | FINISH: |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 728 | return args->m_error.Success(); |
| 729 | } |
| 730 | |
| 731 | bool |
| 732 | ProcessMonitor::EnableIPC() |
| 733 | { |
| 734 | int fd[2]; |
| 735 | |
| 736 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) |
| 737 | return false; |
| 738 | |
| 739 | m_client_fd = fd[0]; |
| 740 | m_server_fd = fd[1]; |
| 741 | return true; |
| 742 | } |
| 743 | |
| 744 | bool |
| 745 | ProcessMonitor::MonitorCallback(void *callback_baton, |
| 746 | lldb::pid_t pid, |
| 747 | int signal, |
| 748 | int status) |
| 749 | { |
| 750 | ProcessMessage message; |
| 751 | ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton); |
| 752 | ProcessLinux *process = monitor->m_process; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 753 | bool stop_monitoring; |
| 754 | siginfo_t info; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 755 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 756 | if (!monitor->GetSignalInfo(pid, &info)) |
| 757 | stop_monitoring = true; // pid is gone. Bail. |
| 758 | else { |
| 759 | switch (info.si_signo) |
| 760 | { |
| 761 | case SIGTRAP: |
| 762 | message = MonitorSIGTRAP(monitor, &info, pid); |
| 763 | break; |
| 764 | |
| 765 | default: |
| 766 | message = MonitorSignal(monitor, &info, pid); |
| 767 | break; |
| 768 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 769 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 770 | process->SendMessage(message); |
| 771 | stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 774 | return stop_monitoring; |
| 775 | } |
| 776 | |
| 777 | ProcessMessage |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 778 | ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, |
| 779 | const struct siginfo *info, lldb::pid_t pid) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 780 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 781 | ProcessMessage message; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 782 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 783 | assert(info->si_signo == SIGTRAP && "Unexpected child signal!"); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 784 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 785 | switch (info->si_code) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 786 | { |
| 787 | default: |
| 788 | assert(false && "Unexpected SIGTRAP code!"); |
| 789 | break; |
| 790 | |
| 791 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 792 | { |
| 793 | // The inferior process is about to exit. Maintain the process in a |
| 794 | // state of "limbo" until we are explicitly commanded to detach, |
| 795 | // destroy, resume, etc. |
| 796 | unsigned long data = 0; |
| 797 | if (!monitor->GetEventMessage(pid, &data)) |
| 798 | data = -1; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 799 | message = ProcessMessage::Limbo(pid, (data >> 8)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | |
| 803 | case 0: |
| 804 | case TRAP_TRACE: |
| 805 | message = ProcessMessage::Trace(pid); |
| 806 | break; |
| 807 | |
| 808 | case SI_KERNEL: |
| 809 | case TRAP_BRKPT: |
| 810 | message = ProcessMessage::Break(pid); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | return message; |
| 815 | } |
| 816 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 817 | ProcessMessage |
| 818 | ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, |
| 819 | const struct siginfo *info, lldb::pid_t pid) |
| 820 | { |
| 821 | ProcessMessage message; |
| 822 | int signo = info->si_signo; |
| 823 | |
| 824 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 825 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 826 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 827 | // |
| 828 | // IOW, user generated signals never generate what we consider to be a |
| 829 | // "crash". |
| 830 | // |
| 831 | // Similarly, ACK signals generated by this monitor. |
| 832 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 833 | { |
| 834 | if (info->si_pid == getpid()) |
| 835 | return ProcessMessage::SignalDelivered(pid, signo); |
| 836 | else |
| 837 | return ProcessMessage::Signal(pid, signo); |
| 838 | } |
| 839 | |
| 840 | if (signo == SIGSEGV) { |
| 841 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 842 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info); |
| 843 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 844 | } |
| 845 | |
| 846 | if (signo == SIGILL) { |
| 847 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 848 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info); |
| 849 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 850 | } |
| 851 | |
| 852 | if (signo == SIGFPE) { |
| 853 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 854 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info); |
| 855 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 856 | } |
| 857 | |
| 858 | if (signo == SIGBUS) { |
| 859 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 860 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info); |
| 861 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 862 | } |
| 863 | |
| 864 | // Everything else is "normal" and does not require any special action on |
| 865 | // our part. |
| 866 | return ProcessMessage::Signal(pid, signo); |
| 867 | } |
| 868 | |
| 869 | ProcessMessage::CrashReason |
| 870 | ProcessMonitor::GetCrashReasonForSIGSEGV(const struct siginfo *info) |
| 871 | { |
| 872 | ProcessMessage::CrashReason reason; |
| 873 | assert(info->si_signo == SIGSEGV); |
| 874 | |
| 875 | reason = ProcessMessage::eInvalidCrashReason; |
| 876 | |
| 877 | switch (info->si_code) |
| 878 | { |
| 879 | default: |
| 880 | assert(false && "unexpected si_code for SIGSEGV"); |
| 881 | break; |
| 882 | case SEGV_MAPERR: |
| 883 | reason = ProcessMessage::eInvalidAddress; |
| 884 | break; |
| 885 | case SEGV_ACCERR: |
| 886 | reason = ProcessMessage::ePrivilegedAddress; |
| 887 | break; |
| 888 | } |
| 889 | |
| 890 | return reason; |
| 891 | } |
| 892 | |
| 893 | ProcessMessage::CrashReason |
| 894 | ProcessMonitor::GetCrashReasonForSIGILL(const struct siginfo *info) |
| 895 | { |
| 896 | ProcessMessage::CrashReason reason; |
| 897 | assert(info->si_signo == SIGILL); |
| 898 | |
| 899 | reason = ProcessMessage::eInvalidCrashReason; |
| 900 | |
| 901 | switch (info->si_code) |
| 902 | { |
| 903 | default: |
| 904 | assert(false && "unexpected si_code for SIGILL"); |
| 905 | break; |
| 906 | case ILL_ILLOPC: |
| 907 | reason = ProcessMessage::eIllegalOpcode; |
| 908 | break; |
| 909 | case ILL_ILLOPN: |
| 910 | reason = ProcessMessage::eIllegalOperand; |
| 911 | break; |
| 912 | case ILL_ILLADR: |
| 913 | reason = ProcessMessage::eIllegalAddressingMode; |
| 914 | break; |
| 915 | case ILL_ILLTRP: |
| 916 | reason = ProcessMessage::eIllegalTrap; |
| 917 | break; |
| 918 | case ILL_PRVOPC: |
| 919 | reason = ProcessMessage::ePrivilegedOpcode; |
| 920 | break; |
| 921 | case ILL_PRVREG: |
| 922 | reason = ProcessMessage::ePrivilegedRegister; |
| 923 | break; |
| 924 | case ILL_COPROC: |
| 925 | reason = ProcessMessage::eCoprocessorError; |
| 926 | break; |
| 927 | case ILL_BADSTK: |
| 928 | reason = ProcessMessage::eInternalStackError; |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | return reason; |
| 933 | } |
| 934 | |
| 935 | ProcessMessage::CrashReason |
| 936 | ProcessMonitor::GetCrashReasonForSIGFPE(const struct siginfo *info) |
| 937 | { |
| 938 | ProcessMessage::CrashReason reason; |
| 939 | assert(info->si_signo == SIGFPE); |
| 940 | |
| 941 | reason = ProcessMessage::eInvalidCrashReason; |
| 942 | |
| 943 | switch (info->si_code) |
| 944 | { |
| 945 | default: |
| 946 | assert(false && "unexpected si_code for SIGFPE"); |
| 947 | break; |
| 948 | case FPE_INTDIV: |
| 949 | reason = ProcessMessage::eIntegerDivideByZero; |
| 950 | break; |
| 951 | case FPE_INTOVF: |
| 952 | reason = ProcessMessage::eIntegerOverflow; |
| 953 | break; |
| 954 | case FPE_FLTDIV: |
| 955 | reason = ProcessMessage::eFloatDivideByZero; |
| 956 | break; |
| 957 | case FPE_FLTOVF: |
| 958 | reason = ProcessMessage::eFloatOverflow; |
| 959 | break; |
| 960 | case FPE_FLTUND: |
| 961 | reason = ProcessMessage::eFloatUnderflow; |
| 962 | break; |
| 963 | case FPE_FLTRES: |
| 964 | reason = ProcessMessage::eFloatInexactResult; |
| 965 | break; |
| 966 | case FPE_FLTINV: |
| 967 | reason = ProcessMessage::eFloatInvalidOperation; |
| 968 | break; |
| 969 | case FPE_FLTSUB: |
| 970 | reason = ProcessMessage::eFloatSubscriptRange; |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | return reason; |
| 975 | } |
| 976 | |
| 977 | ProcessMessage::CrashReason |
| 978 | ProcessMonitor::GetCrashReasonForSIGBUS(const struct siginfo *info) |
| 979 | { |
| 980 | ProcessMessage::CrashReason reason; |
| 981 | assert(info->si_signo == SIGBUS); |
| 982 | |
| 983 | reason = ProcessMessage::eInvalidCrashReason; |
| 984 | |
| 985 | switch (info->si_code) |
| 986 | { |
| 987 | default: |
| 988 | assert(false && "unexpected si_code for SIGBUS"); |
| 989 | break; |
| 990 | case BUS_ADRALN: |
| 991 | reason = ProcessMessage::eIllegalAlignment; |
| 992 | break; |
| 993 | case BUS_ADRERR: |
| 994 | reason = ProcessMessage::eIllegalAddress; |
| 995 | break; |
| 996 | case BUS_OBJERR: |
| 997 | reason = ProcessMessage::eHardwareError; |
| 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | return reason; |
| 1002 | } |
| 1003 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1004 | void |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1005 | ProcessMonitor::ServeOperation(LaunchArgs *args) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1006 | { |
| 1007 | int status; |
| 1008 | pollfd fdset; |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1009 | ProcessMonitor *monitor = args->m_monitor; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1010 | |
| 1011 | fdset.fd = monitor->m_server_fd; |
| 1012 | fdset.events = POLLIN | POLLPRI; |
| 1013 | fdset.revents = 0; |
| 1014 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1015 | // We are finised with the arguments and are ready to go. Sync with the |
| 1016 | // parent thread and start serving operations on the inferior. |
| 1017 | sem_post(&args->m_semaphore); |
| 1018 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1019 | for (;;) |
| 1020 | { |
| 1021 | if ((status = poll(&fdset, 1, -1)) < 0) |
| 1022 | { |
| 1023 | switch (errno) |
| 1024 | { |
| 1025 | default: |
| 1026 | assert(false && "Unexpected poll() failure!"); |
| 1027 | continue; |
| 1028 | |
| 1029 | case EINTR: continue; // Just poll again. |
| 1030 | case EBADF: return; // Connection terminated. |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | assert(status == 1 && "Too many descriptors!"); |
| 1035 | |
| 1036 | if (fdset.revents & POLLIN) |
| 1037 | { |
| 1038 | Operation *op = NULL; |
| 1039 | |
| 1040 | READ_AGAIN: |
| 1041 | if ((status = read(fdset.fd, &op, sizeof(op))) < 0) |
| 1042 | { |
| 1043 | // There is only one acceptable failure. |
| 1044 | assert(errno == EINTR); |
| 1045 | goto READ_AGAIN; |
| 1046 | } |
| 1047 | |
| 1048 | assert(status == sizeof(op)); |
| 1049 | op->Execute(monitor); |
| 1050 | write(fdset.fd, &op, sizeof(op)); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | void |
| 1056 | ProcessMonitor::DoOperation(Operation *op) |
| 1057 | { |
| 1058 | int status; |
| 1059 | Operation *ack = NULL; |
| 1060 | Mutex::Locker lock(m_server_mutex); |
| 1061 | |
| 1062 | // FIXME: Do proper error checking here. |
| 1063 | write(m_client_fd, &op, sizeof(op)); |
| 1064 | |
| 1065 | READ_AGAIN: |
| 1066 | if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0) |
| 1067 | { |
| 1068 | // If interrupted by a signal handler try again. Otherwise the monitor |
| 1069 | // thread probably died and we have a stale file descriptor -- abort the |
| 1070 | // operation. |
| 1071 | if (errno == EINTR) |
| 1072 | goto READ_AGAIN; |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | assert(status == sizeof(ack)); |
| 1077 | assert(ack == op && "Invalid monitor thread response!"); |
| 1078 | } |
| 1079 | |
| 1080 | size_t |
| 1081 | ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 1082 | Error &error) |
| 1083 | { |
| 1084 | size_t result; |
| 1085 | ReadOperation op(vm_addr, buf, size, error, result); |
| 1086 | DoOperation(&op); |
| 1087 | return result; |
| 1088 | } |
| 1089 | |
| 1090 | size_t |
| 1091 | ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 1092 | lldb_private::Error &error) |
| 1093 | { |
| 1094 | size_t result; |
| 1095 | WriteOperation op(vm_addr, buf, size, error, result); |
| 1096 | DoOperation(&op); |
| 1097 | return result; |
| 1098 | } |
| 1099 | |
| 1100 | bool |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame^] | 1101 | ProcessMonitor::ReadRegisterValue(unsigned offset, RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1102 | { |
| 1103 | bool result; |
| 1104 | ReadRegOperation op(offset, value, result); |
| 1105 | DoOperation(&op); |
| 1106 | return result; |
| 1107 | } |
| 1108 | |
| 1109 | bool |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame^] | 1110 | ProcessMonitor::WriteRegisterValue(unsigned offset, const RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1111 | { |
| 1112 | bool result; |
| 1113 | WriteRegOperation op(offset, value, result); |
| 1114 | DoOperation(&op); |
| 1115 | return result; |
| 1116 | } |
| 1117 | |
| 1118 | bool |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1119 | ProcessMonitor::ReadGPR(void *buf) |
| 1120 | { |
| 1121 | bool result; |
| 1122 | ReadGPROperation op(buf, result); |
| 1123 | DoOperation(&op); |
| 1124 | return result; |
| 1125 | } |
| 1126 | |
| 1127 | bool |
| 1128 | ProcessMonitor::ReadFPR(void *buf) |
| 1129 | { |
| 1130 | bool result; |
| 1131 | ReadFPROperation op(buf, result); |
| 1132 | DoOperation(&op); |
| 1133 | return result; |
| 1134 | } |
| 1135 | |
| 1136 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1137 | ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1138 | { |
| 1139 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1140 | ResumeOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1141 | DoOperation(&op); |
| 1142 | return result; |
| 1143 | } |
| 1144 | |
| 1145 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1146 | ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1147 | { |
| 1148 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1149 | SingleStepOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1150 | DoOperation(&op); |
| 1151 | return result; |
| 1152 | } |
| 1153 | |
| 1154 | bool |
| 1155 | ProcessMonitor::BringProcessIntoLimbo() |
| 1156 | { |
| 1157 | bool result; |
| 1158 | KillOperation op(result); |
| 1159 | DoOperation(&op); |
| 1160 | return result; |
| 1161 | } |
| 1162 | |
| 1163 | bool |
| 1164 | ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo) |
| 1165 | { |
| 1166 | bool result; |
| 1167 | SiginfoOperation op(tid, siginfo, result); |
| 1168 | DoOperation(&op); |
| 1169 | return result; |
| 1170 | } |
| 1171 | |
| 1172 | bool |
| 1173 | ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 1174 | { |
| 1175 | bool result; |
| 1176 | EventMessageOperation op(tid, message, result); |
| 1177 | DoOperation(&op); |
| 1178 | return result; |
| 1179 | } |
| 1180 | |
| 1181 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1182 | ProcessMonitor::Detach() |
| 1183 | { |
| 1184 | bool result; |
| 1185 | KillOperation op(result); |
| 1186 | DoOperation(&op); |
| 1187 | StopMonitor(); |
| 1188 | return result; |
| 1189 | } |
| 1190 | |
| 1191 | bool |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1192 | ProcessMonitor::DupDescriptor(const char *path, int fd, int flags) |
| 1193 | { |
| 1194 | int target_fd = open(path, flags); |
| 1195 | |
| 1196 | if (target_fd == -1) |
| 1197 | return false; |
| 1198 | |
| 1199 | return (dup2(fd, target_fd) == -1) ? false : true; |
| 1200 | } |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1201 | |
| 1202 | void |
| 1203 | ProcessMonitor::StopMonitoringChildProcess() |
| 1204 | { |
| 1205 | lldb::thread_result_t thread_result; |
| 1206 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 1207 | if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1208 | { |
| 1209 | Host::ThreadCancel(m_monitor_thread, NULL); |
| 1210 | Host::ThreadJoin(m_monitor_thread, &thread_result, NULL); |
| 1211 | m_monitor_thread = LLDB_INVALID_HOST_THREAD; |
| 1212 | } |
| 1213 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1214 | |
| 1215 | void |
| 1216 | ProcessMonitor::StopMonitor() |
| 1217 | { |
| 1218 | StopMonitoringChildProcess(); |
| 1219 | StopOperationThread(); |
| 1220 | CloseFD(m_terminal_fd); |
| 1221 | CloseFD(m_client_fd); |
| 1222 | CloseFD(m_server_fd); |
| 1223 | } |
| 1224 | |
| 1225 | void |
| 1226 | ProcessMonitor::CloseFD(int &fd) |
| 1227 | { |
| 1228 | if (fd != -1) |
| 1229 | { |
| 1230 | close(fd); |
| 1231 | fd = -1; |
| 1232 | } |
| 1233 | } |