blob: ffd8f658215f91e4e46feef1e88cc8bd9c467d9b [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 Akkerman9ce1a631999-08-29 23:15:07 +000046#include <asm/reg.h>
47#endif
48
Wichert Akkerman15dea971999-10-06 13:06:34 +000049#ifdef HAVE_SYS_REG_H
50#include <sys/reg.h>
51#ifndef PTRACE_PEEKUSR
52# define PTRACE_PEEKUSR PTRACE_PEEKUSER
53#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000054#elif defined(HAVE_LINUX_PTRACE_H)
55#undef PTRACE_SYSCALL
56#include <linux/ptrace.h>
Wichert Akkerman15dea971999-10-06 13:06:34 +000057#endif
58
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000059#if defined(LINUX) && defined(IA64)
60# include <asm/ptrace_offsets.h>
61# include <asm/rse.h>
62#endif
63
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#ifndef SYS_ERRLIST_DECLARED
65extern int sys_nerr;
66extern char *sys_errlist[];
67#endif /* SYS_ERRLIST_DECLARED */
68
Pavel Machekd8ae7e32000-02-01 17:17:25 +000069#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000070#ifdef LINUX
71#ifndef ERESTARTSYS
72#define ERESTARTSYS 512
73#endif
74#ifndef ERESTARTNOINTR
75#define ERESTARTNOINTR 513
76#endif
77#ifndef ERESTARTNOHAND
78#define ERESTARTNOHAND 514 /* restart if no handler.. */
79#endif
80#ifndef ENOIOCTLCMD
81#define ENOIOCTLCMD 515 /* No ioctl command */
82#endif
83#ifndef NSIG
84#define NSIG 32
85#endif
86#ifdef ARM
87#undef NSIG
88#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000089#undef NR_SYSCALL_BASE
90#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
92#endif /* LINUX */
93
94#include "syscall.h"
95
96/* Define these shorthand notations to simplify the syscallent files. */
97#define TF TRACE_FILE
98#define TI TRACE_IPC
99#define TN TRACE_NETWORK
100#define TP TRACE_PROCESS
101#define TS TRACE_SIGNAL
102
103struct sysent sysent0[] = {
104#include "syscallent.h"
105};
106int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
107
108#if SUPPORTED_PERSONALITIES >= 2
109struct sysent sysent1[] = {
110#include "syscallent1.h"
111};
112int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
113#endif /* SUPPORTED_PERSONALITIES >= 2 */
114
115#if SUPPORTED_PERSONALITIES >= 3
116struct sysent sysent2[] = {
117#include "syscallent2.h"
118};
119int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
120#endif /* SUPPORTED_PERSONALITIES >= 3 */
121
122struct sysent *sysent;
123int nsyscalls;
124
125/* Now undef them since short defines cause wicked namespace pollution. */
126#undef TF
127#undef TI
128#undef TN
129#undef TP
130#undef TS
131
132char *errnoent0[] = {
133#include "errnoent.h"
134};
135int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
136
137#if SUPPORTED_PERSONALITIES >= 2
138char *errnoent1[] = {
139#include "errnoent1.h"
140};
141int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
142#endif /* SUPPORTED_PERSONALITIES >= 2 */
143
144#if SUPPORTED_PERSONALITIES >= 3
145char *errnoent2[] = {
146#include "errnoent2.h"
147};
148int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
149#endif /* SUPPORTED_PERSONALITIES >= 3 */
150
151char **errnoent;
152int nerrnos;
153
154int current_personality;
155
156int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000157set_personality(personality)
158int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159{
160 switch (personality) {
161 case 0:
162 errnoent = errnoent0;
163 nerrnos = nerrnos0;
164 sysent = sysent0;
165 nsyscalls = nsyscalls0;
166 ioctlent = ioctlent0;
167 nioctlents = nioctlents0;
168 signalent = signalent0;
169 nsignals = nsignals0;
170 break;
171
172#if SUPPORTED_PERSONALITIES >= 2
173 case 1:
174 errnoent = errnoent1;
175 nerrnos = nerrnos1;
176 sysent = sysent1;
177 nsyscalls = nsyscalls1;
178 ioctlent = ioctlent1;
179 nioctlents = nioctlents1;
180 signalent = signalent1;
181 nsignals = nsignals1;
182 break;
183#endif /* SUPPORTED_PERSONALITIES >= 2 */
184
185#if SUPPORTED_PERSONALITIES >= 3
186 case 2:
187 errnoent = errnoent2;
188 nerrnos = nerrnos2;
189 sysent = sysent2;
190 nsyscalls = nsyscalls2;
191 ioctlent = ioctlent2;
192 nioctlents = nioctlents2;
193 signalent = signalent2;
194 nsignals = nsignals2;
195 break;
196#endif /* SUPPORTED_PERSONALITIES >= 3 */
197
198 default:
199 return -1;
200 }
201
202 current_personality = personality;
203 return 0;
204}
205
206int qual_flags[MAX_QUALS];
207
208static int call_count[MAX_QUALS];
209static int error_count[MAX_QUALS];
210static struct timeval tv_count[MAX_QUALS];
211static int sorted_count[MAX_QUALS];
212
213static struct timeval shortest = { 1000000, 0 };
214
215static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
216
217static struct qual_options {
218 int bitflag;
219 char *option_name;
220 int (*lookup)();
221 char *argument_name;
222} qual_options[] = {
223 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
224 { QUAL_TRACE, "t", lookup_syscall, "system call" },
225 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
226 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
227 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
228 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
229 { QUAL_RAW, "raw", lookup_syscall, "system call" },
230 { QUAL_RAW, "x", lookup_syscall, "system call" },
231 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
232 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
233 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
234 { QUAL_FAULT, "fault", lookup_fault, "fault" },
235 { QUAL_FAULT, "faults", lookup_fault, "fault" },
236 { QUAL_FAULT, "m", lookup_fault, "fault" },
237 { QUAL_READ, "read", lookup_desc, "descriptor" },
238 { QUAL_READ, "reads", lookup_desc, "descriptor" },
239 { QUAL_READ, "r", lookup_desc, "descriptor" },
240 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
241 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
242 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
243 { 0, NULL, NULL, NULL },
244};
245
246static int
247lookup_syscall(s)
248char *s;
249{
250 int i;
251
252 for (i = 0; i < nsyscalls; i++) {
253 if (strcmp(s, sysent[i].sys_name) == 0)
254 return i;
255 }
256 return -1;
257}
258
259static int
260lookup_signal(s)
261char *s;
262{
263 int i;
264 char buf[32];
265
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000266 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267 return atoi(s);
268 strcpy(buf, s);
269 s = buf;
270 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000271 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 if (strncmp(s, "SIG", 3) == 0)
273 s += 3;
274 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000275 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000276 return i;
277 }
278 return -1;
279}
280
281static int
282lookup_fault(s)
283char *s;
284{
285 return -1;
286}
287
288static int
289lookup_desc(s)
290char *s;
291{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000292 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 return atoi(s);
294 return -1;
295}
296
297static int
298lookup_class(s)
299char *s;
300{
301 if (strcmp(s, "file") == 0)
302 return TRACE_FILE;
303 if (strcmp(s, "ipc") == 0)
304 return TRACE_IPC;
305 if (strcmp(s, "network") == 0)
306 return TRACE_NETWORK;
307 if (strcmp(s, "process") == 0)
308 return TRACE_PROCESS;
309 if (strcmp(s, "signal") == 0)
310 return TRACE_SIGNAL;
311 return -1;
312}
313
314void
315qualify(s)
316char *s;
317{
318 struct qual_options *opt;
319 int not;
320 char *p;
321 int i, n;
322
323 opt = &qual_options[0];
324 for (i = 0; (p = qual_options[i].option_name); i++) {
325 n = strlen(p);
326 if (strncmp(s, p, n) == 0 && s[n] == '=') {
327 opt = &qual_options[i];
328 s += n + 1;
329 break;
330 }
331 }
332 not = 0;
333 if (*s == '!') {
334 not = 1;
335 s++;
336 }
337 if (strcmp(s, "none") == 0) {
338 not = 1 - not;
339 s = "all";
340 }
341 if (strcmp(s, "all") == 0) {
342 for (i = 0; i < MAX_QUALS; i++) {
343 if (not)
344 qual_flags[i] &= ~opt->bitflag;
345 else
346 qual_flags[i] |= opt->bitflag;
347 }
348 return;
349 }
350 for (i = 0; i < MAX_QUALS; i++) {
351 if (not)
352 qual_flags[i] |= opt->bitflag;
353 else
354 qual_flags[i] &= ~opt->bitflag;
355 }
356 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
357 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
358 for (i = 0; i < MAX_QUALS; i++) {
359 if (sysent[i].sys_flags & n) {
360 if (not)
361 qual_flags[i] &= ~opt->bitflag;
362 else
363 qual_flags[i] |= opt->bitflag;
364 }
365 }
366 continue;
367 }
368 if ((n = (*opt->lookup)(p)) < 0) {
369 fprintf(stderr, "strace: invalid %s `%s'\n",
370 opt->argument_name, p);
371 exit(1);
372 }
373 if (not)
374 qual_flags[n] &= ~opt->bitflag;
375 else
376 qual_flags[n] |= opt->bitflag;
377 }
378 return;
379}
380
381static void
382dumpio(tcp)
383struct tcb *tcp;
384{
385 if (syserror(tcp))
386 return;
387 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
388 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000389 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390 case SYS_read:
391#ifdef SYS_recv
392 case SYS_recv:
393#endif
394#ifdef SYS_recvfrom
395 case SYS_recvfrom:
396#endif
397 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
398 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
399 break;
400 case SYS_write:
401#ifdef SYS_send
402 case SYS_send:
403#endif
404#ifdef SYS_sendto
405 case SYS_sendto:
406#endif
407 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
409 break;
410 }
411}
412
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000413#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000414enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000415#else /* FREEBSD */
416enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
417
418struct subcall {
419 int call;
420 int nsubcalls;
421 int subcalls[5];
422};
423
424const struct subcall subcalls_table[] = {
425 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000426#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000427 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000428#else
429 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
430#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000431 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
432};
433#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000434
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000435#if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000436
437const int socket_map [] = {
438 /* SYS_SOCKET */ 97,
439 /* SYS_BIND */ 104,
440 /* SYS_CONNECT */ 98,
441 /* SYS_LISTEN */ 106,
442 /* SYS_ACCEPT */ 99,
443 /* SYS_GETSOCKNAME */ 150,
444 /* SYS_GETPEERNAME */ 141,
445 /* SYS_SOCKETPAIR */ 135,
446 /* SYS_SEND */ 101,
447 /* SYS_RECV */ 102,
448 /* SYS_SENDTO */ 133,
449 /* SYS_RECVFROM */ 125,
450 /* SYS_SHUTDOWN */ 134,
451 /* SYS_SETSOCKOPT */ 105,
452 /* SYS_GETSOCKOPT */ 118,
453 /* SYS_SENDMSG */ 114,
454 /* SYS_RECVMSG */ 113
455};
456
457void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000458sparc_socket_decode (tcp)
459struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000460{
461 volatile long addr;
462 volatile int i, n;
463
464 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
465 return;
466 }
467 tcp->scno = socket_map [tcp->u_arg [0]-1];
468 n = tcp->u_nargs = sysent [tcp->scno].nargs;
469 addr = tcp->u_arg [1];
470 for (i = 0; i < n; i++){
471 int arg;
472 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
473 arg = 0;
474 tcp->u_arg [i] = arg;
475 addr += sizeof (arg);
476 }
477}
478
479static void
480decode_subcall(tcp, subcall, nsubcalls, style)
481struct tcb *tcp;
482int subcall;
483int nsubcalls;
484enum subcall_style style;
485{
486 int i, addr, mask, arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000487
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488 switch (style) {
489 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000490 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
491 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000492 tcp->scno = subcall + tcp->u_arg[0];
493 if (sysent[tcp->scno].nargs != -1)
494 tcp->u_nargs = sysent[tcp->scno].nargs;
495 else
496 tcp->u_nargs--;
497 for (i = 0; i < tcp->u_nargs; i++)
498 tcp->u_arg[i] = tcp->u_arg[i + 1];
499 break;
500 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000501 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
502 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503 tcp->scno = subcall + tcp->u_arg[0];
504 addr = tcp->u_arg[1];
505 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
506 if (umove(tcp, addr, &arg) < 0)
507 arg = 0;
508 tcp->u_arg[i] = arg;
509 addr += sizeof(arg);
510 }
511 tcp->u_nargs = sysent[tcp->scno].nargs;
512 break;
513 case mask_style:
514 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515 for (i = 0; mask; i++)
516 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000517 if (i >= nsubcalls)
518 return;
519 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000520 tcp->scno = subcall + i;
521 if (sysent[tcp->scno].nargs != -1)
522 tcp->u_nargs = sysent[tcp->scno].nargs;
523 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000524 case door_style:
525 /*
526 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000527 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000528 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000529 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
530 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000531 tcp->scno = subcall + tcp->u_arg[5];
532 if (sysent[tcp->scno].nargs != -1)
533 tcp->u_nargs = sysent[tcp->scno].nargs;
534 else
535 tcp->u_nargs--;
536 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000537#ifdef FREEBSD
538 case table_style:
539 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
540 if (subcalls_table[i].call == tcp->scno) break;
541 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
542 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
543 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
544 for (i = 0; i < tcp->u_nargs; i++)
545 tcp->u_arg[i] = tcp->u_arg[i + 1];
546 }
547 break;
548#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 }
550}
551#endif
552
553struct tcb *tcp_last = NULL;
554
555static int
556internal_syscall(tcp)
557struct tcb *tcp;
558{
559 /*
560 * We must always trace a few critical system calls in order to
561 * correctly support following forks in the presence of tracing
562 * qualifiers.
563 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000564 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000565#ifdef SYS_fork
566 case SYS_fork:
567#endif
568#ifdef SYS_vfork
569 case SYS_vfork:
570#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571 internal_fork(tcp);
572 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000573#ifdef SYS_clone
574 case SYS_clone:
575 internal_clone(tcp);
576 break;
577#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578#ifdef SYS_execv
579 case SYS_execv:
580#endif
581#ifdef SYS_execve
582 case SYS_execve:
583#endif
584 internal_exec(tcp);
585 break;
586
587#ifdef SYS_wait
588 case SYS_wait:
589#endif
590#ifdef SYS_wait4
591 case SYS_wait4:
592#endif
593#ifdef SYS_waitpid
594 case SYS_waitpid:
595#endif
596#ifdef SYS_waitsys
597 case SYS_waitsys:
598#endif
599 internal_wait(tcp);
600 break;
601
602#ifdef SYS_exit
603 case SYS_exit:
604#endif
605 internal_exit(tcp);
606 break;
607 }
608 return 0;
609}
610
Wichert Akkermanc7926982000-04-10 22:22:31 +0000611
612#ifdef LINUX
613#if defined (I386)
614 static long eax;
615#elif defined (IA64)
616 long r8, r10, psr;
617 long ia32 = 0;
618#elif defined (POWERPC)
619 static long result,flags;
620#elif defined (M68K)
621 static int d0;
622#elif defined (ARM)
623 static int r0;
624#elif defined (ALPHA)
625 static long r0;
626 static long a3;
627#elif defined (SPARC)
628 static struct pt_regs regs;
629 static unsigned long trap;
630#elif defined(MIPS)
631 static long a3;
632 static long r2;
633#elif defined(S390)
634 static long gpr2;
635 static long pc;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000636#elif defined(HPPA)
637 static long r28;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000638#endif
639#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000640#ifdef FREEBSD
641 struct reg regs;
642#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000643
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000644int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000645get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646struct tcb *tcp;
647{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000648 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000649#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000651#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000652
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000653#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000654#if defined(S390)
655 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
656 return -1;
657 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
658 if (errno)
659 return -1;
660 scno&=0xFF;
661#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000662 if (upeek(pid, 4*PT_R0, &scno) < 0)
663 return -1;
664 if (!(tcp->flags & TCB_INSYSCALL)) {
665 /* Check if we return from execve. */
666 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
667 tcp->flags &= ~TCB_WAITEXECVE;
668 return 0;
669 }
670 }
671#elif defined (I386)
672 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
673 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000674#elif defined(IA64)
675#define IA64_PSR_IS ((long)1 << 34)
676 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
677 ia32 = (psr & IA64_PSR_IS);
678 if (!(tcp->flags & TCB_INSYSCALL)) {
679 if (ia32) {
680 if (upeek(pid, PT_R8, &scno) < 0)
681 return -1;
682 } else {
683 if (upeek (pid, PT_R15, &scno) < 0)
684 return -1;
685 }
686 } else {
687 /* syscall in progress */
688 if (upeek (pid, PT_R8, &r8) < 0)
689 return -1;
690 if (upeek (pid, PT_R10, &r10) < 0)
691 return -1;
692 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000693#elif defined (ARM)
694 {
695 long pc;
696 upeek(pid, 4*15, &pc);
697 umoven(tcp, pc-4, 4, (char *)&scno);
698 scno &= 0x000fffff;
699 }
700#elif defined (M68K)
701 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
702 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000703#elif defined (MIPS)
704 if (upeek(pid, REG_A3, &a3) < 0)
705 return -1;
706
707 if(!(tcp->flags & TCB_INSYSCALL)) {
708 if (upeek(pid, REG_V0, &scno) < 0)
709 return -1;
710
711 if (scno < 0 || scno > nsyscalls) {
712 if(a3 == 0 || a3 == -1) {
713 if(debug)
714 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
715 return 0;
716 }
717 }
718 } else {
719 if (upeek(pid, REG_V0, &r2) < 0)
720 return -1;
721 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000722#elif defined (ALPHA)
723 if (upeek(pid, REG_A3, &a3) < 0)
724 return -1;
725
726 if (!(tcp->flags & TCB_INSYSCALL)) {
727 if (upeek(pid, REG_R0, &scno) < 0)
728 return -1;
729
730 /* Check if we return from execve. */
731 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
732 tcp->flags &= ~TCB_WAITEXECVE;
733 return 0;
734 }
735
736 /*
737 * Do some sanity checks to figure out if it's
738 * really a syscall entry
739 */
740 if (scno < 0 || scno > nsyscalls) {
741 if (a3 == 0 || a3 == -1) {
742 if (debug)
743 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
744 return 0;
745 }
746 }
747 }
748 else {
749 if (upeek(pid, REG_R0, &r0) < 0)
750 return -1;
751 }
752#elif defined (SPARC)
753 /* Everything we need is in the current register set. */
754 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
755 return -1;
756
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000757 /* If we are entering, then disassemble the syscall trap. */
758 if (!(tcp->flags & TCB_INSYSCALL)) {
759 /* Retrieve the syscall trap instruction. */
760 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000761 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000762 if (errno)
763 return -1;
764
765 /* Disassemble the trap to see what personality to use. */
766 switch (trap) {
767 case 0x91d02010:
768 /* Linux/SPARC syscall trap. */
769 set_personality(0);
770 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000771 case 0x91d0206d:
772 /* Linux/SPARC64 syscall trap. */
773 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
774 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000775 case 0x91d02000:
776 /* SunOS syscall trap. (pers 1) */
777 fprintf(stderr,"syscall: SunOS no support\n");
778 return -1;
779 case 0x91d02008:
780 /* Solaris 2.x syscall trap. (per 2) */
781 set_personality(1);
782 break;
783 case 0x91d02009:
784 /* NetBSD/FreeBSD syscall trap. */
785 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
786 return -1;
787 case 0x91d02027:
788 /* Solaris 2.x gettimeofday */
789 set_personality(1);
790 break;
791 default:
792 /* Unknown syscall trap. */
793 if(tcp->flags & TCB_WAITEXECVE) {
794 tcp->flags &= ~TCB_WAITEXECVE;
795 return 0;
796 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000797 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000798 return -1;
799 }
800
801 /* Extract the system call number from the registers. */
802 if (trap == 0x91d02027)
803 scno = 156;
804 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000805 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000806 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000807 scno = regs.r_o0;
808 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000809 }
810 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000811#elif defined(HPPA)
812 if (upeek(pid, PT_GR20, &scno) < 0)
813 return -1;
814 if (!(tcp->flags & TCB_INSYSCALL)) {
815 /* Check if we return from execve. */
816 if ((tcp->flags & TCB_WAITEXECVE)) {
817 tcp->flags &= ~TCB_WAITEXECVE;
818 return 0;
819 }
820 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000821#endif
822#endif /* LINUX */
823#ifdef SUNOS4
824 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
825 return -1;
826#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000827#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000828#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +0000829 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000830#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000831#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000832 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000833#else /* FREEBSD */
834 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
835 perror("pread");
836 return -1;
837 }
838 switch (regs.r_eax) {
839 case SYS_syscall:
840 case SYS___syscall:
841 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
842 break;
843 default:
844 scno = regs.r_eax;
845 break;
846 }
847#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000848#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000849#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000850 if (!(tcp->flags & TCB_INSYSCALL))
851 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000852 return 1;
853}
854
Pavel Machek4dc3b142000-02-01 17:58:41 +0000855
856int
857syscall_fixup(tcp)
858struct tcb *tcp;
859{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000860#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +0000861 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000862#else /* USE_PROCFS */
863 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000864
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000865 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000866 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000867 if (
868 scno == SYS_fork
869#ifdef SYS_vfork
870 || scno == SYS_vfork
871#endif /* SYS_vfork */
872 ) {
873 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000874 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000875 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000876 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000877 }
878 else {
879 fprintf(stderr, "syscall: missing entry\n");
880 tcp->flags |= TCB_INSYSCALL;
881 }
882 }
883 }
884 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000885 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000886 fprintf(stderr, "syscall: missing exit\n");
887 tcp->flags &= ~TCB_INSYSCALL;
888 }
889 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000890#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000891#ifdef SUNOS4
892 if (!(tcp->flags & TCB_INSYSCALL)) {
893 if (scno == 0) {
894 fprintf(stderr, "syscall: missing entry\n");
895 tcp->flags |= TCB_INSYSCALL;
896 }
897 }
898 else {
899 if (scno != 0) {
900 if (debug) {
901 /*
902 * This happens when a signal handler
903 * for a signal which interrupted a
904 * a system call makes another system call.
905 */
906 fprintf(stderr, "syscall: missing exit\n");
907 }
908 tcp->flags &= ~TCB_INSYSCALL;
909 }
910 }
911#endif /* SUNOS4 */
912#ifdef LINUX
913#if defined (I386)
914 if (upeek(pid, 4*EAX, &eax) < 0)
915 return -1;
916 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
917 if (debug)
918 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
919 return 0;
920 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000921#elif defined (S390)
922 if (upeek(pid, PT_GPR2, &gpr2) < 0)
923 return -1;
924 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
925 if (debug)
926 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
927 return 0;
928 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000929#elif defined (POWERPC)
930# define SO_MASK 0x10000000
931 if (upeek(pid, 4*PT_CCR, &flags) < 0)
932 return -1;
933 if (upeek(pid, 4*PT_R3, &result) < 0)
934 return -1;
935 if (flags & SO_MASK)
936 result = -result;
937#elif defined (M68K)
938 if (upeek(pid, 4*PT_D0, &d0) < 0)
939 return -1;
940 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
941 if (debug)
942 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
943 return 0;
944 }
945#elif defined (ARM)
946 if (upeek(pid, 4*0, (long *)&r0) < 0)
947 return -1;
948 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
949 if (debug)
950 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
951 return 0;
952 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000953#elif defined (HPPA)
954 if (upeek(pid, PT_GR28, &r28) < 0)
955 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000956#endif
957#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000958 return 1;
959}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000960
Pavel Machek4dc3b142000-02-01 17:58:41 +0000961int
962get_error(tcp)
963struct tcb *tcp;
964{
965 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000966#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000967#ifdef S390
968 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
969 tcp->u_rval = -1;
970 u_error = -gpr2;
971 }
972 else {
973 tcp->u_rval = gpr2;
974 u_error = 0;
975 }
976#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000977#ifdef I386
978 if (eax < 0 && -eax < nerrnos) {
979 tcp->u_rval = -1;
980 u_error = -eax;
981 }
982 else {
983 tcp->u_rval = eax;
984 u_error = 0;
985 }
986#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000987#ifdef IA64
988 if (ia32) {
989 int err;
990
991 err = (int)r8;
992 if (err < 0 && -err < nerrnos) {
993 tcp->u_rval = -1;
994 u_error = -err;
995 }
996 else {
997 tcp->u_rval = err;
998 u_error = 0;
999 }
1000 } else {
1001 if (r10) {
1002 tcp->u_rval = -1;
1003 u_error = r8;
1004 } else {
1005 tcp->u_rval = r8;
1006 u_error = 0;
1007 }
1008 }
1009#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001010#ifdef MIPS
1011 if (a3) {
1012 tcp->u_rval = -1;
1013 u_error = r2;
1014 } else {
1015 tcp->u_rval = r2;
1016 u_error = 0;
1017 }
1018#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001019#ifdef POWERPC
1020 if (result && (unsigned) -result < nerrnos) {
1021 tcp->u_rval = -1;
1022 u_error = -result;
1023 }
1024 else {
1025 tcp->u_rval = result;
1026 u_error = 0;
1027 }
1028#else /* !POWERPC */
1029#ifdef M68K
1030 if (d0 && (unsigned) -d0 < nerrnos) {
1031 tcp->u_rval = -1;
1032 u_error = -d0;
1033 }
1034 else {
1035 tcp->u_rval = d0;
1036 u_error = 0;
1037 }
1038#else /* !M68K */
1039#ifdef ARM
1040 if (r0 && (unsigned) -r0 < nerrnos) {
1041 tcp->u_rval = -1;
1042 u_error = -r0;
1043 }
1044 else {
1045 tcp->u_rval = r0;
1046 u_error = 0;
1047 }
1048#else /* !ARM */
1049#ifdef ALPHA
1050 if (a3) {
1051 tcp->u_rval = -1;
1052 u_error = r0;
1053 }
1054 else {
1055 tcp->u_rval = r0;
1056 u_error = 0;
1057 }
1058#else /* !ALPHA */
1059#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001060 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001061 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001062 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001063 }
1064 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001065 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001066 u_error = 0;
1067 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001068#else /* !SPARC */
1069#ifdef HPPA
1070 if (r28 && (unsigned) -r28 < nerrnos) {
1071 tcp->u_rval = -1;
1072 u_error = -r28;
1073 }
1074 else {
1075 tcp->u_rval = r28;
1076 u_error = 0;
1077 }
1078#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001079#endif /* SPARC */
1080#endif /* ALPHA */
1081#endif /* ARM */
1082#endif /* M68K */
1083#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001084#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001085#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001086#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001087#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001088#endif /* LINUX */
1089#ifdef SUNOS4
1090 /* get error code from user struct */
1091 if (upeek(pid, uoff(u_error), &u_error) < 0)
1092 return -1;
1093 u_error >>= 24; /* u_error is a char */
1094
1095 /* get system call return value */
1096 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1097 return -1;
1098#endif /* SUNOS4 */
1099#ifdef SVR4
1100#ifdef SPARC
1101 /* Judicious guessing goes a long way. */
1102 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1103 tcp->u_rval = -1;
1104 u_error = tcp->status.pr_reg[R_O0];
1105 }
1106 else {
1107 tcp->u_rval = tcp->status.pr_reg[R_O0];
1108 u_error = 0;
1109 }
1110#endif /* SPARC */
1111#ifdef I386
1112 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001113 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001114 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001115 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001116 }
1117 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001118 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001119#ifdef HAVE_LONG_LONG
1120 tcp->u_lrval =
1121 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1122 tcp->status.PR_REG[EAX];
1123#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124 u_error = 0;
1125 }
1126#endif /* I386 */
1127#ifdef MIPS
1128 if (tcp->status.pr_reg[CTX_A3]) {
1129 tcp->u_rval = -1;
1130 u_error = tcp->status.pr_reg[CTX_V0];
1131 }
1132 else {
1133 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1134 u_error = 0;
1135 }
1136#endif /* MIPS */
1137#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001138#ifdef FREEBSD
1139 if (regs.r_eflags & PSL_C) {
1140 tcp->u_rval = -1;
1141 u_error = regs.r_eax;
1142 } else {
1143 tcp->u_rval = regs.r_eax;
1144 tcp->u_lrval =
1145 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1146 u_error = 0;
1147 }
1148#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001149 tcp->u_error = u_error;
1150 return 1;
1151}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001152
Pavel Machek4dc3b142000-02-01 17:58:41 +00001153int syscall_enter(tcp)
1154struct tcb *tcp;
1155{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001156#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001157 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001158#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001159#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001160#if defined(S390)
1161 {
1162 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001163 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1164 tcp->u_nargs = sysent[tcp->scno].nargs;
1165 else
1166 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001167 for (i = 0; i < tcp->u_nargs; i++) {
1168 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1169 return -1;
1170 }
1171 }
1172#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001173 {
1174 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001175 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1176 tcp->u_nargs = sysent[tcp->scno].nargs;
1177 else
1178 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001179 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001180 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1181 * for scno somewhere above here!
1182 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001183 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1184 return -1;
1185 }
1186 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001187#elif defined (IA64)
1188 {
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001189 unsigned long *bsp, cfm, i;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001190
1191 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1192 return -1;
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001193 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1194 return -1;
1195
1196 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001197
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001198 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1199 tcp->u_nargs = sysent[tcp->scno].nargs;
1200 else
1201 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001202 for (i = 0; i < tcp->u_nargs; ++i) {
1203 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1204 (char *) &tcp->u_arg[i])
1205 < 0)
1206 return -1;
1207 }
1208 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001209#elif defined (MIPS)
1210 {
1211 long sp;
1212 int i, nargs;
1213
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001214 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1215 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1216 else
1217 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001218 if(nargs > 4) {
1219 if(upeek(pid, REG_SP, &sp) < 0)
1220 return -1;
1221 for(i = 0; i < 4; i++) {
1222 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1223 return -1;
1224 }
1225 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1226 (char *)(tcp->u_arg + 4));
1227 } else {
1228 for(i = 0; i < nargs; i++) {
1229 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1230 return -1;
1231 }
1232 }
1233 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001234#elif defined (POWERPC)
1235 {
1236 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001237 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1238 tcp->u_nargs = sysent[tcp->scno].nargs;
1239 else
1240 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001241 for (i = 0; i < tcp->u_nargs; i++) {
1242 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1243 return -1;
1244 }
1245 }
1246#elif defined (SPARC)
1247 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001248 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001249
1250 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1251 tcp->u_nargs = sysent[tcp->scno].nargs;
1252 else
1253 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001254 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001255 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001256 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001257#elif defined (HPPA)
1258 {
1259 int i;
1260
1261 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1262 tcp->u_nargs = sysent[tcp->scno].nargs;
1263 else
1264 tcp->u_nargs = MAX_ARGS;
1265 for (i = 0; i < tcp->u_nargs; i++) {
1266 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1267 return -1;
1268 }
1269 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001270#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001271 {
1272 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001273 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1274 tcp->u_nargs = sysent[tcp->scno].nargs;
1275 else
1276 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001277 for (i = 0; i < tcp->u_nargs; i++) {
1278 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1279 return -1;
1280 }
1281 }
1282#endif
1283#endif /* LINUX */
1284#ifdef SUNOS4
1285 {
1286 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001287 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1288 tcp->u_nargs = sysent[tcp->scno].nargs;
1289 else
1290 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001291 for (i = 0; i < tcp->u_nargs; i++) {
1292 struct user *u;
1293
1294 if (upeek(pid, uoff(u_arg[0]) +
1295 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1296 return -1;
1297 }
1298 }
1299#endif /* SUNOS4 */
1300#ifdef SVR4
1301#ifdef MIPS
1302 /*
1303 * SGI is broken: even though it has pr_sysarg, it doesn't
1304 * set them on system call entry. Get a clue.
1305 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001306 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001307 tcp->u_nargs = sysent[tcp->scno].nargs;
1308 else
1309 tcp->u_nargs = tcp->status.pr_nsysarg;
1310 if (tcp->u_nargs > 4) {
1311 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1312 4*sizeof(tcp->u_arg[0]));
1313 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1314 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1315 }
1316 else {
1317 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1318 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1319 }
John Hughes25299712001-03-06 10:10:06 +00001320#elif UNIXWARE >= 2
1321 /*
1322 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1323 */
1324 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1325 tcp->u_nargs = sysent[tcp->scno].nargs;
1326 else
1327 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1328 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1329 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1330#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001331 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001332 tcp->u_nargs = sysent[tcp->scno].nargs;
1333 else
1334 tcp->u_nargs = tcp->status.pr_nsysarg;
1335 {
1336 int i;
1337 for (i = 0; i < tcp->u_nargs; i++)
1338 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1339 }
John Hughes25299712001-03-06 10:10:06 +00001340#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001341 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001342 tcp->u_nargs = sysent[tcp->scno].nargs;
1343 else
1344 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001345 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001346 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001347#else
1348 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001349#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001350#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001351#ifdef FREEBSD
1352 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1353 sysent[tcp->scno].nargs > tcp->status.val)
1354 tcp->u_nargs = sysent[tcp->scno].nargs;
1355 else
1356 tcp->u_nargs = tcp->status.val;
1357 if (tcp->u_nargs < 0)
1358 tcp->u_nargs = 0;
1359 if (tcp->u_nargs > MAX_ARGS)
1360 tcp->u_nargs = MAX_ARGS;
1361 switch(regs.r_eax) {
1362 case SYS___syscall:
1363 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1364 regs.r_esp + sizeof(int) + sizeof(quad_t));
1365 break;
1366 case SYS_syscall:
1367 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1368 regs.r_esp + 2 * sizeof(int));
1369 break;
1370 default:
1371 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1372 regs.r_esp + sizeof(int));
1373 break;
1374 }
1375#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001376 return 1;
1377}
1378
1379int
1380trace_syscall(tcp)
1381struct tcb *tcp;
1382{
1383 int sys_res;
1384 struct timeval tv;
1385 int res;
1386
1387 /* Measure the exit time as early as possible to avoid errors. */
1388 if (dtime && (tcp->flags & TCB_INSYSCALL))
1389 gettimeofday(&tv, NULL);
1390
1391 res = get_scno(tcp);
1392 if (res != 1)
1393 return res;
1394
1395 res = syscall_fixup(tcp);
1396 if (res != 1)
1397 return res;
1398
1399 if (tcp->flags & TCB_INSYSCALL) {
1400 long u_error;
1401 res = get_error(tcp);
1402 if (res != 1)
1403 return res;
1404 u_error = tcp->u_error;
1405
1406
1407 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001408 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1409 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001410 tcp->flags &= ~TCB_INSYSCALL;
1411 return 0;
1412 }
1413
1414 if (tcp->flags & TCB_REPRINT) {
1415 printleader(tcp);
1416 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001417 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001418 tprintf("syscall_%lu", tcp->scno);
1419 else
1420 tprintf("%s", sysent[tcp->scno].sys_name);
1421 tprintf(" resumed> ");
1422 }
1423
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001424 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001425 call_count[tcp->scno]++;
1426 if (tcp->u_error)
1427 error_count[tcp->scno]++;
1428 tv_sub(&tv, &tv, &tcp->etime);
1429#ifdef LINUX
1430 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1431 static struct timeval one_tick =
1432 { 0, 1000000 / HZ };
1433
1434 if (tv_nz(&tcp->dtime))
1435 tv = tcp->dtime;
1436 else if (tv_cmp(&tv, &one_tick) > 0) {
1437 if (tv_cmp(&shortest, &one_tick) < 0)
1438 tv = shortest;
1439 else
1440 tv = one_tick;
1441 }
1442 }
1443#endif /* LINUX */
1444 if (tv_cmp(&tv, &shortest) < 0)
1445 shortest = tv;
1446 tv_add(&tv_count[tcp->scno],
1447 &tv_count[tcp->scno], &tv);
1448 tcp->flags &= ~TCB_INSYSCALL;
1449 return 0;
1450 }
1451
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001452 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001453 || (qual_flags[tcp->scno] & QUAL_RAW))
1454 sys_res = printargs(tcp);
1455 else
1456 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1457 u_error = tcp->u_error;
1458 tprintf(") ");
1459 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001460 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1461 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001462 if (u_error)
1463 tprintf("= -1 (errno %ld)", u_error);
1464 else
1465 tprintf("= %#lx", tcp->u_rval);
1466 }
1467 else if (!(sys_res & RVAL_NONE) && u_error) {
1468 switch (u_error) {
1469#ifdef LINUX
1470 case ERESTARTSYS:
1471 tprintf("= ? ERESTARTSYS (To be restarted)");
1472 break;
1473 case ERESTARTNOINTR:
1474 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1475 break;
1476 case ERESTARTNOHAND:
1477 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1478 break;
1479#endif /* LINUX */
1480 default:
1481 tprintf("= -1 ");
1482 if (u_error < nerrnos && u_error < sys_nerr)
1483 tprintf("%s (%s)", errnoent[u_error],
1484 sys_errlist[u_error]);
1485 else if (u_error < nerrnos)
1486 tprintf("%s (errno %ld)",
1487 errnoent[u_error], u_error);
1488 else if (u_error < sys_nerr)
1489 tprintf("ERRNO_%ld (%s)", u_error,
1490 sys_errlist[u_error]);
1491 else
1492 tprintf("E??? (errno %ld)", u_error);
1493 break;
1494 }
1495 }
1496 else {
1497 if (sys_res & RVAL_NONE)
1498 tprintf("= ?");
1499 else {
1500 switch (sys_res & RVAL_MASK) {
1501 case RVAL_HEX:
1502 tprintf("= %#lx", tcp->u_rval);
1503 break;
1504 case RVAL_OCTAL:
1505 tprintf("= %#lo", tcp->u_rval);
1506 break;
1507 case RVAL_UDECIMAL:
1508 tprintf("= %lu", tcp->u_rval);
1509 break;
1510 case RVAL_DECIMAL:
1511 tprintf("= %ld", tcp->u_rval);
1512 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001513#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001514 case RVAL_LHEX:
1515 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001516 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001517 case RVAL_LOCTAL:
1518 tprintf("= %#llo", tcp->u_lrval);
1519 break;
1520 case RVAL_LUDECIMAL:
1521 tprintf("= %llu", tcp->u_lrval);
1522 break;
1523 case RVAL_LDECIMAL:
1524 tprintf("= %lld", tcp->u_lrval);
1525 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001526#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001527 default:
1528 fprintf(stderr,
1529 "invalid rval format\n");
1530 break;
1531 }
1532 }
1533 if ((sys_res & RVAL_STR) && tcp->auxstr)
1534 tprintf(" (%s)", tcp->auxstr);
1535 }
1536 if (dtime) {
1537 tv_sub(&tv, &tv, &tcp->etime);
1538 tprintf(" <%ld.%06ld>",
1539 (long) tv.tv_sec, (long) tv.tv_usec);
1540 }
1541 printtrailer(tcp);
1542
1543 dumpio(tcp);
1544 if (fflush(tcp->outf) == EOF)
1545 return -1;
1546 tcp->flags &= ~TCB_INSYSCALL;
1547 return 0;
1548 }
1549
1550 /* Entering system call */
1551 res = syscall_enter(tcp);
1552 if (res != 1)
1553 return res;
1554
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001555 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001556#ifdef LINUX
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001557#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001558 case SYS_socketcall:
1559 decode_subcall(tcp, SYS_socket_subcall,
1560 SYS_socket_nsubcalls, deref_style);
1561 break;
1562 case SYS_ipc:
1563 decode_subcall(tcp, SYS_ipc_subcall,
1564 SYS_ipc_nsubcalls, shift_style);
1565 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001566#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001567#ifdef SPARC
1568 case SYS_socketcall:
1569 sparc_socket_decode (tcp);
1570 break;
1571#endif
1572#endif /* LINUX */
1573#ifdef SVR4
1574#ifdef SYS_pgrpsys_subcall
1575 case SYS_pgrpsys:
1576 decode_subcall(tcp, SYS_pgrpsys_subcall,
1577 SYS_pgrpsys_nsubcalls, shift_style);
1578 break;
1579#endif /* SYS_pgrpsys_subcall */
1580#ifdef SYS_sigcall_subcall
1581 case SYS_sigcall:
1582 decode_subcall(tcp, SYS_sigcall_subcall,
1583 SYS_sigcall_nsubcalls, mask_style);
1584 break;
1585#endif /* SYS_sigcall_subcall */
1586 case SYS_msgsys:
1587 decode_subcall(tcp, SYS_msgsys_subcall,
1588 SYS_msgsys_nsubcalls, shift_style);
1589 break;
1590 case SYS_shmsys:
1591 decode_subcall(tcp, SYS_shmsys_subcall,
1592 SYS_shmsys_nsubcalls, shift_style);
1593 break;
1594 case SYS_semsys:
1595 decode_subcall(tcp, SYS_semsys_subcall,
1596 SYS_semsys_nsubcalls, shift_style);
1597 break;
1598#if 0 /* broken */
1599 case SYS_utssys:
1600 decode_subcall(tcp, SYS_utssys_subcall,
1601 SYS_utssys_nsubcalls, shift_style);
1602 break;
1603#endif
1604 case SYS_sysfs:
1605 decode_subcall(tcp, SYS_sysfs_subcall,
1606 SYS_sysfs_nsubcalls, shift_style);
1607 break;
1608 case SYS_spcall:
1609 decode_subcall(tcp, SYS_spcall_subcall,
1610 SYS_spcall_nsubcalls, shift_style);
1611 break;
1612#ifdef SYS_context_subcall
1613 case SYS_context:
1614 decode_subcall(tcp, SYS_context_subcall,
1615 SYS_context_nsubcalls, shift_style);
1616 break;
1617#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001618#ifdef SYS_door_subcall
1619 case SYS_door:
1620 decode_subcall(tcp, SYS_door_subcall,
1621 SYS_door_nsubcalls, door_style);
1622 break;
1623#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00001624#ifdef SYS_kaio_subcall
1625 case SYS_kaio:
1626 decode_subcall(tcp, SYS_kaio_subcall,
1627 SYS_kaio_nsubcalls, shift_style);
1628 break;
1629#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001630#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001631#ifdef FREEBSD
1632 case SYS_msgsys:
1633 case SYS_shmsys:
1634 case SYS_semsys:
1635 decode_subcall(tcp, 0, 0, table_style);
1636 break;
1637#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001638#ifdef SUNOS4
1639 case SYS_semsys:
1640 decode_subcall(tcp, SYS_semsys_subcall,
1641 SYS_semsys_nsubcalls, shift_style);
1642 break;
1643 case SYS_msgsys:
1644 decode_subcall(tcp, SYS_msgsys_subcall,
1645 SYS_msgsys_nsubcalls, shift_style);
1646 break;
1647 case SYS_shmsys:
1648 decode_subcall(tcp, SYS_shmsys_subcall,
1649 SYS_shmsys_nsubcalls, shift_style);
1650 break;
1651#endif
1652 }
1653
1654 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001655 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001656 tcp->flags |= TCB_INSYSCALL;
1657 return 0;
1658 }
1659
1660 if (cflag) {
1661 gettimeofday(&tcp->etime, NULL);
1662 tcp->flags |= TCB_INSYSCALL;
1663 return 0;
1664 }
1665
1666 printleader(tcp);
1667 tcp->flags &= ~TCB_REPRINT;
1668 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001669 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001670 tprintf("syscall_%lu(", tcp->scno);
1671 else
1672 tprintf("%s(", sysent[tcp->scno].sys_name);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001673 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001674 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1675 sys_res = printargs(tcp);
1676 else
1677 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1678 if (fflush(tcp->outf) == EOF)
1679 return -1;
1680 tcp->flags |= TCB_INSYSCALL;
1681 /* Measure the entrance time as late as possible to avoid errors. */
1682 if (dtime)
1683 gettimeofday(&tcp->etime, NULL);
1684 return sys_res;
1685}
1686
1687int
1688printargs(tcp)
1689struct tcb *tcp;
1690{
1691 if (entering(tcp)) {
1692 int i;
1693
1694 for (i = 0; i < tcp->u_nargs; i++)
1695 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1696 }
1697 return 0;
1698}
1699
1700long
1701getrval2(tcp)
1702struct tcb *tcp;
1703{
1704 long val = -1;
1705
1706#ifdef LINUX
1707#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001708 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001709 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1710 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001711 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001712#endif /* SPARC */
1713#endif /* LINUX */
1714
1715#ifdef SUNOS4
1716 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1717 return -1;
1718#endif /* SUNOS4 */
1719
1720#ifdef SVR4
1721#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001722 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001723#endif /* SPARC */
1724#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001725 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001726#endif /* I386 */
1727#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001728 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001729#endif /* MIPS */
1730#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001731#ifdef FREEBSD
1732 struct reg regs;
1733 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1734 val = regs.r_edx;
1735#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001736 return val;
1737}
1738
1739/*
1740 * Apparently, indirect system calls have already be converted by ptrace(2),
1741 * so if you see "indir" this program has gone astray.
1742 */
1743int
1744sys_indir(tcp)
1745struct tcb *tcp;
1746{
1747 int i, scno, nargs;
1748
1749 if (entering(tcp)) {
1750 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1751 fprintf(stderr, "Bogus syscall: %u\n", scno);
1752 return 0;
1753 }
1754 nargs = sysent[scno].nargs;
1755 tprintf("%s", sysent[scno].sys_name);
1756 for (i = 0; i < nargs; i++)
1757 tprintf(", %#lx", tcp->u_arg[i+1]);
1758 }
1759 return 0;
1760}
1761
1762static int
1763time_cmp(a, b)
1764void *a;
1765void *b;
1766{
1767 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1768}
1769
1770static int
1771syscall_cmp(a, b)
1772void *a;
1773void *b;
1774{
1775 return strcmp(sysent[*((int *) a)].sys_name,
1776 sysent[*((int *) b)].sys_name);
1777}
1778
1779static int
1780count_cmp(a, b)
1781void *a;
1782void *b;
1783{
1784 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1785
1786 return (m < n) ? 1 : (m > n) ? -1 : 0;
1787}
1788
1789static int (*sortfun)();
1790static struct timeval overhead = { -1, -1 };
1791
1792void
1793set_sortby(sortby)
1794char *sortby;
1795{
1796 if (strcmp(sortby, "time") == 0)
1797 sortfun = time_cmp;
1798 else if (strcmp(sortby, "calls") == 0)
1799 sortfun = count_cmp;
1800 else if (strcmp(sortby, "name") == 0)
1801 sortfun = syscall_cmp;
1802 else if (strcmp(sortby, "nothing") == 0)
1803 sortfun = NULL;
1804 else {
1805 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1806 exit(1);
1807 }
1808}
1809
1810void set_overhead(n)
1811int n;
1812{
1813 overhead.tv_sec = n / 1000000;
1814 overhead.tv_usec = n % 1000000;
1815}
1816
1817void
1818call_summary(outf)
1819FILE *outf;
1820{
1821 int i, j;
1822 int call_cum, error_cum;
1823 struct timeval tv_cum, dtv;
1824 double percent;
1825 char *dashes = "-------------------------";
1826 char error_str[16];
1827
1828 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1829 if (overhead.tv_sec == -1) {
1830 tv_mul(&overhead, &shortest, 8);
1831 tv_div(&overhead, &overhead, 10);
1832 }
1833 for (i = 0; i < nsyscalls; i++) {
1834 sorted_count[i] = i;
1835 if (call_count[i] == 0)
1836 continue;
1837 tv_mul(&dtv, &overhead, call_count[i]);
1838 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1839 call_cum += call_count[i];
1840 error_cum += error_count[i];
1841 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1842 }
1843 if (sortfun)
1844 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1845 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1846 "% time", "seconds", "usecs/call",
1847 "calls", "errors", "syscall");
1848 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1849 dashes, dashes, dashes, dashes, dashes, dashes);
1850 for (i = 0; i < nsyscalls; i++) {
1851 j = sorted_count[i];
1852 if (call_count[j] == 0)
1853 continue;
1854 tv_div(&dtv, &tv_count[j], call_count[j]);
1855 if (error_count[j])
1856 sprintf(error_str, "%d", error_count[j]);
1857 else
1858 error_str[0] = '\0';
1859 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1860 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1861 percent, (long) tv_count[j].tv_sec,
1862 (long) tv_count[j].tv_usec,
1863 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1864 call_count[j], error_str, sysent[j].sys_name);
1865 }
1866 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1867 dashes, dashes, dashes, dashes, dashes, dashes);
1868 if (error_cum)
1869 sprintf(error_str, "%d", error_cum);
1870 else
1871 error_str[0] = '\0';
1872 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1873 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1874 call_cum, error_str, "total");
1875}