blob: 47bf16d6fbfd126be156d92293d4c4d9414fc3ac [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id$
34 */
35
36#include "defs.h"
37
38#include <signal.h>
39#include <time.h>
40#include <errno.h>
41#include <sys/user.h>
42#include <sys/syscall.h>
43#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044
Wichert Akkerman15dea971999-10-06 13:06:34 +000045#if HAVE_ASM_REG_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000046#include <asm/reg.h>
47#endif
48
Wichert Akkerman15dea971999-10-06 13:06:34 +000049#ifdef HAVE_SYS_REG_H
50#include <sys/reg.h>
51#ifndef PTRACE_PEEKUSR
52# define PTRACE_PEEKUSR PTRACE_PEEKUSER
53#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000054#elif defined(HAVE_LINUX_PTRACE_H)
55#undef PTRACE_SYSCALL
56#include <linux/ptrace.h>
Wichert Akkerman15dea971999-10-06 13:06:34 +000057#endif
58
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000059#if defined(LINUX) && defined(IA64)
60# include <asm/ptrace_offsets.h>
61# include <asm/rse.h>
62#endif
63
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#ifndef SYS_ERRLIST_DECLARED
65extern int sys_nerr;
66extern char *sys_errlist[];
67#endif /* SYS_ERRLIST_DECLARED */
68
Pavel Machekd8ae7e32000-02-01 17:17:25 +000069#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000070#ifdef LINUX
71#ifndef ERESTARTSYS
72#define ERESTARTSYS 512
73#endif
74#ifndef ERESTARTNOINTR
75#define ERESTARTNOINTR 513
76#endif
77#ifndef ERESTARTNOHAND
78#define ERESTARTNOHAND 514 /* restart if no handler.. */
79#endif
80#ifndef ENOIOCTLCMD
81#define ENOIOCTLCMD 515 /* No ioctl command */
82#endif
83#ifndef NSIG
84#define NSIG 32
85#endif
86#ifdef ARM
87#undef NSIG
88#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000089#undef NR_SYSCALL_BASE
90#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
92#endif /* LINUX */
93
94#include "syscall.h"
95
96/* Define these shorthand notations to simplify the syscallent files. */
97#define TF TRACE_FILE
98#define TI TRACE_IPC
99#define TN TRACE_NETWORK
100#define TP TRACE_PROCESS
101#define TS TRACE_SIGNAL
102
103struct sysent sysent0[] = {
104#include "syscallent.h"
105};
106int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
107
108#if SUPPORTED_PERSONALITIES >= 2
109struct sysent sysent1[] = {
110#include "syscallent1.h"
111};
112int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
113#endif /* SUPPORTED_PERSONALITIES >= 2 */
114
115#if SUPPORTED_PERSONALITIES >= 3
116struct sysent sysent2[] = {
117#include "syscallent2.h"
118};
119int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
120#endif /* SUPPORTED_PERSONALITIES >= 3 */
121
122struct sysent *sysent;
123int nsyscalls;
124
125/* Now undef them since short defines cause wicked namespace pollution. */
126#undef TF
127#undef TI
128#undef TN
129#undef TP
130#undef TS
131
132char *errnoent0[] = {
133#include "errnoent.h"
134};
135int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
136
137#if SUPPORTED_PERSONALITIES >= 2
138char *errnoent1[] = {
139#include "errnoent1.h"
140};
141int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
142#endif /* SUPPORTED_PERSONALITIES >= 2 */
143
144#if SUPPORTED_PERSONALITIES >= 3
145char *errnoent2[] = {
146#include "errnoent2.h"
147};
148int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
149#endif /* SUPPORTED_PERSONALITIES >= 3 */
150
151char **errnoent;
152int nerrnos;
153
154int current_personality;
155
156int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000157set_personality(personality)
158int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159{
160 switch (personality) {
161 case 0:
162 errnoent = errnoent0;
163 nerrnos = nerrnos0;
164 sysent = sysent0;
165 nsyscalls = nsyscalls0;
166 ioctlent = ioctlent0;
167 nioctlents = nioctlents0;
168 signalent = signalent0;
169 nsignals = nsignals0;
170 break;
171
172#if SUPPORTED_PERSONALITIES >= 2
173 case 1:
174 errnoent = errnoent1;
175 nerrnos = nerrnos1;
176 sysent = sysent1;
177 nsyscalls = nsyscalls1;
178 ioctlent = ioctlent1;
179 nioctlents = nioctlents1;
180 signalent = signalent1;
181 nsignals = nsignals1;
182 break;
183#endif /* SUPPORTED_PERSONALITIES >= 2 */
184
185#if SUPPORTED_PERSONALITIES >= 3
186 case 2:
187 errnoent = errnoent2;
188 nerrnos = nerrnos2;
189 sysent = sysent2;
190 nsyscalls = nsyscalls2;
191 ioctlent = ioctlent2;
192 nioctlents = nioctlents2;
193 signalent = signalent2;
194 nsignals = nsignals2;
195 break;
196#endif /* SUPPORTED_PERSONALITIES >= 3 */
197
198 default:
199 return -1;
200 }
201
202 current_personality = personality;
203 return 0;
204}
205
206int qual_flags[MAX_QUALS];
207
208static int call_count[MAX_QUALS];
209static int error_count[MAX_QUALS];
210static struct timeval tv_count[MAX_QUALS];
211static int sorted_count[MAX_QUALS];
212
213static struct timeval shortest = { 1000000, 0 };
214
215static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
216
217static struct qual_options {
218 int bitflag;
219 char *option_name;
220 int (*lookup)();
221 char *argument_name;
222} qual_options[] = {
223 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
224 { QUAL_TRACE, "t", lookup_syscall, "system call" },
225 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
226 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
227 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
228 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
229 { QUAL_RAW, "raw", lookup_syscall, "system call" },
230 { QUAL_RAW, "x", lookup_syscall, "system call" },
231 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
232 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
233 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
234 { QUAL_FAULT, "fault", lookup_fault, "fault" },
235 { QUAL_FAULT, "faults", lookup_fault, "fault" },
236 { QUAL_FAULT, "m", lookup_fault, "fault" },
237 { QUAL_READ, "read", lookup_desc, "descriptor" },
238 { QUAL_READ, "reads", lookup_desc, "descriptor" },
239 { QUAL_READ, "r", lookup_desc, "descriptor" },
240 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
241 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
242 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
243 { 0, NULL, NULL, NULL },
244};
245
246static int
247lookup_syscall(s)
248char *s;
249{
250 int i;
251
252 for (i = 0; i < nsyscalls; i++) {
253 if (strcmp(s, sysent[i].sys_name) == 0)
254 return i;
255 }
256 return -1;
257}
258
259static int
260lookup_signal(s)
261char *s;
262{
263 int i;
264 char buf[32];
265
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000266 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267 return atoi(s);
268 strcpy(buf, s);
269 s = buf;
270 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000271 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 if (strncmp(s, "SIG", 3) == 0)
273 s += 3;
274 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000275 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000276 return i;
277 }
278 return -1;
279}
280
281static int
282lookup_fault(s)
283char *s;
284{
285 return -1;
286}
287
288static int
289lookup_desc(s)
290char *s;
291{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000292 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 return atoi(s);
294 return -1;
295}
296
297static int
298lookup_class(s)
299char *s;
300{
301 if (strcmp(s, "file") == 0)
302 return TRACE_FILE;
303 if (strcmp(s, "ipc") == 0)
304 return TRACE_IPC;
305 if (strcmp(s, "network") == 0)
306 return TRACE_NETWORK;
307 if (strcmp(s, "process") == 0)
308 return TRACE_PROCESS;
309 if (strcmp(s, "signal") == 0)
310 return TRACE_SIGNAL;
311 return -1;
312}
313
314void
315qualify(s)
316char *s;
317{
318 struct qual_options *opt;
319 int not;
320 char *p;
321 int i, n;
322
323 opt = &qual_options[0];
324 for (i = 0; (p = qual_options[i].option_name); i++) {
325 n = strlen(p);
326 if (strncmp(s, p, n) == 0 && s[n] == '=') {
327 opt = &qual_options[i];
328 s += n + 1;
329 break;
330 }
331 }
332 not = 0;
333 if (*s == '!') {
334 not = 1;
335 s++;
336 }
337 if (strcmp(s, "none") == 0) {
338 not = 1 - not;
339 s = "all";
340 }
341 if (strcmp(s, "all") == 0) {
342 for (i = 0; i < MAX_QUALS; i++) {
343 if (not)
344 qual_flags[i] &= ~opt->bitflag;
345 else
346 qual_flags[i] |= opt->bitflag;
347 }
348 return;
349 }
350 for (i = 0; i < MAX_QUALS; i++) {
351 if (not)
352 qual_flags[i] |= opt->bitflag;
353 else
354 qual_flags[i] &= ~opt->bitflag;
355 }
356 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
357 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
358 for (i = 0; i < MAX_QUALS; i++) {
359 if (sysent[i].sys_flags & n) {
360 if (not)
361 qual_flags[i] &= ~opt->bitflag;
362 else
363 qual_flags[i] |= opt->bitflag;
364 }
365 }
366 continue;
367 }
368 if ((n = (*opt->lookup)(p)) < 0) {
369 fprintf(stderr, "strace: invalid %s `%s'\n",
370 opt->argument_name, p);
371 exit(1);
372 }
373 if (not)
374 qual_flags[n] &= ~opt->bitflag;
375 else
376 qual_flags[n] |= opt->bitflag;
377 }
378 return;
379}
380
381static void
382dumpio(tcp)
383struct tcb *tcp;
384{
385 if (syserror(tcp))
386 return;
387 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
388 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000389 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390 case SYS_read:
391#ifdef SYS_recv
392 case SYS_recv:
393#endif
394#ifdef SYS_recvfrom
395 case SYS_recvfrom:
396#endif
397 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
398 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
399 break;
400 case SYS_write:
401#ifdef SYS_send
402 case SYS_send:
403#endif
404#ifdef SYS_sendto
405 case SYS_sendto:
406#endif
407 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
409 break;
410 }
411}
412
Wichert Akkerman8829a551999-06-11 13:18:40 +0000413enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000414
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000415#if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000416
417const int socket_map [] = {
418 /* SYS_SOCKET */ 97,
419 /* SYS_BIND */ 104,
420 /* SYS_CONNECT */ 98,
421 /* SYS_LISTEN */ 106,
422 /* SYS_ACCEPT */ 99,
423 /* SYS_GETSOCKNAME */ 150,
424 /* SYS_GETPEERNAME */ 141,
425 /* SYS_SOCKETPAIR */ 135,
426 /* SYS_SEND */ 101,
427 /* SYS_RECV */ 102,
428 /* SYS_SENDTO */ 133,
429 /* SYS_RECVFROM */ 125,
430 /* SYS_SHUTDOWN */ 134,
431 /* SYS_SETSOCKOPT */ 105,
432 /* SYS_GETSOCKOPT */ 118,
433 /* SYS_SENDMSG */ 114,
434 /* SYS_RECVMSG */ 113
435};
436
437void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000438sparc_socket_decode (tcp)
439struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000440{
441 volatile long addr;
442 volatile int i, n;
443
444 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
445 return;
446 }
447 tcp->scno = socket_map [tcp->u_arg [0]-1];
448 n = tcp->u_nargs = sysent [tcp->scno].nargs;
449 addr = tcp->u_arg [1];
450 for (i = 0; i < n; i++){
451 int arg;
452 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
453 arg = 0;
454 tcp->u_arg [i] = arg;
455 addr += sizeof (arg);
456 }
457}
458
459static void
460decode_subcall(tcp, subcall, nsubcalls, style)
461struct tcb *tcp;
462int subcall;
463int nsubcalls;
464enum subcall_style style;
465{
466 int i, addr, mask, arg;
467
468 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
469 return;
470 switch (style) {
471 case shift_style:
472 tcp->scno = subcall + tcp->u_arg[0];
473 if (sysent[tcp->scno].nargs != -1)
474 tcp->u_nargs = sysent[tcp->scno].nargs;
475 else
476 tcp->u_nargs--;
477 for (i = 0; i < tcp->u_nargs; i++)
478 tcp->u_arg[i] = tcp->u_arg[i + 1];
479 break;
480 case deref_style:
481 tcp->scno = subcall + tcp->u_arg[0];
482 addr = tcp->u_arg[1];
483 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
484 if (umove(tcp, addr, &arg) < 0)
485 arg = 0;
486 tcp->u_arg[i] = arg;
487 addr += sizeof(arg);
488 }
489 tcp->u_nargs = sysent[tcp->scno].nargs;
490 break;
491 case mask_style:
492 mask = (tcp->u_arg[0] >> 8) & 0xff;
493 tcp->u_arg[0] &= 0xff;
494 for (i = 0; mask; i++)
495 mask >>= 1;
496 tcp->scno = subcall + i;
497 if (sysent[tcp->scno].nargs != -1)
498 tcp->u_nargs = sysent[tcp->scno].nargs;
499 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000500 case door_style:
501 /*
502 * Oh, yuck. The call code is the *sixth* argument.
503 */
504 tcp->scno = subcall + tcp->u_arg[5];
505 if (sysent[tcp->scno].nargs != -1)
506 tcp->u_nargs = sysent[tcp->scno].nargs;
507 else
508 tcp->u_nargs--;
509 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510 }
511}
512#endif
513
514struct tcb *tcp_last = NULL;
515
516static int
517internal_syscall(tcp)
518struct tcb *tcp;
519{
520 /*
521 * We must always trace a few critical system calls in order to
522 * correctly support following forks in the presence of tracing
523 * qualifiers.
524 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000525 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526#ifdef SYS_fork
527 case SYS_fork:
528#endif
529#ifdef SYS_vfork
530 case SYS_vfork:
531#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000532 internal_fork(tcp);
533 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000534#ifdef SYS_clone
535 case SYS_clone:
536 internal_clone(tcp);
537 break;
538#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539#ifdef SYS_execv
540 case SYS_execv:
541#endif
542#ifdef SYS_execve
543 case SYS_execve:
544#endif
545 internal_exec(tcp);
546 break;
547
548#ifdef SYS_wait
549 case SYS_wait:
550#endif
551#ifdef SYS_wait4
552 case SYS_wait4:
553#endif
554#ifdef SYS_waitpid
555 case SYS_waitpid:
556#endif
557#ifdef SYS_waitsys
558 case SYS_waitsys:
559#endif
560 internal_wait(tcp);
561 break;
562
563#ifdef SYS_exit
564 case SYS_exit:
565#endif
566 internal_exit(tcp);
567 break;
568 }
569 return 0;
570}
571
Wichert Akkermanc7926982000-04-10 22:22:31 +0000572
573#ifdef LINUX
574#if defined (I386)
575 static long eax;
576#elif defined (IA64)
577 long r8, r10, psr;
578 long ia32 = 0;
579#elif defined (POWERPC)
580 static long result,flags;
581#elif defined (M68K)
582 static int d0;
583#elif defined (ARM)
584 static int r0;
585#elif defined (ALPHA)
586 static long r0;
587 static long a3;
588#elif defined (SPARC)
589 static struct pt_regs regs;
590 static unsigned long trap;
591#elif defined(MIPS)
592 static long a3;
593 static long r2;
594#elif defined(S390)
595 static long gpr2;
596 static long pc;
597#endif
598#endif /* LINUX */
599
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000600int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000601get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000602struct tcb *tcp;
603{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000604 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000605 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000606
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000608#if defined(S390)
609 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
610 return -1;
611 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
612 if (errno)
613 return -1;
614 scno&=0xFF;
615#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000616 if (upeek(pid, 4*PT_R0, &scno) < 0)
617 return -1;
618 if (!(tcp->flags & TCB_INSYSCALL)) {
619 /* Check if we return from execve. */
620 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
621 tcp->flags &= ~TCB_WAITEXECVE;
622 return 0;
623 }
624 }
625#elif defined (I386)
626 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
627 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000628#elif defined(IA64)
629#define IA64_PSR_IS ((long)1 << 34)
630 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
631 ia32 = (psr & IA64_PSR_IS);
632 if (!(tcp->flags & TCB_INSYSCALL)) {
633 if (ia32) {
634 if (upeek(pid, PT_R8, &scno) < 0)
635 return -1;
636 } else {
637 if (upeek (pid, PT_R15, &scno) < 0)
638 return -1;
639 }
640 } else {
641 /* syscall in progress */
642 if (upeek (pid, PT_R8, &r8) < 0)
643 return -1;
644 if (upeek (pid, PT_R10, &r10) < 0)
645 return -1;
646 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000647#elif defined (ARM)
648 {
649 long pc;
650 upeek(pid, 4*15, &pc);
651 umoven(tcp, pc-4, 4, (char *)&scno);
652 scno &= 0x000fffff;
653 }
654#elif defined (M68K)
655 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
656 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000657#elif defined (MIPS)
658 if (upeek(pid, REG_A3, &a3) < 0)
659 return -1;
660
661 if(!(tcp->flags & TCB_INSYSCALL)) {
662 if (upeek(pid, REG_V0, &scno) < 0)
663 return -1;
664
665 if (scno < 0 || scno > nsyscalls) {
666 if(a3 == 0 || a3 == -1) {
667 if(debug)
668 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
669 return 0;
670 }
671 }
672 } else {
673 if (upeek(pid, REG_V0, &r2) < 0)
674 return -1;
675 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000676#elif defined (ALPHA)
677 if (upeek(pid, REG_A3, &a3) < 0)
678 return -1;
679
680 if (!(tcp->flags & TCB_INSYSCALL)) {
681 if (upeek(pid, REG_R0, &scno) < 0)
682 return -1;
683
684 /* Check if we return from execve. */
685 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
686 tcp->flags &= ~TCB_WAITEXECVE;
687 return 0;
688 }
689
690 /*
691 * Do some sanity checks to figure out if it's
692 * really a syscall entry
693 */
694 if (scno < 0 || scno > nsyscalls) {
695 if (a3 == 0 || a3 == -1) {
696 if (debug)
697 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
698 return 0;
699 }
700 }
701 }
702 else {
703 if (upeek(pid, REG_R0, &r0) < 0)
704 return -1;
705 }
706#elif defined (SPARC)
707 /* Everything we need is in the current register set. */
708 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
709 return -1;
710
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000711 /* If we are entering, then disassemble the syscall trap. */
712 if (!(tcp->flags & TCB_INSYSCALL)) {
713 /* Retrieve the syscall trap instruction. */
714 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000715 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000716 if (errno)
717 return -1;
718
719 /* Disassemble the trap to see what personality to use. */
720 switch (trap) {
721 case 0x91d02010:
722 /* Linux/SPARC syscall trap. */
723 set_personality(0);
724 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000725 case 0x91d0206d:
726 /* Linux/SPARC64 syscall trap. */
727 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
728 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000729 case 0x91d02000:
730 /* SunOS syscall trap. (pers 1) */
731 fprintf(stderr,"syscall: SunOS no support\n");
732 return -1;
733 case 0x91d02008:
734 /* Solaris 2.x syscall trap. (per 2) */
735 set_personality(1);
736 break;
737 case 0x91d02009:
738 /* NetBSD/FreeBSD syscall trap. */
739 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
740 return -1;
741 case 0x91d02027:
742 /* Solaris 2.x gettimeofday */
743 set_personality(1);
744 break;
745 default:
746 /* Unknown syscall trap. */
747 if(tcp->flags & TCB_WAITEXECVE) {
748 tcp->flags &= ~TCB_WAITEXECVE;
749 return 0;
750 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000751 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000752 return -1;
753 }
754
755 /* Extract the system call number from the registers. */
756 if (trap == 0x91d02027)
757 scno = 156;
758 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000759 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000760 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000761 scno = regs.r_o0;
762 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000763 }
764 }
765#endif
766#endif /* LINUX */
767#ifdef SUNOS4
768 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
769 return -1;
770#endif
771#ifdef SVR4
772#ifdef HAVE_PR_SYSCALL
773 scno = tcp->status.pr_syscall;
774#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000775 scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000776#endif /* !HAVE_PR_SYSCALL */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000777#endif
778 tcp->scno = scno;
779 return 1;
780}
781
Pavel Machek4dc3b142000-02-01 17:58:41 +0000782
783int
784syscall_fixup(tcp)
785struct tcb *tcp;
786{
787 int pid = tcp->pid;
788
789#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000790 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000791 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000792 if (
793 scno == SYS_fork
794#ifdef SYS_vfork
795 || scno == SYS_vfork
796#endif /* SYS_vfork */
797 ) {
798 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000799 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000800 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000801 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000802 }
803 else {
804 fprintf(stderr, "syscall: missing entry\n");
805 tcp->flags |= TCB_INSYSCALL;
806 }
807 }
808 }
809 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000810 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000811 fprintf(stderr, "syscall: missing exit\n");
812 tcp->flags &= ~TCB_INSYSCALL;
813 }
814 }
815#endif /* SVR4 */
816#ifdef SUNOS4
817 if (!(tcp->flags & TCB_INSYSCALL)) {
818 if (scno == 0) {
819 fprintf(stderr, "syscall: missing entry\n");
820 tcp->flags |= TCB_INSYSCALL;
821 }
822 }
823 else {
824 if (scno != 0) {
825 if (debug) {
826 /*
827 * This happens when a signal handler
828 * for a signal which interrupted a
829 * a system call makes another system call.
830 */
831 fprintf(stderr, "syscall: missing exit\n");
832 }
833 tcp->flags &= ~TCB_INSYSCALL;
834 }
835 }
836#endif /* SUNOS4 */
837#ifdef LINUX
838#if defined (I386)
839 if (upeek(pid, 4*EAX, &eax) < 0)
840 return -1;
841 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
842 if (debug)
843 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
844 return 0;
845 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000846#elif defined (S390)
847 if (upeek(pid, PT_GPR2, &gpr2) < 0)
848 return -1;
849 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
850 if (debug)
851 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
852 return 0;
853 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000854#elif defined (POWERPC)
855# define SO_MASK 0x10000000
856 if (upeek(pid, 4*PT_CCR, &flags) < 0)
857 return -1;
858 if (upeek(pid, 4*PT_R3, &result) < 0)
859 return -1;
860 if (flags & SO_MASK)
861 result = -result;
862#elif defined (M68K)
863 if (upeek(pid, 4*PT_D0, &d0) < 0)
864 return -1;
865 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
866 if (debug)
867 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
868 return 0;
869 }
870#elif defined (ARM)
871 if (upeek(pid, 4*0, (long *)&r0) < 0)
872 return -1;
873 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
874 if (debug)
875 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
876 return 0;
877 }
878#else
879#endif
880#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000881 return 1;
882}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000883
Pavel Machek4dc3b142000-02-01 17:58:41 +0000884int
885get_error(tcp)
886struct tcb *tcp;
887{
888 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000889#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000890#ifdef S390
891 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
892 tcp->u_rval = -1;
893 u_error = -gpr2;
894 }
895 else {
896 tcp->u_rval = gpr2;
897 u_error = 0;
898 }
899#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000900#ifdef I386
901 if (eax < 0 && -eax < nerrnos) {
902 tcp->u_rval = -1;
903 u_error = -eax;
904 }
905 else {
906 tcp->u_rval = eax;
907 u_error = 0;
908 }
909#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000910#ifdef IA64
911 if (ia32) {
912 int err;
913
914 err = (int)r8;
915 if (err < 0 && -err < nerrnos) {
916 tcp->u_rval = -1;
917 u_error = -err;
918 }
919 else {
920 tcp->u_rval = err;
921 u_error = 0;
922 }
923 } else {
924 if (r10) {
925 tcp->u_rval = -1;
926 u_error = r8;
927 } else {
928 tcp->u_rval = r8;
929 u_error = 0;
930 }
931 }
932#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000933#ifdef MIPS
934 if (a3) {
935 tcp->u_rval = -1;
936 u_error = r2;
937 } else {
938 tcp->u_rval = r2;
939 u_error = 0;
940 }
941#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000942#ifdef POWERPC
943 if (result && (unsigned) -result < nerrnos) {
944 tcp->u_rval = -1;
945 u_error = -result;
946 }
947 else {
948 tcp->u_rval = result;
949 u_error = 0;
950 }
951#else /* !POWERPC */
952#ifdef M68K
953 if (d0 && (unsigned) -d0 < nerrnos) {
954 tcp->u_rval = -1;
955 u_error = -d0;
956 }
957 else {
958 tcp->u_rval = d0;
959 u_error = 0;
960 }
961#else /* !M68K */
962#ifdef ARM
963 if (r0 && (unsigned) -r0 < nerrnos) {
964 tcp->u_rval = -1;
965 u_error = -r0;
966 }
967 else {
968 tcp->u_rval = r0;
969 u_error = 0;
970 }
971#else /* !ARM */
972#ifdef ALPHA
973 if (a3) {
974 tcp->u_rval = -1;
975 u_error = r0;
976 }
977 else {
978 tcp->u_rval = r0;
979 u_error = 0;
980 }
981#else /* !ALPHA */
982#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000983 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000984 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000985 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000986 }
987 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000988 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000989 u_error = 0;
990 }
991#endif /* SPARC */
992#endif /* ALPHA */
993#endif /* ARM */
994#endif /* M68K */
995#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000996#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000997#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000998#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000999#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001000#endif /* LINUX */
1001#ifdef SUNOS4
1002 /* get error code from user struct */
1003 if (upeek(pid, uoff(u_error), &u_error) < 0)
1004 return -1;
1005 u_error >>= 24; /* u_error is a char */
1006
1007 /* get system call return value */
1008 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1009 return -1;
1010#endif /* SUNOS4 */
1011#ifdef SVR4
1012#ifdef SPARC
1013 /* Judicious guessing goes a long way. */
1014 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1015 tcp->u_rval = -1;
1016 u_error = tcp->status.pr_reg[R_O0];
1017 }
1018 else {
1019 tcp->u_rval = tcp->status.pr_reg[R_O0];
1020 u_error = 0;
1021 }
1022#endif /* SPARC */
1023#ifdef I386
1024 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001025 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001026 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001027 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001028 }
1029 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001030 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001031 u_error = 0;
1032 }
1033#endif /* I386 */
1034#ifdef MIPS
1035 if (tcp->status.pr_reg[CTX_A3]) {
1036 tcp->u_rval = -1;
1037 u_error = tcp->status.pr_reg[CTX_V0];
1038 }
1039 else {
1040 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1041 u_error = 0;
1042 }
1043#endif /* MIPS */
1044#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001045 tcp->u_error = u_error;
1046 return 1;
1047}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001048
Pavel Machek4dc3b142000-02-01 17:58:41 +00001049int syscall_enter(tcp)
1050struct tcb *tcp;
1051{
1052 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001053#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001054#if defined(S390)
1055 {
1056 int i;
1057 tcp->u_nargs = sysent[tcp->scno].nargs;
1058 for (i = 0; i < tcp->u_nargs; i++) {
1059 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1060 return -1;
1061 }
1062 }
1063#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001064 {
1065 int i;
1066 tcp->u_nargs = sysent[tcp->scno].nargs;
1067 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001068 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1069 * for scno somewhere above here!
1070 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001071 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1072 return -1;
1073 }
1074 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001075#elif defined (IA64)
1076 {
1077 unsigned long *bsp, i;
1078
1079 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1080 return -1;
1081
1082 tcp->u_nargs = sysent[tcp->scno].nargs;
1083 for (i = 0; i < tcp->u_nargs; ++i) {
1084 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1085 (char *) &tcp->u_arg[i])
1086 < 0)
1087 return -1;
1088 }
1089 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001090#elif defined (MIPS)
1091 {
1092 long sp;
1093 int i, nargs;
1094
1095 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1096 if(nargs > 4) {
1097 if(upeek(pid, REG_SP, &sp) < 0)
1098 return -1;
1099 for(i = 0; i < 4; i++) {
1100 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1101 return -1;
1102 }
1103 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1104 (char *)(tcp->u_arg + 4));
1105 } else {
1106 for(i = 0; i < nargs; i++) {
1107 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1108 return -1;
1109 }
1110 }
1111 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001112#elif defined (POWERPC)
1113 {
1114 int i;
1115 tcp->u_nargs = sysent[tcp->scno].nargs;
1116 for (i = 0; i < tcp->u_nargs; i++) {
1117 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1118 return -1;
1119 }
1120 }
1121#elif defined (SPARC)
1122 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001123 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001125 tcp->u_nargs = sysent[tcp->scno].nargs;
1126 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001127 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001128 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001129#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001130 {
1131 int i;
1132 tcp->u_nargs = sysent[tcp->scno].nargs;
1133 for (i = 0; i < tcp->u_nargs; i++) {
1134 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1135 return -1;
1136 }
1137 }
1138#endif
1139#endif /* LINUX */
1140#ifdef SUNOS4
1141 {
1142 int i;
1143 tcp->u_nargs = sysent[tcp->scno].nargs;
1144 for (i = 0; i < tcp->u_nargs; i++) {
1145 struct user *u;
1146
1147 if (upeek(pid, uoff(u_arg[0]) +
1148 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1149 return -1;
1150 }
1151 }
1152#endif /* SUNOS4 */
1153#ifdef SVR4
1154#ifdef MIPS
1155 /*
1156 * SGI is broken: even though it has pr_sysarg, it doesn't
1157 * set them on system call entry. Get a clue.
1158 */
1159 if (sysent[tcp->scno].nargs != -1)
1160 tcp->u_nargs = sysent[tcp->scno].nargs;
1161 else
1162 tcp->u_nargs = tcp->status.pr_nsysarg;
1163 if (tcp->u_nargs > 4) {
1164 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1165 4*sizeof(tcp->u_arg[0]));
1166 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1167 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1168 }
1169 else {
1170 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1171 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1172 }
1173#else /* !MIPS */
1174#ifdef HAVE_PR_SYSCALL
1175 if (sysent[tcp->scno].nargs != -1)
1176 tcp->u_nargs = sysent[tcp->scno].nargs;
1177 else
1178 tcp->u_nargs = tcp->status.pr_nsysarg;
1179 {
1180 int i;
1181 for (i = 0; i < tcp->u_nargs; i++)
1182 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1183 }
1184#else /* !HAVE_PR_SYSCALL */
1185#ifdef I386
1186 if (sysent[tcp->scno].nargs != -1)
1187 tcp->u_nargs = sysent[tcp->scno].nargs;
1188 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001189#if UNIXWARE >= 2
1190 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1191#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001192 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001193#endif
1194 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001195 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1196#endif /* I386 */
1197#endif /* !HAVE_PR_SYSCALL */
1198#endif /* !MIPS */
1199#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001200 return 1;
1201}
1202
1203int
1204trace_syscall(tcp)
1205struct tcb *tcp;
1206{
1207 int sys_res;
1208 struct timeval tv;
1209 int res;
1210
1211 /* Measure the exit time as early as possible to avoid errors. */
1212 if (dtime && (tcp->flags & TCB_INSYSCALL))
1213 gettimeofday(&tv, NULL);
1214
1215 res = get_scno(tcp);
1216 if (res != 1)
1217 return res;
1218
1219 res = syscall_fixup(tcp);
1220 if (res != 1)
1221 return res;
1222
1223 if (tcp->flags & TCB_INSYSCALL) {
1224 long u_error;
1225 res = get_error(tcp);
1226 if (res != 1)
1227 return res;
1228 u_error = tcp->u_error;
1229
1230
1231 internal_syscall(tcp);
1232 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1233 tcp->flags &= ~TCB_INSYSCALL;
1234 return 0;
1235 }
1236
1237 if (tcp->flags & TCB_REPRINT) {
1238 printleader(tcp);
1239 tprintf("<... ");
1240 if (tcp->scno >= nsyscalls)
1241 tprintf("syscall_%lu", tcp->scno);
1242 else
1243 tprintf("%s", sysent[tcp->scno].sys_name);
1244 tprintf(" resumed> ");
1245 }
1246
1247 if (cflag) {
1248 call_count[tcp->scno]++;
1249 if (tcp->u_error)
1250 error_count[tcp->scno]++;
1251 tv_sub(&tv, &tv, &tcp->etime);
1252#ifdef LINUX
1253 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1254 static struct timeval one_tick =
1255 { 0, 1000000 / HZ };
1256
1257 if (tv_nz(&tcp->dtime))
1258 tv = tcp->dtime;
1259 else if (tv_cmp(&tv, &one_tick) > 0) {
1260 if (tv_cmp(&shortest, &one_tick) < 0)
1261 tv = shortest;
1262 else
1263 tv = one_tick;
1264 }
1265 }
1266#endif /* LINUX */
1267 if (tv_cmp(&tv, &shortest) < 0)
1268 shortest = tv;
1269 tv_add(&tv_count[tcp->scno],
1270 &tv_count[tcp->scno], &tv);
1271 tcp->flags &= ~TCB_INSYSCALL;
1272 return 0;
1273 }
1274
1275 if (tcp->scno >= nsyscalls
1276 || (qual_flags[tcp->scno] & QUAL_RAW))
1277 sys_res = printargs(tcp);
1278 else
1279 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1280 u_error = tcp->u_error;
1281 tprintf(") ");
1282 tabto(acolumn);
1283 if (qual_flags[tcp->scno] & QUAL_RAW) {
1284 if (u_error)
1285 tprintf("= -1 (errno %ld)", u_error);
1286 else
1287 tprintf("= %#lx", tcp->u_rval);
1288 }
1289 else if (!(sys_res & RVAL_NONE) && u_error) {
1290 switch (u_error) {
1291#ifdef LINUX
1292 case ERESTARTSYS:
1293 tprintf("= ? ERESTARTSYS (To be restarted)");
1294 break;
1295 case ERESTARTNOINTR:
1296 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1297 break;
1298 case ERESTARTNOHAND:
1299 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1300 break;
1301#endif /* LINUX */
1302 default:
1303 tprintf("= -1 ");
1304 if (u_error < nerrnos && u_error < sys_nerr)
1305 tprintf("%s (%s)", errnoent[u_error],
1306 sys_errlist[u_error]);
1307 else if (u_error < nerrnos)
1308 tprintf("%s (errno %ld)",
1309 errnoent[u_error], u_error);
1310 else if (u_error < sys_nerr)
1311 tprintf("ERRNO_%ld (%s)", u_error,
1312 sys_errlist[u_error]);
1313 else
1314 tprintf("E??? (errno %ld)", u_error);
1315 break;
1316 }
1317 }
1318 else {
1319 if (sys_res & RVAL_NONE)
1320 tprintf("= ?");
1321 else {
1322 switch (sys_res & RVAL_MASK) {
1323 case RVAL_HEX:
1324 tprintf("= %#lx", tcp->u_rval);
1325 break;
1326 case RVAL_OCTAL:
1327 tprintf("= %#lo", tcp->u_rval);
1328 break;
1329 case RVAL_UDECIMAL:
1330 tprintf("= %lu", tcp->u_rval);
1331 break;
1332 case RVAL_DECIMAL:
1333 tprintf("= %ld", tcp->u_rval);
1334 break;
1335 default:
1336 fprintf(stderr,
1337 "invalid rval format\n");
1338 break;
1339 }
1340 }
1341 if ((sys_res & RVAL_STR) && tcp->auxstr)
1342 tprintf(" (%s)", tcp->auxstr);
1343 }
1344 if (dtime) {
1345 tv_sub(&tv, &tv, &tcp->etime);
1346 tprintf(" <%ld.%06ld>",
1347 (long) tv.tv_sec, (long) tv.tv_usec);
1348 }
1349 printtrailer(tcp);
1350
1351 dumpio(tcp);
1352 if (fflush(tcp->outf) == EOF)
1353 return -1;
1354 tcp->flags &= ~TCB_INSYSCALL;
1355 return 0;
1356 }
1357
1358 /* Entering system call */
1359 res = syscall_enter(tcp);
1360 if (res != 1)
1361 return res;
1362
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001363 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001364#ifdef LINUX
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001365#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001366 case SYS_socketcall:
1367 decode_subcall(tcp, SYS_socket_subcall,
1368 SYS_socket_nsubcalls, deref_style);
1369 break;
1370 case SYS_ipc:
1371 decode_subcall(tcp, SYS_ipc_subcall,
1372 SYS_ipc_nsubcalls, shift_style);
1373 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001374#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001375#ifdef SPARC
1376 case SYS_socketcall:
1377 sparc_socket_decode (tcp);
1378 break;
1379#endif
1380#endif /* LINUX */
1381#ifdef SVR4
1382#ifdef SYS_pgrpsys_subcall
1383 case SYS_pgrpsys:
1384 decode_subcall(tcp, SYS_pgrpsys_subcall,
1385 SYS_pgrpsys_nsubcalls, shift_style);
1386 break;
1387#endif /* SYS_pgrpsys_subcall */
1388#ifdef SYS_sigcall_subcall
1389 case SYS_sigcall:
1390 decode_subcall(tcp, SYS_sigcall_subcall,
1391 SYS_sigcall_nsubcalls, mask_style);
1392 break;
1393#endif /* SYS_sigcall_subcall */
1394 case SYS_msgsys:
1395 decode_subcall(tcp, SYS_msgsys_subcall,
1396 SYS_msgsys_nsubcalls, shift_style);
1397 break;
1398 case SYS_shmsys:
1399 decode_subcall(tcp, SYS_shmsys_subcall,
1400 SYS_shmsys_nsubcalls, shift_style);
1401 break;
1402 case SYS_semsys:
1403 decode_subcall(tcp, SYS_semsys_subcall,
1404 SYS_semsys_nsubcalls, shift_style);
1405 break;
1406#if 0 /* broken */
1407 case SYS_utssys:
1408 decode_subcall(tcp, SYS_utssys_subcall,
1409 SYS_utssys_nsubcalls, shift_style);
1410 break;
1411#endif
1412 case SYS_sysfs:
1413 decode_subcall(tcp, SYS_sysfs_subcall,
1414 SYS_sysfs_nsubcalls, shift_style);
1415 break;
1416 case SYS_spcall:
1417 decode_subcall(tcp, SYS_spcall_subcall,
1418 SYS_spcall_nsubcalls, shift_style);
1419 break;
1420#ifdef SYS_context_subcall
1421 case SYS_context:
1422 decode_subcall(tcp, SYS_context_subcall,
1423 SYS_context_nsubcalls, shift_style);
1424 break;
1425#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001426#ifdef SYS_door_subcall
1427 case SYS_door:
1428 decode_subcall(tcp, SYS_door_subcall,
1429 SYS_door_nsubcalls, door_style);
1430 break;
1431#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001432#endif /* SVR4 */
1433#ifdef SUNOS4
1434 case SYS_semsys:
1435 decode_subcall(tcp, SYS_semsys_subcall,
1436 SYS_semsys_nsubcalls, shift_style);
1437 break;
1438 case SYS_msgsys:
1439 decode_subcall(tcp, SYS_msgsys_subcall,
1440 SYS_msgsys_nsubcalls, shift_style);
1441 break;
1442 case SYS_shmsys:
1443 decode_subcall(tcp, SYS_shmsys_subcall,
1444 SYS_shmsys_nsubcalls, shift_style);
1445 break;
1446#endif
1447 }
1448
1449 internal_syscall(tcp);
1450 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1451 tcp->flags |= TCB_INSYSCALL;
1452 return 0;
1453 }
1454
1455 if (cflag) {
1456 gettimeofday(&tcp->etime, NULL);
1457 tcp->flags |= TCB_INSYSCALL;
1458 return 0;
1459 }
1460
1461 printleader(tcp);
1462 tcp->flags &= ~TCB_REPRINT;
1463 tcp_last = tcp;
1464 if (tcp->scno >= nsyscalls)
1465 tprintf("syscall_%lu(", tcp->scno);
1466 else
1467 tprintf("%s(", sysent[tcp->scno].sys_name);
1468 if (tcp->scno >= nsyscalls ||
1469 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1470 sys_res = printargs(tcp);
1471 else
1472 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1473 if (fflush(tcp->outf) == EOF)
1474 return -1;
1475 tcp->flags |= TCB_INSYSCALL;
1476 /* Measure the entrance time as late as possible to avoid errors. */
1477 if (dtime)
1478 gettimeofday(&tcp->etime, NULL);
1479 return sys_res;
1480}
1481
1482int
1483printargs(tcp)
1484struct tcb *tcp;
1485{
1486 if (entering(tcp)) {
1487 int i;
1488
1489 for (i = 0; i < tcp->u_nargs; i++)
1490 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1491 }
1492 return 0;
1493}
1494
1495long
1496getrval2(tcp)
1497struct tcb *tcp;
1498{
1499 long val = -1;
1500
1501#ifdef LINUX
1502#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001503 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001504 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1505 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001506 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001507#endif /* SPARC */
1508#endif /* LINUX */
1509
1510#ifdef SUNOS4
1511 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1512 return -1;
1513#endif /* SUNOS4 */
1514
1515#ifdef SVR4
1516#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001517 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001518#endif /* SPARC */
1519#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001520 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001521#endif /* I386 */
1522#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001523 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001524#endif /* MIPS */
1525#endif /* SVR4 */
1526
1527 return val;
1528}
1529
1530/*
1531 * Apparently, indirect system calls have already be converted by ptrace(2),
1532 * so if you see "indir" this program has gone astray.
1533 */
1534int
1535sys_indir(tcp)
1536struct tcb *tcp;
1537{
1538 int i, scno, nargs;
1539
1540 if (entering(tcp)) {
1541 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1542 fprintf(stderr, "Bogus syscall: %u\n", scno);
1543 return 0;
1544 }
1545 nargs = sysent[scno].nargs;
1546 tprintf("%s", sysent[scno].sys_name);
1547 for (i = 0; i < nargs; i++)
1548 tprintf(", %#lx", tcp->u_arg[i+1]);
1549 }
1550 return 0;
1551}
1552
1553static int
1554time_cmp(a, b)
1555void *a;
1556void *b;
1557{
1558 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1559}
1560
1561static int
1562syscall_cmp(a, b)
1563void *a;
1564void *b;
1565{
1566 return strcmp(sysent[*((int *) a)].sys_name,
1567 sysent[*((int *) b)].sys_name);
1568}
1569
1570static int
1571count_cmp(a, b)
1572void *a;
1573void *b;
1574{
1575 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1576
1577 return (m < n) ? 1 : (m > n) ? -1 : 0;
1578}
1579
1580static int (*sortfun)();
1581static struct timeval overhead = { -1, -1 };
1582
1583void
1584set_sortby(sortby)
1585char *sortby;
1586{
1587 if (strcmp(sortby, "time") == 0)
1588 sortfun = time_cmp;
1589 else if (strcmp(sortby, "calls") == 0)
1590 sortfun = count_cmp;
1591 else if (strcmp(sortby, "name") == 0)
1592 sortfun = syscall_cmp;
1593 else if (strcmp(sortby, "nothing") == 0)
1594 sortfun = NULL;
1595 else {
1596 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1597 exit(1);
1598 }
1599}
1600
1601void set_overhead(n)
1602int n;
1603{
1604 overhead.tv_sec = n / 1000000;
1605 overhead.tv_usec = n % 1000000;
1606}
1607
1608void
1609call_summary(outf)
1610FILE *outf;
1611{
1612 int i, j;
1613 int call_cum, error_cum;
1614 struct timeval tv_cum, dtv;
1615 double percent;
1616 char *dashes = "-------------------------";
1617 char error_str[16];
1618
1619 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1620 if (overhead.tv_sec == -1) {
1621 tv_mul(&overhead, &shortest, 8);
1622 tv_div(&overhead, &overhead, 10);
1623 }
1624 for (i = 0; i < nsyscalls; i++) {
1625 sorted_count[i] = i;
1626 if (call_count[i] == 0)
1627 continue;
1628 tv_mul(&dtv, &overhead, call_count[i]);
1629 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1630 call_cum += call_count[i];
1631 error_cum += error_count[i];
1632 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1633 }
1634 if (sortfun)
1635 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1636 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1637 "% time", "seconds", "usecs/call",
1638 "calls", "errors", "syscall");
1639 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1640 dashes, dashes, dashes, dashes, dashes, dashes);
1641 for (i = 0; i < nsyscalls; i++) {
1642 j = sorted_count[i];
1643 if (call_count[j] == 0)
1644 continue;
1645 tv_div(&dtv, &tv_count[j], call_count[j]);
1646 if (error_count[j])
1647 sprintf(error_str, "%d", error_count[j]);
1648 else
1649 error_str[0] = '\0';
1650 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1651 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1652 percent, (long) tv_count[j].tv_sec,
1653 (long) tv_count[j].tv_usec,
1654 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1655 call_count[j], error_str, sysent[j].sys_name);
1656 }
1657 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1658 dashes, dashes, dashes, dashes, dashes, dashes);
1659 if (error_cum)
1660 sprintf(error_str, "%d", error_cum);
1661 else
1662 error_str[0] = '\0';
1663 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1664 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1665 call_cum, error_str, "total");
1666}