dfhack/library/unmaintained/LinuxMemAccess-ptrace.h

182 lines
4.2 KiB
C

2009-09-13 18:02:46 -06:00
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
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.
*/
/**
* DO NOT USE THIS FILE DIRECTLY! USE MemAccess.h INSTEAD!
*/
2009-10-23 12:07:38 -06:00
#include "integers.h"
2009-09-13 18:02:46 -06:00
inline
uint8_t MreadByte (const uint32_t &offset)
{
2009-10-28 16:40:21 -06:00
return ptrace(PTRACE_PEEKDATA,g_ProcessHandle, offset, NULL);
2009-09-13 18:02:46 -06:00
}
inline
void MreadByte (const uint32_t &offset, uint8_t &val )
{
2009-10-28 16:40:21 -06:00
val = ptrace(PTRACE_PEEKDATA,g_ProcessHandle, offset, NULL);
2009-09-13 18:02:46 -06:00
}
inline
uint16_t MreadWord (const uint32_t &offset)
{
2009-10-28 16:40:21 -06:00
return ptrace(PTRACE_PEEKDATA,g_ProcessHandle, offset, NULL);
2009-09-13 18:02:46 -06:00
}
inline
void MreadWord (const uint32_t &offset, uint16_t &val)
{
2009-10-28 16:40:21 -06:00
val = ptrace(PTRACE_PEEKDATA,g_ProcessHandle, offset, NULL);
2009-09-13 18:02:46 -06:00
}
inline
uint32_t MreadDWord (const uint32_t &offset)
{
2009-10-28 16:40:21 -06:00
return ptrace(PTRACE_PEEKDATA,g_ProcessHandle, offset, NULL);
2009-09-13 18:02:46 -06:00
}
inline
void MreadDWord (const uint32_t &offset, uint32_t &val)
{
2009-10-28 16:40:21 -06:00
val = ptrace(PTRACE_PEEKDATA,g_ProcessHandle, offset, NULL);
2009-09-13 18:02:46 -06:00
}
2009-10-28 16:40:21 -06:00
// extremely terrible braindamage
2009-09-13 18:02:46 -06:00
inline
2009-10-28 16:40:21 -06:00
bool Mread ( uint32_t offset, uint32_t size, uint8_t *target)
2009-09-13 18:02:46 -06:00
{
2009-10-28 16:40:21 -06:00
uint8_t *mover = target;
while (size)
{
if(size >= 4)
{
* (uint32_t *)mover = MreadDWord(offset);
mover+=4;
offset +=4;
size -=4;
}
else if(size >= 2)
{
* (uint16_t *)mover = MreadWord(offset);
mover+=2;
offset +=2;
size -=2;
}
else if(size == 1)
{
* (uint8_t *)mover = MreadByte(offset);
mover+=1;
offset ++;
size --;
}
}
return true;
2009-09-13 18:02:46 -06:00
}
2009-10-29 13:22:12 -06:00
/*
* WRITING
*/
2009-09-13 18:02:46 -06:00
inline
void MwriteDWord (uint32_t offset, uint32_t data)
{
ptrace(PTRACE_POKEDATA,g_ProcessHandle, offset, data);
}
// using these is expensive.
inline
void MwriteWord (uint32_t offset, uint16_t data)
{
uint32_t orig = MreadDWord(offset);
2009-10-29 13:22:12 -06:00
orig &= 0xFFFF0000;
orig |= data;
/*
2009-09-13 18:02:46 -06:00
orig |= 0x0000FFFF;
orig &= data;
2009-10-29 13:22:12 -06:00
*/
2009-09-13 18:02:46 -06:00
ptrace(PTRACE_POKEDATA,g_ProcessHandle, offset, orig);
}
inline
void MwriteByte (uint32_t offset, uint8_t data)
{
uint32_t orig = MreadDWord(offset);
2009-10-29 13:22:12 -06:00
orig &= 0xFFFFFF00;
orig |= data;
/*
2009-09-13 18:02:46 -06:00
orig |= 0x000000FF;
orig &= data;
2009-10-29 13:22:12 -06:00
*/
2009-09-13 18:02:46 -06:00
ptrace(PTRACE_POKEDATA,g_ProcessHandle, offset, orig);
}
// blah. I hate the kernel devs for crippling /proc/PID/mem. THIS IS RIDICULOUS
inline
bool Mwrite (uint32_t offset, uint32_t size, uint8_t *source)
{
uint32_t indexptr = 0;
while (size > 0)
{
// default: we push 4 bytes
if(size >= 4)
{
MwriteDWord(offset, *(uint32_t *) (source + indexptr));
offset +=4;
indexptr +=4;
size -=4;
}
// last is either three or 2 bytes
else if(size >= 2)
{
MwriteWord(offset, *(uint16_t *) (source + indexptr));
offset +=2;
indexptr +=2;
size -=2;
}
// finishing move
else if(size == 1)
{
MwriteByte(offset, *(uint8_t *) (source + indexptr));
return true;
}
}
}
inline
const std::string MreadCString (uint32_t offset)
{
std::string temp;
char temp_c[256];
int counter = 0;
char r;
do
{
r = MreadByte(offset+counter);
temp_c[counter] = r;
counter++;
2009-10-29 13:22:12 -06:00
} while (r && counter < 255);
2009-09-13 18:02:46 -06:00
temp_c[counter] = 0;
temp = temp_c;
return temp;
2009-10-29 13:22:12 -06:00
}