dfhack/plugins/devel/memview.cpp

195 lines
4.1 KiB
C++

2011-08-22 07:21:13 -06:00
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/PluginManager.h>
#include <dfhack/Process.h>
#include <dfhack/extra/stopwatch.h>
#include <../depends/tthread/tinythread.h> //not sure if correct
2011-08-22 07:21:13 -06:00
#include <string>
#include <vector>
#include <sstream>
using std::vector;
using std::string;
using namespace DFHack;
uint64_t timeLast=0;
static tthread::mutex* mymutex=0;
struct memory_data
{
size_t addr;
size_t len;
size_t refresh;
int state;
uint8_t *buf,*lbuf;
vector<t_memrange> ranges;
}memdata;
enum HEXVIEW_STATES
{
STATE_OFF,STATE_ON
};
2011-08-22 07:21:13 -06:00
DFhackCExport command_result memview (Core * c, vector <string> & parameters);
DFhackCExport const char * plugin_name ( void )
{
return "memview";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("memview","Shows memory in real time. Params: adrr length refresh_rate. If addr==0 then stop viewing",memview));
memdata.state=STATE_OFF;
mymutex=new tthread::mutex;
2011-08-22 07:21:13 -06:00
return CR_OK;
}
size_t convert(const std::string& p,bool ishex=false)
{
size_t ret;
std::stringstream conv;
if(ishex)
conv<<std::hex;
conv<<p;
conv>>ret;
return ret;
}
bool isAddr(uint32_t *trg,vector<t_memrange> & ranges)
{
if(trg[0]%4==0)
for(size_t i=0;i<ranges.size();i++)
if(ranges[i].isInRange(trg[0]))
return true;
return false;
}
2011-08-24 14:34:49 -06:00
void outputHex(uint8_t *buf,uint8_t *lbuf,size_t len,size_t start,Core *c,vector<t_memrange> & ranges)
{
Console &con=c->con;
const size_t page_size=16;
2011-08-22 07:21:13 -06:00
con.clear();
2011-08-24 14:34:49 -06:00
2011-08-22 07:21:13 -06:00
for(size_t i=0;i<len;i+=page_size)
2011-08-24 14:34:49 -06:00
{
con.gotoxy(1,i/page_size+1);
con.print("0x%08X ",i+start);
2011-08-22 07:21:13 -06:00
for(size_t j=0;(j<page_size) && (i+j<len);j++)
{
if(j%4==0)
{
2011-08-24 14:34:49 -06:00
con.reset_color();
2011-08-22 07:21:13 -06:00
if(isAddr((uint32_t *)(buf+j+i),ranges))
2011-08-24 14:34:49 -06:00
con.color(Console::COLOR_LIGHTRED); //coloring in the middle does not work
//TODO make something better?
2011-08-22 07:21:13 -06:00
}
if(lbuf[j+i]!=buf[j+i])
2011-08-24 14:34:49 -06:00
con.print("*%02X",buf[j+i]); //if modfied show a star
2011-08-22 07:21:13 -06:00
else
2011-08-24 14:34:49 -06:00
con.print(" %02X",buf[j+i]);
2011-08-22 07:21:13 -06:00
}
con.reset_color();
con.print(" | ");
for(size_t j=0;(j<page_size) && (i+j<len);j++)
2011-08-24 14:34:49 -06:00
if((buf[j+i]>31)&&(buf[j+i]<128)) //only printable ascii
2011-08-22 07:21:13 -06:00
con.print("%c",buf[j+i]);
else
con.print(".");
2011-08-24 14:34:49 -06:00
//con.print("\n");
}
con.print("\n");
con.flush();
2011-08-22 07:21:13 -06:00
}
void Deinit()
{
if(memdata.state==STATE_ON)
{
memdata.state=STATE_OFF;
delete [] memdata.buf;
delete [] memdata.lbuf;
}
}
DFhackCExport command_result plugin_onupdate ( Core * c )
{
2011-08-24 14:34:49 -06:00
mymutex->lock();
if(memdata.state==STATE_OFF)
{
mymutex->unlock();
return CR_OK;
}
2011-08-24 14:34:49 -06:00
//Console &con=c->con;
uint64_t time2 = GetTimeMs64();
uint64_t delta = time2-timeLast;
2011-08-24 14:34:49 -06:00
if(memdata.refresh!=0)
if(delta<memdata.refresh)
{
mymutex->unlock();
return CR_OK;
}
timeLast = time2;
c->p->read(memdata.addr,memdata.len,memdata.buf);
2011-08-24 14:34:49 -06:00
outputHex(memdata.buf,memdata.lbuf,memdata.len,memdata.addr,c,memdata.ranges);
memcpy(memdata.lbuf, memdata.buf, memdata.len);
if(memdata.refresh==0)
Deinit();
mymutex->unlock();
return CR_OK;
}
2011-08-22 07:21:13 -06:00
DFhackCExport command_result memview (Core * c, vector <string> & parameters)
{
mymutex->lock();
c->p->getMemRanges(memdata.ranges);
memdata.addr=convert(parameters[0],true);
if(memdata.addr==0)
{
2011-08-24 14:34:49 -06:00
Deinit();
memdata.state=STATE_OFF;
mymutex->unlock();
return CR_OK;
}
else
{
Deinit();
bool isValid=false;
for(size_t i=0;i<memdata.ranges.size();i++)
if(memdata.ranges[i].isInRange(memdata.addr))
isValid=true;
if(!isValid)
{
c->con.printerr("Invalid address:%x\n",memdata.addr);
mymutex->unlock();
return CR_OK;
}
2011-08-24 14:34:49 -06:00
memdata.state=STATE_ON;
}
2011-08-22 07:21:13 -06:00
if(parameters.size()>1)
memdata.len=convert(parameters[1]);
2011-08-22 07:21:13 -06:00
else
memdata.len=20*16;
2011-08-22 07:21:13 -06:00
if(parameters.size()>2)
memdata.refresh=convert(parameters[2]);
2011-08-22 07:21:13 -06:00
else
memdata.refresh=0;
2011-08-24 14:34:49 -06:00
2011-08-22 07:21:13 -06:00
uint8_t *buf,*lbuf;
memdata.buf=new uint8_t[memdata.len];
memdata.lbuf=new uint8_t[memdata.len];
c->p->getMemRanges(memdata.ranges);
mymutex->unlock();
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
2011-08-24 14:34:49 -06:00
mymutex->lock();
Deinit();
delete mymutex;
2011-08-24 14:34:49 -06:00
mymutex->unlock();
return CR_OK;
2011-08-22 07:21:13 -06:00
}