blob: a1c37347c38202612bbe661aa167303905441496 [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;
Michal Ludvig0e035502002-09-23 15:41:01 +0000716#elif defined(X86_64)
717 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000718#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000719#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000720#ifdef FREEBSD
721 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000722#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000723
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000724int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000725get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726struct tcb *tcp;
727{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000729#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000730 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000731#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000732
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000733#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000734#if defined(S390) || defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +0000735 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000736 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000737 if (syscall_mode != -ENOSYS) {
Michal Ludvig882eda82002-11-11 12:50:47 +0000738 /*
739 * Since kernel version 2.5.44 the scno gets passed in gpr2.
740 */
741 scno = syscall_mode;
742 }
Roland McGrath96dc5142003-01-20 10:23:04 +0000743 if (tcp->flags & TCB_WAITEXECVE) {
744 /*
745 * When the execve system call completes successfully, the
746 * new process still has -ENOSYS (old style) or __NR_execve
747 * (new style) in gpr2. We cannot recover the scno again
748 * by disassembly, because the image that executed the
749 * syscall is gone now. Fortunately, we don't want it. We
750 * leave the flag set so that syscall_fixup can fake the
751 * result.
752 */
753 if (tcp->flags & TCB_INSYSCALL)
754 return 1;
755 /*
756 * This is the SIGTRAP after execve. We cannot try to read
757 * the system call here either.
758 */
759 tcp->flags &= ~TCB_WAITEXECVE;
760 return 0;
761 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000762 else {
763 /*
764 * Old style of "passing" the scno via the SVC instruction.
765 */
766
767 long opcode, offset_reg, tmp;
768 void * svc_addr;
769 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
770 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
771 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
772 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000773
Michal Ludvig882eda82002-11-11 12:50:47 +0000774 if (upeek(pid, PT_PSWADDR, &pc) < 0)
775 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000776 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000777 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000778 if (errno) {
779 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000780 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000781 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000782
783 /*
784 * We have to check if the SVC got executed directly or via an
785 * EXECUTE instruction. In case of EXECUTE it is necessary to do
786 * instruction decoding to derive the system call number.
787 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
788 * so that this doesn't work if a SVC opcode is part of an EXECUTE
789 * opcode. Since there is no way to find out the opcode size this
790 * is the best we can do...
791 */
792
793 if ((opcode & 0xff00) == 0x0a00) {
794 /* SVC opcode */
795 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000796 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000797 else {
798 /* SVC got executed by EXECUTE instruction */
799
800 /*
801 * Do instruction decoding of EXECUTE. If you really want to
802 * understand this, read the Principles of Operations.
803 */
804 svc_addr = (void *) (opcode & 0xfff);
805
806 tmp = 0;
807 offset_reg = (opcode & 0x000f0000) >> 16;
808 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
809 return -1;
810 svc_addr += tmp;
811
812 tmp = 0;
813 offset_reg = (opcode & 0x0000f000) >> 12;
814 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
815 return -1;
816 svc_addr += tmp;
817
818 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
819 if (errno)
820 return -1;
821#if defined(S390X)
822 scno >>= 48;
823#else
824 scno >>= 16;
825#endif
826 tmp = 0;
827 offset_reg = (opcode & 0x00f00000) >> 20;
828 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
829 return -1;
830
831 scno = (scno | tmp) & 0xff;
832 }
833 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000834#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000835 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000836 return -1;
837 if (!(tcp->flags & TCB_INSYSCALL)) {
838 /* Check if we return from execve. */
839 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
840 tcp->flags &= ~TCB_WAITEXECVE;
841 return 0;
842 }
843 }
844#elif defined (I386)
845 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
846 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000847#elif defined (X86_64)
848 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
849 return -1;
850
Roland McGrath761b5d72002-12-15 23:58:31 +0000851 if (!(tcp->flags & TCB_INSYSCALL)) {
852 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000853 long val;
854
855 /* Check CS register value. On x86-64 linux it is:
856 * 0x33 for long mode (64 bit)
857 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000858 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000859 * to be cached.
860 */
861 if (upeek(pid, 8*CS, &val) < 0)
862 return -1;
863 switch(val)
864 {
865 case 0x23: currpers = 1; break;
866 case 0x33: currpers = 0; break;
867 default:
868 fprintf(stderr, "Unknown value CS=0x%02X while "
869 "detecting personality of process "
870 "PID=%d\n", (int)val, pid);
871 currpers = current_personality;
872 break;
873 }
874#if 0
875 /* This version analyzes the opcode of a syscall instruction.
876 * (int 0x80 on i386 vs. syscall on x86-64)
877 * It works, but is too complicated.
878 */
879 unsigned long val, rip, i;
880
881 if(upeek(pid, 8*RIP, &rip)<0)
882 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000883
Michal Ludvig0e035502002-09-23 15:41:01 +0000884 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
885 rip-=2;
886 errno = 0;
887
Roland McGrath761b5d72002-12-15 23:58:31 +0000888 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
889 if (errno)
890 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000891 strerror(errno));
892 switch (call & 0xffff)
893 {
894 /* x86-64: syscall = 0x0f 0x05 */
895 case 0x050f: currpers = 0; break;
896 /* i386: int 0x80 = 0xcd 0x80 */
897 case 0x80cd: currpers = 1; break;
898 default:
899 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000900 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000901 "Unknown syscall opcode (0x%04X) while "
902 "detecting personality of process "
903 "PID=%d\n", (int)call, pid);
904 break;
905 }
906#endif
907 if(currpers != current_personality)
908 {
909 char *names[]={"64 bit", "32 bit"};
910 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000911 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000912 pid, names[current_personality]);
913 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000914 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000915#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000916# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000917 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000918 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000919 if (!(tcp->flags & TCB_INSYSCALL)) {
920 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000921 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000922 return -1;
923 } else {
924 if (upeek (pid, PT_R15, &scno) < 0)
925 return -1;
926 }
Roland McGrathba954762003-03-05 06:29:06 +0000927 /* Check if we return from execve. */
928 if (tcp->flags & TCB_WAITEXECVE) {
929 tcp->flags &= ~TCB_WAITEXECVE;
930 return 0;
931 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000932 } else {
933 /* syscall in progress */
934 if (upeek (pid, PT_R8, &r8) < 0)
935 return -1;
936 if (upeek (pid, PT_R10, &r10) < 0)
937 return -1;
938 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000939#elif defined (ARM)
Roland McGrath761b5d72002-12-15 23:58:31 +0000940 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000941 long pc;
942 upeek(pid, 4*15, &pc);
943 umoven(tcp, pc-4, 4, (char *)&scno);
944 scno &= 0x000fffff;
945 }
946#elif defined (M68K)
947 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
948 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000949#elif defined (MIPS)
950 if (upeek(pid, REG_A3, &a3) < 0)
951 return -1;
952
953 if(!(tcp->flags & TCB_INSYSCALL)) {
954 if (upeek(pid, REG_V0, &scno) < 0)
955 return -1;
956
957 if (scno < 0 || scno > nsyscalls) {
958 if(a3 == 0 || a3 == -1) {
959 if(debug)
960 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
961 return 0;
962 }
963 }
964 } else {
965 if (upeek(pid, REG_V0, &r2) < 0)
966 return -1;
967 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000968#elif defined (ALPHA)
969 if (upeek(pid, REG_A3, &a3) < 0)
970 return -1;
971
972 if (!(tcp->flags & TCB_INSYSCALL)) {
973 if (upeek(pid, REG_R0, &scno) < 0)
974 return -1;
975
976 /* Check if we return from execve. */
977 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
978 tcp->flags &= ~TCB_WAITEXECVE;
979 return 0;
980 }
981
982 /*
983 * Do some sanity checks to figure out if it's
984 * really a syscall entry
985 */
986 if (scno < 0 || scno > nsyscalls) {
987 if (a3 == 0 || a3 == -1) {
988 if (debug)
989 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
990 return 0;
991 }
992 }
993 }
994 else {
995 if (upeek(pid, REG_R0, &r0) < 0)
996 return -1;
997 }
998#elif defined (SPARC)
999 /* Everything we need is in the current register set. */
1000 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1001 return -1;
1002
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001003 /* If we are entering, then disassemble the syscall trap. */
1004 if (!(tcp->flags & TCB_INSYSCALL)) {
1005 /* Retrieve the syscall trap instruction. */
1006 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001007 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001008 if (errno)
1009 return -1;
1010
1011 /* Disassemble the trap to see what personality to use. */
1012 switch (trap) {
1013 case 0x91d02010:
1014 /* Linux/SPARC syscall trap. */
1015 set_personality(0);
1016 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001017 case 0x91d0206d:
1018 /* Linux/SPARC64 syscall trap. */
1019 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1020 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001021 case 0x91d02000:
1022 /* SunOS syscall trap. (pers 1) */
1023 fprintf(stderr,"syscall: SunOS no support\n");
1024 return -1;
1025 case 0x91d02008:
1026 /* Solaris 2.x syscall trap. (per 2) */
1027 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001028 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001029 case 0x91d02009:
1030 /* NetBSD/FreeBSD syscall trap. */
1031 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1032 return -1;
1033 case 0x91d02027:
1034 /* Solaris 2.x gettimeofday */
1035 set_personality(1);
1036 break;
1037 default:
1038 /* Unknown syscall trap. */
1039 if(tcp->flags & TCB_WAITEXECVE) {
1040 tcp->flags &= ~TCB_WAITEXECVE;
1041 return 0;
1042 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001043 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001044 return -1;
1045 }
1046
1047 /* Extract the system call number from the registers. */
1048 if (trap == 0x91d02027)
1049 scno = 156;
1050 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001051 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001052 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001053 scno = regs.r_o0;
1054 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001055 }
1056 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001057#elif defined(HPPA)
1058 if (upeek(pid, PT_GR20, &scno) < 0)
1059 return -1;
1060 if (!(tcp->flags & TCB_INSYSCALL)) {
1061 /* Check if we return from execve. */
1062 if ((tcp->flags & TCB_WAITEXECVE)) {
1063 tcp->flags &= ~TCB_WAITEXECVE;
1064 return 0;
1065 }
1066 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001067#elif defined(SH)
1068 /*
1069 * In the new syscall ABI, the system call number is in R3.
1070 */
1071 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1072 return -1;
1073
1074 if (scno < 0) {
1075 /* Odd as it may seem, a glibc bug has been known to cause
1076 glibc to issue bogus negative syscall numbers. So for
1077 our purposes, make strace print what it *should* have been */
1078 long correct_scno = (scno & 0xff);
1079 if (debug)
1080 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001081 "Detected glibc bug: bogus system call number = %ld, "
1082 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001083 scno,
1084 correct_scno);
1085 scno = correct_scno;
1086 }
1087
1088
1089 if (!(tcp->flags & TCB_INSYSCALL)) {
1090 /* Check if we return from execve. */
1091 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1092 tcp->flags &= ~TCB_WAITEXECVE;
1093 return 0;
1094 }
1095 }
1096#endif /* SH */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001097#endif /* LINUX */
1098#ifdef SUNOS4
1099 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1100 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001101#elif defined(SH)
1102 /* new syscall ABI returns result in R0 */
1103 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1104 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001105#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001106#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001107#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001108 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001109#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001110#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001111 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001112#else /* FREEBSD */
1113 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1114 perror("pread");
1115 return -1;
1116 }
1117 switch (regs.r_eax) {
1118 case SYS_syscall:
1119 case SYS___syscall:
1120 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1121 break;
1122 default:
1123 scno = regs.r_eax;
1124 break;
1125 }
1126#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001127#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001128#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001129 if (!(tcp->flags & TCB_INSYSCALL))
1130 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001131 return 1;
1132}
1133
Pavel Machek4dc3b142000-02-01 17:58:41 +00001134
1135int
1136syscall_fixup(tcp)
1137struct tcb *tcp;
1138{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001139#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001140 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001141#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001142 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001143
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001144 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001145 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001146 if (
1147 scno == SYS_fork
1148#ifdef SYS_vfork
1149 || scno == SYS_vfork
1150#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001151#ifdef SYS_fork1
1152 || scno == SYS_fork1
1153#endif /* SYS_fork1 */
1154#ifdef SYS_forkall
1155 || scno == SYS_forkall
1156#endif /* SYS_forkall */
1157#ifdef SYS_rfork1
1158 || scno == SYS_rfork1
1159#endif /* SYS_fork1 */
1160#ifdef SYS_rforkall
1161 || scno == SYS_rforkall
1162#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001163 ) {
1164 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001165 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001166 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001167 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001168 }
1169 else {
1170 fprintf(stderr, "syscall: missing entry\n");
1171 tcp->flags |= TCB_INSYSCALL;
1172 }
1173 }
1174 }
1175 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001176 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001177 fprintf(stderr, "syscall: missing exit\n");
1178 tcp->flags &= ~TCB_INSYSCALL;
1179 }
1180 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001181#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001182#ifdef SUNOS4
1183 if (!(tcp->flags & TCB_INSYSCALL)) {
1184 if (scno == 0) {
1185 fprintf(stderr, "syscall: missing entry\n");
1186 tcp->flags |= TCB_INSYSCALL;
1187 }
1188 }
1189 else {
1190 if (scno != 0) {
1191 if (debug) {
1192 /*
1193 * This happens when a signal handler
1194 * for a signal which interrupted a
1195 * a system call makes another system call.
1196 */
1197 fprintf(stderr, "syscall: missing exit\n");
1198 }
1199 tcp->flags &= ~TCB_INSYSCALL;
1200 }
1201 }
1202#endif /* SUNOS4 */
1203#ifdef LINUX
1204#if defined (I386)
1205 if (upeek(pid, 4*EAX, &eax) < 0)
1206 return -1;
1207 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1208 if (debug)
1209 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1210 return 0;
1211 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001212#elif defined (X86_64)
1213 if (upeek(pid, 8*RAX, &rax) < 0)
1214 return -1;
1215 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1216 if (debug)
1217 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1218 return 0;
1219 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001220#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001221 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1222 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001223 if (syscall_mode != -ENOSYS)
1224 syscall_mode = tcp->scno;
1225 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001226 if (debug)
1227 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1228 return 0;
1229 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001230 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1231 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1232 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1233 /*
1234 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1235 * flag set for the post-execve SIGTRAP to see and reset.
1236 */
1237 gpr2 = 0;
1238 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001239#elif defined (POWERPC)
1240# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001241 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001242 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001243 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001244 return -1;
1245 if (flags & SO_MASK)
1246 result = -result;
1247#elif defined (M68K)
1248 if (upeek(pid, 4*PT_D0, &d0) < 0)
1249 return -1;
1250 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1251 if (debug)
1252 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1253 return 0;
1254 }
1255#elif defined (ARM)
1256 if (upeek(pid, 4*0, (long *)&r0) < 0)
1257 return -1;
1258 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1259 if (debug)
1260 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1261 return 0;
1262 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001263#elif defined (HPPA)
1264 if (upeek(pid, PT_GR28, &r28) < 0)
1265 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001266#elif defined(IA64)
1267 if (upeek(pid, PT_R10, &r10) < 0)
1268 return -1;
1269 if (upeek(pid, PT_R8, &r8) < 0)
1270 return -1;
1271 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1272 if (debug)
1273 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1274 return 0;
1275 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001276#endif
1277#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001278 return 1;
1279}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001280
Pavel Machek4dc3b142000-02-01 17:58:41 +00001281int
1282get_error(tcp)
1283struct tcb *tcp;
1284{
1285 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001286#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001287#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001288 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1289 tcp->u_rval = -1;
1290 u_error = -gpr2;
1291 }
1292 else {
1293 tcp->u_rval = gpr2;
1294 u_error = 0;
1295 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001296#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001297#ifdef I386
1298 if (eax < 0 && -eax < nerrnos) {
1299 tcp->u_rval = -1;
1300 u_error = -eax;
1301 }
1302 else {
1303 tcp->u_rval = eax;
1304 u_error = 0;
1305 }
1306#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001307#ifdef X86_64
1308 if (rax < 0 && -rax < nerrnos) {
1309 tcp->u_rval = -1;
1310 u_error = -rax;
1311 }
1312 else {
1313 tcp->u_rval = rax;
1314 u_error = 0;
1315 }
1316#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001317#ifdef IA64
1318 if (ia32) {
1319 int err;
1320
1321 err = (int)r8;
1322 if (err < 0 && -err < nerrnos) {
1323 tcp->u_rval = -1;
1324 u_error = -err;
1325 }
1326 else {
1327 tcp->u_rval = err;
1328 u_error = 0;
1329 }
1330 } else {
1331 if (r10) {
1332 tcp->u_rval = -1;
1333 u_error = r8;
1334 } else {
1335 tcp->u_rval = r8;
1336 u_error = 0;
1337 }
1338 }
1339#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001340#ifdef MIPS
1341 if (a3) {
1342 tcp->u_rval = -1;
1343 u_error = r2;
1344 } else {
1345 tcp->u_rval = r2;
1346 u_error = 0;
1347 }
1348#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001349#ifdef POWERPC
1350 if (result && (unsigned) -result < nerrnos) {
1351 tcp->u_rval = -1;
1352 u_error = -result;
1353 }
1354 else {
1355 tcp->u_rval = result;
1356 u_error = 0;
1357 }
1358#else /* !POWERPC */
1359#ifdef M68K
1360 if (d0 && (unsigned) -d0 < nerrnos) {
1361 tcp->u_rval = -1;
1362 u_error = -d0;
1363 }
1364 else {
1365 tcp->u_rval = d0;
1366 u_error = 0;
1367 }
1368#else /* !M68K */
1369#ifdef ARM
1370 if (r0 && (unsigned) -r0 < nerrnos) {
1371 tcp->u_rval = -1;
1372 u_error = -r0;
1373 }
1374 else {
1375 tcp->u_rval = r0;
1376 u_error = 0;
1377 }
1378#else /* !ARM */
1379#ifdef ALPHA
1380 if (a3) {
1381 tcp->u_rval = -1;
1382 u_error = r0;
1383 }
1384 else {
1385 tcp->u_rval = r0;
1386 u_error = 0;
1387 }
1388#else /* !ALPHA */
1389#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001390 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001391 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001392 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001393 }
1394 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001395 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001396 u_error = 0;
1397 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001398#else /* !SPARC */
1399#ifdef HPPA
1400 if (r28 && (unsigned) -r28 < nerrnos) {
1401 tcp->u_rval = -1;
1402 u_error = -r28;
1403 }
1404 else {
1405 tcp->u_rval = r28;
1406 u_error = 0;
1407 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001408#else
1409#ifdef SH
1410 /* interpret R0 as return value or error number */
1411 if (r0 && (unsigned) -r0 < nerrnos) {
1412 tcp->u_rval = -1;
1413 u_error = -r0;
1414 }
1415 else {
1416 tcp->u_rval = r0;
1417 u_error = 0;
1418 }
1419#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001420#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001421#endif /* SPARC */
1422#endif /* ALPHA */
1423#endif /* ARM */
1424#endif /* M68K */
1425#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001426#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001427#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001428#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001429#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001430#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001431#endif /* LINUX */
1432#ifdef SUNOS4
1433 /* get error code from user struct */
1434 if (upeek(pid, uoff(u_error), &u_error) < 0)
1435 return -1;
1436 u_error >>= 24; /* u_error is a char */
1437
1438 /* get system call return value */
1439 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1440 return -1;
1441#endif /* SUNOS4 */
1442#ifdef SVR4
1443#ifdef SPARC
1444 /* Judicious guessing goes a long way. */
1445 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1446 tcp->u_rval = -1;
1447 u_error = tcp->status.pr_reg[R_O0];
1448 }
1449 else {
1450 tcp->u_rval = tcp->status.pr_reg[R_O0];
1451 u_error = 0;
1452 }
1453#endif /* SPARC */
1454#ifdef I386
1455 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001456 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001457 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001458 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001459 }
1460 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001461 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001462#ifdef HAVE_LONG_LONG
1463 tcp->u_lrval =
1464 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1465 tcp->status.PR_REG[EAX];
1466#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001467 u_error = 0;
1468 }
1469#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001470#ifdef X86_64
1471 /* Wanna know how to kill an hour single-stepping? */
1472 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1473 tcp->u_rval = -1;
1474 u_error = tcp->status.PR_REG[RAX];
1475 }
1476 else {
1477 tcp->u_rval = tcp->status.PR_REG[RAX];
1478 u_error = 0;
1479 }
1480#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001481#ifdef MIPS
1482 if (tcp->status.pr_reg[CTX_A3]) {
1483 tcp->u_rval = -1;
1484 u_error = tcp->status.pr_reg[CTX_V0];
1485 }
1486 else {
1487 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1488 u_error = 0;
1489 }
1490#endif /* MIPS */
1491#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001492#ifdef FREEBSD
1493 if (regs.r_eflags & PSL_C) {
1494 tcp->u_rval = -1;
1495 u_error = regs.r_eax;
1496 } else {
1497 tcp->u_rval = regs.r_eax;
1498 tcp->u_lrval =
1499 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1500 u_error = 0;
1501 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001502#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001503 tcp->u_error = u_error;
1504 return 1;
1505}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001506
Roland McGrathb69f81b2002-12-21 23:25:18 +00001507int
1508force_result(tcp, error, rval)
1509 struct tcb *tcp;
1510 int error;
1511 long rval;
1512{
1513#ifdef LINUX
1514#if defined(S390) || defined(S390X)
1515 gpr2 = error ? -error : rval;
Roland McGrathd38809b2003-01-10 07:50:21 +00001516 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001517 return -1;
1518 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1519 return -1;
1520#else /* !S390 && !S390X */
1521#ifdef I386
1522 eax = error ? -error : rval;
1523 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1524 return -1;
1525#else /* !I386 */
1526#ifdef X86_64
1527 rax = error ? -error : rval;
1528 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1529 return -1;
1530#else
1531#ifdef IA64
1532 if (ia32) {
1533 r8 = error ? -error : rval;
1534 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1535 return -1;
1536 }
1537 else {
1538 if (error) {
1539 r8 = error;
1540 r10 = -1;
1541 }
1542 else {
1543 r8 = rval;
1544 r10 = 0;
1545 }
1546 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1547 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1548 return -1;
1549 }
1550#else /* !IA64 */
1551#ifdef MIPS
1552 if (error) {
1553 r2 = error;
1554 a3 = -1;
1555 }
1556 else {
1557 r2 = rval;
1558 a3 = 0;
1559 }
1560 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1561 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1562 return -1;
1563#else
1564#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001565 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001566 return -1;
1567 if (error) {
1568 flags |= SO_MASK;
1569 result = error;
1570 }
1571 else {
1572 flags &= ~SO_MASK;
1573 result = rval;
1574 }
Roland McGratheb285352003-01-14 09:59:00 +00001575 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1576 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001577 return -1;
1578#else /* !POWERPC */
1579#ifdef M68K
1580 d0 = error ? -error : rval;
1581 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1582 return -1;
1583#else /* !M68K */
1584#ifdef ARM
1585 r0 = error ? -error : rval;
1586 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1587 return -1;
1588#else /* !ARM */
1589#ifdef ALPHA
1590 if (error) {
1591 a3 = -1;
1592 r0 = error;
1593 }
1594 else {
1595 a3 = 0;
1596 r0 = rval;
1597 }
1598 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1599 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1600 return -1;
1601#else /* !ALPHA */
1602#ifdef SPARC
1603 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1604 return -1;
1605 if (error) {
1606 regs.r_psr |= PSR_C;
1607 regs.r_o0 = error;
1608 }
1609 else {
1610 regs.r_psr &= ~PSR_C;
1611 regs.r_o0 = rval;
1612 }
1613 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1614 return -1;
1615#else /* !SPARC */
1616#ifdef HPPA
1617 r28 = error ? -error : rval;
1618 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1619 return -1;
1620#else
1621#ifdef SH
1622 r0 = error ? -error : rval;
1623 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1624 return -1;
1625#endif /* SH */
1626#endif /* HPPA */
1627#endif /* SPARC */
1628#endif /* ALPHA */
1629#endif /* ARM */
1630#endif /* M68K */
1631#endif /* POWERPC */
1632#endif /* MIPS */
1633#endif /* IA64 */
1634#endif /* X86_64 */
1635#endif /* I386 */
1636#endif /* S390 || S390X */
1637#endif /* LINUX */
1638#ifdef SUNOS4
1639 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1640 error << 24) < 0 ||
1641 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1642 return -1;
1643#endif /* SUNOS4 */
1644#ifdef SVR4
1645 /* XXX no clue */
1646 return -1;
1647#endif /* SVR4 */
1648#ifdef FREEBSD
1649 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1650 perror("pread");
1651 return -1;
1652 }
1653 if (error) {
1654 regs.r_eflags |= PSL_C;
1655 regs.r_eax = error;
1656 }
1657 else {
1658 regs.r_eflags &= ~PSL_C;
1659 regs.r_eax = rval;
1660 }
1661 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1662 perror("pwrite");
1663 return -1;
1664 }
1665#endif /* FREEBSD */
1666
1667 /* All branches reach here on success (only). */
1668 tcp->u_error = error;
1669 tcp->u_rval = rval;
1670 return 0;
1671}
1672
Pavel Machek4dc3b142000-02-01 17:58:41 +00001673int syscall_enter(tcp)
1674struct tcb *tcp;
1675{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001676#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001677 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001678#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001679#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001680#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001681 {
1682 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001683 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1684 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001685 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001686 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001687 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001688 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001689 return -1;
1690 }
1691 }
1692#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001693 {
1694 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001695 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1696 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001697 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001698 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001699 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001700 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1701 * for scno somewhere above here!
1702 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001703 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1704 return -1;
1705 }
1706 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001707#elif defined (IA64)
1708 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001709 if (!ia32) {
1710 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1711 /* be backwards compatible with kernel < 2.4.4... */
1712# ifndef PT_RBS_END
1713# define PT_RBS_END PT_AR_BSP
1714# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001715
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001716 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001717 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001718 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1719 return -1;
1720
1721 sof = (cfm >> 0) & 0x7f;
1722 sol = (cfm >> 7) & 0x7f;
1723 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1724
1725 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1726 && sysent[tcp->scno].nargs != -1)
1727 tcp->u_nargs = sysent[tcp->scno].nargs;
1728 else
1729 tcp->u_nargs = MAX_ARGS;
1730 for (i = 0; i < tcp->u_nargs; ++i) {
1731 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1732 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1733 return -1;
1734 }
1735 } else {
1736 int i;
1737
1738 if (/* EBX = out0 */
1739 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1740 /* ECX = out1 */
1741 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1742 /* EDX = out2 */
1743 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1744 /* ESI = out3 */
1745 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1746 /* EDI = out4 */
1747 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1748 /* EBP = out5 */
1749 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1750 return -1;
1751
1752 for (i = 0; i < 6; ++i)
1753 /* truncate away IVE sign-extension */
1754 tcp->u_arg[i] &= 0xffffffff;
1755
1756 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1757 && sysent[tcp->scno].nargs != -1)
1758 tcp->u_nargs = sysent[tcp->scno].nargs;
1759 else
1760 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001761 }
1762 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001763#elif defined (MIPS)
1764 {
1765 long sp;
1766 int i, nargs;
1767
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001768 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1769 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001770 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001771 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001772 if(nargs > 4) {
1773 if(upeek(pid, REG_SP, &sp) < 0)
1774 return -1;
1775 for(i = 0; i < 4; i++) {
1776 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1777 return -1;
1778 }
1779 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1780 (char *)(tcp->u_arg + 4));
1781 } else {
1782 for(i = 0; i < nargs; i++) {
1783 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1784 return -1;
1785 }
1786 }
1787 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001788#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001789#ifndef PT_ORIG_R3
1790#define PT_ORIG_R3 34
1791#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001792 {
1793 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001794 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1795 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001796 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001797 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001798 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001799 if (upeek(pid, (i==0) ?
1800 (sizeof(unsigned long)*PT_ORIG_R3) :
1801 ((i+PT_R3)*sizeof(unsigned long)),
1802 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001803 return -1;
1804 }
1805 }
1806#elif defined (SPARC)
1807 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001808 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001809
1810 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1811 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001812 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001813 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001814 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001815 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001816 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001817#elif defined (HPPA)
1818 {
1819 int i;
1820
1821 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1822 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001823 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001824 tcp->u_nargs = MAX_ARGS;
1825 for (i = 0; i < tcp->u_nargs; i++) {
1826 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1827 return -1;
1828 }
1829 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001830#elif defined(SH)
1831 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001832 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001833 static int syscall_regs[] = {
1834 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1835 REG_REG0, REG_REG0+1, REG_REG0+2
1836 };
1837
1838 tcp->u_nargs = sysent[tcp->scno].nargs;
1839 for (i = 0; i < tcp->u_nargs; i++) {
1840 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1841 return -1;
1842 }
1843 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001844#elif defined(X86_64)
1845 {
1846 int i;
1847 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1848 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1849 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1850 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001851
Michal Ludvig0e035502002-09-23 15:41:01 +00001852 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1853 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001854 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001855 tcp->u_nargs = MAX_ARGS;
1856 for (i = 0; i < tcp->u_nargs; i++) {
1857 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1858 return -1;
1859 }
1860 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001861#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862 {
1863 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001864 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1865 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001866 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001867 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001868 for (i = 0; i < tcp->u_nargs; i++) {
1869 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1870 return -1;
1871 }
1872 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001873#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001874#endif /* LINUX */
1875#ifdef SUNOS4
1876 {
1877 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001878 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1879 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001880 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001881 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001882 for (i = 0; i < tcp->u_nargs; i++) {
1883 struct user *u;
1884
1885 if (upeek(pid, uoff(u_arg[0]) +
1886 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1887 return -1;
1888 }
1889 }
1890#endif /* SUNOS4 */
1891#ifdef SVR4
1892#ifdef MIPS
1893 /*
1894 * SGI is broken: even though it has pr_sysarg, it doesn't
1895 * set them on system call entry. Get a clue.
1896 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001897 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001898 tcp->u_nargs = sysent[tcp->scno].nargs;
1899 else
1900 tcp->u_nargs = tcp->status.pr_nsysarg;
1901 if (tcp->u_nargs > 4) {
1902 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1903 4*sizeof(tcp->u_arg[0]));
1904 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1905 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1906 }
1907 else {
1908 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1909 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1910 }
John Hughes25299712001-03-06 10:10:06 +00001911#elif UNIXWARE >= 2
1912 /*
1913 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1914 */
1915 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1916 tcp->u_nargs = sysent[tcp->scno].nargs;
1917 else
1918 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1919 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1920 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1921#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001922 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001923 tcp->u_nargs = sysent[tcp->scno].nargs;
1924 else
1925 tcp->u_nargs = tcp->status.pr_nsysarg;
1926 {
1927 int i;
1928 for (i = 0; i < tcp->u_nargs; i++)
1929 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1930 }
John Hughes25299712001-03-06 10:10:06 +00001931#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001932 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001933 tcp->u_nargs = sysent[tcp->scno].nargs;
1934 else
1935 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001936 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001937 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001938#else
1939 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001940#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001941#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001942#ifdef FREEBSD
1943 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1944 sysent[tcp->scno].nargs > tcp->status.val)
1945 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001946 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001947 tcp->u_nargs = tcp->status.val;
1948 if (tcp->u_nargs < 0)
1949 tcp->u_nargs = 0;
1950 if (tcp->u_nargs > MAX_ARGS)
1951 tcp->u_nargs = MAX_ARGS;
1952 switch(regs.r_eax) {
1953 case SYS___syscall:
1954 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1955 regs.r_esp + sizeof(int) + sizeof(quad_t));
1956 break;
1957 case SYS_syscall:
1958 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1959 regs.r_esp + 2 * sizeof(int));
1960 break;
1961 default:
1962 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1963 regs.r_esp + sizeof(int));
1964 break;
1965 }
1966#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001967 return 1;
1968}
1969
1970int
1971trace_syscall(tcp)
1972struct tcb *tcp;
1973{
1974 int sys_res;
1975 struct timeval tv;
1976 int res;
1977
1978 /* Measure the exit time as early as possible to avoid errors. */
1979 if (dtime && (tcp->flags & TCB_INSYSCALL))
1980 gettimeofday(&tv, NULL);
1981
1982 res = get_scno(tcp);
1983 if (res != 1)
1984 return res;
1985
1986 res = syscall_fixup(tcp);
1987 if (res != 1)
1988 return res;
1989
1990 if (tcp->flags & TCB_INSYSCALL) {
1991 long u_error;
1992 res = get_error(tcp);
1993 if (res != 1)
1994 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001995
1996 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001997 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1998 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001999 tcp->flags &= ~TCB_INSYSCALL;
2000 return 0;
2001 }
2002
2003 if (tcp->flags & TCB_REPRINT) {
2004 printleader(tcp);
2005 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002006 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002007 tprintf("syscall_%lu", tcp->scno);
2008 else
2009 tprintf("%s", sysent[tcp->scno].sys_name);
2010 tprintf(" resumed> ");
2011 }
2012
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002013 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002014 call_count[tcp->scno]++;
2015 if (tcp->u_error)
2016 error_count[tcp->scno]++;
2017 tv_sub(&tv, &tv, &tcp->etime);
2018#ifdef LINUX
2019 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2020 static struct timeval one_tick =
2021 { 0, 1000000 / HZ };
2022
2023 if (tv_nz(&tcp->dtime))
2024 tv = tcp->dtime;
2025 else if (tv_cmp(&tv, &one_tick) > 0) {
2026 if (tv_cmp(&shortest, &one_tick) < 0)
2027 tv = shortest;
2028 else
2029 tv = one_tick;
2030 }
2031 }
2032#endif /* LINUX */
2033 if (tv_cmp(&tv, &shortest) < 0)
2034 shortest = tv;
2035 tv_add(&tv_count[tcp->scno],
2036 &tv_count[tcp->scno], &tv);
2037 tcp->flags &= ~TCB_INSYSCALL;
2038 return 0;
2039 }
2040
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002041 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002042 || (qual_flags[tcp->scno] & QUAL_RAW))
2043 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002044 else {
2045 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002046 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002047 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002048 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002049 u_error = tcp->u_error;
2050 tprintf(") ");
2051 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002052 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2053 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002054 if (u_error)
2055 tprintf("= -1 (errno %ld)", u_error);
2056 else
2057 tprintf("= %#lx", tcp->u_rval);
2058 }
2059 else if (!(sys_res & RVAL_NONE) && u_error) {
2060 switch (u_error) {
2061#ifdef LINUX
2062 case ERESTARTSYS:
2063 tprintf("= ? ERESTARTSYS (To be restarted)");
2064 break;
2065 case ERESTARTNOINTR:
2066 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2067 break;
2068 case ERESTARTNOHAND:
2069 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2070 break;
2071#endif /* LINUX */
2072 default:
2073 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002074 if (u_error < 0)
2075 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002076 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002077 tprintf("%s (%s)", errnoent[u_error],
2078 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002079 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002080 tprintf("ERRNO_%ld (%s)", u_error,
2081 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002082 break;
2083 }
2084 }
2085 else {
2086 if (sys_res & RVAL_NONE)
2087 tprintf("= ?");
2088 else {
2089 switch (sys_res & RVAL_MASK) {
2090 case RVAL_HEX:
2091 tprintf("= %#lx", tcp->u_rval);
2092 break;
2093 case RVAL_OCTAL:
2094 tprintf("= %#lo", tcp->u_rval);
2095 break;
2096 case RVAL_UDECIMAL:
2097 tprintf("= %lu", tcp->u_rval);
2098 break;
2099 case RVAL_DECIMAL:
2100 tprintf("= %ld", tcp->u_rval);
2101 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002102#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002103 case RVAL_LHEX:
2104 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002105 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002106 case RVAL_LOCTAL:
2107 tprintf("= %#llo", tcp->u_lrval);
2108 break;
2109 case RVAL_LUDECIMAL:
2110 tprintf("= %llu", tcp->u_lrval);
2111 break;
2112 case RVAL_LDECIMAL:
2113 tprintf("= %lld", tcp->u_lrval);
2114 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002115#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002116 default:
2117 fprintf(stderr,
2118 "invalid rval format\n");
2119 break;
2120 }
2121 }
2122 if ((sys_res & RVAL_STR) && tcp->auxstr)
2123 tprintf(" (%s)", tcp->auxstr);
2124 }
2125 if (dtime) {
2126 tv_sub(&tv, &tv, &tcp->etime);
2127 tprintf(" <%ld.%06ld>",
2128 (long) tv.tv_sec, (long) tv.tv_usec);
2129 }
2130 printtrailer(tcp);
2131
2132 dumpio(tcp);
2133 if (fflush(tcp->outf) == EOF)
2134 return -1;
2135 tcp->flags &= ~TCB_INSYSCALL;
2136 return 0;
2137 }
2138
2139 /* Entering system call */
2140 res = syscall_enter(tcp);
2141 if (res != 1)
2142 return res;
2143
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002144 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002145#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002146#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002147 case SYS_socketcall:
2148 decode_subcall(tcp, SYS_socket_subcall,
2149 SYS_socket_nsubcalls, deref_style);
2150 break;
2151 case SYS_ipc:
2152 decode_subcall(tcp, SYS_ipc_subcall,
2153 SYS_ipc_nsubcalls, shift_style);
2154 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002155#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002156#ifdef SPARC
2157 case SYS_socketcall:
2158 sparc_socket_decode (tcp);
2159 break;
2160#endif
2161#endif /* LINUX */
2162#ifdef SVR4
2163#ifdef SYS_pgrpsys_subcall
2164 case SYS_pgrpsys:
2165 decode_subcall(tcp, SYS_pgrpsys_subcall,
2166 SYS_pgrpsys_nsubcalls, shift_style);
2167 break;
2168#endif /* SYS_pgrpsys_subcall */
2169#ifdef SYS_sigcall_subcall
2170 case SYS_sigcall:
2171 decode_subcall(tcp, SYS_sigcall_subcall,
2172 SYS_sigcall_nsubcalls, mask_style);
2173 break;
2174#endif /* SYS_sigcall_subcall */
2175 case SYS_msgsys:
2176 decode_subcall(tcp, SYS_msgsys_subcall,
2177 SYS_msgsys_nsubcalls, shift_style);
2178 break;
2179 case SYS_shmsys:
2180 decode_subcall(tcp, SYS_shmsys_subcall,
2181 SYS_shmsys_nsubcalls, shift_style);
2182 break;
2183 case SYS_semsys:
2184 decode_subcall(tcp, SYS_semsys_subcall,
2185 SYS_semsys_nsubcalls, shift_style);
2186 break;
2187#if 0 /* broken */
2188 case SYS_utssys:
2189 decode_subcall(tcp, SYS_utssys_subcall,
2190 SYS_utssys_nsubcalls, shift_style);
2191 break;
2192#endif
2193 case SYS_sysfs:
2194 decode_subcall(tcp, SYS_sysfs_subcall,
2195 SYS_sysfs_nsubcalls, shift_style);
2196 break;
2197 case SYS_spcall:
2198 decode_subcall(tcp, SYS_spcall_subcall,
2199 SYS_spcall_nsubcalls, shift_style);
2200 break;
2201#ifdef SYS_context_subcall
2202 case SYS_context:
2203 decode_subcall(tcp, SYS_context_subcall,
2204 SYS_context_nsubcalls, shift_style);
2205 break;
2206#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002207#ifdef SYS_door_subcall
2208 case SYS_door:
2209 decode_subcall(tcp, SYS_door_subcall,
2210 SYS_door_nsubcalls, door_style);
2211 break;
2212#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002213#ifdef SYS_kaio_subcall
2214 case SYS_kaio:
2215 decode_subcall(tcp, SYS_kaio_subcall,
2216 SYS_kaio_nsubcalls, shift_style);
2217 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002218#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002219#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002220#ifdef FREEBSD
2221 case SYS_msgsys:
2222 case SYS_shmsys:
2223 case SYS_semsys:
2224 decode_subcall(tcp, 0, 0, table_style);
2225 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002226#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002227#ifdef SUNOS4
2228 case SYS_semsys:
2229 decode_subcall(tcp, SYS_semsys_subcall,
2230 SYS_semsys_nsubcalls, shift_style);
2231 break;
2232 case SYS_msgsys:
2233 decode_subcall(tcp, SYS_msgsys_subcall,
2234 SYS_msgsys_nsubcalls, shift_style);
2235 break;
2236 case SYS_shmsys:
2237 decode_subcall(tcp, SYS_shmsys_subcall,
2238 SYS_shmsys_nsubcalls, shift_style);
2239 break;
2240#endif
2241 }
2242
2243 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002244 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002245 tcp->flags |= TCB_INSYSCALL;
2246 return 0;
2247 }
2248
2249 if (cflag) {
2250 gettimeofday(&tcp->etime, NULL);
2251 tcp->flags |= TCB_INSYSCALL;
2252 return 0;
2253 }
2254
2255 printleader(tcp);
2256 tcp->flags &= ~TCB_REPRINT;
2257 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002258 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002259 tprintf("syscall_%lu(", tcp->scno);
2260 else
2261 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002262 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002263 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2264 sys_res = printargs(tcp);
2265 else
2266 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2267 if (fflush(tcp->outf) == EOF)
2268 return -1;
2269 tcp->flags |= TCB_INSYSCALL;
2270 /* Measure the entrance time as late as possible to avoid errors. */
2271 if (dtime)
2272 gettimeofday(&tcp->etime, NULL);
2273 return sys_res;
2274}
2275
2276int
2277printargs(tcp)
2278struct tcb *tcp;
2279{
2280 if (entering(tcp)) {
2281 int i;
2282
2283 for (i = 0; i < tcp->u_nargs; i++)
2284 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2285 }
2286 return 0;
2287}
2288
2289long
2290getrval2(tcp)
2291struct tcb *tcp;
2292{
2293 long val = -1;
2294
2295#ifdef LINUX
2296#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002297 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002298 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2299 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002300 val = regs.r_o1;
Roland McGrath6b1d43e2003-03-31 01:05:01 +00002301#elif defined(SH)
2302 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2303 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002304#endif /* SPARC */
2305#endif /* LINUX */
2306
2307#ifdef SUNOS4
2308 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2309 return -1;
2310#endif /* SUNOS4 */
2311
2312#ifdef SVR4
2313#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002314 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002315#endif /* SPARC */
2316#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002317 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002318#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002319#ifdef X86_64
2320 val = tcp->status.PR_REG[RDX];
2321#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002322#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002323 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002324#endif /* MIPS */
2325#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002326#ifdef FREEBSD
2327 struct reg regs;
2328 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2329 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002330#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002331 return val;
2332}
2333
2334/*
2335 * Apparently, indirect system calls have already be converted by ptrace(2),
2336 * so if you see "indir" this program has gone astray.
2337 */
2338int
2339sys_indir(tcp)
2340struct tcb *tcp;
2341{
2342 int i, scno, nargs;
2343
2344 if (entering(tcp)) {
2345 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2346 fprintf(stderr, "Bogus syscall: %u\n", scno);
2347 return 0;
2348 }
2349 nargs = sysent[scno].nargs;
2350 tprintf("%s", sysent[scno].sys_name);
2351 for (i = 0; i < nargs; i++)
2352 tprintf(", %#lx", tcp->u_arg[i+1]);
2353 }
2354 return 0;
2355}
2356
2357static int
2358time_cmp(a, b)
2359void *a;
2360void *b;
2361{
2362 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2363}
2364
2365static int
2366syscall_cmp(a, b)
2367void *a;
2368void *b;
2369{
2370 return strcmp(sysent[*((int *) a)].sys_name,
2371 sysent[*((int *) b)].sys_name);
2372}
2373
2374static int
2375count_cmp(a, b)
2376void *a;
2377void *b;
2378{
2379 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2380
2381 return (m < n) ? 1 : (m > n) ? -1 : 0;
2382}
2383
2384static int (*sortfun)();
2385static struct timeval overhead = { -1, -1 };
2386
2387void
2388set_sortby(sortby)
2389char *sortby;
2390{
2391 if (strcmp(sortby, "time") == 0)
2392 sortfun = time_cmp;
2393 else if (strcmp(sortby, "calls") == 0)
2394 sortfun = count_cmp;
2395 else if (strcmp(sortby, "name") == 0)
2396 sortfun = syscall_cmp;
2397 else if (strcmp(sortby, "nothing") == 0)
2398 sortfun = NULL;
2399 else {
2400 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2401 exit(1);
2402 }
2403}
2404
2405void set_overhead(n)
2406int n;
2407{
2408 overhead.tv_sec = n / 1000000;
2409 overhead.tv_usec = n % 1000000;
2410}
2411
2412void
2413call_summary(outf)
2414FILE *outf;
2415{
2416 int i, j;
2417 int call_cum, error_cum;
2418 struct timeval tv_cum, dtv;
2419 double percent;
2420 char *dashes = "-------------------------";
2421 char error_str[16];
2422
2423 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2424 if (overhead.tv_sec == -1) {
2425 tv_mul(&overhead, &shortest, 8);
2426 tv_div(&overhead, &overhead, 10);
2427 }
2428 for (i = 0; i < nsyscalls; i++) {
2429 sorted_count[i] = i;
2430 if (call_count[i] == 0)
2431 continue;
2432 tv_mul(&dtv, &overhead, call_count[i]);
2433 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2434 call_cum += call_count[i];
2435 error_cum += error_count[i];
2436 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2437 }
2438 if (sortfun)
2439 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2440 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2441 "% time", "seconds", "usecs/call",
2442 "calls", "errors", "syscall");
2443 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2444 dashes, dashes, dashes, dashes, dashes, dashes);
2445 for (i = 0; i < nsyscalls; i++) {
2446 j = sorted_count[i];
2447 if (call_count[j] == 0)
2448 continue;
2449 tv_div(&dtv, &tv_count[j], call_count[j]);
2450 if (error_count[j])
2451 sprintf(error_str, "%d", error_count[j]);
2452 else
2453 error_str[0] = '\0';
2454 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2455 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2456 percent, (long) tv_count[j].tv_sec,
2457 (long) tv_count[j].tv_usec,
2458 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2459 call_count[j], error_str, sysent[j].sys_name);
2460 }
2461 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2462 dashes, dashes, dashes, dashes, dashes, dashes);
2463 if (error_cum)
2464 sprintf(error_str, "%d", error_cum);
2465 else
2466 error_str[0] = '\0';
2467 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2468 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2469 call_cum, error_str, "total");
2470}