MemoryAccessor 1
A command-line front-end for exploring virtual memory of a linux process by accessing /proc/PID/mem file.
Loading...
Searching...
No Matches
memoryaccessor.h
Go to the documentation of this file.
1// MemoryAccessor - A tool for accessing /proc/PID/mem
2// Copyright (C) 2024 zloymish
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
25#ifndef MEMORYACCESSOR_SRC_MEMORYACCESSOR_H_
26#define MEMORYACCESSOR_SRC_MEMORYACCESSOR_H_
27
28#include <sys/types.h>
29
30#include <cstdint>
31#include <exception>
32#include <fstream>
33#include <map>
34#include <string>
35#include <unordered_set>
36#include <vector>
37
38#include "segmentinfo.h"
39#include "tools.h"
40
51public:
57 class BaseException : public std::exception {
64 virtual const char *what() const noexcept override {
65 return "Base exception";
66 }
67 };
68
74 class PidEx : public BaseException {
81 virtual const char *what() const noexcept override {
82 return "PID exception";
83 }
84 };
85
92 class ErrCheckingPidEx : public PidEx {
99 virtual const char *what() const noexcept override {
100 return "An error occurred while checking if PID exists";
101 }
102 };
103
110 class PidNotExistEx : public PidEx {
117 virtual const char *what() const noexcept override {
118 return "PID not exist";
119 }
120 };
121
128 class PidNotSetEx : public PidEx {
135 virtual const char *what() const noexcept override { return "PID not set"; }
136 };
137
143 class FileEx : public BaseException {
150 virtual const char *what() const noexcept override {
151 return "Error in opening file";
152 }
153 };
154
161 class MemFileEx : public FileEx {
168 virtual const char *what() const noexcept override {
169 return "Error in opening current /proc/PID/mem";
170 }
171 };
172
178 class MapsFileEx : public FileEx {
185 virtual const char *what() const noexcept override {
186 return "Error in opening current /proc/PID/maps";
187 }
188 };
189
195 class BadMapsEx : public BaseException {
202 virtual const char *what() const noexcept override {
203 return "Error in parsing /proc/PID/maps";
204 }
205 };
206
213 class SegmentEx : public BaseException {
220 virtual const char *what() const noexcept override {
221 return "Segment exception";
222 }
223 };
224
238 virtual const char *what() const noexcept override {
239 return "The segment of memory does not exist";
240 }
241 };
242
256 virtual const char *what() const noexcept override {
257 return "Access to the segment of memory denied";
258 }
259 };
260
274 virtual const char *what() const noexcept override {
275 return "Address does not belong to any segment";
276 }
277 };
278
279 explicit MemoryAccessor(Tools &tools) noexcept(false);
280
287 MemoryAccessor(const MemoryAccessor &origin) = delete;
288
295 MemoryAccessor(MemoryAccessor &&origin) = delete;
296
303 MemoryAccessor &operator=(const MemoryAccessor &origin) = delete;
304
312
313 ~MemoryAccessor() noexcept;
314
315 pid_t GetPid() const noexcept(false);
316 void SetPid(const pid_t &pid) noexcept(false);
317 void CheckPid() const noexcept(false);
318 void ParseMaps() noexcept(false);
319 std::unordered_set<std::string> GetAllSegmentNames() const noexcept;
320 size_t AddressInSegment(const size_t &address) const noexcept(false);
321 void CheckSegNum(const size_t &num) const noexcept(false);
322 void ResetSegments() noexcept;
323 void Reset() noexcept;
324 size_t ReadSegment(char *dst, const size_t &num, size_t start = 0,
325 size_t amount = SIZE_MAX) noexcept(false);
326 size_t WriteSegment(const char *src, const size_t &num, size_t start = 0,
327 size_t amount = SIZE_MAX) noexcept(false);
328 void Read(char *dst, size_t address, size_t amount,
329 size_t &done_amount) noexcept(false);
330 void Write(const char *src, size_t address, size_t amount,
331 size_t &done_amount) noexcept(false);
332
334
335 std::map<std::string, SegmentInfo *>
340 std::vector<SegmentInfo>
343private:
344 void OpenMem() noexcept(false);
345 void CheckMem() noexcept(false);
346 void CheckSegBoundaries(const size_t &num, const size_t &start,
347 size_t &amount) const noexcept(false);
348 void PrepareMemSegment(const size_t &num, const size_t &start,
349 size_t &amount) noexcept(false);
350
351 static bool one_instance_created_;
353
354 std::fstream mem_;
355
356 pid_t pid_{0};
359 bool pid_set_{
360 false};
361};
362
363#endif // MEMORYACCESSOR_SRC_MEMORYACCESSOR_H_
Ex: Address does not belong to any segment.
Definition memoryaccessor.h:267
Ex: Parsing /proc/PID/maps failed.
Definition memoryaccessor.h:195
Ex: Base exception.
Definition memoryaccessor.h:57
Ex: Could not check if PID exists.
Definition memoryaccessor.h:92
Ex: Exception related to files.
Definition memoryaccessor.h:143
Ex: Opening /proc/PID/maps failed.
Definition memoryaccessor.h:178
Ex: Operation with /proc/PID/mem failed.
Definition memoryaccessor.h:161
Ex: Exception related to PID.
Definition memoryaccessor.h:74
Ex: PID does not exist.
Definition memoryaccessor.h:110
Ex: PID is not set.
Definition memoryaccessor.h:128
Ex: Access to the segment of memory denied.
Definition memoryaccessor.h:249
Ex: Exception related to memory segments.
Definition memoryaccessor.h:213
Ex: Segment of memory does not exist.
Definition memoryaccessor.h:231
A class to perform the main operations with memory.
Definition memoryaccessor.h:50
void CheckPid() const noexcept(false)
Check if PID is set.
Definition memoryaccessor.cc:107
pid_t GetPid() const noexcept(false)
Get PID.
Definition memoryaccessor.cc:72
MemoryAccessor & operator=(const MemoryAccessor &origin)=delete
Copy-assignment operator (deleted).
std::vector< SegmentInfo > segment_infos_
Definition memoryaccessor.h:341
size_t AddressInSegment(const size_t &address) const noexcept(false)
Find out which segment an address belongs to.
Definition memoryaccessor.cc:200
size_t WriteSegment(const char *src, const size_t &num, size_t start=0, size_t amount=SIZE_MAX) noexcept(false)
Write data to memory segment.
Definition memoryaccessor.cc:294
MemoryAccessor(const MemoryAccessor &origin)=delete
Copy constructor (deleted).
MemoryAccessor(MemoryAccessor &&origin)=delete
Move constructor (deleted).
void ParseMaps() noexcept(false)
Parse maps file.
Definition memoryaccessor.cc:121
size_t ReadSegment(char *dst, const size_t &num, size_t start=0, size_t amount=SIZE_MAX) noexcept(false)
Read full memory segment or a part of it.
Definition memoryaccessor.cc:267
void Read(char *dst, size_t address, size_t amount, size_t &done_amount) noexcept(false)
Read data from /proc/PID/mem.
Definition memoryaccessor.cc:322
std::unordered_set< std::string > GetAllSegmentNames() const noexcept
Get all segment names.
Definition memoryaccessor.cc:179
void SetPid(const pid_t &pid) noexcept(false)
Set PID.
Definition memoryaccessor.cc:85
std::map< std::string, SegmentInfo * > special_segment_found_
Definition memoryaccessor.h:336
MemoryAccessor(Tools &tools) noexcept(false)
Constructor.
Definition memoryaccessor.cc:51
void ResetSegments() noexcept
Delete all data related to found segments.
Definition memoryaccessor.cc:232
Tools & tools_
A reference to a Tools class instance.
Definition memoryaccessor.h:333
void Reset() noexcept
Reset all data related to PID.
Definition memoryaccessor.cc:243
~MemoryAccessor() noexcept
Destructor.
Definition memoryaccessor.cc:63
void Write(const char *src, size_t address, size_t amount, size_t &done_amount) noexcept(false)
Write data to /proc/PID/mem.
Definition memoryaccessor.cc:365
void CheckSegNum(const size_t &num) const noexcept(false)
Check if a segment with the given number exists.
Definition memoryaccessor.cc:220
MemoryAccessor & operator=(MemoryAccessor &&origin)=delete
Move-assignment operator (deleted).
A struct with various tools that are independent or depend on operating system.
Definition tools.h:45
SegmentInfo header.
A struct to store the information of a memory segment.
Definition segmentinfo.h:37
Tools header.