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
KittyPerfEvent.hpp
1#pragma once
2
3#include <fcntl.h>
4#include <poll.h>
5#include <unistd.h>
6
7#include <sys/ptrace.h>
8#include <sys/wait.h>
9#include <sys/user.h>
10#include <sys/mman.h>
11#include <sys/ioctl.h>
12
13#include <atomic>
14#include <vector>
15#include <functional>
16#include <cstdint>
17#include <cstring>
18
19#include <linux/perf_event.h>
20#include <linux/hw_breakpoint.h>
21
25static constexpr size_t KT_WATCH_PAGES = 8;
26
30enum KT_WATCH_LEN
31{
32 KT_PERFWATCH_LEN_1 = 1,
33 KT_PERFWATCH_LEN_2 = 2,
34 KT_PERFWATCH_LEN_3 = 3,
35 KT_PERFWATCH_LEN_4 = 4,
36 KT_PERFWATCH_LEN_5 = 5,
37 KT_PERFWATCH_LEN_6 = 6,
38 KT_PERFWATCH_LEN_7 = 7,
39 KT_PERFWATCH_LEN_8 = 8,
40};
41
45enum KT_WATCH_TYPE
46{
47 KT_PERFWATCH_EMPTY = 0,
48 KT_PERFWATCH_R = 1,
49 KT_PERFWATCH_W = 2,
50 KT_PERFWATCH_RW = KT_PERFWATCH_R | KT_PERFWATCH_W,
51 KT_PERFWATCH_X = 4,
52 KT_PERFWATCH_INVALID = KT_PERFWATCH_RW | KT_PERFWATCH_X,
53};
54
58struct KittyPerfSample
59{
63 uint64_t ip;
64
68 uint32_t pid;
69
73 uint32_t tid;
74
78 uint64_t time;
79
83 uint64_t addr;
84
85 KittyPerfSample() : ip(0), pid(0), tid(0), time(0), addr(0)
86 {
87 }
88 KittyPerfSample(uint64_t ip, uint32_t pid, uint32_t tid, uint64_t time, uint64_t addr)
89 : ip(ip), pid(pid), tid(tid), time(time), addr(addr)
90 {
91 }
92};
93
97class KittyPerfWatch
98{
99
100public:
101 KittyPerfWatch() = default;
102 ~KittyPerfWatch()
103 {
104 clear();
105 }
106
115 bool add(pid_t tid, uintptr_t addr, KT_WATCH_TYPE bp_type, KT_WATCH_LEN bp_len);
116
122 void poll(int timeout_ms, const std::function<bool(const KittyPerfSample &)> &cb);
123
127 void clear();
128
129protected:
131 {
132 int fd = -1;
133 void *mmap = nullptr;
134 size_t mmap_sz = 0;
135 };
136
137 std::vector<WatchInfo> _watches;
138
139 static inline int perf_event_open(struct perf_event_attr *attr,
140 pid_t pid,
141 int cpu,
142 int group_fd,
143 unsigned long flags)
144 {
145#if defined(__aarch64__)
146 return syscall(241, attr, pid, cpu, group_fd, flags);
147#elif defined(__arm__)
148 return syscall(364, attr, pid, cpu, group_fd, flags);
149#elif defined(__i386__)
150 return syscall(336, attr, pid, cpu, group_fd, flags);
151#elif defined(__x86_64__)
152 return syscall(298, attr, pid, cpu, group_fd, flags);
153#else
154#error "Unsupported ABI"
155#endif
156 }
157
158 bool enable();
159 bool disable();
160 void consumeRecord(const WatchInfo &w, KittyPerfSample *out);
161 void pollOnce(int timeout_ms, KittyPerfSample *out);
162};
163
167class KittyPerfTrap : public KittyPerfWatch
168{
169public:
178 bool add(pid_t tid, uintptr_t addr, KT_WATCH_TYPE bp_type, KT_WATCH_LEN bp_len);
179};
Class to manage trap signaling hardware watchpoins.
Definition KittyPerfEvent.hpp:168
bool add(pid_t tid, uintptr_t addr, KT_WATCH_TYPE bp_type, KT_WATCH_LEN bp_len)
Adds a new trap signaling hardware watchpoint to the watch list.
void clear()
Clears all watchpoints from the watch list.
bool add(pid_t tid, uintptr_t addr, KT_WATCH_TYPE bp_type, KT_WATCH_LEN bp_len)
Adds a new hardware watchpoint to the watch list.
void poll(int timeout_ms, const std::function< bool(const KittyPerfSample &)> &cb)
Polls the watch list for new samples.
Structure to hold the performance sample.
Definition KittyPerfEvent.hpp:59
uint64_t time
The timestamp of the sample in nanoseconds.
Definition KittyPerfEvent.hpp:78
uint64_t addr
The address of the memory location being watched.
Definition KittyPerfEvent.hpp:83
uint64_t ip
The instruction pointer (IP) of the sampled event.
Definition KittyPerfEvent.hpp:63
uint32_t tid
The thread ID (TID) of the thread that generated the sample.
Definition KittyPerfEvent.hpp:73
uint32_t pid
The process ID (PID) of the process that generated the sample.
Definition KittyPerfEvent.hpp:68
Definition KittyPerfEvent.hpp:131