PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

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