libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
futex.cxx
1// cosmos
2#include <cosmos/formatting.hxx>
3
4// clues
5#include <clues/items/futex.hxx>
6#include <clues/macros.h>
7#include <clues/private/utils.hxx>
8
9namespace clues::item {
10
11std::string FutexOperation::str() const {
12 /*
13 * there are a number of undocumented constants and some flags can be
14 * or'd in like FUTEX_PRIVATE_FLAG. Without exactly understanding that
15 * we can't sensibly trace this ...
16 * it seems the man page doesn't tell the complete story, strace
17 * understands all the "private" stuff that can also be found in the
18 * header.
19 */
20 auto format_op = [](int val) -> std::string {
21 switch (val & FUTEX_CMD_MASK) {
22 CASE_ENUM_TO_STR(FUTEX_CMP_REQUEUE);
23 CASE_ENUM_TO_STR(FUTEX_CMP_REQUEUE_PI);
24 CASE_ENUM_TO_STR(FUTEX_FD);
25 CASE_ENUM_TO_STR(FUTEX_LOCK_PI);
26 CASE_ENUM_TO_STR(FUTEX_LOCK_PI2);
27 CASE_ENUM_TO_STR(FUTEX_REQUEUE);
28 CASE_ENUM_TO_STR(FUTEX_TRYLOCK_PI);
29 CASE_ENUM_TO_STR(FUTEX_UNLOCK_PI);
30 CASE_ENUM_TO_STR(FUTEX_WAIT);
31 CASE_ENUM_TO_STR(FUTEX_WAIT_BITSET);
32 CASE_ENUM_TO_STR(FUTEX_WAIT_REQUEUE_PI);
33 CASE_ENUM_TO_STR(FUTEX_WAKE);
34 CASE_ENUM_TO_STR(FUTEX_WAKE_BITSET);
35 CASE_ENUM_TO_STR(FUTEX_WAKE_OP);
36 default: return cosmos::sprintf("unknown (%d)", val);
37 }
38 };
39
40 const auto val = valueAs<int>();
41 BITFLAGS_FORMAT_START_COMBINED(m_flags, val);
42
43 BITFLAGS_STREAM() << format_op(val);
44
45 if (!m_flags.none()) {
46 BITFLAGS_STREAM() << '|';
47 BITFLAGS_ADD(FUTEX_PRIVATE_FLAG);
48 BITFLAGS_ADD(FUTEX_CLOCK_REALTIME);
49 }
50
51 return BITFLAGS_STR();
52}
53
55 m_cmd = Command{valueAs<int>() & FUTEX_CMD_MASK};
56 m_flags = Flags{valueAs<int>() xor cosmos::to_integral(m_cmd)};
57}
58
59namespace {
60
61std::string get_label(const FutexWakeOperation::Operation op) {
62 switch (cosmos::to_integral(op)) {
63 CASE_ENUM_TO_STR(FUTEX_OP_SET);
64 CASE_ENUM_TO_STR(FUTEX_OP_ADD);
65 CASE_ENUM_TO_STR(FUTEX_OP_OR);
66 CASE_ENUM_TO_STR(FUTEX_OP_ANDN);
67 CASE_ENUM_TO_STR(FUTEX_OP_XOR);
68 default: return cosmos::sprintf("unknown (%d)", cosmos::to_integral(op));
69 }
70}
71
72std::string get_label(const FutexWakeOperation::Comparator cmp) {
73 switch (cosmos::to_integral(cmp)) {
74 CASE_ENUM_TO_STR(FUTEX_OP_CMP_EQ);
75 CASE_ENUM_TO_STR(FUTEX_OP_CMP_NE);
76 CASE_ENUM_TO_STR(FUTEX_OP_CMP_LT);
77 CASE_ENUM_TO_STR(FUTEX_OP_CMP_LE);
78 CASE_ENUM_TO_STR(FUTEX_OP_CMP_GT);
79 CASE_ENUM_TO_STR(FUTEX_OP_CMP_GE);
80 default: return cosmos::sprintf("unknown (%d)", cosmos::to_integral(cmp));
81 }
82}
83
84}
85
87 const auto raw = valueAs<uint32_t>();
88 /*
89 * the integer is structured like this:
90 *
91 * <4 bits Operation><4 bits Comparator><12 bits oparg><12 bits cmparg>
92 *
93 * where the upper bit of the operation is the optional "shift arg"
94 * flag.
95 */
96
97 const auto op_bits = raw >> 28;
98 /*
99 * NOTE: there's a bug in man futex(2), which names FUTEX_OP_ARG_SHIFT
100 * here instead.
101 */
102 m_op = Operation{op_bits & (~FUTEX_OP_OPARG_SHIFT)};
103 m_shift_arg = op_bits & FUTEX_OP_OPARG_SHIFT;
104 m_comp = Comparator{(raw >> 24) & 0xF};
105 m_oparg = (raw >> 12) & 0xFFF;
106 m_cmparg = raw & 0xFFF;
107
108}
109
110std::string FutexWakeOperation::str() const {
111 return cosmos::sprintf("{op=%s%s, cmp=%s, oparg=%u, cmparg=%u}",
112 get_label(m_op).c_str(),
113 m_shift_arg ? "|FUTEX_OP_OPARG_SHIFT" : "",
114 get_label(m_comp).c_str(),
115 m_oparg,
116 m_cmparg);
117}
118
119} // end ns
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
std::string str() const override
Returns a human readable string representation of the item.
Definition futex.cxx:11
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition futex.cxx:54
Operation
Atomic operation to be carried out on futex value.
Definition futex.hxx:94
uint32_t m_oparg
! The operation argument used in uaddr2 OP oparg.
Definition futex.hxx:148
std::string str() const override
Returns a human readable string representation of the item.
Definition futex.cxx:110
uint32_t m_cmparg
! The comparison argument used in oldval CMP cmparg.
Definition futex.hxx:150
void processValue(const Tracee &) override
Processes the value stored in m_val acc. to the actual item type.
Definition futex.cxx:86
bool m_shift_arg
! Whether oparg is additionally replaced by 1 << oparg.
Definition futex.hxx:146