libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
SystemCall.hxx
1#pragma once
2
3// C++
4#include <iosfwd>
5#include <memory>
6#include <optional>
7#include <string_view>
8#include <vector>
9
10// cosmos
11#include <cosmos/error/RuntimeError.hxx>
12#include <cosmos/proc/ptrace.hxx>
13
14// clues
15#include <clues/ErrnoResult.hxx>
16#include <clues/items/error.hxx>
17#include <clues/sysnrs/fwd.hxx>
18#include <clues/types.hxx>
19#include <clues/utils.hxx>
20
21namespace clues {
22 class SystemCall;
23 class SystemCallInfo;
24 class Tracee;
25}
26
27std::ostream& operator<<(std::ostream &o, const clues::SystemCall &sc);
28
29namespace clues {
30
31class SystemCallItem;
32using SystemCallPtr = std::shared_ptr<SystemCall>;
33using SystemCallItemPtr = SystemCallItem*;
34
36
47class CLUES_API SystemCall {
48 friend std::ostream& ::operator<<(std::ostream&, const SystemCall&);
49public: // types
50
52 using ParameterVector = std::vector<SystemCallItemPtr>;
53
54public: // functions
55
57
61 SystemCall(const SystemCallNr nr);
62
63 virtual ~SystemCall() {}
64
65 // mark as non-copyable
66 SystemCall(const SystemCall &other) = delete;
67 SystemCall& operator=(const SystemCall &other) = delete;
68
70
75 void setEntryInfo(const Tracee &proc, const SystemCallInfo &info);
76
78
83 void setExitInfo(const Tracee &proc, const SystemCallInfo &info);
84
86 std::string_view name() const { return m_name; }
88 size_t numPars() const { return m_pars.size(); }
90 SystemCallNr callNr() const { return m_nr; }
91
93 const ParameterVector& parameters() const { return m_pars; }
95 SystemCallItemPtr result() const { return hasResultValue() ? m_return : nullptr; }
97 std::optional<ErrnoResult> error() const { return m_error; }
98
99 bool hasOutParameter() const;
100
101 bool hasResultValue() const {
102 return m_error == std::nullopt;
103 }
104
105 bool hasErrorCode() const {
106 return !hasResultValue();
107 }
108
110 ABI abi() const {
111 return m_abi;
112 }
113
114 bool is32BitEmulationABI() const {
115 return get_default_abi() == ABI::X86_64 && abi() == ABI::I386;
116 }
117
119
122 static const char* name(const SystemCallNr nr);
123
125 static bool validNr(const SystemCallNr nr);
126
128
135 return m_info;
136 }
137
138protected: // functions
139
140 void fillParameters(const Tracee &proc, const SystemCallInfo &info);
141
143
152 m_return = &ret;
153 m_return->setSystemCall(*this);
154 if (!ret.isReturnValue()) {
155 throw cosmos::RuntimeError{"added non-return-value as return item"};
156 }
157 }
158
159 void addParameters() {}
160
161 template <typename T, typename... Targs>
162 void addParameters(T &par, Targs& ...rest) {
163 par.setSystemCall(*this);
164 m_pars.push_back(&par);
165 addParameters(rest...);
166 }
167
168 template <typename... Targs>
169 void setParameters(Targs& ...args) {
170 m_pars.clear();
171 addParameters(args...);
172 }
173
175
186 virtual bool check2ndPass(const Tracee &) { return false; };
187
189 virtual void prepareNewSystemCall() {}
190
192
201 virtual void updateFDTracking(const Tracee &proc) { (void)proc; }
202
203 /*
204 * these are wrappers to make use of SystemCall's friendship towards
205 * Tracee, since specializations wouldn't be allowed to call
206 * `Tracee::dropFD` etc.
207 */
208
209 void dropFD(const Tracee &proc, const cosmos::FileNum num);
210
211 void trackFD(const Tracee &proc, FDInfo &&info);
212
213protected: // data
214
218 const std::string_view m_name;
220 const SystemCallInfo *m_info = nullptr;
222 SystemCallItemPtr m_return;
224 std::optional<ErrnoResult> m_error;
228 ABI m_abi = ABI::UNKNOWN;
229};
230
232CLUES_API SystemCallPtr create_syscall(const SystemCallNr nr);
233
234} // end ns
Extended ptrace system call state information.
Base class for any kind of system call parameter or return value.
Access to System Call Data.
SystemCallItemPtr m_return
The return value of the system call.
std::string_view name() const
Returns the system call's human readable name.
void setReturnItem(SystemCallItem &ret)
Sets the return value system call item.
const SystemCallInfo * m_info
Current system call info during entry/exit processing, nullptr otherwise.
ABI abi() const
Returns the system call ABi seen during system call entry.
const ParameterVector & parameters() const
Access to the parameters associated with this system call.
size_t numPars() const
Returns the number of parameters for this system call.
virtual void prepareNewSystemCall()
Perform any necessary actions before processing a new system call entry event.
const std::string_view m_name
The basic name of the system call.
const SystemCallInfo * currentInfo() const
Access the current SystemCallInfo if currently processing syscall entry/exit.
SystemCallItemPtr result() const
Access to the return value parameter associated with this system call.
std::optional< ErrnoResult > error() const
Access to the errno result seen for this system call.
SystemCall(const SystemCallNr nr)
Instantiates a new SystemCall object with given properties.
SystemCallNr callNr() const
Returns the system call table number for this system call.
std::optional< ErrnoResult > m_error
If the system call fails, this is the error code.
std::vector< SystemCallItemPtr > ParameterVector
Vector of the parameters required for a system call.
ABI m_abi
The current system call ABI which is in effect.
ParameterVector m_pars
The array of system call parameters, if any.
SystemCallNr m_nr
The raw system call number of the system call.
virtual void updateFDTracking(const Tracee &proc)
Update file descriptor tracking.
virtual bool check2ndPass(const Tracee &)
Check whether a second pass needs to be made processing parameters.
Base class for traced processes.
Definition Tracee.hxx:39
SystemCallPtr create_syscall(const SystemCallNr nr)
Creates a dynamically allocated SystemCall instance for the given system call number.
constexpr ABI get_default_abi()
Returns the default ABI for this system.
Definition utils.hxx:59
ABI
System Call ABI.
Definition types.hxx:62
SystemCallNr
Abstract system call number usable across architectures and ABIs.
Definition generic.hxx:29
Contextual information about a file descriptor in a Tracee.
Definition types.hxx:75