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

A range of directory entries from getdents(). More...

#include <fs.hxx>

+ Inheritance diagram for clues::item::DirEntries:

Classes

struct  Entry
 

Public Member Functions

std::string str () const override
 Returns a human readable string representation of the item.
 
const auto & entries () const
 
- 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.
 
template<typename DIRENT>
void parseEntries32 (const size_t bytes)
 Parses the dirent structures from m_buffer.
 
void parseEntries64 (const size_t bytes)
 Parses the dirent structures for getdents64().
 
- 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::vector< Entrym_entries
 
std::unique_ptr< char[]> m_buffer
 the raw buffer backing m_entries
 
- 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

A range of directory entries from getdents().

Definition at line 230 of file fs.hxx.

Constructor & Destructor Documentation

◆ DirEntries()

clues::item::DirEntries::DirEntries ( )
inlineexplicit

Definition at line 246 of file fs.hxx.

246 :
247 PointerOutValue{"dirent", "struct linux_dirent"}
248 {}

Member Function Documentation

◆ entries()

const auto & clues::item::DirEntries::entries ( ) const
inline

Definition at line 252 of file fs.hxx.

252 {
253 return m_entries;
254 }

◆ parseEntries32()

template<typename DIRENT>
void clues::item::DirEntries::parseEntries32 ( const size_t bytes)
protected

Parses the dirent structures from m_buffer.

to make up for differently sized structures when dealing with 32-bit emulation we need a template type here.

Definition at line 424 of file fs.cxx.

424 {
425
426 DIRENT *cur = nullptr;
427 size_t pos = 0;
428 constexpr auto NAME_OFFSET = offsetof(DIRENT, d_name);
429
430 while (pos < bytes) {
431 cur = (DIRENT*)(m_buffer.get() + pos);
432 auto namelen = cur->d_reclen - NAME_OFFSET - 2;
433 /*
434 * the length can still include padding, thus look from the
435 * back for the first non-zero byte at the end to correct the
436 * length.
437 */
438 for (char *last = cur->d_name + namelen - 1; last > cur->d_name && *last == '\0'; last--, namelen--);
439
440 const auto type = *(m_buffer.get() + pos + cur->d_reclen - 1);
441
442 m_entries.emplace_back(Entry{
443 cur->d_ino,
444 static_cast<int64_t>(cur->d_off),
445 // to avoid copying we only keep a string_view in
446 // Entry pointing to the raw buffer data.
447 std::string_view{cur->d_name, namelen},
448 cosmos::DirEntry::Type{static_cast<unsigned char>(type)}
449 });
450 pos += cur->d_reclen;
451 }
452}
std::unique_ptr< char[]> m_buffer
the raw buffer backing m_entries
Definition fs.hxx:280

◆ parseEntries64()

void clues::item::DirEntries::parseEntries64 ( const size_t bytes)
protected

Parses the dirent structures for getdents64().

Definition at line 454 of file fs.cxx.

454 {
455 struct linux_dirent64 *cur = nullptr;
456 size_t pos = 0;
457 constexpr auto NAME_OFFSET = offsetof(linux_dirent64, d_name);
458
459 while (pos < bytes) {
460 cur = (linux_dirent64*)(m_buffer.get() + pos);
461 auto namelen = cur->d_reclen - NAME_OFFSET;
462 /*
463 * remove padding from length
464 */
465 for (char *last = cur->d_name + namelen - 1; last > cur->d_name && *last == '\0'; last--, namelen--);
466
467 m_entries.emplace_back(Entry{
468 cur->d_ino,
469 cur->d_off,
470 std::string_view{cur->d_name, namelen},
471 cosmos::DirEntry::Type{static_cast<unsigned char>(cur->d_type)}
472 });
473
474 pos += cur->d_reclen;
475 }
476}

◆ processValue()

void clues::item::DirEntries::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 258 of file fs.hxx.

258 {
259 m_entries.clear();
260 m_buffer.reset();
261 }

◆ str()

std::string clues::item::DirEntries::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 363 of file fs.cxx.

363 {
364 std::stringstream ss;
365 auto result = m_call->result();
366
367 if (!result) {
368 ss << "undefined";
369 } else if (m_entries.empty()) {
370 ss << "empty";
371 } else {
372 ss << m_entries.size() << " entries: ";
373
374 std::string comma;
375
376 for (const auto &entry: m_entries) {
377 ss << comma << "{d_ino=" << entry.inode
378 << ", d_off=" << entry.offset
379 << ", d_reclen=" << offsetof(struct linux_dirent, d_name) + entry.name.length() + 2
380 << ", d_name=\"" << entry.name
381 << "\", d_type=" << entry_type_label(entry.type)
382 << "}";
383
384 if (comma.empty()) {
385 comma = ", ";
386 }
387 }
388 }
389
390 return ss.str();
391}
const SystemCall * m_call
The system call context this item part of.

◆ updateData()

void clues::item::DirEntries::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 393 of file fs.cxx.

393 {
394 // the amount of data stored at the DirEntries location depends on the
395 // system call result value.
396 auto result = m_call->result();
397 if (!result)
398 // error occurred
399 return;
400
401 const auto bytes = result->valueAs<size_t>();
402 if (bytes == 0)
403 // empty
404 return;
405
406 /*
407 * first copy over all the necessary data from the tracee
408 */
409 m_buffer = std::unique_ptr<char[]>(new char[bytes]);
410 proc.readBlob(asPtr(), m_buffer.get(), bytes);
411
412 if (m_call->callNr() == SystemCallNr::GETDENTS64) {
413 parseEntries64(bytes);
414 } else {
415 if (m_call->is32BitEmulationABI()) {
417 } else {
419 }
420 }
421}
void parseEntries32(const size_t bytes)
Parses the dirent structures from m_buffer.
Definition fs.cxx:424
void parseEntries64(const size_t bytes)
Parses the dirent structures for getdents64().
Definition fs.cxx:454

Member Data Documentation

◆ m_buffer

std::unique_ptr<char[]> clues::item::DirEntries::m_buffer
protected

the raw buffer backing m_entries

Definition at line 280 of file fs.hxx.

◆ m_entries

std::vector<Entry> clues::item::DirEntries::m_entries
protected

Definition at line 278 of file fs.hxx.


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