forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessenum.cpp
More file actions
146 lines (136 loc) · 4.29 KB
/
Copy pathprocessenum.cpp
File metadata and controls
146 lines (136 loc) · 4.29 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Demonstrates the use of ProcessEnumerator
// queries the Enumerator for all DF Processes on user input. Prints them to the terminal.
// also tracks processes that were invalidated
#include <iostream>
#include <climits>
#include <vector>
#include <ctime>
using namespace std;
#include <DFHack.h>
#include <dfhack/DFProcessEnumerator.h>
using namespace DFHack;
#ifndef LINUX_BUILD
#endif
void printhelp ()
{
cout << "enter empty line for next try." << endl;
cout << "enter 'next' or 'n' for next test." << endl;
cout << "enter 'help' to show this text again." << endl;
}
int inputwait (const char * prompt)
{
inputwait_reset:
string command = "";
cout <<"[" << prompt << "]# ";
getline(cin, command);
if(command == "help")
{
printhelp();
goto inputwait_reset;
}
else if(command == "")
{
return 1;
}
else if(command == "next")
{
return 0;
}
else
{
cout << "Command not recognized. Try 'help' for a list of valid commands." << endl;
goto inputwait_reset;
}
return 0;
}
int main (void)
{
printhelp();
cout << endl;
// first test ProcessEnumerator and BadProcesses
{
cout << "Testing ProcessEnumerator" << endl;
ProcessEnumerator Penum("Memory.xml");
VersionInfo * mem;
do
{
// make the ProcessEnumerator update its list of Processes
// by passing the pointer to 'inval', we make it export expired
// processes instead of destroying them outright
// (processes expire when the OS kills them for whatever reason)
BadProcesses inval;
Penum.Refresh(&inval);
int nProc = Penum.size();
int nInval = inval.size();
cout << "Processes:" << endl;
for(int i = 0; i < nProc; i++)
{
mem = Penum[i]->getDescriptor();
cout << "DF instance: " << Penum[i]->getPID()
<< ", " << mem->getVersion() << endl;
}
cout << "Invalidated:" << endl;
for(int i = 0; i < nInval; i++)
{
mem = inval[i]->getDescriptor();
cout << "DF instance: " << inval[i]->getPID()
<< ", " << mem->getVersion() << endl;
}
}
while(inputwait("ProcessEnumerator"));
}
// next test ContextManager and BadContexts
{
cout << "Testing ContextManager" << endl;
ContextManager Cman("Memory.xml");
VersionInfo * mem;
do
{
// make the ContextManager update its list of Contexts
// by passing the pointer to 'inval', we make it export expired
// contexts instead of destroying them outright
// (contexts expire when the OS kills their process for whatever
// reason)
BadContexts inval;
Cman.Refresh(&inval);
int nCont = Cman.size();
int nInval = inval.size();
DFHack::Context * cont = Cman.getSingleContext();
if(cont)
{
if(cont->Attach())
{
DFHack::Maps * mapz = cont->getMaps();
cont->Suspend();
mapz->Start();
cont->Resume();
}
bool result = cont->Detach();
if(!result)
{
cerr << "Something went horribly wrong during detach" << endl;
}
}
cout << "Contexts:" << endl;
for(int i = 0; i < nCont; i++)
{
mem = Cman[i]->getMemoryInfo();
cout << "DF instance: " << Cman[i]->getProcess()->getPID()
<< ", " << mem->getVersion() << endl;
}
cout << "Invalidated:" << endl;
for(int i = 0; i < nInval; i++)
{
mem = inval[i]->getMemoryInfo();
cout << "DF instance: " << inval[i]->getProcess()->getPID()
<< ", " << mem->getVersion() << endl;
}
}
while(inputwait("ContextManager"));
}
#ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl;
cin.ignore();
#endif
return 0;
}