libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
Engine.hxx
1#pragma once
2
3// C++
4#include <map>
5#include <memory>
6#include <optional>
7
8// cosmos
9#include <cosmos/BitMask.hxx>
10#include <cosmos/proc/ptrace.hxx>
11#include <cosmos/proc/types.hxx>
12#include <cosmos/string.hxx>
13
14// clues
15#include <clues/types.hxx>
16
17namespace cosmos {
18 struct ChildState;
19}
20
21namespace clues {
22
23class Tracee;
24class EventConsumer;
25class SystemCall;
26
28
45class CLUES_API Engine {
46 friend class Tracee;
47public: // types
48
49 enum class FormatFlag : uint64_t {
50 FD_INFO = 1,
51 };
52
53 using FormatFlags = cosmos::BitMask<FormatFlag>;
54
55public: // functions
56
58 explicit Engine(EventConsumer &consumer) :
59 m_consumer{consumer} {
60 }
61
63
67 virtual ~Engine();
68
70
97 TraceePtr addTracee(const cosmos::ProcessID pid, const FollowChildren follow_children,
98 const AttachThreads attach_threads, const cosmos::ProcessID sibling = cosmos::ProcessID::INVALID);
99
101
118 TraceePtr addTracee(const cosmos::StringVector &cmdline, const FollowChildren follow_children);
119
121
127 void trace();
128
130
152 void stop(const std::optional<cosmos::Signal> signal);
153
154 FormatFlags formatFlags() const {
155 return m_format_flags;
156 }
157
159
163 void setFormatFlags(const FormatFlags flags) {
164 m_format_flags = flags;
165 }
166
167protected: // types
168
169 using TraceeMap = std::map<cosmos::ProcessID, TraceePtr>;
170
171 using EventMap = std::map<cosmos::ProcessID, cosmos::ChildState>;
172
180
181protected: // functions
182
183 void checkCleanupTracee(TraceeMap::iterator it);
184
185 void checkUnknownEvents();
186
187 Decision handleEvent(const cosmos::ChildState &data);
188
189 void handleNoChildren();
190
192
198 Decision checkUnknownTraceeEvent(const cosmos::ChildState &data);
199
200 bool tryUpdateTraceePID(const cosmos::ProcessID old_pid, const cosmos::ProcessID new_pid);
201
203
209 void handleAutoAttach(Tracee &parent, const cosmos::ProcessID pid,
210 const cosmos::ptrace::Event event, const SystemCall &sc);
211
213 TraceePtr handleSubstitution(const cosmos::ProcessID old_pid);
214
215protected: // data
216
218 TraceeMap m_tracees;
222 cosmos::ProcessID m_newly_attached_pid = cosmos::ProcessID::INVALID;
223 EventConsumer &m_consumer;
225 FormatFlags m_format_flags;
226};
227
228} // end ns
Main class for configuring and running libclues.
Definition Engine.hxx:45
Engine(EventConsumer &consumer)
Creates a new Engine that reports events to consumer.
Definition Engine.hxx:58
TraceeMap m_tracees
Currently active tracees.
Definition Engine.hxx:218
Decision
Different decisions what to do with ptrace events.
Definition Engine.hxx:174
@ DONE
the event has been successfully processed.
Definition Engine.hxx:178
@ STORE
store the event for later.
Definition Engine.hxx:177
@ DROP
ignore/drop the event.
Definition Engine.hxx:176
@ RETRY
retry processing the event.
Definition Engine.hxx:175
void setFormatFlags(const FormatFlags flags)
Change formatting behaviour for system calls.
Definition Engine.hxx:163
EventMap m_unknown_events
Unknown ptrace events stored for later processing.
Definition Engine.hxx:220
cosmos::ProcessID m_newly_attached_pid
The PID of a newly auto-attached Tracee, if any.
Definition Engine.hxx:222
FormatFlags m_format_flags
Format settings for all tracees attached to this engine.
Definition Engine.hxx:225
@ FD_INFO
print detailed file descriptor information
Definition Engine.hxx:50
Callback interface for consumers of tracing events.
Access to System Call Data.
Base class for traced processes.
Definition Tracee.hxx:39
cosmos::NamedBool< struct attach_threads_t, true > AttachThreads
A strong boolean type denoting whether to automatically all other threads of a process.
Definition types.hxx:27
cosmos::NamedBool< struct follow_children_t, true > FollowChildren
A strong boolean type denoting whether to automatically attach to newly created child processes.
Definition types.hxx:24