blob: f06e172cbd1dca0294d323863baeb94714e4c2bc [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id$
34 */
35
36#include "defs.h"
37
38#include <signal.h>
39#include <time.h>
40#include <errno.h>
41#include <sys/user.h>
42#include <sys/syscall.h>
43#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044
Wichert Akkerman15dea971999-10-06 13:06:34 +000045#if HAVE_ASM_REG_H
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000046#ifdef SPARC
47# define fpq kernel_fpq
48# define fq kernel_fq
49# define fpu kernel_fpu
50#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000051#include <asm/reg.h>
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000052#ifdef SPARC
53# undef fpq
54# undef fq
Roland McGrath761b5d72002-12-15 23:58:31 +000055# undef fpu
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000056#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000057#endif
58
Wichert Akkerman15dea971999-10-06 13:06:34 +000059#ifdef HAVE_SYS_REG_H
60#include <sys/reg.h>
61#ifndef PTRACE_PEEKUSR
62# define PTRACE_PEEKUSR PTRACE_PEEKUSER
63#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000064#elif defined(HAVE_LINUX_PTRACE_H)
65#undef PTRACE_SYSCALL
66#include <linux/ptrace.h>
Wichert Akkerman15dea971999-10-06 13:06:34 +000067#endif
68
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000069#if defined(LINUX) && defined(IA64)
70# include <asm/ptrace_offsets.h>
71# include <asm/rse.h>
72#endif
73
Pavel Machekd8ae7e32000-02-01 17:17:25 +000074#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000075#ifdef LINUX
76#ifndef ERESTARTSYS
77#define ERESTARTSYS 512
78#endif
79#ifndef ERESTARTNOINTR
80#define ERESTARTNOINTR 513
81#endif
82#ifndef ERESTARTNOHAND
83#define ERESTARTNOHAND 514 /* restart if no handler.. */
84#endif
85#ifndef ENOIOCTLCMD
86#define ENOIOCTLCMD 515 /* No ioctl command */
87#endif
88#ifndef NSIG
89#define NSIG 32
90#endif
91#ifdef ARM
92#undef NSIG
93#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000094#undef NR_SYSCALL_BASE
95#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096#endif
97#endif /* LINUX */
98
99#include "syscall.h"
100
101/* Define these shorthand notations to simplify the syscallent files. */
102#define TF TRACE_FILE
103#define TI TRACE_IPC
104#define TN TRACE_NETWORK
105#define TP TRACE_PROCESS
106#define TS TRACE_SIGNAL
107
108struct sysent sysent0[] = {
109#include "syscallent.h"
110};
111int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
112
113#if SUPPORTED_PERSONALITIES >= 2
114struct sysent sysent1[] = {
115#include "syscallent1.h"
116};
117int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
118#endif /* SUPPORTED_PERSONALITIES >= 2 */
119
120#if SUPPORTED_PERSONALITIES >= 3
121struct sysent sysent2[] = {
122#include "syscallent2.h"
123};
124int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
125#endif /* SUPPORTED_PERSONALITIES >= 3 */
126
127struct sysent *sysent;
128int nsyscalls;
129
130/* Now undef them since short defines cause wicked namespace pollution. */
131#undef TF
132#undef TI
133#undef TN
134#undef TP
135#undef TS
136
137char *errnoent0[] = {
138#include "errnoent.h"
139};
140int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
141
142#if SUPPORTED_PERSONALITIES >= 2
143char *errnoent1[] = {
144#include "errnoent1.h"
145};
146int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
147#endif /* SUPPORTED_PERSONALITIES >= 2 */
148
149#if SUPPORTED_PERSONALITIES >= 3
150char *errnoent2[] = {
151#include "errnoent2.h"
152};
153int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
154#endif /* SUPPORTED_PERSONALITIES >= 3 */
155
156char **errnoent;
157int nerrnos;
158
159int current_personality;
160
161int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000162set_personality(personality)
163int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164{
165 switch (personality) {
166 case 0:
167 errnoent = errnoent0;
168 nerrnos = nerrnos0;
169 sysent = sysent0;
170 nsyscalls = nsyscalls0;
171 ioctlent = ioctlent0;
172 nioctlents = nioctlents0;
173 signalent = signalent0;
174 nsignals = nsignals0;
175 break;
176
177#if SUPPORTED_PERSONALITIES >= 2
178 case 1:
179 errnoent = errnoent1;
180 nerrnos = nerrnos1;
181 sysent = sysent1;
182 nsyscalls = nsyscalls1;
183 ioctlent = ioctlent1;
184 nioctlents = nioctlents1;
185 signalent = signalent1;
186 nsignals = nsignals1;
187 break;
188#endif /* SUPPORTED_PERSONALITIES >= 2 */
189
190#if SUPPORTED_PERSONALITIES >= 3
191 case 2:
192 errnoent = errnoent2;
193 nerrnos = nerrnos2;
194 sysent = sysent2;
195 nsyscalls = nsyscalls2;
196 ioctlent = ioctlent2;
197 nioctlents = nioctlents2;
198 signalent = signalent2;
199 nsignals = nsignals2;
200 break;
201#endif /* SUPPORTED_PERSONALITIES >= 3 */
202
203 default:
204 return -1;
205 }
206
207 current_personality = personality;
208 return 0;
209}
210
211int qual_flags[MAX_QUALS];
212
213static int call_count[MAX_QUALS];
214static int error_count[MAX_QUALS];
215static struct timeval tv_count[MAX_QUALS];
216static int sorted_count[MAX_QUALS];
217
218static struct timeval shortest = { 1000000, 0 };
219
Roland McGrath9797ceb2002-12-30 10:23:00 +0000220static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000221
222static struct qual_options {
223 int bitflag;
224 char *option_name;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000225 int (*qualify)();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000226 char *argument_name;
227} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000228 { QUAL_TRACE, "trace", qual_syscall, "system call" },
229 { QUAL_TRACE, "t", qual_syscall, "system call" },
230 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
231 { QUAL_ABBREV, "a", qual_syscall, "system call" },
232 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
233 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
234 { QUAL_RAW, "raw", qual_syscall, "system call" },
235 { QUAL_RAW, "x", qual_syscall, "system call" },
236 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
237 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
238 { QUAL_SIGNAL, "s", qual_signal, "signal" },
239 { QUAL_FAULT, "fault", qual_fault, "fault" },
240 { QUAL_FAULT, "faults", qual_fault, "fault" },
241 { QUAL_FAULT, "m", qual_fault, "fault" },
242 { QUAL_READ, "read", qual_desc, "descriptor" },
243 { QUAL_READ, "reads", qual_desc, "descriptor" },
244 { QUAL_READ, "r", qual_desc, "descriptor" },
245 { QUAL_WRITE, "write", qual_desc, "descriptor" },
246 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
247 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000248 { 0, NULL, NULL, NULL },
249};
250
Roland McGrath9797ceb2002-12-30 10:23:00 +0000251static void
252qualify_one(n, opt, not)
253 int n;
254 struct qual_options *opt;
255 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000256{
Roland McGrath9797ceb2002-12-30 10:23:00 +0000257 if (not)
258 qual_flags[n] &= ~opt->bitflag;
259 else
260 qual_flags[n] |= opt->bitflag;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000261}
262
263static int
Roland McGrath9797ceb2002-12-30 10:23:00 +0000264qual_syscall(s, opt, not)
265 char *s;
266 struct qual_options *opt;
267 int not;
268{
269 int i;
270 int any = 0;
271
272 for (i = 0; i < nsyscalls; i++) {
273 if (strcmp(s, sysent[i].sys_name) == 0) {
274 qualify_one(i, opt, not);
275 any = 1;
276 }
277 }
278 return !any;
279}
280
281static int
282qual_signal(s, opt, not)
283 char *s;
284 struct qual_options *opt;
285 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286{
287 int i;
288 char buf[32];
289
Roland McGrath9797ceb2002-12-30 10:23:00 +0000290 if (s && *s && isdigit((unsigned char)*s)) {
291 qualify_one(atoi(s), opt, not);
292 return 1;
293 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 strcpy(buf, s);
295 s = buf;
296 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000297 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 if (strncmp(s, "SIG", 3) == 0)
299 s += 3;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000300 for (i = 0; i <= NSIG; i++)
301 if (strcmp(s, signame(i) + 3) == 0) {
302 qualify_one(atoi(s), opt, not);
303 return 1;
304 }
305 return 0;
306}
307
308static int
309qual_fault(s, opt, not)
310 char *s;
311 struct qual_options *opt;
312 int not;
313{
314 return -1;
315}
316
317static int
318qual_desc(s, opt, not)
319 char *s;
320 struct qual_options *opt;
321 int not;
322{
323 if (s && *s && isdigit((unsigned char)*s)) {
324 qualify_one(atoi(s), opt, not);
Roland McGrath2b619022003-04-10 18:58:20 +0000325 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 }
327 return -1;
328}
329
330static int
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000331lookup_class(s)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000332 char *s;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000333{
334 if (strcmp(s, "file") == 0)
335 return TRACE_FILE;
336 if (strcmp(s, "ipc") == 0)
337 return TRACE_IPC;
338 if (strcmp(s, "network") == 0)
339 return TRACE_NETWORK;
340 if (strcmp(s, "process") == 0)
341 return TRACE_PROCESS;
342 if (strcmp(s, "signal") == 0)
343 return TRACE_SIGNAL;
344 return -1;
345}
346
347void
348qualify(s)
349char *s;
350{
351 struct qual_options *opt;
352 int not;
353 char *p;
354 int i, n;
355
356 opt = &qual_options[0];
357 for (i = 0; (p = qual_options[i].option_name); i++) {
358 n = strlen(p);
359 if (strncmp(s, p, n) == 0 && s[n] == '=') {
360 opt = &qual_options[i];
361 s += n + 1;
362 break;
363 }
364 }
365 not = 0;
366 if (*s == '!') {
367 not = 1;
368 s++;
369 }
370 if (strcmp(s, "none") == 0) {
371 not = 1 - not;
372 s = "all";
373 }
374 if (strcmp(s, "all") == 0) {
375 for (i = 0; i < MAX_QUALS; i++) {
376 if (not)
377 qual_flags[i] &= ~opt->bitflag;
378 else
379 qual_flags[i] |= opt->bitflag;
380 }
381 return;
382 }
383 for (i = 0; i < MAX_QUALS; i++) {
384 if (not)
385 qual_flags[i] |= opt->bitflag;
386 else
387 qual_flags[i] &= ~opt->bitflag;
388 }
389 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
390 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
391 for (i = 0; i < MAX_QUALS; i++) {
392 if (sysent[i].sys_flags & n) {
393 if (not)
394 qual_flags[i] &= ~opt->bitflag;
395 else
396 qual_flags[i] |= opt->bitflag;
397 }
398 }
399 continue;
400 }
Roland McGrath9797ceb2002-12-30 10:23:00 +0000401 if (opt->qualify(p, opt, not)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000402 fprintf(stderr, "strace: invalid %s `%s'\n",
403 opt->argument_name, p);
404 exit(1);
405 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000406 }
407 return;
408}
409
410static void
411dumpio(tcp)
412struct tcb *tcp;
413{
414 if (syserror(tcp))
415 return;
416 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
417 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000418 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000419 case SYS_read:
420#ifdef SYS_recv
421 case SYS_recv:
422#endif
423#ifdef SYS_recvfrom
424 case SYS_recvfrom:
425#endif
426 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
427 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
428 break;
429 case SYS_write:
430#ifdef SYS_send
431 case SYS_send:
432#endif
433#ifdef SYS_sendto
434 case SYS_sendto:
435#endif
436 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
437 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
438 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000439#ifdef SYS_readv
440 case SYS_readv:
441 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
442 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
443 break;
444#endif
445#ifdef SYS_writev
446 case SYS_writev:
447
448 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
449 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
450 break;
451#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452 }
453}
454
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000455#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000456enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000457#else /* FREEBSD */
458enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
459
460struct subcall {
461 int call;
462 int nsubcalls;
463 int subcalls[5];
464};
465
466const struct subcall subcalls_table[] = {
467 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000468#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000469 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000470#else
471 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
472#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000473 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
474};
475#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000477#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478
479const int socket_map [] = {
480 /* SYS_SOCKET */ 97,
481 /* SYS_BIND */ 104,
482 /* SYS_CONNECT */ 98,
483 /* SYS_LISTEN */ 106,
484 /* SYS_ACCEPT */ 99,
485 /* SYS_GETSOCKNAME */ 150,
486 /* SYS_GETPEERNAME */ 141,
487 /* SYS_SOCKETPAIR */ 135,
488 /* SYS_SEND */ 101,
489 /* SYS_RECV */ 102,
490 /* SYS_SENDTO */ 133,
491 /* SYS_RECVFROM */ 125,
492 /* SYS_SHUTDOWN */ 134,
493 /* SYS_SETSOCKOPT */ 105,
494 /* SYS_GETSOCKOPT */ 118,
495 /* SYS_SENDMSG */ 114,
496 /* SYS_RECVMSG */ 113
497};
498
499void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000500sparc_socket_decode (tcp)
501struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502{
503 volatile long addr;
504 volatile int i, n;
505
506 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
507 return;
508 }
509 tcp->scno = socket_map [tcp->u_arg [0]-1];
510 n = tcp->u_nargs = sysent [tcp->scno].nargs;
511 addr = tcp->u_arg [1];
512 for (i = 0; i < n; i++){
513 int arg;
514 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
515 arg = 0;
516 tcp->u_arg [i] = arg;
517 addr += sizeof (arg);
518 }
519}
520
Michal Ludvig0e035502002-09-23 15:41:01 +0000521void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522decode_subcall(tcp, subcall, nsubcalls, style)
523struct tcb *tcp;
524int subcall;
525int nsubcalls;
526enum subcall_style style;
527{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000528 long addr, mask, arg;
529 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000530
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 switch (style) {
532 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000533 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
534 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 tcp->scno = subcall + tcp->u_arg[0];
536 if (sysent[tcp->scno].nargs != -1)
537 tcp->u_nargs = sysent[tcp->scno].nargs;
538 else
539 tcp->u_nargs--;
540 for (i = 0; i < tcp->u_nargs; i++)
541 tcp->u_arg[i] = tcp->u_arg[i + 1];
542 break;
543 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000544 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
545 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 tcp->scno = subcall + tcp->u_arg[0];
547 addr = tcp->u_arg[1];
548 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
549 if (umove(tcp, addr, &arg) < 0)
550 arg = 0;
551 tcp->u_arg[i] = arg;
552 addr += sizeof(arg);
553 }
554 tcp->u_nargs = sysent[tcp->scno].nargs;
555 break;
556 case mask_style:
557 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000558 for (i = 0; mask; i++)
559 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000560 if (i >= nsubcalls)
561 return;
562 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000563 tcp->scno = subcall + i;
564 if (sysent[tcp->scno].nargs != -1)
565 tcp->u_nargs = sysent[tcp->scno].nargs;
566 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000567 case door_style:
568 /*
569 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000570 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000571 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000572 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
573 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000574 tcp->scno = subcall + tcp->u_arg[5];
575 if (sysent[tcp->scno].nargs != -1)
576 tcp->u_nargs = sysent[tcp->scno].nargs;
577 else
578 tcp->u_nargs--;
579 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000580#ifdef FREEBSD
581 case table_style:
582 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
583 if (subcalls_table[i].call == tcp->scno) break;
584 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
585 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
586 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
587 for (i = 0; i < tcp->u_nargs; i++)
588 tcp->u_arg[i] = tcp->u_arg[i + 1];
589 }
590 break;
591#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000592 }
593}
594#endif
595
596struct tcb *tcp_last = NULL;
597
598static int
599internal_syscall(tcp)
600struct tcb *tcp;
601{
602 /*
603 * We must always trace a few critical system calls in order to
604 * correctly support following forks in the presence of tracing
605 * qualifiers.
606 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000607 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000608#ifdef SYS_fork
609 case SYS_fork:
610#endif
611#ifdef SYS_vfork
612 case SYS_vfork:
613#endif
John Hughes4e36a812001-04-18 15:11:51 +0000614#ifdef SYS_fork1
615 case SYS_fork1:
616#endif
617#ifdef SYS_forkall
618 case SYS_forkall:
619#endif
620#ifdef SYS_rfork1
621 case SYS_rfork1:
622#endif
623#ifdef SYS_rforkall
624 case SYS_rforkall:
625#endif
Roland McGrathf3a0e1b2003-02-20 02:45:22 +0000626#ifdef SYS_rfork
627 case SYS_rfork:
628#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000629 internal_fork(tcp);
630 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000631#ifdef SYS_clone
632 case SYS_clone:
633 internal_clone(tcp);
634 break;
635#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000636#ifdef SYS_clone2
637 case SYS_clone2:
638 internal_clone(tcp);
639 break;
640#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000641#ifdef SYS_execv
642 case SYS_execv:
643#endif
644#ifdef SYS_execve
645 case SYS_execve:
646#endif
John Hughes4e36a812001-04-18 15:11:51 +0000647#ifdef SYS_rexecve
648 case SYS_rexecve:
649#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 internal_exec(tcp);
651 break;
652
653#ifdef SYS_wait
654 case SYS_wait:
655#endif
656#ifdef SYS_wait4
657 case SYS_wait4:
658#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000659#ifdef SYS32_wait4
660 case SYS32_wait4:
661#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000662#ifdef SYS_waitpid
663 case SYS_waitpid:
664#endif
665#ifdef SYS_waitsys
666 case SYS_waitsys:
667#endif
668 internal_wait(tcp);
669 break;
670
671#ifdef SYS_exit
672 case SYS_exit:
673#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000674#ifdef SYS32_exit
675 case SYS32_exit:
676#endif
Roland McGrath923f7502003-01-09 06:53:27 +0000677#ifdef __NR_exit_group
678 case __NR_exit_group:
679#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000680 internal_exit(tcp);
681 break;
682 }
683 return 0;
684}
685
Wichert Akkermanc7926982000-04-10 22:22:31 +0000686
687#ifdef LINUX
688#if defined (I386)
689 static long eax;
690#elif defined (IA64)
691 long r8, r10, psr;
692 long ia32 = 0;
693#elif defined (POWERPC)
694 static long result,flags;
695#elif defined (M68K)
696 static int d0;
697#elif defined (ARM)
698 static int r0;
699#elif defined (ALPHA)
700 static long r0;
701 static long a3;
702#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000703 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000704 static unsigned long trap;
705#elif defined(MIPS)
706 static long a3;
707 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000708#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000709 static long gpr2;
710 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000711 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000712#elif defined(HPPA)
713 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000714#elif defined(SH)
715 static long r0;
Roland McGrathe1e584b2003-06-02 19:18:58 +0000716#elif defined(SHMEDIA)
717 static long r9;
Michal Ludvig0e035502002-09-23 15:41:01 +0000718#elif defined(X86_64)
719 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000720#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000721#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000722#ifdef FREEBSD
723 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000724#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000725
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000727get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728struct tcb *tcp;
729{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000730 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000731#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000732 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000733#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000734
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000735#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000736#if defined(S390) || defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +0000737 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000738 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000739 if (syscall_mode != -ENOSYS) {
Michal Ludvig882eda82002-11-11 12:50:47 +0000740 /*
741 * Since kernel version 2.5.44 the scno gets passed in gpr2.
742 */
743 scno = syscall_mode;
744 }
Roland McGrath96dc5142003-01-20 10:23:04 +0000745 if (tcp->flags & TCB_WAITEXECVE) {
746 /*
747 * When the execve system call completes successfully, the
748 * new process still has -ENOSYS (old style) or __NR_execve
749 * (new style) in gpr2. We cannot recover the scno again
750 * by disassembly, because the image that executed the
751 * syscall is gone now. Fortunately, we don't want it. We
752 * leave the flag set so that syscall_fixup can fake the
753 * result.
754 */
755 if (tcp->flags & TCB_INSYSCALL)
756 return 1;
757 /*
758 * This is the SIGTRAP after execve. We cannot try to read
759 * the system call here either.
760 */
761 tcp->flags &= ~TCB_WAITEXECVE;
762 return 0;
763 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000764 else {
765 /*
766 * Old style of "passing" the scno via the SVC instruction.
767 */
768
769 long opcode, offset_reg, tmp;
770 void * svc_addr;
771 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
772 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
773 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
774 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000775
Michal Ludvig882eda82002-11-11 12:50:47 +0000776 if (upeek(pid, PT_PSWADDR, &pc) < 0)
777 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000778 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000779 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000780 if (errno) {
781 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000782 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000783 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000784
785 /*
786 * We have to check if the SVC got executed directly or via an
787 * EXECUTE instruction. In case of EXECUTE it is necessary to do
788 * instruction decoding to derive the system call number.
789 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
790 * so that this doesn't work if a SVC opcode is part of an EXECUTE
791 * opcode. Since there is no way to find out the opcode size this
792 * is the best we can do...
793 */
794
795 if ((opcode & 0xff00) == 0x0a00) {
796 /* SVC opcode */
797 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000798 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000799 else {
800 /* SVC got executed by EXECUTE instruction */
801
802 /*
803 * Do instruction decoding of EXECUTE. If you really want to
804 * understand this, read the Principles of Operations.
805 */
806 svc_addr = (void *) (opcode & 0xfff);
807
808 tmp = 0;
809 offset_reg = (opcode & 0x000f0000) >> 16;
810 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
811 return -1;
812 svc_addr += tmp;
813
814 tmp = 0;
815 offset_reg = (opcode & 0x0000f000) >> 12;
816 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
817 return -1;
818 svc_addr += tmp;
819
820 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
821 if (errno)
822 return -1;
823#if defined(S390X)
824 scno >>= 48;
825#else
826 scno >>= 16;
827#endif
828 tmp = 0;
829 offset_reg = (opcode & 0x00f00000) >> 20;
830 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
831 return -1;
832
833 scno = (scno | tmp) & 0xff;
834 }
835 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000836#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000837 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838 return -1;
839 if (!(tcp->flags & TCB_INSYSCALL)) {
840 /* Check if we return from execve. */
841 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
842 tcp->flags &= ~TCB_WAITEXECVE;
843 return 0;
844 }
845 }
846#elif defined (I386)
847 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
848 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000849#elif defined (X86_64)
850 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
851 return -1;
852
Roland McGrath761b5d72002-12-15 23:58:31 +0000853 if (!(tcp->flags & TCB_INSYSCALL)) {
854 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000855 long val;
856
857 /* Check CS register value. On x86-64 linux it is:
858 * 0x33 for long mode (64 bit)
859 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000860 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000861 * to be cached.
862 */
863 if (upeek(pid, 8*CS, &val) < 0)
864 return -1;
865 switch(val)
866 {
867 case 0x23: currpers = 1; break;
868 case 0x33: currpers = 0; break;
869 default:
870 fprintf(stderr, "Unknown value CS=0x%02X while "
871 "detecting personality of process "
872 "PID=%d\n", (int)val, pid);
873 currpers = current_personality;
874 break;
875 }
876#if 0
877 /* This version analyzes the opcode of a syscall instruction.
878 * (int 0x80 on i386 vs. syscall on x86-64)
879 * It works, but is too complicated.
880 */
881 unsigned long val, rip, i;
882
883 if(upeek(pid, 8*RIP, &rip)<0)
884 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000885
Michal Ludvig0e035502002-09-23 15:41:01 +0000886 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
887 rip-=2;
888 errno = 0;
889
Roland McGrath761b5d72002-12-15 23:58:31 +0000890 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
891 if (errno)
892 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000893 strerror(errno));
894 switch (call & 0xffff)
895 {
896 /* x86-64: syscall = 0x0f 0x05 */
897 case 0x050f: currpers = 0; break;
898 /* i386: int 0x80 = 0xcd 0x80 */
899 case 0x80cd: currpers = 1; break;
900 default:
901 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000902 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000903 "Unknown syscall opcode (0x%04X) while "
904 "detecting personality of process "
905 "PID=%d\n", (int)call, pid);
906 break;
907 }
908#endif
909 if(currpers != current_personality)
910 {
911 char *names[]={"64 bit", "32 bit"};
912 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000913 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000914 pid, names[current_personality]);
915 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000916 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000917#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000918# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000919 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000920 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000921 if (!(tcp->flags & TCB_INSYSCALL)) {
922 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000923 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000924 return -1;
925 } else {
926 if (upeek (pid, PT_R15, &scno) < 0)
927 return -1;
928 }
Roland McGrathba954762003-03-05 06:29:06 +0000929 /* Check if we return from execve. */
930 if (tcp->flags & TCB_WAITEXECVE) {
931 tcp->flags &= ~TCB_WAITEXECVE;
932 return 0;
933 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000934 } else {
935 /* syscall in progress */
936 if (upeek (pid, PT_R8, &r8) < 0)
937 return -1;
938 if (upeek (pid, PT_R10, &r10) < 0)
939 return -1;
940 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000941#elif defined (ARM)
Roland McGrath761b5d72002-12-15 23:58:31 +0000942 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000943 long pc;
944 upeek(pid, 4*15, &pc);
945 umoven(tcp, pc-4, 4, (char *)&scno);
946 scno &= 0x000fffff;
947 }
948#elif defined (M68K)
949 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
950 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000951#elif defined (MIPS)
952 if (upeek(pid, REG_A3, &a3) < 0)
953 return -1;
954
955 if(!(tcp->flags & TCB_INSYSCALL)) {
956 if (upeek(pid, REG_V0, &scno) < 0)
957 return -1;
958
959 if (scno < 0 || scno > nsyscalls) {
960 if(a3 == 0 || a3 == -1) {
961 if(debug)
962 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
963 return 0;
964 }
965 }
966 } else {
967 if (upeek(pid, REG_V0, &r2) < 0)
968 return -1;
969 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000970#elif defined (ALPHA)
971 if (upeek(pid, REG_A3, &a3) < 0)
972 return -1;
973
974 if (!(tcp->flags & TCB_INSYSCALL)) {
975 if (upeek(pid, REG_R0, &scno) < 0)
976 return -1;
977
978 /* Check if we return from execve. */
979 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
980 tcp->flags &= ~TCB_WAITEXECVE;
981 return 0;
982 }
983
984 /*
985 * Do some sanity checks to figure out if it's
986 * really a syscall entry
987 */
988 if (scno < 0 || scno > nsyscalls) {
989 if (a3 == 0 || a3 == -1) {
990 if (debug)
991 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
992 return 0;
993 }
994 }
995 }
996 else {
997 if (upeek(pid, REG_R0, &r0) < 0)
998 return -1;
999 }
1000#elif defined (SPARC)
1001 /* Everything we need is in the current register set. */
1002 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1003 return -1;
1004
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001005 /* If we are entering, then disassemble the syscall trap. */
1006 if (!(tcp->flags & TCB_INSYSCALL)) {
1007 /* Retrieve the syscall trap instruction. */
1008 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001009 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001010 if (errno)
1011 return -1;
1012
1013 /* Disassemble the trap to see what personality to use. */
1014 switch (trap) {
1015 case 0x91d02010:
1016 /* Linux/SPARC syscall trap. */
1017 set_personality(0);
1018 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001019 case 0x91d0206d:
1020 /* Linux/SPARC64 syscall trap. */
1021 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1022 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001023 case 0x91d02000:
1024 /* SunOS syscall trap. (pers 1) */
1025 fprintf(stderr,"syscall: SunOS no support\n");
1026 return -1;
1027 case 0x91d02008:
1028 /* Solaris 2.x syscall trap. (per 2) */
1029 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001030 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001031 case 0x91d02009:
1032 /* NetBSD/FreeBSD syscall trap. */
1033 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1034 return -1;
1035 case 0x91d02027:
1036 /* Solaris 2.x gettimeofday */
1037 set_personality(1);
1038 break;
1039 default:
1040 /* Unknown syscall trap. */
1041 if(tcp->flags & TCB_WAITEXECVE) {
1042 tcp->flags &= ~TCB_WAITEXECVE;
1043 return 0;
1044 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001045 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001046 return -1;
1047 }
1048
1049 /* Extract the system call number from the registers. */
1050 if (trap == 0x91d02027)
1051 scno = 156;
1052 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001053 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001054 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001055 scno = regs.r_o0;
1056 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001057 }
1058 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001059#elif defined(HPPA)
1060 if (upeek(pid, PT_GR20, &scno) < 0)
1061 return -1;
1062 if (!(tcp->flags & TCB_INSYSCALL)) {
1063 /* Check if we return from execve. */
1064 if ((tcp->flags & TCB_WAITEXECVE)) {
1065 tcp->flags &= ~TCB_WAITEXECVE;
1066 return 0;
1067 }
1068 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001069#elif defined(SH)
1070 /*
1071 * In the new syscall ABI, the system call number is in R3.
1072 */
1073 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1074 return -1;
1075
1076 if (scno < 0) {
1077 /* Odd as it may seem, a glibc bug has been known to cause
1078 glibc to issue bogus negative syscall numbers. So for
1079 our purposes, make strace print what it *should* have been */
1080 long correct_scno = (scno & 0xff);
1081 if (debug)
1082 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001083 "Detected glibc bug: bogus system call number = %ld, "
1084 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001085 scno,
1086 correct_scno);
1087 scno = correct_scno;
1088 }
1089
1090
1091 if (!(tcp->flags & TCB_INSYSCALL)) {
1092 /* Check if we return from execve. */
1093 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1094 tcp->flags &= ~TCB_WAITEXECVE;
1095 return 0;
1096 }
1097 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001098#elif defined(SHMEDIA)
1099 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1100 return -1;
1101 scno &= 0xFFFF;
1102
1103 if (!(tcp->flags & TCB_INSYSCALL)) {
1104 /* Check if we return from execve. */
1105 if (tcp->flags & TCB_WAITEXECVE) {
1106 tcp->flags &= ~TCB_WAITEXECVE;
1107 return 0;
1108 }
1109 }
1110#endif /* SHMEDIA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001111#endif /* LINUX */
1112#ifdef SUNOS4
1113 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1114 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001115#elif defined(SH)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001116 /* new syscall ABI returns result in R0 */
1117 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1118 return -1;
1119#elif defined(SHMEDIA)
1120 /* ABI defines result returned in r9 */
1121 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1122 return -1;
1123
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001125#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001127 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001128#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001129#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001130 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001131#else /* FREEBSD */
1132 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1133 perror("pread");
1134 return -1;
1135 }
1136 switch (regs.r_eax) {
1137 case SYS_syscall:
1138 case SYS___syscall:
1139 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1140 break;
1141 default:
1142 scno = regs.r_eax;
1143 break;
1144 }
1145#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001146#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001147#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001148 if (!(tcp->flags & TCB_INSYSCALL))
1149 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001150 return 1;
1151}
1152
Pavel Machek4dc3b142000-02-01 17:58:41 +00001153
1154int
1155syscall_fixup(tcp)
1156struct tcb *tcp;
1157{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001158#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001159 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001160#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001161 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001162
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001163 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001164 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001165 if (
1166 scno == SYS_fork
1167#ifdef SYS_vfork
1168 || scno == SYS_vfork
1169#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001170#ifdef SYS_fork1
1171 || scno == SYS_fork1
1172#endif /* SYS_fork1 */
1173#ifdef SYS_forkall
1174 || scno == SYS_forkall
1175#endif /* SYS_forkall */
1176#ifdef SYS_rfork1
1177 || scno == SYS_rfork1
1178#endif /* SYS_fork1 */
1179#ifdef SYS_rforkall
1180 || scno == SYS_rforkall
1181#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001182 ) {
1183 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001184 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001185 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001186 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001187 }
1188 else {
1189 fprintf(stderr, "syscall: missing entry\n");
1190 tcp->flags |= TCB_INSYSCALL;
1191 }
1192 }
1193 }
1194 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001195 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001196 fprintf(stderr, "syscall: missing exit\n");
1197 tcp->flags &= ~TCB_INSYSCALL;
1198 }
1199 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001200#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001201#ifdef SUNOS4
1202 if (!(tcp->flags & TCB_INSYSCALL)) {
1203 if (scno == 0) {
1204 fprintf(stderr, "syscall: missing entry\n");
1205 tcp->flags |= TCB_INSYSCALL;
1206 }
1207 }
1208 else {
1209 if (scno != 0) {
1210 if (debug) {
1211 /*
1212 * This happens when a signal handler
1213 * for a signal which interrupted a
1214 * a system call makes another system call.
1215 */
1216 fprintf(stderr, "syscall: missing exit\n");
1217 }
1218 tcp->flags &= ~TCB_INSYSCALL;
1219 }
1220 }
1221#endif /* SUNOS4 */
1222#ifdef LINUX
1223#if defined (I386)
1224 if (upeek(pid, 4*EAX, &eax) < 0)
1225 return -1;
1226 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1227 if (debug)
1228 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1229 return 0;
1230 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001231#elif defined (X86_64)
1232 if (upeek(pid, 8*RAX, &rax) < 0)
1233 return -1;
1234 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1235 if (debug)
1236 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1237 return 0;
1238 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001239#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001240 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1241 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001242 if (syscall_mode != -ENOSYS)
1243 syscall_mode = tcp->scno;
1244 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001245 if (debug)
1246 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1247 return 0;
1248 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001249 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1250 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1251 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1252 /*
1253 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1254 * flag set for the post-execve SIGTRAP to see and reset.
1255 */
1256 gpr2 = 0;
1257 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001258#elif defined (POWERPC)
1259# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001260 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001261 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001262 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001263 return -1;
1264 if (flags & SO_MASK)
1265 result = -result;
1266#elif defined (M68K)
1267 if (upeek(pid, 4*PT_D0, &d0) < 0)
1268 return -1;
1269 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1270 if (debug)
1271 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1272 return 0;
1273 }
1274#elif defined (ARM)
1275 if (upeek(pid, 4*0, (long *)&r0) < 0)
1276 return -1;
1277 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1278 if (debug)
1279 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1280 return 0;
1281 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001282#elif defined (HPPA)
1283 if (upeek(pid, PT_GR28, &r28) < 0)
1284 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001285#elif defined(IA64)
1286 if (upeek(pid, PT_R10, &r10) < 0)
1287 return -1;
1288 if (upeek(pid, PT_R8, &r8) < 0)
1289 return -1;
1290 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1291 if (debug)
1292 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1293 return 0;
1294 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001295#endif
1296#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001297 return 1;
1298}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001299
Pavel Machek4dc3b142000-02-01 17:58:41 +00001300int
1301get_error(tcp)
1302struct tcb *tcp;
1303{
1304 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001305#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001306#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001307 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1308 tcp->u_rval = -1;
1309 u_error = -gpr2;
1310 }
1311 else {
1312 tcp->u_rval = gpr2;
1313 u_error = 0;
1314 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001315#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001316#ifdef I386
1317 if (eax < 0 && -eax < nerrnos) {
1318 tcp->u_rval = -1;
1319 u_error = -eax;
1320 }
1321 else {
1322 tcp->u_rval = eax;
1323 u_error = 0;
1324 }
1325#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001326#ifdef X86_64
1327 if (rax < 0 && -rax < nerrnos) {
1328 tcp->u_rval = -1;
1329 u_error = -rax;
1330 }
1331 else {
1332 tcp->u_rval = rax;
1333 u_error = 0;
1334 }
1335#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001336#ifdef IA64
1337 if (ia32) {
1338 int err;
1339
1340 err = (int)r8;
1341 if (err < 0 && -err < nerrnos) {
1342 tcp->u_rval = -1;
1343 u_error = -err;
1344 }
1345 else {
1346 tcp->u_rval = err;
1347 u_error = 0;
1348 }
1349 } else {
1350 if (r10) {
1351 tcp->u_rval = -1;
1352 u_error = r8;
1353 } else {
1354 tcp->u_rval = r8;
1355 u_error = 0;
1356 }
1357 }
1358#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001359#ifdef MIPS
1360 if (a3) {
1361 tcp->u_rval = -1;
1362 u_error = r2;
1363 } else {
1364 tcp->u_rval = r2;
1365 u_error = 0;
1366 }
1367#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001368#ifdef POWERPC
1369 if (result && (unsigned) -result < nerrnos) {
1370 tcp->u_rval = -1;
1371 u_error = -result;
1372 }
1373 else {
1374 tcp->u_rval = result;
1375 u_error = 0;
1376 }
1377#else /* !POWERPC */
1378#ifdef M68K
1379 if (d0 && (unsigned) -d0 < nerrnos) {
1380 tcp->u_rval = -1;
1381 u_error = -d0;
1382 }
1383 else {
1384 tcp->u_rval = d0;
1385 u_error = 0;
1386 }
1387#else /* !M68K */
1388#ifdef ARM
1389 if (r0 && (unsigned) -r0 < nerrnos) {
1390 tcp->u_rval = -1;
1391 u_error = -r0;
1392 }
1393 else {
1394 tcp->u_rval = r0;
1395 u_error = 0;
1396 }
1397#else /* !ARM */
1398#ifdef ALPHA
1399 if (a3) {
1400 tcp->u_rval = -1;
1401 u_error = r0;
1402 }
1403 else {
1404 tcp->u_rval = r0;
1405 u_error = 0;
1406 }
1407#else /* !ALPHA */
1408#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001409 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001410 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001411 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001412 }
1413 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001414 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001415 u_error = 0;
1416 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001417#else /* !SPARC */
1418#ifdef HPPA
1419 if (r28 && (unsigned) -r28 < nerrnos) {
1420 tcp->u_rval = -1;
1421 u_error = -r28;
1422 }
1423 else {
1424 tcp->u_rval = r28;
1425 u_error = 0;
1426 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001427#else
1428#ifdef SH
1429 /* interpret R0 as return value or error number */
1430 if (r0 && (unsigned) -r0 < nerrnos) {
1431 tcp->u_rval = -1;
1432 u_error = -r0;
1433 }
1434 else {
1435 tcp->u_rval = r0;
1436 u_error = 0;
1437 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001438#else
1439#ifdef SHMEDIA
1440 /* interpret result as return value or error number */
1441 if (r9 && (unsigned) -r9 < nerrnos) {
1442 tcp->u_rval = -1;
1443 u_error = -r9;
1444 }
1445 else {
1446 tcp->u_rval = r9;
1447 u_error = 0;
1448 }
1449#endif /* SHMEDIA */
Wichert Akkermanccef6372002-05-01 16:39:22 +00001450#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001451#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001452#endif /* SPARC */
1453#endif /* ALPHA */
1454#endif /* ARM */
1455#endif /* M68K */
1456#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001457#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001458#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001459#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001460#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001461#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001462#endif /* LINUX */
1463#ifdef SUNOS4
1464 /* get error code from user struct */
1465 if (upeek(pid, uoff(u_error), &u_error) < 0)
1466 return -1;
1467 u_error >>= 24; /* u_error is a char */
1468
1469 /* get system call return value */
1470 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1471 return -1;
1472#endif /* SUNOS4 */
1473#ifdef SVR4
1474#ifdef SPARC
1475 /* Judicious guessing goes a long way. */
1476 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1477 tcp->u_rval = -1;
1478 u_error = tcp->status.pr_reg[R_O0];
1479 }
1480 else {
1481 tcp->u_rval = tcp->status.pr_reg[R_O0];
1482 u_error = 0;
1483 }
1484#endif /* SPARC */
1485#ifdef I386
1486 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001487 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001488 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001489 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001490 }
1491 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001492 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001493#ifdef HAVE_LONG_LONG
1494 tcp->u_lrval =
1495 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1496 tcp->status.PR_REG[EAX];
1497#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001498 u_error = 0;
1499 }
1500#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001501#ifdef X86_64
1502 /* Wanna know how to kill an hour single-stepping? */
1503 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1504 tcp->u_rval = -1;
1505 u_error = tcp->status.PR_REG[RAX];
1506 }
1507 else {
1508 tcp->u_rval = tcp->status.PR_REG[RAX];
1509 u_error = 0;
1510 }
1511#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001512#ifdef MIPS
1513 if (tcp->status.pr_reg[CTX_A3]) {
1514 tcp->u_rval = -1;
1515 u_error = tcp->status.pr_reg[CTX_V0];
1516 }
1517 else {
1518 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1519 u_error = 0;
1520 }
1521#endif /* MIPS */
1522#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001523#ifdef FREEBSD
1524 if (regs.r_eflags & PSL_C) {
1525 tcp->u_rval = -1;
1526 u_error = regs.r_eax;
1527 } else {
1528 tcp->u_rval = regs.r_eax;
1529 tcp->u_lrval =
1530 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1531 u_error = 0;
1532 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001533#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001534 tcp->u_error = u_error;
1535 return 1;
1536}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001537
Roland McGrathb69f81b2002-12-21 23:25:18 +00001538int
1539force_result(tcp, error, rval)
1540 struct tcb *tcp;
1541 int error;
1542 long rval;
1543{
1544#ifdef LINUX
1545#if defined(S390) || defined(S390X)
1546 gpr2 = error ? -error : rval;
Roland McGrathd38809b2003-01-10 07:50:21 +00001547 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001548 return -1;
1549 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1550 return -1;
1551#else /* !S390 && !S390X */
1552#ifdef I386
1553 eax = error ? -error : rval;
1554 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1555 return -1;
1556#else /* !I386 */
1557#ifdef X86_64
1558 rax = error ? -error : rval;
1559 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1560 return -1;
1561#else
1562#ifdef IA64
1563 if (ia32) {
1564 r8 = error ? -error : rval;
1565 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1566 return -1;
1567 }
1568 else {
1569 if (error) {
1570 r8 = error;
1571 r10 = -1;
1572 }
1573 else {
1574 r8 = rval;
1575 r10 = 0;
1576 }
1577 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1578 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1579 return -1;
1580 }
1581#else /* !IA64 */
1582#ifdef MIPS
1583 if (error) {
1584 r2 = error;
1585 a3 = -1;
1586 }
1587 else {
1588 r2 = rval;
1589 a3 = 0;
1590 }
1591 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1592 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1593 return -1;
1594#else
1595#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001596 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001597 return -1;
1598 if (error) {
1599 flags |= SO_MASK;
1600 result = error;
1601 }
1602 else {
1603 flags &= ~SO_MASK;
1604 result = rval;
1605 }
Roland McGratheb285352003-01-14 09:59:00 +00001606 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1607 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001608 return -1;
1609#else /* !POWERPC */
1610#ifdef M68K
1611 d0 = error ? -error : rval;
1612 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1613 return -1;
1614#else /* !M68K */
1615#ifdef ARM
1616 r0 = error ? -error : rval;
1617 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1618 return -1;
1619#else /* !ARM */
1620#ifdef ALPHA
1621 if (error) {
1622 a3 = -1;
1623 r0 = error;
1624 }
1625 else {
1626 a3 = 0;
1627 r0 = rval;
1628 }
1629 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1630 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1631 return -1;
1632#else /* !ALPHA */
1633#ifdef SPARC
1634 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1635 return -1;
1636 if (error) {
1637 regs.r_psr |= PSR_C;
1638 regs.r_o0 = error;
1639 }
1640 else {
1641 regs.r_psr &= ~PSR_C;
1642 regs.r_o0 = rval;
1643 }
1644 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1645 return -1;
1646#else /* !SPARC */
1647#ifdef HPPA
1648 r28 = error ? -error : rval;
1649 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1650 return -1;
1651#else
1652#ifdef SH
1653 r0 = error ? -error : rval;
1654 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1655 return -1;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001656#else
1657#ifdef SHMEDIA
1658 r9 = error ? -error : rval;
1659 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1660 return -1;
1661#endif /* SHMEDIA */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001662#endif /* SH */
1663#endif /* HPPA */
1664#endif /* SPARC */
1665#endif /* ALPHA */
1666#endif /* ARM */
1667#endif /* M68K */
1668#endif /* POWERPC */
1669#endif /* MIPS */
1670#endif /* IA64 */
1671#endif /* X86_64 */
1672#endif /* I386 */
1673#endif /* S390 || S390X */
1674#endif /* LINUX */
1675#ifdef SUNOS4
1676 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1677 error << 24) < 0 ||
1678 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1679 return -1;
1680#endif /* SUNOS4 */
1681#ifdef SVR4
1682 /* XXX no clue */
1683 return -1;
1684#endif /* SVR4 */
1685#ifdef FREEBSD
1686 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1687 perror("pread");
1688 return -1;
1689 }
1690 if (error) {
1691 regs.r_eflags |= PSL_C;
1692 regs.r_eax = error;
1693 }
1694 else {
1695 regs.r_eflags &= ~PSL_C;
1696 regs.r_eax = rval;
1697 }
1698 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1699 perror("pwrite");
1700 return -1;
1701 }
1702#endif /* FREEBSD */
1703
1704 /* All branches reach here on success (only). */
1705 tcp->u_error = error;
1706 tcp->u_rval = rval;
1707 return 0;
1708}
1709
Pavel Machek4dc3b142000-02-01 17:58:41 +00001710int syscall_enter(tcp)
1711struct tcb *tcp;
1712{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001713#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001714 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001715#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001716#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001717#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001718 {
1719 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001720 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1721 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001722 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001723 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001724 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001725 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001726 return -1;
1727 }
1728 }
1729#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001730 {
1731 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001732 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1733 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001734 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001735 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001736 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001737 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1738 * for scno somewhere above here!
1739 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001740 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1741 return -1;
1742 }
1743 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001744#elif defined (IA64)
1745 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001746 if (!ia32) {
1747 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1748 /* be backwards compatible with kernel < 2.4.4... */
1749# ifndef PT_RBS_END
1750# define PT_RBS_END PT_AR_BSP
1751# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001752
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001753 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001754 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001755 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1756 return -1;
1757
1758 sof = (cfm >> 0) & 0x7f;
1759 sol = (cfm >> 7) & 0x7f;
1760 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1761
1762 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1763 && sysent[tcp->scno].nargs != -1)
1764 tcp->u_nargs = sysent[tcp->scno].nargs;
1765 else
1766 tcp->u_nargs = MAX_ARGS;
1767 for (i = 0; i < tcp->u_nargs; ++i) {
1768 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1769 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1770 return -1;
1771 }
1772 } else {
1773 int i;
1774
1775 if (/* EBX = out0 */
1776 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1777 /* ECX = out1 */
1778 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1779 /* EDX = out2 */
1780 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1781 /* ESI = out3 */
1782 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1783 /* EDI = out4 */
1784 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1785 /* EBP = out5 */
1786 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1787 return -1;
1788
1789 for (i = 0; i < 6; ++i)
1790 /* truncate away IVE sign-extension */
1791 tcp->u_arg[i] &= 0xffffffff;
1792
1793 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1794 && sysent[tcp->scno].nargs != -1)
1795 tcp->u_nargs = sysent[tcp->scno].nargs;
1796 else
1797 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001798 }
1799 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001800#elif defined (MIPS)
1801 {
1802 long sp;
1803 int i, nargs;
1804
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001805 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1806 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001807 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001808 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001809 if(nargs > 4) {
1810 if(upeek(pid, REG_SP, &sp) < 0)
1811 return -1;
1812 for(i = 0; i < 4; i++) {
1813 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1814 return -1;
1815 }
1816 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1817 (char *)(tcp->u_arg + 4));
1818 } else {
1819 for(i = 0; i < nargs; i++) {
1820 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1821 return -1;
1822 }
1823 }
1824 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001825#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001826#ifndef PT_ORIG_R3
1827#define PT_ORIG_R3 34
1828#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001829 {
1830 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001831 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1832 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001833 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001834 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001835 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001836 if (upeek(pid, (i==0) ?
1837 (sizeof(unsigned long)*PT_ORIG_R3) :
1838 ((i+PT_R3)*sizeof(unsigned long)),
1839 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001840 return -1;
1841 }
1842 }
1843#elif defined (SPARC)
1844 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001845 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001846
1847 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1848 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001849 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001850 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001851 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001852 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001853 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001854#elif defined (HPPA)
1855 {
1856 int i;
1857
1858 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1859 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001860 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001861 tcp->u_nargs = MAX_ARGS;
1862 for (i = 0; i < tcp->u_nargs; i++) {
1863 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1864 return -1;
1865 }
1866 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001867#elif defined(SH)
1868 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001869 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001870 static int syscall_regs[] = {
1871 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1872 REG_REG0, REG_REG0+1, REG_REG0+2
1873 };
1874
1875 tcp->u_nargs = sysent[tcp->scno].nargs;
1876 for (i = 0; i < tcp->u_nargs; i++) {
1877 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1878 return -1;
1879 }
1880 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001881#elif defined(SHMEDIA)
1882 {
1883 int i;
1884 /* Registers used by SH5 Linux system calls for parameters */
1885 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1886
1887 /*
1888 * TODO: should also check that the number of arguments encoded
1889 * in the trap number matches the number strace expects.
1890 */
1891 /*
1892 assert(sysent[tcp->scno].nargs <
1893 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1894 */
1895
1896 tcp->u_nargs = sysent[tcp->scno].nargs;
1897 for (i = 0; i < tcp->u_nargs; i++) {
1898 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1899 return -1;
1900 }
1901 }
1902
Michal Ludvig0e035502002-09-23 15:41:01 +00001903#elif defined(X86_64)
1904 {
1905 int i;
1906 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1907 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1908 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1909 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001910
Michal Ludvig0e035502002-09-23 15:41:01 +00001911 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1912 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001913 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001914 tcp->u_nargs = MAX_ARGS;
1915 for (i = 0; i < tcp->u_nargs; i++) {
1916 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1917 return -1;
1918 }
1919 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001920#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001921 {
1922 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001923 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1924 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001925 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001926 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001927 for (i = 0; i < tcp->u_nargs; i++) {
1928 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1929 return -1;
1930 }
1931 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001932#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001933#endif /* LINUX */
1934#ifdef SUNOS4
1935 {
1936 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001937 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1938 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001939 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001940 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001941 for (i = 0; i < tcp->u_nargs; i++) {
1942 struct user *u;
1943
1944 if (upeek(pid, uoff(u_arg[0]) +
1945 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1946 return -1;
1947 }
1948 }
1949#endif /* SUNOS4 */
1950#ifdef SVR4
1951#ifdef MIPS
1952 /*
1953 * SGI is broken: even though it has pr_sysarg, it doesn't
1954 * set them on system call entry. Get a clue.
1955 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001956 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001957 tcp->u_nargs = sysent[tcp->scno].nargs;
1958 else
1959 tcp->u_nargs = tcp->status.pr_nsysarg;
1960 if (tcp->u_nargs > 4) {
1961 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1962 4*sizeof(tcp->u_arg[0]));
1963 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1964 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1965 }
1966 else {
1967 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1968 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1969 }
John Hughes25299712001-03-06 10:10:06 +00001970#elif UNIXWARE >= 2
1971 /*
1972 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1973 */
1974 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1975 tcp->u_nargs = sysent[tcp->scno].nargs;
1976 else
1977 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1978 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1979 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1980#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001981 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001982 tcp->u_nargs = sysent[tcp->scno].nargs;
1983 else
1984 tcp->u_nargs = tcp->status.pr_nsysarg;
1985 {
1986 int i;
1987 for (i = 0; i < tcp->u_nargs; i++)
1988 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1989 }
John Hughes25299712001-03-06 10:10:06 +00001990#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001991 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001992 tcp->u_nargs = sysent[tcp->scno].nargs;
1993 else
1994 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001995 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001996 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001997#else
1998 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001999#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002000#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002001#ifdef FREEBSD
2002 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2003 sysent[tcp->scno].nargs > tcp->status.val)
2004 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002005 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002006 tcp->u_nargs = tcp->status.val;
2007 if (tcp->u_nargs < 0)
2008 tcp->u_nargs = 0;
2009 if (tcp->u_nargs > MAX_ARGS)
2010 tcp->u_nargs = MAX_ARGS;
2011 switch(regs.r_eax) {
2012 case SYS___syscall:
2013 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2014 regs.r_esp + sizeof(int) + sizeof(quad_t));
2015 break;
2016 case SYS_syscall:
2017 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2018 regs.r_esp + 2 * sizeof(int));
2019 break;
2020 default:
2021 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2022 regs.r_esp + sizeof(int));
2023 break;
2024 }
2025#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002026 return 1;
2027}
2028
2029int
2030trace_syscall(tcp)
2031struct tcb *tcp;
2032{
2033 int sys_res;
2034 struct timeval tv;
2035 int res;
2036
2037 /* Measure the exit time as early as possible to avoid errors. */
2038 if (dtime && (tcp->flags & TCB_INSYSCALL))
2039 gettimeofday(&tv, NULL);
2040
2041 res = get_scno(tcp);
2042 if (res != 1)
2043 return res;
2044
2045 res = syscall_fixup(tcp);
2046 if (res != 1)
2047 return res;
2048
2049 if (tcp->flags & TCB_INSYSCALL) {
2050 long u_error;
2051 res = get_error(tcp);
2052 if (res != 1)
2053 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002054
2055 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002056 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2057 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002058 tcp->flags &= ~TCB_INSYSCALL;
2059 return 0;
2060 }
2061
2062 if (tcp->flags & TCB_REPRINT) {
2063 printleader(tcp);
2064 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002065 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002066 tprintf("syscall_%lu", tcp->scno);
2067 else
2068 tprintf("%s", sysent[tcp->scno].sys_name);
2069 tprintf(" resumed> ");
2070 }
2071
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002072 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002073 call_count[tcp->scno]++;
2074 if (tcp->u_error)
2075 error_count[tcp->scno]++;
2076 tv_sub(&tv, &tv, &tcp->etime);
2077#ifdef LINUX
2078 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2079 static struct timeval one_tick =
2080 { 0, 1000000 / HZ };
2081
2082 if (tv_nz(&tcp->dtime))
2083 tv = tcp->dtime;
2084 else if (tv_cmp(&tv, &one_tick) > 0) {
2085 if (tv_cmp(&shortest, &one_tick) < 0)
2086 tv = shortest;
2087 else
2088 tv = one_tick;
2089 }
2090 }
2091#endif /* LINUX */
2092 if (tv_cmp(&tv, &shortest) < 0)
2093 shortest = tv;
2094 tv_add(&tv_count[tcp->scno],
2095 &tv_count[tcp->scno], &tv);
2096 tcp->flags &= ~TCB_INSYSCALL;
2097 return 0;
2098 }
2099
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002100 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002101 || (qual_flags[tcp->scno] & QUAL_RAW))
2102 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002103 else {
2104 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002105 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002106 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002107 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002108 u_error = tcp->u_error;
2109 tprintf(") ");
2110 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002111 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2112 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002113 if (u_error)
2114 tprintf("= -1 (errno %ld)", u_error);
2115 else
2116 tprintf("= %#lx", tcp->u_rval);
2117 }
2118 else if (!(sys_res & RVAL_NONE) && u_error) {
2119 switch (u_error) {
2120#ifdef LINUX
2121 case ERESTARTSYS:
2122 tprintf("= ? ERESTARTSYS (To be restarted)");
2123 break;
2124 case ERESTARTNOINTR:
2125 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2126 break;
2127 case ERESTARTNOHAND:
2128 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2129 break;
2130#endif /* LINUX */
2131 default:
2132 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002133 if (u_error < 0)
2134 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002135 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002136 tprintf("%s (%s)", errnoent[u_error],
2137 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002138 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002139 tprintf("ERRNO_%ld (%s)", u_error,
2140 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002141 break;
2142 }
2143 }
2144 else {
2145 if (sys_res & RVAL_NONE)
2146 tprintf("= ?");
2147 else {
2148 switch (sys_res & RVAL_MASK) {
2149 case RVAL_HEX:
2150 tprintf("= %#lx", tcp->u_rval);
2151 break;
2152 case RVAL_OCTAL:
2153 tprintf("= %#lo", tcp->u_rval);
2154 break;
2155 case RVAL_UDECIMAL:
2156 tprintf("= %lu", tcp->u_rval);
2157 break;
2158 case RVAL_DECIMAL:
2159 tprintf("= %ld", tcp->u_rval);
2160 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002161#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002162 case RVAL_LHEX:
2163 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002164 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002165 case RVAL_LOCTAL:
2166 tprintf("= %#llo", tcp->u_lrval);
2167 break;
2168 case RVAL_LUDECIMAL:
2169 tprintf("= %llu", tcp->u_lrval);
2170 break;
2171 case RVAL_LDECIMAL:
2172 tprintf("= %lld", tcp->u_lrval);
2173 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002174#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002175 default:
2176 fprintf(stderr,
2177 "invalid rval format\n");
2178 break;
2179 }
2180 }
2181 if ((sys_res & RVAL_STR) && tcp->auxstr)
2182 tprintf(" (%s)", tcp->auxstr);
2183 }
2184 if (dtime) {
2185 tv_sub(&tv, &tv, &tcp->etime);
2186 tprintf(" <%ld.%06ld>",
2187 (long) tv.tv_sec, (long) tv.tv_usec);
2188 }
2189 printtrailer(tcp);
2190
2191 dumpio(tcp);
2192 if (fflush(tcp->outf) == EOF)
2193 return -1;
2194 tcp->flags &= ~TCB_INSYSCALL;
2195 return 0;
2196 }
2197
2198 /* Entering system call */
2199 res = syscall_enter(tcp);
2200 if (res != 1)
2201 return res;
2202
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002203 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002204#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002205#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002206 case SYS_socketcall:
2207 decode_subcall(tcp, SYS_socket_subcall,
2208 SYS_socket_nsubcalls, deref_style);
2209 break;
2210 case SYS_ipc:
2211 decode_subcall(tcp, SYS_ipc_subcall,
2212 SYS_ipc_nsubcalls, shift_style);
2213 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002214#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002215#ifdef SPARC
2216 case SYS_socketcall:
2217 sparc_socket_decode (tcp);
2218 break;
2219#endif
2220#endif /* LINUX */
2221#ifdef SVR4
2222#ifdef SYS_pgrpsys_subcall
2223 case SYS_pgrpsys:
2224 decode_subcall(tcp, SYS_pgrpsys_subcall,
2225 SYS_pgrpsys_nsubcalls, shift_style);
2226 break;
2227#endif /* SYS_pgrpsys_subcall */
2228#ifdef SYS_sigcall_subcall
2229 case SYS_sigcall:
2230 decode_subcall(tcp, SYS_sigcall_subcall,
2231 SYS_sigcall_nsubcalls, mask_style);
2232 break;
2233#endif /* SYS_sigcall_subcall */
2234 case SYS_msgsys:
2235 decode_subcall(tcp, SYS_msgsys_subcall,
2236 SYS_msgsys_nsubcalls, shift_style);
2237 break;
2238 case SYS_shmsys:
2239 decode_subcall(tcp, SYS_shmsys_subcall,
2240 SYS_shmsys_nsubcalls, shift_style);
2241 break;
2242 case SYS_semsys:
2243 decode_subcall(tcp, SYS_semsys_subcall,
2244 SYS_semsys_nsubcalls, shift_style);
2245 break;
2246#if 0 /* broken */
2247 case SYS_utssys:
2248 decode_subcall(tcp, SYS_utssys_subcall,
2249 SYS_utssys_nsubcalls, shift_style);
2250 break;
2251#endif
2252 case SYS_sysfs:
2253 decode_subcall(tcp, SYS_sysfs_subcall,
2254 SYS_sysfs_nsubcalls, shift_style);
2255 break;
2256 case SYS_spcall:
2257 decode_subcall(tcp, SYS_spcall_subcall,
2258 SYS_spcall_nsubcalls, shift_style);
2259 break;
2260#ifdef SYS_context_subcall
2261 case SYS_context:
2262 decode_subcall(tcp, SYS_context_subcall,
2263 SYS_context_nsubcalls, shift_style);
2264 break;
2265#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002266#ifdef SYS_door_subcall
2267 case SYS_door:
2268 decode_subcall(tcp, SYS_door_subcall,
2269 SYS_door_nsubcalls, door_style);
2270 break;
2271#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002272#ifdef SYS_kaio_subcall
2273 case SYS_kaio:
2274 decode_subcall(tcp, SYS_kaio_subcall,
2275 SYS_kaio_nsubcalls, shift_style);
2276 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002277#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002278#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002279#ifdef FREEBSD
2280 case SYS_msgsys:
2281 case SYS_shmsys:
2282 case SYS_semsys:
2283 decode_subcall(tcp, 0, 0, table_style);
2284 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002285#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002286#ifdef SUNOS4
2287 case SYS_semsys:
2288 decode_subcall(tcp, SYS_semsys_subcall,
2289 SYS_semsys_nsubcalls, shift_style);
2290 break;
2291 case SYS_msgsys:
2292 decode_subcall(tcp, SYS_msgsys_subcall,
2293 SYS_msgsys_nsubcalls, shift_style);
2294 break;
2295 case SYS_shmsys:
2296 decode_subcall(tcp, SYS_shmsys_subcall,
2297 SYS_shmsys_nsubcalls, shift_style);
2298 break;
2299#endif
2300 }
2301
2302 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002303 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002304 tcp->flags |= TCB_INSYSCALL;
2305 return 0;
2306 }
2307
2308 if (cflag) {
2309 gettimeofday(&tcp->etime, NULL);
2310 tcp->flags |= TCB_INSYSCALL;
2311 return 0;
2312 }
2313
2314 printleader(tcp);
2315 tcp->flags &= ~TCB_REPRINT;
2316 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002317 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002318 tprintf("syscall_%lu(", tcp->scno);
2319 else
2320 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002321 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002322 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2323 sys_res = printargs(tcp);
2324 else
2325 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2326 if (fflush(tcp->outf) == EOF)
2327 return -1;
2328 tcp->flags |= TCB_INSYSCALL;
2329 /* Measure the entrance time as late as possible to avoid errors. */
2330 if (dtime)
2331 gettimeofday(&tcp->etime, NULL);
2332 return sys_res;
2333}
2334
2335int
2336printargs(tcp)
2337struct tcb *tcp;
2338{
2339 if (entering(tcp)) {
2340 int i;
2341
2342 for (i = 0; i < tcp->u_nargs; i++)
2343 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2344 }
2345 return 0;
2346}
2347
2348long
2349getrval2(tcp)
2350struct tcb *tcp;
2351{
2352 long val = -1;
2353
2354#ifdef LINUX
2355#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002356 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002357 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2358 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002359 val = regs.r_o1;
Roland McGrath6b1d43e2003-03-31 01:05:01 +00002360#elif defined(SH)
2361 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2362 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002363#endif /* SPARC */
2364#endif /* LINUX */
2365
2366#ifdef SUNOS4
2367 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2368 return -1;
2369#endif /* SUNOS4 */
2370
2371#ifdef SVR4
2372#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002373 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002374#endif /* SPARC */
2375#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002376 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002377#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002378#ifdef X86_64
2379 val = tcp->status.PR_REG[RDX];
2380#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002381#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002382 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002383#endif /* MIPS */
2384#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002385#ifdef FREEBSD
2386 struct reg regs;
2387 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2388 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002389#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002390 return val;
2391}
2392
2393/*
2394 * Apparently, indirect system calls have already be converted by ptrace(2),
2395 * so if you see "indir" this program has gone astray.
2396 */
2397int
2398sys_indir(tcp)
2399struct tcb *tcp;
2400{
2401 int i, scno, nargs;
2402
2403 if (entering(tcp)) {
2404 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2405 fprintf(stderr, "Bogus syscall: %u\n", scno);
2406 return 0;
2407 }
2408 nargs = sysent[scno].nargs;
2409 tprintf("%s", sysent[scno].sys_name);
2410 for (i = 0; i < nargs; i++)
2411 tprintf(", %#lx", tcp->u_arg[i+1]);
2412 }
2413 return 0;
2414}
2415
2416static int
2417time_cmp(a, b)
2418void *a;
2419void *b;
2420{
2421 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2422}
2423
2424static int
2425syscall_cmp(a, b)
2426void *a;
2427void *b;
2428{
2429 return strcmp(sysent[*((int *) a)].sys_name,
2430 sysent[*((int *) b)].sys_name);
2431}
2432
2433static int
2434count_cmp(a, b)
2435void *a;
2436void *b;
2437{
2438 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2439
2440 return (m < n) ? 1 : (m > n) ? -1 : 0;
2441}
2442
2443static int (*sortfun)();
2444static struct timeval overhead = { -1, -1 };
2445
2446void
2447set_sortby(sortby)
2448char *sortby;
2449{
2450 if (strcmp(sortby, "time") == 0)
2451 sortfun = time_cmp;
2452 else if (strcmp(sortby, "calls") == 0)
2453 sortfun = count_cmp;
2454 else if (strcmp(sortby, "name") == 0)
2455 sortfun = syscall_cmp;
2456 else if (strcmp(sortby, "nothing") == 0)
2457 sortfun = NULL;
2458 else {
2459 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2460 exit(1);
2461 }
2462}
2463
2464void set_overhead(n)
2465int n;
2466{
2467 overhead.tv_sec = n / 1000000;
2468 overhead.tv_usec = n % 1000000;
2469}
2470
2471void
2472call_summary(outf)
2473FILE *outf;
2474{
2475 int i, j;
2476 int call_cum, error_cum;
2477 struct timeval tv_cum, dtv;
2478 double percent;
2479 char *dashes = "-------------------------";
2480 char error_str[16];
2481
2482 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2483 if (overhead.tv_sec == -1) {
2484 tv_mul(&overhead, &shortest, 8);
2485 tv_div(&overhead, &overhead, 10);
2486 }
2487 for (i = 0; i < nsyscalls; i++) {
2488 sorted_count[i] = i;
2489 if (call_count[i] == 0)
2490 continue;
2491 tv_mul(&dtv, &overhead, call_count[i]);
2492 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2493 call_cum += call_count[i];
2494 error_cum += error_count[i];
2495 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2496 }
2497 if (sortfun)
2498 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2499 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2500 "% time", "seconds", "usecs/call",
2501 "calls", "errors", "syscall");
2502 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2503 dashes, dashes, dashes, dashes, dashes, dashes);
2504 for (i = 0; i < nsyscalls; i++) {
2505 j = sorted_count[i];
2506 if (call_count[j] == 0)
2507 continue;
2508 tv_div(&dtv, &tv_count[j], call_count[j]);
2509 if (error_count[j])
2510 sprintf(error_str, "%d", error_count[j]);
2511 else
2512 error_str[0] = '\0';
2513 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2514 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2515 percent, (long) tv_count[j].tv_sec,
2516 (long) tv_count[j].tv_usec,
2517 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2518 call_count[j], error_str, sysent[j].sys_name);
2519 }
2520 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2521 dashes, dashes, dashes, dashes, dashes, dashes);
2522 if (error_cum)
2523 sprintf(error_str, "%d", error_cum);
2524 else
2525 error_str[0] = '\0';
2526 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2527 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2528 call_cum, error_str, "total");
2529}