libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
strings.cxx
1// cosmos
2#include <cosmos/formatting.hxx>
3
4// clues
5#include <clues/items/strings.hxx>
6#include <clues/Tracee.hxx>
7
8namespace clues::item {
9
10std::string StringData::str() const {
11 if (!m_str) {
12 return "<invalid>";
13 } else if (m_str->empty()) {
14 return "\"\"";
15 } else {
16 return cosmos::sprintf("\"%s\"", m_str->c_str());
17 }
18}
19
20void StringData::fetch(const Tracee &proc) {
21 m_str.emplace(std::string{});
22 // fetch the string from the Tracee's address space.
23 try {
24 proc.readString(asPtr(), *m_str);
25 } catch (...) {
26 m_str.reset();
27 }
28}
29
31 if (m_call->is32BitEmulationABI()) {
32 /* in this case we have to be careful, the Tracee uses 4-byte
33 * pointers, while we are using 8-byte pointers */
34 fetchPointers<uint32_t>(proc);
35 return;
36 }
37
38 fetchPointers<uintptr_t>(proc);
39}
40
41template <typename PTR>
42void StringArrayData::fetchPointers(const Tracee &proc) {
43 const auto array_start = asPtr();
44 std::vector<PTR> string_addrs;
45 m_strs.clear();
46
47 if (array_start == ForeignPtr::NO_POINTER)
48 return;
49
50 // first read in all start adresses of the c-strings for the string array
51 proc.readVector(array_start, string_addrs);
52
53 for (const auto &addr: string_addrs) {
54 m_strs.push_back(std::string{});
55 proc.readString(ForeignPtr{addr}, m_strs.back());
56 }
57}
58
59std::string StringArrayData::str() const {
60
61 if (m_strs.empty()) {
62 if (isZero()) {
63 return "NULL";
64 } else {
65 // pointer to no arguments
66 return "[]";
67 }
68 }
69
70 std::string ret;
71 ret += "[";
72
73 for (const auto &str: m_strs) {
74 ret += "\"";
75 ret += str;
76 ret += "\", ";
77 }
78
79 if (!m_strs.empty())
80 ret.erase(ret.size() - 2);
81
82 ret += "]";
83
84 return ret;
85}
86
87} // 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.
Base class for traced processes.
Definition Tracee.hxx:39
void readVector(const ForeignPtr pointer, VECTOR &out) const
Reads in a zero terminated array of data items into the STL-vector like parameter out.
Definition Tracee.cxx:940
void readString(const ForeignPtr addr, std::string &out) const
Reads a zero-terminated C-string from the tracee.
Definition Tracee.cxx:935
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
Definition strings.cxx:30
std::string str() const override
Returns a human readable string representation of the item.
Definition strings.cxx:59
std::string str() const override
Returns a human readable string representation of the item.
Definition strings.cxx:10
ForeignPtr
Strongly typed opaque pointer to tracee memory.
Definition types.hxx:140