blob: b5c6aa0dfae8e5bdf1a29e8d228f3aacdc9e1267 [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
220static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
221
222static struct qual_options {
223 int bitflag;
224 char *option_name;
225 int (*lookup)();
226 char *argument_name;
227} qual_options[] = {
228 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
229 { QUAL_TRACE, "t", lookup_syscall, "system call" },
230 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
231 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
232 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
233 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
234 { QUAL_RAW, "raw", lookup_syscall, "system call" },
235 { QUAL_RAW, "x", lookup_syscall, "system call" },
236 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
237 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
238 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
239 { QUAL_FAULT, "fault", lookup_fault, "fault" },
240 { QUAL_FAULT, "faults", lookup_fault, "fault" },
241 { QUAL_FAULT, "m", lookup_fault, "fault" },
242 { QUAL_READ, "read", lookup_desc, "descriptor" },
243 { QUAL_READ, "reads", lookup_desc, "descriptor" },
244 { QUAL_READ, "r", lookup_desc, "descriptor" },
245 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
246 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
247 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
248 { 0, NULL, NULL, NULL },
249};
250
251static int
252lookup_syscall(s)
253char *s;
254{
255 int i;
256
257 for (i = 0; i < nsyscalls; i++) {
258 if (strcmp(s, sysent[i].sys_name) == 0)
259 return i;
260 }
261 return -1;
262}
263
264static int
265lookup_signal(s)
266char *s;
267{
268 int i;
269 char buf[32];
270
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000271 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 return atoi(s);
273 strcpy(buf, s);
274 s = buf;
275 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000276 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 if (strncmp(s, "SIG", 3) == 0)
278 s += 3;
279 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000280 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000281 return i;
282 }
283 return -1;
284}
285
286static int
287lookup_fault(s)
288char *s;
289{
290 return -1;
291}
292
293static int
294lookup_desc(s)
295char *s;
296{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000297 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 return atoi(s);
299 return -1;
300}
301
302static int
303lookup_class(s)
304char *s;
305{
306 if (strcmp(s, "file") == 0)
307 return TRACE_FILE;
308 if (strcmp(s, "ipc") == 0)
309 return TRACE_IPC;
310 if (strcmp(s, "network") == 0)
311 return TRACE_NETWORK;
312 if (strcmp(s, "process") == 0)
313 return TRACE_PROCESS;
314 if (strcmp(s, "signal") == 0)
315 return TRACE_SIGNAL;
316 return -1;
317}
318
319void
320qualify(s)
321char *s;
322{
323 struct qual_options *opt;
324 int not;
325 char *p;
326 int i, n;
327
328 opt = &qual_options[0];
329 for (i = 0; (p = qual_options[i].option_name); i++) {
330 n = strlen(p);
331 if (strncmp(s, p, n) == 0 && s[n] == '=') {
332 opt = &qual_options[i];
333 s += n + 1;
334 break;
335 }
336 }
337 not = 0;
338 if (*s == '!') {
339 not = 1;
340 s++;
341 }
342 if (strcmp(s, "none") == 0) {
343 not = 1 - not;
344 s = "all";
345 }
346 if (strcmp(s, "all") == 0) {
347 for (i = 0; i < MAX_QUALS; i++) {
348 if (not)
349 qual_flags[i] &= ~opt->bitflag;
350 else
351 qual_flags[i] |= opt->bitflag;
352 }
353 return;
354 }
355 for (i = 0; i < MAX_QUALS; i++) {
356 if (not)
357 qual_flags[i] |= opt->bitflag;
358 else
359 qual_flags[i] &= ~opt->bitflag;
360 }
361 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
362 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
363 for (i = 0; i < MAX_QUALS; i++) {
364 if (sysent[i].sys_flags & n) {
365 if (not)
366 qual_flags[i] &= ~opt->bitflag;
367 else
368 qual_flags[i] |= opt->bitflag;
369 }
370 }
371 continue;
372 }
373 if ((n = (*opt->lookup)(p)) < 0) {
374 fprintf(stderr, "strace: invalid %s `%s'\n",
375 opt->argument_name, p);
376 exit(1);
377 }
378 if (not)
379 qual_flags[n] &= ~opt->bitflag;
380 else
381 qual_flags[n] |= opt->bitflag;
382 }
383 return;
384}
385
386static void
387dumpio(tcp)
388struct tcb *tcp;
389{
390 if (syserror(tcp))
391 return;
392 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
393 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000394 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000395 case SYS_read:
396#ifdef SYS_recv
397 case SYS_recv:
398#endif
399#ifdef SYS_recvfrom
400 case SYS_recvfrom:
401#endif
402 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
403 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
404 break;
405 case SYS_write:
406#ifdef SYS_send
407 case SYS_send:
408#endif
409#ifdef SYS_sendto
410 case SYS_sendto:
411#endif
412 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
413 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
414 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000415#ifdef SYS_readv
416 case SYS_readv:
417 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
418 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
419 break;
420#endif
421#ifdef SYS_writev
422 case SYS_writev:
423
424 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
425 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
426 break;
427#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428 }
429}
430
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000431#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000432enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000433#else /* FREEBSD */
434enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
435
436struct subcall {
437 int call;
438 int nsubcalls;
439 int subcalls[5];
440};
441
442const struct subcall subcalls_table[] = {
443 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000444#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000445 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000446#else
447 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
448#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000449 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
450};
451#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000453#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000454
455const int socket_map [] = {
456 /* SYS_SOCKET */ 97,
457 /* SYS_BIND */ 104,
458 /* SYS_CONNECT */ 98,
459 /* SYS_LISTEN */ 106,
460 /* SYS_ACCEPT */ 99,
461 /* SYS_GETSOCKNAME */ 150,
462 /* SYS_GETPEERNAME */ 141,
463 /* SYS_SOCKETPAIR */ 135,
464 /* SYS_SEND */ 101,
465 /* SYS_RECV */ 102,
466 /* SYS_SENDTO */ 133,
467 /* SYS_RECVFROM */ 125,
468 /* SYS_SHUTDOWN */ 134,
469 /* SYS_SETSOCKOPT */ 105,
470 /* SYS_GETSOCKOPT */ 118,
471 /* SYS_SENDMSG */ 114,
472 /* SYS_RECVMSG */ 113
473};
474
475void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000476sparc_socket_decode (tcp)
477struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478{
479 volatile long addr;
480 volatile int i, n;
481
482 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
483 return;
484 }
485 tcp->scno = socket_map [tcp->u_arg [0]-1];
486 n = tcp->u_nargs = sysent [tcp->scno].nargs;
487 addr = tcp->u_arg [1];
488 for (i = 0; i < n; i++){
489 int arg;
490 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
491 arg = 0;
492 tcp->u_arg [i] = arg;
493 addr += sizeof (arg);
494 }
495}
496
Michal Ludvig0e035502002-09-23 15:41:01 +0000497void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000498decode_subcall(tcp, subcall, nsubcalls, style)
499struct tcb *tcp;
500int subcall;
501int nsubcalls;
502enum subcall_style style;
503{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000504 long addr, mask, arg;
505 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000506
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 switch (style) {
508 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000509 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
510 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 tcp->scno = subcall + tcp->u_arg[0];
512 if (sysent[tcp->scno].nargs != -1)
513 tcp->u_nargs = sysent[tcp->scno].nargs;
514 else
515 tcp->u_nargs--;
516 for (i = 0; i < tcp->u_nargs; i++)
517 tcp->u_arg[i] = tcp->u_arg[i + 1];
518 break;
519 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000520 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
521 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522 tcp->scno = subcall + tcp->u_arg[0];
523 addr = tcp->u_arg[1];
524 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
525 if (umove(tcp, addr, &arg) < 0)
526 arg = 0;
527 tcp->u_arg[i] = arg;
528 addr += sizeof(arg);
529 }
530 tcp->u_nargs = sysent[tcp->scno].nargs;
531 break;
532 case mask_style:
533 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000534 for (i = 0; mask; i++)
535 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000536 if (i >= nsubcalls)
537 return;
538 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 tcp->scno = subcall + i;
540 if (sysent[tcp->scno].nargs != -1)
541 tcp->u_nargs = sysent[tcp->scno].nargs;
542 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000543 case door_style:
544 /*
545 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000546 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000547 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000548 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
549 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000550 tcp->scno = subcall + tcp->u_arg[5];
551 if (sysent[tcp->scno].nargs != -1)
552 tcp->u_nargs = sysent[tcp->scno].nargs;
553 else
554 tcp->u_nargs--;
555 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000556#ifdef FREEBSD
557 case table_style:
558 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
559 if (subcalls_table[i].call == tcp->scno) break;
560 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
561 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
562 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
563 for (i = 0; i < tcp->u_nargs; i++)
564 tcp->u_arg[i] = tcp->u_arg[i + 1];
565 }
566 break;
567#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 }
569}
570#endif
571
572struct tcb *tcp_last = NULL;
573
574static int
575internal_syscall(tcp)
576struct tcb *tcp;
577{
578 /*
579 * We must always trace a few critical system calls in order to
580 * correctly support following forks in the presence of tracing
581 * qualifiers.
582 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000583 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584#ifdef SYS_fork
585 case SYS_fork:
586#endif
587#ifdef SYS_vfork
588 case SYS_vfork:
589#endif
John Hughes4e36a812001-04-18 15:11:51 +0000590#ifdef SYS_fork1
591 case SYS_fork1:
592#endif
593#ifdef SYS_forkall
594 case SYS_forkall:
595#endif
596#ifdef SYS_rfork1
597 case SYS_rfork1:
598#endif
599#ifdef SYS_rforkall
600 case SYS_rforkall:
601#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000602 internal_fork(tcp);
603 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000604#ifdef SYS_clone
605 case SYS_clone:
606 internal_clone(tcp);
607 break;
608#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000609#ifdef SYS_clone2
610 case SYS_clone2:
611 internal_clone(tcp);
612 break;
613#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000614#ifdef SYS_execv
615 case SYS_execv:
616#endif
617#ifdef SYS_execve
618 case SYS_execve:
619#endif
John Hughes4e36a812001-04-18 15:11:51 +0000620#ifdef SYS_rexecve
621 case SYS_rexecve:
622#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000623 internal_exec(tcp);
624 break;
625
626#ifdef SYS_wait
627 case SYS_wait:
628#endif
629#ifdef SYS_wait4
630 case SYS_wait4:
631#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000632#ifdef SYS32_wait4
633 case SYS32_wait4:
634#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000635#ifdef SYS_waitpid
636 case SYS_waitpid:
637#endif
638#ifdef SYS_waitsys
639 case SYS_waitsys:
640#endif
641 internal_wait(tcp);
642 break;
643
644#ifdef SYS_exit
645 case SYS_exit:
646#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000647#ifdef SYS32_exit
648 case SYS32_exit:
649#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 internal_exit(tcp);
651 break;
652 }
653 return 0;
654}
655
Wichert Akkermanc7926982000-04-10 22:22:31 +0000656
657#ifdef LINUX
658#if defined (I386)
659 static long eax;
660#elif defined (IA64)
661 long r8, r10, psr;
662 long ia32 = 0;
663#elif defined (POWERPC)
664 static long result,flags;
665#elif defined (M68K)
666 static int d0;
667#elif defined (ARM)
668 static int r0;
669#elif defined (ALPHA)
670 static long r0;
671 static long a3;
672#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000673 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000674 static unsigned long trap;
675#elif defined(MIPS)
676 static long a3;
677 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000678#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000679 static long gpr2;
680 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000681 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000682#elif defined(HPPA)
683 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000684#elif defined(SH)
685 static long r0;
Michal Ludvig0e035502002-09-23 15:41:01 +0000686#elif defined(X86_64)
687 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000688#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000689#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000690#ifdef FREEBSD
691 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000692#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000693
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000694int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000695get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000696struct tcb *tcp;
697{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000698 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000699#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000700 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000701#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000702
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000703#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000704#if defined(S390) || defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +0000705 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000706 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +0000707 if (syscall_mode != -ENOSYS){
708 /*
709 * Since kernel version 2.5.44 the scno gets passed in gpr2.
710 */
711 scno = syscall_mode;
712 }
713 else {
714 /*
715 * Old style of "passing" the scno via the SVC instruction.
716 */
717
718 long opcode, offset_reg, tmp;
719 void * svc_addr;
720 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
721 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
722 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
723 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000724
Michal Ludvig882eda82002-11-11 12:50:47 +0000725 if (upeek(pid, PT_PSWADDR, &pc) < 0)
726 return -1;
727 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
728 if (errno)
729 return -1;
730
731 /*
732 * We have to check if the SVC got executed directly or via an
733 * EXECUTE instruction. In case of EXECUTE it is necessary to do
734 * instruction decoding to derive the system call number.
735 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
736 * so that this doesn't work if a SVC opcode is part of an EXECUTE
737 * opcode. Since there is no way to find out the opcode size this
738 * is the best we can do...
739 */
740
741 if ((opcode & 0xff00) == 0x0a00) {
742 /* SVC opcode */
743 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000744 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000745 else {
746 /* SVC got executed by EXECUTE instruction */
747
748 /*
749 * Do instruction decoding of EXECUTE. If you really want to
750 * understand this, read the Principles of Operations.
751 */
752 svc_addr = (void *) (opcode & 0xfff);
753
754 tmp = 0;
755 offset_reg = (opcode & 0x000f0000) >> 16;
756 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
757 return -1;
758 svc_addr += tmp;
759
760 tmp = 0;
761 offset_reg = (opcode & 0x0000f000) >> 12;
762 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
763 return -1;
764 svc_addr += tmp;
765
766 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
767 if (errno)
768 return -1;
769#if defined(S390X)
770 scno >>= 48;
771#else
772 scno >>= 16;
773#endif
774 tmp = 0;
775 offset_reg = (opcode & 0x00f00000) >> 20;
776 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
777 return -1;
778
779 scno = (scno | tmp) & 0xff;
780 }
781 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000782#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000783 if (upeek(pid, 4*PT_R0, &scno) < 0)
784 return -1;
785 if (!(tcp->flags & TCB_INSYSCALL)) {
786 /* Check if we return from execve. */
787 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
788 tcp->flags &= ~TCB_WAITEXECVE;
789 return 0;
790 }
791 }
792#elif defined (I386)
793 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
794 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000795#elif defined (X86_64)
796 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
797 return -1;
798
Roland McGrath761b5d72002-12-15 23:58:31 +0000799 if (!(tcp->flags & TCB_INSYSCALL)) {
800 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000801 long val;
802
803 /* Check CS register value. On x86-64 linux it is:
804 * 0x33 for long mode (64 bit)
805 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000806 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000807 * to be cached.
808 */
809 if (upeek(pid, 8*CS, &val) < 0)
810 return -1;
811 switch(val)
812 {
813 case 0x23: currpers = 1; break;
814 case 0x33: currpers = 0; break;
815 default:
816 fprintf(stderr, "Unknown value CS=0x%02X while "
817 "detecting personality of process "
818 "PID=%d\n", (int)val, pid);
819 currpers = current_personality;
820 break;
821 }
822#if 0
823 /* This version analyzes the opcode of a syscall instruction.
824 * (int 0x80 on i386 vs. syscall on x86-64)
825 * It works, but is too complicated.
826 */
827 unsigned long val, rip, i;
828
829 if(upeek(pid, 8*RIP, &rip)<0)
830 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000831
Michal Ludvig0e035502002-09-23 15:41:01 +0000832 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
833 rip-=2;
834 errno = 0;
835
Roland McGrath761b5d72002-12-15 23:58:31 +0000836 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
837 if (errno)
838 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000839 strerror(errno));
840 switch (call & 0xffff)
841 {
842 /* x86-64: syscall = 0x0f 0x05 */
843 case 0x050f: currpers = 0; break;
844 /* i386: int 0x80 = 0xcd 0x80 */
845 case 0x80cd: currpers = 1; break;
846 default:
847 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000848 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000849 "Unknown syscall opcode (0x%04X) while "
850 "detecting personality of process "
851 "PID=%d\n", (int)call, pid);
852 break;
853 }
854#endif
855 if(currpers != current_personality)
856 {
857 char *names[]={"64 bit", "32 bit"};
858 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000859 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000860 pid, names[current_personality]);
861 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000862 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000863#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000864# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000865 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000866 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000867 if (!(tcp->flags & TCB_INSYSCALL)) {
868 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000869 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000870 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000871 /* Check if we return from execve. */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000872 } else {
873 if (upeek (pid, PT_R15, &scno) < 0)
874 return -1;
875 }
876 } else {
877 /* syscall in progress */
878 if (upeek (pid, PT_R8, &r8) < 0)
879 return -1;
880 if (upeek (pid, PT_R10, &r10) < 0)
881 return -1;
882 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000883 if (tcp->flags & TCB_WAITEXECVE) {
884 tcp->flags &= ~TCB_WAITEXECVE;
885 return 0;
886 }
887
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000888#elif defined (ARM)
Roland McGrath761b5d72002-12-15 23:58:31 +0000889 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000890 long pc;
891 upeek(pid, 4*15, &pc);
892 umoven(tcp, pc-4, 4, (char *)&scno);
893 scno &= 0x000fffff;
894 }
895#elif defined (M68K)
896 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
897 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000898#elif defined (MIPS)
899 if (upeek(pid, REG_A3, &a3) < 0)
900 return -1;
901
902 if(!(tcp->flags & TCB_INSYSCALL)) {
903 if (upeek(pid, REG_V0, &scno) < 0)
904 return -1;
905
906 if (scno < 0 || scno > nsyscalls) {
907 if(a3 == 0 || a3 == -1) {
908 if(debug)
909 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
910 return 0;
911 }
912 }
913 } else {
914 if (upeek(pid, REG_V0, &r2) < 0)
915 return -1;
916 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000917#elif defined (ALPHA)
918 if (upeek(pid, REG_A3, &a3) < 0)
919 return -1;
920
921 if (!(tcp->flags & TCB_INSYSCALL)) {
922 if (upeek(pid, REG_R0, &scno) < 0)
923 return -1;
924
925 /* Check if we return from execve. */
926 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
927 tcp->flags &= ~TCB_WAITEXECVE;
928 return 0;
929 }
930
931 /*
932 * Do some sanity checks to figure out if it's
933 * really a syscall entry
934 */
935 if (scno < 0 || scno > nsyscalls) {
936 if (a3 == 0 || a3 == -1) {
937 if (debug)
938 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
939 return 0;
940 }
941 }
942 }
943 else {
944 if (upeek(pid, REG_R0, &r0) < 0)
945 return -1;
946 }
947#elif defined (SPARC)
948 /* Everything we need is in the current register set. */
949 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
950 return -1;
951
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000952 /* If we are entering, then disassemble the syscall trap. */
953 if (!(tcp->flags & TCB_INSYSCALL)) {
954 /* Retrieve the syscall trap instruction. */
955 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000956 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000957 if (errno)
958 return -1;
959
960 /* Disassemble the trap to see what personality to use. */
961 switch (trap) {
962 case 0x91d02010:
963 /* Linux/SPARC syscall trap. */
964 set_personality(0);
965 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000966 case 0x91d0206d:
967 /* Linux/SPARC64 syscall trap. */
968 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
969 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000970 case 0x91d02000:
971 /* SunOS syscall trap. (pers 1) */
972 fprintf(stderr,"syscall: SunOS no support\n");
973 return -1;
974 case 0x91d02008:
975 /* Solaris 2.x syscall trap. (per 2) */
976 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +0000977 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000978 case 0x91d02009:
979 /* NetBSD/FreeBSD syscall trap. */
980 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
981 return -1;
982 case 0x91d02027:
983 /* Solaris 2.x gettimeofday */
984 set_personality(1);
985 break;
986 default:
987 /* Unknown syscall trap. */
988 if(tcp->flags & TCB_WAITEXECVE) {
989 tcp->flags &= ~TCB_WAITEXECVE;
990 return 0;
991 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000992 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000993 return -1;
994 }
995
996 /* Extract the system call number from the registers. */
997 if (trap == 0x91d02027)
998 scno = 156;
999 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001000 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001001 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001002 scno = regs.r_o0;
1003 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001004 }
1005 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001006#elif defined(HPPA)
1007 if (upeek(pid, PT_GR20, &scno) < 0)
1008 return -1;
1009 if (!(tcp->flags & TCB_INSYSCALL)) {
1010 /* Check if we return from execve. */
1011 if ((tcp->flags & TCB_WAITEXECVE)) {
1012 tcp->flags &= ~TCB_WAITEXECVE;
1013 return 0;
1014 }
1015 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001016#elif defined(SH)
1017 /*
1018 * In the new syscall ABI, the system call number is in R3.
1019 */
1020 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1021 return -1;
1022
1023 if (scno < 0) {
1024 /* Odd as it may seem, a glibc bug has been known to cause
1025 glibc to issue bogus negative syscall numbers. So for
1026 our purposes, make strace print what it *should* have been */
1027 long correct_scno = (scno & 0xff);
1028 if (debug)
1029 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001030 "Detected glibc bug: bogus system call number = %ld, "
1031 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001032 scno,
1033 correct_scno);
1034 scno = correct_scno;
1035 }
1036
1037
1038 if (!(tcp->flags & TCB_INSYSCALL)) {
1039 /* Check if we return from execve. */
1040 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1041 tcp->flags &= ~TCB_WAITEXECVE;
1042 return 0;
1043 }
1044 }
1045#endif /* SH */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001046#endif /* LINUX */
1047#ifdef SUNOS4
1048 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1049 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001050#elif defined(SH)
1051 /* new syscall ABI returns result in R0 */
1052 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1053 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001054#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001055#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001056#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001057 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001058#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001059#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001060 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001061#else /* FREEBSD */
1062 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1063 perror("pread");
1064 return -1;
1065 }
1066 switch (regs.r_eax) {
1067 case SYS_syscall:
1068 case SYS___syscall:
1069 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1070 break;
1071 default:
1072 scno = regs.r_eax;
1073 break;
1074 }
1075#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001076#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001077#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001078 if (!(tcp->flags & TCB_INSYSCALL))
1079 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001080 return 1;
1081}
1082
Pavel Machek4dc3b142000-02-01 17:58:41 +00001083
1084int
1085syscall_fixup(tcp)
1086struct tcb *tcp;
1087{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001088#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001089 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001090#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001091 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001092
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001093 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001094 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095 if (
1096 scno == SYS_fork
1097#ifdef SYS_vfork
1098 || scno == SYS_vfork
1099#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001100#ifdef SYS_fork1
1101 || scno == SYS_fork1
1102#endif /* SYS_fork1 */
1103#ifdef SYS_forkall
1104 || scno == SYS_forkall
1105#endif /* SYS_forkall */
1106#ifdef SYS_rfork1
1107 || scno == SYS_rfork1
1108#endif /* SYS_fork1 */
1109#ifdef SYS_rforkall
1110 || scno == SYS_rforkall
1111#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001112 ) {
1113 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001114 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001115 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001116 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001117 }
1118 else {
1119 fprintf(stderr, "syscall: missing entry\n");
1120 tcp->flags |= TCB_INSYSCALL;
1121 }
1122 }
1123 }
1124 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001125 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126 fprintf(stderr, "syscall: missing exit\n");
1127 tcp->flags &= ~TCB_INSYSCALL;
1128 }
1129 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001130#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131#ifdef SUNOS4
1132 if (!(tcp->flags & TCB_INSYSCALL)) {
1133 if (scno == 0) {
1134 fprintf(stderr, "syscall: missing entry\n");
1135 tcp->flags |= TCB_INSYSCALL;
1136 }
1137 }
1138 else {
1139 if (scno != 0) {
1140 if (debug) {
1141 /*
1142 * This happens when a signal handler
1143 * for a signal which interrupted a
1144 * a system call makes another system call.
1145 */
1146 fprintf(stderr, "syscall: missing exit\n");
1147 }
1148 tcp->flags &= ~TCB_INSYSCALL;
1149 }
1150 }
1151#endif /* SUNOS4 */
1152#ifdef LINUX
1153#if defined (I386)
1154 if (upeek(pid, 4*EAX, &eax) < 0)
1155 return -1;
1156 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1157 if (debug)
1158 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1159 return 0;
1160 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001161#elif defined (X86_64)
1162 if (upeek(pid, 8*RAX, &rax) < 0)
1163 return -1;
1164 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1165 if (debug)
1166 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1167 return 0;
1168 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001169#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001170 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1171 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001172 if (syscall_mode != -ENOSYS)
1173 syscall_mode = tcp->scno;
1174 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001175 if (debug)
1176 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1177 return 0;
1178 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001179#elif defined (POWERPC)
1180# define SO_MASK 0x10000000
1181 if (upeek(pid, 4*PT_CCR, &flags) < 0)
1182 return -1;
1183 if (upeek(pid, 4*PT_R3, &result) < 0)
1184 return -1;
1185 if (flags & SO_MASK)
1186 result = -result;
1187#elif defined (M68K)
1188 if (upeek(pid, 4*PT_D0, &d0) < 0)
1189 return -1;
1190 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1191 if (debug)
1192 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1193 return 0;
1194 }
1195#elif defined (ARM)
1196 if (upeek(pid, 4*0, (long *)&r0) < 0)
1197 return -1;
1198 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1199 if (debug)
1200 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1201 return 0;
1202 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001203#elif defined (HPPA)
1204 if (upeek(pid, PT_GR28, &r28) < 0)
1205 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001206#elif defined(IA64)
1207 if (upeek(pid, PT_R10, &r10) < 0)
1208 return -1;
1209 if (upeek(pid, PT_R8, &r8) < 0)
1210 return -1;
1211 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1212 if (debug)
1213 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1214 return 0;
1215 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001216#endif
1217#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001218 return 1;
1219}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001220
Pavel Machek4dc3b142000-02-01 17:58:41 +00001221int
1222get_error(tcp)
1223struct tcb *tcp;
1224{
1225 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001226#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001227#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001228 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1229 tcp->u_rval = -1;
1230 u_error = -gpr2;
1231 }
1232 else {
1233 tcp->u_rval = gpr2;
1234 u_error = 0;
1235 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001236#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001237#ifdef I386
1238 if (eax < 0 && -eax < nerrnos) {
1239 tcp->u_rval = -1;
1240 u_error = -eax;
1241 }
1242 else {
1243 tcp->u_rval = eax;
1244 u_error = 0;
1245 }
1246#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001247#ifdef X86_64
1248 if (rax < 0 && -rax < nerrnos) {
1249 tcp->u_rval = -1;
1250 u_error = -rax;
1251 }
1252 else {
1253 tcp->u_rval = rax;
1254 u_error = 0;
1255 }
1256#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001257#ifdef IA64
1258 if (ia32) {
1259 int err;
1260
1261 err = (int)r8;
1262 if (err < 0 && -err < nerrnos) {
1263 tcp->u_rval = -1;
1264 u_error = -err;
1265 }
1266 else {
1267 tcp->u_rval = err;
1268 u_error = 0;
1269 }
1270 } else {
1271 if (r10) {
1272 tcp->u_rval = -1;
1273 u_error = r8;
1274 } else {
1275 tcp->u_rval = r8;
1276 u_error = 0;
1277 }
1278 }
1279#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001280#ifdef MIPS
1281 if (a3) {
1282 tcp->u_rval = -1;
1283 u_error = r2;
1284 } else {
1285 tcp->u_rval = r2;
1286 u_error = 0;
1287 }
1288#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001289#ifdef POWERPC
1290 if (result && (unsigned) -result < nerrnos) {
1291 tcp->u_rval = -1;
1292 u_error = -result;
1293 }
1294 else {
1295 tcp->u_rval = result;
1296 u_error = 0;
1297 }
1298#else /* !POWERPC */
1299#ifdef M68K
1300 if (d0 && (unsigned) -d0 < nerrnos) {
1301 tcp->u_rval = -1;
1302 u_error = -d0;
1303 }
1304 else {
1305 tcp->u_rval = d0;
1306 u_error = 0;
1307 }
1308#else /* !M68K */
1309#ifdef ARM
1310 if (r0 && (unsigned) -r0 < nerrnos) {
1311 tcp->u_rval = -1;
1312 u_error = -r0;
1313 }
1314 else {
1315 tcp->u_rval = r0;
1316 u_error = 0;
1317 }
1318#else /* !ARM */
1319#ifdef ALPHA
1320 if (a3) {
1321 tcp->u_rval = -1;
1322 u_error = r0;
1323 }
1324 else {
1325 tcp->u_rval = r0;
1326 u_error = 0;
1327 }
1328#else /* !ALPHA */
1329#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001330 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001331 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001332 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001333 }
1334 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001335 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001336 u_error = 0;
1337 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001338#else /* !SPARC */
1339#ifdef HPPA
1340 if (r28 && (unsigned) -r28 < nerrnos) {
1341 tcp->u_rval = -1;
1342 u_error = -r28;
1343 }
1344 else {
1345 tcp->u_rval = r28;
1346 u_error = 0;
1347 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001348#else
1349#ifdef SH
1350 /* interpret R0 as return value or error number */
1351 if (r0 && (unsigned) -r0 < nerrnos) {
1352 tcp->u_rval = -1;
1353 u_error = -r0;
1354 }
1355 else {
1356 tcp->u_rval = r0;
1357 u_error = 0;
1358 }
1359#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001360#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361#endif /* SPARC */
1362#endif /* ALPHA */
1363#endif /* ARM */
1364#endif /* M68K */
1365#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001366#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001367#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001368#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001369#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001370#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001371#endif /* LINUX */
1372#ifdef SUNOS4
1373 /* get error code from user struct */
1374 if (upeek(pid, uoff(u_error), &u_error) < 0)
1375 return -1;
1376 u_error >>= 24; /* u_error is a char */
1377
1378 /* get system call return value */
1379 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1380 return -1;
1381#endif /* SUNOS4 */
1382#ifdef SVR4
1383#ifdef SPARC
1384 /* Judicious guessing goes a long way. */
1385 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1386 tcp->u_rval = -1;
1387 u_error = tcp->status.pr_reg[R_O0];
1388 }
1389 else {
1390 tcp->u_rval = tcp->status.pr_reg[R_O0];
1391 u_error = 0;
1392 }
1393#endif /* SPARC */
1394#ifdef I386
1395 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001396 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001397 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001398 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001399 }
1400 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001401 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001402#ifdef HAVE_LONG_LONG
1403 tcp->u_lrval =
1404 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1405 tcp->status.PR_REG[EAX];
1406#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001407 u_error = 0;
1408 }
1409#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001410#ifdef X86_64
1411 /* Wanna know how to kill an hour single-stepping? */
1412 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1413 tcp->u_rval = -1;
1414 u_error = tcp->status.PR_REG[RAX];
1415 }
1416 else {
1417 tcp->u_rval = tcp->status.PR_REG[RAX];
1418 u_error = 0;
1419 }
1420#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001421#ifdef MIPS
1422 if (tcp->status.pr_reg[CTX_A3]) {
1423 tcp->u_rval = -1;
1424 u_error = tcp->status.pr_reg[CTX_V0];
1425 }
1426 else {
1427 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1428 u_error = 0;
1429 }
1430#endif /* MIPS */
1431#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001432#ifdef FREEBSD
1433 if (regs.r_eflags & PSL_C) {
1434 tcp->u_rval = -1;
1435 u_error = regs.r_eax;
1436 } else {
1437 tcp->u_rval = regs.r_eax;
1438 tcp->u_lrval =
1439 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1440 u_error = 0;
1441 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001442#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001443 tcp->u_error = u_error;
1444 return 1;
1445}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001446
Roland McGrathb69f81b2002-12-21 23:25:18 +00001447int
1448force_result(tcp, error, rval)
1449 struct tcb *tcp;
1450 int error;
1451 long rval;
1452{
1453#ifdef LINUX
1454#if defined(S390) || defined(S390X)
1455 gpr2 = error ? -error : rval;
1456 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1457 return -1;
1458 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1459 return -1;
1460#else /* !S390 && !S390X */
1461#ifdef I386
1462 eax = error ? -error : rval;
1463 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1464 return -1;
1465#else /* !I386 */
1466#ifdef X86_64
1467 rax = error ? -error : rval;
1468 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1469 return -1;
1470#else
1471#ifdef IA64
1472 if (ia32) {
1473 r8 = error ? -error : rval;
1474 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1475 return -1;
1476 }
1477 else {
1478 if (error) {
1479 r8 = error;
1480 r10 = -1;
1481 }
1482 else {
1483 r8 = rval;
1484 r10 = 0;
1485 }
1486 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1487 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1488 return -1;
1489 }
1490#else /* !IA64 */
1491#ifdef MIPS
1492 if (error) {
1493 r2 = error;
1494 a3 = -1;
1495 }
1496 else {
1497 r2 = rval;
1498 a3 = 0;
1499 }
1500 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1501 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1502 return -1;
1503#else
1504#ifdef POWERPC
1505 if (upeek(tcp->pid, 4*PT_CCR, &flags) < 0)
1506 return -1;
1507 if (error) {
1508 flags |= SO_MASK;
1509 result = error;
1510 }
1511 else {
1512 flags &= ~SO_MASK;
1513 result = rval;
1514 }
1515 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_CCR), flags) < 0 ||
1516 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R3), result) < 0)
1517 return -1;
1518#else /* !POWERPC */
1519#ifdef M68K
1520 d0 = error ? -error : rval;
1521 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1522 return -1;
1523#else /* !M68K */
1524#ifdef ARM
1525 r0 = error ? -error : rval;
1526 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1527 return -1;
1528#else /* !ARM */
1529#ifdef ALPHA
1530 if (error) {
1531 a3 = -1;
1532 r0 = error;
1533 }
1534 else {
1535 a3 = 0;
1536 r0 = rval;
1537 }
1538 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1539 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1540 return -1;
1541#else /* !ALPHA */
1542#ifdef SPARC
1543 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1544 return -1;
1545 if (error) {
1546 regs.r_psr |= PSR_C;
1547 regs.r_o0 = error;
1548 }
1549 else {
1550 regs.r_psr &= ~PSR_C;
1551 regs.r_o0 = rval;
1552 }
1553 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1554 return -1;
1555#else /* !SPARC */
1556#ifdef HPPA
1557 r28 = error ? -error : rval;
1558 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1559 return -1;
1560#else
1561#ifdef SH
1562 r0 = error ? -error : rval;
1563 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1564 return -1;
1565#endif /* SH */
1566#endif /* HPPA */
1567#endif /* SPARC */
1568#endif /* ALPHA */
1569#endif /* ARM */
1570#endif /* M68K */
1571#endif /* POWERPC */
1572#endif /* MIPS */
1573#endif /* IA64 */
1574#endif /* X86_64 */
1575#endif /* I386 */
1576#endif /* S390 || S390X */
1577#endif /* LINUX */
1578#ifdef SUNOS4
1579 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1580 error << 24) < 0 ||
1581 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1582 return -1;
1583#endif /* SUNOS4 */
1584#ifdef SVR4
1585 /* XXX no clue */
1586 return -1;
1587#endif /* SVR4 */
1588#ifdef FREEBSD
1589 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1590 perror("pread");
1591 return -1;
1592 }
1593 if (error) {
1594 regs.r_eflags |= PSL_C;
1595 regs.r_eax = error;
1596 }
1597 else {
1598 regs.r_eflags &= ~PSL_C;
1599 regs.r_eax = rval;
1600 }
1601 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1602 perror("pwrite");
1603 return -1;
1604 }
1605#endif /* FREEBSD */
1606
1607 /* All branches reach here on success (only). */
1608 tcp->u_error = error;
1609 tcp->u_rval = rval;
1610 return 0;
1611}
1612
Pavel Machek4dc3b142000-02-01 17:58:41 +00001613int syscall_enter(tcp)
1614struct tcb *tcp;
1615{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001616#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001617 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001618#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001619#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001620#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001621 {
1622 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001623 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1624 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001625 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001626 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001627 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001628 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001629 return -1;
1630 }
1631 }
1632#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633 {
1634 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001635 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1636 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001637 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001638 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001640 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1641 * for scno somewhere above here!
1642 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1644 return -1;
1645 }
1646 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001647#elif defined (IA64)
1648 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001649 if (!ia32) {
1650 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1651 /* be backwards compatible with kernel < 2.4.4... */
1652# ifndef PT_RBS_END
1653# define PT_RBS_END PT_AR_BSP
1654# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001655
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001656 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001657 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001658 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1659 return -1;
1660
1661 sof = (cfm >> 0) & 0x7f;
1662 sol = (cfm >> 7) & 0x7f;
1663 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1664
1665 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1666 && sysent[tcp->scno].nargs != -1)
1667 tcp->u_nargs = sysent[tcp->scno].nargs;
1668 else
1669 tcp->u_nargs = MAX_ARGS;
1670 for (i = 0; i < tcp->u_nargs; ++i) {
1671 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1672 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1673 return -1;
1674 }
1675 } else {
1676 int i;
1677
1678 if (/* EBX = out0 */
1679 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1680 /* ECX = out1 */
1681 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1682 /* EDX = out2 */
1683 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1684 /* ESI = out3 */
1685 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1686 /* EDI = out4 */
1687 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1688 /* EBP = out5 */
1689 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1690 return -1;
1691
1692 for (i = 0; i < 6; ++i)
1693 /* truncate away IVE sign-extension */
1694 tcp->u_arg[i] &= 0xffffffff;
1695
1696 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1697 && sysent[tcp->scno].nargs != -1)
1698 tcp->u_nargs = sysent[tcp->scno].nargs;
1699 else
1700 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001701 }
1702 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001703#elif defined (MIPS)
1704 {
1705 long sp;
1706 int i, nargs;
1707
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001708 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1709 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001710 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001711 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001712 if(nargs > 4) {
1713 if(upeek(pid, REG_SP, &sp) < 0)
1714 return -1;
1715 for(i = 0; i < 4; i++) {
1716 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1717 return -1;
1718 }
1719 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1720 (char *)(tcp->u_arg + 4));
1721 } else {
1722 for(i = 0; i < nargs; i++) {
1723 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1724 return -1;
1725 }
1726 }
1727 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001728#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001729#ifndef PT_ORIG_R3
1730#define PT_ORIG_R3 34
1731#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001732 {
1733 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001734 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1735 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001736 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001737 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001738 for (i = 0; i < tcp->u_nargs; i++) {
1739 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1740 return -1;
1741 }
1742 }
1743#elif defined (SPARC)
1744 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001745 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001746
1747 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1748 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001749 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001750 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001751 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001752 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001753 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001754#elif defined (HPPA)
1755 {
1756 int i;
1757
1758 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1759 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001760 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001761 tcp->u_nargs = MAX_ARGS;
1762 for (i = 0; i < tcp->u_nargs; i++) {
1763 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1764 return -1;
1765 }
1766 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001767#elif defined(SH)
1768 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001769 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001770 static int syscall_regs[] = {
1771 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1772 REG_REG0, REG_REG0+1, REG_REG0+2
1773 };
1774
1775 tcp->u_nargs = sysent[tcp->scno].nargs;
1776 for (i = 0; i < tcp->u_nargs; i++) {
1777 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1778 return -1;
1779 }
1780 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001781#elif defined(X86_64)
1782 {
1783 int i;
1784 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1785 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1786 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1787 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001788
Michal Ludvig0e035502002-09-23 15:41:01 +00001789 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1790 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001791 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001792 tcp->u_nargs = MAX_ARGS;
1793 for (i = 0; i < tcp->u_nargs; i++) {
1794 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1795 return -1;
1796 }
1797 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001798#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001799 {
1800 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001801 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1802 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001803 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001804 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001805 for (i = 0; i < tcp->u_nargs; i++) {
1806 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1807 return -1;
1808 }
1809 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001810#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001811#endif /* LINUX */
1812#ifdef SUNOS4
1813 {
1814 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001815 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1816 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001817 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001818 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001819 for (i = 0; i < tcp->u_nargs; i++) {
1820 struct user *u;
1821
1822 if (upeek(pid, uoff(u_arg[0]) +
1823 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1824 return -1;
1825 }
1826 }
1827#endif /* SUNOS4 */
1828#ifdef SVR4
1829#ifdef MIPS
1830 /*
1831 * SGI is broken: even though it has pr_sysarg, it doesn't
1832 * set them on system call entry. Get a clue.
1833 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001834 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001835 tcp->u_nargs = sysent[tcp->scno].nargs;
1836 else
1837 tcp->u_nargs = tcp->status.pr_nsysarg;
1838 if (tcp->u_nargs > 4) {
1839 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1840 4*sizeof(tcp->u_arg[0]));
1841 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1842 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1843 }
1844 else {
1845 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1846 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1847 }
John Hughes25299712001-03-06 10:10:06 +00001848#elif UNIXWARE >= 2
1849 /*
1850 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1851 */
1852 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1853 tcp->u_nargs = sysent[tcp->scno].nargs;
1854 else
1855 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1856 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1857 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1858#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001859 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001860 tcp->u_nargs = sysent[tcp->scno].nargs;
1861 else
1862 tcp->u_nargs = tcp->status.pr_nsysarg;
1863 {
1864 int i;
1865 for (i = 0; i < tcp->u_nargs; i++)
1866 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1867 }
John Hughes25299712001-03-06 10:10:06 +00001868#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001869 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001870 tcp->u_nargs = sysent[tcp->scno].nargs;
1871 else
1872 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001873 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001874 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001875#else
1876 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001877#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001879#ifdef FREEBSD
1880 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1881 sysent[tcp->scno].nargs > tcp->status.val)
1882 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001883 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001884 tcp->u_nargs = tcp->status.val;
1885 if (tcp->u_nargs < 0)
1886 tcp->u_nargs = 0;
1887 if (tcp->u_nargs > MAX_ARGS)
1888 tcp->u_nargs = MAX_ARGS;
1889 switch(regs.r_eax) {
1890 case SYS___syscall:
1891 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1892 regs.r_esp + sizeof(int) + sizeof(quad_t));
1893 break;
1894 case SYS_syscall:
1895 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1896 regs.r_esp + 2 * sizeof(int));
1897 break;
1898 default:
1899 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1900 regs.r_esp + sizeof(int));
1901 break;
1902 }
1903#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001904 return 1;
1905}
1906
1907int
1908trace_syscall(tcp)
1909struct tcb *tcp;
1910{
1911 int sys_res;
1912 struct timeval tv;
1913 int res;
1914
1915 /* Measure the exit time as early as possible to avoid errors. */
1916 if (dtime && (tcp->flags & TCB_INSYSCALL))
1917 gettimeofday(&tv, NULL);
1918
1919 res = get_scno(tcp);
1920 if (res != 1)
1921 return res;
1922
1923 res = syscall_fixup(tcp);
1924 if (res != 1)
1925 return res;
1926
1927 if (tcp->flags & TCB_INSYSCALL) {
1928 long u_error;
1929 res = get_error(tcp);
1930 if (res != 1)
1931 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001932
1933 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001934 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1935 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001936 tcp->flags &= ~TCB_INSYSCALL;
1937 return 0;
1938 }
1939
1940 if (tcp->flags & TCB_REPRINT) {
1941 printleader(tcp);
1942 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001943 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001944 tprintf("syscall_%lu", tcp->scno);
1945 else
1946 tprintf("%s", sysent[tcp->scno].sys_name);
1947 tprintf(" resumed> ");
1948 }
1949
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001950 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001951 call_count[tcp->scno]++;
1952 if (tcp->u_error)
1953 error_count[tcp->scno]++;
1954 tv_sub(&tv, &tv, &tcp->etime);
1955#ifdef LINUX
1956 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1957 static struct timeval one_tick =
1958 { 0, 1000000 / HZ };
1959
1960 if (tv_nz(&tcp->dtime))
1961 tv = tcp->dtime;
1962 else if (tv_cmp(&tv, &one_tick) > 0) {
1963 if (tv_cmp(&shortest, &one_tick) < 0)
1964 tv = shortest;
1965 else
1966 tv = one_tick;
1967 }
1968 }
1969#endif /* LINUX */
1970 if (tv_cmp(&tv, &shortest) < 0)
1971 shortest = tv;
1972 tv_add(&tv_count[tcp->scno],
1973 &tv_count[tcp->scno], &tv);
1974 tcp->flags &= ~TCB_INSYSCALL;
1975 return 0;
1976 }
1977
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001978 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001979 || (qual_flags[tcp->scno] & QUAL_RAW))
1980 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001981 else {
1982 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00001983 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001984 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00001985 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001986 u_error = tcp->u_error;
1987 tprintf(") ");
1988 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001989 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1990 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001991 if (u_error)
1992 tprintf("= -1 (errno %ld)", u_error);
1993 else
1994 tprintf("= %#lx", tcp->u_rval);
1995 }
1996 else if (!(sys_res & RVAL_NONE) && u_error) {
1997 switch (u_error) {
1998#ifdef LINUX
1999 case ERESTARTSYS:
2000 tprintf("= ? ERESTARTSYS (To be restarted)");
2001 break;
2002 case ERESTARTNOINTR:
2003 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2004 break;
2005 case ERESTARTNOHAND:
2006 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2007 break;
2008#endif /* LINUX */
2009 default:
2010 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002011 if (u_error < 0)
2012 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002013 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002014 tprintf("%s (%s)", errnoent[u_error],
2015 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002016 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002017 tprintf("ERRNO_%ld (%s)", u_error,
2018 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002019 break;
2020 }
2021 }
2022 else {
2023 if (sys_res & RVAL_NONE)
2024 tprintf("= ?");
2025 else {
2026 switch (sys_res & RVAL_MASK) {
2027 case RVAL_HEX:
2028 tprintf("= %#lx", tcp->u_rval);
2029 break;
2030 case RVAL_OCTAL:
2031 tprintf("= %#lo", tcp->u_rval);
2032 break;
2033 case RVAL_UDECIMAL:
2034 tprintf("= %lu", tcp->u_rval);
2035 break;
2036 case RVAL_DECIMAL:
2037 tprintf("= %ld", tcp->u_rval);
2038 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002039#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002040 case RVAL_LHEX:
2041 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002042 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002043 case RVAL_LOCTAL:
2044 tprintf("= %#llo", tcp->u_lrval);
2045 break;
2046 case RVAL_LUDECIMAL:
2047 tprintf("= %llu", tcp->u_lrval);
2048 break;
2049 case RVAL_LDECIMAL:
2050 tprintf("= %lld", tcp->u_lrval);
2051 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002052#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002053 default:
2054 fprintf(stderr,
2055 "invalid rval format\n");
2056 break;
2057 }
2058 }
2059 if ((sys_res & RVAL_STR) && tcp->auxstr)
2060 tprintf(" (%s)", tcp->auxstr);
2061 }
2062 if (dtime) {
2063 tv_sub(&tv, &tv, &tcp->etime);
2064 tprintf(" <%ld.%06ld>",
2065 (long) tv.tv_sec, (long) tv.tv_usec);
2066 }
2067 printtrailer(tcp);
2068
2069 dumpio(tcp);
2070 if (fflush(tcp->outf) == EOF)
2071 return -1;
2072 tcp->flags &= ~TCB_INSYSCALL;
2073 return 0;
2074 }
2075
2076 /* Entering system call */
2077 res = syscall_enter(tcp);
2078 if (res != 1)
2079 return res;
2080
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002081 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002082#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002083#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002084 case SYS_socketcall:
2085 decode_subcall(tcp, SYS_socket_subcall,
2086 SYS_socket_nsubcalls, deref_style);
2087 break;
2088 case SYS_ipc:
2089 decode_subcall(tcp, SYS_ipc_subcall,
2090 SYS_ipc_nsubcalls, shift_style);
2091 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002092#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002093#ifdef SPARC
2094 case SYS_socketcall:
2095 sparc_socket_decode (tcp);
2096 break;
2097#endif
2098#endif /* LINUX */
2099#ifdef SVR4
2100#ifdef SYS_pgrpsys_subcall
2101 case SYS_pgrpsys:
2102 decode_subcall(tcp, SYS_pgrpsys_subcall,
2103 SYS_pgrpsys_nsubcalls, shift_style);
2104 break;
2105#endif /* SYS_pgrpsys_subcall */
2106#ifdef SYS_sigcall_subcall
2107 case SYS_sigcall:
2108 decode_subcall(tcp, SYS_sigcall_subcall,
2109 SYS_sigcall_nsubcalls, mask_style);
2110 break;
2111#endif /* SYS_sigcall_subcall */
2112 case SYS_msgsys:
2113 decode_subcall(tcp, SYS_msgsys_subcall,
2114 SYS_msgsys_nsubcalls, shift_style);
2115 break;
2116 case SYS_shmsys:
2117 decode_subcall(tcp, SYS_shmsys_subcall,
2118 SYS_shmsys_nsubcalls, shift_style);
2119 break;
2120 case SYS_semsys:
2121 decode_subcall(tcp, SYS_semsys_subcall,
2122 SYS_semsys_nsubcalls, shift_style);
2123 break;
2124#if 0 /* broken */
2125 case SYS_utssys:
2126 decode_subcall(tcp, SYS_utssys_subcall,
2127 SYS_utssys_nsubcalls, shift_style);
2128 break;
2129#endif
2130 case SYS_sysfs:
2131 decode_subcall(tcp, SYS_sysfs_subcall,
2132 SYS_sysfs_nsubcalls, shift_style);
2133 break;
2134 case SYS_spcall:
2135 decode_subcall(tcp, SYS_spcall_subcall,
2136 SYS_spcall_nsubcalls, shift_style);
2137 break;
2138#ifdef SYS_context_subcall
2139 case SYS_context:
2140 decode_subcall(tcp, SYS_context_subcall,
2141 SYS_context_nsubcalls, shift_style);
2142 break;
2143#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002144#ifdef SYS_door_subcall
2145 case SYS_door:
2146 decode_subcall(tcp, SYS_door_subcall,
2147 SYS_door_nsubcalls, door_style);
2148 break;
2149#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002150#ifdef SYS_kaio_subcall
2151 case SYS_kaio:
2152 decode_subcall(tcp, SYS_kaio_subcall,
2153 SYS_kaio_nsubcalls, shift_style);
2154 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002155#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002156#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002157#ifdef FREEBSD
2158 case SYS_msgsys:
2159 case SYS_shmsys:
2160 case SYS_semsys:
2161 decode_subcall(tcp, 0, 0, table_style);
2162 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002163#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002164#ifdef SUNOS4
2165 case SYS_semsys:
2166 decode_subcall(tcp, SYS_semsys_subcall,
2167 SYS_semsys_nsubcalls, shift_style);
2168 break;
2169 case SYS_msgsys:
2170 decode_subcall(tcp, SYS_msgsys_subcall,
2171 SYS_msgsys_nsubcalls, shift_style);
2172 break;
2173 case SYS_shmsys:
2174 decode_subcall(tcp, SYS_shmsys_subcall,
2175 SYS_shmsys_nsubcalls, shift_style);
2176 break;
2177#endif
2178 }
2179
2180 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002181 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002182 tcp->flags |= TCB_INSYSCALL;
2183 return 0;
2184 }
2185
2186 if (cflag) {
2187 gettimeofday(&tcp->etime, NULL);
2188 tcp->flags |= TCB_INSYSCALL;
2189 return 0;
2190 }
2191
2192 printleader(tcp);
2193 tcp->flags &= ~TCB_REPRINT;
2194 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002195 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002196 tprintf("syscall_%lu(", tcp->scno);
2197 else
2198 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002199 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002200 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2201 sys_res = printargs(tcp);
2202 else
2203 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2204 if (fflush(tcp->outf) == EOF)
2205 return -1;
2206 tcp->flags |= TCB_INSYSCALL;
2207 /* Measure the entrance time as late as possible to avoid errors. */
2208 if (dtime)
2209 gettimeofday(&tcp->etime, NULL);
2210 return sys_res;
2211}
2212
2213int
2214printargs(tcp)
2215struct tcb *tcp;
2216{
2217 if (entering(tcp)) {
2218 int i;
2219
2220 for (i = 0; i < tcp->u_nargs; i++)
2221 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2222 }
2223 return 0;
2224}
2225
2226long
2227getrval2(tcp)
2228struct tcb *tcp;
2229{
2230 long val = -1;
2231
2232#ifdef LINUX
2233#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002234 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002235 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2236 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002237 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002238#endif /* SPARC */
2239#endif /* LINUX */
2240
2241#ifdef SUNOS4
2242 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2243 return -1;
2244#endif /* SUNOS4 */
2245
2246#ifdef SVR4
2247#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002248 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002249#endif /* SPARC */
2250#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002251 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002252#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002253#ifdef X86_64
2254 val = tcp->status.PR_REG[RDX];
2255#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002256#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002257 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002258#endif /* MIPS */
2259#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002260#ifdef FREEBSD
2261 struct reg regs;
2262 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2263 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002264#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002265 return val;
2266}
2267
2268/*
2269 * Apparently, indirect system calls have already be converted by ptrace(2),
2270 * so if you see "indir" this program has gone astray.
2271 */
2272int
2273sys_indir(tcp)
2274struct tcb *tcp;
2275{
2276 int i, scno, nargs;
2277
2278 if (entering(tcp)) {
2279 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2280 fprintf(stderr, "Bogus syscall: %u\n", scno);
2281 return 0;
2282 }
2283 nargs = sysent[scno].nargs;
2284 tprintf("%s", sysent[scno].sys_name);
2285 for (i = 0; i < nargs; i++)
2286 tprintf(", %#lx", tcp->u_arg[i+1]);
2287 }
2288 return 0;
2289}
2290
2291static int
2292time_cmp(a, b)
2293void *a;
2294void *b;
2295{
2296 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2297}
2298
2299static int
2300syscall_cmp(a, b)
2301void *a;
2302void *b;
2303{
2304 return strcmp(sysent[*((int *) a)].sys_name,
2305 sysent[*((int *) b)].sys_name);
2306}
2307
2308static int
2309count_cmp(a, b)
2310void *a;
2311void *b;
2312{
2313 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2314
2315 return (m < n) ? 1 : (m > n) ? -1 : 0;
2316}
2317
2318static int (*sortfun)();
2319static struct timeval overhead = { -1, -1 };
2320
2321void
2322set_sortby(sortby)
2323char *sortby;
2324{
2325 if (strcmp(sortby, "time") == 0)
2326 sortfun = time_cmp;
2327 else if (strcmp(sortby, "calls") == 0)
2328 sortfun = count_cmp;
2329 else if (strcmp(sortby, "name") == 0)
2330 sortfun = syscall_cmp;
2331 else if (strcmp(sortby, "nothing") == 0)
2332 sortfun = NULL;
2333 else {
2334 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2335 exit(1);
2336 }
2337}
2338
2339void set_overhead(n)
2340int n;
2341{
2342 overhead.tv_sec = n / 1000000;
2343 overhead.tv_usec = n % 1000000;
2344}
2345
2346void
2347call_summary(outf)
2348FILE *outf;
2349{
2350 int i, j;
2351 int call_cum, error_cum;
2352 struct timeval tv_cum, dtv;
2353 double percent;
2354 char *dashes = "-------------------------";
2355 char error_str[16];
2356
2357 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2358 if (overhead.tv_sec == -1) {
2359 tv_mul(&overhead, &shortest, 8);
2360 tv_div(&overhead, &overhead, 10);
2361 }
2362 for (i = 0; i < nsyscalls; i++) {
2363 sorted_count[i] = i;
2364 if (call_count[i] == 0)
2365 continue;
2366 tv_mul(&dtv, &overhead, call_count[i]);
2367 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2368 call_cum += call_count[i];
2369 error_cum += error_count[i];
2370 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2371 }
2372 if (sortfun)
2373 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2374 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2375 "% time", "seconds", "usecs/call",
2376 "calls", "errors", "syscall");
2377 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2378 dashes, dashes, dashes, dashes, dashes, dashes);
2379 for (i = 0; i < nsyscalls; i++) {
2380 j = sorted_count[i];
2381 if (call_count[j] == 0)
2382 continue;
2383 tv_div(&dtv, &tv_count[j], call_count[j]);
2384 if (error_count[j])
2385 sprintf(error_str, "%d", error_count[j]);
2386 else
2387 error_str[0] = '\0';
2388 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2389 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2390 percent, (long) tv_count[j].tv_sec,
2391 (long) tv_count[j].tv_usec,
2392 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2393 call_count[j], error_str, sysent[j].sys_name);
2394 }
2395 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2396 dashes, dashes, dashes, dashes, dashes, dashes);
2397 if (error_cum)
2398 sprintf(error_str, "%d", error_cum);
2399 else
2400 error_str[0] = '\0';
2401 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2402 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2403 call_cum, error_str, "total");
2404}