From 9b6f5d2ac19873b65e5a53eec8b3720e548209ab Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 31 Mar 2011 09:45:53 -0500 Subject: [PATCH] first commit --- library/DFProcess_C.cpp | 151 +++++++++++++++++++++++++ library/include/dfhack-c/DFProcess_C.h | 112 ++++++++++++++++++ 2 files changed, 263 insertions(+) create mode 100644 library/DFProcess_C.cpp create mode 100644 library/include/dfhack-c/DFProcess_C.h diff --git a/library/DFProcess_C.cpp b/library/DFProcess_C.cpp new file mode 100644 index 000000000..ca4fe6028 --- /dev/null +++ b/library/DFProcess_C.cpp @@ -0,0 +1,151 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "dfhack/DFIntegers.h" +#include "dfhack-c/DFProcess_C.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define PTR_CHECK if(p_Ptr == NULL) return -1; + +int ProcessID_C_Compare(ProcessID_C* left, ProcessID_C* right) +{ + if(left == NULL || right == NULL) + return PROCESSID_C_NULL; + else + { + if(left->time == right->time && left->pid == right->pid) + return PROCESSID_C_EQ; + else if(left->time < right->time || left->pid < right->pid) + return PROCESSID_C_LT; + else + return PROCESSID_C_GT; + } +} + +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) \ +int Process_##func_name (DFHackObject* p_Ptr) \ +{ \ + if(p_Ptr == NULL) \ + return -1; \ + if(((DFHack::Process*)p_Ptr)->func_name()) \ + return 1; \ + else \ + return 0; \ +} + +FUNCTION_FORWARD(Attach) +FUNCTION_FORWARD(Detach) +FUNCTION_FORWARD(Suspend) +FUNCTION_FORWARD(AsyncSuspend) +FUNCTION_FORWARD(Resume) +FUNCTION_FORWARD(ForceResume) +FUNCTION_FORWARD(isSuspended) +FUNCTION_FORWARD(isAttached) +FUNCTION_FORWARD(isIdentified) +FUNCTION_FORWARD(isSnapshot) + +#define MEMREAD_FUNC_FORWARD(func_name, type) \ +int Process_##func_name(DFHackObject* p_Ptr, uint32_t address, type* value) \ +{ \ + PTR_CHECK \ + if(((DFHack::Process*)p_Ptr)->func_name(address, *value)) \ + return 1; \ + else \ + return 0;\ +} + +#define MEMWRITE_FUNC_FORWARD(func_name, type) \ +int Process_##func_name(DFHackObject* p_Ptr, uint32_t address, type value) \ +{ \ + PTR_CHECK \ + ((DFHack::Process*)p_Ptr)->func_name(address, value) \ + return 1; \ +} + +MEMREAD_FUNC_FORWARD(readQuad, uint64_t) +MEMWRITE_FUNC_FORWARD(writeQuad, uint64_t) + +MEMREAD_FUNC_FORWARD(readDWord, uint32_t) +MEMWRITE_FUNC_FORWARD(writeDWord, uint32_t) + +MEMREAD_FUNC_FORWARD(readWord, uint16_t) +MEMWRITE_FUNC_FORWARD(writeWord, uint16_t) + +MEMREAD_FUNC_FORWARD(readFloat, float) +MEMWRITE_FUNC_FORWARD(writeFloat, float) + +MEMREAD_FUNC_FORWARD(readByte, uint8_t) +MEMWRITE_FUNC_FORWARD(writeByte, uint8_t) + +int Process_getPID(DFHackObject* p_Ptr, int32_t* pid) +{ + if(p_Ptr == NULL) + { + *pid = -1; + return -1; + } + else + { + *pid = ((DFHack::Process*)p_Ptr)->getPID(); + + return 1; + } +} + +int Process_getModuleIndex(DFHackObject* p_Ptr, char* name, uint32_t version, uint32_t* output) +{ + PTR_CHECK + + if(((DFHack::Process*)p_Ptr)->getModuleIndex(name, version, *output)) + return 1; + else + return 0; +} + +int Process_SetAndWait(DFHackObject* p_Ptr, uint32_t state) +{ + PTR_CHECK + + if(((DFHack::Process*)p_Ptr)->SetAndWait(state)) + return 1; + else + return 0; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/library/include/dfhack-c/DFProcess_C.h b/library/include/dfhack-c/DFProcess_C.h new file mode 100644 index 000000000..c9d40d757 --- /dev/null +++ b/library/include/dfhack-c/DFProcess_C.h @@ -0,0 +1,112 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef PROCESS_C_API +#define PROCESS_C_API + +#include "DFHack_C.h" +#include "dfhack/DFProcess.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct ProcessID_C +{ + uint64_t time; + uint64_t pid; +}; + +#define PROCESSID_C_NULL -2 +#define PROCESSID_C_LT -1 +#define PROCESSID_C_EQ 0 +#define PROCESSID_C_GT 1 + +DFHACK_EXPORT extern int ProcessID_C_Compare(ProcessID_C* left, ProcessID_C* right); + +struct MemRange_C +{ + 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_Attach(DFHackObject* p_Ptr); +DFHACK_EXPORT int Process_Detach(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_writeQuad(DFHackObject* p_Ptr, uint32_t address, uint64_t value); + +DFHACK_EXPORT int Process_readDWord(DFHackObject* p_Ptr, uint32_t address, uint32_t* value); +DFHACK_EXPORT int Process_writeDWord(DFHackObject* p_Ptr, uint32_t address, uint32_t value); + +DFHACK_EXPORT int Process_readWord(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_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_writeByte(DFHackObject* p_Ptr, uint32_t address, uint8_t value); + +DFHACK_EXPORT int Process_isSuspended(DFHackObject* p_Ptr); +DFHACK_EXPORT int Process_isAttached(DFHackObject* p_Ptr); +DFHACK_EXPORT int Process_isIdentified(DFHackObject* p_Ptr); +DFHACK_EXPORT int Process_isSnapshot(DFHackObject* p_Ptr); + +DFHACK_EXPORT uint32_t* Process_getThreadIDs(DFHackObject* p_Ptr); +DFHACK_EXPORT int 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_SetAndWait(DFHackObject* p_Ptr, uint32_t state); + +#ifdef __cplusplus +} +#endif + +#endif