blob: 99849880f48cec17cdb1aaac5dba9670132ded05 [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
572int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000573get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574struct tcb *tcp;
575{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000577 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000579#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000580#if defined(S390)
581 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
582 return -1;
583 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
584 if (errno)
585 return -1;
586 scno&=0xFF;
587#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588 if (upeek(pid, 4*PT_R0, &scno) < 0)
589 return -1;
590 if (!(tcp->flags & TCB_INSYSCALL)) {
591 /* Check if we return from execve. */
592 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
593 tcp->flags &= ~TCB_WAITEXECVE;
594 return 0;
595 }
596 }
597#elif defined (I386)
598 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
599 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000600#elif defined(IA64)
601#define IA64_PSR_IS ((long)1 << 34)
602 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
603 ia32 = (psr & IA64_PSR_IS);
604 if (!(tcp->flags & TCB_INSYSCALL)) {
605 if (ia32) {
606 if (upeek(pid, PT_R8, &scno) < 0)
607 return -1;
608 } else {
609 if (upeek (pid, PT_R15, &scno) < 0)
610 return -1;
611 }
612 } else {
613 /* syscall in progress */
614 if (upeek (pid, PT_R8, &r8) < 0)
615 return -1;
616 if (upeek (pid, PT_R10, &r10) < 0)
617 return -1;
618 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000619#elif defined (ARM)
620 {
621 long pc;
622 upeek(pid, 4*15, &pc);
623 umoven(tcp, pc-4, 4, (char *)&scno);
624 scno &= 0x000fffff;
625 }
626#elif defined (M68K)
627 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
628 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000629#elif defined (MIPS)
630 if (upeek(pid, REG_A3, &a3) < 0)
631 return -1;
632
633 if(!(tcp->flags & TCB_INSYSCALL)) {
634 if (upeek(pid, REG_V0, &scno) < 0)
635 return -1;
636
637 if (scno < 0 || scno > nsyscalls) {
638 if(a3 == 0 || a3 == -1) {
639 if(debug)
640 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
641 return 0;
642 }
643 }
644 } else {
645 if (upeek(pid, REG_V0, &r2) < 0)
646 return -1;
647 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000648#elif defined (ALPHA)
649 if (upeek(pid, REG_A3, &a3) < 0)
650 return -1;
651
652 if (!(tcp->flags & TCB_INSYSCALL)) {
653 if (upeek(pid, REG_R0, &scno) < 0)
654 return -1;
655
656 /* Check if we return from execve. */
657 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
658 tcp->flags &= ~TCB_WAITEXECVE;
659 return 0;
660 }
661
662 /*
663 * Do some sanity checks to figure out if it's
664 * really a syscall entry
665 */
666 if (scno < 0 || scno > nsyscalls) {
667 if (a3 == 0 || a3 == -1) {
668 if (debug)
669 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
670 return 0;
671 }
672 }
673 }
674 else {
675 if (upeek(pid, REG_R0, &r0) < 0)
676 return -1;
677 }
678#elif defined (SPARC)
679 /* Everything we need is in the current register set. */
680 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
681 return -1;
682
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000683 /* If we are entering, then disassemble the syscall trap. */
684 if (!(tcp->flags & TCB_INSYSCALL)) {
685 /* Retrieve the syscall trap instruction. */
686 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000687 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000688 if (errno)
689 return -1;
690
691 /* Disassemble the trap to see what personality to use. */
692 switch (trap) {
693 case 0x91d02010:
694 /* Linux/SPARC syscall trap. */
695 set_personality(0);
696 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000697 case 0x91d0206d:
698 /* Linux/SPARC64 syscall trap. */
699 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
700 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000701 case 0x91d02000:
702 /* SunOS syscall trap. (pers 1) */
703 fprintf(stderr,"syscall: SunOS no support\n");
704 return -1;
705 case 0x91d02008:
706 /* Solaris 2.x syscall trap. (per 2) */
707 set_personality(1);
708 break;
709 case 0x91d02009:
710 /* NetBSD/FreeBSD syscall trap. */
711 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
712 return -1;
713 case 0x91d02027:
714 /* Solaris 2.x gettimeofday */
715 set_personality(1);
716 break;
717 default:
718 /* Unknown syscall trap. */
719 if(tcp->flags & TCB_WAITEXECVE) {
720 tcp->flags &= ~TCB_WAITEXECVE;
721 return 0;
722 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000723 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000724 return -1;
725 }
726
727 /* Extract the system call number from the registers. */
728 if (trap == 0x91d02027)
729 scno = 156;
730 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000731 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000732 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000733 scno = regs.r_o0;
734 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000735 }
736 }
737#endif
738#endif /* LINUX */
739#ifdef SUNOS4
740 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
741 return -1;
742#endif
743#ifdef SVR4
744#ifdef HAVE_PR_SYSCALL
745 scno = tcp->status.pr_syscall;
746#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000747 scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000748#endif /* !HAVE_PR_SYSCALL */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000749#endif
750 tcp->scno = scno;
751 return 1;
752}
753
754#ifdef LINUX
755#if defined (I386)
756 static long eax;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000757#elif defined (IA64)
758 long r8, r10, psr;
759 long ia32 = 0;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000760#elif defined (POWERPC)
761 static long result,flags;
762#elif defined (M68K)
763 static int d0;
764#elif defined (ARM)
765 static int r0;
766#elif defined (ALPHA)
767 static long r0;
768 static long a3;
769#elif defined (SPARC)
770 static struct pt_regs regs;
771 static unsigned long trap;
772#elif defined(S390)
773 static long gpr2;
774 static long pc;
775#endif
776#endif /* LINUX */
777
778int
779syscall_fixup(tcp)
780struct tcb *tcp;
781{
782 int pid = tcp->pid;
783
784#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000785 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000786 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000787 if (
788 scno == SYS_fork
789#ifdef SYS_vfork
790 || scno == SYS_vfork
791#endif /* SYS_vfork */
792 ) {
793 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000794 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000795 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000796 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000797 }
798 else {
799 fprintf(stderr, "syscall: missing entry\n");
800 tcp->flags |= TCB_INSYSCALL;
801 }
802 }
803 }
804 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000805 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000806 fprintf(stderr, "syscall: missing exit\n");
807 tcp->flags &= ~TCB_INSYSCALL;
808 }
809 }
810#endif /* SVR4 */
811#ifdef SUNOS4
812 if (!(tcp->flags & TCB_INSYSCALL)) {
813 if (scno == 0) {
814 fprintf(stderr, "syscall: missing entry\n");
815 tcp->flags |= TCB_INSYSCALL;
816 }
817 }
818 else {
819 if (scno != 0) {
820 if (debug) {
821 /*
822 * This happens when a signal handler
823 * for a signal which interrupted a
824 * a system call makes another system call.
825 */
826 fprintf(stderr, "syscall: missing exit\n");
827 }
828 tcp->flags &= ~TCB_INSYSCALL;
829 }
830 }
831#endif /* SUNOS4 */
832#ifdef LINUX
833#if defined (I386)
834 if (upeek(pid, 4*EAX, &eax) < 0)
835 return -1;
836 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
837 if (debug)
838 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
839 return 0;
840 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000841#elif defined (S390)
842 if (upeek(pid, PT_GPR2, &gpr2) < 0)
843 return -1;
844 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
845 if (debug)
846 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
847 return 0;
848 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000849#elif defined (POWERPC)
850# define SO_MASK 0x10000000
851 if (upeek(pid, 4*PT_CCR, &flags) < 0)
852 return -1;
853 if (upeek(pid, 4*PT_R3, &result) < 0)
854 return -1;
855 if (flags & SO_MASK)
856 result = -result;
857#elif defined (M68K)
858 if (upeek(pid, 4*PT_D0, &d0) < 0)
859 return -1;
860 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
861 if (debug)
862 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
863 return 0;
864 }
865#elif defined (ARM)
866 if (upeek(pid, 4*0, (long *)&r0) < 0)
867 return -1;
868 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
869 if (debug)
870 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
871 return 0;
872 }
873#else
874#endif
875#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000876 return 1;
877}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000878
Pavel Machek4dc3b142000-02-01 17:58:41 +0000879int
880get_error(tcp)
881struct tcb *tcp;
882{
883 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000884#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000885#ifdef S390
886 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
887 tcp->u_rval = -1;
888 u_error = -gpr2;
889 }
890 else {
891 tcp->u_rval = gpr2;
892 u_error = 0;
893 }
894#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000895#ifdef I386
896 if (eax < 0 && -eax < nerrnos) {
897 tcp->u_rval = -1;
898 u_error = -eax;
899 }
900 else {
901 tcp->u_rval = eax;
902 u_error = 0;
903 }
904#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000905#ifdef IA64
906 if (ia32) {
907 int err;
908
909 err = (int)r8;
910 if (err < 0 && -err < nerrnos) {
911 tcp->u_rval = -1;
912 u_error = -err;
913 }
914 else {
915 tcp->u_rval = err;
916 u_error = 0;
917 }
918 } else {
919 if (r10) {
920 tcp->u_rval = -1;
921 u_error = r8;
922 } else {
923 tcp->u_rval = r8;
924 u_error = 0;
925 }
926 }
927#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000928#ifdef MIPS
929 if (a3) {
930 tcp->u_rval = -1;
931 u_error = r2;
932 } else {
933 tcp->u_rval = r2;
934 u_error = 0;
935 }
936#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000937#ifdef POWERPC
938 if (result && (unsigned) -result < nerrnos) {
939 tcp->u_rval = -1;
940 u_error = -result;
941 }
942 else {
943 tcp->u_rval = result;
944 u_error = 0;
945 }
946#else /* !POWERPC */
947#ifdef M68K
948 if (d0 && (unsigned) -d0 < nerrnos) {
949 tcp->u_rval = -1;
950 u_error = -d0;
951 }
952 else {
953 tcp->u_rval = d0;
954 u_error = 0;
955 }
956#else /* !M68K */
957#ifdef ARM
958 if (r0 && (unsigned) -r0 < nerrnos) {
959 tcp->u_rval = -1;
960 u_error = -r0;
961 }
962 else {
963 tcp->u_rval = r0;
964 u_error = 0;
965 }
966#else /* !ARM */
967#ifdef ALPHA
968 if (a3) {
969 tcp->u_rval = -1;
970 u_error = r0;
971 }
972 else {
973 tcp->u_rval = r0;
974 u_error = 0;
975 }
976#else /* !ALPHA */
977#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000978 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000979 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000980 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000981 }
982 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000983 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000984 u_error = 0;
985 }
986#endif /* SPARC */
987#endif /* ALPHA */
988#endif /* ARM */
989#endif /* M68K */
990#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000991#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000992#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000993#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000994#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000995#endif /* LINUX */
996#ifdef SUNOS4
997 /* get error code from user struct */
998 if (upeek(pid, uoff(u_error), &u_error) < 0)
999 return -1;
1000 u_error >>= 24; /* u_error is a char */
1001
1002 /* get system call return value */
1003 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1004 return -1;
1005#endif /* SUNOS4 */
1006#ifdef SVR4
1007#ifdef SPARC
1008 /* Judicious guessing goes a long way. */
1009 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1010 tcp->u_rval = -1;
1011 u_error = tcp->status.pr_reg[R_O0];
1012 }
1013 else {
1014 tcp->u_rval = tcp->status.pr_reg[R_O0];
1015 u_error = 0;
1016 }
1017#endif /* SPARC */
1018#ifdef I386
1019 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001020 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001021 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001022 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001023 }
1024 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001025 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001026 u_error = 0;
1027 }
1028#endif /* I386 */
1029#ifdef MIPS
1030 if (tcp->status.pr_reg[CTX_A3]) {
1031 tcp->u_rval = -1;
1032 u_error = tcp->status.pr_reg[CTX_V0];
1033 }
1034 else {
1035 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1036 u_error = 0;
1037 }
1038#endif /* MIPS */
1039#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001040 tcp->u_error = u_error;
1041 return 1;
1042}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001043
Pavel Machek4dc3b142000-02-01 17:58:41 +00001044int syscall_enter(tcp)
1045struct tcb *tcp;
1046{
1047 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001048#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001049#if defined(S390)
1050 {
1051 int i;
1052 tcp->u_nargs = sysent[tcp->scno].nargs;
1053 for (i = 0; i < tcp->u_nargs; i++) {
1054 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1055 return -1;
1056 }
1057 }
1058#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001059 {
1060 int i;
1061 tcp->u_nargs = sysent[tcp->scno].nargs;
1062 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001063 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1064 * for scno somewhere above here!
1065 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001066 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1067 return -1;
1068 }
1069 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001070#elif defined (IA64)
1071 {
1072 unsigned long *bsp, i;
1073
1074 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1075 return -1;
1076
1077 tcp->u_nargs = sysent[tcp->scno].nargs;
1078 for (i = 0; i < tcp->u_nargs; ++i) {
1079 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1080 (char *) &tcp->u_arg[i])
1081 < 0)
1082 return -1;
1083 }
1084 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001085#elif defined (MIPS)
1086 {
1087 long sp;
1088 int i, nargs;
1089
1090 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1091 if(nargs > 4) {
1092 if(upeek(pid, REG_SP, &sp) < 0)
1093 return -1;
1094 for(i = 0; i < 4; i++) {
1095 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1096 return -1;
1097 }
1098 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1099 (char *)(tcp->u_arg + 4));
1100 } else {
1101 for(i = 0; i < nargs; i++) {
1102 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1103 return -1;
1104 }
1105 }
1106 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001107#elif defined (POWERPC)
1108 {
1109 int i;
1110 tcp->u_nargs = sysent[tcp->scno].nargs;
1111 for (i = 0; i < tcp->u_nargs; i++) {
1112 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1113 return -1;
1114 }
1115 }
1116#elif defined (SPARC)
1117 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001118 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001119
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001120 tcp->u_nargs = sysent[tcp->scno].nargs;
1121 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001122 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001123 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001124#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001125 {
1126 int i;
1127 tcp->u_nargs = sysent[tcp->scno].nargs;
1128 for (i = 0; i < tcp->u_nargs; i++) {
1129 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1130 return -1;
1131 }
1132 }
1133#endif
1134#endif /* LINUX */
1135#ifdef SUNOS4
1136 {
1137 int i;
1138 tcp->u_nargs = sysent[tcp->scno].nargs;
1139 for (i = 0; i < tcp->u_nargs; i++) {
1140 struct user *u;
1141
1142 if (upeek(pid, uoff(u_arg[0]) +
1143 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1144 return -1;
1145 }
1146 }
1147#endif /* SUNOS4 */
1148#ifdef SVR4
1149#ifdef MIPS
1150 /*
1151 * SGI is broken: even though it has pr_sysarg, it doesn't
1152 * set them on system call entry. Get a clue.
1153 */
1154 if (sysent[tcp->scno].nargs != -1)
1155 tcp->u_nargs = sysent[tcp->scno].nargs;
1156 else
1157 tcp->u_nargs = tcp->status.pr_nsysarg;
1158 if (tcp->u_nargs > 4) {
1159 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1160 4*sizeof(tcp->u_arg[0]));
1161 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1162 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1163 }
1164 else {
1165 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1166 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1167 }
1168#else /* !MIPS */
1169#ifdef HAVE_PR_SYSCALL
1170 if (sysent[tcp->scno].nargs != -1)
1171 tcp->u_nargs = sysent[tcp->scno].nargs;
1172 else
1173 tcp->u_nargs = tcp->status.pr_nsysarg;
1174 {
1175 int i;
1176 for (i = 0; i < tcp->u_nargs; i++)
1177 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1178 }
1179#else /* !HAVE_PR_SYSCALL */
1180#ifdef I386
1181 if (sysent[tcp->scno].nargs != -1)
1182 tcp->u_nargs = sysent[tcp->scno].nargs;
1183 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001184#if UNIXWARE >= 2
1185 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1186#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001187 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001188#endif
1189 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001190 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1191#endif /* I386 */
1192#endif /* !HAVE_PR_SYSCALL */
1193#endif /* !MIPS */
1194#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001195 return 1;
1196}
1197
1198int
1199trace_syscall(tcp)
1200struct tcb *tcp;
1201{
1202 int sys_res;
1203 struct timeval tv;
1204 int res;
1205
1206 /* Measure the exit time as early as possible to avoid errors. */
1207 if (dtime && (tcp->flags & TCB_INSYSCALL))
1208 gettimeofday(&tv, NULL);
1209
1210 res = get_scno(tcp);
1211 if (res != 1)
1212 return res;
1213
1214 res = syscall_fixup(tcp);
1215 if (res != 1)
1216 return res;
1217
1218 if (tcp->flags & TCB_INSYSCALL) {
1219 long u_error;
1220 res = get_error(tcp);
1221 if (res != 1)
1222 return res;
1223 u_error = tcp->u_error;
1224
1225
1226 internal_syscall(tcp);
1227 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1228 tcp->flags &= ~TCB_INSYSCALL;
1229 return 0;
1230 }
1231
1232 if (tcp->flags & TCB_REPRINT) {
1233 printleader(tcp);
1234 tprintf("<... ");
1235 if (tcp->scno >= nsyscalls)
1236 tprintf("syscall_%lu", tcp->scno);
1237 else
1238 tprintf("%s", sysent[tcp->scno].sys_name);
1239 tprintf(" resumed> ");
1240 }
1241
1242 if (cflag) {
1243 call_count[tcp->scno]++;
1244 if (tcp->u_error)
1245 error_count[tcp->scno]++;
1246 tv_sub(&tv, &tv, &tcp->etime);
1247#ifdef LINUX
1248 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1249 static struct timeval one_tick =
1250 { 0, 1000000 / HZ };
1251
1252 if (tv_nz(&tcp->dtime))
1253 tv = tcp->dtime;
1254 else if (tv_cmp(&tv, &one_tick) > 0) {
1255 if (tv_cmp(&shortest, &one_tick) < 0)
1256 tv = shortest;
1257 else
1258 tv = one_tick;
1259 }
1260 }
1261#endif /* LINUX */
1262 if (tv_cmp(&tv, &shortest) < 0)
1263 shortest = tv;
1264 tv_add(&tv_count[tcp->scno],
1265 &tv_count[tcp->scno], &tv);
1266 tcp->flags &= ~TCB_INSYSCALL;
1267 return 0;
1268 }
1269
1270 if (tcp->scno >= nsyscalls
1271 || (qual_flags[tcp->scno] & QUAL_RAW))
1272 sys_res = printargs(tcp);
1273 else
1274 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1275 u_error = tcp->u_error;
1276 tprintf(") ");
1277 tabto(acolumn);
1278 if (qual_flags[tcp->scno] & QUAL_RAW) {
1279 if (u_error)
1280 tprintf("= -1 (errno %ld)", u_error);
1281 else
1282 tprintf("= %#lx", tcp->u_rval);
1283 }
1284 else if (!(sys_res & RVAL_NONE) && u_error) {
1285 switch (u_error) {
1286#ifdef LINUX
1287 case ERESTARTSYS:
1288 tprintf("= ? ERESTARTSYS (To be restarted)");
1289 break;
1290 case ERESTARTNOINTR:
1291 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1292 break;
1293 case ERESTARTNOHAND:
1294 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1295 break;
1296#endif /* LINUX */
1297 default:
1298 tprintf("= -1 ");
1299 if (u_error < nerrnos && u_error < sys_nerr)
1300 tprintf("%s (%s)", errnoent[u_error],
1301 sys_errlist[u_error]);
1302 else if (u_error < nerrnos)
1303 tprintf("%s (errno %ld)",
1304 errnoent[u_error], u_error);
1305 else if (u_error < sys_nerr)
1306 tprintf("ERRNO_%ld (%s)", u_error,
1307 sys_errlist[u_error]);
1308 else
1309 tprintf("E??? (errno %ld)", u_error);
1310 break;
1311 }
1312 }
1313 else {
1314 if (sys_res & RVAL_NONE)
1315 tprintf("= ?");
1316 else {
1317 switch (sys_res & RVAL_MASK) {
1318 case RVAL_HEX:
1319 tprintf("= %#lx", tcp->u_rval);
1320 break;
1321 case RVAL_OCTAL:
1322 tprintf("= %#lo", tcp->u_rval);
1323 break;
1324 case RVAL_UDECIMAL:
1325 tprintf("= %lu", tcp->u_rval);
1326 break;
1327 case RVAL_DECIMAL:
1328 tprintf("= %ld", tcp->u_rval);
1329 break;
1330 default:
1331 fprintf(stderr,
1332 "invalid rval format\n");
1333 break;
1334 }
1335 }
1336 if ((sys_res & RVAL_STR) && tcp->auxstr)
1337 tprintf(" (%s)", tcp->auxstr);
1338 }
1339 if (dtime) {
1340 tv_sub(&tv, &tv, &tcp->etime);
1341 tprintf(" <%ld.%06ld>",
1342 (long) tv.tv_sec, (long) tv.tv_usec);
1343 }
1344 printtrailer(tcp);
1345
1346 dumpio(tcp);
1347 if (fflush(tcp->outf) == EOF)
1348 return -1;
1349 tcp->flags &= ~TCB_INSYSCALL;
1350 return 0;
1351 }
1352
1353 /* Entering system call */
1354 res = syscall_enter(tcp);
1355 if (res != 1)
1356 return res;
1357
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001358 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001359#ifdef LINUX
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001360#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361 case SYS_socketcall:
1362 decode_subcall(tcp, SYS_socket_subcall,
1363 SYS_socket_nsubcalls, deref_style);
1364 break;
1365 case SYS_ipc:
1366 decode_subcall(tcp, SYS_ipc_subcall,
1367 SYS_ipc_nsubcalls, shift_style);
1368 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001369#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001370#ifdef SPARC
1371 case SYS_socketcall:
1372 sparc_socket_decode (tcp);
1373 break;
1374#endif
1375#endif /* LINUX */
1376#ifdef SVR4
1377#ifdef SYS_pgrpsys_subcall
1378 case SYS_pgrpsys:
1379 decode_subcall(tcp, SYS_pgrpsys_subcall,
1380 SYS_pgrpsys_nsubcalls, shift_style);
1381 break;
1382#endif /* SYS_pgrpsys_subcall */
1383#ifdef SYS_sigcall_subcall
1384 case SYS_sigcall:
1385 decode_subcall(tcp, SYS_sigcall_subcall,
1386 SYS_sigcall_nsubcalls, mask_style);
1387 break;
1388#endif /* SYS_sigcall_subcall */
1389 case SYS_msgsys:
1390 decode_subcall(tcp, SYS_msgsys_subcall,
1391 SYS_msgsys_nsubcalls, shift_style);
1392 break;
1393 case SYS_shmsys:
1394 decode_subcall(tcp, SYS_shmsys_subcall,
1395 SYS_shmsys_nsubcalls, shift_style);
1396 break;
1397 case SYS_semsys:
1398 decode_subcall(tcp, SYS_semsys_subcall,
1399 SYS_semsys_nsubcalls, shift_style);
1400 break;
1401#if 0 /* broken */
1402 case SYS_utssys:
1403 decode_subcall(tcp, SYS_utssys_subcall,
1404 SYS_utssys_nsubcalls, shift_style);
1405 break;
1406#endif
1407 case SYS_sysfs:
1408 decode_subcall(tcp, SYS_sysfs_subcall,
1409 SYS_sysfs_nsubcalls, shift_style);
1410 break;
1411 case SYS_spcall:
1412 decode_subcall(tcp, SYS_spcall_subcall,
1413 SYS_spcall_nsubcalls, shift_style);
1414 break;
1415#ifdef SYS_context_subcall
1416 case SYS_context:
1417 decode_subcall(tcp, SYS_context_subcall,
1418 SYS_context_nsubcalls, shift_style);
1419 break;
1420#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001421#ifdef SYS_door_subcall
1422 case SYS_door:
1423 decode_subcall(tcp, SYS_door_subcall,
1424 SYS_door_nsubcalls, door_style);
1425 break;
1426#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001427#endif /* SVR4 */
1428#ifdef SUNOS4
1429 case SYS_semsys:
1430 decode_subcall(tcp, SYS_semsys_subcall,
1431 SYS_semsys_nsubcalls, shift_style);
1432 break;
1433 case SYS_msgsys:
1434 decode_subcall(tcp, SYS_msgsys_subcall,
1435 SYS_msgsys_nsubcalls, shift_style);
1436 break;
1437 case SYS_shmsys:
1438 decode_subcall(tcp, SYS_shmsys_subcall,
1439 SYS_shmsys_nsubcalls, shift_style);
1440 break;
1441#endif
1442 }
1443
1444 internal_syscall(tcp);
1445 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1446 tcp->flags |= TCB_INSYSCALL;
1447 return 0;
1448 }
1449
1450 if (cflag) {
1451 gettimeofday(&tcp->etime, NULL);
1452 tcp->flags |= TCB_INSYSCALL;
1453 return 0;
1454 }
1455
1456 printleader(tcp);
1457 tcp->flags &= ~TCB_REPRINT;
1458 tcp_last = tcp;
1459 if (tcp->scno >= nsyscalls)
1460 tprintf("syscall_%lu(", tcp->scno);
1461 else
1462 tprintf("%s(", sysent[tcp->scno].sys_name);
1463 if (tcp->scno >= nsyscalls ||
1464 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1465 sys_res = printargs(tcp);
1466 else
1467 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1468 if (fflush(tcp->outf) == EOF)
1469 return -1;
1470 tcp->flags |= TCB_INSYSCALL;
1471 /* Measure the entrance time as late as possible to avoid errors. */
1472 if (dtime)
1473 gettimeofday(&tcp->etime, NULL);
1474 return sys_res;
1475}
1476
1477int
1478printargs(tcp)
1479struct tcb *tcp;
1480{
1481 if (entering(tcp)) {
1482 int i;
1483
1484 for (i = 0; i < tcp->u_nargs; i++)
1485 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1486 }
1487 return 0;
1488}
1489
1490long
1491getrval2(tcp)
1492struct tcb *tcp;
1493{
1494 long val = -1;
1495
1496#ifdef LINUX
1497#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001498 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001499 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1500 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001501 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001502#endif /* SPARC */
1503#endif /* LINUX */
1504
1505#ifdef SUNOS4
1506 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1507 return -1;
1508#endif /* SUNOS4 */
1509
1510#ifdef SVR4
1511#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001512 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001513#endif /* SPARC */
1514#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001515 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001516#endif /* I386 */
1517#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001518 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001519#endif /* MIPS */
1520#endif /* SVR4 */
1521
1522 return val;
1523}
1524
1525/*
1526 * Apparently, indirect system calls have already be converted by ptrace(2),
1527 * so if you see "indir" this program has gone astray.
1528 */
1529int
1530sys_indir(tcp)
1531struct tcb *tcp;
1532{
1533 int i, scno, nargs;
1534
1535 if (entering(tcp)) {
1536 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1537 fprintf(stderr, "Bogus syscall: %u\n", scno);
1538 return 0;
1539 }
1540 nargs = sysent[scno].nargs;
1541 tprintf("%s", sysent[scno].sys_name);
1542 for (i = 0; i < nargs; i++)
1543 tprintf(", %#lx", tcp->u_arg[i+1]);
1544 }
1545 return 0;
1546}
1547
1548static int
1549time_cmp(a, b)
1550void *a;
1551void *b;
1552{
1553 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1554}
1555
1556static int
1557syscall_cmp(a, b)
1558void *a;
1559void *b;
1560{
1561 return strcmp(sysent[*((int *) a)].sys_name,
1562 sysent[*((int *) b)].sys_name);
1563}
1564
1565static int
1566count_cmp(a, b)
1567void *a;
1568void *b;
1569{
1570 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1571
1572 return (m < n) ? 1 : (m > n) ? -1 : 0;
1573}
1574
1575static int (*sortfun)();
1576static struct timeval overhead = { -1, -1 };
1577
1578void
1579set_sortby(sortby)
1580char *sortby;
1581{
1582 if (strcmp(sortby, "time") == 0)
1583 sortfun = time_cmp;
1584 else if (strcmp(sortby, "calls") == 0)
1585 sortfun = count_cmp;
1586 else if (strcmp(sortby, "name") == 0)
1587 sortfun = syscall_cmp;
1588 else if (strcmp(sortby, "nothing") == 0)
1589 sortfun = NULL;
1590 else {
1591 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1592 exit(1);
1593 }
1594}
1595
1596void set_overhead(n)
1597int n;
1598{
1599 overhead.tv_sec = n / 1000000;
1600 overhead.tv_usec = n % 1000000;
1601}
1602
1603void
1604call_summary(outf)
1605FILE *outf;
1606{
1607 int i, j;
1608 int call_cum, error_cum;
1609 struct timeval tv_cum, dtv;
1610 double percent;
1611 char *dashes = "-------------------------";
1612 char error_str[16];
1613
1614 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1615 if (overhead.tv_sec == -1) {
1616 tv_mul(&overhead, &shortest, 8);
1617 tv_div(&overhead, &overhead, 10);
1618 }
1619 for (i = 0; i < nsyscalls; i++) {
1620 sorted_count[i] = i;
1621 if (call_count[i] == 0)
1622 continue;
1623 tv_mul(&dtv, &overhead, call_count[i]);
1624 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1625 call_cum += call_count[i];
1626 error_cum += error_count[i];
1627 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1628 }
1629 if (sortfun)
1630 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1631 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1632 "% time", "seconds", "usecs/call",
1633 "calls", "errors", "syscall");
1634 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1635 dashes, dashes, dashes, dashes, dashes, dashes);
1636 for (i = 0; i < nsyscalls; i++) {
1637 j = sorted_count[i];
1638 if (call_count[j] == 0)
1639 continue;
1640 tv_div(&dtv, &tv_count[j], call_count[j]);
1641 if (error_count[j])
1642 sprintf(error_str, "%d", error_count[j]);
1643 else
1644 error_str[0] = '\0';
1645 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1646 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1647 percent, (long) tv_count[j].tv_sec,
1648 (long) tv_count[j].tv_usec,
1649 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1650 call_count[j], error_str, sysent[j].sys_name);
1651 }
1652 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1653 dashes, dashes, dashes, dashes, dashes, dashes);
1654 if (error_cum)
1655 sprintf(error_str, "%d", error_cum);
1656 else
1657 error_str[0] = '\0';
1658 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1659 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1660 call_cum, error_str, "total");
1661}