|
MemoryAccessor 1
A command-line front-end for exploring virtual memory of a linux process by accessing /proc/PID/mem file.
|
A class to perform the main operations with memory. More...
#include <memoryaccessor.h>
Classes | |
| class | AddressNotInSegmentEx |
| Ex: Address does not belong to any segment. More... | |
| class | BadMapsEx |
| Ex: Parsing /proc/PID/maps failed. More... | |
| class | BaseException |
| Ex: Base exception. More... | |
| class | ErrCheckingPidEx |
| Ex: Could not check if PID exists. More... | |
| class | FileEx |
| Ex: Exception related to files. More... | |
| class | MapsFileEx |
| Ex: Opening /proc/PID/maps failed. More... | |
| class | MemFileEx |
| Ex: Operation with /proc/PID/mem failed. More... | |
| class | PidEx |
| Ex: Exception related to PID. More... | |
| class | PidNotExistEx |
| Ex: PID does not exist. More... | |
| class | PidNotSetEx |
| Ex: PID is not set. More... | |
| class | SegmentAccessDeniedEx |
| Ex: Access to the segment of memory denied. More... | |
| class | SegmentEx |
| Ex: Exception related to memory segments. More... | |
| class | SegmentNotExistEx |
| Ex: Segment of memory does not exist. More... | |
Public Member Functions | |
| MemoryAccessor (ProcessApi *process_api) noexcept(false) | |
| Constructor. | |
| MemoryAccessor (const MemoryAccessor &origin)=delete | |
| Copy constructor (deleted). | |
| MemoryAccessor (MemoryAccessor &&origin)=delete | |
| Move constructor (deleted). | |
| MemoryAccessor & | operator= (const MemoryAccessor &origin)=delete |
| Copy-assignment operator (deleted). | |
| MemoryAccessor & | operator= (MemoryAccessor &&origin)=delete |
| Move-assignment operator (deleted). | |
| ~MemoryAccessor () noexcept | |
| Destructor. | |
| pid_t | GetPid () const noexcept(false) |
| Get PID. | |
| void | SetPid (const pid_t &pid) noexcept(false) |
| Set PID. | |
| void | CheckPid () const noexcept(false) |
| Check if PID is set. | |
| void | ParseMaps () noexcept(false) |
| Parse maps file. | |
| std::unordered_set< std::string > | GetAllSegmentNames () const noexcept |
| Get all segment names. | |
| size_t | AddressInSegment (const size_t &address) const noexcept(false) |
| Find out which segment an address belongs to. | |
| void | CheckSegNum (const size_t &num) const noexcept(false) |
| Check if a segment with the given number exists. | |
| void | ResetSegments () noexcept |
| Delete all data related to found segments. | |
| void | Reset () noexcept |
| Reset all data related to PID. | |
| 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. | |
| 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. | |
| void | Read (char *dst, size_t address, size_t amount, size_t &done_amount) noexcept(false) |
| Read data from /proc/PID/mem. | |
| void | Write (const char *src, size_t address, size_t amount, size_t &done_amount) noexcept(false) |
| Write data to /proc/PID/mem. | |
Public Attributes | |
| ProcessApi * | process_api_ {nullptr} |
| A pointer to a ProcessApi class instance. | |
| std::map< std::string, SegmentInfo * > | special_segment_found_ |
| std::vector< SegmentInfo > | segment_infos_ |
A class to perform the main operations with memory.
This class is dedicated to make all the work of reading and writing from/to /proc/PID/mem. Firstly, PID needs to be set. Then the instance parses /proc/PID/maps to get information about memory segments. If everything is correct, on the found segments r/w operations can be performed. Moving, copying and creating more than 1 instance of this class is prohibited.
|
explicit |
Constructor.
| [in,out] | process_api | A pointer to an instance of ProcessApi class. |
| std::logic_error | If an instance of the class have already been created and it is a second instance. |
Initializes ProcessApi class pointer by value got as an parameter. Throws an exception if an instance of the class have already been created. Sets one_instance_created_ to true.
|
delete |
Copy constructor (deleted).
| [in] | origin | MemoryAccessor instance to copy from. |
Create a new object by copying an old one. Prohibited.
|
delete |
Move constructor (deleted).
| [in] | origin | Moved MemoryAccessor object. |
Create a new object by moving an old one. Prohibited.
|
noexcept |
Destructor.
Sets one_instance_created_ to false.
| size_t MemoryAccessor::AddressInSegment | ( | const size_t & | address | ) | const |
Find out which segment an address belongs to.
| [in] | address | Address to process. |
| AddressNotInSegmentEx | If the given address does not belong to any segment. |
Find out which memory segment an address belongs to and return the number of the segment.
| void MemoryAccessor::CheckPid | ( | ) | const |
Check if PID is set.
| PidNotSetEx | If pid_set_ is false. |
Check if pid_set_ is true, and, if it is not, throws an exception.
| void MemoryAccessor::CheckSegNum | ( | const size_t & | num | ) | const |
Check if a segment with the given number exists.
| [in] | num | Number of the memory segment starting from 0. |
| PidNotSetEx | If PID is not set. |
| SegmentNotExistEx | If the segment does not exist. |
Check if a memory segment with the given number exists. The function also checks if PID is set.
|
noexcept |
Get all segment names.
Get all segment names that are currently stored.
| pid_t MemoryAccessor::GetPid | ( | ) | const |
Get PID.
| PidNotSetEx | If PID is not set. |
Get PID if it is set and throw an exception otherwise.
|
delete |
Copy-assignment operator (deleted).
| [in] | origin | MemoryAccessor instance to copy from. |
Assign an object by copying other object. Prohibited.
|
delete |
Move-assignment operator (deleted).
| [in] | origin | Moved MemoryAccessor object. |
Assign an object by moving other object. Prohibited.
| void MemoryAccessor::ParseMaps | ( | ) |
Parse maps file.
| BadMapsEx | If an error in parsing /proc/PID/maps file occured. |
| MapsFileEx | If an error in opening /proc/PID/maps file occured. |
| PidNotSetEx | If PID is not set. |
Open and parse /proc/PID/maps file saving data in segment_infos_ and special_segment_found_.
| void MemoryAccessor::Read | ( | char * | dst, |
| size_t | address, | ||
| size_t | amount, | ||
| size_t & | done_amount ) |
Read data from /proc/PID/mem.
| [out] | dst | Destination to which data will be copied. Needs to be a valid array of the specified amount. |
| [in] | address | Address to start from. |
| [in] | amount | Number of bytes to read. |
| [out] | done_amount | How much data were read. |
| AddressNotInSegmentEx | If an address reached that does not belong to any segment. |
| MemFileEx | If an error in opening /proc/PID/mem file occured. |
| PidNotSetEx | If PID is not set. |
| SegmentAccessDeniedEx | If a segment is reached, access to which is denied by an operating system. |
| SegmentNotExistEx | Must not be thrown normally, but appears in called methods. |
Read data from /proc/PID/mem to a destination "dst", modifying done_amount by how many bytes were read.
| size_t MemoryAccessor::ReadSegment | ( | char * | dst, |
| const size_t & | num, | ||
| size_t | start = 0, | ||
| size_t | amount = SIZE_MAX ) |
Read full memory segment or a part of it.
| [out] | dst | Destination to which data will be copied. Needs to be a valid array of the specified amount. |
| [in] | num | Number of the memory segment starting from 0. |
| [in] | start | Offset relative to the start of the segment, default is 0. |
| [in] | amount | Number of bytes to capture after the "start" parameter, default is SIZE_MAX. If a value is too big, it is set to a maximum appropriate value. |
| AddressNotInSegmentEx | If the value of parameter "start" represents address on/after the end of the segment. |
| MemFileEx | If an error in opening /proc/PID/mem file occured. |
| PidNotSetEx | If PID is not set. |
| SegmentAccessDeniedEx | If access to the segment is denied by an operating system. |
| SegmentNotExistEx | If a segment with a number "num" does not exist. |
Read full memory segment or a part of it to a destination "dst" and return how many bytes were read.
|
noexcept |
Reset all data related to PID.
Clear/delete all variables which are related to current PID, including setting pid_set_ to false.
|
noexcept |
Delete all data related to found segments.
Clear/delete all variables which conatin information about found segments.
| void MemoryAccessor::SetPid | ( | const pid_t & | pid | ) |
Set PID.
| [in] | pid | PID value to set. |
| ErrCheckingPidEx | If an error while checking if PID exists occured. |
| PidNotExistEx | If PID provided is not exist. |
Reset all objects of an instance that are related to PID and set new PID.
| void MemoryAccessor::Write | ( | const char * | src, |
| size_t | address, | ||
| size_t | amount, | ||
| size_t & | done_amount ) |
Write data to /proc/PID/mem.
| [in] | src | Source from which data will be copied. Needs to be a valid array of the specified amount. |
| [in] | address | Address to start from. |
| [in] | amount | Number of bytes to write. |
| [out] | done_amount | How much data were written. |
| AddressNotInSegmentEx | If an address reached that does not belong to any segment. |
| MemFileEx | If an error in opening /proc/PID/mem file occured. |
| PidNotSetEx | If PID is not set. |
| SegmentAccessDeniedEx | If a segment is reached, access to which is denied by an operating system. |
| SegmentNotExistEx | Must not be thrown normally, but appears in called methods. |
Write data to /proc/PID/mem from a source "src", modifying done_amount by how many bytes were written.
| size_t MemoryAccessor::WriteSegment | ( | const char * | src, |
| const size_t & | num, | ||
| size_t | start = 0, | ||
| size_t | amount = SIZE_MAX ) |
Write data to memory segment.
| [in] | src | Source from which data will be copied. Needs to be a valid array of the specified amount. |
| [in] | num | Number of the memory segment starting from 0. |
| [in] | start | Offset relative to the start of the segment, default is 0. |
| [in] | amount | Number of bytes to capture after the "start" parameter, default is SIZE_MAX. If a value is too big, it is set to a maximum appropriate value. |
| AddressNotInSegmentEx | If the value of parameter "start" represents address on/after the end of the segment. |
| MemFileEx | If an error in opening /proc/PID/mem file occured. |
| PidNotSetEx | If PID is not set. |
| SegmentAccessDeniedEx | If access to the segment is denied by an operating system. |
| SegmentNotExistEx | If a segment with a number "num" does not exist. |
Write data to memory segment from a source "src" and return how many bytes were written.
| std::vector<SegmentInfo> MemoryAccessor::segment_infos_ |
SegmentInfo objects got as a result of parsing /proc/PID/maps.
| std::map<std::string, SegmentInfo *> MemoryAccessor::special_segment_found_ |
"Special" segment infos found (segments, which name starts with '['). Contains pairs that consist of a segment name and a pointer to SegmentInfo.