2011-07-17 04:05:14 -06:00
|
|
|
// Lists embeded STL vectors and pointers to STL vectors found in the given
|
|
|
|
// memory range.
|
|
|
|
//
|
2016-04-03 18:47:21 -06:00
|
|
|
// Linux and OS X only
|
2011-07-17 04:05:14 -06:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
#include "Core.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "VersionInfo.h"
|
|
|
|
|
2011-07-17 04:05:14 -06:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
struct t_vecTriplet
|
|
|
|
{
|
2012-01-03 17:45:11 -07:00
|
|
|
void * start;
|
|
|
|
void * end;
|
|
|
|
void * alloc_end;
|
2011-07-17 04:05:14 -06:00
|
|
|
};
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
command_result df_vectors (color_ostream &out, vector <string> & parameters);
|
|
|
|
command_result df_clearvec (color_ostream &out, vector <string> & parameters);
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2012-02-21 10:19:17 -07:00
|
|
|
DFHACK_PLUGIN("vectors");
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
commands.push_back(PluginCommand("vectors",
|
|
|
|
"Scan memory for vectors.\
|
|
|
|
\n 1st param: start of scan\
|
|
|
|
\n 2nd param: number of bytes to scan",
|
|
|
|
df_vectors));
|
2011-07-19 02:17:11 -06:00
|
|
|
|
|
|
|
commands.push_back(PluginCommand("clearvec",
|
|
|
|
"Set list of vectors to zero length",
|
|
|
|
df_clearvec));
|
|
|
|
return CR_OK;
|
2011-07-17 04:05:14 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
static bool hexOrDec(string &str, uintptr_t &value)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
if (str.find("0x") == 0 && sscanf(str.c_str(), "%x", &value) == 1)
|
|
|
|
return true;
|
|
|
|
else if (sscanf(str.c_str(), "%u", &value) == 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
static bool mightBeVec(vector<t_memrange> &ranges,
|
2011-07-17 04:05:14 -06:00
|
|
|
t_vecTriplet *vec)
|
|
|
|
{
|
|
|
|
if ((vec->start > vec->end) || (vec->end > vec->alloc_end))
|
|
|
|
return false;
|
|
|
|
|
2011-07-21 03:19:23 -06:00
|
|
|
// Vector length might not be a multiple of 4 if, for example,
|
|
|
|
// it's a vector of uint8_t or uint16_t. However, the actual memory
|
|
|
|
// allocated to the vector should be 4 byte aligned.
|
2016-07-28 09:17:55 -06:00
|
|
|
if (((intptr_t)vec->start % 4 != 0) || ((intptr_t)vec->alloc_end % 4 != 0))
|
2011-07-17 04:05:14 -06:00
|
|
|
return false;
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
for (size_t i = 0; i < ranges.size(); i++)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
2016-04-03 18:47:21 -06:00
|
|
|
t_memrange &range = ranges[i];
|
2011-07-17 04:05:14 -06:00
|
|
|
|
|
|
|
if (range.isInRange(vec->start) && range.isInRange(vec->alloc_end))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-03 17:45:11 -07:00
|
|
|
static bool inAnyRange(vector<t_memrange> &ranges, void * ptr)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ranges.size(); i++)
|
|
|
|
{
|
|
|
|
if (ranges[i].isInRange(ptr))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
static bool getRanges(color_ostream &out, std::vector<t_memrange> &out_ranges)
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
std::vector<t_memrange> ranges;
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
Core::getInstance().p->getMemRanges(ranges);
|
2011-07-19 02:17:11 -06:00
|
|
|
|
|
|
|
for (size_t i = 0; i < ranges.size(); i++)
|
|
|
|
{
|
|
|
|
t_memrange &range = ranges[i];
|
|
|
|
|
|
|
|
if (range.read && range.write && !range.shared)
|
2016-04-03 18:47:21 -06:00
|
|
|
out_ranges.push_back(range);
|
2011-07-19 02:17:11 -06:00
|
|
|
}
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (out_ranges.empty())
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
2016-04-03 18:47:21 -06:00
|
|
|
out << "No readable segments." << std::endl;
|
2011-07-19 02:17:11 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
// COMMAND: vectors
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
static void vectorsUsage(color_ostream &con)
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
con << "Usage: vectors <start of scan address> <# bytes to scan>"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
static void printVec(color_ostream &con, const char* msg, t_vecTriplet *vec,
|
2016-04-03 18:47:21 -06:00
|
|
|
uint32_t start, uint32_t pos, std::vector<t_memrange> &ranges)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
2016-07-28 09:17:55 -06:00
|
|
|
uint32_t length = (intptr_t)vec->end - (intptr_t)vec->start;
|
2011-07-17 04:05:14 -06:00
|
|
|
uint32_t offset = pos - start;
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
con.print("%8s offset %06p, addr %010p, start %010p, length %u",
|
2011-07-17 04:05:14 -06:00
|
|
|
msg, offset, pos, vec->start, length);
|
2016-04-03 18:47:21 -06:00
|
|
|
if (length >= 4 && length % 4 == 0)
|
|
|
|
{
|
|
|
|
void *ptr = vec->start;
|
|
|
|
for (int level = 0; level < 2; level++)
|
|
|
|
{
|
|
|
|
if (inAnyRange(ranges, ptr))
|
|
|
|
ptr = *(void**)ptr;
|
|
|
|
}
|
|
|
|
std::string classname;
|
|
|
|
if (Core::getInstance().vinfo->getVTableName(ptr, classname))
|
|
|
|
con.print(", 1st item: %s", classname.c_str());
|
|
|
|
}
|
|
|
|
con.print("\n");
|
2011-07-17 04:05:14 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
command_result df_vectors (color_ostream &con, vector <string> & parameters)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
if (parameters.size() != 2)
|
|
|
|
{
|
2011-07-19 02:17:11 -06:00
|
|
|
vectorsUsage(con);
|
2011-07-17 04:05:14 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
uintptr_t start = 0, bytes = 0;
|
2011-07-17 04:05:14 -06:00
|
|
|
|
|
|
|
if (!hexOrDec(parameters[0], start))
|
|
|
|
{
|
2011-07-19 02:17:11 -06:00
|
|
|
vectorsUsage(con);
|
2011-07-17 04:05:14 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2011-07-17 04:05:14 -06:00
|
|
|
if (!hexOrDec(parameters[1], bytes))
|
|
|
|
{
|
2011-07-19 02:17:11 -06:00
|
|
|
vectorsUsage(con);
|
2011-07-17 04:05:14 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
uintptr_t end = start + bytes;
|
2011-07-17 04:05:14 -06:00
|
|
|
|
|
|
|
// 4 byte alignment.
|
|
|
|
while (start % 4 != 0)
|
|
|
|
start++;
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
CoreSuspender suspend;
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
std::vector<t_memrange> ranges;
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (!getRanges(con, ranges))
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
2011-07-19 02:17:11 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2011-07-19 02:17:11 -06:00
|
|
|
bool startInRange = false;
|
2016-04-03 18:47:21 -06:00
|
|
|
for (size_t i = 0; i < ranges.size(); i++)
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
2016-04-03 18:47:21 -06:00
|
|
|
t_memrange &range = ranges[i];
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2012-01-03 17:45:11 -07:00
|
|
|
if (!range.isInRange((void *)start))
|
2011-07-17 04:05:14 -06:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Found the range containing the start
|
2012-01-03 17:45:11 -07:00
|
|
|
if (!range.isInRange((void *)end))
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
con.print("Scanning %u bytes would read past end of memory "
|
|
|
|
"range.\n", bytes);
|
2016-07-28 09:17:55 -06:00
|
|
|
size_t diff = end - (intptr_t)range.end;
|
2011-07-17 04:05:14 -06:00
|
|
|
con.print("Cutting bytes down by %u.\n", diff);
|
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
end = (uintptr_t) range.end;
|
2011-07-17 04:05:14 -06:00
|
|
|
}
|
|
|
|
startInRange = true;
|
2011-07-19 02:17:11 -06:00
|
|
|
break;
|
2011-07-17 04:05:14 -06:00
|
|
|
} // for (size_t i = 0; i < ranges.size(); i++)
|
|
|
|
|
|
|
|
if (!startInRange)
|
|
|
|
{
|
|
|
|
con << "Address not in any memory range." << std::endl;
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
uintptr_t pos = start;
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
const size_t ptr_size = sizeof(void*);
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
for (uintptr_t pos = start; pos < end; pos += ptr_size)
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
// Is it an embeded vector?
|
|
|
|
if (pos <= ( end - sizeof(t_vecTriplet) ))
|
|
|
|
{
|
|
|
|
t_vecTriplet* vec = (t_vecTriplet*) pos;
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (mightBeVec(ranges, vec))
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
2016-04-03 18:47:21 -06:00
|
|
|
printVec(con, "VEC:", vec, start, pos, ranges);
|
2011-07-17 04:05:14 -06:00
|
|
|
// Skip over rest of vector.
|
|
|
|
pos += sizeof(t_vecTriplet) - ptr_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a vector pointer?
|
|
|
|
if (pos <= (end - ptr_size))
|
|
|
|
{
|
2016-07-28 09:17:55 -06:00
|
|
|
uintptr_t ptr = * ( (uintptr_t*) pos);
|
2011-07-17 04:05:14 -06:00
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (inAnyRange(ranges, (void *) ptr))
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
|
|
|
t_vecTriplet* vec = (t_vecTriplet*) ptr;
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (mightBeVec(ranges, vec))
|
2011-07-17 04:05:14 -06:00
|
|
|
{
|
2016-04-03 18:47:21 -06:00
|
|
|
printVec(con, "VEC PTR:", vec, start, pos, ranges);
|
2011-07-17 04:05:14 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // for (uint32_t pos = start; pos < end; pos += ptr_size)
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2011-07-19 02:17:11 -06:00
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
// COMMAND: clearvec
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
static void clearUsage(color_ostream &con)
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
con << "Usage: clearvec <vector1 addr> [vector2 addr] ..." << std::endl;
|
|
|
|
con << "Address can be either for vector or pointer to vector."
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
command_result df_clearvec (color_ostream &con, vector <string> & parameters)
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
if (parameters.size() == 0)
|
|
|
|
{
|
|
|
|
clearUsage(con);
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-07-28 09:17:55 -06:00
|
|
|
uintptr_t start = 0, bytes = 0;
|
2011-07-19 02:17:11 -06:00
|
|
|
|
|
|
|
if (!hexOrDec(parameters[0], start))
|
|
|
|
{
|
|
|
|
vectorsUsage(con);
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2011-07-19 02:17:11 -06:00
|
|
|
if (!hexOrDec(parameters[1], bytes))
|
|
|
|
{
|
|
|
|
vectorsUsage(con);
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-10 06:25:00 -07:00
|
|
|
CoreSuspender suspend;
|
2011-07-19 02:17:11 -06:00
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
std::vector<t_memrange> ranges;
|
2011-07-19 02:17:11 -06:00
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (!getRanges(con, ranges))
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < parameters.size(); i++)
|
|
|
|
{
|
|
|
|
std::string addr_str = parameters[i];
|
2016-07-28 09:17:55 -06:00
|
|
|
uintptr_t addr;
|
2011-07-19 02:17:11 -06:00
|
|
|
if (!hexOrDec(addr_str, addr))
|
|
|
|
{
|
|
|
|
con << "'" << addr_str << "' not a number." << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr % 4 != 0)
|
|
|
|
{
|
|
|
|
con << addr_str << " not 4 byte aligned." << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (!inAnyRange(ranges, (void *) addr))
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
con << addr_str << " not in any valid address range." << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valid = false;
|
|
|
|
bool ptr = false;
|
|
|
|
t_vecTriplet* vec = (t_vecTriplet*) addr;
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (mightBeVec(ranges, vec))
|
2011-07-19 02:17:11 -06:00
|
|
|
valid = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Is it a pointer to a vector?
|
2016-07-28 09:17:55 -06:00
|
|
|
addr = * ( (uintptr_t*) addr);
|
2011-07-19 02:17:11 -06:00
|
|
|
vec = (t_vecTriplet*) addr;
|
|
|
|
|
2016-04-03 18:47:21 -06:00
|
|
|
if (inAnyRange(ranges, (void *) addr) && mightBeVec(ranges, vec))
|
2011-07-19 02:17:11 -06:00
|
|
|
{
|
|
|
|
valid = true;
|
|
|
|
ptr = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid)
|
|
|
|
{
|
|
|
|
con << addr_str << " is not a vector." << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec->end = vec->start;
|
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
con << "Vector pointer ";
|
|
|
|
else
|
|
|
|
con << "Vector ";
|
|
|
|
|
|
|
|
con << addr_str << " set to zero length." << std::endl;
|
|
|
|
} // for (size_t i = 0; i < parameters.size(); i++)
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|