1 // syscall_linux.go -- GNU/Linux specific syscall interface.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 func libc_ptrace(request
int, pid Pid_t
, addr
uintptr, data
*byte) _C_long
__asm__ ("ptrace")
12 func libc_sendfile(int, int, *Offset_t
, Size_t
) Ssize_t
__asm__ ("sendfile")
15 const sizeofPtr
uintptr = uintptr(unsafe
.Sizeof(dummy
))
17 func ptracePeek(req
int, pid
int, addr
uintptr, out
[]byte) (count
int, errno
int) {
18 // The peek requests are machine-size oriented, so we wrap it
19 // to retrieve arbitrary-length data.
21 var buf
[sizeofPtr
]byte;
23 // Leading edge. PEEKTEXT/PEEKDATA don't require aligned
24 // access (PEEKUSER warns that it might), but if we don't
25 // align our reads, we might straddle an unmapped page
26 // boundary and not get the bytes leading up to the page
29 if addr
% sizeofPtr
!= 0 {
31 val
:= libc_ptrace(req
, Pid_t(pid
), addr
- addr%sizeofPtr
, nil);
32 if errno
:= GetErrno(); errno
!= 0 {
35 *(*_C_long
)(unsafe
.Pointer(&buf
[0])) = val
;
36 n
+= copy(out
, buf
[addr%sizeofPtr
:]);
42 // We use an internal buffer to gaurantee alignment.
43 // It's not documented if this is necessary, but we're paranoid.
45 val
:= libc_ptrace(req
, Pid_t(pid
), addr
+uintptr(n
), nil);
46 if errno
= GetErrno(); errno
!= 0 {
49 *(*_C_long
)(unsafe
.Pointer(&buf
[0])) = val
;
50 copied
:= copy(out
, buf
[0:]);
58 func PtracePeekText(pid
int, addr
uintptr, out
[]byte) (count
int, errno
int) {
59 return ptracePeek(_PTRACE_PEEKTEXT
, pid
, addr
, out
);
62 func PtracePeekData(pid
int, addr
uintptr, out
[]byte) (count
int, errno
int) {
63 return ptracePeek(_PTRACE_PEEKDATA
, pid
, addr
, out
);
66 func ptracePoke(pokeReq
int, peekReq
int, pid
int, addr
uintptr, data
[]byte) (count
int, errno
int) {
67 // As for ptracePeek, we need to align our accesses to deal
68 // with the possibility of straddling an invalid page.
72 if addr
% sizeofPtr
!= 0 {
73 var buf
[sizeofPtr
]byte;
74 if libc_ptrace(peekReq
, Pid_t(pid
), addr
- addr%sizeofPtr
, &buf
[0]) < 0 {
77 n
+= copy(buf
[addr%sizeofPtr
:], data
);
78 word
:= (*byte)(unsafe
.Pointer(*((*uintptr)(unsafe
.Pointer(&buf
[0])))));
79 if libc_ptrace(pokeReq
, Pid_t(pid
), addr
- addr%sizeofPtr
, word
) < 0 {
82 data
= data
[n
:len(data
)];
86 for uintptr(len(data
)) > sizeofPtr
{
87 word
:= (*byte)(unsafe
.Pointer(*((*uintptr)(unsafe
.Pointer(&data
[0])))));
88 if libc_ptrace(pokeReq
, Pid_t(pid
), addr
+uintptr(n
), word
) < 0 {
92 data
= data
[sizeofPtr
:len(data
)];
97 var buf
[sizeofPtr
]byte;
98 if libc_ptrace(peekReq
, Pid_t(pid
), addr
+uintptr(n
), &buf
[0]) < 0 {
102 word
:= (*byte)(unsafe
.Pointer(*((*uintptr)(unsafe
.Pointer(&buf
[0])))));
103 if libc_ptrace(pokeReq
, Pid_t(pid
), addr
+uintptr(n
), word
) < 0 {
104 return n
, GetErrno();
112 func PtracePokeText(pid
int, addr
uintptr, data
[]byte) (count
int, errno
int) {
113 return ptracePoke(_PTRACE_POKETEXT
, _PTRACE_PEEKTEXT
, pid
, addr
, data
);
116 func PtracePokeData(pid
int, addr
uintptr, data
[]byte) (count
int, errno
int) {
117 return ptracePoke(_PTRACE_POKEDATA
, _PTRACE_PEEKDATA
, pid
, addr
, data
);
120 func PtraceGetRegs(pid
int, regsout
*PtraceRegs
) (errno
int) {
121 if libc_ptrace(_PTRACE_GETREGS
, Pid_t(pid
), 0, (*byte)(unsafe
.Pointer(regsout
))) < 0 {
128 func PtraceSetRegs(pid
int, regs
*PtraceRegs
) (errno
int) {
129 if libc_ptrace(_PTRACE_SETREGS
, Pid_t(pid
), 0, (*byte)(unsafe
.Pointer(regs
))) < 0 {
136 func PtraceSetOptions(pid
int, options
int) (errno
int) {
137 if libc_ptrace(_PTRACE_SETOPTIONS
, Pid_t(pid
), 0, (*byte)(unsafe
.Pointer(uintptr(options
)))) < 0 {
144 func PtraceGetEventMsg(pid
int) (msg
uint, errno
int) {
146 if libc_ptrace(_PTRACE_GETEVENTMSG
, Pid_t(pid
), 0, (*byte)(unsafe
.Pointer(&data
))) < 0 {
153 func PtraceCont(pid
int, signal
int) (errno
int) {
154 if libc_ptrace(_PTRACE_CONT
, Pid_t(pid
), 0, (*byte)(unsafe
.Pointer(uintptr(signal
)))) < 0 {
161 func PtraceSingleStep(pid
int) (errno
int) {
162 if libc_ptrace(_PTRACE_SINGLESTEP
, Pid_t(pid
), 0, nil) < 0 {
169 func PtraceAttach(pid
int) (errno
int) {
170 if libc_ptrace(_PTRACE_ATTACH
, Pid_t(pid
), 0, nil) < 0 {
177 func PtraceDetach(pid
int) (errno
int) {
178 if libc_ptrace(_PTRACE_DETACH
, Pid_t(pid
), 0, nil) < 0 {
185 func Tgkill(tgid
int, tid
int, sig
int) (errno
int) {
186 r1
, r2
, err
:= Syscall(SYS_TGKILL
, uintptr(tgid
), uintptr(tid
),
191 func Sendfile(outfd
int, infd
int, offset
*int64, count
int) (written
int, errno
int) {
195 o
= Offset_t(*offset
)
198 w
:= libc_sendfile(outfd
, infd
, po
, Size_t(count
))