|
libclues
Linux C++ Tracing Library
|
Inheritance diagram for clues::TermTracer:Public Member Functions | |
| TermTracer (const TermTracer &)=delete | |
| TermTracer & | operator= (const TermTracer &)=delete |
Protected Types | |
| enum class | FollowExecContext { YES , NO , ASK , CHECK_PATH , CHECK_GLOB } |
| What to do upon execve. More... | |
| enum class | FollowChildMode { YES , NO , ASK , THREADS } |
| enum class | Flag { SEEN_INITIAL_EXEC = 1 << 0 , DROPPED_TO_LAST_TRACEE = 1 << 1 } |
| using | Flags = cosmos::BitMask<Flag> |
Protected Member Functions | |
| bool | processPars () |
| void | printSyscalls () |
| void | printABISyscalls (const std::string &abi) |
| void | printABIs () |
| cosmos::ExitStatus | main (const int argc, const char **argv) override |
| bool | configureTracee (const cosmos::ProcessID pid) |
| void | runTrace () |
| void | configureLogger () |
| std::string | formatTraceeInvocation (const Tracee &tracee) |
| std::string | formatTraceeInvocation (const std::string &exe, const cosmos::StringVector &cmdline) const |
| void | printPar (std::ostream &trace, const SystemCallItem &value) const |
| void | printParsOnEntry (std::ostream &trace, const SystemCall::ParameterVector &pars) const |
| void | printParsOnExit (std::ostream &trace, const SystemCall::ParameterVector &pars) const |
| bool | followExecutionContext (Tracee &tracee) |
| std::ostream & | traceStream (const Tracee &tracee, const bool new_line=true) |
| Returns the currently active trace output stream, starting a new output line. | |
| void | startNewLine (std::ostream &trace, const Tracee &tracee) |
| Start a new output line concerning `tracee. | |
| bool | storeUnfinishedSyscallCtx () |
| Store an active system call in m_unfinished_syscalls. | |
| void | checkResumedSyscall (const Tracee &tracee) |
Check whether tracee has an unfinished system call pending. | |
| bool | hasActiveSyscall (const Tracee &tracee) const |
| bool | hasActiveSyscall (const cosmos::ProcessID pid) const |
| const SystemCall * | activeSyscall (const cosmos::ProcessID pid) const |
| const SystemCall * | activeSyscall (const Tracee &tracee) const |
| const SystemCall * | activeSyscall () const |
| const SystemCall * | findSyscall (const Tracee &tracee) const |
Find any active or unfinished system call for pid. | |
| bool | isExecSyscall (const SystemCall &sc) const |
| bool | isEnabled (const SystemCall *sc) const |
Returns true if sc is set and supposed to the printed. | |
| const SystemCall * | currentSyscall (const Tracee &tracee) const |
Returns the system call last seen for tracee, or nullptr if there's none. | |
| void | cleanupTracee (const Tracee &tracee) |
| void | updateTracee (const Tracee &tracee, const cosmos::ProcessID old_pid) |
| Update internal data structures in case a tracee changed PID. | |
| bool | seenInitialExec () const |
| void | abortSyscall (const Tracee &tracee) |
Abort syscall if one was active for tracee. | |
| void | checkABI (const Tracee &tracee, const SystemCallInfo &info) |
| Checks the current system call's ABI and reports ABI changes. | |
| void | syscallEntry (Tracee &tracee, const SystemCall &sc, const StatusFlags flags) override |
| A system call is about to be executed in the Tracee. | |
| void | syscallExit (Tracee &tracee, const SystemCall &sc, const StatusFlags flags) override |
| A system call has been finished. | |
| void | signaled (Tracee &tracee, const cosmos::SigInfo &info) override |
| The tracee has received a signal. | |
| void | attached (Tracee &tracee) override |
| The tracee is now properly attached to. | |
| void | exited (Tracee &tracee, const cosmos::WaitStatus status, const StatusFlags flags) override |
| The tracee is about to end execution. | |
| void | newExecutionContext (Tracee &tracee, const std::string &old_exe, const cosmos::StringVector &old_cmdline, const std::optional< cosmos::ProcessID > old_pid) override |
| A new program is executed in the tracee. | |
| void | newChildProcess (Tracee &parent, Tracee &child, const cosmos::ptrace::Event event, const StatusFlags flags) override |
| A new child process has been created. | |
| void | stopped (Tracee &tracee) override |
| The tracee entered group-stop due to a stopping signal. | |
| void | disappeared (Tracee &tracee, const cosmos::ChildState &data) override |
| The tracee disappeared for unclear reasons. | |
Protected Member Functions inherited from clues::EventConsumer | |
| virtual void | resumed (Tracee &tracee) |
| The tracee resumed due to SIGCONT. | |
| virtual void | vforkComplete (Tracee &parent, TraceePtr child) |
A vfork() in parent for child completed. | |
Protected Attributes | |
| Args | m_args |
| Command line arguments and parser. | |
| cosmos::StdLogger | m_logger |
| cosmos ILogger instance for clues library logging. | |
| cosmos::Init | m_cosmos |
| Engine | m_engine |
| libclues main object. | |
| bool | m_print_pars = true |
| Whether to print system call parameters at all (-s 0 disables it). | |
| size_t | m_par_truncation_len = 64 |
| Maximum length of of system call parameter values to print before truncating the output. | |
| std::string | m_exec_context_arg |
| optional argument to m_follow_exec (e.g. path, glob, script) | |
| std::set< SystemCallNr > | m_syscall_filter |
| Whitelist of system calls to trace, if any. | |
| FollowExecContext | m_follow_exec = FollowExecContext::YES |
| Behaviour upon newExecutionContext() | |
| FollowChildMode | m_follow_children = FollowChildMode::NO |
| Behaviour upon newChildProcess() | |
| size_t | m_num_tracees = 0 |
| The number of tracees we're currently dealing with. | |
| Flags | m_flags |
| State flags with global context or carried between different callbacks. | |
| cosmos::ProcessID | m_main_tracee_pid |
| The PID of the main process we're tracing (the one we created or attached to). | |
| std::optional< cosmos::WaitStatus > | m_main_status |
| The WaitStatus of the main process we've seen upon it exiting. | |
| std::optional< std::tuple< cosmos::ProcessID, const SystemCall * > > | m_active_syscall |
| The currently active system call, if any. | |
| std::map< cosmos::ProcessID, const SystemCall * > | m_unfinished_syscalls |
| Unfinished / preempted system calls. | |
| std::map< cosmos::ProcessID, std::pair< cosmos::ProcessID, cosmos::ptrace::Event > > | m_new_tracees |
| Newly created tracees that haven't seen any ptrace stop yet. | |
| clues::ABI | m_last_abi = clues::ABI::UNKNOWN |
| The ABI of the last system call we've seen. | |
Additional Inherited Members | |
Public Types inherited from clues::EventConsumer | |
| enum class | StatusFlag { INTERRUPTED = 1 << 0 , RESUMED = 1 << 1 , LOST_TO_MT_EXIT = 1 << 2 , ABI_CHANGED = 1 << 3 , CLONED_THREAD = 1 << 4 } |
| Different status flags that can appear in callbacks. More... | |
| using | StatusFlags = cosmos::BitMask<StatusFlag> |
Definition at line 25 of file TermTracer.hxx.
|
protected |
Definition at line 58 of file TermTracer.hxx.
|
strongprotected |
| Enumerator | |
|---|---|
| SEEN_INITIAL_EXEC | whether we've seen a ChildTracee's initial newExecutionContext(). |
| DROPPED_TO_LAST_TRACEE | whether we've returned to tracing only a single PID anymore. |
Definition at line 53 of file TermTracer.hxx.
|
strongprotected |
Definition at line 46 of file TermTracer.hxx.
|
strongprotected |
What to do upon execve.
Definition at line 38 of file TermTracer.hxx.
| clues::TermTracer::TermTracer | ( | ) |
Definition at line 65 of file TermTracer.cxx.
|
protected |
Abort syscall if one was active for tracee.
A condition in tracee occurred which requires to abort a possible already started system call trace. This function performs the necessary steps to do so.
Definition at line 741 of file TermTracer.cxx.
|
inlineprotected |
Definition at line 144 of file TermTracer.hxx.
|
inlineprotected |
Definition at line 127 of file TermTracer.hxx.
|
inlineprotected |
Definition at line 140 of file TermTracer.hxx.
|
overrideprotectedvirtual |
The tracee is now properly attached to.
This should be the first call that that is visible for a tracee. It occurs when the ptrace() relationship is established and a defined tracing state has been reached. The Tracee will be in PTRACE_EVENT_STOP.
The following situations can cause an attached() callback:
tracee was actively registered via Engine::addTracee().Engine::addTracee(const cosmos::ProcessID, ..., AttachThreads{true}). Any additional threads that exist in the process will then automatically be attached to. tracee.isInitiallyAttachedThread() will return true in this case. Reimplemented from clues::EventConsumer.
Definition at line 447 of file TermTracer.cxx.
|
protected |
Checks the current system call's ABI and reports ABI changes.
Definition at line 760 of file TermTracer.cxx.
|
protected |
Check whether tracee has an unfinished system call pending.
If tracee has an unfinished system call pending in m_unfinished_syscalls, then clean up the data structure and output appropriate text to indicate the situation.
Definition at line 686 of file TermTracer.cxx.
|
protected |
Definition at line 708 of file TermTracer.cxx.
|
protected |
Definition at line 250 of file TermTracer.cxx.
|
protected |
Definition at line 777 of file TermTracer.cxx.
|
protected |
Returns the system call last seen for tracee, or nullptr if there's none.
This checks both the currently active as well as any unfinished system calls for tracee.
Definition at line 660 of file TermTracer.cxx.
|
overrideprotectedvirtual |
The tracee disappeared for unclear reasons.
This callback can occur in a number of special situations like:
When this call occurs then the tracee can no longer be accessed via the tracing API. data still tells whether the tracee exited regularly or due to a signal.
Reimplemented from clues::EventConsumer.
Definition at line 497 of file TermTracer.cxx.
|
overrideprotectedvirtual |
The tracee is about to end execution.
The tracee is about to either exit regularly, to be killed by a signal or to disappear due to an execve() in a multi-threaded process.
When this callback occurs the tracee can still be inspected using ptrace(). Once execution is continued the tracer <-> tracee relationship is lost.
If the exit happens due to an execve in a multi-threaded process then Status::LOST_TO_MT_EXIT is set in flags.
Reimplemented from clues::EventConsumer.
Definition at line 465 of file TermTracer.cxx.
|
protected |
Find any active or unfinished system call for pid.
Since system calls can be preempted by other tracees we sometimes need to lookup a SystemCall instance in both, the currently active system call and the unfinished system calls. This is mostly relevant during PTRACE_EVENT stops.
If no entry is found for pid, then nullptr is returned.
Definition at line 640 of file TermTracer.cxx.
|
protected |
Definition at line 584 of file TermTracer.cxx.
|
protected |
Definition at line 333 of file TermTracer.cxx.
|
protected |
Definition at line 329 of file TermTracer.cxx.
|
inlineprotected |
Definition at line 123 of file TermTracer.hxx.
|
inlineprotected |
Definition at line 119 of file TermTracer.hxx.
|
protected |
Returns true if sc is set and supposed to the printed.
Definition at line 677 of file TermTracer.cxx.
|
protected |
Definition at line 651 of file TermTracer.cxx.
|
overrideprotected |
Definition at line 807 of file TermTracer.cxx.
|
overrideprotectedvirtual |
A new child process has been created.
The existing tracee parent has created a new child process, which has automatically been attached to and is now represented in child.
Different ways to create a child process exist, these are differentiated via event. Note that some forms of clone() calls will appear as cosmos::ptrace::Event::FORK or cosmos::ptrace::Event::VFORK instead of cosmos::ptrace::Event::CLONE (when the clone() exit signal is set to SIGCHILD).
cosmos::ptrace::Event::VFORK_DONE occurs when the child process exited or exec()'d and can be ignored if there is no need for it.
StatusFlag::CLONED_THREAD will be set in flags in case the new child process is a thread of parent.
Reimplemented from clues::EventConsumer.
Definition at line 564 of file TermTracer.cxx.
|
overrideprotectedvirtual |
A new program is executed in the tracee.
This call occurs after a successful execve() by the tracee.
The new executable path and command line can be retrieved via Tracee::executable() and Tracee::cmdLine(). The previous values are provided as input parameters.
The callee can decide to stop tracing (by detaching) at this point to prevent following the new tracing context.
If the tracee's process was multi-threaded then there is more complexity involved:
former_pid contains the former PID that the exec'ing thread had, which is now continuing as the main thread of the new execution context.Reimplemented from clues::EventConsumer.
Definition at line 521 of file TermTracer.cxx.
|
protected |
Definition at line 244 of file TermTracer.cxx.
|
protected |
Definition at line 200 of file TermTracer.cxx.
|
protected |
Definition at line 352 of file TermTracer.cxx.
|
protected |
Definition at line 257 of file TermTracer.cxx.
|
protected |
Definition at line 302 of file TermTracer.cxx.
|
protected |
Definition at line 190 of file TermTracer.cxx.
|
protected |
Definition at line 69 of file TermTracer.cxx.
|
inlineprotected |
Definition at line 179 of file TermTracer.hxx.
|
overrideprotectedvirtual |
The tracee has received a signal.
This callback notifies about signals being delivered to the tracee.
Reimplemented from clues::EventConsumer.
Definition at line 443 of file TermTracer.cxx.
|
protected |
Start a new output line concerning `tracee.
This prints out any preamble that might be necessary for starting a new trace output line, like [<pid>], when multiple tracees are present.
It also cares about managing the system call state to make sure unfinished system calls are kept track of.
Definition at line 613 of file TermTracer.cxx.
|
overrideprotectedvirtual |
The tracee entered group-stop due to a stopping signal.
Reimplemented from clues::EventConsumer.
Definition at line 491 of file TermTracer.cxx.
|
protected |
Store an active system call in m_unfinished_syscalls.
If there's an active system call then store if in m_unfinished_syscalls and reset it.
The return value indicates whether a traced system call has been interrupted by this operation. Interrupted but filtered-out system calls are not counted here.
Definition at line 627 of file TermTracer.cxx.
|
overrideprotectedvirtual |
A system call is about to be executed in the Tracee.
At this stage only the type of system call as well system call parameters of ItemType::PARAM_IN or ItemType::PARAM_IN_OUT have useful values. Any out parameters and the return value should not be inspected.
Reimplemented from clues::EventConsumer.
Definition at line 367 of file TermTracer.cxx.
|
overrideprotectedvirtual |
A system call has been finished.
If the system call failed then sc.hasErrorCode() returns true and the ErrnoResult can be inspected from sc.error().
If the system call succeeded sc.hasResultValue() returns true and any system call parameters of ItemType::PARAM_OUT or ItemType::PARAM_IN_OUT should have been updated by the kernel and can be updated and processed by the implementation.
If flags[StatusFlag::INTERRUPTED] is set, then the system call was interrupted by a signal and aborted by the kernel. Whether this happens depends on various factors, e.g. if the SA_RESTART flag is set in the Tracee for the signal which was received and also on the type of system call which is executed. If automatic system call restarting is not active then a regular EINTR error return will be observed and the Tracee has to actively deal with the situation.
libclues tries to keep track of when an automatically restarted system call will be resumed and will set flags[StatusFlag::RESUMED] during syscallEntry() accordingly. There are two different scenarios to take into account here:
restart_syscall() is injected by the kernel. This is the case when the signal was SIGSTOP and the system call was time-related, like clock_nanosleep(), to give userspace (libc) the chance to adjust times before resuming the system call.
libclues looks up the original system call and will not report SystemCallNr::RESTART_SYSCALL, but the resumed system call and will set StatusFlag::RESUMED accordingly.
rt_sigreturn() system call to appear to make out the end of the interruption and then looks for the next system call of the same type as the interrupted system call. When this situation is detected then StatusFlag::RESUMED is set as well. This needs to be reset before returning from this function but after checkResumedSyscall() is called, if at all.
Reimplemented from clues::EventConsumer.
Definition at line 402 of file TermTracer.cxx.
|
protected |
Returns the currently active trace output stream, starting a new output line.
Definition at line 601 of file TermTracer.cxx.
|
protected |
Update internal data structures in case a tracee changed PID.
Definition at line 723 of file TermTracer.cxx.
|
protected |
The currently active system call, if any.
This is only keeping track of system calls that are actually printed to stdout. Ignored system calls are not kept track of here. The Tracee objects keep track of any active system calls already.
Definition at line 261 of file TermTracer.hxx.
|
protected |
Command line arguments and parser.
Definition at line 224 of file TermTracer.hxx.
|
protected |
Definition at line 228 of file TermTracer.hxx.
|
protected |
libclues main object.
Definition at line 231 of file TermTracer.hxx.
|
protected |
optional argument to m_follow_exec (e.g. path, glob, script)
Definition at line 238 of file TermTracer.hxx.
|
protected |
State flags with global context or carried between different callbacks.
Definition at line 249 of file TermTracer.hxx.
|
protected |
Behaviour upon newChildProcess()
Definition at line 244 of file TermTracer.hxx.
|
protected |
Behaviour upon newExecutionContext()
Definition at line 242 of file TermTracer.hxx.
|
protected |
The ABI of the last system call we've seen.
Definition at line 276 of file TermTracer.hxx.
|
protected |
cosmos ILogger instance for clues library logging.
Definition at line 227 of file TermTracer.hxx.
|
protected |
The WaitStatus of the main process we've seen upon it exiting.
Definition at line 253 of file TermTracer.hxx.
|
protected |
The PID of the main process we're tracing (the one we created or attached to).
Definition at line 251 of file TermTracer.hxx.
|
protected |
Newly created tracees that haven't seen any ptrace stop yet.
The values contain the PID that created the tracee and the type of event that led to the creation.
Definition at line 274 of file TermTracer.hxx.
|
protected |
The number of tracees we're currently dealing with.
Definition at line 247 of file TermTracer.hxx.
|
protected |
Maximum length of of system call parameter values to print before truncating the output.
Definition at line 236 of file TermTracer.hxx.
|
protected |
Whether to print system call parameters at all (-s 0 disables it).
Definition at line 234 of file TermTracer.hxx.
|
protected |
Whitelist of system calls to trace, if any.
Definition at line 240 of file TermTracer.hxx.
|
protected |
Unfinished / preempted system calls.
Unfinished in this context is purely tracing related, it only means that we already started printing system call entry for one tracee, while another event came in, preempting this line.
Definition at line 268 of file TermTracer.hxx.