llvm-project/lldb/source/Plugins/Process/Linux/ProcessMonitor.h

327 lines
10 KiB
C
Raw Normal View History

//===-- 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>
#include <signal.h>
// C++ Includes
// Other libraries and framework includes
#include "lldb/lldb-types.h"
#include "lldb/Host/HostThread.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.
ProcessMonitor(ProcessPOSIX *process,
lldb_private::Module *module,
char const *argv[],
char const *envp[],
const char *stdin_path,
const char *stdout_path,
const char *stderr_path,
const char *working_dir,
const lldb_private::ProcessLaunchInfo &launch_info,
lldb_private::Error &error);
ProcessMonitor(ProcessPOSIX *process,
lldb::pid_t pid,
lldb_private::Error &error);
~ProcessMonitor();
enum ResumeSignals
{
eResumeSignalNone = 0
};
/// 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; }
/// Returns a file descriptor to the controlling terminal of the inferior
/// process.
///
/// Reads from this file descriptor yield both the standard output and
/// 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
ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
unsigned size, lldb_private::RegisterValue &value);
/// Writes the given value to the register identified by the given
/// (architecture dependent) offset.
///
/// This method is provided for use by RegisterContextLinux derivatives.
bool
WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
const lldb_private::RegisterValue &value);
/// Reads all general purpose registers into the specified buffer.
bool
ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
/// Reads generic floating point registers into the specified buffer.
bool
ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
/// 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);
/// Writes all general purpose registers into the specified buffer.
bool
WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
/// Writes generic floating point registers into the specified buffer.
bool
WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
/// 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);
Added support for reading thread-local storage variables, as defined using the __thread modifier. To make this work this patch extends LLDB to: - Explicitly track the link_map address for each module. This is effectively the module handle, not sure why it wasn't already being stored off anywhere. As an extension later, it would be nice if someone were to add support for printing this as part of the modules list. - Allow reading the per-thread data pointer via ptrace. I have added support for Linux here. I'll be happy to add support for FreeBSD once this is reviewed. OS X does not appear to have __thread variables, so maybe we don't need it there. Windows support should eventually be workable along the same lines. - Make DWARF expressions track which module they originated from. - Add support for the DW_OP_GNU_push_tls_address DWARF opcode, as generated by gcc and recent versions of clang. Earlier versions of clang (such as 3.2, which is default on Ubuntu right now) do not generate TLS debug info correctly so can not be supported here. - Understand the format of the pthread DTV block. This is where it gets tricky. We have three basic options here: 1) Call "dlinfo" or "__tls_get_addr" on the inferior and ask it directly. However this won't work on core dumps, and generally speaking it's not a good idea for the debugger to call functions itself, as it has the potential to not work depending on the state of the target. 2) Use libthread_db. This is what GDB does. However this option requires having a version of libthread_db on the host cross-compiled for each potential target. This places a large burden on the user, and would make it very hard to cross-debug from Windows to Linux, for example. Trying to build a library intended exclusively for one OS on a different one is not pleasant. GDB sidesteps the problem and asks the user to figure it out. 3) Parse the DTV structure ourselves. On initial inspection this seems to be a bad option, as the DTV structure (the format used by the runtime to manage TLS data) is not in fact a kernel data structure, it is implemented entirely in useerland in libc. Therefore the layout of it's fields are version and OS dependent, and are not standardized. However, it turns out not to be such a problem. All OSes use basically the same algorithm (a per-module lookup table) as detailed in Ulrich Drepper's TLS ELF ABI document, so we can easily write code to decode it ourselves. The only question therefore is the exact field layouts required. Happily, the implementors of libpthread expose the structure of the DTV via metadata exported as symbols from the .so itself, designed exactly for this kind of thing. So this patch simply reads that metadata in, and re-implements libthread_db's algorithm itself. We thereby get cross-platform TLS lookup without either requiring third-party libraries, while still being independent of the version of libpthread being used. Test case included. llvm-svn: 192922
2013-10-18 05:14:00 +08:00
/// Reads the value of the thread-specific pointer for a given thread ID.
bool
ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
/// Writes a siginfo_t structure corresponding to the given thread ID to the
/// memory region pointed to by @p siginfo.
bool
GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
/// 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);
/// Resumes the given thread. If @p signo is anything but
/// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
bool
Resume(lldb::tid_t tid, uint32_t signo);
/// Single steps the given thread. If @p signo is anything but
/// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
bool
SingleStep(lldb::tid_t tid, uint32_t signo);
/// Terminate the traced process.
bool
Kill();
lldb_private::Error
Detach(lldb::tid_t tid);
/// Stops the monitoring the child process thread.
void
StopMonitor();
/// Stops the requested thread and waits for the stop signal.
bool
StopThread(lldb::tid_t tid);
// Waits for the initial stop message from a new thread.
bool
WaitForInitialTIDStop(lldb::tid_t tid);
private:
ProcessLinux *m_process;
lldb_private::HostThread m_operation_thread;
lldb_private::HostThread m_monitor_thread;
lldb::pid_t m_pid;
int m_terminal_fd;
// current operation which must be executed on the priviliged thread
Operation *m_operation;
lldb_private::Mutex m_operation_mutex;
// semaphores notified when Operation is ready to be processed and when
// the operation is complete.
sem_t m_operation_pending;
sem_t m_operation_done;
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.
};
/// @class LauchArgs
///
/// @brief Simple structure to pass data to the thread responsible for
/// launching a child process.
struct LaunchArgs : OperationArgs
{
LaunchArgs(ProcessMonitor *monitor,
lldb_private::Module *module,
char const **argv,
char const **envp,
const char *stdin_path,
const char *stdout_path,
const char *stderr_path,
const char *working_dir,
const lldb_private::ProcessLaunchInfo &launch_info);
~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.
const char *m_working_dir; // Working directory or NULL.
const lldb_private::ProcessLaunchInfo &m_launch_info;
};
void
StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
static void *
LaunchOpThread(void *arg);
static bool
Launch(LaunchArgs *args);
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);
static bool
SetDefaultPtraceOpts(const lldb::pid_t);
static void
ServeOperation(OperationArgs *args);
static bool
DupDescriptor(const char *path, int fd, int flags);
static bool
MonitorCallback(void *callback_baton,
lldb::pid_t pid, bool exited, int signal, int status);
static ProcessMessage
MonitorSIGTRAP(ProcessMonitor *monitor,
const siginfo_t *info, lldb::pid_t pid);
static ProcessMessage
MonitorSignal(ProcessMonitor *monitor,
const siginfo_t *info, lldb::pid_t pid);
static ProcessMessage::CrashReason
GetCrashReasonForSIGSEGV(const siginfo_t *info);
static ProcessMessage::CrashReason
GetCrashReasonForSIGILL(const siginfo_t *info);
static ProcessMessage::CrashReason
GetCrashReasonForSIGFPE(const siginfo_t *info);
static ProcessMessage::CrashReason
GetCrashReasonForSIGBUS(const siginfo_t *info);
void
DoOperation(Operation *op);
/// Stops the child monitor thread.
void
StopMonitoringChildProcess();
/// Stops the operation thread used to attach/launch a process.
void
StopOpThread();
};
#endif // #ifndef liblldb_ProcessMonitor_H_