blob: 6407224ed37f7d449aa9c676102a9a4200015c06 [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#if HAVE_LINUX_PTRACE_H
50#undef PTRACE_SYSCALL
Wichert Akkermanb046b381999-07-13 22:20:16 +000051#include <linux/ptrace.h>
52#endif
53
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#ifdef HAVE_SYS_REG_H
55#include <sys/reg.h>
56#ifndef PTRACE_PEEKUSR
57# define PTRACE_PEEKUSR PTRACE_PEEKUSER
58#endif
59#endif
60
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000061#ifndef SYS_ERRLIST_DECLARED
62extern int sys_nerr;
63extern char *sys_errlist[];
64#endif /* SYS_ERRLIST_DECLARED */
65
Pavel Machekd8ae7e32000-02-01 17:17:25 +000066#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000067#ifdef LINUX
68#ifndef ERESTARTSYS
69#define ERESTARTSYS 512
70#endif
71#ifndef ERESTARTNOINTR
72#define ERESTARTNOINTR 513
73#endif
74#ifndef ERESTARTNOHAND
75#define ERESTARTNOHAND 514 /* restart if no handler.. */
76#endif
77#ifndef ENOIOCTLCMD
78#define ENOIOCTLCMD 515 /* No ioctl command */
79#endif
80#ifndef NSIG
81#define NSIG 32
82#endif
83#ifdef ARM
84#undef NSIG
85#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000086#undef NR_SYSCALL_BASE
87#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088#endif
89#endif /* LINUX */
90
91#include "syscall.h"
92
93/* Define these shorthand notations to simplify the syscallent files. */
94#define TF TRACE_FILE
95#define TI TRACE_IPC
96#define TN TRACE_NETWORK
97#define TP TRACE_PROCESS
98#define TS TRACE_SIGNAL
99
100struct sysent sysent0[] = {
101#include "syscallent.h"
102};
103int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
104
105#if SUPPORTED_PERSONALITIES >= 2
106struct sysent sysent1[] = {
107#include "syscallent1.h"
108};
109int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
110#endif /* SUPPORTED_PERSONALITIES >= 2 */
111
112#if SUPPORTED_PERSONALITIES >= 3
113struct sysent sysent2[] = {
114#include "syscallent2.h"
115};
116int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
117#endif /* SUPPORTED_PERSONALITIES >= 3 */
118
119struct sysent *sysent;
120int nsyscalls;
121
122/* Now undef them since short defines cause wicked namespace pollution. */
123#undef TF
124#undef TI
125#undef TN
126#undef TP
127#undef TS
128
129char *errnoent0[] = {
130#include "errnoent.h"
131};
132int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
133
134#if SUPPORTED_PERSONALITIES >= 2
135char *errnoent1[] = {
136#include "errnoent1.h"
137};
138int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
139#endif /* SUPPORTED_PERSONALITIES >= 2 */
140
141#if SUPPORTED_PERSONALITIES >= 3
142char *errnoent2[] = {
143#include "errnoent2.h"
144};
145int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
146#endif /* SUPPORTED_PERSONALITIES >= 3 */
147
148char **errnoent;
149int nerrnos;
150
151int current_personality;
152
153int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000154set_personality(personality)
155int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000156{
157 switch (personality) {
158 case 0:
159 errnoent = errnoent0;
160 nerrnos = nerrnos0;
161 sysent = sysent0;
162 nsyscalls = nsyscalls0;
163 ioctlent = ioctlent0;
164 nioctlents = nioctlents0;
165 signalent = signalent0;
166 nsignals = nsignals0;
167 break;
168
169#if SUPPORTED_PERSONALITIES >= 2
170 case 1:
171 errnoent = errnoent1;
172 nerrnos = nerrnos1;
173 sysent = sysent1;
174 nsyscalls = nsyscalls1;
175 ioctlent = ioctlent1;
176 nioctlents = nioctlents1;
177 signalent = signalent1;
178 nsignals = nsignals1;
179 break;
180#endif /* SUPPORTED_PERSONALITIES >= 2 */
181
182#if SUPPORTED_PERSONALITIES >= 3
183 case 2:
184 errnoent = errnoent2;
185 nerrnos = nerrnos2;
186 sysent = sysent2;
187 nsyscalls = nsyscalls2;
188 ioctlent = ioctlent2;
189 nioctlents = nioctlents2;
190 signalent = signalent2;
191 nsignals = nsignals2;
192 break;
193#endif /* SUPPORTED_PERSONALITIES >= 3 */
194
195 default:
196 return -1;
197 }
198
199 current_personality = personality;
200 return 0;
201}
202
203int qual_flags[MAX_QUALS];
204
205static int call_count[MAX_QUALS];
206static int error_count[MAX_QUALS];
207static struct timeval tv_count[MAX_QUALS];
208static int sorted_count[MAX_QUALS];
209
210static struct timeval shortest = { 1000000, 0 };
211
212static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
213
214static struct qual_options {
215 int bitflag;
216 char *option_name;
217 int (*lookup)();
218 char *argument_name;
219} qual_options[] = {
220 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
221 { QUAL_TRACE, "t", lookup_syscall, "system call" },
222 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
223 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
224 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
225 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
226 { QUAL_RAW, "raw", lookup_syscall, "system call" },
227 { QUAL_RAW, "x", lookup_syscall, "system call" },
228 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
229 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
230 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
231 { QUAL_FAULT, "fault", lookup_fault, "fault" },
232 { QUAL_FAULT, "faults", lookup_fault, "fault" },
233 { QUAL_FAULT, "m", lookup_fault, "fault" },
234 { QUAL_READ, "read", lookup_desc, "descriptor" },
235 { QUAL_READ, "reads", lookup_desc, "descriptor" },
236 { QUAL_READ, "r", lookup_desc, "descriptor" },
237 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
238 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
239 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
240 { 0, NULL, NULL, NULL },
241};
242
243static int
244lookup_syscall(s)
245char *s;
246{
247 int i;
248
249 for (i = 0; i < nsyscalls; i++) {
250 if (strcmp(s, sysent[i].sys_name) == 0)
251 return i;
252 }
253 return -1;
254}
255
256static int
257lookup_signal(s)
258char *s;
259{
260 int i;
261 char buf[32];
262
263 if (s && *s && isdigit(*s))
264 return atoi(s);
265 strcpy(buf, s);
266 s = buf;
267 for (i = 0; s[i]; i++)
268 s[i] = toupper(s[i]);
269 if (strncmp(s, "SIG", 3) == 0)
270 s += 3;
271 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000272 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000273 return i;
274 }
275 return -1;
276}
277
278static int
279lookup_fault(s)
280char *s;
281{
282 return -1;
283}
284
285static int
286lookup_desc(s)
287char *s;
288{
289 if (s && *s && isdigit(*s))
290 return atoi(s);
291 return -1;
292}
293
294static int
295lookup_class(s)
296char *s;
297{
298 if (strcmp(s, "file") == 0)
299 return TRACE_FILE;
300 if (strcmp(s, "ipc") == 0)
301 return TRACE_IPC;
302 if (strcmp(s, "network") == 0)
303 return TRACE_NETWORK;
304 if (strcmp(s, "process") == 0)
305 return TRACE_PROCESS;
306 if (strcmp(s, "signal") == 0)
307 return TRACE_SIGNAL;
308 return -1;
309}
310
311void
312qualify(s)
313char *s;
314{
315 struct qual_options *opt;
316 int not;
317 char *p;
318 int i, n;
319
320 opt = &qual_options[0];
321 for (i = 0; (p = qual_options[i].option_name); i++) {
322 n = strlen(p);
323 if (strncmp(s, p, n) == 0 && s[n] == '=') {
324 opt = &qual_options[i];
325 s += n + 1;
326 break;
327 }
328 }
329 not = 0;
330 if (*s == '!') {
331 not = 1;
332 s++;
333 }
334 if (strcmp(s, "none") == 0) {
335 not = 1 - not;
336 s = "all";
337 }
338 if (strcmp(s, "all") == 0) {
339 for (i = 0; i < MAX_QUALS; i++) {
340 if (not)
341 qual_flags[i] &= ~opt->bitflag;
342 else
343 qual_flags[i] |= opt->bitflag;
344 }
345 return;
346 }
347 for (i = 0; i < MAX_QUALS; i++) {
348 if (not)
349 qual_flags[i] |= opt->bitflag;
350 else
351 qual_flags[i] &= ~opt->bitflag;
352 }
353 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
354 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
355 for (i = 0; i < MAX_QUALS; i++) {
356 if (sysent[i].sys_flags & n) {
357 if (not)
358 qual_flags[i] &= ~opt->bitflag;
359 else
360 qual_flags[i] |= opt->bitflag;
361 }
362 }
363 continue;
364 }
365 if ((n = (*opt->lookup)(p)) < 0) {
366 fprintf(stderr, "strace: invalid %s `%s'\n",
367 opt->argument_name, p);
368 exit(1);
369 }
370 if (not)
371 qual_flags[n] &= ~opt->bitflag;
372 else
373 qual_flags[n] |= opt->bitflag;
374 }
375 return;
376}
377
378static void
379dumpio(tcp)
380struct tcb *tcp;
381{
382 if (syserror(tcp))
383 return;
384 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
385 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000386 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000387 case SYS_read:
388#ifdef SYS_recv
389 case SYS_recv:
390#endif
391#ifdef SYS_recvfrom
392 case SYS_recvfrom:
393#endif
394 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
395 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
396 break;
397 case SYS_write:
398#ifdef SYS_send
399 case SYS_send:
400#endif
401#ifdef SYS_sendto
402 case SYS_sendto:
403#endif
404 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
405 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
406 break;
407 }
408}
409
Wichert Akkerman8829a551999-06-11 13:18:40 +0000410enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000411
Wichert Akkermanf90da011999-10-31 21:15:38 +0000412#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000413
414const int socket_map [] = {
415 /* SYS_SOCKET */ 97,
416 /* SYS_BIND */ 104,
417 /* SYS_CONNECT */ 98,
418 /* SYS_LISTEN */ 106,
419 /* SYS_ACCEPT */ 99,
420 /* SYS_GETSOCKNAME */ 150,
421 /* SYS_GETPEERNAME */ 141,
422 /* SYS_SOCKETPAIR */ 135,
423 /* SYS_SEND */ 101,
424 /* SYS_RECV */ 102,
425 /* SYS_SENDTO */ 133,
426 /* SYS_RECVFROM */ 125,
427 /* SYS_SHUTDOWN */ 134,
428 /* SYS_SETSOCKOPT */ 105,
429 /* SYS_GETSOCKOPT */ 118,
430 /* SYS_SENDMSG */ 114,
431 /* SYS_RECVMSG */ 113
432};
433
434void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000435sparc_socket_decode (tcp)
436struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000437{
438 volatile long addr;
439 volatile int i, n;
440
441 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
442 return;
443 }
444 tcp->scno = socket_map [tcp->u_arg [0]-1];
445 n = tcp->u_nargs = sysent [tcp->scno].nargs;
446 addr = tcp->u_arg [1];
447 for (i = 0; i < n; i++){
448 int arg;
449 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
450 arg = 0;
451 tcp->u_arg [i] = arg;
452 addr += sizeof (arg);
453 }
454}
455
456static void
457decode_subcall(tcp, subcall, nsubcalls, style)
458struct tcb *tcp;
459int subcall;
460int nsubcalls;
461enum subcall_style style;
462{
463 int i, addr, mask, arg;
464
465 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
466 return;
467 switch (style) {
468 case shift_style:
469 tcp->scno = subcall + tcp->u_arg[0];
470 if (sysent[tcp->scno].nargs != -1)
471 tcp->u_nargs = sysent[tcp->scno].nargs;
472 else
473 tcp->u_nargs--;
474 for (i = 0; i < tcp->u_nargs; i++)
475 tcp->u_arg[i] = tcp->u_arg[i + 1];
476 break;
477 case deref_style:
478 tcp->scno = subcall + tcp->u_arg[0];
479 addr = tcp->u_arg[1];
480 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
481 if (umove(tcp, addr, &arg) < 0)
482 arg = 0;
483 tcp->u_arg[i] = arg;
484 addr += sizeof(arg);
485 }
486 tcp->u_nargs = sysent[tcp->scno].nargs;
487 break;
488 case mask_style:
489 mask = (tcp->u_arg[0] >> 8) & 0xff;
490 tcp->u_arg[0] &= 0xff;
491 for (i = 0; mask; i++)
492 mask >>= 1;
493 tcp->scno = subcall + i;
494 if (sysent[tcp->scno].nargs != -1)
495 tcp->u_nargs = sysent[tcp->scno].nargs;
496 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000497 case door_style:
498 /*
499 * Oh, yuck. The call code is the *sixth* argument.
500 */
501 tcp->scno = subcall + tcp->u_arg[5];
502 if (sysent[tcp->scno].nargs != -1)
503 tcp->u_nargs = sysent[tcp->scno].nargs;
504 else
505 tcp->u_nargs--;
506 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 }
508}
509#endif
510
511struct tcb *tcp_last = NULL;
512
513static int
514internal_syscall(tcp)
515struct tcb *tcp;
516{
517 /*
518 * We must always trace a few critical system calls in order to
519 * correctly support following forks in the presence of tracing
520 * qualifiers.
521 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000522 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000523#ifdef SYS_fork
524 case SYS_fork:
525#endif
526#ifdef SYS_vfork
527 case SYS_vfork:
528#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000529 internal_fork(tcp);
530 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000531#ifdef SYS_clone
532 case SYS_clone:
533 internal_clone(tcp);
534 break;
535#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536#ifdef SYS_execv
537 case SYS_execv:
538#endif
539#ifdef SYS_execve
540 case SYS_execve:
541#endif
542 internal_exec(tcp);
543 break;
544
545#ifdef SYS_wait
546 case SYS_wait:
547#endif
548#ifdef SYS_wait4
549 case SYS_wait4:
550#endif
551#ifdef SYS_waitpid
552 case SYS_waitpid:
553#endif
554#ifdef SYS_waitsys
555 case SYS_waitsys:
556#endif
557 internal_wait(tcp);
558 break;
559
560#ifdef SYS_exit
561 case SYS_exit:
562#endif
563 internal_exit(tcp);
564 break;
565 }
566 return 0;
567}
568
569int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000570get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571struct tcb *tcp;
572{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000575
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000577#if defined(S390)
578 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
579 return -1;
580 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
581 if (errno)
582 return -1;
583 scno&=0xFF;
584#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000585 if (upeek(pid, 4*PT_R0, &scno) < 0)
586 return -1;
587 if (!(tcp->flags & TCB_INSYSCALL)) {
588 /* Check if we return from execve. */
589 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
590 tcp->flags &= ~TCB_WAITEXECVE;
591 return 0;
592 }
593 }
594#elif defined (I386)
595 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
596 return -1;
597#elif defined (ARM)
598 {
599 long pc;
600 upeek(pid, 4*15, &pc);
601 umoven(tcp, pc-4, 4, (char *)&scno);
602 scno &= 0x000fffff;
603 }
604#elif defined (M68K)
605 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
606 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000607#elif defined (MIPS)
608 if (upeek(pid, REG_A3, &a3) < 0)
609 return -1;
610
611 if(!(tcp->flags & TCB_INSYSCALL)) {
612 if (upeek(pid, REG_V0, &scno) < 0)
613 return -1;
614
615 if (scno < 0 || scno > nsyscalls) {
616 if(a3 == 0 || a3 == -1) {
617 if(debug)
618 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
619 return 0;
620 }
621 }
622 } else {
623 if (upeek(pid, REG_V0, &r2) < 0)
624 return -1;
625 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000626#elif defined (ALPHA)
627 if (upeek(pid, REG_A3, &a3) < 0)
628 return -1;
629
630 if (!(tcp->flags & TCB_INSYSCALL)) {
631 if (upeek(pid, REG_R0, &scno) < 0)
632 return -1;
633
634 /* Check if we return from execve. */
635 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
636 tcp->flags &= ~TCB_WAITEXECVE;
637 return 0;
638 }
639
640 /*
641 * Do some sanity checks to figure out if it's
642 * really a syscall entry
643 */
644 if (scno < 0 || scno > nsyscalls) {
645 if (a3 == 0 || a3 == -1) {
646 if (debug)
647 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
648 return 0;
649 }
650 }
651 }
652 else {
653 if (upeek(pid, REG_R0, &r0) < 0)
654 return -1;
655 }
656#elif defined (SPARC)
657 /* Everything we need is in the current register set. */
658 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
659 return -1;
660
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000661 /* If we are entering, then disassemble the syscall trap. */
662 if (!(tcp->flags & TCB_INSYSCALL)) {
663 /* Retrieve the syscall trap instruction. */
664 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000665 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000666 if (errno)
667 return -1;
668
669 /* Disassemble the trap to see what personality to use. */
670 switch (trap) {
671 case 0x91d02010:
672 /* Linux/SPARC syscall trap. */
673 set_personality(0);
674 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000675 case 0x91d0206d:
676 /* Linux/SPARC64 syscall trap. */
677 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
678 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000679 case 0x91d02000:
680 /* SunOS syscall trap. (pers 1) */
681 fprintf(stderr,"syscall: SunOS no support\n");
682 return -1;
683 case 0x91d02008:
684 /* Solaris 2.x syscall trap. (per 2) */
685 set_personality(1);
686 break;
687 case 0x91d02009:
688 /* NetBSD/FreeBSD syscall trap. */
689 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
690 return -1;
691 case 0x91d02027:
692 /* Solaris 2.x gettimeofday */
693 set_personality(1);
694 break;
695 default:
696 /* Unknown syscall trap. */
697 if(tcp->flags & TCB_WAITEXECVE) {
698 tcp->flags &= ~TCB_WAITEXECVE;
699 return 0;
700 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000701 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000702 return -1;
703 }
704
705 /* Extract the system call number from the registers. */
706 if (trap == 0x91d02027)
707 scno = 156;
708 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000709 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000710 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000711 scno = regs.r_o0;
712 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000713 }
714 }
715#endif
716#endif /* LINUX */
717#ifdef SUNOS4
718 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
719 return -1;
720#endif
721#ifdef SVR4
722#ifdef HAVE_PR_SYSCALL
723 scno = tcp->status.pr_syscall;
724#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000725 scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726#endif /* !HAVE_PR_SYSCALL */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000727#endif
728 tcp->scno = scno;
729 return 1;
730}
731
732#ifdef LINUX
733#if defined (I386)
734 static long eax;
735#elif defined (POWERPC)
736 static long result,flags;
737#elif defined (M68K)
738 static int d0;
739#elif defined (ARM)
740 static int r0;
741#elif defined (ALPHA)
742 static long r0;
743 static long a3;
744#elif defined (SPARC)
745 static struct pt_regs regs;
746 static unsigned long trap;
747#elif defined(S390)
748 static long gpr2;
749 static long pc;
750#endif
751#endif /* LINUX */
752
753int
754syscall_fixup(tcp)
755struct tcb *tcp;
756{
757 int pid = tcp->pid;
758
759#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000760 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000761 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000762 if (
763 scno == SYS_fork
764#ifdef SYS_vfork
765 || scno == SYS_vfork
766#endif /* SYS_vfork */
767 ) {
768 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000769 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000770 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000771 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000772 }
773 else {
774 fprintf(stderr, "syscall: missing entry\n");
775 tcp->flags |= TCB_INSYSCALL;
776 }
777 }
778 }
779 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000780 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000781 fprintf(stderr, "syscall: missing exit\n");
782 tcp->flags &= ~TCB_INSYSCALL;
783 }
784 }
785#endif /* SVR4 */
786#ifdef SUNOS4
787 if (!(tcp->flags & TCB_INSYSCALL)) {
788 if (scno == 0) {
789 fprintf(stderr, "syscall: missing entry\n");
790 tcp->flags |= TCB_INSYSCALL;
791 }
792 }
793 else {
794 if (scno != 0) {
795 if (debug) {
796 /*
797 * This happens when a signal handler
798 * for a signal which interrupted a
799 * a system call makes another system call.
800 */
801 fprintf(stderr, "syscall: missing exit\n");
802 }
803 tcp->flags &= ~TCB_INSYSCALL;
804 }
805 }
806#endif /* SUNOS4 */
807#ifdef LINUX
808#if defined (I386)
809 if (upeek(pid, 4*EAX, &eax) < 0)
810 return -1;
811 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
812 if (debug)
813 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
814 return 0;
815 }
816#elif defined (POWERPC)
817# define SO_MASK 0x10000000
818 if (upeek(pid, 4*PT_CCR, &flags) < 0)
819 return -1;
820 if (upeek(pid, 4*PT_R3, &result) < 0)
821 return -1;
822 if (flags & SO_MASK)
823 result = -result;
824#elif defined (M68K)
825 if (upeek(pid, 4*PT_D0, &d0) < 0)
826 return -1;
827 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
828 if (debug)
829 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
830 return 0;
831 }
832#elif defined (ARM)
833 if (upeek(pid, 4*0, (long *)&r0) < 0)
834 return -1;
835 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
836 if (debug)
837 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
838 return 0;
839 }
840#else
841#endif
842#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000843 return 1;
844}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000845
Pavel Machek4dc3b142000-02-01 17:58:41 +0000846int
847get_error(tcp)
848struct tcb *tcp;
849{
850 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000851#ifdef LINUX
852#ifdef I386
853 if (eax < 0 && -eax < nerrnos) {
854 tcp->u_rval = -1;
855 u_error = -eax;
856 }
857 else {
858 tcp->u_rval = eax;
859 u_error = 0;
860 }
861#else /* !I386 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000862#ifdef MIPS
863 if (a3) {
864 tcp->u_rval = -1;
865 u_error = r2;
866 } else {
867 tcp->u_rval = r2;
868 u_error = 0;
869 }
870#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000871#ifdef POWERPC
872 if (result && (unsigned) -result < nerrnos) {
873 tcp->u_rval = -1;
874 u_error = -result;
875 }
876 else {
877 tcp->u_rval = result;
878 u_error = 0;
879 }
880#else /* !POWERPC */
881#ifdef M68K
882 if (d0 && (unsigned) -d0 < nerrnos) {
883 tcp->u_rval = -1;
884 u_error = -d0;
885 }
886 else {
887 tcp->u_rval = d0;
888 u_error = 0;
889 }
890#else /* !M68K */
891#ifdef ARM
892 if (r0 && (unsigned) -r0 < nerrnos) {
893 tcp->u_rval = -1;
894 u_error = -r0;
895 }
896 else {
897 tcp->u_rval = r0;
898 u_error = 0;
899 }
900#else /* !ARM */
901#ifdef ALPHA
902 if (a3) {
903 tcp->u_rval = -1;
904 u_error = r0;
905 }
906 else {
907 tcp->u_rval = r0;
908 u_error = 0;
909 }
910#else /* !ALPHA */
911#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000912 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000913 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000914 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000915 }
916 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000917 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000918 u_error = 0;
919 }
920#endif /* SPARC */
921#endif /* ALPHA */
922#endif /* ARM */
923#endif /* M68K */
924#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000925#endif /* MIPS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000926#endif /* I386 */
927#endif /* LINUX */
928#ifdef SUNOS4
929 /* get error code from user struct */
930 if (upeek(pid, uoff(u_error), &u_error) < 0)
931 return -1;
932 u_error >>= 24; /* u_error is a char */
933
934 /* get system call return value */
935 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
936 return -1;
937#endif /* SUNOS4 */
938#ifdef SVR4
939#ifdef SPARC
940 /* Judicious guessing goes a long way. */
941 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
942 tcp->u_rval = -1;
943 u_error = tcp->status.pr_reg[R_O0];
944 }
945 else {
946 tcp->u_rval = tcp->status.pr_reg[R_O0];
947 u_error = 0;
948 }
949#endif /* SPARC */
950#ifdef I386
951 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000952 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000953 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000954 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000955 }
956 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000957 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000958 u_error = 0;
959 }
960#endif /* I386 */
961#ifdef MIPS
962 if (tcp->status.pr_reg[CTX_A3]) {
963 tcp->u_rval = -1;
964 u_error = tcp->status.pr_reg[CTX_V0];
965 }
966 else {
967 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
968 u_error = 0;
969 }
970#endif /* MIPS */
971#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000972 tcp->u_error = u_error;
973 return 1;
974}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000975
Pavel Machek4dc3b142000-02-01 17:58:41 +0000976int syscall_enter(tcp)
977struct tcb *tcp;
978{
979 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000980#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000981#if defined(S390)
982 {
983 int i;
984 tcp->u_nargs = sysent[tcp->scno].nargs;
985 for (i = 0; i < tcp->u_nargs; i++) {
986 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
987 return -1;
988 }
989 }
990#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000991 {
992 int i;
993 tcp->u_nargs = sysent[tcp->scno].nargs;
994 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +0000995 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
996 * for scno somewhere above here!
997 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000998 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
999 return -1;
1000 }
1001 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001002#elif defined (MIPS)
1003 {
1004 long sp;
1005 int i, nargs;
1006
1007 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1008 if(nargs > 4) {
1009 if(upeek(pid, REG_SP, &sp) < 0)
1010 return -1;
1011 for(i = 0; i < 4; i++) {
1012 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1013 return -1;
1014 }
1015 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1016 (char *)(tcp->u_arg + 4));
1017 } else {
1018 for(i = 0; i < nargs; i++) {
1019 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1020 return -1;
1021 }
1022 }
1023 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001024#elif defined (POWERPC)
1025 {
1026 int i;
1027 tcp->u_nargs = sysent[tcp->scno].nargs;
1028 for (i = 0; i < tcp->u_nargs; i++) {
1029 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1030 return -1;
1031 }
1032 }
1033#elif defined (SPARC)
1034 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001035 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001036
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001037 tcp->u_nargs = sysent[tcp->scno].nargs;
1038 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001039 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001040 }
1041#else
1042 {
1043 int i;
1044 tcp->u_nargs = sysent[tcp->scno].nargs;
1045 for (i = 0; i < tcp->u_nargs; i++) {
1046 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1047 return -1;
1048 }
1049 }
1050#endif
1051#endif /* LINUX */
1052#ifdef SUNOS4
1053 {
1054 int i;
1055 tcp->u_nargs = sysent[tcp->scno].nargs;
1056 for (i = 0; i < tcp->u_nargs; i++) {
1057 struct user *u;
1058
1059 if (upeek(pid, uoff(u_arg[0]) +
1060 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1061 return -1;
1062 }
1063 }
1064#endif /* SUNOS4 */
1065#ifdef SVR4
1066#ifdef MIPS
1067 /*
1068 * SGI is broken: even though it has pr_sysarg, it doesn't
1069 * set them on system call entry. Get a clue.
1070 */
1071 if (sysent[tcp->scno].nargs != -1)
1072 tcp->u_nargs = sysent[tcp->scno].nargs;
1073 else
1074 tcp->u_nargs = tcp->status.pr_nsysarg;
1075 if (tcp->u_nargs > 4) {
1076 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1077 4*sizeof(tcp->u_arg[0]));
1078 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1079 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1080 }
1081 else {
1082 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1083 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1084 }
1085#else /* !MIPS */
1086#ifdef HAVE_PR_SYSCALL
1087 if (sysent[tcp->scno].nargs != -1)
1088 tcp->u_nargs = sysent[tcp->scno].nargs;
1089 else
1090 tcp->u_nargs = tcp->status.pr_nsysarg;
1091 {
1092 int i;
1093 for (i = 0; i < tcp->u_nargs; i++)
1094 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1095 }
1096#else /* !HAVE_PR_SYSCALL */
1097#ifdef I386
1098 if (sysent[tcp->scno].nargs != -1)
1099 tcp->u_nargs = sysent[tcp->scno].nargs;
1100 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001101#if UNIXWARE >= 2
1102 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1103#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001104 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001105#endif
1106 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001107 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1108#endif /* I386 */
1109#endif /* !HAVE_PR_SYSCALL */
1110#endif /* !MIPS */
1111#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001112 return 1;
1113}
1114
1115int
1116trace_syscall(tcp)
1117struct tcb *tcp;
1118{
1119 int sys_res;
1120 struct timeval tv;
1121 int res;
1122
1123 /* Measure the exit time as early as possible to avoid errors. */
1124 if (dtime && (tcp->flags & TCB_INSYSCALL))
1125 gettimeofday(&tv, NULL);
1126
1127 res = get_scno(tcp);
1128 if (res != 1)
1129 return res;
1130
1131 res = syscall_fixup(tcp);
1132 if (res != 1)
1133 return res;
1134
1135 if (tcp->flags & TCB_INSYSCALL) {
1136 long u_error;
1137 res = get_error(tcp);
1138 if (res != 1)
1139 return res;
1140 u_error = tcp->u_error;
1141
1142
1143 internal_syscall(tcp);
1144 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1145 tcp->flags &= ~TCB_INSYSCALL;
1146 return 0;
1147 }
1148
1149 if (tcp->flags & TCB_REPRINT) {
1150 printleader(tcp);
1151 tprintf("<... ");
1152 if (tcp->scno >= nsyscalls)
1153 tprintf("syscall_%lu", tcp->scno);
1154 else
1155 tprintf("%s", sysent[tcp->scno].sys_name);
1156 tprintf(" resumed> ");
1157 }
1158
1159 if (cflag) {
1160 call_count[tcp->scno]++;
1161 if (tcp->u_error)
1162 error_count[tcp->scno]++;
1163 tv_sub(&tv, &tv, &tcp->etime);
1164#ifdef LINUX
1165 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1166 static struct timeval one_tick =
1167 { 0, 1000000 / HZ };
1168
1169 if (tv_nz(&tcp->dtime))
1170 tv = tcp->dtime;
1171 else if (tv_cmp(&tv, &one_tick) > 0) {
1172 if (tv_cmp(&shortest, &one_tick) < 0)
1173 tv = shortest;
1174 else
1175 tv = one_tick;
1176 }
1177 }
1178#endif /* LINUX */
1179 if (tv_cmp(&tv, &shortest) < 0)
1180 shortest = tv;
1181 tv_add(&tv_count[tcp->scno],
1182 &tv_count[tcp->scno], &tv);
1183 tcp->flags &= ~TCB_INSYSCALL;
1184 return 0;
1185 }
1186
1187 if (tcp->scno >= nsyscalls
1188 || (qual_flags[tcp->scno] & QUAL_RAW))
1189 sys_res = printargs(tcp);
1190 else
1191 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1192 u_error = tcp->u_error;
1193 tprintf(") ");
1194 tabto(acolumn);
1195 if (qual_flags[tcp->scno] & QUAL_RAW) {
1196 if (u_error)
1197 tprintf("= -1 (errno %ld)", u_error);
1198 else
1199 tprintf("= %#lx", tcp->u_rval);
1200 }
1201 else if (!(sys_res & RVAL_NONE) && u_error) {
1202 switch (u_error) {
1203#ifdef LINUX
1204 case ERESTARTSYS:
1205 tprintf("= ? ERESTARTSYS (To be restarted)");
1206 break;
1207 case ERESTARTNOINTR:
1208 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1209 break;
1210 case ERESTARTNOHAND:
1211 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1212 break;
1213#endif /* LINUX */
1214 default:
1215 tprintf("= -1 ");
1216 if (u_error < nerrnos && u_error < sys_nerr)
1217 tprintf("%s (%s)", errnoent[u_error],
1218 sys_errlist[u_error]);
1219 else if (u_error < nerrnos)
1220 tprintf("%s (errno %ld)",
1221 errnoent[u_error], u_error);
1222 else if (u_error < sys_nerr)
1223 tprintf("ERRNO_%ld (%s)", u_error,
1224 sys_errlist[u_error]);
1225 else
1226 tprintf("E??? (errno %ld)", u_error);
1227 break;
1228 }
1229 }
1230 else {
1231 if (sys_res & RVAL_NONE)
1232 tprintf("= ?");
1233 else {
1234 switch (sys_res & RVAL_MASK) {
1235 case RVAL_HEX:
1236 tprintf("= %#lx", tcp->u_rval);
1237 break;
1238 case RVAL_OCTAL:
1239 tprintf("= %#lo", tcp->u_rval);
1240 break;
1241 case RVAL_UDECIMAL:
1242 tprintf("= %lu", tcp->u_rval);
1243 break;
1244 case RVAL_DECIMAL:
1245 tprintf("= %ld", tcp->u_rval);
1246 break;
1247 default:
1248 fprintf(stderr,
1249 "invalid rval format\n");
1250 break;
1251 }
1252 }
1253 if ((sys_res & RVAL_STR) && tcp->auxstr)
1254 tprintf(" (%s)", tcp->auxstr);
1255 }
1256 if (dtime) {
1257 tv_sub(&tv, &tv, &tcp->etime);
1258 tprintf(" <%ld.%06ld>",
1259 (long) tv.tv_sec, (long) tv.tv_usec);
1260 }
1261 printtrailer(tcp);
1262
1263 dumpio(tcp);
1264 if (fflush(tcp->outf) == EOF)
1265 return -1;
1266 tcp->flags &= ~TCB_INSYSCALL;
1267 return 0;
1268 }
1269
1270 /* Entering system call */
1271 res = syscall_enter(tcp);
1272 if (res != 1)
1273 return res;
1274
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001275 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001276#ifdef LINUX
Wichert Akkermanf90da011999-10-31 21:15:38 +00001277#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001278 case SYS_socketcall:
1279 decode_subcall(tcp, SYS_socket_subcall,
1280 SYS_socket_nsubcalls, deref_style);
1281 break;
1282 case SYS_ipc:
1283 decode_subcall(tcp, SYS_ipc_subcall,
1284 SYS_ipc_nsubcalls, shift_style);
1285 break;
1286#endif /* !ALPHA && !SPARC */
1287#ifdef SPARC
1288 case SYS_socketcall:
1289 sparc_socket_decode (tcp);
1290 break;
1291#endif
1292#endif /* LINUX */
1293#ifdef SVR4
1294#ifdef SYS_pgrpsys_subcall
1295 case SYS_pgrpsys:
1296 decode_subcall(tcp, SYS_pgrpsys_subcall,
1297 SYS_pgrpsys_nsubcalls, shift_style);
1298 break;
1299#endif /* SYS_pgrpsys_subcall */
1300#ifdef SYS_sigcall_subcall
1301 case SYS_sigcall:
1302 decode_subcall(tcp, SYS_sigcall_subcall,
1303 SYS_sigcall_nsubcalls, mask_style);
1304 break;
1305#endif /* SYS_sigcall_subcall */
1306 case SYS_msgsys:
1307 decode_subcall(tcp, SYS_msgsys_subcall,
1308 SYS_msgsys_nsubcalls, shift_style);
1309 break;
1310 case SYS_shmsys:
1311 decode_subcall(tcp, SYS_shmsys_subcall,
1312 SYS_shmsys_nsubcalls, shift_style);
1313 break;
1314 case SYS_semsys:
1315 decode_subcall(tcp, SYS_semsys_subcall,
1316 SYS_semsys_nsubcalls, shift_style);
1317 break;
1318#if 0 /* broken */
1319 case SYS_utssys:
1320 decode_subcall(tcp, SYS_utssys_subcall,
1321 SYS_utssys_nsubcalls, shift_style);
1322 break;
1323#endif
1324 case SYS_sysfs:
1325 decode_subcall(tcp, SYS_sysfs_subcall,
1326 SYS_sysfs_nsubcalls, shift_style);
1327 break;
1328 case SYS_spcall:
1329 decode_subcall(tcp, SYS_spcall_subcall,
1330 SYS_spcall_nsubcalls, shift_style);
1331 break;
1332#ifdef SYS_context_subcall
1333 case SYS_context:
1334 decode_subcall(tcp, SYS_context_subcall,
1335 SYS_context_nsubcalls, shift_style);
1336 break;
1337#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001338#ifdef SYS_door_subcall
1339 case SYS_door:
1340 decode_subcall(tcp, SYS_door_subcall,
1341 SYS_door_nsubcalls, door_style);
1342 break;
1343#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001344#endif /* SVR4 */
1345#ifdef SUNOS4
1346 case SYS_semsys:
1347 decode_subcall(tcp, SYS_semsys_subcall,
1348 SYS_semsys_nsubcalls, shift_style);
1349 break;
1350 case SYS_msgsys:
1351 decode_subcall(tcp, SYS_msgsys_subcall,
1352 SYS_msgsys_nsubcalls, shift_style);
1353 break;
1354 case SYS_shmsys:
1355 decode_subcall(tcp, SYS_shmsys_subcall,
1356 SYS_shmsys_nsubcalls, shift_style);
1357 break;
1358#endif
1359 }
1360
1361 internal_syscall(tcp);
1362 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1363 tcp->flags |= TCB_INSYSCALL;
1364 return 0;
1365 }
1366
1367 if (cflag) {
1368 gettimeofday(&tcp->etime, NULL);
1369 tcp->flags |= TCB_INSYSCALL;
1370 return 0;
1371 }
1372
1373 printleader(tcp);
1374 tcp->flags &= ~TCB_REPRINT;
1375 tcp_last = tcp;
1376 if (tcp->scno >= nsyscalls)
1377 tprintf("syscall_%lu(", tcp->scno);
1378 else
1379 tprintf("%s(", sysent[tcp->scno].sys_name);
1380 if (tcp->scno >= nsyscalls ||
1381 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1382 sys_res = printargs(tcp);
1383 else
1384 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1385 if (fflush(tcp->outf) == EOF)
1386 return -1;
1387 tcp->flags |= TCB_INSYSCALL;
1388 /* Measure the entrance time as late as possible to avoid errors. */
1389 if (dtime)
1390 gettimeofday(&tcp->etime, NULL);
1391 return sys_res;
1392}
1393
1394int
1395printargs(tcp)
1396struct tcb *tcp;
1397{
1398 if (entering(tcp)) {
1399 int i;
1400
1401 for (i = 0; i < tcp->u_nargs; i++)
1402 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1403 }
1404 return 0;
1405}
1406
1407long
1408getrval2(tcp)
1409struct tcb *tcp;
1410{
1411 long val = -1;
1412
1413#ifdef LINUX
1414#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001415 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001416 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1417 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001418 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001419#endif /* SPARC */
1420#endif /* LINUX */
1421
1422#ifdef SUNOS4
1423 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1424 return -1;
1425#endif /* SUNOS4 */
1426
1427#ifdef SVR4
1428#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001429 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001430#endif /* SPARC */
1431#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001432 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001433#endif /* I386 */
1434#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001435 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001436#endif /* MIPS */
1437#endif /* SVR4 */
1438
1439 return val;
1440}
1441
1442/*
1443 * Apparently, indirect system calls have already be converted by ptrace(2),
1444 * so if you see "indir" this program has gone astray.
1445 */
1446int
1447sys_indir(tcp)
1448struct tcb *tcp;
1449{
1450 int i, scno, nargs;
1451
1452 if (entering(tcp)) {
1453 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1454 fprintf(stderr, "Bogus syscall: %u\n", scno);
1455 return 0;
1456 }
1457 nargs = sysent[scno].nargs;
1458 tprintf("%s", sysent[scno].sys_name);
1459 for (i = 0; i < nargs; i++)
1460 tprintf(", %#lx", tcp->u_arg[i+1]);
1461 }
1462 return 0;
1463}
1464
1465static int
1466time_cmp(a, b)
1467void *a;
1468void *b;
1469{
1470 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1471}
1472
1473static int
1474syscall_cmp(a, b)
1475void *a;
1476void *b;
1477{
1478 return strcmp(sysent[*((int *) a)].sys_name,
1479 sysent[*((int *) b)].sys_name);
1480}
1481
1482static int
1483count_cmp(a, b)
1484void *a;
1485void *b;
1486{
1487 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1488
1489 return (m < n) ? 1 : (m > n) ? -1 : 0;
1490}
1491
1492static int (*sortfun)();
1493static struct timeval overhead = { -1, -1 };
1494
1495void
1496set_sortby(sortby)
1497char *sortby;
1498{
1499 if (strcmp(sortby, "time") == 0)
1500 sortfun = time_cmp;
1501 else if (strcmp(sortby, "calls") == 0)
1502 sortfun = count_cmp;
1503 else if (strcmp(sortby, "name") == 0)
1504 sortfun = syscall_cmp;
1505 else if (strcmp(sortby, "nothing") == 0)
1506 sortfun = NULL;
1507 else {
1508 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1509 exit(1);
1510 }
1511}
1512
1513void set_overhead(n)
1514int n;
1515{
1516 overhead.tv_sec = n / 1000000;
1517 overhead.tv_usec = n % 1000000;
1518}
1519
1520void
1521call_summary(outf)
1522FILE *outf;
1523{
1524 int i, j;
1525 int call_cum, error_cum;
1526 struct timeval tv_cum, dtv;
1527 double percent;
1528 char *dashes = "-------------------------";
1529 char error_str[16];
1530
1531 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1532 if (overhead.tv_sec == -1) {
1533 tv_mul(&overhead, &shortest, 8);
1534 tv_div(&overhead, &overhead, 10);
1535 }
1536 for (i = 0; i < nsyscalls; i++) {
1537 sorted_count[i] = i;
1538 if (call_count[i] == 0)
1539 continue;
1540 tv_mul(&dtv, &overhead, call_count[i]);
1541 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1542 call_cum += call_count[i];
1543 error_cum += error_count[i];
1544 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1545 }
1546 if (sortfun)
1547 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1548 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1549 "% time", "seconds", "usecs/call",
1550 "calls", "errors", "syscall");
1551 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1552 dashes, dashes, dashes, dashes, dashes, dashes);
1553 for (i = 0; i < nsyscalls; i++) {
1554 j = sorted_count[i];
1555 if (call_count[j] == 0)
1556 continue;
1557 tv_div(&dtv, &tv_count[j], call_count[j]);
1558 if (error_count[j])
1559 sprintf(error_str, "%d", error_count[j]);
1560 else
1561 error_str[0] = '\0';
1562 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1563 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1564 percent, (long) tv_count[j].tv_sec,
1565 (long) tv_count[j].tv_usec,
1566 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1567 call_count[j], error_str, sysent[j].sys_name);
1568 }
1569 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1570 dashes, dashes, dashes, dashes, dashes, dashes);
1571 if (error_cum)
1572 sprintf(error_str, "%d", error_cum);
1573 else
1574 error_str[0] = '\0';
1575 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1576 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1577 call_cum, error_str, "total");
1578}