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
console.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_CONSOLE_H_
26#define MEMORYACCESSOR_SRC_CONSOLE_H_
27
28#include <array>
29#include <cstdint>
30#include <exception>
31#include <string>
32#include <vector>
33
34#include "hexviewer.h"
35#include "memoryaccessor.h"
36#include "segmentinfo.h"
37#include "tools.h"
38
39class Console;
40
48struct Command {
49 using CommandFuncP = void (Console::*)(
50 const Command &parent, const std::vector<std::string>
51 &);
52
53 std::string name;
54 CommandFuncP func{nullptr};
55 std::vector<std::array<std::string, 2>>
58};
59
68class Console {
69public:
70 constexpr static int kCommandsNumber{
71 9};
72
73 explicit Console(MemoryAccessor &memory_accessor, HexViewer &hex_viewer,
74 Tools &tools) noexcept(false);
75
82 Console(const Console &origin) = delete;
83
90 Console(Console &&origin) = delete;
91
98 Console &operator=(const Console &origin) = delete;
99
106 Console &operator=(Console &&origin) = delete;
107
108 ~Console() noexcept;
109
116 void SetBufferSize(const size_t &buffer_size) { buffer_size_ = buffer_size; }
117
118 void PrintNameVer() const noexcept;
119 void Start() noexcept;
120 void ReadStdin() noexcept;
121 void HandleCommand(const std::string &line) noexcept;
122
123 const std::string kProjectName{"MemoryAccessor"};
124 const std::string kProjectVersion{"v1.0"};
125 const std::string kProjectDescription{
126 "A command-line front-end for exploring virtual memory of a linux "
127 "process "
128 "by accessing /proc/PID/mem file."};
129 const std::string kConsolePrefix{
130 "(MemAcc)"};
131
136
138 {"help", &Console::CommandHelp, {{"help", "Show help"}}},
139 {"name",
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"},
143 {"",
144 "pid_num of found PIDs if pid_num is specified (starting from 0)."}}},
145 {"pid",
146 &Console::CommandPid,
147 {{"pid PID", "Set PID and parse /proc/PID/maps."}}},
148 {"maps",
149 &Console::CommandMaps,
150 {{"maps", "List memory segments found by parsing /proc/PID/maps."}}},
151 {"view",
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"}}},
159 {"read",
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"}}},
165 {"write",
166 &Console::CommandWrite,
167 {{"write address amount string",
168 "Write amount bytes of string to memory starting from address."},
169 {"or", ""},
170 {"write address amount -f file",
171 "Write amount bytes from file to memory starting from address."}}},
172 {"diff",
173 &Console::CommandDiff,
174 {{"diff length [replacement]",
175 "Find difference in memory states by length and replace to string, if "
176 "specified."}}},
177 {"await",
178 &Console::CommandAwait,
179 {{"await process_name", "Wait for the process with matching name."},
180 {"await -p pid", "Wait for the process with PID."}}},
181 };
182private:
189 enum class Error0Arg {
190 kPidNotSet,
191 kPidNotSetUnexpectably,
192 kErrCheckingPid,
193 kPrintErrCheckingProcess,
194 kPrintErrOpenMaps,
195 kPrintErrParseMaps,
196 kPrintErrOpenMem,
197 kPrintSegNotExist,
198 kPrintSegNoAccess,
199 };
200
208 class WrapperException : public std::exception {
209 public:
216 WrapperException(uint8_t _return_code) : return_code(_return_code) {}
217
218 uint8_t return_code;
219 private:
226 virtual const char *what() const noexcept override {
227 return "Wrapper exception";
228 }
229 };
230
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;
237
238 void PrintSegment(const SegmentInfo &segmentInfo) const noexcept;
239 void
240 PrintSegments(const std::vector<SegmentInfo> &segment_infos) const noexcept;
241
242 std::vector<std::string> ParseCmdline(std::string line) const noexcept;
243
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;
260
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;
274
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;
293
294 static bool one_instance_created_;
296 const std::string kCheckSudoStr{
297 "Check if you're running with \"sudo\"."};
301
302 size_t buffer_size_{
303 0x1000};
304
305 bool seg_not_exist_msg_enabled_{
306 true};
307 bool seg_no_access_msg_enabled_{
308 true};
309};
310
311#endif // MEMORYACCESSOR_SRC_CONSOLE_H_
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 with various tools that are independent or depend on operating system.
Definition tools.h:45
HexViewer header.
MemoryAccessor header.
SegmentInfo header.
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
Tools header.