2010-07-24 10:19:04 +08:00
|
|
|
//===-- ProcessMonitor.h -------------------------------------- -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef liblldb_ProcessMonitor_H_
|
|
|
|
#define liblldb_ProcessMonitor_H_
|
|
|
|
|
|
|
|
// C Includes
|
|
|
|
#include <semaphore.h>
|
2011-03-30 23:55:52 +08:00
|
|
|
#include <signal.h>
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
#include "lldb/lldb-types.h"
|
|
|
|
#include "lldb/Host/Mutex.h"
|
|
|
|
|
|
|
|
namespace lldb_private
|
|
|
|
{
|
|
|
|
class Error;
|
|
|
|
class Module;
|
|
|
|
class Scalar;
|
|
|
|
} // End lldb_private namespace.
|
|
|
|
|
|
|
|
class ProcessLinux;
|
|
|
|
class Operation;
|
|
|
|
|
|
|
|
/// @class ProcessMonitor
|
|
|
|
/// @brief Manages communication with the inferior (debugee) process.
|
|
|
|
///
|
|
|
|
/// Upon construction, this class prepares and launches an inferior process for
|
|
|
|
/// debugging.
|
|
|
|
///
|
|
|
|
/// Changes in the inferior process state are propagated to the associated
|
|
|
|
/// ProcessLinux instance by calling ProcessLinux::SendMessage with the
|
|
|
|
/// appropriate ProcessMessage events.
|
|
|
|
///
|
|
|
|
/// A purposely minimal set of operations are provided to interrogate and change
|
|
|
|
/// the inferior process state.
|
|
|
|
class ProcessMonitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Launches an inferior process ready for debugging. Forms the
|
|
|
|
/// implementation of Process::DoLaunch.
|
2013-07-10 06:36:48 +08:00
|
|
|
ProcessMonitor(ProcessPOSIX *process,
|
2010-07-24 10:19:04 +08:00
|
|
|
lldb_private::Module *module,
|
|
|
|
char const *argv[],
|
|
|
|
char const *envp[],
|
|
|
|
const char *stdin_path,
|
|
|
|
const char *stdout_path,
|
|
|
|
const char *stderr_path,
|
2013-01-08 22:49:22 +08:00
|
|
|
const char *working_dir,
|
2010-07-24 10:19:04 +08:00
|
|
|
lldb_private::Error &error);
|
|
|
|
|
2013-07-10 06:36:48 +08:00
|
|
|
ProcessMonitor(ProcessPOSIX *process,
|
2011-06-15 03:19:50 +08:00
|
|
|
lldb::pid_t pid,
|
|
|
|
lldb_private::Error &error);
|
|
|
|
|
2010-07-24 10:19:04 +08:00
|
|
|
~ProcessMonitor();
|
|
|
|
|
2013-05-29 07:04:25 +08:00
|
|
|
enum ResumeSignals
|
|
|
|
{
|
|
|
|
eResumeSignalNone = 0
|
|
|
|
};
|
|
|
|
|
2010-07-24 10:19:04 +08:00
|
|
|
/// Provides the process number of debugee.
|
|
|
|
lldb::pid_t
|
|
|
|
GetPID() const { return m_pid; }
|
|
|
|
|
|
|
|
/// Returns the process associated with this ProcessMonitor.
|
|
|
|
ProcessLinux &
|
|
|
|
GetProcess() { return *m_process; }
|
|
|
|
|
2011-01-09 04:28:42 +08:00
|
|
|
/// Returns a file descriptor to the controlling terminal of the inferior
|
2010-07-24 10:19:04 +08:00
|
|
|
/// process.
|
|
|
|
///
|
2011-01-09 04:28:42 +08:00
|
|
|
/// Reads from this file descriptor yield both the standard output and
|
2010-07-24 10:19:04 +08:00
|
|
|
/// standard error of this debugee. Even if stderr and stdout were
|
|
|
|
/// redirected on launch it may still happen that data is available on this
|
|
|
|
/// descriptor (if the inferior process opens /dev/tty, for example).
|
|
|
|
///
|
|
|
|
/// If this monitor was attached to an existing process this method returns
|
|
|
|
/// -1.
|
|
|
|
int
|
|
|
|
GetTerminalFD() const { return m_terminal_fd; }
|
|
|
|
|
|
|
|
/// Reads @p size bytes from address @vm_adder in the inferior process
|
|
|
|
/// address space.
|
|
|
|
///
|
|
|
|
/// This method is provided to implement Process::DoReadMemory.
|
|
|
|
size_t
|
|
|
|
ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
|
|
|
|
lldb_private::Error &error);
|
|
|
|
|
|
|
|
/// Writes @p size bytes from address @p vm_adder in the inferior process
|
|
|
|
/// address space.
|
|
|
|
///
|
|
|
|
/// This method is provided to implement Process::DoWriteMemory.
|
|
|
|
size_t
|
|
|
|
WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
|
|
|
|
lldb_private::Error &error);
|
|
|
|
|
|
|
|
/// Reads the contents from the register identified by the given (architecture
|
|
|
|
/// dependent) offset.
|
|
|
|
///
|
|
|
|
/// This method is provided for use by RegisterContextLinux derivatives.
|
|
|
|
bool
|
2013-05-10 03:59:47 +08:00
|
|
|
ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
|
2012-12-19 03:50:15 +08:00
|
|
|
unsigned size, lldb_private::RegisterValue &value);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
/// Writes the given value to the register identified by the given
|
|
|
|
/// (architecture dependent) offset.
|
|
|
|
///
|
|
|
|
/// This method is provided for use by RegisterContextLinux derivatives.
|
|
|
|
bool
|
2013-05-10 03:59:47 +08:00
|
|
|
WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
|
2012-12-19 03:50:15 +08:00
|
|
|
const lldb_private::RegisterValue &value);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
2011-01-19 09:31:38 +08:00
|
|
|
/// Reads all general purpose registers into the specified buffer.
|
|
|
|
bool
|
2013-03-07 01:20:48 +08:00
|
|
|
ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
|
2011-01-19 09:31:38 +08:00
|
|
|
|
2013-03-21 04:34:35 +08:00
|
|
|
/// Reads generic floating point registers into the specified buffer.
|
2011-01-19 09:31:38 +08:00
|
|
|
bool
|
2013-03-07 01:20:48 +08:00
|
|
|
ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
|
2011-01-19 09:31:38 +08:00
|
|
|
|
2013-03-21 04:34:35 +08:00
|
|
|
/// Reads the specified register set into the specified buffer.
|
|
|
|
/// For instance, the extended floating-point register set.
|
|
|
|
bool
|
|
|
|
ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
|
|
|
|
|
2011-06-04 04:41:02 +08:00
|
|
|
/// Writes all general purpose registers into the specified buffer.
|
|
|
|
bool
|
2013-03-07 01:20:48 +08:00
|
|
|
WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
|
2011-06-04 04:41:02 +08:00
|
|
|
|
2013-03-21 04:34:35 +08:00
|
|
|
/// Writes generic floating point registers into the specified buffer.
|
2011-06-04 04:41:02 +08:00
|
|
|
bool
|
2013-03-07 01:20:48 +08:00
|
|
|
WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
|
2011-06-04 04:41:02 +08:00
|
|
|
|
2013-03-21 04:34:35 +08:00
|
|
|
/// Writes the specified register set into the specified buffer.
|
|
|
|
/// For instance, the extended floating-point register set.
|
|
|
|
bool
|
|
|
|
WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
|
|
|
|
|
2010-07-24 10:19:04 +08:00
|
|
|
/// Writes a siginfo_t structure corresponding to the given thread ID to the
|
|
|
|
/// memory region pointed to by @p siginfo.
|
|
|
|
bool
|
2012-11-24 02:09:58 +08:00
|
|
|
GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
/// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
|
|
|
|
/// corresponding to the given thread IDto the memory pointed to by @p
|
|
|
|
/// message.
|
|
|
|
bool
|
|
|
|
GetEventMessage(lldb::tid_t tid, unsigned long *message);
|
|
|
|
|
2011-03-30 23:55:52 +08:00
|
|
|
/// Resumes the given thread. If @p signo is anything but
|
|
|
|
/// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
|
2010-07-24 10:19:04 +08:00
|
|
|
bool
|
2011-03-30 23:55:52 +08:00
|
|
|
Resume(lldb::tid_t tid, uint32_t signo);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
2011-03-30 23:55:52 +08:00
|
|
|
/// Single steps the given thread. If @p signo is anything but
|
|
|
|
/// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
|
2010-07-24 10:19:04 +08:00
|
|
|
bool
|
2011-03-30 23:55:52 +08:00
|
|
|
SingleStep(lldb::tid_t tid, uint32_t signo);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
/// Sends the inferior process a PTRACE_KILL signal. The inferior will
|
|
|
|
/// still exists and can be interrogated. Once resumed it will exit as
|
|
|
|
/// though it received a SIGKILL.
|
|
|
|
bool
|
|
|
|
BringProcessIntoLimbo();
|
|
|
|
|
2012-10-17 04:20:18 +08:00
|
|
|
lldb_private::Error
|
2013-06-01 06:00:07 +08:00
|
|
|
Detach(lldb::tid_t tid);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
2013-07-11 05:57:27 +08:00
|
|
|
/// Stops the monitoring the child process thread.
|
|
|
|
void
|
|
|
|
StopMonitor();
|
|
|
|
|
2013-05-29 07:04:25 +08:00
|
|
|
/// Stops the requested thread and waits for the stop signal.
|
|
|
|
bool
|
|
|
|
StopThread(lldb::tid_t tid);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
2010-07-24 10:19:04 +08:00
|
|
|
private:
|
2013-07-10 06:36:48 +08:00
|
|
|
ProcessLinux *m_process;
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
lldb::thread_t m_operation_thread;
|
2013-03-07 01:20:48 +08:00
|
|
|
lldb::thread_t m_monitor_thread;
|
2010-07-24 10:19:04 +08:00
|
|
|
lldb::pid_t m_pid;
|
|
|
|
int m_terminal_fd;
|
|
|
|
|
|
|
|
|
|
|
|
lldb_private::Mutex m_server_mutex;
|
|
|
|
int m_client_fd;
|
|
|
|
int m_server_fd;
|
|
|
|
|
2011-06-15 03:19:50 +08:00
|
|
|
struct OperationArgs
|
|
|
|
{
|
|
|
|
OperationArgs(ProcessMonitor *monitor);
|
|
|
|
|
|
|
|
~OperationArgs();
|
|
|
|
|
|
|
|
ProcessMonitor *m_monitor; // The monitor performing the attach.
|
|
|
|
sem_t m_semaphore; // Posted to once operation complete.
|
|
|
|
lldb_private::Error m_error; // Set if process operation failed.
|
|
|
|
};
|
|
|
|
|
2010-07-24 10:19:04 +08:00
|
|
|
/// @class LauchArgs
|
|
|
|
///
|
|
|
|
/// @brief Simple structure to pass data to the thread responsible for
|
|
|
|
/// launching a child process.
|
2011-06-15 03:19:50 +08:00
|
|
|
struct LaunchArgs : OperationArgs
|
2010-07-24 10:19:04 +08:00
|
|
|
{
|
|
|
|
LaunchArgs(ProcessMonitor *monitor,
|
|
|
|
lldb_private::Module *module,
|
|
|
|
char const **argv,
|
|
|
|
char const **envp,
|
|
|
|
const char *stdin_path,
|
|
|
|
const char *stdout_path,
|
2013-01-08 22:49:22 +08:00
|
|
|
const char *stderr_path,
|
|
|
|
const char *working_dir);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
~LaunchArgs();
|
|
|
|
|
|
|
|
lldb_private::Module *m_module; // The executable image to launch.
|
|
|
|
char const **m_argv; // Process arguments.
|
|
|
|
char const **m_envp; // Process environment.
|
|
|
|
const char *m_stdin_path; // Redirect stdin or NULL.
|
|
|
|
const char *m_stdout_path; // Redirect stdout or NULL.
|
|
|
|
const char *m_stderr_path; // Redirect stderr or NULL.
|
2013-01-08 22:49:22 +08:00
|
|
|
const char *m_working_dir; // Working directory or NULL.
|
2010-07-24 10:19:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-06-15 03:19:50 +08:00
|
|
|
StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
static void *
|
2011-06-15 03:19:50 +08:00
|
|
|
LaunchOpThread(void *arg);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
static bool
|
|
|
|
Launch(LaunchArgs *args);
|
|
|
|
|
|
|
|
bool
|
|
|
|
EnableIPC();
|
|
|
|
|
2011-06-15 03:19:50 +08:00
|
|
|
struct AttachArgs : OperationArgs
|
|
|
|
{
|
|
|
|
AttachArgs(ProcessMonitor *monitor,
|
|
|
|
lldb::pid_t pid);
|
|
|
|
|
|
|
|
~AttachArgs();
|
|
|
|
|
|
|
|
lldb::pid_t m_pid; // pid of the process to be attached.
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
|
|
|
|
|
|
|
|
static void *
|
|
|
|
AttachOpThread(void *args);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
Attach(AttachArgs *args);
|
|
|
|
|
2013-06-01 06:00:07 +08:00
|
|
|
static bool
|
|
|
|
SetDefaultPtraceOpts(const lldb::pid_t);
|
|
|
|
|
2010-07-24 10:19:04 +08:00
|
|
|
static void
|
2011-06-15 03:19:50 +08:00
|
|
|
ServeOperation(OperationArgs *args);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
static bool
|
|
|
|
DupDescriptor(const char *path, int fd, int flags);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
MonitorCallback(void *callback_baton,
|
2011-11-21 08:10:19 +08:00
|
|
|
lldb::pid_t pid, bool exited, int signal, int status);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
static ProcessMessage
|
2011-03-30 23:55:52 +08:00
|
|
|
MonitorSIGTRAP(ProcessMonitor *monitor,
|
2011-11-30 04:50:10 +08:00
|
|
|
const siginfo_t *info, lldb::pid_t pid);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
|
|
|
static ProcessMessage
|
|
|
|
MonitorSignal(ProcessMonitor *monitor,
|
2011-11-30 04:50:10 +08:00
|
|
|
const siginfo_t *info, lldb::pid_t pid);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
|
|
|
static ProcessMessage::CrashReason
|
2011-11-30 04:50:10 +08:00
|
|
|
GetCrashReasonForSIGSEGV(const siginfo_t *info);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
|
|
|
static ProcessMessage::CrashReason
|
2011-11-30 04:50:10 +08:00
|
|
|
GetCrashReasonForSIGILL(const siginfo_t *info);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
|
|
|
static ProcessMessage::CrashReason
|
2011-11-30 04:50:10 +08:00
|
|
|
GetCrashReasonForSIGFPE(const siginfo_t *info);
|
2011-03-30 23:55:52 +08:00
|
|
|
|
|
|
|
static ProcessMessage::CrashReason
|
2011-11-30 04:50:10 +08:00
|
|
|
GetCrashReasonForSIGBUS(const siginfo_t *info);
|
2010-07-24 10:19:04 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
DoOperation(Operation *op);
|
2011-01-05 05:40:25 +08:00
|
|
|
|
|
|
|
/// Stops the child monitor thread.
|
2011-03-30 23:55:52 +08:00
|
|
|
void
|
|
|
|
StopMonitoringChildProcess();
|
|
|
|
|
2012-10-17 04:20:18 +08:00
|
|
|
/// Stops the operation thread used to attach/launch a process.
|
|
|
|
void
|
|
|
|
StopOpThread();
|
|
|
|
|
2011-03-30 23:55:52 +08:00
|
|
|
void
|
|
|
|
CloseFD(int &fd);
|
2010-07-24 10:19:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // #ifndef liblldb_ProcessMonitor_H_
|