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")
14 const sizeofPtr uintptr = uintptr(unsafe.Sizeof(dummy))
16 func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, errno int) {
17 // The peek requests are machine-size oriented, so we wrap it
18 // to retrieve arbitrary-length data.
20 var buf [sizeofPtr]byte;
22 // Leading edge. PEEKTEXT/PEEKDATA don't require aligned
23 // access (PEEKUSER warns that it might), but if we don't
24 // align our reads, we might straddle an unmapped page
25 // boundary and not get the bytes leading up to the page
28 if addr % sizeofPtr != 0 {
30 val := libc_ptrace(req, Pid_t(pid), addr - addr%sizeofPtr, nil);
31 if errno := GetErrno(); errno != 0 {
34 *(*_C_long)(unsafe.Pointer(&buf[0])) = val;
35 n += copy(out, buf[addr%sizeofPtr:]);
41 // We use an internal buffer to gaurantee alignment.
42 // It's not documented if this is necessary, but we're paranoid.
44 val := libc_ptrace(req, Pid_t(pid), addr+uintptr(n), nil);
45 if errno = GetErrno(); errno != 0 {
48 *(*_C_long)(unsafe.Pointer(&buf[0])) = val;
49 copied := copy(out, buf[0:]);
57 func PtracePeekText(pid int, addr uintptr, out []byte) (count int, errno int) {
58 return ptracePeek(_PTRACE_PEEKTEXT, pid, addr, out);
61 func PtracePeekData(pid int, addr uintptr, out []byte) (count int, errno int) {
62 return ptracePeek(_PTRACE_PEEKDATA, pid, addr, out);
65 func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, errno int) {
66 // As for ptracePeek, we need to align our accesses to deal
67 // with the possibility of straddling an invalid page.
71 if addr % sizeofPtr != 0 {
72 var buf [sizeofPtr]byte;
73 if libc_ptrace(peekReq, Pid_t(pid), addr - addr%sizeofPtr, &buf[0]) < 0 {
76 n += copy(buf[addr%sizeofPtr:], data);
77 word := (*byte)(unsafe.Pointer(*((*uintptr)(unsafe.Pointer(&buf[0])))));
78 if libc_ptrace(pokeReq, Pid_t(pid), addr - addr%sizeofPtr, word) < 0 {
81 data = data[n:len(data)];
85 for uintptr(len(data)) > sizeofPtr {
86 word := (*byte)(unsafe.Pointer(*((*uintptr)(unsafe.Pointer(&data[0])))));
87 if libc_ptrace(pokeReq, Pid_t(pid), addr+uintptr(n), word) < 0 {
91 data = data[sizeofPtr:len(data)];
96 var buf [sizeofPtr]byte;
97 if libc_ptrace(peekReq, Pid_t(pid), addr+uintptr(n), &buf[0]) < 0 {
101 word := (*byte)(unsafe.Pointer(*((*uintptr)(unsafe.Pointer(&buf[0])))));
102 if libc_ptrace(pokeReq, Pid_t(pid), addr+uintptr(n), word) < 0 {
103 return n, GetErrno();
111 func PtracePokeText(pid int, addr uintptr, data []byte) (count int, errno int) {
112 return ptracePoke(_PTRACE_POKETEXT, _PTRACE_PEEKTEXT, pid, addr, data);
115 func PtracePokeData(pid int, addr uintptr, data []byte) (count int, errno int) {
116 return ptracePoke(_PTRACE_POKEDATA, _PTRACE_PEEKDATA, pid, addr, data);
119 func PtraceGetRegs(pid int, regsout *PtraceRegs) (errno int) {
120 if libc_ptrace(_PTRACE_GETREGS, Pid_t(pid), 0, (*byte)(unsafe.Pointer(regsout))) < 0 {
127 func PtraceSetRegs(pid int, regs *PtraceRegs) (errno int) {
128 if libc_ptrace(_PTRACE_SETREGS, Pid_t(pid), 0, (*byte)(unsafe.Pointer(regs))) < 0 {
135 func PtraceSetOptions(pid int, options int) (errno int) {
136 if libc_ptrace(_PTRACE_SETOPTIONS, Pid_t(pid), 0, (*byte)(unsafe.Pointer(uintptr(options)))) < 0 {
143 func PtraceGetEventMsg(pid int) (msg uint, errno int) {
145 if libc_ptrace(_PTRACE_GETEVENTMSG, Pid_t(pid), 0, (*byte)(unsafe.Pointer(&data))) < 0 {
152 func PtraceCont(pid int, signal int) (errno int) {
153 if libc_ptrace(_PTRACE_CONT, Pid_t(pid), 0, (*byte)(unsafe.Pointer(uintptr(signal)))) < 0 {
160 func PtraceSingleStep(pid int) (errno int) {
161 if libc_ptrace(_PTRACE_SINGLESTEP, Pid_t(pid), 0, nil) < 0 {
168 func PtraceAttach(pid int) (errno int) {
169 if libc_ptrace(_PTRACE_ATTACH, Pid_t(pid), 0, nil) < 0 {
176 func PtraceDetach(pid int) (errno int) {
177 if libc_ptrace(_PTRACE_DETACH, Pid_t(pid), 0, nil) < 0 {
184 func Tgkill(tgid int, tid int, sig int) (errno int) {
185 r1, r2, err := Syscall(SYS_TGKILL, uintptr(tgid), uintptr(tid),