libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
limits.cxx
1// C++
2#include <limits>
3#include <sstream>
4
5// clues
6#include <clues/format.hxx>
7#include <clues/items/limits.hxx>
8#include <clues/macros.h>
9#include <clues/sysnrs/generic.hxx>
10#include <clues/Tracee.hxx>
11// private
12#include <clues/private/kernel/other.hxx>
13
14namespace clues::item {
15
16std::string ResourceType::str() const {
17 switch (cosmos::to_integral(m_limit)) {
18 CASE_ENUM_TO_STR(RLIMIT_AS);
19 CASE_ENUM_TO_STR(RLIMIT_CORE);
20 CASE_ENUM_TO_STR(RLIMIT_CPU);
21 CASE_ENUM_TO_STR(RLIMIT_DATA);
22 CASE_ENUM_TO_STR(RLIMIT_FSIZE);
23 CASE_ENUM_TO_STR(RLIMIT_LOCKS);
24 CASE_ENUM_TO_STR(RLIMIT_MEMLOCK);
25 CASE_ENUM_TO_STR(RLIMIT_MSGQUEUE);
26 CASE_ENUM_TO_STR(RLIMIT_NICE);
27 CASE_ENUM_TO_STR(RLIMIT_NOFILE);
28 CASE_ENUM_TO_STR(RLIMIT_NPROC);
29 CASE_ENUM_TO_STR(RLIMIT_RSS);
30 CASE_ENUM_TO_STR(RLIMIT_RTPRIO);
31 CASE_ENUM_TO_STR(RLIMIT_RTTIME);
32 CASE_ENUM_TO_STR(RLIMIT_SIGPENDING);
33 CASE_ENUM_TO_STR(RLIMIT_STACK);
34 default: return "unknown";
35 }
36}
37
39 m_limit = cosmos::LimitType{valueAs<int>()};
40}
41
42std::string ResourceLimit::str() const {
43 if (!m_limit) {
44 return isZero() ? "NULL" : "<invalid>";
45 }
46 std::stringstream ss;
47
48 ss
49 << "{rlim_cur=" << format::limit(m_limit->rlim_cur) << ", rlim_max="
50 << format::limit(m_limit->rlim_max) << "}";
51
52 return ss.str();
53}
54
56 auto update_rlimit = [this, &proc]<typename RLIM_T>() {
57 RLIM_T rlim;
58
59 if (!proc.readStruct(asPtr(), rlim)) {
60 m_limit.reset();
61 return;
62 }
63
64 m_limit = cosmos::LimitSpec{};
65
66 /*
67 * RLIM_INFINITY is simply the maximum of unsigned long, thus
68 * check the ABI-specific maximum value here and translate it
69 * into the native RLIM_INFINITY;
70 */
71 constexpr auto RLIM_T_INFINITY = std::numeric_limits<decltype(rlim.rlim_cur)>::max();
72 if (rlim.rlim_cur == RLIM_T_INFINITY)
73 m_limit->rlim_cur = RLIM_INFINITY;
74 else
75 m_limit->rlim_cur = rlim.rlim_cur;
76
77 if (rlim.rlim_max == RLIM_T_INFINITY)
78 m_limit->rlim_max = RLIM_INFINITY;
79 else
80 m_limit->rlim_max = rlim.rlim_max;
81 };
82
83 if (isCompatSyscall()) {
84 update_rlimit.operator()<struct rlimit32>();
85 } else {
86 update_rlimit.operator()<struct rlimit64>();
87 }
88
89}
90
92 if (m_call->callNr() == SystemCallNr::PRLIMIT64)
93 // prlimit64() always uses 64-bit wide fields
94 return false;
95
96 const auto abi = m_call->abi();
97
98 return abi == ABI::I386;
99}
100
101} // end ns
const SystemCall * m_call
The system call context this item part of.
bool isZero() const
Returns whether the parameter is set to 0 / NULL.
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
std::string str() const override
Returns a human readable string representation of the item.
Definition limits.cxx:42
bool isCompatSyscall() const
Checks whether the current context involves a 32-bit struct rlimit.
Definition limits.cxx:91
void updateData(const Tracee &proc) override
Called upon exit of the system call to update possible out parameters.
Definition limits.cxx:55
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
Definition limits.cxx:38
std::string str() const override
Returns a human readable string representation of the item.
Definition limits.cxx:16
32-bit based struct rlimit used with getrlimit() and setrlimit().
Definition other.hxx:23
64-bit based struct rlimit used with prlimit().
Definition other.hxx:32