blob: ce0ee30db604bb9fdb627ad62492a9af12108c3d [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 Akkerman8b1b40c2000-02-03 21:58:30 +000061#if defined(LINUX) && defined(IA64)
62# include <asm/ptrace_offsets.h>
63# include <asm/rse.h>
64#endif
65
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000066#ifndef SYS_ERRLIST_DECLARED
67extern int sys_nerr;
68extern char *sys_errlist[];
69#endif /* SYS_ERRLIST_DECLARED */
70
Pavel Machekd8ae7e32000-02-01 17:17:25 +000071#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000072#ifdef LINUX
73#ifndef ERESTARTSYS
74#define ERESTARTSYS 512
75#endif
76#ifndef ERESTARTNOINTR
77#define ERESTARTNOINTR 513
78#endif
79#ifndef ERESTARTNOHAND
80#define ERESTARTNOHAND 514 /* restart if no handler.. */
81#endif
82#ifndef ENOIOCTLCMD
83#define ENOIOCTLCMD 515 /* No ioctl command */
84#endif
85#ifndef NSIG
86#define NSIG 32
87#endif
88#ifdef ARM
89#undef NSIG
90#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000091#undef NR_SYSCALL_BASE
92#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#endif
94#endif /* LINUX */
95
96#include "syscall.h"
97
98/* Define these shorthand notations to simplify the syscallent files. */
99#define TF TRACE_FILE
100#define TI TRACE_IPC
101#define TN TRACE_NETWORK
102#define TP TRACE_PROCESS
103#define TS TRACE_SIGNAL
104
105struct sysent sysent0[] = {
106#include "syscallent.h"
107};
108int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
109
110#if SUPPORTED_PERSONALITIES >= 2
111struct sysent sysent1[] = {
112#include "syscallent1.h"
113};
114int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
115#endif /* SUPPORTED_PERSONALITIES >= 2 */
116
117#if SUPPORTED_PERSONALITIES >= 3
118struct sysent sysent2[] = {
119#include "syscallent2.h"
120};
121int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
122#endif /* SUPPORTED_PERSONALITIES >= 3 */
123
124struct sysent *sysent;
125int nsyscalls;
126
127/* Now undef them since short defines cause wicked namespace pollution. */
128#undef TF
129#undef TI
130#undef TN
131#undef TP
132#undef TS
133
134char *errnoent0[] = {
135#include "errnoent.h"
136};
137int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
138
139#if SUPPORTED_PERSONALITIES >= 2
140char *errnoent1[] = {
141#include "errnoent1.h"
142};
143int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
144#endif /* SUPPORTED_PERSONALITIES >= 2 */
145
146#if SUPPORTED_PERSONALITIES >= 3
147char *errnoent2[] = {
148#include "errnoent2.h"
149};
150int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
151#endif /* SUPPORTED_PERSONALITIES >= 3 */
152
153char **errnoent;
154int nerrnos;
155
156int current_personality;
157
158int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000159set_personality(personality)
160int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161{
162 switch (personality) {
163 case 0:
164 errnoent = errnoent0;
165 nerrnos = nerrnos0;
166 sysent = sysent0;
167 nsyscalls = nsyscalls0;
168 ioctlent = ioctlent0;
169 nioctlents = nioctlents0;
170 signalent = signalent0;
171 nsignals = nsignals0;
172 break;
173
174#if SUPPORTED_PERSONALITIES >= 2
175 case 1:
176 errnoent = errnoent1;
177 nerrnos = nerrnos1;
178 sysent = sysent1;
179 nsyscalls = nsyscalls1;
180 ioctlent = ioctlent1;
181 nioctlents = nioctlents1;
182 signalent = signalent1;
183 nsignals = nsignals1;
184 break;
185#endif /* SUPPORTED_PERSONALITIES >= 2 */
186
187#if SUPPORTED_PERSONALITIES >= 3
188 case 2:
189 errnoent = errnoent2;
190 nerrnos = nerrnos2;
191 sysent = sysent2;
192 nsyscalls = nsyscalls2;
193 ioctlent = ioctlent2;
194 nioctlents = nioctlents2;
195 signalent = signalent2;
196 nsignals = nsignals2;
197 break;
198#endif /* SUPPORTED_PERSONALITIES >= 3 */
199
200 default:
201 return -1;
202 }
203
204 current_personality = personality;
205 return 0;
206}
207
208int qual_flags[MAX_QUALS];
209
210static int call_count[MAX_QUALS];
211static int error_count[MAX_QUALS];
212static struct timeval tv_count[MAX_QUALS];
213static int sorted_count[MAX_QUALS];
214
215static struct timeval shortest = { 1000000, 0 };
216
217static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
218
219static struct qual_options {
220 int bitflag;
221 char *option_name;
222 int (*lookup)();
223 char *argument_name;
224} qual_options[] = {
225 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
226 { QUAL_TRACE, "t", lookup_syscall, "system call" },
227 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
228 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
229 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
230 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
231 { QUAL_RAW, "raw", lookup_syscall, "system call" },
232 { QUAL_RAW, "x", lookup_syscall, "system call" },
233 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
234 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
235 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
236 { QUAL_FAULT, "fault", lookup_fault, "fault" },
237 { QUAL_FAULT, "faults", lookup_fault, "fault" },
238 { QUAL_FAULT, "m", lookup_fault, "fault" },
239 { QUAL_READ, "read", lookup_desc, "descriptor" },
240 { QUAL_READ, "reads", lookup_desc, "descriptor" },
241 { QUAL_READ, "r", lookup_desc, "descriptor" },
242 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
243 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
244 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
245 { 0, NULL, NULL, NULL },
246};
247
248static int
249lookup_syscall(s)
250char *s;
251{
252 int i;
253
254 for (i = 0; i < nsyscalls; i++) {
255 if (strcmp(s, sysent[i].sys_name) == 0)
256 return i;
257 }
258 return -1;
259}
260
261static int
262lookup_signal(s)
263char *s;
264{
265 int i;
266 char buf[32];
267
268 if (s && *s && isdigit(*s))
269 return atoi(s);
270 strcpy(buf, s);
271 s = buf;
272 for (i = 0; s[i]; i++)
273 s[i] = toupper(s[i]);
274 if (strncmp(s, "SIG", 3) == 0)
275 s += 3;
276 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000277 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000278 return i;
279 }
280 return -1;
281}
282
283static int
284lookup_fault(s)
285char *s;
286{
287 return -1;
288}
289
290static int
291lookup_desc(s)
292char *s;
293{
294 if (s && *s && isdigit(*s))
295 return atoi(s);
296 return -1;
297}
298
299static int
300lookup_class(s)
301char *s;
302{
303 if (strcmp(s, "file") == 0)
304 return TRACE_FILE;
305 if (strcmp(s, "ipc") == 0)
306 return TRACE_IPC;
307 if (strcmp(s, "network") == 0)
308 return TRACE_NETWORK;
309 if (strcmp(s, "process") == 0)
310 return TRACE_PROCESS;
311 if (strcmp(s, "signal") == 0)
312 return TRACE_SIGNAL;
313 return -1;
314}
315
316void
317qualify(s)
318char *s;
319{
320 struct qual_options *opt;
321 int not;
322 char *p;
323 int i, n;
324
325 opt = &qual_options[0];
326 for (i = 0; (p = qual_options[i].option_name); i++) {
327 n = strlen(p);
328 if (strncmp(s, p, n) == 0 && s[n] == '=') {
329 opt = &qual_options[i];
330 s += n + 1;
331 break;
332 }
333 }
334 not = 0;
335 if (*s == '!') {
336 not = 1;
337 s++;
338 }
339 if (strcmp(s, "none") == 0) {
340 not = 1 - not;
341 s = "all";
342 }
343 if (strcmp(s, "all") == 0) {
344 for (i = 0; i < MAX_QUALS; i++) {
345 if (not)
346 qual_flags[i] &= ~opt->bitflag;
347 else
348 qual_flags[i] |= opt->bitflag;
349 }
350 return;
351 }
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 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
359 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
360 for (i = 0; i < MAX_QUALS; i++) {
361 if (sysent[i].sys_flags & n) {
362 if (not)
363 qual_flags[i] &= ~opt->bitflag;
364 else
365 qual_flags[i] |= opt->bitflag;
366 }
367 }
368 continue;
369 }
370 if ((n = (*opt->lookup)(p)) < 0) {
371 fprintf(stderr, "strace: invalid %s `%s'\n",
372 opt->argument_name, p);
373 exit(1);
374 }
375 if (not)
376 qual_flags[n] &= ~opt->bitflag;
377 else
378 qual_flags[n] |= opt->bitflag;
379 }
380 return;
381}
382
383static void
384dumpio(tcp)
385struct tcb *tcp;
386{
387 if (syserror(tcp))
388 return;
389 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
390 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000391 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000392 case SYS_read:
393#ifdef SYS_recv
394 case SYS_recv:
395#endif
396#ifdef SYS_recvfrom
397 case SYS_recvfrom:
398#endif
399 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
400 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
401 break;
402 case SYS_write:
403#ifdef SYS_send
404 case SYS_send:
405#endif
406#ifdef SYS_sendto
407 case SYS_sendto:
408#endif
409 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
410 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
411 break;
412 }
413}
414
Wichert Akkerman8829a551999-06-11 13:18:40 +0000415enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000416
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000417#if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000418
419const int socket_map [] = {
420 /* SYS_SOCKET */ 97,
421 /* SYS_BIND */ 104,
422 /* SYS_CONNECT */ 98,
423 /* SYS_LISTEN */ 106,
424 /* SYS_ACCEPT */ 99,
425 /* SYS_GETSOCKNAME */ 150,
426 /* SYS_GETPEERNAME */ 141,
427 /* SYS_SOCKETPAIR */ 135,
428 /* SYS_SEND */ 101,
429 /* SYS_RECV */ 102,
430 /* SYS_SENDTO */ 133,
431 /* SYS_RECVFROM */ 125,
432 /* SYS_SHUTDOWN */ 134,
433 /* SYS_SETSOCKOPT */ 105,
434 /* SYS_GETSOCKOPT */ 118,
435 /* SYS_SENDMSG */ 114,
436 /* SYS_RECVMSG */ 113
437};
438
439void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000440sparc_socket_decode (tcp)
441struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000442{
443 volatile long addr;
444 volatile int i, n;
445
446 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
447 return;
448 }
449 tcp->scno = socket_map [tcp->u_arg [0]-1];
450 n = tcp->u_nargs = sysent [tcp->scno].nargs;
451 addr = tcp->u_arg [1];
452 for (i = 0; i < n; i++){
453 int arg;
454 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
455 arg = 0;
456 tcp->u_arg [i] = arg;
457 addr += sizeof (arg);
458 }
459}
460
461static void
462decode_subcall(tcp, subcall, nsubcalls, style)
463struct tcb *tcp;
464int subcall;
465int nsubcalls;
466enum subcall_style style;
467{
468 int i, addr, mask, arg;
469
470 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
471 return;
472 switch (style) {
473 case shift_style:
474 tcp->scno = subcall + tcp->u_arg[0];
475 if (sysent[tcp->scno].nargs != -1)
476 tcp->u_nargs = sysent[tcp->scno].nargs;
477 else
478 tcp->u_nargs--;
479 for (i = 0; i < tcp->u_nargs; i++)
480 tcp->u_arg[i] = tcp->u_arg[i + 1];
481 break;
482 case deref_style:
483 tcp->scno = subcall + tcp->u_arg[0];
484 addr = tcp->u_arg[1];
485 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
486 if (umove(tcp, addr, &arg) < 0)
487 arg = 0;
488 tcp->u_arg[i] = arg;
489 addr += sizeof(arg);
490 }
491 tcp->u_nargs = sysent[tcp->scno].nargs;
492 break;
493 case mask_style:
494 mask = (tcp->u_arg[0] >> 8) & 0xff;
495 tcp->u_arg[0] &= 0xff;
496 for (i = 0; mask; i++)
497 mask >>= 1;
498 tcp->scno = subcall + i;
499 if (sysent[tcp->scno].nargs != -1)
500 tcp->u_nargs = sysent[tcp->scno].nargs;
501 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000502 case door_style:
503 /*
504 * Oh, yuck. The call code is the *sixth* argument.
505 */
506 tcp->scno = subcall + tcp->u_arg[5];
507 if (sysent[tcp->scno].nargs != -1)
508 tcp->u_nargs = sysent[tcp->scno].nargs;
509 else
510 tcp->u_nargs--;
511 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512 }
513}
514#endif
515
516struct tcb *tcp_last = NULL;
517
518static int
519internal_syscall(tcp)
520struct tcb *tcp;
521{
522 /*
523 * We must always trace a few critical system calls in order to
524 * correctly support following forks in the presence of tracing
525 * qualifiers.
526 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000527 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000528#ifdef SYS_fork
529 case SYS_fork:
530#endif
531#ifdef SYS_vfork
532 case SYS_vfork:
533#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000534 internal_fork(tcp);
535 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000536#ifdef SYS_clone
537 case SYS_clone:
538 internal_clone(tcp);
539 break;
540#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000541#ifdef SYS_execv
542 case SYS_execv:
543#endif
544#ifdef SYS_execve
545 case SYS_execve:
546#endif
547 internal_exec(tcp);
548 break;
549
550#ifdef SYS_wait
551 case SYS_wait:
552#endif
553#ifdef SYS_wait4
554 case SYS_wait4:
555#endif
556#ifdef SYS_waitpid
557 case SYS_waitpid:
558#endif
559#ifdef SYS_waitsys
560 case SYS_waitsys:
561#endif
562 internal_wait(tcp);
563 break;
564
565#ifdef SYS_exit
566 case SYS_exit:
567#endif
568 internal_exit(tcp);
569 break;
570 }
571 return 0;
572}
573
574int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000575get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576struct tcb *tcp;
577{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000579 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000580
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000581#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000582#if defined(S390)
583 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
584 return -1;
585 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
586 if (errno)
587 return -1;
588 scno&=0xFF;
589#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000590 if (upeek(pid, 4*PT_R0, &scno) < 0)
591 return -1;
592 if (!(tcp->flags & TCB_INSYSCALL)) {
593 /* Check if we return from execve. */
594 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
595 tcp->flags &= ~TCB_WAITEXECVE;
596 return 0;
597 }
598 }
599#elif defined (I386)
600 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
601 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000602#elif defined(IA64)
603#define IA64_PSR_IS ((long)1 << 34)
604 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
605 ia32 = (psr & IA64_PSR_IS);
606 if (!(tcp->flags & TCB_INSYSCALL)) {
607 if (ia32) {
608 if (upeek(pid, PT_R8, &scno) < 0)
609 return -1;
610 } else {
611 if (upeek (pid, PT_R15, &scno) < 0)
612 return -1;
613 }
614 } else {
615 /* syscall in progress */
616 if (upeek (pid, PT_R8, &r8) < 0)
617 return -1;
618 if (upeek (pid, PT_R10, &r10) < 0)
619 return -1;
620 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000621#elif defined (ARM)
622 {
623 long pc;
624 upeek(pid, 4*15, &pc);
625 umoven(tcp, pc-4, 4, (char *)&scno);
626 scno &= 0x000fffff;
627 }
628#elif defined (M68K)
629 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
630 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000631#elif defined (MIPS)
632 if (upeek(pid, REG_A3, &a3) < 0)
633 return -1;
634
635 if(!(tcp->flags & TCB_INSYSCALL)) {
636 if (upeek(pid, REG_V0, &scno) < 0)
637 return -1;
638
639 if (scno < 0 || scno > nsyscalls) {
640 if(a3 == 0 || a3 == -1) {
641 if(debug)
642 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
643 return 0;
644 }
645 }
646 } else {
647 if (upeek(pid, REG_V0, &r2) < 0)
648 return -1;
649 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650#elif defined (ALPHA)
651 if (upeek(pid, REG_A3, &a3) < 0)
652 return -1;
653
654 if (!(tcp->flags & TCB_INSYSCALL)) {
655 if (upeek(pid, REG_R0, &scno) < 0)
656 return -1;
657
658 /* Check if we return from execve. */
659 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
660 tcp->flags &= ~TCB_WAITEXECVE;
661 return 0;
662 }
663
664 /*
665 * Do some sanity checks to figure out if it's
666 * really a syscall entry
667 */
668 if (scno < 0 || scno > nsyscalls) {
669 if (a3 == 0 || a3 == -1) {
670 if (debug)
671 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
672 return 0;
673 }
674 }
675 }
676 else {
677 if (upeek(pid, REG_R0, &r0) < 0)
678 return -1;
679 }
680#elif defined (SPARC)
681 /* Everything we need is in the current register set. */
682 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
683 return -1;
684
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000685 /* If we are entering, then disassemble the syscall trap. */
686 if (!(tcp->flags & TCB_INSYSCALL)) {
687 /* Retrieve the syscall trap instruction. */
688 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000689 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000690 if (errno)
691 return -1;
692
693 /* Disassemble the trap to see what personality to use. */
694 switch (trap) {
695 case 0x91d02010:
696 /* Linux/SPARC syscall trap. */
697 set_personality(0);
698 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000699 case 0x91d0206d:
700 /* Linux/SPARC64 syscall trap. */
701 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
702 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000703 case 0x91d02000:
704 /* SunOS syscall trap. (pers 1) */
705 fprintf(stderr,"syscall: SunOS no support\n");
706 return -1;
707 case 0x91d02008:
708 /* Solaris 2.x syscall trap. (per 2) */
709 set_personality(1);
710 break;
711 case 0x91d02009:
712 /* NetBSD/FreeBSD syscall trap. */
713 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
714 return -1;
715 case 0x91d02027:
716 /* Solaris 2.x gettimeofday */
717 set_personality(1);
718 break;
719 default:
720 /* Unknown syscall trap. */
721 if(tcp->flags & TCB_WAITEXECVE) {
722 tcp->flags &= ~TCB_WAITEXECVE;
723 return 0;
724 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000725 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726 return -1;
727 }
728
729 /* Extract the system call number from the registers. */
730 if (trap == 0x91d02027)
731 scno = 156;
732 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000733 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000734 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000735 scno = regs.r_o0;
736 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000737 }
738 }
739#endif
740#endif /* LINUX */
741#ifdef SUNOS4
742 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
743 return -1;
744#endif
745#ifdef SVR4
746#ifdef HAVE_PR_SYSCALL
747 scno = tcp->status.pr_syscall;
748#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000749 scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000750#endif /* !HAVE_PR_SYSCALL */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000751#endif
752 tcp->scno = scno;
753 return 1;
754}
755
756#ifdef LINUX
757#if defined (I386)
758 static long eax;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000759#elif defined (IA64)
760 long r8, r10, psr;
761 long ia32 = 0;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000762#elif defined (POWERPC)
763 static long result,flags;
764#elif defined (M68K)
765 static int d0;
766#elif defined (ARM)
767 static int r0;
768#elif defined (ALPHA)
769 static long r0;
770 static long a3;
771#elif defined (SPARC)
772 static struct pt_regs regs;
773 static unsigned long trap;
774#elif defined(S390)
775 static long gpr2;
776 static long pc;
777#endif
778#endif /* LINUX */
779
780int
781syscall_fixup(tcp)
782struct tcb *tcp;
783{
784 int pid = tcp->pid;
785
786#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000787 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000788 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000789 if (
790 scno == SYS_fork
791#ifdef SYS_vfork
792 || scno == SYS_vfork
793#endif /* SYS_vfork */
794 ) {
795 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000796 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000797 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000798 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000799 }
800 else {
801 fprintf(stderr, "syscall: missing entry\n");
802 tcp->flags |= TCB_INSYSCALL;
803 }
804 }
805 }
806 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000807 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000808 fprintf(stderr, "syscall: missing exit\n");
809 tcp->flags &= ~TCB_INSYSCALL;
810 }
811 }
812#endif /* SVR4 */
813#ifdef SUNOS4
814 if (!(tcp->flags & TCB_INSYSCALL)) {
815 if (scno == 0) {
816 fprintf(stderr, "syscall: missing entry\n");
817 tcp->flags |= TCB_INSYSCALL;
818 }
819 }
820 else {
821 if (scno != 0) {
822 if (debug) {
823 /*
824 * This happens when a signal handler
825 * for a signal which interrupted a
826 * a system call makes another system call.
827 */
828 fprintf(stderr, "syscall: missing exit\n");
829 }
830 tcp->flags &= ~TCB_INSYSCALL;
831 }
832 }
833#endif /* SUNOS4 */
834#ifdef LINUX
835#if defined (I386)
836 if (upeek(pid, 4*EAX, &eax) < 0)
837 return -1;
838 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
839 if (debug)
840 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
841 return 0;
842 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000843#elif defined (S390)
844 if (upeek(pid, PT_GPR2, &gpr2) < 0)
845 return -1;
846 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
847 if (debug)
848 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
849 return 0;
850 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000851#elif defined (POWERPC)
852# define SO_MASK 0x10000000
853 if (upeek(pid, 4*PT_CCR, &flags) < 0)
854 return -1;
855 if (upeek(pid, 4*PT_R3, &result) < 0)
856 return -1;
857 if (flags & SO_MASK)
858 result = -result;
859#elif defined (M68K)
860 if (upeek(pid, 4*PT_D0, &d0) < 0)
861 return -1;
862 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
863 if (debug)
864 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
865 return 0;
866 }
867#elif defined (ARM)
868 if (upeek(pid, 4*0, (long *)&r0) < 0)
869 return -1;
870 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
871 if (debug)
872 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
873 return 0;
874 }
875#else
876#endif
877#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000878 return 1;
879}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000880
Pavel Machek4dc3b142000-02-01 17:58:41 +0000881int
882get_error(tcp)
883struct tcb *tcp;
884{
885 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000886#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000887#ifdef S390
888 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
889 tcp->u_rval = -1;
890 u_error = -gpr2;
891 }
892 else {
893 tcp->u_rval = gpr2;
894 u_error = 0;
895 }
896#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000897#ifdef I386
898 if (eax < 0 && -eax < nerrnos) {
899 tcp->u_rval = -1;
900 u_error = -eax;
901 }
902 else {
903 tcp->u_rval = eax;
904 u_error = 0;
905 }
906#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000907#ifdef IA64
908 if (ia32) {
909 int err;
910
911 err = (int)r8;
912 if (err < 0 && -err < nerrnos) {
913 tcp->u_rval = -1;
914 u_error = -err;
915 }
916 else {
917 tcp->u_rval = err;
918 u_error = 0;
919 }
920 } else {
921 if (r10) {
922 tcp->u_rval = -1;
923 u_error = r8;
924 } else {
925 tcp->u_rval = r8;
926 u_error = 0;
927 }
928 }
929#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000930#ifdef MIPS
931 if (a3) {
932 tcp->u_rval = -1;
933 u_error = r2;
934 } else {
935 tcp->u_rval = r2;
936 u_error = 0;
937 }
938#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000939#ifdef POWERPC
940 if (result && (unsigned) -result < nerrnos) {
941 tcp->u_rval = -1;
942 u_error = -result;
943 }
944 else {
945 tcp->u_rval = result;
946 u_error = 0;
947 }
948#else /* !POWERPC */
949#ifdef M68K
950 if (d0 && (unsigned) -d0 < nerrnos) {
951 tcp->u_rval = -1;
952 u_error = -d0;
953 }
954 else {
955 tcp->u_rval = d0;
956 u_error = 0;
957 }
958#else /* !M68K */
959#ifdef ARM
960 if (r0 && (unsigned) -r0 < nerrnos) {
961 tcp->u_rval = -1;
962 u_error = -r0;
963 }
964 else {
965 tcp->u_rval = r0;
966 u_error = 0;
967 }
968#else /* !ARM */
969#ifdef ALPHA
970 if (a3) {
971 tcp->u_rval = -1;
972 u_error = r0;
973 }
974 else {
975 tcp->u_rval = r0;
976 u_error = 0;
977 }
978#else /* !ALPHA */
979#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000980 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000981 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000982 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000983 }
984 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000985 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000986 u_error = 0;
987 }
988#endif /* SPARC */
989#endif /* ALPHA */
990#endif /* ARM */
991#endif /* M68K */
992#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000993#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000994#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000995#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000996#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000997#endif /* LINUX */
998#ifdef SUNOS4
999 /* get error code from user struct */
1000 if (upeek(pid, uoff(u_error), &u_error) < 0)
1001 return -1;
1002 u_error >>= 24; /* u_error is a char */
1003
1004 /* get system call return value */
1005 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1006 return -1;
1007#endif /* SUNOS4 */
1008#ifdef SVR4
1009#ifdef SPARC
1010 /* Judicious guessing goes a long way. */
1011 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1012 tcp->u_rval = -1;
1013 u_error = tcp->status.pr_reg[R_O0];
1014 }
1015 else {
1016 tcp->u_rval = tcp->status.pr_reg[R_O0];
1017 u_error = 0;
1018 }
1019#endif /* SPARC */
1020#ifdef I386
1021 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001022 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001023 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001024 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001025 }
1026 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001027 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001028 u_error = 0;
1029 }
1030#endif /* I386 */
1031#ifdef MIPS
1032 if (tcp->status.pr_reg[CTX_A3]) {
1033 tcp->u_rval = -1;
1034 u_error = tcp->status.pr_reg[CTX_V0];
1035 }
1036 else {
1037 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1038 u_error = 0;
1039 }
1040#endif /* MIPS */
1041#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001042 tcp->u_error = u_error;
1043 return 1;
1044}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001045
Pavel Machek4dc3b142000-02-01 17:58:41 +00001046int syscall_enter(tcp)
1047struct tcb *tcp;
1048{
1049 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001050#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001051#if defined(S390)
1052 {
1053 int i;
1054 tcp->u_nargs = sysent[tcp->scno].nargs;
1055 for (i = 0; i < tcp->u_nargs; i++) {
1056 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1057 return -1;
1058 }
1059 }
1060#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001061 {
1062 int i;
1063 tcp->u_nargs = sysent[tcp->scno].nargs;
1064 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001065 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1066 * for scno somewhere above here!
1067 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001068 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1069 return -1;
1070 }
1071 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001072#elif defined (IA64)
1073 {
1074 unsigned long *bsp, i;
1075
1076 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1077 return -1;
1078
1079 tcp->u_nargs = sysent[tcp->scno].nargs;
1080 for (i = 0; i < tcp->u_nargs; ++i) {
1081 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1082 (char *) &tcp->u_arg[i])
1083 < 0)
1084 return -1;
1085 }
1086 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001087#elif defined (MIPS)
1088 {
1089 long sp;
1090 int i, nargs;
1091
1092 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1093 if(nargs > 4) {
1094 if(upeek(pid, REG_SP, &sp) < 0)
1095 return -1;
1096 for(i = 0; i < 4; i++) {
1097 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1098 return -1;
1099 }
1100 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1101 (char *)(tcp->u_arg + 4));
1102 } else {
1103 for(i = 0; i < nargs; i++) {
1104 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1105 return -1;
1106 }
1107 }
1108 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001109#elif defined (POWERPC)
1110 {
1111 int i;
1112 tcp->u_nargs = sysent[tcp->scno].nargs;
1113 for (i = 0; i < tcp->u_nargs; i++) {
1114 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1115 return -1;
1116 }
1117 }
1118#elif defined (SPARC)
1119 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001120 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001121
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001122 tcp->u_nargs = sysent[tcp->scno].nargs;
1123 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001124 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001125 }
1126#else
1127 {
1128 int i;
1129 tcp->u_nargs = sysent[tcp->scno].nargs;
1130 for (i = 0; i < tcp->u_nargs; i++) {
1131 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1132 return -1;
1133 }
1134 }
1135#endif
1136#endif /* LINUX */
1137#ifdef SUNOS4
1138 {
1139 int i;
1140 tcp->u_nargs = sysent[tcp->scno].nargs;
1141 for (i = 0; i < tcp->u_nargs; i++) {
1142 struct user *u;
1143
1144 if (upeek(pid, uoff(u_arg[0]) +
1145 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1146 return -1;
1147 }
1148 }
1149#endif /* SUNOS4 */
1150#ifdef SVR4
1151#ifdef MIPS
1152 /*
1153 * SGI is broken: even though it has pr_sysarg, it doesn't
1154 * set them on system call entry. Get a clue.
1155 */
1156 if (sysent[tcp->scno].nargs != -1)
1157 tcp->u_nargs = sysent[tcp->scno].nargs;
1158 else
1159 tcp->u_nargs = tcp->status.pr_nsysarg;
1160 if (tcp->u_nargs > 4) {
1161 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1162 4*sizeof(tcp->u_arg[0]));
1163 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1164 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1165 }
1166 else {
1167 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1168 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1169 }
1170#else /* !MIPS */
1171#ifdef HAVE_PR_SYSCALL
1172 if (sysent[tcp->scno].nargs != -1)
1173 tcp->u_nargs = sysent[tcp->scno].nargs;
1174 else
1175 tcp->u_nargs = tcp->status.pr_nsysarg;
1176 {
1177 int i;
1178 for (i = 0; i < tcp->u_nargs; i++)
1179 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1180 }
1181#else /* !HAVE_PR_SYSCALL */
1182#ifdef I386
1183 if (sysent[tcp->scno].nargs != -1)
1184 tcp->u_nargs = sysent[tcp->scno].nargs;
1185 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001186#if UNIXWARE >= 2
1187 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1188#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001189 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001190#endif
1191 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001192 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1193#endif /* I386 */
1194#endif /* !HAVE_PR_SYSCALL */
1195#endif /* !MIPS */
1196#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001197 return 1;
1198}
1199
1200int
1201trace_syscall(tcp)
1202struct tcb *tcp;
1203{
1204 int sys_res;
1205 struct timeval tv;
1206 int res;
1207
1208 /* Measure the exit time as early as possible to avoid errors. */
1209 if (dtime && (tcp->flags & TCB_INSYSCALL))
1210 gettimeofday(&tv, NULL);
1211
1212 res = get_scno(tcp);
1213 if (res != 1)
1214 return res;
1215
1216 res = syscall_fixup(tcp);
1217 if (res != 1)
1218 return res;
1219
1220 if (tcp->flags & TCB_INSYSCALL) {
1221 long u_error;
1222 res = get_error(tcp);
1223 if (res != 1)
1224 return res;
1225 u_error = tcp->u_error;
1226
1227
1228 internal_syscall(tcp);
1229 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1230 tcp->flags &= ~TCB_INSYSCALL;
1231 return 0;
1232 }
1233
1234 if (tcp->flags & TCB_REPRINT) {
1235 printleader(tcp);
1236 tprintf("<... ");
1237 if (tcp->scno >= nsyscalls)
1238 tprintf("syscall_%lu", tcp->scno);
1239 else
1240 tprintf("%s", sysent[tcp->scno].sys_name);
1241 tprintf(" resumed> ");
1242 }
1243
1244 if (cflag) {
1245 call_count[tcp->scno]++;
1246 if (tcp->u_error)
1247 error_count[tcp->scno]++;
1248 tv_sub(&tv, &tv, &tcp->etime);
1249#ifdef LINUX
1250 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1251 static struct timeval one_tick =
1252 { 0, 1000000 / HZ };
1253
1254 if (tv_nz(&tcp->dtime))
1255 tv = tcp->dtime;
1256 else if (tv_cmp(&tv, &one_tick) > 0) {
1257 if (tv_cmp(&shortest, &one_tick) < 0)
1258 tv = shortest;
1259 else
1260 tv = one_tick;
1261 }
1262 }
1263#endif /* LINUX */
1264 if (tv_cmp(&tv, &shortest) < 0)
1265 shortest = tv;
1266 tv_add(&tv_count[tcp->scno],
1267 &tv_count[tcp->scno], &tv);
1268 tcp->flags &= ~TCB_INSYSCALL;
1269 return 0;
1270 }
1271
1272 if (tcp->scno >= nsyscalls
1273 || (qual_flags[tcp->scno] & QUAL_RAW))
1274 sys_res = printargs(tcp);
1275 else
1276 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1277 u_error = tcp->u_error;
1278 tprintf(") ");
1279 tabto(acolumn);
1280 if (qual_flags[tcp->scno] & QUAL_RAW) {
1281 if (u_error)
1282 tprintf("= -1 (errno %ld)", u_error);
1283 else
1284 tprintf("= %#lx", tcp->u_rval);
1285 }
1286 else if (!(sys_res & RVAL_NONE) && u_error) {
1287 switch (u_error) {
1288#ifdef LINUX
1289 case ERESTARTSYS:
1290 tprintf("= ? ERESTARTSYS (To be restarted)");
1291 break;
1292 case ERESTARTNOINTR:
1293 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1294 break;
1295 case ERESTARTNOHAND:
1296 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1297 break;
1298#endif /* LINUX */
1299 default:
1300 tprintf("= -1 ");
1301 if (u_error < nerrnos && u_error < sys_nerr)
1302 tprintf("%s (%s)", errnoent[u_error],
1303 sys_errlist[u_error]);
1304 else if (u_error < nerrnos)
1305 tprintf("%s (errno %ld)",
1306 errnoent[u_error], u_error);
1307 else if (u_error < sys_nerr)
1308 tprintf("ERRNO_%ld (%s)", u_error,
1309 sys_errlist[u_error]);
1310 else
1311 tprintf("E??? (errno %ld)", u_error);
1312 break;
1313 }
1314 }
1315 else {
1316 if (sys_res & RVAL_NONE)
1317 tprintf("= ?");
1318 else {
1319 switch (sys_res & RVAL_MASK) {
1320 case RVAL_HEX:
1321 tprintf("= %#lx", tcp->u_rval);
1322 break;
1323 case RVAL_OCTAL:
1324 tprintf("= %#lo", tcp->u_rval);
1325 break;
1326 case RVAL_UDECIMAL:
1327 tprintf("= %lu", tcp->u_rval);
1328 break;
1329 case RVAL_DECIMAL:
1330 tprintf("= %ld", tcp->u_rval);
1331 break;
1332 default:
1333 fprintf(stderr,
1334 "invalid rval format\n");
1335 break;
1336 }
1337 }
1338 if ((sys_res & RVAL_STR) && tcp->auxstr)
1339 tprintf(" (%s)", tcp->auxstr);
1340 }
1341 if (dtime) {
1342 tv_sub(&tv, &tv, &tcp->etime);
1343 tprintf(" <%ld.%06ld>",
1344 (long) tv.tv_sec, (long) tv.tv_usec);
1345 }
1346 printtrailer(tcp);
1347
1348 dumpio(tcp);
1349 if (fflush(tcp->outf) == EOF)
1350 return -1;
1351 tcp->flags &= ~TCB_INSYSCALL;
1352 return 0;
1353 }
1354
1355 /* Entering system call */
1356 res = syscall_enter(tcp);
1357 if (res != 1)
1358 return res;
1359
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001360 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361#ifdef LINUX
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001362#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001363 case SYS_socketcall:
1364 decode_subcall(tcp, SYS_socket_subcall,
1365 SYS_socket_nsubcalls, deref_style);
1366 break;
1367 case SYS_ipc:
1368 decode_subcall(tcp, SYS_ipc_subcall,
1369 SYS_ipc_nsubcalls, shift_style);
1370 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001371#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001372#ifdef SPARC
1373 case SYS_socketcall:
1374 sparc_socket_decode (tcp);
1375 break;
1376#endif
1377#endif /* LINUX */
1378#ifdef SVR4
1379#ifdef SYS_pgrpsys_subcall
1380 case SYS_pgrpsys:
1381 decode_subcall(tcp, SYS_pgrpsys_subcall,
1382 SYS_pgrpsys_nsubcalls, shift_style);
1383 break;
1384#endif /* SYS_pgrpsys_subcall */
1385#ifdef SYS_sigcall_subcall
1386 case SYS_sigcall:
1387 decode_subcall(tcp, SYS_sigcall_subcall,
1388 SYS_sigcall_nsubcalls, mask_style);
1389 break;
1390#endif /* SYS_sigcall_subcall */
1391 case SYS_msgsys:
1392 decode_subcall(tcp, SYS_msgsys_subcall,
1393 SYS_msgsys_nsubcalls, shift_style);
1394 break;
1395 case SYS_shmsys:
1396 decode_subcall(tcp, SYS_shmsys_subcall,
1397 SYS_shmsys_nsubcalls, shift_style);
1398 break;
1399 case SYS_semsys:
1400 decode_subcall(tcp, SYS_semsys_subcall,
1401 SYS_semsys_nsubcalls, shift_style);
1402 break;
1403#if 0 /* broken */
1404 case SYS_utssys:
1405 decode_subcall(tcp, SYS_utssys_subcall,
1406 SYS_utssys_nsubcalls, shift_style);
1407 break;
1408#endif
1409 case SYS_sysfs:
1410 decode_subcall(tcp, SYS_sysfs_subcall,
1411 SYS_sysfs_nsubcalls, shift_style);
1412 break;
1413 case SYS_spcall:
1414 decode_subcall(tcp, SYS_spcall_subcall,
1415 SYS_spcall_nsubcalls, shift_style);
1416 break;
1417#ifdef SYS_context_subcall
1418 case SYS_context:
1419 decode_subcall(tcp, SYS_context_subcall,
1420 SYS_context_nsubcalls, shift_style);
1421 break;
1422#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001423#ifdef SYS_door_subcall
1424 case SYS_door:
1425 decode_subcall(tcp, SYS_door_subcall,
1426 SYS_door_nsubcalls, door_style);
1427 break;
1428#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001429#endif /* SVR4 */
1430#ifdef SUNOS4
1431 case SYS_semsys:
1432 decode_subcall(tcp, SYS_semsys_subcall,
1433 SYS_semsys_nsubcalls, shift_style);
1434 break;
1435 case SYS_msgsys:
1436 decode_subcall(tcp, SYS_msgsys_subcall,
1437 SYS_msgsys_nsubcalls, shift_style);
1438 break;
1439 case SYS_shmsys:
1440 decode_subcall(tcp, SYS_shmsys_subcall,
1441 SYS_shmsys_nsubcalls, shift_style);
1442 break;
1443#endif
1444 }
1445
1446 internal_syscall(tcp);
1447 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1448 tcp->flags |= TCB_INSYSCALL;
1449 return 0;
1450 }
1451
1452 if (cflag) {
1453 gettimeofday(&tcp->etime, NULL);
1454 tcp->flags |= TCB_INSYSCALL;
1455 return 0;
1456 }
1457
1458 printleader(tcp);
1459 tcp->flags &= ~TCB_REPRINT;
1460 tcp_last = tcp;
1461 if (tcp->scno >= nsyscalls)
1462 tprintf("syscall_%lu(", tcp->scno);
1463 else
1464 tprintf("%s(", sysent[tcp->scno].sys_name);
1465 if (tcp->scno >= nsyscalls ||
1466 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1467 sys_res = printargs(tcp);
1468 else
1469 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1470 if (fflush(tcp->outf) == EOF)
1471 return -1;
1472 tcp->flags |= TCB_INSYSCALL;
1473 /* Measure the entrance time as late as possible to avoid errors. */
1474 if (dtime)
1475 gettimeofday(&tcp->etime, NULL);
1476 return sys_res;
1477}
1478
1479int
1480printargs(tcp)
1481struct tcb *tcp;
1482{
1483 if (entering(tcp)) {
1484 int i;
1485
1486 for (i = 0; i < tcp->u_nargs; i++)
1487 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1488 }
1489 return 0;
1490}
1491
1492long
1493getrval2(tcp)
1494struct tcb *tcp;
1495{
1496 long val = -1;
1497
1498#ifdef LINUX
1499#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001500 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001501 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1502 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001503 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001504#endif /* SPARC */
1505#endif /* LINUX */
1506
1507#ifdef SUNOS4
1508 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1509 return -1;
1510#endif /* SUNOS4 */
1511
1512#ifdef SVR4
1513#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001514 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001515#endif /* SPARC */
1516#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001517 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001518#endif /* I386 */
1519#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001520 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001521#endif /* MIPS */
1522#endif /* SVR4 */
1523
1524 return val;
1525}
1526
1527/*
1528 * Apparently, indirect system calls have already be converted by ptrace(2),
1529 * so if you see "indir" this program has gone astray.
1530 */
1531int
1532sys_indir(tcp)
1533struct tcb *tcp;
1534{
1535 int i, scno, nargs;
1536
1537 if (entering(tcp)) {
1538 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1539 fprintf(stderr, "Bogus syscall: %u\n", scno);
1540 return 0;
1541 }
1542 nargs = sysent[scno].nargs;
1543 tprintf("%s", sysent[scno].sys_name);
1544 for (i = 0; i < nargs; i++)
1545 tprintf(", %#lx", tcp->u_arg[i+1]);
1546 }
1547 return 0;
1548}
1549
1550static int
1551time_cmp(a, b)
1552void *a;
1553void *b;
1554{
1555 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1556}
1557
1558static int
1559syscall_cmp(a, b)
1560void *a;
1561void *b;
1562{
1563 return strcmp(sysent[*((int *) a)].sys_name,
1564 sysent[*((int *) b)].sys_name);
1565}
1566
1567static int
1568count_cmp(a, b)
1569void *a;
1570void *b;
1571{
1572 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1573
1574 return (m < n) ? 1 : (m > n) ? -1 : 0;
1575}
1576
1577static int (*sortfun)();
1578static struct timeval overhead = { -1, -1 };
1579
1580void
1581set_sortby(sortby)
1582char *sortby;
1583{
1584 if (strcmp(sortby, "time") == 0)
1585 sortfun = time_cmp;
1586 else if (strcmp(sortby, "calls") == 0)
1587 sortfun = count_cmp;
1588 else if (strcmp(sortby, "name") == 0)
1589 sortfun = syscall_cmp;
1590 else if (strcmp(sortby, "nothing") == 0)
1591 sortfun = NULL;
1592 else {
1593 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1594 exit(1);
1595 }
1596}
1597
1598void set_overhead(n)
1599int n;
1600{
1601 overhead.tv_sec = n / 1000000;
1602 overhead.tv_usec = n % 1000000;
1603}
1604
1605void
1606call_summary(outf)
1607FILE *outf;
1608{
1609 int i, j;
1610 int call_cum, error_cum;
1611 struct timeval tv_cum, dtv;
1612 double percent;
1613 char *dashes = "-------------------------";
1614 char error_str[16];
1615
1616 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1617 if (overhead.tv_sec == -1) {
1618 tv_mul(&overhead, &shortest, 8);
1619 tv_div(&overhead, &overhead, 10);
1620 }
1621 for (i = 0; i < nsyscalls; i++) {
1622 sorted_count[i] = i;
1623 if (call_count[i] == 0)
1624 continue;
1625 tv_mul(&dtv, &overhead, call_count[i]);
1626 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1627 call_cum += call_count[i];
1628 error_cum += error_count[i];
1629 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1630 }
1631 if (sortfun)
1632 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1633 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1634 "% time", "seconds", "usecs/call",
1635 "calls", "errors", "syscall");
1636 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1637 dashes, dashes, dashes, dashes, dashes, dashes);
1638 for (i = 0; i < nsyscalls; i++) {
1639 j = sorted_count[i];
1640 if (call_count[j] == 0)
1641 continue;
1642 tv_div(&dtv, &tv_count[j], call_count[j]);
1643 if (error_count[j])
1644 sprintf(error_str, "%d", error_count[j]);
1645 else
1646 error_str[0] = '\0';
1647 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1648 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1649 percent, (long) tv_count[j].tv_sec,
1650 (long) tv_count[j].tv_usec,
1651 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1652 call_count[j], error_str, sysent[j].sys_name);
1653 }
1654 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1655 dashes, dashes, dashes, dashes, dashes, dashes);
1656 if (error_cum)
1657 sprintf(error_str, "%d", error_cum);
1658 else
1659 error_str[0] = '\0';
1660 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1661 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1662 call_cum, error_str, "total");
1663}