blob: 29a805e3b01995fb0f63a6b573b509a78ea8fc5f [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
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000778 if (!(tcp->flags & TCB_INSYSCALL))
779 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000780 return 1;
781}
782
Pavel Machek4dc3b142000-02-01 17:58:41 +0000783
784int
785syscall_fixup(tcp)
786struct tcb *tcp;
787{
788 int pid = tcp->pid;
789
790#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000791 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000792 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000793 if (
794 scno == SYS_fork
795#ifdef SYS_vfork
796 || scno == SYS_vfork
797#endif /* SYS_vfork */
798 ) {
799 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000800 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000801 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000802 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000803 }
804 else {
805 fprintf(stderr, "syscall: missing entry\n");
806 tcp->flags |= TCB_INSYSCALL;
807 }
808 }
809 }
810 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000811 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000812 fprintf(stderr, "syscall: missing exit\n");
813 tcp->flags &= ~TCB_INSYSCALL;
814 }
815 }
816#endif /* SVR4 */
817#ifdef SUNOS4
818 if (!(tcp->flags & TCB_INSYSCALL)) {
819 if (scno == 0) {
820 fprintf(stderr, "syscall: missing entry\n");
821 tcp->flags |= TCB_INSYSCALL;
822 }
823 }
824 else {
825 if (scno != 0) {
826 if (debug) {
827 /*
828 * This happens when a signal handler
829 * for a signal which interrupted a
830 * a system call makes another system call.
831 */
832 fprintf(stderr, "syscall: missing exit\n");
833 }
834 tcp->flags &= ~TCB_INSYSCALL;
835 }
836 }
837#endif /* SUNOS4 */
838#ifdef LINUX
839#if defined (I386)
840 if (upeek(pid, 4*EAX, &eax) < 0)
841 return -1;
842 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
843 if (debug)
844 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
845 return 0;
846 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000847#elif defined (S390)
848 if (upeek(pid, PT_GPR2, &gpr2) < 0)
849 return -1;
850 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
851 if (debug)
852 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
853 return 0;
854 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000855#elif defined (POWERPC)
856# define SO_MASK 0x10000000
857 if (upeek(pid, 4*PT_CCR, &flags) < 0)
858 return -1;
859 if (upeek(pid, 4*PT_R3, &result) < 0)
860 return -1;
861 if (flags & SO_MASK)
862 result = -result;
863#elif defined (M68K)
864 if (upeek(pid, 4*PT_D0, &d0) < 0)
865 return -1;
866 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
867 if (debug)
868 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
869 return 0;
870 }
871#elif defined (ARM)
872 if (upeek(pid, 4*0, (long *)&r0) < 0)
873 return -1;
874 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
875 if (debug)
876 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
877 return 0;
878 }
879#else
880#endif
881#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000882 return 1;
883}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000884
Pavel Machek4dc3b142000-02-01 17:58:41 +0000885int
886get_error(tcp)
887struct tcb *tcp;
888{
889 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000890#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000891#ifdef S390
892 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
893 tcp->u_rval = -1;
894 u_error = -gpr2;
895 }
896 else {
897 tcp->u_rval = gpr2;
898 u_error = 0;
899 }
900#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000901#ifdef I386
902 if (eax < 0 && -eax < nerrnos) {
903 tcp->u_rval = -1;
904 u_error = -eax;
905 }
906 else {
907 tcp->u_rval = eax;
908 u_error = 0;
909 }
910#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000911#ifdef IA64
912 if (ia32) {
913 int err;
914
915 err = (int)r8;
916 if (err < 0 && -err < nerrnos) {
917 tcp->u_rval = -1;
918 u_error = -err;
919 }
920 else {
921 tcp->u_rval = err;
922 u_error = 0;
923 }
924 } else {
925 if (r10) {
926 tcp->u_rval = -1;
927 u_error = r8;
928 } else {
929 tcp->u_rval = r8;
930 u_error = 0;
931 }
932 }
933#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000934#ifdef MIPS
935 if (a3) {
936 tcp->u_rval = -1;
937 u_error = r2;
938 } else {
939 tcp->u_rval = r2;
940 u_error = 0;
941 }
942#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000943#ifdef POWERPC
944 if (result && (unsigned) -result < nerrnos) {
945 tcp->u_rval = -1;
946 u_error = -result;
947 }
948 else {
949 tcp->u_rval = result;
950 u_error = 0;
951 }
952#else /* !POWERPC */
953#ifdef M68K
954 if (d0 && (unsigned) -d0 < nerrnos) {
955 tcp->u_rval = -1;
956 u_error = -d0;
957 }
958 else {
959 tcp->u_rval = d0;
960 u_error = 0;
961 }
962#else /* !M68K */
963#ifdef ARM
964 if (r0 && (unsigned) -r0 < nerrnos) {
965 tcp->u_rval = -1;
966 u_error = -r0;
967 }
968 else {
969 tcp->u_rval = r0;
970 u_error = 0;
971 }
972#else /* !ARM */
973#ifdef ALPHA
974 if (a3) {
975 tcp->u_rval = -1;
976 u_error = r0;
977 }
978 else {
979 tcp->u_rval = r0;
980 u_error = 0;
981 }
982#else /* !ALPHA */
983#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000984 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000985 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000986 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000987 }
988 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000989 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000990 u_error = 0;
991 }
992#endif /* SPARC */
993#endif /* ALPHA */
994#endif /* ARM */
995#endif /* M68K */
996#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000997#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000998#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000999#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001000#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001001#endif /* LINUX */
1002#ifdef SUNOS4
1003 /* get error code from user struct */
1004 if (upeek(pid, uoff(u_error), &u_error) < 0)
1005 return -1;
1006 u_error >>= 24; /* u_error is a char */
1007
1008 /* get system call return value */
1009 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1010 return -1;
1011#endif /* SUNOS4 */
1012#ifdef SVR4
1013#ifdef SPARC
1014 /* Judicious guessing goes a long way. */
1015 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1016 tcp->u_rval = -1;
1017 u_error = tcp->status.pr_reg[R_O0];
1018 }
1019 else {
1020 tcp->u_rval = tcp->status.pr_reg[R_O0];
1021 u_error = 0;
1022 }
1023#endif /* SPARC */
1024#ifdef I386
1025 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001026 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001027 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001028 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001029 }
1030 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001031 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001032 u_error = 0;
1033 }
1034#endif /* I386 */
1035#ifdef MIPS
1036 if (tcp->status.pr_reg[CTX_A3]) {
1037 tcp->u_rval = -1;
1038 u_error = tcp->status.pr_reg[CTX_V0];
1039 }
1040 else {
1041 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1042 u_error = 0;
1043 }
1044#endif /* MIPS */
1045#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001046 tcp->u_error = u_error;
1047 return 1;
1048}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001049
Pavel Machek4dc3b142000-02-01 17:58:41 +00001050int syscall_enter(tcp)
1051struct tcb *tcp;
1052{
1053 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001054#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001055#if defined(S390)
1056 {
1057 int i;
1058 tcp->u_nargs = sysent[tcp->scno].nargs;
1059 for (i = 0; i < tcp->u_nargs; i++) {
1060 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1061 return -1;
1062 }
1063 }
1064#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001065 {
1066 int i;
1067 tcp->u_nargs = sysent[tcp->scno].nargs;
1068 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001069 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1070 * for scno somewhere above here!
1071 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001072 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1073 return -1;
1074 }
1075 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001076#elif defined (IA64)
1077 {
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001078 unsigned long *bsp, cfm, i;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001079
1080 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1081 return -1;
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001082 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1083 return -1;
1084
1085 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001086
1087 tcp->u_nargs = sysent[tcp->scno].nargs;
1088 for (i = 0; i < tcp->u_nargs; ++i) {
1089 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1090 (char *) &tcp->u_arg[i])
1091 < 0)
1092 return -1;
1093 }
1094 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001095#elif defined (MIPS)
1096 {
1097 long sp;
1098 int i, nargs;
1099
1100 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1101 if(nargs > 4) {
1102 if(upeek(pid, REG_SP, &sp) < 0)
1103 return -1;
1104 for(i = 0; i < 4; i++) {
1105 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1106 return -1;
1107 }
1108 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1109 (char *)(tcp->u_arg + 4));
1110 } else {
1111 for(i = 0; i < nargs; i++) {
1112 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1113 return -1;
1114 }
1115 }
1116 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001117#elif defined (POWERPC)
1118 {
1119 int i;
1120 tcp->u_nargs = sysent[tcp->scno].nargs;
1121 for (i = 0; i < tcp->u_nargs; i++) {
1122 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1123 return -1;
1124 }
1125 }
1126#elif defined (SPARC)
1127 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001128 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001129
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001130 tcp->u_nargs = sysent[tcp->scno].nargs;
1131 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001132 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001133 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001134#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001135 {
1136 int i;
1137 tcp->u_nargs = sysent[tcp->scno].nargs;
1138 for (i = 0; i < tcp->u_nargs; i++) {
1139 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1140 return -1;
1141 }
1142 }
1143#endif
1144#endif /* LINUX */
1145#ifdef SUNOS4
1146 {
1147 int i;
1148 tcp->u_nargs = sysent[tcp->scno].nargs;
1149 for (i = 0; i < tcp->u_nargs; i++) {
1150 struct user *u;
1151
1152 if (upeek(pid, uoff(u_arg[0]) +
1153 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1154 return -1;
1155 }
1156 }
1157#endif /* SUNOS4 */
1158#ifdef SVR4
1159#ifdef MIPS
1160 /*
1161 * SGI is broken: even though it has pr_sysarg, it doesn't
1162 * set them on system call entry. Get a clue.
1163 */
1164 if (sysent[tcp->scno].nargs != -1)
1165 tcp->u_nargs = sysent[tcp->scno].nargs;
1166 else
1167 tcp->u_nargs = tcp->status.pr_nsysarg;
1168 if (tcp->u_nargs > 4) {
1169 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1170 4*sizeof(tcp->u_arg[0]));
1171 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1172 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1173 }
1174 else {
1175 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1176 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1177 }
1178#else /* !MIPS */
1179#ifdef HAVE_PR_SYSCALL
1180 if (sysent[tcp->scno].nargs != -1)
1181 tcp->u_nargs = sysent[tcp->scno].nargs;
1182 else
1183 tcp->u_nargs = tcp->status.pr_nsysarg;
1184 {
1185 int i;
1186 for (i = 0; i < tcp->u_nargs; i++)
1187 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1188 }
1189#else /* !HAVE_PR_SYSCALL */
1190#ifdef I386
1191 if (sysent[tcp->scno].nargs != -1)
1192 tcp->u_nargs = sysent[tcp->scno].nargs;
1193 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001194#if UNIXWARE >= 2
1195 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1196#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001197 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001198#endif
1199 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001200 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1201#endif /* I386 */
1202#endif /* !HAVE_PR_SYSCALL */
1203#endif /* !MIPS */
1204#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001205 return 1;
1206}
1207
1208int
1209trace_syscall(tcp)
1210struct tcb *tcp;
1211{
1212 int sys_res;
1213 struct timeval tv;
1214 int res;
1215
1216 /* Measure the exit time as early as possible to avoid errors. */
1217 if (dtime && (tcp->flags & TCB_INSYSCALL))
1218 gettimeofday(&tv, NULL);
1219
1220 res = get_scno(tcp);
1221 if (res != 1)
1222 return res;
1223
1224 res = syscall_fixup(tcp);
1225 if (res != 1)
1226 return res;
1227
1228 if (tcp->flags & TCB_INSYSCALL) {
1229 long u_error;
1230 res = get_error(tcp);
1231 if (res != 1)
1232 return res;
1233 u_error = tcp->u_error;
1234
1235
1236 internal_syscall(tcp);
1237 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1238 tcp->flags &= ~TCB_INSYSCALL;
1239 return 0;
1240 }
1241
1242 if (tcp->flags & TCB_REPRINT) {
1243 printleader(tcp);
1244 tprintf("<... ");
1245 if (tcp->scno >= nsyscalls)
1246 tprintf("syscall_%lu", tcp->scno);
1247 else
1248 tprintf("%s", sysent[tcp->scno].sys_name);
1249 tprintf(" resumed> ");
1250 }
1251
1252 if (cflag) {
1253 call_count[tcp->scno]++;
1254 if (tcp->u_error)
1255 error_count[tcp->scno]++;
1256 tv_sub(&tv, &tv, &tcp->etime);
1257#ifdef LINUX
1258 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1259 static struct timeval one_tick =
1260 { 0, 1000000 / HZ };
1261
1262 if (tv_nz(&tcp->dtime))
1263 tv = tcp->dtime;
1264 else if (tv_cmp(&tv, &one_tick) > 0) {
1265 if (tv_cmp(&shortest, &one_tick) < 0)
1266 tv = shortest;
1267 else
1268 tv = one_tick;
1269 }
1270 }
1271#endif /* LINUX */
1272 if (tv_cmp(&tv, &shortest) < 0)
1273 shortest = tv;
1274 tv_add(&tv_count[tcp->scno],
1275 &tv_count[tcp->scno], &tv);
1276 tcp->flags &= ~TCB_INSYSCALL;
1277 return 0;
1278 }
1279
1280 if (tcp->scno >= nsyscalls
1281 || (qual_flags[tcp->scno] & QUAL_RAW))
1282 sys_res = printargs(tcp);
1283 else
1284 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1285 u_error = tcp->u_error;
1286 tprintf(") ");
1287 tabto(acolumn);
1288 if (qual_flags[tcp->scno] & QUAL_RAW) {
1289 if (u_error)
1290 tprintf("= -1 (errno %ld)", u_error);
1291 else
1292 tprintf("= %#lx", tcp->u_rval);
1293 }
1294 else if (!(sys_res & RVAL_NONE) && u_error) {
1295 switch (u_error) {
1296#ifdef LINUX
1297 case ERESTARTSYS:
1298 tprintf("= ? ERESTARTSYS (To be restarted)");
1299 break;
1300 case ERESTARTNOINTR:
1301 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1302 break;
1303 case ERESTARTNOHAND:
1304 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1305 break;
1306#endif /* LINUX */
1307 default:
1308 tprintf("= -1 ");
1309 if (u_error < nerrnos && u_error < sys_nerr)
1310 tprintf("%s (%s)", errnoent[u_error],
1311 sys_errlist[u_error]);
1312 else if (u_error < nerrnos)
1313 tprintf("%s (errno %ld)",
1314 errnoent[u_error], u_error);
1315 else if (u_error < sys_nerr)
1316 tprintf("ERRNO_%ld (%s)", u_error,
1317 sys_errlist[u_error]);
1318 else
1319 tprintf("E??? (errno %ld)", u_error);
1320 break;
1321 }
1322 }
1323 else {
1324 if (sys_res & RVAL_NONE)
1325 tprintf("= ?");
1326 else {
1327 switch (sys_res & RVAL_MASK) {
1328 case RVAL_HEX:
1329 tprintf("= %#lx", tcp->u_rval);
1330 break;
1331 case RVAL_OCTAL:
1332 tprintf("= %#lo", tcp->u_rval);
1333 break;
1334 case RVAL_UDECIMAL:
1335 tprintf("= %lu", tcp->u_rval);
1336 break;
1337 case RVAL_DECIMAL:
1338 tprintf("= %ld", tcp->u_rval);
1339 break;
1340 default:
1341 fprintf(stderr,
1342 "invalid rval format\n");
1343 break;
1344 }
1345 }
1346 if ((sys_res & RVAL_STR) && tcp->auxstr)
1347 tprintf(" (%s)", tcp->auxstr);
1348 }
1349 if (dtime) {
1350 tv_sub(&tv, &tv, &tcp->etime);
1351 tprintf(" <%ld.%06ld>",
1352 (long) tv.tv_sec, (long) tv.tv_usec);
1353 }
1354 printtrailer(tcp);
1355
1356 dumpio(tcp);
1357 if (fflush(tcp->outf) == EOF)
1358 return -1;
1359 tcp->flags &= ~TCB_INSYSCALL;
1360 return 0;
1361 }
1362
1363 /* Entering system call */
1364 res = syscall_enter(tcp);
1365 if (res != 1)
1366 return res;
1367
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001368 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001369#ifdef LINUX
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001370#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001371 case SYS_socketcall:
1372 decode_subcall(tcp, SYS_socket_subcall,
1373 SYS_socket_nsubcalls, deref_style);
1374 break;
1375 case SYS_ipc:
1376 decode_subcall(tcp, SYS_ipc_subcall,
1377 SYS_ipc_nsubcalls, shift_style);
1378 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001379#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001380#ifdef SPARC
1381 case SYS_socketcall:
1382 sparc_socket_decode (tcp);
1383 break;
1384#endif
1385#endif /* LINUX */
1386#ifdef SVR4
1387#ifdef SYS_pgrpsys_subcall
1388 case SYS_pgrpsys:
1389 decode_subcall(tcp, SYS_pgrpsys_subcall,
1390 SYS_pgrpsys_nsubcalls, shift_style);
1391 break;
1392#endif /* SYS_pgrpsys_subcall */
1393#ifdef SYS_sigcall_subcall
1394 case SYS_sigcall:
1395 decode_subcall(tcp, SYS_sigcall_subcall,
1396 SYS_sigcall_nsubcalls, mask_style);
1397 break;
1398#endif /* SYS_sigcall_subcall */
1399 case SYS_msgsys:
1400 decode_subcall(tcp, SYS_msgsys_subcall,
1401 SYS_msgsys_nsubcalls, shift_style);
1402 break;
1403 case SYS_shmsys:
1404 decode_subcall(tcp, SYS_shmsys_subcall,
1405 SYS_shmsys_nsubcalls, shift_style);
1406 break;
1407 case SYS_semsys:
1408 decode_subcall(tcp, SYS_semsys_subcall,
1409 SYS_semsys_nsubcalls, shift_style);
1410 break;
1411#if 0 /* broken */
1412 case SYS_utssys:
1413 decode_subcall(tcp, SYS_utssys_subcall,
1414 SYS_utssys_nsubcalls, shift_style);
1415 break;
1416#endif
1417 case SYS_sysfs:
1418 decode_subcall(tcp, SYS_sysfs_subcall,
1419 SYS_sysfs_nsubcalls, shift_style);
1420 break;
1421 case SYS_spcall:
1422 decode_subcall(tcp, SYS_spcall_subcall,
1423 SYS_spcall_nsubcalls, shift_style);
1424 break;
1425#ifdef SYS_context_subcall
1426 case SYS_context:
1427 decode_subcall(tcp, SYS_context_subcall,
1428 SYS_context_nsubcalls, shift_style);
1429 break;
1430#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001431#ifdef SYS_door_subcall
1432 case SYS_door:
1433 decode_subcall(tcp, SYS_door_subcall,
1434 SYS_door_nsubcalls, door_style);
1435 break;
1436#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437#endif /* SVR4 */
1438#ifdef SUNOS4
1439 case SYS_semsys:
1440 decode_subcall(tcp, SYS_semsys_subcall,
1441 SYS_semsys_nsubcalls, shift_style);
1442 break;
1443 case SYS_msgsys:
1444 decode_subcall(tcp, SYS_msgsys_subcall,
1445 SYS_msgsys_nsubcalls, shift_style);
1446 break;
1447 case SYS_shmsys:
1448 decode_subcall(tcp, SYS_shmsys_subcall,
1449 SYS_shmsys_nsubcalls, shift_style);
1450 break;
1451#endif
1452 }
1453
1454 internal_syscall(tcp);
1455 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1456 tcp->flags |= TCB_INSYSCALL;
1457 return 0;
1458 }
1459
1460 if (cflag) {
1461 gettimeofday(&tcp->etime, NULL);
1462 tcp->flags |= TCB_INSYSCALL;
1463 return 0;
1464 }
1465
1466 printleader(tcp);
1467 tcp->flags &= ~TCB_REPRINT;
1468 tcp_last = tcp;
1469 if (tcp->scno >= nsyscalls)
1470 tprintf("syscall_%lu(", tcp->scno);
1471 else
1472 tprintf("%s(", sysent[tcp->scno].sys_name);
1473 if (tcp->scno >= nsyscalls ||
1474 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1475 sys_res = printargs(tcp);
1476 else
1477 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1478 if (fflush(tcp->outf) == EOF)
1479 return -1;
1480 tcp->flags |= TCB_INSYSCALL;
1481 /* Measure the entrance time as late as possible to avoid errors. */
1482 if (dtime)
1483 gettimeofday(&tcp->etime, NULL);
1484 return sys_res;
1485}
1486
1487int
1488printargs(tcp)
1489struct tcb *tcp;
1490{
1491 if (entering(tcp)) {
1492 int i;
1493
1494 for (i = 0; i < tcp->u_nargs; i++)
1495 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1496 }
1497 return 0;
1498}
1499
1500long
1501getrval2(tcp)
1502struct tcb *tcp;
1503{
1504 long val = -1;
1505
1506#ifdef LINUX
1507#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001508 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001509 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1510 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001511 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001512#endif /* SPARC */
1513#endif /* LINUX */
1514
1515#ifdef SUNOS4
1516 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1517 return -1;
1518#endif /* SUNOS4 */
1519
1520#ifdef SVR4
1521#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001522 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001523#endif /* SPARC */
1524#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001525 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001526#endif /* I386 */
1527#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001528 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001529#endif /* MIPS */
1530#endif /* SVR4 */
1531
1532 return val;
1533}
1534
1535/*
1536 * Apparently, indirect system calls have already be converted by ptrace(2),
1537 * so if you see "indir" this program has gone astray.
1538 */
1539int
1540sys_indir(tcp)
1541struct tcb *tcp;
1542{
1543 int i, scno, nargs;
1544
1545 if (entering(tcp)) {
1546 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1547 fprintf(stderr, "Bogus syscall: %u\n", scno);
1548 return 0;
1549 }
1550 nargs = sysent[scno].nargs;
1551 tprintf("%s", sysent[scno].sys_name);
1552 for (i = 0; i < nargs; i++)
1553 tprintf(", %#lx", tcp->u_arg[i+1]);
1554 }
1555 return 0;
1556}
1557
1558static int
1559time_cmp(a, b)
1560void *a;
1561void *b;
1562{
1563 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1564}
1565
1566static int
1567syscall_cmp(a, b)
1568void *a;
1569void *b;
1570{
1571 return strcmp(sysent[*((int *) a)].sys_name,
1572 sysent[*((int *) b)].sys_name);
1573}
1574
1575static int
1576count_cmp(a, b)
1577void *a;
1578void *b;
1579{
1580 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1581
1582 return (m < n) ? 1 : (m > n) ? -1 : 0;
1583}
1584
1585static int (*sortfun)();
1586static struct timeval overhead = { -1, -1 };
1587
1588void
1589set_sortby(sortby)
1590char *sortby;
1591{
1592 if (strcmp(sortby, "time") == 0)
1593 sortfun = time_cmp;
1594 else if (strcmp(sortby, "calls") == 0)
1595 sortfun = count_cmp;
1596 else if (strcmp(sortby, "name") == 0)
1597 sortfun = syscall_cmp;
1598 else if (strcmp(sortby, "nothing") == 0)
1599 sortfun = NULL;
1600 else {
1601 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1602 exit(1);
1603 }
1604}
1605
1606void set_overhead(n)
1607int n;
1608{
1609 overhead.tv_sec = n / 1000000;
1610 overhead.tv_usec = n % 1000000;
1611}
1612
1613void
1614call_summary(outf)
1615FILE *outf;
1616{
1617 int i, j;
1618 int call_cum, error_cum;
1619 struct timeval tv_cum, dtv;
1620 double percent;
1621 char *dashes = "-------------------------";
1622 char error_str[16];
1623
1624 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1625 if (overhead.tv_sec == -1) {
1626 tv_mul(&overhead, &shortest, 8);
1627 tv_div(&overhead, &overhead, 10);
1628 }
1629 for (i = 0; i < nsyscalls; i++) {
1630 sorted_count[i] = i;
1631 if (call_count[i] == 0)
1632 continue;
1633 tv_mul(&dtv, &overhead, call_count[i]);
1634 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1635 call_cum += call_count[i];
1636 error_cum += error_count[i];
1637 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1638 }
1639 if (sortfun)
1640 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1641 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1642 "% time", "seconds", "usecs/call",
1643 "calls", "errors", "syscall");
1644 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1645 dashes, dashes, dashes, dashes, dashes, dashes);
1646 for (i = 0; i < nsyscalls; i++) {
1647 j = sorted_count[i];
1648 if (call_count[j] == 0)
1649 continue;
1650 tv_div(&dtv, &tv_count[j], call_count[j]);
1651 if (error_count[j])
1652 sprintf(error_str, "%d", error_count[j]);
1653 else
1654 error_str[0] = '\0';
1655 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1656 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1657 percent, (long) tv_count[j].tv_sec,
1658 (long) tv_count[j].tv_usec,
1659 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1660 call_count[j], error_str, sysent[j].sys_name);
1661 }
1662 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1663 dashes, dashes, dashes, dashes, dashes, dashes);
1664 if (error_cum)
1665 sprintf(error_str, "%d", error_cum);
1666 else
1667 error_str[0] = '\0';
1668 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1669 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1670 call_cum, error_str, "total");
1671}