removed unneeded memrange wrapper struct

renamed some stuff to match the C++ version
added read, write, readSTLVector, readString, getPath, getMemranges, and getPID
develop
doomchild 2011-03-31 13:46:50 -05:00
parent fea7d56c32
commit 5e67ee2970
2 changed files with 201 additions and 59 deletions

@ -22,7 +22,14 @@ must not be misrepresented as being the original software.
distribution. distribution.
*/ */
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#include "dfhack/DFIntegers.h" #include "dfhack/DFIntegers.h"
#include "dfhack-c/DFTypes_C.h"
#include "dfhack-c/DFProcess_C.h" #include "dfhack-c/DFProcess_C.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -31,7 +38,7 @@ extern "C" {
#define PTR_CHECK if(p_Ptr == NULL) return -1; #define PTR_CHECK if(p_Ptr == NULL) return -1;
int ProcessID_C_Compare(ProcessID_C* left, ProcessID_C* right) int C_ProcessID_Compare(c_processID* left, c_processID* right)
{ {
if(left == NULL || right == NULL) if(left == NULL || right == NULL)
return PROCESSID_C_NULL; return PROCESSID_C_NULL;
@ -46,16 +53,6 @@ int ProcessID_C_Compare(ProcessID_C* left, ProcessID_C* right)
} }
} }
int MemRange_C_IsInRange(MemRange_C* range, uint64_t address)
{
if(range == NULL)
return -1;
else if(address >= range->start && address <= range->end)
return 1;
else
return 0;
}
#define FUNCTION_FORWARD(func_name) \ #define FUNCTION_FORWARD(func_name) \
int Process_##func_name (DFHackObject* p_Ptr) \ int Process_##func_name (DFHackObject* p_Ptr) \
{ \ { \
@ -67,12 +64,12 @@ int Process_##func_name (DFHackObject* p_Ptr) \
return 0; \ return 0; \
} }
FUNCTION_FORWARD(Attach) FUNCTION_FORWARD(attach)
FUNCTION_FORWARD(Detach) FUNCTION_FORWARD(detach)
FUNCTION_FORWARD(Suspend) FUNCTION_FORWARD(suspend)
FUNCTION_FORWARD(AsyncSuspend) FUNCTION_FORWARD(asyncSuspend)
FUNCTION_FORWARD(Resume) FUNCTION_FORWARD(resume)
FUNCTION_FORWARD(ForceResume) FUNCTION_FORWARD(forceresume)
FUNCTION_FORWARD(isSuspended) FUNCTION_FORWARD(isSuspended)
FUNCTION_FORWARD(isAttached) FUNCTION_FORWARD(isAttached)
FUNCTION_FORWARD(isIdentified) FUNCTION_FORWARD(isIdentified)
@ -82,17 +79,15 @@ FUNCTION_FORWARD(isSnapshot)
int Process_##func_name(DFHackObject* p_Ptr, uint32_t address, type* value) \ int Process_##func_name(DFHackObject* p_Ptr, uint32_t address, type* value) \
{ \ { \
PTR_CHECK \ PTR_CHECK \
if(((DFHack::Process*)p_Ptr)->func_name(address, *value)) \ ((DFHack::Process*)p_Ptr)->func_name(address, *value); \
return 1; \ return 1; \
else \
return 0;\
} }
#define MEMWRITE_FUNC_FORWARD(func_name, type) \ #define MEMWRITE_FUNC_FORWARD(func_name, type) \
int Process_##func_name(DFHackObject* p_Ptr, uint32_t address, type value) \ int Process_##func_name(DFHackObject* p_Ptr, uint32_t address, type value) \
{ \ { \
PTR_CHECK \ PTR_CHECK \
((DFHack::Process*)p_Ptr)->func_name(address, value) \ ((DFHack::Process*)p_Ptr)->func_name(address, value); \
return 1; \ return 1; \
} }
@ -106,11 +101,175 @@ MEMREAD_FUNC_FORWARD(readWord, uint16_t)
MEMWRITE_FUNC_FORWARD(writeWord, uint16_t) MEMWRITE_FUNC_FORWARD(writeWord, uint16_t)
MEMREAD_FUNC_FORWARD(readFloat, float) MEMREAD_FUNC_FORWARD(readFloat, float)
MEMWRITE_FUNC_FORWARD(writeFloat, float)
MEMREAD_FUNC_FORWARD(readByte, uint8_t) MEMREAD_FUNC_FORWARD(readByte, uint8_t)
MEMWRITE_FUNC_FORWARD(writeByte, uint8_t) MEMWRITE_FUNC_FORWARD(writeByte, uint8_t)
MEMREAD_FUNC_FORWARD(readSTLVector, t_vecTriplet);
uint8_t* Process_read(DFHackObject* p_Ptr, uint32_t address, uint32_t length)
{
if(p_Ptr == NULL || alloc_ubyte_buffer_callback == NULL)
return NULL;
uint8_t* buf;
((*alloc_ubyte_buffer_callback)(&buf, length));
if(buf == NULL)
return NULL;
((DFHack::Process*)p_Ptr)->read(address, length, buf);
return buf;
}
void Process_write(DFHackObject* p_Ptr, uint32_t address, uint32_t length, uint8_t* buffer)
{
if(p_Ptr == NULL || buffer == NULL)
return;
((DFHack::Process*)p_Ptr)->write(address, length, buffer);
}
char* Process_readString(DFHackObject* p_Ptr, uint32_t offset)
{
if(p_Ptr == NULL || alloc_char_buffer_callback == NULL)
return NULL;
std::string pString = ((DFHack::Process*)p_Ptr)->readSTLString(offset);
if(pString.length() > 0)
{
size_t length = pString.length();
char* buf;
//add 1 for the null terminator
((*alloc_char_buffer_callback)(&buf, length + 1));
if(buf == NULL)
return NULL;
memset(buf, '\n', length + 1);
pString.copy(buf, length);
return buf;
}
else
return "";
}
char* Process_getPath(DFHackObject* p_Ptr)
{
if(p_Ptr == NULL || alloc_char_buffer_callback == NULL)
return NULL;
std::string pString = ((DFHack::Process*)p_Ptr)->getPath();
if(pString.length() > 0)
{
size_t length = pString.length();
char* buf;
//add 1 for the null terminator
((*alloc_char_buffer_callback)(&buf, length + 1));
if(buf == NULL)
return NULL;
memset(buf, '\0', length + 1);
pString.copy(buf, length);
return buf;
}
else
return "";
}
uint32_t* Process_getThreadIDs(DFHackObject* p_Ptr)
{
if(p_Ptr == NULL || alloc_uint_buffer_callback == NULL)
return NULL;
std::vector<uint32_t> threads;
if(((DFHack::Process*)p_Ptr)->getThreadIDs(threads))
{
uint32_t* buf;
if(threads.size() > 0)
{
((*alloc_uint_buffer_callback)(&buf, threads.size()));
if(buf == NULL)
return NULL;
copy(threads.begin(), threads.end(), buf);
return buf;
}
}
else
return NULL;
}
t_memrange* Process_getMemRanges(DFHackObject* p_Ptr)
{
if(p_Ptr == NULL || alloc_memrange_buffer_callback == NULL)
return NULL;
std::vector<t_memrange> ranges;
((DFHack::Process*)p_Ptr)->getMemRanges(ranges);
if(ranges.size() > 0)
{
t_memrange* buf;
uint32_t* rangeDescriptorBuf = (uint32_t*)calloc(ranges.size(), sizeof(uint32_t));
for(uint32_t i = 0; i < ranges.size(); i++)
{
t_memrange* r = &ranges[i];
rangeDescriptorBuf[i] = (uint32_t)(r->end - r->start);
}
((*alloc_memrange_buffer_callback)(&buf, rangeDescriptorBuf, ranges.size()));
free(rangeDescriptorBuf);
if(buf == NULL)
return NULL;
for(uint32_t i = 0; i < ranges.size(); i++)
{
t_memrange* r = &ranges[i];
buf[i].start = r->start;
buf[i].end = r->end;
memset(buf[i].name, '\0', 1024);
strncpy(buf[i].name, r->name, 1024);
buf[i].read = r->read;
buf[i].write = r->write;
buf[i].execute = r->execute;
buf[i].shared = r->shared;
buf[i].valid = r->valid;
memcpy(buf[i].buffer, r->buffer, r->end - r->start);
}
return buf;
}
else
return NULL;
}
int Process_getPID(DFHackObject* p_Ptr, int32_t* pid) int Process_getPID(DFHackObject* p_Ptr, int32_t* pid)
{ {
if(p_Ptr == NULL) if(p_Ptr == NULL)

@ -32,7 +32,7 @@ distribution.
extern "C" { extern "C" {
#endif #endif
struct ProcessID_C struct c_processID
{ {
uint64_t time; uint64_t time;
uint64_t pid; uint64_t pid;
@ -43,40 +43,15 @@ struct ProcessID_C
#define PROCESSID_C_EQ 0 #define PROCESSID_C_EQ 0
#define PROCESSID_C_GT 1 #define PROCESSID_C_GT 1
DFHACK_EXPORT extern int ProcessID_C_Compare(ProcessID_C* left, ProcessID_C* right); DFHACK_EXPORT extern int C_ProcessID_Compare(c_processID* left, c_processID* right);
struct MemRange_C DFHACK_EXPORT int Process_attach(DFHackObject* p_Ptr);
{ DFHACK_EXPORT int Process_detach(DFHackObject* p_Ptr);
uint64_t start;
uint64_t end;
char name[1024];
uint8_t flags;
uint8_t* buffer;
};
#define MEMRANGE_CAN_READ(x) (x.flags & 0x01)
#define MEMRANGE_CAN_WRITE(x) (x.flags & 0x02)
#define MEMRANGE_CAN_EXECUTE(x) (x.flags & 0x04)
#define MEMRANGE_IS_SHARED(x) (x.flags & 0x08)
#define MEMRANGE_IS_VALID(x) (x.flags & 0x10)
#define MEMRANGE_SET_READ(x, i) (x.flags |= 0x01)
#define MEMRANGE_SET_WRITE(x, i) (x.flags |= 0x02)
#define MEMRANGE_SET_EXECUTE(x, i) (x.flags |= 0x04)
#define MEMRANGE_SET_SHARED(x, i) (x.flags |= 0x08)
#define MEMRANGE_SET_VALID(x, i) (x.flags |= 0x10)
DFHACK_EXPORT extern int MemRange_C_IsInRange(MemRance_C* range, uint64_t address); DFHACK_EXPORT int Process_suspend(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_asyncSuspend(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_Attach(DFHackObject* p_Ptr); DFHACK_EXPORT int Process_resume(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_Detach(DFHackObject* p_Ptr); DFHACK_EXPORT int Process_forceresume(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_Suspend(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_AsyncSuspend(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_Resume(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_ForceResume(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_readQuad(DFHackObject* p_Ptr, uint32_t address, uint64_t* value); DFHACK_EXPORT int Process_readQuad(DFHackObject* p_Ptr, uint32_t address, uint64_t* value);
DFHACK_EXPORT int Process_writeQuad(DFHackObject* p_Ptr, uint32_t address, uint64_t value); DFHACK_EXPORT int Process_writeQuad(DFHackObject* p_Ptr, uint32_t address, uint64_t value);
@ -88,18 +63,26 @@ DFHACK_EXPORT int Process_readWord(DFHackObject* p_Ptr, uint32_t address, uint16
DFHACK_EXPORT int Process_writeWord(DFHackObject* p_Ptr, uint32_t address, uint16_t value); DFHACK_EXPORT int Process_writeWord(DFHackObject* p_Ptr, uint32_t address, uint16_t value);
DFHACK_EXPORT int Process_readFloat(DFHackObject* p_Ptr, uint32_t address, float* value); DFHACK_EXPORT int Process_readFloat(DFHackObject* p_Ptr, uint32_t address, float* value);
DFHACK_EXPORT int Process_writeFloat(DFHackObject* p_Ptr, uint32_t address, float value);
DFHACK_EXPORT int Process_readByte(DFHackObject* p_Ptr, uint32_t address, uint8_t* value); DFHACK_EXPORT int Process_readByte(DFHackObject* p_Ptr, uint32_t address, uint8_t* value);
DFHACK_EXPORT int Process_writeByte(DFHackObject* p_Ptr, uint32_t address, uint8_t value); DFHACK_EXPORT int Process_writeByte(DFHackObject* p_Ptr, uint32_t address, uint8_t value);
DFHACK_EXPORT uint8_t* Process_read(DFHackObject* p_Ptr, uint32_t address, uint32_t length);
DFHACK_EXPORT void Process_write(DFHackObject* p_Ptr, uint32_t address, uint32_t length, uint8_t* buffer);
DFHACK_EXPORT int Process_readSTLVector(DFHackObject* p_Ptr, uint32_t address, t_vecTriplet* vector);
DFHACK_EXPORT char* Process_readString(DFHackObject* p_Ptr, uint32_t offset);
DFHACK_EXPORT char* Process_getPath(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_isSuspended(DFHackObject* p_Ptr); DFHACK_EXPORT int Process_isSuspended(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_isAttached(DFHackObject* p_Ptr); DFHACK_EXPORT int Process_isAttached(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_isIdentified(DFHackObject* p_Ptr); DFHACK_EXPORT int Process_isIdentified(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_isSnapshot(DFHackObject* p_Ptr); DFHACK_EXPORT int Process_isSnapshot(DFHackObject* p_Ptr);
DFHACK_EXPORT uint32_t* Process_getThreadIDs(DFHackObject* p_Ptr); DFHACK_EXPORT uint32_t* Process_getThreadIDs(DFHackObject* p_Ptr);
DFHACK_EXPORT int getPID(DFHackObject* p_Ptr, int32_t* pid); DFHACK_EXPORT t_memrange* Process_getMemRanges(DFHackObject* p_Ptr);
DFHACK_EXPORT int Process_getPID(DFHackObject* p_Ptr, int32_t* pid);
DFHACK_EXPORT int Process_getModuleIndex(DFHackObject* p_Ptr, char* name, uint32_t version, uint32_t* output); DFHACK_EXPORT int Process_getModuleIndex(DFHackObject* p_Ptr, char* name, uint32_t version, uint32_t* output);