summaryrefslogtreecommitdiff
path: root/sysutils/strace/patches/patch-au
blob: e632e70e50f8a5be2ee23dac162d545aee9c1457 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
$NetBSD: patch-au,v 1.3 2007/12/05 16:31:01 christos Exp $

--- syscall.c.orig	2006-12-21 17:13:33.000000000 -0500
+++ syscall.c	2007-12-05 11:16:26.000000000 -0500
@@ -38,9 +38,9 @@
 #include <signal.h>
 #include <time.h>
 #include <errno.h>
+#include <sys/param.h>
 #include <sys/user.h>
 #include <sys/syscall.h>
-#include <sys/param.h>
 
 #if HAVE_ASM_REG_H
 #if defined (SPARC) || defined (SPARC64)
@@ -56,6 +56,10 @@
 #endif
 #endif
 
+#ifdef NETBSD
+#include <machine/reg.h>
+#include <x86/psl.h>
+#endif
 #ifdef HAVE_SYS_REG_H
 #include <sys/reg.h>
 #ifndef PTRACE_PEEKUSR
@@ -624,6 +628,7 @@
 }
 #endif
 
+#ifndef NETBSD
 static void
 decode_subcall(tcp, subcall, nsubcalls, style)
 struct tcb *tcp;
@@ -709,6 +714,7 @@
 #endif /* FREEBSD */
 	}
 }
+#endif /* !NETBSD */
 #endif
 
 struct tcb *tcp_last = NULL;
@@ -756,9 +762,12 @@
 	   )
 		return internal_exec(tcp);
 
-	if (   sys_waitpid == func
-	    || sys_wait4 == func
-#if defined(SVR4) || defined(FREEBSD) || defined(SUNOS4)
+	if (
+	    sys_wait4 == func
+#ifndef NETBSD
+	    || sys_waitpid == func
+#endif
+#if defined(SVR4) || defined(ALLBSD) || defined(SUNOS4)
 	    || sys_wait == func
 #endif
 #ifdef ALPHA
@@ -811,9 +820,9 @@
        static long rax;
 #endif
 #endif /* LINUX */
-#ifdef FREEBSD
-	struct reg regs;
-#endif /* FREEBSD */
+#ifdef ALLBSD
+	static struct reg regs;
+#endif /* ALLBSD */
 
 int
 get_scno(tcp)
@@ -1283,9 +1292,9 @@
 #ifdef HAVE_PR_SYSCALL
 	scno = tcp->status.PR_SYSCALL;
 #else /* !HAVE_PR_SYSCALL */
-#ifndef FREEBSD
+#ifndef ALLBSD
 	scno = tcp->status.PR_WHAT;
-#else /* FREEBSD */
+#else /* ALLBSD */
 	if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
 	        perror("pread");
                 return -1;
@@ -1299,8 +1308,41 @@
 	        scno = regs.r_eax;
 	        break;
 	}
-#endif /* FREEBSD */
+#endif /* ALLBSD */
 #endif /* !HAVE_PR_SYSCALL */
+#else /* !USE_PROCFS */
+#ifdef NETBSD
+	if (ptrace(PTRACE_GETREGS,pid,(char *)&regs, 0) < 0) {
+	        perror("GETREGS");
+		return -1;
+	}
+#ifdef __i386__
+	switch (regs.r_eax) {
+	case SYS_syscall:
+	case SYS___syscall:
+    	        if ((scno = ptrace(PTRACE_PEEKUSER, pid,
+		    (char *)regs.r_esp + sizeof(int), sizeof(int))) == -1) {
+			perror("PEEKUSER __syscall");
+			return -1;
+		}
+	        break;
+	default:
+	        scno = regs.r_eax;
+	        break;
+        }
+#endif
+#ifdef __x86_64__
+	switch (regs.regs[_REG_RAX]) {
+	case SYS_syscall:
+	case SYS___syscall:
+		scno = regs.regs[_REG_RDI];
+	        break;
+	default:
+	        scno = regs.regs[_REG_RAX];
+	        break;
+        }
+#endif
+#endif /* NETBSD */
 #endif /* USE_PROCFS */
 	if (!(tcp->flags & TCB_INSYSCALL))
 		tcp->scno = scno;
@@ -1325,7 +1367,9 @@
 struct tcb *tcp;
 {
 #ifndef USE_PROCFS
+#ifndef NETBSD
 	int pid = tcp->pid;
+#endif
 #else /* USE_PROCFS */
 	int scno = known_scno(tcp);
 
@@ -1698,17 +1742,36 @@
 		}
 #endif /* MIPS */
 #endif /* SVR4 */
-#ifdef FREEBSD
+#ifdef ALLBSD
+#ifdef __i386__
 		if (regs.r_eflags & PSL_C) {
  		        tcp->u_rval = -1;
 		        u_error = regs.r_eax;
 		} else {
 		        tcp->u_rval = regs.r_eax;
+#if 0
+			/* XXX Linux only */
 			tcp->u_lrval =
 			  ((unsigned long long) regs.r_edx << 32) +  regs.r_eax;
+#endif
 		        u_error = 0;
 		}
-#endif /* FREEBSD */
+#endif
+#ifdef __x86_64__
+		if (regs.regs[_REG_RFL] & PSL_C) {
+ 		        tcp->u_rval = -1;
+		        u_error = regs.regs[_REG_RAX];
+		} else {
+		        tcp->u_rval = regs.regs[_REG_RAX];
+#if 0
+			/* XXX Linux only */
+			tcp->u_lrval =
+			  ((unsigned long long) regs.regs[_REG_RDX] << 64) +  regs.regs[_REG_RAX];
+#endif
+		        u_error = 0;
+		}
+#endif
+#endif /* ALLBSD */
 	tcp->u_error = u_error;
 	return 1;
 }
@@ -1891,6 +1954,32 @@
                 return -1;
         }
 #endif /* FREEBSD */
+#ifdef NETBSD
+	if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
+		return -1;
+#ifdef __i386__
+	if (error) {
+		regs.r_eflags |= PSL_C;
+		regs.r_eax = error;
+	}
+	else {
+		regs.r_eflags &= ~PSL_C;
+		regs.r_eax = rval;
+	}
+#endif
+#ifdef __x86_64__
+	if (error) {
+		regs.regs[_REG_RFL] |= PSL_C;
+		regs.regs[_REG_RAX] = error;
+	}
+	else {
+		regs.regs[_REG_RFL] &= ~PSL_C;
+		regs.regs[_REG_RAX] = rval;
+	}
+#endif
+	if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
+		return -1;
+#endif /* NETBSD */
 
 	/* All branches reach here on success (only).  */
 	tcp->u_error = error;
@@ -1903,7 +1992,9 @@
 struct tcb *tcp;
 {
 #ifndef USE_PROCFS
+#ifndef NETBSD
 	int pid = tcp->pid;
+#endif
 #endif /* !USE_PROCFS */
 #ifdef LINUX
 #if defined(S390) || defined(S390X)
@@ -2201,6 +2292,58 @@
 	I DONT KNOW WHAT TO DO
 #endif /* !HAVE_PR_SYSCALL */
 #endif /* SVR4 */
+#ifdef NETBSD
+	if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
+		tcp->u_nargs = sysent[tcp->scno].nargs;
+	else
+		tcp->u_nargs = 5;
+#ifdef __i386__
+	switch(regs.r_eax) {
+	case SYS___syscall:
+		umoven(tcp, regs.r_esp + sizeof(int) + sizeof(quad_t),
+		    tcp->u_nargs * sizeof(unsigned long),
+		    (char *) tcp->u_arg);
+		break;
+        case SYS_syscall:
+		umoven(tcp, regs.r_esp + 2 * sizeof(int),
+		    tcp->u_nargs * sizeof(unsigned long),
+		    (char *) tcp->u_arg);
+		break;
+        default:
+		umoven(tcp, regs.r_esp + sizeof(int),
+		    tcp->u_nargs * sizeof(unsigned long),
+		    (char *) tcp->u_arg);
+		break;
+	}
+#endif
+#ifdef __x86_64__
+{
+	int bias, i;
+	static int ar[2][6] = {
+	    { _REG_RDI, _REG_RSI, _REG_RDX,
+	      _REG_R10, _REG_R8, _REG_R9 },	/* x86-64 ABI */
+	    { _REG_RBX, _REG_RCX, _REG_RDX,
+	      _REG_RSI, _REG_RDI, _REG_RBP }	/* i386 ABI */
+	};
+	switch(regs.regs[_REG_RAX]) {
+	case SYS___syscall:
+        case SYS_syscall:
+		bias = 1;
+		break;
+        default:
+		bias = 0;
+		break;
+	}
+	for (i = 0; i < tcp->u_nargs; i++) {
+		tcp->u_arg[i] = regs.regs[ar[0][i + bias]];
+#if 0
+		if (upeek(tcp->pid, ar[0][i + bias] * 8, &tcp->u_arg[i]) < 0)
+			return -1;
+#endif
+	}
+}
+#endif
+#endif /* NETBSD */
 #ifdef FREEBSD
 	if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
 	    sysent[tcp->scno].nargs > tcp->status.val)
@@ -2570,6 +2713,19 @@
 	pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
 	val = regs.r_edx;
 #endif
+#ifdef NETBSD
+	struct reg regs;
+	if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, sizeof(regs)) < 0) {
+		perror("PTRACE_GETREGS get edx");
+		return -1;
+	}
+#ifdef __i386__
+	val = regs.r_edx;
+#endif
+#ifdef __x86_64__
+	val = regs.regs[_REG_RDX];
+#endif
+#endif /* NETBSD */
 	return val;
 }