KittyMemoryEx 1.0.0
Dedicated library for runtime code patching, memory scanning, tracing, injection and some more useful memory utilities. works for both Android and Linux
Loading...
Searching...
No Matches
KittyMemoryEx.hpp
1#pragma once
2
3#include "KittyUtils.hpp"
4#include <unordered_map>
5
9namespace KittyMemoryEx
10{
14 class ProcMap
15 {
16 public:
17 pid_t pid;
18 uintptr_t startAddress;
19 uintptr_t endAddress;
20 size_t length;
21 int protection;
22 bool readable, writeable, executable;
23 bool is_private, is_shared;
24 bool is_ro, is_rw, is_rx;
25 uintptr_t offset;
26 std::string dev;
27 uint64_t inode;
28 std::string pathname;
29
30 ProcMap()
31 : pid(0), startAddress(0), endAddress(0), length(0), protection(0), readable(false), writeable(false),
32 executable(false), is_private(false), is_shared(false), is_ro(false), is_rw(false), is_rx(false),
33 offset(0), inode(0)
34 {
35 }
36
37 inline bool operator==(const ProcMap &other) const
38 {
39 return (pid == other.pid && startAddress == other.startAddress && endAddress == other.endAddress &&
40 protection == other.protection && is_private == other.is_private && is_shared == other.is_shared &&
41 offset == other.offset && dev == other.dev && inode == other.inode && pathname == other.pathname);
42 }
43
44 inline bool operator!=(const ProcMap &other) const
45 {
46 return (pid != other.pid || startAddress != other.startAddress || endAddress != other.endAddress ||
47 protection != other.protection || is_private != other.is_private || is_shared != other.is_shared ||
48 offset != other.offset || dev != other.dev || inode != other.inode || pathname != other.pathname);
49 }
50
54 inline bool isValid() const
55 {
56 return (startAddress && endAddress && length);
57 }
58
62 inline bool isUnknown() const
63 {
64 return pathname.empty();
65 }
66
73 inline bool contains(uintptr_t address) const
74 {
75 return address >= startAddress && address < endAddress;
76 }
77
81 inline std::string toString() const
82 {
83 return KittyUtils::String::fmt("%" PRIxPTR "-%" PRIxPTR " %c%c%c%c %" PRIxPTR " %s %" PRIu64 " %s",
84 startAddress,
85 endAddress,
86 readable ? 'r' : '-',
87 writeable ? 'w' : '-',
88 executable ? 'x' : '-',
89 is_private ? 'p' : 's',
90 offset,
91 dev.c_str(),
92 inode,
93 pathname.c_str());
94 }
95 };
96
106 std::string getProcessName(pid_t pid);
107
117 std::vector<pid_t> getProcessIDs(const std::string &processName);
118
128 pid_t getProcessID(const std::string &processName);
129
139 std::vector<pid_t> getAllThreads(pid_t pid);
140
144 class ProcStatus
145 {
146 pid_t _pid, _tid;
147 std::unordered_map<std::string, std::string> data;
148 static bool parse(const std::string &path, ProcStatus *out);
149
150 public:
151 ProcStatus() : _pid(-1), _tid(-1)
152 {
153 }
154 ~ProcStatus()
155 {
156 data.clear();
157 }
158
166 inline static bool parse(pid_t pid, ProcStatus *out)
167 {
168 if (pid <= 0 || !out)
169 return false;
170
171 out->_pid = pid;
172 return parse("/proc/" + std::to_string(pid) + "/status", out);
173 }
174
183 inline static bool parse(pid_t pid, pid_t tid, ProcStatus *out)
184 {
185 if (pid <= 0 || tid <= 0 || !out)
186 return false;
187
188 out->_pid = pid;
189 out->_tid = tid;
190 return parse("/proc/" + std::to_string(pid) + "/task/" + std::to_string(tid) + "/status", out);
191 }
192
201 inline bool refresh()
202 {
203 return _tid <= 0 ? parse(_pid, this) : parse(_pid, _tid, this);
204 }
205
212 inline bool contains(const std::string &key) const
213 {
214 return data.find(key) != data.end();
215 }
216
223 inline std::string getString(const std::string &key) const
224 {
225 auto it = data.find(key);
226 return (it != data.end()) ? it->second : "";
227 }
228
235 inline long long getInt(const std::string &key) const
236 {
237 auto it = data.find(key);
238 if (it == data.end())
239 return 0;
240
241 return std::strtoll(it->second.c_str(), nullptr, 10);
242 }
243
250 inline bool getBool(const std::string &key) const
251 {
252 return getInt(key) == 1;
253 }
254 };
255
259 enum class EProcMapFilter
260 {
261 Equal,
262 Contains,
263 StartWith,
264 EndWith,
265 Regex
266 };
267
274 std::vector<ProcMap> getAllMaps(pid_t pid);
275
285 std::vector<ProcMap> getMaps(pid_t pid,
286 EProcMapFilter filter,
287 const std::string &name,
288 const std::vector<ProcMap> &maps = std::vector<ProcMap>());
289
298 ProcMap getAddressMap(pid_t pid, uintptr_t address, const std::vector<ProcMap> &maps = std::vector<ProcMap>());
299
315 std::vector<std::vector<ProcMap>> getFileMappings(pid_t pid, const std::string &path,
316 const std::vector<ProcMap> &maps = std::vector<ProcMap>());
317
318#ifdef __ANDROID__
325 std::string getAppDirectory(const std::string &pkg);
326#endif
327} // namespace KittyMemoryEx
Represents a mapping of a memory region.
Definition KittyMemoryEx.hpp:15
bool isUnknown() const
Checks if the map is unknown (i.e., no pathname).
Definition KittyMemoryEx.hpp:62
bool contains(uintptr_t address) const
Checks if the map contains a specific address.
Definition KittyMemoryEx.hpp:73
std::string toString() const
Converts the map to a string representation..
Definition KittyMemoryEx.hpp:81
bool isValid() const
Checks if the map is valid.
Definition KittyMemoryEx.hpp:54
std::string getString(const std::string &key) const
Retrieves the string value associated with a key.
Definition KittyMemoryEx.hpp:223
bool getBool(const std::string &key) const
Retrieves the boolean value associated with a key.
Definition KittyMemoryEx.hpp:250
static bool parse(pid_t pid, pid_t tid, ProcStatus *out)
Parses the status of a process given its PID and thread ID.
Definition KittyMemoryEx.hpp:183
bool contains(const std::string &key) const
Checks if the process status data contains a specific key.
Definition KittyMemoryEx.hpp:212
bool refresh()
Refreshes the process status.
Definition KittyMemoryEx.hpp:201
long long getInt(const std::string &key) const
Retrieves the integer value associated with a key.
Definition KittyMemoryEx.hpp:235
static bool parse(pid_t pid, ProcStatus *out)
Parses the status of a process given its PID.
Definition KittyMemoryEx.hpp:166
Provides utility functions for process memory.
Definition KittyMemoryEx.hpp:10
std::vector< pid_t > getAllThreads(pid_t pid)
Retrieves a list of thread IDs for a given process ID.
std::vector< ProcMap > getMaps(pid_t pid, EProcMapFilter filter, const std::string &name, const std::vector< ProcMap > &maps=std::vector< ProcMap >())
Retrieves information about all memory maps in a remote process.
pid_t getProcessID(const std::string &processName)
Retrieves the process ID that matches a given process name.
std::vector< pid_t > getProcessIDs(const std::string &processName)
Retrieves a list of process IDs that match a given process name.
std::vector< std::vector< ProcMap > > getFileMappings(pid_t pid, const std::string &path, const std::vector< ProcMap > &maps=std::vector< ProcMap >())
Returns all contiguous mappings of a file in a remote process.
EProcMapFilter
Enumerates the filter types for finding memory maps.
Definition KittyMemoryEx.hpp:260
std::string getProcessName(pid_t pid)
Retrieves the name of the process by its process ID.
ProcMap getAddressMap(pid_t pid, uintptr_t address, const std::vector< ProcMap > &maps=std::vector< ProcMap >())
Retrieves the map information for a specific address in a remote process.
std::vector< ProcMap > getAllMaps(pid_t pid)
Retrieves information about all memory maps in a remote process.
std::string fmt(const char *fmt,...)
Formats a string using a printf-style format.