libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
items.cxx
1// C++
2#include <algorithm>
3#include <sstream>
4#include <type_traits>
5
6// cosmos
7#include <cosmos/formatting.hxx>
8#include <cosmos/io/ILogger.hxx>
9
10// clues
11#include <clues/format.hxx>
12#include <clues/items/items.hxx>
13#include <clues/logger.hxx>
14#include <clues/Tracee.hxx>
15
16using namespace std::string_literals;
17
18namespace clues::item {
19
20namespace {
21
22template <typename INT>
23std::string format_number(INT value, const Base base) {
24 switch (base) {
25 default: [[fallthrough]];
26 case Base::DEC: return std::to_string(value);
27 case Base::OCT: return static_cast<std::string>(cosmos::OctNum<INT>{value, 0});
28 case Base::HEX: return static_cast<std::string>(cosmos::HexNum<INT>{value, 0});
29 }
30}
31
32} // end ns
33
34std::string GenericPointerValue::str() const {
35 std::stringstream ss;
36 if (auto ptr = valueAs<long*>(); ptr) {
37 ss << ptr;
38 } else {
39 ss << "NULL";
40 }
41 return ss.str();
42}
43
44template <typename INT>
45std::string IntValueT<INT>::str() const {
46 return format_number(m_value, m_base);
47}
48
49template <typename INT>
50void PointerToScalar<INT>::fetchValue(const Tracee &tracee) {
51 INT val;
52
53 m_val.reset();
54
55 if (m_ptr == ForeignPtr::NO_POINTER)
56 return;
57
58 try {
59 if (tracee.readStruct(m_ptr, val)) {
60 m_val = val;
61 }
62 } catch (const std::exception &) {
63 // probably a bad address
64 }
65}
66
67template <typename INT>
68std::string PointerToScalar<INT>::str() const {
69 if (m_ptr == ForeignPtr::NO_POINTER) {
70 return "NULL";
71 }
72
73 return format::pointer(m_ptr, m_val ? scalarToString() : "???"s);
74}
75
76template <typename INT>
77std::string PointerToScalar<INT>::scalarToString() const {
78 if constexpr (std::is_enum_v<INT>) {
79 return format_number(cosmos::to_integral(*m_val), m_base);
80 }
81 if constexpr (!std::is_enum_v<INT>) {
82 if constexpr (std::is_pointer_v<INT>) {
83 return format_number(reinterpret_cast<uintptr_t>(*m_val), clues::Base::HEX);
84 }
85 if constexpr (!std::is_pointer_v<INT>) {
86 return format_number(*m_val, m_base);
87 }
88 }
89}
90
92 if (isOut()) {
93 // this is an out buffer only, will be filled in updateData()
94 m_data.clear();
95 return;
96 }
97
98 fillBuffer(tracee);
99}
100
102 if (isIn()) {
103 // nothing to update on system call return
104 return;
105 }
106
107 fillBuffer(tracee);
108}
109
110void BufferPointer::fillBuffer(const Tracee &tracee) {
111 const auto to_fetch = std::min(tracee.maxBufferPrefetch(), availableBytes());
112 m_data.resize(to_fetch);
113
114 try {
115 tracee.readBlob(asPtr(), reinterpret_cast<char*>(m_data.data()), to_fetch);
116 } catch (const cosmos::CosmosError &e) {
117 LOG_ERROR("Failed to fetch buffer data from Tracee: " << e.what());
118 m_data.clear();
119 }
120}
121
123 return m_size_par.valueAs<size_t>();
124}
125
126std::string BufferPointer::str() const {
127 const auto is_cut_off = availableBytes() != m_data.size();
128 auto ret = format::buffer(m_data.data(), m_data.size());
129
130 if (is_cut_off) {
131 ret += "...";
132 }
133
134 return ret;
135}
136
137SystemCallItem unused = SystemCallItem{ItemType::PARAM_IN, "unused", "unused parameter"};
138
139/*
140 * explicit template instantiations
141 */
142
143template class CLUES_API PointerToScalar<unsigned long>;
144template class CLUES_API PointerToScalar<unsigned int>;
145template class CLUES_API PointerToScalar<int>;
146template class CLUES_API PointerToScalar<cosmos::ProcessID>;
147template class CLUES_API PointerToScalar<cosmos::FileNum>;
148template class CLUES_API PointerToScalar<void*>;
149template class CLUES_API PointerToScalar<ForeignPtr>;
150template class CLUES_API IntValueT<int>;
151template class CLUES_API IntValueT<uint32_t>;
152template class CLUES_API IntValueT<unsigned long>;
153template class CLUES_API IntValueT<off_t>;
154
155} // 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.
Base class for traced processes.
Definition Tracee.hxx:39
bool readStruct(const ForeignPtr addr, T &out) const
Reads a system call struct from the tracee's address space into out.
Definition Tracee.hxx:221
void readBlob(const ForeignPtr addr, char *buffer, const size_t bytes) const
Reads an arbitrary binary blob of fixed length from the tracee.
Definition Tracee.cxx:981
std::string str() const override
Returns a human readable string representation of the item.
Definition items.cxx:126
size_t availableBytes() const
Returns the actual number of input bytes available in the Tracee.
Definition items.cxx:122
void updateData(const Tracee &) override
Called upon exit of the system call to update possible out parameters.
Definition items.cxx:101
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition items.cxx:91
std::string str() const override
Returns a human readable string representation of the item.
Definition items.cxx:34
std::string str() const override
Returns a human readable string representation of the item.
Definition items.cxx:45
std::string str() const override
Returns a human readable string representation of the item.
Definition items.cxx:68
@ PARAM_IN
An input parameter to the system call.
Base
Integer number display base for formatting purposes.
Definition types.hxx:129