2010-05-27 21:18:32 -06:00
|
|
|
// 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>
|
2011-12-31 04:48:42 -07:00
|
|
|
#include <DFProcessEnumerator.h>
|
2010-05-27 21:18:32 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
#endif
|
2010-05-29 13:34:36 -06:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-27 21:18:32 -06:00
|
|
|
int main (void)
|
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
printhelp();
|
|
|
|
cout << endl;
|
|
|
|
// first test ProcessEnumerator and BadProcesses
|
2010-05-27 21:18:32 -06:00
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
cout << "Testing ProcessEnumerator" << endl;
|
|
|
|
ProcessEnumerator Penum("Memory.xml");
|
2010-08-20 06:10:05 -06:00
|
|
|
VersionInfo * mem;
|
2010-05-29 13:34:36 -06:00
|
|
|
do
|
2010-05-27 21:18:32 -06:00
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
// 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;
|
|
|
|
}
|
2010-05-27 21:18:32 -06:00
|
|
|
|
2010-05-29 13:34:36 -06:00
|
|
|
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
|
|
|
|
{
|
2010-06-05 16:56:09 -06:00
|
|
|
cout << "Testing ContextManager" << endl;
|
2010-05-29 13:34:36 -06:00
|
|
|
ContextManager Cman("Memory.xml");
|
2010-08-20 06:10:05 -06:00
|
|
|
VersionInfo * mem;
|
2010-05-29 13:34:36 -06:00
|
|
|
do
|
2010-05-27 21:18:32 -06:00
|
|
|
{
|
2010-06-05 16:56:09 -06:00
|
|
|
// make the ContextManager update its list of Contexts
|
2010-05-29 13:34:36 -06:00
|
|
|
// by passing the pointer to 'inval', we make it export expired
|
2010-06-05 16:56:09 -06:00
|
|
|
// contexts instead of destroying them outright
|
|
|
|
// (contexts expire when the OS kills their process for whatever
|
|
|
|
// reason)
|
2010-05-29 13:34:36 -06:00
|
|
|
BadContexts inval;
|
|
|
|
Cman.Refresh(&inval);
|
|
|
|
int nCont = Cman.size();
|
|
|
|
int nInval = inval.size();
|
2010-08-13 17:25:18 -06:00
|
|
|
DFHack::Context * cont = Cman.getSingleContext();
|
|
|
|
if(cont)
|
|
|
|
{
|
|
|
|
if(cont->Attach())
|
2010-08-14 10:32:38 -06:00
|
|
|
{
|
|
|
|
DFHack::Maps * mapz = cont->getMaps();
|
|
|
|
cont->Suspend();
|
|
|
|
mapz->Start();
|
|
|
|
cont->Resume();
|
|
|
|
}
|
|
|
|
|
2010-08-13 17:25:18 -06:00
|
|
|
bool result = cont->Detach();
|
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
cerr << "Something went horribly wrong during detach" << endl;
|
|
|
|
}
|
|
|
|
}
|
2010-05-29 13:34:36 -06:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2010-05-27 21:18:32 -06:00
|
|
|
}
|
2010-05-29 13:34:36 -06:00
|
|
|
while(inputwait("ContextManager"));
|
2010-05-27 21:18:32 -06:00
|
|
|
}
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cout << "Done. Press any key to continue" << endl;
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|