-
Notifications
You must be signed in to change notification settings - Fork 0
/
memlib.h
83 lines (68 loc) · 1.82 KB
/
memlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef MEMLIB_H
#define MEMLIB_H
#include <sys/uio.h>
#include <dirent.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <memory>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <istream>
#include <iterator>
#define READABLE(x) ((x >> 2) & 1)
#define WRITABLE(x) ((x >> 1) & 1)
#define EXECUTABLE(x) ((x) & 1)
namespace memlib {
class MemoryTool {
public:
MemoryTool(pid_t pid) : _pid(pid) { };
~MemoryTool() { };
MemoryTool(const MemoryTool& other) = delete;
MemoryTool& operator=(const MemoryTool& rhs) = delete;
bool ReadMem(char* start, const uint32_t len, char** result);
bool WriteMem(char* start, const uint32_t len, const char* data);
private:
pid_t _pid;
};
struct VMMapEntry {
char* start;
char* end;
uint8_t perms;
std::string name;
friend std::ostream& operator<<(std::ostream &out,
const VMMapEntry& entry) {
if (!entry.name.empty()) {
out << entry.name << ": ";
} else {
out << "(Unknown): ";
}
out << std::hex << static_cast<void*>(entry.start) << "-";
out << static_cast<void*>(entry.end) << " ";
if (READABLE(entry.perms))
out << "r";
if (WRITABLE(entry.perms))
out << "w";
if (EXECUTABLE(entry.perms))
out << "x";
return out;
}
};
class Process {
public:
Process(pid_t pid);
std::string GetName() const { return _name; };
std::vector<VMMapEntry>* GetVMMap() const;
pid_t GetPID() const { return _pid; };
private:
pid_t _pid;
std::string _name;
};
std::ostream& operator<<(std::ostream& out, const memlib::Process& proc);
std::vector<pid_t>* get_all_pids();
}
#endif // MEMLIB_H