libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
fs.hxx
1#pragma once
2
3// C++
4#include <optional>
5#include <string_view>
6
7// Linux
8#include <sys/stat.h>
9
10// cosmos
11#include <cosmos/fs/DirEntry.hxx>
12#include <cosmos/fs/FileStatus.hxx>
13#include <cosmos/fs/filesystem.hxx>
14#include <cosmos/utils.hxx>
15
16// clues
17#include <clues/items/fcntl.hxx>
18#include <clues/items/items.hxx>
19#include <clues/SystemCallItem.hxx>
20#include <clues/types.hxx>
21
22namespace clues::item {
23
24using AtSemantics = cosmos::NamedBool<struct at_semantics_t, false>;
25
27class CLUES_API FileDescriptor :
28 public SystemCallItem {
29
30public: // functions
31
39 const AtSemantics at_semantics = AtSemantics{false},
40 const std::string_view short_name = "fd",
41 const std::string_view long_name = "file descriptor") :
42 SystemCallItem{type, short_name, long_name},
43 m_at_semantics{at_semantics} {
44 }
45
46 std::string str() const override;
47
48 auto fd() const {
49 return m_fd;
50 }
51
53
57 const std::optional<FDInfo>& info() const {
58 return m_info;
59 }
60
61protected: // functions
62
63 void processValue(const Tracee &) override;
64
65protected: // data
66
67 cosmos::FileNum m_fd;
68
69 std::optional<FDInfo> m_info;
70
71 const AtSemantics m_at_semantics = AtSemantics{false};
72};
73
75class CLUES_API OpenFlagsValue :
76 public SystemCallItem {
77public:
78 explicit OpenFlagsValue(const ItemType type = ItemType::PARAM_IN) :
79 SystemCallItem{type, "flags", "open flags"} {
80 }
81
82 std::string str() const override;
83
84 cosmos::OpenMode mode() const {
85 return m_mode;
86 }
87
88 cosmos::OpenFlags flags() const {
89 return m_flags;
90 }
91
92protected: // functions
93
94 void processValue(const Tracee&) override;
95
96protected: // data
97
98 cosmos::OpenMode m_mode;
99 cosmos::OpenFlags m_flags;
100};
101
103class CLUES_API AtFlagsValue :
104 public SystemCallItem {
105public: // types
106
107 enum class AtFlag : int {
108 EMPTY_PATH = AT_EMPTY_PATH,
109 SYMLINK_NOFOLLOW = AT_SYMLINK_NOFOLLOW,
110 SYMLINK_FOLLOW = AT_SYMLINK_FOLLOW,
111 REMOVEDIR = AT_REMOVEDIR,
112 EACCESS = AT_EACCESS,
113 NO_AUTOMOUNT = AT_NO_AUTOMOUNT,
114 };
115
116 using AtFlags = cosmos::BitMask<AtFlag>;
117
118public: // functions
119
120 AtFlagsValue() :
121 SystemCallItem{ItemType::PARAM_IN, "flags", "*at flags"} {
122 }
123
124 std::string str() const override;
125
126 AtFlags flags() const {
127 return m_flags;
128 }
129
130protected: // functions
131
132 void processValue(const Tracee&) override;
133
134protected: // data
135
136 AtFlags m_flags;
137};
138
140class CLUES_API AccessModeParameter :
142public:
143 explicit AccessModeParameter() :
144 item::ValueInParameter{"check", "access check"} {
145 }
146
147 std::string str() const override;
148
149 cosmos::fs::AccessChecks checks() const {
150 return m_checks;
151 }
152
153protected: // functions
154
155 void processValue(const Tracee&) override;
156
157protected: // data
158
159 cosmos::fs::AccessChecks m_checks;
160};
161
163class CLUES_API FileModeParameter :
165public:
166
167 FileModeParameter() :
168 item::ValueInParameter{"mode", "file-mode"} {
169 }
170
171 std::string str() const override;
172
173 auto mode() const {
174 return m_mode;
175 }
176
177protected: // functions
178
179 void processValue(const Tracee&) override {
180 m_mode = cosmos::FileMode{valueAs<cosmos::ModeT>()};
181 }
182
183protected: // data
184
185 cosmos::FileMode m_mode;
186};
187
189class CLUES_API StatParameter :
190 public PointerOutValue {
191public: // functions
192 explicit StatParameter() :
193 PointerOutValue{"stat", "struct stat"} {
194 }
195
196 std::string str() const override;
197
199
203 const std::optional<cosmos::FileStatus>& status() const {
204 return m_stat;
205 }
206
207protected: // functions
208
209 void processValue(const Tracee &) override {
210 m_stat.reset();
211 }
212
213 void updateData(const Tracee &proc) override;
214
216 bool isOldStat() const;
217
219 bool isStat64() const;
220
222 bool isRegularStat() const;
223
224protected: // data
225
226 std::optional<cosmos::FileStatus> m_stat;
227};
228
230class CLUES_API DirEntries :
231 public PointerOutValue {
232public: // types
233
234 struct Entry {
235 uint64_t inode;
236 /* for getdents() this is an unsigned type, for getdents64()
237 * it is a signed type. I guess using a signed type (off_t)
238 * makes sense. */
239 int64_t offset;
240 std::string_view name;
241 cosmos::DirEntry::Type type;
242 };
243
244public: // functions
245
246 explicit DirEntries() :
247 PointerOutValue{"dirent", "struct linux_dirent"}
248 {}
249
250 std::string str() const override;
251
252 const auto& entries() const {
253 return m_entries;
254 }
255
256protected: // functions
257
258 void processValue(const Tracee &) override {
259 m_entries.clear();
260 m_buffer.reset();
261 }
262
263 void updateData(const Tracee &proc) override;
264
266
270 template <typename DIRENT>
271 void parseEntries32(const size_t bytes);
272
274 void parseEntries64(const size_t bytes);
275
276protected: // data
277
278 std::vector<Entry> m_entries;
280 std::unique_ptr<char[]> m_buffer;
281};
282
283} // end ns
Base class for any kind of system call parameter or return value.
OTHER valueAs() const
Helper to cast the strongly typed Word m_val to other strong enum types.
SystemCallItem(const ItemType type, const std::string_view short_name={}, const std::string_view long_name={})
Constructs a new SystemCallItem.
Base class for traced processes.
Definition Tracee.hxx:39
std::string str() const override
Returns a human readable string representation of the item.
Definition fs.cxx:141
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition fs.cxx:137
Flags for system calls with at semantics like linkat(), faccessat().
Definition fs.hxx:104
@ EACCESS
only used with faccessat2
Definition fs.hxx:112
@ REMOVEDIR
only used with unlinkat
Definition fs.hxx:111
@ NO_AUTOMOUNT
only used with fstatat()
Definition fs.hxx:113
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition fs.hxx:258
std::unique_ptr< char[]> m_buffer
the raw buffer backing m_entries
Definition fs.hxx:280
FileDescriptor(const ItemType type=ItemType::PARAM_IN, const AtSemantics at_semantics=AtSemantics{false}, const std::string_view short_name="fd", const std::string_view long_name="file descriptor")
Definition fs.hxx:38
const std::optional< FDInfo > & info() const
Returns optional extended file descriptor metadata.
Definition fs.hxx:57
std::optional< FDInfo > m_info
filled if FormatFlag::FD_INFO is set.
Definition fs.hxx:69
std::string str() const override
Returns a human readable string representation of the item.
Definition fs.cxx:169
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition fs.hxx:179
std::string str() const override
Returns a human readable string representation of the item.
Definition fs.cxx:53
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition fs.cxx:106
std::string str() const override
Returns a human readable string representation of the item.
Definition fs.cxx:174
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition fs.hxx:209
const std::optional< cosmos::FileStatus > & status() const
Access to full FileStatus information.
Definition fs.hxx:203
Specialization of ValueParameter for PARAM_IN parameters.
Definition items.hxx:47
ItemType
Basic type of a SystemCallItem.
@ PARAM_IN
An input parameter to the system call.