blob: 7c31fc0b9316d794f88a238ff515c999d818c627 [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>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $Id$
30 */
31
32#include "defs.h"
33
34#include <signal.h>
35#include <time.h>
36#include <errno.h>
37#include <sys/user.h>
38#include <sys/syscall.h>
39#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000040
Wichert Akkerman15dea971999-10-06 13:06:34 +000041#if HAVE_ASM_REG_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000042#include <asm/reg.h>
43#endif
44
Wichert Akkerman15dea971999-10-06 13:06:34 +000045#if HAVE_LINUX_PTRACE_H
46#undef PTRACE_SYSCALL
Wichert Akkermanb046b381999-07-13 22:20:16 +000047#include <linux/ptrace.h>
48#endif
49
Wichert Akkerman15dea971999-10-06 13:06:34 +000050#ifdef HAVE_SYS_REG_H
51#include <sys/reg.h>
52#ifndef PTRACE_PEEKUSR
53# define PTRACE_PEEKUSR PTRACE_PEEKUSER
54#endif
55#endif
56
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000057#ifndef SYS_ERRLIST_DECLARED
58extern int sys_nerr;
59extern char *sys_errlist[];
60#endif /* SYS_ERRLIST_DECLARED */
61
62#ifdef LINUX
63#ifndef ERESTARTSYS
64#define ERESTARTSYS 512
65#endif
66#ifndef ERESTARTNOINTR
67#define ERESTARTNOINTR 513
68#endif
69#ifndef ERESTARTNOHAND
70#define ERESTARTNOHAND 514 /* restart if no handler.. */
71#endif
72#ifndef ENOIOCTLCMD
73#define ENOIOCTLCMD 515 /* No ioctl command */
74#endif
75#ifndef NSIG
76#define NSIG 32
77#endif
78#ifdef ARM
79#undef NSIG
80#define NSIG 32
81#endif
82#endif /* LINUX */
83
84#include "syscall.h"
85
86/* Define these shorthand notations to simplify the syscallent files. */
87#define TF TRACE_FILE
88#define TI TRACE_IPC
89#define TN TRACE_NETWORK
90#define TP TRACE_PROCESS
91#define TS TRACE_SIGNAL
92
93struct sysent sysent0[] = {
94#include "syscallent.h"
95};
96int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
97
98#if SUPPORTED_PERSONALITIES >= 2
99struct sysent sysent1[] = {
100#include "syscallent1.h"
101};
102int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
103#endif /* SUPPORTED_PERSONALITIES >= 2 */
104
105#if SUPPORTED_PERSONALITIES >= 3
106struct sysent sysent2[] = {
107#include "syscallent2.h"
108};
109int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
110#endif /* SUPPORTED_PERSONALITIES >= 3 */
111
112struct sysent *sysent;
113int nsyscalls;
114
115/* Now undef them since short defines cause wicked namespace pollution. */
116#undef TF
117#undef TI
118#undef TN
119#undef TP
120#undef TS
121
122char *errnoent0[] = {
123#include "errnoent.h"
124};
125int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
126
127#if SUPPORTED_PERSONALITIES >= 2
128char *errnoent1[] = {
129#include "errnoent1.h"
130};
131int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
132#endif /* SUPPORTED_PERSONALITIES >= 2 */
133
134#if SUPPORTED_PERSONALITIES >= 3
135char *errnoent2[] = {
136#include "errnoent2.h"
137};
138int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
139#endif /* SUPPORTED_PERSONALITIES >= 3 */
140
141char **errnoent;
142int nerrnos;
143
144int current_personality;
145
146int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000147set_personality(personality)
148int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000149{
150 switch (personality) {
151 case 0:
152 errnoent = errnoent0;
153 nerrnos = nerrnos0;
154 sysent = sysent0;
155 nsyscalls = nsyscalls0;
156 ioctlent = ioctlent0;
157 nioctlents = nioctlents0;
158 signalent = signalent0;
159 nsignals = nsignals0;
160 break;
161
162#if SUPPORTED_PERSONALITIES >= 2
163 case 1:
164 errnoent = errnoent1;
165 nerrnos = nerrnos1;
166 sysent = sysent1;
167 nsyscalls = nsyscalls1;
168 ioctlent = ioctlent1;
169 nioctlents = nioctlents1;
170 signalent = signalent1;
171 nsignals = nsignals1;
172 break;
173#endif /* SUPPORTED_PERSONALITIES >= 2 */
174
175#if SUPPORTED_PERSONALITIES >= 3
176 case 2:
177 errnoent = errnoent2;
178 nerrnos = nerrnos2;
179 sysent = sysent2;
180 nsyscalls = nsyscalls2;
181 ioctlent = ioctlent2;
182 nioctlents = nioctlents2;
183 signalent = signalent2;
184 nsignals = nsignals2;
185 break;
186#endif /* SUPPORTED_PERSONALITIES >= 3 */
187
188 default:
189 return -1;
190 }
191
192 current_personality = personality;
193 return 0;
194}
195
196int qual_flags[MAX_QUALS];
197
198static int call_count[MAX_QUALS];
199static int error_count[MAX_QUALS];
200static struct timeval tv_count[MAX_QUALS];
201static int sorted_count[MAX_QUALS];
202
203static struct timeval shortest = { 1000000, 0 };
204
205static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
206
207static struct qual_options {
208 int bitflag;
209 char *option_name;
210 int (*lookup)();
211 char *argument_name;
212} qual_options[] = {
213 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
214 { QUAL_TRACE, "t", lookup_syscall, "system call" },
215 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
216 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
217 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
218 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
219 { QUAL_RAW, "raw", lookup_syscall, "system call" },
220 { QUAL_RAW, "x", lookup_syscall, "system call" },
221 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
222 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
223 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
224 { QUAL_FAULT, "fault", lookup_fault, "fault" },
225 { QUAL_FAULT, "faults", lookup_fault, "fault" },
226 { QUAL_FAULT, "m", lookup_fault, "fault" },
227 { QUAL_READ, "read", lookup_desc, "descriptor" },
228 { QUAL_READ, "reads", lookup_desc, "descriptor" },
229 { QUAL_READ, "r", lookup_desc, "descriptor" },
230 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
231 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
232 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
233 { 0, NULL, NULL, NULL },
234};
235
236static int
237lookup_syscall(s)
238char *s;
239{
240 int i;
241
242 for (i = 0; i < nsyscalls; i++) {
243 if (strcmp(s, sysent[i].sys_name) == 0)
244 return i;
245 }
246 return -1;
247}
248
249static int
250lookup_signal(s)
251char *s;
252{
253 int i;
254 char buf[32];
255
256 if (s && *s && isdigit(*s))
257 return atoi(s);
258 strcpy(buf, s);
259 s = buf;
260 for (i = 0; s[i]; i++)
261 s[i] = toupper(s[i]);
262 if (strncmp(s, "SIG", 3) == 0)
263 s += 3;
264 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000265 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000266 return i;
267 }
268 return -1;
269}
270
271static int
272lookup_fault(s)
273char *s;
274{
275 return -1;
276}
277
278static int
279lookup_desc(s)
280char *s;
281{
282 if (s && *s && isdigit(*s))
283 return atoi(s);
284 return -1;
285}
286
287static int
288lookup_class(s)
289char *s;
290{
291 if (strcmp(s, "file") == 0)
292 return TRACE_FILE;
293 if (strcmp(s, "ipc") == 0)
294 return TRACE_IPC;
295 if (strcmp(s, "network") == 0)
296 return TRACE_NETWORK;
297 if (strcmp(s, "process") == 0)
298 return TRACE_PROCESS;
299 if (strcmp(s, "signal") == 0)
300 return TRACE_SIGNAL;
301 return -1;
302}
303
304void
305qualify(s)
306char *s;
307{
308 struct qual_options *opt;
309 int not;
310 char *p;
311 int i, n;
312
313 opt = &qual_options[0];
314 for (i = 0; (p = qual_options[i].option_name); i++) {
315 n = strlen(p);
316 if (strncmp(s, p, n) == 0 && s[n] == '=') {
317 opt = &qual_options[i];
318 s += n + 1;
319 break;
320 }
321 }
322 not = 0;
323 if (*s == '!') {
324 not = 1;
325 s++;
326 }
327 if (strcmp(s, "none") == 0) {
328 not = 1 - not;
329 s = "all";
330 }
331 if (strcmp(s, "all") == 0) {
332 for (i = 0; i < MAX_QUALS; i++) {
333 if (not)
334 qual_flags[i] &= ~opt->bitflag;
335 else
336 qual_flags[i] |= opt->bitflag;
337 }
338 return;
339 }
340 for (i = 0; i < MAX_QUALS; i++) {
341 if (not)
342 qual_flags[i] |= opt->bitflag;
343 else
344 qual_flags[i] &= ~opt->bitflag;
345 }
346 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
347 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
348 for (i = 0; i < MAX_QUALS; i++) {
349 if (sysent[i].sys_flags & n) {
350 if (not)
351 qual_flags[i] &= ~opt->bitflag;
352 else
353 qual_flags[i] |= opt->bitflag;
354 }
355 }
356 continue;
357 }
358 if ((n = (*opt->lookup)(p)) < 0) {
359 fprintf(stderr, "strace: invalid %s `%s'\n",
360 opt->argument_name, p);
361 exit(1);
362 }
363 if (not)
364 qual_flags[n] &= ~opt->bitflag;
365 else
366 qual_flags[n] |= opt->bitflag;
367 }
368 return;
369}
370
371static void
372dumpio(tcp)
373struct tcb *tcp;
374{
375 if (syserror(tcp))
376 return;
377 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
378 return;
379#ifdef __arm__
380 switch (tcp->scno + __NR_SYSCALL_BASE) {
381#else
382 switch (tcp->scno) {
383#endif
384 case SYS_read:
385#ifdef SYS_recv
386 case SYS_recv:
387#endif
388#ifdef SYS_recvfrom
389 case SYS_recvfrom:
390#endif
391 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
392 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
393 break;
394 case SYS_write:
395#ifdef SYS_send
396 case SYS_send:
397#endif
398#ifdef SYS_sendto
399 case SYS_sendto:
400#endif
401 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
402 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
403 break;
404 }
405}
406
Wichert Akkerman8829a551999-06-11 13:18:40 +0000407enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000408
409#if !(defined(LINUX) && defined(ALPHA))
410
411const int socket_map [] = {
412 /* SYS_SOCKET */ 97,
413 /* SYS_BIND */ 104,
414 /* SYS_CONNECT */ 98,
415 /* SYS_LISTEN */ 106,
416 /* SYS_ACCEPT */ 99,
417 /* SYS_GETSOCKNAME */ 150,
418 /* SYS_GETPEERNAME */ 141,
419 /* SYS_SOCKETPAIR */ 135,
420 /* SYS_SEND */ 101,
421 /* SYS_RECV */ 102,
422 /* SYS_SENDTO */ 133,
423 /* SYS_RECVFROM */ 125,
424 /* SYS_SHUTDOWN */ 134,
425 /* SYS_SETSOCKOPT */ 105,
426 /* SYS_GETSOCKOPT */ 118,
427 /* SYS_SENDMSG */ 114,
428 /* SYS_RECVMSG */ 113
429};
430
431void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000432sparc_socket_decode (tcp)
433struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000434{
435 volatile long addr;
436 volatile int i, n;
437
438 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
439 return;
440 }
441 tcp->scno = socket_map [tcp->u_arg [0]-1];
442 n = tcp->u_nargs = sysent [tcp->scno].nargs;
443 addr = tcp->u_arg [1];
444 for (i = 0; i < n; i++){
445 int arg;
446 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
447 arg = 0;
448 tcp->u_arg [i] = arg;
449 addr += sizeof (arg);
450 }
451}
452
453static void
454decode_subcall(tcp, subcall, nsubcalls, style)
455struct tcb *tcp;
456int subcall;
457int nsubcalls;
458enum subcall_style style;
459{
460 int i, addr, mask, arg;
461
462 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
463 return;
464 switch (style) {
465 case shift_style:
466 tcp->scno = subcall + tcp->u_arg[0];
467 if (sysent[tcp->scno].nargs != -1)
468 tcp->u_nargs = sysent[tcp->scno].nargs;
469 else
470 tcp->u_nargs--;
471 for (i = 0; i < tcp->u_nargs; i++)
472 tcp->u_arg[i] = tcp->u_arg[i + 1];
473 break;
474 case deref_style:
475 tcp->scno = subcall + tcp->u_arg[0];
476 addr = tcp->u_arg[1];
477 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
478 if (umove(tcp, addr, &arg) < 0)
479 arg = 0;
480 tcp->u_arg[i] = arg;
481 addr += sizeof(arg);
482 }
483 tcp->u_nargs = sysent[tcp->scno].nargs;
484 break;
485 case mask_style:
486 mask = (tcp->u_arg[0] >> 8) & 0xff;
487 tcp->u_arg[0] &= 0xff;
488 for (i = 0; mask; i++)
489 mask >>= 1;
490 tcp->scno = subcall + i;
491 if (sysent[tcp->scno].nargs != -1)
492 tcp->u_nargs = sysent[tcp->scno].nargs;
493 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000494 case door_style:
495 /*
496 * Oh, yuck. The call code is the *sixth* argument.
497 */
498 tcp->scno = subcall + tcp->u_arg[5];
499 if (sysent[tcp->scno].nargs != -1)
500 tcp->u_nargs = sysent[tcp->scno].nargs;
501 else
502 tcp->u_nargs--;
503 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000504 }
505}
506#endif
507
508struct tcb *tcp_last = NULL;
509
510static int
511internal_syscall(tcp)
512struct tcb *tcp;
513{
514 /*
515 * We must always trace a few critical system calls in order to
516 * correctly support following forks in the presence of tracing
517 * qualifiers.
518 */
519#ifdef __arm__
520 switch (tcp->scno + __NR_SYSCALL_BASE) {
521#else
522 switch (tcp->scno) {
523#endif
524#ifdef SYS_fork
525 case SYS_fork:
526#endif
527#ifdef SYS_vfork
528 case SYS_vfork:
529#endif
530#ifdef SYS_clone
531 case SYS_clone:
532#endif
533 internal_fork(tcp);
534 break;
535
536#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
570trace_syscall(tcp)
571struct tcb *tcp;
572{
573 int sys_res;
574 struct timeval tv;
575 long scno = 0;
576#ifdef LINUX
577#if defined (I386)
578 long eax;
579#elif defined (POWERPC)
580 long result,flags;
581#elif defined (M68K)
582 int d0;
583#elif defined (ARM)
584 int r0;
585#elif defined (ALPHA)
586 long r0;
587 long a3;
588#elif defined (SPARC)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000589 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000590 unsigned long trap;
591#endif
592#endif /* LINUX */
593
594#ifndef SVR4
595 int pid = tcp->pid;
596#endif /* !SVR4 */
597
598 /* Measure the exit time as early as possible to avoid errors. */
599 if (dtime && (tcp->flags & TCB_INSYSCALL))
600 gettimeofday(&tv, NULL);
601#ifdef LINUX
602#if defined (POWERPC)
603 if (upeek(pid, 4*PT_R0, &scno) < 0)
604 return -1;
605 if (!(tcp->flags & TCB_INSYSCALL)) {
606 /* Check if we return from execve. */
607 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
608 tcp->flags &= ~TCB_WAITEXECVE;
609 return 0;
610 }
611 }
612#elif defined (I386)
613 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
614 return -1;
615#elif defined (ARM)
616 {
617 long pc;
618 upeek(pid, 4*15, &pc);
619 umoven(tcp, pc-4, 4, (char *)&scno);
620 scno &= 0x000fffff;
621 }
622#elif defined (M68K)
623 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
624 return -1;
625#elif defined (ALPHA)
626 if (upeek(pid, REG_A3, &a3) < 0)
627 return -1;
628
629 if (!(tcp->flags & TCB_INSYSCALL)) {
630 if (upeek(pid, REG_R0, &scno) < 0)
631 return -1;
632
633 /* Check if we return from execve. */
634 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
635 tcp->flags &= ~TCB_WAITEXECVE;
636 return 0;
637 }
638
639 /*
640 * Do some sanity checks to figure out if it's
641 * really a syscall entry
642 */
643 if (scno < 0 || scno > nsyscalls) {
644 if (a3 == 0 || a3 == -1) {
645 if (debug)
646 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
647 return 0;
648 }
649 }
650 }
651 else {
652 if (upeek(pid, REG_R0, &r0) < 0)
653 return -1;
654 }
655#elif defined (SPARC)
656 /* Everything we need is in the current register set. */
657 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
658 return -1;
659
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000660 /* If we are entering, then disassemble the syscall trap. */
661 if (!(tcp->flags & TCB_INSYSCALL)) {
662 /* Retrieve the syscall trap instruction. */
663 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000664 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000665 if (errno)
666 return -1;
667
668 /* Disassemble the trap to see what personality to use. */
669 switch (trap) {
670 case 0x91d02010:
671 /* Linux/SPARC syscall trap. */
672 set_personality(0);
673 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000674 case 0x91d0206d:
675 /* Linux/SPARC64 syscall trap. */
676 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
677 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000678 case 0x91d02000:
679 /* SunOS syscall trap. (pers 1) */
680 fprintf(stderr,"syscall: SunOS no support\n");
681 return -1;
682 case 0x91d02008:
683 /* Solaris 2.x syscall trap. (per 2) */
684 set_personality(1);
685 break;
686 case 0x91d02009:
687 /* NetBSD/FreeBSD syscall trap. */
688 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
689 return -1;
690 case 0x91d02027:
691 /* Solaris 2.x gettimeofday */
692 set_personality(1);
693 break;
694 default:
695 /* Unknown syscall trap. */
696 if(tcp->flags & TCB_WAITEXECVE) {
697 tcp->flags &= ~TCB_WAITEXECVE;
698 return 0;
699 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000700 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000701 return -1;
702 }
703
704 /* Extract the system call number from the registers. */
705 if (trap == 0x91d02027)
706 scno = 156;
707 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000708 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000709 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000710 scno = regs.r_o0;
711 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000712 }
713 }
714#endif
715#endif /* LINUX */
716#ifdef SUNOS4
717 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
718 return -1;
719#endif
720#ifdef SVR4
721#ifdef HAVE_PR_SYSCALL
722 scno = tcp->status.pr_syscall;
723#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000724 scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000725#endif /* !HAVE_PR_SYSCALL */
726 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000727 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728 if (
729 scno == SYS_fork
730#ifdef SYS_vfork
731 || scno == SYS_vfork
732#endif /* SYS_vfork */
733 ) {
734 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000735 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000736 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000737 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000738 }
739 else {
740 fprintf(stderr, "syscall: missing entry\n");
741 tcp->flags |= TCB_INSYSCALL;
742 }
743 }
744 }
745 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000746 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000747 fprintf(stderr, "syscall: missing exit\n");
748 tcp->flags &= ~TCB_INSYSCALL;
749 }
750 }
751#endif /* SVR4 */
752#ifdef SUNOS4
753 if (!(tcp->flags & TCB_INSYSCALL)) {
754 if (scno == 0) {
755 fprintf(stderr, "syscall: missing entry\n");
756 tcp->flags |= TCB_INSYSCALL;
757 }
758 }
759 else {
760 if (scno != 0) {
761 if (debug) {
762 /*
763 * This happens when a signal handler
764 * for a signal which interrupted a
765 * a system call makes another system call.
766 */
767 fprintf(stderr, "syscall: missing exit\n");
768 }
769 tcp->flags &= ~TCB_INSYSCALL;
770 }
771 }
772#endif /* SUNOS4 */
773#ifdef LINUX
774#if defined (I386)
775 if (upeek(pid, 4*EAX, &eax) < 0)
776 return -1;
777 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
778 if (debug)
779 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
780 return 0;
781 }
782#elif defined (POWERPC)
783# define SO_MASK 0x10000000
784 if (upeek(pid, 4*PT_CCR, &flags) < 0)
785 return -1;
786 if (upeek(pid, 4*PT_R3, &result) < 0)
787 return -1;
788 if (flags & SO_MASK)
789 result = -result;
790#elif defined (M68K)
791 if (upeek(pid, 4*PT_D0, &d0) < 0)
792 return -1;
793 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
794 if (debug)
795 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
796 return 0;
797 }
798#elif defined (ARM)
799 if (upeek(pid, 4*0, (long *)&r0) < 0)
800 return -1;
801 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
802 if (debug)
803 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
804 return 0;
805 }
806#else
807#endif
808#endif /* LINUX */
809
810 if (tcp->flags & TCB_INSYSCALL) {
811 long u_error;
812
813#ifdef LINUX
814#ifdef I386
815 if (eax < 0 && -eax < nerrnos) {
816 tcp->u_rval = -1;
817 u_error = -eax;
818 }
819 else {
820 tcp->u_rval = eax;
821 u_error = 0;
822 }
823#else /* !I386 */
824#ifdef POWERPC
825 if (result && (unsigned) -result < nerrnos) {
826 tcp->u_rval = -1;
827 u_error = -result;
828 }
829 else {
830 tcp->u_rval = result;
831 u_error = 0;
832 }
833#else /* !POWERPC */
834#ifdef M68K
835 if (d0 && (unsigned) -d0 < nerrnos) {
836 tcp->u_rval = -1;
837 u_error = -d0;
838 }
839 else {
840 tcp->u_rval = d0;
841 u_error = 0;
842 }
843#else /* !M68K */
844#ifdef ARM
845 if (r0 && (unsigned) -r0 < nerrnos) {
846 tcp->u_rval = -1;
847 u_error = -r0;
848 }
849 else {
850 tcp->u_rval = r0;
851 u_error = 0;
852 }
853#else /* !ARM */
854#ifdef ALPHA
855 if (a3) {
856 tcp->u_rval = -1;
857 u_error = r0;
858 }
859 else {
860 tcp->u_rval = r0;
861 u_error = 0;
862 }
863#else /* !ALPHA */
864#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000865 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000866 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000867 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000868 }
869 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000870 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000871 u_error = 0;
872 }
873#endif /* SPARC */
874#endif /* ALPHA */
875#endif /* ARM */
876#endif /* M68K */
877#endif /* POWERPC */
878#endif /* I386 */
879#endif /* LINUX */
880#ifdef SUNOS4
881 /* get error code from user struct */
882 if (upeek(pid, uoff(u_error), &u_error) < 0)
883 return -1;
884 u_error >>= 24; /* u_error is a char */
885
886 /* get system call return value */
887 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
888 return -1;
889#endif /* SUNOS4 */
890#ifdef SVR4
891#ifdef SPARC
892 /* Judicious guessing goes a long way. */
893 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
894 tcp->u_rval = -1;
895 u_error = tcp->status.pr_reg[R_O0];
896 }
897 else {
898 tcp->u_rval = tcp->status.pr_reg[R_O0];
899 u_error = 0;
900 }
901#endif /* SPARC */
902#ifdef I386
903 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000904 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000905 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000906 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000907 }
908 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000909 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000910 u_error = 0;
911 }
912#endif /* I386 */
913#ifdef MIPS
914 if (tcp->status.pr_reg[CTX_A3]) {
915 tcp->u_rval = -1;
916 u_error = tcp->status.pr_reg[CTX_V0];
917 }
918 else {
919 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
920 u_error = 0;
921 }
922#endif /* MIPS */
923#endif /* SVR4 */
924 tcp->u_error = u_error;
925
926 internal_syscall(tcp);
927 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
928 tcp->flags &= ~TCB_INSYSCALL;
929 return 0;
930 }
931
932 if (tcp->flags & TCB_REPRINT) {
933 printleader(tcp);
934 tprintf("<... ");
935 if (tcp->scno >= nsyscalls)
936 tprintf("syscall_%lu", tcp->scno);
937 else
938 tprintf("%s", sysent[tcp->scno].sys_name);
939 tprintf(" resumed> ");
940 }
941
942 if (cflag) {
943 call_count[tcp->scno]++;
944 if (u_error)
945 error_count[tcp->scno]++;
946 tv_sub(&tv, &tv, &tcp->etime);
947#ifdef LINUX
948 if (tv_cmp(&tv, &tcp->dtime) > 0) {
949 static struct timeval one_tick =
950 { 0, 1000000 / HZ };
951
952 if (tv_nz(&tcp->dtime))
953 tv = tcp->dtime;
954 else if (tv_cmp(&tv, &one_tick) > 0) {
955 if (tv_cmp(&shortest, &one_tick) < 0)
956 tv = shortest;
957 else
958 tv = one_tick;
959 }
960 }
961#endif /* LINUX */
962 if (tv_cmp(&tv, &shortest) < 0)
963 shortest = tv;
964 tv_add(&tv_count[tcp->scno],
965 &tv_count[tcp->scno], &tv);
966 tcp->flags &= ~TCB_INSYSCALL;
967 return 0;
968 }
969
970 if (tcp->scno >= nsyscalls
971 || (qual_flags[tcp->scno] & QUAL_RAW))
972 sys_res = printargs(tcp);
973 else
974 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
975 u_error = tcp->u_error;
976 tprintf(") ");
977 tabto(acolumn);
978 if (qual_flags[tcp->scno] & QUAL_RAW) {
979 if (u_error)
980 tprintf("= -1 (errno %ld)", u_error);
981 else
982 tprintf("= %#lx", tcp->u_rval);
983 }
984 else if (!(sys_res & RVAL_NONE) && u_error) {
985#ifdef LINUX
986 switch (u_error) {
987 case ERESTARTSYS:
988 tprintf("= ? ERESTARTSYS (To be restarted)");
989 break;
990 case ERESTARTNOINTR:
991 tprintf("= ? ERESTARTNOINTR (To be restarted)");
992 break;
993 case ERESTARTNOHAND:
994 tprintf("= ? ERESTARTNOHAND (To be restarted)");
995 break;
996 default:
997#endif /* LINUX */
998 tprintf("= -1 ");
999 if (u_error < nerrnos && u_error < sys_nerr)
1000 tprintf("%s (%s)", errnoent[u_error],
1001 sys_errlist[u_error]);
1002 else if (u_error < nerrnos)
1003 tprintf("%s (errno %ld)",
1004 errnoent[u_error], u_error);
1005 else if (u_error < sys_nerr)
1006 tprintf("ERRNO_%ld (%s)", u_error,
1007 sys_errlist[u_error]);
1008 else
1009 tprintf("E??? (errno %ld)", u_error);
1010#ifdef LINUX
1011 break;
1012 }
1013#endif /* LINUX */
1014 }
1015 else {
1016 if (sys_res & RVAL_NONE)
1017 tprintf("= ?");
1018 else {
1019 switch (sys_res & RVAL_MASK) {
1020 case RVAL_HEX:
1021 tprintf("= %#lx", tcp->u_rval);
1022 break;
1023 case RVAL_OCTAL:
1024 tprintf("= %#lo", tcp->u_rval);
1025 break;
1026 case RVAL_UDECIMAL:
1027 tprintf("= %lu", tcp->u_rval);
1028 break;
1029 case RVAL_DECIMAL:
1030 tprintf("= %ld", tcp->u_rval);
1031 break;
1032 default:
1033 fprintf(stderr,
1034 "invalid rval format\n");
1035 break;
1036 }
1037 }
1038 if ((sys_res & RVAL_STR) && tcp->auxstr)
1039 tprintf(" (%s)", tcp->auxstr);
1040 }
1041 if (dtime) {
1042 tv_sub(&tv, &tv, &tcp->etime);
1043 tprintf(" <%ld.%06ld>",
1044 (long) tv.tv_sec, (long) tv.tv_usec);
1045 }
1046 printtrailer(tcp);
1047
1048 dumpio(tcp);
1049 if (fflush(tcp->outf) == EOF)
1050 return -1;
1051 tcp->flags &= ~TCB_INSYSCALL;
1052 return 0;
1053 }
1054
1055 /* Entering system call */
1056 tcp->scno = scno;
1057#ifdef LINUX
1058#if defined (ALPHA)
1059 {
1060 int i;
1061 tcp->u_nargs = sysent[tcp->scno].nargs;
1062 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001063 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1064 * for scno somewhere above here!
1065 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001066 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1067 return -1;
1068 }
1069 }
1070#elif defined (POWERPC)
1071 {
1072 int i;
1073 tcp->u_nargs = sysent[tcp->scno].nargs;
1074 for (i = 0; i < tcp->u_nargs; i++) {
1075 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1076 return -1;
1077 }
1078 }
1079#elif defined (SPARC)
1080 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001081 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001082
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001083 tcp->u_nargs = sysent[tcp->scno].nargs;
1084 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001085 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001086 }
1087#else
1088 {
1089 int i;
1090 tcp->u_nargs = sysent[tcp->scno].nargs;
1091 for (i = 0; i < tcp->u_nargs; i++) {
1092 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1093 return -1;
1094 }
1095 }
1096#endif
1097#endif /* LINUX */
1098#ifdef SUNOS4
1099 {
1100 int i;
1101 tcp->u_nargs = sysent[tcp->scno].nargs;
1102 for (i = 0; i < tcp->u_nargs; i++) {
1103 struct user *u;
1104
1105 if (upeek(pid, uoff(u_arg[0]) +
1106 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1107 return -1;
1108 }
1109 }
1110#endif /* SUNOS4 */
1111#ifdef SVR4
1112#ifdef MIPS
1113 /*
1114 * SGI is broken: even though it has pr_sysarg, it doesn't
1115 * set them on system call entry. Get a clue.
1116 */
1117 if (sysent[tcp->scno].nargs != -1)
1118 tcp->u_nargs = sysent[tcp->scno].nargs;
1119 else
1120 tcp->u_nargs = tcp->status.pr_nsysarg;
1121 if (tcp->u_nargs > 4) {
1122 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1123 4*sizeof(tcp->u_arg[0]));
1124 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1125 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1126 }
1127 else {
1128 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1129 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1130 }
1131#else /* !MIPS */
1132#ifdef HAVE_PR_SYSCALL
1133 if (sysent[tcp->scno].nargs != -1)
1134 tcp->u_nargs = sysent[tcp->scno].nargs;
1135 else
1136 tcp->u_nargs = tcp->status.pr_nsysarg;
1137 {
1138 int i;
1139 for (i = 0; i < tcp->u_nargs; i++)
1140 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1141 }
1142#else /* !HAVE_PR_SYSCALL */
1143#ifdef I386
1144 if (sysent[tcp->scno].nargs != -1)
1145 tcp->u_nargs = sysent[tcp->scno].nargs;
1146 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001147#if UNIXWARE >= 2
1148 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1149#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001150 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001151#endif
1152 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001153 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1154#endif /* I386 */
1155#endif /* !HAVE_PR_SYSCALL */
1156#endif /* !MIPS */
1157#endif /* SVR4 */
1158#ifdef __arm__
1159 switch (tcp->scno + __NR_SYSCALL_BASE) {
1160#else
1161 switch (tcp->scno) {
1162#endif
1163#ifdef LINUX
1164#if !defined (ALPHA) && !defined(SPARC)
1165 case SYS_socketcall:
1166 decode_subcall(tcp, SYS_socket_subcall,
1167 SYS_socket_nsubcalls, deref_style);
1168 break;
1169 case SYS_ipc:
1170 decode_subcall(tcp, SYS_ipc_subcall,
1171 SYS_ipc_nsubcalls, shift_style);
1172 break;
1173#endif /* !ALPHA && !SPARC */
1174#ifdef SPARC
1175 case SYS_socketcall:
1176 sparc_socket_decode (tcp);
1177 break;
1178#endif
1179#endif /* LINUX */
1180#ifdef SVR4
1181#ifdef SYS_pgrpsys_subcall
1182 case SYS_pgrpsys:
1183 decode_subcall(tcp, SYS_pgrpsys_subcall,
1184 SYS_pgrpsys_nsubcalls, shift_style);
1185 break;
1186#endif /* SYS_pgrpsys_subcall */
1187#ifdef SYS_sigcall_subcall
1188 case SYS_sigcall:
1189 decode_subcall(tcp, SYS_sigcall_subcall,
1190 SYS_sigcall_nsubcalls, mask_style);
1191 break;
1192#endif /* SYS_sigcall_subcall */
1193 case SYS_msgsys:
1194 decode_subcall(tcp, SYS_msgsys_subcall,
1195 SYS_msgsys_nsubcalls, shift_style);
1196 break;
1197 case SYS_shmsys:
1198 decode_subcall(tcp, SYS_shmsys_subcall,
1199 SYS_shmsys_nsubcalls, shift_style);
1200 break;
1201 case SYS_semsys:
1202 decode_subcall(tcp, SYS_semsys_subcall,
1203 SYS_semsys_nsubcalls, shift_style);
1204 break;
1205#if 0 /* broken */
1206 case SYS_utssys:
1207 decode_subcall(tcp, SYS_utssys_subcall,
1208 SYS_utssys_nsubcalls, shift_style);
1209 break;
1210#endif
1211 case SYS_sysfs:
1212 decode_subcall(tcp, SYS_sysfs_subcall,
1213 SYS_sysfs_nsubcalls, shift_style);
1214 break;
1215 case SYS_spcall:
1216 decode_subcall(tcp, SYS_spcall_subcall,
1217 SYS_spcall_nsubcalls, shift_style);
1218 break;
1219#ifdef SYS_context_subcall
1220 case SYS_context:
1221 decode_subcall(tcp, SYS_context_subcall,
1222 SYS_context_nsubcalls, shift_style);
1223 break;
1224#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001225#ifdef SYS_door_subcall
1226 case SYS_door:
1227 decode_subcall(tcp, SYS_door_subcall,
1228 SYS_door_nsubcalls, door_style);
1229 break;
1230#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001231#endif /* SVR4 */
1232#ifdef SUNOS4
1233 case SYS_semsys:
1234 decode_subcall(tcp, SYS_semsys_subcall,
1235 SYS_semsys_nsubcalls, shift_style);
1236 break;
1237 case SYS_msgsys:
1238 decode_subcall(tcp, SYS_msgsys_subcall,
1239 SYS_msgsys_nsubcalls, shift_style);
1240 break;
1241 case SYS_shmsys:
1242 decode_subcall(tcp, SYS_shmsys_subcall,
1243 SYS_shmsys_nsubcalls, shift_style);
1244 break;
1245#endif
1246 }
1247
1248 internal_syscall(tcp);
1249 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1250 tcp->flags |= TCB_INSYSCALL;
1251 return 0;
1252 }
1253
1254 if (cflag) {
1255 gettimeofday(&tcp->etime, NULL);
1256 tcp->flags |= TCB_INSYSCALL;
1257 return 0;
1258 }
1259
1260 printleader(tcp);
1261 tcp->flags &= ~TCB_REPRINT;
1262 tcp_last = tcp;
1263 if (tcp->scno >= nsyscalls)
1264 tprintf("syscall_%lu(", tcp->scno);
1265 else
1266 tprintf("%s(", sysent[tcp->scno].sys_name);
1267 if (tcp->scno >= nsyscalls ||
1268 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1269 sys_res = printargs(tcp);
1270 else
1271 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1272 if (fflush(tcp->outf) == EOF)
1273 return -1;
1274 tcp->flags |= TCB_INSYSCALL;
1275 /* Measure the entrance time as late as possible to avoid errors. */
1276 if (dtime)
1277 gettimeofday(&tcp->etime, NULL);
1278 return sys_res;
1279}
1280
1281int
1282printargs(tcp)
1283struct tcb *tcp;
1284{
1285 if (entering(tcp)) {
1286 int i;
1287
1288 for (i = 0; i < tcp->u_nargs; i++)
1289 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1290 }
1291 return 0;
1292}
1293
1294long
1295getrval2(tcp)
1296struct tcb *tcp;
1297{
1298 long val = -1;
1299
1300#ifdef LINUX
1301#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001302 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001303 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1304 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001305 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001306#endif /* SPARC */
1307#endif /* LINUX */
1308
1309#ifdef SUNOS4
1310 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1311 return -1;
1312#endif /* SUNOS4 */
1313
1314#ifdef SVR4
1315#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001316 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001317#endif /* SPARC */
1318#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001319 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001320#endif /* I386 */
1321#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001322 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001323#endif /* MIPS */
1324#endif /* SVR4 */
1325
1326 return val;
1327}
1328
1329/*
1330 * Apparently, indirect system calls have already be converted by ptrace(2),
1331 * so if you see "indir" this program has gone astray.
1332 */
1333int
1334sys_indir(tcp)
1335struct tcb *tcp;
1336{
1337 int i, scno, nargs;
1338
1339 if (entering(tcp)) {
1340 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1341 fprintf(stderr, "Bogus syscall: %u\n", scno);
1342 return 0;
1343 }
1344 nargs = sysent[scno].nargs;
1345 tprintf("%s", sysent[scno].sys_name);
1346 for (i = 0; i < nargs; i++)
1347 tprintf(", %#lx", tcp->u_arg[i+1]);
1348 }
1349 return 0;
1350}
1351
1352static int
1353time_cmp(a, b)
1354void *a;
1355void *b;
1356{
1357 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1358}
1359
1360static int
1361syscall_cmp(a, b)
1362void *a;
1363void *b;
1364{
1365 return strcmp(sysent[*((int *) a)].sys_name,
1366 sysent[*((int *) b)].sys_name);
1367}
1368
1369static int
1370count_cmp(a, b)
1371void *a;
1372void *b;
1373{
1374 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1375
1376 return (m < n) ? 1 : (m > n) ? -1 : 0;
1377}
1378
1379static int (*sortfun)();
1380static struct timeval overhead = { -1, -1 };
1381
1382void
1383set_sortby(sortby)
1384char *sortby;
1385{
1386 if (strcmp(sortby, "time") == 0)
1387 sortfun = time_cmp;
1388 else if (strcmp(sortby, "calls") == 0)
1389 sortfun = count_cmp;
1390 else if (strcmp(sortby, "name") == 0)
1391 sortfun = syscall_cmp;
1392 else if (strcmp(sortby, "nothing") == 0)
1393 sortfun = NULL;
1394 else {
1395 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1396 exit(1);
1397 }
1398}
1399
1400void set_overhead(n)
1401int n;
1402{
1403 overhead.tv_sec = n / 1000000;
1404 overhead.tv_usec = n % 1000000;
1405}
1406
1407void
1408call_summary(outf)
1409FILE *outf;
1410{
1411 int i, j;
1412 int call_cum, error_cum;
1413 struct timeval tv_cum, dtv;
1414 double percent;
1415 char *dashes = "-------------------------";
1416 char error_str[16];
1417
1418 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1419 if (overhead.tv_sec == -1) {
1420 tv_mul(&overhead, &shortest, 8);
1421 tv_div(&overhead, &overhead, 10);
1422 }
1423 for (i = 0; i < nsyscalls; i++) {
1424 sorted_count[i] = i;
1425 if (call_count[i] == 0)
1426 continue;
1427 tv_mul(&dtv, &overhead, call_count[i]);
1428 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1429 call_cum += call_count[i];
1430 error_cum += error_count[i];
1431 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1432 }
1433 if (sortfun)
1434 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1435 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1436 "% time", "seconds", "usecs/call",
1437 "calls", "errors", "syscall");
1438 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1439 dashes, dashes, dashes, dashes, dashes, dashes);
1440 for (i = 0; i < nsyscalls; i++) {
1441 j = sorted_count[i];
1442 if (call_count[j] == 0)
1443 continue;
1444 tv_div(&dtv, &tv_count[j], call_count[j]);
1445 if (error_count[j])
1446 sprintf(error_str, "%d", error_count[j]);
1447 else
1448 error_str[0] = '\0';
1449 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1450 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1451 percent, (long) tv_count[j].tv_sec,
1452 (long) tv_count[j].tv_usec,
1453 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1454 call_count[j], error_str, sysent[j].sys_name);
1455 }
1456 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1457 dashes, dashes, dashes, dashes, dashes, dashes);
1458 if (error_cum)
1459 sprintf(error_str, "%d", error_cum);
1460 else
1461 error_str[0] = '\0';
1462 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1463 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1464 call_cum, error_str, "total");
1465}