|
libclues
Linux C++ Tracing Library
|
Base class for traced processes. More...
#include <Tracee.hxx>
Inheritance diagram for clues::Tracee:Public Types | |
| enum class | State { UNKNOWN , RUNNING , SYSCALL_ENTER_STOP , SYSCALL_EXIT_STOP , SIGNAL_DELIVERY_STOP , GROUP_STOP , EVENT_STOP , DEAD , DETACHED } |
| Current tracing state for a single tracee. More... | |
| enum class | Flag { WAIT_FOR_ATTACH_STOP = 1 << 0 , DETACH_AT_NEXT_STOP = 1 << 1 , INJECTED_SIGSTOP = 1 << 2 , INJECTED_SIGCONT = 1 << 3 , SYSCALL_ENTERED = 1 << 4 , WAIT_FOR_EXITED = 1 << 5 , ATTACH_THREADS_PENDING = 1 << 6 , SEEN_SIGRETURN = 1 << 7 } |
| Different flags reflecting the tracer status. More... | |
| using | Flags = cosmos::BitMask<Flag> |
Public Member Functions | |
| const std::string & | executable () const |
| const cosmos::StringVector & | cmdLine () const |
| const FDInfoMap & | fdInfoMap () const |
| Provides access to the current knowledge about file descriptors in the tracee. | |
| cosmos::ProcessID | pid () const |
| bool | alive () const |
| size_t | maxBufferPrefetch () const |
| void | setMaxBufferPrefetch (const size_t bytes) |
| Sets an upper limit for the retrieval of variable-length buffer parameters in system calls. | |
| size_t | syscallCtr () const |
| Returns the number of system calls observed so far. | |
| std::optional< cosmos::Signal > | stopSignal () const |
| For state() == State::GROUP_STOP this returns the stopping signal that caused it. | |
| State | state () const |
| State | prevState () const |
| bool | isEnterStop () const |
| bool | isExitStop () const |
| Flags | flags () const |
| std::optional< cosmos::ChildState > | exitData () const |
| Returns possible tracee exit data. | |
| virtual void | attach (const FollowChildren follow_children, const AttachThreads attach_threads=AttachThreads{false}) |
| Logic to handle attaching to the tracee. | |
| bool | detach () |
| Attempt to detach the Tracee. | |
| long | getData (const ForeignPtr addr) const |
| Reads a word of data from the tracee's memory. | |
| void | readString (const ForeignPtr addr, std::string &out) const |
| Reads a zero-terminated C-string from the tracee. | |
| void | readBlob (const ForeignPtr addr, char *buffer, const size_t bytes) const |
| Reads an arbitrary binary blob of fixed length from the tracee. | |
| template<typename T, bool CHECK_TRIVIAL = true> | |
| bool | readStruct (const ForeignPtr addr, T &out) const |
Reads a system call struct from the tracee's address space into out. | |
| template<typename VECTOR> | |
| void | readVector (const ForeignPtr pointer, VECTOR &out) const |
Reads in a zero terminated array of data items into the STL-vector like parameter out. | |
| virtual bool | isChildProcess () const |
| Returns whether the tracee is a child process created by us. | |
| bool | isThreadGroupLeader () const |
| Checks in the proc file system whether the Tracee is a thread group leader. | |
| bool | isInitiallyAttachedThread () const |
| Indicates whether this Tracee is an automatically attached thread. | |
| std::optional< SystemCallNr > | currentSystemCallNr () const |
| Returns the number of the currently running system call, if any. | |
| const std::optional< SystemCallInfo > & | currentSystemCallInfo () const |
| const Engine & | engine () const |
Static Public Member Functions | |
| static const char * | getStateLabel (const State state) |
Protected Member Functions | |
| Tracee (Engine &engine, EventConsumer &consumer, TraceePtr sibling=nullptr) | |
| void | processEvent (const cosmos::ChildState &data) |
| Process the given ptrace event. | |
| void | updateExecutable () |
| void | updateCmdLine () |
| void | updateExecInfo () |
| void | syncFDsAfterExec () |
| void | changeState (const State new_state) |
| void | interrupt () |
| Forces the traced process to stop. | |
| void | restart (const cosmos::Tracee::RestartMode mode=cosmos::Tracee::RestartMode::CONT, const std::optional< cosmos::Signal > signal={}) |
Restarts the traced process, optionally delivering signal. | |
| void | setOptions (const cosmos::ptrace::Opts opts) |
Applies the given trace flags. | |
| void | seize (const cosmos::ptrace::Opts opts) |
| Makes the tracee a tracee. | |
| void | setPID (const cosmos::ProcessID tracee) |
| Sets the tracee PID. | |
| template<ABI abi> | |
| void | getRegisters (RegisterSet< abi > &rs) |
| Reads the current register set from the process. | |
| void | handleSystemCall () |
| void | handleSystemCallEntry () |
| void | handleSystemCallExit () |
| void | handleStateMismatch () |
| void | handleSignal (const cosmos::SigInfo &info) |
| void | handleEvent (const cosmos::ChildState &data, const cosmos::ptrace::Event event, const cosmos::Signal signal) |
| void | handleStopEvent (const cosmos::Signal signal) |
| void | handleExitEvent () |
| void | handleExecEvent (const cosmos::ProcessID main_pid) |
| void | handleNewChildEvent (const cosmos::ptrace::Event event) |
| void | handleAttached () |
| void | syncState (Tracee &other) |
| void | attachThreads () |
| template<typename FILLER> | |
| void | fillData (ForeignPtr addr, FILLER &filler) const |
Reads data from the Tracee starting at addr and feeds it to filler until it's saturated. | |
| virtual void | cleanupChild () |
| void | verifyArch () |
| Verifies the tracee's architecture according to m_syscall_info, throws on mismatch. | |
| std::optional< SystemCallNr > | getInitialSyscallNr (const ABI abi) const |
Returns the initial system call nr. stored in m_initial_regset, if available for abi. | |
| void | getInitialRegisters () |
| void | unshareProcessData () |
| void | doDetach () |
| Actually perform a detach without checking tracee state. | |
| void | handleError (const cosmos::ApiError &error) |
| Handle ApiErrors raised from ptrace() requests. | |
| bool | hasClonedThread () const |
| Returns whether the current/last seen system call was a clone() for a thread. | |
| void | trackFD (FDInfo &&info) const |
| Track a new file descriptor for this Tracee's thread group. | |
| void | dropFD (const cosmos::FileNum fd) const |
| Drop a file descriptor from the tracking of the Tracee's thread group. | |
Protected Attributes | |
| Engine & | m_engine |
| The engine that manages this tracee. | |
| EventConsumer & | m_consumer |
| Callback interface receiving our information. | |
| State | m_state = State::UNKNOWN |
| The current state the tracee is in. | |
| State | m_prev_state = State::UNKNOWN |
| The previous Tracee state we've seen (except RUNNING). | |
| Flags | m_flags |
| These keep track of various state on the tracer side. | |
| cosmos::Tracee | m_ptrace |
| libcosmos API for the Tracee. | |
| cosmos::ptrace::Opts | m_ptrace_opts |
| The options we've set for ptrace(). | |
| std::optional< SystemCallInfo > | m_syscall_info |
| The current system call information, if any. | |
| SystemCallDB | m_syscall_db |
| Reusable database object for tracing system calls. | |
| SystemCall * | m_current_syscall = nullptr |
| Holds state for the currently executing system call. | |
| SystemCall * | m_interrupted_syscall = nullptr |
| Previous system call, if it has been interrupted. | |
| cosmos::Tracee::RestartMode | m_restart_mode = cosmos::Tracee::RestartMode::CONT |
| current RestartMode to use. | |
| std::optional< cosmos::Signal > | m_inject_sig |
| signal to inject upon next restart of the tracee. | |
| std::optional< cosmos::ChildState > | m_exit_data |
| If tracee exit was observed then this contains the final exit data. | |
| std::optional< cosmos::ProcessID > | m_initial_attacher |
| If this Tracee was automatically attached due to AttachThreads{true} then this contains the ProcessID of the initial Thread that caused this. | |
| size_t | m_syscall_ctr = 0 |
| Number of system calls observed. | |
| AnyRegisterSet | m_initial_regset |
| Register set observed during initial attach event stop. | |
| std::optional< cosmos::Signal > | m_stop_signal |
| For State::GROUP_STOP this contains the signal that caused it. | |
| ProcessDataPtr | m_process_data |
| Shared process data. | |
| size_t | m_max_buffer_prefetch = 128 |
| Number of bytes system calls will fetch for variable-length data buffers. | |
Static Protected Attributes | |
| static constexpr std::array< cosmos::Signal, 4 > | STOPPING_SIGNALS |
| Array of signals that cause tracee stop. | |
Friends | |
| class | Engine |
| class | SystemCall |
Base class for traced processes.
The concrete implementation of Tracee defines the means of how the traced process is attached to and detached from etc.
Definition at line 39 of file Tracee.hxx.
| using clues::Tracee::Flags = cosmos::BitMask<Flag> |
Definition at line 72 of file Tracee.hxx.
|
strong |
Different flags reflecting the tracer status.
Definition at line 61 of file Tracee.hxx.
|
strong |
Current tracing state for a single tracee.
These states are modelled after the states described in man(2) ptrace.
Definition at line 48 of file Tracee.hxx.
|
virtual |
Definition at line 108 of file Tracee.cxx.
|
explicitprotected |
| [in] | sibling | establish a process data sharing relationship with `sibling. Do this only if you knows that both tracees are related (e.g. member of the same thread group). |
Definition at line 100 of file Tracee.cxx.
|
inline |
Definition at line 95 of file Tracee.hxx.
|
virtual |
Logic to handle attaching to the tracee.
| [in] | follow_children | If true then newly created child processes will automatically be attached. The EventConsumer interface will received a newChildProcess() callback once a new child process has been attached. This covers all ways by which new child processes can be created (fork, vfork, clone). |
Definition at line 133 of file Tracee.cxx.
|
protected |
Attach to all threads of the current Tracee's process.
Definition at line 725 of file Tracee.cxx.
|
protected |
Definition at line 336 of file Tracee.cxx.
|
inlineprotectedvirtual |
Definition at line 393 of file Tracee.hxx.
|
inline |
Definition at line 82 of file Tracee.hxx.
|
inline |
Definition at line 276 of file Tracee.hxx.
| std::optional< SystemCallNr > clues::Tracee::currentSystemCallNr | ( | ) | const |
Returns the number of the currently running system call, if any.
Definition at line 974 of file Tracee.cxx.
| bool clues::Tracee::detach | ( | ) |
Attempt to detach the Tracee.
Detaching is only possible if the tracee is in a stopped state. If this is the case then the detach is performed and true is returned.
Otherwise a future detach will be prepared by adjusting state flags and false is returned.
If the tracee is already detached then nothing happens and true is returned.
Definition at line 177 of file Tracee.cxx.
|
protected |
Actually perform a detach without checking tracee state.
This can throw if the current trace state doesn't allow detaching.
Definition at line 206 of file Tracee.cxx.
|
protected |
Drop a file descriptor from the tracking of the Tracee's thread group.
Definition at line 1039 of file Tracee.cxx.
|
inline |
Definition at line 280 of file Tracee.hxx.
|
inline |
Definition at line 78 of file Tracee.hxx.
|
inline |
Returns possible tracee exit data.
After the tracee is detached, if tracee exit was observed, this returns the tracee exit information (it's exit status or kill signal etc.).
Definition at line 169 of file Tracee.hxx.
|
inline |
Provides access to the current knowledge about file descriptors in the tracee.
Definition at line 87 of file Tracee.hxx.
|
protected |
Reads data from the Tracee starting at addr and feeds it to filler until it's saturated.
Reads data from the Tracee and feeds it to filler until it's saturated.
Definition at line 924 of file Tracee.cxx.
|
inline |
Definition at line 159 of file Tracee.hxx.
| long clues::Tracee::getData | ( | const ForeignPtr | addr | ) | const |
Reads a word of data from the tracee's memory.
The word found at addr is returned from this function on success. On error an exception is thrown.
Definition at line 917 of file Tracee.cxx.
|
protected |
Definition at line 986 of file Tracee.cxx.
|
protected |
Returns the initial system call nr. stored in m_initial_regset, if available for abi.
Definition at line 818 of file Tracee.cxx.
|
protected |
Reads the current register set from the process.
This is only possible it the tracee is currently in stopped state.
Definition at line 909 of file Tracee.cxx.
|
static |
Definition at line 114 of file Tracee.cxx.
|
protected |
Definition at line 689 of file Tracee.cxx.
|
protected |
Handle ApiErrors raised from ptrace() requests.
This function will potentially clean up object state but can also rethrow the exception, thus it must be called from a catch block.
Definition at line 219 of file Tracee.cxx.
|
protected |
Definition at line 536 of file Tracee.cxx.
|
protected |
Definition at line 620 of file Tracee.cxx.
|
protected |
Definition at line 581 of file Tracee.cxx.
|
protected |
Definition at line 680 of file Tracee.cxx.
|
protected |
Definition at line 524 of file Tracee.cxx.
|
protected |
Definition at line 371 of file Tracee.cxx.
|
protected |
Definition at line 556 of file Tracee.cxx.
|
protected |
Definition at line 381 of file Tracee.cxx.
|
protected |
Definition at line 410 of file Tracee.cxx.
|
protected |
Definition at line 477 of file Tracee.cxx.
|
protected |
Returns whether the current/last seen system call was a clone() for a thread.
Definition at line 237 of file Tracee.cxx.
|
inlineprotected |
Forces the traced process to stop.
Definition at line 328 of file Tracee.hxx.
|
inlinevirtual |
Returns whether the tracee is a child process created by us.
If the tracee is a child process then the tracer needs to take care of its lifecycle, while non-related tracee's can just be detached from.
Reimplemented in clues::ChildTracee.
Definition at line 245 of file Tracee.hxx.
|
inline |
Definition at line 151 of file Tracee.hxx.
|
inline |
Definition at line 155 of file Tracee.hxx.
|
inline |
Indicates whether this Tracee is an automatically attached thread.
If this is true then the Tracee was attached to due to the AttachThreads{true} setting.
Definition at line 269 of file Tracee.hxx.
| bool clues::Tracee::isThreadGroupLeader | ( | ) | const |
Checks in the proc file system whether the Tracee is a thread group leader.
Calling this function is only safe during a ptrace stop.
This information is relevant for the execve in a multi-threaded process situation.
Sadly this bit is not easily accessible via system calls. The only way to determine it is from /proc/<pid>/status or implicitly e.g. a newly fork()'ed process is a thread-group leader while clone()'ed processes can be thread-group leaders (if its not a thread that has been created).
Definition at line 947 of file Tracee.cxx.
|
inline |
Definition at line 99 of file Tracee.hxx.
|
inline |
Definition at line 91 of file Tracee.hxx.
|
inline |
Definition at line 147 of file Tracee.hxx.
|
protected |
Process the given ptrace event.
This call will be invoked by the Engine class to drive the tracing logic.
Definition at line 837 of file Tracee.cxx.
| void clues::Tracee::readBlob | ( | const ForeignPtr | addr, |
| char * | buffer, | ||
| const size_t | bytes ) const |
Reads an arbitrary binary blob of fixed length from the tracee.
Definition at line 981 of file Tracee.cxx.
| void clues::Tracee::readString | ( | const ForeignPtr | addr, |
| std::string & | out ) const |
Reads a zero-terminated C-string from the tracee.
Read from the tracee's address space starting at addr into the C++ string object out.
Definition at line 935 of file Tracee.cxx.
|
inline |
Reads a system call struct from the tracee's address space into out.
true if out could be filled, false otherwise (e.g. nullptr was encouneterd). Definition at line 221 of file Tracee.hxx.
| void clues::Tracee::readVector | ( | const ForeignPtr | pointer, |
| VECTOR & | out ) const |
Reads in a zero terminated array of data items into the STL-vector like parameter out.
Definition at line 940 of file Tracee.cxx.
|
inlineprotected |
Restarts the traced process, optionally delivering signal.
Definition at line 333 of file Tracee.hxx.
|
inlineprotected |
Makes the tracee a tracee.
Definition at line 344 of file Tracee.hxx.
|
inline |
Sets an upper limit for the retrieval of variable-length buffer parameters in system calls.
System calls like read() and write() process variable-length raw buffer data, which can be quite large. Fetching the complete buffer contents from tracee memory for all these system calls can affect tracing performance, especially when the full buffer data is not actually needed by the tracer application.
By default only a small part of the buffer data will be automatically fetched by specializations of SystemCall. You can set this to 0 to completely disable prefetching of buffer data or to SIZE_MAX to always fetch the complete buffers from tracees. In case the buffer data has not been completely pre-fetched, but the application is interested in the buffer data for a specific system call only, then the application can explicitly fetch the rest of the data from the tracee during a system call stop event.
This is a per-tracee setting to allow tracee-specific variations of this setting.
Definition at line 123 of file Tracee.hxx.
|
inlineprotected |
Applies the given trace flags.
Definition at line 339 of file Tracee.hxx.
|
protected |
Sets the tracee PID.
Definition at line 129 of file Tracee.cxx.
|
inline |
Definition at line 143 of file Tracee.hxx.
|
inline |
For state() == State::GROUP_STOP this returns the stopping signal that caused it.
Definition at line 137 of file Tracee.hxx.
|
protected |
Definition at line 309 of file Tracee.cxx.
|
protected |
Definition at line 828 of file Tracee.cxx.
|
inline |
Returns the number of system calls observed so far.
This counts the number of system call entries observed while tracing this PID.
Definition at line 132 of file Tracee.hxx.
|
protected |
Track a new file descriptor for this Tracee's thread group.
This function is intended in the context of SystemCall exit based on the knowledge of concrete SystemCall implementations.
Definition at line 1024 of file Tracee.cxx.
|
protected |
Definition at line 1020 of file Tracee.cxx.
|
protected |
Definition at line 284 of file Tracee.cxx.
|
inlineprotected |
Definition at line 318 of file Tracee.hxx.
|
protected |
Definition at line 257 of file Tracee.cxx.
|
protected |
Verifies the tracee's architecture according to m_syscall_info, throws on mismatch.
Definition at line 777 of file Tracee.cxx.
|
friend |
Definition at line 40 of file Tracee.hxx.
|
friend |
Definition at line 41 of file Tracee.hxx.
|
protected |
Callback interface receiving our information.
Definition at line 436 of file Tracee.hxx.
|
protected |
Holds state for the currently executing system call.
Definition at line 452 of file Tracee.hxx.
|
protected |
The engine that manages this tracee.
Definition at line 434 of file Tracee.hxx.
|
protected |
If tracee exit was observed then this contains the final exit data.
Definition at line 460 of file Tracee.hxx.
|
protected |
These keep track of various state on the tracer side.
Definition at line 442 of file Tracee.hxx.
|
protected |
If this Tracee was automatically attached due to AttachThreads{true} then this contains the ProcessID of the initial Thread that caused this.
Definition at line 462 of file Tracee.hxx.
|
protected |
Register set observed during initial attach event stop.
Definition at line 466 of file Tracee.hxx.
|
protected |
signal to inject upon next restart of the tracee.
Definition at line 458 of file Tracee.hxx.
|
protected |
Previous system call, if it has been interrupted.
Definition at line 454 of file Tracee.hxx.
|
protected |
Number of bytes system calls will fetch for variable-length data buffers.
Definition at line 472 of file Tracee.hxx.
|
protected |
The previous Tracee state we've seen (except RUNNING).
Definition at line 440 of file Tracee.hxx.
|
protected |
Shared process data.
Definition at line 470 of file Tracee.hxx.
|
protected |
libcosmos API for the Tracee.
Definition at line 444 of file Tracee.hxx.
|
protected |
The options we've set for ptrace().
Definition at line 446 of file Tracee.hxx.
|
protected |
current RestartMode to use.
Definition at line 456 of file Tracee.hxx.
|
protected |
The current state the tracee is in.
Definition at line 438 of file Tracee.hxx.
|
protected |
For State::GROUP_STOP this contains the signal that caused it.
Definition at line 468 of file Tracee.hxx.
|
protected |
Number of system calls observed.
Definition at line 464 of file Tracee.hxx.
|
protected |
Reusable database object for tracing system calls.
Definition at line 450 of file Tracee.hxx.
|
protected |
The current system call information, if any.
Definition at line 448 of file Tracee.hxx.
|
staticconstexprprotected |
Array of signals that cause tracee stop.
Stopping signals are treated specially during tracing since they result in a group-stop state change. This constexpr array is used to identify them.
Definition at line 292 of file Tracee.hxx.