aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.6/libgo/syscalls/syscall_linux.go
blob: bdb92c5f4caa9daf66d13118b6e22b29178df4a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// syscall_linux.go -- GNU/Linux specific syscall interface.

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syscall

import "unsafe"

func libc_ptrace(request int, pid Pid_t, addr uintptr, data *byte) _C_long __asm__ ("ptrace")

var dummy *byte
const sizeofPtr uintptr = uintptr(unsafe.Sizeof(dummy))

func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, errno int) {
	// The peek requests are machine-size oriented, so we wrap it
	// to retrieve arbitrary-length data.

	var buf [sizeofPtr]byte;

	// Leading edge.  PEEKTEXT/PEEKDATA don't require aligned
	// access (PEEKUSER warns that it might), but if we don't
	// align our reads, we might straddle an unmapped page
	// boundary and not get the bytes leading up to the page
	// boundary.
	n := 0;
	if addr % sizeofPtr != 0 {
		SetErrno(0);
		val := libc_ptrace(req, Pid_t(pid), addr - addr%sizeofPtr, nil);
		if errno := GetErrno(); errno != 0 {
			return 0, errno;
		}
		*(*_C_long)(unsafe.Pointer(&buf[0])) = val;
		n += copy(out, buf[addr%sizeofPtr:]);
		out = out[n:];
	}

	// Remainder.
	for len(out) > 0 {
		// We use an internal buffer to gaurantee alignment.
		// It's not documented if this is necessary, but we're paranoid.
		SetErrno(0);
		val := libc_ptrace(req, Pid_t(pid), addr+uintptr(n), nil);
		if errno = GetErrno(); errno != 0 {
			return n, errno;
		}
		*(*_C_long)(unsafe.Pointer(&buf[0])) = val;
		copied := copy(out, buf[0:]);
		n += copied;
		out = out[copied:];
	}

	return n, 0;
}

func PtracePeekText(pid int, addr uintptr, out []byte) (count int, errno int) {
	return ptracePeek(_PTRACE_PEEKTEXT, pid, addr, out);
}

func PtracePeekData(pid int, addr uintptr, out []byte) (count int, errno int) {
	return ptracePeek(_PTRACE_PEEKDATA, pid, addr, out);
}

func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, errno int) {
	// As for ptracePeek, we need to align our accesses to deal
	// with the possibility of straddling an invalid page.

	// Leading edge.
	n := 0;
	if addr % sizeofPtr != 0 {
		var buf [sizeofPtr]byte;
		if libc_ptrace(peekReq, Pid_t(pid), addr - addr%sizeofPtr, &buf[0]) < 0 {
			return 0, GetErrno();
		}
		n += copy(buf[addr%sizeofPtr:], data);
		word := (*byte)(unsafe.Pointer(*((*uintptr)(unsafe.Pointer(&buf[0])))));
		if libc_ptrace(pokeReq, Pid_t(pid), addr - addr%sizeofPtr, word) < 0 {
			return 0, GetErrno();
		}
		data = data[n:len(data)];
	}

	// Interior.
	for uintptr(len(data)) > sizeofPtr {
		word := (*byte)(unsafe.Pointer(*((*uintptr)(unsafe.Pointer(&data[0])))));
		if libc_ptrace(pokeReq, Pid_t(pid), addr+uintptr(n), word) < 0 {
			return n, GetErrno();
		}
		n += int(sizeofPtr);
		data = data[sizeofPtr:len(data)];
	}

	// Trailing edge.
	if len(data) > 0 {
		var buf [sizeofPtr]byte;
		if libc_ptrace(peekReq, Pid_t(pid), addr+uintptr(n), &buf[0]) < 0 {
			return n, GetErrno();
		}
		copy(buf[0:], data);
		word := (*byte)(unsafe.Pointer(*((*uintptr)(unsafe.Pointer(&buf[0])))));
		if libc_ptrace(pokeReq, Pid_t(pid), addr+uintptr(n), word) < 0 {
			return n, GetErrno();
		}
		n += len(data);
	}

	return n, 0;
}

func PtracePokeText(pid int, addr uintptr, data []byte) (count int, errno int) {
	return ptracePoke(_PTRACE_POKETEXT, _PTRACE_PEEKTEXT, pid, addr, data);
}

func PtracePokeData(pid int, addr uintptr, data []byte) (count int, errno int) {
	return ptracePoke(_PTRACE_POKEDATA, _PTRACE_PEEKDATA, pid, addr, data);
}

func PtraceGetRegs(pid int, regsout *PtraceRegs) (errno int) {
	if libc_ptrace(_PTRACE_GETREGS, Pid_t(pid), 0, (*byte)(unsafe.Pointer(regsout))) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func PtraceSetRegs(pid int, regs *PtraceRegs) (errno int) {
	if libc_ptrace(_PTRACE_SETREGS, Pid_t(pid), 0, (*byte)(unsafe.Pointer(regs))) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func PtraceSetOptions(pid int, options int) (errno int) {
	if libc_ptrace(_PTRACE_SETOPTIONS, Pid_t(pid), 0, (*byte)(unsafe.Pointer(uintptr(options)))) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func PtraceGetEventMsg(pid int) (msg uint, errno int) {
	var data _C_long;
	if libc_ptrace(_PTRACE_GETEVENTMSG, Pid_t(pid), 0, (*byte)(unsafe.Pointer(&data))) < 0 {
		errno = GetErrno();
	}
	msg = uint(data);
	return;
}

func PtraceCont(pid int, signal int) (errno int) {
	if libc_ptrace(_PTRACE_CONT, Pid_t(pid), 0, (*byte)(unsafe.Pointer(uintptr(signal)))) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func PtraceSingleStep(pid int) (errno int) {
	if libc_ptrace(_PTRACE_SINGLESTEP, Pid_t(pid), 0, nil) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func PtraceAttach(pid int) (errno int) {
	if libc_ptrace(_PTRACE_ATTACH, Pid_t(pid), 0, nil) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func PtraceDetach(pid int) (errno int) {
	if libc_ptrace(_PTRACE_DETACH, Pid_t(pid), 0, nil) < 0 {
		return GetErrno();
	} else {
		return 0;
	}
}

func Tgkill(tgid int, tid int, sig int) (errno int) {
	r1, r2, err := Syscall(SYS_TGKILL, uintptr(tgid), uintptr(tid),
				 uintptr(sig));
	return int(err);
}