blob: 39cc026020bc7730d8cc7a25146ea94303440b8e [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
55# undef fpu
56#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
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000074#ifndef SYS_ERRLIST_DECLARED
75extern int sys_nerr;
76extern char *sys_errlist[];
77#endif /* SYS_ERRLIST_DECLARED */
78
Pavel Machekd8ae7e32000-02-01 17:17:25 +000079#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000080#ifdef LINUX
81#ifndef ERESTARTSYS
82#define ERESTARTSYS 512
83#endif
84#ifndef ERESTARTNOINTR
85#define ERESTARTNOINTR 513
86#endif
87#ifndef ERESTARTNOHAND
88#define ERESTARTNOHAND 514 /* restart if no handler.. */
89#endif
90#ifndef ENOIOCTLCMD
91#define ENOIOCTLCMD 515 /* No ioctl command */
92#endif
93#ifndef NSIG
94#define NSIG 32
95#endif
96#ifdef ARM
97#undef NSIG
98#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000099#undef NR_SYSCALL_BASE
100#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#endif
102#endif /* LINUX */
103
104#include "syscall.h"
105
106/* Define these shorthand notations to simplify the syscallent files. */
107#define TF TRACE_FILE
108#define TI TRACE_IPC
109#define TN TRACE_NETWORK
110#define TP TRACE_PROCESS
111#define TS TRACE_SIGNAL
112
113struct sysent sysent0[] = {
114#include "syscallent.h"
115};
116int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
117
118#if SUPPORTED_PERSONALITIES >= 2
119struct sysent sysent1[] = {
120#include "syscallent1.h"
121};
122int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
123#endif /* SUPPORTED_PERSONALITIES >= 2 */
124
125#if SUPPORTED_PERSONALITIES >= 3
126struct sysent sysent2[] = {
127#include "syscallent2.h"
128};
129int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
130#endif /* SUPPORTED_PERSONALITIES >= 3 */
131
132struct sysent *sysent;
133int nsyscalls;
134
135/* Now undef them since short defines cause wicked namespace pollution. */
136#undef TF
137#undef TI
138#undef TN
139#undef TP
140#undef TS
141
142char *errnoent0[] = {
143#include "errnoent.h"
144};
145int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
146
147#if SUPPORTED_PERSONALITIES >= 2
148char *errnoent1[] = {
149#include "errnoent1.h"
150};
151int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
152#endif /* SUPPORTED_PERSONALITIES >= 2 */
153
154#if SUPPORTED_PERSONALITIES >= 3
155char *errnoent2[] = {
156#include "errnoent2.h"
157};
158int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
159#endif /* SUPPORTED_PERSONALITIES >= 3 */
160
161char **errnoent;
162int nerrnos;
163
164int current_personality;
165
166int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000167set_personality(personality)
168int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169{
170 switch (personality) {
171 case 0:
172 errnoent = errnoent0;
173 nerrnos = nerrnos0;
174 sysent = sysent0;
175 nsyscalls = nsyscalls0;
176 ioctlent = ioctlent0;
177 nioctlents = nioctlents0;
178 signalent = signalent0;
179 nsignals = nsignals0;
180 break;
181
182#if SUPPORTED_PERSONALITIES >= 2
183 case 1:
184 errnoent = errnoent1;
185 nerrnos = nerrnos1;
186 sysent = sysent1;
187 nsyscalls = nsyscalls1;
188 ioctlent = ioctlent1;
189 nioctlents = nioctlents1;
190 signalent = signalent1;
191 nsignals = nsignals1;
192 break;
193#endif /* SUPPORTED_PERSONALITIES >= 2 */
194
195#if SUPPORTED_PERSONALITIES >= 3
196 case 2:
197 errnoent = errnoent2;
198 nerrnos = nerrnos2;
199 sysent = sysent2;
200 nsyscalls = nsyscalls2;
201 ioctlent = ioctlent2;
202 nioctlents = nioctlents2;
203 signalent = signalent2;
204 nsignals = nsignals2;
205 break;
206#endif /* SUPPORTED_PERSONALITIES >= 3 */
207
208 default:
209 return -1;
210 }
211
212 current_personality = personality;
213 return 0;
214}
215
216int qual_flags[MAX_QUALS];
217
218static int call_count[MAX_QUALS];
219static int error_count[MAX_QUALS];
220static struct timeval tv_count[MAX_QUALS];
221static int sorted_count[MAX_QUALS];
222
223static struct timeval shortest = { 1000000, 0 };
224
225static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
226
227static struct qual_options {
228 int bitflag;
229 char *option_name;
230 int (*lookup)();
231 char *argument_name;
232} qual_options[] = {
233 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
234 { QUAL_TRACE, "t", lookup_syscall, "system call" },
235 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
236 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
237 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
238 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
239 { QUAL_RAW, "raw", lookup_syscall, "system call" },
240 { QUAL_RAW, "x", lookup_syscall, "system call" },
241 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
242 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
243 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
244 { QUAL_FAULT, "fault", lookup_fault, "fault" },
245 { QUAL_FAULT, "faults", lookup_fault, "fault" },
246 { QUAL_FAULT, "m", lookup_fault, "fault" },
247 { QUAL_READ, "read", lookup_desc, "descriptor" },
248 { QUAL_READ, "reads", lookup_desc, "descriptor" },
249 { QUAL_READ, "r", lookup_desc, "descriptor" },
250 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
251 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
252 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
253 { 0, NULL, NULL, NULL },
254};
255
256static int
257lookup_syscall(s)
258char *s;
259{
260 int i;
261
262 for (i = 0; i < nsyscalls; i++) {
263 if (strcmp(s, sysent[i].sys_name) == 0)
264 return i;
265 }
266 return -1;
267}
268
269static int
270lookup_signal(s)
271char *s;
272{
273 int i;
274 char buf[32];
275
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000276 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 return atoi(s);
278 strcpy(buf, s);
279 s = buf;
280 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000281 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000282 if (strncmp(s, "SIG", 3) == 0)
283 s += 3;
284 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000285 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286 return i;
287 }
288 return -1;
289}
290
291static int
292lookup_fault(s)
293char *s;
294{
295 return -1;
296}
297
298static int
299lookup_desc(s)
300char *s;
301{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000302 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303 return atoi(s);
304 return -1;
305}
306
307static int
308lookup_class(s)
309char *s;
310{
311 if (strcmp(s, "file") == 0)
312 return TRACE_FILE;
313 if (strcmp(s, "ipc") == 0)
314 return TRACE_IPC;
315 if (strcmp(s, "network") == 0)
316 return TRACE_NETWORK;
317 if (strcmp(s, "process") == 0)
318 return TRACE_PROCESS;
319 if (strcmp(s, "signal") == 0)
320 return TRACE_SIGNAL;
321 return -1;
322}
323
324void
325qualify(s)
326char *s;
327{
328 struct qual_options *opt;
329 int not;
330 char *p;
331 int i, n;
332
333 opt = &qual_options[0];
334 for (i = 0; (p = qual_options[i].option_name); i++) {
335 n = strlen(p);
336 if (strncmp(s, p, n) == 0 && s[n] == '=') {
337 opt = &qual_options[i];
338 s += n + 1;
339 break;
340 }
341 }
342 not = 0;
343 if (*s == '!') {
344 not = 1;
345 s++;
346 }
347 if (strcmp(s, "none") == 0) {
348 not = 1 - not;
349 s = "all";
350 }
351 if (strcmp(s, "all") == 0) {
352 for (i = 0; i < MAX_QUALS; i++) {
353 if (not)
354 qual_flags[i] &= ~opt->bitflag;
355 else
356 qual_flags[i] |= opt->bitflag;
357 }
358 return;
359 }
360 for (i = 0; i < MAX_QUALS; i++) {
361 if (not)
362 qual_flags[i] |= opt->bitflag;
363 else
364 qual_flags[i] &= ~opt->bitflag;
365 }
366 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
367 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
368 for (i = 0; i < MAX_QUALS; i++) {
369 if (sysent[i].sys_flags & n) {
370 if (not)
371 qual_flags[i] &= ~opt->bitflag;
372 else
373 qual_flags[i] |= opt->bitflag;
374 }
375 }
376 continue;
377 }
378 if ((n = (*opt->lookup)(p)) < 0) {
379 fprintf(stderr, "strace: invalid %s `%s'\n",
380 opt->argument_name, p);
381 exit(1);
382 }
383 if (not)
384 qual_flags[n] &= ~opt->bitflag;
385 else
386 qual_flags[n] |= opt->bitflag;
387 }
388 return;
389}
390
391static void
392dumpio(tcp)
393struct tcb *tcp;
394{
395 if (syserror(tcp))
396 return;
397 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
398 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000399 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000400 case SYS_read:
401#ifdef SYS_recv
402 case SYS_recv:
403#endif
404#ifdef SYS_recvfrom
405 case SYS_recvfrom:
406#endif
407 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
409 break;
410 case SYS_write:
411#ifdef SYS_send
412 case SYS_send:
413#endif
414#ifdef SYS_sendto
415 case SYS_sendto:
416#endif
417 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
418 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
419 break;
420 }
421}
422
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000423#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000424enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000425#else /* FREEBSD */
426enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
427
428struct subcall {
429 int call;
430 int nsubcalls;
431 int subcalls[5];
432};
433
434const struct subcall subcalls_table[] = {
435 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000436#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000437 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000438#else
439 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
440#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000441 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
442};
443#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000444
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000445#if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000446
447const int socket_map [] = {
448 /* SYS_SOCKET */ 97,
449 /* SYS_BIND */ 104,
450 /* SYS_CONNECT */ 98,
451 /* SYS_LISTEN */ 106,
452 /* SYS_ACCEPT */ 99,
453 /* SYS_GETSOCKNAME */ 150,
454 /* SYS_GETPEERNAME */ 141,
455 /* SYS_SOCKETPAIR */ 135,
456 /* SYS_SEND */ 101,
457 /* SYS_RECV */ 102,
458 /* SYS_SENDTO */ 133,
459 /* SYS_RECVFROM */ 125,
460 /* SYS_SHUTDOWN */ 134,
461 /* SYS_SETSOCKOPT */ 105,
462 /* SYS_GETSOCKOPT */ 118,
463 /* SYS_SENDMSG */ 114,
464 /* SYS_RECVMSG */ 113
465};
466
467void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000468sparc_socket_decode (tcp)
469struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000470{
471 volatile long addr;
472 volatile int i, n;
473
474 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
475 return;
476 }
477 tcp->scno = socket_map [tcp->u_arg [0]-1];
478 n = tcp->u_nargs = sysent [tcp->scno].nargs;
479 addr = tcp->u_arg [1];
480 for (i = 0; i < n; i++){
481 int arg;
482 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
483 arg = 0;
484 tcp->u_arg [i] = arg;
485 addr += sizeof (arg);
486 }
487}
488
489static void
490decode_subcall(tcp, subcall, nsubcalls, style)
491struct tcb *tcp;
492int subcall;
493int nsubcalls;
494enum subcall_style style;
495{
496 int i, addr, mask, arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000497
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000498 switch (style) {
499 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000500 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
501 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502 tcp->scno = subcall + tcp->u_arg[0];
503 if (sysent[tcp->scno].nargs != -1)
504 tcp->u_nargs = sysent[tcp->scno].nargs;
505 else
506 tcp->u_nargs--;
507 for (i = 0; i < tcp->u_nargs; i++)
508 tcp->u_arg[i] = tcp->u_arg[i + 1];
509 break;
510 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000511 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
512 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000513 tcp->scno = subcall + tcp->u_arg[0];
514 addr = tcp->u_arg[1];
515 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
516 if (umove(tcp, addr, &arg) < 0)
517 arg = 0;
518 tcp->u_arg[i] = arg;
519 addr += sizeof(arg);
520 }
521 tcp->u_nargs = sysent[tcp->scno].nargs;
522 break;
523 case mask_style:
524 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000525 for (i = 0; mask; i++)
526 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000527 if (i >= nsubcalls)
528 return;
529 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000530 tcp->scno = subcall + i;
531 if (sysent[tcp->scno].nargs != -1)
532 tcp->u_nargs = sysent[tcp->scno].nargs;
533 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000534 case door_style:
535 /*
536 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000537 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000538 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000539 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
540 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000541 tcp->scno = subcall + tcp->u_arg[5];
542 if (sysent[tcp->scno].nargs != -1)
543 tcp->u_nargs = sysent[tcp->scno].nargs;
544 else
545 tcp->u_nargs--;
546 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000547#ifdef FREEBSD
548 case table_style:
549 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
550 if (subcalls_table[i].call == tcp->scno) break;
551 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
552 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
553 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
554 for (i = 0; i < tcp->u_nargs; i++)
555 tcp->u_arg[i] = tcp->u_arg[i + 1];
556 }
557 break;
558#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000559 }
560}
561#endif
562
563struct tcb *tcp_last = NULL;
564
565static int
566internal_syscall(tcp)
567struct tcb *tcp;
568{
569 /*
570 * We must always trace a few critical system calls in order to
571 * correctly support following forks in the presence of tracing
572 * qualifiers.
573 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000574 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000575#ifdef SYS_fork
576 case SYS_fork:
577#endif
578#ifdef SYS_vfork
579 case SYS_vfork:
580#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000581 internal_fork(tcp);
582 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000583#ifdef SYS_clone
584 case SYS_clone:
585 internal_clone(tcp);
586 break;
587#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588#ifdef SYS_execv
589 case SYS_execv:
590#endif
591#ifdef SYS_execve
592 case SYS_execve:
593#endif
594 internal_exec(tcp);
595 break;
596
597#ifdef SYS_wait
598 case SYS_wait:
599#endif
600#ifdef SYS_wait4
601 case SYS_wait4:
602#endif
603#ifdef SYS_waitpid
604 case SYS_waitpid:
605#endif
606#ifdef SYS_waitsys
607 case SYS_waitsys:
608#endif
609 internal_wait(tcp);
610 break;
611
612#ifdef SYS_exit
613 case SYS_exit:
614#endif
615 internal_exit(tcp);
616 break;
617 }
618 return 0;
619}
620
Wichert Akkermanc7926982000-04-10 22:22:31 +0000621
622#ifdef LINUX
623#if defined (I386)
624 static long eax;
625#elif defined (IA64)
626 long r8, r10, psr;
627 long ia32 = 0;
628#elif defined (POWERPC)
629 static long result,flags;
630#elif defined (M68K)
631 static int d0;
632#elif defined (ARM)
633 static int r0;
634#elif defined (ALPHA)
635 static long r0;
636 static long a3;
637#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000638 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000639 static unsigned long trap;
640#elif defined(MIPS)
641 static long a3;
642 static long r2;
643#elif defined(S390)
644 static long gpr2;
645 static long pc;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000646#elif defined(HPPA)
647 static long r28;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000648#endif
649#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000650#ifdef FREEBSD
651 struct reg regs;
652#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000653
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000654int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000655get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000656struct tcb *tcp;
657{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000658 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000659#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000660 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000661#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000662
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000663#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000664#if defined(S390)
665 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
666 return -1;
667 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
668 if (errno)
669 return -1;
670 scno&=0xFF;
671#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000672 if (upeek(pid, 4*PT_R0, &scno) < 0)
673 return -1;
674 if (!(tcp->flags & TCB_INSYSCALL)) {
675 /* Check if we return from execve. */
676 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
677 tcp->flags &= ~TCB_WAITEXECVE;
678 return 0;
679 }
680 }
681#elif defined (I386)
682 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
683 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000684#elif defined(IA64)
685#define IA64_PSR_IS ((long)1 << 34)
686 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
687 ia32 = (psr & IA64_PSR_IS);
688 if (!(tcp->flags & TCB_INSYSCALL)) {
689 if (ia32) {
690 if (upeek(pid, PT_R8, &scno) < 0)
691 return -1;
692 } else {
693 if (upeek (pid, PT_R15, &scno) < 0)
694 return -1;
695 }
696 } else {
697 /* syscall in progress */
698 if (upeek (pid, PT_R8, &r8) < 0)
699 return -1;
700 if (upeek (pid, PT_R10, &r10) < 0)
701 return -1;
702 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000703#elif defined (ARM)
704 {
705 long pc;
706 upeek(pid, 4*15, &pc);
707 umoven(tcp, pc-4, 4, (char *)&scno);
708 scno &= 0x000fffff;
709 }
710#elif defined (M68K)
711 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
712 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000713#elif defined (MIPS)
714 if (upeek(pid, REG_A3, &a3) < 0)
715 return -1;
716
717 if(!(tcp->flags & TCB_INSYSCALL)) {
718 if (upeek(pid, REG_V0, &scno) < 0)
719 return -1;
720
721 if (scno < 0 || scno > nsyscalls) {
722 if(a3 == 0 || a3 == -1) {
723 if(debug)
724 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
725 return 0;
726 }
727 }
728 } else {
729 if (upeek(pid, REG_V0, &r2) < 0)
730 return -1;
731 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000732#elif defined (ALPHA)
733 if (upeek(pid, REG_A3, &a3) < 0)
734 return -1;
735
736 if (!(tcp->flags & TCB_INSYSCALL)) {
737 if (upeek(pid, REG_R0, &scno) < 0)
738 return -1;
739
740 /* Check if we return from execve. */
741 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
742 tcp->flags &= ~TCB_WAITEXECVE;
743 return 0;
744 }
745
746 /*
747 * Do some sanity checks to figure out if it's
748 * really a syscall entry
749 */
750 if (scno < 0 || scno > nsyscalls) {
751 if (a3 == 0 || a3 == -1) {
752 if (debug)
753 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
754 return 0;
755 }
756 }
757 }
758 else {
759 if (upeek(pid, REG_R0, &r0) < 0)
760 return -1;
761 }
762#elif defined (SPARC)
763 /* Everything we need is in the current register set. */
764 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
765 return -1;
766
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000767 /* If we are entering, then disassemble the syscall trap. */
768 if (!(tcp->flags & TCB_INSYSCALL)) {
769 /* Retrieve the syscall trap instruction. */
770 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000771 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000772 if (errno)
773 return -1;
774
775 /* Disassemble the trap to see what personality to use. */
776 switch (trap) {
777 case 0x91d02010:
778 /* Linux/SPARC syscall trap. */
779 set_personality(0);
780 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000781 case 0x91d0206d:
782 /* Linux/SPARC64 syscall trap. */
783 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
784 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000785 case 0x91d02000:
786 /* SunOS syscall trap. (pers 1) */
787 fprintf(stderr,"syscall: SunOS no support\n");
788 return -1;
789 case 0x91d02008:
790 /* Solaris 2.x syscall trap. (per 2) */
791 set_personality(1);
792 break;
793 case 0x91d02009:
794 /* NetBSD/FreeBSD syscall trap. */
795 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
796 return -1;
797 case 0x91d02027:
798 /* Solaris 2.x gettimeofday */
799 set_personality(1);
800 break;
801 default:
802 /* Unknown syscall trap. */
803 if(tcp->flags & TCB_WAITEXECVE) {
804 tcp->flags &= ~TCB_WAITEXECVE;
805 return 0;
806 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000807 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000808 return -1;
809 }
810
811 /* Extract the system call number from the registers. */
812 if (trap == 0x91d02027)
813 scno = 156;
814 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000815 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000816 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000817 scno = regs.r_o0;
818 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000819 }
820 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000821#elif defined(HPPA)
822 if (upeek(pid, PT_GR20, &scno) < 0)
823 return -1;
824 if (!(tcp->flags & TCB_INSYSCALL)) {
825 /* Check if we return from execve. */
826 if ((tcp->flags & TCB_WAITEXECVE)) {
827 tcp->flags &= ~TCB_WAITEXECVE;
828 return 0;
829 }
830 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000831#endif
832#endif /* LINUX */
833#ifdef SUNOS4
834 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
835 return -1;
836#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000837#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +0000839 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000840#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000841#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000842 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000843#else /* FREEBSD */
844 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
845 perror("pread");
846 return -1;
847 }
848 switch (regs.r_eax) {
849 case SYS_syscall:
850 case SYS___syscall:
851 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
852 break;
853 default:
854 scno = regs.r_eax;
855 break;
856 }
857#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000858#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000859#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000860 if (!(tcp->flags & TCB_INSYSCALL))
861 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000862 return 1;
863}
864
Pavel Machek4dc3b142000-02-01 17:58:41 +0000865
866int
867syscall_fixup(tcp)
868struct tcb *tcp;
869{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000870#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +0000871 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000872#else /* USE_PROCFS */
873 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000874
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000875 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000876 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000877 if (
878 scno == SYS_fork
879#ifdef SYS_vfork
880 || scno == SYS_vfork
881#endif /* SYS_vfork */
882 ) {
883 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000884 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000885 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000886 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000887 }
888 else {
889 fprintf(stderr, "syscall: missing entry\n");
890 tcp->flags |= TCB_INSYSCALL;
891 }
892 }
893 }
894 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000895 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000896 fprintf(stderr, "syscall: missing exit\n");
897 tcp->flags &= ~TCB_INSYSCALL;
898 }
899 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000900#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000901#ifdef SUNOS4
902 if (!(tcp->flags & TCB_INSYSCALL)) {
903 if (scno == 0) {
904 fprintf(stderr, "syscall: missing entry\n");
905 tcp->flags |= TCB_INSYSCALL;
906 }
907 }
908 else {
909 if (scno != 0) {
910 if (debug) {
911 /*
912 * This happens when a signal handler
913 * for a signal which interrupted a
914 * a system call makes another system call.
915 */
916 fprintf(stderr, "syscall: missing exit\n");
917 }
918 tcp->flags &= ~TCB_INSYSCALL;
919 }
920 }
921#endif /* SUNOS4 */
922#ifdef LINUX
923#if defined (I386)
924 if (upeek(pid, 4*EAX, &eax) < 0)
925 return -1;
926 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
927 if (debug)
928 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
929 return 0;
930 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000931#elif defined (S390)
932 if (upeek(pid, PT_GPR2, &gpr2) < 0)
933 return -1;
934 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
935 if (debug)
936 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
937 return 0;
938 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000939#elif defined (POWERPC)
940# define SO_MASK 0x10000000
941 if (upeek(pid, 4*PT_CCR, &flags) < 0)
942 return -1;
943 if (upeek(pid, 4*PT_R3, &result) < 0)
944 return -1;
945 if (flags & SO_MASK)
946 result = -result;
947#elif defined (M68K)
948 if (upeek(pid, 4*PT_D0, &d0) < 0)
949 return -1;
950 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
951 if (debug)
952 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
953 return 0;
954 }
955#elif defined (ARM)
956 if (upeek(pid, 4*0, (long *)&r0) < 0)
957 return -1;
958 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
959 if (debug)
960 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
961 return 0;
962 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000963#elif defined (HPPA)
964 if (upeek(pid, PT_GR28, &r28) < 0)
965 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000966#endif
967#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000968 return 1;
969}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000970
Pavel Machek4dc3b142000-02-01 17:58:41 +0000971int
972get_error(tcp)
973struct tcb *tcp;
974{
975 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000976#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000977#ifdef S390
978 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
979 tcp->u_rval = -1;
980 u_error = -gpr2;
981 }
982 else {
983 tcp->u_rval = gpr2;
984 u_error = 0;
985 }
986#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000987#ifdef I386
988 if (eax < 0 && -eax < nerrnos) {
989 tcp->u_rval = -1;
990 u_error = -eax;
991 }
992 else {
993 tcp->u_rval = eax;
994 u_error = 0;
995 }
996#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000997#ifdef IA64
998 if (ia32) {
999 int err;
1000
1001 err = (int)r8;
1002 if (err < 0 && -err < nerrnos) {
1003 tcp->u_rval = -1;
1004 u_error = -err;
1005 }
1006 else {
1007 tcp->u_rval = err;
1008 u_error = 0;
1009 }
1010 } else {
1011 if (r10) {
1012 tcp->u_rval = -1;
1013 u_error = r8;
1014 } else {
1015 tcp->u_rval = r8;
1016 u_error = 0;
1017 }
1018 }
1019#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001020#ifdef MIPS
1021 if (a3) {
1022 tcp->u_rval = -1;
1023 u_error = r2;
1024 } else {
1025 tcp->u_rval = r2;
1026 u_error = 0;
1027 }
1028#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001029#ifdef POWERPC
1030 if (result && (unsigned) -result < nerrnos) {
1031 tcp->u_rval = -1;
1032 u_error = -result;
1033 }
1034 else {
1035 tcp->u_rval = result;
1036 u_error = 0;
1037 }
1038#else /* !POWERPC */
1039#ifdef M68K
1040 if (d0 && (unsigned) -d0 < nerrnos) {
1041 tcp->u_rval = -1;
1042 u_error = -d0;
1043 }
1044 else {
1045 tcp->u_rval = d0;
1046 u_error = 0;
1047 }
1048#else /* !M68K */
1049#ifdef ARM
1050 if (r0 && (unsigned) -r0 < nerrnos) {
1051 tcp->u_rval = -1;
1052 u_error = -r0;
1053 }
1054 else {
1055 tcp->u_rval = r0;
1056 u_error = 0;
1057 }
1058#else /* !ARM */
1059#ifdef ALPHA
1060 if (a3) {
1061 tcp->u_rval = -1;
1062 u_error = r0;
1063 }
1064 else {
1065 tcp->u_rval = r0;
1066 u_error = 0;
1067 }
1068#else /* !ALPHA */
1069#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001070 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001071 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001072 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001073 }
1074 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001075 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001076 u_error = 0;
1077 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001078#else /* !SPARC */
1079#ifdef HPPA
1080 if (r28 && (unsigned) -r28 < nerrnos) {
1081 tcp->u_rval = -1;
1082 u_error = -r28;
1083 }
1084 else {
1085 tcp->u_rval = r28;
1086 u_error = 0;
1087 }
1088#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001089#endif /* SPARC */
1090#endif /* ALPHA */
1091#endif /* ARM */
1092#endif /* M68K */
1093#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001094#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001095#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001096#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001097#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001098#endif /* LINUX */
1099#ifdef SUNOS4
1100 /* get error code from user struct */
1101 if (upeek(pid, uoff(u_error), &u_error) < 0)
1102 return -1;
1103 u_error >>= 24; /* u_error is a char */
1104
1105 /* get system call return value */
1106 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1107 return -1;
1108#endif /* SUNOS4 */
1109#ifdef SVR4
1110#ifdef SPARC
1111 /* Judicious guessing goes a long way. */
1112 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1113 tcp->u_rval = -1;
1114 u_error = tcp->status.pr_reg[R_O0];
1115 }
1116 else {
1117 tcp->u_rval = tcp->status.pr_reg[R_O0];
1118 u_error = 0;
1119 }
1120#endif /* SPARC */
1121#ifdef I386
1122 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001123 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001125 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126 }
1127 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001128 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001129#ifdef HAVE_LONG_LONG
1130 tcp->u_lrval =
1131 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1132 tcp->status.PR_REG[EAX];
1133#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001134 u_error = 0;
1135 }
1136#endif /* I386 */
1137#ifdef MIPS
1138 if (tcp->status.pr_reg[CTX_A3]) {
1139 tcp->u_rval = -1;
1140 u_error = tcp->status.pr_reg[CTX_V0];
1141 }
1142 else {
1143 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1144 u_error = 0;
1145 }
1146#endif /* MIPS */
1147#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001148#ifdef FREEBSD
1149 if (regs.r_eflags & PSL_C) {
1150 tcp->u_rval = -1;
1151 u_error = regs.r_eax;
1152 } else {
1153 tcp->u_rval = regs.r_eax;
1154 tcp->u_lrval =
1155 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1156 u_error = 0;
1157 }
1158#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001159 tcp->u_error = u_error;
1160 return 1;
1161}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001162
Pavel Machek4dc3b142000-02-01 17:58:41 +00001163int syscall_enter(tcp)
1164struct tcb *tcp;
1165{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001166#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001167 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001168#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001169#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001170#if defined(S390)
1171 {
1172 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001173 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1174 tcp->u_nargs = sysent[tcp->scno].nargs;
1175 else
1176 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001177 for (i = 0; i < tcp->u_nargs; i++) {
1178 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1179 return -1;
1180 }
1181 }
1182#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001183 {
1184 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001185 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1186 tcp->u_nargs = sysent[tcp->scno].nargs;
1187 else
1188 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001189 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001190 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1191 * for scno somewhere above here!
1192 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001193 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1194 return -1;
1195 }
1196 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001197#elif defined (IA64)
1198 {
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001199 unsigned long *bsp, cfm, i;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001200
1201 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1202 return -1;
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001203 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1204 return -1;
1205
1206 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001207
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001208 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1209 tcp->u_nargs = sysent[tcp->scno].nargs;
1210 else
1211 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001212 for (i = 0; i < tcp->u_nargs; ++i) {
1213 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1214 (char *) &tcp->u_arg[i])
1215 < 0)
1216 return -1;
1217 }
1218 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001219#elif defined (MIPS)
1220 {
1221 long sp;
1222 int i, nargs;
1223
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001224 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1225 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1226 else
1227 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001228 if(nargs > 4) {
1229 if(upeek(pid, REG_SP, &sp) < 0)
1230 return -1;
1231 for(i = 0; i < 4; i++) {
1232 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1233 return -1;
1234 }
1235 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1236 (char *)(tcp->u_arg + 4));
1237 } else {
1238 for(i = 0; i < nargs; i++) {
1239 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1240 return -1;
1241 }
1242 }
1243 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001244#elif defined (POWERPC)
1245 {
1246 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001247 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1248 tcp->u_nargs = sysent[tcp->scno].nargs;
1249 else
1250 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001251 for (i = 0; i < tcp->u_nargs; i++) {
1252 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1253 return -1;
1254 }
1255 }
1256#elif defined (SPARC)
1257 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001258 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001259
1260 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1261 tcp->u_nargs = sysent[tcp->scno].nargs;
1262 else
1263 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001264 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001265 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001266 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001267#elif defined (HPPA)
1268 {
1269 int i;
1270
1271 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1272 tcp->u_nargs = sysent[tcp->scno].nargs;
1273 else
1274 tcp->u_nargs = MAX_ARGS;
1275 for (i = 0; i < tcp->u_nargs; i++) {
1276 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1277 return -1;
1278 }
1279 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001280#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001281 {
1282 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001283 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1284 tcp->u_nargs = sysent[tcp->scno].nargs;
1285 else
1286 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001287 for (i = 0; i < tcp->u_nargs; i++) {
1288 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1289 return -1;
1290 }
1291 }
1292#endif
1293#endif /* LINUX */
1294#ifdef SUNOS4
1295 {
1296 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001297 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1298 tcp->u_nargs = sysent[tcp->scno].nargs;
1299 else
1300 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001301 for (i = 0; i < tcp->u_nargs; i++) {
1302 struct user *u;
1303
1304 if (upeek(pid, uoff(u_arg[0]) +
1305 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1306 return -1;
1307 }
1308 }
1309#endif /* SUNOS4 */
1310#ifdef SVR4
1311#ifdef MIPS
1312 /*
1313 * SGI is broken: even though it has pr_sysarg, it doesn't
1314 * set them on system call entry. Get a clue.
1315 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001316 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001317 tcp->u_nargs = sysent[tcp->scno].nargs;
1318 else
1319 tcp->u_nargs = tcp->status.pr_nsysarg;
1320 if (tcp->u_nargs > 4) {
1321 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1322 4*sizeof(tcp->u_arg[0]));
1323 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1324 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1325 }
1326 else {
1327 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1328 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1329 }
John Hughes25299712001-03-06 10:10:06 +00001330#elif UNIXWARE >= 2
1331 /*
1332 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1333 */
1334 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1335 tcp->u_nargs = sysent[tcp->scno].nargs;
1336 else
1337 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1338 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1339 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1340#elif defined (HAVE_PR_SYSCALL)
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 = tcp->status.pr_nsysarg;
1345 {
1346 int i;
1347 for (i = 0; i < tcp->u_nargs; i++)
1348 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1349 }
John Hughes25299712001-03-06 10:10:06 +00001350#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001351 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001352 tcp->u_nargs = sysent[tcp->scno].nargs;
1353 else
1354 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001355 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001356 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001357#else
1358 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001359#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001360#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001361#ifdef FREEBSD
1362 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1363 sysent[tcp->scno].nargs > tcp->status.val)
1364 tcp->u_nargs = sysent[tcp->scno].nargs;
1365 else
1366 tcp->u_nargs = tcp->status.val;
1367 if (tcp->u_nargs < 0)
1368 tcp->u_nargs = 0;
1369 if (tcp->u_nargs > MAX_ARGS)
1370 tcp->u_nargs = MAX_ARGS;
1371 switch(regs.r_eax) {
1372 case SYS___syscall:
1373 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1374 regs.r_esp + sizeof(int) + sizeof(quad_t));
1375 break;
1376 case SYS_syscall:
1377 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1378 regs.r_esp + 2 * sizeof(int));
1379 break;
1380 default:
1381 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1382 regs.r_esp + sizeof(int));
1383 break;
1384 }
1385#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001386 return 1;
1387}
1388
1389int
1390trace_syscall(tcp)
1391struct tcb *tcp;
1392{
1393 int sys_res;
1394 struct timeval tv;
1395 int res;
1396
1397 /* Measure the exit time as early as possible to avoid errors. */
1398 if (dtime && (tcp->flags & TCB_INSYSCALL))
1399 gettimeofday(&tv, NULL);
1400
1401 res = get_scno(tcp);
1402 if (res != 1)
1403 return res;
1404
1405 res = syscall_fixup(tcp);
1406 if (res != 1)
1407 return res;
1408
1409 if (tcp->flags & TCB_INSYSCALL) {
1410 long u_error;
1411 res = get_error(tcp);
1412 if (res != 1)
1413 return res;
1414 u_error = tcp->u_error;
1415
1416
1417 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001418 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1419 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001420 tcp->flags &= ~TCB_INSYSCALL;
1421 return 0;
1422 }
1423
1424 if (tcp->flags & TCB_REPRINT) {
1425 printleader(tcp);
1426 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001427 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001428 tprintf("syscall_%lu", tcp->scno);
1429 else
1430 tprintf("%s", sysent[tcp->scno].sys_name);
1431 tprintf(" resumed> ");
1432 }
1433
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001434 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001435 call_count[tcp->scno]++;
1436 if (tcp->u_error)
1437 error_count[tcp->scno]++;
1438 tv_sub(&tv, &tv, &tcp->etime);
1439#ifdef LINUX
1440 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1441 static struct timeval one_tick =
1442 { 0, 1000000 / HZ };
1443
1444 if (tv_nz(&tcp->dtime))
1445 tv = tcp->dtime;
1446 else if (tv_cmp(&tv, &one_tick) > 0) {
1447 if (tv_cmp(&shortest, &one_tick) < 0)
1448 tv = shortest;
1449 else
1450 tv = one_tick;
1451 }
1452 }
1453#endif /* LINUX */
1454 if (tv_cmp(&tv, &shortest) < 0)
1455 shortest = tv;
1456 tv_add(&tv_count[tcp->scno],
1457 &tv_count[tcp->scno], &tv);
1458 tcp->flags &= ~TCB_INSYSCALL;
1459 return 0;
1460 }
1461
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001462 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001463 || (qual_flags[tcp->scno] & QUAL_RAW))
1464 sys_res = printargs(tcp);
1465 else
1466 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1467 u_error = tcp->u_error;
1468 tprintf(") ");
1469 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001470 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1471 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001472 if (u_error)
1473 tprintf("= -1 (errno %ld)", u_error);
1474 else
1475 tprintf("= %#lx", tcp->u_rval);
1476 }
1477 else if (!(sys_res & RVAL_NONE) && u_error) {
1478 switch (u_error) {
1479#ifdef LINUX
1480 case ERESTARTSYS:
1481 tprintf("= ? ERESTARTSYS (To be restarted)");
1482 break;
1483 case ERESTARTNOINTR:
1484 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1485 break;
1486 case ERESTARTNOHAND:
1487 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1488 break;
1489#endif /* LINUX */
1490 default:
1491 tprintf("= -1 ");
1492 if (u_error < nerrnos && u_error < sys_nerr)
1493 tprintf("%s (%s)", errnoent[u_error],
1494 sys_errlist[u_error]);
1495 else if (u_error < nerrnos)
1496 tprintf("%s (errno %ld)",
1497 errnoent[u_error], u_error);
1498 else if (u_error < sys_nerr)
1499 tprintf("ERRNO_%ld (%s)", u_error,
1500 sys_errlist[u_error]);
1501 else
1502 tprintf("E??? (errno %ld)", u_error);
1503 break;
1504 }
1505 }
1506 else {
1507 if (sys_res & RVAL_NONE)
1508 tprintf("= ?");
1509 else {
1510 switch (sys_res & RVAL_MASK) {
1511 case RVAL_HEX:
1512 tprintf("= %#lx", tcp->u_rval);
1513 break;
1514 case RVAL_OCTAL:
1515 tprintf("= %#lo", tcp->u_rval);
1516 break;
1517 case RVAL_UDECIMAL:
1518 tprintf("= %lu", tcp->u_rval);
1519 break;
1520 case RVAL_DECIMAL:
1521 tprintf("= %ld", tcp->u_rval);
1522 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001523#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001524 case RVAL_LHEX:
1525 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001526 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001527 case RVAL_LOCTAL:
1528 tprintf("= %#llo", tcp->u_lrval);
1529 break;
1530 case RVAL_LUDECIMAL:
1531 tprintf("= %llu", tcp->u_lrval);
1532 break;
1533 case RVAL_LDECIMAL:
1534 tprintf("= %lld", tcp->u_lrval);
1535 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001536#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001537 default:
1538 fprintf(stderr,
1539 "invalid rval format\n");
1540 break;
1541 }
1542 }
1543 if ((sys_res & RVAL_STR) && tcp->auxstr)
1544 tprintf(" (%s)", tcp->auxstr);
1545 }
1546 if (dtime) {
1547 tv_sub(&tv, &tv, &tcp->etime);
1548 tprintf(" <%ld.%06ld>",
1549 (long) tv.tv_sec, (long) tv.tv_usec);
1550 }
1551 printtrailer(tcp);
1552
1553 dumpio(tcp);
1554 if (fflush(tcp->outf) == EOF)
1555 return -1;
1556 tcp->flags &= ~TCB_INSYSCALL;
1557 return 0;
1558 }
1559
1560 /* Entering system call */
1561 res = syscall_enter(tcp);
1562 if (res != 1)
1563 return res;
1564
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001565 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001566#ifdef LINUX
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001567#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001568 case SYS_socketcall:
1569 decode_subcall(tcp, SYS_socket_subcall,
1570 SYS_socket_nsubcalls, deref_style);
1571 break;
1572 case SYS_ipc:
1573 decode_subcall(tcp, SYS_ipc_subcall,
1574 SYS_ipc_nsubcalls, shift_style);
1575 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001576#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001577#ifdef SPARC
1578 case SYS_socketcall:
1579 sparc_socket_decode (tcp);
1580 break;
1581#endif
1582#endif /* LINUX */
1583#ifdef SVR4
1584#ifdef SYS_pgrpsys_subcall
1585 case SYS_pgrpsys:
1586 decode_subcall(tcp, SYS_pgrpsys_subcall,
1587 SYS_pgrpsys_nsubcalls, shift_style);
1588 break;
1589#endif /* SYS_pgrpsys_subcall */
1590#ifdef SYS_sigcall_subcall
1591 case SYS_sigcall:
1592 decode_subcall(tcp, SYS_sigcall_subcall,
1593 SYS_sigcall_nsubcalls, mask_style);
1594 break;
1595#endif /* SYS_sigcall_subcall */
1596 case SYS_msgsys:
1597 decode_subcall(tcp, SYS_msgsys_subcall,
1598 SYS_msgsys_nsubcalls, shift_style);
1599 break;
1600 case SYS_shmsys:
1601 decode_subcall(tcp, SYS_shmsys_subcall,
1602 SYS_shmsys_nsubcalls, shift_style);
1603 break;
1604 case SYS_semsys:
1605 decode_subcall(tcp, SYS_semsys_subcall,
1606 SYS_semsys_nsubcalls, shift_style);
1607 break;
1608#if 0 /* broken */
1609 case SYS_utssys:
1610 decode_subcall(tcp, SYS_utssys_subcall,
1611 SYS_utssys_nsubcalls, shift_style);
1612 break;
1613#endif
1614 case SYS_sysfs:
1615 decode_subcall(tcp, SYS_sysfs_subcall,
1616 SYS_sysfs_nsubcalls, shift_style);
1617 break;
1618 case SYS_spcall:
1619 decode_subcall(tcp, SYS_spcall_subcall,
1620 SYS_spcall_nsubcalls, shift_style);
1621 break;
1622#ifdef SYS_context_subcall
1623 case SYS_context:
1624 decode_subcall(tcp, SYS_context_subcall,
1625 SYS_context_nsubcalls, shift_style);
1626 break;
1627#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001628#ifdef SYS_door_subcall
1629 case SYS_door:
1630 decode_subcall(tcp, SYS_door_subcall,
1631 SYS_door_nsubcalls, door_style);
1632 break;
1633#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00001634#ifdef SYS_kaio_subcall
1635 case SYS_kaio:
1636 decode_subcall(tcp, SYS_kaio_subcall,
1637 SYS_kaio_nsubcalls, shift_style);
1638 break;
1639#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001641#ifdef FREEBSD
1642 case SYS_msgsys:
1643 case SYS_shmsys:
1644 case SYS_semsys:
1645 decode_subcall(tcp, 0, 0, table_style);
1646 break;
1647#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001648#ifdef SUNOS4
1649 case SYS_semsys:
1650 decode_subcall(tcp, SYS_semsys_subcall,
1651 SYS_semsys_nsubcalls, shift_style);
1652 break;
1653 case SYS_msgsys:
1654 decode_subcall(tcp, SYS_msgsys_subcall,
1655 SYS_msgsys_nsubcalls, shift_style);
1656 break;
1657 case SYS_shmsys:
1658 decode_subcall(tcp, SYS_shmsys_subcall,
1659 SYS_shmsys_nsubcalls, shift_style);
1660 break;
1661#endif
1662 }
1663
1664 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001665 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001666 tcp->flags |= TCB_INSYSCALL;
1667 return 0;
1668 }
1669
1670 if (cflag) {
1671 gettimeofday(&tcp->etime, NULL);
1672 tcp->flags |= TCB_INSYSCALL;
1673 return 0;
1674 }
1675
1676 printleader(tcp);
1677 tcp->flags &= ~TCB_REPRINT;
1678 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001679 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001680 tprintf("syscall_%lu(", tcp->scno);
1681 else
1682 tprintf("%s(", sysent[tcp->scno].sys_name);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001683 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1685 sys_res = printargs(tcp);
1686 else
1687 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1688 if (fflush(tcp->outf) == EOF)
1689 return -1;
1690 tcp->flags |= TCB_INSYSCALL;
1691 /* Measure the entrance time as late as possible to avoid errors. */
1692 if (dtime)
1693 gettimeofday(&tcp->etime, NULL);
1694 return sys_res;
1695}
1696
1697int
1698printargs(tcp)
1699struct tcb *tcp;
1700{
1701 if (entering(tcp)) {
1702 int i;
1703
1704 for (i = 0; i < tcp->u_nargs; i++)
1705 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1706 }
1707 return 0;
1708}
1709
1710long
1711getrval2(tcp)
1712struct tcb *tcp;
1713{
1714 long val = -1;
1715
1716#ifdef LINUX
1717#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001718 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001719 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1720 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001721 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001722#endif /* SPARC */
1723#endif /* LINUX */
1724
1725#ifdef SUNOS4
1726 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1727 return -1;
1728#endif /* SUNOS4 */
1729
1730#ifdef SVR4
1731#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001732 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001733#endif /* SPARC */
1734#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001735 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001736#endif /* I386 */
1737#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001738 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001739#endif /* MIPS */
1740#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001741#ifdef FREEBSD
1742 struct reg regs;
1743 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1744 val = regs.r_edx;
1745#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001746 return val;
1747}
1748
1749/*
1750 * Apparently, indirect system calls have already be converted by ptrace(2),
1751 * so if you see "indir" this program has gone astray.
1752 */
1753int
1754sys_indir(tcp)
1755struct tcb *tcp;
1756{
1757 int i, scno, nargs;
1758
1759 if (entering(tcp)) {
1760 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1761 fprintf(stderr, "Bogus syscall: %u\n", scno);
1762 return 0;
1763 }
1764 nargs = sysent[scno].nargs;
1765 tprintf("%s", sysent[scno].sys_name);
1766 for (i = 0; i < nargs; i++)
1767 tprintf(", %#lx", tcp->u_arg[i+1]);
1768 }
1769 return 0;
1770}
1771
1772static int
1773time_cmp(a, b)
1774void *a;
1775void *b;
1776{
1777 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1778}
1779
1780static int
1781syscall_cmp(a, b)
1782void *a;
1783void *b;
1784{
1785 return strcmp(sysent[*((int *) a)].sys_name,
1786 sysent[*((int *) b)].sys_name);
1787}
1788
1789static int
1790count_cmp(a, b)
1791void *a;
1792void *b;
1793{
1794 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1795
1796 return (m < n) ? 1 : (m > n) ? -1 : 0;
1797}
1798
1799static int (*sortfun)();
1800static struct timeval overhead = { -1, -1 };
1801
1802void
1803set_sortby(sortby)
1804char *sortby;
1805{
1806 if (strcmp(sortby, "time") == 0)
1807 sortfun = time_cmp;
1808 else if (strcmp(sortby, "calls") == 0)
1809 sortfun = count_cmp;
1810 else if (strcmp(sortby, "name") == 0)
1811 sortfun = syscall_cmp;
1812 else if (strcmp(sortby, "nothing") == 0)
1813 sortfun = NULL;
1814 else {
1815 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1816 exit(1);
1817 }
1818}
1819
1820void set_overhead(n)
1821int n;
1822{
1823 overhead.tv_sec = n / 1000000;
1824 overhead.tv_usec = n % 1000000;
1825}
1826
1827void
1828call_summary(outf)
1829FILE *outf;
1830{
1831 int i, j;
1832 int call_cum, error_cum;
1833 struct timeval tv_cum, dtv;
1834 double percent;
1835 char *dashes = "-------------------------";
1836 char error_str[16];
1837
1838 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1839 if (overhead.tv_sec == -1) {
1840 tv_mul(&overhead, &shortest, 8);
1841 tv_div(&overhead, &overhead, 10);
1842 }
1843 for (i = 0; i < nsyscalls; i++) {
1844 sorted_count[i] = i;
1845 if (call_count[i] == 0)
1846 continue;
1847 tv_mul(&dtv, &overhead, call_count[i]);
1848 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1849 call_cum += call_count[i];
1850 error_cum += error_count[i];
1851 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1852 }
1853 if (sortfun)
1854 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1855 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1856 "% time", "seconds", "usecs/call",
1857 "calls", "errors", "syscall");
1858 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1859 dashes, dashes, dashes, dashes, dashes, dashes);
1860 for (i = 0; i < nsyscalls; i++) {
1861 j = sorted_count[i];
1862 if (call_count[j] == 0)
1863 continue;
1864 tv_div(&dtv, &tv_count[j], call_count[j]);
1865 if (error_count[j])
1866 sprintf(error_str, "%d", error_count[j]);
1867 else
1868 error_str[0] = '\0';
1869 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1870 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1871 percent, (long) tv_count[j].tv_sec,
1872 (long) tv_count[j].tv_usec,
1873 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1874 call_count[j], error_str, sysent[j].sys_name);
1875 }
1876 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1877 dashes, dashes, dashes, dashes, dashes, dashes);
1878 if (error_cum)
1879 sprintf(error_str, "%d", error_cum);
1880 else
1881 error_str[0] = '\0';
1882 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1883 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1884 call_cum, error_str, "total");
1885}