{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Memory.Debug.FFI where
import Control.Monad
import Foreign
import Foreign.C
import System.Posix.Types
import Data.Memory.Debug.IOVec
type SSize_T = CULong
foreign import ccall unsafe "sys/uio.h process_vm_readv"
processVMReadV_
:: CPid
-> Ptr IOVec
-> CULong
-> Ptr IOVec
-> CULong
-> CULong
-> IO SSize_T
processVMReadV
:: CPid
-> Ptr Word8
-> Int
-> IO [Word8]
processVMReadV pid addr len =
allocaBytes len $ \buf ->
alloca $ \iov_local_ptr ->
alloca $ \iov_remote_ptr ->
do
poke iov_local_ptr $ IOVec buf (fromIntegral len)
poke iov_remote_ptr $ IOVec addr (fromIntegral len)
print $ IOVec addr (fromIntegral len)
readBytes <- processVMReadV_ pid iov_local_ptr 1 iov_remote_ptr 1 0
print readBytes
iov_local <- peek iov_local_ptr
result <- peekArray (iov_len iov_local) (iov_base iov_local)
return result
foreign import ccall unsafe "sys/uio.h process_vm_writev"
processVMWriteV_
:: CPid
-> Ptr IOVec
-> CULong
-> Ptr IOVec
-> CULong
-> CULong
-> IO SSize_T
processVMWriteV
:: CPid
-> Ptr Word8
-> Int
-> [Word8]
-> IO ()
processVMWriteV pid addr len xs =
allocaBytes len $ \buf ->
alloca $ \iov_local_ptr ->
alloca $ \iov_remote_ptr ->
do
pokeArray buf xs
poke iov_local_ptr $ IOVec buf (fromIntegral len)
poke iov_remote_ptr $ IOVec addr (fromIntegral len)
print $ IOVec addr (fromIntegral len)
writtenBytes <- processVMWriteV_ pid iov_local_ptr 1 iov_remote_ptr 1 0
return ()