blob: 96424460de3368595c1f1d3d8cfc4df850cd43f3 [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;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000467 switch (style) {
468 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000469 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
470 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000471 tcp->scno = subcall + tcp->u_arg[0];
472 if (sysent[tcp->scno].nargs != -1)
473 tcp->u_nargs = sysent[tcp->scno].nargs;
474 else
475 tcp->u_nargs--;
476 for (i = 0; i < tcp->u_nargs; i++)
477 tcp->u_arg[i] = tcp->u_arg[i + 1];
478 break;
479 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000480 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
481 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000482 tcp->scno = subcall + tcp->u_arg[0];
483 addr = tcp->u_arg[1];
484 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
485 if (umove(tcp, addr, &arg) < 0)
486 arg = 0;
487 tcp->u_arg[i] = arg;
488 addr += sizeof(arg);
489 }
490 tcp->u_nargs = sysent[tcp->scno].nargs;
491 break;
492 case mask_style:
493 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000494 for (i = 0; mask; i++)
495 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000496 if (i >= nsubcalls)
497 return;
498 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000499 tcp->scno = subcall + i;
500 if (sysent[tcp->scno].nargs != -1)
501 tcp->u_nargs = sysent[tcp->scno].nargs;
502 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000503 case door_style:
504 /*
505 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000506 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000507 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000508 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
509 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000510 tcp->scno = subcall + tcp->u_arg[5];
511 if (sysent[tcp->scno].nargs != -1)
512 tcp->u_nargs = sysent[tcp->scno].nargs;
513 else
514 tcp->u_nargs--;
515 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516 }
517}
518#endif
519
520struct tcb *tcp_last = NULL;
521
522static int
523internal_syscall(tcp)
524struct tcb *tcp;
525{
526 /*
527 * We must always trace a few critical system calls in order to
528 * correctly support following forks in the presence of tracing
529 * qualifiers.
530 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000531 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000532#ifdef SYS_fork
533 case SYS_fork:
534#endif
535#ifdef SYS_vfork
536 case SYS_vfork:
537#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 internal_fork(tcp);
539 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000540#ifdef SYS_clone
541 case SYS_clone:
542 internal_clone(tcp);
543 break;
544#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000545#ifdef SYS_execv
546 case SYS_execv:
547#endif
548#ifdef SYS_execve
549 case SYS_execve:
550#endif
551 internal_exec(tcp);
552 break;
553
554#ifdef SYS_wait
555 case SYS_wait:
556#endif
557#ifdef SYS_wait4
558 case SYS_wait4:
559#endif
560#ifdef SYS_waitpid
561 case SYS_waitpid:
562#endif
563#ifdef SYS_waitsys
564 case SYS_waitsys:
565#endif
566 internal_wait(tcp);
567 break;
568
569#ifdef SYS_exit
570 case SYS_exit:
571#endif
572 internal_exit(tcp);
573 break;
574 }
575 return 0;
576}
577
Wichert Akkermanc7926982000-04-10 22:22:31 +0000578
579#ifdef LINUX
580#if defined (I386)
581 static long eax;
582#elif defined (IA64)
583 long r8, r10, psr;
584 long ia32 = 0;
585#elif defined (POWERPC)
586 static long result,flags;
587#elif defined (M68K)
588 static int d0;
589#elif defined (ARM)
590 static int r0;
591#elif defined (ALPHA)
592 static long r0;
593 static long a3;
594#elif defined (SPARC)
595 static struct pt_regs regs;
596 static unsigned long trap;
597#elif defined(MIPS)
598 static long a3;
599 static long r2;
600#elif defined(S390)
601 static long gpr2;
602 static long pc;
603#endif
604#endif /* LINUX */
605
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000606int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000607get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000608struct tcb *tcp;
609{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000610 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000611 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000612
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000613#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000614#if defined(S390)
615 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
616 return -1;
617 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
618 if (errno)
619 return -1;
620 scno&=0xFF;
621#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000622 if (upeek(pid, 4*PT_R0, &scno) < 0)
623 return -1;
624 if (!(tcp->flags & TCB_INSYSCALL)) {
625 /* Check if we return from execve. */
626 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
627 tcp->flags &= ~TCB_WAITEXECVE;
628 return 0;
629 }
630 }
631#elif defined (I386)
632 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
633 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000634#elif defined(IA64)
635#define IA64_PSR_IS ((long)1 << 34)
636 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
637 ia32 = (psr & IA64_PSR_IS);
638 if (!(tcp->flags & TCB_INSYSCALL)) {
639 if (ia32) {
640 if (upeek(pid, PT_R8, &scno) < 0)
641 return -1;
642 } else {
643 if (upeek (pid, PT_R15, &scno) < 0)
644 return -1;
645 }
646 } else {
647 /* syscall in progress */
648 if (upeek (pid, PT_R8, &r8) < 0)
649 return -1;
650 if (upeek (pid, PT_R10, &r10) < 0)
651 return -1;
652 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000653#elif defined (ARM)
654 {
655 long pc;
656 upeek(pid, 4*15, &pc);
657 umoven(tcp, pc-4, 4, (char *)&scno);
658 scno &= 0x000fffff;
659 }
660#elif defined (M68K)
661 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
662 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000663#elif defined (MIPS)
664 if (upeek(pid, REG_A3, &a3) < 0)
665 return -1;
666
667 if(!(tcp->flags & TCB_INSYSCALL)) {
668 if (upeek(pid, REG_V0, &scno) < 0)
669 return -1;
670
671 if (scno < 0 || scno > nsyscalls) {
672 if(a3 == 0 || a3 == -1) {
673 if(debug)
674 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
675 return 0;
676 }
677 }
678 } else {
679 if (upeek(pid, REG_V0, &r2) < 0)
680 return -1;
681 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000682#elif defined (ALPHA)
683 if (upeek(pid, REG_A3, &a3) < 0)
684 return -1;
685
686 if (!(tcp->flags & TCB_INSYSCALL)) {
687 if (upeek(pid, REG_R0, &scno) < 0)
688 return -1;
689
690 /* Check if we return from execve. */
691 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
692 tcp->flags &= ~TCB_WAITEXECVE;
693 return 0;
694 }
695
696 /*
697 * Do some sanity checks to figure out if it's
698 * really a syscall entry
699 */
700 if (scno < 0 || scno > nsyscalls) {
701 if (a3 == 0 || a3 == -1) {
702 if (debug)
703 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
704 return 0;
705 }
706 }
707 }
708 else {
709 if (upeek(pid, REG_R0, &r0) < 0)
710 return -1;
711 }
712#elif defined (SPARC)
713 /* Everything we need is in the current register set. */
714 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
715 return -1;
716
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000717 /* If we are entering, then disassemble the syscall trap. */
718 if (!(tcp->flags & TCB_INSYSCALL)) {
719 /* Retrieve the syscall trap instruction. */
720 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000721 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000722 if (errno)
723 return -1;
724
725 /* Disassemble the trap to see what personality to use. */
726 switch (trap) {
727 case 0x91d02010:
728 /* Linux/SPARC syscall trap. */
729 set_personality(0);
730 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000731 case 0x91d0206d:
732 /* Linux/SPARC64 syscall trap. */
733 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
734 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000735 case 0x91d02000:
736 /* SunOS syscall trap. (pers 1) */
737 fprintf(stderr,"syscall: SunOS no support\n");
738 return -1;
739 case 0x91d02008:
740 /* Solaris 2.x syscall trap. (per 2) */
741 set_personality(1);
742 break;
743 case 0x91d02009:
744 /* NetBSD/FreeBSD syscall trap. */
745 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
746 return -1;
747 case 0x91d02027:
748 /* Solaris 2.x gettimeofday */
749 set_personality(1);
750 break;
751 default:
752 /* Unknown syscall trap. */
753 if(tcp->flags & TCB_WAITEXECVE) {
754 tcp->flags &= ~TCB_WAITEXECVE;
755 return 0;
756 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000757 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000758 return -1;
759 }
760
761 /* Extract the system call number from the registers. */
762 if (trap == 0x91d02027)
763 scno = 156;
764 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000765 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000766 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000767 scno = regs.r_o0;
768 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000769 }
770 }
771#endif
772#endif /* LINUX */
773#ifdef SUNOS4
774 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
775 return -1;
776#endif
777#ifdef SVR4
778#ifdef HAVE_PR_SYSCALL
779 scno = tcp->status.pr_syscall;
780#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000781 scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000782#endif /* !HAVE_PR_SYSCALL */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000783#endif
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000784 if (!(tcp->flags & TCB_INSYSCALL))
785 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000786 return 1;
787}
788
Pavel Machek4dc3b142000-02-01 17:58:41 +0000789
790int
791syscall_fixup(tcp)
792struct tcb *tcp;
793{
794 int pid = tcp->pid;
795
796#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000797 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000798 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000799 if (
800 scno == SYS_fork
801#ifdef SYS_vfork
802 || scno == SYS_vfork
803#endif /* SYS_vfork */
804 ) {
805 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000806 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000807 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000808 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000809 }
810 else {
811 fprintf(stderr, "syscall: missing entry\n");
812 tcp->flags |= TCB_INSYSCALL;
813 }
814 }
815 }
816 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000817 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000818 fprintf(stderr, "syscall: missing exit\n");
819 tcp->flags &= ~TCB_INSYSCALL;
820 }
821 }
822#endif /* SVR4 */
823#ifdef SUNOS4
824 if (!(tcp->flags & TCB_INSYSCALL)) {
825 if (scno == 0) {
826 fprintf(stderr, "syscall: missing entry\n");
827 tcp->flags |= TCB_INSYSCALL;
828 }
829 }
830 else {
831 if (scno != 0) {
832 if (debug) {
833 /*
834 * This happens when a signal handler
835 * for a signal which interrupted a
836 * a system call makes another system call.
837 */
838 fprintf(stderr, "syscall: missing exit\n");
839 }
840 tcp->flags &= ~TCB_INSYSCALL;
841 }
842 }
843#endif /* SUNOS4 */
844#ifdef LINUX
845#if defined (I386)
846 if (upeek(pid, 4*EAX, &eax) < 0)
847 return -1;
848 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
849 if (debug)
850 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
851 return 0;
852 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000853#elif defined (S390)
854 if (upeek(pid, PT_GPR2, &gpr2) < 0)
855 return -1;
856 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
857 if (debug)
858 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
859 return 0;
860 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000861#elif defined (POWERPC)
862# define SO_MASK 0x10000000
863 if (upeek(pid, 4*PT_CCR, &flags) < 0)
864 return -1;
865 if (upeek(pid, 4*PT_R3, &result) < 0)
866 return -1;
867 if (flags & SO_MASK)
868 result = -result;
869#elif defined (M68K)
870 if (upeek(pid, 4*PT_D0, &d0) < 0)
871 return -1;
872 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
873 if (debug)
874 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
875 return 0;
876 }
877#elif defined (ARM)
878 if (upeek(pid, 4*0, (long *)&r0) < 0)
879 return -1;
880 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
881 if (debug)
882 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
883 return 0;
884 }
885#else
886#endif
887#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000888 return 1;
889}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000890
Pavel Machek4dc3b142000-02-01 17:58:41 +0000891int
892get_error(tcp)
893struct tcb *tcp;
894{
895 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000896#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000897#ifdef S390
898 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
899 tcp->u_rval = -1;
900 u_error = -gpr2;
901 }
902 else {
903 tcp->u_rval = gpr2;
904 u_error = 0;
905 }
906#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000907#ifdef I386
908 if (eax < 0 && -eax < nerrnos) {
909 tcp->u_rval = -1;
910 u_error = -eax;
911 }
912 else {
913 tcp->u_rval = eax;
914 u_error = 0;
915 }
916#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000917#ifdef IA64
918 if (ia32) {
919 int err;
920
921 err = (int)r8;
922 if (err < 0 && -err < nerrnos) {
923 tcp->u_rval = -1;
924 u_error = -err;
925 }
926 else {
927 tcp->u_rval = err;
928 u_error = 0;
929 }
930 } else {
931 if (r10) {
932 tcp->u_rval = -1;
933 u_error = r8;
934 } else {
935 tcp->u_rval = r8;
936 u_error = 0;
937 }
938 }
939#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000940#ifdef MIPS
941 if (a3) {
942 tcp->u_rval = -1;
943 u_error = r2;
944 } else {
945 tcp->u_rval = r2;
946 u_error = 0;
947 }
948#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000949#ifdef POWERPC
950 if (result && (unsigned) -result < nerrnos) {
951 tcp->u_rval = -1;
952 u_error = -result;
953 }
954 else {
955 tcp->u_rval = result;
956 u_error = 0;
957 }
958#else /* !POWERPC */
959#ifdef M68K
960 if (d0 && (unsigned) -d0 < nerrnos) {
961 tcp->u_rval = -1;
962 u_error = -d0;
963 }
964 else {
965 tcp->u_rval = d0;
966 u_error = 0;
967 }
968#else /* !M68K */
969#ifdef ARM
970 if (r0 && (unsigned) -r0 < nerrnos) {
971 tcp->u_rval = -1;
972 u_error = -r0;
973 }
974 else {
975 tcp->u_rval = r0;
976 u_error = 0;
977 }
978#else /* !ARM */
979#ifdef ALPHA
980 if (a3) {
981 tcp->u_rval = -1;
982 u_error = r0;
983 }
984 else {
985 tcp->u_rval = r0;
986 u_error = 0;
987 }
988#else /* !ALPHA */
989#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000990 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000991 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000992 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000993 }
994 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000995 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000996 u_error = 0;
997 }
998#endif /* SPARC */
999#endif /* ALPHA */
1000#endif /* ARM */
1001#endif /* M68K */
1002#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001003#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001004#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001005#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001006#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001007#endif /* LINUX */
1008#ifdef SUNOS4
1009 /* get error code from user struct */
1010 if (upeek(pid, uoff(u_error), &u_error) < 0)
1011 return -1;
1012 u_error >>= 24; /* u_error is a char */
1013
1014 /* get system call return value */
1015 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1016 return -1;
1017#endif /* SUNOS4 */
1018#ifdef SVR4
1019#ifdef SPARC
1020 /* Judicious guessing goes a long way. */
1021 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1022 tcp->u_rval = -1;
1023 u_error = tcp->status.pr_reg[R_O0];
1024 }
1025 else {
1026 tcp->u_rval = tcp->status.pr_reg[R_O0];
1027 u_error = 0;
1028 }
1029#endif /* SPARC */
1030#ifdef I386
1031 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001032 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001033 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001034 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001035 }
1036 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001037 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001038#ifdef HAVE_LONG_LONG
1039 tcp->u_lrval =
1040 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1041 tcp->status.PR_REG[EAX];
1042#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001043 u_error = 0;
1044 }
1045#endif /* I386 */
1046#ifdef MIPS
1047 if (tcp->status.pr_reg[CTX_A3]) {
1048 tcp->u_rval = -1;
1049 u_error = tcp->status.pr_reg[CTX_V0];
1050 }
1051 else {
1052 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1053 u_error = 0;
1054 }
1055#endif /* MIPS */
1056#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001057 tcp->u_error = u_error;
1058 return 1;
1059}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001060
Pavel Machek4dc3b142000-02-01 17:58:41 +00001061int syscall_enter(tcp)
1062struct tcb *tcp;
1063{
1064 int pid = tcp->pid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001065#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001066#if defined(S390)
1067 {
1068 int i;
1069 tcp->u_nargs = sysent[tcp->scno].nargs;
1070 for (i = 0; i < tcp->u_nargs; i++) {
1071 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1072 return -1;
1073 }
1074 }
1075#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001076 {
1077 int i;
1078 tcp->u_nargs = sysent[tcp->scno].nargs;
1079 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001080 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1081 * for scno somewhere above here!
1082 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001083 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1084 return -1;
1085 }
1086 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001087#elif defined (IA64)
1088 {
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001089 unsigned long *bsp, cfm, i;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001090
1091 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1092 return -1;
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001093 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1094 return -1;
1095
1096 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001097
1098 tcp->u_nargs = sysent[tcp->scno].nargs;
1099 for (i = 0; i < tcp->u_nargs; ++i) {
1100 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1101 (char *) &tcp->u_arg[i])
1102 < 0)
1103 return -1;
1104 }
1105 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001106#elif defined (MIPS)
1107 {
1108 long sp;
1109 int i, nargs;
1110
1111 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1112 if(nargs > 4) {
1113 if(upeek(pid, REG_SP, &sp) < 0)
1114 return -1;
1115 for(i = 0; i < 4; i++) {
1116 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1117 return -1;
1118 }
1119 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1120 (char *)(tcp->u_arg + 4));
1121 } else {
1122 for(i = 0; i < nargs; i++) {
1123 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1124 return -1;
1125 }
1126 }
1127 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001128#elif defined (POWERPC)
1129 {
1130 int i;
1131 tcp->u_nargs = sysent[tcp->scno].nargs;
1132 for (i = 0; i < tcp->u_nargs; i++) {
1133 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1134 return -1;
1135 }
1136 }
1137#elif defined (SPARC)
1138 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001139 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001140
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001141 tcp->u_nargs = sysent[tcp->scno].nargs;
1142 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001143 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001144 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001145#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001146 {
1147 int i;
1148 tcp->u_nargs = sysent[tcp->scno].nargs;
1149 for (i = 0; i < tcp->u_nargs; i++) {
1150 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1151 return -1;
1152 }
1153 }
1154#endif
1155#endif /* LINUX */
1156#ifdef SUNOS4
1157 {
1158 int i;
1159 tcp->u_nargs = sysent[tcp->scno].nargs;
1160 for (i = 0; i < tcp->u_nargs; i++) {
1161 struct user *u;
1162
1163 if (upeek(pid, uoff(u_arg[0]) +
1164 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1165 return -1;
1166 }
1167 }
1168#endif /* SUNOS4 */
1169#ifdef SVR4
1170#ifdef MIPS
1171 /*
1172 * SGI is broken: even though it has pr_sysarg, it doesn't
1173 * set them on system call entry. Get a clue.
1174 */
1175 if (sysent[tcp->scno].nargs != -1)
1176 tcp->u_nargs = sysent[tcp->scno].nargs;
1177 else
1178 tcp->u_nargs = tcp->status.pr_nsysarg;
1179 if (tcp->u_nargs > 4) {
1180 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1181 4*sizeof(tcp->u_arg[0]));
1182 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1183 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1184 }
1185 else {
1186 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1187 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1188 }
1189#else /* !MIPS */
1190#ifdef HAVE_PR_SYSCALL
1191 if (sysent[tcp->scno].nargs != -1)
1192 tcp->u_nargs = sysent[tcp->scno].nargs;
1193 else
1194 tcp->u_nargs = tcp->status.pr_nsysarg;
1195 {
1196 int i;
1197 for (i = 0; i < tcp->u_nargs; i++)
1198 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1199 }
1200#else /* !HAVE_PR_SYSCALL */
1201#ifdef I386
1202 if (sysent[tcp->scno].nargs != -1)
1203 tcp->u_nargs = sysent[tcp->scno].nargs;
1204 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001205#if UNIXWARE >= 2
1206 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1207#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001208 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001209#endif
1210 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001211 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1212#endif /* I386 */
1213#endif /* !HAVE_PR_SYSCALL */
1214#endif /* !MIPS */
1215#endif /* SVR4 */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001216 return 1;
1217}
1218
1219int
1220trace_syscall(tcp)
1221struct tcb *tcp;
1222{
1223 int sys_res;
1224 struct timeval tv;
1225 int res;
1226
1227 /* Measure the exit time as early as possible to avoid errors. */
1228 if (dtime && (tcp->flags & TCB_INSYSCALL))
1229 gettimeofday(&tv, NULL);
1230
1231 res = get_scno(tcp);
1232 if (res != 1)
1233 return res;
1234
1235 res = syscall_fixup(tcp);
1236 if (res != 1)
1237 return res;
1238
1239 if (tcp->flags & TCB_INSYSCALL) {
1240 long u_error;
1241 res = get_error(tcp);
1242 if (res != 1)
1243 return res;
1244 u_error = tcp->u_error;
1245
1246
1247 internal_syscall(tcp);
1248 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1249 tcp->flags &= ~TCB_INSYSCALL;
1250 return 0;
1251 }
1252
1253 if (tcp->flags & TCB_REPRINT) {
1254 printleader(tcp);
1255 tprintf("<... ");
1256 if (tcp->scno >= nsyscalls)
1257 tprintf("syscall_%lu", tcp->scno);
1258 else
1259 tprintf("%s", sysent[tcp->scno].sys_name);
1260 tprintf(" resumed> ");
1261 }
1262
1263 if (cflag) {
1264 call_count[tcp->scno]++;
1265 if (tcp->u_error)
1266 error_count[tcp->scno]++;
1267 tv_sub(&tv, &tv, &tcp->etime);
1268#ifdef LINUX
1269 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1270 static struct timeval one_tick =
1271 { 0, 1000000 / HZ };
1272
1273 if (tv_nz(&tcp->dtime))
1274 tv = tcp->dtime;
1275 else if (tv_cmp(&tv, &one_tick) > 0) {
1276 if (tv_cmp(&shortest, &one_tick) < 0)
1277 tv = shortest;
1278 else
1279 tv = one_tick;
1280 }
1281 }
1282#endif /* LINUX */
1283 if (tv_cmp(&tv, &shortest) < 0)
1284 shortest = tv;
1285 tv_add(&tv_count[tcp->scno],
1286 &tv_count[tcp->scno], &tv);
1287 tcp->flags &= ~TCB_INSYSCALL;
1288 return 0;
1289 }
1290
1291 if (tcp->scno >= nsyscalls
1292 || (qual_flags[tcp->scno] & QUAL_RAW))
1293 sys_res = printargs(tcp);
1294 else
1295 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1296 u_error = tcp->u_error;
1297 tprintf(") ");
1298 tabto(acolumn);
1299 if (qual_flags[tcp->scno] & QUAL_RAW) {
1300 if (u_error)
1301 tprintf("= -1 (errno %ld)", u_error);
1302 else
1303 tprintf("= %#lx", tcp->u_rval);
1304 }
1305 else if (!(sys_res & RVAL_NONE) && u_error) {
1306 switch (u_error) {
1307#ifdef LINUX
1308 case ERESTARTSYS:
1309 tprintf("= ? ERESTARTSYS (To be restarted)");
1310 break;
1311 case ERESTARTNOINTR:
1312 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1313 break;
1314 case ERESTARTNOHAND:
1315 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1316 break;
1317#endif /* LINUX */
1318 default:
1319 tprintf("= -1 ");
1320 if (u_error < nerrnos && u_error < sys_nerr)
1321 tprintf("%s (%s)", errnoent[u_error],
1322 sys_errlist[u_error]);
1323 else if (u_error < nerrnos)
1324 tprintf("%s (errno %ld)",
1325 errnoent[u_error], u_error);
1326 else if (u_error < sys_nerr)
1327 tprintf("ERRNO_%ld (%s)", u_error,
1328 sys_errlist[u_error]);
1329 else
1330 tprintf("E??? (errno %ld)", u_error);
1331 break;
1332 }
1333 }
1334 else {
1335 if (sys_res & RVAL_NONE)
1336 tprintf("= ?");
1337 else {
1338 switch (sys_res & RVAL_MASK) {
1339 case RVAL_HEX:
1340 tprintf("= %#lx", tcp->u_rval);
1341 break;
1342 case RVAL_OCTAL:
1343 tprintf("= %#lo", tcp->u_rval);
1344 break;
1345 case RVAL_UDECIMAL:
1346 tprintf("= %lu", tcp->u_rval);
1347 break;
1348 case RVAL_DECIMAL:
1349 tprintf("= %ld", tcp->u_rval);
1350 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001351#ifdef HAVE_LONG_LONG
1352 case RVAL_LDECIMAL:
1353 tprintf ("= %lld", tcp->u_lrval);
1354 break;
1355 /* LHEX, LOCTAL, LUDECIMAL... */
1356#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001357 default:
1358 fprintf(stderr,
1359 "invalid rval format\n");
1360 break;
1361 }
1362 }
1363 if ((sys_res & RVAL_STR) && tcp->auxstr)
1364 tprintf(" (%s)", tcp->auxstr);
1365 }
1366 if (dtime) {
1367 tv_sub(&tv, &tv, &tcp->etime);
1368 tprintf(" <%ld.%06ld>",
1369 (long) tv.tv_sec, (long) tv.tv_usec);
1370 }
1371 printtrailer(tcp);
1372
1373 dumpio(tcp);
1374 if (fflush(tcp->outf) == EOF)
1375 return -1;
1376 tcp->flags &= ~TCB_INSYSCALL;
1377 return 0;
1378 }
1379
1380 /* Entering system call */
1381 res = syscall_enter(tcp);
1382 if (res != 1)
1383 return res;
1384
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001385 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001386#ifdef LINUX
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001387#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001388 case SYS_socketcall:
1389 decode_subcall(tcp, SYS_socket_subcall,
1390 SYS_socket_nsubcalls, deref_style);
1391 break;
1392 case SYS_ipc:
1393 decode_subcall(tcp, SYS_ipc_subcall,
1394 SYS_ipc_nsubcalls, shift_style);
1395 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001396#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001397#ifdef SPARC
1398 case SYS_socketcall:
1399 sparc_socket_decode (tcp);
1400 break;
1401#endif
1402#endif /* LINUX */
1403#ifdef SVR4
1404#ifdef SYS_pgrpsys_subcall
1405 case SYS_pgrpsys:
1406 decode_subcall(tcp, SYS_pgrpsys_subcall,
1407 SYS_pgrpsys_nsubcalls, shift_style);
1408 break;
1409#endif /* SYS_pgrpsys_subcall */
1410#ifdef SYS_sigcall_subcall
1411 case SYS_sigcall:
1412 decode_subcall(tcp, SYS_sigcall_subcall,
1413 SYS_sigcall_nsubcalls, mask_style);
1414 break;
1415#endif /* SYS_sigcall_subcall */
1416 case SYS_msgsys:
1417 decode_subcall(tcp, SYS_msgsys_subcall,
1418 SYS_msgsys_nsubcalls, shift_style);
1419 break;
1420 case SYS_shmsys:
1421 decode_subcall(tcp, SYS_shmsys_subcall,
1422 SYS_shmsys_nsubcalls, shift_style);
1423 break;
1424 case SYS_semsys:
1425 decode_subcall(tcp, SYS_semsys_subcall,
1426 SYS_semsys_nsubcalls, shift_style);
1427 break;
1428#if 0 /* broken */
1429 case SYS_utssys:
1430 decode_subcall(tcp, SYS_utssys_subcall,
1431 SYS_utssys_nsubcalls, shift_style);
1432 break;
1433#endif
1434 case SYS_sysfs:
1435 decode_subcall(tcp, SYS_sysfs_subcall,
1436 SYS_sysfs_nsubcalls, shift_style);
1437 break;
1438 case SYS_spcall:
1439 decode_subcall(tcp, SYS_spcall_subcall,
1440 SYS_spcall_nsubcalls, shift_style);
1441 break;
1442#ifdef SYS_context_subcall
1443 case SYS_context:
1444 decode_subcall(tcp, SYS_context_subcall,
1445 SYS_context_nsubcalls, shift_style);
1446 break;
1447#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001448#ifdef SYS_door_subcall
1449 case SYS_door:
1450 decode_subcall(tcp, SYS_door_subcall,
1451 SYS_door_nsubcalls, door_style);
1452 break;
1453#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001454#endif /* SVR4 */
1455#ifdef SUNOS4
1456 case SYS_semsys:
1457 decode_subcall(tcp, SYS_semsys_subcall,
1458 SYS_semsys_nsubcalls, shift_style);
1459 break;
1460 case SYS_msgsys:
1461 decode_subcall(tcp, SYS_msgsys_subcall,
1462 SYS_msgsys_nsubcalls, shift_style);
1463 break;
1464 case SYS_shmsys:
1465 decode_subcall(tcp, SYS_shmsys_subcall,
1466 SYS_shmsys_nsubcalls, shift_style);
1467 break;
1468#endif
1469 }
1470
1471 internal_syscall(tcp);
1472 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1473 tcp->flags |= TCB_INSYSCALL;
1474 return 0;
1475 }
1476
1477 if (cflag) {
1478 gettimeofday(&tcp->etime, NULL);
1479 tcp->flags |= TCB_INSYSCALL;
1480 return 0;
1481 }
1482
1483 printleader(tcp);
1484 tcp->flags &= ~TCB_REPRINT;
1485 tcp_last = tcp;
1486 if (tcp->scno >= nsyscalls)
1487 tprintf("syscall_%lu(", tcp->scno);
1488 else
1489 tprintf("%s(", sysent[tcp->scno].sys_name);
1490 if (tcp->scno >= nsyscalls ||
1491 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1492 sys_res = printargs(tcp);
1493 else
1494 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1495 if (fflush(tcp->outf) == EOF)
1496 return -1;
1497 tcp->flags |= TCB_INSYSCALL;
1498 /* Measure the entrance time as late as possible to avoid errors. */
1499 if (dtime)
1500 gettimeofday(&tcp->etime, NULL);
1501 return sys_res;
1502}
1503
1504int
1505printargs(tcp)
1506struct tcb *tcp;
1507{
1508 if (entering(tcp)) {
1509 int i;
1510
1511 for (i = 0; i < tcp->u_nargs; i++)
1512 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1513 }
1514 return 0;
1515}
1516
1517long
1518getrval2(tcp)
1519struct tcb *tcp;
1520{
1521 long val = -1;
1522
1523#ifdef LINUX
1524#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001525 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001526 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1527 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001528 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001529#endif /* SPARC */
1530#endif /* LINUX */
1531
1532#ifdef SUNOS4
1533 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1534 return -1;
1535#endif /* SUNOS4 */
1536
1537#ifdef SVR4
1538#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001539 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001540#endif /* SPARC */
1541#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001542 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001543#endif /* I386 */
1544#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001545 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001546#endif /* MIPS */
1547#endif /* SVR4 */
1548
1549 return val;
1550}
1551
1552/*
1553 * Apparently, indirect system calls have already be converted by ptrace(2),
1554 * so if you see "indir" this program has gone astray.
1555 */
1556int
1557sys_indir(tcp)
1558struct tcb *tcp;
1559{
1560 int i, scno, nargs;
1561
1562 if (entering(tcp)) {
1563 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1564 fprintf(stderr, "Bogus syscall: %u\n", scno);
1565 return 0;
1566 }
1567 nargs = sysent[scno].nargs;
1568 tprintf("%s", sysent[scno].sys_name);
1569 for (i = 0; i < nargs; i++)
1570 tprintf(", %#lx", tcp->u_arg[i+1]);
1571 }
1572 return 0;
1573}
1574
1575static int
1576time_cmp(a, b)
1577void *a;
1578void *b;
1579{
1580 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1581}
1582
1583static int
1584syscall_cmp(a, b)
1585void *a;
1586void *b;
1587{
1588 return strcmp(sysent[*((int *) a)].sys_name,
1589 sysent[*((int *) b)].sys_name);
1590}
1591
1592static int
1593count_cmp(a, b)
1594void *a;
1595void *b;
1596{
1597 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1598
1599 return (m < n) ? 1 : (m > n) ? -1 : 0;
1600}
1601
1602static int (*sortfun)();
1603static struct timeval overhead = { -1, -1 };
1604
1605void
1606set_sortby(sortby)
1607char *sortby;
1608{
1609 if (strcmp(sortby, "time") == 0)
1610 sortfun = time_cmp;
1611 else if (strcmp(sortby, "calls") == 0)
1612 sortfun = count_cmp;
1613 else if (strcmp(sortby, "name") == 0)
1614 sortfun = syscall_cmp;
1615 else if (strcmp(sortby, "nothing") == 0)
1616 sortfun = NULL;
1617 else {
1618 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1619 exit(1);
1620 }
1621}
1622
1623void set_overhead(n)
1624int n;
1625{
1626 overhead.tv_sec = n / 1000000;
1627 overhead.tv_usec = n % 1000000;
1628}
1629
1630void
1631call_summary(outf)
1632FILE *outf;
1633{
1634 int i, j;
1635 int call_cum, error_cum;
1636 struct timeval tv_cum, dtv;
1637 double percent;
1638 char *dashes = "-------------------------";
1639 char error_str[16];
1640
1641 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1642 if (overhead.tv_sec == -1) {
1643 tv_mul(&overhead, &shortest, 8);
1644 tv_div(&overhead, &overhead, 10);
1645 }
1646 for (i = 0; i < nsyscalls; i++) {
1647 sorted_count[i] = i;
1648 if (call_count[i] == 0)
1649 continue;
1650 tv_mul(&dtv, &overhead, call_count[i]);
1651 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1652 call_cum += call_count[i];
1653 error_cum += error_count[i];
1654 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1655 }
1656 if (sortfun)
1657 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1658 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1659 "% time", "seconds", "usecs/call",
1660 "calls", "errors", "syscall");
1661 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1662 dashes, dashes, dashes, dashes, dashes, dashes);
1663 for (i = 0; i < nsyscalls; i++) {
1664 j = sorted_count[i];
1665 if (call_count[j] == 0)
1666 continue;
1667 tv_div(&dtv, &tv_count[j], call_count[j]);
1668 if (error_count[j])
1669 sprintf(error_str, "%d", error_count[j]);
1670 else
1671 error_str[0] = '\0';
1672 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1673 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1674 percent, (long) tv_count[j].tv_sec,
1675 (long) tv_count[j].tv_usec,
1676 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1677 call_count[j], error_str, sysent[j].sys_name);
1678 }
1679 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1680 dashes, dashes, dashes, dashes, dashes, dashes);
1681 if (error_cum)
1682 sprintf(error_str, "%d", error_cum);
1683 else
1684 error_str[0] = '\0';
1685 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1686 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1687 call_cum, error_str, "total");
1688}