/src/Ptrace.hsc

http://github.com/Eelis/geordi · Unknown · 51 lines · 38 code · 13 blank · 0 comment · 0 complexity · 33f07ff93b6b9bf6a44c6f564f56c2bb MD5 · raw file

  1. {-# LANGUAGE UnicodeSyntax, ForeignFunctionInterface #-}
  2. module Ptrace (traceme, syscall, peektext, peekuser, pokeuser, cont, kill, tracesysgood) where
  3. import Foreign.C
  4. import System.Posix
  5. import Control.Monad (when)
  6. import Prelude
  7. #include <sys/ptrace.h>
  8. #include <linux/ptrace.h>
  9. #include <syscall.h>
  10. foreign import ccall "sys/ptrace.h ptrace" c_ptrace :: CInt ? CPid ? CLong ? CLong ? IO CLong
  11. ignored_param :: CLong
  12. ignored_param = 0
  13. ptrace :: CInt ? ProcessID ? CLong ? CLong ? IO CLong
  14. ptrace act procid addr datum = do
  15. resetErrno
  16. r ? c_ptrace act procid addr datum
  17. errno ? getErrno
  18. if errno /= eOK then throwErrno "ptrace" else return r
  19. traceme :: IO ()
  20. traceme = ptrace (#const PTRACE_TRACEME) 0 ignored_param ignored_param >> return ()
  21. syscall :: ProcessID ? IO ()
  22. syscall p = ptrace (#const PTRACE_SYSCALL) p ignored_param ignored_param >> return ()
  23. peekuser :: ProcessID ? CLong ? IO CLong
  24. peekuser p a = ptrace (#const PTRACE_PEEKUSER) p a ignored_param
  25. peektext :: ProcessID ? CLong ? IO CLong
  26. peektext p a = ptrace (#const PTRACE_PEEKTEXT) p a ignored_param
  27. pokeuser :: ProcessID ? CLong ? CLong ? IO ()
  28. pokeuser p a d = ptrace (#const PTRACE_POKEUSER) p a d >> return ()
  29. cont :: ProcessID ? Maybe CLong ? IO ()
  30. cont p s = ptrace (#const PTRACE_CONT) p ignored_param (maybe 0 id s) >> return ()
  31. tracesysgood :: ProcessID ? IO ()
  32. tracesysgood p = ptrace (#const PTRACE_SETOPTIONS) p
  33. ignored_param (#const PTRACE_O_TRACESYSGOOD) >> return ()
  34. kill :: ProcessID ? IO ()
  35. kill pid = do
  36. r ? c_ptrace (#const PTRACE_KILL) pid ignored_param ignored_param
  37. e ? getErrno
  38. when (r == -1 && e /= eSRCH) $ throwErrno "ptrace KILL"