-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
101 lines (91 loc) · 2.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <fstream>
#include <VAS16.h>
#include <FullReadVNVMProvider.h>
#include <Instruction.h>
#include <VNProcess.h>
std::unique_ptr<VNProcess> ActiveProcess;
bool isProcessLoaded() {
if(ActiveProcess.get() == nullptr) {
std::cout << "No active process loaded" << std::endl;
return false;
}
return true;
}
void loadProcess(std::string path) {
std::ifstream reader(path, std::ios::binary | std::ios::in);
auto exeRegProvider = std::make_unique<FullReadNVVMProvider>(reader);
auto vas = std::make_unique<VAS16>(exeRegProvider.get());
vas->SetStackPtr(exeRegProvider->GetStackPtr());
std::unique_ptr<IVirtualAddressSpace<Word>> addressSpace = std::move(vas);
std::unique_ptr<IRegistryProvider> regProvider = std::move(exeRegProvider);
ActiveProcess.reset(new VNProcess(addressSpace, regProvider));
}
void run() {
if(isProcessLoaded()) {
while (ActiveProcess->Iteration()) {
}
std::cout << "Process terminated" << std::endl;
}
}
void step() {
if(isProcessLoaded()) {
ActiveProcess->Iteration();
}
}
void registers() {
if(isProcessLoaded()) {
ActiveProcess->DumpRegisters();
}
}
void stack(int n) {
if(isProcessLoaded()) {
ActiveProcess->DumpStack(n);
}
}
void dump(std::string dump) {
if(isProcessLoaded()) {
ActiveProcess->DumpImage(dump);
}
}
int main(int argc, char** argv)
{
if(argc > 1) {
std::string path = argv[1];
loadProcess(path);
}
std::string command;
std::cin >> command;
while (command.compare("exit") != 0) {
if(command.compare("load") == 0) {
std::string path;
std::cin >> path;
loadProcess(path);
} else if(command.compare("run") == 0) {
run();
} else if(command.compare("step") == 0) {
step();
} else if(command.compare("regs") == 0) {
registers();
} else if(command.compare("stack") == 0) {
int topN;
std::cin >> topN;
stack(topN);
} else if(command.compare("dump") == 0) {
std::string path;
std::cin >> path;
dump(path);
} else {
std::cout << "Not a command. Valid commands:" << std::endl <<
"\texit" << std::endl <<
"\tload <file>" << std::endl <<
"\trun" << std::endl <<
"\tstep" << std::endl <<
"\tregs" << std::endl <<
"\tstack <N>" << std::endl <<
"\tdump <file>" << std::endl;
}
std::cin >> command;
}
return 0;
}