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 Class Reference

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 (Tools &tools) noexcept(false)
 Constructor.
 
 MemoryAccessor (const MemoryAccessor &origin)=delete
 Copy constructor (deleted).
 
 MemoryAccessor (MemoryAccessor &&origin)=delete
 Move constructor (deleted).
 
MemoryAccessoroperator= (const MemoryAccessor &origin)=delete
 Copy-assignment operator (deleted).
 
MemoryAccessoroperator= (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

Toolstools_
 A reference to a Tools class instance.
 
std::map< std::string, SegmentInfo * > special_segment_found_
 
std::vector< SegmentInfosegment_infos_
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MemoryAccessor() [1/3]

MemoryAccessor::MemoryAccessor ( Tools & tools)
explicit

Constructor.

Parameters
[in,out]toolsA reference to an instance of Tools struct.
Exceptions
std::logic_errorIf an instance of the class have already been created and it is a second instance.

Initializes Tools struct reference 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.

◆ MemoryAccessor() [2/3]

MemoryAccessor::MemoryAccessor ( const MemoryAccessor & origin)
delete

Copy constructor (deleted).

Parameters
[in]originMemoryAccessor instance to copy from.

Create a new object by copying an old one. Prohibited.

◆ MemoryAccessor() [3/3]

MemoryAccessor::MemoryAccessor ( MemoryAccessor && origin)
delete

Move constructor (deleted).

Parameters
[in]originMoved MemoryAccessor object.

Create a new object by moving an old one. Prohibited.

◆ ~MemoryAccessor()

MemoryAccessor::~MemoryAccessor ( )
noexcept

Destructor.

Sets one_instance_created_ to false.

Member Function Documentation

◆ AddressInSegment()

size_t MemoryAccessor::AddressInSegment ( const size_t & address) const

Find out which segment an address belongs to.

Parameters
[in]addressAddress to process.
Returns
Number of the segment.
Exceptions
AddressNotInSegmentExIf 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.

◆ CheckPid()

void MemoryAccessor::CheckPid ( ) const

Check if PID is set.

Exceptions
PidNotSetExIf pid_set_ is false.

Check if pid_set_ is true, and, if it is not, throws an exception.

◆ CheckSegNum()

void MemoryAccessor::CheckSegNum ( const size_t & num) const

Check if a segment with the given number exists.

Parameters
[in]numNumber of the memory segment starting from 0.
Exceptions
PidNotSetExIf PID is not set.
SegmentNotExistExIf the segment does not exist.

Check if a memory segment with the given number exists. The function also checks if PID is set.

◆ GetAllSegmentNames()

std::unordered_set< std::string > MemoryAccessor::GetAllSegmentNames ( ) const
noexcept

Get all segment names.

Returns
std::unordered_set with all segment names in std::string type.

Get all segment names that are currently stored.

◆ GetPid()

pid_t MemoryAccessor::GetPid ( ) const

Get PID.

Returns
Current PID assigned to an instance.
Exceptions
PidNotSetExIf PID is not set.

Get PID if it is set and throw an exception otherwise.

◆ operator=() [1/2]

MemoryAccessor & MemoryAccessor::operator= ( const MemoryAccessor & origin)
delete

Copy-assignment operator (deleted).

Parameters
[in]originMemoryAccessor instance to copy from.

Assign an object by copying other object. Prohibited.

◆ operator=() [2/2]

MemoryAccessor & MemoryAccessor::operator= ( MemoryAccessor && origin)
delete

Move-assignment operator (deleted).

Parameters
[in]originMoved MemoryAccessor object.

Assign an object by moving other object. Prohibited.

◆ ParseMaps()

void MemoryAccessor::ParseMaps ( )

Parse maps file.

Exceptions
BadMapsExIf an error in parsing /proc/PID/maps file occured.
MapsFileExIf an error in opening /proc/PID/maps file occured.
PidNotSetExIf PID is not set.

Open and parse /proc/PID/maps file saving data in segment_infos_ and special_segment_found_.

◆ Read()

void MemoryAccessor::Read ( char * dst,
size_t address,
size_t amount,
size_t & done_amount )

Read data from /proc/PID/mem.

Parameters
[out]dstDestination to which data will be copied.
[in]addressAddress to start from.
[in]amountNumber of bytes to read.
[out]done_amountHow much data were read.
Exceptions
AddressNotInSegmentExIf an address reached that does not belong to any segment.
MemFileExIf an error in opening /proc/PID/mem file occured.
PidNotSetExIf PID is not set.
SegmentAccessDeniedExIf a segment is reached, access to which is denied by an operating system.
SegmentNotExistExMust 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.

◆ ReadSegment()

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.

Parameters
[out]dstDestination to which data will be copied.
[in]numNumber of the memory segment starting from 0.
[in]startOffset relative to the start of the segment, default is 0.
[in]amountNumber 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.
Returns
Amount of bytes read.
Exceptions
AddressNotInSegmentExIf the value of parameter "start" represents address on/after the end of the segment.
MemFileExIf an error in opening /proc/PID/mem file occured.
PidNotSetExIf PID is not set.
SegmentAccessDeniedExIf access to the segment is denied by an operating system.
SegmentNotExistExIf 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.

◆ Reset()

void MemoryAccessor::Reset ( )
noexcept

Reset all data related to PID.

Clear/delete all variables which are related to current PID, including setting pid_set_ to false.

◆ ResetSegments()

void MemoryAccessor::ResetSegments ( )
noexcept

Delete all data related to found segments.

Clear/delete all variables which conatin information about found segments.

◆ SetPid()

void MemoryAccessor::SetPid ( const pid_t & pid)

Set PID.

Parameters
[in]pidPID value to set.
Exceptions
ErrCheckingPidExIf an error while checking if PID exists occured.
PidNotExistExIf PID provided is not exist.

Reset all objects of an instance that are related to PID and set new PID.

◆ Write()

void MemoryAccessor::Write ( const char * src,
size_t address,
size_t amount,
size_t & done_amount )

Write data to /proc/PID/mem.

Parameters
[in]srcSource from which data will be copied.
[in]addressAddress to start from.
[in]amountNumber of bytes to write.
[out]done_amountHow much data were written.
Exceptions
AddressNotInSegmentExIf an address reached that does not belong to any segment.
MemFileExIf an error in opening /proc/PID/mem file occured.
PidNotSetExIf PID is not set.
SegmentAccessDeniedExIf a segment is reached, access to which is denied by an operating system.
SegmentNotExistExMust 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.

◆ WriteSegment()

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.

Parameters
[in]srcSource from which data will be copied.
[in]numNumber of the memory segment starting from 0.
[in]startOffset relative to the start of the segment, default is 0.
[in]amountNumber 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.
Returns
Amount of bytes written.
Exceptions
AddressNotInSegmentExIf the value of parameter "start" represents address on/after the end of the segment.
MemFileExIf an error in opening /proc/PID/mem file occured.
PidNotSetExIf PID is not set.
SegmentAccessDeniedExIf access to the segment is denied by an operating system.
SegmentNotExistExIf 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.

Member Data Documentation

◆ segment_infos_

std::vector<SegmentInfo> MemoryAccessor::segment_infos_

SegmentInfo objects got as a result of parsing /proc/PID/maps.

◆ special_segment_found_

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.


The documentation for this class was generated from the following files: