libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
clues::item::StatParameter Class Reference

The stat structure used in stat() & friends. More...

#include <fs.hxx>

+ Inheritance diagram for clues::item::StatParameter:

Public Member Functions

std::string str () const override
 Returns a human readable string representation of the item.
 
const std::optional< cosmos::FileStatus > & status () const
 Access to full FileStatus information.
 
- Public Member Functions inherited from clues::item::PointerOutValue
 PointerOutValue (const std::string_view short_name, const std::string_view long_name={}, const ItemType type=ItemType::PARAM_OUT)
 
- Public Member Functions inherited from clues::item::PointerValue
 PointerValue (const ItemType type, const std::string_view short_name, const std::string_view long_name)
 
- Public Member Functions inherited from clues::SystemCallItem
 SystemCallItem (const ItemType type, const std::string_view short_name={}, const std::string_view long_name={})
 Constructs a new SystemCallItem.
 
auto type () const
 
bool isIn () const
 
bool isOut () const
 
bool isInOut () const
 
bool isReturnValue () const
 
void fill (const Tracee &proc, const Word word)
 Fills the item from the given register data.
 
bool needsUpdate () const
 Returns whether the item needs to be updated after the system call is finished.
 
std::string_view shortName () const
 Returns the friendly short name for this item.
 
std::string_view longName () const
 Returns the friendly long name for this item, if available, else the short name.
 
auto hasLongName () const
 
bool isZero () const
 Returns whether the parameter is set to 0 / NULL.
 
Word value () const
 Returns the currently stored raw value of the item.
 
template<typename OTHER>
OTHER valueAs () const
 Helper to cast the strongly typed Word m_val to other strong enum types.
 
ForeignPtr asPtr () const
 
Flags flags () const
 
bool deferFill () const
 

Protected Member Functions

void processValue (const Tracee &) override
 Processes the value stored in m_val acc. to the actual item type.
 
void updateData (const Tracee &proc) override
 Called upon exit of the system call to update possible out parameters.
 
bool isOldStat () const
 Whether this is related to one of the OLDSTAT family of system calls.
 
bool isStat64 () const
 Whether this is related to one of the STAT64 family of system calls.
 
bool isRegularStat () const
 Whether this is related to one of the STAT family of system calls.
 
- Protected Member Functions inherited from clues::SystemCallItem
void setSystemCall (const SystemCall &sc)
 Sets the system call context this item is a part of.
 

Protected Attributes

std::optional< cosmos::FileStatus > m_stat
 
- Protected Attributes inherited from clues::SystemCallItem
const SystemCallm_call = nullptr
 The system call context this item part of.
 
const ItemType m_type
 The type of item.
 
std::string_view m_short_name
 A human readable short name for the item, should be one word only.
 
std::string_view m_long_name
 A human readable longer name for the item.
 
Word m_val
 The raw register value for the item.
 
Flags m_flags
 Flags influencing the processing of the item.
 

Additional Inherited Members

- Public Types inherited from clues::SystemCallItem
enum class  Flag { DEFER_FILL = 1 << 0 }
 
using Flags = cosmos::BitMask<Flag>
 

Detailed Description

The stat structure used in stat() & friends.

Definition at line 189 of file fs.hxx.

Constructor & Destructor Documentation

◆ StatParameter()

clues::item::StatParameter::StatParameter ( )
inlineexplicit

Definition at line 192 of file fs.hxx.

192 :
193 PointerOutValue{"stat", "struct stat"} {
194 }

Member Function Documentation

◆ isOldStat()

bool clues::item::StatParameter::isOldStat ( ) const
protected

Whether this is related to one of the OLDSTAT family of system calls.

Definition at line 217 of file fs.cxx.

217 {
218 using enum SystemCallNr;
219
220 switch (m_call->callNr()) {
221 case OLDSTAT: [[fallthrough]];
222 case OLDLSTAT: [[fallthrough]];
223 case OLDFSTAT: return true;
224 default: return false;
225 }
226}
const SystemCall * m_call
The system call context this item part of.
SystemCallNr
Abstract system call number usable across architectures and ABIs.
Definition generic.hxx:29

◆ isRegularStat()

bool clues::item::StatParameter::isRegularStat ( ) const
protected

Whether this is related to one of the STAT family of system calls.

Definition at line 240 of file fs.cxx.

240 {
241 using enum SystemCallNr;
242
243 switch (m_call->callNr()) {
244 case SystemCallNr::STAT: [[fallthrough]];
245 case SystemCallNr::LSTAT: [[fallthrough]];
246 case SystemCallNr::FSTAT: return true;
247 default: return false;
248 }
249}

◆ isStat64()

bool clues::item::StatParameter::isStat64 ( ) const
protected

Whether this is related to one of the STAT64 family of system calls.

Definition at line 228 of file fs.cxx.

228 {
229 using enum SystemCallNr;
230
231 switch (m_call->callNr()) {
232 case STAT64: [[fallthrough]];
233 case LSTAT64: [[fallthrough]];
234 case FSTAT64: [[fallthrough]];
235 case FSTATAT64: return true;
236 default: return false;
237 }
238}

◆ processValue()

void clues::item::StatParameter::processValue ( const Tracee & )
inlineoverrideprotectedvirtual

Processes the value stored in m_val acc. to the actual item type.

This function is called for all parameter types upon entry to a system call, and for ItemType::RETVAL upon exit from a system call.

For parameters of ItemType::PARAM_OUT this callback can be used to reset any stored data to be filled in later when updateData() is called.

Reimplemented from clues::SystemCallItem.

Definition at line 209 of file fs.hxx.

209 {
210 m_stat.reset();
211 }

◆ status()

const std::optional< cosmos::FileStatus > & clues::item::StatParameter::status ( ) const
inline

Access to full FileStatus information.

This optional can be unset if the system call never occurred or if reading the data from the Tracee failed.

Definition at line 203 of file fs.hxx.

203 {
204 return m_stat;
205 }

◆ str()

std::string clues::item::StatParameter::str ( ) const
overridevirtual

Returns a human readable string representation of the item.

This member function should be specialized in derived classes to output the item's data in a fashion suitable for the concrete item type.

Reimplemented from clues::SystemCallItem.

Definition at line 174 of file fs.cxx.

174 {
175 if (!m_stat) {
176 if (isZero()) {
177 return "NULL";
178 } else {
179 return "<invalid>";
180 }
181 }
182
183 std::stringstream ss;
184
185 const auto st = *m_stat->raw();
186 using namespace std::string_literals;
187
188 const auto is_old_stat = isOldStat();
189
190 ss
191 << "{"
192 << "ino=" << st.st_ino << ", "
193 << "dev=" << format::device_id(m_stat->device()) << ", "
194 << "mode=" << format::file_type(m_stat->type()) << "|" << format::file_mode_numeric(m_stat->mode().mask()) << ", "
195 << "nlink=" << m_stat->numLinks() << ", "
196 << "uid=" << st.st_uid << ", "
197 << "gid=" << st.st_gid << ", "
198 << (m_stat->type().isCharDev() || m_stat->type().isBlockDev() ? "rdev="s + format::device_id(m_stat->representedDevice()) + ", " : "")
199 << "size=" << st.st_size << ", ";
200
201 if (!is_old_stat) {
202 /*
203 * these fields do not exist in oldstat syscalls
204 */
205 ss << "blksize=" << st.st_blksize << ", " << "blocks=" << st.st_blocks << ", ";
206 }
207
208 ss
209 << "atim=" << format::timespec(m_stat->accessTime(), is_old_stat) << ", "
210 << "mtim=" << format::timespec(m_stat->modTime(), is_old_stat) << ", "
211 << "ctim=" << format::timespec(m_stat->statusTime(), is_old_stat)
212 << "}";
213
214 return ss.str();
215}
bool isZero() const
Returns whether the parameter is set to 0 / NULL.
bool isOldStat() const
Whether this is related to one of the OLDSTAT family of system calls.
Definition fs.cxx:217

◆ updateData()

void clues::item::StatParameter::updateData ( const Tracee & t)
overrideprotectedvirtual

Called upon exit of the system call to update possible out parameters.

This function is called for parameters of ItemType::PARAM_OUT and ItemType::PARAM_IN_OUT upon system call exit to update the data from the values returned from the system call.

The default implementation calls processValue() to allow to share the same data processing code for input and output for item types that support both.

This function is called regardless of system call success or error, so it can happen that there is no valid data returned by the kernel or pointers in userspace are broken. Implementations should take this into consideration when operating on the data.

Reimplemented from clues::SystemCallItem.

Definition at line 251 of file fs.cxx.

251 {
252 if (!m_stat) {
253 m_stat = std::make_optional<cosmos::FileStatus>();
254 }
255
256#if defined(COSMOS_X86_64) || defined(COSMOS_AARCH64)
257 /*
258 * on 64-bit platforms life is simple, we directly copy the data into
259 * a cosmos::FileStatus struct.
260 * make sure the raw kernel data structure `stat64` matches `struct
261 * stat` and then copy everything into it.
262 */
263 static_assert(sizeof(*m_stat->raw()) == sizeof(stat64));
264#else
265 /*
266 * on modern 32-bit we can still do the same for the stat64 family of
267 * system calls, but only if the tracer is 32-bit as well...
268 */
269 static_assert(sizeof(*m_stat->raw()) == sizeof(stat32_64));
270#endif
271
272 auto fetch_and_copy = [this, &proc]<typename STAT>() {
273 STAT st;
274 if (!proc.readStruct(asPtr(), st)) {
275 m_stat.reset();
276 return;
277 }
278
279 auto &raw = *m_stat->raw();
280 /*
281 * to be layout agnostic simply copy over field-by-field
282 */
283 raw.st_dev = st.dev;
284 raw.st_ino = st.ino;
285 raw.st_mode = st.mode;
286 raw.st_nlink = st.nlink;
287 raw.st_uid = st.uid;
288 raw.st_gid = st.gid;
289 raw.st_rdev = st.rdev;
290 raw.st_size = st.size;
291
292 if constexpr (std::is_same_v<STAT, old_kernel_stat>) {
293 /* the old kernel stat is missing a lot of stuff */
294 raw.st_atim.tv_sec = st.atime;
295 raw.st_atim.tv_nsec = 0;
296 raw.st_mtim.tv_sec = st.mtime;
297 raw.st_mtim.tv_nsec = 0;
298 raw.st_ctim.tv_sec = st.ctime;
299 raw.st_ctim.tv_nsec = 0;
300 raw.st_blksize = 0;
301 raw.st_blocks = 0;
302 }
303
304 if constexpr (!std::is_same_v<STAT, old_kernel_stat>) {
305 raw.st_blksize = st.blksize;
306 raw.st_blocks = st.blocks;
307 raw.st_atim.tv_sec = st.atime;
308 raw.st_atim.tv_nsec = st.atime_nsec;
309 raw.st_mtim.tv_sec = st.mtime;
310 raw.st_mtim.tv_nsec = st.mtime_nsec;
311 raw.st_ctim.tv_sec = st.ctime;
312 raw.st_ctim.tv_nsec = st.ctime_nsec;
313 }
314 };
315
316 switch (m_call->abi()) {
317 case ABI::X86_64: [[fallthrough]];
318 case ABI::X32: [[fallthrough]];
319 case ABI::AARCH64: {
320 /*
321 * there's only one type of stat
322 * directly read into libcosmos's FileStatus
323 */
324 if (!proc.readStruct(asPtr(), *m_stat->raw())) {
325 m_stat.reset();
326 }
327 break;
328 } case ABI::I386: {
329 /*
330 * on 32-bit platforms things get messy, we have three
331 * different possibilities using three different data
332 * structures.
333 */
334 if (isOldStat()) {
335 fetch_and_copy.operator()<old_kernel_stat>();
336 } else if (isStat64()) {
337 fetch_and_copy.operator()<stat32_64>();
338 } else if (isRegularStat()) {
339 // regular 32-bit stat
340 fetch_and_copy.operator()<stat32>();
341 } else {
342 throw cosmos::RuntimeError{"unexpected syscall nr for struct stat"};
343 }
344 break;
345 } default: throw cosmos::RuntimeError{"unexpected ABI for struct stat"};
346 }
347}
bool isRegularStat() const
Whether this is related to one of the STAT family of system calls.
Definition fs.cxx:240
bool isStat64() const
Whether this is related to one of the STAT64 family of system calls.
Definition fs.cxx:228
@ X32
X86_64 with 32-bit pointers.
Definition types.hxx:66

Member Data Documentation

◆ m_stat

std::optional<cosmos::FileStatus> clues::item::StatParameter::m_stat
protected

Definition at line 226 of file fs.hxx.


The documentation for this class was generated from the following files: