25#ifndef MEMORYACCESSOR_SRC_CONSOLE_H_
26#define MEMORYACCESSOR_SRC_CONSOLE_H_
50 const Command &parent,
const std::vector<std::string>
55 std::vector<std::array<std::string, 2>>
74 Tools &tools)
noexcept(
false);
116 void SetBufferSize(const
size_t &buffer_size) { buffer_size_ = buffer_size; }
119 void Start() noexcept;
126 "A command-line front-end for exploring virtual memory of a linux "
128 "by accessing /proc/PID/mem file."};
138 {
"help", &Console::CommandHelp, {{
"help",
"Show help"}}},
140 &Console::CommandName,
141 {{
"name name [pid_num]",
"Search for PID by name and set PID if only 1 "
142 "PID found, or set PID number"},
144 "pid_num of found PIDs if pid_num is specified (starting from 0)."}}},
146 &Console::CommandPid,
147 {{
"pid PID",
"Set PID and parse /proc/PID/maps."}}},
149 &Console::CommandMaps,
150 {{
"maps",
"List memory segments found by parsing /proc/PID/maps."}}},
152 &Console::CommandView,
153 {{
"view SEGMENT",
"Print data of memory segment, where SEGMENT is its "
154 "\"maps\" number, or first"},
155 {
"",
"with matching name."},
156 {
"-h",
"show hex (if no -r specified)"},
157 {
"-r",
"print raw data"},
158 {
"-f file",
"output to file"}}},
160 &Console::CommandRead,
161 {{
"read address amount",
"Read amount bytes starting from address."},
162 {
"-h",
"show hex (if no -r specified)"},
163 {
"-r",
"print raw data"},
164 {
"-f file",
"output to file"}}},
166 &Console::CommandWrite,
167 {{
"write address amount string",
168 "Write amount bytes of string to memory starting from address."},
170 {
"write address amount -f file",
171 "Write amount bytes from file to memory starting from address."}}},
173 &Console::CommandDiff,
174 {{
"diff length [replacement]",
175 "Find difference in memory states by length and replace to string, if "
178 &Console::CommandAwait,
179 {{
"await process_name",
"Wait for the process with matching name."},
180 {
"await -p pid",
"Wait for the process with PID."}}},
189 enum class Error0Arg {
191 kPidNotSetUnexpectably,
193 kPrintErrCheckingProcess,
208 class WrapperException :
public std::exception {
216 WrapperException(uint8_t _return_code) : return_code(_return_code) {}
226 virtual const char *what() const noexcept
override {
227 return "Wrapper exception";
231 void PrintDescription(
const Command &command, uint32_t left = 2,
232 uint32_t middle = 0) const noexcept;
233 void ShowUsage(const
Command &command) const noexcept;
234 void PrintError0Arg(const Error0Arg &error) const noexcept;
235 void PrintFileNotOpened(const std::
string &path) const noexcept;
236 void PrintFileFail(const std::
string &path) const noexcept;
238 void PrintSegment(const
SegmentInfo &segmentInfo) const noexcept;
240 PrintSegments(const std::vector<
SegmentInfo> &segment_infos) const noexcept;
242 std::vector<std::
string> ParseCmdline(std::
string line) const noexcept;
244 uint8_t ParseAddress(const std::
string &s,
size_t &result) const noexcept;
245 uint8_t StoiWrapper(const std::
string &s,
int &result,
246 const std::
string &name) const noexcept;
247 uint8_t StoullWrapper(const std::
string &s, uint64_t &result,
248 const std::
string &name) const noexcept;
249 uint8_t ParseMapsWrapper() const noexcept;
250 uint8_t CheckPidWrapper() const noexcept;
251 uint8_t CheckSegNumWrapper(const
size_t &num) const noexcept;
252 uint8_t ReadSegWrapper(
char *dst, const
size_t &num,
size_t start = 0,
253 size_t amount = SIZE_MAX) const noexcept;
254 uint8_t WriteSegWrapper(
char *src, const
size_t &num,
size_t start = 0,
255 size_t amount = SIZE_MAX) const noexcept;
256 uint8_t ReadWrapper(
char *dst,
size_t address,
size_t amount,
257 size_t &done_amount) const noexcept;
258 uint8_t WriteWrapper(
char *src,
size_t address,
size_t amount,
259 size_t &done_amount) const noexcept;
261 uint8_t DiffReadSeg(std::unique_ptr<
char[]> &mem_dump,
262 const
size_t &num) noexcept;
263 void DiffCompare(const
char *old_dump, const
char *new_dump,
264 const
size_t &o_offs, const
size_t &n_offs,
size_t amount,
265 size_t start_addr, const
size_t &length,
266 const std::
string &replacement) noexcept;
267 uint8_t DiffOldNext(
size_t &i, const
size_t &old_segments_amount,
268 std::vector<std::unique_ptr<
char[]>>::iterator &it,
269 std::vector<std::unique_ptr<
char[]>> &full_dump) noexcept;
270 uint8_t DiffNewNext(
size_t &j,
271 std::vector<std::unique_ptr<
char[]>>::iterator &it,
272 std::unique_ptr<
char[]> &mem_dump,
273 std::vector<std::unique_ptr<
char[]>> &full_dump) noexcept;
275 void CommandHelp(const
Command &parent,
276 const std::vector<std::
string> &args) noexcept;
277 void CommandName(const
Command &parent,
278 const std::vector<std::
string> &args) noexcept;
279 void CommandPid(const
Command &parent,
280 const std::vector<std::
string> &args) noexcept;
281 void CommandMaps(const
Command &parent,
282 const std::vector<std::
string> &args) noexcept;
283 void CommandView(const
Command &parent,
284 const std::vector<std::
string> &args) noexcept;
285 void CommandRead(const
Command &parent,
286 const std::vector<std::
string> &args) noexcept;
287 void CommandWrite(const
Command &parent,
288 const std::vector<std::
string> &args) noexcept;
289 void CommandDiff(const
Command &parent,
290 const std::vector<std::
string> &args) noexcept;
291 void CommandAwait(const
Command &parent,
292 const std::vector<std::
string> &args) noexcept;
294 static
bool one_instance_created_;
296 const std::
string kCheckSudoStr{
297 "Check if you're running with \"sudo\"."};
305 bool seg_not_exist_msg_enabled_{
307 bool seg_no_access_msg_enabled_{
A class to perform CLI.
Definition console.h:68
void SetBufferSize(const size_t &buffer_size)
Set buffer size of an instance.
Definition console.h:116
Console & operator=(const Console &origin)=delete
Copy-assignment operator (deleted).
const std::string kProjectDescription
Project description.
Definition console.h:125
Console(Console &&origin)=delete
Move constructor (deleted).
Console(const Console &origin)=delete
Copy constructor (deleted).
const std::string kProjectName
Name of the project.
Definition console.h:123
Console & operator=(Console &&origin)=delete
Move-assignment operator (deleted).
void Start() noexcept
Start the console.
Definition console.cc:351
void ReadStdin() noexcept
Read and process input from stdin.
Definition console.cc:371
Console(MemoryAccessor &memory_accessor, HexViewer &hex_viewer, Tools &tools) noexcept(false)
Constructor.
Definition console.cc:306
static constexpr int kCommandsNumber
Number of the commands available.
Definition console.h:70
void HandleCommand(const std::string &line) noexcept
Handle line with command.
Definition console.cc:397
const Command kCommands[kCommandsNumber]
Definitions of commands.
Definition console.h:137
Tools & tools_
A reference to a Tools class instance.
Definition console.h:135
const std::string kProjectVersion
Project version.
Definition console.h:124
const std::string kConsolePrefix
Prefix shown in console input.
Definition console.h:129
~Console() noexcept
Destructor.
Definition console.cc:322
MemoryAccessor & memory_accessor_
A reference to a MemoryAccessor class instance.
Definition console.h:133
HexViewer & hex_viewer_
A reference to a HexViewer class instance.
Definition console.h:134
void PrintNameVer() const noexcept
Print project name and version.
Definition console.cc:340
A class for printing data in a readable format.
Definition hexviewer.h:36
A class to perform the main operations with memory.
Definition memoryaccessor.h:50
A struct that represents command in Console.
Definition console.h:48
std::vector< std::array< std::string, 2 > > description
Definition console.h:56
void(Console::*)( const Command &parent, const std::vector< std::string > &) CommandFuncP
Type of pointer to command function.
Definition console.h:49
std::string name
Command name.
Definition console.h:53
CommandFuncP func
Pointer.
Definition console.h:54
A struct to store the information of a memory segment.
Definition segmentinfo.h:37